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