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