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