GNU Linux-libre 5.15.137-gnu
[releases.git] / fs / overlayfs / ovl_entry.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  *
4  * Copyright (C) 2011 Novell Inc.
5  * Copyright (C) 2016 Red Hat, Inc.
6  */
7
8 struct ovl_config {
9         char *lowerdir;
10         char *upperdir;
11         char *workdir;
12         bool default_permissions;
13         bool redirect_dir;
14         bool redirect_follow;
15         const char *redirect_mode;
16         bool index;
17         bool uuid;
18         bool nfs_export;
19         int xino;
20         bool metacopy;
21         bool userxattr;
22         bool ovl_volatile;
23 };
24
25 struct ovl_sb {
26         struct super_block *sb;
27         dev_t pseudo_dev;
28         /* Unusable (conflicting) uuid */
29         bool bad_uuid;
30         /* Used as a lower layer (but maybe also as upper) */
31         bool is_lower;
32 };
33
34 struct ovl_layer {
35         /* ovl_free_fs() relies on @mnt being the first member! */
36         struct vfsmount *mnt;
37         /* Trap in ovl inode cache */
38         struct inode *trap;
39         struct ovl_sb *fs;
40         /* Index of this layer in fs root (upper idx == 0) */
41         int idx;
42         /* One fsid per unique underlying sb (upper fsid == 0) */
43         int fsid;
44 };
45
46 /*
47  * ovl_free_fs() relies on @mnt being the first member when unmounting
48  * the private mounts created for each layer. Let's check both the
49  * offset and type.
50  */
51 static_assert(offsetof(struct ovl_layer, mnt) == 0);
52 static_assert(__same_type(typeof_member(struct ovl_layer, mnt), struct vfsmount *));
53
54 struct ovl_path {
55         const struct ovl_layer *layer;
56         struct dentry *dentry;
57 };
58
59 /* private information held for overlayfs's superblock */
60 struct ovl_fs {
61         unsigned int numlayer;
62         /* Number of unique fs among layers including upper fs */
63         unsigned int numfs;
64         const struct ovl_layer *layers;
65         struct ovl_sb *fs;
66         /* workbasedir is the path at workdir= mount option */
67         struct dentry *workbasedir;
68         /* workdir is the 'work' directory under workbasedir */
69         struct dentry *workdir;
70         /* index directory listing overlay inodes by origin file handle */
71         struct dentry *indexdir;
72         long namelen;
73         /* pathnames of lower and upper dirs, for show_options */
74         struct ovl_config config;
75         /* creds of process who forced instantiation of super block */
76         const struct cred *creator_cred;
77         bool tmpfile;
78         bool noxattr;
79         /* Did we take the inuse lock? */
80         bool upperdir_locked;
81         bool workdir_locked;
82         bool share_whiteout;
83         /* Traps in ovl inode cache */
84         struct inode *workbasedir_trap;
85         struct inode *workdir_trap;
86         struct inode *indexdir_trap;
87         /* -1: disabled, 0: same fs, 1..32: number of unused ino bits */
88         int xino_mode;
89         /* For allocation of non-persistent inode numbers */
90         atomic_long_t last_ino;
91         /* Whiteout dentry cache */
92         struct dentry *whiteout;
93         /* r/o snapshot of upperdir sb's only taken on volatile mounts */
94         errseq_t errseq;
95 };
96
97 static inline struct vfsmount *ovl_upper_mnt(struct ovl_fs *ofs)
98 {
99         return ofs->layers[0].mnt;
100 }
101
102 static inline struct ovl_fs *OVL_FS(struct super_block *sb)
103 {
104         return (struct ovl_fs *)sb->s_fs_info;
105 }
106
107 static inline bool ovl_should_sync(struct ovl_fs *ofs)
108 {
109         return !ofs->config.ovl_volatile;
110 }
111
112 /* private information held for every overlayfs dentry */
113 struct ovl_entry {
114         union {
115                 struct {
116                         unsigned long flags;
117                 };
118                 struct rcu_head rcu;
119         };
120         unsigned numlower;
121         struct ovl_path lowerstack[];
122 };
123
124 struct ovl_entry *ovl_alloc_entry(unsigned int numlower);
125
126 static inline struct ovl_entry *OVL_E(struct dentry *dentry)
127 {
128         return (struct ovl_entry *) dentry->d_fsdata;
129 }
130
131 struct ovl_inode {
132         union {
133                 struct ovl_dir_cache *cache;    /* directory */
134                 struct inode *lowerdata;        /* regular file */
135         };
136         const char *redirect;
137         u64 version;
138         unsigned long flags;
139         struct inode vfs_inode;
140         struct dentry *__upperdentry;
141         struct ovl_path lowerpath;
142
143         /* synchronize copy up and more */
144         struct mutex lock;
145 };
146
147 static inline struct ovl_inode *OVL_I(struct inode *inode)
148 {
149         return container_of(inode, struct ovl_inode, vfs_inode);
150 }
151
152 static inline struct dentry *ovl_upperdentry_dereference(struct ovl_inode *oi)
153 {
154         return READ_ONCE(oi->__upperdentry);
155 }