Whamcloud - gitweb
LU-7001 osp: fix llog processing
[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, 2016, 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 <dt_object.h>
48 #include <llog_swab.h>
49 #include <lustre_fid.h>
50 #include <obd.h>
51 #include <obd_class.h>
52
53 #include "llog_internal.h"
54 #include "local_storage.h"
55
56 /**
57  * Implementation of the llog_operations::lop_declare_create
58  *
59  * This function is a wrapper over local_storage API function
60  * local_object_declare_create().
61  *
62  * \param[in] env       execution environment
63  * \param[in] los       local_storage for bottom storage device
64  * \param[in] o         dt_object to create
65  * \param[in] th        current transaction handle
66  *
67  * \retval              0 on successful declaration of the new object
68  * \retval              negative error if declaration was failed
69  */
70 static int llog_osd_declare_new_object(const struct lu_env *env,
71                                        struct local_oid_storage *los,
72                                        struct dt_object *o,
73                                        struct thandle *th)
74 {
75         struct llog_thread_info *lgi = llog_info(env);
76
77         lgi->lgi_attr.la_valid = LA_MODE;
78         lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
79         lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
80
81         return local_object_declare_create(env, los, o, &lgi->lgi_attr,
82                                            &lgi->lgi_dof, th);
83 }
84
85 /**
86  * Implementation of the llog_operations::lop_create
87  *
88  * This function is a wrapper over local_storage API function
89  * local_object_create().
90  *
91  * \param[in] env       execution environment
92  * \param[in] los       local_storage for bottom storage device
93  * \param[in] o         dt_object to create
94  * \param[in] th        current transaction handle
95  *
96  * \retval              0 on successful creation of the new object
97  * \retval              negative error if creation was failed
98  */
99 static int llog_osd_create_new_object(const struct lu_env *env,
100                                       struct local_oid_storage *los,
101                                       struct dt_object *o,
102                                       struct thandle *th)
103 {
104         struct llog_thread_info *lgi = llog_info(env);
105
106         lgi->lgi_attr.la_valid = LA_MODE;
107         lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
108         lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
109
110         return local_object_create(env, los, o, &lgi->lgi_attr,
111                                    &lgi->lgi_dof, th);
112 }
113
114 /**
115  * Implementation of the llog_operations::lop_exist
116  *
117  * This function checks that llog exists on storage.
118  *
119  * \param[in] handle    llog handle of the current llog
120  *
121  * \retval              true if llog object exists and is not just destroyed
122  * \retval              false if llog doesn't exist or just destroyed
123  */
124 static int llog_osd_exist(struct llog_handle *handle)
125 {
126         LASSERT(handle->lgh_obj);
127         return dt_object_exists(handle->lgh_obj) &&
128                 !lu_object_is_dying(handle->lgh_obj->do_lu.lo_header);
129 }
130
131 static void *rec_tail(struct llog_rec_hdr *rec)
132 {
133         return (void *)((char *)rec + rec->lrh_len -
134                         sizeof(struct llog_rec_tail));
135 }
136
137 /**
138  * Write a padding record to the llog
139  *
140  * This function writes a padding record to the end of llog. That may
141  * be needed if llog contains records of variable size, e.g. config logs
142  * or changelogs.
143  * The padding record just aligns llog to the llog chunk_size boundary if
144  * the current record doesn't fit in the remaining space.
145  *
146  * It allocates full length to avoid two separate writes for header and tail.
147  * Such 2-steps scheme needs extra protection and complex error handling.
148  *
149  * \param[in]     env   execution environment
150  * \param[in]     o     dt_object to create
151  * \param[in,out] off   pointer to the padding start offset
152  * \param[in]     len   padding length
153  * \param[in]     index index of the padding record in a llog
154  * \param[in]     th    current transaction handle
155  *
156  * \retval              0 on successful padding write
157  * \retval              negative error if write failed
158  */
159 static int llog_osd_pad(const struct lu_env *env, struct dt_object *o,
160                         loff_t *off, int len, int index, struct thandle *th)
161 {
162         struct llog_thread_info *lgi = llog_info(env);
163         struct llog_rec_hdr     *rec;
164         struct llog_rec_tail    *tail;
165         int                      rc;
166
167         ENTRY;
168
169         LASSERT(th);
170         LASSERT(off);
171         LASSERT(len >= LLOG_MIN_REC_SIZE && (len & 0x7) == 0);
172
173         OBD_ALLOC(rec, len);
174         if (rec == NULL)
175                 RETURN(-ENOMEM);
176
177         rec->lrh_len = len;
178         rec->lrh_index = index;
179         rec->lrh_type = LLOG_PAD_MAGIC;
180
181         tail = rec_tail(rec);
182         tail->lrt_len = len;
183         tail->lrt_index = index;
184
185         lgi->lgi_buf.lb_buf = rec;
186         lgi->lgi_buf.lb_len = len;
187         rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
188         if (rc)
189                 CERROR("%s: error writing padding record: rc = %d\n",
190                        o->do_lu.lo_dev->ld_obd->obd_name, rc);
191
192         OBD_FREE(rec, len);
193         RETURN(rc);
194 }
195
196 /**
197  * Implementation of the llog_operations::lop_read_header
198  *
199  * This function reads the current llog header from the bottom storage
200  * device.
201  *
202  * \param[in] env       execution environment
203  * \param[in] handle    llog handle of the current llog
204  *
205  * \retval              0 on successful header read
206  * \retval              negative error if read failed
207  */
208 static int llog_osd_read_header(const struct lu_env *env,
209                                 struct llog_handle *handle)
210 {
211         struct llog_rec_hdr     *llh_hdr;
212         struct dt_object        *o;
213         struct llog_thread_info *lgi;
214         enum llog_flag           flags;
215         int                      rc;
216
217         ENTRY;
218
219         o = handle->lgh_obj;
220         LASSERT(o);
221
222         lgi = llog_info(env);
223
224         rc = dt_attr_get(env, o, &lgi->lgi_attr);
225         if (rc)
226                 RETURN(rc);
227
228         LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
229
230         if (lgi->lgi_attr.la_size == 0) {
231                 CDEBUG(D_HA, "not reading header from 0-byte log\n");
232                 RETURN(LLOG_EEMPTY);
233         }
234
235         flags = handle->lgh_hdr->llh_flags;
236
237         lgi->lgi_off = 0;
238         lgi->lgi_buf.lb_buf = handle->lgh_hdr;
239         lgi->lgi_buf.lb_len = handle->lgh_hdr_size;
240         rc = dt_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
241         llh_hdr = &handle->lgh_hdr->llh_hdr;
242         if (rc < sizeof(*llh_hdr) || rc < llh_hdr->lrh_len) {
243                 CERROR("%s: error reading "DFID" log header size %d: rc = %d\n",
244                        o->do_lu.lo_dev->ld_obd->obd_name,
245                        PFID(lu_object_fid(&o->do_lu)), rc < 0 ? 0 : rc,
246                        -EFAULT);
247
248                 if (rc >= 0)
249                         rc = -EFAULT;
250
251                 RETURN(rc);
252         }
253
254         if (LLOG_REC_HDR_NEEDS_SWABBING(llh_hdr))
255                 lustre_swab_llog_hdr(handle->lgh_hdr);
256
257         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
258                 CERROR("%s: bad log %s "DFID" header magic: %#x "
259                        "(expected %#x)\n", o->do_lu.lo_dev->ld_obd->obd_name,
260                        handle->lgh_name ? handle->lgh_name : "",
261                        PFID(lu_object_fid(&o->do_lu)),
262                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
263                 RETURN(-EIO);
264         } else if (llh_hdr->lrh_len < LLOG_MIN_CHUNK_SIZE ||
265                    llh_hdr->lrh_len > handle->lgh_hdr_size) {
266                 CERROR("%s: incorrectly sized log %s "DFID" header: "
267                        "%#x (expected at least %#x)\n"
268                        "you may need to re-run lconf --write_conf.\n",
269                        o->do_lu.lo_dev->ld_obd->obd_name,
270                        handle->lgh_name ? handle->lgh_name : "",
271                        PFID(lu_object_fid(&o->do_lu)),
272                        llh_hdr->lrh_len, LLOG_MIN_CHUNK_SIZE);
273                 RETURN(-EIO);
274         } else if (LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index >
275                    LLOG_HDR_BITMAP_SIZE(handle->lgh_hdr) ||
276                    LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len !=
277                         llh_hdr->lrh_len) {
278                 CERROR("%s: incorrectly sized log %s "DFID" tailer: "
279                        "%#x : rc = %d\n",
280                        o->do_lu.lo_dev->ld_obd->obd_name,
281                        handle->lgh_name ? handle->lgh_name : "",
282                        PFID(lu_object_fid(&o->do_lu)),
283                        LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len, -EIO);
284                 RETURN(-EIO);
285         }
286
287         handle->lgh_hdr->llh_flags |= (flags & LLOG_F_EXT_MASK);
288         handle->lgh_last_idx = LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index;
289
290         RETURN(0);
291 }
292
293 /**
294  * Implementation of the llog_operations::lop_declare_write
295  *
296  * This function declares the new record write.
297  *
298  * \param[in] env       execution environment
299  * \param[in] loghandle llog handle of the current llog
300  * \param[in] rec       llog record header. This is a real header of the full
301  *                      llog record to write. This is the beginning of buffer
302  *                      to write, the length of buffer is stored in
303  *                      \a rec::lrh_len
304  * \param[in] idx       index of the llog record. If \a idx == -1 then this is
305  *                      append case, otherwise \a idx is the index of record
306  *                      to modify
307  * \param[in] th        current transaction handle
308  *
309  * \retval              0 on successful declaration
310  * \retval              negative error if declaration failed
311  */
312 static int llog_osd_declare_write_rec(const struct lu_env *env,
313                                       struct llog_handle *loghandle,
314                                       struct llog_rec_hdr *rec,
315                                       int idx, struct thandle *th)
316 {
317         struct llog_thread_info *lgi = llog_info(env);
318         __u32                   chunk_size;
319         struct dt_object        *o;
320         int                      rc;
321
322         ENTRY;
323
324         LASSERT(env);
325         LASSERT(th);
326         LASSERT(loghandle);
327         LASSERT(rec);
328         LASSERT(rec->lrh_len <= loghandle->lgh_ctxt->loc_chunk_size);
329
330         o = loghandle->lgh_obj;
331         LASSERT(o);
332
333         chunk_size = loghandle->lgh_ctxt->loc_chunk_size;
334         lgi->lgi_buf.lb_len = chunk_size;
335         lgi->lgi_buf.lb_buf = NULL;
336         /* each time we update header */
337         rc = dt_declare_record_write(env, o, &lgi->lgi_buf, 0,
338                                      th);
339         if (rc || idx == 0) /* if error or just header */
340                 RETURN(rc);
341
342         /**
343          * the pad record can be inserted so take into account double
344          * record size
345          */
346         lgi->lgi_buf.lb_len = chunk_size * 2;
347         lgi->lgi_buf.lb_buf = NULL;
348         /* XXX: implement declared window or multi-chunks approach */
349         rc = dt_declare_record_write(env, o, &lgi->lgi_buf, -1, th);
350
351         RETURN(rc);
352 }
353
354 /**
355  * Implementation of the llog_operations::lop_write
356  *
357  * This function writes the new record in the llog or modify the existed one.
358  *
359  * \param[in]  env              execution environment
360  * \param[in]  loghandle        llog handle of the current llog
361  * \param[in]  rec              llog record header. This is a real header of
362  *                              the full llog record to write. This is
363  *                              the beginning of buffer to write, the length
364  *                              of buffer is stored in \a rec::lrh_len
365  * \param[out] reccookie        pointer to the cookie to return back if needed.
366  *                              It is used for further cancel of this llog
367  *                              record.
368  * \param[in]  idx              index of the llog record. If \a idx == -1 then
369  *                              this is append case, otherwise \a idx is
370  *                              the index of record to modify
371  * \param[in]  th               current transaction handle
372  *
373  * \retval                      0 on successful write && \a reccookie == NULL
374  *                              1 on successful write && \a reccookie != NULL
375  * \retval                      negative error if write failed
376  */
377 static int llog_osd_write_rec(const struct lu_env *env,
378                               struct llog_handle *loghandle,
379                               struct llog_rec_hdr *rec,
380                               struct llog_cookie *reccookie,
381                               int idx, struct thandle *th)
382 {
383         struct llog_thread_info *lgi = llog_info(env);
384         struct llog_log_hdr     *llh;
385         int                      reclen = rec->lrh_len;
386         int                      index, rc;
387         struct llog_rec_tail    *lrt;
388         struct dt_object        *o;
389         __u32                   chunk_size;
390         size_t                   left;
391         __u32                   orig_last_idx;
392         ENTRY;
393
394         llh = loghandle->lgh_hdr;
395         o = loghandle->lgh_obj;
396
397         chunk_size = llh->llh_hdr.lrh_len;
398         CDEBUG(D_OTHER, "new record %x to "DFID"\n",
399                rec->lrh_type, PFID(lu_object_fid(&o->do_lu)));
400
401         if (!llog_osd_exist(loghandle))
402                 RETURN(-ENOENT);
403
404         /* record length should not bigger than  */
405         if (reclen > loghandle->lgh_hdr->llh_hdr.lrh_len)
406                 RETURN(-E2BIG);
407
408         /* sanity check for fixed-records llog */
409         if (idx != LLOG_HEADER_IDX && (llh->llh_flags & LLOG_F_IS_FIXSIZE)) {
410                 LASSERT(llh->llh_size != 0);
411                 LASSERT(llh->llh_size == reclen);
412         }
413
414         rc = dt_attr_get(env, o, &lgi->lgi_attr);
415         if (rc)
416                 RETURN(rc);
417
418         /**
419          * The modification case.
420          * If idx set then the record with that index must be modified.
421          * There are three cases possible:
422          * 1) the common case is the llog header update (idx == 0)
423          * 2) the llog record modification during llog process.
424          *    This is indicated by the \a loghandle::lgh_cur_idx > 0.
425          *    In that case the \a loghandle::lgh_cur_offset
426          * 3) otherwise this is assumed that llog consist of records of
427          *    fixed size, i.e. catalog. The llog header must has llh_size
428          *    field equal to record size. The record offset is calculated
429          *    just by /a idx value
430          *
431          * During modification we don't need extra header update because
432          * the bitmap and record count are not changed. The record header
433          * and tail remains the same too.
434          */
435         if (idx != LLOG_NEXT_IDX) {
436                 /* llog can be empty only when first record is being written */
437                 LASSERT(ergo(idx > 0, lgi->lgi_attr.la_size > 0));
438
439                 if (!ext2_test_bit(idx, LLOG_HDR_BITMAP(llh))) {
440                         CERROR("%s: modify unset record %u\n",
441                                o->do_lu.lo_dev->ld_obd->obd_name, idx);
442                         RETURN(-ENOENT);
443                 }
444
445                 if (idx != rec->lrh_index) {
446                         CERROR("%s: modify index mismatch %d %u\n",
447                                o->do_lu.lo_dev->ld_obd->obd_name, idx,
448                                rec->lrh_index);
449                         RETURN(-EFAULT);
450                 }
451
452                 if (idx == LLOG_HEADER_IDX) {
453                         /* llog header update */
454                         __u32   *bitmap = LLOG_HDR_BITMAP(llh);
455
456                         lgi->lgi_off = 0;
457
458                         /* If it does not indicate the bitmap index
459                          * (reccookie == NULL), then it means update
460                          * the whole update header. Otherwise only
461                          * update header and bits needs to be updated,
462                          * and in DNE cases, it will signaficantly
463                          * shrink the RPC size.
464                          * see distribute_txn_cancel_records()*/
465                         if (reccookie == NULL) {
466                                 lgi->lgi_buf.lb_len = reclen;
467                                 lgi->lgi_buf.lb_buf = rec;
468                                 rc = dt_record_write(env, o, &lgi->lgi_buf,
469                                                      &lgi->lgi_off, th);
470                                 RETURN(rc);
471                         }
472
473                         /* update the header */
474                         lgi->lgi_buf.lb_len = llh->llh_bitmap_offset;
475                         lgi->lgi_buf.lb_buf = llh;
476                         rc = dt_record_write(env, o, &lgi->lgi_buf,
477                                              &lgi->lgi_off, th);
478                         if (rc != 0)
479                                 RETURN(rc);
480
481                         /* update the bitmap */
482                         index = reccookie->lgc_index;
483                         lgi->lgi_off = llh->llh_bitmap_offset +
484                                       (index / (sizeof(*bitmap) * 8)) *
485                                                         sizeof(*bitmap);
486                         lgi->lgi_buf.lb_len = sizeof(*bitmap);
487                         lgi->lgi_buf.lb_buf =
488                                         &bitmap[index/(sizeof(*bitmap)*8)];
489                         rc = dt_record_write(env, o, &lgi->lgi_buf,
490                                              &lgi->lgi_off, th);
491
492                         RETURN(rc);
493                 } else if (loghandle->lgh_cur_idx > 0) {
494                         /**
495                          * The lgh_cur_offset can be used only if index is
496                          * the same.
497                          */
498                         if (idx != loghandle->lgh_cur_idx) {
499                                 CERROR("%s: modify index mismatch %d %d\n",
500                                        o->do_lu.lo_dev->ld_obd->obd_name, idx,
501                                        loghandle->lgh_cur_idx);
502                                 RETURN(-EFAULT);
503                         }
504
505                         lgi->lgi_off = loghandle->lgh_cur_offset;
506                         CDEBUG(D_OTHER, "modify record "DFID": idx:%u, "
507                                "len:%u offset %llu\n",
508                                PFID(&loghandle->lgh_id.lgl_oi.oi_fid), idx,
509                                rec->lrh_len, (long long)lgi->lgi_off);
510                 } else if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
511                         lgi->lgi_off = llh->llh_hdr.lrh_len +
512                                        (idx - 1) * reclen;
513                 } else {
514                         /* This can be result of lgh_cur_idx is not set during
515                          * llog processing or llh_size is not set to proper
516                          * record size for fixed records llog. Therefore it is
517                          * impossible to get record offset. */
518                         CERROR("%s: can't get record offset, idx:%d, "
519                                "len:%u.\n", o->do_lu.lo_dev->ld_obd->obd_name,
520                                idx, rec->lrh_len);
521                         RETURN(-EFAULT);
522                 }
523
524                 /* update only data, header and tail remain the same */
525                 lgi->lgi_off += sizeof(struct llog_rec_hdr);
526                 lgi->lgi_buf.lb_len = REC_DATA_LEN(rec);
527                 lgi->lgi_buf.lb_buf = REC_DATA(rec);
528                 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
529                 if (rc == 0 && reccookie) {
530                         reccookie->lgc_lgl = loghandle->lgh_id;
531                         reccookie->lgc_index = idx;
532                         rc = 1;
533                 }
534                 RETURN(rc);
535         }
536
537         /**
538          * The append case.
539          * The most common case of using llog. The new index is assigned to
540          * the new record, new bit is set in llog bitmap and llog count is
541          * incremented.
542          *
543          * Make sure that records don't cross a chunk boundary, so we can
544          * process them page-at-a-time if needed.  If it will cross a chunk
545          * boundary, write in a fake (but referenced) entry to pad the chunk.
546          */
547
548
549         /* simulate ENOSPC when new plain llog is being added to the
550          * catalog */
551         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2) &&
552             llh->llh_flags & LLOG_F_IS_CAT)
553                 RETURN(-ENOSPC);
554
555         LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
556         orig_last_idx = loghandle->lgh_last_idx;
557         lgi->lgi_off = lgi->lgi_attr.la_size;
558
559         if (loghandle->lgh_max_size > 0 &&
560             lgi->lgi_off >= loghandle->lgh_max_size) {
561                 CDEBUG(D_OTHER, "llog is getting too large (%u > %u) at %u "
562                        DFID"\n", (unsigned)lgi->lgi_off,
563                        loghandle->lgh_max_size, (int)loghandle->lgh_last_idx,
564                        PFID(&loghandle->lgh_id.lgl_oi.oi_fid));
565                 /* this is to signal that this llog is full */
566                 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) - 1;
567                 RETURN(-ENOSPC);
568         }
569
570         left = chunk_size - (lgi->lgi_off & (chunk_size - 1));
571         /* NOTE: padding is a record, but no bit is set */
572         if (left != 0 && left != reclen &&
573             left < (reclen + LLOG_MIN_REC_SIZE)) {
574                 index = loghandle->lgh_last_idx + 1;
575                 rc = llog_osd_pad(env, o, &lgi->lgi_off, left, index, th);
576                 if (rc)
577                         RETURN(rc);
578
579                 loghandle->lgh_last_idx++; /* for pad rec */
580         }
581         /* if it's the last idx in log file, then return -ENOSPC
582          * or wrap around if a catalog */
583         if (llog_is_full(loghandle) ||
584             unlikely(llh->llh_flags & LLOG_F_IS_CAT &&
585                      OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS) &&
586                      loghandle->lgh_last_idx >= cfs_fail_val)) {
587                 if (llh->llh_flags & LLOG_F_IS_CAT)
588                         loghandle->lgh_last_idx = 0;
589                 else
590                         RETURN(-ENOSPC);
591         }
592
593         /* increment the last_idx along with llh_tail index, they should
594          * be equal for a llog lifetime */
595         loghandle->lgh_last_idx++;
596         index = loghandle->lgh_last_idx;
597         LLOG_HDR_TAIL(llh)->lrt_index = index;
598         /**
599          * NB: the caller should make sure only 1 process access
600          * the lgh_last_idx, e.g. append should be exclusive.
601          * Otherwise it might hit the assert.
602          */
603         LASSERT(index < LLOG_HDR_BITMAP_SIZE(llh));
604         rec->lrh_index = index;
605         lrt = rec_tail(rec);
606         lrt->lrt_len = rec->lrh_len;
607         lrt->lrt_index = rec->lrh_index;
608
609         /* the lgh_hdr_mutex protects llog header data from concurrent
610          * update/cancel, the llh_count and llh_bitmap are protected */
611         mutex_lock(&loghandle->lgh_hdr_mutex);
612         if (ext2_set_bit(index, LLOG_HDR_BITMAP(llh))) {
613                 CERROR("%s: index %u already set in log bitmap\n",
614                        o->do_lu.lo_dev->ld_obd->obd_name, index);
615                 mutex_unlock(&loghandle->lgh_hdr_mutex);
616                 LBUG(); /* should never happen */
617         }
618         llh->llh_count++;
619
620         if (!(llh->llh_flags & LLOG_F_IS_FIXSIZE)) {
621                 /* Update the minimum size of the llog record */
622                 if (llh->llh_size == 0)
623                         llh->llh_size = reclen;
624                 else if (reclen < llh->llh_size)
625                         llh->llh_size = reclen;
626         }
627
628         if (lgi->lgi_attr.la_size == 0) {
629                 lgi->lgi_off = 0;
630                 lgi->lgi_buf.lb_len = llh->llh_hdr.lrh_len;
631                 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
632                 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
633                 if (rc != 0)
634                         GOTO(out_unlock, rc);
635         } else {
636                 __u32   *bitmap = LLOG_HDR_BITMAP(llh);
637
638                 /* Note: If this is not initialization (size == 0), then do not
639                  * write the whole header (8k bytes), only update header/tail
640                  * and bits needs to be updated. Because this update might be
641                  * part of cross-MDT operation, which needs to write these
642                  * updates into the update log(32KB limit) and also pack inside
643                  * the RPC (1MB limit), if we write 8K for each operation, which
644                  * will cost a lot space, and keep us adding more updates to one
645                  * update log.*/
646                 lgi->lgi_off = 0;
647                 lgi->lgi_buf.lb_len = llh->llh_bitmap_offset;
648                 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
649                 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
650                 if (rc != 0)
651                         GOTO(out_unlock, rc);
652
653                 lgi->lgi_off = llh->llh_bitmap_offset +
654                               (index / (sizeof(*bitmap) * 8)) * sizeof(*bitmap);
655                 lgi->lgi_buf.lb_len = sizeof(*bitmap);
656                 lgi->lgi_buf.lb_buf = &bitmap[index/(sizeof(*bitmap)*8)];
657                 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
658                 if (rc != 0)
659                         GOTO(out_unlock, rc);
660
661                 lgi->lgi_off =  (unsigned long)LLOG_HDR_TAIL(llh) -
662                                 (unsigned long)llh;
663                 lgi->lgi_buf.lb_len = sizeof(llh->llh_tail);
664                 lgi->lgi_buf.lb_buf = LLOG_HDR_TAIL(llh);
665                 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
666                 if (rc != 0)
667                         GOTO(out_unlock, rc);
668         }
669
670 out_unlock:
671         /* unlock here for remote object */
672         mutex_unlock(&loghandle->lgh_hdr_mutex);
673         if (rc)
674                 GOTO(out, rc);
675
676         /* computed index can be used to determine offset for fixed-size
677          * records. This also allows to handle Catalog wrap around case */
678         if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
679                 lgi->lgi_off = llh->llh_hdr.lrh_len + (index - 1) * reclen;
680         } else {
681                 rc = dt_attr_get(env, o, &lgi->lgi_attr);
682                 if (rc)
683                         GOTO(out, rc);
684
685                 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
686                 lgi->lgi_off = max_t(__u64, lgi->lgi_attr.la_size,
687                                      lgi->lgi_off);
688         }
689
690         lgi->lgi_buf.lb_len = reclen;
691         lgi->lgi_buf.lb_buf = rec;
692         rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
693         if (rc < 0)
694                 GOTO(out, rc);
695
696         CDEBUG(D_HA, "added record "DFID".%u, %u off%llu\n",
697                PFID(lu_object_fid(&o->do_lu)), index, rec->lrh_len,
698                lgi->lgi_off);
699         if (reccookie != NULL) {
700                 reccookie->lgc_lgl = loghandle->lgh_id;
701                 reccookie->lgc_index = index;
702                 if ((rec->lrh_type == MDS_UNLINK_REC) ||
703                     (rec->lrh_type == MDS_SETATTR64_REC))
704                         reccookie->lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
705                 else if (rec->lrh_type == OST_SZ_REC)
706                         reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
707                 else
708                         reccookie->lgc_subsys = -1;
709                 rc = 1;
710         }
711         RETURN(rc);
712 out:
713         /* cleanup llog for error case */
714         mutex_lock(&loghandle->lgh_hdr_mutex);
715         ext2_clear_bit(index, LLOG_HDR_BITMAP(llh));
716         llh->llh_count--;
717         mutex_unlock(&loghandle->lgh_hdr_mutex);
718
719         /* restore llog last_idx */
720         if (dt_object_remote(o)) {
721                 loghandle->lgh_last_idx = orig_last_idx;
722         } else if (--loghandle->lgh_last_idx == 0 &&
723             (llh->llh_flags & LLOG_F_IS_CAT) && llh->llh_cat_idx != 0) {
724                 /* catalog had just wrap-around case */
725                 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) - 1;
726         }
727
728         LLOG_HDR_TAIL(llh)->lrt_index = loghandle->lgh_last_idx;
729
730         RETURN(rc);
731 }
732
733 /**
734  * We can skip reading at least as many log blocks as the number of
735  * minimum sized log records we are skipping.  If it turns out
736  * that we are not far enough along the log (because the
737  * actual records are larger than minimum size) we just skip
738  * some more records.
739  *
740  * Note: in llog_process_thread, it will use bitmap offset as
741  * the index to locate the record, which also includs some pad
742  * records, whose record size is very small, and it also does not
743  * consider pad record when recording minimum record size (otherwise
744  * min_record size might be too small), so in some rare cases,
745  * it might skip too much record for @goal, see llog_osd_next_block().
746  *
747  * When force_mini_rec is true, it means we have to use LLOG_MIN_REC_SIZE
748  * as the min record size to skip over, usually because in the previous
749  * try, it skip too much record, see loog_osd_next(prev)_block().
750  */
751 static inline void llog_skip_over(struct llog_handle *lgh, __u64 *off,
752                                   int curr, int goal, __u32 chunk_size,
753                                   bool force_mini_rec)
754 {
755         struct llog_log_hdr *llh = lgh->lgh_hdr;
756
757         /* Goal should not bigger than the record count */
758         if (goal > lgh->lgh_last_idx)
759                 goal = lgh->lgh_last_idx;
760
761         if (goal > curr) {
762                 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
763                         *off = chunk_size + (goal - 1) * llh->llh_size;
764                 } else {
765                         __u64 min_rec_size = LLOG_MIN_REC_SIZE;
766
767                         if (llh->llh_size > 0 && !force_mini_rec)
768                                 min_rec_size = llh->llh_size;
769
770                         *off = *off + (goal - curr - 1) * min_rec_size;
771                 }
772         }
773         /* always align with lower chunk boundary*/
774         *off &= ~(chunk_size - 1);
775 }
776
777 /**
778  * Remove optional fields that the client doesn't expect.
779  * This is typically in order to ensure compatibility with older clients.
780  * It is assumed that since we exclusively remove fields, the block will be
781  * big enough to handle the remapped records. It is also assumed that records
782  * of a block have the same format (i.e.: the same features enabled).
783  *
784  * \param[in,out]    hdr        Header of the block of records to remap.
785  * \param[in,out]    last_hdr   Last header, don't read past this point.
786  * \param[in]        flags      Flags describing the fields to keep.
787  */
788 static void changelog_block_trim_ext(struct llog_rec_hdr *hdr,
789                                      struct llog_rec_hdr *last_hdr,
790                                      enum changelog_rec_flags flags)
791 {
792         if (hdr->lrh_type != CHANGELOG_REC)
793                 return;
794
795         do {
796                 struct changelog_rec *rec = (struct changelog_rec *)(hdr + 1);
797
798                 changelog_remap_rec(rec, rec->cr_flags & flags);
799                 hdr = llog_rec_hdr_next(hdr);
800         } while ((char *)hdr <= (char *)last_hdr);
801 }
802
803 /**
804  * Implementation of the llog_operations::lop_next_block
805  *
806  * This function finds the the next llog block to return which contains
807  * record with required index. It is main part of llog processing.
808  *
809  * \param[in]     env           execution environment
810  * \param[in]     loghandle     llog handle of the current llog
811  * \param[in,out] cur_idx       index preceeding cur_offset
812  * \param[in]     next_idx      target index to find
813  * \param[in,out] cur_offset    furtherst point read in the file
814  * \param[in]     buf           pointer to data buffer to fill
815  * \param[in]     len           required len to read, it is
816  *                              usually llog chunk_size.
817  *
818  * \retval                      0 on successful buffer read
819  * \retval                      negative value on error
820  */
821 static int llog_osd_next_block(const struct lu_env *env,
822                                struct llog_handle *loghandle, int *cur_idx,
823                                int next_idx, __u64 *cur_offset, void *buf,
824                                int len)
825 {
826         struct llog_thread_info *lgi = llog_info(env);
827         struct dt_object        *o;
828         struct dt_device        *dt;
829         int                      rc;
830         __u32                   chunk_size;
831         int last_idx = *cur_idx;
832         __u64 last_offset = *cur_offset;
833         bool force_mini_rec = false;
834
835         ENTRY;
836
837         LASSERT(env);
838         LASSERT(lgi);
839
840         chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
841         if (len == 0 || len & (chunk_size - 1))
842                 RETURN(-EINVAL);
843
844         LASSERT(loghandle);
845         LASSERT(loghandle->lgh_ctxt);
846
847         o = loghandle->lgh_obj;
848         LASSERT(o);
849         LASSERT(dt_object_exists(o));
850         dt = lu2dt_dev(o->do_lu.lo_dev);
851         LASSERT(dt);
852
853         rc = dt_attr_get(env, o, &lgi->lgi_attr);
854         if (rc)
855                 GOTO(out, rc);
856
857         CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off"
858                "%llu), size %llu\n", next_idx, *cur_idx,
859                *cur_offset, lgi->lgi_attr.la_size);
860
861         while (*cur_offset < lgi->lgi_attr.la_size) {
862                 struct llog_rec_hdr     *rec, *last_rec;
863                 struct llog_rec_tail    *tail;
864
865                 llog_skip_over(loghandle, cur_offset, *cur_idx,
866                                next_idx, chunk_size, force_mini_rec);
867
868                 /* read up to next llog chunk_size block */
869                 lgi->lgi_buf.lb_len = chunk_size -
870                                       (*cur_offset & (chunk_size - 1));
871                 lgi->lgi_buf.lb_buf = buf;
872
873                 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
874                 if (rc < 0) {
875                         if (rc == -EBADR && !force_mini_rec)
876                                 goto retry;
877
878                         CERROR("%s: can't read llog block from log "DFID
879                                " offset %llu: rc = %d\n",
880                                o->do_lu.lo_dev->ld_obd->obd_name,
881                                PFID(lu_object_fid(&o->do_lu)), *cur_offset,
882                                rc);
883                         GOTO(out, rc);
884                 }
885
886                 if (rc < len) {
887                         /* signal the end of the valid buffer to
888                          * llog_process */
889                         memset(buf + rc, 0, len - rc);
890                 }
891
892                 if (rc == 0) { /* end of file, nothing to do */
893                         if (!force_mini_rec)
894                                 goto retry;
895                         GOTO(out, rc);
896                 }
897
898                 if (rc < sizeof(*tail)) {
899                         if (!force_mini_rec)
900                                 goto retry;
901
902                         CERROR("%s: invalid llog block at log id "DFID":%x "
903                                "offset %llu\n",
904                                o->do_lu.lo_dev->ld_obd->obd_name,
905                                PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
906                                loghandle->lgh_id.lgl_ogen, *cur_offset);
907                         GOTO(out, rc = -EINVAL);
908                 }
909
910                 rec = buf;
911                 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
912                         lustre_swab_llog_rec(rec);
913
914                 tail = (struct llog_rec_tail *)((char *)buf + rc -
915                                                 sizeof(struct llog_rec_tail));
916                 /* get the last record in block */
917                 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
918                                                    tail->lrt_len);
919
920                 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
921                         lustre_swab_llog_rec(last_rec);
922
923                 if (last_rec->lrh_index != tail->lrt_index) {
924                         CERROR("%s: invalid llog tail at log id "DFID":%x "
925                                "offset %llu last_rec idx %u tail idx %u"
926                                "lrt len %u read_size %d\n",
927                                o->do_lu.lo_dev->ld_obd->obd_name,
928                                PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
929                                loghandle->lgh_id.lgl_ogen, *cur_offset,
930                                last_rec->lrh_index, tail->lrt_index,
931                                tail->lrt_len, rc);
932                         GOTO(out, rc = -EINVAL);
933                 }
934
935                 *cur_idx = tail->lrt_index;
936
937                 /* this shouldn't happen */
938                 if (tail->lrt_index == 0) {
939                         CERROR("%s: invalid llog tail at log id "DFID":%x "
940                                "offset %llu bytes %d\n",
941                                o->do_lu.lo_dev->ld_obd->obd_name,
942                                PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
943                                loghandle->lgh_id.lgl_ogen, *cur_offset, rc);
944                         GOTO(out, rc = -EINVAL);
945                 }
946                 if (tail->lrt_index < next_idx) {
947                         last_idx = *cur_idx;
948                         last_offset = *cur_offset;
949                         continue;
950                 }
951
952                 /* sanity check that the start of the new buffer is no farther
953                  * than the record that we wanted.  This shouldn't happen. */
954                 if (rec->lrh_index > next_idx) {
955                         if (!force_mini_rec && next_idx > last_idx)
956                                 goto retry;
957
958                         CERROR("%s: missed desired record? %u > %u\n",
959                                o->do_lu.lo_dev->ld_obd->obd_name,
960                                rec->lrh_index, next_idx);
961                         GOTO(out, rc = -ENOENT);
962                 }
963
964                 /* Trim unsupported extensions for compat w/ older clients */
965                 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_JOBID))
966                         changelog_block_trim_ext(rec, last_rec,
967                                                  CLF_VERSION | CLF_RENAME);
968
969                 GOTO(out, rc = 0);
970
971 retry:
972                 /* Note: because there are some pad records in the
973                  * llog, so llog_skip_over() might skip too much
974                  * records, let's try skip again with minimum record */
975                 force_mini_rec = true;
976                 *cur_offset = last_offset;
977                 *cur_idx = last_idx;
978         }
979         GOTO(out, rc = -EIO);
980 out:
981         return rc;
982 }
983
984 /**
985  * Implementation of the llog_operations::lop_prev_block
986  *
987  * This function finds the llog block to return which contains
988  * record with required index but in reverse order - from end of llog
989  * to the beginning.
990  * It is main part of reverse llog processing.
991  *
992  * \param[in] env       execution environment
993  * \param[in] loghandle llog handle of the current llog
994  * \param[in] prev_idx  target index to find
995  * \param[in] buf       pointer to data buffer to fill
996  * \param[in] len       required len to read, it is llog_chunk_size usually.
997  *
998  * \retval              0 on successful buffer read
999  * \retval              negative value on error
1000  */
1001 static int llog_osd_prev_block(const struct lu_env *env,
1002                                struct llog_handle *loghandle,
1003                                int prev_idx, void *buf, int len)
1004 {
1005         struct llog_thread_info *lgi = llog_info(env);
1006         struct dt_object        *o;
1007         struct dt_device        *dt;
1008         loff_t                   cur_offset;
1009         __u32                   chunk_size;
1010         int                      rc;
1011
1012         ENTRY;
1013
1014         chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
1015         if (len == 0 || len & (chunk_size - 1))
1016                 RETURN(-EINVAL);
1017
1018         CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
1019
1020         LASSERT(loghandle);
1021         LASSERT(loghandle->lgh_ctxt);
1022
1023         o = loghandle->lgh_obj;
1024         LASSERT(o);
1025         LASSERT(dt_object_exists(o));
1026         dt = lu2dt_dev(o->do_lu.lo_dev);
1027         LASSERT(dt);
1028
1029         /* Let's only use mini record size for previous block read
1030          * for now XXX */
1031         cur_offset = chunk_size;
1032         llog_skip_over(loghandle, &cur_offset, 0, prev_idx,
1033                        chunk_size, true);
1034
1035         rc = dt_attr_get(env, o, &lgi->lgi_attr);
1036         if (rc)
1037                 GOTO(out, rc);
1038
1039         while (cur_offset < lgi->lgi_attr.la_size) {
1040                 struct llog_rec_hdr     *rec, *last_rec;
1041                 struct llog_rec_tail    *tail;
1042
1043                 lgi->lgi_buf.lb_len = len;
1044                 lgi->lgi_buf.lb_buf = buf;
1045                 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
1046                 if (rc < 0) {
1047                         CERROR("%s: can't read llog block from log "DFID
1048                                " offset %llu: rc = %d\n",
1049                                o->do_lu.lo_dev->ld_obd->obd_name,
1050                                PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
1051                         GOTO(out, rc);
1052                 }
1053
1054                 if (rc == 0) /* end of file, nothing to do */
1055                         GOTO(out, rc);
1056
1057                 if (rc < sizeof(*tail)) {
1058                         CERROR("%s: invalid llog block at log id "DFID":%x "
1059                                "offset %llu\n",
1060                                o->do_lu.lo_dev->ld_obd->obd_name,
1061                                PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
1062                                loghandle->lgh_id.lgl_ogen, cur_offset);
1063                         GOTO(out, rc = -EINVAL);
1064                 }
1065
1066                 rec = buf;
1067                 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
1068                         lustre_swab_llog_rec(rec);
1069
1070                 tail = (struct llog_rec_tail *)((char *)buf + rc -
1071                                                 sizeof(struct llog_rec_tail));
1072                 /* get the last record in block */
1073                 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
1074                                                    le32_to_cpu(tail->lrt_len));
1075
1076                 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
1077                         lustre_swab_llog_rec(last_rec);
1078                 LASSERT(last_rec->lrh_index == tail->lrt_index);
1079
1080                 /* this shouldn't happen */
1081                 if (tail->lrt_index == 0) {
1082                         CERROR("%s: invalid llog tail at log id "DFID":%x "
1083                                "offset %llu\n",
1084                                o->do_lu.lo_dev->ld_obd->obd_name,
1085                                PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
1086                                loghandle->lgh_id.lgl_ogen, cur_offset);
1087                         GOTO(out, rc = -EINVAL);
1088                 }
1089                 if (tail->lrt_index < prev_idx)
1090                         continue;
1091
1092                 /* sanity check that the start of the new buffer is no farther
1093                  * than the record that we wanted.  This shouldn't happen. */
1094                 if (rec->lrh_index > prev_idx) {
1095                         CERROR("%s: missed desired record? %u > %u\n",
1096                                o->do_lu.lo_dev->ld_obd->obd_name,
1097                                rec->lrh_index, prev_idx);
1098                         GOTO(out, rc = -ENOENT);
1099                 }
1100
1101                 /* Trim unsupported extensions for compat w/ older clients */
1102                 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_JOBID))
1103                         changelog_block_trim_ext(rec, last_rec,
1104                                                  CLF_VERSION | CLF_RENAME);
1105
1106                 GOTO(out, rc = 0);
1107         }
1108         GOTO(out, rc = -EIO);
1109 out:
1110         return rc;
1111 }
1112
1113 /**
1114  * This is helper function to get llog directory object. It is used by named
1115  * llog operations to find/insert/delete llog entry from llog directory.
1116  *
1117  * \param[in] env       execution environment
1118  * \param[in] ctxt      llog context
1119  *
1120  * \retval              dt_object of llog directory
1121  * \retval              ERR_PTR of negative value on error
1122  */
1123 static struct dt_object *llog_osd_dir_get(const struct lu_env *env,
1124                                           struct llog_ctxt *ctxt)
1125 {
1126         struct dt_device        *dt;
1127         struct dt_thread_info   *dti = dt_info(env);
1128         struct dt_object        *dir;
1129         int                      rc;
1130
1131         dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1132         if (ctxt->loc_dir == NULL) {
1133                 rc = dt_root_get(env, dt, &dti->dti_fid);
1134                 if (rc)
1135                         return ERR_PTR(rc);
1136                 dir = dt_locate(env, dt, &dti->dti_fid);
1137
1138                 if (!IS_ERR(dir) && !dt_try_as_dir(env, dir)) {
1139                         dt_object_put(env, dir);
1140                         return ERR_PTR(-ENOTDIR);
1141                 }
1142         } else {
1143                 lu_object_get(&ctxt->loc_dir->do_lu);
1144                 dir = ctxt->loc_dir;
1145         }
1146
1147         return dir;
1148 }
1149
1150 /**
1151  * Implementation of the llog_operations::lop_open
1152  *
1153  * This function opens the llog by its logid or by name, it may open also
1154  * non existent llog and assing then new id to it.
1155  * The llog_open/llog_close pair works similar to lu_object_find/put,
1156  * the object may not exist prior open. The result of open is just dt_object
1157  * in the llog header.
1158  *
1159  * \param[in] env               execution environment
1160  * \param[in] handle            llog handle of the current llog
1161  * \param[in] logid             logid of llog to open (nameless llog)
1162  * \param[in] name              name of llog to open (named llog)
1163  * \param[in] open_param
1164  *                              LLOG_OPEN_NEW - new llog, may not exist
1165  *                              LLOG_OPEN_EXIST - old llog, must exist
1166  *
1167  * \retval                      0 on successful open, llog_handle::lgh_obj
1168  *                              contains the dt_object of the llog.
1169  * \retval                      negative value on error
1170  */
1171 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
1172                          struct llog_logid *logid, char *name,
1173                          enum llog_open_param open_param)
1174 {
1175         struct llog_thread_info         *lgi = llog_info(env);
1176         struct llog_ctxt                *ctxt = handle->lgh_ctxt;
1177         struct dt_object                *o;
1178         struct dt_device                *dt;
1179         struct ls_device                *ls;
1180         struct local_oid_storage        *los = NULL;
1181         int                              rc = 0;
1182         bool new_id = false;
1183
1184         ENTRY;
1185
1186         LASSERT(env);
1187         LASSERT(ctxt);
1188         LASSERT(ctxt->loc_exp);
1189         LASSERT(ctxt->loc_exp->exp_obd);
1190         dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1191         LASSERT(dt);
1192         if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1193                 struct lu_object_conf conf = { 0 };
1194                 if (logid != NULL) {
1195                         logid_to_fid(logid, &lgi->lgi_fid);
1196                 } else {
1197                         /* If logid == NULL, then it means the caller needs
1198                          * to allocate new FID (llog_cat_declare_add_rec()). */
1199                         rc = obd_fid_alloc(env, ctxt->loc_exp,
1200                                            &lgi->lgi_fid, NULL);
1201                         if (rc < 0)
1202                                 RETURN(rc);
1203                         rc = 0;
1204                         conf.loc_flags = LOC_F_NEW;
1205                 }
1206
1207                 o = dt_locate_at(env, dt, &lgi->lgi_fid,
1208                                  dt->dd_lu_dev.ld_site->ls_top_dev, &conf);
1209                 if (IS_ERR(o))
1210                         RETURN(PTR_ERR(o));
1211
1212                 goto after_open;
1213         }
1214
1215         ls = ls_device_get(dt);
1216         if (IS_ERR(ls))
1217                 RETURN(PTR_ERR(ls));
1218
1219         mutex_lock(&ls->ls_los_mutex);
1220         los = dt_los_find(ls, name != NULL ? FID_SEQ_LLOG_NAME : FID_SEQ_LLOG);
1221         mutex_unlock(&ls->ls_los_mutex);
1222         LASSERT(los);
1223         ls_device_put(env, ls);
1224
1225         LASSERT(handle);
1226
1227         if (logid != NULL) {
1228                 logid_to_fid(logid, &lgi->lgi_fid);
1229         } else if (name) {
1230                 struct dt_object *llog_dir;
1231
1232                 llog_dir = llog_osd_dir_get(env, ctxt);
1233                 if (IS_ERR(llog_dir))
1234                         GOTO(out, rc = PTR_ERR(llog_dir));
1235                 dt_read_lock(env, llog_dir, 0);
1236                 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
1237                 dt_read_unlock(env, llog_dir);
1238                 dt_object_put(env, llog_dir);
1239                 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
1240                         /* generate fid for new llog */
1241                         rc = local_object_fid_generate(env, los,
1242                                                        &lgi->lgi_fid);
1243                         new_id = true;
1244                 }
1245                 if (rc < 0)
1246                         GOTO(out, rc);
1247                 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
1248                 if (handle->lgh_name)
1249                         strcpy(handle->lgh_name, name);
1250                 else
1251                         GOTO(out, rc = -ENOMEM);
1252         } else {
1253                 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
1254                 /* generate fid for new llog */
1255 generate:
1256                 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
1257                 if (rc < 0)
1258                         GOTO(out, rc);
1259                 new_id = true;
1260         }
1261
1262         o = ls_locate(env, ls, &lgi->lgi_fid, NULL);
1263         if (IS_ERR(o))
1264                 GOTO(out_name, rc = PTR_ERR(o));
1265
1266         if (dt_object_exists(o) && new_id) {
1267                 /* llog exists with just generated ID, e.g. some old llog file
1268                  * still is in use or is orphan, drop a warn and skip it. */
1269                 CDEBUG(D_INFO, "%s: llog exists with the same FID: "DFID
1270                        ", skipping\n",
1271                        o->do_lu.lo_dev->ld_obd->obd_name,
1272                        PFID(lu_object_fid(&o->do_lu)));
1273                 dt_object_put(env, o);
1274                 /* just skip this llog ID, we shouldn't delete it because we
1275                  * don't know exactly what is its purpose and state. */
1276                 goto generate;
1277         }
1278
1279 after_open:
1280         /* No new llog is expected but doesn't exist */
1281         if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o)) {
1282                 CDEBUG(D_INFO, "%s: llog FID: "DFID" obj %p doesn`t exist\n",
1283                        o->do_lu.lo_dev->ld_obd->obd_name,
1284                        PFID(lu_object_fid(&o->do_lu)), o);
1285                 GOTO(out_put, rc = -ENOENT);
1286         }
1287         fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
1288         handle->lgh_obj = o;
1289         handle->private_data = los;
1290         LASSERT(handle->lgh_ctxt);
1291
1292         RETURN(rc);
1293
1294 out_put:
1295         dt_object_put(env, o);
1296 out_name:
1297         if (handle->lgh_name != NULL)
1298                 OBD_FREE(handle->lgh_name, strlen(name) + 1);
1299 out:
1300         if (los != NULL)
1301                 dt_los_put(los);
1302         RETURN(rc);
1303 }
1304
1305 /**
1306  * Get dir for regular fid log object
1307  *
1308  * Get directory for regular fid log object, and these regular fid log
1309  * object will be inserted under this directory, to satisfy the FS
1310  * consistency check, e2fsck etc.
1311  *
1312  * \param [in] env      execution environment
1313  * \param [in] dto      llog object
1314  *
1315  * \retval              pointer to the directory if it is found.
1316  * \retval              ERR_PTR(negative errno) if it fails.
1317  */
1318 struct dt_object *llog_osd_get_regular_fid_dir(const struct lu_env *env,
1319                                                struct dt_object *dto)
1320 {
1321         struct llog_thread_info *lgi = llog_info(env);
1322         struct seq_server_site *ss = dto->do_lu.lo_dev->ld_site->ld_seq_site;
1323         struct lu_seq_range     *range = &lgi->lgi_range;
1324         struct lu_fid           *dir_fid = &lgi->lgi_fid;
1325         struct dt_object        *dir;
1326         int                     rc;
1327         ENTRY;
1328
1329         fld_range_set_any(range);
1330         LASSERT(ss != NULL);
1331         rc = ss->ss_server_fld->lsf_seq_lookup(env, ss->ss_server_fld,
1332                                    fid_seq(lu_object_fid(&dto->do_lu)), range);
1333         if (rc < 0)
1334                 RETURN(ERR_PTR(rc));
1335
1336         lu_update_log_dir_fid(dir_fid, range->lsr_index);
1337         dir = dt_locate(env, lu2dt_dev(dto->do_lu.lo_dev), dir_fid);
1338         if (IS_ERR(dir))
1339                 RETURN(dir);
1340
1341         if (!dt_try_as_dir(env, dir)) {
1342                 dt_object_put(env, dir);
1343                 RETURN(ERR_PTR(-ENOTDIR));
1344         }
1345
1346         RETURN(dir);
1347 }
1348
1349 /**
1350  * Add llog object with regular FID to name entry
1351  *
1352  * Add llog object with regular FID to name space, and each llog
1353  * object on each MDT will be /update_log_dir/[seq:oid:ver],
1354  * so to satisfy the namespace consistency check, e2fsck etc.
1355  *
1356  * \param [in] env      execution environment
1357  * \param [in] dto      llog object
1358  * \param [in] th       thandle
1359  * \param [in] declare  if it is declare or execution
1360  *
1361  * \retval              0 if insertion succeeds.
1362  * \retval              negative errno if insertion fails.
1363  */
1364 static int
1365 llog_osd_regular_fid_add_name_entry(const struct lu_env *env,
1366                                     struct dt_object *dto,
1367                                     struct thandle *th, bool declare)
1368 {
1369         struct llog_thread_info *lgi = llog_info(env);
1370         const struct lu_fid     *fid = lu_object_fid(&dto->do_lu);
1371         struct dt_insert_rec    *rec = &lgi->lgi_dt_rec;
1372         struct dt_object        *dir;
1373         char                    *name = lgi->lgi_name;
1374         int                     rc;
1375         ENTRY;
1376
1377         if (!fid_is_norm(fid))
1378                 RETURN(0);
1379
1380         dir = llog_osd_get_regular_fid_dir(env, dto);
1381         if (IS_ERR(dir))
1382                 RETURN(PTR_ERR(dir));
1383
1384         rec->rec_fid = fid;
1385         rec->rec_type = S_IFREG;
1386         snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1387         dt_write_lock(env, dir, 0);
1388         if (declare) {
1389                 rc = dt_declare_insert(env, dir, (struct dt_rec *)rec,
1390                                (struct dt_key *)name, th);
1391         } else {
1392                 rc = dt_insert(env, dir, (struct dt_rec *)rec,
1393                                (struct dt_key *)name, th, 1);
1394         }
1395         dt_write_unlock(env, dir);
1396
1397         dt_object_put(env, dir);
1398         RETURN(rc);
1399 }
1400
1401
1402 /**
1403  * Implementation of the llog_operations::lop_declare_create
1404  *
1405  * This function declares the llog create. It declares also name insert
1406  * into llog directory in case of named llog.
1407  *
1408  * \param[in] env       execution environment
1409  * \param[in] res       llog handle of the current llog
1410  * \param[in] th        current transaction handle
1411  *
1412  * \retval              0 on successful create declaration
1413  * \retval              negative value on error
1414  */
1415 static int llog_osd_declare_create(const struct lu_env *env,
1416                                    struct llog_handle *res, struct thandle *th)
1417 {
1418         struct llog_thread_info         *lgi = llog_info(env);
1419         struct dt_insert_rec            *rec = &lgi->lgi_dt_rec;
1420         struct local_oid_storage        *los;
1421         struct dt_object                *o;
1422         int                              rc;
1423
1424         ENTRY;
1425
1426         LASSERT(res->lgh_obj);
1427         LASSERT(th);
1428
1429         /* object can be created by another thread */
1430         o = res->lgh_obj;
1431         if (dt_object_exists(o))
1432                 RETURN(0);
1433
1434         if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1435                 struct llog_thread_info *lgi = llog_info(env);
1436
1437                 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE;
1438                 lgi->lgi_attr.la_size = 0;
1439                 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1440                 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1441
1442                 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1443                                        &lgi->lgi_dof, th);
1444                 if (rc < 0)
1445                         RETURN(rc);
1446
1447
1448                 rc = llog_osd_regular_fid_add_name_entry(env, o, th, true);
1449
1450                 RETURN(rc);
1451         }
1452         los = res->private_data;
1453         LASSERT(los);
1454
1455         rc = llog_osd_declare_new_object(env, los, o, th);
1456         if (rc)
1457                 RETURN(rc);
1458
1459         /* do not declare header initialization here as it's declared
1460          * in llog_osd_declare_write_rec() which is always called */
1461
1462         if (res->lgh_name) {
1463                 struct dt_object *llog_dir;
1464
1465                 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1466                 if (IS_ERR(llog_dir))
1467                         RETURN(PTR_ERR(llog_dir));
1468                 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1469                 rec->rec_fid = &lgi->lgi_fid;
1470                 rec->rec_type = S_IFREG;
1471                 rc = dt_declare_insert(env, llog_dir,
1472                                        (struct dt_rec *)rec,
1473                                        (struct dt_key *)res->lgh_name, th);
1474                 dt_object_put(env, llog_dir);
1475                 if (rc)
1476                         CERROR("%s: can't declare named llog %s: rc = %d\n",
1477                                o->do_lu.lo_dev->ld_obd->obd_name,
1478                                res->lgh_name, rc);
1479         }
1480         RETURN(rc);
1481 }
1482
1483 /**
1484  * Implementation of the llog_operations::lop_create
1485  *
1486  * This function creates the llog according with llog_handle::lgh_obj
1487  * and llog_handle::lgh_name.
1488  *
1489  * \param[in] env       execution environment
1490  * \param[in] res       llog handle of the current llog
1491  * \param[in] th        current transaction handle
1492  *
1493  * \retval              0 on successful create
1494  * \retval              negative value on error
1495  */
1496 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
1497                            struct thandle *th)
1498 {
1499         struct llog_thread_info *lgi = llog_info(env);
1500         struct dt_insert_rec    *rec = &lgi->lgi_dt_rec;
1501         struct local_oid_storage *los;
1502         struct dt_object        *o;
1503         int                      rc = 0;
1504
1505         ENTRY;
1506
1507         LASSERT(env);
1508         o = res->lgh_obj;
1509         LASSERT(o);
1510
1511         /* llog can be already created */
1512         if (dt_object_exists(o))
1513                 RETURN(-EEXIST);
1514
1515         if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1516                 struct llog_thread_info *lgi = llog_info(env);
1517
1518                 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE | LA_TYPE;
1519                 lgi->lgi_attr.la_size = 0;
1520                 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1521                 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1522
1523                 dt_write_lock(env, o, 0);
1524                 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1525                                &lgi->lgi_dof, th);
1526                 dt_write_unlock(env, o);
1527                 if (rc < 0)
1528                         RETURN(rc);
1529
1530                 rc = llog_osd_regular_fid_add_name_entry(env, o, th, false);
1531
1532                 RETURN(rc);
1533         }
1534
1535         los = res->private_data;
1536         LASSERT(los);
1537
1538         dt_write_lock(env, o, 0);
1539         if (!dt_object_exists(o))
1540                 rc = llog_osd_create_new_object(env, los, o, th);
1541         else
1542                 rc = -EEXIST;
1543
1544         dt_write_unlock(env, o);
1545         if (rc)
1546                 RETURN(rc);
1547
1548         if (res->lgh_name) {
1549                 struct dt_object *llog_dir;
1550
1551                 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1552                 if (IS_ERR(llog_dir))
1553                         RETURN(PTR_ERR(llog_dir));
1554
1555                 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1556                 rec->rec_fid = &lgi->lgi_fid;
1557                 rec->rec_type = S_IFREG;
1558                 dt_read_lock(env, llog_dir, 0);
1559                 rc = dt_insert(env, llog_dir, (struct dt_rec *)rec,
1560                                (struct dt_key *)res->lgh_name,
1561                                th, 1);
1562                 dt_read_unlock(env, llog_dir);
1563                 dt_object_put(env, llog_dir);
1564                 if (rc)
1565                         CERROR("%s: can't create named llog %s: rc = %d\n",
1566                                o->do_lu.lo_dev->ld_obd->obd_name,
1567                                res->lgh_name, rc);
1568         }
1569         RETURN(rc);
1570 }
1571
1572 /**
1573  * Implementation of the llog_operations::lop_close
1574  *
1575  * This function closes the llog. It just put llog object and referenced
1576  * local storage.
1577  *
1578  * \param[in] env       execution environment
1579  * \param[in] handle    llog handle of the current llog
1580  *
1581  * \retval              0 on successful llog close
1582  * \retval              negative value on error
1583  */
1584 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
1585 {
1586         struct local_oid_storage        *los;
1587         int                              rc = 0;
1588
1589         ENTRY;
1590
1591         LASSERT(handle->lgh_obj);
1592
1593         if (handle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1594                 /* Remove the object from the cache, otherwise it may
1595                  * hold LOD being released during cleanup process */
1596                 dt_object_put_nocache(env, handle->lgh_obj);
1597                 LASSERT(handle->private_data == NULL);
1598                 RETURN(rc);
1599         } else {
1600                 dt_object_put(env, handle->lgh_obj);
1601         }
1602         los = handle->private_data;
1603         LASSERT(los);
1604         dt_los_put(los);
1605
1606         if (handle->lgh_name)
1607                 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
1608
1609         RETURN(rc);
1610 }
1611
1612 /**
1613  * delete llog object name entry
1614  *
1615  * Delete llog object (with regular FID) from name space (under
1616  * update_log_dir).
1617  *
1618  * \param [in] env      execution environment
1619  * \param [in] dto      llog object
1620  * \param [in] th       thandle
1621  * \param [in] declare  if it is declare or execution
1622  *
1623  * \retval              0 if deletion succeeds.
1624  * \retval              negative errno if deletion fails.
1625  */
1626 static int
1627 llog_osd_regular_fid_del_name_entry(const struct lu_env *env,
1628                                     struct dt_object *dto,
1629                                     struct thandle *th, bool declare)
1630 {
1631         struct llog_thread_info *lgi = llog_info(env);
1632         const struct lu_fid     *fid = lu_object_fid(&dto->do_lu);
1633         struct dt_object        *dir;
1634         char                    *name = lgi->lgi_name;
1635         int                     rc;
1636         ENTRY;
1637
1638         if (!fid_is_norm(fid))
1639                 RETURN(0);
1640
1641         dir = llog_osd_get_regular_fid_dir(env, dto);
1642         if (IS_ERR(dir))
1643                 RETURN(PTR_ERR(dir));
1644
1645         snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1646         dt_write_lock(env, dir, 0);
1647         if (declare) {
1648                 rc = dt_declare_delete(env, dir, (struct dt_key *)name,
1649                                        th);
1650         } else {
1651                 rc = dt_delete(env, dir, (struct dt_key *)name, th);
1652         }
1653         dt_write_unlock(env, dir);
1654
1655         dt_object_put(env, dir);
1656         RETURN(rc);
1657 }
1658
1659 /**
1660  * Implementation of the llog_operations::lop_declare_destroy
1661  *
1662  * This function declare destroys the llog and deletes also entry in the
1663  * llog directory in case of named llog. Llog should be opened prior that.
1664  *
1665  * \param[in] env               execution environment
1666  * \param[in] loghandle llog handle of the current llog
1667  *
1668  * \retval              0 on successful destroy
1669  * \retval              negative value on error
1670  */
1671 static int llog_osd_declare_destroy(const struct lu_env *env,
1672                                     struct llog_handle *loghandle,
1673                                     struct thandle *th)
1674 {
1675         struct llog_ctxt        *ctxt;
1676         struct dt_object        *o, *llog_dir = NULL;
1677         int                      rc;
1678
1679         ENTRY;
1680
1681         ctxt = loghandle->lgh_ctxt;
1682         LASSERT(ctxt);
1683
1684         o = loghandle->lgh_obj;
1685         LASSERT(o);
1686
1687         if (loghandle->lgh_name) {
1688                 llog_dir = llog_osd_dir_get(env, ctxt);
1689                 if (IS_ERR(llog_dir))
1690                         RETURN(PTR_ERR(llog_dir));
1691
1692                 rc = dt_declare_delete(env, llog_dir,
1693                                        (struct dt_key *)loghandle->lgh_name,
1694                                        th);
1695                 if (rc < 0)
1696                         GOTO(out_put, rc);
1697         }
1698
1699         rc = dt_declare_ref_del(env, o, th);
1700         if (rc < 0)
1701                 GOTO(out_put, rc);
1702
1703         rc = dt_declare_destroy(env, o, th);
1704         if (rc < 0)
1705                 GOTO(out_put, rc);
1706
1707         if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1708                 rc = llog_osd_regular_fid_del_name_entry(env, o, th, true);
1709                 if (rc < 0)
1710                         GOTO(out_put, rc);
1711         }
1712
1713 out_put:
1714         if (!(IS_ERR_OR_NULL(llog_dir)))
1715                 dt_object_put(env, llog_dir);
1716
1717         RETURN(rc);
1718 }
1719
1720
1721 /**
1722  * Implementation of the llog_operations::lop_destroy
1723  *
1724  * This function destroys the llog and deletes also entry in the
1725  * llog directory in case of named llog. Llog should be opened prior that.
1726  * Destroy method is not part of external transaction and does everything
1727  * inside.
1728  *
1729  * \param[in] env               execution environment
1730  * \param[in] loghandle llog handle of the current llog
1731  *
1732  * \retval              0 on successful destroy
1733  * \retval              negative value on error
1734  */
1735 static int llog_osd_destroy(const struct lu_env *env,
1736                             struct llog_handle *loghandle, struct thandle *th)
1737 {
1738         struct llog_ctxt        *ctxt;
1739         struct dt_object        *o, *llog_dir = NULL;
1740         int                      rc;
1741
1742         ENTRY;
1743
1744         ctxt = loghandle->lgh_ctxt;
1745         LASSERT(ctxt != NULL);
1746
1747         o = loghandle->lgh_obj;
1748         LASSERT(o != NULL);
1749
1750         dt_write_lock(env, o, 0);
1751         if (!dt_object_exists(o))
1752                 GOTO(out_unlock, rc = 0);
1753
1754         if (loghandle->lgh_name) {
1755                 llog_dir = llog_osd_dir_get(env, ctxt);
1756                 if (IS_ERR(llog_dir))
1757                         GOTO(out_unlock, rc = PTR_ERR(llog_dir));
1758
1759                 dt_read_lock(env, llog_dir, 0);
1760                 rc = dt_delete(env, llog_dir,
1761                                (struct dt_key *)loghandle->lgh_name,
1762                                th);
1763                 dt_read_unlock(env, llog_dir);
1764                 if (rc) {
1765                         CERROR("%s: can't remove llog %s: rc = %d\n",
1766                                o->do_lu.lo_dev->ld_obd->obd_name,
1767                                loghandle->lgh_name, rc);
1768                         GOTO(out_unlock, rc);
1769                 }
1770         }
1771
1772         dt_ref_del(env, o, th);
1773         rc = dt_destroy(env, o, th);
1774         if (rc < 0)
1775                 GOTO(out_unlock, rc);
1776
1777         if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1778                 rc = llog_osd_regular_fid_del_name_entry(env, o, th, false);
1779                 if (rc < 0)
1780                         GOTO(out_unlock, rc);
1781         }
1782
1783 out_unlock:
1784         dt_write_unlock(env, o);
1785         if (!(IS_ERR_OR_NULL(llog_dir)))
1786                 dt_object_put(env, llog_dir);
1787         RETURN(rc);
1788 }
1789
1790 /**
1791  * Implementation of the llog_operations::lop_setup
1792  *
1793  * This function setup the llog on local storage.
1794  *
1795  * \param[in] env       execution environment
1796  * \param[in] obd       obd device the llog belongs to
1797  * \param[in] olg       the llog group, it is always zero group now.
1798  * \param[in] ctxt_idx  the llog index, it defines the purpose of this llog.
1799  *                      Every new llog type have to use own index.
1800  * \param[in] disk_obd  the storage obd, where llog is stored.
1801  *
1802  * \retval              0 on successful llog setup
1803  * \retval              negative value on error
1804  */
1805 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1806                           struct obd_llog_group *olg, int ctxt_idx,
1807                           struct obd_device *disk_obd)
1808 {
1809         struct llog_thread_info         *lgi = llog_info(env);
1810         struct llog_ctxt                *ctxt;
1811         int                              rc = 0;
1812         ENTRY;
1813
1814         LASSERT(obd);
1815         LASSERT(olg->olg_ctxts[ctxt_idx]);
1816
1817         ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1818         LASSERT(ctxt);
1819
1820         if (disk_obd == NULL)
1821                 GOTO(out, rc = 0);
1822
1823         /* initialize data allowing to generate new fids,
1824          * literally we need a sequece */
1825         lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1826         lgi->lgi_fid.f_oid = 1;
1827         lgi->lgi_fid.f_ver = 0;
1828         rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1829                                     &lgi->lgi_fid,
1830                                     &ctxt->loc_los_nameless);
1831         if (rc != 0)
1832                 GOTO(out, rc);
1833
1834         lgi->lgi_fid.f_seq = FID_SEQ_LLOG_NAME;
1835         lgi->lgi_fid.f_oid = 1;
1836         lgi->lgi_fid.f_ver = 0;
1837         rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1838                                     &lgi->lgi_fid,
1839                                     &ctxt->loc_los_named);
1840         if (rc != 0) {
1841                 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1842                 ctxt->loc_los_nameless = NULL;
1843         }
1844
1845         GOTO(out, rc);
1846
1847 out:
1848         llog_ctxt_put(ctxt);
1849         return rc;
1850 }
1851
1852 /**
1853  * Implementation of the llog_operations::lop_cleanup
1854  *
1855  * This function cleanups the llog on local storage.
1856  *
1857  * \param[in] env       execution environment
1858  * \param[in] ctxt      the llog context
1859  *
1860  * \retval              0
1861  */
1862 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1863 {
1864         if (ctxt->loc_los_nameless != NULL) {
1865                 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1866                 ctxt->loc_los_nameless = NULL;
1867         }
1868
1869         if (ctxt->loc_los_named != NULL) {
1870                 local_oid_storage_fini(env, ctxt->loc_los_named);
1871                 ctxt->loc_los_named = NULL;
1872         }
1873
1874         return 0;
1875 }
1876
1877 struct llog_operations llog_osd_ops = {
1878         .lop_next_block         = llog_osd_next_block,
1879         .lop_prev_block         = llog_osd_prev_block,
1880         .lop_read_header        = llog_osd_read_header,
1881         .lop_declare_destroy    = llog_osd_declare_destroy,
1882         .lop_destroy            = llog_osd_destroy,
1883         .lop_setup              = llog_osd_setup,
1884         .lop_cleanup            = llog_osd_cleanup,
1885         .lop_open               = llog_osd_open,
1886         .lop_exist              = llog_osd_exist,
1887         .lop_declare_create     = llog_osd_declare_create,
1888         .lop_create             = llog_osd_create,
1889         .lop_declare_write_rec  = llog_osd_declare_write_rec,
1890         .lop_write_rec          = llog_osd_write_rec,
1891         .lop_close              = llog_osd_close,
1892 };
1893 EXPORT_SYMBOL(llog_osd_ops);
1894
1895 struct llog_operations llog_common_cat_ops = {
1896         .lop_next_block         = llog_osd_next_block,
1897         .lop_prev_block         = llog_osd_prev_block,
1898         .lop_read_header        = llog_osd_read_header,
1899         .lop_declare_destroy    = llog_osd_declare_destroy,
1900         .lop_destroy            = llog_osd_destroy,
1901         .lop_setup              = llog_osd_setup,
1902         .lop_cleanup            = llog_osd_cleanup,
1903         .lop_open               = llog_osd_open,
1904         .lop_exist              = llog_osd_exist,
1905         .lop_declare_create     = llog_osd_declare_create,
1906         .lop_create             = llog_osd_create,
1907         .lop_declare_write_rec  = llog_osd_declare_write_rec,
1908         .lop_write_rec          = llog_osd_write_rec,
1909         .lop_close              = llog_osd_close,
1910         .lop_add                = llog_cat_add_rec,
1911         .lop_declare_add        = llog_cat_declare_add_rec,
1912 };
1913 EXPORT_SYMBOL(llog_common_cat_ops);
1914
1915 /**
1916  * Read the special file which contains the list of llog catalogs IDs
1917  *
1918  * This function reads the CATALOGS file which contains the array of llog
1919  * catalogs IDs. The main purpose of this file is to store OSP llogs indexed
1920  * by OST/MDT number.
1921  *
1922  * \param[in]  env              execution environment
1923  * \param[in]  d                corresponding storage device
1924  * \param[in]  idx              position to start from, usually OST/MDT index
1925  * \param[in]  count            how many catalog IDs to read
1926  * \param[out] idarray          the buffer for the data. If it is NULL then
1927  *                              function returns just number of catalog IDs
1928  *                              in the file.
1929  * \param[in]  fid              LLOG_CATALOGS_OID for CATALOG object
1930  *
1931  * \retval                      0 on successful read of catalog IDs
1932  * \retval                      negative value on error
1933  * \retval                      positive value which is number of records in
1934  *                              the file if \a idarray is NULL
1935  */
1936 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
1937                           int idx, int count, struct llog_catid *idarray,
1938                           const struct lu_fid *fid)
1939 {
1940         struct llog_thread_info *lgi = llog_info(env);
1941         struct dt_object        *o = NULL;
1942         struct thandle          *th;
1943         int                      rc, size;
1944
1945         ENTRY;
1946
1947         LASSERT(d);
1948
1949         size = sizeof(*idarray) * count;
1950         lgi->lgi_off = idx *  sizeof(*idarray);
1951
1952         lgi->lgi_fid = *fid;
1953         o = dt_locate(env, d, &lgi->lgi_fid);
1954         if (IS_ERR(o))
1955                 RETURN(PTR_ERR(o));
1956
1957         if (!dt_object_exists(o)) {
1958                 th = dt_trans_create(env, d);
1959                 if (IS_ERR(th))
1960                         GOTO(out, rc = PTR_ERR(th));
1961
1962                 lgi->lgi_attr.la_valid = LA_MODE;
1963                 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1964                 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1965
1966                 th->th_wait_submit = 1;
1967                 /* Make the llog object creation synchronization, so
1968                  * it will be reliable to the reference, especially
1969                  * for remote reference */
1970                 th->th_sync = 1;
1971
1972                 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1973                                        &lgi->lgi_dof, th);
1974                 if (rc)
1975                         GOTO(out_trans, rc);
1976
1977                 rc = dt_trans_start_local(env, d, th);
1978                 if (rc)
1979                         GOTO(out_trans, rc);
1980
1981                 dt_write_lock(env, o, 0);
1982                 if (!dt_object_exists(o))
1983                         rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1984                                        &lgi->lgi_dof, th);
1985                 dt_write_unlock(env, o);
1986 out_trans:
1987                 dt_trans_stop(env, d, th);
1988                 if (rc)
1989                         GOTO(out, rc);
1990         }
1991
1992         rc = dt_attr_get(env, o, &lgi->lgi_attr);
1993         if (rc)
1994                 GOTO(out, rc);
1995
1996         if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1997                 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1998                        o->do_lu.lo_dev->ld_obd->obd_name,
1999                        lgi->lgi_attr.la_mode);
2000                 GOTO(out, rc = -ENOENT);
2001         }
2002
2003         CDEBUG(D_CONFIG, "cat list: disk size=%d, read=%d\n",
2004                (int)lgi->lgi_attr.la_size, size);
2005
2006         /* return just number of llogs */
2007         if (idarray == NULL) {
2008                 rc = lgi->lgi_attr.la_size / sizeof(*idarray);
2009                 GOTO(out, rc);
2010         }
2011
2012         /* read for new ost index or for empty file */
2013         memset(idarray, 0, size);
2014         if (lgi->lgi_attr.la_size <= lgi->lgi_off)
2015                 GOTO(out, rc = 0);
2016         if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
2017                 size = lgi->lgi_attr.la_size - lgi->lgi_off;
2018
2019         lgi->lgi_buf.lb_buf = idarray;
2020         lgi->lgi_buf.lb_len = size;
2021         rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
2022         /* -EFAULT means the llog is a sparse file. This is not an error
2023          * after arbitrary OST index is supported. */
2024         if (rc < 0 && rc != -EFAULT) {
2025                 CERROR("%s: error reading CATALOGS: rc = %d\n",
2026                        o->do_lu.lo_dev->ld_obd->obd_name,  rc);
2027                 GOTO(out, rc);
2028         }
2029
2030         EXIT;
2031 out:
2032         dt_object_put(env, o);
2033         RETURN(rc);
2034 }
2035 EXPORT_SYMBOL(llog_osd_get_cat_list);
2036
2037 /**
2038  * Write the special file which contains the list of llog catalogs IDs
2039  *
2040  * This function writes the CATALOG file which contains the array of llog
2041  * catalogs IDs. It is used mostly to store OSP llogs indexed by OST/MDT
2042  * number.
2043  *
2044  * \param[in]  env      execution environment
2045  * \param[in]  d        corresponding storage device
2046  * \param[in]  idx      position to start from, usually OST/MDT index
2047  * \param[in]  count    how many catalog IDs to write
2048  * \param[out] idarray  the buffer with the data to write.
2049  * \param[in]  fid      LLOG_CATALOGS_OID for CATALOG object
2050  *
2051  * \retval              0 on successful write of catalog IDs
2052  * \retval              negative value on error
2053  */
2054 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
2055                           int idx, int count, struct llog_catid *idarray,
2056                           const struct lu_fid *fid)
2057 {
2058         struct llog_thread_info *lgi = llog_info(env);
2059         struct dt_object        *o = NULL;
2060         struct thandle          *th;
2061         int                      rc, size;
2062
2063         if (count == 0)
2064                 RETURN(0);
2065
2066         LASSERT(d);
2067
2068         size = sizeof(*idarray) * count;
2069         lgi->lgi_off = idx * sizeof(*idarray);
2070         lgi->lgi_fid = *fid;
2071
2072         o = dt_locate(env, d, &lgi->lgi_fid);
2073         if (IS_ERR(o))
2074                 RETURN(PTR_ERR(o));
2075
2076         if (!dt_object_exists(o))
2077                 GOTO(out, rc = -ENOENT);
2078
2079         rc = dt_attr_get(env, o, &lgi->lgi_attr);
2080         if (rc)
2081                 GOTO(out, rc);
2082
2083         if (!S_ISREG(lgi->lgi_attr.la_mode)) {
2084                 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
2085                        o->do_lu.lo_dev->ld_obd->obd_name,
2086                        lgi->lgi_attr.la_mode);
2087                 GOTO(out, rc = -ENOENT);
2088         }
2089
2090         th = dt_trans_create(env, d);
2091         if (IS_ERR(th))
2092                 GOTO(out, rc = PTR_ERR(th));
2093
2094         lgi->lgi_buf.lb_len = size;
2095         lgi->lgi_buf.lb_buf = idarray;
2096         rc = dt_declare_record_write(env, o, &lgi->lgi_buf, lgi->lgi_off, th);
2097         if (rc)
2098                 GOTO(out_trans, rc);
2099
2100         /* For update log, this happens during initialization,
2101          * see lod_sub_prep_llog(), and we need make sure catlog
2102          * file ID is written to catlist file(committed) before
2103          * cross-MDT operation write update records to catlog FILE,
2104          * otherwise, during failover these update records might
2105          * missing */
2106         if (fid_is_update_log(fid))
2107                 th->th_sync = 1;
2108
2109         rc = dt_trans_start_local(env, d, th);
2110         if (rc)
2111                 GOTO(out_trans, rc);
2112
2113         th->th_wait_submit = 1;
2114
2115         rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
2116         if (rc)
2117                 CDEBUG(D_INODE, "can't write CATALOGS at index %d: rc = %d\n",
2118                        idx, rc);
2119 out_trans:
2120         dt_trans_stop(env, d, th);
2121 out:
2122         dt_object_put(env, o);
2123         RETURN(rc);
2124 }
2125 EXPORT_SYMBOL(llog_osd_put_cat_list);