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 /* got from mds_update_record.
74 * FIXME: maybe some attribute in reint_record and update_record will be
76 /* XXX BUG 3188 -- must return to one set of structures. */
77 /* XXX use fixed-sized fields (__u32) instead of dev_t and iattr->gid_t, etc */
79 struct update_record {
84 struct iattr ur_iattr;
85 struct iattr ur_pattr;
91 struct update_record u_rec;
98 struct llog_smfs_rec {
99 struct llog_rec_hdr lsr_hdr;
100 struct update_record lsr_rec;
101 struct llog_rec_tail lsr_tail;
104 /* llog.c - general API */
105 typedef int (*llog_cb_t)(struct llog_handle *, struct llog_rec_hdr *, void *);
106 struct llog_handle *llog_alloc_handle(void);
107 void llog_free_handle(struct llog_handle *handle);
108 int llog_cancel_rec(struct llog_handle *loghandle, int index);
109 int llog_init_handle(struct llog_handle *handle, int flags,
110 struct obd_uuid *uuid);
111 int llog_close(struct llog_handle *cathandle);
112 int llog_process(struct llog_handle *loghandle, llog_cb_t cb,
113 void *data, void *catdata);
114 int llog_reverse_process(struct llog_handle *loghandle, llog_cb_t cb,
115 void *data, void *catdata);
117 /* llog_cat.c - catalog api */
118 struct llog_process_data {
123 struct llog_process_cat_data {
126 /* to process catalog across zero record */
129 int llog_cat_id2handle(struct llog_handle *cathandle, struct llog_handle **res,
130 struct llog_logid *logid);
131 int llog_cat_put(struct llog_handle *cathandle);
132 int llog_cat_add_rec(struct llog_handle *cathandle, struct llog_rec_hdr *rec,
133 struct llog_cookie *reccookie, void *buf);
134 int llog_cat_cancel_records(struct llog_handle *cathandle, int count,
135 struct llog_cookie *cookies);
136 int llog_cat_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data);
137 int llog_cat_reverse_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data);
138 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index);
141 int llog_catalog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
142 void *buf, struct llog_cookie *reccookie, int, void *data);
143 int llog_catalog_cancel(struct llog_ctxt *ctxt, int count, struct llog_cookie *,
144 int flags, void *data);
145 int llog_catalog_setup(struct llog_ctxt **res, char *name, struct obd_export *exp,
146 struct lvfs_run_ctxt *, struct fsfilt_operations *fsops,
147 struct dentry *logs_de, struct dentry *objects_de);
148 int llog_catalog_cleanup(struct llog_ctxt *ctxt);
149 int llog_cat_half_bottom(struct llog_cookie *, struct llog_handle *);
152 int llog_get_cat_list(struct lvfs_run_ctxt *, struct fsfilt_operations *,
153 char *name, int count, struct llog_catid *idarray);
154 int llog_put_cat_list(struct lvfs_run_ctxt *, struct fsfilt_operations *,
155 char *name, int count, struct llog_catid *idarray);
156 extern struct llog_operations llog_lvfs_ops;
158 int llog_obd_origin_setup(struct obd_device *, struct obd_llogs *, int,
159 struct obd_device *, int, struct llog_logid *);
160 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt);
161 int llog_obd_origin_add(struct llog_ctxt *ctxt,
162 struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
163 struct llog_cookie *logcookies, int numcookies);
165 int obd_llog_cat_initialize(struct obd_device *, struct obd_llogs *, int, char *);
167 /* llog_obd.c - obd llog api */
168 int obd_llog_setup(struct obd_device *obd, struct obd_llogs *, int index,
169 struct obd_device *disk_obd, int count, struct llog_logid *logid,
170 struct llog_operations *op);
171 int obd_llog_init(struct obd_device *, struct obd_llogs *, struct obd_device *,
172 int, struct llog_catid *);
173 int obd_llog_cleanup(struct llog_ctxt *);
175 int obd_llog_finish(struct obd_device *, struct obd_llogs *, int);
178 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data);
179 int llog_catalog_list(struct obd_device *obd, int count,
180 struct obd_ioctl_data *data);
183 int llog_initiator_connect(struct llog_ctxt *ctxt);
184 int llog_receptor_accept(struct llog_ctxt *ctxt, struct obd_import *imp);
185 int llog_origin_connect(struct llog_ctxt *ctxt, int count,
186 struct llog_logid *logid, struct llog_gen *gen,
187 struct obd_uuid *uuid);
188 int llog_handle_connect(struct ptlrpc_request *req);
191 int llog_obd_repl_cancel(struct llog_ctxt *ctxt, int count,
192 struct llog_cookie *cookies, int flags, void *data);
194 int llog_obd_repl_sync(struct llog_ctxt *ctxt, struct obd_export *exp);
195 int llog_repl_connect(struct llog_ctxt *ctxt, int count,
196 struct llog_logid *logid, struct llog_gen *gen,
197 struct obd_uuid *uuid);
199 struct llog_operations {
200 int (*lop_setup)(struct obd_device *, struct obd_llogs *, int,
201 struct obd_device *, int, struct llog_logid *);
203 int (*lop_cleanup)(struct llog_ctxt *ctxt);
204 int (*lop_create)(struct llog_ctxt *ctxt, struct llog_handle **,
205 struct llog_logid *logid, char *name);
206 int (*lop_destroy)(struct llog_handle *handle);
207 int (*lop_close)(struct llog_handle *handle);
209 int (*lop_read_header)(struct llog_handle *handle);
210 int (*lop_add)(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
211 void *buf, struct llog_cookie *logcookies,
212 int numcookies, void *data);
213 int (*lop_cancel)(struct llog_ctxt *ctxt, int count,
214 struct llog_cookie *cookies, int flags, void *data);
215 int (*lop_write_rec)(struct llog_handle *loghandle,
216 struct llog_rec_hdr *rec,
217 struct llog_cookie *logcookies, int numcookies,
219 int (*lop_next_block)(struct llog_handle *h, int *curr_idx,
220 int next_idx, __u64 *offset, void *buf, int len);
221 int (*lop_prev_block)(struct llog_handle *h,
222 int prev_idx, void *buf, int len);
224 int (*lop_sync)(struct llog_ctxt *ctxt, struct obd_export *exp);
225 int (*lop_connect)(struct llog_ctxt *ctxt, int count,
226 struct llog_logid *logid, struct llog_gen *gen,
227 struct obd_uuid *uuid);
231 /* needed for lvfs based log */
232 struct llog_handle *loc_handle;
233 struct llog_operations *loc_logops;
234 struct fsfilt_operations *loc_fsops;
235 struct dentry *loc_logs_dir;
236 struct dentry *loc_objects_dir;
237 struct lvfs_run_ctxt *loc_lvfs_ctxt;
239 struct obd_device *loc_obd; /* points back to the containing obd */
240 struct llog_gen loc_gen;
241 int loc_idx; /* my index the obd array of ctxt's */
242 int loc_alone; /* is this llog ctxt has an obd? */
244 struct obd_export *loc_exp;
245 struct obd_import *loc_imp; /* to use in RPC's: can be backward
247 struct llog_canceld_ctxt *loc_llcd;
248 struct semaphore loc_sem; /* protects loc_llcd */
250 struct obd_llogs *loc_llogs;
253 static inline void llog_gen_init(struct llog_ctxt *ctxt)
255 struct obd_device *obd = ctxt->loc_exp->exp_obd;
257 if (!strcmp(obd->obd_type->typ_name, "mds"))
258 ctxt->loc_gen.mnt_cnt = obd->u.mds.mds_mount_count;
259 else if (!strstr(obd->obd_type->typ_name, "filter"))
260 ctxt->loc_gen.mnt_cnt = obd->u.filter.fo_mount_count;
262 ctxt->loc_gen.mnt_cnt = 0;
265 static inline int llog_gen_lt(struct llog_gen a, struct llog_gen b)
267 if (a.mnt_cnt < b.mnt_cnt)
269 if (a.mnt_cnt > b.mnt_cnt)
271 return(a.conn_cnt < b.conn_cnt ? 1 : 0);
274 #define LLOG_GEN_INC(gen) ((gen).conn_cnt) ++
275 #define LLOG_PROC_BREAK 0x0001
276 #define LLOG_DEL_RECORD 0x0002
278 static inline int llog_ctxt2ops(struct llog_ctxt *ctxt,
279 struct llog_operations **lop)
284 *lop = ctxt->loc_logops;
291 static inline int llog_handle2ops(struct llog_handle *loghandle,
292 struct llog_operations **lop)
294 if (loghandle == NULL)
297 return llog_ctxt2ops(loghandle->lgh_ctxt, lop);
300 static inline int llog_data_len(int len)
302 return size_round(len);
305 static inline struct llog_ctxt *llog_get_context(struct obd_llogs *llogs,
308 if (index < 0 || index >= LLOG_MAX_CTXTS)
311 return llogs->llog_ctxt[index];
314 static inline int llog_create(struct llog_ctxt *ctxt, struct llog_handle **res,
315 struct llog_logid *logid, char *name)
317 struct llog_operations *lop;
321 rc = llog_ctxt2ops(ctxt, &lop);
324 if (lop->lop_create == NULL)
327 rc = lop->lop_create(ctxt, res, logid, name);
331 static inline int llog_destroy(struct llog_handle *handle)
333 struct llog_operations *lop;
337 rc = llog_handle2ops(handle, &lop);
340 if (lop->lop_destroy == NULL)
343 rc = lop->lop_destroy(handle);
347 static inline int llog_read_header(struct llog_handle *handle)
349 struct llog_operations *lop;
353 rc = llog_handle2ops(handle, &lop);
356 if (lop->lop_read_header == NULL)
359 rc = lop->lop_read_header(handle);
363 static inline int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
364 void *buf, struct llog_cookie *logcookies,
365 int numcookies, void *data)
367 struct llog_operations *lop;
371 rc = llog_ctxt2ops(ctxt, &lop);
374 if (lop->lop_add == NULL)
377 rc = lop->lop_add(ctxt, rec, buf, logcookies, numcookies, data);
381 static inline int llog_cancel(struct llog_ctxt *ctxt, int count,
382 struct llog_cookie *cookies, int flags, void *data)
384 struct llog_operations *lop;
388 rc = llog_ctxt2ops(ctxt, &lop);
391 if (lop->lop_cancel == NULL)
394 rc = lop->lop_cancel(ctxt, count, cookies, flags, data);
398 static inline int llog_write_rec(struct llog_handle *handle,
399 struct llog_rec_hdr *rec,
400 struct llog_cookie *logcookies,
401 int numcookies, void *buf, int idx)
403 struct llog_operations *lop;
407 rc = llog_handle2ops(handle, &lop);
410 if (lop->lop_write_rec == NULL)
414 buflen = le32_to_cpu(rec->lrh_len) + sizeof(struct llog_rec_hdr)
415 + sizeof(struct llog_rec_tail);
417 buflen = le32_to_cpu(rec->lrh_len);
418 LASSERT(size_round(buflen) == buflen);
420 rc = lop->lop_write_rec(handle, rec, logcookies, numcookies, buf, idx);
424 static inline int llog_next_block(struct llog_handle *loghandle, int *curr_idx,
425 int next_idx, __u64 *curr_offset, void *buf,
428 struct llog_operations *lop;
432 rc = llog_handle2ops(loghandle, &lop);
435 if (lop->lop_next_block == NULL)
438 rc = lop->lop_next_block(loghandle, curr_idx, next_idx, curr_offset, buf,
443 static inline int llog_prev_block(struct llog_handle *loghandle,
444 int prev_idx, void *buf, int len)
446 struct llog_operations *lop;
450 rc = llog_handle2ops(loghandle, &lop);
453 if (lop->lop_prev_block == NULL)
456 rc = lop->lop_prev_block(loghandle, prev_idx, buf, len);
460 static inline int llog_connect(struct llog_ctxt *ctxt, int count,
461 struct llog_logid *logid, struct llog_gen *gen,
462 struct obd_uuid *uuid)
464 struct llog_operations *lop;
468 rc = llog_ctxt2ops(ctxt, &lop);
471 if (lop->lop_connect == NULL)
474 rc = lop->lop_connect(ctxt, count, logid, gen, uuid);
478 static inline int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
480 struct llog_operations *lop;
484 rc = llog_ctxt2ops(ctxt, &lop);
487 if (lop->lop_sync == NULL)
490 rc = lop->lop_sync(ctxt, exp);