Whamcloud - gitweb
86f9c3067aeb821dffda7bfe2c9091c775266ad6
[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 #ifndef __KERNEL__
33 #include <liblustre.h>
34 #else
35 #include <linux/fs.h>
36 #endif
37
38 #include <linux/obd_class.h>
39 #include <linux/lustre_log.h>
40 #include <linux/lustre_net.h>
41 #include <portals/list.h>
42 #include <linux/lustre_fsfilt.h>
43
44 #ifdef __KERNEL__
45
46 int llog_origin_handle_create(struct ptlrpc_request *req)
47 {
48         struct obd_export *exp = req->rq_export;
49         struct obd_device *obd = exp->exp_obd;
50         struct llog_handle  *loghandle;
51         struct llogd_body *body;
52         struct lvfs_run_ctxt saved;
53         struct llog_logid *logid = NULL;
54         struct llog_ctxt *ctxt;
55         char * name = NULL;
56         int size = sizeof (*body);
57         int rc, rc2;
58         ENTRY;
59
60         body = lustre_swab_reqbuf(req, 0, sizeof(*body),
61                                  lustre_swab_llogd_body);
62         if (body == NULL) {
63                 CERROR ("Can't unpack llogd_body\n");
64                 GOTO(out, rc =-EFAULT);
65         }
66
67         if (body->lgd_logid.lgl_oid > 0)
68                 logid = &body->lgd_logid;
69
70         if (req->rq_reqmsg->bufcount > 1) {
71                 name = lustre_msg_string(req->rq_reqmsg, 1, 0);
72                 if (name == NULL) {
73                         CERROR("Can't unpack name\n");
74                         GOTO(out, rc = -EFAULT);
75                 }
76         }
77
78         ctxt = llog_get_context(&obd->obd_llogs, body->lgd_ctxt_idx);
79         LASSERT(ctxt != NULL);
80         push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
81
82         rc = llog_create(ctxt, &loghandle, logid, name);
83         if (rc)
84                 GOTO(out_pop, rc);
85
86         rc = lustre_pack_reply(req, 1, &size, NULL);
87         if (rc)
88                 GOTO(out_close, rc = -ENOMEM);
89
90         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
91         body->lgd_logid = loghandle->lgh_id;
92
93 out_close:
94         rc2 = llog_close(loghandle);
95         if (!rc)
96                 rc = rc2;
97 out_pop:
98         pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
99 out:
100         RETURN(rc);
101 }
102
103 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
104 {
105         struct obd_export *exp = req->rq_export;
106         struct obd_device *obd = exp->exp_obd;
107         struct llog_handle  *loghandle;
108         struct llogd_body *body;
109         struct lvfs_run_ctxt saved;
110         struct llog_ctxt *ctxt;
111         __u32 flags;
112         __u8 *buf;
113         void * ptr;
114         int rc, rc2, size[] = {sizeof (*body), LLOG_CHUNK_SIZE};
115         ENTRY;
116
117         body = lustre_swab_reqbuf(req, 0, sizeof(*body),
118                                   lustre_swab_llogd_body);
119         if (body == NULL) {
120                 CERROR ("Can't unpack llogd_body\n");
121                 GOTO(out, rc =-EFAULT);
122         }
123
124         OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
125         if (!buf)
126                 GOTO(out, rc = -ENOMEM);
127
128         ctxt = llog_get_context(&obd->obd_llogs, body->lgd_ctxt_idx);
129         LASSERT(ctxt != NULL);
130         push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
131
132         rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
133         if (rc)
134                 GOTO(out_pop, rc);
135
136         flags = body->lgd_llh_flags;
137         rc = llog_init_handle(loghandle, flags, NULL);
138         if (rc)
139                 GOTO(out_close, rc);
140
141         memset(buf, 0, LLOG_CHUNK_SIZE);
142         rc = llog_prev_block(loghandle, body->lgd_index, 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, ctxt->loc_lvfs_ctxt, NULL);
164         OBD_FREE(buf, LLOG_CHUNK_SIZE);
165 out:
166         RETURN(rc);
167 }
168
169 int llog_origin_handle_next_block(struct ptlrpc_request *req)
170 {
171         struct obd_export *exp = req->rq_export;
172         struct obd_device *obd = exp->exp_obd;
173         struct llog_handle  *loghandle;
174         struct llogd_body *body;
175         struct lvfs_run_ctxt saved;
176         struct llog_ctxt *ctxt;
177         __u32 flags;
178         __u8 *buf;
179         void * ptr;
180         int size[] = {sizeof (*body),
181                       LLOG_CHUNK_SIZE};
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->obd_llogs, body->lgd_ctxt_idx);
197         LASSERT(ctxt != NULL);
198         push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
199
200         rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
201         if (rc)
202                 GOTO(out_pop, rc);
203
204         flags = body->lgd_llh_flags;
205         rc = llog_init_handle(loghandle, flags, NULL);
206         if (rc)
207                 GOTO(out_close, rc);
208
209         memset(buf, 0, LLOG_CHUNK_SIZE);
210         rc = llog_next_block(loghandle, &body->lgd_saved_index,
211                              body->lgd_index,
212                              &body->lgd_cur_offset, buf, LLOG_CHUNK_SIZE);
213         if (rc)
214                 GOTO(out_close, rc);
215
216
217         rc = lustre_pack_reply(req, 2, size, NULL);
218         if (rc)
219                 GOTO(out_close, rc = -ENOMEM);
220
221         ptr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (body));
222         memcpy(ptr, body, sizeof(*body));
223
224         ptr = lustre_msg_buf(req->rq_repmsg, 1, LLOG_CHUNK_SIZE);
225         memcpy(ptr, buf, LLOG_CHUNK_SIZE);
226
227 out_close:
228         rc2 = llog_close(loghandle);
229         if (!rc)
230                 rc = rc2;
231
232 out_pop:
233         pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
234         OBD_FREE(buf, LLOG_CHUNK_SIZE);
235 out:
236         RETURN(rc);
237 }
238
239 int llog_origin_handle_read_header(struct ptlrpc_request *req)
240 {
241         struct obd_export *exp = req->rq_export;
242         struct obd_device *obd = exp->exp_obd;
243         struct llog_handle  *loghandle;
244         struct llogd_body *body;
245         struct llog_log_hdr *hdr;
246         struct lvfs_run_ctxt saved;
247         struct llog_ctxt *ctxt;
248         __u32 flags;
249         int size[] = {sizeof (*hdr)};
250         int rc, rc2;
251         ENTRY;
252
253         body = lustre_swab_reqbuf(req, 0, sizeof(*body),
254                                   lustre_swab_llogd_body);
255         if (body == NULL) {
256                 CERROR ("Can't unpack llogd_body\n");
257                 GOTO(out, rc =-EFAULT);
258         }
259
260         ctxt = llog_get_context(&obd->obd_llogs, body->lgd_ctxt_idx);
261         LASSERT(ctxt != NULL);
262         push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
263
264         rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
265         if (rc)
266                 GOTO(out_pop, rc);
267
268         /* init_handle reads the header */
269         flags = body->lgd_llh_flags;
270         rc = llog_init_handle(loghandle, flags, NULL);
271         if (rc)
272                 GOTO(out_close, rc);
273
274         rc = lustre_pack_reply(req, 1, size, NULL);
275         if (rc)
276                 GOTO(out_close, rc = -ENOMEM);
277
278         hdr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*hdr));
279         memcpy(hdr, loghandle->lgh_hdr, sizeof(*hdr));
280
281 out_close:
282         rc2 = llog_close(loghandle);
283         if (!rc)
284                 rc = rc2;
285
286 out_pop:
287         pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
288
289 out:
290         RETURN(rc);
291 }
292
293 int llog_origin_handle_close(struct ptlrpc_request *req)
294 {
295         int rc;
296
297         rc = 0;
298
299         RETURN(rc);
300 }
301
302 int llog_origin_handle_cancel(struct ptlrpc_request *req)
303 {
304         struct obd_device *obd = req->rq_export->exp_obd;
305         struct llog_cookie *logcookies;
306         struct llog_ctxt *ctxt;
307         int num_cookies, rc = 0, err, i;
308         struct lvfs_run_ctxt saved;
309         struct llog_handle *cathandle;
310         struct inode *inode;
311         void *handle;
312         ENTRY;
313
314         logcookies = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*logcookies));
315         num_cookies = req->rq_reqmsg->buflens[0]/sizeof(*logcookies);
316         if (logcookies == NULL || num_cookies == 0) {
317                 DEBUG_REQ(D_HA, req, "no cookies sent");
318                 RETURN(-EFAULT);
319         }
320
321         ctxt = llog_get_context(&obd->obd_llogs, logcookies->lgc_subsys);
322         if (ctxt == NULL) {
323                 CWARN("llog subsys not setup or already cleanup\n");
324                 RETURN(-ENOENT);
325         }
326
327         push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
328         for (i = 0; i < num_cookies; i++, logcookies++) {
329                 cathandle = ctxt->loc_handle;
330                 LASSERT(cathandle != NULL);
331                 inode = cathandle->lgh_file->f_dentry->d_inode;
332
333                 handle = llog_fsfilt_start(ctxt, inode,
334                                            FSFILT_OP_CANCEL_UNLINK, NULL);
335                 if (IS_ERR(handle)) {
336                         CERROR("fsfilt_start failed: %ld\n", PTR_ERR(handle));
337                         GOTO(pop_ctxt, rc = PTR_ERR(handle));
338                 }
339
340                 rc = llog_cat_cancel_records(cathandle, 1, logcookies);
341
342                 err = llog_fsfilt_commit(ctxt, inode, handle, 0);
343                 if (err) {
344                         CERROR("error committing transaction: %d\n", err);
345                         if (!rc)
346                                 rc = err;
347                         GOTO(pop_ctxt, rc);
348                 }
349         }
350 pop_ctxt:
351         pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
352         if (rc)
353                 CERROR("cancel %d llog-records failed: %d\n", num_cookies, rc);
354         else
355                 CDEBUG(D_HA, "cancel %d llog-records\n", num_cookies);
356
357         RETURN(rc);
358 }
359 EXPORT_SYMBOL(llog_origin_handle_cancel);
360
361 static int llog_catinfo_config(struct obd_device *obd, char *buf, int buf_len,
362                                char *client)
363 {
364         struct mds_obd *mds = &obd->u.mds;
365         struct llog_ctxt *ctxt;
366         struct lvfs_run_ctxt saved;
367         struct llog_handle *handle = NULL;
368         char name[4][64];
369         int rc, i, l, remains = buf_len;
370         char *out = buf;
371
372         ctxt = llog_get_context(&obd->obd_llogs, LLOG_CONFIG_ORIG_CTXT);
373         if (ctxt == NULL || mds == NULL)
374                 RETURN(-EOPNOTSUPP);
375
376         push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
377
378         sprintf(name[0], "%s", mds->mds_profile);
379         sprintf(name[1], "%s-clean", mds->mds_profile);
380         sprintf(name[2], "%s", client);
381         sprintf(name[3], "%s-clean", client);
382
383         for (i = 0; i < 4; i++) {
384                 int index, uncanceled = 0;
385                 rc = llog_create(ctxt, &handle, NULL, name[i]);
386                 if (rc)
387                         GOTO(out_pop, rc);
388                 rc = llog_init_handle(handle, 0, NULL);
389                 if (rc) {
390                         llog_close(handle);
391                         GOTO(out_pop, rc = -ENOENT);
392                 }
393
394                 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index ++) {
395                         if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
396                                 uncanceled++;
397                 }
398
399                 l = snprintf(out, remains, "[Log Name]: %s\nLog Size: %llu\n"
400                              "Last Index: %d\nUncanceled Records: %d\n\n",
401                              name[i],
402                              handle->lgh_file->f_dentry->d_inode->i_size,
403                              handle->lgh_last_idx, uncanceled);
404                 out += l;
405                 remains -= l;
406
407                 llog_close(handle);
408                 if (remains <= 0)
409                         break;
410         }
411 out_pop:
412         pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
413         RETURN(rc);
414 }
415
416 struct cb_data {
417         struct llog_ctxt *ctxt;
418         char *out;
419         int  remains;
420         int  init;
421 };
422
423 static int llog_catinfo_cb(struct llog_handle *cat,
424                            struct llog_rec_hdr *rec, void *data)
425 {
426         static char *out = NULL;
427         static int remains = 0;
428         struct llog_ctxt *ctxt;
429         struct llog_handle *handle;
430         struct llog_logid *logid;
431         struct llog_logid_rec *lir;
432         int l, rc, index, count = 0;
433         struct cb_data *cbd = (struct cb_data*)data;
434
435         if (cbd->init) {
436                 out = cbd->out;
437                 remains = cbd->remains;
438                 cbd->init = 0;
439         }
440         ctxt = cbd->ctxt;
441
442         if (!(cat->lgh_hdr->llh_flags & LLOG_F_IS_CAT))
443                 RETURN(-EINVAL);
444
445         lir = (struct llog_logid_rec *)rec;
446         logid = &lir->lid_id;
447         rc = llog_create(ctxt, &handle, logid, NULL);
448         if (rc)
449                 RETURN(-EINVAL);
450         rc = llog_init_handle(handle, 0, NULL);
451         if (rc)
452                 GOTO(out_close, rc);
453
454         for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
455                 if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
456                         count++;
457         }
458
459         l = snprintf(out, remains, "\t[Log ID]: #"LPX64"#"LPX64"#%08x\n"
460                      "\tLog Size: %llu\n\tLast Index: %d\n"
461                      "\tUncanceled Records: %d\n",
462                      logid->lgl_oid, logid->lgl_ogr, logid->lgl_ogen,
463                      handle->lgh_file->f_dentry->d_inode->i_size,
464                      handle->lgh_last_idx, count);
465         out += l;
466         remains -= l;
467         cbd->out = out;
468         cbd->remains = remains;
469         if (remains <= 0) {
470                 CWARN("Not enough memory\n");
471                 rc = -ENOMEM;
472         }
473
474 out_close:
475         llog_close(handle);
476         RETURN(rc);
477 }
478
479 static int llog_catinfo_deletions(struct obd_device *obd, char *buf,
480                                   int buf_len)
481 {
482         struct mds_obd *mds = &obd->u.mds;
483         struct llog_handle *handle;
484         struct lvfs_run_ctxt saved;
485         int size, i, count;
486         struct llog_catid *idarray;
487         struct llog_logid *id;
488         char name[32] = CATLIST;
489         int rc;
490         struct cb_data data;
491         struct llog_ctxt *ctxt = llog_get_context(&obd->obd_llogs, 
492                                                   LLOG_CONFIG_ORIG_CTXT);
493
494         if (ctxt == NULL || mds == NULL)
495                 RETURN(-EOPNOTSUPP);
496
497         count = mds->mds_lov_desc.ld_tgt_count;
498         size = sizeof(*idarray) * count;
499
500         OBD_ALLOC(idarray, size);
501         if (!idarray)
502                 RETURN(-ENOMEM);
503
504         rc = llog_get_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops, 
505                                name, count, idarray);
506         if (rc)
507                 GOTO(out_free, rc);
508
509         push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
510
511         data.ctxt = ctxt;
512         data.out = buf;
513         data.remains = buf_len;
514         for (i = 0; i < count; i++) {
515                 int l, index, uncanceled = 0;
516
517                 id = &idarray[i].lci_logid;
518                 rc = llog_create(ctxt, &handle, id, NULL);
519                 if (rc)
520                         GOTO(out_pop, rc);
521                 rc = llog_init_handle(handle, 0, NULL);
522                 if (rc) {
523                         llog_close(handle);
524                         GOTO(out_pop, rc = -ENOENT);
525                 }
526                 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
527                         if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
528                                 uncanceled++;
529                 }
530                 l = snprintf(data.out, data.remains,
531                              "\n[Catlog ID]: #"LPX64"#"LPX64"#%08x  "
532                              "[Log Count]: %d\n",
533                              id->lgl_oid, id->lgl_ogr, id->lgl_ogen,
534                              uncanceled);
535
536                 data.out += l;
537                 data.remains -= l;
538                 data.init = 1;
539
540                 llog_process(handle, llog_catinfo_cb, &data, NULL);
541                 llog_close(handle);
542
543                 if (data.remains <= 0)
544                         break;
545         }
546 out_pop:
547         pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
548 out_free:
549         OBD_FREE(idarray, size);
550         RETURN(rc);
551 }
552
553 int llog_catinfo(struct ptlrpc_request *req)
554 {
555         struct obd_export *exp = req->rq_export;
556         struct obd_device *obd = exp->exp_obd;
557         char *keyword;
558         char *buf, *reply;
559         int rc, buf_len = LLOG_CHUNK_SIZE;
560
561         OBD_ALLOC(buf, buf_len);
562         if (buf == NULL)
563                 return -ENOMEM;
564         memset(buf, 0, buf_len);
565
566         keyword = lustre_msg_string(req->rq_reqmsg, 0, 0);
567
568         if (strcmp(keyword, "config") == 0) {
569                 char *client = lustre_msg_string(req->rq_reqmsg, 1, 0);
570                 rc = llog_catinfo_config(obd, buf, buf_len, client);
571         } else if (strcmp(keyword, "deletions") == 0) {
572                 rc = llog_catinfo_deletions(obd, buf, buf_len);
573         } else {
574                 rc = -EOPNOTSUPP;
575         }
576
577         rc = lustre_pack_reply(req, 1, &buf_len, NULL);
578         if (rc)
579                 GOTO(out_free, rc = -ENOMEM);
580
581         reply = lustre_msg_buf(req->rq_repmsg, 0, buf_len);
582         if (strlen(buf) == 0)
583                 sprintf(buf, "%s", "No log informations\n");
584         memcpy(reply, buf, buf_len);
585
586 out_free:
587         OBD_FREE(buf, buf_len);
588         return rc;
589 }
590
591 #else /* !__KERNEL__ */
592 int llog_origin_handle_create(struct ptlrpc_request *req)
593 {
594         LBUG();
595         return 0;
596 }
597 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
598 {
599         LBUG();
600         return 0;
601 }
602 int llog_origin_handle_next_block(struct ptlrpc_request *req)
603 {
604         LBUG();
605         return 0;
606 }
607 int llog_origin_handle_read_header(struct ptlrpc_request *req)
608 {
609         LBUG();
610         return 0;
611 }
612 int llog_origin_handle_close(struct ptlrpc_request *req)
613 {
614         LBUG();
615         return 0;
616 }
617 int llog_origin_handle_cancel(struct ptlrpc_request *req)
618 {
619         LBUG();
620         return 0;
621 }
622 #endif