1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (c) 2002 Cluster File Systems, Inc. <info@clusterfs.com>
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #define DEBUG_SUBSYSTEM S_CMOBD
24 #include <linux/version.h>
25 #include <linux/init.h>
26 #include <linux/obd_support.h>
27 #include <linux/lustre_lib.h>
28 #include <linux/lustre_net.h>
29 #include <linux/lustre_idl.h>
30 #include <linux/lustre_dlm.h>
31 #include <linux/obd_class.h>
32 #include <linux/lustre_log.h>
33 #include <linux/lustre_cmobd.h>
34 #include <linux/lustre_fsfilt.h>
35 #include <linux/lustre_smfs.h>
37 #include "cm_internal.h"
39 void lov_free_memmd(struct lov_stripe_md **lsmp);
41 int lov_alloc_memmd(struct lov_stripe_md **lsmp, int stripe_count,
44 int smfs_rec_unpack(struct smfs_proc_args *args, char *record,
45 char **pbuf, int *opcode);
47 /* helper functions for cmobd to construct pseudo lsm */
48 int cmobd_dummy_lsm(struct lov_stripe_md **lsmp, int stripe_cnt,
49 struct obdo *oa, __u32 stripe_size)
54 rc = lov_alloc_memmd(lsmp, stripe_cnt, LOV_PATTERN_CMOBD);
58 for (i = 0; i < stripe_cnt; i++) {
59 (*lsmp)->lsm_oinfo[i].loi_id = oa->o_id;
60 (*lsmp)->lsm_object_id = oa->o_id;
61 if (oa->o_valid & OBD_MD_FLGROUP) {
62 (*lsmp)->lsm_oinfo[i].loi_gr = oa->o_gr;
63 (*lsmp)->lsm_object_gr = oa->o_gr;
65 (*lsmp)->lsm_oinfo[i].loi_ost_idx = i;
66 (*lsmp)->lsm_stripe_size = stripe_size;
71 void cmobd_free_lsm(struct lov_stripe_md **lsmp)
78 /* reintegration functions */
79 static int cmobd_setattr_reint(struct obd_device *obd, void *rec)
82 struct lov_stripe_md *lsm;
83 struct cm_obd *cmobd = &obd->u.cm;
84 struct obd_export *exp = cmobd->master_exp;
85 struct obdo *oa = (struct obdo *)rec;
88 rc = cmobd_dummy_lsm(&lsm, cmobd->master_desc.ld_tgt_count, oa,
89 (__u32)cmobd->master_desc.ld_default_stripe_size);
93 rc = obd_setattr(exp, oa, lsm, NULL);
100 static int cmobd_create_reint(struct obd_device *obd, void *rec)
102 struct obdo *oa = (struct obdo *)rec;
103 struct cm_obd *cmobd = &obd->u.cm;
104 struct obd_export *exp = cmobd->master_exp;
105 struct lov_stripe_md *lsm;
106 struct obd_trans_info oti = { 0 };
110 rc = cmobd_dummy_lsm(&lsm, cmobd->master_desc.ld_tgt_count, oa,
111 (__u32)cmobd->master_desc.ld_default_stripe_size);
114 if (cmobd->master_group != oa->o_gr) {
115 int group = oa->o_gr;
116 int valsize = sizeof(group);
117 rc = obd_set_info(exp, strlen("mds_conn"), "mds_conn",
120 GOTO(out, rc = -EINVAL);
121 cmobd->master_group = oa->o_gr;
123 rc = obd_create(exp, oa, NULL, 0, &lsm, &oti);
125 cmobd_free_lsm(&lsm);
130 /* direct cut-n-paste of filter_blocking_ast() */
131 static int cache_blocking_ast(struct ldlm_lock *lock,
132 struct ldlm_lock_desc *desc,
133 void *data, int flag)
138 if (flag == LDLM_CB_CANCELING) {
139 /* Don't need to do anything here. */
143 /* XXX layering violation! -phil */
144 lock_res_and_lock(lock);
146 /* Get this: if filter_blocking_ast is racing with ldlm_intent_policy,
147 * such that filter_blocking_ast is called just before l_i_p takes the
148 * ns_lock, then by the time we get the lock, we might not be the
149 * correct blocking function anymore. So check, and return early, if
151 if (lock->l_blocking_ast != cache_blocking_ast) {
152 unlock_res_and_lock(lock);
156 lock->l_flags |= LDLM_FL_CBPENDING;
157 do_ast = (!lock->l_readers && !lock->l_writers);
158 unlock_res_and_lock(lock);
161 struct lustre_handle lockh;
164 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
165 ldlm_lock2handle(lock, &lockh);
166 rc = ldlm_cli_cancel(&lockh);
168 CERROR("ldlm_cli_cancel: %d\n", rc);
170 LDLM_DEBUG(lock, "Lock still has references, will be "
176 static int master_blocking_ast(struct ldlm_lock *lock,
177 struct ldlm_lock_desc *desc,
178 void *data, int flag)
181 struct lustre_handle lockh;
185 case LDLM_CB_BLOCKING:
186 ldlm_lock2handle(lock, &lockh);
187 rc = ldlm_cli_cancel(&lockh);
189 CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
193 case LDLM_CB_CANCELING:
194 /* do nothing here by now */
202 static int cmobd_write_extents(struct obd_device *obd, struct obdo *oa,
203 struct ldlm_extent *extent)
205 struct cm_obd *cmobd = &obd->u.cm;
206 struct obd_device *cache = cmobd->cache_exp->exp_obd;
207 struct lustre_handle lockh_src = { 0 };
208 struct lustre_handle lockh_dst = { 0 };
209 struct ldlm_res_id res_id;
210 ldlm_policy_data_t policy;
211 struct lov_stripe_md *lsm;
212 int flags = 0, err, rc = 0;
215 /* XXX for debug write replay without smfs and kml */
216 res_id.name[0]= oa->o_id;
217 res_id.name[1]= oa->o_gr;
218 policy.l_extent.start = extent->start;
219 policy.l_extent.end = extent->end;
221 /* get extent read lock on the source replay file */
222 rc = ldlm_cli_enqueue(NULL, NULL, cache->obd_namespace, res_id,
223 LDLM_EXTENT, &policy, LCK_PR,
224 &flags, cache_blocking_ast, ldlm_completion_ast,
225 NULL, NULL, NULL, 0, NULL, &lockh_src);
229 /* construct the pseudo lsm */
232 * it is not good to access lov fields like @desc directly. This is
233 * layering violation. It should be accessed via some interface method,
234 * like llite does. --umka
236 rc = cmobd_dummy_lsm(&lsm, cmobd->master_desc.ld_tgt_count, oa,
237 (__u32)cmobd->master_desc.ld_default_stripe_size);
241 rc = obd_enqueue(cmobd->master_exp, lsm, LDLM_EXTENT, &policy,
242 LCK_PW, &flags, master_blocking_ast,
243 ldlm_completion_ast, NULL,
244 NULL, 0, NULL, &lockh_dst);
248 err = cmobd_replay_write(obd, oa, &policy.l_extent);
250 rc = obd_cancel(cmobd->master_exp, lsm, LCK_PW, &lockh_dst);
253 /* XXX in fact, I just want to cancel the only lockh_dst
255 rc = obd_cancel_unused(cmobd->master_exp, lsm, 0, NULL);
259 cmobd_free_lsm(&lsm);
261 ldlm_lock_decref(&lockh_src, LCK_PR);
265 static int cmobd_write_reint(struct obd_device *obd, void *rec)
267 struct cm_obd *cmobd = &obd->u.cm;
268 struct obdo *oa = (struct obdo *)rec;
269 struct ldlm_extent *extent = NULL;
270 char *extents_buf = NULL;
271 struct obd_device *cache;
272 int rc = 0, ext_num = 0;
273 unsigned long csb, ino;
278 obd_get_info(cmobd->cache_exp, strlen("cache_sb") + 1,
279 "cache_sb", &size, &csb);
281 ino = *(int*)(&oa->o_inline[0]);
283 cache = cmobd->cache_exp->exp_obd;
284 rc = fsfilt_get_ino_write_extents(cache, (struct super_block *)csb,
285 ino, &extents_buf, &ext_num);
288 extent = (struct ldlm_extent *)extents_buf;
290 while (extent && size --) {
291 rc = cmobd_write_extents(obd, oa, extent);
298 fsfilt_free_write_extents(cache, (struct super_block *)csb,
299 ino, extents_buf, ext_num);
303 int cmobd_reint_oss(struct obd_device *obd, void *record, int opcode)
307 return cmobd_create_reint(obd, record);
309 return cmobd_setattr_reint(obd, record);
311 return cmobd_write_reint(obd, record);
313 CERROR("unrecognized oss reint opcode %d\n",