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