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