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