Whamcloud - gitweb
remove the lustre book-building bits from Makefile.am, in preparation for
[fs/lustre-release.git] / lustre / include / linux / lustre_log.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001 Cluster File Systems, Inc. <info@clusterfs.com>
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
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.
11  *
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.
16  *
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.
20  *
21  * Generic infrastructure for managing a collection of logs.
22  *
23  * These logs are used for:
24  *
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
28  *
29  * - mds unlink log: the MDS adds an entry upon delete
30  *
31  * - raid1 replication log between OST's
32  * - MDS replication logs
33  */
34
35 #ifndef _LUSTRE_LOG_H
36 #define _LUSTRE_LOG_H
37
38 #include <linux/obd.h>
39 #include <linux/lustre_idl.h>
40
41 #define LOG_NAME_LIMIT(logname, name)                   \
42         snprintf(logname, sizeof(logname), "LOGS/%s", name)
43 #define LLOG_EEMPTY 4711
44
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 */
49         int                 phd_last_idx;
50 };
51
52 struct cat_handle_data {
53         struct list_head        chd_head;
54         struct llog_handle     *chd_current_log; /* currently open log */
55 };
56
57 /* In-memory descriptor for a log object or log catalog */
58 struct llog_handle {
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;
63         int                     lgh_last_idx;
64         struct llog_ctxt       *lgh_ctxt;
65         union {
66                 struct plain_handle_data phd;
67                 struct cat_handle_data   chd;
68         } u;
69 };
70
71 /* llog.c  -  general API */
72 typedef int (*llog_cb_t)(struct llog_handle *, struct llog_rec_hdr *, void *);
73 int llog_init_handle(struct llog_handle *handle, int flags,
74                      struct obd_uuid *uuid);
75 int llog_process(struct llog_handle *loghandle, llog_cb_t cb,
76                  void *data, void *catdata);
77 extern struct llog_handle *llog_alloc_handle(void);
78 extern void llog_free_handle(struct llog_handle *handle);
79 extern int llog_close(struct llog_handle *cathandle);
80 extern int llog_cancel_rec(struct llog_handle *loghandle, int index);
81
82 /* llog_cat.c   -  catalog api */
83 struct llog_process_data {
84         void *lpd_data;
85         llog_cb_t lpd_cb;
86 };
87
88 struct llog_process_cat_data {
89         int     first_idx;
90         int     last_idx;
91         /* to process catalog across zero record */
92 };
93
94 int llog_cat_put(struct llog_handle *cathandle);
95 int llog_cat_add_rec(struct llog_handle *cathandle, struct llog_rec_hdr *rec,
96                      struct llog_cookie *reccookie, void *buf);
97 int llog_cat_cancel_records(struct llog_handle *cathandle, int count,
98                             struct llog_cookie *cookies);
99 int llog_cat_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data);
100 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index);
101
102 /* llog_obd.c */
103 int llog_setup(struct obd_device *obd, int index, struct obd_device *disk_obd,
104                int count,  struct llog_logid *logid,struct llog_operations *op);
105 int llog_cleanup(struct llog_ctxt *);
106 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp);
107 int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
108              struct lov_stripe_md *lsm, struct llog_cookie *logcookies,
109              int numcookies);
110 int llog_cancel(struct llog_ctxt *, struct lov_stripe_md *lsm,
111                 int count, struct llog_cookie *cookies, int flags);
112
113 int llog_obd_origin_setup(struct obd_device *obd, int index,
114                           struct obd_device *disk_obd, int count,
115                           struct llog_logid *logid);
116 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt);
117 int llog_obd_origin_add(struct llog_ctxt *ctxt,
118                         struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
119                         struct llog_cookie *logcookies, int numcookies);
120
121 int llog_cat_initialize(struct obd_device *obd, int count);
122 int obd_llog_init(struct obd_device *obd, struct obd_device *disk_obd,
123                   int count, struct llog_catid *logid);
124
125 int obd_llog_finish(struct obd_device *obd, int count);
126
127 /* llog_ioctl.c */
128 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data);
129 int llog_catalog_list(struct obd_device *obd, int count,
130                       struct obd_ioctl_data *data);
131
132 /* llog_net.c */
133 int llog_initiator_connect(struct llog_ctxt *ctxt);
134 int llog_receptor_accept(struct llog_ctxt *ctxt, struct obd_import *imp);
135 int llog_origin_connect(struct llog_ctxt *ctxt, int count,
136                         struct llog_logid *logid, struct llog_gen *gen,
137                         struct obd_uuid *uuid);
138 int llog_handle_connect(struct ptlrpc_request *req);
139
140 /* recov_thread.c */
141 int llog_obd_repl_cancel(struct llog_ctxt *ctxt,
142                          struct lov_stripe_md *lsm, int count,
143                          struct llog_cookie *cookies, int flags);
144 int llog_obd_repl_sync(struct llog_ctxt *ctxt, struct obd_export *exp);
145 int llog_repl_connect(struct llog_ctxt *ctxt, int count,
146                       struct llog_logid *logid, struct llog_gen *gen,
147                       struct obd_uuid *uuid);
148
149 struct llog_operations {
150         int (*lop_write_rec)(struct llog_handle *loghandle,
151                              struct llog_rec_hdr *rec,
152                              struct llog_cookie *logcookies, int numcookies,
153                              void *, int idx);
154         int (*lop_destroy)(struct llog_handle *handle);
155         int (*lop_next_block)(struct llog_handle *h, int *curr_idx,
156                               int next_idx, __u64 *offset, void *buf, int len);
157         int (*lop_create)(struct llog_ctxt *ctxt, struct llog_handle **,
158                           struct llog_logid *logid, char *name);
159         int (*lop_close)(struct llog_handle *handle);
160         int (*lop_read_header)(struct llog_handle *handle);
161
162         int (*lop_setup)(struct obd_device *obd, int ctxt_idx,
163                          struct obd_device *disk_obd, int count,
164                          struct llog_logid *logid);
165         int (*lop_sync)(struct llog_ctxt *ctxt, struct obd_export *exp);
166         int (*lop_cleanup)(struct llog_ctxt *ctxt);
167         int (*lop_add)(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
168                        struct lov_stripe_md *lsm,
169                        struct llog_cookie *logcookies, int numcookies);
170         int (*lop_cancel)(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
171                           int count, struct llog_cookie *cookies, int flags);
172         int (*lop_connect)(struct llog_ctxt *ctxt, int count,
173                            struct llog_logid *logid, struct llog_gen *gen,
174                            struct obd_uuid *uuid);
175         /* XXX add 2 more: commit callbacks and llog recovery functions */
176 };
177
178 /* llog_lvfs.c */
179 extern struct llog_operations llog_lvfs_ops;
180 int llog_get_cat_list(struct obd_device *obd, struct obd_device *disk_obd,
181                       char *name, int count, struct llog_catid *idarray);
182
183 struct llog_ctxt {
184         int                      loc_idx; /* my index the obd array of ctxt's */
185         struct llog_gen          loc_gen;
186         struct obd_device       *loc_obd; /* points back to the containing obd*/
187         struct obd_export       *loc_exp;
188         struct obd_import       *loc_imp; /* to use in RPC's: can be backward
189                                              pointing import */
190         struct llog_operations  *loc_logops;
191         struct llog_handle      *loc_handle;
192         struct llog_canceld_ctxt *loc_llcd;
193         struct semaphore         loc_sem; /* protects loc_llcd and loc_imp */
194         void                    *llog_proc_cb;
195 };
196
197 static inline void llog_gen_init(struct llog_ctxt *ctxt)
198 {
199         struct obd_device *obd = ctxt->loc_exp->exp_obd;
200
201         if (!strcmp(obd->obd_type->typ_name, "mds"))
202                 ctxt->loc_gen.mnt_cnt = obd->u.mds.mds_mount_count;
203         else if (!strstr(obd->obd_type->typ_name, "filter"))
204                 ctxt->loc_gen.mnt_cnt = obd->u.filter.fo_mount_count;
205         else
206                 ctxt->loc_gen.mnt_cnt = 0;
207 }
208
209 static inline int llog_gen_lt(struct llog_gen a, struct llog_gen b)
210 {
211         if (a.mnt_cnt < b.mnt_cnt)
212                 return 1;
213         if (a.mnt_cnt > b.mnt_cnt)
214                 return 0;
215         return(a.conn_cnt < b.conn_cnt ? 1 : 0);
216 }
217
218 #define LLOG_GEN_INC(gen)  ((gen).conn_cnt) ++
219 #define LLOG_PROC_BREAK 0x0001
220
221 static inline int llog_obd2ops(struct llog_ctxt *ctxt,
222                                struct llog_operations **lop)
223 {
224        if (ctxt == NULL)
225                 return -ENOTCONN;
226
227         *lop = ctxt->loc_logops;
228         if (*lop == NULL)
229                 return -EOPNOTSUPP;
230
231         return 0;
232 }
233
234 static inline int llog_handle2ops(struct llog_handle *loghandle,
235                                   struct llog_operations **lop)
236 {
237         if (loghandle == NULL)
238                 return -EINVAL;
239
240         return llog_obd2ops(loghandle->lgh_ctxt, lop);
241 }
242
243 static inline int llog_data_len(int len)
244 {
245         return size_round(len);
246 }
247
248 static inline struct llog_ctxt *llog_get_context(struct obd_device *obd,
249                                                  int index)
250 {
251         if (index < 0 || index >= LLOG_MAX_CTXTS)
252                 return NULL;
253
254         return obd->obd_llog_ctxt[index];
255 }
256
257 static inline int llog_write_rec(struct llog_handle *handle,
258                                  struct llog_rec_hdr *rec,
259                                  struct llog_cookie *logcookies,
260                                  int numcookies, void *buf, int idx)
261 {
262         struct llog_operations *lop;
263         int rc, buflen;
264         ENTRY;
265
266         rc = llog_handle2ops(handle, &lop);
267         if (rc)
268                 RETURN(rc);
269         if (lop->lop_write_rec == NULL)
270                 RETURN(-EOPNOTSUPP);
271
272         if (buf)
273                 buflen = rec->lrh_len + sizeof(struct llog_rec_hdr)
274                                 + sizeof(struct llog_rec_tail);
275         else
276                 buflen = rec->lrh_len;
277         LASSERT(size_round(buflen) == buflen);
278
279         rc = lop->lop_write_rec(handle, rec, logcookies, numcookies, buf, idx);
280         RETURN(rc);
281 }
282
283 static inline int llog_read_header(struct llog_handle *handle)
284 {
285         struct llog_operations *lop;
286         int rc;
287         ENTRY;
288
289         rc = llog_handle2ops(handle, &lop);
290         if (rc)
291                 RETURN(rc);
292         if (lop->lop_read_header == NULL)
293                 RETURN(-EOPNOTSUPP);
294
295         rc = lop->lop_read_header(handle);
296         RETURN(rc);
297 }
298
299 static inline int llog_destroy(struct llog_handle *handle)
300 {
301         struct llog_operations *lop;
302         int rc;
303         ENTRY;
304
305         rc = llog_handle2ops(handle, &lop);
306         if (rc)
307                 RETURN(rc);
308         if (lop->lop_destroy == NULL)
309                 RETURN(-EOPNOTSUPP);
310
311         rc = lop->lop_destroy(handle);
312         RETURN(rc);
313 }
314
315 #if 0
316 static inline int llog_cancel(struct obd_export *exp,
317                               struct lov_stripe_md *lsm, int count,
318                               struct llog_cookie *cookies, int flags)
319 {
320         struct llog_operations *lop;
321         int rc;
322         ENTRY;
323
324         rc = llog_handle2ops(loghandle, &lop);
325         if (rc)
326                 RETURN(rc);
327         if (lop->lop_cancel == NULL)
328                 RETURN(-EOPNOTSUPP);
329
330         rc = lop->lop_cancel(exp, lsm, count, cookies, flags);
331         RETURN(rc);
332 }
333 #endif
334
335 static inline int llog_next_block(struct llog_handle *loghandle, int *cur_idx,
336                                   int next_idx, __u64 *cur_offset, void *buf,
337                                   int len)
338 {
339         struct llog_operations *lop;
340         int rc;
341         ENTRY;
342
343         rc = llog_handle2ops(loghandle, &lop);
344         if (rc)
345                 RETURN(rc);
346         if (lop->lop_next_block == NULL)
347                 RETURN(-EOPNOTSUPP);
348
349         rc = lop->lop_next_block(loghandle, cur_idx, next_idx, cur_offset, buf,
350                                  len);
351         RETURN(rc);
352 }
353
354 static inline int llog_create(struct llog_ctxt *ctxt, struct llog_handle **res,
355                               struct llog_logid *logid, char *name)
356 {
357         struct llog_operations *lop;
358         int rc;
359         ENTRY;
360
361         rc = llog_obd2ops(ctxt, &lop);
362         if (rc)
363                 RETURN(rc);
364         if (lop->lop_create == NULL)
365                 RETURN(-EOPNOTSUPP);
366
367         rc = lop->lop_create(ctxt, res, logid, name);
368         RETURN(rc);
369 }
370
371 static inline int llog_connect(struct llog_ctxt *ctxt, int count,
372                                struct llog_logid *logid, struct llog_gen *gen,
373                                struct obd_uuid *uuid)
374 {
375         struct llog_operations *lop;
376         int rc;
377         ENTRY;
378
379         rc = llog_obd2ops(ctxt, &lop);
380         if (rc)
381                 RETURN(rc);
382         if (lop->lop_connect == NULL)
383                 RETURN(-EOPNOTSUPP);
384
385         rc = lop->lop_connect(ctxt, count, logid, gen, uuid);
386         RETURN(rc);
387 }
388
389 #endif