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