Whamcloud - gitweb
LU-4528 llog: don't write llog in 3-steps
[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         int                      rc;
191
192         ENTRY;
193
194         LASSERT(sizeof(*handle->lgh_hdr) == LLOG_CHUNK_SIZE);
195
196         o = handle->lgh_obj;
197         LASSERT(o);
198
199         lgi = llog_info(env);
200
201         rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
202         if (rc)
203                 RETURN(rc);
204
205         LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
206
207         if (lgi->lgi_attr.la_size == 0) {
208                 CDEBUG(D_HA, "not reading header from 0-byte log\n");
209                 RETURN(LLOG_EEMPTY);
210         }
211
212         lgi->lgi_off = 0;
213         lgi->lgi_buf.lb_buf = handle->lgh_hdr;
214         lgi->lgi_buf.lb_len = LLOG_CHUNK_SIZE;
215
216         rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
217         if (rc) {
218                 CERROR("%s: error reading log header from "DFID": rc = %d\n",
219                        o->do_lu.lo_dev->ld_obd->obd_name,
220                        PFID(lu_object_fid(&o->do_lu)), rc);
221                 RETURN(rc);
222         }
223
224         llh_hdr = &handle->lgh_hdr->llh_hdr;
225         if (LLOG_REC_HDR_NEEDS_SWABBING(llh_hdr))
226                 lustre_swab_llog_hdr(handle->lgh_hdr);
227
228         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
229                 CERROR("%s: bad log %s "DFID" header magic: %#x "
230                        "(expected %#x)\n", o->do_lu.lo_dev->ld_obd->obd_name,
231                        handle->lgh_name ? handle->lgh_name : "",
232                        PFID(lu_object_fid(&o->do_lu)),
233                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
234                 RETURN(-EIO);
235         } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
236                 CERROR("%s: incorrectly sized log %s "DFID" header: "
237                        "%#x (expected %#x)\n"
238                        "you may need to re-run lconf --write_conf.\n",
239                        o->do_lu.lo_dev->ld_obd->obd_name,
240                        handle->lgh_name ? handle->lgh_name : "",
241                        PFID(lu_object_fid(&o->do_lu)),
242                        llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
243                 RETURN(-EIO);
244         }
245
246         handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
247
248         RETURN(0);
249 }
250
251 /**
252  * Implementation of the llog_operations::lop_declare_write
253  *
254  * This function declares the new record write.
255  *
256  * \param[in] env       execution environment
257  * \param[in] loghandle llog handle of the current llog
258  * \param[in] rec       llog record header. This is a real header of the full
259  *                      llog record to write. This is the beginning of buffer
260  *                      to write, the length of buffer is stored in
261  *                      \a rec::lrh_len
262  * \param[in] idx       index of the llog record. If \a idx == -1 then this is
263  *                      append case, otherwise \a idx is the index of record
264  *                      to modify
265  * \param[in] th        current transaction handle
266  *
267  * \retval              0 on successful declaration
268  * \retval              negative error if declaration failed
269  */
270 static int llog_osd_declare_write_rec(const struct lu_env *env,
271                                       struct llog_handle *loghandle,
272                                       struct llog_rec_hdr *rec,
273                                       int idx, struct thandle *th)
274 {
275         struct llog_thread_info *lgi = llog_info(env);
276         struct dt_object        *o;
277         int                      rc;
278
279         ENTRY;
280
281         LASSERT(env);
282         LASSERT(th);
283         LASSERT(loghandle);
284         LASSERT(rec);
285         LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
286
287         o = loghandle->lgh_obj;
288         LASSERT(o);
289
290         lgi->lgi_buf.lb_len = sizeof(struct llog_log_hdr);
291         lgi->lgi_buf.lb_buf = NULL;
292         /* each time we update header */
293         rc = dt_declare_record_write(env, o, &lgi->lgi_buf, 0,
294                                      th);
295         if (rc || idx == 0) /* if error or just header */
296                 RETURN(rc);
297
298         /**
299          * the pad record can be inserted so take into account double
300          * record size
301          */
302         lgi->lgi_buf.lb_len = rec->lrh_len * 2;
303         lgi->lgi_buf.lb_buf = NULL;
304         /* XXX: implement declared window or multi-chunks approach */
305         rc = dt_declare_record_write(env, o, &lgi->lgi_buf, -1, th);
306
307         RETURN(rc);
308 }
309
310 /**
311  * Implementation of the llog_operations::lop_write
312  *
313  * This function writes the new record in the llog or modify the existed one.
314  *
315  * \param[in]  env              execution environment
316  * \param[in]  loghandle        llog handle of the current llog
317  * \param[in]  rec              llog record header. This is a real header of
318  *                              the full llog record to write. This is
319  *                              the beginning of buffer to write, the length
320  *                              of buffer is stored in \a rec::lrh_len
321  * \param[out] reccookie        pointer to the cookie to return back if needed.
322  *                              It is used for further cancel of this llog
323  *                              record.
324  * \param[in]  idx              index of the llog record. If \a idx == -1 then
325  *                              this is append case, otherwise \a idx is
326  *                              the index of record to modify
327  * \param[in]  th               current transaction handle
328  *
329  * \retval                      0 on successful write && \a reccookie == NULL
330  *                              1 on successful write && \a reccookie != NULL
331  * \retval                      negative error if write failed
332  */
333 static int llog_osd_write_rec(const struct lu_env *env,
334                               struct llog_handle *loghandle,
335                               struct llog_rec_hdr *rec,
336                               struct llog_cookie *reccookie,
337                               int idx, struct thandle *th)
338 {
339         struct llog_thread_info *lgi = llog_info(env);
340         struct llog_log_hdr     *llh;
341         int                      reclen = rec->lrh_len;
342         int                      index, rc;
343         struct llog_rec_tail    *lrt;
344         struct dt_object        *o;
345         size_t                   left;
346         bool                     header_is_updated = false;
347
348         ENTRY;
349
350         LASSERT(env);
351         llh = loghandle->lgh_hdr;
352         LASSERT(llh);
353         o = loghandle->lgh_obj;
354         LASSERT(o);
355         LASSERT(th);
356
357         CDEBUG(D_OTHER, "new record %x to "DFID"\n",
358                rec->lrh_type, PFID(lu_object_fid(&o->do_lu)));
359
360         /* record length should not bigger than LLOG_CHUNK_SIZE */
361         if (reclen > LLOG_CHUNK_SIZE)
362                 RETURN(-E2BIG);
363
364         rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
365         if (rc)
366                 RETURN(rc);
367
368         /**
369          * The modification case.
370          * If idx set then the record with that index must be modified.
371          * There are three cases possible:
372          * 1) the common case is the llog header update (idx == 0)
373          * 2) the llog record modification during llog process.
374          *    This is indicated by the \a loghandle::lgh_cur_idx > 0.
375          *    In that case the \a loghandle::lgh_cur_offset
376          * 3) otherwise this is assumed that llog consist of records of
377          *    fixed size, i.e. catalog. The llog header must has llh_size
378          *    field equal to record size. The record offset is calculated
379          *    just by /a idx value
380          *
381          * During modification we don't need extra header update because
382          * the bitmap and record count are not changed. The record header
383          * and tail remains the same too.
384          */
385         if (idx != LLOG_NEXT_IDX) {
386                 /* llog can be empty only when first record is being written */
387                 LASSERT(ergo(idx > 0, lgi->lgi_attr.la_size > 0));
388
389                 if (!ext2_test_bit(idx, llh->llh_bitmap)) {
390                         CERROR("%s: modify unset record %u\n",
391                                o->do_lu.lo_dev->ld_obd->obd_name, idx);
392                         RETURN(-ENOENT);
393                 }
394
395                 if (idx != rec->lrh_index) {
396                         CERROR("%s: modify index mismatch %d %u\n",
397                                o->do_lu.lo_dev->ld_obd->obd_name, idx,
398                                rec->lrh_index);
399                         RETURN(-EFAULT);
400                 }
401
402                 if (idx == LLOG_HEADER_IDX) {
403                         /* llog header update */
404                         LASSERT(reclen == sizeof(struct llog_log_hdr));
405                         LASSERT(rec == &llh->llh_hdr);
406
407                         lgi->lgi_off = 0;
408                         lgi->lgi_buf.lb_len = reclen;
409                         lgi->lgi_buf.lb_buf = rec;
410                         rc = dt_record_write(env, o, &lgi->lgi_buf,
411                                              &lgi->lgi_off, th);
412                         RETURN(rc);
413                 } else if (loghandle->lgh_cur_idx > 0) {
414                         /**
415                          * The lgh_cur_offset can be used only if index is
416                          * the same.
417                          */
418                         if (idx != loghandle->lgh_cur_idx) {
419                                 CERROR("%s: modify index mismatch %d %d\n",
420                                        o->do_lu.lo_dev->ld_obd->obd_name, idx,
421                                        loghandle->lgh_cur_idx);
422                                 RETURN(-EFAULT);
423                         }
424
425                         lgi->lgi_off = loghandle->lgh_cur_offset;
426                         CDEBUG(D_OTHER, "modify record "DOSTID": idx:%d, "
427                                "len:%u offset %llu\n",
428                                POSTID(&loghandle->lgh_id.lgl_oi), idx,
429                                rec->lrh_len, (long long)lgi->lgi_off);
430                 } else if (llh->llh_size > 0) {
431                         if (llh->llh_size != rec->lrh_len) {
432                                 CERROR("%s: wrong record size, llh_size is %u"
433                                        " but record size is %u\n",
434                                        o->do_lu.lo_dev->ld_obd->obd_name,
435                                        llh->llh_size, rec->lrh_len);
436                                 RETURN(-EINVAL);
437                         }
438                         lgi->lgi_off = sizeof(*llh) + (idx - 1) * reclen;
439                 } else {
440                         /* This can be result of lgh_cur_idx is not set during
441                          * llog processing or llh_size is not set to proper
442                          * record size for fixed records llog. Therefore it is
443                          * impossible to get record offset. */
444                         CERROR("%s: can't get record offset, idx:%d, "
445                                "len:%u.\n", o->do_lu.lo_dev->ld_obd->obd_name,
446                                idx, rec->lrh_len);
447                         RETURN(-EFAULT);
448                 }
449
450                 /* update only data, header and tail remain the same */
451                 lgi->lgi_off += sizeof(struct llog_rec_hdr);
452                 lgi->lgi_buf.lb_len = REC_DATA_LEN(rec);
453                 lgi->lgi_buf.lb_buf = REC_DATA(rec);
454                 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
455                 if (rc == 0 && reccookie) {
456                         reccookie->lgc_lgl = loghandle->lgh_id;
457                         reccookie->lgc_index = idx;
458                         rc = 1;
459                 }
460                 RETURN(rc);
461         }
462
463         /**
464          * The append case.
465          * The most common case of using llog. The new index is assigned to
466          * the new record, new bit is set in llog bitmap and llog count is
467          * incremented.
468          *
469          * Make sure that records don't cross a chunk boundary, so we can
470          * process them page-at-a-time if needed.  If it will cross a chunk
471          * boundary, write in a fake (but referenced) entry to pad the chunk.
472          */
473         LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
474         lgi->lgi_off = lgi->lgi_attr.la_size;
475         left = LLOG_CHUNK_SIZE - (lgi->lgi_off & (LLOG_CHUNK_SIZE - 1));
476         /* NOTE: padding is a record, but no bit is set */
477         if (left != 0 && left != reclen &&
478             left < (reclen + LLOG_MIN_REC_SIZE)) {
479                 index = loghandle->lgh_last_idx + 1;
480                 rc = llog_osd_pad(env, o, &lgi->lgi_off, left, index, th);
481                 if (rc)
482                         RETURN(rc);
483                 loghandle->lgh_last_idx++; /* for pad rec */
484         }
485         /* if it's the last idx in log file, then return -ENOSPC */
486         if (loghandle->lgh_last_idx >= LLOG_BITMAP_SIZE(llh) - 1)
487                 RETURN(-ENOSPC);
488
489         /* increment the last_idx along with llh_tail index, they should
490          * be equal for a llog lifetime */
491         loghandle->lgh_last_idx++;
492         index = loghandle->lgh_last_idx;
493         llh->llh_tail.lrt_index = index;
494         /**
495          * NB: the caller should make sure only 1 process access
496          * the lgh_last_idx, e.g. append should be exclusive.
497          * Otherwise it might hit the assert.
498          */
499         LASSERT(index < LLOG_BITMAP_SIZE(llh));
500         rec->lrh_index = index;
501         lrt = rec_tail(rec);
502         lrt->lrt_len = rec->lrh_len;
503         lrt->lrt_index = rec->lrh_index;
504
505         /* the lgh_hdr_lock protects llog header data from concurrent
506          * update/cancel, the llh_count and llh_bitmap are protected */
507         spin_lock(&loghandle->lgh_hdr_lock);
508         if (ext2_set_bit(index, llh->llh_bitmap)) {
509                 CERROR("%s: index %u already set in log bitmap\n",
510                        o->do_lu.lo_dev->ld_obd->obd_name, index);
511                 spin_unlock(&loghandle->lgh_hdr_lock);
512                 LBUG(); /* should never happen */
513         }
514         llh->llh_count++;
515         spin_unlock(&loghandle->lgh_hdr_lock);
516
517         lgi->lgi_off = 0;
518         lgi->lgi_buf.lb_len = llh->llh_hdr.lrh_len;
519         lgi->lgi_buf.lb_buf = &llh->llh_hdr;
520         rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
521         if (rc)
522                 GOTO(out, rc);
523
524         header_is_updated = true;
525         rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
526         if (rc)
527                 GOTO(out, rc);
528
529         LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
530         lgi->lgi_off = lgi->lgi_attr.la_size;
531         lgi->lgi_buf.lb_len = reclen;
532         lgi->lgi_buf.lb_buf = rec;
533         rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
534         if (rc < 0)
535                 GOTO(out, rc);
536
537         CDEBUG(D_OTHER, "added record "DOSTID": idx: %u, %u\n",
538                POSTID(&loghandle->lgh_id.lgl_oi), index, rec->lrh_len);
539         if (reccookie != NULL) {
540                 reccookie->lgc_lgl = loghandle->lgh_id;
541                 reccookie->lgc_index = index;
542                 if ((rec->lrh_type == MDS_UNLINK_REC) ||
543                     (rec->lrh_type == MDS_SETATTR64_REC))
544                         reccookie->lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
545                 else if (rec->lrh_type == OST_SZ_REC)
546                         reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
547                 else
548                         reccookie->lgc_subsys = -1;
549                 rc = 1;
550         }
551         RETURN(rc);
552 out:
553         /* cleanup llog for error case */
554         spin_lock(&loghandle->lgh_hdr_lock);
555         ext2_clear_bit(index, llh->llh_bitmap);
556         llh->llh_count--;
557         spin_unlock(&loghandle->lgh_hdr_lock);
558
559         /* restore llog last_idx */
560         loghandle->lgh_last_idx--;
561         llh->llh_tail.lrt_index = loghandle->lgh_last_idx;
562
563         /* restore the header on disk if it was written */
564         if (header_is_updated) {
565                 lgi->lgi_off = 0;
566                 lgi->lgi_buf.lb_len = llh->llh_hdr.lrh_len;
567                 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
568                 dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
569         }
570
571         RETURN(rc);
572 }
573
574 /**
575  * We can skip reading at least as many log blocks as the number of
576  * minimum sized log records we are skipping.  If it turns out
577  * that we are not far enough along the log (because the
578  * actual records are larger than minimum size) we just skip
579  * some more records.
580  */
581 static inline void llog_skip_over(__u64 *off, int curr, int goal)
582 {
583         if (goal <= curr)
584                 return;
585         *off = (*off + (goal - curr - 1) * LLOG_MIN_REC_SIZE) &
586                 ~(LLOG_CHUNK_SIZE - 1);
587 }
588
589 /**
590  * Implementation of the llog_operations::lop_next_block
591  *
592  * This function finds the the next llog block to return which contains
593  * record with required index. It is main part of llog processing.
594  *
595  * \param[in]     env           execution environment
596  * \param[in]     loghandle     llog handle of the current llog
597  * \param[in,out] cur_idx       index preceeding cur_offset
598  * \param[in]     next_idx      target index to find
599  * \param[in,out] cur_offset    furtherst point read in the file
600  * \param[in]     buf           pointer to data buffer to fill
601  * \param[in]     len           required len to read, it is
602  *                              LLOG_CHUNK_SIZE usually.
603  *
604  * \retval                      0 on successful buffer read
605  * \retval                      negative value on error
606  */
607 static int llog_osd_next_block(const struct lu_env *env,
608                                struct llog_handle *loghandle, int *cur_idx,
609                                int next_idx, __u64 *cur_offset, void *buf,
610                                int len)
611 {
612         struct llog_thread_info *lgi = llog_info(env);
613         struct dt_object        *o;
614         struct dt_device        *dt;
615         int                      rc;
616
617         ENTRY;
618
619         LASSERT(env);
620         LASSERT(lgi);
621
622         if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
623                 RETURN(-EINVAL);
624
625         CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off "LPU64")\n",
626                next_idx, *cur_idx, *cur_offset);
627
628         LASSERT(loghandle);
629         LASSERT(loghandle->lgh_ctxt);
630
631         o = loghandle->lgh_obj;
632         LASSERT(o);
633         LASSERT(dt_object_exists(o));
634         dt = lu2dt_dev(o->do_lu.lo_dev);
635         LASSERT(dt);
636
637         rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
638         if (rc)
639                 GOTO(out, rc);
640
641         while (*cur_offset < lgi->lgi_attr.la_size) {
642                 struct llog_rec_hdr     *rec, *last_rec;
643                 struct llog_rec_tail    *tail;
644
645                 llog_skip_over(cur_offset, *cur_idx, next_idx);
646
647                 /* read up to next LLOG_CHUNK_SIZE block */
648                 lgi->lgi_buf.lb_len = LLOG_CHUNK_SIZE -
649                                       (*cur_offset & (LLOG_CHUNK_SIZE - 1));
650                 lgi->lgi_buf.lb_buf = buf;
651
652                 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
653                 if (rc < 0) {
654                         CERROR("%s: can't read llog block from log "DFID
655                                " offset "LPU64": rc = %d\n",
656                                o->do_lu.lo_dev->ld_obd->obd_name,
657                                PFID(lu_object_fid(&o->do_lu)), *cur_offset,
658                                rc);
659                         GOTO(out, rc);
660                 }
661
662                 if (rc < len) {
663                         /* signal the end of the valid buffer to
664                          * llog_process */
665                         memset(buf + rc, 0, len - rc);
666                 }
667
668                 if (rc == 0) /* end of file, nothing to do */
669                         GOTO(out, rc);
670
671                 if (rc < sizeof(*tail)) {
672                         CERROR("%s: invalid llog block at log id "DOSTID"/%u "
673                                "offset "LPU64"\n",
674                                o->do_lu.lo_dev->ld_obd->obd_name,
675                                POSTID(&loghandle->lgh_id.lgl_oi),
676                                loghandle->lgh_id.lgl_ogen, *cur_offset);
677                         GOTO(out, rc = -EINVAL);
678                 }
679
680                 rec = buf;
681                 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
682                         lustre_swab_llog_rec(rec);
683
684                 tail = (struct llog_rec_tail *)((char *)buf + rc -
685                                                 sizeof(struct llog_rec_tail));
686                 /* get the last record in block */
687                 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
688                                                    le32_to_cpu(tail->lrt_len));
689
690                 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
691                         lustre_swab_llog_rec(last_rec);
692                 LASSERT(last_rec->lrh_index == tail->lrt_index);
693
694                 *cur_idx = tail->lrt_index;
695
696                 /* this shouldn't happen */
697                 if (tail->lrt_index == 0) {
698                         CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
699                                "offset "LPU64"\n",
700                                o->do_lu.lo_dev->ld_obd->obd_name,
701                                POSTID(&loghandle->lgh_id.lgl_oi),
702                                loghandle->lgh_id.lgl_ogen, *cur_offset);
703                         GOTO(out, rc = -EINVAL);
704                 }
705                 if (tail->lrt_index < next_idx)
706                         continue;
707
708                 /* sanity check that the start of the new buffer is no farther
709                  * than the record that we wanted.  This shouldn't happen. */
710                 if (rec->lrh_index > next_idx) {
711                         CERROR("%s: missed desired record? %u > %u\n",
712                                o->do_lu.lo_dev->ld_obd->obd_name,
713                                rec->lrh_index, next_idx);
714                         GOTO(out, rc = -ENOENT);
715                 }
716                 GOTO(out, rc = 0);
717         }
718         GOTO(out, rc = -EIO);
719 out:
720         return rc;
721 }
722
723 /**
724  * Implementation of the llog_operations::lop_prev_block
725  *
726  * This function finds the llog block to return which contains
727  * record with required index but in reverse order - from end of llog
728  * to the beginning.
729  * It is main part of reverse llog processing.
730  *
731  * \param[in] env       execution environment
732  * \param[in] loghandle llog handle of the current llog
733  * \param[in] prev_idx  target index to find
734  * \param[in] buf       pointer to data buffer to fill
735  * \param[in] len       required len to read, it is LLOG_CHUNK_SIZE usually.
736  *
737  * \retval              0 on successful buffer read
738  * \retval              negative value on error
739  */
740 static int llog_osd_prev_block(const struct lu_env *env,
741                                struct llog_handle *loghandle,
742                                int prev_idx, void *buf, int len)
743 {
744         struct llog_thread_info *lgi = llog_info(env);
745         struct dt_object        *o;
746         struct dt_device        *dt;
747         loff_t                   cur_offset;
748         int                      rc;
749
750         ENTRY;
751
752         if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
753                 RETURN(-EINVAL);
754
755         CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
756
757         LASSERT(loghandle);
758         LASSERT(loghandle->lgh_ctxt);
759
760         o = loghandle->lgh_obj;
761         LASSERT(o);
762         LASSERT(dt_object_exists(o));
763         dt = lu2dt_dev(o->do_lu.lo_dev);
764         LASSERT(dt);
765
766         cur_offset = LLOG_CHUNK_SIZE;
767         llog_skip_over(&cur_offset, 0, prev_idx);
768
769         rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
770         if (rc)
771                 GOTO(out, rc);
772
773         while (cur_offset < lgi->lgi_attr.la_size) {
774                 struct llog_rec_hdr     *rec, *last_rec;
775                 struct llog_rec_tail    *tail;
776
777                 lgi->lgi_buf.lb_len = len;
778                 lgi->lgi_buf.lb_buf = buf;
779                 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
780                 if (rc < 0) {
781                         CERROR("%s: can't read llog block from log "DFID
782                                " offset "LPU64": rc = %d\n",
783                                o->do_lu.lo_dev->ld_obd->obd_name,
784                                PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
785                         GOTO(out, rc);
786                 }
787
788                 if (rc == 0) /* end of file, nothing to do */
789                         GOTO(out, rc);
790
791                 if (rc < sizeof(*tail)) {
792                         CERROR("%s: invalid llog block at log id "DOSTID"/%u "
793                                "offset "LPU64"\n",
794                                o->do_lu.lo_dev->ld_obd->obd_name,
795                                POSTID(&loghandle->lgh_id.lgl_oi),
796                                loghandle->lgh_id.lgl_ogen, cur_offset);
797                         GOTO(out, rc = -EINVAL);
798                 }
799
800                 rec = buf;
801                 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
802                         lustre_swab_llog_rec(rec);
803
804                 tail = (struct llog_rec_tail *)((char *)buf + rc -
805                                                 sizeof(struct llog_rec_tail));
806                 /* get the last record in block */
807                 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
808                                                    le32_to_cpu(tail->lrt_len));
809
810                 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
811                         lustre_swab_llog_rec(last_rec);
812                 LASSERT(last_rec->lrh_index == tail->lrt_index);
813
814                 /* this shouldn't happen */
815                 if (tail->lrt_index == 0) {
816                         CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
817                                "offset "LPU64"\n",
818                                o->do_lu.lo_dev->ld_obd->obd_name,
819                                POSTID(&loghandle->lgh_id.lgl_oi),
820                                loghandle->lgh_id.lgl_ogen, cur_offset);
821                         GOTO(out, rc = -EINVAL);
822                 }
823                 if (tail->lrt_index < prev_idx)
824                         continue;
825
826                 /* sanity check that the start of the new buffer is no farther
827                  * than the record that we wanted.  This shouldn't happen. */
828                 if (rec->lrh_index > prev_idx) {
829                         CERROR("%s: missed desired record? %u > %u\n",
830                                o->do_lu.lo_dev->ld_obd->obd_name,
831                                rec->lrh_index, prev_idx);
832                         GOTO(out, rc = -ENOENT);
833                 }
834                 GOTO(out, rc = 0);
835         }
836         GOTO(out, rc = -EIO);
837 out:
838         return rc;
839 }
840
841 /**
842  * This is helper function to get llog directory object. It is used by named
843  * llog operations to find/insert/delete llog entry from llog directory.
844  *
845  * \param[in] env       execution environment
846  * \param[in] ctxt      llog context
847  *
848  * \retval              dt_object of llog directory
849  * \retval              ERR_PTR of negative value on error
850  */
851 struct dt_object *llog_osd_dir_get(const struct lu_env *env,
852                                    struct llog_ctxt *ctxt)
853 {
854         struct dt_device        *dt;
855         struct dt_thread_info   *dti = dt_info(env);
856         struct dt_object        *dir;
857         int                      rc;
858
859         dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
860         if (ctxt->loc_dir == NULL) {
861                 rc = dt_root_get(env, dt, &dti->dti_fid);
862                 if (rc)
863                         return ERR_PTR(rc);
864                 dir = dt_locate(env, dt, &dti->dti_fid);
865
866                 if (!IS_ERR(dir) && !dt_try_as_dir(env, dir)) {
867                         lu_object_put(env, &dir->do_lu);
868                         return ERR_PTR(-ENOTDIR);
869                 }
870         } else {
871                 lu_object_get(&ctxt->loc_dir->do_lu);
872                 dir = ctxt->loc_dir;
873         }
874
875         return dir;
876 }
877
878 /**
879  * Implementation of the llog_operations::lop_open
880  *
881  * This function opens the llog by its logid or by name, it may open also
882  * non existent llog and assing then new id to it.
883  * The llog_open/llog_close pair works similar to lu_object_find/put,
884  * the object may not exist prior open. The result of open is just dt_object
885  * in the llog header.
886  *
887  * \param[in] env               execution environment
888  * \param[in] handle            llog handle of the current llog
889  * \param[in] logid             logid of llog to open (nameless llog)
890  * \param[in] name              name of llog to open (named llog)
891  * \param[in] open_param
892  *                              LLOG_OPEN_NEW - new llog, may not exist
893  *                              LLOG_OPEN_EXIST - old llog, must exist
894  *
895  * \retval                      0 on successful open, llog_handle::lgh_obj
896  *                              contains the dt_object of the llog.
897  * \retval                      negative value on error
898  */
899 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
900                          struct llog_logid *logid, char *name,
901                          enum llog_open_param open_param)
902 {
903         struct llog_thread_info         *lgi = llog_info(env);
904         struct llog_ctxt                *ctxt = handle->lgh_ctxt;
905         struct dt_object                *o;
906         struct dt_device                *dt;
907         struct ls_device                *ls;
908         struct local_oid_storage        *los;
909         int                              rc = 0;
910
911         ENTRY;
912
913         LASSERT(env);
914         LASSERT(ctxt);
915         LASSERT(ctxt->loc_exp);
916         LASSERT(ctxt->loc_exp->exp_obd);
917         dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
918         LASSERT(dt);
919
920         ls = ls_device_get(dt);
921         if (IS_ERR(ls))
922                 RETURN(PTR_ERR(ls));
923
924         mutex_lock(&ls->ls_los_mutex);
925         los = dt_los_find(ls, name != NULL ? FID_SEQ_LLOG_NAME : FID_SEQ_LLOG);
926         mutex_unlock(&ls->ls_los_mutex);
927         LASSERT(los);
928         ls_device_put(env, ls);
929
930         LASSERT(handle);
931
932         if (logid != NULL) {
933                 logid_to_fid(logid, &lgi->lgi_fid);
934         } else if (name) {
935                 struct dt_object *llog_dir;
936
937                 llog_dir = llog_osd_dir_get(env, ctxt);
938                 if (IS_ERR(llog_dir))
939                         GOTO(out, rc = PTR_ERR(llog_dir));
940                 dt_read_lock(env, llog_dir, 0);
941                 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
942                 dt_read_unlock(env, llog_dir);
943                 lu_object_put(env, &llog_dir->do_lu);
944                 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
945                         /* generate fid for new llog */
946                         rc = local_object_fid_generate(env, los,
947                                                        &lgi->lgi_fid);
948                 }
949                 if (rc < 0)
950                         GOTO(out, rc);
951                 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
952                 if (handle->lgh_name)
953                         strcpy(handle->lgh_name, name);
954                 else
955                         GOTO(out, rc = -ENOMEM);
956         } else {
957                 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
958                 /* generate fid for new llog */
959                 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
960                 if (rc < 0)
961                         GOTO(out, rc);
962         }
963
964         o = ls_locate(env, ls, &lgi->lgi_fid, NULL);
965         if (IS_ERR(o))
966                 GOTO(out_name, rc = PTR_ERR(o));
967
968         /* No new llog is expected but doesn't exist */
969         if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o))
970                 GOTO(out_put, rc = -ENOENT);
971
972         fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
973         handle->lgh_obj = o;
974         handle->private_data = los;
975         LASSERT(handle->lgh_ctxt);
976
977         RETURN(rc);
978
979 out_put:
980         lu_object_put(env, &o->do_lu);
981 out_name:
982         if (handle->lgh_name != NULL)
983                 OBD_FREE(handle->lgh_name, strlen(name) + 1);
984 out:
985         dt_los_put(los);
986         RETURN(rc);
987 }
988
989 /**
990  * Implementation of the llog_operations::lop_exist
991  *
992  * This function checks that llog exists on storage.
993  *
994  * \param[in] handle    llog handle of the current llog
995  *
996  * \retval              true if llog object exists and is not just destroyed
997  * \retval              false if llog doesn't exist or just destroyed
998  */
999 static int llog_osd_exist(struct llog_handle *handle)
1000 {
1001         LASSERT(handle->lgh_obj);
1002         return (dt_object_exists(handle->lgh_obj) &&
1003                 !lu_object_is_dying(handle->lgh_obj->do_lu.lo_header));
1004 }
1005
1006 /**
1007  * Implementation of the llog_operations::lop_declare_create
1008  *
1009  * This function declares the llog create. It declares also name insert
1010  * into llog directory in case of named llog.
1011  *
1012  * \param[in] env       execution environment
1013  * \param[in] res       llog handle of the current llog
1014  * \param[in] th        current transaction handle
1015  *
1016  * \retval              0 on successful create declaration
1017  * \retval              negative value on error
1018  */
1019 static int llog_osd_declare_create(const struct lu_env *env,
1020                                    struct llog_handle *res, struct thandle *th)
1021 {
1022         struct llog_thread_info         *lgi = llog_info(env);
1023         struct local_oid_storage        *los;
1024         struct dt_object                *o;
1025         int                              rc;
1026
1027         ENTRY;
1028
1029         LASSERT(res->lgh_obj);
1030         LASSERT(th);
1031
1032         /* object can be created by another thread */
1033         o = res->lgh_obj;
1034         if (dt_object_exists(o))
1035                 RETURN(0);
1036
1037         los = res->private_data;
1038         LASSERT(los);
1039
1040         rc = llog_osd_declare_new_object(env, los, o, th);
1041         if (rc)
1042                 RETURN(rc);
1043
1044         /* do not declare header initialization here as it's declared
1045          * in llog_osd_declare_write_rec() which is always called */
1046
1047         if (res->lgh_name) {
1048                 struct dt_object *llog_dir;
1049
1050                 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1051                 if (IS_ERR(llog_dir))
1052                         RETURN(PTR_ERR(llog_dir));
1053                 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1054                 rc = dt_declare_insert(env, llog_dir,
1055                                        (struct dt_rec *)&lgi->lgi_fid,
1056                                        (struct dt_key *)res->lgh_name, th);
1057                 lu_object_put(env, &llog_dir->do_lu);
1058                 if (rc)
1059                         CERROR("%s: can't declare named llog %s: rc = %d\n",
1060                                o->do_lu.lo_dev->ld_obd->obd_name,
1061                                res->lgh_name, rc);
1062         }
1063         RETURN(rc);
1064 }
1065
1066 /**
1067  * Implementation of the llog_operations::lop_create
1068  *
1069  * This function creates the llog according with llog_handle::lgh_obj
1070  * and llog_handle::lgh_name.
1071  *
1072  * \param[in] env       execution environment
1073  * \param[in] res       llog handle of the current llog
1074  * \param[in] th        current transaction handle
1075  *
1076  * \retval              0 on successful create
1077  * \retval              negative value on error
1078  */
1079 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
1080                            struct thandle *th)
1081 {
1082         struct llog_thread_info *lgi = llog_info(env);
1083         struct local_oid_storage *los;
1084         struct dt_object        *o;
1085         int                      rc = 0;
1086
1087         ENTRY;
1088
1089         LASSERT(env);
1090         o = res->lgh_obj;
1091         LASSERT(o);
1092
1093         /* llog can be already created */
1094         if (dt_object_exists(o))
1095                 RETURN(-EEXIST);
1096
1097         los = res->private_data;
1098         LASSERT(los);
1099
1100         dt_write_lock(env, o, 0);
1101         if (!dt_object_exists(o))
1102                 rc = llog_osd_create_new_object(env, los, o, th);
1103         else
1104                 rc = -EEXIST;
1105
1106         dt_write_unlock(env, o);
1107         if (rc)
1108                 RETURN(rc);
1109
1110         if (res->lgh_name) {
1111                 struct dt_object *llog_dir;
1112
1113                 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1114                 if (IS_ERR(llog_dir))
1115                         RETURN(PTR_ERR(llog_dir));
1116
1117                 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1118                 dt_read_lock(env, llog_dir, 0);
1119                 rc = dt_insert(env, llog_dir,
1120                                (struct dt_rec *)&lgi->lgi_fid,
1121                                (struct dt_key *)res->lgh_name,
1122                                th, BYPASS_CAPA, 1);
1123                 dt_read_unlock(env, llog_dir);
1124                 lu_object_put(env, &llog_dir->do_lu);
1125                 if (rc)
1126                         CERROR("%s: can't create named llog %s: rc = %d\n",
1127                                o->do_lu.lo_dev->ld_obd->obd_name,
1128                                res->lgh_name, rc);
1129         }
1130         RETURN(rc);
1131 }
1132
1133 /**
1134  * Implementation of the llog_operations::lop_close
1135  *
1136  * This function closes the llog. It just put llog object and referenced
1137  * local storage.
1138  *
1139  * \param[in] env       execution environment
1140  * \param[in] handle    llog handle of the current llog
1141  *
1142  * \retval              0 on successful llog close
1143  * \retval              negative value on error
1144  */
1145 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
1146 {
1147         struct local_oid_storage        *los;
1148         int                              rc = 0;
1149
1150         ENTRY;
1151
1152         LASSERT(handle->lgh_obj);
1153
1154         lu_object_put(env, &handle->lgh_obj->do_lu);
1155
1156         los = handle->private_data;
1157         LASSERT(los);
1158         dt_los_put(los);
1159
1160         if (handle->lgh_name)
1161                 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
1162
1163         RETURN(rc);
1164 }
1165
1166 /**
1167  * Implementation of the llog_operations::lop_destroy
1168  *
1169  * This function destroys the llog and deletes also entry in the
1170  * llog directory in case of named llog. Llog should be opened prior that.
1171  * Destroy method is not part of external transaction and does everything
1172  * inside.
1173  *
1174  * \param[in] env               execution environment
1175  * \param[in] loghandle llog handle of the current llog
1176  *
1177  * \retval              0 on successful destroy
1178  * \retval              negative value on error
1179  */
1180 static int llog_osd_destroy(const struct lu_env *env,
1181                             struct llog_handle *loghandle)
1182 {
1183         struct llog_ctxt        *ctxt;
1184         struct dt_object        *o, *llog_dir = NULL;
1185         struct dt_device        *d;
1186         struct thandle          *th;
1187         char                    *name = NULL;
1188         int                      rc;
1189
1190         ENTRY;
1191
1192         ctxt = loghandle->lgh_ctxt;
1193         LASSERT(ctxt);
1194
1195         o = loghandle->lgh_obj;
1196         LASSERT(o);
1197
1198         d = lu2dt_dev(o->do_lu.lo_dev);
1199         LASSERT(d);
1200         LASSERT(d == ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt);
1201
1202         th = dt_trans_create(env, d);
1203         if (IS_ERR(th))
1204                 RETURN(PTR_ERR(th));
1205
1206         if (loghandle->lgh_name) {
1207                 llog_dir = llog_osd_dir_get(env, ctxt);
1208                 if (IS_ERR(llog_dir))
1209                         GOTO(out_trans, rc = PTR_ERR(llog_dir));
1210
1211                 name = loghandle->lgh_name;
1212                 rc = dt_declare_delete(env, llog_dir,
1213                                        (struct dt_key *)name, th);
1214                 if (rc)
1215                         GOTO(out_trans, rc);
1216         }
1217
1218         dt_declare_ref_del(env, o, th);
1219
1220         rc = dt_declare_destroy(env, o, th);
1221         if (rc)
1222                 GOTO(out_trans, rc);
1223
1224         rc = dt_trans_start_local(env, d, th);
1225         if (rc)
1226                 GOTO(out_trans, rc);
1227
1228         dt_write_lock(env, o, 0);
1229         if (dt_object_exists(o)) {
1230                 if (name) {
1231                         dt_read_lock(env, llog_dir, 0);
1232                         rc = dt_delete(env, llog_dir,
1233                                        (struct dt_key *) name,
1234                                        th, BYPASS_CAPA);
1235                         dt_read_unlock(env, llog_dir);
1236                         if (rc) {
1237                                 CERROR("%s: can't remove llog %s: rc = %d\n",
1238                                        o->do_lu.lo_dev->ld_obd->obd_name,
1239                                        name, rc);
1240                                 GOTO(out_unlock, rc);
1241                         }
1242                 }
1243                 dt_ref_del(env, o, th);
1244                 rc = dt_destroy(env, o, th);
1245                 if (rc)
1246                         GOTO(out_unlock, rc);
1247         }
1248 out_unlock:
1249         dt_write_unlock(env, o);
1250 out_trans:
1251         dt_trans_stop(env, d, th);
1252         if (llog_dir != NULL)
1253                 lu_object_put(env, &llog_dir->do_lu);
1254         RETURN(rc);
1255 }
1256
1257 /**
1258  * Implementation of the llog_operations::lop_setup
1259  *
1260  * This function setup the llog on local storage.
1261  *
1262  * \param[in] env       execution environment
1263  * \param[in] obd       obd device the llog belongs to
1264  * \param[in] olg       the llog group, it is always zero group now.
1265  * \param[in] ctxt_idx  the llog index, it defines the purpose of this llog.
1266  *                      Every new llog type have to use own index.
1267  * \param[in] disk_obd  the storage obd, where llog is stored.
1268  *
1269  * \retval              0 on successful llog setup
1270  * \retval              negative value on error
1271  */
1272 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1273                           struct obd_llog_group *olg, int ctxt_idx,
1274                           struct obd_device *disk_obd)
1275 {
1276         struct llog_thread_info         *lgi = llog_info(env);
1277         struct llog_ctxt                *ctxt;
1278         int                              rc = 0;
1279         ENTRY;
1280
1281         LASSERT(obd);
1282         LASSERT(olg->olg_ctxts[ctxt_idx]);
1283
1284         ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1285         LASSERT(ctxt);
1286
1287         /* initialize data allowing to generate new fids,
1288          * literally we need a sequece */
1289         lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1290         lgi->lgi_fid.f_oid = 1;
1291         lgi->lgi_fid.f_ver = 0;
1292         rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1293                                     &lgi->lgi_fid,
1294                                     &ctxt->loc_los_nameless);
1295         if (rc != 0)
1296                 GOTO(out, rc);
1297
1298         lgi->lgi_fid.f_seq = FID_SEQ_LLOG_NAME;
1299         lgi->lgi_fid.f_oid = 1;
1300         lgi->lgi_fid.f_ver = 0;
1301         rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1302                                     &lgi->lgi_fid,
1303                                     &ctxt->loc_los_named);
1304         if (rc != 0) {
1305                 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1306                 ctxt->loc_los_nameless = NULL;
1307         }
1308
1309         GOTO(out, rc);
1310
1311 out:
1312         llog_ctxt_put(ctxt);
1313         return rc;
1314 }
1315
1316 /**
1317  * Implementation of the llog_operations::lop_cleanup
1318  *
1319  * This function cleanups the llog on local storage.
1320  *
1321  * \param[in] env       execution environment
1322  * \param[in] ctxt      the llog context
1323  *
1324  * \retval              0
1325  */
1326 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1327 {
1328         if (ctxt->loc_los_nameless != NULL) {
1329                 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1330                 ctxt->loc_los_nameless = NULL;
1331         }
1332
1333         if (ctxt->loc_los_named != NULL) {
1334                 local_oid_storage_fini(env, ctxt->loc_los_named);
1335                 ctxt->loc_los_named = NULL;
1336         }
1337
1338         return 0;
1339 }
1340
1341 struct llog_operations llog_osd_ops = {
1342         .lop_next_block         = llog_osd_next_block,
1343         .lop_prev_block         = llog_osd_prev_block,
1344         .lop_read_header        = llog_osd_read_header,
1345         .lop_destroy            = llog_osd_destroy,
1346         .lop_setup              = llog_osd_setup,
1347         .lop_cleanup            = llog_osd_cleanup,
1348         .lop_open               = llog_osd_open,
1349         .lop_exist              = llog_osd_exist,
1350         .lop_declare_create     = llog_osd_declare_create,
1351         .lop_create             = llog_osd_create,
1352         .lop_declare_write_rec  = llog_osd_declare_write_rec,
1353         .lop_write_rec          = llog_osd_write_rec,
1354         .lop_close              = llog_osd_close,
1355 };
1356 EXPORT_SYMBOL(llog_osd_ops);
1357
1358 /**
1359  * Read the special file which contains the list of llog catalogs IDs
1360  *
1361  * This function reads the CATALOGS file which contains the array of llog
1362  * catalogs IDs. The main purpose of this file is to store OSP llogs indexed
1363  * by OST/MDT number.
1364  *
1365  * \param[in]  env              execution environment
1366  * \param[in]  d                corresponding storage device
1367  * \param[in]  idx              position to start from, usually OST/MDT index
1368  * \param[in]  count            how many catalog IDs to read
1369  * \param[out] idarray          the buffer for the data. If it is NULL then
1370  *                              function returns just number of catalog IDs
1371  *                              in the file.
1372  * \param[in]  fid              LLOG_CATALOGS_OID for CATALOG object
1373  *
1374  * \retval                      0 on successful read of catalog IDs
1375  * \retval                      negative value on error
1376  * \retval                      positive value which is number of records in
1377  *                              the file if \a idarray is NULL
1378  */
1379 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
1380                           int idx, int count, struct llog_catid *idarray,
1381                           const struct lu_fid *fid)
1382 {
1383         struct llog_thread_info *lgi = llog_info(env);
1384         struct dt_object        *o = NULL;
1385         struct thandle          *th;
1386         int                      rc, size;
1387
1388         ENTRY;
1389
1390         LASSERT(d);
1391
1392         size = sizeof(*idarray) * count;
1393         lgi->lgi_off = idx *  sizeof(*idarray);
1394
1395         lgi->lgi_fid = *fid;
1396         o = dt_locate(env, d, &lgi->lgi_fid);
1397         if (IS_ERR(o))
1398                 RETURN(PTR_ERR(o));
1399
1400         if (!dt_object_exists(o)) {
1401                 th = dt_trans_create(env, d);
1402                 if (IS_ERR(th))
1403                         GOTO(out, rc = PTR_ERR(th));
1404
1405                 lgi->lgi_attr.la_valid = LA_MODE;
1406                 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1407                 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1408
1409                 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1410                                        &lgi->lgi_dof, th);
1411                 if (rc)
1412                         GOTO(out_trans, rc);
1413
1414                 rc = dt_trans_start_local(env, d, th);
1415                 if (rc)
1416                         GOTO(out_trans, rc);
1417
1418                 dt_write_lock(env, o, 0);
1419                 if (!dt_object_exists(o))
1420                         rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1421                                        &lgi->lgi_dof, th);
1422                 dt_write_unlock(env, o);
1423 out_trans:
1424                 dt_trans_stop(env, d, th);
1425                 if (rc)
1426                         GOTO(out, rc);
1427         }
1428
1429         rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
1430         if (rc)
1431                 GOTO(out, rc);
1432
1433         if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1434                 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1435                        o->do_lu.lo_dev->ld_obd->obd_name,
1436                        lgi->lgi_attr.la_mode);
1437                 GOTO(out, rc = -ENOENT);
1438         }
1439
1440         CDEBUG(D_CONFIG, "cat list: disk size=%d, read=%d\n",
1441                (int)lgi->lgi_attr.la_size, size);
1442
1443         /* return just number of llogs */
1444         if (idarray == NULL) {
1445                 rc = lgi->lgi_attr.la_size / sizeof(*idarray);
1446                 GOTO(out, rc);
1447         }
1448
1449         /* read for new ost index or for empty file */
1450         memset(idarray, 0, size);
1451         if (lgi->lgi_attr.la_size <= lgi->lgi_off)
1452                 GOTO(out, rc = 0);
1453         if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
1454                 size = lgi->lgi_attr.la_size - lgi->lgi_off;
1455
1456         lgi->lgi_buf.lb_buf = idarray;
1457         lgi->lgi_buf.lb_len = size;
1458         rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
1459         if (rc) {
1460                 CERROR("%s: error reading CATALOGS: rc = %d\n",
1461                        o->do_lu.lo_dev->ld_obd->obd_name,  rc);
1462                 GOTO(out, rc);
1463         }
1464
1465         EXIT;
1466 out:
1467         lu_object_put(env, &o->do_lu);
1468         RETURN(rc);
1469 }
1470 EXPORT_SYMBOL(llog_osd_get_cat_list);
1471
1472 /**
1473  * Write the special file which contains the list of llog catalogs IDs
1474  *
1475  * This function writes the CATALOG file which contains the array of llog
1476  * catalogs IDs. It is used mostly to store OSP llogs indexed by OST/MDT
1477  * number.
1478  *
1479  * \param[in]  env      execution environment
1480  * \param[in]  d        corresponding storage device
1481  * \param[in]  idx      position to start from, usually OST/MDT index
1482  * \param[in]  count    how many catalog IDs to write
1483  * \param[out] idarray  the buffer with the data to write.
1484  * \param[in]  fid      LLOG_CATALOGS_OID for CATALOG object
1485  *
1486  * \retval              0 on successful write of catalog IDs
1487  * \retval              negative value on error
1488  */
1489 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
1490                           int idx, int count, struct llog_catid *idarray,
1491                           const struct lu_fid *fid)
1492 {
1493         struct llog_thread_info *lgi = llog_info(env);
1494         struct dt_object        *o = NULL;
1495         struct thandle          *th;
1496         int                      rc, size;
1497
1498         if (count == 0)
1499                 RETURN(0);
1500
1501         LASSERT(d);
1502
1503         size = sizeof(*idarray) * count;
1504         lgi->lgi_off = idx * sizeof(*idarray);
1505         lgi->lgi_fid = *fid;
1506
1507         o = dt_locate(env, d, &lgi->lgi_fid);
1508         if (IS_ERR(o))
1509                 RETURN(PTR_ERR(o));
1510
1511         if (!dt_object_exists(o))
1512                 GOTO(out, rc = -ENOENT);
1513
1514         rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
1515         if (rc)
1516                 GOTO(out, rc);
1517
1518         if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1519                 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1520                        o->do_lu.lo_dev->ld_obd->obd_name,
1521                        lgi->lgi_attr.la_mode);
1522                 GOTO(out, rc = -ENOENT);
1523         }
1524
1525         th = dt_trans_create(env, d);
1526         if (IS_ERR(th))
1527                 GOTO(out, rc = PTR_ERR(th));
1528
1529         lgi->lgi_buf.lb_len = size;
1530         lgi->lgi_buf.lb_buf = idarray;
1531         rc = dt_declare_record_write(env, o, &lgi->lgi_buf, lgi->lgi_off, th);
1532         if (rc)
1533                 GOTO(out, rc);
1534
1535         rc = dt_trans_start_local(env, d, th);
1536         if (rc)
1537                 GOTO(out_trans, rc);
1538
1539         rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
1540         if (rc)
1541                 CDEBUG(D_INODE, "can't write CATALOGS at index %d: rc = %d\n",
1542                        idx, rc);
1543 out_trans:
1544         dt_trans_stop(env, d, th);
1545 out:
1546         lu_object_put(env, &o->do_lu);
1547         RETURN(rc);
1548 }
1549 EXPORT_SYMBOL(llog_osd_put_cat_list);