Whamcloud - gitweb
LU-2843 llog: restore the size of llog for -ENOSPC
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdclass/llog_osd.c - low level llog routines on top of OSD API
37  *
38  * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
39  * Author: Mikhail Pershin <mike.pershin@intel.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_LOG
43
44 #ifndef EXPORT_SYMTAB
45 #define EXPORT_SYMTAB
46 #endif
47
48 #include <obd.h>
49 #include <obd_class.h>
50 #include <lustre_fid.h>
51 #include <dt_object.h>
52
53 #include "llog_internal.h"
54 #include "local_storage.h"
55
56 /*
57  * - multi-chunks or big-declaration approach
58  * - use unique sequence instead of llog sb tracking unique ids
59  * - re-use existing environment
60  * - named llog support (can be used for testing only at the present)
61  * - llog_origin_connect() work with OSD API
62  */
63
64 static int llog_osd_declare_new_object(const struct lu_env *env,
65                                        struct local_oid_storage *los,
66                                        struct dt_object *o,
67                                        struct thandle *th)
68 {
69         struct llog_thread_info *lgi = llog_info(env);
70
71         lgi->lgi_attr.la_valid = LA_MODE;
72         lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
73         lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
74
75         return local_object_declare_create(env, los, o, &lgi->lgi_attr,
76                                            &lgi->lgi_dof, th);
77 }
78
79 static int llog_osd_create_new_object(const struct lu_env *env,
80                                       struct local_oid_storage *los,
81                                       struct dt_object *o,
82                                       struct thandle *th)
83 {
84         struct llog_thread_info *lgi = llog_info(env);
85
86         lgi->lgi_attr.la_valid = LA_MODE;
87         lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
88         lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
89
90         return local_object_create(env, los, o, &lgi->lgi_attr,
91                                    &lgi->lgi_dof, th);
92 }
93
94 static int llog_osd_pad(const struct lu_env *env, struct dt_object *o,
95                         loff_t *off, int len, int index, struct thandle *th)
96 {
97         struct llog_thread_info *lgi = llog_info(env);
98         int                      rc;
99
100         ENTRY;
101
102         LASSERT(th);
103         LASSERT(off);
104         LASSERT(len >= LLOG_MIN_REC_SIZE && (len & 0x7) == 0);
105
106         lgi->lgi_tail.lrt_len = lgi->lgi_lrh.lrh_len = len;
107         lgi->lgi_tail.lrt_index = lgi->lgi_lrh.lrh_index = index;
108         lgi->lgi_lrh.lrh_type = LLOG_PAD_MAGIC;
109
110         lgi->lgi_buf.lb_buf = &lgi->lgi_lrh;
111         lgi->lgi_buf.lb_len = sizeof(lgi->lgi_lrh);
112         dt_write_lock(env, o, 0);
113         rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
114         if (rc) {
115                 CERROR("%s: error writing padding record: rc = %d\n",
116                        o->do_lu.lo_dev->ld_obd->obd_name, rc);
117                 GOTO(out, rc);
118         }
119
120         lgi->lgi_buf.lb_buf = &lgi->lgi_tail;
121         lgi->lgi_buf.lb_len = sizeof(lgi->lgi_tail);
122         *off += len - sizeof(lgi->lgi_lrh) - sizeof(lgi->lgi_tail);
123         rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
124         if (rc)
125                 CERROR("%s: error writing padding record: rc = %d\n",
126                        o->do_lu.lo_dev->ld_obd->obd_name, rc);
127 out:
128         dt_write_unlock(env, o);
129         RETURN(rc);
130 }
131
132 static int llog_osd_write_blob(const struct lu_env *env, struct dt_object *o,
133                                struct llog_rec_hdr *rec, void *buf,
134                                loff_t *off, struct thandle *th)
135 {
136         struct llog_thread_info *lgi = llog_info(env);
137         int                      buflen = rec->lrh_len;
138         int                      rc;
139
140         ENTRY;
141
142         LASSERT(env);
143         LASSERT(o);
144
145         if (buflen == 0)
146                 CWARN("0-length record\n");
147
148         CDEBUG(D_OTHER, "write blob with type %x, buf %p/%u at off %llu\n",
149                rec->lrh_type, buf, buflen, *off);
150
151         lgi->lgi_attr.la_valid = LA_SIZE;
152         lgi->lgi_attr.la_size = *off;
153
154         if (!buf) {
155                 lgi->lgi_buf.lb_len = buflen;
156                 lgi->lgi_buf.lb_buf = rec;
157                 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
158                 if (rc)
159                         CERROR("%s: error writing log record: rc = %d\n",
160                                o->do_lu.lo_dev->ld_obd->obd_name, rc);
161                 GOTO(out, rc);
162         }
163
164         /* the buf case */
165         /* protect the following 3 writes from concurrent read */
166         dt_write_lock(env, o, 0);
167         rec->lrh_len = sizeof(*rec) + buflen + sizeof(lgi->lgi_tail);
168         lgi->lgi_buf.lb_len = sizeof(*rec);
169         lgi->lgi_buf.lb_buf = rec;
170         rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
171         if (rc) {
172                 CERROR("%s: error writing log hdr: rc = %d\n",
173                        o->do_lu.lo_dev->ld_obd->obd_name, rc);
174                 GOTO(out_unlock, rc);
175         }
176
177         lgi->lgi_buf.lb_len = buflen;
178         lgi->lgi_buf.lb_buf = buf;
179         rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
180         if (rc) {
181                 CERROR("%s: error writing log buffer: rc = %d\n",
182                        o->do_lu.lo_dev->ld_obd->obd_name,  rc);
183                 GOTO(out_unlock, rc);
184         }
185
186         lgi->lgi_tail.lrt_len = rec->lrh_len;
187         lgi->lgi_tail.lrt_index = rec->lrh_index;
188         lgi->lgi_buf.lb_len = sizeof(lgi->lgi_tail);
189         lgi->lgi_buf.lb_buf = &lgi->lgi_tail;
190         rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
191         if (rc)
192                 CERROR("%s: error writing log tail: rc = %d\n",
193                        o->do_lu.lo_dev->ld_obd->obd_name, rc);
194
195 out_unlock:
196         dt_write_unlock(env, o);
197
198 out:
199         /* cleanup the content written above */
200         if (rc) {
201                 dt_punch(env, o, lgi->lgi_attr.la_size, OBD_OBJECT_EOF, th,
202                          BYPASS_CAPA);
203                 dt_attr_set(env, o, &lgi->lgi_attr, th, BYPASS_CAPA);
204         }
205
206         RETURN(rc);
207 }
208
209 static int llog_osd_read_header(const struct lu_env *env,
210                                 struct llog_handle *handle)
211 {
212         struct llog_rec_hdr     *llh_hdr;
213         struct dt_object        *o;
214         struct llog_thread_info *lgi;
215         int                      rc;
216
217         ENTRY;
218
219         LASSERT(sizeof(*handle->lgh_hdr) == LLOG_CHUNK_SIZE);
220
221         o = handle->lgh_obj;
222         LASSERT(o);
223
224         lgi = llog_info(env);
225
226         rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
227         if (rc)
228                 RETURN(rc);
229
230         LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
231
232         if (lgi->lgi_attr.la_size == 0) {
233                 CDEBUG(D_HA, "not reading header from 0-byte log\n");
234                 RETURN(LLOG_EEMPTY);
235         }
236
237         lgi->lgi_off = 0;
238         lgi->lgi_buf.lb_buf = handle->lgh_hdr;
239         lgi->lgi_buf.lb_len = LLOG_CHUNK_SIZE;
240
241         rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
242         if (rc) {
243                 CERROR("%s: error reading log header from "DFID": rc = %d\n",
244                        o->do_lu.lo_dev->ld_obd->obd_name,
245                        PFID(lu_object_fid(&o->do_lu)), rc);
246                 RETURN(rc);
247         }
248
249         llh_hdr = &handle->lgh_hdr->llh_hdr;
250         if (LLOG_REC_HDR_NEEDS_SWABBING(llh_hdr))
251                 lustre_swab_llog_hdr(handle->lgh_hdr);
252
253         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
254                 CERROR("%s: bad log %s "DFID" header magic: %#x "
255                        "(expected %#x)\n", o->do_lu.lo_dev->ld_obd->obd_name,
256                        handle->lgh_name ? handle->lgh_name : "",
257                        PFID(lu_object_fid(&o->do_lu)),
258                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
259                 RETURN(-EIO);
260         } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
261                 CERROR("%s: incorrectly sized log %s "DFID" header: "
262                        "%#x (expected %#x)\n"
263                        "you may need to re-run lconf --write_conf.\n",
264                        o->do_lu.lo_dev->ld_obd->obd_name,
265                        handle->lgh_name ? handle->lgh_name : "",
266                        PFID(lu_object_fid(&o->do_lu)),
267                        llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
268                 RETURN(-EIO);
269         }
270
271         handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
272
273         RETURN(0);
274 }
275
276 static int llog_osd_declare_write_rec(const struct lu_env *env,
277                                       struct llog_handle *loghandle,
278                                       struct llog_rec_hdr *rec,
279                                       int idx, struct thandle *th)
280 {
281         struct llog_thread_info *lgi = llog_info(env);
282         struct dt_object        *o;
283         int                      rc;
284
285         ENTRY;
286
287         LASSERT(env);
288         LASSERT(th);
289         LASSERT(loghandle);
290
291         o = loghandle->lgh_obj;
292         LASSERT(o);
293
294         /* each time we update header */
295         rc = dt_declare_record_write(env, o, sizeof(struct llog_log_hdr), 0,
296                                      th);
297         if (rc || idx == 0) /* if error or just header */
298                 RETURN(rc);
299
300         if (dt_object_exists(o)) {
301                 rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
302                 lgi->lgi_off = lgi->lgi_attr.la_size;
303                 LASSERT(ergo(rc == 0, lgi->lgi_attr.la_valid & LA_SIZE));
304                 if (rc)
305                         RETURN(rc);
306
307                 rc = dt_declare_punch(env, o, lgi->lgi_off, OBD_OBJECT_EOF, th);
308                 if (rc)
309                         RETURN(rc);
310         } else {
311                 lgi->lgi_off = 0;
312         }
313
314         /* XXX: implement declared window or multi-chunks approach */
315         rc = dt_declare_record_write(env, o, 32 * 1024, lgi->lgi_off, th);
316
317         RETURN(rc);
318 }
319
320 /* returns negative in on error; 0 if success && reccookie == 0; 1 otherwise */
321 /* appends if idx == -1, otherwise overwrites record idx. */
322 static int llog_osd_write_rec(const struct lu_env *env,
323                               struct llog_handle *loghandle,
324                               struct llog_rec_hdr *rec,
325                               struct llog_cookie *reccookie, int cookiecount,
326                               void *buf, int idx, struct thandle *th)
327 {
328         struct llog_thread_info *lgi = llog_info(env);
329         struct llog_log_hdr     *llh;
330         int                      reclen = rec->lrh_len;
331         int                      index, rc, old_tail_idx;
332         struct llog_rec_tail    *lrt;
333         struct dt_object        *o;
334         size_t                   left;
335
336         ENTRY;
337
338         LASSERT(env);
339         llh = loghandle->lgh_hdr;
340         LASSERT(llh);
341         o = loghandle->lgh_obj;
342         LASSERT(o);
343         LASSERT(th);
344
345         CDEBUG(D_OTHER, "new record %x to "DFID"\n",
346                rec->lrh_type, PFID(lu_object_fid(&o->do_lu)));
347
348         /* record length should not bigger than LLOG_CHUNK_SIZE */
349         if (buf)
350                 rc = (reclen > LLOG_CHUNK_SIZE - sizeof(struct llog_rec_hdr) -
351                       sizeof(struct llog_rec_tail)) ? -E2BIG : 0;
352         else
353                 rc = (reclen > LLOG_CHUNK_SIZE) ? -E2BIG : 0;
354         if (rc)
355                 RETURN(rc);
356
357         rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
358         if (rc)
359                 RETURN(rc);
360
361         if (buf)
362                 /* write_blob adds header and tail to lrh_len. */
363                 reclen = sizeof(*rec) + rec->lrh_len +
364                          sizeof(struct llog_rec_tail);
365
366         if (idx != -1) {
367                 /* no header: only allowed to insert record 1 */
368                 if (idx != 1 && lgi->lgi_attr.la_size == 0)
369                         LBUG();
370
371                 if (idx && llh->llh_size && llh->llh_size != rec->lrh_len)
372                         RETURN(-EINVAL);
373
374                 if (!ext2_test_bit(idx, llh->llh_bitmap))
375                         CERROR("%s: modify unset record %u\n",
376                                o->do_lu.lo_dev->ld_obd->obd_name, idx);
377                 if (idx != rec->lrh_index)
378                         CERROR("%s: index mismatch %d %u\n",
379                                o->do_lu.lo_dev->ld_obd->obd_name, idx,
380                                rec->lrh_index);
381
382                 lgi->lgi_off = 0;
383                 rc = llog_osd_write_blob(env, o, &llh->llh_hdr, NULL,
384                                          &lgi->lgi_off, th);
385                 /* we are done if we only write the header or on error */
386                 if (rc || idx == 0)
387                         RETURN(rc);
388
389                 if (buf) {
390                         /* We assume that caller has set lgh_cur_* */
391                         lgi->lgi_off = loghandle->lgh_cur_offset;
392                         CDEBUG(D_OTHER,
393                                "modify record "LPX64": idx:%d/%u/%d, len:%u "
394                                "offset %llu\n",
395                                loghandle->lgh_id.lgl_oid, idx, rec->lrh_index,
396                                loghandle->lgh_cur_idx, rec->lrh_len,
397                                (long long)(lgi->lgi_off - sizeof(*llh)));
398                         if (rec->lrh_index != loghandle->lgh_cur_idx) {
399                                 CERROR("%s: modify idx mismatch %u/%d\n",
400                                        o->do_lu.lo_dev->ld_obd->obd_name, idx,
401                                        loghandle->lgh_cur_idx);
402                                 RETURN(-EFAULT);
403                         }
404                 } else {
405                         /* Assumes constant lrh_len */
406                         lgi->lgi_off = sizeof(*llh) + (idx - 1) * reclen;
407                 }
408
409                 rc = llog_osd_write_blob(env, o, rec, buf, &lgi->lgi_off, th);
410                 if (rc == 0 && reccookie) {
411                         reccookie->lgc_lgl = loghandle->lgh_id;
412                         reccookie->lgc_index = idx;
413                         rc = 1;
414                 }
415                 RETURN(rc);
416         }
417
418         /* Make sure that records don't cross a chunk boundary, so we can
419          * process them page-at-a-time if needed.  If it will cross a chunk
420          * boundary, write in a fake (but referenced) entry to pad the chunk.
421          *
422          * We know that llog_current_log() will return a loghandle that is
423          * big enough to hold reclen, so all we care about is padding here.
424          */
425         LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
426         lgi->lgi_off = lgi->lgi_attr.la_size;
427         left = LLOG_CHUNK_SIZE - (lgi->lgi_off & (LLOG_CHUNK_SIZE - 1));
428         /* NOTE: padding is a record, but no bit is set */
429         if (left != 0 && left != reclen &&
430             left < (reclen + LLOG_MIN_REC_SIZE)) {
431                 index = loghandle->lgh_last_idx + 1;
432                 rc = llog_osd_pad(env, o, &lgi->lgi_off, left, index, th);
433                 if (rc)
434                         RETURN(rc);
435                 loghandle->lgh_last_idx++; /*for pad rec*/
436         }
437         /* if it's the last idx in log file, then return -ENOSPC */
438         if (loghandle->lgh_last_idx >= LLOG_BITMAP_SIZE(llh) - 1)
439                 RETURN(-ENOSPC);
440
441         loghandle->lgh_last_idx++;
442         index = loghandle->lgh_last_idx;
443         LASSERT(index < LLOG_BITMAP_SIZE(llh));
444         rec->lrh_index = index;
445         if (buf == NULL) {
446                 lrt = (struct llog_rec_tail *)((char *)rec + rec->lrh_len -
447                                                sizeof(*lrt));
448                 lrt->lrt_len = rec->lrh_len;
449                 lrt->lrt_index = rec->lrh_index;
450         }
451         /* The caller should make sure only 1 process access the lgh_last_idx,
452          * Otherwise it might hit the assert.*/
453         LASSERT(index < LLOG_BITMAP_SIZE(llh));
454         spin_lock(&loghandle->lgh_hdr_lock);
455         if (ext2_set_bit(index, llh->llh_bitmap)) {
456                 CERROR("%s: index %u already set in log bitmap\n",
457                        o->do_lu.lo_dev->ld_obd->obd_name, index);
458                 spin_unlock(&loghandle->lgh_hdr_lock);
459                 LBUG(); /* should never happen */
460         }
461         llh->llh_count++;
462         spin_unlock(&loghandle->lgh_hdr_lock);
463         old_tail_idx = llh->llh_tail.lrt_index;
464         llh->llh_tail.lrt_index = index;
465
466         lgi->lgi_off = 0;
467         rc = llog_osd_write_blob(env, o, &llh->llh_hdr, NULL, &lgi->lgi_off,
468                                  th);
469         if (rc)
470                 GOTO(out, rc);
471
472         rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
473         if (rc)
474                 GOTO(out, rc);
475
476         LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
477         lgi->lgi_off = lgi->lgi_attr.la_size;
478
479         rc = llog_osd_write_blob(env, o, rec, buf, &lgi->lgi_off, th);
480
481 out:
482         /* cleanup llog for error case */
483         if (rc) {
484                 spin_lock(&loghandle->lgh_hdr_lock);
485                 ext2_clear_bit(index, llh->llh_bitmap);
486                 llh->llh_count--;
487                 spin_unlock(&loghandle->lgh_hdr_lock);
488
489                 /* restore the header */
490                 loghandle->lgh_last_idx--;
491                 llh->llh_tail.lrt_index = old_tail_idx;
492                 lgi->lgi_off = 0;
493                 llog_osd_write_blob(env, o, &llh->llh_hdr, NULL,
494                                     &lgi->lgi_off, th);
495         }
496
497         CDEBUG(D_RPCTRACE, "added record "LPX64": idx: %u, %u\n",
498                loghandle->lgh_id.lgl_oid, index, rec->lrh_len);
499         if (rc == 0 && reccookie) {
500                 reccookie->lgc_lgl = loghandle->lgh_id;
501                 reccookie->lgc_index = index;
502                 if ((rec->lrh_type == MDS_UNLINK_REC) ||
503                     (rec->lrh_type == MDS_SETATTR64_REC))
504                         reccookie->lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
505                 else if (rec->lrh_type == OST_SZ_REC)
506                         reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
507                 else
508                         reccookie->lgc_subsys = -1;
509                 rc = 1;
510         }
511         RETURN(rc);
512 }
513
514 /* We can skip reading at least as many log blocks as the number of
515  * minimum sized log records we are skipping.  If it turns out
516  * that we are not far enough along the log (because the
517  * actual records are larger than minimum size) we just skip
518  * some more records.
519  */
520 static void llog_skip_over(__u64 *off, int curr, int goal)
521 {
522         if (goal <= curr)
523                 return;
524         *off = (*off + (goal - curr - 1) * LLOG_MIN_REC_SIZE) &
525                 ~(LLOG_CHUNK_SIZE - 1);
526 }
527
528 /* sets:
529  *  - cur_offset to the furthest point read in the log file
530  *  - cur_idx to the log index preceeding cur_offset
531  * returns -EIO/-EINVAL on error
532  */
533 static int llog_osd_next_block(const struct lu_env *env,
534                                struct llog_handle *loghandle, int *cur_idx,
535                                int next_idx, __u64 *cur_offset, void *buf,
536                                int len)
537 {
538         struct llog_thread_info *lgi = llog_info(env);
539         struct dt_object        *o;
540         struct dt_device        *dt;
541         int                      rc;
542
543         ENTRY;
544
545         LASSERT(env);
546         LASSERT(lgi);
547
548         if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
549                 RETURN(-EINVAL);
550
551         CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off "LPU64")\n",
552                next_idx, *cur_idx, *cur_offset);
553
554         LASSERT(loghandle);
555         LASSERT(loghandle->lgh_ctxt);
556
557         o = loghandle->lgh_obj;
558         LASSERT(o);
559         LASSERT(dt_object_exists(o));
560         dt = lu2dt_dev(o->do_lu.lo_dev);
561         LASSERT(dt);
562
563         rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
564         if (rc)
565                 GOTO(out, rc);
566
567         while (*cur_offset < lgi->lgi_attr.la_size) {
568                 struct llog_rec_hdr     *rec, *last_rec;
569                 struct llog_rec_tail    *tail;
570
571                 llog_skip_over(cur_offset, *cur_idx, next_idx);
572
573                 /* read up to next LLOG_CHUNK_SIZE block */
574                 lgi->lgi_buf.lb_len = LLOG_CHUNK_SIZE -
575                                       (*cur_offset & (LLOG_CHUNK_SIZE - 1));
576                 lgi->lgi_buf.lb_buf = buf;
577
578                 /* Note: read lock is not needed around la_size get above at
579                  * the time of dt_attr_get(). There are only two cases that
580                  * matter. Either la_size == cur_offset, in which case the
581                  * entire read is skipped, or la_size > cur_offset and the loop
582                  * is entered and this thread is blocked at dt_read_lock()
583                  * until the write is completed. When the write completes, then
584                  * the dt_read() will be done with the full length, and will
585                  * get the full data.
586                  */
587                 dt_read_lock(env, o, 0);
588                 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
589                 dt_read_unlock(env, o);
590                 if (rc < 0) {
591                         CERROR("%s: can't read llog block from log "DFID
592                                " offset "LPU64": rc = %d\n",
593                                o->do_lu.lo_dev->ld_obd->obd_name,
594                                PFID(lu_object_fid(&o->do_lu)), *cur_offset,
595                                rc);
596                         GOTO(out, rc);
597                 }
598
599                 if (rc < len) {
600                         /* signal the end of the valid buffer to
601                          * llog_process */
602                         memset(buf + rc, 0, len - rc);
603                 }
604
605                 if (rc == 0) /* end of file, nothing to do */
606                         GOTO(out, rc);
607
608                 if (rc < sizeof(*tail)) {
609                         CERROR("%s: invalid llog block at log id "LPU64"/%u "
610                                "offset "LPU64"\n",
611                                o->do_lu.lo_dev->ld_obd->obd_name,
612                                loghandle->lgh_id.lgl_oid,
613                                loghandle->lgh_id.lgl_ogen, *cur_offset);
614                         GOTO(out, rc = -EINVAL);
615                 }
616
617                 rec = buf;
618                 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
619                         lustre_swab_llog_rec(rec);
620
621                 tail = (struct llog_rec_tail *)((char *)buf + rc -
622                                                 sizeof(struct llog_rec_tail));
623                 /* get the last record in block */
624                 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
625                                                    le32_to_cpu(tail->lrt_len));
626
627                 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
628                         lustre_swab_llog_rec(last_rec);
629                 LASSERT(last_rec->lrh_index == tail->lrt_index);
630
631                 *cur_idx = tail->lrt_index;
632
633                 /* this shouldn't happen */
634                 if (tail->lrt_index == 0) {
635                         CERROR("%s: invalid llog tail at log id "LPU64"/%u "
636                                "offset "LPU64"\n",
637                                o->do_lu.lo_dev->ld_obd->obd_name,
638                                loghandle->lgh_id.lgl_oid,
639                                loghandle->lgh_id.lgl_ogen, *cur_offset);
640                         GOTO(out, rc = -EINVAL);
641                 }
642                 if (tail->lrt_index < next_idx)
643                         continue;
644
645                 /* sanity check that the start of the new buffer is no farther
646                  * than the record that we wanted.  This shouldn't happen. */
647                 if (rec->lrh_index > next_idx) {
648                         CERROR("%s: missed desired record? %u > %u\n",
649                                o->do_lu.lo_dev->ld_obd->obd_name,
650                                rec->lrh_index, next_idx);
651                         GOTO(out, rc = -ENOENT);
652                 }
653                 GOTO(out, rc = 0);
654         }
655         GOTO(out, rc = -EIO);
656 out:
657         return rc;
658 }
659
660 static int llog_osd_prev_block(const struct lu_env *env,
661                                struct llog_handle *loghandle,
662                                int prev_idx, void *buf, int len)
663 {
664         struct llog_thread_info *lgi = llog_info(env);
665         struct dt_object        *o;
666         struct dt_device        *dt;
667         loff_t                   cur_offset;
668         int                      rc;
669
670         ENTRY;
671
672         if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
673                 RETURN(-EINVAL);
674
675         CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
676
677         LASSERT(loghandle);
678         LASSERT(loghandle->lgh_ctxt);
679
680         o = loghandle->lgh_obj;
681         LASSERT(o);
682         LASSERT(dt_object_exists(o));
683         dt = lu2dt_dev(o->do_lu.lo_dev);
684         LASSERT(dt);
685
686         cur_offset = LLOG_CHUNK_SIZE;
687         llog_skip_over(&cur_offset, 0, prev_idx);
688
689         rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
690         if (rc)
691                 GOTO(out, rc);
692
693         while (cur_offset < lgi->lgi_attr.la_size) {
694                 struct llog_rec_hdr     *rec, *last_rec;
695                 struct llog_rec_tail    *tail;
696
697                 lgi->lgi_buf.lb_len = len;
698                 lgi->lgi_buf.lb_buf = buf;
699                 /* It is OK to have locking around dt_read() only, see
700                  * comment in llog_osd_next_block for details
701                  */
702                 dt_read_lock(env, o, 0);
703                 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
704                 dt_read_unlock(env, o);
705                 if (rc < 0) {
706                         CERROR("%s: can't read llog block from log "DFID
707                                " offset "LPU64": rc = %d\n",
708                                o->do_lu.lo_dev->ld_obd->obd_name,
709                                PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
710                         GOTO(out, rc);
711                 }
712
713                 if (rc == 0) /* end of file, nothing to do */
714                         GOTO(out, rc);
715
716                 if (rc < sizeof(*tail)) {
717                         CERROR("%s: invalid llog block at log id "LPU64"/%u "
718                                "offset "LPU64"\n",
719                                o->do_lu.lo_dev->ld_obd->obd_name,
720                                loghandle->lgh_id.lgl_oid,
721                                loghandle->lgh_id.lgl_ogen, cur_offset);
722                         GOTO(out, rc = -EINVAL);
723                 }
724
725                 rec = buf;
726                 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
727                         lustre_swab_llog_rec(rec);
728
729                 tail = (struct llog_rec_tail *)((char *)buf + rc -
730                                                 sizeof(struct llog_rec_tail));
731                 /* get the last record in block */
732                 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
733                                                    le32_to_cpu(tail->lrt_len));
734
735                 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
736                         lustre_swab_llog_rec(last_rec);
737                 LASSERT(last_rec->lrh_index == tail->lrt_index);
738
739                 /* this shouldn't happen */
740                 if (tail->lrt_index == 0) {
741                         CERROR("%s: invalid llog tail at log id "LPU64"/%u "
742                                "offset "LPU64"\n",
743                                o->do_lu.lo_dev->ld_obd->obd_name,
744                                loghandle->lgh_id.lgl_oid,
745                                loghandle->lgh_id.lgl_ogen, cur_offset);
746                         GOTO(out, rc = -EINVAL);
747                 }
748                 if (tail->lrt_index < prev_idx)
749                         continue;
750
751                 /* sanity check that the start of the new buffer is no farther
752                  * than the record that we wanted.  This shouldn't happen. */
753                 if (rec->lrh_index > prev_idx) {
754                         CERROR("%s: missed desired record? %u > %u\n",
755                                o->do_lu.lo_dev->ld_obd->obd_name,
756                                rec->lrh_index, prev_idx);
757                         GOTO(out, rc = -ENOENT);
758                 }
759                 GOTO(out, rc = 0);
760         }
761         GOTO(out, rc = -EIO);
762 out:
763         return rc;
764 }
765
766 struct dt_object *llog_osd_dir_get(const struct lu_env *env,
767                                    struct llog_ctxt *ctxt)
768 {
769         struct dt_device        *dt;
770         struct dt_thread_info   *dti = dt_info(env);
771         struct dt_object        *dir;
772         int                      rc;
773
774         dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
775         if (ctxt->loc_dir == NULL) {
776                 rc = dt_root_get(env, dt, &dti->dti_fid);
777                 if (rc)
778                         return ERR_PTR(rc);
779                 dir = dt_locate(env, dt, &dti->dti_fid);
780         } else {
781                 lu_object_get(&ctxt->loc_dir->do_lu);
782                 dir = ctxt->loc_dir;
783         }
784
785         return dir;
786 }
787
788 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
789                          struct llog_logid *logid, char *name,
790                          enum llog_open_param open_param)
791 {
792         struct llog_thread_info         *lgi = llog_info(env);
793         struct llog_ctxt                *ctxt = handle->lgh_ctxt;
794         struct dt_object                *o;
795         struct dt_device                *dt;
796         struct ls_device                *ls;
797         struct local_oid_storage        *los;
798         int                              rc = 0;
799
800         ENTRY;
801
802         LASSERT(env);
803         LASSERT(ctxt);
804         LASSERT(ctxt->loc_exp);
805         LASSERT(ctxt->loc_exp->exp_obd);
806         dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
807         LASSERT(dt);
808
809         ls = ls_device_get(dt);
810         if (IS_ERR(ls))
811                 RETURN(PTR_ERR(ls));
812
813         mutex_lock(&ls->ls_los_mutex);
814         los = dt_los_find(ls, FID_SEQ_LLOG);
815         mutex_unlock(&ls->ls_los_mutex);
816         LASSERT(los);
817         ls_device_put(env, ls);
818
819         LASSERT(handle);
820
821         if (logid != NULL) {
822                 logid_to_fid(logid, &lgi->lgi_fid);
823         } else if (name) {
824                 struct dt_object *llog_dir;
825
826                 llog_dir = llog_osd_dir_get(env, ctxt);
827                 if (IS_ERR(llog_dir))
828                         GOTO(out, rc = PTR_ERR(llog_dir));
829                 dt_read_lock(env, llog_dir, 0);
830                 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
831                 dt_read_unlock(env, llog_dir);
832                 lu_object_put(env, &llog_dir->do_lu);
833                 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
834                         /* generate fid for new llog */
835                         rc = local_object_fid_generate(env, los,
836                                                        &lgi->lgi_fid);
837                 }
838                 if (rc < 0)
839                         GOTO(out, rc);
840                 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
841                 if (handle->lgh_name)
842                         strcpy(handle->lgh_name, name);
843                 else
844                         GOTO(out, rc = -ENOMEM);
845         } else {
846                 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
847                 /* generate fid for new llog */
848                 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
849                 if (rc < 0)
850                         GOTO(out, rc);
851         }
852
853         o = ls_locate(env, ls, &lgi->lgi_fid);
854         if (IS_ERR(o))
855                 GOTO(out_name, rc = PTR_ERR(o));
856
857         /* No new llog is expected but doesn't exist */
858         if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o))
859                 GOTO(out_put, rc = -ENOENT);
860
861         fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
862         handle->lgh_obj = o;
863         handle->private_data = los;
864         LASSERT(handle->lgh_ctxt);
865
866         RETURN(rc);
867
868 out_put:
869         lu_object_put(env, &o->do_lu);
870 out_name:
871         if (handle->lgh_name != NULL)
872                 OBD_FREE(handle->lgh_name, strlen(name) + 1);
873 out:
874         dt_los_put(los);
875         RETURN(rc);
876 }
877
878 static int llog_osd_exist(struct llog_handle *handle)
879 {
880         LASSERT(handle->lgh_obj);
881         return (dt_object_exists(handle->lgh_obj) &&
882                 !lu_object_is_dying(handle->lgh_obj->do_lu.lo_header));
883 }
884
885 static int llog_osd_declare_create(const struct lu_env *env,
886                                    struct llog_handle *res, struct thandle *th)
887 {
888         struct llog_thread_info         *lgi = llog_info(env);
889         struct local_oid_storage        *los;
890         struct dt_object                *o;
891         int                              rc;
892
893         ENTRY;
894
895         LASSERT(res->lgh_obj);
896         LASSERT(th);
897
898         /* object can be created by another thread */
899         o = res->lgh_obj;
900         if (dt_object_exists(o))
901                 RETURN(0);
902
903         los = res->private_data;
904         LASSERT(los);
905
906         rc = llog_osd_declare_new_object(env, los, o, th);
907         if (rc)
908                 RETURN(rc);
909
910         rc = dt_declare_record_write(env, o, LLOG_CHUNK_SIZE, 0, th);
911         if (rc)
912                 RETURN(rc);
913
914         if (res->lgh_name) {
915                 struct dt_object *llog_dir;
916
917                 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
918                 if (IS_ERR(llog_dir))
919                         RETURN(PTR_ERR(llog_dir));
920                 dt_declare_ref_add(env, o, th);
921                 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
922                 rc = dt_declare_insert(env, llog_dir,
923                                        (struct dt_rec *)&lgi->lgi_fid,
924                                        (struct dt_key *)res->lgh_name, th);
925                 lu_object_put(env, &llog_dir->do_lu);
926                 if (rc)
927                         CERROR("%s: can't declare named llog %s: rc = %d\n",
928                                o->do_lu.lo_dev->ld_obd->obd_name,
929                                res->lgh_name, rc);
930         }
931         RETURN(rc);
932 }
933
934 /* This is a callback from the llog_* functions.
935  * Assumes caller has already pushed us into the kernel context. */
936 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
937                            struct thandle *th)
938 {
939         struct llog_thread_info *lgi = llog_info(env);
940         struct local_oid_storage *los;
941         struct dt_object        *o;
942         int                      rc = 0;
943
944         ENTRY;
945
946         LASSERT(env);
947         o = res->lgh_obj;
948         LASSERT(o);
949
950         /* llog can be already created */
951         if (dt_object_exists(o))
952                 RETURN(-EEXIST);
953
954         los = res->private_data;
955         LASSERT(los);
956
957         dt_write_lock(env, o, 0);
958         if (!dt_object_exists(o))
959                 rc = llog_osd_create_new_object(env, los, o, th);
960         else
961                 rc = -EEXIST;
962         if (res->lgh_name)
963                 dt_ref_add(env, o, th);
964         dt_write_unlock(env, o);
965         if (rc)
966                 RETURN(rc);
967
968         if (res->lgh_name) {
969                 struct dt_object *llog_dir;
970
971                 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
972                 if (IS_ERR(llog_dir))
973                         RETURN(PTR_ERR(llog_dir));
974
975                 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
976                 dt_read_lock(env, llog_dir, 0);
977                 rc = dt_insert(env, llog_dir,
978                                (struct dt_rec *)&lgi->lgi_fid,
979                                (struct dt_key *)res->lgh_name,
980                                th, BYPASS_CAPA, 1);
981                 dt_read_unlock(env, llog_dir);
982                 lu_object_put(env, &llog_dir->do_lu);
983                 if (rc)
984                         CERROR("%s: can't create named llog %s: rc = %d\n",
985                                o->do_lu.lo_dev->ld_obd->obd_name,
986                                res->lgh_name, rc);
987         }
988         RETURN(rc);
989 }
990
991 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
992 {
993         struct local_oid_storage        *los;
994         int                              rc = 0;
995
996         ENTRY;
997
998         LASSERT(handle->lgh_obj);
999
1000         lu_object_put(env, &handle->lgh_obj->do_lu);
1001
1002         los = handle->private_data;
1003         LASSERT(los);
1004         dt_los_put(los);
1005
1006         if (handle->lgh_name)
1007                 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
1008
1009         RETURN(rc);
1010 }
1011
1012 static int llog_osd_destroy(const struct lu_env *env,
1013                             struct llog_handle *loghandle)
1014 {
1015         struct llog_thread_info *lgi = llog_info(env);
1016         struct llog_ctxt        *ctxt;
1017         struct dt_object        *o, *llog_dir = NULL;
1018         struct dt_device        *d;
1019         struct thandle          *th;
1020         char                    *name = NULL;
1021         int                      rc;
1022
1023         ENTRY;
1024
1025         ctxt = loghandle->lgh_ctxt;
1026         LASSERT(ctxt);
1027
1028         o = loghandle->lgh_obj;
1029         LASSERT(o);
1030
1031         d = lu2dt_dev(o->do_lu.lo_dev);
1032         LASSERT(d);
1033         LASSERT(d == ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt);
1034
1035         th = dt_trans_create(env, d);
1036         if (IS_ERR(th))
1037                 RETURN(PTR_ERR(th));
1038
1039         if (loghandle->lgh_name) {
1040                 llog_dir = llog_osd_dir_get(env, ctxt);
1041                 if (IS_ERR(llog_dir))
1042                         GOTO(out_trans, rc = PTR_ERR(llog_dir));
1043
1044                 dt_declare_ref_del(env, o, th);
1045                 name = loghandle->lgh_name;
1046                 rc = dt_declare_delete(env, llog_dir,
1047                                        (struct dt_key *)name, th);
1048                 if (rc)
1049                         GOTO(out_trans, rc);
1050         }
1051
1052         dt_declare_ref_del(env, o, th);
1053
1054         rc = dt_declare_destroy(env, o, th);
1055         if (rc)
1056                 GOTO(out_trans, rc);
1057
1058         rc = dt_trans_start_local(env, d, th);
1059         if (rc)
1060                 GOTO(out_trans, rc);
1061
1062         dt_write_lock(env, o, 0);
1063         if (dt_object_exists(o)) {
1064                 if (name) {
1065                         dt_ref_del(env, o, th);
1066                         dt_read_lock(env, llog_dir, 0);
1067                         rc = dt_delete(env, llog_dir,
1068                                        (struct dt_key *) name,
1069                                        th, BYPASS_CAPA);
1070                         dt_read_unlock(env, llog_dir);
1071                         if (rc) {
1072                                 CERROR("%s: can't remove llog %s: rc = %d\n",
1073                                        o->do_lu.lo_dev->ld_obd->obd_name,
1074                                        name, rc);
1075                                 GOTO(out_unlock, rc);
1076                         }
1077                 }
1078                 /*
1079                  * XXX: compatibility bits
1080                  *      on old filesystems llogs are referenced by the name
1081                  *      on the new ones they are referenced by OI and by
1082                  *      the name
1083                  */
1084                 rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
1085                 if (rc)
1086                         GOTO(out_unlock, rc);
1087                 LASSERT(lgi->lgi_attr.la_nlink < 2);
1088                 if (lgi->lgi_attr.la_nlink == 1)
1089                         dt_ref_del(env, o, th);
1090                 rc = dt_destroy(env, o, th);
1091                 if (rc)
1092                         GOTO(out_unlock, rc);
1093         }
1094 out_unlock:
1095         dt_write_unlock(env, o);
1096 out_trans:
1097         dt_trans_stop(env, d, th);
1098         if (llog_dir != NULL)
1099                 lu_object_put(env, &llog_dir->do_lu);
1100         RETURN(rc);
1101 }
1102
1103 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1104                           struct obd_llog_group *olg, int ctxt_idx,
1105                           struct obd_device *disk_obd)
1106 {
1107         struct local_oid_storage        *los;
1108         struct llog_thread_info         *lgi = llog_info(env);
1109         struct llog_ctxt                *ctxt;
1110         int                              rc = 0;
1111
1112         ENTRY;
1113
1114         LASSERT(obd);
1115         LASSERT(olg->olg_ctxts[ctxt_idx]);
1116
1117         ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1118         LASSERT(ctxt);
1119
1120         /* initialize data allowing to generate new fids,
1121          * literally we need a sequece */
1122         lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1123         lgi->lgi_fid.f_oid = 1;
1124         lgi->lgi_fid.f_ver = 0;
1125         rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1126                                     &lgi->lgi_fid, &los);
1127         llog_ctxt_put(ctxt);
1128         return rc;
1129 }
1130
1131 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1132 {
1133         struct dt_device                *dt;
1134         struct ls_device                *ls;
1135         struct local_oid_storage        *los;
1136
1137         LASSERT(ctxt->loc_exp->exp_obd);
1138         dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1139         ls = ls_device_get(dt);
1140         if (IS_ERR(ls))
1141                 RETURN(PTR_ERR(ls));
1142
1143         mutex_lock(&ls->ls_los_mutex);
1144         los = dt_los_find(ls, FID_SEQ_LLOG);
1145         mutex_unlock(&ls->ls_los_mutex);
1146         if (los != NULL) {
1147                 dt_los_put(los);
1148                 local_oid_storage_fini(env, los);
1149         }
1150         ls_device_put(env, ls);
1151         return 0;
1152 }
1153
1154 struct llog_operations llog_osd_ops = {
1155         .lop_next_block         = llog_osd_next_block,
1156         .lop_prev_block         = llog_osd_prev_block,
1157         .lop_read_header        = llog_osd_read_header,
1158         .lop_destroy            = llog_osd_destroy,
1159         .lop_setup              = llog_osd_setup,
1160         .lop_cleanup            = llog_osd_cleanup,
1161         .lop_open               = llog_osd_open,
1162         .lop_exist              = llog_osd_exist,
1163         .lop_declare_create     = llog_osd_declare_create,
1164         .lop_create             = llog_osd_create,
1165         .lop_declare_write_rec  = llog_osd_declare_write_rec,
1166         .lop_write_rec          = llog_osd_write_rec,
1167         .lop_close              = llog_osd_close,
1168 };
1169 EXPORT_SYMBOL(llog_osd_ops);
1170
1171 /* reads the catalog list */
1172 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
1173                           int idx, int count, struct llog_catid *idarray)
1174 {
1175         struct llog_thread_info *lgi = llog_info(env);
1176         struct dt_object        *o = NULL;
1177         struct thandle          *th;
1178         int                      rc, size;
1179
1180         ENTRY;
1181
1182         LASSERT(d);
1183
1184         size = sizeof(*idarray) * count;
1185         lgi->lgi_off = idx *  sizeof(*idarray);
1186
1187         lu_local_obj_fid(&lgi->lgi_fid, LLOG_CATALOGS_OID);
1188
1189         o = dt_locate(env, d, &lgi->lgi_fid);
1190         if (IS_ERR(o))
1191                 RETURN(PTR_ERR(o));
1192
1193         if (!dt_object_exists(o)) {
1194                 th = dt_trans_create(env, d);
1195                 if (IS_ERR(th))
1196                         GOTO(out, rc = PTR_ERR(th));
1197
1198                 lgi->lgi_attr.la_valid = LA_MODE;
1199                 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1200                 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1201
1202                 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1203                                        &lgi->lgi_dof, th);
1204                 if (rc)
1205                         GOTO(out_trans, rc);
1206
1207                 rc = dt_trans_start_local(env, d, th);
1208                 if (rc)
1209                         GOTO(out_trans, rc);
1210
1211                 dt_write_lock(env, o, 0);
1212                 if (!dt_object_exists(o))
1213                         rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1214                                        &lgi->lgi_dof, th);
1215                 dt_write_unlock(env, o);
1216 out_trans:
1217                 dt_trans_stop(env, d, th);
1218                 if (rc)
1219                         GOTO(out, rc);
1220         }
1221
1222         rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
1223         if (rc)
1224                 GOTO(out, rc);
1225
1226         if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1227                 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1228                        o->do_lu.lo_dev->ld_obd->obd_name,
1229                        lgi->lgi_attr.la_mode);
1230                 GOTO(out, rc = -ENOENT);
1231         }
1232
1233         CDEBUG(D_CONFIG, "cat list: disk size=%d, read=%d\n",
1234                (int)lgi->lgi_attr.la_size, size);
1235
1236         /* return just number of llogs */
1237         if (idarray == NULL) {
1238                 rc = lgi->lgi_attr.la_size / sizeof(*idarray);
1239                 GOTO(out, rc);
1240         }
1241
1242         /* read for new ost index or for empty file */
1243         memset(idarray, 0, size);
1244         if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
1245                 GOTO(out, rc = 0);
1246         if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
1247                 size = lgi->lgi_attr.la_size - lgi->lgi_off;
1248
1249         lgi->lgi_buf.lb_buf = idarray;
1250         lgi->lgi_buf.lb_len = size;
1251         rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
1252         if (rc) {
1253                 CERROR("%s: error reading CATALOGS: rc = %d\n",
1254                        o->do_lu.lo_dev->ld_obd->obd_name,  rc);
1255                 GOTO(out, rc);
1256         }
1257
1258         EXIT;
1259 out:
1260         lu_object_put(env, &o->do_lu);
1261         RETURN(rc);
1262 }
1263 EXPORT_SYMBOL(llog_osd_get_cat_list);
1264
1265 /* writes the cat list */
1266 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
1267                           int idx, int count, struct llog_catid *idarray)
1268 {
1269         struct llog_thread_info *lgi = llog_info(env);
1270         struct dt_object        *o = NULL;
1271         struct thandle          *th;
1272         int                      rc, size;
1273
1274         if (!count)
1275                 RETURN(0);
1276
1277         LASSERT(d);
1278
1279         size = sizeof(*idarray) * count;
1280         lgi->lgi_off = idx * sizeof(*idarray);
1281
1282         lu_local_obj_fid(&lgi->lgi_fid, LLOG_CATALOGS_OID);
1283
1284         o = dt_locate(env, d, &lgi->lgi_fid);
1285         if (IS_ERR(o))
1286                 RETURN(PTR_ERR(o));
1287
1288         if (!dt_object_exists(o))
1289                 GOTO(out, rc = -ENOENT);
1290
1291         rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
1292         if (rc)
1293                 GOTO(out, rc);
1294
1295         if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1296                 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1297                        o->do_lu.lo_dev->ld_obd->obd_name,
1298                        lgi->lgi_attr.la_mode);
1299                 GOTO(out, rc = -ENOENT);
1300         }
1301
1302         th = dt_trans_create(env, d);
1303         if (IS_ERR(th))
1304                 GOTO(out, rc = PTR_ERR(th));
1305
1306         rc = dt_declare_record_write(env, o, size, lgi->lgi_off, th);
1307         if (rc)
1308                 GOTO(out, rc);
1309
1310         rc = dt_trans_start_local(env, d, th);
1311         if (rc)
1312                 GOTO(out_trans, rc);
1313
1314         lgi->lgi_buf.lb_buf = idarray;
1315         lgi->lgi_buf.lb_len = size;
1316         rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
1317         if (rc)
1318                 CDEBUG(D_INODE, "error writeing CATALOGS: rc = %d\n", rc);
1319 out_trans:
1320         dt_trans_stop(env, d, th);
1321 out:
1322         lu_object_put(env, &o->do_lu);
1323         RETURN(rc);
1324 }
1325 EXPORT_SYMBOL(llog_osd_put_cat_list);