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