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