Whamcloud - gitweb
645a32ee42255d68142407bb7a7cd00c33c173d4
[fs/lustre-release.git] / lustre / ptlrpc / llog_server.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *   Author: Andreas Dilger <adilger@clusterfs.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  *  remote api for llog - server side
23  *
24  */
25
26 #define DEBUG_SUBSYSTEM S_LOG
27
28 #ifndef EXPORT_SYMTAB
29 #define EXPORT_SYMTAB
30 #endif
31
32 #include <linux/fs.h>
33 #include <linux/obd_class.h>
34 #include <linux/lustre_log.h>
35 #include <linux/lustre_net.h>
36 #include <portals/list.h>
37
38 int llog_origin_handle_create(struct ptlrpc_request *req)
39 {
40         struct obd_export *exp = req->rq_export;
41         struct obd_device *obd = exp->exp_obd;
42         struct obd_device *disk_obd;
43         struct llog_handle  *loghandle;
44         struct llogd_body *body;
45         struct obd_run_ctxt saved;
46         struct llog_logid *logid = NULL;
47         struct llog_ctxt *ctxt;
48         char * name = NULL;
49         int size = sizeof (*body);
50         int rc, rc2;
51         ENTRY;
52
53         body = lustre_swab_reqbuf(req, 0, sizeof(*body),
54                                  lustre_swab_llogd_body);
55         if (body == NULL) {
56                 CERROR ("Can't unpack llogd_body\n");
57                 GOTO(out, rc =-EFAULT);
58         }
59
60         if (body->lgd_logid.lgl_oid > 0)
61                 logid = &body->lgd_logid;
62
63         if (req->rq_reqmsg->bufcount > 1) {
64                 name = lustre_msg_string(req->rq_reqmsg, 1, 0);
65                 if (name == NULL) {
66                         CERROR("Can't unpack name\n");
67                         GOTO(out, rc = -EFAULT);
68                 }
69         }
70
71         ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
72         LASSERT(ctxt != NULL);
73         disk_obd = ctxt->loc_exp->exp_obd;
74         push_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
75
76         rc = llog_create(ctxt, &loghandle, logid, name);
77         if (rc)
78                 GOTO(out_pop, rc);
79
80         rc = lustre_pack_reply(req, 1, &size, NULL);
81         if (rc)
82                 GOTO(out_close, rc = -ENOMEM);
83
84         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
85         body->lgd_logid = loghandle->lgh_id;
86
87 out_close:
88         rc2 = llog_close(loghandle);
89         if (!rc)
90                 rc = rc2;
91 out_pop:
92         pop_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
93 out:
94         RETURN(rc);
95 }
96
97 int llog_origin_handle_next_block(struct ptlrpc_request *req)
98 {
99         struct obd_export *exp = req->rq_export;
100         struct obd_device *obd = exp->exp_obd;
101         struct obd_device *disk_obd;
102         struct llog_handle  *loghandle;
103         struct llogd_body *body;
104         struct obd_run_ctxt saved;
105         struct llog_ctxt *ctxt;
106         __u32 flags;
107         __u8 *buf;
108         void * ptr;
109         int size[] = {sizeof (*body),
110                       LLOG_CHUNK_SIZE};
111         int rc, rc2;
112         ENTRY;
113
114         body = lustre_swab_reqbuf(req, 0, sizeof(*body),
115                                   lustre_swab_llogd_body);
116         if (body == NULL) {
117                 CERROR ("Can't unpack llogd_body\n");
118                 GOTO(out, rc =-EFAULT);
119         }
120
121         OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
122         if (!buf)
123                 GOTO(out, rc = -ENOMEM);
124
125         ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
126         LASSERT(ctxt != NULL);
127         disk_obd = ctxt->loc_exp->exp_obd;
128         push_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
129
130         rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
131         if (rc)
132                 GOTO(out_pop, rc);
133
134         flags = body->lgd_llh_flags;
135         rc = llog_init_handle(loghandle, flags, NULL);
136         if (rc)
137                 GOTO(out_close, rc);
138
139         memset(buf, 0, LLOG_CHUNK_SIZE);
140         rc = llog_next_block(loghandle, &body->lgd_saved_index,
141                              body->lgd_index,
142                              &body->lgd_cur_offset, buf, LLOG_CHUNK_SIZE);
143         if (rc)
144                 GOTO(out_close, rc);
145
146
147         rc = lustre_pack_reply(req, 2, size, NULL);
148         if (rc)
149                 GOTO(out_close, rc = -ENOMEM);
150
151         ptr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (body));
152         memcpy(ptr, body, sizeof(*body));
153
154         ptr = lustre_msg_buf(req->rq_repmsg, 1, LLOG_CHUNK_SIZE);
155         memcpy(ptr, buf, LLOG_CHUNK_SIZE);
156
157 out_close:
158         rc2 = llog_close(loghandle);
159         if (!rc)
160                 rc = rc2;
161
162 out_pop:
163         pop_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
164         OBD_FREE(buf, LLOG_CHUNK_SIZE);
165 out:
166         RETURN(rc);
167 }
168
169 int llog_origin_handle_read_header(struct ptlrpc_request *req)
170 {
171         struct obd_export *exp = req->rq_export;
172         struct obd_device *obd = exp->exp_obd;
173         struct obd_device *disk_obd;
174         struct llog_handle  *loghandle;
175         struct llogd_body *body;
176         struct llog_log_hdr *hdr;
177         struct obd_run_ctxt saved;
178         struct llog_ctxt *ctxt;
179         __u32 flags;
180         __u8 *buf;
181         int size[] = {sizeof (*hdr)};
182         int rc, rc2;
183         ENTRY;
184
185         body = lustre_swab_reqbuf(req, 0, sizeof(*body),
186                                   lustre_swab_llogd_body);
187         if (body == NULL) {
188                 CERROR ("Can't unpack llogd_body\n");
189                 GOTO(out, rc =-EFAULT);
190         }
191
192         OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
193         if (!buf)
194                 GOTO(out, rc = -ENOMEM);
195
196         ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
197         LASSERT(ctxt != NULL);
198         disk_obd = ctxt->loc_exp->exp_obd;
199         push_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
200
201         rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
202         if (rc)
203                 GOTO(out_pop, rc);
204
205         /* init_handle reads the header */
206         flags = body->lgd_llh_flags;
207         rc = llog_init_handle(loghandle, flags, NULL);
208         if (rc)
209                 GOTO(out_close, rc);
210
211
212         rc = lustre_pack_reply(req, 1, size, NULL);
213         if (rc)
214                 GOTO(out_close, rc = -ENOMEM);
215
216         hdr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*hdr));
217         memcpy(hdr, loghandle->lgh_hdr, sizeof(*hdr));
218
219 out_close:
220         rc2 = llog_close(loghandle);
221         if (!rc)
222                 rc = rc2;
223
224 out_pop:
225         pop_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
226         OBD_FREE(buf, LLOG_CHUNK_SIZE);
227
228 out:
229         RETURN(rc);
230 }
231
232 int llog_origin_handle_close(struct ptlrpc_request *req)
233 {
234         int rc;
235
236         rc = 0;
237
238         RETURN(rc);
239 }
240
241 #ifdef ENABLE_ORPHANS
242 int llog_origin_handle_cancel(struct ptlrpc_request *req)
243 {
244         struct obd_device *obd = req->rq_export->exp_obd;
245         struct obd_device *disk_obd;
246         struct llog_cookie *logcookies;
247         struct llog_ctxt *ctxt;
248         int num_cookies, rc = 0;
249         struct obd_run_ctxt saved;
250         struct llog_handle *cathandle;
251         ENTRY;
252
253         logcookies = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*logcookies));
254         num_cookies = req->rq_reqmsg->buflens[0]/sizeof(*logcookies);
255         if (logcookies == NULL || num_cookies == 0) {
256                 DEBUG_REQ(D_HA, req, "no cookies sent");
257                 RETURN(-EFAULT);
258         }
259
260         ctxt = llog_get_context(obd, logcookies->lgc_subsys);
261         if (ctxt == NULL) {
262                 CWARN("llog subsys not setup or already cleanup\n");
263                 RETURN(-ENOENT);
264         }
265         down(&ctxt->loc_sem);
266         disk_obd = ctxt->loc_exp->exp_obd;
267         cathandle = ctxt->loc_handle;
268         LASSERT(cathandle);
269
270         push_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
271         rc = llog_cat_cancel_records(cathandle, num_cookies, logcookies);
272         if (rc)
273                 CERROR("cancel %d llog-records failed: %d\n", num_cookies, rc);
274         else
275                 CWARN("cancel %d llog-records\n", num_cookies);
276
277         pop_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
278         up(&ctxt->loc_sem);
279
280         RETURN(rc);
281 }
282 EXPORT_SYMBOL(llog_origin_handle_cancel);
283 #endif
284
285 static int llog_catinfo_config(struct obd_device *obd, char *buf, int buf_len, 
286                                char *client)
287 {
288         struct mds_obd *mds = &obd->u.mds;
289         struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
290         struct obd_run_ctxt saved;
291         struct llog_handle *handle = NULL;
292         char name[4][64];
293         int rc, i, l, remains = buf_len;
294         char *out = buf;
295         
296         if (ctxt == NULL || mds == NULL)
297                 RETURN(-EOPNOTSUPP);
298
299         push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
300         
301         sprintf(name[0], "%s", mds->mds_profile);
302         sprintf(name[1], "%s-clean", mds->mds_profile);
303         sprintf(name[2], "%s", client);
304         sprintf(name[3], "%s-clean", client);
305         
306         for (i = 0; i < 4; i++) {
307                 int index, uncanceled = 0;
308                 rc = llog_create(ctxt, &handle, NULL, name[i]);
309                 if (rc)
310                         GOTO(out_pop, rc);
311                 rc = llog_init_handle(handle, 0, NULL);
312                 if (rc) {
313                         llog_close(handle);
314                         GOTO(out_pop, rc = -ENOENT);
315                 }
316                 
317                 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index ++) {
318                         if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
319                                 uncanceled++;
320                 }
321                 
322                 l = snprintf(out, remains, "[Log Name]: %s\nLog Size: "LPD64"\n"
323                              "Last Index: %d\nUncanceled Records: %d\n\n",
324                              name[i], 
325                              handle->lgh_file->f_dentry->d_inode->i_size,
326                              handle->lgh_last_idx,
327                              uncanceled);
328                 out += l;
329                 remains -= l;
330
331                 llog_close(handle);
332                 if (remains <= 0)
333                         break;
334         }
335 out_pop:
336         pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
337         RETURN(rc);
338 }
339
340 struct cb_data {
341         struct llog_ctxt *ctxt;
342         char *out;
343         int  remains;
344         int  init;
345 };
346
347 static int llog_catinfo_cb(struct llog_handle *cat, 
348                            struct llog_rec_hdr *rec, void *data)
349 {
350         static char *out = NULL;
351         static int remains = 0;
352         struct llog_ctxt *ctxt;
353         struct llog_handle *handle;
354         struct llog_logid *logid;
355         struct llog_logid_rec *lir;
356         int l, rc, index, count = 0;
357         struct cb_data *cbd = (struct cb_data*)data;
358
359         if (cbd->init) {
360                 out = cbd->out;
361                 remains = cbd->remains;
362                 cbd->init = 0;
363         }
364         ctxt = cbd->ctxt;
365
366         if (!(cat->lgh_hdr->llh_flags & cpu_to_le32(LLOG_F_IS_CAT)))
367                 RETURN(-EINVAL);
368         
369         lir = (struct llog_logid_rec *)rec;
370         logid = &lir->lid_id;
371         rc = llog_create(ctxt, &handle, logid, NULL);
372         if (rc)
373                 RETURN(-EINVAL);
374         rc = llog_init_handle(handle, 0, NULL);
375         if (rc)
376                 GOTO(out_close, rc);
377         
378         for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
379                 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
380                         count++;
381         }
382
383         l = snprintf(out, remains, "\t[Log ID]: #"LPX64"#"LPX64"#%08x\n"
384                      "\tLog Size: "LPD64"\n\tLast Index: %d\n"
385                      "\tUncanceled Records: %d\n",
386                      logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen,
387                      handle->lgh_file->f_dentry->d_inode->i_size,
388                      handle->lgh_last_idx, count);
389         out += l;
390         remains -= l;
391         cbd->out = out;
392         cbd->remains = remains;
393         if (remains <= 0) {
394                 CWARN("Not enough memory\n");
395                 rc = -ENOMEM;
396         }
397         
398 out_close:
399         llog_close(handle);
400         RETURN(rc);
401 }
402                 
403 static int llog_catinfo_deletions(struct obd_device *obd, char *buf, 
404                                   int buf_len)
405 {
406         struct mds_obd *mds = &obd->u.mds;
407         struct llog_handle *handle;
408         struct obd_run_ctxt saved;
409         int size, i, count;
410         struct llog_logid *idarray, *id;
411         char name[32] = "CATLIST";
412         int rc;
413         struct cb_data data;
414         struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
415  
416         if (ctxt == NULL || mds == NULL)
417                 RETURN(-EOPNOTSUPP);
418        
419         count = mds->mds_lov_desc.ld_tgt_count;
420         size = sizeof(*idarray) * count;
421         
422         OBD_ALLOC(idarray, size);
423         if (!idarray)
424                 RETURN(-ENOMEM);
425         memset(idarray, 0, size);
426         
427         rc = llog_get_cat_list(obd, obd, name, count, idarray);
428         if (rc) 
429                 GOTO(out_free, rc);
430
431         push_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
432         
433         id = idarray;
434         data.ctxt = ctxt;
435         data.out = buf;
436         data.remains = buf_len;
437         for (i = 0; i < count; i++) {
438                 int l, index, uncanceled = 0;
439                 rc = llog_create(ctxt, &handle, id, NULL);
440                 if (rc)
441                         GOTO(out_pop, rc);
442                 rc = llog_init_handle(handle, 0, NULL);
443                 if (rc) {
444                         llog_close(handle);
445                         GOTO(out_pop, rc = -ENOENT);
446                 }
447                 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
448                         if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
449                                 uncanceled++;
450                 }
451                 l = snprintf(data.out, data.remains, 
452                              "\n[Catlog ID]: #"LPX64"#"LPX64"#%08x  "
453                              "[Log Count]: %d\n",
454                              id->lgl_oid, id->lgl_ogr, id->lgl_ogen,
455                              uncanceled);
456                 
457                 data.out += l;
458                 data.remains -= l;
459                 data.init = 1;
460                 
461                 llog_process(handle, llog_catinfo_cb, &data);
462                 llog_close(handle);
463                 
464                 if (data.remains <= 0)
465                         break;
466         }
467 out_pop:
468         pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_ctxt, NULL);
469 out_free:
470         OBD_FREE(idarray, size);
471         RETURN(rc);
472 }
473         
474 int llog_catinfo(struct ptlrpc_request *req)
475 {
476         struct obd_export *exp = req->rq_export;
477         struct obd_device *obd = exp->exp_obd;
478         char *keyword;
479         char *buf, *reply;
480         int rc, buf_len = LLOG_CHUNK_SIZE;
481
482         OBD_ALLOC(buf, buf_len);
483         if (buf == NULL)
484                 return -ENOMEM;
485         memset(buf, 0, buf_len);
486
487         keyword = lustre_msg_string(req->rq_reqmsg, 0, 0);
488
489         if (strcmp(keyword, "config") == 0) {
490                 char *client = lustre_msg_string(req->rq_reqmsg, 1, 0);
491                 rc = llog_catinfo_config(obd, buf, buf_len, client);
492         } else if (strcmp(keyword, "deletions") == 0) {
493                 rc = llog_catinfo_deletions(obd, buf, buf_len);
494         } else {
495                 rc = -EOPNOTSUPP;
496         }
497
498         rc = lustre_pack_reply(req, 1, &buf_len, NULL);
499         if (rc)
500                 GOTO(out_free, rc = -ENOMEM);
501
502         reply = lustre_msg_buf(req->rq_repmsg, 0, buf_len);
503         if (strlen(buf) == 0)
504                 sprintf(buf, "%s", "No log informations\n");
505         memcpy(reply, buf, buf_len);
506         
507 out_free:
508         OBD_FREE(buf, buf_len);
509         return rc;
510 }