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