1*4882a593Smuzhiyun /* SPDX-License-Identifier: GPL-2.0-only */
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Copyright (C) 2011 Novell Inc.
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <linux/kernel.h>
8*4882a593Smuzhiyun #include <linux/uuid.h>
9*4882a593Smuzhiyun #include <linux/fs.h>
10*4882a593Smuzhiyun #include "ovl_entry.h"
11*4882a593Smuzhiyun
12*4882a593Smuzhiyun #undef pr_fmt
13*4882a593Smuzhiyun #define pr_fmt(fmt) "overlayfs: " fmt
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun enum ovl_path_type {
16*4882a593Smuzhiyun __OVL_PATH_UPPER = (1 << 0),
17*4882a593Smuzhiyun __OVL_PATH_MERGE = (1 << 1),
18*4882a593Smuzhiyun __OVL_PATH_ORIGIN = (1 << 2),
19*4882a593Smuzhiyun };
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #define OVL_TYPE_UPPER(type) ((type) & __OVL_PATH_UPPER)
22*4882a593Smuzhiyun #define OVL_TYPE_MERGE(type) ((type) & __OVL_PATH_MERGE)
23*4882a593Smuzhiyun #define OVL_TYPE_ORIGIN(type) ((type) & __OVL_PATH_ORIGIN)
24*4882a593Smuzhiyun
25*4882a593Smuzhiyun #define OVL_XATTR_PREFIX XATTR_TRUSTED_PREFIX "overlay."
26*4882a593Smuzhiyun
27*4882a593Smuzhiyun enum ovl_xattr {
28*4882a593Smuzhiyun OVL_XATTR_OPAQUE,
29*4882a593Smuzhiyun OVL_XATTR_REDIRECT,
30*4882a593Smuzhiyun OVL_XATTR_ORIGIN,
31*4882a593Smuzhiyun OVL_XATTR_IMPURE,
32*4882a593Smuzhiyun OVL_XATTR_NLINK,
33*4882a593Smuzhiyun OVL_XATTR_UPPER,
34*4882a593Smuzhiyun OVL_XATTR_METACOPY,
35*4882a593Smuzhiyun };
36*4882a593Smuzhiyun
37*4882a593Smuzhiyun enum ovl_inode_flag {
38*4882a593Smuzhiyun /* Pure upper dir that may contain non pure upper entries */
39*4882a593Smuzhiyun OVL_IMPURE,
40*4882a593Smuzhiyun /* Non-merge dir that may contain whiteout entries */
41*4882a593Smuzhiyun OVL_WHITEOUTS,
42*4882a593Smuzhiyun OVL_INDEX,
43*4882a593Smuzhiyun OVL_UPPERDATA,
44*4882a593Smuzhiyun /* Inode number will remain constant over copy up. */
45*4882a593Smuzhiyun OVL_CONST_INO,
46*4882a593Smuzhiyun };
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun enum ovl_entry_flag {
49*4882a593Smuzhiyun OVL_E_UPPER_ALIAS,
50*4882a593Smuzhiyun OVL_E_OPAQUE,
51*4882a593Smuzhiyun OVL_E_CONNECTED,
52*4882a593Smuzhiyun };
53*4882a593Smuzhiyun
54*4882a593Smuzhiyun enum {
55*4882a593Smuzhiyun OVL_XINO_OFF,
56*4882a593Smuzhiyun OVL_XINO_AUTO,
57*4882a593Smuzhiyun OVL_XINO_ON,
58*4882a593Smuzhiyun };
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun /*
61*4882a593Smuzhiyun * The tuple (fh,uuid) is a universal unique identifier for a copy up origin,
62*4882a593Smuzhiyun * where:
63*4882a593Smuzhiyun * origin.fh - exported file handle of the lower file
64*4882a593Smuzhiyun * origin.uuid - uuid of the lower filesystem
65*4882a593Smuzhiyun */
66*4882a593Smuzhiyun #define OVL_FH_VERSION 0
67*4882a593Smuzhiyun #define OVL_FH_MAGIC 0xfb
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /* CPU byte order required for fid decoding: */
70*4882a593Smuzhiyun #define OVL_FH_FLAG_BIG_ENDIAN (1 << 0)
71*4882a593Smuzhiyun #define OVL_FH_FLAG_ANY_ENDIAN (1 << 1)
72*4882a593Smuzhiyun /* Is the real inode encoded in fid an upper inode? */
73*4882a593Smuzhiyun #define OVL_FH_FLAG_PATH_UPPER (1 << 2)
74*4882a593Smuzhiyun
75*4882a593Smuzhiyun #define OVL_FH_FLAG_ALL (OVL_FH_FLAG_BIG_ENDIAN | OVL_FH_FLAG_ANY_ENDIAN | \
76*4882a593Smuzhiyun OVL_FH_FLAG_PATH_UPPER)
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun #if defined(__LITTLE_ENDIAN)
79*4882a593Smuzhiyun #define OVL_FH_FLAG_CPU_ENDIAN 0
80*4882a593Smuzhiyun #elif defined(__BIG_ENDIAN)
81*4882a593Smuzhiyun #define OVL_FH_FLAG_CPU_ENDIAN OVL_FH_FLAG_BIG_ENDIAN
82*4882a593Smuzhiyun #else
83*4882a593Smuzhiyun #error Endianness not defined
84*4882a593Smuzhiyun #endif
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /* The type used to be returned by overlay exportfs for misaligned fid */
87*4882a593Smuzhiyun #define OVL_FILEID_V0 0xfb
88*4882a593Smuzhiyun /* The type returned by overlay exportfs for 32bit aligned fid */
89*4882a593Smuzhiyun #define OVL_FILEID_V1 0xf8
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /* On-disk format for "origin" file handle */
92*4882a593Smuzhiyun struct ovl_fb {
93*4882a593Smuzhiyun u8 version; /* 0 */
94*4882a593Smuzhiyun u8 magic; /* 0xfb */
95*4882a593Smuzhiyun u8 len; /* size of this header + size of fid */
96*4882a593Smuzhiyun u8 flags; /* OVL_FH_FLAG_* */
97*4882a593Smuzhiyun u8 type; /* fid_type of fid */
98*4882a593Smuzhiyun uuid_t uuid; /* uuid of filesystem */
99*4882a593Smuzhiyun u32 fid[]; /* file identifier should be 32bit aligned in-memory */
100*4882a593Smuzhiyun } __packed;
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /* In-memory and on-wire format for overlay file handle */
103*4882a593Smuzhiyun struct ovl_fh {
104*4882a593Smuzhiyun u8 padding[3]; /* make sure fb.fid is 32bit aligned */
105*4882a593Smuzhiyun union {
106*4882a593Smuzhiyun struct ovl_fb fb;
107*4882a593Smuzhiyun u8 buf[0];
108*4882a593Smuzhiyun };
109*4882a593Smuzhiyun } __packed;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun #define OVL_FH_WIRE_OFFSET offsetof(struct ovl_fh, fb)
112*4882a593Smuzhiyun #define OVL_FH_LEN(fh) (OVL_FH_WIRE_OFFSET + (fh)->fb.len)
113*4882a593Smuzhiyun #define OVL_FH_FID_OFFSET (OVL_FH_WIRE_OFFSET + \
114*4882a593Smuzhiyun offsetof(struct ovl_fb, fid))
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun extern const char *ovl_xattr_table[];
ovl_xattr(struct ovl_fs * ofs,enum ovl_xattr ox)117*4882a593Smuzhiyun static inline const char *ovl_xattr(struct ovl_fs *ofs, enum ovl_xattr ox)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun return ovl_xattr_table[ox];
120*4882a593Smuzhiyun }
121*4882a593Smuzhiyun
ovl_do_rmdir(struct inode * dir,struct dentry * dentry)122*4882a593Smuzhiyun static inline int ovl_do_rmdir(struct inode *dir, struct dentry *dentry)
123*4882a593Smuzhiyun {
124*4882a593Smuzhiyun int err = vfs_rmdir(dir, dentry);
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun pr_debug("rmdir(%pd2) = %i\n", dentry, err);
127*4882a593Smuzhiyun return err;
128*4882a593Smuzhiyun }
129*4882a593Smuzhiyun
ovl_do_unlink(struct inode * dir,struct dentry * dentry)130*4882a593Smuzhiyun static inline int ovl_do_unlink(struct inode *dir, struct dentry *dentry)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun int err = vfs_unlink(dir, dentry, NULL);
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun pr_debug("unlink(%pd2) = %i\n", dentry, err);
135*4882a593Smuzhiyun return err;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
ovl_do_link(struct dentry * old_dentry,struct inode * dir,struct dentry * new_dentry)138*4882a593Smuzhiyun static inline int ovl_do_link(struct dentry *old_dentry, struct inode *dir,
139*4882a593Smuzhiyun struct dentry *new_dentry)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun int err = vfs_link(old_dentry, dir, new_dentry, NULL);
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun pr_debug("link(%pd2, %pd2) = %i\n", old_dentry, new_dentry, err);
144*4882a593Smuzhiyun return err;
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun
ovl_do_create(struct inode * dir,struct dentry * dentry,umode_t mode)147*4882a593Smuzhiyun static inline int ovl_do_create(struct inode *dir, struct dentry *dentry,
148*4882a593Smuzhiyun umode_t mode)
149*4882a593Smuzhiyun {
150*4882a593Smuzhiyun int err = vfs_create(dir, dentry, mode, true);
151*4882a593Smuzhiyun
152*4882a593Smuzhiyun pr_debug("create(%pd2, 0%o) = %i\n", dentry, mode, err);
153*4882a593Smuzhiyun return err;
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun
ovl_do_mkdir(struct inode * dir,struct dentry * dentry,umode_t mode)156*4882a593Smuzhiyun static inline int ovl_do_mkdir(struct inode *dir, struct dentry *dentry,
157*4882a593Smuzhiyun umode_t mode)
158*4882a593Smuzhiyun {
159*4882a593Smuzhiyun int err = vfs_mkdir(dir, dentry, mode);
160*4882a593Smuzhiyun pr_debug("mkdir(%pd2, 0%o) = %i\n", dentry, mode, err);
161*4882a593Smuzhiyun return err;
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
ovl_do_mknod(struct inode * dir,struct dentry * dentry,umode_t mode,dev_t dev)164*4882a593Smuzhiyun static inline int ovl_do_mknod(struct inode *dir, struct dentry *dentry,
165*4882a593Smuzhiyun umode_t mode, dev_t dev)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun int err = vfs_mknod(dir, dentry, mode, dev);
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun pr_debug("mknod(%pd2, 0%o, 0%o) = %i\n", dentry, mode, dev, err);
170*4882a593Smuzhiyun return err;
171*4882a593Smuzhiyun }
172*4882a593Smuzhiyun
ovl_do_symlink(struct inode * dir,struct dentry * dentry,const char * oldname)173*4882a593Smuzhiyun static inline int ovl_do_symlink(struct inode *dir, struct dentry *dentry,
174*4882a593Smuzhiyun const char *oldname)
175*4882a593Smuzhiyun {
176*4882a593Smuzhiyun int err = vfs_symlink(dir, dentry, oldname);
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun pr_debug("symlink(\"%s\", %pd2) = %i\n", oldname, dentry, err);
179*4882a593Smuzhiyun return err;
180*4882a593Smuzhiyun }
181*4882a593Smuzhiyun
ovl_do_getxattr(struct ovl_fs * ofs,struct dentry * dentry,enum ovl_xattr ox,void * value,size_t size)182*4882a593Smuzhiyun static inline ssize_t ovl_do_getxattr(struct ovl_fs *ofs, struct dentry *dentry,
183*4882a593Smuzhiyun enum ovl_xattr ox, void *value,
184*4882a593Smuzhiyun size_t size)
185*4882a593Smuzhiyun {
186*4882a593Smuzhiyun const char *name = ovl_xattr(ofs, ox);
187*4882a593Smuzhiyun struct inode *ip = d_inode(dentry);
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun return __vfs_getxattr(dentry, ip, name, value, size, XATTR_NOSECURITY);
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun
ovl_do_setxattr(struct ovl_fs * ofs,struct dentry * dentry,enum ovl_xattr ox,const void * value,size_t size)192*4882a593Smuzhiyun static inline int ovl_do_setxattr(struct ovl_fs *ofs, struct dentry *dentry,
193*4882a593Smuzhiyun enum ovl_xattr ox, const void *value,
194*4882a593Smuzhiyun size_t size)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun const char *name = ovl_xattr(ofs, ox);
197*4882a593Smuzhiyun int err = vfs_setxattr(dentry, name, value, size, 0);
198*4882a593Smuzhiyun pr_debug("setxattr(%pd2, \"%s\", \"%*pE\", %zu, 0) = %i\n",
199*4882a593Smuzhiyun dentry, name, min((int)size, 48), value, size, err);
200*4882a593Smuzhiyun return err;
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
ovl_do_removexattr(struct ovl_fs * ofs,struct dentry * dentry,enum ovl_xattr ox)203*4882a593Smuzhiyun static inline int ovl_do_removexattr(struct ovl_fs *ofs, struct dentry *dentry,
204*4882a593Smuzhiyun enum ovl_xattr ox)
205*4882a593Smuzhiyun {
206*4882a593Smuzhiyun const char *name = ovl_xattr(ofs, ox);
207*4882a593Smuzhiyun int err = vfs_removexattr(dentry, name);
208*4882a593Smuzhiyun pr_debug("removexattr(%pd2, \"%s\") = %i\n", dentry, name, err);
209*4882a593Smuzhiyun return err;
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
ovl_do_rename(struct inode * olddir,struct dentry * olddentry,struct inode * newdir,struct dentry * newdentry,unsigned int flags)212*4882a593Smuzhiyun static inline int ovl_do_rename(struct inode *olddir, struct dentry *olddentry,
213*4882a593Smuzhiyun struct inode *newdir, struct dentry *newdentry,
214*4882a593Smuzhiyun unsigned int flags)
215*4882a593Smuzhiyun {
216*4882a593Smuzhiyun int err;
217*4882a593Smuzhiyun
218*4882a593Smuzhiyun pr_debug("rename(%pd2, %pd2, 0x%x)\n", olddentry, newdentry, flags);
219*4882a593Smuzhiyun err = vfs_rename(olddir, olddentry, newdir, newdentry, NULL, flags);
220*4882a593Smuzhiyun if (err) {
221*4882a593Smuzhiyun pr_debug("...rename(%pd2, %pd2, ...) = %i\n",
222*4882a593Smuzhiyun olddentry, newdentry, err);
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun return err;
225*4882a593Smuzhiyun }
226*4882a593Smuzhiyun
ovl_do_whiteout(struct inode * dir,struct dentry * dentry)227*4882a593Smuzhiyun static inline int ovl_do_whiteout(struct inode *dir, struct dentry *dentry)
228*4882a593Smuzhiyun {
229*4882a593Smuzhiyun int err = vfs_whiteout(dir, dentry);
230*4882a593Smuzhiyun pr_debug("whiteout(%pd2) = %i\n", dentry, err);
231*4882a593Smuzhiyun return err;
232*4882a593Smuzhiyun }
233*4882a593Smuzhiyun
ovl_do_tmpfile(struct dentry * dentry,umode_t mode)234*4882a593Smuzhiyun static inline struct dentry *ovl_do_tmpfile(struct dentry *dentry, umode_t mode)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun struct dentry *ret = vfs_tmpfile(dentry, mode, 0);
237*4882a593Smuzhiyun int err = PTR_ERR_OR_ZERO(ret);
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun pr_debug("tmpfile(%pd2, 0%o) = %i\n", dentry, mode, err);
240*4882a593Smuzhiyun return ret;
241*4882a593Smuzhiyun }
242*4882a593Smuzhiyun
ovl_open_flags_need_copy_up(int flags)243*4882a593Smuzhiyun static inline bool ovl_open_flags_need_copy_up(int flags)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun if (!flags)
246*4882a593Smuzhiyun return false;
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun return ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC));
249*4882a593Smuzhiyun }
250*4882a593Smuzhiyun
251*4882a593Smuzhiyun /* util.c */
252*4882a593Smuzhiyun int ovl_want_write(struct dentry *dentry);
253*4882a593Smuzhiyun void ovl_drop_write(struct dentry *dentry);
254*4882a593Smuzhiyun struct dentry *ovl_workdir(struct dentry *dentry);
255*4882a593Smuzhiyun const struct cred *ovl_override_creds(struct super_block *sb);
256*4882a593Smuzhiyun void ovl_revert_creds(struct super_block *sb, const struct cred *oldcred);
257*4882a593Smuzhiyun int ovl_can_decode_fh(struct super_block *sb);
258*4882a593Smuzhiyun struct dentry *ovl_indexdir(struct super_block *sb);
259*4882a593Smuzhiyun bool ovl_index_all(struct super_block *sb);
260*4882a593Smuzhiyun bool ovl_verify_lower(struct super_block *sb);
261*4882a593Smuzhiyun struct ovl_entry *ovl_alloc_entry(unsigned int numlower);
262*4882a593Smuzhiyun bool ovl_dentry_remote(struct dentry *dentry);
263*4882a593Smuzhiyun void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry,
264*4882a593Smuzhiyun unsigned int mask);
265*4882a593Smuzhiyun bool ovl_dentry_weird(struct dentry *dentry);
266*4882a593Smuzhiyun enum ovl_path_type ovl_path_type(struct dentry *dentry);
267*4882a593Smuzhiyun void ovl_path_upper(struct dentry *dentry, struct path *path);
268*4882a593Smuzhiyun void ovl_path_lower(struct dentry *dentry, struct path *path);
269*4882a593Smuzhiyun void ovl_path_lowerdata(struct dentry *dentry, struct path *path);
270*4882a593Smuzhiyun enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
271*4882a593Smuzhiyun struct dentry *ovl_dentry_upper(struct dentry *dentry);
272*4882a593Smuzhiyun struct dentry *ovl_dentry_lower(struct dentry *dentry);
273*4882a593Smuzhiyun struct dentry *ovl_dentry_lowerdata(struct dentry *dentry);
274*4882a593Smuzhiyun const struct ovl_layer *ovl_layer_lower(struct dentry *dentry);
275*4882a593Smuzhiyun struct dentry *ovl_dentry_real(struct dentry *dentry);
276*4882a593Smuzhiyun struct dentry *ovl_i_dentry_upper(struct inode *inode);
277*4882a593Smuzhiyun struct inode *ovl_inode_upper(struct inode *inode);
278*4882a593Smuzhiyun struct inode *ovl_inode_lower(struct inode *inode);
279*4882a593Smuzhiyun struct inode *ovl_inode_lowerdata(struct inode *inode);
280*4882a593Smuzhiyun struct inode *ovl_inode_real(struct inode *inode);
281*4882a593Smuzhiyun struct inode *ovl_inode_realdata(struct inode *inode);
282*4882a593Smuzhiyun struct ovl_dir_cache *ovl_dir_cache(struct inode *inode);
283*4882a593Smuzhiyun void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache);
284*4882a593Smuzhiyun void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry);
285*4882a593Smuzhiyun void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry);
286*4882a593Smuzhiyun bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry);
287*4882a593Smuzhiyun bool ovl_dentry_is_opaque(struct dentry *dentry);
288*4882a593Smuzhiyun bool ovl_dentry_is_whiteout(struct dentry *dentry);
289*4882a593Smuzhiyun void ovl_dentry_set_opaque(struct dentry *dentry);
290*4882a593Smuzhiyun bool ovl_dentry_has_upper_alias(struct dentry *dentry);
291*4882a593Smuzhiyun void ovl_dentry_set_upper_alias(struct dentry *dentry);
292*4882a593Smuzhiyun bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags);
293*4882a593Smuzhiyun bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags);
294*4882a593Smuzhiyun bool ovl_has_upperdata(struct inode *inode);
295*4882a593Smuzhiyun void ovl_set_upperdata(struct inode *inode);
296*4882a593Smuzhiyun bool ovl_redirect_dir(struct super_block *sb);
297*4882a593Smuzhiyun const char *ovl_dentry_get_redirect(struct dentry *dentry);
298*4882a593Smuzhiyun void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect);
299*4882a593Smuzhiyun void ovl_inode_update(struct inode *inode, struct dentry *upperdentry);
300*4882a593Smuzhiyun void ovl_dir_modified(struct dentry *dentry, bool impurity);
301*4882a593Smuzhiyun u64 ovl_dentry_version_get(struct dentry *dentry);
302*4882a593Smuzhiyun bool ovl_is_whiteout(struct dentry *dentry);
303*4882a593Smuzhiyun struct file *ovl_path_open(struct path *path, int flags);
304*4882a593Smuzhiyun int ovl_copy_up_start(struct dentry *dentry, int flags);
305*4882a593Smuzhiyun void ovl_copy_up_end(struct dentry *dentry);
306*4882a593Smuzhiyun bool ovl_already_copied_up(struct dentry *dentry, int flags);
307*4882a593Smuzhiyun bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry);
308*4882a593Smuzhiyun bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry,
309*4882a593Smuzhiyun enum ovl_xattr ox);
310*4882a593Smuzhiyun int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
311*4882a593Smuzhiyun enum ovl_xattr ox, const void *value, size_t size,
312*4882a593Smuzhiyun int xerr);
313*4882a593Smuzhiyun int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry);
314*4882a593Smuzhiyun bool ovl_inuse_trylock(struct dentry *dentry);
315*4882a593Smuzhiyun void ovl_inuse_unlock(struct dentry *dentry);
316*4882a593Smuzhiyun bool ovl_is_inuse(struct dentry *dentry);
317*4882a593Smuzhiyun bool ovl_need_index(struct dentry *dentry);
318*4882a593Smuzhiyun int ovl_nlink_start(struct dentry *dentry);
319*4882a593Smuzhiyun void ovl_nlink_end(struct dentry *dentry);
320*4882a593Smuzhiyun int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir);
321*4882a593Smuzhiyun int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry);
322*4882a593Smuzhiyun bool ovl_is_metacopy_dentry(struct dentry *dentry);
323*4882a593Smuzhiyun char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry,
324*4882a593Smuzhiyun int padding);
325*4882a593Smuzhiyun int ovl_sync_status(struct ovl_fs *ofs);
326*4882a593Smuzhiyun
ovl_set_flag(unsigned long flag,struct inode * inode)327*4882a593Smuzhiyun static inline void ovl_set_flag(unsigned long flag, struct inode *inode)
328*4882a593Smuzhiyun {
329*4882a593Smuzhiyun set_bit(flag, &OVL_I(inode)->flags);
330*4882a593Smuzhiyun }
331*4882a593Smuzhiyun
ovl_clear_flag(unsigned long flag,struct inode * inode)332*4882a593Smuzhiyun static inline void ovl_clear_flag(unsigned long flag, struct inode *inode)
333*4882a593Smuzhiyun {
334*4882a593Smuzhiyun clear_bit(flag, &OVL_I(inode)->flags);
335*4882a593Smuzhiyun }
336*4882a593Smuzhiyun
ovl_test_flag(unsigned long flag,struct inode * inode)337*4882a593Smuzhiyun static inline bool ovl_test_flag(unsigned long flag, struct inode *inode)
338*4882a593Smuzhiyun {
339*4882a593Smuzhiyun return test_bit(flag, &OVL_I(inode)->flags);
340*4882a593Smuzhiyun }
341*4882a593Smuzhiyun
ovl_is_impuredir(struct super_block * sb,struct dentry * dentry)342*4882a593Smuzhiyun static inline bool ovl_is_impuredir(struct super_block *sb,
343*4882a593Smuzhiyun struct dentry *dentry)
344*4882a593Smuzhiyun {
345*4882a593Smuzhiyun return ovl_check_dir_xattr(sb, dentry, OVL_XATTR_IMPURE);
346*4882a593Smuzhiyun }
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun /*
349*4882a593Smuzhiyun * With xino=auto, we do best effort to keep all inodes on same st_dev and
350*4882a593Smuzhiyun * d_ino consistent with st_ino.
351*4882a593Smuzhiyun * With xino=on, we do the same effort but we warn if we failed.
352*4882a593Smuzhiyun */
ovl_xino_warn(struct super_block * sb)353*4882a593Smuzhiyun static inline bool ovl_xino_warn(struct super_block *sb)
354*4882a593Smuzhiyun {
355*4882a593Smuzhiyun return OVL_FS(sb)->config.xino == OVL_XINO_ON;
356*4882a593Smuzhiyun }
357*4882a593Smuzhiyun
358*4882a593Smuzhiyun /* All layers on same fs? */
ovl_same_fs(struct super_block * sb)359*4882a593Smuzhiyun static inline bool ovl_same_fs(struct super_block *sb)
360*4882a593Smuzhiyun {
361*4882a593Smuzhiyun return OVL_FS(sb)->xino_mode == 0;
362*4882a593Smuzhiyun }
363*4882a593Smuzhiyun
364*4882a593Smuzhiyun /* All overlay inodes have same st_dev? */
ovl_same_dev(struct super_block * sb)365*4882a593Smuzhiyun static inline bool ovl_same_dev(struct super_block *sb)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun return OVL_FS(sb)->xino_mode >= 0;
368*4882a593Smuzhiyun }
369*4882a593Smuzhiyun
ovl_xino_bits(struct super_block * sb)370*4882a593Smuzhiyun static inline unsigned int ovl_xino_bits(struct super_block *sb)
371*4882a593Smuzhiyun {
372*4882a593Smuzhiyun return ovl_same_dev(sb) ? OVL_FS(sb)->xino_mode : 0;
373*4882a593Smuzhiyun }
374*4882a593Smuzhiyun
ovl_inode_lock(struct inode * inode)375*4882a593Smuzhiyun static inline void ovl_inode_lock(struct inode *inode)
376*4882a593Smuzhiyun {
377*4882a593Smuzhiyun mutex_lock(&OVL_I(inode)->lock);
378*4882a593Smuzhiyun }
379*4882a593Smuzhiyun
ovl_inode_lock_interruptible(struct inode * inode)380*4882a593Smuzhiyun static inline int ovl_inode_lock_interruptible(struct inode *inode)
381*4882a593Smuzhiyun {
382*4882a593Smuzhiyun return mutex_lock_interruptible(&OVL_I(inode)->lock);
383*4882a593Smuzhiyun }
384*4882a593Smuzhiyun
ovl_inode_unlock(struct inode * inode)385*4882a593Smuzhiyun static inline void ovl_inode_unlock(struct inode *inode)
386*4882a593Smuzhiyun {
387*4882a593Smuzhiyun mutex_unlock(&OVL_I(inode)->lock);
388*4882a593Smuzhiyun }
389*4882a593Smuzhiyun
390*4882a593Smuzhiyun
391*4882a593Smuzhiyun /* namei.c */
392*4882a593Smuzhiyun int ovl_check_fb_len(struct ovl_fb *fb, int fb_len);
393*4882a593Smuzhiyun
ovl_check_fh_len(struct ovl_fh * fh,int fh_len)394*4882a593Smuzhiyun static inline int ovl_check_fh_len(struct ovl_fh *fh, int fh_len)
395*4882a593Smuzhiyun {
396*4882a593Smuzhiyun if (fh_len < sizeof(struct ovl_fh))
397*4882a593Smuzhiyun return -EINVAL;
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun return ovl_check_fb_len(&fh->fb, fh_len - OVL_FH_WIRE_OFFSET);
400*4882a593Smuzhiyun }
401*4882a593Smuzhiyun
402*4882a593Smuzhiyun struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt,
403*4882a593Smuzhiyun bool connected);
404*4882a593Smuzhiyun int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
405*4882a593Smuzhiyun struct dentry *upperdentry, struct ovl_path **stackp);
406*4882a593Smuzhiyun int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry,
407*4882a593Smuzhiyun enum ovl_xattr ox, struct dentry *real, bool is_upper,
408*4882a593Smuzhiyun bool set);
409*4882a593Smuzhiyun struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index);
410*4882a593Smuzhiyun int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index);
411*4882a593Smuzhiyun int ovl_get_index_name(struct dentry *origin, struct qstr *name);
412*4882a593Smuzhiyun struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh);
413*4882a593Smuzhiyun struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
414*4882a593Smuzhiyun struct dentry *origin, bool verify);
415*4882a593Smuzhiyun int ovl_path_next(int idx, struct dentry *dentry, struct path *path);
416*4882a593Smuzhiyun struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
417*4882a593Smuzhiyun unsigned int flags);
418*4882a593Smuzhiyun bool ovl_lower_positive(struct dentry *dentry);
419*4882a593Smuzhiyun
ovl_verify_origin(struct ovl_fs * ofs,struct dentry * upper,struct dentry * origin,bool set)420*4882a593Smuzhiyun static inline int ovl_verify_origin(struct ovl_fs *ofs, struct dentry *upper,
421*4882a593Smuzhiyun struct dentry *origin, bool set)
422*4882a593Smuzhiyun {
423*4882a593Smuzhiyun return ovl_verify_set_fh(ofs, upper, OVL_XATTR_ORIGIN, origin,
424*4882a593Smuzhiyun false, set);
425*4882a593Smuzhiyun }
426*4882a593Smuzhiyun
ovl_verify_upper(struct ovl_fs * ofs,struct dentry * index,struct dentry * upper,bool set)427*4882a593Smuzhiyun static inline int ovl_verify_upper(struct ovl_fs *ofs, struct dentry *index,
428*4882a593Smuzhiyun struct dentry *upper, bool set)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun return ovl_verify_set_fh(ofs, index, OVL_XATTR_UPPER, upper, true, set);
431*4882a593Smuzhiyun }
432*4882a593Smuzhiyun
433*4882a593Smuzhiyun /* readdir.c */
434*4882a593Smuzhiyun extern const struct file_operations ovl_dir_operations;
435*4882a593Smuzhiyun struct file *ovl_dir_real_file(const struct file *file, bool want_upper);
436*4882a593Smuzhiyun int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list);
437*4882a593Smuzhiyun void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list);
438*4882a593Smuzhiyun void ovl_cache_free(struct list_head *list);
439*4882a593Smuzhiyun void ovl_dir_cache_free(struct inode *inode);
440*4882a593Smuzhiyun int ovl_check_d_type_supported(struct path *realpath);
441*4882a593Smuzhiyun int ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
442*4882a593Smuzhiyun struct dentry *dentry, int level);
443*4882a593Smuzhiyun int ovl_indexdir_cleanup(struct ovl_fs *ofs);
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun /*
446*4882a593Smuzhiyun * Can we iterate real dir directly?
447*4882a593Smuzhiyun *
448*4882a593Smuzhiyun * Non-merge dir may contain whiteouts from a time it was a merge upper, before
449*4882a593Smuzhiyun * lower dir was removed under it and possibly before it was rotated from upper
450*4882a593Smuzhiyun * to lower layer.
451*4882a593Smuzhiyun */
ovl_dir_is_real(struct dentry * dir)452*4882a593Smuzhiyun static inline bool ovl_dir_is_real(struct dentry *dir)
453*4882a593Smuzhiyun {
454*4882a593Smuzhiyun return !ovl_test_flag(OVL_WHITEOUTS, d_inode(dir));
455*4882a593Smuzhiyun }
456*4882a593Smuzhiyun
457*4882a593Smuzhiyun /* inode.c */
458*4882a593Smuzhiyun int ovl_set_nlink_upper(struct dentry *dentry);
459*4882a593Smuzhiyun int ovl_set_nlink_lower(struct dentry *dentry);
460*4882a593Smuzhiyun unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry,
461*4882a593Smuzhiyun struct dentry *upperdentry,
462*4882a593Smuzhiyun unsigned int fallback);
463*4882a593Smuzhiyun int ovl_setattr(struct dentry *dentry, struct iattr *attr);
464*4882a593Smuzhiyun int ovl_getattr(const struct path *path, struct kstat *stat,
465*4882a593Smuzhiyun u32 request_mask, unsigned int flags);
466*4882a593Smuzhiyun int ovl_permission(struct inode *inode, int mask);
467*4882a593Smuzhiyun int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
468*4882a593Smuzhiyun const void *value, size_t size, int flags);
469*4882a593Smuzhiyun int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
470*4882a593Smuzhiyun void *value, size_t size, int flags);
471*4882a593Smuzhiyun ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
472*4882a593Smuzhiyun struct posix_acl *ovl_get_acl(struct inode *inode, int type);
473*4882a593Smuzhiyun int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags);
474*4882a593Smuzhiyun bool ovl_is_private_xattr(struct super_block *sb, const char *name);
475*4882a593Smuzhiyun
476*4882a593Smuzhiyun struct ovl_inode_params {
477*4882a593Smuzhiyun struct inode *newinode;
478*4882a593Smuzhiyun struct dentry *upperdentry;
479*4882a593Smuzhiyun struct ovl_path *lowerpath;
480*4882a593Smuzhiyun bool index;
481*4882a593Smuzhiyun unsigned int numlower;
482*4882a593Smuzhiyun char *redirect;
483*4882a593Smuzhiyun struct dentry *lowerdata;
484*4882a593Smuzhiyun };
485*4882a593Smuzhiyun void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip,
486*4882a593Smuzhiyun unsigned long ino, int fsid);
487*4882a593Smuzhiyun struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev);
488*4882a593Smuzhiyun struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
489*4882a593Smuzhiyun bool is_upper);
490*4882a593Smuzhiyun bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir);
491*4882a593Smuzhiyun struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir);
492*4882a593Smuzhiyun struct inode *ovl_get_inode(struct super_block *sb,
493*4882a593Smuzhiyun struct ovl_inode_params *oip);
ovl_copyattr(struct inode * from,struct inode * to)494*4882a593Smuzhiyun static inline void ovl_copyattr(struct inode *from, struct inode *to)
495*4882a593Smuzhiyun {
496*4882a593Smuzhiyun to->i_uid = from->i_uid;
497*4882a593Smuzhiyun to->i_gid = from->i_gid;
498*4882a593Smuzhiyun to->i_mode = from->i_mode;
499*4882a593Smuzhiyun to->i_atime = from->i_atime;
500*4882a593Smuzhiyun to->i_mtime = from->i_mtime;
501*4882a593Smuzhiyun to->i_ctime = from->i_ctime;
502*4882a593Smuzhiyun i_size_write(to, i_size_read(from));
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun
ovl_copyflags(struct inode * from,struct inode * to)505*4882a593Smuzhiyun static inline void ovl_copyflags(struct inode *from, struct inode *to)
506*4882a593Smuzhiyun {
507*4882a593Smuzhiyun unsigned int mask = S_SYNC | S_IMMUTABLE | S_APPEND | S_NOATIME;
508*4882a593Smuzhiyun
509*4882a593Smuzhiyun inode_set_flags(to, from->i_flags & mask, mask);
510*4882a593Smuzhiyun }
511*4882a593Smuzhiyun
512*4882a593Smuzhiyun /* dir.c */
513*4882a593Smuzhiyun extern const struct inode_operations ovl_dir_inode_operations;
514*4882a593Smuzhiyun int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct inode *dir,
515*4882a593Smuzhiyun struct dentry *dentry);
516*4882a593Smuzhiyun struct ovl_cattr {
517*4882a593Smuzhiyun dev_t rdev;
518*4882a593Smuzhiyun umode_t mode;
519*4882a593Smuzhiyun const char *link;
520*4882a593Smuzhiyun struct dentry *hardlink;
521*4882a593Smuzhiyun };
522*4882a593Smuzhiyun
523*4882a593Smuzhiyun #define OVL_CATTR(m) (&(struct ovl_cattr) { .mode = (m) })
524*4882a593Smuzhiyun
525*4882a593Smuzhiyun int ovl_mkdir_real(struct inode *dir, struct dentry **newdentry, umode_t mode);
526*4882a593Smuzhiyun struct dentry *ovl_create_real(struct inode *dir, struct dentry *newdentry,
527*4882a593Smuzhiyun struct ovl_cattr *attr);
528*4882a593Smuzhiyun int ovl_cleanup(struct inode *dir, struct dentry *dentry);
529*4882a593Smuzhiyun struct dentry *ovl_lookup_temp(struct dentry *workdir);
530*4882a593Smuzhiyun struct dentry *ovl_create_temp(struct dentry *workdir, struct ovl_cattr *attr);
531*4882a593Smuzhiyun
532*4882a593Smuzhiyun /* file.c */
533*4882a593Smuzhiyun extern const struct file_operations ovl_file_operations;
534*4882a593Smuzhiyun int __init ovl_aio_request_cache_init(void);
535*4882a593Smuzhiyun void ovl_aio_request_cache_destroy(void);
536*4882a593Smuzhiyun long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
537*4882a593Smuzhiyun long ovl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
538*4882a593Smuzhiyun
539*4882a593Smuzhiyun /* copy_up.c */
540*4882a593Smuzhiyun int ovl_copy_up(struct dentry *dentry);
541*4882a593Smuzhiyun int ovl_copy_up_with_data(struct dentry *dentry);
542*4882a593Smuzhiyun int ovl_maybe_copy_up(struct dentry *dentry, int flags);
543*4882a593Smuzhiyun int ovl_copy_xattr(struct super_block *sb, struct dentry *old,
544*4882a593Smuzhiyun struct dentry *new);
545*4882a593Smuzhiyun int ovl_set_attr(struct dentry *upper, struct kstat *stat);
546*4882a593Smuzhiyun struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper);
547*4882a593Smuzhiyun int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
548*4882a593Smuzhiyun struct dentry *upper);
549*4882a593Smuzhiyun
550*4882a593Smuzhiyun /* export.c */
551*4882a593Smuzhiyun extern const struct export_operations ovl_export_operations;
552