Whamcloud - gitweb
landing b_cmobd_merge on HEAD
[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 /* got from mds_update_record.
74  * FIXME: maybe some attribute in reint_record and update_record will be
75  * changed later. */
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 */
78
79 struct update_record {
80         __u32 ur_opcode;
81         __u32 ur_fsuid;
82         __u32 ur_fsgid;
83         dev_t ur_rdev;
84         struct iattr ur_iattr;
85         struct iattr ur_pattr;
86         __u32 ur_flags;
87         __u32 ur_len;
88 };
89
90 struct reint_record {
91        struct update_record u_rec;
92        char *rec_data1;
93        int rec1_size;
94        char *rec_data2;
95        int rec2_size;
96 };
97
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;
102 };
103
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);
116
117 /* llog_cat.c   -  catalog api */
118 struct llog_process_data {
119         void *lpd_data;
120         llog_cb_t lpd_cb;
121 };
122
123 struct llog_process_cat_data {
124         int     first_idx;
125         int     last_idx;
126         /* to process catalog across zero record */
127 };
128
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);
139
140 /* llog_obd.c */
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 *);
150
151 /* llog_lvfs.c */
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;
157
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);
164
165 int obd_llog_cat_initialize(struct obd_device *, struct obd_llogs *, int, char *);
166
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 *);
174
175 int obd_llog_finish(struct obd_device *, struct obd_llogs *, int);
176
177 /* llog_ioctl.c */
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);
181
182 /* llog_net.c */
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);
189
190 /* recov_thread.c */
191 int llog_obd_repl_cancel(struct llog_ctxt *ctxt, int count,
192                          struct llog_cookie *cookies, int flags, void *data);
193                          
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);
198
199 struct llog_operations {
200         int (*lop_setup)(struct obd_device *, struct obd_llogs *, int,
201                          struct obd_device *, int, struct llog_logid *);
202
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);
208
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,
218                              void *, int idx);
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);
223
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);
228 };
229
230 struct llog_ctxt {
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;
238
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? */
243
244         struct obd_export       *loc_exp;
245         struct obd_import       *loc_imp;   /* to use in RPC's: can be backward
246                                                pointing import */
247         struct llog_canceld_ctxt *loc_llcd;
248         struct semaphore         loc_sem;   /* protects loc_llcd */
249         void                    *llog_proc_cb;
250         struct obd_llogs        *loc_llogs;
251 };
252
253 static inline void llog_gen_init(struct llog_ctxt *ctxt)
254 {
255         struct obd_device *obd = ctxt->loc_exp->exp_obd;
256
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;
261         else
262                 ctxt->loc_gen.mnt_cnt = 0;
263 }
264
265 static inline int llog_gen_lt(struct llog_gen a, struct llog_gen b)
266 {
267         if (a.mnt_cnt < b.mnt_cnt)
268                 return 1;
269         if (a.mnt_cnt > b.mnt_cnt)
270                 return 0;
271         return(a.conn_cnt < b.conn_cnt ? 1 : 0);
272 }
273
274 #define LLOG_GEN_INC(gen)  ((gen).conn_cnt) ++
275 #define LLOG_PROC_BREAK 0x0001
276 #define LLOG_DEL_RECORD 0x0002
277
278 static inline int llog_ctxt2ops(struct llog_ctxt *ctxt,
279                                struct llog_operations **lop)
280 {
281         if (ctxt == NULL)
282                 return -ENOTCONN;
283
284         *lop = ctxt->loc_logops;
285         if (*lop == NULL)
286                 return -EOPNOTSUPP;
287
288         return 0;
289 }
290
291 static inline int llog_handle2ops(struct llog_handle *loghandle,
292                                   struct llog_operations **lop)
293 {
294         if (loghandle == NULL)
295                 return -EINVAL;
296
297         return llog_ctxt2ops(loghandle->lgh_ctxt, lop);
298 }
299
300 static inline int llog_data_len(int len)
301 {
302         return size_round(len);
303 }
304
305 static inline struct llog_ctxt *llog_get_context(struct obd_llogs *llogs,
306                                                  int index)
307 {
308         if (index < 0 || index >= LLOG_MAX_CTXTS)
309                 return NULL;
310
311         return llogs->llog_ctxt[index];
312 }
313
314 static inline int llog_create(struct llog_ctxt *ctxt, struct llog_handle **res,
315                               struct llog_logid *logid, char *name)
316 {
317         struct llog_operations *lop;
318         int rc;
319         ENTRY;
320
321         rc = llog_ctxt2ops(ctxt, &lop);
322         if (rc)
323                 RETURN(rc);
324         if (lop->lop_create == NULL)
325                 RETURN(-EOPNOTSUPP);
326
327         rc = lop->lop_create(ctxt, res, logid, name);
328         RETURN(rc);
329 }
330
331 static inline int llog_destroy(struct llog_handle *handle)
332 {
333         struct llog_operations *lop;
334         int rc;
335         ENTRY;
336
337         rc = llog_handle2ops(handle, &lop);
338         if (rc)
339                 RETURN(rc);
340         if (lop->lop_destroy == NULL)
341                 RETURN(-EOPNOTSUPP);
342
343         rc = lop->lop_destroy(handle);
344         RETURN(rc);
345 }
346
347 static inline int llog_read_header(struct llog_handle *handle)
348 {
349         struct llog_operations *lop;
350         int rc;
351         ENTRY;
352
353         rc = llog_handle2ops(handle, &lop);
354         if (rc)
355                 RETURN(rc);
356         if (lop->lop_read_header == NULL)
357                 RETURN(-EOPNOTSUPP);
358
359         rc = lop->lop_read_header(handle);
360         RETURN(rc);
361 }
362
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)
366 {
367         struct llog_operations *lop;
368         int rc;
369         ENTRY;
370
371         rc = llog_ctxt2ops(ctxt, &lop);
372         if (rc)
373                 RETURN(rc);
374         if (lop->lop_add == NULL)
375                 RETURN(-EOPNOTSUPP);
376
377         rc = lop->lop_add(ctxt, rec, buf, logcookies, numcookies, data);
378         RETURN(rc);
379 }
380
381 static inline int llog_cancel(struct llog_ctxt *ctxt, int count,
382                               struct llog_cookie *cookies, int flags, void *data)
383 {
384         struct llog_operations *lop;
385         int rc;
386         ENTRY;
387
388         rc = llog_ctxt2ops(ctxt, &lop);
389         if (rc)
390                 RETURN(rc);
391         if (lop->lop_cancel == NULL)
392                 RETURN(-EOPNOTSUPP);
393
394         rc = lop->lop_cancel(ctxt, count, cookies, flags, data);
395         RETURN(rc);
396 }
397
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)
402 {
403         struct llog_operations *lop;
404         int rc, buflen;
405         ENTRY;
406
407         rc = llog_handle2ops(handle, &lop);
408         if (rc)
409                 RETURN(rc);
410         if (lop->lop_write_rec == NULL)
411                 RETURN(-EOPNOTSUPP);
412
413         if (buf)
414                 buflen = le32_to_cpu(rec->lrh_len) + sizeof(struct llog_rec_hdr)
415                                 + sizeof(struct llog_rec_tail);
416         else
417                 buflen = le32_to_cpu(rec->lrh_len);
418         LASSERT(size_round(buflen) == buflen);
419
420         rc = lop->lop_write_rec(handle, rec, logcookies, numcookies, buf, idx);
421         RETURN(rc);
422 }
423
424 static inline int llog_next_block(struct llog_handle *loghandle, int *curr_idx,
425                                   int next_idx, __u64 *curr_offset, void *buf,
426                                   int len)
427 {
428         struct llog_operations *lop;
429         int rc;
430         ENTRY;
431
432         rc = llog_handle2ops(loghandle, &lop);
433         if (rc)
434                 RETURN(rc);
435         if (lop->lop_next_block == NULL)
436                 RETURN(-EOPNOTSUPP);
437
438         rc = lop->lop_next_block(loghandle, curr_idx, next_idx, curr_offset, buf,
439                                  len);
440         RETURN(rc);
441 }
442
443 static inline int llog_prev_block(struct llog_handle *loghandle,
444                                   int prev_idx, void *buf, int len)
445 {
446         struct llog_operations *lop;
447         int rc;
448         ENTRY;
449
450         rc = llog_handle2ops(loghandle, &lop);
451         if (rc)
452                 RETURN(rc);
453         if (lop->lop_prev_block == NULL)
454                 RETURN(-EOPNOTSUPP);
455
456         rc = lop->lop_prev_block(loghandle, prev_idx, buf, len);
457         RETURN(rc);
458 }
459
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)
463 {
464         struct llog_operations *lop;
465         int rc;
466         ENTRY;
467
468         rc = llog_ctxt2ops(ctxt, &lop);
469         if (rc)
470                 RETURN(rc);
471         if (lop->lop_connect == NULL)
472                 RETURN(-EOPNOTSUPP);
473
474         rc = lop->lop_connect(ctxt, count, logid, gen, uuid);
475         RETURN(rc);
476 }
477
478 static inline int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
479 {
480         struct llog_operations *lop;
481         int rc;
482         ENTRY;
483
484         rc = llog_ctxt2ops(ctxt, &lop);
485         if (rc)
486                 RETURN(rc);
487         if (lop->lop_sync == NULL)
488                 RETURN(-EOPNOTSUPP);
489
490         rc = lop->lop_sync(ctxt, exp);
491         RETURN(rc);
492 }
493
494 #endif