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