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> |
1da177e4 | 20 | #include <linux/slab.h> |
66a5c40f | 21 | #include <linux/wordpart.h> |
1da177e4 | 22 | #include <linux/fs.h> |
5970e15d | 23 | #include <linux/filelock.h> |
1da177e4 | 24 | #include <linux/namei.h> |
1da177e4 | 25 | #include <linux/pagemap.h> |
2d878178 | 26 | #include <linux/sched/mm.h> |
0eeca283 | 27 | #include <linux/fsnotify.h> |
1da177e4 LT |
28 | #include <linux/personality.h> |
29 | #include <linux/security.h> | |
30 | #include <linux/syscalls.h> | |
31 | #include <linux/mount.h> | |
32 | #include <linux/audit.h> | |
16f7e0fe | 33 | #include <linux/capability.h> |
834f2a4a | 34 | #include <linux/file.h> |
5590ff0d | 35 | #include <linux/fcntl.h> |
08ce5f16 | 36 | #include <linux/device_cgroup.h> |
5ad4e53b | 37 | #include <linux/fs_struct.h> |
e77819e5 | 38 | #include <linux/posix_acl.h> |
99d263d4 | 39 | #include <linux/hash.h> |
2a18da7a | 40 | #include <linux/bitops.h> |
aeaa4a79 | 41 | #include <linux/init_task.h> |
7c0f6ba6 | 42 | #include <linux/uaccess.h> |
1da177e4 | 43 | |
e81e3f4d | 44 | #include "internal.h" |
c7105365 | 45 | #include "mount.h" |
e81e3f4d | 46 | |
1da177e4 LT |
47 | /* [Feb-1997 T. Schoebel-Theuer] |
48 | * Fundamental changes in the pathname lookup mechanisms (namei) | |
49 | * were necessary because of omirr. The reason is that omirr needs | |
50 | * to know the _real_ pathname, not the user-supplied one, in case | |
51 | * of symlinks (and also when transname replacements occur). | |
52 | * | |
53 | * The new code replaces the old recursive symlink resolution with | |
54 | * an iterative one (in case of non-nested symlink chains). It does | |
55 | * this with calls to <fs>_follow_link(). | |
56 | * As a side effect, dir_namei(), _namei() and follow_link() are now | |
57 | * replaced with a single function lookup_dentry() that can handle all | |
58 | * the special cases of the former code. | |
59 | * | |
60 | * With the new dcache, the pathname is stored at each inode, at least as | |
61 | * long as the refcount of the inode is positive. As a side effect, the | |
62 | * size of the dcache depends on the inode cache and thus is dynamic. | |
63 | * | |
64 | * [29-Apr-1998 C. Scott Ananian] Updated above description of symlink | |
65 | * resolution to correspond with current state of the code. | |
66 | * | |
67 | * Note that the symlink resolution is not *completely* iterative. | |
68 | * There is still a significant amount of tail- and mid- recursion in | |
69 | * the algorithm. Also, note that <fs>_readlink() is not used in | |
70 | * lookup_dentry(): lookup_dentry() on the result of <fs>_readlink() | |
71 | * may return different results than <fs>_follow_link(). Many virtual | |
72 | * filesystems (including /proc) exhibit this behavior. | |
73 | */ | |
74 | ||
75 | /* [24-Feb-97 T. Schoebel-Theuer] Side effects caused by new implementation: | |
76 | * New symlink semantics: when open() is called with flags O_CREAT | O_EXCL | |
77 | * and the name already exists in form of a symlink, try to create the new | |
78 | * name indicated by the symlink. The old code always complained that the | |
79 | * name already exists, due to not following the symlink even if its target | |
80 | * is nonexistent. The new semantics affects also mknod() and link() when | |
25985edc | 81 | * the name is a symlink pointing to a non-existent name. |
1da177e4 LT |
82 | * |
83 | * I don't know which semantics is the right one, since I have no access | |
84 | * to standards. But I found by trial that HP-UX 9.0 has the full "new" | |
85 | * semantics implemented, while SunOS 4.1.1 and Solaris (SunOS 5.4) have the | |
86 | * "old" one. Personally, I think the new semantics is much more logical. | |
87 | * Note that "ln old new" where "new" is a symlink pointing to a non-existing | |
88 | * file does succeed in both HP-UX and SunOs, but not in Solaris | |
89 | * and in the old Linux semantics. | |
90 | */ | |
91 | ||
92 | /* [16-Dec-97 Kevin Buhr] For security reasons, we change some symlink | |
93 | * semantics. See the comments in "open_namei" and "do_link" below. | |
94 | * | |
95 | * [10-Sep-98 Alan Modra] Another symlink change. | |
96 | */ | |
97 | ||
98 | /* [Feb-Apr 2000 AV] Complete rewrite. Rules for symlinks: | |
99 | * inside the path - always follow. | |
100 | * in the last component in creation/removal/renaming - never follow. | |
101 | * if LOOKUP_FOLLOW passed - follow. | |
102 | * if the pathname has trailing slashes - follow. | |
103 | * otherwise - don't follow. | |
104 | * (applied in that order). | |
105 | * | |
106 | * [Jun 2000 AV] Inconsistent behaviour of open() in case if flags==O_CREAT | |
107 | * restored for 2.4. This is the last surviving part of old 4.2BSD bug. | |
108 | * During the 2.4 we need to fix the userland stuff depending on it - | |
109 | * hopefully we will be able to get rid of that wart in 2.5. So far only | |
110 | * XEmacs seems to be relying on it... | |
111 | */ | |
112 | /* | |
113 | * [Sep 2001 AV] Single-semaphore locking scheme (kudos to David Holland) | |
a11f3a05 | 114 | * implemented. Let's see if raised priority of ->s_vfs_rename_mutex gives |
1da177e4 LT |
115 | * any extra contention... |
116 | */ | |
117 | ||
118 | /* In order to reduce some races, while at the same time doing additional | |
119 | * checking and hopefully speeding things up, we copy filenames to the | |
120 | * kernel data space before using them.. | |
121 | * | |
122 | * POSIX.1 2.4: an empty pathname is invalid (ENOENT). | |
123 | * PATH_MAX includes the nul terminator --RR. | |
124 | */ | |
91a27b2a | 125 | |
fd2f7cb5 | 126 | #define EMBEDDED_NAME_MAX (PATH_MAX - offsetof(struct filename, iname)) |
7950e385 | 127 | |
b463d7fd | 128 | static inline void initname(struct filename *name, const char __user *uptr) |
61185101 | 129 | { |
b463d7fd | 130 | name->uptr = uptr; |
61185101 MG |
131 | name->aname = NULL; |
132 | atomic_set(&name->refcnt, 1); | |
133 | } | |
134 | ||
51f39a1f | 135 | struct filename * |
dff60734 | 136 | getname_flags(const char __user *filename, int flags) |
91a27b2a | 137 | { |
94b5d262 | 138 | struct filename *result; |
7950e385 | 139 | char *kname; |
94b5d262 | 140 | int len; |
4043cde8 | 141 | |
7ac86265 JL |
142 | result = audit_reusename(filename); |
143 | if (result) | |
144 | return result; | |
145 | ||
7950e385 | 146 | result = __getname(); |
3f9f0aa6 | 147 | if (unlikely(!result)) |
4043cde8 EP |
148 | return ERR_PTR(-ENOMEM); |
149 | ||
7950e385 JL |
150 | /* |
151 | * First, try to embed the struct filename inside the names_cache | |
152 | * allocation | |
153 | */ | |
fd2f7cb5 | 154 | kname = (char *)result->iname; |
91a27b2a | 155 | result->name = kname; |
7950e385 | 156 | |
94b5d262 | 157 | len = strncpy_from_user(kname, filename, EMBEDDED_NAME_MAX); |
d4f50ea9 MG |
158 | /* |
159 | * Handle both empty path and copy failure in one go. | |
160 | */ | |
161 | if (unlikely(len <= 0)) { | |
162 | if (unlikely(len < 0)) { | |
163 | __putname(result); | |
164 | return ERR_PTR(len); | |
165 | } | |
166 | ||
167 | /* The empty path is special. */ | |
168 | if (!(flags & LOOKUP_EMPTY)) { | |
169 | __putname(result); | |
170 | return ERR_PTR(-ENOENT); | |
171 | } | |
91a27b2a | 172 | } |
3f9f0aa6 | 173 | |
7950e385 JL |
174 | /* |
175 | * Uh-oh. We have a name that's approaching PATH_MAX. Allocate a | |
176 | * separate struct filename so we can dedicate the entire | |
177 | * names_cache allocation for the pathname, and re-do the copy from | |
178 | * userland. | |
179 | */ | |
94b5d262 | 180 | if (unlikely(len == EMBEDDED_NAME_MAX)) { |
fd2f7cb5 | 181 | const size_t size = offsetof(struct filename, iname[1]); |
7950e385 JL |
182 | kname = (char *)result; |
183 | ||
fd2f7cb5 AV |
184 | /* |
185 | * size is chosen that way we to guarantee that | |
186 | * result->iname[0] is within the same object and that | |
187 | * kname can't be equal to result->iname, no matter what. | |
188 | */ | |
189 | result = kzalloc(size, GFP_KERNEL); | |
94b5d262 AV |
190 | if (unlikely(!result)) { |
191 | __putname(kname); | |
192 | return ERR_PTR(-ENOMEM); | |
7950e385 JL |
193 | } |
194 | result->name = kname; | |
94b5d262 AV |
195 | len = strncpy_from_user(kname, filename, PATH_MAX); |
196 | if (unlikely(len < 0)) { | |
197 | __putname(kname); | |
198 | kfree(result); | |
199 | return ERR_PTR(len); | |
200 | } | |
d4f50ea9 MG |
201 | /* The empty path is special. */ |
202 | if (unlikely(!len) && !(flags & LOOKUP_EMPTY)) { | |
203 | __putname(kname); | |
204 | kfree(result); | |
205 | return ERR_PTR(-ENOENT); | |
206 | } | |
94b5d262 AV |
207 | if (unlikely(len == PATH_MAX)) { |
208 | __putname(kname); | |
209 | kfree(result); | |
210 | return ERR_PTR(-ENAMETOOLONG); | |
211 | } | |
7950e385 | 212 | } |
b463d7fd | 213 | initname(result, filename); |
7950e385 JL |
214 | audit_getname(result); |
215 | return result; | |
1da177e4 LT |
216 | } |
217 | ||
e896474f | 218 | struct filename *getname_uflags(const char __user *filename, int uflags) |
8228e2c3 DK |
219 | { |
220 | int flags = (uflags & AT_EMPTY_PATH) ? LOOKUP_EMPTY : 0; | |
221 | ||
dff60734 | 222 | return getname_flags(filename, flags); |
8228e2c3 DK |
223 | } |
224 | ||
e896474f AV |
225 | struct filename *__getname_maybe_null(const char __user *pathname) |
226 | { | |
227 | struct filename *name; | |
228 | char c; | |
229 | ||
230 | /* try to save on allocations; loss on um, though */ | |
231 | if (get_user(c, pathname)) | |
232 | return ERR_PTR(-EFAULT); | |
233 | if (!c) | |
234 | return NULL; | |
235 | ||
236 | name = getname_flags(pathname, LOOKUP_EMPTY); | |
237 | if (!IS_ERR(name) && !(name->name[0])) { | |
238 | putname(name); | |
239 | name = NULL; | |
240 | } | |
241 | return name; | |
242 | } | |
243 | ||
244 | struct filename *getname_kernel(const char * filename) | |
c4ad8f98 LT |
245 | { |
246 | struct filename *result; | |
08518549 | 247 | int len = strlen(filename) + 1; |
c4ad8f98 LT |
248 | |
249 | result = __getname(); | |
250 | if (unlikely(!result)) | |
251 | return ERR_PTR(-ENOMEM); | |
252 | ||
08518549 | 253 | if (len <= EMBEDDED_NAME_MAX) { |
fd2f7cb5 | 254 | result->name = (char *)result->iname; |
08518549 | 255 | } else if (len <= PATH_MAX) { |
30ce4d19 | 256 | const size_t size = offsetof(struct filename, iname[1]); |
08518549 PM |
257 | struct filename *tmp; |
258 | ||
30ce4d19 | 259 | tmp = kmalloc(size, GFP_KERNEL); |
08518549 PM |
260 | if (unlikely(!tmp)) { |
261 | __putname(result); | |
262 | return ERR_PTR(-ENOMEM); | |
263 | } | |
264 | tmp->name = (char *)result; | |
08518549 PM |
265 | result = tmp; |
266 | } else { | |
267 | __putname(result); | |
268 | return ERR_PTR(-ENAMETOOLONG); | |
269 | } | |
270 | memcpy((char *)result->name, filename, len); | |
b463d7fd | 271 | initname(result, NULL); |
fd3522fd | 272 | audit_getname(result); |
c4ad8f98 LT |
273 | return result; |
274 | } | |
74d7970f | 275 | EXPORT_SYMBOL(getname_kernel); |
c4ad8f98 | 276 | |
91a27b2a | 277 | void putname(struct filename *name) |
1da177e4 | 278 | { |
86f40fa6 MG |
279 | int refcnt; |
280 | ||
e896474f | 281 | if (IS_ERR_OR_NULL(name)) |
91ef658f DK |
282 | return; |
283 | ||
86f40fa6 MG |
284 | refcnt = atomic_read(&name->refcnt); |
285 | if (refcnt != 1) { | |
286 | if (WARN_ON_ONCE(!refcnt)) | |
287 | return; | |
55422d0b | 288 | |
86f40fa6 MG |
289 | if (!atomic_dec_and_test(&name->refcnt)) |
290 | return; | |
291 | } | |
55422d0b | 292 | |
fd2f7cb5 | 293 | if (name->name != name->iname) { |
55422d0b PM |
294 | __putname(name->name); |
295 | kfree(name); | |
296 | } else | |
297 | __putname(name); | |
1da177e4 | 298 | } |
74d7970f | 299 | EXPORT_SYMBOL(putname); |
1da177e4 | 300 | |
47291baa CB |
301 | /** |
302 | * check_acl - perform ACL permission checking | |
700b7940 | 303 | * @idmap: idmap of the mount the inode was found from |
47291baa CB |
304 | * @inode: inode to check permissions on |
305 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) | |
306 | * | |
307 | * This function performs the ACL permission checking. Since this function | |
308 | * retrieve POSIX acls it needs to know whether it is called from a blocking or | |
309 | * non-blocking context and thus cares about the MAY_NOT_BLOCK bit. | |
310 | * | |
700b7940 CB |
311 | * If the inode has been found through an idmapped mount the idmap of |
312 | * the vfsmount must be passed through @idmap. This function will then take | |
313 | * care to map the inode according to @idmap before checking permissions. | |
47291baa | 314 | * On non-idmapped mounts or if permission checking is to be performed on the |
376870aa | 315 | * raw inode simply pass @nop_mnt_idmap. |
47291baa | 316 | */ |
700b7940 | 317 | static int check_acl(struct mnt_idmap *idmap, |
47291baa | 318 | struct inode *inode, int mask) |
e77819e5 | 319 | { |
84635d68 | 320 | #ifdef CONFIG_FS_POSIX_ACL |
e77819e5 LT |
321 | struct posix_acl *acl; |
322 | ||
e77819e5 | 323 | if (mask & MAY_NOT_BLOCK) { |
3567866b AV |
324 | acl = get_cached_acl_rcu(inode, ACL_TYPE_ACCESS); |
325 | if (!acl) | |
e77819e5 | 326 | return -EAGAIN; |
cac2f8b8 | 327 | /* no ->get_inode_acl() calls in RCU mode... */ |
b8a7a3a6 | 328 | if (is_uncached_acl(acl)) |
3567866b | 329 | return -ECHILD; |
700b7940 | 330 | return posix_acl_permission(idmap, inode, acl, mask); |
e77819e5 LT |
331 | } |
332 | ||
cac2f8b8 | 333 | acl = get_inode_acl(inode, ACL_TYPE_ACCESS); |
2982baa2 CH |
334 | if (IS_ERR(acl)) |
335 | return PTR_ERR(acl); | |
e77819e5 | 336 | if (acl) { |
700b7940 | 337 | int error = posix_acl_permission(idmap, inode, acl, mask); |
e77819e5 LT |
338 | posix_acl_release(acl); |
339 | return error; | |
340 | } | |
84635d68 | 341 | #endif |
e77819e5 LT |
342 | |
343 | return -EAGAIN; | |
344 | } | |
345 | ||
cb80d907 LT |
346 | /* |
347 | * Very quick optimistic "we know we have no ACL's" check. | |
348 | * | |
349 | * Note that this is purely for ACL_TYPE_ACCESS, and purely | |
350 | * for the "we have cached that there are no ACLs" case. | |
351 | * | |
352 | * If this returns true, we know there are no ACLs. But if | |
353 | * it returns false, we might still not have ACLs (it could | |
354 | * be the is_uncached_acl() case). | |
355 | */ | |
356 | static inline bool no_acl_inode(struct inode *inode) | |
357 | { | |
358 | #ifdef CONFIG_FS_POSIX_ACL | |
359 | return likely(!READ_ONCE(inode->i_acl)); | |
360 | #else | |
361 | return true; | |
362 | #endif | |
363 | } | |
364 | ||
47291baa CB |
365 | /** |
366 | * acl_permission_check - perform basic UNIX permission checking | |
700b7940 | 367 | * @idmap: idmap of the mount the inode was found from |
47291baa CB |
368 | * @inode: inode to check permissions on |
369 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) | |
370 | * | |
371 | * This function performs the basic UNIX permission checking. Since this | |
372 | * function may retrieve POSIX acls it needs to know whether it is called from a | |
373 | * blocking or non-blocking context and thus cares about the MAY_NOT_BLOCK bit. | |
5fc475b7 | 374 | * |
700b7940 CB |
375 | * If the inode has been found through an idmapped mount the idmap of |
376 | * the vfsmount must be passed through @idmap. This function will then take | |
377 | * care to map the inode according to @idmap before checking permissions. | |
47291baa | 378 | * On non-idmapped mounts or if permission checking is to be performed on the |
376870aa | 379 | * raw inode simply pass @nop_mnt_idmap. |
1da177e4 | 380 | */ |
700b7940 | 381 | static int acl_permission_check(struct mnt_idmap *idmap, |
47291baa | 382 | struct inode *inode, int mask) |
1da177e4 | 383 | { |
26cf46be | 384 | unsigned int mode = inode->i_mode; |
a2bd096f | 385 | vfsuid_t vfsuid; |
1da177e4 | 386 | |
cb80d907 LT |
387 | /* |
388 | * Common cheap case: everybody has the requested | |
389 | * rights, and there are no ACLs to check. No need | |
390 | * to do any owner/group checks in that case. | |
391 | * | |
392 | * - 'mask&7' is the requested permission bit set | |
393 | * - multiplying by 0111 spreads them out to all of ugo | |
394 | * - '& ~mode' looks for missing inode permission bits | |
395 | * - the '!' is for "no missing permissions" | |
396 | * | |
397 | * After that, we just need to check that there are no | |
398 | * ACL's on the inode - do the 'IS_POSIXACL()' check last | |
399 | * because it will dereference the ->i_sb pointer and we | |
400 | * want to avoid that if at all possible. | |
401 | */ | |
402 | if (!((mask & 7) * 0111 & ~mode)) { | |
403 | if (no_acl_inode(inode)) | |
404 | return 0; | |
405 | if (!IS_POSIXACL(inode)) | |
406 | return 0; | |
407 | } | |
408 | ||
5fc475b7 | 409 | /* Are we the owner? If so, ACL's don't matter */ |
e67fe633 | 410 | vfsuid = i_uid_into_vfsuid(idmap, inode); |
a2bd096f | 411 | if (likely(vfsuid_eq_kuid(vfsuid, current_fsuid()))) { |
5fc475b7 | 412 | mask &= 7; |
1da177e4 | 413 | mode >>= 6; |
5fc475b7 LT |
414 | return (mask & ~mode) ? -EACCES : 0; |
415 | } | |
1da177e4 | 416 | |
5fc475b7 LT |
417 | /* Do we have ACL's? */ |
418 | if (IS_POSIXACL(inode) && (mode & S_IRWXG)) { | |
700b7940 | 419 | int error = check_acl(idmap, inode, mask); |
5fc475b7 LT |
420 | if (error != -EAGAIN) |
421 | return error; | |
1da177e4 LT |
422 | } |
423 | ||
5fc475b7 LT |
424 | /* Only RWX matters for group/other mode bits */ |
425 | mask &= 7; | |
426 | ||
1da177e4 | 427 | /* |
5fc475b7 LT |
428 | * Are the group permissions different from |
429 | * the other permissions in the bits we care | |
430 | * about? Need to check group ownership if so. | |
1da177e4 | 431 | */ |
5fc475b7 | 432 | if (mask & (mode ^ (mode >> 3))) { |
e67fe633 | 433 | vfsgid_t vfsgid = i_gid_into_vfsgid(idmap, inode); |
a2bd096f | 434 | if (vfsgid_in_group_p(vfsgid)) |
5fc475b7 LT |
435 | mode >>= 3; |
436 | } | |
437 | ||
438 | /* Bits in 'mode' clear that we require? */ | |
439 | return (mask & ~mode) ? -EACCES : 0; | |
5909ccaa LT |
440 | } |
441 | ||
442 | /** | |
b74c79e9 | 443 | * generic_permission - check for access rights on a Posix-like filesystem |
4609e1f1 | 444 | * @idmap: idmap of the mount the inode was found from |
5909ccaa | 445 | * @inode: inode to check access rights for |
5fc475b7 LT |
446 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC, |
447 | * %MAY_NOT_BLOCK ...) | |
5909ccaa LT |
448 | * |
449 | * Used to check for read/write/execute permissions on a file. | |
450 | * We use "fsuid" for this, letting us set arbitrary permissions | |
451 | * for filesystem access without changing the "normal" uids which | |
b74c79e9 NP |
452 | * are used for other things. |
453 | * | |
454 | * generic_permission is rcu-walk aware. It returns -ECHILD in case an rcu-walk | |
455 | * request cannot be satisfied (eg. requires blocking or too much complexity). | |
456 | * It would then be called again in ref-walk mode. | |
47291baa | 457 | * |
4609e1f1 CB |
458 | * If the inode has been found through an idmapped mount the idmap of |
459 | * the vfsmount must be passed through @idmap. This function will then take | |
460 | * care to map the inode according to @idmap before checking permissions. | |
47291baa | 461 | * On non-idmapped mounts or if permission checking is to be performed on the |
376870aa | 462 | * raw inode simply pass @nop_mnt_idmap. |
5909ccaa | 463 | */ |
4609e1f1 | 464 | int generic_permission(struct mnt_idmap *idmap, struct inode *inode, |
47291baa | 465 | int mask) |
5909ccaa LT |
466 | { |
467 | int ret; | |
468 | ||
469 | /* | |
948409c7 | 470 | * Do the basic permission checks. |
5909ccaa | 471 | */ |
700b7940 | 472 | ret = acl_permission_check(idmap, inode, mask); |
5909ccaa LT |
473 | if (ret != -EACCES) |
474 | return ret; | |
1da177e4 | 475 | |
d594e7ec AV |
476 | if (S_ISDIR(inode->i_mode)) { |
477 | /* DACs are overridable for directories */ | |
d594e7ec | 478 | if (!(mask & MAY_WRITE)) |
9452e93e | 479 | if (capable_wrt_inode_uidgid(idmap, inode, |
23adbe12 | 480 | CAP_DAC_READ_SEARCH)) |
d594e7ec | 481 | return 0; |
9452e93e | 482 | if (capable_wrt_inode_uidgid(idmap, inode, |
0558c1bf | 483 | CAP_DAC_OVERRIDE)) |
1da177e4 | 484 | return 0; |
2a4c2242 SS |
485 | return -EACCES; |
486 | } | |
1da177e4 LT |
487 | |
488 | /* | |
489 | * Searching includes executable on directories, else just read. | |
490 | */ | |
7ea66001 | 491 | mask &= MAY_READ | MAY_WRITE | MAY_EXEC; |
d594e7ec | 492 | if (mask == MAY_READ) |
9452e93e | 493 | if (capable_wrt_inode_uidgid(idmap, inode, |
0558c1bf | 494 | CAP_DAC_READ_SEARCH)) |
1da177e4 | 495 | return 0; |
2a4c2242 SS |
496 | /* |
497 | * Read/write DACs are always overridable. | |
498 | * Executable DACs are overridable when there is | |
499 | * at least one exec bit set. | |
500 | */ | |
501 | if (!(mask & MAY_EXEC) || (inode->i_mode & S_IXUGO)) | |
9452e93e | 502 | if (capable_wrt_inode_uidgid(idmap, inode, |
0558c1bf | 503 | CAP_DAC_OVERRIDE)) |
2a4c2242 | 504 | return 0; |
1da177e4 LT |
505 | |
506 | return -EACCES; | |
507 | } | |
4d359507 | 508 | EXPORT_SYMBOL(generic_permission); |
1da177e4 | 509 | |
47291baa CB |
510 | /** |
511 | * do_inode_permission - UNIX permission checking | |
4609e1f1 | 512 | * @idmap: idmap of the mount the inode was found from |
47291baa CB |
513 | * @inode: inode to check permissions on |
514 | * @mask: right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC ...) | |
515 | * | |
3ddcd056 LT |
516 | * We _really_ want to just do "generic_permission()" without |
517 | * even looking at the inode->i_op values. So we keep a cache | |
518 | * flag in inode->i_opflags, that says "this has not special | |
519 | * permission function, use the fast case". | |
520 | */ | |
4609e1f1 | 521 | static inline int do_inode_permission(struct mnt_idmap *idmap, |
47291baa | 522 | struct inode *inode, int mask) |
3ddcd056 LT |
523 | { |
524 | if (unlikely(!(inode->i_opflags & IOP_FASTPERM))) { | |
525 | if (likely(inode->i_op->permission)) | |
4609e1f1 | 526 | return inode->i_op->permission(idmap, inode, mask); |
3ddcd056 LT |
527 | |
528 | /* This gets set once for the inode lifetime */ | |
529 | spin_lock(&inode->i_lock); | |
530 | inode->i_opflags |= IOP_FASTPERM; | |
531 | spin_unlock(&inode->i_lock); | |
532 | } | |
4609e1f1 | 533 | return generic_permission(idmap, inode, mask); |
3ddcd056 LT |
534 | } |
535 | ||
0bdaea90 DH |
536 | /** |
537 | * sb_permission - Check superblock-level permissions | |
538 | * @sb: Superblock of inode to check permission on | |
55852635 | 539 | * @inode: Inode to check permission on |
0bdaea90 DH |
540 | * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) |
541 | * | |
542 | * Separate out file-system wide checks from inode-specific permission checks. | |
543 | */ | |
544 | static int sb_permission(struct super_block *sb, struct inode *inode, int mask) | |
545 | { | |
546 | if (unlikely(mask & MAY_WRITE)) { | |
547 | umode_t mode = inode->i_mode; | |
548 | ||
549 | /* Nobody gets write access to a read-only fs. */ | |
bc98a42c | 550 | if (sb_rdonly(sb) && (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) |
0bdaea90 DH |
551 | return -EROFS; |
552 | } | |
553 | return 0; | |
554 | } | |
555 | ||
556 | /** | |
557 | * inode_permission - Check for access rights to a given inode | |
4609e1f1 | 558 | * @idmap: idmap of the mount the inode was found from |
47291baa CB |
559 | * @inode: Inode to check permission on |
560 | * @mask: Right to check for (%MAY_READ, %MAY_WRITE, %MAY_EXEC) | |
0bdaea90 DH |
561 | * |
562 | * Check for read/write/execute permissions on an inode. We use fs[ug]id for | |
563 | * this, letting us set arbitrary permissions for filesystem access without | |
564 | * changing the "normal" UIDs which are used for other things. | |
565 | * | |
566 | * When checking for MAY_APPEND, MAY_WRITE must also be set in @mask. | |
567 | */ | |
4609e1f1 | 568 | int inode_permission(struct mnt_idmap *idmap, |
47291baa | 569 | struct inode *inode, int mask) |
0bdaea90 DH |
570 | { |
571 | int retval; | |
572 | ||
573 | retval = sb_permission(inode->i_sb, inode, mask); | |
875ccc0d | 574 | if (unlikely(retval)) |
0bdaea90 | 575 | return retval; |
4bfd054a EB |
576 | |
577 | if (unlikely(mask & MAY_WRITE)) { | |
578 | /* | |
579 | * Nobody gets write access to an immutable file. | |
580 | */ | |
875ccc0d | 581 | if (unlikely(IS_IMMUTABLE(inode))) |
4bfd054a EB |
582 | return -EPERM; |
583 | ||
584 | /* | |
585 | * Updating mtime will likely cause i_uid and i_gid to be | |
586 | * written back improperly if their true value is unknown | |
587 | * to the vfs. | |
588 | */ | |
875ccc0d | 589 | if (unlikely(HAS_UNMAPPED_ID(idmap, inode))) |
4bfd054a EB |
590 | return -EACCES; |
591 | } | |
592 | ||
4609e1f1 | 593 | retval = do_inode_permission(idmap, inode, mask); |
875ccc0d | 594 | if (unlikely(retval)) |
4bfd054a EB |
595 | return retval; |
596 | ||
597 | retval = devcgroup_inode_permission(inode, mask); | |
875ccc0d | 598 | if (unlikely(retval)) |
4bfd054a EB |
599 | return retval; |
600 | ||
601 | return security_inode_permission(inode, mask); | |
0bdaea90 | 602 | } |
4d359507 | 603 | EXPORT_SYMBOL(inode_permission); |
0bdaea90 | 604 | |
5dd784d0 JB |
605 | /** |
606 | * path_get - get a reference to a path | |
607 | * @path: path to get the reference to | |
608 | * | |
609 | * Given a path increment the reference count to the dentry and the vfsmount. | |
610 | */ | |
dcf787f3 | 611 | void path_get(const struct path *path) |
5dd784d0 JB |
612 | { |
613 | mntget(path->mnt); | |
614 | dget(path->dentry); | |
615 | } | |
616 | EXPORT_SYMBOL(path_get); | |
617 | ||
1d957f9b JB |
618 | /** |
619 | * path_put - put a reference to a path | |
620 | * @path: path to put the reference to | |
621 | * | |
622 | * Given a path decrement the reference count to the dentry and the vfsmount. | |
623 | */ | |
dcf787f3 | 624 | void path_put(const struct path *path) |
1da177e4 | 625 | { |
1d957f9b JB |
626 | dput(path->dentry); |
627 | mntput(path->mnt); | |
1da177e4 | 628 | } |
1d957f9b | 629 | EXPORT_SYMBOL(path_put); |
1da177e4 | 630 | |
894bc8c4 | 631 | #define EMBEDDED_LEVELS 2 |
1f55a6ec AV |
632 | struct nameidata { |
633 | struct path path; | |
1cf2665b | 634 | struct qstr last; |
1f55a6ec AV |
635 | struct path root; |
636 | struct inode *inode; /* path.dentry.d_inode */ | |
bcba1e7d | 637 | unsigned int flags, state; |
03fa86e9 | 638 | unsigned seq, next_seq, m_seq, r_seq; |
1f55a6ec AV |
639 | int last_type; |
640 | unsigned depth; | |
756daf26 | 641 | int total_link_count; |
697fc6ca AV |
642 | struct saved { |
643 | struct path link; | |
fceef393 | 644 | struct delayed_call done; |
697fc6ca | 645 | const char *name; |
0450b2d1 | 646 | unsigned seq; |
894bc8c4 | 647 | } *stack, internal[EMBEDDED_LEVELS]; |
9883d185 | 648 | struct filename *name; |
5b313bcb | 649 | const char *pathname; |
9883d185 AV |
650 | struct nameidata *saved; |
651 | unsigned root_seq; | |
652 | int dfd; | |
a2bd096f | 653 | vfsuid_t dir_vfsuid; |
0f705953 | 654 | umode_t dir_mode; |
3859a271 | 655 | } __randomize_layout; |
1f55a6ec | 656 | |
bcba1e7d AV |
657 | #define ND_ROOT_PRESET 1 |
658 | #define ND_ROOT_GRABBED 2 | |
659 | #define ND_JUMPED 4 | |
660 | ||
06422964 | 661 | static void __set_nameidata(struct nameidata *p, int dfd, struct filename *name) |
894bc8c4 | 662 | { |
756daf26 N |
663 | struct nameidata *old = current->nameidata; |
664 | p->stack = p->internal; | |
7962c7d1 | 665 | p->depth = 0; |
c8a53ee5 AV |
666 | p->dfd = dfd; |
667 | p->name = name; | |
5b313bcb | 668 | p->pathname = likely(name) ? name->name : ""; |
7d01ef75 AV |
669 | p->path.mnt = NULL; |
670 | p->path.dentry = NULL; | |
756daf26 | 671 | p->total_link_count = old ? old->total_link_count : 0; |
9883d185 | 672 | p->saved = old; |
756daf26 | 673 | current->nameidata = p; |
894bc8c4 AV |
674 | } |
675 | ||
06422964 AV |
676 | static inline void set_nameidata(struct nameidata *p, int dfd, struct filename *name, |
677 | const struct path *root) | |
678 | { | |
679 | __set_nameidata(p, dfd, name); | |
680 | p->state = 0; | |
681 | if (unlikely(root)) { | |
682 | p->state = ND_ROOT_PRESET; | |
683 | p->root = *root; | |
684 | } | |
685 | } | |
686 | ||
9883d185 | 687 | static void restore_nameidata(void) |
894bc8c4 | 688 | { |
9883d185 | 689 | struct nameidata *now = current->nameidata, *old = now->saved; |
756daf26 N |
690 | |
691 | current->nameidata = old; | |
692 | if (old) | |
693 | old->total_link_count = now->total_link_count; | |
e1a63bbc | 694 | if (now->stack != now->internal) |
756daf26 | 695 | kfree(now->stack); |
894bc8c4 AV |
696 | } |
697 | ||
60ef60c7 | 698 | static bool nd_alloc_stack(struct nameidata *nd) |
894bc8c4 | 699 | { |
bc40aee0 AV |
700 | struct saved *p; |
701 | ||
60ef60c7 AV |
702 | p= kmalloc_array(MAXSYMLINKS, sizeof(struct saved), |
703 | nd->flags & LOOKUP_RCU ? GFP_ATOMIC : GFP_KERNEL); | |
704 | if (unlikely(!p)) | |
705 | return false; | |
894bc8c4 AV |
706 | memcpy(p, nd->internal, sizeof(nd->internal)); |
707 | nd->stack = p; | |
60ef60c7 | 708 | return true; |
894bc8c4 AV |
709 | } |
710 | ||
397d425d | 711 | /** |
6b03f7ed | 712 | * path_connected - Verify that a dentry is below mnt.mnt_root |
35931eb3 MWO |
713 | * @mnt: The mountpoint to check. |
714 | * @dentry: The dentry to check. | |
397d425d EB |
715 | * |
716 | * Rename can sometimes move a file or directory outside of a bind | |
717 | * mount, path_connected allows those cases to be detected. | |
718 | */ | |
6b03f7ed | 719 | static bool path_connected(struct vfsmount *mnt, struct dentry *dentry) |
397d425d | 720 | { |
95dd7758 | 721 | struct super_block *sb = mnt->mnt_sb; |
397d425d | 722 | |
402dd2cf CH |
723 | /* Bind mounts can have disconnected paths */ |
724 | if (mnt->mnt_root == sb->s_root) | |
397d425d EB |
725 | return true; |
726 | ||
6b03f7ed | 727 | return is_subdir(dentry, mnt->mnt_root); |
397d425d EB |
728 | } |
729 | ||
7973387a AV |
730 | static void drop_links(struct nameidata *nd) |
731 | { | |
732 | int i = nd->depth; | |
733 | while (i--) { | |
734 | struct saved *last = nd->stack + i; | |
fceef393 AV |
735 | do_delayed_call(&last->done); |
736 | clear_delayed_call(&last->done); | |
7973387a AV |
737 | } |
738 | } | |
739 | ||
6e180327 AV |
740 | static void leave_rcu(struct nameidata *nd) |
741 | { | |
742 | nd->flags &= ~LOOKUP_RCU; | |
03fa86e9 | 743 | nd->seq = nd->next_seq = 0; |
6e180327 AV |
744 | rcu_read_unlock(); |
745 | } | |
746 | ||
7973387a AV |
747 | static void terminate_walk(struct nameidata *nd) |
748 | { | |
749 | drop_links(nd); | |
750 | if (!(nd->flags & LOOKUP_RCU)) { | |
751 | int i; | |
752 | path_put(&nd->path); | |
753 | for (i = 0; i < nd->depth; i++) | |
754 | path_put(&nd->stack[i].link); | |
bcba1e7d | 755 | if (nd->state & ND_ROOT_GRABBED) { |
102b8af2 | 756 | path_put(&nd->root); |
bcba1e7d | 757 | nd->state &= ~ND_ROOT_GRABBED; |
102b8af2 | 758 | } |
7973387a | 759 | } else { |
6e180327 | 760 | leave_rcu(nd); |
7973387a AV |
761 | } |
762 | nd->depth = 0; | |
7d01ef75 AV |
763 | nd->path.mnt = NULL; |
764 | nd->path.dentry = NULL; | |
7973387a AV |
765 | } |
766 | ||
767 | /* path_put is needed afterwards regardless of success or failure */ | |
2aa38470 | 768 | static bool __legitimize_path(struct path *path, unsigned seq, unsigned mseq) |
7973387a | 769 | { |
2aa38470 | 770 | int res = __legitimize_mnt(path->mnt, mseq); |
7973387a AV |
771 | if (unlikely(res)) { |
772 | if (res > 0) | |
773 | path->mnt = NULL; | |
774 | path->dentry = NULL; | |
775 | return false; | |
776 | } | |
777 | if (unlikely(!lockref_get_not_dead(&path->dentry->d_lockref))) { | |
778 | path->dentry = NULL; | |
779 | return false; | |
780 | } | |
781 | return !read_seqcount_retry(&path->dentry->d_seq, seq); | |
782 | } | |
783 | ||
2aa38470 AV |
784 | static inline bool legitimize_path(struct nameidata *nd, |
785 | struct path *path, unsigned seq) | |
786 | { | |
5bd73286 | 787 | return __legitimize_path(path, seq, nd->m_seq); |
2aa38470 AV |
788 | } |
789 | ||
7973387a AV |
790 | static bool legitimize_links(struct nameidata *nd) |
791 | { | |
792 | int i; | |
eacd9aa8 AV |
793 | if (unlikely(nd->flags & LOOKUP_CACHED)) { |
794 | drop_links(nd); | |
795 | nd->depth = 0; | |
796 | return false; | |
797 | } | |
7973387a AV |
798 | for (i = 0; i < nd->depth; i++) { |
799 | struct saved *last = nd->stack + i; | |
800 | if (unlikely(!legitimize_path(nd, &last->link, last->seq))) { | |
801 | drop_links(nd); | |
802 | nd->depth = i + 1; | |
803 | return false; | |
804 | } | |
805 | } | |
806 | return true; | |
807 | } | |
808 | ||
ee594bff AV |
809 | static bool legitimize_root(struct nameidata *nd) |
810 | { | |
adb21d2b | 811 | /* Nothing to do if nd->root is zero or is managed by the VFS user. */ |
bcba1e7d | 812 | if (!nd->root.mnt || (nd->state & ND_ROOT_PRESET)) |
ee594bff | 813 | return true; |
bcba1e7d | 814 | nd->state |= ND_ROOT_GRABBED; |
ee594bff AV |
815 | return legitimize_path(nd, &nd->root, nd->root_seq); |
816 | } | |
817 | ||
19660af7 | 818 | /* |
31e6b01f | 819 | * Path walking has 2 modes, rcu-walk and ref-walk (see |
19660af7 AV |
820 | * Documentation/filesystems/path-lookup.txt). In situations when we can't |
821 | * continue in RCU mode, we attempt to drop out of rcu-walk mode and grab | |
57e3715c | 822 | * normal reference counts on dentries and vfsmounts to transition to ref-walk |
19660af7 AV |
823 | * mode. Refcounts are grabbed at the last known good point before rcu-walk |
824 | * got stuck, so ref-walk may continue from there. If this is not successful | |
825 | * (eg. a seqcount has changed), then failure is returned and it's up to caller | |
826 | * to restart the path walk from the beginning in ref-walk mode. | |
31e6b01f | 827 | */ |
31e6b01f NP |
828 | |
829 | /** | |
e36cffed | 830 | * try_to_unlazy - try to switch to ref-walk mode. |
19660af7 | 831 | * @nd: nameidata pathwalk data |
e36cffed | 832 | * Returns: true on success, false on failure |
31e6b01f | 833 | * |
e36cffed | 834 | * try_to_unlazy attempts to legitimize the current nd->path and nd->root |
4675ac39 AV |
835 | * for ref-walk mode. |
836 | * Must be called from rcu-walk context. | |
e36cffed | 837 | * Nothing should touch nameidata between try_to_unlazy() failure and |
7973387a | 838 | * terminate_walk(). |
31e6b01f | 839 | */ |
e36cffed | 840 | static bool try_to_unlazy(struct nameidata *nd) |
31e6b01f | 841 | { |
31e6b01f NP |
842 | struct dentry *parent = nd->path.dentry; |
843 | ||
844 | BUG_ON(!(nd->flags & LOOKUP_RCU)); | |
e5c832d5 | 845 | |
4675ac39 | 846 | if (unlikely(!legitimize_links(nd))) |
4675ac39 | 847 | goto out1; |
84a2bd39 AV |
848 | if (unlikely(!legitimize_path(nd, &nd->path, nd->seq))) |
849 | goto out; | |
ee594bff AV |
850 | if (unlikely(!legitimize_root(nd))) |
851 | goto out; | |
6e180327 | 852 | leave_rcu(nd); |
4675ac39 | 853 | BUG_ON(nd->inode != parent->d_inode); |
e36cffed | 854 | return true; |
4675ac39 | 855 | |
84a2bd39 | 856 | out1: |
4675ac39 AV |
857 | nd->path.mnt = NULL; |
858 | nd->path.dentry = NULL; | |
4675ac39 | 859 | out: |
6e180327 | 860 | leave_rcu(nd); |
e36cffed | 861 | return false; |
4675ac39 AV |
862 | } |
863 | ||
864 | /** | |
ae66db45 | 865 | * try_to_unlazy_next - try to switch to ref-walk mode. |
4675ac39 | 866 | * @nd: nameidata pathwalk data |
ae66db45 | 867 | * @dentry: next dentry to step into |
ae66db45 | 868 | * Returns: true on success, false on failure |
4675ac39 | 869 | * |
30476f7e | 870 | * Similar to try_to_unlazy(), but here we have the next dentry already |
ae66db45 AV |
871 | * picked by rcu-walk and want to legitimize that in addition to the current |
872 | * nd->path and nd->root for ref-walk mode. Must be called from rcu-walk context. | |
873 | * Nothing should touch nameidata between try_to_unlazy_next() failure and | |
4675ac39 AV |
874 | * terminate_walk(). |
875 | */ | |
03fa86e9 | 876 | static bool try_to_unlazy_next(struct nameidata *nd, struct dentry *dentry) |
4675ac39 | 877 | { |
7e4745a0 | 878 | int res; |
4675ac39 AV |
879 | BUG_ON(!(nd->flags & LOOKUP_RCU)); |
880 | ||
7973387a AV |
881 | if (unlikely(!legitimize_links(nd))) |
882 | goto out2; | |
7e4745a0 AV |
883 | res = __legitimize_mnt(nd->path.mnt, nd->m_seq); |
884 | if (unlikely(res)) { | |
885 | if (res > 0) | |
886 | goto out2; | |
887 | goto out1; | |
888 | } | |
4675ac39 | 889 | if (unlikely(!lockref_get_not_dead(&nd->path.dentry->d_lockref))) |
7973387a | 890 | goto out1; |
48a066e7 | 891 | |
15570086 | 892 | /* |
4675ac39 AV |
893 | * We need to move both the parent and the dentry from the RCU domain |
894 | * to be properly refcounted. And the sequence number in the dentry | |
895 | * validates *both* dentry counters, since we checked the sequence | |
896 | * number of the parent after we got the child sequence number. So we | |
897 | * know the parent must still be valid if the child sequence number is | |
15570086 | 898 | */ |
4675ac39 AV |
899 | if (unlikely(!lockref_get_not_dead(&dentry->d_lockref))) |
900 | goto out; | |
03fa86e9 | 901 | if (read_seqcount_retry(&dentry->d_seq, nd->next_seq)) |
84a2bd39 | 902 | goto out_dput; |
e5c832d5 LT |
903 | /* |
904 | * Sequence counts matched. Now make sure that the root is | |
905 | * still valid and get it if required. | |
906 | */ | |
84a2bd39 AV |
907 | if (unlikely(!legitimize_root(nd))) |
908 | goto out_dput; | |
6e180327 | 909 | leave_rcu(nd); |
ae66db45 | 910 | return true; |
19660af7 | 911 | |
7973387a AV |
912 | out2: |
913 | nd->path.mnt = NULL; | |
914 | out1: | |
915 | nd->path.dentry = NULL; | |
e5c832d5 | 916 | out: |
6e180327 | 917 | leave_rcu(nd); |
ae66db45 | 918 | return false; |
84a2bd39 | 919 | out_dput: |
6e180327 | 920 | leave_rcu(nd); |
84a2bd39 | 921 | dput(dentry); |
ae66db45 | 922 | return false; |
31e6b01f NP |
923 | } |
924 | ||
5be1fa8a AV |
925 | static inline int d_revalidate(struct inode *dir, const struct qstr *name, |
926 | struct dentry *dentry, unsigned int flags) | |
34286d66 | 927 | { |
a89f8337 | 928 | if (unlikely(dentry->d_flags & DCACHE_OP_REVALIDATE)) |
5be1fa8a | 929 | return dentry->d_op->d_revalidate(dir, name, dentry, flags); |
a89f8337 AV |
930 | else |
931 | return 1; | |
34286d66 NP |
932 | } |
933 | ||
9f1fafee AV |
934 | /** |
935 | * complete_walk - successful completion of path walk | |
936 | * @nd: pointer nameidata | |
39159de2 | 937 | * |
9f1fafee AV |
938 | * If we had been in RCU mode, drop out of it and legitimize nd->path. |
939 | * Revalidate the final result, unless we'd already done that during | |
940 | * the path walk or the filesystem doesn't ask for it. Return 0 on | |
941 | * success, -error on failure. In case of failure caller does not | |
942 | * need to drop nd->path. | |
39159de2 | 943 | */ |
9f1fafee | 944 | static int complete_walk(struct nameidata *nd) |
39159de2 | 945 | { |
16c2cd71 | 946 | struct dentry *dentry = nd->path.dentry; |
39159de2 | 947 | int status; |
39159de2 | 948 | |
9f1fafee | 949 | if (nd->flags & LOOKUP_RCU) { |
adb21d2b AS |
950 | /* |
951 | * We don't want to zero nd->root for scoped-lookups or | |
952 | * externally-managed nd->root. | |
953 | */ | |
bcba1e7d AV |
954 | if (!(nd->state & ND_ROOT_PRESET)) |
955 | if (!(nd->flags & LOOKUP_IS_SCOPED)) | |
956 | nd->root.mnt = NULL; | |
6c6ec2b0 | 957 | nd->flags &= ~LOOKUP_CACHED; |
e36cffed | 958 | if (!try_to_unlazy(nd)) |
9f1fafee | 959 | return -ECHILD; |
9f1fafee AV |
960 | } |
961 | ||
adb21d2b AS |
962 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { |
963 | /* | |
964 | * While the guarantee of LOOKUP_IS_SCOPED is (roughly) "don't | |
965 | * ever step outside the root during lookup" and should already | |
966 | * be guaranteed by the rest of namei, we want to avoid a namei | |
967 | * BUG resulting in userspace being given a path that was not | |
968 | * scoped within the root at some point during the lookup. | |
969 | * | |
970 | * So, do a final sanity-check to make sure that in the | |
971 | * worst-case scenario (a complete bypass of LOOKUP_IS_SCOPED) | |
972 | * we won't silently return an fd completely outside of the | |
973 | * requested root to userspace. | |
974 | * | |
975 | * Userspace could move the path outside the root after this | |
976 | * check, but as discussed elsewhere this is not a concern (the | |
977 | * resolved file was inside the root at some point). | |
978 | */ | |
979 | if (!path_is_under(&nd->path, &nd->root)) | |
980 | return -EXDEV; | |
981 | } | |
982 | ||
bcba1e7d | 983 | if (likely(!(nd->state & ND_JUMPED))) |
16c2cd71 AV |
984 | return 0; |
985 | ||
ecf3d1f1 | 986 | if (likely(!(dentry->d_flags & DCACHE_OP_WEAK_REVALIDATE))) |
39159de2 JL |
987 | return 0; |
988 | ||
ecf3d1f1 | 989 | status = dentry->d_op->d_weak_revalidate(dentry, nd->flags); |
39159de2 JL |
990 | if (status > 0) |
991 | return 0; | |
992 | ||
16c2cd71 | 993 | if (!status) |
39159de2 | 994 | status = -ESTALE; |
16c2cd71 | 995 | |
39159de2 JL |
996 | return status; |
997 | } | |
998 | ||
740a1678 | 999 | static int set_root(struct nameidata *nd) |
31e6b01f | 1000 | { |
7bd88377 | 1001 | struct fs_struct *fs = current->fs; |
c28cc364 | 1002 | |
adb21d2b AS |
1003 | /* |
1004 | * Jumping to the real root in a scoped-lookup is a BUG in namei, but we | |
1005 | * still have to ensure it doesn't happen because it will cause a breakout | |
1006 | * from the dirfd. | |
1007 | */ | |
1008 | if (WARN_ON(nd->flags & LOOKUP_IS_SCOPED)) | |
1009 | return -ENOTRECOVERABLE; | |
1010 | ||
9e6697e2 AV |
1011 | if (nd->flags & LOOKUP_RCU) { |
1012 | unsigned seq; | |
1013 | ||
1014 | do { | |
1015 | seq = read_seqcount_begin(&fs->seq); | |
1016 | nd->root = fs->root; | |
1017 | nd->root_seq = __read_seqcount_begin(&nd->root.dentry->d_seq); | |
1018 | } while (read_seqcount_retry(&fs->seq, seq)); | |
1019 | } else { | |
1020 | get_fs_root(fs, &nd->root); | |
bcba1e7d | 1021 | nd->state |= ND_ROOT_GRABBED; |
9e6697e2 | 1022 | } |
740a1678 | 1023 | return 0; |
31e6b01f NP |
1024 | } |
1025 | ||
248fb5b9 AV |
1026 | static int nd_jump_root(struct nameidata *nd) |
1027 | { | |
adb21d2b AS |
1028 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
1029 | return -EXDEV; | |
72ba2929 AS |
1030 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { |
1031 | /* Absolute path arguments to path_init() are allowed. */ | |
1032 | if (nd->path.mnt != NULL && nd->path.mnt != nd->root.mnt) | |
1033 | return -EXDEV; | |
1034 | } | |
740a1678 AS |
1035 | if (!nd->root.mnt) { |
1036 | int error = set_root(nd); | |
1037 | if (error) | |
1038 | return error; | |
1039 | } | |
248fb5b9 AV |
1040 | if (nd->flags & LOOKUP_RCU) { |
1041 | struct dentry *d; | |
1042 | nd->path = nd->root; | |
1043 | d = nd->path.dentry; | |
1044 | nd->inode = d->d_inode; | |
1045 | nd->seq = nd->root_seq; | |
82ef0698 | 1046 | if (read_seqcount_retry(&d->d_seq, nd->seq)) |
248fb5b9 AV |
1047 | return -ECHILD; |
1048 | } else { | |
1049 | path_put(&nd->path); | |
1050 | nd->path = nd->root; | |
1051 | path_get(&nd->path); | |
1052 | nd->inode = nd->path.dentry->d_inode; | |
1053 | } | |
bcba1e7d | 1054 | nd->state |= ND_JUMPED; |
248fb5b9 AV |
1055 | return 0; |
1056 | } | |
1057 | ||
b5fb63c1 | 1058 | /* |
6b255391 | 1059 | * Helper to directly jump to a known parsed path from ->get_link, |
b5fb63c1 CH |
1060 | * caller must have taken a reference to path beforehand. |
1061 | */ | |
ea4af4aa | 1062 | int nd_jump_link(const struct path *path) |
b5fb63c1 | 1063 | { |
4b99d499 | 1064 | int error = -ELOOP; |
6e77137b | 1065 | struct nameidata *nd = current->nameidata; |
b5fb63c1 | 1066 | |
4b99d499 AS |
1067 | if (unlikely(nd->flags & LOOKUP_NO_MAGICLINKS)) |
1068 | goto err; | |
1069 | ||
72ba2929 AS |
1070 | error = -EXDEV; |
1071 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) { | |
1072 | if (nd->path.mnt != path->mnt) | |
1073 | goto err; | |
1074 | } | |
adb21d2b AS |
1075 | /* Not currently safe for scoped-lookups. */ |
1076 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) | |
1077 | goto err; | |
72ba2929 | 1078 | |
4b99d499 | 1079 | path_put(&nd->path); |
b5fb63c1 CH |
1080 | nd->path = *path; |
1081 | nd->inode = nd->path.dentry->d_inode; | |
bcba1e7d | 1082 | nd->state |= ND_JUMPED; |
1bc82070 | 1083 | return 0; |
4b99d499 AS |
1084 | |
1085 | err: | |
1086 | path_put(path); | |
1087 | return error; | |
b5fb63c1 CH |
1088 | } |
1089 | ||
b9ff4429 | 1090 | static inline void put_link(struct nameidata *nd) |
574197e0 | 1091 | { |
21c3003d | 1092 | struct saved *last = nd->stack + --nd->depth; |
fceef393 | 1093 | do_delayed_call(&last->done); |
6548fae2 AV |
1094 | if (!(nd->flags & LOOKUP_RCU)) |
1095 | path_put(&last->link); | |
574197e0 AV |
1096 | } |
1097 | ||
9c011be1 LC |
1098 | static int sysctl_protected_symlinks __read_mostly; |
1099 | static int sysctl_protected_hardlinks __read_mostly; | |
1100 | static int sysctl_protected_fifos __read_mostly; | |
1101 | static int sysctl_protected_regular __read_mostly; | |
1102 | ||
1103 | #ifdef CONFIG_SYSCTL | |
1751f872 | 1104 | static const struct ctl_table namei_sysctls[] = { |
9c011be1 LC |
1105 | { |
1106 | .procname = "protected_symlinks", | |
1107 | .data = &sysctl_protected_symlinks, | |
1108 | .maxlen = sizeof(int), | |
c7031c14 | 1109 | .mode = 0644, |
9c011be1 LC |
1110 | .proc_handler = proc_dointvec_minmax, |
1111 | .extra1 = SYSCTL_ZERO, | |
1112 | .extra2 = SYSCTL_ONE, | |
1113 | }, | |
1114 | { | |
1115 | .procname = "protected_hardlinks", | |
1116 | .data = &sysctl_protected_hardlinks, | |
1117 | .maxlen = sizeof(int), | |
c7031c14 | 1118 | .mode = 0644, |
9c011be1 LC |
1119 | .proc_handler = proc_dointvec_minmax, |
1120 | .extra1 = SYSCTL_ZERO, | |
1121 | .extra2 = SYSCTL_ONE, | |
1122 | }, | |
1123 | { | |
1124 | .procname = "protected_fifos", | |
1125 | .data = &sysctl_protected_fifos, | |
1126 | .maxlen = sizeof(int), | |
c7031c14 | 1127 | .mode = 0644, |
9c011be1 LC |
1128 | .proc_handler = proc_dointvec_minmax, |
1129 | .extra1 = SYSCTL_ZERO, | |
1130 | .extra2 = SYSCTL_TWO, | |
1131 | }, | |
1132 | { | |
1133 | .procname = "protected_regular", | |
1134 | .data = &sysctl_protected_regular, | |
1135 | .maxlen = sizeof(int), | |
c7031c14 | 1136 | .mode = 0644, |
9c011be1 LC |
1137 | .proc_handler = proc_dointvec_minmax, |
1138 | .extra1 = SYSCTL_ZERO, | |
1139 | .extra2 = SYSCTL_TWO, | |
1140 | }, | |
9c011be1 LC |
1141 | }; |
1142 | ||
1143 | static int __init init_fs_namei_sysctls(void) | |
1144 | { | |
1145 | register_sysctl_init("fs", namei_sysctls); | |
1146 | return 0; | |
1147 | } | |
1148 | fs_initcall(init_fs_namei_sysctls); | |
1149 | ||
1150 | #endif /* CONFIG_SYSCTL */ | |
800179c9 KC |
1151 | |
1152 | /** | |
1153 | * may_follow_link - Check symlink following for unsafe situations | |
55852635 | 1154 | * @nd: nameidata pathwalk data |
35931eb3 | 1155 | * @inode: Used for idmapping. |
800179c9 KC |
1156 | * |
1157 | * In the case of the sysctl_protected_symlinks sysctl being enabled, | |
1158 | * CAP_DAC_OVERRIDE needs to be specifically ignored if the symlink is | |
1159 | * in a sticky world-writable directory. This is to protect privileged | |
1160 | * processes from failing races against path names that may change out | |
1161 | * from under them by way of other users creating malicious symlinks. | |
1162 | * It will permit symlinks to be followed only when outside a sticky | |
1163 | * world-writable directory, or when the uid of the symlink and follower | |
1164 | * match, or when the directory owner matches the symlink's owner. | |
1165 | * | |
1166 | * Returns 0 if following the symlink is allowed, -ve on error. | |
1167 | */ | |
ad6cc4c3 | 1168 | static inline int may_follow_link(struct nameidata *nd, const struct inode *inode) |
800179c9 | 1169 | { |
e67fe633 | 1170 | struct mnt_idmap *idmap; |
a2bd096f | 1171 | vfsuid_t vfsuid; |
ba73d987 | 1172 | |
800179c9 KC |
1173 | if (!sysctl_protected_symlinks) |
1174 | return 0; | |
1175 | ||
e67fe633 CB |
1176 | idmap = mnt_idmap(nd->path.mnt); |
1177 | vfsuid = i_uid_into_vfsuid(idmap, inode); | |
800179c9 | 1178 | /* Allowed if owner and follower match. */ |
a2bd096f | 1179 | if (vfsuid_eq_kuid(vfsuid, current_fsuid())) |
800179c9 KC |
1180 | return 0; |
1181 | ||
1182 | /* Allowed if parent directory not sticky and world-writable. */ | |
0f705953 | 1183 | if ((nd->dir_mode & (S_ISVTX|S_IWOTH)) != (S_ISVTX|S_IWOTH)) |
800179c9 KC |
1184 | return 0; |
1185 | ||
1186 | /* Allowed if parent directory and link owner match. */ | |
a2bd096f | 1187 | if (vfsuid_valid(nd->dir_vfsuid) && vfsuid_eq(nd->dir_vfsuid, vfsuid)) |
800179c9 KC |
1188 | return 0; |
1189 | ||
31956502 AV |
1190 | if (nd->flags & LOOKUP_RCU) |
1191 | return -ECHILD; | |
1192 | ||
ea841baf | 1193 | audit_inode(nd->name, nd->stack[0].link.dentry, 0); |
245d7369 | 1194 | audit_log_path_denied(AUDIT_ANOM_LINK, "follow_link"); |
800179c9 KC |
1195 | return -EACCES; |
1196 | } | |
1197 | ||
1198 | /** | |
1199 | * safe_hardlink_source - Check for safe hardlink conditions | |
4609e1f1 | 1200 | * @idmap: idmap of the mount the inode was found from |
800179c9 KC |
1201 | * @inode: the source inode to hardlink from |
1202 | * | |
1203 | * Return false if at least one of the following conditions: | |
1204 | * - inode is not a regular file | |
1205 | * - inode is setuid | |
1206 | * - inode is setgid and group-exec | |
1207 | * - access failure for read and write | |
1208 | * | |
1209 | * Otherwise returns true. | |
1210 | */ | |
4609e1f1 | 1211 | static bool safe_hardlink_source(struct mnt_idmap *idmap, |
ba73d987 | 1212 | struct inode *inode) |
800179c9 KC |
1213 | { |
1214 | umode_t mode = inode->i_mode; | |
1215 | ||
1216 | /* Special files should not get pinned to the filesystem. */ | |
1217 | if (!S_ISREG(mode)) | |
1218 | return false; | |
1219 | ||
1220 | /* Setuid files should not get pinned to the filesystem. */ | |
1221 | if (mode & S_ISUID) | |
1222 | return false; | |
1223 | ||
1224 | /* Executable setgid files should not get pinned to the filesystem. */ | |
1225 | if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) | |
1226 | return false; | |
1227 | ||
1228 | /* Hardlinking to unreadable or unwritable sources is dangerous. */ | |
4609e1f1 | 1229 | if (inode_permission(idmap, inode, MAY_READ | MAY_WRITE)) |
800179c9 KC |
1230 | return false; |
1231 | ||
1232 | return true; | |
1233 | } | |
1234 | ||
1235 | /** | |
1236 | * may_linkat - Check permissions for creating a hardlink | |
4609e1f1 CB |
1237 | * @idmap: idmap of the mount the inode was found from |
1238 | * @link: the source to hardlink from | |
800179c9 KC |
1239 | * |
1240 | * Block hardlink when all of: | |
1241 | * - sysctl_protected_hardlinks enabled | |
1242 | * - fsuid does not match inode | |
1243 | * - hardlink source is unsafe (see safe_hardlink_source() above) | |
f2ca3796 | 1244 | * - not CAP_FOWNER in a namespace with the inode owner uid mapped |
800179c9 | 1245 | * |
4609e1f1 CB |
1246 | * If the inode has been found through an idmapped mount the idmap of |
1247 | * the vfsmount must be passed through @idmap. This function will then take | |
1248 | * care to map the inode according to @idmap before checking permissions. | |
ba73d987 | 1249 | * On non-idmapped mounts or if permission checking is to be performed on the |
4609e1f1 | 1250 | * raw inode simply pass @nop_mnt_idmap. |
ba73d987 | 1251 | * |
800179c9 KC |
1252 | * Returns 0 if successful, -ve on error. |
1253 | */ | |
4609e1f1 | 1254 | int may_linkat(struct mnt_idmap *idmap, const struct path *link) |
800179c9 | 1255 | { |
593d1ce8 EB |
1256 | struct inode *inode = link->dentry->d_inode; |
1257 | ||
1258 | /* Inode writeback is not safe when the uid or gid are invalid. */ | |
e67fe633 CB |
1259 | if (!vfsuid_valid(i_uid_into_vfsuid(idmap, inode)) || |
1260 | !vfsgid_valid(i_gid_into_vfsgid(idmap, inode))) | |
593d1ce8 | 1261 | return -EOVERFLOW; |
800179c9 KC |
1262 | |
1263 | if (!sysctl_protected_hardlinks) | |
1264 | return 0; | |
1265 | ||
800179c9 KC |
1266 | /* Source inode owner (or CAP_FOWNER) can hardlink all they like, |
1267 | * otherwise, it must be a safe source. | |
1268 | */ | |
4609e1f1 | 1269 | if (safe_hardlink_source(idmap, inode) || |
01beba79 | 1270 | inode_owner_or_capable(idmap, inode)) |
800179c9 KC |
1271 | return 0; |
1272 | ||
245d7369 | 1273 | audit_log_path_denied(AUDIT_ANOM_LINK, "linkat"); |
800179c9 KC |
1274 | return -EPERM; |
1275 | } | |
1276 | ||
30aba665 SM |
1277 | /** |
1278 | * may_create_in_sticky - Check whether an O_CREAT open in a sticky directory | |
1279 | * should be allowed, or not, on files that already | |
1280 | * exist. | |
e67fe633 | 1281 | * @idmap: idmap of the mount the inode was found from |
2111c3c0 | 1282 | * @nd: nameidata pathwalk data |
30aba665 SM |
1283 | * @inode: the inode of the file to open |
1284 | * | |
1285 | * Block an O_CREAT open of a FIFO (or a regular file) when: | |
1286 | * - sysctl_protected_fifos (or sysctl_protected_regular) is enabled | |
1287 | * - the file already exists | |
1288 | * - we are in a sticky directory | |
1289 | * - we don't own the file | |
1290 | * - the owner of the directory doesn't own the file | |
1291 | * - the directory is world writable | |
1292 | * If the sysctl_protected_fifos (or sysctl_protected_regular) is set to 2 | |
1293 | * the directory doesn't have to be world writable: being group writable will | |
1294 | * be enough. | |
1295 | * | |
e67fe633 CB |
1296 | * If the inode has been found through an idmapped mount the idmap of |
1297 | * the vfsmount must be passed through @idmap. This function will then take | |
1298 | * care to map the inode according to @idmap before checking permissions. | |
ba73d987 | 1299 | * On non-idmapped mounts or if permission checking is to be performed on the |
e67fe633 | 1300 | * raw inode simply pass @nop_mnt_idmap. |
ba73d987 | 1301 | * |
30aba665 SM |
1302 | * Returns 0 if the open is allowed, -ve on error. |
1303 | */ | |
9fb9ff7e CB |
1304 | static int may_create_in_sticky(struct mnt_idmap *idmap, struct nameidata *nd, |
1305 | struct inode *const inode) | |
30aba665 | 1306 | { |
ba73d987 | 1307 | umode_t dir_mode = nd->dir_mode; |
9fb9ff7e | 1308 | vfsuid_t dir_vfsuid = nd->dir_vfsuid, i_vfsuid; |
ba73d987 | 1309 | |
9fb9ff7e | 1310 | if (likely(!(dir_mode & S_ISVTX))) |
30aba665 SM |
1311 | return 0; |
1312 | ||
9fb9ff7e CB |
1313 | if (S_ISREG(inode->i_mode) && !sysctl_protected_regular) |
1314 | return 0; | |
ba73d987 | 1315 | |
9fb9ff7e | 1316 | if (S_ISFIFO(inode->i_mode) && !sysctl_protected_fifos) |
30aba665 SM |
1317 | return 0; |
1318 | ||
9fb9ff7e CB |
1319 | i_vfsuid = i_uid_into_vfsuid(idmap, inode); |
1320 | ||
1321 | if (vfsuid_eq(i_vfsuid, dir_vfsuid)) | |
1322 | return 0; | |
1323 | ||
1324 | if (vfsuid_eq_kuid(i_vfsuid, current_fsuid())) | |
1325 | return 0; | |
1326 | ||
1327 | if (likely(dir_mode & 0002)) { | |
1328 | audit_log_path_denied(AUDIT_ANOM_CREAT, "sticky_create"); | |
30aba665 SM |
1329 | return -EACCES; |
1330 | } | |
9fb9ff7e CB |
1331 | |
1332 | if (dir_mode & 0020) { | |
1333 | if (sysctl_protected_fifos >= 2 && S_ISFIFO(inode->i_mode)) { | |
1334 | audit_log_path_denied(AUDIT_ANOM_CREAT, | |
1335 | "sticky_create_fifo"); | |
1336 | return -EACCES; | |
1337 | } | |
1338 | ||
1339 | if (sysctl_protected_regular >= 2 && S_ISREG(inode->i_mode)) { | |
1340 | audit_log_path_denied(AUDIT_ANOM_CREAT, | |
1341 | "sticky_create_regular"); | |
1342 | return -EACCES; | |
1343 | } | |
1344 | } | |
1345 | ||
30aba665 SM |
1346 | return 0; |
1347 | } | |
1348 | ||
f015f126 DH |
1349 | /* |
1350 | * follow_up - Find the mountpoint of path's vfsmount | |
1351 | * | |
1352 | * Given a path, find the mountpoint of its source file system. | |
1353 | * Replace @path with the path of the mountpoint in the parent mount. | |
1354 | * Up is towards /. | |
1355 | * | |
1356 | * Return 1 if we went up a level and 0 if we were already at the | |
1357 | * root. | |
1358 | */ | |
bab77ebf | 1359 | int follow_up(struct path *path) |
1da177e4 | 1360 | { |
0714a533 AV |
1361 | struct mount *mnt = real_mount(path->mnt); |
1362 | struct mount *parent; | |
1da177e4 | 1363 | struct dentry *mountpoint; |
99b7db7b | 1364 | |
48a066e7 | 1365 | read_seqlock_excl(&mount_lock); |
0714a533 | 1366 | parent = mnt->mnt_parent; |
3c0a6163 | 1367 | if (parent == mnt) { |
48a066e7 | 1368 | read_sequnlock_excl(&mount_lock); |
1da177e4 LT |
1369 | return 0; |
1370 | } | |
0714a533 | 1371 | mntget(&parent->mnt); |
a73324da | 1372 | mountpoint = dget(mnt->mnt_mountpoint); |
48a066e7 | 1373 | read_sequnlock_excl(&mount_lock); |
bab77ebf AV |
1374 | dput(path->dentry); |
1375 | path->dentry = mountpoint; | |
1376 | mntput(path->mnt); | |
0714a533 | 1377 | path->mnt = &parent->mnt; |
1da177e4 LT |
1378 | return 1; |
1379 | } | |
4d359507 | 1380 | EXPORT_SYMBOL(follow_up); |
1da177e4 | 1381 | |
7ef482fa AV |
1382 | static bool choose_mountpoint_rcu(struct mount *m, const struct path *root, |
1383 | struct path *path, unsigned *seqp) | |
1384 | { | |
1385 | while (mnt_has_parent(m)) { | |
1386 | struct dentry *mountpoint = m->mnt_mountpoint; | |
1387 | ||
1388 | m = m->mnt_parent; | |
1389 | if (unlikely(root->dentry == mountpoint && | |
1390 | root->mnt == &m->mnt)) | |
1391 | break; | |
1392 | if (mountpoint != m->mnt.mnt_root) { | |
1393 | path->mnt = &m->mnt; | |
1394 | path->dentry = mountpoint; | |
1395 | *seqp = read_seqcount_begin(&mountpoint->d_seq); | |
1396 | return true; | |
1397 | } | |
1398 | } | |
1399 | return false; | |
1400 | } | |
1401 | ||
2aa38470 AV |
1402 | static bool choose_mountpoint(struct mount *m, const struct path *root, |
1403 | struct path *path) | |
1404 | { | |
1405 | bool found; | |
1406 | ||
1407 | rcu_read_lock(); | |
1408 | while (1) { | |
1409 | unsigned seq, mseq = read_seqbegin(&mount_lock); | |
1410 | ||
1411 | found = choose_mountpoint_rcu(m, root, path, &seq); | |
1412 | if (unlikely(!found)) { | |
1413 | if (!read_seqretry(&mount_lock, mseq)) | |
1414 | break; | |
1415 | } else { | |
1416 | if (likely(__legitimize_path(path, seq, mseq))) | |
1417 | break; | |
1418 | rcu_read_unlock(); | |
1419 | path_put(path); | |
1420 | rcu_read_lock(); | |
1421 | } | |
1422 | } | |
1423 | rcu_read_unlock(); | |
1424 | return found; | |
1425 | } | |
1426 | ||
b5c84bf6 | 1427 | /* |
9875cf80 DH |
1428 | * Perform an automount |
1429 | * - return -EISDIR to tell follow_managed() to stop and return the path we | |
1430 | * were called with. | |
1da177e4 | 1431 | */ |
1c9f5e06 | 1432 | static int follow_automount(struct path *path, int *count, unsigned lookup_flags) |
31e6b01f | 1433 | { |
25e195aa | 1434 | struct dentry *dentry = path->dentry; |
9875cf80 | 1435 | |
0ec26fd0 MS |
1436 | /* We don't want to mount if someone's just doing a stat - |
1437 | * unless they're stat'ing a directory and appended a '/' to | |
1438 | * the name. | |
1439 | * | |
1440 | * We do, however, want to mount if someone wants to open or | |
1441 | * create a file of any type under the mountpoint, wants to | |
1442 | * traverse through the mountpoint or wants to open the | |
1443 | * mounted directory. Also, autofs may mark negative dentries | |
1444 | * as being automount points. These will need the attentions | |
1445 | * of the daemon to instantiate them before they can be used. | |
9875cf80 | 1446 | */ |
1c9f5e06 | 1447 | if (!(lookup_flags & (LOOKUP_PARENT | LOOKUP_DIRECTORY | |
5d38f049 | 1448 | LOOKUP_OPEN | LOOKUP_CREATE | LOOKUP_AUTOMOUNT)) && |
25e195aa | 1449 | dentry->d_inode) |
5d38f049 | 1450 | return -EISDIR; |
0ec26fd0 | 1451 | |
1c9f5e06 | 1452 | if (count && (*count)++ >= MAXSYMLINKS) |
9875cf80 DH |
1453 | return -ELOOP; |
1454 | ||
25e195aa | 1455 | return finish_automount(dentry->d_op->d_automount(path), path); |
463ffb2e AV |
1456 | } |
1457 | ||
9875cf80 | 1458 | /* |
9deed3eb AV |
1459 | * mount traversal - out-of-line part. One note on ->d_flags accesses - |
1460 | * dentries are pinned but not locked here, so negative dentry can go | |
1461 | * positive right under us. Use of smp_load_acquire() provides a barrier | |
1462 | * sufficient for ->d_inode and ->d_flags consistency. | |
9875cf80 | 1463 | */ |
9deed3eb AV |
1464 | static int __traverse_mounts(struct path *path, unsigned flags, bool *jumped, |
1465 | int *count, unsigned lookup_flags) | |
1da177e4 | 1466 | { |
9deed3eb | 1467 | struct vfsmount *mnt = path->mnt; |
9875cf80 | 1468 | bool need_mntput = false; |
8aef1884 | 1469 | int ret = 0; |
9875cf80 | 1470 | |
9deed3eb | 1471 | while (flags & DCACHE_MANAGED_DENTRY) { |
cc53ce53 DH |
1472 | /* Allow the filesystem to manage the transit without i_mutex |
1473 | * being held. */ | |
d41efb52 | 1474 | if (flags & DCACHE_MANAGE_TRANSIT) { |
fb5f51c7 | 1475 | ret = path->dentry->d_op->d_manage(path, false); |
508c8772 | 1476 | flags = smp_load_acquire(&path->dentry->d_flags); |
cc53ce53 | 1477 | if (ret < 0) |
8aef1884 | 1478 | break; |
cc53ce53 DH |
1479 | } |
1480 | ||
9deed3eb | 1481 | if (flags & DCACHE_MOUNTED) { // something's mounted on it.. |
9875cf80 | 1482 | struct vfsmount *mounted = lookup_mnt(path); |
9deed3eb | 1483 | if (mounted) { // ... in our namespace |
9875cf80 DH |
1484 | dput(path->dentry); |
1485 | if (need_mntput) | |
1486 | mntput(path->mnt); | |
1487 | path->mnt = mounted; | |
1488 | path->dentry = dget(mounted->mnt_root); | |
9deed3eb AV |
1489 | // here we know it's positive |
1490 | flags = path->dentry->d_flags; | |
9875cf80 DH |
1491 | need_mntput = true; |
1492 | continue; | |
1493 | } | |
9875cf80 DH |
1494 | } |
1495 | ||
9deed3eb AV |
1496 | if (!(flags & DCACHE_NEED_AUTOMOUNT)) |
1497 | break; | |
9875cf80 | 1498 | |
9deed3eb AV |
1499 | // uncovered automount point |
1500 | ret = follow_automount(path, count, lookup_flags); | |
1501 | flags = smp_load_acquire(&path->dentry->d_flags); | |
1502 | if (ret < 0) | |
1503 | break; | |
1da177e4 | 1504 | } |
8aef1884 | 1505 | |
9deed3eb AV |
1506 | if (ret == -EISDIR) |
1507 | ret = 0; | |
1508 | // possible if you race with several mount --move | |
1509 | if (need_mntput && path->mnt == mnt) | |
1510 | mntput(path->mnt); | |
1511 | if (!ret && unlikely(d_flags_negative(flags))) | |
d41efb52 | 1512 | ret = -ENOENT; |
9deed3eb | 1513 | *jumped = need_mntput; |
8402752e | 1514 | return ret; |
1da177e4 LT |
1515 | } |
1516 | ||
9deed3eb AV |
1517 | static inline int traverse_mounts(struct path *path, bool *jumped, |
1518 | int *count, unsigned lookup_flags) | |
1519 | { | |
1520 | unsigned flags = smp_load_acquire(&path->dentry->d_flags); | |
1521 | ||
1522 | /* fastpath */ | |
1523 | if (likely(!(flags & DCACHE_MANAGED_DENTRY))) { | |
1524 | *jumped = false; | |
1525 | if (unlikely(d_flags_negative(flags))) | |
1526 | return -ENOENT; | |
1527 | return 0; | |
1528 | } | |
1529 | return __traverse_mounts(path, flags, jumped, count, lookup_flags); | |
1530 | } | |
1531 | ||
cc53ce53 | 1532 | int follow_down_one(struct path *path) |
1da177e4 LT |
1533 | { |
1534 | struct vfsmount *mounted; | |
1535 | ||
1c755af4 | 1536 | mounted = lookup_mnt(path); |
1da177e4 | 1537 | if (mounted) { |
9393bd07 AV |
1538 | dput(path->dentry); |
1539 | mntput(path->mnt); | |
1540 | path->mnt = mounted; | |
1541 | path->dentry = dget(mounted->mnt_root); | |
1da177e4 LT |
1542 | return 1; |
1543 | } | |
1544 | return 0; | |
1545 | } | |
4d359507 | 1546 | EXPORT_SYMBOL(follow_down_one); |
1da177e4 | 1547 | |
9deed3eb AV |
1548 | /* |
1549 | * Follow down to the covering mount currently visible to userspace. At each | |
1550 | * point, the filesystem owning that dentry may be queried as to whether the | |
1551 | * caller is permitted to proceed or not. | |
1552 | */ | |
e1f19857 | 1553 | int follow_down(struct path *path, unsigned int flags) |
9deed3eb AV |
1554 | { |
1555 | struct vfsmount *mnt = path->mnt; | |
1556 | bool jumped; | |
e1f19857 | 1557 | int ret = traverse_mounts(path, &jumped, NULL, flags); |
9deed3eb AV |
1558 | |
1559 | if (path->mnt != mnt) | |
1560 | mntput(mnt); | |
1561 | return ret; | |
1562 | } | |
1563 | EXPORT_SYMBOL(follow_down); | |
1564 | ||
9875cf80 | 1565 | /* |
287548e4 AV |
1566 | * Try to skip to top of mountpoint pile in rcuwalk mode. Fail if |
1567 | * we meet a managed dentry that would need blocking. | |
9875cf80 | 1568 | */ |
3bd8bc89 | 1569 | static bool __follow_mount_rcu(struct nameidata *nd, struct path *path) |
9875cf80 | 1570 | { |
ea936aeb AV |
1571 | struct dentry *dentry = path->dentry; |
1572 | unsigned int flags = dentry->d_flags; | |
1573 | ||
1574 | if (likely(!(flags & DCACHE_MANAGED_DENTRY))) | |
1575 | return true; | |
1576 | ||
1577 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) | |
1578 | return false; | |
1579 | ||
62a7375e | 1580 | for (;;) { |
62a7375e IK |
1581 | /* |
1582 | * Don't forget we might have a non-mountpoint managed dentry | |
1583 | * that wants to block transit. | |
1584 | */ | |
ea936aeb AV |
1585 | if (unlikely(flags & DCACHE_MANAGE_TRANSIT)) { |
1586 | int res = dentry->d_op->d_manage(path, true); | |
1587 | if (res) | |
1588 | return res == -EISDIR; | |
1589 | flags = dentry->d_flags; | |
b8faf035 | 1590 | } |
62a7375e | 1591 | |
ea936aeb AV |
1592 | if (flags & DCACHE_MOUNTED) { |
1593 | struct mount *mounted = __lookup_mnt(path->mnt, dentry); | |
1594 | if (mounted) { | |
1595 | path->mnt = &mounted->mnt; | |
1596 | dentry = path->dentry = mounted->mnt.mnt_root; | |
bcba1e7d | 1597 | nd->state |= ND_JUMPED; |
03fa86e9 | 1598 | nd->next_seq = read_seqcount_begin(&dentry->d_seq); |
ea936aeb | 1599 | flags = dentry->d_flags; |
03fa86e9 AV |
1600 | // makes sure that non-RCU pathwalk could reach |
1601 | // this state. | |
20aac6c6 AV |
1602 | if (read_seqretry(&mount_lock, nd->m_seq)) |
1603 | return false; | |
ea936aeb AV |
1604 | continue; |
1605 | } | |
1606 | if (read_seqretry(&mount_lock, nd->m_seq)) | |
1607 | return false; | |
1608 | } | |
1609 | return !(flags & DCACHE_NEED_AUTOMOUNT); | |
9875cf80 | 1610 | } |
287548e4 AV |
1611 | } |
1612 | ||
db3c9ade | 1613 | static inline int handle_mounts(struct nameidata *nd, struct dentry *dentry, |
3bd8bc89 | 1614 | struct path *path) |
bd7c4b50 | 1615 | { |
9deed3eb | 1616 | bool jumped; |
db3c9ade | 1617 | int ret; |
bd7c4b50 | 1618 | |
db3c9ade AV |
1619 | path->mnt = nd->path.mnt; |
1620 | path->dentry = dentry; | |
c153007b | 1621 | if (nd->flags & LOOKUP_RCU) { |
03fa86e9 | 1622 | unsigned int seq = nd->next_seq; |
3bd8bc89 | 1623 | if (likely(__follow_mount_rcu(nd, path))) |
9deed3eb | 1624 | return 0; |
03fa86e9 | 1625 | // *path and nd->next_seq might've been clobbered |
c153007b AV |
1626 | path->mnt = nd->path.mnt; |
1627 | path->dentry = dentry; | |
03fa86e9 AV |
1628 | nd->next_seq = seq; |
1629 | if (!try_to_unlazy_next(nd, dentry)) | |
1630 | return -ECHILD; | |
c153007b | 1631 | } |
9deed3eb AV |
1632 | ret = traverse_mounts(path, &jumped, &nd->total_link_count, nd->flags); |
1633 | if (jumped) { | |
1634 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) | |
1635 | ret = -EXDEV; | |
1636 | else | |
bcba1e7d | 1637 | nd->state |= ND_JUMPED; |
9deed3eb AV |
1638 | } |
1639 | if (unlikely(ret)) { | |
1640 | dput(path->dentry); | |
1641 | if (path->mnt != nd->path.mnt) | |
1642 | mntput(path->mnt); | |
bd7c4b50 AV |
1643 | } |
1644 | return ret; | |
1645 | } | |
1646 | ||
baa03890 | 1647 | /* |
f4fdace9 OD |
1648 | * This looks up the name in dcache and possibly revalidates the found dentry. |
1649 | * NULL is returned if the dentry does not exist in the cache. | |
baa03890 | 1650 | */ |
e3c13928 AV |
1651 | static struct dentry *lookup_dcache(const struct qstr *name, |
1652 | struct dentry *dir, | |
6c51e513 | 1653 | unsigned int flags) |
baa03890 | 1654 | { |
a89f8337 | 1655 | struct dentry *dentry = d_lookup(dir, name); |
bad61189 | 1656 | if (dentry) { |
5be1fa8a | 1657 | int error = d_revalidate(dir->d_inode, name, dentry, flags); |
a89f8337 AV |
1658 | if (unlikely(error <= 0)) { |
1659 | if (!error) | |
1660 | d_invalidate(dentry); | |
1661 | dput(dentry); | |
1662 | return ERR_PTR(error); | |
bad61189 MS |
1663 | } |
1664 | } | |
baa03890 NP |
1665 | return dentry; |
1666 | } | |
1667 | ||
c86b300b CB |
1668 | static struct dentry *lookup_one_qstr_excl_raw(const struct qstr *name, |
1669 | struct dentry *base, | |
1670 | unsigned int flags) | |
a3255546 | 1671 | { |
c86b300b | 1672 | struct dentry *dentry; |
a03ece5f | 1673 | struct dentry *old; |
c86b300b | 1674 | struct inode *dir; |
a3255546 | 1675 | |
c86b300b | 1676 | dentry = lookup_dcache(name, base, flags); |
6c51e513 | 1677 | if (dentry) |
c86b300b | 1678 | return dentry; |
a3255546 | 1679 | |
a03ece5f | 1680 | /* Don't create child dentry for a dead directory. */ |
c86b300b | 1681 | dir = base->d_inode; |
a03ece5f AV |
1682 | if (unlikely(IS_DEADDIR(dir))) |
1683 | return ERR_PTR(-ENOENT); | |
1684 | ||
6c51e513 AV |
1685 | dentry = d_alloc(base, name); |
1686 | if (unlikely(!dentry)) | |
1687 | return ERR_PTR(-ENOMEM); | |
1688 | ||
a03ece5f AV |
1689 | old = dir->i_op->lookup(dir, dentry, flags); |
1690 | if (unlikely(old)) { | |
1691 | dput(dentry); | |
1692 | dentry = old; | |
1693 | } | |
c86b300b CB |
1694 | return dentry; |
1695 | } | |
1696 | ||
1697 | /* | |
1698 | * Parent directory has inode locked exclusive. This is one | |
1699 | * and only case when ->lookup() gets called on non in-lookup | |
1700 | * dentries - as the matter of fact, this only gets called | |
1701 | * when directory is guaranteed to have no in-lookup children | |
1702 | * at all. | |
1703 | * Will return -ENOENT if name isn't found and LOOKUP_CREATE wasn't passed. | |
1704 | * Will return -EEXIST if name is found and LOOKUP_EXCL was passed. | |
1705 | */ | |
1706 | struct dentry *lookup_one_qstr_excl(const struct qstr *name, | |
1707 | struct dentry *base, unsigned int flags) | |
1708 | { | |
1709 | struct dentry *dentry; | |
1710 | ||
1711 | dentry = lookup_one_qstr_excl_raw(name, base, flags); | |
204a575e N |
1712 | if (IS_ERR(dentry)) |
1713 | return dentry; | |
1714 | if (d_is_negative(dentry) && !(flags & LOOKUP_CREATE)) { | |
1715 | dput(dentry); | |
1716 | return ERR_PTR(-ENOENT); | |
1717 | } | |
1718 | if (d_is_positive(dentry) && (flags & LOOKUP_EXCL)) { | |
1719 | dput(dentry); | |
1720 | return ERR_PTR(-EEXIST); | |
1721 | } | |
a03ece5f | 1722 | return dentry; |
a3255546 | 1723 | } |
74d7970f | 1724 | EXPORT_SYMBOL(lookup_one_qstr_excl); |
a3255546 | 1725 | |
46460c1d JL |
1726 | /** |
1727 | * lookup_fast - do fast lockless (but racy) lookup of a dentry | |
1728 | * @nd: current nameidata | |
1729 | * | |
1730 | * Do a fast, but racy lookup in the dcache for the given dentry, and | |
1731 | * revalidate it. Returns a valid dentry pointer or NULL if one wasn't | |
1732 | * found. On error, an ERR_PTR will be returned. | |
1733 | * | |
1734 | * If this function returns a valid dentry and the walk is no longer | |
1735 | * lazy, the dentry will carry a reference that must later be put. If | |
1736 | * RCU mode is still in force, then this is not the case and the dentry | |
1737 | * must be legitimized before use. If this returns NULL, then the walk | |
1738 | * will no longer be in RCU mode. | |
1739 | */ | |
4cb64024 | 1740 | static struct dentry *lookup_fast(struct nameidata *nd) |
1da177e4 | 1741 | { |
31e6b01f | 1742 | struct dentry *dentry, *parent = nd->path.dentry; |
5a18fff2 | 1743 | int status = 1; |
9875cf80 | 1744 | |
b04f784e NP |
1745 | /* |
1746 | * Rename seqlock is not required here because in the off chance | |
5d0f49c1 AV |
1747 | * of a false negative due to a concurrent rename, the caller is |
1748 | * going to fall back to non-racy lookup. | |
b04f784e | 1749 | */ |
31e6b01f | 1750 | if (nd->flags & LOOKUP_RCU) { |
03fa86e9 | 1751 | dentry = __d_lookup_rcu(parent, &nd->last, &nd->next_seq); |
5d0f49c1 | 1752 | if (unlikely(!dentry)) { |
e36cffed | 1753 | if (!try_to_unlazy(nd)) |
20e34357 AV |
1754 | return ERR_PTR(-ECHILD); |
1755 | return NULL; | |
5d0f49c1 | 1756 | } |
5a18fff2 | 1757 | |
12f8ad4b LT |
1758 | /* |
1759 | * This sequence count validates that the parent had no | |
1760 | * changes while we did the lookup of the dentry above. | |
12f8ad4b | 1761 | */ |
4cb64024 | 1762 | if (read_seqcount_retry(&parent->d_seq, nd->seq)) |
20e34357 | 1763 | return ERR_PTR(-ECHILD); |
5a18fff2 | 1764 | |
5be1fa8a | 1765 | status = d_revalidate(nd->inode, &nd->last, dentry, nd->flags); |
c153007b | 1766 | if (likely(status > 0)) |
20e34357 | 1767 | return dentry; |
03fa86e9 | 1768 | if (!try_to_unlazy_next(nd, dentry)) |
20e34357 | 1769 | return ERR_PTR(-ECHILD); |
26ddb45e | 1770 | if (status == -ECHILD) |
209a7fb2 | 1771 | /* we'd been told to redo it in non-rcu mode */ |
5be1fa8a AV |
1772 | status = d_revalidate(nd->inode, &nd->last, |
1773 | dentry, nd->flags); | |
5a18fff2 | 1774 | } else { |
e97cdc87 | 1775 | dentry = __d_lookup(parent, &nd->last); |
5d0f49c1 | 1776 | if (unlikely(!dentry)) |
20e34357 | 1777 | return NULL; |
5be1fa8a | 1778 | status = d_revalidate(nd->inode, &nd->last, dentry, nd->flags); |
9875cf80 | 1779 | } |
5a18fff2 | 1780 | if (unlikely(status <= 0)) { |
e9742b53 | 1781 | if (!status) |
5d0f49c1 | 1782 | d_invalidate(dentry); |
5542aa2f | 1783 | dput(dentry); |
20e34357 | 1784 | return ERR_PTR(status); |
24643087 | 1785 | } |
20e34357 | 1786 | return dentry; |
697f514d MS |
1787 | } |
1788 | ||
1789 | /* Fast lookup failed, do it the slow way */ | |
88d8331a AV |
1790 | static struct dentry *__lookup_slow(const struct qstr *name, |
1791 | struct dentry *dir, | |
1792 | unsigned int flags) | |
697f514d | 1793 | { |
88d8331a | 1794 | struct dentry *dentry, *old; |
1936386e | 1795 | struct inode *inode = dir->d_inode; |
d9171b93 | 1796 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
1936386e | 1797 | |
1936386e | 1798 | /* Don't go there if it's already dead */ |
94bdd655 | 1799 | if (unlikely(IS_DEADDIR(inode))) |
88d8331a | 1800 | return ERR_PTR(-ENOENT); |
94bdd655 | 1801 | again: |
d9171b93 | 1802 | dentry = d_alloc_parallel(dir, name, &wq); |
94bdd655 | 1803 | if (IS_ERR(dentry)) |
88d8331a | 1804 | return dentry; |
94bdd655 | 1805 | if (unlikely(!d_in_lookup(dentry))) { |
5be1fa8a | 1806 | int error = d_revalidate(inode, name, dentry, flags); |
c64cd6e3 AV |
1807 | if (unlikely(error <= 0)) { |
1808 | if (!error) { | |
1809 | d_invalidate(dentry); | |
949a852e | 1810 | dput(dentry); |
c64cd6e3 | 1811 | goto again; |
949a852e | 1812 | } |
c64cd6e3 AV |
1813 | dput(dentry); |
1814 | dentry = ERR_PTR(error); | |
949a852e | 1815 | } |
94bdd655 AV |
1816 | } else { |
1817 | old = inode->i_op->lookup(inode, dentry, flags); | |
1818 | d_lookup_done(dentry); | |
1819 | if (unlikely(old)) { | |
1820 | dput(dentry); | |
1821 | dentry = old; | |
949a852e AV |
1822 | } |
1823 | } | |
e3c13928 | 1824 | return dentry; |
1da177e4 LT |
1825 | } |
1826 | ||
88d8331a AV |
1827 | static struct dentry *lookup_slow(const struct qstr *name, |
1828 | struct dentry *dir, | |
1829 | unsigned int flags) | |
1830 | { | |
1831 | struct inode *inode = dir->d_inode; | |
1832 | struct dentry *res; | |
1833 | inode_lock_shared(inode); | |
1834 | res = __lookup_slow(name, dir, flags); | |
1835 | inode_unlock_shared(inode); | |
1836 | return res; | |
1837 | } | |
1838 | ||
4609e1f1 | 1839 | static inline int may_lookup(struct mnt_idmap *idmap, |
58b0afa0 | 1840 | struct nameidata *restrict nd) |
52094c8a | 1841 | { |
58b0afa0 LT |
1842 | int err, mask; |
1843 | ||
1844 | mask = nd->flags & LOOKUP_RCU ? MAY_NOT_BLOCK : 0; | |
1845 | err = inode_permission(idmap, nd->inode, mask | MAY_EXEC); | |
1846 | if (likely(!err)) | |
1847 | return 0; | |
1848 | ||
1849 | // If we failed, and we weren't in LOOKUP_RCU, it's final | |
1850 | if (!(nd->flags & LOOKUP_RCU)) | |
1851 | return err; | |
1852 | ||
1853 | // Drop out of RCU mode to make sure it wasn't transient | |
1854 | if (!try_to_unlazy(nd)) | |
1855 | return -ECHILD; // redo it all non-lazy | |
1856 | ||
1857 | if (err != -ECHILD) // hard error | |
1858 | return err; | |
1859 | ||
4609e1f1 | 1860 | return inode_permission(idmap, nd->inode, MAY_EXEC); |
52094c8a AV |
1861 | } |
1862 | ||
03fa86e9 | 1863 | static int reserve_stack(struct nameidata *nd, struct path *link) |
49055906 | 1864 | { |
49055906 AV |
1865 | if (unlikely(nd->total_link_count++ >= MAXSYMLINKS)) |
1866 | return -ELOOP; | |
4542576b AV |
1867 | |
1868 | if (likely(nd->depth != EMBEDDED_LEVELS)) | |
1869 | return 0; | |
1870 | if (likely(nd->stack != nd->internal)) | |
1871 | return 0; | |
60ef60c7 | 1872 | if (likely(nd_alloc_stack(nd))) |
49055906 | 1873 | return 0; |
60ef60c7 AV |
1874 | |
1875 | if (nd->flags & LOOKUP_RCU) { | |
1876 | // we need to grab link before we do unlazy. And we can't skip | |
1877 | // unlazy even if we fail to grab the link - cleanup needs it | |
03fa86e9 | 1878 | bool grabbed_link = legitimize_path(nd, link, nd->next_seq); |
60ef60c7 | 1879 | |
e5ca024e | 1880 | if (!try_to_unlazy(nd) || !grabbed_link) |
60ef60c7 AV |
1881 | return -ECHILD; |
1882 | ||
1883 | if (nd_alloc_stack(nd)) | |
1884 | return 0; | |
49055906 | 1885 | } |
60ef60c7 | 1886 | return -ENOMEM; |
49055906 AV |
1887 | } |
1888 | ||
b1a81972 AV |
1889 | enum {WALK_TRAILING = 1, WALK_MORE = 2, WALK_NOFOLLOW = 4}; |
1890 | ||
06708adb | 1891 | static const char *pick_link(struct nameidata *nd, struct path *link, |
03fa86e9 | 1892 | struct inode *inode, int flags) |
d63ff28f | 1893 | { |
1cf2665b | 1894 | struct saved *last; |
ad6cc4c3 | 1895 | const char *res; |
03fa86e9 | 1896 | int error = reserve_stack(nd, link); |
ad6cc4c3 | 1897 | |
626de996 | 1898 | if (unlikely(error)) { |
49055906 | 1899 | if (!(nd->flags & LOOKUP_RCU)) |
bc40aee0 | 1900 | path_put(link); |
49055906 | 1901 | return ERR_PTR(error); |
626de996 | 1902 | } |
ab104923 | 1903 | last = nd->stack + nd->depth++; |
1cf2665b | 1904 | last->link = *link; |
fceef393 | 1905 | clear_delayed_call(&last->done); |
03fa86e9 | 1906 | last->seq = nd->next_seq; |
ad6cc4c3 | 1907 | |
b1a81972 | 1908 | if (flags & WALK_TRAILING) { |
ad6cc4c3 AV |
1909 | error = may_follow_link(nd, inode); |
1910 | if (unlikely(error)) | |
1911 | return ERR_PTR(error); | |
1912 | } | |
1913 | ||
dab741e0 MN |
1914 | if (unlikely(nd->flags & LOOKUP_NO_SYMLINKS) || |
1915 | unlikely(link->mnt->mnt_flags & MNT_NOSYMFOLLOW)) | |
ad6cc4c3 AV |
1916 | return ERR_PTR(-ELOOP); |
1917 | ||
e45960c2 MG |
1918 | if (unlikely(atime_needs_update(&last->link, inode))) { |
1919 | if (nd->flags & LOOKUP_RCU) { | |
1920 | if (!try_to_unlazy(nd)) | |
1921 | return ERR_PTR(-ECHILD); | |
1922 | } | |
ad6cc4c3 AV |
1923 | touch_atime(&last->link); |
1924 | cond_resched(); | |
ad6cc4c3 AV |
1925 | } |
1926 | ||
1927 | error = security_inode_follow_link(link->dentry, inode, | |
1928 | nd->flags & LOOKUP_RCU); | |
1929 | if (unlikely(error)) | |
1930 | return ERR_PTR(error); | |
1931 | ||
ad6cc4c3 AV |
1932 | res = READ_ONCE(inode->i_link); |
1933 | if (!res) { | |
1934 | const char * (*get)(struct dentry *, struct inode *, | |
1935 | struct delayed_call *); | |
1936 | get = inode->i_op->get_link; | |
1937 | if (nd->flags & LOOKUP_RCU) { | |
1938 | res = get(NULL, inode, &last->done); | |
e36cffed | 1939 | if (res == ERR_PTR(-ECHILD) && try_to_unlazy(nd)) |
ad6cc4c3 | 1940 | res = get(link->dentry, inode, &last->done); |
ad6cc4c3 AV |
1941 | } else { |
1942 | res = get(link->dentry, inode, &last->done); | |
1943 | } | |
1944 | if (!res) | |
1945 | goto all_done; | |
1946 | if (IS_ERR(res)) | |
1947 | return res; | |
1948 | } | |
1949 | if (*res == '/') { | |
1950 | error = nd_jump_root(nd); | |
1951 | if (unlikely(error)) | |
1952 | return ERR_PTR(error); | |
1953 | while (unlikely(*++res == '/')) | |
1954 | ; | |
1955 | } | |
1956 | if (*res) | |
1957 | return res; | |
1958 | all_done: // pure jump | |
1959 | put_link(nd); | |
1960 | return NULL; | |
d63ff28f AV |
1961 | } |
1962 | ||
3ddcd056 LT |
1963 | /* |
1964 | * Do we need to follow links? We _really_ want to be able | |
1965 | * to do this check without having to look at inode->i_op, | |
1966 | * so we keep a cache of "no, this doesn't need follow_link" | |
1967 | * for the common case. | |
03fa86e9 AV |
1968 | * |
1969 | * NOTE: dentry must be what nd->next_seq had been sampled from. | |
3ddcd056 | 1970 | */ |
b0417d2c | 1971 | static const char *step_into(struct nameidata *nd, int flags, |
a4f5b521 | 1972 | struct dentry *dentry) |
3ddcd056 | 1973 | { |
cbae4d12 | 1974 | struct path path; |
a4f5b521 | 1975 | struct inode *inode; |
3bd8bc89 | 1976 | int err = handle_mounts(nd, dentry, &path); |
cbae4d12 AV |
1977 | |
1978 | if (err < 0) | |
b0417d2c | 1979 | return ERR_PTR(err); |
3bd8bc89 | 1980 | inode = path.dentry->d_inode; |
cbae4d12 | 1981 | if (likely(!d_is_symlink(path.dentry)) || |
8c4efe22 | 1982 | ((flags & WALK_TRAILING) && !(nd->flags & LOOKUP_FOLLOW)) || |
aca2903e | 1983 | (flags & WALK_NOFOLLOW)) { |
8f64fb1c | 1984 | /* not a symlink or should not follow */ |
3bd8bc89 AV |
1985 | if (nd->flags & LOOKUP_RCU) { |
1986 | if (read_seqcount_retry(&path.dentry->d_seq, nd->next_seq)) | |
1987 | return ERR_PTR(-ECHILD); | |
1988 | if (unlikely(!inode)) | |
1989 | return ERR_PTR(-ENOENT); | |
1990 | } else { | |
c99687a0 AV |
1991 | dput(nd->path.dentry); |
1992 | if (nd->path.mnt != path.mnt) | |
1993 | mntput(nd->path.mnt); | |
1994 | } | |
1995 | nd->path = path; | |
8f64fb1c | 1996 | nd->inode = inode; |
03fa86e9 | 1997 | nd->seq = nd->next_seq; |
b0417d2c | 1998 | return NULL; |
8f64fb1c | 1999 | } |
a7f77542 | 2000 | if (nd->flags & LOOKUP_RCU) { |
84f0cd9e | 2001 | /* make sure that d_is_symlink above matches inode */ |
03fa86e9 | 2002 | if (read_seqcount_retry(&path.dentry->d_seq, nd->next_seq)) |
b0417d2c | 2003 | return ERR_PTR(-ECHILD); |
84f0cd9e AV |
2004 | } else { |
2005 | if (path.mnt == nd->path.mnt) | |
2006 | mntget(path.mnt); | |
a7f77542 | 2007 | } |
03fa86e9 | 2008 | return pick_link(nd, &path, inode, flags); |
3ddcd056 LT |
2009 | } |
2010 | ||
b16c001d | 2011 | static struct dentry *follow_dotdot_rcu(struct nameidata *nd) |
957dd41d | 2012 | { |
12487f30 | 2013 | struct dentry *parent, *old; |
957dd41d | 2014 | |
12487f30 AV |
2015 | if (path_equal(&nd->path, &nd->root)) |
2016 | goto in_root; | |
2017 | if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { | |
7ef482fa | 2018 | struct path path; |
efe772d6 | 2019 | unsigned seq; |
7ef482fa AV |
2020 | if (!choose_mountpoint_rcu(real_mount(nd->path.mnt), |
2021 | &nd->root, &path, &seq)) | |
2022 | goto in_root; | |
efe772d6 AV |
2023 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
2024 | return ERR_PTR(-ECHILD); | |
2025 | nd->path = path; | |
2026 | nd->inode = path.dentry->d_inode; | |
2027 | nd->seq = seq; | |
03fa86e9 | 2028 | // makes sure that non-RCU pathwalk could reach this state |
82ef0698 | 2029 | if (read_seqretry(&mount_lock, nd->m_seq)) |
efe772d6 AV |
2030 | return ERR_PTR(-ECHILD); |
2031 | /* we know that mountpoint was pinned */ | |
957dd41d | 2032 | } |
12487f30 AV |
2033 | old = nd->path.dentry; |
2034 | parent = old->d_parent; | |
03fa86e9 AV |
2035 | nd->next_seq = read_seqcount_begin(&parent->d_seq); |
2036 | // makes sure that non-RCU pathwalk could reach this state | |
82ef0698 | 2037 | if (read_seqcount_retry(&old->d_seq, nd->seq)) |
12487f30 AV |
2038 | return ERR_PTR(-ECHILD); |
2039 | if (unlikely(!path_connected(nd->path.mnt, parent))) | |
2040 | return ERR_PTR(-ECHILD); | |
2041 | return parent; | |
2042 | in_root: | |
82ef0698 | 2043 | if (read_seqretry(&mount_lock, nd->m_seq)) |
efe772d6 | 2044 | return ERR_PTR(-ECHILD); |
c2df1968 AV |
2045 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
2046 | return ERR_PTR(-ECHILD); | |
03fa86e9 | 2047 | nd->next_seq = nd->seq; |
51c6546c | 2048 | return nd->path.dentry; |
957dd41d AV |
2049 | } |
2050 | ||
b16c001d | 2051 | static struct dentry *follow_dotdot(struct nameidata *nd) |
957dd41d | 2052 | { |
12487f30 AV |
2053 | struct dentry *parent; |
2054 | ||
2055 | if (path_equal(&nd->path, &nd->root)) | |
2056 | goto in_root; | |
2057 | if (unlikely(nd->path.dentry == nd->path.mnt->mnt_root)) { | |
2aa38470 AV |
2058 | struct path path; |
2059 | ||
2060 | if (!choose_mountpoint(real_mount(nd->path.mnt), | |
2061 | &nd->root, &path)) | |
2062 | goto in_root; | |
165200d6 AV |
2063 | path_put(&nd->path); |
2064 | nd->path = path; | |
2aa38470 | 2065 | nd->inode = path.dentry->d_inode; |
165200d6 AV |
2066 | if (unlikely(nd->flags & LOOKUP_NO_XDEV)) |
2067 | return ERR_PTR(-EXDEV); | |
957dd41d | 2068 | } |
12487f30 AV |
2069 | /* rare case of legitimate dget_parent()... */ |
2070 | parent = dget_parent(nd->path.dentry); | |
2071 | if (unlikely(!path_connected(nd->path.mnt, parent))) { | |
2072 | dput(parent); | |
2073 | return ERR_PTR(-ENOENT); | |
2074 | } | |
12487f30 AV |
2075 | return parent; |
2076 | ||
2077 | in_root: | |
c2df1968 AV |
2078 | if (unlikely(nd->flags & LOOKUP_BENEATH)) |
2079 | return ERR_PTR(-EXDEV); | |
51c6546c | 2080 | return dget(nd->path.dentry); |
957dd41d AV |
2081 | } |
2082 | ||
7521f22b | 2083 | static const char *handle_dots(struct nameidata *nd, int type) |
957dd41d AV |
2084 | { |
2085 | if (type == LAST_DOTDOT) { | |
7521f22b | 2086 | const char *error = NULL; |
c2df1968 | 2087 | struct dentry *parent; |
957dd41d AV |
2088 | |
2089 | if (!nd->root.mnt) { | |
7521f22b | 2090 | error = ERR_PTR(set_root(nd)); |
957dd41d AV |
2091 | if (error) |
2092 | return error; | |
2093 | } | |
2094 | if (nd->flags & LOOKUP_RCU) | |
b16c001d | 2095 | parent = follow_dotdot_rcu(nd); |
957dd41d | 2096 | else |
b16c001d | 2097 | parent = follow_dotdot(nd); |
c2df1968 AV |
2098 | if (IS_ERR(parent)) |
2099 | return ERR_CAST(parent); | |
a4f5b521 | 2100 | error = step_into(nd, WALK_NOFOLLOW, parent); |
c2df1968 | 2101 | if (unlikely(error)) |
957dd41d AV |
2102 | return error; |
2103 | ||
2104 | if (unlikely(nd->flags & LOOKUP_IS_SCOPED)) { | |
2105 | /* | |
2106 | * If there was a racing rename or mount along our | |
2107 | * path, then we can't be sure that ".." hasn't jumped | |
2108 | * above nd->root (and so userspace should retry or use | |
2109 | * some fallback). | |
2110 | */ | |
2111 | smp_rmb(); | |
82ef0698 | 2112 | if (__read_seqcount_retry(&mount_lock.seqcount, nd->m_seq)) |
7521f22b | 2113 | return ERR_PTR(-EAGAIN); |
82ef0698 | 2114 | if (__read_seqcount_retry(&rename_lock.seqcount, nd->r_seq)) |
7521f22b | 2115 | return ERR_PTR(-EAGAIN); |
957dd41d AV |
2116 | } |
2117 | } | |
7521f22b | 2118 | return NULL; |
957dd41d AV |
2119 | } |
2120 | ||
92d27016 | 2121 | static const char *walk_component(struct nameidata *nd, int flags) |
ce57dfc1 | 2122 | { |
db3c9ade | 2123 | struct dentry *dentry; |
ce57dfc1 AV |
2124 | /* |
2125 | * "." and ".." are special - ".." especially so because it has | |
2126 | * to be able to know about the current root directory and | |
2127 | * parent relationships. | |
2128 | */ | |
4693a547 | 2129 | if (unlikely(nd->last_type != LAST_NORM)) { |
1c4ff1a8 | 2130 | if (!(flags & WALK_MORE) && nd->depth) |
4693a547 | 2131 | put_link(nd); |
7521f22b | 2132 | return handle_dots(nd, nd->last_type); |
4693a547 | 2133 | } |
4cb64024 | 2134 | dentry = lookup_fast(nd); |
20e34357 | 2135 | if (IS_ERR(dentry)) |
92d27016 | 2136 | return ERR_CAST(dentry); |
20e34357 | 2137 | if (unlikely(!dentry)) { |
db3c9ade AV |
2138 | dentry = lookup_slow(&nd->last, nd->path.dentry, nd->flags); |
2139 | if (IS_ERR(dentry)) | |
92d27016 | 2140 | return ERR_CAST(dentry); |
ce57dfc1 | 2141 | } |
56676ec3 AV |
2142 | if (!(flags & WALK_MORE) && nd->depth) |
2143 | put_link(nd); | |
a4f5b521 | 2144 | return step_into(nd, flags, dentry); |
ce57dfc1 AV |
2145 | } |
2146 | ||
bfcfaa77 LT |
2147 | /* |
2148 | * We can do the critical dentry name comparison and hashing | |
2149 | * operations one word at a time, but we are limited to: | |
2150 | * | |
2151 | * - Architectures with fast unaligned word accesses. We could | |
2152 | * do a "get_unaligned()" if this helps and is sufficiently | |
2153 | * fast. | |
2154 | * | |
bfcfaa77 LT |
2155 | * - non-CONFIG_DEBUG_PAGEALLOC configurations (so that we |
2156 | * do not trap on the (extremely unlikely) case of a page | |
2157 | * crossing operation. | |
2158 | * | |
2159 | * - Furthermore, we need an efficient 64-bit compile for the | |
2160 | * 64-bit case in order to generate the "number of bytes in | |
2161 | * the final mask". Again, that could be replaced with a | |
2162 | * efficient population count instruction or similar. | |
2163 | */ | |
2164 | #ifdef CONFIG_DCACHE_WORD_ACCESS | |
2165 | ||
f68e556e | 2166 | #include <asm/word-at-a-time.h> |
bfcfaa77 | 2167 | |
468a9428 | 2168 | #ifdef HASH_MIX |
bfcfaa77 | 2169 | |
468a9428 | 2170 | /* Architecture provides HASH_MIX and fold_hash() in <asm/hash.h> */ |
bfcfaa77 | 2171 | |
468a9428 | 2172 | #elif defined(CONFIG_64BIT) |
0fed3ac8 | 2173 | /* |
2a18da7a GS |
2174 | * Register pressure in the mixing function is an issue, particularly |
2175 | * on 32-bit x86, but almost any function requires one state value and | |
2176 | * one temporary. Instead, use a function designed for two state values | |
2177 | * and no temporaries. | |
2178 | * | |
2179 | * This function cannot create a collision in only two iterations, so | |
2180 | * we have two iterations to achieve avalanche. In those two iterations, | |
2181 | * we have six layers of mixing, which is enough to spread one bit's | |
2182 | * influence out to 2^6 = 64 state bits. | |
2183 | * | |
2184 | * Rotate constants are scored by considering either 64 one-bit input | |
2185 | * deltas or 64*63/2 = 2016 two-bit input deltas, and finding the | |
2186 | * probability of that delta causing a change to each of the 128 output | |
2187 | * bits, using a sample of random initial states. | |
2188 | * | |
2189 | * The Shannon entropy of the computed probabilities is then summed | |
2190 | * to produce a score. Ideally, any input change has a 50% chance of | |
2191 | * toggling any given output bit. | |
2192 | * | |
2193 | * Mixing scores (in bits) for (12,45): | |
2194 | * Input delta: 1-bit 2-bit | |
2195 | * 1 round: 713.3 42542.6 | |
2196 | * 2 rounds: 2753.7 140389.8 | |
2197 | * 3 rounds: 5954.1 233458.2 | |
2198 | * 4 rounds: 7862.6 256672.2 | |
2199 | * Perfect: 8192 258048 | |
2200 | * (64*128) (64*63/2 * 128) | |
0fed3ac8 | 2201 | */ |
2a18da7a GS |
2202 | #define HASH_MIX(x, y, a) \ |
2203 | ( x ^= (a), \ | |
2204 | y ^= x, x = rol64(x,12),\ | |
2205 | x += y, y = rol64(y,45),\ | |
2206 | y *= 9 ) | |
bfcfaa77 | 2207 | |
0fed3ac8 | 2208 | /* |
2a18da7a GS |
2209 | * Fold two longs into one 32-bit hash value. This must be fast, but |
2210 | * latency isn't quite as critical, as there is a fair bit of additional | |
2211 | * work done before the hash value is used. | |
0fed3ac8 | 2212 | */ |
2a18da7a | 2213 | static inline unsigned int fold_hash(unsigned long x, unsigned long y) |
0fed3ac8 | 2214 | { |
2a18da7a GS |
2215 | y ^= x * GOLDEN_RATIO_64; |
2216 | y *= GOLDEN_RATIO_64; | |
2217 | return y >> 32; | |
0fed3ac8 GS |
2218 | } |
2219 | ||
bfcfaa77 LT |
2220 | #else /* 32-bit case */ |
2221 | ||
2a18da7a GS |
2222 | /* |
2223 | * Mixing scores (in bits) for (7,20): | |
2224 | * Input delta: 1-bit 2-bit | |
2225 | * 1 round: 330.3 9201.6 | |
2226 | * 2 rounds: 1246.4 25475.4 | |
2227 | * 3 rounds: 1907.1 31295.1 | |
2228 | * 4 rounds: 2042.3 31718.6 | |
2229 | * Perfect: 2048 31744 | |
2230 | * (32*64) (32*31/2 * 64) | |
2231 | */ | |
2232 | #define HASH_MIX(x, y, a) \ | |
2233 | ( x ^= (a), \ | |
2234 | y ^= x, x = rol32(x, 7),\ | |
2235 | x += y, y = rol32(y,20),\ | |
2236 | y *= 9 ) | |
bfcfaa77 | 2237 | |
2a18da7a | 2238 | static inline unsigned int fold_hash(unsigned long x, unsigned long y) |
0fed3ac8 | 2239 | { |
2a18da7a GS |
2240 | /* Use arch-optimized multiply if one exists */ |
2241 | return __hash_32(y ^ __hash_32(x)); | |
0fed3ac8 GS |
2242 | } |
2243 | ||
bfcfaa77 LT |
2244 | #endif |
2245 | ||
2a18da7a GS |
2246 | /* |
2247 | * Return the hash of a string of known length. This is carfully | |
2248 | * designed to match hash_name(), which is the more critical function. | |
2249 | * In particular, we must end by hashing a final word containing 0..7 | |
2250 | * payload bytes, to match the way that hash_name() iterates until it | |
2251 | * finds the delimiter after the name. | |
2252 | */ | |
8387ff25 | 2253 | unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) |
bfcfaa77 | 2254 | { |
8387ff25 | 2255 | unsigned long a, x = 0, y = (unsigned long)salt; |
bfcfaa77 LT |
2256 | |
2257 | for (;;) { | |
fcfd2fbf GS |
2258 | if (!len) |
2259 | goto done; | |
e419b4cc | 2260 | a = load_unaligned_zeropad(name); |
bfcfaa77 LT |
2261 | if (len < sizeof(unsigned long)) |
2262 | break; | |
2a18da7a | 2263 | HASH_MIX(x, y, a); |
bfcfaa77 LT |
2264 | name += sizeof(unsigned long); |
2265 | len -= sizeof(unsigned long); | |
bfcfaa77 | 2266 | } |
2a18da7a | 2267 | x ^= a & bytemask_from_count(len); |
bfcfaa77 | 2268 | done: |
2a18da7a | 2269 | return fold_hash(x, y); |
bfcfaa77 LT |
2270 | } |
2271 | EXPORT_SYMBOL(full_name_hash); | |
2272 | ||
fcfd2fbf | 2273 | /* Return the "hash_len" (hash and length) of a null-terminated string */ |
8387ff25 | 2274 | u64 hashlen_string(const void *salt, const char *name) |
fcfd2fbf | 2275 | { |
8387ff25 LT |
2276 | unsigned long a = 0, x = 0, y = (unsigned long)salt; |
2277 | unsigned long adata, mask, len; | |
fcfd2fbf GS |
2278 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
2279 | ||
8387ff25 LT |
2280 | len = 0; |
2281 | goto inside; | |
2282 | ||
fcfd2fbf | 2283 | do { |
2a18da7a | 2284 | HASH_MIX(x, y, a); |
fcfd2fbf | 2285 | len += sizeof(unsigned long); |
8387ff25 | 2286 | inside: |
fcfd2fbf GS |
2287 | a = load_unaligned_zeropad(name+len); |
2288 | } while (!has_zero(a, &adata, &constants)); | |
2289 | ||
2290 | adata = prep_zero_mask(a, adata, &constants); | |
2291 | mask = create_zero_mask(adata); | |
2a18da7a | 2292 | x ^= a & zero_bytemask(mask); |
fcfd2fbf | 2293 | |
2a18da7a | 2294 | return hashlen_create(fold_hash(x, y), len + find_zero(mask)); |
fcfd2fbf GS |
2295 | } |
2296 | EXPORT_SYMBOL(hashlen_string); | |
2297 | ||
bfcfaa77 LT |
2298 | /* |
2299 | * Calculate the length and hash of the path component, and | |
ba848a77 | 2300 | * return the length as the result. |
bfcfaa77 | 2301 | */ |
13694f0d LT |
2302 | static inline const char *hash_name(struct nameidata *nd, |
2303 | const char *name, | |
2304 | unsigned long *lastword) | |
bfcfaa77 | 2305 | { |
13694f0d | 2306 | unsigned long a, b, x, y = (unsigned long)nd->path.dentry; |
8387ff25 | 2307 | unsigned long adata, bdata, mask, len; |
36126f8f | 2308 | const struct word_at_a_time constants = WORD_AT_A_TIME_CONSTANTS; |
bfcfaa77 | 2309 | |
13694f0d LT |
2310 | /* |
2311 | * The first iteration is special, because it can result in | |
2312 | * '.' and '..' and has no mixing other than the final fold. | |
2313 | */ | |
2314 | a = load_unaligned_zeropad(name); | |
2315 | b = a ^ REPEAT_BYTE('/'); | |
2316 | if (has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants)) { | |
2317 | adata = prep_zero_mask(a, adata, &constants); | |
2318 | bdata = prep_zero_mask(b, bdata, &constants); | |
2319 | mask = create_zero_mask(adata | bdata); | |
2320 | a &= zero_bytemask(mask); | |
2321 | *lastword = a; | |
2322 | len = find_zero(mask); | |
2323 | nd->last.hash = fold_hash(a, y); | |
2324 | nd->last.len = len; | |
2325 | return name + len; | |
2326 | } | |
8387ff25 | 2327 | |
13694f0d LT |
2328 | len = 0; |
2329 | x = 0; | |
bfcfaa77 | 2330 | do { |
2a18da7a | 2331 | HASH_MIX(x, y, a); |
bfcfaa77 | 2332 | len += sizeof(unsigned long); |
e419b4cc | 2333 | a = load_unaligned_zeropad(name+len); |
36126f8f LT |
2334 | b = a ^ REPEAT_BYTE('/'); |
2335 | } while (!(has_zero(a, &adata, &constants) | has_zero(b, &bdata, &constants))); | |
2336 | ||
2337 | adata = prep_zero_mask(a, adata, &constants); | |
2338 | bdata = prep_zero_mask(b, bdata, &constants); | |
36126f8f | 2339 | mask = create_zero_mask(adata | bdata); |
ba848a77 | 2340 | a &= zero_bytemask(mask); |
ba848a77 | 2341 | x ^= a; |
631e1a71 | 2342 | len += find_zero(mask); |
13694f0d | 2343 | *lastword = 0; // Multi-word components cannot be DOT or DOTDOT |
36126f8f | 2344 | |
631e1a71 LT |
2345 | nd->last.hash = fold_hash(x, y); |
2346 | nd->last.len = len; | |
13694f0d | 2347 | return name + len; |
bfcfaa77 LT |
2348 | } |
2349 | ||
ba848a77 LT |
2350 | /* |
2351 | * Note that the 'last' word is always zero-masked, but | |
2352 | * was loaded as a possibly big-endian word. | |
2353 | */ | |
2354 | #ifdef __BIG_ENDIAN | |
2355 | #define LAST_WORD_IS_DOT (0x2eul << (BITS_PER_LONG-8)) | |
2356 | #define LAST_WORD_IS_DOTDOT (0x2e2eul << (BITS_PER_LONG-16)) | |
2357 | #endif | |
2358 | ||
2a18da7a | 2359 | #else /* !CONFIG_DCACHE_WORD_ACCESS: Slow, byte-at-a-time version */ |
bfcfaa77 | 2360 | |
fcfd2fbf | 2361 | /* Return the hash of a string of known length */ |
8387ff25 | 2362 | unsigned int full_name_hash(const void *salt, const char *name, unsigned int len) |
0145acc2 | 2363 | { |
8387ff25 | 2364 | unsigned long hash = init_name_hash(salt); |
0145acc2 | 2365 | while (len--) |
fcfd2fbf | 2366 | hash = partial_name_hash((unsigned char)*name++, hash); |
0145acc2 LT |
2367 | return end_name_hash(hash); |
2368 | } | |
ae942ae7 | 2369 | EXPORT_SYMBOL(full_name_hash); |
0145acc2 | 2370 | |
fcfd2fbf | 2371 | /* Return the "hash_len" (hash and length) of a null-terminated string */ |
8387ff25 | 2372 | u64 hashlen_string(const void *salt, const char *name) |
fcfd2fbf | 2373 | { |
8387ff25 | 2374 | unsigned long hash = init_name_hash(salt); |
fcfd2fbf GS |
2375 | unsigned long len = 0, c; |
2376 | ||
2377 | c = (unsigned char)*name; | |
e0ab7af9 | 2378 | while (c) { |
fcfd2fbf GS |
2379 | len++; |
2380 | hash = partial_name_hash(c, hash); | |
2381 | c = (unsigned char)name[len]; | |
e0ab7af9 | 2382 | } |
fcfd2fbf GS |
2383 | return hashlen_create(end_name_hash(hash), len); |
2384 | } | |
f2a031b6 | 2385 | EXPORT_SYMBOL(hashlen_string); |
fcfd2fbf | 2386 | |
200e9ef7 LT |
2387 | /* |
2388 | * We know there's a real path component here of at least | |
2389 | * one character. | |
2390 | */ | |
13694f0d | 2391 | static inline const char *hash_name(struct nameidata *nd, const char *name, unsigned long *lastword) |
200e9ef7 | 2392 | { |
631e1a71 | 2393 | unsigned long hash = init_name_hash(nd->path.dentry); |
ba848a77 | 2394 | unsigned long len = 0, c, last = 0; |
200e9ef7 LT |
2395 | |
2396 | c = (unsigned char)*name; | |
2397 | do { | |
ba848a77 | 2398 | last = (last << 8) + c; |
200e9ef7 LT |
2399 | len++; |
2400 | hash = partial_name_hash(c, hash); | |
2401 | c = (unsigned char)name[len]; | |
2402 | } while (c && c != '/'); | |
13694f0d LT |
2403 | |
2404 | // This is reliable for DOT or DOTDOT, since the component | |
2405 | // cannot contain NUL characters - top bits being zero means | |
2406 | // we cannot have had any other pathnames. | |
ba848a77 | 2407 | *lastword = last; |
631e1a71 LT |
2408 | nd->last.hash = end_name_hash(hash); |
2409 | nd->last.len = len; | |
13694f0d | 2410 | return name + len; |
200e9ef7 LT |
2411 | } |
2412 | ||
bfcfaa77 LT |
2413 | #endif |
2414 | ||
ba848a77 LT |
2415 | #ifndef LAST_WORD_IS_DOT |
2416 | #define LAST_WORD_IS_DOT 0x2e | |
2417 | #define LAST_WORD_IS_DOTDOT 0x2e2e | |
2418 | #endif | |
2419 | ||
1da177e4 LT |
2420 | /* |
2421 | * Name resolution. | |
ea3834d9 PM |
2422 | * This is the basic name resolution function, turning a pathname into |
2423 | * the final dentry. We expect 'base' to be positive and a directory. | |
1da177e4 | 2424 | * |
ea3834d9 PM |
2425 | * Returns 0 and nd will have valid dentry and mnt on success. |
2426 | * Returns error and drops reference to input namei data on failure. | |
1da177e4 | 2427 | */ |
6de88d72 | 2428 | static int link_path_walk(const char *name, struct nameidata *nd) |
1da177e4 | 2429 | { |
d8d4611a | 2430 | int depth = 0; // depth <= nd->depth |
1da177e4 | 2431 | int err; |
32cd7468 | 2432 | |
b4c03536 | 2433 | nd->last_type = LAST_ROOT; |
c108837e | 2434 | nd->flags |= LOOKUP_PARENT; |
9b5858e9 AV |
2435 | if (IS_ERR(name)) |
2436 | return PTR_ERR(name); | |
8564124c MG |
2437 | if (*name == '/') { |
2438 | do { | |
2439 | name++; | |
2440 | } while (unlikely(*name == '/')); | |
2441 | } | |
2442 | if (unlikely(!*name)) { | |
1a97d899 | 2443 | nd->dir_mode = 0; // short-circuit the 'hardening' idiocy |
9e18f10a | 2444 | return 0; |
1a97d899 | 2445 | } |
1da177e4 | 2446 | |
1da177e4 LT |
2447 | /* At this point we know we have a real path component. */ |
2448 | for(;;) { | |
4609e1f1 | 2449 | struct mnt_idmap *idmap; |
92d27016 | 2450 | const char *link; |
ba848a77 | 2451 | unsigned long lastword; |
1da177e4 | 2452 | |
4609e1f1 | 2453 | idmap = mnt_idmap(nd->path.mnt); |
4609e1f1 | 2454 | err = may_lookup(idmap, nd); |
8564124c | 2455 | if (unlikely(err)) |
3595e234 | 2456 | return err; |
1da177e4 | 2457 | |
7d286849 | 2458 | nd->last.name = name; |
13694f0d | 2459 | name = hash_name(nd, name, &lastword); |
1da177e4 | 2460 | |
ba848a77 LT |
2461 | switch(lastword) { |
2462 | case LAST_WORD_IS_DOTDOT: | |
ba848a77 LT |
2463 | nd->last_type = LAST_DOTDOT; |
2464 | nd->state |= ND_JUMPED; | |
2465 | break; | |
2466 | ||
2467 | case LAST_WORD_IS_DOT: | |
ba848a77 LT |
2468 | nd->last_type = LAST_DOT; |
2469 | break; | |
2470 | ||
2471 | default: | |
ba848a77 | 2472 | nd->last_type = LAST_NORM; |
bcba1e7d | 2473 | nd->state &= ~ND_JUMPED; |
ba848a77 LT |
2474 | |
2475 | struct dentry *parent = nd->path.dentry; | |
5a202bcd | 2476 | if (unlikely(parent->d_flags & DCACHE_OP_HASH)) { |
7d286849 | 2477 | err = parent->d_op->d_hash(parent, &nd->last); |
5a202bcd | 2478 | if (err < 0) |
3595e234 | 2479 | return err; |
5a202bcd AV |
2480 | } |
2481 | } | |
fe479a58 | 2482 | |
d6bb3e90 | 2483 | if (!*name) |
bdf6cbf1 | 2484 | goto OK; |
200e9ef7 LT |
2485 | /* |
2486 | * If it wasn't NUL, we know it was '/'. Skip that | |
2487 | * slash, and continue until no more slashes. | |
2488 | */ | |
2489 | do { | |
d6bb3e90 LT |
2490 | name++; |
2491 | } while (unlikely(*name == '/')); | |
8620c238 AV |
2492 | if (unlikely(!*name)) { |
2493 | OK: | |
d8d4611a | 2494 | /* pathname or trailing symlink, done */ |
c108837e | 2495 | if (!depth) { |
e67fe633 | 2496 | nd->dir_vfsuid = i_uid_into_vfsuid(idmap, nd->inode); |
0f705953 | 2497 | nd->dir_mode = nd->inode->i_mode; |
c108837e | 2498 | nd->flags &= ~LOOKUP_PARENT; |
8620c238 | 2499 | return 0; |
c108837e | 2500 | } |
8620c238 | 2501 | /* last component of nested symlink */ |
d8d4611a | 2502 | name = nd->stack[--depth].name; |
8c4efe22 | 2503 | link = walk_component(nd, 0); |
1c4ff1a8 AV |
2504 | } else { |
2505 | /* not the last component */ | |
8c4efe22 | 2506 | link = walk_component(nd, WALK_MORE); |
8620c238 | 2507 | } |
92d27016 AV |
2508 | if (unlikely(link)) { |
2509 | if (IS_ERR(link)) | |
2510 | return PTR_ERR(link); | |
2511 | /* a symlink to follow */ | |
d8d4611a | 2512 | nd->stack[depth++].name = name; |
92d27016 AV |
2513 | name = link; |
2514 | continue; | |
31e6b01f | 2515 | } |
97242f99 AV |
2516 | if (unlikely(!d_can_lookup(nd->path.dentry))) { |
2517 | if (nd->flags & LOOKUP_RCU) { | |
e36cffed | 2518 | if (!try_to_unlazy(nd)) |
97242f99 AV |
2519 | return -ECHILD; |
2520 | } | |
3595e234 | 2521 | return -ENOTDIR; |
97242f99 | 2522 | } |
1da177e4 | 2523 | } |
1da177e4 LT |
2524 | } |
2525 | ||
edc2b1da | 2526 | /* must be paired with terminate_walk() */ |
c8a53ee5 | 2527 | static const char *path_init(struct nameidata *nd, unsigned flags) |
31e6b01f | 2528 | { |
740a1678 | 2529 | int error; |
5b313bcb | 2530 | const char *s = nd->pathname; |
31e6b01f | 2531 | |
6c6ec2b0 JA |
2532 | /* LOOKUP_CACHED requires RCU, ask caller to retry */ |
2533 | if ((flags & (LOOKUP_RCU | LOOKUP_CACHED)) == LOOKUP_CACHED) | |
2534 | return ERR_PTR(-EAGAIN); | |
2535 | ||
c0eb027e LT |
2536 | if (!*s) |
2537 | flags &= ~LOOKUP_RCU; | |
edc2b1da AV |
2538 | if (flags & LOOKUP_RCU) |
2539 | rcu_read_lock(); | |
03fa86e9 AV |
2540 | else |
2541 | nd->seq = nd->next_seq = 0; | |
c0eb027e | 2542 | |
bcba1e7d AV |
2543 | nd->flags = flags; |
2544 | nd->state |= ND_JUMPED; | |
ab87f9a5 AS |
2545 | |
2546 | nd->m_seq = __read_seqcount_begin(&mount_lock.seqcount); | |
2547 | nd->r_seq = __read_seqcount_begin(&rename_lock.seqcount); | |
2548 | smp_rmb(); | |
2549 | ||
bcba1e7d | 2550 | if (nd->state & ND_ROOT_PRESET) { |
b18825a7 DH |
2551 | struct dentry *root = nd->root.dentry; |
2552 | struct inode *inode = root->d_inode; | |
93893862 AV |
2553 | if (*s && unlikely(!d_can_lookup(root))) |
2554 | return ERR_PTR(-ENOTDIR); | |
5b6ca027 AV |
2555 | nd->path = nd->root; |
2556 | nd->inode = inode; | |
2557 | if (flags & LOOKUP_RCU) { | |
ab87f9a5 | 2558 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); |
8f47a016 | 2559 | nd->root_seq = nd->seq; |
5b6ca027 AV |
2560 | } else { |
2561 | path_get(&nd->path); | |
2562 | } | |
368ee9ba | 2563 | return s; |
5b6ca027 AV |
2564 | } |
2565 | ||
31e6b01f | 2566 | nd->root.mnt = NULL; |
31e6b01f | 2567 | |
8db52c7e AS |
2568 | /* Absolute pathname -- fetch the root (LOOKUP_IN_ROOT uses nd->dfd). */ |
2569 | if (*s == '/' && !(flags & LOOKUP_IN_ROOT)) { | |
740a1678 AS |
2570 | error = nd_jump_root(nd); |
2571 | if (unlikely(error)) | |
2572 | return ERR_PTR(error); | |
2573 | return s; | |
8db52c7e AS |
2574 | } |
2575 | ||
2576 | /* Relative pathname -- get the starting-point it is relative to. */ | |
2577 | if (nd->dfd == AT_FDCWD) { | |
e41f7d4e AV |
2578 | if (flags & LOOKUP_RCU) { |
2579 | struct fs_struct *fs = current->fs; | |
2580 | unsigned seq; | |
31e6b01f | 2581 | |
e41f7d4e AV |
2582 | do { |
2583 | seq = read_seqcount_begin(&fs->seq); | |
2584 | nd->path = fs->pwd; | |
ef55d917 | 2585 | nd->inode = nd->path.dentry->d_inode; |
e41f7d4e AV |
2586 | nd->seq = __read_seqcount_begin(&nd->path.dentry->d_seq); |
2587 | } while (read_seqcount_retry(&fs->seq, seq)); | |
2588 | } else { | |
2589 | get_fs_pwd(current->fs, &nd->path); | |
ef55d917 | 2590 | nd->inode = nd->path.dentry->d_inode; |
e41f7d4e | 2591 | } |
31e6b01f | 2592 | } else { |
582aa64a | 2593 | /* Caller must check execute permissions on the starting path component */ |
04818199 | 2594 | CLASS(fd_raw, f)(nd->dfd); |
31e6b01f NP |
2595 | struct dentry *dentry; |
2596 | ||
04818199 | 2597 | if (fd_empty(f)) |
368ee9ba | 2598 | return ERR_PTR(-EBADF); |
31e6b01f | 2599 | |
42bd2af5 | 2600 | if (flags & LOOKUP_LINKAT_EMPTY) { |
1da91ea8 | 2601 | if (fd_file(f)->f_cred != current_cred() && |
04818199 | 2602 | !ns_capable(fd_file(f)->f_cred->user_ns, CAP_DAC_READ_SEARCH)) |
42bd2af5 | 2603 | return ERR_PTR(-ENOENT); |
42bd2af5 LT |
2604 | } |
2605 | ||
1da91ea8 | 2606 | dentry = fd_file(f)->f_path.dentry; |
31e6b01f | 2607 | |
04818199 | 2608 | if (*s && unlikely(!d_can_lookup(dentry))) |
edc2b1da | 2609 | return ERR_PTR(-ENOTDIR); |
31e6b01f | 2610 | |
1da91ea8 | 2611 | nd->path = fd_file(f)->f_path; |
e41f7d4e | 2612 | if (flags & LOOKUP_RCU) { |
34a26b99 AV |
2613 | nd->inode = nd->path.dentry->d_inode; |
2614 | nd->seq = read_seqcount_begin(&nd->path.dentry->d_seq); | |
e41f7d4e | 2615 | } else { |
2903ff01 | 2616 | path_get(&nd->path); |
34a26b99 | 2617 | nd->inode = nd->path.dentry->d_inode; |
e41f7d4e | 2618 | } |
31e6b01f | 2619 | } |
8db52c7e | 2620 | |
adb21d2b AS |
2621 | /* For scoped-lookups we need to set the root to the dirfd as well. */ |
2622 | if (flags & LOOKUP_IS_SCOPED) { | |
2623 | nd->root = nd->path; | |
2624 | if (flags & LOOKUP_RCU) { | |
2625 | nd->root_seq = nd->seq; | |
2626 | } else { | |
2627 | path_get(&nd->root); | |
bcba1e7d | 2628 | nd->state |= ND_ROOT_GRABBED; |
adb21d2b AS |
2629 | } |
2630 | } | |
2631 | return s; | |
9b4a9b14 AV |
2632 | } |
2633 | ||
1ccac622 | 2634 | static inline const char *lookup_last(struct nameidata *nd) |
bd92d7fe AV |
2635 | { |
2636 | if (nd->last_type == LAST_NORM && nd->last.name[nd->last.len]) | |
2637 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; | |
2638 | ||
c108837e | 2639 | return walk_component(nd, WALK_TRAILING); |
bd92d7fe AV |
2640 | } |
2641 | ||
4f757f3c AV |
2642 | static int handle_lookup_down(struct nameidata *nd) |
2643 | { | |
c153007b | 2644 | if (!(nd->flags & LOOKUP_RCU)) |
db3c9ade | 2645 | dget(nd->path.dentry); |
03fa86e9 | 2646 | nd->next_seq = nd->seq; |
a4f5b521 | 2647 | return PTR_ERR(step_into(nd, WALK_NOFOLLOW, nd->path.dentry)); |
4f757f3c AV |
2648 | } |
2649 | ||
6f672f7b | 2650 | /* Returns 0 and nd will be valid on success; Returns error, otherwise. */ |
c8a53ee5 | 2651 | static int path_lookupat(struct nameidata *nd, unsigned flags, struct path *path) |
9b4a9b14 | 2652 | { |
c8a53ee5 | 2653 | const char *s = path_init(nd, flags); |
bd92d7fe | 2654 | int err; |
31e6b01f | 2655 | |
9b5858e9 | 2656 | if (unlikely(flags & LOOKUP_DOWN) && !IS_ERR(s)) { |
4f757f3c | 2657 | err = handle_lookup_down(nd); |
5f336e72 AV |
2658 | if (unlikely(err < 0)) |
2659 | s = ERR_PTR(err); | |
4f757f3c AV |
2660 | } |
2661 | ||
1ccac622 AV |
2662 | while (!(err = link_path_walk(s, nd)) && |
2663 | (s = lookup_last(nd)) != NULL) | |
2664 | ; | |
4f0ed93f AV |
2665 | if (!err && unlikely(nd->flags & LOOKUP_MOUNTPOINT)) { |
2666 | err = handle_lookup_down(nd); | |
bcba1e7d | 2667 | nd->state &= ~ND_JUMPED; // no d_weak_revalidate(), please... |
4f0ed93f | 2668 | } |
9f1fafee AV |
2669 | if (!err) |
2670 | err = complete_walk(nd); | |
bd92d7fe | 2671 | |
deb106c6 AV |
2672 | if (!err && nd->flags & LOOKUP_DIRECTORY) |
2673 | if (!d_can_lookup(nd->path.dentry)) | |
bd23a539 | 2674 | err = -ENOTDIR; |
625b6d10 AV |
2675 | if (!err) { |
2676 | *path = nd->path; | |
2677 | nd->path.mnt = NULL; | |
2678 | nd->path.dentry = NULL; | |
2679 | } | |
2680 | terminate_walk(nd); | |
bd92d7fe | 2681 | return err; |
ee0827cd | 2682 | } |
31e6b01f | 2683 | |
794ebcea | 2684 | int filename_lookup(int dfd, struct filename *name, unsigned flags, |
31d921c7 | 2685 | struct path *path, struct path *root) |
ee0827cd | 2686 | { |
894bc8c4 | 2687 | int retval; |
9883d185 | 2688 | struct nameidata nd; |
abc9f5be AV |
2689 | if (IS_ERR(name)) |
2690 | return PTR_ERR(name); | |
06422964 | 2691 | set_nameidata(&nd, dfd, name, root); |
c8a53ee5 | 2692 | retval = path_lookupat(&nd, flags | LOOKUP_RCU, path); |
ee0827cd | 2693 | if (unlikely(retval == -ECHILD)) |
c8a53ee5 | 2694 | retval = path_lookupat(&nd, flags, path); |
ee0827cd | 2695 | if (unlikely(retval == -ESTALE)) |
c8a53ee5 | 2696 | retval = path_lookupat(&nd, flags | LOOKUP_REVAL, path); |
31e6b01f | 2697 | |
f78570dd | 2698 | if (likely(!retval)) |
161aff1d AV |
2699 | audit_inode(name, path->dentry, |
2700 | flags & LOOKUP_MOUNTPOINT ? AUDIT_INODE_NOEVAL : 0); | |
9883d185 | 2701 | restore_nameidata(); |
020250f3 DK |
2702 | return retval; |
2703 | } | |
2704 | ||
6f672f7b | 2705 | /* Returns 0 and nd will be valid on success; Returns error, otherwise. */ |
c8a53ee5 | 2706 | static int path_parentat(struct nameidata *nd, unsigned flags, |
391172c4 | 2707 | struct path *parent) |
8bcb77fa | 2708 | { |
c8a53ee5 | 2709 | const char *s = path_init(nd, flags); |
9b5858e9 | 2710 | int err = link_path_walk(s, nd); |
8bcb77fa AV |
2711 | if (!err) |
2712 | err = complete_walk(nd); | |
391172c4 AV |
2713 | if (!err) { |
2714 | *parent = nd->path; | |
2715 | nd->path.mnt = NULL; | |
2716 | nd->path.dentry = NULL; | |
2717 | } | |
2718 | terminate_walk(nd); | |
8bcb77fa AV |
2719 | return err; |
2720 | } | |
2721 | ||
0766ec82 | 2722 | /* Note: this does not consume "name" */ |
74d7970f NJ |
2723 | static int __filename_parentat(int dfd, struct filename *name, |
2724 | unsigned int flags, struct path *parent, | |
2725 | struct qstr *last, int *type, | |
2726 | const struct path *root) | |
8bcb77fa AV |
2727 | { |
2728 | int retval; | |
9883d185 | 2729 | struct nameidata nd; |
8bcb77fa | 2730 | |
5c31b6ce | 2731 | if (IS_ERR(name)) |
0ee50b47 | 2732 | return PTR_ERR(name); |
74d7970f | 2733 | set_nameidata(&nd, dfd, name, root); |
c8a53ee5 | 2734 | retval = path_parentat(&nd, flags | LOOKUP_RCU, parent); |
8bcb77fa | 2735 | if (unlikely(retval == -ECHILD)) |
c8a53ee5 | 2736 | retval = path_parentat(&nd, flags, parent); |
8bcb77fa | 2737 | if (unlikely(retval == -ESTALE)) |
c8a53ee5 | 2738 | retval = path_parentat(&nd, flags | LOOKUP_REVAL, parent); |
391172c4 AV |
2739 | if (likely(!retval)) { |
2740 | *last = nd.last; | |
2741 | *type = nd.last_type; | |
c9b07eab | 2742 | audit_inode(name, parent->dentry, AUDIT_INODE_PARENT); |
391172c4 | 2743 | } |
9883d185 | 2744 | restore_nameidata(); |
0ee50b47 DK |
2745 | return retval; |
2746 | } | |
2747 | ||
74d7970f NJ |
2748 | static int filename_parentat(int dfd, struct filename *name, |
2749 | unsigned int flags, struct path *parent, | |
2750 | struct qstr *last, int *type) | |
2751 | { | |
2752 | return __filename_parentat(dfd, name, flags, parent, last, type, NULL); | |
2753 | } | |
2754 | ||
79714f72 | 2755 | /* does lookup, returns the object with parent locked */ |
74d016ec | 2756 | static struct dentry *__kern_path_locked(int dfd, struct filename *name, struct path *path) |
5590ff0d | 2757 | { |
a681b7c1 | 2758 | struct path parent_path __free(path_put) = {}; |
5c31b6ce | 2759 | struct dentry *d; |
391172c4 | 2760 | struct qstr last; |
0ee50b47 | 2761 | int type, error; |
51689104 | 2762 | |
a681b7c1 | 2763 | error = filename_parentat(dfd, name, 0, &parent_path, &last, &type); |
0ee50b47 DK |
2764 | if (error) |
2765 | return ERR_PTR(error); | |
a681b7c1 | 2766 | if (unlikely(type != LAST_NORM)) |
5c31b6ce | 2767 | return ERR_PTR(-EINVAL); |
a681b7c1 CB |
2768 | inode_lock_nested(parent_path.dentry->d_inode, I_MUTEX_PARENT); |
2769 | d = lookup_one_qstr_excl(&last, parent_path.dentry, 0); | |
79714f72 | 2770 | if (IS_ERR(d)) { |
a681b7c1 CB |
2771 | inode_unlock(parent_path.dentry->d_inode); |
2772 | return d; | |
79714f72 | 2773 | } |
a681b7c1 CB |
2774 | path->dentry = no_free_ptr(parent_path.dentry); |
2775 | path->mnt = no_free_ptr(parent_path.mnt); | |
79714f72 | 2776 | return d; |
5590ff0d UD |
2777 | } |
2778 | ||
c86b300b CB |
2779 | struct dentry *kern_path_locked_negative(const char *name, struct path *path) |
2780 | { | |
a681b7c1 | 2781 | struct path parent_path __free(path_put) = {}; |
c86b300b CB |
2782 | struct filename *filename __free(putname) = getname_kernel(name); |
2783 | struct dentry *d; | |
2784 | struct qstr last; | |
2785 | int type, error; | |
2786 | ||
a681b7c1 | 2787 | error = filename_parentat(AT_FDCWD, filename, 0, &parent_path, &last, &type); |
c86b300b CB |
2788 | if (error) |
2789 | return ERR_PTR(error); | |
a681b7c1 | 2790 | if (unlikely(type != LAST_NORM)) |
c86b300b | 2791 | return ERR_PTR(-EINVAL); |
a681b7c1 CB |
2792 | inode_lock_nested(parent_path.dentry->d_inode, I_MUTEX_PARENT); |
2793 | d = lookup_one_qstr_excl_raw(&last, parent_path.dentry, 0); | |
c86b300b | 2794 | if (IS_ERR(d)) { |
a681b7c1 CB |
2795 | inode_unlock(parent_path.dentry->d_inode); |
2796 | return d; | |
c86b300b | 2797 | } |
a681b7c1 CB |
2798 | path->dentry = no_free_ptr(parent_path.dentry); |
2799 | path->mnt = no_free_ptr(parent_path.mnt); | |
c86b300b CB |
2800 | return d; |
2801 | } | |
2802 | ||
0766ec82 SB |
2803 | struct dentry *kern_path_locked(const char *name, struct path *path) |
2804 | { | |
2805 | struct filename *filename = getname_kernel(name); | |
74d016ec | 2806 | struct dentry *res = __kern_path_locked(AT_FDCWD, filename, path); |
0766ec82 SB |
2807 | |
2808 | putname(filename); | |
2809 | return res; | |
2810 | } | |
2811 | ||
74d016ec AV |
2812 | struct dentry *user_path_locked_at(int dfd, const char __user *name, struct path *path) |
2813 | { | |
2814 | struct filename *filename = getname(name); | |
2815 | struct dentry *res = __kern_path_locked(dfd, filename, path); | |
2816 | ||
2817 | putname(filename); | |
2818 | return res; | |
2819 | } | |
2820 | EXPORT_SYMBOL(user_path_locked_at); | |
2821 | ||
d1811465 AV |
2822 | int kern_path(const char *name, unsigned int flags, struct path *path) |
2823 | { | |
794ebcea SB |
2824 | struct filename *filename = getname_kernel(name); |
2825 | int ret = filename_lookup(AT_FDCWD, filename, flags, path, NULL); | |
2826 | ||
2827 | putname(filename); | |
2828 | return ret; | |
2829 | ||
d1811465 | 2830 | } |
4d359507 | 2831 | EXPORT_SYMBOL(kern_path); |
d1811465 | 2832 | |
74d7970f NJ |
2833 | /** |
2834 | * vfs_path_parent_lookup - lookup a parent path relative to a dentry-vfsmount pair | |
2835 | * @filename: filename structure | |
2836 | * @flags: lookup flags | |
2837 | * @parent: pointer to struct path to fill | |
2838 | * @last: last component | |
2839 | * @type: type of the last component | |
2840 | * @root: pointer to struct path of the base directory | |
2841 | */ | |
2842 | int vfs_path_parent_lookup(struct filename *filename, unsigned int flags, | |
2843 | struct path *parent, struct qstr *last, int *type, | |
2844 | const struct path *root) | |
2845 | { | |
2846 | return __filename_parentat(AT_FDCWD, filename, flags, parent, last, | |
2847 | type, root); | |
2848 | } | |
2849 | EXPORT_SYMBOL(vfs_path_parent_lookup); | |
2850 | ||
16f18200 JJS |
2851 | /** |
2852 | * vfs_path_lookup - lookup a file path relative to a dentry-vfsmount pair | |
2853 | * @dentry: pointer to dentry of the base directory | |
2854 | * @mnt: pointer to vfs mount of the base directory | |
2855 | * @name: pointer to file name | |
2856 | * @flags: lookup flags | |
e0a01249 | 2857 | * @path: pointer to struct path to fill |
16f18200 JJS |
2858 | */ |
2859 | int vfs_path_lookup(struct dentry *dentry, struct vfsmount *mnt, | |
2860 | const char *name, unsigned int flags, | |
e0a01249 | 2861 | struct path *path) |
16f18200 | 2862 | { |
794ebcea | 2863 | struct filename *filename; |
9ad1aaa6 | 2864 | struct path root = {.mnt = mnt, .dentry = dentry}; |
794ebcea SB |
2865 | int ret; |
2866 | ||
2867 | filename = getname_kernel(name); | |
9ad1aaa6 | 2868 | /* the first argument of filename_lookup() is ignored with root */ |
794ebcea SB |
2869 | ret = filename_lookup(AT_FDCWD, filename, flags, path, &root); |
2870 | putname(filename); | |
2871 | return ret; | |
16f18200 | 2872 | } |
4d359507 | 2873 | EXPORT_SYMBOL(vfs_path_lookup); |
16f18200 | 2874 | |
390e34bc | 2875 | static int lookup_noperm_common(struct qstr *qname, struct dentry *base) |
057f6c01 | 2876 | { |
390e34bc N |
2877 | const char *name = qname->name; |
2878 | u32 len = qname->len; | |
2879 | ||
2880 | qname->hash = full_name_hash(base, name, len); | |
6a96ba54 | 2881 | if (!len) |
3c95f0dc | 2882 | return -EACCES; |
6a96ba54 | 2883 | |
42c3732f CL |
2884 | if (is_dot_dotdot(name, len)) |
2885 | return -EACCES; | |
21d8a15a | 2886 | |
6a96ba54 | 2887 | while (len--) { |
3c95f0dc | 2888 | unsigned int c = *(const unsigned char *)name++; |
6a96ba54 | 2889 | if (c == '/' || c == '\0') |
3c95f0dc | 2890 | return -EACCES; |
6a96ba54 | 2891 | } |
5a202bcd AV |
2892 | /* |
2893 | * See if the low-level filesystem might want | |
2894 | * to use its own hash.. | |
2895 | */ | |
2896 | if (base->d_flags & DCACHE_OP_HASH) { | |
390e34bc | 2897 | int err = base->d_op->d_hash(base, qname); |
5a202bcd | 2898 | if (err < 0) |
3c95f0dc | 2899 | return err; |
5a202bcd | 2900 | } |
fa6fe07d N |
2901 | return 0; |
2902 | } | |
eead1911 | 2903 | |
fa6fe07d | 2904 | static int lookup_one_common(struct mnt_idmap *idmap, |
390e34bc N |
2905 | struct qstr *qname, struct dentry *base) |
2906 | { | |
fa6fe07d | 2907 | int err; |
390e34bc | 2908 | err = lookup_noperm_common(qname, base); |
fa6fe07d N |
2909 | if (err < 0) |
2910 | return err; | |
4609e1f1 | 2911 | return inode_permission(idmap, base->d_inode, MAY_EXEC); |
3c95f0dc AV |
2912 | } |
2913 | ||
0da0b7fd | 2914 | /** |
fa6fe07d N |
2915 | * try_lookup_noperm - filesystem helper to lookup single pathname component |
2916 | * @name: qstr storing pathname component to lookup | |
0da0b7fd | 2917 | * @base: base directory to lookup from |
0da0b7fd DH |
2918 | * |
2919 | * Look up a dentry by name in the dcache, returning NULL if it does not | |
ad5a0351 N |
2920 | * currently exist. The function does not try to create a dentry and if one |
2921 | * is found it doesn't try to revalidate it. | |
0da0b7fd DH |
2922 | * |
2923 | * Note that this routine is purely a helper for filesystem usage and should | |
fa6fe07d | 2924 | * not be called by generic code. It does no permission checking. |
0da0b7fd | 2925 | * |
514b6871 N |
2926 | * No locks need be held - only a counted reference to @base is needed. |
2927 | * | |
0da0b7fd | 2928 | */ |
fa6fe07d | 2929 | struct dentry *try_lookup_noperm(struct qstr *name, struct dentry *base) |
0da0b7fd | 2930 | { |
0da0b7fd DH |
2931 | int err; |
2932 | ||
390e34bc | 2933 | err = lookup_noperm_common(name, base); |
0da0b7fd DH |
2934 | if (err) |
2935 | return ERR_PTR(err); | |
2936 | ||
ad5a0351 | 2937 | return d_lookup(base, name); |
0da0b7fd | 2938 | } |
fa6fe07d | 2939 | EXPORT_SYMBOL(try_lookup_noperm); |
0da0b7fd | 2940 | |
3c95f0dc | 2941 | /** |
fa6fe07d N |
2942 | * lookup_noperm - filesystem helper to lookup single pathname component |
2943 | * @name: qstr storing pathname component to lookup | |
3c95f0dc | 2944 | * @base: base directory to lookup from |
3c95f0dc AV |
2945 | * |
2946 | * Note that this routine is purely a helper for filesystem usage and should | |
fa6fe07d | 2947 | * not be called by generic code. It does no permission checking. |
3c95f0dc AV |
2948 | * |
2949 | * The caller must hold base->i_mutex. | |
2950 | */ | |
fa6fe07d | 2951 | struct dentry *lookup_noperm(struct qstr *name, struct dentry *base) |
3c95f0dc | 2952 | { |
8613a209 | 2953 | struct dentry *dentry; |
3c95f0dc AV |
2954 | int err; |
2955 | ||
2956 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); | |
2957 | ||
390e34bc | 2958 | err = lookup_noperm_common(name, base); |
cda309de MS |
2959 | if (err) |
2960 | return ERR_PTR(err); | |
2961 | ||
390e34bc N |
2962 | dentry = lookup_dcache(name, base, 0); |
2963 | return dentry ? dentry : __lookup_slow(name, base, 0); | |
057f6c01 | 2964 | } |
fa6fe07d | 2965 | EXPORT_SYMBOL(lookup_noperm); |
057f6c01 | 2966 | |
c2fd68b6 | 2967 | /** |
57419096 | 2968 | * lookup_one - lookup single pathname component |
4609e1f1 | 2969 | * @idmap: idmap of the mount the lookup is performed from |
57419096 | 2970 | * @name: qstr holding pathname component to lookup |
c2fd68b6 | 2971 | * @base: base directory to lookup from |
c2fd68b6 | 2972 | * |
57419096 | 2973 | * This can be used for in-kernel filesystem clients such as file servers. |
c2fd68b6 CB |
2974 | * |
2975 | * The caller must hold base->i_mutex. | |
2976 | */ | |
57419096 N |
2977 | struct dentry *lookup_one(struct mnt_idmap *idmap, struct qstr *name, |
2978 | struct dentry *base) | |
c2fd68b6 CB |
2979 | { |
2980 | struct dentry *dentry; | |
c2fd68b6 CB |
2981 | int err; |
2982 | ||
2983 | WARN_ON_ONCE(!inode_is_locked(base->d_inode)); | |
2984 | ||
390e34bc | 2985 | err = lookup_one_common(idmap, name, base); |
c2fd68b6 CB |
2986 | if (err) |
2987 | return ERR_PTR(err); | |
2988 | ||
390e34bc N |
2989 | dentry = lookup_dcache(name, base, 0); |
2990 | return dentry ? dentry : __lookup_slow(name, base, 0); | |
c2fd68b6 CB |
2991 | } |
2992 | EXPORT_SYMBOL(lookup_one); | |
2993 | ||
bbddca8e | 2994 | /** |
57419096 | 2995 | * lookup_one_unlocked - lookup single pathname component |
4609e1f1 | 2996 | * @idmap: idmap of the mount the lookup is performed from |
57419096 | 2997 | * @name: qstr olding pathname component to lookup |
bbddca8e | 2998 | * @base: base directory to lookup from |
bbddca8e | 2999 | * |
57419096 | 3000 | * This can be used for in-kernel filesystem clients such as file servers. |
bbddca8e | 3001 | * |
fa6fe07d | 3002 | * Unlike lookup_one, it should be called without the parent |
57419096 | 3003 | * i_rwsem held, and will take the i_rwsem itself if necessary. |
bbddca8e | 3004 | */ |
390e34bc N |
3005 | struct dentry *lookup_one_unlocked(struct mnt_idmap *idmap, struct qstr *name, |
3006 | struct dentry *base) | |
bbddca8e | 3007 | { |
bbddca8e | 3008 | int err; |
20d00ee8 | 3009 | struct dentry *ret; |
bbddca8e | 3010 | |
390e34bc | 3011 | err = lookup_one_common(idmap, name, base); |
bbddca8e N |
3012 | if (err) |
3013 | return ERR_PTR(err); | |
3014 | ||
390e34bc | 3015 | ret = lookup_dcache(name, base, 0); |
20d00ee8 | 3016 | if (!ret) |
390e34bc | 3017 | ret = lookup_slow(name, base, 0); |
20d00ee8 | 3018 | return ret; |
bbddca8e | 3019 | } |
00675017 CB |
3020 | EXPORT_SYMBOL(lookup_one_unlocked); |
3021 | ||
3022 | /** | |
57419096 | 3023 | * lookup_one_positive_unlocked - lookup single pathname component |
4609e1f1 | 3024 | * @idmap: idmap of the mount the lookup is performed from |
57419096 | 3025 | * @name: qstr holding pathname component to lookup |
00675017 | 3026 | * @base: base directory to lookup from |
00675017 CB |
3027 | * |
3028 | * This helper will yield ERR_PTR(-ENOENT) on negatives. The helper returns | |
3029 | * known positive or ERR_PTR(). This is what most of the users want. | |
3030 | * | |
3031 | * Note that pinned negative with unlocked parent _can_ become positive at any | |
3032 | * time, so callers of lookup_one_unlocked() need to be very careful; pinned | |
3033 | * positives have >d_inode stable, so this one avoids such problems. | |
3034 | * | |
57419096 | 3035 | * This can be used for in-kernel filesystem clients such as file servers. |
00675017 | 3036 | * |
57419096 | 3037 | * The helper should be called without i_rwsem held. |
00675017 | 3038 | */ |
4609e1f1 | 3039 | struct dentry *lookup_one_positive_unlocked(struct mnt_idmap *idmap, |
57419096 N |
3040 | struct qstr *name, |
3041 | struct dentry *base) | |
00675017 | 3042 | { |
57419096 | 3043 | struct dentry *ret = lookup_one_unlocked(idmap, name, base); |
00675017 CB |
3044 | |
3045 | if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { | |
3046 | dput(ret); | |
3047 | ret = ERR_PTR(-ENOENT); | |
3048 | } | |
3049 | return ret; | |
3050 | } | |
3051 | EXPORT_SYMBOL(lookup_one_positive_unlocked); | |
3052 | ||
3053 | /** | |
fa6fe07d | 3054 | * lookup_noperm_unlocked - filesystem helper to lookup single pathname component |
00675017 CB |
3055 | * @name: pathname component to lookup |
3056 | * @base: base directory to lookup from | |
00675017 CB |
3057 | * |
3058 | * Note that this routine is purely a helper for filesystem usage and should | |
fa6fe07d | 3059 | * not be called by generic code. It does no permission checking. |
00675017 | 3060 | * |
ad5a0351 | 3061 | * Unlike lookup_noperm(), it should be called without the parent |
fa6fe07d | 3062 | * i_rwsem held, and will take the i_rwsem itself if necessary. |
ad5a0351 N |
3063 | * |
3064 | * Unlike try_lookup_noperm() it *does* revalidate the dentry if it already | |
3065 | * existed. | |
00675017 | 3066 | */ |
fa6fe07d | 3067 | struct dentry *lookup_noperm_unlocked(struct qstr *name, struct dentry *base) |
00675017 | 3068 | { |
fa6fe07d | 3069 | struct dentry *ret; |
ad5a0351 | 3070 | int err; |
fa6fe07d | 3071 | |
ad5a0351 N |
3072 | err = lookup_noperm_common(name, base); |
3073 | if (err) | |
3074 | return ERR_PTR(err); | |
3075 | ||
3076 | ret = lookup_dcache(name, base, 0); | |
fa6fe07d N |
3077 | if (!ret) |
3078 | ret = lookup_slow(name, base, 0); | |
3079 | return ret; | |
00675017 | 3080 | } |
fa6fe07d | 3081 | EXPORT_SYMBOL(lookup_noperm_unlocked); |
bbddca8e | 3082 | |
6c2d4798 | 3083 | /* |
fa6fe07d | 3084 | * Like lookup_noperm_unlocked(), except that it yields ERR_PTR(-ENOENT) |
6c2d4798 AV |
3085 | * on negatives. Returns known positive or ERR_PTR(); that's what |
3086 | * most of the users want. Note that pinned negative with unlocked parent | |
fa6fe07d | 3087 | * _can_ become positive at any time, so callers of lookup_noperm_unlocked() |
6c2d4798 AV |
3088 | * need to be very careful; pinned positives have ->d_inode stable, so |
3089 | * this one avoids such problems. | |
3090 | */ | |
fa6fe07d N |
3091 | struct dentry *lookup_noperm_positive_unlocked(struct qstr *name, |
3092 | struct dentry *base) | |
6c2d4798 | 3093 | { |
fa6fe07d N |
3094 | struct dentry *ret; |
3095 | ||
3096 | ret = lookup_noperm_unlocked(name, base); | |
3097 | if (!IS_ERR(ret) && d_flags_negative(smp_load_acquire(&ret->d_flags))) { | |
3098 | dput(ret); | |
3099 | ret = ERR_PTR(-ENOENT); | |
3100 | } | |
3101 | return ret; | |
6c2d4798 | 3102 | } |
fa6fe07d | 3103 | EXPORT_SYMBOL(lookup_noperm_positive_unlocked); |
6c2d4798 | 3104 | |
eedf265a EB |
3105 | #ifdef CONFIG_UNIX98_PTYS |
3106 | int path_pts(struct path *path) | |
3107 | { | |
3108 | /* Find something mounted on "pts" in the same directory as | |
3109 | * the input path. | |
3110 | */ | |
a6a7eb76 AV |
3111 | struct dentry *parent = dget_parent(path->dentry); |
3112 | struct dentry *child; | |
19f6028a | 3113 | struct qstr this = QSTR_INIT("pts", 3); |
eedf265a | 3114 | |
a6a7eb76 AV |
3115 | if (unlikely(!path_connected(path->mnt, parent))) { |
3116 | dput(parent); | |
63b27720 | 3117 | return -ENOENT; |
a6a7eb76 | 3118 | } |
63b27720 AV |
3119 | dput(path->dentry); |
3120 | path->dentry = parent; | |
eedf265a | 3121 | child = d_hash_and_lookup(parent, &this); |
0d5a4f8f | 3122 | if (IS_ERR_OR_NULL(child)) |
eedf265a EB |
3123 | return -ENOENT; |
3124 | ||
3125 | path->dentry = child; | |
3126 | dput(parent); | |
e1f19857 | 3127 | follow_down(path, 0); |
eedf265a EB |
3128 | return 0; |
3129 | } | |
3130 | #endif | |
3131 | ||
dff60734 MG |
3132 | int user_path_at(int dfd, const char __user *name, unsigned flags, |
3133 | struct path *path) | |
1da177e4 | 3134 | { |
dff60734 | 3135 | struct filename *filename = getname_flags(name, flags); |
794ebcea SB |
3136 | int ret = filename_lookup(dfd, filename, flags, path, NULL); |
3137 | ||
3138 | putname(filename); | |
3139 | return ret; | |
1da177e4 | 3140 | } |
dff60734 | 3141 | EXPORT_SYMBOL(user_path_at); |
1fa1e7f6 | 3142 | |
9452e93e | 3143 | int __check_sticky(struct mnt_idmap *idmap, struct inode *dir, |
ba73d987 | 3144 | struct inode *inode) |
1da177e4 | 3145 | { |
8e96e3b7 | 3146 | kuid_t fsuid = current_fsuid(); |
da9592ed | 3147 | |
e67fe633 | 3148 | if (vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, inode), fsuid)) |
1da177e4 | 3149 | return 0; |
e67fe633 | 3150 | if (vfsuid_eq_kuid(i_uid_into_vfsuid(idmap, dir), fsuid)) |
1da177e4 | 3151 | return 0; |
9452e93e | 3152 | return !capable_wrt_inode_uidgid(idmap, inode, CAP_FOWNER); |
1da177e4 | 3153 | } |
cbdf35bc | 3154 | EXPORT_SYMBOL(__check_sticky); |
1da177e4 LT |
3155 | |
3156 | /* | |
3157 | * Check whether we can remove a link victim from directory dir, check | |
3158 | * whether the type of victim is right. | |
3159 | * 1. We can't do it if dir is read-only (done in permission()) | |
3160 | * 2. We should have write and exec permissions on dir | |
3161 | * 3. We can't remove anything from append-only dir | |
3162 | * 4. We can't do anything with immutable dir (done in permission()) | |
3163 | * 5. If the sticky bit on dir is set we should either | |
3164 | * a. be owner of dir, or | |
3165 | * b. be owner of victim, or | |
3166 | * c. have CAP_FOWNER capability | |
3167 | * 6. If the victim is append-only or immutable we can't do antyhing with | |
3168 | * links pointing to it. | |
0bd23d09 EB |
3169 | * 7. If the victim has an unknown uid or gid we can't change the inode. |
3170 | * 8. If we were asked to remove a directory and victim isn't one - ENOTDIR. | |
3171 | * 9. If we were asked to remove a non-directory and victim isn't one - EISDIR. | |
3172 | * 10. We can't remove a root or mountpoint. | |
3173 | * 11. We don't allow removal of NFS sillyrenamed files; it's handled by | |
1da177e4 LT |
3174 | * nfs_async_unlink(). |
3175 | */ | |
4609e1f1 | 3176 | static int may_delete(struct mnt_idmap *idmap, struct inode *dir, |
ba73d987 | 3177 | struct dentry *victim, bool isdir) |
1da177e4 | 3178 | { |
63afdfc7 | 3179 | struct inode *inode = d_backing_inode(victim); |
1da177e4 LT |
3180 | int error; |
3181 | ||
b18825a7 | 3182 | if (d_is_negative(victim)) |
1da177e4 | 3183 | return -ENOENT; |
b18825a7 | 3184 | BUG_ON(!inode); |
1da177e4 LT |
3185 | |
3186 | BUG_ON(victim->d_parent->d_inode != dir); | |
593d1ce8 EB |
3187 | |
3188 | /* Inode writeback is not safe when the uid or gid are invalid. */ | |
e67fe633 CB |
3189 | if (!vfsuid_valid(i_uid_into_vfsuid(idmap, inode)) || |
3190 | !vfsgid_valid(i_gid_into_vfsgid(idmap, inode))) | |
593d1ce8 EB |
3191 | return -EOVERFLOW; |
3192 | ||
4fa6b5ec | 3193 | audit_inode_child(dir, victim, AUDIT_TYPE_CHILD_DELETE); |
1da177e4 | 3194 | |
4609e1f1 | 3195 | error = inode_permission(idmap, dir, MAY_WRITE | MAY_EXEC); |
1da177e4 LT |
3196 | if (error) |
3197 | return error; | |
3198 | if (IS_APPEND(dir)) | |
3199 | return -EPERM; | |
b18825a7 | 3200 | |
9452e93e | 3201 | if (check_sticky(idmap, dir, inode) || IS_APPEND(inode) || |
ba73d987 | 3202 | IS_IMMUTABLE(inode) || IS_SWAPFILE(inode) || |
4609e1f1 | 3203 | HAS_UNMAPPED_ID(idmap, inode)) |
1da177e4 LT |
3204 | return -EPERM; |
3205 | if (isdir) { | |
44b1d530 | 3206 | if (!d_is_dir(victim)) |
1da177e4 LT |
3207 | return -ENOTDIR; |
3208 | if (IS_ROOT(victim)) | |
3209 | return -EBUSY; | |
44b1d530 | 3210 | } else if (d_is_dir(victim)) |
1da177e4 LT |
3211 | return -EISDIR; |
3212 | if (IS_DEADDIR(dir)) | |
3213 | return -ENOENT; | |
3214 | if (victim->d_flags & DCACHE_NFSFS_RENAMED) | |
3215 | return -EBUSY; | |
3216 | return 0; | |
3217 | } | |
3218 | ||
3219 | /* Check whether we can create an object with dentry child in directory | |
3220 | * dir. | |
3221 | * 1. We can't do it if child already exists (open has special treatment for | |
3222 | * this case, but since we are inlined it's OK) | |
3223 | * 2. We can't do it if dir is read-only (done in permission()) | |
036d5236 EB |
3224 | * 3. We can't do it if the fs can't represent the fsuid or fsgid. |
3225 | * 4. We should have write and exec permissions on dir | |
3226 | * 5. We can't do it if dir is immutable (done in permission()) | |
1da177e4 | 3227 | */ |
4609e1f1 | 3228 | static inline int may_create(struct mnt_idmap *idmap, |
ba73d987 | 3229 | struct inode *dir, struct dentry *child) |
1da177e4 | 3230 | { |
14e972b4 | 3231 | audit_inode_child(dir, child, AUDIT_TYPE_CHILD_CREATE); |
1da177e4 LT |
3232 | if (child->d_inode) |
3233 | return -EEXIST; | |
3234 | if (IS_DEADDIR(dir)) | |
3235 | return -ENOENT; | |
4609e1f1 | 3236 | if (!fsuidgid_has_mapping(dir->i_sb, idmap)) |
036d5236 | 3237 | return -EOVERFLOW; |
8e538913 | 3238 | |
4609e1f1 | 3239 | return inode_permission(idmap, dir, MAY_WRITE | MAY_EXEC); |
1da177e4 LT |
3240 | } |
3241 | ||
a8b00268 | 3242 | // p1 != p2, both are on the same filesystem, ->s_vfs_rename_mutex is held |
9bc37e04 | 3243 | static struct dentry *lock_two_directories(struct dentry *p1, struct dentry *p2) |
1da177e4 | 3244 | { |
a8b00268 | 3245 | struct dentry *p = p1, *q = p2, *r; |
1da177e4 | 3246 | |
a8b00268 AV |
3247 | while ((r = p->d_parent) != p2 && r != p) |
3248 | p = r; | |
3249 | if (r == p2) { | |
3250 | // p is a child of p2 and an ancestor of p1 or p1 itself | |
5955102c | 3251 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); |
22e111ed | 3252 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT2); |
e2761a11 | 3253 | return p; |
1da177e4 | 3254 | } |
a8b00268 AV |
3255 | // p is the root of connected component that contains p1 |
3256 | // p2 does not occur on the path from p to p1 | |
3257 | while ((r = q->d_parent) != p1 && r != p && r != q) | |
3258 | q = r; | |
3259 | if (r == p1) { | |
3260 | // q is a child of p1 and an ancestor of p2 or p2 itself | |
5955102c | 3261 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); |
a8b00268 AV |
3262 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2); |
3263 | return q; | |
3264 | } else if (likely(r == p)) { | |
3265 | // both p2 and p1 are descendents of p | |
3266 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); | |
3267 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT2); | |
3268 | return NULL; | |
3269 | } else { // no common ancestor at the time we'd been called | |
3270 | mutex_unlock(&p1->d_sb->s_vfs_rename_mutex); | |
3271 | return ERR_PTR(-EXDEV); | |
1da177e4 | 3272 | } |
1da177e4 | 3273 | } |
9bc37e04 AV |
3274 | |
3275 | /* | |
3276 | * p1 and p2 should be directories on the same fs. | |
3277 | */ | |
3278 | struct dentry *lock_rename(struct dentry *p1, struct dentry *p2) | |
3279 | { | |
3280 | if (p1 == p2) { | |
3281 | inode_lock_nested(p1->d_inode, I_MUTEX_PARENT); | |
3282 | return NULL; | |
3283 | } | |
3284 | ||
3285 | mutex_lock(&p1->d_sb->s_vfs_rename_mutex); | |
3286 | return lock_two_directories(p1, p2); | |
3287 | } | |
4d359507 | 3288 | EXPORT_SYMBOL(lock_rename); |
1da177e4 | 3289 | |
9bc37e04 AV |
3290 | /* |
3291 | * c1 and p2 should be on the same fs. | |
3292 | */ | |
3293 | struct dentry *lock_rename_child(struct dentry *c1, struct dentry *p2) | |
3294 | { | |
3295 | if (READ_ONCE(c1->d_parent) == p2) { | |
3296 | /* | |
3297 | * hopefully won't need to touch ->s_vfs_rename_mutex at all. | |
3298 | */ | |
3299 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); | |
3300 | /* | |
3301 | * now that p2 is locked, nobody can move in or out of it, | |
3302 | * so the test below is safe. | |
3303 | */ | |
3304 | if (likely(c1->d_parent == p2)) | |
3305 | return NULL; | |
3306 | ||
3307 | /* | |
3308 | * c1 got moved out of p2 while we'd been taking locks; | |
3309 | * unlock and fall back to slow case. | |
3310 | */ | |
3311 | inode_unlock(p2->d_inode); | |
3312 | } | |
3313 | ||
3314 | mutex_lock(&c1->d_sb->s_vfs_rename_mutex); | |
3315 | /* | |
3316 | * nobody can move out of any directories on this fs. | |
3317 | */ | |
3318 | if (likely(c1->d_parent != p2)) | |
3319 | return lock_two_directories(c1->d_parent, p2); | |
3320 | ||
3321 | /* | |
3322 | * c1 got moved into p2 while we were taking locks; | |
3323 | * we need p2 locked and ->s_vfs_rename_mutex unlocked, | |
3324 | * for consistency with lock_rename(). | |
3325 | */ | |
3326 | inode_lock_nested(p2->d_inode, I_MUTEX_PARENT); | |
3327 | mutex_unlock(&c1->d_sb->s_vfs_rename_mutex); | |
3328 | return NULL; | |
3329 | } | |
3330 | EXPORT_SYMBOL(lock_rename_child); | |
3331 | ||
1da177e4 LT |
3332 | void unlock_rename(struct dentry *p1, struct dentry *p2) |
3333 | { | |
5955102c | 3334 | inode_unlock(p1->d_inode); |
1da177e4 | 3335 | if (p1 != p2) { |
5955102c | 3336 | inode_unlock(p2->d_inode); |
fc64005c | 3337 | mutex_unlock(&p1->d_sb->s_vfs_rename_mutex); |
1da177e4 LT |
3338 | } |
3339 | } | |
4d359507 | 3340 | EXPORT_SYMBOL(unlock_rename); |
1da177e4 | 3341 | |
1639a49c YX |
3342 | /** |
3343 | * vfs_prepare_mode - prepare the mode to be used for a new inode | |
9452e93e | 3344 | * @idmap: idmap of the mount the inode was found from |
1639a49c YX |
3345 | * @dir: parent directory of the new inode |
3346 | * @mode: mode of the new inode | |
3347 | * @mask_perms: allowed permission by the vfs | |
3348 | * @type: type of file to be created | |
3349 | * | |
3350 | * This helper consolidates and enforces vfs restrictions on the @mode of a new | |
3351 | * object to be created. | |
3352 | * | |
3353 | * Umask stripping depends on whether the filesystem supports POSIX ACLs (see | |
3354 | * the kernel documentation for mode_strip_umask()). Moving umask stripping | |
3355 | * after setgid stripping allows the same ordering for both non-POSIX ACL and | |
3356 | * POSIX ACL supporting filesystems. | |
3357 | * | |
3358 | * Note that it's currently valid for @type to be 0 if a directory is created. | |
3359 | * Filesystems raise that flag individually and we need to check whether each | |
3360 | * filesystem can deal with receiving S_IFDIR from the vfs before we enforce a | |
3361 | * non-zero type. | |
3362 | * | |
3363 | * Returns: mode to be passed to the filesystem | |
3364 | */ | |
9452e93e | 3365 | static inline umode_t vfs_prepare_mode(struct mnt_idmap *idmap, |
1639a49c YX |
3366 | const struct inode *dir, umode_t mode, |
3367 | umode_t mask_perms, umode_t type) | |
3368 | { | |
9452e93e | 3369 | mode = mode_strip_sgid(idmap, dir, mode); |
1639a49c YX |
3370 | mode = mode_strip_umask(dir, mode); |
3371 | ||
3372 | /* | |
3373 | * Apply the vfs mandated allowed permission mask and set the type of | |
3374 | * file to be created before we call into the filesystem. | |
3375 | */ | |
3376 | mode &= (mask_perms & ~S_IFMT); | |
3377 | mode |= (type & S_IFMT); | |
3378 | ||
3379 | return mode; | |
3380 | } | |
3381 | ||
6521f891 CB |
3382 | /** |
3383 | * vfs_create - create new file | |
abf08576 | 3384 | * @idmap: idmap of the mount the inode was found from |
b40c8e7a CZ |
3385 | * @dir: inode of the parent directory |
3386 | * @dentry: dentry of the child file | |
3387 | * @mode: mode of the child file | |
6521f891 CB |
3388 | * @want_excl: whether the file must not yet exist |
3389 | * | |
3390 | * Create a new file. | |
3391 | * | |
abf08576 CB |
3392 | * If the inode has been found through an idmapped mount the idmap of |
3393 | * the vfsmount must be passed through @idmap. This function will then take | |
3394 | * care to map the inode according to @idmap before checking permissions. | |
6521f891 | 3395 | * On non-idmapped mounts or if permission checking is to be performed on the |
376870aa | 3396 | * raw inode simply pass @nop_mnt_idmap. |
6521f891 | 3397 | */ |
abf08576 | 3398 | int vfs_create(struct mnt_idmap *idmap, struct inode *dir, |
6521f891 | 3399 | struct dentry *dentry, umode_t mode, bool want_excl) |
1da177e4 | 3400 | { |
abf08576 CB |
3401 | int error; |
3402 | ||
4609e1f1 | 3403 | error = may_create(idmap, dir, dentry); |
1da177e4 LT |
3404 | if (error) |
3405 | return error; | |
3406 | ||
acfa4380 | 3407 | if (!dir->i_op->create) |
1da177e4 | 3408 | return -EACCES; /* shouldn't it be ENOSYS? */ |
1639a49c | 3409 | |
9452e93e | 3410 | mode = vfs_prepare_mode(idmap, dir, mode, S_IALLUGO, S_IFREG); |
1da177e4 LT |
3411 | error = security_inode_create(dir, dentry, mode); |
3412 | if (error) | |
3413 | return error; | |
6c960e68 | 3414 | error = dir->i_op->create(idmap, dir, dentry, mode, want_excl); |
a74574aa | 3415 | if (!error) |
f38aa942 | 3416 | fsnotify_create(dir, dentry); |
1da177e4 LT |
3417 | return error; |
3418 | } | |
4d359507 | 3419 | EXPORT_SYMBOL(vfs_create); |
1da177e4 | 3420 | |
8e6c848e AV |
3421 | int vfs_mkobj(struct dentry *dentry, umode_t mode, |
3422 | int (*f)(struct dentry *, umode_t, void *), | |
3423 | void *arg) | |
3424 | { | |
3425 | struct inode *dir = dentry->d_parent->d_inode; | |
4609e1f1 | 3426 | int error = may_create(&nop_mnt_idmap, dir, dentry); |
8e6c848e AV |
3427 | if (error) |
3428 | return error; | |
3429 | ||
3430 | mode &= S_IALLUGO; | |
3431 | mode |= S_IFREG; | |
3432 | error = security_inode_create(dir, dentry, mode); | |
3433 | if (error) | |
3434 | return error; | |
3435 | error = f(dentry, mode, arg); | |
3436 | if (!error) | |
3437 | fsnotify_create(dir, dentry); | |
3438 | return error; | |
3439 | } | |
3440 | EXPORT_SYMBOL(vfs_mkobj); | |
3441 | ||
a2982cc9 EB |
3442 | bool may_open_dev(const struct path *path) |
3443 | { | |
3444 | return !(path->mnt->mnt_flags & MNT_NODEV) && | |
3445 | !(path->mnt->mnt_sb->s_iflags & SB_I_NODEV); | |
3446 | } | |
3447 | ||
4609e1f1 | 3448 | static int may_open(struct mnt_idmap *idmap, const struct path *path, |
ba73d987 | 3449 | int acc_mode, int flag) |
1da177e4 | 3450 | { |
3fb64190 | 3451 | struct dentry *dentry = path->dentry; |
1da177e4 LT |
3452 | struct inode *inode = dentry->d_inode; |
3453 | int error; | |
3454 | ||
3455 | if (!inode) | |
3456 | return -ENOENT; | |
3457 | ||
c8fe8f30 CH |
3458 | switch (inode->i_mode & S_IFMT) { |
3459 | case S_IFLNK: | |
1da177e4 | 3460 | return -ELOOP; |
c8fe8f30 | 3461 | case S_IFDIR: |
fc4177be | 3462 | if (acc_mode & MAY_WRITE) |
c8fe8f30 | 3463 | return -EISDIR; |
fc4177be KC |
3464 | if (acc_mode & MAY_EXEC) |
3465 | return -EACCES; | |
c8fe8f30 CH |
3466 | break; |
3467 | case S_IFBLK: | |
3468 | case S_IFCHR: | |
a2982cc9 | 3469 | if (!may_open_dev(path)) |
1da177e4 | 3470 | return -EACCES; |
633fb6ac | 3471 | fallthrough; |
c8fe8f30 CH |
3472 | case S_IFIFO: |
3473 | case S_IFSOCK: | |
633fb6ac KC |
3474 | if (acc_mode & MAY_EXEC) |
3475 | return -EACCES; | |
1da177e4 | 3476 | flag &= ~O_TRUNC; |
c8fe8f30 | 3477 | break; |
0fd338b2 KC |
3478 | case S_IFREG: |
3479 | if ((acc_mode & MAY_EXEC) && path_noexec(path)) | |
3480 | return -EACCES; | |
3481 | break; | |
af153bb6 | 3482 | default: |
1e7ab6f6 | 3483 | VFS_BUG_ON_INODE(!IS_ANON_FILE(inode), inode); |
4a3fd211 | 3484 | } |
b41572e9 | 3485 | |
4609e1f1 | 3486 | error = inode_permission(idmap, inode, MAY_OPEN | acc_mode); |
b41572e9 DH |
3487 | if (error) |
3488 | return error; | |
6146f0d5 | 3489 | |
1da177e4 LT |
3490 | /* |
3491 | * An append-only file must be opened in append mode for writing. | |
3492 | */ | |
3493 | if (IS_APPEND(inode)) { | |
8737c930 | 3494 | if ((flag & O_ACCMODE) != O_RDONLY && !(flag & O_APPEND)) |
7715b521 | 3495 | return -EPERM; |
1da177e4 | 3496 | if (flag & O_TRUNC) |
7715b521 | 3497 | return -EPERM; |
1da177e4 LT |
3498 | } |
3499 | ||
3500 | /* O_NOATIME can only be set by the owner or superuser */ | |
01beba79 | 3501 | if (flag & O_NOATIME && !inode_owner_or_capable(idmap, inode)) |
7715b521 | 3502 | return -EPERM; |
1da177e4 | 3503 | |
f3c7691e | 3504 | return 0; |
7715b521 | 3505 | } |
1da177e4 | 3506 | |
abf08576 | 3507 | static int handle_truncate(struct mnt_idmap *idmap, struct file *filp) |
7715b521 | 3508 | { |
f0bb5aaf | 3509 | const struct path *path = &filp->f_path; |
7715b521 AV |
3510 | struct inode *inode = path->dentry->d_inode; |
3511 | int error = get_write_access(inode); | |
3512 | if (error) | |
3513 | return error; | |
482e0007 | 3514 | |
3350607d | 3515 | error = security_file_truncate(filp); |
7715b521 | 3516 | if (!error) { |
abf08576 | 3517 | error = do_truncate(idmap, path->dentry, 0, |
7715b521 | 3518 | ATTR_MTIME|ATTR_CTIME|ATTR_OPEN, |
e1181ee6 | 3519 | filp); |
7715b521 AV |
3520 | } |
3521 | put_write_access(inode); | |
acd0c935 | 3522 | return error; |
1da177e4 LT |
3523 | } |
3524 | ||
d57999e1 DH |
3525 | static inline int open_to_namei_flags(int flag) |
3526 | { | |
8a5e929d AV |
3527 | if ((flag & O_ACCMODE) == 3) |
3528 | flag--; | |
d57999e1 DH |
3529 | return flag; |
3530 | } | |
3531 | ||
4609e1f1 | 3532 | static int may_o_create(struct mnt_idmap *idmap, |
ba73d987 CB |
3533 | const struct path *dir, struct dentry *dentry, |
3534 | umode_t mode) | |
d18e9008 MS |
3535 | { |
3536 | int error = security_path_mknod(dir, dentry, mode, 0); | |
3537 | if (error) | |
3538 | return error; | |
3539 | ||
4609e1f1 | 3540 | if (!fsuidgid_has_mapping(dir->dentry->d_sb, idmap)) |
1328c727 SF |
3541 | return -EOVERFLOW; |
3542 | ||
4609e1f1 | 3543 | error = inode_permission(idmap, dir->dentry->d_inode, |
47291baa | 3544 | MAY_WRITE | MAY_EXEC); |
d18e9008 MS |
3545 | if (error) |
3546 | return error; | |
3547 | ||
3548 | return security_inode_create(dir->dentry->d_inode, dentry, mode); | |
3549 | } | |
3550 | ||
1acf0af9 DH |
3551 | /* |
3552 | * Attempt to atomically look up, create and open a file from a negative | |
3553 | * dentry. | |
3554 | * | |
3555 | * Returns 0 if successful. The file will have been created and attached to | |
3556 | * @file by the filesystem calling finish_open(). | |
3557 | * | |
00a07c15 AV |
3558 | * If the file was looked up only or didn't need creating, FMODE_OPENED won't |
3559 | * be set. The caller will need to perform the open themselves. @path will | |
3560 | * have been updated to point to the new dentry. This may be negative. | |
1acf0af9 DH |
3561 | * |
3562 | * Returns an error code otherwise. | |
3563 | */ | |
239eb983 AV |
3564 | static struct dentry *atomic_open(struct nameidata *nd, struct dentry *dentry, |
3565 | struct file *file, | |
239eb983 | 3566 | int open_flag, umode_t mode) |
d18e9008 | 3567 | { |
384f26e2 | 3568 | struct dentry *const DENTRY_NOT_SET = (void *) -1UL; |
d18e9008 | 3569 | struct inode *dir = nd->path.dentry->d_inode; |
d18e9008 | 3570 | int error; |
d18e9008 | 3571 | |
d18e9008 MS |
3572 | if (nd->flags & LOOKUP_DIRECTORY) |
3573 | open_flag |= O_DIRECTORY; | |
3574 | ||
30d90494 AV |
3575 | file->f_path.dentry = DENTRY_NOT_SET; |
3576 | file->f_path.mnt = nd->path.mnt; | |
0fb1ea09 | 3577 | error = dir->i_op->atomic_open(dir, dentry, file, |
44907d79 | 3578 | open_to_namei_flags(open_flag), mode); |
6fbd0714 | 3579 | d_lookup_done(dentry); |
384f26e2 | 3580 | if (!error) { |
64e1ac4d | 3581 | if (file->f_mode & FMODE_OPENED) { |
6fb968cd AV |
3582 | if (unlikely(dentry != file->f_path.dentry)) { |
3583 | dput(dentry); | |
3584 | dentry = dget(file->f_path.dentry); | |
3585 | } | |
64e1ac4d | 3586 | } else if (WARN_ON(file->f_path.dentry == DENTRY_NOT_SET)) { |
2675a4eb | 3587 | error = -EIO; |
03da633a | 3588 | } else { |
384f26e2 AV |
3589 | if (file->f_path.dentry) { |
3590 | dput(dentry); | |
3591 | dentry = file->f_path.dentry; | |
03da633a | 3592 | } |
239eb983 | 3593 | if (unlikely(d_is_negative(dentry))) |
a01e718f | 3594 | error = -ENOENT; |
62b2ce96 | 3595 | } |
d18e9008 | 3596 | } |
239eb983 AV |
3597 | if (error) { |
3598 | dput(dentry); | |
3599 | dentry = ERR_PTR(error); | |
3600 | } | |
3601 | return dentry; | |
d18e9008 MS |
3602 | } |
3603 | ||
d58ffd35 | 3604 | /* |
1acf0af9 | 3605 | * Look up and maybe create and open the last component. |
d58ffd35 | 3606 | * |
00a07c15 | 3607 | * Must be called with parent locked (exclusive in O_CREAT case). |
1acf0af9 | 3608 | * |
00a07c15 AV |
3609 | * Returns 0 on success, that is, if |
3610 | * the file was successfully atomically created (if necessary) and opened, or | |
3611 | * the file was not completely opened at this time, though lookups and | |
3612 | * creations were performed. | |
3613 | * These case are distinguished by presence of FMODE_OPENED on file->f_mode. | |
3614 | * In the latter case dentry returned in @path might be negative if O_CREAT | |
3615 | * hadn't been specified. | |
1acf0af9 | 3616 | * |
00a07c15 | 3617 | * An error code is returned on failure. |
d58ffd35 | 3618 | */ |
da5ebf5a AV |
3619 | static struct dentry *lookup_open(struct nameidata *nd, struct file *file, |
3620 | const struct open_flags *op, | |
3621 | bool got_write) | |
d58ffd35 | 3622 | { |
6c960e68 | 3623 | struct mnt_idmap *idmap; |
d58ffd35 | 3624 | struct dentry *dir = nd->path.dentry; |
54ef4872 | 3625 | struct inode *dir_inode = dir->d_inode; |
1643b43f | 3626 | int open_flag = op->open_flag; |
d58ffd35 | 3627 | struct dentry *dentry; |
1643b43f | 3628 | int error, create_error = 0; |
1643b43f | 3629 | umode_t mode = op->mode; |
6fbd0714 | 3630 | DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wq); |
d58ffd35 | 3631 | |
ce8644fc | 3632 | if (unlikely(IS_DEADDIR(dir_inode))) |
da5ebf5a | 3633 | return ERR_PTR(-ENOENT); |
d58ffd35 | 3634 | |
73a09dd9 | 3635 | file->f_mode &= ~FMODE_CREATED; |
6fbd0714 AV |
3636 | dentry = d_lookup(dir, &nd->last); |
3637 | for (;;) { | |
3638 | if (!dentry) { | |
3639 | dentry = d_alloc_parallel(dir, &nd->last, &wq); | |
3640 | if (IS_ERR(dentry)) | |
da5ebf5a | 3641 | return dentry; |
6fbd0714 AV |
3642 | } |
3643 | if (d_in_lookup(dentry)) | |
3644 | break; | |
d58ffd35 | 3645 | |
5be1fa8a | 3646 | error = d_revalidate(dir_inode, &nd->last, dentry, nd->flags); |
6fbd0714 AV |
3647 | if (likely(error > 0)) |
3648 | break; | |
3649 | if (error) | |
3650 | goto out_dput; | |
3651 | d_invalidate(dentry); | |
3652 | dput(dentry); | |
3653 | dentry = NULL; | |
3654 | } | |
3655 | if (dentry->d_inode) { | |
6c51e513 | 3656 | /* Cached positive dentry: will open in f_op->open */ |
da5ebf5a | 3657 | return dentry; |
6c51e513 | 3658 | } |
d18e9008 | 3659 | |
c65d41c5 CB |
3660 | if (open_flag & O_CREAT) |
3661 | audit_inode(nd->name, dir, AUDIT_INODE_PARENT); | |
3662 | ||
1643b43f AV |
3663 | /* |
3664 | * Checking write permission is tricky, bacuse we don't know if we are | |
3665 | * going to actually need it: O_CREAT opens should work as long as the | |
3666 | * file exists. But checking existence breaks atomicity. The trick is | |
3667 | * to check access and if not granted clear O_CREAT from the flags. | |
3668 | * | |
3669 | * Another problem is returing the "right" error value (e.g. for an | |
3670 | * O_EXCL open we want to return EEXIST not EROFS). | |
3671 | */ | |
99a4a90c AV |
3672 | if (unlikely(!got_write)) |
3673 | open_flag &= ~O_TRUNC; | |
6c960e68 | 3674 | idmap = mnt_idmap(nd->path.mnt); |
1643b43f | 3675 | if (open_flag & O_CREAT) { |
99a4a90c AV |
3676 | if (open_flag & O_EXCL) |
3677 | open_flag &= ~O_TRUNC; | |
9452e93e | 3678 | mode = vfs_prepare_mode(idmap, dir->d_inode, mode, mode, mode); |
99a4a90c | 3679 | if (likely(got_write)) |
4609e1f1 | 3680 | create_error = may_o_create(idmap, &nd->path, |
ba73d987 | 3681 | dentry, mode); |
99a4a90c AV |
3682 | else |
3683 | create_error = -EROFS; | |
d18e9008 | 3684 | } |
99a4a90c AV |
3685 | if (create_error) |
3686 | open_flag &= ~O_CREAT; | |
6ac08709 | 3687 | if (dir_inode->i_op->atomic_open) { |
d489cf9a | 3688 | dentry = atomic_open(nd, dentry, file, open_flag, mode); |
da5ebf5a AV |
3689 | if (unlikely(create_error) && dentry == ERR_PTR(-ENOENT)) |
3690 | dentry = ERR_PTR(create_error); | |
3691 | return dentry; | |
d18e9008 | 3692 | } |
54ef4872 | 3693 | |
6fbd0714 | 3694 | if (d_in_lookup(dentry)) { |
12fa5e24 AV |
3695 | struct dentry *res = dir_inode->i_op->lookup(dir_inode, dentry, |
3696 | nd->flags); | |
6fbd0714 | 3697 | d_lookup_done(dentry); |
12fa5e24 AV |
3698 | if (unlikely(res)) { |
3699 | if (IS_ERR(res)) { | |
3700 | error = PTR_ERR(res); | |
3701 | goto out_dput; | |
3702 | } | |
3703 | dput(dentry); | |
3704 | dentry = res; | |
3705 | } | |
54ef4872 MS |
3706 | } |
3707 | ||
d58ffd35 | 3708 | /* Negative dentry, just create the file */ |
1643b43f | 3709 | if (!dentry->d_inode && (open_flag & O_CREAT)) { |
73a09dd9 | 3710 | file->f_mode |= FMODE_CREATED; |
ce8644fc | 3711 | audit_inode_child(dir_inode, dentry, AUDIT_TYPE_CHILD_CREATE); |
ce8644fc AV |
3712 | if (!dir_inode->i_op->create) { |
3713 | error = -EACCES; | |
d58ffd35 | 3714 | goto out_dput; |
ce8644fc | 3715 | } |
549c7297 | 3716 | |
6c960e68 | 3717 | error = dir_inode->i_op->create(idmap, dir_inode, dentry, |
549c7297 | 3718 | mode, open_flag & O_EXCL); |
d58ffd35 MS |
3719 | if (error) |
3720 | goto out_dput; | |
3721 | } | |
1643b43f AV |
3722 | if (unlikely(create_error) && !dentry->d_inode) { |
3723 | error = create_error; | |
3724 | goto out_dput; | |
d58ffd35 | 3725 | } |
da5ebf5a | 3726 | return dentry; |
d58ffd35 MS |
3727 | |
3728 | out_dput: | |
3729 | dput(dentry); | |
da5ebf5a | 3730 | return ERR_PTR(error); |
d58ffd35 MS |
3731 | } |
3732 | ||
e747e151 JL |
3733 | static inline bool trailing_slashes(struct nameidata *nd) |
3734 | { | |
3735 | return (bool)nd->last.name[nd->last.len]; | |
3736 | } | |
3737 | ||
3738 | static struct dentry *lookup_fast_for_open(struct nameidata *nd, int open_flag) | |
3739 | { | |
3740 | struct dentry *dentry; | |
3741 | ||
3742 | if (open_flag & O_CREAT) { | |
4770d96a CB |
3743 | if (trailing_slashes(nd)) |
3744 | return ERR_PTR(-EISDIR); | |
3745 | ||
e747e151 JL |
3746 | /* Don't bother on an O_EXCL create */ |
3747 | if (open_flag & O_EXCL) | |
3748 | return NULL; | |
e747e151 JL |
3749 | } |
3750 | ||
3751 | if (trailing_slashes(nd)) | |
3752 | nd->flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY; | |
3753 | ||
3754 | dentry = lookup_fast(nd); | |
3755 | if (IS_ERR_OR_NULL(dentry)) | |
3756 | return dentry; | |
3757 | ||
3758 | if (open_flag & O_CREAT) { | |
3759 | /* Discard negative dentries. Need inode_lock to do the create */ | |
3760 | if (!dentry->d_inode) { | |
3761 | if (!(nd->flags & LOOKUP_RCU)) | |
3762 | dput(dentry); | |
3763 | dentry = NULL; | |
3764 | } | |
3765 | } | |
3766 | return dentry; | |
3767 | } | |
3768 | ||
c981a482 | 3769 | static const char *open_last_lookups(struct nameidata *nd, |
3ec2eef1 | 3770 | struct file *file, const struct open_flags *op) |
fb1cc555 | 3771 | { |
a1e28038 | 3772 | struct dentry *dir = nd->path.dentry; |
ca344a89 | 3773 | int open_flag = op->open_flag; |
64894cf8 | 3774 | bool got_write = false; |
da5ebf5a | 3775 | struct dentry *dentry; |
b0417d2c | 3776 | const char *res; |
1f36f774 | 3777 | |
c3e380b0 AV |
3778 | nd->flags |= op->intent; |
3779 | ||
bc77daa7 | 3780 | if (nd->last_type != LAST_NORM) { |
56676ec3 AV |
3781 | if (nd->depth) |
3782 | put_link(nd); | |
ff326a32 | 3783 | return handle_dots(nd, nd->last_type); |
1f36f774 | 3784 | } |
67ee3ad2 | 3785 | |
e747e151 JL |
3786 | /* We _can_ be in RCU mode here */ |
3787 | dentry = lookup_fast_for_open(nd, open_flag); | |
3788 | if (IS_ERR(dentry)) | |
3789 | return ERR_CAST(dentry); | |
3790 | ||
0f93bb54 CB |
3791 | if (likely(dentry)) |
3792 | goto finish_lookup; | |
71574865 | 3793 | |
0f93bb54 | 3794 | if (!(open_flag & O_CREAT)) { |
c04d905f BS |
3795 | if (WARN_ON_ONCE(nd->flags & LOOKUP_RCU)) |
3796 | return ERR_PTR(-ECHILD); | |
b6183df7 | 3797 | } else { |
72287417 | 3798 | if (nd->flags & LOOKUP_RCU) { |
0f93bb54 | 3799 | if (!try_to_unlazy(nd)) |
e36cffed | 3800 | return ERR_PTR(-ECHILD); |
72287417 | 3801 | } |
b6183df7 | 3802 | } |
a2c36b45 | 3803 | |
9cf843e3 | 3804 | if (open_flag & (O_CREAT | O_TRUNC | O_WRONLY | O_RDWR)) { |
e36cffed | 3805 | got_write = !mnt_want_write(nd->path.mnt); |
64894cf8 AV |
3806 | /* |
3807 | * do _not_ fail yet - we might not need that or fail with | |
3808 | * a different error; let lookup_open() decide; we'll be | |
3809 | * dropping this one anyway. | |
3810 | */ | |
3811 | } | |
9cf843e3 AV |
3812 | if (open_flag & O_CREAT) |
3813 | inode_lock(dir->d_inode); | |
3814 | else | |
3815 | inode_lock_shared(dir->d_inode); | |
da5ebf5a | 3816 | dentry = lookup_open(nd, file, op, got_write); |
7d1cf5e6 N |
3817 | if (!IS_ERR(dentry)) { |
3818 | if (file->f_mode & FMODE_CREATED) | |
3819 | fsnotify_create(dir->d_inode, dentry); | |
3820 | if (file->f_mode & FMODE_OPENED) | |
3821 | fsnotify_open(file); | |
3822 | } | |
9cf843e3 AV |
3823 | if (open_flag & O_CREAT) |
3824 | inode_unlock(dir->d_inode); | |
3825 | else | |
3826 | inode_unlock_shared(dir->d_inode); | |
a1e28038 | 3827 | |
c981a482 | 3828 | if (got_write) |
59e96e65 | 3829 | mnt_drop_write(nd->path.mnt); |
d18e9008 | 3830 | |
59e96e65 AV |
3831 | if (IS_ERR(dentry)) |
3832 | return ERR_CAST(dentry); | |
3833 | ||
973d4b73 | 3834 | if (file->f_mode & (FMODE_OPENED | FMODE_CREATED)) { |
e73cabff AV |
3835 | dput(nd->path.dentry); |
3836 | nd->path.dentry = dentry; | |
c981a482 | 3837 | return NULL; |
fb1cc555 AV |
3838 | } |
3839 | ||
20e34357 | 3840 | finish_lookup: |
56676ec3 AV |
3841 | if (nd->depth) |
3842 | put_link(nd); | |
a4f5b521 | 3843 | res = step_into(nd, WALK_TRAILING, dentry); |
ff326a32 | 3844 | if (unlikely(res)) |
b0417d2c | 3845 | nd->flags &= ~(LOOKUP_OPEN|LOOKUP_CREATE|LOOKUP_EXCL); |
ff326a32 | 3846 | return res; |
c981a482 AV |
3847 | } |
3848 | ||
3849 | /* | |
3850 | * Handle the last step of open() | |
3851 | */ | |
c5971b8c | 3852 | static int do_open(struct nameidata *nd, |
c981a482 AV |
3853 | struct file *file, const struct open_flags *op) |
3854 | { | |
abf08576 | 3855 | struct mnt_idmap *idmap; |
c981a482 AV |
3856 | int open_flag = op->open_flag; |
3857 | bool do_truncate; | |
3858 | int acc_mode; | |
c981a482 AV |
3859 | int error; |
3860 | ||
ff326a32 AV |
3861 | if (!(file->f_mode & (FMODE_OPENED | FMODE_CREATED))) { |
3862 | error = complete_walk(nd); | |
3863 | if (error) | |
3864 | return error; | |
3865 | } | |
973d4b73 AV |
3866 | if (!(file->f_mode & FMODE_CREATED)) |
3867 | audit_inode(nd->name, nd->path.dentry, 0); | |
abf08576 | 3868 | idmap = mnt_idmap(nd->path.mnt); |
30aba665 | 3869 | if (open_flag & O_CREAT) { |
b94e0b32 AV |
3870 | if ((open_flag & O_EXCL) && !(file->f_mode & FMODE_CREATED)) |
3871 | return -EEXIST; | |
30aba665 | 3872 | if (d_is_dir(nd->path.dentry)) |
c5971b8c | 3873 | return -EISDIR; |
e67fe633 | 3874 | error = may_create_in_sticky(idmap, nd, |
30aba665 SM |
3875 | d_backing_inode(nd->path.dentry)); |
3876 | if (unlikely(error)) | |
c5971b8c | 3877 | return error; |
30aba665 | 3878 | } |
44b1d530 | 3879 | if ((nd->flags & LOOKUP_DIRECTORY) && !d_can_lookup(nd->path.dentry)) |
c5971b8c | 3880 | return -ENOTDIR; |
6c0d46c4 | 3881 | |
8795e7d4 AV |
3882 | do_truncate = false; |
3883 | acc_mode = op->acc_mode; | |
5a2d3edd AV |
3884 | if (file->f_mode & FMODE_CREATED) { |
3885 | /* Don't check for write permission, don't truncate */ | |
3886 | open_flag &= ~O_TRUNC; | |
5a2d3edd | 3887 | acc_mode = 0; |
8795e7d4 | 3888 | } else if (d_is_reg(nd->path.dentry) && open_flag & O_TRUNC) { |
0f9d1a10 AV |
3889 | error = mnt_want_write(nd->path.mnt); |
3890 | if (error) | |
c5971b8c | 3891 | return error; |
8795e7d4 | 3892 | do_truncate = true; |
0f9d1a10 | 3893 | } |
4609e1f1 | 3894 | error = may_open(idmap, &nd->path, acc_mode, open_flag); |
8795e7d4 | 3895 | if (!error && !(file->f_mode & FMODE_OPENED)) |
3ad5615a | 3896 | error = vfs_open(&nd->path, file); |
8795e7d4 | 3897 | if (!error) |
8f46ff57 | 3898 | error = security_file_post_open(file, op->acc_mode); |
8795e7d4 | 3899 | if (!error && do_truncate) |
abf08576 | 3900 | error = handle_truncate(idmap, file); |
c80567c8 AV |
3901 | if (unlikely(error > 0)) { |
3902 | WARN_ON(1); | |
3903 | error = -EINVAL; | |
3904 | } | |
8795e7d4 | 3905 | if (do_truncate) |
0f9d1a10 | 3906 | mnt_drop_write(nd->path.mnt); |
c5971b8c | 3907 | return error; |
fb1cc555 AV |
3908 | } |
3909 | ||
6521f891 CB |
3910 | /** |
3911 | * vfs_tmpfile - create tmpfile | |
abf08576 | 3912 | * @idmap: idmap of the mount the inode was found from |
73bb5a90 RS |
3913 | * @parentpath: pointer to the path of the base directory |
3914 | * @file: file descriptor of the new tmpfile | |
6521f891 | 3915 | * @mode: mode of the new tmpfile |
6521f891 CB |
3916 | * |
3917 | * Create a temporary file. | |
3918 | * | |
abf08576 CB |
3919 | * If the inode has been found through an idmapped mount the idmap of |
3920 | * the vfsmount must be passed through @idmap. This function will then take | |
3921 | * care to map the inode according to @idmap before checking permissions. | |
6521f891 | 3922 | * On non-idmapped mounts or if permission checking is to be performed on the |
376870aa | 3923 | * raw inode simply pass @nop_mnt_idmap. |
6521f891 | 3924 | */ |
9a87907d MS |
3925 | int vfs_tmpfile(struct mnt_idmap *idmap, |
3926 | const struct path *parentpath, | |
3927 | struct file *file, umode_t mode) | |
af7bd4dc | 3928 | { |
9751b338 MS |
3929 | struct dentry *child; |
3930 | struct inode *dir = d_inode(parentpath->dentry); | |
af7bd4dc AG |
3931 | struct inode *inode; |
3932 | int error; | |
406c706c | 3933 | int open_flag = file->f_flags; |
af7bd4dc AG |
3934 | |
3935 | /* we want directory to be writable */ | |
4609e1f1 | 3936 | error = inode_permission(idmap, dir, MAY_WRITE | MAY_EXEC); |
af7bd4dc | 3937 | if (error) |
9751b338 | 3938 | return error; |
af7bd4dc | 3939 | if (!dir->i_op->tmpfile) |
9751b338 MS |
3940 | return -EOPNOTSUPP; |
3941 | child = d_alloc(parentpath->dentry, &slash_name); | |
af7bd4dc | 3942 | if (unlikely(!child)) |
9751b338 MS |
3943 | return -ENOMEM; |
3944 | file->f_path.mnt = parentpath->mnt; | |
3945 | file->f_path.dentry = child; | |
9452e93e | 3946 | mode = vfs_prepare_mode(idmap, dir, mode, mode, mode); |
011e2b71 | 3947 | error = dir->i_op->tmpfile(idmap, dir, file, mode); |
9751b338 | 3948 | dput(child); |
7d1cf5e6 N |
3949 | if (file->f_mode & FMODE_OPENED) |
3950 | fsnotify_open(file); | |
af7bd4dc | 3951 | if (error) |
9751b338 MS |
3952 | return error; |
3953 | /* Don't check for other permissions, the inode was just created */ | |
4609e1f1 | 3954 | error = may_open(idmap, &file->f_path, 0, file->f_flags); |
af7bd4dc | 3955 | if (error) |
9751b338 MS |
3956 | return error; |
3957 | inode = file_inode(file); | |
406c706c | 3958 | if (!(open_flag & O_EXCL)) { |
af7bd4dc AG |
3959 | spin_lock(&inode->i_lock); |
3960 | inode->i_state |= I_LINKABLE; | |
3961 | spin_unlock(&inode->i_lock); | |
3962 | } | |
a7811e34 | 3963 | security_inode_post_create_tmpfile(idmap, inode); |
9751b338 | 3964 | return 0; |
af7bd4dc | 3965 | } |
af7bd4dc | 3966 | |
22873dea | 3967 | /** |
d56e0ddb | 3968 | * kernel_tmpfile_open - open a tmpfile for kernel internal use |
abf08576 | 3969 | * @idmap: idmap of the mount the inode was found from |
22873dea MS |
3970 | * @parentpath: path of the base directory |
3971 | * @mode: mode of the new tmpfile | |
3972 | * @open_flag: flags | |
3973 | * @cred: credentials for open | |
3974 | * | |
3975 | * Create and open a temporary file. The file is not accounted in nr_files, | |
3976 | * hence this is only for kernel internal use, and must not be installed into | |
3977 | * file tables or such. | |
3978 | */ | |
d56e0ddb AG |
3979 | struct file *kernel_tmpfile_open(struct mnt_idmap *idmap, |
3980 | const struct path *parentpath, | |
3981 | umode_t mode, int open_flag, | |
3982 | const struct cred *cred) | |
22873dea MS |
3983 | { |
3984 | struct file *file; | |
3985 | int error; | |
22873dea | 3986 | |
9751b338 | 3987 | file = alloc_empty_file_noaccount(open_flag, cred); |
d56e0ddb AG |
3988 | if (IS_ERR(file)) |
3989 | return file; | |
3990 | ||
3991 | error = vfs_tmpfile(idmap, parentpath, file, mode); | |
3992 | if (error) { | |
3993 | fput(file); | |
3994 | file = ERR_PTR(error); | |
9751b338 | 3995 | } |
22873dea | 3996 | return file; |
af7bd4dc | 3997 | } |
d56e0ddb | 3998 | EXPORT_SYMBOL(kernel_tmpfile_open); |
af7bd4dc | 3999 | |
c8a53ee5 | 4000 | static int do_tmpfile(struct nameidata *nd, unsigned flags, |
60545d0d | 4001 | const struct open_flags *op, |
3ec2eef1 | 4002 | struct file *file) |
60545d0d | 4003 | { |
625b6d10 | 4004 | struct path path; |
c8a53ee5 | 4005 | int error = path_lookupat(nd, flags | LOOKUP_DIRECTORY, &path); |
9751b338 | 4006 | |
60545d0d AV |
4007 | if (unlikely(error)) |
4008 | return error; | |
625b6d10 | 4009 | error = mnt_want_write(path.mnt); |
60545d0d AV |
4010 | if (unlikely(error)) |
4011 | goto out; | |
abf08576 | 4012 | error = vfs_tmpfile(mnt_idmap(path.mnt), &path, file, op->mode); |
9751b338 | 4013 | if (error) |
60545d0d | 4014 | goto out2; |
9751b338 | 4015 | audit_inode(nd->name, file->f_path.dentry, 0); |
60545d0d | 4016 | out2: |
625b6d10 | 4017 | mnt_drop_write(path.mnt); |
60545d0d | 4018 | out: |
625b6d10 | 4019 | path_put(&path); |
60545d0d AV |
4020 | return error; |
4021 | } | |
4022 | ||
6ac08709 AV |
4023 | static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file) |
4024 | { | |
4025 | struct path path; | |
4026 | int error = path_lookupat(nd, flags, &path); | |
4027 | if (!error) { | |
4028 | audit_inode(nd->name, path.dentry, 0); | |
ae2bb293 | 4029 | error = vfs_open(&path, file); |
6ac08709 AV |
4030 | path_put(&path); |
4031 | } | |
4032 | return error; | |
4033 | } | |
4034 | ||
c8a53ee5 AV |
4035 | static struct file *path_openat(struct nameidata *nd, |
4036 | const struct open_flags *op, unsigned flags) | |
1da177e4 | 4037 | { |
30d90494 | 4038 | struct file *file; |
13aab428 | 4039 | int error; |
31e6b01f | 4040 | |
ea73ea72 | 4041 | file = alloc_empty_file(op->open_flag, current_cred()); |
1afc99be AV |
4042 | if (IS_ERR(file)) |
4043 | return file; | |
31e6b01f | 4044 | |
bb458c64 | 4045 | if (unlikely(file->f_flags & __O_TMPFILE)) { |
3ec2eef1 | 4046 | error = do_tmpfile(nd, flags, op, file); |
5f336e72 | 4047 | } else if (unlikely(file->f_flags & O_PATH)) { |
6ac08709 | 4048 | error = do_o_path(nd, flags, file); |
5f336e72 AV |
4049 | } else { |
4050 | const char *s = path_init(nd, flags); | |
4051 | while (!(error = link_path_walk(s, nd)) && | |
c5971b8c | 4052 | (s = open_last_lookups(nd, file, op)) != NULL) |
1ccac622 | 4053 | ; |
c5971b8c AV |
4054 | if (!error) |
4055 | error = do_open(nd, file, op); | |
5f336e72 | 4056 | terminate_walk(nd); |
806b681c | 4057 | } |
7c1c01ec | 4058 | if (likely(!error)) { |
aad888f8 | 4059 | if (likely(file->f_mode & FMODE_OPENED)) |
7c1c01ec AV |
4060 | return file; |
4061 | WARN_ON(1); | |
4062 | error = -EINVAL; | |
16b1c1cd | 4063 | } |
606623de | 4064 | fput_close(file); |
7c1c01ec AV |
4065 | if (error == -EOPENSTALE) { |
4066 | if (flags & LOOKUP_RCU) | |
4067 | error = -ECHILD; | |
4068 | else | |
4069 | error = -ESTALE; | |
2675a4eb | 4070 | } |
7c1c01ec | 4071 | return ERR_PTR(error); |
1da177e4 LT |
4072 | } |
4073 | ||
669abf4e | 4074 | struct file *do_filp_open(int dfd, struct filename *pathname, |
f9652e10 | 4075 | const struct open_flags *op) |
13aab428 | 4076 | { |
9883d185 | 4077 | struct nameidata nd; |
f9652e10 | 4078 | int flags = op->lookup_flags; |
13aab428 AV |
4079 | struct file *filp; |
4080 | ||
06422964 | 4081 | set_nameidata(&nd, dfd, pathname, NULL); |
c8a53ee5 | 4082 | filp = path_openat(&nd, op, flags | LOOKUP_RCU); |
13aab428 | 4083 | if (unlikely(filp == ERR_PTR(-ECHILD))) |
c8a53ee5 | 4084 | filp = path_openat(&nd, op, flags); |
13aab428 | 4085 | if (unlikely(filp == ERR_PTR(-ESTALE))) |
c8a53ee5 | 4086 | filp = path_openat(&nd, op, flags | LOOKUP_REVAL); |
9883d185 | 4087 | restore_nameidata(); |
13aab428 AV |
4088 | return filp; |
4089 | } | |
4090 | ||
ffb37ca3 | 4091 | struct file *do_file_open_root(const struct path *root, |
f9652e10 | 4092 | const char *name, const struct open_flags *op) |
73d049a4 | 4093 | { |
9883d185 | 4094 | struct nameidata nd; |
73d049a4 | 4095 | struct file *file; |
51689104 | 4096 | struct filename *filename; |
bcba1e7d | 4097 | int flags = op->lookup_flags; |
73d049a4 | 4098 | |
ffb37ca3 | 4099 | if (d_is_symlink(root->dentry) && op->intent & LOOKUP_OPEN) |
73d049a4 AV |
4100 | return ERR_PTR(-ELOOP); |
4101 | ||
51689104 | 4102 | filename = getname_kernel(name); |
a1c83681 | 4103 | if (IS_ERR(filename)) |
51689104 PM |
4104 | return ERR_CAST(filename); |
4105 | ||
06422964 | 4106 | set_nameidata(&nd, -1, filename, root); |
c8a53ee5 | 4107 | file = path_openat(&nd, op, flags | LOOKUP_RCU); |
73d049a4 | 4108 | if (unlikely(file == ERR_PTR(-ECHILD))) |
c8a53ee5 | 4109 | file = path_openat(&nd, op, flags); |
73d049a4 | 4110 | if (unlikely(file == ERR_PTR(-ESTALE))) |
c8a53ee5 | 4111 | file = path_openat(&nd, op, flags | LOOKUP_REVAL); |
9883d185 | 4112 | restore_nameidata(); |
51689104 | 4113 | putname(filename); |
73d049a4 AV |
4114 | return file; |
4115 | } | |
4116 | ||
b4a4f213 SB |
4117 | static struct dentry *filename_create(int dfd, struct filename *name, |
4118 | struct path *path, unsigned int lookup_flags) | |
1da177e4 | 4119 | { |
c663e5d8 | 4120 | struct dentry *dentry = ERR_PTR(-EEXIST); |
391172c4 | 4121 | struct qstr last; |
b3d4650d N |
4122 | bool want_dir = lookup_flags & LOOKUP_DIRECTORY; |
4123 | unsigned int reval_flag = lookup_flags & LOOKUP_REVAL; | |
4124 | unsigned int create_flags = LOOKUP_CREATE | LOOKUP_EXCL; | |
391172c4 | 4125 | int type; |
c30dabfe | 4126 | int err2; |
1ac12b4b | 4127 | int error; |
1ac12b4b | 4128 | |
b3d4650d | 4129 | error = filename_parentat(dfd, name, reval_flag, path, &last, &type); |
0ee50b47 DK |
4130 | if (error) |
4131 | return ERR_PTR(error); | |
1da177e4 | 4132 | |
c663e5d8 CH |
4133 | /* |
4134 | * Yucky last component or no last component at all? | |
4135 | * (foo/., foo/.., /////) | |
4136 | */ | |
5c31b6ce | 4137 | if (unlikely(type != LAST_NORM)) |
ed75e95d | 4138 | goto out; |
c663e5d8 | 4139 | |
c30dabfe | 4140 | /* don't fail immediately if it's r/o, at least try to report other errors */ |
391172c4 | 4141 | err2 = mnt_want_write(path->mnt); |
c663e5d8 | 4142 | /* |
b3d4650d N |
4143 | * Do the final lookup. Suppress 'create' if there is a trailing |
4144 | * '/', and a directory wasn't requested. | |
c663e5d8 | 4145 | */ |
b3d4650d | 4146 | if (last.name[last.len] && !want_dir) |
204a575e | 4147 | create_flags &= ~LOOKUP_CREATE; |
5955102c | 4148 | inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT); |
74d7970f NJ |
4149 | dentry = lookup_one_qstr_excl(&last, path->dentry, |
4150 | reval_flag | create_flags); | |
1da177e4 | 4151 | if (IS_ERR(dentry)) |
a8104a9f | 4152 | goto unlock; |
c663e5d8 | 4153 | |
c30dabfe JK |
4154 | if (unlikely(err2)) { |
4155 | error = err2; | |
a8104a9f | 4156 | goto fail; |
c30dabfe | 4157 | } |
1da177e4 | 4158 | return dentry; |
1da177e4 | 4159 | fail: |
a8104a9f AV |
4160 | dput(dentry); |
4161 | dentry = ERR_PTR(error); | |
4162 | unlock: | |
5955102c | 4163 | inode_unlock(path->dentry->d_inode); |
c30dabfe | 4164 | if (!err2) |
391172c4 | 4165 | mnt_drop_write(path->mnt); |
ed75e95d | 4166 | out: |
391172c4 | 4167 | path_put(path); |
1da177e4 LT |
4168 | return dentry; |
4169 | } | |
fa14a0b8 | 4170 | |
b4a4f213 | 4171 | struct dentry *kern_path_create(int dfd, const char *pathname, |
584d3226 DK |
4172 | struct path *path, unsigned int lookup_flags) |
4173 | { | |
b4a4f213 SB |
4174 | struct filename *filename = getname_kernel(pathname); |
4175 | struct dentry *res = filename_create(dfd, filename, path, lookup_flags); | |
584d3226 | 4176 | |
b4a4f213 | 4177 | putname(filename); |
584d3226 DK |
4178 | return res; |
4179 | } | |
dae6ad8f AV |
4180 | EXPORT_SYMBOL(kern_path_create); |
4181 | ||
921a1650 AV |
4182 | void done_path_create(struct path *path, struct dentry *dentry) |
4183 | { | |
c54b3869 N |
4184 | if (!IS_ERR(dentry)) |
4185 | dput(dentry); | |
5955102c | 4186 | inode_unlock(path->dentry->d_inode); |
a8104a9f | 4187 | mnt_drop_write(path->mnt); |
921a1650 AV |
4188 | path_put(path); |
4189 | } | |
4190 | EXPORT_SYMBOL(done_path_create); | |
4191 | ||
520ae687 | 4192 | inline struct dentry *user_path_create(int dfd, const char __user *pathname, |
1ac12b4b | 4193 | struct path *path, unsigned int lookup_flags) |
dae6ad8f | 4194 | { |
b4a4f213 SB |
4195 | struct filename *filename = getname(pathname); |
4196 | struct dentry *res = filename_create(dfd, filename, path, lookup_flags); | |
4197 | ||
4198 | putname(filename); | |
4199 | return res; | |
dae6ad8f AV |
4200 | } |
4201 | EXPORT_SYMBOL(user_path_create); | |
4202 | ||
6521f891 CB |
4203 | /** |
4204 | * vfs_mknod - create device node or file | |
abf08576 | 4205 | * @idmap: idmap of the mount the inode was found from |
b40c8e7a CZ |
4206 | * @dir: inode of the parent directory |
4207 | * @dentry: dentry of the child device node | |
4208 | * @mode: mode of the child device node | |
6521f891 CB |
4209 | * @dev: device number of device to create |
4210 | * | |
4211 | * Create a device node or file. | |
4212 | * | |
abf08576 CB |
4213 | * If the inode has been found through an idmapped mount the idmap of |
4214 | * the vfsmount must be passed through @idmap. This function will then take | |
4215 | * care to map the inode according to @idmap before checking permissions. | |
6521f891 | 4216 | * On non-idmapped mounts or if permission checking is to be performed on the |
376870aa | 4217 | * raw inode simply pass @nop_mnt_idmap. |
6521f891 | 4218 | */ |
abf08576 | 4219 | int vfs_mknod(struct mnt_idmap *idmap, struct inode *dir, |
6521f891 | 4220 | struct dentry *dentry, umode_t mode, dev_t dev) |
1da177e4 | 4221 | { |
a3c751a5 | 4222 | bool is_whiteout = S_ISCHR(mode) && dev == WHITEOUT_DEV; |
4609e1f1 | 4223 | int error = may_create(idmap, dir, dentry); |
1da177e4 LT |
4224 | |
4225 | if (error) | |
4226 | return error; | |
4227 | ||
a3c751a5 MS |
4228 | if ((S_ISCHR(mode) || S_ISBLK(mode)) && !is_whiteout && |
4229 | !capable(CAP_MKNOD)) | |
1da177e4 LT |
4230 | return -EPERM; |
4231 | ||
acfa4380 | 4232 | if (!dir->i_op->mknod) |
1da177e4 LT |
4233 | return -EPERM; |
4234 | ||
9452e93e | 4235 | mode = vfs_prepare_mode(idmap, dir, mode, mode, mode); |
08ce5f16 SH |
4236 | error = devcgroup_inode_mknod(mode, dev); |
4237 | if (error) | |
4238 | return error; | |
4239 | ||
1da177e4 LT |
4240 | error = security_inode_mknod(dir, dentry, mode, dev); |
4241 | if (error) | |
4242 | return error; | |
4243 | ||
5ebb29be | 4244 | error = dir->i_op->mknod(idmap, dir, dentry, mode, dev); |
a74574aa | 4245 | if (!error) |
f38aa942 | 4246 | fsnotify_create(dir, dentry); |
1da177e4 LT |
4247 | return error; |
4248 | } | |
4d359507 | 4249 | EXPORT_SYMBOL(vfs_mknod); |
1da177e4 | 4250 | |
f69aac00 | 4251 | static int may_mknod(umode_t mode) |
463c3197 DH |
4252 | { |
4253 | switch (mode & S_IFMT) { | |
4254 | case S_IFREG: | |
4255 | case S_IFCHR: | |
4256 | case S_IFBLK: | |
4257 | case S_IFIFO: | |
4258 | case S_IFSOCK: | |
4259 | case 0: /* zero mode translates to S_IFREG */ | |
4260 | return 0; | |
4261 | case S_IFDIR: | |
4262 | return -EPERM; | |
4263 | default: | |
4264 | return -EINVAL; | |
4265 | } | |
4266 | } | |
4267 | ||
45f30dab | 4268 | static int do_mknodat(int dfd, struct filename *name, umode_t mode, |
87c4e192 | 4269 | unsigned int dev) |
1da177e4 | 4270 | { |
abf08576 | 4271 | struct mnt_idmap *idmap; |
2ad94ae6 | 4272 | struct dentry *dentry; |
dae6ad8f AV |
4273 | struct path path; |
4274 | int error; | |
972567f1 | 4275 | unsigned int lookup_flags = 0; |
1da177e4 | 4276 | |
8e4bfca1 AV |
4277 | error = may_mknod(mode); |
4278 | if (error) | |
7797251b | 4279 | goto out1; |
972567f1 | 4280 | retry: |
b4a4f213 | 4281 | dentry = filename_create(dfd, name, &path, lookup_flags); |
7797251b | 4282 | error = PTR_ERR(dentry); |
dae6ad8f | 4283 | if (IS_ERR(dentry)) |
7797251b | 4284 | goto out1; |
2ad94ae6 | 4285 | |
1639a49c YX |
4286 | error = security_path_mknod(&path, dentry, |
4287 | mode_strip_umask(path.dentry->d_inode, mode), dev); | |
be6d3e56 | 4288 | if (error) |
7797251b | 4289 | goto out2; |
6521f891 | 4290 | |
abf08576 | 4291 | idmap = mnt_idmap(path.mnt); |
463c3197 | 4292 | switch (mode & S_IFMT) { |
1da177e4 | 4293 | case 0: case S_IFREG: |
abf08576 | 4294 | error = vfs_create(idmap, path.dentry->d_inode, |
6521f891 | 4295 | dentry, mode, true); |
701b3899 RS |
4296 | if (!error) |
4297 | security_path_post_mknod(idmap, dentry); | |
1da177e4 LT |
4298 | break; |
4299 | case S_IFCHR: case S_IFBLK: | |
abf08576 | 4300 | error = vfs_mknod(idmap, path.dentry->d_inode, |
6521f891 | 4301 | dentry, mode, new_decode_dev(dev)); |
1da177e4 LT |
4302 | break; |
4303 | case S_IFIFO: case S_IFSOCK: | |
abf08576 | 4304 | error = vfs_mknod(idmap, path.dentry->d_inode, |
6521f891 | 4305 | dentry, mode, 0); |
1da177e4 | 4306 | break; |
1da177e4 | 4307 | } |
7797251b | 4308 | out2: |
921a1650 | 4309 | done_path_create(&path, dentry); |
972567f1 JL |
4310 | if (retry_estale(error, lookup_flags)) { |
4311 | lookup_flags |= LOOKUP_REVAL; | |
4312 | goto retry; | |
4313 | } | |
7797251b DK |
4314 | out1: |
4315 | putname(name); | |
1da177e4 LT |
4316 | return error; |
4317 | } | |
4318 | ||
87c4e192 DB |
4319 | SYSCALL_DEFINE4(mknodat, int, dfd, const char __user *, filename, umode_t, mode, |
4320 | unsigned int, dev) | |
4321 | { | |
7797251b | 4322 | return do_mknodat(dfd, getname(filename), mode, dev); |
87c4e192 DB |
4323 | } |
4324 | ||
8208a22b | 4325 | SYSCALL_DEFINE3(mknod, const char __user *, filename, umode_t, mode, unsigned, dev) |
5590ff0d | 4326 | { |
7797251b | 4327 | return do_mknodat(AT_FDCWD, getname(filename), mode, dev); |
5590ff0d UD |
4328 | } |
4329 | ||
6521f891 | 4330 | /** |
c54b3869 | 4331 | * vfs_mkdir - create directory returning correct dentry if possible |
abf08576 | 4332 | * @idmap: idmap of the mount the inode was found from |
b40c8e7a CZ |
4333 | * @dir: inode of the parent directory |
4334 | * @dentry: dentry of the child directory | |
4335 | * @mode: mode of the child directory | |
6521f891 CB |
4336 | * |
4337 | * Create a directory. | |
4338 | * | |
abf08576 CB |
4339 | * If the inode has been found through an idmapped mount the idmap of |
4340 | * the vfsmount must be passed through @idmap. This function will then take | |
4341 | * care to map the inode according to @idmap before checking permissions. | |
6521f891 | 4342 | * On non-idmapped mounts or if permission checking is to be performed on the |
376870aa | 4343 | * raw inode simply pass @nop_mnt_idmap. |
c54b3869 N |
4344 | * |
4345 | * In the event that the filesystem does not use the *@dentry but leaves it | |
4346 | * negative or unhashes it and possibly splices a different one returning it, | |
4347 | * the original dentry is dput() and the alternate is returned. | |
4348 | * | |
4349 | * In case of an error the dentry is dput() and an ERR_PTR() is returned. | |
6521f891 | 4350 | */ |
c54b3869 N |
4351 | struct dentry *vfs_mkdir(struct mnt_idmap *idmap, struct inode *dir, |
4352 | struct dentry *dentry, umode_t mode) | |
1da177e4 | 4353 | { |
abf08576 | 4354 | int error; |
8de52778 | 4355 | unsigned max_links = dir->i_sb->s_max_links; |
88d5baf6 | 4356 | struct dentry *de; |
1da177e4 | 4357 | |
4609e1f1 | 4358 | error = may_create(idmap, dir, dentry); |
1da177e4 | 4359 | if (error) |
c54b3869 | 4360 | goto err; |
1da177e4 | 4361 | |
c54b3869 | 4362 | error = -EPERM; |
acfa4380 | 4363 | if (!dir->i_op->mkdir) |
c54b3869 | 4364 | goto err; |
1da177e4 | 4365 | |
9452e93e | 4366 | mode = vfs_prepare_mode(idmap, dir, mode, S_IRWXUGO | S_ISVTX, 0); |
1da177e4 LT |
4367 | error = security_inode_mkdir(dir, dentry, mode); |
4368 | if (error) | |
c54b3869 | 4369 | goto err; |
1da177e4 | 4370 | |
c54b3869 | 4371 | error = -EMLINK; |
8de52778 | 4372 | if (max_links && dir->i_nlink >= max_links) |
c54b3869 | 4373 | goto err; |
8de52778 | 4374 | |
88d5baf6 | 4375 | de = dir->i_op->mkdir(idmap, dir, dentry, mode); |
c54b3869 | 4376 | error = PTR_ERR(de); |
88d5baf6 | 4377 | if (IS_ERR(de)) |
c54b3869 | 4378 | goto err; |
88d5baf6 | 4379 | if (de) { |
c54b3869 N |
4380 | dput(dentry); |
4381 | dentry = de; | |
88d5baf6 | 4382 | } |
c54b3869 N |
4383 | fsnotify_mkdir(dir, dentry); |
4384 | return dentry; | |
88d5baf6 | 4385 | |
c54b3869 N |
4386 | err: |
4387 | dput(dentry); | |
4388 | return ERR_PTR(error); | |
1da177e4 | 4389 | } |
4d359507 | 4390 | EXPORT_SYMBOL(vfs_mkdir); |
1da177e4 | 4391 | |
45f30dab | 4392 | int do_mkdirat(int dfd, struct filename *name, umode_t mode) |
1da177e4 | 4393 | { |
6902d925 | 4394 | struct dentry *dentry; |
dae6ad8f AV |
4395 | struct path path; |
4396 | int error; | |
b76d8b82 | 4397 | unsigned int lookup_flags = LOOKUP_DIRECTORY; |
1da177e4 | 4398 | |
b76d8b82 | 4399 | retry: |
b4a4f213 | 4400 | dentry = filename_create(dfd, name, &path, lookup_flags); |
584d3226 | 4401 | error = PTR_ERR(dentry); |
6902d925 | 4402 | if (IS_ERR(dentry)) |
584d3226 | 4403 | goto out_putname; |
1da177e4 | 4404 | |
1639a49c YX |
4405 | error = security_path_mkdir(&path, dentry, |
4406 | mode_strip_umask(path.dentry->d_inode, mode)); | |
6521f891 | 4407 | if (!error) { |
c54b3869 | 4408 | dentry = vfs_mkdir(mnt_idmap(path.mnt), path.dentry->d_inode, |
abf08576 | 4409 | dentry, mode); |
c54b3869 N |
4410 | if (IS_ERR(dentry)) |
4411 | error = PTR_ERR(dentry); | |
6521f891 | 4412 | } |
921a1650 | 4413 | done_path_create(&path, dentry); |
b76d8b82 JL |
4414 | if (retry_estale(error, lookup_flags)) { |
4415 | lookup_flags |= LOOKUP_REVAL; | |
4416 | goto retry; | |
4417 | } | |
584d3226 DK |
4418 | out_putname: |
4419 | putname(name); | |
1da177e4 LT |
4420 | return error; |
4421 | } | |
4422 | ||
0101db7a DB |
4423 | SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode) |
4424 | { | |
584d3226 | 4425 | return do_mkdirat(dfd, getname(pathname), mode); |
0101db7a DB |
4426 | } |
4427 | ||
a218d0fd | 4428 | SYSCALL_DEFINE2(mkdir, const char __user *, pathname, umode_t, mode) |
5590ff0d | 4429 | { |
584d3226 | 4430 | return do_mkdirat(AT_FDCWD, getname(pathname), mode); |
5590ff0d UD |
4431 | } |
4432 | ||
6521f891 CB |
4433 | /** |
4434 | * vfs_rmdir - remove directory | |
abf08576 | 4435 | * @idmap: idmap of the mount the inode was found from |
b40c8e7a CZ |
4436 | * @dir: inode of the parent directory |
4437 | * @dentry: dentry of the child directory | |
6521f891 CB |
4438 | * |
4439 | * Remove a directory. | |
4440 | * | |
abf08576 CB |
4441 | * If the inode has been found through an idmapped mount the idmap of |
4442 | * the vfsmount must be passed through @idmap. This function will then take | |
4443 | * care to map the inode according to @idmap before checking permissions. | |
6521f891 | 4444 | * On non-idmapped mounts or if permission checking is to be performed on the |
376870aa | 4445 | * raw inode simply pass @nop_mnt_idmap. |
6521f891 | 4446 | */ |
abf08576 | 4447 | int vfs_rmdir(struct mnt_idmap *idmap, struct inode *dir, |
6521f891 | 4448 | struct dentry *dentry) |
1da177e4 | 4449 | { |
4609e1f1 | 4450 | int error = may_delete(idmap, dir, dentry, 1); |
1da177e4 LT |
4451 | |
4452 | if (error) | |
4453 | return error; | |
4454 | ||
acfa4380 | 4455 | if (!dir->i_op->rmdir) |
1da177e4 LT |
4456 | return -EPERM; |
4457 | ||
1d2ef590 | 4458 | dget(dentry); |
5955102c | 4459 | inode_lock(dentry->d_inode); |
912dbc15 SW |
4460 | |
4461 | error = -EBUSY; | |
1bd9c4e4 DH |
4462 | if (is_local_mountpoint(dentry) || |
4463 | (dentry->d_inode->i_flags & S_KERNEL_FILE)) | |
912dbc15 SW |
4464 | goto out; |
4465 | ||
4466 | error = security_inode_rmdir(dir, dentry); | |
4467 | if (error) | |
4468 | goto out; | |
4469 | ||
4470 | error = dir->i_op->rmdir(dir, dentry); | |
4471 | if (error) | |
4472 | goto out; | |
4473 | ||
8767712f | 4474 | shrink_dcache_parent(dentry); |
912dbc15 SW |
4475 | dentry->d_inode->i_flags |= S_DEAD; |
4476 | dont_mount(dentry); | |
8ed936b5 | 4477 | detach_mounts(dentry); |
912dbc15 SW |
4478 | |
4479 | out: | |
5955102c | 4480 | inode_unlock(dentry->d_inode); |
1d2ef590 | 4481 | dput(dentry); |
912dbc15 | 4482 | if (!error) |
a37d9a17 | 4483 | d_delete_notify(dir, dentry); |
1da177e4 LT |
4484 | return error; |
4485 | } | |
4d359507 | 4486 | EXPORT_SYMBOL(vfs_rmdir); |
1da177e4 | 4487 | |
45f30dab | 4488 | int do_rmdir(int dfd, struct filename *name) |
1da177e4 | 4489 | { |
0ee50b47 | 4490 | int error; |
1da177e4 | 4491 | struct dentry *dentry; |
f5beed75 AV |
4492 | struct path path; |
4493 | struct qstr last; | |
4494 | int type; | |
c6ee9206 JL |
4495 | unsigned int lookup_flags = 0; |
4496 | retry: | |
c5f563f9 | 4497 | error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type); |
0ee50b47 DK |
4498 | if (error) |
4499 | goto exit1; | |
1da177e4 | 4500 | |
f5beed75 | 4501 | switch (type) { |
0612d9fb OH |
4502 | case LAST_DOTDOT: |
4503 | error = -ENOTEMPTY; | |
0ee50b47 | 4504 | goto exit2; |
0612d9fb OH |
4505 | case LAST_DOT: |
4506 | error = -EINVAL; | |
0ee50b47 | 4507 | goto exit2; |
0612d9fb OH |
4508 | case LAST_ROOT: |
4509 | error = -EBUSY; | |
0ee50b47 | 4510 | goto exit2; |
1da177e4 | 4511 | } |
0612d9fb | 4512 | |
f5beed75 | 4513 | error = mnt_want_write(path.mnt); |
c30dabfe | 4514 | if (error) |
0ee50b47 | 4515 | goto exit2; |
0612d9fb | 4516 | |
5955102c | 4517 | inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); |
74d7970f | 4518 | dentry = lookup_one_qstr_excl(&last, path.dentry, lookup_flags); |
1da177e4 | 4519 | error = PTR_ERR(dentry); |
6902d925 | 4520 | if (IS_ERR(dentry)) |
0ee50b47 | 4521 | goto exit3; |
f5beed75 | 4522 | error = security_path_rmdir(&path, dentry); |
be6d3e56 | 4523 | if (error) |
0ee50b47 | 4524 | goto exit4; |
abf08576 | 4525 | error = vfs_rmdir(mnt_idmap(path.mnt), path.dentry->d_inode, dentry); |
0ee50b47 | 4526 | exit4: |
6902d925 | 4527 | dput(dentry); |
0ee50b47 | 4528 | exit3: |
5955102c | 4529 | inode_unlock(path.dentry->d_inode); |
f5beed75 | 4530 | mnt_drop_write(path.mnt); |
0ee50b47 | 4531 | exit2: |
f5beed75 | 4532 | path_put(&path); |
c6ee9206 JL |
4533 | if (retry_estale(error, lookup_flags)) { |
4534 | lookup_flags |= LOOKUP_REVAL; | |
4535 | goto retry; | |
4536 | } | |
0ee50b47 | 4537 | exit1: |
24fb33d4 | 4538 | putname(name); |
1da177e4 LT |
4539 | return error; |
4540 | } | |
4541 | ||
3cdad428 | 4542 | SYSCALL_DEFINE1(rmdir, const char __user *, pathname) |
5590ff0d | 4543 | { |
e24ab0ef | 4544 | return do_rmdir(AT_FDCWD, getname(pathname)); |
5590ff0d UD |
4545 | } |
4546 | ||
b21996e3 BF |
4547 | /** |
4548 | * vfs_unlink - unlink a filesystem object | |
abf08576 | 4549 | * @idmap: idmap of the mount the inode was found from |
b21996e3 BF |
4550 | * @dir: parent directory |
4551 | * @dentry: victim | |
4552 | * @delegated_inode: returns victim inode, if the inode is delegated. | |
4553 | * | |
4554 | * The caller must hold dir->i_mutex. | |
4555 | * | |
4556 | * If vfs_unlink discovers a delegation, it will return -EWOULDBLOCK and | |
4557 | * return a reference to the inode in delegated_inode. The caller | |
4558 | * should then break the delegation on that inode and retry. Because | |
4559 | * breaking a delegation may take a long time, the caller should drop | |
4560 | * dir->i_mutex before doing so. | |
4561 | * | |
4562 | * Alternatively, a caller may pass NULL for delegated_inode. This may | |
4563 | * be appropriate for callers that expect the underlying filesystem not | |
4564 | * to be NFS exported. | |
6521f891 | 4565 | * |
abf08576 CB |
4566 | * If the inode has been found through an idmapped mount the idmap of |
4567 | * the vfsmount must be passed through @idmap. This function will then take | |
4568 | * care to map the inode according to @idmap before checking permissions. | |
6521f891 | 4569 | * On non-idmapped mounts or if permission checking is to be performed on the |
376870aa | 4570 | * raw inode simply pass @nop_mnt_idmap. |
b21996e3 | 4571 | */ |
abf08576 | 4572 | int vfs_unlink(struct mnt_idmap *idmap, struct inode *dir, |
6521f891 | 4573 | struct dentry *dentry, struct inode **delegated_inode) |
1da177e4 | 4574 | { |
9accbb97 | 4575 | struct inode *target = dentry->d_inode; |
4609e1f1 | 4576 | int error = may_delete(idmap, dir, dentry, 0); |
1da177e4 LT |
4577 | |
4578 | if (error) | |
4579 | return error; | |
4580 | ||
acfa4380 | 4581 | if (!dir->i_op->unlink) |
1da177e4 LT |
4582 | return -EPERM; |
4583 | ||
5955102c | 4584 | inode_lock(target); |
51cc3a66 HD |
4585 | if (IS_SWAPFILE(target)) |
4586 | error = -EPERM; | |
4587 | else if (is_local_mountpoint(dentry)) | |
1da177e4 LT |
4588 | error = -EBUSY; |
4589 | else { | |
4590 | error = security_inode_unlink(dir, dentry); | |
bec1052e | 4591 | if (!error) { |
5a14696c BF |
4592 | error = try_break_deleg(target, delegated_inode); |
4593 | if (error) | |
b21996e3 | 4594 | goto out; |
1da177e4 | 4595 | error = dir->i_op->unlink(dir, dentry); |
8ed936b5 | 4596 | if (!error) { |
d83c49f3 | 4597 | dont_mount(dentry); |
8ed936b5 EB |
4598 | detach_mounts(dentry); |
4599 | } | |
bec1052e | 4600 | } |
1da177e4 | 4601 | } |
b21996e3 | 4602 | out: |
5955102c | 4603 | inode_unlock(target); |
1da177e4 LT |
4604 | |
4605 | /* We don't d_delete() NFS sillyrenamed files--they still exist. */ | |
a37d9a17 AG |
4606 | if (!error && dentry->d_flags & DCACHE_NFSFS_RENAMED) { |
4607 | fsnotify_unlink(dir, dentry); | |
4608 | } else if (!error) { | |
9accbb97 | 4609 | fsnotify_link_count(target); |
a37d9a17 | 4610 | d_delete_notify(dir, dentry); |
1da177e4 | 4611 | } |
0eeca283 | 4612 | |
1da177e4 LT |
4613 | return error; |
4614 | } | |
4d359507 | 4615 | EXPORT_SYMBOL(vfs_unlink); |
1da177e4 LT |
4616 | |
4617 | /* | |
4618 | * Make sure that the actual truncation of the file will occur outside its | |
1b1dcc1b | 4619 | * directory's i_mutex. Truncate can take a long time if there is a lot of |
1da177e4 LT |
4620 | * writeout happening, and we don't want to prevent access to the directory |
4621 | * while waiting on the I/O. | |
4622 | */ | |
45f30dab | 4623 | int do_unlinkat(int dfd, struct filename *name) |
1da177e4 | 4624 | { |
2ad94ae6 | 4625 | int error; |
1da177e4 | 4626 | struct dentry *dentry; |
f5beed75 AV |
4627 | struct path path; |
4628 | struct qstr last; | |
4629 | int type; | |
1da177e4 | 4630 | struct inode *inode = NULL; |
b21996e3 | 4631 | struct inode *delegated_inode = NULL; |
5d18f813 JL |
4632 | unsigned int lookup_flags = 0; |
4633 | retry: | |
c5f563f9 | 4634 | error = filename_parentat(dfd, name, lookup_flags, &path, &last, &type); |
0ee50b47 DK |
4635 | if (error) |
4636 | goto exit1; | |
2ad94ae6 | 4637 | |
1da177e4 | 4638 | error = -EISDIR; |
f5beed75 | 4639 | if (type != LAST_NORM) |
0ee50b47 | 4640 | goto exit2; |
0612d9fb | 4641 | |
f5beed75 | 4642 | error = mnt_want_write(path.mnt); |
c30dabfe | 4643 | if (error) |
0ee50b47 | 4644 | goto exit2; |
b21996e3 | 4645 | retry_deleg: |
5955102c | 4646 | inode_lock_nested(path.dentry->d_inode, I_MUTEX_PARENT); |
74d7970f | 4647 | dentry = lookup_one_qstr_excl(&last, path.dentry, lookup_flags); |
1da177e4 LT |
4648 | error = PTR_ERR(dentry); |
4649 | if (!IS_ERR(dentry)) { | |
6521f891 | 4650 | |
1da177e4 | 4651 | /* Why not before? Because we want correct error value */ |
204a575e | 4652 | if (last.name[last.len]) |
50338b88 | 4653 | goto slashes; |
1da177e4 | 4654 | inode = dentry->d_inode; |
e6bc45d6 | 4655 | ihold(inode); |
f5beed75 | 4656 | error = security_path_unlink(&path, dentry); |
be6d3e56 | 4657 | if (error) |
0ee50b47 | 4658 | goto exit3; |
abf08576 CB |
4659 | error = vfs_unlink(mnt_idmap(path.mnt), path.dentry->d_inode, |
4660 | dentry, &delegated_inode); | |
0ee50b47 | 4661 | exit3: |
1da177e4 LT |
4662 | dput(dentry); |
4663 | } | |
5955102c | 4664 | inode_unlock(path.dentry->d_inode); |
1da177e4 LT |
4665 | if (inode) |
4666 | iput(inode); /* truncate the inode here */ | |
b21996e3 BF |
4667 | inode = NULL; |
4668 | if (delegated_inode) { | |
5a14696c | 4669 | error = break_deleg_wait(&delegated_inode); |
b21996e3 BF |
4670 | if (!error) |
4671 | goto retry_deleg; | |
4672 | } | |
f5beed75 | 4673 | mnt_drop_write(path.mnt); |
0ee50b47 | 4674 | exit2: |
f5beed75 | 4675 | path_put(&path); |
5d18f813 JL |
4676 | if (retry_estale(error, lookup_flags)) { |
4677 | lookup_flags |= LOOKUP_REVAL; | |
4678 | inode = NULL; | |
4679 | goto retry; | |
4680 | } | |
0ee50b47 | 4681 | exit1: |
da2f1362 | 4682 | putname(name); |
1da177e4 LT |
4683 | return error; |
4684 | ||
4685 | slashes: | |
204a575e | 4686 | if (d_is_dir(dentry)) |
b18825a7 DH |
4687 | error = -EISDIR; |
4688 | else | |
4689 | error = -ENOTDIR; | |
0ee50b47 | 4690 | goto exit3; |
1da177e4 LT |
4691 | } |
4692 | ||
2e4d0924 | 4693 | SYSCALL_DEFINE3(unlinkat, int, dfd, const char __user *, pathname, int, flag) |
5590ff0d UD |
4694 | { |
4695 | if ((flag & ~AT_REMOVEDIR) != 0) | |
4696 | return -EINVAL; | |
4697 | ||
4698 | if (flag & AT_REMOVEDIR) | |
e24ab0ef | 4699 | return do_rmdir(dfd, getname(pathname)); |
da2f1362 | 4700 | return do_unlinkat(dfd, getname(pathname)); |
5590ff0d UD |
4701 | } |
4702 | ||
3480b257 | 4703 | SYSCALL_DEFINE1(unlink, const char __user *, pathname) |
5590ff0d | 4704 | { |
da2f1362 | 4705 | return do_unlinkat(AT_FDCWD, getname(pathname)); |
5590ff0d UD |
4706 | } |
4707 | ||
6521f891 CB |
4708 | /** |
4709 | * vfs_symlink - create symlink | |
abf08576 | 4710 | * @idmap: idmap of the mount the inode was found from |
b40c8e7a CZ |
4711 | * @dir: inode of the parent directory |
4712 | * @dentry: dentry of the child symlink file | |
6521f891 CB |
4713 | * @oldname: name of the file to link to |
4714 | * | |
4715 | * Create a symlink. | |
4716 | * | |
abf08576 CB |
4717 | * If the inode has been found through an idmapped mount the idmap of |
4718 | * the vfsmount must be passed through @idmap. This function will then take | |
4719 | * care to map the inode according to @idmap before checking permissions. | |
6521f891 | 4720 | * On non-idmapped mounts or if permission checking is to be performed on the |
376870aa | 4721 | * raw inode simply pass @nop_mnt_idmap. |
6521f891 | 4722 | */ |
abf08576 | 4723 | int vfs_symlink(struct mnt_idmap *idmap, struct inode *dir, |
6521f891 | 4724 | struct dentry *dentry, const char *oldname) |
1da177e4 | 4725 | { |
7a77db95 | 4726 | int error; |
1da177e4 | 4727 | |
4609e1f1 | 4728 | error = may_create(idmap, dir, dentry); |
1da177e4 LT |
4729 | if (error) |
4730 | return error; | |
4731 | ||
acfa4380 | 4732 | if (!dir->i_op->symlink) |
1da177e4 LT |
4733 | return -EPERM; |
4734 | ||
4735 | error = security_inode_symlink(dir, dentry, oldname); | |
4736 | if (error) | |
4737 | return error; | |
4738 | ||
7a77db95 | 4739 | error = dir->i_op->symlink(idmap, dir, dentry, oldname); |
a74574aa | 4740 | if (!error) |
f38aa942 | 4741 | fsnotify_create(dir, dentry); |
1da177e4 LT |
4742 | return error; |
4743 | } | |
4d359507 | 4744 | EXPORT_SYMBOL(vfs_symlink); |
1da177e4 | 4745 | |
7a8721f8 | 4746 | int do_symlinkat(struct filename *from, int newdfd, struct filename *to) |
1da177e4 | 4747 | { |
2ad94ae6 | 4748 | int error; |
6902d925 | 4749 | struct dentry *dentry; |
dae6ad8f | 4750 | struct path path; |
f46d3567 | 4751 | unsigned int lookup_flags = 0; |
1da177e4 | 4752 | |
da2d0ced DK |
4753 | if (IS_ERR(from)) { |
4754 | error = PTR_ERR(from); | |
4755 | goto out_putnames; | |
4756 | } | |
f46d3567 | 4757 | retry: |
b4a4f213 | 4758 | dentry = filename_create(newdfd, to, &path, lookup_flags); |
6902d925 DH |
4759 | error = PTR_ERR(dentry); |
4760 | if (IS_ERR(dentry)) | |
da2d0ced | 4761 | goto out_putnames; |
6902d925 | 4762 | |
91a27b2a | 4763 | error = security_path_symlink(&path, dentry, from->name); |
abf08576 CB |
4764 | if (!error) |
4765 | error = vfs_symlink(mnt_idmap(path.mnt), path.dentry->d_inode, | |
4766 | dentry, from->name); | |
921a1650 | 4767 | done_path_create(&path, dentry); |
f46d3567 JL |
4768 | if (retry_estale(error, lookup_flags)) { |
4769 | lookup_flags |= LOOKUP_REVAL; | |
4770 | goto retry; | |
4771 | } | |
da2d0ced DK |
4772 | out_putnames: |
4773 | putname(to); | |
1da177e4 LT |
4774 | putname(from); |
4775 | return error; | |
4776 | } | |
4777 | ||
b724e846 DB |
4778 | SYSCALL_DEFINE3(symlinkat, const char __user *, oldname, |
4779 | int, newdfd, const char __user *, newname) | |
4780 | { | |
da2d0ced | 4781 | return do_symlinkat(getname(oldname), newdfd, getname(newname)); |
b724e846 DB |
4782 | } |
4783 | ||
3480b257 | 4784 | SYSCALL_DEFINE2(symlink, const char __user *, oldname, const char __user *, newname) |
5590ff0d | 4785 | { |
da2d0ced | 4786 | return do_symlinkat(getname(oldname), AT_FDCWD, getname(newname)); |
5590ff0d UD |
4787 | } |
4788 | ||
146a8595 BF |
4789 | /** |
4790 | * vfs_link - create a new link | |
4791 | * @old_dentry: object to be linked | |
abf08576 | 4792 | * @idmap: idmap of the mount |
146a8595 BF |
4793 | * @dir: new parent |
4794 | * @new_dentry: where to create the new link | |
4795 | * @delegated_inode: returns inode needing a delegation break | |
4796 | * | |
4797 | * The caller must hold dir->i_mutex | |
4798 | * | |
4799 | * If vfs_link discovers a delegation on the to-be-linked file in need | |
4800 | * of breaking, it will return -EWOULDBLOCK and return a reference to the | |
4801 | * inode in delegated_inode. The caller should then break the delegation | |
4802 | * and retry. Because breaking a delegation may take a long time, the | |
4803 | * caller should drop the i_mutex before doing so. | |
4804 | * | |
4805 | * Alternatively, a caller may pass NULL for delegated_inode. This may | |
4806 | * be appropriate for callers that expect the underlying filesystem not | |
4807 | * to be NFS exported. | |
6521f891 | 4808 | * |
abf08576 CB |
4809 | * If the inode has been found through an idmapped mount the idmap of |
4810 | * the vfsmount must be passed through @idmap. This function will then take | |
4811 | * care to map the inode according to @idmap before checking permissions. | |
6521f891 | 4812 | * On non-idmapped mounts or if permission checking is to be performed on the |
376870aa | 4813 | * raw inode simply pass @nop_mnt_idmap. |
146a8595 | 4814 | */ |
abf08576 | 4815 | int vfs_link(struct dentry *old_dentry, struct mnt_idmap *idmap, |
6521f891 CB |
4816 | struct inode *dir, struct dentry *new_dentry, |
4817 | struct inode **delegated_inode) | |
1da177e4 LT |
4818 | { |
4819 | struct inode *inode = old_dentry->d_inode; | |
8de52778 | 4820 | unsigned max_links = dir->i_sb->s_max_links; |
1da177e4 LT |
4821 | int error; |
4822 | ||
4823 | if (!inode) | |
4824 | return -ENOENT; | |
4825 | ||
4609e1f1 | 4826 | error = may_create(idmap, dir, new_dentry); |
1da177e4 LT |
4827 | if (error) |
4828 | return error; | |
4829 | ||
4830 | if (dir->i_sb != inode->i_sb) | |
4831 | return -EXDEV; | |
4832 | ||
4833 | /* | |
4834 | * A link to an append-only or immutable file cannot be created. | |
4835 | */ | |
4836 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) | |
4837 | return -EPERM; | |
0bd23d09 EB |
4838 | /* |
4839 | * Updating the link count will likely cause i_uid and i_gid to | |
4840 | * be writen back improperly if their true value is unknown to | |
4841 | * the vfs. | |
4842 | */ | |
4609e1f1 | 4843 | if (HAS_UNMAPPED_ID(idmap, inode)) |
0bd23d09 | 4844 | return -EPERM; |
acfa4380 | 4845 | if (!dir->i_op->link) |
1da177e4 | 4846 | return -EPERM; |
7e79eedb | 4847 | if (S_ISDIR(inode->i_mode)) |
1da177e4 LT |
4848 | return -EPERM; |
4849 | ||
4850 | error = security_inode_link(old_dentry, dir, new_dentry); | |
4851 | if (error) | |
4852 | return error; | |
4853 | ||
5955102c | 4854 | inode_lock(inode); |
aae8a97d | 4855 | /* Make sure we don't allow creating hardlink to an unlinked file */ |
f4e0c30c | 4856 | if (inode->i_nlink == 0 && !(inode->i_state & I_LINKABLE)) |
aae8a97d | 4857 | error = -ENOENT; |
8de52778 AV |
4858 | else if (max_links && inode->i_nlink >= max_links) |
4859 | error = -EMLINK; | |
146a8595 BF |
4860 | else { |
4861 | error = try_break_deleg(inode, delegated_inode); | |
4862 | if (!error) | |
4863 | error = dir->i_op->link(old_dentry, dir, new_dentry); | |
4864 | } | |
f4e0c30c AV |
4865 | |
4866 | if (!error && (inode->i_state & I_LINKABLE)) { | |
4867 | spin_lock(&inode->i_lock); | |
4868 | inode->i_state &= ~I_LINKABLE; | |
4869 | spin_unlock(&inode->i_lock); | |
4870 | } | |
5955102c | 4871 | inode_unlock(inode); |
e31e14ec | 4872 | if (!error) |
7e79eedb | 4873 | fsnotify_link(dir, inode, new_dentry); |
1da177e4 LT |
4874 | return error; |
4875 | } | |
4d359507 | 4876 | EXPORT_SYMBOL(vfs_link); |
1da177e4 LT |
4877 | |
4878 | /* | |
4879 | * Hardlinks are often used in delicate situations. We avoid | |
4880 | * security-related surprises by not following symlinks on the | |
4881 | * newname. --KAB | |
4882 | * | |
4883 | * We don't follow them on the oldname either to be compatible | |
4884 | * with linux 2.0, and to avoid hard-linking to directories | |
4885 | * and other special files. --ADM | |
4886 | */ | |
cf30da90 | 4887 | int do_linkat(int olddfd, struct filename *old, int newdfd, |
020250f3 | 4888 | struct filename *new, int flags) |
1da177e4 | 4889 | { |
abf08576 | 4890 | struct mnt_idmap *idmap; |
1da177e4 | 4891 | struct dentry *new_dentry; |
dae6ad8f | 4892 | struct path old_path, new_path; |
146a8595 | 4893 | struct inode *delegated_inode = NULL; |
11a7b371 | 4894 | int how = 0; |
1da177e4 | 4895 | int error; |
1da177e4 | 4896 | |
020250f3 DK |
4897 | if ((flags & ~(AT_SYMLINK_FOLLOW | AT_EMPTY_PATH)) != 0) { |
4898 | error = -EINVAL; | |
4899 | goto out_putnames; | |
4900 | } | |
11a7b371 | 4901 | /* |
42bd2af5 LT |
4902 | * To use null names we require CAP_DAC_READ_SEARCH or |
4903 | * that the open-time creds of the dfd matches current. | |
f0cc6ffb | 4904 | * This ensures that not everyone will be able to create |
42bd2af5 | 4905 | * a hardlink using the passed file descriptor. |
11a7b371 | 4906 | */ |
42bd2af5 LT |
4907 | if (flags & AT_EMPTY_PATH) |
4908 | how |= LOOKUP_LINKAT_EMPTY; | |
11a7b371 AK |
4909 | |
4910 | if (flags & AT_SYMLINK_FOLLOW) | |
4911 | how |= LOOKUP_FOLLOW; | |
442e31ca | 4912 | retry: |
794ebcea | 4913 | error = filename_lookup(olddfd, old, how, &old_path, NULL); |
1da177e4 | 4914 | if (error) |
020250f3 | 4915 | goto out_putnames; |
2ad94ae6 | 4916 | |
b4a4f213 | 4917 | new_dentry = filename_create(newdfd, new, &new_path, |
442e31ca | 4918 | (how & LOOKUP_REVAL)); |
1da177e4 | 4919 | error = PTR_ERR(new_dentry); |
6902d925 | 4920 | if (IS_ERR(new_dentry)) |
020250f3 | 4921 | goto out_putpath; |
dae6ad8f AV |
4922 | |
4923 | error = -EXDEV; | |
4924 | if (old_path.mnt != new_path.mnt) | |
4925 | goto out_dput; | |
abf08576 | 4926 | idmap = mnt_idmap(new_path.mnt); |
4609e1f1 | 4927 | error = may_linkat(idmap, &old_path); |
800179c9 KC |
4928 | if (unlikely(error)) |
4929 | goto out_dput; | |
dae6ad8f | 4930 | error = security_path_link(old_path.dentry, &new_path, new_dentry); |
be6d3e56 | 4931 | if (error) |
a8104a9f | 4932 | goto out_dput; |
abf08576 | 4933 | error = vfs_link(old_path.dentry, idmap, new_path.dentry->d_inode, |
6521f891 | 4934 | new_dentry, &delegated_inode); |
75c3f29d | 4935 | out_dput: |
921a1650 | 4936 | done_path_create(&new_path, new_dentry); |
146a8595 BF |
4937 | if (delegated_inode) { |
4938 | error = break_deleg_wait(&delegated_inode); | |
d22e6338 OD |
4939 | if (!error) { |
4940 | path_put(&old_path); | |
146a8595 | 4941 | goto retry; |
d22e6338 | 4942 | } |
146a8595 | 4943 | } |
442e31ca | 4944 | if (retry_estale(error, how)) { |
d22e6338 | 4945 | path_put(&old_path); |
442e31ca JL |
4946 | how |= LOOKUP_REVAL; |
4947 | goto retry; | |
4948 | } | |
020250f3 | 4949 | out_putpath: |
2d8f3038 | 4950 | path_put(&old_path); |
020250f3 DK |
4951 | out_putnames: |
4952 | putname(old); | |
4953 | putname(new); | |
1da177e4 LT |
4954 | |
4955 | return error; | |
4956 | } | |
4957 | ||
46ea89eb DB |
4958 | SYSCALL_DEFINE5(linkat, int, olddfd, const char __user *, oldname, |
4959 | int, newdfd, const char __user *, newname, int, flags) | |
4960 | { | |
020250f3 DK |
4961 | return do_linkat(olddfd, getname_uflags(oldname, flags), |
4962 | newdfd, getname(newname), flags); | |
46ea89eb DB |
4963 | } |
4964 | ||
3480b257 | 4965 | SYSCALL_DEFINE2(link, const char __user *, oldname, const char __user *, newname) |
5590ff0d | 4966 | { |
020250f3 | 4967 | return do_linkat(AT_FDCWD, getname(oldname), AT_FDCWD, getname(newname), 0); |
5590ff0d UD |
4968 | } |
4969 | ||
bc27027a MS |
4970 | /** |
4971 | * vfs_rename - rename a filesystem object | |
2111c3c0 | 4972 | * @rd: pointer to &struct renamedata info |
bc27027a MS |
4973 | * |
4974 | * The caller must hold multiple mutexes--see lock_rename()). | |
4975 | * | |
4976 | * If vfs_rename discovers a delegation in need of breaking at either | |
4977 | * the source or destination, it will return -EWOULDBLOCK and return a | |
4978 | * reference to the inode in delegated_inode. The caller should then | |
4979 | * break the delegation and retry. Because breaking a delegation may | |
4980 | * take a long time, the caller should drop all locks before doing | |
4981 | * so. | |
4982 | * | |
4983 | * Alternatively, a caller may pass NULL for delegated_inode. This may | |
4984 | * be appropriate for callers that expect the underlying filesystem not | |
4985 | * to be NFS exported. | |
4986 | * | |
1da177e4 LT |
4987 | * The worst of all namespace operations - renaming directory. "Perverted" |
4988 | * doesn't even start to describe it. Somebody in UCB had a heck of a trip... | |
4989 | * Problems: | |
0117d427 | 4990 | * |
d03b29a2 | 4991 | * a) we can get into loop creation. |
1da177e4 | 4992 | * b) race potential - two innocent renames can create a loop together. |
22e111ed | 4993 | * That's where 4.4BSD screws up. Current fix: serialization on |
a11f3a05 | 4994 | * sb->s_vfs_rename_mutex. We might be more accurate, but that's another |
1da177e4 | 4995 | * story. |
22e111ed AV |
4996 | * c) we may have to lock up to _four_ objects - parents and victim (if it exists), |
4997 | * and source (if it's a non-directory or a subdirectory that moves to | |
4998 | * different parent). | |
1b1dcc1b | 4999 | * And that - after we got ->i_mutex on parents (until then we don't know |
1da177e4 LT |
5000 | * whether the target exists). Solution: try to be smart with locking |
5001 | * order for inodes. We rely on the fact that tree topology may change | |
a11f3a05 | 5002 | * only under ->s_vfs_rename_mutex _and_ that parent of the object we |
1da177e4 LT |
5003 | * move will be locked. Thus we can rank directories by the tree |
5004 | * (ancestors first) and rank all non-directories after them. | |
5005 | * That works since everybody except rename does "lock parent, lookup, | |
a11f3a05 | 5006 | * lock child" and rename is under ->s_vfs_rename_mutex. |
1da177e4 LT |
5007 | * HOWEVER, it relies on the assumption that any object with ->lookup() |
5008 | * has no more than 1 dentry. If "hybrid" objects will ever appear, | |
5009 | * we'd better make sure that there's no link(2) for them. | |
e4eaac06 | 5010 | * d) conversion from fhandle to dentry may come in the wrong moment - when |
1b1dcc1b | 5011 | * we are removing the target. Solution: we will have to grab ->i_mutex |
1da177e4 | 5012 | * in the fhandle_to_dentry code. [FIXME - current nfsfh.c relies on |
c41b20e7 | 5013 | * ->i_mutex on parents, which works but leads to some truly excessive |
1da177e4 LT |
5014 | * locking]. |
5015 | */ | |
9fe61450 | 5016 | int vfs_rename(struct renamedata *rd) |
1da177e4 | 5017 | { |
bc27027a | 5018 | int error; |
9fe61450 CB |
5019 | struct inode *old_dir = rd->old_dir, *new_dir = rd->new_dir; |
5020 | struct dentry *old_dentry = rd->old_dentry; | |
5021 | struct dentry *new_dentry = rd->new_dentry; | |
5022 | struct inode **delegated_inode = rd->delegated_inode; | |
5023 | unsigned int flags = rd->flags; | |
bc27027a | 5024 | bool is_dir = d_is_dir(old_dentry); |
bc27027a | 5025 | struct inode *source = old_dentry->d_inode; |
9055cba7 | 5026 | struct inode *target = new_dentry->d_inode; |
da1ce067 MS |
5027 | bool new_is_dir = false; |
5028 | unsigned max_links = new_dir->i_sb->s_max_links; | |
49d31c2f | 5029 | struct name_snapshot old_name; |
22e111ed | 5030 | bool lock_old_subdir, lock_new_subdir; |
bc27027a | 5031 | |
8d3e2936 | 5032 | if (source == target) |
bc27027a MS |
5033 | return 0; |
5034 | ||
4609e1f1 | 5035 | error = may_delete(rd->old_mnt_idmap, old_dir, old_dentry, is_dir); |
bc27027a MS |
5036 | if (error) |
5037 | return error; | |
5038 | ||
da1ce067 | 5039 | if (!target) { |
4609e1f1 | 5040 | error = may_create(rd->new_mnt_idmap, new_dir, new_dentry); |
da1ce067 MS |
5041 | } else { |
5042 | new_is_dir = d_is_dir(new_dentry); | |
5043 | ||
5044 | if (!(flags & RENAME_EXCHANGE)) | |
4609e1f1 | 5045 | error = may_delete(rd->new_mnt_idmap, new_dir, |
6521f891 | 5046 | new_dentry, is_dir); |
da1ce067 | 5047 | else |
4609e1f1 | 5048 | error = may_delete(rd->new_mnt_idmap, new_dir, |
6521f891 | 5049 | new_dentry, new_is_dir); |
da1ce067 | 5050 | } |
bc27027a MS |
5051 | if (error) |
5052 | return error; | |
5053 | ||
2773bf00 | 5054 | if (!old_dir->i_op->rename) |
bc27027a | 5055 | return -EPERM; |
1da177e4 LT |
5056 | |
5057 | /* | |
5058 | * If we are going to change the parent - check write permissions, | |
5059 | * we'll need to flip '..'. | |
5060 | */ | |
da1ce067 MS |
5061 | if (new_dir != old_dir) { |
5062 | if (is_dir) { | |
4609e1f1 | 5063 | error = inode_permission(rd->old_mnt_idmap, source, |
47291baa | 5064 | MAY_WRITE); |
da1ce067 MS |
5065 | if (error) |
5066 | return error; | |
5067 | } | |
5068 | if ((flags & RENAME_EXCHANGE) && new_is_dir) { | |
4609e1f1 | 5069 | error = inode_permission(rd->new_mnt_idmap, target, |
47291baa | 5070 | MAY_WRITE); |
da1ce067 MS |
5071 | if (error) |
5072 | return error; | |
5073 | } | |
1da177e4 LT |
5074 | } |
5075 | ||
0b3974eb MS |
5076 | error = security_inode_rename(old_dir, old_dentry, new_dir, new_dentry, |
5077 | flags); | |
1da177e4 LT |
5078 | if (error) |
5079 | return error; | |
5080 | ||
49d31c2f | 5081 | take_dentry_name_snapshot(&old_name, old_dentry); |
1d2ef590 | 5082 | dget(new_dentry); |
28eceeda | 5083 | /* |
22e111ed AV |
5084 | * Lock children. |
5085 | * The source subdirectory needs to be locked on cross-directory | |
5086 | * rename or cross-directory exchange since its parent changes. | |
5087 | * The target subdirectory needs to be locked on cross-directory | |
5088 | * exchange due to parent change and on any rename due to becoming | |
5089 | * a victim. | |
5090 | * Non-directories need locking in all cases (for NFS reasons); | |
5091 | * they get locked after any subdirectories (in inode address order). | |
5092 | * | |
5093 | * NOTE: WE ONLY LOCK UNRELATED DIRECTORIES IN CROSS-DIRECTORY CASE. | |
5094 | * NEVER, EVER DO THAT WITHOUT ->s_vfs_rename_mutex. | |
28eceeda | 5095 | */ |
22e111ed AV |
5096 | lock_old_subdir = new_dir != old_dir; |
5097 | lock_new_subdir = new_dir != old_dir || !(flags & RENAME_EXCHANGE); | |
5098 | if (is_dir) { | |
5099 | if (lock_old_subdir) | |
5100 | inode_lock_nested(source, I_MUTEX_CHILD); | |
5101 | if (target && (!new_is_dir || lock_new_subdir)) | |
5102 | inode_lock(target); | |
5103 | } else if (new_is_dir) { | |
5104 | if (lock_new_subdir) | |
5105 | inode_lock_nested(target, I_MUTEX_CHILD); | |
5106 | inode_lock(source); | |
5107 | } else { | |
5108 | lock_two_nondirectories(source, target); | |
5109 | } | |
9055cba7 | 5110 | |
51cc3a66 HD |
5111 | error = -EPERM; |
5112 | if (IS_SWAPFILE(source) || (target && IS_SWAPFILE(target))) | |
5113 | goto out; | |
5114 | ||
9055cba7 | 5115 | error = -EBUSY; |
7af1364f | 5116 | if (is_local_mountpoint(old_dentry) || is_local_mountpoint(new_dentry)) |
9055cba7 SW |
5117 | goto out; |
5118 | ||
da1ce067 | 5119 | if (max_links && new_dir != old_dir) { |
bc27027a | 5120 | error = -EMLINK; |
da1ce067 | 5121 | if (is_dir && !new_is_dir && new_dir->i_nlink >= max_links) |
bc27027a | 5122 | goto out; |
da1ce067 MS |
5123 | if ((flags & RENAME_EXCHANGE) && !is_dir && new_is_dir && |
5124 | old_dir->i_nlink >= max_links) | |
5125 | goto out; | |
5126 | } | |
da1ce067 | 5127 | if (!is_dir) { |
bc27027a | 5128 | error = try_break_deleg(source, delegated_inode); |
8e6d782c BF |
5129 | if (error) |
5130 | goto out; | |
da1ce067 MS |
5131 | } |
5132 | if (target && !new_is_dir) { | |
5133 | error = try_break_deleg(target, delegated_inode); | |
5134 | if (error) | |
5135 | goto out; | |
8e6d782c | 5136 | } |
e18275ae | 5137 | error = old_dir->i_op->rename(rd->new_mnt_idmap, old_dir, old_dentry, |
549c7297 | 5138 | new_dir, new_dentry, flags); |
51892bbb SW |
5139 | if (error) |
5140 | goto out; | |
5141 | ||
da1ce067 | 5142 | if (!(flags & RENAME_EXCHANGE) && target) { |
8767712f AV |
5143 | if (is_dir) { |
5144 | shrink_dcache_parent(new_dentry); | |
bc27027a | 5145 | target->i_flags |= S_DEAD; |
8767712f | 5146 | } |
51892bbb | 5147 | dont_mount(new_dentry); |
8ed936b5 | 5148 | detach_mounts(new_dentry); |
bc27027a | 5149 | } |
da1ce067 MS |
5150 | if (!(old_dir->i_sb->s_type->fs_flags & FS_RENAME_DOES_D_MOVE)) { |
5151 | if (!(flags & RENAME_EXCHANGE)) | |
5152 | d_move(old_dentry, new_dentry); | |
5153 | else | |
5154 | d_exchange(old_dentry, new_dentry); | |
5155 | } | |
51892bbb | 5156 | out: |
22e111ed AV |
5157 | if (!is_dir || lock_old_subdir) |
5158 | inode_unlock(source); | |
5159 | if (target && (!new_is_dir || lock_new_subdir)) | |
5955102c | 5160 | inode_unlock(target); |
1da177e4 | 5161 | dput(new_dentry); |
da1ce067 | 5162 | if (!error) { |
f4ec3a3d | 5163 | fsnotify_move(old_dir, new_dir, &old_name.name, is_dir, |
da1ce067 MS |
5164 | !(flags & RENAME_EXCHANGE) ? target : NULL, old_dentry); |
5165 | if (flags & RENAME_EXCHANGE) { | |
f4ec3a3d | 5166 | fsnotify_move(new_dir, old_dir, &old_dentry->d_name, |
da1ce067 MS |
5167 | new_is_dir, NULL, new_dentry); |
5168 | } | |
5169 | } | |
49d31c2f | 5170 | release_dentry_name_snapshot(&old_name); |
0eeca283 | 5171 | |
1da177e4 LT |
5172 | return error; |
5173 | } | |
4d359507 | 5174 | EXPORT_SYMBOL(vfs_rename); |
1da177e4 | 5175 | |
e886663c JA |
5176 | int do_renameat2(int olddfd, struct filename *from, int newdfd, |
5177 | struct filename *to, unsigned int flags) | |
1da177e4 | 5178 | { |
9fe61450 | 5179 | struct renamedata rd; |
2ad94ae6 AV |
5180 | struct dentry *old_dentry, *new_dentry; |
5181 | struct dentry *trap; | |
f5beed75 AV |
5182 | struct path old_path, new_path; |
5183 | struct qstr old_last, new_last; | |
5184 | int old_type, new_type; | |
8e6d782c | 5185 | struct inode *delegated_inode = NULL; |
204a575e N |
5186 | unsigned int lookup_flags = 0, target_flags = |
5187 | LOOKUP_RENAME_TARGET | LOOKUP_CREATE; | |
c6a94284 | 5188 | bool should_retry = false; |
e886663c | 5189 | int error = -EINVAL; |
520c8b16 | 5190 | |
0d7a8555 | 5191 | if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT)) |
0ee50b47 | 5192 | goto put_names; |
da1ce067 | 5193 | |
0d7a8555 MS |
5194 | if ((flags & (RENAME_NOREPLACE | RENAME_WHITEOUT)) && |
5195 | (flags & RENAME_EXCHANGE)) | |
0ee50b47 | 5196 | goto put_names; |
520c8b16 | 5197 | |
f5beed75 AV |
5198 | if (flags & RENAME_EXCHANGE) |
5199 | target_flags = 0; | |
204a575e N |
5200 | if (flags & RENAME_NOREPLACE) |
5201 | target_flags |= LOOKUP_EXCL; | |
f5beed75 | 5202 | |
c6a94284 | 5203 | retry: |
c5f563f9 AV |
5204 | error = filename_parentat(olddfd, from, lookup_flags, &old_path, |
5205 | &old_last, &old_type); | |
0ee50b47 DK |
5206 | if (error) |
5207 | goto put_names; | |
1da177e4 | 5208 | |
c5f563f9 AV |
5209 | error = filename_parentat(newdfd, to, lookup_flags, &new_path, &new_last, |
5210 | &new_type); | |
0ee50b47 | 5211 | if (error) |
1da177e4 LT |
5212 | goto exit1; |
5213 | ||
5214 | error = -EXDEV; | |
f5beed75 | 5215 | if (old_path.mnt != new_path.mnt) |
1da177e4 LT |
5216 | goto exit2; |
5217 | ||
1da177e4 | 5218 | error = -EBUSY; |
f5beed75 | 5219 | if (old_type != LAST_NORM) |
1da177e4 LT |
5220 | goto exit2; |
5221 | ||
0a7c3937 MS |
5222 | if (flags & RENAME_NOREPLACE) |
5223 | error = -EEXIST; | |
f5beed75 | 5224 | if (new_type != LAST_NORM) |
1da177e4 LT |
5225 | goto exit2; |
5226 | ||
f5beed75 | 5227 | error = mnt_want_write(old_path.mnt); |
c30dabfe JK |
5228 | if (error) |
5229 | goto exit2; | |
5230 | ||
8e6d782c | 5231 | retry_deleg: |
f5beed75 | 5232 | trap = lock_rename(new_path.dentry, old_path.dentry); |
a8b00268 AV |
5233 | if (IS_ERR(trap)) { |
5234 | error = PTR_ERR(trap); | |
5235 | goto exit_lock_rename; | |
5236 | } | |
1da177e4 | 5237 | |
74d7970f NJ |
5238 | old_dentry = lookup_one_qstr_excl(&old_last, old_path.dentry, |
5239 | lookup_flags); | |
1da177e4 LT |
5240 | error = PTR_ERR(old_dentry); |
5241 | if (IS_ERR(old_dentry)) | |
5242 | goto exit3; | |
74d7970f NJ |
5243 | new_dentry = lookup_one_qstr_excl(&new_last, new_path.dentry, |
5244 | lookup_flags | target_flags); | |
0a7c3937 MS |
5245 | error = PTR_ERR(new_dentry); |
5246 | if (IS_ERR(new_dentry)) | |
5247 | goto exit4; | |
da1ce067 | 5248 | if (flags & RENAME_EXCHANGE) { |
da1ce067 MS |
5249 | if (!d_is_dir(new_dentry)) { |
5250 | error = -ENOTDIR; | |
f5beed75 | 5251 | if (new_last.name[new_last.len]) |
da1ce067 MS |
5252 | goto exit5; |
5253 | } | |
5254 | } | |
1da177e4 | 5255 | /* unless the source is a directory trailing slashes give -ENOTDIR */ |
44b1d530 | 5256 | if (!d_is_dir(old_dentry)) { |
1da177e4 | 5257 | error = -ENOTDIR; |
f5beed75 | 5258 | if (old_last.name[old_last.len]) |
0a7c3937 | 5259 | goto exit5; |
f5beed75 | 5260 | if (!(flags & RENAME_EXCHANGE) && new_last.name[new_last.len]) |
0a7c3937 | 5261 | goto exit5; |
1da177e4 LT |
5262 | } |
5263 | /* source should not be ancestor of target */ | |
5264 | error = -EINVAL; | |
5265 | if (old_dentry == trap) | |
0a7c3937 | 5266 | goto exit5; |
1da177e4 | 5267 | /* target should not be an ancestor of source */ |
da1ce067 MS |
5268 | if (!(flags & RENAME_EXCHANGE)) |
5269 | error = -ENOTEMPTY; | |
1da177e4 LT |
5270 | if (new_dentry == trap) |
5271 | goto exit5; | |
5272 | ||
f5beed75 AV |
5273 | error = security_path_rename(&old_path, old_dentry, |
5274 | &new_path, new_dentry, flags); | |
be6d3e56 | 5275 | if (error) |
c30dabfe | 5276 | goto exit5; |
9fe61450 CB |
5277 | |
5278 | rd.old_dir = old_path.dentry->d_inode; | |
5279 | rd.old_dentry = old_dentry; | |
abf08576 | 5280 | rd.old_mnt_idmap = mnt_idmap(old_path.mnt); |
9fe61450 CB |
5281 | rd.new_dir = new_path.dentry->d_inode; |
5282 | rd.new_dentry = new_dentry; | |
abf08576 | 5283 | rd.new_mnt_idmap = mnt_idmap(new_path.mnt); |
9fe61450 CB |
5284 | rd.delegated_inode = &delegated_inode; |
5285 | rd.flags = flags; | |
5286 | error = vfs_rename(&rd); | |
1da177e4 LT |
5287 | exit5: |
5288 | dput(new_dentry); | |
5289 | exit4: | |
5290 | dput(old_dentry); | |
5291 | exit3: | |
f5beed75 | 5292 | unlock_rename(new_path.dentry, old_path.dentry); |
a8b00268 | 5293 | exit_lock_rename: |
8e6d782c BF |
5294 | if (delegated_inode) { |
5295 | error = break_deleg_wait(&delegated_inode); | |
5296 | if (!error) | |
5297 | goto retry_deleg; | |
5298 | } | |
f5beed75 | 5299 | mnt_drop_write(old_path.mnt); |
1da177e4 | 5300 | exit2: |
c6a94284 JL |
5301 | if (retry_estale(error, lookup_flags)) |
5302 | should_retry = true; | |
f5beed75 | 5303 | path_put(&new_path); |
1da177e4 | 5304 | exit1: |
f5beed75 | 5305 | path_put(&old_path); |
c6a94284 JL |
5306 | if (should_retry) { |
5307 | should_retry = false; | |
5308 | lookup_flags |= LOOKUP_REVAL; | |
5309 | goto retry; | |
5310 | } | |
0ee50b47 | 5311 | put_names: |
91ef658f | 5312 | putname(from); |
91ef658f | 5313 | putname(to); |
1da177e4 LT |
5314 | return error; |
5315 | } | |
5316 | ||
ee81feb6 DB |
5317 | SYSCALL_DEFINE5(renameat2, int, olddfd, const char __user *, oldname, |
5318 | int, newdfd, const char __user *, newname, unsigned int, flags) | |
5319 | { | |
e886663c JA |
5320 | return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname), |
5321 | flags); | |
ee81feb6 DB |
5322 | } |
5323 | ||
520c8b16 MS |
5324 | SYSCALL_DEFINE4(renameat, int, olddfd, const char __user *, oldname, |
5325 | int, newdfd, const char __user *, newname) | |
5326 | { | |
e886663c JA |
5327 | return do_renameat2(olddfd, getname(oldname), newdfd, getname(newname), |
5328 | 0); | |
520c8b16 MS |
5329 | } |
5330 | ||
a26eab24 | 5331 | SYSCALL_DEFINE2(rename, const char __user *, oldname, const char __user *, newname) |
5590ff0d | 5332 | { |
e886663c JA |
5333 | return do_renameat2(AT_FDCWD, getname(oldname), AT_FDCWD, |
5334 | getname(newname), 0); | |
5590ff0d UD |
5335 | } |
5336 | ||
ea382199 | 5337 | int readlink_copy(char __user *buffer, int buflen, const char *link, int linklen) |
1da177e4 | 5338 | { |
ea382199 | 5339 | int copylen; |
1da177e4 | 5340 | |
ea382199 MG |
5341 | copylen = linklen; |
5342 | if (unlikely(copylen > (unsigned) buflen)) | |
5343 | copylen = buflen; | |
5344 | if (copy_to_user(buffer, link, copylen)) | |
5345 | copylen = -EFAULT; | |
5346 | return copylen; | |
1da177e4 LT |
5347 | } |
5348 | ||
fd4a0edf MS |
5349 | /** |
5350 | * vfs_readlink - copy symlink body into userspace buffer | |
5351 | * @dentry: dentry on which to get symbolic link | |
5352 | * @buffer: user memory pointer | |
5353 | * @buflen: size of buffer | |
5354 | * | |
5355 | * Does not touch atime. That's up to the caller if necessary | |
5356 | * | |
5357 | * Does not call security hook. | |
5358 | */ | |
5359 | int vfs_readlink(struct dentry *dentry, char __user *buffer, int buflen) | |
5360 | { | |
5361 | struct inode *inode = d_inode(dentry); | |
f2df5da6 AV |
5362 | DEFINE_DELAYED_CALL(done); |
5363 | const char *link; | |
5364 | int res; | |
fd4a0edf | 5365 | |
ea382199 MG |
5366 | if (inode->i_opflags & IOP_CACHED_LINK) |
5367 | return readlink_copy(buffer, buflen, inode->i_link, inode->i_linklen); | |
5368 | ||
76fca90e MS |
5369 | if (unlikely(!(inode->i_opflags & IOP_DEFAULT_READLINK))) { |
5370 | if (unlikely(inode->i_op->readlink)) | |
5371 | return inode->i_op->readlink(dentry, buffer, buflen); | |
5372 | ||
5373 | if (!d_is_symlink(dentry)) | |
5374 | return -EINVAL; | |
5375 | ||
5376 | spin_lock(&inode->i_lock); | |
5377 | inode->i_opflags |= IOP_DEFAULT_READLINK; | |
5378 | spin_unlock(&inode->i_lock); | |
5379 | } | |
fd4a0edf | 5380 | |
4c4f7c19 | 5381 | link = READ_ONCE(inode->i_link); |
f2df5da6 AV |
5382 | if (!link) { |
5383 | link = inode->i_op->get_link(dentry, inode, &done); | |
5384 | if (IS_ERR(link)) | |
5385 | return PTR_ERR(link); | |
5386 | } | |
ea382199 | 5387 | res = readlink_copy(buffer, buflen, link, strlen(link)); |
f2df5da6 AV |
5388 | do_delayed_call(&done); |
5389 | return res; | |
fd4a0edf MS |
5390 | } |
5391 | EXPORT_SYMBOL(vfs_readlink); | |
1da177e4 | 5392 | |
d60874cd MS |
5393 | /** |
5394 | * vfs_get_link - get symlink body | |
5395 | * @dentry: dentry on which to get symbolic link | |
5396 | * @done: caller needs to free returned data with this | |
5397 | * | |
5398 | * Calls security hook and i_op->get_link() on the supplied inode. | |
5399 | * | |
5400 | * It does not touch atime. That's up to the caller if necessary. | |
5401 | * | |
5402 | * Does not work on "special" symlinks like /proc/$$/fd/N | |
5403 | */ | |
5404 | const char *vfs_get_link(struct dentry *dentry, struct delayed_call *done) | |
5405 | { | |
5406 | const char *res = ERR_PTR(-EINVAL); | |
5407 | struct inode *inode = d_inode(dentry); | |
5408 | ||
5409 | if (d_is_symlink(dentry)) { | |
5410 | res = ERR_PTR(security_inode_readlink(dentry)); | |
5411 | if (!res) | |
5412 | res = inode->i_op->get_link(dentry, inode, done); | |
5413 | } | |
5414 | return res; | |
5415 | } | |
5416 | EXPORT_SYMBOL(vfs_get_link); | |
5417 | ||
1da177e4 | 5418 | /* get the link contents into pagecache */ |
b4c173df MS |
5419 | static char *__page_get_link(struct dentry *dentry, struct inode *inode, |
5420 | struct delayed_call *callback) | |
1da177e4 | 5421 | { |
5f152cc0 | 5422 | struct folio *folio; |
6b255391 AV |
5423 | struct address_space *mapping = inode->i_mapping; |
5424 | ||
d3883d4f | 5425 | if (!dentry) { |
5f152cc0 MWO |
5426 | folio = filemap_get_folio(mapping, 0); |
5427 | if (IS_ERR(folio)) | |
d3883d4f | 5428 | return ERR_PTR(-ECHILD); |
5f152cc0 MWO |
5429 | if (!folio_test_uptodate(folio)) { |
5430 | folio_put(folio); | |
d3883d4f AV |
5431 | return ERR_PTR(-ECHILD); |
5432 | } | |
5433 | } else { | |
5f152cc0 MWO |
5434 | folio = read_mapping_folio(mapping, 0, NULL); |
5435 | if (IS_ERR(folio)) | |
5436 | return ERR_CAST(folio); | |
d3883d4f | 5437 | } |
4ec373b7 | 5438 | set_delayed_call(callback, page_put_link, folio); |
21fc61c7 | 5439 | BUG_ON(mapping_gfp_mask(mapping) & __GFP_HIGHMEM); |
5f152cc0 | 5440 | return folio_address(folio); |
b4c173df MS |
5441 | } |
5442 | ||
5443 | const char *page_get_link_raw(struct dentry *dentry, struct inode *inode, | |
5444 | struct delayed_call *callback) | |
5445 | { | |
5446 | return __page_get_link(dentry, inode, callback); | |
5447 | } | |
5448 | EXPORT_SYMBOL_GPL(page_get_link_raw); | |
5449 | ||
4ec373b7 MWO |
5450 | /** |
5451 | * page_get_link() - An implementation of the get_link inode_operation. | |
5452 | * @dentry: The directory entry which is the symlink. | |
5453 | * @inode: The inode for the symlink. | |
5454 | * @callback: Used to drop the reference to the symlink. | |
5455 | * | |
5456 | * Filesystems which store their symlinks in the page cache should use | |
5457 | * this to implement the get_link() member of their inode_operations. | |
5458 | * | |
5459 | * Return: A pointer to the NUL-terminated symlink. | |
5460 | */ | |
b4c173df MS |
5461 | const char *page_get_link(struct dentry *dentry, struct inode *inode, |
5462 | struct delayed_call *callback) | |
5463 | { | |
5464 | char *kaddr = __page_get_link(dentry, inode, callback); | |
5465 | ||
5466 | if (!IS_ERR(kaddr)) | |
5467 | nd_terminate_link(kaddr, inode->i_size, PAGE_SIZE - 1); | |
ebd09abb | 5468 | return kaddr; |
1da177e4 | 5469 | } |
6b255391 | 5470 | EXPORT_SYMBOL(page_get_link); |
1da177e4 | 5471 | |
4ec373b7 MWO |
5472 | /** |
5473 | * page_put_link() - Drop the reference to the symlink. | |
5474 | * @arg: The folio which contains the symlink. | |
5475 | * | |
5476 | * This is used internally by page_get_link(). It is exported for use | |
5477 | * by filesystems which need to implement a variant of page_get_link() | |
5478 | * themselves. Despite the apparent symmetry, filesystems which use | |
5479 | * page_get_link() do not need to call page_put_link(). | |
5480 | * | |
5481 | * The argument, while it has a void pointer type, must be a pointer to | |
5482 | * the folio which was retrieved from the page cache. The delayed_call | |
5483 | * infrastructure is used to drop the reference count once the caller | |
5484 | * is done with the symlink. | |
5485 | */ | |
fceef393 | 5486 | void page_put_link(void *arg) |
1da177e4 | 5487 | { |
4ec373b7 | 5488 | folio_put(arg); |
1da177e4 | 5489 | } |
4d359507 | 5490 | EXPORT_SYMBOL(page_put_link); |
1da177e4 | 5491 | |
aa80deab AV |
5492 | int page_readlink(struct dentry *dentry, char __user *buffer, int buflen) |
5493 | { | |
ea382199 MG |
5494 | const char *link; |
5495 | int res; | |
5496 | ||
fceef393 | 5497 | DEFINE_DELAYED_CALL(done); |
ea382199 MG |
5498 | link = page_get_link(dentry, d_inode(dentry), &done); |
5499 | res = PTR_ERR(link); | |
5500 | if (!IS_ERR(link)) | |
5501 | res = readlink_copy(buffer, buflen, link, strlen(link)); | |
fceef393 | 5502 | do_delayed_call(&done); |
aa80deab AV |
5503 | return res; |
5504 | } | |
5505 | EXPORT_SYMBOL(page_readlink); | |
5506 | ||
56f5746c | 5507 | int page_symlink(struct inode *inode, const char *symname, int len) |
1da177e4 LT |
5508 | { |
5509 | struct address_space *mapping = inode->i_mapping; | |
27a77913 | 5510 | const struct address_space_operations *aops = mapping->a_ops; |
56f5746c | 5511 | bool nofs = !mapping_gfp_constraint(mapping, __GFP_FS); |
1da86618 | 5512 | struct folio *folio; |
1468c6f4 | 5513 | void *fsdata = NULL; |
beb497ab | 5514 | int err; |
2d878178 | 5515 | unsigned int flags; |
1da177e4 | 5516 | |
7e53cac4 | 5517 | retry: |
2d878178 MWO |
5518 | if (nofs) |
5519 | flags = memalloc_nofs_save(); | |
1da86618 | 5520 | err = aops->write_begin(NULL, mapping, 0, len-1, &folio, &fsdata); |
2d878178 MWO |
5521 | if (nofs) |
5522 | memalloc_nofs_restore(flags); | |
1da177e4 | 5523 | if (err) |
afddba49 NP |
5524 | goto fail; |
5525 | ||
1da86618 | 5526 | memcpy(folio_address(folio), symname, len - 1); |
afddba49 | 5527 | |
1da86618 MWO |
5528 | err = aops->write_end(NULL, mapping, 0, len - 1, len - 1, |
5529 | folio, fsdata); | |
1da177e4 LT |
5530 | if (err < 0) |
5531 | goto fail; | |
afddba49 NP |
5532 | if (err < len-1) |
5533 | goto retry; | |
5534 | ||
1da177e4 LT |
5535 | mark_inode_dirty(inode); |
5536 | return 0; | |
1da177e4 LT |
5537 | fail: |
5538 | return err; | |
5539 | } | |
4d359507 | 5540 | EXPORT_SYMBOL(page_symlink); |
0adb25d2 | 5541 | |
92e1d5be | 5542 | const struct inode_operations page_symlink_inode_operations = { |
6b255391 | 5543 | .get_link = page_get_link, |
1da177e4 | 5544 | }; |
1da177e4 | 5545 | EXPORT_SYMBOL(page_symlink_inode_operations); |