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