]>
git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - fs/aufs/dynop.c
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2010-2021 Junjiro R. Okajima
5 * This program, aufs is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * dynamically customizable operations for regular files
25 #define DyPrSym(key) AuDbgSym(key->dk_op.dy_hop)
28 * How large will these lists be?
29 * Usually just a few elements, 20-30 at most for each, I guess.
31 static struct hlist_bl_head dynop
[AuDyLast
];
33 static struct au_dykey
*dy_gfind_get(struct hlist_bl_head
*hbl
,
36 struct au_dykey
*key
, *tmp
;
37 struct hlist_bl_node
*pos
;
41 hlist_bl_for_each_entry(tmp
, pos
, hbl
, dk_hnode
)
42 if (tmp
->dk_op
.dy_hop
== h_op
) {
43 if (kref_get_unless_zero(&tmp
->dk_kref
))
52 static struct au_dykey
*dy_bradd(struct au_branch
*br
, struct au_dykey
*key
)
54 struct au_dykey
**k
, *found
;
55 const void *h_op
= key
->dk_op
.dy_hop
;
60 for (i
= 0; i
< AuBrDynOp
; i
++)
62 if (k
[i
]->dk_op
.dy_hop
== h_op
) {
69 spin_lock(&br
->br_dykey_lock
);
70 for (; i
< AuBrDynOp
; i
++)
72 if (k
[i
]->dk_op
.dy_hop
== h_op
) {
80 spin_unlock(&br
->br_dykey_lock
);
81 BUG_ON(i
== AuBrDynOp
); /* expand the array */
87 /* kref_get() if @key is already added */
88 static struct au_dykey
*dy_gadd(struct hlist_bl_head
*hbl
, struct au_dykey
*key
)
90 struct au_dykey
*tmp
, *found
;
91 struct hlist_bl_node
*pos
;
92 const void *h_op
= key
->dk_op
.dy_hop
;
96 hlist_bl_for_each_entry(tmp
, pos
, hbl
, dk_hnode
)
97 if (tmp
->dk_op
.dy_hop
== h_op
) {
98 if (kref_get_unless_zero(&tmp
->dk_kref
))
103 hlist_bl_add_head(&key
->dk_hnode
, hbl
);
104 hlist_bl_unlock(hbl
);
111 static void dy_free_rcu(struct rcu_head
*rcu
)
113 struct au_dykey
*key
;
115 key
= container_of(rcu
, struct au_dykey
, dk_rcu
);
120 static void dy_free(struct kref
*kref
)
122 struct au_dykey
*key
;
123 struct hlist_bl_head
*hbl
;
125 key
= container_of(kref
, struct au_dykey
, dk_kref
);
126 hbl
= dynop
+ key
->dk_op
.dy_type
;
127 au_hbl_del(&key
->dk_hnode
, hbl
);
128 call_rcu(&key
->dk_rcu
, dy_free_rcu
);
131 void au_dy_put(struct au_dykey
*key
)
133 kref_put(&key
->dk_kref
, dy_free
);
136 /* ---------------------------------------------------------------------- */
138 #define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
140 #ifdef CONFIG_AUFS_DEBUG
141 #define DyDbgDeclare(cnt) unsigned int cnt = 0
142 #define DyDbgInc(cnt) do { cnt++; } while (0)
144 #define DyDbgDeclare(cnt) do {} while (0)
145 #define DyDbgInc(cnt) do {} while (0)
148 #define DySet(func, dst, src, h_op, h_sb) do { \
152 dst.func = src.func; \
154 AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
158 #define DySetForce(func, dst, src) do { \
159 AuDebugOn(!src.func); \
161 dst.func = src.func; \
164 #define DySetAop(func) \
165 DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
166 #define DySetAopForce(func) \
167 DySetForce(func, dyaop->da_op, aufs_aop)
169 static void dy_aop(struct au_dykey
*key
, const void *h_op
,
170 struct super_block
*h_sb __maybe_unused
)
172 struct au_dyaop
*dyaop
= (void *)key
;
173 const struct address_space_operations
*h_aop
= h_op
;
176 AuDbg("%s\n", au_sbtype(h_sb
));
179 DySetAopForce(readpage
); /* force */
180 DySetAop(writepages
);
181 DySetAop(set_page_dirty
);
184 DySetAop(write_begin
);
187 DySetAop(invalidatepage
);
188 DySetAop(releasepage
);
190 /* this one will be changed according to an aufs mount option */
192 DySetAop(migratepage
);
193 DySetAop(isolate_page
);
194 DySetAop(putback_page
);
195 DySetAop(launder_page
);
196 DySetAop(is_partially_uptodate
);
197 DySetAop(is_dirty_writeback
);
198 DySetAop(error_remove_page
);
199 DySetAop(swap_activate
);
200 DySetAop(swap_deactivate
);
202 DyDbgSize(cnt
, *h_aop
);
205 /* ---------------------------------------------------------------------- */
207 static void dy_bug(struct kref
*kref
)
212 static struct au_dykey
*dy_get(struct au_dynop
*op
, struct au_branch
*br
)
214 struct au_dykey
*key
, *old
;
215 struct hlist_bl_head
*hbl
;
218 void (*set
)(struct au_dykey
*key
, const void *h_op
,
219 struct super_block
*h_sb __maybe_unused
);
221 static const struct op a
[] = {
223 .sz
= sizeof(struct au_dyaop
),
229 hbl
= dynop
+ op
->dy_type
;
230 key
= dy_gfind_get(hbl
, op
->dy_hop
);
232 goto out_add
; /* success */
235 key
= kzalloc(p
->sz
, GFP_NOFS
);
236 if (unlikely(!key
)) {
237 key
= ERR_PTR(-ENOMEM
);
241 key
->dk_op
.dy_hop
= op
->dy_hop
;
242 kref_init(&key
->dk_kref
);
243 p
->set(key
, op
->dy_hop
, au_br_sb(br
));
244 old
= dy_gadd(hbl
, key
);
251 old
= dy_bradd(br
, key
);
253 /* its ref-count should never be zero here */
254 kref_put(&key
->dk_kref
, dy_bug
);
259 /* ---------------------------------------------------------------------- */
261 * Aufs prohibits O_DIRECT by default even if the branch supports it.
262 * This behaviour is necessary to return an error from open(O_DIRECT) instead
263 * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
264 * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
265 * See the aufs manual in detail.
267 static void dy_adx(struct au_dyaop
*dyaop
, int do_dx
)
270 dyaop
->da_op
.direct_IO
= NULL
;
272 dyaop
->da_op
.direct_IO
= aufs_aop
.direct_IO
;
275 static struct au_dyaop
*dy_aget(struct au_branch
*br
,
276 const struct address_space_operations
*h_aop
,
279 struct au_dyaop
*dyaop
;
282 op
.dy_type
= AuDy_AOP
;
284 dyaop
= (void *)dy_get(&op
, br
);
287 dy_adx(dyaop
, do_dx
);
293 int au_dy_iaop(struct inode
*inode
, aufs_bindex_t bindex
,
294 struct inode
*h_inode
)
297 struct super_block
*sb
;
298 struct au_branch
*br
;
299 struct au_dyaop
*dyaop
;
301 AuDebugOn(!S_ISREG(h_inode
->i_mode
));
302 IiMustWriteLock(inode
);
305 br
= au_sbr(sb
, bindex
);
306 do_dx
= !!au_opt_test(au_mntflags(sb
), DIO
);
307 dyaop
= dy_aget(br
, h_inode
->i_mapping
->a_ops
, do_dx
);
308 err
= PTR_ERR(dyaop
);
310 /* unnecessary to call dy_fput() */
314 inode
->i_mapping
->a_ops
= &dyaop
->da_op
;
321 * Is it safe to replace a_ops during the inode/file is in operation?
324 int au_dy_irefresh(struct inode
*inode
)
328 struct inode
*h_inode
;
331 if (S_ISREG(inode
->i_mode
)) {
332 btop
= au_ibtop(inode
);
333 h_inode
= au_h_iptr(inode
, btop
);
334 err
= au_dy_iaop(inode
, btop
, h_inode
);
339 void au_dy_arefresh(int do_dx
)
341 struct hlist_bl_head
*hbl
;
342 struct hlist_bl_node
*pos
;
343 struct au_dykey
*key
;
345 hbl
= dynop
+ AuDy_AOP
;
347 hlist_bl_for_each_entry(key
, pos
, hbl
, dk_hnode
)
348 dy_adx((void *)key
, do_dx
);
349 hlist_bl_unlock(hbl
);
352 /* ---------------------------------------------------------------------- */
354 void __init
au_dy_init(void)
358 for (i
= 0; i
< AuDyLast
; i
++)
359 INIT_HLIST_BL_HEAD(dynop
+ i
);
366 for (i
= 0; i
< AuDyLast
; i
++)
367 WARN_ON(!hlist_bl_empty(dynop
+ i
));