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