update aufs to it's latest standalone 3.2 branch
[pandora-kernel.git] / fs / aufs / super.h
1 /*
2  * Copyright (C) 2005-2013 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, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17  */
18
19 /*
20  * super_block operations
21  */
22
23 #ifndef __AUFS_SUPER_H__
24 #define __AUFS_SUPER_H__
25
26 #ifdef __KERNEL__
27
28 #include <linux/fs.h>
29 #include "rwsem.h"
30 #include "spl.h"
31 #include "wkq.h"
32
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,
35                                loff_t *);
36
37 /* policies to select one among multiple writable branches */
38 struct au_wbr_copyup_operations {
39         int (*copyup)(struct dentry *dentry);
40 };
41
42 #define AuWbr_DIR       1               /* target is a dir */
43 #define AuWbr_PARENT    (1 << 1)        /* always require a parent */
44
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; }
48
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);
53 };
54
55 struct au_wbr_mfs {
56         struct mutex    mfs_lock; /* protect this structure */
57         unsigned long   mfs_jiffy;
58         unsigned long   mfs_expire;
59         aufs_bindex_t   mfs_bindex;
60
61         unsigned long long      mfsrr_bytes;
62         unsigned long long      mfsrr_watermark;
63 };
64
65 struct pseudo_link {
66         union {
67                 struct hlist_node hlist;
68                 struct rcu_head rcu;
69         };
70         struct inode *inode;
71 };
72
73 #define AuPlink_NHASH 100
74 static inline int au_plink_hash(ino_t ino)
75 {
76         return ino % AuPlink_NHASH;
77 }
78
79 struct au_branch;
80 struct au_sbinfo {
81         /* nowait tasks in the system-wide workqueue */
82         struct au_nowait_tasks  si_nowait;
83
84         /*
85          * tried sb->s_umount, but failed due to the dependecy between i_mutex.
86          * rwsem for au_sbinfo is necessary.
87          */
88         struct au_rwsem         si_rwsem;
89
90         /* prevent recursive locking in deleting inode */
91         struct {
92                 unsigned long           *bitmap;
93                 spinlock_t              tree_lock;
94                 struct radix_tree_root  tree;
95         } au_si_pid;
96
97         /*
98          * dirty approach to protect sb->sb_inodes and ->s_files from remount.
99          */
100         atomic_long_t           si_ninodes, si_nfiles;
101
102         /* branch management */
103         unsigned int            si_generation;
104
105         /* see above flags */
106         unsigned char           au_si_status;
107
108         aufs_bindex_t           si_bend;
109
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;
114
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;
120
121         /* round robin */
122         atomic_t                si_wbr_rr_next;
123
124         /* most free space */
125         struct au_wbr_mfs       si_wbr_mfs;
126
127         /* mount flags */
128         /* include/asm-ia64/siginfo.h defines a macro named si_flags */
129         unsigned int            si_mntflags;
130
131         /* external inode number (bitmap and translation table) */
132         au_readf_t              si_xread;
133         au_writef_t             si_xwrite;
134         struct file             *si_xib;
135         struct mutex            si_xib_mtx; /* protect xib members */
136         unsigned long           *si_xib_buf;
137         unsigned long           si_xib_last_pindex;
138         int                     si_xib_next_bit;
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 */
144
145 #ifdef CONFIG_AUFS_EXPORT
146         /* i_generation */
147         struct file             *si_xigen;
148         atomic_t                si_xigen_next;
149 #endif
150
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 */
155
156         /*
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).
161          */
162         unsigned int            si_dirwh;
163
164         /*
165          * rename(2) a directory with all children.
166          */
167         /* reserved for future use */
168         /* int                  si_rendir; */
169
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;
175
176         /*
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.
181          */
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;
189 #endif
190 #endif
191
192 #ifdef CONFIG_AUFS_SBILIST
193         struct list_head        si_list;
194 #endif
195
196         /* dirty, necessary for unmounting, sysfs and sysrq */
197         struct super_block      *si_sb;
198 };
199
200 /* sbinfo status flags */
201 /*
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
205  */
206 #define AuSi_FAILED_REFRESH_DIR 1
207 static inline unsigned char au_do_ftest_si(struct au_sbinfo *sbi,
208                                            unsigned int flag)
209 {
210         AuRwMustAnyLock(&sbi->si_rwsem);
211         return sbi->au_si_status & flag;
212 }
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; \
217 } while (0)
218 #define au_fclr_si(sbinfo, name) do { \
219         AuRwMustWriteLock(&(sbinfo)->si_rwsem); \
220         (sbinfo)->au_si_status &= ~AuSi_##name; \
221 } while (0)
222
223 /* ---------------------------------------------------------------------- */
224
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__))
230
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)
245
246 /* ---------------------------------------------------------------------- */
247
248 /* super.c */
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,
252                                            void *arg);
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);
257
258 /* sbinfo.c */
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);
262
263 unsigned int au_sigen_inc(struct super_block *sb);
264 aufs_bindex_t au_new_br_id(struct super_block *sb);
265
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);
274
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);
278
279 /* wbr_policy.c */
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);
283
284 /* ---------------------------------------------------------------------- */
285
286 static inline struct au_sbinfo *au_sbi(struct super_block *sb)
287 {
288         return sb->s_fs_info;
289 }
290
291 /* ---------------------------------------------------------------------- */
292
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);
300
301 static inline int au_busy_or_stale(void)
302 {
303         if (!au_test_nfsd())
304                 return -EBUSY;
305         return -ESTALE;
306 }
307 #else
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)
315 {
316         return -EBUSY;
317 }
318 #endif /* CONFIG_AUFS_EXPORT */
319
320 /* ---------------------------------------------------------------------- */
321
322 #ifdef CONFIG_AUFS_SBILIST
323 /* module.c */
324 extern struct au_splhead au_sbilist;
325
326 static inline void au_sbilist_init(void)
327 {
328         au_spl_init(&au_sbilist);
329 }
330
331 static inline void au_sbilist_add(struct super_block *sb)
332 {
333         au_spl_add(&au_sbi(sb)->si_list, &au_sbilist);
334 }
335
336 static inline void au_sbilist_del(struct super_block *sb)
337 {
338         au_spl_del(&au_sbi(sb)->si_list, &au_sbilist);
339 }
340
341 #ifdef CONFIG_AUFS_MAGIC_SYSRQ
342 static inline void au_sbilist_lock(void)
343 {
344         spin_lock(&au_sbilist.spin);
345 }
346
347 static inline void au_sbilist_unlock(void)
348 {
349         spin_unlock(&au_sbilist.spin);
350 }
351 #define AuGFP_SBILIST   GFP_ATOMIC
352 #else
353 AuStubVoid(au_sbilist_lock, void)
354 AuStubVoid(au_sbilist_unlock, void)
355 #define AuGFP_SBILIST   GFP_NOFS
356 #endif /* CONFIG_AUFS_MAGIC_SYSRQ */
357 #else
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
364 #endif
365
366 /* ---------------------------------------------------------------------- */
367
368 static inline void dbgaufs_si_null(struct au_sbinfo *sbinfo)
369 {
370         /*
371          * This function is a dynamic '__init' fucntion actually,
372          * so the tiny check for si_rwsem is unnecessary.
373          */
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;
381 #endif
382 #endif
383 }
384
385 /* ---------------------------------------------------------------------- */
386
387 static inline pid_t si_pid_bit(void)
388 {
389         /* the origin of pid is 1, but the bitmap's is 0 */
390         return current->pid - 1;
391 }
392
393 static inline int si_pid_test(struct super_block *sb)
394 {
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);
398         else
399                 return si_pid_test_slow(sb);
400 }
401
402 static inline void si_pid_set(struct super_block *sb)
403 {
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);
408                 /* smp_mb(); */
409         } else
410                 si_pid_set_slow(sb);
411 }
412
413 static inline void si_pid_clr(struct super_block *sb)
414 {
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);
419                 /* smp_mb(); */
420         } else
421                 si_pid_clr_slow(sb);
422 }
423
424 /* ---------------------------------------------------------------------- */
425
426 /* lock superblock. mainly for entry point functions */
427 /*
428  * __si_read_lock, __si_write_lock,
429  * __si_read_unlock, __si_write_unlock, __si_downgrade_lock
430  */
431 AuSimpleRwsemFuncs(__si, struct super_block *sb, &au_sbi(sb)->si_rwsem);
432
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)
436
437 static inline void si_noflush_read_lock(struct super_block *sb)
438 {
439         __si_read_lock(sb);
440         si_pid_set(sb);
441 }
442
443 static inline int si_noflush_read_trylock(struct super_block *sb)
444 {
445         int locked = __si_read_trylock(sb);
446         if (locked)
447                 si_pid_set(sb);
448         return locked;
449 }
450
451 static inline void si_noflush_write_lock(struct super_block *sb)
452 {
453         __si_write_lock(sb);
454         si_pid_set(sb);
455 }
456
457 static inline int si_noflush_write_trylock(struct super_block *sb)
458 {
459         int locked = __si_write_trylock(sb);
460         if (locked)
461                 si_pid_set(sb);
462         return locked;
463 }
464
465 #if 0 /* unused */
466 static inline int si_read_trylock(struct super_block *sb, int flags)
467 {
468         if (au_ftest_lock(flags, FLUSH))
469                 au_nwt_flush(&au_sbi(sb)->si_nowait);
470         return si_noflush_read_trylock(sb);
471 }
472 #endif
473
474 static inline void si_read_unlock(struct super_block *sb)
475 {
476         si_pid_clr(sb);
477         __si_read_unlock(sb);
478 }
479
480 #if 0 /* unused */
481 static inline int si_write_trylock(struct super_block *sb, int flags)
482 {
483         if (au_ftest_lock(flags, FLUSH))
484                 au_nwt_flush(&au_sbi(sb)->si_nowait);
485         return si_noflush_write_trylock(sb);
486 }
487 #endif
488
489 static inline void si_write_unlock(struct super_block *sb)
490 {
491         si_pid_clr(sb);
492         __si_write_unlock(sb);
493 }
494
495 #if 0 /* unused */
496 static inline void si_downgrade_lock(struct super_block *sb)
497 {
498         __si_downgrade_lock(sb);
499 }
500 #endif
501
502 /* ---------------------------------------------------------------------- */
503
504 static inline aufs_bindex_t au_sbend(struct super_block *sb)
505 {
506         SiMustAnyLock(sb);
507         return au_sbi(sb)->si_bend;
508 }
509
510 static inline unsigned int au_mntflags(struct super_block *sb)
511 {
512         SiMustAnyLock(sb);
513         return au_sbi(sb)->si_mntflags;
514 }
515
516 static inline unsigned int au_sigen(struct super_block *sb)
517 {
518         SiMustAnyLock(sb);
519         return au_sbi(sb)->si_generation;
520 }
521
522 static inline void au_ninodes_inc(struct super_block *sb)
523 {
524         atomic_long_inc(&au_sbi(sb)->si_ninodes);
525 }
526
527 static inline void au_ninodes_dec(struct super_block *sb)
528 {
529         AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_ninodes));
530         atomic_long_dec(&au_sbi(sb)->si_ninodes);
531 }
532
533 static inline void au_nfiles_inc(struct super_block *sb)
534 {
535         atomic_long_inc(&au_sbi(sb)->si_nfiles);
536 }
537
538 static inline void au_nfiles_dec(struct super_block *sb)
539 {
540         AuDebugOn(!atomic_long_read(&au_sbi(sb)->si_nfiles));
541         atomic_long_dec(&au_sbi(sb)->si_nfiles);
542 }
543
544 static inline struct au_branch *au_sbr(struct super_block *sb,
545                                        aufs_bindex_t bindex)
546 {
547         SiMustAnyLock(sb);
548         return au_sbi(sb)->si_branch[0 + bindex];
549 }
550
551 static inline void au_xino_brid_set(struct super_block *sb, aufs_bindex_t brid)
552 {
553         SiMustWriteLock(sb);
554         au_sbi(sb)->si_xino_brid = brid;
555 }
556
557 static inline aufs_bindex_t au_xino_brid(struct super_block *sb)
558 {
559         SiMustAnyLock(sb);
560         return au_sbi(sb)->si_xino_brid;
561 }
562
563 #endif /* __KERNEL__ */
564 #endif /* __AUFS_SUPER_H__ */