Whamcloud - gitweb
15804cdbc7628c389f891913bd2ec5b5a70c2447
[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 |
423                               LLOG_F_ZAP_WHEN_EMPTY | fmt, NULL);
424         if (rc < 0) {
425                 llog_close(env, loghandle);
426                 *res = NULL;
427                 RETURN(rc);
428         }
429
430         *res = llog_handle_get(loghandle);
431         LASSERT(*res);
432         down_write(&cathandle->lgh_lock);
433         list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
434         up_write(&cathandle->lgh_lock);
435
436         loghandle->u.phd.phd_cat_handle = cathandle;
437         loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
438         loghandle->u.phd.phd_cookie.lgc_index =
439                                 loghandle->lgh_hdr->llh_cat_idx;
440         RETURN(0);
441 }
442
443 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
444 {
445         struct llog_handle      *loghandle, *n;
446         int                      rc;
447
448         ENTRY;
449
450         list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
451                                  u.phd.phd_entry) {
452                 struct llog_log_hdr     *llh = loghandle->lgh_hdr;
453                 int                      index;
454
455                 /* unlink open-not-created llogs */
456                 list_del_init(&loghandle->u.phd.phd_entry);
457                 llh = loghandle->lgh_hdr;
458                 if (loghandle->lgh_obj != NULL && llh != NULL &&
459                     (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
460                     (llh->llh_count == 1)) {
461                         rc = llog_destroy(env, loghandle);
462                         if (rc)
463                                 CERROR("%s: failure destroying log during "
464                                        "cleanup: rc = %d\n",
465                                        loghandle->lgh_ctxt->loc_obd->obd_name,
466                                        rc);
467
468                         index = loghandle->u.phd.phd_cookie.lgc_index;
469                         llog_cat_cleanup(env, cathandle, NULL, index);
470                 }
471                 llog_close(env, loghandle);
472         }
473         /* if handle was stored in ctxt, remove it too */
474         if (cathandle->lgh_ctxt->loc_handle == cathandle)
475                 cathandle->lgh_ctxt->loc_handle = NULL;
476         rc = llog_close(env, cathandle);
477         RETURN(rc);
478 }
479 EXPORT_SYMBOL(llog_cat_close);
480
481 /** Return the currently active log handle.  If the current log handle doesn't
482  * have enough space left for the current record, start a new one.
483  *
484  * If reclen is 0, we only want to know what the currently active log is,
485  * otherwise we get a lock on this log so nobody can steal our space.
486  *
487  * Assumes caller has already pushed us into the kernel context and is locking.
488  *
489  * NOTE: loghandle is write-locked upon successful return
490  */
491 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
492                                                 struct thandle *th)
493 {
494         struct llog_handle *loghandle = NULL;
495         ENTRY;
496
497
498         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2)) {
499                 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
500                 GOTO(next, loghandle);
501         }
502
503         down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
504         loghandle = cathandle->u.chd.chd_current_log;
505         if (loghandle) {
506                 struct llog_log_hdr *llh;
507
508                 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
509                 llh = loghandle->lgh_hdr;
510                 if (llh == NULL || !llog_is_full(loghandle)) {
511                         up_read(&cathandle->lgh_lock);
512                         RETURN(loghandle);
513                 } else {
514                         up_write(&loghandle->lgh_lock);
515                 }
516         }
517         up_read(&cathandle->lgh_lock);
518
519         /* time to use next log */
520
521         /* first, we have to make sure the state hasn't changed */
522         down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
523         loghandle = cathandle->u.chd.chd_current_log;
524         if (loghandle) {
525                 struct llog_log_hdr *llh;
526
527                 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
528                 llh = loghandle->lgh_hdr;
529                 if (llh == NULL || !llog_is_full(loghandle))
530                         GOTO(out_unlock, loghandle);
531                 else
532                         up_write(&loghandle->lgh_lock);
533         }
534
535 next:
536         /* Sigh, the chd_next_log and chd_current_log is initialized
537          * in declare phase, and we do not serialize the catlog
538          * accessing, so it might be possible the llog creation
539          * thread (see llog_cat_declare_add_rec()) did not create
540          * llog successfully, then the following thread might
541          * meet this situation. */
542         if (IS_ERR_OR_NULL(cathandle->u.chd.chd_next_log)) {
543                 CERROR("%s: next log does not exist!\n",
544                        cathandle->lgh_ctxt->loc_obd->obd_name);
545                 loghandle = ERR_PTR(-EIO);
546                 if (cathandle->u.chd.chd_next_log == NULL) {
547                         /* Store the error in chd_next_log, so
548                          * the following process can get correct
549                          * failure value */
550                         cathandle->u.chd.chd_next_log = loghandle;
551                 }
552                 GOTO(out_unlock, loghandle);
553         }
554
555         CDEBUG(D_INODE, "use next log\n");
556
557         loghandle = cathandle->u.chd.chd_next_log;
558         cathandle->u.chd.chd_current_log = loghandle;
559         cathandle->u.chd.chd_next_log = NULL;
560         down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
561
562 out_unlock:
563         up_write(&cathandle->lgh_lock);
564         LASSERT(loghandle);
565         RETURN(loghandle);
566 }
567
568 /* Add a single record to the recovery log(s) using a catalog
569  * Returns as llog_write_record
570  *
571  * Assumes caller has already pushed us into the kernel context.
572  */
573 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
574                      struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
575                      struct thandle *th)
576 {
577         struct llog_handle *loghandle;
578         int rc, retried = 0;
579         ENTRY;
580
581         LASSERT(rec->lrh_len <= cathandle->lgh_ctxt->loc_chunk_size);
582
583 retry:
584         loghandle = llog_cat_current_log(cathandle, th);
585         if (IS_ERR(loghandle))
586                 RETURN(PTR_ERR(loghandle));
587
588         /* loghandle is already locked by llog_cat_current_log() for us */
589         if (!llog_exist(loghandle)) {
590                 rc = llog_cat_new_log(env, cathandle, loghandle, th);
591                 if (rc < 0) {
592                         up_write(&loghandle->lgh_lock);
593                         /* nobody should be trying to use this llog */
594                         down_write(&cathandle->lgh_lock);
595                         if (cathandle->u.chd.chd_current_log == loghandle)
596                                 cathandle->u.chd.chd_current_log = NULL;
597                         up_write(&cathandle->lgh_lock);
598                         RETURN(rc);
599                 }
600         }
601         /* now let's try to add the record */
602         rc = llog_write_rec(env, loghandle, rec, reccookie, LLOG_NEXT_IDX, th);
603         if (rc < 0) {
604                 CDEBUG_LIMIT(rc == -ENOSPC ? D_HA : D_ERROR,
605                              "llog_write_rec %d: lh=%p\n", rc, loghandle);
606                 /* -ENOSPC is returned if no empty records left
607                  * and when it's lack of space on the stogage.
608                  * there is no point to try again if it's the second
609                  * case. many callers (like llog test) expect ENOSPC,
610                  * so we preserve this error code, but look for the
611                  * actual cause here */
612                 if (rc == -ENOSPC && llog_is_full(loghandle))
613                         rc = -ENOBUFS;
614         }
615         up_write(&loghandle->lgh_lock);
616
617         if (rc == -ENOBUFS) {
618                 if (retried++ == 0)
619                         GOTO(retry, rc);
620                 CERROR("%s: error on 2nd llog: rc = %d\n",
621                        cathandle->lgh_ctxt->loc_obd->obd_name, rc);
622         }
623
624         RETURN(rc);
625 }
626 EXPORT_SYMBOL(llog_cat_add_rec);
627
628 int llog_cat_declare_add_rec(const struct lu_env *env,
629                              struct llog_handle *cathandle,
630                              struct llog_rec_hdr *rec, struct thandle *th)
631 {
632         int rc;
633
634         ENTRY;
635
636 start:
637         down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
638         rc = llog_cat_prep_log(env, cathandle,
639                                &cathandle->u.chd.chd_current_log, th);
640         if (rc)
641                 GOTO(unlock, rc);
642
643         rc = llog_cat_prep_log(env, cathandle, &cathandle->u.chd.chd_next_log,
644                                th);
645         if (rc)
646                 GOTO(unlock, rc);
647
648         rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
649                                     rec, -1, th);
650         if (rc == -ESTALE && dt_object_remote(cathandle->lgh_obj)) {
651                 up_read(&cathandle->lgh_lock);
652                 rc = llog_cat_refresh(env, cathandle);
653                 if (rc)
654                         RETURN(rc);
655                 goto start;
656         }
657
658 #if 0
659         /*
660          * XXX: we hope for declarations made for existing llog this might be
661          * not correct with some backends where declarations are expected
662          * against specific object like ZFS with full debugging enabled.
663          */
664         rc = llog_declare_write_rec(env, cathandle->u.chd.chd_next_log, rec, -1,
665                                     th);
666 #endif
667 unlock:
668         up_read(&cathandle->lgh_lock);
669         RETURN(rc);
670 }
671 EXPORT_SYMBOL(llog_cat_declare_add_rec);
672
673 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
674                  struct llog_rec_hdr *rec, struct llog_cookie *reccookie)
675 {
676         struct llog_ctxt        *ctxt;
677         struct dt_device        *dt;
678         struct thandle          *th = NULL;
679         int                      rc;
680
681         ctxt = cathandle->lgh_ctxt;
682         LASSERT(ctxt);
683         LASSERT(ctxt->loc_exp);
684
685         LASSERT(cathandle->lgh_obj != NULL);
686         dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
687
688         th = dt_trans_create(env, dt);
689         if (IS_ERR(th))
690                 RETURN(PTR_ERR(th));
691
692         rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
693         if (rc)
694                 GOTO(out_trans, rc);
695
696         rc = dt_trans_start_local(env, dt, th);
697         if (rc)
698                 GOTO(out_trans, rc);
699         rc = llog_cat_add_rec(env, cathandle, rec, reccookie, th);
700 out_trans:
701         dt_trans_stop(env, dt, th);
702         RETURN(rc);
703 }
704 EXPORT_SYMBOL(llog_cat_add);
705
706 int llog_cat_cancel_arr_rec(const struct lu_env *env,
707                             struct llog_handle *cathandle,
708                             struct llog_logid *lgl, int count, int *index)
709 {
710         struct llog_handle *loghandle;
711         int  rc;
712
713         ENTRY;
714         rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
715         if (rc) {
716                 CDEBUG(D_HA, "%s: cannot find llog for handle "DFID":%x"
717                        ": rc = %d\n",
718                        cathandle->lgh_ctxt->loc_obd->obd_name,
719                        PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, rc);
720                 RETURN(rc);
721         }
722
723         if ((cathandle->lgh_ctxt->loc_flags &
724              LLOG_CTXT_FLAG_NORMAL_FID) && !llog_exist(loghandle)) {
725                 /* For update log, some of loghandles of cathandle
726                  * might not exist because remote llog creation might
727                  * be failed, so let's skip the record cancellation
728                  * for these non-exist llogs.
729                  */
730                 rc = -ENOENT;
731                 CDEBUG(D_HA, "%s: llog "DFID":%x does not exist"
732                        ": rc = %d\n",
733                        cathandle->lgh_ctxt->loc_obd->obd_name,
734                        PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, rc);
735
736                 llog_handle_put(env, loghandle);
737                 RETURN(rc);
738         }
739
740         rc = llog_cancel_arr_rec(env, loghandle, count, index);
741         if (rc == LLOG_DEL_PLAIN) { /* log has been destroyed */
742                 int cat_index;
743
744                 cat_index = loghandle->u.phd.phd_cookie.lgc_index;
745                 rc = llog_cat_cleanup(env, cathandle, loghandle, cat_index);
746                 if (rc)
747                         CERROR("%s: fail to cancel catalog record: rc = %d\n",
748                                cathandle->lgh_ctxt->loc_obd->obd_name, rc);
749                 rc = 0;
750
751         }
752         llog_handle_put(env, loghandle);
753
754         if (rc)
755                 CERROR("%s: fail to cancel %d llog-records: rc = %d\n",
756                        cathandle->lgh_ctxt->loc_obd->obd_name, count,
757                        rc);
758
759         RETURN(rc);
760 }
761 EXPORT_SYMBOL(llog_cat_cancel_arr_rec);
762
763 /* For each cookie in the cookie array, we clear the log in-use bit and either:
764  * - the log is empty, so mark it free in the catalog header and delete it
765  * - the log is not empty, just write out the log header
766  *
767  * The cookies may be in different log files, so we need to get new logs
768  * each time.
769  *
770  * Assumes caller has already pushed us into the kernel context.
771  */
772 int llog_cat_cancel_records(const struct lu_env *env,
773                             struct llog_handle *cathandle, int count,
774                             struct llog_cookie *cookies)
775 {
776         int i, rc = 0, failed = 0;
777
778         ENTRY;
779
780         for (i = 0; i < count; i++, cookies++) {
781                 int lrc;
782
783                 lrc = llog_cat_cancel_arr_rec(env, cathandle, &cookies->lgc_lgl,
784                                              1, &cookies->lgc_index);
785                 if (lrc) {
786                         failed++;
787                         if (!rc)
788                                 rc = lrc;
789                 }
790         }
791         if (failed)
792                 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
793                        cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
794                        rc);
795         RETURN(rc);
796 }
797 EXPORT_SYMBOL(llog_cat_cancel_records);
798
799 static int llog_cat_process_common(const struct lu_env *env,
800                                    struct llog_handle *cat_llh,
801                                    struct llog_rec_hdr *rec,
802                                    struct llog_handle **llhp)
803 {
804         struct llog_logid_rec *lir = container_of(rec, typeof(*lir), lid_hdr);
805         struct llog_log_hdr *hdr;
806         int rc;
807
808         ENTRY;
809         if (rec->lrh_type != le32_to_cpu(LLOG_LOGID_MAGIC)) {
810                 rc = -EINVAL;
811                 CWARN("%s: invalid record in catalog "DFID":%x: rc = %d\n",
812                       cat_llh->lgh_ctxt->loc_obd->obd_name,
813                       PFID(&cat_llh->lgh_id.lgl_oi.oi_fid),
814                       cat_llh->lgh_id.lgl_ogen, rc);
815                 RETURN(rc);
816         }
817         CDEBUG(D_HA, "processing log "DFID":%x at index %u of catalog "DFID"\n",
818                PFID(&lir->lid_id.lgl_oi.oi_fid), lir->lid_id.lgl_ogen,
819                le32_to_cpu(rec->lrh_index),
820                PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
821
822         rc = llog_cat_id2handle(env, cat_llh, llhp, &lir->lid_id);
823         if (rc) {
824                 /* After a server crash, a stub of index record in catlog could
825                  * be kept, because plain log destroy + catlog index record
826                  * deletion are not atomic. So we end up with an index but no
827                  * actual record. Destroy the index and move on. */
828                 if (rc == -ENOENT || rc == -ESTALE)
829                         rc = LLOG_DEL_RECORD;
830                 else if (rc)
831                         CWARN("%s: can't find llog handle "DFID":%x: rc = %d\n",
832                               cat_llh->lgh_ctxt->loc_obd->obd_name,
833                               PFID(&lir->lid_id.lgl_oi.oi_fid),
834                               lir->lid_id.lgl_ogen, rc);
835
836                 RETURN(rc);
837         }
838
839         /* clean old empty llogs, do not consider current llog in use */
840         /* ignore remote (lgh_obj == NULL) llogs */
841         hdr = (*llhp)->lgh_hdr;
842         if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
843             hdr->llh_count == 1 && cat_llh->lgh_obj != NULL &&
844             *llhp != cat_llh->u.chd.chd_current_log) {
845                 rc = llog_destroy(env, *llhp);
846                 if (rc)
847                         CWARN("%s: can't destroy empty log "DFID": rc = %d\n",
848                               (*llhp)->lgh_ctxt->loc_obd->obd_name,
849                               PFID(&lir->lid_id.lgl_oi.oi_fid), rc);
850                 rc = LLOG_DEL_PLAIN;
851         }
852
853         RETURN(rc);
854 }
855
856 static int llog_cat_process_cb(const struct lu_env *env,
857                                struct llog_handle *cat_llh,
858                                struct llog_rec_hdr *rec, void *data)
859 {
860         struct llog_process_data *d = data;
861         struct llog_handle *llh = NULL;
862         int rc;
863
864         ENTRY;
865         rc = llog_cat_process_common(env, cat_llh, rec, &llh);
866         if (rc)
867                 GOTO(out, rc);
868
869         if (rec->lrh_index < d->lpd_startcat) {
870                 /* Skip processing of the logs until startcat */
871                 rc = 0;
872         } else if (d->lpd_startidx > 0) {
873                 struct llog_process_cat_data cd;
874
875                 cd.lpcd_first_idx = d->lpd_startidx;
876                 cd.lpcd_last_idx = 0;
877                 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
878                                           &cd, false);
879                 /* Continue processing the next log from idx 0 */
880                 d->lpd_startidx = 0;
881         } else {
882                 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
883                                           NULL, false);
884         }
885
886 out:
887         /* The empty plain log was destroyed while processing */
888         if (rc == LLOG_DEL_PLAIN || rc == LLOG_DEL_RECORD)
889                 /* clear wrong catalog entry */
890                 rc = llog_cat_cleanup(env, cat_llh, llh, rec->lrh_index);
891
892         if (llh)
893                 llog_handle_put(env, llh);
894
895         RETURN(rc);
896 }
897
898 int llog_cat_process_or_fork(const struct lu_env *env,
899                              struct llog_handle *cat_llh, llog_cb_t cat_cb,
900                              llog_cb_t cb, void *data, int startcat,
901                              int startidx, bool fork)
902 {
903         struct llog_process_data d;
904         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
905         int rc;
906
907         ENTRY;
908
909         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
910         d.lpd_data = data;
911         d.lpd_cb = cb;
912         d.lpd_startcat = (startcat == LLOG_CAT_FIRST ? 0 : startcat);
913         d.lpd_startidx = startidx;
914
915         if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
916             llh->llh_count > 1) {
917                 struct llog_process_cat_data cd;
918
919                 CWARN("%s: catlog "DFID" crosses index zero\n",
920                       cat_llh->lgh_ctxt->loc_obd->obd_name,
921                       PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
922                 /*startcat = 0 is default value for general processing */
923                 if ((startcat != LLOG_CAT_FIRST &&
924                     startcat >= llh->llh_cat_idx) || !startcat) {
925                         /* processing the catalog part at the end */
926                         cd.lpcd_first_idx = (startcat ? startcat :
927                                              llh->llh_cat_idx);
928                         if (OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS))
929                                 cd.lpcd_last_idx = cfs_fail_val;
930                         else
931                                 cd.lpcd_last_idx = 0;
932                         rc = llog_process_or_fork(env, cat_llh, cat_cb,
933                                                   &d, &cd, fork);
934                         /* Reset the startcat becasue it has already reached
935                          * catalog bottom.
936                          */
937                         startcat = 0;
938                         d.lpd_startcat = 0;
939                         if (rc != 0)
940                                 RETURN(rc);
941                 }
942                 /* processing the catalog part at the begining */
943                 cd.lpcd_first_idx = (startcat == LLOG_CAT_FIRST) ? 0 : startcat;
944                 /* Note, the processing will stop at the lgh_last_idx value,
945                  * and it could be increased during processing. So records
946                  * between current lgh_last_idx and lgh_last_idx in future
947                  * would left unprocessed.
948                  */
949                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
950                 rc = llog_process_or_fork(env, cat_llh, cat_cb,
951                                           &d, &cd, fork);
952         } else {
953                 rc = llog_process_or_fork(env, cat_llh, cat_cb,
954                                           &d, NULL, fork);
955         }
956
957         RETURN(rc);
958 }
959 EXPORT_SYMBOL(llog_cat_process_or_fork);
960
961 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
962                      llog_cb_t cb, void *data, int startcat, int startidx)
963 {
964         return llog_cat_process_or_fork(env, cat_llh, llog_cat_process_cb,
965                                         cb, data, startcat, startidx, false);
966 }
967 EXPORT_SYMBOL(llog_cat_process);
968
969 static int llog_cat_size_cb(const struct lu_env *env,
970                              struct llog_handle *cat_llh,
971                              struct llog_rec_hdr *rec, void *data)
972 {
973         struct llog_process_data *d = data;
974         struct llog_handle *llh = NULL;
975         __u64 *cum_size = d->lpd_data;
976         __u64 size;
977         int rc;
978
979         ENTRY;
980         rc = llog_cat_process_common(env, cat_llh, rec, &llh);
981
982         if (rc == LLOG_DEL_PLAIN) {
983                 /* empty log was deleted, don't count it */
984                 rc = llog_cat_cleanup(env, cat_llh, llh,
985                                       llh->u.phd.phd_cookie.lgc_index);
986         } else if (rc == LLOG_DEL_RECORD) {
987                 /* clear wrong catalog entry */
988                 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
989         } else {
990                 size = llog_size(env, llh);
991                 *cum_size += size;
992
993                 CDEBUG(D_INFO, "Add llog entry "DFID" size=%llu, tot=%llu\n",
994                        PFID(&llh->lgh_id.lgl_oi.oi_fid), size, *cum_size);
995         }
996
997         if (llh != NULL)
998                 llog_handle_put(env, llh);
999
1000         RETURN(0);
1001 }
1002
1003 __u64 llog_cat_size(const struct lu_env *env, struct llog_handle *cat_llh)
1004 {
1005         __u64 size = llog_size(env, cat_llh);
1006
1007         llog_cat_process_or_fork(env, cat_llh, llog_cat_size_cb,
1008                                  NULL, &size, 0, 0, false);
1009
1010         return size;
1011 }
1012 EXPORT_SYMBOL(llog_cat_size);
1013
1014 /* currently returns the number of "free" entries in catalog,
1015  * ie the available entries for a new plain LLOG file creation,
1016  * even if catalog has wrapped
1017  */
1018 __u32 llog_cat_free_space(struct llog_handle *cat_llh)
1019 {
1020         /* simulate almost full Catalog */
1021         if (OBD_FAIL_CHECK(OBD_FAIL_CAT_FREE_RECORDS))
1022                 return cfs_fail_val;
1023
1024         if (cat_llh->lgh_hdr->llh_count == 1)
1025                 return LLOG_HDR_BITMAP_SIZE(cat_llh->lgh_hdr) - 1;
1026
1027         if (cat_llh->lgh_last_idx > cat_llh->lgh_hdr->llh_cat_idx)
1028                 return LLOG_HDR_BITMAP_SIZE(cat_llh->lgh_hdr) - 1 +
1029                        cat_llh->lgh_hdr->llh_cat_idx - cat_llh->lgh_last_idx;
1030
1031         /* catalog is presently wrapped */
1032         return cat_llh->lgh_hdr->llh_cat_idx - cat_llh->lgh_last_idx;
1033 }
1034 EXPORT_SYMBOL(llog_cat_free_space);
1035
1036 static int llog_cat_reverse_process_cb(const struct lu_env *env,
1037                                        struct llog_handle *cat_llh,
1038                                        struct llog_rec_hdr *rec, void *data)
1039 {
1040         struct llog_process_data *d = data;
1041         struct llog_handle *llh;
1042         int rc;
1043
1044         ENTRY;
1045         rc = llog_cat_process_common(env, cat_llh, rec, &llh);
1046
1047         /* The empty plain log was destroyed while processing */
1048         if (rc == LLOG_DEL_PLAIN) {
1049                 rc = llog_cat_cleanup(env, cat_llh, llh,
1050                                       llh->u.phd.phd_cookie.lgc_index);
1051         } else if (rc == LLOG_DEL_RECORD) {
1052                 /* clear wrong catalog entry */
1053                 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
1054         }
1055         if (rc)
1056                 RETURN(rc);
1057
1058         rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
1059
1060         /* The empty plain was destroyed while processing */
1061         if (rc == LLOG_DEL_PLAIN)
1062                 rc = llog_cat_cleanup(env, cat_llh, llh,
1063                                       llh->u.phd.phd_cookie.lgc_index);
1064
1065         llog_handle_put(env, llh);
1066         RETURN(rc);
1067 }
1068
1069 int llog_cat_reverse_process(const struct lu_env *env,
1070                              struct llog_handle *cat_llh,
1071                              llog_cb_t cb, void *data)
1072 {
1073         struct llog_process_data d;
1074         struct llog_process_cat_data cd;
1075         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
1076         int rc;
1077         ENTRY;
1078
1079         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
1080         d.lpd_data = data;
1081         d.lpd_cb = cb;
1082
1083         if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
1084             llh->llh_count > 1) {
1085                 CWARN("%s: catalog "DFID" crosses index zero\n",
1086                       cat_llh->lgh_ctxt->loc_obd->obd_name,
1087                       PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
1088
1089                 cd.lpcd_first_idx = 0;
1090                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
1091                 rc = llog_reverse_process(env, cat_llh,
1092                                           llog_cat_reverse_process_cb,
1093                                           &d, &cd);
1094                 if (rc != 0)
1095                         RETURN(rc);
1096
1097                 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
1098                 cd.lpcd_last_idx = 0;
1099                 rc = llog_reverse_process(env, cat_llh,
1100                                           llog_cat_reverse_process_cb,
1101                                           &d, &cd);
1102         } else {
1103                 rc = llog_reverse_process(env, cat_llh,
1104                                           llog_cat_reverse_process_cb,
1105                                           &d, NULL);
1106         }
1107
1108         RETURN(rc);
1109 }
1110 EXPORT_SYMBOL(llog_cat_reverse_process);
1111
1112 static int llog_cat_set_first_idx(struct llog_handle *cathandle, int idx)
1113 {
1114         struct llog_log_hdr *llh = cathandle->lgh_hdr;
1115         int bitmap_size;
1116
1117         ENTRY;
1118
1119         bitmap_size = LLOG_HDR_BITMAP_SIZE(llh);
1120         /*
1121          * The llh_cat_idx equals to the first used index minus 1
1122          * so if we canceled the first index then llh_cat_idx
1123          * must be renewed.
1124          */
1125         if (llh->llh_cat_idx == (idx - 1)) {
1126                 llh->llh_cat_idx = idx;
1127
1128                 while (idx != cathandle->lgh_last_idx) {
1129                         idx = (idx + 1) % bitmap_size;
1130                         if (!test_bit_le(idx, LLOG_HDR_BITMAP(llh))) {
1131                                 /* update llh_cat_idx for each unset bit,
1132                                  * expecting the next one is set */
1133                                 llh->llh_cat_idx = idx;
1134                         } else if (idx == 0) {
1135                                 /* skip header bit */
1136                                 llh->llh_cat_idx = 0;
1137                                 continue;
1138                         } else {
1139                                 /* the first index is found */
1140                                 break;
1141                         }
1142                 }
1143
1144                 CDEBUG(D_HA, "catlog "DFID" first idx %u, last_idx %u\n",
1145                        PFID(&cathandle->lgh_id.lgl_oi.oi_fid),
1146                        llh->llh_cat_idx, cathandle->lgh_last_idx);
1147         }
1148
1149         RETURN(0);
1150 }
1151
1152 /* Cleanup deleted plain llog traces from catalog */
1153 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
1154                      struct llog_handle *loghandle, int index)
1155 {
1156         int rc;
1157         struct lu_fid fid = {.f_seq = 0, .f_oid = 0, .f_ver = 0};
1158
1159         LASSERT(index);
1160         if (loghandle != NULL) {
1161                 /* remove destroyed llog from catalog list and
1162                  * chd_current_log variable */
1163                 fid = loghandle->lgh_id.lgl_oi.oi_fid;
1164                 down_write(&cathandle->lgh_lock);
1165                 if (cathandle->u.chd.chd_current_log == loghandle)
1166                         cathandle->u.chd.chd_current_log = NULL;
1167                 list_del_init(&loghandle->u.phd.phd_entry);
1168                 up_write(&cathandle->lgh_lock);
1169                 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index ||
1170                         loghandle->u.phd.phd_cookie.lgc_index == 0);
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,
1184                        "cancel plain log "DFID" at index %u of catalog "DFID"\n",
1185                        PFID(&fid), index,
1186                        PFID(&cathandle->lgh_id.lgl_oi.oi_fid));
1187         return rc;
1188 }