1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001-2003 Cluster File Systems, Inc.
5 * Author: Andreas Dilger <adilger@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * OST<->MDS recovery logging infrastructure.
24 * Invariants in implementation:
25 * - we do not share logs among different OST<->MDS connections, so that
26 * if an OST or MDS fails it need only look at log(s) relevant to itself
29 #define DEBUG_SUBSYSTEM S_LOG
38 #include <liblustre.h>
41 #include <linux/obd_class.h>
42 #include <linux/lustre_log.h>
43 #include <portals/list.h>
45 /* Create a new log handle and add it to the open list.
46 * This log handle will be closed when all of the records in it are removed.
48 * Assumes caller has already pushed us into the kernel context and is locking.
50 static struct llog_handle *llog_cat_new_log(struct llog_handle *cathandle)
52 struct llog_handle *loghandle;
53 struct llog_log_hdr *llh;
54 struct llog_logid_rec rec;
55 int rc, index, bitmap_size, i;
58 rc = llog_create(cathandle->lgh_ctxt, &loghandle, NULL, NULL);
62 rc = llog_init_handle(loghandle,
63 LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
64 &cathandle->lgh_hdr->llh_tgtuuid);
66 GOTO(out_destroy, rc);
68 /* Find first free entry */
69 llh = cathandle->lgh_hdr;
70 bitmap_size = sizeof(llh->llh_bitmap) * 8;
71 for (i = 0, index = le32_to_cpu(llh->llh_count); i < bitmap_size;
74 if (ext2_set_bit(index, llh->llh_bitmap)) {
75 /* XXX This should trigger log clean up or similar */
76 CERROR("catalog index %d is still in use\n", index);
78 cathandle->lgh_last_idx = index;
79 llh->llh_count = cpu_to_le32(le32_to_cpu(llh->llh_count) + 1);
83 if (i == bitmap_size) {
84 CERROR("no free catalog slots for log...\n");
85 GOTO(out_destroy, rc = -ENOSPC);
87 CWARN("new recovery log "LPX64":%x for index %u of catalog "LPX64"\n",
88 loghandle->lgh_id.lgl_oid, loghandle->lgh_id.lgl_ogen, index,
89 cathandle->lgh_id.lgl_oid);
90 /* build the record for this log in the catalog */
91 rec.lid_hdr.lrh_len = cpu_to_le32(sizeof(rec));
92 rec.lid_hdr.lrh_index = cpu_to_le32(index);
93 rec.lid_hdr.lrh_type = cpu_to_le32(LLOG_LOGID_MAGIC);
94 rec.lid_id = loghandle->lgh_id;
95 rec.lid_tail.lrt_len = cpu_to_le32(sizeof(rec));
96 rec.lid_tail.lrt_index = cpu_to_le32(index);
98 /* update the catalog: header and record */
99 rc = llog_write_rec(cathandle, &rec.lid_hdr,
100 &loghandle->u.phd.phd_cookie, 1, NULL, index);
102 GOTO(out_destroy, rc);
105 loghandle->lgh_hdr->llh_cat_idx = cpu_to_le32(index);
106 cathandle->u.chd.chd_current_log = loghandle;
107 LASSERT(list_empty(&loghandle->u.phd.phd_entry));
108 list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
112 llog_destroy(loghandle);
116 EXPORT_SYMBOL(llog_cat_new_log);
118 /* Assumes caller has already pushed us into the kernel context and is locking.
119 * We return a lock on the handle to ensure nobody yanks it from us.
121 int llog_cat_id2handle(struct llog_handle *cathandle, struct llog_handle **res,
122 struct llog_logid *logid)
124 struct llog_handle *loghandle;
128 if (cathandle == NULL)
131 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
133 struct llog_logid *cgl = &loghandle->lgh_id;
134 if (cgl->lgl_oid == logid->lgl_oid) {
135 if (cgl->lgl_ogen != logid->lgl_ogen) {
136 CERROR("log "LPX64" generation %x != %x\n",
137 logid->lgl_oid, cgl->lgl_ogen,
141 loghandle->u.phd.phd_cat_handle = cathandle;
142 cathandle->u.chd.chd_current_log = loghandle;
147 rc = llog_create(cathandle->lgh_ctxt, &loghandle, logid, NULL);
149 CERROR("error opening log id "LPX64":%x: rc %d\n",
150 logid->lgl_oid, logid->lgl_ogen, rc);
152 rc = llog_init_handle(loghandle, LLOG_F_IS_PLAIN, NULL);
154 list_add(&loghandle->u.phd.phd_entry,
155 &cathandle->u.chd.chd_head);
156 cathandle->u.chd.chd_current_log = loghandle;
160 loghandle->u.phd.phd_cat_handle = cathandle;
161 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
162 loghandle->u.phd.phd_cookie.lgc_index =
163 le32_to_cpu(loghandle->lgh_hdr->llh_cat_idx);
171 int llog_cat_put(struct llog_handle *cathandle)
173 struct llog_handle *loghandle, *n;
177 list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
179 int err = llog_close(loghandle);
181 CERROR("error closing loghandle\n");
183 rc = llog_close(cathandle);
186 EXPORT_SYMBOL(llog_cat_put);
188 /* Return the currently active log handle. If the current log handle doesn't
189 * have enough space left for the current record, start a new one.
191 * If reclen is 0, we only want to know what the currently active log is,
192 * otherwise we get a lock on this log so nobody can steal our space.
194 * Assumes caller has already pushed us into the kernel context and is locking.
196 * NOTE: loghandle is write-locked upon successful return
198 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
201 struct llog_handle *loghandle = NULL;
204 down_read(&cathandle->lgh_lock);
205 loghandle = cathandle->u.chd.chd_current_log;
207 struct llog_log_hdr *llh = loghandle->lgh_hdr;
208 if (loghandle->lgh_last_idx < (sizeof(llh->llh_bitmap) * 8) - 1) {
209 down_write(&loghandle->lgh_lock);
210 up_read(&cathandle->lgh_lock);
216 down_write(&loghandle->lgh_lock);
217 up_read(&cathandle->lgh_lock);
220 up_read(&cathandle->lgh_lock);
222 /* time to create new log */
224 /* first, we have to make sure the state hasn't changed */
225 down_write(&cathandle->lgh_lock);
226 loghandle = cathandle->u.chd.chd_current_log;
228 struct llog_log_hdr *llh = loghandle->lgh_hdr;
229 if (loghandle->lgh_last_idx < (sizeof(llh->llh_bitmap) * 8) - 1) {
230 down_write(&loghandle->lgh_lock);
231 up_write(&cathandle->lgh_lock);
236 CDEBUG(D_INODE, "creating new log\n");
237 loghandle = llog_cat_new_log(cathandle);
239 down_write(&loghandle->lgh_lock);
240 up_write(&cathandle->lgh_lock);
244 /* Add a single record to the recovery log(s) using a catalog
245 * Returns as llog_write_record
247 * Assumes caller has already pushed us into the kernel context.
249 int llog_cat_add_rec(struct llog_handle *cathandle, struct llog_rec_hdr *rec,
250 struct llog_cookie *reccookie, void *buf)
252 struct llog_handle *loghandle;
256 LASSERT(le32_to_cpu(rec->lrh_len) <= LLOG_CHUNK_SIZE);
257 loghandle = llog_cat_current_log(cathandle, 1);
258 if (IS_ERR(loghandle))
259 RETURN(PTR_ERR(loghandle));
260 /* loghandle is already locked by llog_cat_current_log() for us */
261 rc = llog_write_rec(loghandle, rec, reccookie, 1, buf, -1);
262 up_write(&loghandle->lgh_lock);
265 EXPORT_SYMBOL(llog_cat_add_rec);
267 /* For each cookie in the cookie array, we clear the log in-use bit and either:
268 * - the log is empty, so mark it free in the catalog header and delete it
269 * - the log is not empty, just write out the log header
271 * The cookies may be in different log files, so we need to get new logs
274 * Assumes caller has already pushed us into the kernel context.
276 int llog_cat_cancel_records(struct llog_handle *cathandle, int count,
277 struct llog_cookie *cookies)
279 int i, index, rc = 0;
282 down_write(&cathandle->lgh_lock);
283 for (i = 0; i < count; i++, cookies++) {
284 struct llog_handle *loghandle;
285 struct llog_logid *lgl = &cookies->lgc_lgl;
287 rc = llog_cat_id2handle(cathandle, &loghandle, lgl);
289 CERROR("Cannot find log "LPX64"\n", lgl->lgl_oid);
293 down_write(&loghandle->lgh_lock);
294 rc = llog_cancel_rec(loghandle, cookies->lgc_index);
295 up_write(&loghandle->lgh_lock);
297 if (rc == 1) { /* log has been destroyed */
298 index = loghandle->u.phd.phd_cookie.lgc_index;
299 if (cathandle->u.chd.chd_current_log == loghandle)
300 cathandle->u.chd.chd_current_log = NULL;
301 llog_free_handle(loghandle);
304 rc = llog_cancel_rec(cathandle, index);
307 up_write(&cathandle->lgh_lock);
311 EXPORT_SYMBOL(llog_cat_cancel_records);
313 int llog_cat_process_cb(struct llog_handle *cat_llh, struct llog_rec_hdr *rec, void *data)
315 struct llog_process_data *d = data;
316 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
317 struct llog_handle *llh;
320 if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
321 CERROR("invalid record in catalog\n");
324 CWARN("processing log "LPX64":%x at index %u of catalog "LPX64"\n",
325 lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
326 le32_to_cpu(rec->lrh_index), cat_llh->lgh_id.lgl_oid);
328 rc = llog_cat_id2handle(cat_llh, &llh, &lir->lid_id);
330 CERROR("Cannot find handle for log "LPX64"\n", lir->lid_id.lgl_oid);
334 rc = llog_process(llh, d->lpd_cb, d->lpd_data);
338 int llog_cat_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data)
340 struct llog_process_data d;
346 rc = llog_process(cat_llh, llog_cat_process_cb, &d);
349 EXPORT_SYMBOL(llog_cat_process);
353 /* Assumes caller has already pushed us into the kernel context. */
354 int llog_cat_init(struct llog_handle *cathandle, struct obd_uuid *tgtuuid)
356 struct llog_log_hdr *llh;
361 LASSERT(sizeof(*llh) == LLOG_CHUNK_SIZE);
363 down(&cathandle->lgh_lock);
364 llh = cathandle->lgh_hdr;
366 if (cathandle->lgh_file->f_dentry->d_inode->i_size == 0) {
367 llog_write_rec(cathandle, &llh->llh_hdr, NULL, 0, NULL, 0);
370 rc = lustre_fwrite(cathandle->lgh_file, llh, LLOG_CHUNK_SIZE,
372 if (rc != LLOG_CHUNK_SIZE) {
373 CERROR("error writing catalog header: rc %d\n", rc);
374 OBD_FREE(llh, sizeof(*llh));
380 rc = lustre_fread(cathandle->lgh_file, llh, LLOG_CHUNK_SIZE,
382 if (rc != LLOG_CHUNK_SIZE) {
383 CERROR("error reading catalog header: rc %d\n", rc);
384 /* Can we do much else if the header is bad? */
390 cathandle->lgh_tgtuuid = &llh->llh_tgtuuid;
391 up(&cathandle->lgh_lock);
394 EXPORT_SYMBOL(llog_cat_init);