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