Whamcloud - gitweb
Branch b1_4_mountconf
[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 the Lustre file system, http://www.lustre.org
8  *   Lustre is a trademark of Cluster File Systems, Inc.
9  *
10  *   You may have signed or agreed to another license before downloading
11  *   this software.  If so, you are bound by the terms and conditions
12  *   of that agreement, and the following does not apply to you.  See the
13  *   LICENSE file included with this distribution for more information.
14  *
15  *   If you did not agree to a different license, then this copy of Lustre
16  *   is open source software; you can redistribute it and/or modify it
17  *   under the terms of version 2 of the GNU General Public License as
18  *   published by the Free Software Foundation.
19  *
20  *   In either case, Lustre is distributed in the hope that it will be
21  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *   license text for more details.
24  *
25  *  remote api for llog - server side
26  *
27  */
28
29 #define DEBUG_SUBSYSTEM S_LOG
30
31 #ifndef EXPORT_SYMTAB
32 #define EXPORT_SYMTAB
33 #endif
34
35 #ifndef __KERNEL__
36 #include <liblustre.h>
37 #else
38 #include <linux/fs.h>
39 #endif
40
41 #include <linux/obd_class.h>
42 #include <linux/lustre_log.h>
43 #include <linux/lustre_net.h>
44 #include <libcfs/list.h>
45 #include <linux/lustre_fsfilt.h>
46
47 #ifdef __KERNEL__
48
49 int llog_origin_handle_create(struct ptlrpc_request *req)
50 {
51         struct obd_export *exp = req->rq_export;
52         struct obd_device *obd = exp->exp_obd;
53         struct obd_device *disk_obd;
54         struct llog_handle  *loghandle;
55         struct llogd_body *body;
56         struct lvfs_run_ctxt saved;
57         struct llog_logid *logid = NULL;
58         struct llog_ctxt *ctxt;
59         char * name = NULL;
60         int size = sizeof (*body);
61         int rc, rc2;
62         ENTRY;
63
64         body = lustre_swab_reqbuf(req, 0, sizeof(*body),
65                                  lustre_swab_llogd_body);
66         if (body == NULL) {
67                 CERROR ("Can't unpack llogd_body\n");
68                 GOTO(out, rc =-EFAULT);
69         }
70
71         if (body->lgd_logid.lgl_oid > 0)
72                 logid = &body->lgd_logid;
73
74         if (req->rq_reqmsg->bufcount > 1) {
75                 name = lustre_msg_string(req->rq_reqmsg, 1, 0);
76                 if (name == NULL) {
77                         CERROR("Can't unpack name\n");
78                         GOTO(out, rc = -EFAULT);
79                 }
80                 CDEBUG(D_INFO, "opening log %s\n", name);
81         }
82
83         ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
84         if (ctxt == NULL)
85                 GOTO(out, rc = -EINVAL);
86         disk_obd = ctxt->loc_exp->exp_obd;
87         push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
88
89         rc = llog_create(ctxt, &loghandle, logid, NULL, name);
90         if (rc)
91                 GOTO(out_pop, rc);
92
93         rc = lustre_pack_reply(req, 1, &size, NULL);
94         if (rc)
95                 GOTO(out_close, rc = -ENOMEM);
96
97         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
98         body->lgd_logid = loghandle->lgh_id;
99
100 out_close:
101         rc2 = llog_close(loghandle);
102         if (!rc)
103                 rc = rc2;
104 out_pop:
105         pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
106 out:
107         RETURN(rc);
108 }
109
110 int llog_origin_handle_next_block(struct ptlrpc_request *req)
111 {
112         struct obd_export *exp = req->rq_export;
113         struct obd_device *obd = exp->exp_obd;
114         struct obd_device *disk_obd;
115         struct llog_handle  *loghandle;
116         struct llogd_body *body;
117         struct lvfs_run_ctxt saved;
118         struct llog_ctxt *ctxt;
119         __u32 flags;
120         __u8 *buf;
121         void * ptr;
122         int size[] = {sizeof (*body),
123                       LLOG_CHUNK_SIZE};
124         int rc, rc2;
125         ENTRY;
126
127         body = lustre_swab_reqbuf(req, 0, sizeof(*body),
128                                   lustre_swab_llogd_body);
129         if (body == NULL) {
130                 CERROR ("Can't unpack llogd_body\n");
131                 GOTO(out, rc =-EFAULT);
132         }
133
134         OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
135         if (!buf)
136                 GOTO(out, rc = -ENOMEM);
137
138         ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
139         if (ctxt == NULL)
140                 GOTO(out, rc = -EINVAL);
141         disk_obd = ctxt->loc_exp->exp_obd;
142         push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
143
144         rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL, NULL);
145         if (rc)
146                 GOTO(out_pop, rc);
147
148         flags = body->lgd_llh_flags;
149         rc = llog_init_handle(loghandle, flags, NULL);
150         if (rc)
151                 GOTO(out_close, rc);
152
153         memset(buf, 0, LLOG_CHUNK_SIZE);
154         rc = llog_next_block(loghandle, &body->lgd_saved_index,
155                              body->lgd_index,
156                              &body->lgd_cur_offset, buf, LLOG_CHUNK_SIZE);
157         if (rc)
158                 GOTO(out_close, rc);
159
160
161         rc = lustre_pack_reply(req, 2, size, NULL);
162         if (rc)
163                 GOTO(out_close, rc = -ENOMEM);
164
165         ptr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (body));
166         memcpy(ptr, body, sizeof(*body));
167
168         ptr = lustre_msg_buf(req->rq_repmsg, 1, LLOG_CHUNK_SIZE);
169         memcpy(ptr, buf, LLOG_CHUNK_SIZE);
170
171 out_close:
172         rc2 = llog_close(loghandle);
173         if (!rc)
174                 rc = rc2;
175
176 out_pop:
177         pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
178         OBD_FREE(buf, LLOG_CHUNK_SIZE);
179 out:
180         RETURN(rc);
181 }
182
183 int llog_origin_handle_read_header(struct ptlrpc_request *req)
184 {
185         struct obd_export *exp = req->rq_export;
186         struct obd_device *obd = exp->exp_obd;
187         struct obd_device *disk_obd;
188         struct llog_handle  *loghandle;
189         struct llogd_body *body;
190         struct llog_log_hdr *hdr;
191         struct lvfs_run_ctxt saved;
192         struct llog_ctxt *ctxt;
193         __u32 flags;
194         int size[] = {sizeof (*hdr)};
195         int rc, rc2;
196         ENTRY;
197
198         body = lustre_swab_reqbuf(req, 0, sizeof(*body),
199                                   lustre_swab_llogd_body);
200         if (body == NULL) {
201                 CERROR ("Can't unpack llogd_body\n");
202                 GOTO(out, rc =-EFAULT);
203         }
204
205         ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
206         if (ctxt == NULL)
207                 GOTO(out, rc = -EINVAL);
208         disk_obd = ctxt->loc_exp->exp_obd;
209         push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
210
211         rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL, NULL);
212         if (rc)
213                 GOTO(out_pop, rc);
214
215         /* init_handle reads the header */
216         flags = body->lgd_llh_flags;
217         rc = llog_init_handle(loghandle, flags, NULL);
218         if (rc)
219                 GOTO(out_close, rc);
220
221         rc = lustre_pack_reply(req, 1, size, NULL);
222         if (rc)
223                 GOTO(out_close, rc = -ENOMEM);
224
225         hdr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*hdr));
226         memcpy(hdr, loghandle->lgh_hdr, sizeof(*hdr));
227
228 out_close:
229         rc2 = llog_close(loghandle);
230         if (!rc)
231                 rc = rc2;
232
233 out_pop:
234         pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
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 lvfs_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_lvfs_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_log(disk_obd, inode,
283                                           FSFILT_OP_CANCEL_UNLINK, NULL, 1);
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_lvfs_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 lvfs_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_lvfs_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, 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_lvfs_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 & 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, 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 lvfs_run_ctxt saved;
433         int size, i, count;
434         struct llog_catid *idarray;
435         struct llog_logid *id;
436         char name[32] = CATLIST;
437         int rc;
438         struct cb_data data;
439         struct llog_ctxt *ctxt = llog_get_context(obd, LLOG_CONFIG_ORIG_CTXT);
440
441         if (ctxt == NULL || mds == NULL)
442                 RETURN(-EOPNOTSUPP);
443
444         count = mds->mds_lov_desc.ld_tgt_count;
445         size = sizeof(*idarray) * count;
446
447         OBD_ALLOC(idarray, size);
448         if (!idarray)
449                 RETURN(-ENOMEM);
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_lvfs_ctxt, NULL);
456
457         data.ctxt = ctxt;
458         data.out = buf;
459         data.remains = buf_len;
460         for (i = 0; i < count; i++) {
461                 int l, index, uncanceled = 0;
462
463                 id = &idarray[i].lci_logid;
464                 rc = llog_create(ctxt, &handle, id, NULL, NULL);
465                 if (rc)
466                         GOTO(out_pop, rc);
467                 rc = llog_init_handle(handle, 0, NULL);
468                 if (rc) {
469                         llog_close(handle);
470                         GOTO(out_pop, rc = -ENOENT);
471                 }
472                 for (index = 1; index < (LLOG_BITMAP_BYTES * 8); index++) {
473                         if (ext2_test_bit(index, handle->lgh_hdr->llh_bitmap))
474                                 uncanceled++;
475                 }
476                 l = snprintf(data.out, data.remains,
477                              "\n[Catlog ID]: #"LPX64"#"LPX64"#%08x  "
478                              "[Log Count]: %d\n",
479                              id->lgl_oid, id->lgl_ogr, id->lgl_ogen,
480                              uncanceled);
481
482                 data.out += l;
483                 data.remains -= l;
484                 data.init = 1;
485
486                 llog_process(handle, llog_catinfo_cb, &data, NULL);
487                 llog_close(handle);
488
489                 if (data.remains <= 0)
490                         break;
491         }
492 out_pop:
493         pop_ctxt(&saved, &ctxt->loc_exp->exp_obd->obd_lvfs_ctxt, NULL);
494 out_free:
495         OBD_FREE(idarray, size);
496         RETURN(rc);
497 }
498
499 int llog_catinfo(struct ptlrpc_request *req)
500 {
501         struct obd_export *exp = req->rq_export;
502         struct obd_device *obd = exp->exp_obd;
503         char *keyword;
504         char *buf, *reply;
505         int rc, buf_len = LLOG_CHUNK_SIZE;
506
507         OBD_ALLOC(buf, buf_len);
508         if (buf == NULL)
509                 return -ENOMEM;
510         memset(buf, 0, buf_len);
511
512         keyword = lustre_msg_string(req->rq_reqmsg, 0, 0);
513
514         if (strcmp(keyword, "config") == 0) {
515                 char *client = lustre_msg_string(req->rq_reqmsg, 1, 0);
516                 rc = llog_catinfo_config(obd, buf, buf_len, client);
517         } else if (strcmp(keyword, "deletions") == 0) {
518                 rc = llog_catinfo_deletions(obd, buf, buf_len);
519         } else {
520                 rc = -EOPNOTSUPP;
521         }
522
523         rc = lustre_pack_reply(req, 1, &buf_len, NULL);
524         if (rc)
525                 GOTO(out_free, rc = -ENOMEM);
526
527         reply = lustre_msg_buf(req->rq_repmsg, 0, buf_len);
528         if (strlen(buf) == 0)
529                 sprintf(buf, "%s", "No log informations\n");
530         memcpy(reply, buf, buf_len);
531
532 out_free:
533         OBD_FREE(buf, buf_len);
534         return rc;
535 }
536
537 #else /* !__KERNEL__ */
538 int llog_origin_handle_create(struct ptlrpc_request *req)
539 {
540         LBUG();
541         return 0;
542 }
543 int llog_origin_handle_next_block(struct ptlrpc_request *req)
544 {
545         LBUG();
546         return 0;
547 }
548 int llog_origin_handle_read_header(struct ptlrpc_request *req)
549 {
550         LBUG();
551         return 0;
552 }
553 int llog_origin_handle_close(struct ptlrpc_request *req)
554 {
555         LBUG();
556         return 0;
557 }
558 int llog_origin_handle_cancel(struct ptlrpc_request *req)
559 {
560         LBUG();
561         return 0;
562 }
563 #endif