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