]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
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> | |
24 | #include <linux/dnotify.h> | |
25 | #include <linux/smp_lock.h> | |
26 | #include <linux/personality.h> | |
27 | #include <linux/security.h> | |
28 | #include <linux/syscalls.h> | |
29 | #include <linux/mount.h> | |
30 | #include <linux/audit.h> | |
31 | #include <asm/namei.h> | |
32 | #include <asm/uaccess.h> | |
33 | ||
34 | #define ACC_MODE(x) ("\000\004\002\006"[(x)&O_ACCMODE]) | |
35 | ||
36 | /* [Feb-1997 T. Schoebel-Theuer] | |
37 | * Fundamental changes in the pathname lookup mechanisms (namei) | |
38 | * were necessary because of omirr. The reason is that omirr needs | |
39 | * to know the _real_ pathname, not the user-supplied one, in case | |
40 | * of symlinks (and also when transname replacements occur). | |
41 | * | |
42 | * The new code replaces the old recursive symlink resolution with | |
43 | * an iterative one (in case of non-nested symlink chains). It does | |
44 | * this with calls to <fs>_follow_link(). | |
45 | * As a side effect, dir_namei(), _namei() and follow_link() are now | |
46 | * replaced with a single function lookup_dentry() that can handle all | |
47 | * the special cases of the former code. | |
48 | * | |
49 | * With the new dcache, the pathname is stored at each inode, at least as | |
50 | * long as the refcount of the inode is positive. As a side effect, the | |
51 | * size of the dcache depends on the inode cache and thus is dynamic. | |
52 | * | |
53 | * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink | |
54 | * resolution to correspond with current state of the code. | |
55 | * | |
56 | * Note that the symlink resolution is not *completely* iterative. | |
57 | * There is still a significant amount of tail- and mid- recursion in | |
58 | * the algorithm. Also, note that <fs>_readlink() is not used in | |
59 | * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() | |
60 | * may return different results than <fs>_follow_link(). Many virtual | |
61 | * filesystems (including /proc) exhibit this behavior. | |
62 | */ | |
63 | ||
64 | /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: | |
65 | * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL | |
66 | * and the name already exists in form of a symlink, try to create the new | |
67 | * name indicated by the symlink. The old code always complained that the | |
68 | * name already exists, due to not following the symlink even if its target | |
69 | * is nonexistent. The new semantics affects also mknod() and link() when | |
70 | * the name is a symlink pointing to a non-existant name. | |
71 | * | |
72 | * I don't know which semantics is the right one, since I have no access | |
73 | * to standards. But I found by trial that HP-UX 9.0 has the full "new" | |
74 | * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the | |
75 | * "old" one. Personally, I think the new semantics is much more logical. | |
76 | * Note that "ln old new" where "new" is a symlink pointing to a non-existing | |
77 | * file does succeed in both HP-UX and SunOs, but not in Solaris | |
78 | * and in the old Linux semantics. | |
79 | */ | |
80 | ||
81 | /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink | |
82 | * semantics. See the comments in "open_namei" and "do_link" below. | |
83 | * | |
84 | * [10-Sep-98 Alan Modra] Another symlink change. | |
85 | */ | |
86 | ||
87 | /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: | |
88 | * inside the path - always follow. | |
89 | * in the last component in creation/removal/renaming - never follow. | |
90 | * if LOOKUP_FOLLOW passed - follow. | |
91 | * if the pathname has trailing slashes - follow. | |
92 | * otherwise - don't follow. | |
93 | * (applied in that order). | |
94 | * | |
95 | * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT | |
96 | * restored for 2.4. This is the last surviving part of old 4.2BSD bug. | |
97 | * During the 2.4 we need to fix the userland stuff depending on it - | |
98 | * hopefully we will be able to get rid of that wart in 2.5. So far only | |
99 | * XEmacs seems to be relying on it... | |
100 | */ | |
101 | /* | |
102 | * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) | |
103 | * implemented. Let's see if raised priority of ->s_vfs_rename_sem gives | |
104 | * any extra contention... | |
105 | */ | |
106 | ||
107 | /* In order to reduce some races, while at the same time doing additional | |
108 | * checking and hopefully speeding things up, we copy filenames to the | |
109 | * kernel data space before using them.. | |
110 | * | |
111 | * POSIX.1 2.4: an empty pathname is invalid (ENOENT). | |
112 | * PATH_MAX includes the nul terminator --RR. | |
113 | */ | |
114 | static inline int do_getname(const char __user *filename, char *page) | |
115 | { | |
116 | int retval; | |
117 | unsigned long len = PATH_MAX; | |
118 | ||
119 | if (!segment_eq(get_fs(), KERNEL_DS)) { | |
120 | if ((unsigned long) filename >= TASK_SIZE) | |
121 | return -EFAULT; | |
122 | if (TASK_SIZE - (unsigned long) filename < PATH_MAX) | |
123 | len = TASK_SIZE - (unsigned long) filename; | |
124 | } | |
125 | ||
126 | retval = strncpy_from_user(page, filename, len); | |
127 | if (retval > 0) { | |
128 | if (retval < len) | |
129 | return 0; | |
130 | return -ENAMETOOLONG; | |
131 | } else if (!retval) | |
132 | retval = -ENOENT; | |
133 | return retval; | |
134 | } | |
135 | ||
136 | char * getname(const char __user * filename) | |
137 | { | |
138 | char *tmp, *result; | |
139 | ||
140 | result = ERR_PTR(-ENOMEM); | |
141 | tmp = __getname(); | |
142 | if (tmp) { | |
143 | int retval = do_getname(filename, tmp); | |
144 | ||
145 | result = tmp; | |
146 | if (retval < 0) { | |
147 | __putname(tmp); | |
148 | result = ERR_PTR(retval); | |
149 | } | |
150 | } | |
151 | audit_getname(result); | |
152 | return result; | |
153 | } | |
154 | ||
155 | #ifdef CONFIG_AUDITSYSCALL | |
156 | void putname(const char *name) | |
157 | { | |
158 | if (unlikely(current->audit_context)) | |
159 | audit_putname(name); | |
160 | else | |
161 | __putname(name); | |
162 | } | |
163 | EXPORT_SYMBOL(putname); | |
164 | #endif | |
165 | ||
166 | ||
167 | /** | |
168 | * generic_permission - check for access rights on a Posix-like filesystem | |
169 | * @inode: inode to check access rights for | |
170 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) | |
171 | * @check_acl: optional callback to check for Posix ACLs | |
172 | * | |
173 | * Used to check for read/write/execute permissions on a file. | |
174 | * We use "fsuid" for this, letting us set arbitrary permissions | |
175 | * for filesystem access without changing the "normal" uids which | |
176 | * are used for other things.. | |
177 | */ | |
178 | int generic_permission(struct inode *inode, int mask, | |
179 | int (*check_acl)(struct inode *inode, int mask)) | |
180 | { | |
181 | umode_t mode = inode->i_mode; | |
182 | ||
183 | if (current->fsuid == inode->i_uid) | |
184 | mode >>= 6; | |
185 | else { | |
186 | if (IS_POSIXACL(inode) && (mode & S_IRWXG) && check_acl) { | |
187 | int error = check_acl(inode, mask); | |
188 | if (error == -EACCES) | |
189 | goto check_capabilities; | |
190 | else if (error != -EAGAIN) | |
191 | return error; | |
192 | } | |
193 | ||
194 | if (in_group_p(inode->i_gid)) | |
195 | mode >>= 3; | |
196 | } | |
197 | ||
198 | /* | |
199 | * If the DACs are ok we don't need any capability check. | |
200 | */ | |
201 | if (((mode & mask & (MAY_READ|MAY_WRITE|MAY_EXEC)) == mask)) | |
202 | return 0; | |
203 | ||
204 | check_capabilities: | |
205 | /* | |
206 | * Read/write DACs are always overridable. | |
207 | * Executable DACs are overridable if at least one exec bit is set. | |
208 | */ | |
209 | if (!(mask & MAY_EXEC) || | |
210 | (inode->i_mode & S_IXUGO) || S_ISDIR(inode->i_mode)) | |
211 | if (capable(CAP_DAC_OVERRIDE)) | |
212 | return 0; | |
213 | ||
214 | /* | |
215 | * Searching includes executable on directories, else just read. | |
216 | */ | |
217 | if (mask == MAY_READ || (S_ISDIR(inode->i_mode) && !(mask & MAY_WRITE))) | |
218 | if (capable(CAP_DAC_READ_SEARCH)) | |
219 | return 0; | |
220 | ||
221 | return -EACCES; | |
222 | } | |
223 | ||
224 | int permission(struct inode *inode, int mask, struct nameidata *nd) | |
225 | { | |
226 | int retval, submask; | |
227 | ||
228 | if (mask & MAY_WRITE) { | |
229 | umode_t mode = inode->i_mode; | |
230 | ||
231 | /* | |
232 | * Nobody gets write access to a read-only fs. | |
233 | */ | |
234 | if (IS_RDONLY(inode) && | |
235 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) | |
236 | return -EROFS; | |
237 | ||
238 | /* | |
239 | * Nobody gets write access to an immutable file. | |
240 | */ | |
241 | if (IS_IMMUTABLE(inode)) | |
242 | return -EACCES; | |
243 | } | |
244 | ||
245 | ||
246 | /* Ordinary permission routines do not understand MAY_APPEND. */ | |
247 | submask = mask & ~MAY_APPEND; | |
248 | if (inode->i_op && inode->i_op->permission) | |
249 | retval = inode->i_op->permission(inode, submask, nd); | |
250 | else | |
251 | retval = generic_permission(inode, submask, NULL); | |
252 | if (retval) | |
253 | return retval; | |
254 | ||
255 | return security_inode_permission(inode, mask, nd); | |
256 | } | |
257 | ||
258 | /* | |
259 | * get_write_access() gets write permission for a file. | |
260 | * put_write_access() releases this write permission. | |
261 | * This is used for regular files. | |
262 | * We cannot support write (and maybe mmap read-write shared) accesses and | |
263 | * MAP_DENYWRITE mmappings simultaneously. The i_writecount field of an inode | |
264 | * can have the following values: | |
265 | * 0: no writers, no VM_DENYWRITE mappings | |
266 | * < 0: (-i_writecount) vm_area_structs with VM_DENYWRITE set exist | |
267 | * > 0: (i_writecount) users are writing to the file. | |
268 | * | |
269 | * Normally we operate on that counter with atomic_{inc,dec} and it's safe | |
270 | * except for the cases where we don't hold i_writecount yet. Then we need to | |
271 | * use {get,deny}_write_access() - these functions check the sign and refuse | |
272 | * to do the change if sign is wrong. Exclusion between them is provided by | |
273 | * the inode->i_lock spinlock. | |
274 | */ | |
275 | ||
276 | int get_write_access(struct inode * inode) | |
277 | { | |
278 | spin_lock(&inode->i_lock); | |
279 | if (atomic_read(&inode->i_writecount) < 0) { | |
280 | spin_unlock(&inode->i_lock); | |
281 | return -ETXTBSY; | |
282 | } | |
283 | atomic_inc(&inode->i_writecount); | |
284 | spin_unlock(&inode->i_lock); | |
285 | ||
286 | return 0; | |
287 | } | |
288 | ||
289 | int deny_write_access(struct file * file) | |
290 | { | |
291 | struct inode *inode = file->f_dentry->d_inode; | |
292 | ||
293 | spin_lock(&inode->i_lock); | |
294 | if (atomic_read(&inode->i_writecount) > 0) { | |
295 | spin_unlock(&inode->i_lock); | |
296 | return -ETXTBSY; | |
297 | } | |
298 | atomic_dec(&inode->i_writecount); | |
299 | spin_unlock(&inode->i_lock); | |
300 | ||
301 | return 0; | |
302 | } | |
303 | ||
304 | void path_release(struct nameidata *nd) | |
305 | { | |
306 | dput(nd->dentry); | |
307 | mntput(nd->mnt); | |
308 | } | |
309 | ||
310 | /* | |
311 | * umount() mustn't call path_release()/mntput() as that would clear | |
312 | * mnt_expiry_mark | |
313 | */ | |
314 | void path_release_on_umount(struct nameidata *nd) | |
315 | { | |
316 | dput(nd->dentry); | |
317 | _mntput(nd->mnt); | |
318 | } | |
319 | ||
320 | /* | |
321 | * Internal lookup() using the new generic dcache. | |
322 | * SMP-safe | |
323 | */ | |
324 | static struct dentry * cached_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd) | |
325 | { | |
326 | struct dentry * dentry = __d_lookup(parent, name); | |
327 | ||
328 | /* lockess __d_lookup may fail due to concurrent d_move() | |
329 | * in some unrelated directory, so try with d_lookup | |
330 | */ | |
331 | if (!dentry) | |
332 | dentry = d_lookup(parent, name); | |
333 | ||
334 | if (dentry && dentry->d_op && dentry->d_op->d_revalidate) { | |
335 | if (!dentry->d_op->d_revalidate(dentry, nd) && !d_invalidate(dentry)) { | |
336 | dput(dentry); | |
337 | dentry = NULL; | |
338 | } | |
339 | } | |
340 | return dentry; | |
341 | } | |
342 | ||
343 | /* | |
344 | * Short-cut version of permission(), for calling by | |
345 | * path_walk(), when dcache lock is held. Combines parts | |
346 | * of permission() and generic_permission(), and tests ONLY for | |
347 | * MAY_EXEC permission. | |
348 | * | |
349 | * If appropriate, check DAC only. If not appropriate, or | |
350 | * short-cut DAC fails, then call permission() to do more | |
351 | * complete permission check. | |
352 | */ | |
353 | static inline int exec_permission_lite(struct inode *inode, | |
354 | struct nameidata *nd) | |
355 | { | |
356 | umode_t mode = inode->i_mode; | |
357 | ||
358 | if (inode->i_op && inode->i_op->permission) | |
359 | return -EAGAIN; | |
360 | ||
361 | if (current->fsuid == inode->i_uid) | |
362 | mode >>= 6; | |
363 | else if (in_group_p(inode->i_gid)) | |
364 | mode >>= 3; | |
365 | ||
366 | if (mode & MAY_EXEC) | |
367 | goto ok; | |
368 | ||
369 | if ((inode->i_mode & S_IXUGO) && capable(CAP_DAC_OVERRIDE)) | |
370 | goto ok; | |
371 | ||
372 | if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_OVERRIDE)) | |
373 | goto ok; | |
374 | ||
375 | if (S_ISDIR(inode->i_mode) && capable(CAP_DAC_READ_SEARCH)) | |
376 | goto ok; | |
377 | ||
378 | return -EACCES; | |
379 | ok: | |
380 | return security_inode_permission(inode, MAY_EXEC, nd); | |
381 | } | |
382 | ||
383 | /* | |
384 | * This is called when everything else fails, and we actually have | |
385 | * to go to the low-level filesystem to find out what we should do.. | |
386 | * | |
387 | * We get the directory semaphore, and after getting that we also | |
388 | * make sure that nobody added the entry to the dcache in the meantime.. | |
389 | * SMP-safe | |
390 | */ | |
391 | static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd) | |
392 | { | |
393 | struct dentry * result; | |
394 | struct inode *dir = parent->d_inode; | |
395 | ||
396 | down(&dir->i_sem); | |
397 | /* | |
398 | * First re-do the cached lookup just in case it was created | |
399 | * while we waited for the directory semaphore.. | |
400 | * | |
401 | * FIXME! This could use version numbering or similar to | |
402 | * avoid unnecessary cache lookups. | |
403 | * | |
404 | * The "dcache_lock" is purely to protect the RCU list walker | |
405 | * from concurrent renames at this point (we mustn't get false | |
406 | * negatives from the RCU list walk here, unlike the optimistic | |
407 | * fast walk). | |
408 | * | |
409 | * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup | |
410 | */ | |
411 | result = d_lookup(parent, name); | |
412 | if (!result) { | |
413 | struct dentry * dentry = d_alloc(parent, name); | |
414 | result = ERR_PTR(-ENOMEM); | |
415 | if (dentry) { | |
416 | result = dir->i_op->lookup(dir, dentry, nd); | |
417 | if (result) | |
418 | dput(dentry); | |
419 | else | |
420 | result = dentry; | |
421 | } | |
422 | up(&dir->i_sem); | |
423 | return result; | |
424 | } | |
425 | ||
426 | /* | |
427 | * Uhhuh! Nasty case: the cache was re-populated while | |
428 | * we waited on the semaphore. Need to revalidate. | |
429 | */ | |
430 | up(&dir->i_sem); | |
431 | if (result->d_op && result->d_op->d_revalidate) { | |
432 | if (!result->d_op->d_revalidate(result, nd) && !d_invalidate(result)) { | |
433 | dput(result); | |
434 | result = ERR_PTR(-ENOENT); | |
435 | } | |
436 | } | |
437 | return result; | |
438 | } | |
439 | ||
440 | static int __emul_lookup_dentry(const char *, struct nameidata *); | |
441 | ||
442 | /* SMP-safe */ | |
443 | static inline int | |
444 | walk_init_root(const char *name, struct nameidata *nd) | |
445 | { | |
446 | read_lock(¤t->fs->lock); | |
447 | if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) { | |
448 | nd->mnt = mntget(current->fs->altrootmnt); | |
449 | nd->dentry = dget(current->fs->altroot); | |
450 | read_unlock(¤t->fs->lock); | |
451 | if (__emul_lookup_dentry(name,nd)) | |
452 | return 0; | |
453 | read_lock(¤t->fs->lock); | |
454 | } | |
455 | nd->mnt = mntget(current->fs->rootmnt); | |
456 | nd->dentry = dget(current->fs->root); | |
457 | read_unlock(¤t->fs->lock); | |
458 | return 1; | |
459 | } | |
460 | ||
461 | static inline int __vfs_follow_link(struct nameidata *nd, const char *link) | |
462 | { | |
463 | int res = 0; | |
464 | char *name; | |
465 | if (IS_ERR(link)) | |
466 | goto fail; | |
467 | ||
468 | if (*link == '/') { | |
469 | path_release(nd); | |
470 | if (!walk_init_root(link, nd)) | |
471 | /* weird __emul_prefix() stuff did it */ | |
472 | goto out; | |
473 | } | |
474 | res = link_path_walk(link, nd); | |
475 | out: | |
476 | if (nd->depth || res || nd->last_type!=LAST_NORM) | |
477 | return res; | |
478 | /* | |
479 | * If it is an iterative symlinks resolution in open_namei() we | |
480 | * have to copy the last component. And all that crap because of | |
481 | * bloody create() on broken symlinks. Furrfu... | |
482 | */ | |
483 | name = __getname(); | |
484 | if (unlikely(!name)) { | |
485 | path_release(nd); | |
486 | return -ENOMEM; | |
487 | } | |
488 | strcpy(name, nd->last.name); | |
489 | nd->last.name = name; | |
490 | return 0; | |
491 | fail: | |
492 | path_release(nd); | |
493 | return PTR_ERR(link); | |
494 | } | |
495 | ||
90ebe565 AV |
496 | struct path { |
497 | struct vfsmount *mnt; | |
498 | struct dentry *dentry; | |
499 | }; | |
500 | ||
cd4e91d3 | 501 | static inline int __do_follow_link(struct path *path, struct nameidata *nd) |
1da177e4 LT |
502 | { |
503 | int error; | |
cd4e91d3 | 504 | struct dentry *dentry = path->dentry; |
1da177e4 LT |
505 | |
506 | touch_atime(nd->mnt, dentry); | |
507 | nd_set_link(nd, NULL); | |
cd4e91d3 AV |
508 | |
509 | mntget(path->mnt); | |
1da177e4 LT |
510 | error = dentry->d_inode->i_op->follow_link(dentry, nd); |
511 | if (!error) { | |
512 | char *s = nd_get_link(nd); | |
513 | if (s) | |
514 | error = __vfs_follow_link(nd, s); | |
515 | if (dentry->d_inode->i_op->put_link) | |
516 | dentry->d_inode->i_op->put_link(dentry, nd); | |
517 | } | |
cd4e91d3 AV |
518 | dput(dentry); |
519 | mntput(path->mnt); | |
1da177e4 LT |
520 | |
521 | return error; | |
522 | } | |
523 | ||
524 | /* | |
525 | * This limits recursive symlink follows to 8, while | |
526 | * limiting consecutive symlinks to 40. | |
527 | * | |
528 | * Without that kind of total limit, nasty chains of consecutive | |
529 | * symlinks can cause almost arbitrarily long lookups. | |
530 | */ | |
90ebe565 | 531 | static inline int do_follow_link(struct path *path, struct nameidata *nd) |
1da177e4 LT |
532 | { |
533 | int err = -ELOOP; | |
534 | if (current->link_count >= MAX_NESTED_LINKS) | |
535 | goto loop; | |
536 | if (current->total_link_count >= 40) | |
537 | goto loop; | |
538 | BUG_ON(nd->depth >= MAX_NESTED_LINKS); | |
539 | cond_resched(); | |
90ebe565 | 540 | err = security_inode_follow_link(path->dentry, nd); |
1da177e4 LT |
541 | if (err) |
542 | goto loop; | |
543 | current->link_count++; | |
544 | current->total_link_count++; | |
545 | nd->depth++; | |
cd4e91d3 | 546 | err = __do_follow_link(path, nd); |
839d9f93 AV |
547 | current->link_count--; |
548 | nd->depth--; | |
1da177e4 LT |
549 | return err; |
550 | loop: | |
5f92b3bc | 551 | dput(path->dentry); |
839d9f93 | 552 | path_release(nd); |
1da177e4 LT |
553 | return err; |
554 | } | |
555 | ||
556 | int follow_up(struct vfsmount **mnt, struct dentry **dentry) | |
557 | { | |
558 | struct vfsmount *parent; | |
559 | struct dentry *mountpoint; | |
560 | spin_lock(&vfsmount_lock); | |
561 | parent=(*mnt)->mnt_parent; | |
562 | if (parent == *mnt) { | |
563 | spin_unlock(&vfsmount_lock); | |
564 | return 0; | |
565 | } | |
566 | mntget(parent); | |
567 | mountpoint=dget((*mnt)->mnt_mountpoint); | |
568 | spin_unlock(&vfsmount_lock); | |
569 | dput(*dentry); | |
570 | *dentry = mountpoint; | |
571 | mntput(*mnt); | |
572 | *mnt = parent; | |
573 | return 1; | |
574 | } | |
575 | ||
576 | /* no need for dcache_lock, as serialization is taken care in | |
577 | * namespace.c | |
578 | */ | |
579 | static int follow_mount(struct vfsmount **mnt, struct dentry **dentry) | |
580 | { | |
581 | int res = 0; | |
582 | while (d_mountpoint(*dentry)) { | |
583 | struct vfsmount *mounted = lookup_mnt(*mnt, *dentry); | |
584 | if (!mounted) | |
585 | break; | |
586 | mntput(*mnt); | |
587 | *mnt = mounted; | |
588 | dput(*dentry); | |
589 | *dentry = dget(mounted->mnt_root); | |
590 | res = 1; | |
591 | } | |
592 | return res; | |
593 | } | |
594 | ||
595 | /* no need for dcache_lock, as serialization is taken care in | |
596 | * namespace.c | |
597 | */ | |
598 | static inline int __follow_down(struct vfsmount **mnt, struct dentry **dentry) | |
599 | { | |
600 | struct vfsmount *mounted; | |
601 | ||
602 | mounted = lookup_mnt(*mnt, *dentry); | |
603 | if (mounted) { | |
604 | mntput(*mnt); | |
605 | *mnt = mounted; | |
606 | dput(*dentry); | |
607 | *dentry = dget(mounted->mnt_root); | |
608 | return 1; | |
609 | } | |
610 | return 0; | |
611 | } | |
612 | ||
613 | int follow_down(struct vfsmount **mnt, struct dentry **dentry) | |
614 | { | |
615 | return __follow_down(mnt,dentry); | |
616 | } | |
617 | ||
618 | static inline void follow_dotdot(struct vfsmount **mnt, struct dentry **dentry) | |
619 | { | |
620 | while(1) { | |
621 | struct vfsmount *parent; | |
622 | struct dentry *old = *dentry; | |
623 | ||
624 | read_lock(¤t->fs->lock); | |
625 | if (*dentry == current->fs->root && | |
626 | *mnt == current->fs->rootmnt) { | |
627 | read_unlock(¤t->fs->lock); | |
628 | break; | |
629 | } | |
630 | read_unlock(¤t->fs->lock); | |
631 | spin_lock(&dcache_lock); | |
632 | if (*dentry != (*mnt)->mnt_root) { | |
633 | *dentry = dget((*dentry)->d_parent); | |
634 | spin_unlock(&dcache_lock); | |
635 | dput(old); | |
636 | break; | |
637 | } | |
638 | spin_unlock(&dcache_lock); | |
639 | spin_lock(&vfsmount_lock); | |
640 | parent = (*mnt)->mnt_parent; | |
641 | if (parent == *mnt) { | |
642 | spin_unlock(&vfsmount_lock); | |
643 | break; | |
644 | } | |
645 | mntget(parent); | |
646 | *dentry = dget((*mnt)->mnt_mountpoint); | |
647 | spin_unlock(&vfsmount_lock); | |
648 | dput(old); | |
649 | mntput(*mnt); | |
650 | *mnt = parent; | |
651 | } | |
652 | follow_mount(mnt, dentry); | |
653 | } | |
654 | ||
1da177e4 LT |
655 | /* |
656 | * It's more convoluted than I'd like it to be, but... it's still fairly | |
657 | * small and for now I'd prefer to have fast path as straight as possible. | |
658 | * It _is_ time-critical. | |
659 | */ | |
660 | static int do_lookup(struct nameidata *nd, struct qstr *name, | |
661 | struct path *path) | |
662 | { | |
663 | struct vfsmount *mnt = nd->mnt; | |
664 | struct dentry *dentry = __d_lookup(nd->dentry, name); | |
665 | ||
666 | if (!dentry) | |
667 | goto need_lookup; | |
668 | if (dentry->d_op && dentry->d_op->d_revalidate) | |
669 | goto need_revalidate; | |
670 | done: | |
671 | path->mnt = mnt; | |
672 | path->dentry = dentry; | |
673 | return 0; | |
674 | ||
675 | need_lookup: | |
676 | dentry = real_lookup(nd->dentry, name, nd); | |
677 | if (IS_ERR(dentry)) | |
678 | goto fail; | |
679 | goto done; | |
680 | ||
681 | need_revalidate: | |
682 | if (dentry->d_op->d_revalidate(dentry, nd)) | |
683 | goto done; | |
684 | if (d_invalidate(dentry)) | |
685 | goto done; | |
686 | dput(dentry); | |
687 | goto need_lookup; | |
688 | ||
689 | fail: | |
690 | return PTR_ERR(dentry); | |
691 | } | |
692 | ||
693 | /* | |
694 | * Name resolution. | |
ea3834d9 PM |
695 | * This is the basic name resolution function, turning a pathname into |
696 | * the final dentry. We expect 'base' to be positive and a directory. | |
1da177e4 | 697 | * |
ea3834d9 PM |
698 | * Returns 0 and nd will have valid dentry and mnt on success. |
699 | * Returns error and drops reference to input namei data on failure. | |
1da177e4 LT |
700 | */ |
701 | static fastcall int __link_path_walk(const char * name, struct nameidata *nd) | |
702 | { | |
703 | struct path next; | |
704 | struct inode *inode; | |
705 | int err; | |
706 | unsigned int lookup_flags = nd->flags; | |
707 | ||
708 | while (*name=='/') | |
709 | name++; | |
710 | if (!*name) | |
711 | goto return_reval; | |
712 | ||
713 | inode = nd->dentry->d_inode; | |
714 | if (nd->depth) | |
715 | lookup_flags = LOOKUP_FOLLOW; | |
716 | ||
717 | /* At this point we know we have a real path component. */ | |
718 | for(;;) { | |
719 | unsigned long hash; | |
720 | struct qstr this; | |
721 | unsigned int c; | |
722 | ||
723 | err = exec_permission_lite(inode, nd); | |
724 | if (err == -EAGAIN) { | |
725 | err = permission(inode, MAY_EXEC, nd); | |
726 | } | |
727 | if (err) | |
728 | break; | |
729 | ||
730 | this.name = name; | |
731 | c = *(const unsigned char *)name; | |
732 | ||
733 | hash = init_name_hash(); | |
734 | do { | |
735 | name++; | |
736 | hash = partial_name_hash(c, hash); | |
737 | c = *(const unsigned char *)name; | |
738 | } while (c && (c != '/')); | |
739 | this.len = name - (const char *) this.name; | |
740 | this.hash = end_name_hash(hash); | |
741 | ||
742 | /* remove trailing slashes? */ | |
743 | if (!c) | |
744 | goto last_component; | |
745 | while (*++name == '/'); | |
746 | if (!*name) | |
747 | goto last_with_slashes; | |
748 | ||
749 | /* | |
750 | * "." and ".." are special - ".." especially so because it has | |
751 | * to be able to know about the current root directory and | |
752 | * parent relationships. | |
753 | */ | |
754 | if (this.name[0] == '.') switch (this.len) { | |
755 | default: | |
756 | break; | |
757 | case 2: | |
758 | if (this.name[1] != '.') | |
759 | break; | |
760 | follow_dotdot(&nd->mnt, &nd->dentry); | |
761 | inode = nd->dentry->d_inode; | |
762 | /* fallthrough */ | |
763 | case 1: | |
764 | continue; | |
765 | } | |
766 | /* | |
767 | * See if the low-level filesystem might want | |
768 | * to use its own hash.. | |
769 | */ | |
770 | if (nd->dentry->d_op && nd->dentry->d_op->d_hash) { | |
771 | err = nd->dentry->d_op->d_hash(nd->dentry, &this); | |
772 | if (err < 0) | |
773 | break; | |
774 | } | |
775 | nd->flags |= LOOKUP_CONTINUE; | |
776 | /* This does the actual lookups.. */ | |
777 | err = do_lookup(nd, &this, &next); | |
778 | if (err) | |
779 | break; | |
780 | /* Check mountpoints.. */ | |
781 | follow_mount(&next.mnt, &next.dentry); | |
782 | ||
783 | err = -ENOENT; | |
784 | inode = next.dentry->d_inode; | |
785 | if (!inode) | |
786 | goto out_dput; | |
787 | err = -ENOTDIR; | |
788 | if (!inode->i_op) | |
789 | goto out_dput; | |
790 | ||
791 | if (inode->i_op->follow_link) { | |
90ebe565 | 792 | err = do_follow_link(&next, nd); |
1da177e4 LT |
793 | if (err) |
794 | goto return_err; | |
795 | err = -ENOENT; | |
796 | inode = nd->dentry->d_inode; | |
797 | if (!inode) | |
798 | break; | |
799 | err = -ENOTDIR; | |
800 | if (!inode->i_op) | |
801 | break; | |
802 | } else { | |
803 | dput(nd->dentry); | |
804 | nd->mnt = next.mnt; | |
805 | nd->dentry = next.dentry; | |
806 | } | |
807 | err = -ENOTDIR; | |
808 | if (!inode->i_op->lookup) | |
809 | break; | |
810 | continue; | |
811 | /* here ends the main loop */ | |
812 | ||
813 | last_with_slashes: | |
814 | lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; | |
815 | last_component: | |
816 | nd->flags &= ~LOOKUP_CONTINUE; | |
817 | if (lookup_flags & LOOKUP_PARENT) | |
818 | goto lookup_parent; | |
819 | if (this.name[0] == '.') switch (this.len) { | |
820 | default: | |
821 | break; | |
822 | case 2: | |
823 | if (this.name[1] != '.') | |
824 | break; | |
825 | follow_dotdot(&nd->mnt, &nd->dentry); | |
826 | inode = nd->dentry->d_inode; | |
827 | /* fallthrough */ | |
828 | case 1: | |
829 | goto return_reval; | |
830 | } | |
831 | if (nd->dentry->d_op && nd->dentry->d_op->d_hash) { | |
832 | err = nd->dentry->d_op->d_hash(nd->dentry, &this); | |
833 | if (err < 0) | |
834 | break; | |
835 | } | |
836 | err = do_lookup(nd, &this, &next); | |
837 | if (err) | |
838 | break; | |
839 | follow_mount(&next.mnt, &next.dentry); | |
840 | inode = next.dentry->d_inode; | |
841 | if ((lookup_flags & LOOKUP_FOLLOW) | |
842 | && inode && inode->i_op && inode->i_op->follow_link) { | |
90ebe565 | 843 | err = do_follow_link(&next, nd); |
1da177e4 LT |
844 | if (err) |
845 | goto return_err; | |
846 | inode = nd->dentry->d_inode; | |
847 | } else { | |
848 | dput(nd->dentry); | |
849 | nd->mnt = next.mnt; | |
850 | nd->dentry = next.dentry; | |
851 | } | |
852 | err = -ENOENT; | |
853 | if (!inode) | |
854 | break; | |
855 | if (lookup_flags & LOOKUP_DIRECTORY) { | |
856 | err = -ENOTDIR; | |
857 | if (!inode->i_op || !inode->i_op->lookup) | |
858 | break; | |
859 | } | |
860 | goto return_base; | |
861 | lookup_parent: | |
862 | nd->last = this; | |
863 | nd->last_type = LAST_NORM; | |
864 | if (this.name[0] != '.') | |
865 | goto return_base; | |
866 | if (this.len == 1) | |
867 | nd->last_type = LAST_DOT; | |
868 | else if (this.len == 2 && this.name[1] == '.') | |
869 | nd->last_type = LAST_DOTDOT; | |
870 | else | |
871 | goto return_base; | |
872 | return_reval: | |
873 | /* | |
874 | * We bypassed the ordinary revalidation routines. | |
875 | * We may need to check the cached dentry for staleness. | |
876 | */ | |
877 | if (nd->dentry && nd->dentry->d_sb && | |
878 | (nd->dentry->d_sb->s_type->fs_flags & FS_REVAL_DOT)) { | |
879 | err = -ESTALE; | |
880 | /* Note: we do not d_invalidate() */ | |
881 | if (!nd->dentry->d_op->d_revalidate(nd->dentry, nd)) | |
882 | break; | |
883 | } | |
884 | return_base: | |
885 | return 0; | |
886 | out_dput: | |
887 | dput(next.dentry); | |
888 | break; | |
889 | } | |
890 | path_release(nd); | |
891 | return_err: | |
892 | return err; | |
893 | } | |
894 | ||
895 | /* | |
896 | * Wrapper to retry pathname resolution whenever the underlying | |
897 | * file system returns an ESTALE. | |
898 | * | |
899 | * Retry the whole path once, forcing real lookup requests | |
900 | * instead of relying on the dcache. | |
901 | */ | |
902 | int fastcall link_path_walk(const char *name, struct nameidata *nd) | |
903 | { | |
904 | struct nameidata save = *nd; | |
905 | int result; | |
906 | ||
907 | /* make sure the stuff we saved doesn't go away */ | |
908 | dget(save.dentry); | |
909 | mntget(save.mnt); | |
910 | ||
911 | result = __link_path_walk(name, nd); | |
912 | if (result == -ESTALE) { | |
913 | *nd = save; | |
914 | dget(nd->dentry); | |
915 | mntget(nd->mnt); | |
916 | nd->flags |= LOOKUP_REVAL; | |
917 | result = __link_path_walk(name, nd); | |
918 | } | |
919 | ||
920 | dput(save.dentry); | |
921 | mntput(save.mnt); | |
922 | ||
923 | return result; | |
924 | } | |
925 | ||
926 | int fastcall path_walk(const char * name, struct nameidata *nd) | |
927 | { | |
928 | current->total_link_count = 0; | |
929 | return link_path_walk(name, nd); | |
930 | } | |
931 | ||
ea3834d9 PM |
932 | /* |
933 | * SMP-safe: Returns 1 and nd will have valid dentry and mnt, if | |
934 | * everything is done. Returns 0 and drops input nd, if lookup failed; | |
935 | */ | |
1da177e4 LT |
936 | static int __emul_lookup_dentry(const char *name, struct nameidata *nd) |
937 | { | |
938 | if (path_walk(name, nd)) | |
939 | return 0; /* something went wrong... */ | |
940 | ||
941 | if (!nd->dentry->d_inode || S_ISDIR(nd->dentry->d_inode->i_mode)) { | |
942 | struct dentry *old_dentry = nd->dentry; | |
943 | struct vfsmount *old_mnt = nd->mnt; | |
944 | struct qstr last = nd->last; | |
945 | int last_type = nd->last_type; | |
946 | /* | |
947 | * NAME was not found in alternate root or it's a directory. Try to find | |
948 | * it in the normal root: | |
949 | */ | |
950 | nd->last_type = LAST_ROOT; | |
951 | read_lock(¤t->fs->lock); | |
952 | nd->mnt = mntget(current->fs->rootmnt); | |
953 | nd->dentry = dget(current->fs->root); | |
954 | read_unlock(¤t->fs->lock); | |
955 | if (path_walk(name, nd) == 0) { | |
956 | if (nd->dentry->d_inode) { | |
957 | dput(old_dentry); | |
958 | mntput(old_mnt); | |
959 | return 1; | |
960 | } | |
961 | path_release(nd); | |
962 | } | |
963 | nd->dentry = old_dentry; | |
964 | nd->mnt = old_mnt; | |
965 | nd->last = last; | |
966 | nd->last_type = last_type; | |
967 | } | |
968 | return 1; | |
969 | } | |
970 | ||
971 | void set_fs_altroot(void) | |
972 | { | |
973 | char *emul = __emul_prefix(); | |
974 | struct nameidata nd; | |
975 | struct vfsmount *mnt = NULL, *oldmnt; | |
976 | struct dentry *dentry = NULL, *olddentry; | |
977 | int err; | |
978 | ||
979 | if (!emul) | |
980 | goto set_it; | |
981 | err = path_lookup(emul, LOOKUP_FOLLOW|LOOKUP_DIRECTORY|LOOKUP_NOALT, &nd); | |
982 | if (!err) { | |
983 | mnt = nd.mnt; | |
984 | dentry = nd.dentry; | |
985 | } | |
986 | set_it: | |
987 | write_lock(¤t->fs->lock); | |
988 | oldmnt = current->fs->altrootmnt; | |
989 | olddentry = current->fs->altroot; | |
990 | current->fs->altrootmnt = mnt; | |
991 | current->fs->altroot = dentry; | |
992 | write_unlock(¤t->fs->lock); | |
993 | if (olddentry) { | |
994 | dput(olddentry); | |
995 | mntput(oldmnt); | |
996 | } | |
997 | } | |
998 | ||
ea3834d9 | 999 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
1da177e4 LT |
1000 | int fastcall path_lookup(const char *name, unsigned int flags, struct nameidata *nd) |
1001 | { | |
ea3834d9 | 1002 | int retval = 0; |
1da177e4 LT |
1003 | |
1004 | nd->last_type = LAST_ROOT; /* if there are only slashes... */ | |
1005 | nd->flags = flags; | |
1006 | nd->depth = 0; | |
1007 | ||
1008 | read_lock(¤t->fs->lock); | |
1009 | if (*name=='/') { | |
1010 | if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) { | |
1011 | nd->mnt = mntget(current->fs->altrootmnt); | |
1012 | nd->dentry = dget(current->fs->altroot); | |
1013 | read_unlock(¤t->fs->lock); | |
1014 | if (__emul_lookup_dentry(name,nd)) | |
ea3834d9 | 1015 | goto out; /* found in altroot */ |
1da177e4 LT |
1016 | read_lock(¤t->fs->lock); |
1017 | } | |
1018 | nd->mnt = mntget(current->fs->rootmnt); | |
1019 | nd->dentry = dget(current->fs->root); | |
1020 | } else { | |
1021 | nd->mnt = mntget(current->fs->pwdmnt); | |
1022 | nd->dentry = dget(current->fs->pwd); | |
1023 | } | |
1024 | read_unlock(¤t->fs->lock); | |
1025 | current->total_link_count = 0; | |
1026 | retval = link_path_walk(name, nd); | |
ea3834d9 | 1027 | out: |
1da177e4 LT |
1028 | if (unlikely(current->audit_context |
1029 | && nd && nd->dentry && nd->dentry->d_inode)) | |
1030 | audit_inode(name, nd->dentry->d_inode); | |
1031 | return retval; | |
1032 | } | |
1033 | ||
1034 | /* | |
1035 | * Restricted form of lookup. Doesn't follow links, single-component only, | |
1036 | * needs parent already locked. Doesn't follow mounts. | |
1037 | * SMP-safe. | |
1038 | */ | |
1039 | static struct dentry * __lookup_hash(struct qstr *name, struct dentry * base, struct nameidata *nd) | |
1040 | { | |
1041 | struct dentry * dentry; | |
1042 | struct inode *inode; | |
1043 | int err; | |
1044 | ||
1045 | inode = base->d_inode; | |
1046 | err = permission(inode, MAY_EXEC, nd); | |
1047 | dentry = ERR_PTR(err); | |
1048 | if (err) | |
1049 | goto out; | |
1050 | ||
1051 | /* | |
1052 | * See if the low-level filesystem might want | |
1053 | * to use its own hash.. | |
1054 | */ | |
1055 | if (base->d_op && base->d_op->d_hash) { | |
1056 | err = base->d_op->d_hash(base, name); | |
1057 | dentry = ERR_PTR(err); | |
1058 | if (err < 0) | |
1059 | goto out; | |
1060 | } | |
1061 | ||
1062 | dentry = cached_lookup(base, name, nd); | |
1063 | if (!dentry) { | |
1064 | struct dentry *new = d_alloc(base, name); | |
1065 | dentry = ERR_PTR(-ENOMEM); | |
1066 | if (!new) | |
1067 | goto out; | |
1068 | dentry = inode->i_op->lookup(inode, new, nd); | |
1069 | if (!dentry) | |
1070 | dentry = new; | |
1071 | else | |
1072 | dput(new); | |
1073 | } | |
1074 | out: | |
1075 | return dentry; | |
1076 | } | |
1077 | ||
1078 | struct dentry * lookup_hash(struct qstr *name, struct dentry * base) | |
1079 | { | |
1080 | return __lookup_hash(name, base, NULL); | |
1081 | } | |
1082 | ||
1083 | /* SMP-safe */ | |
1084 | struct dentry * lookup_one_len(const char * name, struct dentry * base, int len) | |
1085 | { | |
1086 | unsigned long hash; | |
1087 | struct qstr this; | |
1088 | unsigned int c; | |
1089 | ||
1090 | this.name = name; | |
1091 | this.len = len; | |
1092 | if (!len) | |
1093 | goto access; | |
1094 | ||
1095 | hash = init_name_hash(); | |
1096 | while (len--) { | |
1097 | c = *(const unsigned char *)name++; | |
1098 | if (c == '/' || c == '\0') | |
1099 | goto access; | |
1100 | hash = partial_name_hash(c, hash); | |
1101 | } | |
1102 | this.hash = end_name_hash(hash); | |
1103 | ||
1104 | return lookup_hash(&this, base); | |
1105 | access: | |
1106 | return ERR_PTR(-EACCES); | |
1107 | } | |
1108 | ||
1109 | /* | |
1110 | * namei() | |
1111 | * | |
1112 | * is used by most simple commands to get the inode of a specified name. | |
1113 | * Open, link etc use their own routines, but this is enough for things | |
1114 | * like 'chmod' etc. | |
1115 | * | |
1116 | * namei exists in two versions: namei/lnamei. The only difference is | |
1117 | * that namei follows links, while lnamei does not. | |
1118 | * SMP-safe | |
1119 | */ | |
1120 | int fastcall __user_walk(const char __user *name, unsigned flags, struct nameidata *nd) | |
1121 | { | |
1122 | char *tmp = getname(name); | |
1123 | int err = PTR_ERR(tmp); | |
1124 | ||
1125 | if (!IS_ERR(tmp)) { | |
1126 | err = path_lookup(tmp, flags, nd); | |
1127 | putname(tmp); | |
1128 | } | |
1129 | return err; | |
1130 | } | |
1131 | ||
1132 | /* | |
1133 | * It's inline, so penalty for filesystems that don't use sticky bit is | |
1134 | * minimal. | |
1135 | */ | |
1136 | static inline int check_sticky(struct inode *dir, struct inode *inode) | |
1137 | { | |
1138 | if (!(dir->i_mode & S_ISVTX)) | |
1139 | return 0; | |
1140 | if (inode->i_uid == current->fsuid) | |
1141 | return 0; | |
1142 | if (dir->i_uid == current->fsuid) | |
1143 | return 0; | |
1144 | return !capable(CAP_FOWNER); | |
1145 | } | |
1146 | ||
1147 | /* | |
1148 | * Check whether we can remove a link victim from directory dir, check | |
1149 | * whether the type of victim is right. | |
1150 | * 1. We can't do it if dir is read-only (done in permission()) | |
1151 | * 2. We should have write and exec permissions on dir | |
1152 | * 3. We can't remove anything from append-only dir | |
1153 | * 4. We can't do anything with immutable dir (done in permission()) | |
1154 | * 5. If the sticky bit on dir is set we should either | |
1155 | * a. be owner of dir, or | |
1156 | * b. be owner of victim, or | |
1157 | * c. have CAP_FOWNER capability | |
1158 | * 6. If the victim is append-only or immutable we can't do antyhing with | |
1159 | * links pointing to it. | |
1160 | * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. | |
1161 | * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. | |
1162 | * 9. We can't remove a root or mountpoint. | |
1163 | * 10. We don't allow removal of NFS sillyrenamed files; it's handled by | |
1164 | * nfs_async_unlink(). | |
1165 | */ | |
1166 | static inline int may_delete(struct inode *dir,struct dentry *victim,int isdir) | |
1167 | { | |
1168 | int error; | |
1169 | ||
1170 | if (!victim->d_inode) | |
1171 | return -ENOENT; | |
1172 | ||
1173 | BUG_ON(victim->d_parent->d_inode != dir); | |
1174 | ||
1175 | error = permission(dir,MAY_WRITE | MAY_EXEC, NULL); | |
1176 | if (error) | |
1177 | return error; | |
1178 | if (IS_APPEND(dir)) | |
1179 | return -EPERM; | |
1180 | if (check_sticky(dir, victim->d_inode)||IS_APPEND(victim->d_inode)|| | |
1181 | IS_IMMUTABLE(victim->d_inode)) | |
1182 | return -EPERM; | |
1183 | if (isdir) { | |
1184 | if (!S_ISDIR(victim->d_inode->i_mode)) | |
1185 | return -ENOTDIR; | |
1186 | if (IS_ROOT(victim)) | |
1187 | return -EBUSY; | |
1188 | } else if (S_ISDIR(victim->d_inode->i_mode)) | |
1189 | return -EISDIR; | |
1190 | if (IS_DEADDIR(dir)) | |
1191 | return -ENOENT; | |
1192 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) | |
1193 | return -EBUSY; | |
1194 | return 0; | |
1195 | } | |
1196 | ||
1197 | /* Check whether we can create an object with dentry child in directory | |
1198 | * dir. | |
1199 | * 1. We can't do it if child already exists (open has special treatment for | |
1200 | * this case, but since we are inlined it's OK) | |
1201 | * 2. We can't do it if dir is read-only (done in permission()) | |
1202 | * 3. We should have write and exec permissions on dir | |
1203 | * 4. We can't do it if dir is immutable (done in permission()) | |
1204 | */ | |
1205 | static inline int may_create(struct inode *dir, struct dentry *child, | |
1206 | struct nameidata *nd) | |
1207 | { | |
1208 | if (child->d_inode) | |
1209 | return -EEXIST; | |
1210 | if (IS_DEADDIR(dir)) | |
1211 | return -ENOENT; | |
1212 | return permission(dir,MAY_WRITE | MAY_EXEC, nd); | |
1213 | } | |
1214 | ||
1215 | /* | |
1216 | * Special case: O_CREAT|O_EXCL implies O_NOFOLLOW for security | |
1217 | * reasons. | |
1218 | * | |
1219 | * O_DIRECTORY translates into forcing a directory lookup. | |
1220 | */ | |
1221 | static inline int lookup_flags(unsigned int f) | |
1222 | { | |
1223 | unsigned long retval = LOOKUP_FOLLOW; | |
1224 | ||
1225 | if (f & O_NOFOLLOW) | |
1226 | retval &= ~LOOKUP_FOLLOW; | |
1227 | ||
1228 | if ((f & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL)) | |
1229 | retval &= ~LOOKUP_FOLLOW; | |
1230 | ||
1231 | if (f & O_DIRECTORY) | |
1232 | retval |= LOOKUP_DIRECTORY; | |
1233 | ||
1234 | return retval; | |
1235 | } | |
1236 | ||
1237 | /* | |
1238 | * p1 and p2 should be directories on the same fs. | |
1239 | */ | |
1240 | struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) | |
1241 | { | |
1242 | struct dentry *p; | |
1243 | ||
1244 | if (p1 == p2) { | |
1245 | down(&p1->d_inode->i_sem); | |
1246 | return NULL; | |
1247 | } | |
1248 | ||
1249 | down(&p1->d_inode->i_sb->s_vfs_rename_sem); | |
1250 | ||
1251 | for (p = p1; p->d_parent != p; p = p->d_parent) { | |
1252 | if (p->d_parent == p2) { | |
1253 | down(&p2->d_inode->i_sem); | |
1254 | down(&p1->d_inode->i_sem); | |
1255 | return p; | |
1256 | } | |
1257 | } | |
1258 | ||
1259 | for (p = p2; p->d_parent != p; p = p->d_parent) { | |
1260 | if (p->d_parent == p1) { | |
1261 | down(&p1->d_inode->i_sem); | |
1262 | down(&p2->d_inode->i_sem); | |
1263 | return p; | |
1264 | } | |
1265 | } | |
1266 | ||
1267 | down(&p1->d_inode->i_sem); | |
1268 | down(&p2->d_inode->i_sem); | |
1269 | return NULL; | |
1270 | } | |
1271 | ||
1272 | void unlock_rename(struct dentry *p1, struct dentry *p2) | |
1273 | { | |
1274 | up(&p1->d_inode->i_sem); | |
1275 | if (p1 != p2) { | |
1276 | up(&p2->d_inode->i_sem); | |
1277 | up(&p1->d_inode->i_sb->s_vfs_rename_sem); | |
1278 | } | |
1279 | } | |
1280 | ||
1281 | int vfs_create(struct inode *dir, struct dentry *dentry, int mode, | |
1282 | struct nameidata *nd) | |
1283 | { | |
1284 | int error = may_create(dir, dentry, nd); | |
1285 | ||
1286 | if (error) | |
1287 | return error; | |
1288 | ||
1289 | if (!dir->i_op || !dir->i_op->create) | |
1290 | return -EACCES; /* shouldn't it be ENOSYS? */ | |
1291 | mode &= S_IALLUGO; | |
1292 | mode |= S_IFREG; | |
1293 | error = security_inode_create(dir, dentry, mode); | |
1294 | if (error) | |
1295 | return error; | |
1296 | DQUOT_INIT(dir); | |
1297 | error = dir->i_op->create(dir, dentry, mode, nd); | |
1298 | if (!error) { | |
1299 | inode_dir_notify(dir, DN_CREATE); | |
1300 | security_inode_post_create(dir, dentry, mode); | |
1301 | } | |
1302 | return error; | |
1303 | } | |
1304 | ||
1305 | int may_open(struct nameidata *nd, int acc_mode, int flag) | |
1306 | { | |
1307 | struct dentry *dentry = nd->dentry; | |
1308 | struct inode *inode = dentry->d_inode; | |
1309 | int error; | |
1310 | ||
1311 | if (!inode) | |
1312 | return -ENOENT; | |
1313 | ||
1314 | if (S_ISLNK(inode->i_mode)) | |
1315 | return -ELOOP; | |
1316 | ||
1317 | if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE)) | |
1318 | return -EISDIR; | |
1319 | ||
1320 | error = permission(inode, acc_mode, nd); | |
1321 | if (error) | |
1322 | return error; | |
1323 | ||
1324 | /* | |
1325 | * FIFO's, sockets and device files are special: they don't | |
1326 | * actually live on the filesystem itself, and as such you | |
1327 | * can write to them even if the filesystem is read-only. | |
1328 | */ | |
1329 | if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) { | |
1330 | flag &= ~O_TRUNC; | |
1331 | } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) { | |
1332 | if (nd->mnt->mnt_flags & MNT_NODEV) | |
1333 | return -EACCES; | |
1334 | ||
1335 | flag &= ~O_TRUNC; | |
1336 | } else if (IS_RDONLY(inode) && (flag & FMODE_WRITE)) | |
1337 | return -EROFS; | |
1338 | /* | |
1339 | * An append-only file must be opened in append mode for writing. | |
1340 | */ | |
1341 | if (IS_APPEND(inode)) { | |
1342 | if ((flag & FMODE_WRITE) && !(flag & O_APPEND)) | |
1343 | return -EPERM; | |
1344 | if (flag & O_TRUNC) | |
1345 | return -EPERM; | |
1346 | } | |
1347 | ||
1348 | /* O_NOATIME can only be set by the owner or superuser */ | |
1349 | if (flag & O_NOATIME) | |
1350 | if (current->fsuid != inode->i_uid && !capable(CAP_FOWNER)) | |
1351 | return -EPERM; | |
1352 | ||
1353 | /* | |
1354 | * Ensure there are no outstanding leases on the file. | |
1355 | */ | |
1356 | error = break_lease(inode, flag); | |
1357 | if (error) | |
1358 | return error; | |
1359 | ||
1360 | if (flag & O_TRUNC) { | |
1361 | error = get_write_access(inode); | |
1362 | if (error) | |
1363 | return error; | |
1364 | ||
1365 | /* | |
1366 | * Refuse to truncate files with mandatory locks held on them. | |
1367 | */ | |
1368 | error = locks_verify_locked(inode); | |
1369 | if (!error) { | |
1370 | DQUOT_INIT(inode); | |
1371 | ||
1372 | error = do_truncate(dentry, 0); | |
1373 | } | |
1374 | put_write_access(inode); | |
1375 | if (error) | |
1376 | return error; | |
1377 | } else | |
1378 | if (flag & FMODE_WRITE) | |
1379 | DQUOT_INIT(inode); | |
1380 | ||
1381 | return 0; | |
1382 | } | |
1383 | ||
1384 | /* | |
1385 | * open_namei() | |
1386 | * | |
1387 | * namei for open - this is in fact almost the whole open-routine. | |
1388 | * | |
1389 | * Note that the low bits of "flag" aren't the same as in the open | |
1390 | * system call - they are 00 - no permissions needed | |
1391 | * 01 - read permission needed | |
1392 | * 10 - write permission needed | |
1393 | * 11 - read/write permissions needed | |
1394 | * which is a lot more logical, and also allows the "no perm" needed | |
1395 | * for symlinks (where the permissions are checked later). | |
1396 | * SMP-safe | |
1397 | */ | |
1398 | int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd) | |
1399 | { | |
1400 | int acc_mode, error = 0; | |
4e7506e4 | 1401 | struct path path; |
1da177e4 LT |
1402 | struct dentry *dir; |
1403 | int count = 0; | |
1404 | ||
1405 | acc_mode = ACC_MODE(flag); | |
1406 | ||
1407 | /* Allow the LSM permission hook to distinguish append | |
1408 | access from general write access. */ | |
1409 | if (flag & O_APPEND) | |
1410 | acc_mode |= MAY_APPEND; | |
1411 | ||
1412 | /* Fill in the open() intent data */ | |
1413 | nd->intent.open.flags = flag; | |
1414 | nd->intent.open.create_mode = mode; | |
1415 | ||
1416 | /* | |
1417 | * The simplest case - just a plain lookup. | |
1418 | */ | |
1419 | if (!(flag & O_CREAT)) { | |
1420 | error = path_lookup(pathname, lookup_flags(flag)|LOOKUP_OPEN, nd); | |
1421 | if (error) | |
1422 | return error; | |
1423 | goto ok; | |
1424 | } | |
1425 | ||
1426 | /* | |
1427 | * Create - we need to know the parent. | |
1428 | */ | |
1429 | error = path_lookup(pathname, LOOKUP_PARENT|LOOKUP_OPEN|LOOKUP_CREATE, nd); | |
1430 | if (error) | |
1431 | return error; | |
1432 | ||
1433 | /* | |
1434 | * We have the parent and last component. First of all, check | |
1435 | * that we are not asked to creat(2) an obvious directory - that | |
1436 | * will not do. | |
1437 | */ | |
1438 | error = -EISDIR; | |
1439 | if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len]) | |
1440 | goto exit; | |
1441 | ||
1442 | dir = nd->dentry; | |
1443 | nd->flags &= ~LOOKUP_PARENT; | |
1444 | down(&dir->d_inode->i_sem); | |
4e7506e4 | 1445 | path.dentry = __lookup_hash(&nd->last, nd->dentry, nd); |
d73ffe16 | 1446 | path.mnt = nd->mnt; |
1da177e4 LT |
1447 | |
1448 | do_last: | |
4e7506e4 AV |
1449 | error = PTR_ERR(path.dentry); |
1450 | if (IS_ERR(path.dentry)) { | |
1da177e4 LT |
1451 | up(&dir->d_inode->i_sem); |
1452 | goto exit; | |
1453 | } | |
1454 | ||
1455 | /* Negative dentry, just create the file */ | |
4e7506e4 | 1456 | if (!path.dentry->d_inode) { |
1da177e4 LT |
1457 | if (!IS_POSIXACL(dir->d_inode)) |
1458 | mode &= ~current->fs->umask; | |
4e7506e4 | 1459 | error = vfs_create(dir->d_inode, path.dentry, mode, nd); |
1da177e4 LT |
1460 | up(&dir->d_inode->i_sem); |
1461 | dput(nd->dentry); | |
4e7506e4 | 1462 | nd->dentry = path.dentry; |
1da177e4 LT |
1463 | if (error) |
1464 | goto exit; | |
1465 | /* Don't check for write permission, don't truncate */ | |
1466 | acc_mode = 0; | |
1467 | flag &= ~O_TRUNC; | |
1468 | goto ok; | |
1469 | } | |
1470 | ||
1471 | /* | |
1472 | * It already exists. | |
1473 | */ | |
1474 | up(&dir->d_inode->i_sem); | |
1475 | ||
1476 | error = -EEXIST; | |
1477 | if (flag & O_EXCL) | |
1478 | goto exit_dput; | |
1479 | ||
4e7506e4 | 1480 | if (d_mountpoint(path.dentry)) { |
1da177e4 LT |
1481 | error = -ELOOP; |
1482 | if (flag & O_NOFOLLOW) | |
1483 | goto exit_dput; | |
d73ffe16 AV |
1484 | while (__follow_down(&path.mnt,&path.dentry) && d_mountpoint(path.dentry)); |
1485 | nd->mnt = path.mnt; | |
1da177e4 LT |
1486 | } |
1487 | error = -ENOENT; | |
4e7506e4 | 1488 | if (!path.dentry->d_inode) |
1da177e4 | 1489 | goto exit_dput; |
4e7506e4 | 1490 | if (path.dentry->d_inode->i_op && path.dentry->d_inode->i_op->follow_link) |
1da177e4 LT |
1491 | goto do_link; |
1492 | ||
1493 | dput(nd->dentry); | |
4e7506e4 | 1494 | nd->dentry = path.dentry; |
1da177e4 | 1495 | error = -EISDIR; |
4e7506e4 | 1496 | if (path.dentry->d_inode && S_ISDIR(path.dentry->d_inode->i_mode)) |
1da177e4 LT |
1497 | goto exit; |
1498 | ok: | |
1499 | error = may_open(nd, acc_mode, flag); | |
1500 | if (error) | |
1501 | goto exit; | |
1502 | return 0; | |
1503 | ||
1504 | exit_dput: | |
4e7506e4 | 1505 | dput(path.dentry); |
1da177e4 LT |
1506 | exit: |
1507 | path_release(nd); | |
1508 | return error; | |
1509 | ||
1510 | do_link: | |
1511 | error = -ELOOP; | |
1512 | if (flag & O_NOFOLLOW) | |
1513 | goto exit_dput; | |
1514 | /* | |
1515 | * This is subtle. Instead of calling do_follow_link() we do the | |
1516 | * thing by hands. The reason is that this way we have zero link_count | |
1517 | * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT. | |
1518 | * After that we have the parent and last component, i.e. | |
1519 | * we are in the same situation as after the first path_walk(). | |
1520 | * Well, almost - if the last component is normal we get its copy | |
1521 | * stored in nd->last.name and we will have to putname() it when we | |
1522 | * are done. Procfs-like symlinks just set LAST_BIND. | |
1523 | */ | |
1524 | nd->flags |= LOOKUP_PARENT; | |
4e7506e4 | 1525 | error = security_inode_follow_link(path.dentry, nd); |
1da177e4 LT |
1526 | if (error) |
1527 | goto exit_dput; | |
cd4e91d3 | 1528 | error = __do_follow_link(&path, nd); |
d73ffe16 | 1529 | path.mnt = nd->mnt; |
1da177e4 LT |
1530 | if (error) |
1531 | return error; | |
1532 | nd->flags &= ~LOOKUP_PARENT; | |
1533 | if (nd->last_type == LAST_BIND) { | |
4e7506e4 | 1534 | path.dentry = nd->dentry; |
1da177e4 LT |
1535 | goto ok; |
1536 | } | |
1537 | error = -EISDIR; | |
1538 | if (nd->last_type != LAST_NORM) | |
1539 | goto exit; | |
1540 | if (nd->last.name[nd->last.len]) { | |
1541 | putname(nd->last.name); | |
1542 | goto exit; | |
1543 | } | |
1544 | error = -ELOOP; | |
1545 | if (count++==32) { | |
1546 | putname(nd->last.name); | |
1547 | goto exit; | |
1548 | } | |
1549 | dir = nd->dentry; | |
1550 | down(&dir->d_inode->i_sem); | |
4e7506e4 | 1551 | path.dentry = __lookup_hash(&nd->last, nd->dentry, nd); |
1da177e4 LT |
1552 | putname(nd->last.name); |
1553 | goto do_last; | |
1554 | } | |
1555 | ||
1556 | /** | |
1557 | * lookup_create - lookup a dentry, creating it if it doesn't exist | |
1558 | * @nd: nameidata info | |
1559 | * @is_dir: directory flag | |
1560 | * | |
1561 | * Simple function to lookup and return a dentry and create it | |
1562 | * if it doesn't exist. Is SMP-safe. | |
1563 | */ | |
1564 | struct dentry *lookup_create(struct nameidata *nd, int is_dir) | |
1565 | { | |
1566 | struct dentry *dentry; | |
1567 | ||
1568 | down(&nd->dentry->d_inode->i_sem); | |
1569 | dentry = ERR_PTR(-EEXIST); | |
1570 | if (nd->last_type != LAST_NORM) | |
1571 | goto fail; | |
1572 | nd->flags &= ~LOOKUP_PARENT; | |
1573 | dentry = lookup_hash(&nd->last, nd->dentry); | |
1574 | if (IS_ERR(dentry)) | |
1575 | goto fail; | |
1576 | if (!is_dir && nd->last.name[nd->last.len] && !dentry->d_inode) | |
1577 | goto enoent; | |
1578 | return dentry; | |
1579 | enoent: | |
1580 | dput(dentry); | |
1581 | dentry = ERR_PTR(-ENOENT); | |
1582 | fail: | |
1583 | return dentry; | |
1584 | } | |
f81a0bff | 1585 | EXPORT_SYMBOL_GPL(lookup_create); |
1da177e4 LT |
1586 | |
1587 | int vfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev) | |
1588 | { | |
1589 | int error = may_create(dir, dentry, NULL); | |
1590 | ||
1591 | if (error) | |
1592 | return error; | |
1593 | ||
1594 | if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) | |
1595 | return -EPERM; | |
1596 | ||
1597 | if (!dir->i_op || !dir->i_op->mknod) | |
1598 | return -EPERM; | |
1599 | ||
1600 | error = security_inode_mknod(dir, dentry, mode, dev); | |
1601 | if (error) | |
1602 | return error; | |
1603 | ||
1604 | DQUOT_INIT(dir); | |
1605 | error = dir->i_op->mknod(dir, dentry, mode, dev); | |
1606 | if (!error) { | |
1607 | inode_dir_notify(dir, DN_CREATE); | |
1608 | security_inode_post_mknod(dir, dentry, mode, dev); | |
1609 | } | |
1610 | return error; | |
1611 | } | |
1612 | ||
1613 | asmlinkage long sys_mknod(const char __user * filename, int mode, unsigned dev) | |
1614 | { | |
1615 | int error = 0; | |
1616 | char * tmp; | |
1617 | struct dentry * dentry; | |
1618 | struct nameidata nd; | |
1619 | ||
1620 | if (S_ISDIR(mode)) | |
1621 | return -EPERM; | |
1622 | tmp = getname(filename); | |
1623 | if (IS_ERR(tmp)) | |
1624 | return PTR_ERR(tmp); | |
1625 | ||
1626 | error = path_lookup(tmp, LOOKUP_PARENT, &nd); | |
1627 | if (error) | |
1628 | goto out; | |
1629 | dentry = lookup_create(&nd, 0); | |
1630 | error = PTR_ERR(dentry); | |
1631 | ||
1632 | if (!IS_POSIXACL(nd.dentry->d_inode)) | |
1633 | mode &= ~current->fs->umask; | |
1634 | if (!IS_ERR(dentry)) { | |
1635 | switch (mode & S_IFMT) { | |
1636 | case 0: case S_IFREG: | |
1637 | error = vfs_create(nd.dentry->d_inode,dentry,mode,&nd); | |
1638 | break; | |
1639 | case S_IFCHR: case S_IFBLK: | |
1640 | error = vfs_mknod(nd.dentry->d_inode,dentry,mode, | |
1641 | new_decode_dev(dev)); | |
1642 | break; | |
1643 | case S_IFIFO: case S_IFSOCK: | |
1644 | error = vfs_mknod(nd.dentry->d_inode,dentry,mode,0); | |
1645 | break; | |
1646 | case S_IFDIR: | |
1647 | error = -EPERM; | |
1648 | break; | |
1649 | default: | |
1650 | error = -EINVAL; | |
1651 | } | |
1652 | dput(dentry); | |
1653 | } | |
1654 | up(&nd.dentry->d_inode->i_sem); | |
1655 | path_release(&nd); | |
1656 | out: | |
1657 | putname(tmp); | |
1658 | ||
1659 | return error; | |
1660 | } | |
1661 | ||
1662 | int vfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) | |
1663 | { | |
1664 | int error = may_create(dir, dentry, NULL); | |
1665 | ||
1666 | if (error) | |
1667 | return error; | |
1668 | ||
1669 | if (!dir->i_op || !dir->i_op->mkdir) | |
1670 | return -EPERM; | |
1671 | ||
1672 | mode &= (S_IRWXUGO|S_ISVTX); | |
1673 | error = security_inode_mkdir(dir, dentry, mode); | |
1674 | if (error) | |
1675 | return error; | |
1676 | ||
1677 | DQUOT_INIT(dir); | |
1678 | error = dir->i_op->mkdir(dir, dentry, mode); | |
1679 | if (!error) { | |
1680 | inode_dir_notify(dir, DN_CREATE); | |
1681 | security_inode_post_mkdir(dir,dentry, mode); | |
1682 | } | |
1683 | return error; | |
1684 | } | |
1685 | ||
1686 | asmlinkage long sys_mkdir(const char __user * pathname, int mode) | |
1687 | { | |
1688 | int error = 0; | |
1689 | char * tmp; | |
1690 | ||
1691 | tmp = getname(pathname); | |
1692 | error = PTR_ERR(tmp); | |
1693 | if (!IS_ERR(tmp)) { | |
1694 | struct dentry *dentry; | |
1695 | struct nameidata nd; | |
1696 | ||
1697 | error = path_lookup(tmp, LOOKUP_PARENT, &nd); | |
1698 | if (error) | |
1699 | goto out; | |
1700 | dentry = lookup_create(&nd, 1); | |
1701 | error = PTR_ERR(dentry); | |
1702 | if (!IS_ERR(dentry)) { | |
1703 | if (!IS_POSIXACL(nd.dentry->d_inode)) | |
1704 | mode &= ~current->fs->umask; | |
1705 | error = vfs_mkdir(nd.dentry->d_inode, dentry, mode); | |
1706 | dput(dentry); | |
1707 | } | |
1708 | up(&nd.dentry->d_inode->i_sem); | |
1709 | path_release(&nd); | |
1710 | out: | |
1711 | putname(tmp); | |
1712 | } | |
1713 | ||
1714 | return error; | |
1715 | } | |
1716 | ||
1717 | /* | |
1718 | * We try to drop the dentry early: we should have | |
1719 | * a usage count of 2 if we're the only user of this | |
1720 | * dentry, and if that is true (possibly after pruning | |
1721 | * the dcache), then we drop the dentry now. | |
1722 | * | |
1723 | * A low-level filesystem can, if it choses, legally | |
1724 | * do a | |
1725 | * | |
1726 | * if (!d_unhashed(dentry)) | |
1727 | * return -EBUSY; | |
1728 | * | |
1729 | * if it cannot handle the case of removing a directory | |
1730 | * that is still in use by something else.. | |
1731 | */ | |
1732 | void dentry_unhash(struct dentry *dentry) | |
1733 | { | |
1734 | dget(dentry); | |
1735 | if (atomic_read(&dentry->d_count)) | |
1736 | shrink_dcache_parent(dentry); | |
1737 | spin_lock(&dcache_lock); | |
1738 | spin_lock(&dentry->d_lock); | |
1739 | if (atomic_read(&dentry->d_count) == 2) | |
1740 | __d_drop(dentry); | |
1741 | spin_unlock(&dentry->d_lock); | |
1742 | spin_unlock(&dcache_lock); | |
1743 | } | |
1744 | ||
1745 | int vfs_rmdir(struct inode *dir, struct dentry *dentry) | |
1746 | { | |
1747 | int error = may_delete(dir, dentry, 1); | |
1748 | ||
1749 | if (error) | |
1750 | return error; | |
1751 | ||
1752 | if (!dir->i_op || !dir->i_op->rmdir) | |
1753 | return -EPERM; | |
1754 | ||
1755 | DQUOT_INIT(dir); | |
1756 | ||
1757 | down(&dentry->d_inode->i_sem); | |
1758 | dentry_unhash(dentry); | |
1759 | if (d_mountpoint(dentry)) | |
1760 | error = -EBUSY; | |
1761 | else { | |
1762 | error = security_inode_rmdir(dir, dentry); | |
1763 | if (!error) { | |
1764 | error = dir->i_op->rmdir(dir, dentry); | |
1765 | if (!error) | |
1766 | dentry->d_inode->i_flags |= S_DEAD; | |
1767 | } | |
1768 | } | |
1769 | up(&dentry->d_inode->i_sem); | |
1770 | if (!error) { | |
1771 | inode_dir_notify(dir, DN_DELETE); | |
1772 | d_delete(dentry); | |
1773 | } | |
1774 | dput(dentry); | |
1775 | ||
1776 | return error; | |
1777 | } | |
1778 | ||
1779 | asmlinkage long sys_rmdir(const char __user * pathname) | |
1780 | { | |
1781 | int error = 0; | |
1782 | char * name; | |
1783 | struct dentry *dentry; | |
1784 | struct nameidata nd; | |
1785 | ||
1786 | name = getname(pathname); | |
1787 | if(IS_ERR(name)) | |
1788 | return PTR_ERR(name); | |
1789 | ||
1790 | error = path_lookup(name, LOOKUP_PARENT, &nd); | |
1791 | if (error) | |
1792 | goto exit; | |
1793 | ||
1794 | switch(nd.last_type) { | |
1795 | case LAST_DOTDOT: | |
1796 | error = -ENOTEMPTY; | |
1797 | goto exit1; | |
1798 | case LAST_DOT: | |
1799 | error = -EINVAL; | |
1800 | goto exit1; | |
1801 | case LAST_ROOT: | |
1802 | error = -EBUSY; | |
1803 | goto exit1; | |
1804 | } | |
1805 | down(&nd.dentry->d_inode->i_sem); | |
1806 | dentry = lookup_hash(&nd.last, nd.dentry); | |
1807 | error = PTR_ERR(dentry); | |
1808 | if (!IS_ERR(dentry)) { | |
1809 | error = vfs_rmdir(nd.dentry->d_inode, dentry); | |
1810 | dput(dentry); | |
1811 | } | |
1812 | up(&nd.dentry->d_inode->i_sem); | |
1813 | exit1: | |
1814 | path_release(&nd); | |
1815 | exit: | |
1816 | putname(name); | |
1817 | return error; | |
1818 | } | |
1819 | ||
1820 | int vfs_unlink(struct inode *dir, struct dentry *dentry) | |
1821 | { | |
1822 | int error = may_delete(dir, dentry, 0); | |
1823 | ||
1824 | if (error) | |
1825 | return error; | |
1826 | ||
1827 | if (!dir->i_op || !dir->i_op->unlink) | |
1828 | return -EPERM; | |
1829 | ||
1830 | DQUOT_INIT(dir); | |
1831 | ||
1832 | down(&dentry->d_inode->i_sem); | |
1833 | if (d_mountpoint(dentry)) | |
1834 | error = -EBUSY; | |
1835 | else { | |
1836 | error = security_inode_unlink(dir, dentry); | |
1837 | if (!error) | |
1838 | error = dir->i_op->unlink(dir, dentry); | |
1839 | } | |
1840 | up(&dentry->d_inode->i_sem); | |
1841 | ||
1842 | /* We don't d_delete() NFS sillyrenamed files--they still exist. */ | |
1843 | if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { | |
1844 | d_delete(dentry); | |
1845 | inode_dir_notify(dir, DN_DELETE); | |
1846 | } | |
1847 | return error; | |
1848 | } | |
1849 | ||
1850 | /* | |
1851 | * Make sure that the actual truncation of the file will occur outside its | |
1852 | * directory's i_sem. Truncate can take a long time if there is a lot of | |
1853 | * writeout happening, and we don't want to prevent access to the directory | |
1854 | * while waiting on the I/O. | |
1855 | */ | |
1856 | asmlinkage long sys_unlink(const char __user * pathname) | |
1857 | { | |
1858 | int error = 0; | |
1859 | char * name; | |
1860 | struct dentry *dentry; | |
1861 | struct nameidata nd; | |
1862 | struct inode *inode = NULL; | |
1863 | ||
1864 | name = getname(pathname); | |
1865 | if(IS_ERR(name)) | |
1866 | return PTR_ERR(name); | |
1867 | ||
1868 | error = path_lookup(name, LOOKUP_PARENT, &nd); | |
1869 | if (error) | |
1870 | goto exit; | |
1871 | error = -EISDIR; | |
1872 | if (nd.last_type != LAST_NORM) | |
1873 | goto exit1; | |
1874 | down(&nd.dentry->d_inode->i_sem); | |
1875 | dentry = lookup_hash(&nd.last, nd.dentry); | |
1876 | error = PTR_ERR(dentry); | |
1877 | if (!IS_ERR(dentry)) { | |
1878 | /* Why not before? Because we want correct error value */ | |
1879 | if (nd.last.name[nd.last.len]) | |
1880 | goto slashes; | |
1881 | inode = dentry->d_inode; | |
1882 | if (inode) | |
1883 | atomic_inc(&inode->i_count); | |
1884 | error = vfs_unlink(nd.dentry->d_inode, dentry); | |
1885 | exit2: | |
1886 | dput(dentry); | |
1887 | } | |
1888 | up(&nd.dentry->d_inode->i_sem); | |
1889 | if (inode) | |
1890 | iput(inode); /* truncate the inode here */ | |
1891 | exit1: | |
1892 | path_release(&nd); | |
1893 | exit: | |
1894 | putname(name); | |
1895 | return error; | |
1896 | ||
1897 | slashes: | |
1898 | error = !dentry->d_inode ? -ENOENT : | |
1899 | S_ISDIR(dentry->d_inode->i_mode) ? -EISDIR : -ENOTDIR; | |
1900 | goto exit2; | |
1901 | } | |
1902 | ||
1903 | int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname, int mode) | |
1904 | { | |
1905 | int error = may_create(dir, dentry, NULL); | |
1906 | ||
1907 | if (error) | |
1908 | return error; | |
1909 | ||
1910 | if (!dir->i_op || !dir->i_op->symlink) | |
1911 | return -EPERM; | |
1912 | ||
1913 | error = security_inode_symlink(dir, dentry, oldname); | |
1914 | if (error) | |
1915 | return error; | |
1916 | ||
1917 | DQUOT_INIT(dir); | |
1918 | error = dir->i_op->symlink(dir, dentry, oldname); | |
1919 | if (!error) { | |
1920 | inode_dir_notify(dir, DN_CREATE); | |
1921 | security_inode_post_symlink(dir, dentry, oldname); | |
1922 | } | |
1923 | return error; | |
1924 | } | |
1925 | ||
1926 | asmlinkage long sys_symlink(const char __user * oldname, const char __user * newname) | |
1927 | { | |
1928 | int error = 0; | |
1929 | char * from; | |
1930 | char * to; | |
1931 | ||
1932 | from = getname(oldname); | |
1933 | if(IS_ERR(from)) | |
1934 | return PTR_ERR(from); | |
1935 | to = getname(newname); | |
1936 | error = PTR_ERR(to); | |
1937 | if (!IS_ERR(to)) { | |
1938 | struct dentry *dentry; | |
1939 | struct nameidata nd; | |
1940 | ||
1941 | error = path_lookup(to, LOOKUP_PARENT, &nd); | |
1942 | if (error) | |
1943 | goto out; | |
1944 | dentry = lookup_create(&nd, 0); | |
1945 | error = PTR_ERR(dentry); | |
1946 | if (!IS_ERR(dentry)) { | |
1947 | error = vfs_symlink(nd.dentry->d_inode, dentry, from, S_IALLUGO); | |
1948 | dput(dentry); | |
1949 | } | |
1950 | up(&nd.dentry->d_inode->i_sem); | |
1951 | path_release(&nd); | |
1952 | out: | |
1953 | putname(to); | |
1954 | } | |
1955 | putname(from); | |
1956 | return error; | |
1957 | } | |
1958 | ||
1959 | int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry) | |
1960 | { | |
1961 | struct inode *inode = old_dentry->d_inode; | |
1962 | int error; | |
1963 | ||
1964 | if (!inode) | |
1965 | return -ENOENT; | |
1966 | ||
1967 | error = may_create(dir, new_dentry, NULL); | |
1968 | if (error) | |
1969 | return error; | |
1970 | ||
1971 | if (dir->i_sb != inode->i_sb) | |
1972 | return -EXDEV; | |
1973 | ||
1974 | /* | |
1975 | * A link to an append-only or immutable file cannot be created. | |
1976 | */ | |
1977 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | |
1978 | return -EPERM; | |
1979 | if (!dir->i_op || !dir->i_op->link) | |
1980 | return -EPERM; | |
1981 | if (S_ISDIR(old_dentry->d_inode->i_mode)) | |
1982 | return -EPERM; | |
1983 | ||
1984 | error = security_inode_link(old_dentry, dir, new_dentry); | |
1985 | if (error) | |
1986 | return error; | |
1987 | ||
1988 | down(&old_dentry->d_inode->i_sem); | |
1989 | DQUOT_INIT(dir); | |
1990 | error = dir->i_op->link(old_dentry, dir, new_dentry); | |
1991 | up(&old_dentry->d_inode->i_sem); | |
1992 | if (!error) { | |
1993 | inode_dir_notify(dir, DN_CREATE); | |
1994 | security_inode_post_link(old_dentry, dir, new_dentry); | |
1995 | } | |
1996 | return error; | |
1997 | } | |
1998 | ||
1999 | /* | |
2000 | * Hardlinks are often used in delicate situations. We avoid | |
2001 | * security-related surprises by not following symlinks on the | |
2002 | * newname. --KAB | |
2003 | * | |
2004 | * We don't follow them on the oldname either to be compatible | |
2005 | * with linux 2.0, and to avoid hard-linking to directories | |
2006 | * and other special files. --ADM | |
2007 | */ | |
2008 | asmlinkage long sys_link(const char __user * oldname, const char __user * newname) | |
2009 | { | |
2010 | struct dentry *new_dentry; | |
2011 | struct nameidata nd, old_nd; | |
2012 | int error; | |
2013 | char * to; | |
2014 | ||
2015 | to = getname(newname); | |
2016 | if (IS_ERR(to)) | |
2017 | return PTR_ERR(to); | |
2018 | ||
2019 | error = __user_walk(oldname, 0, &old_nd); | |
2020 | if (error) | |
2021 | goto exit; | |
2022 | error = path_lookup(to, LOOKUP_PARENT, &nd); | |
2023 | if (error) | |
2024 | goto out; | |
2025 | error = -EXDEV; | |
2026 | if (old_nd.mnt != nd.mnt) | |
2027 | goto out_release; | |
2028 | new_dentry = lookup_create(&nd, 0); | |
2029 | error = PTR_ERR(new_dentry); | |
2030 | if (!IS_ERR(new_dentry)) { | |
2031 | error = vfs_link(old_nd.dentry, nd.dentry->d_inode, new_dentry); | |
2032 | dput(new_dentry); | |
2033 | } | |
2034 | up(&nd.dentry->d_inode->i_sem); | |
2035 | out_release: | |
2036 | path_release(&nd); | |
2037 | out: | |
2038 | path_release(&old_nd); | |
2039 | exit: | |
2040 | putname(to); | |
2041 | ||
2042 | return error; | |
2043 | } | |
2044 | ||
2045 | /* | |
2046 | * The worst of all namespace operations - renaming directory. "Perverted" | |
2047 | * doesn't even start to describe it. Somebody in UCB had a heck of a trip... | |
2048 | * Problems: | |
2049 | * a) we can get into loop creation. Check is done in is_subdir(). | |
2050 | * b) race potential - two innocent renames can create a loop together. | |
2051 | * That's where 4.4 screws up. Current fix: serialization on | |
2052 | * sb->s_vfs_rename_sem. We might be more accurate, but that's another | |
2053 | * story. | |
2054 | * c) we have to lock _three_ objects - parents and victim (if it exists). | |
2055 | * And that - after we got ->i_sem on parents (until then we don't know | |
2056 | * whether the target exists). Solution: try to be smart with locking | |
2057 | * order for inodes. We rely on the fact that tree topology may change | |
2058 | * only under ->s_vfs_rename_sem _and_ that parent of the object we | |
2059 | * move will be locked. Thus we can rank directories by the tree | |
2060 | * (ancestors first) and rank all non-directories after them. | |
2061 | * That works since everybody except rename does "lock parent, lookup, | |
2062 | * lock child" and rename is under ->s_vfs_rename_sem. | |
2063 | * HOWEVER, it relies on the assumption that any object with ->lookup() | |
2064 | * has no more than 1 dentry. If "hybrid" objects will ever appear, | |
2065 | * we'd better make sure that there's no link(2) for them. | |
2066 | * d) some filesystems don't support opened-but-unlinked directories, | |
2067 | * either because of layout or because they are not ready to deal with | |
2068 | * all cases correctly. The latter will be fixed (taking this sort of | |
2069 | * stuff into VFS), but the former is not going away. Solution: the same | |
2070 | * trick as in rmdir(). | |
2071 | * e) conversion from fhandle to dentry may come in the wrong moment - when | |
2072 | * we are removing the target. Solution: we will have to grab ->i_sem | |
2073 | * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on | |
2074 | * ->i_sem on parents, which works but leads to some truely excessive | |
2075 | * locking]. | |
2076 | */ | |
75c96f85 AB |
2077 | static int vfs_rename_dir(struct inode *old_dir, struct dentry *old_dentry, |
2078 | struct inode *new_dir, struct dentry *new_dentry) | |
1da177e4 LT |
2079 | { |
2080 | int error = 0; | |
2081 | struct inode *target; | |
2082 | ||
2083 | /* | |
2084 | * If we are going to change the parent - check write permissions, | |
2085 | * we'll need to flip '..'. | |
2086 | */ | |
2087 | if (new_dir != old_dir) { | |
2088 | error = permission(old_dentry->d_inode, MAY_WRITE, NULL); | |
2089 | if (error) | |
2090 | return error; | |
2091 | } | |
2092 | ||
2093 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); | |
2094 | if (error) | |
2095 | return error; | |
2096 | ||
2097 | target = new_dentry->d_inode; | |
2098 | if (target) { | |
2099 | down(&target->i_sem); | |
2100 | dentry_unhash(new_dentry); | |
2101 | } | |
2102 | if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) | |
2103 | error = -EBUSY; | |
2104 | else | |
2105 | error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); | |
2106 | if (target) { | |
2107 | if (!error) | |
2108 | target->i_flags |= S_DEAD; | |
2109 | up(&target->i_sem); | |
2110 | if (d_unhashed(new_dentry)) | |
2111 | d_rehash(new_dentry); | |
2112 | dput(new_dentry); | |
2113 | } | |
2114 | if (!error) { | |
2115 | d_move(old_dentry,new_dentry); | |
2116 | security_inode_post_rename(old_dir, old_dentry, | |
2117 | new_dir, new_dentry); | |
2118 | } | |
2119 | return error; | |
2120 | } | |
2121 | ||
75c96f85 AB |
2122 | static int vfs_rename_other(struct inode *old_dir, struct dentry *old_dentry, |
2123 | struct inode *new_dir, struct dentry *new_dentry) | |
1da177e4 LT |
2124 | { |
2125 | struct inode *target; | |
2126 | int error; | |
2127 | ||
2128 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry); | |
2129 | if (error) | |
2130 | return error; | |
2131 | ||
2132 | dget(new_dentry); | |
2133 | target = new_dentry->d_inode; | |
2134 | if (target) | |
2135 | down(&target->i_sem); | |
2136 | if (d_mountpoint(old_dentry)||d_mountpoint(new_dentry)) | |
2137 | error = -EBUSY; | |
2138 | else | |
2139 | error = old_dir->i_op->rename(old_dir, old_dentry, new_dir, new_dentry); | |
2140 | if (!error) { | |
2141 | /* The following d_move() should become unconditional */ | |
2142 | if (!(old_dir->i_sb->s_type->fs_flags & FS_ODD_RENAME)) | |
2143 | d_move(old_dentry, new_dentry); | |
2144 | security_inode_post_rename(old_dir, old_dentry, new_dir, new_dentry); | |
2145 | } | |
2146 | if (target) | |
2147 | up(&target->i_sem); | |
2148 | dput(new_dentry); | |
2149 | return error; | |
2150 | } | |
2151 | ||
2152 | int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, | |
2153 | struct inode *new_dir, struct dentry *new_dentry) | |
2154 | { | |
2155 | int error; | |
2156 | int is_dir = S_ISDIR(old_dentry->d_inode->i_mode); | |
2157 | ||
2158 | if (old_dentry->d_inode == new_dentry->d_inode) | |
2159 | return 0; | |
2160 | ||
2161 | error = may_delete(old_dir, old_dentry, is_dir); | |
2162 | if (error) | |
2163 | return error; | |
2164 | ||
2165 | if (!new_dentry->d_inode) | |
2166 | error = may_create(new_dir, new_dentry, NULL); | |
2167 | else | |
2168 | error = may_delete(new_dir, new_dentry, is_dir); | |
2169 | if (error) | |
2170 | return error; | |
2171 | ||
2172 | if (!old_dir->i_op || !old_dir->i_op->rename) | |
2173 | return -EPERM; | |
2174 | ||
2175 | DQUOT_INIT(old_dir); | |
2176 | DQUOT_INIT(new_dir); | |
2177 | ||
2178 | if (is_dir) | |
2179 | error = vfs_rename_dir(old_dir,old_dentry,new_dir,new_dentry); | |
2180 | else | |
2181 | error = vfs_rename_other(old_dir,old_dentry,new_dir,new_dentry); | |
2182 | if (!error) { | |
2183 | if (old_dir == new_dir) | |
2184 | inode_dir_notify(old_dir, DN_RENAME); | |
2185 | else { | |
2186 | inode_dir_notify(old_dir, DN_DELETE); | |
2187 | inode_dir_notify(new_dir, DN_CREATE); | |
2188 | } | |
2189 | } | |
2190 | return error; | |
2191 | } | |
2192 | ||
2193 | static inline int do_rename(const char * oldname, const char * newname) | |
2194 | { | |
2195 | int error = 0; | |
2196 | struct dentry * old_dir, * new_dir; | |
2197 | struct dentry * old_dentry, *new_dentry; | |
2198 | struct dentry * trap; | |
2199 | struct nameidata oldnd, newnd; | |
2200 | ||
2201 | error = path_lookup(oldname, LOOKUP_PARENT, &oldnd); | |
2202 | if (error) | |
2203 | goto exit; | |
2204 | ||
2205 | error = path_lookup(newname, LOOKUP_PARENT, &newnd); | |
2206 | if (error) | |
2207 | goto exit1; | |
2208 | ||
2209 | error = -EXDEV; | |
2210 | if (oldnd.mnt != newnd.mnt) | |
2211 | goto exit2; | |
2212 | ||
2213 | old_dir = oldnd.dentry; | |
2214 | error = -EBUSY; | |
2215 | if (oldnd.last_type != LAST_NORM) | |
2216 | goto exit2; | |
2217 | ||
2218 | new_dir = newnd.dentry; | |
2219 | if (newnd.last_type != LAST_NORM) | |
2220 | goto exit2; | |
2221 | ||
2222 | trap = lock_rename(new_dir, old_dir); | |
2223 | ||
2224 | old_dentry = lookup_hash(&oldnd.last, old_dir); | |
2225 | error = PTR_ERR(old_dentry); | |
2226 | if (IS_ERR(old_dentry)) | |
2227 | goto exit3; | |
2228 | /* source must exist */ | |
2229 | error = -ENOENT; | |
2230 | if (!old_dentry->d_inode) | |
2231 | goto exit4; | |
2232 | /* unless the source is a directory trailing slashes give -ENOTDIR */ | |
2233 | if (!S_ISDIR(old_dentry->d_inode->i_mode)) { | |
2234 | error = -ENOTDIR; | |
2235 | if (oldnd.last.name[oldnd.last.len]) | |
2236 | goto exit4; | |
2237 | if (newnd.last.name[newnd.last.len]) | |
2238 | goto exit4; | |
2239 | } | |
2240 | /* source should not be ancestor of target */ | |
2241 | error = -EINVAL; | |
2242 | if (old_dentry == trap) | |
2243 | goto exit4; | |
2244 | new_dentry = lookup_hash(&newnd.last, new_dir); | |
2245 | error = PTR_ERR(new_dentry); | |
2246 | if (IS_ERR(new_dentry)) | |
2247 | goto exit4; | |
2248 | /* target should not be an ancestor of source */ | |
2249 | error = -ENOTEMPTY; | |
2250 | if (new_dentry == trap) | |
2251 | goto exit5; | |
2252 | ||
2253 | error = vfs_rename(old_dir->d_inode, old_dentry, | |
2254 | new_dir->d_inode, new_dentry); | |
2255 | exit5: | |
2256 | dput(new_dentry); | |
2257 | exit4: | |
2258 | dput(old_dentry); | |
2259 | exit3: | |
2260 | unlock_rename(new_dir, old_dir); | |
2261 | exit2: | |
2262 | path_release(&newnd); | |
2263 | exit1: | |
2264 | path_release(&oldnd); | |
2265 | exit: | |
2266 | return error; | |
2267 | } | |
2268 | ||
2269 | asmlinkage long sys_rename(const char __user * oldname, const char __user * newname) | |
2270 | { | |
2271 | int error; | |
2272 | char * from; | |
2273 | char * to; | |
2274 | ||
2275 | from = getname(oldname); | |
2276 | if(IS_ERR(from)) | |
2277 | return PTR_ERR(from); | |
2278 | to = getname(newname); | |
2279 | error = PTR_ERR(to); | |
2280 | if (!IS_ERR(to)) { | |
2281 | error = do_rename(from,to); | |
2282 | putname(to); | |
2283 | } | |
2284 | putname(from); | |
2285 | return error; | |
2286 | } | |
2287 | ||
2288 | int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen, const char *link) | |
2289 | { | |
2290 | int len; | |
2291 | ||
2292 | len = PTR_ERR(link); | |
2293 | if (IS_ERR(link)) | |
2294 | goto out; | |
2295 | ||
2296 | len = strlen(link); | |
2297 | if (len > (unsigned) buflen) | |
2298 | len = buflen; | |
2299 | if (copy_to_user(buffer, link, len)) | |
2300 | len = -EFAULT; | |
2301 | out: | |
2302 | return len; | |
2303 | } | |
2304 | ||
2305 | /* | |
2306 | * A helper for ->readlink(). This should be used *ONLY* for symlinks that | |
2307 | * have ->follow_link() touching nd only in nd_set_link(). Using (or not | |
2308 | * using) it for any given inode is up to filesystem. | |
2309 | */ | |
2310 | int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) | |
2311 | { | |
2312 | struct nameidata nd; | |
2313 | int res; | |
2314 | nd.depth = 0; | |
2315 | res = dentry->d_inode->i_op->follow_link(dentry, &nd); | |
2316 | if (!res) { | |
2317 | res = vfs_readlink(dentry, buffer, buflen, nd_get_link(&nd)); | |
2318 | if (dentry->d_inode->i_op->put_link) | |
2319 | dentry->d_inode->i_op->put_link(dentry, &nd); | |
2320 | } | |
2321 | return res; | |
2322 | } | |
2323 | ||
2324 | int vfs_follow_link(struct nameidata *nd, const char *link) | |
2325 | { | |
2326 | return __vfs_follow_link(nd, link); | |
2327 | } | |
2328 | ||
2329 | /* get the link contents into pagecache */ | |
2330 | static char *page_getlink(struct dentry * dentry, struct page **ppage) | |
2331 | { | |
2332 | struct page * page; | |
2333 | struct address_space *mapping = dentry->d_inode->i_mapping; | |
2334 | page = read_cache_page(mapping, 0, (filler_t *)mapping->a_ops->readpage, | |
2335 | NULL); | |
2336 | if (IS_ERR(page)) | |
2337 | goto sync_fail; | |
2338 | wait_on_page_locked(page); | |
2339 | if (!PageUptodate(page)) | |
2340 | goto async_fail; | |
2341 | *ppage = page; | |
2342 | return kmap(page); | |
2343 | ||
2344 | async_fail: | |
2345 | page_cache_release(page); | |
2346 | return ERR_PTR(-EIO); | |
2347 | ||
2348 | sync_fail: | |
2349 | return (char*)page; | |
2350 | } | |
2351 | ||
2352 | int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) | |
2353 | { | |
2354 | struct page *page = NULL; | |
2355 | char *s = page_getlink(dentry, &page); | |
2356 | int res = vfs_readlink(dentry,buffer,buflen,s); | |
2357 | if (page) { | |
2358 | kunmap(page); | |
2359 | page_cache_release(page); | |
2360 | } | |
2361 | return res; | |
2362 | } | |
2363 | ||
2364 | int page_follow_link_light(struct dentry *dentry, struct nameidata *nd) | |
2365 | { | |
2366 | struct page *page; | |
2367 | nd_set_link(nd, page_getlink(dentry, &page)); | |
2368 | return 0; | |
2369 | } | |
2370 | ||
2371 | void page_put_link(struct dentry *dentry, struct nameidata *nd) | |
2372 | { | |
2373 | if (!IS_ERR(nd_get_link(nd))) { | |
2374 | struct page *page; | |
2375 | page = find_get_page(dentry->d_inode->i_mapping, 0); | |
2376 | if (!page) | |
2377 | BUG(); | |
2378 | kunmap(page); | |
2379 | page_cache_release(page); | |
2380 | page_cache_release(page); | |
2381 | } | |
2382 | } | |
2383 | ||
2384 | int page_symlink(struct inode *inode, const char *symname, int len) | |
2385 | { | |
2386 | struct address_space *mapping = inode->i_mapping; | |
2387 | struct page *page = grab_cache_page(mapping, 0); | |
2388 | int err = -ENOMEM; | |
2389 | char *kaddr; | |
2390 | ||
2391 | if (!page) | |
2392 | goto fail; | |
2393 | err = mapping->a_ops->prepare_write(NULL, page, 0, len-1); | |
2394 | if (err) | |
2395 | goto fail_map; | |
2396 | kaddr = kmap_atomic(page, KM_USER0); | |
2397 | memcpy(kaddr, symname, len-1); | |
2398 | kunmap_atomic(kaddr, KM_USER0); | |
2399 | mapping->a_ops->commit_write(NULL, page, 0, len-1); | |
2400 | /* | |
2401 | * Notice that we are _not_ going to block here - end of page is | |
2402 | * unmapped, so this will only try to map the rest of page, see | |
2403 | * that it is unmapped (typically even will not look into inode - | |
2404 | * ->i_size will be enough for everything) and zero it out. | |
2405 | * OTOH it's obviously correct and should make the page up-to-date. | |
2406 | */ | |
2407 | if (!PageUptodate(page)) { | |
2408 | err = mapping->a_ops->readpage(NULL, page); | |
2409 | wait_on_page_locked(page); | |
2410 | } else { | |
2411 | unlock_page(page); | |
2412 | } | |
2413 | page_cache_release(page); | |
2414 | if (err < 0) | |
2415 | goto fail; | |
2416 | mark_inode_dirty(inode); | |
2417 | return 0; | |
2418 | fail_map: | |
2419 | unlock_page(page); | |
2420 | page_cache_release(page); | |
2421 | fail: | |
2422 | return err; | |
2423 | } | |
2424 | ||
2425 | struct inode_operations page_symlink_inode_operations = { | |
2426 | .readlink = generic_readlink, | |
2427 | .follow_link = page_follow_link_light, | |
2428 | .put_link = page_put_link, | |
2429 | }; | |
2430 | ||
2431 | EXPORT_SYMBOL(__user_walk); | |
2432 | EXPORT_SYMBOL(follow_down); | |
2433 | EXPORT_SYMBOL(follow_up); | |
2434 | EXPORT_SYMBOL(get_write_access); /* binfmt_aout */ | |
2435 | EXPORT_SYMBOL(getname); | |
2436 | EXPORT_SYMBOL(lock_rename); | |
2437 | EXPORT_SYMBOL(lookup_hash); | |
2438 | EXPORT_SYMBOL(lookup_one_len); | |
2439 | EXPORT_SYMBOL(page_follow_link_light); | |
2440 | EXPORT_SYMBOL(page_put_link); | |
2441 | EXPORT_SYMBOL(page_readlink); | |
2442 | EXPORT_SYMBOL(page_symlink); | |
2443 | EXPORT_SYMBOL(page_symlink_inode_operations); | |
2444 | EXPORT_SYMBOL(path_lookup); | |
2445 | EXPORT_SYMBOL(path_release); | |
2446 | EXPORT_SYMBOL(path_walk); | |
2447 | EXPORT_SYMBOL(permission); | |
2448 | EXPORT_SYMBOL(unlock_rename); | |
2449 | EXPORT_SYMBOL(vfs_create); | |
2450 | EXPORT_SYMBOL(vfs_follow_link); | |
2451 | EXPORT_SYMBOL(vfs_link); | |
2452 | EXPORT_SYMBOL(vfs_mkdir); | |
2453 | EXPORT_SYMBOL(vfs_mknod); | |
2454 | EXPORT_SYMBOL(generic_permission); | |
2455 | EXPORT_SYMBOL(vfs_readlink); | |
2456 | EXPORT_SYMBOL(vfs_rename); | |
2457 | EXPORT_SYMBOL(vfs_rmdir); | |
2458 | EXPORT_SYMBOL(vfs_symlink); | |
2459 | EXPORT_SYMBOL(vfs_unlink); | |
2460 | EXPORT_SYMBOL(dentry_unhash); | |
2461 | EXPORT_SYMBOL(generic_readlink); |