Whamcloud - gitweb
LU-14688 mdt: changelog purge deletes plain llog
[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         if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_CHANGELOG_DEL) &&
907             cfs_fail_val == ((unsigned long)loghandle & 0xFFFFFFFF)) {
908                 OBD_RACE(OBD_FAIL_MDS_CHANGELOG_DEL);
909                 msleep(MSEC_PER_SEC >> 2);
910         }
911
912         o = loghandle->lgh_obj;
913         LASSERT(o);
914         dt_read_lock(env, o, 0);
915         if (!llog_osd_exist(loghandle))
916                 GOTO(out, rc = -ESTALE); //object was destroyed
917
918         dt = lu2dt_dev(o->do_lu.lo_dev);
919         LASSERT(dt);
920
921         rc = dt_attr_get(env, o, &lgi->lgi_attr);
922         if (rc)
923                 GOTO(out, rc);
924
925         CDEBUG(D_OTHER,
926                "looking for log index %u (cur idx %u off %llu), size %llu\n",
927                next_idx, *cur_idx,
928                *cur_offset, lgi->lgi_attr.la_size);
929
930         while (*cur_offset < lgi->lgi_attr.la_size) {
931                 struct llog_rec_hdr     *rec, *last_rec;
932                 struct llog_rec_tail    *tail;
933
934                 llog_skip_over(loghandle, cur_offset, *cur_idx,
935                                next_idx, chunk_size, force_mini_rec);
936
937                 /* read up to next llog chunk_size block */
938                 lgi->lgi_buf.lb_len = chunk_size -
939                                       (*cur_offset & (chunk_size - 1));
940                 lgi->lgi_buf.lb_buf = buf;
941
942                 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
943                 if (rc < 0) {
944                         if (rc == -EBADR && !force_mini_rec)
945                                 goto retry;
946
947                         CERROR("%s: can't read llog block from log "DFID
948                                " offset %llu: rc = %d\n",
949                                o->do_lu.lo_dev->ld_obd->obd_name,
950                                PFID(lu_object_fid(&o->do_lu)), *cur_offset,
951                                rc);
952                         GOTO(out, rc);
953                 }
954
955                 if (rc < len) {
956                         /* signal the end of the valid buffer to
957                          * llog_process */
958                         memset(buf + rc, 0, len - rc);
959                 }
960
961                 if (rc == 0) { /* end of file, nothing to do */
962                         if (!force_mini_rec)
963                                 goto retry;
964                         GOTO(out, rc);
965                 }
966
967                 if (rc < sizeof(*tail)) {
968                         if (!force_mini_rec)
969                                 goto retry;
970
971                         CERROR("%s: invalid llog block at log id "DFID":%x "
972                                "offset %llu\n",
973                                o->do_lu.lo_dev->ld_obd->obd_name,
974                                PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
975                                loghandle->lgh_id.lgl_ogen, *cur_offset);
976                         GOTO(out, rc = -EINVAL);
977                 }
978
979                 rec = buf;
980                 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
981                         lustre_swab_llog_rec(rec);
982                 tail = (struct llog_rec_tail *)((char *)buf + rc -
983                                                 sizeof(struct llog_rec_tail));
984
985                 if (llog_verify_record(loghandle, rec)) {
986                         /*
987                          * the block seems corrupted. make a pad record so the
988                          * caller can skip the block and try with the next one
989                          */
990                         rec->lrh_len = rc;
991                         rec->lrh_index = next_idx;
992                         rec->lrh_type = LLOG_PAD_MAGIC;
993
994                         tail = rec_tail(rec);
995                         tail->lrt_len = rc;
996                         tail->lrt_index = next_idx;
997
998                         GOTO(out, rc = 0);
999                 }
1000
1001                 /* get the last record in block */
1002                 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
1003                                                    tail->lrt_len);
1004
1005                 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
1006                         lustre_swab_llog_rec(last_rec);
1007
1008                 if (last_rec->lrh_index != tail->lrt_index) {
1009                         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",
1010                                o->do_lu.lo_dev->ld_obd->obd_name,
1011                                PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
1012                                loghandle->lgh_id.lgl_ogen, *cur_offset,
1013                                last_rec->lrh_index, tail->lrt_index,
1014                                tail->lrt_len, rc);
1015                         GOTO(out, rc = -EINVAL);
1016                 }
1017
1018                 *cur_idx = tail->lrt_index;
1019
1020                 /* this shouldn't happen */
1021                 if (tail->lrt_index == 0) {
1022                         CERROR("%s: invalid llog tail at log id "DFID":%x "
1023                                "offset %llu bytes %d\n",
1024                                o->do_lu.lo_dev->ld_obd->obd_name,
1025                                PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
1026                                loghandle->lgh_id.lgl_ogen, *cur_offset, rc);
1027                         GOTO(out, rc = -EINVAL);
1028                 }
1029                 if (tail->lrt_index < next_idx) {
1030                         last_idx = *cur_idx;
1031                         last_offset = *cur_offset;
1032                         continue;
1033                 }
1034
1035                 /* sanity check that the start of the new buffer is no farther
1036                  * than the record that we wanted.  This shouldn't happen. */
1037                 if (next_idx && rec->lrh_index > next_idx) {
1038                         if (!force_mini_rec && next_idx > last_idx)
1039                                 goto retry;
1040
1041                         CERROR("%s: missed desired record? %u > %u\n",
1042                                o->do_lu.lo_dev->ld_obd->obd_name,
1043                                rec->lrh_index, next_idx);
1044                         GOTO(out, rc = -ENOENT);
1045                 }
1046
1047                 /* Trim unsupported extensions for compat w/ older clients */
1048                 changelog_block_trim_ext(rec, last_rec, loghandle);
1049
1050                 GOTO(out, rc = 0);
1051
1052 retry:
1053                 /* Note: because there are some pad records in the
1054                  * llog, so llog_skip_over() might skip too much
1055                  * records, let's try skip again with minimum record */
1056                 force_mini_rec = true;
1057                 *cur_offset = last_offset;
1058                 *cur_idx = last_idx;
1059         }
1060         GOTO(out, rc = -EIO);
1061 out:
1062         dt_read_unlock(env, o);
1063         return rc;
1064 }
1065
1066 /**
1067  * Implementation of the llog_operations::lop_prev_block
1068  *
1069  * This function finds the llog block to return which contains
1070  * record with required index but in reverse order - from end of llog
1071  * to the beginning.
1072  * It is main part of reverse llog processing.
1073  *
1074  * \param[in] env       execution environment
1075  * \param[in] loghandle llog handle of the current llog
1076  * \param[in] prev_idx  target index to find
1077  * \param[in] buf       pointer to data buffer to fill
1078  * \param[in] len       required len to read, it is llog_chunk_size usually.
1079  *
1080  * \retval              0 on successful buffer read
1081  * \retval              negative value on error
1082  */
1083 static int llog_osd_prev_block(const struct lu_env *env,
1084                                struct llog_handle *loghandle,
1085                                int prev_idx, void *buf, int len)
1086 {
1087         struct llog_thread_info *lgi = llog_info(env);
1088         struct dt_object        *o;
1089         struct dt_device        *dt;
1090         loff_t                   cur_offset;
1091         __u32                   chunk_size;
1092         int                      rc;
1093
1094         ENTRY;
1095
1096         chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
1097         if (len == 0 || len & (chunk_size - 1))
1098                 RETURN(-EINVAL);
1099
1100         CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
1101
1102         LASSERT(loghandle);
1103         LASSERT(loghandle->lgh_ctxt);
1104
1105         o = loghandle->lgh_obj;
1106         LASSERT(o);
1107         dt_read_lock(env, o, 0);
1108         if (!llog_osd_exist(loghandle))
1109                 GOTO(out, rc = -ESTALE);
1110
1111         dt = lu2dt_dev(o->do_lu.lo_dev);
1112         LASSERT(dt);
1113
1114         /* Let's only use mini record size for previous block read
1115          * for now XXX */
1116         cur_offset = chunk_size;
1117         llog_skip_over(loghandle, &cur_offset, 0, prev_idx,
1118                        chunk_size, true);
1119
1120         rc = dt_attr_get(env, o, &lgi->lgi_attr);
1121         if (rc)
1122                 GOTO(out, rc);
1123
1124         while (cur_offset < lgi->lgi_attr.la_size) {
1125                 struct llog_rec_hdr     *rec, *last_rec;
1126                 struct llog_rec_tail    *tail;
1127
1128                 lgi->lgi_buf.lb_len = len;
1129                 lgi->lgi_buf.lb_buf = buf;
1130                 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
1131                 if (rc < 0) {
1132                         CERROR("%s: can't read llog block from log "DFID
1133                                " offset %llu: rc = %d\n",
1134                                o->do_lu.lo_dev->ld_obd->obd_name,
1135                                PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
1136                         GOTO(out, rc);
1137                 }
1138
1139                 if (rc == 0) /* end of file, nothing to do */
1140                         GOTO(out, rc);
1141
1142                 if (rc < sizeof(*tail)) {
1143                         CERROR("%s: invalid llog block at log id "DFID":%x "
1144                                "offset %llu\n",
1145                                o->do_lu.lo_dev->ld_obd->obd_name,
1146                                PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
1147                                loghandle->lgh_id.lgl_ogen, cur_offset);
1148                         GOTO(out, rc = -EINVAL);
1149                 }
1150
1151                 rec = buf;
1152                 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
1153                         lustre_swab_llog_rec(rec);
1154
1155                 tail = (struct llog_rec_tail *)((char *)buf + rc -
1156                                                 sizeof(struct llog_rec_tail));
1157                 /* get the last record in block */
1158                 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
1159                                                    le32_to_cpu(tail->lrt_len));
1160
1161                 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
1162                         lustre_swab_llog_rec(last_rec);
1163                 LASSERT(last_rec->lrh_index == tail->lrt_index);
1164
1165                 /* this shouldn't happen */
1166                 if (tail->lrt_index == 0) {
1167                         CERROR("%s: invalid llog tail at log id "DFID":%x "
1168                                "offset %llu\n",
1169                                o->do_lu.lo_dev->ld_obd->obd_name,
1170                                PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
1171                                loghandle->lgh_id.lgl_ogen, cur_offset);
1172                         GOTO(out, rc = -EINVAL);
1173                 }
1174                 if (tail->lrt_index < prev_idx)
1175                         continue;
1176
1177                 /* sanity check that the start of the new buffer is no farther
1178                  * than the record that we wanted.  This shouldn't happen. */
1179                 if (rec->lrh_index > prev_idx) {
1180                         CERROR("%s: missed desired record? %u > %u\n",
1181                                o->do_lu.lo_dev->ld_obd->obd_name,
1182                                rec->lrh_index, prev_idx);
1183                         GOTO(out, rc = -ENOENT);
1184                 }
1185
1186                 /* Trim unsupported extensions for compat w/ older clients */
1187                 changelog_block_trim_ext(rec, last_rec, loghandle);
1188
1189                 GOTO(out, rc = 0);
1190         }
1191         GOTO(out, rc = -EIO);
1192 out:
1193         dt_read_unlock(env, o);
1194         return rc;
1195 }
1196
1197 /**
1198  * This is helper function to get llog directory object. It is used by named
1199  * llog operations to find/insert/delete llog entry from llog directory.
1200  *
1201  * \param[in] env       execution environment
1202  * \param[in] ctxt      llog context
1203  *
1204  * \retval              dt_object of llog directory
1205  * \retval              ERR_PTR of negative value on error
1206  */
1207 static struct dt_object *llog_osd_dir_get(const struct lu_env *env,
1208                                           struct llog_ctxt *ctxt)
1209 {
1210         struct dt_device        *dt;
1211         struct dt_thread_info   *dti = dt_info(env);
1212         struct dt_object        *dir;
1213         int                      rc;
1214
1215         dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1216         if (ctxt->loc_dir == NULL) {
1217                 rc = dt_root_get(env, dt, &dti->dti_fid);
1218                 if (rc)
1219                         return ERR_PTR(rc);
1220                 dir = dt_locate(env, dt, &dti->dti_fid);
1221
1222                 if (!IS_ERR(dir) && !dt_try_as_dir(env, dir)) {
1223                         dt_object_put(env, dir);
1224                         return ERR_PTR(-ENOTDIR);
1225                 }
1226         } else {
1227                 lu_object_get(&ctxt->loc_dir->do_lu);
1228                 dir = ctxt->loc_dir;
1229         }
1230
1231         return dir;
1232 }
1233
1234 /**
1235  * Implementation of the llog_operations::lop_open
1236  *
1237  * This function opens the llog by its logid or by name, it may open also
1238  * non existent llog and assing then new id to it.
1239  * The llog_open/llog_close pair works similar to lu_object_find/put,
1240  * the object may not exist prior open. The result of open is just dt_object
1241  * in the llog header.
1242  *
1243  * \param[in] env               execution environment
1244  * \param[in] handle            llog handle of the current llog
1245  * \param[in] logid             logid of llog to open (nameless llog)
1246  * \param[in] name              name of llog to open (named llog)
1247  * \param[in] open_param
1248  *                              LLOG_OPEN_NEW - new llog, may not exist
1249  *                              LLOG_OPEN_EXIST - old llog, must exist
1250  *
1251  * \retval                      0 on successful open, llog_handle::lgh_obj
1252  *                              contains the dt_object of the llog.
1253  * \retval                      negative value on error
1254  */
1255 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
1256                          struct llog_logid *logid, char *name,
1257                          enum llog_open_param open_param)
1258 {
1259         struct llog_thread_info         *lgi = llog_info(env);
1260         struct llog_ctxt                *ctxt = handle->lgh_ctxt;
1261         struct dt_object                *o;
1262         struct dt_device                *dt;
1263         struct ls_device                *ls;
1264         struct local_oid_storage        *los = NULL;
1265         int                              rc = 0;
1266         bool new_id = false;
1267
1268         ENTRY;
1269
1270         LASSERT(env);
1271         LASSERT(ctxt);
1272         LASSERT(ctxt->loc_exp);
1273         LASSERT(ctxt->loc_exp->exp_obd);
1274         dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1275         LASSERT(dt);
1276         if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1277                 struct lu_object_conf conf = { 0 };
1278                 if (logid != NULL) {
1279                         logid_to_fid(logid, &lgi->lgi_fid);
1280                 } else {
1281                         /* If logid == NULL, then it means the caller needs
1282                          * to allocate new FID (llog_cat_declare_add_rec()). */
1283                         rc = dt_fid_alloc(env, dt, &lgi->lgi_fid, NULL, NULL);
1284                         if (rc < 0)
1285                                 RETURN(rc);
1286                         rc = 0;
1287                         conf.loc_flags = LOC_F_NEW;
1288                 }
1289
1290                 o = dt_locate_at(env, dt, &lgi->lgi_fid,
1291                                  dt->dd_lu_dev.ld_site->ls_top_dev, &conf);
1292                 if (IS_ERR(o))
1293                         RETURN(PTR_ERR(o));
1294
1295                 goto after_open;
1296         }
1297
1298         ls = ls_device_get(dt);
1299         if (IS_ERR(ls))
1300                 RETURN(PTR_ERR(ls));
1301
1302         mutex_lock(&ls->ls_los_mutex);
1303         los = dt_los_find(ls, name != NULL ? FID_SEQ_LLOG_NAME : FID_SEQ_LLOG);
1304         mutex_unlock(&ls->ls_los_mutex);
1305         LASSERT(los);
1306         ls_device_put(env, ls);
1307
1308         LASSERT(handle);
1309
1310         if (logid != NULL) {
1311                 logid_to_fid(logid, &lgi->lgi_fid);
1312         } else if (name) {
1313                 struct dt_object *llog_dir;
1314
1315                 llog_dir = llog_osd_dir_get(env, ctxt);
1316                 if (IS_ERR(llog_dir))
1317                         GOTO(out, rc = PTR_ERR(llog_dir));
1318                 dt_read_lock(env, llog_dir, 0);
1319                 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
1320                 dt_read_unlock(env, llog_dir);
1321                 dt_object_put(env, llog_dir);
1322                 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
1323                         /* generate fid for new llog */
1324                         rc = local_object_fid_generate(env, los,
1325                                                        &lgi->lgi_fid);
1326                         new_id = true;
1327                 }
1328                 if (rc < 0)
1329                         GOTO(out, rc);
1330                 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
1331                 if (handle->lgh_name)
1332                         strcpy(handle->lgh_name, name);
1333                 else
1334                         GOTO(out, rc = -ENOMEM);
1335         } else {
1336                 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
1337                 /* generate fid for new llog */
1338 generate:
1339                 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
1340                 if (rc < 0)
1341                         GOTO(out, rc);
1342                 new_id = true;
1343         }
1344         if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_LLOG_UMOUNT_RACE) &&
1345             cfs_fail_val == 1) {
1346                 cfs_fail_val = 2;
1347                 OBD_RACE(OBD_FAIL_MDS_LLOG_UMOUNT_RACE);
1348                 msleep(MSEC_PER_SEC);
1349         }
1350         o = ls_locate(env, ls, &lgi->lgi_fid, NULL);
1351         if (IS_ERR(o))
1352                 GOTO(out_name, rc = PTR_ERR(o));
1353
1354         if (dt_object_exists(o) && new_id) {
1355                 /* llog exists with just generated ID, e.g. some old llog file
1356                  * still is in use or is orphan, drop a warn and skip it. */
1357                 CDEBUG(D_INFO, "%s: llog exists with the same FID: "DFID
1358                        ", skipping\n",
1359                        o->do_lu.lo_dev->ld_obd->obd_name,
1360                        PFID(lu_object_fid(&o->do_lu)));
1361                 dt_object_put(env, o);
1362                 /* just skip this llog ID, we shouldn't delete it because we
1363                  * don't know exactly what is its purpose and state. */
1364                 goto generate;
1365         }
1366
1367 after_open:
1368         /* No new llog is expected but doesn't exist */
1369         if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o)) {
1370                 CDEBUG(D_INFO, "%s: llog FID: "DFID" obj %p doesn`t exist\n",
1371                        o->do_lu.lo_dev->ld_obd->obd_name,
1372                        PFID(lu_object_fid(&o->do_lu)), o);
1373                 GOTO(out_put, rc = -ENOENT);
1374         }
1375         fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
1376         handle->lgh_obj = o;
1377         handle->private_data = los;
1378         LASSERT(handle->lgh_ctxt);
1379
1380         RETURN(rc);
1381
1382 out_put:
1383         dt_object_put(env, o);
1384 out_name:
1385         if (handle->lgh_name != NULL)
1386                 OBD_FREE(handle->lgh_name, strlen(name) + 1);
1387 out:
1388         if (los != NULL)
1389                 dt_los_put(los);
1390         RETURN(rc);
1391 }
1392
1393 /**
1394  * Get dir for regular fid log object
1395  *
1396  * Get directory for regular fid log object, and these regular fid log
1397  * object will be inserted under this directory, to satisfy the FS
1398  * consistency check, e2fsck etc.
1399  *
1400  * \param [in] env      execution environment
1401  * \param [in] dto      llog object
1402  *
1403  * \retval              pointer to the directory if it is found.
1404  * \retval              ERR_PTR(negative errno) if it fails.
1405  */
1406 struct dt_object *llog_osd_get_regular_fid_dir(const struct lu_env *env,
1407                                                struct dt_object *dto)
1408 {
1409         struct llog_thread_info *lgi = llog_info(env);
1410         struct seq_server_site *ss = dto->do_lu.lo_dev->ld_site->ld_seq_site;
1411         struct lu_seq_range     *range = &lgi->lgi_range;
1412         struct lu_fid           *dir_fid = &lgi->lgi_fid;
1413         struct dt_object        *dir;
1414         int                     rc;
1415         ENTRY;
1416
1417         fld_range_set_any(range);
1418         LASSERT(ss != NULL);
1419         rc = ss->ss_server_fld->lsf_seq_lookup(env, ss->ss_server_fld,
1420                                    fid_seq(lu_object_fid(&dto->do_lu)), range);
1421         if (rc < 0)
1422                 RETURN(ERR_PTR(rc));
1423
1424         lu_update_log_dir_fid(dir_fid, range->lsr_index);
1425         dir = dt_locate(env, lu2dt_dev(dto->do_lu.lo_dev), dir_fid);
1426         if (IS_ERR(dir))
1427                 RETURN(dir);
1428
1429         if (!dt_try_as_dir(env, dir)) {
1430                 dt_object_put(env, dir);
1431                 RETURN(ERR_PTR(-ENOTDIR));
1432         }
1433
1434         RETURN(dir);
1435 }
1436
1437 /**
1438  * Add llog object with regular FID to name entry
1439  *
1440  * Add llog object with regular FID to name space, and each llog
1441  * object on each MDT will be /update_log_dir/[seq:oid:ver],
1442  * so to satisfy the namespace consistency check, e2fsck etc.
1443  *
1444  * \param [in] env      execution environment
1445  * \param [in] dto      llog object
1446  * \param [in] th       thandle
1447  * \param [in] declare  if it is declare or execution
1448  *
1449  * \retval              0 if insertion succeeds.
1450  * \retval              negative errno if insertion fails.
1451  */
1452 static int
1453 llog_osd_regular_fid_add_name_entry(const struct lu_env *env,
1454                                     struct dt_object *dto,
1455                                     struct thandle *th, bool declare)
1456 {
1457         struct llog_thread_info *lgi = llog_info(env);
1458         const struct lu_fid     *fid = lu_object_fid(&dto->do_lu);
1459         struct dt_insert_rec    *rec = &lgi->lgi_dt_rec;
1460         struct dt_object        *dir;
1461         char                    *name = lgi->lgi_name;
1462         int                     rc;
1463         ENTRY;
1464
1465         if (!fid_is_norm(fid))
1466                 RETURN(0);
1467
1468         dir = llog_osd_get_regular_fid_dir(env, dto);
1469         if (IS_ERR(dir))
1470                 RETURN(PTR_ERR(dir));
1471
1472         rec->rec_fid = fid;
1473         rec->rec_type = S_IFREG;
1474         snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1475         dt_write_lock(env, dir, 0);
1476         if (declare) {
1477                 rc = dt_declare_insert(env, dir, (struct dt_rec *)rec,
1478                                (struct dt_key *)name, th);
1479         } else {
1480                 rc = dt_insert(env, dir, (struct dt_rec *)rec,
1481                                (struct dt_key *)name, th);
1482         }
1483         dt_write_unlock(env, dir);
1484
1485         dt_object_put(env, dir);
1486         RETURN(rc);
1487 }
1488
1489
1490 /**
1491  * Implementation of the llog_operations::lop_declare_create
1492  *
1493  * This function declares the llog create. It declares also name insert
1494  * into llog directory in case of named llog.
1495  *
1496  * \param[in] env       execution environment
1497  * \param[in] res       llog handle of the current llog
1498  * \param[in] th        current transaction handle
1499  *
1500  * \retval              0 on successful create declaration
1501  * \retval              negative value on error
1502  */
1503 static int llog_osd_declare_create(const struct lu_env *env,
1504                                    struct llog_handle *res, struct thandle *th)
1505 {
1506         struct llog_thread_info         *lgi = llog_info(env);
1507         struct dt_insert_rec            *rec = &lgi->lgi_dt_rec;
1508         struct local_oid_storage        *los;
1509         struct dt_object                *o;
1510         int                              rc;
1511
1512         ENTRY;
1513
1514         LASSERT(res->lgh_obj);
1515         LASSERT(th);
1516
1517         /* object can be created by another thread */
1518         o = res->lgh_obj;
1519         if (dt_object_exists(o))
1520                 RETURN(0);
1521
1522         if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1523                 struct llog_thread_info *lgi = llog_info(env);
1524
1525                 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE;
1526                 lgi->lgi_attr.la_size = 0;
1527                 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1528                 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1529
1530                 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1531                                        &lgi->lgi_dof, th);
1532                 if (rc < 0)
1533                         RETURN(rc);
1534
1535
1536                 rc = llog_osd_regular_fid_add_name_entry(env, o, th, true);
1537
1538                 RETURN(rc);
1539         }
1540         los = res->private_data;
1541         LASSERT(los);
1542
1543         rc = llog_osd_declare_new_object(env, los, o, th);
1544         if (rc)
1545                 RETURN(rc);
1546
1547         /* do not declare header initialization here as it's declared
1548          * in llog_osd_declare_write_rec() which is always called */
1549
1550         if (res->lgh_name) {
1551                 struct dt_object *llog_dir;
1552
1553                 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1554                 if (IS_ERR(llog_dir))
1555                         RETURN(PTR_ERR(llog_dir));
1556                 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1557                 rec->rec_fid = &lgi->lgi_fid;
1558                 rec->rec_type = S_IFREG;
1559                 rc = dt_declare_insert(env, llog_dir,
1560                                        (struct dt_rec *)rec,
1561                                        (struct dt_key *)res->lgh_name, th);
1562                 dt_object_put(env, llog_dir);
1563                 if (rc)
1564                         CERROR("%s: can't declare named llog %s: rc = %d\n",
1565                                o->do_lu.lo_dev->ld_obd->obd_name,
1566                                res->lgh_name, rc);
1567         }
1568         RETURN(rc);
1569 }
1570
1571 /**
1572  * Implementation of the llog_operations::lop_create
1573  *
1574  * This function creates the llog according with llog_handle::lgh_obj
1575  * and llog_handle::lgh_name.
1576  *
1577  * \param[in] env       execution environment
1578  * \param[in] res       llog handle of the current llog
1579  * \param[in] th        current transaction handle
1580  *
1581  * \retval              0 on successful create
1582  * \retval              negative value on error
1583  */
1584 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
1585                            struct thandle *th)
1586 {
1587         struct llog_thread_info *lgi = llog_info(env);
1588         struct dt_insert_rec    *rec = &lgi->lgi_dt_rec;
1589         struct local_oid_storage *los;
1590         struct dt_object        *o;
1591         int                      rc = 0;
1592
1593         ENTRY;
1594
1595         LASSERT(env);
1596         o = res->lgh_obj;
1597         LASSERT(o);
1598
1599         /* llog can be already created */
1600         if (dt_object_exists(o))
1601                 RETURN(-EEXIST);
1602
1603         if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1604                 struct llog_thread_info *lgi = llog_info(env);
1605
1606                 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE | LA_TYPE;
1607                 lgi->lgi_attr.la_size = 0;
1608                 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1609                 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1610
1611                 dt_write_lock(env, o, 0);
1612                 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1613                                &lgi->lgi_dof, th);
1614                 dt_write_unlock(env, o);
1615                 if (rc < 0)
1616                         RETURN(rc);
1617
1618                 rc = llog_osd_regular_fid_add_name_entry(env, o, th, false);
1619
1620                 RETURN(rc);
1621         }
1622
1623         los = res->private_data;
1624         LASSERT(los);
1625
1626         dt_write_lock(env, o, 0);
1627         if (!dt_object_exists(o))
1628                 rc = llog_osd_create_new_object(env, los, o, th);
1629         else
1630                 rc = -EEXIST;
1631
1632         dt_write_unlock(env, o);
1633         if (rc)
1634                 RETURN(rc);
1635
1636         if (res->lgh_name) {
1637                 struct dt_object *llog_dir;
1638
1639                 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1640                 if (IS_ERR(llog_dir))
1641                         RETURN(PTR_ERR(llog_dir));
1642
1643                 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1644                 rec->rec_fid = &lgi->lgi_fid;
1645                 rec->rec_type = S_IFREG;
1646                 dt_read_lock(env, llog_dir, 0);
1647                 rc = dt_insert(env, llog_dir, (struct dt_rec *)rec,
1648                                (struct dt_key *)res->lgh_name, th);
1649                 dt_read_unlock(env, llog_dir);
1650                 dt_object_put(env, llog_dir);
1651                 if (rc)
1652                         CERROR("%s: can't create named llog %s: rc = %d\n",
1653                                o->do_lu.lo_dev->ld_obd->obd_name,
1654                                res->lgh_name, rc);
1655         }
1656         RETURN(rc);
1657 }
1658
1659 /**
1660  * Implementation of the llog_operations::lop_close
1661  *
1662  * This function closes the llog. It just put llog object and referenced
1663  * local storage.
1664  *
1665  * \param[in] env       execution environment
1666  * \param[in] handle    llog handle of the current llog
1667  *
1668  * \retval              0 on successful llog close
1669  * \retval              negative value on error
1670  */
1671 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
1672 {
1673         struct local_oid_storage        *los;
1674         int                              rc = 0;
1675
1676         ENTRY;
1677
1678         LASSERT(handle->lgh_obj);
1679
1680         if (handle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1681                 /* Remove the object from the cache, otherwise it may
1682                  * hold LOD being released during cleanup process */
1683                 dt_object_put_nocache(env, handle->lgh_obj);
1684                 LASSERT(handle->private_data == NULL);
1685                 RETURN(rc);
1686         } else {
1687                 dt_object_put(env, handle->lgh_obj);
1688         }
1689         los = handle->private_data;
1690         LASSERT(los);
1691         dt_los_put(los);
1692
1693         if (handle->lgh_name)
1694                 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
1695
1696         RETURN(rc);
1697 }
1698
1699 /**
1700  * delete llog object name entry
1701  *
1702  * Delete llog object (with regular FID) from name space (under
1703  * update_log_dir).
1704  *
1705  * \param [in] env      execution environment
1706  * \param [in] dto      llog object
1707  * \param [in] th       thandle
1708  * \param [in] declare  if it is declare or execution
1709  *
1710  * \retval              0 if deletion succeeds.
1711  * \retval              negative errno if deletion fails.
1712  */
1713 static int
1714 llog_osd_regular_fid_del_name_entry(const struct lu_env *env,
1715                                     struct dt_object *dto,
1716                                     struct thandle *th, bool declare)
1717 {
1718         struct llog_thread_info *lgi = llog_info(env);
1719         const struct lu_fid     *fid = lu_object_fid(&dto->do_lu);
1720         struct dt_object        *dir;
1721         char                    *name = lgi->lgi_name;
1722         int                     rc;
1723         ENTRY;
1724
1725         if (!fid_is_norm(fid))
1726                 RETURN(0);
1727
1728         dir = llog_osd_get_regular_fid_dir(env, dto);
1729         if (IS_ERR(dir))
1730                 RETURN(PTR_ERR(dir));
1731
1732         snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1733         dt_write_lock(env, dir, 0);
1734         if (declare) {
1735                 rc = dt_declare_delete(env, dir, (struct dt_key *)name,
1736                                        th);
1737         } else {
1738                 rc = dt_delete(env, dir, (struct dt_key *)name, th);
1739         }
1740         dt_write_unlock(env, dir);
1741
1742         dt_object_put(env, dir);
1743         RETURN(rc);
1744 }
1745
1746 /**
1747  * Implementation of the llog_operations::lop_declare_destroy
1748  *
1749  * This function declare destroys the llog and deletes also entry in the
1750  * llog directory in case of named llog. Llog should be opened prior that.
1751  *
1752  * \param[in] env               execution environment
1753  * \param[in] loghandle llog handle of the current llog
1754  *
1755  * \retval              0 on successful destroy
1756  * \retval              negative value on error
1757  */
1758 static int llog_osd_declare_destroy(const struct lu_env *env,
1759                                     struct llog_handle *loghandle,
1760                                     struct thandle *th)
1761 {
1762         struct llog_ctxt        *ctxt;
1763         struct dt_object        *o, *llog_dir = NULL;
1764         int                      rc;
1765
1766         ENTRY;
1767
1768         ctxt = loghandle->lgh_ctxt;
1769         LASSERT(ctxt);
1770
1771         o = loghandle->lgh_obj;
1772         LASSERT(o);
1773
1774         if (loghandle->lgh_name) {
1775                 llog_dir = llog_osd_dir_get(env, ctxt);
1776                 if (IS_ERR(llog_dir))
1777                         RETURN(PTR_ERR(llog_dir));
1778
1779                 rc = dt_declare_delete(env, llog_dir,
1780                                        (struct dt_key *)loghandle->lgh_name,
1781                                        th);
1782                 if (rc < 0)
1783                         GOTO(out_put, rc);
1784         }
1785
1786         rc = dt_declare_ref_del(env, o, th);
1787         if (rc < 0)
1788                 GOTO(out_put, rc);
1789
1790         rc = dt_declare_destroy(env, o, th);
1791         if (rc < 0)
1792                 GOTO(out_put, rc);
1793
1794         if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1795                 rc = llog_osd_regular_fid_del_name_entry(env, o, th, true);
1796                 if (rc < 0)
1797                         GOTO(out_put, rc);
1798         }
1799
1800 out_put:
1801         if (!(IS_ERR_OR_NULL(llog_dir)))
1802                 dt_object_put(env, llog_dir);
1803
1804         RETURN(rc);
1805 }
1806
1807
1808 /**
1809  * Implementation of the llog_operations::lop_destroy
1810  *
1811  * This function destroys the llog and deletes also entry in the
1812  * llog directory in case of named llog. Llog should be opened prior that.
1813  * Destroy method is not part of external transaction and does everything
1814  * inside.
1815  *
1816  * \param[in] env               execution environment
1817  * \param[in] loghandle llog handle of the current llog
1818  *
1819  * \retval              0 on successful destroy
1820  * \retval              negative value on error
1821  */
1822 static int llog_osd_destroy(const struct lu_env *env,
1823                             struct llog_handle *loghandle, struct thandle *th)
1824 {
1825         struct llog_ctxt        *ctxt;
1826         struct dt_object        *o, *llog_dir = NULL;
1827         int                      rc;
1828
1829         ENTRY;
1830
1831         ctxt = loghandle->lgh_ctxt;
1832         LASSERT(ctxt != NULL);
1833
1834         o = loghandle->lgh_obj;
1835         LASSERT(o != NULL);
1836
1837         dt_write_lock(env, o, 0);
1838         if (!llog_osd_exist(loghandle))
1839                 GOTO(out_unlock, rc = 0);
1840
1841         if (loghandle->lgh_name) {
1842                 llog_dir = llog_osd_dir_get(env, ctxt);
1843                 if (IS_ERR(llog_dir))
1844                         GOTO(out_unlock, rc = PTR_ERR(llog_dir));
1845
1846                 dt_read_lock(env, llog_dir, 0);
1847                 rc = dt_delete(env, llog_dir,
1848                                (struct dt_key *)loghandle->lgh_name,
1849                                th);
1850                 dt_read_unlock(env, llog_dir);
1851                 if (rc) {
1852                         CERROR("%s: can't remove llog %s: rc = %d\n",
1853                                o->do_lu.lo_dev->ld_obd->obd_name,
1854                                loghandle->lgh_name, rc);
1855                         GOTO(out_unlock, rc);
1856                 }
1857         }
1858
1859         dt_ref_del(env, o, th);
1860         rc = dt_destroy(env, o, th);
1861         if (rc < 0)
1862                 GOTO(out_unlock, rc);
1863
1864         loghandle->lgh_destroyed = true;
1865         if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1866                 rc = llog_osd_regular_fid_del_name_entry(env, o, th, false);
1867                 if (rc < 0)
1868                         GOTO(out_unlock, rc);
1869         }
1870
1871 out_unlock:
1872         dt_write_unlock(env, o);
1873         if (!(IS_ERR_OR_NULL(llog_dir)))
1874                 dt_object_put(env, llog_dir);
1875         RETURN(rc);
1876 }
1877
1878 /**
1879  * Implementation of the llog_operations::lop_setup
1880  *
1881  * This function setup the llog on local storage.
1882  *
1883  * \param[in] env       execution environment
1884  * \param[in] obd       obd device the llog belongs to
1885  * \param[in] olg       the llog group, it is always zero group now.
1886  * \param[in] ctxt_idx  the llog index, it defines the purpose of this llog.
1887  *                      Every new llog type have to use own index.
1888  * \param[in] disk_obd  the storage obd, where llog is stored.
1889  *
1890  * \retval              0 on successful llog setup
1891  * \retval              negative value on error
1892  */
1893 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1894                           struct obd_llog_group *olg, int ctxt_idx,
1895                           struct obd_device *disk_obd)
1896 {
1897         struct llog_thread_info         *lgi = llog_info(env);
1898         struct llog_ctxt                *ctxt;
1899         int                              rc = 0;
1900         ENTRY;
1901
1902         LASSERT(obd);
1903         LASSERT(olg->olg_ctxts[ctxt_idx]);
1904
1905         ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1906         LASSERT(ctxt);
1907
1908         if (disk_obd == NULL)
1909                 GOTO(out, rc = 0);
1910
1911         /* initialize data allowing to generate new fids,
1912          * literally we need a sequece */
1913         lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1914         lgi->lgi_fid.f_oid = 1;
1915         lgi->lgi_fid.f_ver = 0;
1916         rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1917                                     &lgi->lgi_fid,
1918                                     &ctxt->loc_los_nameless);
1919         if (rc != 0)
1920                 GOTO(out, rc);
1921
1922         lgi->lgi_fid.f_seq = FID_SEQ_LLOG_NAME;
1923         lgi->lgi_fid.f_oid = 1;
1924         lgi->lgi_fid.f_ver = 0;
1925         rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1926                                     &lgi->lgi_fid,
1927                                     &ctxt->loc_los_named);
1928         if (rc != 0) {
1929                 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1930                 ctxt->loc_los_nameless = NULL;
1931         }
1932
1933         GOTO(out, rc);
1934
1935 out:
1936         llog_ctxt_put(ctxt);
1937         return rc;
1938 }
1939
1940 /**
1941  * Implementation of the llog_operations::lop_cleanup
1942  *
1943  * This function cleanups the llog on local storage.
1944  *
1945  * \param[in] env       execution environment
1946  * \param[in] ctxt      the llog context
1947  *
1948  * \retval              0
1949  */
1950 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1951 {
1952         if (ctxt->loc_los_nameless != NULL) {
1953                 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1954                 ctxt->loc_los_nameless = NULL;
1955         }
1956
1957         if (ctxt->loc_los_named != NULL) {
1958                 local_oid_storage_fini(env, ctxt->loc_los_named);
1959                 ctxt->loc_los_named = NULL;
1960         }
1961
1962         return 0;
1963 }
1964
1965 const struct llog_operations llog_osd_ops = {
1966         .lop_next_block         = llog_osd_next_block,
1967         .lop_prev_block         = llog_osd_prev_block,
1968         .lop_read_header        = llog_osd_read_header,
1969         .lop_declare_destroy    = llog_osd_declare_destroy,
1970         .lop_destroy            = llog_osd_destroy,
1971         .lop_setup              = llog_osd_setup,
1972         .lop_cleanup            = llog_osd_cleanup,
1973         .lop_open               = llog_osd_open,
1974         .lop_exist              = llog_osd_exist,
1975         .lop_declare_create     = llog_osd_declare_create,
1976         .lop_create             = llog_osd_create,
1977         .lop_declare_write_rec  = llog_osd_declare_write_rec,
1978         .lop_write_rec          = llog_osd_write_rec,
1979         .lop_close              = llog_osd_close,
1980 };
1981 EXPORT_SYMBOL(llog_osd_ops);
1982
1983 const struct llog_operations llog_common_cat_ops = {
1984         .lop_next_block         = llog_osd_next_block,
1985         .lop_prev_block         = llog_osd_prev_block,
1986         .lop_read_header        = llog_osd_read_header,
1987         .lop_declare_destroy    = llog_osd_declare_destroy,
1988         .lop_destroy            = llog_osd_destroy,
1989         .lop_setup              = llog_osd_setup,
1990         .lop_cleanup            = llog_osd_cleanup,
1991         .lop_open               = llog_osd_open,
1992         .lop_exist              = llog_osd_exist,
1993         .lop_declare_create     = llog_osd_declare_create,
1994         .lop_create             = llog_osd_create,
1995         .lop_declare_write_rec  = llog_osd_declare_write_rec,
1996         .lop_write_rec          = llog_osd_write_rec,
1997         .lop_close              = llog_osd_close,
1998         .lop_add                = llog_cat_add_rec,
1999         .lop_declare_add        = llog_cat_declare_add_rec,
2000 };
2001 EXPORT_SYMBOL(llog_common_cat_ops);
2002
2003 /**
2004  * Read the special file which contains the list of llog catalogs IDs
2005  *
2006  * This function reads the CATALOGS file which contains the array of llog
2007  * catalogs IDs. The main purpose of this file is to store OSP llogs indexed
2008  * by OST/MDT number.
2009  *
2010  * \param[in]  env              execution environment
2011  * \param[in]  d                corresponding storage device
2012  * \param[in]  idx              position to start from, usually OST/MDT index
2013  * \param[in]  count            how many catalog IDs to read
2014  * \param[out] idarray          the buffer for the data. If it is NULL then
2015  *                              function returns just number of catalog IDs
2016  *                              in the file.
2017  * \param[in]  fid              LLOG_CATALOGS_OID for CATALOG object
2018  *
2019  * \retval                      0 on successful read of catalog IDs
2020  * \retval                      negative value on error
2021  * \retval                      positive value which is number of records in
2022  *                              the file if \a idarray is NULL
2023  */
2024 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
2025                           int idx, int count, struct llog_catid *idarray,
2026                           const struct lu_fid *fid)
2027 {
2028         struct llog_thread_info *lgi = llog_info(env);
2029         struct dt_object        *o = NULL;
2030         struct thandle          *th;
2031         int                      rc, size;
2032
2033         ENTRY;
2034
2035         LASSERT(d);
2036
2037         size = sizeof(*idarray) * count;
2038         lgi->lgi_off = idx *  sizeof(*idarray);
2039
2040         lgi->lgi_fid = *fid;
2041         o = dt_locate(env, d, &lgi->lgi_fid);
2042         if (IS_ERR(o))
2043                 RETURN(PTR_ERR(o));
2044
2045         if (!dt_object_exists(o)) {
2046                 th = dt_trans_create(env, d);
2047                 if (IS_ERR(th))
2048                         GOTO(out, rc = PTR_ERR(th));
2049
2050                 lgi->lgi_attr.la_valid = LA_MODE | LA_TYPE;
2051                 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
2052                 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
2053
2054                 th->th_wait_submit = 1;
2055                 /* Make the llog object creation synchronization, so
2056                  * it will be reliable to the reference, especially
2057                  * for remote reference */
2058                 th->th_sync = 1;
2059
2060                 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
2061                                        &lgi->lgi_dof, th);
2062                 if (rc)
2063                         GOTO(out_trans, rc);
2064
2065                 rc = dt_trans_start_local(env, d, th);
2066                 if (rc)
2067                         GOTO(out_trans, rc);
2068
2069                 dt_write_lock(env, o, 0);
2070                 if (!dt_object_exists(o))
2071                         rc = dt_create(env, o, &lgi->lgi_attr, NULL,
2072                                        &lgi->lgi_dof, th);
2073                 dt_write_unlock(env, o);
2074 out_trans:
2075                 dt_trans_stop(env, d, th);
2076                 if (rc)
2077                         GOTO(out, rc);
2078         }
2079
2080         rc = dt_attr_get(env, o, &lgi->lgi_attr);
2081         if (rc)
2082                 GOTO(out, rc);
2083
2084         if (!S_ISREG(lgi->lgi_attr.la_mode)) {
2085                 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
2086                        o->do_lu.lo_dev->ld_obd->obd_name,
2087                        lgi->lgi_attr.la_mode);
2088                 GOTO(out, rc = -ENOENT);
2089         }
2090
2091         CDEBUG(D_CONFIG, "cat list: disk size=%d, read=%d\n",
2092                (int)lgi->lgi_attr.la_size, size);
2093
2094         /* return just number of llogs */
2095         if (idarray == NULL) {
2096                 rc = lgi->lgi_attr.la_size / sizeof(*idarray);
2097                 GOTO(out, rc);
2098         }
2099
2100         /* read for new ost index or for empty file */
2101         memset(idarray, 0, size);
2102         if (lgi->lgi_attr.la_size <= lgi->lgi_off)
2103                 GOTO(out, rc = 0);
2104         if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
2105                 size = lgi->lgi_attr.la_size - lgi->lgi_off;
2106
2107         lgi->lgi_buf.lb_buf = idarray;
2108         lgi->lgi_buf.lb_len = size;
2109         rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
2110         /* -EFAULT means the llog is a sparse file. This is not an error
2111          * after arbitrary OST index is supported. */
2112         if (rc < 0 && rc != -EFAULT) {
2113                 CERROR("%s: error reading CATALOGS: rc = %d\n",
2114                        o->do_lu.lo_dev->ld_obd->obd_name,  rc);
2115                 GOTO(out, rc);
2116         }
2117
2118         EXIT;
2119 out:
2120         dt_object_put(env, o);
2121         RETURN(rc);
2122 }
2123 EXPORT_SYMBOL(llog_osd_get_cat_list);
2124
2125 /**
2126  * Write the special file which contains the list of llog catalogs IDs
2127  *
2128  * This function writes the CATALOG file which contains the array of llog
2129  * catalogs IDs. It is used mostly to store OSP llogs indexed by OST/MDT
2130  * number.
2131  *
2132  * \param[in]  env      execution environment
2133  * \param[in]  d        corresponding storage device
2134  * \param[in]  idx      position to start from, usually OST/MDT index
2135  * \param[in]  count    how many catalog IDs to write
2136  * \param[out] idarray  the buffer with the data to write.
2137  * \param[in]  fid      LLOG_CATALOGS_OID for CATALOG object
2138  *
2139  * \retval              0 on successful write of catalog IDs
2140  * \retval              negative value on error
2141  */
2142 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
2143                           int idx, int count, struct llog_catid *idarray,
2144                           const struct lu_fid *fid)
2145 {
2146         struct llog_thread_info *lgi = llog_info(env);
2147         struct dt_object        *o = NULL;
2148         struct thandle          *th;
2149         int                      rc, size;
2150
2151         if (count == 0)
2152                 RETURN(0);
2153
2154         LASSERT(d);
2155
2156         size = sizeof(*idarray) * count;
2157         lgi->lgi_off = idx * sizeof(*idarray);
2158         lgi->lgi_fid = *fid;
2159
2160         o = dt_locate(env, d, &lgi->lgi_fid);
2161         if (IS_ERR(o))
2162                 RETURN(PTR_ERR(o));
2163
2164         if (!dt_object_exists(o))
2165                 GOTO(out, rc = -ENOENT);
2166
2167         rc = dt_attr_get(env, o, &lgi->lgi_attr);
2168         if (rc)
2169                 GOTO(out, rc);
2170
2171         if (!S_ISREG(lgi->lgi_attr.la_mode)) {
2172                 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
2173                        o->do_lu.lo_dev->ld_obd->obd_name,
2174                        lgi->lgi_attr.la_mode);
2175                 GOTO(out, rc = -ENOENT);
2176         }
2177
2178         th = dt_trans_create(env, d);
2179         if (IS_ERR(th))
2180                 GOTO(out, rc = PTR_ERR(th));
2181
2182         lgi->lgi_buf.lb_len = size;
2183         lgi->lgi_buf.lb_buf = idarray;
2184         rc = dt_declare_record_write(env, o, &lgi->lgi_buf, lgi->lgi_off, th);
2185         if (rc)
2186                 GOTO(out_trans, rc);
2187
2188         /* For update log, this happens during initialization,
2189          * see lod_sub_prep_llog(), and we need make sure catlog
2190          * file ID is written to catlist file(committed) before
2191          * cross-MDT operation write update records to catlog FILE,
2192          * otherwise, during failover these update records might
2193          * missing */
2194         if (fid_is_update_log(fid))
2195                 th->th_sync = 1;
2196
2197         rc = dt_trans_start_local(env, d, th);
2198         if (rc)
2199                 GOTO(out_trans, rc);
2200
2201         th->th_wait_submit = 1;
2202
2203         rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
2204         if (rc)
2205                 CDEBUG(D_INODE, "can't write CATALOGS at index %d: rc = %d\n",
2206                        idx, rc);
2207 out_trans:
2208         dt_trans_stop(env, d, th);
2209 out:
2210         dt_object_put(env, o);
2211         RETURN(rc);
2212 }
2213 EXPORT_SYMBOL(llog_osd_put_cat_list);