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