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