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