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