Whamcloud - gitweb
LU-3105 osd: remove capa related stuff from servers
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2014 Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  */
32 /*
33  * lustre/obdclass/llog_osd.c
34  *
35  * Low level llog routines on top of OSD API
36  *
37  * This file provides set of methods for llog operations on top of
38  * dt_device. It contains all supported llog_operations interfaces and
39  * supplimental functions.
40  *
41  * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
42  * Author: Mikhail Pershin <mike.pershin@intel.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_LOG
46
47 #include <obd.h>
48 #include <obd_class.h>
49 #include <lustre_fid.h>
50 #include <dt_object.h>
51
52 #include "llog_internal.h"
53 #include "local_storage.h"
54
55 /**
56  * Implementation of the llog_operations::lop_declare_create
57  *
58  * This function is a wrapper over local_storage API function
59  * local_object_declare_create().
60  *
61  * \param[in] env       execution environment
62  * \param[in] los       local_storage for bottom storage device
63  * \param[in] o         dt_object to create
64  * \param[in] th        current transaction handle
65  *
66  * \retval              0 on successful declaration of the new object
67  * \retval              negative error if declaration was failed
68  */
69 static int llog_osd_declare_new_object(const struct lu_env *env,
70                                        struct local_oid_storage *los,
71                                        struct dt_object *o,
72                                        struct thandle *th)
73 {
74         struct llog_thread_info *lgi = llog_info(env);
75
76         lgi->lgi_attr.la_valid = LA_MODE;
77         lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
78         lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
79
80         return local_object_declare_create(env, los, o, &lgi->lgi_attr,
81                                            &lgi->lgi_dof, th);
82 }
83
84 /**
85  * Implementation of the llog_operations::lop_create
86  *
87  * This function is a wrapper over local_storage API function
88  * local_object_create().
89  *
90  * \param[in] env       execution environment
91  * \param[in] los       local_storage for bottom storage device
92  * \param[in] o         dt_object to create
93  * \param[in] th        current transaction handle
94  *
95  * \retval              0 on successful creation of the new object
96  * \retval              negative error if creation was failed
97  */
98 static int llog_osd_create_new_object(const struct lu_env *env,
99                                       struct local_oid_storage *los,
100                                       struct dt_object *o,
101                                       struct thandle *th)
102 {
103         struct llog_thread_info *lgi = llog_info(env);
104
105         lgi->lgi_attr.la_valid = LA_MODE;
106         lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
107         lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
108
109         return local_object_create(env, los, o, &lgi->lgi_attr,
110                                    &lgi->lgi_dof, th);
111 }
112
113 /**
114  * Write a padding record to the llog
115  *
116  * This function writes a padding record to the end of llog. That may
117  * be needed if llog contains records of variable size, e.g. config logs
118  * or changelogs.
119  * The padding record just aligns llog to the LLOG_CHUNK_SIZE boundary if
120  * the current record doesn't fit in the remaining space.
121  *
122  * It allocates full length to avoid two separate writes for header and tail.
123  * Such 2-steps scheme needs extra protection and complex error handling.
124  *
125  * \param[in]     env   execution environment
126  * \param[in]     o     dt_object to create
127  * \param[in,out] off   pointer to the padding start offset
128  * \param[in]     len   padding length
129  * \param[in]     index index of the padding record in a llog
130  * \param[in]     th    current transaction handle
131  *
132  * \retval              0 on successful padding write
133  * \retval              negative error if write failed
134  */
135 static int llog_osd_pad(const struct lu_env *env, struct dt_object *o,
136                         loff_t *off, int len, int index, struct thandle *th)
137 {
138         struct llog_thread_info *lgi = llog_info(env);
139         struct llog_rec_hdr     *rec;
140         struct llog_rec_tail    *tail;
141         int                      rc;
142
143         ENTRY;
144
145         LASSERT(th);
146         LASSERT(off);
147         LASSERT(len >= LLOG_MIN_REC_SIZE && (len & 0x7) == 0);
148
149         OBD_ALLOC(rec, len);
150         if (rec == NULL)
151                 RETURN(-ENOMEM);
152
153         rec->lrh_len = len;
154         rec->lrh_index = index;
155         rec->lrh_type = LLOG_PAD_MAGIC;
156
157         tail = rec_tail(rec);
158         tail->lrt_len = len;
159         tail->lrt_index = index;
160
161         lgi->lgi_buf.lb_buf = rec;
162         lgi->lgi_buf.lb_len = len;
163         rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
164         if (rc)
165                 CERROR("%s: error writing padding record: rc = %d\n",
166                        o->do_lu.lo_dev->ld_obd->obd_name, rc);
167
168         OBD_FREE(rec, len);
169         RETURN(rc);
170 }
171
172 /**
173  * Implementation of the llog_operations::lop_read_header
174  *
175  * This function reads the current llog header from the bottom storage
176  * device.
177  *
178  * \param[in] env       execution environment
179  * \param[in] handle    llog handle of the current llog
180  *
181  * \retval              0 on successful header read
182  * \retval              negative error if read failed
183  */
184 static int llog_osd_read_header(const struct lu_env *env,
185                                 struct llog_handle *handle)
186 {
187         struct llog_rec_hdr     *llh_hdr;
188         struct dt_object        *o;
189         struct llog_thread_info *lgi;
190         enum llog_flag           flags;
191         int                      rc;
192
193         ENTRY;
194
195         LASSERT(sizeof(*handle->lgh_hdr) == LLOG_CHUNK_SIZE);
196
197         o = handle->lgh_obj;
198         LASSERT(o);
199
200         lgi = llog_info(env);
201
202         rc = dt_attr_get(env, o, &lgi->lgi_attr);
203         if (rc)
204                 RETURN(rc);
205
206         LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
207
208         if (lgi->lgi_attr.la_size == 0) {
209                 CDEBUG(D_HA, "not reading header from 0-byte log\n");
210                 RETURN(LLOG_EEMPTY);
211         }
212
213         flags = handle->lgh_hdr->llh_flags;
214
215         lgi->lgi_off = 0;
216         lgi->lgi_buf.lb_buf = handle->lgh_hdr;
217         lgi->lgi_buf.lb_len = LLOG_CHUNK_SIZE;
218
219         rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
220         if (rc) {
221                 CERROR("%s: error reading log header from "DFID": rc = %d\n",
222                        o->do_lu.lo_dev->ld_obd->obd_name,
223                        PFID(lu_object_fid(&o->do_lu)), rc);
224                 RETURN(rc);
225         }
226
227         llh_hdr = &handle->lgh_hdr->llh_hdr;
228         if (LLOG_REC_HDR_NEEDS_SWABBING(llh_hdr))
229                 lustre_swab_llog_hdr(handle->lgh_hdr);
230
231         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
232                 CERROR("%s: bad log %s "DFID" header magic: %#x "
233                        "(expected %#x)\n", o->do_lu.lo_dev->ld_obd->obd_name,
234                        handle->lgh_name ? handle->lgh_name : "",
235                        PFID(lu_object_fid(&o->do_lu)),
236                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
237                 RETURN(-EIO);
238         } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
239                 CERROR("%s: incorrectly sized log %s "DFID" header: "
240                        "%#x (expected %#x)\n"
241                        "you may need to re-run lconf --write_conf.\n",
242                        o->do_lu.lo_dev->ld_obd->obd_name,
243                        handle->lgh_name ? handle->lgh_name : "",
244                        PFID(lu_object_fid(&o->do_lu)),
245                        llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
246                 RETURN(-EIO);
247         }
248
249         handle->lgh_hdr->llh_flags |= (flags & LLOG_F_EXT_MASK);
250         handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
251
252         RETURN(0);
253 }
254
255 /**
256  * Implementation of the llog_operations::lop_declare_write
257  *
258  * This function declares the new record write.
259  *
260  * \param[in] env       execution environment
261  * \param[in] loghandle llog handle of the current llog
262  * \param[in] rec       llog record header. This is a real header of the full
263  *                      llog record to write. This is the beginning of buffer
264  *                      to write, the length of buffer is stored in
265  *                      \a rec::lrh_len
266  * \param[in] idx       index of the llog record. If \a idx == -1 then this is
267  *                      append case, otherwise \a idx is the index of record
268  *                      to modify
269  * \param[in] th        current transaction handle
270  *
271  * \retval              0 on successful declaration
272  * \retval              negative error if declaration failed
273  */
274 static int llog_osd_declare_write_rec(const struct lu_env *env,
275                                       struct llog_handle *loghandle,
276                                       struct llog_rec_hdr *rec,
277                                       int idx, struct thandle *th)
278 {
279         struct llog_thread_info *lgi = llog_info(env);
280         struct dt_object        *o;
281         int                      rc;
282
283         ENTRY;
284
285         LASSERT(env);
286         LASSERT(th);
287         LASSERT(loghandle);
288         LASSERT(rec);
289         LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
290
291         o = loghandle->lgh_obj;
292         LASSERT(o);
293
294         lgi->lgi_buf.lb_len = sizeof(struct llog_log_hdr);
295         lgi->lgi_buf.lb_buf = NULL;
296         /* each time we update header */
297         rc = dt_declare_record_write(env, o, &lgi->lgi_buf, 0,
298                                      th);
299         if (rc || idx == 0) /* if error or just header */
300                 RETURN(rc);
301
302         /**
303          * the pad record can be inserted so take into account double
304          * record size
305          */
306         lgi->lgi_buf.lb_len = rec->lrh_len * 2;
307         lgi->lgi_buf.lb_buf = NULL;
308         /* XXX: implement declared window or multi-chunks approach */
309         rc = dt_declare_record_write(env, o, &lgi->lgi_buf, -1, th);
310
311         RETURN(rc);
312 }
313
314 /**
315  * Implementation of the llog_operations::lop_write
316  *
317  * This function writes the new record in the llog or modify the existed one.
318  *
319  * \param[in]  env              execution environment
320  * \param[in]  loghandle        llog handle of the current llog
321  * \param[in]  rec              llog record header. This is a real header of
322  *                              the full llog record to write. This is
323  *                              the beginning of buffer to write, the length
324  *                              of buffer is stored in \a rec::lrh_len
325  * \param[out] reccookie        pointer to the cookie to return back if needed.
326  *                              It is used for further cancel of this llog
327  *                              record.
328  * \param[in]  idx              index of the llog record. If \a idx == -1 then
329  *                              this is append case, otherwise \a idx is
330  *                              the index of record to modify
331  * \param[in]  th               current transaction handle
332  *
333  * \retval                      0 on successful write && \a reccookie == NULL
334  *                              1 on successful write && \a reccookie != NULL
335  * \retval                      negative error if write failed
336  */
337 static int llog_osd_write_rec(const struct lu_env *env,
338                               struct llog_handle *loghandle,
339                               struct llog_rec_hdr *rec,
340                               struct llog_cookie *reccookie,
341                               int idx, struct thandle *th)
342 {
343         struct llog_thread_info *lgi = llog_info(env);
344         struct llog_log_hdr     *llh;
345         int                      reclen = rec->lrh_len;
346         int                      index, rc;
347         struct llog_rec_tail    *lrt;
348         struct dt_object        *o;
349         size_t                   left;
350         bool                     header_is_updated = false;
351
352         ENTRY;
353
354         LASSERT(env);
355         llh = loghandle->lgh_hdr;
356         LASSERT(llh);
357         o = loghandle->lgh_obj;
358         LASSERT(o);
359         LASSERT(th);
360
361         CDEBUG(D_OTHER, "new record %x to "DFID"\n",
362                rec->lrh_type, PFID(lu_object_fid(&o->do_lu)));
363
364         /* record length should not bigger than LLOG_CHUNK_SIZE */
365         if (reclen > LLOG_CHUNK_SIZE)
366                 RETURN(-E2BIG);
367
368         rc = dt_attr_get(env, o, &lgi->lgi_attr);
369         if (rc)
370                 RETURN(rc);
371
372         /**
373          * The modification case.
374          * If idx set then the record with that index must be modified.
375          * There are three cases possible:
376          * 1) the common case is the llog header update (idx == 0)
377          * 2) the llog record modification during llog process.
378          *    This is indicated by the \a loghandle::lgh_cur_idx > 0.
379          *    In that case the \a loghandle::lgh_cur_offset
380          * 3) otherwise this is assumed that llog consist of records of
381          *    fixed size, i.e. catalog. The llog header must has llh_size
382          *    field equal to record size. The record offset is calculated
383          *    just by /a idx value
384          *
385          * During modification we don't need extra header update because
386          * the bitmap and record count are not changed. The record header
387          * and tail remains the same too.
388          */
389         if (idx != LLOG_NEXT_IDX) {
390                 /* llog can be empty only when first record is being written */
391                 LASSERT(ergo(idx > 0, lgi->lgi_attr.la_size > 0));
392
393                 if (!ext2_test_bit(idx, llh->llh_bitmap)) {
394                         CERROR("%s: modify unset record %u\n",
395                                o->do_lu.lo_dev->ld_obd->obd_name, idx);
396                         RETURN(-ENOENT);
397                 }
398
399                 if (idx != rec->lrh_index) {
400                         CERROR("%s: modify index mismatch %d %u\n",
401                                o->do_lu.lo_dev->ld_obd->obd_name, idx,
402                                rec->lrh_index);
403                         RETURN(-EFAULT);
404                 }
405
406                 if (idx == LLOG_HEADER_IDX) {
407                         /* llog header update */
408                         LASSERT(reclen == sizeof(struct llog_log_hdr));
409                         LASSERT(rec == &llh->llh_hdr);
410
411                         lgi->lgi_off = 0;
412                         lgi->lgi_buf.lb_len = reclen;
413                         lgi->lgi_buf.lb_buf = rec;
414                         rc = dt_record_write(env, o, &lgi->lgi_buf,
415                                              &lgi->lgi_off, th);
416                         RETURN(rc);
417                 } else if (loghandle->lgh_cur_idx > 0) {
418                         /**
419                          * The lgh_cur_offset can be used only if index is
420                          * the same.
421                          */
422                         if (idx != loghandle->lgh_cur_idx) {
423                                 CERROR("%s: modify index mismatch %d %d\n",
424                                        o->do_lu.lo_dev->ld_obd->obd_name, idx,
425                                        loghandle->lgh_cur_idx);
426                                 RETURN(-EFAULT);
427                         }
428
429                         lgi->lgi_off = loghandle->lgh_cur_offset;
430                         CDEBUG(D_OTHER, "modify record "DOSTID": idx:%d, "
431                                "len:%u offset %llu\n",
432                                POSTID(&loghandle->lgh_id.lgl_oi), idx,
433                                rec->lrh_len, (long long)lgi->lgi_off);
434                 } else if (llh->llh_size > 0) {
435                         if (llh->llh_size != rec->lrh_len) {
436                                 CERROR("%s: wrong record size, llh_size is %u"
437                                        " but record size is %u\n",
438                                        o->do_lu.lo_dev->ld_obd->obd_name,
439                                        llh->llh_size, rec->lrh_len);
440                                 RETURN(-EINVAL);
441                         }
442                         lgi->lgi_off = sizeof(*llh) + (idx - 1) * reclen;
443                 } else {
444                         /* This can be result of lgh_cur_idx is not set during
445                          * llog processing or llh_size is not set to proper
446                          * record size for fixed records llog. Therefore it is
447                          * impossible to get record offset. */
448                         CERROR("%s: can't get record offset, idx:%d, "
449                                "len:%u.\n", o->do_lu.lo_dev->ld_obd->obd_name,
450                                idx, rec->lrh_len);
451                         RETURN(-EFAULT);
452                 }
453
454                 /* update only data, header and tail remain the same */
455                 lgi->lgi_off += sizeof(struct llog_rec_hdr);
456                 lgi->lgi_buf.lb_len = REC_DATA_LEN(rec);
457                 lgi->lgi_buf.lb_buf = REC_DATA(rec);
458                 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
459                 if (rc == 0 && reccookie) {
460                         reccookie->lgc_lgl = loghandle->lgh_id;
461                         reccookie->lgc_index = idx;
462                         rc = 1;
463                 }
464                 RETURN(rc);
465         }
466
467         /**
468          * The append case.
469          * The most common case of using llog. The new index is assigned to
470          * the new record, new bit is set in llog bitmap and llog count is
471          * incremented.
472          *
473          * Make sure that records don't cross a chunk boundary, so we can
474          * process them page-at-a-time if needed.  If it will cross a chunk
475          * boundary, write in a fake (but referenced) entry to pad the chunk.
476          */
477         LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
478         lgi->lgi_off = lgi->lgi_attr.la_size;
479         left = LLOG_CHUNK_SIZE - (lgi->lgi_off & (LLOG_CHUNK_SIZE - 1));
480         /* NOTE: padding is a record, but no bit is set */
481         if (left != 0 && left != reclen &&
482             left < (reclen + LLOG_MIN_REC_SIZE)) {
483                 index = loghandle->lgh_last_idx + 1;
484                 rc = llog_osd_pad(env, o, &lgi->lgi_off, left, index, th);
485                 if (rc)
486                         RETURN(rc);
487                 loghandle->lgh_last_idx++; /* for pad rec */
488         }
489         /* if it's the last idx in log file, then return -ENOSPC */
490         if (loghandle->lgh_last_idx >= LLOG_BITMAP_SIZE(llh) - 1)
491                 RETURN(-ENOSPC);
492
493         /* increment the last_idx along with llh_tail index, they should
494          * be equal for a llog lifetime */
495         loghandle->lgh_last_idx++;
496         index = loghandle->lgh_last_idx;
497         llh->llh_tail.lrt_index = index;
498         /**
499          * NB: the caller should make sure only 1 process access
500          * the lgh_last_idx, e.g. append should be exclusive.
501          * Otherwise it might hit the assert.
502          */
503         LASSERT(index < LLOG_BITMAP_SIZE(llh));
504         rec->lrh_index = index;
505         lrt = rec_tail(rec);
506         lrt->lrt_len = rec->lrh_len;
507         lrt->lrt_index = rec->lrh_index;
508
509         /* the lgh_hdr_lock protects llog header data from concurrent
510          * update/cancel, the llh_count and llh_bitmap are protected */
511         spin_lock(&loghandle->lgh_hdr_lock);
512         if (ext2_set_bit(index, llh->llh_bitmap)) {
513                 CERROR("%s: index %u already set in log bitmap\n",
514                        o->do_lu.lo_dev->ld_obd->obd_name, index);
515                 spin_unlock(&loghandle->lgh_hdr_lock);
516                 LBUG(); /* should never happen */
517         }
518         llh->llh_count++;
519         spin_unlock(&loghandle->lgh_hdr_lock);
520
521         lgi->lgi_off = 0;
522         lgi->lgi_buf.lb_len = llh->llh_hdr.lrh_len;
523         lgi->lgi_buf.lb_buf = &llh->llh_hdr;
524         rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
525         if (rc)
526                 GOTO(out, rc);
527
528         header_is_updated = true;
529         rc = dt_attr_get(env, o, &lgi->lgi_attr);
530         if (rc)
531                 GOTO(out, rc);
532
533         LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
534         lgi->lgi_off = lgi->lgi_attr.la_size;
535         lgi->lgi_buf.lb_len = reclen;
536         lgi->lgi_buf.lb_buf = rec;
537         rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
538         if (rc < 0)
539                 GOTO(out, rc);
540
541         CDEBUG(D_OTHER, "added record "DOSTID": idx: %u, %u\n",
542                POSTID(&loghandle->lgh_id.lgl_oi), index, rec->lrh_len);
543         if (reccookie != NULL) {
544                 reccookie->lgc_lgl = loghandle->lgh_id;
545                 reccookie->lgc_index = index;
546                 if ((rec->lrh_type == MDS_UNLINK_REC) ||
547                     (rec->lrh_type == MDS_SETATTR64_REC))
548                         reccookie->lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
549                 else if (rec->lrh_type == OST_SZ_REC)
550                         reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
551                 else
552                         reccookie->lgc_subsys = -1;
553                 rc = 1;
554         }
555         RETURN(rc);
556 out:
557         /* cleanup llog for error case */
558         spin_lock(&loghandle->lgh_hdr_lock);
559         ext2_clear_bit(index, llh->llh_bitmap);
560         llh->llh_count--;
561         spin_unlock(&loghandle->lgh_hdr_lock);
562
563         /* restore llog last_idx */
564         loghandle->lgh_last_idx--;
565         llh->llh_tail.lrt_index = loghandle->lgh_last_idx;
566
567         /* restore the header on disk if it was written */
568         if (header_is_updated) {
569                 lgi->lgi_off = 0;
570                 lgi->lgi_buf.lb_len = llh->llh_hdr.lrh_len;
571                 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
572                 dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
573         }
574
575         RETURN(rc);
576 }
577
578 /**
579  * We can skip reading at least as many log blocks as the number of
580  * minimum sized log records we are skipping.  If it turns out
581  * that we are not far enough along the log (because the
582  * actual records are larger than minimum size) we just skip
583  * some more records.
584  */
585 static inline void llog_skip_over(__u64 *off, int curr, int goal)
586 {
587         if (goal <= curr)
588                 return;
589         *off = (*off + (goal - curr - 1) * LLOG_MIN_REC_SIZE) &
590                 ~(LLOG_CHUNK_SIZE - 1);
591 }
592
593 /**
594  * Remove optional fields that the client doesn't expect.
595  * This is typically in order to ensure compatibility with older clients.
596  * It is assumed that since we exclusively remove fields, the block will be
597  * big enough to handle the remapped records. It is also assumed that records
598  * of a block have the same format (i.e.: the same features enabled).
599  *
600  * \param[in,out]    hdr        Header of the block of records to remap.
601  * \param[in,out]    last_hdr   Last header, don't read past this point.
602  * \param[in]        flags      Flags describing the fields to keep.
603  */
604 static void changelog_block_trim_ext(struct llog_rec_hdr *hdr,
605                                      struct llog_rec_hdr *last_hdr,
606                                      enum changelog_rec_flags flags)
607 {
608         if (hdr->lrh_type != CHANGELOG_REC)
609                 return;
610
611         do {
612                 struct changelog_rec *rec = (struct changelog_rec *)(hdr + 1);
613
614                 changelog_remap_rec(rec, rec->cr_flags & flags);
615                 hdr = llog_rec_hdr_next(hdr);
616         } while ((char *)hdr <= (char *)last_hdr);
617 }
618
619 /**
620  * Implementation of the llog_operations::lop_next_block
621  *
622  * This function finds the the next llog block to return which contains
623  * record with required index. It is main part of llog processing.
624  *
625  * \param[in]     env           execution environment
626  * \param[in]     loghandle     llog handle of the current llog
627  * \param[in,out] cur_idx       index preceeding cur_offset
628  * \param[in]     next_idx      target index to find
629  * \param[in,out] cur_offset    furtherst point read in the file
630  * \param[in]     buf           pointer to data buffer to fill
631  * \param[in]     len           required len to read, it is
632  *                              LLOG_CHUNK_SIZE usually.
633  *
634  * \retval                      0 on successful buffer read
635  * \retval                      negative value on error
636  */
637 static int llog_osd_next_block(const struct lu_env *env,
638                                struct llog_handle *loghandle, int *cur_idx,
639                                int next_idx, __u64 *cur_offset, void *buf,
640                                int len)
641 {
642         struct llog_thread_info *lgi = llog_info(env);
643         struct dt_object        *o;
644         struct dt_device        *dt;
645         int                      rc;
646
647         ENTRY;
648
649         LASSERT(env);
650         LASSERT(lgi);
651
652         if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
653                 RETURN(-EINVAL);
654
655         CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off "LPU64")\n",
656                next_idx, *cur_idx, *cur_offset);
657
658         LASSERT(loghandle);
659         LASSERT(loghandle->lgh_ctxt);
660
661         o = loghandle->lgh_obj;
662         LASSERT(o);
663         LASSERT(dt_object_exists(o));
664         dt = lu2dt_dev(o->do_lu.lo_dev);
665         LASSERT(dt);
666
667         rc = dt_attr_get(env, o, &lgi->lgi_attr);
668         if (rc)
669                 GOTO(out, rc);
670
671         while (*cur_offset < lgi->lgi_attr.la_size) {
672                 struct llog_rec_hdr     *rec, *last_rec;
673                 struct llog_rec_tail    *tail;
674
675                 llog_skip_over(cur_offset, *cur_idx, next_idx);
676
677                 /* read up to next LLOG_CHUNK_SIZE block */
678                 lgi->lgi_buf.lb_len = LLOG_CHUNK_SIZE -
679                                       (*cur_offset & (LLOG_CHUNK_SIZE - 1));
680                 lgi->lgi_buf.lb_buf = buf;
681
682                 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
683                 if (rc < 0) {
684                         CERROR("%s: can't read llog block from log "DFID
685                                " offset "LPU64": rc = %d\n",
686                                o->do_lu.lo_dev->ld_obd->obd_name,
687                                PFID(lu_object_fid(&o->do_lu)), *cur_offset,
688                                rc);
689                         GOTO(out, rc);
690                 }
691
692                 if (rc < len) {
693                         /* signal the end of the valid buffer to
694                          * llog_process */
695                         memset(buf + rc, 0, len - rc);
696                 }
697
698                 if (rc == 0) /* end of file, nothing to do */
699                         GOTO(out, rc);
700
701                 if (rc < sizeof(*tail)) {
702                         CERROR("%s: invalid llog block at log id "DOSTID"/%u "
703                                "offset "LPU64"\n",
704                                o->do_lu.lo_dev->ld_obd->obd_name,
705                                POSTID(&loghandle->lgh_id.lgl_oi),
706                                loghandle->lgh_id.lgl_ogen, *cur_offset);
707                         GOTO(out, rc = -EINVAL);
708                 }
709
710                 rec = buf;
711                 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
712                         lustre_swab_llog_rec(rec);
713
714                 tail = (struct llog_rec_tail *)((char *)buf + rc -
715                                                 sizeof(struct llog_rec_tail));
716                 /* get the last record in block */
717                 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
718                                                    tail->lrt_len);
719
720                 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
721                         lustre_swab_llog_rec(last_rec);
722                 LASSERT(last_rec->lrh_index == tail->lrt_index);
723
724                 *cur_idx = tail->lrt_index;
725
726                 /* this shouldn't happen */
727                 if (tail->lrt_index == 0) {
728                         CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
729                                "offset "LPU64"\n",
730                                o->do_lu.lo_dev->ld_obd->obd_name,
731                                POSTID(&loghandle->lgh_id.lgl_oi),
732                                loghandle->lgh_id.lgl_ogen, *cur_offset);
733                         GOTO(out, rc = -EINVAL);
734                 }
735                 if (tail->lrt_index < next_idx)
736                         continue;
737
738                 /* sanity check that the start of the new buffer is no farther
739                  * than the record that we wanted.  This shouldn't happen. */
740                 if (rec->lrh_index > next_idx) {
741                         CERROR("%s: missed desired record? %u > %u\n",
742                                o->do_lu.lo_dev->ld_obd->obd_name,
743                                rec->lrh_index, next_idx);
744                         GOTO(out, rc = -ENOENT);
745                 }
746
747                 /* Trim unsupported extensions for compat w/ older clients */
748                 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_JOBID))
749                         changelog_block_trim_ext(rec, last_rec,
750                                                  CLF_VERSION | CLF_RENAME);
751
752                 GOTO(out, rc = 0);
753         }
754         GOTO(out, rc = -EIO);
755 out:
756         return rc;
757 }
758
759 /**
760  * Implementation of the llog_operations::lop_prev_block
761  *
762  * This function finds the llog block to return which contains
763  * record with required index but in reverse order - from end of llog
764  * to the beginning.
765  * It is main part of reverse llog processing.
766  *
767  * \param[in] env       execution environment
768  * \param[in] loghandle llog handle of the current llog
769  * \param[in] prev_idx  target index to find
770  * \param[in] buf       pointer to data buffer to fill
771  * \param[in] len       required len to read, it is LLOG_CHUNK_SIZE usually.
772  *
773  * \retval              0 on successful buffer read
774  * \retval              negative value on error
775  */
776 static int llog_osd_prev_block(const struct lu_env *env,
777                                struct llog_handle *loghandle,
778                                int prev_idx, void *buf, int len)
779 {
780         struct llog_thread_info *lgi = llog_info(env);
781         struct dt_object        *o;
782         struct dt_device        *dt;
783         loff_t                   cur_offset;
784         int                      rc;
785
786         ENTRY;
787
788         if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
789                 RETURN(-EINVAL);
790
791         CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
792
793         LASSERT(loghandle);
794         LASSERT(loghandle->lgh_ctxt);
795
796         o = loghandle->lgh_obj;
797         LASSERT(o);
798         LASSERT(dt_object_exists(o));
799         dt = lu2dt_dev(o->do_lu.lo_dev);
800         LASSERT(dt);
801
802         cur_offset = LLOG_CHUNK_SIZE;
803         llog_skip_over(&cur_offset, 0, prev_idx);
804
805         rc = dt_attr_get(env, o, &lgi->lgi_attr);
806         if (rc)
807                 GOTO(out, rc);
808
809         while (cur_offset < lgi->lgi_attr.la_size) {
810                 struct llog_rec_hdr     *rec, *last_rec;
811                 struct llog_rec_tail    *tail;
812
813                 lgi->lgi_buf.lb_len = len;
814                 lgi->lgi_buf.lb_buf = buf;
815                 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
816                 if (rc < 0) {
817                         CERROR("%s: can't read llog block from log "DFID
818                                " offset "LPU64": rc = %d\n",
819                                o->do_lu.lo_dev->ld_obd->obd_name,
820                                PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
821                         GOTO(out, rc);
822                 }
823
824                 if (rc == 0) /* end of file, nothing to do */
825                         GOTO(out, rc);
826
827                 if (rc < sizeof(*tail)) {
828                         CERROR("%s: invalid llog block at log id "DOSTID"/%u "
829                                "offset "LPU64"\n",
830                                o->do_lu.lo_dev->ld_obd->obd_name,
831                                POSTID(&loghandle->lgh_id.lgl_oi),
832                                loghandle->lgh_id.lgl_ogen, cur_offset);
833                         GOTO(out, rc = -EINVAL);
834                 }
835
836                 rec = buf;
837                 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
838                         lustre_swab_llog_rec(rec);
839
840                 tail = (struct llog_rec_tail *)((char *)buf + rc -
841                                                 sizeof(struct llog_rec_tail));
842                 /* get the last record in block */
843                 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
844                                                    le32_to_cpu(tail->lrt_len));
845
846                 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
847                         lustre_swab_llog_rec(last_rec);
848                 LASSERT(last_rec->lrh_index == tail->lrt_index);
849
850                 /* this shouldn't happen */
851                 if (tail->lrt_index == 0) {
852                         CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
853                                "offset "LPU64"\n",
854                                o->do_lu.lo_dev->ld_obd->obd_name,
855                                POSTID(&loghandle->lgh_id.lgl_oi),
856                                loghandle->lgh_id.lgl_ogen, cur_offset);
857                         GOTO(out, rc = -EINVAL);
858                 }
859                 if (tail->lrt_index < prev_idx)
860                         continue;
861
862                 /* sanity check that the start of the new buffer is no farther
863                  * than the record that we wanted.  This shouldn't happen. */
864                 if (rec->lrh_index > prev_idx) {
865                         CERROR("%s: missed desired record? %u > %u\n",
866                                o->do_lu.lo_dev->ld_obd->obd_name,
867                                rec->lrh_index, prev_idx);
868                         GOTO(out, rc = -ENOENT);
869                 }
870
871                 /* Trim unsupported extensions for compat w/ older clients */
872                 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_JOBID))
873                         changelog_block_trim_ext(rec, last_rec,
874                                                  CLF_VERSION | CLF_RENAME);
875
876                 GOTO(out, rc = 0);
877         }
878         GOTO(out, rc = -EIO);
879 out:
880         return rc;
881 }
882
883 /**
884  * This is helper function to get llog directory object. It is used by named
885  * llog operations to find/insert/delete llog entry from llog directory.
886  *
887  * \param[in] env       execution environment
888  * \param[in] ctxt      llog context
889  *
890  * \retval              dt_object of llog directory
891  * \retval              ERR_PTR of negative value on error
892  */
893 static struct dt_object *llog_osd_dir_get(const struct lu_env *env,
894                                           struct llog_ctxt *ctxt)
895 {
896         struct dt_device        *dt;
897         struct dt_thread_info   *dti = dt_info(env);
898         struct dt_object        *dir;
899         int                      rc;
900
901         dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
902         if (ctxt->loc_dir == NULL) {
903                 rc = dt_root_get(env, dt, &dti->dti_fid);
904                 if (rc)
905                         return ERR_PTR(rc);
906                 dir = dt_locate(env, dt, &dti->dti_fid);
907
908                 if (!IS_ERR(dir) && !dt_try_as_dir(env, dir)) {
909                         lu_object_put(env, &dir->do_lu);
910                         return ERR_PTR(-ENOTDIR);
911                 }
912         } else {
913                 lu_object_get(&ctxt->loc_dir->do_lu);
914                 dir = ctxt->loc_dir;
915         }
916
917         return dir;
918 }
919
920 /**
921  * Implementation of the llog_operations::lop_open
922  *
923  * This function opens the llog by its logid or by name, it may open also
924  * non existent llog and assing then new id to it.
925  * The llog_open/llog_close pair works similar to lu_object_find/put,
926  * the object may not exist prior open. The result of open is just dt_object
927  * in the llog header.
928  *
929  * \param[in] env               execution environment
930  * \param[in] handle            llog handle of the current llog
931  * \param[in] logid             logid of llog to open (nameless llog)
932  * \param[in] name              name of llog to open (named llog)
933  * \param[in] open_param
934  *                              LLOG_OPEN_NEW - new llog, may not exist
935  *                              LLOG_OPEN_EXIST - old llog, must exist
936  *
937  * \retval                      0 on successful open, llog_handle::lgh_obj
938  *                              contains the dt_object of the llog.
939  * \retval                      negative value on error
940  */
941 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
942                          struct llog_logid *logid, char *name,
943                          enum llog_open_param open_param)
944 {
945         struct llog_thread_info         *lgi = llog_info(env);
946         struct llog_ctxt                *ctxt = handle->lgh_ctxt;
947         struct dt_object                *o;
948         struct dt_device                *dt;
949         struct ls_device                *ls;
950         struct local_oid_storage        *los;
951         int                              rc = 0;
952
953         ENTRY;
954
955         LASSERT(env);
956         LASSERT(ctxt);
957         LASSERT(ctxt->loc_exp);
958         LASSERT(ctxt->loc_exp->exp_obd);
959         dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
960         LASSERT(dt);
961
962         ls = ls_device_get(dt);
963         if (IS_ERR(ls))
964                 RETURN(PTR_ERR(ls));
965
966         mutex_lock(&ls->ls_los_mutex);
967         los = dt_los_find(ls, name != NULL ? FID_SEQ_LLOG_NAME : FID_SEQ_LLOG);
968         mutex_unlock(&ls->ls_los_mutex);
969         LASSERT(los);
970         ls_device_put(env, ls);
971
972         LASSERT(handle);
973
974         if (logid != NULL) {
975                 logid_to_fid(logid, &lgi->lgi_fid);
976         } else if (name) {
977                 struct dt_object *llog_dir;
978
979                 llog_dir = llog_osd_dir_get(env, ctxt);
980                 if (IS_ERR(llog_dir))
981                         GOTO(out, rc = PTR_ERR(llog_dir));
982                 dt_read_lock(env, llog_dir, 0);
983                 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
984                 dt_read_unlock(env, llog_dir);
985                 lu_object_put(env, &llog_dir->do_lu);
986                 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
987                         /* generate fid for new llog */
988                         rc = local_object_fid_generate(env, los,
989                                                        &lgi->lgi_fid);
990                 }
991                 if (rc < 0)
992                         GOTO(out, rc);
993                 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
994                 if (handle->lgh_name)
995                         strcpy(handle->lgh_name, name);
996                 else
997                         GOTO(out, rc = -ENOMEM);
998         } else {
999                 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
1000                 /* generate fid for new llog */
1001                 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
1002                 if (rc < 0)
1003                         GOTO(out, rc);
1004         }
1005
1006         o = ls_locate(env, ls, &lgi->lgi_fid, NULL);
1007         if (IS_ERR(o))
1008                 GOTO(out_name, rc = PTR_ERR(o));
1009
1010         /* No new llog is expected but doesn't exist */
1011         if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o))
1012                 GOTO(out_put, rc = -ENOENT);
1013
1014         fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
1015         handle->lgh_obj = o;
1016         handle->private_data = los;
1017         LASSERT(handle->lgh_ctxt);
1018
1019         RETURN(rc);
1020
1021 out_put:
1022         lu_object_put(env, &o->do_lu);
1023 out_name:
1024         if (handle->lgh_name != NULL)
1025                 OBD_FREE(handle->lgh_name, strlen(name) + 1);
1026 out:
1027         dt_los_put(los);
1028         RETURN(rc);
1029 }
1030
1031 /**
1032  * Implementation of the llog_operations::lop_exist
1033  *
1034  * This function checks that llog exists on storage.
1035  *
1036  * \param[in] handle    llog handle of the current llog
1037  *
1038  * \retval              true if llog object exists and is not just destroyed
1039  * \retval              false if llog doesn't exist or just destroyed
1040  */
1041 static int llog_osd_exist(struct llog_handle *handle)
1042 {
1043         LASSERT(handle->lgh_obj);
1044         return (dt_object_exists(handle->lgh_obj) &&
1045                 !lu_object_is_dying(handle->lgh_obj->do_lu.lo_header));
1046 }
1047
1048 /**
1049  * Implementation of the llog_operations::lop_declare_create
1050  *
1051  * This function declares the llog create. It declares also name insert
1052  * into llog directory in case of named llog.
1053  *
1054  * \param[in] env       execution environment
1055  * \param[in] res       llog handle of the current llog
1056  * \param[in] th        current transaction handle
1057  *
1058  * \retval              0 on successful create declaration
1059  * \retval              negative value on error
1060  */
1061 static int llog_osd_declare_create(const struct lu_env *env,
1062                                    struct llog_handle *res, struct thandle *th)
1063 {
1064         struct llog_thread_info         *lgi = llog_info(env);
1065         struct dt_insert_rec            *rec = &lgi->lgi_dt_rec;
1066         struct local_oid_storage        *los;
1067         struct dt_object                *o;
1068         int                              rc;
1069
1070         ENTRY;
1071
1072         LASSERT(res->lgh_obj);
1073         LASSERT(th);
1074
1075         /* object can be created by another thread */
1076         o = res->lgh_obj;
1077         if (dt_object_exists(o))
1078                 RETURN(0);
1079
1080         los = res->private_data;
1081         LASSERT(los);
1082
1083         rc = llog_osd_declare_new_object(env, los, o, th);
1084         if (rc)
1085                 RETURN(rc);
1086
1087         /* do not declare header initialization here as it's declared
1088          * in llog_osd_declare_write_rec() which is always called */
1089
1090         if (res->lgh_name) {
1091                 struct dt_object *llog_dir;
1092
1093                 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1094                 if (IS_ERR(llog_dir))
1095                         RETURN(PTR_ERR(llog_dir));
1096                 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1097                 rec->rec_fid = &lgi->lgi_fid;
1098                 rec->rec_type = S_IFREG;
1099                 rc = dt_declare_insert(env, llog_dir,
1100                                        (struct dt_rec *)rec,
1101                                        (struct dt_key *)res->lgh_name, th);
1102                 lu_object_put(env, &llog_dir->do_lu);
1103                 if (rc)
1104                         CERROR("%s: can't declare named llog %s: rc = %d\n",
1105                                o->do_lu.lo_dev->ld_obd->obd_name,
1106                                res->lgh_name, rc);
1107         }
1108         RETURN(rc);
1109 }
1110
1111 /**
1112  * Implementation of the llog_operations::lop_create
1113  *
1114  * This function creates the llog according with llog_handle::lgh_obj
1115  * and llog_handle::lgh_name.
1116  *
1117  * \param[in] env       execution environment
1118  * \param[in] res       llog handle of the current llog
1119  * \param[in] th        current transaction handle
1120  *
1121  * \retval              0 on successful create
1122  * \retval              negative value on error
1123  */
1124 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
1125                            struct thandle *th)
1126 {
1127         struct llog_thread_info *lgi = llog_info(env);
1128         struct dt_insert_rec    *rec = &lgi->lgi_dt_rec;
1129         struct local_oid_storage *los;
1130         struct dt_object        *o;
1131         int                      rc = 0;
1132
1133         ENTRY;
1134
1135         LASSERT(env);
1136         o = res->lgh_obj;
1137         LASSERT(o);
1138
1139         /* llog can be already created */
1140         if (dt_object_exists(o))
1141                 RETURN(-EEXIST);
1142
1143         los = res->private_data;
1144         LASSERT(los);
1145
1146         dt_write_lock(env, o, 0);
1147         if (!dt_object_exists(o))
1148                 rc = llog_osd_create_new_object(env, los, o, th);
1149         else
1150                 rc = -EEXIST;
1151
1152         dt_write_unlock(env, o);
1153         if (rc)
1154                 RETURN(rc);
1155
1156         if (res->lgh_name) {
1157                 struct dt_object *llog_dir;
1158
1159                 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1160                 if (IS_ERR(llog_dir))
1161                         RETURN(PTR_ERR(llog_dir));
1162
1163                 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1164                 rec->rec_fid = &lgi->lgi_fid;
1165                 rec->rec_type = S_IFREG;
1166                 dt_read_lock(env, llog_dir, 0);
1167                 rc = dt_insert(env, llog_dir, (struct dt_rec *)rec,
1168                                (struct dt_key *)res->lgh_name,
1169                                th, 1);
1170                 dt_read_unlock(env, llog_dir);
1171                 lu_object_put(env, &llog_dir->do_lu);
1172                 if (rc)
1173                         CERROR("%s: can't create named llog %s: rc = %d\n",
1174                                o->do_lu.lo_dev->ld_obd->obd_name,
1175                                res->lgh_name, rc);
1176         }
1177         RETURN(rc);
1178 }
1179
1180 /**
1181  * Implementation of the llog_operations::lop_close
1182  *
1183  * This function closes the llog. It just put llog object and referenced
1184  * local storage.
1185  *
1186  * \param[in] env       execution environment
1187  * \param[in] handle    llog handle of the current llog
1188  *
1189  * \retval              0 on successful llog close
1190  * \retval              negative value on error
1191  */
1192 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
1193 {
1194         struct local_oid_storage        *los;
1195         int                              rc = 0;
1196
1197         ENTRY;
1198
1199         LASSERT(handle->lgh_obj);
1200
1201         lu_object_put(env, &handle->lgh_obj->do_lu);
1202
1203         los = handle->private_data;
1204         LASSERT(los);
1205         dt_los_put(los);
1206
1207         if (handle->lgh_name)
1208                 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
1209
1210         RETURN(rc);
1211 }
1212
1213 /**
1214  * Implementation of the llog_operations::lop_destroy
1215  *
1216  * This function destroys the llog and deletes also entry in the
1217  * llog directory in case of named llog. Llog should be opened prior that.
1218  * Destroy method is not part of external transaction and does everything
1219  * inside.
1220  *
1221  * \param[in] env               execution environment
1222  * \param[in] loghandle llog handle of the current llog
1223  *
1224  * \retval              0 on successful destroy
1225  * \retval              negative value on error
1226  */
1227 static int llog_osd_destroy(const struct lu_env *env,
1228                             struct llog_handle *loghandle)
1229 {
1230         struct llog_ctxt        *ctxt;
1231         struct dt_object        *o, *llog_dir = NULL;
1232         struct dt_device        *d;
1233         struct thandle          *th;
1234         char                    *name = NULL;
1235         int                      rc;
1236
1237         ENTRY;
1238
1239         ctxt = loghandle->lgh_ctxt;
1240         LASSERT(ctxt);
1241
1242         o = loghandle->lgh_obj;
1243         LASSERT(o);
1244
1245         d = lu2dt_dev(o->do_lu.lo_dev);
1246         LASSERT(d);
1247         LASSERT(d == ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt);
1248
1249         th = dt_trans_create(env, d);
1250         if (IS_ERR(th))
1251                 RETURN(PTR_ERR(th));
1252
1253         if (loghandle->lgh_name) {
1254                 llog_dir = llog_osd_dir_get(env, ctxt);
1255                 if (IS_ERR(llog_dir))
1256                         GOTO(out_trans, rc = PTR_ERR(llog_dir));
1257
1258                 name = loghandle->lgh_name;
1259                 rc = dt_declare_delete(env, llog_dir,
1260                                        (struct dt_key *)name, th);
1261                 if (rc)
1262                         GOTO(out_trans, rc);
1263         }
1264
1265         rc = dt_declare_ref_del(env, o, th);
1266         if (rc < 0)
1267                 GOTO(out_trans, rc);
1268
1269         rc = dt_declare_destroy(env, o, th);
1270         if (rc)
1271                 GOTO(out_trans, rc);
1272
1273         rc = dt_trans_start_local(env, d, th);
1274         if (rc)
1275                 GOTO(out_trans, rc);
1276
1277         dt_write_lock(env, o, 0);
1278         if (dt_object_exists(o)) {
1279                 if (name) {
1280                         dt_read_lock(env, llog_dir, 0);
1281                         rc = dt_delete(env, llog_dir,
1282                                        (struct dt_key *) name,
1283                                        th);
1284                         dt_read_unlock(env, llog_dir);
1285                         if (rc) {
1286                                 CERROR("%s: can't remove llog %s: rc = %d\n",
1287                                        o->do_lu.lo_dev->ld_obd->obd_name,
1288                                        name, rc);
1289                                 GOTO(out_unlock, rc);
1290                         }
1291                 }
1292                 dt_ref_del(env, o, th);
1293                 rc = dt_destroy(env, o, th);
1294                 if (rc)
1295                         GOTO(out_unlock, rc);
1296         }
1297 out_unlock:
1298         dt_write_unlock(env, o);
1299 out_trans:
1300         dt_trans_stop(env, d, th);
1301         if (llog_dir != NULL)
1302                 lu_object_put(env, &llog_dir->do_lu);
1303         RETURN(rc);
1304 }
1305
1306 /**
1307  * Implementation of the llog_operations::lop_setup
1308  *
1309  * This function setup the llog on local storage.
1310  *
1311  * \param[in] env       execution environment
1312  * \param[in] obd       obd device the llog belongs to
1313  * \param[in] olg       the llog group, it is always zero group now.
1314  * \param[in] ctxt_idx  the llog index, it defines the purpose of this llog.
1315  *                      Every new llog type have to use own index.
1316  * \param[in] disk_obd  the storage obd, where llog is stored.
1317  *
1318  * \retval              0 on successful llog setup
1319  * \retval              negative value on error
1320  */
1321 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1322                           struct obd_llog_group *olg, int ctxt_idx,
1323                           struct obd_device *disk_obd)
1324 {
1325         struct llog_thread_info         *lgi = llog_info(env);
1326         struct llog_ctxt                *ctxt;
1327         int                              rc = 0;
1328         ENTRY;
1329
1330         LASSERT(obd);
1331         LASSERT(olg->olg_ctxts[ctxt_idx]);
1332
1333         ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1334         LASSERT(ctxt);
1335
1336         /* initialize data allowing to generate new fids,
1337          * literally we need a sequece */
1338         lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1339         lgi->lgi_fid.f_oid = 1;
1340         lgi->lgi_fid.f_ver = 0;
1341         rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1342                                     &lgi->lgi_fid,
1343                                     &ctxt->loc_los_nameless);
1344         if (rc != 0)
1345                 GOTO(out, rc);
1346
1347         lgi->lgi_fid.f_seq = FID_SEQ_LLOG_NAME;
1348         lgi->lgi_fid.f_oid = 1;
1349         lgi->lgi_fid.f_ver = 0;
1350         rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1351                                     &lgi->lgi_fid,
1352                                     &ctxt->loc_los_named);
1353         if (rc != 0) {
1354                 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1355                 ctxt->loc_los_nameless = NULL;
1356         }
1357
1358         GOTO(out, rc);
1359
1360 out:
1361         llog_ctxt_put(ctxt);
1362         return rc;
1363 }
1364
1365 /**
1366  * Implementation of the llog_operations::lop_cleanup
1367  *
1368  * This function cleanups the llog on local storage.
1369  *
1370  * \param[in] env       execution environment
1371  * \param[in] ctxt      the llog context
1372  *
1373  * \retval              0
1374  */
1375 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1376 {
1377         if (ctxt->loc_los_nameless != NULL) {
1378                 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1379                 ctxt->loc_los_nameless = NULL;
1380         }
1381
1382         if (ctxt->loc_los_named != NULL) {
1383                 local_oid_storage_fini(env, ctxt->loc_los_named);
1384                 ctxt->loc_los_named = NULL;
1385         }
1386
1387         return 0;
1388 }
1389
1390 struct llog_operations llog_osd_ops = {
1391         .lop_next_block         = llog_osd_next_block,
1392         .lop_prev_block         = llog_osd_prev_block,
1393         .lop_read_header        = llog_osd_read_header,
1394         .lop_destroy            = llog_osd_destroy,
1395         .lop_setup              = llog_osd_setup,
1396         .lop_cleanup            = llog_osd_cleanup,
1397         .lop_open               = llog_osd_open,
1398         .lop_exist              = llog_osd_exist,
1399         .lop_declare_create     = llog_osd_declare_create,
1400         .lop_create             = llog_osd_create,
1401         .lop_declare_write_rec  = llog_osd_declare_write_rec,
1402         .lop_write_rec          = llog_osd_write_rec,
1403         .lop_close              = llog_osd_close,
1404 };
1405 EXPORT_SYMBOL(llog_osd_ops);
1406
1407 /**
1408  * Read the special file which contains the list of llog catalogs IDs
1409  *
1410  * This function reads the CATALOGS file which contains the array of llog
1411  * catalogs IDs. The main purpose of this file is to store OSP llogs indexed
1412  * by OST/MDT number.
1413  *
1414  * \param[in]  env              execution environment
1415  * \param[in]  d                corresponding storage device
1416  * \param[in]  idx              position to start from, usually OST/MDT index
1417  * \param[in]  count            how many catalog IDs to read
1418  * \param[out] idarray          the buffer for the data. If it is NULL then
1419  *                              function returns just number of catalog IDs
1420  *                              in the file.
1421  * \param[in]  fid              LLOG_CATALOGS_OID for CATALOG object
1422  *
1423  * \retval                      0 on successful read of catalog IDs
1424  * \retval                      negative value on error
1425  * \retval                      positive value which is number of records in
1426  *                              the file if \a idarray is NULL
1427  */
1428 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
1429                           int idx, int count, struct llog_catid *idarray,
1430                           const struct lu_fid *fid)
1431 {
1432         struct llog_thread_info *lgi = llog_info(env);
1433         struct dt_object        *o = NULL;
1434         struct thandle          *th;
1435         int                      rc, size;
1436
1437         ENTRY;
1438
1439         LASSERT(d);
1440
1441         size = sizeof(*idarray) * count;
1442         lgi->lgi_off = idx *  sizeof(*idarray);
1443
1444         lgi->lgi_fid = *fid;
1445         o = dt_locate(env, d, &lgi->lgi_fid);
1446         if (IS_ERR(o))
1447                 RETURN(PTR_ERR(o));
1448
1449         if (!dt_object_exists(o)) {
1450                 th = dt_trans_create(env, d);
1451                 if (IS_ERR(th))
1452                         GOTO(out, rc = PTR_ERR(th));
1453
1454                 lgi->lgi_attr.la_valid = LA_MODE;
1455                 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1456                 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1457
1458                 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1459                                        &lgi->lgi_dof, th);
1460                 if (rc)
1461                         GOTO(out_trans, rc);
1462
1463                 rc = dt_trans_start_local(env, d, th);
1464                 if (rc)
1465                         GOTO(out_trans, rc);
1466
1467                 dt_write_lock(env, o, 0);
1468                 if (!dt_object_exists(o))
1469                         rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1470                                        &lgi->lgi_dof, th);
1471                 dt_write_unlock(env, o);
1472 out_trans:
1473                 dt_trans_stop(env, d, th);
1474                 if (rc)
1475                         GOTO(out, rc);
1476         }
1477
1478         rc = dt_attr_get(env, o, &lgi->lgi_attr);
1479         if (rc)
1480                 GOTO(out, rc);
1481
1482         if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1483                 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1484                        o->do_lu.lo_dev->ld_obd->obd_name,
1485                        lgi->lgi_attr.la_mode);
1486                 GOTO(out, rc = -ENOENT);
1487         }
1488
1489         CDEBUG(D_CONFIG, "cat list: disk size=%d, read=%d\n",
1490                (int)lgi->lgi_attr.la_size, size);
1491
1492         /* return just number of llogs */
1493         if (idarray == NULL) {
1494                 rc = lgi->lgi_attr.la_size / sizeof(*idarray);
1495                 GOTO(out, rc);
1496         }
1497
1498         /* read for new ost index or for empty file */
1499         memset(idarray, 0, size);
1500         if (lgi->lgi_attr.la_size <= lgi->lgi_off)
1501                 GOTO(out, rc = 0);
1502         if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
1503                 size = lgi->lgi_attr.la_size - lgi->lgi_off;
1504
1505         lgi->lgi_buf.lb_buf = idarray;
1506         lgi->lgi_buf.lb_len = size;
1507         rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
1508         /* -EFAULT means the llog is a sparse file. This is not an error
1509          * after arbitrary OST index is supported. */
1510         if (rc < 0 && rc != -EFAULT) {
1511                 CERROR("%s: error reading CATALOGS: rc = %d\n",
1512                        o->do_lu.lo_dev->ld_obd->obd_name,  rc);
1513                 GOTO(out, rc);
1514         }
1515
1516         EXIT;
1517 out:
1518         lu_object_put(env, &o->do_lu);
1519         RETURN(rc);
1520 }
1521 EXPORT_SYMBOL(llog_osd_get_cat_list);
1522
1523 /**
1524  * Write the special file which contains the list of llog catalogs IDs
1525  *
1526  * This function writes the CATALOG file which contains the array of llog
1527  * catalogs IDs. It is used mostly to store OSP llogs indexed by OST/MDT
1528  * number.
1529  *
1530  * \param[in]  env      execution environment
1531  * \param[in]  d        corresponding storage device
1532  * \param[in]  idx      position to start from, usually OST/MDT index
1533  * \param[in]  count    how many catalog IDs to write
1534  * \param[out] idarray  the buffer with the data to write.
1535  * \param[in]  fid      LLOG_CATALOGS_OID for CATALOG object
1536  *
1537  * \retval              0 on successful write of catalog IDs
1538  * \retval              negative value on error
1539  */
1540 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
1541                           int idx, int count, struct llog_catid *idarray,
1542                           const struct lu_fid *fid)
1543 {
1544         struct llog_thread_info *lgi = llog_info(env);
1545         struct dt_object        *o = NULL;
1546         struct thandle          *th;
1547         int                      rc, size;
1548
1549         if (count == 0)
1550                 RETURN(0);
1551
1552         LASSERT(d);
1553
1554         size = sizeof(*idarray) * count;
1555         lgi->lgi_off = idx * sizeof(*idarray);
1556         lgi->lgi_fid = *fid;
1557
1558         o = dt_locate(env, d, &lgi->lgi_fid);
1559         if (IS_ERR(o))
1560                 RETURN(PTR_ERR(o));
1561
1562         if (!dt_object_exists(o))
1563                 GOTO(out, rc = -ENOENT);
1564
1565         rc = dt_attr_get(env, o, &lgi->lgi_attr);
1566         if (rc)
1567                 GOTO(out, rc);
1568
1569         if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1570                 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1571                        o->do_lu.lo_dev->ld_obd->obd_name,
1572                        lgi->lgi_attr.la_mode);
1573                 GOTO(out, rc = -ENOENT);
1574         }
1575
1576         th = dt_trans_create(env, d);
1577         if (IS_ERR(th))
1578                 GOTO(out, rc = PTR_ERR(th));
1579
1580         lgi->lgi_buf.lb_len = size;
1581         lgi->lgi_buf.lb_buf = idarray;
1582         rc = dt_declare_record_write(env, o, &lgi->lgi_buf, lgi->lgi_off, th);
1583         if (rc)
1584                 GOTO(out, rc);
1585
1586         rc = dt_trans_start_local(env, d, th);
1587         if (rc)
1588                 GOTO(out_trans, rc);
1589
1590         rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
1591         if (rc)
1592                 CDEBUG(D_INODE, "can't write CATALOGS at index %d: rc = %d\n",
1593                        idx, rc);
1594 out_trans:
1595         dt_trans_stop(env, d, th);
1596 out:
1597         lu_object_put(env, &o->do_lu);
1598         RETURN(rc);
1599 }
1600 EXPORT_SYMBOL(llog_osd_put_cat_list);