2 * Copyright (C) 2005-2016 Junjiro R. Okajima
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.
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.
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/>.
19 * super_block operations
22 #ifndef __AUFS_SUPER_H__
23 #define __AUFS_SUPER_H__
28 #include <linux/kobject.h>
33 /* policies to select one among multiple writable branches */
34 struct au_wbr_copyup_operations
{
35 int (*copyup
)(struct dentry
*dentry
);
38 #define AuWbr_DIR 1 /* target is a dir */
39 #define AuWbr_PARENT (1 << 1) /* always require a parent */
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; }
45 struct 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
);
52 struct mutex mfs_lock
; /* protect this structure */
53 unsigned long mfs_jiffy
;
54 unsigned long mfs_expire
;
55 aufs_bindex_t mfs_bindex
;
57 unsigned long long mfsrr_bytes
;
58 unsigned long long mfsrr_watermark
;
61 #define AuPlink_NHASH 100
62 static inline int au_plink_hash(ino_t ino
)
64 return ino
% AuPlink_NHASH
;
67 /* File-based Hierarchical Storage Management */
69 #ifdef CONFIG_AUFS_FHSM
70 /* allow only one process who can receive the notification */
73 wait_queue_head_t fhsm_wqh
;
74 atomic_t fhsm_readable
;
76 /* these are protected by si_rwsem */
77 unsigned long fhsm_expire
;
78 aufs_bindex_t fhsm_bottom
;
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)
85 unsigned long *pid_bitmap
[AU_NPIDMAP
];
91 /* nowait tasks in the system-wide workqueue */
92 struct au_nowait_tasks si_nowait
;
95 * tried sb->s_umount, but failed due to the dependecy between i_mutex.
96 * rwsem for au_sbinfo is necessary.
98 struct au_rwsem si_rwsem
;
100 /* prevent recursive locking in deleting inode */
101 struct au_si_pid au_si_pid
;
104 * dirty approach to protect sb->sb_inodes and ->s_files (gone) from
107 struct percpu_counter si_ninodes
, si_nfiles
;
109 /* branch management */
110 unsigned int si_generation
;
112 /* see AuSi_ flags */
113 unsigned char au_si_status
;
115 aufs_bindex_t si_bbot
;
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
;
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
;
129 atomic_t si_wbr_rr_next
;
131 /* most free space */
132 struct au_wbr_mfs si_wbr_mfs
;
134 /* File-based Hierarchical Storage Management */
135 struct au_fhsm si_fhsm
;
138 /* include/asm-ia64/siginfo.h defines a macro named si_flags */
139 unsigned int si_mntflags
;
141 /* external inode number (bitmap and translation table) */
142 vfs_readf_t si_xread
;
143 vfs_writef_t si_xwrite
;
145 struct mutex si_xib_mtx
; /* protect xib members */
146 unsigned long *si_xib_buf
;
147 unsigned long si_xib_last_pindex
;
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 */
155 #ifdef CONFIG_AUFS_EXPORT
157 struct file
*si_xigen
;
158 atomic_t si_xigen_next
;
161 /* dirty trick to suppoer atomic_open */
162 struct au_sphlhead si_aopen
;
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 */
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).
175 unsigned int si_dirwh
;
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
;
184 struct au_sphlhead si_files
;
186 /* with/without getattr, brother of sb->s_d_op */
187 struct inode_operations
*si_iop_array
;
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.
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
;
205 #ifdef CONFIG_AUFS_SBILIST
206 struct hlist_node si_list
;
209 /* dirty, necessary for unmounting, sysfs and sysrq */
210 struct super_block
*si_sb
;
213 /* sbinfo status flags */
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
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 */
223 #ifndef CONFIG_AUFS_FHSM
228 static inline unsigned char au_do_ftest_si(struct au_sbinfo
*sbi
,
231 AuRwMustAnyLock(&sbi
->si_rwsem
);
232 return sbi
->au_si_status
& flag
;
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; \
239 #define au_fclr_si(sbinfo, name) do { \
240 AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
241 (sbinfo)->au_si_status &= ~AuSi_##name; \
244 /* ---------------------------------------------------------------------- */
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__))
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)
268 /* ---------------------------------------------------------------------- */
271 extern struct file_system_type aufs_fs_type
;
272 struct inode
*au_iget_locked(struct super_block
*sb
, ino_t ino
);
273 typedef unsigned long long (*au_arraycb_t
)(struct super_block
*sb
, void *array
,
274 unsigned long long max
, void *arg
);
275 void *au_array_alloc(unsigned long long *hint
, au_arraycb_t cb
,
276 struct super_block
*sb
, void *arg
);
277 struct inode
**au_iarray_alloc(struct super_block
*sb
, unsigned long long *max
);
278 void au_iarray_free(struct inode
**a
, unsigned long long max
);
281 void au_si_free(struct kobject
*kobj
);
282 int au_si_alloc(struct super_block
*sb
);
283 int au_sbr_realloc(struct au_sbinfo
*sbinfo
, int nbr
, int may_shrink
);
285 unsigned int au_sigen_inc(struct super_block
*sb
);
286 aufs_bindex_t
au_new_br_id(struct super_block
*sb
);
288 int si_read_lock(struct super_block
*sb
, int flags
);
289 int si_write_lock(struct super_block
*sb
, int flags
);
290 int aufs_read_lock(struct dentry
*dentry
, int flags
);
291 void aufs_read_unlock(struct dentry
*dentry
, int flags
);
292 void aufs_write_lock(struct dentry
*dentry
);
293 void aufs_write_unlock(struct dentry
*dentry
);
294 int aufs_read_and_write_lock2(struct dentry
*d1
, struct dentry
*d2
, int flags
);
295 void aufs_read_and_write_unlock2(struct dentry
*d1
, struct dentry
*d2
);
298 extern struct au_wbr_copyup_operations au_wbr_copyup_ops
[];
299 extern struct au_wbr_create_operations au_wbr_create_ops
[];
300 int au_cpdown_dirs(struct dentry
*dentry
, aufs_bindex_t bdst
);
301 int au_wbr_nonopq(struct dentry
*dentry
, aufs_bindex_t bindex
);
302 int au_wbr_do_copyup_bu(struct dentry
*dentry
, aufs_bindex_t btop
);
305 int au_mvdown(struct dentry
*dentry
, struct aufs_mvdown __user
*arg
);
307 #ifdef CONFIG_AUFS_FHSM
310 static inline pid_t
au_fhsm_pid(struct au_fhsm
*fhsm
)
314 spin_lock(&fhsm
->fhsm_spin
);
315 pid
= fhsm
->fhsm_pid
;
316 spin_unlock(&fhsm
->fhsm_spin
);
321 void au_fhsm_wrote(struct super_block
*sb
, aufs_bindex_t bindex
, int force
);
322 void au_fhsm_wrote_all(struct super_block
*sb
, int force
);
323 int au_fhsm_fd(struct super_block
*sb
, int oflags
);
324 int au_fhsm_br_alloc(struct au_branch
*br
);
325 void au_fhsm_set_bottom(struct super_block
*sb
, aufs_bindex_t bindex
);
326 void au_fhsm_fin(struct super_block
*sb
);
327 void au_fhsm_init(struct au_sbinfo
*sbinfo
);
328 void au_fhsm_set(struct au_sbinfo
*sbinfo
, unsigned int sec
);
329 void au_fhsm_show(struct seq_file
*seq
, struct au_sbinfo
*sbinfo
);
331 AuStubVoid(au_fhsm_wrote
, struct super_block
*sb
, aufs_bindex_t bindex
,
333 AuStubVoid(au_fhsm_wrote_all
, struct super_block
*sb
, int force
)
334 AuStub(int, au_fhsm_fd
, return -EOPNOTSUPP
, struct super_block
*sb
, int oflags
)
335 AuStub(pid_t
, au_fhsm_pid
, return 0, struct au_fhsm
*fhsm
)
336 AuStubInt0(au_fhsm_br_alloc
, struct au_branch
*br
)
337 AuStubVoid(au_fhsm_set_bottom
, struct super_block
*sb
, aufs_bindex_t bindex
)
338 AuStubVoid(au_fhsm_fin
, struct super_block
*sb
)
339 AuStubVoid(au_fhsm_init
, struct au_sbinfo
*sbinfo
)
340 AuStubVoid(au_fhsm_set
, struct au_sbinfo
*sbinfo
, unsigned int sec
)
341 AuStubVoid(au_fhsm_show
, struct seq_file
*seq
, struct au_sbinfo
*sbinfo
)
344 /* ---------------------------------------------------------------------- */
346 static inline struct au_sbinfo
*au_sbi(struct super_block
*sb
)
348 return sb
->s_fs_info
;
351 /* ---------------------------------------------------------------------- */
353 #ifdef CONFIG_AUFS_EXPORT
354 int au_test_nfsd(void);
355 void au_export_init(struct super_block
*sb
);
356 void au_xigen_inc(struct inode
*inode
);
357 int au_xigen_new(struct inode
*inode
);
358 int au_xigen_set(struct super_block
*sb
, struct file
*base
);
359 void au_xigen_clr(struct super_block
*sb
);
361 static inline int au_busy_or_stale(void)
368 AuStubInt0(au_test_nfsd
, void)
369 AuStubVoid(au_export_init
, struct super_block
*sb
)
370 AuStubVoid(au_xigen_inc
, struct inode
*inode
)
371 AuStubInt0(au_xigen_new
, struct inode
*inode
)
372 AuStubInt0(au_xigen_set
, struct super_block
*sb
, struct file
*base
)
373 AuStubVoid(au_xigen_clr
, struct super_block
*sb
)
374 AuStub(int, au_busy_or_stale
, return -EBUSY
, void)
375 #endif /* CONFIG_AUFS_EXPORT */
377 /* ---------------------------------------------------------------------- */
379 #ifdef CONFIG_AUFS_SBILIST
381 extern struct au_sphlhead au_sbilist
;
383 static inline void au_sbilist_init(void)
385 au_sphl_init(&au_sbilist
);
388 static inline void au_sbilist_add(struct super_block
*sb
)
390 au_sphl_add(&au_sbi(sb
)->si_list
, &au_sbilist
);
393 static inline void au_sbilist_del(struct super_block
*sb
)
395 au_sphl_del(&au_sbi(sb
)->si_list
, &au_sbilist
);
398 #ifdef CONFIG_AUFS_MAGIC_SYSRQ
399 static inline void au_sbilist_lock(void)
401 spin_lock(&au_sbilist
.spin
);
404 static inline void au_sbilist_unlock(void)
406 spin_unlock(&au_sbilist
.spin
);
408 #define AuGFP_SBILIST GFP_ATOMIC
410 AuStubVoid(au_sbilist_lock
, void)
411 AuStubVoid(au_sbilist_unlock
, void)
412 #define AuGFP_SBILIST GFP_NOFS
413 #endif /* CONFIG_AUFS_MAGIC_SYSRQ */
415 AuStubVoid(au_sbilist_init
, void)
416 AuStubVoid(au_sbilist_add
, struct super_block
*sb
)
417 AuStubVoid(au_sbilist_del
, struct super_block
*sb
)
418 AuStubVoid(au_sbilist_lock
, void)
419 AuStubVoid(au_sbilist_unlock
, void)
420 #define AuGFP_SBILIST GFP_NOFS
423 /* ---------------------------------------------------------------------- */
425 static inline void dbgaufs_si_null(struct au_sbinfo
*sbinfo
)
428 * This function is a dynamic '__init' function actually,
429 * so the tiny check for si_rwsem is unnecessary.
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
;
442 /* ---------------------------------------------------------------------- */
444 static inline void si_pid_idx_bit(int *idx
, pid_t
*bit
)
446 /* the origin of pid is 1, but the bitmap's is 0 */
447 *bit
= current
->pid
- 1;
448 *idx
= *bit
/ AU_PIDSTEP
;
452 static inline int si_pid_test(struct super_block
*sb
)
456 unsigned long *bitmap
;
458 si_pid_idx_bit(&idx
, &bit
);
459 bitmap
= au_sbi(sb
)->au_si_pid
.pid_bitmap
[idx
];
461 return test_bit(bit
, bitmap
);
465 static inline void si_pid_clr(struct super_block
*sb
)
469 unsigned long *bitmap
;
471 si_pid_idx_bit(&idx
, &bit
);
472 bitmap
= au_sbi(sb
)->au_si_pid
.pid_bitmap
[idx
];
474 AuDebugOn(!test_bit(bit
, bitmap
));
475 clear_bit(bit
, bitmap
);
479 void si_pid_set(struct super_block
*sb
);
481 /* ---------------------------------------------------------------------- */
483 /* lock superblock. mainly for entry point functions */
485 * __si_read_lock, __si_write_lock,
486 * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
488 AuSimpleRwsemFuncs(__si
, struct super_block
*sb
, &au_sbi(sb
)->si_rwsem
);
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)
494 static inline void si_noflush_read_lock(struct super_block
*sb
)
500 static inline int si_noflush_read_trylock(struct super_block
*sb
)
504 locked
= __si_read_trylock(sb
);
510 static inline void si_noflush_write_lock(struct super_block
*sb
)
516 static inline int si_noflush_write_trylock(struct super_block
*sb
)
520 locked
= __si_write_trylock(sb
);
527 static inline int si_read_trylock(struct super_block
*sb
, int flags
)
529 if (au_ftest_lock(flags
, FLUSH
))
530 au_nwt_flush(&au_sbi(sb
)->si_nowait
);
531 return si_noflush_read_trylock(sb
);
535 static inline void si_read_unlock(struct super_block
*sb
)
538 __si_read_unlock(sb
);
542 static inline int si_write_trylock(struct super_block
*sb
, int flags
)
544 if (au_ftest_lock(flags
, FLUSH
))
545 au_nwt_flush(&au_sbi(sb
)->si_nowait
);
546 return si_noflush_write_trylock(sb
);
550 static inline void si_write_unlock(struct super_block
*sb
)
553 __si_write_unlock(sb
);
557 static inline void si_downgrade_lock(struct super_block
*sb
)
559 __si_downgrade_lock(sb
);
563 /* ---------------------------------------------------------------------- */
565 static inline aufs_bindex_t
au_sbbot(struct super_block
*sb
)
568 return au_sbi(sb
)->si_bbot
;
571 static inline unsigned int au_mntflags(struct super_block
*sb
)
574 return au_sbi(sb
)->si_mntflags
;
577 static inline unsigned int au_sigen(struct super_block
*sb
)
580 return au_sbi(sb
)->si_generation
;
583 static inline unsigned long long au_ninodes(struct super_block
*sb
)
585 s64 n
= percpu_counter_sum(&au_sbi(sb
)->si_ninodes
);
591 static inline void au_ninodes_inc(struct super_block
*sb
)
593 percpu_counter_inc(&au_sbi(sb
)->si_ninodes
);
596 static inline void au_ninodes_dec(struct super_block
*sb
)
598 percpu_counter_dec(&au_sbi(sb
)->si_ninodes
);
601 static inline unsigned long long au_nfiles(struct super_block
*sb
)
603 s64 n
= percpu_counter_sum(&au_sbi(sb
)->si_nfiles
);
609 static inline void au_nfiles_inc(struct super_block
*sb
)
611 percpu_counter_inc(&au_sbi(sb
)->si_nfiles
);
614 static inline void au_nfiles_dec(struct super_block
*sb
)
616 percpu_counter_dec(&au_sbi(sb
)->si_nfiles
);
619 static inline struct au_branch
*au_sbr(struct super_block
*sb
,
620 aufs_bindex_t bindex
)
623 return au_sbi(sb
)->si_branch
[0 + bindex
];
626 static inline void au_xino_brid_set(struct super_block
*sb
, aufs_bindex_t brid
)
629 au_sbi(sb
)->si_xino_brid
= brid
;
632 static inline aufs_bindex_t
au_xino_brid(struct super_block
*sb
)
635 return au_sbi(sb
)->si_xino_brid
;
638 #endif /* __KERNEL__ */
639 #endif /* __AUFS_SUPER_H__ */