2 * Copyright (C) 2005-2012 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
23 #ifndef __AUFS_INODE_H__
24 #define __AUFS_INODE_H__
28 #include <linux/fsnotify.h>
34 #ifdef CONFIG_AUFS_HNOTIFY
35 #ifdef CONFIG_AUFS_HFSNOTIFY
36 /* never use fsnotify_add_vfsmount_mark() */
37 struct fsnotify_mark hn_mark;
40 struct inode *hn_aufs_inode; /* no get/put */
42 } ____cacheline_aligned_in_smp;
45 struct inode *hi_inode;
47 #ifdef CONFIG_AUFS_HNOTIFY
48 struct au_hnotify *hi_notify;
51 /* reference to the copied-up whiteout with get/put */
52 struct dentry *hi_whdentry;
57 atomic_t ii_generation;
58 struct super_block *ii_hsb1; /* no get/put */
60 struct au_rwsem ii_rwsem;
61 aufs_bindex_t ii_bstart, ii_bend;
63 struct au_hinode *ii_hinode;
64 struct au_vdir *ii_vdir;
68 struct au_iinfo iinfo;
69 struct inode vfs_inode;
70 } ____cacheline_aligned_in_smp;
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)
83 struct dentry *dentry;
85 unsigned char lsc_di, lsc_hi, flags;
89 struct dentry *parent;
90 struct au_hinode *hdir;
91 struct vfsmount *h_mnt;
94 /* ---------------------------------------------------------------------- */
96 static inline struct au_iinfo *au_ii(struct inode *inode)
98 struct au_iinfo *iinfo;
100 iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
101 if (iinfo->ii_hinode)
103 return NULL; /* debugging bad_inode case */
106 /* ---------------------------------------------------------------------- */
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);
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)
123 #ifdef CONFIG_AUFS_SHWH
124 return au_ino(sb, bindex, h_ino, d_type, ino);
131 extern struct inode_operations aufs_iop, aufs_symlink_iop, aufs_dir_iop;
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)
142 struct au_wr_dir_args {
143 aufs_bindex_t force_btgt;
146 int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
147 struct au_wr_dir_args *args);
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);
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);
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);
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);
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);
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)
197 #ifndef CONFIG_AUFS_HNOTIFY
199 #define AuHi_HNOTIFY 0
202 void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
203 struct inode *h_inode, unsigned int flags);
205 void au_update_iigen(struct inode *inode);
206 void au_update_ibrange(struct inode *inode, int do_put_zero);
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);
213 #ifdef CONFIG_PROC_FS
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);
221 AuStubVoid(au_plink_list, struct super_block *sb)
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);
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 */
245 /* ---------------------------------------------------------------------- */
247 /* lock subclass for iinfo */
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 */
254 AuLsc_II_PARENT3, /* copyup dirs */
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,
267 #define AuReadLockFunc(name, lsc) \
268 static inline void ii_read_lock_##name(struct inode *i) \
270 au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
273 #define AuWriteLockFunc(name, lsc) \
274 static inline void ii_write_lock_##name(struct inode *i) \
276 au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
279 #define AuRWLockFuncs(name, lsc) \
280 AuReadLockFunc(name, lsc) \
281 AuWriteLockFunc(name, lsc)
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);
291 #undef AuReadLockFunc
292 #undef AuWriteLockFunc
296 * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
298 AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
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)
304 /* ---------------------------------------------------------------------- */
306 static inline void au_icntnr_init(struct au_icntnr *c)
308 #ifdef CONFIG_AUFS_DEBUG
309 c->vfs_inode.i_mode = 0;
313 static inline unsigned int au_iigen(struct inode *inode)
315 return atomic_read(&au_ii(inode)->ii_generation);
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)
322 struct au_iinfo *iinfo;
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);
330 static inline void au_iigen_dec(struct inode *inode)
332 atomic_dec(&au_ii(inode)->ii_generation);
335 static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
340 if (unlikely(inode && au_iigen(inode) != sigen))
346 /* ---------------------------------------------------------------------- */
348 static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
349 aufs_bindex_t bindex)
351 IiMustAnyLock(inode);
352 return au_ii(inode)->ii_hinode[0 + bindex].hi_id;
355 static inline aufs_bindex_t au_ibstart(struct inode *inode)
357 IiMustAnyLock(inode);
358 return au_ii(inode)->ii_bstart;
361 static inline aufs_bindex_t au_ibend(struct inode *inode)
363 IiMustAnyLock(inode);
364 return au_ii(inode)->ii_bend;
367 static inline struct au_vdir *au_ivdir(struct inode *inode)
369 IiMustAnyLock(inode);
370 return au_ii(inode)->ii_vdir;
373 static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
375 IiMustAnyLock(inode);
376 return au_ii(inode)->ii_hinode[0 + bindex].hi_whdentry;
379 static inline void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex)
381 IiMustWriteLock(inode);
382 au_ii(inode)->ii_bstart = bindex;
385 static inline void au_set_ibend(struct inode *inode, aufs_bindex_t bindex)
387 IiMustWriteLock(inode);
388 au_ii(inode)->ii_bend = bindex;
391 static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
393 IiMustWriteLock(inode);
394 au_ii(inode)->ii_vdir = vdir;
397 static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
399 IiMustAnyLock(inode);
400 return au_ii(inode)->ii_hinode + bindex;
403 /* ---------------------------------------------------------------------- */
405 static inline struct dentry *au_pinned_parent(struct au_pin *pin)
412 static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
414 if (pin && pin->hdir)
415 return pin->hdir->hi_inode;
419 static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
426 static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
429 pin->dentry = dentry;
432 static inline void au_pin_set_parent_lflag(struct au_pin *pin,
437 au_fset_pin(pin->flags, DI_LOCKED);
439 au_fclr_pin(pin->flags, DI_LOCKED);
443 static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
447 pin->parent = dget(parent);
451 /* ---------------------------------------------------------------------- */
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);
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);
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);
482 extern const struct au_hnotify_op au_hnotify_op;
485 void au_hn_init(struct au_hinode *hinode)
487 hinode->hi_notify = NULL;
490 static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
492 return hinode->hi_notify;
497 int au_hn_alloc(struct au_hinode *hinode __maybe_unused,
498 struct inode *inode __maybe_unused)
503 static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
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 */
524 static inline void au_hn_suspend(struct au_hinode *hdir)
526 au_hn_ctl(hdir, /*do_set*/0);
529 static inline void au_hn_resume(struct au_hinode *hdir)
531 au_hn_ctl(hdir, /*do_set*/1);
534 static inline void au_hn_imtx_lock(struct au_hinode *hdir)
536 mutex_lock(&hdir->hi_inode->i_mutex);
540 static inline void au_hn_imtx_lock_nested(struct au_hinode *hdir,
541 unsigned int sc __maybe_unused)
543 mutex_lock_nested(&hdir->hi_inode->i_mutex, sc);
547 static inline void au_hn_imtx_unlock(struct au_hinode *hdir)
550 mutex_unlock(&hdir->hi_inode->i_mutex);
553 #endif /* __KERNEL__ */
554 #endif /* __AUFS_INODE_H__ */