Whamcloud - gitweb
18a41f935de5e4fc4b704591f9105ed408c57405
[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 /* For each cookie in the cookie array, we clear the log in-use bit and either:
675  * - the log is empty, so mark it free in the catalog header and delete it
676  * - the log is not empty, just write out the log header
677  *
678  * The cookies may be in different log files, so we need to get new logs
679  * each time.
680  *
681  * Assumes caller has already pushed us into the kernel context.
682  */
683 int llog_cat_cancel_records(const struct lu_env *env,
684                             struct llog_handle *cathandle, int count,
685                             struct llog_cookie *cookies)
686 {
687         int i, index, rc = 0, failed = 0;
688
689         ENTRY;
690
691         for (i = 0; i < count; i++, cookies++) {
692                 struct llog_handle *loghandle;
693                 struct llog_logid *lgl = &cookies->lgc_lgl;
694                 int  lrc;
695
696                 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
697                 if (rc) {
698                         CDEBUG(D_HA, "%s: cannot find llog for handle "DFID":%x"
699                                ": rc = %d\n",
700                                cathandle->lgh_ctxt->loc_obd->obd_name,
701                                PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, rc);
702                         failed++;
703                         continue;
704                 }
705
706                 if ((cathandle->lgh_ctxt->loc_flags &
707                      LLOG_CTXT_FLAG_NORMAL_FID) && !llog_exist(loghandle)) {
708                         /* For update log, some of loghandles of cathandle
709                          * might not exist because remote llog creation might
710                          * be failed, so let's skip the record cancellation
711                          * for these non-exist llogs.
712                          */
713                         lrc = -ENOENT;
714                         CDEBUG(D_HA, "%s: llog "DFID":%x does not exist"
715                                ": rc = %d\n",
716                                cathandle->lgh_ctxt->loc_obd->obd_name,
717                                PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, lrc);
718                         failed++;
719                         if (rc == 0)
720                                 rc = lrc;
721                         continue;
722                 }
723
724                 lrc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
725                 if (lrc == LLOG_DEL_PLAIN) { /* log has been destroyed */
726                         index = loghandle->u.phd.phd_cookie.lgc_index;
727                         lrc = llog_cat_cleanup(env, cathandle, loghandle,
728                                                index);
729                         if (rc == 0)
730                                 rc = lrc;
731                 } else if (lrc == -ENOENT) {
732                         if (rc == 0) /* ENOENT shouldn't rewrite any error */
733                                 rc = lrc;
734                 } else if (lrc < 0) {
735                         failed++;
736                         if (rc == 0)
737                                 rc = lrc;
738                 }
739                 llog_handle_put(loghandle);
740         }
741         if (rc)
742                 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
743                        cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
744                        rc);
745
746         RETURN(rc);
747 }
748 EXPORT_SYMBOL(llog_cat_cancel_records);
749
750 static int llog_cat_process_common(const struct lu_env *env,
751                                    struct llog_handle *cat_llh,
752                                    struct llog_rec_hdr *rec,
753                                    struct llog_handle **llhp)
754 {
755         struct llog_logid_rec *lir = container_of(rec, typeof(*lir), lid_hdr);
756         struct llog_log_hdr *hdr;
757         int rc;
758
759         ENTRY;
760         if (rec->lrh_type != le32_to_cpu(LLOG_LOGID_MAGIC)) {
761                 rc = -EINVAL;
762                 CWARN("%s: invalid record in catalog "DFID":%x: rc = %d\n",
763                       cat_llh->lgh_ctxt->loc_obd->obd_name,
764                       PFID(&cat_llh->lgh_id.lgl_oi.oi_fid),
765                       cat_llh->lgh_id.lgl_ogen, rc);
766                 RETURN(rc);
767         }
768         CDEBUG(D_HA, "processing log "DFID":%x at index %u of catalog "DFID"\n",
769                PFID(&lir->lid_id.lgl_oi.oi_fid), lir->lid_id.lgl_ogen,
770                le32_to_cpu(rec->lrh_index),
771                PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
772
773         rc = llog_cat_id2handle(env, cat_llh, llhp, &lir->lid_id);
774         if (rc) {
775                 /* After a server crash, a stub of index record in catlog could
776                  * be kept, because plain log destroy + catlog index record
777                  * deletion are not atomic. So we end up with an index but no
778                  * actual record. Destroy the index and move on. */
779                 if (rc == -ENOENT || rc == -ESTALE)
780                         rc = LLOG_DEL_RECORD;
781                 else if (rc)
782                         CWARN("%s: can't find llog handle "DFID":%x: rc = %d\n",
783                               cat_llh->lgh_ctxt->loc_obd->obd_name,
784                               PFID(&lir->lid_id.lgl_oi.oi_fid),
785                               lir->lid_id.lgl_ogen, rc);
786
787                 RETURN(rc);
788         }
789
790         /* clean old empty llogs, do not consider current llog in use */
791         /* ignore remote (lgh_obj == NULL) llogs */
792         hdr = (*llhp)->lgh_hdr;
793         if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
794             hdr->llh_count == 1 && cat_llh->lgh_obj != NULL &&
795             *llhp != cat_llh->u.chd.chd_current_log) {
796                 rc = llog_destroy(env, *llhp);
797                 if (rc)
798                         CWARN("%s: can't destroy empty log "DFID": rc = %d\n",
799                               (*llhp)->lgh_ctxt->loc_obd->obd_name,
800                               PFID(&lir->lid_id.lgl_oi.oi_fid), rc);
801                 rc = LLOG_DEL_PLAIN;
802         }
803
804         RETURN(rc);
805 }
806
807 static int llog_cat_process_cb(const struct lu_env *env,
808                                struct llog_handle *cat_llh,
809                                struct llog_rec_hdr *rec, void *data)
810 {
811         struct llog_process_data *d = data;
812         struct llog_handle *llh = NULL;
813         int rc;
814
815         ENTRY;
816         rc = llog_cat_process_common(env, cat_llh, rec, &llh);
817         if (rc)
818                 GOTO(out, rc);
819
820         if (rec->lrh_index < d->lpd_startcat) {
821                 /* Skip processing of the logs until startcat */
822                 rc = 0;
823         } else if (d->lpd_startidx > 0) {
824                 struct llog_process_cat_data cd;
825
826                 cd.lpcd_first_idx = d->lpd_startidx;
827                 cd.lpcd_last_idx = 0;
828                 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
829                                           &cd, false);
830                 /* Continue processing the next log from idx 0 */
831                 d->lpd_startidx = 0;
832         } else {
833                 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
834                                           NULL, false);
835         }
836
837 out:
838         /* The empty plain log was destroyed while processing */
839         if (rc == LLOG_DEL_PLAIN) {
840                 rc = llog_cat_cleanup(env, cat_llh, llh,
841                                       llh->u.phd.phd_cookie.lgc_index);
842         } else if (rc == LLOG_DEL_RECORD) {
843                 /* clear wrong catalog entry */
844                 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
845         }
846
847         if (llh)
848                 llog_handle_put(llh);
849
850         RETURN(rc);
851 }
852
853 int llog_cat_process_or_fork(const struct lu_env *env,
854                              struct llog_handle *cat_llh, llog_cb_t cat_cb,
855                              llog_cb_t cb, void *data, int startcat,
856                              int startidx, bool fork)
857 {
858         struct llog_process_data d;
859         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
860         int rc;
861         ENTRY;
862
863         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
864         d.lpd_data = data;
865         d.lpd_cb = cb;
866         d.lpd_startcat = startcat;
867         d.lpd_startidx = startidx;
868
869         if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
870             llh->llh_count > 1) {
871                 struct llog_process_cat_data cd;
872
873                 CWARN("%s: catlog "DFID" crosses index zero\n",
874                       cat_llh->lgh_ctxt->loc_obd->obd_name,
875                       PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
876
877                 cd.lpcd_first_idx = llh->llh_cat_idx;
878                 cd.lpcd_last_idx = 0;
879                 rc = llog_process_or_fork(env, cat_llh, cat_cb,
880                                           &d, &cd, fork);
881                 if (rc != 0)
882                         RETURN(rc);
883
884                 cd.lpcd_first_idx = 0;
885                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
886                 rc = llog_process_or_fork(env, cat_llh, cat_cb,
887                                           &d, &cd, fork);
888         } else {
889                 rc = llog_process_or_fork(env, cat_llh, cat_cb,
890                                           &d, NULL, fork);
891         }
892
893         RETURN(rc);
894 }
895
896 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
897                      llog_cb_t cb, void *data, int startcat, int startidx)
898 {
899         return llog_cat_process_or_fork(env, cat_llh, llog_cat_process_cb,
900                                         cb, data, startcat, startidx, false);
901 }
902 EXPORT_SYMBOL(llog_cat_process);
903
904 static int llog_cat_size_cb(const struct lu_env *env,
905                              struct llog_handle *cat_llh,
906                              struct llog_rec_hdr *rec, void *data)
907 {
908         struct llog_process_data *d = data;
909         struct llog_handle *llh = NULL;
910         __u64 *cum_size = d->lpd_data;
911         __u64 size;
912         int rc;
913
914         ENTRY;
915         rc = llog_cat_process_common(env, cat_llh, rec, &llh);
916
917         if (rc == LLOG_DEL_PLAIN) {
918                 /* empty log was deleted, don't count it */
919                 rc = llog_cat_cleanup(env, cat_llh, llh,
920                                       llh->u.phd.phd_cookie.lgc_index);
921         } else if (rc == LLOG_DEL_RECORD) {
922                 /* clear wrong catalog entry */
923                 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
924         } else {
925                 size = llog_size(env, llh);
926                 *cum_size += size;
927
928                 CDEBUG(D_INFO, "Add llog entry "DFID" size=%llu, tot=%llu\n",
929                        PFID(&llh->lgh_id.lgl_oi.oi_fid), size, *cum_size);
930         }
931
932         if (llh != NULL)
933                 llog_handle_put(llh);
934
935         RETURN(0);
936 }
937
938 __u64 llog_cat_size(const struct lu_env *env, struct llog_handle *cat_llh)
939 {
940         __u64 size = llog_size(env, cat_llh);
941
942         llog_cat_process_or_fork(env, cat_llh, llog_cat_size_cb,
943                                  NULL, &size, 0, 0, false);
944
945         return size;
946 }
947 EXPORT_SYMBOL(llog_cat_size);
948
949 /* currently returns the number of "free" entries in catalog,
950  * ie the available entries for a new plain LLOG file creation,
951  * even if catalog has wrapped
952  */
953 __u32 llog_cat_free_space(struct llog_handle *cat_llh)
954 {
955         /* simulate almost full Catalog */
956         if (OBD_FAIL_CHECK(OBD_FAIL_CAT_FREE_RECORDS))
957                 return cfs_fail_val;
958
959         if (cat_llh->lgh_hdr->llh_count == 1)
960                 return LLOG_HDR_BITMAP_SIZE(cat_llh->lgh_hdr) - 1;
961
962         if (cat_llh->lgh_last_idx > cat_llh->lgh_hdr->llh_cat_idx)
963                 return LLOG_HDR_BITMAP_SIZE(cat_llh->lgh_hdr) - 1 +
964                        cat_llh->lgh_hdr->llh_cat_idx - cat_llh->lgh_last_idx;
965
966         /* catalog is presently wrapped */
967         return cat_llh->lgh_hdr->llh_cat_idx - cat_llh->lgh_last_idx;
968 }
969 EXPORT_SYMBOL(llog_cat_free_space);
970
971 static int llog_cat_reverse_process_cb(const struct lu_env *env,
972                                        struct llog_handle *cat_llh,
973                                        struct llog_rec_hdr *rec, void *data)
974 {
975         struct llog_process_data *d = data;
976         struct llog_handle *llh;
977         int rc;
978
979         ENTRY;
980         rc = llog_cat_process_common(env, cat_llh, rec, &llh);
981
982         /* The empty plain log was destroyed while processing */
983         if (rc == LLOG_DEL_PLAIN) {
984                 rc = llog_cat_cleanup(env, cat_llh, llh,
985                                       llh->u.phd.phd_cookie.lgc_index);
986         } else if (rc == LLOG_DEL_RECORD) {
987                 /* clear wrong catalog entry */
988                 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
989         }
990         if (rc)
991                 RETURN(rc);
992
993         rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
994
995         /* The empty plain was destroyed while processing */
996         if (rc == LLOG_DEL_PLAIN)
997                 rc = llog_cat_cleanup(env, cat_llh, llh,
998                                       llh->u.phd.phd_cookie.lgc_index);
999
1000         llog_handle_put(llh);
1001         RETURN(rc);
1002 }
1003
1004 int llog_cat_reverse_process(const struct lu_env *env,
1005                              struct llog_handle *cat_llh,
1006                              llog_cb_t cb, void *data)
1007 {
1008         struct llog_process_data d;
1009         struct llog_process_cat_data cd;
1010         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
1011         int rc;
1012         ENTRY;
1013
1014         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
1015         d.lpd_data = data;
1016         d.lpd_cb = cb;
1017
1018         if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
1019             llh->llh_count > 1) {
1020                 CWARN("%s: catalog "DFID" crosses index zero\n",
1021                       cat_llh->lgh_ctxt->loc_obd->obd_name,
1022                       PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
1023
1024                 cd.lpcd_first_idx = 0;
1025                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
1026                 rc = llog_reverse_process(env, cat_llh,
1027                                           llog_cat_reverse_process_cb,
1028                                           &d, &cd);
1029                 if (rc != 0)
1030                         RETURN(rc);
1031
1032                 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
1033                 cd.lpcd_last_idx = 0;
1034                 rc = llog_reverse_process(env, cat_llh,
1035                                           llog_cat_reverse_process_cb,
1036                                           &d, &cd);
1037         } else {
1038                 rc = llog_reverse_process(env, cat_llh,
1039                                           llog_cat_reverse_process_cb,
1040                                           &d, NULL);
1041         }
1042
1043         RETURN(rc);
1044 }
1045 EXPORT_SYMBOL(llog_cat_reverse_process);
1046
1047 static int llog_cat_set_first_idx(struct llog_handle *cathandle, int idx)
1048 {
1049         struct llog_log_hdr *llh = cathandle->lgh_hdr;
1050         int bitmap_size;
1051
1052         ENTRY;
1053
1054         bitmap_size = LLOG_HDR_BITMAP_SIZE(llh);
1055         /*
1056          * The llh_cat_idx equals to the first used index minus 1
1057          * so if we canceled the first index then llh_cat_idx
1058          * must be renewed.
1059          */
1060         if (llh->llh_cat_idx == (idx - 1)) {
1061                 llh->llh_cat_idx = idx;
1062
1063                 while (idx != cathandle->lgh_last_idx) {
1064                         idx = (idx + 1) % bitmap_size;
1065                         if (!ext2_test_bit(idx, LLOG_HDR_BITMAP(llh))) {
1066                                 /* update llh_cat_idx for each unset bit,
1067                                  * expecting the next one is set */
1068                                 llh->llh_cat_idx = idx;
1069                         } else if (idx == 0) {
1070                                 /* skip header bit */
1071                                 llh->llh_cat_idx = 0;
1072                                 continue;
1073                         } else {
1074                                 /* the first index is found */
1075                                 break;
1076                         }
1077                 }
1078
1079                 CDEBUG(D_RPCTRACE, "catlog "DFID" first idx %u, last_idx %u\n",
1080                        PFID(&cathandle->lgh_id.lgl_oi.oi_fid),
1081                        llh->llh_cat_idx, cathandle->lgh_last_idx);
1082         }
1083
1084         RETURN(0);
1085 }
1086
1087 /* Cleanup deleted plain llog traces from catalog */
1088 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
1089                      struct llog_handle *loghandle, int index)
1090 {
1091         int rc;
1092
1093         LASSERT(index);
1094         if (loghandle != NULL) {
1095                 /* remove destroyed llog from catalog list and
1096                  * chd_current_log variable */
1097                 down_write(&cathandle->lgh_lock);
1098                 if (cathandle->u.chd.chd_current_log == loghandle)
1099                         cathandle->u.chd.chd_current_log = NULL;
1100                 list_del_init(&loghandle->u.phd.phd_entry);
1101                 up_write(&cathandle->lgh_lock);
1102                 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index);
1103                 /* llog was opened and keep in a list, close it now */
1104                 llog_close(env, loghandle);
1105         }
1106
1107         /* do not attempt to cleanup on-disk llog if on client side */
1108         if (cathandle->lgh_obj == NULL)
1109                 return 0;
1110
1111         /* remove plain llog entry from catalog by index */
1112         llog_cat_set_first_idx(cathandle, index);
1113         rc = llog_cancel_rec(env, cathandle, index);
1114         if (rc == 0)
1115                 CDEBUG(D_HA, "cancel plain log at index %u of catalog "DFID"\n",
1116                        index, PFID(&cathandle->lgh_id.lgl_oi.oi_fid));
1117         return rc;
1118 }