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