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