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