2 * Copyright (C) 2010-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
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 au_splhead dynop[AuDyLast];
33 static struct au_dykey *dy_gfind_get(struct au_splhead *spl, const void *h_op)
35 struct au_dykey *key, *tmp;
36 struct list_head *head;
41 list_for_each_entry_rcu(tmp, head, dk_list)
42 if (tmp->dk_op.dy_hop == h_op) {
44 kref_get(&key->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 au_splhead *spl, struct au_dykey *key)
90 struct au_dykey *tmp, *found;
91 struct list_head *head;
92 const void *h_op = key->dk_op.dy_hop;
96 spin_lock(&spl->spin);
97 list_for_each_entry(tmp, head, dk_list)
98 if (tmp->dk_op.dy_hop == h_op) {
99 kref_get(&tmp->dk_kref);
104 list_add_rcu(&key->dk_list, head);
105 spin_unlock(&spl->spin);
112 static void dy_free_rcu(struct rcu_head *rcu)
114 struct au_dykey *key;
116 key = container_of(rcu, struct au_dykey, dk_rcu);
121 static void dy_free(struct kref *kref)
123 struct au_dykey *key;
124 struct au_splhead *spl;
126 key = container_of(kref, struct au_dykey, dk_kref);
127 spl = dynop + key->dk_op.dy_type;
128 au_spl_del_rcu(&key->dk_list, spl);
129 call_rcu(&key->dk_rcu, dy_free_rcu);
132 void au_dy_put(struct au_dykey *key)
134 kref_put(&key->dk_kref, dy_free);
137 /* ---------------------------------------------------------------------- */
139 #define DyDbgSize(cnt, op) AuDebugOn(cnt != sizeof(op)/sizeof(void *))
141 #ifdef CONFIG_AUFS_DEBUG
142 #define DyDbgDeclare(cnt) unsigned int cnt = 0
143 #define DyDbgInc(cnt) do { cnt++; } while (0)
145 #define DyDbgDeclare(cnt) do {} while (0)
146 #define DyDbgInc(cnt) do {} while (0)
149 #define DySet(func, dst, src, h_op, h_sb) do { \
153 dst.func = src.func; \
155 AuDbg("%s %s\n", au_sbtype(h_sb), #func); \
159 #define DySetForce(func, dst, src) do { \
160 AuDebugOn(!src.func); \
162 dst.func = src.func; \
165 #define DySetAop(func) \
166 DySet(func, dyaop->da_op, aufs_aop, h_aop, h_sb)
167 #define DySetAopForce(func) \
168 DySetForce(func, dyaop->da_op, aufs_aop)
170 static void dy_aop(struct au_dykey *key, const void *h_op,
171 struct super_block *h_sb __maybe_unused)
173 struct au_dyaop *dyaop = (void *)key;
174 const struct address_space_operations *h_aop = h_op;
177 AuDbg("%s\n", au_sbtype(h_sb));
180 DySetAopForce(readpage); /* force */
181 DySetAop(writepages);
182 DySetAop(set_page_dirty);
184 DySetAop(write_begin);
187 DySetAop(invalidatepage);
188 DySetAop(releasepage);
190 /* these two will be changed according to an aufs mount option */
192 DySetAop(get_xip_mem);
193 DySetAop(migratepage);
194 DySetAop(launder_page);
195 DySetAop(is_partially_uptodate);
196 DySetAop(error_remove_page);
198 DyDbgSize(cnt, *h_aop);
199 dyaop->da_get_xip_mem = h_aop->get_xip_mem;
202 /* ---------------------------------------------------------------------- */
204 static void dy_bug(struct kref *kref)
209 static struct au_dykey *dy_get(struct au_dynop *op, struct au_branch *br)
211 struct au_dykey *key, *old;
212 struct au_splhead *spl;
215 void (*set)(struct au_dykey *key, const void *h_op,
216 struct super_block *h_sb __maybe_unused);
218 static const struct op a[] = {
220 .sz = sizeof(struct au_dyaop),
226 spl = dynop + op->dy_type;
227 key = dy_gfind_get(spl, op->dy_hop);
229 goto out_add; /* success */
232 key = kzalloc(p->sz, GFP_NOFS);
233 if (unlikely(!key)) {
234 key = ERR_PTR(-ENOMEM);
238 key->dk_op.dy_hop = op->dy_hop;
239 kref_init(&key->dk_kref);
240 p->set(key, op->dy_hop, au_br_sb(br));
241 old = dy_gadd(spl, key);
248 old = dy_bradd(br, key);
250 /* its ref-count should never be zero here */
251 kref_put(&key->dk_kref, dy_bug);
256 /* ---------------------------------------------------------------------- */
258 * Aufs prohibits O_DIRECT by defaut even if the branch supports it.
259 * This behaviour is neccessary to return an error from open(O_DIRECT) instead
260 * of the succeeding I/O. The dio mount option enables O_DIRECT and makes
261 * open(O_DIRECT) always succeed, but the succeeding I/O may return an error.
262 * See the aufs manual in detail.
264 * To keep this behaviour, aufs has to set NULL to ->get_xip_mem too, and the
265 * performance of fadvise() and madvise() may be affected.
267 static void dy_adx(struct au_dyaop *dyaop, int do_dx)
270 dyaop->da_op.direct_IO = NULL;
271 dyaop->da_op.get_xip_mem = NULL;
273 dyaop->da_op.direct_IO = aufs_aop.direct_IO;
274 dyaop->da_op.get_xip_mem = aufs_aop.get_xip_mem;
275 if (!dyaop->da_get_xip_mem)
276 dyaop->da_op.get_xip_mem = NULL;
280 static struct au_dyaop *dy_aget(struct au_branch *br,
281 const struct address_space_operations *h_aop,
284 struct au_dyaop *dyaop;
287 op.dy_type = AuDy_AOP;
289 dyaop = (void *)dy_get(&op, br);
292 dy_adx(dyaop, do_dx);
298 int au_dy_iaop(struct inode *inode, aufs_bindex_t bindex,
299 struct inode *h_inode)
302 struct super_block *sb;
303 struct au_branch *br;
304 struct au_dyaop *dyaop;
306 AuDebugOn(!S_ISREG(h_inode->i_mode));
307 IiMustWriteLock(inode);
310 br = au_sbr(sb, bindex);
311 do_dx = !!au_opt_test(au_mntflags(sb), DIO);
312 dyaop = dy_aget(br, h_inode->i_mapping->a_ops, do_dx);
313 err = PTR_ERR(dyaop);
315 /* unnecessary to call dy_fput() */
319 inode->i_mapping->a_ops = &dyaop->da_op;
326 * Is it safe to replace a_ops during the inode/file is in operation?
329 int au_dy_irefresh(struct inode *inode)
332 aufs_bindex_t bstart;
333 struct inode *h_inode;
336 if (S_ISREG(inode->i_mode)) {
337 bstart = au_ibstart(inode);
338 h_inode = au_h_iptr(inode, bstart);
339 err = au_dy_iaop(inode, bstart, h_inode);
344 void au_dy_arefresh(int do_dx)
346 struct au_splhead *spl;
347 struct list_head *head;
348 struct au_dykey *key;
350 spl = dynop + AuDy_AOP;
352 spin_lock(&spl->spin);
353 list_for_each_entry(key, head, dk_list)
354 dy_adx((void *)key, do_dx);
355 spin_unlock(&spl->spin);
358 /* ---------------------------------------------------------------------- */
360 void __init au_dy_init(void)
364 /* make sure that 'struct au_dykey *' can be any type */
365 BUILD_BUG_ON(offsetof(struct au_dyaop, da_key));
367 for (i = 0; i < AuDyLast; i++)
368 au_spl_init(dynop + i);
375 for (i = 0; i < AuDyLast; i++)
376 WARN_ON(!list_empty(&dynop[i].head));