| 1 | |
| 2 | |
| 3 | *
|
| 4 | * Copyright (C) 2011 Novell Inc.
|
| 5 |
|
| 6 |
|
| 7 | #include <linux/fs.h>
|
| 8 | #include <linux/slab.h>
|
| 9 | #include <linux/cred.h>
|
| 10 | #include <linux/xattr.h>
|
| 11 | #include <linux/ratelimit.h>
|
| 12 | #include <linux/fiemap.h>
|
| 13 | #include <linux/fileattr.h>
|
| 14 | #include <linux/security.h>
|
| 15 | #include <linux/namei.h>
|
| 16 | #include <linux/posix_acl.h>
|
| 17 | #include <linux/posix_acl_xattr.h>
|
| 18 | #include "overlayfs.h"
|
| 19 |
|
| 20 |
|
| 21 | int ovl_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,
|
| 22 | struct iattr *attr)
|
| 23 | {
|
| 24 | int err;
|
| 25 | struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
|
| 26 | bool full_copy_up = false;
|
| 27 | struct dentry *upperdentry;
|
| 28 |
|
| 29 | err = setattr_prepare(idmap, dentry, attr);
|
| 30 | if (err)
|
| 31 | return err;
|
| 32 |
|
| 33 |
|
| 34 | if (attr->ia_valid & ATTR_UID)
|
| 35 | attr->ia_vfsuid = VFSUIDT_INIT(from_vfsuid(idmap,
|
| 36 | i_user_ns(d_inode(dentry)), attr->ia_vfsuid));
|
| 37 | if (attr->ia_valid & ATTR_GID)
|
| 38 | attr->ia_vfsgid = VFSGIDT_INIT(from_vfsgid(idmap,
|
| 39 | i_user_ns(d_inode(dentry)), attr->ia_vfsgid));
|
| 40 |
|
| 41 | if (attr->ia_valid & ATTR_SIZE) {
|
| 42 |
|
| 43 | full_copy_up = true;
|
| 44 | }
|
| 45 |
|
| 46 | if (!full_copy_up)
|
| 47 | err = ovl_copy_up(dentry);
|
| 48 | else
|
| 49 | err = ovl_copy_up_with_data(dentry);
|
| 50 | if (!err) {
|
| 51 | struct inode *winode = NULL;
|
| 52 |
|
| 53 | upperdentry = ovl_dentry_upper(dentry);
|
| 54 |
|
| 55 | if (attr->ia_valid & ATTR_SIZE) {
|
| 56 | winode = d_inode(upperdentry);
|
| 57 | err = get_write_access(winode);
|
| 58 | if (err)
|
| 59 | goto out;
|
| 60 | }
|
| 61 |
|
| 62 | if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
|
| 63 | attr->ia_valid &= ~ATTR_MODE;
|
| 64 |
|
| 65 | |
| 66 | * We might have to translate ovl file into real file object
|
| 67 | * once use cases emerge. For now, simply don't let underlying
|
| 68 | * filesystem rely on attr->ia_file
|
| 69 |
|
| 70 | attr->ia_valid &= ~ATTR_FILE;
|
| 71 |
|
| 72 | |
| 73 | * If open(O_TRUNC) is done, VFS calls ->setattr with ATTR_OPEN
|
| 74 | * set. Overlayfs does not pass O_TRUNC flag to underlying
|
| 75 | * filesystem during open -> do not pass ATTR_OPEN. This
|
| 76 | * disables optimization in fuse which assumes open(O_TRUNC)
|
| 77 | * already set file size to 0. But we never passed O_TRUNC to
|
| 78 | * fuse. So by clearing ATTR_OPEN, fuse will be forced to send
|
| 79 | * setattr request to server.
|
| 80 |
|
| 81 | attr->ia_valid &= ~ATTR_OPEN;
|
| 82 |
|
| 83 | err = ovl_want_write(dentry);
|
| 84 | if (err)
|
| 85 | goto out_put_write;
|
| 86 |
|
| 87 | inode_lock(upperdentry->d_inode);
|
| 88 | with_ovl_creds(dentry->d_sb)
|
| 89 | err = ovl_do_notify_change(ofs, upperdentry, attr);
|
| 90 | if (!err)
|
| 91 | ovl_copyattr(dentry->d_inode);
|
| 92 | inode_unlock(upperdentry->d_inode);
|
| 93 | ovl_drop_write(dentry);
|
| 94 |
|
| 95 | out_put_write:
|
| 96 | if (winode)
|
| 97 | put_write_access(winode);
|
| 98 | }
|
| 99 | out:
|
| 100 | return err;
|
| 101 | }
|
| 102 |
|
| 103 | static void ovl_map_dev_ino(struct dentry *dentry, struct kstat *stat, int fsid)
|
| 104 | {
|
| 105 | struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
|
| 106 | bool samefs = ovl_same_fs(ofs);
|
| 107 | unsigned int xinobits = ovl_xino_bits(ofs);
|
| 108 | unsigned int xinoshift = 64 - xinobits;
|
| 109 |
|
| 110 | if (samefs) {
|
| 111 | |
| 112 | * When all layers are on the same fs, all real inode
|
| 113 | * number are unique, so we use the overlay st_dev,
|
| 114 | * which is friendly to du -x.
|
| 115 |
|
| 116 | stat->dev = dentry->d_sb->s_dev;
|
| 117 | return;
|
| 118 | } else if (xinobits) {
|
| 119 | |
| 120 | * All inode numbers of underlying fs should not be using the
|
| 121 | * high xinobits, so we use high xinobits to partition the
|
| 122 | * overlay st_ino address space. The high bits holds the fsid
|
| 123 | * (upper fsid is 0). The lowest xinobit is reserved for mapping
|
| 124 | * the non-persistent inode numbers range in case of overflow.
|
| 125 | * This way all overlay inode numbers are unique and use the
|
| 126 | * overlay st_dev.
|
| 127 |
|
| 128 | if (likely(!(stat->ino >> xinoshift))) {
|
| 129 | stat->ino |= ((u64)fsid) << (xinoshift + 1);
|
| 130 | stat->dev = dentry->d_sb->s_dev;
|
| 131 | return;
|
| 132 | } else if (ovl_xino_warn(ofs)) {
|
| 133 | pr_warn_ratelimited("inode number too big (%pd2, ino=%llu, xinobits=%d)\n",
|
| 134 | dentry, stat->ino, xinobits);
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 |
|
| 139 | if (S_ISDIR(dentry->d_inode->i_mode)) {
|
| 140 | |
| 141 | * Always use the overlay st_dev for directories, so 'find
|
| 142 | * -xdev' will scan the entire overlay mount and won't cross the
|
| 143 | * overlay mount boundaries.
|
| 144 | *
|
| 145 | * If not all layers are on the same fs the pair {real st_ino;
|
| 146 | * overlay st_dev} is not unique, so use the non persistent
|
| 147 | * overlay st_ino for directories.
|
| 148 |
|
| 149 | stat->dev = dentry->d_sb->s_dev;
|
| 150 | stat->ino = dentry->d_inode->i_ino;
|
| 151 | } else {
|
| 152 | |
| 153 | * For non-samefs setup, if we cannot map all layers st_ino
|
| 154 | * to a unified address space, we need to make sure that st_dev
|
| 155 | * is unique per underlying fs, so we use the unique anonymous
|
| 156 | * bdev assigned to the underlying fs.
|
| 157 |
|
| 158 | stat->dev = ofs->fs[fsid].pseudo_dev;
|
| 159 | }
|
| 160 | }
|
| 161 |
|
| 162 | static inline int ovl_real_getattr_nosec(struct super_block *sb,
|
| 163 | const struct path *path,
|
| 164 | struct kstat *stat, u32 request_mask,
|
| 165 | unsigned int flags)
|
| 166 | {
|
| 167 | with_ovl_creds(sb)
|
| 168 | return vfs_getattr_nosec(path, stat, request_mask, flags);
|
| 169 | }
|
| 170 |
|
| 171 | int ovl_getattr(const struct mnt_idmap *idmap, const struct path *path,
|
| 172 | struct kstat *stat, u32 request_mask, unsigned int flags)
|
| 173 | {
|
| 174 | struct dentry *dentry = path->dentry;
|
| 175 | struct super_block *sb = dentry->d_sb;
|
| 176 | enum ovl_path_type type;
|
| 177 | struct path realpath;
|
| 178 | struct inode *inode = d_inode(dentry);
|
| 179 | bool is_dir = S_ISDIR(inode->i_mode);
|
| 180 | int fsid = 0;
|
| 181 | int err;
|
| 182 | bool metacopy_blocks = false;
|
| 183 | vfsuid_t vfsuid;
|
| 184 | vfsgid_t vfsgid;
|
| 185 |
|
| 186 | metacopy_blocks = ovl_is_metacopy_dentry(dentry);
|
| 187 |
|
| 188 | type = ovl_path_real(dentry, &realpath);
|
| 189 | err = ovl_real_getattr_nosec(sb, &realpath, stat, request_mask, flags);
|
| 190 | if (err)
|
| 191 | return err;
|
| 192 |
|
| 193 |
|
| 194 | generic_fill_statx_attr(inode, stat);
|
| 195 |
|
| 196 | |
| 197 | * For non-dir or same fs, we use st_ino of the copy up origin.
|
| 198 | * This guaranties constant st_dev/st_ino across copy up.
|
| 199 | * With xino feature and non-samefs, we use st_ino of the copy up
|
| 200 | * origin masked with high bits that represent the layer id.
|
| 201 | *
|
| 202 | * If lower filesystem supports NFS file handles, this also guaranties
|
| 203 | * persistent st_ino across mount cycle.
|
| 204 |
|
| 205 | if (!is_dir || ovl_same_dev(OVL_FS(dentry->d_sb))) {
|
| 206 | if (!OVL_TYPE_UPPER(type)) {
|
| 207 | fsid = ovl_layer_lower(dentry)->fsid;
|
| 208 | } else if (OVL_TYPE_ORIGIN(type)) {
|
| 209 | struct kstat lowerstat;
|
| 210 | u32 lowermask = STATX_INO | STATX_BLOCKS |
|
| 211 | (!is_dir ? STATX_NLINK : 0);
|
| 212 |
|
| 213 | ovl_path_lower(dentry, &realpath);
|
| 214 | err = ovl_real_getattr_nosec(sb, &realpath, &lowerstat, lowermask, flags);
|
| 215 | if (err)
|
| 216 | return err;
|
| 217 |
|
| 218 | |
| 219 | * Lower hardlinks may be broken on copy up to different
|
| 220 | * upper files, so we cannot use the lower origin st_ino
|
| 221 | * for those different files, even for the same fs case.
|
| 222 | *
|
| 223 | * Similarly, several redirected dirs can point to the
|
| 224 | * same dir on a lower layer. With the "verify_lower"
|
| 225 | * feature, we do not use the lower origin st_ino, if
|
| 226 | * we haven't verified that this redirect is unique.
|
| 227 | *
|
| 228 | * With inodes index enabled, it is safe to use st_ino
|
| 229 | * of an indexed origin. The index validates that the
|
| 230 | * upper hardlink is not broken and that a redirected
|
| 231 | * dir is the only redirect to that origin.
|
| 232 |
|
| 233 | if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) ||
|
| 234 | (!ovl_verify_lower(dentry->d_sb) &&
|
| 235 | (is_dir || lowerstat.nlink == 1))) {
|
| 236 | fsid = ovl_layer_lower(dentry)->fsid;
|
| 237 | stat->ino = lowerstat.ino;
|
| 238 | }
|
| 239 |
|
| 240 | |
| 241 | * If we are querying a metacopy dentry and lower
|
| 242 | * dentry is data dentry, then use the blocks we
|
| 243 | * queried just now. We don't have to do additional
|
| 244 | * vfs_getattr(). If lower itself is metacopy, then
|
| 245 | * additional vfs_getattr() is unavoidable.
|
| 246 |
|
| 247 | if (metacopy_blocks &&
|
| 248 | realpath.dentry == ovl_dentry_lowerdata(dentry)) {
|
| 249 | stat->blocks = lowerstat.blocks;
|
| 250 | metacopy_blocks = false;
|
| 251 | }
|
| 252 | }
|
| 253 |
|
| 254 | if (metacopy_blocks) {
|
| 255 | |
| 256 | * If lower is not same as lowerdata or if there was
|
| 257 | * no origin on upper, we can end up here.
|
| 258 | * With lazy lowerdata lookup, guess lowerdata blocks
|
| 259 | * from size to avoid lowerdata lookup on stat(2).
|
| 260 |
|
| 261 | struct kstat lowerdatastat;
|
| 262 | u32 lowermask = STATX_BLOCKS;
|
| 263 |
|
| 264 | ovl_path_lowerdata(dentry, &realpath);
|
| 265 | if (realpath.dentry) {
|
| 266 | err = ovl_real_getattr_nosec(sb, &realpath, &lowerdatastat,
|
| 267 | lowermask, flags);
|
| 268 | if (err)
|
| 269 | return err;
|
| 270 | } else {
|
| 271 | lowerdatastat.blocks =
|
| 272 | round_up(stat->size, stat->blksize) >> 9;
|
| 273 | }
|
| 274 | stat->blocks = lowerdatastat.blocks;
|
| 275 | }
|
| 276 | }
|
| 277 |
|
| 278 | ovl_map_dev_ino(dentry, stat, fsid);
|
| 279 |
|
| 280 | |
| 281 | * It's probably not worth it to count subdirs to get the
|
| 282 | * correct link count. nlink=1 seems to pacify 'find' and
|
| 283 | * other utilities.
|
| 284 |
|
| 285 | if (is_dir && OVL_TYPE_MERGE(type))
|
| 286 | stat->nlink = 1;
|
| 287 |
|
| 288 | |
| 289 | * Return the overlay inode nlinks for indexed upper inodes.
|
| 290 | * Overlay inode nlink counts the union of the upper hardlinks
|
| 291 | * and non-covered lower hardlinks. It does not include the upper
|
| 292 | * index hardlink.
|
| 293 |
|
| 294 | if (!is_dir && ovl_test_flag(OVL_INDEX, d_inode(dentry)))
|
| 295 | stat->nlink = dentry->d_inode->i_nlink;
|
| 296 |
|
| 297 |
|
| 298 | vfsuid = make_vfsuid(idmap, i_user_ns(inode), stat->uid);
|
| 299 | vfsgid = make_vfsgid(idmap, i_user_ns(inode), stat->gid);
|
| 300 | stat->uid = vfsuid_into_kuid(vfsuid);
|
| 301 | stat->gid = vfsgid_into_kgid(vfsgid);
|
| 302 |
|
| 303 | return err;
|
| 304 | }
|
| 305 |
|
| 306 | int ovl_permission(const struct mnt_idmap *idmap,
|
| 307 | struct inode *inode, int mask)
|
| 308 | {
|
| 309 | struct inode *upperinode = ovl_inode_upper(inode);
|
| 310 | struct inode *realinode;
|
| 311 | struct path realpath;
|
| 312 | int err;
|
| 313 |
|
| 314 |
|
| 315 | realinode = ovl_i_path_real(inode, &realpath);
|
| 316 | if (!realinode) {
|
| 317 | WARN_ON(!(mask & MAY_NOT_BLOCK));
|
| 318 | return -ECHILD;
|
| 319 | }
|
| 320 |
|
| 321 | |
| 322 | * Check overlay inode with the creds of task and underlying inode
|
| 323 | * with creds of mounter
|
| 324 |
|
| 325 | err = generic_permission(idmap, inode, mask);
|
| 326 | if (err)
|
| 327 | return err;
|
| 328 |
|
| 329 | if (!upperinode &&
|
| 330 | !special_file(realinode->i_mode) && mask & MAY_WRITE) {
|
| 331 | mask &= ~(MAY_WRITE | MAY_APPEND);
|
| 332 |
|
| 333 | mask |= MAY_READ;
|
| 334 | }
|
| 335 |
|
| 336 | with_ovl_creds(inode->i_sb)
|
| 337 | return inode_permission(mnt_idmap(realpath.mnt), realinode, mask);
|
| 338 | }
|
| 339 |
|
| 340 | static const char *ovl_get_link(struct dentry *dentry,
|
| 341 | struct inode *inode,
|
| 342 | struct delayed_call *done)
|
| 343 | {
|
| 344 | if (!dentry)
|
| 345 | return ERR_PTR(-ECHILD);
|
| 346 |
|
| 347 | with_ovl_creds(dentry->d_sb)
|
| 348 | return vfs_get_link(ovl_dentry_real(dentry), done);
|
| 349 | }
|
| 350 |
|
| 351 | #ifdef CONFIG_FS_POSIX_ACL
|
| 352 | |
| 353 | * Apply the idmapping of the layer to POSIX ACLs. The caller must pass a clone
|
| 354 | * of the POSIX ACLs retrieved from the lower layer to this function to not
|
| 355 | * alter the POSIX ACLs for the underlying filesystem.
|
| 356 |
|
| 357 | static void ovl_idmap_posix_acl(const struct inode *realinode,
|
| 358 | const struct mnt_idmap *idmap,
|
| 359 | struct posix_acl *acl)
|
| 360 | {
|
| 361 | struct user_namespace *fs_userns = i_user_ns(realinode);
|
| 362 |
|
| 363 | for (unsigned int i = 0; i < acl->a_count; i++) {
|
| 364 | vfsuid_t vfsuid;
|
| 365 | vfsgid_t vfsgid;
|
| 366 |
|
| 367 | struct posix_acl_entry *e = &acl->a_entries[i];
|
| 368 | switch (e->e_tag) {
|
| 369 | case ACL_USER:
|
| 370 | vfsuid = make_vfsuid(idmap, fs_userns, e->e_uid);
|
| 371 | e->e_uid = vfsuid_into_kuid(vfsuid);
|
| 372 | break;
|
| 373 | case ACL_GROUP:
|
| 374 | vfsgid = make_vfsgid(idmap, fs_userns, e->e_gid);
|
| 375 | e->e_gid = vfsgid_into_kgid(vfsgid);
|
| 376 | break;
|
| 377 | }
|
| 378 | }
|
| 379 | }
|
| 380 |
|
| 381 | |
| 382 | * The @noperm argument is used to skip permission checking and is a temporary
|
| 383 | * measure. Quoting Miklos from an earlier discussion:
|
| 384 | *
|
| 385 | * > So there are two paths to getting an acl:
|
| 386 | * > 1) permission checking and 2) retrieving the value via getxattr(2).
|
| 387 | * > This is a similar situation as reading a symlink vs. following it.
|
| 388 | * > When following a symlink overlayfs always reads the link on the
|
| 389 | * > underlying fs just as if it was a readlink(2) call, calling
|
| 390 | * > security_inode_readlink() instead of security_inode_follow_link().
|
| 391 | * > This is logical: we are reading the link from the underlying storage,
|
| 392 | * > and following it on overlayfs.
|
| 393 | * >
|
| 394 | * > Applying the same logic to acl: we do need to call the
|
| 395 | * > security_inode_getxattr() on the underlying fs, even if just want to
|
| 396 | * > check permissions on overlay. This is currently not done, which is an
|
| 397 | * > inconsistency.
|
| 398 | * >
|
| 399 | * > Maybe adding the check to ovl_get_acl() is the right way to go, but
|
| 400 | * > I'm a little afraid of a performance regression. Will look into that.
|
| 401 | *
|
| 402 | * Until we have made a decision allow this helper to take the @noperm
|
| 403 | * argument. We should hopefully be able to remove it soon.
|
| 404 |
|
| 405 | struct posix_acl *ovl_get_acl_path(const struct path *path,
|
| 406 | const char *acl_name, bool noperm)
|
| 407 | {
|
| 408 | struct posix_acl *real_acl, *clone;
|
| 409 | const struct mnt_idmap *idmap;
|
| 410 | struct inode *realinode = d_inode(path->dentry);
|
| 411 |
|
| 412 | idmap = mnt_idmap(path->mnt);
|
| 413 |
|
| 414 | if (noperm)
|
| 415 | real_acl = get_inode_acl(realinode, posix_acl_type(acl_name));
|
| 416 | else
|
| 417 | real_acl = vfs_get_acl(idmap, path->dentry, acl_name);
|
| 418 | if (IS_ERR_OR_NULL(real_acl))
|
| 419 | return real_acl;
|
| 420 |
|
| 421 | if (!is_idmapped_mnt(path->mnt))
|
| 422 | return real_acl;
|
| 423 |
|
| 424 | |
| 425 | * We cannot alter the ACLs returned from the relevant layer as that
|
| 426 | * would alter the cached values filesystem wide for the lower
|
| 427 | * filesystem. Instead we can clone the ACLs and then apply the
|
| 428 | * relevant idmapping of the layer.
|
| 429 |
|
| 430 | clone = posix_acl_clone(real_acl, GFP_KERNEL);
|
| 431 | posix_acl_release(real_acl);
|
| 432 | if (!clone)
|
| 433 | return ERR_PTR(-ENOMEM);
|
| 434 |
|
| 435 | ovl_idmap_posix_acl(realinode, idmap, clone);
|
| 436 | return clone;
|
| 437 | }
|
| 438 |
|
| 439 | |
| 440 | * When the relevant layer is an idmapped mount we need to take the idmapping
|
| 441 | * of the layer into account and translate any ACL_{GROUP,USER} values
|
| 442 | * according to the idmapped mount.
|
| 443 | *
|
| 444 | * We cannot alter the ACLs returned from the relevant layer as that would
|
| 445 | * alter the cached values filesystem wide for the lower filesystem. Instead we
|
| 446 | * can clone the ACLs and then apply the relevant idmapping of the layer.
|
| 447 | *
|
| 448 | * This is obviously only relevant when idmapped layers are used.
|
| 449 |
|
| 450 | struct posix_acl *do_ovl_get_acl(const struct mnt_idmap *idmap,
|
| 451 | struct inode *inode, int type,
|
| 452 | bool rcu, bool noperm)
|
| 453 | {
|
| 454 | struct inode *realinode;
|
| 455 | struct posix_acl *acl;
|
| 456 | struct path realpath;
|
| 457 |
|
| 458 |
|
| 459 | realinode = ovl_i_path_real(inode, &realpath);
|
| 460 | if (!realinode) {
|
| 461 | WARN_ON(!rcu);
|
| 462 | return ERR_PTR(-ECHILD);
|
| 463 | }
|
| 464 |
|
| 465 | if (!IS_POSIXACL(realinode))
|
| 466 | return NULL;
|
| 467 |
|
| 468 | if (rcu) {
|
| 469 | |
| 470 | * If the layer is idmapped drop out of RCU path walk
|
| 471 | * so we can clone the ACLs.
|
| 472 |
|
| 473 | if (is_idmapped_mnt(realpath.mnt))
|
| 474 | return ERR_PTR(-ECHILD);
|
| 475 |
|
| 476 | acl = get_cached_acl_rcu(realinode, type);
|
| 477 | } else {
|
| 478 | with_ovl_creds(inode->i_sb)
|
| 479 | acl = ovl_get_acl_path(&realpath, posix_acl_xattr_name(type), noperm);
|
| 480 | }
|
| 481 |
|
| 482 | return acl;
|
| 483 | }
|
| 484 |
|
| 485 | static int ovl_set_or_remove_acl(struct dentry *dentry, struct inode *inode,
|
| 486 | struct posix_acl *acl, int type)
|
| 487 | {
|
| 488 | int err;
|
| 489 | struct path realpath;
|
| 490 | const char *acl_name;
|
| 491 | struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
|
| 492 | struct dentry *upperdentry = ovl_dentry_upper(dentry);
|
| 493 | struct dentry *realdentry = upperdentry ?: ovl_dentry_lower(dentry);
|
| 494 |
|
| 495 | |
| 496 | * If ACL is to be removed from a lower file, check if it exists in
|
| 497 | * the first place before copying it up.
|
| 498 |
|
| 499 | acl_name = posix_acl_xattr_name(type);
|
| 500 | if (!acl && !upperdentry) {
|
| 501 | struct posix_acl *real_acl;
|
| 502 |
|
| 503 | ovl_path_lower(dentry, &realpath);
|
| 504 | with_ovl_creds(dentry->d_sb)
|
| 505 | real_acl = vfs_get_acl(mnt_idmap(realpath.mnt), realdentry, acl_name);
|
| 506 | if (IS_ERR(real_acl)) {
|
| 507 | err = PTR_ERR(real_acl);
|
| 508 | goto out;
|
| 509 | }
|
| 510 | posix_acl_release(real_acl);
|
| 511 | }
|
| 512 |
|
| 513 | if (!upperdentry) {
|
| 514 | err = ovl_copy_up(dentry);
|
| 515 | if (err)
|
| 516 | goto out;
|
| 517 |
|
| 518 | realdentry = ovl_dentry_upper(dentry);
|
| 519 | }
|
| 520 |
|
| 521 | err = ovl_want_write(dentry);
|
| 522 | if (err)
|
| 523 | goto out;
|
| 524 |
|
| 525 | with_ovl_creds(dentry->d_sb) {
|
| 526 | if (acl)
|
| 527 | err = ovl_do_set_acl(ofs, realdentry, acl_name, acl);
|
| 528 | else
|
| 529 | err = ovl_do_remove_acl(ofs, realdentry, acl_name);
|
| 530 | }
|
| 531 | ovl_drop_write(dentry);
|
| 532 |
|
| 533 |
|
| 534 | ovl_copyattr(inode);
|
| 535 | out:
|
| 536 | return err;
|
| 537 | }
|
| 538 |
|
| 539 | int ovl_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry,
|
| 540 | struct posix_acl *acl, int type)
|
| 541 | {
|
| 542 | int err;
|
| 543 | struct inode *inode = d_inode(dentry);
|
| 544 | struct dentry *workdir = ovl_workdir(dentry);
|
| 545 | struct inode *realinode = ovl_inode_real(inode);
|
| 546 |
|
| 547 | if (!IS_POSIXACL(d_inode(workdir)))
|
| 548 | return -EOPNOTSUPP;
|
| 549 | if (!realinode->i_op->set_acl)
|
| 550 | return -EOPNOTSUPP;
|
| 551 | if (type == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode))
|
| 552 | return acl ? -EACCES : 0;
|
| 553 | if (!inode_owner_or_capable(idmap, inode))
|
| 554 | return -EPERM;
|
| 555 |
|
| 556 | |
| 557 | * Check if sgid bit needs to be cleared (actual setacl operation will
|
| 558 | * be done with mounter's capabilities and so that won't do it for us).
|
| 559 |
|
| 560 | if (unlikely(inode->i_mode & S_ISGID) && type == ACL_TYPE_ACCESS &&
|
| 561 | !in_group_or_capable(idmap, inode,
|
| 562 | i_gid_into_vfsgid(idmap, inode))) {
|
| 563 | struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
|
| 564 |
|
| 565 | err = ovl_setattr(&nop_mnt_idmap, dentry, &iattr);
|
| 566 | if (err)
|
| 567 | return err;
|
| 568 | }
|
| 569 |
|
| 570 | return ovl_set_or_remove_acl(dentry, inode, acl, type);
|
| 571 | }
|
| 572 | #endif
|
| 573 |
|
| 574 | int ovl_update_time(struct inode *inode, enum fs_update_time type,
|
| 575 | unsigned int flags)
|
| 576 | {
|
| 577 | if (type == FS_UPD_ATIME) {
|
| 578 | struct ovl_fs *ofs = OVL_FS(inode->i_sb);
|
| 579 | struct path upperpath = {
|
| 580 | .mnt = ovl_upper_mnt(ofs),
|
| 581 | .dentry = ovl_upperdentry_dereference(OVL_I(inode)),
|
| 582 | };
|
| 583 |
|
| 584 | if (upperpath.dentry) {
|
| 585 | if (flags & IOCB_NOWAIT)
|
| 586 | return -EAGAIN;
|
| 587 | touch_atime(&upperpath);
|
| 588 | inode_set_atime_to_ts(inode,
|
| 589 | inode_get_atime(d_inode(upperpath.dentry)));
|
| 590 | }
|
| 591 | }
|
| 592 | return 0;
|
| 593 | }
|
| 594 |
|
| 595 | static int ovl_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
|
| 596 | u64 start, u64 len)
|
| 597 | {
|
| 598 | struct inode *realinode = ovl_inode_realdata(inode);
|
| 599 |
|
| 600 | if (!realinode)
|
| 601 | return -EIO;
|
| 602 |
|
| 603 | if (!realinode->i_op->fiemap)
|
| 604 | return -EOPNOTSUPP;
|
| 605 |
|
| 606 | with_ovl_creds(inode->i_sb)
|
| 607 | return realinode->i_op->fiemap(realinode, fieinfo, start, len);
|
| 608 | }
|
| 609 |
|
| 610 | |
| 611 | * Work around the fact that security_file_ioctl() takes a file argument.
|
| 612 | * Introducing security_inode_fileattr_get/set() hooks would solve this issue
|
| 613 | * properly.
|
| 614 |
|
| 615 | static int ovl_security_fileattr(const struct path *realpath, struct file_kattr *fa,
|
| 616 | bool set)
|
| 617 | {
|
| 618 | struct file *file;
|
| 619 | unsigned int cmd;
|
| 620 | int err;
|
| 621 | unsigned int flags;
|
| 622 |
|
| 623 | flags = O_RDONLY;
|
| 624 | if (force_o_largefile())
|
| 625 | flags |= O_LARGEFILE;
|
| 626 |
|
| 627 | file = dentry_open(realpath, flags, current_cred());
|
| 628 | if (IS_ERR(file))
|
| 629 | return PTR_ERR(file);
|
| 630 |
|
| 631 | if (set)
|
| 632 | cmd = fa->fsx_valid ? FS_IOC_FSSETXATTR : FS_IOC_SETFLAGS;
|
| 633 | else
|
| 634 | cmd = fa->fsx_valid ? FS_IOC_FSGETXATTR : FS_IOC_GETFLAGS;
|
| 635 |
|
| 636 | err = security_file_ioctl(file, cmd, 0);
|
| 637 | fput(file);
|
| 638 |
|
| 639 | return err;
|
| 640 | }
|
| 641 |
|
| 642 | int ovl_real_fileattr_set(const struct path *realpath, struct file_kattr *fa)
|
| 643 | {
|
| 644 | int err;
|
| 645 |
|
| 646 | err = ovl_security_fileattr(realpath, fa, true);
|
| 647 | if (err)
|
| 648 | return err;
|
| 649 |
|
| 650 | return vfs_fileattr_set(mnt_idmap(realpath->mnt), realpath->dentry, fa);
|
| 651 | }
|
| 652 |
|
| 653 | int ovl_fileattr_set(const struct mnt_idmap *idmap,
|
| 654 | struct dentry *dentry, struct file_kattr *fa)
|
| 655 | {
|
| 656 | struct inode *inode = d_inode(dentry);
|
| 657 | struct path upperpath;
|
| 658 | unsigned int flags;
|
| 659 | int err;
|
| 660 |
|
| 661 | err = ovl_copy_up(dentry);
|
| 662 | if (!err) {
|
| 663 | ovl_path_real(dentry, &upperpath);
|
| 664 |
|
| 665 | err = ovl_want_write(dentry);
|
| 666 | if (err)
|
| 667 | goto out;
|
| 668 |
|
| 669 | with_ovl_creds(inode->i_sb) {
|
| 670 | |
| 671 | * Store immutable/append-only flags in xattr and clear them
|
| 672 | * in upper fileattr (in case they were set by older kernel)
|
| 673 | * so children of "ovl-immutable" directories lower aliases of
|
| 674 | * "ovl-immutable" hardlinks could be copied up.
|
| 675 | * Clear xattr when flags are cleared.
|
| 676 |
|
| 677 | err = ovl_set_protattr(inode, upperpath.dentry, fa);
|
| 678 | if (!err)
|
| 679 | err = ovl_real_fileattr_set(&upperpath, fa);
|
| 680 | }
|
| 681 | ovl_drop_write(dentry);
|
| 682 |
|
| 683 | |
| 684 | * Merge real inode flags with inode flags read from
|
| 685 | * overlay.protattr xattr
|
| 686 |
|
| 687 | flags = ovl_inode_real(inode)->i_flags & OVL_COPY_I_FLAGS_MASK;
|
| 688 |
|
| 689 | BUILD_BUG_ON(OVL_PROT_I_FLAGS_MASK & ~OVL_COPY_I_FLAGS_MASK);
|
| 690 | flags |= inode->i_flags & OVL_PROT_I_FLAGS_MASK;
|
| 691 | inode_set_flags(inode, flags, OVL_COPY_I_FLAGS_MASK);
|
| 692 |
|
| 693 |
|
| 694 | ovl_copyattr(inode);
|
| 695 | }
|
| 696 | out:
|
| 697 | return err;
|
| 698 | }
|
| 699 |
|
| 700 |
|
| 701 | static void ovl_fileattr_prot_flags(struct inode *inode, struct file_kattr *fa)
|
| 702 | {
|
| 703 | BUILD_BUG_ON(OVL_PROT_FS_FLAGS_MASK & ~FS_COMMON_FL);
|
| 704 | BUILD_BUG_ON(OVL_PROT_FSX_FLAGS_MASK & ~FS_XFLAG_COMMON);
|
| 705 |
|
| 706 | if (inode->i_flags & S_APPEND) {
|
| 707 | fa->flags |= FS_APPEND_FL;
|
| 708 | fa->fsx_xflags |= FS_XFLAG_APPEND;
|
| 709 | }
|
| 710 | if (inode->i_flags & S_IMMUTABLE) {
|
| 711 | fa->flags |= FS_IMMUTABLE_FL;
|
| 712 | fa->fsx_xflags |= FS_XFLAG_IMMUTABLE;
|
| 713 | }
|
| 714 | }
|
| 715 |
|
| 716 | int ovl_real_fileattr_get(const struct path *realpath, struct file_kattr *fa)
|
| 717 | {
|
| 718 | int err;
|
| 719 |
|
| 720 | err = ovl_security_fileattr(realpath, fa, false);
|
| 721 | if (err)
|
| 722 | return err;
|
| 723 |
|
| 724 | err = vfs_fileattr_get(realpath->dentry, fa);
|
| 725 | if (err == -ENOIOCTLCMD)
|
| 726 | err = -ENOTTY;
|
| 727 | return err;
|
| 728 | }
|
| 729 |
|
| 730 | int ovl_fileattr_get(struct dentry *dentry, struct file_kattr *fa)
|
| 731 | {
|
| 732 | struct inode *inode = d_inode(dentry);
|
| 733 | struct path realpath;
|
| 734 | int err;
|
| 735 |
|
| 736 | ovl_path_real(dentry, &realpath);
|
| 737 |
|
| 738 | with_ovl_creds(inode->i_sb)
|
| 739 | err = ovl_real_fileattr_get(&realpath, fa);
|
| 740 | ovl_fileattr_prot_flags(inode, fa);
|
| 741 |
|
| 742 | return err;
|
| 743 | }
|
| 744 |
|
| 745 | static const struct inode_operations ovl_file_inode_operations = {
|
| 746 | .setattr = ovl_setattr,
|
| 747 | .permission = ovl_permission,
|
| 748 | .getattr = ovl_getattr,
|
| 749 | .listxattr = ovl_listxattr,
|
| 750 | .get_inode_acl = ovl_get_inode_acl,
|
| 751 | .get_acl = ovl_get_acl,
|
| 752 | .set_acl = ovl_set_acl,
|
| 753 | .update_time = ovl_update_time,
|
| 754 | .fiemap = ovl_fiemap,
|
| 755 | .fileattr_get = ovl_fileattr_get,
|
| 756 | .fileattr_set = ovl_fileattr_set,
|
| 757 | };
|
| 758 |
|
| 759 | static const struct inode_operations ovl_symlink_inode_operations = {
|
| 760 | .setattr = ovl_setattr,
|
| 761 | .get_link = ovl_get_link,
|
| 762 | .getattr = ovl_getattr,
|
| 763 | .listxattr = ovl_listxattr,
|
| 764 | .update_time = ovl_update_time,
|
| 765 | };
|
| 766 |
|
| 767 | static const struct inode_operations ovl_special_inode_operations = {
|
| 768 | .setattr = ovl_setattr,
|
| 769 | .permission = ovl_permission,
|
| 770 | .getattr = ovl_getattr,
|
| 771 | .listxattr = ovl_listxattr,
|
| 772 | .get_inode_acl = ovl_get_inode_acl,
|
| 773 | .get_acl = ovl_get_acl,
|
| 774 | .set_acl = ovl_set_acl,
|
| 775 | .update_time = ovl_update_time,
|
| 776 | };
|
| 777 |
|
| 778 | static const struct address_space_operations ovl_aops = {
|
| 779 |
|
| 780 | .direct_IO = noop_direct_IO,
|
| 781 | };
|
| 782 |
|
| 783 | |
| 784 | * It is possible to stack overlayfs instance on top of another
|
| 785 | * overlayfs instance as lower layer. We need to annotate the
|
| 786 | * stackable i_mutex locks according to stack level of the super
|
| 787 | * block instance. An overlayfs instance can never be in stack
|
| 788 | * depth 0 (there is always a real fs below it). An overlayfs
|
| 789 | * inode lock will use the lockdep annotation ovl_i_mutex_key[depth].
|
| 790 | *
|
| 791 | * For example, here is a snip from /proc/lockdep_chains after
|
| 792 | * dir_iterate of nested overlayfs:
|
| 793 | *
|
| 794 | * [...] &ovl_i_mutex_dir_key[depth] (stack_depth=2)
|
| 795 | * [...] &ovl_i_mutex_dir_key[depth]#2 (stack_depth=1)
|
| 796 | * [...] &type->i_mutex_dir_key (stack_depth=0)
|
| 797 | *
|
| 798 | * Locking order w.r.t ovl_want_write() is important for nested overlayfs.
|
| 799 | *
|
| 800 | * This chain is valid:
|
| 801 | * - inode->i_rwsem (inode_lock[2])
|
| 802 | * - OVL_I(inode)->lock (ovl_inode_lock[2])
|
| 803 | * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
|
| 804 | * - OVL_I(lowerinode)->lock (ovl_inode_lock[1])
|
| 805 | *
|
| 806 | * And this chain is valid:
|
| 807 | * - inode->i_rwsem (inode_lock[2])
|
| 808 | * - OVL_I(inode)->lock (ovl_inode_lock[2])
|
| 809 | * - lowerinode->i_rwsem (inode_lock[1])
|
| 810 | * - OVL_I(lowerinode)->lock (ovl_inode_lock[1])
|
| 811 | *
|
| 812 | * But lowerinode->i_rwsem SHOULD NOT be acquired while ovl_want_write() is
|
| 813 | * held, because it is in reverse order of the non-nested case using the same
|
| 814 | * upper fs:
|
| 815 | * - inode->i_rwsem (inode_lock[1])
|
| 816 | * - OVL_I(inode)->lock (ovl_inode_lock[1])
|
| 817 | * - upper_mnt->mnt_sb->s_writers (ovl_want_write[0])
|
| 818 |
|
| 819 |
|
| 820 | static inline void ovl_lockdep_annotate_inode_mutex_key(struct inode *inode)
|
| 821 | {
|
| 822 | #ifdef CONFIG_LOCKDEP
|
| 823 | static struct lock_class_key ovl_i_mutex_key[OVL_MAX_NESTING];
|
| 824 | static struct lock_class_key ovl_i_mutex_dir_key[OVL_MAX_NESTING];
|
| 825 | static struct lock_class_key ovl_i_lock_key[OVL_MAX_NESTING];
|
| 826 |
|
| 827 | int depth = inode->i_sb->s_stack_depth - 1;
|
| 828 |
|
| 829 | if (WARN_ON_ONCE(depth < 0 || depth >= OVL_MAX_NESTING))
|
| 830 | depth = 0;
|
| 831 |
|
| 832 | if (S_ISDIR(inode->i_mode))
|
| 833 | lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_dir_key[depth]);
|
| 834 | else
|
| 835 | lockdep_set_class(&inode->i_rwsem, &ovl_i_mutex_key[depth]);
|
| 836 |
|
| 837 | lockdep_set_class(&OVL_I(inode)->lock, &ovl_i_lock_key[depth]);
|
| 838 | #endif
|
| 839 | }
|
| 840 |
|
| 841 | static void ovl_next_ino(struct inode *inode)
|
| 842 | {
|
| 843 | struct ovl_fs *ofs = OVL_FS(inode->i_sb);
|
| 844 |
|
| 845 | inode->i_ino = atomic_long_inc_return(&ofs->last_ino);
|
| 846 | if (unlikely(!inode->i_ino))
|
| 847 | inode->i_ino = atomic_long_inc_return(&ofs->last_ino);
|
| 848 | }
|
| 849 |
|
| 850 | static void ovl_map_ino(struct inode *inode, unsigned long ino, int fsid)
|
| 851 | {
|
| 852 | struct ovl_fs *ofs = OVL_FS(inode->i_sb);
|
| 853 | int xinobits = ovl_xino_bits(ofs);
|
| 854 | unsigned int xinoshift = 64 - xinobits;
|
| 855 |
|
| 856 | |
| 857 | * When d_ino is consistent with st_ino (samefs or i_ino has enough
|
| 858 | * bits to encode layer), set the same value used for st_ino to i_ino,
|
| 859 | * so inode number exposed via /proc/locks and a like will be
|
| 860 | * consistent with d_ino and st_ino values. An i_ino value inconsistent
|
| 861 | * with d_ino also causes nfsd readdirplus to fail.
|
| 862 |
|
| 863 | inode->i_ino = ino;
|
| 864 | if (ovl_same_fs(ofs)) {
|
| 865 | return;
|
| 866 | } else if (xinobits && likely(!(ino >> xinoshift))) {
|
| 867 | inode->i_ino |= (unsigned long)fsid << (xinoshift + 1);
|
| 868 | return;
|
| 869 | }
|
| 870 |
|
| 871 | |
| 872 | * For directory inodes on non-samefs with xino disabled or xino
|
| 873 | * overflow, we allocate a non-persistent inode number, to be used for
|
| 874 | * resolving st_ino collisions in ovl_map_dev_ino().
|
| 875 | *
|
| 876 | * To avoid ino collision with legitimate xino values from upper
|
| 877 | * layer (fsid 0), use the lowest xinobit to map the non
|
| 878 | * persistent inode numbers to the unified st_ino address space.
|
| 879 |
|
| 880 | if (S_ISDIR(inode->i_mode)) {
|
| 881 | ovl_next_ino(inode);
|
| 882 | if (xinobits) {
|
| 883 | inode->i_ino &= ~0UL >> xinobits;
|
| 884 | inode->i_ino |= 1UL << xinoshift;
|
| 885 | }
|
| 886 | }
|
| 887 | }
|
| 888 |
|
| 889 | void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip,
|
| 890 | unsigned long ino, int fsid)
|
| 891 | {
|
| 892 | struct inode *realinode;
|
| 893 | struct ovl_inode *oi = OVL_I(inode);
|
| 894 |
|
| 895 | oi->__upperdentry = oip->upperdentry;
|
| 896 | oi->oe = oip->oe;
|
| 897 | oi->redirect = oip->redirect;
|
| 898 | oi->lowerdata_redirect = oip->lowerdata_redirect;
|
| 899 |
|
| 900 | realinode = ovl_inode_real(inode);
|
| 901 | ovl_copyattr(inode);
|
| 902 | ovl_copyflags(realinode, inode);
|
| 903 | ovl_map_ino(inode, ino, fsid);
|
| 904 | }
|
| 905 |
|
| 906 | static void ovl_fill_inode(struct inode *inode, umode_t mode, dev_t rdev)
|
| 907 | {
|
| 908 | inode->i_mode = mode;
|
| 909 | inode->i_flags |= S_NOCMTIME;
|
| 910 | #ifdef CONFIG_FS_POSIX_ACL
|
| 911 | inode->i_acl = inode->i_default_acl = ACL_DONT_CACHE;
|
| 912 | #endif
|
| 913 |
|
| 914 | ovl_lockdep_annotate_inode_mutex_key(inode);
|
| 915 |
|
| 916 | switch (mode & S_IFMT) {
|
| 917 | case S_IFREG:
|
| 918 | inode->i_op = &ovl_file_inode_operations;
|
| 919 | inode->i_fop = &ovl_file_operations;
|
| 920 | inode->i_mapping->a_ops = &ovl_aops;
|
| 921 | break;
|
| 922 |
|
| 923 | case S_IFDIR:
|
| 924 | inode->i_op = &ovl_dir_inode_operations;
|
| 925 | inode->i_fop = &ovl_dir_operations;
|
| 926 | break;
|
| 927 |
|
| 928 | case S_IFLNK:
|
| 929 | inode->i_op = &ovl_symlink_inode_operations;
|
| 930 | break;
|
| 931 |
|
| 932 | default:
|
| 933 | inode->i_op = &ovl_special_inode_operations;
|
| 934 | init_special_inode(inode, mode, rdev);
|
| 935 | break;
|
| 936 | }
|
| 937 | }
|
| 938 |
|
| 939 | |
| 940 | * With inodes index enabled, an overlay inode nlink counts the union of upper
|
| 941 | * hardlinks and non-covered lower hardlinks. During the lifetime of a non-pure
|
| 942 | * upper inode, the following nlink modifying operations can happen:
|
| 943 | *
|
| 944 | * 1. Lower hardlink copy up
|
| 945 | * 2. Upper hardlink created, unlinked or renamed over
|
| 946 | * 3. Lower hardlink whiteout or renamed over
|
| 947 | *
|
| 948 | * For the first, copy up case, the union nlink does not change, whether the
|
| 949 | * operation succeeds or fails, but the upper inode nlink may change.
|
| 950 | * Therefore, before copy up, we store the union nlink value relative to the
|
| 951 | * lower inode nlink in the index inode xattr .overlay.nlink.
|
| 952 | *
|
| 953 | * For the second, upper hardlink case, the union nlink should be incremented
|
| 954 | * or decremented IFF the operation succeeds, aligned with nlink change of the
|
| 955 | * upper inode. Therefore, before link/unlink/rename, we store the union nlink
|
| 956 | * value relative to the upper inode nlink in the index inode.
|
| 957 | *
|
| 958 | * For the last, lower cover up case, we simplify things by preceding the
|
| 959 | * whiteout or cover up with copy up. This makes sure that there is an index
|
| 960 | * upper inode where the nlink xattr can be stored before the copied up upper
|
| 961 | * entry is unlink.
|
| 962 |
|
| 963 | #define OVL_NLINK_ADD_UPPER (1 << 0)
|
| 964 |
|
| 965 | |
| 966 | * On-disk format for indexed nlink:
|
| 967 | *
|
| 968 | * nlink relative to the upper inode - "U[+-]NUM"
|
| 969 | * nlink relative to the lower inode - "L[+-]NUM"
|
| 970 |
|
| 971 |
|
| 972 | static int ovl_set_nlink_common(struct dentry *dentry,
|
| 973 | struct dentry *realdentry, const char *format)
|
| 974 | {
|
| 975 | struct inode *inode = d_inode(dentry);
|
| 976 | struct inode *realinode = d_inode(realdentry);
|
| 977 | char buf[13];
|
| 978 | int len;
|
| 979 |
|
| 980 | len = snprintf(buf, sizeof(buf), format,
|
| 981 | (int) (inode->i_nlink - realinode->i_nlink));
|
| 982 |
|
| 983 | if (WARN_ON(len >= sizeof(buf)))
|
| 984 | return -EIO;
|
| 985 |
|
| 986 | return ovl_setxattr(OVL_FS(inode->i_sb), ovl_dentry_upper(dentry),
|
| 987 | OVL_XATTR_NLINK, buf, len);
|
| 988 | }
|
| 989 |
|
| 990 | int ovl_set_nlink_upper(struct dentry *dentry)
|
| 991 | {
|
| 992 | return ovl_set_nlink_common(dentry, ovl_dentry_upper(dentry), "U%+i");
|
| 993 | }
|
| 994 |
|
| 995 | int ovl_set_nlink_lower(struct dentry *dentry)
|
| 996 | {
|
| 997 | return ovl_set_nlink_common(dentry, ovl_dentry_lower(dentry), "L%+i");
|
| 998 | }
|
| 999 |
|
| 1000 | unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry,
|
| 1001 | struct dentry *upperdentry,
|
| 1002 | unsigned int fallback)
|
| 1003 | {
|
| 1004 | int nlink_diff;
|
| 1005 | int nlink;
|
| 1006 | char buf[13];
|
| 1007 | int err;
|
| 1008 |
|
| 1009 | if (!lowerdentry || !upperdentry || d_inode(lowerdentry)->i_nlink == 1)
|
| 1010 | return fallback;
|
| 1011 |
|
| 1012 | err = ovl_getxattr_upper(ofs, upperdentry, OVL_XATTR_NLINK,
|
| 1013 | &buf, sizeof(buf) - 1);
|
| 1014 | if (err < 0)
|
| 1015 | goto fail;
|
| 1016 |
|
| 1017 | buf[err] = '\0';
|
| 1018 | if ((buf[0] != 'L' && buf[0] != 'U') ||
|
| 1019 | (buf[1] != '+' && buf[1] != '-'))
|
| 1020 | goto fail;
|
| 1021 |
|
| 1022 | err = kstrtoint(buf + 1, 10, &nlink_diff);
|
| 1023 | if (err < 0)
|
| 1024 | goto fail;
|
| 1025 |
|
| 1026 | nlink = d_inode(buf[0] == 'L' ? lowerdentry : upperdentry)->i_nlink;
|
| 1027 | nlink += nlink_diff;
|
| 1028 |
|
| 1029 | if (nlink <= 0)
|
| 1030 | goto fail;
|
| 1031 |
|
| 1032 | return nlink;
|
| 1033 |
|
| 1034 | fail:
|
| 1035 | pr_warn_ratelimited("failed to get index nlink (%pd2, err=%i)\n",
|
| 1036 | upperdentry, err);
|
| 1037 | return fallback;
|
| 1038 | }
|
| 1039 |
|
| 1040 | struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev)
|
| 1041 | {
|
| 1042 | struct inode *inode;
|
| 1043 |
|
| 1044 | inode = new_inode(sb);
|
| 1045 | if (inode)
|
| 1046 | ovl_fill_inode(inode, mode, rdev);
|
| 1047 |
|
| 1048 | return inode;
|
| 1049 | }
|
| 1050 |
|
| 1051 | static int ovl_inode_test(struct inode *inode, void *data)
|
| 1052 | {
|
| 1053 | return inode->i_private == data;
|
| 1054 | }
|
| 1055 |
|
| 1056 | static int ovl_inode_set(struct inode *inode, void *data)
|
| 1057 | {
|
| 1058 | inode->i_private = data;
|
| 1059 | return 0;
|
| 1060 | }
|
| 1061 |
|
| 1062 | static bool ovl_verify_inode(struct inode *inode, struct dentry *lowerdentry,
|
| 1063 | struct dentry *upperdentry, bool strict)
|
| 1064 | {
|
| 1065 | |
| 1066 | * For directories, @strict verify from lookup path performs consistency
|
| 1067 | * checks, so NULL lower/upper in dentry must match NULL lower/upper in
|
| 1068 | * inode. Non @strict verify from NFS handle decode path passes NULL for
|
| 1069 | * 'unknown' lower/upper.
|
| 1070 |
|
| 1071 | if (S_ISDIR(inode->i_mode) && strict) {
|
| 1072 |
|
| 1073 | if (!lowerdentry && ovl_inode_lower(inode))
|
| 1074 | return false;
|
| 1075 |
|
| 1076 |
|
| 1077 | if (!upperdentry && ovl_inode_upper(inode))
|
| 1078 | return false;
|
| 1079 | }
|
| 1080 |
|
| 1081 | |
| 1082 | * Allow non-NULL lower inode in ovl_inode even if lowerdentry is NULL.
|
| 1083 | * This happens when finding a copied up overlay inode for a renamed
|
| 1084 | * or hardlinked overlay dentry and lower dentry cannot be followed
|
| 1085 | * by origin because lower fs does not support file handles.
|
| 1086 |
|
| 1087 | if (lowerdentry && ovl_inode_lower(inode) != d_inode(lowerdentry))
|
| 1088 | return false;
|
| 1089 |
|
| 1090 | |
| 1091 | * Allow non-NULL __upperdentry in inode even if upperdentry is NULL.
|
| 1092 | * This happens when finding a lower alias for a copied up hard link.
|
| 1093 |
|
| 1094 | if (upperdentry && ovl_inode_upper(inode) != d_inode(upperdentry))
|
| 1095 | return false;
|
| 1096 |
|
| 1097 | return true;
|
| 1098 | }
|
| 1099 |
|
| 1100 | struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
|
| 1101 | bool is_upper)
|
| 1102 | {
|
| 1103 | struct inode *inode, *key = d_inode(real);
|
| 1104 |
|
| 1105 | inode = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
|
| 1106 | if (!inode)
|
| 1107 | return NULL;
|
| 1108 |
|
| 1109 | if (!ovl_verify_inode(inode, is_upper ? NULL : real,
|
| 1110 | is_upper ? real : NULL, false)) {
|
| 1111 | iput(inode);
|
| 1112 | return ERR_PTR(-ESTALE);
|
| 1113 | }
|
| 1114 |
|
| 1115 | return inode;
|
| 1116 | }
|
| 1117 |
|
| 1118 | bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir)
|
| 1119 | {
|
| 1120 | struct inode *key = d_inode(dir);
|
| 1121 | struct inode *trap;
|
| 1122 | bool res;
|
| 1123 |
|
| 1124 | trap = ilookup5(sb, (unsigned long) key, ovl_inode_test, key);
|
| 1125 | if (!trap)
|
| 1126 | return false;
|
| 1127 |
|
| 1128 | res = IS_DEADDIR(trap) && !ovl_inode_upper(trap) &&
|
| 1129 | !ovl_inode_lower(trap);
|
| 1130 |
|
| 1131 | iput(trap);
|
| 1132 | return res;
|
| 1133 | }
|
| 1134 |
|
| 1135 | |
| 1136 | * Create an inode cache entry for layer root dir, that will intentionally
|
| 1137 | * fail ovl_verify_inode(), so any lookup that will find some layer root
|
| 1138 | * will fail.
|
| 1139 |
|
| 1140 | struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir)
|
| 1141 | {
|
| 1142 | struct inode *key = d_inode(dir);
|
| 1143 | struct inode *trap;
|
| 1144 |
|
| 1145 | if (!d_is_dir(dir))
|
| 1146 | return ERR_PTR(-ENOTDIR);
|
| 1147 |
|
| 1148 | trap = iget5_locked(sb, (unsigned long) key, ovl_inode_test,
|
| 1149 | ovl_inode_set, key);
|
| 1150 | if (!trap)
|
| 1151 | return ERR_PTR(-ENOMEM);
|
| 1152 |
|
| 1153 | if (!(inode_state_read_once(trap) & I_NEW)) {
|
| 1154 |
|
| 1155 | iput(trap);
|
| 1156 | return ERR_PTR(-ELOOP);
|
| 1157 | }
|
| 1158 |
|
| 1159 | trap->i_mode = S_IFDIR;
|
| 1160 | trap->i_flags = S_DEAD;
|
| 1161 | unlock_new_inode(trap);
|
| 1162 |
|
| 1163 | return trap;
|
| 1164 | }
|
| 1165 |
|
| 1166 | |
| 1167 | * Does overlay inode need to be hashed by lower inode?
|
| 1168 |
|
| 1169 | static bool ovl_hash_bylower(struct super_block *sb, struct dentry *upper,
|
| 1170 | struct dentry *lower, bool index)
|
| 1171 | {
|
| 1172 | struct ovl_fs *ofs = OVL_FS(sb);
|
| 1173 |
|
| 1174 |
|
| 1175 | if (!lower)
|
| 1176 | return false;
|
| 1177 |
|
| 1178 |
|
| 1179 | if (index)
|
| 1180 | return true;
|
| 1181 |
|
| 1182 |
|
| 1183 | if (!ovl_upper_mnt(ofs))
|
| 1184 | return true;
|
| 1185 |
|
| 1186 |
|
| 1187 | if ((upper || !ovl_indexdir(sb)) &&
|
| 1188 | !d_is_dir(lower) && d_inode(lower)->i_nlink > 1)
|
| 1189 | return false;
|
| 1190 |
|
| 1191 |
|
| 1192 | if (ofs->config.nfs_export && upper)
|
| 1193 | return false;
|
| 1194 |
|
| 1195 |
|
| 1196 | return true;
|
| 1197 | }
|
| 1198 |
|
| 1199 | static struct inode *ovl_iget5(struct super_block *sb, struct inode *newinode,
|
| 1200 | struct inode *key)
|
| 1201 | {
|
| 1202 | return newinode ? inode_insert5(newinode, (unsigned long) key,
|
| 1203 | ovl_inode_test, ovl_inode_set, key) :
|
| 1204 | iget5_locked(sb, (unsigned long) key,
|
| 1205 | ovl_inode_test, ovl_inode_set, key);
|
| 1206 | }
|
| 1207 |
|
| 1208 | struct inode *ovl_get_inode(struct super_block *sb,
|
| 1209 | struct ovl_inode_params *oip)
|
| 1210 | {
|
| 1211 | struct ovl_fs *ofs = OVL_FS(sb);
|
| 1212 | struct dentry *upperdentry = oip->upperdentry;
|
| 1213 | struct ovl_path *lowerpath = ovl_lowerpath(oip->oe);
|
| 1214 | struct inode *realinode = upperdentry ? d_inode(upperdentry) : NULL;
|
| 1215 | struct inode *inode;
|
| 1216 | struct dentry *lowerdentry = lowerpath ? lowerpath->dentry : NULL;
|
| 1217 | struct path realpath = {
|
| 1218 | .dentry = upperdentry ?: lowerdentry,
|
| 1219 | .mnt = upperdentry ? ovl_upper_mnt(ofs) : lowerpath->layer->mnt,
|
| 1220 | };
|
| 1221 | bool bylower = ovl_hash_bylower(sb, upperdentry, lowerdentry,
|
| 1222 | oip->index);
|
| 1223 | int fsid = bylower ? lowerpath->layer->fsid : 0;
|
| 1224 | bool is_dir;
|
| 1225 | unsigned long ino = 0;
|
| 1226 | int err = oip->newinode ? -EEXIST : -ENOMEM;
|
| 1227 |
|
| 1228 | if (!realinode)
|
| 1229 | realinode = d_inode(lowerdentry);
|
| 1230 |
|
| 1231 | |
| 1232 | * Copy up origin (lower) may exist for non-indexed upper, but we must
|
| 1233 | * not use lower as hash key if this is a broken hardlink.
|
| 1234 |
|
| 1235 | is_dir = S_ISDIR(realinode->i_mode);
|
| 1236 | if (upperdentry || bylower) {
|
| 1237 | struct inode *key = d_inode(bylower ? lowerdentry :
|
| 1238 | upperdentry);
|
| 1239 | unsigned int nlink = is_dir ? 1 : realinode->i_nlink;
|
| 1240 |
|
| 1241 | inode = ovl_iget5(sb, oip->newinode, key);
|
| 1242 | if (!inode)
|
| 1243 | goto out_err;
|
| 1244 | if (!(inode_state_read_once(inode) & I_NEW)) {
|
| 1245 | |
| 1246 | * Verify that the underlying files stored in the inode
|
| 1247 | * match those in the dentry.
|
| 1248 |
|
| 1249 | if (!ovl_verify_inode(inode, lowerdentry, upperdentry,
|
| 1250 | true)) {
|
| 1251 | iput(inode);
|
| 1252 | err = -ESTALE;
|
| 1253 | goto out_err;
|
| 1254 | }
|
| 1255 |
|
| 1256 | dput(upperdentry);
|
| 1257 | ovl_free_entry(oip->oe);
|
| 1258 | kfree(oip->redirect);
|
| 1259 | kfree(oip->lowerdata_redirect);
|
| 1260 | goto out;
|
| 1261 | }
|
| 1262 |
|
| 1263 |
|
| 1264 | if (!is_dir)
|
| 1265 | nlink = ovl_get_nlink(ofs, lowerdentry, upperdentry,
|
| 1266 | nlink);
|
| 1267 | set_nlink(inode, nlink);
|
| 1268 | ino = key->i_ino;
|
| 1269 | } else {
|
| 1270 |
|
| 1271 | inode = new_inode(sb);
|
| 1272 | if (!inode) {
|
| 1273 | err = -ENOMEM;
|
| 1274 | goto out_err;
|
| 1275 | }
|
| 1276 | ino = realinode->i_ino;
|
| 1277 | fsid = lowerpath->layer->fsid;
|
| 1278 | }
|
| 1279 | ovl_fill_inode(inode, realinode->i_mode, realinode->i_rdev);
|
| 1280 | ovl_inode_init(inode, oip, ino, fsid);
|
| 1281 | WARN_ON_ONCE(!!IS_CASEFOLDED(inode) != ofs->casefold);
|
| 1282 |
|
| 1283 | if (upperdentry && ovl_is_impuredir(sb, upperdentry))
|
| 1284 | ovl_set_flag(OVL_IMPURE, inode);
|
| 1285 |
|
| 1286 | if (oip->index)
|
| 1287 | ovl_set_flag(OVL_INDEX, inode);
|
| 1288 |
|
| 1289 | if (bylower)
|
| 1290 | ovl_set_flag(OVL_CONST_INO, inode);
|
| 1291 |
|
| 1292 |
|
| 1293 | if (is_dir) {
|
| 1294 | if (((upperdentry && lowerdentry) || ovl_numlower(oip->oe) > 1) ||
|
| 1295 | ovl_path_check_origin_xattr(ofs, &realpath)) {
|
| 1296 | ovl_set_flag(OVL_WHITEOUTS, inode);
|
| 1297 | }
|
| 1298 | }
|
| 1299 |
|
| 1300 |
|
| 1301 | if (upperdentry)
|
| 1302 | ovl_check_protattr(inode, upperdentry);
|
| 1303 |
|
| 1304 | if (inode_state_read_once(inode) & I_NEW)
|
| 1305 | unlock_new_inode(inode);
|
| 1306 | out:
|
| 1307 | return inode;
|
| 1308 |
|
| 1309 | out_err:
|
| 1310 | pr_warn_ratelimited("failed to get inode (%i)\n", err);
|
| 1311 | inode = ERR_PTR(err);
|
| 1312 | goto out;
|
| 1313 | }
|