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