lguest: Read offset of device_cap later
[linux-2.6-block.git] / drivers / staging / lustre / 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, 2015, 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 #include "obd_class.h"
60 #include "lustre/lustre_idl.h"
61
62 #define LOG_NAME_LIMIT(logname, name)              \
63         snprintf(logname, sizeof(logname), "LOGS/%s", name)
64 #define LLOG_EEMPTY 4711
65
66 enum llog_open_param {
67         LLOG_OPEN_EXISTS        = 0x0000,
68         LLOG_OPEN_NEW           = 0x0001,
69 };
70
71 struct plain_handle_data {
72         struct list_head          phd_entry;
73         struct llog_handle *phd_cat_handle;
74         struct llog_cookie  phd_cookie; /* cookie of this log in its cat */
75 };
76
77 struct cat_handle_data {
78         struct list_head              chd_head;
79         struct llog_handle     *chd_current_log; /* currently open log */
80         struct llog_handle      *chd_next_log; /* llog to be used next */
81 };
82
83 struct llog_handle;
84
85 /* llog.c  -  general API */
86 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
87                      int flags, struct obd_uuid *uuid);
88 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
89                  llog_cb_t cb, void *data, void *catdata);
90 int llog_process_or_fork(const struct lu_env *env,
91                          struct llog_handle *loghandle,
92                          llog_cb_t cb, void *data, void *catdata, bool fork);
93 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
94               struct llog_handle **lgh, struct llog_logid *logid,
95               char *name, enum llog_open_param open_param);
96 int llog_close(const struct lu_env *env, struct llog_handle *cathandle);
97
98 /* llog_process flags */
99 #define LLOG_FLAG_NODEAMON 0x0001
100
101 /* llog_cat.c - catalog api */
102 struct llog_process_data {
103         /**
104          * Any useful data needed while processing catalog. This is
105          * passed later to process callback.
106          */
107         void            *lpd_data;
108         /**
109          * Catalog process callback function, called for each record
110          * in catalog.
111          */
112         llog_cb_t           lpd_cb;
113         /**
114          * Start processing the catalog from startcat/startidx
115          */
116         int               lpd_startcat;
117         int               lpd_startidx;
118 };
119
120 struct llog_process_cat_data {
121         /**
122          * Temporary stored first_idx while scanning log.
123          */
124         int               lpcd_first_idx;
125         /**
126          * Temporary stored last_idx while scanning log.
127          */
128         int               lpcd_last_idx;
129 };
130
131 struct thandle;
132
133 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle);
134 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
135                      llog_cb_t cb, void *data, int startcat, int startidx);
136
137 /* llog_obd.c */
138 int llog_setup(const struct lu_env *env, struct obd_device *obd,
139                struct obd_llog_group *olg, int index,
140                struct obd_device *disk_obd, struct llog_operations *op);
141 int __llog_ctxt_put(const struct lu_env *env, struct llog_ctxt *ctxt);
142 int llog_cleanup(const struct lu_env *env, struct llog_ctxt *);
143
144 /* llog_net.c */
145 int llog_initiator_connect(struct llog_ctxt *ctxt);
146
147 struct llog_operations {
148         int (*lop_next_block)(const struct lu_env *env, struct llog_handle *h,
149                               int *curr_idx, int next_idx, __u64 *offset,
150                               void *buf, int len);
151         int (*lop_prev_block)(const struct lu_env *env, struct llog_handle *h,
152                               int prev_idx, void *buf, int len);
153         int (*lop_read_header)(const struct lu_env *env,
154                                struct llog_handle *handle);
155         int (*lop_setup)(const struct lu_env *env, struct obd_device *obd,
156                          struct obd_llog_group *olg, int ctxt_idx,
157                          struct obd_device *disk_obd);
158         int (*lop_sync)(struct llog_ctxt *ctxt, struct obd_export *exp,
159                         int flags);
160         int (*lop_cleanup)(const struct lu_env *env, struct llog_ctxt *ctxt);
161         int (*lop_cancel)(const struct lu_env *env, struct llog_ctxt *ctxt,
162                           struct llog_cookie *cookies, int flags);
163         int (*lop_connect)(struct llog_ctxt *ctxt, struct llog_logid *logid,
164                            struct llog_gen *gen, struct obd_uuid *uuid);
165         /**
166          * Any llog file must be opened first using llog_open().  Llog can be
167          * opened by name, logid or without both, in last case the new logid
168          * will be generated.
169          */
170         int (*lop_open)(const struct lu_env *env, struct llog_handle *lgh,
171                         struct llog_logid *logid, char *name,
172                         enum llog_open_param);
173         /**
174          * Opened llog may not exist and this must be checked where needed using
175          * the llog_exist() call.
176          */
177         int (*lop_exist)(struct llog_handle *lgh);
178         /**
179          * Close llog file and calls llog_free_handle() implicitly.
180          * Any opened llog must be closed by llog_close() call.
181          */
182         int (*lop_close)(const struct lu_env *env, struct llog_handle *handle);
183         /**
184          * Create new llog file. The llog must be opened.
185          * Must be used only for local llog operations.
186          */
187         int (*lop_declare_create)(const struct lu_env *env,
188                                   struct llog_handle *handle,
189                                   struct thandle *th);
190         /**
191          * write new record in llog. It appends records usually but can edit
192          * existing records too.
193          */
194         int (*lop_declare_write_rec)(const struct lu_env *env,
195                                      struct llog_handle *lgh,
196                                      struct llog_rec_hdr *rec,
197                                      int idx, struct thandle *th);
198         int (*lop_write_rec)(const struct lu_env *env,
199                              struct llog_handle *loghandle,
200                              struct llog_rec_hdr *rec,
201                              struct llog_cookie *cookie, int cookiecount,
202                              void *buf, int idx, struct thandle *th);
203         /**
204          * Add new record in llog catalog. Does the same as llog_write_rec()
205          * but using llog catalog.
206          */
207         int (*lop_declare_add)(const struct lu_env *env,
208                                struct llog_handle *lgh,
209                                struct llog_rec_hdr *rec, struct thandle *th);
210         int (*lop_add)(const struct lu_env *env, struct llog_handle *lgh,
211                        struct llog_rec_hdr *rec, struct llog_cookie *cookie,
212                        void *buf, struct thandle *th);
213 };
214
215 /* In-memory descriptor for a log object or log catalog */
216 struct llog_handle {
217         struct rw_semaphore      lgh_lock;
218         spinlock_t               lgh_hdr_lock; /* protect lgh_hdr data */
219         struct llog_logid        lgh_id; /* id of this log */
220         struct llog_log_hdr     *lgh_hdr;
221         int                      lgh_last_idx;
222         int                      lgh_cur_idx; /* used during llog_process */
223         __u64                    lgh_cur_offset; /* used during llog_process */
224         struct llog_ctxt        *lgh_ctxt;
225         union {
226                 struct plain_handle_data         phd;
227                 struct cat_handle_data           chd;
228         } u;
229         char                    *lgh_name;
230         void                    *private_data;
231         struct llog_operations  *lgh_logops;
232         atomic_t                 lgh_refcount;
233 };
234
235 #define LLOG_CTXT_FLAG_UNINITIALIZED     0x00000001
236 #define LLOG_CTXT_FLAG_STOP              0x00000002
237
238 struct llog_ctxt {
239         int                   loc_idx; /* my index the obd array of ctxt's */
240         struct obd_device       *loc_obd; /* points back to the containing obd*/
241         struct obd_llog_group   *loc_olg; /* group containing that ctxt */
242         struct obd_export       *loc_exp; /* parent "disk" export (e.g. MDS) */
243         struct obd_import       *loc_imp; /* to use in RPC's: can be backward
244                                            * pointing import
245                                            */
246         struct llog_operations  *loc_logops;
247         struct llog_handle      *loc_handle;
248         struct mutex             loc_mutex; /* protect loc_imp */
249         atomic_t             loc_refcount;
250         long                 loc_flags; /* flags, see above defines */
251 };
252
253 #define LLOG_PROC_BREAK 0x0001
254 #define LLOG_DEL_RECORD 0x0002
255
256 static inline int llog_handle2ops(struct llog_handle *loghandle,
257                                   struct llog_operations **lop)
258 {
259         if (!loghandle || !loghandle->lgh_logops)
260                 return -EINVAL;
261
262         *lop = loghandle->lgh_logops;
263         return 0;
264 }
265
266 static inline struct llog_ctxt *llog_ctxt_get(struct llog_ctxt *ctxt)
267 {
268         atomic_inc(&ctxt->loc_refcount);
269         CDEBUG(D_INFO, "GETting ctxt %p : new refcount %d\n", ctxt,
270                atomic_read(&ctxt->loc_refcount));
271         return ctxt;
272 }
273
274 static inline void llog_ctxt_put(struct llog_ctxt *ctxt)
275 {
276         if (!ctxt)
277                 return;
278         LASSERT_ATOMIC_GT_LT(&ctxt->loc_refcount, 0, LI_POISON);
279         CDEBUG(D_INFO, "PUTting ctxt %p : new refcount %d\n", ctxt,
280                atomic_read(&ctxt->loc_refcount) - 1);
281         __llog_ctxt_put(NULL, ctxt);
282 }
283
284 static inline void llog_group_init(struct obd_llog_group *olg, int group)
285 {
286         init_waitqueue_head(&olg->olg_waitq);
287         spin_lock_init(&olg->olg_lock);
288         mutex_init(&olg->olg_cat_processing);
289         olg->olg_seq = group;
290 }
291
292 static inline int llog_group_set_ctxt(struct obd_llog_group *olg,
293                                       struct llog_ctxt *ctxt, int index)
294 {
295         LASSERT(index >= 0 && index < LLOG_MAX_CTXTS);
296
297         spin_lock(&olg->olg_lock);
298         if (olg->olg_ctxts[index]) {
299                 spin_unlock(&olg->olg_lock);
300                 return -EEXIST;
301         }
302         olg->olg_ctxts[index] = ctxt;
303         spin_unlock(&olg->olg_lock);
304         return 0;
305 }
306
307 static inline struct llog_ctxt *llog_group_get_ctxt(struct obd_llog_group *olg,
308                                                     int index)
309 {
310         struct llog_ctxt *ctxt;
311
312         LASSERT(index >= 0 && index < LLOG_MAX_CTXTS);
313
314         spin_lock(&olg->olg_lock);
315         if (!olg->olg_ctxts[index])
316                 ctxt = NULL;
317         else
318                 ctxt = llog_ctxt_get(olg->olg_ctxts[index]);
319         spin_unlock(&olg->olg_lock);
320         return ctxt;
321 }
322
323 static inline void llog_group_clear_ctxt(struct obd_llog_group *olg, int index)
324 {
325         LASSERT(index >= 0 && index < LLOG_MAX_CTXTS);
326         spin_lock(&olg->olg_lock);
327         olg->olg_ctxts[index] = NULL;
328         spin_unlock(&olg->olg_lock);
329 }
330
331 static inline struct llog_ctxt *llog_get_context(struct obd_device *obd,
332                                                  int index)
333 {
334         return llog_group_get_ctxt(&obd->obd_olg, index);
335 }
336
337 static inline int llog_group_ctxt_null(struct obd_llog_group *olg, int index)
338 {
339         return (!olg->olg_ctxts[index]);
340 }
341
342 static inline int llog_ctxt_null(struct obd_device *obd, int index)
343 {
344         return llog_group_ctxt_null(&obd->obd_olg, index);
345 }
346
347 static inline int llog_next_block(const struct lu_env *env,
348                                   struct llog_handle *loghandle, int *cur_idx,
349                                   int next_idx, __u64 *cur_offset, void *buf,
350                                   int len)
351 {
352         struct llog_operations *lop;
353         int rc;
354
355         rc = llog_handle2ops(loghandle, &lop);
356         if (rc)
357                 return rc;
358         if (!lop->lop_next_block)
359                 return -EOPNOTSUPP;
360
361         rc = lop->lop_next_block(env, loghandle, cur_idx, next_idx,
362                                  cur_offset, buf, len);
363         return rc;
364 }
365
366 /* llog.c */
367 int llog_declare_write_rec(const struct lu_env *env,
368                            struct llog_handle *handle,
369                            struct llog_rec_hdr *rec, int idx,
370                            struct thandle *th);
371 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
372                    struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
373                    int numcookies, void *buf, int idx, struct thandle *th);
374 int lustre_process_log(struct super_block *sb, char *logname,
375                        struct config_llog_instance *cfg);
376 int lustre_end_log(struct super_block *sb, char *logname,
377                    struct config_llog_instance *cfg);
378 /** @} log */
379
380 #endif