]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/aufs/super.h
drm/vc4: Add support for feeding DSI encoders from the pixel valve.
[mirror_ubuntu-zesty-kernel.git] / fs / aufs / super.h
CommitLineData
e14748e8
SF
1/*
2 * Copyright (C) 2005-2016 Junjiro R. Okajima
3 *
4 * This program, aufs is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18/*
19 * super_block operations
20 */
21
22#ifndef __AUFS_SUPER_H__
23#define __AUFS_SUPER_H__
24
25#ifdef __KERNEL__
26
27#include <linux/fs.h>
28#include <linux/kobject.h>
29#include "rwsem.h"
30#include "spl.h"
31#include "wkq.h"
32
33/* policies to select one among multiple writable branches */
34struct au_wbr_copyup_operations {
35 int (*copyup)(struct dentry *dentry);
36};
37
38#define AuWbr_DIR 1 /* target is a dir */
39#define AuWbr_PARENT (1 << 1) /* always require a parent */
40
41#define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
42#define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
43#define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
44
45struct au_wbr_create_operations {
46 int (*create)(struct dentry *dentry, unsigned int flags);
47 int (*init)(struct super_block *sb);
48 int (*fin)(struct super_block *sb);
49};
50
51struct au_wbr_mfs {
52 struct mutex mfs_lock; /* protect this structure */
53 unsigned long mfs_jiffy;
54 unsigned long mfs_expire;
55 aufs_bindex_t mfs_bindex;
56
57 unsigned long long mfsrr_bytes;
58 unsigned long long mfsrr_watermark;
59};
60
61#define AuPlink_NHASH 100
62static inline int au_plink_hash(ino_t ino)
63{
64 return ino % AuPlink_NHASH;
65}
66
67/* File-based Hierarchical Storage Management */
68struct au_fhsm {
69#ifdef CONFIG_AUFS_FHSM
70 /* allow only one process who can receive the notification */
71 spinlock_t fhsm_spin;
72 pid_t fhsm_pid;
73 wait_queue_head_t fhsm_wqh;
74 atomic_t fhsm_readable;
75
76 /* these are protected by si_rwsem */
77 unsigned long fhsm_expire;
78 aufs_bindex_t fhsm_bottom;
79#endif
80};
81
82#define AU_PIDSTEP (int)(BITS_TO_LONGS(PID_MAX_DEFAULT) * BITS_PER_LONG)
83#define AU_NPIDMAP (int)DIV_ROUND_UP(PID_MAX_LIMIT, AU_PIDSTEP)
84struct au_si_pid {
85 unsigned long *pid_bitmap[AU_NPIDMAP];
86 struct mutex pid_mtx;
87};
88
89struct au_branch;
90struct au_sbinfo {
91 /* nowait tasks in the system-wide workqueue */
92 struct au_nowait_tasks si_nowait;
93
94 /*
95 * tried sb->s_umount, but failed due to the dependecy between i_mutex.
96 * rwsem for au_sbinfo is necessary.
97 */
98 struct au_rwsem si_rwsem;
99
100 /* prevent recursive locking in deleting inode */
101 struct au_si_pid au_si_pid;
102
103 /*
104 * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
105 * remount.
106 */
107 struct percpu_counter si_ninodes, si_nfiles;
108
109 /* branch management */
110 unsigned int si_generation;
111
112 /* see AuSi_ flags */
113 unsigned char au_si_status;
114
115 aufs_bindex_t si_bbot;
116
117 /* dirty trick to keep br_id plus */
118 unsigned int si_last_br_id :
119 sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
120 struct au_branch **si_branch;
121
122 /* policy to select a writable branch */
123 unsigned char si_wbr_copyup;
124 unsigned char si_wbr_create;
125 struct au_wbr_copyup_operations *si_wbr_copyup_ops;
126 struct au_wbr_create_operations *si_wbr_create_ops;
127
128 /* round robin */
129 atomic_t si_wbr_rr_next;
130
131 /* most free space */
132 struct au_wbr_mfs si_wbr_mfs;
133
134 /* File-based Hierarchical Storage Management */
135 struct au_fhsm si_fhsm;
136
137 /* mount flags */
138 /* include/asm-ia64/siginfo.h defines a macro named si_flags */
139 unsigned int si_mntflags;
140
141 /* external inode number (bitmap and translation table) */
142 vfs_readf_t si_xread;
143 vfs_writef_t si_xwrite;
144 struct file *si_xib;
145 struct mutex si_xib_mtx; /* protect xib members */
146 unsigned long *si_xib_buf;
147 unsigned long si_xib_last_pindex;
148 int si_xib_next_bit;
149 aufs_bindex_t si_xino_brid;
150 unsigned long si_xino_jiffy;
151 unsigned long si_xino_expire;
152 /* reserved for future use */
153 /* unsigned long long si_xib_limit; */ /* Max xib file size */
154
155#ifdef CONFIG_AUFS_EXPORT
156 /* i_generation */
157 struct file *si_xigen;
158 atomic_t si_xigen_next;
159#endif
160
161 /* dirty trick to suppoer atomic_open */
162 struct au_sphlhead si_aopen;
163
164 /* vdir parameters */
165 unsigned long si_rdcache; /* max cache time in jiffies */
166 unsigned int si_rdblk; /* deblk size */
167 unsigned int si_rdhash; /* hash size */
168
169 /*
170 * If the number of whiteouts are larger than si_dirwh, leave all of
171 * them after au_whtmp_ren to reduce the cost of rmdir(2).
172 * future fsck.aufs or kernel thread will remove them later.
173 * Otherwise, remove all whiteouts and the dir in rmdir(2).
174 */
175 unsigned int si_dirwh;
176
177 /* pseudo_link list */
178 struct au_sphlhead si_plink[AuPlink_NHASH];
179 wait_queue_head_t si_plink_wq;
180 spinlock_t si_plink_maint_lock;
181 pid_t si_plink_maint_pid;
182
183 /* file list */
184 struct au_sphlhead si_files;
185
186 /* with/without getattr, brother of sb->s_d_op */
187 struct inode_operations *si_iop_array;
188
189 /*
190 * sysfs and lifetime management.
191 * this is not a small structure and it may be a waste of memory in case
192 * of sysfs is disabled, particulary when many aufs-es are mounted.
193 * but using sysfs is majority.
194 */
195 struct kobject si_kobj;
196#ifdef CONFIG_DEBUG_FS
197 struct dentry *si_dbgaufs;
198 struct dentry *si_dbgaufs_plink;
199 struct dentry *si_dbgaufs_xib;
200#ifdef CONFIG_AUFS_EXPORT
201 struct dentry *si_dbgaufs_xigen;
202#endif
203#endif
204
205#ifdef CONFIG_AUFS_SBILIST
206 struct hlist_node si_list;
207#endif
208
209 /* dirty, necessary for unmounting, sysfs and sysrq */
210 struct super_block *si_sb;
211};
212
213/* sbinfo status flags */
214/*
215 * set true when refresh_dirs() failed at remount time.
216 * then try refreshing dirs at access time again.
217 * if it is false, refreshing dirs at access time is unnecesary
218 */
219#define AuSi_FAILED_REFRESH_DIR 1
220#define AuSi_FHSM (1 << 1) /* fhsm is active now */
221#define AuSi_NO_DREVAL (1 << 2) /* disable all d_revalidate */
222
223#ifndef CONFIG_AUFS_FHSM
224#undef AuSi_FHSM
225#define AuSi_FHSM 0
226#endif
227
228static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
229 unsigned int flag)
230{
231 AuRwMustAnyLock(&sbi->si_rwsem);
232 return sbi->au_si_status & flag;
233}
234#define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
235#define au_fset_si(sbinfo, name) do { \
236 AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
237 (sbinfo)->au_si_status |= AuSi_##name; \
238} while (0)
239#define au_fclr_si(sbinfo, name) do { \
240 AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
241 (sbinfo)->au_si_status &= ~AuSi_##name; \
242} while (0)
243
244/* ---------------------------------------------------------------------- */
245
246/* policy to select one among writable branches */
247#define AuWbrCopyup(sbinfo, ...) \
248 ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
249#define AuWbrCreate(sbinfo, ...) \
250 ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
251
252/* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
253#define AuLock_DW 1 /* write-lock dentry */
254#define AuLock_IR (1 << 1) /* read-lock inode */
255#define AuLock_IW (1 << 2) /* write-lock inode */
256#define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
257#define AuLock_DIRS (1 << 4) /* target is a pair of dirs */
258 /* except RENAME_EXCHANGE */
259#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
260#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
261#define AuLock_GEN (1 << 7) /* test digen/iigen */
262#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
263#define au_fset_lock(flags, name) \
264 do { (flags) |= AuLock_##name; } while (0)
265#define au_fclr_lock(flags, name) \
266 do { (flags) &= ~AuLock_##name; } while (0)
267
268/* ---------------------------------------------------------------------- */
269
270/* super.c */
271extern struct file_system_type aufs_fs_type;
272struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
273typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
274 unsigned long long max, void *arg);
275void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
276 struct super_block *sb, void *arg);
277struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
278void au_iarray_free(struct inode **a, unsigned long long max);
279
280/* sbinfo.c */
281void au_si_free(struct kobject *kobj);
282int au_si_alloc(struct super_block *sb);
283int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
284
285unsigned int au_sigen_inc(struct super_block *sb);
286aufs_bindex_t au_new_br_id(struct super_block *sb);
287
288int si_read_lock(struct super_block *sb, int flags);
289int si_write_lock(struct super_block *sb, int flags);
290int aufs_read_lock(struct dentry *dentry, int flags);
291void aufs_read_unlock(struct dentry *dentry, int flags);
292void aufs_write_lock(struct dentry *dentry);
293void aufs_write_unlock(struct dentry *dentry);
294int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
295void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
296
297/* wbr_policy.c */
298extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
299extern struct au_wbr_create_operations au_wbr_create_ops[];
300int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
301int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
302int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
303
304/* mvdown.c */
305int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
306
307#ifdef CONFIG_AUFS_FHSM
308/* fhsm.c */
309
310static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
311{
312 pid_t pid;
313
314 spin_lock(&fhsm->fhsm_spin);
315 pid = fhsm->fhsm_pid;
316 spin_unlock(&fhsm->fhsm_spin);
317
318 return pid;
319}
320
321void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
322void au_fhsm_wrote_all(struct super_block *sb, int force);
323int au_fhsm_fd(struct super_block *sb, int oflags);
324int au_fhsm_br_alloc(struct au_branch *br);
325void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
326void au_fhsm_fin(struct super_block *sb);
327void au_fhsm_init(struct au_sbinfo *sbinfo);
328void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
329void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
330#else
331AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
332 int force)
333AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
334AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
335AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
336AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
337AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
338AuStubVoid(au_fhsm_fin, struct super_block *sb)
339AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
340AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
341AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
342#endif
343
344/* ---------------------------------------------------------------------- */
345
346static inline struct au_sbinfo *au_sbi(struct super_block *sb)
347{
348 return sb->s_fs_info;
349}
350
351/* ---------------------------------------------------------------------- */
352
353#ifdef CONFIG_AUFS_EXPORT
354int au_test_nfsd(void);
355void au_export_init(struct super_block *sb);
356void au_xigen_inc(struct inode *inode);
357int au_xigen_new(struct inode *inode);
358int au_xigen_set(struct super_block *sb, struct file *base);
359void au_xigen_clr(struct super_block *sb);
360
361static inline int au_busy_or_stale(void)
362{
363 if (!au_test_nfsd())
364 return -EBUSY;
365 return -ESTALE;
366}
367#else
368AuStubInt0(au_test_nfsd, void)
369AuStubVoid(au_export_init, struct super_block *sb)
370AuStubVoid(au_xigen_inc, struct inode *inode)
371AuStubInt0(au_xigen_new, struct inode *inode)
372AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
373AuStubVoid(au_xigen_clr, struct super_block *sb)
374AuStub(int, au_busy_or_stale, return -EBUSY, void)
375#endif /* CONFIG_AUFS_EXPORT */
376
377/* ---------------------------------------------------------------------- */
378
379#ifdef CONFIG_AUFS_SBILIST
380/* module.c */
381extern struct au_sphlhead au_sbilist;
382
383static inline void au_sbilist_init(void)
384{
385 au_sphl_init(&au_sbilist);
386}
387
388static inline void au_sbilist_add(struct super_block *sb)
389{
390 au_sphl_add(&au_sbi(sb)->si_list, &au_sbilist);
391}
392
393static inline void au_sbilist_del(struct super_block *sb)
394{
395 au_sphl_del(&au_sbi(sb)->si_list, &au_sbilist);
396}
397
398#ifdef CONFIG_AUFS_MAGIC_SYSRQ
399static inline void au_sbilist_lock(void)
400{
401 spin_lock(&au_sbilist.spin);
402}
403
404static inline void au_sbilist_unlock(void)
405{
406 spin_unlock(&au_sbilist.spin);
407}
408#define AuGFP_SBILIST GFP_ATOMIC
409#else
410AuStubVoid(au_sbilist_lock, void)
411AuStubVoid(au_sbilist_unlock, void)
412#define AuGFP_SBILIST GFP_NOFS
413#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
414#else
415AuStubVoid(au_sbilist_init, void)
416AuStubVoid(au_sbilist_add, struct super_block *sb)
417AuStubVoid(au_sbilist_del, struct super_block *sb)
418AuStubVoid(au_sbilist_lock, void)
419AuStubVoid(au_sbilist_unlock, void)
420#define AuGFP_SBILIST GFP_NOFS
421#endif
422
423/* ---------------------------------------------------------------------- */
424
425static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
426{
427 /*
428 * This function is a dynamic '__init' function actually,
429 * so the tiny check for si_rwsem is unnecessary.
430 */
431 /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
432#ifdef CONFIG_DEBUG_FS
433 sbinfo->si_dbgaufs = NULL;
434 sbinfo->si_dbgaufs_plink = NULL;
435 sbinfo->si_dbgaufs_xib = NULL;
436#ifdef CONFIG_AUFS_EXPORT
437 sbinfo->si_dbgaufs_xigen = NULL;
438#endif
439#endif
440}
441
442/* ---------------------------------------------------------------------- */
443
444static inline void si_pid_idx_bit(int *idx, pid_t *bit)
445{
446 /* the origin of pid is 1, but the bitmap's is 0 */
447 *bit = current->pid - 1;
448 *idx = *bit / AU_PIDSTEP;
449 *bit %= AU_PIDSTEP;
450}
451
452static inline int si_pid_test(struct super_block *sb)
453{
454 pid_t bit;
455 int idx;
456 unsigned long *bitmap;
457
458 si_pid_idx_bit(&idx, &bit);
459 bitmap = au_sbi(sb)->au_si_pid.pid_bitmap[idx];
460 if (bitmap)
461 return test_bit(bit, bitmap);
462 return 0;
463}
464
465static inline void si_pid_clr(struct super_block *sb)
466{
467 pid_t bit;
468 int idx;
469 unsigned long *bitmap;
470
471 si_pid_idx_bit(&idx, &bit);
472 bitmap = au_sbi(sb)->au_si_pid.pid_bitmap[idx];
473 BUG_ON(!bitmap);
474 AuDebugOn(!test_bit(bit, bitmap));
475 clear_bit(bit, bitmap);
476 /* smp_mb(); */
477}
478
479void si_pid_set(struct super_block *sb);
480
481/* ---------------------------------------------------------------------- */
482
483/* lock superblock. mainly for entry point functions */
484/*
485 * __si_read_lock, __si_write_lock,
486 * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
487 */
488AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
489
490#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
491#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
492#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
493
494static inline void si_noflush_read_lock(struct super_block *sb)
495{
496 __si_read_lock(sb);
497 si_pid_set(sb);
498}
499
500static inline int si_noflush_read_trylock(struct super_block *sb)
501{
502 int locked;
503
504 locked = __si_read_trylock(sb);
505 if (locked)
506 si_pid_set(sb);
507 return locked;
508}
509
510static inline void si_noflush_write_lock(struct super_block *sb)
511{
512 __si_write_lock(sb);
513 si_pid_set(sb);
514}
515
516static inline int si_noflush_write_trylock(struct super_block *sb)
517{
518 int locked;
519
520 locked = __si_write_trylock(sb);
521 if (locked)
522 si_pid_set(sb);
523 return locked;
524}
525
526#if 0 /* reserved */
527static inline int si_read_trylock(struct super_block *sb, int flags)
528{
529 if (au_ftest_lock(flags, FLUSH))
530 au_nwt_flush(&au_sbi(sb)->si_nowait);
531 return si_noflush_read_trylock(sb);
532}
533#endif
534
535static inline void si_read_unlock(struct super_block *sb)
536{
537 si_pid_clr(sb);
538 __si_read_unlock(sb);
539}
540
541#if 0 /* reserved */
542static inline int si_write_trylock(struct super_block *sb, int flags)
543{
544 if (au_ftest_lock(flags, FLUSH))
545 au_nwt_flush(&au_sbi(sb)->si_nowait);
546 return si_noflush_write_trylock(sb);
547}
548#endif
549
550static inline void si_write_unlock(struct super_block *sb)
551{
552 si_pid_clr(sb);
553 __si_write_unlock(sb);
554}
555
556#if 0 /* reserved */
557static inline void si_downgrade_lock(struct super_block *sb)
558{
559 __si_downgrade_lock(sb);
560}
561#endif
562
563/* ---------------------------------------------------------------------- */
564
565static inline aufs_bindex_t au_sbbot(struct super_block *sb)
566{
567 SiMustAnyLock(sb);
568 return au_sbi(sb)->si_bbot;
569}
570
571static inline unsigned int au_mntflags(struct super_block *sb)
572{
573 SiMustAnyLock(sb);
574 return au_sbi(sb)->si_mntflags;
575}
576
577static inline unsigned int au_sigen(struct super_block *sb)
578{
579 SiMustAnyLock(sb);
580 return au_sbi(sb)->si_generation;
581}
582
583static inline unsigned long long au_ninodes(struct super_block *sb)
584{
585 s64 n = percpu_counter_sum(&au_sbi(sb)->si_ninodes);
586
587 BUG_ON(n < 0);
588 return n;
589}
590
591static inline void au_ninodes_inc(struct super_block *sb)
592{
593 percpu_counter_inc(&au_sbi(sb)->si_ninodes);
594}
595
596static inline void au_ninodes_dec(struct super_block *sb)
597{
598 percpu_counter_dec(&au_sbi(sb)->si_ninodes);
599}
600
601static inline unsigned long long au_nfiles(struct super_block *sb)
602{
603 s64 n = percpu_counter_sum(&au_sbi(sb)->si_nfiles);
604
605 BUG_ON(n < 0);
606 return n;
607}
608
609static inline void au_nfiles_inc(struct super_block *sb)
610{
611 percpu_counter_inc(&au_sbi(sb)->si_nfiles);
612}
613
614static inline void au_nfiles_dec(struct super_block *sb)
615{
616 percpu_counter_dec(&au_sbi(sb)->si_nfiles);
617}
618
619static inline struct au_branch *au_sbr(struct super_block *sb,
620 aufs_bindex_t bindex)
621{
622 SiMustAnyLock(sb);
623 return au_sbi(sb)->si_branch[0 + bindex];
624}
625
626static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
627{
628 SiMustWriteLock(sb);
629 au_sbi(sb)->si_xino_brid = brid;
630}
631
632static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
633{
634 SiMustAnyLock(sb);
635 return au_sbi(sb)->si_xino_brid;
636}
637
638#endif /* __KERNEL__ */
639#endif /* __AUFS_SUPER_H__ */