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