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