Whamcloud - gitweb
2dd6ad441554b046ba26296b0f413b1785fdd539
[fs/lustre-release.git] / lustre / include / lustre_log.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/include/lustre_log.h
37  *
38  * Generic infrastructure for managing a collection of logs.
39  * These logs are used for:
40  *
41  * - orphan recovery: OST adds record on create
42  * - mtime/size consistency: the OST adds a record on first write
43  * - open/unlinked objects: OST adds a record on destroy
44  *
45  * - mds unlink log: the MDS adds an entry upon delete
46  *
47  * - raid1 replication log between OST's
48  * - MDS replication logs
49  */
50
51 #ifndef _LUSTRE_LOG_H
52 #define _LUSTRE_LOG_H
53
54 /** \defgroup log log
55  *
56  * @{
57  */
58
59 #if defined(__linux__)
60 #include <linux/lustre_log.h>
61 #elif defined(__APPLE__)
62 #include <darwin/lustre_log.h>
63 #elif defined(__WINNT__)
64 #include <winnt/lustre_log.h>
65 #else
66 #error Unsupported operating system.
67 #endif
68
69 #include <obd_class.h>
70 #include <lustre/lustre_idl.h>
71 #include <dt_object.h>
72
73 #define LOG_NAME_LIMIT(logname, name)                   \
74         snprintf(logname, sizeof(logname), "LOGS/%s", name)
75 #define LLOG_EEMPTY 4711
76
77 enum llog_open_param {
78         LLOG_OPEN_EXISTS        = 0x0000,
79         LLOG_OPEN_NEW           = 0x0001,
80 };
81
82 struct plain_handle_data {
83         cfs_list_t          phd_entry;
84         struct llog_handle *phd_cat_handle;
85         struct llog_cookie  phd_cookie; /* cookie of this log in its cat */
86 };
87
88 struct cat_handle_data {
89         cfs_list_t              chd_head;
90         struct llog_handle     *chd_current_log; /* currently open log */
91         struct llog_handle      *chd_next_log; /* llog to be used next */
92 };
93
94 static inline void logid_to_fid(struct llog_logid *id, struct lu_fid *fid)
95 {
96         /* For compatibility purposes we identify pre-OSD (~< 2.3.51 MDS)
97          * logid's by non-zero ogen (inode generation) and convert them
98          * into IGIF */
99         if (id->lgl_ogen == 0) {
100                 fid->f_seq = id->lgl_oi.oi.oi_seq;
101                 fid->f_oid = id->lgl_oi.oi.oi_id;
102                 fid->f_ver = 0;
103         } else {
104                 lu_igif_build(fid, id->lgl_oi.oi.oi_id, id->lgl_ogen);
105         }
106 }
107
108 static inline void fid_to_logid(struct lu_fid *fid, struct llog_logid *id)
109 {
110         id->lgl_oi.oi.oi_seq = fid->f_seq;
111         id->lgl_oi.oi.oi_id = fid->f_oid;
112         id->lgl_ogen = 0;
113 }
114
115 static inline void logid_set_id(struct llog_logid *log_id, __u64 id)
116 {
117         log_id->lgl_oi.oi.oi_id = id;
118 }
119
120 static inline __u64 logid_id(struct llog_logid *log_id)
121 {
122         return log_id->lgl_oi.oi.oi_id;
123 }
124
125 struct llog_handle;
126
127 /* llog.c  -  general API */
128 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
129                      int flags, struct obd_uuid *uuid);
130 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
131                       struct llog_rec_hdr *rec, void *data);
132 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
133                  llog_cb_t cb, void *data, void *catdata);
134 int llog_process_or_fork(const struct lu_env *env,
135                          struct llog_handle *loghandle,
136                          llog_cb_t cb, void *data, void *catdata, bool fork);
137 int llog_reverse_process(const struct lu_env *env,
138                          struct llog_handle *loghandle, llog_cb_t cb,
139                          void *data, void *catdata);
140 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
141                     int index);
142 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
143               struct llog_handle **lgh, struct llog_logid *logid,
144               char *name, enum llog_open_param open_param);
145 int llog_close(const struct lu_env *env, struct llog_handle *cathandle);
146 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
147                   char *name);
148 int llog_backup(const struct lu_env *env, struct obd_device *obd,
149                 struct llog_ctxt *ctxt, struct llog_ctxt *bak_ctxt,
150                 char *name, char *backup);
151
152 /* llog_process flags */
153 #define LLOG_FLAG_NODEAMON 0x0001
154
155 /* llog_cat.c - catalog api */
156 struct llog_process_data {
157         /**
158          * Any useful data needed while processing catalog. This is
159          * passed later to process callback.
160          */
161         void                *lpd_data;
162         /**
163          * Catalog process callback function, called for each record
164          * in catalog.
165          */
166         llog_cb_t            lpd_cb;
167         /**
168          * Start processing the catalog from startcat/startidx
169          */
170         int                  lpd_startcat;
171         int                  lpd_startidx;
172 };
173
174 struct llog_process_cat_data {
175         /**
176          * Temporary stored first_idx while scanning log.
177          */
178         int                  lpcd_first_idx;
179         /**
180          * Temporary stored last_idx while scanning log.
181          */
182         int                  lpcd_last_idx;
183 };
184
185 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle);
186 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
187                      struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
188                      void *buf, struct thandle *th);
189 int llog_cat_declare_add_rec(const struct lu_env *env,
190                              struct llog_handle *cathandle,
191                              struct llog_rec_hdr *rec, struct thandle *th);
192 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
193                  struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
194                  void *buf);
195 int llog_cat_cancel_records(const struct lu_env *env,
196                             struct llog_handle *cathandle, int count,
197                             struct llog_cookie *cookies);
198 int llog_cat_process_or_fork(const struct lu_env *env,
199                              struct llog_handle *cat_llh, llog_cb_t cb,
200                              void *data, int startcat, int startidx, bool fork);
201 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
202                      llog_cb_t cb, void *data, int startcat, int startidx);
203 int llog_cat_reverse_process(const struct lu_env *env,
204                              struct llog_handle *cat_llh, llog_cb_t cb,
205                              void *data);
206 int llog_cat_init_and_process(const struct lu_env *env,
207                               struct llog_handle *llh);
208
209 /* llog_obd.c */
210 int llog_setup(const struct lu_env *env, struct obd_device *obd,
211                struct obd_llog_group *olg, int index,
212                struct obd_device *disk_obd, struct llog_operations *op);
213 int __llog_ctxt_put(const struct lu_env *env, struct llog_ctxt *ctxt);
214 int llog_cleanup(const struct lu_env *env, struct llog_ctxt *);
215 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp, int flags);
216 int llog_cancel(const struct lu_env *env, struct llog_ctxt *ctxt,
217                 struct llog_cookie *cookies, int flags);
218
219 int obd_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
220                   struct obd_device *disk_obd, int *idx);
221
222 int obd_llog_finish(struct obd_device *obd, int count);
223
224 /* llog_ioctl.c */
225 int llog_ioctl(const struct lu_env *env, struct llog_ctxt *ctxt, int cmd,
226                struct obd_ioctl_data *data);
227 int llog_catalog_list(const struct lu_env *env, struct dt_device *d,
228                       int count, struct obd_ioctl_data *data,
229                       const struct lu_fid *fid);
230
231 /* llog_net.c */
232 int llog_initiator_connect(struct llog_ctxt *ctxt);
233
234 struct llog_operations {
235         int (*lop_destroy)(const struct lu_env *env,
236                            struct llog_handle *handle);
237         int (*lop_next_block)(const struct lu_env *env, struct llog_handle *h,
238                               int *curr_idx, int next_idx, __u64 *offset,
239                               void *buf, int len);
240         int (*lop_prev_block)(const struct lu_env *env, struct llog_handle *h,
241                               int prev_idx, void *buf, int len);
242         int (*lop_read_header)(const struct lu_env *env,
243                                struct llog_handle *handle);
244         int (*lop_setup)(const struct lu_env *env, struct obd_device *obd,
245                          struct obd_llog_group *olg, int ctxt_idx,
246                          struct obd_device *disk_obd);
247         int (*lop_sync)(struct llog_ctxt *ctxt, struct obd_export *exp,
248                         int flags);
249         int (*lop_cleanup)(const struct lu_env *env, struct llog_ctxt *ctxt);
250         int (*lop_cancel)(const struct lu_env *env, struct llog_ctxt *ctxt,
251                           struct llog_cookie *cookies, int flags);
252         int (*lop_connect)(struct llog_ctxt *ctxt, struct llog_logid *logid,
253                            struct llog_gen *gen, struct obd_uuid *uuid);
254         /**
255          * Any llog file must be opened first using llog_open().  Llog can be
256          * opened by name, logid or without both, in last case the new logid
257          * will be generated.
258          */
259         int (*lop_open)(const struct lu_env *env, struct llog_handle *lgh,
260                         struct llog_logid *logid, char *name,
261                         enum llog_open_param);
262         /**
263          * Opened llog may not exist and this must be checked where needed using
264          * the llog_exist() call.
265          */
266         int (*lop_exist)(struct llog_handle *lgh);
267         /**
268          * Close llog file and calls llog_free_handle() implicitly.
269          * Any opened llog must be closed by llog_close() call.
270          */
271         int (*lop_close)(const struct lu_env *env, struct llog_handle *handle);
272         /**
273          * Create new llog file. The llog must be opened.
274          * Must be used only for local llog operations.
275          */
276         int (*lop_declare_create)(const struct lu_env *env,
277                                   struct llog_handle *handle,
278                                   struct thandle *th);
279         int (*lop_create)(const struct lu_env *env, struct llog_handle *handle,
280                           struct thandle *th);
281         /**
282          * write new record in llog. It appends records usually but can edit
283          * existing records too.
284          */
285         int (*lop_declare_write_rec)(const struct lu_env *env,
286                                      struct llog_handle *lgh,
287                                      struct llog_rec_hdr *rec,
288                                      int idx, struct thandle *th);
289         int (*lop_write_rec)(const struct lu_env *env,
290                              struct llog_handle *loghandle,
291                              struct llog_rec_hdr *rec,
292                              struct llog_cookie *cookie, int cookiecount,
293                              void *buf, int idx, struct thandle *th);
294         /**
295          * Add new record in llog catalog. Does the same as llog_write_rec()
296          * but using llog catalog.
297          */
298         int (*lop_declare_add)(const struct lu_env *env,
299                                struct llog_handle *lgh,
300                                struct llog_rec_hdr *rec, struct thandle *th);
301         int (*lop_add)(const struct lu_env *env, struct llog_handle *lgh,
302                        struct llog_rec_hdr *rec, struct llog_cookie *cookie,
303                        void *buf, struct thandle *th);
304 };
305
306 /* In-memory descriptor for a log object or log catalog */
307 struct llog_handle {
308         struct rw_semaphore      lgh_lock;
309         spinlock_t               lgh_hdr_lock; /* protect lgh_hdr data */
310         struct llog_logid        lgh_id; /* id of this log */
311         struct llog_log_hdr     *lgh_hdr;
312         struct dt_object        *lgh_obj;
313         int                      lgh_last_idx;
314         int                      lgh_cur_idx; /* used during llog_process */
315         __u64                    lgh_cur_offset; /* used during llog_process */
316         struct llog_ctxt        *lgh_ctxt;
317         union {
318                 struct plain_handle_data         phd;
319                 struct cat_handle_data           chd;
320         } u;
321         char                    *lgh_name;
322         void                    *private_data;
323         struct llog_operations  *lgh_logops;
324         atomic_t                 lgh_refcount;
325 };
326
327 /* llog_osd.c */
328 extern struct llog_operations llog_osd_ops;
329 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
330                           int idx, int count, struct llog_catid *idarray,
331                           const struct lu_fid *fid);
332 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
333                           int idx, int count, struct llog_catid *idarray,
334                           const struct lu_fid *fid);
335
336 #define LLOG_CTXT_FLAG_UNINITIALIZED     0x00000001
337 #define LLOG_CTXT_FLAG_STOP              0x00000002
338
339 struct llog_ctxt {
340         int                      loc_idx; /* my index the obd array of ctxt's */
341         struct obd_device       *loc_obd; /* points back to the containing obd*/
342         struct obd_llog_group   *loc_olg; /* group containing that ctxt */
343         struct obd_export       *loc_exp; /* parent "disk" export (e.g. MDS) */
344         struct obd_import       *loc_imp; /* to use in RPC's: can be backward
345                                              pointing import */
346         struct llog_operations  *loc_logops;
347         struct llog_handle      *loc_handle;
348         struct mutex             loc_mutex; /* protect loc_imp */
349         atomic_t                 loc_refcount;
350         long                     loc_flags; /* flags, see above defines */
351         struct dt_object        *loc_dir;
352         struct local_oid_storage *loc_los_nameless;
353         struct local_oid_storage *loc_los_named;
354 };
355
356 #define LLOG_PROC_BREAK 0x0001
357 #define LLOG_DEL_RECORD 0x0002
358 #define LLOG_DEL_PLAIN  0x0003
359
360 static inline int llog_obd2ops(struct llog_ctxt *ctxt,
361                                struct llog_operations **lop)
362 {
363         if (ctxt == NULL)
364                 return -ENOTCONN;
365
366         *lop = ctxt->loc_logops;
367         if (*lop == NULL)
368                 return -EOPNOTSUPP;
369
370         return 0;
371 }
372
373 static inline int llog_handle2ops(struct llog_handle *loghandle,
374                                   struct llog_operations **lop)
375 {
376         if (loghandle == NULL || loghandle->lgh_logops == NULL)
377                 return -EINVAL;
378
379         *lop = loghandle->lgh_logops;
380         return 0;
381 }
382
383 static inline int llog_data_len(int len)
384 {
385         return cfs_size_round(len);
386 }
387
388 static inline int llog_get_size(struct llog_handle *loghandle)
389 {
390         if (loghandle && loghandle->lgh_hdr)
391                 return loghandle->lgh_hdr->llh_count;
392         return 0;
393 }
394
395 static inline struct llog_ctxt *llog_ctxt_get(struct llog_ctxt *ctxt)
396 {
397         atomic_inc(&ctxt->loc_refcount);
398         CDEBUG(D_INFO, "GETting ctxt %p : new refcount %d\n", ctxt,
399                atomic_read(&ctxt->loc_refcount));
400         return ctxt;
401 }
402
403 static inline void llog_ctxt_put(struct llog_ctxt *ctxt)
404 {
405         if (ctxt == NULL)
406                 return;
407         LASSERT_ATOMIC_GT_LT(&ctxt->loc_refcount, 0, LI_POISON);
408         CDEBUG(D_INFO, "PUTting ctxt %p : new refcount %d\n", ctxt,
409                atomic_read(&ctxt->loc_refcount) - 1);
410         __llog_ctxt_put(NULL, ctxt);
411 }
412
413 static inline void llog_group_init(struct obd_llog_group *olg, int group)
414 {
415         init_waitqueue_head(&olg->olg_waitq);
416         spin_lock_init(&olg->olg_lock);
417         mutex_init(&olg->olg_cat_processing);
418         olg->olg_seq = group;
419 }
420
421 static inline int llog_group_set_ctxt(struct obd_llog_group *olg,
422                                       struct llog_ctxt *ctxt, int index)
423 {
424         LASSERT(index >= 0 && index < LLOG_MAX_CTXTS);
425
426         spin_lock(&olg->olg_lock);
427         if (olg->olg_ctxts[index] != NULL) {
428                 spin_unlock(&olg->olg_lock);
429                 return -EEXIST;
430         }
431         olg->olg_ctxts[index] = ctxt;
432         spin_unlock(&olg->olg_lock);
433         return 0;
434 }
435
436 static inline struct llog_ctxt *llog_group_get_ctxt(struct obd_llog_group *olg,
437                                                     int index)
438 {
439         struct llog_ctxt *ctxt;
440
441         LASSERT(index >= 0 && index < LLOG_MAX_CTXTS);
442
443         spin_lock(&olg->olg_lock);
444         if (olg->olg_ctxts[index] == NULL)
445                 ctxt = NULL;
446         else
447                 ctxt = llog_ctxt_get(olg->olg_ctxts[index]);
448         spin_unlock(&olg->olg_lock);
449         return ctxt;
450 }
451
452 static inline void llog_group_clear_ctxt(struct obd_llog_group *olg, int index)
453 {
454         LASSERT(index >= 0 && index < LLOG_MAX_CTXTS);
455         spin_lock(&olg->olg_lock);
456         olg->olg_ctxts[index] = NULL;
457         spin_unlock(&olg->olg_lock);
458 }
459
460 static inline struct llog_ctxt *llog_get_context(struct obd_device *obd,
461                                                  int index)
462 {
463         return llog_group_get_ctxt(&obd->obd_olg, index);
464 }
465
466 static inline int llog_group_ctxt_null(struct obd_llog_group *olg, int index)
467 {
468         return (olg->olg_ctxts[index] == NULL);
469 }
470
471 static inline int llog_ctxt_null(struct obd_device *obd, int index)
472 {
473         return (llog_group_ctxt_null(&obd->obd_olg, index));
474 }
475
476 static inline int llog_destroy(const struct lu_env *env,
477                                struct llog_handle *handle)
478 {
479         struct llog_operations *lop;
480         int rc;
481
482         ENTRY;
483
484         rc = llog_handle2ops(handle, &lop);
485         if (rc)
486                 RETURN(rc);
487         if (lop->lop_destroy == NULL)
488                 RETURN(-EOPNOTSUPP);
489
490         rc = lop->lop_destroy(env, handle);
491         RETURN(rc);
492 }
493
494 static inline int llog_next_block(const struct lu_env *env,
495                                   struct llog_handle *loghandle, int *cur_idx,
496                                   int next_idx, __u64 *cur_offset, void *buf,
497                                   int len)
498 {
499         struct llog_operations *lop;
500         int rc;
501
502         ENTRY;
503
504         rc = llog_handle2ops(loghandle, &lop);
505         if (rc)
506                 RETURN(rc);
507         if (lop->lop_next_block == NULL)
508                 RETURN(-EOPNOTSUPP);
509
510         rc = lop->lop_next_block(env, loghandle, cur_idx, next_idx,
511                                  cur_offset, buf, len);
512         RETURN(rc);
513 }
514
515 static inline int llog_prev_block(const struct lu_env *env,
516                                   struct llog_handle *loghandle,
517                                   int prev_idx, void *buf, int len)
518 {
519         struct llog_operations *lop;
520         int rc;
521
522         ENTRY;
523
524         rc = llog_handle2ops(loghandle, &lop);
525         if (rc)
526                 RETURN(rc);
527         if (lop->lop_prev_block == NULL)
528                 RETURN(-EOPNOTSUPP);
529
530         rc = lop->lop_prev_block(env, loghandle, prev_idx, buf, len);
531         RETURN(rc);
532 }
533
534 static inline int llog_connect(struct llog_ctxt *ctxt,
535                                struct llog_logid *logid, struct llog_gen *gen,
536                                struct obd_uuid *uuid)
537 {
538         struct llog_operations  *lop;
539         int                      rc;
540
541         ENTRY;
542
543         rc = llog_obd2ops(ctxt, &lop);
544         if (rc)
545                 RETURN(rc);
546         if (lop->lop_connect == NULL)
547                 RETURN(-EOPNOTSUPP);
548
549         rc = lop->lop_connect(ctxt, logid, gen, uuid);
550         RETURN(rc);
551 }
552
553 /* llog.c */
554 int llog_exist(struct llog_handle *loghandle);
555 int llog_declare_create(const struct lu_env *env,
556                         struct llog_handle *loghandle, struct thandle *th);
557 int llog_create(const struct lu_env *env, struct llog_handle *handle,
558                 struct thandle *th);
559 int llog_declare_write_rec(const struct lu_env *env,
560                            struct llog_handle *handle,
561                            struct llog_rec_hdr *rec, int idx,
562                            struct thandle *th);
563 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
564                    struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
565                    int numcookies, void *buf, int idx, struct thandle *th);
566 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
567              struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
568              void *buf, struct thandle *th);
569 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
570                      struct llog_rec_hdr *rec, struct thandle *th);
571 int lustre_process_log(struct super_block *sb, char *logname,
572                        struct config_llog_instance *cfg);
573 int lustre_end_log(struct super_block *sb, char *logname,
574                    struct config_llog_instance *cfg);
575 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
576                      struct llog_handle **res, struct llog_logid *logid,
577                      char *name);
578 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
579                struct llog_logid *logid, char *name);
580 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
581                struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
582                int cookiecount, void *buf, int idx);
583
584 /** @} log */
585
586 #endif