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