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