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