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