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