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