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