rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * |
| 3 | * Copyright (C) 2011 Novell Inc. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published by |
| 7 | * the Free Software Foundation. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/cred.h> |
| 13 | #include <linux/xattr.h> |
| 14 | #include <linux/posix_acl.h> |
| 15 | #include <linux/ratelimit.h> |
| 16 | #include "overlayfs.h" |
| 17 | #include "ovl_entry.h" |
| 18 | |
| 19 | int ovl_setattr(struct dentry *dentry, struct iattr *attr) |
| 20 | { |
| 21 | int err; |
| 22 | struct dentry *upperdentry; |
| 23 | const struct cred *old_cred; |
| 24 | |
| 25 | /* |
| 26 | * Check for permissions before trying to copy-up. This is redundant |
| 27 | * since it will be rechecked later by ->setattr() on upper dentry. But |
| 28 | * without this, copy-up can be triggered by just about anybody. |
| 29 | * |
| 30 | * We don't initialize inode->size, which just means that |
| 31 | * inode_newsize_ok() will always check against MAX_LFS_FILESIZE and not |
| 32 | * check for a swapfile (which this won't be anyway). |
| 33 | */ |
| 34 | err = setattr_prepare(dentry, attr); |
| 35 | if (err) |
| 36 | return err; |
| 37 | |
| 38 | err = ovl_want_write(dentry); |
| 39 | if (err) |
| 40 | goto out; |
| 41 | |
| 42 | err = ovl_copy_up(dentry); |
| 43 | if (!err) { |
| 44 | upperdentry = ovl_dentry_upper(dentry); |
| 45 | |
| 46 | if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) |
| 47 | attr->ia_valid &= ~ATTR_MODE; |
| 48 | |
| 49 | inode_lock(upperdentry->d_inode); |
| 50 | old_cred = ovl_override_creds(dentry->d_sb); |
| 51 | err = notify_change(upperdentry, attr, NULL); |
| 52 | revert_creds(old_cred); |
| 53 | if (!err) |
| 54 | ovl_copyattr(upperdentry->d_inode, dentry->d_inode); |
| 55 | inode_unlock(upperdentry->d_inode); |
| 56 | } |
| 57 | ovl_drop_write(dentry); |
| 58 | out: |
| 59 | return err; |
| 60 | } |
| 61 | |
| 62 | int ovl_getattr(const struct path *path, struct kstat *stat, |
| 63 | u32 request_mask, unsigned int flags) |
| 64 | { |
| 65 | struct dentry *dentry = path->dentry; |
| 66 | enum ovl_path_type type; |
| 67 | struct path realpath; |
| 68 | const struct cred *old_cred; |
| 69 | bool is_dir = S_ISDIR(dentry->d_inode->i_mode); |
| 70 | int err; |
| 71 | |
| 72 | type = ovl_path_real(dentry, &realpath); |
| 73 | old_cred = ovl_override_creds(dentry->d_sb); |
| 74 | err = vfs_getattr(&realpath, stat, request_mask, flags); |
| 75 | if (err) |
| 76 | goto out; |
| 77 | |
| 78 | /* |
| 79 | * When all layers are on the same fs, all real inode number are |
| 80 | * unique, so we use the overlay st_dev, which is friendly to du -x. |
| 81 | * |
| 82 | * We also use st_ino of the copy up origin, if we know it. |
| 83 | * This guaranties constant st_dev/st_ino across copy up. |
| 84 | * |
| 85 | * If filesystem supports NFS export ops, this also guaranties |
| 86 | * persistent st_ino across mount cycle. |
| 87 | */ |
| 88 | if (ovl_same_sb(dentry->d_sb)) { |
| 89 | if (OVL_TYPE_ORIGIN(type)) { |
| 90 | struct kstat lowerstat; |
| 91 | u32 lowermask = STATX_INO | (!is_dir ? STATX_NLINK : 0); |
| 92 | |
| 93 | ovl_path_lower(dentry, &realpath); |
| 94 | err = vfs_getattr(&realpath, &lowerstat, |
| 95 | lowermask, flags); |
| 96 | if (err) |
| 97 | goto out; |
| 98 | |
| 99 | WARN_ON_ONCE(stat->dev != lowerstat.dev); |
| 100 | /* |
| 101 | * Lower hardlinks may be broken on copy up to different |
| 102 | * upper files, so we cannot use the lower origin st_ino |
| 103 | * for those different files, even for the same fs case. |
| 104 | * With inodes index enabled, it is safe to use st_ino |
| 105 | * of an indexed hardlinked origin. The index validates |
| 106 | * that the upper hardlink is not broken. |
| 107 | */ |
| 108 | if (is_dir || lowerstat.nlink == 1 || |
| 109 | ovl_test_flag(OVL_INDEX, d_inode(dentry))) |
| 110 | stat->ino = lowerstat.ino; |
| 111 | } |
| 112 | stat->dev = dentry->d_sb->s_dev; |
| 113 | } else if (is_dir) { |
| 114 | /* |
| 115 | * If not all layers are on the same fs the pair {real st_ino; |
| 116 | * overlay st_dev} is not unique, so use the non persistent |
| 117 | * overlay st_ino. |
| 118 | * |
| 119 | * Always use the overlay st_dev for directories, so 'find |
| 120 | * -xdev' will scan the entire overlay mount and won't cross the |
| 121 | * overlay mount boundaries. |
| 122 | */ |
| 123 | stat->dev = dentry->d_sb->s_dev; |
| 124 | stat->ino = dentry->d_inode->i_ino; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * It's probably not worth it to count subdirs to get the |
| 129 | * correct link count. nlink=1 seems to pacify 'find' and |
| 130 | * other utilities. |
| 131 | */ |
| 132 | if (is_dir && OVL_TYPE_MERGE(type)) |
| 133 | stat->nlink = 1; |
| 134 | |
| 135 | /* |
| 136 | * Return the overlay inode nlinks for indexed upper inodes. |
| 137 | * Overlay inode nlink counts the union of the upper hardlinks |
| 138 | * and non-covered lower hardlinks. It does not include the upper |
| 139 | * index hardlink. |
| 140 | */ |
| 141 | if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry))) |
| 142 | stat->nlink = dentry->d_inode->i_nlink; |
| 143 | |
| 144 | out: |
| 145 | revert_creds(old_cred); |
| 146 | |
| 147 | return err; |
| 148 | } |
| 149 | |
| 150 | int ovl_permission(struct inode *inode, int mask) |
| 151 | { |
| 152 | struct inode *upperinode = ovl_inode_upper(inode); |
| 153 | struct inode *realinode = upperinode ?: ovl_inode_lower(inode); |
| 154 | const struct cred *old_cred; |
| 155 | int err; |
| 156 | |
| 157 | /* Careful in RCU walk mode */ |
| 158 | if (!realinode) { |
| 159 | WARN_ON(!(mask & MAY_NOT_BLOCK)); |
| 160 | return -ECHILD; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Check overlay inode with the creds of task and underlying inode |
| 165 | * with creds of mounter |
| 166 | */ |
| 167 | err = generic_permission(inode, mask); |
| 168 | if (err) |
| 169 | return err; |
| 170 | |
| 171 | old_cred = ovl_override_creds(inode->i_sb); |
| 172 | if (!upperinode && |
| 173 | !special_file(realinode->i_mode) && mask & MAY_WRITE) { |
| 174 | mask &= ~(MAY_WRITE | MAY_APPEND); |
| 175 | /* Make sure mounter can read file for copy up later */ |
| 176 | mask |= MAY_READ; |
| 177 | } |
| 178 | err = inode_permission(realinode, mask); |
| 179 | revert_creds(old_cred); |
| 180 | |
| 181 | return err; |
| 182 | } |
| 183 | |
| 184 | static const char *ovl_get_link(struct dentry *dentry, |
| 185 | struct inode *inode, |
| 186 | struct delayed_call *done) |
| 187 | { |
| 188 | const struct cred *old_cred; |
| 189 | const char *p; |
| 190 | |
| 191 | if (!dentry) |
| 192 | return ERR_PTR(-ECHILD); |
| 193 | |
| 194 | old_cred = ovl_override_creds(dentry->d_sb); |
| 195 | p = vfs_get_link(ovl_dentry_real(dentry), done); |
| 196 | revert_creds(old_cred); |
| 197 | return p; |
| 198 | } |
| 199 | |
| 200 | bool ovl_is_private_xattr(const char *name) |
| 201 | { |
| 202 | return strncmp(name, OVL_XATTR_PREFIX, |
| 203 | sizeof(OVL_XATTR_PREFIX) - 1) == 0; |
| 204 | } |
| 205 | |
| 206 | int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name, |
| 207 | const void *value, size_t size, int flags) |
| 208 | { |
| 209 | int err; |
| 210 | struct dentry *upperdentry = ovl_i_dentry_upper(inode); |
| 211 | struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry); |
| 212 | const struct cred *old_cred; |
| 213 | |
| 214 | err = ovl_want_write(dentry); |
| 215 | if (err) |
| 216 | goto out; |
| 217 | |
| 218 | if (!value && !upperdentry) { |
| 219 | err = vfs_getxattr(realdentry, name, NULL, 0); |
| 220 | if (err < 0) |
| 221 | goto out_drop_write; |
| 222 | } |
| 223 | |
| 224 | if (!upperdentry) { |
| 225 | err = ovl_copy_up(dentry); |
| 226 | if (err) |
| 227 | goto out_drop_write; |
| 228 | |
| 229 | realdentry = ovl_dentry_upper(dentry); |
| 230 | } |
| 231 | |
| 232 | old_cred = ovl_override_creds(dentry->d_sb); |
| 233 | if (value) |
| 234 | err = vfs_setxattr(realdentry, name, value, size, flags); |
| 235 | else { |
| 236 | WARN_ON(flags != XATTR_REPLACE); |
| 237 | err = vfs_removexattr(realdentry, name); |
| 238 | } |
| 239 | revert_creds(old_cred); |
| 240 | |
| 241 | out_drop_write: |
| 242 | ovl_drop_write(dentry); |
| 243 | out: |
| 244 | return err; |
| 245 | } |
| 246 | |
| 247 | int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name, |
| 248 | void *value, size_t size) |
| 249 | { |
| 250 | ssize_t res; |
| 251 | const struct cred *old_cred; |
| 252 | struct dentry *realdentry = |
| 253 | ovl_i_dentry_upper(inode) ?: ovl_dentry_lower(dentry); |
| 254 | |
| 255 | old_cred = ovl_override_creds(dentry->d_sb); |
| 256 | res = vfs_getxattr(realdentry, name, value, size); |
| 257 | revert_creds(old_cred); |
| 258 | return res; |
| 259 | } |
| 260 | |
| 261 | static bool ovl_can_list(const char *s) |
| 262 | { |
| 263 | /* List all non-trusted xatts */ |
| 264 | if (strncmp(s, XATTR_TRUSTED_PREFIX, XATTR_TRUSTED_PREFIX_LEN) != 0) |
| 265 | return true; |
| 266 | |
| 267 | /* Never list trusted.overlay, list other trusted for superuser only */ |
| 268 | return !ovl_is_private_xattr(s) && |
| 269 | ns_capable_noaudit(&init_user_ns, CAP_SYS_ADMIN); |
| 270 | } |
| 271 | |
| 272 | ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size) |
| 273 | { |
| 274 | struct dentry *realdentry = ovl_dentry_real(dentry); |
| 275 | ssize_t res; |
| 276 | size_t len; |
| 277 | char *s; |
| 278 | const struct cred *old_cred; |
| 279 | |
| 280 | old_cred = ovl_override_creds(dentry->d_sb); |
| 281 | res = vfs_listxattr(realdentry, list, size); |
| 282 | revert_creds(old_cred); |
| 283 | if (res <= 0 || size == 0) |
| 284 | return res; |
| 285 | |
| 286 | /* filter out private xattrs */ |
| 287 | for (s = list, len = res; len;) { |
| 288 | size_t slen = strnlen(s, len) + 1; |
| 289 | |
| 290 | /* underlying fs providing us with an broken xattr list? */ |
| 291 | if (WARN_ON(slen > len)) |
| 292 | return -EIO; |
| 293 | |
| 294 | len -= slen; |
| 295 | if (!ovl_can_list(s)) { |
| 296 | res -= slen; |
| 297 | memmove(s, s + slen, len); |
| 298 | } else { |
| 299 | s += slen; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | return res; |
| 304 | } |
| 305 | |
| 306 | struct posix_acl *ovl_get_acl(struct inode *inode, int type) |
| 307 | { |
| 308 | struct inode *realinode = ovl_inode_real(inode); |
| 309 | const struct cred *old_cred; |
| 310 | struct posix_acl *acl; |
| 311 | |
| 312 | if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !IS_POSIXACL(realinode)) |
| 313 | return NULL; |
| 314 | |
| 315 | old_cred = ovl_override_creds(inode->i_sb); |
| 316 | acl = get_acl(realinode, type); |
| 317 | revert_creds(old_cred); |
| 318 | |
| 319 | return acl; |
| 320 | } |
| 321 | |
| 322 | static bool ovl_open_need_copy_up(struct dentry *dentry, int flags) |
| 323 | { |
| 324 | if (ovl_dentry_upper(dentry) && |
| 325 | ovl_dentry_has_upper_alias(dentry)) |
| 326 | return false; |
| 327 | |
| 328 | if (special_file(d_inode(dentry)->i_mode)) |
| 329 | return false; |
| 330 | |
| 331 | if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC)) |
| 332 | return false; |
| 333 | |
| 334 | return true; |
| 335 | } |
| 336 | |
| 337 | int ovl_open_maybe_copy_up(struct dentry *dentry, unsigned int file_flags) |
| 338 | { |
| 339 | int err = 0; |
| 340 | |
| 341 | if (ovl_open_need_copy_up(dentry, file_flags)) { |
| 342 | err = ovl_want_write(dentry); |
| 343 | if (!err) { |
| 344 | err = ovl_copy_up_flags(dentry, file_flags); |
| 345 | ovl_drop_write(dentry); |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | return err; |
| 350 | } |
| 351 | |
| 352 | int ovl_update_time(struct inode *inode, struct timespec *ts, int flags) |
| 353 | { |
| 354 | struct dentry *alias; |
| 355 | struct path upperpath; |
| 356 | |
| 357 | if (!(flags & S_ATIME)) |
| 358 | return 0; |
| 359 | |
| 360 | alias = d_find_any_alias(inode); |
| 361 | if (!alias) |
| 362 | return 0; |
| 363 | |
| 364 | ovl_path_upper(alias, &upperpath); |
| 365 | if (upperpath.dentry) { |
| 366 | touch_atime(&upperpath); |
| 367 | inode->i_atime = d_inode(upperpath.dentry)->i_atime; |
| 368 | } |
| 369 | |
| 370 | dput(alias); |
| 371 | |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | static const struct inode_operations ovl_file_inode_operations = { |
| 376 | .setattr = ovl_setattr, |
| 377 | .permission = ovl_permission, |
| 378 | .getattr = ovl_getattr, |
| 379 | .listxattr = ovl_listxattr, |
| 380 | .get_acl = ovl_get_acl, |
| 381 | .update_time = ovl_update_time, |
| 382 | }; |
| 383 | |
| 384 | static const struct inode_operations ovl_symlink_inode_operations = { |
| 385 | .setattr = ovl_setattr, |
| 386 | .get_link = ovl_get_link, |
| 387 | .getattr = ovl_getattr, |
| 388 | .listxattr = ovl_listxattr, |
| 389 | .update_time = ovl_update_time, |
| 390 | }; |
| 391 | |
| 392 | /* |
| 393 | * It is possible to stack overlayfs instance on top of another |
| 394 | * overlayfs instance as lower layer. We need to annonate the |
| 395 | * stackable i_mutex locks according to stack level of the super |
| 396 | * block instance. An overlayfs instance can never be in stack |
| 397 | * depth 0 (there is always a real fs below it). An overlayfs |
| 398 | * inode lock will use the lockdep annotaion ovl_i_mutex_key[depth]. |
| 399 | * |
| 400 | * For example, here is a snip from /proc/lockdep_chains after |
| 401 | * dir_iterate of nested overlayfs: |
| 402 | * |
| 403 | * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2) |
| 404 | * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1) |
| 405 | * [...] &type->i_mutex_dir_key (stack_depth=0) |
| 406 | */ |
| 407 | #define OVL_MAX_NESTING FILESYSTEM_MAX_STACK_DEPTH |
| 408 | |
| 409 | static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode) |
| 410 | { |
| 411 | #ifdef CONFIG_LOCKDEP |
| 412 | static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING]; |
| 413 | static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING]; |
| 414 | |
| 415 | int depth = inode->i_sb->s_stack_depth - 1; |
| 416 | |
| 417 | if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING)) |
| 418 | depth = 0; |
| 419 | |
| 420 | if (S_ISDIR(inode->i_mode)) |
| 421 | lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]); |
| 422 | else |
| 423 | lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]); |
| 424 | #endif |
| 425 | } |
| 426 | |
| 427 | static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev) |
| 428 | { |
| 429 | inode->i_ino = get_next_ino(); |
| 430 | inode->i_mode = mode; |
| 431 | inode->i_flags |= S_NOCMTIME; |
| 432 | #ifdef CONFIG_FS_POSIX_ACL |
| 433 | inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE; |
| 434 | #endif |
| 435 | |
| 436 | ovl_lockdep_annotate_inode_mutex_key(inode); |
| 437 | |
| 438 | switch (mode & S_IFMT) { |
| 439 | case S_IFREG: |
| 440 | inode->i_op = &ovl_file_inode_operations; |
| 441 | break; |
| 442 | |
| 443 | case S_IFDIR: |
| 444 | inode->i_op = &ovl_dir_inode_operations; |
| 445 | inode->i_fop = &ovl_dir_operations; |
| 446 | break; |
| 447 | |
| 448 | case S_IFLNK: |
| 449 | inode->i_op = &ovl_symlink_inode_operations; |
| 450 | break; |
| 451 | |
| 452 | default: |
| 453 | inode->i_op = &ovl_file_inode_operations; |
| 454 | init_special_inode(inode, mode, rdev); |
| 455 | break; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | /* |
| 460 | * With inodes index enabled, an overlay inode nlink counts the union of upper |
| 461 | * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure |
| 462 | * upper inode, the following nlink modifying operations can happen: |
| 463 | * |
| 464 | * 1. Lower hardlink copy up |
| 465 | * 2. Upper hardlink created, unlinked or renamed over |
| 466 | * 3. Lower hardlink whiteout or renamed over |
| 467 | * |
| 468 | * For the first, copy up case, the union nlink does not change, whether the |
| 469 | * operation succeeds or fails, but the upper inode nlink may change. |
| 470 | * Therefore, before copy up, we store the union nlink value relative to the |
| 471 | * lower inode nlink in the index inode xattr trusted.overlay.nlink. |
| 472 | * |
| 473 | * For the second, upper hardlink case, the union nlink should be incremented |
| 474 | * or decremented IFF the operation succeeds, aligned with nlink change of the |
| 475 | * upper inode. Therefore, before link/unlink/rename, we store the union nlink |
| 476 | * value relative to the upper inode nlink in the index inode. |
| 477 | * |
| 478 | * For the last, lower cover up case, we simplify things by preceding the |
| 479 | * whiteout or cover up with copy up. This makes sure that there is an index |
| 480 | * upper inode where the nlink xattr can be stored before the copied up upper |
| 481 | * entry is unlink. |
| 482 | */ |
| 483 | #define OVL_NLINK_ADD_UPPER (1 << 0) |
| 484 | |
| 485 | /* |
| 486 | * On-disk format for indexed nlink: |
| 487 | * |
| 488 | * nlink relative to the upper inode - "U[+-]NUM" |
| 489 | * nlink relative to the lower inode - "L[+-]NUM" |
| 490 | */ |
| 491 | |
| 492 | static int ovl_set_nlink_common(struct dentry *dentry, |
| 493 | struct dentry *realdentry, const char *format) |
| 494 | { |
| 495 | struct inode *inode = d_inode(dentry); |
| 496 | struct inode *realinode = d_inode(realdentry); |
| 497 | char buf[13]; |
| 498 | int len; |
| 499 | |
| 500 | len = snprintf(buf, sizeof(buf), format, |
| 501 | (int) (inode->i_nlink - realinode->i_nlink)); |
| 502 | |
| 503 | if (WARN_ON(len >= sizeof(buf))) |
| 504 | return -EIO; |
| 505 | |
| 506 | return ovl_do_setxattr(ovl_dentry_upper(dentry), |
| 507 | OVL_XATTR_NLINK, buf, len, 0); |
| 508 | } |
| 509 | |
| 510 | int ovl_set_nlink_upper(struct dentry *dentry) |
| 511 | { |
| 512 | return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i"); |
| 513 | } |
| 514 | |
| 515 | int ovl_set_nlink_lower(struct dentry *dentry) |
| 516 | { |
| 517 | return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i"); |
| 518 | } |
| 519 | |
| 520 | unsigned int ovl_get_nlink(struct dentry *lowerdentry, |
| 521 | struct dentry *upperdentry, |
| 522 | unsigned int fallback) |
| 523 | { |
| 524 | int nlink_diff; |
| 525 | int nlink; |
| 526 | char buf[13]; |
| 527 | int err; |
| 528 | |
| 529 | if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1) |
| 530 | return fallback; |
| 531 | |
| 532 | err = vfs_getxattr(upperdentry, OVL_XATTR_NLINK, &buf, sizeof(buf) - 1); |
| 533 | if (err < 0) |
| 534 | goto fail; |
| 535 | |
| 536 | buf[err] = '\0'; |
| 537 | if ((buf[0] != 'L' && buf[0] != 'U') || |
| 538 | (buf[1] != '+' && buf[1] != '-')) |
| 539 | goto fail; |
| 540 | |
| 541 | err = kstrtoint(buf + 1, 10, &nlink_diff); |
| 542 | if (err < 0) |
| 543 | goto fail; |
| 544 | |
| 545 | nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink; |
| 546 | nlink += nlink_diff; |
| 547 | |
| 548 | if (nlink <= 0) |
| 549 | goto fail; |
| 550 | |
| 551 | return nlink; |
| 552 | |
| 553 | fail: |
| 554 | pr_warn_ratelimited("overlayfs: failed to get index nlink (%pd2, err=%i)\n", |
| 555 | upperdentry, err); |
| 556 | return fallback; |
| 557 | } |
| 558 | |
| 559 | struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev) |
| 560 | { |
| 561 | struct inode *inode; |
| 562 | |
| 563 | inode = new_inode(sb); |
| 564 | if (inode) |
| 565 | ovl_fill_inode(inode, mode, rdev); |
| 566 | |
| 567 | return inode; |
| 568 | } |
| 569 | |
| 570 | static int ovl_inode_test(struct inode *inode, void *data) |
| 571 | { |
| 572 | return inode->i_private == data; |
| 573 | } |
| 574 | |
| 575 | static int ovl_inode_set(struct inode *inode, void *data) |
| 576 | { |
| 577 | inode->i_private = data; |
| 578 | return 0; |
| 579 | } |
| 580 | |
| 581 | static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry, |
| 582 | struct dentry *upperdentry) |
| 583 | { |
| 584 | if (S_ISDIR(inode->i_mode)) { |
| 585 | /* Real lower dir moved to upper layer under us? */ |
| 586 | if (!lowerdentry && ovl_inode_lower(inode)) |
| 587 | return false; |
| 588 | |
| 589 | /* Lookup of an uncovered redirect origin? */ |
| 590 | if (!upperdentry && ovl_inode_upper(inode)) |
| 591 | return false; |
| 592 | } |
| 593 | |
| 594 | /* |
| 595 | * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL. |
| 596 | * This happens when finding a copied up overlay inode for a renamed |
| 597 | * or hardlinked overlay dentry and lower dentry cannot be followed |
| 598 | * by origin because lower fs does not support file handles. |
| 599 | */ |
| 600 | if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry)) |
| 601 | return false; |
| 602 | |
| 603 | /* |
| 604 | * Allow non-NULL __upperdentry in inode even if upperdentry is NULL. |
| 605 | * This happens when finding a lower alias for a copied up hard link. |
| 606 | */ |
| 607 | if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry)) |
| 608 | return false; |
| 609 | |
| 610 | return true; |
| 611 | } |
| 612 | |
| 613 | /* |
| 614 | * Does overlay inode need to be hashed by lower inode? |
| 615 | */ |
| 616 | static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper, |
| 617 | struct dentry *lower, struct dentry *index) |
| 618 | { |
| 619 | struct ovl_fs *ofs = sb->s_fs_info; |
| 620 | |
| 621 | /* No, if pure upper */ |
| 622 | if (!lower) |
| 623 | return false; |
| 624 | |
| 625 | /* Yes, if already indexed */ |
| 626 | if (index) |
| 627 | return true; |
| 628 | |
| 629 | /* Yes, if won't be copied up */ |
| 630 | if (!ofs->upper_mnt) |
| 631 | return true; |
| 632 | |
| 633 | /* No, if lower hardlink is or will be broken on copy up */ |
| 634 | if ((upper || !ovl_indexdir(sb)) && |
| 635 | !d_is_dir(lower) && d_inode(lower)->i_nlink > 1) |
| 636 | return false; |
| 637 | |
| 638 | /* No, if non-indexed upper with NFS export */ |
| 639 | if (sb->s_export_op && upper) |
| 640 | return false; |
| 641 | |
| 642 | /* Otherwise, hash by lower inode for fsnotify */ |
| 643 | return true; |
| 644 | } |
| 645 | |
| 646 | struct inode *ovl_get_inode(struct dentry *dentry, struct dentry *upperdentry, |
| 647 | struct dentry *index) |
| 648 | { |
| 649 | struct super_block *sb = dentry->d_sb; |
| 650 | struct dentry *lowerdentry = ovl_dentry_lower(dentry); |
| 651 | struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL; |
| 652 | struct inode *inode; |
| 653 | bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry, index); |
| 654 | bool is_dir; |
| 655 | |
| 656 | if (!realinode) |
| 657 | realinode = d_inode(lowerdentry); |
| 658 | |
| 659 | /* |
| 660 | * Copy up origin (lower) may exist for non-indexed upper, but we must |
| 661 | * not use lower as hash key if this is a broken hardlink. |
| 662 | */ |
| 663 | is_dir = S_ISDIR(realinode->i_mode); |
| 664 | if (upperdentry || bylower) { |
| 665 | struct inode *key = d_inode(bylower ? lowerdentry : |
| 666 | upperdentry); |
| 667 | unsigned int nlink = is_dir ? 1 : realinode->i_nlink; |
| 668 | |
| 669 | inode = iget5_locked(sb, (unsigned long) key, |
| 670 | ovl_inode_test, ovl_inode_set, key); |
| 671 | if (!inode) |
| 672 | goto out_nomem; |
| 673 | if (!(inode->i_state & I_NEW)) { |
| 674 | /* |
| 675 | * Verify that the underlying files stored in the inode |
| 676 | * match those in the dentry. |
| 677 | */ |
| 678 | if (!ovl_verify_inode(inode, lowerdentry, upperdentry)) { |
| 679 | iput(inode); |
| 680 | inode = ERR_PTR(-ESTALE); |
| 681 | goto out; |
| 682 | } |
| 683 | |
| 684 | dput(upperdentry); |
| 685 | goto out; |
| 686 | } |
| 687 | |
| 688 | /* Recalculate nlink for non-dir due to indexing */ |
| 689 | if (!is_dir) |
| 690 | nlink = ovl_get_nlink(lowerdentry, upperdentry, nlink); |
| 691 | set_nlink(inode, nlink); |
| 692 | } else { |
| 693 | /* Lower hardlink that will be broken on copy up */ |
| 694 | inode = new_inode(sb); |
| 695 | if (!inode) |
| 696 | goto out_nomem; |
| 697 | } |
| 698 | ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev); |
| 699 | ovl_inode_init(inode, upperdentry, lowerdentry); |
| 700 | |
| 701 | if (upperdentry && ovl_is_impuredir(upperdentry)) |
| 702 | ovl_set_flag(OVL_IMPURE, inode); |
| 703 | |
| 704 | if (inode->i_state & I_NEW) |
| 705 | unlock_new_inode(inode); |
| 706 | out: |
| 707 | return inode; |
| 708 | |
| 709 | out_nomem: |
| 710 | inode = ERR_PTR(-ENOMEM); |
| 711 | goto out; |
| 712 | } |