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