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