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