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