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