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