]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - fs/aufs/branch.h
012459ad57f345b0383426228e8596a9611a7d31
[mirror_ubuntu-focal-kernel.git] / fs / aufs / branch.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Copyright (C) 2005-2019 Junjiro R. Okajima
4 *
5 * This program, aufs is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 /*
20 * branch filesystems and xino for them
21 */
22
23 #ifndef __AUFS_BRANCH_H__
24 #define __AUFS_BRANCH_H__
25
26 #ifdef __KERNEL__
27
28 #include <linux/mount.h>
29 #include "dirren.h"
30 #include "dynop.h"
31 #include "lcnt.h"
32 #include "rwsem.h"
33 #include "super.h"
34
35 /* ---------------------------------------------------------------------- */
36
37 /* a xino file */
38 struct au_xino {
39 struct file **xi_file;
40 unsigned int xi_nfile;
41
42 struct {
43 spinlock_t spin;
44 ino_t *array;
45 int total;
46 /* reserved for future use */
47 /* unsigned long *bitmap; */
48 wait_queue_head_t wqh;
49 } xi_nondir;
50
51 struct mutex xi_mtx; /* protects xi_file array */
52 struct hlist_bl_head xi_writing;
53
54 atomic_t xi_truncating;
55
56 struct kref xi_kref;
57 };
58
59 /* File-based Hierarchical Storage Management */
60 struct au_br_fhsm {
61 #ifdef CONFIG_AUFS_FHSM
62 struct mutex bf_lock;
63 unsigned long bf_jiffy;
64 struct aufs_stfs bf_stfs;
65 int bf_readable;
66 #endif
67 };
68
69 /* members for writable branch only */
70 enum {AuBrWh_BASE, AuBrWh_PLINK, AuBrWh_ORPH, AuBrWh_Last};
71 struct au_wbr {
72 struct au_rwsem wbr_wh_rwsem;
73 struct dentry *wbr_wh[AuBrWh_Last];
74 atomic_t wbr_wh_running;
75 #define wbr_whbase wbr_wh[AuBrWh_BASE] /* whiteout base */
76 #define wbr_plink wbr_wh[AuBrWh_PLINK] /* pseudo-link dir */
77 #define wbr_orph wbr_wh[AuBrWh_ORPH] /* dir for orphans */
78
79 /* mfs mode */
80 unsigned long long wbr_bytes;
81 };
82
83 /* ext2 has 3 types of operations at least, ext3 has 4 */
84 #define AuBrDynOp (AuDyLast * 4)
85
86 #ifdef CONFIG_AUFS_HFSNOTIFY
87 /* support for asynchronous destruction */
88 struct au_br_hfsnotify {
89 struct fsnotify_group *hfsn_group;
90 };
91 #endif
92
93 /* sysfs entries */
94 struct au_brsysfs {
95 char name[16];
96 struct attribute attr;
97 };
98
99 enum {
100 AuBrSysfs_BR,
101 AuBrSysfs_BRID,
102 AuBrSysfs_Last
103 };
104
105 /* protected by superblock rwsem */
106 struct au_branch {
107 struct au_xino *br_xino;
108
109 aufs_bindex_t br_id;
110
111 int br_perm;
112 struct path br_path;
113 spinlock_t br_dykey_lock;
114 struct au_dykey *br_dykey[AuBrDynOp];
115 au_lcnt_t br_nfiles; /* opened files */
116 au_lcnt_t br_count; /* in-use for other */
117
118 struct au_wbr *br_wbr;
119 struct au_br_fhsm *br_fhsm;
120
121 #ifdef CONFIG_AUFS_HFSNOTIFY
122 struct au_br_hfsnotify *br_hfsn;
123 #endif
124
125 #ifdef CONFIG_SYSFS
126 /* entries under sysfs per mount-point */
127 struct au_brsysfs br_sysfs[AuBrSysfs_Last];
128 #endif
129
130 #ifdef CONFIG_DEBUG_FS
131 struct dentry *br_dbgaufs; /* xino */
132 #endif
133
134 struct au_dr_br br_dirren;
135 };
136
137 /* ---------------------------------------------------------------------- */
138
139 static inline struct vfsmount *au_br_mnt(struct au_branch *br)
140 {
141 return br->br_path.mnt;
142 }
143
144 static inline struct dentry *au_br_dentry(struct au_branch *br)
145 {
146 return br->br_path.dentry;
147 }
148
149 static inline struct super_block *au_br_sb(struct au_branch *br)
150 {
151 return au_br_mnt(br)->mnt_sb;
152 }
153
154 static inline int au_br_rdonly(struct au_branch *br)
155 {
156 return (sb_rdonly(au_br_sb(br))
157 || !au_br_writable(br->br_perm))
158 ? -EROFS : 0;
159 }
160
161 static inline int au_br_hnotifyable(int brperm __maybe_unused)
162 {
163 #ifdef CONFIG_AUFS_HNOTIFY
164 return !(brperm & AuBrPerm_RR);
165 #else
166 return 0;
167 #endif
168 }
169
170 static inline int au_br_test_oflag(int oflag, struct au_branch *br)
171 {
172 int err, exec_flag;
173
174 err = 0;
175 exec_flag = oflag & __FMODE_EXEC;
176 if (unlikely(exec_flag && path_noexec(&br->br_path)))
177 err = -EACCES;
178
179 return err;
180 }
181
182 static inline void au_xino_get(struct au_branch *br)
183 {
184 struct au_xino *xi;
185
186 xi = br->br_xino;
187 if (xi)
188 kref_get(&xi->xi_kref);
189 }
190
191 static inline int au_xino_count(struct au_branch *br)
192 {
193 int v;
194 struct au_xino *xi;
195
196 v = 0;
197 xi = br->br_xino;
198 if (xi)
199 v = kref_read(&xi->xi_kref);
200
201 return v;
202 }
203
204 /* ---------------------------------------------------------------------- */
205
206 /* branch.c */
207 struct au_sbinfo;
208 void au_br_free(struct au_sbinfo *sinfo);
209 int au_br_index(struct super_block *sb, aufs_bindex_t br_id);
210 struct au_opt_add;
211 int au_br_add(struct super_block *sb, struct au_opt_add *add, int remount);
212 struct au_opt_del;
213 int au_br_del(struct super_block *sb, struct au_opt_del *del, int remount);
214 long au_ibusy_ioctl(struct file *file, unsigned long arg);
215 #ifdef CONFIG_COMPAT
216 long au_ibusy_compat_ioctl(struct file *file, unsigned long arg);
217 #endif
218 struct au_opt_mod;
219 int au_br_mod(struct super_block *sb, struct au_opt_mod *mod, int remount,
220 int *do_refresh);
221 struct aufs_stfs;
222 int au_br_stfs(struct au_branch *br, struct aufs_stfs *stfs);
223
224 /* xino.c */
225 static const loff_t au_loff_max = LLONG_MAX;
226
227 aufs_bindex_t au_xi_root(struct super_block *sb, struct dentry *dentry);
228 struct file *au_xino_create(struct super_block *sb, char *fpath, int silent,
229 int wbrtop);
230 struct file *au_xino_create2(struct super_block *sb, struct path *base,
231 struct file *copy_src);
232 struct au_xi_new {
233 struct au_xino *xi; /* switch between xino and xigen */
234 int idx;
235 struct path *base;
236 struct file *copy_src;
237 };
238 struct file *au_xi_new(struct super_block *sb, struct au_xi_new *xinew);
239
240 int au_xino_read(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
241 ino_t *ino);
242 int au_xino_write(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
243 ino_t ino);
244 ssize_t xino_fread(vfs_readf_t func, struct file *file, void *buf, size_t size,
245 loff_t *pos);
246 ssize_t xino_fwrite(vfs_writef_t func, struct file *file, void *buf,
247 size_t size, loff_t *pos);
248
249 int au_xib_trunc(struct super_block *sb);
250 int au_xino_trunc(struct super_block *sb, aufs_bindex_t bindex, int idx_begin);
251
252 struct au_xino *au_xino_alloc(unsigned int nfile);
253 int au_xino_put(struct au_branch *br);
254 struct file *au_xino_file1(struct au_xino *xi);
255
256 struct au_opt_xino;
257 void au_xino_clr(struct super_block *sb);
258 int au_xino_set(struct super_block *sb, struct au_opt_xino *xiopt, int remount);
259 struct file *au_xino_def(struct super_block *sb);
260 int au_xino_init_br(struct super_block *sb, struct au_branch *br, ino_t hino,
261 struct path *base);
262
263 ino_t au_xino_new_ino(struct super_block *sb);
264 void au_xino_delete_inode(struct inode *inode, const int unlinked);
265
266 void au_xinondir_leave(struct super_block *sb, aufs_bindex_t bindex,
267 ino_t h_ino, int idx);
268 int au_xinondir_enter(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
269 int *idx);
270
271 int au_xino_path(struct seq_file *seq, struct file *file);
272
273 /* ---------------------------------------------------------------------- */
274
275 /* @idx is signed to accept -1 meaning the first file */
276 static inline struct file *au_xino_file(struct au_xino *xi, int idx)
277 {
278 struct file *file;
279
280 file = NULL;
281 if (!xi)
282 goto out;
283
284 if (idx >= 0) {
285 if (idx < xi->xi_nfile)
286 file = xi->xi_file[idx];
287 } else
288 file = au_xino_file1(xi);
289
290 out:
291 return file;
292 }
293
294 /* ---------------------------------------------------------------------- */
295
296 /* Superblock to branch */
297 static inline
298 aufs_bindex_t au_sbr_id(struct super_block *sb, aufs_bindex_t bindex)
299 {
300 return au_sbr(sb, bindex)->br_id;
301 }
302
303 static inline
304 struct vfsmount *au_sbr_mnt(struct super_block *sb, aufs_bindex_t bindex)
305 {
306 return au_br_mnt(au_sbr(sb, bindex));
307 }
308
309 static inline
310 struct super_block *au_sbr_sb(struct super_block *sb, aufs_bindex_t bindex)
311 {
312 return au_br_sb(au_sbr(sb, bindex));
313 }
314
315 static inline int au_sbr_perm(struct super_block *sb, aufs_bindex_t bindex)
316 {
317 return au_sbr(sb, bindex)->br_perm;
318 }
319
320 static inline int au_sbr_whable(struct super_block *sb, aufs_bindex_t bindex)
321 {
322 return au_br_whable(au_sbr_perm(sb, bindex));
323 }
324
325 /* ---------------------------------------------------------------------- */
326
327 #define wbr_wh_read_lock(wbr) au_rw_read_lock(&(wbr)->wbr_wh_rwsem)
328 #define wbr_wh_write_lock(wbr) au_rw_write_lock(&(wbr)->wbr_wh_rwsem)
329 #define wbr_wh_read_trylock(wbr) au_rw_read_trylock(&(wbr)->wbr_wh_rwsem)
330 #define wbr_wh_write_trylock(wbr) au_rw_write_trylock(&(wbr)->wbr_wh_rwsem)
331 /*
332 #define wbr_wh_read_trylock_nested(wbr) \
333 au_rw_read_trylock_nested(&(wbr)->wbr_wh_rwsem)
334 #define wbr_wh_write_trylock_nested(wbr) \
335 au_rw_write_trylock_nested(&(wbr)->wbr_wh_rwsem)
336 */
337
338 #define wbr_wh_read_unlock(wbr) au_rw_read_unlock(&(wbr)->wbr_wh_rwsem)
339 #define wbr_wh_write_unlock(wbr) au_rw_write_unlock(&(wbr)->wbr_wh_rwsem)
340 #define wbr_wh_downgrade_lock(wbr) au_rw_dgrade_lock(&(wbr)->wbr_wh_rwsem)
341
342 #define WbrWhMustNoWaiters(wbr) AuRwMustNoWaiters(&(wbr)->wbr_wh_rwsem)
343 #define WbrWhMustAnyLock(wbr) AuRwMustAnyLock(&(wbr)->wbr_wh_rwsem)
344 #define WbrWhMustWriteLock(wbr) AuRwMustWriteLock(&(wbr)->wbr_wh_rwsem)
345
346 /* ---------------------------------------------------------------------- */
347
348 #ifdef CONFIG_AUFS_FHSM
349 static inline void au_br_fhsm_init(struct au_br_fhsm *brfhsm)
350 {
351 mutex_init(&brfhsm->bf_lock);
352 brfhsm->bf_jiffy = 0;
353 brfhsm->bf_readable = 0;
354 }
355
356 static inline void au_br_fhsm_fin(struct au_br_fhsm *brfhsm)
357 {
358 mutex_destroy(&brfhsm->bf_lock);
359 }
360 #else
361 AuStubVoid(au_br_fhsm_init, struct au_br_fhsm *brfhsm)
362 AuStubVoid(au_br_fhsm_fin, struct au_br_fhsm *brfhsm)
363 #endif
364
365 #endif /* __KERNEL__ */
366 #endif /* __AUFS_BRANCH_H__ */