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