blob: ae0e56fdb7429cf363f0ad8d60b91591434d8fbb [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
Al Viroe6305c42008-07-15 21:03:57 -0400188 mask &= MAY_READ | MAY_WRITE | MAY_EXEC;
189
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 if (current->fsuid == inode->i_uid)
191 mode >>= 6;
192 else {
193 if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) {
194 int error = check_acl(inode, mask);
195 if (error == -EACCES)
196 goto check_capabilities;
197 else if (error != -EAGAIN)
198 return error;
199 }
200
201 if (in_group_p(inode->i_gid))
202 mode >>= 3;
203 }
204
205 /*
206 * If the DACs are ok we don't need any capability check.
207 */
Al Viroe6305c42008-07-15 21:03:57 -0400208 if ((mask & ~mode) == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return 0;
210
211 check_capabilities:
212 /*
213 * Read/write DACs are always overridable.
214 * Executable DACs are overridable if at least one exec bit is set.
215 */
216 if (!(mask & MAY_EXEC) ||
217 (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode))
218 if (capable(CAP_DAC_OVERRIDE))
219 return 0;
220
221 /*
222 * Searching includes executable on directories, else just read.
223 */
224 if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE)))
225 if (capable(CAP_DAC_READ_SEARCH))
226 return 0;
227
228 return -EACCES;
229}
230
231int permission(struct inode *inode, int mask, struct nameidata *nd)
232{
Al Viroe6305c42008-07-15 21:03:57 -0400233 int retval;
Dave Hansenc7eb2662007-10-16 23:31:14 -0700234 struct vfsmount *mnt = NULL;
235
236 if (nd)
Jan Blunck4ac91372008-02-14 19:34:32 -0800237 mnt = nd->path.mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 if (mask & MAY_WRITE) {
Miklos Szeredi22590e42007-10-16 23:27:08 -0700240 umode_t mode = inode->i_mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 /*
243 * Nobody gets write access to a read-only fs.
244 */
245 if (IS_RDONLY(inode) &&
246 (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
247 return -EROFS;
248
249 /*
250 * Nobody gets write access to an immutable file.
251 */
252 if (IS_IMMUTABLE(inode))
253 return -EACCES;
254 }
255
Miklos Szeredi22590e42007-10-16 23:27:08 -0700256 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
257 /*
258 * MAY_EXEC on regular files is denied if the fs is mounted
259 * with the "noexec" flag.
260 */
Dave Hansenc7eb2662007-10-16 23:31:14 -0700261 if (mnt && (mnt->mnt_flags & MNT_NOEXEC))
Miklos Szeredi22590e42007-10-16 23:27:08 -0700262 return -EACCES;
263 }
Trond Myklebusta343bb72006-08-22 20:06:03 -0400264
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 /* Ordinary permission routines do not understand MAY_APPEND. */
Miklos Szeredi22590e42007-10-16 23:27:08 -0700266 if (inode->i_op && inode->i_op->permission) {
Al Viroe6305c42008-07-15 21:03:57 -0400267 int extra = 0;
268 if (nd) {
269 if (nd->flags & LOOKUP_ACCESS)
270 extra |= MAY_ACCESS;
271 if (nd->flags & LOOKUP_CHDIR)
272 extra |= MAY_CHDIR;
273 if (nd->flags & LOOKUP_OPEN)
274 extra |= MAY_OPEN;
275 }
276 retval = inode->i_op->permission(inode, mask | extra);
Miklos Szeredi22590e42007-10-16 23:27:08 -0700277 if (!retval) {
278 /*
279 * Exec permission on a regular file is denied if none
280 * of the execute bits are set.
281 *
282 * This check should be done by the ->permission()
283 * method.
284 */
285 if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode) &&
286 !(inode->i_mode & S_IXUGO))
287 return -EACCES;
288 }
289 } else {
Al Viroe6305c42008-07-15 21:03:57 -0400290 retval = generic_permission(inode, mask, NULL);
Miklos Szeredi22590e42007-10-16 23:27:08 -0700291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 if (retval)
293 return retval;
294
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -0700295 retval = devcgroup_inode_permission(inode, mask);
296 if (retval)
297 return retval;
298
Al Viroe6305c42008-07-15 21:03:57 -0400299 return security_inode_permission(inode,
300 mask & (MAY_READ|MAY_WRITE|MAY_EXEC), nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301}
302
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800303/**
304 * vfs_permission - check for access rights to a given path
305 * @nd: lookup result that describes the path
306 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
307 *
308 * Used to check for read/write/execute permissions on a path.
309 * We use "fsuid" for this, letting us set arbitrary permissions
310 * for filesystem access without changing the "normal" uids which
311 * are used for other things.
312 */
313int vfs_permission(struct nameidata *nd, int mask)
314{
Jan Blunck4ac91372008-02-14 19:34:32 -0800315 return permission(nd->path.dentry->d_inode, mask, nd);
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800316}
317
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800318/**
319 * file_permission - check for additional access rights to a given file
320 * @file: file to check access rights for
321 * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC)
322 *
323 * Used to check for read/write/execute permissions on an already opened
324 * file.
325 *
326 * Note:
327 * Do not use this function in new code. All access checks should
328 * be done using vfs_permission().
329 */
330int file_permission(struct file *file, int mask)
331{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800332 return permission(file->f_path.dentry->d_inode, mask, NULL);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -0800333}
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335/*
336 * get_write_access() gets write permission for a file.
337 * put_write_access() releases this write permission.
338 * This is used for regular files.
339 * We cannot support write (and maybe mmap read-write shared) accesses and
340 * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode
341 * can have the following values:
342 * 0: no writers, no VM_DENYWRITE mappings
343 * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist
344 * > 0: (i_writecount) users are writing to the file.
345 *
346 * Normally we operate on that counter with atomic_{inc,dec} and it's safe
347 * except for the cases where we don't hold i_writecount yet. Then we need to
348 * use {get,deny}_write_access() - these functions check the sign and refuse
349 * to do the change if sign is wrong. Exclusion between them is provided by
350 * the inode->i_lock spinlock.
351 */
352
353int get_write_access(struct inode * inode)
354{
355 spin_lock(&inode->i_lock);
356 if (atomic_read(&inode->i_writecount) < 0) {
357 spin_unlock(&inode->i_lock);
358 return -ETXTBSY;
359 }
360 atomic_inc(&inode->i_writecount);
361 spin_unlock(&inode->i_lock);
362
363 return 0;
364}
365
366int deny_write_access(struct file * file)
367{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800368 struct inode *inode = file->f_path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
370 spin_lock(&inode->i_lock);
371 if (atomic_read(&inode->i_writecount) > 0) {
372 spin_unlock(&inode->i_lock);
373 return -ETXTBSY;
374 }
375 atomic_dec(&inode->i_writecount);
376 spin_unlock(&inode->i_lock);
377
378 return 0;
379}
380
Jan Blunck1d957f92008-02-14 19:34:35 -0800381/**
Jan Blunck5dd784d2008-02-14 19:34:38 -0800382 * path_get - get a reference to a path
383 * @path: path to get the reference to
384 *
385 * Given a path increment the reference count to the dentry and the vfsmount.
386 */
387void path_get(struct path *path)
388{
389 mntget(path->mnt);
390 dget(path->dentry);
391}
392EXPORT_SYMBOL(path_get);
393
394/**
Jan Blunck1d957f92008-02-14 19:34:35 -0800395 * path_put - put a reference to a path
396 * @path: path to put the reference to
397 *
398 * Given a path decrement the reference count to the dentry and the vfsmount.
399 */
400void path_put(struct path *path)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401{
Jan Blunck1d957f92008-02-14 19:34:35 -0800402 dput(path->dentry);
403 mntput(path->mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404}
Jan Blunck1d957f92008-02-14 19:34:35 -0800405EXPORT_SYMBOL(path_put);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
Trond Myklebust834f2a42005-10-18 14:20:16 -0700407/**
408 * release_open_intent - free up open intent resources
409 * @nd: pointer to nameidata
410 */
411void release_open_intent(struct nameidata *nd)
412{
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -0800413 if (nd->intent.open.file->f_path.dentry == NULL)
Trond Myklebust834f2a42005-10-18 14:20:16 -0700414 put_filp(nd->intent.open.file);
415 else
416 fput(nd->intent.open.file);
417}
418
Ian Kentbcdc5e02006-09-27 01:50:44 -0700419static inline struct dentry *
420do_revalidate(struct dentry *dentry, struct nameidata *nd)
421{
422 int status = dentry->d_op->d_revalidate(dentry, nd);
423 if (unlikely(status <= 0)) {
424 /*
425 * The dentry failed validation.
426 * If d_revalidate returned 0 attempt to invalidate
427 * the dentry otherwise d_revalidate is asking us
428 * to return a fail status.
429 */
430 if (!status) {
431 if (!d_invalidate(dentry)) {
432 dput(dentry);
433 dentry = NULL;
434 }
435 } else {
436 dput(dentry);
437 dentry = ERR_PTR(status);
438 }
439 }
440 return dentry;
441}
442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443/*
444 * Internal lookup() using the new generic dcache.
445 * SMP-safe
446 */
447static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
448{
449 struct dentry * dentry = __d_lookup(parent, name);
450
451 /* lockess __d_lookup may fail due to concurrent d_move()
452 * in some unrelated directory, so try with d_lookup
453 */
454 if (!dentry)
455 dentry = d_lookup(parent, name);
456
Ian Kentbcdc5e02006-09-27 01:50:44 -0700457 if (dentry && dentry->d_op && dentry->d_op->d_revalidate)
458 dentry = do_revalidate(dentry, nd);
459
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return dentry;
461}
462
463/*
464 * Short-cut version of permission(), for calling by
465 * path_walk(), when dcache lock is held. Combines parts
466 * of permission() and generic_permission(), and tests ONLY for
467 * MAY_EXEC permission.
468 *
469 * If appropriate, check DAC only. If not appropriate, or
470 * short-cut DAC fails, then call permission() to do more
471 * complete permission check.
472 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800473static int exec_permission_lite(struct inode *inode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 struct nameidata *nd)
475{
476 umode_t mode = inode->i_mode;
477
478 if (inode->i_op && inode->i_op->permission)
479 return -EAGAIN;
480
481 if (current->fsuid == inode->i_uid)
482 mode >>= 6;
483 else if (in_group_p(inode->i_gid))
484 mode >>= 3;
485
486 if (mode & MAY_EXEC)
487 goto ok;
488
489 if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE))
490 goto ok;
491
492 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE))
493 goto ok;
494
495 if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH))
496 goto ok;
497
498 return -EACCES;
499ok:
500 return security_inode_permission(inode, MAY_EXEC, nd);
501}
502
503/*
504 * This is called when everything else fails, and we actually have
505 * to go to the low-level filesystem to find out what we should do..
506 *
507 * We get the directory semaphore, and after getting that we also
508 * make sure that nobody added the entry to the dcache in the meantime..
509 * SMP-safe
510 */
511static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
512{
513 struct dentry * result;
514 struct inode *dir = parent->d_inode;
515
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800516 mutex_lock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 /*
518 * First re-do the cached lookup just in case it was created
519 * while we waited for the directory semaphore..
520 *
521 * FIXME! This could use version numbering or similar to
522 * avoid unnecessary cache lookups.
523 *
524 * The "dcache_lock" is purely to protect the RCU list walker
525 * from concurrent renames at this point (we mustn't get false
526 * negatives from the RCU list walk here, unlike the optimistic
527 * fast walk).
528 *
529 * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
530 */
531 result = d_lookup(parent, name);
532 if (!result) {
Miklos Szeredid70b67c2008-07-02 21:30:15 +0200533 struct dentry *dentry;
534
535 /* Don't create child dentry for a dead directory. */
536 result = ERR_PTR(-ENOENT);
537 if (IS_DEADDIR(dir))
538 goto out_unlock;
539
540 dentry = d_alloc(parent, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 result = ERR_PTR(-ENOMEM);
542 if (dentry) {
543 result = dir->i_op->lookup(dir, dentry, nd);
544 if (result)
545 dput(dentry);
546 else
547 result = dentry;
548 }
Miklos Szeredid70b67c2008-07-02 21:30:15 +0200549out_unlock:
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800550 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return result;
552 }
553
554 /*
555 * Uhhuh! Nasty case: the cache was re-populated while
556 * we waited on the semaphore. Need to revalidate.
557 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -0800558 mutex_unlock(&dir->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (result->d_op && result->d_op->d_revalidate) {
Ian Kentbcdc5e02006-09-27 01:50:44 -0700560 result = do_revalidate(result, nd);
561 if (!result)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 result = ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 }
564 return result;
565}
566
567static int __emul_lookup_dentry(const char *, struct nameidata *);
568
569/* SMP-safe */
Arjan van de Venf1662352006-01-14 13:21:31 -0800570static __always_inline int
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571walk_init_root(const char *name, struct nameidata *nd)
572{
Andreas Mohre518ddb2006-09-29 02:01:22 -0700573 struct fs_struct *fs = current->fs;
574
575 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -0800576 if (fs->altroot.dentry && !(nd->flags & LOOKUP_NOALT)) {
577 nd->path = fs->altroot;
578 path_get(&fs->altroot);
Andreas Mohre518ddb2006-09-29 02:01:22 -0700579 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 if (__emul_lookup_dentry(name,nd))
581 return 0;
Andreas Mohre518ddb2006-09-29 02:01:22 -0700582 read_lock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
Jan Blunck6ac08c32008-02-14 19:34:38 -0800584 nd->path = fs->root;
585 path_get(&fs->root);
Andreas Mohre518ddb2006-09-29 02:01:22 -0700586 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 return 1;
588}
589
Al Viroa02f76c2008-02-23 15:14:28 +0000590/*
591 * Wrapper to retry pathname resolution whenever the underlying
592 * file system returns an ESTALE.
593 *
594 * Retry the whole path once, forcing real lookup requests
595 * instead of relying on the dcache.
596 */
597static __always_inline int link_path_walk(const char *name, struct nameidata *nd)
598{
599 struct path save = nd->path;
600 int result;
601
602 /* make sure the stuff we saved doesn't go away */
Jan Blunckc8e7f442008-06-09 16:40:35 -0700603 path_get(&save);
Al Viroa02f76c2008-02-23 15:14:28 +0000604
605 result = __link_path_walk(name, nd);
606 if (result == -ESTALE) {
607 /* nd->path had been dropped */
608 nd->path = save;
Jan Blunckc8e7f442008-06-09 16:40:35 -0700609 path_get(&nd->path);
Al Viroa02f76c2008-02-23 15:14:28 +0000610 nd->flags |= LOOKUP_REVAL;
611 result = __link_path_walk(name, nd);
612 }
613
614 path_put(&save);
615
616 return result;
617}
618
Arjan van de Venf1662352006-01-14 13:21:31 -0800619static __always_inline int __vfs_follow_link(struct nameidata *nd, const char *link)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
621 int res = 0;
622 char *name;
623 if (IS_ERR(link))
624 goto fail;
625
626 if (*link == '/') {
Jan Blunck1d957f92008-02-14 19:34:35 -0800627 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (!walk_init_root(link, nd))
629 /* weird __emul_prefix() stuff did it */
630 goto out;
631 }
632 res = link_path_walk(link, nd);
633out:
634 if (nd->depth || res || nd->last_type!=LAST_NORM)
635 return res;
636 /*
637 * If it is an iterative symlinks resolution in open_namei() we
638 * have to copy the last component. And all that crap because of
639 * bloody create() on broken symlinks. Furrfu...
640 */
641 name = __getname();
642 if (unlikely(!name)) {
Jan Blunck1d957f92008-02-14 19:34:35 -0800643 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return -ENOMEM;
645 }
646 strcpy(name, nd->last.name);
647 nd->last.name = name;
648 return 0;
649fail:
Jan Blunck1d957f92008-02-14 19:34:35 -0800650 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 return PTR_ERR(link);
652}
653
Jan Blunck1d957f92008-02-14 19:34:35 -0800654static void path_put_conditional(struct path *path, struct nameidata *nd)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700655{
656 dput(path->dentry);
Jan Blunck4ac91372008-02-14 19:34:32 -0800657 if (path->mnt != nd->path.mnt)
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700658 mntput(path->mnt);
659}
660
661static inline void path_to_nameidata(struct path *path, struct nameidata *nd)
662{
Jan Blunck4ac91372008-02-14 19:34:32 -0800663 dput(nd->path.dentry);
664 if (nd->path.mnt != path->mnt)
665 mntput(nd->path.mnt);
666 nd->path.mnt = path->mnt;
667 nd->path.dentry = path->dentry;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700668}
669
Ian Kent051d3812006-03-27 01:14:53 -0800670static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
671{
672 int error;
673 void *cookie;
674 struct dentry *dentry = path->dentry;
675
676 touch_atime(path->mnt, dentry);
677 nd_set_link(nd, NULL);
678
Jan Blunck4ac91372008-02-14 19:34:32 -0800679 if (path->mnt != nd->path.mnt) {
Ian Kent051d3812006-03-27 01:14:53 -0800680 path_to_nameidata(path, nd);
681 dget(dentry);
682 }
683 mntget(path->mnt);
684 cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
685 error = PTR_ERR(cookie);
686 if (!IS_ERR(cookie)) {
687 char *s = nd_get_link(nd);
688 error = 0;
689 if (s)
690 error = __vfs_follow_link(nd, s);
691 if (dentry->d_inode->i_op->put_link)
692 dentry->d_inode->i_op->put_link(dentry, nd, cookie);
693 }
Jan Blunck09da59162008-02-14 19:34:37 -0800694 path_put(path);
Ian Kent051d3812006-03-27 01:14:53 -0800695
696 return error;
697}
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699/*
700 * This limits recursive symlink follows to 8, while
701 * limiting consecutive symlinks to 40.
702 *
703 * Without that kind of total limit, nasty chains of consecutive
704 * symlinks can cause almost arbitrarily long lookups.
705 */
Al Viro90ebe562005-06-06 13:35:58 -0700706static inline int do_follow_link(struct path *path, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707{
708 int err = -ELOOP;
709 if (current->link_count >= MAX_NESTED_LINKS)
710 goto loop;
711 if (current->total_link_count >= 40)
712 goto loop;
713 BUG_ON(nd->depth >= MAX_NESTED_LINKS);
714 cond_resched();
Al Viro90ebe562005-06-06 13:35:58 -0700715 err = security_inode_follow_link(path->dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (err)
717 goto loop;
718 current->link_count++;
719 current->total_link_count++;
720 nd->depth++;
Al Virocd4e91d2005-06-06 13:36:03 -0700721 err = __do_follow_link(path, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 current->link_count--;
723 nd->depth--;
724 return err;
725loop:
Jan Blunck1d957f92008-02-14 19:34:35 -0800726 path_put_conditional(path, nd);
727 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 return err;
729}
730
731int follow_up(struct vfsmount **mnt, struct dentry **dentry)
732{
733 struct vfsmount *parent;
734 struct dentry *mountpoint;
735 spin_lock(&vfsmount_lock);
736 parent=(*mnt)->mnt_parent;
737 if (parent == *mnt) {
738 spin_unlock(&vfsmount_lock);
739 return 0;
740 }
741 mntget(parent);
742 mountpoint=dget((*mnt)->mnt_mountpoint);
743 spin_unlock(&vfsmount_lock);
744 dput(*dentry);
745 *dentry = mountpoint;
746 mntput(*mnt);
747 *mnt = parent;
748 return 1;
749}
750
751/* no need for dcache_lock, as serialization is taken care in
752 * namespace.c
753 */
Al Viro463ffb22005-06-06 13:36:05 -0700754static int __follow_mount(struct path *path)
755{
756 int res = 0;
757 while (d_mountpoint(path->dentry)) {
758 struct vfsmount *mounted = lookup_mnt(path->mnt, path->dentry);
759 if (!mounted)
760 break;
761 dput(path->dentry);
762 if (res)
763 mntput(path->mnt);
764 path->mnt = mounted;
765 path->dentry = dget(mounted->mnt_root);
766 res = 1;
767 }
768 return res;
769}
770
Al Viro58c465e2005-06-06 13:36:13 -0700771static void follow_mount(struct vfsmount **mnt, struct dentry **dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 while (d_mountpoint(*dentry)) {
774 struct vfsmount *mounted = lookup_mnt(*mnt, *dentry);
775 if (!mounted)
776 break;
Al Viro58c465e2005-06-06 13:36:13 -0700777 dput(*dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 mntput(*mnt);
779 *mnt = mounted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 *dentry = dget(mounted->mnt_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782}
783
784/* no need for dcache_lock, as serialization is taken care in
785 * namespace.c
786 */
Al Viroe13b2102005-06-06 13:36:06 -0700787int follow_down(struct vfsmount **mnt, struct dentry **dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 struct vfsmount *mounted;
790
791 mounted = lookup_mnt(*mnt, *dentry);
792 if (mounted) {
Al Viroe13b2102005-06-06 13:36:06 -0700793 dput(*dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 mntput(*mnt);
795 *mnt = mounted;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 *dentry = dget(mounted->mnt_root);
797 return 1;
798 }
799 return 0;
800}
801
Arjan van de Venf1662352006-01-14 13:21:31 -0800802static __always_inline void follow_dotdot(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Andreas Mohre518ddb2006-09-29 02:01:22 -0700804 struct fs_struct *fs = current->fs;
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 while(1) {
807 struct vfsmount *parent;
Jan Blunck4ac91372008-02-14 19:34:32 -0800808 struct dentry *old = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Andreas Mohre518ddb2006-09-29 02:01:22 -0700810 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -0800811 if (nd->path.dentry == fs->root.dentry &&
812 nd->path.mnt == fs->root.mnt) {
Andreas Mohre518ddb2006-09-29 02:01:22 -0700813 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 break;
815 }
Andreas Mohre518ddb2006-09-29 02:01:22 -0700816 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 spin_lock(&dcache_lock);
Jan Blunck4ac91372008-02-14 19:34:32 -0800818 if (nd->path.dentry != nd->path.mnt->mnt_root) {
819 nd->path.dentry = dget(nd->path.dentry->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 spin_unlock(&dcache_lock);
821 dput(old);
822 break;
823 }
824 spin_unlock(&dcache_lock);
825 spin_lock(&vfsmount_lock);
Jan Blunck4ac91372008-02-14 19:34:32 -0800826 parent = nd->path.mnt->mnt_parent;
827 if (parent == nd->path.mnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 spin_unlock(&vfsmount_lock);
829 break;
830 }
831 mntget(parent);
Jan Blunck4ac91372008-02-14 19:34:32 -0800832 nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 spin_unlock(&vfsmount_lock);
834 dput(old);
Jan Blunck4ac91372008-02-14 19:34:32 -0800835 mntput(nd->path.mnt);
836 nd->path.mnt = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
Jan Blunck4ac91372008-02-14 19:34:32 -0800838 follow_mount(&nd->path.mnt, &nd->path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841/*
842 * It's more convoluted than I'd like it to be, but... it's still fairly
843 * small and for now I'd prefer to have fast path as straight as possible.
844 * It _is_ time-critical.
845 */
846static int do_lookup(struct nameidata *nd, struct qstr *name,
847 struct path *path)
848{
Jan Blunck4ac91372008-02-14 19:34:32 -0800849 struct vfsmount *mnt = nd->path.mnt;
850 struct dentry *dentry = __d_lookup(nd->path.dentry, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 if (!dentry)
853 goto need_lookup;
854 if (dentry->d_op && dentry->d_op->d_revalidate)
855 goto need_revalidate;
856done:
857 path->mnt = mnt;
858 path->dentry = dentry;
Al Viro634ee702005-06-06 13:36:13 -0700859 __follow_mount(path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 return 0;
861
862need_lookup:
Jan Blunck4ac91372008-02-14 19:34:32 -0800863 dentry = real_lookup(nd->path.dentry, name, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 if (IS_ERR(dentry))
865 goto fail;
866 goto done;
867
868need_revalidate:
Ian Kentbcdc5e02006-09-27 01:50:44 -0700869 dentry = do_revalidate(dentry, nd);
870 if (!dentry)
871 goto need_lookup;
872 if (IS_ERR(dentry))
873 goto fail;
874 goto done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876fail:
877 return PTR_ERR(dentry);
878}
879
880/*
881 * Name resolution.
Prasanna Medaea3834d2005-04-29 16:00:17 +0100882 * This is the basic name resolution function, turning a pathname into
883 * the final dentry. We expect 'base' to be positive and a directory.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 *
Prasanna Medaea3834d2005-04-29 16:00:17 +0100885 * Returns 0 and nd will have valid dentry and mnt on success.
886 * Returns error and drops reference to input namei data on failure.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -0800888static int __link_path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889{
890 struct path next;
891 struct inode *inode;
892 int err;
893 unsigned int lookup_flags = nd->flags;
894
895 while (*name=='/')
896 name++;
897 if (!*name)
898 goto return_reval;
899
Jan Blunck4ac91372008-02-14 19:34:32 -0800900 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 if (nd->depth)
Trond Myklebustf55eab82006-02-04 23:28:01 -0800902 lookup_flags = LOOKUP_FOLLOW | (nd->flags & LOOKUP_CONTINUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 /* At this point we know we have a real path component. */
905 for(;;) {
906 unsigned long hash;
907 struct qstr this;
908 unsigned int c;
909
Trond Myklebustcdce5d62005-10-18 14:20:18 -0700910 nd->flags |= LOOKUP_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 err = exec_permission_lite(inode, nd);
Christoph Hellwige4543ed2005-11-08 21:35:04 -0800912 if (err == -EAGAIN)
913 err = vfs_permission(nd, MAY_EXEC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (err)
915 break;
916
917 this.name = name;
918 c = *(const unsigned char *)name;
919
920 hash = init_name_hash();
921 do {
922 name++;
923 hash = partial_name_hash(c, hash);
924 c = *(const unsigned char *)name;
925 } while (c && (c != '/'));
926 this.len = name - (const char *) this.name;
927 this.hash = end_name_hash(hash);
928
929 /* remove trailing slashes? */
930 if (!c)
931 goto last_component;
932 while (*++name == '/');
933 if (!*name)
934 goto last_with_slashes;
935
936 /*
937 * "." and ".." are special - ".." especially so because it has
938 * to be able to know about the current root directory and
939 * parent relationships.
940 */
941 if (this.name[0] == '.') switch (this.len) {
942 default:
943 break;
944 case 2:
945 if (this.name[1] != '.')
946 break;
Al Viro58c465e2005-06-06 13:36:13 -0700947 follow_dotdot(nd);
Jan Blunck4ac91372008-02-14 19:34:32 -0800948 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 /* fallthrough */
950 case 1:
951 continue;
952 }
953 /*
954 * See if the low-level filesystem might want
955 * to use its own hash..
956 */
Jan Blunck4ac91372008-02-14 19:34:32 -0800957 if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
958 err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
959 &this);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 if (err < 0)
961 break;
962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 /* This does the actual lookups.. */
964 err = do_lookup(nd, &this, &next);
965 if (err)
966 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967
968 err = -ENOENT;
969 inode = next.dentry->d_inode;
970 if (!inode)
971 goto out_dput;
972 err = -ENOTDIR;
973 if (!inode->i_op)
974 goto out_dput;
975
976 if (inode->i_op->follow_link) {
Al Viro90ebe562005-06-06 13:35:58 -0700977 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 if (err)
979 goto return_err;
980 err = -ENOENT;
Jan Blunck4ac91372008-02-14 19:34:32 -0800981 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (!inode)
983 break;
984 err = -ENOTDIR;
985 if (!inode->i_op)
986 break;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -0700987 } else
988 path_to_nameidata(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 err = -ENOTDIR;
990 if (!inode->i_op->lookup)
991 break;
992 continue;
993 /* here ends the main loop */
994
995last_with_slashes:
996 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;
997last_component:
Trond Myklebustf55eab82006-02-04 23:28:01 -0800998 /* Clear LOOKUP_CONTINUE iff it was previously unset */
999 nd->flags &= lookup_flags | ~LOOKUP_CONTINUE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 if (lookup_flags & LOOKUP_PARENT)
1001 goto lookup_parent;
1002 if (this.name[0] == '.') switch (this.len) {
1003 default:
1004 break;
1005 case 2:
1006 if (this.name[1] != '.')
1007 break;
Al Viro58c465e2005-06-06 13:36:13 -07001008 follow_dotdot(nd);
Jan Blunck4ac91372008-02-14 19:34:32 -08001009 inode = nd->path.dentry->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 /* fallthrough */
1011 case 1:
1012 goto return_reval;
1013 }
Jan Blunck4ac91372008-02-14 19:34:32 -08001014 if (nd->path.dentry->d_op && nd->path.dentry->d_op->d_hash) {
1015 err = nd->path.dentry->d_op->d_hash(nd->path.dentry,
1016 &this);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (err < 0)
1018 break;
1019 }
1020 err = do_lookup(nd, &this, &next);
1021 if (err)
1022 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 inode = next.dentry->d_inode;
1024 if ((lookup_flags & LOOKUP_FOLLOW)
1025 && inode && inode->i_op && inode->i_op->follow_link) {
Al Viro90ebe562005-06-06 13:35:58 -07001026 err = do_follow_link(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 if (err)
1028 goto return_err;
Jan Blunck4ac91372008-02-14 19:34:32 -08001029 inode = nd->path.dentry->d_inode;
Miklos Szeredi09dd17d2005-09-06 15:18:21 -07001030 } else
1031 path_to_nameidata(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 err = -ENOENT;
1033 if (!inode)
1034 break;
1035 if (lookup_flags & LOOKUP_DIRECTORY) {
1036 err = -ENOTDIR;
1037 if (!inode->i_op || !inode->i_op->lookup)
1038 break;
1039 }
1040 goto return_base;
1041lookup_parent:
1042 nd->last = this;
1043 nd->last_type = LAST_NORM;
1044 if (this.name[0] != '.')
1045 goto return_base;
1046 if (this.len == 1)
1047 nd->last_type = LAST_DOT;
1048 else if (this.len == 2 && this.name[1] == '.')
1049 nd->last_type = LAST_DOTDOT;
1050 else
1051 goto return_base;
1052return_reval:
1053 /*
1054 * We bypassed the ordinary revalidation routines.
1055 * We may need to check the cached dentry for staleness.
1056 */
Jan Blunck4ac91372008-02-14 19:34:32 -08001057 if (nd->path.dentry && nd->path.dentry->d_sb &&
1058 (nd->path.dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 err = -ESTALE;
1060 /* Note: we do not d_invalidate() */
Jan Blunck4ac91372008-02-14 19:34:32 -08001061 if (!nd->path.dentry->d_op->d_revalidate(
1062 nd->path.dentry, nd))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 break;
1064 }
1065return_base:
1066 return 0;
1067out_dput:
Jan Blunck1d957f92008-02-14 19:34:35 -08001068 path_put_conditional(&next, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 break;
1070 }
Jan Blunck1d957f92008-02-14 19:34:35 -08001071 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072return_err:
1073 return err;
1074}
1075
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001076static int path_walk(const char *name, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
1078 current->total_link_count = 0;
1079 return link_path_walk(name, nd);
1080}
1081
Prasanna Medaea3834d2005-04-29 16:00:17 +01001082/*
1083 * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if
1084 * everything is done. Returns 0 and drops input nd, if lookup failed;
1085 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086static int __emul_lookup_dentry(const char *name, struct nameidata *nd)
1087{
1088 if (path_walk(name, nd))
1089 return 0; /* something went wrong... */
1090
Jan Blunck4ac91372008-02-14 19:34:32 -08001091 if (!nd->path.dentry->d_inode ||
1092 S_ISDIR(nd->path.dentry->d_inode->i_mode)) {
Jan Blunck09da59162008-02-14 19:34:37 -08001093 struct path old_path = nd->path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 struct qstr last = nd->last;
1095 int last_type = nd->last_type;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001096 struct fs_struct *fs = current->fs;
1097
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 /*
Andreas Mohre518ddb2006-09-29 02:01:22 -07001099 * NAME was not found in alternate root or it's a directory.
1100 * Try to find it in the normal root:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 */
1102 nd->last_type = LAST_ROOT;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001103 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08001104 nd->path = fs->root;
1105 path_get(&fs->root);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001106 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 if (path_walk(name, nd) == 0) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001108 if (nd->path.dentry->d_inode) {
Jan Blunck09da59162008-02-14 19:34:37 -08001109 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return 1;
1111 }
Jan Blunck1d957f92008-02-14 19:34:35 -08001112 path_put(&nd->path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 }
Jan Blunck09da59162008-02-14 19:34:37 -08001114 nd->path = old_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 nd->last = last;
1116 nd->last_type = last_type;
1117 }
1118 return 1;
1119}
1120
1121void set_fs_altroot(void)
1122{
1123 char *emul = __emul_prefix();
1124 struct nameidata nd;
Jan Blunck6ac08c32008-02-14 19:34:38 -08001125 struct path path = {}, old_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 int err;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001127 struct fs_struct *fs = current->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
1129 if (!emul)
1130 goto set_it;
1131 err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd);
Jan Blunck6ac08c32008-02-14 19:34:38 -08001132 if (!err)
1133 path = nd.path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134set_it:
Andreas Mohre518ddb2006-09-29 02:01:22 -07001135 write_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08001136 old_path = fs->altroot;
1137 fs->altroot = path;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001138 write_unlock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08001139 if (old_path.dentry)
1140 path_put(&old_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
Prasanna Medaea3834d2005-04-29 16:00:17 +01001143/* Returns 0 and nd will be valid on success; Retuns error, otherwise. */
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001144static int do_path_lookup(int dfd, const char *name,
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001145 unsigned int flags, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146{
Prasanna Medaea3834d2005-04-29 16:00:17 +01001147 int retval = 0;
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001148 int fput_needed;
1149 struct file *file;
Andreas Mohre518ddb2006-09-29 02:01:22 -07001150 struct fs_struct *fs = current->fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152 nd->last_type = LAST_ROOT; /* if there are only slashes... */
1153 nd->flags = flags;
1154 nd->depth = 0;
1155
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 if (*name=='/') {
Andreas Mohre518ddb2006-09-29 02:01:22 -07001157 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08001158 if (fs->altroot.dentry && !(nd->flags & LOOKUP_NOALT)) {
1159 nd->path = fs->altroot;
1160 path_get(&fs->altroot);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001161 read_unlock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162 if (__emul_lookup_dentry(name,nd))
Prasanna Medaea3834d2005-04-29 16:00:17 +01001163 goto out; /* found in altroot */
Andreas Mohre518ddb2006-09-29 02:01:22 -07001164 read_lock(&fs->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 }
Jan Blunck6ac08c32008-02-14 19:34:38 -08001166 nd->path = fs->root;
1167 path_get(&fs->root);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001168 read_unlock(&fs->lock);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001169 } else if (dfd == AT_FDCWD) {
Andreas Mohre518ddb2006-09-29 02:01:22 -07001170 read_lock(&fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08001171 nd->path = fs->pwd;
1172 path_get(&fs->pwd);
Andreas Mohre518ddb2006-09-29 02:01:22 -07001173 read_unlock(&fs->lock);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001174 } else {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001175 struct dentry *dentry;
1176
1177 file = fget_light(dfd, &fput_needed);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001178 retval = -EBADF;
1179 if (!file)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001180 goto out_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001181
Josef "Jeff" Sipek0f7fc9e2006-12-08 02:36:35 -08001182 dentry = file->f_path.dentry;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001183
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001184 retval = -ENOTDIR;
1185 if (!S_ISDIR(dentry->d_inode->i_mode))
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001186 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001187
1188 retval = file_permission(file, MAY_EXEC);
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001189 if (retval)
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001190 goto fput_fail;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001191
Jan Blunck5dd784d2008-02-14 19:34:38 -08001192 nd->path = file->f_path;
1193 path_get(&file->f_path);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001194
1195 fput_light(file, fput_needed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 }
Josef 'Jeff' Sipek2dfdd262007-05-09 02:33:41 -07001197
1198 retval = path_walk(name, nd);
Prasanna Medaea3834d2005-04-29 16:00:17 +01001199out:
Jan Blunck4ac91372008-02-14 19:34:32 -08001200 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1201 nd->path.dentry->d_inode))
1202 audit_inode(name, nd->path.dentry);
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001203out_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001204 return retval;
1205
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001206fput_fail:
Ulrich Drepper170aa3d2006-02-04 23:28:02 -08001207 fput_light(file, fput_needed);
Trond Myklebust6d09bb62006-06-04 02:51:37 -07001208 goto out_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001211int path_lookup(const char *name, unsigned int flags,
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001212 struct nameidata *nd)
1213{
1214 return do_path_lookup(AT_FDCWD, name, flags, nd);
1215}
1216
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001217/**
1218 * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair
1219 * @dentry: pointer to dentry of the base directory
1220 * @mnt: pointer to vfs mount of the base directory
1221 * @name: pointer to file name
1222 * @flags: lookup flags
1223 * @nd: pointer to nameidata
1224 */
1225int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt,
1226 const char *name, unsigned int flags,
1227 struct nameidata *nd)
1228{
1229 int retval;
1230
1231 /* same as do_path_lookup */
1232 nd->last_type = LAST_ROOT;
1233 nd->flags = flags;
1234 nd->depth = 0;
1235
Jan Blunckc8e7f442008-06-09 16:40:35 -07001236 nd->path.dentry = dentry;
1237 nd->path.mnt = mnt;
1238 path_get(&nd->path);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001239
1240 retval = path_walk(name, nd);
Jan Blunck4ac91372008-02-14 19:34:32 -08001241 if (unlikely(!retval && !audit_dummy_context() && nd->path.dentry &&
1242 nd->path.dentry->d_inode))
1243 audit_inode(name, nd->path.dentry);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07001244
1245 return retval;
1246
1247}
1248
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001249static int __path_lookup_intent_open(int dfd, const char *name,
1250 unsigned int lookup_flags, struct nameidata *nd,
1251 int open_flags, int create_mode)
Trond Myklebust834f2a42005-10-18 14:20:16 -07001252{
1253 struct file *filp = get_empty_filp();
1254 int err;
1255
1256 if (filp == NULL)
1257 return -ENFILE;
1258 nd->intent.open.file = filp;
1259 nd->intent.open.flags = open_flags;
1260 nd->intent.open.create_mode = create_mode;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001261 err = do_path_lookup(dfd, name, lookup_flags|LOOKUP_OPEN, nd);
Trond Myklebust834f2a42005-10-18 14:20:16 -07001262 if (IS_ERR(nd->intent.open.file)) {
1263 if (err == 0) {
1264 err = PTR_ERR(nd->intent.open.file);
Jan Blunck1d957f92008-02-14 19:34:35 -08001265 path_put(&nd->path);
Trond Myklebust834f2a42005-10-18 14:20:16 -07001266 }
1267 } else if (err != 0)
1268 release_open_intent(nd);
1269 return err;
1270}
1271
1272/**
1273 * path_lookup_open - lookup a file path with open intent
Martin Waitz7045f372006-02-01 03:06:57 -08001274 * @dfd: the directory to use as base, or AT_FDCWD
Trond Myklebust834f2a42005-10-18 14:20:16 -07001275 * @name: pointer to file name
1276 * @lookup_flags: lookup intent flags
1277 * @nd: pointer to nameidata
1278 * @open_flags: open intent flags
1279 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001280int path_lookup_open(int dfd, const char *name, unsigned int lookup_flags,
Trond Myklebust834f2a42005-10-18 14:20:16 -07001281 struct nameidata *nd, int open_flags)
1282{
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001283 return __path_lookup_intent_open(dfd, name, lookup_flags, nd,
Trond Myklebust834f2a42005-10-18 14:20:16 -07001284 open_flags, 0);
1285}
1286
1287/**
1288 * path_lookup_create - lookup a file path with open + create intent
Martin Waitz7045f372006-02-01 03:06:57 -08001289 * @dfd: the directory to use as base, or AT_FDCWD
Trond Myklebust834f2a42005-10-18 14:20:16 -07001290 * @name: pointer to file name
1291 * @lookup_flags: lookup intent flags
1292 * @nd: pointer to nameidata
1293 * @open_flags: open intent flags
1294 * @create_mode: create intent flags
1295 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001296static int path_lookup_create(int dfd, const char *name,
1297 unsigned int lookup_flags, struct nameidata *nd,
1298 int open_flags, int create_mode)
Trond Myklebust834f2a42005-10-18 14:20:16 -07001299{
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001300 return __path_lookup_intent_open(dfd, name, lookup_flags|LOOKUP_CREATE,
1301 nd, open_flags, create_mode);
Trond Myklebust834f2a42005-10-18 14:20:16 -07001302}
1303
1304int __user_path_lookup_open(const char __user *name, unsigned int lookup_flags,
1305 struct nameidata *nd, int open_flags)
1306{
1307 char *tmp = getname(name);
1308 int err = PTR_ERR(tmp);
1309
1310 if (!IS_ERR(tmp)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001311 err = __path_lookup_intent_open(AT_FDCWD, tmp, lookup_flags, nd, open_flags, 0);
Trond Myklebust834f2a42005-10-18 14:20:16 -07001312 putname(tmp);
1313 }
1314 return err;
1315}
1316
Christoph Hellwigeead1912007-10-16 23:25:38 -07001317static struct dentry *__lookup_hash(struct qstr *name,
1318 struct dentry *base, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
James Morris057f6c02007-04-26 00:12:05 -07001320 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321 struct inode *inode;
1322 int err;
1323
1324 inode = base->d_inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325
1326 /*
1327 * See if the low-level filesystem might want
1328 * to use its own hash..
1329 */
1330 if (base->d_op && base->d_op->d_hash) {
1331 err = base->d_op->d_hash(base, name);
1332 dentry = ERR_PTR(err);
1333 if (err < 0)
1334 goto out;
1335 }
1336
1337 dentry = cached_lookup(base, name, nd);
1338 if (!dentry) {
Miklos Szeredid70b67c2008-07-02 21:30:15 +02001339 struct dentry *new;
1340
1341 /* Don't create child dentry for a dead directory. */
1342 dentry = ERR_PTR(-ENOENT);
1343 if (IS_DEADDIR(inode))
1344 goto out;
1345
1346 new = d_alloc(base, name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 dentry = ERR_PTR(-ENOMEM);
1348 if (!new)
1349 goto out;
1350 dentry = inode->i_op->lookup(inode, new, nd);
1351 if (!dentry)
1352 dentry = new;
1353 else
1354 dput(new);
1355 }
1356out:
1357 return dentry;
1358}
1359
James Morris057f6c02007-04-26 00:12:05 -07001360/*
1361 * Restricted form of lookup. Doesn't follow links, single-component only,
1362 * needs parent already locked. Doesn't follow mounts.
1363 * SMP-safe.
1364 */
Adrian Bunka244e162006-03-31 02:32:11 -08001365static struct dentry *lookup_hash(struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366{
Christoph Hellwigeead1912007-10-16 23:25:38 -07001367 int err;
1368
Jan Blunck4ac91372008-02-14 19:34:32 -08001369 err = permission(nd->path.dentry->d_inode, MAY_EXEC, nd);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001370 if (err)
1371 return ERR_PTR(err);
Jan Blunck4ac91372008-02-14 19:34:32 -08001372 return __lookup_hash(&nd->last, nd->path.dentry, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373}
1374
Christoph Hellwigeead1912007-10-16 23:25:38 -07001375static int __lookup_one_len(const char *name, struct qstr *this,
1376 struct dentry *base, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
1378 unsigned long hash;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 unsigned int c;
1380
James Morris057f6c02007-04-26 00:12:05 -07001381 this->name = name;
1382 this->len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 if (!len)
James Morris057f6c02007-04-26 00:12:05 -07001384 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386 hash = init_name_hash();
1387 while (len--) {
1388 c = *(const unsigned char *)name++;
1389 if (c == '/' || c == '\0')
James Morris057f6c02007-04-26 00:12:05 -07001390 return -EACCES;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 hash = partial_name_hash(c, hash);
1392 }
James Morris057f6c02007-04-26 00:12:05 -07001393 this->hash = end_name_hash(hash);
1394 return 0;
1395}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396
Christoph Hellwigeead1912007-10-16 23:25:38 -07001397/**
Randy Dunlapa6b91912008-03-19 17:01:00 -07001398 * lookup_one_len - filesystem helper to lookup single pathname component
Christoph Hellwigeead1912007-10-16 23:25:38 -07001399 * @name: pathname component to lookup
1400 * @base: base directory to lookup from
1401 * @len: maximum length @len should be interpreted to
1402 *
Randy Dunlapa6b91912008-03-19 17:01:00 -07001403 * Note that this routine is purely a helper for filesystem usage and should
1404 * not be called by generic code. Also note that by using this function the
Christoph Hellwigeead1912007-10-16 23:25:38 -07001405 * nameidata argument is passed to the filesystem methods and a filesystem
1406 * using this helper needs to be prepared for that.
1407 */
James Morris057f6c02007-04-26 00:12:05 -07001408struct dentry *lookup_one_len(const char *name, struct dentry *base, int len)
1409{
1410 int err;
1411 struct qstr this;
1412
1413 err = __lookup_one_len(name, &this, base, len);
1414 if (err)
1415 return ERR_PTR(err);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001416
1417 err = permission(base->d_inode, MAY_EXEC, NULL);
1418 if (err)
1419 return ERR_PTR(err);
Christoph Hellwig49705b72005-11-08 21:35:06 -08001420 return __lookup_hash(&this, base, NULL);
James Morris057f6c02007-04-26 00:12:05 -07001421}
1422
Christoph Hellwigeead1912007-10-16 23:25:38 -07001423/**
1424 * lookup_one_noperm - bad hack for sysfs
1425 * @name: pathname component to lookup
1426 * @base: base directory to lookup from
1427 *
1428 * This is a variant of lookup_one_len that doesn't perform any permission
1429 * checks. It's a horrible hack to work around the braindead sysfs
1430 * architecture and should not be used anywhere else.
1431 *
1432 * DON'T USE THIS FUNCTION EVER, thanks.
1433 */
1434struct dentry *lookup_one_noperm(const char *name, struct dentry *base)
James Morris057f6c02007-04-26 00:12:05 -07001435{
1436 int err;
1437 struct qstr this;
1438
Christoph Hellwigeead1912007-10-16 23:25:38 -07001439 err = __lookup_one_len(name, &this, base, strlen(name));
James Morris057f6c02007-04-26 00:12:05 -07001440 if (err)
1441 return ERR_PTR(err);
Christoph Hellwigeead1912007-10-16 23:25:38 -07001442 return __lookup_hash(&this, base, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443}
1444
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001445int __user_walk_fd(int dfd, const char __user *name, unsigned flags,
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001446 struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447{
1448 char *tmp = getname(name);
1449 int err = PTR_ERR(tmp);
1450
1451 if (!IS_ERR(tmp)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001452 err = do_path_lookup(dfd, tmp, flags, nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453 putname(tmp);
1454 }
1455 return err;
1456}
1457
Harvey Harrisonfc9b52c2008-02-08 04:19:52 -08001458int __user_walk(const char __user *name, unsigned flags, struct nameidata *nd)
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001459{
1460 return __user_walk_fd(AT_FDCWD, name, flags, nd);
1461}
1462
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463/*
1464 * It's inline, so penalty for filesystems that don't use sticky bit is
1465 * minimal.
1466 */
1467static inline int check_sticky(struct inode *dir, struct inode *inode)
1468{
1469 if (!(dir->i_mode & S_ISVTX))
1470 return 0;
1471 if (inode->i_uid == current->fsuid)
1472 return 0;
1473 if (dir->i_uid == current->fsuid)
1474 return 0;
1475 return !capable(CAP_FOWNER);
1476}
1477
1478/*
1479 * Check whether we can remove a link victim from directory dir, check
1480 * whether the type of victim is right.
1481 * 1. We can't do it if dir is read-only (done in permission())
1482 * 2. We should have write and exec permissions on dir
1483 * 3. We can't remove anything from append-only dir
1484 * 4. We can't do anything with immutable dir (done in permission())
1485 * 5. If the sticky bit on dir is set we should either
1486 * a. be owner of dir, or
1487 * b. be owner of victim, or
1488 * c. have CAP_FOWNER capability
1489 * 6. If the victim is append-only or immutable we can't do antyhing with
1490 * links pointing to it.
1491 * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR.
1492 * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR.
1493 * 9. We can't remove a root or mountpoint.
1494 * 10. We don't allow removal of NFS sillyrenamed files; it's handled by
1495 * nfs_async_unlink().
1496 */
Arjan van de Ven858119e2006-01-14 13:20:43 -08001497static int may_delete(struct inode *dir,struct dentry *victim,int isdir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498{
1499 int error;
1500
1501 if (!victim->d_inode)
1502 return -ENOENT;
1503
1504 BUG_ON(victim->d_parent->d_inode != dir);
Al Viro5a190ae2007-06-07 12:19:32 -04001505 audit_inode_child(victim->d_name.name, victim, dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
1507 error = permission(dir,MAY_WRITE | MAY_EXEC, NULL);
1508 if (error)
1509 return error;
1510 if (IS_APPEND(dir))
1511 return -EPERM;
1512 if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)||
1513 IS_IMMUTABLE(victim->d_inode))
1514 return -EPERM;
1515 if (isdir) {
1516 if (!S_ISDIR(victim->d_inode->i_mode))
1517 return -ENOTDIR;
1518 if (IS_ROOT(victim))
1519 return -EBUSY;
1520 } else if (S_ISDIR(victim->d_inode->i_mode))
1521 return -EISDIR;
1522 if (IS_DEADDIR(dir))
1523 return -ENOENT;
1524 if (victim->d_flags & DCACHE_NFSFS_RENAMED)
1525 return -EBUSY;
1526 return 0;
1527}
1528
1529/* Check whether we can create an object with dentry child in directory
1530 * dir.
1531 * 1. We can't do it if child already exists (open has special treatment for
1532 * this case, but since we are inlined it's OK)
1533 * 2. We can't do it if dir is read-only (done in permission())
1534 * 3. We should have write and exec permissions on dir
1535 * 4. We can't do it if dir is immutable (done in permission())
1536 */
1537static inline int may_create(struct inode *dir, struct dentry *child,
1538 struct nameidata *nd)
1539{
1540 if (child->d_inode)
1541 return -EEXIST;
1542 if (IS_DEADDIR(dir))
1543 return -ENOENT;
1544 return permission(dir,MAY_WRITE | MAY_EXEC, nd);
1545}
1546
1547/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548 * O_DIRECTORY translates into forcing a directory lookup.
1549 */
1550static inline int lookup_flags(unsigned int f)
1551{
1552 unsigned long retval = LOOKUP_FOLLOW;
1553
1554 if (f & O_NOFOLLOW)
1555 retval &= ~LOOKUP_FOLLOW;
1556
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 if (f & O_DIRECTORY)
1558 retval |= LOOKUP_DIRECTORY;
1559
1560 return retval;
1561}
1562
1563/*
1564 * p1 and p2 should be directories on the same fs.
1565 */
1566struct dentry *lock_rename(struct dentry *p1, struct dentry *p2)
1567{
1568 struct dentry *p;
1569
1570 if (p1 == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001571 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 return NULL;
1573 }
1574
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001575 mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001576
1577 for (p = p1; p->d_parent != p; p = p->d_parent) {
1578 if (p->d_parent == p2) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001579 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT);
1580 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return p;
1582 }
1583 }
1584
1585 for (p = p2; p->d_parent != p; p = p->d_parent) {
1586 if (p->d_parent == p1) {
Ingo Molnarf2eace22006-07-03 00:25:05 -07001587 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1588 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 return p;
1590 }
1591 }
1592
Ingo Molnarf2eace22006-07-03 00:25:05 -07001593 mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT);
1594 mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 return NULL;
1596}
1597
1598void unlock_rename(struct dentry *p1, struct dentry *p2)
1599{
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001600 mutex_unlock(&p1->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (p1 != p2) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001602 mutex_unlock(&p2->d_inode->i_mutex);
Arjan van de Vena11f3a02006-03-23 03:00:33 -08001603 mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
1605}
1606
1607int vfs_create(struct inode *dir, struct dentry *dentry, int mode,
1608 struct nameidata *nd)
1609{
1610 int error = may_create(dir, dentry, nd);
1611
1612 if (error)
1613 return error;
1614
1615 if (!dir->i_op || !dir->i_op->create)
1616 return -EACCES; /* shouldn't it be ENOSYS? */
1617 mode &= S_IALLUGO;
1618 mode |= S_IFREG;
1619 error = security_inode_create(dir, dentry, mode);
1620 if (error)
1621 return error;
1622 DQUOT_INIT(dir);
1623 error = dir->i_op->create(dir, dentry, mode, nd);
Stephen Smalleya74574a2005-09-09 13:01:44 -07001624 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00001625 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 return error;
1627}
1628
1629int may_open(struct nameidata *nd, int acc_mode, int flag)
1630{
Jan Blunck4ac91372008-02-14 19:34:32 -08001631 struct dentry *dentry = nd->path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 struct inode *inode = dentry->d_inode;
1633 int error;
1634
1635 if (!inode)
1636 return -ENOENT;
1637
1638 if (S_ISLNK(inode->i_mode))
1639 return -ELOOP;
1640
Linus Torvalds974a9f02008-01-12 14:06:34 -08001641 if (S_ISDIR(inode->i_mode) && (acc_mode & MAY_WRITE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 return -EISDIR;
1643
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 /*
1645 * FIFO's, sockets and device files are special: they don't
1646 * actually live on the filesystem itself, and as such you
1647 * can write to them even if the filesystem is read-only.
1648 */
1649 if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
1650 flag &= ~O_TRUNC;
1651 } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {
Jan Blunck4ac91372008-02-14 19:34:32 -08001652 if (nd->path.mnt->mnt_flags & MNT_NODEV)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 return -EACCES;
1654
1655 flag &= ~O_TRUNC;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001656 }
Dave Hansenb41572e2007-10-16 23:31:14 -07001657
1658 error = vfs_permission(nd, acc_mode);
1659 if (error)
1660 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 /*
1662 * An append-only file must be opened in append mode for writing.
1663 */
1664 if (IS_APPEND(inode)) {
1665 if ((flag & FMODE_WRITE) && !(flag & O_APPEND))
1666 return -EPERM;
1667 if (flag & O_TRUNC)
1668 return -EPERM;
1669 }
1670
1671 /* O_NOATIME can only be set by the owner or superuser */
1672 if (flag & O_NOATIME)
Satyam Sharma3bd858a2007-07-17 15:00:08 +05301673 if (!is_owner_or_cap(inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 return -EPERM;
1675
1676 /*
1677 * Ensure there are no outstanding leases on the file.
1678 */
1679 error = break_lease(inode, flag);
1680 if (error)
1681 return error;
1682
1683 if (flag & O_TRUNC) {
1684 error = get_write_access(inode);
1685 if (error)
1686 return error;
1687
1688 /*
1689 * Refuse to truncate files with mandatory locks held on them.
1690 */
1691 error = locks_verify_locked(inode);
1692 if (!error) {
1693 DQUOT_INIT(inode);
Miklos Szeredid139d7f2007-10-18 03:07:00 -07001694
1695 error = do_truncate(dentry, 0,
1696 ATTR_MTIME|ATTR_CTIME|ATTR_OPEN,
1697 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 }
1699 put_write_access(inode);
1700 if (error)
1701 return error;
1702 } else
1703 if (flag & FMODE_WRITE)
1704 DQUOT_INIT(inode);
1705
1706 return 0;
1707}
1708
Dave Hansend57999e2008-02-15 14:37:27 -08001709/*
1710 * Be careful about ever adding any more callers of this
1711 * function. Its flags must be in the namei format, not
1712 * what get passed to sys_open().
1713 */
1714static int __open_namei_create(struct nameidata *nd, struct path *path,
Dave Hansenaab520e2006-09-30 23:29:02 -07001715 int flag, int mode)
1716{
1717 int error;
Jan Blunck4ac91372008-02-14 19:34:32 -08001718 struct dentry *dir = nd->path.dentry;
Dave Hansenaab520e2006-09-30 23:29:02 -07001719
1720 if (!IS_POSIXACL(dir->d_inode))
1721 mode &= ~current->fs->umask;
1722 error = vfs_create(dir->d_inode, path->dentry, mode, nd);
1723 mutex_unlock(&dir->d_inode->i_mutex);
Jan Blunck4ac91372008-02-14 19:34:32 -08001724 dput(nd->path.dentry);
1725 nd->path.dentry = path->dentry;
Dave Hansenaab520e2006-09-30 23:29:02 -07001726 if (error)
1727 return error;
1728 /* Don't check for write permission, don't truncate */
1729 return may_open(nd, 0, flag & ~O_TRUNC);
1730}
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732/*
Dave Hansend57999e2008-02-15 14:37:27 -08001733 * Note that while the flag value (low two bits) for sys_open means:
1734 * 00 - read-only
1735 * 01 - write-only
1736 * 10 - read-write
1737 * 11 - special
1738 * it is changed into
1739 * 00 - no permissions needed
1740 * 01 - read-permission
1741 * 10 - write-permission
1742 * 11 - read-write
1743 * for the internal routines (ie open_namei()/follow_link() etc)
1744 * This is more logical, and also allows the 00 "no perm needed"
1745 * to be used for symlinks (where the permissions are checked
1746 * later).
1747 *
1748*/
1749static inline int open_to_namei_flags(int flag)
1750{
1751 if ((flag+1) & O_ACCMODE)
1752 flag++;
1753 return flag;
1754}
1755
Dave Hansen4a3fd212008-02-15 14:37:48 -08001756static int open_will_write_to_fs(int flag, struct inode *inode)
1757{
1758 /*
1759 * We'll never write to the fs underlying
1760 * a device file.
1761 */
1762 if (special_file(inode->i_mode))
1763 return 0;
1764 return (flag & O_TRUNC);
1765}
1766
Dave Hansend57999e2008-02-15 14:37:27 -08001767/*
Dave Hansen4a3fd212008-02-15 14:37:48 -08001768 * Note that the low bits of the passed in "open_flag"
1769 * are not the same as in the local variable "flag". See
1770 * open_to_namei_flags() for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001772struct file *do_filp_open(int dfd, const char *pathname,
1773 int open_flag, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774{
Dave Hansen4a3fd212008-02-15 14:37:48 -08001775 struct file *filp;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001776 struct nameidata nd;
Trond Myklebust834f2a42005-10-18 14:20:16 -07001777 int acc_mode, error;
Al Viro4e7506e2005-06-06 13:36:00 -07001778 struct path path;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 struct dentry *dir;
1780 int count = 0;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001781 int will_write;
Dave Hansend57999e2008-02-15 14:37:27 -08001782 int flag = open_to_namei_flags(open_flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 acc_mode = ACC_MODE(flag);
1785
Trond Myklebust834f2a42005-10-18 14:20:16 -07001786 /* O_TRUNC implies we need access checks for write permissions */
1787 if (flag & O_TRUNC)
1788 acc_mode |= MAY_WRITE;
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 /* Allow the LSM permission hook to distinguish append
1791 access from general write access. */
1792 if (flag & O_APPEND)
1793 acc_mode |= MAY_APPEND;
1794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 /*
1796 * The simplest case - just a plain lookup.
1797 */
1798 if (!(flag & O_CREAT)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08001799 error = path_lookup_open(dfd, pathname, lookup_flags(flag),
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001800 &nd, flag);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (error)
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001802 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 goto ok;
1804 }
1805
1806 /*
1807 * Create - we need to know the parent.
1808 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001809 error = path_lookup_create(dfd, pathname, LOOKUP_PARENT,
1810 &nd, flag, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 if (error)
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001812 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 /*
1815 * We have the parent and last component. First of all, check
1816 * that we are not asked to creat(2) an obvious directory - that
1817 * will not do.
1818 */
1819 error = -EISDIR;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001820 if (nd.last_type != LAST_NORM || nd.last.name[nd.last.len])
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 goto exit;
1822
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001823 dir = nd.path.dentry;
1824 nd.flags &= ~LOOKUP_PARENT;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001825 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001826 path.dentry = lookup_hash(&nd);
1827 path.mnt = nd.path.mnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828
1829do_last:
Al Viro4e7506e2005-06-06 13:36:00 -07001830 error = PTR_ERR(path.dentry);
1831 if (IS_ERR(path.dentry)) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001832 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 goto exit;
1834 }
1835
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001836 if (IS_ERR(nd.intent.open.file)) {
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001837 error = PTR_ERR(nd.intent.open.file);
Dave Hansen4a3fd212008-02-15 14:37:48 -08001838 goto exit_mutex_unlock;
Oleg Drokin4af4c522006-03-25 03:06:54 -08001839 }
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 /* Negative dentry, just create the file */
Al Viro4e7506e2005-06-06 13:36:00 -07001842 if (!path.dentry->d_inode) {
Dave Hansen4a3fd212008-02-15 14:37:48 -08001843 /*
1844 * This write is needed to ensure that a
1845 * ro->rw transition does not occur between
1846 * the time when the file is created and when
1847 * a permanent write count is taken through
1848 * the 'struct file' in nameidata_to_filp().
1849 */
1850 error = mnt_want_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 if (error)
Dave Hansen4a3fd212008-02-15 14:37:48 -08001852 goto exit_mutex_unlock;
1853 error = __open_namei_create(&nd, &path, flag, mode);
1854 if (error) {
1855 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 goto exit;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001857 }
1858 filp = nameidata_to_filp(&nd, open_flag);
1859 mnt_drop_write(nd.path.mnt);
1860 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
1862
1863 /*
1864 * It already exists.
1865 */
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001866 mutex_unlock(&dir->d_inode->i_mutex);
Al Viro5a190ae2007-06-07 12:19:32 -04001867 audit_inode(pathname, path.dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 error = -EEXIST;
1870 if (flag & O_EXCL)
1871 goto exit_dput;
1872
Al Viroe13b2102005-06-06 13:36:06 -07001873 if (__follow_mount(&path)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 error = -ELOOP;
Al Viroba7a4c12005-06-06 13:36:08 -07001875 if (flag & O_NOFOLLOW)
1876 goto exit_dput;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 }
Amy Griffis3e2efce2006-07-13 13:16:02 -04001878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 error = -ENOENT;
Al Viro4e7506e2005-06-06 13:36:00 -07001880 if (!path.dentry->d_inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 goto exit_dput;
Al Viro4e7506e2005-06-06 13:36:00 -07001882 if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 goto do_link;
1884
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001885 path_to_nameidata(&path, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 error = -EISDIR;
Al Viro4e7506e2005-06-06 13:36:00 -07001887 if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 goto exit;
1889ok:
Dave Hansen4a3fd212008-02-15 14:37:48 -08001890 /*
1891 * Consider:
1892 * 1. may_open() truncates a file
1893 * 2. a rw->ro mount transition occurs
1894 * 3. nameidata_to_filp() fails due to
1895 * the ro mount.
1896 * That would be inconsistent, and should
1897 * be avoided. Taking this mnt write here
1898 * ensures that (2) can not occur.
1899 */
1900 will_write = open_will_write_to_fs(flag, nd.path.dentry->d_inode);
1901 if (will_write) {
1902 error = mnt_want_write(nd.path.mnt);
1903 if (error)
1904 goto exit;
1905 }
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001906 error = may_open(&nd, acc_mode, flag);
Dave Hansen4a3fd212008-02-15 14:37:48 -08001907 if (error) {
1908 if (will_write)
1909 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 goto exit;
Dave Hansen4a3fd212008-02-15 14:37:48 -08001911 }
1912 filp = nameidata_to_filp(&nd, open_flag);
1913 /*
1914 * It is now safe to drop the mnt write
1915 * because the filp has had a write taken
1916 * on its behalf.
1917 */
1918 if (will_write)
1919 mnt_drop_write(nd.path.mnt);
1920 return filp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
Dave Hansen4a3fd212008-02-15 14:37:48 -08001922exit_mutex_unlock:
1923 mutex_unlock(&dir->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924exit_dput:
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001925 path_put_conditional(&path, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926exit:
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001927 if (!IS_ERR(nd.intent.open.file))
1928 release_open_intent(&nd);
1929 path_put(&nd.path);
1930 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
1932do_link:
1933 error = -ELOOP;
1934 if (flag & O_NOFOLLOW)
1935 goto exit_dput;
1936 /*
1937 * This is subtle. Instead of calling do_follow_link() we do the
1938 * thing by hands. The reason is that this way we have zero link_count
1939 * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT.
1940 * After that we have the parent and last component, i.e.
1941 * we are in the same situation as after the first path_walk().
1942 * Well, almost - if the last component is normal we get its copy
1943 * stored in nd->last.name and we will have to putname() it when we
1944 * are done. Procfs-like symlinks just set LAST_BIND.
1945 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001946 nd.flags |= LOOKUP_PARENT;
1947 error = security_inode_follow_link(path.dentry, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (error)
1949 goto exit_dput;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001950 error = __do_follow_link(&path, &nd);
Kirill Korotaevde459212006-07-14 00:23:49 -07001951 if (error) {
1952 /* Does someone understand code flow here? Or it is only
1953 * me so stupid? Anathema to whoever designed this non-sense
1954 * with "intent.open".
1955 */
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001956 release_open_intent(&nd);
1957 return ERR_PTR(error);
Kirill Korotaevde459212006-07-14 00:23:49 -07001958 }
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001959 nd.flags &= ~LOOKUP_PARENT;
1960 if (nd.last_type == LAST_BIND)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 goto ok;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 error = -EISDIR;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001963 if (nd.last_type != LAST_NORM)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 goto exit;
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001965 if (nd.last.name[nd.last.len]) {
1966 __putname(nd.last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 goto exit;
1968 }
1969 error = -ELOOP;
1970 if (count++==32) {
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001971 __putname(nd.last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 goto exit;
1973 }
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001974 dir = nd.path.dentry;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08001975 mutex_lock(&dir->d_inode->i_mutex);
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001976 path.dentry = lookup_hash(&nd);
1977 path.mnt = nd.path.mnt;
1978 __putname(nd.last.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 goto do_last;
1980}
1981
1982/**
Christoph Hellwiga70e65d2008-02-15 14:37:28 -08001983 * filp_open - open file and return file pointer
1984 *
1985 * @filename: path to open
1986 * @flags: open flags as per the open(2) second argument
1987 * @mode: mode for the new file if O_CREAT is set, else ignored
1988 *
1989 * This is the helper to open a file from kernelspace if you really
1990 * have to. But in generally you should not do this, so please move
1991 * along, nothing to see here..
1992 */
1993struct file *filp_open(const char *filename, int flags, int mode)
1994{
1995 return do_filp_open(AT_FDCWD, filename, flags, mode);
1996}
1997EXPORT_SYMBOL(filp_open);
1998
1999/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 * lookup_create - lookup a dentry, creating it if it doesn't exist
2001 * @nd: nameidata info
2002 * @is_dir: directory flag
2003 *
2004 * Simple function to lookup and return a dentry and create it
2005 * if it doesn't exist. Is SMP-safe.
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002006 *
Jan Blunck4ac91372008-02-14 19:34:32 -08002007 * Returns with nd->path.dentry->d_inode->i_mutex locked.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 */
2009struct dentry *lookup_create(struct nameidata *nd, int is_dir)
2010{
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002011 struct dentry *dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
Jan Blunck4ac91372008-02-14 19:34:32 -08002013 mutex_lock_nested(&nd->path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002014 /*
2015 * Yucky last component or no last component at all?
2016 * (foo/., foo/.., /////)
2017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if (nd->last_type != LAST_NORM)
2019 goto fail;
2020 nd->flags &= ~LOOKUP_PARENT;
ASANO Masahiroa6349042006-08-22 20:06:02 -04002021 nd->flags |= LOOKUP_CREATE;
2022 nd->intent.open.flags = O_EXCL;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002023
2024 /*
2025 * Do the final lookup.
2026 */
Christoph Hellwig49705b72005-11-08 21:35:06 -08002027 dentry = lookup_hash(nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (IS_ERR(dentry))
2029 goto fail;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002030
Al Viroe9baf6e2008-05-15 04:49:12 -04002031 if (dentry->d_inode)
2032 goto eexist;
Christoph Hellwigc663e5d2005-06-23 00:09:49 -07002033 /*
2034 * Special case - lookup gave negative, but... we had foo/bar/
2035 * From the vfs_mknod() POV we just have a negative dentry -
2036 * all is fine. Let's be bastards - you had / on the end, you've
2037 * been asking for (non-existent) directory. -ENOENT for you.
2038 */
Al Viroe9baf6e2008-05-15 04:49:12 -04002039 if (unlikely(!is_dir && nd->last.name[nd->last.len])) {
2040 dput(dentry);
2041 dentry = ERR_PTR(-ENOENT);
2042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 return dentry;
Al Viroe9baf6e2008-05-15 04:49:12 -04002044eexist:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 dput(dentry);
Al Viroe9baf6e2008-05-15 04:49:12 -04002046 dentry = ERR_PTR(-EEXIST);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047fail:
2048 return dentry;
2049}
Christoph Hellwigf81a0bf2005-05-19 12:26:43 -07002050EXPORT_SYMBOL_GPL(lookup_create);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
2053{
2054 int error = may_create(dir, dentry, NULL);
2055
2056 if (error)
2057 return error;
2058
2059 if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD))
2060 return -EPERM;
2061
2062 if (!dir->i_op || !dir->i_op->mknod)
2063 return -EPERM;
2064
Serge E. Hallyn08ce5f12008-04-29 01:00:10 -07002065 error = devcgroup_inode_mknod(mode, dev);
2066 if (error)
2067 return error;
2068
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 error = security_inode_mknod(dir, dentry, mode, dev);
2070 if (error)
2071 return error;
2072
2073 DQUOT_INIT(dir);
2074 error = dir->i_op->mknod(dir, dentry, mode, dev);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002075 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002076 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 return error;
2078}
2079
Dave Hansen463c3192008-02-15 14:37:57 -08002080static int may_mknod(mode_t mode)
2081{
2082 switch (mode & S_IFMT) {
2083 case S_IFREG:
2084 case S_IFCHR:
2085 case S_IFBLK:
2086 case S_IFIFO:
2087 case S_IFSOCK:
2088 case 0: /* zero mode translates to S_IFREG */
2089 return 0;
2090 case S_IFDIR:
2091 return -EPERM;
2092 default:
2093 return -EINVAL;
2094 }
2095}
2096
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002097asmlinkage long sys_mknodat(int dfd, const char __user *filename, int mode,
2098 unsigned dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
2100 int error = 0;
2101 char * tmp;
2102 struct dentry * dentry;
2103 struct nameidata nd;
2104
2105 if (S_ISDIR(mode))
2106 return -EPERM;
2107 tmp = getname(filename);
2108 if (IS_ERR(tmp))
2109 return PTR_ERR(tmp);
2110
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002111 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 if (error)
2113 goto out;
2114 dentry = lookup_create(&nd, 0);
Dave Hansen463c3192008-02-15 14:37:57 -08002115 if (IS_ERR(dentry)) {
2116 error = PTR_ERR(dentry);
2117 goto out_unlock;
2118 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002119 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 mode &= ~current->fs->umask;
Dave Hansen463c3192008-02-15 14:37:57 -08002121 error = may_mknod(mode);
2122 if (error)
2123 goto out_dput;
2124 error = mnt_want_write(nd.path.mnt);
2125 if (error)
2126 goto out_dput;
2127 switch (mode & S_IFMT) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 case 0: case S_IFREG:
Jan Blunck4ac91372008-02-14 19:34:32 -08002129 error = vfs_create(nd.path.dentry->d_inode,dentry,mode,&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 break;
2131 case S_IFCHR: case S_IFBLK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002132 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 new_decode_dev(dev));
2134 break;
2135 case S_IFIFO: case S_IFSOCK:
Jan Blunck4ac91372008-02-14 19:34:32 -08002136 error = vfs_mknod(nd.path.dentry->d_inode,dentry,mode,0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 }
Dave Hansen463c3192008-02-15 14:37:57 -08002139 mnt_drop_write(nd.path.mnt);
2140out_dput:
2141 dput(dentry);
2142out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002143 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002144 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145out:
2146 putname(tmp);
2147
2148 return error;
2149}
2150
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002151asmlinkage long sys_mknod(const char __user *filename, int mode, unsigned dev)
2152{
2153 return sys_mknodat(AT_FDCWD, filename, mode, dev);
2154}
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2157{
2158 int error = may_create(dir, dentry, NULL);
2159
2160 if (error)
2161 return error;
2162
2163 if (!dir->i_op || !dir->i_op->mkdir)
2164 return -EPERM;
2165
2166 mode &= (S_IRWXUGO|S_ISVTX);
2167 error = security_inode_mkdir(dir, dentry, mode);
2168 if (error)
2169 return error;
2170
2171 DQUOT_INIT(dir);
2172 error = dir->i_op->mkdir(dir, dentry, mode);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002173 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002174 fsnotify_mkdir(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 return error;
2176}
2177
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002178asmlinkage long sys_mkdirat(int dfd, const char __user *pathname, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
2180 int error = 0;
2181 char * tmp;
Dave Hansen6902d922006-09-30 23:29:01 -07002182 struct dentry *dentry;
2183 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 tmp = getname(pathname);
2186 error = PTR_ERR(tmp);
Dave Hansen6902d922006-09-30 23:29:01 -07002187 if (IS_ERR(tmp))
2188 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189
Dave Hansen6902d922006-09-30 23:29:01 -07002190 error = do_path_lookup(dfd, tmp, LOOKUP_PARENT, &nd);
2191 if (error)
2192 goto out;
2193 dentry = lookup_create(&nd, 1);
2194 error = PTR_ERR(dentry);
2195 if (IS_ERR(dentry))
2196 goto out_unlock;
2197
Jan Blunck4ac91372008-02-14 19:34:32 -08002198 if (!IS_POSIXACL(nd.path.dentry->d_inode))
Dave Hansen6902d922006-09-30 23:29:01 -07002199 mode &= ~current->fs->umask;
Dave Hansen463c3192008-02-15 14:37:57 -08002200 error = mnt_want_write(nd.path.mnt);
2201 if (error)
2202 goto out_dput;
Jan Blunck4ac91372008-02-14 19:34:32 -08002203 error = vfs_mkdir(nd.path.dentry->d_inode, dentry, mode);
Dave Hansen463c3192008-02-15 14:37:57 -08002204 mnt_drop_write(nd.path.mnt);
2205out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002206 dput(dentry);
2207out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002208 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002209 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210out:
Dave Hansen6902d922006-09-30 23:29:01 -07002211 putname(tmp);
2212out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 return error;
2214}
2215
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002216asmlinkage long sys_mkdir(const char __user *pathname, int mode)
2217{
2218 return sys_mkdirat(AT_FDCWD, pathname, mode);
2219}
2220
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221/*
2222 * We try to drop the dentry early: we should have
2223 * a usage count of 2 if we're the only user of this
2224 * dentry, and if that is true (possibly after pruning
2225 * the dcache), then we drop the dentry now.
2226 *
2227 * A low-level filesystem can, if it choses, legally
2228 * do a
2229 *
2230 * if (!d_unhashed(dentry))
2231 * return -EBUSY;
2232 *
2233 * if it cannot handle the case of removing a directory
2234 * that is still in use by something else..
2235 */
2236void dentry_unhash(struct dentry *dentry)
2237{
2238 dget(dentry);
Vasily Averindc168422006-12-06 20:37:07 -08002239 shrink_dcache_parent(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 spin_lock(&dcache_lock);
2241 spin_lock(&dentry->d_lock);
2242 if (atomic_read(&dentry->d_count) == 2)
2243 __d_drop(dentry);
2244 spin_unlock(&dentry->d_lock);
2245 spin_unlock(&dcache_lock);
2246}
2247
2248int vfs_rmdir(struct inode *dir, struct dentry *dentry)
2249{
2250 int error = may_delete(dir, dentry, 1);
2251
2252 if (error)
2253 return error;
2254
2255 if (!dir->i_op || !dir->i_op->rmdir)
2256 return -EPERM;
2257
2258 DQUOT_INIT(dir);
2259
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002260 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 dentry_unhash(dentry);
2262 if (d_mountpoint(dentry))
2263 error = -EBUSY;
2264 else {
2265 error = security_inode_rmdir(dir, dentry);
2266 if (!error) {
2267 error = dir->i_op->rmdir(dir, dentry);
2268 if (!error)
2269 dentry->d_inode->i_flags |= S_DEAD;
2270 }
2271 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002272 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (!error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 d_delete(dentry);
2275 }
2276 dput(dentry);
2277
2278 return error;
2279}
2280
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002281static long do_rmdir(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
2283 int error = 0;
2284 char * name;
2285 struct dentry *dentry;
2286 struct nameidata nd;
2287
2288 name = getname(pathname);
2289 if(IS_ERR(name))
2290 return PTR_ERR(name);
2291
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002292 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 if (error)
2294 goto exit;
2295
2296 switch(nd.last_type) {
2297 case LAST_DOTDOT:
2298 error = -ENOTEMPTY;
2299 goto exit1;
2300 case LAST_DOT:
2301 error = -EINVAL;
2302 goto exit1;
2303 case LAST_ROOT:
2304 error = -EBUSY;
2305 goto exit1;
2306 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002307 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002308 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 error = PTR_ERR(dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002310 if (IS_ERR(dentry))
2311 goto exit2;
Dave Hansen06227532008-02-15 14:37:34 -08002312 error = mnt_want_write(nd.path.mnt);
2313 if (error)
2314 goto exit3;
Jan Blunck4ac91372008-02-14 19:34:32 -08002315 error = vfs_rmdir(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08002316 mnt_drop_write(nd.path.mnt);
2317exit3:
Dave Hansen6902d922006-09-30 23:29:01 -07002318 dput(dentry);
2319exit2:
Jan Blunck4ac91372008-02-14 19:34:32 -08002320 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002322 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323exit:
2324 putname(name);
2325 return error;
2326}
2327
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002328asmlinkage long sys_rmdir(const char __user *pathname)
2329{
2330 return do_rmdir(AT_FDCWD, pathname);
2331}
2332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333int vfs_unlink(struct inode *dir, struct dentry *dentry)
2334{
2335 int error = may_delete(dir, dentry, 0);
2336
2337 if (error)
2338 return error;
2339
2340 if (!dir->i_op || !dir->i_op->unlink)
2341 return -EPERM;
2342
2343 DQUOT_INIT(dir);
2344
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002345 mutex_lock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346 if (d_mountpoint(dentry))
2347 error = -EBUSY;
2348 else {
2349 error = security_inode_unlink(dir, dentry);
2350 if (!error)
2351 error = dir->i_op->unlink(dir, dentry);
2352 }
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002353 mutex_unlock(&dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354
2355 /* We don't d_delete() NFS sillyrenamed files--they still exist. */
2356 if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) {
Jan Karaece95912008-02-06 01:37:13 -08002357 fsnotify_link_count(dentry->d_inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 d_delete(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 }
Robert Love0eeca282005-07-12 17:06:03 -04002360
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return error;
2362}
2363
2364/*
2365 * Make sure that the actual truncation of the file will occur outside its
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002366 * directory's i_mutex. Truncate can take a long time if there is a lot of
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367 * writeout happening, and we don't want to prevent access to the directory
2368 * while waiting on the I/O.
2369 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002370static long do_unlinkat(int dfd, const char __user *pathname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371{
2372 int error = 0;
2373 char * name;
2374 struct dentry *dentry;
2375 struct nameidata nd;
2376 struct inode *inode = NULL;
2377
2378 name = getname(pathname);
2379 if(IS_ERR(name))
2380 return PTR_ERR(name);
2381
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002382 error = do_path_lookup(dfd, name, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383 if (error)
2384 goto exit;
2385 error = -EISDIR;
2386 if (nd.last_type != LAST_NORM)
2387 goto exit1;
Jan Blunck4ac91372008-02-14 19:34:32 -08002388 mutex_lock_nested(&nd.path.dentry->d_inode->i_mutex, I_MUTEX_PARENT);
Christoph Hellwig49705b72005-11-08 21:35:06 -08002389 dentry = lookup_hash(&nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 error = PTR_ERR(dentry);
2391 if (!IS_ERR(dentry)) {
2392 /* Why not before? Because we want correct error value */
2393 if (nd.last.name[nd.last.len])
2394 goto slashes;
2395 inode = dentry->d_inode;
2396 if (inode)
2397 atomic_inc(&inode->i_count);
Dave Hansen06227532008-02-15 14:37:34 -08002398 error = mnt_want_write(nd.path.mnt);
2399 if (error)
2400 goto exit2;
Jan Blunck4ac91372008-02-14 19:34:32 -08002401 error = vfs_unlink(nd.path.dentry->d_inode, dentry);
Dave Hansen06227532008-02-15 14:37:34 -08002402 mnt_drop_write(nd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403 exit2:
2404 dput(dentry);
2405 }
Jan Blunck4ac91372008-02-14 19:34:32 -08002406 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 if (inode)
2408 iput(inode); /* truncate the inode here */
2409exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002410 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411exit:
2412 putname(name);
2413 return error;
2414
2415slashes:
2416 error = !dentry->d_inode ? -ENOENT :
2417 S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR;
2418 goto exit2;
2419}
2420
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002421asmlinkage long sys_unlinkat(int dfd, const char __user *pathname, int flag)
2422{
2423 if ((flag & ~AT_REMOVEDIR) != 0)
2424 return -EINVAL;
2425
2426 if (flag & AT_REMOVEDIR)
2427 return do_rmdir(dfd, pathname);
2428
2429 return do_unlinkat(dfd, pathname);
2430}
2431
2432asmlinkage long sys_unlink(const char __user *pathname)
2433{
2434 return do_unlinkat(AT_FDCWD, pathname);
2435}
2436
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002437int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002438{
2439 int error = may_create(dir, dentry, NULL);
2440
2441 if (error)
2442 return error;
2443
2444 if (!dir->i_op || !dir->i_op->symlink)
2445 return -EPERM;
2446
2447 error = security_inode_symlink(dir, dentry, oldname);
2448 if (error)
2449 return error;
2450
2451 DQUOT_INIT(dir);
2452 error = dir->i_op->symlink(dir, dentry, oldname);
Stephen Smalleya74574a2005-09-09 13:01:44 -07002453 if (!error)
Amy Griffisf38aa942005-11-03 15:57:06 +00002454 fsnotify_create(dir, dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455 return error;
2456}
2457
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002458asmlinkage long sys_symlinkat(const char __user *oldname,
2459 int newdfd, const char __user *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
2461 int error = 0;
2462 char * from;
2463 char * to;
Dave Hansen6902d922006-09-30 23:29:01 -07002464 struct dentry *dentry;
2465 struct nameidata nd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466
2467 from = getname(oldname);
2468 if(IS_ERR(from))
2469 return PTR_ERR(from);
2470 to = getname(newname);
2471 error = PTR_ERR(to);
Dave Hansen6902d922006-09-30 23:29:01 -07002472 if (IS_ERR(to))
2473 goto out_putname;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Dave Hansen6902d922006-09-30 23:29:01 -07002475 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
2476 if (error)
2477 goto out;
2478 dentry = lookup_create(&nd, 0);
2479 error = PTR_ERR(dentry);
2480 if (IS_ERR(dentry))
2481 goto out_unlock;
2482
Dave Hansen75c3f292008-02-15 14:37:45 -08002483 error = mnt_want_write(nd.path.mnt);
2484 if (error)
2485 goto out_dput;
Miklos Szeredidb2e7472008-06-24 16:50:16 +02002486 error = vfs_symlink(nd.path.dentry->d_inode, dentry, from);
Dave Hansen75c3f292008-02-15 14:37:45 -08002487 mnt_drop_write(nd.path.mnt);
2488out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002489 dput(dentry);
2490out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002491 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Jan Blunck1d957f92008-02-14 19:34:35 -08002492 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493out:
Dave Hansen6902d922006-09-30 23:29:01 -07002494 putname(to);
2495out_putname:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 putname(from);
2497 return error;
2498}
2499
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002500asmlinkage long sys_symlink(const char __user *oldname, const char __user *newname)
2501{
2502 return sys_symlinkat(oldname, AT_FDCWD, newname);
2503}
2504
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry)
2506{
2507 struct inode *inode = old_dentry->d_inode;
2508 int error;
2509
2510 if (!inode)
2511 return -ENOENT;
2512
2513 error = may_create(dir, new_dentry, NULL);
2514 if (error)
2515 return error;
2516
2517 if (dir->i_sb != inode->i_sb)
2518 return -EXDEV;
2519
2520 /*
2521 * A link to an append-only or immutable file cannot be created.
2522 */
2523 if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2524 return -EPERM;
2525 if (!dir->i_op || !dir->i_op->link)
2526 return -EPERM;
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002527 if (S_ISDIR(inode->i_mode))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 return -EPERM;
2529
2530 error = security_inode_link(old_dentry, dir, new_dentry);
2531 if (error)
2532 return error;
2533
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002534 mutex_lock(&inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 DQUOT_INIT(dir);
2536 error = dir->i_op->link(old_dentry, dir, new_dentry);
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002537 mutex_unlock(&inode->i_mutex);
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002538 if (!error)
Tetsuo Handa7e79eed2008-06-24 16:50:15 +02002539 fsnotify_link(dir, inode, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540 return error;
2541}
2542
2543/*
2544 * Hardlinks are often used in delicate situations. We avoid
2545 * security-related surprises by not following symlinks on the
2546 * newname. --KAB
2547 *
2548 * We don't follow them on the oldname either to be compatible
2549 * with linux 2.0, and to avoid hard-linking to directories
2550 * and other special files. --ADM
2551 */
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002552asmlinkage long sys_linkat(int olddfd, const char __user *oldname,
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002553 int newdfd, const char __user *newname,
2554 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555{
2556 struct dentry *new_dentry;
2557 struct nameidata nd, old_nd;
2558 int error;
2559 char * to;
2560
Ulrich Drepper45c9b112006-06-25 05:49:11 -07002561 if ((flags & ~AT_SYMLINK_FOLLOW) != 0)
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002562 return -EINVAL;
2563
Linus Torvalds1da177e2005-04-16 15:20:36 -07002564 to = getname(newname);
2565 if (IS_ERR(to))
2566 return PTR_ERR(to);
2567
Ulrich Drepper45c9b112006-06-25 05:49:11 -07002568 error = __user_walk_fd(olddfd, oldname,
2569 flags & AT_SYMLINK_FOLLOW ? LOOKUP_FOLLOW : 0,
2570 &old_nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 if (error)
2572 goto exit;
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002573 error = do_path_lookup(newdfd, to, LOOKUP_PARENT, &nd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574 if (error)
2575 goto out;
2576 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08002577 if (old_nd.path.mnt != nd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 goto out_release;
2579 new_dentry = lookup_create(&nd, 0);
2580 error = PTR_ERR(new_dentry);
Dave Hansen6902d922006-09-30 23:29:01 -07002581 if (IS_ERR(new_dentry))
2582 goto out_unlock;
Dave Hansen75c3f292008-02-15 14:37:45 -08002583 error = mnt_want_write(nd.path.mnt);
2584 if (error)
2585 goto out_dput;
Jan Blunck4ac91372008-02-14 19:34:32 -08002586 error = vfs_link(old_nd.path.dentry, nd.path.dentry->d_inode, new_dentry);
Dave Hansen75c3f292008-02-15 14:37:45 -08002587 mnt_drop_write(nd.path.mnt);
2588out_dput:
Dave Hansen6902d922006-09-30 23:29:01 -07002589 dput(new_dentry);
2590out_unlock:
Jan Blunck4ac91372008-02-14 19:34:32 -08002591 mutex_unlock(&nd.path.dentry->d_inode->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592out_release:
Jan Blunck1d957f92008-02-14 19:34:35 -08002593 path_put(&nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594out:
Jan Blunck1d957f92008-02-14 19:34:35 -08002595 path_put(&old_nd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596exit:
2597 putname(to);
2598
2599 return error;
2600}
2601
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002602asmlinkage long sys_link(const char __user *oldname, const char __user *newname)
2603{
Ulrich Drepperc04030e2006-02-24 13:04:21 -08002604 return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002605}
2606
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607/*
2608 * The worst of all namespace operations - renaming directory. "Perverted"
2609 * doesn't even start to describe it. Somebody in UCB had a heck of a trip...
2610 * Problems:
2611 * a) we can get into loop creation. Check is done in is_subdir().
2612 * b) race potential - two innocent renames can create a loop together.
2613 * That's where 4.4 screws up. Current fix: serialization on
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002614 * sb->s_vfs_rename_mutex. We might be more accurate, but that's another
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 * story.
2616 * c) we have to lock _three_ objects - parents and victim (if it exists).
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002617 * And that - after we got ->i_mutex on parents (until then we don't know
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618 * whether the target exists). Solution: try to be smart with locking
2619 * order for inodes. We rely on the fact that tree topology may change
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002620 * only under ->s_vfs_rename_mutex _and_ that parent of the object we
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 * move will be locked. Thus we can rank directories by the tree
2622 * (ancestors first) and rank all non-directories after them.
2623 * That works since everybody except rename does "lock parent, lookup,
Arjan van de Vena11f3a02006-03-23 03:00:33 -08002624 * lock child" and rename is under ->s_vfs_rename_mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 * HOWEVER, it relies on the assumption that any object with ->lookup()
2626 * has no more than 1 dentry. If "hybrid" objects will ever appear,
2627 * we'd better make sure that there's no link(2) for them.
2628 * d) some filesystems don't support opened-but-unlinked directories,
2629 * either because of layout or because they are not ready to deal with
2630 * all cases correctly. The latter will be fixed (taking this sort of
2631 * stuff into VFS), but the former is not going away. Solution: the same
2632 * trick as in rmdir().
2633 * e) conversion from fhandle to dentry may come in the wrong moment - when
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002634 * we are removing the target. Solution: we will have to grab ->i_mutex
Linus Torvalds1da177e2005-04-16 15:20:36 -07002635 * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002636 * ->i_mutex on parents, which works but leads to some truely excessive
Linus Torvalds1da177e2005-04-16 15:20:36 -07002637 * locking].
2638 */
Adrian Bunk75c96f82005-05-05 16:16:09 -07002639static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry,
2640 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641{
2642 int error = 0;
2643 struct inode *target;
2644
2645 /*
2646 * If we are going to change the parent - check write permissions,
2647 * we'll need to flip '..'.
2648 */
2649 if (new_dir != old_dir) {
2650 error = permission(old_dentry->d_inode, MAY_WRITE, NULL);
2651 if (error)
2652 return error;
2653 }
2654
2655 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2656 if (error)
2657 return error;
2658
2659 target = new_dentry->d_inode;
2660 if (target) {
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002661 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662 dentry_unhash(new_dentry);
2663 }
2664 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2665 error = -EBUSY;
2666 else
2667 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2668 if (target) {
2669 if (!error)
2670 target->i_flags |= S_DEAD;
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002671 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 if (d_unhashed(new_dentry))
2673 d_rehash(new_dentry);
2674 dput(new_dentry);
2675 }
Stephen Smalleye31e14e2005-09-09 13:01:45 -07002676 if (!error)
Mark Fasheh349457c2006-09-08 14:22:21 -07002677 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
2678 d_move(old_dentry,new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 return error;
2680}
2681
Adrian Bunk75c96f82005-05-05 16:16:09 -07002682static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry,
2683 struct inode *new_dir, struct dentry *new_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684{
2685 struct inode *target;
2686 int error;
2687
2688 error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry);
2689 if (error)
2690 return error;
2691
2692 dget(new_dentry);
2693 target = new_dentry->d_inode;
2694 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002695 mutex_lock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696 if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry))
2697 error = -EBUSY;
2698 else
2699 error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry);
2700 if (!error) {
Mark Fasheh349457c2006-09-08 14:22:21 -07002701 if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702 d_move(old_dentry, new_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002703 }
2704 if (target)
Jes Sorensen1b1dcc12006-01-09 15:59:24 -08002705 mutex_unlock(&target->i_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002706 dput(new_dentry);
2707 return error;
2708}
2709
2710int vfs_rename(struct inode *old_dir, struct dentry *old_dentry,
2711 struct inode *new_dir, struct dentry *new_dentry)
2712{
2713 int error;
2714 int is_dir = S_ISDIR(old_dentry->d_inode->i_mode);
Robert Love0eeca282005-07-12 17:06:03 -04002715 const char *old_name;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
2717 if (old_dentry->d_inode == new_dentry->d_inode)
2718 return 0;
2719
2720 error = may_delete(old_dir, old_dentry, is_dir);
2721 if (error)
2722 return error;
2723
2724 if (!new_dentry->d_inode)
2725 error = may_create(new_dir, new_dentry, NULL);
2726 else
2727 error = may_delete(new_dir, new_dentry, is_dir);
2728 if (error)
2729 return error;
2730
2731 if (!old_dir->i_op || !old_dir->i_op->rename)
2732 return -EPERM;
2733
2734 DQUOT_INIT(old_dir);
2735 DQUOT_INIT(new_dir);
2736
Robert Love0eeca282005-07-12 17:06:03 -04002737 old_name = fsnotify_oldname_init(old_dentry->d_name.name);
2738
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 if (is_dir)
2740 error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry);
2741 else
2742 error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry);
2743 if (!error) {
Robert Love0eeca282005-07-12 17:06:03 -04002744 const char *new_name = old_dentry->d_name.name;
John McCutchan89204c42005-08-15 12:13:28 -04002745 fsnotify_move(old_dir, new_dir, old_name, new_name, is_dir,
Al Viro5a190ae2007-06-07 12:19:32 -04002746 new_dentry->d_inode, old_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747 }
Robert Love0eeca282005-07-12 17:06:03 -04002748 fsnotify_oldname_free(old_name);
2749
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750 return error;
2751}
2752
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002753static int do_rename(int olddfd, const char *oldname,
2754 int newdfd, const char *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755{
2756 int error = 0;
2757 struct dentry * old_dir, * new_dir;
2758 struct dentry * old_dentry, *new_dentry;
2759 struct dentry * trap;
2760 struct nameidata oldnd, newnd;
2761
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002762 error = do_path_lookup(olddfd, oldname, LOOKUP_PARENT, &oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 if (error)
2764 goto exit;
2765
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002766 error = do_path_lookup(newdfd, newname, LOOKUP_PARENT, &newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 if (error)
2768 goto exit1;
2769
2770 error = -EXDEV;
Jan Blunck4ac91372008-02-14 19:34:32 -08002771 if (oldnd.path.mnt != newnd.path.mnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002772 goto exit2;
2773
Jan Blunck4ac91372008-02-14 19:34:32 -08002774 old_dir = oldnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775 error = -EBUSY;
2776 if (oldnd.last_type != LAST_NORM)
2777 goto exit2;
2778
Jan Blunck4ac91372008-02-14 19:34:32 -08002779 new_dir = newnd.path.dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780 if (newnd.last_type != LAST_NORM)
2781 goto exit2;
2782
2783 trap = lock_rename(new_dir, old_dir);
2784
Christoph Hellwig49705b72005-11-08 21:35:06 -08002785 old_dentry = lookup_hash(&oldnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002786 error = PTR_ERR(old_dentry);
2787 if (IS_ERR(old_dentry))
2788 goto exit3;
2789 /* source must exist */
2790 error = -ENOENT;
2791 if (!old_dentry->d_inode)
2792 goto exit4;
2793 /* unless the source is a directory trailing slashes give -ENOTDIR */
2794 if (!S_ISDIR(old_dentry->d_inode->i_mode)) {
2795 error = -ENOTDIR;
2796 if (oldnd.last.name[oldnd.last.len])
2797 goto exit4;
2798 if (newnd.last.name[newnd.last.len])
2799 goto exit4;
2800 }
2801 /* source should not be ancestor of target */
2802 error = -EINVAL;
2803 if (old_dentry == trap)
2804 goto exit4;
Christoph Hellwig49705b72005-11-08 21:35:06 -08002805 new_dentry = lookup_hash(&newnd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 error = PTR_ERR(new_dentry);
2807 if (IS_ERR(new_dentry))
2808 goto exit4;
2809 /* target should not be an ancestor of source */
2810 error = -ENOTEMPTY;
2811 if (new_dentry == trap)
2812 goto exit5;
2813
Dave Hansen9079b1e2008-02-15 14:37:49 -08002814 error = mnt_want_write(oldnd.path.mnt);
2815 if (error)
2816 goto exit5;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817 error = vfs_rename(old_dir->d_inode, old_dentry,
2818 new_dir->d_inode, new_dentry);
Dave Hansen9079b1e2008-02-15 14:37:49 -08002819 mnt_drop_write(oldnd.path.mnt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820exit5:
2821 dput(new_dentry);
2822exit4:
2823 dput(old_dentry);
2824exit3:
2825 unlock_rename(new_dir, old_dir);
2826exit2:
Jan Blunck1d957f92008-02-14 19:34:35 -08002827 path_put(&newnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828exit1:
Jan Blunck1d957f92008-02-14 19:34:35 -08002829 path_put(&oldnd.path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830exit:
2831 return error;
2832}
2833
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002834asmlinkage long sys_renameat(int olddfd, const char __user *oldname,
2835 int newdfd, const char __user *newname)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002836{
2837 int error;
2838 char * from;
2839 char * to;
2840
2841 from = getname(oldname);
2842 if(IS_ERR(from))
2843 return PTR_ERR(from);
2844 to = getname(newname);
2845 error = PTR_ERR(to);
2846 if (!IS_ERR(to)) {
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002847 error = do_rename(olddfd, from, newdfd, to);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 putname(to);
2849 }
2850 putname(from);
2851 return error;
2852}
2853
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002854asmlinkage long sys_rename(const char __user *oldname, const char __user *newname)
2855{
2856 return sys_renameat(AT_FDCWD, oldname, AT_FDCWD, newname);
2857}
2858
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link)
2860{
2861 int len;
2862
2863 len = PTR_ERR(link);
2864 if (IS_ERR(link))
2865 goto out;
2866
2867 len = strlen(link);
2868 if (len > (unsigned) buflen)
2869 len = buflen;
2870 if (copy_to_user(buffer, link, len))
2871 len = -EFAULT;
2872out:
2873 return len;
2874}
2875
2876/*
2877 * A helper for ->readlink(). This should be used *ONLY* for symlinks that
2878 * have ->follow_link() touching nd only in nd_set_link(). Using (or not
2879 * using) it for any given inode is up to filesystem.
2880 */
2881int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2882{
2883 struct nameidata nd;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002884 void *cookie;
Marcin Slusarz694a1762008-06-09 16:40:37 -07002885 int res;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002886
Linus Torvalds1da177e2005-04-16 15:20:36 -07002887 nd.depth = 0;
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002888 cookie = dentry->d_inode->i_op->follow_link(dentry, &nd);
Marcin Slusarz694a1762008-06-09 16:40:37 -07002889 if (IS_ERR(cookie))
2890 return PTR_ERR(cookie);
2891
2892 res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd));
2893 if (dentry->d_inode->i_op->put_link)
2894 dentry->d_inode->i_op->put_link(dentry, &nd, cookie);
2895 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896}
2897
2898int vfs_follow_link(struct nameidata *nd, const char *link)
2899{
2900 return __vfs_follow_link(nd, link);
2901}
2902
2903/* get the link contents into pagecache */
2904static char *page_getlink(struct dentry * dentry, struct page **ppage)
2905{
2906 struct page * page;
2907 struct address_space *mapping = dentry->d_inode->i_mapping;
Pekka Enberg090d2b12006-06-23 02:05:08 -07002908 page = read_mapping_page(mapping, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909 if (IS_ERR(page))
Nick Piggin6fe69002007-05-06 14:49:04 -07002910 return (char*)page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002911 *ppage = page;
2912 return kmap(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913}
2914
2915int page_readlink(struct dentry *dentry, char __user *buffer, int buflen)
2916{
2917 struct page *page = NULL;
2918 char *s = page_getlink(dentry, &page);
2919 int res = vfs_readlink(dentry,buffer,buflen,s);
2920 if (page) {
2921 kunmap(page);
2922 page_cache_release(page);
2923 }
2924 return res;
2925}
2926
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002927void *page_follow_link_light(struct dentry *dentry, struct nameidata *nd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002929 struct page *page = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 nd_set_link(nd, page_getlink(dentry, &page));
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002931 return page;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932}
2933
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002934void page_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935{
Linus Torvaldscc314ee2005-08-19 18:02:56 -07002936 struct page *page = cookie;
2937
2938 if (page) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 kunmap(page);
2940 page_cache_release(page);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 }
2942}
2943
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002944int __page_symlink(struct inode *inode, const char *symname, int len,
2945 gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946{
2947 struct address_space *mapping = inode->i_mapping;
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002948 struct page *page;
Nick Pigginafddba42007-10-16 01:25:01 -07002949 void *fsdata;
Dmitriy Monakhovbeb497a2007-02-16 01:27:18 -08002950 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002951 char *kaddr;
2952
NeilBrown7e53cac2006-03-25 03:07:57 -08002953retry:
Nick Pigginafddba42007-10-16 01:25:01 -07002954 err = pagecache_write_begin(NULL, mapping, 0, len-1,
2955 AOP_FLAG_UNINTERRUPTIBLE, &page, &fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 if (err)
Nick Pigginafddba42007-10-16 01:25:01 -07002957 goto fail;
2958
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 kaddr = kmap_atomic(page, KM_USER0);
2960 memcpy(kaddr, symname, len-1);
2961 kunmap_atomic(kaddr, KM_USER0);
Nick Pigginafddba42007-10-16 01:25:01 -07002962
2963 err = pagecache_write_end(NULL, mapping, 0, len-1, len-1,
2964 page, fsdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 if (err < 0)
2966 goto fail;
Nick Pigginafddba42007-10-16 01:25:01 -07002967 if (err < len-1)
2968 goto retry;
2969
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 mark_inode_dirty(inode);
2971 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972fail:
2973 return err;
2974}
2975
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002976int page_symlink(struct inode *inode, const char *symname, int len)
2977{
2978 return __page_symlink(inode, symname, len,
2979 mapping_gfp_mask(inode->i_mapping));
2980}
2981
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08002982const struct inode_operations page_symlink_inode_operations = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 .readlink = generic_readlink,
2984 .follow_link = page_follow_link_light,
2985 .put_link = page_put_link,
2986};
2987
2988EXPORT_SYMBOL(__user_walk);
Ulrich Drepper5590ff02006-01-18 17:43:53 -08002989EXPORT_SYMBOL(__user_walk_fd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990EXPORT_SYMBOL(follow_down);
2991EXPORT_SYMBOL(follow_up);
2992EXPORT_SYMBOL(get_write_access); /* binfmt_aout */
2993EXPORT_SYMBOL(getname);
2994EXPORT_SYMBOL(lock_rename);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995EXPORT_SYMBOL(lookup_one_len);
2996EXPORT_SYMBOL(page_follow_link_light);
2997EXPORT_SYMBOL(page_put_link);
2998EXPORT_SYMBOL(page_readlink);
Kirill Korotaev0adb25d2006-03-11 03:27:13 -08002999EXPORT_SYMBOL(__page_symlink);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000EXPORT_SYMBOL(page_symlink);
3001EXPORT_SYMBOL(page_symlink_inode_operations);
3002EXPORT_SYMBOL(path_lookup);
Josef 'Jeff' Sipek16f18202007-07-19 01:48:18 -07003003EXPORT_SYMBOL(vfs_path_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004EXPORT_SYMBOL(permission);
Christoph Hellwige4543ed2005-11-08 21:35:04 -08003005EXPORT_SYMBOL(vfs_permission);
Christoph Hellwig8c744fb2005-11-08 21:35:04 -08003006EXPORT_SYMBOL(file_permission);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007EXPORT_SYMBOL(unlock_rename);
3008EXPORT_SYMBOL(vfs_create);
3009EXPORT_SYMBOL(vfs_follow_link);
3010EXPORT_SYMBOL(vfs_link);
3011EXPORT_SYMBOL(vfs_mkdir);
3012EXPORT_SYMBOL(vfs_mknod);
3013EXPORT_SYMBOL(generic_permission);
3014EXPORT_SYMBOL(vfs_readlink);
3015EXPORT_SYMBOL(vfs_rename);
3016EXPORT_SYMBOL(vfs_rmdir);
3017EXPORT_SYMBOL(vfs_symlink);
3018EXPORT_SYMBOL(vfs_unlink);
3019EXPORT_SYMBOL(dentry_unhash);
3020EXPORT_SYMBOL(generic_readlink);