Whamcloud - gitweb
b=17167 libcfs: ensure all libcfs exported symbols to have cfs_ prefix
[fs/lustre-release.git] / lustre / obdclass / llog_cat.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdclass/llog_cat.c
37  *
38  * OST<->MDS recovery logging infrastructure.
39  *
40  * Invariants in implementation:
41  * - we do not share logs among different OST<->MDS connections, so that
42  *   if an OST or MDS fails it need only look at log(s) relevant to itself
43  *
44  * Author: Andreas Dilger <adilger@clusterfs.com>
45  */
46
47 #define DEBUG_SUBSYSTEM S_LOG
48
49 #ifndef EXPORT_SYMTAB
50 #define EXPORT_SYMTAB
51 #endif
52
53 #ifndef __KERNEL__
54 #include <liblustre.h>
55 #endif
56
57 #include <obd_class.h>
58 #include <lustre_log.h>
59 #include <libcfs/list.h>
60
61 /* Create a new log handle and add it to the open list.
62  * This log handle will be closed when all of the records in it are removed.
63  *
64  * Assumes caller has already pushed us into the kernel context and is locking.
65  */
66 static struct llog_handle *llog_cat_new_log(struct llog_handle *cathandle)
67 {
68         struct llog_handle *loghandle;
69         struct llog_log_hdr *llh;
70         struct llog_logid_rec rec = { { 0 }, };
71         int rc, index, bitmap_size;
72         ENTRY;
73
74         llh = cathandle->lgh_hdr;
75         bitmap_size = LLOG_BITMAP_SIZE(llh);
76
77         index = (cathandle->lgh_last_idx + 1) % bitmap_size;
78
79         /* maximum number of available slots in catlog is bitmap_size - 2 */
80         if (llh->llh_cat_idx == index) {
81                 CERROR("no free catalog slots for log...\n");
82                 RETURN(ERR_PTR(-ENOSPC));
83         }
84
85         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED))
86                 RETURN(ERR_PTR(-ENOSPC));
87
88         rc = llog_create(cathandle->lgh_ctxt, &loghandle, NULL, NULL);
89         if (rc)
90                 RETURN(ERR_PTR(rc));
91
92         rc = llog_init_handle(loghandle,
93                               LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
94                               &cathandle->lgh_hdr->llh_tgtuuid);
95         if (rc)
96                 GOTO(out_destroy, rc);
97
98         if (index == 0)
99                 index = 1;
100         if (ext2_set_bit(index, llh->llh_bitmap)) {
101                 CERROR("argh, index %u already set in log bitmap?\n",
102                        index);
103                 LBUG(); /* should never happen */
104         }
105         cathandle->lgh_last_idx = index;
106         llh->llh_count++;
107         llh->llh_tail.lrt_index = index;
108
109         CDEBUG(D_RPCTRACE,"new recovery log "LPX64":%x for index %u of catalog "
110                LPX64"\n", loghandle->lgh_id.lgl_oid, loghandle->lgh_id.lgl_ogen,
111                index, cathandle->lgh_id.lgl_oid);
112         /* build the record for this log in the catalog */
113         rec.lid_hdr.lrh_len = sizeof(rec);
114         rec.lid_hdr.lrh_index = index;
115         rec.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
116         rec.lid_id = loghandle->lgh_id;
117         rec.lid_tail.lrt_len = sizeof(rec);
118         rec.lid_tail.lrt_index = index;
119
120         /* update the catalog: header and record */
121         rc = llog_write_rec(cathandle, &rec.lid_hdr,
122                             &loghandle->u.phd.phd_cookie, 1, NULL, index);
123         if (rc < 0) {
124                 GOTO(out_destroy, rc);
125         }
126
127         loghandle->lgh_hdr->llh_cat_idx = index;
128         cathandle->u.chd.chd_current_log = loghandle;
129         LASSERT(cfs_list_empty(&loghandle->u.phd.phd_entry));
130         cfs_list_add_tail(&loghandle->u.phd.phd_entry,
131                           &cathandle->u.chd.chd_head);
132
133 out_destroy:
134         if (rc < 0)
135                 llog_destroy(loghandle);
136
137         RETURN(loghandle);
138 }
139
140 /* Open an existent log handle and add it to the open list.
141  * This log handle will be closed when all of the records in it are removed.
142  *
143  * Assumes caller has already pushed us into the kernel context and is locking.
144  * We return a lock on the handle to ensure nobody yanks it from us.
145  */
146 int llog_cat_id2handle(struct llog_handle *cathandle, struct llog_handle **res,
147                        struct llog_logid *logid)
148 {
149         struct llog_handle *loghandle;
150         int rc = 0;
151         ENTRY;
152
153         if (cathandle == NULL)
154                 RETURN(-EBADF);
155
156         cfs_list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
157                                 u.phd.phd_entry) {
158                 struct llog_logid *cgl = &loghandle->lgh_id;
159                 if (cgl->lgl_oid == logid->lgl_oid) {
160                         if (cgl->lgl_ogen != logid->lgl_ogen) {
161                                 CERROR("log "LPX64" generation %x != %x\n",
162                                        logid->lgl_oid, cgl->lgl_ogen,
163                                        logid->lgl_ogen);
164                                 continue;
165                         }
166                         loghandle->u.phd.phd_cat_handle = cathandle;
167                         GOTO(out, rc = 0);
168                 }
169         }
170
171         rc = llog_create(cathandle->lgh_ctxt, &loghandle, logid, NULL);
172         if (rc) {
173                 CERROR("error opening log id "LPX64":%x: rc %d\n",
174                        logid->lgl_oid, logid->lgl_ogen, rc);
175         } else {
176                 rc = llog_init_handle(loghandle, LLOG_F_IS_PLAIN, NULL);
177                 if (!rc) {
178                         cfs_list_add(&loghandle->u.phd.phd_entry,
179                                      &cathandle->u.chd.chd_head);
180                 }
181         }
182         if (!rc) {
183                 loghandle->u.phd.phd_cat_handle = cathandle;
184                 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
185                 loghandle->u.phd.phd_cookie.lgc_index =
186                         loghandle->lgh_hdr->llh_cat_idx;
187         }
188
189 out:
190         *res = loghandle;
191         RETURN(rc);
192 }
193
194 int llog_cat_put(struct llog_handle *cathandle)
195 {
196         struct llog_handle *loghandle, *n;
197         int rc;
198         ENTRY;
199
200         cfs_list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
201                                      u.phd.phd_entry) {
202                 int err = llog_close(loghandle);
203                 if (err)
204                         CERROR("error closing loghandle\n");
205         }
206         rc = llog_close(cathandle);
207         RETURN(rc);
208 }
209 EXPORT_SYMBOL(llog_cat_put);
210
211 /**
212  * lockdep markers for nested struct llog_handle::lgh_lock locking.
213  */
214 enum {
215         LLOGH_CAT,
216         LLOGH_LOG
217 };
218
219 /** Return the currently active log handle.  If the current log handle doesn't
220  * have enough space left for the current record, start a new one.
221  *
222  * If reclen is 0, we only want to know what the currently active log is,
223  * otherwise we get a lock on this log so nobody can steal our space.
224  *
225  * Assumes caller has already pushed us into the kernel context and is locking.
226  *
227  * NOTE: loghandle is write-locked upon successful return
228  */
229 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
230                                                 int create)
231 {
232         struct llog_handle *loghandle = NULL;
233         ENTRY;
234
235         cfs_down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
236         loghandle = cathandle->u.chd.chd_current_log;
237         if (loghandle) {
238                 struct llog_log_hdr *llh = loghandle->lgh_hdr;
239                 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
240                 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
241                         cfs_up_read(&cathandle->lgh_lock);
242                         RETURN(loghandle);
243                 } else {
244                         cfs_up_write(&loghandle->lgh_lock);
245                 }
246         }
247         if (!create) {
248                 if (loghandle)
249                         cfs_down_write(&loghandle->lgh_lock);
250                 cfs_up_read(&cathandle->lgh_lock);
251                 RETURN(loghandle);
252         }
253         cfs_up_read(&cathandle->lgh_lock);
254
255         /* time to create new log */
256
257         /* first, we have to make sure the state hasn't changed */
258         cfs_down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
259         loghandle = cathandle->u.chd.chd_current_log;
260         if (loghandle) {
261                 struct llog_log_hdr *llh = loghandle->lgh_hdr;
262                 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
263                 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
264                         cfs_up_write(&cathandle->lgh_lock);
265                         RETURN(loghandle);
266                 } else {
267                         cfs_up_write(&loghandle->lgh_lock);
268                 }
269         }
270
271         CDEBUG(D_INODE, "creating new log\n");
272         loghandle = llog_cat_new_log(cathandle);
273         if (!IS_ERR(loghandle))
274                 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
275         cfs_up_write(&cathandle->lgh_lock);
276         RETURN(loghandle);
277 }
278
279 /* Add a single record to the recovery log(s) using a catalog
280  * Returns as llog_write_record
281  *
282  * Assumes caller has already pushed us into the kernel context.
283  */
284 int llog_cat_add_rec(struct llog_handle *cathandle, struct llog_rec_hdr *rec,
285                      struct llog_cookie *reccookie, void *buf)
286 {
287         struct llog_handle *loghandle;
288         int rc;
289         ENTRY;
290
291         LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
292         loghandle = llog_cat_current_log(cathandle, 1);
293         if (IS_ERR(loghandle))
294                 RETURN(PTR_ERR(loghandle));
295         /* loghandle is already locked by llog_cat_current_log() for us */
296         rc = llog_write_rec(loghandle, rec, reccookie, 1, buf, -1);
297         if (rc < 0)
298                 CERROR("llog_write_rec %d: lh=%p\n", rc, loghandle);
299         cfs_up_write(&loghandle->lgh_lock);
300         if (rc == -ENOSPC) {
301                 /* to create a new plain log */
302                 loghandle = llog_cat_current_log(cathandle, 1);
303                 if (IS_ERR(loghandle))
304                         RETURN(PTR_ERR(loghandle));
305                 rc = llog_write_rec(loghandle, rec, reccookie, 1, buf, -1);
306                 cfs_up_write(&loghandle->lgh_lock);
307         }
308
309         RETURN(rc);
310 }
311 EXPORT_SYMBOL(llog_cat_add_rec);
312
313 /* For each cookie in the cookie array, we clear the log in-use bit and either:
314  * - the log is empty, so mark it free in the catalog header and delete it
315  * - the log is not empty, just write out the log header
316  *
317  * The cookies may be in different log files, so we need to get new logs
318  * each time.
319  *
320  * Assumes caller has already pushed us into the kernel context.
321  */
322 int llog_cat_cancel_records(struct llog_handle *cathandle, int count,
323                             struct llog_cookie *cookies)
324 {
325         int i, index, rc = 0;
326         ENTRY;
327
328         cfs_down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
329         for (i = 0; i < count; i++, cookies++) {
330                 struct llog_handle *loghandle;
331                 struct llog_logid *lgl = &cookies->lgc_lgl;
332
333                 rc = llog_cat_id2handle(cathandle, &loghandle, lgl);
334                 if (rc) {
335                         CERROR("Cannot find log "LPX64"\n", lgl->lgl_oid);
336                         break;
337                 }
338
339                 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
340                 rc = llog_cancel_rec(loghandle, cookies->lgc_index);
341                 cfs_up_write(&loghandle->lgh_lock);
342
343                 if (rc == 1) {          /* log has been destroyed */
344                         index = loghandle->u.phd.phd_cookie.lgc_index;
345                         if (cathandle->u.chd.chd_current_log == loghandle)
346                                 cathandle->u.chd.chd_current_log = NULL;
347                         llog_free_handle(loghandle);
348
349                         LASSERT(index);
350                         llog_cat_set_first_idx(cathandle, index);
351                         rc = llog_cancel_rec(cathandle, index);
352                         if (rc == 0)
353                                 CDEBUG(D_RPCTRACE,"cancel plain log at index %u"
354                                        " of catalog "LPX64"\n",
355                                        index, cathandle->lgh_id.lgl_oid);
356                 }
357         }
358         cfs_up_write(&cathandle->lgh_lock);
359
360         RETURN(rc);
361 }
362 EXPORT_SYMBOL(llog_cat_cancel_records);
363
364 int llog_cat_process_cb(struct llog_handle *cat_llh, struct llog_rec_hdr *rec,
365                         void *data)
366 {
367         struct llog_process_data *d = data;
368         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
369         struct llog_handle *llh;
370         int rc;
371
372         ENTRY;
373         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
374                 CERROR("invalid record in catalog\n");
375                 RETURN(-EINVAL);
376         }
377         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
378                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
379                rec->lrh_index, cat_llh->lgh_id.lgl_oid);
380
381         rc = llog_cat_id2handle(cat_llh, &llh, &lir->lid_id);
382         if (rc) {
383                 CERROR("Cannot find handle for log "LPX64"\n",
384                        lir->lid_id.lgl_oid);
385                 RETURN(rc);
386         }
387
388         if (rec->lrh_index < d->lpd_startcat)
389                 /* Skip processing of the logs until startcat */
390                 RETURN(0);
391
392         if (d->lpd_startidx > 0) {
393                 struct llog_process_cat_data cd;
394
395                 cd.lpcd_first_idx = d->lpd_startidx;
396                 cd.lpcd_last_idx = 0;
397                 rc = llog_process(llh, d->lpd_cb, d->lpd_data, &cd);
398                 /* Continue processing the next log from idx 0 */
399                 d->lpd_startidx = 0;
400         } else {
401                 rc = llog_process(llh, d->lpd_cb, d->lpd_data, NULL);
402         }
403
404         RETURN(rc);
405 }
406
407 int llog_cat_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data,
408                      int startcat, int startidx)
409 {
410         struct llog_process_data d;
411         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
412         int rc;
413         ENTRY;
414
415         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
416         d.lpd_data = data;
417         d.lpd_cb = cb;
418         d.lpd_startcat = startcat;
419         d.lpd_startidx = startidx;
420
421         if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
422                 struct llog_process_cat_data cd;
423
424                 CWARN("catlog "LPX64" crosses index zero\n",
425                       cat_llh->lgh_id.lgl_oid);
426
427                 cd.lpcd_first_idx = llh->llh_cat_idx;
428                 cd.lpcd_last_idx = 0;
429                 rc = llog_process(cat_llh, llog_cat_process_cb, &d, &cd);
430                 if (rc != 0)
431                         RETURN(rc);
432
433                 cd.lpcd_first_idx = 0;
434                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
435                 rc = llog_process(cat_llh, llog_cat_process_cb, &d, &cd);
436         } else {
437                 rc = llog_process(cat_llh, llog_cat_process_cb, &d, NULL);
438         }
439
440         RETURN(rc);
441 }
442 EXPORT_SYMBOL(llog_cat_process);
443
444 #ifdef __KERNEL__
445 int llog_cat_process_thread(void *data)
446 {
447         struct llog_process_cat_args *args = data;
448         struct llog_ctxt *ctxt = args->lpca_ctxt;
449         struct llog_handle *llh = NULL;
450         llog_cb_t cb = args->lpca_cb;
451         struct llog_logid logid;
452         int rc;
453         ENTRY;
454
455         cfs_daemonize_ctxt("ll_log_process");
456
457         logid = *(struct llog_logid *)(args->lpca_arg);
458         rc = llog_create(ctxt, &llh, &logid, NULL);
459         if (rc) {
460                 CERROR("llog_create() failed %d\n", rc);
461                 GOTO(out, rc);
462         }
463         rc = llog_init_handle(llh, LLOG_F_IS_CAT, NULL);
464         if (rc) {
465                 CERROR("llog_init_handle failed %d\n", rc);
466                 GOTO(release_llh, rc);
467         }
468
469         if (cb) {
470                 rc = llog_cat_process(llh, cb, NULL, 0, 0);
471                 if (rc != LLOG_PROC_BREAK && rc != 0)
472                         CERROR("llog_cat_process() failed %d\n", rc);
473                 cb(llh, NULL, NULL);
474         } else {
475                 CWARN("No callback function for recovery\n");
476         }
477
478         /*
479          * Make sure that all cached data is sent.
480          */
481         llog_sync(ctxt, NULL);
482         GOTO(release_llh, rc);
483 release_llh:
484         rc = llog_cat_put(llh);
485         if (rc)
486                 CERROR("llog_cat_put() failed %d\n", rc);
487 out:
488         llog_ctxt_put(ctxt);
489         OBD_FREE_PTR(args);
490         return rc;
491 }
492 EXPORT_SYMBOL(llog_cat_process_thread);
493 #endif
494
495 static int llog_cat_reverse_process_cb(struct llog_handle *cat_llh,
496                                        struct llog_rec_hdr *rec, void *data)
497 {
498         struct llog_process_data *d = data;
499         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
500         struct llog_handle *llh;
501         int rc;
502
503         if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
504                 CERROR("invalid record in catalog\n");
505                 RETURN(-EINVAL);
506         }
507         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
508                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
509                le32_to_cpu(rec->lrh_index), cat_llh->lgh_id.lgl_oid);
510
511         rc = llog_cat_id2handle(cat_llh, &llh, &lir->lid_id);
512         if (rc) {
513                 CERROR("Cannot find handle for log "LPX64"\n",
514                        lir->lid_id.lgl_oid);
515                 RETURN(rc);
516         }
517
518         rc = llog_reverse_process(llh, d->lpd_cb, d->lpd_data, NULL);
519         RETURN(rc);
520 }
521
522 int llog_cat_reverse_process(struct llog_handle *cat_llh,
523                              llog_cb_t cb, void *data)
524 {
525         struct llog_process_data d;
526         struct llog_process_cat_data cd;
527         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
528         int rc;
529         ENTRY;
530
531         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
532         d.lpd_data = data;
533         d.lpd_cb = cb;
534
535         if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
536                 CWARN("catalog "LPX64" crosses index zero\n",
537                       cat_llh->lgh_id.lgl_oid);
538
539                 cd.lpcd_first_idx = 0;
540                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
541                 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
542                                           &d, &cd);
543                 if (rc != 0)
544                         RETURN(rc);
545
546                 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
547                 cd.lpcd_last_idx = 0;
548                 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
549                                           &d, &cd);
550         } else {
551                 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
552                                           &d, NULL);
553         }
554
555         RETURN(rc);
556 }
557 EXPORT_SYMBOL(llog_cat_reverse_process);
558
559 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
560 {
561         struct llog_log_hdr *llh = cathandle->lgh_hdr;
562         int i, bitmap_size, idx;
563         ENTRY;
564
565         bitmap_size = LLOG_BITMAP_SIZE(llh);
566         if (llh->llh_cat_idx == (index - 1)) {
567                 idx = llh->llh_cat_idx + 1;
568                 llh->llh_cat_idx = idx;
569                 if (idx == cathandle->lgh_last_idx)
570                         goto out;
571                 for (i = (index + 1) % bitmap_size;
572                      i != cathandle->lgh_last_idx;
573                      i = (i + 1) % bitmap_size) {
574                         if (!ext2_test_bit(i, llh->llh_bitmap)) {
575                                 idx = llh->llh_cat_idx + 1;
576                                 llh->llh_cat_idx = idx;
577                         } else if (i == 0) {
578                                 llh->llh_cat_idx = 0;
579                         } else {
580                                 break;
581                         }
582                 }
583 out:
584                 CDEBUG(D_RPCTRACE, "set catlog "LPX64" first idx %u\n",
585                        cathandle->lgh_id.lgl_oid, llh->llh_cat_idx);
586         }
587
588         RETURN(0);
589 }