2 * Copyright (C) 2005-2013 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, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * super_block operations
23 #ifndef __AUFS_SUPER_H__
24 #define __AUFS_SUPER_H__
33 typedef ssize_t (*au_readf_t)(struct file *, char __user *, size_t, loff_t *);
34 typedef ssize_t (*au_writef_t)(struct file *, const char __user *, size_t,
37 /* policies to select one among multiple writable branches */
38 struct au_wbr_copyup_operations {
39 int (*copyup)(struct dentry *dentry);
42 #define AuWbr_DIR 1 /* target is a dir */
43 #define AuWbr_PARENT (1 << 1) /* always require a parent */
45 #define au_ftest_wbr(flags, name) ((flags) & AuWbr_##name)
46 #define au_fset_wbr(flags, name) { (flags) |= AuWbr_##name; }
47 #define au_fclr_wbr(flags, name) { (flags) &= ~AuWbr_##name; }
49 struct au_wbr_create_operations {
50 int (*create)(struct dentry *dentry, unsigned int flags);
51 int (*init)(struct super_block *sb);
52 int (*fin)(struct super_block *sb);
56 struct mutex mfs_lock; /* protect this structure */
57 unsigned long mfs_jiffy;
58 unsigned long mfs_expire;
59 aufs_bindex_t mfs_bindex;
61 unsigned long long mfsrr_bytes;
62 unsigned long long mfsrr_watermark;
67 struct hlist_node hlist;
73 #define AuPlink_NHASH 100
74 static inline int au_plink_hash(ino_t ino)
76 return ino % AuPlink_NHASH;
81 /* nowait tasks in the system-wide workqueue */
82 struct au_nowait_tasks si_nowait;
85 * tried sb->s_umount, but failed due to the dependecy between i_mutex.
86 * rwsem for au_sbinfo is necessary.
88 struct au_rwsem si_rwsem;
90 /* prevent recursive locking in deleting inode */
92 unsigned long *bitmap;
94 struct radix_tree_root tree;
98 * dirty approach to protect sb->sb_inodes and ->s_files from remount.
100 atomic_long_t si_ninodes, si_nfiles;
102 /* branch management */
103 unsigned int si_generation;
105 /* see above flags */
106 unsigned char au_si_status;
108 aufs_bindex_t si_bend;
110 /* dirty trick to keep br_id plus */
111 unsigned int si_last_br_id :
112 sizeof(aufs_bindex_t) * BITS_PER_BYTE - 1;
113 struct au_branch **si_branch;
115 /* policy to select a writable branch */
116 unsigned char si_wbr_copyup;
117 unsigned char si_wbr_create;
118 struct au_wbr_copyup_operations *si_wbr_copyup_ops;
119 struct au_wbr_create_operations *si_wbr_create_ops;
122 atomic_t si_wbr_rr_next;
124 /* most free space */
125 struct au_wbr_mfs si_wbr_mfs;
128 /* include/asm-ia64/siginfo.h defines a macro named si_flags */
129 unsigned int si_mntflags;
131 /* external inode number (bitmap and translation table) */
133 au_writef_t si_xwrite;
135 struct mutex si_xib_mtx; /* protect xib members */
136 unsigned long *si_xib_buf;
137 unsigned long si_xib_last_pindex;
139 aufs_bindex_t si_xino_brid;
140 unsigned long si_xino_jiffy;
141 unsigned long si_xino_expire;
142 /* reserved for future use */
143 /* unsigned long long si_xib_limit; */ /* Max xib file size */
145 #ifdef CONFIG_AUFS_EXPORT
147 struct file *si_xigen;
148 atomic_t si_xigen_next;
151 /* vdir parameters */
152 unsigned long si_rdcache; /* max cache time in jiffies */
153 unsigned int si_rdblk; /* deblk size */
154 unsigned int si_rdhash; /* hash size */
157 * If the number of whiteouts are larger than si_dirwh, leave all of
158 * them after au_whtmp_ren to reduce the cost of rmdir(2).
159 * future fsck.aufs or kernel thread will remove them later.
160 * Otherwise, remove all whiteouts and the dir in rmdir(2).
162 unsigned int si_dirwh;
165 * rename(2) a directory with all children.
167 /* reserved for future use */
170 /* pseudo_link list */
171 struct au_sphlhead si_plink[AuPlink_NHASH];
172 wait_queue_head_t si_plink_wq;
173 spinlock_t si_plink_maint_lock;
174 pid_t si_plink_maint_pid;
177 * sysfs and lifetime management.
178 * this is not a small structure and it may be a waste of memory in case
179 * of sysfs is disabled, particulary when many aufs-es are mounted.
180 * but using sysfs is majority.
182 struct kobject si_kobj;
183 #ifdef CONFIG_DEBUG_FS
184 struct dentry *si_dbgaufs;
185 struct dentry *si_dbgaufs_plink;
186 struct dentry *si_dbgaufs_xib;
187 #ifdef CONFIG_AUFS_EXPORT
188 struct dentry *si_dbgaufs_xigen;
192 #ifdef CONFIG_AUFS_SBILIST
193 struct list_head si_list;
196 /* dirty, necessary for unmounting, sysfs and sysrq */
197 struct super_block *si_sb;
200 /* sbinfo status flags */
202 * set true when refresh_dirs() failed at remount time.
203 * then try refreshing dirs at access time again.
204 * if it is false, refreshing dirs at access time is unnecesary
206 #define AuSi_FAILED_REFRESH_DIR 1
207 static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
210 AuRwMustAnyLock(&sbi->si_rwsem);
211 return sbi->au_si_status & flag;
213 #define au_ftest_si(sbinfo, name) au_do_ftest_si(sbinfo, AuSi_##name)
214 #define au_fset_si(sbinfo, name) do { \
215 AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
216 (sbinfo)->au_si_status |= AuSi_##name; \
218 #define au_fclr_si(sbinfo, name) do { \
219 AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
220 (sbinfo)->au_si_status &= ~AuSi_##name; \
223 /* ---------------------------------------------------------------------- */
225 /* policy to select one among writable branches */
226 #define AuWbrCopyup(sbinfo, ...) \
227 ((sbinfo)->si_wbr_copyup_ops->copyup(__VA_ARGS__))
228 #define AuWbrCreate(sbinfo, ...) \
229 ((sbinfo)->si_wbr_create_ops->create(__VA_ARGS__))
231 /* flags for si_read_lock()/aufs_read_lock()/di_read_lock() */
232 #define AuLock_DW 1 /* write-lock dentry */
233 #define AuLock_IR (1 << 1) /* read-lock inode */
234 #define AuLock_IW (1 << 2) /* write-lock inode */
235 #define AuLock_FLUSH (1 << 3) /* wait for 'nowait' tasks */
236 #define AuLock_DIR (1 << 4) /* target is a dir */
237 #define AuLock_NOPLM (1 << 5) /* return err in plm mode */
238 #define AuLock_NOPLMW (1 << 6) /* wait for plm mode ends */
239 #define AuLock_GEN (1 << 7) /* test digen/iigen */
240 #define au_ftest_lock(flags, name) ((flags) & AuLock_##name)
241 #define au_fset_lock(flags, name) \
242 do { (flags) |= AuLock_##name; } while (0)
243 #define au_fclr_lock(flags, name) \
244 do { (flags) &= ~AuLock_##name; } while (0)
246 /* ---------------------------------------------------------------------- */
249 extern struct file_system_type aufs_fs_type;
250 struct inode *au_iget_locked(struct super_block *sb, ino_t ino);
251 typedef unsigned long long (*au_arraycb_t)(void *array, unsigned long long max,
253 void au_array_free(void *array);
254 void *au_array_alloc(unsigned long long *hint, au_arraycb_t cb, void *arg);
255 struct inode **au_iarray_alloc(struct super_block *sb, unsigned long long *max);
256 void au_iarray_free(struct inode **a, unsigned long long max);
259 void au_si_free(struct kobject *kobj);
260 int au_si_alloc(struct super_block *sb);
261 int au_sbr_realloc(struct au_sbinfo *sbinfo, int nbr);
263 unsigned int au_sigen_inc(struct super_block *sb);
264 aufs_bindex_t au_new_br_id(struct super_block *sb);
266 int si_read_lock(struct super_block *sb, int flags);
267 int si_write_lock(struct super_block *sb, int flags);
268 int aufs_read_lock(struct dentry *dentry, int flags);
269 void aufs_read_unlock(struct dentry *dentry, int flags);
270 void aufs_write_lock(struct dentry *dentry);
271 void aufs_write_unlock(struct dentry *dentry);
272 int aufs_read_and_write_lock2(struct dentry *d1, struct dentry *d2, int flags);
273 void aufs_read_and_write_unlock2(struct dentry *d1, struct dentry *d2);
275 int si_pid_test_slow(struct super_block *sb);
276 void si_pid_set_slow(struct super_block *sb);
277 void si_pid_clr_slow(struct super_block *sb);
280 extern struct au_wbr_copyup_operations au_wbr_copyup_ops[];
281 extern struct au_wbr_create_operations au_wbr_create_ops[];
282 int au_cpdown_dirs(struct dentry *dentry, aufs_bindex_t bdst);
284 /* ---------------------------------------------------------------------- */
286 static inline struct au_sbinfo *au_sbi(struct super_block *sb)
288 return sb->s_fs_info;
291 /* ---------------------------------------------------------------------- */
293 #ifdef CONFIG_AUFS_EXPORT
294 int au_test_nfsd(void);
295 void au_export_init(struct super_block *sb);
296 void au_xigen_inc(struct inode *inode);
297 int au_xigen_new(struct inode *inode);
298 int au_xigen_set(struct super_block *sb, struct file *base);
299 void au_xigen_clr(struct super_block *sb);
301 static inline int au_busy_or_stale(void)
308 AuStubInt0(au_test_nfsd, void)
309 AuStubVoid(au_export_init, struct super_block *sb)
310 AuStubVoid(au_xigen_inc, struct inode *inode)
311 AuStubInt0(au_xigen_new, struct inode *inode)
312 AuStubInt0(au_xigen_set, struct super_block *sb, struct file *base)
313 AuStubVoid(au_xigen_clr, struct super_block *sb)
314 static inline int au_busy_or_stale(void)
318 #endif /* CONFIG_AUFS_EXPORT */
320 /* ---------------------------------------------------------------------- */
322 #ifdef CONFIG_AUFS_SBILIST
324 extern struct au_splhead au_sbilist;
326 static inline void au_sbilist_init(void)
328 au_spl_init(&au_sbilist);
331 static inline void au_sbilist_add(struct super_block *sb)
333 au_spl_add(&au_sbi(sb)->si_list, &au_sbilist);
336 static inline void au_sbilist_del(struct super_block *sb)
338 au_spl_del(&au_sbi(sb)->si_list, &au_sbilist);
341 #ifdef CONFIG_AUFS_MAGIC_SYSRQ
342 static inline void au_sbilist_lock(void)
344 spin_lock(&au_sbilist.spin);
347 static inline void au_sbilist_unlock(void)
349 spin_unlock(&au_sbilist.spin);
351 #define AuGFP_SBILIST GFP_ATOMIC
353 AuStubVoid(au_sbilist_lock, void)
354 AuStubVoid(au_sbilist_unlock, void)
355 #define AuGFP_SBILIST GFP_NOFS
356 #endif /* CONFIG_AUFS_MAGIC_SYSRQ */
358 AuStubVoid(au_sbilist_init, void)
359 AuStubVoid(au_sbilist_add, struct super_block*)
360 AuStubVoid(au_sbilist_del, struct super_block*)
361 AuStubVoid(au_sbilist_lock, void)
362 AuStubVoid(au_sbilist_unlock, void)
363 #define AuGFP_SBILIST GFP_NOFS
366 /* ---------------------------------------------------------------------- */
368 static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
371 * This function is a dynamic '__init' fucntion actually,
372 * so the tiny check for si_rwsem is unnecessary.
374 /* AuRwMustWriteLock(&sbinfo->si_rwsem); */
375 #ifdef CONFIG_DEBUG_FS
376 sbinfo->si_dbgaufs = NULL;
377 sbinfo->si_dbgaufs_plink = NULL;
378 sbinfo->si_dbgaufs_xib = NULL;
379 #ifdef CONFIG_AUFS_EXPORT
380 sbinfo->si_dbgaufs_xigen = NULL;
385 /* ---------------------------------------------------------------------- */
387 static inline pid_t si_pid_bit(void)
389 /* the origin of pid is 1, but the bitmap's is 0 */
390 return current->pid - 1;
393 static inline int si_pid_test(struct super_block *sb)
395 pid_t bit = si_pid_bit();
396 if (bit < PID_MAX_DEFAULT)
397 return test_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
399 return si_pid_test_slow(sb);
402 static inline void si_pid_set(struct super_block *sb)
404 pid_t bit = si_pid_bit();
405 if (bit < PID_MAX_DEFAULT) {
406 AuDebugOn(test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
407 set_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
413 static inline void si_pid_clr(struct super_block *sb)
415 pid_t bit = si_pid_bit();
416 if (bit < PID_MAX_DEFAULT) {
417 AuDebugOn(!test_bit(bit, au_sbi(sb)->au_si_pid.bitmap));
418 clear_bit(bit, au_sbi(sb)->au_si_pid.bitmap);
424 /* ---------------------------------------------------------------------- */
426 /* lock superblock. mainly for entry point functions */
428 * __si_read_lock, __si_write_lock,
429 * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
431 AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
433 #define SiMustNoWaiters(sb) AuRwMustNoWaiters(&au_sbi(sb)->si_rwsem)
434 #define SiMustAnyLock(sb) AuRwMustAnyLock(&au_sbi(sb)->si_rwsem)
435 #define SiMustWriteLock(sb) AuRwMustWriteLock(&au_sbi(sb)->si_rwsem)
437 static inline void si_noflush_read_lock(struct super_block *sb)
443 static inline int si_noflush_read_trylock(struct super_block *sb)
445 int locked = __si_read_trylock(sb);
451 static inline void si_noflush_write_lock(struct super_block *sb)
457 static inline int si_noflush_write_trylock(struct super_block *sb)
459 int locked = __si_write_trylock(sb);
466 static inline int si_read_trylock(struct super_block *sb, int flags)
468 if (au_ftest_lock(flags, FLUSH))
469 au_nwt_flush(&au_sbi(sb)->si_nowait);
470 return si_noflush_read_trylock(sb);
474 static inline void si_read_unlock(struct super_block *sb)
477 __si_read_unlock(sb);
481 static inline int si_write_trylock(struct super_block *sb, int flags)
483 if (au_ftest_lock(flags, FLUSH))
484 au_nwt_flush(&au_sbi(sb)->si_nowait);
485 return si_noflush_write_trylock(sb);
489 static inline void si_write_unlock(struct super_block *sb)
492 __si_write_unlock(sb);
496 static inline void si_downgrade_lock(struct super_block *sb)
498 __si_downgrade_lock(sb);
502 /* ---------------------------------------------------------------------- */
504 static inline aufs_bindex_t au_sbend(struct super_block *sb)
507 return au_sbi(sb)->si_bend;
510 static inline unsigned int au_mntflags(struct super_block *sb)
513 return au_sbi(sb)->si_mntflags;
516 static inline unsigned int au_sigen(struct super_block *sb)
519 return au_sbi(sb)->si_generation;
522 static inline void au_ninodes_inc(struct super_block *sb)
524 atomic_long_inc(&au_sbi(sb)->si_ninodes);
527 static inline void au_ninodes_dec(struct super_block *sb)
529 AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_ninodes));
530 atomic_long_dec(&au_sbi(sb)->si_ninodes);
533 static inline void au_nfiles_inc(struct super_block *sb)
535 atomic_long_inc(&au_sbi(sb)->si_nfiles);
538 static inline void au_nfiles_dec(struct super_block *sb)
540 AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_nfiles));
541 atomic_long_dec(&au_sbi(sb)->si_nfiles);
544 static inline struct au_branch *au_sbr(struct super_block *sb,
545 aufs_bindex_t bindex)
548 return au_sbi(sb)->si_branch[0 + bindex];
551 static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
554 au_sbi(sb)->si_xino_brid = brid;
557 static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
560 return au_sbi(sb)->si_xino_brid;
563 #endif /* __KERNEL__ */
564 #endif /* __AUFS_SUPER_H__ */