Whamcloud - gitweb
f08fd6ef58046bd7d18fc8a85e7237c4699360ff
[fs/lustre-release.git] / lustre / obdclass / llog.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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdclass/llog.c
37  *
38  * OST<->MDS recovery logging infrastructure.
39  * Invariants in implementation:
40  * - we do not share logs among different OST<->MDS connections, so that
41  *   if an OST or MDS fails it need only look at log(s) relevant to itself
42  *
43  * Author: Andreas Dilger <adilger@clusterfs.com>
44  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
45  * Author: Mikhail Pershin <tappro@whamcloud.com>
46  */
47
48 #define DEBUG_SUBSYSTEM S_LOG
49
50 #ifndef __KERNEL__
51 #include <liblustre.h>
52 #endif
53
54 #include <obd_class.h>
55 #include <lustre_log.h>
56 #include "llog_internal.h"
57
58 /*
59  * Allocate a new log or catalog handle
60  * Used inside llog_open().
61  */
62 struct llog_handle *llog_alloc_handle(void)
63 {
64         struct llog_handle *loghandle;
65
66         OBD_ALLOC_PTR(loghandle);
67         if (loghandle == NULL)
68                 return ERR_PTR(-ENOMEM);
69
70         init_rwsem(&loghandle->lgh_lock);
71         spin_lock_init(&loghandle->lgh_hdr_lock);
72         CFS_INIT_LIST_HEAD(&loghandle->u.phd.phd_entry);
73         cfs_atomic_set(&loghandle->lgh_refcount, 1);
74
75         return loghandle;
76 }
77
78 /*
79  * Free llog handle and header data if exists. Used in llog_close() only
80  */
81 void llog_free_handle(struct llog_handle *loghandle)
82 {
83         LASSERT(loghandle != NULL);
84
85         /* failed llog_init_handle */
86         if (!loghandle->lgh_hdr)
87                 goto out;
88
89         if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)
90                 LASSERT(cfs_list_empty(&loghandle->u.phd.phd_entry));
91         else if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
92                 LASSERT(cfs_list_empty(&loghandle->u.chd.chd_head));
93         LASSERT(sizeof(*(loghandle->lgh_hdr)) == LLOG_CHUNK_SIZE);
94         OBD_FREE(loghandle->lgh_hdr, LLOG_CHUNK_SIZE);
95 out:
96         OBD_FREE_PTR(loghandle);
97 }
98
99 void llog_handle_get(struct llog_handle *loghandle)
100 {
101         cfs_atomic_inc(&loghandle->lgh_refcount);
102 }
103
104 void llog_handle_put(struct llog_handle *loghandle)
105 {
106         LASSERT(cfs_atomic_read(&loghandle->lgh_refcount) > 0);
107         if (cfs_atomic_dec_and_test(&loghandle->lgh_refcount))
108                 llog_free_handle(loghandle);
109 }
110
111 /* returns negative on error; 0 if success; 1 if success & log destroyed */
112 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
113                     int index)
114 {
115         struct llog_log_hdr *llh = loghandle->lgh_hdr;
116         int rc = 0;
117         ENTRY;
118
119         CDEBUG(D_RPCTRACE, "Canceling %d in log "LPX64"\n",
120                index, loghandle->lgh_id.lgl_oid);
121
122         if (index == 0) {
123                 CERROR("Can't cancel index 0 which is header\n");
124                 RETURN(-EINVAL);
125         }
126
127         spin_lock(&loghandle->lgh_hdr_lock);
128         if (!ext2_clear_bit(index, llh->llh_bitmap)) {
129                 spin_unlock(&loghandle->lgh_hdr_lock);
130                 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n", index);
131                 RETURN(-ENOENT);
132         }
133
134         llh->llh_count--;
135
136         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
137             (llh->llh_count == 1) &&
138             (loghandle->lgh_last_idx == (LLOG_BITMAP_BYTES * 8) - 1)) {
139                 spin_unlock(&loghandle->lgh_hdr_lock);
140                 rc = llog_destroy(env, loghandle);
141                 if (rc < 0) {
142                         CERROR("%s: can't destroy empty llog #"LPX64"#"LPX64
143                                "#%08x: rc = %d\n",
144                                loghandle->lgh_ctxt->loc_obd->obd_name,
145                                loghandle->lgh_id.lgl_oid,
146                                loghandle->lgh_id.lgl_oseq,
147                                loghandle->lgh_id.lgl_ogen, rc);
148                         GOTO(out_err, rc);
149                 }
150                 RETURN(1);
151         }
152         spin_unlock(&loghandle->lgh_hdr_lock);
153
154         rc = llog_write(env, loghandle, &llh->llh_hdr, NULL, 0, NULL, 0);
155         if (rc < 0) {
156                 CERROR("%s: fail to write header for llog #"LPX64"#"LPX64
157                        "#%08x: rc = %d\n",
158                        loghandle->lgh_ctxt->loc_obd->obd_name,
159                        loghandle->lgh_id.lgl_oid,
160                        loghandle->lgh_id.lgl_oseq,
161                        loghandle->lgh_id.lgl_ogen, rc);
162                 GOTO(out_err, rc);
163         }
164         RETURN(0);
165 out_err:
166         spin_lock(&loghandle->lgh_hdr_lock);
167         ext2_set_bit(index, llh->llh_bitmap);
168         llh->llh_count++;
169         spin_unlock(&loghandle->lgh_hdr_lock);
170         return rc;
171 }
172 EXPORT_SYMBOL(llog_cancel_rec);
173
174 static int llog_read_header(const struct lu_env *env,
175                             struct llog_handle *handle,
176                             struct obd_uuid *uuid)
177 {
178         struct llog_operations *lop;
179         int rc;
180
181         rc = llog_handle2ops(handle, &lop);
182         if (rc)
183                 RETURN(rc);
184
185         if (lop->lop_read_header == NULL)
186                 RETURN(-EOPNOTSUPP);
187
188         rc = lop->lop_read_header(env, handle);
189         if (rc == LLOG_EEMPTY) {
190                 struct llog_log_hdr *llh = handle->lgh_hdr;
191
192                 handle->lgh_last_idx = 0; /* header is record with index 0 */
193                 llh->llh_count = 1;         /* for the header record */
194                 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
195                 llh->llh_hdr.lrh_len = llh->llh_tail.lrt_len = LLOG_CHUNK_SIZE;
196                 llh->llh_hdr.lrh_index = llh->llh_tail.lrt_index = 0;
197                 llh->llh_timestamp = cfs_time_current_sec();
198                 if (uuid)
199                         memcpy(&llh->llh_tgtuuid, uuid,
200                                sizeof(llh->llh_tgtuuid));
201                 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
202                 ext2_set_bit(0, llh->llh_bitmap);
203                 rc = 0;
204         }
205         return rc;
206 }
207
208 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
209                      int flags, struct obd_uuid *uuid)
210 {
211         struct llog_log_hdr     *llh;
212         int                      rc;
213
214         ENTRY;
215         LASSERT(handle->lgh_hdr == NULL);
216
217         OBD_ALLOC_PTR(llh);
218         if (llh == NULL)
219                 RETURN(-ENOMEM);
220         handle->lgh_hdr = llh;
221         /* first assign flags to use llog_client_ops */
222         llh->llh_flags = flags;
223         rc = llog_read_header(env, handle, uuid);
224         if (rc == 0) {
225                 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
226                               flags & LLOG_F_IS_CAT) ||
227                              (llh->llh_flags & LLOG_F_IS_CAT &&
228                               flags & LLOG_F_IS_PLAIN))) {
229                         CERROR("%s: llog type is %s but initializing %s\n",
230                                handle->lgh_ctxt->loc_obd->obd_name,
231                                llh->llh_flags & LLOG_F_IS_CAT ?
232                                "catalog" : "plain",
233                                flags & LLOG_F_IS_CAT ? "catalog" : "plain");
234                         GOTO(out, rc = -EINVAL);
235                 } else if (llh->llh_flags &
236                            (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
237                         /*
238                          * it is possible to open llog without specifying llog
239                          * type so it is taken from llh_flags
240                          */
241                         flags = llh->llh_flags;
242                 } else {
243                         /* for some reason the llh_flags has no type set */
244                         CERROR("llog type is not specified!\n");
245                         GOTO(out, rc = -EINVAL);
246                 }
247                 if (unlikely(uuid &&
248                              !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
249                         CERROR("%s: llog uuid mismatch: %s/%s\n",
250                                handle->lgh_ctxt->loc_obd->obd_name,
251                                (char *)uuid->uuid,
252                                (char *)llh->llh_tgtuuid.uuid);
253                         GOTO(out, rc = -EEXIST);
254                 }
255         }
256         if (flags & LLOG_F_IS_CAT) {
257                 LASSERT(cfs_list_empty(&handle->u.chd.chd_head));
258                 CFS_INIT_LIST_HEAD(&handle->u.chd.chd_head);
259                 llh->llh_size = sizeof(struct llog_logid_rec);
260         } else if (!(flags & LLOG_F_IS_PLAIN)) {
261                 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
262                        handle->lgh_ctxt->loc_obd->obd_name,
263                        flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
264                 rc = -EINVAL;
265         }
266 out:
267         if (rc) {
268                 OBD_FREE_PTR(llh);
269                 handle->lgh_hdr = NULL;
270         }
271         RETURN(rc);
272 }
273 EXPORT_SYMBOL(llog_init_handle);
274
275 int llog_copy_handler(const struct lu_env *env,
276                       struct llog_handle *llh,
277                       struct llog_rec_hdr *rec,
278                       void *data)
279 {
280         struct llog_rec_hdr local_rec = *rec;
281         struct llog_handle *local_llh = (struct llog_handle *)data;
282         char *cfg_buf = (char*) (rec + 1);
283         struct lustre_cfg *lcfg;
284         int rc = 0;
285         ENTRY;
286
287         /* Append all records */
288         local_rec.lrh_len -= sizeof(*rec) + sizeof(struct llog_rec_tail);
289         rc = llog_write(env, local_llh, &local_rec, NULL, 0,
290                         (void *)cfg_buf, -1);
291
292         lcfg = (struct lustre_cfg *)cfg_buf;
293         CDEBUG(D_INFO, "idx=%d, rc=%d, len=%d, cmd %x %s %s\n",
294                rec->lrh_index, rc, rec->lrh_len, lcfg->lcfg_command,
295                lustre_cfg_string(lcfg, 0), lustre_cfg_string(lcfg, 1));
296
297         RETURN(rc);
298 }
299 EXPORT_SYMBOL(llog_copy_handler);
300
301 static int llog_process_thread(void *arg)
302 {
303         struct llog_process_info        *lpi = arg;
304         struct llog_handle              *loghandle = lpi->lpi_loghandle;
305         struct llog_log_hdr             *llh = loghandle->lgh_hdr;
306         struct llog_process_cat_data    *cd  = lpi->lpi_catdata;
307         char                            *buf;
308         __u64                            cur_offset = LLOG_CHUNK_SIZE;
309         __u64                            last_offset;
310         int                              rc = 0, index = 1, last_index;
311         int                              saved_index = 0;
312         int                              last_called_index = 0;
313
314         ENTRY;
315
316         LASSERT(llh);
317
318         OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
319         if (!buf) {
320                 lpi->lpi_rc = -ENOMEM;
321                 RETURN(0);
322         }
323
324         if (cd != NULL) {
325                 last_called_index = cd->lpcd_first_idx;
326                 index = cd->lpcd_first_idx + 1;
327         }
328         if (cd != NULL && cd->lpcd_last_idx)
329                 last_index = cd->lpcd_last_idx;
330         else
331                 last_index = LLOG_BITMAP_BYTES * 8 - 1;
332
333         while (rc == 0) {
334                 struct llog_rec_hdr *rec;
335
336                 /* skip records not set in bitmap */
337                 while (index <= last_index &&
338                        !ext2_test_bit(index, llh->llh_bitmap))
339                         ++index;
340
341                 LASSERT(index <= last_index + 1);
342                 if (index == last_index + 1)
343                         break;
344 repeat:
345                 CDEBUG(D_OTHER, "index: %d last_index %d\n",
346                        index, last_index);
347
348                 /* get the buf with our target record; avoid old garbage */
349                 memset(buf, 0, LLOG_CHUNK_SIZE);
350                 last_offset = cur_offset;
351                 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
352                                      index, &cur_offset, buf, LLOG_CHUNK_SIZE);
353                 if (rc)
354                         GOTO(out, rc);
355
356                 /* NB: when rec->lrh_len is accessed it is already swabbed
357                  * since it is used at the "end" of the loop and the rec
358                  * swabbing is done at the beginning of the loop. */
359                 for (rec = (struct llog_rec_hdr *)buf;
360                      (char *)rec < buf + LLOG_CHUNK_SIZE;
361                      rec = (struct llog_rec_hdr *)((char *)rec + rec->lrh_len)){
362
363                         CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
364                                rec, rec->lrh_type);
365
366                         if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
367                                 lustre_swab_llog_rec(rec);
368
369                         CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
370                                rec->lrh_type, rec->lrh_index);
371
372                         if (rec->lrh_index == 0) {
373                                 /* probably another rec just got added? */
374                                 if (index <= loghandle->lgh_last_idx)
375                                         GOTO(repeat, rc = 0);
376                                 GOTO(out, rc = 0); /* no more records */
377                         }
378                         if (rec->lrh_len == 0 ||
379                             rec->lrh_len > LLOG_CHUNK_SIZE) {
380                                 CWARN("invalid length %d in llog record for "
381                                       "index %d/%d\n", rec->lrh_len,
382                                       rec->lrh_index, index);
383                                 GOTO(out, rc = -EINVAL);
384                         }
385
386                         if (rec->lrh_index < index) {
387                                 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
388                                        rec->lrh_index);
389                                 continue;
390                         }
391
392                         CDEBUG(D_OTHER,
393                                "lrh_index: %d lrh_len: %d (%d remains)\n",
394                                rec->lrh_index, rec->lrh_len,
395                                (int)(buf + LLOG_CHUNK_SIZE - (char *)rec));
396
397                         loghandle->lgh_cur_idx = rec->lrh_index;
398                         loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
399                                                     last_offset;
400
401                         /* if set, process the callback on this record */
402                         if (ext2_test_bit(index, llh->llh_bitmap)) {
403                                 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
404                                                  lpi->lpi_cbdata);
405                                 last_called_index = index;
406                                 if (rc == LLOG_PROC_BREAK) {
407                                         GOTO(out, rc);
408                                 } else if (rc == LLOG_DEL_RECORD) {
409                                         llog_cancel_rec(lpi->lpi_env,
410                                                         loghandle,
411                                                         rec->lrh_index);
412                                         rc = 0;
413                                 }
414                                 if (rc)
415                                         GOTO(out, rc);
416                         } else {
417                                 CDEBUG(D_OTHER, "Skipped index %d\n", index);
418                         }
419
420                         /* next record, still in buffer? */
421                         ++index;
422                         if (index > last_index)
423                                 GOTO(out, rc = 0);
424                 }
425         }
426
427 out:
428         if (cd != NULL)
429                 cd->lpcd_last_idx = last_called_index;
430
431         OBD_FREE(buf, LLOG_CHUNK_SIZE);
432         lpi->lpi_rc = rc;
433         return 0;
434 }
435
436 #ifdef __KERNEL__
437 static int llog_process_thread_daemonize(void *arg)
438 {
439         struct llog_process_info        *lpi = arg;
440         struct lu_env                    env;
441         int                              rc;
442
443         cfs_daemonize_ctxt("llog_process_thread");
444
445         /* client env has no keys, tags is just 0 */
446         rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
447         if (rc)
448                 goto out;
449         lpi->lpi_env = &env;
450
451         rc = llog_process_thread(arg);
452
453         lu_env_fini(&env);
454 out:
455         complete(&lpi->lpi_completion);
456         return rc;
457 }
458 #endif
459
460 int llog_process_or_fork(const struct lu_env *env,
461                          struct llog_handle *loghandle,
462                          llog_cb_t cb, void *data, void *catdata, bool fork)
463 {
464         struct llog_process_info *lpi;
465         int                      rc;
466
467         ENTRY;
468
469         OBD_ALLOC_PTR(lpi);
470         if (lpi == NULL) {
471                 CERROR("cannot alloc pointer\n");
472                 RETURN(-ENOMEM);
473         }
474         lpi->lpi_loghandle = loghandle;
475         lpi->lpi_cb        = cb;
476         lpi->lpi_cbdata    = data;
477         lpi->lpi_catdata   = catdata;
478
479 #ifdef __KERNEL__
480         if (fork) {
481                 /* The new thread can't use parent env,
482                  * init the new one in llog_process_thread_daemonize. */
483                 lpi->lpi_env = NULL;
484                 init_completion(&lpi->lpi_completion);
485                 rc = cfs_create_thread(llog_process_thread_daemonize, lpi,
486                                        CFS_DAEMON_FLAGS);
487                 if (rc < 0) {
488                         CERROR("%s: cannot start thread: rc = %d\n",
489                                loghandle->lgh_ctxt->loc_obd->obd_name, rc);
490                         OBD_FREE_PTR(lpi);
491                         RETURN(rc);
492                 }
493                 wait_for_completion(&lpi->lpi_completion);
494         } else {
495                 lpi->lpi_env = env;
496                 llog_process_thread(lpi);
497         }
498 #else
499         lpi->lpi_env = env;
500         llog_process_thread(lpi);
501 #endif
502         rc = lpi->lpi_rc;
503         OBD_FREE_PTR(lpi);
504         RETURN(rc);
505 }
506 EXPORT_SYMBOL(llog_process_or_fork);
507
508 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
509                  llog_cb_t cb, void *data, void *catdata)
510 {
511         return llog_process_or_fork(env, loghandle, cb, data, catdata, true);
512 }
513 EXPORT_SYMBOL(llog_process);
514
515 inline int llog_get_size(struct llog_handle *loghandle)
516 {
517         if (loghandle && loghandle->lgh_hdr)
518                 return loghandle->lgh_hdr->llh_count;
519         return 0;
520 }
521 EXPORT_SYMBOL(llog_get_size);
522
523 int llog_reverse_process(const struct lu_env *env,
524                          struct llog_handle *loghandle, llog_cb_t cb,
525                          void *data, void *catdata)
526 {
527         struct llog_log_hdr *llh = loghandle->lgh_hdr;
528         struct llog_process_cat_data *cd = catdata;
529         void *buf;
530         int rc = 0, first_index = 1, index, idx;
531         ENTRY;
532
533         OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
534         if (!buf)
535                 RETURN(-ENOMEM);
536
537         if (cd != NULL)
538                 first_index = cd->lpcd_first_idx + 1;
539         if (cd != NULL && cd->lpcd_last_idx)
540                 index = cd->lpcd_last_idx;
541         else
542                 index = LLOG_BITMAP_BYTES * 8 - 1;
543
544         while (rc == 0) {
545                 struct llog_rec_hdr *rec;
546                 struct llog_rec_tail *tail;
547
548                 /* skip records not set in bitmap */
549                 while (index >= first_index &&
550                        !ext2_test_bit(index, llh->llh_bitmap))
551                         --index;
552
553                 LASSERT(index >= first_index - 1);
554                 if (index == first_index - 1)
555                         break;
556
557                 /* get the buf with our target record; avoid old garbage */
558                 memset(buf, 0, LLOG_CHUNK_SIZE);
559                 rc = llog_prev_block(env, loghandle, index, buf,
560                                      LLOG_CHUNK_SIZE);
561                 if (rc)
562                         GOTO(out, rc);
563
564                 rec = buf;
565                 idx = rec->lrh_index;
566                 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
567                 while (idx < index) {
568                         rec = (void *)rec + rec->lrh_len;
569                         if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
570                                 lustre_swab_llog_rec(rec);
571                         idx ++;
572                 }
573                 LASSERT(idx == index);
574                 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
575
576                 /* process records in buffer, starting where we found one */
577                 while ((void *)tail > buf) {
578                         if (tail->lrt_index == 0)
579                                 GOTO(out, rc = 0); /* no more records */
580
581                         /* if set, process the callback on this record */
582                         if (ext2_test_bit(index, llh->llh_bitmap)) {
583                                 rec = (void *)tail - tail->lrt_len +
584                                       sizeof(*tail);
585
586                                 rc = cb(env, loghandle, rec, data);
587                                 if (rc == LLOG_PROC_BREAK) {
588                                         GOTO(out, rc);
589                                 } else if (rc == LLOG_DEL_RECORD) {
590                                         llog_cancel_rec(env, loghandle,
591                                                         tail->lrt_index);
592                                         rc = 0;
593                                 }
594                                 if (rc)
595                                         GOTO(out, rc);
596                         }
597
598                         /* previous record, still in buffer? */
599                         --index;
600                         if (index < first_index)
601                                 GOTO(out, rc = 0);
602                         tail = (void *)tail - tail->lrt_len;
603                 }
604         }
605
606 out:
607         if (buf)
608                 OBD_FREE(buf, LLOG_CHUNK_SIZE);
609         RETURN(rc);
610 }
611 EXPORT_SYMBOL(llog_reverse_process);
612
613 /**
614  * new llog API
615  *
616  * API functions:
617  *      llog_open - open llog, may not exist
618  *      llog_exist - check if llog exists
619  *      llog_close - close opened llog, pair for open, frees llog_handle
620  *      llog_declare_create - declare llog creation
621  *      llog_create - create new llog on disk, need transaction handle
622  *      llog_declare_write_rec - declaration of llog write
623  *      llog_write_rec - write llog record on disk, need transaction handle
624  *      llog_declare_add - declare llog catalog record addition
625  *      llog_add - add llog record in catalog, need transaction handle
626  */
627 int llog_exist(struct llog_handle *loghandle)
628 {
629         struct llog_operations  *lop;
630         int                      rc;
631
632         ENTRY;
633
634         rc = llog_handle2ops(loghandle, &lop);
635         if (rc)
636                 RETURN(rc);
637         if (lop->lop_exist == NULL)
638                 RETURN(-EOPNOTSUPP);
639
640         rc = lop->lop_exist(loghandle);
641         RETURN(rc);
642 }
643 EXPORT_SYMBOL(llog_exist);
644
645 int llog_declare_create(const struct lu_env *env,
646                         struct llog_handle *loghandle, struct thandle *th)
647 {
648         struct llog_operations  *lop;
649         int                      raised, rc;
650
651         ENTRY;
652
653         rc = llog_handle2ops(loghandle, &lop);
654         if (rc)
655                 RETURN(rc);
656         if (lop->lop_declare_create == NULL)
657                 RETURN(-EOPNOTSUPP);
658
659         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
660         if (!raised)
661                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
662         rc = lop->lop_declare_create(env, loghandle, th);
663         if (!raised)
664                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
665         RETURN(rc);
666 }
667 EXPORT_SYMBOL(llog_declare_create);
668
669 int llog_create(const struct lu_env *env, struct llog_handle *handle,
670                 struct thandle *th)
671 {
672         struct llog_operations  *lop;
673         int                      raised, rc;
674
675         ENTRY;
676
677         rc = llog_handle2ops(handle, &lop);
678         if (rc)
679                 RETURN(rc);
680         if (lop->lop_create == NULL)
681                 RETURN(-EOPNOTSUPP);
682
683         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
684         if (!raised)
685                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
686         rc = lop->lop_create(env, handle, th);
687         if (!raised)
688                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
689         RETURN(rc);
690 }
691 EXPORT_SYMBOL(llog_create);
692
693 int llog_declare_write_rec(const struct lu_env *env,
694                            struct llog_handle *handle,
695                            struct llog_rec_hdr *rec, int idx,
696                            struct thandle *th)
697 {
698         struct llog_operations  *lop;
699         int                      raised, rc;
700
701         ENTRY;
702
703         rc = llog_handle2ops(handle, &lop);
704         if (rc)
705                 RETURN(rc);
706         LASSERT(lop);
707         if (lop->lop_declare_write_rec == NULL)
708                 RETURN(-EOPNOTSUPP);
709
710         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
711         if (!raised)
712                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
713         rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
714         if (!raised)
715                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
716         RETURN(rc);
717 }
718 EXPORT_SYMBOL(llog_declare_write_rec);
719
720 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
721                    struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
722                    int numcookies, void *buf, int idx, struct thandle *th)
723 {
724         struct llog_operations  *lop;
725         int                      raised, rc, buflen;
726
727         ENTRY;
728
729         rc = llog_handle2ops(handle, &lop);
730         if (rc)
731                 RETURN(rc);
732
733         LASSERT(lop);
734         if (lop->lop_write_rec == NULL)
735                 RETURN(-EOPNOTSUPP);
736
737         if (buf)
738                 buflen = rec->lrh_len + sizeof(struct llog_rec_hdr) +
739                          sizeof(struct llog_rec_tail);
740         else
741                 buflen = rec->lrh_len;
742         LASSERT(cfs_size_round(buflen) == buflen);
743
744         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
745         if (!raised)
746                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
747         rc = lop->lop_write_rec(env, handle, rec, logcookies, numcookies,
748                                 buf, idx, th);
749         if (!raised)
750                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
751         RETURN(rc);
752 }
753 EXPORT_SYMBOL(llog_write_rec);
754
755 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
756              struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
757              void *buf, struct thandle *th)
758 {
759         int raised, rc;
760
761         ENTRY;
762
763         if (lgh->lgh_logops->lop_add == NULL)
764                 RETURN(-EOPNOTSUPP);
765
766         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
767         if (!raised)
768                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
769         rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, buf, th);
770         if (!raised)
771                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
772         RETURN(rc);
773 }
774 EXPORT_SYMBOL(llog_add);
775
776 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
777                      struct llog_rec_hdr *rec, struct thandle *th)
778 {
779         int raised, rc;
780
781         ENTRY;
782
783         if (lgh->lgh_logops->lop_declare_add == NULL)
784                 RETURN(-EOPNOTSUPP);
785
786         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
787         if (!raised)
788                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
789         rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
790         if (!raised)
791                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
792         RETURN(rc);
793 }
794 EXPORT_SYMBOL(llog_declare_add);
795
796 /**
797  * Helper function to open llog or create it if doesn't exist.
798  * It hides all transaction handling from caller.
799  */
800 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
801                      struct llog_handle **res, struct llog_logid *logid,
802                      char *name)
803 {
804         struct thandle  *th;
805         int              rc;
806
807         ENTRY;
808
809         rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
810         if (rc)
811                 RETURN(rc);
812
813         if (llog_exist(*res))
814                 RETURN(0);
815
816         if ((*res)->lgh_obj != NULL) {
817                 struct dt_device *d;
818
819                 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
820
821                 th = dt_trans_create(env, d);
822                 if (IS_ERR(th))
823                         GOTO(out, rc = PTR_ERR(th));
824
825                 rc = llog_declare_create(env, *res, th);
826                 if (rc == 0) {
827                         rc = dt_trans_start_local(env, d, th);
828                         if (rc == 0)
829                                 rc = llog_create(env, *res, th);
830                 }
831                 dt_trans_stop(env, d, th);
832         } else {
833                 /* lvfs compat code */
834                 LASSERT((*res)->lgh_file == NULL);
835                 rc = llog_create(env, *res, NULL);
836         }
837 out:
838         if (rc)
839                 llog_close(env, *res);
840         RETURN(rc);
841 }
842 EXPORT_SYMBOL(llog_open_create);
843
844 /**
845  * Helper function to delete existent llog.
846  */
847 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
848                struct llog_logid *logid, char *name)
849 {
850         struct llog_handle      *handle;
851         int                      rc = 0, rc2;
852
853         ENTRY;
854
855         /* nothing to erase */
856         if (name == NULL && logid == NULL)
857                 RETURN(0);
858
859         rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
860         if (rc < 0)
861                 RETURN(rc);
862
863         rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
864         if (rc == 0)
865                 rc = llog_destroy(env, handle);
866
867         rc2 = llog_close(env, handle);
868         if (rc == 0)
869                 rc = rc2;
870         RETURN(rc);
871 }
872 EXPORT_SYMBOL(llog_erase);
873
874 /*
875  * Helper function for write record in llog.
876  * It hides all transaction handling from caller.
877  * Valid only with local llog.
878  */
879 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
880                struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
881                int cookiecount, void *buf, int idx)
882 {
883         int rc;
884
885         ENTRY;
886
887         LASSERT(loghandle);
888         LASSERT(loghandle->lgh_ctxt);
889
890         if (loghandle->lgh_obj != NULL) {
891                 struct dt_device        *dt;
892                 struct thandle          *th;
893
894                 dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
895
896                 th = dt_trans_create(env, dt);
897                 if (IS_ERR(th))
898                         RETURN(PTR_ERR(th));
899
900                 rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
901                 if (rc)
902                         GOTO(out_trans, rc);
903
904                 rc = dt_trans_start_local(env, dt, th);
905                 if (rc)
906                         GOTO(out_trans, rc);
907
908                 down_write(&loghandle->lgh_lock);
909                 rc = llog_write_rec(env, loghandle, rec, reccookie,
910                                     cookiecount, buf, idx, th);
911                 up_write(&loghandle->lgh_lock);
912 out_trans:
913                 dt_trans_stop(env, dt, th);
914         } else { /* lvfs compatibility */
915                 down_write(&loghandle->lgh_lock);
916                 rc = llog_write_rec(env, loghandle, rec, reccookie,
917                                     cookiecount, buf, idx, NULL);
918                 up_write(&loghandle->lgh_lock);
919         }
920         RETURN(rc);
921 }
922 EXPORT_SYMBOL(llog_write);
923
924 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
925               struct llog_handle **lgh, struct llog_logid *logid,
926               char *name, enum llog_open_param open_param)
927 {
928         int      raised;
929         int      rc;
930
931         ENTRY;
932
933         LASSERT(ctxt);
934         LASSERT(ctxt->loc_logops);
935
936         if (ctxt->loc_logops->lop_open == NULL) {
937                 *lgh = NULL;
938                 RETURN(-EOPNOTSUPP);
939         }
940
941         *lgh = llog_alloc_handle();
942         if (*lgh == NULL)
943                 RETURN(-ENOMEM);
944         (*lgh)->lgh_ctxt = ctxt;
945         (*lgh)->lgh_logops = ctxt->loc_logops;
946
947         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
948         if (!raised)
949                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
950         rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
951         if (!raised)
952                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
953         if (rc) {
954                 llog_free_handle(*lgh);
955                 *lgh = NULL;
956         }
957         RETURN(rc);
958 }
959 EXPORT_SYMBOL(llog_open);
960
961 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
962 {
963         struct llog_operations  *lop;
964         int                      rc;
965
966         ENTRY;
967
968         rc = llog_handle2ops(loghandle, &lop);
969         if (rc)
970                 GOTO(out, rc);
971         if (lop->lop_close == NULL)
972                 GOTO(out, rc = -EOPNOTSUPP);
973         rc = lop->lop_close(env, loghandle);
974 out:
975         llog_handle_put(loghandle);
976         RETURN(rc);
977 }
978 EXPORT_SYMBOL(llog_close);