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