Whamcloud - gitweb
b=6332
[fs/lustre-release.git] / lustre / cmobd / cm_oss_reint.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2002 Cluster File Systems, Inc. <info@clusterfs.com>
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
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.
11  *
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.
16  *
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.
20  */
21
22 #define DEBUG_SUBSYSTEM S_CMOBD
23
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/obd_class.h>
31 #include <linux/lustre_log.h>
32 #include <linux/lustre_cmobd.h>
33 #include <linux/lustre_fsfilt.h>
34 #include <linux/lustre_smfs.h>
35
36 #include "cm_internal.h"
37
38 void lov_free_memmd(struct lov_stripe_md **lsmp);
39
40 int lov_alloc_memmd(struct lov_stripe_md **lsmp, int stripe_count, 
41                     int pattern);
42
43 int smfs_rec_unpack(struct smfs_proc_args *args, char *record, 
44                     char **pbuf, int *opcode);
45
46 /* helper functions for cmobd to construct pseudo lsm */
47 int cmobd_dummy_lsm(struct lov_stripe_md **lsmp, int stripe_cnt, 
48                     struct obdo *oa, __u32 stripe_size)
49 {
50         int i, rc;
51         ENTRY;
52
53         rc = lov_alloc_memmd(lsmp, stripe_cnt, LOV_PATTERN_CMOBD);
54         if (rc < 0)
55                 RETURN(rc);
56         
57         for (i = 0; i < stripe_cnt; i++) {
58                 (*lsmp)->lsm_oinfo[i].loi_id = oa->o_id;
59                 (*lsmp)->lsm_object_id = oa->o_id;
60                 if (oa->o_valid & OBD_MD_FLGROUP) {
61                         (*lsmp)->lsm_oinfo[i].loi_gr = oa->o_gr;
62                         (*lsmp)->lsm_object_gr = oa->o_gr;
63                 }
64                 (*lsmp)->lsm_oinfo[i].loi_ost_idx = i;
65                 (*lsmp)->lsm_stripe_size = stripe_size;
66         }
67         RETURN(0);
68 }
69
70 void cmobd_free_lsm(struct lov_stripe_md **lsmp)
71 {
72         ENTRY;
73         lov_free_memmd(lsmp);
74         EXIT;
75 }
76
77 /* reintegration functions */
78 static int cmobd_setattr_reint(struct obd_device *obd, void *rec)
79 {
80         struct obdo *oa = (struct obdo*)rec;
81         struct cm_obd *cmobd = &obd->u.cm;
82         struct obd_export *exp = cmobd->master_exp;
83         struct lov_stripe_md *lsm;
84         struct lov_obd *lov;
85         int rc;
86         ENTRY;
87         
88         /* 
89          * nevertheless ost is not used anymore and lov should be always present
90          * as a object storage export, using ost is still possible (just
91          * deprecated) and we should make sure here, that this is really
92          * lov. --umka.
93          */
94         lov = &cmobd->master_exp->exp_obd->u.lov;
95         rc = cmobd_dummy_lsm(&lsm, lov->desc.ld_tgt_count, oa, 
96                              (__u32)lov->desc.ld_default_stripe_size);
97         if (rc)
98                 GOTO(out, rc);
99
100         rc = obd_setattr(exp, oa, lsm, NULL);
101
102         cmobd_free_lsm(&lsm);
103 out:
104         RETURN(rc);
105 }
106
107 static int cmobd_create_reint(struct obd_device *obd, void *rec)
108 {
109         struct obdo *oa = (struct obdo *)rec;
110         struct cm_obd *cmobd = &obd->u.cm;
111         struct obd_export *exp = cmobd->master_exp;
112         struct lov_stripe_md *lsm;
113         struct obd_trans_info oti = { 0 };
114         struct lov_obd *lov;
115         int rc;
116         ENTRY;
117          
118         /* 
119          * nevertheless ost is not used anymore and lov should be always present
120          * as a object storage export, using ost is still possible (just
121          * deprecated) and we should make sure here, that this is really
122          * lov. --umka.
123          */
124         lov = &cmobd->master_exp->exp_obd->u.lov;
125         rc = cmobd_dummy_lsm(&lsm, lov->desc.ld_tgt_count, oa,
126                              (__u32)lov->desc.ld_default_stripe_size);
127         if (rc)
128                 GOTO(out, rc);
129         if (cmobd->master_group != oa->o_gr) {
130                 int group = oa->o_gr;
131                 int valsize = sizeof(group);
132                 rc = obd_set_info(exp, strlen("mds_conn"), "mds_conn",
133                                   valsize, &group);
134                 if (rc)
135                         GOTO(out, rc = -EINVAL);
136                 cmobd->master_group = oa->o_gr;
137         }
138         rc = obd_create(exp, oa, NULL, 0, &lsm, &oti);
139
140         cmobd_free_lsm(&lsm);
141 out:
142         RETURN(rc);
143 }
144
145 /* direct cut-n-paste of filter_blocking_ast() */
146 static int cache_blocking_ast(struct ldlm_lock *lock,
147                               struct ldlm_lock_desc *desc,
148                               void *data, int flag)
149 {
150         int do_ast;
151         ENTRY;
152
153         if (flag == LDLM_CB_CANCELING) {
154                 /* Don't need to do anything here. */
155                 RETURN(0);
156         }
157
158         /* XXX layering violation!  -phil */
159         l_lock(&lock->l_resource->lr_namespace->ns_lock);
160         /* Get this: if filter_blocking_ast is racing with ldlm_intent_policy,
161          * such that filter_blocking_ast is called just before l_i_p takes the
162          * ns_lock, then by the time we get the lock, we might not be the
163          * correct blocking function anymore.  So check, and return early, if
164          * so. */
165         if (lock->l_blocking_ast != cache_blocking_ast) {
166                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
167                 RETURN(0);
168         }
169
170         lock->l_flags |= LDLM_FL_CBPENDING;
171         do_ast = (!lock->l_readers && !lock->l_writers);
172         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
173
174         if (do_ast) {
175                 struct lustre_handle lockh;
176                 int rc;
177
178                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
179                 ldlm_lock2handle(lock, &lockh);
180                 rc = ldlm_cli_cancel(&lockh);
181                 if (rc < 0)
182                         CERROR("ldlm_cli_cancel: %d\n", rc);
183         } else {
184                 LDLM_DEBUG(lock, "Lock still has references, will be "
185                            "cancelled later");
186         }
187         RETURN(0);
188 }
189
190 static int master_blocking_ast(struct ldlm_lock *lock, 
191                                struct ldlm_lock_desc *desc,
192                                void *data, int flag)
193 {
194         int rc;
195         struct lustre_handle lockh;
196         ENTRY;
197
198         switch (flag) {
199         case LDLM_CB_BLOCKING:
200                 ldlm_lock2handle(lock, &lockh);
201                 rc = ldlm_cli_cancel(&lockh);
202                 if (rc < 0) {
203                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
204                         RETURN(rc);
205                 }
206                 break;
207         case LDLM_CB_CANCELING: 
208                 /* do nothing here by now */
209                 break;
210         default:
211                 LBUG();
212         }
213         RETURN(0);
214 }
215
216 static int cmobd_write_extents(struct obd_device *obd, struct obdo *oa, 
217                                struct ldlm_extent *extent)
218 {
219         struct cm_obd *cmobd = &obd->u.cm;
220         struct obd_device *cache = cmobd->cache_exp->exp_obd;
221         struct lustre_handle lockh_src = { 0 };
222         struct lustre_handle lockh_dst = { 0 };
223         struct ldlm_res_id res_id;
224         ldlm_policy_data_t policy;
225         struct lov_stripe_md *lsm;
226         int flags = 0, err, rc = 0;
227         struct lov_obd *lov;
228         ENTRY;
229
230         /* XXX for debug write replay without smfs and kml */
231         res_id.name[0]= oa->o_id;
232         res_id.name[1]= oa->o_gr;
233         policy.l_extent.start = extent->start;
234         policy.l_extent.end = extent->end;
235         
236         /* get extent read lock on the source replay file */
237         rc = ldlm_cli_enqueue(NULL, NULL, cache->obd_namespace, res_id,
238                               LDLM_EXTENT, &policy, LCK_PR,
239                               &flags, cache_blocking_ast, ldlm_completion_ast,
240                               NULL, NULL, NULL, 0, NULL, &lockh_src);
241         if (rc != ELDLM_OK)
242                 RETURN(rc);
243         
244         /* 
245          * nevertheless ost is not used anymore and lov should be always present
246          * as a object storage export, using ost is still possible (just
247          * deprecated) and we should make sure here, that this is really
248          * lov. --umka.
249          */
250         lov = &cmobd->master_exp->exp_obd->u.lov;
251
252         /* construct the pseudo lsm */
253
254         /*
255          * it is not good to access lov fields like @desc directly. This is
256          * layering violation. It should be accessed via some interface method,
257          * like llite does. --umka
258          */
259         rc = cmobd_dummy_lsm(&lsm, lov->desc.ld_tgt_count, oa,
260                              (__u32)lov->desc.ld_default_stripe_size);
261         if (rc)
262                 GOTO(out_lock, rc);
263         
264         rc = obd_enqueue(cmobd->master_exp, lsm, LDLM_EXTENT, &policy, 
265                          LCK_PW, &flags, master_blocking_ast, 
266                          ldlm_completion_ast, NULL,
267                          NULL, 0, NULL, &lockh_dst);
268         if (rc != ELDLM_OK)
269                 GOTO(out_lsm, rc);
270
271         err = cmobd_replay_write(obd, oa, &policy.l_extent);
272         
273         rc = obd_cancel(cmobd->master_exp, lsm, LCK_PW, &lockh_dst);
274         if (rc)
275                 GOTO(out_lsm, rc);
276         /* XXX in fact, I just want to cancel the only lockh_dst 
277          *     instantly. */
278         rc = obd_cancel_unused(cmobd->master_exp, lsm, 0, NULL);
279         if (err)
280                 rc = err;
281 out_lsm:
282         cmobd_free_lsm(&lsm);
283 out_lock:
284         ldlm_lock_decref(&lockh_src, LCK_PR);
285         RETURN(rc);
286 }
287
288 static int cmobd_write_reint(struct obd_device *obd, void *rec)
289 {
290         struct cm_obd *cmobd = &obd->u.cm;
291         struct obd_device *cache = cmobd->cache_exp->exp_obd;
292         struct obdo *oa = (struct obdo *)rec;
293         struct ldlm_extent *extent = NULL; 
294         unsigned long csb, ino;
295         char   *extents_buf = NULL;
296         int    size = 0, rc = 0, ext_num = 0; 
297         ENTRY;
298
299         size = sizeof(csb);
300
301         obd_get_info(cmobd->cache_exp, strlen("cache_sb") + 1,
302                      "cache_sb", &size, &csb); 
303  
304         ino = *(int*)(&oa->o_inline[0]);
305         rc = fsfilt_get_ino_write_extents(cache, (struct super_block *)csb, ino,
306                                           &extents_buf, &ext_num);
307         if (rc)
308                 GOTO(out, rc);   
309         extent = (struct ldlm_extent *)extents_buf;
310         size = ext_num;
311         while (extent && size --) { 
312                 rc = cmobd_write_extents(obd, oa, extent);
313                 if (rc)
314                         GOTO(out, rc); 
315                 extent ++;
316         }
317 out:
318         if (extents_buf)
319                 fsfilt_free_write_extents(cache, (struct super_block *)csb, 
320                                           ino, extents_buf, ext_num); 
321         RETURN(rc);
322 }
323
324 int cmobd_reint_oss(struct obd_device *obd, void *record, int opcode)
325 {
326         switch (opcode) {
327         case OST_CREATE:
328                 return cmobd_create_reint(obd, record);
329         case OST_SETATTR:
330                 return cmobd_setattr_reint(obd, record);
331         case OST_WRITE:
332                 return cmobd_write_reint(obd, record);
333         default:
334                 CERROR("unrecognized oss reint opcode %d\n", 
335                        opcode);
336                 return -EINVAL;
337         }
338 }