Whamcloud - gitweb
LU-14111 obdclass: count eviction per obd_device
[fs/lustre-release.git] / lustre / obdclass / llog_osd.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  */
31 /*
32  * lustre/obdclass/llog_osd.c
33  *
34  * Low level llog routines on top of OSD API
35  *
36  * This file provides set of methods for llog operations on top of
37  * dt_device. It contains all supported llog_operations interfaces and
38  * supplimental functions.
39  *
40  * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
41  * Author: Mikhail Pershin <mike.pershin@intel.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_LOG
45
46 #include <linux/delay.h>
47
48 #include <dt_object.h>
49 #include <llog_swab.h>
50 #include <lustre_fid.h>
51 #include <obd.h>
52 #include <obd_class.h>
53
54 #include "llog_internal.h"
55 #include "local_storage.h"
56
57 /**
58  * Implementation of the llog_operations::lop_declare_create
59  *
60  * This function is a wrapper over local_storage API function
61  * local_object_declare_create().
62  *
63  * \param[in] env       execution environment
64  * \param[in] los       local_storage for bottom storage device
65  * \param[in] o         dt_object to create
66  * \param[in] th        current transaction handle
67  *
68  * \retval              0 on successful declaration of the new object
69  * \retval              negative error if declaration was failed
70  */
71 static int llog_osd_declare_new_object(const struct lu_env *env,
72                                        struct local_oid_storage *los,
73                                        struct dt_object *o,
74                                        struct thandle *th)
75 {
76         struct llog_thread_info *lgi = llog_info(env);
77
78         lgi->lgi_attr.la_valid = LA_MODE;
79         lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
80         lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
81
82         return local_object_declare_create(env, los, o, &lgi->lgi_attr,
83                                            &lgi->lgi_dof, th);
84 }
85
86 /**
87  * Implementation of the llog_operations::lop_create
88  *
89  * This function is a wrapper over local_storage API function
90  * local_object_create().
91  *
92  * \param[in] env       execution environment
93  * \param[in] los       local_storage for bottom storage device
94  * \param[in] o         dt_object to create
95  * \param[in] th        current transaction handle
96  *
97  * \retval              0 on successful creation of the new object
98  * \retval              negative error if creation was failed
99  */
100 static int llog_osd_create_new_object(const struct lu_env *env,
101                                       struct local_oid_storage *los,
102                                       struct dt_object *o,
103                                       struct thandle *th)
104 {
105         struct llog_thread_info *lgi = llog_info(env);
106
107         lgi->lgi_attr.la_valid = LA_MODE;
108         lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
109         lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
110
111         return local_object_create(env, los, o, &lgi->lgi_attr,
112                                    &lgi->lgi_dof, th);
113 }
114
115 /**
116  * Implementation of the llog_operations::lop_exist
117  *
118  * This function checks that llog exists on storage.
119  *
120  * \param[in] handle    llog handle of the current llog
121  *
122  * \retval              true if llog object exists and is not just destroyed
123  * \retval              false if llog doesn't exist or just destroyed
124  */
125 static int llog_osd_exist(struct llog_handle *handle)
126 {
127         LASSERT(handle->lgh_obj);
128         return dt_object_exists(handle->lgh_obj) && !handle->lgh_destroyed;
129 }
130
131 static void *rec_tail(struct llog_rec_hdr *rec)
132 {
133         return (void *)((char *)rec + rec->lrh_len -
134                         sizeof(struct llog_rec_tail));
135 }
136
137 /**
138  * Write a padding record to the llog
139  *
140  * This function writes a padding record to the end of llog. That may
141  * be needed if llog contains records of variable size, e.g. config logs
142  * or changelogs.
143  * The padding record just aligns llog to the llog chunk_size boundary if
144  * the current record doesn't fit in the remaining space.
145  *
146  * It allocates full length to avoid two separate writes for header and tail.
147  * Such 2-steps scheme needs extra protection and complex error handling.
148  *
149  * \param[in]     env   execution environment
150  * \param[in]     o     dt_object to create
151  * \param[in,out] off   pointer to the padding start offset
152  * \param[in]     len   padding length
153  * \param[in]     index index of the padding record in a llog
154  * \param[in]     th    current transaction handle
155  *
156  * \retval              0 on successful padding write
157  * \retval              negative error if write failed
158  */
159 static int llog_osd_pad(const struct lu_env *env, struct dt_object *o,
160                         loff_t *off, int len, int index, struct thandle *th)
161 {
162         struct llog_thread_info *lgi = llog_info(env);
163         struct llog_rec_hdr     *rec;
164         struct llog_rec_tail    *tail;
165         int                      rc;
166
167         ENTRY;
168
169         LASSERT(th);
170         LASSERT(off);
171         LASSERT(len >= LLOG_MIN_REC_SIZE && (len & 0x7) == 0);
172
173         OBD_ALLOC(rec, len);
174         if (rec == NULL)
175                 RETURN(-ENOMEM);
176
177         rec->lrh_len = len;
178         rec->lrh_index = index;
179         rec->lrh_type = LLOG_PAD_MAGIC;
180
181         tail = rec_tail(rec);
182         tail->lrt_len = len;
183         tail->lrt_index = index;
184
185         lgi->lgi_buf.lb_buf = rec;
186         lgi->lgi_buf.lb_len = len;
187         rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
188         if (rc)
189                 CERROR("%s: error writing padding record: rc = %d\n",
190                        o->do_lu.lo_dev->ld_obd->obd_name, rc);
191
192         OBD_FREE(rec, len);
193         RETURN(rc);
194 }
195
196 /**
197  * Implementation of the llog_operations::lop_read_header
198  *
199  * This function reads the current llog header from the bottom storage
200  * device.
201  *
202  * \param[in] env       execution environment
203  * \param[in] handle    llog handle of the current llog
204  *
205  * \retval              0 on successful header read
206  * \retval              negative error if read failed
207  */
208 static int llog_osd_read_header(const struct lu_env *env,
209                                 struct llog_handle *handle)
210 {
211         struct llog_rec_hdr     *llh_hdr;
212         struct dt_object        *o;
213         struct llog_thread_info *lgi;
214         enum llog_flag           flags;
215         int                      rc;
216
217         ENTRY;
218
219         o = handle->lgh_obj;
220         LASSERT(o);
221
222         lgi = llog_info(env);
223
224         dt_read_lock(env, o, 0);
225
226         rc = dt_attr_get(env, o, &lgi->lgi_attr);
227         if (rc)
228                 GOTO(unlock, rc);
229
230         LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
231
232         if (lgi->lgi_attr.la_size == 0) {
233                 CDEBUG(D_HA, "not reading header from 0-byte log\n");
234                 GOTO(unlock, rc = LLOG_EEMPTY);
235         }
236
237         flags = handle->lgh_hdr->llh_flags;
238
239         lgi->lgi_off = 0;
240         lgi->lgi_buf.lb_buf = handle->lgh_hdr;
241         lgi->lgi_buf.lb_len = handle->lgh_hdr_size;
242         rc = dt_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
243         llh_hdr = &handle->lgh_hdr->llh_hdr;
244         if (rc < sizeof(*llh_hdr) || rc < llh_hdr->lrh_len) {
245                 CERROR("%s: error reading "DFID" log header size %d: rc = %d\n",
246                        o->do_lu.lo_dev->ld_obd->obd_name,
247                        PFID(lu_object_fid(&o->do_lu)), rc < 0 ? 0 : rc,
248                        -EFAULT);
249
250                 if (rc >= 0)
251                         rc = -EFAULT;
252
253                 GOTO(unlock, rc);
254         }
255
256         if (LLOG_REC_HDR_NEEDS_SWABBING(llh_hdr))
257                 lustre_swab_llog_hdr(handle->lgh_hdr);
258
259         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
260                 CERROR("%s: bad log %s "DFID" header magic: %#x "
261                        "(expected %#x)\n", o->do_lu.lo_dev->ld_obd->obd_name,
262                        handle->lgh_name ? handle->lgh_name : "",
263                        PFID(lu_object_fid(&o->do_lu)),
264                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
265                 GOTO(unlock, rc = -EIO);
266         } else if (llh_hdr->lrh_len < LLOG_MIN_CHUNK_SIZE ||
267                    llh_hdr->lrh_len > handle->lgh_hdr_size) {
268                 CERROR("%s: incorrectly sized log %s "DFID" header: "
269                        "%#x (expected at least %#x)\n"
270                        "you may need to re-run lconf --write_conf.\n",
271                        o->do_lu.lo_dev->ld_obd->obd_name,
272                        handle->lgh_name ? handle->lgh_name : "",
273                        PFID(lu_object_fid(&o->do_lu)),
274                        llh_hdr->lrh_len, LLOG_MIN_CHUNK_SIZE);
275                 GOTO(unlock, rc = -EIO);
276         } else if (LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index >
277                    LLOG_HDR_BITMAP_SIZE(handle->lgh_hdr) ||
278                    LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len !=
279                         llh_hdr->lrh_len) {
280                 CERROR("%s: incorrectly sized log %s "DFID" tailer: "
281                        "%#x : rc = %d\n",
282                        o->do_lu.lo_dev->ld_obd->obd_name,
283                        handle->lgh_name ? handle->lgh_name : "",
284                        PFID(lu_object_fid(&o->do_lu)),
285                        LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len, -EIO);
286                 GOTO(unlock, rc = -EIO);
287         }
288
289         handle->lgh_hdr->llh_flags |= (flags & LLOG_F_EXT_MASK);
290         handle->lgh_last_idx = LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index;
291         rc = 0;
292
293 unlock:
294         dt_read_unlock(env, o);
295         RETURN(rc);
296 }
297
298 /**
299  * Implementation of the llog_operations::lop_declare_write
300  *
301  * This function declares the new record write.
302  *
303  * \param[in] env       execution environment
304  * \param[in] loghandle llog handle of the current llog
305  * \param[in] rec       llog record header. This is a real header of the full
306  *                      llog record to write. This is the beginning of buffer
307  *                      to write, the length of buffer is stored in
308  *                      \a rec::lrh_len
309  * \param[in] idx       index of the llog record. If \a idx == -1 then this is
310  *                      append case, otherwise \a idx is the index of record
311  *                      to modify
312  * \param[in] th        current transaction handle
313  *
314  * \retval              0 on successful declaration
315  * \retval              negative error if declaration failed
316  */
317 static int llog_osd_declare_write_rec(const struct lu_env *env,
318                                       struct llog_handle *loghandle,
319                                       struct llog_rec_hdr *rec,
320                                       int idx, struct thandle *th)
321 {
322         struct llog_thread_info *lgi = llog_info(env);
323         __u32                   chunk_size;
324         struct dt_object        *o;
325         int                      rc;
326
327         ENTRY;
328
329         LASSERT(env);
330         LASSERT(th);
331         LASSERT(loghandle);
332         LASSERT(rec);
333         LASSERT(rec->lrh_len <= loghandle->lgh_ctxt->loc_chunk_size);
334
335         o = loghandle->lgh_obj;
336         LASSERT(o);
337
338         chunk_size = loghandle->lgh_ctxt->loc_chunk_size;
339         lgi->lgi_buf.lb_len = chunk_size;
340         lgi->lgi_buf.lb_buf = NULL;
341         /* each time we update header */
342         rc = dt_declare_record_write(env, o, &lgi->lgi_buf, 0, th);
343         if (rc || idx == 0) /* if error or just header */
344                 RETURN(rc);
345
346         /**
347          * the pad record can be inserted so take into account double
348          * record size: pad and the actual record into a new block
349          */
350         lgi->lgi_buf.lb_len = rec->lrh_len * 2;
351         lgi->lgi_buf.lb_buf = NULL;
352         /* XXX: implement declared window or multi-chunks approach */
353         rc = dt_declare_record_write(env, o, &lgi->lgi_buf, -1, th);
354
355         RETURN(rc);
356 }
357
358 /**
359  * Implementation of the llog_operations::lop_write
360  *
361  * This function writes the new record in the llog or modify the existed one.
362  *
363  * \param[in]  env              execution environment
364  * \param[in]  loghandle        llog handle of the current llog
365  * \param[in]  rec              llog record header. This is a real header of
366  *                              the full llog record to write. This is
367  *                              the beginning of buffer to write, the length
368  *                              of buffer is stored in \a rec::lrh_len
369  * \param[in,out] reccookie     pointer to the cookie to return back if needed.
370  *                              It is used for further cancel of this llog
371  *                              record.
372  * \param[in]  idx              index of the llog record. If \a idx == -1 then
373  *                              this is append case, otherwise \a idx is
374  *                              the index of record to modify
375  * \param[in]  th               current transaction handle
376  *
377  * \retval                      0 on successful write && \a reccookie == NULL
378  *                              1 on successful write && \a reccookie != NULL
379  * \retval                      negative error if write failed
380  */
381 static int llog_osd_write_rec(const struct lu_env *env,
382                               struct llog_handle *loghandle,
383                               struct llog_rec_hdr *rec,
384                               struct llog_cookie *reccookie,
385                               int idx, struct thandle *th)
386 {
387         struct llog_thread_info *lgi = llog_info(env);
388         struct llog_log_hdr *llh;
389         int reclen = rec->lrh_len;
390         int index, rc;
391         struct llog_rec_tail *lrt;
392         struct dt_object *o;
393         __u32 chunk_size;
394         size_t left;
395         __u32 orig_last_idx;
396         bool pad = false;
397         ENTRY;
398
399         llh = loghandle->lgh_hdr;
400         o = loghandle->lgh_obj;
401
402         chunk_size = llh->llh_hdr.lrh_len;
403         CDEBUG(D_OTHER, "new record %x to "DFID"\n",
404                rec->lrh_type, PFID(lu_object_fid(&o->do_lu)));
405
406         if (!llog_osd_exist(loghandle))
407                 RETURN(-ENOENT);
408
409         /* record length should not bigger than  */
410         if (reclen > loghandle->lgh_hdr->llh_hdr.lrh_len)
411                 RETURN(-E2BIG);
412
413         /* sanity check for fixed-records llog */
414         if (idx != LLOG_HEADER_IDX && (llh->llh_flags & LLOG_F_IS_FIXSIZE)) {
415                 LASSERT(llh->llh_size != 0);
416                 LASSERT(llh->llh_size == reclen);
417         }
418
419         /* return error if osp object is stale */
420         if (idx != LLOG_HEADER_IDX && dt_object_stale(o))
421                 RETURN(-ESTALE);
422         rc = dt_attr_get(env, o, &lgi->lgi_attr);
423         if (rc)
424                 RETURN(rc);
425
426         /**
427          * The modification case.
428          * If idx set then the record with that index must be modified.
429          * There are three cases possible:
430          * 1) the common case is the llog header update (idx == 0)
431          * 2) the llog record modification during llog process.
432          *    This is indicated by the \a loghandle::lgh_cur_idx > 0.
433          *    In that case the \a loghandle::lgh_cur_offset
434          * 3) otherwise this is assumed that llog consist of records of
435          *    fixed size, i.e. catalog. The llog header must has llh_size
436          *    field equal to record size. The record offset is calculated
437          *    just by /a idx value
438          *
439          * During modification we don't need extra header update because
440          * the bitmap and record count are not changed. The record header
441          * and tail remains the same too.
442          */
443         if (idx != LLOG_NEXT_IDX) {
444                 /* llog can be empty only when first record is being written */
445                 LASSERT(ergo(idx > 0, lgi->lgi_attr.la_size > 0));
446
447                 if (!test_bit_le(idx, LLOG_HDR_BITMAP(llh))) {
448                         CERROR("%s: modify unset record %u\n",
449                                o->do_lu.lo_dev->ld_obd->obd_name, idx);
450                         RETURN(-ENOENT);
451                 }
452
453                 if (idx != rec->lrh_index) {
454                         CERROR("%s: modify index mismatch %d %u\n",
455                                o->do_lu.lo_dev->ld_obd->obd_name, idx,
456                                rec->lrh_index);
457                         RETURN(-EFAULT);
458                 }
459
460                 if (idx == LLOG_HEADER_IDX) {
461                         /* llog header update */
462                         __u32   *bitmap = LLOG_HDR_BITMAP(llh);
463
464                         lgi->lgi_off = 0;
465
466                         /* If it does not indicate the bitmap index
467                          * (reccookie == NULL), then it means update
468                          * the whole update header. Otherwise only
469                          * update header and bits needs to be updated,
470                          * and in DNE cases, it will signaficantly
471                          * shrink the RPC size.
472                          * see distribute_txn_cancel_records()*/
473                         if (reccookie == NULL) {
474                                 lgi->lgi_buf.lb_len = reclen;
475                                 lgi->lgi_buf.lb_buf = rec;
476                                 rc = dt_record_write(env, o, &lgi->lgi_buf,
477                                                      &lgi->lgi_off, th);
478                                 RETURN(rc);
479                         }
480
481                         /* update the header */
482                         lgi->lgi_buf.lb_len = llh->llh_bitmap_offset;
483                         lgi->lgi_buf.lb_buf = llh;
484                         rc = dt_record_write(env, o, &lgi->lgi_buf,
485                                              &lgi->lgi_off, th);
486                         if (rc != 0)
487                                 RETURN(rc);
488
489                         /* update the bitmap */
490                         index = reccookie->lgc_index;
491                         lgi->lgi_off = llh->llh_bitmap_offset +
492                                       (index / (sizeof(*bitmap) * 8)) *
493                                                         sizeof(*bitmap);
494                         lgi->lgi_buf.lb_len = sizeof(*bitmap);
495                         lgi->lgi_buf.lb_buf =
496                                         &bitmap[index/(sizeof(*bitmap)*8)];
497                         rc = dt_record_write(env, o, &lgi->lgi_buf,
498                                              &lgi->lgi_off, th);
499
500                         RETURN(rc);
501                 } else if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
502                         lgi->lgi_off = llh->llh_hdr.lrh_len +
503                                        (idx - 1) * reclen;
504                 } else if (reccookie != NULL && reccookie->lgc_index > 0) {
505                         /**
506                          * The lgc_offset can be used only if index is
507                          * the same.
508                          */
509                         if (idx != reccookie->lgc_index) {
510                                 CERROR("%s: modify index mismatch %d %d\n",
511                                        o->do_lu.lo_dev->ld_obd->obd_name, idx,
512                                        reccookie->lgc_index);
513                                 RETURN(-EFAULT);
514                         }
515
516                         lgi->lgi_off = reccookie->lgc_offset;
517                         CDEBUG(D_OTHER, "modify record "DFID": idx:%u, "
518                                "len:%u offset %llu\n",
519                                PLOGID(&loghandle->lgh_id), idx,
520                                rec->lrh_len, (long long)lgi->lgi_off);
521                 } else {
522                         /* This can be result of lgh_cur_idx is not set during
523                          * llog processing or llh_size is not set to proper
524                          * record size for fixed records llog. Therefore it is
525                          * impossible to get record offset. */
526                         CERROR("%s: can't get record offset, idx:%d, "
527                                "len:%u.\n", o->do_lu.lo_dev->ld_obd->obd_name,
528                                idx, rec->lrh_len);
529                         RETURN(-EFAULT);
530                 }
531
532                 /* update only data, header and tail remain the same */
533                 lgi->lgi_off += sizeof(struct llog_rec_hdr);
534                 lgi->lgi_buf.lb_len = REC_DATA_LEN(rec);
535                 lgi->lgi_buf.lb_buf = REC_DATA(rec);
536                 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
537                 if (rc == 0 && reccookie) {
538                         reccookie->lgc_lgl = loghandle->lgh_id;
539                         reccookie->lgc_index = idx;
540                         rc = 1;
541                 }
542                 RETURN(rc);
543         }
544
545         /**
546          * The append case.
547          * The most common case of using llog. The new index is assigned to
548          * the new record, new bit is set in llog bitmap and llog count is
549          * incremented.
550          *
551          * Make sure that records don't cross a chunk boundary, so we can
552          * process them page-at-a-time if needed.  If it will cross a chunk
553          * boundary, write in a fake (but referenced) entry to pad the chunk.
554          */
555
556
557         /* simulate ENOSPC when new plain llog is being added to the
558          * catalog */
559         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2) &&
560             llh->llh_flags & LLOG_F_IS_CAT)
561                 RETURN(-ENOSPC);
562
563         LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
564         orig_last_idx = loghandle->lgh_last_idx;
565         lgi->lgi_off = lgi->lgi_attr.la_size;
566
567         if (loghandle->lgh_max_size > 0 &&
568             lgi->lgi_off >= loghandle->lgh_max_size) {
569                 CDEBUG(D_OTHER, "llog is getting too large (%u > %u) at %u "
570                        DFID"\n", (unsigned)lgi->lgi_off,
571                        loghandle->lgh_max_size, (int)loghandle->lgh_last_idx,
572                        PLOGID(&loghandle->lgh_id));
573                 /* this is to signal that this llog is full */
574                 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) - 1;
575                 RETURN(-ENOSPC);
576         }
577
578         left = chunk_size - (lgi->lgi_off & (chunk_size - 1));
579         /* NOTE: padding is a record, but no bit is set */
580         if (left != 0 && left != reclen &&
581             left < (reclen + LLOG_MIN_REC_SIZE)) {
582                 index = loghandle->lgh_last_idx + 1;
583                 rc = llog_osd_pad(env, o, &lgi->lgi_off, left, index, th);
584                 if (rc)
585                         RETURN(rc);
586
587                 loghandle->lgh_last_idx++; /* for pad rec */
588                 pad = true;
589         }
590         /* if it's the last idx in log file, then return -ENOSPC
591          * or wrap around if a catalog */
592         if (llog_is_full(loghandle) ||
593             unlikely(llh->llh_flags & LLOG_F_IS_CAT &&
594                      OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS) &&
595                      loghandle->lgh_last_idx >= cfs_fail_val)) {
596                 if (llh->llh_flags & LLOG_F_IS_CAT)
597                         loghandle->lgh_last_idx = 0;
598                 else
599                         RETURN(-ENOSPC);
600         }
601
602         down_write(&loghandle->lgh_last_sem);
603         /* increment the last_idx along with llh_tail index, they should
604          * be equal for a llog lifetime */
605         if (OBD_FAIL_CHECK(OBD_FAIL_LLOG_ADD_GAP) && --cfs_fail_val == 0)
606                 loghandle->lgh_last_idx++;
607         loghandle->lgh_last_idx++;
608         index = loghandle->lgh_last_idx;
609         LLOG_HDR_TAIL(llh)->lrt_index = index;
610         /**
611          * NB: the caller should make sure only 1 process access
612          * the lgh_last_idx, e.g. append should be exclusive.
613          * Otherwise it might hit the assert.
614          */
615         LASSERT(index < LLOG_HDR_BITMAP_SIZE(llh));
616         rec->lrh_index = index;
617         lrt = rec_tail(rec);
618         lrt->lrt_len = rec->lrh_len;
619         lrt->lrt_index = rec->lrh_index;
620
621         /* the lgh_hdr_mutex protects llog header data from concurrent
622          * update/cancel, the llh_count and llh_bitmap are protected */
623         mutex_lock(&loghandle->lgh_hdr_mutex);
624         if (__test_and_set_bit_le(index, LLOG_HDR_BITMAP(llh))) {
625                 CERROR("%s: index %u already set in llog bitmap "DFID"\n",
626                        o->do_lu.lo_dev->ld_obd->obd_name, index,
627                        PFID(lu_object_fid(&o->do_lu)));
628                 mutex_unlock(&loghandle->lgh_hdr_mutex);
629                 LBUG(); /* should never happen */
630         }
631         llh->llh_count++;
632
633         if (!(llh->llh_flags & LLOG_F_IS_FIXSIZE)) {
634                 /* Update the minimum size of the llog record */
635                 if (llh->llh_size == 0)
636                         llh->llh_size = reclen;
637                 else if (reclen < llh->llh_size)
638                         llh->llh_size = reclen;
639         }
640
641         /*
642          * readers (e.g. llog_osd_read_header()) must not find
643          * llog updated partially (bitmap/counter claims record,
644          * but a record hasn't been added yet) as this results
645          * in EIO.
646          */
647         dt_write_lock(env, o, 0);
648
649         if (lgi->lgi_attr.la_size == 0) {
650                 lgi->lgi_off = 0;
651                 lgi->lgi_buf.lb_len = llh->llh_hdr.lrh_len;
652                 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
653                 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
654                 if (rc != 0)
655                         GOTO(out_unlock, rc);
656         } else {
657                 __u32   *bitmap = LLOG_HDR_BITMAP(llh);
658
659                 /* Note: If this is not initialization (size == 0), then do not
660                  * write the whole header (8k bytes), only update header/tail
661                  * and bits needs to be updated. Because this update might be
662                  * part of cross-MDT operation, which needs to write these
663                  * updates into the update log(32KB limit) and also pack inside
664                  * the RPC (1MB limit), if we write 8K for each operation, which
665                  * will cost a lot space, and keep us adding more updates to one
666                  * update log.*/
667                 lgi->lgi_off = 0;
668                 lgi->lgi_buf.lb_len = llh->llh_bitmap_offset;
669                 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
670                 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
671                 if (rc != 0)
672                         GOTO(out_unlock, rc);
673
674                 lgi->lgi_off = llh->llh_bitmap_offset +
675                               (index / (sizeof(*bitmap) * 8)) * sizeof(*bitmap);
676                 lgi->lgi_buf.lb_len = sizeof(*bitmap);
677                 lgi->lgi_buf.lb_buf = &bitmap[index/(sizeof(*bitmap)*8)];
678                 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
679                 if (rc != 0)
680                         GOTO(out_unlock, rc);
681
682                 lgi->lgi_off =  (unsigned long)LLOG_HDR_TAIL(llh) -
683                                 (unsigned long)llh;
684                 lgi->lgi_buf.lb_len = sizeof(llh->llh_tail);
685                 lgi->lgi_buf.lb_buf = LLOG_HDR_TAIL(llh);
686                 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
687                 if (rc != 0)
688                         GOTO(out_unlock, rc);
689         }
690         if (OBD_FAIL_PRECHECK(OBD_FAIL_LLOG_PAUSE_AFTER_PAD) && pad) {
691                 /* a window for concurrent llog reader, see LU-12577 */
692                 OBD_FAIL_TIMEOUT(OBD_FAIL_LLOG_PAUSE_AFTER_PAD,
693                                  cfs_fail_val ?: 1);
694         }
695
696 out_unlock:
697         /* unlock here for remote object */
698         mutex_unlock(&loghandle->lgh_hdr_mutex);
699         if (rc) {
700                 dt_write_unlock(env, o);
701                 GOTO(out, rc);
702         }
703
704         if (OBD_FAIL_PRECHECK(OBD_FAIL_LLOG_PROCESS_TIMEOUT) &&
705            cfs_fail_val == (unsigned int)(loghandle->lgh_id.lgl_oi.oi.oi_id &
706                                           0xFFFFFFFF)) {
707                 OBD_RACE(OBD_FAIL_LLOG_PROCESS_TIMEOUT);
708                 msleep(1 * MSEC_PER_SEC);
709         }
710         /* computed index can be used to determine offset for fixed-size
711          * records. This also allows to handle Catalog wrap around case */
712         if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
713                 lgi->lgi_off = llh->llh_hdr.lrh_len + (index - 1) * reclen;
714         } else {
715                 rc = dt_attr_get(env, o, &lgi->lgi_attr);
716                 if (rc) {
717                         dt_write_unlock(env, o);
718                         GOTO(out, rc);
719                 }
720
721                 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
722                 lgi->lgi_off = max_t(__u64, lgi->lgi_attr.la_size,
723                                      lgi->lgi_off);
724         }
725
726         lgi->lgi_buf.lb_len = reclen;
727         lgi->lgi_buf.lb_buf = rec;
728         rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
729
730         dt_write_unlock(env, o);
731         if (rc < 0)
732                 GOTO(out, rc);
733
734         up_write(&loghandle->lgh_last_sem);
735
736         CDEBUG(D_HA, "added record "DFID".%u, %u off%llu\n",
737                PFID(lu_object_fid(&o->do_lu)), index, rec->lrh_len,
738                lgi->lgi_off);
739         if (reccookie != NULL) {
740                 reccookie->lgc_lgl = loghandle->lgh_id;
741                 reccookie->lgc_index = index;
742                 if ((rec->lrh_type == MDS_UNLINK_REC) ||
743                     (rec->lrh_type == MDS_SETATTR64_REC))
744                         reccookie->lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
745                 else if (rec->lrh_type == OST_SZ_REC)
746                         reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
747                 else
748                         reccookie->lgc_subsys = -1;
749                 rc = 1;
750         }
751         RETURN(rc);
752 out:
753         /* cleanup llog for error case */
754         mutex_lock(&loghandle->lgh_hdr_mutex);
755         clear_bit_le(index, LLOG_HDR_BITMAP(llh));
756         llh->llh_count--;
757         mutex_unlock(&loghandle->lgh_hdr_mutex);
758
759         /* restore llog last_idx */
760         if (dt_object_remote(o)) {
761                 loghandle->lgh_last_idx = orig_last_idx;
762         } else if (--loghandle->lgh_last_idx == 0 &&
763             (llh->llh_flags & LLOG_F_IS_CAT) && llh->llh_cat_idx != 0) {
764                 /* catalog had just wrap-around case */
765                 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) - 1;
766         }
767
768         LLOG_HDR_TAIL(llh)->lrt_index = loghandle->lgh_last_idx;
769         up_write(&loghandle->lgh_last_sem);
770
771         RETURN(rc);
772 }
773
774 /**
775  * We can skip reading at least as many log blocks as the number of
776  * minimum sized log records we are skipping.  If it turns out
777  * that we are not far enough along the log (because the
778  * actual records are larger than minimum size) we just skip
779  * some more records.
780  *
781  * Note: in llog_process_thread, it will use bitmap offset as
782  * the index to locate the record, which also includs some pad
783  * records, whose record size is very small, and it also does not
784  * consider pad record when recording minimum record size (otherwise
785  * min_record size might be too small), so in some rare cases,
786  * it might skip too much record for @goal, see llog_osd_next_block().
787  *
788  * When force_mini_rec is true, it means we have to use LLOG_MIN_REC_SIZE
789  * as the min record size to skip over, usually because in the previous
790  * try, it skip too much record, see loog_osd_next(prev)_block().
791  */
792 static inline void llog_skip_over(struct llog_handle *lgh, __u64 *off,
793                                   int curr, int goal, __u32 chunk_size,
794                                   bool force_mini_rec)
795 {
796         struct llog_log_hdr *llh = lgh->lgh_hdr;
797
798         /* Goal should not bigger than the record count */
799         if (goal > lgh->lgh_last_idx)
800                 goal = lgh->lgh_last_idx;
801
802         if (goal > curr) {
803                 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
804                         *off = chunk_size + (goal - 1) * llh->llh_size;
805                 } else {
806                         __u64 min_rec_size = LLOG_MIN_REC_SIZE;
807
808                         if (llh->llh_size > 0 && !force_mini_rec)
809                                 min_rec_size = llh->llh_size;
810
811                         *off = *off + (goal - curr - 1) * min_rec_size;
812                 }
813         }
814         /* always align with lower chunk boundary*/
815         *off &= ~(chunk_size - 1);
816 }
817
818 /**
819  * Remove optional fields that the client doesn't expect.
820  * This is typically in order to ensure compatibility with older clients.
821  * It is assumed that since we exclusively remove fields, the block will be
822  * big enough to handle the remapped records. It is also assumed that records
823  * of a block have the same format (i.e.: the same features enabled).
824  *
825  * \param[in,out]    hdr           Header of the block of records to remap.
826  * \param[in,out]    last_hdr      Last header, don't read past this point.
827  * \param[in]        flags         Flags describing the fields to keep.
828  * \param[in]        extra_flags   Flags describing the extra fields to keep.
829  */
830 static void changelog_block_trim_ext(struct llog_rec_hdr *hdr,
831                                      struct llog_rec_hdr *last_hdr,
832                                      struct llog_handle *loghandle)
833 {
834         enum changelog_rec_flags flags = CLF_SUPPORTED;
835         enum changelog_rec_extra_flags extra_flags = CLFE_SUPPORTED;
836
837         if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_XATTR))
838                 extra_flags &= ~CLFE_XATTR;
839         if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_OMODE))
840                 extra_flags &= ~CLFE_OPEN;
841         if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_NID))
842                 extra_flags &= ~CLFE_NID;
843         if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_UIDGID))
844                 extra_flags &= ~CLFE_UIDGID;
845         if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_EXTRA_FLAGS))
846                 flags &= ~CLF_EXTRA_FLAGS;
847         if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_JOBID))
848                 flags &= ~CLF_JOBID;
849
850         if (flags == CLF_SUPPORTED && extra_flags == CLFE_SUPPORTED)
851                 return;
852
853         if (hdr->lrh_type != CHANGELOG_REC)
854                 return;
855
856         do {
857                 struct changelog_rec *rec = (struct changelog_rec *)(hdr + 1);
858                 enum changelog_rec_extra_flags xflag = CLFE_INVALID;
859
860                 if (flags & CLF_EXTRA_FLAGS &&
861                     rec->cr_flags & CLF_EXTRA_FLAGS) {
862                         xflag = changelog_rec_extra_flags(rec)->cr_extra_flags &
863                                 extra_flags;
864                 }
865
866                 if (unlikely(hdr->lrh_len == 0)) {
867                         /* It is corruption case, we cannot know the next rec,
868                          * jump to the last one directly to avoid dead loop. */
869                         LCONSOLE(D_WARNING, "Hit invalid llog record: "
870                                  "idx %u, type %u, id %u\n",
871                                  hdr->lrh_index, hdr->lrh_type, hdr->lrh_id);
872                         hdr = llog_rec_hdr_next(last_hdr);
873                         if (unlikely(hdr == last_hdr))
874                                 LCONSOLE(D_WARNING, "The last record crashed: "
875                                          "idx %u, type %u, id %u\n",
876                                          hdr->lrh_index, hdr->lrh_type,
877                                          hdr->lrh_id);
878                         break;
879                 }
880
881                 changelog_remap_rec(rec, rec->cr_flags & flags, xflag);
882                 hdr = llog_rec_hdr_next(hdr);
883                 /* Yield CPU to avoid soft-lockup if there are too many records
884                  * to be handled. */
885                 cond_resched();
886         } while ((char *)hdr <= (char *)last_hdr);
887 }
888
889 /**
890  * Implementation of the llog_operations::lop_next_block
891  *
892  * This function finds the the next llog block to return which contains
893  * record with required index. It is main part of llog processing.
894  *
895  * \param[in]     env           execution environment
896  * \param[in]     loghandle     llog handle of the current llog
897  * \param[in,out] cur_idx       index preceeding cur_offset
898  * \param[in]     next_idx      target index to find
899  * \param[in,out] cur_offset    furtherst point read in the file
900  * \param[in]     buf           pointer to data buffer to fill
901  * \param[in]     len           required len to read, it is
902  *                              usually llog chunk_size.
903  *
904  * \retval                      0 on successful buffer read
905  * \retval                      negative value on error
906  */
907 static int llog_osd_next_block(const struct lu_env *env,
908                                struct llog_handle *loghandle, int *cur_idx,
909                                int next_idx, __u64 *cur_offset, void *buf,
910                                int len)
911 {
912         struct llog_thread_info *lgi = llog_info(env);
913         struct dt_object        *o;
914         struct dt_device        *dt;
915         int                      rc;
916         __u32                   chunk_size;
917         int last_idx = *cur_idx;
918         __u64 last_offset = *cur_offset;
919         bool force_mini_rec = !next_idx;
920
921         ENTRY;
922
923         LASSERT(env);
924         LASSERT(lgi);
925
926         chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
927         if (len == 0 || len & (chunk_size - 1))
928                 RETURN(-EINVAL);
929
930         LASSERT(loghandle);
931         LASSERT(loghandle->lgh_ctxt);
932
933         if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_CHANGELOG_DEL) &&
934             cfs_fail_val == ((unsigned long)loghandle & 0xFFFFFFFF)) {
935                 OBD_RACE(OBD_FAIL_MDS_CHANGELOG_DEL);
936                 msleep(MSEC_PER_SEC >> 2);
937         }
938
939         o = loghandle->lgh_obj;
940         LASSERT(o);
941         dt_read_lock(env, o, 0);
942         if (!llog_osd_exist(loghandle))
943                 GOTO(out, rc = -ESTALE); //object was destroyed
944
945         dt = lu2dt_dev(o->do_lu.lo_dev);
946         LASSERT(dt);
947
948         rc = dt_attr_get(env, o, &lgi->lgi_attr);
949         if (rc)
950                 GOTO(out, rc);
951
952         CDEBUG(D_OTHER,
953                "looking for log index %u (cur idx %u off %llu), size %llu\n",
954                next_idx, *cur_idx,
955                *cur_offset, lgi->lgi_attr.la_size);
956
957         while (*cur_offset < lgi->lgi_attr.la_size) {
958                 struct llog_rec_hdr     *rec, *last_rec;
959                 struct llog_rec_tail    *tail;
960
961                 llog_skip_over(loghandle, cur_offset, *cur_idx,
962                                next_idx, chunk_size, force_mini_rec);
963
964                 /* read up to next llog chunk_size block */
965                 lgi->lgi_buf.lb_len = chunk_size -
966                                       (*cur_offset & (chunk_size - 1));
967                 lgi->lgi_buf.lb_buf = buf;
968
969                 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
970                 if (rc < 0) {
971                         if (rc == -EBADR) {
972                                 /* no goal is valid case */
973                                 if (!next_idx)
974                                         GOTO(out, rc);
975                                 if (!force_mini_rec)
976                                         goto retry;
977                         }
978                         CERROR("%s: can't read llog block from log "DFID
979                                " offset %llu: rc = %d\n",
980                                o->do_lu.lo_dev->ld_obd->obd_name,
981                                PFID(lu_object_fid(&o->do_lu)), *cur_offset,
982                                rc);
983                         GOTO(out, rc);
984                 }
985
986                 if (rc < len) {
987                         /* signal the end of the valid buffer to
988                          * llog_process */
989                         memset(buf + rc, 0, len - rc);
990                 }
991
992                 if (rc == 0) { /* end of file, nothing to do */
993                         if (!force_mini_rec)
994                                 goto retry;
995                         GOTO(out, rc);
996                 }
997
998                 if (rc < sizeof(*tail)) {
999                         if (!force_mini_rec)
1000                                 goto retry;
1001
1002                         CERROR("%s: invalid llog block at log id "DFID" offset %llu\n",
1003                                o->do_lu.lo_dev->ld_obd->obd_name,
1004                                PLOGID(&loghandle->lgh_id), *cur_offset);
1005                         GOTO(out, rc = -EINVAL);
1006                 }
1007
1008                 rec = buf;
1009                 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
1010                         lustre_swab_llog_rec(rec);
1011                 tail = (struct llog_rec_tail *)((char *)buf + rc -
1012                                                 sizeof(struct llog_rec_tail));
1013
1014                 /* caller handles bad records if any */
1015                 if (llog_verify_record(loghandle, rec))
1016                         GOTO(out, rc = 0);
1017
1018                 /* get the last record in block */
1019                 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
1020                                                    tail->lrt_len);
1021
1022                 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
1023                         lustre_swab_llog_rec(last_rec);
1024
1025                 if (last_rec->lrh_index != tail->lrt_index) {
1026                         CERROR("%s: invalid llog tail at log id "DFID" offset %llu last_rec idx %u tail idx %u lrt len %u read_size %d\n",
1027                                o->do_lu.lo_dev->ld_obd->obd_name,
1028                                PLOGID(&loghandle->lgh_id), *cur_offset,
1029                                last_rec->lrh_index, tail->lrt_index,
1030                                tail->lrt_len, rc);
1031                         GOTO(out, rc = -EINVAL);
1032                 }
1033
1034                 *cur_idx = tail->lrt_index;
1035
1036                 /* this shouldn't happen */
1037                 if (tail->lrt_index == 0) {
1038                         CERROR("%s: invalid llog tail at log id "DFID"offset %llu bytes %d\n",
1039                                o->do_lu.lo_dev->ld_obd->obd_name,
1040                                PLOGID(&loghandle->lgh_id), *cur_offset, rc);
1041                         GOTO(out, rc = -EINVAL);
1042                 }
1043                 if (tail->lrt_index < next_idx) {
1044                         last_idx = *cur_idx;
1045                         last_offset = *cur_offset;
1046                         continue;
1047                 }
1048
1049                 /* sanity check that the start of the new buffer is no farther
1050                  * than the record that we wanted.  This shouldn't happen. */
1051                 if (next_idx && rec->lrh_index > next_idx) {
1052                         if (!force_mini_rec && next_idx > last_idx)
1053                                 goto retry;
1054
1055                         CERROR("%s: missed desired record? %u > %u\n",
1056                                o->do_lu.lo_dev->ld_obd->obd_name,
1057                                rec->lrh_index, next_idx);
1058                         GOTO(out, rc = -ENOENT);
1059                 }
1060
1061                 /* Trim unsupported extensions for compat w/ older clients */
1062                 changelog_block_trim_ext(rec, last_rec, loghandle);
1063
1064                 GOTO(out, rc = 0);
1065
1066 retry:
1067                 /* Note: because there are some pad records in the
1068                  * llog, so llog_skip_over() might skip too much
1069                  * records, let's try skip again with minimum record */
1070                 force_mini_rec = true;
1071                 *cur_offset = last_offset;
1072                 *cur_idx = last_idx;
1073         }
1074         /* being here means we reach end of llog but didn't find needed idx
1075          * normally could happen while processing remote llog, return -EBADR
1076          * to indicate access beyond end of file like dt_read() does and to
1077          * distunguish this situation from real IO or network issues.
1078          */
1079         GOTO(out, rc = -EBADR);
1080 out:
1081         dt_read_unlock(env, o);
1082         return rc;
1083 }
1084
1085 /**
1086  * Implementation of the llog_operations::lop_prev_block
1087  *
1088  * This function finds the llog block to return which contains
1089  * record with required index but in reverse order - from end of llog
1090  * to the beginning.
1091  * It is main part of reverse llog processing.
1092  *
1093  * \param[in] env       execution environment
1094  * \param[in] loghandle llog handle of the current llog
1095  * \param[in] prev_idx  target index to find
1096  * \param[in] buf       pointer to data buffer to fill
1097  * \param[in] len       required len to read, it is llog_chunk_size usually.
1098  *
1099  * \retval              0 on successful buffer read
1100  * \retval              negative value on error
1101  */
1102 static int llog_osd_prev_block(const struct lu_env *env,
1103                                struct llog_handle *loghandle,
1104                                int prev_idx, void *buf, int len)
1105 {
1106         struct llog_thread_info *lgi = llog_info(env);
1107         struct dt_object        *o;
1108         struct dt_device        *dt;
1109         loff_t                   cur_offset;
1110         __u32                   chunk_size;
1111         int                      rc;
1112
1113         ENTRY;
1114
1115         chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
1116         if (len == 0 || len & (chunk_size - 1))
1117                 RETURN(-EINVAL);
1118
1119         CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
1120
1121         LASSERT(loghandle);
1122         LASSERT(loghandle->lgh_ctxt);
1123
1124         o = loghandle->lgh_obj;
1125         LASSERT(o);
1126         dt_read_lock(env, o, 0);
1127         if (!llog_osd_exist(loghandle))
1128                 GOTO(out, rc = -ESTALE);
1129
1130         dt = lu2dt_dev(o->do_lu.lo_dev);
1131         LASSERT(dt);
1132
1133         /* Let's only use mini record size for previous block read
1134          * for now XXX */
1135         cur_offset = chunk_size;
1136         llog_skip_over(loghandle, &cur_offset, 0, prev_idx,
1137                        chunk_size, true);
1138
1139         rc = dt_attr_get(env, o, &lgi->lgi_attr);
1140         if (rc)
1141                 GOTO(out, rc);
1142
1143         while (cur_offset < lgi->lgi_attr.la_size) {
1144                 struct llog_rec_hdr     *rec, *last_rec;
1145                 struct llog_rec_tail    *tail;
1146
1147                 lgi->lgi_buf.lb_len = len;
1148                 lgi->lgi_buf.lb_buf = buf;
1149                 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
1150                 if (rc < 0) {
1151                         CERROR("%s: can't read llog block from log "DFID
1152                                " offset %llu: rc = %d\n",
1153                                o->do_lu.lo_dev->ld_obd->obd_name,
1154                                PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
1155                         GOTO(out, rc);
1156                 }
1157
1158                 if (rc == 0) /* end of file, nothing to do */
1159                         GOTO(out, rc);
1160
1161                 if (rc < sizeof(*tail)) {
1162                         CERROR("%s: invalid llog block at log id "DFID" offset %llu\n",
1163                                o->do_lu.lo_dev->ld_obd->obd_name,
1164                                PLOGID(&loghandle->lgh_id), cur_offset);
1165                         GOTO(out, rc = -EINVAL);
1166                 }
1167
1168                 rec = buf;
1169                 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
1170                         lustre_swab_llog_rec(rec);
1171
1172                 tail = (struct llog_rec_tail *)((char *)buf + rc -
1173                                                 sizeof(struct llog_rec_tail));
1174                 /* get the last record in block */
1175                 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
1176                                                    le32_to_cpu(tail->lrt_len));
1177
1178                 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
1179                         lustre_swab_llog_rec(last_rec);
1180                 LASSERT(last_rec->lrh_index == tail->lrt_index);
1181
1182                 /* this shouldn't happen */
1183                 if (tail->lrt_index == 0) {
1184                         CERROR("%s: invalid llog tail at log id "DFID" offset %llu\n",
1185                                o->do_lu.lo_dev->ld_obd->obd_name,
1186                                PLOGID(&loghandle->lgh_id), cur_offset);
1187                         GOTO(out, rc = -EINVAL);
1188                 }
1189                 if (tail->lrt_index < prev_idx)
1190                         continue;
1191
1192                 /* sanity check that the start of the new buffer is no farther
1193                  * than the record that we wanted.  This shouldn't happen. */
1194                 if (rec->lrh_index > prev_idx) {
1195                         CERROR("%s: missed desired record? %u > %u\n",
1196                                o->do_lu.lo_dev->ld_obd->obd_name,
1197                                rec->lrh_index, prev_idx);
1198                         GOTO(out, rc = -ENOENT);
1199                 }
1200
1201                 /* Trim unsupported extensions for compat w/ older clients */
1202                 changelog_block_trim_ext(rec, last_rec, loghandle);
1203
1204                 GOTO(out, rc = 0);
1205         }
1206         GOTO(out, rc = -EIO);
1207 out:
1208         dt_read_unlock(env, o);
1209         return rc;
1210 }
1211
1212 /**
1213  * This is helper function to get llog directory object. It is used by named
1214  * llog operations to find/insert/delete llog entry from llog directory.
1215  *
1216  * \param[in] env       execution environment
1217  * \param[in] ctxt      llog context
1218  *
1219  * \retval              dt_object of llog directory
1220  * \retval              ERR_PTR of negative value on error
1221  */
1222 static struct dt_object *llog_osd_dir_get(const struct lu_env *env,
1223                                           struct llog_ctxt *ctxt)
1224 {
1225         struct dt_device        *dt;
1226         struct dt_thread_info   *dti = dt_info(env);
1227         struct dt_object        *dir;
1228         int                      rc;
1229
1230         dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1231         if (ctxt->loc_dir == NULL) {
1232                 rc = dt_root_get(env, dt, &dti->dti_fid);
1233                 if (rc)
1234                         return ERR_PTR(rc);
1235                 dir = dt_locate(env, dt, &dti->dti_fid);
1236
1237                 if (!IS_ERR(dir) && !dt_try_as_dir(env, dir, true)) {
1238                         dt_object_put(env, dir);
1239                         return ERR_PTR(-ENOTDIR);
1240                 }
1241         } else {
1242                 lu_object_get(&ctxt->loc_dir->do_lu);
1243                 dir = ctxt->loc_dir;
1244         }
1245
1246         return dir;
1247 }
1248
1249 /**
1250  * Implementation of the llog_operations::lop_open
1251  *
1252  * This function opens the llog by its logid or by name, it may open also
1253  * non existent llog and assing then new id to it.
1254  * The llog_open/llog_close pair works similar to lu_object_find/put,
1255  * the object may not exist prior open. The result of open is just dt_object
1256  * in the llog header.
1257  *
1258  * \param[in] env               execution environment
1259  * \param[in] handle            llog handle of the current llog
1260  * \param[in] logid             logid of llog to open (nameless llog)
1261  * \param[in] name              name of llog to open (named llog)
1262  * \param[in] open_param
1263  *                              LLOG_OPEN_NEW - new llog, may not exist
1264  *                              LLOG_OPEN_EXIST - old llog, must exist
1265  *
1266  * \retval                      0 on successful open, llog_handle::lgh_obj
1267  *                              contains the dt_object of the llog.
1268  * \retval                      negative value on error
1269  */
1270 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
1271                          struct llog_logid *logid, char *name,
1272                          enum llog_open_param open_param)
1273 {
1274         struct llog_thread_info         *lgi = llog_info(env);
1275         struct llog_ctxt                *ctxt = handle->lgh_ctxt;
1276         struct dt_object                *o;
1277         struct dt_device                *dt;
1278         struct ls_device                *ls;
1279         struct local_oid_storage        *los = NULL;
1280         int                              rc = 0;
1281         bool new_id = false;
1282
1283         ENTRY;
1284
1285         LASSERT(env);
1286         LASSERT(ctxt);
1287         LASSERT(ctxt->loc_exp);
1288         LASSERT(ctxt->loc_exp->exp_obd);
1289         dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1290         LASSERT(dt);
1291         if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1292                 struct lu_object_conf conf = { 0 };
1293                 if (logid != NULL) {
1294                         logid_to_fid(logid, &lgi->lgi_fid);
1295                 } else {
1296                         /* If logid == NULL, then it means the caller needs
1297                          * to allocate new FID (llog_cat_declare_add_rec()). */
1298                         rc = dt_fid_alloc(env, dt, &lgi->lgi_fid, NULL, NULL);
1299                         if (rc < 0)
1300                                 RETURN(rc);
1301                         rc = 0;
1302                         conf.loc_flags = LOC_F_NEW;
1303                 }
1304
1305                 o = dt_locate_at(env, dt, &lgi->lgi_fid,
1306                                  dt->dd_lu_dev.ld_site->ls_top_dev, &conf);
1307                 if (IS_ERR(o))
1308                         RETURN(PTR_ERR(o));
1309
1310                 goto after_open;
1311         }
1312
1313         ls = ls_device_get(dt);
1314         if (IS_ERR(ls))
1315                 RETURN(PTR_ERR(ls));
1316
1317         mutex_lock(&ls->ls_los_mutex);
1318         los = dt_los_find(ls, name != NULL ? FID_SEQ_LLOG_NAME : FID_SEQ_LLOG);
1319         mutex_unlock(&ls->ls_los_mutex);
1320         LASSERT(los);
1321         ls_device_put(env, ls);
1322
1323         LASSERT(handle);
1324
1325         if (logid != NULL) {
1326                 logid_to_fid(logid, &lgi->lgi_fid);
1327         } else if (name) {
1328                 struct dt_object *llog_dir;
1329
1330                 llog_dir = llog_osd_dir_get(env, ctxt);
1331                 if (IS_ERR(llog_dir))
1332                         GOTO(out, rc = PTR_ERR(llog_dir));
1333                 dt_read_lock(env, llog_dir, 0);
1334                 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
1335                 dt_read_unlock(env, llog_dir);
1336                 dt_object_put(env, llog_dir);
1337                 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
1338                         /* generate fid for new llog */
1339                         rc = local_object_fid_generate(env, los,
1340                                                        &lgi->lgi_fid);
1341                         new_id = true;
1342                 }
1343                 if (rc < 0)
1344                         GOTO(out, rc);
1345                 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
1346                 if (handle->lgh_name)
1347                         strcpy(handle->lgh_name, name);
1348                 else
1349                         GOTO(out, rc = -ENOMEM);
1350         } else {
1351                 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
1352                 /* generate fid for new llog */
1353 generate:
1354                 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
1355                 if (rc < 0)
1356                         GOTO(out, rc);
1357                 new_id = true;
1358         }
1359         if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_LLOG_UMOUNT_RACE) &&
1360             cfs_fail_val == 1) {
1361                 cfs_fail_val = 2;
1362                 OBD_RACE(OBD_FAIL_MDS_LLOG_UMOUNT_RACE);
1363                 msleep(MSEC_PER_SEC);
1364         }
1365         o = ls_locate(env, ls, &lgi->lgi_fid, NULL);
1366         if (IS_ERR(o))
1367                 GOTO(out_name, rc = PTR_ERR(o));
1368
1369         if (dt_object_exists(o) && new_id) {
1370                 /* llog exists with just generated ID, e.g. some old llog file
1371                  * still is in use or is orphan, drop a warn and skip it. */
1372                 CDEBUG(D_INFO, "%s: llog exists with the same FID: "DFID
1373                        ", skipping\n",
1374                        o->do_lu.lo_dev->ld_obd->obd_name,
1375                        PFID(lu_object_fid(&o->do_lu)));
1376                 dt_object_put(env, o);
1377                 /* just skip this llog ID, we shouldn't delete it because we
1378                  * don't know exactly what is its purpose and state. */
1379                 goto generate;
1380         }
1381
1382 after_open:
1383         /* No new llog is expected but doesn't exist */
1384         if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o)) {
1385                 CDEBUG(D_INFO, "%s: llog FID: "DFID" obj %p doesn`t exist\n",
1386                        o->do_lu.lo_dev->ld_obd->obd_name,
1387                        PFID(lu_object_fid(&o->do_lu)), o);
1388                 GOTO(out_put, rc = -ENOENT);
1389         }
1390         fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
1391         handle->lgh_obj = o;
1392         handle->private_data = los;
1393         LASSERT(handle->lgh_ctxt);
1394
1395         RETURN(rc);
1396
1397 out_put:
1398         dt_object_put(env, o);
1399 out_name:
1400         if (handle->lgh_name != NULL)
1401                 OBD_FREE(handle->lgh_name, strlen(name) + 1);
1402 out:
1403         if (los != NULL)
1404                 dt_los_put(los);
1405         RETURN(rc);
1406 }
1407
1408 /**
1409  * Get dir for regular fid log object
1410  *
1411  * Get directory for regular fid log object, and these regular fid log
1412  * object will be inserted under this directory, to satisfy the FS
1413  * consistency check, e2fsck etc.
1414  *
1415  * \param [in] env      execution environment
1416  * \param [in] dto      llog object
1417  *
1418  * \retval              pointer to the directory if it is found.
1419  * \retval              ERR_PTR(negative errno) if it fails.
1420  */
1421 struct dt_object *llog_osd_get_regular_fid_dir(const struct lu_env *env,
1422                                                struct dt_object *dto)
1423 {
1424         struct llog_thread_info *lgi = llog_info(env);
1425         struct seq_server_site *ss = dto->do_lu.lo_dev->ld_site->ld_seq_site;
1426         struct lu_seq_range     *range = &lgi->lgi_range;
1427         struct lu_fid           *dir_fid = &lgi->lgi_fid;
1428         struct dt_object        *dir;
1429         int                     rc;
1430         ENTRY;
1431
1432         fld_range_set_any(range);
1433         LASSERT(ss != NULL);
1434         rc = ss->ss_server_fld->lsf_seq_lookup(env, ss->ss_server_fld,
1435                                    fid_seq(lu_object_fid(&dto->do_lu)), range);
1436         if (rc < 0)
1437                 RETURN(ERR_PTR(rc));
1438
1439         lu_update_log_dir_fid(dir_fid, range->lsr_index);
1440         dir = dt_locate(env, lu2dt_dev(dto->do_lu.lo_dev), dir_fid);
1441         if (IS_ERR(dir))
1442                 RETURN(dir);
1443
1444         if (!dt_try_as_dir(env, dir, true)) {
1445                 dt_object_put(env, dir);
1446                 RETURN(ERR_PTR(-ENOTDIR));
1447         }
1448
1449         RETURN(dir);
1450 }
1451
1452 /**
1453  * Add llog object with regular FID to name entry
1454  *
1455  * Add llog object with regular FID to name space, and each llog
1456  * object on each MDT will be /update_log_dir/[seq:oid:ver],
1457  * so to satisfy the namespace consistency check, e2fsck etc.
1458  *
1459  * \param [in] env      execution environment
1460  * \param [in] dto      llog object
1461  * \param [in] th       thandle
1462  * \param [in] declare  if it is declare or execution
1463  *
1464  * \retval              0 if insertion succeeds.
1465  * \retval              negative errno if insertion fails.
1466  */
1467 static int
1468 llog_osd_regular_fid_add_name_entry(const struct lu_env *env,
1469                                     struct dt_object *dto,
1470                                     struct thandle *th, bool declare)
1471 {
1472         struct llog_thread_info *lgi = llog_info(env);
1473         const struct lu_fid     *fid = lu_object_fid(&dto->do_lu);
1474         struct dt_insert_rec    *rec = &lgi->lgi_dt_rec;
1475         struct dt_object        *dir;
1476         char                    *name = lgi->lgi_name;
1477         int                     rc;
1478         ENTRY;
1479
1480         if (!fid_is_norm(fid))
1481                 RETURN(0);
1482
1483         dir = llog_osd_get_regular_fid_dir(env, dto);
1484         if (IS_ERR(dir))
1485                 RETURN(PTR_ERR(dir));
1486
1487         rec->rec_fid = fid;
1488         rec->rec_type = S_IFREG;
1489         snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1490         dt_write_lock(env, dir, 0);
1491         if (declare) {
1492                 rc = dt_declare_insert(env, dir, (struct dt_rec *)rec,
1493                                (struct dt_key *)name, th);
1494         } else {
1495                 rc = dt_insert(env, dir, (struct dt_rec *)rec,
1496                                (struct dt_key *)name, th);
1497         }
1498         dt_write_unlock(env, dir);
1499
1500         dt_object_put(env, dir);
1501         RETURN(rc);
1502 }
1503
1504
1505 /**
1506  * Implementation of the llog_operations::lop_declare_create
1507  *
1508  * This function declares the llog create. It declares also name insert
1509  * into llog directory in case of named llog.
1510  *
1511  * \param[in] env       execution environment
1512  * \param[in] res       llog handle of the current llog
1513  * \param[in] th        current transaction handle
1514  *
1515  * \retval              0 on successful create declaration
1516  * \retval              negative value on error
1517  */
1518 static int llog_osd_declare_create(const struct lu_env *env,
1519                                    struct llog_handle *res, struct thandle *th)
1520 {
1521         struct llog_thread_info         *lgi = llog_info(env);
1522         struct dt_insert_rec            *rec = &lgi->lgi_dt_rec;
1523         struct local_oid_storage        *los;
1524         struct dt_object                *o;
1525         int                              rc;
1526
1527         ENTRY;
1528
1529         LASSERT(res->lgh_obj);
1530         LASSERT(th);
1531
1532         /* object can be created by another thread */
1533         o = res->lgh_obj;
1534         if (dt_object_exists(o))
1535                 RETURN(0);
1536
1537         if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1538                 struct llog_thread_info *lgi = llog_info(env);
1539
1540                 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE;
1541                 lgi->lgi_attr.la_size = 0;
1542                 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1543                 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1544
1545                 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1546                                        &lgi->lgi_dof, th);
1547                 if (rc < 0)
1548                         RETURN(rc);
1549
1550
1551                 rc = llog_osd_regular_fid_add_name_entry(env, o, th, true);
1552
1553                 RETURN(rc);
1554         }
1555         los = res->private_data;
1556         LASSERT(los);
1557
1558         rc = llog_osd_declare_new_object(env, los, o, th);
1559         if (rc)
1560                 RETURN(rc);
1561
1562         /* do not declare header initialization here as it's declared
1563          * in llog_osd_declare_write_rec() which is always called */
1564
1565         if (res->lgh_name) {
1566                 struct dt_object *llog_dir;
1567
1568                 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1569                 if (IS_ERR(llog_dir))
1570                         RETURN(PTR_ERR(llog_dir));
1571                 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1572                 rec->rec_fid = &lgi->lgi_fid;
1573                 rec->rec_type = S_IFREG;
1574                 rc = dt_declare_insert(env, llog_dir,
1575                                        (struct dt_rec *)rec,
1576                                        (struct dt_key *)res->lgh_name, th);
1577                 dt_object_put(env, llog_dir);
1578                 if (rc)
1579                         CERROR("%s: can't declare named llog %s: rc = %d\n",
1580                                o->do_lu.lo_dev->ld_obd->obd_name,
1581                                res->lgh_name, rc);
1582         }
1583         RETURN(rc);
1584 }
1585
1586 /**
1587  * Implementation of the llog_operations::lop_create
1588  *
1589  * This function creates the llog according with llog_handle::lgh_obj
1590  * and llog_handle::lgh_name.
1591  *
1592  * \param[in] env       execution environment
1593  * \param[in] res       llog handle of the current llog
1594  * \param[in] th        current transaction handle
1595  *
1596  * \retval              0 on successful create
1597  * \retval              negative value on error
1598  */
1599 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
1600                            struct thandle *th)
1601 {
1602         struct llog_thread_info *lgi = llog_info(env);
1603         struct dt_insert_rec    *rec = &lgi->lgi_dt_rec;
1604         struct local_oid_storage *los;
1605         struct dt_object        *o;
1606         int                      rc = 0;
1607
1608         ENTRY;
1609
1610         LASSERT(env);
1611         o = res->lgh_obj;
1612         LASSERT(o);
1613
1614         /* llog can be already created */
1615         if (dt_object_exists(o))
1616                 RETURN(-EEXIST);
1617
1618         if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1619                 struct llog_thread_info *lgi = llog_info(env);
1620
1621                 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE | LA_TYPE;
1622                 lgi->lgi_attr.la_size = 0;
1623                 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1624                 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1625
1626                 dt_write_lock(env, o, 0);
1627                 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1628                                &lgi->lgi_dof, th);
1629                 dt_write_unlock(env, o);
1630                 if (rc < 0)
1631                         RETURN(rc);
1632
1633                 rc = llog_osd_regular_fid_add_name_entry(env, o, th, false);
1634
1635                 RETURN(rc);
1636         }
1637
1638         los = res->private_data;
1639         LASSERT(los);
1640
1641         dt_write_lock(env, o, 0);
1642         if (!dt_object_exists(o))
1643                 rc = llog_osd_create_new_object(env, los, o, th);
1644         else
1645                 rc = -EEXIST;
1646
1647         dt_write_unlock(env, o);
1648         if (rc)
1649                 RETURN(rc);
1650
1651         if (res->lgh_name) {
1652                 struct dt_object *llog_dir;
1653
1654                 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1655                 if (IS_ERR(llog_dir))
1656                         RETURN(PTR_ERR(llog_dir));
1657
1658                 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1659                 rec->rec_fid = &lgi->lgi_fid;
1660                 rec->rec_type = S_IFREG;
1661                 dt_read_lock(env, llog_dir, 0);
1662                 rc = dt_insert(env, llog_dir, (struct dt_rec *)rec,
1663                                (struct dt_key *)res->lgh_name, th);
1664                 dt_read_unlock(env, llog_dir);
1665                 dt_object_put(env, llog_dir);
1666                 if (rc)
1667                         CERROR("%s: can't create named llog %s: rc = %d\n",
1668                                o->do_lu.lo_dev->ld_obd->obd_name,
1669                                res->lgh_name, rc);
1670         }
1671         RETURN(rc);
1672 }
1673
1674 /**
1675  * Implementation of the llog_operations::lop_close
1676  *
1677  * This function closes the llog. It just put llog object and referenced
1678  * local storage.
1679  *
1680  * \param[in] env       execution environment
1681  * \param[in] handle    llog handle of the current llog
1682  *
1683  * \retval              0 on successful llog close
1684  * \retval              negative value on error
1685  */
1686 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
1687 {
1688         struct local_oid_storage        *los;
1689         int                              rc = 0;
1690
1691         ENTRY;
1692
1693         LASSERT(handle->lgh_obj);
1694
1695         if (handle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1696                 /* Remove the object from the cache, otherwise it may
1697                  * hold LOD being released during cleanup process */
1698                 dt_object_put_nocache(env, handle->lgh_obj);
1699                 LASSERT(handle->private_data == NULL);
1700                 RETURN(rc);
1701         } else {
1702                 dt_object_put(env, handle->lgh_obj);
1703         }
1704         los = handle->private_data;
1705         LASSERT(los);
1706         dt_los_put(los);
1707
1708         if (handle->lgh_name)
1709                 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
1710
1711         RETURN(rc);
1712 }
1713
1714 /**
1715  * delete llog object name entry
1716  *
1717  * Delete llog object (with regular FID) from name space (under
1718  * update_log_dir).
1719  *
1720  * \param [in] env      execution environment
1721  * \param [in] dto      llog object
1722  * \param [in] th       thandle
1723  * \param [in] declare  if it is declare or execution
1724  *
1725  * \retval              0 if deletion succeeds.
1726  * \retval              negative errno if deletion fails.
1727  */
1728 static int
1729 llog_osd_regular_fid_del_name_entry(const struct lu_env *env,
1730                                     struct dt_object *dto,
1731                                     struct thandle *th, bool declare)
1732 {
1733         struct llog_thread_info *lgi = llog_info(env);
1734         const struct lu_fid     *fid = lu_object_fid(&dto->do_lu);
1735         struct dt_object        *dir;
1736         char                    *name = lgi->lgi_name;
1737         int                     rc;
1738         ENTRY;
1739
1740         if (!fid_is_norm(fid))
1741                 RETURN(0);
1742
1743         dir = llog_osd_get_regular_fid_dir(env, dto);
1744         if (IS_ERR(dir))
1745                 RETURN(PTR_ERR(dir));
1746
1747         snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1748         dt_write_lock(env, dir, 0);
1749         if (declare) {
1750                 rc = dt_declare_delete(env, dir, (struct dt_key *)name,
1751                                        th);
1752         } else {
1753                 rc = dt_delete(env, dir, (struct dt_key *)name, th);
1754         }
1755         dt_write_unlock(env, dir);
1756
1757         dt_object_put(env, dir);
1758         RETURN(rc);
1759 }
1760
1761 /**
1762  * Implementation of the llog_operations::lop_declare_destroy
1763  *
1764  * This function declare destroys the llog and deletes also entry in the
1765  * llog directory in case of named llog. Llog should be opened prior that.
1766  *
1767  * \param[in] env               execution environment
1768  * \param[in] loghandle llog handle of the current llog
1769  *
1770  * \retval              0 on successful destroy
1771  * \retval              negative value on error
1772  */
1773 static int llog_osd_declare_destroy(const struct lu_env *env,
1774                                     struct llog_handle *loghandle,
1775                                     struct thandle *th)
1776 {
1777         struct llog_ctxt        *ctxt;
1778         struct dt_object        *o, *llog_dir = NULL;
1779         int                      rc;
1780
1781         ENTRY;
1782
1783         ctxt = loghandle->lgh_ctxt;
1784         LASSERT(ctxt);
1785
1786         o = loghandle->lgh_obj;
1787         LASSERT(o);
1788
1789         if (loghandle->lgh_name) {
1790                 llog_dir = llog_osd_dir_get(env, ctxt);
1791                 if (IS_ERR(llog_dir))
1792                         RETURN(PTR_ERR(llog_dir));
1793
1794                 rc = dt_declare_delete(env, llog_dir,
1795                                        (struct dt_key *)loghandle->lgh_name,
1796                                        th);
1797                 if (rc < 0)
1798                         GOTO(out_put, rc);
1799         }
1800
1801         rc = dt_declare_ref_del(env, o, th);
1802         if (rc < 0)
1803                 GOTO(out_put, rc);
1804
1805         rc = dt_declare_destroy(env, o, th);
1806         if (rc < 0)
1807                 GOTO(out_put, rc);
1808
1809         if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1810                 rc = llog_osd_regular_fid_del_name_entry(env, o, th, true);
1811                 if (rc < 0)
1812                         GOTO(out_put, rc);
1813         }
1814
1815 out_put:
1816         if (!(IS_ERR_OR_NULL(llog_dir)))
1817                 dt_object_put(env, llog_dir);
1818
1819         RETURN(rc);
1820 }
1821
1822
1823 /**
1824  * Implementation of the llog_operations::lop_destroy
1825  *
1826  * This function destroys the llog and deletes also entry in the
1827  * llog directory in case of named llog. Llog should be opened prior that.
1828  * Destroy method is not part of external transaction and does everything
1829  * inside.
1830  *
1831  * \param[in] env               execution environment
1832  * \param[in] loghandle llog handle of the current llog
1833  *
1834  * \retval              0 on successful destroy
1835  * \retval              negative value on error
1836  */
1837 static int llog_osd_destroy(const struct lu_env *env,
1838                             struct llog_handle *loghandle, struct thandle *th)
1839 {
1840         struct llog_ctxt        *ctxt;
1841         struct dt_object        *o, *llog_dir = NULL;
1842         int                      rc;
1843
1844         ENTRY;
1845
1846         ctxt = loghandle->lgh_ctxt;
1847         LASSERT(ctxt != NULL);
1848
1849         o = loghandle->lgh_obj;
1850         LASSERT(o != NULL);
1851
1852         dt_write_lock(env, o, 0);
1853         if (!llog_osd_exist(loghandle))
1854                 GOTO(out_unlock, rc = 0);
1855
1856         if (loghandle->lgh_name) {
1857                 llog_dir = llog_osd_dir_get(env, ctxt);
1858                 if (IS_ERR(llog_dir))
1859                         GOTO(out_unlock, rc = PTR_ERR(llog_dir));
1860
1861                 dt_read_lock(env, llog_dir, 0);
1862                 rc = dt_delete(env, llog_dir,
1863                                (struct dt_key *)loghandle->lgh_name,
1864                                th);
1865                 dt_read_unlock(env, llog_dir);
1866                 if (rc) {
1867                         CERROR("%s: can't remove llog %s: rc = %d\n",
1868                                o->do_lu.lo_dev->ld_obd->obd_name,
1869                                loghandle->lgh_name, rc);
1870                         GOTO(out_unlock, rc);
1871                 }
1872         }
1873
1874         dt_ref_del(env, o, th);
1875         rc = dt_destroy(env, o, th);
1876         if (rc < 0)
1877                 GOTO(out_unlock, rc);
1878
1879         loghandle->lgh_destroyed = true;
1880         if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1881                 rc = llog_osd_regular_fid_del_name_entry(env, o, th, false);
1882                 if (rc < 0)
1883                         GOTO(out_unlock, rc);
1884         }
1885
1886 out_unlock:
1887         dt_write_unlock(env, o);
1888         if (!(IS_ERR_OR_NULL(llog_dir)))
1889                 dt_object_put(env, llog_dir);
1890         RETURN(rc);
1891 }
1892
1893 /**
1894  * Implementation of the llog_operations::lop_setup
1895  *
1896  * This function setup the llog on local storage.
1897  *
1898  * \param[in] env       execution environment
1899  * \param[in] obd       obd device the llog belongs to
1900  * \param[in] olg       the llog group, it is always zero group now.
1901  * \param[in] ctxt_idx  the llog index, it defines the purpose of this llog.
1902  *                      Every new llog type have to use own index.
1903  * \param[in] disk_obd  the storage obd, where llog is stored.
1904  *
1905  * \retval              0 on successful llog setup
1906  * \retval              negative value on error
1907  */
1908 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1909                           struct obd_llog_group *olg, int ctxt_idx,
1910                           struct obd_device *disk_obd)
1911 {
1912         struct llog_thread_info         *lgi = llog_info(env);
1913         struct llog_ctxt                *ctxt;
1914         int                              rc = 0;
1915         ENTRY;
1916
1917         LASSERT(obd);
1918         LASSERT(olg->olg_ctxts[ctxt_idx]);
1919
1920         ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1921         LASSERT(ctxt);
1922
1923         if (disk_obd == NULL)
1924                 GOTO(out, rc = 0);
1925
1926         /* initialize data allowing to generate new fids,
1927          * literally we need a sequece */
1928         lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1929         lgi->lgi_fid.f_oid = 1;
1930         lgi->lgi_fid.f_ver = 0;
1931         rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1932                                     &lgi->lgi_fid,
1933                                     &ctxt->loc_los_nameless);
1934         if (rc != 0)
1935                 GOTO(out, rc);
1936
1937         lgi->lgi_fid.f_seq = FID_SEQ_LLOG_NAME;
1938         lgi->lgi_fid.f_oid = 1;
1939         lgi->lgi_fid.f_ver = 0;
1940         rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1941                                     &lgi->lgi_fid,
1942                                     &ctxt->loc_los_named);
1943         if (rc != 0) {
1944                 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1945                 ctxt->loc_los_nameless = NULL;
1946         }
1947
1948         GOTO(out, rc);
1949
1950 out:
1951         llog_ctxt_put(ctxt);
1952         return rc;
1953 }
1954
1955 /**
1956  * Implementation of the llog_operations::lop_cleanup
1957  *
1958  * This function cleanups the llog on local storage.
1959  *
1960  * \param[in] env       execution environment
1961  * \param[in] ctxt      the llog context
1962  *
1963  * \retval              0
1964  */
1965 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1966 {
1967         if (ctxt->loc_los_nameless != NULL) {
1968                 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1969                 ctxt->loc_los_nameless = NULL;
1970         }
1971
1972         if (ctxt->loc_los_named != NULL) {
1973                 local_oid_storage_fini(env, ctxt->loc_los_named);
1974                 ctxt->loc_los_named = NULL;
1975         }
1976
1977         return 0;
1978 }
1979
1980 const struct llog_operations llog_osd_ops = {
1981         .lop_next_block         = llog_osd_next_block,
1982         .lop_prev_block         = llog_osd_prev_block,
1983         .lop_read_header        = llog_osd_read_header,
1984         .lop_declare_destroy    = llog_osd_declare_destroy,
1985         .lop_destroy            = llog_osd_destroy,
1986         .lop_setup              = llog_osd_setup,
1987         .lop_cleanup            = llog_osd_cleanup,
1988         .lop_open               = llog_osd_open,
1989         .lop_exist              = llog_osd_exist,
1990         .lop_declare_create     = llog_osd_declare_create,
1991         .lop_create             = llog_osd_create,
1992         .lop_declare_write_rec  = llog_osd_declare_write_rec,
1993         .lop_write_rec          = llog_osd_write_rec,
1994         .lop_close              = llog_osd_close,
1995 };
1996 EXPORT_SYMBOL(llog_osd_ops);
1997
1998 const struct llog_operations llog_common_cat_ops = {
1999         .lop_next_block         = llog_osd_next_block,
2000         .lop_prev_block         = llog_osd_prev_block,
2001         .lop_read_header        = llog_osd_read_header,
2002         .lop_declare_destroy    = llog_osd_declare_destroy,
2003         .lop_destroy            = llog_osd_destroy,
2004         .lop_setup              = llog_osd_setup,
2005         .lop_cleanup            = llog_osd_cleanup,
2006         .lop_open               = llog_osd_open,
2007         .lop_exist              = llog_osd_exist,
2008         .lop_declare_create     = llog_osd_declare_create,
2009         .lop_create             = llog_osd_create,
2010         .lop_declare_write_rec  = llog_osd_declare_write_rec,
2011         .lop_write_rec          = llog_osd_write_rec,
2012         .lop_close              = llog_osd_close,
2013         .lop_add                = llog_cat_add_rec,
2014         .lop_declare_add        = llog_cat_declare_add_rec,
2015 };
2016 EXPORT_SYMBOL(llog_common_cat_ops);
2017
2018 /**
2019  * Read the special file which contains the list of llog catalogs IDs
2020  *
2021  * This function reads the CATALOGS file which contains the array of llog
2022  * catalogs IDs. The main purpose of this file is to store OSP llogs indexed
2023  * by OST/MDT number.
2024  *
2025  * \param[in]  env              execution environment
2026  * \param[in]  d                corresponding storage device
2027  * \param[in]  idx              position to start from, usually OST/MDT index
2028  * \param[in]  count            how many catalog IDs to read
2029  * \param[out] idarray          the buffer for the data. If it is NULL then
2030  *                              function returns just number of catalog IDs
2031  *                              in the file.
2032  * \param[in]  fid              LLOG_CATALOGS_OID for CATALOG object
2033  *
2034  * \retval                      0 on successful read of catalog IDs
2035  * \retval                      negative value on error
2036  * \retval                      positive value which is number of records in
2037  *                              the file if \a idarray is NULL
2038  */
2039 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
2040                           int idx, int count, struct llog_catid *idarray,
2041                           const struct lu_fid *fid)
2042 {
2043         struct llog_thread_info *lgi = llog_info(env);
2044         struct dt_object        *o = NULL;
2045         struct thandle          *th;
2046         int                      rc, size;
2047
2048         ENTRY;
2049
2050         LASSERT(d);
2051
2052         size = sizeof(*idarray) * count;
2053         lgi->lgi_off = idx *  sizeof(*idarray);
2054
2055         lgi->lgi_fid = *fid;
2056         o = dt_locate(env, d, &lgi->lgi_fid);
2057         if (IS_ERR(o))
2058                 RETURN(PTR_ERR(o));
2059
2060         if (!dt_object_exists(o)) {
2061                 th = dt_trans_create(env, d);
2062                 if (IS_ERR(th))
2063                         GOTO(out, rc = PTR_ERR(th));
2064
2065                 lgi->lgi_attr.la_valid = LA_MODE | LA_TYPE;
2066                 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
2067                 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
2068
2069                 th->th_wait_submit = 1;
2070                 /* Make the llog object creation synchronization, so
2071                  * it will be reliable to the reference, especially
2072                  * for remote reference */
2073                 th->th_sync = 1;
2074
2075                 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
2076                                        &lgi->lgi_dof, th);
2077                 if (rc)
2078                         GOTO(out_trans, rc);
2079
2080                 rc = dt_trans_start_local(env, d, th);
2081                 if (rc)
2082                         GOTO(out_trans, rc);
2083
2084                 dt_write_lock(env, o, 0);
2085                 if (!dt_object_exists(o))
2086                         rc = dt_create(env, o, &lgi->lgi_attr, NULL,
2087                                        &lgi->lgi_dof, th);
2088                 dt_write_unlock(env, o);
2089 out_trans:
2090                 dt_trans_stop(env, d, th);
2091                 if (rc)
2092                         GOTO(out, rc);
2093         }
2094
2095         rc = dt_attr_get(env, o, &lgi->lgi_attr);
2096         if (rc)
2097                 GOTO(out, rc);
2098
2099         if (!S_ISREG(lgi->lgi_attr.la_mode)) {
2100                 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
2101                        o->do_lu.lo_dev->ld_obd->obd_name,
2102                        lgi->lgi_attr.la_mode);
2103                 GOTO(out, rc = -ENOENT);
2104         }
2105
2106         CDEBUG(D_CONFIG, "cat list: disk size=%d, read=%d\n",
2107                (int)lgi->lgi_attr.la_size, size);
2108
2109         /* return just number of llogs */
2110         if (idarray == NULL) {
2111                 rc = lgi->lgi_attr.la_size / sizeof(*idarray);
2112                 GOTO(out, rc);
2113         }
2114
2115         /* read for new ost index or for empty file */
2116         memset(idarray, 0, size);
2117         if (lgi->lgi_attr.la_size <= lgi->lgi_off)
2118                 GOTO(out, rc = 0);
2119         if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
2120                 size = lgi->lgi_attr.la_size - lgi->lgi_off;
2121
2122         lgi->lgi_buf.lb_buf = idarray;
2123         lgi->lgi_buf.lb_len = size;
2124         rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
2125         /* -EFAULT means the llog is a sparse file. This is not an error
2126          * after arbitrary OST index is supported. */
2127         if (rc < 0 && rc != -EFAULT) {
2128                 CERROR("%s: error reading CATALOGS: rc = %d\n",
2129                        o->do_lu.lo_dev->ld_obd->obd_name,  rc);
2130                 GOTO(out, rc);
2131         }
2132
2133         EXIT;
2134 out:
2135         dt_object_put(env, o);
2136         RETURN(rc);
2137 }
2138 EXPORT_SYMBOL(llog_osd_get_cat_list);
2139
2140 /**
2141  * Write the special file which contains the list of llog catalogs IDs
2142  *
2143  * This function writes the CATALOG file which contains the array of llog
2144  * catalogs IDs. It is used mostly to store OSP llogs indexed by OST/MDT
2145  * number.
2146  *
2147  * \param[in]  env      execution environment
2148  * \param[in]  d        corresponding storage device
2149  * \param[in]  idx      position to start from, usually OST/MDT index
2150  * \param[in]  count    how many catalog IDs to write
2151  * \param[out] idarray  the buffer with the data to write.
2152  * \param[in]  fid      LLOG_CATALOGS_OID for CATALOG object
2153  *
2154  * \retval              0 on successful write of catalog IDs
2155  * \retval              negative value on error
2156  */
2157 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
2158                           int idx, int count, struct llog_catid *idarray,
2159                           const struct lu_fid *fid)
2160 {
2161         struct llog_thread_info *lgi = llog_info(env);
2162         struct dt_object        *o = NULL;
2163         struct thandle          *th;
2164         int                      rc, size;
2165
2166         if (count == 0)
2167                 RETURN(0);
2168
2169         LASSERT(d);
2170
2171         size = sizeof(*idarray) * count;
2172         lgi->lgi_off = idx * sizeof(*idarray);
2173         lgi->lgi_fid = *fid;
2174
2175         o = dt_locate(env, d, &lgi->lgi_fid);
2176         if (IS_ERR(o))
2177                 RETURN(PTR_ERR(o));
2178
2179         if (!dt_object_exists(o))
2180                 GOTO(out, rc = -ENOENT);
2181
2182         rc = dt_attr_get(env, o, &lgi->lgi_attr);
2183         if (rc)
2184                 GOTO(out, rc);
2185
2186         if (!S_ISREG(lgi->lgi_attr.la_mode)) {
2187                 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
2188                        o->do_lu.lo_dev->ld_obd->obd_name,
2189                        lgi->lgi_attr.la_mode);
2190                 GOTO(out, rc = -ENOENT);
2191         }
2192
2193         th = dt_trans_create(env, d);
2194         if (IS_ERR(th))
2195                 GOTO(out, rc = PTR_ERR(th));
2196
2197         lgi->lgi_buf.lb_len = size;
2198         lgi->lgi_buf.lb_buf = idarray;
2199         rc = dt_declare_record_write(env, o, &lgi->lgi_buf, lgi->lgi_off, th);
2200         if (rc)
2201                 GOTO(out_trans, rc);
2202
2203         /* For update log, this happens during initialization,
2204          * see lod_sub_prep_llog(), and we need make sure catlog
2205          * file ID is written to catlist file(committed) before
2206          * cross-MDT operation write update records to catlog FILE,
2207          * otherwise, during failover these update records might
2208          * missing */
2209         if (fid_is_update_log(fid))
2210                 th->th_sync = 1;
2211
2212         rc = dt_trans_start_local(env, d, th);
2213         if (rc)
2214                 GOTO(out_trans, rc);
2215
2216         th->th_wait_submit = 1;
2217
2218         rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
2219         if (rc)
2220                 CDEBUG(D_INODE, "can't write CATALOGS at index %d: rc = %d\n",
2221                        idx, rc);
2222 out_trans:
2223         dt_trans_stop(env, d, th);
2224 out:
2225         dt_object_put(env, o);
2226         RETURN(rc);
2227 }
2228 EXPORT_SYMBOL(llog_osd_put_cat_list);