Whamcloud - gitweb
0a09fb530f7cc7b6e13c6ee5bb47450a879f8ecc
[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 off"LPU64"\n",
608                POSTID(&loghandle->lgh_id.lgl_oi), index, rec->lrh_len,
609                lgi->lgi_off);
610         if (reccookie != NULL) {
611                 reccookie->lgc_lgl = loghandle->lgh_id;
612                 reccookie->lgc_index = index;
613                 if ((rec->lrh_type == MDS_UNLINK_REC) ||
614                     (rec->lrh_type == MDS_SETATTR64_REC))
615                         reccookie->lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
616                 else if (rec->lrh_type == OST_SZ_REC)
617                         reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
618                 else
619                         reccookie->lgc_subsys = -1;
620                 rc = 1;
621         }
622         RETURN(rc);
623 out:
624         /* cleanup llog for error case */
625         down_write(&loghandle->lgh_hdr_lock);
626         ext2_clear_bit(index, LLOG_HDR_BITMAP(llh));
627         llh->llh_count--;
628         up_write(&loghandle->lgh_hdr_lock);
629
630         /* restore llog last_idx */
631         loghandle->lgh_last_idx--;
632         LLOG_HDR_TAIL(llh)->lrt_index = loghandle->lgh_last_idx;
633
634         RETURN(rc);
635 }
636
637 /**
638  * We can skip reading at least as many log blocks as the number of
639  * minimum sized log records we are skipping.  If it turns out
640  * that we are not far enough along the log (because the
641  * actual records are larger than minimum size) we just skip
642  * some more records.
643  */
644 static inline void llog_skip_over(__u64 *off, int curr, int goal,
645                                   __u32 chunk_size)
646 {
647         if (goal <= curr)
648                 return;
649         *off = (*off + (goal - curr - 1) * LLOG_MIN_REC_SIZE) &
650                 ~(chunk_size - 1);
651 }
652
653 /**
654  * Remove optional fields that the client doesn't expect.
655  * This is typically in order to ensure compatibility with older clients.
656  * It is assumed that since we exclusively remove fields, the block will be
657  * big enough to handle the remapped records. It is also assumed that records
658  * of a block have the same format (i.e.: the same features enabled).
659  *
660  * \param[in,out]    hdr        Header of the block of records to remap.
661  * \param[in,out]    last_hdr   Last header, don't read past this point.
662  * \param[in]        flags      Flags describing the fields to keep.
663  */
664 static void changelog_block_trim_ext(struct llog_rec_hdr *hdr,
665                                      struct llog_rec_hdr *last_hdr,
666                                      enum changelog_rec_flags flags)
667 {
668         if (hdr->lrh_type != CHANGELOG_REC)
669                 return;
670
671         do {
672                 struct changelog_rec *rec = (struct changelog_rec *)(hdr + 1);
673
674                 changelog_remap_rec(rec, rec->cr_flags & flags);
675                 hdr = llog_rec_hdr_next(hdr);
676         } while ((char *)hdr <= (char *)last_hdr);
677 }
678
679 /**
680  * Implementation of the llog_operations::lop_next_block
681  *
682  * This function finds the the next llog block to return which contains
683  * record with required index. It is main part of llog processing.
684  *
685  * \param[in]     env           execution environment
686  * \param[in]     loghandle     llog handle of the current llog
687  * \param[in,out] cur_idx       index preceeding cur_offset
688  * \param[in]     next_idx      target index to find
689  * \param[in,out] cur_offset    furtherst point read in the file
690  * \param[in]     buf           pointer to data buffer to fill
691  * \param[in]     len           required len to read, it is
692  *                              usually llog chunk_size.
693  *
694  * \retval                      0 on successful buffer read
695  * \retval                      negative value on error
696  */
697 static int llog_osd_next_block(const struct lu_env *env,
698                                struct llog_handle *loghandle, int *cur_idx,
699                                int next_idx, __u64 *cur_offset, void *buf,
700                                int len)
701 {
702         struct llog_thread_info *lgi = llog_info(env);
703         struct dt_object        *o;
704         struct dt_device        *dt;
705         int                      rc;
706         __u32                   chunk_size;
707
708         ENTRY;
709
710         LASSERT(env);
711         LASSERT(lgi);
712
713         chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
714         if (len == 0 || len & (chunk_size - 1))
715                 RETURN(-EINVAL);
716
717         CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off "LPU64")\n",
718                next_idx, *cur_idx, *cur_offset);
719
720         LASSERT(loghandle);
721         LASSERT(loghandle->lgh_ctxt);
722
723         o = loghandle->lgh_obj;
724         LASSERT(o);
725         LASSERT(dt_object_exists(o));
726         dt = lu2dt_dev(o->do_lu.lo_dev);
727         LASSERT(dt);
728
729         rc = dt_attr_get(env, o, &lgi->lgi_attr);
730         if (rc)
731                 GOTO(out, rc);
732
733         while (*cur_offset < lgi->lgi_attr.la_size) {
734                 struct llog_rec_hdr     *rec, *last_rec;
735                 struct llog_rec_tail    *tail;
736
737                 llog_skip_over(cur_offset, *cur_idx, next_idx, chunk_size);
738
739                 /* read up to next llog chunk_size block */
740                 lgi->lgi_buf.lb_len = chunk_size -
741                                       (*cur_offset & (chunk_size - 1));
742                 lgi->lgi_buf.lb_buf = buf;
743
744                 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
745                 if (rc < 0) {
746                         CERROR("%s: can't read llog block from log "DFID
747                                " offset "LPU64": rc = %d\n",
748                                o->do_lu.lo_dev->ld_obd->obd_name,
749                                PFID(lu_object_fid(&o->do_lu)), *cur_offset,
750                                rc);
751                         GOTO(out, rc);
752                 }
753
754                 if (rc < len) {
755                         /* signal the end of the valid buffer to
756                          * llog_process */
757                         memset(buf + rc, 0, len - rc);
758                 }
759
760                 if (rc == 0) /* end of file, nothing to do */
761                         GOTO(out, rc);
762
763                 if (rc < sizeof(*tail)) {
764                         CERROR("%s: invalid llog block at log id "DOSTID"/%u "
765                                "offset "LPU64"\n",
766                                o->do_lu.lo_dev->ld_obd->obd_name,
767                                POSTID(&loghandle->lgh_id.lgl_oi),
768                                loghandle->lgh_id.lgl_ogen, *cur_offset);
769                         GOTO(out, rc = -EINVAL);
770                 }
771
772                 rec = buf;
773                 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
774                         lustre_swab_llog_rec(rec);
775
776                 tail = (struct llog_rec_tail *)((char *)buf + rc -
777                                                 sizeof(struct llog_rec_tail));
778                 /* get the last record in block */
779                 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
780                                                    tail->lrt_len);
781
782                 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
783                         lustre_swab_llog_rec(last_rec);
784                 LASSERT(last_rec->lrh_index == tail->lrt_index);
785
786                 *cur_idx = tail->lrt_index;
787
788                 /* this shouldn't happen */
789                 if (tail->lrt_index == 0) {
790                         CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
791                                "offset "LPU64"\n",
792                                o->do_lu.lo_dev->ld_obd->obd_name,
793                                POSTID(&loghandle->lgh_id.lgl_oi),
794                                loghandle->lgh_id.lgl_ogen, *cur_offset);
795                         GOTO(out, rc = -EINVAL);
796                 }
797                 if (tail->lrt_index < next_idx)
798                         continue;
799
800                 /* sanity check that the start of the new buffer is no farther
801                  * than the record that we wanted.  This shouldn't happen. */
802                 if (rec->lrh_index > next_idx) {
803                         CERROR("%s: missed desired record? %u > %u\n",
804                                o->do_lu.lo_dev->ld_obd->obd_name,
805                                rec->lrh_index, next_idx);
806                         GOTO(out, rc = -ENOENT);
807                 }
808
809                 /* Trim unsupported extensions for compat w/ older clients */
810                 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_JOBID))
811                         changelog_block_trim_ext(rec, last_rec,
812                                                  CLF_VERSION | CLF_RENAME);
813
814                 GOTO(out, rc = 0);
815         }
816         GOTO(out, rc = -EIO);
817 out:
818         return rc;
819 }
820
821 /**
822  * Implementation of the llog_operations::lop_prev_block
823  *
824  * This function finds the llog block to return which contains
825  * record with required index but in reverse order - from end of llog
826  * to the beginning.
827  * It is main part of reverse llog processing.
828  *
829  * \param[in] env       execution environment
830  * \param[in] loghandle llog handle of the current llog
831  * \param[in] prev_idx  target index to find
832  * \param[in] buf       pointer to data buffer to fill
833  * \param[in] len       required len to read, it is llog_chunk_size usually.
834  *
835  * \retval              0 on successful buffer read
836  * \retval              negative value on error
837  */
838 static int llog_osd_prev_block(const struct lu_env *env,
839                                struct llog_handle *loghandle,
840                                int prev_idx, void *buf, int len)
841 {
842         struct llog_thread_info *lgi = llog_info(env);
843         struct dt_object        *o;
844         struct dt_device        *dt;
845         loff_t                   cur_offset;
846         __u32                   chunk_size;
847         int                      rc;
848
849         ENTRY;
850
851         chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
852         if (len == 0 || len & (chunk_size - 1))
853                 RETURN(-EINVAL);
854
855         CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
856
857         LASSERT(loghandle);
858         LASSERT(loghandle->lgh_ctxt);
859
860         o = loghandle->lgh_obj;
861         LASSERT(o);
862         LASSERT(dt_object_exists(o));
863         dt = lu2dt_dev(o->do_lu.lo_dev);
864         LASSERT(dt);
865
866         cur_offset = chunk_size;
867         llog_skip_over(&cur_offset, 0, prev_idx, chunk_size);
868
869         rc = dt_attr_get(env, o, &lgi->lgi_attr);
870         if (rc)
871                 GOTO(out, rc);
872
873         while (cur_offset < lgi->lgi_attr.la_size) {
874                 struct llog_rec_hdr     *rec, *last_rec;
875                 struct llog_rec_tail    *tail;
876
877                 lgi->lgi_buf.lb_len = len;
878                 lgi->lgi_buf.lb_buf = buf;
879                 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
880                 if (rc < 0) {
881                         CERROR("%s: can't read llog block from log "DFID
882                                " offset "LPU64": rc = %d\n",
883                                o->do_lu.lo_dev->ld_obd->obd_name,
884                                PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
885                         GOTO(out, rc);
886                 }
887
888                 if (rc == 0) /* end of file, nothing to do */
889                         GOTO(out, rc);
890
891                 if (rc < sizeof(*tail)) {
892                         CERROR("%s: invalid llog block at log id "DOSTID"/%u "
893                                "offset "LPU64"\n",
894                                o->do_lu.lo_dev->ld_obd->obd_name,
895                                POSTID(&loghandle->lgh_id.lgl_oi),
896                                loghandle->lgh_id.lgl_ogen, cur_offset);
897                         GOTO(out, rc = -EINVAL);
898                 }
899
900                 rec = buf;
901                 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
902                         lustre_swab_llog_rec(rec);
903
904                 tail = (struct llog_rec_tail *)((char *)buf + rc -
905                                                 sizeof(struct llog_rec_tail));
906                 /* get the last record in block */
907                 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
908                                                    le32_to_cpu(tail->lrt_len));
909
910                 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
911                         lustre_swab_llog_rec(last_rec);
912                 LASSERT(last_rec->lrh_index == tail->lrt_index);
913
914                 /* this shouldn't happen */
915                 if (tail->lrt_index == 0) {
916                         CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
917                                "offset "LPU64"\n",
918                                o->do_lu.lo_dev->ld_obd->obd_name,
919                                POSTID(&loghandle->lgh_id.lgl_oi),
920                                loghandle->lgh_id.lgl_ogen, cur_offset);
921                         GOTO(out, rc = -EINVAL);
922                 }
923                 if (tail->lrt_index < prev_idx)
924                         continue;
925
926                 /* sanity check that the start of the new buffer is no farther
927                  * than the record that we wanted.  This shouldn't happen. */
928                 if (rec->lrh_index > prev_idx) {
929                         CERROR("%s: missed desired record? %u > %u\n",
930                                o->do_lu.lo_dev->ld_obd->obd_name,
931                                rec->lrh_index, prev_idx);
932                         GOTO(out, rc = -ENOENT);
933                 }
934
935                 /* Trim unsupported extensions for compat w/ older clients */
936                 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_JOBID))
937                         changelog_block_trim_ext(rec, last_rec,
938                                                  CLF_VERSION | CLF_RENAME);
939
940                 GOTO(out, rc = 0);
941         }
942         GOTO(out, rc = -EIO);
943 out:
944         return rc;
945 }
946
947 /**
948  * This is helper function to get llog directory object. It is used by named
949  * llog operations to find/insert/delete llog entry from llog directory.
950  *
951  * \param[in] env       execution environment
952  * \param[in] ctxt      llog context
953  *
954  * \retval              dt_object of llog directory
955  * \retval              ERR_PTR of negative value on error
956  */
957 static struct dt_object *llog_osd_dir_get(const struct lu_env *env,
958                                           struct llog_ctxt *ctxt)
959 {
960         struct dt_device        *dt;
961         struct dt_thread_info   *dti = dt_info(env);
962         struct dt_object        *dir;
963         int                      rc;
964
965         dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
966         if (ctxt->loc_dir == NULL) {
967                 rc = dt_root_get(env, dt, &dti->dti_fid);
968                 if (rc)
969                         return ERR_PTR(rc);
970                 dir = dt_locate(env, dt, &dti->dti_fid);
971
972                 if (!IS_ERR(dir) && !dt_try_as_dir(env, dir)) {
973                         lu_object_put(env, &dir->do_lu);
974                         return ERR_PTR(-ENOTDIR);
975                 }
976         } else {
977                 lu_object_get(&ctxt->loc_dir->do_lu);
978                 dir = ctxt->loc_dir;
979         }
980
981         return dir;
982 }
983
984 /**
985  * Implementation of the llog_operations::lop_open
986  *
987  * This function opens the llog by its logid or by name, it may open also
988  * non existent llog and assing then new id to it.
989  * The llog_open/llog_close pair works similar to lu_object_find/put,
990  * the object may not exist prior open. The result of open is just dt_object
991  * in the llog header.
992  *
993  * \param[in] env               execution environment
994  * \param[in] handle            llog handle of the current llog
995  * \param[in] logid             logid of llog to open (nameless llog)
996  * \param[in] name              name of llog to open (named llog)
997  * \param[in] open_param
998  *                              LLOG_OPEN_NEW - new llog, may not exist
999  *                              LLOG_OPEN_EXIST - old llog, must exist
1000  *
1001  * \retval                      0 on successful open, llog_handle::lgh_obj
1002  *                              contains the dt_object of the llog.
1003  * \retval                      negative value on error
1004  */
1005 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
1006                          struct llog_logid *logid, char *name,
1007                          enum llog_open_param open_param)
1008 {
1009         struct llog_thread_info         *lgi = llog_info(env);
1010         struct llog_ctxt                *ctxt = handle->lgh_ctxt;
1011         struct dt_object                *o;
1012         struct dt_device                *dt;
1013         struct ls_device                *ls;
1014         struct local_oid_storage        *los = NULL;
1015         int                              rc = 0;
1016
1017         ENTRY;
1018
1019         LASSERT(env);
1020         LASSERT(ctxt);
1021         LASSERT(ctxt->loc_exp);
1022         LASSERT(ctxt->loc_exp->exp_obd);
1023         dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1024         LASSERT(dt);
1025         if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1026                 if (logid != NULL) {
1027                         logid_to_fid(logid, &lgi->lgi_fid);
1028                 } else {
1029                         /* If logid == NULL, then it means the caller needs
1030                          * to allocate new FID (llog_cat_declare_add_rec()). */
1031                         rc = obd_fid_alloc(env, ctxt->loc_exp,
1032                                            &lgi->lgi_fid, NULL);
1033                         if (rc < 0)
1034                                 RETURN(rc);
1035                         rc = 0;
1036                 }
1037
1038                 o = dt_locate(env, dt, &lgi->lgi_fid);
1039                 if (IS_ERR(o))
1040                         RETURN(PTR_ERR(o));
1041
1042                 goto after_open;
1043         }
1044
1045         ls = ls_device_get(dt);
1046         if (IS_ERR(ls))
1047                 RETURN(PTR_ERR(ls));
1048
1049         mutex_lock(&ls->ls_los_mutex);
1050         los = dt_los_find(ls, name != NULL ? FID_SEQ_LLOG_NAME : FID_SEQ_LLOG);
1051         mutex_unlock(&ls->ls_los_mutex);
1052         LASSERT(los);
1053         ls_device_put(env, ls);
1054
1055         LASSERT(handle);
1056
1057         if (logid != NULL) {
1058                 logid_to_fid(logid, &lgi->lgi_fid);
1059         } else if (name) {
1060                 struct dt_object *llog_dir;
1061
1062                 llog_dir = llog_osd_dir_get(env, ctxt);
1063                 if (IS_ERR(llog_dir))
1064                         GOTO(out, rc = PTR_ERR(llog_dir));
1065                 dt_read_lock(env, llog_dir, 0);
1066                 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
1067                 dt_read_unlock(env, llog_dir);
1068                 lu_object_put(env, &llog_dir->do_lu);
1069                 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
1070                         /* generate fid for new llog */
1071                         rc = local_object_fid_generate(env, los,
1072                                                        &lgi->lgi_fid);
1073                 }
1074                 if (rc < 0)
1075                         GOTO(out, rc);
1076                 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
1077                 if (handle->lgh_name)
1078                         strcpy(handle->lgh_name, name);
1079                 else
1080                         GOTO(out, rc = -ENOMEM);
1081         } else {
1082                 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
1083                 /* generate fid for new llog */
1084                 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
1085                 if (rc < 0)
1086                         GOTO(out, rc);
1087         }
1088
1089         o = ls_locate(env, ls, &lgi->lgi_fid, NULL);
1090         if (IS_ERR(o))
1091                 GOTO(out_name, rc = PTR_ERR(o));
1092
1093 after_open:
1094         /* No new llog is expected but doesn't exist */
1095         if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o))
1096                 GOTO(out_put, rc = -ENOENT);
1097
1098         fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
1099         handle->lgh_obj = o;
1100         handle->private_data = los;
1101         LASSERT(handle->lgh_ctxt);
1102
1103         RETURN(rc);
1104
1105 out_put:
1106         lu_object_put(env, &o->do_lu);
1107 out_name:
1108         if (handle->lgh_name != NULL)
1109                 OBD_FREE(handle->lgh_name, strlen(name) + 1);
1110 out:
1111         if (los != NULL)
1112                 dt_los_put(los);
1113         RETURN(rc);
1114 }
1115
1116 /**
1117  * Implementation of the llog_operations::lop_exist
1118  *
1119  * This function checks that llog exists on storage.
1120  *
1121  * \param[in] handle    llog handle of the current llog
1122  *
1123  * \retval              true if llog object exists and is not just destroyed
1124  * \retval              false if llog doesn't exist or just destroyed
1125  */
1126 static int llog_osd_exist(struct llog_handle *handle)
1127 {
1128         LASSERT(handle->lgh_obj);
1129         return (dt_object_exists(handle->lgh_obj) &&
1130                 !lu_object_is_dying(handle->lgh_obj->do_lu.lo_header));
1131 }
1132
1133 /**
1134  * Get dir for regular fid log object
1135  *
1136  * Get directory for regular fid log object, and these regular fid log
1137  * object will be inserted under this directory, to satisfy the FS
1138  * consistency check, e2fsck etc.
1139  *
1140  * \param [in] env      execution environment
1141  * \param [in] dto      llog object
1142  *
1143  * \retval              pointer to the directory if it is found.
1144  * \retval              ERR_PTR(negative errno) if it fails.
1145  */
1146 struct dt_object *llog_osd_get_regular_fid_dir(const struct lu_env *env,
1147                                                struct dt_object *dto)
1148 {
1149         struct llog_thread_info *lgi = llog_info(env);
1150         struct seq_server_site *ss = dto->do_lu.lo_dev->ld_site->ld_seq_site;
1151         struct lu_seq_range     *range = &lgi->lgi_range;
1152         struct lu_fid           *dir_fid = &lgi->lgi_fid;
1153         struct dt_object        *dir;
1154         int                     rc;
1155         ENTRY;
1156
1157         fld_range_set_any(range);
1158         LASSERT(ss != NULL);
1159         rc = ss->ss_server_fld->lsf_seq_lookup(env, ss->ss_server_fld,
1160                                    fid_seq(lu_object_fid(&dto->do_lu)), range);
1161         if (rc < 0)
1162                 RETURN(ERR_PTR(rc));
1163
1164         lu_update_log_dir_fid(dir_fid, range->lsr_index);
1165         dir = dt_locate(env, lu2dt_dev(dto->do_lu.lo_dev), dir_fid);
1166         if (IS_ERR(dir))
1167                 RETURN(dir);
1168
1169         if (!dt_try_as_dir(env, dir)) {
1170                 lu_object_put(env, &dir->do_lu);
1171                 RETURN(ERR_PTR(-ENOTDIR));
1172         }
1173
1174         RETURN(dir);
1175 }
1176
1177 /**
1178  * Add llog object with regular FID to name entry
1179  *
1180  * Add llog object with regular FID to name space, and each llog
1181  * object on each MDT will be /update_log_dir/[seq:oid:ver],
1182  * so to satisfy the namespace consistency check, e2fsck etc.
1183  *
1184  * \param [in] env      execution environment
1185  * \param [in] dto      llog object
1186  * \param [in] th       thandle
1187  * \param [in] declare  if it is declare or execution
1188  *
1189  * \retval              0 if insertion succeeds.
1190  * \retval              negative errno if insertion fails.
1191  */
1192 static int
1193 llog_osd_regular_fid_add_name_entry(const struct lu_env *env,
1194                                     struct dt_object *dto,
1195                                     struct thandle *th, bool declare)
1196 {
1197         struct llog_thread_info *lgi = llog_info(env);
1198         const struct lu_fid     *fid = lu_object_fid(&dto->do_lu);
1199         struct dt_insert_rec    *rec = &lgi->lgi_dt_rec;
1200         struct dt_object        *dir;
1201         char                    *name = lgi->lgi_name;
1202         int                     rc;
1203         ENTRY;
1204
1205         if (!fid_is_norm(fid))
1206                 RETURN(0);
1207
1208         dir = llog_osd_get_regular_fid_dir(env, dto);
1209         if (IS_ERR(dir))
1210                 RETURN(PTR_ERR(dir));
1211
1212         rec->rec_fid = fid;
1213         rec->rec_type = S_IFREG;
1214         snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1215         dt_write_lock(env, dir, 0);
1216         if (declare) {
1217                 rc = dt_declare_insert(env, dir, (struct dt_rec *)rec,
1218                                (struct dt_key *)name, th);
1219         } else {
1220                 rc = dt_insert(env, dir, (struct dt_rec *)rec,
1221                                (struct dt_key *)name, th, 1);
1222         }
1223         dt_write_unlock(env, dir);
1224
1225         lu_object_put(env, &dir->do_lu);
1226         RETURN(rc);
1227 }
1228
1229
1230 /**
1231  * Implementation of the llog_operations::lop_declare_create
1232  *
1233  * This function declares the llog create. It declares also name insert
1234  * into llog directory in case of named llog.
1235  *
1236  * \param[in] env       execution environment
1237  * \param[in] res       llog handle of the current llog
1238  * \param[in] th        current transaction handle
1239  *
1240  * \retval              0 on successful create declaration
1241  * \retval              negative value on error
1242  */
1243 static int llog_osd_declare_create(const struct lu_env *env,
1244                                    struct llog_handle *res, struct thandle *th)
1245 {
1246         struct llog_thread_info         *lgi = llog_info(env);
1247         struct dt_insert_rec            *rec = &lgi->lgi_dt_rec;
1248         struct local_oid_storage        *los;
1249         struct dt_object                *o;
1250         int                              rc;
1251
1252         ENTRY;
1253
1254         LASSERT(res->lgh_obj);
1255         LASSERT(th);
1256
1257         /* object can be created by another thread */
1258         o = res->lgh_obj;
1259         if (dt_object_exists(o))
1260                 RETURN(0);
1261
1262         if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1263                 struct llog_thread_info *lgi = llog_info(env);
1264
1265                 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE;
1266                 lgi->lgi_attr.la_size = 0;
1267                 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1268                 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1269
1270                 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1271                                        &lgi->lgi_dof, th);
1272                 if (rc < 0)
1273                         RETURN(rc);
1274
1275
1276                 rc = llog_osd_regular_fid_add_name_entry(env, o, th, true);
1277
1278                 RETURN(rc);
1279         }
1280         los = res->private_data;
1281         LASSERT(los);
1282
1283         rc = llog_osd_declare_new_object(env, los, o, th);
1284         if (rc)
1285                 RETURN(rc);
1286
1287         /* do not declare header initialization here as it's declared
1288          * in llog_osd_declare_write_rec() which is always called */
1289
1290         if (res->lgh_name) {
1291                 struct dt_object *llog_dir;
1292
1293                 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1294                 if (IS_ERR(llog_dir))
1295                         RETURN(PTR_ERR(llog_dir));
1296                 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1297                 rec->rec_fid = &lgi->lgi_fid;
1298                 rec->rec_type = S_IFREG;
1299                 rc = dt_declare_insert(env, llog_dir,
1300                                        (struct dt_rec *)rec,
1301                                        (struct dt_key *)res->lgh_name, th);
1302                 lu_object_put(env, &llog_dir->do_lu);
1303                 if (rc)
1304                         CERROR("%s: can't declare named llog %s: rc = %d\n",
1305                                o->do_lu.lo_dev->ld_obd->obd_name,
1306                                res->lgh_name, rc);
1307         }
1308         RETURN(rc);
1309 }
1310
1311 /**
1312  * Implementation of the llog_operations::lop_create
1313  *
1314  * This function creates the llog according with llog_handle::lgh_obj
1315  * and llog_handle::lgh_name.
1316  *
1317  * \param[in] env       execution environment
1318  * \param[in] res       llog handle of the current llog
1319  * \param[in] th        current transaction handle
1320  *
1321  * \retval              0 on successful create
1322  * \retval              negative value on error
1323  */
1324 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
1325                            struct thandle *th)
1326 {
1327         struct llog_thread_info *lgi = llog_info(env);
1328         struct dt_insert_rec    *rec = &lgi->lgi_dt_rec;
1329         struct local_oid_storage *los;
1330         struct dt_object        *o;
1331         int                      rc = 0;
1332
1333         ENTRY;
1334
1335         LASSERT(env);
1336         o = res->lgh_obj;
1337         LASSERT(o);
1338
1339         /* llog can be already created */
1340         if (dt_object_exists(o))
1341                 RETURN(-EEXIST);
1342
1343         if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1344                 struct llog_thread_info *lgi = llog_info(env);
1345
1346                 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE | LA_TYPE;
1347                 lgi->lgi_attr.la_size = 0;
1348                 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1349                 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1350
1351                 dt_write_lock(env, o, 0);
1352                 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1353                                &lgi->lgi_dof, th);
1354                 dt_write_unlock(env, o);
1355                 if (rc < 0)
1356                         RETURN(rc);
1357
1358                 rc = llog_osd_regular_fid_add_name_entry(env, o, th, false);
1359
1360                 RETURN(rc);
1361         }
1362
1363         los = res->private_data;
1364         LASSERT(los);
1365
1366         dt_write_lock(env, o, 0);
1367         if (!dt_object_exists(o))
1368                 rc = llog_osd_create_new_object(env, los, o, th);
1369         else
1370                 rc = -EEXIST;
1371
1372         dt_write_unlock(env, o);
1373         if (rc)
1374                 RETURN(rc);
1375
1376         if (res->lgh_name) {
1377                 struct dt_object *llog_dir;
1378
1379                 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1380                 if (IS_ERR(llog_dir))
1381                         RETURN(PTR_ERR(llog_dir));
1382
1383                 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1384                 rec->rec_fid = &lgi->lgi_fid;
1385                 rec->rec_type = S_IFREG;
1386                 dt_read_lock(env, llog_dir, 0);
1387                 rc = dt_insert(env, llog_dir, (struct dt_rec *)rec,
1388                                (struct dt_key *)res->lgh_name,
1389                                th, 1);
1390                 dt_read_unlock(env, llog_dir);
1391                 lu_object_put(env, &llog_dir->do_lu);
1392                 if (rc)
1393                         CERROR("%s: can't create named llog %s: rc = %d\n",
1394                                o->do_lu.lo_dev->ld_obd->obd_name,
1395                                res->lgh_name, rc);
1396         }
1397         RETURN(rc);
1398 }
1399
1400 /**
1401  * Implementation of the llog_operations::lop_close
1402  *
1403  * This function closes the llog. It just put llog object and referenced
1404  * local storage.
1405  *
1406  * \param[in] env       execution environment
1407  * \param[in] handle    llog handle of the current llog
1408  *
1409  * \retval              0 on successful llog close
1410  * \retval              negative value on error
1411  */
1412 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
1413 {
1414         struct local_oid_storage        *los;
1415         int                              rc = 0;
1416
1417         ENTRY;
1418
1419         LASSERT(handle->lgh_obj);
1420
1421         if (handle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1422                 /* Remove the object from the cache, otherwise it may
1423                  * hold LOD being released during cleanup process */
1424                 lu_object_put_nocache(env, &handle->lgh_obj->do_lu);
1425                 LASSERT(handle->private_data == NULL);
1426                 RETURN(rc);
1427         } else {
1428                 lu_object_put(env, &handle->lgh_obj->do_lu);
1429         }
1430         los = handle->private_data;
1431         LASSERT(los);
1432         dt_los_put(los);
1433
1434         if (handle->lgh_name)
1435                 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
1436
1437         RETURN(rc);
1438 }
1439
1440 /**
1441  * delete llog object name entry
1442  *
1443  * Delete llog object (with regular FID) from name space (under
1444  * update_log_dir).
1445  *
1446  * \param [in] env      execution environment
1447  * \param [in] dto      llog object
1448  * \param [in] th       thandle
1449  * \param [in] declare  if it is declare or execution
1450  *
1451  * \retval              0 if deletion succeeds.
1452  * \retval              negative errno if deletion fails.
1453  */
1454 static int
1455 llog_osd_regular_fid_del_name_entry(const struct lu_env *env,
1456                                     struct dt_object *dto,
1457                                     struct thandle *th, bool declare)
1458 {
1459         struct llog_thread_info *lgi = llog_info(env);
1460         const struct lu_fid     *fid = lu_object_fid(&dto->do_lu);
1461         struct dt_object        *dir;
1462         char                    *name = lgi->lgi_name;
1463         int                     rc;
1464         ENTRY;
1465
1466         if (!fid_is_norm(fid))
1467                 RETURN(0);
1468
1469         dir = llog_osd_get_regular_fid_dir(env, dto);
1470         if (IS_ERR(dir))
1471                 RETURN(PTR_ERR(dir));
1472
1473         snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1474         dt_write_lock(env, dir, 0);
1475         if (declare) {
1476                 rc = dt_declare_delete(env, dir, (struct dt_key *)name,
1477                                        th);
1478         } else {
1479                 rc = dt_delete(env, dir, (struct dt_key *)name, th);
1480         }
1481         dt_write_unlock(env, dir);
1482
1483         lu_object_put(env, &dir->do_lu);
1484         RETURN(rc);
1485 }
1486
1487
1488 /**
1489  * Implementation of the llog_operations::lop_destroy
1490  *
1491  * This function destroys the llog and deletes also entry in the
1492  * llog directory in case of named llog. Llog should be opened prior that.
1493  * Destroy method is not part of external transaction and does everything
1494  * inside.
1495  *
1496  * \param[in] env               execution environment
1497  * \param[in] loghandle llog handle of the current llog
1498  *
1499  * \retval              0 on successful destroy
1500  * \retval              negative value on error
1501  */
1502 static int llog_osd_destroy(const struct lu_env *env,
1503                             struct llog_handle *loghandle)
1504 {
1505         struct llog_ctxt        *ctxt;
1506         struct dt_object        *o, *llog_dir = NULL;
1507         struct dt_device        *d;
1508         struct thandle          *th;
1509         char                    *name = NULL;
1510         int                      rc;
1511
1512         ENTRY;
1513
1514         ctxt = loghandle->lgh_ctxt;
1515         LASSERT(ctxt);
1516
1517         o = loghandle->lgh_obj;
1518         LASSERT(o);
1519
1520         d = lu2dt_dev(o->do_lu.lo_dev);
1521         LASSERT(d);
1522         LASSERT(d == ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt);
1523
1524         th = dt_trans_create(env, d);
1525         if (IS_ERR(th))
1526                 RETURN(PTR_ERR(th));
1527
1528         if (loghandle->lgh_name) {
1529                 llog_dir = llog_osd_dir_get(env, ctxt);
1530                 if (IS_ERR(llog_dir))
1531                         GOTO(out_trans, rc = PTR_ERR(llog_dir));
1532
1533                 name = loghandle->lgh_name;
1534                 rc = dt_declare_delete(env, llog_dir,
1535                                        (struct dt_key *)name, th);
1536                 if (rc)
1537                         GOTO(out_trans, rc);
1538         }
1539
1540         rc = dt_declare_ref_del(env, o, th);
1541         if (rc < 0)
1542                 GOTO(out_trans, rc);
1543
1544         rc = dt_declare_destroy(env, o, th);
1545         if (rc)
1546                 GOTO(out_trans, rc);
1547
1548         if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1549                 rc = llog_osd_regular_fid_del_name_entry(env, o, th, true);
1550                 if (rc < 0)
1551                         GOTO(out_trans, rc);
1552         }
1553
1554         rc = dt_trans_start_local(env, d, th);
1555         if (rc)
1556                 GOTO(out_trans, rc);
1557
1558         th->th_wait_submit = 1;
1559
1560         dt_write_lock(env, o, 0);
1561         if (dt_object_exists(o)) {
1562                 if (name) {
1563                         dt_read_lock(env, llog_dir, 0);
1564                         rc = dt_delete(env, llog_dir,
1565                                        (struct dt_key *) name,
1566                                        th);
1567                         dt_read_unlock(env, llog_dir);
1568                         if (rc) {
1569                                 CERROR("%s: can't remove llog %s: rc = %d\n",
1570                                        o->do_lu.lo_dev->ld_obd->obd_name,
1571                                        name, rc);
1572                                 GOTO(out_unlock, rc);
1573                         }
1574                 }
1575                 dt_ref_del(env, o, th);
1576                 rc = dt_destroy(env, o, th);
1577                 if (rc)
1578                         GOTO(out_unlock, rc);
1579
1580                 if (loghandle->lgh_ctxt->loc_flags &
1581                                                 LLOG_CTXT_FLAG_NORMAL_FID) {
1582                         rc = llog_osd_regular_fid_del_name_entry(env, o, th,
1583                                                                  false);
1584                         if (rc < 0)
1585                                 GOTO(out_unlock, rc);
1586                 }
1587         }
1588 out_unlock:
1589         dt_write_unlock(env, o);
1590 out_trans:
1591         dt_trans_stop(env, d, th);
1592         if (!(IS_ERR_OR_NULL(llog_dir)))
1593                 lu_object_put(env, &llog_dir->do_lu);
1594         RETURN(rc);
1595 }
1596
1597 /**
1598  * Implementation of the llog_operations::lop_setup
1599  *
1600  * This function setup the llog on local storage.
1601  *
1602  * \param[in] env       execution environment
1603  * \param[in] obd       obd device the llog belongs to
1604  * \param[in] olg       the llog group, it is always zero group now.
1605  * \param[in] ctxt_idx  the llog index, it defines the purpose of this llog.
1606  *                      Every new llog type have to use own index.
1607  * \param[in] disk_obd  the storage obd, where llog is stored.
1608  *
1609  * \retval              0 on successful llog setup
1610  * \retval              negative value on error
1611  */
1612 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1613                           struct obd_llog_group *olg, int ctxt_idx,
1614                           struct obd_device *disk_obd)
1615 {
1616         struct llog_thread_info         *lgi = llog_info(env);
1617         struct llog_ctxt                *ctxt;
1618         int                              rc = 0;
1619         ENTRY;
1620
1621         LASSERT(obd);
1622         LASSERT(olg->olg_ctxts[ctxt_idx]);
1623
1624         ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1625         LASSERT(ctxt);
1626
1627         if (disk_obd == NULL)
1628                 GOTO(out, rc = 0);
1629
1630         /* initialize data allowing to generate new fids,
1631          * literally we need a sequece */
1632         lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1633         lgi->lgi_fid.f_oid = 1;
1634         lgi->lgi_fid.f_ver = 0;
1635         rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1636                                     &lgi->lgi_fid,
1637                                     &ctxt->loc_los_nameless);
1638         if (rc != 0)
1639                 GOTO(out, rc);
1640
1641         lgi->lgi_fid.f_seq = FID_SEQ_LLOG_NAME;
1642         lgi->lgi_fid.f_oid = 1;
1643         lgi->lgi_fid.f_ver = 0;
1644         rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1645                                     &lgi->lgi_fid,
1646                                     &ctxt->loc_los_named);
1647         if (rc != 0) {
1648                 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1649                 ctxt->loc_los_nameless = NULL;
1650         }
1651
1652         GOTO(out, rc);
1653
1654 out:
1655         llog_ctxt_put(ctxt);
1656         return rc;
1657 }
1658
1659 /**
1660  * Implementation of the llog_operations::lop_cleanup
1661  *
1662  * This function cleanups the llog on local storage.
1663  *
1664  * \param[in] env       execution environment
1665  * \param[in] ctxt      the llog context
1666  *
1667  * \retval              0
1668  */
1669 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1670 {
1671         if (ctxt->loc_los_nameless != NULL) {
1672                 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1673                 ctxt->loc_los_nameless = NULL;
1674         }
1675
1676         if (ctxt->loc_los_named != NULL) {
1677                 local_oid_storage_fini(env, ctxt->loc_los_named);
1678                 ctxt->loc_los_named = NULL;
1679         }
1680
1681         return 0;
1682 }
1683
1684 struct llog_operations llog_osd_ops = {
1685         .lop_next_block         = llog_osd_next_block,
1686         .lop_prev_block         = llog_osd_prev_block,
1687         .lop_read_header        = llog_osd_read_header,
1688         .lop_destroy            = llog_osd_destroy,
1689         .lop_setup              = llog_osd_setup,
1690         .lop_cleanup            = llog_osd_cleanup,
1691         .lop_open               = llog_osd_open,
1692         .lop_exist              = llog_osd_exist,
1693         .lop_declare_create     = llog_osd_declare_create,
1694         .lop_create             = llog_osd_create,
1695         .lop_declare_write_rec  = llog_osd_declare_write_rec,
1696         .lop_write_rec          = llog_osd_write_rec,
1697         .lop_close              = llog_osd_close,
1698 };
1699 EXPORT_SYMBOL(llog_osd_ops);
1700
1701 struct llog_operations llog_common_cat_ops = {
1702         .lop_next_block         = llog_osd_next_block,
1703         .lop_prev_block         = llog_osd_prev_block,
1704         .lop_read_header        = llog_osd_read_header,
1705         .lop_destroy            = llog_osd_destroy,
1706         .lop_setup              = llog_osd_setup,
1707         .lop_cleanup            = llog_osd_cleanup,
1708         .lop_open               = llog_osd_open,
1709         .lop_exist              = llog_osd_exist,
1710         .lop_declare_create     = llog_osd_declare_create,
1711         .lop_create             = llog_osd_create,
1712         .lop_declare_write_rec  = llog_osd_declare_write_rec,
1713         .lop_write_rec          = llog_osd_write_rec,
1714         .lop_close              = llog_osd_close,
1715         .lop_add                = llog_cat_add_rec,
1716         .lop_declare_add        = llog_cat_declare_add_rec,
1717 };
1718 EXPORT_SYMBOL(llog_common_cat_ops);
1719
1720 /**
1721  * Read the special file which contains the list of llog catalogs IDs
1722  *
1723  * This function reads the CATALOGS file which contains the array of llog
1724  * catalogs IDs. The main purpose of this file is to store OSP llogs indexed
1725  * by OST/MDT number.
1726  *
1727  * \param[in]  env              execution environment
1728  * \param[in]  d                corresponding storage device
1729  * \param[in]  idx              position to start from, usually OST/MDT index
1730  * \param[in]  count            how many catalog IDs to read
1731  * \param[out] idarray          the buffer for the data. If it is NULL then
1732  *                              function returns just number of catalog IDs
1733  *                              in the file.
1734  * \param[in]  fid              LLOG_CATALOGS_OID for CATALOG object
1735  *
1736  * \retval                      0 on successful read of catalog IDs
1737  * \retval                      negative value on error
1738  * \retval                      positive value which is number of records in
1739  *                              the file if \a idarray is NULL
1740  */
1741 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
1742                           int idx, int count, struct llog_catid *idarray,
1743                           const struct lu_fid *fid)
1744 {
1745         struct llog_thread_info *lgi = llog_info(env);
1746         struct dt_object        *o = NULL;
1747         struct thandle          *th;
1748         int                      rc, size;
1749
1750         ENTRY;
1751
1752         LASSERT(d);
1753
1754         size = sizeof(*idarray) * count;
1755         lgi->lgi_off = idx *  sizeof(*idarray);
1756
1757         lgi->lgi_fid = *fid;
1758         o = dt_locate(env, d, &lgi->lgi_fid);
1759         if (IS_ERR(o))
1760                 RETURN(PTR_ERR(o));
1761
1762         if (!dt_object_exists(o)) {
1763                 th = dt_trans_create(env, d);
1764                 if (IS_ERR(th))
1765                         GOTO(out, rc = PTR_ERR(th));
1766
1767                 lgi->lgi_attr.la_valid = LA_MODE;
1768                 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1769                 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1770
1771                 th->th_wait_submit = 1;
1772                 /* Make the llog object creation synchronization, so
1773                  * it will be reliable to the reference, especially
1774                  * for remote reference */
1775                 th->th_sync = 1;
1776
1777                 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1778                                        &lgi->lgi_dof, th);
1779                 if (rc)
1780                         GOTO(out_trans, rc);
1781
1782                 rc = dt_trans_start_local(env, d, th);
1783                 if (rc)
1784                         GOTO(out_trans, rc);
1785
1786                 dt_write_lock(env, o, 0);
1787                 if (!dt_object_exists(o))
1788                         rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1789                                        &lgi->lgi_dof, th);
1790                 dt_write_unlock(env, o);
1791 out_trans:
1792                 dt_trans_stop(env, d, th);
1793                 if (rc)
1794                         GOTO(out, rc);
1795         }
1796
1797         rc = dt_attr_get(env, o, &lgi->lgi_attr);
1798         if (rc)
1799                 GOTO(out, rc);
1800
1801         if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1802                 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1803                        o->do_lu.lo_dev->ld_obd->obd_name,
1804                        lgi->lgi_attr.la_mode);
1805                 GOTO(out, rc = -ENOENT);
1806         }
1807
1808         CDEBUG(D_CONFIG, "cat list: disk size=%d, read=%d\n",
1809                (int)lgi->lgi_attr.la_size, size);
1810
1811         /* return just number of llogs */
1812         if (idarray == NULL) {
1813                 rc = lgi->lgi_attr.la_size / sizeof(*idarray);
1814                 GOTO(out, rc);
1815         }
1816
1817         /* read for new ost index or for empty file */
1818         memset(idarray, 0, size);
1819         if (lgi->lgi_attr.la_size <= lgi->lgi_off)
1820                 GOTO(out, rc = 0);
1821         if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
1822                 size = lgi->lgi_attr.la_size - lgi->lgi_off;
1823
1824         lgi->lgi_buf.lb_buf = idarray;
1825         lgi->lgi_buf.lb_len = size;
1826         rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
1827         /* -EFAULT means the llog is a sparse file. This is not an error
1828          * after arbitrary OST index is supported. */
1829         if (rc < 0 && rc != -EFAULT) {
1830                 CERROR("%s: error reading CATALOGS: rc = %d\n",
1831                        o->do_lu.lo_dev->ld_obd->obd_name,  rc);
1832                 GOTO(out, rc);
1833         }
1834
1835         EXIT;
1836 out:
1837         lu_object_put(env, &o->do_lu);
1838         RETURN(rc);
1839 }
1840 EXPORT_SYMBOL(llog_osd_get_cat_list);
1841
1842 /**
1843  * Write the special file which contains the list of llog catalogs IDs
1844  *
1845  * This function writes the CATALOG file which contains the array of llog
1846  * catalogs IDs. It is used mostly to store OSP llogs indexed by OST/MDT
1847  * number.
1848  *
1849  * \param[in]  env      execution environment
1850  * \param[in]  d        corresponding storage device
1851  * \param[in]  idx      position to start from, usually OST/MDT index
1852  * \param[in]  count    how many catalog IDs to write
1853  * \param[out] idarray  the buffer with the data to write.
1854  * \param[in]  fid      LLOG_CATALOGS_OID for CATALOG object
1855  *
1856  * \retval              0 on successful write of catalog IDs
1857  * \retval              negative value on error
1858  */
1859 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
1860                           int idx, int count, struct llog_catid *idarray,
1861                           const struct lu_fid *fid)
1862 {
1863         struct llog_thread_info *lgi = llog_info(env);
1864         struct dt_object        *o = NULL;
1865         struct thandle          *th;
1866         int                      rc, size;
1867
1868         if (count == 0)
1869                 RETURN(0);
1870
1871         LASSERT(d);
1872
1873         size = sizeof(*idarray) * count;
1874         lgi->lgi_off = idx * sizeof(*idarray);
1875         lgi->lgi_fid = *fid;
1876
1877         o = dt_locate(env, d, &lgi->lgi_fid);
1878         if (IS_ERR(o))
1879                 RETURN(PTR_ERR(o));
1880
1881         if (!dt_object_exists(o))
1882                 GOTO(out, rc = -ENOENT);
1883
1884         rc = dt_attr_get(env, o, &lgi->lgi_attr);
1885         if (rc)
1886                 GOTO(out, rc);
1887
1888         if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1889                 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1890                        o->do_lu.lo_dev->ld_obd->obd_name,
1891                        lgi->lgi_attr.la_mode);
1892                 GOTO(out, rc = -ENOENT);
1893         }
1894
1895         th = dt_trans_create(env, d);
1896         if (IS_ERR(th))
1897                 GOTO(out, rc = PTR_ERR(th));
1898
1899         lgi->lgi_buf.lb_len = size;
1900         lgi->lgi_buf.lb_buf = idarray;
1901         rc = dt_declare_record_write(env, o, &lgi->lgi_buf, lgi->lgi_off, th);
1902         if (rc)
1903                 GOTO(out, rc);
1904
1905         rc = dt_trans_start_local(env, d, th);
1906         if (rc)
1907                 GOTO(out_trans, rc);
1908
1909         th->th_wait_submit = 1;
1910
1911         rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
1912         if (rc)
1913                 CDEBUG(D_INODE, "can't write CATALOGS at index %d: rc = %d\n",
1914                        idx, rc);
1915 out_trans:
1916         dt_trans_stop(env, d, th);
1917 out:
1918         lu_object_put(env, &o->do_lu);
1919         RETURN(rc);
1920 }
1921 EXPORT_SYMBOL(llog_osd_put_cat_list);