UBUNTU: ubuntu: AUFS -- update to aufs2.1-36-UNRELEASED-20101103
[linux-flexiantxendom0-natty.git] / ubuntu / aufs / inode.h
1 /*
2  * Copyright (C) 2005-2010 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/fs.h>
29 #include <linux/fsnotify.h>
30 #include <linux/aufs_type.h>
31 #include "rwsem.h"
32
33 struct vfsmount;
34
35 struct au_hnotify {
36 #ifdef CONFIG_AUFS_HNOTIFY
37 #ifdef CONFIG_AUFS_HFSNOTIFY
38         struct fsnotify_mark            hn_mark;
39 #else
40         struct inotify_watch            hn_watch;
41 #endif
42         struct inode                            *hn_aufs_inode; /* no get/put */
43 #endif
44 } ____cacheline_aligned_in_smp;
45
46 struct au_hinode {
47         struct inode            *hi_inode;
48         aufs_bindex_t           hi_id;
49 #ifdef CONFIG_AUFS_HNOTIFY
50         struct au_hnotify       *hi_notify;
51 #endif
52
53         /* reference to the copied-up whiteout with get/put */
54         struct dentry           *hi_whdentry;
55 };
56
57 struct au_vdir;
58 struct au_iinfo {
59         atomic_t                ii_generation;
60         struct super_block      *ii_hsb1;       /* no get/put */
61
62         struct au_rwsem         ii_rwsem;
63         aufs_bindex_t           ii_bstart, ii_bend;
64         __u32                   ii_higen;
65         struct au_hinode        *ii_hinode;
66         struct au_vdir          *ii_vdir;
67 };
68
69 struct au_icntnr {
70         struct au_iinfo iinfo;
71         struct inode vfs_inode;
72 } ____cacheline_aligned_in_smp;
73
74 /* au_pin flags */
75 #define AuPin_DI_LOCKED         1
76 #define AuPin_MNT_WRITE         (1 << 1)
77 #define au_ftest_pin(flags, name)       ((flags) & AuPin_##name)
78 #define au_fset_pin(flags, name)        { (flags) |= AuPin_##name; }
79 #define au_fclr_pin(flags, name)        { (flags) &= ~AuPin_##name; }
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, int do_attr);
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)      { (flags) |= AuWrDir_##name; }
138 #define au_fclr_wrdir(flags, name)      { (flags) &= ~AuWrDir_##name; }
139
140 struct au_wr_dir_args {
141         aufs_bindex_t force_btgt;
142         unsigned char flags;
143 };
144 int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
145               struct au_wr_dir_args *args);
146
147 struct dentry *au_pinned_h_parent(struct au_pin *pin);
148 void au_pin_init(struct au_pin *pin, struct dentry *dentry,
149                  aufs_bindex_t bindex, int lsc_di, int lsc_hi,
150                  unsigned int udba, unsigned char flags);
151 int au_pin(struct au_pin *pin, struct dentry *dentry, aufs_bindex_t bindex,
152            unsigned int udba, unsigned char flags) __must_check;
153 int au_do_pin(struct au_pin *pin) __must_check;
154 void au_unpin(struct au_pin *pin);
155
156 /* i_op_add.c */
157 int au_may_add(struct dentry *dentry, aufs_bindex_t bindex,
158                struct dentry *h_parent, int isdir);
159 int aufs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev);
160 int aufs_symlink(struct inode *dir, struct dentry *dentry, const char *symname);
161 int aufs_create(struct inode *dir, struct dentry *dentry, int mode,
162                 struct nameidata *nd);
163 int aufs_link(struct dentry *src_dentry, struct inode *dir,
164               struct dentry *dentry);
165 int aufs_mkdir(struct inode *dir, struct dentry *dentry, int mode);
166
167 /* i_op_del.c */
168 int au_wr_dir_need_wh(struct dentry *dentry, int isdir, aufs_bindex_t *bcpup);
169 int au_may_del(struct dentry *dentry, aufs_bindex_t bindex,
170                struct dentry *h_parent, int isdir);
171 int aufs_unlink(struct inode *dir, struct dentry *dentry);
172 int aufs_rmdir(struct inode *dir, struct dentry *dentry);
173
174 /* i_op_ren.c */
175 int au_wbr(struct dentry *dentry, aufs_bindex_t btgt);
176 int aufs_rename(struct inode *src_dir, struct dentry *src_dentry,
177                 struct inode *dir, struct dentry *dentry);
178
179 /* iinfo.c */
180 struct inode *au_h_iptr(struct inode *inode, aufs_bindex_t bindex);
181 void au_hiput(struct au_hinode *hinode);
182 void au_set_hi_wh(struct inode *inode, aufs_bindex_t bindex,
183                   struct dentry *h_wh);
184 unsigned int au_hi_flags(struct inode *inode, int isdir);
185
186 /* hinode flags */
187 #define AuHi_XINO       1
188 #define AuHi_HNOTIFY    (1 << 1)
189 #define au_ftest_hi(flags, name)        ((flags) & AuHi_##name)
190 #define au_fset_hi(flags, name)         { (flags) |= AuHi_##name; }
191 #define au_fclr_hi(flags, name)         { (flags) &= ~AuHi_##name; }
192
193 #ifndef CONFIG_AUFS_HNOTIFY
194 #undef AuHi_HNOTIFY
195 #define AuHi_HNOTIFY    0
196 #endif
197
198 void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
199                    struct inode *h_inode, unsigned int flags);
200
201 void au_update_iigen(struct inode *inode);
202 void au_update_ibrange(struct inode *inode, int do_put_zero);
203
204 void au_icntnr_init_once(void *_c);
205 int au_iinfo_init(struct inode *inode);
206 void au_iinfo_fin(struct inode *inode);
207 int au_ii_realloc(struct au_iinfo *iinfo, int nbr);
208
209 #ifdef CONFIG_PROC_FS
210 /* plink.c */
211 int au_plink_maint(struct super_block *sb, int flags);
212 void au_plink_maint_leave(struct au_sbinfo *sbinfo);
213 int au_plink_maint_enter(struct super_block *sb);
214 #ifdef CONFIG_AUFS_DEBUG
215 void au_plink_list(struct super_block *sb);
216 #else
217 AuStubVoid(au_plink_list, struct super_block *sb)
218 #endif
219 int au_plink_test(struct inode *inode);
220 struct dentry *au_plink_lkup(struct inode *inode, aufs_bindex_t bindex);
221 void au_plink_append(struct inode *inode, aufs_bindex_t bindex,
222                      struct dentry *h_dentry);
223 void au_plink_put(struct super_block *sb, int verbose);
224 void au_plink_clean(struct super_block *sb, int verbose);
225 void au_plink_half_refresh(struct super_block *sb, aufs_bindex_t br_id);
226 #else
227 AuStubInt0(au_plink_maint, struct super_block *sb, int flags);
228 AuStubVoid(au_plink_maint_leave, struct au_sbinfo *sbinfo);
229 AuStubInt0(au_plink_maint_enter, struct super_block *sb);
230 AuStubVoid(au_plink_list, struct super_block *sb);
231 AuStubInt0(au_plink_test, struct inode *inode);
232 AuStub(struct dentry *, au_plink_lkup, return NULL,
233        struct inode *inode, aufs_bindex_t bindex);
234 AuStubVoid(au_plink_append, struct inode *inode, aufs_bindex_t bindex,
235            struct dentry *h_dentry);
236 AuStubVoid(au_plink_put, struct super_block *sb, int verbose);
237 AuStubVoid(au_plink_clean, struct super_block *sb, int verbose);
238 AuStubVoid(au_plink_half_refresh, struct super_block *sb, aufs_bindex_t br_id);
239 #endif /* CONFIG_PROC_FS */
240
241 /* ---------------------------------------------------------------------- */
242
243 /* lock subclass for iinfo */
244 enum {
245         AuLsc_II_CHILD,         /* child first */
246         AuLsc_II_CHILD2,        /* rename(2), link(2), and cpup at hnotify */
247         AuLsc_II_CHILD3,        /* copyup dirs */
248         AuLsc_II_PARENT,        /* see AuLsc_I_PARENT in vfsub.h */
249         AuLsc_II_PARENT2,
250         AuLsc_II_PARENT3,       /* copyup dirs */
251         AuLsc_II_NEW_CHILD
252 };
253
254 /*
255  * ii_read_lock_child, ii_write_lock_child,
256  * ii_read_lock_child2, ii_write_lock_child2,
257  * ii_read_lock_child3, ii_write_lock_child3,
258  * ii_read_lock_parent, ii_write_lock_parent,
259  * ii_read_lock_parent2, ii_write_lock_parent2,
260  * ii_read_lock_parent3, ii_write_lock_parent3,
261  * ii_read_lock_new_child, ii_write_lock_new_child,
262  */
263 #define AuReadLockFunc(name, lsc) \
264 static inline void ii_read_lock_##name(struct inode *i) \
265 { \
266         au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
267 }
268
269 #define AuWriteLockFunc(name, lsc) \
270 static inline void ii_write_lock_##name(struct inode *i) \
271 { \
272         au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
273 }
274
275 #define AuRWLockFuncs(name, lsc) \
276         AuReadLockFunc(name, lsc) \
277         AuWriteLockFunc(name, lsc)
278
279 AuRWLockFuncs(child, CHILD);
280 AuRWLockFuncs(child2, CHILD2);
281 AuRWLockFuncs(child3, CHILD3);
282 AuRWLockFuncs(parent, PARENT);
283 AuRWLockFuncs(parent2, PARENT2);
284 AuRWLockFuncs(parent3, PARENT3);
285 AuRWLockFuncs(new_child, NEW_CHILD);
286
287 #undef AuReadLockFunc
288 #undef AuWriteLockFunc
289 #undef AuRWLockFuncs
290
291 /*
292  * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
293  */
294 AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
295
296 #define IiMustNoWaiters(i)      AuRwMustNoWaiters(&au_ii(i)->ii_rwsem)
297 #define IiMustAnyLock(i)        AuRwMustAnyLock(&au_ii(i)->ii_rwsem)
298 #define IiMustWriteLock(i)      AuRwMustWriteLock(&au_ii(i)->ii_rwsem)
299
300 /* ---------------------------------------------------------------------- */
301
302 static inline unsigned int au_iigen(struct inode *inode)
303 {
304         return atomic_read(&au_ii(inode)->ii_generation);
305 }
306
307 /* tiny test for inode number */
308 /* tmpfs generation is too rough */
309 static inline int au_test_higen(struct inode *inode, struct inode *h_inode)
310 {
311         struct au_iinfo *iinfo;
312
313         iinfo = au_ii(inode);
314         AuRwMustAnyLock(&iinfo->ii_rwsem);
315         return !(iinfo->ii_hsb1 == h_inode->i_sb
316                  && iinfo->ii_higen == h_inode->i_generation);
317 }
318
319 static inline void au_iigen_dec(struct inode *inode)
320 {
321 #ifdef CONFIG_AUFS_HNOTIFY
322         atomic_dec(&au_ii(inode)->ii_generation);
323 #endif
324 }
325
326 /* ---------------------------------------------------------------------- */
327
328 static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
329                                         aufs_bindex_t bindex)
330 {
331         IiMustAnyLock(inode);
332         return au_ii(inode)->ii_hinode[0 + bindex].hi_id;
333 }
334
335 static inline aufs_bindex_t au_ibstart(struct inode *inode)
336 {
337         IiMustAnyLock(inode);
338         return au_ii(inode)->ii_bstart;
339 }
340
341 static inline aufs_bindex_t au_ibend(struct inode *inode)
342 {
343         IiMustAnyLock(inode);
344         return au_ii(inode)->ii_bend;
345 }
346
347 static inline struct au_vdir *au_ivdir(struct inode *inode)
348 {
349         IiMustAnyLock(inode);
350         return au_ii(inode)->ii_vdir;
351 }
352
353 static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
354 {
355         IiMustAnyLock(inode);
356         return au_ii(inode)->ii_hinode[0 + bindex].hi_whdentry;
357 }
358
359 static inline void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex)
360 {
361         IiMustWriteLock(inode);
362         au_ii(inode)->ii_bstart = bindex;
363 }
364
365 static inline void au_set_ibend(struct inode *inode, aufs_bindex_t bindex)
366 {
367         IiMustWriteLock(inode);
368         au_ii(inode)->ii_bend = bindex;
369 }
370
371 static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
372 {
373         IiMustWriteLock(inode);
374         au_ii(inode)->ii_vdir = vdir;
375 }
376
377 static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
378 {
379         IiMustAnyLock(inode);
380         return au_ii(inode)->ii_hinode + bindex;
381 }
382
383 /* ---------------------------------------------------------------------- */
384
385 static inline struct dentry *au_pinned_parent(struct au_pin *pin)
386 {
387         if (pin)
388                 return pin->parent;
389         return NULL;
390 }
391
392 static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
393 {
394         if (pin && pin->hdir)
395                 return pin->hdir->hi_inode;
396         return NULL;
397 }
398
399 static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
400 {
401         if (pin)
402                 return pin->hdir;
403         return NULL;
404 }
405
406 static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
407 {
408         if (pin)
409                 pin->dentry = dentry;
410 }
411
412 static inline void au_pin_set_parent_lflag(struct au_pin *pin,
413                                            unsigned char lflag)
414 {
415         if (pin) {
416                 /* dirty macros require brackets */
417                 if (lflag) {
418                         au_fset_pin(pin->flags, DI_LOCKED);
419                 } else {
420                         au_fclr_pin(pin->flags, DI_LOCKED);
421                 }
422         }
423 }
424
425 static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
426 {
427         if (pin) {
428                 dput(pin->parent);
429                 pin->parent = dget(parent);
430         }
431 }
432
433 /* ---------------------------------------------------------------------- */
434
435 #ifdef CONFIG_AUFS_HNOTIFY
436 struct au_hnotify_op {
437         void (*ctl)(struct au_hinode *hinode, int do_set);
438         int (*alloc)(struct au_hnotify *hn, struct inode *h_inode);
439         void (*free)(struct au_hnotify *hn);
440
441         void (*fin)(void);
442         int (*init)(void);
443 };
444
445 /* hnotify.c */
446 int au_hn_alloc(struct au_hinode *hinode, struct inode *inode,
447                 struct inode *h_inode);
448 void au_hn_free(struct au_hinode *hinode);
449 void au_hn_ctl(struct au_hinode *hinode, int do_set);
450 void au_hn_reset(struct inode *inode, unsigned int flags);
451 int au_hnotify(struct inode *h_dir, struct au_hnotify *hnotify, u32 mask,
452                struct qstr *h_child_qstr, struct inode *h_child_inode);
453 int __init au_hnotify_init(void);
454 void au_hnotify_fin(void);
455
456 /* hinotify.c */
457 extern const struct au_hnotify_op au_hnotify_op;
458
459 static inline
460 void au_hn_init(struct au_hinode *hinode)
461 {
462         hinode->hi_notify = NULL;
463 }
464
465 #else
466 static inline
467 int au_hn_alloc(struct au_hinode *hinode __maybe_unused,
468                 struct inode *inode __maybe_unused,
469                 struct inode *h_inode __maybe_unused)
470 {
471         return -EOPNOTSUPP;
472 }
473
474 AuStubVoid(au_hn_free, struct au_hinode *hinode __maybe_unused)
475 AuStubVoid(au_hn_ctl, struct au_hinode *hinode __maybe_unused,
476            int do_set __maybe_unused)
477 AuStubVoid(au_hn_reset, struct inode *inode __maybe_unused,
478            unsigned int flags __maybe_unused)
479 AuStubInt0(__init au_hnotify_init, void)
480 AuStubVoid(au_hnotify_fin, void)
481 AuStubVoid(au_hn_init, struct au_hinode *hinode __maybe_unused)
482 #endif /* CONFIG_AUFS_HNOTIFY */
483
484 static inline void au_hn_suspend(struct au_hinode *hdir)
485 {
486         au_hn_ctl(hdir, /*do_set*/0);
487 }
488
489 static inline void au_hn_resume(struct au_hinode *hdir)
490 {
491         au_hn_ctl(hdir, /*do_set*/1);
492 }
493
494 static inline void au_hn_imtx_lock(struct au_hinode *hdir)
495 {
496         mutex_lock(&hdir->hi_inode->i_mutex);
497         au_hn_suspend(hdir);
498 }
499
500 static inline void au_hn_imtx_lock_nested(struct au_hinode *hdir,
501                                           unsigned int sc __maybe_unused)
502 {
503         mutex_lock_nested(&hdir->hi_inode->i_mutex, sc);
504         au_hn_suspend(hdir);
505 }
506
507 static inline void au_hn_imtx_unlock(struct au_hinode *hdir)
508 {
509         au_hn_resume(hdir);
510         mutex_unlock(&hdir->hi_inode->i_mutex);
511 }
512
513 #endif /* __KERNEL__ */
514 #endif /* __AUFS_INODE_H__ */