pandora: defconfig: update
[pandora-kernel.git] / fs / aufs / inode.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  * inode operations
21  */
22
23 #ifndef __AUFS_INODE_H__
24 #define __AUFS_INODE_H__
25
26 #ifdef __KERNEL__
27
28 #include <linux/fsnotify.h>
29 #include "rwsem.h"
30
31 struct vfsmount;
32
33 struct au_hnotify {
34 #ifdef CONFIG_AUFS_HNOTIFY
35 #ifdef CONFIG_AUFS_HFSNOTIFY
36         /* never use fsnotify_add_vfsmount_mark() */
37         struct fsnotify_mark            hn_mark;
38 #endif
39         struct inode                    *hn_aufs_inode; /* no get/put */
40 #endif
41 } ____cacheline_aligned_in_smp;
42
43 struct au_hinode {
44         struct inode            *hi_inode;
45         aufs_bindex_t           hi_id;
46 #ifdef CONFIG_AUFS_HNOTIFY
47         struct au_hnotify       *hi_notify;
48 #endif
49
50         /* reference to the copied-up whiteout with get/put */
51         struct dentry           *hi_whdentry;
52 };
53
54 /* ig_flags */
55 #define AuIG_HALF_REFRESHED             1
56 #define au_ig_ftest(flags, name)        ((flags) & AuIG_##name)
57 #define au_ig_fset(flags, name) \
58         do { (flags) |= AuIG_##name; } while (0)
59 #define au_ig_fclr(flags, name) \
60         do { (flags) &= ~AuIG_##name; } while (0)
61
62 struct au_iigen {
63         __u32           ig_generation, ig_flags;
64 };
65
66 struct au_vdir;
67 struct au_iinfo {
68         spinlock_t              ii_genspin;
69         struct au_iigen         ii_generation;
70         struct super_block      *ii_hsb1;       /* no get/put */
71
72         struct au_rwsem         ii_rwsem;
73         aufs_bindex_t           ii_bstart, ii_bend;
74         __u32                   ii_higen;
75         struct au_hinode        *ii_hinode;
76         struct au_vdir          *ii_vdir;
77 };
78
79 struct au_icntnr {
80         struct au_iinfo iinfo;
81         struct inode vfs_inode;
82 } ____cacheline_aligned_in_smp;
83
84 /* au_pin flags */
85 #define AuPin_DI_LOCKED         1
86 #define AuPin_MNT_WRITE         (1 << 1)
87 #define au_ftest_pin(flags, name)       ((flags) & AuPin_##name)
88 #define au_fset_pin(flags, name) \
89         do { (flags) |= AuPin_##name; } while (0)
90 #define au_fclr_pin(flags, name) \
91         do { (flags) &= ~AuPin_##name; } while (0)
92
93 struct au_pin {
94         /* input */
95         struct dentry *dentry;
96         unsigned int udba;
97         unsigned char lsc_di, lsc_hi, flags;
98         aufs_bindex_t bindex;
99
100         /* output */
101         struct dentry *parent;
102         struct au_hinode *hdir;
103         struct vfsmount *h_mnt;
104
105         /* temporary unlock/relock for copyup */
106         struct dentry *h_dentry, *h_parent;
107         struct au_branch *br;
108         struct task_struct *task;
109 };
110
111 void au_pin_hdir_unlock(struct au_pin *p);
112 int au_pin_hdir_relock(struct au_pin *p);
113 void au_pin_hdir_set_owner(struct au_pin *p, struct task_struct *task);
114 void au_pin_hdir_acquire_nest(struct au_pin *p);
115 void au_pin_hdir_release(struct au_pin *p);
116
117 /* ---------------------------------------------------------------------- */
118
119 static inline struct au_iinfo *au_ii(struct inode *inode)
120 {
121         struct au_iinfo *iinfo;
122
123         iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
124         if (iinfo->ii_hinode)
125                 return iinfo;
126         return NULL; /* debugging bad_inode case */
127 }
128
129 /* ---------------------------------------------------------------------- */
130
131 /* inode.c */
132 struct inode *au_igrab(struct inode *inode);
133 int au_refresh_hinode_self(struct inode *inode);
134 int au_refresh_hinode(struct inode *inode, struct dentry *dentry);
135 int au_ino(struct super_block *sb, aufs_bindex_t bindex, ino_t h_ino,
136            unsigned int d_type, ino_t *ino);
137 struct inode *au_new_inode(struct dentry *dentry, int must_new);
138 int au_test_ro(struct super_block *sb, aufs_bindex_t bindex,
139                struct inode *inode);
140 int au_test_h_perm(struct inode *h_inode, int mask);
141 int au_test_h_perm_sio(struct inode *h_inode, int mask);
142
143 static inline int au_wh_ino(struct super_block *sb, aufs_bindex_t bindex,
144                             ino_t h_ino, unsigned int d_type, ino_t *ino)
145 {
146 #ifdef CONFIG_AUFS_SHWH
147         return au_ino(sb, bindex, h_ino, d_type, ino);
148 #else
149         return 0;
150 #endif
151 }
152
153 /* i_op.c */
154 extern struct inode_operations aufs_iop, aufs_symlink_iop, aufs_dir_iop;
155
156 /* au_wr_dir flags */
157 #define AuWrDir_ADD_ENTRY       1
158 #define AuWrDir_TMP_WHENTRY     (1 << 1)
159 #define AuWrDir_ISDIR           (1 << 2)
160 #define au_ftest_wrdir(flags, name)     ((flags) & AuWrDir_##name)
161 #define au_fset_wrdir(flags, name) \
162         do { (flags) |= AuWrDir_##name; } while (0)
163 #define au_fclr_wrdir(flags, name) \
164         do { (flags) &= ~AuWrDir_##name; } while (0)
165
166 struct au_wr_dir_args {
167         aufs_bindex_t force_btgt;
168         unsigned char flags;
169 };
170 int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
171               struct au_wr_dir_args *args);
172
173 struct dentry *au_pinned_h_parent(struct au_pin *pin);
174 void au_pin_init(struct au_pin *pin, struct dentry *dentry,
175                  aufs_bindex_t bindex, int lsc_di, int lsc_hi,
176                  unsigned int udba, unsigned char flags);
177 int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
178            unsigned int udba, unsigned char flags) __must_check;
179 int au_do_pin(struct au_pin *pin) __must_check;
180 void au_unpin(struct au_pin *pin);
181
182 /* i_op_add.c */
183 int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
184                struct dentry *h_parent, int isdir);
185 int aufs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev);
186 int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
187 int aufs_create(struct inode *dir, struct dentry *dentry, int mode,
188                 struct nameidata *nd);
189 int aufs_link(struct dentry *src_dentry, struct inode *dir,
190               struct dentry *dentry);
191 int aufs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
192
193 /* i_op_del.c */
194 int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
195 int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
196                struct dentry *h_parent, int isdir);
197 int aufs_unlink(struct inode *dir, struct dentry *dentry);
198 int aufs_rmdir(struct inode *dir, struct dentry *dentry);
199
200 /* i_op_ren.c */
201 int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
202 int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
203                 struct inode *dir, struct dentry *dentry);
204
205 /* iinfo.c */
206 struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
207 void au_hiput(struct au_hinode *hinode);
208 void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
209                   struct dentry *h_wh);
210 unsigned int au_hi_flags(struct inode *inode, int isdir);
211
212 /* hinode flags */
213 #define AuHi_XINO       1
214 #define AuHi_HNOTIFY    (1 << 1)
215 #define au_ftest_hi(flags, name)        ((flags) & AuHi_##name)
216 #define au_fset_hi(flags, name) \
217         do { (flags) |= AuHi_##name; } while (0)
218 #define au_fclr_hi(flags, name) \
219         do { (flags) &= ~AuHi_##name; } while (0)
220
221 #ifndef CONFIG_AUFS_HNOTIFY
222 #undef AuHi_HNOTIFY
223 #define AuHi_HNOTIFY    0
224 #endif
225
226 void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
227                    struct inode *h_inode, unsigned int flags);
228
229 void au_update_iigen(struct inode *inode, int half);
230 void au_update_ibrange(struct inode *inode, int do_put_zero);
231
232 void au_icntnr_init_once(void *_c);
233 int au_iinfo_init(struct inode *inode);
234 void au_iinfo_fin(struct inode *inode);
235 int au_ii_realloc(struct au_iinfo *iinfo, int nbr);
236
237 #ifdef CONFIG_PROC_FS
238 /* plink.c */
239 int au_plink_maint(struct super_block *sb, int flags);
240 void au_plink_maint_leave(struct au_sbinfo *sbinfo);
241 int au_plink_maint_enter(struct super_block *sb);
242 #ifdef CONFIG_AUFS_DEBUG
243 void au_plink_list(struct super_block *sb);
244 #else
245 AuStubVoid(au_plink_list, struct super_block *sb)
246 #endif
247 int au_plink_test(struct inode *inode);
248 struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
249 void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
250                      struct dentry *h_dentry);
251 void au_plink_put(struct super_block *sb, int verbose);
252 void au_plink_clean(struct super_block *sb, int verbose);
253 void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
254 #else
255 AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
256 AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
257 AuStubInt0(au_plink_maint_enter, struct super_block *sb);
258 AuStubVoid(au_plink_list, struct super_block *sb);
259 AuStubInt0(au_plink_test, struct inode *inode);
260 AuStub(struct dentry *, au_plink_lkup, return NULL,
261        struct inode *inode, aufs_bindex_t bindex);
262 AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
263            struct dentry *h_dentry);
264 AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
265 AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
266 AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
267 #endif /* CONFIG_PROC_FS */
268
269 /* ---------------------------------------------------------------------- */
270
271 /* lock subclass for iinfo */
272 enum {
273         AuLsc_II_CHILD,         /* child first */
274         AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
275         AuLsc_II_CHILD3,        /* copyup dirs */
276         AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
277         AuLsc_II_PARENT2,
278         AuLsc_II_PARENT3,       /* copyup dirs */
279         AuLsc_II_NEW_CHILD
280 };
281
282 /*
283  * ii_read_lock_child, ii_write_lock_child,
284  * ii_read_lock_child2, ii_write_lock_child2,
285  * ii_read_lock_child3, ii_write_lock_child3,
286  * ii_read_lock_parent, ii_write_lock_parent,
287  * ii_read_lock_parent2, ii_write_lock_parent2,
288  * ii_read_lock_parent3, ii_write_lock_parent3,
289  * ii_read_lock_new_child, ii_write_lock_new_child,
290  */
291 #define AuReadLockFunc(name, lsc) \
292 static inline void ii_read_lock_##name(struct inode *i) \
293 { \
294         au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
295 }
296
297 #define AuWriteLockFunc(name, lsc) \
298 static inline void ii_write_lock_##name(struct inode *i) \
299 { \
300         au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
301 }
302
303 #define AuRWLockFuncs(name, lsc) \
304         AuReadLockFunc(name, lsc) \
305         AuWriteLockFunc(name, lsc)
306
307 AuRWLockFuncs(child, CHILD);
308 AuRWLockFuncs(child2, CHILD2);
309 AuRWLockFuncs(child3, CHILD3);
310 AuRWLockFuncs(parent, PARENT);
311 AuRWLockFuncs(parent2, PARENT2);
312 AuRWLockFuncs(parent3, PARENT3);
313 AuRWLockFuncs(new_child, NEW_CHILD);
314
315 #undef AuReadLockFunc
316 #undef AuWriteLockFunc
317 #undef AuRWLockFuncs
318
319 /*
320  * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
321  */
322 AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
323
324 #define IiMustNoWaiters(i)      AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
325 #define IiMustAnyLock(i)        AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
326 #define IiMustWriteLock(i)      AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
327
328 /* ---------------------------------------------------------------------- */
329
330 static inline void au_icntnr_init(struct au_icntnr *c)
331 {
332 #ifdef CONFIG_AUFS_DEBUG
333         c->vfs_inode.i_mode = 0;
334 #endif
335 }
336
337 static inline unsigned int au_iigen(struct inode *inode, struct au_iigen *iigen)
338 {
339         unsigned int gen;
340         struct au_iinfo *iinfo;
341
342         iinfo = au_ii(inode);
343         spin_lock(&iinfo->ii_genspin);
344         if (iigen)
345                 *iigen = iinfo->ii_generation;
346         gen = iinfo->ii_generation.ig_generation;
347         spin_unlock(&iinfo->ii_genspin);
348
349         return gen;
350 }
351
352 /* tiny test for inode number */
353 /* tmpfs generation is too rough */
354 static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
355 {
356         struct au_iinfo *iinfo;
357
358         iinfo = au_ii(inode);
359         AuRwMustAnyLock(&iinfo->ii_rwsem);
360         return !(iinfo->ii_hsb1 == h_inode->i_sb
361                  && iinfo->ii_higen == h_inode->i_generation);
362 }
363
364 static inline void au_iigen_dec(struct inode *inode)
365 {
366         struct au_iinfo *iinfo;
367
368         iinfo = au_ii(inode);
369         spin_lock(&iinfo->ii_genspin);
370         iinfo->ii_generation.ig_generation--;
371         spin_unlock(&iinfo->ii_genspin);
372 }
373
374 static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
375 {
376         int err;
377
378         err = 0;
379         if (unlikely(inode && au_iigen(inode, NULL) != sigen))
380                 err = -EIO;
381
382         return err;
383 }
384
385 /* ---------------------------------------------------------------------- */
386
387 static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
388                                         aufs_bindex_t bindex)
389 {
390         IiMustAnyLock(inode);
391         return au_ii(inode)->ii_hinode[0 + bindex].hi_id;
392 }
393
394 static inline aufs_bindex_t au_ibstart(struct inode *inode)
395 {
396         IiMustAnyLock(inode);
397         return au_ii(inode)->ii_bstart;
398 }
399
400 static inline aufs_bindex_t au_ibend(struct inode *inode)
401 {
402         IiMustAnyLock(inode);
403         return au_ii(inode)->ii_bend;
404 }
405
406 static inline struct au_vdir *au_ivdir(struct inode *inode)
407 {
408         IiMustAnyLock(inode);
409         return au_ii(inode)->ii_vdir;
410 }
411
412 static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
413 {
414         IiMustAnyLock(inode);
415         return au_ii(inode)->ii_hinode[0 + bindex].hi_whdentry;
416 }
417
418 static inline void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex)
419 {
420         IiMustWriteLock(inode);
421         au_ii(inode)->ii_bstart = bindex;
422 }
423
424 static inline void au_set_ibend(struct inode *inode, aufs_bindex_t bindex)
425 {
426         IiMustWriteLock(inode);
427         au_ii(inode)->ii_bend = bindex;
428 }
429
430 static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
431 {
432         IiMustWriteLock(inode);
433         au_ii(inode)->ii_vdir = vdir;
434 }
435
436 static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
437 {
438         IiMustAnyLock(inode);
439         return au_ii(inode)->ii_hinode + bindex;
440 }
441
442 /* ---------------------------------------------------------------------- */
443
444 static inline struct dentry *au_pinned_parent(struct au_pin *pin)
445 {
446         if (pin)
447                 return pin->parent;
448         return NULL;
449 }
450
451 static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
452 {
453         if (pin && pin->hdir)
454                 return pin->hdir->hi_inode;
455         return NULL;
456 }
457
458 static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
459 {
460         if (pin)
461                 return pin->hdir;
462         return NULL;
463 }
464
465 static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
466 {
467         if (pin)
468                 pin->dentry = dentry;
469 }
470
471 static inline void au_pin_set_parent_lflag(struct au_pin *pin,
472                                            unsigned char lflag)
473 {
474         if (pin) {
475                 if (lflag)
476                         au_fset_pin(pin->flags, DI_LOCKED);
477                 else
478                         au_fclr_pin(pin->flags, DI_LOCKED);
479         }
480 }
481
482 static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
483 {
484         if (pin) {
485                 dput(pin->parent);
486                 pin->parent = dget(parent);
487         }
488 }
489
490 /* ---------------------------------------------------------------------- */
491
492 struct au_branch;
493 #ifdef CONFIG_AUFS_HNOTIFY
494 struct au_hnotify_op {
495         void (*ctl)(struct au_hinode *hinode, int do_set);
496         int (*alloc)(struct au_hinode *hinode);
497
498         /*
499          * if it returns true, the the caller should free hinode->hi_notify,
500          * otherwise ->free() frees it.
501          */
502         int (*free)(struct au_hinode *hinode,
503                     struct au_hnotify *hn) __must_check;
504
505         void (*fin)(void);
506         int (*init)(void);
507
508         int (*reset_br)(unsigned int udba, struct au_branch *br, int perm);
509         void (*fin_br)(struct au_branch *br);
510         int (*init_br)(struct au_branch *br, int perm);
511 };
512
513 /* hnotify.c */
514 int au_hn_alloc(struct au_hinode *hinode, struct inode *inode);
515 void au_hn_free(struct au_hinode *hinode);
516 void au_hn_ctl(struct au_hinode *hinode, int do_set);
517 void au_hn_reset(struct inode *inode, unsigned int flags);
518 int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
519                struct qstr *h_child_qstr, struct inode *h_child_inode);
520 int au_hnotify_reset_br(unsigned int udba, struct au_branch *br, int perm);
521 int au_hnotify_init_br(struct au_branch *br, int perm);
522 void au_hnotify_fin_br(struct au_branch *br);
523 int __init au_hnotify_init(void);
524 void au_hnotify_fin(void);
525
526 /* hfsnotify.c */
527 extern const struct au_hnotify_op au_hnotify_op;
528
529 static inline
530 void au_hn_init(struct au_hinode *hinode)
531 {
532         hinode->hi_notify = NULL;
533 }
534
535 static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
536 {
537         return hinode->hi_notify;
538 }
539
540 #else
541 static inline
542 int au_hn_alloc(struct au_hinode *hinode __maybe_unused,
543                 struct inode *inode __maybe_unused)
544 {
545         return -EOPNOTSUPP;
546 }
547
548 static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
549 {
550         return NULL;
551 }
552
553 AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
554 AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
555            int do_set __maybe_unused)
556 AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
557            unsigned int flags __maybe_unused)
558 AuStubInt0(au_hnotify_reset_br, unsigned int udba __maybe_unused,
559            struct au_branch *br __maybe_unused,
560            int perm __maybe_unused)
561 AuStubInt0(au_hnotify_init_br, struct au_branch *br __maybe_unused,
562            int perm __maybe_unused)
563 AuStubVoid(au_hnotify_fin_br, struct au_branch *br __maybe_unused)
564 AuStubInt0(__init au_hnotify_init, void)
565 AuStubVoid(au_hnotify_fin, void)
566 AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
567 #endif /* CONFIG_AUFS_HNOTIFY */
568
569 static inline void au_hn_suspend(struct au_hinode *hdir)
570 {
571         au_hn_ctl(hdir, /*do_set*/0);
572 }
573
574 static inline void au_hn_resume(struct au_hinode *hdir)
575 {
576         au_hn_ctl(hdir, /*do_set*/1);
577 }
578
579 static inline void au_hn_imtx_lock(struct au_hinode *hdir)
580 {
581         mutex_lock(&hdir->hi_inode->i_mutex);
582         au_hn_suspend(hdir);
583 }
584
585 static inline void au_hn_imtx_lock_nested(struct au_hinode *hdir,
586                                           unsigned int sc __maybe_unused)
587 {
588         mutex_lock_nested(&hdir->hi_inode->i_mutex, sc);
589         au_hn_suspend(hdir);
590 }
591
592 static inline void au_hn_imtx_unlock(struct au_hinode *hdir)
593 {
594         au_hn_resume(hdir);
595         mutex_unlock(&hdir->hi_inode->i_mutex);
596 }
597
598 #endif /* __KERNEL__ */
599 #endif /* __AUFS_INODE_H__ */