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