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