Whamcloud - gitweb
drop truncate capa after ll_truncate.
[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_oinfo[i].loi_ost_gen = 1;
67                 (*lsmp)->lsm_stripe_size = stripe_size;
68         }
69         RETURN(0);
70 }
71
72 void cmobd_free_lsm(struct lov_stripe_md **lsmp)
73 {
74         ENTRY;
75         lov_free_memmd(lsmp);
76         EXIT;
77 }
78
79 /* reintegration functions */
80 static int cmobd_setattr_reint(struct obd_device *obd, void *rec)
81 {
82         int rc = 0;
83         struct lov_stripe_md *lsm;
84         struct cm_obd *cmobd = &obd->u.cm;
85         struct obd_export *exp = cmobd->master_exp;
86         struct obdo *oa = (struct obdo *)rec;
87         ENTRY;
88         
89         rc = cmobd_dummy_lsm(&lsm, cmobd->master_desc.ld_tgt_count, oa, 
90                              (__u32)cmobd->master_desc.ld_default_stripe_size);
91         if (rc)
92                 GOTO(out, rc);
93
94         rc = obd_setattr(exp, oa, lsm, NULL, NULL);
95
96         cmobd_free_lsm(&lsm);
97 out:
98         RETURN(rc);
99 }
100
101 static int cmobd_create_reint(struct obd_device *obd, void *rec)
102 {
103         struct cm_obd *cmobd = &obd->u.cm;
104         struct obd_export *exp = cmobd->master_exp;
105         struct obdo *oa = (struct obdo *)rec;
106         struct obd_trans_info oti = { 0 };
107         struct lov_stripe_md *lsm;
108         int rc;
109         ENTRY;
110          
111         rc = cmobd_dummy_lsm(&lsm, cmobd->master_desc.ld_tgt_count, oa,
112                              (__u32)cmobd->master_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
119                 rc = obd_set_info(exp, strlen("mds_conn"),
120                                   "mds_conn", valsize, &group);
121                 if (rc)
122                         GOTO(out, rc);
123                 cmobd->master_group = oa->o_gr;
124         }
125
126         oti.oti_flags |= OBD_MODE_CROW;
127         rc = obd_create(exp, oa, NULL, 0, &lsm, &oti);
128         cmobd_free_lsm(&lsm);
129         EXIT;
130 out:
131         return rc;
132 }
133
134 /* direct cut-n-paste of filter_blocking_ast() */
135 static int cache_blocking_ast(struct ldlm_lock *lock,
136                               struct ldlm_lock_desc *desc,
137                               void *data, int flag)
138 {
139         int rc, do_ast;
140         ENTRY;
141
142         if (flag == LDLM_CB_CANCELING) {
143                 /* Don't need to do anything here. */
144                 RETURN(0);
145         }
146
147         /* XXX layering violation!  -phil */
148         lock_res_and_lock(lock);
149         
150         /* get this: if filter_blocking_ast() is racing with ldlm_intent_policy,
151          * such that filter_blocking_ast is called just before l_i_p takes the
152          * ns_lock, then by the time we get the lock, we might not be the
153          * correct blocking function anymore.  So check, and return early, if
154          * so. */
155         if (lock->l_blocking_ast != cache_blocking_ast) {
156                 unlock_res_and_lock(lock);
157                 RETURN(0);
158         }
159
160         lock->l_flags |= LDLM_FL_CBPENDING;
161         do_ast = (!lock->l_readers && !lock->l_writers);
162         unlock_res_and_lock(lock);
163
164         if (do_ast) {
165                 struct lustre_handle lockh;
166                 LDLM_DEBUG(lock, "already unused, calling ldlm_cli_cancel");
167                 ldlm_lock2handle(lock, &lockh);
168                 rc = ldlm_cli_cancel(&lockh);
169                 if (rc < 0)
170                         CERROR("ldlm_cli_cancel: %d\n", rc);
171         } else {
172                 LDLM_DEBUG(lock, "Lock still has references, will be "
173                            "cancelled later");
174         }
175         RETURN(0);
176 }
177
178 static int master_blocking_ast(struct ldlm_lock *lock, 
179                                struct ldlm_lock_desc *desc,
180                                void *data, int flag)
181 {
182         int rc;
183         struct lustre_handle lockh;
184         ENTRY;
185
186         switch (flag) {
187         case LDLM_CB_BLOCKING:
188                 ldlm_lock2handle(lock, &lockh);
189                 rc = ldlm_cli_cancel(&lockh);
190                 if (rc < 0) {
191                         CDEBUG(D_INODE, "ldlm_cli_cancel: %d\n", rc);
192                         RETURN(rc);
193                 }
194                 break;
195         case LDLM_CB_CANCELING: 
196                 /* do nothing here by now */
197                 break;
198         default:
199                 LBUG();
200         }
201         RETURN(0);
202 }
203
204 static int cmobd_write_extents(struct obd_device *obd, struct obdo *oa, 
205                                struct ldlm_extent *extent)
206 {
207         struct cm_obd *cmobd = &obd->u.cm;
208         struct obd_device *cache = cmobd->cache_exp->exp_obd;
209         struct lustre_handle lockh_src = { 0 };
210         struct lustre_handle lockh_dst = { 0 };
211         struct ldlm_res_id res_id;
212         ldlm_policy_data_t policy;
213         struct lov_stripe_md *lsm;
214         int flags = 0, err, rc = 0;
215         ENTRY;
216
217         /* XXX for debug write replay without smfs and kml */
218         res_id.name[0]= oa->o_id;
219         res_id.name[1]= oa->o_gr;
220         policy.l_extent = *extent;
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, cmobd->master_desc.ld_tgt_count, oa,
232                              (__u32)cmobd->master_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
249         /* XXX in fact, I just want to cancel the only lockh_dst 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 obdo *oa = (struct obdo *)rec;
263         struct cm_obd *cmobd = &obd->u.cm;
264         struct ldlm_extent *extent = NULL; 
265         char *extents_buf = NULL;
266         struct obd_device *cache;
267         int rc = 0, ext_num = 0;
268         unsigned long csb, ino;
269         __u32 size = 0;
270         ENTRY;
271
272         size = sizeof(csb);
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         
278         cache = cmobd->cache_exp->exp_obd;
279         rc = fsfilt_get_ino_write_extents(cache, (struct super_block *)csb,
280                                           ino, &extents_buf, &ext_num);
281         if (rc)
282                 GOTO(out, rc);   
283         extent = (struct ldlm_extent *)extents_buf;
284         size = ext_num;
285         while (extent && size --) { 
286                 rc = cmobd_write_extents(obd, oa, extent);
287                 if (rc)
288                         GOTO(out, rc); 
289                 extent ++;
290         }
291 out:
292         if (extents_buf)
293                 fsfilt_free_write_extents(cache, (struct super_block *)csb, 
294                                           ino, extents_buf, ext_num); 
295         RETURN(rc);
296 }
297
298 int cmobd_reint_oss(struct obd_device *obd, void *record, int opcode)
299 {
300         switch (opcode) {
301         case OST_CREATE:
302                 return cmobd_create_reint(obd, record);
303         case OST_SETATTR:
304                 return cmobd_setattr_reint(obd, record);
305         case OST_WRITE:
306                 return cmobd_write_reint(obd, record);
307         default:
308                 CERROR("unrecognized oss reint opcode %d\n", 
309                        opcode);
310                 return -EINVAL;
311         }
312 }