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
45 struct plain_handle_data {
46 struct list_head phd_entry;
47 struct llog_handle *phd_cat_handle;
48 struct llog_cookie phd_cookie; /* cookie of this log in its cat */
52 struct cat_handle_data {
53 struct list_head chd_head;
54 struct llog_handle *chd_current_log; /* currently open log */
57 /* In-memory descriptor for a log object or log catalog */
59 struct rw_semaphore lgh_lock;
60 struct llog_logid lgh_id; /* id of this log */
61 struct llog_log_hdr *lgh_hdr;
62 struct file *lgh_file;
64 struct llog_ctxt *lgh_ctxt;
66 struct plain_handle_data phd;
67 struct cat_handle_data chd;
71 /* llog.c - general API */
72 typedef int (*llog_cb_t)(struct llog_handle *, struct llog_rec_hdr *, void *);
73 struct llog_handle *llog_alloc_handle(void);
74 void llog_free_handle(struct llog_handle *handle);
75 int llog_cancel_rec(struct llog_handle *loghandle, int index);
76 int llog_init_handle(struct llog_handle *handle, int flags,
77 struct obd_uuid *uuid);
78 int llog_close(struct llog_handle *cathandle);
79 int llog_process(struct llog_handle *loghandle, llog_cb_t cb,
80 void *data, void *catdata);
81 int llog_reverse_process(struct llog_handle *loghandle, llog_cb_t cb,
82 void *data, void *catdata);
84 /* llog_cat.c - catalog api */
85 struct llog_process_data {
90 struct llog_process_cat_data {
93 /* to process catalog across zero record */
96 int llog_cat_id2handle(struct llog_handle *cathandle, struct llog_handle **res,
97 struct llog_logid *logid);
98 int llog_cat_put(struct llog_handle *cathandle);
99 int llog_cat_add_rec(struct llog_handle *cathandle, struct llog_rec_hdr *rec,
100 struct llog_cookie *reccookie, void *buf);
101 int llog_cat_cancel_records(struct llog_handle *cathandle, int count,
102 struct llog_cookie *cookies);
103 int llog_cat_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data);
104 int llog_cat_reverse_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data);
105 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index);
107 int llog_catalog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
108 void *buf, struct llog_cookie *reccookie, int, void *data);
109 int llog_catalog_cancel(struct llog_ctxt *ctxt, int count, struct llog_cookie *,
110 int flags, void *data);
111 int llog_catalog_setup(struct llog_ctxt **res, char *name, struct lvfs_run_ctxt *,
112 struct fsfilt_operations *fsops, struct dentry *logs_de,
113 struct dentry *objects_de);
114 int llog_catalog_cleanup(struct llog_ctxt *ctxt);
115 int llog_cat_half_bottom(struct llog_cookie *, struct llog_handle *);
118 int llog_get_cat_list(struct lvfs_run_ctxt *, struct fsfilt_operations *,
119 char *name, int count, struct llog_catid *idarray);
120 int llog_put_cat_list(struct lvfs_run_ctxt *, struct fsfilt_operations *,
121 char *name, int count, struct llog_catid *idarray);
122 extern struct llog_operations llog_lvfs_ops;
124 /* llog_obd.c - obd llog api */
125 int obd_llog_setup(struct obd_device *obd, int index, struct obd_device *disk_obd,
126 int count, struct llog_logid *logid,struct llog_operations *op);
127 int obd_llog_cleanup(struct llog_ctxt *);
128 int llog_obd_origin_setup(struct obd_device *obd, int index,
129 struct obd_device *disk_obd, int count,
130 struct llog_logid *logid);
131 int obd_llog_cat_initialize(struct obd_device *obd, int count, char *name);
132 int obd_llog_init(struct obd_device *obd, struct obd_device *disk_obd,
133 int count, struct llog_catid *logid);
135 int obd_llog_finish(struct obd_device *obd, int count);
138 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data);
139 int llog_catalog_list(struct obd_device *obd, int count,
140 struct obd_ioctl_data *data);
143 int llog_initiator_connect(struct llog_ctxt *ctxt);
144 int llog_receptor_accept(struct llog_ctxt *ctxt, struct obd_import *imp);
145 int llog_origin_connect(struct llog_ctxt *ctxt, int count,
146 struct llog_logid *logid, struct llog_gen *gen,
147 struct obd_uuid *uuid);
148 int llog_handle_connect(struct ptlrpc_request *req);
151 int llog_obd_repl_cancel(struct llog_ctxt *ctxt, int count,
152 struct llog_cookie *cookies, int flags, void *data);
154 int llog_obd_repl_sync(struct llog_ctxt *ctxt, struct obd_export *exp);
155 int llog_repl_connect(struct llog_ctxt *ctxt, int count,
156 struct llog_logid *logid, struct llog_gen *gen,
157 struct obd_uuid *uuid);
159 struct llog_operations {
160 int (*lop_setup)(struct obd_device *obd, int ctxt_idx,
161 struct obd_device *disk_obd, int count,
162 struct llog_logid *logid);
163 int (*lop_cleanup)(struct llog_ctxt *ctxt);
164 int (*lop_create)(struct llog_ctxt *ctxt, struct llog_handle **,
165 struct llog_logid *logid, char *name);
166 int (*lop_destroy)(struct llog_handle *handle);
167 int (*lop_close)(struct llog_handle *handle);
169 int (*lop_read_header)(struct llog_handle *handle);
170 int (*lop_add)(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
171 void *buf, struct llog_cookie *logcookies,
172 int numcookies, void *data);
173 int (*lop_cancel)(struct llog_ctxt *ctxt, int count,
174 struct llog_cookie *cookies, int flags, void *data);
175 int (*lop_write_rec)(struct llog_handle *loghandle,
176 struct llog_rec_hdr *rec,
177 struct llog_cookie *logcookies, int numcookies,
179 int (*lop_next_block)(struct llog_handle *h, int *curr_idx,
180 int next_idx, __u64 *offset, void *buf, int len);
181 int (*lop_prev_block)(struct llog_handle *h,
182 int prev_idx, void *buf, int len);
184 /* XXX add 2 more: commit callbacks and llog recovery functions */
185 int (*lop_sync)(struct llog_ctxt *ctxt, struct obd_export *exp);
186 int (*lop_connect)(struct llog_ctxt *ctxt, int count,
187 struct llog_logid *logid, struct llog_gen *gen,
188 struct obd_uuid *uuid);
192 /* needed for lvfs based log */
193 struct llog_handle *loc_handle;
194 struct llog_operations *loc_logops;
195 struct fsfilt_operations *loc_fsops;
196 struct dentry *loc_logs_dir;
197 struct dentry *loc_objects_dir;
198 struct lvfs_run_ctxt *loc_lvfs_ctxt;
200 struct obd_device *loc_obd; /* points back to the containing obd*/
201 struct llog_gen loc_gen;
202 int loc_idx; /* my index the obd array of ctxt's */
204 struct obd_export *loc_exp;
205 struct obd_import *loc_imp; /* to use in RPC's: can be backward
207 struct llog_canceld_ctxt *loc_llcd;
208 struct semaphore loc_sem; /* protects loc_llcd */
209 void *loc_proc_cb; /* cb for recovery */
212 static inline void llog_gen_init(struct llog_ctxt *ctxt)
214 struct obd_device *obd = ctxt->loc_exp->exp_obd;
216 if (!strcmp(obd->obd_type->typ_name, "mds"))
217 ctxt->loc_gen.mnt_cnt = obd->u.mds.mds_mount_count;
218 else if (!strstr(obd->obd_type->typ_name, "filter"))
219 ctxt->loc_gen.mnt_cnt = obd->u.filter.fo_mount_count;
221 ctxt->loc_gen.mnt_cnt = 0;
224 static inline int llog_gen_lt(struct llog_gen a, struct llog_gen b)
226 if (a.mnt_cnt < b.mnt_cnt)
228 if (a.mnt_cnt > b.mnt_cnt)
230 return(a.conn_cnt < b.conn_cnt ? 1 : 0);
233 #define LLOG_GEN_INC(gen) ((gen).conn_cnt) ++
234 #define LLOG_PROC_BREAK 0x0001
236 static inline int llog_ctxt2ops(struct llog_ctxt *ctxt,
237 struct llog_operations **lop)
242 *lop = ctxt->loc_logops;
249 static inline int llog_handle2ops(struct llog_handle *loghandle,
250 struct llog_operations **lop)
252 if (loghandle == NULL)
255 return llog_ctxt2ops(loghandle->lgh_ctxt, lop);
258 static inline int llog_data_len(int len)
260 return size_round(len);
263 static inline struct llog_ctxt *llog_get_context(struct obd_device *obd,
266 if (index < 0 || index >= LLOG_MAX_CTXTS)
269 return obd->obd_llog_ctxt[index];
272 static inline int llog_create(struct llog_ctxt *ctxt, struct llog_handle **res,
273 struct llog_logid *logid, char *name)
275 struct llog_operations *lop;
279 rc = llog_ctxt2ops(ctxt, &lop);
282 if (lop->lop_create == NULL)
285 rc = lop->lop_create(ctxt, res, logid, name);
289 static inline int llog_destroy(struct llog_handle *handle)
291 struct llog_operations *lop;
295 rc = llog_handle2ops(handle, &lop);
298 if (lop->lop_destroy == NULL)
301 rc = lop->lop_destroy(handle);
305 static inline int llog_read_header(struct llog_handle *handle)
307 struct llog_operations *lop;
311 rc = llog_handle2ops(handle, &lop);
314 if (lop->lop_read_header == NULL)
317 rc = lop->lop_read_header(handle);
321 static inline int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
322 void *buf, struct llog_cookie *logcookies,
323 int numcookies, void *data)
325 struct llog_operations *lop;
329 rc = llog_ctxt2ops(ctxt, &lop);
332 if (lop->lop_add == NULL)
335 rc = lop->lop_add(ctxt, rec, buf, logcookies, numcookies, data);
339 static inline int llog_cancel(struct llog_ctxt *ctxt, int count,
340 struct llog_cookie *cookies, int flags, void *data)
342 struct llog_operations *lop;
346 rc = llog_ctxt2ops(ctxt, &lop);
349 if (lop->lop_cancel == NULL)
352 rc = lop->lop_cancel(ctxt, count, cookies, flags, data);
356 static inline int llog_write_rec(struct llog_handle *handle,
357 struct llog_rec_hdr *rec,
358 struct llog_cookie *logcookies,
359 int numcookies, void *buf, int idx)
361 struct llog_operations *lop;
365 rc = llog_handle2ops(handle, &lop);
368 if (lop->lop_write_rec == NULL)
372 buflen = le32_to_cpu(rec->lrh_len) + sizeof(struct llog_rec_hdr)
373 + sizeof(struct llog_rec_tail);
375 buflen = le32_to_cpu(rec->lrh_len);
376 LASSERT(size_round(buflen) == buflen);
378 rc = lop->lop_write_rec(handle, rec, logcookies, numcookies, buf, idx);
382 static inline int llog_next_block(struct llog_handle *loghandle, int *curr_idx,
383 int next_idx, __u64 *curr_offset, void *buf,
386 struct llog_operations *lop;
390 rc = llog_handle2ops(loghandle, &lop);
393 if (lop->lop_next_block == NULL)
396 rc = lop->lop_next_block(loghandle, curr_idx, next_idx, curr_offset, buf,
401 static inline int llog_prev_block(struct llog_handle *loghandle,
402 int prev_idx, void *buf, int len)
404 struct llog_operations *lop;
408 rc = llog_handle2ops(loghandle, &lop);
411 if (lop->lop_prev_block == NULL)
414 rc = lop->lop_prev_block(loghandle, prev_idx, buf, len);
418 static inline int llog_connect(struct llog_ctxt *ctxt, int count,
419 struct llog_logid *logid, struct llog_gen *gen,
420 struct obd_uuid *uuid)
422 struct llog_operations *lop;
426 rc = llog_ctxt2ops(ctxt, &lop);
429 if (lop->lop_connect == NULL)
432 rc = lop->lop_connect(ctxt, count, logid, gen, uuid);
436 static inline int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
438 struct llog_operations *lop;
442 rc = llog_ctxt2ops(ctxt, &lop);
445 if (lop->lop_sync == NULL)
448 rc = lop->lop_sync(ctxt, exp);