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/xattr.h>
14 #include "overlayfs.h"
15 #include "ovl_entry.h"
17 int ovl_want_write(struct dentry
*dentry
)
19 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
20 return mnt_want_write(ofs
->upper_mnt
);
23 void ovl_drop_write(struct dentry
*dentry
)
25 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
26 mnt_drop_write(ofs
->upper_mnt
);
29 struct dentry
*ovl_workdir(struct dentry
*dentry
)
31 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
35 const struct cred
*ovl_override_creds(struct super_block
*sb
)
37 struct ovl_fs
*ofs
= sb
->s_fs_info
;
39 return override_creds(ofs
->creator_cred
);
42 struct ovl_entry
*ovl_alloc_entry(unsigned int numlower
)
44 size_t size
= offsetof(struct ovl_entry
, lowerstack
[numlower
]);
45 struct ovl_entry
*oe
= kzalloc(size
, GFP_KERNEL
);
48 oe
->numlower
= numlower
;
53 bool ovl_dentry_remote(struct dentry
*dentry
)
55 return dentry
->d_flags
&
56 (DCACHE_OP_REVALIDATE
| DCACHE_OP_WEAK_REVALIDATE
|
60 bool ovl_dentry_weird(struct dentry
*dentry
)
62 return dentry
->d_flags
& (DCACHE_NEED_AUTOMOUNT
|
63 DCACHE_MANAGE_TRANSIT
|
68 enum ovl_path_type
ovl_path_type(struct dentry
*dentry
)
70 struct ovl_entry
*oe
= dentry
->d_fsdata
;
71 enum ovl_path_type type
= 0;
73 if (oe
->__upperdentry
) {
74 type
= __OVL_PATH_UPPER
;
77 * Non-dir dentry can hold lower dentry from previous
80 if (oe
->numlower
&& d_is_dir(dentry
))
81 type
|= __OVL_PATH_MERGE
;
84 type
|= __OVL_PATH_MERGE
;
89 void ovl_path_upper(struct dentry
*dentry
, struct path
*path
)
91 struct ovl_fs
*ofs
= dentry
->d_sb
->s_fs_info
;
92 struct ovl_entry
*oe
= dentry
->d_fsdata
;
94 path
->mnt
= ofs
->upper_mnt
;
95 path
->dentry
= ovl_upperdentry_dereference(oe
);
98 void ovl_path_lower(struct dentry
*dentry
, struct path
*path
)
100 struct ovl_entry
*oe
= dentry
->d_fsdata
;
102 *path
= oe
->numlower
? oe
->lowerstack
[0] : (struct path
) { NULL
, NULL
};
105 enum ovl_path_type
ovl_path_real(struct dentry
*dentry
, struct path
*path
)
107 enum ovl_path_type type
= ovl_path_type(dentry
);
109 if (!OVL_TYPE_UPPER(type
))
110 ovl_path_lower(dentry
, path
);
112 ovl_path_upper(dentry
, path
);
117 struct dentry
*ovl_dentry_upper(struct dentry
*dentry
)
119 struct ovl_entry
*oe
= dentry
->d_fsdata
;
121 return ovl_upperdentry_dereference(oe
);
124 static struct dentry
*__ovl_dentry_lower(struct ovl_entry
*oe
)
126 return oe
->numlower
? oe
->lowerstack
[0].dentry
: NULL
;
129 struct dentry
*ovl_dentry_lower(struct dentry
*dentry
)
131 struct ovl_entry
*oe
= dentry
->d_fsdata
;
133 return __ovl_dentry_lower(oe
);
136 struct dentry
*ovl_dentry_real(struct dentry
*dentry
)
138 struct ovl_entry
*oe
= dentry
->d_fsdata
;
139 struct dentry
*realdentry
;
141 realdentry
= ovl_upperdentry_dereference(oe
);
143 realdentry
= __ovl_dentry_lower(oe
);
148 struct ovl_dir_cache
*ovl_dir_cache(struct dentry
*dentry
)
150 struct ovl_entry
*oe
= dentry
->d_fsdata
;
155 void ovl_set_dir_cache(struct dentry
*dentry
, struct ovl_dir_cache
*cache
)
157 struct ovl_entry
*oe
= dentry
->d_fsdata
;
162 bool ovl_dentry_is_opaque(struct dentry
*dentry
)
164 struct ovl_entry
*oe
= dentry
->d_fsdata
;
168 bool ovl_dentry_is_whiteout(struct dentry
*dentry
)
170 return !dentry
->d_inode
&& ovl_dentry_is_opaque(dentry
);
173 void ovl_dentry_set_opaque(struct dentry
*dentry
)
175 struct ovl_entry
*oe
= dentry
->d_fsdata
;
180 bool ovl_redirect_dir(struct super_block
*sb
)
182 struct ovl_fs
*ofs
= sb
->s_fs_info
;
184 return ofs
->config
.redirect_dir
;
187 void ovl_clear_redirect_dir(struct super_block
*sb
)
189 struct ovl_fs
*ofs
= sb
->s_fs_info
;
191 ofs
->config
.redirect_dir
= false;
194 const char *ovl_dentry_get_redirect(struct dentry
*dentry
)
196 struct ovl_entry
*oe
= dentry
->d_fsdata
;
201 void ovl_dentry_set_redirect(struct dentry
*dentry
, const char *redirect
)
203 struct ovl_entry
*oe
= dentry
->d_fsdata
;
206 oe
->redirect
= redirect
;
209 void ovl_dentry_update(struct dentry
*dentry
, struct dentry
*upperdentry
)
211 struct ovl_entry
*oe
= dentry
->d_fsdata
;
213 WARN_ON(!inode_is_locked(upperdentry
->d_parent
->d_inode
));
214 WARN_ON(oe
->__upperdentry
);
216 * Make sure upperdentry is consistent before making it visible to
217 * ovl_upperdentry_dereference().
220 oe
->__upperdentry
= upperdentry
;
223 void ovl_inode_init(struct inode
*inode
, struct inode
*realinode
, bool is_upper
)
225 WRITE_ONCE(inode
->i_private
, (unsigned long) realinode
|
226 (is_upper
? OVL_ISUPPER_MASK
: 0));
229 void ovl_inode_update(struct inode
*inode
, struct inode
*upperinode
)
231 WARN_ON(!upperinode
);
232 WARN_ON(!inode_unhashed(inode
));
233 WRITE_ONCE(inode
->i_private
,
234 (unsigned long) upperinode
| OVL_ISUPPER_MASK
);
235 if (!S_ISDIR(upperinode
->i_mode
))
236 __insert_inode_hash(inode
, (unsigned long) upperinode
);
239 void ovl_dentry_version_inc(struct dentry
*dentry
)
241 struct ovl_entry
*oe
= dentry
->d_fsdata
;
243 WARN_ON(!inode_is_locked(dentry
->d_inode
));
247 u64
ovl_dentry_version_get(struct dentry
*dentry
)
249 struct ovl_entry
*oe
= dentry
->d_fsdata
;
251 WARN_ON(!inode_is_locked(dentry
->d_inode
));
255 bool ovl_is_whiteout(struct dentry
*dentry
)
257 struct inode
*inode
= dentry
->d_inode
;
259 return inode
&& IS_WHITEOUT(inode
);
262 struct file
*ovl_path_open(struct path
*path
, int flags
)
264 return dentry_open(path
, flags
| O_NOATIME
, current_cred());