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/sched/signal.h>
16 #include "overlayfs.h"
17 #include "ovl_entry.h"
19 int ovl_want_write(struct dentry *dentry)
21 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
22 return mnt_want_write(ofs->upper_mnt);
25 void ovl_drop_write(struct dentry *dentry)
27 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
28 mnt_drop_write(ofs->upper_mnt);
31 struct dentry *ovl_workdir(struct dentry *dentry)
33 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
37 const struct cred *ovl_override_creds(struct super_block *sb)
39 struct ovl_fs *ofs = sb->s_fs_info;
41 return override_creds(ofs->creator_cred);
44 struct super_block *ovl_same_sb(struct super_block *sb)
46 struct ovl_fs *ofs = sb->s_fs_info;
51 struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
53 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
54 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
57 oe->numlower = numlower;
62 bool ovl_dentry_remote(struct dentry *dentry)
64 return dentry->d_flags &
65 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
69 bool ovl_dentry_weird(struct dentry *dentry)
71 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
72 DCACHE_MANAGE_TRANSIT |
77 enum ovl_path_type ovl_path_type(struct dentry *dentry)
79 struct ovl_entry *oe = dentry->d_fsdata;
80 enum ovl_path_type type = 0;
82 if (oe->__upperdentry) {
83 type = __OVL_PATH_UPPER;
86 * Non-dir dentry can hold lower dentry of its copy up origin.
89 type |= __OVL_PATH_ORIGIN;
91 type |= __OVL_PATH_MERGE;
95 type |= __OVL_PATH_MERGE;
100 void ovl_path_upper(struct dentry *dentry, struct path *path)
102 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
103 struct ovl_entry *oe = dentry->d_fsdata;
105 path->mnt = ofs->upper_mnt;
106 path->dentry = ovl_upperdentry_dereference(oe);
109 void ovl_path_lower(struct dentry *dentry, struct path *path)
111 struct ovl_entry *oe = dentry->d_fsdata;
113 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { };
116 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
118 enum ovl_path_type type = ovl_path_type(dentry);
120 if (!OVL_TYPE_UPPER(type))
121 ovl_path_lower(dentry, path);
123 ovl_path_upper(dentry, path);
128 struct dentry *ovl_dentry_upper(struct dentry *dentry)
130 struct ovl_entry *oe = dentry->d_fsdata;
132 return ovl_upperdentry_dereference(oe);
135 static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
137 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
140 struct dentry *ovl_dentry_lower(struct dentry *dentry)
142 struct ovl_entry *oe = dentry->d_fsdata;
144 return __ovl_dentry_lower(oe);
147 struct dentry *ovl_dentry_real(struct dentry *dentry)
149 struct ovl_entry *oe = dentry->d_fsdata;
150 struct dentry *realdentry;
152 realdentry = ovl_upperdentry_dereference(oe);
154 realdentry = __ovl_dentry_lower(oe);
159 struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
161 struct ovl_entry *oe = dentry->d_fsdata;
166 void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
168 struct ovl_entry *oe = dentry->d_fsdata;
173 bool ovl_dentry_is_opaque(struct dentry *dentry)
175 struct ovl_entry *oe = dentry->d_fsdata;
179 bool ovl_dentry_is_whiteout(struct dentry *dentry)
181 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
184 void ovl_dentry_set_opaque(struct dentry *dentry)
186 struct ovl_entry *oe = dentry->d_fsdata;
191 bool ovl_redirect_dir(struct super_block *sb)
193 struct ovl_fs *ofs = sb->s_fs_info;
195 return ofs->config.redirect_dir;
198 void ovl_clear_redirect_dir(struct super_block *sb)
200 struct ovl_fs *ofs = sb->s_fs_info;
202 ofs->config.redirect_dir = false;
205 const char *ovl_dentry_get_redirect(struct dentry *dentry)
207 struct ovl_entry *oe = dentry->d_fsdata;
212 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
214 struct ovl_entry *oe = dentry->d_fsdata;
217 oe->redirect = redirect;
220 void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
222 struct ovl_entry *oe = dentry->d_fsdata;
224 WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
225 WARN_ON(oe->__upperdentry);
227 * Make sure upperdentry is consistent before making it visible to
228 * ovl_upperdentry_dereference().
231 oe->__upperdentry = upperdentry;
234 void ovl_inode_init(struct inode *inode, struct inode *realinode, bool is_upper)
236 WRITE_ONCE(inode->i_private, (unsigned long) realinode |
237 (is_upper ? OVL_ISUPPER_MASK : 0));
240 void ovl_inode_update(struct inode *inode, struct inode *upperinode)
242 WARN_ON(!upperinode);
243 WARN_ON(!inode_unhashed(inode));
244 WRITE_ONCE(inode->i_private,
245 (unsigned long) upperinode | OVL_ISUPPER_MASK);
246 if (!S_ISDIR(upperinode->i_mode))
247 __insert_inode_hash(inode, (unsigned long) upperinode);
250 void ovl_dentry_version_inc(struct dentry *dentry)
252 struct ovl_entry *oe = dentry->d_fsdata;
254 WARN_ON(!inode_is_locked(dentry->d_inode));
258 u64 ovl_dentry_version_get(struct dentry *dentry)
260 struct ovl_entry *oe = dentry->d_fsdata;
262 WARN_ON(!inode_is_locked(dentry->d_inode));
266 bool ovl_is_whiteout(struct dentry *dentry)
268 struct inode *inode = dentry->d_inode;
270 return inode && IS_WHITEOUT(inode);
273 struct file *ovl_path_open(struct path *path, int flags)
275 return dentry_open(path, flags | O_NOATIME, current_cred());
278 int ovl_copy_up_start(struct dentry *dentry)
280 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
281 struct ovl_entry *oe = dentry->d_fsdata;
284 spin_lock(&ofs->copyup_wq.lock);
285 err = wait_event_interruptible_locked(ofs->copyup_wq, !oe->copying);
287 if (oe->__upperdentry)
288 err = 1; /* Already copied up */
292 spin_unlock(&ofs->copyup_wq.lock);
297 void ovl_copy_up_end(struct dentry *dentry)
299 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
300 struct ovl_entry *oe = dentry->d_fsdata;
302 spin_lock(&ofs->copyup_wq.lock);
304 wake_up_locked(&ofs->copyup_wq);
305 spin_unlock(&ofs->copyup_wq.lock);