| 1 | --- a/fs/open.c |
| 2 | +++ b/fs/open.c |
| 3 | @@ -651,19 +651,19 @@ static inline int __get_file_write_acces |
| 4 | return error; |
| 5 | } |
| 6 | |
| 7 | -static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt, |
| 8 | - struct file *f, |
| 9 | +static struct file *__dentry_open(struct path *path, struct file *f, |
| 10 | int (*open)(struct inode *, struct file *), |
| 11 | const struct cred *cred) |
| 12 | { |
| 13 | struct inode *inode; |
| 14 | int error; |
| 15 | |
| 16 | + path_get(path); |
| 17 | f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK | |
| 18 | FMODE_PREAD | FMODE_PWRITE; |
| 19 | - inode = dentry->d_inode; |
| 20 | + inode = path->dentry->d_inode; |
| 21 | if (f->f_mode & FMODE_WRITE) { |
| 22 | - error = __get_file_write_access(inode, mnt); |
| 23 | + error = __get_file_write_access(inode, path->mnt); |
| 24 | if (error) |
| 25 | goto cleanup_file; |
| 26 | if (!special_file(inode->i_mode)) |
| 27 | @@ -671,8 +671,7 @@ static struct file *__dentry_open(struct |
| 28 | } |
| 29 | |
| 30 | f->f_mapping = inode->i_mapping; |
| 31 | - f->f_path.dentry = dentry; |
| 32 | - f->f_path.mnt = mnt; |
| 33 | + f->f_path = *path; |
| 34 | f->f_pos = 0; |
| 35 | f->f_op = fops_get(inode->i_fop); |
| 36 | file_sb_list_add(f, inode->i_sb); |
| 37 | @@ -718,7 +717,7 @@ cleanup_all: |
| 38 | * here, so just reset the state. |
| 39 | */ |
| 40 | file_reset_write(f); |
| 41 | - mnt_drop_write(mnt); |
| 42 | + mnt_drop_write(path->mnt); |
| 43 | } |
| 44 | } |
| 45 | file_sb_list_del(f); |
| 46 | @@ -726,8 +725,7 @@ cleanup_all: |
| 47 | f->f_path.mnt = NULL; |
| 48 | cleanup_file: |
| 49 | put_filp(f); |
| 50 | - dput(dentry); |
| 51 | - mntput(mnt); |
| 52 | + path_put(path); |
| 53 | return ERR_PTR(error); |
| 54 | } |
| 55 | |
| 56 | @@ -753,14 +751,14 @@ cleanup_file: |
| 57 | struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry, |
| 58 | int (*open)(struct inode *, struct file *)) |
| 59 | { |
| 60 | + struct path path = { .dentry = dentry, .mnt = nd->path.mnt }; |
| 61 | const struct cred *cred = current_cred(); |
| 62 | |
| 63 | if (IS_ERR(nd->intent.open.file)) |
| 64 | goto out; |
| 65 | if (IS_ERR(dentry)) |
| 66 | goto out_err; |
| 67 | - nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt), |
| 68 | - nd->intent.open.file, |
| 69 | + nd->intent.open.file = __dentry_open(&path, nd->intent.open.file, |
| 70 | open, cred); |
| 71 | out: |
| 72 | return nd->intent.open.file; |
| 73 | @@ -787,10 +785,17 @@ struct file *nameidata_to_filp(struct na |
| 74 | filp = nd->intent.open.file; |
| 75 | /* Has the filesystem initialised the file for us? */ |
| 76 | if (filp->f_path.dentry == NULL) { |
| 77 | - path_get(&nd->path); |
| 78 | - filp = __dentry_open(nd->path.dentry, nd->path.mnt, filp, |
| 79 | - NULL, cred); |
| 80 | + struct inode *inode = nd->path.dentry->d_inode; |
| 81 | + |
| 82 | + if (inode->i_op->open) { |
| 83 | + int flags = filp->f_flags; |
| 84 | + put_filp(filp); |
| 85 | + filp = inode->i_op->open(nd->path.dentry, flags, cred); |
| 86 | + } else { |
| 87 | + filp = __dentry_open(&nd->path, filp, NULL, cred); |
| 88 | + } |
| 89 | } |
| 90 | + |
| 91 | return filp; |
| 92 | } |
| 93 | |
| 94 | @@ -801,35 +806,45 @@ struct file *nameidata_to_filp(struct na |
| 95 | struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags, |
| 96 | const struct cred *cred) |
| 97 | { |
| 98 | - int error; |
| 99 | + struct path path = { .dentry = dentry, .mnt = mnt }; |
| 100 | + struct file *ret; |
| 101 | + |
| 102 | + BUG_ON(!mnt); |
| 103 | + |
| 104 | + ret = vfs_open(&path, flags, cred); |
| 105 | + path_put(&path); |
| 106 | + |
| 107 | + return ret; |
| 108 | +} |
| 109 | +EXPORT_SYMBOL(dentry_open); |
| 110 | + |
| 111 | +/** |
| 112 | + * vfs_open - open the file at the given path |
| 113 | + * @path: path to open |
| 114 | + * @flags: open flags |
| 115 | + * @cred: credentials to use |
| 116 | + * |
| 117 | + * Open the file. If successful, the returned file will have acquired |
| 118 | + * an additional reference for path. |
| 119 | + */ |
| 120 | +struct file *vfs_open(struct path *path, int flags, const struct cred *cred) |
| 121 | +{ |
| 122 | struct file *f; |
| 123 | + struct inode *inode = path->dentry->d_inode; |
| 124 | |
| 125 | validate_creds(cred); |
| 126 | |
| 127 | - /* |
| 128 | - * We must always pass in a valid mount pointer. Historically |
| 129 | - * callers got away with not passing it, but we must enforce this at |
| 130 | - * the earliest possible point now to avoid strange problems deep in the |
| 131 | - * filesystem stack. |
| 132 | - */ |
| 133 | - if (!mnt) { |
| 134 | - printk(KERN_WARNING "%s called with NULL vfsmount\n", __func__); |
| 135 | - dump_stack(); |
| 136 | - return ERR_PTR(-EINVAL); |
| 137 | - } |
| 138 | + if (inode->i_op->open) |
| 139 | + return inode->i_op->open(path->dentry, flags, cred); |
| 140 | |
| 141 | - error = -ENFILE; |
| 142 | f = get_empty_filp(); |
| 143 | - if (f == NULL) { |
| 144 | - dput(dentry); |
| 145 | - mntput(mnt); |
| 146 | - return ERR_PTR(error); |
| 147 | - } |
| 148 | + if (f == NULL) |
| 149 | + return ERR_PTR(-ENFILE); |
| 150 | |
| 151 | f->f_flags = flags; |
| 152 | - return __dentry_open(dentry, mnt, f, NULL, cred); |
| 153 | + return __dentry_open(path, f, NULL, cred); |
| 154 | } |
| 155 | -EXPORT_SYMBOL(dentry_open); |
| 156 | +EXPORT_SYMBOL(vfs_open); |
| 157 | |
| 158 | static void __put_unused_fd(struct files_struct *files, unsigned int fd) |
| 159 | { |
| 160 | --- a/include/linux/fs.h |
| 161 | +++ b/include/linux/fs.h |
| 162 | @@ -1574,6 +1574,7 @@ struct inode_operations { |
| 163 | loff_t len); |
| 164 | int (*fiemap)(struct inode *, struct fiemap_extent_info *, u64 start, |
| 165 | u64 len); |
| 166 | + struct file *(*open)(struct dentry *, int flags, const struct cred *); |
| 167 | }; |
| 168 | |
| 169 | struct seq_file; |
| 170 | @@ -1975,6 +1976,7 @@ extern int do_fallocate(struct file *fil |
| 171 | extern long do_sys_open(int dfd, const char __user *filename, int flags, |
| 172 | int mode); |
| 173 | extern struct file *filp_open(const char *, int, int); |
| 174 | +extern struct file *vfs_open(struct path *, int flags, const struct cred *); |
| 175 | extern struct file * dentry_open(struct dentry *, struct vfsmount *, int, |
| 176 | const struct cred *); |
| 177 | extern int filp_close(struct file *, fl_owner_t id); |
| 178 | --- a/fs/splice.c |
| 179 | +++ b/fs/splice.c |
| 180 | @@ -1307,6 +1307,7 @@ long do_splice_direct(struct file *in, l |
| 181 | |
| 182 | return ret; |
| 183 | } |
| 184 | +EXPORT_SYMBOL(do_splice_direct); |
| 185 | |
| 186 | static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe, |
| 187 | struct pipe_inode_info *opipe, |
| 188 | --- a/fs/namespace.c |
| 189 | +++ b/fs/namespace.c |
| 190 | @@ -1307,6 +1307,23 @@ void drop_collected_mounts(struct vfsmou |
| 191 | release_mounts(&umount_list); |
| 192 | } |
| 193 | |
| 194 | +struct vfsmount *clone_private_mount(struct path *path) |
| 195 | +{ |
| 196 | + struct vfsmount *mnt; |
| 197 | + |
| 198 | + if (IS_MNT_UNBINDABLE(path->mnt)) |
| 199 | + return ERR_PTR(-EINVAL); |
| 200 | + |
| 201 | + down_read(&namespace_sem); |
| 202 | + mnt = clone_mnt(path->mnt, path->dentry, CL_PRIVATE); |
| 203 | + up_read(&namespace_sem); |
| 204 | + if (!mnt) |
| 205 | + return ERR_PTR(-ENOMEM); |
| 206 | + |
| 207 | + return mnt; |
| 208 | +} |
| 209 | +EXPORT_SYMBOL_GPL(clone_private_mount); |
| 210 | + |
| 211 | int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg, |
| 212 | struct vfsmount *root) |
| 213 | { |
| 214 | --- a/include/linux/mount.h |
| 215 | +++ b/include/linux/mount.h |
| 216 | @@ -121,6 +121,9 @@ static inline void mntput(struct vfsmoun |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | +struct path; |
| 221 | +extern struct vfsmount *clone_private_mount(struct path *path); |
| 222 | + |
| 223 | extern struct vfsmount *do_kern_mount(const char *fstype, int flags, |
| 224 | const char *name, void *data); |
| 225 | |
| 226 | --- a/fs/Kconfig |
| 227 | +++ b/fs/Kconfig |
| 228 | @@ -64,6 +64,7 @@ source "fs/quota/Kconfig" |
| 229 | |
| 230 | source "fs/autofs4/Kconfig" |
| 231 | source "fs/fuse/Kconfig" |
| 232 | +source "fs/overlayfs/Kconfig" |
| 233 | |
| 234 | config CUSE |
| 235 | tristate "Character device in Userspace support" |
| 236 | --- a/fs/Makefile |
| 237 | +++ b/fs/Makefile |
| 238 | @@ -103,6 +103,7 @@ obj-$(CONFIG_QNX4FS_FS) += qnx4/ |
| 239 | obj-$(CONFIG_AUTOFS4_FS) += autofs4/ |
| 240 | obj-$(CONFIG_ADFS_FS) += adfs/ |
| 241 | obj-$(CONFIG_FUSE_FS) += fuse/ |
| 242 | +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs/ |
| 243 | obj-$(CONFIG_UDF_FS) += udf/ |
| 244 | obj-$(CONFIG_SUN_OPENPROMFS) += openpromfs/ |
| 245 | obj-$(CONFIG_OMFS_FS) += omfs/ |
| 246 | --- /dev/null |
| 247 | +++ b/fs/overlayfs/Kconfig |
| 248 | @@ -0,0 +1,4 @@ |
| 249 | +config OVERLAYFS_FS |
| 250 | + tristate "Overlay filesystem support" |
| 251 | + help |
| 252 | + Add support for overlay filesystem. |
| 253 | --- /dev/null |
| 254 | +++ b/fs/overlayfs/Makefile |
| 255 | @@ -0,0 +1,5 @@ |
| 256 | +# |
| 257 | +# Makefile for the overlay filesystem. |
| 258 | +# |
| 259 | + |
| 260 | +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs.o |
| 261 | --- /dev/null |
| 262 | +++ b/fs/overlayfs/overlayfs.c |
| 263 | @@ -0,0 +1,2358 @@ |
| 264 | +#include <linux/fs.h> |
| 265 | +#include <linux/namei.h> |
| 266 | +#include <linux/sched.h> |
| 267 | +#include <linux/fs_struct.h> |
| 268 | +#include <linux/file.h> |
| 269 | +#include <linux/xattr.h> |
| 270 | +#include <linux/security.h> |
| 271 | +#include <linux/device_cgroup.h> |
| 272 | +#include <linux/mount.h> |
| 273 | +#include <linux/splice.h> |
| 274 | +#include <linux/slab.h> |
| 275 | +#include <linux/parser.h> |
| 276 | +#include <linux/module.h> |
| 277 | +#include <linux/uaccess.h> |
| 278 | +#include <linux/rbtree.h> |
| 279 | + |
| 280 | +MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>"); |
| 281 | +MODULE_DESCRIPTION("Overlay filesystem"); |
| 282 | +MODULE_LICENSE("GPL"); |
| 283 | + |
| 284 | +#define OVL_COPY_UP_CHUNK_SIZE (1 << 20) |
| 285 | + |
| 286 | +struct ovl_fs { |
| 287 | + struct vfsmount *upper_mnt; |
| 288 | + struct vfsmount *lower_mnt; |
| 289 | +}; |
| 290 | + |
| 291 | +struct ovl_entry { |
| 292 | + struct dentry *__upperdentry; |
| 293 | + struct dentry *lowerdentry; |
| 294 | + union { |
| 295 | + struct { |
| 296 | + u64 version; |
| 297 | + bool opaque; |
| 298 | + }; |
| 299 | + struct rcu_head rcu; |
| 300 | + }; |
| 301 | +}; |
| 302 | + |
| 303 | +static const char *ovl_whiteout_xattr = "trusted.overlay.whiteout"; |
| 304 | +static const char *ovl_opaque_xattr = "trusted.overlay.opaque"; |
| 305 | +static const char *ovl_whiteout_symlink = "(overlay-whiteout)"; |
| 306 | + |
| 307 | +enum ovl_path_type { |
| 308 | + OVL_PATH_UPPER, |
| 309 | + OVL_PATH_MERGE, |
| 310 | + OVL_PATH_LOWER, |
| 311 | +}; |
| 312 | + |
| 313 | +static enum ovl_path_type ovl_path_type(struct dentry *dentry) |
| 314 | +{ |
| 315 | + struct ovl_entry *oe = dentry->d_fsdata; |
| 316 | + |
| 317 | + if (oe->__upperdentry) { |
| 318 | + if (oe->lowerdentry && S_ISDIR(dentry->d_inode->i_mode)) |
| 319 | + return OVL_PATH_MERGE; |
| 320 | + else |
| 321 | + return OVL_PATH_UPPER; |
| 322 | + } else { |
| 323 | + return OVL_PATH_LOWER; |
| 324 | + } |
| 325 | +} |
| 326 | + |
| 327 | +static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe) |
| 328 | +{ |
| 329 | + struct dentry *upperdentry = ACCESS_ONCE(oe->__upperdentry); |
| 330 | + smp_read_barrier_depends(); |
| 331 | + return upperdentry; |
| 332 | +} |
| 333 | + |
| 334 | +static void ovl_path_upper(struct dentry *dentry, struct path *path) |
| 335 | +{ |
| 336 | + struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 337 | + struct ovl_entry *oe = dentry->d_fsdata; |
| 338 | + |
| 339 | + path->mnt = ofs->upper_mnt; |
| 340 | + path->dentry = ovl_upperdentry_dereference(oe); |
| 341 | +} |
| 342 | + |
| 343 | +static void ovl_path_lower(struct dentry *dentry, struct path *path) |
| 344 | +{ |
| 345 | + struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 346 | + struct ovl_entry *oe = dentry->d_fsdata; |
| 347 | + |
| 348 | + path->mnt = ofs->lower_mnt; |
| 349 | + path->dentry = oe->lowerdentry; |
| 350 | +} |
| 351 | + |
| 352 | +static enum ovl_path_type ovl_path_real(struct dentry *dentry, |
| 353 | + struct path *path) |
| 354 | +{ |
| 355 | + |
| 356 | + enum ovl_path_type type = ovl_path_type(dentry); |
| 357 | + |
| 358 | + if (type == OVL_PATH_LOWER) |
| 359 | + ovl_path_lower(dentry, path); |
| 360 | + else |
| 361 | + ovl_path_upper(dentry, path); |
| 362 | + |
| 363 | + return type; |
| 364 | +} |
| 365 | + |
| 366 | +static struct dentry *ovl_dentry_upper(struct dentry *dentry) |
| 367 | +{ |
| 368 | + struct ovl_entry *oe = dentry->d_fsdata; |
| 369 | + |
| 370 | + return ovl_upperdentry_dereference(oe); |
| 371 | +} |
| 372 | + |
| 373 | +static struct dentry *ovl_dentry_lower(struct dentry *dentry) |
| 374 | +{ |
| 375 | + struct ovl_entry *oe = dentry->d_fsdata; |
| 376 | + |
| 377 | + return oe->lowerdentry; |
| 378 | +} |
| 379 | + |
| 380 | +static struct dentry *ovl_dentry_real(struct dentry *dentry) |
| 381 | +{ |
| 382 | + struct ovl_entry *oe = dentry->d_fsdata; |
| 383 | + struct dentry *realdentry; |
| 384 | + |
| 385 | + realdentry = ovl_upperdentry_dereference(oe); |
| 386 | + if (!realdentry) |
| 387 | + realdentry = oe->lowerdentry; |
| 388 | + |
| 389 | + return realdentry; |
| 390 | +} |
| 391 | + |
| 392 | +static bool ovl_dentry_is_opaque(struct dentry *dentry) |
| 393 | +{ |
| 394 | + struct ovl_entry *oe = dentry->d_fsdata; |
| 395 | + return oe->opaque; |
| 396 | +} |
| 397 | + |
| 398 | +static void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque) |
| 399 | +{ |
| 400 | + struct ovl_entry *oe = dentry->d_fsdata; |
| 401 | + oe->opaque = opaque; |
| 402 | +} |
| 403 | + |
| 404 | +static void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry) |
| 405 | +{ |
| 406 | + struct ovl_entry *oe = dentry->d_fsdata; |
| 407 | + |
| 408 | + WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex)); |
| 409 | + WARN_ON(oe->__upperdentry); |
| 410 | + smp_wmb(); |
| 411 | + oe->__upperdentry = upperdentry; |
| 412 | +} |
| 413 | + |
| 414 | +static void ovl_dentry_version_inc(struct dentry *dentry) |
| 415 | +{ |
| 416 | + struct ovl_entry *oe = dentry->d_fsdata; |
| 417 | + |
| 418 | + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex)); |
| 419 | + oe->version++; |
| 420 | +} |
| 421 | + |
| 422 | +static u64 ovl_dentry_version_get(struct dentry *dentry) |
| 423 | +{ |
| 424 | + struct ovl_entry *oe = dentry->d_fsdata; |
| 425 | + |
| 426 | + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex)); |
| 427 | + return oe->version; |
| 428 | +} |
| 429 | + |
| 430 | +static bool ovl_is_whiteout(struct dentry *dentry) |
| 431 | +{ |
| 432 | + int res; |
| 433 | + char val; |
| 434 | + |
| 435 | + if (!dentry) |
| 436 | + return false; |
| 437 | + if (!dentry->d_inode) |
| 438 | + return false; |
| 439 | + if (!S_ISLNK(dentry->d_inode->i_mode)) |
| 440 | + return false; |
| 441 | + |
| 442 | + res = vfs_getxattr(dentry, ovl_whiteout_xattr, &val, 1); |
| 443 | + if (res == 1 && val == 'y') |
| 444 | + return true; |
| 445 | + |
| 446 | + return false; |
| 447 | +} |
| 448 | + |
| 449 | +static bool ovl_is_opaquedir(struct dentry *dentry) |
| 450 | +{ |
| 451 | + int res; |
| 452 | + char val; |
| 453 | + |
| 454 | + if (!S_ISDIR(dentry->d_inode->i_mode)) |
| 455 | + return false; |
| 456 | + |
| 457 | + res = vfs_getxattr(dentry, ovl_opaque_xattr, &val, 1); |
| 458 | + if (res == 1 && val == 'y') |
| 459 | + return true; |
| 460 | + |
| 461 | + return false; |
| 462 | +} |
| 463 | + |
| 464 | +struct ovl_cache_entry { |
| 465 | + const char *name; |
| 466 | + unsigned int len; |
| 467 | + unsigned int type; |
| 468 | + u64 ino; |
| 469 | + bool is_whiteout; |
| 470 | + struct list_head l_node; |
| 471 | + struct rb_node node; |
| 472 | +}; |
| 473 | + |
| 474 | +struct ovl_readdir_data { |
| 475 | + struct rb_root *root; |
| 476 | + struct list_head *list; |
| 477 | + struct list_head *middle; |
| 478 | + struct dentry *dir; |
| 479 | + int count; |
| 480 | + int err; |
| 481 | +}; |
| 482 | + |
| 483 | +struct ovl_dir_file { |
| 484 | + bool is_real; |
| 485 | + bool is_cached; |
| 486 | + struct list_head cursor; |
| 487 | + u64 cache_version; |
| 488 | + struct list_head cache; |
| 489 | + struct file *realfile; |
| 490 | +}; |
| 491 | + |
| 492 | +static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n) |
| 493 | +{ |
| 494 | + return container_of(n, struct ovl_cache_entry, node); |
| 495 | +} |
| 496 | + |
| 497 | +static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root, |
| 498 | + const char *name, int len) |
| 499 | +{ |
| 500 | + struct rb_node *node = root->rb_node; |
| 501 | + int cmp; |
| 502 | + |
| 503 | + while (node) { |
| 504 | + struct ovl_cache_entry *p = ovl_cache_entry_from_node(node); |
| 505 | + |
| 506 | + cmp = strncmp(name, p->name, len); |
| 507 | + if (cmp > 0) |
| 508 | + node = p->node.rb_right; |
| 509 | + else if (cmp < 0 || len < p->len) |
| 510 | + node = p->node.rb_left; |
| 511 | + else |
| 512 | + return p; |
| 513 | + } |
| 514 | + |
| 515 | + return NULL; |
| 516 | +} |
| 517 | + |
| 518 | +static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len, |
| 519 | + u64 ino, unsigned int d_type, |
| 520 | + bool is_whiteout) |
| 521 | +{ |
| 522 | + struct ovl_cache_entry *p; |
| 523 | + |
| 524 | + p = kmalloc(sizeof(*p) + len + 1, GFP_KERNEL); |
| 525 | + if (p) { |
| 526 | + char *name_copy = (char *) (p + 1); |
| 527 | + memcpy(name_copy, name, len); |
| 528 | + name_copy[len] = '\0'; |
| 529 | + p->name = name_copy; |
| 530 | + p->len = len; |
| 531 | + p->type = d_type; |
| 532 | + p->ino = ino; |
| 533 | + p->is_whiteout = is_whiteout; |
| 534 | + } |
| 535 | + |
| 536 | + return p; |
| 537 | +} |
| 538 | + |
| 539 | +static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd, |
| 540 | + const char *name, int len, u64 ino, |
| 541 | + unsigned int d_type, bool is_whiteout) |
| 542 | +{ |
| 543 | + struct rb_node **newp = &rdd->root->rb_node; |
| 544 | + struct rb_node *parent = NULL; |
| 545 | + struct ovl_cache_entry *p; |
| 546 | + |
| 547 | + while (*newp) { |
| 548 | + int cmp; |
| 549 | + struct ovl_cache_entry *tmp; |
| 550 | + |
| 551 | + parent = *newp; |
| 552 | + tmp = ovl_cache_entry_from_node(*newp); |
| 553 | + cmp = strncmp(name, tmp->name, len); |
| 554 | + if (cmp > 0) |
| 555 | + newp = &tmp->node.rb_right; |
| 556 | + else if (cmp < 0 || len < tmp->len) |
| 557 | + newp = &tmp->node.rb_left; |
| 558 | + else |
| 559 | + return 0; |
| 560 | + } |
| 561 | + |
| 562 | + p = ovl_cache_entry_new(name, len, ino, d_type, is_whiteout); |
| 563 | + if (p == NULL) |
| 564 | + return -ENOMEM; |
| 565 | + |
| 566 | + list_add_tail(&p->l_node, rdd->list); |
| 567 | + rb_link_node(&p->node, parent, newp); |
| 568 | + rb_insert_color(&p->node, rdd->root); |
| 569 | + |
| 570 | + return 0; |
| 571 | +} |
| 572 | + |
| 573 | +static int ovl_fill_lower(void *buf, const char *name, int namelen, |
| 574 | + loff_t offset, u64 ino, unsigned int d_type) |
| 575 | +{ |
| 576 | + struct ovl_readdir_data *rdd = buf; |
| 577 | + struct ovl_cache_entry *p; |
| 578 | + |
| 579 | + rdd->count++; |
| 580 | + p = ovl_cache_entry_find(rdd->root, name, namelen); |
| 581 | + if (p) { |
| 582 | + list_move_tail(&p->l_node, rdd->middle); |
| 583 | + } else { |
| 584 | + p = ovl_cache_entry_new(name, namelen, ino, d_type, false); |
| 585 | + if (p == NULL) |
| 586 | + rdd->err = -ENOMEM; |
| 587 | + else |
| 588 | + list_add_tail(&p->l_node, rdd->middle); |
| 589 | + } |
| 590 | + |
| 591 | + return rdd->err; |
| 592 | +} |
| 593 | + |
| 594 | +static void ovl_cache_free(struct list_head *list) |
| 595 | +{ |
| 596 | + struct ovl_cache_entry *p; |
| 597 | + struct ovl_cache_entry *n; |
| 598 | + |
| 599 | + list_for_each_entry_safe(p, n, list, l_node) |
| 600 | + kfree(p); |
| 601 | + |
| 602 | + INIT_LIST_HEAD(list); |
| 603 | +} |
| 604 | + |
| 605 | +static int ovl_fill_upper(void *buf, const char *name, int namelen, |
| 606 | + loff_t offset, u64 ino, unsigned int d_type) |
| 607 | +{ |
| 608 | + struct ovl_readdir_data *rdd = buf; |
| 609 | + bool is_whiteout = false; |
| 610 | + |
| 611 | + rdd->count++; |
| 612 | + if (d_type == DT_LNK) { |
| 613 | + struct dentry *dentry; |
| 614 | + |
| 615 | + dentry = lookup_one_len(name, rdd->dir, namelen); |
| 616 | + if (IS_ERR(dentry)) { |
| 617 | + rdd->err = PTR_ERR(dentry); |
| 618 | + goto out; |
| 619 | + } |
| 620 | + is_whiteout = ovl_is_whiteout(dentry); |
| 621 | + dput(dentry); |
| 622 | + } |
| 623 | + |
| 624 | + rdd->err = ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type, |
| 625 | + is_whiteout); |
| 626 | + |
| 627 | +out: |
| 628 | + return rdd->err; |
| 629 | +} |
| 630 | + |
| 631 | +static int ovl_dir_read(struct path *realpath, struct ovl_readdir_data *rdd, |
| 632 | + filldir_t filler) |
| 633 | +{ |
| 634 | + const struct cred *old_cred; |
| 635 | + struct cred *override_cred; |
| 636 | + struct file *realfile; |
| 637 | + int err; |
| 638 | + |
| 639 | + realfile = vfs_open(realpath, O_RDONLY | O_DIRECTORY, current_cred()); |
| 640 | + if (IS_ERR(realfile)) |
| 641 | + return PTR_ERR(realfile); |
| 642 | + |
| 643 | + err = -ENOMEM; |
| 644 | + override_cred = prepare_creds(); |
| 645 | + if (override_cred) { |
| 646 | + /* |
| 647 | + * CAP_SYS_ADMIN for getxattr |
| 648 | + * CAP_DAC_OVERRIDE for lookup and unlink |
| 649 | + */ |
| 650 | + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN); |
| 651 | + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE); |
| 652 | + old_cred = override_creds(override_cred); |
| 653 | + |
| 654 | + do { |
| 655 | + rdd->count = 0; |
| 656 | + rdd->err = 0; |
| 657 | + err = vfs_readdir(realfile, filler, rdd); |
| 658 | + if (err >= 0) |
| 659 | + err = rdd->err; |
| 660 | + } while (!err && rdd->count); |
| 661 | + |
| 662 | + revert_creds(old_cred); |
| 663 | + put_cred(override_cred); |
| 664 | + } |
| 665 | + fput(realfile); |
| 666 | + |
| 667 | + if (err) { |
| 668 | + if (rdd->list) |
| 669 | + ovl_cache_free(rdd->list); |
| 670 | + return err; |
| 671 | + } |
| 672 | + |
| 673 | + return 0; |
| 674 | +} |
| 675 | + |
| 676 | +static void ovl_dir_reset(struct file *file) |
| 677 | +{ |
| 678 | + struct ovl_dir_file *od = file->private_data; |
| 679 | + enum ovl_path_type type = ovl_path_type(file->f_path.dentry); |
| 680 | + |
| 681 | + if (ovl_dentry_version_get(file->f_path.dentry) != od->cache_version) { |
| 682 | + list_del_init(&od->cursor); |
| 683 | + ovl_cache_free(&od->cache); |
| 684 | + od->is_cached = false; |
| 685 | + } |
| 686 | + WARN_ON(!od->is_real && type != OVL_PATH_MERGE); |
| 687 | + if (od->is_real && type == OVL_PATH_MERGE) { |
| 688 | + fput(od->realfile); |
| 689 | + od->realfile = NULL; |
| 690 | + od->is_real = false; |
| 691 | + } |
| 692 | +} |
| 693 | + |
| 694 | +static int ovl_dir_read_merged(struct path *upperpath, struct path *lowerpath, |
| 695 | + struct ovl_readdir_data *rdd) |
| 696 | +{ |
| 697 | + int err; |
| 698 | + struct rb_root root = RB_ROOT; |
| 699 | + struct list_head middle; |
| 700 | + |
| 701 | + rdd->root = &root; |
| 702 | + if (upperpath->dentry) { |
| 703 | + rdd->dir = upperpath->dentry; |
| 704 | + err = ovl_dir_read(upperpath, rdd, ovl_fill_upper); |
| 705 | + if (err) |
| 706 | + goto out; |
| 707 | + } |
| 708 | + /* |
| 709 | + * Insert lowerpath entries before upperpath ones, this allows |
| 710 | + * offsets to be reasonably constant |
| 711 | + */ |
| 712 | + list_add(&middle, rdd->list); |
| 713 | + rdd->middle = &middle; |
| 714 | + err = ovl_dir_read(lowerpath, rdd, ovl_fill_lower); |
| 715 | + list_del(&middle); |
| 716 | +out: |
| 717 | + rdd->root = NULL; |
| 718 | + |
| 719 | + return err; |
| 720 | +} |
| 721 | + |
| 722 | +static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos) |
| 723 | +{ |
| 724 | + struct list_head *l; |
| 725 | + loff_t off; |
| 726 | + |
| 727 | + l = od->cache.next; |
| 728 | + for (off = 0; off < pos; off++) { |
| 729 | + if (l == &od->cache) |
| 730 | + break; |
| 731 | + l = l->next; |
| 732 | + } |
| 733 | + list_move_tail(&od->cursor, l); |
| 734 | +} |
| 735 | + |
| 736 | +static int ovl_readdir(struct file *file, void *buf, filldir_t filler) |
| 737 | +{ |
| 738 | + struct ovl_dir_file *od = file->private_data; |
| 739 | + int res; |
| 740 | + |
| 741 | + if (!file->f_pos) |
| 742 | + ovl_dir_reset(file); |
| 743 | + |
| 744 | + if (od->is_real) { |
| 745 | + res = vfs_readdir(od->realfile, filler, buf); |
| 746 | + file->f_pos = od->realfile->f_pos; |
| 747 | + |
| 748 | + return res; |
| 749 | + } |
| 750 | + |
| 751 | + if (!od->is_cached) { |
| 752 | + struct path lowerpath; |
| 753 | + struct path upperpath; |
| 754 | + struct ovl_readdir_data rdd = { .list = &od->cache }; |
| 755 | + |
| 756 | + ovl_path_lower(file->f_path.dentry, &lowerpath); |
| 757 | + ovl_path_upper(file->f_path.dentry, &upperpath); |
| 758 | + |
| 759 | + res = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd); |
| 760 | + if (res) |
| 761 | + return res; |
| 762 | + |
| 763 | + od->cache_version = ovl_dentry_version_get(file->f_path.dentry); |
| 764 | + od->is_cached = true; |
| 765 | + |
| 766 | + ovl_seek_cursor(od, file->f_pos); |
| 767 | + } |
| 768 | + |
| 769 | + while (od->cursor.next != &od->cache) { |
| 770 | + int over; |
| 771 | + loff_t off; |
| 772 | + struct ovl_cache_entry *p; |
| 773 | + |
| 774 | + p = list_entry(od->cursor.next, struct ovl_cache_entry, l_node); |
| 775 | + off = file->f_pos; |
| 776 | + file->f_pos++; |
| 777 | + list_move(&od->cursor, &p->l_node); |
| 778 | + |
| 779 | + if (p->is_whiteout) |
| 780 | + continue; |
| 781 | + |
| 782 | + over = filler(buf, p->name, p->len, off, p->ino, p->type); |
| 783 | + if (over) |
| 784 | + break; |
| 785 | + } |
| 786 | + |
| 787 | + return 0; |
| 788 | +} |
| 789 | + |
| 790 | +static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin) |
| 791 | +{ |
| 792 | + loff_t res; |
| 793 | + struct ovl_dir_file *od = file->private_data; |
| 794 | + |
| 795 | + mutex_lock(&file->f_dentry->d_inode->i_mutex); |
| 796 | + if (!file->f_pos) |
| 797 | + ovl_dir_reset(file); |
| 798 | + |
| 799 | + if (od->is_real) { |
| 800 | + res = vfs_llseek(od->realfile, offset, origin); |
| 801 | + file->f_pos = od->realfile->f_pos; |
| 802 | + } else { |
| 803 | + res = -EINVAL; |
| 804 | + |
| 805 | + switch (origin) { |
| 806 | + case SEEK_CUR: |
| 807 | + offset += file->f_pos; |
| 808 | + break; |
| 809 | + case SEEK_SET: |
| 810 | + break; |
| 811 | + default: |
| 812 | + goto out_unlock; |
| 813 | + } |
| 814 | + if (offset < 0) |
| 815 | + goto out_unlock; |
| 816 | + |
| 817 | + if (offset != file->f_pos) { |
| 818 | + file->f_pos = offset; |
| 819 | + if (od->is_cached) |
| 820 | + ovl_seek_cursor(od, offset); |
| 821 | + } |
| 822 | + res = offset; |
| 823 | + } |
| 824 | +out_unlock: |
| 825 | + mutex_unlock(&file->f_dentry->d_inode->i_mutex); |
| 826 | + |
| 827 | + return res; |
| 828 | +} |
| 829 | + |
| 830 | +static int ovl_dir_fsync(struct file *file, int datasync) |
| 831 | +{ |
| 832 | + struct ovl_dir_file *od = file->private_data; |
| 833 | + |
| 834 | + /* May need to reopen directory if it got copied up */ |
| 835 | + if (!od->realfile) { |
| 836 | + struct path upperpath; |
| 837 | + |
| 838 | + ovl_path_upper(file->f_path.dentry, &upperpath); |
| 839 | + od->realfile = vfs_open(&upperpath, O_RDONLY, current_cred()); |
| 840 | + if (IS_ERR(od->realfile)) |
| 841 | + return PTR_ERR(od->realfile); |
| 842 | + } |
| 843 | + |
| 844 | + return vfs_fsync(od->realfile, datasync); |
| 845 | +} |
| 846 | + |
| 847 | +static int ovl_dir_release(struct inode *inode, struct file *file) |
| 848 | +{ |
| 849 | + struct ovl_dir_file *od = file->private_data; |
| 850 | + |
| 851 | + list_del(&od->cursor); |
| 852 | + ovl_cache_free(&od->cache); |
| 853 | + if (od->realfile) |
| 854 | + fput(od->realfile); |
| 855 | + kfree(od); |
| 856 | + |
| 857 | + return 0; |
| 858 | +} |
| 859 | + |
| 860 | +static int ovl_dir_open(struct inode *inode, struct file *file) |
| 861 | +{ |
| 862 | + struct path realpath; |
| 863 | + struct file *realfile; |
| 864 | + struct ovl_dir_file *od; |
| 865 | + enum ovl_path_type type; |
| 866 | + |
| 867 | + od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL); |
| 868 | + if (!od) |
| 869 | + return -ENOMEM; |
| 870 | + |
| 871 | + type = ovl_path_real(file->f_path.dentry, &realpath); |
| 872 | + realfile = vfs_open(&realpath, file->f_flags, current_cred()); |
| 873 | + if (IS_ERR(realfile)) { |
| 874 | + kfree(od); |
| 875 | + return PTR_ERR(realfile); |
| 876 | + } |
| 877 | + INIT_LIST_HEAD(&od->cache); |
| 878 | + INIT_LIST_HEAD(&od->cursor); |
| 879 | + od->is_cached = false; |
| 880 | + od->realfile = realfile; |
| 881 | + od->is_real = (type != OVL_PATH_MERGE); |
| 882 | + file->private_data = od; |
| 883 | + |
| 884 | + return 0; |
| 885 | +} |
| 886 | + |
| 887 | +static const struct file_operations ovl_dir_operations = { |
| 888 | + .read = generic_read_dir, |
| 889 | + .open = ovl_dir_open, |
| 890 | + .readdir = ovl_readdir, |
| 891 | + .llseek = ovl_dir_llseek, |
| 892 | + .fsync = ovl_dir_fsync, |
| 893 | + .release = ovl_dir_release, |
| 894 | +}; |
| 895 | + |
| 896 | +static const struct inode_operations ovl_dir_inode_operations; |
| 897 | + |
| 898 | +static void ovl_entry_free(struct rcu_head *head) |
| 899 | +{ |
| 900 | + struct ovl_entry *oe = container_of(head, struct ovl_entry, rcu); |
| 901 | + kfree(oe); |
| 902 | +} |
| 903 | + |
| 904 | +static void ovl_dentry_release(struct dentry *dentry) |
| 905 | +{ |
| 906 | + struct ovl_entry *oe = dentry->d_fsdata; |
| 907 | + |
| 908 | + if (oe) { |
| 909 | + dput(oe->__upperdentry); |
| 910 | + dput(oe->lowerdentry); |
| 911 | + call_rcu(&oe->rcu, ovl_entry_free); |
| 912 | + } |
| 913 | +} |
| 914 | + |
| 915 | +static const struct dentry_operations ovl_dentry_operations = { |
| 916 | + .d_release = ovl_dentry_release, |
| 917 | +}; |
| 918 | + |
| 919 | +static struct dentry *ovl_lookup_real(struct dentry *dir, struct qstr *name) |
| 920 | +{ |
| 921 | + struct dentry *dentry; |
| 922 | + |
| 923 | + mutex_lock(&dir->d_inode->i_mutex); |
| 924 | + dentry = lookup_one_len(name->name, dir, name->len); |
| 925 | + mutex_unlock(&dir->d_inode->i_mutex); |
| 926 | + |
| 927 | + if (IS_ERR(dentry)) { |
| 928 | + if (PTR_ERR(dentry) == -ENOENT) |
| 929 | + dentry = NULL; |
| 930 | + } else if (!dentry->d_inode) { |
| 931 | + dput(dentry); |
| 932 | + dentry = NULL; |
| 933 | + } |
| 934 | + return dentry; |
| 935 | +} |
| 936 | + |
| 937 | +static struct ovl_entry *ovl_alloc_entry(void) |
| 938 | +{ |
| 939 | + return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL); |
| 940 | +} |
| 941 | + |
| 942 | +static struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, |
| 943 | + struct ovl_entry *oe); |
| 944 | + |
| 945 | +static struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry, |
| 946 | + struct nameidata *nd) |
| 947 | +{ |
| 948 | + struct ovl_entry *oe; |
| 949 | + struct dentry *upperdir; |
| 950 | + struct dentry *lowerdir; |
| 951 | + struct dentry *upperdentry = NULL; |
| 952 | + struct dentry *lowerdentry = NULL; |
| 953 | + struct inode *inode = NULL; |
| 954 | + int err; |
| 955 | + |
| 956 | + err = -ENOMEM; |
| 957 | + oe = ovl_alloc_entry(); |
| 958 | + if (!oe) |
| 959 | + goto out; |
| 960 | + |
| 961 | + upperdir = ovl_dentry_upper(dentry->d_parent); |
| 962 | + lowerdir = ovl_dentry_lower(dentry->d_parent); |
| 963 | + |
| 964 | + if (upperdir) { |
| 965 | + upperdentry = ovl_lookup_real(upperdir, &dentry->d_name); |
| 966 | + err = PTR_ERR(upperdentry); |
| 967 | + if (IS_ERR(upperdentry)) |
| 968 | + goto out_put_dir; |
| 969 | + |
| 970 | + if (lowerdir && upperdentry && |
| 971 | + (S_ISLNK(upperdentry->d_inode->i_mode) || |
| 972 | + S_ISDIR(upperdentry->d_inode->i_mode))) { |
| 973 | + const struct cred *old_cred; |
| 974 | + struct cred *override_cred; |
| 975 | + |
| 976 | + err = -ENOMEM; |
| 977 | + override_cred = prepare_creds(); |
| 978 | + if (!override_cred) |
| 979 | + goto out_dput_upper; |
| 980 | + |
| 981 | + /* CAP_SYS_ADMIN needed for getxattr */ |
| 982 | + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN); |
| 983 | + old_cred = override_creds(override_cred); |
| 984 | + |
| 985 | + if (ovl_is_opaquedir(upperdentry)) { |
| 986 | + oe->opaque = true; |
| 987 | + } else if (ovl_is_whiteout(upperdentry)) { |
| 988 | + dput(upperdentry); |
| 989 | + upperdentry = NULL; |
| 990 | + oe->opaque = true; |
| 991 | + } |
| 992 | + revert_creds(old_cred); |
| 993 | + put_cred(override_cred); |
| 994 | + } |
| 995 | + } |
| 996 | + if (lowerdir && !oe->opaque) { |
| 997 | + lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name); |
| 998 | + err = PTR_ERR(lowerdentry); |
| 999 | + if (IS_ERR(lowerdentry)) |
| 1000 | + goto out_dput_upper; |
| 1001 | + } |
| 1002 | + |
| 1003 | + if (lowerdentry && upperdentry && |
| 1004 | + (!S_ISDIR(upperdentry->d_inode->i_mode) || |
| 1005 | + !S_ISDIR(lowerdentry->d_inode->i_mode))) { |
| 1006 | + dput(lowerdentry); |
| 1007 | + lowerdentry = NULL; |
| 1008 | + oe->opaque = true; |
| 1009 | + } |
| 1010 | + |
| 1011 | + if (lowerdentry || upperdentry) { |
| 1012 | + struct dentry *realdentry; |
| 1013 | + |
| 1014 | + realdentry = upperdentry ? upperdentry : lowerdentry; |
| 1015 | + err = -ENOMEM; |
| 1016 | + inode = ovl_new_inode(dir->i_sb, realdentry->d_inode->i_mode, oe); |
| 1017 | + if (!inode) |
| 1018 | + goto out_dput; |
| 1019 | + } |
| 1020 | + |
| 1021 | + if (upperdentry) |
| 1022 | + oe->__upperdentry = upperdentry; |
| 1023 | + |
| 1024 | + if (lowerdentry) |
| 1025 | + oe->lowerdentry = lowerdentry; |
| 1026 | + |
| 1027 | + dentry->d_fsdata = oe; |
| 1028 | + dentry->d_op = &ovl_dentry_operations; |
| 1029 | + d_add(dentry, inode); |
| 1030 | + |
| 1031 | + return NULL; |
| 1032 | + |
| 1033 | +out_dput: |
| 1034 | + dput(lowerdentry); |
| 1035 | +out_dput_upper: |
| 1036 | + dput(upperdentry); |
| 1037 | +out_put_dir: |
| 1038 | + kfree(oe); |
| 1039 | +out: |
| 1040 | + return ERR_PTR(err); |
| 1041 | +} |
| 1042 | + |
| 1043 | +static int ovl_copy_up_xattr(struct dentry *old, struct dentry *new) |
| 1044 | +{ |
| 1045 | + ssize_t list_size, size; |
| 1046 | + char *buf, *name, *value; |
| 1047 | + int error; |
| 1048 | + |
| 1049 | + if (!old->d_inode->i_op->getxattr || |
| 1050 | + !new->d_inode->i_op->getxattr) |
| 1051 | + return 0; |
| 1052 | + |
| 1053 | + list_size = vfs_listxattr(old, NULL, 0); |
| 1054 | + if (list_size <= 0) { |
| 1055 | + if (list_size == -EOPNOTSUPP) |
| 1056 | + return 0; |
| 1057 | + return list_size; |
| 1058 | + } |
| 1059 | + |
| 1060 | + buf = kzalloc(list_size, GFP_KERNEL); |
| 1061 | + if (!buf) |
| 1062 | + return -ENOMEM; |
| 1063 | + |
| 1064 | + error = -ENOMEM; |
| 1065 | + value = kmalloc(XATTR_SIZE_MAX, GFP_KERNEL); |
| 1066 | + if (!value) |
| 1067 | + goto out; |
| 1068 | + |
| 1069 | + list_size = vfs_listxattr(old, buf, list_size); |
| 1070 | + if (list_size <= 0) { |
| 1071 | + error = list_size; |
| 1072 | + goto out_free_value; |
| 1073 | + } |
| 1074 | + |
| 1075 | + for (name = buf; name < (buf + list_size); name += strlen(name) + 1) { |
| 1076 | + size = vfs_getxattr(old, name, value, XATTR_SIZE_MAX); |
| 1077 | + if (size <= 0) { |
| 1078 | + error = size; |
| 1079 | + goto out_free_value; |
| 1080 | + } |
| 1081 | + error = vfs_setxattr(new, name, value, size, 0); |
| 1082 | + if (error) |
| 1083 | + goto out_free_value; |
| 1084 | + } |
| 1085 | + |
| 1086 | +out_free_value: |
| 1087 | + kfree(value); |
| 1088 | +out: |
| 1089 | + kfree(buf); |
| 1090 | + return error; |
| 1091 | +} |
| 1092 | + |
| 1093 | +static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len) |
| 1094 | +{ |
| 1095 | + struct file *old_file; |
| 1096 | + struct file *new_file; |
| 1097 | + int error = 0; |
| 1098 | + |
| 1099 | + if (len == 0) |
| 1100 | + return 0; |
| 1101 | + |
| 1102 | + old_file = vfs_open(old, O_RDONLY, current_cred()); |
| 1103 | + if (IS_ERR(old_file)) |
| 1104 | + return PTR_ERR(old_file); |
| 1105 | + |
| 1106 | + new_file = vfs_open(new, O_WRONLY, current_cred()); |
| 1107 | + if (IS_ERR(new_file)) { |
| 1108 | + error = PTR_ERR(new_file); |
| 1109 | + goto out_fput; |
| 1110 | + } |
| 1111 | + |
| 1112 | + /* FIXME: copy up sparse files efficiently */ |
| 1113 | + while (len) { |
| 1114 | + loff_t offset = new_file->f_pos; |
| 1115 | + size_t this_len = OVL_COPY_UP_CHUNK_SIZE; |
| 1116 | + long bytes; |
| 1117 | + |
| 1118 | + if (len < this_len) |
| 1119 | + this_len = len; |
| 1120 | + |
| 1121 | + if (signal_pending_state(TASK_KILLABLE, current)) |
| 1122 | + return -EINTR; |
| 1123 | + |
| 1124 | + bytes = do_splice_direct(old_file, &offset, new_file, this_len, |
| 1125 | + SPLICE_F_MOVE); |
| 1126 | + if (bytes <= 0) { |
| 1127 | + error = bytes; |
| 1128 | + break; |
| 1129 | + } |
| 1130 | + |
| 1131 | + len -= bytes; |
| 1132 | + } |
| 1133 | + |
| 1134 | + fput(new_file); |
| 1135 | +out_fput: |
| 1136 | + fput(old_file); |
| 1137 | + return error; |
| 1138 | +} |
| 1139 | + |
| 1140 | +static struct dentry *ovl_lookup_create(struct dentry *upperdir, |
| 1141 | + struct dentry *template) |
| 1142 | +{ |
| 1143 | + int err; |
| 1144 | + struct dentry *newdentry; |
| 1145 | + struct qstr *name = &template->d_name; |
| 1146 | + |
| 1147 | + newdentry = lookup_one_len(name->name, upperdir, name->len); |
| 1148 | + if (IS_ERR(newdentry)) |
| 1149 | + return newdentry; |
| 1150 | + |
| 1151 | + if (newdentry->d_inode) { |
| 1152 | + const struct cred *old_cred; |
| 1153 | + struct cred *override_cred; |
| 1154 | + |
| 1155 | + /* No need to check whiteout if lower parent is non-existent */ |
| 1156 | + err = -EEXIST; |
| 1157 | + if (!ovl_dentry_lower(template->d_parent)) |
| 1158 | + goto out_dput; |
| 1159 | + |
| 1160 | + if (!S_ISLNK(newdentry->d_inode->i_mode)) |
| 1161 | + goto out_dput; |
| 1162 | + |
| 1163 | + err = -ENOMEM; |
| 1164 | + override_cred = prepare_creds(); |
| 1165 | + if (!override_cred) |
| 1166 | + goto out_dput; |
| 1167 | + |
| 1168 | + /* |
| 1169 | + * CAP_SYS_ADMIN for getxattr |
| 1170 | + * CAP_FOWNER for unlink in sticky directory |
| 1171 | + */ |
| 1172 | + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN); |
| 1173 | + cap_raise(override_cred->cap_effective, CAP_FOWNER); |
| 1174 | + old_cred = override_creds(override_cred); |
| 1175 | + |
| 1176 | + err = -EEXIST; |
| 1177 | + if (ovl_is_whiteout(newdentry)) |
| 1178 | + err = vfs_unlink(upperdir->d_inode, newdentry); |
| 1179 | + |
| 1180 | + revert_creds(old_cred); |
| 1181 | + put_cred(override_cred); |
| 1182 | + if (err) |
| 1183 | + goto out_dput; |
| 1184 | + |
| 1185 | + dput(newdentry); |
| 1186 | + newdentry = lookup_one_len(name->name, upperdir, name->len); |
| 1187 | + if (IS_ERR(newdentry)) |
| 1188 | + return newdentry; |
| 1189 | + |
| 1190 | + /* |
| 1191 | + * Whiteout just been successfully removed, parent |
| 1192 | + * i_mutex is still held, there's no way the lookup |
| 1193 | + * could return positive. |
| 1194 | + */ |
| 1195 | + WARN_ON(newdentry->d_inode); |
| 1196 | + } |
| 1197 | + |
| 1198 | + return newdentry; |
| 1199 | + |
| 1200 | +out_dput: |
| 1201 | + dput(newdentry); |
| 1202 | + return ERR_PTR(err); |
| 1203 | +} |
| 1204 | + |
| 1205 | +static struct dentry *ovl_upper_create(struct dentry *upperdir, |
| 1206 | + struct dentry *dentry, |
| 1207 | + struct kstat *stat, const char *link) |
| 1208 | +{ |
| 1209 | + int err; |
| 1210 | + struct dentry *newdentry; |
| 1211 | + struct inode *dir = upperdir->d_inode; |
| 1212 | + |
| 1213 | + newdentry = ovl_lookup_create(upperdir, dentry); |
| 1214 | + if (IS_ERR(newdentry)) |
| 1215 | + goto out; |
| 1216 | + |
| 1217 | + switch (stat->mode & S_IFMT) { |
| 1218 | + case S_IFREG: |
| 1219 | + err = vfs_create(dir, newdentry, stat->mode, NULL); |
| 1220 | + break; |
| 1221 | + |
| 1222 | + case S_IFDIR: |
| 1223 | + err = vfs_mkdir(dir, newdentry, stat->mode); |
| 1224 | + break; |
| 1225 | + |
| 1226 | + case S_IFCHR: |
| 1227 | + case S_IFBLK: |
| 1228 | + case S_IFIFO: |
| 1229 | + case S_IFSOCK: |
| 1230 | + err = vfs_mknod(dir, newdentry, stat->mode, stat->rdev); |
| 1231 | + break; |
| 1232 | + |
| 1233 | + case S_IFLNK: |
| 1234 | + err = vfs_symlink(dir, newdentry, link); |
| 1235 | + break; |
| 1236 | + |
| 1237 | + default: |
| 1238 | + err = -EPERM; |
| 1239 | + } |
| 1240 | + if (err) { |
| 1241 | + dput(newdentry); |
| 1242 | + newdentry = ERR_PTR(err); |
| 1243 | + } |
| 1244 | + |
| 1245 | +out: |
| 1246 | + return newdentry; |
| 1247 | + |
| 1248 | +} |
| 1249 | + |
| 1250 | +static char *ovl_read_symlink(struct dentry *realdentry) |
| 1251 | +{ |
| 1252 | + int res; |
| 1253 | + char *buf; |
| 1254 | + struct inode *inode = realdentry->d_inode; |
| 1255 | + mm_segment_t old_fs; |
| 1256 | + |
| 1257 | + res = -EINVAL; |
| 1258 | + if (!inode->i_op->readlink) |
| 1259 | + goto err; |
| 1260 | + |
| 1261 | + res = -ENOMEM; |
| 1262 | + buf = (char *) __get_free_page(GFP_KERNEL); |
| 1263 | + if (!buf) |
| 1264 | + goto err; |
| 1265 | + |
| 1266 | + old_fs = get_fs(); |
| 1267 | + set_fs(get_ds()); |
| 1268 | + /* The cast to a user pointer is valid due to the set_fs() */ |
| 1269 | + res = inode->i_op->readlink(realdentry, |
| 1270 | + (char __user *)buf, PAGE_SIZE - 1); |
| 1271 | + set_fs(old_fs); |
| 1272 | + if (res < 0) { |
| 1273 | + free_page((unsigned long) buf); |
| 1274 | + goto err; |
| 1275 | + } |
| 1276 | + buf[res] = '\0'; |
| 1277 | + |
| 1278 | + return buf; |
| 1279 | + |
| 1280 | +err: |
| 1281 | + return ERR_PTR(res); |
| 1282 | +} |
| 1283 | + |
| 1284 | +static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat) |
| 1285 | +{ |
| 1286 | + struct iattr attr = { |
| 1287 | + .ia_valid = ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET, |
| 1288 | + .ia_atime = stat->atime, |
| 1289 | + .ia_mtime = stat->mtime, |
| 1290 | + }; |
| 1291 | + |
| 1292 | + return notify_change(upperdentry, &attr); |
| 1293 | +} |
| 1294 | + |
| 1295 | +static int ovl_set_mode(struct dentry *upperdentry, umode_t mode) |
| 1296 | +{ |
| 1297 | + struct iattr attr = { |
| 1298 | + .ia_valid = ATTR_MODE, |
| 1299 | + .ia_mode = mode, |
| 1300 | + }; |
| 1301 | + |
| 1302 | + return notify_change(upperdentry, &attr); |
| 1303 | +} |
| 1304 | + |
| 1305 | +static int ovl_set_opaque(struct dentry *upperdentry) |
| 1306 | +{ |
| 1307 | + int err; |
| 1308 | + const struct cred *old_cred; |
| 1309 | + struct cred *override_cred; |
| 1310 | + |
| 1311 | + override_cred = prepare_creds(); |
| 1312 | + if (!override_cred) |
| 1313 | + return -ENOMEM; |
| 1314 | + |
| 1315 | + /* CAP_SYS_ADMIN for setxattr of "trusted" namespace */ |
| 1316 | + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN); |
| 1317 | + old_cred = override_creds(override_cred); |
| 1318 | + err = vfs_setxattr(upperdentry, ovl_opaque_xattr, "y", 1, 0); |
| 1319 | + revert_creds(old_cred); |
| 1320 | + put_cred(override_cred); |
| 1321 | + |
| 1322 | + return err; |
| 1323 | +} |
| 1324 | + |
| 1325 | +static int ovl_remove_opaque(struct dentry *upperdentry) |
| 1326 | +{ |
| 1327 | + int err; |
| 1328 | + const struct cred *old_cred; |
| 1329 | + struct cred *override_cred; |
| 1330 | + |
| 1331 | + override_cred = prepare_creds(); |
| 1332 | + if (!override_cred) |
| 1333 | + return -ENOMEM; |
| 1334 | + |
| 1335 | + /* CAP_SYS_ADMIN for removexattr of "trusted" namespace */ |
| 1336 | + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN); |
| 1337 | + old_cred = override_creds(override_cred); |
| 1338 | + err = vfs_removexattr(upperdentry, ovl_opaque_xattr); |
| 1339 | + revert_creds(old_cred); |
| 1340 | + put_cred(override_cred); |
| 1341 | + |
| 1342 | + return err; |
| 1343 | +} |
| 1344 | + |
| 1345 | +static int ovl_copy_up_locked(struct dentry *upperdir, struct dentry *dentry, |
| 1346 | + struct path *lowerpath, struct kstat *stat, |
| 1347 | + const char *link) |
| 1348 | +{ |
| 1349 | + int err; |
| 1350 | + struct path newpath; |
| 1351 | + umode_t mode = stat->mode; |
| 1352 | + struct ovl_fs *ofs = dentry->d_sb->s_fs_info; |
| 1353 | + |
| 1354 | + /* Can't properly set mode on creation because of the umask */ |
| 1355 | + stat->mode &= S_IFMT; |
| 1356 | + |
| 1357 | + newpath.mnt = ofs->upper_mnt; |
| 1358 | + newpath.dentry = ovl_upper_create(upperdir, dentry, stat, link); |
| 1359 | + if (IS_ERR(newpath.dentry)) { |
| 1360 | + err = PTR_ERR(newpath.dentry); |
| 1361 | + |
| 1362 | + /* Already copied up? */ |
| 1363 | + if (err == -EEXIST && ovl_path_type(dentry) != OVL_PATH_LOWER) |
| 1364 | + return 0; |
| 1365 | + |
| 1366 | + return err; |
| 1367 | + } |
| 1368 | + |
| 1369 | + /* FIXME: recovery from failure to copy up */ |
| 1370 | + |
| 1371 | + if (S_ISREG(stat->mode)) { |
| 1372 | + err = ovl_copy_up_data(lowerpath, &newpath, stat->size); |
| 1373 | + if (err) |
| 1374 | + return err; |
| 1375 | + } |
| 1376 | + |
| 1377 | + err = ovl_copy_up_xattr(lowerpath->dentry, newpath.dentry); |
| 1378 | + if (err) |
| 1379 | + return err; |
| 1380 | + |
| 1381 | + mutex_lock(&newpath.dentry->d_inode->i_mutex); |
| 1382 | + if (!S_ISLNK(stat->mode)) |
| 1383 | + err = ovl_set_mode(newpath.dentry, mode); |
| 1384 | + if (!err) |
| 1385 | + err = ovl_set_timestamps(newpath.dentry, stat); |
| 1386 | + mutex_unlock(&newpath.dentry->d_inode->i_mutex); |
| 1387 | + if (err) |
| 1388 | + return err; |
| 1389 | + |
| 1390 | + ovl_dentry_update(dentry, newpath.dentry); |
| 1391 | + |
| 1392 | + /* |
| 1393 | + * Easiest way to get rid of the lower dentry reference is to |
| 1394 | + * drop this dentry. This is neither needed nor possible for |
| 1395 | + * directories. |
| 1396 | + */ |
| 1397 | + if (!S_ISDIR(stat->mode)) |
| 1398 | + d_drop(dentry); |
| 1399 | + |
| 1400 | + return 0; |
| 1401 | +} |
| 1402 | + |
| 1403 | +static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry, |
| 1404 | + struct path *lowerpath, struct kstat *stat) |
| 1405 | +{ |
| 1406 | + int err; |
| 1407 | + struct kstat pstat; |
| 1408 | + struct path parentpath; |
| 1409 | + struct dentry *upperdir; |
| 1410 | + const struct cred *old_cred; |
| 1411 | + struct cred *override_cred; |
| 1412 | + char *link = NULL; |
| 1413 | + |
| 1414 | + ovl_path_upper(parent, &parentpath); |
| 1415 | + upperdir = parentpath.dentry; |
| 1416 | + |
| 1417 | + err = vfs_getattr(parentpath.mnt, parentpath.dentry, &pstat); |
| 1418 | + if (err) |
| 1419 | + return err; |
| 1420 | + |
| 1421 | + if (S_ISLNK(stat->mode)) { |
| 1422 | + link = ovl_read_symlink(lowerpath->dentry); |
| 1423 | + if (IS_ERR(link)) |
| 1424 | + return PTR_ERR(link); |
| 1425 | + } |
| 1426 | + |
| 1427 | + err = -ENOMEM; |
| 1428 | + override_cred = prepare_creds(); |
| 1429 | + if (!override_cred) |
| 1430 | + goto out_free_link; |
| 1431 | + |
| 1432 | + override_cred->fsuid = stat->uid; |
| 1433 | + override_cred->fsgid = stat->gid; |
| 1434 | + /* |
| 1435 | + * CAP_SYS_ADMIN for copying up extended attributes |
| 1436 | + * CAP_DAC_OVERRIDE for create |
| 1437 | + * CAP_FOWNER for chmod, timestamp update |
| 1438 | + * CAP_FSETID for chmod |
| 1439 | + * CAP_MKNOD for mknod |
| 1440 | + */ |
| 1441 | + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN); |
| 1442 | + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE); |
| 1443 | + cap_raise(override_cred->cap_effective, CAP_FOWNER); |
| 1444 | + cap_raise(override_cred->cap_effective, CAP_FSETID); |
| 1445 | + cap_raise(override_cred->cap_effective, CAP_MKNOD); |
| 1446 | + old_cred = override_creds(override_cred); |
| 1447 | + |
| 1448 | + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT); |
| 1449 | + /* |
| 1450 | + * Using upper filesystem locking to protect against copy up |
| 1451 | + * racing with rename (rename means the copy up was already |
| 1452 | + * successful). |
| 1453 | + */ |
| 1454 | + if (dentry->d_parent != parent) { |
| 1455 | + WARN_ON((ovl_path_type(dentry) == OVL_PATH_LOWER)); |
| 1456 | + err = 0; |
| 1457 | + } else { |
| 1458 | + err = ovl_copy_up_locked(upperdir, dentry, lowerpath, |
| 1459 | + stat, link); |
| 1460 | + if (!err) { |
| 1461 | + /* Restore timestamps on parent (best effort) */ |
| 1462 | + ovl_set_timestamps(upperdir, &pstat); |
| 1463 | + } |
| 1464 | + } |
| 1465 | + |
| 1466 | + mutex_unlock(&upperdir->d_inode->i_mutex); |
| 1467 | + |
| 1468 | + revert_creds(old_cred); |
| 1469 | + put_cred(override_cred); |
| 1470 | + |
| 1471 | +out_free_link: |
| 1472 | + if (link) |
| 1473 | + free_page((unsigned long) link); |
| 1474 | + |
| 1475 | + return err; |
| 1476 | +} |
| 1477 | + |
| 1478 | +static int ovl_copy_up(struct dentry *dentry) |
| 1479 | +{ |
| 1480 | + int err; |
| 1481 | + |
| 1482 | + err = 0; |
| 1483 | + while (!err) { |
| 1484 | + struct dentry *next; |
| 1485 | + struct dentry *parent; |
| 1486 | + struct path lowerpath; |
| 1487 | + struct kstat stat; |
| 1488 | + enum ovl_path_type type = ovl_path_type(dentry); |
| 1489 | + |
| 1490 | + if (type != OVL_PATH_LOWER) |
| 1491 | + break; |
| 1492 | + |
| 1493 | + next = dget(dentry); |
| 1494 | + /* find the topmost dentry not yet copied up */ |
| 1495 | + for (;;) { |
| 1496 | + parent = dget_parent(next); |
| 1497 | + |
| 1498 | + type = ovl_path_type(parent); |
| 1499 | + if (type != OVL_PATH_LOWER) |
| 1500 | + break; |
| 1501 | + |
| 1502 | + dput(next); |
| 1503 | + next = parent; |
| 1504 | + } |
| 1505 | + |
| 1506 | + ovl_path_lower(next, &lowerpath); |
| 1507 | + err = vfs_getattr(lowerpath.mnt, lowerpath.dentry, &stat); |
| 1508 | + if (!err) |
| 1509 | + err = ovl_copy_up_one(parent, next, &lowerpath, &stat); |
| 1510 | + |
| 1511 | + dput(parent); |
| 1512 | + dput(next); |
| 1513 | + } |
| 1514 | + |
| 1515 | + return err; |
| 1516 | +} |
| 1517 | + |
| 1518 | +/* Optimize by not copying up the file first and truncating later */ |
| 1519 | +static int ovl_copy_up_truncate(struct dentry *dentry, loff_t size) |
| 1520 | +{ |
| 1521 | + int err; |
| 1522 | + struct kstat stat; |
| 1523 | + struct path lowerpath; |
| 1524 | + struct dentry *parent = dget_parent(dentry); |
| 1525 | + |
| 1526 | + err = ovl_copy_up(parent); |
| 1527 | + if (err) |
| 1528 | + goto out_dput_parent; |
| 1529 | + |
| 1530 | + ovl_path_lower(dentry, &lowerpath); |
| 1531 | + err = vfs_getattr(lowerpath.mnt, lowerpath.dentry, &stat); |
| 1532 | + if (err) |
| 1533 | + goto out_dput_parent; |
| 1534 | + |
| 1535 | + if (size < stat.size) |
| 1536 | + stat.size = size; |
| 1537 | + |
| 1538 | + err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat); |
| 1539 | + |
| 1540 | +out_dput_parent: |
| 1541 | + dput(parent); |
| 1542 | + return err; |
| 1543 | +} |
| 1544 | + |
| 1545 | +static int ovl_setattr(struct dentry *dentry, struct iattr *attr) |
| 1546 | +{ |
| 1547 | + struct dentry *upperdentry; |
| 1548 | + int err; |
| 1549 | + |
| 1550 | + if ((attr->ia_valid & ATTR_SIZE) && !ovl_dentry_upper(dentry)) |
| 1551 | + err = ovl_copy_up_truncate(dentry, attr->ia_size); |
| 1552 | + else |
| 1553 | + err = ovl_copy_up(dentry); |
| 1554 | + if (err) |
| 1555 | + return err; |
| 1556 | + |
| 1557 | + upperdentry = ovl_dentry_upper(dentry); |
| 1558 | + |
| 1559 | + if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID)) |
| 1560 | + attr->ia_valid &= ~ATTR_MODE; |
| 1561 | + |
| 1562 | + mutex_lock(&upperdentry->d_inode->i_mutex); |
| 1563 | + err = notify_change(upperdentry, attr); |
| 1564 | + mutex_unlock(&upperdentry->d_inode->i_mutex); |
| 1565 | + |
| 1566 | + return err; |
| 1567 | +} |
| 1568 | + |
| 1569 | +static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 1570 | + struct kstat *stat) |
| 1571 | +{ |
| 1572 | + struct path realpath; |
| 1573 | + |
| 1574 | + ovl_path_real(dentry, &realpath); |
| 1575 | + return vfs_getattr(realpath.mnt, realpath.dentry, stat); |
| 1576 | +} |
| 1577 | + |
| 1578 | +static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry, |
| 1579 | + struct kstat *stat) |
| 1580 | +{ |
| 1581 | + int err; |
| 1582 | + enum ovl_path_type type; |
| 1583 | + struct path realpath; |
| 1584 | + |
| 1585 | + type = ovl_path_real(dentry, &realpath); |
| 1586 | + err = vfs_getattr(realpath.mnt, realpath.dentry, stat); |
| 1587 | + if (err) |
| 1588 | + return err; |
| 1589 | + |
| 1590 | + stat->dev = dentry->d_sb->s_dev; |
| 1591 | + stat->ino = dentry->d_inode->i_ino; |
| 1592 | + |
| 1593 | + /* |
| 1594 | + * It's probably not worth it to count subdirs to get the |
| 1595 | + * correct link count. nlink=1 seems to pacify 'find' and |
| 1596 | + * other utilities. |
| 1597 | + */ |
| 1598 | + if (type == OVL_PATH_MERGE) |
| 1599 | + stat->nlink = 1; |
| 1600 | + |
| 1601 | + return 0; |
| 1602 | +} |
| 1603 | + |
| 1604 | +static int ovl_permission(struct inode *inode, int mask, unsigned int flags) |
| 1605 | +{ |
| 1606 | + struct ovl_entry *oe; |
| 1607 | + struct dentry *alias = NULL; |
| 1608 | + struct inode *realinode; |
| 1609 | + struct dentry *realdentry; |
| 1610 | + bool is_upper; |
| 1611 | + int err; |
| 1612 | + |
| 1613 | + if (S_ISDIR(inode->i_mode)) { |
| 1614 | + oe = inode->i_private; |
| 1615 | + } else if (flags & IPERM_FLAG_RCU) { |
| 1616 | + return -ECHILD; |
| 1617 | + } else { |
| 1618 | + /* |
| 1619 | + * For non-directories find an alias and get the info |
| 1620 | + * from there. |
| 1621 | + */ |
| 1622 | + spin_lock(&inode->i_lock); |
| 1623 | + if (WARN_ON(list_empty(&inode->i_dentry))) { |
| 1624 | + spin_unlock(&inode->i_lock); |
| 1625 | + return -ENOENT; |
| 1626 | + } |
| 1627 | + alias = list_entry(inode->i_dentry.next, struct dentry, d_alias); |
| 1628 | + dget(alias); |
| 1629 | + spin_unlock(&inode->i_lock); |
| 1630 | + oe = alias->d_fsdata; |
| 1631 | + } |
| 1632 | + |
| 1633 | + realdentry = ovl_upperdentry_dereference(oe); |
| 1634 | + is_upper = true; |
| 1635 | + if (!realdentry) { |
| 1636 | + realdentry = oe->lowerdentry; |
| 1637 | + is_upper = false; |
| 1638 | + } |
| 1639 | + |
| 1640 | + /* Careful in RCU walk mode */ |
| 1641 | + realinode = ACCESS_ONCE(realdentry->d_inode); |
| 1642 | + if (!realinode) { |
| 1643 | + WARN_ON(!(flags & IPERM_FLAG_RCU)); |
| 1644 | + return -ENOENT; |
| 1645 | + } |
| 1646 | + |
| 1647 | + if (mask & MAY_WRITE) { |
| 1648 | + umode_t mode = realinode->i_mode; |
| 1649 | + |
| 1650 | + /* |
| 1651 | + * Writes will always be redirected to upper layer, so |
| 1652 | + * ignore lower layer being read-only. |
| 1653 | + */ |
| 1654 | + err = -EROFS; |
| 1655 | + if (is_upper && IS_RDONLY(realinode) && |
| 1656 | + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) |
| 1657 | + goto out_dput; |
| 1658 | + |
| 1659 | + /* |
| 1660 | + * Nobody gets write access to an immutable file. |
| 1661 | + */ |
| 1662 | + err = -EACCES; |
| 1663 | + if (IS_IMMUTABLE(realinode)) |
| 1664 | + goto out_dput; |
| 1665 | + } |
| 1666 | + |
| 1667 | + if (realinode->i_op->permission) |
| 1668 | + err = realinode->i_op->permission(realinode, mask, flags); |
| 1669 | + else |
| 1670 | + err = generic_permission(realinode, mask, flags, |
| 1671 | + realinode->i_op->check_acl); |
| 1672 | +out_dput: |
| 1673 | + dput(alias); |
| 1674 | + return err; |
| 1675 | +} |
| 1676 | + |
| 1677 | +static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev, |
| 1678 | + const char *link) |
| 1679 | +{ |
| 1680 | + int err; |
| 1681 | + struct dentry *newdentry; |
| 1682 | + struct dentry *upperdir; |
| 1683 | + struct inode *inode; |
| 1684 | + struct kstat stat = { |
| 1685 | + .mode = mode, |
| 1686 | + .rdev = rdev, |
| 1687 | + }; |
| 1688 | + |
| 1689 | + err = -ENOMEM; |
| 1690 | + inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata); |
| 1691 | + if (!inode) |
| 1692 | + goto out; |
| 1693 | + |
| 1694 | + err = ovl_copy_up(dentry->d_parent); |
| 1695 | + if (err) |
| 1696 | + goto out_iput; |
| 1697 | + |
| 1698 | + upperdir = ovl_dentry_upper(dentry->d_parent); |
| 1699 | + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT); |
| 1700 | + |
| 1701 | + newdentry = ovl_upper_create(upperdir, dentry, &stat, link); |
| 1702 | + err = PTR_ERR(newdentry); |
| 1703 | + if (IS_ERR(newdentry)) |
| 1704 | + goto out_unlock; |
| 1705 | + |
| 1706 | + ovl_dentry_version_inc(dentry->d_parent); |
| 1707 | + if (ovl_dentry_is_opaque(dentry) && S_ISDIR(mode)) { |
| 1708 | + err = ovl_set_opaque(newdentry); |
| 1709 | + if (err) |
| 1710 | + goto out_dput; |
| 1711 | + } |
| 1712 | + ovl_dentry_update(dentry, newdentry); |
| 1713 | + d_instantiate(dentry, inode); |
| 1714 | + inode = NULL; |
| 1715 | + newdentry = NULL; |
| 1716 | + err = 0; |
| 1717 | + |
| 1718 | +out_dput: |
| 1719 | + dput(newdentry); |
| 1720 | +out_unlock: |
| 1721 | + mutex_unlock(&upperdir->d_inode->i_mutex); |
| 1722 | +out_iput: |
| 1723 | + iput(inode); |
| 1724 | +out: |
| 1725 | + return err; |
| 1726 | +} |
| 1727 | + |
| 1728 | +static int ovl_create(struct inode *dir, struct dentry *dentry, int mode, |
| 1729 | + struct nameidata *nd) |
| 1730 | +{ |
| 1731 | + return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL); |
| 1732 | +} |
| 1733 | + |
| 1734 | +static int ovl_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 1735 | +{ |
| 1736 | + return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL); |
| 1737 | +} |
| 1738 | + |
| 1739 | +static int ovl_mknod(struct inode *dir, struct dentry *dentry, int mode, |
| 1740 | + dev_t rdev) |
| 1741 | +{ |
| 1742 | + return ovl_create_object(dentry, mode, rdev, NULL); |
| 1743 | +} |
| 1744 | + |
| 1745 | +static int ovl_symlink(struct inode *dir, struct dentry *dentry, |
| 1746 | + const char *link) |
| 1747 | +{ |
| 1748 | + return ovl_create_object(dentry, S_IFLNK, 0, link); |
| 1749 | +} |
| 1750 | + |
| 1751 | +struct ovl_link_data { |
| 1752 | + struct dentry *realdentry; |
| 1753 | + void *cookie; |
| 1754 | +}; |
| 1755 | + |
| 1756 | +static void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd) |
| 1757 | +{ |
| 1758 | + void *ret; |
| 1759 | + struct dentry *realdentry; |
| 1760 | + struct inode *realinode; |
| 1761 | + |
| 1762 | + realdentry = ovl_dentry_real(dentry); |
| 1763 | + realinode = realdentry->d_inode; |
| 1764 | + |
| 1765 | + if (WARN_ON(!realinode->i_op->follow_link)) |
| 1766 | + return ERR_PTR(-EPERM); |
| 1767 | + |
| 1768 | + ret = realinode->i_op->follow_link(realdentry, nd); |
| 1769 | + if (IS_ERR(ret)) |
| 1770 | + return ret; |
| 1771 | + |
| 1772 | + if (realinode->i_op->put_link) { |
| 1773 | + struct ovl_link_data *data; |
| 1774 | + |
| 1775 | + data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL); |
| 1776 | + if (!data) { |
| 1777 | + realinode->i_op->put_link(realdentry, nd, ret); |
| 1778 | + return ERR_PTR(-ENOMEM); |
| 1779 | + } |
| 1780 | + data->realdentry = realdentry; |
| 1781 | + data->cookie = ret; |
| 1782 | + |
| 1783 | + return data; |
| 1784 | + } else { |
| 1785 | + return NULL; |
| 1786 | + } |
| 1787 | +} |
| 1788 | + |
| 1789 | +static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, void *c) |
| 1790 | +{ |
| 1791 | + struct inode *realinode; |
| 1792 | + struct ovl_link_data *data = c; |
| 1793 | + |
| 1794 | + if (!data) |
| 1795 | + return; |
| 1796 | + |
| 1797 | + realinode = data->realdentry->d_inode; |
| 1798 | + realinode->i_op->put_link(data->realdentry, nd, data->cookie); |
| 1799 | + kfree(data); |
| 1800 | +} |
| 1801 | + |
| 1802 | +static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz) |
| 1803 | +{ |
| 1804 | + struct path realpath; |
| 1805 | + struct inode *realinode; |
| 1806 | + |
| 1807 | + ovl_path_real(dentry, &realpath); |
| 1808 | + realinode = realpath.dentry->d_inode; |
| 1809 | + |
| 1810 | + if (!realinode->i_op->readlink) |
| 1811 | + return -EINVAL; |
| 1812 | + |
| 1813 | + touch_atime(realpath.mnt, realpath.dentry); |
| 1814 | + |
| 1815 | + return realinode->i_op->readlink(realpath.dentry, buf, bufsiz); |
| 1816 | +} |
| 1817 | + |
| 1818 | +static int ovl_whiteout(struct dentry *upperdir, struct dentry *dentry) |
| 1819 | +{ |
| 1820 | + int err; |
| 1821 | + struct dentry *newdentry; |
| 1822 | + const struct cred *old_cred; |
| 1823 | + struct cred *override_cred; |
| 1824 | + |
| 1825 | + /* FIXME: recheck lower dentry to see if whiteout is really needed */ |
| 1826 | + |
| 1827 | + err = -ENOMEM; |
| 1828 | + override_cred = prepare_creds(); |
| 1829 | + if (!override_cred) |
| 1830 | + goto out; |
| 1831 | + |
| 1832 | + /* |
| 1833 | + * CAP_SYS_ADMIN for setxattr |
| 1834 | + * CAP_DAC_OVERRIDE for symlink creation |
| 1835 | + */ |
| 1836 | + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN); |
| 1837 | + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE); |
| 1838 | + override_cred->fsuid = 0; |
| 1839 | + override_cred->fsgid = 0; |
| 1840 | + old_cred = override_creds(override_cred); |
| 1841 | + |
| 1842 | + newdentry = lookup_one_len(dentry->d_name.name, upperdir, |
| 1843 | + dentry->d_name.len); |
| 1844 | + err = PTR_ERR(newdentry); |
| 1845 | + if (IS_ERR(newdentry)) |
| 1846 | + goto out_put_cred; |
| 1847 | + |
| 1848 | + /* Just been removed within the same locked region */ |
| 1849 | + WARN_ON(newdentry->d_inode); |
| 1850 | + |
| 1851 | + err = vfs_symlink(upperdir->d_inode, newdentry, ovl_whiteout_symlink); |
| 1852 | + if (err) |
| 1853 | + goto out_dput; |
| 1854 | + |
| 1855 | + ovl_dentry_version_inc(dentry->d_parent); |
| 1856 | + |
| 1857 | + err = vfs_setxattr(newdentry, ovl_whiteout_xattr, "y", 1, 0); |
| 1858 | + |
| 1859 | +out_dput: |
| 1860 | + dput(newdentry); |
| 1861 | +out_put_cred: |
| 1862 | + revert_creds(old_cred); |
| 1863 | + put_cred(override_cred); |
| 1864 | +out: |
| 1865 | + return err; |
| 1866 | +} |
| 1867 | + |
| 1868 | +static int ovl_do_remove(struct dentry *dentry, bool is_dir) |
| 1869 | +{ |
| 1870 | + int err; |
| 1871 | + enum ovl_path_type type; |
| 1872 | + struct path realpath; |
| 1873 | + struct dentry *upperdir; |
| 1874 | + |
| 1875 | + err = ovl_copy_up(dentry->d_parent); |
| 1876 | + if (err) |
| 1877 | + return err; |
| 1878 | + |
| 1879 | + upperdir = ovl_dentry_upper(dentry->d_parent); |
| 1880 | + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT); |
| 1881 | + type = ovl_path_real(dentry, &realpath); |
| 1882 | + if (type != OVL_PATH_LOWER) { |
| 1883 | + err = -ESTALE; |
| 1884 | + if (realpath.dentry->d_parent != upperdir) |
| 1885 | + goto out_d_drop; |
| 1886 | + |
| 1887 | + if (is_dir) |
| 1888 | + err = vfs_rmdir(upperdir->d_inode, realpath.dentry); |
| 1889 | + else |
| 1890 | + err = vfs_unlink(upperdir->d_inode, realpath.dentry); |
| 1891 | + if (err) |
| 1892 | + goto out_d_drop; |
| 1893 | + |
| 1894 | + ovl_dentry_version_inc(dentry->d_parent); |
| 1895 | + } |
| 1896 | + |
| 1897 | + if (type != OVL_PATH_UPPER || ovl_dentry_is_opaque(dentry)) |
| 1898 | + err = ovl_whiteout(upperdir, dentry); |
| 1899 | + |
| 1900 | + /* |
| 1901 | + * Keeping this dentry hashed would mean having to release |
| 1902 | + * upperpath/lowerpath, which could only be done if we are the |
| 1903 | + * sole user of this dentry. Too tricky... Just unhash for |
| 1904 | + * now. |
| 1905 | + */ |
| 1906 | +out_d_drop: |
| 1907 | + d_drop(dentry); |
| 1908 | + mutex_unlock(&upperdir->d_inode->i_mutex); |
| 1909 | + |
| 1910 | + return err; |
| 1911 | +} |
| 1912 | + |
| 1913 | +static int ovl_unlink(struct inode *dir, struct dentry *dentry) |
| 1914 | +{ |
| 1915 | + return ovl_do_remove(dentry, false); |
| 1916 | +} |
| 1917 | + |
| 1918 | +static int ovl_check_empty_dir(struct dentry *dentry) |
| 1919 | +{ |
| 1920 | + int err; |
| 1921 | + struct path lowerpath; |
| 1922 | + struct path upperpath; |
| 1923 | + struct ovl_cache_entry *p; |
| 1924 | + LIST_HEAD(list); |
| 1925 | + struct ovl_readdir_data rdd = { .list = &list }; |
| 1926 | + |
| 1927 | + ovl_path_upper(dentry, &upperpath); |
| 1928 | + ovl_path_lower(dentry, &lowerpath); |
| 1929 | + |
| 1930 | + err = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd); |
| 1931 | + if (err) |
| 1932 | + return err; |
| 1933 | + |
| 1934 | + err = 0; |
| 1935 | + |
| 1936 | + list_for_each_entry(p, &list, l_node) { |
| 1937 | + if (p->is_whiteout) |
| 1938 | + continue; |
| 1939 | + |
| 1940 | + if (p->name[0] == '.') { |
| 1941 | + if (p->len == 1) |
| 1942 | + continue; |
| 1943 | + if (p->len == 2 && p->name[1] == '.') |
| 1944 | + continue; |
| 1945 | + } |
| 1946 | + err = -ENOTEMPTY; |
| 1947 | + break; |
| 1948 | + } |
| 1949 | + |
| 1950 | + ovl_cache_free(&list); |
| 1951 | + |
| 1952 | + return err; |
| 1953 | +} |
| 1954 | + |
| 1955 | +static int ovl_unlink_whiteout(void *buf, const char *name, int namelen, |
| 1956 | + loff_t offset, u64 ino, unsigned int d_type) |
| 1957 | +{ |
| 1958 | + struct ovl_readdir_data *rdd = buf; |
| 1959 | + |
| 1960 | + rdd->count++; |
| 1961 | + /* check d_type to filter out "." and ".." */ |
| 1962 | + if (d_type == DT_LNK) { |
| 1963 | + struct dentry *dentry; |
| 1964 | + |
| 1965 | + dentry = lookup_one_len(name, rdd->dir, namelen); |
| 1966 | + if (IS_ERR(dentry)) { |
| 1967 | + rdd->err = PTR_ERR(dentry); |
| 1968 | + } else { |
| 1969 | + rdd->err = vfs_unlink(rdd->dir->d_inode, dentry); |
| 1970 | + dput(dentry); |
| 1971 | + } |
| 1972 | + } |
| 1973 | + |
| 1974 | + return rdd->err; |
| 1975 | +} |
| 1976 | + |
| 1977 | +static int ovl_remove_whiteouts(struct dentry *dentry) |
| 1978 | +{ |
| 1979 | + struct path upperpath; |
| 1980 | + struct ovl_readdir_data rdd = { .list = NULL }; |
| 1981 | + |
| 1982 | + ovl_path_upper(dentry, &upperpath); |
| 1983 | + rdd.dir = upperpath.dentry; |
| 1984 | + |
| 1985 | + return ovl_dir_read(&upperpath, &rdd, ovl_unlink_whiteout); |
| 1986 | +} |
| 1987 | + |
| 1988 | +static int ovl_rmdir(struct inode *dir, struct dentry *dentry) |
| 1989 | +{ |
| 1990 | + int err; |
| 1991 | + enum ovl_path_type type; |
| 1992 | + |
| 1993 | + type = ovl_path_type(dentry); |
| 1994 | + if (type != OVL_PATH_UPPER) { |
| 1995 | + err = ovl_check_empty_dir(dentry); |
| 1996 | + if (err) |
| 1997 | + return err; |
| 1998 | + |
| 1999 | + if (type == OVL_PATH_MERGE) { |
| 2000 | + err = ovl_remove_whiteouts(dentry); |
| 2001 | + if (err) |
| 2002 | + return err; |
| 2003 | + } |
| 2004 | + } |
| 2005 | + |
| 2006 | + return ovl_do_remove(dentry, true); |
| 2007 | +} |
| 2008 | + |
| 2009 | +static int ovl_link(struct dentry *old, struct inode *newdir, |
| 2010 | + struct dentry *new) |
| 2011 | +{ |
| 2012 | + int err; |
| 2013 | + struct dentry *olddentry; |
| 2014 | + struct dentry *newdentry; |
| 2015 | + struct dentry *upperdir; |
| 2016 | + |
| 2017 | + err = ovl_copy_up(old); |
| 2018 | + if (err) |
| 2019 | + goto out; |
| 2020 | + |
| 2021 | + err = ovl_copy_up(new->d_parent); |
| 2022 | + if (err) |
| 2023 | + goto out; |
| 2024 | + |
| 2025 | + upperdir = ovl_dentry_upper(new->d_parent); |
| 2026 | + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT); |
| 2027 | + newdentry = ovl_lookup_create(upperdir, new); |
| 2028 | + err = PTR_ERR(newdentry); |
| 2029 | + if (IS_ERR(newdentry)) |
| 2030 | + goto out_unlock; |
| 2031 | + |
| 2032 | + olddentry = ovl_dentry_upper(old); |
| 2033 | + err = vfs_link(olddentry, upperdir->d_inode, newdentry); |
| 2034 | + if (!err) { |
| 2035 | + ovl_dentry_version_inc(new->d_parent); |
| 2036 | + ovl_dentry_update(new, newdentry); |
| 2037 | + |
| 2038 | + ihold(old->d_inode); |
| 2039 | + d_instantiate(new, old->d_inode); |
| 2040 | + } else { |
| 2041 | + dput(newdentry); |
| 2042 | + } |
| 2043 | +out_unlock: |
| 2044 | + mutex_unlock(&upperdir->d_inode->i_mutex); |
| 2045 | +out: |
| 2046 | + return err; |
| 2047 | + |
| 2048 | +} |
| 2049 | + |
| 2050 | +static int ovl_rename(struct inode *olddir, struct dentry *old, |
| 2051 | + struct inode *newdir, struct dentry *new) |
| 2052 | +{ |
| 2053 | + int err; |
| 2054 | + enum ovl_path_type old_type; |
| 2055 | + struct dentry *old_upperdir; |
| 2056 | + struct dentry *new_upperdir; |
| 2057 | + struct dentry *olddentry; |
| 2058 | + struct dentry *newdentry; |
| 2059 | + struct dentry *trap; |
| 2060 | + bool is_dir = S_ISDIR(old->d_inode->i_mode); |
| 2061 | + |
| 2062 | + /* Don't copy up directory trees */ |
| 2063 | + old_type = ovl_path_type(old); |
| 2064 | + if (old_type != OVL_PATH_UPPER && is_dir) |
| 2065 | + return -EXDEV; |
| 2066 | + |
| 2067 | + if (new->d_inode) { |
| 2068 | + enum ovl_path_type new_type; |
| 2069 | + |
| 2070 | + new_type = ovl_path_type(new); |
| 2071 | + |
| 2072 | + if (new_type == OVL_PATH_LOWER && old_type == OVL_PATH_LOWER) { |
| 2073 | + if (ovl_dentry_lower(old)->d_inode == |
| 2074 | + ovl_dentry_lower(new)->d_inode) |
| 2075 | + return 0; |
| 2076 | + } |
| 2077 | + if (new_type != OVL_PATH_LOWER && old_type != OVL_PATH_LOWER) { |
| 2078 | + if (ovl_dentry_upper(old)->d_inode == |
| 2079 | + ovl_dentry_upper(new)->d_inode) |
| 2080 | + return 0; |
| 2081 | + } |
| 2082 | + |
| 2083 | + if (new_type != OVL_PATH_UPPER && |
| 2084 | + S_ISDIR(new->d_inode->i_mode)) { |
| 2085 | + err = ovl_check_empty_dir(new); |
| 2086 | + if (err) |
| 2087 | + return err; |
| 2088 | + |
| 2089 | + if (new_type == OVL_PATH_MERGE) { |
| 2090 | + err = ovl_remove_whiteouts(new); |
| 2091 | + if (err) |
| 2092 | + return err; |
| 2093 | + } |
| 2094 | + } |
| 2095 | + } |
| 2096 | + |
| 2097 | + err = ovl_copy_up(old); |
| 2098 | + if (err) |
| 2099 | + return err; |
| 2100 | + |
| 2101 | + err = ovl_copy_up(new->d_parent); |
| 2102 | + if (err) |
| 2103 | + return err; |
| 2104 | + |
| 2105 | + old_upperdir = ovl_dentry_upper(old->d_parent); |
| 2106 | + new_upperdir = ovl_dentry_upper(new->d_parent); |
| 2107 | + |
| 2108 | + trap = lock_rename(new_upperdir, old_upperdir); |
| 2109 | + |
| 2110 | + olddentry = ovl_dentry_upper(old); |
| 2111 | + newdentry = ovl_dentry_upper(new); |
| 2112 | + if (newdentry) { |
| 2113 | + dget(newdentry); |
| 2114 | + } else { |
| 2115 | + newdentry = ovl_lookup_create(new_upperdir, new); |
| 2116 | + err = PTR_ERR(newdentry); |
| 2117 | + if (IS_ERR(newdentry)) |
| 2118 | + goto out_unlock; |
| 2119 | + } |
| 2120 | + |
| 2121 | + err = -ESTALE; |
| 2122 | + if (olddentry->d_parent != old_upperdir) |
| 2123 | + goto out_dput; |
| 2124 | + if (newdentry->d_parent != new_upperdir) |
| 2125 | + goto out_dput; |
| 2126 | + if (olddentry == trap) |
| 2127 | + goto out_dput; |
| 2128 | + if (newdentry == trap) |
| 2129 | + goto out_dput; |
| 2130 | + |
| 2131 | + err = vfs_rename(old_upperdir->d_inode, olddentry, |
| 2132 | + new_upperdir->d_inode, newdentry); |
| 2133 | + |
| 2134 | + if (!err) { |
| 2135 | + bool old_opaque = ovl_dentry_is_opaque(old); |
| 2136 | + bool new_opaque = ovl_dentry_is_opaque(new); |
| 2137 | + |
| 2138 | + if (ovl_path_type(new) != OVL_PATH_UPPER) |
| 2139 | + new_opaque = true; |
| 2140 | + |
| 2141 | + if (old_type != OVL_PATH_UPPER || old_opaque) |
| 2142 | + err = ovl_whiteout(old_upperdir, old); |
| 2143 | + if (!err && is_dir) { |
| 2144 | + if (old_opaque && !new_opaque) { |
| 2145 | + ovl_remove_opaque(olddentry); |
| 2146 | + ovl_dentry_set_opaque(old, false); |
| 2147 | + } |
| 2148 | + if (!old_opaque && new_opaque) { |
| 2149 | + err = ovl_set_opaque(olddentry); |
| 2150 | + ovl_dentry_set_opaque(old, true); |
| 2151 | + } |
| 2152 | + } |
| 2153 | + ovl_dentry_version_inc(old->d_parent); |
| 2154 | + ovl_dentry_version_inc(new->d_parent); |
| 2155 | + } |
| 2156 | + |
| 2157 | +out_dput: |
| 2158 | + dput(newdentry); |
| 2159 | +out_unlock: |
| 2160 | + unlock_rename(new_upperdir, old_upperdir); |
| 2161 | + return err; |
| 2162 | +} |
| 2163 | + |
| 2164 | +static bool ovl_is_private_xattr(const char *name) |
| 2165 | +{ |
| 2166 | + return strncmp(name, "trusted.overlay.", 14) == 0; |
| 2167 | +} |
| 2168 | + |
| 2169 | +static int ovl_setxattr(struct dentry *dentry, const char *name, |
| 2170 | + const void *value, size_t size, int flags) |
| 2171 | +{ |
| 2172 | + int err; |
| 2173 | + struct dentry *upperdentry; |
| 2174 | + |
| 2175 | + if (ovl_is_private_xattr(name)) |
| 2176 | + return -EPERM; |
| 2177 | + |
| 2178 | + err = ovl_copy_up(dentry); |
| 2179 | + if (err) |
| 2180 | + return err; |
| 2181 | + |
| 2182 | + upperdentry = ovl_dentry_upper(dentry); |
| 2183 | + return vfs_setxattr(upperdentry, name, value, size, flags); |
| 2184 | +} |
| 2185 | + |
| 2186 | +static ssize_t ovl_getxattr(struct dentry *dentry, const char *name, |
| 2187 | + void *value, size_t size) |
| 2188 | +{ |
| 2189 | + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE && |
| 2190 | + ovl_is_private_xattr(name)) |
| 2191 | + return -ENODATA; |
| 2192 | + |
| 2193 | + return vfs_getxattr(ovl_dentry_real(dentry), name, value, size); |
| 2194 | +} |
| 2195 | + |
| 2196 | +static ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size) |
| 2197 | +{ |
| 2198 | + ssize_t res; |
| 2199 | + int off; |
| 2200 | + |
| 2201 | + res = vfs_listxattr(ovl_dentry_real(dentry), list, size); |
| 2202 | + if (res <= 0 || size == 0) |
| 2203 | + return res; |
| 2204 | + |
| 2205 | + if (ovl_path_type(dentry->d_parent) != OVL_PATH_MERGE) |
| 2206 | + return res; |
| 2207 | + |
| 2208 | + /* filter out private xattrs */ |
| 2209 | + for (off = 0; off < res;) { |
| 2210 | + char *s = list + off; |
| 2211 | + size_t slen = strlen(s) + 1; |
| 2212 | + |
| 2213 | + BUG_ON(off + slen > res); |
| 2214 | + |
| 2215 | + if (ovl_is_private_xattr(s)) { |
| 2216 | + res -= slen; |
| 2217 | + memmove(s, s + slen, res - off); |
| 2218 | + } else { |
| 2219 | + off += slen; |
| 2220 | + } |
| 2221 | + } |
| 2222 | + |
| 2223 | + return res; |
| 2224 | +} |
| 2225 | + |
| 2226 | +static int ovl_removexattr(struct dentry *dentry, const char *name) |
| 2227 | +{ |
| 2228 | + int err; |
| 2229 | + struct path realpath; |
| 2230 | + enum ovl_path_type type; |
| 2231 | + |
| 2232 | + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE && |
| 2233 | + ovl_is_private_xattr(name)) |
| 2234 | + return -ENODATA; |
| 2235 | + |
| 2236 | + type = ovl_path_real(dentry, &realpath); |
| 2237 | + if (type == OVL_PATH_LOWER) { |
| 2238 | + err = vfs_getxattr(realpath.dentry, name, NULL, 0); |
| 2239 | + if (err < 0) |
| 2240 | + return err; |
| 2241 | + |
| 2242 | + err = ovl_copy_up(dentry); |
| 2243 | + if (err) |
| 2244 | + return err; |
| 2245 | + |
| 2246 | + ovl_path_upper(dentry, &realpath); |
| 2247 | + } |
| 2248 | + |
| 2249 | + return vfs_removexattr(realpath.dentry, name); |
| 2250 | +} |
| 2251 | + |
| 2252 | +static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type, |
| 2253 | + struct dentry *realdentry) |
| 2254 | +{ |
| 2255 | + if (type != OVL_PATH_LOWER) |
| 2256 | + return false; |
| 2257 | + |
| 2258 | + if (special_file(realdentry->d_inode->i_mode)) |
| 2259 | + return false; |
| 2260 | + |
| 2261 | + if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC)) |
| 2262 | + return false; |
| 2263 | + |
| 2264 | + return true; |
| 2265 | +} |
| 2266 | + |
| 2267 | +static struct file *ovl_open(struct dentry *dentry, int flags, |
| 2268 | + const struct cred *cred) |
| 2269 | +{ |
| 2270 | + int err; |
| 2271 | + struct path realpath; |
| 2272 | + enum ovl_path_type type; |
| 2273 | + |
| 2274 | + type = ovl_path_real(dentry, &realpath); |
| 2275 | + if (ovl_open_need_copy_up(flags, type, realpath.dentry)) { |
| 2276 | + if (flags & O_TRUNC) |
| 2277 | + err = ovl_copy_up_truncate(dentry, 0); |
| 2278 | + else |
| 2279 | + err = ovl_copy_up(dentry); |
| 2280 | + if (err) |
| 2281 | + return ERR_PTR(err); |
| 2282 | + |
| 2283 | + ovl_path_upper(dentry, &realpath); |
| 2284 | + } |
| 2285 | + |
| 2286 | + return vfs_open(&realpath, flags, cred); |
| 2287 | +} |
| 2288 | + |
| 2289 | +static const struct inode_operations ovl_dir_inode_operations = { |
| 2290 | + .lookup = ovl_lookup, |
| 2291 | + .mkdir = ovl_mkdir, |
| 2292 | + .symlink = ovl_symlink, |
| 2293 | + .unlink = ovl_unlink, |
| 2294 | + .rmdir = ovl_rmdir, |
| 2295 | + .rename = ovl_rename, |
| 2296 | + .link = ovl_link, |
| 2297 | + .setattr = ovl_setattr, |
| 2298 | + .create = ovl_create, |
| 2299 | + .mknod = ovl_mknod, |
| 2300 | + .permission = ovl_permission, |
| 2301 | + .getattr = ovl_dir_getattr, |
| 2302 | + .setxattr = ovl_setxattr, |
| 2303 | + .getxattr = ovl_getxattr, |
| 2304 | + .listxattr = ovl_listxattr, |
| 2305 | + .removexattr = ovl_removexattr, |
| 2306 | +}; |
| 2307 | + |
| 2308 | +static const struct inode_operations ovl_file_inode_operations = { |
| 2309 | + .setattr = ovl_setattr, |
| 2310 | + .permission = ovl_permission, |
| 2311 | + .getattr = ovl_getattr, |
| 2312 | + .setxattr = ovl_setxattr, |
| 2313 | + .getxattr = ovl_getxattr, |
| 2314 | + .listxattr = ovl_listxattr, |
| 2315 | + .removexattr = ovl_removexattr, |
| 2316 | + .open = ovl_open, |
| 2317 | +}; |
| 2318 | + |
| 2319 | +static const struct inode_operations ovl_symlink_inode_operations = { |
| 2320 | + .setattr = ovl_setattr, |
| 2321 | + .follow_link = ovl_follow_link, |
| 2322 | + .put_link = ovl_put_link, |
| 2323 | + .readlink = ovl_readlink, |
| 2324 | + .getattr = ovl_getattr, |
| 2325 | + .setxattr = ovl_setxattr, |
| 2326 | + .getxattr = ovl_getxattr, |
| 2327 | + .listxattr = ovl_listxattr, |
| 2328 | + .removexattr = ovl_removexattr, |
| 2329 | +}; |
| 2330 | + |
| 2331 | +static struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, |
| 2332 | + struct ovl_entry *oe) |
| 2333 | +{ |
| 2334 | + struct inode *inode; |
| 2335 | + |
| 2336 | + inode = new_inode(sb); |
| 2337 | + if (!inode) |
| 2338 | + return NULL; |
| 2339 | + |
| 2340 | + mode &= S_IFMT; |
| 2341 | + |
| 2342 | + inode->i_ino = get_next_ino(); |
| 2343 | + inode->i_mode = mode; |
| 2344 | + inode->i_flags |= S_NOATIME | S_NOCMTIME; |
| 2345 | + |
| 2346 | + switch (mode) { |
| 2347 | + case S_IFDIR: |
| 2348 | + inode->i_private = oe; |
| 2349 | + inode->i_op = &ovl_dir_inode_operations; |
| 2350 | + inode->i_fop = &ovl_dir_operations; |
| 2351 | + break; |
| 2352 | + |
| 2353 | + case S_IFLNK: |
| 2354 | + inode->i_op = &ovl_symlink_inode_operations; |
| 2355 | + break; |
| 2356 | + |
| 2357 | + case S_IFREG: |
| 2358 | + case S_IFSOCK: |
| 2359 | + case S_IFBLK: |
| 2360 | + case S_IFCHR: |
| 2361 | + case S_IFIFO: |
| 2362 | + inode->i_op = &ovl_file_inode_operations; |
| 2363 | + break; |
| 2364 | + |
| 2365 | + default: |
| 2366 | + WARN(1, "illegal file type: %i\n", mode); |
| 2367 | + inode = NULL; |
| 2368 | + } |
| 2369 | + |
| 2370 | + return inode; |
| 2371 | + |
| 2372 | +} |
| 2373 | + |
| 2374 | +static void ovl_put_super(struct super_block *sb) |
| 2375 | +{ |
| 2376 | + struct ovl_fs *ufs = sb->s_fs_info; |
| 2377 | + |
| 2378 | + if (!(sb->s_flags & MS_RDONLY)) |
| 2379 | + mnt_drop_write(ufs->upper_mnt); |
| 2380 | + |
| 2381 | + mntput(ufs->upper_mnt); |
| 2382 | + mntput(ufs->lower_mnt); |
| 2383 | + |
| 2384 | + kfree(ufs); |
| 2385 | +} |
| 2386 | + |
| 2387 | +static int ovl_remount_fs(struct super_block *sb, int *flagsp, char *data) |
| 2388 | +{ |
| 2389 | + int flags = *flagsp; |
| 2390 | + struct ovl_fs *ufs = sb->s_fs_info; |
| 2391 | + |
| 2392 | + /* When remounting rw or ro, we need to adjust the write access to the |
| 2393 | + * upper fs. |
| 2394 | + */ |
| 2395 | + if (((flags ^ sb->s_flags) & MS_RDONLY) == 0) |
| 2396 | + /* No change to readonly status */ |
| 2397 | + return 0; |
| 2398 | + |
| 2399 | + if (flags & MS_RDONLY) { |
| 2400 | + mnt_drop_write(ufs->upper_mnt); |
| 2401 | + return 0; |
| 2402 | + } else |
| 2403 | + return mnt_want_write(ufs->upper_mnt); |
| 2404 | +} |
| 2405 | + |
| 2406 | +/** |
| 2407 | + * ovl_statfs |
| 2408 | + * @sb: The overlayfs super block |
| 2409 | + * @buf: The struct kstatfs to fill in with stats |
| 2410 | + * |
| 2411 | + * Get the filesystem statistics. As writes always target the upper layer |
| 2412 | + * filesystem pass the statfs to the same filesystem. |
| 2413 | + */ |
| 2414 | +static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf) |
| 2415 | +{ |
| 2416 | + struct dentry *root_dentry = dentry->d_sb->s_root; |
| 2417 | + struct path path; |
| 2418 | + ovl_path_upper(root_dentry, &path); |
| 2419 | + |
| 2420 | + if (!path.dentry->d_sb->s_op->statfs) |
| 2421 | + return -ENOSYS; |
| 2422 | + return path.dentry->d_sb->s_op->statfs(path.dentry, buf); |
| 2423 | +} |
| 2424 | + |
| 2425 | +static const struct super_operations ovl_super_operations = { |
| 2426 | + .put_super = ovl_put_super, |
| 2427 | + .remount_fs = ovl_remount_fs, |
| 2428 | + .statfs = ovl_statfs, |
| 2429 | +}; |
| 2430 | + |
| 2431 | +struct ovl_config { |
| 2432 | + char *lowerdir; |
| 2433 | + char *upperdir; |
| 2434 | +}; |
| 2435 | + |
| 2436 | +enum { |
| 2437 | + Opt_lowerdir, |
| 2438 | + Opt_upperdir, |
| 2439 | + Opt_err, |
| 2440 | +}; |
| 2441 | + |
| 2442 | +static const match_table_t ovl_tokens = { |
| 2443 | + {Opt_lowerdir, "lowerdir=%s"}, |
| 2444 | + {Opt_upperdir, "upperdir=%s"}, |
| 2445 | + {Opt_err, NULL} |
| 2446 | +}; |
| 2447 | + |
| 2448 | +static int ovl_parse_opt(char *opt, struct ovl_config *config) |
| 2449 | +{ |
| 2450 | + char *p; |
| 2451 | + |
| 2452 | + config->upperdir = NULL; |
| 2453 | + config->lowerdir = NULL; |
| 2454 | + |
| 2455 | + while ((p = strsep(&opt, ",")) != NULL) { |
| 2456 | + int token; |
| 2457 | + substring_t args[MAX_OPT_ARGS]; |
| 2458 | + |
| 2459 | + if (!*p) |
| 2460 | + continue; |
| 2461 | + |
| 2462 | + token = match_token(p, ovl_tokens, args); |
| 2463 | + switch (token) { |
| 2464 | + case Opt_upperdir: |
| 2465 | + kfree(config->upperdir); |
| 2466 | + config->upperdir = match_strdup(&args[0]); |
| 2467 | + if (!config->upperdir) |
| 2468 | + return -ENOMEM; |
| 2469 | + break; |
| 2470 | + |
| 2471 | + case Opt_lowerdir: |
| 2472 | + kfree(config->lowerdir); |
| 2473 | + config->lowerdir = match_strdup(&args[0]); |
| 2474 | + if (!config->lowerdir) |
| 2475 | + return -ENOMEM; |
| 2476 | + break; |
| 2477 | + |
| 2478 | + default: |
| 2479 | + return -EINVAL; |
| 2480 | + } |
| 2481 | + } |
| 2482 | + return 0; |
| 2483 | +} |
| 2484 | + |
| 2485 | +static int ovl_fill_super(struct super_block *sb, void *data, int silent) |
| 2486 | +{ |
| 2487 | + struct path lowerpath; |
| 2488 | + struct path upperpath; |
| 2489 | + struct inode *root_inode; |
| 2490 | + struct dentry *root_dentry; |
| 2491 | + struct ovl_entry *oe; |
| 2492 | + struct ovl_fs *ufs; |
| 2493 | + struct ovl_config config; |
| 2494 | + int err; |
| 2495 | + |
| 2496 | + err = ovl_parse_opt((char *) data, &config); |
| 2497 | + if (err) |
| 2498 | + goto out; |
| 2499 | + |
| 2500 | + err = -EINVAL; |
| 2501 | + if (!config.upperdir || !config.lowerdir) { |
| 2502 | + printk(KERN_ERR "overlayfs: missing upperdir or lowerdir\n"); |
| 2503 | + goto out_free_config; |
| 2504 | + } |
| 2505 | + |
| 2506 | + err = -ENOMEM; |
| 2507 | + ufs = kmalloc(sizeof(struct ovl_fs), GFP_KERNEL); |
| 2508 | + if (!ufs) |
| 2509 | + goto out_free_config; |
| 2510 | + |
| 2511 | + oe = ovl_alloc_entry(); |
| 2512 | + if (oe == NULL) |
| 2513 | + goto out_free_ufs; |
| 2514 | + |
| 2515 | + root_inode = ovl_new_inode(sb, S_IFDIR, oe); |
| 2516 | + if (!root_inode) |
| 2517 | + goto out_free_oe; |
| 2518 | + |
| 2519 | + err = kern_path(config.upperdir, LOOKUP_FOLLOW, &upperpath); |
| 2520 | + if (err) |
| 2521 | + goto out_put_root; |
| 2522 | + |
| 2523 | + err = kern_path(config.lowerdir, LOOKUP_FOLLOW, &lowerpath); |
| 2524 | + if (err) |
| 2525 | + goto out_put_upperpath; |
| 2526 | + |
| 2527 | + err = -ENOTDIR; |
| 2528 | + if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) || |
| 2529 | + !S_ISDIR(lowerpath.dentry->d_inode->i_mode)) |
| 2530 | + goto out_put_lowerpath; |
| 2531 | + |
| 2532 | + ufs->upper_mnt = clone_private_mount(&upperpath); |
| 2533 | + err = PTR_ERR(ufs->upper_mnt); |
| 2534 | + if (IS_ERR(ufs->upper_mnt)) { |
| 2535 | + printk(KERN_ERR "overlayfs: failed to clone upperpath\n"); |
| 2536 | + goto out_put_lowerpath; |
| 2537 | + } |
| 2538 | + |
| 2539 | + ufs->lower_mnt = clone_private_mount(&lowerpath); |
| 2540 | + err = PTR_ERR(ufs->lower_mnt); |
| 2541 | + if (IS_ERR(ufs->lower_mnt)) { |
| 2542 | + printk(KERN_ERR "overlayfs: failed to clone lowerpath\n"); |
| 2543 | + goto out_put_upper_mnt; |
| 2544 | + } |
| 2545 | + |
| 2546 | + if (!(sb->s_flags & MS_RDONLY)) { |
| 2547 | + err = mnt_want_write(ufs->upper_mnt); |
| 2548 | + if (err) |
| 2549 | + goto out_put_lower_mnt; |
| 2550 | + } |
| 2551 | + |
| 2552 | + err = -ENOMEM; |
| 2553 | + root_dentry = d_alloc_root(root_inode); |
| 2554 | + if (!root_dentry) |
| 2555 | + goto out_drop_write; |
| 2556 | + |
| 2557 | + mntput(upperpath.mnt); |
| 2558 | + mntput(lowerpath.mnt); |
| 2559 | + |
| 2560 | + oe->__upperdentry = upperpath.dentry; |
| 2561 | + oe->lowerdentry = lowerpath.dentry; |
| 2562 | + |
| 2563 | + root_dentry->d_fsdata = oe; |
| 2564 | + root_dentry->d_op = &ovl_dentry_operations; |
| 2565 | + |
| 2566 | + sb->s_op = &ovl_super_operations; |
| 2567 | + sb->s_root = root_dentry; |
| 2568 | + sb->s_fs_info = ufs; |
| 2569 | + |
| 2570 | + return 0; |
| 2571 | + |
| 2572 | +out_drop_write: |
| 2573 | + if (!(sb->s_flags & MS_RDONLY)) |
| 2574 | + mnt_drop_write(ufs->upper_mnt); |
| 2575 | +out_put_lower_mnt: |
| 2576 | + mntput(ufs->lower_mnt); |
| 2577 | +out_put_upper_mnt: |
| 2578 | + mntput(ufs->upper_mnt); |
| 2579 | +out_put_lowerpath: |
| 2580 | + path_put(&lowerpath); |
| 2581 | +out_put_upperpath: |
| 2582 | + path_put(&upperpath); |
| 2583 | +out_put_root: |
| 2584 | + iput(root_inode); |
| 2585 | +out_free_oe: |
| 2586 | + kfree(oe); |
| 2587 | +out_free_ufs: |
| 2588 | + kfree(ufs); |
| 2589 | +out_free_config: |
| 2590 | + kfree(config.lowerdir); |
| 2591 | + kfree(config.upperdir); |
| 2592 | +out: |
| 2593 | + return err; |
| 2594 | +} |
| 2595 | + |
| 2596 | +static int ovl_get_sb(struct file_system_type *fs_type, |
| 2597 | + int flags, const char *dev_name, |
| 2598 | + void *raw_data, struct vfsmount *mnt) |
| 2599 | +{ |
| 2600 | + return get_sb_nodev(fs_type, flags, raw_data, ovl_fill_super, mnt); |
| 2601 | +} |
| 2602 | + |
| 2603 | +static struct file_system_type ovl_fs_type = { |
| 2604 | + .owner = THIS_MODULE, |
| 2605 | + .name = "overlayfs", |
| 2606 | + .get_sb = ovl_get_sb, |
| 2607 | + .kill_sb = kill_anon_super, |
| 2608 | +}; |
| 2609 | + |
| 2610 | +static int __init ovl_init(void) |
| 2611 | +{ |
| 2612 | + return register_filesystem(&ovl_fs_type); |
| 2613 | +} |
| 2614 | + |
| 2615 | +static void __exit ovl_exit(void) |
| 2616 | +{ |
| 2617 | + unregister_filesystem(&ovl_fs_type); |
| 2618 | +} |
| 2619 | + |
| 2620 | +module_init(ovl_init); |
| 2621 | +module_exit(ovl_exit); |
| 2622 | --- /dev/null |
| 2623 | +++ b/Documentation/filesystems/overlayfs.txt |
| 2624 | @@ -0,0 +1,163 @@ |
| 2625 | +Written by: Neil Brown <neilb@suse.de> |
| 2626 | + |
| 2627 | +Overlay Filesystem |
| 2628 | +================== |
| 2629 | + |
| 2630 | +This document describes a prototype for a new approach to providing |
| 2631 | +overlay-filesystem functionality in Linux (sometimes referred to as |
| 2632 | +union-filesystems). An overlay-filesystem tries to present a |
| 2633 | +filesystem which is the result over overlaying one filesystem on top |
| 2634 | +of the other. |
| 2635 | + |
| 2636 | +The result will inevitably fail to look exactly like a normal |
| 2637 | +filesystem for various technical reasons. The expectation is that |
| 2638 | +many use cases will be able to ignore these differences. |
| 2639 | + |
| 2640 | +This approach is 'hybrid' because the objects that appear in the |
| 2641 | +filesystem do not all appear to belong to that filesystem. In many |
| 2642 | +case an object accessed in the union will be indistinguishable |
| 2643 | +from accessing the corresponding object from the original filesystem. |
| 2644 | +This is most obvious from the 'st_dev' field returned by stat(2). |
| 2645 | + |
| 2646 | +While directories will report an st_dev for the overlay-filesystem, |
| 2647 | +all non-directory objects will report an st_dev whichever of the |
| 2648 | +'lower' or 'upper' filesystem that is providing the object. Similarly |
| 2649 | +st_ino will only be unique when combined with st_dev, and both of |
| 2650 | +these can change over the lifetime of a non-directory object. Many |
| 2651 | +applications and tools ignore these values and will not be affected. |
| 2652 | + |
| 2653 | +Upper and Lower |
| 2654 | +--------------- |
| 2655 | + |
| 2656 | +An overlay filesystem combines two filesystems - an 'upper' filesystem |
| 2657 | +and a 'lower' filesystem. When a name exists in both filesystems, the |
| 2658 | +object in the 'upper' filesystem is visible while the object in the |
| 2659 | +'lower' filesystem is either hidden or, in the case of directories, |
| 2660 | +merged with the 'upper' object. |
| 2661 | + |
| 2662 | +It would be more correct to refer to an upper and lower 'directory |
| 2663 | +tree' rather than 'filesystem' as it is quite possible for both |
| 2664 | +directory trees to be in the same filesystem and there is no |
| 2665 | +requirement that the root of a filesystem be given for either upper or |
| 2666 | +lower. |
| 2667 | + |
| 2668 | +The lower filesystem can be any filesystem supported by Linux and does |
| 2669 | +not need to be writable. The lower filesystem can even be another |
| 2670 | +overlayfs. The upper filesystem will normally be writable and if it |
| 2671 | +is it must support the creation of trusted.* extended attributes, and |
| 2672 | +must provide valid d_type in readdir responses, at least for symbolic |
| 2673 | +links - so NFS is not suitable. |
| 2674 | + |
| 2675 | +A read-only overlay of two read-only filesystems may use any |
| 2676 | +filesystem type. |
| 2677 | + |
| 2678 | +Directories |
| 2679 | +----------- |
| 2680 | + |
| 2681 | +Overlaying mainly involved directories. If a given name appears in both |
| 2682 | +upper and lower filesystems and refers to a non-directory in either, |
| 2683 | +then the lower object is hidden - the name refers only to the upper |
| 2684 | +object. |
| 2685 | + |
| 2686 | +Where both upper and lower objects are directories, a merged directory |
| 2687 | +is formed. |
| 2688 | + |
| 2689 | +At mount time, the two directories given as mount options are combined |
| 2690 | +into a merged directory. Then whenever a lookup is requested in such |
| 2691 | +a merged directory, the lookup is performed in each actual directory |
| 2692 | +and the combined result is cached in the dentry belonging to the overlay |
| 2693 | +filesystem. If both actual lookups find directories, both are stored |
| 2694 | +and a merged directory is created, otherwise only one is stored: the |
| 2695 | +upper if it exists, else the lower. |
| 2696 | + |
| 2697 | +Only the lists of names from directories are merged. Other content |
| 2698 | +such as metadata and extended attributes are reported for the upper |
| 2699 | +directory only. These attributes of the lower directory are hidden. |
| 2700 | + |
| 2701 | +whiteouts and opaque directories |
| 2702 | +-------------------------------- |
| 2703 | + |
| 2704 | +In order to support rm and rmdir without changing the lower |
| 2705 | +filesystem, an overlay filesystem needs to record in the upper filesystem |
| 2706 | +that files have been removed. This is done using whiteouts and opaque |
| 2707 | +directories (non-directories are always opaque). |
| 2708 | + |
| 2709 | +The overlay filesystem uses extended attributes with a |
| 2710 | +"trusted.overlay." prefix to record these details. |
| 2711 | + |
| 2712 | +A whiteout is created as a symbolic link with target |
| 2713 | +"(overlay-whiteout)" and with xattr "trusted.overlay.whiteout" set to "y". |
| 2714 | +When a whiteout is found in the upper level of a merged directory, any |
| 2715 | +matching name in the lower level is ignored, and the whiteout itself |
| 2716 | +is also hidden. |
| 2717 | + |
| 2718 | +A directory is made opaque by setting the xattr "trusted.overlay.opaque" |
| 2719 | +to "y". Where the upper filesystem contains an opaque directory, any |
| 2720 | +directory in the lower filesystem with the same name is ignored. |
| 2721 | + |
| 2722 | +readdir |
| 2723 | +------- |
| 2724 | + |
| 2725 | +When a 'readdir' request is made on a merged directory, the upper and |
| 2726 | +lower directories are each read and the name lists merged in the |
| 2727 | +obvious way (upper is read first, then lower - entries that already |
| 2728 | +exist are not re-added). This merged name list is cached in the |
| 2729 | +'struct file' and so remains as long as the file is kept open. If the |
| 2730 | +directory is opened and read by two processes at the same time, they |
| 2731 | +will each have separate caches. A seekdir to the start of the |
| 2732 | +directory (offset 0) followed by a readdir will cause the cache to be |
| 2733 | +discarded and rebuilt. |
| 2734 | + |
| 2735 | +This means that changes to the merged directory do not appear while a |
| 2736 | +directory is being read. This is unlikely to be noticed by many |
| 2737 | +programs. |
| 2738 | + |
| 2739 | +seek offsets are assigned sequentially when the directories are read. |
| 2740 | +Thus if |
| 2741 | + - read part of a directory |
| 2742 | + - remember an offset, and close the directory |
| 2743 | + - re-open the directory some time later |
| 2744 | + - seek to the remembered offset |
| 2745 | + |
| 2746 | +there may be little correlation between the old and new locations in |
| 2747 | +the list of filenames, particularly if anything has changed in the |
| 2748 | +directory. |
| 2749 | + |
| 2750 | +Readdir on directories that are not merged is simply handled by the |
| 2751 | +underlying directory (upper or lower). |
| 2752 | + |
| 2753 | + |
| 2754 | +Non-directories |
| 2755 | +--------------- |
| 2756 | + |
| 2757 | +Objects that are not directories (files, symlinks, device-special |
| 2758 | +files etc) are presented either from the upper or lower filesystem as |
| 2759 | +appropriate. When a file in the lower filesystem is accessed in a way |
| 2760 | +the requires write-access; such as opening for write access, changing |
| 2761 | +some metadata etc, the file is first copied from the lower filesystem |
| 2762 | +to the upper filesystem (copy_up). Note that creating a hard-link |
| 2763 | +also requires copy-up, though of course creation of a symlink does |
| 2764 | +not. |
| 2765 | + |
| 2766 | +The copy_up process first makes sure that the containing directory |
| 2767 | +exists in the upper filesystem - creating it and any parents as |
| 2768 | +necessary. It then creates the object with the same metadata (owner, |
| 2769 | +mode, mtime, symlink-target etc) and then if the object is a file, the |
| 2770 | +data is copied from the lower to the upper filesystem. Finally any |
| 2771 | +extended attributes are copied up. |
| 2772 | + |
| 2773 | +Once the copy_up is complete, the overlay filesystem simply |
| 2774 | +provides direct access to the newly created file in the upper |
| 2775 | +filesystem - future operations on the file are barely noticed by the |
| 2776 | +overlay filesystem (though an operation on the name of the file such as |
| 2777 | +rename or unlink will of course be noticed and handled). |
| 2778 | + |
| 2779 | +Changes to underlying filesystems |
| 2780 | +--------------------------------- |
| 2781 | + |
| 2782 | +Offline changes, when the overlay is not mounted, are allowed to either |
| 2783 | +the upper or the lower trees. |
| 2784 | + |
| 2785 | +Changes to the underlying filesystems while part of a mounted overlay |
| 2786 | +filesystem are not allowed. This is not yet enforced, but will be in |
| 2787 | +the future. |
| 2788 | |