Whamcloud - gitweb
- landing of b_fid after merge with b_hd_cleanup_merge.
[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 cm_obd *cmobd = &obd->u.cm;
81         struct lov_obd *lov = &cmobd->master_obd->u.lov;
82         struct obd_export *exp = cmobd->master_exp;
83         struct lov_stripe_md *lsm;
84         struct obdo *oa = (struct obdo*)rec;
85         int rc;
86         ENTRY;
87         
88         rc = cmobd_dummy_lsm(&lsm, lov->desc.ld_tgt_count, oa, 
89                              (__u32)lov->desc.ld_default_stripe_size);
90         if (rc)
91                 GOTO(out, rc);
92
93         rc = obd_setattr(exp, oa, lsm, NULL);
94
95         cmobd_free_lsm(&lsm);
96 out:
97         RETURN(rc);
98 }
99
100 static int cmobd_create_reint(struct obd_device *obd, void *rec)
101 {
102         struct cm_obd *cmobd = &obd->u.cm;
103         struct lov_obd *lov = &cmobd->master_obd->u.lov;
104         struct obd_export *exp = cmobd->master_exp;
105         struct lov_stripe_md *lsm;
106         struct obd_trans_info oti = { 0 };
107         struct obdo *oa=(struct obdo*)rec;
108         int rc;
109         ENTRY;
110          
111         rc = cmobd_dummy_lsm(&lsm, lov->desc.ld_tgt_count, oa,
112                              (__u32)lov->desc.ld_default_stripe_size);
113         if (rc)
114                 GOTO(out, rc);
115         if (cmobd->master_group != oa->o_gr) {
116                 int group = oa->o_gr;
117                 int valsize = sizeof(group);
118                 rc = obd_set_info(exp, strlen("mds_conn"), "mds_conn",
119                                   valsize, &group);
120                 if (rc)
121                         GOTO(out, rc = -EINVAL);
122                 cmobd->master_group = oa->o_gr;
123         }
124         rc = obd_create(exp, oa, &lsm, &oti);
125
126         cmobd_free_lsm(&lsm);
127 out:
128         RETURN(rc);
129 }
130
131 /* direct cut-n-paste of filter_blocking_ast() */
132 static int cache_blocking_ast(struct ldlm_lock *lock,
133                               struct ldlm_lock_desc *desc,
134                               void *data, int flag)
135 {
136         int do_ast;
137         ENTRY;
138
139         if (flag == LDLM_CB_CANCELING) {
140                 /* Don't need to do anything here. */
141                 RETURN(0);
142         }
143
144         /* XXX layering violation!  -phil */
145         l_lock(&lock->l_resource->lr_namespace->ns_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
150          * so. */
151         if (lock->l_blocking_ast != cache_blocking_ast) {
152                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
153                 RETURN(0);
154         }
155
156         lock->l_flags |= LDLM_FL_CBPENDING;
157         do_ast = (!lock->l_readers && !lock->l_writers);
158         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
159
160         if (do_ast) {
161                 struct lustre_handle lockh;
162                 int rc;
163
164                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
165                 ldlm_lock2handle(lock, &lockh);
166                 rc = ldlm_cli_cancel(&lockh);
167                 if (rc < 0)
168                         CERROR("ldlm_cli_cancel: %d\n", rc);
169         } else {
170                 LDLM_DEBUG(lock, "Lock still has references, will be "
171                            "cancelled later");
172         }
173         RETURN(0);
174 }
175
176 static int master_blocking_ast(struct ldlm_lock *lock, 
177                                struct ldlm_lock_desc *desc,
178                                void *data, int flag)
179 {
180         int rc;
181         struct lustre_handle lockh;
182         ENTRY;
183
184         switch (flag) {
185         case LDLM_CB_BLOCKING:
186                 ldlm_lock2handle(lock, &lockh);
187                 rc = ldlm_cli_cancel(&lockh);
188                 if (rc < 0) {
189                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
190                         RETURN(rc);
191                 }
192                 break;
193         case LDLM_CB_CANCELING: 
194                 /* do nothing here by now */
195                 break;
196         default:
197                 LBUG();
198         }
199         RETURN(0);
200 }
201
202 static int cmobd_write_extents(struct obd_device *obd, struct obdo *oa, 
203                                struct ldlm_extent *extent)
204 {
205         struct cm_obd *cmobd = &obd->u.cm;
206         struct obd_device *cache = cmobd->cache_obd;
207         struct lov_obd *lov = &cmobd->master_obd->u.lov;
208         struct ldlm_res_id res_id;
209         ldlm_policy_data_t policy;
210         struct lustre_handle lockh_src = { 0 };
211         struct lustre_handle lockh_dst = { 0 };
212         struct lov_stripe_md *lsm;
213         int flags = 0, err, rc = 0;
214         ENTRY;
215
216         /* XXX for debug write replay without smfs and kml */
217         res_id.name[0]= oa->o_id;
218         res_id.name[1]= oa->o_gr;
219         policy.l_extent.start = extent->start;
220         policy.l_extent.end = extent->end;
221         
222         /* get extent read lock on the source replay file */
223         rc = ldlm_cli_enqueue(NULL, NULL, cache->obd_namespace, res_id,
224                               LDLM_EXTENT, &policy, LCK_PR,
225                               &flags, cache_blocking_ast, ldlm_completion_ast,
226                               NULL, NULL, NULL, 0, NULL, &lockh_src);
227         if (rc != ELDLM_OK)
228                 RETURN(rc);
229         
230         /* construct the pseudo lsm */
231         rc = cmobd_dummy_lsm(&lsm, lov->desc.ld_tgt_count, oa,
232                              (__u32)lov->desc.ld_default_stripe_size);
233         if (rc)
234                 GOTO(out_lock, rc);
235         
236         rc = obd_enqueue(cmobd->master_exp, lsm, LDLM_EXTENT, &policy, 
237                          LCK_PW, &flags, master_blocking_ast, 
238                          ldlm_completion_ast, NULL,
239                          NULL, 0, NULL, &lockh_dst);
240         if (rc != ELDLM_OK)
241                 GOTO(out_lsm, rc);
242
243         err = cmobd_replay_write(obd, oa, &policy.l_extent);
244         
245         rc = obd_cancel(cmobd->master_exp, lsm, LCK_PW, &lockh_dst);
246         if (rc)
247                 GOTO(out_lsm, rc);
248         /* XXX in fact, I just want to cancel the only lockh_dst 
249          *     instantly. */
250         rc = obd_cancel_unused(cmobd->master_exp, lsm, 0, NULL);
251         if (err)
252                 rc = err;
253 out_lsm:
254         cmobd_free_lsm(&lsm);
255 out_lock:
256         ldlm_lock_decref(&lockh_src, LCK_PR);
257         RETURN(rc);
258 }
259
260 static int cmobd_write_reint(struct obd_device *obd, void *rec)
261 {
262         struct cm_obd *cmobd = &obd->u.cm;
263         struct obd_device *cache = cmobd->cache_obd;
264         struct obdo *oa = (struct obdo *)rec;
265         struct ldlm_extent *extent = NULL; 
266         unsigned long csb, ino;
267         char   *extents_buf = NULL;
268         int    size = 0, rc = 0, ext_num = 0; 
269         ENTRY;
270
271         size = sizeof(csb);
272
273         obd_get_info(cmobd->cache_exp, strlen("cache_sb") + 1,
274                      "cache_sb", &size, &csb); 
275  
276         ino = *(int*)(&oa->o_inline[0]);
277         rc = fsfilt_get_ino_write_extents(cache, (struct super_block *)csb, ino,
278                                           &extents_buf, &ext_num);
279         if (rc)
280                 GOTO(out, rc);   
281         extent = (struct ldlm_extent *)extents_buf;
282         size = ext_num;
283         while (extent && size --) { 
284                 rc = cmobd_write_extents(obd, oa, extent);
285                 if (rc)
286                         GOTO(out, rc); 
287                 extent ++;
288         }
289 out:
290         if (extents_buf)
291                 fsfilt_free_write_extents(cache, (struct super_block *)csb, 
292                                           ino, extents_buf, ext_num); 
293         RETURN(rc);
294 }
295
296 int cmobd_reint_oss(struct obd_device *obd, void *record, int opcode)
297 {
298         switch (opcode) {
299         case OST_CREATE:
300                 return cmobd_create_reint(obd, record);
301         case OST_SETATTR:
302                 return cmobd_setattr_reint(obd, record);
303         case OST_WRITE:
304                 return cmobd_write_reint(obd, record);
305         default:
306                 CERROR("unrecognized oss reint opcode %d\n", 
307                        opcode);
308                 return -EINVAL;
309         }
310 }