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