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