Whamcloud - gitweb
LU-1302 llog: structures changes, llog_thread_info
[fs/lustre-release.git] / lustre / obdclass / llog_cat.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/obdclass/llog_cat.c
35  *
36  * OST<->MDS recovery logging infrastructure.
37  *
38  * Invariants in implementation:
39  * - we do not share logs among different OST<->MDS connections, so that
40  *   if an OST or MDS fails it need only look at log(s) relevant to itself
41  *
42  * Author: Andreas Dilger <adilger@clusterfs.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_LOG
46
47 #ifndef __KERNEL__
48 #include <liblustre.h>
49 #endif
50
51 #include <obd_class.h>
52 #include <lustre_log.h>
53 #include <libcfs/list.h>
54
55 /* Create a new log handle and add it to the open list.
56  * This log handle will be closed when all of the records in it are removed.
57  *
58  * Assumes caller has already pushed us into the kernel context and is locking.
59  */
60 static struct llog_handle *llog_cat_new_log(struct llog_handle *cathandle)
61 {
62         struct llog_handle *loghandle;
63         struct llog_log_hdr *llh;
64         struct llog_logid_rec rec = { { 0 }, };
65         int rc, index, bitmap_size;
66         ENTRY;
67
68         llh = cathandle->lgh_hdr;
69         bitmap_size = LLOG_BITMAP_SIZE(llh);
70
71         index = (cathandle->lgh_last_idx + 1) % bitmap_size;
72
73         /* maximum number of available slots in catlog is bitmap_size - 2 */
74         if (llh->llh_cat_idx == index) {
75                 CERROR("no free catalog slots for log...\n");
76                 RETURN(ERR_PTR(-ENOSPC));
77         }
78
79         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED))
80                 RETURN(ERR_PTR(-ENOSPC));
81
82         rc = llog_create(cathandle->lgh_ctxt, &loghandle, NULL, NULL);
83         if (rc)
84                 RETURN(ERR_PTR(rc));
85
86         rc = llog_init_handle(loghandle,
87                               LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
88                               &cathandle->lgh_hdr->llh_tgtuuid);
89         if (rc)
90                 GOTO(out_destroy, rc);
91
92         if (index == 0)
93                 index = 1;
94
95         cfs_spin_lock(&loghandle->lgh_hdr_lock);
96         llh->llh_count++;
97         if (ext2_set_bit(index, llh->llh_bitmap)) {
98                 CERROR("argh, index %u already set in log bitmap?\n",
99                        index);
100                 cfs_spin_unlock(&loghandle->lgh_hdr_lock);
101                 LBUG(); /* should never happen */
102         }
103         cfs_spin_unlock(&loghandle->lgh_hdr_lock);
104
105         cathandle->lgh_last_idx = index;
106         llh->llh_tail.lrt_index = index;
107
108         CDEBUG(D_RPCTRACE,"new recovery log "LPX64":%x for index %u of catalog "
109                LPX64"\n", loghandle->lgh_id.lgl_oid, loghandle->lgh_id.lgl_ogen,
110                index, cathandle->lgh_id.lgl_oid);
111         /* build the record for this log in the catalog */
112         rec.lid_hdr.lrh_len = sizeof(rec);
113         rec.lid_hdr.lrh_index = index;
114         rec.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
115         rec.lid_id = loghandle->lgh_id;
116         rec.lid_tail.lrt_len = sizeof(rec);
117         rec.lid_tail.lrt_index = index;
118
119         /* update the catalog: header and record */
120         rc = llog_write_rec(cathandle, &rec.lid_hdr,
121                             &loghandle->u.phd.phd_cookie, 1, NULL, index);
122         if (rc < 0) {
123                 GOTO(out_destroy, rc);
124         }
125
126         loghandle->lgh_hdr->llh_cat_idx = index;
127         cathandle->u.chd.chd_current_log = loghandle;
128         LASSERT(cfs_list_empty(&loghandle->u.phd.phd_entry));
129         cfs_list_add_tail(&loghandle->u.phd.phd_entry,
130                           &cathandle->u.chd.chd_head);
131
132 out_destroy:
133         if (rc < 0)
134                 llog_destroy(loghandle);
135
136         RETURN(loghandle);
137 }
138
139 /* Open an existent log handle and add it to the open list.
140  * This log handle will be closed when all of the records in it are removed.
141  *
142  * Assumes caller has already pushed us into the kernel context and is locking.
143  * We return a lock on the handle to ensure nobody yanks it from us.
144  */
145 int llog_cat_id2handle(struct llog_handle *cathandle, struct llog_handle **res,
146                        struct llog_logid *logid)
147 {
148         struct llog_handle *loghandle;
149         int rc = 0;
150         ENTRY;
151
152         if (cathandle == NULL)
153                 RETURN(-EBADF);
154
155         cfs_list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
156                                 u.phd.phd_entry) {
157                 struct llog_logid *cgl = &loghandle->lgh_id;
158                 if (cgl->lgl_oid == logid->lgl_oid) {
159                         if (cgl->lgl_ogen != logid->lgl_ogen) {
160                                 CERROR("log "LPX64" generation %x != %x\n",
161                                        logid->lgl_oid, cgl->lgl_ogen,
162                                        logid->lgl_ogen);
163                                 continue;
164                         }
165                         loghandle->u.phd.phd_cat_handle = cathandle;
166                         GOTO(out, rc = 0);
167                 }
168         }
169
170         rc = llog_create(cathandle->lgh_ctxt, &loghandle, logid, NULL);
171         if (rc) {
172                 CERROR("error opening log id "LPX64":%x: rc %d\n",
173                        logid->lgl_oid, logid->lgl_ogen, rc);
174         } else {
175                 rc = llog_init_handle(loghandle, LLOG_F_IS_PLAIN, NULL);
176                 if (!rc) {
177                         cfs_list_add(&loghandle->u.phd.phd_entry,
178                                      &cathandle->u.chd.chd_head);
179                 }
180         }
181         if (!rc) {
182                 loghandle->u.phd.phd_cat_handle = cathandle;
183                 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
184                 loghandle->u.phd.phd_cookie.lgc_index =
185                         loghandle->lgh_hdr->llh_cat_idx;
186         }
187
188 out:
189         *res = loghandle;
190         RETURN(rc);
191 }
192
193 int llog_cat_put(struct llog_handle *cathandle)
194 {
195         struct llog_handle *loghandle, *n;
196         int rc;
197         ENTRY;
198
199         cfs_list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
200                                      u.phd.phd_entry) {
201                 int err = llog_close(loghandle);
202                 if (err)
203                         CERROR("error closing loghandle\n");
204         }
205         rc = llog_close(cathandle);
206         RETURN(rc);
207 }
208 EXPORT_SYMBOL(llog_cat_put);
209
210 /**
211  * lockdep markers for nested struct llog_handle::lgh_lock locking.
212  */
213 enum {
214         LLOGH_CAT,
215         LLOGH_LOG
216 };
217
218 /** Return the currently active log handle.  If the current log handle doesn't
219  * have enough space left for the current record, start a new one.
220  *
221  * If reclen is 0, we only want to know what the currently active log is,
222  * otherwise we get a lock on this log so nobody can steal our space.
223  *
224  * Assumes caller has already pushed us into the kernel context and is locking.
225  *
226  * NOTE: loghandle is write-locked upon successful return
227  */
228 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
229                                                 int create)
230 {
231         struct llog_handle *loghandle = NULL;
232         ENTRY;
233
234         cfs_down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
235         loghandle = cathandle->u.chd.chd_current_log;
236         if (loghandle) {
237                 struct llog_log_hdr *llh = loghandle->lgh_hdr;
238                 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
239                 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
240                         cfs_up_read(&cathandle->lgh_lock);
241                         RETURN(loghandle);
242                 } else {
243                         cfs_up_write(&loghandle->lgh_lock);
244                 }
245         }
246         if (!create) {
247                 if (loghandle)
248                         cfs_down_write(&loghandle->lgh_lock);
249                 cfs_up_read(&cathandle->lgh_lock);
250                 RETURN(loghandle);
251         }
252         cfs_up_read(&cathandle->lgh_lock);
253
254         /* time to create new log */
255
256         /* first, we have to make sure the state hasn't changed */
257         cfs_down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
258         loghandle = cathandle->u.chd.chd_current_log;
259         if (loghandle) {
260                 struct llog_log_hdr *llh = loghandle->lgh_hdr;
261                 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
262                 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
263                         cfs_up_write(&cathandle->lgh_lock);
264                         RETURN(loghandle);
265                 } else {
266                         cfs_up_write(&loghandle->lgh_lock);
267                 }
268         }
269
270         CDEBUG(D_INODE, "creating new log\n");
271         loghandle = llog_cat_new_log(cathandle);
272         if (!IS_ERR(loghandle))
273                 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
274         cfs_up_write(&cathandle->lgh_lock);
275         RETURN(loghandle);
276 }
277
278 /* Add a single record to the recovery log(s) using a catalog
279  * Returns as llog_write_record
280  *
281  * Assumes caller has already pushed us into the kernel context.
282  */
283 int llog_cat_add_rec(struct llog_handle *cathandle, struct llog_rec_hdr *rec,
284                      struct llog_cookie *reccookie, void *buf)
285 {
286         struct llog_handle *loghandle;
287         int rc;
288         ENTRY;
289
290         LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
291         loghandle = llog_cat_current_log(cathandle, 1);
292         if (IS_ERR(loghandle))
293                 RETURN(PTR_ERR(loghandle));
294         /* loghandle is already locked by llog_cat_current_log() for us */
295         rc = llog_write_rec(loghandle, rec, reccookie, 1, buf, -1);
296         if (rc < 0)
297                 CERROR("llog_write_rec %d: lh=%p\n", rc, loghandle);
298         cfs_up_write(&loghandle->lgh_lock);
299         if (rc == -ENOSPC) {
300                 /* to create a new plain log */
301                 loghandle = llog_cat_current_log(cathandle, 1);
302                 if (IS_ERR(loghandle))
303                         RETURN(PTR_ERR(loghandle));
304                 rc = llog_write_rec(loghandle, rec, reccookie, 1, buf, -1);
305                 cfs_up_write(&loghandle->lgh_lock);
306         }
307
308         RETURN(rc);
309 }
310 EXPORT_SYMBOL(llog_cat_add_rec);
311
312 /* For each cookie in the cookie array, we clear the log in-use bit and either:
313  * - the log is empty, so mark it free in the catalog header and delete it
314  * - the log is not empty, just write out the log header
315  *
316  * The cookies may be in different log files, so we need to get new logs
317  * each time.
318  *
319  * Assumes caller has already pushed us into the kernel context.
320  */
321 int llog_cat_cancel_records(struct llog_handle *cathandle, int count,
322                             struct llog_cookie *cookies)
323 {
324         int i, index, rc = 0;
325         ENTRY;
326
327         cfs_down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
328         for (i = 0; i < count; i++, cookies++) {
329                 struct llog_handle *loghandle;
330                 struct llog_logid *lgl = &cookies->lgc_lgl;
331
332                 rc = llog_cat_id2handle(cathandle, &loghandle, lgl);
333                 if (rc) {
334                         CERROR("Cannot find log "LPX64"\n", lgl->lgl_oid);
335                         break;
336                 }
337
338                 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
339                 rc = llog_cancel_rec(loghandle, cookies->lgc_index);
340                 cfs_up_write(&loghandle->lgh_lock);
341
342                 if (rc == 1) {          /* log has been destroyed */
343                         index = loghandle->u.phd.phd_cookie.lgc_index;
344                         if (cathandle->u.chd.chd_current_log == loghandle)
345                                 cathandle->u.chd.chd_current_log = NULL;
346                         llog_free_handle(loghandle);
347
348                         LASSERT(index);
349                         llog_cat_set_first_idx(cathandle, index);
350                         rc = llog_cancel_rec(cathandle, index);
351                         if (rc == 0)
352                                 CDEBUG(D_RPCTRACE,"cancel plain log at index %u"
353                                        " of catalog "LPX64"\n",
354                                        index, cathandle->lgh_id.lgl_oid);
355                 }
356         }
357         cfs_up_write(&cathandle->lgh_lock);
358
359         RETURN(rc);
360 }
361 EXPORT_SYMBOL(llog_cat_cancel_records);
362
363 int llog_cat_process_cb(struct llog_handle *cat_llh, struct llog_rec_hdr *rec,
364                         void *data)
365 {
366         struct llog_process_data *d = data;
367         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
368         struct llog_handle *llh;
369         int rc;
370
371         ENTRY;
372         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
373                 CERROR("invalid record in catalog\n");
374                 RETURN(-EINVAL);
375         }
376         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
377                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
378                rec->lrh_index, cat_llh->lgh_id.lgl_oid);
379
380         rc = llog_cat_id2handle(cat_llh, &llh, &lir->lid_id);
381         if (rc) {
382                 CERROR("Cannot find handle for log "LPX64"\n",
383                        lir->lid_id.lgl_oid);
384                 RETURN(rc);
385         }
386
387         if (rec->lrh_index < d->lpd_startcat)
388                 /* Skip processing of the logs until startcat */
389                 RETURN(0);
390
391         if (d->lpd_startidx > 0) {
392                 struct llog_process_cat_data cd;
393
394                 cd.lpcd_first_idx = d->lpd_startidx;
395                 cd.lpcd_last_idx = 0;
396                 rc = llog_process_flags(llh, d->lpd_cb, d->lpd_data, &cd,
397                                         d->lpd_flags);
398                 /* Continue processing the next log from idx 0 */
399                 d->lpd_startidx = 0;
400         } else {
401                 rc = llog_process_flags(llh, d->lpd_cb, d->lpd_data, NULL,
402                                         d->lpd_flags);
403         }
404
405         RETURN(rc);
406 }
407
408 int llog_cat_process_flags(struct llog_handle *cat_llh, llog_cb_t cb,
409                            void *data, int flags, int startcat, int startidx)
410 {
411         struct llog_process_data d;
412         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
413         int rc;
414         ENTRY;
415
416         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
417         d.lpd_data = data;
418         d.lpd_cb = cb;
419         d.lpd_startcat = startcat;
420         d.lpd_startidx = startidx;
421         d.lpd_flags = flags;
422
423         if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
424                 struct llog_process_cat_data cd;
425
426                 CWARN("catlog "LPX64" crosses index zero\n",
427                       cat_llh->lgh_id.lgl_oid);
428
429                 cd.lpcd_first_idx = llh->llh_cat_idx;
430                 cd.lpcd_last_idx = 0;
431                 rc = llog_process_flags(cat_llh, llog_cat_process_cb, &d, &cd,
432                                         flags);
433                 if (rc != 0)
434                         RETURN(rc);
435
436                 cd.lpcd_first_idx = 0;
437                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
438                 rc = llog_process_flags(cat_llh, llog_cat_process_cb, &d, &cd,
439                                         flags);
440         } else {
441                 rc = llog_process_flags(cat_llh, llog_cat_process_cb, &d, NULL,
442                                         flags);
443         }
444
445         RETURN(rc);
446 }
447 EXPORT_SYMBOL(llog_cat_process_flags);
448
449 int llog_cat_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data,
450                      int startcat, int startidx)
451 {
452         return llog_cat_process_flags(cat_llh, cb, data, 0, startcat, startidx);
453 }
454 EXPORT_SYMBOL(llog_cat_process);
455
456 #ifdef __KERNEL__
457 int llog_cat_process_thread(void *data)
458 {
459         struct llog_process_cat_args *args = data;
460         struct llog_ctxt *ctxt = args->lpca_ctxt;
461         struct llog_handle *llh = NULL;
462         llog_cb_t cb = args->lpca_cb;
463         struct llog_logid logid;
464         int rc;
465         ENTRY;
466
467         cfs_daemonize_ctxt("ll_log_process");
468
469         logid = *(struct llog_logid *)(args->lpca_arg);
470         rc = llog_create(ctxt, &llh, &logid, NULL);
471         if (rc) {
472                 CERROR("llog_create() failed %d\n", rc);
473                 GOTO(out, rc);
474         }
475         rc = llog_init_handle(llh, LLOG_F_IS_CAT, NULL);
476         if (rc) {
477                 CERROR("llog_init_handle failed %d\n", rc);
478                 GOTO(release_llh, rc);
479         }
480
481         if (cb) {
482                 rc = llog_cat_process(llh, cb, NULL, 0, 0);
483                 if (rc != LLOG_PROC_BREAK && rc != 0)
484                         CERROR("llog_cat_process() failed %d\n", rc);
485                 cb(llh, NULL, NULL);
486         } else {
487                 CWARN("No callback function for recovery\n");
488         }
489
490         /*
491          * Make sure that all cached data is sent.
492          */
493         llog_sync(ctxt, NULL, 0);
494         GOTO(release_llh, rc);
495 release_llh:
496         rc = llog_cat_put(llh);
497         if (rc)
498                 CERROR("llog_cat_put() failed %d\n", rc);
499 out:
500         llog_ctxt_put(ctxt);
501         OBD_FREE_PTR(args);
502         return rc;
503 }
504 EXPORT_SYMBOL(llog_cat_process_thread);
505 #endif
506
507 static int llog_cat_reverse_process_cb(struct llog_handle *cat_llh,
508                                        struct llog_rec_hdr *rec, void *data)
509 {
510         struct llog_process_data *d = data;
511         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
512         struct llog_handle *llh;
513         int rc;
514
515         if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
516                 CERROR("invalid record in catalog\n");
517                 RETURN(-EINVAL);
518         }
519         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
520                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
521                le32_to_cpu(rec->lrh_index), cat_llh->lgh_id.lgl_oid);
522
523         rc = llog_cat_id2handle(cat_llh, &llh, &lir->lid_id);
524         if (rc) {
525                 CERROR("Cannot find handle for log "LPX64"\n",
526                        lir->lid_id.lgl_oid);
527                 RETURN(rc);
528         }
529
530         rc = llog_reverse_process(llh, d->lpd_cb, d->lpd_data, NULL);
531         RETURN(rc);
532 }
533
534 int llog_cat_reverse_process(struct llog_handle *cat_llh,
535                              llog_cb_t cb, void *data)
536 {
537         struct llog_process_data d;
538         struct llog_process_cat_data cd;
539         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
540         int rc;
541         ENTRY;
542
543         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
544         d.lpd_data = data;
545         d.lpd_cb = cb;
546
547         if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
548                 CWARN("catalog "LPX64" crosses index zero\n",
549                       cat_llh->lgh_id.lgl_oid);
550
551                 cd.lpcd_first_idx = 0;
552                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
553                 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
554                                           &d, &cd);
555                 if (rc != 0)
556                         RETURN(rc);
557
558                 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
559                 cd.lpcd_last_idx = 0;
560                 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
561                                           &d, &cd);
562         } else {
563                 rc = llog_reverse_process(cat_llh, llog_cat_reverse_process_cb,
564                                           &d, NULL);
565         }
566
567         RETURN(rc);
568 }
569 EXPORT_SYMBOL(llog_cat_reverse_process);
570
571 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
572 {
573         struct llog_log_hdr *llh = cathandle->lgh_hdr;
574         int i, bitmap_size, idx;
575         ENTRY;
576
577         bitmap_size = LLOG_BITMAP_SIZE(llh);
578         if (llh->llh_cat_idx == (index - 1)) {
579                 idx = llh->llh_cat_idx + 1;
580                 llh->llh_cat_idx = idx;
581                 if (idx == cathandle->lgh_last_idx)
582                         goto out;
583                 for (i = (index + 1) % bitmap_size;
584                      i != cathandle->lgh_last_idx;
585                      i = (i + 1) % bitmap_size) {
586                         if (!ext2_test_bit(i, llh->llh_bitmap)) {
587                                 idx = llh->llh_cat_idx + 1;
588                                 llh->llh_cat_idx = idx;
589                         } else if (i == 0) {
590                                 llh->llh_cat_idx = 0;
591                         } else {
592                                 break;
593                         }
594                 }
595 out:
596                 CDEBUG(D_RPCTRACE, "set catlog "LPX64" first idx %u\n",
597                        cathandle->lgh_id.lgl_oid, llh->llh_cat_idx);
598         }
599
600         RETURN(0);
601 }