Whamcloud - gitweb
ce42b652e62744370bf85974baaed908595e9b42
[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 obd_llogs;
46
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 */
51         int                 phd_last_idx;
52 };
53
54 struct cat_handle_data {
55         struct list_head        chd_head;
56         struct llog_handle     *chd_current_log; /* currently open log */
57 };
58
59 /* In-memory descriptor for a log object or log catalog */
60 struct llog_handle {
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;
65         int                     lgh_last_idx;
66         struct llog_ctxt       *lgh_ctxt;
67         union {
68                 struct plain_handle_data phd;
69                 struct cat_handle_data   chd;
70         } u;
71 };
72
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);
85
86 /* llog_cat.c   -  catalog api */
87 struct llog_process_data {
88         void *lpd_data;
89         llog_cb_t lpd_cb;
90 };
91
92 struct llog_process_cat_data {
93         int     first_idx;
94         int     last_idx;
95         /* to process catalog across zero record */
96 };
97
98 int llog_cat_id2handle(struct llog_handle *cathandle, struct llog_handle **res,
99                        struct llog_logid *logid);
100 int llog_cat_put(struct llog_handle *cathandle);
101 int llog_cat_add_rec(struct llog_handle *cathandle, struct llog_rec_hdr *rec,
102                      struct llog_cookie *reccookie, void *buf);
103 int llog_cat_cancel_records(struct llog_handle *cathandle, int count,
104                             struct llog_cookie *cookies);
105 int llog_cat_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data);
106 int llog_cat_reverse_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data);
107 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index);
108
109 /* llog_obd.c */
110 int llog_catalog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
111                      void *buf, struct llog_cookie *reccookie, int, void *data);
112 int llog_catalog_cancel(struct llog_ctxt *ctxt, int count, struct llog_cookie *,
113                         int flags, void *data);
114 int llog_catalog_setup(struct llog_ctxt **res, char *name, struct obd_export *exp,
115                        struct lvfs_run_ctxt *, struct fsfilt_operations *fsops, 
116                        struct dentry *logs_de, struct dentry *objects_de);
117 int llog_catalog_cleanup(struct llog_ctxt *ctxt);
118 int llog_cat_half_bottom(struct llog_cookie *, struct llog_handle *);
119
120 /* llog_lvfs.c */
121 int llog_get_cat_list(struct lvfs_run_ctxt *, struct fsfilt_operations *,
122                       char *name, int count, struct llog_catid *idarray);
123 int llog_put_cat_list(struct lvfs_run_ctxt *, struct fsfilt_operations *, 
124                       char *name, int count, struct llog_catid *idarray);
125 extern struct llog_operations llog_lvfs_ops;
126
127 int llog_obd_origin_setup(struct obd_device *, struct obd_llogs *, int,
128                           struct obd_device *, int, struct llog_logid *);
129 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt);
130 int llog_obd_origin_add(struct llog_ctxt *ctxt,
131                         struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
132                         struct llog_cookie *logcookies, int numcookies);
133
134 int obd_llog_cat_initialize(struct obd_device *, struct obd_llogs *, int, char *);
135
136 /* llog_obd.c - obd llog api */
137 int obd_llog_setup(struct obd_device *obd, struct obd_llogs *, int index, 
138                    struct obd_device *disk_obd, int count, struct llog_logid *logid,
139                    struct llog_operations *op);
140 int obd_llog_init(struct obd_device *, struct obd_llogs *, struct obd_device *,
141                   int, struct llog_catid *);
142 int obd_llog_cleanup(struct llog_ctxt *);
143
144 int obd_llog_finish(struct obd_device *, struct obd_llogs *, int);
145
146 /* llog_ioctl.c */
147 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data);
148 int llog_catalog_list(struct obd_device *obd, int count,
149                      struct obd_ioctl_data *data);
150
151 /* llog_net.c */
152 int llog_initiator_connect(struct llog_ctxt *ctxt);
153 int llog_receptor_accept(struct llog_ctxt *ctxt, struct obd_import *imp);
154 int llog_origin_connect(struct llog_ctxt *ctxt, int count,
155                         struct llog_logid *logid, struct llog_gen *gen,
156                         struct obd_uuid *uuid);
157 int llog_handle_connect(struct ptlrpc_request *req);
158
159 /* recov_thread.c */
160 int llog_obd_repl_cancel(struct llog_ctxt *ctxt, int count,
161                          struct llog_cookie *cookies, int flags, void *data);
162                          
163 int llog_obd_repl_sync(struct llog_ctxt *ctxt, struct obd_export *exp);
164 int llog_repl_connect(struct llog_ctxt *ctxt, int count,
165                       struct llog_logid *logid, struct llog_gen *gen,
166                       struct obd_uuid *uuid);
167
168 struct llog_operations {
169         int (*lop_setup)(struct obd_device *, struct obd_llogs *, int,
170                          struct obd_device *, int, struct llog_logid *);
171
172         int (*lop_cleanup)(struct llog_ctxt *ctxt);
173         int (*lop_open)(struct llog_ctxt *ctxt, struct llog_handle **,
174                         struct llog_logid *logid, char *name, int flags);
175         int (*lop_destroy)(struct llog_handle *handle);
176         int (*lop_close)(struct llog_handle *handle);
177
178         int (*lop_read_header)(struct llog_handle *handle);
179         int (*lop_add)(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
180                        void *buf, struct llog_cookie *logcookies,
181                        int numcookies, void *data);
182         int (*lop_cancel)(struct llog_ctxt *ctxt, int count,
183                           struct llog_cookie *cookies, int flags, void *data);
184         int (*lop_write_rec)(struct llog_handle *loghandle,
185                              struct llog_rec_hdr *rec,
186                              struct llog_cookie *logcookies, int numcookies,
187                              void *, int idx);
188         int (*lop_next_block)(struct llog_handle *h, int *curr_idx,
189                               int next_idx, __u64 *offset, void *buf, int len);
190         int (*lop_prev_block)(struct llog_handle *h,
191                               int prev_idx, void *buf, int len);
192
193         int (*lop_sync)(struct llog_ctxt *ctxt, struct obd_export *exp);
194         int (*lop_connect)(struct llog_ctxt *ctxt, int count,
195                            struct llog_logid *logid, struct llog_gen *gen,
196                            struct obd_uuid *uuid);
197 };
198
199 struct llog_ctxt {
200         /* needed for lvfs based log */
201         struct llog_handle      *loc_handle;
202         struct llog_operations  *loc_logops;
203         struct fsfilt_operations *loc_fsops;
204         struct dentry           *loc_logs_dir;
205         struct dentry           *loc_objects_dir;
206         struct lvfs_run_ctxt    *loc_lvfs_ctxt;
207
208         struct obd_device       *loc_obd;   /* points back to the containing obd */
209         struct llog_gen          loc_gen;
210         int                      loc_idx;   /* my index the obd array of ctxt's */
211         int                      loc_alone; /* is this llog ctxt has an obd? */
212
213         struct obd_export       *loc_exp;
214         struct obd_import       *loc_imp;   /* to use in RPC's: can be backward
215                                                pointing import */
216         struct llog_canceld_ctxt *loc_llcd;
217         struct semaphore         loc_sem;   /* protects loc_llcd */
218         void                    *llog_proc_cb;
219         struct obd_llogs        *loc_llogs;
220 };
221
222 static inline void llog_gen_init(struct llog_ctxt *ctxt)
223 {
224         struct obd_device *obd = ctxt->loc_exp->exp_obd;
225
226         if (!strcmp(obd->obd_type->typ_name, "mds"))
227                 ctxt->loc_gen.mnt_cnt = obd->u.mds.mds_mount_count;
228         else if (!strstr(obd->obd_type->typ_name, "filter"))
229                 ctxt->loc_gen.mnt_cnt = obd->u.filter.fo_mount_count;
230         else
231                 ctxt->loc_gen.mnt_cnt = 0;
232 }
233
234 static inline int llog_gen_lt(struct llog_gen a, struct llog_gen b)
235 {
236         if (a.mnt_cnt < b.mnt_cnt)
237                 return 1;
238         if (a.mnt_cnt > b.mnt_cnt)
239                 return 0;
240         return(a.conn_cnt < b.conn_cnt ? 1 : 0);
241 }
242
243 #define LLOG_GEN_INC(gen)  ((gen).conn_cnt) ++
244 #define LLOG_PROC_BREAK 0x0001
245 #define LLOG_DEL_RECORD 0x0002
246
247 static inline int llog_ctxt2ops(struct llog_ctxt *ctxt,
248                                struct llog_operations **lop)
249 {
250         if (ctxt == NULL)
251                 return -ENOTCONN;
252
253         *lop = ctxt->loc_logops;
254         if (*lop == NULL)
255                 return -EOPNOTSUPP;
256
257         return 0;
258 }
259
260 static inline int llog_handle2ops(struct llog_handle *loghandle,
261                                   struct llog_operations **lop)
262 {
263         if (loghandle == NULL)
264                 return -EINVAL;
265
266         return llog_ctxt2ops(loghandle->lgh_ctxt, lop);
267 }
268
269 static inline int llog_data_len(int len)
270 {
271         return size_round(len);
272 }
273
274 static inline struct llog_ctxt *llog_get_context(struct obd_llogs *llogs,
275                                                  int index)
276 {
277         if (index < 0 || index >= LLOG_MAX_CTXTS)
278                 return NULL;
279
280         return llogs->llog_ctxt[index];
281 }
282
283 static inline int llog_open(struct llog_ctxt *ctxt, struct llog_handle **res,
284                             struct llog_logid *logid, char *name, int flags)
285 {
286         struct llog_operations *lop;
287         int rc;
288         ENTRY;
289
290         rc = llog_ctxt2ops(ctxt, &lop);
291         if (rc)
292                 RETURN(rc);
293         if (lop->lop_open == NULL)
294                 RETURN(-EOPNOTSUPP);
295
296         rc = lop->lop_open(ctxt, res, logid, name, flags);
297         RETURN(rc);
298 }
299
300 static inline int llog_destroy(struct llog_handle *handle)
301 {
302         struct llog_operations *lop;
303         int rc;
304         ENTRY;
305
306         rc = llog_handle2ops(handle, &lop);
307         if (rc)
308                 RETURN(rc);
309         if (lop->lop_destroy == NULL)
310                 RETURN(-EOPNOTSUPP);
311
312         rc = lop->lop_destroy(handle);
313         RETURN(rc);
314 }
315
316 static inline int llog_read_header(struct llog_handle *handle)
317 {
318         struct llog_operations *lop;
319         int rc;
320         ENTRY;
321
322         rc = llog_handle2ops(handle, &lop);
323         if (rc)
324                 RETURN(rc);
325         if (lop->lop_read_header == NULL)
326                 RETURN(-EOPNOTSUPP);
327
328         rc = lop->lop_read_header(handle);
329         RETURN(rc);
330 }
331
332 static inline int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
333                            void *buf, struct llog_cookie *logcookies,
334                            int numcookies, void *data)
335 {
336         struct llog_operations *lop;
337         int rc;
338         ENTRY;
339
340         rc = llog_ctxt2ops(ctxt, &lop);
341         if (rc)
342                 RETURN(rc);
343         if (lop->lop_add == NULL)
344                 RETURN(-EOPNOTSUPP);
345
346         rc = lop->lop_add(ctxt, rec, buf, logcookies, numcookies, data);
347         RETURN(rc);
348 }
349
350 static inline int llog_cancel(struct llog_ctxt *ctxt, int count,
351                               struct llog_cookie *cookies, int flags, void *data)
352 {
353         struct llog_operations *lop;
354         int rc;
355         ENTRY;
356
357         rc = llog_ctxt2ops(ctxt, &lop);
358         if (rc)
359                 RETURN(rc);
360         if (lop->lop_cancel == NULL)
361                 RETURN(-EOPNOTSUPP);
362
363         rc = lop->lop_cancel(ctxt, count, cookies, flags, data);
364         RETURN(rc);
365 }
366
367 static inline int llog_write_rec(struct llog_handle *handle,
368                                  struct llog_rec_hdr *rec,
369                                  struct llog_cookie *logcookies,
370                                  int numcookies, void *buf, int idx)
371 {
372         struct llog_operations *lop;
373         int rc, buflen;
374         ENTRY;
375
376         rc = llog_handle2ops(handle, &lop);
377         if (rc)
378                 RETURN(rc);
379         if (lop->lop_write_rec == NULL)
380                 RETURN(-EOPNOTSUPP);
381
382         if (buf)
383                 buflen = le32_to_cpu(rec->lrh_len) + sizeof(struct llog_rec_hdr)
384                                 + sizeof(struct llog_rec_tail);
385         else
386                 buflen = le32_to_cpu(rec->lrh_len);
387         LASSERT(size_round(buflen) == buflen);
388
389         rc = lop->lop_write_rec(handle, rec, logcookies, numcookies, buf, idx);
390         RETURN(rc);
391 }
392
393 static inline int llog_next_block(struct llog_handle *loghandle, int *curr_idx,
394                                   int next_idx, __u64 *curr_offset, void *buf,
395                                   int len)
396 {
397         struct llog_operations *lop;
398         int rc;
399         ENTRY;
400
401         rc = llog_handle2ops(loghandle, &lop);
402         if (rc)
403                 RETURN(rc);
404         if (lop->lop_next_block == NULL)
405                 RETURN(-EOPNOTSUPP);
406
407         rc = lop->lop_next_block(loghandle, curr_idx, next_idx, curr_offset, buf,
408                                  len);
409         RETURN(rc);
410 }
411
412 static inline int llog_prev_block(struct llog_handle *loghandle,
413                                   int prev_idx, void *buf, int len)
414 {
415         struct llog_operations *lop;
416         int rc;
417         ENTRY;
418
419         rc = llog_handle2ops(loghandle, &lop);
420         if (rc)
421                 RETURN(rc);
422         if (lop->lop_prev_block == NULL)
423                 RETURN(-EOPNOTSUPP);
424
425         rc = lop->lop_prev_block(loghandle, prev_idx, buf, len);
426         RETURN(rc);
427 }
428
429 static inline int llog_connect(struct llog_ctxt *ctxt, int count,
430                                struct llog_logid *logid, struct llog_gen *gen,
431                                struct obd_uuid *uuid)
432 {
433         struct llog_operations *lop;
434         int rc;
435         ENTRY;
436
437         rc = llog_ctxt2ops(ctxt, &lop);
438         if (rc)
439                 RETURN(rc);
440         if (lop->lop_connect == NULL)
441                 RETURN(-EOPNOTSUPP);
442
443         rc = lop->lop_connect(ctxt, count, logid, gen, uuid);
444         RETURN(rc);
445 }
446
447 static inline int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
448 {
449         struct llog_operations *lop;
450         int rc;
451         ENTRY;
452
453         rc = llog_ctxt2ops(ctxt, &lop);
454         if (rc)
455                 RETURN(rc);
456         if (lop->lop_sync == NULL)
457                 RETURN(-EOPNOTSUPP);
458
459         rc = lop->lop_sync(ctxt, exp);
460         RETURN(rc);
461 }
462
463 #endif