Whamcloud - gitweb
LU-11943 llog: Reset current log on ENOSPC
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2016, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/obdclass/llog_cat.c
33  *
34  * OST<->MDS recovery logging infrastructure.
35  *
36  * Invariants in implementation:
37  * - we do not share logs among different OST<->MDS connections, so that
38  *   if an OST or MDS fails it need only look at log(s) relevant to itself
39  *
40  * Author: Andreas Dilger <adilger@clusterfs.com>
41  * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
42  * Author: Mikhail Pershin <mike.pershin@intel.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_LOG
46
47
48 #include <obd_class.h>
49
50 #include "llog_internal.h"
51
52 /* Create a new log handle and add it to the open list.
53  * This log handle will be closed when all of the records in it are removed.
54  *
55  * Assumes caller has already pushed us into the kernel context and is locking.
56  */
57 static int llog_cat_new_log(const struct lu_env *env,
58                             struct llog_handle *cathandle,
59                             struct llog_handle *loghandle,
60                             struct thandle *th)
61 {
62         struct llog_thread_info *lgi = llog_info(env);
63         struct llog_logid_rec   *rec = &lgi->lgi_logid;
64         struct thandle *handle = NULL;
65         struct dt_device *dt = NULL;
66         struct llog_log_hdr     *llh = cathandle->lgh_hdr;
67         int                      rc, index;
68
69         ENTRY;
70
71         index = (cathandle->lgh_last_idx + 1) %
72                 (OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS) ? (cfs_fail_val + 1) :
73                                                 LLOG_HDR_BITMAP_SIZE(llh));
74
75         /* check that new llog index will not overlap with the first one.
76          * - llh_cat_idx is the index just before the first/oldest still in-use
77          *      index in catalog
78          * - lgh_last_idx is the last/newest used index in catalog
79          *
80          * When catalog is not wrapped yet then lgh_last_idx is always larger
81          * than llh_cat_idx. After the wrap around lgh_last_idx re-starts
82          * from 0 and llh_cat_idx becomes the upper limit for it
83          *
84          * Check if catalog has already wrapped around or not by comparing
85          * last_idx and cat_idx */
86         if ((index == llh->llh_cat_idx + 1 && llh->llh_count > 1) ||
87             (index == 0 && llh->llh_cat_idx == 0)) {
88                 if (cathandle->lgh_name == NULL) {
89                         CWARN("%s: there are no more free slots in catalog "
90                               DFID":%x\n",
91                               loghandle->lgh_ctxt->loc_obd->obd_name,
92                               PFID(&cathandle->lgh_id.lgl_oi.oi_fid),
93                               cathandle->lgh_id.lgl_ogen);
94                 } else {
95                         CWARN("%s: there are no more free slots in "
96                               "catalog %s\n",
97                               loghandle->lgh_ctxt->loc_obd->obd_name,
98                               cathandle->lgh_name);
99                 }
100                 RETURN(-ENOSPC);
101         }
102
103         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED))
104                 RETURN(-ENOSPC);
105
106         if (loghandle->lgh_hdr != NULL) {
107                 /* If llog object is remote and creation is failed, lgh_hdr
108                  * might be left over here, free it first */
109                 LASSERT(!llog_exist(loghandle));
110                 OBD_FREE_LARGE(loghandle->lgh_hdr, loghandle->lgh_hdr_size);
111                 loghandle->lgh_hdr = NULL;
112         }
113
114         if (th == NULL) {
115                 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
116
117                 handle = dt_trans_create(env, dt);
118                 if (IS_ERR(handle))
119                         RETURN(PTR_ERR(handle));
120
121                 /* Create update llog object synchronously, which
122                  * happens during inialization process see
123                  * lod_sub_prep_llog(), to make sure the update
124                  * llog object is created before corss-MDT writing
125                  * updates into the llog object */
126                 if (cathandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
127                         handle->th_sync = 1;
128
129                 handle->th_wait_submit = 1;
130
131                 rc = llog_declare_create(env, loghandle, handle);
132                 if (rc != 0)
133                         GOTO(out, rc);
134
135                 rec->lid_hdr.lrh_len = sizeof(*rec);
136                 rec->lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
137                 rec->lid_id = loghandle->lgh_id;
138                 rc = llog_declare_write_rec(env, cathandle, &rec->lid_hdr, -1,
139                                             handle);
140                 if (rc != 0)
141                         GOTO(out, rc);
142
143                 rc = dt_trans_start_local(env, dt, handle);
144                 if (rc != 0)
145                         GOTO(out, rc);
146
147                 th = handle;
148         }
149
150         rc = llog_create(env, loghandle, th);
151         /* if llog is already created, no need to initialize it */
152         if (rc == -EEXIST) {
153                 GOTO(out, rc = 0);
154         } else if (rc != 0) {
155                 CERROR("%s: can't create new plain llog in catalog: rc = %d\n",
156                        loghandle->lgh_ctxt->loc_obd->obd_name, rc);
157                 GOTO(out, rc);
158         }
159
160         rc = llog_init_handle(env, loghandle,
161                               LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
162                               &cathandle->lgh_hdr->llh_tgtuuid);
163         if (rc < 0)
164                 GOTO(out, rc);
165
166         /* build the record for this log in the catalog */
167         rec->lid_hdr.lrh_len = sizeof(*rec);
168         rec->lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
169         rec->lid_id = loghandle->lgh_id;
170
171         /* append the new record into catalog. The new index will be
172          * assigned to the record and updated in rec header */
173         rc = llog_write_rec(env, cathandle, &rec->lid_hdr,
174                             &loghandle->u.phd.phd_cookie, LLOG_NEXT_IDX, th);
175         if (rc < 0)
176                 GOTO(out_destroy, rc);
177
178         CDEBUG(D_OTHER, "new plain log "DFID".%u of catalog "DFID"\n",
179                PFID(&loghandle->lgh_id.lgl_oi.oi_fid), rec->lid_hdr.lrh_index,
180                PFID(&cathandle->lgh_id.lgl_oi.oi_fid));
181
182         loghandle->lgh_hdr->llh_cat_idx = rec->lid_hdr.lrh_index;
183
184         /* limit max size of plain llog so that space can be
185          * released sooner, especially on small filesystems */
186         /* 2MB for the cases when free space hasn't been learned yet */
187         loghandle->lgh_max_size = 2 << 20;
188         dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
189         rc = dt_statfs(env, dt, &lgi->lgi_statfs);
190         if (rc == 0 && lgi->lgi_statfs.os_bfree > 0) {
191                 __u64 freespace = (lgi->lgi_statfs.os_bfree *
192                                   lgi->lgi_statfs.os_bsize) >> 6;
193                 if (freespace < loghandle->lgh_max_size)
194                         loghandle->lgh_max_size = freespace;
195                 /* shouldn't be > 128MB in any case?
196                  * it's 256K records of 512 bytes each */
197                 if (freespace > (128 << 20))
198                         loghandle->lgh_max_size = 128 << 20;
199         }
200         rc = 0;
201
202 out:
203         if (handle != NULL) {
204                 handle->th_result = rc >= 0 ? 0 : rc;
205                 dt_trans_stop(env, dt, handle);
206         }
207         RETURN(rc);
208
209 out_destroy:
210         /* to signal llog_cat_close() it shouldn't try to destroy the llog,
211          * we want to destroy it in this transaction, otherwise the object
212          * becomes an orphan */
213         loghandle->lgh_hdr->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
214         /* this is to mimic full log, so another llog_cat_current_log()
215          * can skip it and ask for another onet */
216         loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) + 1;
217         llog_trans_destroy(env, loghandle, th);
218         RETURN(rc);
219 }
220
221 /* Open an existent log handle and add it to the open list.
222  * This log handle will be closed when all of the records in it are removed.
223  *
224  * Assumes caller has already pushed us into the kernel context and is locking.
225  * We return a lock on the handle to ensure nobody yanks it from us.
226  *
227  * This takes extra reference on llog_handle via llog_handle_get() and require
228  * this reference to be put by caller using llog_handle_put()
229  */
230 int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
231                        struct llog_handle **res, struct llog_logid *logid)
232 {
233         struct llog_handle      *loghandle;
234         enum llog_flag           fmt;
235         int                      rc = 0;
236
237         ENTRY;
238
239         if (cathandle == NULL)
240                 RETURN(-EBADF);
241
242         fmt = cathandle->lgh_hdr->llh_flags & LLOG_F_EXT_MASK;
243         down_write(&cathandle->lgh_lock);
244         list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
245                             u.phd.phd_entry) {
246                 struct llog_logid *cgl = &loghandle->lgh_id;
247
248                 if (ostid_id(&cgl->lgl_oi) == ostid_id(&logid->lgl_oi) &&
249                     ostid_seq(&cgl->lgl_oi) == ostid_seq(&logid->lgl_oi)) {
250                         if (cgl->lgl_ogen != logid->lgl_ogen) {
251                                 CWARN("%s: log "DFID" generation %x != %x\n",
252                                       loghandle->lgh_ctxt->loc_obd->obd_name,
253                                       PFID(&logid->lgl_oi.oi_fid),
254                                       cgl->lgl_ogen, logid->lgl_ogen);
255                                 continue;
256                         }
257                         loghandle->u.phd.phd_cat_handle = cathandle;
258                         up_write(&cathandle->lgh_lock);
259                         GOTO(out, rc = 0);
260                 }
261         }
262         up_write(&cathandle->lgh_lock);
263
264         rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
265                        LLOG_OPEN_EXISTS);
266         if (rc < 0) {
267                 CERROR("%s: error opening log id "DFID":%x: rc = %d\n",
268                        cathandle->lgh_ctxt->loc_obd->obd_name,
269                        PFID(&logid->lgl_oi.oi_fid), logid->lgl_ogen, rc);
270                 RETURN(rc);
271         }
272
273         rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN | fmt, NULL);
274         if (rc < 0) {
275                 llog_close(env, loghandle);
276                 loghandle = NULL;
277                 RETURN(rc);
278         }
279
280         down_write(&cathandle->lgh_lock);
281         list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
282         up_write(&cathandle->lgh_lock);
283
284         loghandle->u.phd.phd_cat_handle = cathandle;
285         loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
286         loghandle->u.phd.phd_cookie.lgc_index =
287                                 loghandle->lgh_hdr->llh_cat_idx;
288         EXIT;
289 out:
290         llog_handle_get(loghandle);
291         *res = loghandle;
292         return 0;
293 }
294
295 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
296 {
297         struct llog_handle      *loghandle, *n;
298         int                      rc;
299
300         ENTRY;
301
302         list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
303                                  u.phd.phd_entry) {
304                 struct llog_log_hdr     *llh = loghandle->lgh_hdr;
305                 int                      index;
306
307                 /* unlink open-not-created llogs */
308                 list_del_init(&loghandle->u.phd.phd_entry);
309                 llh = loghandle->lgh_hdr;
310                 if (loghandle->lgh_obj != NULL && llh != NULL &&
311                     (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
312                     (llh->llh_count == 1)) {
313                         rc = llog_destroy(env, loghandle);
314                         if (rc)
315                                 CERROR("%s: failure destroying log during "
316                                        "cleanup: rc = %d\n",
317                                        loghandle->lgh_ctxt->loc_obd->obd_name,
318                                        rc);
319
320                         index = loghandle->u.phd.phd_cookie.lgc_index;
321                         llog_cat_cleanup(env, cathandle, NULL, index);
322                 }
323                 llog_close(env, loghandle);
324         }
325         /* if handle was stored in ctxt, remove it too */
326         if (cathandle->lgh_ctxt->loc_handle == cathandle)
327                 cathandle->lgh_ctxt->loc_handle = NULL;
328         rc = llog_close(env, cathandle);
329         RETURN(rc);
330 }
331 EXPORT_SYMBOL(llog_cat_close);
332
333 /**
334  * lockdep markers for nested struct llog_handle::lgh_lock locking.
335  */
336 enum {
337         LLOGH_CAT,
338         LLOGH_LOG
339 };
340
341 /** Return the currently active log handle.  If the current log handle doesn't
342  * have enough space left for the current record, start a new one.
343  *
344  * If reclen is 0, we only want to know what the currently active log is,
345  * otherwise we get a lock on this log so nobody can steal our space.
346  *
347  * Assumes caller has already pushed us into the kernel context and is locking.
348  *
349  * NOTE: loghandle is write-locked upon successful return
350  */
351 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
352                                                 struct thandle *th)
353 {
354         struct llog_handle *loghandle = NULL;
355         ENTRY;
356
357
358         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2)) {
359                 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
360                 GOTO(next, loghandle);
361         }
362
363         down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
364         loghandle = cathandle->u.chd.chd_current_log;
365         if (loghandle) {
366                 struct llog_log_hdr *llh;
367
368                 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
369                 llh = loghandle->lgh_hdr;
370                 if (llh == NULL || !llog_is_full(loghandle)) {
371                         up_read(&cathandle->lgh_lock);
372                         RETURN(loghandle);
373                 } else {
374                         up_write(&loghandle->lgh_lock);
375                 }
376         }
377         up_read(&cathandle->lgh_lock);
378
379         /* time to use next log */
380
381         /* first, we have to make sure the state hasn't changed */
382         down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
383         loghandle = cathandle->u.chd.chd_current_log;
384         if (loghandle) {
385                 struct llog_log_hdr *llh;
386
387                 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
388                 llh = loghandle->lgh_hdr;
389                 if (llh == NULL || !llog_is_full(loghandle))
390                         GOTO(out_unlock, loghandle);
391                 else
392                         up_write(&loghandle->lgh_lock);
393         }
394
395 next:
396         /* Sigh, the chd_next_log and chd_current_log is initialized
397          * in declare phase, and we do not serialize the catlog
398          * accessing, so it might be possible the llog creation
399          * thread (see llog_cat_declare_add_rec()) did not create
400          * llog successfully, then the following thread might
401          * meet this situation. */
402         if (IS_ERR_OR_NULL(cathandle->u.chd.chd_next_log)) {
403                 CERROR("%s: next log does not exist!\n",
404                        cathandle->lgh_ctxt->loc_obd->obd_name);
405                 loghandle = ERR_PTR(-EIO);
406                 if (cathandle->u.chd.chd_next_log == NULL) {
407                         /* Store the error in chd_next_log, so
408                          * the following process can get correct
409                          * failure value */
410                         cathandle->u.chd.chd_next_log = loghandle;
411                 }
412                 GOTO(out_unlock, loghandle);
413         }
414
415         CDEBUG(D_INODE, "use next log\n");
416
417         loghandle = cathandle->u.chd.chd_next_log;
418         cathandle->u.chd.chd_current_log = loghandle;
419         cathandle->u.chd.chd_next_log = NULL;
420         down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
421
422 out_unlock:
423         up_write(&cathandle->lgh_lock);
424         LASSERT(loghandle);
425         RETURN(loghandle);
426 }
427
428 static int llog_cat_update_header(const struct lu_env *env,
429                            struct llog_handle *cathandle)
430 {
431         struct llog_handle *loghandle;
432         int rc;
433         ENTRY;
434
435         /* refresh llog */
436         down_write(&cathandle->lgh_lock);
437         if (!cathandle->lgh_stale) {
438                 up_write(&cathandle->lgh_lock);
439                 RETURN(0);
440         }
441         list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
442                             u.phd.phd_entry) {
443                 if (!llog_exist(loghandle))
444                         continue;
445
446                 rc = llog_read_header(env, loghandle, NULL);
447                 if (rc != 0) {
448                         up_write(&cathandle->lgh_lock);
449                         GOTO(out, rc);
450                 }
451         }
452         rc = llog_read_header(env, cathandle, NULL);
453         if (rc == 0)
454                 cathandle->lgh_stale = 0;
455         up_write(&cathandle->lgh_lock);
456         if (rc != 0)
457                 GOTO(out, rc);
458 out:
459         RETURN(rc);
460 }
461
462 /* Add a single record to the recovery log(s) using a catalog
463  * Returns as llog_write_record
464  *
465  * Assumes caller has already pushed us into the kernel context.
466  */
467 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
468                      struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
469                      struct thandle *th)
470 {
471         struct llog_handle *loghandle;
472         int rc, retried = 0;
473         ENTRY;
474
475         LASSERT(rec->lrh_len <= cathandle->lgh_ctxt->loc_chunk_size);
476
477 retry:
478         loghandle = llog_cat_current_log(cathandle, th);
479         if (IS_ERR(loghandle))
480                 RETURN(PTR_ERR(loghandle));
481
482         /* loghandle is already locked by llog_cat_current_log() for us */
483         if (!llog_exist(loghandle)) {
484                 rc = llog_cat_new_log(env, cathandle, loghandle, th);
485                 if (rc < 0) {
486                         up_write(&loghandle->lgh_lock);
487                         /* nobody should be trying to use this llog */
488                         down_write(&cathandle->lgh_lock);
489                         if (cathandle->u.chd.chd_current_log == loghandle)
490                                 cathandle->u.chd.chd_current_log = NULL;
491                         up_write(&cathandle->lgh_lock);
492                         RETURN(rc);
493                 }
494         }
495         /* now let's try to add the record */
496         rc = llog_write_rec(env, loghandle, rec, reccookie, LLOG_NEXT_IDX, th);
497         if (rc < 0) {
498                 CDEBUG_LIMIT(rc == -ENOSPC ? D_HA : D_ERROR,
499                              "llog_write_rec %d: lh=%p\n", rc, loghandle);
500                 /* -ENOSPC is returned if no empty records left
501                  * and when it's lack of space on the stogage.
502                  * there is no point to try again if it's the second
503                  * case. many callers (like llog test) expect ENOSPC,
504                  * so we preserve this error code, but look for the
505                  * actual cause here */
506                 if (rc == -ENOSPC && llog_is_full(loghandle))
507                         rc = -ENOBUFS;
508         }
509         up_write(&loghandle->lgh_lock);
510
511         if (rc == -ENOBUFS) {
512                 if (retried++ == 0)
513                         GOTO(retry, rc);
514                 CERROR("%s: error on 2nd llog: rc = %d\n",
515                        cathandle->lgh_ctxt->loc_obd->obd_name, rc);
516         }
517
518         RETURN(rc);
519 }
520 EXPORT_SYMBOL(llog_cat_add_rec);
521
522 int llog_cat_declare_add_rec(const struct lu_env *env,
523                              struct llog_handle *cathandle,
524                              struct llog_rec_hdr *rec, struct thandle *th)
525 {
526         struct llog_thread_info *lgi = llog_info(env);
527         struct llog_logid_rec   *lirec = &lgi->lgi_logid;
528         struct llog_handle      *loghandle, *next;
529         int                      rc = 0;
530
531         ENTRY;
532
533         if (cathandle->u.chd.chd_current_log == NULL) {
534                 /* declare new plain llog */
535                 down_write(&cathandle->lgh_lock);
536                 if (cathandle->u.chd.chd_current_log == NULL) {
537                         rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
538                                        NULL, NULL, LLOG_OPEN_NEW);
539                         if (rc == 0) {
540                                 cathandle->u.chd.chd_current_log = loghandle;
541                                 list_add_tail(&loghandle->u.phd.phd_entry,
542                                               &cathandle->u.chd.chd_head);
543                         }
544                 }
545                 up_write(&cathandle->lgh_lock);
546         } else if (cathandle->u.chd.chd_next_log == NULL ||
547                    IS_ERR(cathandle->u.chd.chd_next_log)) {
548                 /* declare next plain llog */
549                 down_write(&cathandle->lgh_lock);
550                 if (cathandle->u.chd.chd_next_log == NULL ||
551                     IS_ERR(cathandle->u.chd.chd_next_log)) {
552                         rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
553                                        NULL, NULL, LLOG_OPEN_NEW);
554                         if (rc == 0) {
555                                 cathandle->u.chd.chd_next_log = loghandle;
556                                 list_add_tail(&loghandle->u.phd.phd_entry,
557                                               &cathandle->u.chd.chd_head);
558                         }
559                 }
560                 up_write(&cathandle->lgh_lock);
561         }
562         if (rc)
563                 GOTO(out, rc);
564
565         lirec->lid_hdr.lrh_len = sizeof(*lirec);
566
567         if (!llog_exist(cathandle->u.chd.chd_current_log)) {
568                 if (dt_object_remote(cathandle->lgh_obj)) {
569                         /* For remote operation, if we put the llog object
570                          * creation in the current transaction, then the
571                          * llog object will not be created on the remote
572                          * target until the transaction stop, if other
573                          * operations start before the transaction stop,
574                          * and use the same llog object, will be dependent
575                          * on the success of this transaction. So let's
576                          * create the llog object synchronously here to
577                          * remove the dependency. */
578 create_again:
579                         down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
580                         loghandle = cathandle->u.chd.chd_current_log;
581                         down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
582                         if (cathandle->lgh_stale) {
583                                 up_write(&loghandle->lgh_lock);
584                                 up_read(&cathandle->lgh_lock);
585                                 GOTO(out, rc = -EIO);
586                         }
587                         if (!llog_exist(loghandle)) {
588                                 rc = llog_cat_new_log(env, cathandle, loghandle,
589                                                       NULL);
590                                 if (rc == -ESTALE)
591                                         cathandle->lgh_stale = 1;
592                         }
593                         up_write(&loghandle->lgh_lock);
594                         up_read(&cathandle->lgh_lock);
595                         if (rc == -ESTALE) {
596                                 rc = llog_cat_update_header(env, cathandle);
597                                 if (rc != 0)
598                                         GOTO(out, rc);
599                                 goto create_again;
600                         } else if (rc < 0) {
601                                 GOTO(out, rc);
602                         }
603                 } else {
604                         rc = llog_declare_create(env,
605                                         cathandle->u.chd.chd_current_log, th);
606                         if (rc)
607                                 GOTO(out, rc);
608                         llog_declare_write_rec(env, cathandle,
609                                                &lirec->lid_hdr, -1, th);
610                 }
611         }
612
613 write_again:
614         /* declare records in the llogs */
615         rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
616                                     rec, -1, th);
617         if (rc == -ESTALE) {
618                 down_write(&cathandle->lgh_lock);
619                 if (cathandle->lgh_stale) {
620                         up_write(&cathandle->lgh_lock);
621                         GOTO(out, rc = -EIO);
622                 }
623
624                 cathandle->lgh_stale = 1;
625                 up_write(&cathandle->lgh_lock);
626                 rc = llog_cat_update_header(env, cathandle);
627                 if (rc != 0)
628                         GOTO(out, rc);
629                 goto write_again;
630         } else if (rc < 0) {
631                 GOTO(out, rc);
632         }
633
634         next = cathandle->u.chd.chd_next_log;
635         if (!IS_ERR_OR_NULL(next)) {
636                 if (!llog_exist(next)) {
637                         if (dt_object_remote(cathandle->lgh_obj)) {
638                                 /* For remote operation, if we put the llog
639                                  * object creation in the current transaction,
640                                  * then the llog object will not be created on
641                                  * the remote target until the transaction stop,
642                                  * if other operations start before the
643                                  * transaction stop, and use the same llog
644                                  * object, will be dependent on the success of
645                                  * this transaction. So let's create the llog
646                                  * object synchronously here to remove the
647                                  * dependency. */
648                                 down_write_nested(&cathandle->lgh_lock,
649                                                  LLOGH_CAT);
650                                 next = cathandle->u.chd.chd_next_log;
651                                 if (IS_ERR_OR_NULL(next)) {
652                                         /* Sigh, another thread just tried,
653                                          * let's fail as well */
654                                         up_write(&cathandle->lgh_lock);
655                                         if (next == NULL)
656                                                 rc = -EIO;
657                                         else
658                                                 rc = PTR_ERR(next);
659                                         GOTO(out, rc);
660                                 }
661
662                                 down_write_nested(&next->lgh_lock, LLOGH_LOG);
663                                 if (!llog_exist(next)) {
664                                         rc = llog_cat_new_log(env, cathandle,
665                                                               next, NULL);
666                                         if (rc < 0)
667                                                 cathandle->u.chd.chd_next_log =
668                                                                 ERR_PTR(rc);
669                                 }
670                                 up_write(&next->lgh_lock);
671                                 up_write(&cathandle->lgh_lock);
672                                 if (rc < 0)
673                                         GOTO(out, rc);
674                         } else {
675                                 rc = llog_declare_create(env, next, th);
676                                 llog_declare_write_rec(env, cathandle,
677                                                 &lirec->lid_hdr, -1, th);
678                         }
679                 }
680                 /* XXX: we hope for declarations made for existing llog
681                  *      this might be not correct with some backends
682                  *      where declarations are expected against specific
683                  *      object like ZFS with full debugging enabled */
684                 /*llog_declare_write_rec(env, next, rec, -1, th);*/
685         }
686 out:
687         RETURN(rc);
688 }
689 EXPORT_SYMBOL(llog_cat_declare_add_rec);
690
691 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
692                  struct llog_rec_hdr *rec, struct llog_cookie *reccookie)
693 {
694         struct llog_ctxt        *ctxt;
695         struct dt_device        *dt;
696         struct thandle          *th = NULL;
697         int                      rc;
698
699         ctxt = cathandle->lgh_ctxt;
700         LASSERT(ctxt);
701         LASSERT(ctxt->loc_exp);
702
703         LASSERT(cathandle->lgh_obj != NULL);
704         dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
705
706         th = dt_trans_create(env, dt);
707         if (IS_ERR(th))
708                 RETURN(PTR_ERR(th));
709
710         rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
711         if (rc)
712                 GOTO(out_trans, rc);
713
714         rc = dt_trans_start_local(env, dt, th);
715         if (rc)
716                 GOTO(out_trans, rc);
717         rc = llog_cat_add_rec(env, cathandle, rec, reccookie, th);
718 out_trans:
719         dt_trans_stop(env, dt, th);
720         RETURN(rc);
721 }
722 EXPORT_SYMBOL(llog_cat_add);
723
724 /* For each cookie in the cookie array, we clear the log in-use bit and either:
725  * - the log is empty, so mark it free in the catalog header and delete it
726  * - the log is not empty, just write out the log header
727  *
728  * The cookies may be in different log files, so we need to get new logs
729  * each time.
730  *
731  * Assumes caller has already pushed us into the kernel context.
732  */
733 int llog_cat_cancel_records(const struct lu_env *env,
734                             struct llog_handle *cathandle, int count,
735                             struct llog_cookie *cookies)
736 {
737         int i, index, rc = 0, failed = 0;
738
739         ENTRY;
740
741         for (i = 0; i < count; i++, cookies++) {
742                 struct llog_handle *loghandle;
743                 struct llog_logid *lgl = &cookies->lgc_lgl;
744                 int  lrc;
745
746                 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
747                 if (rc) {
748                         CDEBUG(D_HA, "%s: cannot find llog for handle "DFID":%x"
749                                ": rc = %d\n",
750                                cathandle->lgh_ctxt->loc_obd->obd_name,
751                                PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, rc);
752                         failed++;
753                         continue;
754                 }
755
756                 if ((cathandle->lgh_ctxt->loc_flags &
757                      LLOG_CTXT_FLAG_NORMAL_FID) && !llog_exist(loghandle)) {
758                         /* For update log, some of loghandles of cathandle
759                          * might not exist because remote llog creation might
760                          * be failed, so let's skip the record cancellation
761                          * for these non-exist llogs.
762                          */
763                         lrc = -ENOENT;
764                         CDEBUG(D_HA, "%s: llog "DFID":%x does not exist"
765                                ": rc = %d\n",
766                                cathandle->lgh_ctxt->loc_obd->obd_name,
767                                PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, lrc);
768                         failed++;
769                         if (rc == 0)
770                                 rc = lrc;
771                         continue;
772                 }
773
774                 lrc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
775                 if (lrc == LLOG_DEL_PLAIN) { /* log has been destroyed */
776                         index = loghandle->u.phd.phd_cookie.lgc_index;
777                         lrc = llog_cat_cleanup(env, cathandle, loghandle,
778                                                index);
779                         if (rc == 0)
780                                 rc = lrc;
781                 } else if (lrc == -ENOENT) {
782                         if (rc == 0) /* ENOENT shouldn't rewrite any error */
783                                 rc = lrc;
784                 } else if (lrc < 0) {
785                         failed++;
786                         if (rc == 0)
787                                 rc = lrc;
788                 }
789                 llog_handle_put(loghandle);
790         }
791         if (rc)
792                 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
793                        cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
794                        rc);
795
796         RETURN(rc);
797 }
798 EXPORT_SYMBOL(llog_cat_cancel_records);
799
800 static int llog_cat_process_cb(const struct lu_env *env,
801                                struct llog_handle *cat_llh,
802                                struct llog_rec_hdr *rec, void *data)
803 {
804         struct llog_process_data *d = data;
805         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
806         struct llog_handle *llh;
807         struct llog_log_hdr *hdr;
808         int rc;
809
810         ENTRY;
811         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
812                 CERROR("invalid record in catalog\n");
813                 RETURN(-EINVAL);
814         }
815         CDEBUG(D_HA, "processing log "DFID":%x at index %u of catalog "
816                DFID"\n", PFID(&lir->lid_id.lgl_oi.oi_fid), lir->lid_id.lgl_ogen,
817                rec->lrh_index, PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
818
819         rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
820         if (rc) {
821                 CERROR("%s: cannot find handle for llog "DFID": rc = %d\n",
822                        cat_llh->lgh_ctxt->loc_obd->obd_name,
823                        PFID(&lir->lid_id.lgl_oi.oi_fid), rc);
824                 if (rc == -ENOENT || rc == -ESTALE) {
825                         /* After a server crash, a stub of index
826                          * record in catlog could be kept, because
827                          * plain log destroy + catlog index record
828                          * deletion are not atomic. So we end up with
829                          * an index but no actual record. Destroy the
830                          * index and move on. */
831                         rc = llog_cat_cleanup(env, cat_llh, NULL,
832                                               rec->lrh_index);
833                 }
834
835                 RETURN(rc);
836         }
837
838         /* clean old empty llogs, do not consider current llog in use */
839         /* ignore remote (lgh_obj=NULL) llogs */
840         hdr = llh->lgh_hdr;
841         if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
842             hdr->llh_count == 1 && cat_llh->lgh_obj != NULL &&
843             llh != cat_llh->u.chd.chd_current_log) {
844                 rc = llog_destroy(env, llh);
845                 if (rc)
846                         CERROR("%s: fail to destroy empty log: rc = %d\n",
847                                llh->lgh_ctxt->loc_obd->obd_name, rc);
848                 GOTO(out, rc = LLOG_DEL_PLAIN);
849         }
850
851         if (rec->lrh_index < d->lpd_startcat) {
852                 /* Skip processing of the logs until startcat */
853                 rc = 0;
854         } else if (d->lpd_startidx > 0) {
855                 struct llog_process_cat_data cd;
856
857                 cd.lpcd_first_idx = d->lpd_startidx;
858                 cd.lpcd_last_idx = 0;
859                 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
860                                           &cd, false);
861                 /* Continue processing the next log from idx 0 */
862                 d->lpd_startidx = 0;
863         } else {
864                 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
865                                           NULL, false);
866         }
867
868 out:
869         /* The empty plain log was destroyed while processing */
870         if (rc == LLOG_DEL_PLAIN)
871                 rc = llog_cat_cleanup(env, cat_llh, llh,
872                                       llh->u.phd.phd_cookie.lgc_index);
873         llog_handle_put(llh);
874
875         RETURN(rc);
876 }
877
878 int llog_cat_process_or_fork(const struct lu_env *env,
879                              struct llog_handle *cat_llh, llog_cb_t cat_cb,
880                              llog_cb_t cb, void *data, int startcat,
881                              int startidx, bool fork)
882 {
883         struct llog_process_data d;
884         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
885         int rc;
886         ENTRY;
887
888         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
889         d.lpd_data = data;
890         d.lpd_cb = cb;
891         d.lpd_startcat = startcat;
892         d.lpd_startidx = startidx;
893
894         if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
895             llh->llh_count > 1) {
896                 struct llog_process_cat_data cd;
897
898                 CWARN("%s: catlog "DFID" crosses index zero\n",
899                       cat_llh->lgh_ctxt->loc_obd->obd_name,
900                       PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
901
902                 cd.lpcd_first_idx = llh->llh_cat_idx;
903                 cd.lpcd_last_idx = 0;
904                 rc = llog_process_or_fork(env, cat_llh, cat_cb,
905                                           &d, &cd, fork);
906                 if (rc != 0)
907                         RETURN(rc);
908
909                 cd.lpcd_first_idx = 0;
910                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
911                 rc = llog_process_or_fork(env, cat_llh, cat_cb,
912                                           &d, &cd, fork);
913         } else {
914                 rc = llog_process_or_fork(env, cat_llh, cat_cb,
915                                           &d, NULL, fork);
916         }
917
918         RETURN(rc);
919 }
920
921 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
922                      llog_cb_t cb, void *data, int startcat, int startidx)
923 {
924         return llog_cat_process_or_fork(env, cat_llh, llog_cat_process_cb,
925                                         cb, data, startcat, startidx, false);
926 }
927 EXPORT_SYMBOL(llog_cat_process);
928
929 static int llog_cat_size_cb(const struct lu_env *env,
930                              struct llog_handle *cat_llh,
931                              struct llog_rec_hdr *rec, void *data)
932 {
933         struct llog_process_data *d = data;
934         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
935         struct llog_handle *llh;
936         int rc;
937         __u64 *cum_size = d->lpd_data;
938         __u64 size;
939
940         ENTRY;
941         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
942                 CERROR("%s: invalid record in catalog, rc = %d\n",
943                        cat_llh->lgh_ctxt->loc_obd->obd_name, -EINVAL);
944                 RETURN(-EINVAL);
945         }
946         CDEBUG(D_HA, "processing log "DFID":%x at index %u of catalog "
947                DFID"\n", PFID(&lir->lid_id.lgl_oi.oi_fid), lir->lid_id.lgl_ogen,
948                rec->lrh_index, PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
949
950         rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
951         if (rc) {
952                 CWARN("%s: cannot find handle for llog "DFID": rc = %d\n",
953                       cat_llh->lgh_ctxt->loc_obd->obd_name,
954                       PFID(&lir->lid_id.lgl_oi.oi_fid), rc);
955                 RETURN(0);
956         }
957         size = llog_size(env, llh);
958         *cum_size += size;
959
960         CDEBUG(D_INFO, "Add llog entry "DFID" size %llu\n",
961                PFID(&llh->lgh_id.lgl_oi.oi_fid), size);
962
963         llog_handle_put(llh);
964
965         RETURN(0);
966
967 }
968
969 __u64 llog_cat_size(const struct lu_env *env, struct llog_handle *cat_llh)
970 {
971         __u64 size = llog_size(env, cat_llh);
972
973         llog_cat_process_or_fork(env, cat_llh, llog_cat_size_cb,
974                                  NULL, &size, 0, 0, false);
975
976         return size;
977 }
978 EXPORT_SYMBOL(llog_cat_size);
979
980 static int llog_cat_reverse_process_cb(const struct lu_env *env,
981                                        struct llog_handle *cat_llh,
982                                        struct llog_rec_hdr *rec, void *data)
983 {
984         struct llog_process_data *d = data;
985         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
986         struct llog_handle *llh;
987         struct llog_log_hdr *hdr;
988         int rc;
989
990         if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
991                 CERROR("invalid record in catalog\n");
992                 RETURN(-EINVAL);
993         }
994         CDEBUG(D_HA, "processing log "DFID":%x at index %u of catalog "
995                DFID"\n", PFID(&lir->lid_id.lgl_oi.oi_fid), lir->lid_id.lgl_ogen,
996                le32_to_cpu(rec->lrh_index),
997                PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
998
999         rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
1000         if (rc) {
1001                 CERROR("%s: cannot find handle for llog "DFID": rc = %d\n",
1002                        cat_llh->lgh_ctxt->loc_obd->obd_name,
1003                        PFID(&lir->lid_id.lgl_oi.oi_fid), rc);
1004                 if (rc == -ENOENT || rc == -ESTALE) {
1005                         /* After a server crash, a stub of index
1006                          * record in catlog could be kept, because
1007                          * plain log destroy + catlog index record
1008                          * deletion are not atomic. So we end up with
1009                          * an index but no actual record. Destroy the
1010                          * index and move on. */
1011                         rc = llog_cat_cleanup(env, cat_llh, NULL,
1012                                               rec->lrh_index);
1013                 }
1014
1015                 RETURN(rc);
1016         }
1017
1018         /* clean old empty llogs, do not consider current llog in use */
1019         hdr = llh->lgh_hdr;
1020         if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
1021             hdr->llh_count == 1 &&
1022             llh != cat_llh->u.chd.chd_current_log) {
1023                 rc = llog_destroy(env, llh);
1024                 if (rc)
1025                         CERROR("%s: fail to destroy empty log: rc = %d\n",
1026                                llh->lgh_ctxt->loc_obd->obd_name, rc);
1027                 GOTO(out, rc = LLOG_DEL_PLAIN);
1028         }
1029
1030         rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
1031
1032 out:
1033         /* The empty plain was destroyed while processing */
1034         if (rc == LLOG_DEL_PLAIN)
1035                 rc = llog_cat_cleanup(env, cat_llh, llh,
1036                                       llh->u.phd.phd_cookie.lgc_index);
1037
1038         llog_handle_put(llh);
1039         RETURN(rc);
1040 }
1041
1042 int llog_cat_reverse_process(const struct lu_env *env,
1043                              struct llog_handle *cat_llh,
1044                              llog_cb_t cb, void *data)
1045 {
1046         struct llog_process_data d;
1047         struct llog_process_cat_data cd;
1048         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
1049         int rc;
1050         ENTRY;
1051
1052         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
1053         d.lpd_data = data;
1054         d.lpd_cb = cb;
1055
1056         if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
1057             llh->llh_count > 1) {
1058                 CWARN("%s: catalog "DFID" crosses index zero\n",
1059                       cat_llh->lgh_ctxt->loc_obd->obd_name,
1060                       PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
1061
1062                 cd.lpcd_first_idx = 0;
1063                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
1064                 rc = llog_reverse_process(env, cat_llh,
1065                                           llog_cat_reverse_process_cb,
1066                                           &d, &cd);
1067                 if (rc != 0)
1068                         RETURN(rc);
1069
1070                 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
1071                 cd.lpcd_last_idx = 0;
1072                 rc = llog_reverse_process(env, cat_llh,
1073                                           llog_cat_reverse_process_cb,
1074                                           &d, &cd);
1075         } else {
1076                 rc = llog_reverse_process(env, cat_llh,
1077                                           llog_cat_reverse_process_cb,
1078                                           &d, NULL);
1079         }
1080
1081         RETURN(rc);
1082 }
1083 EXPORT_SYMBOL(llog_cat_reverse_process);
1084
1085 static int llog_cat_set_first_idx(struct llog_handle *cathandle, int idx)
1086 {
1087         struct llog_log_hdr *llh = cathandle->lgh_hdr;
1088         int bitmap_size;
1089
1090         ENTRY;
1091
1092         bitmap_size = LLOG_HDR_BITMAP_SIZE(llh);
1093         /*
1094          * The llh_cat_idx equals to the first used index minus 1
1095          * so if we canceled the first index then llh_cat_idx
1096          * must be renewed.
1097          */
1098         if (llh->llh_cat_idx == (idx - 1)) {
1099                 llh->llh_cat_idx = idx;
1100
1101                 while (idx != cathandle->lgh_last_idx) {
1102                         idx = (idx + 1) % bitmap_size;
1103                         if (!ext2_test_bit(idx, LLOG_HDR_BITMAP(llh))) {
1104                                 /* update llh_cat_idx for each unset bit,
1105                                  * expecting the next one is set */
1106                                 llh->llh_cat_idx = idx;
1107                         } else if (idx == 0) {
1108                                 /* skip header bit */
1109                                 llh->llh_cat_idx = 0;
1110                                 continue;
1111                         } else {
1112                                 /* the first index is found */
1113                                 break;
1114                         }
1115                 }
1116
1117                 CDEBUG(D_RPCTRACE, "catlog "DFID" first idx %u, last_idx %u\n",
1118                        PFID(&cathandle->lgh_id.lgl_oi.oi_fid),
1119                        llh->llh_cat_idx, cathandle->lgh_last_idx);
1120         }
1121
1122         RETURN(0);
1123 }
1124
1125 /* Cleanup deleted plain llog traces from catalog */
1126 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
1127                      struct llog_handle *loghandle, int index)
1128 {
1129         int rc;
1130
1131         LASSERT(index);
1132         if (loghandle != NULL) {
1133                 /* remove destroyed llog from catalog list and
1134                  * chd_current_log variable */
1135                 down_write(&cathandle->lgh_lock);
1136                 if (cathandle->u.chd.chd_current_log == loghandle)
1137                         cathandle->u.chd.chd_current_log = NULL;
1138                 list_del_init(&loghandle->u.phd.phd_entry);
1139                 up_write(&cathandle->lgh_lock);
1140                 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index);
1141                 /* llog was opened and keep in a list, close it now */
1142                 llog_close(env, loghandle);
1143         }
1144
1145         /* do not attempt to cleanup on-disk llog if on client side */
1146         if (cathandle->lgh_obj == NULL)
1147                 return 0;
1148
1149         /* remove plain llog entry from catalog by index */
1150         llog_cat_set_first_idx(cathandle, index);
1151         rc = llog_cancel_rec(env, cathandle, index);
1152         if (rc == 0)
1153                 CDEBUG(D_HA, "cancel plain log at index %u of catalog "DFID"\n",
1154                        index, PFID(&cathandle->lgh_id.lgl_oi.oi_fid));
1155         return rc;
1156 }