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