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