Whamcloud - gitweb
land b1_5 onto HEAD
[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  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *   Author: Andreas Dilger <adilger@clusterfs.com>
6  *
7  *   This file is part of the Lustre file system, http://www.lustre.org
8  *   Lustre is a trademark of Cluster File Systems, Inc.
9  *
10  *   You may have signed or agreed to another license before downloading
11  *   this software.  If so, you are bound by the terms and conditions
12  *   of that agreement, and the following does not apply to you.  See the
13  *   LICENSE file included with this distribution for more information.
14  *
15  *   If you did not agree to a different license, then this copy of Lustre
16  *   is open source software; you can redistribute it and/or modify it
17  *   under the terms of version 2 of the GNU General Public License as
18  *   published by the Free Software Foundation.
19  *
20  *   In either case, Lustre is distributed in the hope that it will be
21  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *   license text for more details.
24  *
25  * OST<->MDS recovery logging infrastructure.
26  *
27  * Invariants in implementation:
28  * - we do not share logs among different OST<->MDS connections, so that
29  *   if an OST or MDS fails it need only look at log(s) relevant to itself
30  */
31
32 #define DEBUG_SUBSYSTEM S_LOG
33
34 #ifndef EXPORT_SYMTAB
35 #define EXPORT_SYMTAB
36 #endif
37
38 #ifndef __KERNEL__
39 #include <liblustre.h>
40 #endif
41
42 #include <obd_class.h>
43 #include <lustre_log.h>
44 #include <libcfs/list.h>
45
46 /* Create a new log handle and add it to the open list.
47  * This log handle will be closed when all of the records in it are removed.
48  *
49  * Assumes caller has already pushed us into the kernel context and is locking.
50  */
51 static struct llog_handle *llog_cat_new_log(struct llog_handle *cathandle)
52 {
53         struct llog_handle *loghandle;
54         struct llog_log_hdr *llh;
55         struct llog_logid_rec rec = { { 0 }, };
56         int rc, index, bitmap_size;
57         ENTRY;
58
59         llh = cathandle->lgh_hdr;
60         bitmap_size = LLOG_BITMAP_SIZE(llh);
61
62         index = (cathandle->lgh_last_idx + 1) % bitmap_size;
63
64         /* maximum number of available slots in catlog is bitmap_size - 2 */
65         if (llh->llh_cat_idx == index) {
66                 CERROR("no free catalog slots for log...\n");
67                 RETURN(ERR_PTR(-ENOSPC));
68         } else {
69                 if (index == 0)
70                         index = 1;
71                 if (ext2_set_bit(index, llh->llh_bitmap)) {
72                         CERROR("argh, index %u already set in log bitmap?\n",
73                                index);
74                         LBUG(); /* should never happen */
75                 }
76                 cathandle->lgh_last_idx = index;
77                 llh->llh_count++;
78                 llh->llh_tail.lrt_index = index;
79         }
80
81         rc = llog_create(cathandle->lgh_ctxt, &loghandle, NULL, NULL);
82         if (rc)
83                 RETURN(ERR_PTR(rc));
84
85         rc = llog_init_handle(loghandle,
86                               LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
87                               &cathandle->lgh_hdr->llh_tgtuuid);
88         if (rc)
89                 GOTO(out_destroy, rc);
90
91         CDEBUG(D_HA, "new recovery log "LPX64":%x for index %u of catalog "
92                LPX64"\n", loghandle->lgh_id.lgl_oid, loghandle->lgh_id.lgl_ogen,
93                index, cathandle->lgh_id.lgl_oid);
94         /* build the record for this log in the catalog */
95         rec.lid_hdr.lrh_len = sizeof(rec);
96         rec.lid_hdr.lrh_index = index;
97         rec.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
98         rec.lid_id = loghandle->lgh_id;
99         rec.lid_tail.lrt_len = sizeof(rec);
100         rec.lid_tail.lrt_index = index;
101
102         /* update the catalog: header and record */
103         rc = llog_write_rec(cathandle, &rec.lid_hdr,
104                             &loghandle->u.phd.phd_cookie, 1, NULL, index);
105         if (rc < 0) {
106                 GOTO(out_destroy, rc);
107         }
108
109         loghandle->lgh_hdr->llh_cat_idx = index;
110         cathandle->u.chd.chd_current_log = loghandle;
111         LASSERT(list_empty(&loghandle->u.phd.phd_entry));
112         list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
113
114  out_destroy:
115         if (rc < 0)
116                 llog_destroy(loghandle);
117
118         RETURN(loghandle);
119 }
120 EXPORT_SYMBOL(llog_cat_new_log);
121
122 /* Open an existent log handle and add it to the open list.
123  * This log handle will be closed when all of the records in it are removed.
124  *
125  * Assumes caller has already pushed us into the kernel context and is locking.
126  * We return a lock on the handle to ensure nobody yanks it from us.
127  */
128 int llog_cat_id2handle(struct llog_handle *cathandle, struct llog_handle **res,
129                        struct llog_logid *logid)
130 {
131         struct llog_handle *loghandle;
132         int rc = 0;
133         ENTRY;
134
135         if (cathandle == NULL)
136                 RETURN(-EBADF);
137
138         list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
139                             u.phd.phd_entry) {
140                 struct llog_logid *cgl = &loghandle->lgh_id;
141                 if (cgl->lgl_oid == logid->lgl_oid) {
142                         if (cgl->lgl_ogen != logid->lgl_ogen) {
143                                 CERROR("log "LPX64" generation %x != %x\n",
144                                        logid->lgl_oid, cgl->lgl_ogen,
145                                        logid->lgl_ogen);
146                                 continue;
147                         }
148                         loghandle->u.phd.phd_cat_handle = cathandle;
149                         GOTO(out, rc = 0);
150                 }
151         }
152
153         rc = llog_create(cathandle->lgh_ctxt, &loghandle, logid, NULL);
154         if (rc) {
155                 CERROR("error opening log id "LPX64":%x: rc %d\n",
156                        logid->lgl_oid, logid->lgl_ogen, rc);
157         } else {
158                 rc = llog_init_handle(loghandle, LLOG_F_IS_PLAIN, NULL);
159                 if (!rc) {
160                         list_add(&loghandle->u.phd.phd_entry,
161                                  &cathandle->u.chd.chd_head);
162                 }
163         }
164         if (!rc) {
165                 loghandle->u.phd.phd_cat_handle = cathandle;
166                 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
167                 loghandle->u.phd.phd_cookie.lgc_index = 
168                         loghandle->lgh_hdr->llh_cat_idx;
169         }
170
171 out:
172         *res = loghandle;
173         RETURN(rc);
174 }
175
176 int llog_cat_put(struct llog_handle *cathandle)
177 {
178         struct llog_handle *loghandle, *n;
179         int rc;
180         ENTRY;
181
182         list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
183                                  u.phd.phd_entry) {
184                 int err = llog_close(loghandle);
185                 if (err)
186                         CERROR("error closing loghandle\n");
187         }
188         rc = llog_close(cathandle);
189         RETURN(rc);
190 }
191 EXPORT_SYMBOL(llog_cat_put);
192
193 /* Return the currently active log handle.  If the current log handle doesn't
194  * have enough space left for the current record, start a new one.
195  *
196  * If reclen is 0, we only want to know what the currently active log is,
197  * otherwise we get a lock on this log so nobody can steal our space.
198  *
199  * Assumes caller has already pushed us into the kernel context and is locking.
200  *
201  * NOTE: loghandle is write-locked upon successful return
202  */
203 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
204                                                 int create)
205 {
206         struct llog_handle *loghandle = NULL;
207         ENTRY;
208
209         down_read(&cathandle->lgh_lock);
210         loghandle = cathandle->u.chd.chd_current_log;
211         if (loghandle) {
212                 struct llog_log_hdr *llh = loghandle->lgh_hdr;
213                 down_write(&loghandle->lgh_lock);
214                 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
215                         up_read(&cathandle->lgh_lock);
216                         RETURN(loghandle);
217                 } else {
218                         up_write(&loghandle->lgh_lock);
219                 }
220         }
221         if (!create) {
222                 if (loghandle)
223                         down_write(&loghandle->lgh_lock);
224                 up_read(&cathandle->lgh_lock);
225                 RETURN(loghandle);
226         }
227         up_read(&cathandle->lgh_lock);
228
229         /* time to create new log */
230
231         /* first, we have to make sure the state hasn't changed */
232         down_write(&cathandle->lgh_lock);
233         loghandle = cathandle->u.chd.chd_current_log;
234         if (loghandle) {
235                 struct llog_log_hdr *llh = loghandle->lgh_hdr;
236                 down_write(&loghandle->lgh_lock);
237                 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
238                         up_write(&cathandle->lgh_lock);
239                         RETURN(loghandle);
240                 } else {
241                         up_write(&loghandle->lgh_lock);
242                 }
243         }
244
245         CDEBUG(D_INODE, "creating new log\n");
246         loghandle = llog_cat_new_log(cathandle);
247         if (!IS_ERR(loghandle))
248                 down_write(&loghandle->lgh_lock);
249         up_write(&cathandle->lgh_lock);
250         RETURN(loghandle);
251 }
252
253 /* Add a single record to the recovery log(s) using a catalog
254  * Returns as llog_write_record
255  *
256  * Assumes caller has already pushed us into the kernel context.
257  */
258 int llog_cat_add_rec(struct llog_handle *cathandle, struct llog_rec_hdr *rec,
259                      struct llog_cookie *reccookie, void *buf)
260 {
261         struct llog_handle *loghandle;
262         int rc;
263         ENTRY;
264
265         LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
266         loghandle = llog_cat_current_log(cathandle, 1);
267         if (IS_ERR(loghandle))
268                 RETURN(PTR_ERR(loghandle));
269         /* loghandle is already locked by llog_cat_current_log() for us */
270         rc = llog_write_rec(loghandle, rec, reccookie, 1, buf, -1);
271         up_write(&loghandle->lgh_lock);
272         if (rc == -ENOSPC) {
273                 /* to create a new plain log */
274                 loghandle = llog_cat_current_log(cathandle, 1);
275                 if (IS_ERR(loghandle))
276                         RETURN(PTR_ERR(loghandle));
277                 rc = llog_write_rec(loghandle, rec, reccookie, 1, buf, -1);
278                 up_write(&loghandle->lgh_lock);
279         }
280
281         RETURN(rc);
282 }
283 EXPORT_SYMBOL(llog_cat_add_rec);
284
285 /* For each cookie in the cookie array, we clear the log in-use bit and either:
286  * - the log is empty, so mark it free in the catalog header and delete it
287  * - the log is not empty, just write out the log header
288  *
289  * The cookies may be in different log files, so we need to get new logs
290  * each time.
291  *
292  * Assumes caller has already pushed us into the kernel context.
293  */
294 int llog_cat_cancel_records(struct llog_handle *cathandle, int count,
295                         struct llog_cookie *cookies)
296 {
297         int i, index, rc = 0;
298         ENTRY;
299
300         down_write(&cathandle->lgh_lock);
301         for (i = 0; i < count; i++, cookies++) {
302                 struct llog_handle *loghandle;
303                 struct llog_logid *lgl = &cookies->lgc_lgl;
304
305                 rc = llog_cat_id2handle(cathandle, &loghandle, lgl);
306                 if (rc) {
307                         CERROR("Cannot find log "LPX64"\n", lgl->lgl_oid);
308                         break;
309                 }
310
311                 down_write(&loghandle->lgh_lock);
312                 rc = llog_cancel_rec(loghandle, cookies->lgc_index);
313                 up_write(&loghandle->lgh_lock);
314
315                 if (rc == 1) {          /* log has been destroyed */
316                         index = loghandle->u.phd.phd_cookie.lgc_index;
317                         if (cathandle->u.chd.chd_current_log == loghandle)
318                                 cathandle->u.chd.chd_current_log = NULL;
319                         llog_free_handle(loghandle);
320
321                         LASSERT(index);
322                         llog_cat_set_first_idx(cathandle, index);
323                         rc = llog_cancel_rec(cathandle, index);
324                         if (rc == 0)
325                                 CDEBUG(D_HA, "cancel plain log at index %u "
326                                        "of catalog "LPX64"\n",
327                                        index, cathandle->lgh_id.lgl_oid);
328                 }
329         }
330         up_write(&cathandle->lgh_lock);
331
332         RETURN(rc);
333 }
334 EXPORT_SYMBOL(llog_cat_cancel_records);
335
336 int llog_cat_process_cb(struct llog_handle *cat_llh, struct llog_rec_hdr *rec,
337                         void *data)
338 {
339         struct llog_process_data *d = data;
340         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
341         struct llog_handle *llh;
342         int rc;
343
344         ENTRY;
345         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
346                 CERROR("invalid record in catalog\n");
347                 RETURN(-EINVAL);
348         }
349         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
350                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
351                rec->lrh_index, cat_llh->lgh_id.lgl_oid);
352
353         rc = llog_cat_id2handle(cat_llh, &llh, &lir->lid_id);
354         if (rc) {
355                 CERROR("Cannot find handle for log "LPX64"\n",
356                        lir->lid_id.lgl_oid);
357                 RETURN(rc);
358         }
359
360         rc = llog_process(llh, d->lpd_cb, d->lpd_data, NULL);
361         RETURN(rc);
362 }
363
364 int llog_cat_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data)
365 {
366         struct llog_process_data d;
367         struct llog_process_cat_data cd;
368         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
369         int rc;
370         ENTRY;
371
372         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
373         d.lpd_data = data;
374         d.lpd_cb = cb;
375
376         if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
377                 CWARN("catlog "LPX64" crosses index zero\n",
378                       cat_llh->lgh_id.lgl_oid);
379
380                 cd.first_idx = llh->llh_cat_idx;
381                 cd.last_idx = 0;
382                 rc = llog_process(cat_llh, llog_cat_process_cb, &d, &cd);
383                 if (rc != 0)
384                         RETURN(rc);
385
386                 cd.first_idx = 0;
387                 cd.last_idx = cat_llh->lgh_last_idx;
388                 rc = llog_process(cat_llh, llog_cat_process_cb, &d, &cd);
389         } else {
390                 rc = llog_process(cat_llh, llog_cat_process_cb, &d, NULL);
391         }
392
393         RETURN(rc);
394 }
395 EXPORT_SYMBOL(llog_cat_process);
396
397 static int llog_cat_reverse_process_cb(struct llog_handle *cat_llh,
398                                        struct llog_rec_hdr *rec, void *data)
399 {
400         struct llog_process_data *d = data;
401         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
402         struct llog_handle *llh;
403         int rc;
404
405         if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
406                 CERROR("invalid record in catalog\n");
407                 RETURN(-EINVAL);
408         }
409         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
410                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
411                le32_to_cpu(rec->lrh_index), cat_llh->lgh_id.lgl_oid);
412
413         rc = llog_cat_id2handle(cat_llh, &llh, &lir->lid_id);
414         if (rc) {
415                 CERROR("Cannot find handle for log "LPX64"\n",
416                        lir->lid_id.lgl_oid);
417                 RETURN(rc);
418         }
419
420         rc = llog_reverse_process(llh, d->lpd_cb, d->lpd_data, NULL);
421         RETURN(rc);
422 }
423
424 int llog_cat_reverse_process(struct llog_handle *cat_llh,
425                              llog_cb_t cb, void *data)
426 {
427         struct llog_process_data d;
428         struct llog_process_cat_data cd;
429         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
430         int rc;
431         ENTRY;
432
433         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
434         d.lpd_data = data;
435         d.lpd_cb = cb;
436
437         if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
438                 CWARN("catalog "LPX64" crosses index zero\n",
439                       cat_llh->lgh_id.lgl_oid);
440
441                 cd.first_idx = 0;
442                 cd.last_idx = cat_llh->lgh_last_idx;
443                 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
444                                           &d, &cd);
445                 if (rc != 0)
446                         RETURN(rc);
447
448                 cd.first_idx = le32_to_cpu(llh->llh_cat_idx);
449                 cd.last_idx = 0;
450                 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
451                                           &d, &cd);
452         } else {
453                 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
454                                           &d, NULL);
455         }
456
457         RETURN(rc);
458 }
459 EXPORT_SYMBOL(llog_cat_reverse_process);
460
461 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
462 {
463         struct llog_log_hdr *llh = cathandle->lgh_hdr;
464         int i, bitmap_size, idx;
465         ENTRY;
466
467         bitmap_size = LLOG_BITMAP_SIZE(llh);
468         if (llh->llh_cat_idx == (index - 1)) {
469                 idx = llh->llh_cat_idx + 1;
470                 llh->llh_cat_idx = idx;
471                 if (idx == cathandle->lgh_last_idx)
472                         goto out;
473                 for (i = (index + 1) % bitmap_size;
474                      i != cathandle->lgh_last_idx;
475                      i = (i + 1) % bitmap_size) {
476                         if (!ext2_test_bit(i, llh->llh_bitmap)) {
477                                 idx = llh->llh_cat_idx + 1;
478                                 llh->llh_cat_idx = idx;
479                         } else if (i == 0) {
480                                 llh->llh_cat_idx = 0;
481                         } else {
482                                 break;
483                         }
484                 }
485 out:
486                 CDEBUG(D_HA, "set catlog "LPX64" first idx %u\n",
487                        cathandle->lgh_id.lgl_oid, llh->llh_cat_idx);
488         }
489
490         RETURN(0);
491 }
492
493 #if 0
494 /* Assumes caller has already pushed us into the kernel context. */
495 int llog_cat_init(struct llog_handle *cathandle, struct obd_uuid *tgtuuid)
496 {
497         struct llog_log_hdr *llh;
498         loff_t offset = 0;
499         int rc = 0;
500         ENTRY;
501
502         LASSERT(sizeof(*llh) == LLOG_CHUNK_SIZE);
503
504         down(&cathandle->lgh_lock);
505         llh = cathandle->lgh_hdr;
506
507         if (cathandle->lgh_file->f_dentry->d_inode->i_size == 0) {
508                 llog_write_rec(cathandle, &llh->llh_hdr, NULL, 0, NULL, 0);
509
510 write_hdr:
511                 rc = lustre_fwrite(cathandle->lgh_file, llh, LLOG_CHUNK_SIZE,
512                                    &offset);
513                 if (rc != LLOG_CHUNK_SIZE) {
514                         CERROR("error writing catalog header: rc %d\n", rc);
515                         OBD_FREE(llh, sizeof(*llh));
516                         if (rc >= 0)
517                                 rc = -ENOSPC;
518                 } else
519                         rc = 0;
520         } else {
521                 rc = lustre_fread(cathandle->lgh_file, llh, LLOG_CHUNK_SIZE,
522                                   &offset);
523                 if (rc != LLOG_CHUNK_SIZE) {
524                         CERROR("error reading catalog header: rc %d\n", rc);
525                         /* Can we do much else if the header is bad? */
526                         goto write_hdr;
527                 } else
528                         rc = 0;
529         }
530
531         cathandle->lgh_tgtuuid = &llh->llh_tgtuuid;
532         up(&cathandle->lgh_lock);
533         RETURN(rc);
534 }
535 EXPORT_SYMBOL(llog_cat_init);
536
537 #endif