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