]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/aufs/dynop.c
2 * Copyright (C) 2010-2017 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, see <http://www.gnu.org/licenses/>.
19 * dynamically customizable operations for regular files
24 #define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
27 * How large will these lists be?
28 * Usually just a few elements, 20-30 at most for each, I guess.
30 static struct hlist_bl_head dynop
[AuDyLast
];
32 static struct au_dykey
*dy_gfind_get(struct hlist_bl_head
*hbl
,
35 struct au_dykey
*key
, *tmp
;
36 struct hlist_bl_node
*pos
;
40 hlist_bl_for_each_entry(tmp
, pos
, hbl
, dk_hnode
)
41 if (tmp
->dk_op
.dy_hop
== h_op
) {
43 kref_get(&key
->dk_kref
);
51 static struct au_dykey
*dy_bradd(struct au_branch
*br
, struct au_dykey
*key
)
53 struct au_dykey
**k
, *found
;
54 const void *h_op
= key
->dk_op
.dy_hop
;
59 for (i
= 0; i
< AuBrDynOp
; i
++)
61 if (k
[i
]->dk_op
.dy_hop
== h_op
) {
68 spin_lock(&br
->br_dykey_lock
);
69 for (; i
< AuBrDynOp
; i
++)
71 if (k
[i
]->dk_op
.dy_hop
== h_op
) {
79 spin_unlock(&br
->br_dykey_lock
);
80 BUG_ON(i
== AuBrDynOp
); /* expand the array */
86 /* kref_get() if @key is already added */
87 static struct au_dykey
*dy_gadd(struct hlist_bl_head
*hbl
, struct au_dykey
*key
)
89 struct au_dykey
*tmp
, *found
;
90 struct hlist_bl_node
*pos
;
91 const void *h_op
= key
->dk_op
.dy_hop
;
95 hlist_bl_for_each_entry(tmp
, pos
, hbl
, dk_hnode
)
96 if (tmp
->dk_op
.dy_hop
== h_op
) {
97 kref_get(&tmp
->dk_kref
);
102 hlist_bl_add_head(&key
->dk_hnode
, hbl
);
103 hlist_bl_unlock(hbl
);
110 static void dy_free_rcu(struct rcu_head
*rcu
)
112 struct au_dykey
*key
;
114 key
= container_of(rcu
, struct au_dykey
, dk_rcu
);
119 static void dy_free(struct kref
*kref
)
121 struct au_dykey
*key
;
122 struct hlist_bl_head
*hbl
;
124 key
= container_of(kref
, struct au_dykey
, dk_kref
);
125 hbl
= dynop
+ key
->dk_op
.dy_type
;
126 au_hbl_del(&key
->dk_hnode
, hbl
);
127 call_rcu(&key
->dk_rcu
, dy_free_rcu
);
130 void au_dy_put(struct au_dykey
*key
)
132 kref_put(&key
->dk_kref
, dy_free
);
135 /* ---------------------------------------------------------------------- */
137 #define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
139 #ifdef CONFIG_AUFS_DEBUG
140 #define DyDbgDeclare(cnt) unsigned int cnt = 0
141 #define DyDbgInc(cnt) do { cnt++; } while (0)
143 #define DyDbgDeclare(cnt) do {} while (0)
144 #define DyDbgInc(cnt) do {} while (0)
147 #define DySet(func, dst, src, h_op, h_sb) do { \
151 dst.func = src.func; \
153 AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
157 #define DySetForce(func, dst, src) do { \
158 AuDebugOn(!src.func); \
160 dst.func = src.func; \
163 #define DySetAop(func) \
164 DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
165 #define DySetAopForce(func) \
166 DySetForce(func, dyaop->da_op, aufs_aop)
168 static void dy_aop(struct au_dykey
*key
, const void *h_op
,
169 struct super_block
*h_sb __maybe_unused
)
171 struct au_dyaop
*dyaop
= (void *)key
;
172 const struct address_space_operations
*h_aop
= h_op
;
175 AuDbg("%s\n", au_sbtype(h_sb
));
178 DySetAopForce(readpage
); /* force */
179 DySetAop(writepages
);
180 DySetAop(set_page_dirty
);
182 DySetAop(write_begin
);
185 DySetAop(invalidatepage
);
186 DySetAop(releasepage
);
188 /* this one will be changed according to an aufs mount option */
190 DySetAop(migratepage
);
191 DySetAop(isolate_page
);
192 DySetAop(putback_page
);
193 DySetAop(launder_page
);
194 DySetAop(is_partially_uptodate
);
195 DySetAop(is_dirty_writeback
);
196 DySetAop(error_remove_page
);
197 DySetAop(swap_activate
);
198 DySetAop(swap_deactivate
);
200 DyDbgSize(cnt
, *h_aop
);
203 /* ---------------------------------------------------------------------- */
205 static void dy_bug(struct kref
*kref
)
210 static struct au_dykey
*dy_get(struct au_dynop
*op
, struct au_branch
*br
)
212 struct au_dykey
*key
, *old
;
213 struct hlist_bl_head
*hbl
;
216 void (*set
)(struct au_dykey
*key
, const void *h_op
,
217 struct super_block
*h_sb __maybe_unused
);
219 static const struct op a
[] = {
221 .sz
= sizeof(struct au_dyaop
),
227 hbl
= dynop
+ op
->dy_type
;
228 key
= dy_gfind_get(hbl
, op
->dy_hop
);
230 goto out_add
; /* success */
233 key
= kzalloc(p
->sz
, GFP_NOFS
);
234 if (unlikely(!key
)) {
235 key
= ERR_PTR(-ENOMEM
);
239 key
->dk_op
.dy_hop
= op
->dy_hop
;
240 kref_init(&key
->dk_kref
);
241 p
->set(key
, op
->dy_hop
, au_br_sb(br
));
242 old
= dy_gadd(hbl
, key
);
249 old
= dy_bradd(br
, key
);
251 /* its ref-count should never be zero here */
252 kref_put(&key
->dk_kref
, dy_bug
);
257 /* ---------------------------------------------------------------------- */
259 * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
260 * This behaviour is necessary to return an error from open(O_DIRECT) instead
261 * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
262 * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
263 * See the aufs manual in detail.
265 static void dy_adx(struct au_dyaop
*dyaop
, int do_dx
)
268 dyaop
->da_op
.direct_IO
= NULL
;
270 dyaop
->da_op
.direct_IO
= aufs_aop
.direct_IO
;
273 static struct au_dyaop
*dy_aget(struct au_branch
*br
,
274 const struct address_space_operations
*h_aop
,
277 struct au_dyaop
*dyaop
;
280 op
.dy_type
= AuDy_AOP
;
282 dyaop
= (void *)dy_get(&op
, br
);
285 dy_adx(dyaop
, do_dx
);
291 int au_dy_iaop(struct inode
*inode
, aufs_bindex_t bindex
,
292 struct inode
*h_inode
)
295 struct super_block
*sb
;
296 struct au_branch
*br
;
297 struct au_dyaop
*dyaop
;
299 AuDebugOn(!S_ISREG(h_inode
->i_mode
));
300 IiMustWriteLock(inode
);
303 br
= au_sbr(sb
, bindex
);
304 do_dx
= !!au_opt_test(au_mntflags(sb
), DIO
);
305 dyaop
= dy_aget(br
, h_inode
->i_mapping
->a_ops
, do_dx
);
306 err
= PTR_ERR(dyaop
);
308 /* unnecessary to call dy_fput() */
312 inode
->i_mapping
->a_ops
= &dyaop
->da_op
;
319 * Is it safe to replace a_ops during the inode/file is in operation?
322 int au_dy_irefresh(struct inode
*inode
)
326 struct inode
*h_inode
;
329 if (S_ISREG(inode
->i_mode
)) {
330 btop
= au_ibtop(inode
);
331 h_inode
= au_h_iptr(inode
, btop
);
332 err
= au_dy_iaop(inode
, btop
, h_inode
);
337 void au_dy_arefresh(int do_dx
)
339 struct hlist_bl_head
*hbl
;
340 struct hlist_bl_node
*pos
;
341 struct au_dykey
*key
;
343 hbl
= dynop
+ AuDy_AOP
;
345 hlist_bl_for_each_entry(key
, pos
, hbl
, dk_hnode
)
346 dy_adx((void *)key
, do_dx
);
347 hlist_bl_unlock(hbl
);
350 /* ---------------------------------------------------------------------- */
352 void __init
au_dy_init(void)
356 /* make sure that 'struct au_dykey *' can be any type */
357 BUILD_BUG_ON(offsetof(struct au_dyaop
, da_key
));
359 for (i
= 0; i
< AuDyLast
; i
++)
360 INIT_HLIST_BL_HEAD(dynop
+ i
);
367 for (i
= 0; i
< AuDyLast
; i
++)
368 WARN_ON(!hlist_bl_empty(dynop
+ i
));