]>
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> | |
630d9c47 | 18 | #include <linux/export.h> |
44696908 | 19 | #include <linux/kernel.h> |
1da177e4 LT |
20 | #include <linux/slab.h> |
21 | #include <linux/fs.h> | |
22 | #include <linux/namei.h> | |
1da177e4 | 23 | #include <linux/pagemap.h> |
0eeca283 | 24 | #include <linux/fsnotify.h> |
1da177e4 LT |
25 | #include <linux/personality.h> |
26 | #include <linux/security.h> | |
6146f0d5 | 27 | #include <linux/ima.h> |
1da177e4 LT |
28 | #include <linux/syscalls.h> |
29 | #include <linux/mount.h> | |
30 | #include <linux/audit.h> | |
16f7e0fe | 31 | #include <linux/capability.h> |
834f2a4a | 32 | #include <linux/file.h> |
5590ff0d | 33 | #include <linux/fcntl.h> |
08ce5f16 | 34 | #include <linux/device_cgroup.h> |
5ad4e53b | 35 | #include <linux/fs_struct.h> |
e77819e5 | 36 | #include <linux/posix_acl.h> |
99d263d4 | 37 | #include <linux/hash.h> |
1da177e4 LT |
38 | #include <asm/uaccess.h> |
39 | ||
e81e3f4d | 40 | #include "internal.h" |
c7105365 | 41 | #include "mount.h" |
e81e3f4d | 42 | |
1da177e4 LT |
43 | /* [Feb-1997 T. Schoebel-Theuer] |
44 | * Fundamental changes in the pathname lookup mechanisms (namei) | |
45 | * were necessary because of omirr. The reason is that omirr needs | |
46 | * to know the _real_ pathname, not the user-supplied one, in case | |
47 | * of symlinks (and also when transname replacements occur). | |
48 | * | |
49 | * The new code replaces the old recursive symlink resolution with | |
50 | * an iterative one (in case of non-nested symlink chains). It does | |
51 | * this with calls to <fs>_follow_link(). | |
52 | * As a side effect, dir_namei(), _namei() and follow_link() are now | |
53 | * replaced with a single function lookup_dentry() that can handle all | |
54 | * the special cases of the former code. | |
55 | * | |
56 | * With the new dcache, the pathname is stored at each inode, at least as | |
57 | * long as the refcount of the inode is positive. As a side effect, the | |
58 | * size of the dcache depends on the inode cache and thus is dynamic. | |
59 | * | |
60 | * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink | |
61 | * resolution to correspond with current state of the code. | |
62 | * | |
63 | * Note that the symlink resolution is not *completely* iterative. | |
64 | * There is still a significant amount of tail- and mid- recursion in | |
65 | * the algorithm. Also, note that <fs>_readlink() is not used in | |
66 | * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() | |
67 | * may return different results than <fs>_follow_link(). Many virtual | |
68 | * filesystems (including /proc) exhibit this behavior. | |
69 | */ | |
70 | ||
71 | /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: | |
72 | * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL | |
73 | * and the name already exists in form of a symlink, try to create the new | |
74 | * name indicated by the symlink. The old code always complained that the | |
75 | * name already exists, due to not following the symlink even if its target | |
76 | * is nonexistent. The new semantics affects also mknod() and link() when | |
25985edc | 77 | * the name is a symlink pointing to a non-existent name. |
1da177e4 LT |
78 | * |
79 | * I don't know which semantics is the right one, since I have no access | |
80 | * to standards. But I found by trial that HP-UX 9.0 has the full "new" | |
81 | * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the | |
82 | * "old" one. Personally, I think the new semantics is much more logical. | |
83 | * Note that "ln old new" where "new" is a symlink pointing to a non-existing | |
84 | * file does succeed in both HP-UX and SunOs, but not in Solaris | |
85 | * and in the old Linux semantics. | |
86 | */ | |
87 | ||
88 | /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink | |
89 | * semantics. See the comments in "open_namei" and "do_link" below. | |
90 | * | |
91 | * [10-Sep-98 Alan Modra] Another symlink change. | |
92 | */ | |
93 | ||
94 | /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: | |
95 | * inside the path - always follow. | |
96 | * in the last component in creation/removal/renaming - never follow. | |
97 | * if LOOKUP_FOLLOW passed - follow. | |
98 | * if the pathname has trailing slashes - follow. | |
99 | * otherwise - don't follow. | |
100 | * (applied in that order). | |
101 | * | |
102 | * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT | |
103 | * restored for 2.4. This is the last surviving part of old 4.2BSD bug. | |
104 | * During the 2.4 we need to fix the userland stuff depending on it - | |
105 | * hopefully we will be able to get rid of that wart in 2.5. So far only | |
106 | * XEmacs seems to be relying on it... | |
107 | */ | |
108 | /* | |
109 | * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) | |
a11f3a05 | 110 | * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives |
1da177e4 LT |
111 | * any extra contention... |
112 | */ | |
113 | ||
114 | /* In order to reduce some races, while at the same time doing additional | |
115 | * checking and hopefully speeding things up, we copy filenames to the | |
116 | * kernel data space before using them.. | |
117 | * | |
118 | * POSIX.1 2.4: an empty pathname is invalid (ENOENT). | |
119 | * PATH_MAX includes the nul terminator --RR. | |
120 | */ | |
91a27b2a | 121 | |
fd2f7cb5 | 122 | #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname)) |
7950e385 | 123 | |
51f39a1f | 124 | struct filename * |
91a27b2a JL |
125 | getname_flags(const char __user *filename, int flags, int *empty) |
126 | { | |
94b5d262 | 127 | struct filename *result; |
7950e385 | 128 | char *kname; |
94b5d262 | 129 | int len; |
4043cde8 | 130 | |
7ac86265 JL |
131 | result = audit_reusename(filename); |
132 | if (result) | |
133 | return result; | |
134 | ||
7950e385 | 135 | result = __getname(); |
3f9f0aa6 | 136 | if (unlikely(!result)) |
4043cde8 EP |
137 | return ERR_PTR(-ENOMEM); |
138 | ||
7950e385 JL |
139 | /* |
140 | * First, try to embed the struct filename inside the names_cache | |
141 | * allocation | |
142 | */ | |
fd2f7cb5 | 143 | kname = (char *)result->iname; |
91a27b2a | 144 | result->name = kname; |
7950e385 | 145 | |
94b5d262 | 146 | len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX); |
91a27b2a | 147 | if (unlikely(len < 0)) { |
94b5d262 AV |
148 | __putname(result); |
149 | return ERR_PTR(len); | |
91a27b2a | 150 | } |
3f9f0aa6 | 151 | |
7950e385 JL |
152 | /* |
153 | * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a | |
154 | * separate struct filename so we can dedicate the entire | |
155 | * names_cache allocation for the pathname, and re-do the copy from | |
156 | * userland. | |
157 | */ | |
94b5d262 | 158 | if (unlikely(len == EMBEDDED_NAME_MAX)) { |
fd2f7cb5 | 159 | const size_t size = offsetof(struct filename, iname[1]); |
7950e385 JL |
160 | kname = (char *)result; |
161 | ||
fd2f7cb5 AV |
162 | /* |
163 | * size is chosen that way we to guarantee that | |
164 | * result->iname[0] is within the same object and that | |
165 | * kname can't be equal to result->iname, no matter what. | |
166 | */ | |
167 | result = kzalloc(size, GFP_KERNEL); | |
94b5d262 AV |
168 | if (unlikely(!result)) { |
169 | __putname(kname); | |
170 | return ERR_PTR(-ENOMEM); | |
7950e385 JL |
171 | } |
172 | result->name = kname; | |
94b5d262 AV |
173 | len = strncpy_from_user(kname, filename, PATH_MAX); |
174 | if (unlikely(len < 0)) { | |
175 | __putname(kname); | |
176 | kfree(result); | |
177 | return ERR_PTR(len); | |
178 | } | |
179 | if (unlikely(len == PATH_MAX)) { | |
180 | __putname(kname); | |
181 | kfree(result); | |
182 | return ERR_PTR(-ENAMETOOLONG); | |
183 | } | |
7950e385 JL |
184 | } |
185 | ||
94b5d262 | 186 | result->refcnt = 1; |
3f9f0aa6 LT |
187 | /* The empty path is special. */ |
188 | if (unlikely(!len)) { | |
189 | if (empty) | |
4043cde8 | 190 | *empty = 1; |
94b5d262 AV |
191 | if (!(flags & LOOKUP_EMPTY)) { |
192 | putname(result); | |
193 | return ERR_PTR(-ENOENT); | |
194 | } | |
1da177e4 | 195 | } |
3f9f0aa6 | 196 | |
7950e385 | 197 | result->uptr = filename; |
c4ad8f98 | 198 | result->aname = NULL; |
7950e385 JL |
199 | audit_getname(result); |
200 | return result; | |
1da177e4 LT |
201 | } |
202 | ||
91a27b2a JL |
203 | struct filename * |
204 | getname(const char __user * filename) | |
f52e0c11 | 205 | { |
f7493e5d | 206 | return getname_flags(filename, 0, NULL); |
f52e0c11 AV |
207 | } |
208 | ||
c4ad8f98 LT |
209 | struct filename * |
210 | getname_kernel(const char * filename) | |
211 | { | |
212 | struct filename *result; | |
08518549 | 213 | int len = strlen(filename) + 1; |
c4ad8f98 LT |
214 | |
215 | result = __getname(); | |
216 | if (unlikely(!result)) | |
217 | return ERR_PTR(-ENOMEM); | |
218 | ||
08518549 | 219 | if (len <= EMBEDDED_NAME_MAX) { |
fd2f7cb5 | 220 | result->name = (char *)result->iname; |
08518549 PM |
221 | } else if (len <= PATH_MAX) { |
222 | struct filename *tmp; | |
223 | ||
224 | tmp = kmalloc(sizeof(*tmp), GFP_KERNEL); | |
225 | if (unlikely(!tmp)) { | |
226 | __putname(result); | |
227 | return ERR_PTR(-ENOMEM); | |
228 | } | |
229 | tmp->name = (char *)result; | |
08518549 PM |
230 | result = tmp; |
231 | } else { | |
232 | __putname(result); | |
233 | return ERR_PTR(-ENAMETOOLONG); | |
234 | } | |
235 | memcpy((char *)result->name, filename, len); | |
c4ad8f98 LT |
236 | result->uptr = NULL; |
237 | result->aname = NULL; | |
55422d0b | 238 | result->refcnt = 1; |
fd3522fd | 239 | audit_getname(result); |
c4ad8f98 | 240 | |
c4ad8f98 LT |
241 | return result; |
242 | } | |
243 | ||
91a27b2a | 244 | void putname(struct filename *name) |
1da177e4 | 245 | { |
55422d0b PM |
246 | BUG_ON(name->refcnt <= 0); |
247 | ||
248 | if (--name->refcnt > 0) | |
249 | return; | |
250 | ||
fd2f7cb5 | 251 | if (name->name != name->iname) { |
55422d0b PM |
252 | __putname(name->name); |
253 | kfree(name); | |
254 | } else | |
255 | __putname(name); | |
1da177e4 | 256 | } |
1da177e4 | 257 | |
e77819e5 LT |
258 | static int check_acl(struct inode *inode, int mask) |
259 | { | |
84635d68 | 260 | #ifdef CONFIG_FS_POSIX_ACL |
e77819e5 LT |
261 | struct posix_acl *acl; |
262 | ||
e77819e5 | 263 | if (mask & MAY_NOT_BLOCK) { |
3567866b AV |
264 | acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); |
265 | if (!acl) | |
e77819e5 | 266 | return -EAGAIN; |
3567866b AV |
267 | /* no ->get_acl() calls in RCU mode... */ |
268 | if (acl == ACL_NOT_CACHED) | |
269 | return -ECHILD; | |
206b1d09 | 270 | return posix_acl_permission(inode, acl, mask & ~MAY_NOT_BLOCK); |
e77819e5 LT |
271 | } |
272 | ||
2982baa2 CH |
273 | acl = get_acl(inode, ACL_TYPE_ACCESS); |
274 | if (IS_ERR(acl)) | |
275 | return PTR_ERR(acl); | |
e77819e5 LT |
276 | if (acl) { |
277 | int error = posix_acl_permission(inode, acl, mask); | |
278 | posix_acl_release(acl); | |
279 | return error; | |
280 | } | |
84635d68 | 281 | #endif |
e77819e5 LT |
282 | |
283 | return -EAGAIN; | |
284 | } | |
285 | ||
5909ccaa | 286 | /* |
948409c7 | 287 | * This does the basic permission checking |
1da177e4 | 288 | */ |
7e40145e | 289 | static int acl_permission_check(struct inode *inode, int mask) |
1da177e4 | 290 | { |
26cf46be | 291 | unsigned int mode = inode->i_mode; |
1da177e4 | 292 | |
8e96e3b7 | 293 | if (likely(uid_eq(current_fsuid(), inode->i_uid))) |
1da177e4 LT |
294 | mode >>= 6; |
295 | else { | |
e77819e5 | 296 | if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { |
7e40145e | 297 | int error = check_acl(inode, mask); |
b74c79e9 NP |
298 | if (error != -EAGAIN) |
299 | return error; | |
1da177e4 LT |
300 | } |
301 | ||
302 | if (in_group_p(inode->i_gid)) | |
303 | mode >>= 3; | |
304 | } | |
305 | ||
306 | /* | |
307 | * If the DACs are ok we don't need any capability check. | |
308 | */ | |
9c2c7039 | 309 | if ((mask & ~mode & (MAY_READ | MAY_WRITE | MAY_EXEC)) == 0) |
1da177e4 | 310 | return 0; |
5909ccaa LT |
311 | return -EACCES; |
312 | } | |
313 | ||
314 | /** | |
b74c79e9 | 315 | * generic_permission - check for access rights on a Posix-like filesystem |
5909ccaa | 316 | * @inode: inode to check access rights for |
8fd90c8d | 317 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, ...) |
5909ccaa LT |
318 | * |
319 | * Used to check for read/write/execute permissions on a file. | |
320 | * We use "fsuid" for this, letting us set arbitrary permissions | |
321 | * for filesystem access without changing the "normal" uids which | |
b74c79e9 NP |
322 | * are used for other things. |
323 | * | |
324 | * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk | |
325 | * request cannot be satisfied (eg. requires blocking or too much complexity). | |
326 | * It would then be called again in ref-walk mode. | |
5909ccaa | 327 | */ |
2830ba7f | 328 | int generic_permission(struct inode *inode, int mask) |
5909ccaa LT |
329 | { |
330 | int ret; | |
331 | ||
332 | /* | |
948409c7 | 333 | * Do the basic permission checks. |
5909ccaa | 334 | */ |
7e40145e | 335 | ret = acl_permission_check(inode, mask); |
5909ccaa LT |
336 | if (ret != -EACCES) |
337 | return ret; | |
1da177e4 | 338 | |
d594e7ec AV |
339 | if (S_ISDIR(inode->i_mode)) { |
340 | /* DACs are overridable for directories */ | |
23adbe12 | 341 | if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE)) |
d594e7ec AV |
342 | return 0; |
343 | if (!(mask & MAY_WRITE)) | |
23adbe12 AL |
344 | if (capable_wrt_inode_uidgid(inode, |
345 | CAP_DAC_READ_SEARCH)) | |
d594e7ec AV |
346 | return 0; |
347 | return -EACCES; | |
348 | } | |
1da177e4 LT |
349 | /* |
350 | * Read/write DACs are always overridable. | |
d594e7ec AV |
351 | * Executable DACs are overridable when there is |
352 | * at least one exec bit set. | |
1da177e4 | 353 | */ |
d594e7ec | 354 | if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) |
23adbe12 | 355 | if (capable_wrt_inode_uidgid(inode, CAP_DAC_OVERRIDE)) |
1da177e4 LT |
356 | return 0; |
357 | ||
358 | /* | |
359 | * Searching includes executable on directories, else just read. | |
360 | */ | |
7ea66001 | 361 | mask &= MAY_READ | MAY_WRITE | MAY_EXEC; |
d594e7ec | 362 | if (mask == MAY_READ) |
23adbe12 | 363 | if (capable_wrt_inode_uidgid(inode, CAP_DAC_READ_SEARCH)) |
1da177e4 LT |
364 | return 0; |
365 | ||
366 | return -EACCES; | |
367 | } | |
4d359507 | 368 | EXPORT_SYMBOL(generic_permission); |
1da177e4 | 369 | |
3ddcd056 LT |
370 | /* |
371 | * We _really_ want to just do "generic_permission()" without | |
372 | * even looking at the inode->i_op values. So we keep a cache | |
373 | * flag in inode->i_opflags, that says "this has not special | |
374 | * permission function, use the fast case". | |
375 | */ | |
376 | static inline int do_inode_permission(struct inode *inode, int mask) | |
377 | { | |
378 | if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { | |
379 | if (likely(inode->i_op->permission)) | |
380 | return inode->i_op->permission(inode, mask); | |
381 | ||
382 | /* This gets set once for the inode lifetime */ | |
383 | spin_lock(&inode->i_lock); | |
384 | inode->i_opflags |= IOP_FASTPERM; | |
385 | spin_unlock(&inode->i_lock); | |
386 | } | |
387 | return generic_permission(inode, mask); | |
388 | } | |
389 | ||
cb23beb5 | 390 | /** |
0bdaea90 DH |
391 | * __inode_permission - Check for access rights to a given inode |
392 | * @inode: Inode to check permission on | |
393 | * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) | |
cb23beb5 | 394 | * |
0bdaea90 | 395 | * Check for read/write/execute permissions on an inode. |
948409c7 AG |
396 | * |
397 | * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. | |
0bdaea90 DH |
398 | * |
399 | * This does not check for a read-only file system. You probably want | |
400 | * inode_permission(). | |
cb23beb5 | 401 | */ |
0bdaea90 | 402 | int __inode_permission(struct inode *inode, int mask) |
1da177e4 | 403 | { |
e6305c43 | 404 | int retval; |
1da177e4 | 405 | |
3ddcd056 | 406 | if (unlikely(mask & MAY_WRITE)) { |
1da177e4 LT |
407 | /* |
408 | * Nobody gets write access to an immutable file. | |
409 | */ | |
410 | if (IS_IMMUTABLE(inode)) | |
411 | return -EACCES; | |
412 | } | |
413 | ||
3ddcd056 | 414 | retval = do_inode_permission(inode, mask); |
1da177e4 LT |
415 | if (retval) |
416 | return retval; | |
417 | ||
08ce5f16 SH |
418 | retval = devcgroup_inode_permission(inode, mask); |
419 | if (retval) | |
420 | return retval; | |
421 | ||
d09ca739 | 422 | return security_inode_permission(inode, mask); |
1da177e4 | 423 | } |
bd5d0856 | 424 | EXPORT_SYMBOL(__inode_permission); |
1da177e4 | 425 | |
0bdaea90 DH |
426 | /** |
427 | * sb_permission - Check superblock-level permissions | |
428 | * @sb: Superblock of inode to check permission on | |
55852635 | 429 | * @inode: Inode to check permission on |
0bdaea90 DH |
430 | * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) |
431 | * | |
432 | * Separate out file-system wide checks from inode-specific permission checks. | |
433 | */ | |
434 | static int sb_permission(struct super_block *sb, struct inode *inode, int mask) | |
435 | { | |
436 | if (unlikely(mask & MAY_WRITE)) { | |
437 | umode_t mode = inode->i_mode; | |
438 | ||
439 | /* Nobody gets write access to a read-only fs. */ | |
440 | if ((sb->s_flags & MS_RDONLY) && | |
441 | (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) | |
442 | return -EROFS; | |
443 | } | |
444 | return 0; | |
445 | } | |
446 | ||
447 | /** | |
448 | * inode_permission - Check for access rights to a given inode | |
449 | * @inode: Inode to check permission on | |
450 | * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) | |
451 | * | |
452 | * Check for read/write/execute permissions on an inode. We use fs[ug]id for | |
453 | * this, letting us set arbitrary permissions for filesystem access without | |
454 | * changing the "normal" UIDs which are used for other things. | |
455 | * | |
456 | * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. | |
457 | */ | |
458 | int inode_permission(struct inode *inode, int mask) | |
459 | { | |
460 | int retval; | |
461 | ||
462 | retval = sb_permission(inode->i_sb, inode, mask); | |
463 | if (retval) | |
464 | return retval; | |
465 | return __inode_permission(inode, mask); | |
466 | } | |
4d359507 | 467 | EXPORT_SYMBOL(inode_permission); |
0bdaea90 | 468 | |
5dd784d0 JB |
469 | /** |
470 | * path_get - get a reference to a path | |
471 | * @path: path to get the reference to | |
472 | * | |
473 | * Given a path increment the reference count to the dentry and the vfsmount. | |
474 | */ | |
dcf787f3 | 475 | void path_get(const struct path *path) |
5dd784d0 JB |
476 | { |
477 | mntget(path->mnt); | |
478 | dget(path->dentry); | |
479 | } | |
480 | EXPORT_SYMBOL(path_get); | |
481 | ||
1d957f9b JB |
482 | /** |
483 | * path_put - put a reference to a path | |
484 | * @path: path to put the reference to | |
485 | * | |
486 | * Given a path decrement the reference count to the dentry and the vfsmount. | |
487 | */ | |
dcf787f3 | 488 | void path_put(const struct path *path) |
1da177e4 | 489 | { |
1d957f9b JB |
490 | dput(path->dentry); |
491 | mntput(path->mnt); | |
1da177e4 | 492 | } |
1d957f9b | 493 | EXPORT_SYMBOL(path_put); |
1da177e4 | 494 | |
894bc8c4 | 495 | #define EMBEDDED_LEVELS 2 |
1f55a6ec AV |
496 | struct nameidata { |
497 | struct path path; | |
1cf2665b | 498 | struct qstr last; |
1f55a6ec AV |
499 | struct path root; |
500 | struct inode *inode; /* path.dentry.d_inode */ | |
501 | unsigned int flags; | |
9883d185 | 502 | unsigned seq, m_seq; |
1f55a6ec AV |
503 | int last_type; |
504 | unsigned depth; | |
756daf26 | 505 | int total_link_count; |
697fc6ca AV |
506 | struct saved { |
507 | struct path link; | |
508 | void *cookie; | |
509 | const char *name; | |
237d8b32 | 510 | struct inode *inode; |
0450b2d1 | 511 | unsigned seq; |
894bc8c4 | 512 | } *stack, internal[EMBEDDED_LEVELS]; |
9883d185 AV |
513 | struct filename *name; |
514 | struct nameidata *saved; | |
515 | unsigned root_seq; | |
516 | int dfd; | |
1f55a6ec AV |
517 | }; |
518 | ||
9883d185 | 519 | static void set_nameidata(struct nameidata *p, int dfd, struct filename *name) |
894bc8c4 | 520 | { |
756daf26 N |
521 | struct nameidata *old = current->nameidata; |
522 | p->stack = p->internal; | |
c8a53ee5 AV |
523 | p->dfd = dfd; |
524 | p->name = name; | |
756daf26 | 525 | p->total_link_count = old ? old->total_link_count : 0; |
9883d185 | 526 | p->saved = old; |
756daf26 | 527 | current->nameidata = p; |
894bc8c4 AV |
528 | } |
529 | ||
9883d185 | 530 | static void restore_nameidata(void) |
894bc8c4 | 531 | { |
9883d185 | 532 | struct nameidata *now = current->nameidata, *old = now->saved; |
756daf26 N |
533 | |
534 | current->nameidata = old; | |
535 | if (old) | |
536 | old->total_link_count = now->total_link_count; | |
537 | if (now->stack != now->internal) { | |
538 | kfree(now->stack); | |
539 | now->stack = now->internal; | |
894bc8c4 AV |
540 | } |
541 | } | |
542 | ||
543 | static int __nd_alloc_stack(struct nameidata *nd) | |
544 | { | |
bc40aee0 AV |
545 | struct saved *p; |
546 | ||
547 | if (nd->flags & LOOKUP_RCU) { | |
548 | p= kmalloc(MAXSYMLINKS * sizeof(struct saved), | |
549 | GFP_ATOMIC); | |
550 | if (unlikely(!p)) | |
551 | return -ECHILD; | |
552 | } else { | |
553 | p= kmalloc(MAXSYMLINKS * sizeof(struct saved), | |
894bc8c4 | 554 | GFP_KERNEL); |
bc40aee0 AV |
555 | if (unlikely(!p)) |
556 | return -ENOMEM; | |
557 | } | |
894bc8c4 AV |
558 | memcpy(p, nd->internal, sizeof(nd->internal)); |
559 | nd->stack = p; | |
560 | return 0; | |
561 | } | |
562 | ||
397d425d EB |
563 | /** |
564 | * path_connected - Verify that a path->dentry is below path->mnt.mnt_root | |
565 | * @path: nameidate to verify | |
566 | * | |
567 | * Rename can sometimes move a file or directory outside of a bind | |
568 | * mount, path_connected allows those cases to be detected. | |
569 | */ | |
570 | static bool path_connected(const struct path *path) | |
571 | { | |
572 | struct vfsmount *mnt = path->mnt; | |
573 | ||
574 | /* Only bind mounts can have disconnected paths */ | |
575 | if (mnt->mnt_root == mnt->mnt_sb->s_root) | |
576 | return true; | |
577 | ||
578 | return is_subdir(path->dentry, mnt->mnt_root); | |
579 | } | |
580 | ||
894bc8c4 AV |
581 | static inline int nd_alloc_stack(struct nameidata *nd) |
582 | { | |
da4e0be0 | 583 | if (likely(nd->depth != EMBEDDED_LEVELS)) |
894bc8c4 AV |
584 | return 0; |
585 | if (likely(nd->stack != nd->internal)) | |
586 | return 0; | |
587 | return __nd_alloc_stack(nd); | |
588 | } | |
589 | ||
7973387a AV |
590 | static void drop_links(struct nameidata *nd) |
591 | { | |
592 | int i = nd->depth; | |
593 | while (i--) { | |
594 | struct saved *last = nd->stack + i; | |
595 | struct inode *inode = last->inode; | |
596 | if (last->cookie && inode->i_op->put_link) { | |
597 | inode->i_op->put_link(inode, last->cookie); | |
598 | last->cookie = NULL; | |
599 | } | |
600 | } | |
601 | } | |
602 | ||
603 | static void terminate_walk(struct nameidata *nd) | |
604 | { | |
605 | drop_links(nd); | |
606 | if (!(nd->flags & LOOKUP_RCU)) { | |
607 | int i; | |
608 | path_put(&nd->path); | |
609 | for (i = 0; i < nd->depth; i++) | |
610 | path_put(&nd->stack[i].link); | |
102b8af2 AV |
611 | if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { |
612 | path_put(&nd->root); | |
613 | nd->root.mnt = NULL; | |
614 | } | |
7973387a AV |
615 | } else { |
616 | nd->flags &= ~LOOKUP_RCU; | |
617 | if (!(nd->flags & LOOKUP_ROOT)) | |
618 | nd->root.mnt = NULL; | |
619 | rcu_read_unlock(); | |
620 | } | |
621 | nd->depth = 0; | |
622 | } | |
623 | ||
624 | /* path_put is needed afterwards regardless of success or failure */ | |
625 | static bool legitimize_path(struct nameidata *nd, | |
626 | struct path *path, unsigned seq) | |
627 | { | |
628 | int res = __legitimize_mnt(path->mnt, nd->m_seq); | |
629 | if (unlikely(res)) { | |
630 | if (res > 0) | |
631 | path->mnt = NULL; | |
632 | path->dentry = NULL; | |
633 | return false; | |
634 | } | |
635 | if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) { | |
636 | path->dentry = NULL; | |
637 | return false; | |
638 | } | |
639 | return !read_seqcount_retry(&path->dentry->d_seq, seq); | |
640 | } | |
641 | ||
642 | static bool legitimize_links(struct nameidata *nd) | |
643 | { | |
644 | int i; | |
645 | for (i = 0; i < nd->depth; i++) { | |
646 | struct saved *last = nd->stack + i; | |
647 | if (unlikely(!legitimize_path(nd, &last->link, last->seq))) { | |
648 | drop_links(nd); | |
649 | nd->depth = i + 1; | |
650 | return false; | |
651 | } | |
652 | } | |
653 | return true; | |
654 | } | |
655 | ||
19660af7 | 656 | /* |
31e6b01f | 657 | * Path walking has 2 modes, rcu-walk and ref-walk (see |
19660af7 AV |
658 | * Documentation/filesystems/path-lookup.txt). In situations when we can't |
659 | * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab | |
57e3715c | 660 | * normal reference counts on dentries and vfsmounts to transition to ref-walk |
19660af7 AV |
661 | * mode. Refcounts are grabbed at the last known good point before rcu-walk |
662 | * got stuck, so ref-walk may continue from there. If this is not successful | |
663 | * (eg. a seqcount has changed), then failure is returned and it's up to caller | |
664 | * to restart the path walk from the beginning in ref-walk mode. | |
31e6b01f | 665 | */ |
31e6b01f NP |
666 | |
667 | /** | |
19660af7 AV |
668 | * unlazy_walk - try to switch to ref-walk mode. |
669 | * @nd: nameidata pathwalk data | |
670 | * @dentry: child of nd->path.dentry or NULL | |
6e9918b7 | 671 | * @seq: seq number to check dentry against |
39191628 | 672 | * Returns: 0 on success, -ECHILD on failure |
31e6b01f | 673 | * |
19660af7 AV |
674 | * unlazy_walk attempts to legitimize the current nd->path, nd->root and dentry |
675 | * for ref-walk mode. @dentry must be a path found by a do_lookup call on | |
676 | * @nd or NULL. Must be called from rcu-walk context. | |
7973387a AV |
677 | * Nothing should touch nameidata between unlazy_walk() failure and |
678 | * terminate_walk(). | |
31e6b01f | 679 | */ |
6e9918b7 | 680 | static int unlazy_walk(struct nameidata *nd, struct dentry *dentry, unsigned seq) |
31e6b01f | 681 | { |
31e6b01f NP |
682 | struct dentry *parent = nd->path.dentry; |
683 | ||
684 | BUG_ON(!(nd->flags & LOOKUP_RCU)); | |
e5c832d5 | 685 | |
e5c832d5 | 686 | nd->flags &= ~LOOKUP_RCU; |
7973387a AV |
687 | if (unlikely(!legitimize_links(nd))) |
688 | goto out2; | |
689 | if (unlikely(!legitimize_mnt(nd->path.mnt, nd->m_seq))) | |
690 | goto out2; | |
691 | if (unlikely(!lockref_get_not_dead(&parent->d_lockref))) | |
692 | goto out1; | |
48a066e7 | 693 | |
15570086 LT |
694 | /* |
695 | * For a negative lookup, the lookup sequence point is the parents | |
696 | * sequence point, and it only needs to revalidate the parent dentry. | |
697 | * | |
698 | * For a positive lookup, we need to move both the parent and the | |
699 | * dentry from the RCU domain to be properly refcounted. And the | |
700 | * sequence number in the dentry validates *both* dentry counters, | |
701 | * since we checked the sequence number of the parent after we got | |
702 | * the child sequence number. So we know the parent must still | |
703 | * be valid if the child sequence number is still valid. | |
704 | */ | |
19660af7 | 705 | if (!dentry) { |
e5c832d5 LT |
706 | if (read_seqcount_retry(&parent->d_seq, nd->seq)) |
707 | goto out; | |
19660af7 AV |
708 | BUG_ON(nd->inode != parent->d_inode); |
709 | } else { | |
e5c832d5 LT |
710 | if (!lockref_get_not_dead(&dentry->d_lockref)) |
711 | goto out; | |
6e9918b7 | 712 | if (read_seqcount_retry(&dentry->d_seq, seq)) |
e5c832d5 | 713 | goto drop_dentry; |
19660af7 | 714 | } |
e5c832d5 LT |
715 | |
716 | /* | |
717 | * Sequence counts matched. Now make sure that the root is | |
718 | * still valid and get it if required. | |
719 | */ | |
720 | if (nd->root.mnt && !(nd->flags & LOOKUP_ROOT)) { | |
5a8d87e8 AV |
721 | if (unlikely(!legitimize_path(nd, &nd->root, nd->root_seq))) { |
722 | rcu_read_unlock(); | |
723 | dput(dentry); | |
724 | return -ECHILD; | |
7973387a | 725 | } |
31e6b01f | 726 | } |
31e6b01f | 727 | |
8b61e74f | 728 | rcu_read_unlock(); |
31e6b01f | 729 | return 0; |
19660af7 | 730 | |
e5c832d5 | 731 | drop_dentry: |
8b61e74f | 732 | rcu_read_unlock(); |
15570086 | 733 | dput(dentry); |
d0d27277 | 734 | goto drop_root_mnt; |
7973387a AV |
735 | out2: |
736 | nd->path.mnt = NULL; | |
737 | out1: | |
738 | nd->path.dentry = NULL; | |
e5c832d5 | 739 | out: |
8b61e74f | 740 | rcu_read_unlock(); |
d0d27277 LT |
741 | drop_root_mnt: |
742 | if (!(nd->flags & LOOKUP_ROOT)) | |
743 | nd->root.mnt = NULL; | |
31e6b01f NP |
744 | return -ECHILD; |
745 | } | |
746 | ||
7973387a AV |
747 | static int unlazy_link(struct nameidata *nd, struct path *link, unsigned seq) |
748 | { | |
749 | if (unlikely(!legitimize_path(nd, link, seq))) { | |
750 | drop_links(nd); | |
751 | nd->depth = 0; | |
752 | nd->flags &= ~LOOKUP_RCU; | |
753 | nd->path.mnt = NULL; | |
754 | nd->path.dentry = NULL; | |
755 | if (!(nd->flags & LOOKUP_ROOT)) | |
756 | nd->root.mnt = NULL; | |
757 | rcu_read_unlock(); | |
758 | } else if (likely(unlazy_walk(nd, NULL, 0)) == 0) { | |
759 | return 0; | |
760 | } | |
761 | path_put(link); | |
762 | return -ECHILD; | |
763 | } | |
764 | ||
4ce16ef3 | 765 | static inline int d_revalidate(struct dentry *dentry, unsigned int flags) |
34286d66 | 766 | { |
4ce16ef3 | 767 | return dentry->d_op->d_revalidate(dentry, flags); |
34286d66 NP |
768 | } |
769 | ||
9f1fafee AV |
770 | /** |
771 | * complete_walk - successful completion of path walk | |
772 | * @nd: pointer nameidata | |
39159de2 | 773 | * |
9f1fafee AV |
774 | * If we had been in RCU mode, drop out of it and legitimize nd->path. |
775 | * Revalidate the final result, unless we'd already done that during | |
776 | * the path walk or the filesystem doesn't ask for it. Return 0 on | |
777 | * success, -error on failure. In case of failure caller does not | |
778 | * need to drop nd->path. | |
39159de2 | 779 | */ |
9f1fafee | 780 | static int complete_walk(struct nameidata *nd) |
39159de2 | 781 | { |
16c2cd71 | 782 | struct dentry *dentry = nd->path.dentry; |
39159de2 | 783 | int status; |
39159de2 | 784 | |
9f1fafee | 785 | if (nd->flags & LOOKUP_RCU) { |
9f1fafee AV |
786 | if (!(nd->flags & LOOKUP_ROOT)) |
787 | nd->root.mnt = NULL; | |
6e9918b7 | 788 | if (unlikely(unlazy_walk(nd, NULL, 0))) |
9f1fafee | 789 | return -ECHILD; |
9f1fafee AV |
790 | } |
791 | ||
16c2cd71 AV |
792 | if (likely(!(nd->flags & LOOKUP_JUMPED))) |
793 | return 0; | |
794 | ||
ecf3d1f1 | 795 | if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) |
39159de2 JL |
796 | return 0; |
797 | ||
ecf3d1f1 | 798 | status = dentry->d_op->d_weak_revalidate(dentry, nd->flags); |
39159de2 JL |
799 | if (status > 0) |
800 | return 0; | |
801 | ||
16c2cd71 | 802 | if (!status) |
39159de2 | 803 | status = -ESTALE; |
16c2cd71 | 804 | |
39159de2 JL |
805 | return status; |
806 | } | |
807 | ||
18d8c860 | 808 | static void set_root(struct nameidata *nd) |
31e6b01f | 809 | { |
7bd88377 | 810 | struct fs_struct *fs = current->fs; |
c28cc364 | 811 | |
9e6697e2 AV |
812 | if (nd->flags & LOOKUP_RCU) { |
813 | unsigned seq; | |
814 | ||
815 | do { | |
816 | seq = read_seqcount_begin(&fs->seq); | |
817 | nd->root = fs->root; | |
818 | nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq); | |
819 | } while (read_seqcount_retry(&fs->seq, seq)); | |
820 | } else { | |
821 | get_fs_root(fs, &nd->root); | |
822 | } | |
31e6b01f NP |
823 | } |
824 | ||
1d957f9b | 825 | static void path_put_conditional(struct path *path, struct nameidata *nd) |
051d3812 IK |
826 | { |
827 | dput(path->dentry); | |
4ac91378 | 828 | if (path->mnt != nd->path.mnt) |
051d3812 IK |
829 | mntput(path->mnt); |
830 | } | |
831 | ||
7b9337aa NP |
832 | static inline void path_to_nameidata(const struct path *path, |
833 | struct nameidata *nd) | |
051d3812 | 834 | { |
31e6b01f NP |
835 | if (!(nd->flags & LOOKUP_RCU)) { |
836 | dput(nd->path.dentry); | |
837 | if (nd->path.mnt != path->mnt) | |
838 | mntput(nd->path.mnt); | |
9a229683 | 839 | } |
31e6b01f | 840 | nd->path.mnt = path->mnt; |
4ac91378 | 841 | nd->path.dentry = path->dentry; |
051d3812 IK |
842 | } |
843 | ||
b5fb63c1 CH |
844 | /* |
845 | * Helper to directly jump to a known parsed path from ->follow_link, | |
846 | * caller must have taken a reference to path beforehand. | |
847 | */ | |
6e77137b | 848 | void nd_jump_link(struct path *path) |
b5fb63c1 | 849 | { |
6e77137b | 850 | struct nameidata *nd = current->nameidata; |
b5fb63c1 CH |
851 | path_put(&nd->path); |
852 | ||
853 | nd->path = *path; | |
854 | nd->inode = nd->path.dentry->d_inode; | |
855 | nd->flags |= LOOKUP_JUMPED; | |
b5fb63c1 CH |
856 | } |
857 | ||
b9ff4429 | 858 | static inline void put_link(struct nameidata *nd) |
574197e0 | 859 | { |
21c3003d | 860 | struct saved *last = nd->stack + --nd->depth; |
237d8b32 | 861 | struct inode *inode = last->inode; |
b9ff4429 | 862 | if (last->cookie && inode->i_op->put_link) |
5f2c4179 | 863 | inode->i_op->put_link(inode, last->cookie); |
6548fae2 AV |
864 | if (!(nd->flags & LOOKUP_RCU)) |
865 | path_put(&last->link); | |
574197e0 AV |
866 | } |
867 | ||
561ec64a LT |
868 | int sysctl_protected_symlinks __read_mostly = 0; |
869 | int sysctl_protected_hardlinks __read_mostly = 0; | |
800179c9 KC |
870 | |
871 | /** | |
872 | * may_follow_link - Check symlink following for unsafe situations | |
55852635 | 873 | * @nd: nameidata pathwalk data |
800179c9 KC |
874 | * |
875 | * In the case of the sysctl_protected_symlinks sysctl being enabled, | |
876 | * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is | |
877 | * in a sticky world-writable directory. This is to protect privileged | |
878 | * processes from failing races against path names that may change out | |
879 | * from under them by way of other users creating malicious symlinks. | |
880 | * It will permit symlinks to be followed only when outside a sticky | |
881 | * world-writable directory, or when the uid of the symlink and follower | |
882 | * match, or when the directory owner matches the symlink's owner. | |
883 | * | |
884 | * Returns 0 if following the symlink is allowed, -ve on error. | |
885 | */ | |
fec2fa24 | 886 | static inline int may_follow_link(struct nameidata *nd) |
800179c9 KC |
887 | { |
888 | const struct inode *inode; | |
889 | const struct inode *parent; | |
890 | ||
891 | if (!sysctl_protected_symlinks) | |
892 | return 0; | |
893 | ||
894 | /* Allowed if owner and follower match. */ | |
237d8b32 | 895 | inode = nd->stack[0].inode; |
81abe27b | 896 | if (uid_eq(current_cred()->fsuid, inode->i_uid)) |
800179c9 KC |
897 | return 0; |
898 | ||
899 | /* Allowed if parent directory not sticky and world-writable. */ | |
aa65fa35 | 900 | parent = nd->inode; |
800179c9 KC |
901 | if ((parent->i_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) |
902 | return 0; | |
903 | ||
904 | /* Allowed if parent directory and link owner match. */ | |
81abe27b | 905 | if (uid_eq(parent->i_uid, inode->i_uid)) |
800179c9 KC |
906 | return 0; |
907 | ||
31956502 AV |
908 | if (nd->flags & LOOKUP_RCU) |
909 | return -ECHILD; | |
910 | ||
1cf2665b | 911 | audit_log_link_denied("follow_link", &nd->stack[0].link); |
800179c9 KC |
912 | return -EACCES; |
913 | } | |
914 | ||
915 | /** | |
916 | * safe_hardlink_source - Check for safe hardlink conditions | |
917 | * @inode: the source inode to hardlink from | |
918 | * | |
919 | * Return false if at least one of the following conditions: | |
920 | * - inode is not a regular file | |
921 | * - inode is setuid | |
922 | * - inode is setgid and group-exec | |
923 | * - access failure for read and write | |
924 | * | |
925 | * Otherwise returns true. | |
926 | */ | |
927 | static bool safe_hardlink_source(struct inode *inode) | |
928 | { | |
929 | umode_t mode = inode->i_mode; | |
930 | ||
931 | /* Special files should not get pinned to the filesystem. */ | |
932 | if (!S_ISREG(mode)) | |
933 | return false; | |
934 | ||
935 | /* Setuid files should not get pinned to the filesystem. */ | |
936 | if (mode & S_ISUID) | |
937 | return false; | |
938 | ||
939 | /* Executable setgid files should not get pinned to the filesystem. */ | |
940 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) | |
941 | return false; | |
942 | ||
943 | /* Hardlinking to unreadable or unwritable sources is dangerous. */ | |
944 | if (inode_permission(inode, MAY_READ | MAY_WRITE)) | |
945 | return false; | |
946 | ||
947 | return true; | |
948 | } | |
949 | ||
950 | /** | |
951 | * may_linkat - Check permissions for creating a hardlink | |
952 | * @link: the source to hardlink from | |
953 | * | |
954 | * Block hardlink when all of: | |
955 | * - sysctl_protected_hardlinks enabled | |
956 | * - fsuid does not match inode | |
957 | * - hardlink source is unsafe (see safe_hardlink_source() above) | |
f2ca3796 | 958 | * - not CAP_FOWNER in a namespace with the inode owner uid mapped |
800179c9 KC |
959 | * |
960 | * Returns 0 if successful, -ve on error. | |
961 | */ | |
962 | static int may_linkat(struct path *link) | |
963 | { | |
800179c9 KC |
964 | struct inode *inode; |
965 | ||
966 | if (!sysctl_protected_hardlinks) | |
967 | return 0; | |
968 | ||
800179c9 KC |
969 | inode = link->dentry->d_inode; |
970 | ||
971 | /* Source inode owner (or CAP_FOWNER) can hardlink all they like, | |
972 | * otherwise, it must be a safe source. | |
973 | */ | |
f2ca3796 | 974 | if (inode_owner_or_capable(inode) || safe_hardlink_source(inode)) |
800179c9 KC |
975 | return 0; |
976 | ||
a51d9eaa | 977 | audit_log_link_denied("linkat", link); |
800179c9 KC |
978 | return -EPERM; |
979 | } | |
980 | ||
3b2e7f75 AV |
981 | static __always_inline |
982 | const char *get_link(struct nameidata *nd) | |
1da177e4 | 983 | { |
ab104923 | 984 | struct saved *last = nd->stack + nd->depth - 1; |
1cf2665b | 985 | struct dentry *dentry = last->link.dentry; |
237d8b32 | 986 | struct inode *inode = last->inode; |
6d7b5aae | 987 | int error; |
0a959df5 | 988 | const char *res; |
1da177e4 | 989 | |
8fa9dd24 N |
990 | if (!(nd->flags & LOOKUP_RCU)) { |
991 | touch_atime(&last->link); | |
992 | cond_resched(); | |
993 | } else if (atime_needs_update(&last->link, inode)) { | |
bc40aee0 AV |
994 | if (unlikely(unlazy_walk(nd, NULL, 0))) |
995 | return ERR_PTR(-ECHILD); | |
8fa9dd24 | 996 | touch_atime(&last->link); |
bc40aee0 | 997 | } |
574197e0 | 998 | |
bda0be7a N |
999 | error = security_inode_follow_link(dentry, inode, |
1000 | nd->flags & LOOKUP_RCU); | |
1001 | if (unlikely(error)) | |
6920a440 | 1002 | return ERR_PTR(error); |
36f3b4f6 | 1003 | |
86acdca1 | 1004 | nd->last_type = LAST_BIND; |
d4dee48b AV |
1005 | res = inode->i_link; |
1006 | if (!res) { | |
8c1b4566 AV |
1007 | if (nd->flags & LOOKUP_RCU) { |
1008 | if (unlikely(unlazy_walk(nd, NULL, 0))) | |
1009 | return ERR_PTR(-ECHILD); | |
1010 | } | |
6e77137b | 1011 | res = inode->i_op->follow_link(dentry, &last->cookie); |
fab51e8a | 1012 | if (IS_ERR_OR_NULL(res)) { |
6920a440 | 1013 | last->cookie = NULL; |
fab51e8a AV |
1014 | return res; |
1015 | } | |
1016 | } | |
1017 | if (*res == '/') { | |
9e6697e2 AV |
1018 | if (!nd->root.mnt) |
1019 | set_root(nd); | |
8c1b4566 | 1020 | if (nd->flags & LOOKUP_RCU) { |
8f47a016 | 1021 | struct dentry *d; |
8f47a016 AV |
1022 | nd->path = nd->root; |
1023 | d = nd->path.dentry; | |
1024 | nd->inode = d->d_inode; | |
1025 | nd->seq = nd->root_seq; | |
1026 | if (unlikely(read_seqcount_retry(&d->d_seq, nd->seq))) | |
8c1b4566 | 1027 | return ERR_PTR(-ECHILD); |
8f47a016 | 1028 | } else { |
8f47a016 AV |
1029 | path_put(&nd->path); |
1030 | nd->path = nd->root; | |
1031 | path_get(&nd->root); | |
1032 | nd->inode = nd->path.dentry->d_inode; | |
8c1b4566 | 1033 | } |
fab51e8a AV |
1034 | nd->flags |= LOOKUP_JUMPED; |
1035 | while (unlikely(*++res == '/')) | |
1036 | ; | |
1da177e4 | 1037 | } |
fab51e8a AV |
1038 | if (!*res) |
1039 | res = NULL; | |
0a959df5 AV |
1040 | return res; |
1041 | } | |
6d7b5aae | 1042 | |
f015f126 DH |
1043 | /* |
1044 | * follow_up - Find the mountpoint of path's vfsmount | |
1045 | * | |
1046 | * Given a path, find the mountpoint of its source file system. | |
1047 | * Replace @path with the path of the mountpoint in the parent mount. | |
1048 | * Up is towards /. | |
1049 | * | |
1050 | * Return 1 if we went up a level and 0 if we were already at the | |
1051 | * root. | |
1052 | */ | |
bab77ebf | 1053 | int follow_up(struct path *path) |
1da177e4 | 1054 | { |
0714a533 AV |
1055 | struct mount *mnt = real_mount(path->mnt); |
1056 | struct mount *parent; | |
1da177e4 | 1057 | struct dentry *mountpoint; |
99b7db7b | 1058 | |
48a066e7 | 1059 | read_seqlock_excl(&mount_lock); |
0714a533 | 1060 | parent = mnt->mnt_parent; |
3c0a6163 | 1061 | if (parent == mnt) { |
48a066e7 | 1062 | read_sequnlock_excl(&mount_lock); |
1da177e4 LT |
1063 | return 0; |
1064 | } | |
0714a533 | 1065 | mntget(&parent->mnt); |
a73324da | 1066 | mountpoint = dget(mnt->mnt_mountpoint); |
48a066e7 | 1067 | read_sequnlock_excl(&mount_lock); |
bab77ebf AV |
1068 | dput(path->dentry); |
1069 | path->dentry = mountpoint; | |
1070 | mntput(path->mnt); | |
0714a533 | 1071 | path->mnt = &parent->mnt; |
1da177e4 LT |
1072 | return 1; |
1073 | } | |
4d359507 | 1074 | EXPORT_SYMBOL(follow_up); |
1da177e4 | 1075 | |
b5c84bf6 | 1076 | /* |
9875cf80 DH |
1077 | * Perform an automount |
1078 | * - return -EISDIR to tell follow_managed() to stop and return the path we | |
1079 | * were called with. | |
1da177e4 | 1080 | */ |
756daf26 | 1081 | static int follow_automount(struct path *path, struct nameidata *nd, |
9875cf80 | 1082 | bool *need_mntput) |
31e6b01f | 1083 | { |
9875cf80 | 1084 | struct vfsmount *mnt; |
ea5b778a | 1085 | int err; |
9875cf80 DH |
1086 | |
1087 | if (!path->dentry->d_op || !path->dentry->d_op->d_automount) | |
1088 | return -EREMOTE; | |
1089 | ||
0ec26fd0 MS |
1090 | /* We don't want to mount if someone's just doing a stat - |
1091 | * unless they're stat'ing a directory and appended a '/' to | |
1092 | * the name. | |
1093 | * | |
1094 | * We do, however, want to mount if someone wants to open or | |
1095 | * create a file of any type under the mountpoint, wants to | |
1096 | * traverse through the mountpoint or wants to open the | |
1097 | * mounted directory. Also, autofs may mark negative dentries | |
1098 | * as being automount points. These will need the attentions | |
1099 | * of the daemon to instantiate them before they can be used. | |
9875cf80 | 1100 | */ |
756daf26 N |
1101 | if (!(nd->flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | |
1102 | LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && | |
0ec26fd0 MS |
1103 | path->dentry->d_inode) |
1104 | return -EISDIR; | |
1105 | ||
756daf26 N |
1106 | nd->total_link_count++; |
1107 | if (nd->total_link_count >= 40) | |
9875cf80 DH |
1108 | return -ELOOP; |
1109 | ||
1110 | mnt = path->dentry->d_op->d_automount(path); | |
1111 | if (IS_ERR(mnt)) { | |
1112 | /* | |
1113 | * The filesystem is allowed to return -EISDIR here to indicate | |
1114 | * it doesn't want to automount. For instance, autofs would do | |
1115 | * this so that its userspace daemon can mount on this dentry. | |
1116 | * | |
1117 | * However, we can only permit this if it's a terminal point in | |
1118 | * the path being looked up; if it wasn't then the remainder of | |
1119 | * the path is inaccessible and we should say so. | |
1120 | */ | |
756daf26 | 1121 | if (PTR_ERR(mnt) == -EISDIR && (nd->flags & LOOKUP_PARENT)) |
9875cf80 DH |
1122 | return -EREMOTE; |
1123 | return PTR_ERR(mnt); | |
31e6b01f | 1124 | } |
ea5b778a | 1125 | |
9875cf80 DH |
1126 | if (!mnt) /* mount collision */ |
1127 | return 0; | |
31e6b01f | 1128 | |
8aef1884 AV |
1129 | if (!*need_mntput) { |
1130 | /* lock_mount() may release path->mnt on error */ | |
1131 | mntget(path->mnt); | |
1132 | *need_mntput = true; | |
1133 | } | |
19a167af | 1134 | err = finish_automount(mnt, path); |
9875cf80 | 1135 | |
ea5b778a DH |
1136 | switch (err) { |
1137 | case -EBUSY: | |
1138 | /* Someone else made a mount here whilst we were busy */ | |
19a167af | 1139 | return 0; |
ea5b778a | 1140 | case 0: |
8aef1884 | 1141 | path_put(path); |
ea5b778a DH |
1142 | path->mnt = mnt; |
1143 | path->dentry = dget(mnt->mnt_root); | |
ea5b778a | 1144 | return 0; |
19a167af AV |
1145 | default: |
1146 | return err; | |
ea5b778a | 1147 | } |
19a167af | 1148 | |
463ffb2e AV |
1149 | } |
1150 | ||
9875cf80 DH |
1151 | /* |
1152 | * Handle a dentry that is managed in some way. | |
cc53ce53 | 1153 | * - Flagged for transit management (autofs) |
9875cf80 DH |
1154 | * - Flagged as mountpoint |
1155 | * - Flagged as automount point | |
1156 | * | |
1157 | * This may only be called in refwalk mode. | |
1158 | * | |
1159 | * Serialization is taken care of in namespace.c | |
1160 | */ | |
756daf26 | 1161 | static int follow_managed(struct path *path, struct nameidata *nd) |
1da177e4 | 1162 | { |
8aef1884 | 1163 | struct vfsmount *mnt = path->mnt; /* held by caller, must be left alone */ |
9875cf80 DH |
1164 | unsigned managed; |
1165 | bool need_mntput = false; | |
8aef1884 | 1166 | int ret = 0; |
9875cf80 DH |
1167 | |
1168 | /* Given that we're not holding a lock here, we retain the value in a | |
1169 | * local variable for each dentry as we look at it so that we don't see | |
1170 | * the components of that value change under us */ | |
1171 | while (managed = ACCESS_ONCE(path->dentry->d_flags), | |
1172 | managed &= DCACHE_MANAGED_DENTRY, | |
1173 | unlikely(managed != 0)) { | |
cc53ce53 DH |
1174 | /* Allow the filesystem to manage the transit without i_mutex |
1175 | * being held. */ | |
1176 | if (managed & DCACHE_MANAGE_TRANSIT) { | |
1177 | BUG_ON(!path->dentry->d_op); | |
1178 | BUG_ON(!path->dentry->d_op->d_manage); | |
1aed3e42 | 1179 | ret = path->dentry->d_op->d_manage(path->dentry, false); |
cc53ce53 | 1180 | if (ret < 0) |
8aef1884 | 1181 | break; |
cc53ce53 DH |
1182 | } |
1183 | ||
9875cf80 DH |
1184 | /* Transit to a mounted filesystem. */ |
1185 | if (managed & DCACHE_MOUNTED) { | |
1186 | struct vfsmount *mounted = lookup_mnt(path); | |
1187 | if (mounted) { | |
1188 | dput(path->dentry); | |
1189 | if (need_mntput) | |
1190 | mntput(path->mnt); | |
1191 | path->mnt = mounted; | |
1192 | path->dentry = dget(mounted->mnt_root); | |
1193 | need_mntput = true; | |
1194 | continue; | |
1195 | } | |
1196 | ||
1197 | /* Something is mounted on this dentry in another | |
1198 | * namespace and/or whatever was mounted there in this | |
48a066e7 AV |
1199 | * namespace got unmounted before lookup_mnt() could |
1200 | * get it */ | |
9875cf80 DH |
1201 | } |
1202 | ||
1203 | /* Handle an automount point */ | |
1204 | if (managed & DCACHE_NEED_AUTOMOUNT) { | |
756daf26 | 1205 | ret = follow_automount(path, nd, &need_mntput); |
9875cf80 | 1206 | if (ret < 0) |
8aef1884 | 1207 | break; |
9875cf80 DH |
1208 | continue; |
1209 | } | |
1210 | ||
1211 | /* We didn't change the current path point */ | |
1212 | break; | |
1da177e4 | 1213 | } |
8aef1884 AV |
1214 | |
1215 | if (need_mntput && path->mnt == mnt) | |
1216 | mntput(path->mnt); | |
1217 | if (ret == -EISDIR) | |
1218 | ret = 0; | |
8402752e AV |
1219 | if (need_mntput) |
1220 | nd->flags |= LOOKUP_JUMPED; | |
1221 | if (unlikely(ret < 0)) | |
1222 | path_put_conditional(path, nd); | |
1223 | return ret; | |
1da177e4 LT |
1224 | } |
1225 | ||
cc53ce53 | 1226 | int follow_down_one(struct path *path) |
1da177e4 LT |
1227 | { |
1228 | struct vfsmount *mounted; | |
1229 | ||
1c755af4 | 1230 | mounted = lookup_mnt(path); |
1da177e4 | 1231 | if (mounted) { |
9393bd07 AV |
1232 | dput(path->dentry); |
1233 | mntput(path->mnt); | |
1234 | path->mnt = mounted; | |
1235 | path->dentry = dget(mounted->mnt_root); | |
1da177e4 LT |
1236 | return 1; |
1237 | } | |
1238 | return 0; | |
1239 | } | |
4d359507 | 1240 | EXPORT_SYMBOL(follow_down_one); |
1da177e4 | 1241 | |
b8faf035 | 1242 | static inline int managed_dentry_rcu(struct dentry *dentry) |
62a7375e | 1243 | { |
b8faf035 N |
1244 | return (dentry->d_flags & DCACHE_MANAGE_TRANSIT) ? |
1245 | dentry->d_op->d_manage(dentry, true) : 0; | |
62a7375e IK |
1246 | } |
1247 | ||
9875cf80 | 1248 | /* |
287548e4 AV |
1249 | * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if |
1250 | * we meet a managed dentry that would need blocking. | |
9875cf80 DH |
1251 | */ |
1252 | static bool __follow_mount_rcu(struct nameidata *nd, struct path *path, | |
254cf582 | 1253 | struct inode **inode, unsigned *seqp) |
9875cf80 | 1254 | { |
62a7375e | 1255 | for (;;) { |
c7105365 | 1256 | struct mount *mounted; |
62a7375e IK |
1257 | /* |
1258 | * Don't forget we might have a non-mountpoint managed dentry | |
1259 | * that wants to block transit. | |
1260 | */ | |
b8faf035 N |
1261 | switch (managed_dentry_rcu(path->dentry)) { |
1262 | case -ECHILD: | |
1263 | default: | |
ab90911f | 1264 | return false; |
b8faf035 N |
1265 | case -EISDIR: |
1266 | return true; | |
1267 | case 0: | |
1268 | break; | |
1269 | } | |
62a7375e IK |
1270 | |
1271 | if (!d_mountpoint(path->dentry)) | |
b8faf035 | 1272 | return !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT); |
62a7375e | 1273 | |
474279dc | 1274 | mounted = __lookup_mnt(path->mnt, path->dentry); |
9875cf80 DH |
1275 | if (!mounted) |
1276 | break; | |
c7105365 AV |
1277 | path->mnt = &mounted->mnt; |
1278 | path->dentry = mounted->mnt.mnt_root; | |
a3fbbde7 | 1279 | nd->flags |= LOOKUP_JUMPED; |
254cf582 | 1280 | *seqp = read_seqcount_begin(&path->dentry->d_seq); |
59430262 LT |
1281 | /* |
1282 | * Update the inode too. We don't need to re-check the | |
1283 | * dentry sequence number here after this d_inode read, | |
1284 | * because a mount-point is always pinned. | |
1285 | */ | |
1286 | *inode = path->dentry->d_inode; | |
9875cf80 | 1287 | } |
f5be3e29 | 1288 | return !read_seqretry(&mount_lock, nd->m_seq) && |
b8faf035 | 1289 | !(path->dentry->d_flags & DCACHE_NEED_AUTOMOUNT); |
287548e4 AV |
1290 | } |
1291 | ||
31e6b01f NP |
1292 | static int follow_dotdot_rcu(struct nameidata *nd) |
1293 | { | |
4023bfc9 | 1294 | struct inode *inode = nd->inode; |
31e6b01f | 1295 | |
9875cf80 | 1296 | while (1) { |
aed434ad | 1297 | if (path_equal(&nd->path, &nd->root)) |
31e6b01f | 1298 | break; |
31e6b01f NP |
1299 | if (nd->path.dentry != nd->path.mnt->mnt_root) { |
1300 | struct dentry *old = nd->path.dentry; | |
1301 | struct dentry *parent = old->d_parent; | |
1302 | unsigned seq; | |
1303 | ||
4023bfc9 | 1304 | inode = parent->d_inode; |
31e6b01f | 1305 | seq = read_seqcount_begin(&parent->d_seq); |
aed434ad AV |
1306 | if (unlikely(read_seqcount_retry(&old->d_seq, nd->seq))) |
1307 | return -ECHILD; | |
31e6b01f NP |
1308 | nd->path.dentry = parent; |
1309 | nd->seq = seq; | |
397d425d EB |
1310 | if (unlikely(!path_connected(&nd->path))) |
1311 | return -ENOENT; | |
31e6b01f | 1312 | break; |
aed434ad AV |
1313 | } else { |
1314 | struct mount *mnt = real_mount(nd->path.mnt); | |
1315 | struct mount *mparent = mnt->mnt_parent; | |
1316 | struct dentry *mountpoint = mnt->mnt_mountpoint; | |
1317 | struct inode *inode2 = mountpoint->d_inode; | |
1318 | unsigned seq = read_seqcount_begin(&mountpoint->d_seq); | |
1319 | if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) | |
1320 | return -ECHILD; | |
1321 | if (&mparent->mnt == nd->path.mnt) | |
1322 | break; | |
1323 | /* we know that mountpoint was pinned */ | |
1324 | nd->path.dentry = mountpoint; | |
1325 | nd->path.mnt = &mparent->mnt; | |
1326 | inode = inode2; | |
1327 | nd->seq = seq; | |
31e6b01f | 1328 | } |
31e6b01f | 1329 | } |
aed434ad | 1330 | while (unlikely(d_mountpoint(nd->path.dentry))) { |
b37199e6 AV |
1331 | struct mount *mounted; |
1332 | mounted = __lookup_mnt(nd->path.mnt, nd->path.dentry); | |
aed434ad AV |
1333 | if (unlikely(read_seqretry(&mount_lock, nd->m_seq))) |
1334 | return -ECHILD; | |
b37199e6 AV |
1335 | if (!mounted) |
1336 | break; | |
1337 | nd->path.mnt = &mounted->mnt; | |
1338 | nd->path.dentry = mounted->mnt.mnt_root; | |
4023bfc9 | 1339 | inode = nd->path.dentry->d_inode; |
b37199e6 | 1340 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
b37199e6 | 1341 | } |
4023bfc9 | 1342 | nd->inode = inode; |
31e6b01f NP |
1343 | return 0; |
1344 | } | |
1345 | ||
cc53ce53 DH |
1346 | /* |
1347 | * Follow down to the covering mount currently visible to userspace. At each | |
1348 | * point, the filesystem owning that dentry may be queried as to whether the | |
1349 | * caller is permitted to proceed or not. | |
cc53ce53 | 1350 | */ |
7cc90cc3 | 1351 | int follow_down(struct path *path) |
cc53ce53 DH |
1352 | { |
1353 | unsigned managed; | |
1354 | int ret; | |
1355 | ||
1356 | while (managed = ACCESS_ONCE(path->dentry->d_flags), | |
1357 | unlikely(managed & DCACHE_MANAGED_DENTRY)) { | |
1358 | /* Allow the filesystem to manage the transit without i_mutex | |
1359 | * being held. | |
1360 | * | |
1361 | * We indicate to the filesystem if someone is trying to mount | |
1362 | * something here. This gives autofs the chance to deny anyone | |
1363 | * other than its daemon the right to mount on its | |
1364 | * superstructure. | |
1365 | * | |
1366 | * The filesystem may sleep at this point. | |
1367 | */ | |
1368 | if (managed & DCACHE_MANAGE_TRANSIT) { | |
1369 | BUG_ON(!path->dentry->d_op); | |
1370 | BUG_ON(!path->dentry->d_op->d_manage); | |
ab90911f | 1371 | ret = path->dentry->d_op->d_manage( |
1aed3e42 | 1372 | path->dentry, false); |
cc53ce53 DH |
1373 | if (ret < 0) |
1374 | return ret == -EISDIR ? 0 : ret; | |
1375 | } | |
1376 | ||
1377 | /* Transit to a mounted filesystem. */ | |
1378 | if (managed & DCACHE_MOUNTED) { | |
1379 | struct vfsmount *mounted = lookup_mnt(path); | |
1380 | if (!mounted) | |
1381 | break; | |
1382 | dput(path->dentry); | |
1383 | mntput(path->mnt); | |
1384 | path->mnt = mounted; | |
1385 | path->dentry = dget(mounted->mnt_root); | |
1386 | continue; | |
1387 | } | |
1388 | ||
1389 | /* Don't handle automount points here */ | |
1390 | break; | |
1391 | } | |
1392 | return 0; | |
1393 | } | |
4d359507 | 1394 | EXPORT_SYMBOL(follow_down); |
cc53ce53 | 1395 | |
9875cf80 DH |
1396 | /* |
1397 | * Skip to top of mountpoint pile in refwalk mode for follow_dotdot() | |
1398 | */ | |
1399 | static void follow_mount(struct path *path) | |
1400 | { | |
1401 | while (d_mountpoint(path->dentry)) { | |
1402 | struct vfsmount *mounted = lookup_mnt(path); | |
1403 | if (!mounted) | |
1404 | break; | |
1405 | dput(path->dentry); | |
1406 | mntput(path->mnt); | |
1407 | path->mnt = mounted; | |
1408 | path->dentry = dget(mounted->mnt_root); | |
1409 | } | |
1410 | } | |
1411 | ||
397d425d | 1412 | static int follow_dotdot(struct nameidata *nd) |
1da177e4 LT |
1413 | { |
1414 | while(1) { | |
4ac91378 | 1415 | struct dentry *old = nd->path.dentry; |
1da177e4 | 1416 | |
2a737871 AV |
1417 | if (nd->path.dentry == nd->root.dentry && |
1418 | nd->path.mnt == nd->root.mnt) { | |
1da177e4 LT |
1419 | break; |
1420 | } | |
4ac91378 | 1421 | if (nd->path.dentry != nd->path.mnt->mnt_root) { |
3088dd70 AV |
1422 | /* rare case of legitimate dget_parent()... */ |
1423 | nd->path.dentry = dget_parent(nd->path.dentry); | |
1da177e4 | 1424 | dput(old); |
397d425d EB |
1425 | if (unlikely(!path_connected(&nd->path))) |
1426 | return -ENOENT; | |
1da177e4 LT |
1427 | break; |
1428 | } | |
3088dd70 | 1429 | if (!follow_up(&nd->path)) |
1da177e4 | 1430 | break; |
1da177e4 | 1431 | } |
79ed0226 | 1432 | follow_mount(&nd->path); |
31e6b01f | 1433 | nd->inode = nd->path.dentry->d_inode; |
397d425d | 1434 | return 0; |
1da177e4 LT |
1435 | } |
1436 | ||
baa03890 | 1437 | /* |
bad61189 MS |
1438 | * This looks up the name in dcache, possibly revalidates the old dentry and |
1439 | * allocates a new one if not found or not valid. In the need_lookup argument | |
1440 | * returns whether i_op->lookup is necessary. | |
1441 | * | |
1442 | * dir->d_inode->i_mutex must be held | |
baa03890 | 1443 | */ |
bad61189 | 1444 | static struct dentry *lookup_dcache(struct qstr *name, struct dentry *dir, |
201f956e | 1445 | unsigned int flags, bool *need_lookup) |
baa03890 | 1446 | { |
baa03890 | 1447 | struct dentry *dentry; |
bad61189 | 1448 | int error; |
baa03890 | 1449 | |
bad61189 MS |
1450 | *need_lookup = false; |
1451 | dentry = d_lookup(dir, name); | |
1452 | if (dentry) { | |
39e3c955 | 1453 | if (dentry->d_flags & DCACHE_OP_REVALIDATE) { |
201f956e | 1454 | error = d_revalidate(dentry, flags); |
bad61189 MS |
1455 | if (unlikely(error <= 0)) { |
1456 | if (error < 0) { | |
1457 | dput(dentry); | |
1458 | return ERR_PTR(error); | |
5542aa2f EB |
1459 | } else { |
1460 | d_invalidate(dentry); | |
bad61189 MS |
1461 | dput(dentry); |
1462 | dentry = NULL; | |
1463 | } | |
1464 | } | |
1465 | } | |
1466 | } | |
baa03890 | 1467 | |
bad61189 MS |
1468 | if (!dentry) { |
1469 | dentry = d_alloc(dir, name); | |
1470 | if (unlikely(!dentry)) | |
1471 | return ERR_PTR(-ENOMEM); | |
baa03890 | 1472 | |
bad61189 | 1473 | *need_lookup = true; |
baa03890 NP |
1474 | } |
1475 | return dentry; | |
1476 | } | |
1477 | ||
44396f4b | 1478 | /* |
13a2c3be BF |
1479 | * Call i_op->lookup on the dentry. The dentry must be negative and |
1480 | * unhashed. | |
bad61189 MS |
1481 | * |
1482 | * dir->d_inode->i_mutex must be held | |
44396f4b | 1483 | */ |
bad61189 | 1484 | static struct dentry *lookup_real(struct inode *dir, struct dentry *dentry, |
72bd866a | 1485 | unsigned int flags) |
44396f4b | 1486 | { |
44396f4b JB |
1487 | struct dentry *old; |
1488 | ||
1489 | /* Don't create child dentry for a dead directory. */ | |
bad61189 | 1490 | if (unlikely(IS_DEADDIR(dir))) { |
e188dc02 | 1491 | dput(dentry); |
44396f4b | 1492 | return ERR_PTR(-ENOENT); |
e188dc02 | 1493 | } |
44396f4b | 1494 | |
72bd866a | 1495 | old = dir->i_op->lookup(dir, dentry, flags); |
44396f4b JB |
1496 | if (unlikely(old)) { |
1497 | dput(dentry); | |
1498 | dentry = old; | |
1499 | } | |
1500 | return dentry; | |
1501 | } | |
1502 | ||
a3255546 | 1503 | static struct dentry *__lookup_hash(struct qstr *name, |
72bd866a | 1504 | struct dentry *base, unsigned int flags) |
a3255546 | 1505 | { |
bad61189 | 1506 | bool need_lookup; |
a3255546 AV |
1507 | struct dentry *dentry; |
1508 | ||
72bd866a | 1509 | dentry = lookup_dcache(name, base, flags, &need_lookup); |
bad61189 MS |
1510 | if (!need_lookup) |
1511 | return dentry; | |
a3255546 | 1512 | |
72bd866a | 1513 | return lookup_real(base->d_inode, dentry, flags); |
a3255546 AV |
1514 | } |
1515 | ||
1da177e4 LT |
1516 | /* |
1517 | * It's more convoluted than I'd like it to be, but... it's still fairly | |
1518 | * small and for now I'd prefer to have fast path as straight as possible. | |
1519 | * It _is_ time-critical. | |
1520 | */ | |
e97cdc87 | 1521 | static int lookup_fast(struct nameidata *nd, |
254cf582 AV |
1522 | struct path *path, struct inode **inode, |
1523 | unsigned *seqp) | |
1da177e4 | 1524 | { |
4ac91378 | 1525 | struct vfsmount *mnt = nd->path.mnt; |
31e6b01f | 1526 | struct dentry *dentry, *parent = nd->path.dentry; |
5a18fff2 AV |
1527 | int need_reval = 1; |
1528 | int status = 1; | |
9875cf80 DH |
1529 | int err; |
1530 | ||
b04f784e NP |
1531 | /* |
1532 | * Rename seqlock is not required here because in the off chance | |
1533 | * of a false negative due to a concurrent rename, we're going to | |
1534 | * do the non-racy lookup, below. | |
1535 | */ | |
31e6b01f NP |
1536 | if (nd->flags & LOOKUP_RCU) { |
1537 | unsigned seq; | |
766c4cbf | 1538 | bool negative; |
da53be12 | 1539 | dentry = __d_lookup_rcu(parent, &nd->last, &seq); |
5a18fff2 AV |
1540 | if (!dentry) |
1541 | goto unlazy; | |
1542 | ||
12f8ad4b LT |
1543 | /* |
1544 | * This sequence count validates that the inode matches | |
1545 | * the dentry name information from lookup. | |
1546 | */ | |
63afdfc7 | 1547 | *inode = d_backing_inode(dentry); |
766c4cbf | 1548 | negative = d_is_negative(dentry); |
12f8ad4b LT |
1549 | if (read_seqcount_retry(&dentry->d_seq, seq)) |
1550 | return -ECHILD; | |
1551 | ||
1552 | /* | |
1553 | * This sequence count validates that the parent had no | |
1554 | * changes while we did the lookup of the dentry above. | |
1555 | * | |
1556 | * The memory barrier in read_seqcount_begin of child is | |
1557 | * enough, we can use __read_seqcount_retry here. | |
1558 | */ | |
31e6b01f NP |
1559 | if (__read_seqcount_retry(&parent->d_seq, nd->seq)) |
1560 | return -ECHILD; | |
5a18fff2 | 1561 | |
254cf582 | 1562 | *seqp = seq; |
24643087 | 1563 | if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) { |
4ce16ef3 | 1564 | status = d_revalidate(dentry, nd->flags); |
5a18fff2 AV |
1565 | if (unlikely(status <= 0)) { |
1566 | if (status != -ECHILD) | |
1567 | need_reval = 0; | |
1568 | goto unlazy; | |
1569 | } | |
24643087 | 1570 | } |
daf3761c TM |
1571 | /* |
1572 | * Note: do negative dentry check after revalidation in | |
1573 | * case that drops it. | |
1574 | */ | |
1575 | if (negative) | |
1576 | return -ENOENT; | |
31e6b01f NP |
1577 | path->mnt = mnt; |
1578 | path->dentry = dentry; | |
254cf582 | 1579 | if (likely(__follow_mount_rcu(nd, path, inode, seqp))) |
b8faf035 | 1580 | return 0; |
5a18fff2 | 1581 | unlazy: |
254cf582 | 1582 | if (unlazy_walk(nd, dentry, seq)) |
19660af7 | 1583 | return -ECHILD; |
5a18fff2 | 1584 | } else { |
e97cdc87 | 1585 | dentry = __d_lookup(parent, &nd->last); |
9875cf80 | 1586 | } |
5a18fff2 | 1587 | |
81e6f520 AV |
1588 | if (unlikely(!dentry)) |
1589 | goto need_lookup; | |
1590 | ||
5a18fff2 | 1591 | if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE) && need_reval) |
4ce16ef3 | 1592 | status = d_revalidate(dentry, nd->flags); |
5a18fff2 AV |
1593 | if (unlikely(status <= 0)) { |
1594 | if (status < 0) { | |
1595 | dput(dentry); | |
1596 | return status; | |
1597 | } | |
5542aa2f EB |
1598 | d_invalidate(dentry); |
1599 | dput(dentry); | |
1600 | goto need_lookup; | |
24643087 | 1601 | } |
697f514d | 1602 | |
766c4cbf AV |
1603 | if (unlikely(d_is_negative(dentry))) { |
1604 | dput(dentry); | |
1605 | return -ENOENT; | |
1606 | } | |
9875cf80 DH |
1607 | path->mnt = mnt; |
1608 | path->dentry = dentry; | |
756daf26 | 1609 | err = follow_managed(path, nd); |
8402752e | 1610 | if (likely(!err)) |
63afdfc7 | 1611 | *inode = d_backing_inode(path->dentry); |
8402752e | 1612 | return err; |
81e6f520 AV |
1613 | |
1614 | need_lookup: | |
697f514d MS |
1615 | return 1; |
1616 | } | |
1617 | ||
1618 | /* Fast lookup failed, do it the slow way */ | |
cc2a5271 | 1619 | static int lookup_slow(struct nameidata *nd, struct path *path) |
697f514d MS |
1620 | { |
1621 | struct dentry *dentry, *parent; | |
697f514d MS |
1622 | |
1623 | parent = nd->path.dentry; | |
81e6f520 AV |
1624 | BUG_ON(nd->inode != parent->d_inode); |
1625 | ||
1626 | mutex_lock(&parent->d_inode->i_mutex); | |
cc2a5271 | 1627 | dentry = __lookup_hash(&nd->last, parent, nd->flags); |
81e6f520 AV |
1628 | mutex_unlock(&parent->d_inode->i_mutex); |
1629 | if (IS_ERR(dentry)) | |
1630 | return PTR_ERR(dentry); | |
697f514d MS |
1631 | path->mnt = nd->path.mnt; |
1632 | path->dentry = dentry; | |
8402752e | 1633 | return follow_managed(path, nd); |
1da177e4 LT |
1634 | } |
1635 | ||
52094c8a AV |
1636 | static inline int may_lookup(struct nameidata *nd) |
1637 | { | |
1638 | if (nd->flags & LOOKUP_RCU) { | |
4ad5abb3 | 1639 | int err = inode_permission(nd->inode, MAY_EXEC|MAY_NOT_BLOCK); |
52094c8a AV |
1640 | if (err != -ECHILD) |
1641 | return err; | |
6e9918b7 | 1642 | if (unlazy_walk(nd, NULL, 0)) |
52094c8a AV |
1643 | return -ECHILD; |
1644 | } | |
4ad5abb3 | 1645 | return inode_permission(nd->inode, MAY_EXEC); |
52094c8a AV |
1646 | } |
1647 | ||
9856fa1b AV |
1648 | static inline int handle_dots(struct nameidata *nd, int type) |
1649 | { | |
1650 | if (type == LAST_DOTDOT) { | |
9e6697e2 AV |
1651 | if (!nd->root.mnt) |
1652 | set_root(nd); | |
9856fa1b | 1653 | if (nd->flags & LOOKUP_RCU) { |
70291aec | 1654 | return follow_dotdot_rcu(nd); |
9856fa1b | 1655 | } else |
397d425d | 1656 | return follow_dotdot(nd); |
9856fa1b AV |
1657 | } |
1658 | return 0; | |
1659 | } | |
1660 | ||
181548c0 AV |
1661 | static int pick_link(struct nameidata *nd, struct path *link, |
1662 | struct inode *inode, unsigned seq) | |
d63ff28f | 1663 | { |
626de996 | 1664 | int error; |
1cf2665b | 1665 | struct saved *last; |
756daf26 | 1666 | if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) { |
626de996 AV |
1667 | path_to_nameidata(link, nd); |
1668 | return -ELOOP; | |
1669 | } | |
bc40aee0 | 1670 | if (!(nd->flags & LOOKUP_RCU)) { |
7973387a AV |
1671 | if (link->mnt == nd->path.mnt) |
1672 | mntget(link->mnt); | |
d63ff28f | 1673 | } |
626de996 AV |
1674 | error = nd_alloc_stack(nd); |
1675 | if (unlikely(error)) { | |
bc40aee0 AV |
1676 | if (error == -ECHILD) { |
1677 | if (unlikely(unlazy_link(nd, link, seq))) | |
1678 | return -ECHILD; | |
1679 | error = nd_alloc_stack(nd); | |
1680 | } | |
1681 | if (error) { | |
1682 | path_put(link); | |
1683 | return error; | |
1684 | } | |
626de996 AV |
1685 | } |
1686 | ||
ab104923 | 1687 | last = nd->stack + nd->depth++; |
1cf2665b | 1688 | last->link = *link; |
ab104923 | 1689 | last->cookie = NULL; |
181548c0 | 1690 | last->inode = inode; |
0450b2d1 | 1691 | last->seq = seq; |
d63ff28f AV |
1692 | return 1; |
1693 | } | |
1694 | ||
3ddcd056 LT |
1695 | /* |
1696 | * Do we need to follow links? We _really_ want to be able | |
1697 | * to do this check without having to look at inode->i_op, | |
1698 | * so we keep a cache of "no, this doesn't need follow_link" | |
1699 | * for the common case. | |
1700 | */ | |
254cf582 | 1701 | static inline int should_follow_link(struct nameidata *nd, struct path *link, |
181548c0 AV |
1702 | int follow, |
1703 | struct inode *inode, unsigned seq) | |
3ddcd056 | 1704 | { |
d63ff28f AV |
1705 | if (likely(!d_is_symlink(link->dentry))) |
1706 | return 0; | |
1707 | if (!follow) | |
1708 | return 0; | |
181548c0 | 1709 | return pick_link(nd, link, inode, seq); |
3ddcd056 LT |
1710 | } |
1711 | ||
4693a547 AV |
1712 | enum {WALK_GET = 1, WALK_PUT = 2}; |
1713 | ||
1714 | static int walk_component(struct nameidata *nd, int flags) | |
ce57dfc1 | 1715 | { |
caa85634 | 1716 | struct path path; |
ce57dfc1 | 1717 | struct inode *inode; |
254cf582 | 1718 | unsigned seq; |
ce57dfc1 AV |
1719 | int err; |
1720 | /* | |
1721 | * "." and ".." are special - ".." especially so because it has | |
1722 | * to be able to know about the current root directory and | |
1723 | * parent relationships. | |
1724 | */ | |
4693a547 AV |
1725 | if (unlikely(nd->last_type != LAST_NORM)) { |
1726 | err = handle_dots(nd, nd->last_type); | |
1727 | if (flags & WALK_PUT) | |
1728 | put_link(nd); | |
1729 | return err; | |
1730 | } | |
254cf582 | 1731 | err = lookup_fast(nd, &path, &inode, &seq); |
ce57dfc1 | 1732 | if (unlikely(err)) { |
697f514d | 1733 | if (err < 0) |
f0a9ba70 | 1734 | return err; |
697f514d | 1735 | |
caa85634 | 1736 | err = lookup_slow(nd, &path); |
697f514d | 1737 | if (err < 0) |
f0a9ba70 | 1738 | return err; |
697f514d | 1739 | |
63afdfc7 | 1740 | inode = d_backing_inode(path.dentry); |
254cf582 | 1741 | seq = 0; /* we are already out of RCU mode */ |
766c4cbf | 1742 | err = -ENOENT; |
caa85634 | 1743 | if (d_is_negative(path.dentry)) |
766c4cbf | 1744 | goto out_path_put; |
ce57dfc1 | 1745 | } |
697f514d | 1746 | |
4693a547 AV |
1747 | if (flags & WALK_PUT) |
1748 | put_link(nd); | |
181548c0 | 1749 | err = should_follow_link(nd, &path, flags & WALK_GET, inode, seq); |
d63ff28f AV |
1750 | if (unlikely(err)) |
1751 | return err; | |
caa85634 | 1752 | path_to_nameidata(&path, nd); |
ce57dfc1 | 1753 | nd->inode = inode; |
254cf582 | 1754 | nd->seq = seq; |
ce57dfc1 | 1755 | return 0; |
697f514d MS |
1756 | |
1757 | out_path_put: | |
caa85634 | 1758 | path_to_nameidata(&path, nd); |
697f514d | 1759 | return err; |
ce57dfc1 AV |
1760 | } |
1761 | ||
bfcfaa77 LT |
1762 | /* |
1763 | * We can do the critical dentry name comparison and hashing | |
1764 | * operations one word at a time, but we are limited to: | |
1765 | * | |
1766 | * - Architectures with fast unaligned word accesses. We could | |
1767 | * do a "get_unaligned()" if this helps and is sufficiently | |
1768 | * fast. | |
1769 | * | |
bfcfaa77 LT |
1770 | * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we |
1771 | * do not trap on the (extremely unlikely) case of a page | |
1772 | * crossing operation. | |
1773 | * | |
1774 | * - Furthermore, we need an efficient 64-bit compile for the | |
1775 | * 64-bit case in order to generate the "number of bytes in | |
1776 | * the final mask". Again, that could be replaced with a | |
1777 | * efficient population count instruction or similar. | |
1778 | */ | |
1779 | #ifdef CONFIG_DCACHE_WORD_ACCESS | |
1780 | ||
f68e556e | 1781 | #include <asm/word-at-a-time.h> |
bfcfaa77 | 1782 | |
f68e556e | 1783 | #ifdef CONFIG_64BIT |
bfcfaa77 LT |
1784 | |
1785 | static inline unsigned int fold_hash(unsigned long hash) | |
1786 | { | |
99d263d4 | 1787 | return hash_64(hash, 32); |
bfcfaa77 LT |
1788 | } |
1789 | ||
1790 | #else /* 32-bit case */ | |
1791 | ||
bfcfaa77 LT |
1792 | #define fold_hash(x) (x) |
1793 | ||
1794 | #endif | |
1795 | ||
1796 | unsigned int full_name_hash(const unsigned char *name, unsigned int len) | |
1797 | { | |
1798 | unsigned long a, mask; | |
1799 | unsigned long hash = 0; | |
1800 | ||
1801 | for (;;) { | |
e419b4cc | 1802 | a = load_unaligned_zeropad(name); |
bfcfaa77 LT |
1803 | if (len < sizeof(unsigned long)) |
1804 | break; | |
1805 | hash += a; | |
f132c5be | 1806 | hash *= 9; |
bfcfaa77 LT |
1807 | name += sizeof(unsigned long); |
1808 | len -= sizeof(unsigned long); | |
1809 | if (!len) | |
1810 | goto done; | |
1811 | } | |
a5c21dce | 1812 | mask = bytemask_from_count(len); |
bfcfaa77 LT |
1813 | hash += mask & a; |
1814 | done: | |
1815 | return fold_hash(hash); | |
1816 | } | |
1817 | EXPORT_SYMBOL(full_name_hash); | |
1818 | ||
bfcfaa77 LT |
1819 | /* |
1820 | * Calculate the length and hash of the path component, and | |
d6bb3e90 | 1821 | * return the "hash_len" as the result. |
bfcfaa77 | 1822 | */ |
d6bb3e90 | 1823 | static inline u64 hash_name(const char *name) |
bfcfaa77 | 1824 | { |
36126f8f LT |
1825 | unsigned long a, b, adata, bdata, mask, hash, len; |
1826 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; | |
bfcfaa77 LT |
1827 | |
1828 | hash = a = 0; | |
1829 | len = -sizeof(unsigned long); | |
1830 | do { | |
1831 | hash = (hash + a) * 9; | |
1832 | len += sizeof(unsigned long); | |
e419b4cc | 1833 | a = load_unaligned_zeropad(name+len); |
36126f8f LT |
1834 | b = a ^ REPEAT_BYTE('/'); |
1835 | } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); | |
1836 | ||
1837 | adata = prep_zero_mask(a, adata, &constants); | |
1838 | bdata = prep_zero_mask(b, bdata, &constants); | |
1839 | ||
1840 | mask = create_zero_mask(adata | bdata); | |
1841 | ||
1842 | hash += a & zero_bytemask(mask); | |
9226b5b4 | 1843 | len += find_zero(mask); |
d6bb3e90 | 1844 | return hashlen_create(fold_hash(hash), len); |
bfcfaa77 LT |
1845 | } |
1846 | ||
1847 | #else | |
1848 | ||
0145acc2 LT |
1849 | unsigned int full_name_hash(const unsigned char *name, unsigned int len) |
1850 | { | |
1851 | unsigned long hash = init_name_hash(); | |
1852 | while (len--) | |
1853 | hash = partial_name_hash(*name++, hash); | |
1854 | return end_name_hash(hash); | |
1855 | } | |
ae942ae7 | 1856 | EXPORT_SYMBOL(full_name_hash); |
0145acc2 | 1857 | |
200e9ef7 LT |
1858 | /* |
1859 | * We know there's a real path component here of at least | |
1860 | * one character. | |
1861 | */ | |
d6bb3e90 | 1862 | static inline u64 hash_name(const char *name) |
200e9ef7 LT |
1863 | { |
1864 | unsigned long hash = init_name_hash(); | |
1865 | unsigned long len = 0, c; | |
1866 | ||
1867 | c = (unsigned char)*name; | |
1868 | do { | |
1869 | len++; | |
1870 | hash = partial_name_hash(c, hash); | |
1871 | c = (unsigned char)name[len]; | |
1872 | } while (c && c != '/'); | |
d6bb3e90 | 1873 | return hashlen_create(end_name_hash(hash), len); |
200e9ef7 LT |
1874 | } |
1875 | ||
bfcfaa77 LT |
1876 | #endif |
1877 | ||
1da177e4 LT |
1878 | /* |
1879 | * Name resolution. | |
ea3834d9 PM |
1880 | * This is the basic name resolution function, turning a pathname into |
1881 | * the final dentry. We expect 'base' to be positive and a directory. | |
1da177e4 | 1882 | * |
ea3834d9 PM |
1883 | * Returns 0 and nd will have valid dentry and mnt on success. |
1884 | * Returns error and drops reference to input namei data on failure. | |
1da177e4 | 1885 | */ |
6de88d72 | 1886 | static int link_path_walk(const char *name, struct nameidata *nd) |
1da177e4 | 1887 | { |
1da177e4 | 1888 | int err; |
32cd7468 | 1889 | |
1da177e4 LT |
1890 | while (*name=='/') |
1891 | name++; | |
1892 | if (!*name) | |
9e18f10a | 1893 | return 0; |
1da177e4 | 1894 | |
1da177e4 LT |
1895 | /* At this point we know we have a real path component. */ |
1896 | for(;;) { | |
d6bb3e90 | 1897 | u64 hash_len; |
fe479a58 | 1898 | int type; |
1da177e4 | 1899 | |
52094c8a | 1900 | err = may_lookup(nd); |
1da177e4 | 1901 | if (err) |
3595e234 | 1902 | return err; |
1da177e4 | 1903 | |
d6bb3e90 | 1904 | hash_len = hash_name(name); |
1da177e4 | 1905 | |
fe479a58 | 1906 | type = LAST_NORM; |
d6bb3e90 | 1907 | if (name[0] == '.') switch (hashlen_len(hash_len)) { |
fe479a58 | 1908 | case 2: |
200e9ef7 | 1909 | if (name[1] == '.') { |
fe479a58 | 1910 | type = LAST_DOTDOT; |
16c2cd71 AV |
1911 | nd->flags |= LOOKUP_JUMPED; |
1912 | } | |
fe479a58 AV |
1913 | break; |
1914 | case 1: | |
1915 | type = LAST_DOT; | |
1916 | } | |
5a202bcd AV |
1917 | if (likely(type == LAST_NORM)) { |
1918 | struct dentry *parent = nd->path.dentry; | |
16c2cd71 | 1919 | nd->flags &= ~LOOKUP_JUMPED; |
5a202bcd | 1920 | if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { |
a060dc50 | 1921 | struct qstr this = { { .hash_len = hash_len }, .name = name }; |
da53be12 | 1922 | err = parent->d_op->d_hash(parent, &this); |
5a202bcd | 1923 | if (err < 0) |
3595e234 | 1924 | return err; |
d6bb3e90 LT |
1925 | hash_len = this.hash_len; |
1926 | name = this.name; | |
5a202bcd AV |
1927 | } |
1928 | } | |
fe479a58 | 1929 | |
d6bb3e90 LT |
1930 | nd->last.hash_len = hash_len; |
1931 | nd->last.name = name; | |
5f4a6a69 AV |
1932 | nd->last_type = type; |
1933 | ||
d6bb3e90 LT |
1934 | name += hashlen_len(hash_len); |
1935 | if (!*name) | |
bdf6cbf1 | 1936 | goto OK; |
200e9ef7 LT |
1937 | /* |
1938 | * If it wasn't NUL, we know it was '/'. Skip that | |
1939 | * slash, and continue until no more slashes. | |
1940 | */ | |
1941 | do { | |
d6bb3e90 LT |
1942 | name++; |
1943 | } while (unlikely(*name == '/')); | |
8620c238 AV |
1944 | if (unlikely(!*name)) { |
1945 | OK: | |
368ee9ba | 1946 | /* pathname body, done */ |
8620c238 AV |
1947 | if (!nd->depth) |
1948 | return 0; | |
1949 | name = nd->stack[nd->depth - 1].name; | |
368ee9ba | 1950 | /* trailing symlink, done */ |
8620c238 AV |
1951 | if (!name) |
1952 | return 0; | |
1953 | /* last component of nested symlink */ | |
4693a547 | 1954 | err = walk_component(nd, WALK_GET | WALK_PUT); |
8620c238 | 1955 | } else { |
4693a547 | 1956 | err = walk_component(nd, WALK_GET); |
8620c238 | 1957 | } |
ce57dfc1 | 1958 | if (err < 0) |
3595e234 | 1959 | return err; |
1da177e4 | 1960 | |
ce57dfc1 | 1961 | if (err) { |
626de996 | 1962 | const char *s = get_link(nd); |
5a460275 | 1963 | |
a1c83681 | 1964 | if (IS_ERR(s)) |
3595e234 | 1965 | return PTR_ERR(s); |
d40bcc09 AV |
1966 | err = 0; |
1967 | if (unlikely(!s)) { | |
1968 | /* jumped */ | |
b9ff4429 | 1969 | put_link(nd); |
d40bcc09 | 1970 | } else { |
fab51e8a AV |
1971 | nd->stack[nd->depth - 1].name = name; |
1972 | name = s; | |
1973 | continue; | |
d40bcc09 | 1974 | } |
31e6b01f | 1975 | } |
97242f99 AV |
1976 | if (unlikely(!d_can_lookup(nd->path.dentry))) { |
1977 | if (nd->flags & LOOKUP_RCU) { | |
1978 | if (unlazy_walk(nd, NULL, 0)) | |
1979 | return -ECHILD; | |
1980 | } | |
3595e234 | 1981 | return -ENOTDIR; |
97242f99 | 1982 | } |
1da177e4 | 1983 | } |
1da177e4 LT |
1984 | } |
1985 | ||
c8a53ee5 | 1986 | static const char *path_init(struct nameidata *nd, unsigned flags) |
31e6b01f NP |
1987 | { |
1988 | int retval = 0; | |
c8a53ee5 | 1989 | const char *s = nd->name->name; |
31e6b01f NP |
1990 | |
1991 | nd->last_type = LAST_ROOT; /* if there are only slashes... */ | |
980f3ea2 | 1992 | nd->flags = flags | LOOKUP_JUMPED | LOOKUP_PARENT; |
31e6b01f | 1993 | nd->depth = 0; |
5b6ca027 | 1994 | if (flags & LOOKUP_ROOT) { |
b18825a7 DH |
1995 | struct dentry *root = nd->root.dentry; |
1996 | struct inode *inode = root->d_inode; | |
fd2f7cb5 | 1997 | if (*s) { |
44b1d530 | 1998 | if (!d_can_lookup(root)) |
368ee9ba | 1999 | return ERR_PTR(-ENOTDIR); |
73d049a4 AV |
2000 | retval = inode_permission(inode, MAY_EXEC); |
2001 | if (retval) | |
368ee9ba | 2002 | return ERR_PTR(retval); |
73d049a4 | 2003 | } |
5b6ca027 AV |
2004 | nd->path = nd->root; |
2005 | nd->inode = inode; | |
2006 | if (flags & LOOKUP_RCU) { | |
8b61e74f | 2007 | rcu_read_lock(); |
5b6ca027 | 2008 | nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); |
8f47a016 | 2009 | nd->root_seq = nd->seq; |
48a066e7 | 2010 | nd->m_seq = read_seqbegin(&mount_lock); |
5b6ca027 AV |
2011 | } else { |
2012 | path_get(&nd->path); | |
2013 | } | |
368ee9ba | 2014 | return s; |
5b6ca027 AV |
2015 | } |
2016 | ||
31e6b01f | 2017 | nd->root.mnt = NULL; |
31e6b01f | 2018 | |
48a066e7 | 2019 | nd->m_seq = read_seqbegin(&mount_lock); |
fd2f7cb5 | 2020 | if (*s == '/') { |
9e6697e2 | 2021 | if (flags & LOOKUP_RCU) |
8b61e74f | 2022 | rcu_read_lock(); |
9e6697e2 AV |
2023 | set_root(nd); |
2024 | if (flags & LOOKUP_RCU) { | |
06d7137e | 2025 | nd->seq = nd->root_seq; |
9e6697e2 | 2026 | nd->path = nd->root; |
e41f7d4e | 2027 | } else { |
e41f7d4e | 2028 | path_get(&nd->root); |
9e6697e2 | 2029 | nd->path = nd->root; |
e41f7d4e | 2030 | } |
ef55d917 AV |
2031 | nd->inode = nd->path.dentry->d_inode; |
2032 | if (!(flags & LOOKUP_RCU)) | |
2033 | return s; | |
2034 | if (likely(!read_seqcount_retry(&nd->path.dentry->d_seq, nd->seq))) | |
2035 | return s; | |
2036 | if (!(nd->flags & LOOKUP_ROOT)) | |
2037 | nd->root.mnt = NULL; | |
2038 | rcu_read_unlock(); | |
2039 | return ERR_PTR(-ECHILD); | |
c8a53ee5 | 2040 | } else if (nd->dfd == AT_FDCWD) { |
e41f7d4e AV |
2041 | if (flags & LOOKUP_RCU) { |
2042 | struct fs_struct *fs = current->fs; | |
2043 | unsigned seq; | |
31e6b01f | 2044 | |
8b61e74f | 2045 | rcu_read_lock(); |
c28cc364 | 2046 | |
e41f7d4e AV |
2047 | do { |
2048 | seq = read_seqcount_begin(&fs->seq); | |
2049 | nd->path = fs->pwd; | |
ef55d917 | 2050 | nd->inode = nd->path.dentry->d_inode; |
e41f7d4e AV |
2051 | nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); |
2052 | } while (read_seqcount_retry(&fs->seq, seq)); | |
2053 | } else { | |
2054 | get_fs_pwd(current->fs, &nd->path); | |
ef55d917 | 2055 | nd->inode = nd->path.dentry->d_inode; |
e41f7d4e | 2056 | } |
ef55d917 | 2057 | return s; |
31e6b01f | 2058 | } else { |
582aa64a | 2059 | /* Caller must check execute permissions on the starting path component */ |
c8a53ee5 | 2060 | struct fd f = fdget_raw(nd->dfd); |
31e6b01f NP |
2061 | struct dentry *dentry; |
2062 | ||
2903ff01 | 2063 | if (!f.file) |
368ee9ba | 2064 | return ERR_PTR(-EBADF); |
31e6b01f | 2065 | |
2903ff01 | 2066 | dentry = f.file->f_path.dentry; |
31e6b01f | 2067 | |
fd2f7cb5 | 2068 | if (*s) { |
44b1d530 | 2069 | if (!d_can_lookup(dentry)) { |
2903ff01 | 2070 | fdput(f); |
368ee9ba | 2071 | return ERR_PTR(-ENOTDIR); |
2903ff01 | 2072 | } |
f52e0c11 | 2073 | } |
31e6b01f | 2074 | |
2903ff01 | 2075 | nd->path = f.file->f_path; |
e41f7d4e | 2076 | if (flags & LOOKUP_RCU) { |
8b61e74f | 2077 | rcu_read_lock(); |
34a26b99 AV |
2078 | nd->inode = nd->path.dentry->d_inode; |
2079 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); | |
e41f7d4e | 2080 | } else { |
2903ff01 | 2081 | path_get(&nd->path); |
34a26b99 | 2082 | nd->inode = nd->path.dentry->d_inode; |
e41f7d4e | 2083 | } |
34a26b99 | 2084 | fdput(f); |
368ee9ba | 2085 | return s; |
31e6b01f | 2086 | } |
9b4a9b14 AV |
2087 | } |
2088 | ||
3bdba28b | 2089 | static const char *trailing_symlink(struct nameidata *nd) |
95fa25d9 AV |
2090 | { |
2091 | const char *s; | |
fec2fa24 | 2092 | int error = may_follow_link(nd); |
deb106c6 | 2093 | if (unlikely(error)) |
3bdba28b | 2094 | return ERR_PTR(error); |
95fa25d9 | 2095 | nd->flags |= LOOKUP_PARENT; |
fab51e8a | 2096 | nd->stack[0].name = NULL; |
3b2e7f75 | 2097 | s = get_link(nd); |
deb106c6 | 2098 | return s ? s : ""; |
95fa25d9 AV |
2099 | } |
2100 | ||
caa85634 | 2101 | static inline int lookup_last(struct nameidata *nd) |
bd92d7fe AV |
2102 | { |
2103 | if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) | |
2104 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; | |
2105 | ||
2106 | nd->flags &= ~LOOKUP_PARENT; | |
deb106c6 | 2107 | return walk_component(nd, |
4693a547 AV |
2108 | nd->flags & LOOKUP_FOLLOW |
2109 | ? nd->depth | |
2110 | ? WALK_PUT | WALK_GET | |
2111 | : WALK_GET | |
2112 | : 0); | |
bd92d7fe AV |
2113 | } |
2114 | ||
9b4a9b14 | 2115 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
c8a53ee5 | 2116 | static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path) |
9b4a9b14 | 2117 | { |
c8a53ee5 | 2118 | const char *s = path_init(nd, flags); |
bd92d7fe | 2119 | int err; |
31e6b01f | 2120 | |
368ee9ba AV |
2121 | if (IS_ERR(s)) |
2122 | return PTR_ERR(s); | |
3bdba28b AV |
2123 | while (!(err = link_path_walk(s, nd)) |
2124 | && ((err = lookup_last(nd)) > 0)) { | |
2125 | s = trailing_symlink(nd); | |
2126 | if (IS_ERR(s)) { | |
2127 | err = PTR_ERR(s); | |
2128 | break; | |
bd92d7fe AV |
2129 | } |
2130 | } | |
9f1fafee AV |
2131 | if (!err) |
2132 | err = complete_walk(nd); | |
bd92d7fe | 2133 | |
deb106c6 AV |
2134 | if (!err && nd->flags & LOOKUP_DIRECTORY) |
2135 | if (!d_can_lookup(nd->path.dentry)) | |
bd23a539 | 2136 | err = -ENOTDIR; |
625b6d10 AV |
2137 | if (!err) { |
2138 | *path = nd->path; | |
2139 | nd->path.mnt = NULL; | |
2140 | nd->path.dentry = NULL; | |
2141 | } | |
2142 | terminate_walk(nd); | |
bd92d7fe | 2143 | return err; |
ee0827cd | 2144 | } |
31e6b01f | 2145 | |
625b6d10 | 2146 | static int filename_lookup(int dfd, struct filename *name, unsigned flags, |
9ad1aaa6 | 2147 | struct path *path, struct path *root) |
ee0827cd | 2148 | { |
894bc8c4 | 2149 | int retval; |
9883d185 | 2150 | struct nameidata nd; |
abc9f5be AV |
2151 | if (IS_ERR(name)) |
2152 | return PTR_ERR(name); | |
9ad1aaa6 AV |
2153 | if (unlikely(root)) { |
2154 | nd.root = *root; | |
2155 | flags |= LOOKUP_ROOT; | |
2156 | } | |
9883d185 | 2157 | set_nameidata(&nd, dfd, name); |
c8a53ee5 | 2158 | retval = path_lookupat(&nd, flags | LOOKUP_RCU, path); |
ee0827cd | 2159 | if (unlikely(retval == -ECHILD)) |
c8a53ee5 | 2160 | retval = path_lookupat(&nd, flags, path); |
ee0827cd | 2161 | if (unlikely(retval == -ESTALE)) |
c8a53ee5 | 2162 | retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path); |
31e6b01f | 2163 | |
f78570dd | 2164 | if (likely(!retval)) |
625b6d10 | 2165 | audit_inode(name, path->dentry, flags & LOOKUP_PARENT); |
9883d185 | 2166 | restore_nameidata(); |
e4bd1c1a | 2167 | putname(name); |
170aa3d0 | 2168 | return retval; |
1da177e4 LT |
2169 | } |
2170 | ||
8bcb77fa | 2171 | /* Returns 0 and nd will be valid on success; Retuns error, otherwise. */ |
c8a53ee5 | 2172 | static int path_parentat(struct nameidata *nd, unsigned flags, |
391172c4 | 2173 | struct path *parent) |
8bcb77fa | 2174 | { |
c8a53ee5 | 2175 | const char *s = path_init(nd, flags); |
368ee9ba AV |
2176 | int err; |
2177 | if (IS_ERR(s)) | |
2178 | return PTR_ERR(s); | |
2179 | err = link_path_walk(s, nd); | |
8bcb77fa AV |
2180 | if (!err) |
2181 | err = complete_walk(nd); | |
391172c4 AV |
2182 | if (!err) { |
2183 | *parent = nd->path; | |
2184 | nd->path.mnt = NULL; | |
2185 | nd->path.dentry = NULL; | |
2186 | } | |
2187 | terminate_walk(nd); | |
8bcb77fa AV |
2188 | return err; |
2189 | } | |
2190 | ||
5c31b6ce | 2191 | static struct filename *filename_parentat(int dfd, struct filename *name, |
391172c4 AV |
2192 | unsigned int flags, struct path *parent, |
2193 | struct qstr *last, int *type) | |
8bcb77fa AV |
2194 | { |
2195 | int retval; | |
9883d185 | 2196 | struct nameidata nd; |
8bcb77fa | 2197 | |
5c31b6ce AV |
2198 | if (IS_ERR(name)) |
2199 | return name; | |
9883d185 | 2200 | set_nameidata(&nd, dfd, name); |
c8a53ee5 | 2201 | retval = path_parentat(&nd, flags | LOOKUP_RCU, parent); |
8bcb77fa | 2202 | if (unlikely(retval == -ECHILD)) |
c8a53ee5 | 2203 | retval = path_parentat(&nd, flags, parent); |
8bcb77fa | 2204 | if (unlikely(retval == -ESTALE)) |
c8a53ee5 | 2205 | retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent); |
391172c4 AV |
2206 | if (likely(!retval)) { |
2207 | *last = nd.last; | |
2208 | *type = nd.last_type; | |
2209 | audit_inode(name, parent->dentry, LOOKUP_PARENT); | |
5c31b6ce AV |
2210 | } else { |
2211 | putname(name); | |
2212 | name = ERR_PTR(retval); | |
391172c4 | 2213 | } |
9883d185 | 2214 | restore_nameidata(); |
5c31b6ce | 2215 | return name; |
8bcb77fa AV |
2216 | } |
2217 | ||
79714f72 AV |
2218 | /* does lookup, returns the object with parent locked */ |
2219 | struct dentry *kern_path_locked(const char *name, struct path *path) | |
5590ff0d | 2220 | { |
5c31b6ce AV |
2221 | struct filename *filename; |
2222 | struct dentry *d; | |
391172c4 AV |
2223 | struct qstr last; |
2224 | int type; | |
51689104 | 2225 | |
5c31b6ce AV |
2226 | filename = filename_parentat(AT_FDCWD, getname_kernel(name), 0, path, |
2227 | &last, &type); | |
51689104 PM |
2228 | if (IS_ERR(filename)) |
2229 | return ERR_CAST(filename); | |
5c31b6ce | 2230 | if (unlikely(type != LAST_NORM)) { |
391172c4 | 2231 | path_put(path); |
5c31b6ce AV |
2232 | putname(filename); |
2233 | return ERR_PTR(-EINVAL); | |
79714f72 | 2234 | } |
391172c4 AV |
2235 | mutex_lock_nested(&path->dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
2236 | d = __lookup_hash(&last, path->dentry, 0); | |
79714f72 | 2237 | if (IS_ERR(d)) { |
391172c4 AV |
2238 | mutex_unlock(&path->dentry->d_inode->i_mutex); |
2239 | path_put(path); | |
79714f72 | 2240 | } |
51689104 | 2241 | putname(filename); |
79714f72 | 2242 | return d; |
5590ff0d UD |
2243 | } |
2244 | ||
d1811465 AV |
2245 | int kern_path(const char *name, unsigned int flags, struct path *path) |
2246 | { | |
abc9f5be AV |
2247 | return filename_lookup(AT_FDCWD, getname_kernel(name), |
2248 | flags, path, NULL); | |
d1811465 | 2249 | } |
4d359507 | 2250 | EXPORT_SYMBOL(kern_path); |
d1811465 | 2251 | |
16f18200 JJS |
2252 | /** |
2253 | * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair | |
2254 | * @dentry: pointer to dentry of the base directory | |
2255 | * @mnt: pointer to vfs mount of the base directory | |
2256 | * @name: pointer to file name | |
2257 | * @flags: lookup flags | |
e0a01249 | 2258 | * @path: pointer to struct path to fill |
16f18200 JJS |
2259 | */ |
2260 | int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, | |
2261 | const char *name, unsigned int flags, | |
e0a01249 | 2262 | struct path *path) |
16f18200 | 2263 | { |
9ad1aaa6 | 2264 | struct path root = {.mnt = mnt, .dentry = dentry}; |
9ad1aaa6 | 2265 | /* the first argument of filename_lookup() is ignored with root */ |
abc9f5be AV |
2266 | return filename_lookup(AT_FDCWD, getname_kernel(name), |
2267 | flags , path, &root); | |
16f18200 | 2268 | } |
4d359507 | 2269 | EXPORT_SYMBOL(vfs_path_lookup); |
16f18200 | 2270 | |
eead1911 | 2271 | /** |
a6b91919 | 2272 | * lookup_one_len - filesystem helper to lookup single pathname component |
eead1911 CH |
2273 | * @name: pathname component to lookup |
2274 | * @base: base directory to lookup from | |
2275 | * @len: maximum length @len should be interpreted to | |
2276 | * | |
a6b91919 | 2277 | * Note that this routine is purely a helper for filesystem usage and should |
9e7543e9 | 2278 | * not be called by generic code. |
eead1911 | 2279 | */ |
057f6c01 JM |
2280 | struct dentry *lookup_one_len(const char *name, struct dentry *base, int len) |
2281 | { | |
057f6c01 | 2282 | struct qstr this; |
6a96ba54 | 2283 | unsigned int c; |
cda309de | 2284 | int err; |
057f6c01 | 2285 | |
2f9092e1 DW |
2286 | WARN_ON_ONCE(!mutex_is_locked(&base->d_inode->i_mutex)); |
2287 | ||
6a96ba54 AV |
2288 | this.name = name; |
2289 | this.len = len; | |
0145acc2 | 2290 | this.hash = full_name_hash(name, len); |
6a96ba54 AV |
2291 | if (!len) |
2292 | return ERR_PTR(-EACCES); | |
2293 | ||
21d8a15a AV |
2294 | if (unlikely(name[0] == '.')) { |
2295 | if (len < 2 || (len == 2 && name[1] == '.')) | |
2296 | return ERR_PTR(-EACCES); | |
2297 | } | |
2298 | ||
6a96ba54 AV |
2299 | while (len--) { |
2300 | c = *(const unsigned char *)name++; | |
2301 | if (c == '/' || c == '\0') | |
2302 | return ERR_PTR(-EACCES); | |
6a96ba54 | 2303 | } |
5a202bcd AV |
2304 | /* |
2305 | * See if the low-level filesystem might want | |
2306 | * to use its own hash.. | |
2307 | */ | |
2308 | if (base->d_flags & DCACHE_OP_HASH) { | |
da53be12 | 2309 | int err = base->d_op->d_hash(base, &this); |
5a202bcd AV |
2310 | if (err < 0) |
2311 | return ERR_PTR(err); | |
2312 | } | |
eead1911 | 2313 | |
cda309de MS |
2314 | err = inode_permission(base->d_inode, MAY_EXEC); |
2315 | if (err) | |
2316 | return ERR_PTR(err); | |
2317 | ||
72bd866a | 2318 | return __lookup_hash(&this, base, 0); |
057f6c01 | 2319 | } |
4d359507 | 2320 | EXPORT_SYMBOL(lookup_one_len); |
057f6c01 | 2321 | |
1fa1e7f6 AW |
2322 | int user_path_at_empty(int dfd, const char __user *name, unsigned flags, |
2323 | struct path *path, int *empty) | |
1da177e4 | 2324 | { |
abc9f5be AV |
2325 | return filename_lookup(dfd, getname_flags(name, flags, empty), |
2326 | flags, path, NULL); | |
1da177e4 | 2327 | } |
b853a161 | 2328 | EXPORT_SYMBOL(user_path_at_empty); |
1fa1e7f6 | 2329 | |
873f1eed JL |
2330 | /* |
2331 | * NB: most callers don't do anything directly with the reference to the | |
2332 | * to struct filename, but the nd->last pointer points into the name string | |
2333 | * allocated by getname. So we must hold the reference to it until all | |
2334 | * path-walking is complete. | |
2335 | */ | |
a2ec4a2d | 2336 | static inline struct filename * |
f5beed75 AV |
2337 | user_path_parent(int dfd, const char __user *path, |
2338 | struct path *parent, | |
2339 | struct qstr *last, | |
2340 | int *type, | |
9e790bd6 | 2341 | unsigned int flags) |
2ad94ae6 | 2342 | { |
9e790bd6 | 2343 | /* only LOOKUP_REVAL is allowed in extra flags */ |
5c31b6ce AV |
2344 | return filename_parentat(dfd, getname(path), flags & LOOKUP_REVAL, |
2345 | parent, last, type); | |
2ad94ae6 AV |
2346 | } |
2347 | ||
8033426e | 2348 | /** |
197df04c | 2349 | * mountpoint_last - look up last component for umount |
8033426e JL |
2350 | * @nd: pathwalk nameidata - currently pointing at parent directory of "last" |
2351 | * @path: pointer to container for result | |
2352 | * | |
2353 | * This is a special lookup_last function just for umount. In this case, we | |
2354 | * need to resolve the path without doing any revalidation. | |
2355 | * | |
2356 | * The nameidata should be the result of doing a LOOKUP_PARENT pathwalk. Since | |
2357 | * mountpoints are always pinned in the dcache, their ancestors are too. Thus, | |
2358 | * in almost all cases, this lookup will be served out of the dcache. The only | |
2359 | * cases where it won't are if nd->last refers to a symlink or the path is | |
2360 | * bogus and it doesn't exist. | |
2361 | * | |
2362 | * Returns: | |
2363 | * -error: if there was an error during lookup. This includes -ENOENT if the | |
2364 | * lookup found a negative dentry. The nd->path reference will also be | |
2365 | * put in this case. | |
2366 | * | |
2367 | * 0: if we successfully resolved nd->path and found it to not to be a | |
2368 | * symlink that needs to be followed. "path" will also be populated. | |
2369 | * The nd->path reference will also be put. | |
2370 | * | |
2371 | * 1: if we successfully resolved nd->last and found it to be a symlink | |
2372 | * that needs to be followed. "path" will be populated with the path | |
2373 | * to the link, and nd->path will *not* be put. | |
2374 | */ | |
2375 | static int | |
197df04c | 2376 | mountpoint_last(struct nameidata *nd, struct path *path) |
8033426e JL |
2377 | { |
2378 | int error = 0; | |
2379 | struct dentry *dentry; | |
2380 | struct dentry *dir = nd->path.dentry; | |
2381 | ||
35759521 AV |
2382 | /* If we're in rcuwalk, drop out of it to handle last component */ |
2383 | if (nd->flags & LOOKUP_RCU) { | |
6e9918b7 | 2384 | if (unlazy_walk(nd, NULL, 0)) |
deb106c6 | 2385 | return -ECHILD; |
8033426e JL |
2386 | } |
2387 | ||
2388 | nd->flags &= ~LOOKUP_PARENT; | |
2389 | ||
2390 | if (unlikely(nd->last_type != LAST_NORM)) { | |
2391 | error = handle_dots(nd, nd->last_type); | |
35759521 | 2392 | if (error) |
deb106c6 | 2393 | return error; |
35759521 AV |
2394 | dentry = dget(nd->path.dentry); |
2395 | goto done; | |
8033426e JL |
2396 | } |
2397 | ||
2398 | mutex_lock(&dir->d_inode->i_mutex); | |
2399 | dentry = d_lookup(dir, &nd->last); | |
2400 | if (!dentry) { | |
2401 | /* | |
2402 | * No cached dentry. Mounted dentries are pinned in the cache, | |
2403 | * so that means that this dentry is probably a symlink or the | |
2404 | * path doesn't actually point to a mounted dentry. | |
2405 | */ | |
2406 | dentry = d_alloc(dir, &nd->last); | |
2407 | if (!dentry) { | |
bcceeeba | 2408 | mutex_unlock(&dir->d_inode->i_mutex); |
deb106c6 | 2409 | return -ENOMEM; |
8033426e | 2410 | } |
35759521 | 2411 | dentry = lookup_real(dir->d_inode, dentry, nd->flags); |
bcceeeba DJ |
2412 | if (IS_ERR(dentry)) { |
2413 | mutex_unlock(&dir->d_inode->i_mutex); | |
deb106c6 | 2414 | return PTR_ERR(dentry); |
bcceeeba | 2415 | } |
8033426e JL |
2416 | } |
2417 | mutex_unlock(&dir->d_inode->i_mutex); | |
2418 | ||
35759521 | 2419 | done: |
698934df | 2420 | if (d_is_negative(dentry)) { |
35759521 | 2421 | dput(dentry); |
deb106c6 | 2422 | return -ENOENT; |
8033426e | 2423 | } |
191d7f73 AV |
2424 | if (nd->depth) |
2425 | put_link(nd); | |
35759521 | 2426 | path->dentry = dentry; |
295dc39d | 2427 | path->mnt = nd->path.mnt; |
181548c0 AV |
2428 | error = should_follow_link(nd, path, nd->flags & LOOKUP_FOLLOW, |
2429 | d_backing_inode(dentry), 0); | |
deb106c6 | 2430 | if (unlikely(error)) |
d63ff28f | 2431 | return error; |
295dc39d | 2432 | mntget(path->mnt); |
35759521 | 2433 | follow_mount(path); |
deb106c6 | 2434 | return 0; |
8033426e JL |
2435 | } |
2436 | ||
2437 | /** | |
197df04c | 2438 | * path_mountpoint - look up a path to be umounted |
2a78b857 | 2439 | * @nd: lookup context |
8033426e | 2440 | * @flags: lookup flags |
c8a53ee5 | 2441 | * @path: pointer to container for result |
8033426e JL |
2442 | * |
2443 | * Look up the given name, but don't attempt to revalidate the last component. | |
606d6fe3 | 2444 | * Returns 0 and "path" will be valid on success; Returns error otherwise. |
8033426e JL |
2445 | */ |
2446 | static int | |
c8a53ee5 | 2447 | path_mountpoint(struct nameidata *nd, unsigned flags, struct path *path) |
8033426e | 2448 | { |
c8a53ee5 | 2449 | const char *s = path_init(nd, flags); |
368ee9ba AV |
2450 | int err; |
2451 | if (IS_ERR(s)) | |
2452 | return PTR_ERR(s); | |
3bdba28b AV |
2453 | while (!(err = link_path_walk(s, nd)) && |
2454 | (err = mountpoint_last(nd, path)) > 0) { | |
2455 | s = trailing_symlink(nd); | |
2456 | if (IS_ERR(s)) { | |
2457 | err = PTR_ERR(s); | |
8033426e | 2458 | break; |
3bdba28b | 2459 | } |
8033426e | 2460 | } |
deb106c6 | 2461 | terminate_walk(nd); |
8033426e JL |
2462 | return err; |
2463 | } | |
2464 | ||
2d864651 | 2465 | static int |
668696dc | 2466 | filename_mountpoint(int dfd, struct filename *name, struct path *path, |
2d864651 AV |
2467 | unsigned int flags) |
2468 | { | |
9883d185 | 2469 | struct nameidata nd; |
cbaab2db | 2470 | int error; |
668696dc AV |
2471 | if (IS_ERR(name)) |
2472 | return PTR_ERR(name); | |
9883d185 | 2473 | set_nameidata(&nd, dfd, name); |
c8a53ee5 | 2474 | error = path_mountpoint(&nd, flags | LOOKUP_RCU, path); |
2d864651 | 2475 | if (unlikely(error == -ECHILD)) |
c8a53ee5 | 2476 | error = path_mountpoint(&nd, flags, path); |
2d864651 | 2477 | if (unlikely(error == -ESTALE)) |
c8a53ee5 | 2478 | error = path_mountpoint(&nd, flags | LOOKUP_REVAL, path); |
2d864651 | 2479 | if (likely(!error)) |
668696dc | 2480 | audit_inode(name, path->dentry, 0); |
9883d185 | 2481 | restore_nameidata(); |
668696dc | 2482 | putname(name); |
2d864651 AV |
2483 | return error; |
2484 | } | |
2485 | ||
8033426e | 2486 | /** |
197df04c | 2487 | * user_path_mountpoint_at - lookup a path from userland in order to umount it |
8033426e JL |
2488 | * @dfd: directory file descriptor |
2489 | * @name: pathname from userland | |
2490 | * @flags: lookup flags | |
2491 | * @path: pointer to container to hold result | |
2492 | * | |
2493 | * A umount is a special case for path walking. We're not actually interested | |
2494 | * in the inode in this situation, and ESTALE errors can be a problem. We | |
2495 | * simply want track down the dentry and vfsmount attached at the mountpoint | |
2496 | * and avoid revalidating the last component. | |
2497 | * | |
2498 | * Returns 0 and populates "path" on success. | |
2499 | */ | |
2500 | int | |
197df04c | 2501 | user_path_mountpoint_at(int dfd, const char __user *name, unsigned int flags, |
8033426e JL |
2502 | struct path *path) |
2503 | { | |
cbaab2db | 2504 | return filename_mountpoint(dfd, getname(name), path, flags); |
8033426e JL |
2505 | } |
2506 | ||
2d864651 AV |
2507 | int |
2508 | kern_path_mountpoint(int dfd, const char *name, struct path *path, | |
2509 | unsigned int flags) | |
2510 | { | |
cbaab2db | 2511 | return filename_mountpoint(dfd, getname_kernel(name), path, flags); |
2d864651 AV |
2512 | } |
2513 | EXPORT_SYMBOL(kern_path_mountpoint); | |
2514 | ||
cbdf35bc | 2515 | int __check_sticky(struct inode *dir, struct inode *inode) |
1da177e4 | 2516 | { |
8e96e3b7 | 2517 | kuid_t fsuid = current_fsuid(); |
da9592ed | 2518 | |
8e96e3b7 | 2519 | if (uid_eq(inode->i_uid, fsuid)) |
1da177e4 | 2520 | return 0; |
8e96e3b7 | 2521 | if (uid_eq(dir->i_uid, fsuid)) |
1da177e4 | 2522 | return 0; |
23adbe12 | 2523 | return !capable_wrt_inode_uidgid(inode, CAP_FOWNER); |
1da177e4 | 2524 | } |
cbdf35bc | 2525 | EXPORT_SYMBOL(__check_sticky); |
1da177e4 LT |
2526 | |
2527 | /* | |
2528 | * Check whether we can remove a link victim from directory dir, check | |
2529 | * whether the type of victim is right. | |
2530 | * 1. We can't do it if dir is read-only (done in permission()) | |
2531 | * 2. We should have write and exec permissions on dir | |
2532 | * 3. We can't remove anything from append-only dir | |
2533 | * 4. We can't do anything with immutable dir (done in permission()) | |
2534 | * 5. If the sticky bit on dir is set we should either | |
2535 | * a. be owner of dir, or | |
2536 | * b. be owner of victim, or | |
2537 | * c. have CAP_FOWNER capability | |
2538 | * 6. If the victim is append-only or immutable we can't do antyhing with | |
2539 | * links pointing to it. | |
2540 | * 7. If we were asked to remove a directory and victim isn't one - ENOTDIR. | |
2541 | * 8. If we were asked to remove a non-directory and victim isn't one - EISDIR. | |
2542 | * 9. We can't remove a root or mountpoint. | |
2543 | * 10. We don't allow removal of NFS sillyrenamed files; it's handled by | |
2544 | * nfs_async_unlink(). | |
2545 | */ | |
b18825a7 | 2546 | static int may_delete(struct inode *dir, struct dentry *victim, bool isdir) |
1da177e4 | 2547 | { |
63afdfc7 | 2548 | struct inode *inode = d_backing_inode(victim); |
1da177e4 LT |
2549 | int error; |
2550 | ||
b18825a7 | 2551 | if (d_is_negative(victim)) |
1da177e4 | 2552 | return -ENOENT; |
b18825a7 | 2553 | BUG_ON(!inode); |
1da177e4 LT |
2554 | |
2555 | BUG_ON(victim->d_parent->d_inode != dir); | |
4fa6b5ec | 2556 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
1da177e4 | 2557 | |
f419a2e3 | 2558 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); |
1da177e4 LT |
2559 | if (error) |
2560 | return error; | |
2561 | if (IS_APPEND(dir)) | |
2562 | return -EPERM; | |
b18825a7 DH |
2563 | |
2564 | if (check_sticky(dir, inode) || IS_APPEND(inode) || | |
2565 | IS_IMMUTABLE(inode) || IS_SWAPFILE(inode)) | |
1da177e4 LT |
2566 | return -EPERM; |
2567 | if (isdir) { | |
44b1d530 | 2568 | if (!d_is_dir(victim)) |
1da177e4 LT |
2569 | return -ENOTDIR; |
2570 | if (IS_ROOT(victim)) | |
2571 | return -EBUSY; | |
44b1d530 | 2572 | } else if (d_is_dir(victim)) |
1da177e4 LT |
2573 | return -EISDIR; |
2574 | if (IS_DEADDIR(dir)) | |
2575 | return -ENOENT; | |
2576 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) | |
2577 | return -EBUSY; | |
2578 | return 0; | |
2579 | } | |
2580 | ||
2581 | /* Check whether we can create an object with dentry child in directory | |
2582 | * dir. | |
2583 | * 1. We can't do it if child already exists (open has special treatment for | |
2584 | * this case, but since we are inlined it's OK) | |
2585 | * 2. We can't do it if dir is read-only (done in permission()) | |
2586 | * 3. We should have write and exec permissions on dir | |
2587 | * 4. We can't do it if dir is immutable (done in permission()) | |
2588 | */ | |
a95164d9 | 2589 | static inline int may_create(struct inode *dir, struct dentry *child) |
1da177e4 | 2590 | { |
14e972b4 | 2591 | audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE); |
1da177e4 LT |
2592 | if (child->d_inode) |
2593 | return -EEXIST; | |
2594 | if (IS_DEADDIR(dir)) | |
2595 | return -ENOENT; | |
f419a2e3 | 2596 | return inode_permission(dir, MAY_WRITE | MAY_EXEC); |
1da177e4 LT |
2597 | } |
2598 | ||
1da177e4 LT |
2599 | /* |
2600 | * p1 and p2 should be directories on the same fs. | |
2601 | */ | |
2602 | struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) | |
2603 | { | |
2604 | struct dentry *p; | |
2605 | ||
2606 | if (p1 == p2) { | |
f2eace23 | 2607 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); |
1da177e4 LT |
2608 | return NULL; |
2609 | } | |
2610 | ||
a11f3a05 | 2611 | mutex_lock(&p1->d_inode->i_sb->s_vfs_rename_mutex); |
1da177e4 | 2612 | |
e2761a11 OH |
2613 | p = d_ancestor(p2, p1); |
2614 | if (p) { | |
2615 | mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT); | |
2616 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_CHILD); | |
2617 | return p; | |
1da177e4 LT |
2618 | } |
2619 | ||
e2761a11 OH |
2620 | p = d_ancestor(p1, p2); |
2621 | if (p) { | |
2622 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); | |
2623 | mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_CHILD); | |
2624 | return p; | |
1da177e4 LT |
2625 | } |
2626 | ||
f2eace23 | 2627 | mutex_lock_nested(&p1->d_inode->i_mutex, I_MUTEX_PARENT); |
d1b72cc6 | 2628 | mutex_lock_nested(&p2->d_inode->i_mutex, I_MUTEX_PARENT2); |
1da177e4 LT |
2629 | return NULL; |
2630 | } | |
4d359507 | 2631 | EXPORT_SYMBOL(lock_rename); |
1da177e4 LT |
2632 | |
2633 | void unlock_rename(struct dentry *p1, struct dentry *p2) | |
2634 | { | |
1b1dcc1b | 2635 | mutex_unlock(&p1->d_inode->i_mutex); |
1da177e4 | 2636 | if (p1 != p2) { |
1b1dcc1b | 2637 | mutex_unlock(&p2->d_inode->i_mutex); |
a11f3a05 | 2638 | mutex_unlock(&p1->d_inode->i_sb->s_vfs_rename_mutex); |
1da177e4 LT |
2639 | } |
2640 | } | |
4d359507 | 2641 | EXPORT_SYMBOL(unlock_rename); |
1da177e4 | 2642 | |
4acdaf27 | 2643 | int vfs_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
312b63fb | 2644 | bool want_excl) |
1da177e4 | 2645 | { |
a95164d9 | 2646 | int error = may_create(dir, dentry); |
1da177e4 LT |
2647 | if (error) |
2648 | return error; | |
2649 | ||
acfa4380 | 2650 | if (!dir->i_op->create) |
1da177e4 LT |
2651 | return -EACCES; /* shouldn't it be ENOSYS? */ |
2652 | mode &= S_IALLUGO; | |
2653 | mode |= S_IFREG; | |
2654 | error = security_inode_create(dir, dentry, mode); | |
2655 | if (error) | |
2656 | return error; | |
312b63fb | 2657 | error = dir->i_op->create(dir, dentry, mode, want_excl); |
a74574aa | 2658 | if (!error) |
f38aa942 | 2659 | fsnotify_create(dir, dentry); |
1da177e4 LT |
2660 | return error; |
2661 | } | |
4d359507 | 2662 | EXPORT_SYMBOL(vfs_create); |
1da177e4 | 2663 | |
73d049a4 | 2664 | static int may_open(struct path *path, int acc_mode, int flag) |
1da177e4 | 2665 | { |
3fb64190 | 2666 | struct dentry *dentry = path->dentry; |
1da177e4 LT |
2667 | struct inode *inode = dentry->d_inode; |
2668 | int error; | |
2669 | ||
bcda7652 AV |
2670 | /* O_PATH? */ |
2671 | if (!acc_mode) | |
2672 | return 0; | |
2673 | ||
1da177e4 LT |
2674 | if (!inode) |
2675 | return -ENOENT; | |
2676 | ||
c8fe8f30 CH |
2677 | switch (inode->i_mode & S_IFMT) { |
2678 | case S_IFLNK: | |
1da177e4 | 2679 | return -ELOOP; |
c8fe8f30 CH |
2680 | case S_IFDIR: |
2681 | if (acc_mode & MAY_WRITE) | |
2682 | return -EISDIR; | |
2683 | break; | |
2684 | case S_IFBLK: | |
2685 | case S_IFCHR: | |
3fb64190 | 2686 | if (path->mnt->mnt_flags & MNT_NODEV) |
1da177e4 | 2687 | return -EACCES; |
c8fe8f30 CH |
2688 | /*FALLTHRU*/ |
2689 | case S_IFIFO: | |
2690 | case S_IFSOCK: | |
1da177e4 | 2691 | flag &= ~O_TRUNC; |
c8fe8f30 | 2692 | break; |
4a3fd211 | 2693 | } |
b41572e9 | 2694 | |
3fb64190 | 2695 | error = inode_permission(inode, acc_mode); |
b41572e9 DH |
2696 | if (error) |
2697 | return error; | |
6146f0d5 | 2698 | |
1da177e4 LT |
2699 | /* |
2700 | * An append-only file must be opened in append mode for writing. | |
2701 | */ | |
2702 | if (IS_APPEND(inode)) { | |
8737c930 | 2703 | if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) |
7715b521 | 2704 | return -EPERM; |
1da177e4 | 2705 | if (flag & O_TRUNC) |
7715b521 | 2706 | return -EPERM; |
1da177e4 LT |
2707 | } |
2708 | ||
2709 | /* O_NOATIME can only be set by the owner or superuser */ | |
2e149670 | 2710 | if (flag & O_NOATIME && !inode_owner_or_capable(inode)) |
7715b521 | 2711 | return -EPERM; |
1da177e4 | 2712 | |
f3c7691e | 2713 | return 0; |
7715b521 | 2714 | } |
1da177e4 | 2715 | |
e1181ee6 | 2716 | static int handle_truncate(struct file *filp) |
7715b521 | 2717 | { |
e1181ee6 | 2718 | struct path *path = &filp->f_path; |
7715b521 AV |
2719 | struct inode *inode = path->dentry->d_inode; |
2720 | int error = get_write_access(inode); | |
2721 | if (error) | |
2722 | return error; | |
2723 | /* | |
2724 | * Refuse to truncate files with mandatory locks held on them. | |
2725 | */ | |
d7a06983 | 2726 | error = locks_verify_locked(filp); |
7715b521 | 2727 | if (!error) |
ea0d3ab2 | 2728 | error = security_path_truncate(path); |
7715b521 AV |
2729 | if (!error) { |
2730 | error = do_truncate(path->dentry, 0, | |
2731 | ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, | |
e1181ee6 | 2732 | filp); |
7715b521 AV |
2733 | } |
2734 | put_write_access(inode); | |
acd0c935 | 2735 | return error; |
1da177e4 LT |
2736 | } |
2737 | ||
d57999e1 DH |
2738 | static inline int open_to_namei_flags(int flag) |
2739 | { | |
8a5e929d AV |
2740 | if ((flag & O_ACCMODE) == 3) |
2741 | flag--; | |
d57999e1 DH |
2742 | return flag; |
2743 | } | |
2744 | ||
d18e9008 MS |
2745 | static int may_o_create(struct path *dir, struct dentry *dentry, umode_t mode) |
2746 | { | |
2747 | int error = security_path_mknod(dir, dentry, mode, 0); | |
2748 | if (error) | |
2749 | return error; | |
2750 | ||
2751 | error = inode_permission(dir->dentry->d_inode, MAY_WRITE | MAY_EXEC); | |
2752 | if (error) | |
2753 | return error; | |
2754 | ||
2755 | return security_inode_create(dir->dentry->d_inode, dentry, mode); | |
2756 | } | |
2757 | ||
1acf0af9 DH |
2758 | /* |
2759 | * Attempt to atomically look up, create and open a file from a negative | |
2760 | * dentry. | |
2761 | * | |
2762 | * Returns 0 if successful. The file will have been created and attached to | |
2763 | * @file by the filesystem calling finish_open(). | |
2764 | * | |
2765 | * Returns 1 if the file was looked up only or didn't need creating. The | |
2766 | * caller will need to perform the open themselves. @path will have been | |
2767 | * updated to point to the new dentry. This may be negative. | |
2768 | * | |
2769 | * Returns an error code otherwise. | |
2770 | */ | |
2675a4eb AV |
2771 | static int atomic_open(struct nameidata *nd, struct dentry *dentry, |
2772 | struct path *path, struct file *file, | |
2773 | const struct open_flags *op, | |
64894cf8 | 2774 | bool got_write, bool need_lookup, |
2675a4eb | 2775 | int *opened) |
d18e9008 MS |
2776 | { |
2777 | struct inode *dir = nd->path.dentry->d_inode; | |
2778 | unsigned open_flag = open_to_namei_flags(op->open_flag); | |
2779 | umode_t mode; | |
2780 | int error; | |
2781 | int acc_mode; | |
d18e9008 MS |
2782 | int create_error = 0; |
2783 | struct dentry *const DENTRY_NOT_SET = (void *) -1UL; | |
116cc022 | 2784 | bool excl; |
d18e9008 MS |
2785 | |
2786 | BUG_ON(dentry->d_inode); | |
2787 | ||
2788 | /* Don't create child dentry for a dead directory. */ | |
2789 | if (unlikely(IS_DEADDIR(dir))) { | |
2675a4eb | 2790 | error = -ENOENT; |
d18e9008 MS |
2791 | goto out; |
2792 | } | |
2793 | ||
62b259d8 | 2794 | mode = op->mode; |
d18e9008 MS |
2795 | if ((open_flag & O_CREAT) && !IS_POSIXACL(dir)) |
2796 | mode &= ~current_umask(); | |
2797 | ||
116cc022 MS |
2798 | excl = (open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT); |
2799 | if (excl) | |
d18e9008 | 2800 | open_flag &= ~O_TRUNC; |
d18e9008 MS |
2801 | |
2802 | /* | |
2803 | * Checking write permission is tricky, bacuse we don't know if we are | |
2804 | * going to actually need it: O_CREAT opens should work as long as the | |
2805 | * file exists. But checking existence breaks atomicity. The trick is | |
2806 | * to check access and if not granted clear O_CREAT from the flags. | |
2807 | * | |
2808 | * Another problem is returing the "right" error value (e.g. for an | |
2809 | * O_EXCL open we want to return EEXIST not EROFS). | |
2810 | */ | |
64894cf8 AV |
2811 | if (((open_flag & (O_CREAT | O_TRUNC)) || |
2812 | (open_flag & O_ACCMODE) != O_RDONLY) && unlikely(!got_write)) { | |
2813 | if (!(open_flag & O_CREAT)) { | |
d18e9008 MS |
2814 | /* |
2815 | * No O_CREATE -> atomicity not a requirement -> fall | |
2816 | * back to lookup + open | |
2817 | */ | |
2818 | goto no_open; | |
2819 | } else if (open_flag & (O_EXCL | O_TRUNC)) { | |
2820 | /* Fall back and fail with the right error */ | |
64894cf8 | 2821 | create_error = -EROFS; |
d18e9008 MS |
2822 | goto no_open; |
2823 | } else { | |
2824 | /* No side effects, safe to clear O_CREAT */ | |
64894cf8 | 2825 | create_error = -EROFS; |
d18e9008 MS |
2826 | open_flag &= ~O_CREAT; |
2827 | } | |
2828 | } | |
2829 | ||
2830 | if (open_flag & O_CREAT) { | |
38227f78 | 2831 | error = may_o_create(&nd->path, dentry, mode); |
d18e9008 MS |
2832 | if (error) { |
2833 | create_error = error; | |
2834 | if (open_flag & O_EXCL) | |
2835 | goto no_open; | |
2836 | open_flag &= ~O_CREAT; | |
2837 | } | |
2838 | } | |
2839 | ||
2840 | if (nd->flags & LOOKUP_DIRECTORY) | |
2841 | open_flag |= O_DIRECTORY; | |
2842 | ||
30d90494 AV |
2843 | file->f_path.dentry = DENTRY_NOT_SET; |
2844 | file->f_path.mnt = nd->path.mnt; | |
2845 | error = dir->i_op->atomic_open(dir, dentry, file, open_flag, mode, | |
47237687 | 2846 | opened); |
d9585277 | 2847 | if (error < 0) { |
d9585277 AV |
2848 | if (create_error && error == -ENOENT) |
2849 | error = create_error; | |
d18e9008 MS |
2850 | goto out; |
2851 | } | |
2852 | ||
d9585277 | 2853 | if (error) { /* returned 1, that is */ |
30d90494 | 2854 | if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { |
2675a4eb | 2855 | error = -EIO; |
d18e9008 MS |
2856 | goto out; |
2857 | } | |
30d90494 | 2858 | if (file->f_path.dentry) { |
d18e9008 | 2859 | dput(dentry); |
30d90494 | 2860 | dentry = file->f_path.dentry; |
d18e9008 | 2861 | } |
03da633a AV |
2862 | if (*opened & FILE_CREATED) |
2863 | fsnotify_create(dir, dentry); | |
2864 | if (!dentry->d_inode) { | |
2865 | WARN_ON(*opened & FILE_CREATED); | |
2866 | if (create_error) { | |
2867 | error = create_error; | |
2868 | goto out; | |
2869 | } | |
2870 | } else { | |
2871 | if (excl && !(*opened & FILE_CREATED)) { | |
2872 | error = -EEXIST; | |
2873 | goto out; | |
2874 | } | |
62b2ce96 | 2875 | } |
d18e9008 MS |
2876 | goto looked_up; |
2877 | } | |
2878 | ||
2879 | /* | |
2880 | * We didn't have the inode before the open, so check open permission | |
2881 | * here. | |
2882 | */ | |
03da633a AV |
2883 | acc_mode = op->acc_mode; |
2884 | if (*opened & FILE_CREATED) { | |
2885 | WARN_ON(!(open_flag & O_CREAT)); | |
2886 | fsnotify_create(dir, dentry); | |
2887 | acc_mode = MAY_OPEN; | |
2888 | } | |
2675a4eb AV |
2889 | error = may_open(&file->f_path, acc_mode, open_flag); |
2890 | if (error) | |
2891 | fput(file); | |
d18e9008 MS |
2892 | |
2893 | out: | |
2894 | dput(dentry); | |
2675a4eb | 2895 | return error; |
d18e9008 | 2896 | |
d18e9008 MS |
2897 | no_open: |
2898 | if (need_lookup) { | |
72bd866a | 2899 | dentry = lookup_real(dir, dentry, nd->flags); |
d18e9008 | 2900 | if (IS_ERR(dentry)) |
2675a4eb | 2901 | return PTR_ERR(dentry); |
d18e9008 MS |
2902 | |
2903 | if (create_error) { | |
2904 | int open_flag = op->open_flag; | |
2905 | ||
2675a4eb | 2906 | error = create_error; |
d18e9008 MS |
2907 | if ((open_flag & O_EXCL)) { |
2908 | if (!dentry->d_inode) | |
2909 | goto out; | |
2910 | } else if (!dentry->d_inode) { | |
2911 | goto out; | |
2912 | } else if ((open_flag & O_TRUNC) && | |
e36cb0b8 | 2913 | d_is_reg(dentry)) { |
d18e9008 MS |
2914 | goto out; |
2915 | } | |
2916 | /* will fail later, go on to get the right error */ | |
2917 | } | |
2918 | } | |
2919 | looked_up: | |
2920 | path->dentry = dentry; | |
2921 | path->mnt = nd->path.mnt; | |
2675a4eb | 2922 | return 1; |
d18e9008 MS |
2923 | } |
2924 | ||
d58ffd35 | 2925 | /* |
1acf0af9 | 2926 | * Look up and maybe create and open the last component. |
d58ffd35 MS |
2927 | * |
2928 | * Must be called with i_mutex held on parent. | |
2929 | * | |
1acf0af9 DH |
2930 | * Returns 0 if the file was successfully atomically created (if necessary) and |
2931 | * opened. In this case the file will be returned attached to @file. | |
2932 | * | |
2933 | * Returns 1 if the file was not completely opened at this time, though lookups | |
2934 | * and creations will have been performed and the dentry returned in @path will | |
2935 | * be positive upon return if O_CREAT was specified. If O_CREAT wasn't | |
2936 | * specified then a negative dentry may be returned. | |
2937 | * | |
2938 | * An error code is returned otherwise. | |
2939 | * | |
2940 | * FILE_CREATE will be set in @*opened if the dentry was created and will be | |
2941 | * cleared otherwise prior to returning. | |
d58ffd35 | 2942 | */ |
2675a4eb AV |
2943 | static int lookup_open(struct nameidata *nd, struct path *path, |
2944 | struct file *file, | |
2945 | const struct open_flags *op, | |
64894cf8 | 2946 | bool got_write, int *opened) |
d58ffd35 MS |
2947 | { |
2948 | struct dentry *dir = nd->path.dentry; | |
54ef4872 | 2949 | struct inode *dir_inode = dir->d_inode; |
d58ffd35 MS |
2950 | struct dentry *dentry; |
2951 | int error; | |
54ef4872 | 2952 | bool need_lookup; |
d58ffd35 | 2953 | |
47237687 | 2954 | *opened &= ~FILE_CREATED; |
201f956e | 2955 | dentry = lookup_dcache(&nd->last, dir, nd->flags, &need_lookup); |
d58ffd35 | 2956 | if (IS_ERR(dentry)) |
2675a4eb | 2957 | return PTR_ERR(dentry); |
d58ffd35 | 2958 | |
d18e9008 MS |
2959 | /* Cached positive dentry: will open in f_op->open */ |
2960 | if (!need_lookup && dentry->d_inode) | |
2961 | goto out_no_open; | |
2962 | ||
2963 | if ((nd->flags & LOOKUP_OPEN) && dir_inode->i_op->atomic_open) { | |
64894cf8 | 2964 | return atomic_open(nd, dentry, path, file, op, got_write, |
47237687 | 2965 | need_lookup, opened); |
d18e9008 MS |
2966 | } |
2967 | ||
54ef4872 MS |
2968 | if (need_lookup) { |
2969 | BUG_ON(dentry->d_inode); | |
2970 | ||
72bd866a | 2971 | dentry = lookup_real(dir_inode, dentry, nd->flags); |
54ef4872 | 2972 | if (IS_ERR(dentry)) |
2675a4eb | 2973 | return PTR_ERR(dentry); |
54ef4872 MS |
2974 | } |
2975 | ||
d58ffd35 MS |
2976 | /* Negative dentry, just create the file */ |
2977 | if (!dentry->d_inode && (op->open_flag & O_CREAT)) { | |
2978 | umode_t mode = op->mode; | |
2979 | if (!IS_POSIXACL(dir->d_inode)) | |
2980 | mode &= ~current_umask(); | |
2981 | /* | |
2982 | * This write is needed to ensure that a | |
2983 | * rw->ro transition does not occur between | |
2984 | * the time when the file is created and when | |
2985 | * a permanent write count is taken through | |
015c3bbc | 2986 | * the 'struct file' in finish_open(). |
d58ffd35 | 2987 | */ |
64894cf8 AV |
2988 | if (!got_write) { |
2989 | error = -EROFS; | |
d58ffd35 | 2990 | goto out_dput; |
64894cf8 | 2991 | } |
47237687 | 2992 | *opened |= FILE_CREATED; |
d58ffd35 MS |
2993 | error = security_path_mknod(&nd->path, dentry, mode, 0); |
2994 | if (error) | |
2995 | goto out_dput; | |
312b63fb AV |
2996 | error = vfs_create(dir->d_inode, dentry, mode, |
2997 | nd->flags & LOOKUP_EXCL); | |
d58ffd35 MS |
2998 | if (error) |
2999 | goto out_dput; | |
3000 | } | |
d18e9008 | 3001 | out_no_open: |
d58ffd35 MS |
3002 | path->dentry = dentry; |
3003 | path->mnt = nd->path.mnt; | |
2675a4eb | 3004 | return 1; |
d58ffd35 MS |
3005 | |
3006 | out_dput: | |
3007 | dput(dentry); | |
2675a4eb | 3008 | return error; |
d58ffd35 MS |
3009 | } |
3010 | ||
31e6b01f | 3011 | /* |
fe2d35ff | 3012 | * Handle the last step of open() |
31e6b01f | 3013 | */ |
896475d5 | 3014 | static int do_last(struct nameidata *nd, |
2675a4eb | 3015 | struct file *file, const struct open_flags *op, |
76ae2a5a | 3016 | int *opened) |
fb1cc555 | 3017 | { |
a1e28038 | 3018 | struct dentry *dir = nd->path.dentry; |
ca344a89 | 3019 | int open_flag = op->open_flag; |
77d660a8 | 3020 | bool will_truncate = (open_flag & O_TRUNC) != 0; |
64894cf8 | 3021 | bool got_write = false; |
bcda7652 | 3022 | int acc_mode = op->acc_mode; |
254cf582 | 3023 | unsigned seq; |
a1eb3315 | 3024 | struct inode *inode; |
16b1c1cd | 3025 | struct path save_parent = { .dentry = NULL, .mnt = NULL }; |
896475d5 | 3026 | struct path path; |
16b1c1cd | 3027 | bool retried = false; |
16c2cd71 | 3028 | int error; |
1f36f774 | 3029 | |
c3e380b0 AV |
3030 | nd->flags &= ~LOOKUP_PARENT; |
3031 | nd->flags |= op->intent; | |
3032 | ||
bc77daa7 | 3033 | if (nd->last_type != LAST_NORM) { |
fe2d35ff | 3034 | error = handle_dots(nd, nd->last_type); |
deb106c6 | 3035 | if (unlikely(error)) |
2675a4eb | 3036 | return error; |
e83db167 | 3037 | goto finish_open; |
1f36f774 | 3038 | } |
67ee3ad2 | 3039 | |
ca344a89 | 3040 | if (!(open_flag & O_CREAT)) { |
fe2d35ff AV |
3041 | if (nd->last.name[nd->last.len]) |
3042 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; | |
3043 | /* we _can_ be in RCU mode here */ | |
254cf582 | 3044 | error = lookup_fast(nd, &path, &inode, &seq); |
71574865 MS |
3045 | if (likely(!error)) |
3046 | goto finish_lookup; | |
3047 | ||
3048 | if (error < 0) | |
deb106c6 | 3049 | return error; |
71574865 MS |
3050 | |
3051 | BUG_ON(nd->inode != dir->d_inode); | |
b6183df7 MS |
3052 | } else { |
3053 | /* create side of things */ | |
3054 | /* | |
3055 | * This will *only* deal with leaving RCU mode - LOOKUP_JUMPED | |
3056 | * has been cleared when we got to the last component we are | |
3057 | * about to look up | |
3058 | */ | |
3059 | error = complete_walk(nd); | |
e8bb73df | 3060 | if (error) |
2675a4eb | 3061 | return error; |
fe2d35ff | 3062 | |
76ae2a5a | 3063 | audit_inode(nd->name, dir, LOOKUP_PARENT); |
b6183df7 | 3064 | /* trailing slashes? */ |
deb106c6 AV |
3065 | if (unlikely(nd->last.name[nd->last.len])) |
3066 | return -EISDIR; | |
b6183df7 | 3067 | } |
a2c36b45 | 3068 | |
16b1c1cd | 3069 | retry_lookup: |
64894cf8 AV |
3070 | if (op->open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { |
3071 | error = mnt_want_write(nd->path.mnt); | |
3072 | if (!error) | |
3073 | got_write = true; | |
3074 | /* | |
3075 | * do _not_ fail yet - we might not need that or fail with | |
3076 | * a different error; let lookup_open() decide; we'll be | |
3077 | * dropping this one anyway. | |
3078 | */ | |
3079 | } | |
a1e28038 | 3080 | mutex_lock(&dir->d_inode->i_mutex); |
896475d5 | 3081 | error = lookup_open(nd, &path, file, op, got_write, opened); |
d58ffd35 | 3082 | mutex_unlock(&dir->d_inode->i_mutex); |
a1e28038 | 3083 | |
2675a4eb AV |
3084 | if (error <= 0) { |
3085 | if (error) | |
d18e9008 MS |
3086 | goto out; |
3087 | ||
47237687 | 3088 | if ((*opened & FILE_CREATED) || |
496ad9aa | 3089 | !S_ISREG(file_inode(file)->i_mode)) |
77d660a8 | 3090 | will_truncate = false; |
d18e9008 | 3091 | |
76ae2a5a | 3092 | audit_inode(nd->name, file->f_path.dentry, 0); |
d18e9008 MS |
3093 | goto opened; |
3094 | } | |
fb1cc555 | 3095 | |
47237687 | 3096 | if (*opened & FILE_CREATED) { |
9b44f1b3 | 3097 | /* Don't check for write permission, don't truncate */ |
ca344a89 | 3098 | open_flag &= ~O_TRUNC; |
77d660a8 | 3099 | will_truncate = false; |
bcda7652 | 3100 | acc_mode = MAY_OPEN; |
896475d5 | 3101 | path_to_nameidata(&path, nd); |
e83db167 | 3102 | goto finish_open_created; |
fb1cc555 AV |
3103 | } |
3104 | ||
3105 | /* | |
3134f37e | 3106 | * create/update audit record if it already exists. |
fb1cc555 | 3107 | */ |
896475d5 | 3108 | if (d_is_positive(path.dentry)) |
76ae2a5a | 3109 | audit_inode(nd->name, path.dentry, 0); |
fb1cc555 | 3110 | |
d18e9008 MS |
3111 | /* |
3112 | * If atomic_open() acquired write access it is dropped now due to | |
3113 | * possible mount and symlink following (this might be optimized away if | |
3114 | * necessary...) | |
3115 | */ | |
64894cf8 | 3116 | if (got_write) { |
d18e9008 | 3117 | mnt_drop_write(nd->path.mnt); |
64894cf8 | 3118 | got_write = false; |
d18e9008 MS |
3119 | } |
3120 | ||
deb106c6 AV |
3121 | if (unlikely((open_flag & (O_EXCL | O_CREAT)) == (O_EXCL | O_CREAT))) { |
3122 | path_to_nameidata(&path, nd); | |
3123 | return -EEXIST; | |
3124 | } | |
fb1cc555 | 3125 | |
756daf26 | 3126 | error = follow_managed(&path, nd); |
deb106c6 AV |
3127 | if (unlikely(error < 0)) |
3128 | return error; | |
a3fbbde7 | 3129 | |
decf3400 | 3130 | BUG_ON(nd->flags & LOOKUP_RCU); |
63afdfc7 | 3131 | inode = d_backing_inode(path.dentry); |
254cf582 | 3132 | seq = 0; /* out of RCU mode, so the value doesn't matter */ |
deb106c6 | 3133 | if (unlikely(d_is_negative(path.dentry))) { |
896475d5 | 3134 | path_to_nameidata(&path, nd); |
deb106c6 | 3135 | return -ENOENT; |
54c33e7f | 3136 | } |
766c4cbf | 3137 | finish_lookup: |
d63ff28f AV |
3138 | if (nd->depth) |
3139 | put_link(nd); | |
181548c0 AV |
3140 | error = should_follow_link(nd, &path, nd->flags & LOOKUP_FOLLOW, |
3141 | inode, seq); | |
deb106c6 | 3142 | if (unlikely(error)) |
d63ff28f | 3143 | return error; |
fb1cc555 | 3144 | |
896475d5 AV |
3145 | if (unlikely(d_is_symlink(path.dentry)) && !(open_flag & O_PATH)) { |
3146 | path_to_nameidata(&path, nd); | |
deb106c6 | 3147 | return -ELOOP; |
a5cfe2d5 AV |
3148 | } |
3149 | ||
896475d5 AV |
3150 | if ((nd->flags & LOOKUP_RCU) || nd->path.mnt != path.mnt) { |
3151 | path_to_nameidata(&path, nd); | |
16b1c1cd MS |
3152 | } else { |
3153 | save_parent.dentry = nd->path.dentry; | |
896475d5 AV |
3154 | save_parent.mnt = mntget(path.mnt); |
3155 | nd->path.dentry = path.dentry; | |
16b1c1cd MS |
3156 | |
3157 | } | |
decf3400 | 3158 | nd->inode = inode; |
254cf582 | 3159 | nd->seq = seq; |
a3fbbde7 | 3160 | /* Why this, you ask? _Now_ we might have grown LOOKUP_JUMPED... */ |
bc77daa7 | 3161 | finish_open: |
a3fbbde7 | 3162 | error = complete_walk(nd); |
16b1c1cd MS |
3163 | if (error) { |
3164 | path_put(&save_parent); | |
2675a4eb | 3165 | return error; |
16b1c1cd | 3166 | } |
76ae2a5a | 3167 | audit_inode(nd->name, nd->path.dentry, 0); |
fb1cc555 | 3168 | error = -EISDIR; |
44b1d530 | 3169 | if ((open_flag & O_CREAT) && d_is_dir(nd->path.dentry)) |
2675a4eb | 3170 | goto out; |
af2f5542 | 3171 | error = -ENOTDIR; |
44b1d530 | 3172 | if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry)) |
2675a4eb | 3173 | goto out; |
4bbcbd3b | 3174 | if (!d_is_reg(nd->path.dentry)) |
77d660a8 | 3175 | will_truncate = false; |
6c0d46c4 | 3176 | |
0f9d1a10 AV |
3177 | if (will_truncate) { |
3178 | error = mnt_want_write(nd->path.mnt); | |
3179 | if (error) | |
2675a4eb | 3180 | goto out; |
64894cf8 | 3181 | got_write = true; |
0f9d1a10 | 3182 | } |
e83db167 | 3183 | finish_open_created: |
bcda7652 | 3184 | error = may_open(&nd->path, acc_mode, open_flag); |
ca344a89 | 3185 | if (error) |
2675a4eb | 3186 | goto out; |
4aa7c634 MS |
3187 | |
3188 | BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */ | |
3189 | error = vfs_open(&nd->path, file, current_cred()); | |
3190 | if (!error) { | |
3191 | *opened |= FILE_OPENED; | |
3192 | } else { | |
30d90494 | 3193 | if (error == -EOPENSTALE) |
f60dc3db | 3194 | goto stale_open; |
015c3bbc | 3195 | goto out; |
f60dc3db | 3196 | } |
a8277b9b | 3197 | opened: |
2675a4eb | 3198 | error = open_check_o_direct(file); |
015c3bbc MS |
3199 | if (error) |
3200 | goto exit_fput; | |
3034a146 | 3201 | error = ima_file_check(file, op->acc_mode, *opened); |
aa4caadb MS |
3202 | if (error) |
3203 | goto exit_fput; | |
3204 | ||
3205 | if (will_truncate) { | |
2675a4eb | 3206 | error = handle_truncate(file); |
aa4caadb MS |
3207 | if (error) |
3208 | goto exit_fput; | |
0f9d1a10 | 3209 | } |
ca344a89 | 3210 | out: |
64894cf8 | 3211 | if (got_write) |
0f9d1a10 | 3212 | mnt_drop_write(nd->path.mnt); |
16b1c1cd | 3213 | path_put(&save_parent); |
2675a4eb | 3214 | return error; |
fb1cc555 | 3215 | |
015c3bbc | 3216 | exit_fput: |
2675a4eb AV |
3217 | fput(file); |
3218 | goto out; | |
015c3bbc | 3219 | |
f60dc3db MS |
3220 | stale_open: |
3221 | /* If no saved parent or already retried then can't retry */ | |
3222 | if (!save_parent.dentry || retried) | |
3223 | goto out; | |
3224 | ||
3225 | BUG_ON(save_parent.dentry != dir); | |
3226 | path_put(&nd->path); | |
3227 | nd->path = save_parent; | |
3228 | nd->inode = dir->d_inode; | |
3229 | save_parent.mnt = NULL; | |
3230 | save_parent.dentry = NULL; | |
64894cf8 | 3231 | if (got_write) { |
f60dc3db | 3232 | mnt_drop_write(nd->path.mnt); |
64894cf8 | 3233 | got_write = false; |
f60dc3db MS |
3234 | } |
3235 | retried = true; | |
3236 | goto retry_lookup; | |
fb1cc555 AV |
3237 | } |
3238 | ||
c8a53ee5 | 3239 | static int do_tmpfile(struct nameidata *nd, unsigned flags, |
60545d0d AV |
3240 | const struct open_flags *op, |
3241 | struct file *file, int *opened) | |
3242 | { | |
3243 | static const struct qstr name = QSTR_INIT("/", 1); | |
625b6d10 | 3244 | struct dentry *child; |
60545d0d | 3245 | struct inode *dir; |
625b6d10 | 3246 | struct path path; |
c8a53ee5 | 3247 | int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path); |
60545d0d AV |
3248 | if (unlikely(error)) |
3249 | return error; | |
625b6d10 | 3250 | error = mnt_want_write(path.mnt); |
60545d0d AV |
3251 | if (unlikely(error)) |
3252 | goto out; | |
625b6d10 | 3253 | dir = path.dentry->d_inode; |
60545d0d | 3254 | /* we want directory to be writable */ |
625b6d10 | 3255 | error = inode_permission(dir, MAY_WRITE | MAY_EXEC); |
60545d0d AV |
3256 | if (error) |
3257 | goto out2; | |
60545d0d AV |
3258 | if (!dir->i_op->tmpfile) { |
3259 | error = -EOPNOTSUPP; | |
3260 | goto out2; | |
3261 | } | |
625b6d10 | 3262 | child = d_alloc(path.dentry, &name); |
60545d0d AV |
3263 | if (unlikely(!child)) { |
3264 | error = -ENOMEM; | |
3265 | goto out2; | |
3266 | } | |
625b6d10 AV |
3267 | dput(path.dentry); |
3268 | path.dentry = child; | |
3269 | error = dir->i_op->tmpfile(dir, child, op->mode); | |
60545d0d AV |
3270 | if (error) |
3271 | goto out2; | |
c8a53ee5 | 3272 | audit_inode(nd->name, child, 0); |
69a91c23 | 3273 | /* Don't check for other permissions, the inode was just created */ |
625b6d10 | 3274 | error = may_open(&path, MAY_OPEN, op->open_flag); |
60545d0d AV |
3275 | if (error) |
3276 | goto out2; | |
625b6d10 AV |
3277 | file->f_path.mnt = path.mnt; |
3278 | error = finish_open(file, child, NULL, opened); | |
60545d0d AV |
3279 | if (error) |
3280 | goto out2; | |
3281 | error = open_check_o_direct(file); | |
f4e0c30c | 3282 | if (error) { |
60545d0d | 3283 | fput(file); |
f4e0c30c AV |
3284 | } else if (!(op->open_flag & O_EXCL)) { |
3285 | struct inode *inode = file_inode(file); | |
3286 | spin_lock(&inode->i_lock); | |
3287 | inode->i_state |= I_LINKABLE; | |
3288 | spin_unlock(&inode->i_lock); | |
3289 | } | |
60545d0d | 3290 | out2: |
625b6d10 | 3291 | mnt_drop_write(path.mnt); |
60545d0d | 3292 | out: |
625b6d10 | 3293 | path_put(&path); |
60545d0d AV |
3294 | return error; |
3295 | } | |
3296 | ||
c8a53ee5 AV |
3297 | static struct file *path_openat(struct nameidata *nd, |
3298 | const struct open_flags *op, unsigned flags) | |
1da177e4 | 3299 | { |
368ee9ba | 3300 | const char *s; |
30d90494 | 3301 | struct file *file; |
47237687 | 3302 | int opened = 0; |
13aab428 | 3303 | int error; |
31e6b01f | 3304 | |
30d90494 | 3305 | file = get_empty_filp(); |
1afc99be AV |
3306 | if (IS_ERR(file)) |
3307 | return file; | |
31e6b01f | 3308 | |
30d90494 | 3309 | file->f_flags = op->open_flag; |
31e6b01f | 3310 | |
bb458c64 | 3311 | if (unlikely(file->f_flags & __O_TMPFILE)) { |
c8a53ee5 | 3312 | error = do_tmpfile(nd, flags, op, file, &opened); |
f15133df | 3313 | goto out2; |
60545d0d AV |
3314 | } |
3315 | ||
c8a53ee5 | 3316 | s = path_init(nd, flags); |
368ee9ba AV |
3317 | if (IS_ERR(s)) { |
3318 | put_filp(file); | |
3319 | return ERR_CAST(s); | |
3320 | } | |
3bdba28b | 3321 | while (!(error = link_path_walk(s, nd)) && |
76ae2a5a | 3322 | (error = do_last(nd, file, op, &opened)) > 0) { |
73d049a4 | 3323 | nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); |
3bdba28b AV |
3324 | s = trailing_symlink(nd); |
3325 | if (IS_ERR(s)) { | |
3326 | error = PTR_ERR(s); | |
2675a4eb | 3327 | break; |
3bdba28b | 3328 | } |
806b681c | 3329 | } |
deb106c6 | 3330 | terminate_walk(nd); |
f15133df | 3331 | out2: |
2675a4eb AV |
3332 | if (!(opened & FILE_OPENED)) { |
3333 | BUG_ON(!error); | |
30d90494 | 3334 | put_filp(file); |
16b1c1cd | 3335 | } |
2675a4eb AV |
3336 | if (unlikely(error)) { |
3337 | if (error == -EOPENSTALE) { | |
3338 | if (flags & LOOKUP_RCU) | |
3339 | error = -ECHILD; | |
3340 | else | |
3341 | error = -ESTALE; | |
3342 | } | |
3343 | file = ERR_PTR(error); | |
3344 | } | |
3345 | return file; | |
1da177e4 LT |
3346 | } |
3347 | ||
669abf4e | 3348 | struct file *do_filp_open(int dfd, struct filename *pathname, |
f9652e10 | 3349 | const struct open_flags *op) |
13aab428 | 3350 | { |
9883d185 | 3351 | struct nameidata nd; |
f9652e10 | 3352 | int flags = op->lookup_flags; |
13aab428 AV |
3353 | struct file *filp; |
3354 | ||
9883d185 | 3355 | set_nameidata(&nd, dfd, pathname); |
c8a53ee5 | 3356 | filp = path_openat(&nd, op, flags | LOOKUP_RCU); |
13aab428 | 3357 | if (unlikely(filp == ERR_PTR(-ECHILD))) |
c8a53ee5 | 3358 | filp = path_openat(&nd, op, flags); |
13aab428 | 3359 | if (unlikely(filp == ERR_PTR(-ESTALE))) |
c8a53ee5 | 3360 | filp = path_openat(&nd, op, flags | LOOKUP_REVAL); |
9883d185 | 3361 | restore_nameidata(); |
13aab428 AV |
3362 | return filp; |
3363 | } | |
3364 | ||
73d049a4 | 3365 | struct file *do_file_open_root(struct dentry *dentry, struct vfsmount *mnt, |
f9652e10 | 3366 | const char *name, const struct open_flags *op) |
73d049a4 | 3367 | { |
9883d185 | 3368 | struct nameidata nd; |
73d049a4 | 3369 | struct file *file; |
51689104 | 3370 | struct filename *filename; |
f9652e10 | 3371 | int flags = op->lookup_flags | LOOKUP_ROOT; |
73d049a4 AV |
3372 | |
3373 | nd.root.mnt = mnt; | |
3374 | nd.root.dentry = dentry; | |
3375 | ||
b18825a7 | 3376 | if (d_is_symlink(dentry) && op->intent & LOOKUP_OPEN) |
73d049a4 AV |
3377 | return ERR_PTR(-ELOOP); |
3378 | ||
51689104 | 3379 | filename = getname_kernel(name); |
a1c83681 | 3380 | if (IS_ERR(filename)) |
51689104 PM |
3381 | return ERR_CAST(filename); |
3382 | ||
9883d185 | 3383 | set_nameidata(&nd, -1, filename); |
c8a53ee5 | 3384 | file = path_openat(&nd, op, flags | LOOKUP_RCU); |
73d049a4 | 3385 | if (unlikely(file == ERR_PTR(-ECHILD))) |
c8a53ee5 | 3386 | file = path_openat(&nd, op, flags); |
73d049a4 | 3387 | if (unlikely(file == ERR_PTR(-ESTALE))) |
c8a53ee5 | 3388 | file = path_openat(&nd, op, flags | LOOKUP_REVAL); |
9883d185 | 3389 | restore_nameidata(); |
51689104 | 3390 | putname(filename); |
73d049a4 AV |
3391 | return file; |
3392 | } | |
3393 | ||
fa14a0b8 | 3394 | static struct dentry *filename_create(int dfd, struct filename *name, |
1ac12b4b | 3395 | struct path *path, unsigned int lookup_flags) |
1da177e4 | 3396 | { |
c663e5d8 | 3397 | struct dentry *dentry = ERR_PTR(-EEXIST); |
391172c4 AV |
3398 | struct qstr last; |
3399 | int type; | |
c30dabfe | 3400 | int err2; |
1ac12b4b JL |
3401 | int error; |
3402 | bool is_dir = (lookup_flags & LOOKUP_DIRECTORY); | |
3403 | ||
3404 | /* | |
3405 | * Note that only LOOKUP_REVAL and LOOKUP_DIRECTORY matter here. Any | |
3406 | * other flags passed in are ignored! | |
3407 | */ | |
3408 | lookup_flags &= LOOKUP_REVAL; | |
3409 | ||
5c31b6ce AV |
3410 | name = filename_parentat(dfd, name, lookup_flags, path, &last, &type); |
3411 | if (IS_ERR(name)) | |
3412 | return ERR_CAST(name); | |
1da177e4 | 3413 | |
c663e5d8 CH |
3414 | /* |
3415 | * Yucky last component or no last component at all? | |
3416 | * (foo/., foo/.., /////) | |
3417 | */ | |
5c31b6ce | 3418 | if (unlikely(type != LAST_NORM)) |
ed75e95d | 3419 | goto out; |
c663e5d8 | 3420 | |
c30dabfe | 3421 | /* don't fail immediately if it's r/o, at least try to report other errors */ |
391172c4 | 3422 | err2 = mnt_want_write(path->mnt); |
c663e5d8 CH |
3423 | /* |
3424 | * Do the final lookup. | |
3425 | */ | |
391172c4 AV |
3426 | lookup_flags |= LOOKUP_CREATE | LOOKUP_EXCL; |
3427 | mutex_lock_nested(&path->dentry->d_inode->i_mutex, I_MUTEX_PARENT); | |
3428 | dentry = __lookup_hash(&last, path->dentry, lookup_flags); | |
1da177e4 | 3429 | if (IS_ERR(dentry)) |
a8104a9f | 3430 | goto unlock; |
c663e5d8 | 3431 | |
a8104a9f | 3432 | error = -EEXIST; |
b18825a7 | 3433 | if (d_is_positive(dentry)) |
a8104a9f | 3434 | goto fail; |
b18825a7 | 3435 | |
c663e5d8 CH |
3436 | /* |
3437 | * Special case - lookup gave negative, but... we had foo/bar/ | |
3438 | * From the vfs_mknod() POV we just have a negative dentry - | |
3439 | * all is fine. Let's be bastards - you had / on the end, you've | |
3440 | * been asking for (non-existent) directory. -ENOENT for you. | |
3441 | */ | |
391172c4 | 3442 | if (unlikely(!is_dir && last.name[last.len])) { |
a8104a9f | 3443 | error = -ENOENT; |
ed75e95d | 3444 | goto fail; |
e9baf6e5 | 3445 | } |
c30dabfe JK |
3446 | if (unlikely(err2)) { |
3447 | error = err2; | |
a8104a9f | 3448 | goto fail; |
c30dabfe | 3449 | } |
181c37b6 | 3450 | putname(name); |
1da177e4 | 3451 | return dentry; |
1da177e4 | 3452 | fail: |
a8104a9f AV |
3453 | dput(dentry); |
3454 | dentry = ERR_PTR(error); | |
3455 | unlock: | |
391172c4 | 3456 | mutex_unlock(&path->dentry->d_inode->i_mutex); |
c30dabfe | 3457 | if (!err2) |
391172c4 | 3458 | mnt_drop_write(path->mnt); |
ed75e95d | 3459 | out: |
391172c4 | 3460 | path_put(path); |
181c37b6 | 3461 | putname(name); |
1da177e4 LT |
3462 | return dentry; |
3463 | } | |
fa14a0b8 AV |
3464 | |
3465 | struct dentry *kern_path_create(int dfd, const char *pathname, | |
3466 | struct path *path, unsigned int lookup_flags) | |
3467 | { | |
181c37b6 AV |
3468 | return filename_create(dfd, getname_kernel(pathname), |
3469 | path, lookup_flags); | |
fa14a0b8 | 3470 | } |
dae6ad8f AV |
3471 | EXPORT_SYMBOL(kern_path_create); |
3472 | ||
921a1650 AV |
3473 | void done_path_create(struct path *path, struct dentry *dentry) |
3474 | { | |
3475 | dput(dentry); | |
3476 | mutex_unlock(&path->dentry->d_inode->i_mutex); | |
a8104a9f | 3477 | mnt_drop_write(path->mnt); |
921a1650 AV |
3478 | path_put(path); |
3479 | } | |
3480 | EXPORT_SYMBOL(done_path_create); | |
3481 | ||
520ae687 | 3482 | inline struct dentry *user_path_create(int dfd, const char __user *pathname, |
1ac12b4b | 3483 | struct path *path, unsigned int lookup_flags) |
dae6ad8f | 3484 | { |
181c37b6 | 3485 | return filename_create(dfd, getname(pathname), path, lookup_flags); |
dae6ad8f AV |
3486 | } |
3487 | EXPORT_SYMBOL(user_path_create); | |
3488 | ||
1a67aafb | 3489 | int vfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev) |
1da177e4 | 3490 | { |
a95164d9 | 3491 | int error = may_create(dir, dentry); |
1da177e4 LT |
3492 | |
3493 | if (error) | |
3494 | return error; | |
3495 | ||
975d6b39 | 3496 | if ((S_ISCHR(mode) || S_ISBLK(mode)) && !capable(CAP_MKNOD)) |
1da177e4 LT |
3497 | return -EPERM; |
3498 | ||
acfa4380 | 3499 | if (!dir->i_op->mknod) |
1da177e4 LT |
3500 | return -EPERM; |
3501 | ||
08ce5f16 SH |
3502 | error = devcgroup_inode_mknod(mode, dev); |
3503 | if (error) | |
3504 | return error; | |
3505 | ||
1da177e4 LT |
3506 | error = security_inode_mknod(dir, dentry, mode, dev); |
3507 | if (error) | |
3508 | return error; | |
3509 | ||
1da177e4 | 3510 | error = dir->i_op->mknod(dir, dentry, mode, dev); |
a74574aa | 3511 | if (!error) |
f38aa942 | 3512 | fsnotify_create(dir, dentry); |
1da177e4 LT |
3513 | return error; |
3514 | } | |
4d359507 | 3515 | EXPORT_SYMBOL(vfs_mknod); |
1da177e4 | 3516 | |
f69aac00 | 3517 | static int may_mknod(umode_t mode) |
463c3197 DH |
3518 | { |
3519 | switch (mode & S_IFMT) { | |
3520 | case S_IFREG: | |
3521 | case S_IFCHR: | |
3522 | case S_IFBLK: | |
3523 | case S_IFIFO: | |
3524 | case S_IFSOCK: | |
3525 | case 0: /* zero mode translates to S_IFREG */ | |
3526 | return 0; | |
3527 | case S_IFDIR: | |
3528 | return -EPERM; | |
3529 | default: | |
3530 | return -EINVAL; | |
3531 | } | |
3532 | } | |
3533 | ||
8208a22b | 3534 | SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, |
2e4d0924 | 3535 | unsigned, dev) |
1da177e4 | 3536 | { |
2ad94ae6 | 3537 | struct dentry *dentry; |
dae6ad8f AV |
3538 | struct path path; |
3539 | int error; | |
972567f1 | 3540 | unsigned int lookup_flags = 0; |
1da177e4 | 3541 | |
8e4bfca1 AV |
3542 | error = may_mknod(mode); |
3543 | if (error) | |
3544 | return error; | |
972567f1 JL |
3545 | retry: |
3546 | dentry = user_path_create(dfd, filename, &path, lookup_flags); | |
dae6ad8f AV |
3547 | if (IS_ERR(dentry)) |
3548 | return PTR_ERR(dentry); | |
2ad94ae6 | 3549 | |
dae6ad8f | 3550 | if (!IS_POSIXACL(path.dentry->d_inode)) |
ce3b0f8d | 3551 | mode &= ~current_umask(); |
dae6ad8f | 3552 | error = security_path_mknod(&path, dentry, mode, dev); |
be6d3e56 | 3553 | if (error) |
a8104a9f | 3554 | goto out; |
463c3197 | 3555 | switch (mode & S_IFMT) { |
1da177e4 | 3556 | case 0: case S_IFREG: |
312b63fb | 3557 | error = vfs_create(path.dentry->d_inode,dentry,mode,true); |
1da177e4 LT |
3558 | break; |
3559 | case S_IFCHR: case S_IFBLK: | |
dae6ad8f | 3560 | error = vfs_mknod(path.dentry->d_inode,dentry,mode, |
1da177e4 LT |
3561 | new_decode_dev(dev)); |
3562 | break; | |
3563 | case S_IFIFO: case S_IFSOCK: | |
dae6ad8f | 3564 | error = vfs_mknod(path.dentry->d_inode,dentry,mode,0); |
1da177e4 | 3565 | break; |
1da177e4 | 3566 | } |
a8104a9f | 3567 | out: |
921a1650 | 3568 | done_path_create(&path, dentry); |
972567f1 JL |
3569 | if (retry_estale(error, lookup_flags)) { |
3570 | lookup_flags |= LOOKUP_REVAL; | |
3571 | goto retry; | |
3572 | } | |
1da177e4 LT |
3573 | return error; |
3574 | } | |
3575 | ||
8208a22b | 3576 | SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) |
5590ff0d UD |
3577 | { |
3578 | return sys_mknodat(AT_FDCWD, filename, mode, dev); | |
3579 | } | |
3580 | ||
18bb1db3 | 3581 | int vfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
1da177e4 | 3582 | { |
a95164d9 | 3583 | int error = may_create(dir, dentry); |
8de52778 | 3584 | unsigned max_links = dir->i_sb->s_max_links; |
1da177e4 LT |
3585 | |
3586 | if (error) | |
3587 | return error; | |
3588 | ||
acfa4380 | 3589 | if (!dir->i_op->mkdir) |
1da177e4 LT |
3590 | return -EPERM; |
3591 | ||
3592 | mode &= (S_IRWXUGO|S_ISVTX); | |
3593 | error = security_inode_mkdir(dir, dentry, mode); | |
3594 | if (error) | |
3595 | return error; | |
3596 | ||
8de52778 AV |
3597 | if (max_links && dir->i_nlink >= max_links) |
3598 | return -EMLINK; | |
3599 | ||
1da177e4 | 3600 | error = dir->i_op->mkdir(dir, dentry, mode); |
a74574aa | 3601 | if (!error) |
f38aa942 | 3602 | fsnotify_mkdir(dir, dentry); |
1da177e4 LT |
3603 | return error; |
3604 | } | |
4d359507 | 3605 | EXPORT_SYMBOL(vfs_mkdir); |
1da177e4 | 3606 | |
a218d0fd | 3607 | SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) |
1da177e4 | 3608 | { |
6902d925 | 3609 | struct dentry *dentry; |
dae6ad8f AV |
3610 | struct path path; |
3611 | int error; | |
b76d8b82 | 3612 | unsigned int lookup_flags = LOOKUP_DIRECTORY; |
1da177e4 | 3613 | |
b76d8b82 JL |
3614 | retry: |
3615 | dentry = user_path_create(dfd, pathname, &path, lookup_flags); | |
6902d925 | 3616 | if (IS_ERR(dentry)) |
dae6ad8f | 3617 | return PTR_ERR(dentry); |
1da177e4 | 3618 | |
dae6ad8f | 3619 | if (!IS_POSIXACL(path.dentry->d_inode)) |
ce3b0f8d | 3620 | mode &= ~current_umask(); |
dae6ad8f | 3621 | error = security_path_mkdir(&path, dentry, mode); |
a8104a9f AV |
3622 | if (!error) |
3623 | error = vfs_mkdir(path.dentry->d_inode, dentry, mode); | |
921a1650 | 3624 | done_path_create(&path, dentry); |
b76d8b82 JL |
3625 | if (retry_estale(error, lookup_flags)) { |
3626 | lookup_flags |= LOOKUP_REVAL; | |
3627 | goto retry; | |
3628 | } | |
1da177e4 LT |
3629 | return error; |
3630 | } | |
3631 | ||
a218d0fd | 3632 | SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) |
5590ff0d UD |
3633 | { |
3634 | return sys_mkdirat(AT_FDCWD, pathname, mode); | |
3635 | } | |
3636 | ||
1da177e4 | 3637 | /* |
a71905f0 | 3638 | * The dentry_unhash() helper will try to drop the dentry early: we |
c0d02594 | 3639 | * should have a usage count of 1 if we're the only user of this |
a71905f0 SW |
3640 | * dentry, and if that is true (possibly after pruning the dcache), |
3641 | * then we drop the dentry now. | |
1da177e4 LT |
3642 | * |
3643 | * A low-level filesystem can, if it choses, legally | |
3644 | * do a | |
3645 | * | |
3646 | * if (!d_unhashed(dentry)) | |
3647 | * return -EBUSY; | |
3648 | * | |
3649 | * if it cannot handle the case of removing a directory | |
3650 | * that is still in use by something else.. | |
3651 | */ | |
3652 | void dentry_unhash(struct dentry *dentry) | |
3653 | { | |
dc168427 | 3654 | shrink_dcache_parent(dentry); |
1da177e4 | 3655 | spin_lock(&dentry->d_lock); |
98474236 | 3656 | if (dentry->d_lockref.count == 1) |
1da177e4 LT |
3657 | __d_drop(dentry); |
3658 | spin_unlock(&dentry->d_lock); | |
1da177e4 | 3659 | } |
4d359507 | 3660 | EXPORT_SYMBOL(dentry_unhash); |
1da177e4 LT |
3661 | |
3662 | int vfs_rmdir(struct inode *dir, struct dentry *dentry) | |
3663 | { | |
3664 | int error = may_delete(dir, dentry, 1); | |
3665 | ||
3666 | if (error) | |
3667 | return error; | |
3668 | ||
acfa4380 | 3669 | if (!dir->i_op->rmdir) |
1da177e4 LT |
3670 | return -EPERM; |
3671 | ||
1d2ef590 | 3672 | dget(dentry); |
1b1dcc1b | 3673 | mutex_lock(&dentry->d_inode->i_mutex); |
912dbc15 SW |
3674 | |
3675 | error = -EBUSY; | |
7af1364f | 3676 | if (is_local_mountpoint(dentry)) |
912dbc15 SW |
3677 | goto out; |
3678 | ||
3679 | error = security_inode_rmdir(dir, dentry); | |
3680 | if (error) | |
3681 | goto out; | |
3682 | ||
3cebde24 | 3683 | shrink_dcache_parent(dentry); |
912dbc15 SW |
3684 | error = dir->i_op->rmdir(dir, dentry); |
3685 | if (error) | |
3686 | goto out; | |
3687 | ||
3688 | dentry->d_inode->i_flags |= S_DEAD; | |
3689 | dont_mount(dentry); | |
8ed936b5 | 3690 | detach_mounts(dentry); |
912dbc15 SW |
3691 | |
3692 | out: | |
1b1dcc1b | 3693 | mutex_unlock(&dentry->d_inode->i_mutex); |
1d2ef590 | 3694 | dput(dentry); |
912dbc15 | 3695 | if (!error) |
1da177e4 | 3696 | d_delete(dentry); |
1da177e4 LT |
3697 | return error; |
3698 | } | |
4d359507 | 3699 | EXPORT_SYMBOL(vfs_rmdir); |
1da177e4 | 3700 | |
5590ff0d | 3701 | static long do_rmdir(int dfd, const char __user *pathname) |
1da177e4 LT |
3702 | { |
3703 | int error = 0; | |
91a27b2a | 3704 | struct filename *name; |
1da177e4 | 3705 | struct dentry *dentry; |
f5beed75 AV |
3706 | struct path path; |
3707 | struct qstr last; | |
3708 | int type; | |
c6ee9206 JL |
3709 | unsigned int lookup_flags = 0; |
3710 | retry: | |
f5beed75 AV |
3711 | name = user_path_parent(dfd, pathname, |
3712 | &path, &last, &type, lookup_flags); | |
91a27b2a JL |
3713 | if (IS_ERR(name)) |
3714 | return PTR_ERR(name); | |
1da177e4 | 3715 | |
f5beed75 | 3716 | switch (type) { |
0612d9fb OH |
3717 | case LAST_DOTDOT: |
3718 | error = -ENOTEMPTY; | |
3719 | goto exit1; | |
3720 | case LAST_DOT: | |
3721 | error = -EINVAL; | |
3722 | goto exit1; | |
3723 | case LAST_ROOT: | |
3724 | error = -EBUSY; | |
3725 | goto exit1; | |
1da177e4 | 3726 | } |
0612d9fb | 3727 | |
f5beed75 | 3728 | error = mnt_want_write(path.mnt); |
c30dabfe JK |
3729 | if (error) |
3730 | goto exit1; | |
0612d9fb | 3731 | |
f5beed75 AV |
3732 | mutex_lock_nested(&path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
3733 | dentry = __lookup_hash(&last, path.dentry, lookup_flags); | |
1da177e4 | 3734 | error = PTR_ERR(dentry); |
6902d925 DH |
3735 | if (IS_ERR(dentry)) |
3736 | goto exit2; | |
e6bc45d6 TT |
3737 | if (!dentry->d_inode) { |
3738 | error = -ENOENT; | |
3739 | goto exit3; | |
3740 | } | |
f5beed75 | 3741 | error = security_path_rmdir(&path, dentry); |
be6d3e56 | 3742 | if (error) |
c30dabfe | 3743 | goto exit3; |
f5beed75 | 3744 | error = vfs_rmdir(path.dentry->d_inode, dentry); |
0622753b | 3745 | exit3: |
6902d925 DH |
3746 | dput(dentry); |
3747 | exit2: | |
f5beed75 AV |
3748 | mutex_unlock(&path.dentry->d_inode->i_mutex); |
3749 | mnt_drop_write(path.mnt); | |
1da177e4 | 3750 | exit1: |
f5beed75 | 3751 | path_put(&path); |
1da177e4 | 3752 | putname(name); |
c6ee9206 JL |
3753 | if (retry_estale(error, lookup_flags)) { |
3754 | lookup_flags |= LOOKUP_REVAL; | |
3755 | goto retry; | |
3756 | } | |
1da177e4 LT |
3757 | return error; |
3758 | } | |
3759 | ||
3cdad428 | 3760 | SYSCALL_DEFINE1(rmdir, const char __user *, pathname) |
5590ff0d UD |
3761 | { |
3762 | return do_rmdir(AT_FDCWD, pathname); | |
3763 | } | |
3764 | ||
b21996e3 BF |
3765 | /** |
3766 | * vfs_unlink - unlink a filesystem object | |
3767 | * @dir: parent directory | |
3768 | * @dentry: victim | |
3769 | * @delegated_inode: returns victim inode, if the inode is delegated. | |
3770 | * | |
3771 | * The caller must hold dir->i_mutex. | |
3772 | * | |
3773 | * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and | |
3774 | * return a reference to the inode in delegated_inode. The caller | |
3775 | * should then break the delegation on that inode and retry. Because | |
3776 | * breaking a delegation may take a long time, the caller should drop | |
3777 | * dir->i_mutex before doing so. | |
3778 | * | |
3779 | * Alternatively, a caller may pass NULL for delegated_inode. This may | |
3780 | * be appropriate for callers that expect the underlying filesystem not | |
3781 | * to be NFS exported. | |
3782 | */ | |
3783 | int vfs_unlink(struct inode *dir, struct dentry *dentry, struct inode **delegated_inode) | |
1da177e4 | 3784 | { |
9accbb97 | 3785 | struct inode *target = dentry->d_inode; |
1da177e4 LT |
3786 | int error = may_delete(dir, dentry, 0); |
3787 | ||
3788 | if (error) | |
3789 | return error; | |
3790 | ||
acfa4380 | 3791 | if (!dir->i_op->unlink) |
1da177e4 LT |
3792 | return -EPERM; |
3793 | ||
9accbb97 | 3794 | mutex_lock(&target->i_mutex); |
8ed936b5 | 3795 | if (is_local_mountpoint(dentry)) |
1da177e4 LT |
3796 | error = -EBUSY; |
3797 | else { | |
3798 | error = security_inode_unlink(dir, dentry); | |
bec1052e | 3799 | if (!error) { |
5a14696c BF |
3800 | error = try_break_deleg(target, delegated_inode); |
3801 | if (error) | |
b21996e3 | 3802 | goto out; |
1da177e4 | 3803 | error = dir->i_op->unlink(dir, dentry); |
8ed936b5 | 3804 | if (!error) { |
d83c49f3 | 3805 | dont_mount(dentry); |
8ed936b5 EB |
3806 | detach_mounts(dentry); |
3807 | } | |
bec1052e | 3808 | } |
1da177e4 | 3809 | } |
b21996e3 | 3810 | out: |
9accbb97 | 3811 | mutex_unlock(&target->i_mutex); |
1da177e4 LT |
3812 | |
3813 | /* We don't d_delete() NFS sillyrenamed files--they still exist. */ | |
3814 | if (!error && !(dentry->d_flags & DCACHE_NFSFS_RENAMED)) { | |
9accbb97 | 3815 | fsnotify_link_count(target); |
e234f35c | 3816 | d_delete(dentry); |
1da177e4 | 3817 | } |
0eeca283 | 3818 | |
1da177e4 LT |
3819 | return error; |
3820 | } | |
4d359507 | 3821 | EXPORT_SYMBOL(vfs_unlink); |
1da177e4 LT |
3822 | |
3823 | /* | |
3824 | * Make sure that the actual truncation of the file will occur outside its | |
1b1dcc1b | 3825 | * directory's i_mutex. Truncate can take a long time if there is a lot of |
1da177e4 LT |
3826 | * writeout happening, and we don't want to prevent access to the directory |
3827 | * while waiting on the I/O. | |
3828 | */ | |
5590ff0d | 3829 | static long do_unlinkat(int dfd, const char __user *pathname) |
1da177e4 | 3830 | { |
2ad94ae6 | 3831 | int error; |
91a27b2a | 3832 | struct filename *name; |
1da177e4 | 3833 | struct dentry *dentry; |
f5beed75 AV |
3834 | struct path path; |
3835 | struct qstr last; | |
3836 | int type; | |
1da177e4 | 3837 | struct inode *inode = NULL; |
b21996e3 | 3838 | struct inode *delegated_inode = NULL; |
5d18f813 JL |
3839 | unsigned int lookup_flags = 0; |
3840 | retry: | |
f5beed75 AV |
3841 | name = user_path_parent(dfd, pathname, |
3842 | &path, &last, &type, lookup_flags); | |
91a27b2a JL |
3843 | if (IS_ERR(name)) |
3844 | return PTR_ERR(name); | |
2ad94ae6 | 3845 | |
1da177e4 | 3846 | error = -EISDIR; |
f5beed75 | 3847 | if (type != LAST_NORM) |
1da177e4 | 3848 | goto exit1; |
0612d9fb | 3849 | |
f5beed75 | 3850 | error = mnt_want_write(path.mnt); |
c30dabfe JK |
3851 | if (error) |
3852 | goto exit1; | |
b21996e3 | 3853 | retry_deleg: |
f5beed75 AV |
3854 | mutex_lock_nested(&path.dentry->d_inode->i_mutex, I_MUTEX_PARENT); |
3855 | dentry = __lookup_hash(&last, path.dentry, lookup_flags); | |
1da177e4 LT |
3856 | error = PTR_ERR(dentry); |
3857 | if (!IS_ERR(dentry)) { | |
3858 | /* Why not before? Because we want correct error value */ | |
f5beed75 | 3859 | if (last.name[last.len]) |
50338b88 | 3860 | goto slashes; |
1da177e4 | 3861 | inode = dentry->d_inode; |
b18825a7 | 3862 | if (d_is_negative(dentry)) |
e6bc45d6 TT |
3863 | goto slashes; |
3864 | ihold(inode); | |
f5beed75 | 3865 | error = security_path_unlink(&path, dentry); |
be6d3e56 | 3866 | if (error) |
c30dabfe | 3867 | goto exit2; |
f5beed75 | 3868 | error = vfs_unlink(path.dentry->d_inode, dentry, &delegated_inode); |
c30dabfe | 3869 | exit2: |
1da177e4 LT |
3870 | dput(dentry); |
3871 | } | |
f5beed75 | 3872 | mutex_unlock(&path.dentry->d_inode->i_mutex); |
1da177e4 LT |
3873 | if (inode) |
3874 | iput(inode); /* truncate the inode here */ | |
b21996e3 BF |
3875 | inode = NULL; |
3876 | if (delegated_inode) { | |
5a14696c | 3877 | error = break_deleg_wait(&delegated_inode); |
b21996e3 BF |
3878 | if (!error) |
3879 | goto retry_deleg; | |
3880 | } | |
f5beed75 | 3881 | mnt_drop_write(path.mnt); |
1da177e4 | 3882 | exit1: |
f5beed75 | 3883 | path_put(&path); |
1da177e4 | 3884 | putname(name); |
5d18f813 JL |
3885 | if (retry_estale(error, lookup_flags)) { |
3886 | lookup_flags |= LOOKUP_REVAL; | |
3887 | inode = NULL; | |
3888 | goto retry; | |
3889 | } | |
1da177e4 LT |
3890 | return error; |
3891 | ||
3892 | slashes: | |
b18825a7 DH |
3893 | if (d_is_negative(dentry)) |
3894 | error = -ENOENT; | |
44b1d530 | 3895 | else if (d_is_dir(dentry)) |
b18825a7 DH |
3896 | error = -EISDIR; |
3897 | else | |
3898 | error = -ENOTDIR; | |
1da177e4 LT |
3899 | goto exit2; |
3900 | } | |
3901 | ||
2e4d0924 | 3902 | SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) |
5590ff0d UD |
3903 | { |
3904 | if ((flag & ~AT_REMOVEDIR) != 0) | |
3905 | return -EINVAL; | |
3906 | ||
3907 | if (flag & AT_REMOVEDIR) | |
3908 | return do_rmdir(dfd, pathname); | |
3909 | ||
3910 | return do_unlinkat(dfd, pathname); | |
3911 | } | |
3912 | ||
3480b257 | 3913 | SYSCALL_DEFINE1(unlink, const char __user *, pathname) |
5590ff0d UD |
3914 | { |
3915 | return do_unlinkat(AT_FDCWD, pathname); | |
3916 | } | |
3917 | ||
db2e747b | 3918 | int vfs_symlink(struct inode *dir, struct dentry *dentry, const char *oldname) |
1da177e4 | 3919 | { |
a95164d9 | 3920 | int error = may_create(dir, dentry); |
1da177e4 LT |
3921 | |
3922 | if (error) | |
3923 | return error; | |
3924 | ||
acfa4380 | 3925 | if (!dir->i_op->symlink) |
1da177e4 LT |
3926 | return -EPERM; |
3927 | ||
3928 | error = security_inode_symlink(dir, dentry, oldname); | |
3929 | if (error) | |
3930 | return error; | |
3931 | ||
1da177e4 | 3932 | error = dir->i_op->symlink(dir, dentry, oldname); |
a74574aa | 3933 | if (!error) |
f38aa942 | 3934 | fsnotify_create(dir, dentry); |
1da177e4 LT |
3935 | return error; |
3936 | } | |
4d359507 | 3937 | EXPORT_SYMBOL(vfs_symlink); |
1da177e4 | 3938 | |
2e4d0924 HC |
3939 | SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, |
3940 | int, newdfd, const char __user *, newname) | |
1da177e4 | 3941 | { |
2ad94ae6 | 3942 | int error; |
91a27b2a | 3943 | struct filename *from; |
6902d925 | 3944 | struct dentry *dentry; |
dae6ad8f | 3945 | struct path path; |
f46d3567 | 3946 | unsigned int lookup_flags = 0; |
1da177e4 LT |
3947 | |
3948 | from = getname(oldname); | |
2ad94ae6 | 3949 | if (IS_ERR(from)) |
1da177e4 | 3950 | return PTR_ERR(from); |
f46d3567 JL |
3951 | retry: |
3952 | dentry = user_path_create(newdfd, newname, &path, lookup_flags); | |
6902d925 DH |
3953 | error = PTR_ERR(dentry); |
3954 | if (IS_ERR(dentry)) | |
dae6ad8f | 3955 | goto out_putname; |
6902d925 | 3956 | |
91a27b2a | 3957 | error = security_path_symlink(&path, dentry, from->name); |
a8104a9f | 3958 | if (!error) |
91a27b2a | 3959 | error = vfs_symlink(path.dentry->d_inode, dentry, from->name); |
921a1650 | 3960 | done_path_create(&path, dentry); |
f46d3567 JL |
3961 | if (retry_estale(error, lookup_flags)) { |
3962 | lookup_flags |= LOOKUP_REVAL; | |
3963 | goto retry; | |
3964 | } | |
6902d925 | 3965 | out_putname: |
1da177e4 LT |
3966 | putname(from); |
3967 | return error; | |
3968 | } | |
3969 | ||
3480b257 | 3970 | SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) |
5590ff0d UD |
3971 | { |
3972 | return sys_symlinkat(oldname, AT_FDCWD, newname); | |
3973 | } | |
3974 | ||
146a8595 BF |
3975 | /** |
3976 | * vfs_link - create a new link | |
3977 | * @old_dentry: object to be linked | |
3978 | * @dir: new parent | |
3979 | * @new_dentry: where to create the new link | |
3980 | * @delegated_inode: returns inode needing a delegation break | |
3981 | * | |
3982 | * The caller must hold dir->i_mutex | |
3983 | * | |
3984 | * If vfs_link discovers a delegation on the to-be-linked file in need | |
3985 | * of breaking, it will return -EWOULDBLOCK and return a reference to the | |
3986 | * inode in delegated_inode. The caller should then break the delegation | |
3987 | * and retry. Because breaking a delegation may take a long time, the | |
3988 | * caller should drop the i_mutex before doing so. | |
3989 | * | |
3990 | * Alternatively, a caller may pass NULL for delegated_inode. This may | |
3991 | * be appropriate for callers that expect the underlying filesystem not | |
3992 | * to be NFS exported. | |
3993 | */ | |
3994 | int vfs_link(struct dentry *old_dentry, struct inode *dir, struct dentry *new_dentry, struct inode **delegated_inode) | |
1da177e4 LT |
3995 | { |
3996 | struct inode *inode = old_dentry->d_inode; | |
8de52778 | 3997 | unsigned max_links = dir->i_sb->s_max_links; |
1da177e4 LT |
3998 | int error; |
3999 | ||
4000 | if (!inode) | |
4001 | return -ENOENT; | |
4002 | ||
a95164d9 | 4003 | error = may_create(dir, new_dentry); |
1da177e4 LT |
4004 | if (error) |
4005 | return error; | |
4006 | ||
4007 | if (dir->i_sb != inode->i_sb) | |
4008 | return -EXDEV; | |
4009 | ||
4010 | /* | |
4011 | * A link to an append-only or immutable file cannot be created. | |
4012 | */ | |
4013 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | |
4014 | return -EPERM; | |
acfa4380 | 4015 | if (!dir->i_op->link) |
1da177e4 | 4016 | return -EPERM; |
7e79eedb | 4017 | if (S_ISDIR(inode->i_mode)) |
1da177e4 LT |
4018 | return -EPERM; |
4019 | ||
4020 | error = security_inode_link(old_dentry, dir, new_dentry); | |
4021 | if (error) | |
4022 | return error; | |
4023 | ||
7e79eedb | 4024 | mutex_lock(&inode->i_mutex); |
aae8a97d | 4025 | /* Make sure we don't allow creating hardlink to an unlinked file */ |
f4e0c30c | 4026 | if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) |
aae8a97d | 4027 | error = -ENOENT; |
8de52778 AV |
4028 | else if (max_links && inode->i_nlink >= max_links) |
4029 | error = -EMLINK; | |
146a8595 BF |
4030 | else { |
4031 | error = try_break_deleg(inode, delegated_inode); | |
4032 | if (!error) | |
4033 | error = dir->i_op->link(old_dentry, dir, new_dentry); | |
4034 | } | |
f4e0c30c AV |
4035 | |
4036 | if (!error && (inode->i_state & I_LINKABLE)) { | |
4037 | spin_lock(&inode->i_lock); | |
4038 | inode->i_state &= ~I_LINKABLE; | |
4039 | spin_unlock(&inode->i_lock); | |
4040 | } | |
7e79eedb | 4041 | mutex_unlock(&inode->i_mutex); |
e31e14ec | 4042 | if (!error) |
7e79eedb | 4043 | fsnotify_link(dir, inode, new_dentry); |
1da177e4 LT |
4044 | return error; |
4045 | } | |
4d359507 | 4046 | EXPORT_SYMBOL(vfs_link); |
1da177e4 LT |
4047 | |
4048 | /* | |
4049 | * Hardlinks are often used in delicate situations. We avoid | |
4050 | * security-related surprises by not following symlinks on the | |
4051 | * newname. --KAB | |
4052 | * | |
4053 | * We don't follow them on the oldname either to be compatible | |
4054 | * with linux 2.0, and to avoid hard-linking to directories | |
4055 | * and other special files. --ADM | |
4056 | */ | |
2e4d0924 HC |
4057 | SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, |
4058 | int, newdfd, const char __user *, newname, int, flags) | |
1da177e4 LT |
4059 | { |
4060 | struct dentry *new_dentry; | |
dae6ad8f | 4061 | struct path old_path, new_path; |
146a8595 | 4062 | struct inode *delegated_inode = NULL; |
11a7b371 | 4063 | int how = 0; |
1da177e4 | 4064 | int error; |
1da177e4 | 4065 | |
11a7b371 | 4066 | if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) |
c04030e1 | 4067 | return -EINVAL; |
11a7b371 | 4068 | /* |
f0cc6ffb LT |
4069 | * To use null names we require CAP_DAC_READ_SEARCH |
4070 | * This ensures that not everyone will be able to create | |
4071 | * handlink using the passed filedescriptor. | |
11a7b371 | 4072 | */ |
f0cc6ffb LT |
4073 | if (flags & AT_EMPTY_PATH) { |
4074 | if (!capable(CAP_DAC_READ_SEARCH)) | |
4075 | return -ENOENT; | |
11a7b371 | 4076 | how = LOOKUP_EMPTY; |
f0cc6ffb | 4077 | } |
11a7b371 AK |
4078 | |
4079 | if (flags & AT_SYMLINK_FOLLOW) | |
4080 | how |= LOOKUP_FOLLOW; | |
442e31ca | 4081 | retry: |
11a7b371 | 4082 | error = user_path_at(olddfd, oldname, how, &old_path); |
1da177e4 | 4083 | if (error) |
2ad94ae6 AV |
4084 | return error; |
4085 | ||
442e31ca JL |
4086 | new_dentry = user_path_create(newdfd, newname, &new_path, |
4087 | (how & LOOKUP_REVAL)); | |
1da177e4 | 4088 | error = PTR_ERR(new_dentry); |
6902d925 | 4089 | if (IS_ERR(new_dentry)) |
dae6ad8f AV |
4090 | goto out; |
4091 | ||
4092 | error = -EXDEV; | |
4093 | if (old_path.mnt != new_path.mnt) | |
4094 | goto out_dput; | |
800179c9 KC |
4095 | error = may_linkat(&old_path); |
4096 | if (unlikely(error)) | |
4097 | goto out_dput; | |
dae6ad8f | 4098 | error = security_path_link(old_path.dentry, &new_path, new_dentry); |
be6d3e56 | 4099 | if (error) |
a8104a9f | 4100 | goto out_dput; |
146a8595 | 4101 | error = vfs_link(old_path.dentry, new_path.dentry->d_inode, new_dentry, &delegated_inode); |
75c3f29d | 4102 | out_dput: |
921a1650 | 4103 | done_path_create(&new_path, new_dentry); |
146a8595 BF |
4104 | if (delegated_inode) { |
4105 | error = break_deleg_wait(&delegated_inode); | |
d22e6338 OD |
4106 | if (!error) { |
4107 | path_put(&old_path); | |
146a8595 | 4108 | goto retry; |
d22e6338 | 4109 | } |
146a8595 | 4110 | } |
442e31ca | 4111 | if (retry_estale(error, how)) { |
d22e6338 | 4112 | path_put(&old_path); |
442e31ca JL |
4113 | how |= LOOKUP_REVAL; |
4114 | goto retry; | |
4115 | } | |
1da177e4 | 4116 | out: |
2d8f3038 | 4117 | path_put(&old_path); |
1da177e4 LT |
4118 | |
4119 | return error; | |
4120 | } | |
4121 | ||
3480b257 | 4122 | SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) |
5590ff0d | 4123 | { |
c04030e1 | 4124 | return sys_linkat(AT_FDCWD, oldname, AT_FDCWD, newname, 0); |
5590ff0d UD |
4125 | } |
4126 | ||
bc27027a MS |
4127 | /** |
4128 | * vfs_rename - rename a filesystem object | |
4129 | * @old_dir: parent of source | |
4130 | * @old_dentry: source | |
4131 | * @new_dir: parent of destination | |
4132 | * @new_dentry: destination | |
4133 | * @delegated_inode: returns an inode needing a delegation break | |
520c8b16 | 4134 | * @flags: rename flags |
bc27027a MS |
4135 | * |
4136 | * The caller must hold multiple mutexes--see lock_rename()). | |
4137 | * | |
4138 | * If vfs_rename discovers a delegation in need of breaking at either | |
4139 | * the source or destination, it will return -EWOULDBLOCK and return a | |
4140 | * reference to the inode in delegated_inode. The caller should then | |
4141 | * break the delegation and retry. Because breaking a delegation may | |
4142 | * take a long time, the caller should drop all locks before doing | |
4143 | * so. | |
4144 | * | |
4145 | * Alternatively, a caller may pass NULL for delegated_inode. This may | |
4146 | * be appropriate for callers that expect the underlying filesystem not | |
4147 | * to be NFS exported. | |
4148 | * | |
1da177e4 LT |
4149 | * The worst of all namespace operations - renaming directory. "Perverted" |
4150 | * doesn't even start to describe it. Somebody in UCB had a heck of a trip... | |
4151 | * Problems: | |
d03b29a2 | 4152 | * a) we can get into loop creation. |
1da177e4 LT |
4153 | * b) race potential - two innocent renames can create a loop together. |
4154 | * That's where 4.4 screws up. Current fix: serialization on | |
a11f3a05 | 4155 | * sb->s_vfs_rename_mutex. We might be more accurate, but that's another |
1da177e4 | 4156 | * story. |
6cedba89 BF |
4157 | * c) we have to lock _four_ objects - parents and victim (if it exists), |
4158 | * and source (if it is not a directory). | |
1b1dcc1b | 4159 | * And that - after we got ->i_mutex on parents (until then we don't know |
1da177e4 LT |
4160 | * whether the target exists). Solution: try to be smart with locking |
4161 | * order for inodes. We rely on the fact that tree topology may change | |
a11f3a05 | 4162 | * only under ->s_vfs_rename_mutex _and_ that parent of the object we |
1da177e4 LT |
4163 | * move will be locked. Thus we can rank directories by the tree |
4164 | * (ancestors first) and rank all non-directories after them. | |
4165 | * That works since everybody except rename does "lock parent, lookup, | |
a11f3a05 | 4166 | * lock child" and rename is under ->s_vfs_rename_mutex. |
1da177e4 LT |
4167 | * HOWEVER, it relies on the assumption that any object with ->lookup() |
4168 | * has no more than 1 dentry. If "hybrid" objects will ever appear, | |
4169 | * we'd better make sure that there's no link(2) for them. | |
e4eaac06 | 4170 | * d) conversion from fhandle to dentry may come in the wrong moment - when |
1b1dcc1b | 4171 | * we are removing the target. Solution: we will have to grab ->i_mutex |
1da177e4 | 4172 | * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on |
c41b20e7 | 4173 | * ->i_mutex on parents, which works but leads to some truly excessive |
1da177e4 LT |
4174 | * locking]. |
4175 | */ | |
bc27027a MS |
4176 | int vfs_rename(struct inode *old_dir, struct dentry *old_dentry, |
4177 | struct inode *new_dir, struct dentry *new_dentry, | |
520c8b16 | 4178 | struct inode **delegated_inode, unsigned int flags) |
1da177e4 | 4179 | { |
bc27027a MS |
4180 | int error; |
4181 | bool is_dir = d_is_dir(old_dentry); | |
4182 | const unsigned char *old_name; | |
4183 | struct inode *source = old_dentry->d_inode; | |
9055cba7 | 4184 | struct inode *target = new_dentry->d_inode; |
da1ce067 MS |
4185 | bool new_is_dir = false; |
4186 | unsigned max_links = new_dir->i_sb->s_max_links; | |
bc27027a MS |
4187 | |
4188 | if (source == target) | |
4189 | return 0; | |
4190 | ||
4191 | error = may_delete(old_dir, old_dentry, is_dir); | |
4192 | if (error) | |
4193 | return error; | |
4194 | ||
da1ce067 | 4195 | if (!target) { |
bc27027a | 4196 | error = may_create(new_dir, new_dentry); |
da1ce067 MS |
4197 | } else { |
4198 | new_is_dir = d_is_dir(new_dentry); | |
4199 | ||
4200 | if (!(flags & RENAME_EXCHANGE)) | |
4201 | error = may_delete(new_dir, new_dentry, is_dir); | |
4202 | else | |
4203 | error = may_delete(new_dir, new_dentry, new_is_dir); | |
4204 | } | |
bc27027a MS |
4205 | if (error) |
4206 | return error; | |
4207 | ||
7177a9c4 | 4208 | if (!old_dir->i_op->rename && !old_dir->i_op->rename2) |
bc27027a | 4209 | return -EPERM; |
1da177e4 | 4210 | |
520c8b16 MS |
4211 | if (flags && !old_dir->i_op->rename2) |
4212 | return -EINVAL; | |
4213 | ||
1da177e4 LT |
4214 | /* |
4215 | * If we are going to change the parent - check write permissions, | |
4216 | * we'll need to flip '..'. | |
4217 | */ | |
da1ce067 MS |
4218 | if (new_dir != old_dir) { |
4219 | if (is_dir) { | |
4220 | error = inode_permission(source, MAY_WRITE); | |
4221 | if (error) | |
4222 | return error; | |
4223 | } | |
4224 | if ((flags & RENAME_EXCHANGE) && new_is_dir) { | |
4225 | error = inode_permission(target, MAY_WRITE); | |
4226 | if (error) | |
4227 | return error; | |
4228 | } | |
1da177e4 LT |
4229 | } |
4230 | ||
0b3974eb MS |
4231 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry, |
4232 | flags); | |
1da177e4 LT |
4233 | if (error) |
4234 | return error; | |
4235 | ||
bc27027a | 4236 | old_name = fsnotify_oldname_init(old_dentry->d_name.name); |
1d2ef590 | 4237 | dget(new_dentry); |
da1ce067 | 4238 | if (!is_dir || (flags & RENAME_EXCHANGE)) |
bc27027a MS |
4239 | lock_two_nondirectories(source, target); |
4240 | else if (target) | |
1b1dcc1b | 4241 | mutex_lock(&target->i_mutex); |
9055cba7 SW |
4242 | |
4243 | error = -EBUSY; | |
7af1364f | 4244 | if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry)) |
9055cba7 SW |
4245 | goto out; |
4246 | ||
da1ce067 | 4247 | if (max_links && new_dir != old_dir) { |
bc27027a | 4248 | error = -EMLINK; |
da1ce067 | 4249 | if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links) |
bc27027a | 4250 | goto out; |
da1ce067 MS |
4251 | if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir && |
4252 | old_dir->i_nlink >= max_links) | |
4253 | goto out; | |
4254 | } | |
4255 | if (is_dir && !(flags & RENAME_EXCHANGE) && target) | |
4256 | shrink_dcache_parent(new_dentry); | |
4257 | if (!is_dir) { | |
bc27027a | 4258 | error = try_break_deleg(source, delegated_inode); |
8e6d782c BF |
4259 | if (error) |
4260 | goto out; | |
da1ce067 MS |
4261 | } |
4262 | if (target && !new_is_dir) { | |
4263 | error = try_break_deleg(target, delegated_inode); | |
4264 | if (error) | |
4265 | goto out; | |
8e6d782c | 4266 | } |
7177a9c4 | 4267 | if (!old_dir->i_op->rename2) { |
520c8b16 MS |
4268 | error = old_dir->i_op->rename(old_dir, old_dentry, |
4269 | new_dir, new_dentry); | |
4270 | } else { | |
7177a9c4 | 4271 | WARN_ON(old_dir->i_op->rename != NULL); |
520c8b16 MS |
4272 | error = old_dir->i_op->rename2(old_dir, old_dentry, |
4273 | new_dir, new_dentry, flags); | |
4274 | } | |
51892bbb SW |
4275 | if (error) |
4276 | goto out; | |
4277 | ||
da1ce067 | 4278 | if (!(flags & RENAME_EXCHANGE) && target) { |
bc27027a MS |
4279 | if (is_dir) |
4280 | target->i_flags |= S_DEAD; | |
51892bbb | 4281 | dont_mount(new_dentry); |
8ed936b5 | 4282 | detach_mounts(new_dentry); |
bc27027a | 4283 | } |
da1ce067 MS |
4284 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) { |
4285 | if (!(flags & RENAME_EXCHANGE)) | |
4286 | d_move(old_dentry, new_dentry); | |
4287 | else | |
4288 | d_exchange(old_dentry, new_dentry); | |
4289 | } | |
51892bbb | 4290 | out: |
da1ce067 | 4291 | if (!is_dir || (flags & RENAME_EXCHANGE)) |
bc27027a MS |
4292 | unlock_two_nondirectories(source, target); |
4293 | else if (target) | |
4294 | mutex_unlock(&target->i_mutex); | |
1da177e4 | 4295 | dput(new_dentry); |
da1ce067 | 4296 | if (!error) { |
123df294 | 4297 | fsnotify_move(old_dir, new_dir, old_name, is_dir, |
da1ce067 MS |
4298 | !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry); |
4299 | if (flags & RENAME_EXCHANGE) { | |
4300 | fsnotify_move(new_dir, old_dir, old_dentry->d_name.name, | |
4301 | new_is_dir, NULL, new_dentry); | |
4302 | } | |
4303 | } | |
0eeca283 RL |
4304 | fsnotify_oldname_free(old_name); |
4305 | ||
1da177e4 LT |
4306 | return error; |
4307 | } | |
4d359507 | 4308 | EXPORT_SYMBOL(vfs_rename); |
1da177e4 | 4309 | |
520c8b16 MS |
4310 | SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname, |
4311 | int, newdfd, const char __user *, newname, unsigned int, flags) | |
1da177e4 | 4312 | { |
2ad94ae6 AV |
4313 | struct dentry *old_dentry, *new_dentry; |
4314 | struct dentry *trap; | |
f5beed75 AV |
4315 | struct path old_path, new_path; |
4316 | struct qstr old_last, new_last; | |
4317 | int old_type, new_type; | |
8e6d782c | 4318 | struct inode *delegated_inode = NULL; |
91a27b2a JL |
4319 | struct filename *from; |
4320 | struct filename *to; | |
f5beed75 | 4321 | unsigned int lookup_flags = 0, target_flags = LOOKUP_RENAME_TARGET; |
c6a94284 | 4322 | bool should_retry = false; |
2ad94ae6 | 4323 | int error; |
520c8b16 | 4324 | |
0d7a8555 | 4325 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
da1ce067 MS |
4326 | return -EINVAL; |
4327 | ||
0d7a8555 MS |
4328 | if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) && |
4329 | (flags & RENAME_EXCHANGE)) | |
520c8b16 MS |
4330 | return -EINVAL; |
4331 | ||
0d7a8555 MS |
4332 | if ((flags & RENAME_WHITEOUT) && !capable(CAP_MKNOD)) |
4333 | return -EPERM; | |
4334 | ||
f5beed75 AV |
4335 | if (flags & RENAME_EXCHANGE) |
4336 | target_flags = 0; | |
4337 | ||
c6a94284 | 4338 | retry: |
f5beed75 AV |
4339 | from = user_path_parent(olddfd, oldname, |
4340 | &old_path, &old_last, &old_type, lookup_flags); | |
91a27b2a JL |
4341 | if (IS_ERR(from)) { |
4342 | error = PTR_ERR(from); | |
1da177e4 | 4343 | goto exit; |
91a27b2a | 4344 | } |
1da177e4 | 4345 | |
f5beed75 AV |
4346 | to = user_path_parent(newdfd, newname, |
4347 | &new_path, &new_last, &new_type, lookup_flags); | |
91a27b2a JL |
4348 | if (IS_ERR(to)) { |
4349 | error = PTR_ERR(to); | |
1da177e4 | 4350 | goto exit1; |
91a27b2a | 4351 | } |
1da177e4 LT |
4352 | |
4353 | error = -EXDEV; | |
f5beed75 | 4354 | if (old_path.mnt != new_path.mnt) |
1da177e4 LT |
4355 | goto exit2; |
4356 | ||
1da177e4 | 4357 | error = -EBUSY; |
f5beed75 | 4358 | if (old_type != LAST_NORM) |
1da177e4 LT |
4359 | goto exit2; |
4360 | ||
0a7c3937 MS |
4361 | if (flags & RENAME_NOREPLACE) |
4362 | error = -EEXIST; | |
f5beed75 | 4363 | if (new_type != LAST_NORM) |
1da177e4 LT |
4364 | goto exit2; |
4365 | ||
f5beed75 | 4366 | error = mnt_want_write(old_path.mnt); |
c30dabfe JK |
4367 | if (error) |
4368 | goto exit2; | |
4369 | ||
8e6d782c | 4370 | retry_deleg: |
f5beed75 | 4371 | trap = lock_rename(new_path.dentry, old_path.dentry); |
1da177e4 | 4372 | |
f5beed75 | 4373 | old_dentry = __lookup_hash(&old_last, old_path.dentry, lookup_flags); |
1da177e4 LT |
4374 | error = PTR_ERR(old_dentry); |
4375 | if (IS_ERR(old_dentry)) | |
4376 | goto exit3; | |
4377 | /* source must exist */ | |
4378 | error = -ENOENT; | |
b18825a7 | 4379 | if (d_is_negative(old_dentry)) |
1da177e4 | 4380 | goto exit4; |
f5beed75 | 4381 | new_dentry = __lookup_hash(&new_last, new_path.dentry, lookup_flags | target_flags); |
0a7c3937 MS |
4382 | error = PTR_ERR(new_dentry); |
4383 | if (IS_ERR(new_dentry)) | |
4384 | goto exit4; | |
4385 | error = -EEXIST; | |
4386 | if ((flags & RENAME_NOREPLACE) && d_is_positive(new_dentry)) | |
4387 | goto exit5; | |
da1ce067 MS |
4388 | if (flags & RENAME_EXCHANGE) { |
4389 | error = -ENOENT; | |
4390 | if (d_is_negative(new_dentry)) | |
4391 | goto exit5; | |
4392 | ||
4393 | if (!d_is_dir(new_dentry)) { | |
4394 | error = -ENOTDIR; | |
f5beed75 | 4395 | if (new_last.name[new_last.len]) |
da1ce067 MS |
4396 | goto exit5; |
4397 | } | |
4398 | } | |
1da177e4 | 4399 | /* unless the source is a directory trailing slashes give -ENOTDIR */ |
44b1d530 | 4400 | if (!d_is_dir(old_dentry)) { |
1da177e4 | 4401 | error = -ENOTDIR; |
f5beed75 | 4402 | if (old_last.name[old_last.len]) |
0a7c3937 | 4403 | goto exit5; |
f5beed75 | 4404 | if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len]) |
0a7c3937 | 4405 | goto exit5; |
1da177e4 LT |
4406 | } |
4407 | /* source should not be ancestor of target */ | |
4408 | error = -EINVAL; | |
4409 | if (old_dentry == trap) | |
0a7c3937 | 4410 | goto exit5; |
1da177e4 | 4411 | /* target should not be an ancestor of source */ |
da1ce067 MS |
4412 | if (!(flags & RENAME_EXCHANGE)) |
4413 | error = -ENOTEMPTY; | |
1da177e4 LT |
4414 | if (new_dentry == trap) |
4415 | goto exit5; | |
4416 | ||
f5beed75 AV |
4417 | error = security_path_rename(&old_path, old_dentry, |
4418 | &new_path, new_dentry, flags); | |
be6d3e56 | 4419 | if (error) |
c30dabfe | 4420 | goto exit5; |
f5beed75 AV |
4421 | error = vfs_rename(old_path.dentry->d_inode, old_dentry, |
4422 | new_path.dentry->d_inode, new_dentry, | |
520c8b16 | 4423 | &delegated_inode, flags); |
1da177e4 LT |
4424 | exit5: |
4425 | dput(new_dentry); | |
4426 | exit4: | |
4427 | dput(old_dentry); | |
4428 | exit3: | |
f5beed75 | 4429 | unlock_rename(new_path.dentry, old_path.dentry); |
8e6d782c BF |
4430 | if (delegated_inode) { |
4431 | error = break_deleg_wait(&delegated_inode); | |
4432 | if (!error) | |
4433 | goto retry_deleg; | |
4434 | } | |
f5beed75 | 4435 | mnt_drop_write(old_path.mnt); |
1da177e4 | 4436 | exit2: |
c6a94284 JL |
4437 | if (retry_estale(error, lookup_flags)) |
4438 | should_retry = true; | |
f5beed75 | 4439 | path_put(&new_path); |
2ad94ae6 | 4440 | putname(to); |
1da177e4 | 4441 | exit1: |
f5beed75 | 4442 | path_put(&old_path); |
1da177e4 | 4443 | putname(from); |
c6a94284 JL |
4444 | if (should_retry) { |
4445 | should_retry = false; | |
4446 | lookup_flags |= LOOKUP_REVAL; | |
4447 | goto retry; | |
4448 | } | |
2ad94ae6 | 4449 | exit: |
1da177e4 LT |
4450 | return error; |
4451 | } | |
4452 | ||
520c8b16 MS |
4453 | SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, |
4454 | int, newdfd, const char __user *, newname) | |
4455 | { | |
4456 | return sys_renameat2(olddfd, oldname, newdfd, newname, 0); | |
4457 | } | |
4458 | ||
a26eab24 | 4459 | SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) |
5590ff0d | 4460 | { |
520c8b16 | 4461 | return sys_renameat2(AT_FDCWD, oldname, AT_FDCWD, newname, 0); |
5590ff0d UD |
4462 | } |
4463 | ||
787fb6bc MS |
4464 | int vfs_whiteout(struct inode *dir, struct dentry *dentry) |
4465 | { | |
4466 | int error = may_create(dir, dentry); | |
4467 | if (error) | |
4468 | return error; | |
4469 | ||
4470 | if (!dir->i_op->mknod) | |
4471 | return -EPERM; | |
4472 | ||
4473 | return dir->i_op->mknod(dir, dentry, | |
4474 | S_IFCHR | WHITEOUT_MODE, WHITEOUT_DEV); | |
4475 | } | |
4476 | EXPORT_SYMBOL(vfs_whiteout); | |
4477 | ||
5d826c84 | 4478 | int readlink_copy(char __user *buffer, int buflen, const char *link) |
1da177e4 | 4479 | { |
5d826c84 | 4480 | int len = PTR_ERR(link); |
1da177e4 LT |
4481 | if (IS_ERR(link)) |
4482 | goto out; | |
4483 | ||
4484 | len = strlen(link); | |
4485 | if (len > (unsigned) buflen) | |
4486 | len = buflen; | |
4487 | if (copy_to_user(buffer, link, len)) | |
4488 | len = -EFAULT; | |
4489 | out: | |
4490 | return len; | |
4491 | } | |
5d826c84 | 4492 | EXPORT_SYMBOL(readlink_copy); |
1da177e4 LT |
4493 | |
4494 | /* | |
4495 | * A helper for ->readlink(). This should be used *ONLY* for symlinks that | |
4496 | * have ->follow_link() touching nd only in nd_set_link(). Using (or not | |
4497 | * using) it for any given inode is up to filesystem. | |
4498 | */ | |
4499 | int generic_readlink(struct dentry *dentry, char __user *buffer, int buflen) | |
4500 | { | |
cc314eef | 4501 | void *cookie; |
5f2c4179 AV |
4502 | struct inode *inode = d_inode(dentry); |
4503 | const char *link = inode->i_link; | |
694a1764 | 4504 | int res; |
cc314eef | 4505 | |
d4dee48b | 4506 | if (!link) { |
5f2c4179 | 4507 | link = inode->i_op->follow_link(dentry, &cookie); |
d4dee48b AV |
4508 | if (IS_ERR(link)) |
4509 | return PTR_ERR(link); | |
4510 | } | |
680baacb | 4511 | res = readlink_copy(buffer, buflen, link); |
5f2c4179 AV |
4512 | if (inode->i_op->put_link) |
4513 | inode->i_op->put_link(inode, cookie); | |
694a1764 | 4514 | return res; |
1da177e4 | 4515 | } |
4d359507 | 4516 | EXPORT_SYMBOL(generic_readlink); |
1da177e4 | 4517 | |
1da177e4 LT |
4518 | /* get the link contents into pagecache */ |
4519 | static char *page_getlink(struct dentry * dentry, struct page **ppage) | |
4520 | { | |
ebd09abb DG |
4521 | char *kaddr; |
4522 | struct page *page; | |
1da177e4 | 4523 | struct address_space *mapping = dentry->d_inode->i_mapping; |
090d2b18 | 4524 | page = read_mapping_page(mapping, 0, NULL); |
1da177e4 | 4525 | if (IS_ERR(page)) |
6fe6900e | 4526 | return (char*)page; |
1da177e4 | 4527 | *ppage = page; |
ebd09abb DG |
4528 | kaddr = kmap(page); |
4529 | nd_terminate_link(kaddr, dentry->d_inode->i_size, PAGE_SIZE - 1); | |
4530 | return kaddr; | |
1da177e4 LT |
4531 | } |
4532 | ||
4533 | int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) | |
4534 | { | |
4535 | struct page *page = NULL; | |
5d826c84 | 4536 | int res = readlink_copy(buffer, buflen, page_getlink(dentry, &page)); |
1da177e4 LT |
4537 | if (page) { |
4538 | kunmap(page); | |
4539 | page_cache_release(page); | |
4540 | } | |
4541 | return res; | |
4542 | } | |
4d359507 | 4543 | EXPORT_SYMBOL(page_readlink); |
1da177e4 | 4544 | |
6e77137b | 4545 | const char *page_follow_link_light(struct dentry *dentry, void **cookie) |
1da177e4 | 4546 | { |
cc314eef | 4547 | struct page *page = NULL; |
680baacb AV |
4548 | char *res = page_getlink(dentry, &page); |
4549 | if (!IS_ERR(res)) | |
4550 | *cookie = page; | |
4551 | return res; | |
1da177e4 | 4552 | } |
4d359507 | 4553 | EXPORT_SYMBOL(page_follow_link_light); |
1da177e4 | 4554 | |
5f2c4179 | 4555 | void page_put_link(struct inode *unused, void *cookie) |
1da177e4 | 4556 | { |
cc314eef | 4557 | struct page *page = cookie; |
680baacb AV |
4558 | kunmap(page); |
4559 | page_cache_release(page); | |
1da177e4 | 4560 | } |
4d359507 | 4561 | EXPORT_SYMBOL(page_put_link); |
1da177e4 | 4562 | |
54566b2c NP |
4563 | /* |
4564 | * The nofs argument instructs pagecache_write_begin to pass AOP_FLAG_NOFS | |
4565 | */ | |
4566 | int __page_symlink(struct inode *inode, const char *symname, int len, int nofs) | |
1da177e4 LT |
4567 | { |
4568 | struct address_space *mapping = inode->i_mapping; | |
0adb25d2 | 4569 | struct page *page; |
afddba49 | 4570 | void *fsdata; |
beb497ab | 4571 | int err; |
1da177e4 | 4572 | char *kaddr; |
54566b2c NP |
4573 | unsigned int flags = AOP_FLAG_UNINTERRUPTIBLE; |
4574 | if (nofs) | |
4575 | flags |= AOP_FLAG_NOFS; | |
1da177e4 | 4576 | |
7e53cac4 | 4577 | retry: |
afddba49 | 4578 | err = pagecache_write_begin(NULL, mapping, 0, len-1, |
54566b2c | 4579 | flags, &page, &fsdata); |
1da177e4 | 4580 | if (err) |
afddba49 NP |
4581 | goto fail; |
4582 | ||
e8e3c3d6 | 4583 | kaddr = kmap_atomic(page); |
1da177e4 | 4584 | memcpy(kaddr, symname, len-1); |
e8e3c3d6 | 4585 | kunmap_atomic(kaddr); |
afddba49 NP |
4586 | |
4587 | err = pagecache_write_end(NULL, mapping, 0, len-1, len-1, | |
4588 | page, fsdata); | |
1da177e4 LT |
4589 | if (err < 0) |
4590 | goto fail; | |
afddba49 NP |
4591 | if (err < len-1) |
4592 | goto retry; | |
4593 | ||
1da177e4 LT |
4594 | mark_inode_dirty(inode); |
4595 | return 0; | |
1da177e4 LT |
4596 | fail: |
4597 | return err; | |
4598 | } | |
4d359507 | 4599 | EXPORT_SYMBOL(__page_symlink); |
1da177e4 | 4600 | |
0adb25d2 KK |
4601 | int page_symlink(struct inode *inode, const char *symname, int len) |
4602 | { | |
4603 | return __page_symlink(inode, symname, len, | |
c62d2555 | 4604 | !mapping_gfp_constraint(inode->i_mapping, __GFP_FS)); |
0adb25d2 | 4605 | } |
4d359507 | 4606 | EXPORT_SYMBOL(page_symlink); |
0adb25d2 | 4607 | |
92e1d5be | 4608 | const struct inode_operations page_symlink_inode_operations = { |
1da177e4 LT |
4609 | .readlink = generic_readlink, |
4610 | .follow_link = page_follow_link_light, | |
4611 | .put_link = page_put_link, | |
4612 | }; | |
1da177e4 | 4613 | EXPORT_SYMBOL(page_symlink_inode_operations); |