2 * Copyright (C) 2011 Novell Inc.
3 * Copyright (C) 2016 Red Hat, Inc.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published by
7 * the Free Software Foundation.
11 #include <linux/mount.h>
12 #include <linux/slab.h>
13 #include <linux/cred.h>
14 #include <linux/xattr.h>
15 #include <linux/exportfs.h>
16 #include <linux/uuid.h>
17 #include <linux/namei.h>
18 #include <linux/ratelimit.h>
19 #include "overlayfs.h"
20 #include "ovl_entry.h"
22 int ovl_want_write(struct dentry *dentry)
24 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
25 return mnt_want_write(ofs->upper_mnt);
28 void ovl_drop_write(struct dentry *dentry)
30 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
31 mnt_drop_write(ofs->upper_mnt);
34 struct dentry *ovl_workdir(struct dentry *dentry)
36 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
40 const struct cred *ovl_override_creds(struct super_block *sb)
42 struct ovl_fs *ofs = sb->s_fs_info;
44 return override_creds(ofs->creator_cred);
47 struct super_block *ovl_same_sb(struct super_block *sb)
49 struct ovl_fs *ofs = sb->s_fs_info;
54 bool ovl_can_decode_fh(struct super_block *sb)
56 return (sb->s_export_op && sb->s_export_op->fh_to_dentry &&
57 !uuid_is_null(&sb->s_uuid));
60 struct dentry *ovl_indexdir(struct super_block *sb)
62 struct ovl_fs *ofs = sb->s_fs_info;
67 struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
69 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
70 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
73 oe->numlower = numlower;
78 bool ovl_dentry_remote(struct dentry *dentry)
80 return dentry->d_flags &
81 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
85 bool ovl_dentry_weird(struct dentry *dentry)
87 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
88 DCACHE_MANAGE_TRANSIT |
93 enum ovl_path_type ovl_path_type(struct dentry *dentry)
95 struct ovl_entry *oe = dentry->d_fsdata;
96 enum ovl_path_type type = 0;
98 if (ovl_dentry_upper(dentry)) {
99 type = __OVL_PATH_UPPER;
102 * Non-dir dentry can hold lower dentry of its copy up origin.
105 type |= __OVL_PATH_ORIGIN;
106 if (d_is_dir(dentry))
107 type |= __OVL_PATH_MERGE;
110 if (oe->numlower > 1)
111 type |= __OVL_PATH_MERGE;
116 void ovl_path_upper(struct dentry *dentry, struct path *path)
118 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
120 path->mnt = ofs->upper_mnt;
121 path->dentry = ovl_dentry_upper(dentry);
124 void ovl_path_lower(struct dentry *dentry, struct path *path)
126 struct ovl_entry *oe = dentry->d_fsdata;
128 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { };
131 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
133 enum ovl_path_type type = ovl_path_type(dentry);
135 if (!OVL_TYPE_UPPER(type))
136 ovl_path_lower(dentry, path);
138 ovl_path_upper(dentry, path);
143 struct dentry *ovl_dentry_upper(struct dentry *dentry)
145 return ovl_upperdentry_dereference(OVL_I(d_inode(dentry)));
148 struct dentry *ovl_dentry_lower(struct dentry *dentry)
150 struct ovl_entry *oe = dentry->d_fsdata;
152 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
155 struct dentry *ovl_dentry_real(struct dentry *dentry)
157 return ovl_dentry_upper(dentry) ?: ovl_dentry_lower(dentry);
160 struct dentry *ovl_i_dentry_upper(struct inode *inode)
162 return ovl_upperdentry_dereference(OVL_I(inode));
165 struct inode *ovl_inode_upper(struct inode *inode)
167 struct dentry *upperdentry = ovl_i_dentry_upper(inode);
169 return upperdentry ? d_inode(upperdentry) : NULL;
172 struct inode *ovl_inode_lower(struct inode *inode)
174 return OVL_I(inode)->lower;
177 struct inode *ovl_inode_real(struct inode *inode)
179 return ovl_inode_upper(inode) ?: ovl_inode_lower(inode);
183 struct ovl_dir_cache *ovl_dir_cache(struct inode *inode)
185 return OVL_I(inode)->cache;
188 void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache)
190 OVL_I(inode)->cache = cache;
193 bool ovl_dentry_is_opaque(struct dentry *dentry)
195 struct ovl_entry *oe = dentry->d_fsdata;
199 bool ovl_dentry_is_whiteout(struct dentry *dentry)
201 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
204 void ovl_dentry_set_opaque(struct dentry *dentry)
206 struct ovl_entry *oe = dentry->d_fsdata;
212 * For hard links it's possible for ovl_dentry_upper() to return positive, while
213 * there's no actual upper alias for the inode. Copy up code needs to know
214 * about the existence of the upper alias, so it can't use ovl_dentry_upper().
216 bool ovl_dentry_has_upper_alias(struct dentry *dentry)
218 struct ovl_entry *oe = dentry->d_fsdata;
220 return oe->has_upper;
223 void ovl_dentry_set_upper_alias(struct dentry *dentry)
225 struct ovl_entry *oe = dentry->d_fsdata;
227 oe->has_upper = true;
230 bool ovl_redirect_dir(struct super_block *sb)
232 struct ovl_fs *ofs = sb->s_fs_info;
234 return ofs->config.redirect_dir && !ofs->noxattr;
237 const char *ovl_dentry_get_redirect(struct dentry *dentry)
239 return OVL_I(d_inode(dentry))->redirect;
242 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
244 struct ovl_inode *oi = OVL_I(d_inode(dentry));
247 oi->redirect = redirect;
250 void ovl_inode_init(struct inode *inode, struct dentry *upperdentry,
251 struct dentry *lowerdentry)
254 OVL_I(inode)->__upperdentry = upperdentry;
256 OVL_I(inode)->lower = igrab(d_inode(lowerdentry));
258 ovl_copyattr(d_inode(upperdentry ?: lowerdentry), inode);
261 void ovl_inode_update(struct inode *inode, struct dentry *upperdentry)
263 struct inode *upperinode = d_inode(upperdentry);
265 WARN_ON(OVL_I(inode)->__upperdentry);
268 * Make sure upperdentry is consistent before making it visible
271 OVL_I(inode)->__upperdentry = upperdentry;
272 if (inode_unhashed(inode)) {
273 inode->i_private = upperinode;
274 __insert_inode_hash(inode, (unsigned long) upperinode);
278 void ovl_dentry_version_inc(struct dentry *dentry, bool impurity)
280 struct inode *inode = d_inode(dentry);
282 WARN_ON(!inode_is_locked(inode));
284 * Version is used by readdir code to keep cache consistent. For merge
285 * dirs all changes need to be noted. For non-merge dirs, cache only
286 * contains impure (ones which have been copied up and have origins)
287 * entries, so only need to note changes to impure entries.
289 if (OVL_TYPE_MERGE(ovl_path_type(dentry)) || impurity)
290 OVL_I(inode)->version++;
293 u64 ovl_dentry_version_get(struct dentry *dentry)
295 struct inode *inode = d_inode(dentry);
297 WARN_ON(!inode_is_locked(inode));
298 return OVL_I(inode)->version;
301 bool ovl_is_whiteout(struct dentry *dentry)
303 struct inode *inode = dentry->d_inode;
305 return inode && IS_WHITEOUT(inode);
308 struct file *ovl_path_open(struct path *path, int flags)
310 return dentry_open(path, flags | O_NOATIME, current_cred());
313 int ovl_copy_up_start(struct dentry *dentry)
315 struct ovl_inode *oi = OVL_I(d_inode(dentry));
318 err = mutex_lock_interruptible(&oi->lock);
319 if (!err && ovl_dentry_has_upper_alias(dentry)) {
320 err = 1; /* Already copied up */
321 mutex_unlock(&oi->lock);
327 void ovl_copy_up_end(struct dentry *dentry)
329 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
332 bool ovl_check_dir_xattr(struct dentry *dentry, const char *name)
337 if (!d_is_dir(dentry))
340 res = vfs_getxattr(dentry, name, &val, 1);
341 if (res == 1 && val == 'y')
347 int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
348 const char *name, const void *value, size_t size,
352 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
357 err = ovl_do_setxattr(upperdentry, name, value, size, 0);
359 if (err == -EOPNOTSUPP) {
360 pr_warn("overlayfs: cannot set %s xattr on upper\n", name);
368 int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry)
372 if (ovl_test_flag(OVL_IMPURE, d_inode(dentry)))
376 * Do not fail when upper doesn't support xattrs.
377 * Upper inodes won't have origin nor redirect xattr anyway.
379 err = ovl_check_setxattr(dentry, upperdentry, OVL_XATTR_IMPURE,
382 ovl_set_flag(OVL_IMPURE, d_inode(dentry));
387 void ovl_set_flag(unsigned long flag, struct inode *inode)
389 set_bit(flag, &OVL_I(inode)->flags);
392 void ovl_clear_flag(unsigned long flag, struct inode *inode)
394 clear_bit(flag, &OVL_I(inode)->flags);
397 bool ovl_test_flag(unsigned long flag, struct inode *inode)
399 return test_bit(flag, &OVL_I(inode)->flags);
403 * Caller must hold a reference to inode to prevent it from being freed while
404 * it is marked inuse.
406 bool ovl_inuse_trylock(struct dentry *dentry)
408 struct inode *inode = d_inode(dentry);
411 spin_lock(&inode->i_lock);
412 if (!(inode->i_state & I_OVL_INUSE)) {
413 inode->i_state |= I_OVL_INUSE;
416 spin_unlock(&inode->i_lock);
421 void ovl_inuse_unlock(struct dentry *dentry)
424 struct inode *inode = d_inode(dentry);
426 spin_lock(&inode->i_lock);
427 WARN_ON(!(inode->i_state & I_OVL_INUSE));
428 inode->i_state &= ~I_OVL_INUSE;
429 spin_unlock(&inode->i_lock);
433 /* Caller must hold OVL_I(inode)->lock */
434 static void ovl_cleanup_index(struct dentry *dentry)
436 struct inode *dir = ovl_indexdir(dentry->d_sb)->d_inode;
437 struct dentry *lowerdentry = ovl_dentry_lower(dentry);
438 struct dentry *upperdentry = ovl_dentry_upper(dentry);
439 struct dentry *index = NULL;
441 struct qstr name = { };
444 err = ovl_get_index_name(lowerdentry, &name);
448 inode = d_inode(upperdentry);
449 if (inode->i_nlink != 1) {
450 pr_warn_ratelimited("overlayfs: cleanup linked index (%pd2, ino=%lu, nlink=%u)\n",
451 upperdentry, inode->i_ino, inode->i_nlink);
453 * We either have a bug with persistent union nlink or a lower
454 * hardlink was added while overlay is mounted. Adding a lower
455 * hardlink and then unlinking all overlay hardlinks would drop
456 * overlay nlink to zero before all upper inodes are unlinked.
457 * As a safety measure, when that situation is detected, set
458 * the overlay nlink to the index inode nlink minus one for the
459 * index entry itself.
461 set_nlink(d_inode(dentry), inode->i_nlink - 1);
462 ovl_set_nlink_upper(dentry);
466 inode_lock_nested(dir, I_MUTEX_PARENT);
467 /* TODO: whiteout instead of cleanup to block future open by handle */
468 index = lookup_one_len(name.name, ovl_indexdir(dentry->d_sb), name.len);
469 err = PTR_ERR(index);
471 err = ovl_cleanup(dir, index);
485 pr_err("overlayfs: cleanup index of '%pd2' failed (%i)\n", dentry, err);
490 * Operations that change overlay inode and upper inode nlink need to be
491 * synchronized with copy up for persistent nlink accounting.
493 int ovl_nlink_start(struct dentry *dentry, bool *locked)
495 struct ovl_inode *oi = OVL_I(d_inode(dentry));
496 const struct cred *old_cred;
499 if (!d_inode(dentry) || d_is_dir(dentry))
503 * With inodes index is enabled, we store the union overlay nlink
504 * in an xattr on the index inode. When whiting out lower hardlinks
505 * we need to decrement the overlay persistent nlink, but before the
506 * first copy up, we have no upper index inode to store the xattr.
508 * As a workaround, before whiteout/rename over of a lower hardlink,
509 * copy up to create the upper index. Creating the upper index will
510 * initialize the overlay nlink, so it could be dropped if unlink
511 * or rename succeeds.
513 * TODO: implement metadata only index copy up when called with
514 * ovl_copy_up_flags(dentry, O_PATH).
516 if (ovl_indexdir(dentry->d_sb) && !ovl_dentry_has_upper_alias(dentry) &&
517 d_inode(ovl_dentry_lower(dentry))->i_nlink > 1) {
518 err = ovl_copy_up(dentry);
523 err = mutex_lock_interruptible(&oi->lock);
527 if (!ovl_test_flag(OVL_INDEX, d_inode(dentry)))
530 old_cred = ovl_override_creds(dentry->d_sb);
532 * The overlay inode nlink should be incremented/decremented IFF the
533 * upper operation succeeds, along with nlink change of upper inode.
534 * Therefore, before link/unlink/rename, we store the union nlink
535 * value relative to the upper inode nlink in an upper inode xattr.
537 err = ovl_set_nlink_upper(dentry);
538 revert_creds(old_cred);
542 mutex_unlock(&oi->lock);
549 void ovl_nlink_end(struct dentry *dentry, bool locked)
552 if (ovl_test_flag(OVL_INDEX, d_inode(dentry)) &&
553 d_inode(dentry)->i_nlink == 0) {
554 const struct cred *old_cred;
556 old_cred = ovl_override_creds(dentry->d_sb);
557 ovl_cleanup_index(dentry);
558 revert_creds(old_cred);
561 mutex_unlock(&OVL_I(d_inode(dentry))->lock);
565 int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir)
567 /* Workdir should not be the same as upperdir */
568 if (workdir == upperdir)
571 /* Workdir should not be subdir of upperdir and vice versa */
572 if (lock_rename(workdir, upperdir) != NULL)
578 unlock_rename(workdir, upperdir);
580 pr_err("overlayfs: failed to lock workdir+upperdir\n");