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