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