blob: 825377de085915d2904055d2865a860235fec1cb [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001/* SPDX-License-Identifier: GPL-2.0-only */
Miklos Szeredie9be9d52014-10-24 00:14:38 +02002/*
3 *
4 * Copyright (C) 2011 Novell Inc.
Miklos Szeredie9be9d52014-10-24 00:14:38 +02005 */
6
7#include <linux/kernel.h>
Amir Goldstein3a1e8192017-03-30 15:22:16 +03008#include <linux/uuid.h>
Miklos Szeredi46e5d0a2018-07-18 15:44:41 +02009#include <linux/fs.h>
Amir Goldsteinee023c32017-10-30 13:33:11 +020010#include "ovl_entry.h"
Miklos Szeredie9be9d52014-10-24 00:14:38 +020011
lijiazi1bd0a3a2019-12-16 19:12:32 +080012#undef pr_fmt
13#define pr_fmt(fmt) "overlayfs: " fmt
14
Miklos Szeredie9be9d52014-10-24 00:14:38 +020015enum ovl_path_type {
Miklos Szeredi38e813d2016-12-16 11:02:55 +010016 __OVL_PATH_UPPER = (1 << 0),
17 __OVL_PATH_MERGE = (1 << 1),
Amir Goldstein59548502017-04-23 23:12:34 +030018 __OVL_PATH_ORIGIN = (1 << 2),
Miklos Szeredie9be9d52014-10-24 00:14:38 +020019};
20
Miklos Szeredi1afaba12014-12-13 00:59:42 +010021#define OVL_TYPE_UPPER(type) ((type) & __OVL_PATH_UPPER)
22#define OVL_TYPE_MERGE(type) ((type) & __OVL_PATH_MERGE)
Amir Goldstein59548502017-04-23 23:12:34 +030023#define OVL_TYPE_ORIGIN(type) ((type) & __OVL_PATH_ORIGIN)
Miklos Szeredid837a492016-07-29 12:05:24 +020024
Andreas Gruenbacherfe2b7592016-08-22 17:59:22 +020025#define OVL_XATTR_PREFIX XATTR_TRUSTED_PREFIX "overlay."
Miklos Szeredi43d193f2020-09-02 10:58:49 +020026
27enum ovl_xattr {
28 OVL_XATTR_OPAQUE,
29 OVL_XATTR_REDIRECT,
30 OVL_XATTR_ORIGIN,
31 OVL_XATTR_IMPURE,
32 OVL_XATTR_NLINK,
33 OVL_XATTR_UPPER,
34 OVL_XATTR_METACOPY,
35};
Amir Goldstein3a1e8192017-03-30 15:22:16 +030036
Amir Goldsteinc62520a2018-01-14 19:25:31 +020037enum ovl_inode_flag {
Amir Goldsteinb79e05a2017-06-25 16:37:17 +030038 /* Pure upper dir that may contain non pure upper entries */
Miklos Szeredi13c72072017-07-04 22:03:16 +020039 OVL_IMPURE,
Amir Goldsteinb79e05a2017-06-25 16:37:17 +030040 /* Non-merge dir that may contain whiteout entries */
41 OVL_WHITEOUTS,
Amir Goldstein359f3922017-06-21 15:28:41 +030042 OVL_INDEX,
Vivek Goyal0c288872018-05-11 11:49:28 -040043 OVL_UPPERDATA,
Vivek Goyala00c2d52018-05-11 11:49:32 -040044 /* Inode number will remain constant over copy up. */
45 OVL_CONST_INO,
Miklos Szeredi13c72072017-07-04 22:03:16 +020046};
47
Amir Goldsteinc62520a2018-01-14 19:25:31 +020048enum ovl_entry_flag {
49 OVL_E_UPPER_ALIAS,
50 OVL_E_OPAQUE,
Amir Goldstein2ca3c142018-01-30 13:31:09 +020051 OVL_E_CONNECTED,
Amir Goldsteinc62520a2018-01-14 19:25:31 +020052};
53
Amir Goldstein926e94d2020-02-21 16:34:45 +020054enum {
55 OVL_XINO_OFF,
56 OVL_XINO_AUTO,
57 OVL_XINO_ON,
58};
59
Amir Goldstein3a1e8192017-03-30 15:22:16 +030060/*
61 * The tuple (fh,uuid) is a universal unique identifier for a copy up origin,
62 * where:
63 * origin.fh - exported file handle of the lower file
64 * origin.uuid - uuid of the lower filesystem
65 */
66#define OVL_FH_VERSION 0
67#define OVL_FH_MAGIC 0xfb
68
69/* CPU byte order required for fid decoding: */
70#define OVL_FH_FLAG_BIG_ENDIAN (1 << 0)
71#define OVL_FH_FLAG_ANY_ENDIAN (1 << 1)
Amir Goldstein54fb3472017-06-21 15:28:38 +030072/* Is the real inode encoded in fid an upper inode? */
73#define OVL_FH_FLAG_PATH_UPPER (1 << 2)
Amir Goldstein3a1e8192017-03-30 15:22:16 +030074
Amir Goldstein961af642017-07-11 15:58:36 +030075#define OVL_FH_FLAG_ALL (OVL_FH_FLAG_BIG_ENDIAN | OVL_FH_FLAG_ANY_ENDIAN | \
76 OVL_FH_FLAG_PATH_UPPER)
Amir Goldstein3a1e8192017-03-30 15:22:16 +030077
78#if defined(__LITTLE_ENDIAN)
79#define OVL_FH_FLAG_CPU_ENDIAN 0
80#elif defined(__BIG_ENDIAN)
81#define OVL_FH_FLAG_CPU_ENDIAN OVL_FH_FLAG_BIG_ENDIAN
82#else
83#error Endianness not defined
84#endif
85
Amir Goldsteincbe7fba2019-11-15 13:33:03 +020086/* The type used to be returned by overlay exportfs for misaligned fid */
87#define OVL_FILEID_V0 0xfb
88/* The type returned by overlay exportfs for 32bit aligned fid */
89#define OVL_FILEID_V1 0xf8
Amir Goldstein8ed5eec2017-07-12 14:17:16 +030090
Amir Goldsteincbe7fba2019-11-15 13:33:03 +020091/* On-disk format for "origin" file handle */
92struct ovl_fb {
Amir Goldstein3a1e8192017-03-30 15:22:16 +030093 u8 version; /* 0 */
94 u8 magic; /* 0xfb */
95 u8 len; /* size of this header + size of fid */
96 u8 flags; /* OVL_FH_FLAG_* */
97 u8 type; /* fid_type of fid */
Christoph Hellwig01633fd2017-05-17 09:32:50 +020098 uuid_t uuid; /* uuid of filesystem */
Gustavo A. R. Silva0efbe7c2020-03-09 15:22:33 -050099 u32 fid[]; /* file identifier should be 32bit aligned in-memory */
Amir Goldstein3a1e8192017-03-30 15:22:16 +0300100} __packed;
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200101
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200102/* In-memory and on-wire format for overlay file handle */
103struct ovl_fh {
104 u8 padding[3]; /* make sure fb.fid is 32bit aligned */
105 union {
106 struct ovl_fb fb;
107 u8 buf[0];
108 };
109} __packed;
110
111#define OVL_FH_WIRE_OFFSET offsetof(struct ovl_fh, fb)
112#define OVL_FH_LEN(fh) (OVL_FH_WIRE_OFFSET + (fh)->fb.len)
113#define OVL_FH_FID_OFFSET (OVL_FH_WIRE_OFFSET + \
114 offsetof(struct ovl_fb, fid))
115
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200116extern const char *ovl_xattr_table[];
117static inline const char *ovl_xattr(struct ovl_fs *ofs, enum ovl_xattr ox)
118{
119 return ovl_xattr_table[ox];
120}
121
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200122static inline int ovl_do_rmdir(struct inode *dir, struct dentry *dentry)
123{
124 int err = vfs_rmdir(dir, dentry);
Amir Goldstein6cf00762018-05-16 17:04:00 +0300125
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200126 pr_debug("rmdir(%pd2) = %i\n", dentry, err);
127 return err;
128}
129
130static inline int ovl_do_unlink(struct inode *dir, struct dentry *dentry)
131{
132 int err = vfs_unlink(dir, dentry, NULL);
Amir Goldstein6cf00762018-05-16 17:04:00 +0300133
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200134 pr_debug("unlink(%pd2) = %i\n", dentry, err);
135 return err;
136}
137
138static inline int ovl_do_link(struct dentry *old_dentry, struct inode *dir,
Amir Goldstein6cf00762018-05-16 17:04:00 +0300139 struct dentry *new_dentry)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200140{
141 int err = vfs_link(old_dentry, dir, new_dentry, NULL);
Amir Goldstein6cf00762018-05-16 17:04:00 +0300142
143 pr_debug("link(%pd2, %pd2) = %i\n", old_dentry, new_dentry, err);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200144 return err;
145}
146
147static inline int ovl_do_create(struct inode *dir, struct dentry *dentry,
Amir Goldstein6cf00762018-05-16 17:04:00 +0300148 umode_t mode)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200149{
150 int err = vfs_create(dir, dentry, mode, true);
Amir Goldstein6cf00762018-05-16 17:04:00 +0300151
152 pr_debug("create(%pd2, 0%o) = %i\n", dentry, mode, err);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200153 return err;
154}
155
156static inline int ovl_do_mkdir(struct inode *dir, struct dentry *dentry,
Amir Goldstein6cf00762018-05-16 17:04:00 +0300157 umode_t mode)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200158{
159 int err = vfs_mkdir(dir, dentry, mode);
Amir Goldstein6cf00762018-05-16 17:04:00 +0300160 pr_debug("mkdir(%pd2, 0%o) = %i\n", dentry, mode, err);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200161 return err;
162}
163
164static inline int ovl_do_mknod(struct inode *dir, struct dentry *dentry,
Amir Goldstein6cf00762018-05-16 17:04:00 +0300165 umode_t mode, dev_t dev)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200166{
167 int err = vfs_mknod(dir, dentry, mode, dev);
Amir Goldstein6cf00762018-05-16 17:04:00 +0300168
169 pr_debug("mknod(%pd2, 0%o, 0%o) = %i\n", dentry, mode, dev, err);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200170 return err;
171}
172
173static inline int ovl_do_symlink(struct inode *dir, struct dentry *dentry,
Amir Goldstein6cf00762018-05-16 17:04:00 +0300174 const char *oldname)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200175{
176 int err = vfs_symlink(dir, dentry, oldname);
Amir Goldstein6cf00762018-05-16 17:04:00 +0300177
178 pr_debug("symlink(\"%s\", %pd2) = %i\n", oldname, dentry, err);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200179 return err;
180}
181
Miklos Szeredi610afc02020-09-02 10:58:49 +0200182static inline ssize_t ovl_do_getxattr(struct ovl_fs *ofs, struct dentry *dentry,
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200183 enum ovl_xattr ox, void *value,
Miklos Szeredi610afc02020-09-02 10:58:49 +0200184 size_t size)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200185{
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200186 const char *name = ovl_xattr(ofs, ox);
Mark Salyzyn8d5dc2c2020-10-28 09:26:51 -0700187 struct inode *ip = d_inode(dentry);
188
189 return __vfs_getxattr(dentry, ip, name, value, size, XATTR_NOSECURITY);
Miklos Szeredid5dc7482020-09-02 10:58:48 +0200190}
191
Miklos Szeredi610afc02020-09-02 10:58:49 +0200192static inline int ovl_do_setxattr(struct ovl_fs *ofs, struct dentry *dentry,
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200193 enum ovl_xattr ox, const void *value,
Miklos Szeredi610afc02020-09-02 10:58:49 +0200194 size_t size)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200195{
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200196 const char *name = ovl_xattr(ofs, ox);
Miklos Szeredi26150ab2020-09-02 10:58:48 +0200197 int err = vfs_setxattr(dentry, name, value, size, 0);
198 pr_debug("setxattr(%pd2, \"%s\", \"%*pE\", %zu, 0) = %i\n",
199 dentry, name, min((int)size, 48), value, size, err);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200200 return err;
201}
202
Miklos Szeredi610afc02020-09-02 10:58:49 +0200203static inline int ovl_do_removexattr(struct ovl_fs *ofs, struct dentry *dentry,
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200204 enum ovl_xattr ox)
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200205{
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200206 const char *name = ovl_xattr(ofs, ox);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200207 int err = vfs_removexattr(dentry, name);
208 pr_debug("removexattr(%pd2, \"%s\") = %i\n", dentry, name, err);
209 return err;
210}
211
212static inline int ovl_do_rename(struct inode *olddir, struct dentry *olddentry,
213 struct inode *newdir, struct dentry *newdentry,
214 unsigned int flags)
215{
216 int err;
217
Amir Goldstein6cf00762018-05-16 17:04:00 +0300218 pr_debug("rename(%pd2, %pd2, 0x%x)\n", olddentry, newdentry, flags);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200219 err = vfs_rename(olddir, olddentry, newdir, newdentry, NULL, flags);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200220 if (err) {
Miklos Szeredi2773bf02016-09-27 11:03:58 +0200221 pr_debug("...rename(%pd2, %pd2, ...) = %i\n",
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200222 olddentry, newdentry, err);
223 }
224 return err;
225}
226
227static inline int ovl_do_whiteout(struct inode *dir, struct dentry *dentry)
228{
229 int err = vfs_whiteout(dir, dentry);
230 pr_debug("whiteout(%pd2) = %i\n", dentry, err);
231 return err;
232}
233
Amir Goldsteine7f52422017-01-17 06:34:53 +0200234static inline struct dentry *ovl_do_tmpfile(struct dentry *dentry, umode_t mode)
235{
236 struct dentry *ret = vfs_tmpfile(dentry, mode, 0);
Vasyl Gomonovych7879cb42017-11-28 00:09:23 +0100237 int err = PTR_ERR_OR_ZERO(ret);
Amir Goldsteine7f52422017-01-17 06:34:53 +0200238
239 pr_debug("tmpfile(%pd2, 0%o) = %i\n", dentry, mode, err);
240 return ret;
241}
242
Vivek Goyal0c288872018-05-11 11:49:28 -0400243static inline bool ovl_open_flags_need_copy_up(int flags)
244{
245 if (!flags)
246 return false;
247
248 return ((OPEN_FMODE(flags) & FMODE_WRITE) || (flags & O_TRUNC));
249}
250
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100251/* util.c */
252int ovl_want_write(struct dentry *dentry);
253void ovl_drop_write(struct dentry *dentry);
254struct dentry *ovl_workdir(struct dentry *dentry);
255const struct cred *ovl_override_creds(struct super_block *sb);
Mark Salyzyn6120a4d2018-06-14 11:15:22 -0700256void ovl_revert_creds(struct super_block *sb, const struct cred *oldcred);
Amir Goldsteine487d882017-11-07 13:55:04 +0200257int ovl_can_decode_fh(struct super_block *sb);
Amir Goldstein02bcd152017-06-21 15:28:36 +0300258struct dentry *ovl_indexdir(struct super_block *sb);
Amir Goldsteinf168f102018-01-19 11:26:53 +0200259bool ovl_index_all(struct super_block *sb);
260bool ovl_verify_lower(struct super_block *sb);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100261struct ovl_entry *ovl_alloc_entry(unsigned int numlower);
262bool ovl_dentry_remote(struct dentry *dentry);
Miklos Szeredif4288842020-03-17 15:04:22 +0100263void ovl_dentry_update_reval(struct dentry *dentry, struct dentry *upperdentry,
264 unsigned int mask);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100265bool ovl_dentry_weird(struct dentry *dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200266enum ovl_path_type ovl_path_type(struct dentry *dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200267void ovl_path_upper(struct dentry *dentry, struct path *path);
268void ovl_path_lower(struct dentry *dentry, struct path *path);
Vivek Goyal4f93b422018-05-11 11:49:30 -0400269void ovl_path_lowerdata(struct dentry *dentry, struct path *path);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200270enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200271struct dentry *ovl_dentry_upper(struct dentry *dentry);
272struct dentry *ovl_dentry_lower(struct dentry *dentry);
Vivek Goyal647d2532018-05-11 11:49:30 -0400273struct dentry *ovl_dentry_lowerdata(struct dentry *dentry);
Miklos Szeredi13464162020-01-24 09:46:45 +0100274const struct ovl_layer *ovl_layer_lower(struct dentry *dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200275struct dentry *ovl_dentry_real(struct dentry *dentry);
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200276struct dentry *ovl_i_dentry_upper(struct inode *inode);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200277struct inode *ovl_inode_upper(struct inode *inode);
278struct inode *ovl_inode_lower(struct inode *inode);
Vivek Goyal2664bd02018-05-11 11:49:30 -0400279struct inode *ovl_inode_lowerdata(struct inode *inode);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200280struct inode *ovl_inode_real(struct inode *inode);
Vivek Goyal4823d492018-05-11 11:49:31 -0400281struct inode *ovl_inode_realdata(struct inode *inode);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200282struct ovl_dir_cache *ovl_dir_cache(struct inode *inode);
283void ovl_set_dir_cache(struct inode *inode, struct ovl_dir_cache *cache);
Amir Goldsteinc62520a2018-01-14 19:25:31 +0200284void ovl_dentry_set_flag(unsigned long flag, struct dentry *dentry);
285void ovl_dentry_clear_flag(unsigned long flag, struct dentry *dentry);
286bool ovl_dentry_test_flag(unsigned long flag, struct dentry *dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200287bool ovl_dentry_is_opaque(struct dentry *dentry);
Miklos Szeredic412ce42016-12-16 11:02:55 +0100288bool ovl_dentry_is_whiteout(struct dentry *dentry);
Miklos Szeredi5cf5b472016-12-16 11:02:57 +0100289void ovl_dentry_set_opaque(struct dentry *dentry);
Miklos Szeredi55acc662017-07-04 22:03:18 +0200290bool ovl_dentry_has_upper_alias(struct dentry *dentry);
291void ovl_dentry_set_upper_alias(struct dentry *dentry);
Vivek Goyal0c288872018-05-11 11:49:28 -0400292bool ovl_dentry_needs_data_copy_up(struct dentry *dentry, int flags);
293bool ovl_dentry_needs_data_copy_up_locked(struct dentry *dentry, int flags);
294bool ovl_has_upperdata(struct inode *inode);
295void ovl_set_upperdata(struct inode *inode);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100296bool ovl_redirect_dir(struct super_block *sb);
Miklos Szeredia6c60652016-12-16 11:02:56 +0100297const char *ovl_dentry_get_redirect(struct dentry *dentry);
298void ovl_dentry_set_redirect(struct dentry *dentry, const char *redirect);
Miklos Szeredi09d8b582017-07-04 22:03:16 +0200299void ovl_inode_update(struct inode *inode, struct dentry *upperdentry);
Miklos Szeredid9854c82018-07-18 15:44:40 +0200300void ovl_dir_modified(struct dentry *dentry, bool impurity);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100301u64 ovl_dentry_version_get(struct dentry *dentry);
302bool ovl_is_whiteout(struct dentry *dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200303struct file *ovl_path_open(struct path *path, int flags);
Vivek Goyal0c288872018-05-11 11:49:28 -0400304int ovl_copy_up_start(struct dentry *dentry, int flags);
Amir Goldstein39d3d602017-01-17 06:34:56 +0200305void ovl_copy_up_end(struct dentry *dentry);
Vivek Goyal0c288872018-05-11 11:49:28 -0400306bool ovl_already_copied_up(struct dentry *dentry, int flags);
Miklos Szeredi610afc02020-09-02 10:58:49 +0200307bool ovl_check_origin_xattr(struct ovl_fs *ofs, struct dentry *dentry);
308bool ovl_check_dir_xattr(struct super_block *sb, struct dentry *dentry,
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200309 enum ovl_xattr ox);
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300310int ovl_check_setxattr(struct dentry *dentry, struct dentry *upperdentry,
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200311 enum ovl_xattr ox, const void *value, size_t size,
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300312 int xerr);
313int ovl_set_impure(struct dentry *dentry, struct dentry *upperdentry);
Amir Goldsteinad0af712017-06-21 15:28:32 +0300314bool ovl_inuse_trylock(struct dentry *dentry);
315void ovl_inuse_unlock(struct dentry *dentry);
Amir Goldstein146d62e2019-04-18 17:42:08 +0300316bool ovl_is_inuse(struct dentry *dentry);
Amir Goldstein24b33ee2017-09-26 07:55:26 +0300317bool ovl_need_index(struct dentry *dentry);
Amir Goldstein0e329922018-10-18 18:37:13 +0300318int ovl_nlink_start(struct dentry *dentry);
319void ovl_nlink_end(struct dentry *dentry);
Amir Goldstein5820dc02017-09-25 16:39:55 +0300320int ovl_lock_rename_workdir(struct dentry *workdir, struct dentry *upperdir);
Miklos Szeredi610afc02020-09-02 10:58:49 +0200321int ovl_check_metacopy_xattr(struct ovl_fs *ofs, struct dentry *dentry);
Vivek Goyal67d756c22018-05-11 11:49:30 -0400322bool ovl_is_metacopy_dentry(struct dentry *dentry);
Miklos Szeredi610afc02020-09-02 10:58:49 +0200323char *ovl_get_redirect_xattr(struct ovl_fs *ofs, struct dentry *dentry,
324 int padding);
Sargun Dhillon8ccf963c2021-01-07 16:10:43 -0800325int ovl_sync_status(struct ovl_fs *ofs);
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300326
Amir Goldstein0f8528c72021-04-11 12:22:23 +0300327static inline void ovl_set_flag(unsigned long flag, struct inode *inode)
328{
329 set_bit(flag, &OVL_I(inode)->flags);
330}
331
332static inline void ovl_clear_flag(unsigned long flag, struct inode *inode)
333{
334 clear_bit(flag, &OVL_I(inode)->flags);
335}
336
337static inline bool ovl_test_flag(unsigned long flag, struct inode *inode)
338{
339 return test_bit(flag, &OVL_I(inode)->flags);
340}
341
Miklos Szeredi610afc02020-09-02 10:58:49 +0200342static inline bool ovl_is_impuredir(struct super_block *sb,
343 struct dentry *dentry)
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300344{
Miklos Szeredi610afc02020-09-02 10:58:49 +0200345 return ovl_check_dir_xattr(sb, dentry, OVL_XATTR_IMPURE);
Amir Goldsteinf3a15682017-05-24 15:29:33 +0300346}
347
Amir Goldstein926e94d2020-02-21 16:34:45 +0200348/*
349 * With xino=auto, we do best effort to keep all inodes on same st_dev and
350 * d_ino consistent with st_ino.
351 * With xino=on, we do the same effort but we warn if we failed.
352 */
353static inline bool ovl_xino_warn(struct super_block *sb)
354{
355 return OVL_FS(sb)->config.xino == OVL_XINO_ON;
356}
357
Amir Goldstein0f831ec2019-11-16 18:14:41 +0200358/* All layers on same fs? */
359static inline bool ovl_same_fs(struct super_block *sb)
360{
361 return OVL_FS(sb)->xino_mode == 0;
362}
363
364/* All overlay inodes have same st_dev? */
365static inline bool ovl_same_dev(struct super_block *sb)
366{
367 return OVL_FS(sb)->xino_mode >= 0;
368}
369
Amir Goldsteine487d882017-11-07 13:55:04 +0200370static inline unsigned int ovl_xino_bits(struct super_block *sb)
371{
Amir Goldstein0f831ec2019-11-16 18:14:41 +0200372 return ovl_same_dev(sb) ? OVL_FS(sb)->xino_mode : 0;
Amir Goldsteine487d882017-11-07 13:55:04 +0200373}
374
Amir Goldstein531d3042020-03-02 15:03:35 +0200375static inline void ovl_inode_lock(struct inode *inode)
376{
377 mutex_lock(&OVL_I(inode)->lock);
378}
379
380static inline int ovl_inode_lock_interruptible(struct inode *inode)
Amir Goldstein1e92e302018-10-18 18:37:14 +0300381{
382 return mutex_lock_interruptible(&OVL_I(inode)->lock);
383}
384
385static inline void ovl_inode_unlock(struct inode *inode)
386{
387 mutex_unlock(&OVL_I(inode)->lock);
388}
389
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200390
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100391/* namei.c */
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200392int ovl_check_fb_len(struct ovl_fb *fb, int fb_len);
393
394static inline int ovl_check_fh_len(struct ovl_fh *fh, int fh_len)
395{
Amir Goldstein522f6e62020-05-23 16:21:55 +0300396 if (fh_len < sizeof(struct ovl_fh))
397 return -EINVAL;
398
Amir Goldsteincbe7fba2019-11-15 13:33:03 +0200399 return ovl_check_fb_len(&fh->fb, fh_len - OVL_FH_WIRE_OFFSET);
400}
401
Amir Goldstein8a22efa2018-03-09 15:51:02 +0200402struct dentry *ovl_decode_real_fh(struct ovl_fh *fh, struct vfsmount *mnt,
403 bool connected);
404int ovl_check_origin_fh(struct ovl_fs *ofs, struct ovl_fh *fh, bool connected,
Amir Goldsteinf9418662018-01-19 21:33:44 +0200405 struct dentry *upperdentry, struct ovl_path **stackp);
Miklos Szeredi610afc02020-09-02 10:58:49 +0200406int ovl_verify_set_fh(struct ovl_fs *ofs, struct dentry *dentry,
Miklos Szeredi43d193f2020-09-02 10:58:49 +0200407 enum ovl_xattr ox, struct dentry *real, bool is_upper,
Miklos Szeredi610afc02020-09-02 10:58:49 +0200408 bool set);
Amir Goldstein3b0bfc62017-12-24 18:42:16 +0200409struct dentry *ovl_index_upper(struct ovl_fs *ofs, struct dentry *index);
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200410int ovl_verify_index(struct ovl_fs *ofs, struct dentry *index);
Amir Goldstein359f3922017-06-21 15:28:41 +0300411int ovl_get_index_name(struct dentry *origin, struct qstr *name);
Amir Goldstein91ffe7b2017-12-28 20:23:05 +0200412struct dentry *ovl_get_index_fh(struct ovl_fs *ofs, struct ovl_fh *fh);
Amir Goldstein06170152018-01-17 14:40:27 +0200413struct dentry *ovl_lookup_index(struct ovl_fs *ofs, struct dentry *upper,
414 struct dentry *origin, bool verify);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100415int ovl_path_next(int idx, struct dentry *dentry, struct path *path);
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200416struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
417 unsigned int flags);
Miklos Szeredibbb1e542016-12-16 11:02:56 +0100418bool ovl_lower_positive(struct dentry *dentry);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200419
Miklos Szeredi610afc02020-09-02 10:58:49 +0200420static inline int ovl_verify_origin(struct ovl_fs *ofs, struct dentry *upper,
Amir Goldstein05122442018-01-11 08:25:32 +0200421 struct dentry *origin, bool set)
422{
Miklos Szeredi610afc02020-09-02 10:58:49 +0200423 return ovl_verify_set_fh(ofs, upper, OVL_XATTR_ORIGIN, origin,
424 false, set);
Amir Goldstein05122442018-01-11 08:25:32 +0200425}
426
Miklos Szeredi610afc02020-09-02 10:58:49 +0200427static inline int ovl_verify_upper(struct ovl_fs *ofs, struct dentry *index,
428 struct dentry *upper, bool set)
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200429{
Miklos Szeredi610afc02020-09-02 10:58:49 +0200430 return ovl_verify_set_fh(ofs, index, OVL_XATTR_UPPER, upper, true, set);
Amir Goldsteinad1d6152018-01-11 10:47:03 +0200431}
432
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200433/* readdir.c */
434extern const struct file_operations ovl_dir_operations;
Amir Goldstein61536be2020-09-29 15:28:47 +0800435struct file *ovl_dir_real_file(const struct file *file, bool want_upper);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200436int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list);
437void ovl_cleanup_whiteouts(struct dentry *upper, struct list_head *list);
438void ovl_cache_free(struct list_head *list);
Miklos Szeredi4edb83b2017-07-27 21:54:06 +0200439void ovl_dir_cache_free(struct inode *inode);
Vivek Goyal45aebea2016-02-22 09:28:34 -0500440int ovl_check_d_type_supported(struct path *realpath);
Amir Goldstein30116452020-04-03 07:58:49 +0300441int ovl_workdir_cleanup(struct inode *dir, struct vfsmount *mnt,
442 struct dentry *dentry, int level);
Amir Goldstein1eff1a12017-12-12 22:40:46 +0200443int ovl_indexdir_cleanup(struct ovl_fs *ofs);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200444
Amir Goldstein0f8528c72021-04-11 12:22:23 +0300445/*
446 * Can we iterate real dir directly?
447 *
448 * Non-merge dir may contain whiteouts from a time it was a merge upper, before
449 * lower dir was removed under it and possibly before it was rotated from upper
450 * to lower layer.
451 */
452static inline bool ovl_dir_is_real(struct dentry *dir)
453{
454 return !ovl_test_flag(OVL_WHITEOUTS, d_inode(dir));
455}
456
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200457/* inode.c */
Amir Goldstein5f8415d2017-06-20 15:35:14 +0300458int ovl_set_nlink_upper(struct dentry *dentry);
459int ovl_set_nlink_lower(struct dentry *dentry);
Miklos Szeredi610afc02020-09-02 10:58:49 +0200460unsigned int ovl_get_nlink(struct ovl_fs *ofs, struct dentry *lowerdentry,
Amir Goldsteincaf70cb2017-06-21 13:46:12 +0300461 struct dentry *upperdentry,
462 unsigned int fallback);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200463int ovl_setattr(struct dentry *dentry, struct iattr *attr);
Miklos Szeredi5b712092017-05-05 11:38:58 +0200464int ovl_getattr(const struct path *path, struct kstat *stat,
465 u32 request_mask, unsigned int flags);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200466int ovl_permission(struct inode *inode, int mask);
Miklos Szeredi1d88f182017-07-20 11:08:21 +0200467int ovl_xattr_set(struct dentry *dentry, struct inode *inode, const char *name,
468 const void *value, size_t size, int flags);
469int ovl_xattr_get(struct dentry *dentry, struct inode *inode, const char *name,
Greg Kroah-Hartman9406c912020-02-08 14:18:12 +0100470 void *value, size_t size, int flags);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200471ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
Vivek Goyal39a25b22016-07-01 16:34:26 -0400472struct posix_acl *ovl_get_acl(struct inode *inode, int type);
Deepa Dinamani95582b02018-05-08 19:36:02 -0700473int ovl_update_time(struct inode *inode, struct timespec64 *ts, int flags);
Miklos Szeredi610afc02020-09-02 10:58:49 +0200474bool ovl_is_private_xattr(struct super_block *sb, const char *name);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200475
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400476struct ovl_inode_params {
Amir Goldstein01b39dc2018-05-11 11:15:15 +0300477 struct inode *newinode;
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400478 struct dentry *upperdentry;
479 struct ovl_path *lowerpath;
Miklos Szeredi74c6e382020-06-04 10:48:19 +0200480 bool index;
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400481 unsigned int numlower;
Vivek Goyal9cec54c2018-05-11 11:49:27 -0400482 char *redirect;
Vivek Goyal2664bd02018-05-11 11:49:30 -0400483 struct dentry *lowerdata;
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400484};
Amir Goldstein62c832e2019-11-19 15:31:46 +0200485void ovl_inode_init(struct inode *inode, struct ovl_inode_params *oip,
486 unsigned long ino, int fsid);
Miklos Szeredica4c8a32016-12-16 11:02:55 +0100487struct inode *ovl_new_inode(struct super_block *sb, umode_t mode, dev_t rdev);
Amir Goldstein4b91c302018-01-18 16:39:13 +0200488struct inode *ovl_lookup_inode(struct super_block *sb, struct dentry *real,
489 bool is_upper);
Amir Goldstein146d62e2019-04-18 17:42:08 +0300490bool ovl_lookup_trap_inode(struct super_block *sb, struct dentry *dir);
491struct inode *ovl_get_trap_inode(struct super_block *sb, struct dentry *dir);
Vivek Goyalac6a52e2018-05-08 09:27:21 -0400492struct inode *ovl_get_inode(struct super_block *sb,
493 struct ovl_inode_params *oip);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200494static inline void ovl_copyattr(struct inode *from, struct inode *to)
495{
496 to->i_uid = from->i_uid;
497 to->i_gid = from->i_gid;
Vivek Goyal07a2daa2016-07-01 16:34:25 -0400498 to->i_mode = from->i_mode;
Miklos Szeredid719e8f2016-07-29 12:05:23 +0200499 to->i_atime = from->i_atime;
500 to->i_mtime = from->i_mtime;
501 to->i_ctime = from->i_ctime;
Miklos Szeredi46e5d0a2018-07-18 15:44:41 +0200502 i_size_write(to, i_size_read(from));
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200503}
504
Miklos Szeredi4f357292018-07-18 15:44:41 +0200505static inline void ovl_copyflags(struct inode *from, struct inode *to)
506{
507 unsigned int mask = S_SYNC | S_IMMUTABLE | S_APPEND | S_NOATIME;
508
509 inode_set_flags(to, from->i_flags & mask, mask);
510}
511
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200512/* dir.c */
513extern const struct inode_operations ovl_dir_inode_operations;
Chengguang Xuc21c8392020-04-24 10:55:17 +0800514int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct inode *dir,
Amir Goldsteine7dd0e72017-10-24 17:38:33 +0300515 struct dentry *dentry);
Amir Goldstein471ec5d2018-05-16 17:35:02 +0300516struct ovl_cattr {
Al Viro32a3d842016-12-04 17:33:17 +0000517 dev_t rdev;
518 umode_t mode;
519 const char *link;
Amir Goldstein471ec5d2018-05-16 17:35:02 +0300520 struct dentry *hardlink;
Al Viro32a3d842016-12-04 17:33:17 +0000521};
Amir Goldstein471ec5d2018-05-16 17:35:02 +0300522
523#define OVL_CATTR(m) (&(struct ovl_cattr) { .mode = (m) })
524
Miklos Szeredi95a1c812018-05-16 17:51:25 +0300525struct dentry *ovl_create_real(struct inode *dir, struct dentry *newdentry,
526 struct ovl_cattr *attr);
Amir Goldstein415543d2017-06-21 15:28:42 +0300527int ovl_cleanup(struct inode *dir, struct dentry *dentry);
Amir Goldsteincad218a2020-02-20 09:00:19 +0200528struct dentry *ovl_lookup_temp(struct dentry *workdir);
Amir Goldstein137ec522018-05-16 17:51:25 +0300529struct dentry *ovl_create_temp(struct dentry *workdir, struct ovl_cattr *attr);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200530
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200531/* file.c */
532extern const struct file_operations ovl_file_operations;
Jiufei Xue2406a302019-11-20 17:45:26 +0800533int __init ovl_aio_request_cache_init(void);
534void ovl_aio_request_cache_destroy(void);
Amir Goldstein61536be2020-09-29 15:28:47 +0800535long ovl_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
536long ovl_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg);
Miklos Szeredid1d04ef2018-07-18 15:44:41 +0200537
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200538/* copy_up.c */
539int ovl_copy_up(struct dentry *dentry);
Vivek Goyald1e6f6a2018-05-11 11:49:33 -0400540int ovl_copy_up_with_data(struct dentry *dentry);
Amir Goldstein34280302019-01-22 07:01:39 +0200541int ovl_maybe_copy_up(struct dentry *dentry, int flags);
Miklos Szeredi610afc02020-09-02 10:58:49 +0200542int ovl_copy_xattr(struct super_block *sb, struct dentry *old,
543 struct dentry *new);
Miklos Szeredie9be9d52014-10-24 00:14:38 +0200544int ovl_set_attr(struct dentry *upper, struct kstat *stat);
Amir Goldstein5b2cccd2018-02-02 10:42:03 +0200545struct ovl_fh *ovl_encode_real_fh(struct dentry *real, bool is_upper);
Amir Goldstein9678e632018-01-03 19:34:45 +0200546int ovl_set_origin(struct dentry *dentry, struct dentry *lower,
547 struct dentry *upper);
Amir Goldstein8ed5eec2017-07-12 14:17:16 +0300548
549/* export.c */
550extern const struct export_operations ovl_export_operations;