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