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