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