2 * Copyright (C) 2005-2013 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;
39 struct inode *hn_aufs_inode; /* no get/put */
41 } ____cacheline_aligned_in_smp;
44 struct inode *hi_inode;
46 #ifdef CONFIG_AUFS_HNOTIFY
47 struct au_hnotify *hi_notify;
50 /* reference to the copied-up whiteout with get/put */
51 struct dentry *hi_whdentry;
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)
63 __u32 ig_generation, ig_flags;
68 spinlock_t ii_genspin;
69 struct au_iigen ii_generation;
70 struct super_block *ii_hsb1; /* no get/put */
72 struct au_rwsem ii_rwsem;
73 aufs_bindex_t ii_bstart, ii_bend;
75 struct au_hinode *ii_hinode;
76 struct au_vdir *ii_vdir;
80 struct au_iinfo iinfo;
81 struct inode vfs_inode;
82 } ____cacheline_aligned_in_smp;
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)
95 struct dentry *dentry;
97 unsigned char lsc_di, lsc_hi, flags;
101 struct dentry *parent;
102 struct au_hinode *hdir;
103 struct vfsmount *h_mnt;
105 /* temporary unlock/relock for copyup */
106 struct dentry *h_dentry, *h_parent;
107 struct au_branch *br;
108 struct task_struct *task;
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);
117 /* ---------------------------------------------------------------------- */
119 static inline struct au_iinfo *au_ii(struct inode *inode)
121 struct au_iinfo *iinfo;
123 iinfo = &(container_of(inode, struct au_icntnr, vfs_inode)->iinfo);
124 if (iinfo->ii_hinode)
126 return NULL; /* debugging bad_inode case */
129 /* ---------------------------------------------------------------------- */
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);
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)
146 #ifdef CONFIG_AUFS_SHWH
147 return au_ino(sb, bindex, h_ino, d_type, ino);
154 extern struct inode_operations aufs_iop, aufs_symlink_iop, aufs_dir_iop;
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)
166 struct au_wr_dir_args {
167 aufs_bindex_t force_btgt;
170 int au_wr_dir(struct dentry *dentry, struct dentry *src_dentry,
171 struct au_wr_dir_args *args);
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);
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);
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);
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);
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);
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)
221 #ifndef CONFIG_AUFS_HNOTIFY
223 #define AuHi_HNOTIFY 0
226 void au_set_h_iptr(struct inode *inode, aufs_bindex_t bindex,
227 struct inode *h_inode, unsigned int flags);
229 void au_update_iigen(struct inode *inode, int half);
230 void au_update_ibrange(struct inode *inode, int do_put_zero);
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);
237 #ifdef CONFIG_PROC_FS
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);
245 AuStubVoid(au_plink_list, struct super_block *sb)
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);
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 */
269 /* ---------------------------------------------------------------------- */
271 /* lock subclass for iinfo */
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 */
278 AuLsc_II_PARENT3, /* copyup dirs */
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,
291 #define AuReadLockFunc(name, lsc) \
292 static inline void ii_read_lock_##name(struct inode *i) \
294 au_rw_read_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
297 #define AuWriteLockFunc(name, lsc) \
298 static inline void ii_write_lock_##name(struct inode *i) \
300 au_rw_write_lock_nested(&au_ii(i)->ii_rwsem, AuLsc_II_##lsc); \
303 #define AuRWLockFuncs(name, lsc) \
304 AuReadLockFunc(name, lsc) \
305 AuWriteLockFunc(name, lsc)
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);
315 #undef AuReadLockFunc
316 #undef AuWriteLockFunc
320 * ii_read_unlock, ii_write_unlock, ii_downgrade_lock
322 AuSimpleUnlockRwsemFuncs(ii, struct inode *i, &au_ii(i)->ii_rwsem);
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)
328 /* ---------------------------------------------------------------------- */
330 static inline void au_icntnr_init(struct au_icntnr *c)
332 #ifdef CONFIG_AUFS_DEBUG
333 c->vfs_inode.i_mode = 0;
337 static inline unsigned int au_iigen(struct inode *inode, struct au_iigen *iigen)
340 struct au_iinfo *iinfo;
342 iinfo = au_ii(inode);
343 spin_lock(&iinfo->ii_genspin);
345 *iigen = iinfo->ii_generation;
346 gen = iinfo->ii_generation.ig_generation;
347 spin_unlock(&iinfo->ii_genspin);
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)
356 struct au_iinfo *iinfo;
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);
364 static inline void au_iigen_dec(struct inode *inode)
366 struct au_iinfo *iinfo;
368 iinfo = au_ii(inode);
369 spin_lock(&iinfo->ii_genspin);
370 iinfo->ii_generation.ig_generation--;
371 spin_unlock(&iinfo->ii_genspin);
374 static inline int au_iigen_test(struct inode *inode, unsigned int sigen)
379 if (unlikely(inode && au_iigen(inode, NULL) != sigen))
385 /* ---------------------------------------------------------------------- */
387 static inline aufs_bindex_t au_ii_br_id(struct inode *inode,
388 aufs_bindex_t bindex)
390 IiMustAnyLock(inode);
391 return au_ii(inode)->ii_hinode[0 + bindex].hi_id;
394 static inline aufs_bindex_t au_ibstart(struct inode *inode)
396 IiMustAnyLock(inode);
397 return au_ii(inode)->ii_bstart;
400 static inline aufs_bindex_t au_ibend(struct inode *inode)
402 IiMustAnyLock(inode);
403 return au_ii(inode)->ii_bend;
406 static inline struct au_vdir *au_ivdir(struct inode *inode)
408 IiMustAnyLock(inode);
409 return au_ii(inode)->ii_vdir;
412 static inline struct dentry *au_hi_wh(struct inode *inode, aufs_bindex_t bindex)
414 IiMustAnyLock(inode);
415 return au_ii(inode)->ii_hinode[0 + bindex].hi_whdentry;
418 static inline void au_set_ibstart(struct inode *inode, aufs_bindex_t bindex)
420 IiMustWriteLock(inode);
421 au_ii(inode)->ii_bstart = bindex;
424 static inline void au_set_ibend(struct inode *inode, aufs_bindex_t bindex)
426 IiMustWriteLock(inode);
427 au_ii(inode)->ii_bend = bindex;
430 static inline void au_set_ivdir(struct inode *inode, struct au_vdir *vdir)
432 IiMustWriteLock(inode);
433 au_ii(inode)->ii_vdir = vdir;
436 static inline struct au_hinode *au_hi(struct inode *inode, aufs_bindex_t bindex)
438 IiMustAnyLock(inode);
439 return au_ii(inode)->ii_hinode + bindex;
442 /* ---------------------------------------------------------------------- */
444 static inline struct dentry *au_pinned_parent(struct au_pin *pin)
451 static inline struct inode *au_pinned_h_dir(struct au_pin *pin)
453 if (pin && pin->hdir)
454 return pin->hdir->hi_inode;
458 static inline struct au_hinode *au_pinned_hdir(struct au_pin *pin)
465 static inline void au_pin_set_dentry(struct au_pin *pin, struct dentry *dentry)
468 pin->dentry = dentry;
471 static inline void au_pin_set_parent_lflag(struct au_pin *pin,
476 au_fset_pin(pin->flags, DI_LOCKED);
478 au_fclr_pin(pin->flags, DI_LOCKED);
482 static inline void au_pin_set_parent(struct au_pin *pin, struct dentry *parent)
486 pin->parent = dget(parent);
490 /* ---------------------------------------------------------------------- */
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);
499 * if it returns true, the the caller should free hinode->hi_notify,
500 * otherwise ->free() frees it.
502 int (*free)(struct au_hinode *hinode,
503 struct au_hnotify *hn) __must_check;
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);
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);
527 extern const struct au_hnotify_op au_hnotify_op;
530 void au_hn_init(struct au_hinode *hinode)
532 hinode->hi_notify = NULL;
535 static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
537 return hinode->hi_notify;
542 int au_hn_alloc(struct au_hinode *hinode __maybe_unused,
543 struct inode *inode __maybe_unused)
548 static inline struct au_hnotify *au_hn(struct au_hinode *hinode)
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 */
569 static inline void au_hn_suspend(struct au_hinode *hdir)
571 au_hn_ctl(hdir, /*do_set*/0);
574 static inline void au_hn_resume(struct au_hinode *hdir)
576 au_hn_ctl(hdir, /*do_set*/1);
579 static inline void au_hn_imtx_lock(struct au_hinode *hdir)
581 mutex_lock(&hdir->hi_inode->i_mutex);
585 static inline void au_hn_imtx_lock_nested(struct au_hinode *hdir,
586 unsigned int sc __maybe_unused)
588 mutex_lock_nested(&hdir->hi_inode->i_mutex, sc);
592 static inline void au_hn_imtx_unlock(struct au_hinode *hdir)
595 mutex_unlock(&hdir->hi_inode->i_mutex);
598 #endif /* __KERNEL__ */
599 #endif /* __AUFS_INODE_H__ */