Whamcloud - gitweb
b=3550
[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                      struct rw_semaphore **lock, int *lock_count);
104 int llog_cat_cancel_records(struct llog_handle *cathandle, int count,
105                             struct llog_cookie *cookies);
106 int llog_cat_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data);
107 int llog_cat_reverse_process(struct llog_handle *cat_llh, llog_cb_t cb, void *data);
108 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index);
109
110 /* llog_obd.c */
111 int llog_catalog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
112                      void *buf, struct llog_cookie *reccookie, int, void *data,
113                      struct rw_semaphore **lock, int *lock_count);
114 int llog_catalog_cancel(struct llog_ctxt *ctxt, int count, struct llog_cookie *,
115                         int flags, void *data);
116 int llog_catalog_setup(struct llog_ctxt **res, char *name, struct obd_export *exp,
117                        struct lvfs_run_ctxt *, struct fsfilt_operations *fsops, 
118                        struct dentry *logs_de, struct dentry *objects_de);
119 int llog_catalog_cleanup(struct llog_ctxt *ctxt);
120 int llog_cat_half_bottom(struct llog_cookie *, struct llog_handle *);
121
122 /* llog_lvfs.c */
123 int llog_get_cat_list(struct lvfs_run_ctxt *, struct fsfilt_operations *,
124                       char *name, int count, struct llog_catid *idarray);
125 int llog_put_cat_list(struct lvfs_run_ctxt *, struct fsfilt_operations *, 
126                       char *name, int count, struct llog_catid *idarray);
127 extern struct llog_operations llog_lvfs_ops;
128
129 int llog_obd_origin_setup(struct obd_device *, struct obd_llogs *, int,
130                           struct obd_device *, int, struct llog_logid *);
131
132 int obd_llog_cat_initialize(struct obd_device *, struct obd_llogs *, int, char *);
133
134 /* llog_obd.c - obd llog api */
135 int obd_llog_setup(struct obd_device *obd, struct obd_llogs *, int index, 
136                    struct obd_device *disk_obd, int count, struct llog_logid *logid,
137                    struct llog_operations *op);
138 int obd_llog_init(struct obd_device *, struct obd_llogs *, struct obd_device *,
139                   int, struct llog_catid *);
140 int obd_llog_cleanup(struct llog_ctxt *);
141
142 int obd_llog_finish(struct obd_device *, struct obd_llogs *, int);
143
144 /* llog_ioctl.c */
145 int llog_ioctl(struct llog_ctxt *ctxt, int cmd, struct obd_ioctl_data *data);
146 int llog_catalog_list(struct obd_device *obd, int count,
147                      struct obd_ioctl_data *data);
148
149 /* llog_net.c */
150 int llog_initiator_connect(struct llog_ctxt *ctxt);
151 int llog_receptor_accept(struct llog_ctxt *ctxt, struct obd_import *imp);
152 int llog_origin_connect(struct llog_ctxt *ctxt, int count,
153                         struct llog_logid *logid, struct llog_gen *gen,
154                         struct obd_uuid *uuid);
155 int llog_handle_connect(struct ptlrpc_request *req);
156
157 /* recov_thread.c */
158 int llog_obd_repl_cancel(struct llog_ctxt *ctxt, int count,
159                          struct llog_cookie *cookies, int flags, void *data);
160                          
161 int llog_obd_repl_sync(struct llog_ctxt *ctxt, struct obd_export *exp);
162 int llog_repl_connect(struct llog_ctxt *ctxt, int count,
163                       struct llog_logid *logid, struct llog_gen *gen,
164                       struct obd_uuid *uuid);
165
166 struct llog_operations {
167         int (*lop_setup)(struct obd_device *, struct obd_llogs *, int,
168                          struct obd_device *, int, struct llog_logid *);
169
170         int (*lop_cleanup)(struct llog_ctxt *ctxt);
171         int (*lop_open)(struct llog_ctxt *ctxt, struct llog_handle **,
172                         struct llog_logid *logid, char *name, int flags);
173         int (*lop_destroy)(struct llog_handle *handle);
174         int (*lop_close)(struct llog_handle *handle);
175
176         int (*lop_read_header)(struct llog_handle *handle);
177         int (*lop_add)(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
178                        void *buf, struct llog_cookie *logcookies,
179                        int numcookies, void *data, struct rw_semaphore **lock,
180                        int *lock_count);
181         int (*lop_cancel)(struct llog_ctxt *ctxt, int count,
182                           struct llog_cookie *cookies, int flags, void *data);
183         int (*lop_write_rec)(struct llog_handle *loghandle,
184                              struct llog_rec_hdr *rec,
185                              struct llog_cookie *logcookies, int numcookies,
186                              void *, int idx);
187         int (*lop_next_block)(struct llog_handle *h, int *curr_idx,
188                               int next_idx, __u64 *offset, void *buf, int len);
189         int (*lop_prev_block)(struct llog_handle *h,
190                               int prev_idx, void *buf, int len);
191
192         int (*lop_sync)(struct llog_ctxt *ctxt, struct obd_export *exp);
193         int (*lop_connect)(struct llog_ctxt *ctxt, int count,
194                            struct llog_logid *logid, struct llog_gen *gen,
195                            struct obd_uuid *uuid);
196 };
197
198 struct llog_ctxt {
199         /* needed for lvfs based log */
200         struct llog_handle      *loc_handle;
201         struct llog_operations  *loc_logops;
202         struct fsfilt_operations *loc_fsops;
203         struct dentry           *loc_logs_dir;
204         struct dentry           *loc_objects_dir;
205         struct lvfs_run_ctxt    *loc_lvfs_ctxt;
206
207         struct obd_device       *loc_obd;   /* points back to the containing obd */
208         struct llog_gen          loc_gen;
209         int                      loc_idx;   /* my index the obd array of ctxt's */
210         int                      loc_alone; /* is this llog ctxt has an obd? */
211
212         struct obd_export       *loc_exp;
213         struct obd_import       *loc_imp;   /* to use in RPC's: can be backward
214                                                pointing import */
215         struct llog_canceld_ctxt *loc_llcd;
216         struct semaphore         loc_sem;   /* protects loc_llcd */
217         void                    *llog_proc_cb;
218         struct obd_llogs        *loc_llogs;
219 };
220
221 struct llog_create_locks {
222         int                     lcl_count;
223         struct rw_semaphore    *lcl_locks[0];
224 };
225
226 static inline void llog_gen_init(struct llog_ctxt *ctxt)
227 {
228         struct obd_device *obd = ctxt->loc_exp->exp_obd;
229
230         if (!strcmp(obd->obd_type->typ_name, "mds"))
231                 ctxt->loc_gen.mnt_cnt = obd->u.mds.mds_mount_count;
232         else if (!strstr(obd->obd_type->typ_name, "filter"))
233                 ctxt->loc_gen.mnt_cnt = obd->u.filter.fo_mount_count;
234         else
235                 ctxt->loc_gen.mnt_cnt = 0;
236 }
237
238 static inline int llog_gen_lt(struct llog_gen a, struct llog_gen b)
239 {
240         if (a.mnt_cnt < b.mnt_cnt)
241                 return 1;
242         if (a.mnt_cnt > b.mnt_cnt)
243                 return 0;
244         return(a.conn_cnt < b.conn_cnt ? 1 : 0);
245 }
246
247 #define LLOG_GEN_INC(gen)  ((gen).conn_cnt) ++
248 #define LLOG_PROC_BREAK 0x0001
249 #define LLOG_DEL_RECORD 0x0002
250
251 #define EQ_LOGID(a, b) (a.lgl_oid == b.lgl_oid &&       \
252                         a.lgl_ogr == b.lgl_ogr &&       \
253                         a.lgl_ogen == b.lgl_ogen)
254
255 /* Flags are in the bottom 16 bit */
256 #define LLOG_COOKIE_FLAG_MASK   0x0000ffff
257 #define LLOG_COOKIE_REPLAY_NEW  0x00000001
258 #define LLOG_COOKIE_REPLAY      0x00000002
259
260 static inline int llog_cookie_get_flags(struct llog_cookie *logcookie)
261 {
262         return(logcookie->lgc_flags & LLOG_COOKIE_FLAG_MASK);
263 }
264
265 static inline void llog_cookie_add_flags(struct llog_cookie *logcookie, int flags)
266 {
267         logcookie->lgc_flags |= LLOG_COOKIE_FLAG_MASK & flags; 
268 }
269
270 static inline void llog_cookie_set_flags(struct llog_cookie *logcookie, int flags)
271 {
272         logcookie->lgc_flags &= ~LLOG_COOKIE_FLAG_MASK;
273         llog_cookie_add_flags(logcookie, flags); 
274 }
275
276 static inline void llog_cookie_clear_flags(struct llog_cookie *logcookie, int flags)
277 {
278         logcookie->lgc_flags &= ~(LLOG_COOKIE_FLAG_MASK & flags);
279 }
280
281 static inline int llog_ctxt2ops(struct llog_ctxt *ctxt,
282                                struct llog_operations **lop)
283 {
284         if (ctxt == NULL)
285                 return -ENOTCONN;
286
287         *lop = ctxt->loc_logops;
288         if (*lop == NULL)
289                 return -EOPNOTSUPP;
290
291         return 0;
292 }
293
294 static inline int llog_handle2ops(struct llog_handle *loghandle,
295                                   struct llog_operations **lop)
296 {
297         if (loghandle == NULL)
298                 return -EINVAL;
299
300         return llog_ctxt2ops(loghandle->lgh_ctxt, lop);
301 }
302
303 static inline int llog_data_len(int len)
304 {
305         return size_round(len);
306 }
307
308 static inline struct llog_ctxt *llog_get_context(struct obd_llogs *llogs,
309                                                  int index)
310 {
311         if (index < 0 || index >= LLOG_MAX_CTXTS)
312                 return NULL;
313
314         return llogs->llog_ctxt[index];
315 }
316
317 static inline int llog_open(struct llog_ctxt *ctxt, struct llog_handle **res,
318                             struct llog_logid *logid, char *name, int flags)
319 {
320         struct llog_operations *lop;
321         int rc;
322         ENTRY;
323
324         rc = llog_ctxt2ops(ctxt, &lop);
325         if (rc)
326                 RETURN(rc);
327         if (lop->lop_open == NULL)
328                 RETURN(-EOPNOTSUPP);
329
330         rc = lop->lop_open(ctxt, res, logid, name, flags);
331         RETURN(rc);
332 }
333
334 static inline int llog_destroy(struct llog_handle *handle)
335 {
336         struct llog_operations *lop;
337         int rc;
338         ENTRY;
339
340         rc = llog_handle2ops(handle, &lop);
341         if (rc)
342                 RETURN(rc);
343         if (lop->lop_destroy == NULL)
344                 RETURN(-EOPNOTSUPP);
345
346         rc = lop->lop_destroy(handle);
347         RETURN(rc);
348 }
349
350 static inline int llog_read_header(struct llog_handle *handle)
351 {
352         struct llog_operations *lop;
353         int rc;
354         ENTRY;
355
356         rc = llog_handle2ops(handle, &lop);
357         if (rc)
358                 RETURN(rc);
359         if (lop->lop_read_header == NULL)
360                 RETURN(-EOPNOTSUPP);
361
362         rc = lop->lop_read_header(handle);
363         RETURN(rc);
364 }
365
366 static inline int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
367                            void *buf, struct llog_cookie *logcookies,
368                            int numcookies, void *data, 
369                            struct rw_semaphore **lock, int *lock_count)
370 {
371         struct llog_operations *lop;
372         int rc;
373         ENTRY;
374
375         rc = llog_ctxt2ops(ctxt, &lop);
376         if (rc)
377                 RETURN(rc);
378         if (lop->lop_add == NULL)
379                 RETURN(-EOPNOTSUPP);
380
381         rc = lop->lop_add(ctxt, rec, buf, logcookies, numcookies, data,
382                           lock, lock_count);
383         RETURN(rc);
384 }
385
386 static inline int llog_cancel(struct llog_ctxt *ctxt, int count,
387                               struct llog_cookie *cookies, int flags, void *data)
388 {
389         struct llog_operations *lop;
390         int rc;
391         ENTRY;
392
393         rc = llog_ctxt2ops(ctxt, &lop);
394         if (rc)
395                 RETURN(rc);
396         if (lop->lop_cancel == NULL)
397                 RETURN(-EOPNOTSUPP);
398
399         rc = lop->lop_cancel(ctxt, count, cookies, flags, data);
400         RETURN(rc);
401 }
402
403 static inline int llog_write_rec(struct llog_handle *handle,
404                                  struct llog_rec_hdr *rec,
405                                  struct llog_cookie *logcookies,
406                                  int numcookies, void *buf, int idx)
407 {
408         struct llog_operations *lop;
409         int rc, buflen;
410         ENTRY;
411
412         rc = llog_handle2ops(handle, &lop);
413         if (rc)
414                 RETURN(rc);
415         if (lop->lop_write_rec == NULL)
416                 RETURN(-EOPNOTSUPP);
417
418         if (buf)
419                 buflen = le32_to_cpu(rec->lrh_len) + sizeof(struct llog_rec_hdr)
420                                 + sizeof(struct llog_rec_tail);
421         else
422                 buflen = le32_to_cpu(rec->lrh_len);
423         LASSERT(size_round(buflen) == buflen);
424
425         rc = lop->lop_write_rec(handle, rec, logcookies, numcookies, buf, idx);
426         RETURN(rc);
427 }
428
429 static inline int llog_next_block(struct llog_handle *loghandle, int *curr_idx,
430                                   int next_idx, __u64 *curr_offset, void *buf,
431                                   int len)
432 {
433         struct llog_operations *lop;
434         int rc;
435         ENTRY;
436
437         rc = llog_handle2ops(loghandle, &lop);
438         if (rc)
439                 RETURN(rc);
440         if (lop->lop_next_block == NULL)
441                 RETURN(-EOPNOTSUPP);
442
443         rc = lop->lop_next_block(loghandle, curr_idx, next_idx, curr_offset, buf,
444                                  len);
445         RETURN(rc);
446 }
447
448 static inline int llog_prev_block(struct llog_handle *loghandle,
449                                   int prev_idx, void *buf, int len)
450 {
451         struct llog_operations *lop;
452         int rc;
453         ENTRY;
454
455         rc = llog_handle2ops(loghandle, &lop);
456         if (rc)
457                 RETURN(rc);
458         if (lop->lop_prev_block == NULL)
459                 RETURN(-EOPNOTSUPP);
460
461         rc = lop->lop_prev_block(loghandle, prev_idx, buf, len);
462         RETURN(rc);
463 }
464
465 static inline int llog_connect(struct llog_ctxt *ctxt, int count,
466                                struct llog_logid *logid, struct llog_gen *gen,
467                                struct obd_uuid *uuid)
468 {
469         struct llog_operations *lop;
470         int rc;
471         ENTRY;
472
473         rc = llog_ctxt2ops(ctxt, &lop);
474         if (rc)
475                 RETURN(rc);
476         if (lop->lop_connect == NULL)
477                 RETURN(-EOPNOTSUPP);
478
479         rc = lop->lop_connect(ctxt, count, logid, gen, uuid);
480         RETURN(rc);
481 }
482
483 static inline int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
484 {
485         struct llog_operations *lop;
486         int rc;
487         ENTRY;
488
489         rc = llog_ctxt2ops(ctxt, &lop);
490         if (rc)
491                 RETURN(rc);
492         if (lop->lop_sync == NULL)
493                 RETURN(-EOPNOTSUPP);
494
495         rc = lop->lop_sync(ctxt, exp);
496         RETURN(rc);
497 }
498
499 static inline void llog_create_lock_free(struct llog_create_locks *lcl)
500 {
501         int size, offset = offsetof(struct llog_create_locks, lcl_locks);
502         int i;
503         ENTRY;
504                                                                                                                              
505         for (i = 0; i < lcl->lcl_count; i ++) {
506                 if (lcl->lcl_locks[i] != NULL) {
507 #ifdef __KERNEL__
508                         LASSERT(down_write_trylock(lcl->lcl_locks[i]) == 0);
509 #endif
510                         up_write(lcl->lcl_locks[i]);
511                 }
512         }
513         size = offset + sizeof(struct rw_semaphore *) * lcl->lcl_count;
514         OBD_FREE(lcl, size);
515 }
516
517 #endif