1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001 Cluster File Systems, Inc. <info@clusterfs.com>
6 * This file is part of Lustre, http://www.lustre.org.
8 * Lustre is free software; you can redistribute it and/or
9 * modify it under the terms of version 2 of the GNU General Public
10 * License as published by the Free Software Foundation.
12 * Lustre is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Lustre; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 * Generic infrastructure for managing a collection of logs.
23 * These logs are used for:
25 * - orphan recovery: OST adds record on create
26 * - mtime/size consistency: the OST adds a record on first write
27 * - open/unlinked objects: OST adds a record on destroy
29 * - mds unlink log: the MDS adds an entry upon delete
31 * - raid1 replication log between OST's
32 * - MDS replication logs
38 #include <linux/obd.h>
39 #include <linux/lustre_idl.h>
41 #define LOG_NAME_LIMIT(logname, name) \
42 snprintf(logname, sizeof(logname), "LOGS/%s", name)
43 #define LLOG_EEMPTY 4711
47 struct plain_handle_data {
48 struct list_head phd_entry;
49 struct llog_handle *phd_cat_handle;
50 struct llog_cookie phd_cookie; /* cookie of this log in its cat */
54 struct cat_handle_data {
55 struct list_head chd_head;
56 struct llog_handle *chd_current_log; /* currently open log */
59 /* In-memory descriptor for a log object or log catalog */
61 struct rw_semaphore lgh_lock;
62 struct llog_logid lgh_id; /* id of this log */
63 struct llog_log_hdr *lgh_hdr;
64 struct file *lgh_file;
66 struct llog_ctxt *lgh_ctxt;
68 struct plain_handle_data phd;
69 struct cat_handle_data chd;
73 /* llog.c - general API */
74 typedef int (*llog_cb_t)(struct llog_handle *, struct llog_rec_hdr *, void *);
75 struct llog_handle *llog_alloc_handle(void);
76 void llog_free_handle(struct llog_handle *handle);
77 int llog_cancel_rec(struct llog_handle *loghandle, int index);
78 int llog_init_handle(struct llog_handle *handle, int flags,
79 struct obd_uuid *uuid);
80 int llog_close(struct llog_handle *cathandle);
81 int llog_process(struct llog_handle *loghandle, llog_cb_t cb,
82 void *data, void *catdata);
83 int llog_reverse_process(struct llog_handle *loghandle, llog_cb_t cb,
84 void *data, void *catdata);
86 /* llog_cat.c - catalog api */
87 struct llog_process_data {
92 struct llog_process_cat_data {
95 /* to process catalog across zero record */
98 int llog_cat_id2handle(struct llog_handle *cathandle, struct llog_handle **res,
99 struct llog_logid *logid);
100 int class_config_dump_handler(struct llog_handle * handle,
101 struct llog_rec_hdr *rec, void *data);
102 int llog_cat_put(struct llog_handle *cathandle);
103 int llog_cat_add_rec(struct llog_handle *cathandle, struct llog_rec_hdr *rec,
104 struct llog_cookie *reccookie, void *buf,
105 struct rw_semaphore **lock, int *lock_count);
106 int llog_cat_cancel_records(struct llog_handle *cathandle, int count,
107 struct llog_cookie *cookies);
108 int llog_cat_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data);
109 int llog_cat_reverse_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data);
110 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index);
113 int llog_catalog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
114 void *buf, struct llog_cookie *reccookie, int, void *data,
115 struct rw_semaphore **lock, int *lock_count);
116 int llog_catalog_cancel(struct llog_ctxt *ctxt, int count, struct llog_cookie *,
117 int flags, void *data);
118 int llog_catalog_setup(struct llog_ctxt **res, char *name, struct obd_export *exp,
119 struct lvfs_run_ctxt *, struct fsfilt_operations *fsops,
120 struct dentry *logs_de, struct dentry *objects_de);
121 int llog_catalog_cleanup(struct llog_ctxt *ctxt);
122 int llog_cat_half_bottom(struct llog_cookie *, struct llog_handle *);
125 int llog_get_cat_list(struct lvfs_run_ctxt *, struct fsfilt_operations *,
126 const char *name, int count, struct llog_catid *idarray);
127 int llog_put_cat_list(struct lvfs_run_ctxt *, struct fsfilt_operations *,
128 const char *name, int count, struct llog_catid *idarray);
130 extern struct llog_operations llog_lvfs_ops;
132 int llog_obd_origin_setup(struct obd_device *, struct obd_llogs *, int,
133 struct obd_device *, int, struct llog_logid *);
135 int obd_llog_cat_initialize(struct obd_device *, struct obd_llogs *, int, const char *);
137 /* llog_obd.c - obd llog api */
138 int obd_llog_setup(struct obd_device *obd, struct obd_llogs *, int index,
139 struct obd_device *disk_obd, int count, struct llog_logid *logid,
140 struct llog_operations *op);
141 int obd_llog_init(struct obd_device *, struct obd_llogs *, struct obd_device *,
142 int, struct llog_catid *);
143 int obd_llog_cleanup(struct llog_ctxt *);
145 int obd_llog_finish(struct obd_device *, struct obd_llogs *, int);
148 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data);
149 int llog_catalog_list(struct obd_device *obd, int count,
150 struct obd_ioctl_data *data);
153 int llog_initiator_connect(struct llog_ctxt *ctxt);
154 int llog_receptor_accept(struct llog_ctxt *ctxt, struct obd_import *imp);
155 int llog_origin_connect(struct llog_ctxt *ctxt, int count,
156 struct llog_logid *logid, struct llog_gen *gen,
157 struct obd_uuid *uuid);
158 int llog_handle_connect(struct ptlrpc_request *req);
161 int llog_obd_repl_cancel(struct llog_ctxt *ctxt, int count,
162 struct llog_cookie *cookies, int flags, void *data);
164 int llog_obd_repl_sync(struct llog_ctxt *ctxt, struct obd_export *exp);
165 int llog_repl_connect(struct llog_ctxt *ctxt, int count,
166 struct llog_logid *logid, struct llog_gen *gen,
167 struct obd_uuid *uuid);
169 struct llog_operations {
170 int (*lop_setup)(struct obd_device *, struct obd_llogs *, int,
171 struct obd_device *, int, struct llog_logid *);
173 int (*lop_cleanup)(struct llog_ctxt *ctxt);
174 int (*lop_open)(struct llog_ctxt *ctxt, struct llog_handle **,
175 struct llog_logid *logid, char *name, int flags);
176 int (*lop_destroy)(struct llog_handle *handle);
177 int (*lop_close)(struct llog_handle *handle);
179 int (*lop_read_header)(struct llog_handle *handle);
180 int (*lop_add)(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
181 void *buf, struct llog_cookie *logcookies,
182 int numcookies, void *data, struct rw_semaphore **lock,
184 int (*lop_cancel)(struct llog_ctxt *ctxt, int count,
185 struct llog_cookie *cookies, int flags, void *data);
186 int (*lop_write_rec)(struct llog_handle *loghandle,
187 struct llog_rec_hdr *rec,
188 struct llog_cookie *logcookies, int numcookies,
190 int (*lop_next_block)(struct llog_handle *h, int *curr_idx,
191 int next_idx, __u64 *offset, void *buf, int len);
192 int (*lop_prev_block)(struct llog_handle *h,
193 int prev_idx, void *buf, int len);
195 int (*lop_sync)(struct llog_ctxt *ctxt, struct obd_export *exp);
196 int (*lop_connect)(struct llog_ctxt *ctxt, int count,
197 struct llog_logid *logid, struct llog_gen *gen,
198 struct obd_uuid *uuid);
202 /* needed for lvfs based log */
203 struct llog_handle *loc_handle;
204 struct llog_operations *loc_logops;
205 struct fsfilt_operations *loc_fsops;
206 struct dentry *loc_logs_dir;
207 struct dentry *loc_objects_dir;
208 struct lvfs_run_ctxt *loc_lvfs_ctxt;
210 struct obd_device *loc_obd; /* points back to the containing obd */
211 struct llog_gen loc_gen;
212 int loc_idx; /* my index the obd array of ctxt's */
213 int loc_alone; /* is this llog ctxt has an obd? */
215 struct obd_export *loc_exp;
216 struct obd_import *loc_imp; /* to use in RPC's: can be backward
218 struct llog_canceld_ctxt *loc_llcd;
219 struct semaphore loc_sem; /* protects loc_llcd and loc_imp */
221 struct obd_llogs *loc_llogs;
224 struct llog_create_locks {
226 struct rw_semaphore *lcl_locks[0];
229 static inline void llog_gen_init(struct llog_ctxt *ctxt)
231 struct obd_device *obd = ctxt->loc_exp->exp_obd;
233 if (!strcmp(obd->obd_type->typ_name, "mds"))
234 ctxt->loc_gen.mnt_cnt = obd->u.mds.mds_mount_count;
235 else if (!strstr(obd->obd_type->typ_name, "filter"))
236 ctxt->loc_gen.mnt_cnt = obd->u.filter.fo_mount_count;
238 ctxt->loc_gen.mnt_cnt = 0;
241 static inline int llog_gen_lt(struct llog_gen a, struct llog_gen b)
243 if (a.mnt_cnt < b.mnt_cnt)
245 if (a.mnt_cnt > b.mnt_cnt)
247 return(a.conn_cnt < b.conn_cnt ? 1 : 0);
250 #define LLOG_GEN_INC(gen) ((gen).conn_cnt) ++
251 #define LLOG_PROC_BREAK 0x0001
252 #define LLOG_DEL_RECORD 0x0002
254 #define EQ_LOGID(a, b) (a.lgl_oid == b.lgl_oid && \
255 a.lgl_ogr == b.lgl_ogr && \
256 a.lgl_ogen == b.lgl_ogen)
258 /* Flags are in the bottom 16 bit */
259 #define LLOG_COOKIE_FLAG_MASK 0x0000ffff
260 #define LLOG_COOKIE_REPLAY_NEW 0x00000001
261 #define LLOG_COOKIE_REPLAY 0x00000002
263 static inline int llog_cookie_get_flags(struct llog_cookie *logcookie)
265 return(logcookie->lgc_flags & LLOG_COOKIE_FLAG_MASK);
268 static inline void llog_cookie_add_flags(struct llog_cookie *logcookie, int flags)
270 logcookie->lgc_flags |= LLOG_COOKIE_FLAG_MASK & flags;
273 static inline void llog_cookie_set_flags(struct llog_cookie *logcookie, int flags)
275 logcookie->lgc_flags &= ~LLOG_COOKIE_FLAG_MASK;
276 llog_cookie_add_flags(logcookie, flags);
279 static inline void llog_cookie_clear_flags(struct llog_cookie *logcookie, int flags)
281 logcookie->lgc_flags &= ~(LLOG_COOKIE_FLAG_MASK & flags);
284 static inline int llog_ctxt2ops(struct llog_ctxt *ctxt,
285 struct llog_operations **lop)
290 *lop = ctxt->loc_logops;
297 static inline int llog_handle2ops(struct llog_handle *loghandle,
298 struct llog_operations **lop)
300 if (loghandle == NULL)
303 return llog_ctxt2ops(loghandle->lgh_ctxt, lop);
306 static inline int llog_data_len(int len)
308 return size_round(len);
311 static inline struct llog_ctxt *llog_get_context(struct obd_llogs *llogs,
314 if (index < 0 || index >= LLOG_MAX_CTXTS)
317 return llogs->llog_ctxt[index];
320 static inline int llog_open(struct llog_ctxt *ctxt, struct llog_handle **res,
321 struct llog_logid *logid, char *name, int flags)
323 struct llog_operations *lop;
327 rc = llog_ctxt2ops(ctxt, &lop);
330 if (lop->lop_open == NULL)
333 rc = lop->lop_open(ctxt, res, logid, name, flags);
337 static inline int llog_destroy(struct llog_handle *handle)
339 struct llog_operations *lop;
343 rc = llog_handle2ops(handle, &lop);
346 if (lop->lop_destroy == NULL)
349 rc = lop->lop_destroy(handle);
353 static inline int llog_read_header(struct llog_handle *handle)
355 struct llog_operations *lop;
359 rc = llog_handle2ops(handle, &lop);
362 if (lop->lop_read_header == NULL)
365 rc = lop->lop_read_header(handle);
369 static inline int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
370 void *buf, struct llog_cookie *logcookies,
371 int numcookies, void *data,
372 struct rw_semaphore **lock, int *lock_count)
374 struct llog_operations *lop;
378 rc = llog_ctxt2ops(ctxt, &lop);
381 if (lop->lop_add == NULL)
384 rc = lop->lop_add(ctxt, rec, buf, logcookies, numcookies, data,
389 static inline int llog_cancel(struct llog_ctxt *ctxt, int count,
390 struct llog_cookie *cookies, int flags, void *data)
392 struct llog_operations *lop;
396 rc = llog_ctxt2ops(ctxt, &lop);
399 if (lop->lop_cancel == NULL)
402 rc = lop->lop_cancel(ctxt, count, cookies, flags, data);
406 static inline int llog_write_rec(struct llog_handle *handle,
407 struct llog_rec_hdr *rec,
408 struct llog_cookie *logcookies,
409 int numcookies, void *buf, int idx)
411 struct llog_operations *lop;
415 rc = llog_handle2ops(handle, &lop);
418 if (lop->lop_write_rec == NULL)
422 buflen = le32_to_cpu(rec->lrh_len) + sizeof(struct llog_rec_hdr)
423 + sizeof(struct llog_rec_tail);
425 buflen = le32_to_cpu(rec->lrh_len);
426 LASSERT(size_round(buflen) == buflen);
428 rc = lop->lop_write_rec(handle, rec, logcookies, numcookies, buf, idx);
432 static inline int llog_next_block(struct llog_handle *loghandle, int *curr_idx,
433 int next_idx, __u64 *curr_offset, void *buf,
436 struct llog_operations *lop;
440 rc = llog_handle2ops(loghandle, &lop);
443 if (lop->lop_next_block == NULL)
446 rc = lop->lop_next_block(loghandle, curr_idx, next_idx, curr_offset, buf,
451 static inline int llog_prev_block(struct llog_handle *loghandle,
452 int prev_idx, void *buf, int len)
454 struct llog_operations *lop;
458 rc = llog_handle2ops(loghandle, &lop);
461 if (lop->lop_prev_block == NULL)
464 rc = lop->lop_prev_block(loghandle, prev_idx, buf, len);
468 static inline int llog_connect(struct llog_ctxt *ctxt, int count,
469 struct llog_logid *logid, struct llog_gen *gen,
470 struct obd_uuid *uuid)
472 struct llog_operations *lop;
476 rc = llog_ctxt2ops(ctxt, &lop);
479 if (lop->lop_connect == NULL)
482 rc = lop->lop_connect(ctxt, count, logid, gen, uuid);
486 static inline int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
488 struct llog_operations *lop;
492 rc = llog_ctxt2ops(ctxt, &lop);
495 if (lop->lop_sync == NULL)
498 rc = lop->lop_sync(ctxt, exp);
502 static inline void llog_create_lock_free(struct llog_create_locks *lcl)
504 int size, offset = offsetof(struct llog_create_locks, lcl_locks);
508 for (i = 0; i < lcl->lcl_count; i ++) {
509 if (lcl->lcl_locks[i] != NULL) {
511 LASSERT(down_write_trylock(lcl->lcl_locks[i]) == 0);
513 up_write(lcl->lcl_locks[i]);
516 size = offset + sizeof(struct rw_semaphore *) * lcl->lcl_count;