]> git.ipfire.org Git - thirdparty/linux.git/blame - fs/overlayfs/overlayfs.h
ovl: modify layer parameter parsing
[thirdparty/linux.git] / fs / overlayfs / overlayfs.h
CommitLineData
d2912cb1 1/* SPDX-License-Identifier: GPL-2.0-only */
e9be9d5e
MS
2/*
3 *
4 * Copyright (C) 2011 Novell Inc.
e9be9d5e
MS
5 */
6
7#include <linux/kernel.h>
3a1e819b 8#include <linux/uuid.h>
46e5d0a3 9#include <linux/fs.h>
22f289ce 10#include <linux/namei.h>
0e641857
CB
11#include <linux/posix_acl.h>
12#include <linux/posix_acl_xattr.h>
ee023c30 13#include "ovl_entry.h"
e9be9d5e 14
1bd0a3ae 15#undef pr_fmt
16#define pr_fmt(fmt) "overlayfs: " fmt
17
e9be9d5e 18enum ovl_path_type {
38e813db
MS
19 __OVL_PATH_UPPER = (1 << 0),
20 __OVL_PATH_MERGE = (1 << 1),
59548503 21 __OVL_PATH_ORIGIN = (1 << 2),
e9be9d5e
MS
22};
23
1afaba1e
MS
24#define OVL_TYPE_UPPER(type) ((type) & __OVL_PATH_UPPER)
25#define OVL_TYPE_MERGE(type) ((type) & __OVL_PATH_MERGE)
59548503 26#define OVL_TYPE_ORIGIN(type) ((type) & __OVL_PATH_ORIGIN)
d837a49b 27
2d2f2d73
MS
28#define OVL_XATTR_NAMESPACE "overlay."
29#define OVL_XATTR_TRUSTED_PREFIX XATTR_TRUSTED_PREFIX OVL_XATTR_NAMESPACE
30#define OVL_XATTR_USER_PREFIX XATTR_USER_PREFIX OVL_XATTR_NAMESPACE
43d193f8
MS
31
32enum ovl_xattr {
33 OVL_XATTR_OPAQUE,
34 OVL_XATTR_REDIRECT,
35 OVL_XATTR_ORIGIN,
36 OVL_XATTR_IMPURE,
37 OVL_XATTR_NLINK,
38 OVL_XATTR_UPPER,
39 OVL_XATTR_METACOPY,
096a218a 40 OVL_XATTR_PROTATTR,
43d193f8 41};
3a1e819b 42
c62520a8 43enum ovl_inode_flag {
b79e05aa 44 /* Pure upper dir that may contain non pure upper entries */
13c72075 45 OVL_IMPURE,
b79e05aa
AG
46 /* Non-merge dir that may contain whiteout entries */
47 OVL_WHITEOUTS,
359f392c 48 OVL_INDEX,
0c288874 49 OVL_UPPERDATA,
a00c2d59
VG
50 /* Inode number will remain constant over copy up. */
51 OVL_CONST_INO,
13c72075
MS
52};
53
c62520a8
AG
54enum ovl_entry_flag {
55 OVL_E_UPPER_ALIAS,
56 OVL_E_OPAQUE,
2ca3c148 57 OVL_E_CONNECTED,
c62520a8
AG
58};
59
af5f2396
AG
60enum {
61 OVL_REDIRECT_OFF, /* "off" mode is never used. In effect */
62 OVL_REDIRECT_FOLLOW, /* ...it translates to either "follow" */
63 OVL_REDIRECT_NOFOLLOW, /* ...or "nofollow". */
64 OVL_REDIRECT_ON,
65};
66
926e94d7
AG
67enum {
68 OVL_XINO_OFF,
69 OVL_XINO_AUTO,
70 OVL_XINO_ON,
71};
72
ac519625
AG
73/* The set of options that user requested explicitly via mount options */
74struct ovl_opt_set {
75 bool metacopy;
76 bool redirect;
77 bool nfs_export;
78 bool index;
79};
80
3a1e819b
AG
81/*
82 * The tuple (fh,uuid) is a universal unique identifier for a copy up origin,
83 * where:
84 * origin.fh - exported file handle of the lower file
85 * origin.uuid - uuid of the lower filesystem
86 */
87#define OVL_FH_VERSION 0
88#define OVL_FH_MAGIC 0xfb
89
90/* CPU byte order required for fid decoding: */
91#define OVL_FH_FLAG_BIG_ENDIAN (1 << 0)
92#define OVL_FH_FLAG_ANY_ENDIAN (1 << 1)
54fb347e
AG
93/* Is the real inode encoded in fid an upper inode? */
94#define OVL_FH_FLAG_PATH_UPPER (1 << 2)
3a1e819b 95
961af647
AG
96#define OVL_FH_FLAG_ALL (OVL_FH_FLAG_BIG_ENDIAN | OVL_FH_FLAG_ANY_ENDIAN | \
97 OVL_FH_FLAG_PATH_UPPER)
3a1e819b
AG
98
99#if defined(__LITTLE_ENDIAN)
100#define OVL_FH_FLAG_CPU_ENDIAN 0
101#elif defined(__BIG_ENDIAN)
102#define OVL_FH_FLAG_CPU_ENDIAN OVL_FH_FLAG_BIG_ENDIAN
103#else
104#error Endianness not defined
105#endif
106
cbe7fba8
AG
107/* The type used to be returned by overlay exportfs for misaligned fid */
108#define OVL_FILEID_V0 0xfb
109/* The type returned by overlay exportfs for 32bit aligned fid */
110#define OVL_FILEID_V1 0xf8
8ed5eec9 111
cbe7fba8
AG
112/* On-disk format for "origin" file handle */
113struct ovl_fb {
3a1e819b
AG
114 u8 version; /* 0 */
115 u8 magic; /* 0xfb */
116 u8 len; /* size of this header + size of fid */
117 u8 flags; /* OVL_FH_FLAG_* */
118 u8 type; /* fid_type of fid */
01633fd2 119 uuid_t uuid; /* uuid of filesystem */
0efbe7c4 120 u32 fid[]; /* file identifier should be 32bit aligned in-memory */
3a1e819b 121} __packed;
e9be9d5e 122
cbe7fba8
AG
123/* In-memory and on-wire format for overlay file handle */
124struct ovl_fh {
125 u8 padding[3]; /* make sure fb.fid is 32bit aligned */
126 union {
127 struct ovl_fb fb;
cf8aa9bf 128 DECLARE_FLEX_ARRAY(u8, buf);
cbe7fba8
AG
129 };
130} __packed;
131
132#define OVL_FH_WIRE_OFFSET offsetof(struct ovl_fh, fb)
133#define OVL_FH_LEN(fh) (OVL_FH_WIRE_OFFSET + (fh)->fb.len)
134#define OVL_FH_FID_OFFSET (OVL_FH_WIRE_OFFSET + \
135 offsetof(struct ovl_fb, fid))
136
2d2f2d73 137extern const char *const ovl_xattr_table[][2];
43d193f8
MS
138static inline const char *ovl_xattr(struct ovl_fs *ofs, enum ovl_xattr ox)
139{
2d2f2d73 140 return ovl_xattr_table[ox][ofs->config.userxattr];
43d193f8
MS
141}
142
a15506ea
CB
143/*
144 * When changing ownership of an upper object map the intended ownership
145 * according to the upper layer's idmapping. When an upper mount idmaps files
146 * that are stored on-disk as owned by id 1001 to id 1000 this means stat on
147 * this object will report it as being owned by id 1000 when calling stat via
148 * the upper mount.
149 * In order to change ownership of an object so stat reports id 1000 when
150 * called on an idmapped upper mount the value written to disk - i.e., the
151 * value stored in ia_*id - must 1001. The mount mapping helper will thus take
152 * care to map 1000 to 1001.
153 * The mnt idmapping helpers are nops if the upper layer isn't idmapped.
154 */
155static inline int ovl_do_notify_change(struct ovl_fs *ofs,
156 struct dentry *upperdentry,
157 struct iattr *attr)
158{
abf08576 159 return notify_change(ovl_upper_mnt_idmap(ofs), upperdentry, attr, NULL);
a15506ea
CB
160}
161
576bb263
CB
162static inline int ovl_do_rmdir(struct ovl_fs *ofs,
163 struct inode *dir, struct dentry *dentry)
e9be9d5e 164{
abf08576 165 int err = vfs_rmdir(ovl_upper_mnt_idmap(ofs), dir, dentry);
6cf00764 166
e9be9d5e
MS
167 pr_debug("rmdir(%pd2) = %i\n", dentry, err);
168 return err;
169}
170
576bb263
CB
171static inline int ovl_do_unlink(struct ovl_fs *ofs, struct inode *dir,
172 struct dentry *dentry)
e9be9d5e 173{
abf08576 174 int err = vfs_unlink(ovl_upper_mnt_idmap(ofs), dir, dentry, NULL);
6cf00764 175
e9be9d5e
MS
176 pr_debug("unlink(%pd2) = %i\n", dentry, err);
177 return err;
178}
179
576bb263
CB
180static inline int ovl_do_link(struct ovl_fs *ofs, struct dentry *old_dentry,
181 struct inode *dir, struct dentry *new_dentry)
e9be9d5e 182{
abf08576
CB
183 int err = vfs_link(old_dentry, ovl_upper_mnt_idmap(ofs), dir,
184 new_dentry, NULL);
6cf00764
AG
185
186 pr_debug("link(%pd2, %pd2) = %i\n", old_dentry, new_dentry, err);
e9be9d5e
MS
187 return err;
188}
189
576bb263
CB
190static inline int ovl_do_create(struct ovl_fs *ofs,
191 struct inode *dir, struct dentry *dentry,
6cf00764 192 umode_t mode)
e9be9d5e 193{
abf08576 194 int err = vfs_create(ovl_upper_mnt_idmap(ofs), dir, dentry, mode, true);
6cf00764
AG
195
196 pr_debug("create(%pd2, 0%o) = %i\n", dentry, mode, err);
e9be9d5e
MS
197 return err;
198}
199
576bb263
CB
200static inline int ovl_do_mkdir(struct ovl_fs *ofs,
201 struct inode *dir, struct dentry *dentry,
6cf00764 202 umode_t mode)
e9be9d5e 203{
abf08576 204 int err = vfs_mkdir(ovl_upper_mnt_idmap(ofs), dir, dentry, mode);
6cf00764 205 pr_debug("mkdir(%pd2, 0%o) = %i\n", dentry, mode, err);
e9be9d5e
MS
206 return err;
207}
208
576bb263
CB
209static inline int ovl_do_mknod(struct ovl_fs *ofs,
210 struct inode *dir, struct dentry *dentry,
6cf00764 211 umode_t mode, dev_t dev)
e9be9d5e 212{
abf08576 213 int err = vfs_mknod(ovl_upper_mnt_idmap(ofs), dir, dentry, mode, dev);
6cf00764
AG
214
215 pr_debug("mknod(%pd2, 0%o, 0%o) = %i\n", dentry, mode, dev, err);
e9be9d5e
MS
216 return err;
217}
218
576bb263
CB
219static inline int ovl_do_symlink(struct ovl_fs *ofs,
220 struct inode *dir, struct dentry *dentry,
6cf00764 221 const char *oldname)
e9be9d5e 222{
abf08576 223 int err = vfs_symlink(ovl_upper_mnt_idmap(ofs), dir, dentry, oldname);
6cf00764
AG
224
225 pr_debug("symlink(\"%s\", %pd2) = %i\n", oldname, dentry, err);
e9be9d5e
MS
226 return err;
227}
228
2d343087 229static inline ssize_t ovl_do_getxattr(const struct path *path, const char *name,
dad7017a 230 void *value, size_t size)
d5dc7486 231{
dad7017a
CB
232 int err, len;
233
234 WARN_ON(path->dentry->d_sb != path->mnt->mnt_sb);
235
4609e1f1 236 err = vfs_getxattr(mnt_idmap(path->mnt), path->dentry,
dad7017a
CB
237 name, value, size);
238 len = (value && err > 0) ? err : 0;
5e717c6f
AG
239
240 pr_debug("getxattr(%pd2, \"%s\", \"%*pE\", %zu, 0) = %i\n",
dad7017a 241 path->dentry, name, min(len, 48), value, size, err);
5e717c6f 242 return err;
d5dc7486
MS
243}
244
dad7017a
CB
245static inline ssize_t ovl_getxattr_upper(struct ovl_fs *ofs,
246 struct dentry *upperdentry,
247 enum ovl_xattr ox, void *value,
248 size_t size)
249{
250 struct path upperpath = {
251 .dentry = upperdentry,
252 .mnt = ovl_upper_mnt(ofs),
253 };
254
255 return ovl_do_getxattr(&upperpath, ovl_xattr(ofs, ox), value, size);
256}
257
258static inline ssize_t ovl_path_getxattr(struct ovl_fs *ofs,
2d343087 259 const struct path *path,
dad7017a
CB
260 enum ovl_xattr ox, void *value,
261 size_t size)
c914c0e2 262{
dad7017a 263 return ovl_do_getxattr(path, ovl_xattr(ofs, ox), value, size);
c914c0e2
AG
264}
265
610afc0b 266static inline int ovl_do_setxattr(struct ovl_fs *ofs, struct dentry *dentry,
c914c0e2
AG
267 const char *name, const void *value,
268 size_t size, int flags)
e9be9d5e 269{
4609e1f1 270 int err = vfs_setxattr(ovl_upper_mnt_idmap(ofs), dentry, name,
6344e669 271 value, size, flags);
c914c0e2
AG
272
273 pr_debug("setxattr(%pd2, \"%s\", \"%*pE\", %zu, %d) = %i\n",
274 dentry, name, min((int)size, 48), value, size, flags, err);
e9be9d5e
MS
275 return err;
276}
277
c914c0e2
AG
278static inline int ovl_setxattr(struct ovl_fs *ofs, struct dentry *dentry,
279 enum ovl_xattr ox, const void *value,
280 size_t size)
281{
282 return ovl_do_setxattr(ofs, dentry, ovl_xattr(ofs, ox), value, size, 0);
283}
284
610afc0b 285static inline int ovl_do_removexattr(struct ovl_fs *ofs, struct dentry *dentry,
c914c0e2 286 const char *name)
e9be9d5e 287{
4609e1f1 288 int err = vfs_removexattr(ovl_upper_mnt_idmap(ofs), dentry, name);
e9be9d5e
MS
289 pr_debug("removexattr(%pd2, \"%s\") = %i\n", dentry, name, err);
290 return err;
291}
292
c914c0e2
AG
293static inline int ovl_removexattr(struct ovl_fs *ofs, struct dentry *dentry,
294 enum ovl_xattr ox)
295{
296 return ovl_do_removexattr(ofs, dentry, ovl_xattr(ofs, ox));
297}
298
0e641857
CB
299static inline int ovl_do_set_acl(struct ovl_fs *ofs, struct dentry *dentry,
300 const char *acl_name, struct posix_acl *acl)
301{
13e83a49 302 return vfs_set_acl(ovl_upper_mnt_idmap(ofs), dentry, acl_name, acl);
0e641857
CB
303}
304
305static inline int ovl_do_remove_acl(struct ovl_fs *ofs, struct dentry *dentry,
306 const char *acl_name)
307{
13e83a49 308 return vfs_remove_acl(ovl_upper_mnt_idmap(ofs), dentry, acl_name);
0e641857
CB
309}
310
576bb263
CB
311static inline int ovl_do_rename(struct ovl_fs *ofs, struct inode *olddir,
312 struct dentry *olddentry, struct inode *newdir,
313 struct dentry *newdentry, unsigned int flags)
e9be9d5e
MS
314{
315 int err;
9fe61450 316 struct renamedata rd = {
abf08576 317 .old_mnt_idmap = ovl_upper_mnt_idmap(ofs),
9fe61450
CB
318 .old_dir = olddir,
319 .old_dentry = olddentry,
abf08576 320 .new_mnt_idmap = ovl_upper_mnt_idmap(ofs),
9fe61450
CB
321 .new_dir = newdir,
322 .new_dentry = newdentry,
323 .flags = flags,
324 };
e9be9d5e 325
6cf00764 326 pr_debug("rename(%pd2, %pd2, 0x%x)\n", olddentry, newdentry, flags);
9fe61450 327 err = vfs_rename(&rd);
e9be9d5e 328 if (err) {
2773bf00 329 pr_debug("...rename(%pd2, %pd2, ...) = %i\n",
e9be9d5e
MS
330 olddentry, newdentry, err);
331 }
332 return err;
333}
334
576bb263
CB
335static inline int ovl_do_whiteout(struct ovl_fs *ofs,
336 struct inode *dir, struct dentry *dentry)
e9be9d5e 337{
abf08576 338 int err = vfs_whiteout(ovl_upper_mnt_idmap(ofs), dir, dentry);
e9be9d5e
MS
339 pr_debug("whiteout(%pd2) = %i\n", dentry, err);
340 return err;
341}
342
2b1a7746
MS
343static inline struct file *ovl_do_tmpfile(struct ovl_fs *ofs,
344 struct dentry *dentry, umode_t mode)
e7f52429 345{
2b1a7746 346 struct path path = { .mnt = ovl_upper_mnt(ofs), .dentry = dentry };
abf08576 347 struct file *file = vfs_tmpfile_open(ovl_upper_mnt_idmap(ofs), &path, mode,
2b1a7746
MS
348 O_LARGEFILE | O_WRONLY, current_cred());
349 int err = PTR_ERR_OR_ZERO(file);
e7f52429
AG
350
351 pr_debug("tmpfile(%pd2, 0%o) = %i\n", dentry, mode, err);
2b1a7746 352 return file;
e7f52429
AG
353}
354
22f289ce
CB
355static inline struct dentry *ovl_lookup_upper(struct ovl_fs *ofs,
356 const char *name,
357 struct dentry *base, int len)
358{
4609e1f1 359 return lookup_one(ovl_upper_mnt_idmap(ofs), name, base, len);
22f289ce
CB
360}
361
0c288874
VG
362static inline bool ovl_open_flags_need_copy_up(int flags)
363{
364 if (!flags)
365 return false;
366
367 return ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC));
368}
369
ca45275c 370
b36a5780
CB
371/* params.c */
372#define OVL_MAX_STACK 500
373
374struct ovl_fs_context_layer {
375 char *name;
376 struct path path;
377};
378
379struct ovl_fs_context {
380 struct path upper;
381 struct path work;
382 size_t capacity;
383 size_t nr; /* includes nr_data */
384 size_t nr_data;
385 struct ovl_opt_set set;
386 struct ovl_fs_context_layer *lower;
387};
388
389int ovl_parse_param_upperdir(const char *name, struct fs_context *fc,
390 bool workdir);
391int ovl_parse_param_lowerdir(const char *name, struct fs_context *fc);
392void ovl_parse_param_drop_lowerdir(struct ovl_fs_context *ctx);
393
bbb1e54d
MS
394/* util.c */
395int ovl_want_write(struct dentry *dentry);
396void ovl_drop_write(struct dentry *dentry);
397struct dentry *ovl_workdir(struct dentry *dentry);
398const struct cred *ovl_override_creds(struct super_block *sb);
e487d889 399int ovl_can_decode_fh(struct super_block *sb);
02bcd157 400struct dentry *ovl_indexdir(struct super_block *sb);
f168f109
AG
401bool ovl_index_all(struct super_block *sb);
402bool ovl_verify_lower(struct super_block *sb);
163db0da
AG
403struct ovl_path *ovl_stack_alloc(unsigned int n);
404void ovl_stack_cpy(struct ovl_path *dst, struct ovl_path *src, unsigned int n);
405void ovl_stack_put(struct ovl_path *stack, unsigned int n);
406void ovl_stack_free(struct ovl_path *stack, unsigned int n);
bbb1e54d 407struct ovl_entry *ovl_alloc_entry(unsigned int numlower);
163db0da 408void ovl_free_entry(struct ovl_entry *oe);
bbb1e54d 409bool ovl_dentry_remote(struct dentry *dentry);
b07d5cc9 410void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *realdentry);
0af950f5
AG
411void ovl_dentry_init_reval(struct dentry *dentry, struct dentry *upperdentry,
412 struct ovl_entry *oe);
b07d5cc9 413void ovl_dentry_init_flags(struct dentry *dentry, struct dentry *upperdentry,
0af950f5 414 struct ovl_entry *oe, unsigned int mask);
bbb1e54d 415bool ovl_dentry_weird(struct dentry *dentry);
e9be9d5e 416enum ovl_path_type ovl_path_type(struct dentry *dentry);
e9be9d5e
MS
417void ovl_path_upper(struct dentry *dentry, struct path *path);
418void ovl_path_lower(struct dentry *dentry, struct path *path);
4f93b426 419void ovl_path_lowerdata(struct dentry *dentry, struct path *path);
b2dd05f1 420struct inode *ovl_i_path_real(struct inode *inode, struct path *path);
e9be9d5e 421enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
1248ea4b 422enum ovl_path_type ovl_path_realdata(struct dentry *dentry, struct path *path);
e9be9d5e
MS
423struct dentry *ovl_dentry_upper(struct dentry *dentry);
424struct dentry *ovl_dentry_lower(struct dentry *dentry);
647d253f 425struct dentry *ovl_dentry_lowerdata(struct dentry *dentry);
42dd69ae 426int ovl_dentry_set_lowerdata(struct dentry *dentry, struct ovl_path *datapath);
ffa5723c 427const struct ovl_layer *ovl_i_layer_lower(struct inode *inode);
13464165 428const struct ovl_layer *ovl_layer_lower(struct dentry *dentry);
e9be9d5e 429struct dentry *ovl_dentry_real(struct dentry *dentry);
1d88f183 430struct dentry *ovl_i_dentry_upper(struct inode *inode);
09d8b586
MS
431struct inode *ovl_inode_upper(struct inode *inode);
432struct inode *ovl_inode_lower(struct inode *inode);
2664bd08 433struct inode *ovl_inode_lowerdata(struct inode *inode);
09d8b586 434struct inode *ovl_inode_real(struct inode *inode);
4823d49c 435struct inode *ovl_inode_realdata(struct inode *inode);
2b21da92 436const char *ovl_lowerdata_redirect(struct inode *inode);
4edb83bb
MS
437struct ovl_dir_cache *ovl_dir_cache(struct inode *inode);
438void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache);
c62520a8
AG
439void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry);
440void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry);
441bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry);
e9be9d5e 442bool ovl_dentry_is_opaque(struct dentry *dentry);
c412ce49 443bool ovl_dentry_is_whiteout(struct dentry *dentry);
5cf5b477 444void ovl_dentry_set_opaque(struct dentry *dentry);
55acc661
MS
445bool ovl_dentry_has_upper_alias(struct dentry *dentry);
446void ovl_dentry_set_upper_alias(struct dentry *dentry);
0c288874
VG
447bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags);
448bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags);
449bool ovl_has_upperdata(struct inode *inode);
450void ovl_set_upperdata(struct inode *inode);
a6c60655
MS
451const char *ovl_dentry_get_redirect(struct dentry *dentry);
452void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect);
09d8b586 453void ovl_inode_update(struct inode *inode, struct dentry *upperdentry);
d9854c87 454void ovl_dir_modified(struct dentry *dentry, bool impurity);
1fa9c5c5 455u64 ovl_inode_version_get(struct inode *inode);
bbb1e54d 456bool ovl_is_whiteout(struct dentry *dentry);
2d343087 457struct file *ovl_path_open(const struct path *path, int flags);
0c288874 458int ovl_copy_up_start(struct dentry *dentry, int flags);
39d3d60a 459void ovl_copy_up_end(struct dentry *dentry);
0c288874 460bool ovl_already_copied_up(struct dentry *dentry, int flags);
2d343087 461bool ovl_path_check_dir_xattr(struct ovl_fs *ofs, const struct path *path,
dad7017a 462 enum ovl_xattr ox);
2d343087 463bool ovl_path_check_origin_xattr(struct ovl_fs *ofs, const struct path *path);
dad7017a
CB
464
465static inline bool ovl_check_origin_xattr(struct ovl_fs *ofs,
466 struct dentry *upperdentry)
467{
468 struct path upperpath = {
469 .dentry = upperdentry,
470 .mnt = ovl_upper_mnt(ofs),
471 };
472 return ovl_path_check_origin_xattr(ofs, &upperpath);
473}
474
a0c236b1 475int ovl_check_setxattr(struct ovl_fs *ofs, struct dentry *upperdentry,
43d193f8 476 enum ovl_xattr ox, const void *value, size_t size,
f3a15685
AG
477 int xerr);
478int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry);
ad0af710
AG
479bool ovl_inuse_trylock(struct dentry *dentry);
480void ovl_inuse_unlock(struct dentry *dentry);
146d62e5 481bool ovl_is_inuse(struct dentry *dentry);
24b33ee1 482bool ovl_need_index(struct dentry *dentry);
0e32992f
AG
483int ovl_nlink_start(struct dentry *dentry);
484void ovl_nlink_end(struct dentry *dentry);
5820dc08 485int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir);
2d343087 486int ovl_check_metacopy_xattr(struct ovl_fs *ofs, const struct path *path);
67d756c2 487bool ovl_is_metacopy_dentry(struct dentry *dentry);
2d343087 488char *ovl_get_redirect_xattr(struct ovl_fs *ofs, const struct path *path, int padding);
335d3fc5 489int ovl_sync_status(struct ovl_fs *ofs);
f3a15685 490
65cd913e
AG
491static inline void ovl_set_flag(unsigned long flag, struct inode *inode)
492{
493 set_bit(flag, &OVL_I(inode)->flags);
494}
495
496static inline void ovl_clear_flag(unsigned long flag, struct inode *inode)
497{
498 clear_bit(flag, &OVL_I(inode)->flags);
499}
500
501static inline bool ovl_test_flag(unsigned long flag, struct inode *inode)
502{
503 return test_bit(flag, &OVL_I(inode)->flags);
504}
505
610afc0b 506static inline bool ovl_is_impuredir(struct super_block *sb,
dad7017a 507 struct dentry *upperdentry)
f3a15685 508{
dad7017a
CB
509 struct ovl_fs *ofs = OVL_FS(sb);
510 struct path upperpath = {
511 .dentry = upperdentry,
512 .mnt = ovl_upper_mnt(ofs),
513 };
514
515 return ovl_path_check_dir_xattr(ofs, &upperpath, OVL_XATTR_IMPURE);
f3a15685
AG
516}
517
af5f2396
AG
518static inline bool ovl_redirect_follow(struct ovl_fs *ofs)
519{
520 return ofs->config.redirect_mode != OVL_REDIRECT_NOFOLLOW;
521}
522
523static inline bool ovl_redirect_dir(struct ovl_fs *ofs)
524{
525 return ofs->config.redirect_mode == OVL_REDIRECT_ON;
526}
527
926e94d7
AG
528/*
529 * With xino=auto, we do best effort to keep all inodes on same st_dev and
530 * d_ino consistent with st_ino.
531 * With xino=on, we do the same effort but we warn if we failed.
532 */
dcb399de 533static inline bool ovl_xino_warn(struct ovl_fs *ofs)
926e94d7 534{
dcb399de 535 return ofs->config.xino == OVL_XINO_ON;
926e94d7
AG
536}
537
af5f2396
AG
538/*
539 * To avoid regressions in existing setups with overlay lower offline changes,
540 * we allow lower changes only if none of the new features are used.
541 */
542static inline bool ovl_allow_offline_changes(struct ovl_fs *ofs)
543{
544 return (!ofs->config.index && !ofs->config.metacopy &&
545 !ovl_redirect_dir(ofs) && !ovl_xino_warn(ofs));
546}
547
0f831ec8 548/* All layers on same fs? */
dcb399de 549static inline bool ovl_same_fs(struct ovl_fs *ofs)
e487d889 550{
dcb399de 551 return ofs->xino_mode == 0;
0f831ec8 552}
e487d889 553
0f831ec8 554/* All overlay inodes have same st_dev? */
dcb399de 555static inline bool ovl_same_dev(struct ovl_fs *ofs)
0f831ec8 556{
dcb399de 557 return ofs->xino_mode >= 0;
0f831ec8
AG
558}
559
dcb399de 560static inline unsigned int ovl_xino_bits(struct ovl_fs *ofs)
0f831ec8 561{
dcb399de 562 return ovl_same_dev(ofs) ? ofs->xino_mode : 0;
e487d889
AG
563}
564
531d3040
AG
565static inline void ovl_inode_lock(struct inode *inode)
566{
567 mutex_lock(&OVL_I(inode)->lock);
568}
569
570static inline int ovl_inode_lock_interruptible(struct inode *inode)
1e92e307
AG
571{
572 return mutex_lock_interruptible(&OVL_I(inode)->lock);
573}
574
575static inline void ovl_inode_unlock(struct inode *inode)
576{
577 mutex_unlock(&OVL_I(inode)->lock);
578}
579
e9be9d5e 580
bbb1e54d 581/* namei.c */
cbe7fba8
AG
582int ovl_check_fb_len(struct ovl_fb *fb, int fb_len);
583
584static inline int ovl_check_fh_len(struct ovl_fh *fh, int fh_len)
585{
522f6e6c
AG
586 if (fh_len < sizeof(struct ovl_fh))
587 return -EINVAL;
588
cbe7fba8
AG
589 return ovl_check_fb_len(&fh->fb, fh_len - OVL_FH_WIRE_OFFSET);
590}
591
1cdb0cb6
PT
592struct dentry *ovl_decode_real_fh(struct ovl_fs *ofs, struct ovl_fh *fh,
593 struct vfsmount *mnt, bool connected);
8a22efa1 594int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
f941866f 595 struct dentry *upperdentry, struct ovl_path **stackp);
610afc0b 596int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry,
43d193f8 597 enum ovl_xattr ox, struct dentry *real, bool is_upper,
610afc0b 598 bool set);
8ea28765
AG
599struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index,
600 bool connected);
1eff1a1d 601int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index);
1cdb0cb6
PT
602int ovl_get_index_name(struct ovl_fs *ofs, struct dentry *origin,
603 struct qstr *name);
91ffe7be 604struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh);
06170154
AG
605struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
606 struct dentry *origin, bool verify);
bbb1e54d 607int ovl_path_next(int idx, struct dentry *dentry, struct path *path);
42dd69ae 608int ovl_maybe_lookup_lowerdata(struct dentry *dentry);
1eff1a1d
AG
609struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
610 unsigned int flags);
bbb1e54d 611bool ovl_lower_positive(struct dentry *dentry);
e9be9d5e 612
610afc0b 613static inline int ovl_verify_origin(struct ovl_fs *ofs, struct dentry *upper,
05122443
AG
614 struct dentry *origin, bool set)
615{
610afc0b
MS
616 return ovl_verify_set_fh(ofs, upper, OVL_XATTR_ORIGIN, origin,
617 false, set);
05122443
AG
618}
619
610afc0b
MS
620static inline int ovl_verify_upper(struct ovl_fs *ofs, struct dentry *index,
621 struct dentry *upper, bool set)
ad1d615c 622{
610afc0b 623 return ovl_verify_set_fh(ofs, index, OVL_XATTR_UPPER, upper, true, set);
ad1d615c
AG
624}
625
e9be9d5e
MS
626/* readdir.c */
627extern const struct file_operations ovl_dir_operations;
61536bed 628struct file *ovl_dir_real_file(const struct file *file, bool want_upper);
e9be9d5e 629int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list);
576bb263
CB
630void ovl_cleanup_whiteouts(struct ovl_fs *ofs, struct dentry *upper,
631 struct list_head *list);
e9be9d5e 632void ovl_cache_free(struct list_head *list);
4edb83bb 633void ovl_dir_cache_free(struct inode *inode);
2d343087 634int ovl_check_d_type_supported(const struct path *realpath);
576bb263
CB
635int ovl_workdir_cleanup(struct ovl_fs *ofs, struct inode *dir,
636 struct vfsmount *mnt, struct dentry *dentry, int level);
1eff1a1d 637int ovl_indexdir_cleanup(struct ovl_fs *ofs);
e9be9d5e 638
65cd913e
AG
639/*
640 * Can we iterate real dir directly?
641 *
642 * Non-merge dir may contain whiteouts from a time it was a merge upper, before
643 * lower dir was removed under it and possibly before it was rotated from upper
644 * to lower layer.
645 */
1fa9c5c5 646static inline bool ovl_dir_is_real(struct inode *dir)
65cd913e 647{
1fa9c5c5 648 return !ovl_test_flag(OVL_WHITEOUTS, dir);
65cd913e
AG
649}
650
e9be9d5e 651/* inode.c */
5f8415d6
AG
652int ovl_set_nlink_upper(struct dentry *dentry);
653int ovl_set_nlink_lower(struct dentry *dentry);
610afc0b 654unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry,
caf70cb2
AG
655 struct dentry *upperdentry,
656 unsigned int fallback);
c1632a0f 657int ovl_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
549c7297 658 struct iattr *attr);
b74d24f7 659int ovl_getattr(struct mnt_idmap *idmap, const struct path *path,
549c7297 660 struct kstat *stat, u32 request_mask, unsigned int flags);
4609e1f1 661int ovl_permission(struct mnt_idmap *idmap, struct inode *inode,
549c7297 662 int mask);
1d88f183
MS
663int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
664 const void *value, size_t size, int flags);
665int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
0eb45fc3 666 void *value, size_t size);
e9be9d5e 667ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
ded53656
YX
668
669#ifdef CONFIG_FS_POSIX_ACL
77435322 670struct posix_acl *do_ovl_get_acl(struct mnt_idmap *idmap,
6c0a8bfb
CB
671 struct inode *inode, int type,
672 bool rcu, bool noperm);
673static inline struct posix_acl *ovl_get_inode_acl(struct inode *inode, int type,
674 bool rcu)
675{
77435322 676 return do_ovl_get_acl(&nop_mnt_idmap, inode, type, rcu, true);
6c0a8bfb 677}
77435322 678static inline struct posix_acl *ovl_get_acl(struct mnt_idmap *idmap,
6c0a8bfb
CB
679 struct dentry *dentry, int type)
680{
77435322 681 return do_ovl_get_acl(idmap, d_inode(dentry), type, false, false);
6c0a8bfb 682}
13e83a49 683int ovl_set_acl(struct mnt_idmap *idmap, struct dentry *dentry,
0e641857 684 struct posix_acl *acl, int type);
31acceb9
CB
685struct posix_acl *ovl_get_acl_path(const struct path *path,
686 const char *acl_name, bool noperm);
ded53656 687#else
6c0a8bfb
CB
688#define ovl_get_inode_acl NULL
689#define ovl_get_acl NULL
0e641857 690#define ovl_set_acl NULL
31acceb9
CB
691static inline struct posix_acl *ovl_get_acl_path(const struct path *path,
692 const char *acl_name,
693 bool noperm)
694{
695 return NULL;
696}
ded53656
YX
697#endif
698
95582b00 699int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags);
610afc0b 700bool ovl_is_private_xattr(struct super_block *sb, const char *name);
e9be9d5e 701
ac6a52eb 702struct ovl_inode_params {
01b39dcc 703 struct inode *newinode;
ac6a52eb 704 struct dentry *upperdentry;
0af950f5 705 struct ovl_entry *oe;
74c6e384 706 bool index;
9cec54c8 707 char *redirect;
2b21da92 708 char *lowerdata_redirect;
ac6a52eb 709};
62c832ed
AG
710void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip,
711 unsigned long ino, int fsid);
ca4c8a3a 712struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev);
4b91c30a
AG
713struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
714 bool is_upper);
146d62e5
AG
715bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir);
716struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir);
ac6a52eb
VG
717struct inode *ovl_get_inode(struct super_block *sb,
718 struct ovl_inode_params *oip);
2878dffc 719void ovl_copyattr(struct inode *to);
e9be9d5e 720
72db8211
AG
721/* vfs inode flags copied from real to ovl inode */
722#define OVL_COPY_I_FLAGS_MASK (S_SYNC | S_NOATIME | S_APPEND | S_IMMUTABLE)
096a218a
AG
723/* vfs inode flags read from overlay.protattr xattr to ovl inode */
724#define OVL_PROT_I_FLAGS_MASK (S_APPEND | S_IMMUTABLE)
72db8211
AG
725
726/*
727 * fileattr flags copied from lower to upper inode on copy up.
096a218a
AG
728 * We cannot copy up immutable/append-only flags, because that would prevent
729 * linking temp inode to upper dir, so we store them in xattr instead.
72db8211
AG
730 */
731#define OVL_COPY_FS_FLAGS_MASK (FS_SYNC_FL | FS_NOATIME_FL)
732#define OVL_COPY_FSX_FLAGS_MASK (FS_XFLAG_SYNC | FS_XFLAG_NOATIME)
096a218a
AG
733#define OVL_PROT_FS_FLAGS_MASK (FS_APPEND_FL | FS_IMMUTABLE_FL)
734#define OVL_PROT_FSX_FLAGS_MASK (FS_XFLAG_APPEND | FS_XFLAG_IMMUTABLE)
735
736void ovl_check_protattr(struct inode *inode, struct dentry *upper);
737int ovl_set_protattr(struct inode *inode, struct dentry *upper,
738 struct fileattr *fa);
72db8211 739
4f357295
MS
740static inline void ovl_copyflags(struct inode *from, struct inode *to)
741{
72db8211 742 unsigned int mask = OVL_COPY_I_FLAGS_MASK;
4f357295
MS
743
744 inode_set_flags(to, from->i_flags & mask, mask);
745}
746
e9be9d5e
MS
747/* dir.c */
748extern const struct inode_operations ovl_dir_inode_operations;
c21c839b 749int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct inode *dir,
e7dd0e71 750 struct dentry *dentry);
471ec5dc 751struct ovl_cattr {
32a3d848
AV
752 dev_t rdev;
753 umode_t mode;
754 const char *link;
471ec5dc 755 struct dentry *hardlink;
32a3d848 756};
471ec5dc
AG
757
758#define OVL_CATTR(m) (&(struct ovl_cattr) { .mode = (m) })
759
576bb263
CB
760int ovl_mkdir_real(struct ovl_fs *ofs, struct inode *dir,
761 struct dentry **newdentry, umode_t mode);
762struct dentry *ovl_create_real(struct ovl_fs *ofs,
763 struct inode *dir, struct dentry *newdentry,
764 struct ovl_cattr *attr);
765int ovl_cleanup(struct ovl_fs *ofs, struct inode *dir, struct dentry *dentry);
766struct dentry *ovl_lookup_temp(struct ovl_fs *ofs, struct dentry *workdir);
767struct dentry *ovl_create_temp(struct ovl_fs *ofs, struct dentry *workdir,
95a1c815 768 struct ovl_cattr *attr);
e9be9d5e 769
d1d04ef8
MS
770/* file.c */
771extern const struct file_operations ovl_file_operations;
2406a307
JX
772int __init ovl_aio_request_cache_init(void);
773void ovl_aio_request_cache_destroy(void);
2d343087
AV
774int ovl_real_fileattr_get(const struct path *realpath, struct fileattr *fa);
775int ovl_real_fileattr_set(const struct path *realpath, struct fileattr *fa);
66dbfabf 776int ovl_fileattr_get(struct dentry *dentry, struct fileattr *fa);
8782a9ae 777int ovl_fileattr_set(struct mnt_idmap *idmap,
66dbfabf 778 struct dentry *dentry, struct fileattr *fa);
d1d04ef8 779
e9be9d5e
MS
780/* copy_up.c */
781int ovl_copy_up(struct dentry *dentry);
d1e6f6a9 782int ovl_copy_up_with_data(struct dentry *dentry);
3428030d 783int ovl_maybe_copy_up(struct dentry *dentry, int flags);
2d343087 784int ovl_copy_xattr(struct super_block *sb, const struct path *path, struct dentry *new);
5272eaf3 785int ovl_set_attr(struct ovl_fs *ofs, struct dentry *upper, struct kstat *stat);
1cdb0cb6
PT
786struct ovl_fh *ovl_encode_real_fh(struct ovl_fs *ofs, struct dentry *real,
787 bool is_upper);
a0c236b1
AG
788int ovl_set_origin(struct ovl_fs *ofs, struct dentry *lower,
789 struct dentry *upper);
8ed5eec9
AG
790
791/* export.c */
792extern const struct export_operations ovl_export_operations;