Whamcloud - gitweb
b=23787 make struct lprocfs_percpu definition C99 compliant
[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 (c) 2003, 2010, Oracle and/or its affiliates. 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_ONCE(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         if (index == 0)
98                 index = 1;
99         if (ext2_set_bit(index, llh->llh_bitmap)) {
100                 CERROR("argh, index %u already set in log bitmap?\n",
101                        index);
102                 LBUG(); /* should never happen */
103         }
104         cathandle->lgh_last_idx = index;
105         llh->llh_count++;
106         llh->llh_tail.lrt_index = index;
107
108         CDEBUG(D_RPCTRACE,"new recovery log "LPX64":%x for index %u of catalog "
109                LPX64"\n", loghandle->lgh_id.lgl_oid, loghandle->lgh_id.lgl_ogen,
110                index, cathandle->lgh_id.lgl_oid);
111         /* build the record for this log in the catalog */
112         rec.lid_hdr.lrh_len = sizeof(rec);
113         rec.lid_hdr.lrh_index = index;
114         rec.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
115         rec.lid_id = loghandle->lgh_id;
116         rec.lid_tail.lrt_len = sizeof(rec);
117         rec.lid_tail.lrt_index = index;
118
119         /* update the catalog: header and record */
120         rc = llog_write_rec(cathandle, &rec.lid_hdr,
121                             &loghandle->u.phd.phd_cookie, 1, NULL, index);
122         if (rc < 0) {
123                 GOTO(out_destroy, rc);
124         }
125
126         loghandle->lgh_hdr->llh_cat_idx = index;
127         cathandle->u.chd.chd_current_log = loghandle;
128         LASSERT(list_empty(&loghandle->u.phd.phd_entry));
129         list_add_tail(&loghandle->u.phd.phd_entry, &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         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                         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         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 /* Return the currently active log handle.  If the current log handle doesn't
210  * have enough space left for the current record, start a new one.
211  *
212  * If reclen is 0, we only want to know what the currently active log is,
213  * otherwise we get a lock on this log so nobody can steal our space.
214  *
215  * Assumes caller has already pushed us into the kernel context and is locking.
216  *
217  * NOTE: loghandle is write-locked upon successful return
218  */
219 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
220                                                 int create)
221 {
222         struct llog_handle *loghandle = NULL;
223         ENTRY;
224
225         down_read(&cathandle->lgh_lock);
226         loghandle = cathandle->u.chd.chd_current_log;
227         if (loghandle) {
228                 struct llog_log_hdr *llh = loghandle->lgh_hdr;
229                 down_write(&loghandle->lgh_lock);
230                 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
231                         up_read(&cathandle->lgh_lock);
232                         RETURN(loghandle);
233                 } else {
234                         up_write(&loghandle->lgh_lock);
235                 }
236         }
237         if (!create) {
238                 if (loghandle)
239                         down_write(&loghandle->lgh_lock);
240                 up_read(&cathandle->lgh_lock);
241                 RETURN(loghandle);
242         }
243         up_read(&cathandle->lgh_lock);
244
245         /* time to create new log */
246
247         /* first, we have to make sure the state hasn't changed */
248         down_write(&cathandle->lgh_lock);
249         loghandle = cathandle->u.chd.chd_current_log;
250         if (loghandle) {
251                 struct llog_log_hdr *llh = loghandle->lgh_hdr;
252                 down_write(&loghandle->lgh_lock);
253                 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
254                         up_write(&cathandle->lgh_lock);
255                         RETURN(loghandle);
256                 } else {
257                         up_write(&loghandle->lgh_lock);
258                 }
259         }
260
261         CDEBUG(D_INODE, "creating new log\n");
262         loghandle = llog_cat_new_log(cathandle);
263         if (!IS_ERR(loghandle))
264                 down_write(&loghandle->lgh_lock);
265         up_write(&cathandle->lgh_lock);
266         RETURN(loghandle);
267 }
268
269 /* Add a single record to the recovery log(s) using a catalog
270  * Returns as llog_write_record
271  *
272  * Assumes caller has already pushed us into the kernel context.
273  */
274 int llog_cat_add_rec(struct llog_handle *cathandle, struct llog_rec_hdr *rec,
275                      struct llog_cookie *reccookie, void *buf)
276 {
277         struct llog_handle *loghandle;
278         int rc;
279         ENTRY;
280
281         LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
282         loghandle = llog_cat_current_log(cathandle, 1);
283         if (IS_ERR(loghandle))
284                 RETURN(PTR_ERR(loghandle));
285         /* loghandle is already locked by llog_cat_current_log() for us */
286         rc = llog_write_rec(loghandle, rec, reccookie, 1, buf, -1);
287         up_write(&loghandle->lgh_lock);
288         if (rc == -ENOSPC) {
289                 /* to create a new plain log */
290                 loghandle = llog_cat_current_log(cathandle, 1);
291                 if (IS_ERR(loghandle))
292                         RETURN(PTR_ERR(loghandle));
293                 rc = llog_write_rec(loghandle, rec, reccookie, 1, buf, -1);
294                 up_write(&loghandle->lgh_lock);
295         }
296
297         RETURN(rc);
298 }
299 EXPORT_SYMBOL(llog_cat_add_rec);
300
301 /* For each cookie in the cookie array, we clear the log in-use bit and either:
302  * - the log is empty, so mark it free in the catalog header and delete it
303  * - the log is not empty, just write out the log header
304  *
305  * The cookies may be in different log files, so we need to get new logs
306  * each time.
307  *
308  * Assumes caller has already pushed us into the kernel context.
309  */
310 int llog_cat_cancel_records(struct llog_handle *cathandle, int count,
311                             struct llog_cookie *cookies)
312 {
313         int i, index, rc = 0;
314         ENTRY;
315
316         down_write(&cathandle->lgh_lock);
317         for (i = 0; i < count; i++, cookies++) {
318                 struct llog_handle *loghandle;
319                 struct llog_logid *lgl = &cookies->lgc_lgl;
320
321                 rc = llog_cat_id2handle(cathandle, &loghandle, lgl);
322                 if (rc) {
323                         CERROR("Cannot find log "LPX64"\n", lgl->lgl_oid);
324                         break;
325                 }
326
327                 down_write(&loghandle->lgh_lock);
328                 rc = llog_cancel_rec(loghandle, cookies->lgc_index);
329                 up_write(&loghandle->lgh_lock);
330
331                 if (rc == 1) {          /* log has been destroyed */
332                         index = loghandle->u.phd.phd_cookie.lgc_index;
333                         if (cathandle->u.chd.chd_current_log == loghandle)
334                                 cathandle->u.chd.chd_current_log = NULL;
335                         llog_free_handle(loghandle);
336
337                         LASSERT(index);
338                         llog_cat_set_first_idx(cathandle, index);
339                         rc = llog_cancel_rec(cathandle, index);
340                         if (rc == 0)
341                                 CDEBUG(D_RPCTRACE,"cancel plain log at index %u"
342                                        " of catalog "LPX64"\n",
343                                        index, cathandle->lgh_id.lgl_oid);
344                 }
345         }
346         up_write(&cathandle->lgh_lock);
347
348         RETURN(rc);
349 }
350 EXPORT_SYMBOL(llog_cat_cancel_records);
351
352 int llog_cat_process_cb(struct llog_handle *cat_llh, struct llog_rec_hdr *rec,
353                         void *data)
354 {
355         struct llog_process_data *d = data;
356         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
357         struct llog_handle *llh;
358         int rc;
359
360         ENTRY;
361         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
362                 CERROR("invalid record in catalog\n");
363                 RETURN(-EINVAL);
364         }
365         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
366                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
367                rec->lrh_index, cat_llh->lgh_id.lgl_oid);
368
369         rc = llog_cat_id2handle(cat_llh, &llh, &lir->lid_id);
370         if (rc) {
371                 CERROR("Cannot find handle for log "LPX64"\n",
372                        lir->lid_id.lgl_oid);
373                 RETURN(rc);
374         }
375
376         rc = llog_process(llh, d->lpd_cb, d->lpd_data, NULL);
377         RETURN(rc);
378 }
379
380 int llog_cat_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data)
381 {
382         struct llog_process_data d;
383         struct llog_process_cat_data cd;
384         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
385         int rc;
386         ENTRY;
387
388         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
389         d.lpd_data = data;
390         d.lpd_cb = cb;
391
392         if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
393                 CWARN("catlog "LPX64" crosses index zero\n",
394                       cat_llh->lgh_id.lgl_oid);
395
396                 cd.lpcd_first_idx = llh->llh_cat_idx;
397                 cd.lpcd_last_idx = 0;
398                 rc = llog_process(cat_llh, llog_cat_process_cb, &d, &cd);
399                 if (rc != 0)
400                         RETURN(rc);
401
402                 cd.lpcd_first_idx = 0;
403                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
404                 rc = llog_process(cat_llh, llog_cat_process_cb, &d, &cd);
405         } else {
406                 rc = llog_process(cat_llh, llog_cat_process_cb, &d, NULL);
407         }
408
409         RETURN(rc);
410 }
411 EXPORT_SYMBOL(llog_cat_process);
412
413 #ifdef __KERNEL__
414 int llog_cat_process_thread(void *data)
415 {
416         struct llog_process_cat_args *args = data;
417         struct llog_ctxt *ctxt = args->lpca_ctxt;
418         struct llog_handle *llh = NULL;
419         llog_cb_t cb = args->lpca_cb;
420         struct llog_logid logid;
421         int rc;
422         ENTRY;
423
424         cfs_daemonize_ctxt("ll_log_process");
425
426         logid = *(struct llog_logid *)(args->lpca_arg);
427         rc = llog_create(ctxt, &llh, &logid, NULL);
428         if (rc) {
429                 CERROR("llog_create() failed %d\n", rc);
430                 GOTO(out, rc);
431         }
432         rc = llog_init_handle(llh, LLOG_F_IS_CAT, NULL);
433         if (rc) {
434                 CERROR("llog_init_handle failed %d\n", rc);
435                 GOTO(release_llh, rc);
436         }
437
438         if (cb) {
439                 rc = llog_cat_process(llh, cb, NULL);
440                 if (rc != LLOG_PROC_BREAK && rc != 0)
441                         CERROR("llog_cat_process() failed %d\n", rc);
442                 cb(llh, NULL, NULL);
443         } else {
444                 CWARN("No callback function for recovery\n");
445         }
446
447         /* 
448          * Make sure that all cached data is sent. 
449          */
450         llog_sync(ctxt, NULL);
451         GOTO(release_llh, rc);
452 release_llh:
453         rc = llog_cat_put(llh);
454         if (rc)
455                 CERROR("llog_cat_put() failed %d\n", rc);
456 out:
457         llog_ctxt_put(ctxt);
458         OBD_FREE_PTR(args);
459         return rc;
460 }
461 EXPORT_SYMBOL(llog_cat_process_thread);
462 #endif
463
464 static int llog_cat_reverse_process_cb(struct llog_handle *cat_llh,
465                                        struct llog_rec_hdr *rec, void *data)
466 {
467         struct llog_process_data *d = data;
468         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
469         struct llog_handle *llh;
470         int rc;
471
472         if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
473                 CERROR("invalid record in catalog\n");
474                 RETURN(-EINVAL);
475         }
476         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
477                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
478                le32_to_cpu(rec->lrh_index), cat_llh->lgh_id.lgl_oid);
479
480         rc = llog_cat_id2handle(cat_llh, &llh, &lir->lid_id);
481         if (rc) {
482                 CERROR("Cannot find handle for log "LPX64"\n",
483                        lir->lid_id.lgl_oid);
484                 RETURN(rc);
485         }
486
487         rc = llog_reverse_process(llh, d->lpd_cb, d->lpd_data, NULL);
488         RETURN(rc);
489 }
490
491 int llog_cat_reverse_process(struct llog_handle *cat_llh,
492                              llog_cb_t cb, void *data)
493 {
494         struct llog_process_data d;
495         struct llog_process_cat_data cd;
496         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
497         int rc;
498         ENTRY;
499
500         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
501         d.lpd_data = data;
502         d.lpd_cb = cb;
503
504         if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
505                 CWARN("catalog "LPX64" crosses index zero\n",
506                       cat_llh->lgh_id.lgl_oid);
507
508                 cd.lpcd_first_idx = 0;
509                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
510                 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
511                                           &d, &cd);
512                 if (rc != 0)
513                         RETURN(rc);
514
515                 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
516                 cd.lpcd_last_idx = 0;
517                 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
518                                           &d, &cd);
519         } else {
520                 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
521                                           &d, NULL);
522         }
523
524         RETURN(rc);
525 }
526 EXPORT_SYMBOL(llog_cat_reverse_process);
527
528 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
529 {
530         struct llog_log_hdr *llh = cathandle->lgh_hdr;
531         int i, bitmap_size, idx;
532         ENTRY;
533
534         bitmap_size = LLOG_BITMAP_SIZE(llh);
535         if (llh->llh_cat_idx == (index - 1)) {
536                 idx = llh->llh_cat_idx + 1;
537                 llh->llh_cat_idx = idx;
538                 if (idx == cathandle->lgh_last_idx)
539                         goto out;
540                 for (i = (index + 1) % bitmap_size;
541                      i != cathandle->lgh_last_idx;
542                      i = (i + 1) % bitmap_size) {
543                         if (!ext2_test_bit(i, llh->llh_bitmap)) {
544                                 idx = llh->llh_cat_idx + 1;
545                                 llh->llh_cat_idx = idx;
546                         } else if (i == 0) {
547                                 llh->llh_cat_idx = 0;
548                         } else {
549                                 break;
550                         }
551                 }
552 out:
553                 CDEBUG(D_RPCTRACE, "set catlog "LPX64" first idx %u\n",
554                        cathandle->lgh_id.lgl_oid, llh->llh_cat_idx);
555         }
556
557         RETURN(0);
558 }