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