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 /* got from mds_update_record.
72 * FIXME: maybe some attribute in reint_record and update_record will be
74 /* XXX BUG 3188 -- must return to one set of structures. */
75 /* XXX use fixed-sized fields (__u32) instead of dev_t and iattr->gid_t, etc */
77 struct update_record {
82 struct iattr ur_iattr;
83 struct iattr ur_pattr;
89 struct update_record u_rec;
96 struct llog_smfs_rec {
97 struct llog_rec_hdr lsr_hdr;
98 struct update_record lsr_rec;
99 struct llog_rec_tail lsr_tail;
102 /* llog.c - general API */
103 typedef int (*llog_cb_t)(struct llog_handle *, struct llog_rec_hdr *, void *);
104 struct llog_handle *llog_alloc_handle(void);
105 void llog_free_handle(struct llog_handle *handle);
106 int llog_cancel_rec(struct llog_handle *loghandle, int index);
107 int llog_init_handle(struct llog_handle *handle, int flags,
108 struct obd_uuid *uuid);
109 int llog_close(struct llog_handle *cathandle);
110 int llog_process(struct llog_handle *loghandle, llog_cb_t cb,
111 void *data, void *catdata);
112 int llog_reverse_process(struct llog_handle *loghandle, llog_cb_t cb,
113 void *data, void *catdata);
115 /* llog_cat.c - catalog api */
116 struct llog_process_data {
121 struct llog_process_cat_data {
124 /* to process catalog across zero record */
127 int llog_cat_id2handle(struct llog_handle *cathandle, struct llog_handle **res,
128 struct llog_logid *logid);
129 int llog_cat_put(struct llog_handle *cathandle);
130 int llog_cat_add_rec(struct llog_handle *cathandle, struct llog_rec_hdr *rec,
131 struct llog_cookie *reccookie, void *buf);
132 int llog_cat_cancel_records(struct llog_handle *cathandle, int count,
133 struct llog_cookie *cookies);
134 int llog_cat_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data);
135 int llog_cat_reverse_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data);
136 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index);
138 int llog_catalog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
139 void *buf, struct llog_cookie *reccookie, int, void *data);
140 int llog_catalog_cancel(struct llog_ctxt *ctxt, int count, struct llog_cookie *,
141 int flags, void *data);
142 int llog_catalog_setup(struct llog_ctxt **res, char *name, struct lvfs_run_ctxt *,
143 struct fsfilt_operations *fsops, struct dentry *logs_de,
144 struct dentry *objects_de);
145 int llog_catalog_cleanup(struct llog_ctxt *ctxt);
146 int llog_cat_half_bottom(struct llog_cookie *, struct llog_handle *);
149 int llog_get_cat_list(struct lvfs_run_ctxt *, struct fsfilt_operations *,
150 char *name, int count, struct llog_catid *idarray);
151 int llog_put_cat_list(struct lvfs_run_ctxt *, struct fsfilt_operations *,
152 char *name, int count, struct llog_catid *idarray);
153 extern struct llog_operations llog_lvfs_ops;
155 /* llog_obd.c - obd llog api */
156 int obd_llog_setup(struct obd_device *obd, int index, struct obd_device *disk_obd,
157 int count, struct llog_logid *logid,struct llog_operations *op);
158 int obd_llog_cleanup(struct llog_ctxt *);
159 int llog_obd_origin_setup(struct obd_device *obd, int index,
160 struct obd_device *disk_obd, int count,
161 struct llog_logid *logid);
162 int obd_llog_cat_initialize(struct obd_device *obd, int count, char *name);
163 int obd_llog_init(struct obd_device *obd, struct obd_device *disk_obd,
164 int count, struct llog_catid *logid);
166 int obd_llog_finish(struct obd_device *obd, int count);
169 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data);
170 int llog_catalog_list(struct obd_device *obd, int count,
171 struct obd_ioctl_data *data);
174 int llog_initiator_connect(struct llog_ctxt *ctxt);
175 int llog_receptor_accept(struct llog_ctxt *ctxt, struct obd_import *imp);
176 int llog_origin_connect(struct llog_ctxt *ctxt, int count,
177 struct llog_logid *logid, struct llog_gen *gen,
178 struct obd_uuid *uuid);
179 int llog_handle_connect(struct ptlrpc_request *req);
182 int llog_obd_repl_cancel(struct llog_ctxt *ctxt, int count,
183 struct llog_cookie *cookies, int flags, void *data);
185 int llog_obd_repl_sync(struct llog_ctxt *ctxt, struct obd_export *exp);
186 int llog_repl_connect(struct llog_ctxt *ctxt, int count,
187 struct llog_logid *logid, struct llog_gen *gen,
188 struct obd_uuid *uuid);
190 struct llog_operations {
191 int (*lop_setup)(struct obd_device *obd, int ctxt_idx,
192 struct obd_device *disk_obd, int count,
193 struct llog_logid *logid);
194 int (*lop_cleanup)(struct llog_ctxt *ctxt);
195 int (*lop_create)(struct llog_ctxt *ctxt, struct llog_handle **,
196 struct llog_logid *logid, char *name);
197 int (*lop_destroy)(struct llog_handle *handle);
198 int (*lop_close)(struct llog_handle *handle);
200 int (*lop_read_header)(struct llog_handle *handle);
201 int (*lop_add)(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
202 void *buf, struct llog_cookie *logcookies,
203 int numcookies, void *data);
204 int (*lop_cancel)(struct llog_ctxt *ctxt, int count,
205 struct llog_cookie *cookies, int flags, void *data);
206 int (*lop_write_rec)(struct llog_handle *loghandle,
207 struct llog_rec_hdr *rec,
208 struct llog_cookie *logcookies, int numcookies,
210 int (*lop_next_block)(struct llog_handle *h, int *curr_idx,
211 int next_idx, __u64 *offset, void *buf, int len);
212 int (*lop_prev_block)(struct llog_handle *h,
213 int prev_idx, void *buf, int len);
215 /* XXX add 2 more: commit callbacks and llog recovery functions */
216 int (*lop_sync)(struct llog_ctxt *ctxt, struct obd_export *exp);
217 int (*lop_connect)(struct llog_ctxt *ctxt, int count,
218 struct llog_logid *logid, struct llog_gen *gen,
219 struct obd_uuid *uuid);
223 /* needed for lvfs based log */
224 struct llog_handle *loc_handle;
225 struct llog_operations *loc_logops;
226 struct fsfilt_operations *loc_fsops;
227 struct dentry *loc_logs_dir;
228 struct dentry *loc_objects_dir;
229 struct lvfs_run_ctxt *loc_lvfs_ctxt;
231 struct obd_device *loc_obd; /* points back to the containing obd*/
232 struct llog_gen loc_gen;
233 int loc_idx; /* my index the obd array of ctxt's */
235 struct obd_export *loc_exp;
236 struct obd_import *loc_imp; /* to use in RPC's: can be backward
238 struct llog_canceld_ctxt *loc_llcd;
239 struct semaphore loc_sem; /* protects loc_llcd */
240 void *loc_proc_cb; /* cb for recovery */
243 static inline void llog_gen_init(struct llog_ctxt *ctxt)
245 struct obd_device *obd = ctxt->loc_exp->exp_obd;
247 if (!strcmp(obd->obd_type->typ_name, "mds"))
248 ctxt->loc_gen.mnt_cnt = obd->u.mds.mds_mount_count;
249 else if (!strstr(obd->obd_type->typ_name, "filter"))
250 ctxt->loc_gen.mnt_cnt = obd->u.filter.fo_mount_count;
252 ctxt->loc_gen.mnt_cnt = 0;
255 static inline int llog_gen_lt(struct llog_gen a, struct llog_gen b)
257 if (a.mnt_cnt < b.mnt_cnt)
259 if (a.mnt_cnt > b.mnt_cnt)
261 return(a.conn_cnt < b.conn_cnt ? 1 : 0);
264 #define LLOG_GEN_INC(gen) ((gen).conn_cnt) ++
265 #define LLOG_PROC_BREAK 0x0001
267 static inline int llog_ctxt2ops(struct llog_ctxt *ctxt,
268 struct llog_operations **lop)
273 *lop = ctxt->loc_logops;
280 static inline int llog_handle2ops(struct llog_handle *loghandle,
281 struct llog_operations **lop)
283 if (loghandle == NULL)
286 return llog_ctxt2ops(loghandle->lgh_ctxt, lop);
289 static inline int llog_data_len(int len)
291 return size_round(len);
294 static inline struct llog_ctxt *llog_get_context(struct obd_device *obd,
297 if (index < 0 || index >= LLOG_MAX_CTXTS)
300 return obd->obd_llog_ctxt[index];
303 static inline int llog_create(struct llog_ctxt *ctxt, struct llog_handle **res,
304 struct llog_logid *logid, char *name)
306 struct llog_operations *lop;
310 rc = llog_ctxt2ops(ctxt, &lop);
313 if (lop->lop_create == NULL)
316 rc = lop->lop_create(ctxt, res, logid, name);
320 static inline int llog_destroy(struct llog_handle *handle)
322 struct llog_operations *lop;
326 rc = llog_handle2ops(handle, &lop);
329 if (lop->lop_destroy == NULL)
332 rc = lop->lop_destroy(handle);
336 static inline int llog_read_header(struct llog_handle *handle)
338 struct llog_operations *lop;
342 rc = llog_handle2ops(handle, &lop);
345 if (lop->lop_read_header == NULL)
348 rc = lop->lop_read_header(handle);
352 static inline int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
353 void *buf, struct llog_cookie *logcookies,
354 int numcookies, void *data)
356 struct llog_operations *lop;
360 rc = llog_ctxt2ops(ctxt, &lop);
363 if (lop->lop_add == NULL)
366 rc = lop->lop_add(ctxt, rec, buf, logcookies, numcookies, data);
370 static inline int llog_cancel(struct llog_ctxt *ctxt, int count,
371 struct llog_cookie *cookies, int flags, void *data)
373 struct llog_operations *lop;
377 rc = llog_ctxt2ops(ctxt, &lop);
380 if (lop->lop_cancel == NULL)
383 rc = lop->lop_cancel(ctxt, count, cookies, flags, data);
387 static inline int llog_write_rec(struct llog_handle *handle,
388 struct llog_rec_hdr *rec,
389 struct llog_cookie *logcookies,
390 int numcookies, void *buf, int idx)
392 struct llog_operations *lop;
396 rc = llog_handle2ops(handle, &lop);
399 if (lop->lop_write_rec == NULL)
403 buflen = le32_to_cpu(rec->lrh_len) + sizeof(struct llog_rec_hdr)
404 + sizeof(struct llog_rec_tail);
406 buflen = le32_to_cpu(rec->lrh_len);
407 LASSERT(size_round(buflen) == buflen);
409 rc = lop->lop_write_rec(handle, rec, logcookies, numcookies, buf, idx);
413 static inline int llog_next_block(struct llog_handle *loghandle, int *curr_idx,
414 int next_idx, __u64 *curr_offset, void *buf,
417 struct llog_operations *lop;
421 rc = llog_handle2ops(loghandle, &lop);
424 if (lop->lop_next_block == NULL)
427 rc = lop->lop_next_block(loghandle, curr_idx, next_idx, curr_offset, buf,
432 static inline int llog_prev_block(struct llog_handle *loghandle,
433 int prev_idx, void *buf, int len)
435 struct llog_operations *lop;
439 rc = llog_handle2ops(loghandle, &lop);
442 if (lop->lop_prev_block == NULL)
445 rc = lop->lop_prev_block(loghandle, prev_idx, buf, len);
449 static inline int llog_connect(struct llog_ctxt *ctxt, int count,
450 struct llog_logid *logid, struct llog_gen *gen,
451 struct obd_uuid *uuid)
453 struct llog_operations *lop;
457 rc = llog_ctxt2ops(ctxt, &lop);
460 if (lop->lop_connect == NULL)
463 rc = lop->lop_connect(ctxt, count, logid, gen, uuid);
467 static inline int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
469 struct llog_operations *lop;
473 rc = llog_ctxt2ops(ctxt, &lop);
476 if (lop->lop_sync == NULL)
479 rc = lop->lop_sync(ctxt, exp);