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