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