Whamcloud - gitweb
landing b_cmobd_merge on HEAD
[fs/lustre-release.git] / lustre / cmobd / cmobd_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 "cmobd_internal.h"
37
38 int lov_alloc_memmd(struct lov_stripe_md **lsmp, int stripe_count, int pattern);
39 void lov_free_memmd(struct lov_stripe_md **lsmp);
40 int smfs_rec_unpack(struct smfs_proc_args *args, char *record, char **pbuf, 
41                     int *opcode);
42
43 /* helper functions for cmobd to construct pseudo lsm */
44 int cmobd_dummy_lsm(struct lov_stripe_md **lsmp, int stripe_cnt, 
45                     struct obdo *oa, __u32 stripe_size)
46 {
47         int i, rc;
48         ENTRY;
49
50         rc = lov_alloc_memmd(lsmp, stripe_cnt, LOV_PATTERN_CMOBD);
51         if (rc < 0)
52                 RETURN(rc);
53         
54         for (i = 0; i < stripe_cnt; i++) {
55                 (*lsmp)->lsm_oinfo[i].loi_id = oa->o_id;
56                 (*lsmp)->lsm_object_id = oa->o_id;
57                 if (oa->o_valid & OBD_MD_FLGROUP) {
58                         (*lsmp)->lsm_oinfo[i].loi_gr = oa->o_gr;
59                         (*lsmp)->lsm_object_gr = oa->o_gr;
60                 }
61                 (*lsmp)->lsm_oinfo[i].loi_ost_idx = i;
62                 (*lsmp)->lsm_stripe_size = stripe_size;
63         }
64         RETURN(0);
65 }
66
67 void cmobd_free_lsm(struct lov_stripe_md **lsmp)
68 {
69         ENTRY;
70         lov_free_memmd(lsmp);
71         EXIT;
72 }
73
74 /* reintegration functions */
75 int cmobd_reint_setattr(struct obd_device *obd, void *rec)
76 {
77         struct cache_manager_obd *cmobd = &obd->u.cmobd;
78         struct lov_obd *lov = &cmobd->cm_master_obd->u.lov;
79         struct obd_export *exp = cmobd->cm_master_exp;
80         struct lov_stripe_md *lsm;
81         struct obdo *oa = (struct obdo*)rec;
82         int rc;
83         ENTRY;
84         
85         rc = cmobd_dummy_lsm(&lsm, lov->desc.ld_tgt_count, oa, 
86                              (__u32)lov->desc.ld_default_stripe_size);
87         if (rc)
88                 GOTO(out, rc);
89
90         rc = obd_setattr(exp, oa, lsm, NULL);
91
92         cmobd_free_lsm(&lsm);
93 out:
94         RETURN(rc);
95 }
96
97 int cmobd_reint_create(struct obd_device *obd, void *rec)
98 {
99         struct cache_manager_obd *cmobd = &obd->u.cmobd;
100         struct lov_obd *lov = &cmobd->cm_master_obd->u.lov;
101         struct obd_export *exp = cmobd->cm_master_exp;
102         struct lov_stripe_md *lsm;
103         struct obd_trans_info oti = { 0 };
104         struct obdo *oa=(struct obdo*)rec;
105         int rc;
106         ENTRY;
107          
108         rc = cmobd_dummy_lsm(&lsm, lov->desc.ld_tgt_count, oa,
109                              (__u32)lov->desc.ld_default_stripe_size);
110         if (rc)
111                 GOTO(out, rc);
112         if (cmobd->cm_master_group != oa->o_gr) {
113                 int group = oa->o_gr;
114                 int valsize = sizeof(group);
115                 rc = obd_set_info(exp, strlen("mds_conn"), "mds_conn",
116                                   valsize, &group);
117                 if (rc)
118                         GOTO(out, rc = -EINVAL);
119                 cmobd->cm_master_group = oa->o_gr;
120         }
121         rc = obd_create(exp, oa, &lsm, &oti);
122
123         cmobd_free_lsm(&lsm);
124 out:
125         RETURN(rc);
126 }
127
128 /* direct cut-n-paste of filter_blocking_ast() */
129 static int cache_blocking_ast(struct ldlm_lock *lock,
130                               struct ldlm_lock_desc *desc,
131                               void *data, int flag)
132 {
133         int do_ast;
134         ENTRY;
135
136         if (flag == LDLM_CB_CANCELING) {
137                 /* Don't need to do anything here. */
138                 RETURN(0);
139         }
140
141         /* XXX layering violation!  -phil */
142         l_lock(&lock->l_resource->lr_namespace->ns_lock);
143         /* Get this: if filter_blocking_ast is racing with ldlm_intent_policy,
144          * such that filter_blocking_ast is called just before l_i_p takes the
145          * ns_lock, then by the time we get the lock, we might not be the
146          * correct blocking function anymore.  So check, and return early, if
147          * so. */
148         if (lock->l_blocking_ast != cache_blocking_ast) {
149                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
150                 RETURN(0);
151         }
152
153         lock->l_flags |= LDLM_FL_CBPENDING;
154         do_ast = (!lock->l_readers && !lock->l_writers);
155         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
156
157         if (do_ast) {
158                 struct lustre_handle lockh;
159                 int rc;
160
161                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
162                 ldlm_lock2handle(lock, &lockh);
163                 rc = ldlm_cli_cancel(&lockh);
164                 if (rc < 0)
165                         CERROR("ldlm_cli_cancel: %d\n", rc);
166         } else {
167                 LDLM_DEBUG(lock, "Lock still has references, will be "
168                            "cancelled later");
169         }
170         RETURN(0);
171 }
172
173 static int master_blocking_ast(struct ldlm_lock *lock, 
174                                struct ldlm_lock_desc *desc,
175                                void *data, int flag)
176 {
177         int rc;
178         struct lustre_handle lockh;
179         ENTRY;
180
181         switch (flag) {
182         case LDLM_CB_BLOCKING:
183                 ldlm_lock2handle(lock, &lockh);
184                 rc = ldlm_cli_cancel(&lockh);
185                 if (rc < 0) {
186                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
187                         RETURN(rc);
188                 }
189                 break;
190         case LDLM_CB_CANCELING: 
191                 /* do nothing here by now */
192                 break;
193         default:
194                 LBUG();
195         }
196         RETURN(0);
197 }
198
199 static int cmobd_write_extents(struct obd_device *obd, struct obdo *oa, 
200                                struct ldlm_extent *extent)
201 {
202         struct cache_manager_obd *cmobd = &obd->u.cmobd;
203         struct obd_device *cache = cmobd->cm_cache_obd;
204         struct lov_obd *lov = &cmobd->cm_master_obd->u.lov;
205         struct ldlm_res_id res_id;
206         ldlm_policy_data_t policy;
207         struct lustre_handle lockh_src = { 0 };
208         struct lustre_handle lockh_dst = { 0 };
209         struct lov_stripe_md *lsm;
210         int flags = 0, err, rc = 0;
211         ENTRY;
212
213         /* XXX for debug write replay without smfs and kml */
214         res_id.name[0]= oa->o_id;
215         res_id.name[1]= oa->o_gr;
216         policy.l_extent.start = extent->start;
217         policy.l_extent.end = extent->end;
218         
219         /* get extent read lock on the source replay file */
220         rc = ldlm_cli_enqueue(NULL, NULL, cache->obd_namespace, res_id,
221                               LDLM_EXTENT, &policy, LCK_PR,
222                               &flags, cache_blocking_ast, ldlm_completion_ast,
223                               NULL, NULL, NULL, 0, NULL, &lockh_src);
224         if (rc != ELDLM_OK)
225                 RETURN(rc);
226         
227         /* construct the pseudo lsm */
228         rc = cmobd_dummy_lsm(&lsm, lov->desc.ld_tgt_count, oa,
229                              (__u32)lov->desc.ld_default_stripe_size);
230         if (rc)
231                 GOTO(out_lock, rc);
232         
233         rc = obd_enqueue(cmobd->cm_master_exp, lsm, LDLM_EXTENT, &policy, 
234                          LCK_PW, &flags, master_blocking_ast, 
235                          ldlm_completion_ast, NULL,
236                          NULL, 0, NULL, &lockh_dst);
237         if (rc != ELDLM_OK)
238                 GOTO(out_lsm, rc);
239
240         err = cmobd_replay_write(obd, oa, &policy.l_extent);
241         
242         rc = obd_cancel(cmobd->cm_master_exp, lsm, LCK_PW, &lockh_dst);
243         if (rc)
244                 GOTO(out_lsm, rc);
245         /* XXX in fact, I just want to cancel the only lockh_dst 
246          *     instantly. */
247         rc = obd_cancel_unused(cmobd->cm_master_exp, lsm, 0, NULL);
248         if (err)
249                 rc = err;
250 out_lsm:
251         cmobd_free_lsm(&lsm);
252 out_lock:
253         ldlm_lock_decref(&lockh_src, LCK_PR);
254         RETURN(rc);
255 }
256 int cmobd_reint_write(struct obd_device *obd, void *rec)
257 {
258         struct cache_manager_obd *cmobd = &obd->u.cmobd;
259         struct obd_device *cache = cmobd->cm_cache_obd;
260         struct obdo *oa = (struct obdo *)rec;
261         struct ldlm_extent *extent = NULL; 
262         unsigned long csb, ino;
263         char   *extents_buf = NULL;
264         int    size = 0, rc = 0, ext_num = 0; 
265         ENTRY;
266
267         size = sizeof(csb);
268
269         obd_get_info(cmobd->cm_cache_exp, strlen("cache_sb") + 1,
270                      "cache_sb", &size, &csb); 
271  
272         ino = *(int*)(&oa->o_inline[0]);
273         rc = fsfilt_get_ino_write_extents(cache, (struct super_block *)csb, ino,
274                                           &extents_buf, &ext_num);
275         if (rc)
276                 GOTO(out, rc);   
277         extent = (struct ldlm_extent *)extents_buf;
278         size = ext_num;
279         while (extent && size --) { 
280                 rc = cmobd_write_extents(obd, oa, extent);
281                 if (rc)
282                         GOTO(out, rc); 
283                 extent ++;
284         }
285 out:
286         if (extents_buf)
287                 fsfilt_free_write_extents(cache, (struct super_block *)csb, ino, 
288                                           extents_buf, ext_num); 
289         RETURN(rc);
290 }
291