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