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 "overlayfs.h"
16 #include "ovl_entry.h"
18 int ovl_want_write(struct dentry *dentry)
20 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
21 return mnt_want_write(ofs->upper_mnt);
24 void ovl_drop_write(struct dentry *dentry)
26 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
27 mnt_drop_write(ofs->upper_mnt);
30 struct dentry *ovl_workdir(struct dentry *dentry)
32 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
36 const struct cred *ovl_override_creds(struct super_block *sb)
38 struct ovl_fs *ofs = sb->s_fs_info;
40 return override_creds(ofs->creator_cred);
43 struct ovl_entry *ovl_alloc_entry(unsigned int numlower)
45 size_t size = offsetof(struct ovl_entry, lowerstack[numlower]);
46 struct ovl_entry *oe = kzalloc(size, GFP_KERNEL);
49 oe->numlower = numlower;
54 bool ovl_dentry_remote(struct dentry *dentry)
56 return dentry->d_flags &
57 (DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE |
61 bool ovl_dentry_weird(struct dentry *dentry)
63 return dentry->d_flags & (DCACHE_NEED_AUTOMOUNT |
64 DCACHE_MANAGE_TRANSIT |
69 enum ovl_path_type ovl_path_type(struct dentry *dentry)
71 struct ovl_entry *oe = dentry->d_fsdata;
72 enum ovl_path_type type = 0;
74 if (oe->__upperdentry) {
75 type = __OVL_PATH_UPPER;
78 * Non-dir dentry can hold lower dentry from previous
81 if (oe->numlower && d_is_dir(dentry))
82 type |= __OVL_PATH_MERGE;
85 type |= __OVL_PATH_MERGE;
90 void ovl_path_upper(struct dentry *dentry, struct path *path)
92 struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
93 struct ovl_entry *oe = dentry->d_fsdata;
95 path->mnt = ofs->upper_mnt;
96 path->dentry = ovl_upperdentry_dereference(oe);
99 void ovl_path_lower(struct dentry *dentry, struct path *path)
101 struct ovl_entry *oe = dentry->d_fsdata;
103 *path = oe->numlower ? oe->lowerstack[0] : (struct path) { NULL, NULL };
106 enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
108 enum ovl_path_type type = ovl_path_type(dentry);
110 if (!OVL_TYPE_UPPER(type))
111 ovl_path_lower(dentry, path);
113 ovl_path_upper(dentry, path);
118 struct dentry *ovl_dentry_upper(struct dentry *dentry)
120 struct ovl_entry *oe = dentry->d_fsdata;
122 return ovl_upperdentry_dereference(oe);
125 static struct dentry *__ovl_dentry_lower(struct ovl_entry *oe)
127 return oe->numlower ? oe->lowerstack[0].dentry : NULL;
130 struct dentry *ovl_dentry_lower(struct dentry *dentry)
132 struct ovl_entry *oe = dentry->d_fsdata;
134 return __ovl_dentry_lower(oe);
137 struct dentry *ovl_dentry_real(struct dentry *dentry)
139 struct ovl_entry *oe = dentry->d_fsdata;
140 struct dentry *realdentry;
142 realdentry = ovl_upperdentry_dereference(oe);
144 realdentry = __ovl_dentry_lower(oe);
149 struct ovl_dir_cache *ovl_dir_cache(struct dentry *dentry)
151 struct ovl_entry *oe = dentry->d_fsdata;
156 void ovl_set_dir_cache(struct dentry *dentry, struct ovl_dir_cache *cache)
158 struct ovl_entry *oe = dentry->d_fsdata;
163 bool ovl_dentry_is_opaque(struct dentry *dentry)
165 struct ovl_entry *oe = dentry->d_fsdata;
169 bool ovl_dentry_is_whiteout(struct dentry *dentry)
171 return !dentry->d_inode && ovl_dentry_is_opaque(dentry);
174 void ovl_dentry_set_opaque(struct dentry *dentry)
176 struct ovl_entry *oe = dentry->d_fsdata;
181 bool ovl_redirect_dir(struct super_block *sb)
183 struct ovl_fs *ofs = sb->s_fs_info;
185 return ofs->config.redirect_dir;
188 void ovl_clear_redirect_dir(struct super_block *sb)
190 struct ovl_fs *ofs = sb->s_fs_info;
192 ofs->config.redirect_dir = false;
195 const char *ovl_dentry_get_redirect(struct dentry *dentry)
197 struct ovl_entry *oe = dentry->d_fsdata;
202 void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect)
204 struct ovl_entry *oe = dentry->d_fsdata;
207 oe->redirect = redirect;
210 void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
212 struct ovl_entry *oe = dentry->d_fsdata;
214 WARN_ON(!inode_is_locked(upperdentry->d_parent->d_inode));
215 WARN_ON(oe->__upperdentry);
217 * Make sure upperdentry is consistent before making it visible to
218 * ovl_upperdentry_dereference().
221 oe->__upperdentry = upperdentry;
224 void ovl_inode_init(struct inode *inode, struct inode *realinode, bool is_upper)
226 WRITE_ONCE(inode->i_private, (unsigned long) realinode |
227 (is_upper ? OVL_ISUPPER_MASK : 0));
230 void ovl_inode_update(struct inode *inode, struct inode *upperinode)
232 WARN_ON(!upperinode);
233 WARN_ON(!inode_unhashed(inode));
234 WRITE_ONCE(inode->i_private,
235 (unsigned long) upperinode | OVL_ISUPPER_MASK);
236 if (!S_ISDIR(upperinode->i_mode))
237 __insert_inode_hash(inode, (unsigned long) upperinode);
240 void ovl_dentry_version_inc(struct dentry *dentry)
242 struct ovl_entry *oe = dentry->d_fsdata;
244 WARN_ON(!inode_is_locked(dentry->d_inode));
248 u64 ovl_dentry_version_get(struct dentry *dentry)
250 struct ovl_entry *oe = dentry->d_fsdata;
252 WARN_ON(!inode_is_locked(dentry->d_inode));
256 bool ovl_is_whiteout(struct dentry *dentry)
258 struct inode *inode = dentry->d_inode;
260 return inode && IS_WHITEOUT(inode);
263 struct file *ovl_path_open(struct path *path, int flags)
265 return dentry_open(path, flags | O_NOATIME, current_cred());