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