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