]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - fs/aufs/super.h
UBUNTU: SAUCE: aufs -- Convert to use xattr handlers
[mirror_ubuntu-zesty-kernel.git] / fs / aufs / super.h
CommitLineData
5b88fdd9
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#define AuLock_NOPLM (1 << 5) /* return err in plm mode */
259#define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
260#define AuLock_GEN (1 << 7) /* test digen/iigen */
261#define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
262#define au_fset_lock(flags, name) \
263 do { (flags) |= AuLock_##name; } while (0)
264#define au_fclr_lock(flags, name) \
265 do { (flags) &= ~AuLock_##name; } while (0)
266
267/* ---------------------------------------------------------------------- */
268
269/* super.c */
270extern struct file_system_type aufs_fs_type;
271struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
272typedef unsigned long long (*au_arraycb_t)(struct super_block *sb, void *array,
273 unsigned long long max, void *arg);
274void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb,
275 struct super_block *sb, void *arg);
276struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
277void au_iarray_free(struct inode **a, unsigned long long max);
278
279/* sbinfo.c */
280void au_si_free(struct kobject *kobj);
281int au_si_alloc(struct super_block *sb);
282int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr, int may_shrink);
283
284unsigned int au_sigen_inc(struct super_block *sb);
285aufs_bindex_t au_new_br_id(struct super_block *sb);
286
287int si_read_lock(struct super_block *sb, int flags);
288int si_write_lock(struct super_block *sb, int flags);
289int aufs_read_lock(struct dentry *dentry, int flags);
290void aufs_read_unlock(struct dentry *dentry, int flags);
291void aufs_write_lock(struct dentry *dentry);
292void aufs_write_unlock(struct dentry *dentry);
293int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
294void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
295
296/* wbr_policy.c */
297extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
298extern struct au_wbr_create_operations au_wbr_create_ops[];
299int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
300int au_wbr_nonopq(struct dentry *dentry, aufs_bindex_t bindex);
301int au_wbr_do_copyup_bu(struct dentry *dentry, aufs_bindex_t btop);
302
303/* mvdown.c */
304int au_mvdown(struct dentry *dentry, struct aufs_mvdown __user *arg);
305
306#ifdef CONFIG_AUFS_FHSM
307/* fhsm.c */
308
309static inline pid_t au_fhsm_pid(struct au_fhsm *fhsm)
310{
311 pid_t pid;
312
313 spin_lock(&fhsm->fhsm_spin);
314 pid = fhsm->fhsm_pid;
315 spin_unlock(&fhsm->fhsm_spin);
316
317 return pid;
318}
319
320void au_fhsm_wrote(struct super_block *sb, aufs_bindex_t bindex, int force);
321void au_fhsm_wrote_all(struct super_block *sb, int force);
322int au_fhsm_fd(struct super_block *sb, int oflags);
323int au_fhsm_br_alloc(struct au_branch *br);
324void au_fhsm_set_bottom(struct super_block *sb, aufs_bindex_t bindex);
325void au_fhsm_fin(struct super_block *sb);
326void au_fhsm_init(struct au_sbinfo *sbinfo);
327void au_fhsm_set(struct au_sbinfo *sbinfo, unsigned int sec);
328void au_fhsm_show(struct seq_file *seq, struct au_sbinfo *sbinfo);
329#else
330AuStubVoid(au_fhsm_wrote, struct super_block *sb, aufs_bindex_t bindex,
331 int force)
332AuStubVoid(au_fhsm_wrote_all, struct super_block *sb, int force)
333AuStub(int, au_fhsm_fd, return -EOPNOTSUPP, struct super_block *sb, int oflags)
334AuStub(pid_t, au_fhsm_pid, return 0, struct au_fhsm *fhsm)
335AuStubInt0(au_fhsm_br_alloc, struct au_branch *br)
336AuStubVoid(au_fhsm_set_bottom, struct super_block *sb, aufs_bindex_t bindex)
337AuStubVoid(au_fhsm_fin, struct super_block *sb)
338AuStubVoid(au_fhsm_init, struct au_sbinfo *sbinfo)
339AuStubVoid(au_fhsm_set, struct au_sbinfo *sbinfo, unsigned int sec)
340AuStubVoid(au_fhsm_show, struct seq_file *seq, struct au_sbinfo *sbinfo)
341#endif
342
343/* ---------------------------------------------------------------------- */
344
345static inline struct au_sbinfo *au_sbi(struct super_block *sb)
346{
347 return sb->s_fs_info;
348}
349
350/* ---------------------------------------------------------------------- */
351
352#ifdef CONFIG_AUFS_EXPORT
353int au_test_nfsd(void);
354void au_export_init(struct super_block *sb);
355void au_xigen_inc(struct inode *inode);
356int au_xigen_new(struct inode *inode);
357int au_xigen_set(struct super_block *sb, struct file *base);
358void au_xigen_clr(struct super_block *sb);
359
360static inline int au_busy_or_stale(void)
361{
362 if (!au_test_nfsd())
363 return -EBUSY;
364 return -ESTALE;
365}
366#else
367AuStubInt0(au_test_nfsd, void)
368AuStubVoid(au_export_init, struct super_block *sb)
369AuStubVoid(au_xigen_inc, struct inode *inode)
370AuStubInt0(au_xigen_new, struct inode *inode)
371AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
372AuStubVoid(au_xigen_clr, struct super_block *sb)
373AuStub(int, au_busy_or_stale, return -EBUSY, void)
374#endif /* CONFIG_AUFS_EXPORT */
375
376/* ---------------------------------------------------------------------- */
377
378#ifdef CONFIG_AUFS_SBILIST
379/* module.c */
380extern struct au_sphlhead au_sbilist;
381
382static inline void au_sbilist_init(void)
383{
384 au_sphl_init(&au_sbilist);
385}
386
387static inline void au_sbilist_add(struct super_block *sb)
388{
389 au_sphl_add(&au_sbi(sb)->si_list, &au_sbilist);
390}
391
392static inline void au_sbilist_del(struct super_block *sb)
393{
394 au_sphl_del(&au_sbi(sb)->si_list, &au_sbilist);
395}
396
397#ifdef CONFIG_AUFS_MAGIC_SYSRQ
398static inline void au_sbilist_lock(void)
399{
400 spin_lock(&au_sbilist.spin);
401}
402
403static inline void au_sbilist_unlock(void)
404{
405 spin_unlock(&au_sbilist.spin);
406}
407#define AuGFP_SBILIST GFP_ATOMIC
408#else
409AuStubVoid(au_sbilist_lock, void)
410AuStubVoid(au_sbilist_unlock, void)
411#define AuGFP_SBILIST GFP_NOFS
412#endif /* CONFIG_AUFS_MAGIC_SYSRQ */
413#else
414AuStubVoid(au_sbilist_init, void)
415AuStubVoid(au_sbilist_add, struct super_block *sb)
416AuStubVoid(au_sbilist_del, struct super_block *sb)
417AuStubVoid(au_sbilist_lock, void)
418AuStubVoid(au_sbilist_unlock, void)
419#define AuGFP_SBILIST GFP_NOFS
420#endif
421
422/* ---------------------------------------------------------------------- */
423
424static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
425{
426 /*
427 * This function is a dynamic '__init' function actually,
428 * so the tiny check for si_rwsem is unnecessary.
429 */
430 /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
431#ifdef CONFIG_DEBUG_FS
432 sbinfo->si_dbgaufs = NULL;
433 sbinfo->si_dbgaufs_plink = NULL;
434 sbinfo->si_dbgaufs_xib = NULL;
435#ifdef CONFIG_AUFS_EXPORT
436 sbinfo->si_dbgaufs_xigen = NULL;
437#endif
438#endif
439}
440
441/* ---------------------------------------------------------------------- */
442
443static inline void si_pid_idx_bit(int *idx, pid_t *bit)
444{
445 /* the origin of pid is 1, but the bitmap's is 0 */
446 *bit = current->pid - 1;
447 *idx = *bit / AU_PIDSTEP;
448 *bit %= AU_PIDSTEP;
449}
450
451static inline int si_pid_test(struct super_block *sb)
452{
453 pid_t bit;
454 int idx;
455 unsigned long *bitmap;
456
457 si_pid_idx_bit(&idx, &bit);
458 bitmap = au_sbi(sb)->au_si_pid.pid_bitmap[idx];
459 if (bitmap)
460 return test_bit(bit, bitmap);
461 return 0;
462}
463
464static inline void si_pid_clr(struct super_block *sb)
465{
466 pid_t bit;
467 int idx;
468 unsigned long *bitmap;
469
470 si_pid_idx_bit(&idx, &bit);
471 bitmap = au_sbi(sb)->au_si_pid.pid_bitmap[idx];
472 BUG_ON(!bitmap);
473 AuDebugOn(!test_bit(bit, bitmap));
474 clear_bit(bit, bitmap);
475 /* smp_mb(); */
476}
477
478void si_pid_set(struct super_block *sb);
479
480/* ---------------------------------------------------------------------- */
481
482/* lock superblock. mainly for entry point functions */
483/*
484 * __si_read_lock, __si_write_lock,
485 * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
486 */
487AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
488
489#define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
490#define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
491#define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
492
493static inline void si_noflush_read_lock(struct super_block *sb)
494{
495 __si_read_lock(sb);
496 si_pid_set(sb);
497}
498
499static inline int si_noflush_read_trylock(struct super_block *sb)
500{
501 int locked;
502
503 locked = __si_read_trylock(sb);
504 if (locked)
505 si_pid_set(sb);
506 return locked;
507}
508
509static inline void si_noflush_write_lock(struct super_block *sb)
510{
511 __si_write_lock(sb);
512 si_pid_set(sb);
513}
514
515static inline int si_noflush_write_trylock(struct super_block *sb)
516{
517 int locked;
518
519 locked = __si_write_trylock(sb);
520 if (locked)
521 si_pid_set(sb);
522 return locked;
523}
524
525#if 0 /* reserved */
526static inline int si_read_trylock(struct super_block *sb, int flags)
527{
528 if (au_ftest_lock(flags, FLUSH))
529 au_nwt_flush(&au_sbi(sb)->si_nowait);
530 return si_noflush_read_trylock(sb);
531}
532#endif
533
534static inline void si_read_unlock(struct super_block *sb)
535{
536 si_pid_clr(sb);
537 __si_read_unlock(sb);
538}
539
540#if 0 /* reserved */
541static inline int si_write_trylock(struct super_block *sb, int flags)
542{
543 if (au_ftest_lock(flags, FLUSH))
544 au_nwt_flush(&au_sbi(sb)->si_nowait);
545 return si_noflush_write_trylock(sb);
546}
547#endif
548
549static inline void si_write_unlock(struct super_block *sb)
550{
551 si_pid_clr(sb);
552 __si_write_unlock(sb);
553}
554
555#if 0 /* reserved */
556static inline void si_downgrade_lock(struct super_block *sb)
557{
558 __si_downgrade_lock(sb);
559}
560#endif
561
562/* ---------------------------------------------------------------------- */
563
564static inline aufs_bindex_t au_sbbot(struct super_block *sb)
565{
566 SiMustAnyLock(sb);
567 return au_sbi(sb)->si_bbot;
568}
569
570static inline unsigned int au_mntflags(struct super_block *sb)
571{
572 SiMustAnyLock(sb);
573 return au_sbi(sb)->si_mntflags;
574}
575
576static inline unsigned int au_sigen(struct super_block *sb)
577{
578 SiMustAnyLock(sb);
579 return au_sbi(sb)->si_generation;
580}
581
582static inline unsigned long long au_ninodes(struct super_block *sb)
583{
584 s64 n = percpu_counter_sum(&au_sbi(sb)->si_ninodes);
585
586 BUG_ON(n < 0);
587 return n;
588}
589
590static inline void au_ninodes_inc(struct super_block *sb)
591{
592 percpu_counter_inc(&au_sbi(sb)->si_ninodes);
593}
594
595static inline void au_ninodes_dec(struct super_block *sb)
596{
597 percpu_counter_dec(&au_sbi(sb)->si_ninodes);
598}
599
600static inline unsigned long long au_nfiles(struct super_block *sb)
601{
602 s64 n = percpu_counter_sum(&au_sbi(sb)->si_nfiles);
603
604 BUG_ON(n < 0);
605 return n;
606}
607
608static inline void au_nfiles_inc(struct super_block *sb)
609{
610 percpu_counter_inc(&au_sbi(sb)->si_nfiles);
611}
612
613static inline void au_nfiles_dec(struct super_block *sb)
614{
615 percpu_counter_dec(&au_sbi(sb)->si_nfiles);
616}
617
618static inline struct au_branch *au_sbr(struct super_block *sb,
619 aufs_bindex_t bindex)
620{
621 SiMustAnyLock(sb);
622 return au_sbi(sb)->si_branch[0 + bindex];
623}
624
625static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
626{
627 SiMustWriteLock(sb);
628 au_sbi(sb)->si_xino_brid = brid;
629}
630
631static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
632{
633 SiMustAnyLock(sb);
634 return au_sbi(sb)->si_xino_brid;
635}
636
637#endif /* __KERNEL__ */
638#endif /* __AUFS_SUPER_H__ */