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