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