Whamcloud - gitweb
landing smfs.
[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, 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, 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, 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, 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, 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 = llog_get_context(obd, LLOG_CONFIG_ORIG_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         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_create(ctxt, &handle, NULL, name[i]);
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_create(ctxt, &handle, logid, NULL);
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, LLOG_CONFIG_ORIG_CTXT);
491
492         if (ctxt == NULL || mds == NULL)
493                 RETURN(-EOPNOTSUPP);
494
495         count = mds->mds_lov_desc.ld_tgt_count;
496         size = sizeof(*idarray) * count;
497
498         OBD_ALLOC(idarray, size);
499         if (!idarray)
500                 RETURN(-ENOMEM);
501
502         rc = llog_get_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops, 
503                                name, count, idarray);
504         if (rc)
505                 GOTO(out_free, rc);
506
507         push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
508
509         data.ctxt = ctxt;
510         data.out = buf;
511         data.remains = buf_len;
512         for (i = 0; i < count; i++) {
513                 int l, index, uncanceled = 0;
514
515                 id = &idarray[i].lci_logid;
516                 rc = llog_create(ctxt, &handle, id, NULL);
517                 if (rc)
518                         GOTO(out_pop, rc);
519                 rc = llog_init_handle(handle, 0, NULL);
520                 if (rc) {
521                         llog_close(handle);
522                         GOTO(out_pop, rc = -ENOENT);
523                 }
524                 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
525                         if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
526                                 uncanceled++;
527                 }
528                 l = snprintf(data.out, data.remains,
529                              "\n[Catlog ID]: #"LPX64"#"LPX64"#%08x  "
530                              "[Log Count]: %d\n",
531                              id->lgl_oid, id->lgl_ogr, id->lgl_ogen,
532                              uncanceled);
533
534                 data.out += l;
535                 data.remains -= l;
536                 data.init = 1;
537
538                 llog_process(handle, llog_catinfo_cb, &data, NULL);
539                 llog_close(handle);
540
541                 if (data.remains <= 0)
542                         break;
543         }
544 out_pop:
545         pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
546 out_free:
547         OBD_FREE(idarray, size);
548         RETURN(rc);
549 }
550
551 int llog_catinfo(struct ptlrpc_request *req)
552 {
553         struct obd_export *exp = req->rq_export;
554         struct obd_device *obd = exp->exp_obd;
555         char *keyword;
556         char *buf, *reply;
557         int rc, buf_len = LLOG_CHUNK_SIZE;
558
559         OBD_ALLOC(buf, buf_len);
560         if (buf == NULL)
561                 return -ENOMEM;
562         memset(buf, 0, buf_len);
563
564         keyword = lustre_msg_string(req->rq_reqmsg, 0, 0);
565
566         if (strcmp(keyword, "config") == 0) {
567                 char *client = lustre_msg_string(req->rq_reqmsg, 1, 0);
568                 rc = llog_catinfo_config(obd, buf, buf_len, client);
569         } else if (strcmp(keyword, "deletions") == 0) {
570                 rc = llog_catinfo_deletions(obd, buf, buf_len);
571         } else {
572                 rc = -EOPNOTSUPP;
573         }
574
575         rc = lustre_pack_reply(req, 1, &buf_len, NULL);
576         if (rc)
577                 GOTO(out_free, rc = -ENOMEM);
578
579         reply = lustre_msg_buf(req->rq_repmsg, 0, buf_len);
580         if (strlen(buf) == 0)
581                 sprintf(buf, "%s", "No log informations\n");
582         memcpy(reply, buf, buf_len);
583
584 out_free:
585         OBD_FREE(buf, buf_len);
586         return rc;
587 }
588
589 #else /* !__KERNEL__ */
590 int llog_origin_handle_create(struct ptlrpc_request *req)
591 {
592         LBUG();
593         return 0;
594 }
595 int llog_origin_handle_prev_block(struct ptlrpc_request *req)
596 {
597         LBUG();
598         return 0;
599 }
600 int llog_origin_handle_next_block(struct ptlrpc_request *req)
601 {
602         LBUG();
603         return 0;
604 }
605 int llog_origin_handle_read_header(struct ptlrpc_request *req)
606 {
607         LBUG();
608         return 0;
609 }
610 int llog_origin_handle_close(struct ptlrpc_request *req)
611 {
612         LBUG();
613         return 0;
614 }
615 int llog_origin_handle_cancel(struct ptlrpc_request *req)
616 {
617         LBUG();
618         return 0;
619 }
620 #endif