Whamcloud - gitweb
00151a69a14187ea7ae4ed7992d06ab35de07251
[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(list_empty(&loghandle->u.phd.phd_entry));
130         list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
131
132 out_destroy:
133         if (rc < 0)
134                 llog_destroy(loghandle);
135
136         RETURN(loghandle);
137 }
138
139 /* Open an existent log handle and add it to the open list.
140  * This log handle will be closed when all of the records in it are removed.
141  *
142  * Assumes caller has already pushed us into the kernel context and is locking.
143  * We return a lock on the handle to ensure nobody yanks it from us.
144  */
145 int llog_cat_id2handle(struct llog_handle *cathandle, struct llog_handle **res,
146                        struct llog_logid *logid)
147 {
148         struct llog_handle *loghandle;
149         int rc = 0;
150         ENTRY;
151
152         if (cathandle == NULL)
153                 RETURN(-EBADF);
154
155         list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
156                             u.phd.phd_entry) {
157                 struct llog_logid *cgl = &loghandle->lgh_id;
158                 if (cgl->lgl_oid == logid->lgl_oid) {
159                         if (cgl->lgl_ogen != logid->lgl_ogen) {
160                                 CERROR("log "LPX64" generation %x != %x\n",
161                                        logid->lgl_oid, cgl->lgl_ogen,
162                                        logid->lgl_ogen);
163                                 continue;
164                         }
165                         loghandle->u.phd.phd_cat_handle = cathandle;
166                         GOTO(out, rc = 0);
167                 }
168         }
169
170         rc = llog_create(cathandle->lgh_ctxt, &loghandle, logid, NULL);
171         if (rc) {
172                 CERROR("error opening log id "LPX64":%x: rc %d\n",
173                        logid->lgl_oid, logid->lgl_ogen, rc);
174         } else {
175                 rc = llog_init_handle(loghandle, LLOG_F_IS_PLAIN, NULL);
176                 if (!rc) {
177                         list_add(&loghandle->u.phd.phd_entry,
178                                  &cathandle->u.chd.chd_head);
179                 }
180         }
181         if (!rc) {
182                 loghandle->u.phd.phd_cat_handle = cathandle;
183                 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
184                 loghandle->u.phd.phd_cookie.lgc_index =
185                         loghandle->lgh_hdr->llh_cat_idx;
186         }
187
188 out:
189         *res = loghandle;
190         RETURN(rc);
191 }
192
193 int llog_cat_put(struct llog_handle *cathandle)
194 {
195         struct llog_handle *loghandle, *n;
196         int rc;
197         ENTRY;
198
199         list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
200                                  u.phd.phd_entry) {
201                 int err = llog_close(loghandle);
202                 if (err)
203                         CERROR("error closing loghandle\n");
204         }
205         rc = llog_close(cathandle);
206         RETURN(rc);
207 }
208 EXPORT_SYMBOL(llog_cat_put);
209
210 /**
211  * lockdep markers for nested struct llog_handle::lgh_lock locking.
212  */
213 enum {
214         LLOGH_CAT,
215         LLOGH_LOG
216 };
217
218 /** Return the currently active log handle.  If the current log handle doesn't
219  * have enough space left for the current record, start a new one.
220  *
221  * If reclen is 0, we only want to know what the currently active log is,
222  * otherwise we get a lock on this log so nobody can steal our space.
223  *
224  * Assumes caller has already pushed us into the kernel context and is locking.
225  *
226  * NOTE: loghandle is write-locked upon successful return
227  */
228 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
229                                                 int create)
230 {
231         struct llog_handle *loghandle = NULL;
232         ENTRY;
233
234         down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
235         loghandle = cathandle->u.chd.chd_current_log;
236         if (loghandle) {
237                 struct llog_log_hdr *llh = loghandle->lgh_hdr;
238                 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
239                 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
240                         up_read(&cathandle->lgh_lock);
241                         RETURN(loghandle);
242                 } else {
243                         up_write(&loghandle->lgh_lock);
244                 }
245         }
246         if (!create) {
247                 if (loghandle)
248                         down_write(&loghandle->lgh_lock);
249                 up_read(&cathandle->lgh_lock);
250                 RETURN(loghandle);
251         }
252         up_read(&cathandle->lgh_lock);
253
254         /* time to create new log */
255
256         /* first, we have to make sure the state hasn't changed */
257         down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
258         loghandle = cathandle->u.chd.chd_current_log;
259         if (loghandle) {
260                 struct llog_log_hdr *llh = loghandle->lgh_hdr;
261                 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
262                 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
263                         up_write(&cathandle->lgh_lock);
264                         RETURN(loghandle);
265                 } else {
266                         up_write(&loghandle->lgh_lock);
267                 }
268         }
269
270         CDEBUG(D_INODE, "creating new log\n");
271         loghandle = llog_cat_new_log(cathandle);
272         if (!IS_ERR(loghandle))
273                 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
274         up_write(&cathandle->lgh_lock);
275         RETURN(loghandle);
276 }
277
278 /* Add a single record to the recovery log(s) using a catalog
279  * Returns as llog_write_record
280  *
281  * Assumes caller has already pushed us into the kernel context.
282  */
283 int llog_cat_add_rec(struct llog_handle *cathandle, struct llog_rec_hdr *rec,
284                      struct llog_cookie *reccookie, void *buf)
285 {
286         struct llog_handle *loghandle;
287         int rc;
288         ENTRY;
289
290         LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
291         loghandle = llog_cat_current_log(cathandle, 1);
292         if (IS_ERR(loghandle))
293                 RETURN(PTR_ERR(loghandle));
294         /* loghandle is already locked by llog_cat_current_log() for us */
295         rc = llog_write_rec(loghandle, rec, reccookie, 1, buf, -1);
296         up_write(&loghandle->lgh_lock);
297         if (rc == -ENOSPC) {
298                 /* to create a new plain log */
299                 loghandle = llog_cat_current_log(cathandle, 1);
300                 if (IS_ERR(loghandle))
301                         RETURN(PTR_ERR(loghandle));
302                 rc = llog_write_rec(loghandle, rec, reccookie, 1, buf, -1);
303                 up_write(&loghandle->lgh_lock);
304         }
305
306         RETURN(rc);
307 }
308 EXPORT_SYMBOL(llog_cat_add_rec);
309
310 /* For each cookie in the cookie array, we clear the log in-use bit and either:
311  * - the log is empty, so mark it free in the catalog header and delete it
312  * - the log is not empty, just write out the log header
313  *
314  * The cookies may be in different log files, so we need to get new logs
315  * each time.
316  *
317  * Assumes caller has already pushed us into the kernel context.
318  */
319 int llog_cat_cancel_records(struct llog_handle *cathandle, int count,
320                             struct llog_cookie *cookies)
321 {
322         int i, index, rc = 0;
323         ENTRY;
324
325         down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
326         for (i = 0; i < count; i++, cookies++) {
327                 struct llog_handle *loghandle;
328                 struct llog_logid *lgl = &cookies->lgc_lgl;
329
330                 rc = llog_cat_id2handle(cathandle, &loghandle, lgl);
331                 if (rc) {
332                         CERROR("Cannot find log "LPX64"\n", lgl->lgl_oid);
333                         break;
334                 }
335
336                 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
337                 rc = llog_cancel_rec(loghandle, cookies->lgc_index);
338                 up_write(&loghandle->lgh_lock);
339
340                 if (rc == 1) {          /* log has been destroyed */
341                         index = loghandle->u.phd.phd_cookie.lgc_index;
342                         if (cathandle->u.chd.chd_current_log == loghandle)
343                                 cathandle->u.chd.chd_current_log = NULL;
344                         llog_free_handle(loghandle);
345
346                         LASSERT(index);
347                         llog_cat_set_first_idx(cathandle, index);
348                         rc = llog_cancel_rec(cathandle, index);
349                         if (rc == 0)
350                                 CDEBUG(D_RPCTRACE,"cancel plain log at index %u"
351                                        " of catalog "LPX64"\n",
352                                        index, cathandle->lgh_id.lgl_oid);
353                 }
354         }
355         up_write(&cathandle->lgh_lock);
356
357         RETURN(rc);
358 }
359 EXPORT_SYMBOL(llog_cat_cancel_records);
360
361 int llog_cat_process_cb(struct llog_handle *cat_llh, struct llog_rec_hdr *rec,
362                         void *data)
363 {
364         struct llog_process_data *d = data;
365         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
366         struct llog_handle *llh;
367         int rc;
368
369         ENTRY;
370         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
371                 CERROR("invalid record in catalog\n");
372                 RETURN(-EINVAL);
373         }
374         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
375                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
376                rec->lrh_index, cat_llh->lgh_id.lgl_oid);
377
378         rc = llog_cat_id2handle(cat_llh, &llh, &lir->lid_id);
379         if (rc) {
380                 CERROR("Cannot find handle for log "LPX64"\n",
381                        lir->lid_id.lgl_oid);
382                 RETURN(rc);
383         }
384
385         rc = llog_process(llh, d->lpd_cb, d->lpd_data, NULL);
386         RETURN(rc);
387 }
388
389 int llog_cat_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data)
390 {
391         struct llog_process_data d;
392         struct llog_process_cat_data cd;
393         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
394         int rc;
395         ENTRY;
396
397         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
398         d.lpd_data = data;
399         d.lpd_cb = cb;
400
401         if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
402                 CWARN("catlog "LPX64" crosses index zero\n",
403                       cat_llh->lgh_id.lgl_oid);
404
405                 cd.lpcd_first_idx = llh->llh_cat_idx;
406                 cd.lpcd_last_idx = 0;
407                 rc = llog_process(cat_llh, llog_cat_process_cb, &d, &cd);
408                 if (rc != 0)
409                         RETURN(rc);
410
411                 cd.lpcd_first_idx = 0;
412                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
413                 rc = llog_process(cat_llh, llog_cat_process_cb, &d, &cd);
414         } else {
415                 rc = llog_process(cat_llh, llog_cat_process_cb, &d, NULL);
416         }
417
418         RETURN(rc);
419 }
420 EXPORT_SYMBOL(llog_cat_process);
421
422 #ifdef __KERNEL__
423 int llog_cat_process_thread(void *data)
424 {
425         struct llog_process_cat_args *args = data;
426         struct llog_ctxt *ctxt = args->lpca_ctxt;
427         struct llog_handle *llh = NULL;
428         void  *cb = args->lpca_cb;
429         struct llog_logid logid;
430         int rc;
431         ENTRY;
432
433         cfs_daemonize_ctxt("ll_log_process");
434
435         logid = *(struct llog_logid *)(args->lpca_arg);
436         rc = llog_create(ctxt, &llh, &logid, NULL);
437         if (rc) {
438                 CERROR("llog_create() failed %d\n", rc);
439                 GOTO(out, rc);
440         }
441         rc = llog_init_handle(llh, LLOG_F_IS_CAT, NULL);
442         if (rc) {
443                 CERROR("llog_init_handle failed %d\n", rc);
444                 GOTO(release_llh, rc);
445         }
446
447         if (cb) {
448                 rc = llog_cat_process(llh, (llog_cb_t)cb, NULL);
449                 if (rc != LLOG_PROC_BREAK)
450                         CERROR("llog_cat_process() failed %d\n", rc);
451         } else {
452                 CWARN("No callback function for recovery\n");
453         }
454
455         /*
456          * Make sure that all cached data is sent.
457          */
458         llog_sync(ctxt, NULL);
459         GOTO(release_llh, rc);
460 release_llh:
461         rc = llog_cat_put(llh);
462         if (rc)
463                 CERROR("llog_cat_put() failed %d\n", rc);
464 out:
465         llog_ctxt_put(ctxt);
466         OBD_FREE_PTR(args);
467         return rc;
468 }
469 EXPORT_SYMBOL(llog_cat_process_thread);
470 #endif
471
472 static int llog_cat_reverse_process_cb(struct llog_handle *cat_llh,
473                                        struct llog_rec_hdr *rec, void *data)
474 {
475         struct llog_process_data *d = data;
476         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
477         struct llog_handle *llh;
478         int rc;
479
480         if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
481                 CERROR("invalid record in catalog\n");
482                 RETURN(-EINVAL);
483         }
484         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
485                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
486                le32_to_cpu(rec->lrh_index), cat_llh->lgh_id.lgl_oid);
487
488         rc = llog_cat_id2handle(cat_llh, &llh, &lir->lid_id);
489         if (rc) {
490                 CERROR("Cannot find handle for log "LPX64"\n",
491                        lir->lid_id.lgl_oid);
492                 RETURN(rc);
493         }
494
495         rc = llog_reverse_process(llh, d->lpd_cb, d->lpd_data, NULL);
496         RETURN(rc);
497 }
498
499 int llog_cat_reverse_process(struct llog_handle *cat_llh,
500                              llog_cb_t cb, void *data)
501 {
502         struct llog_process_data d;
503         struct llog_process_cat_data cd;
504         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
505         int rc;
506         ENTRY;
507
508         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
509         d.lpd_data = data;
510         d.lpd_cb = cb;
511
512         if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
513                 CWARN("catalog "LPX64" crosses index zero\n",
514                       cat_llh->lgh_id.lgl_oid);
515
516                 cd.lpcd_first_idx = 0;
517                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
518                 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
519                                           &d, &cd);
520                 if (rc != 0)
521                         RETURN(rc);
522
523                 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
524                 cd.lpcd_last_idx = 0;
525                 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
526                                           &d, &cd);
527         } else {
528                 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
529                                           &d, NULL);
530         }
531
532         RETURN(rc);
533 }
534 EXPORT_SYMBOL(llog_cat_reverse_process);
535
536 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
537 {
538         struct llog_log_hdr *llh = cathandle->lgh_hdr;
539         int i, bitmap_size, idx;
540         ENTRY;
541
542         bitmap_size = LLOG_BITMAP_SIZE(llh);
543         if (llh->llh_cat_idx == (index - 1)) {
544                 idx = llh->llh_cat_idx + 1;
545                 llh->llh_cat_idx = idx;
546                 if (idx == cathandle->lgh_last_idx)
547                         goto out;
548                 for (i = (index + 1) % bitmap_size;
549                      i != cathandle->lgh_last_idx;
550                      i = (i + 1) % bitmap_size) {
551                         if (!ext2_test_bit(i, llh->llh_bitmap)) {
552                                 idx = llh->llh_cat_idx + 1;
553                                 llh->llh_cat_idx = idx;
554                         } else if (i == 0) {
555                                 llh->llh_cat_idx = 0;
556                         } else {
557                                 break;
558                         }
559                 }
560 out:
561                 CDEBUG(D_RPCTRACE, "set catlog "LPX64" first idx %u\n",
562                        cathandle->lgh_id.lgl_oid, llh->llh_cat_idx);
563         }
564
565         RETURN(0);
566 }