Whamcloud - gitweb
LU-2183 osp: cleanup osp-on-ost
[fs/lustre-release.git] / lustre / osp / osp_internal.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, 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/osp/osp_internal.h
37  *
38  * Author: Alex Zhuravlev <alexey.zhuravlev@intel.com>
39  */
40
41 #ifndef _OSP_INTERNAL_H
42 #define _OSP_INTERNAL_H
43
44 #include <obd.h>
45 #include <obd_class.h>
46 #include <dt_object.h>
47 #include <lustre_fid.h>
48
49 /*
50  * Infrastructure to support tracking of last committed llog record
51  */
52 struct osp_id_tracker {
53         spinlock_t               otr_lock;
54         __u32                    otr_next_id;
55         __u32                    otr_committed_id;
56         /* callback is register once per diskfs -- that's the whole point */
57         struct dt_txn_callback   otr_tx_cb;
58         /* single node can run many clusters */
59         cfs_list_t               otr_wakeup_list;
60         cfs_list_t               otr_list;
61         /* underlying shared device */
62         struct dt_device        *otr_dev;
63         /* how many users of this tracker */
64         cfs_atomic_t             otr_refcount;
65 };
66
67 struct osp_device {
68         struct dt_device                 opd_dt_dev;
69         /* corresponded OST index */
70         int                              opd_index;
71         /* device used to store persistent state (llogs, last ids) */
72         struct obd_export               *opd_storage_exp;
73         struct dt_device                *opd_storage;
74         struct dt_object                *opd_last_used_oid_file;
75         struct dt_object                *opd_last_used_seq_file;
76
77         /* stored persistently in LE format, updated directly to/from disk
78          * and required le64_to_cpu() conversion before use.
79          * Protected by opd_pre_lock */
80         struct lu_fid                   opd_last_used_fid;
81         struct lu_fid                   opd_gap_start_fid;
82         int                              opd_gap_count;
83         /* connection to OST */
84         struct obd_device               *opd_obd;
85         struct obd_export               *opd_exp;
86         struct obd_uuid                  opd_cluuid;
87         struct obd_connect_data         *opd_connect_data;
88         int                              opd_connects;
89         cfs_proc_dir_entry_t            *opd_proc_entry;
90         struct lprocfs_stats            *opd_stats;
91         /* connection status. */
92         int                              opd_new_connection;
93         int                              opd_got_disconnected;
94         int                              opd_imp_connected;
95         int                              opd_imp_active;
96         int                              opd_imp_seen_connected:1,
97                                          opd_connect_mdt:1;
98
99         /* whether local recovery is completed:
100          * reported via ->ldo_recovery_complete() */
101         int                              opd_recovery_completed;
102
103         /*
104          * Precreation pool
105          */
106         spinlock_t                       opd_pre_lock;
107
108         /* last fid to assign in creation */
109         struct lu_fid                    opd_pre_used_fid;
110         /* last created id OST reported, next-created - available id's */
111         struct lu_fid                    opd_pre_last_created_fid;
112         /* how many ids are reserved in declare, we shouldn't block in create */
113         __u64                            opd_pre_reserved;
114         /* dedicate precreate thread */
115         struct ptlrpc_thread             opd_pre_thread;
116         /* thread waits for signals about pool going empty */
117         cfs_waitq_t                      opd_pre_waitq;
118         /* consumers (who needs new ids) wait here */
119         cfs_waitq_t                      opd_pre_user_waitq;
120         /* current precreation status: working, failed, stopping? */
121         int                              opd_pre_status;
122         /* how many to precreate next time */
123         int                              opd_pre_grow_count;
124         int                              opd_pre_min_grow_count;
125         int                              opd_pre_max_grow_count;
126         /* whether to grow precreation window next time or not */
127         int                              opd_pre_grow_slow;
128         /* cleaning up orphans or recreating missing objects */
129         int                              opd_pre_recovering;
130
131         /*
132          * OST synchronization
133          */
134         spinlock_t                       opd_syn_lock;
135         /* unique generation, to recognize start of new records in the llog */
136         struct llog_gen                  opd_syn_generation;
137         /* number of changes to sync, used to wake up sync thread */
138         unsigned long                    opd_syn_changes;
139         /* processing of changes from previous mount is done? */
140         int                              opd_syn_prev_done;
141         /* found records */
142         struct ptlrpc_thread             opd_syn_thread;
143         cfs_waitq_t                      opd_syn_waitq;
144         /* list of remotely committed rpc */
145         cfs_list_t                       opd_syn_committed_there;
146         /* number of changes being under sync */
147         int                              opd_syn_sync_in_progress;
148         /* number of RPCs in flight - flow control */
149         int                              opd_syn_rpc_in_flight;
150         int                              opd_syn_max_rpc_in_flight;
151         /* number of RPC in processing (including non-committed by OST) */
152         int                              opd_syn_rpc_in_progress;
153         int                              opd_syn_max_rpc_in_progress;
154         /* osd api's commit cb control structure */
155         struct dt_txn_callback           opd_syn_txn_cb;
156         /* last used change number -- semantically similar to transno */
157         unsigned long                    opd_syn_last_used_id;
158         /* last committed change number -- semantically similar to
159          * last_committed */
160         unsigned long                    opd_syn_last_committed_id;
161         /* last processed (taken from llog) id */
162         unsigned long                    opd_syn_last_processed_id;
163         struct osp_id_tracker           *opd_syn_tracker;
164         cfs_list_t                       opd_syn_ontrack;
165
166         /*
167          * statfs related fields: OSP maintains it on its own
168          */
169         struct obd_statfs                opd_statfs;
170         cfs_time_t                       opd_statfs_fresh_till;
171         cfs_timer_t                      opd_statfs_timer;
172         int                              opd_statfs_update_in_progress;
173         /* how often to update statfs data */
174         int                              opd_statfs_maxage;
175
176         cfs_proc_dir_entry_t            *opd_symlink;
177 };
178
179 extern cfs_mem_cache_t *osp_object_kmem;
180
181 /* this is a top object */
182 struct osp_object {
183         struct lu_object_header  opo_header;
184         struct dt_object         opo_obj;
185         int                      opo_reserved:1,
186                                  opo_new:1;
187 };
188
189 extern struct lu_object_operations osp_lu_obj_ops;
190 extern const struct dt_device_operations osp_dt_ops;
191
192 struct osp_thread_info {
193         struct lu_buf            osi_lb;
194         struct lu_buf            osi_lb2;
195         struct lu_fid            osi_fid;
196         struct lu_attr           osi_attr;
197         struct ost_id            osi_oi;
198         struct ost_id            osi_oi2;
199         obd_id                   osi_id;
200         loff_t                   osi_off;
201         union {
202                 struct llog_rec_hdr             osi_hdr;
203                 struct llog_unlink64_rec        osi_unlink;
204                 struct llog_setattr64_rec       osi_setattr;
205                 struct llog_gen_rec             osi_gen;
206         };
207         struct llog_cookie       osi_cookie;
208         struct llog_catid        osi_cid;
209         struct lu_seq_range      osi_seq;
210 };
211
212 static inline void osp_objid_buf_prep(struct lu_buf *buf, loff_t *off,
213                                       __u32 *id, int index)
214 {
215         buf->lb_buf = (void *)id;
216         buf->lb_len = sizeof(obd_id);
217         *off = sizeof(obd_id) * index;
218 }
219
220 static inline void osp_objseq_buf_prep(struct lu_buf *buf, loff_t *off,
221                                        __u64 *seq, int index)
222 {
223         buf->lb_buf = (void *)seq;
224         buf->lb_len = sizeof(obd_id);
225         *off = sizeof(obd_id) * index;
226 }
227
228 static inline void osp_buf_prep(struct lu_buf *lb, void *buf, int buf_len)
229 {
230         lb->lb_buf = buf;
231         lb->lb_len = buf_len;
232 }
233
234 extern struct lu_context_key osp_thread_key;
235
236 static inline struct osp_thread_info *osp_env_info(const struct lu_env *env)
237 {
238         struct osp_thread_info *info;
239
240         info = lu_context_key_get(&env->le_ctx, &osp_thread_key);
241         if (info == NULL) {
242                 lu_env_refill((struct lu_env *)env);
243                 info = lu_context_key_get(&env->le_ctx, &osp_thread_key);
244         }
245         LASSERT(info);
246         return info;
247 }
248
249 struct osp_txn_info {
250         __u32   oti_current_id;
251 };
252
253 extern struct lu_context_key osp_txn_key;
254
255 static inline struct osp_txn_info *osp_txn_info(struct lu_context *ctx)
256 {
257         struct osp_txn_info *info;
258
259         info = lu_context_key_get(ctx, &osp_txn_key);
260         return info;
261 }
262
263 extern const struct lu_device_operations osp_lu_ops;
264
265 static inline int lu_device_is_osp(struct lu_device *d)
266 {
267         return ergo(d != NULL && d->ld_ops != NULL, d->ld_ops == &osp_lu_ops);
268 }
269
270 static inline struct osp_device *lu2osp_dev(struct lu_device *d)
271 {
272         LASSERT(lu_device_is_osp(d));
273         return container_of0(d, struct osp_device, opd_dt_dev.dd_lu_dev);
274 }
275
276 static inline struct lu_device *osp2lu_dev(struct osp_device *d)
277 {
278         return &d->opd_dt_dev.dd_lu_dev;
279 }
280
281 static inline struct osp_device *dt2osp_dev(struct dt_device *d)
282 {
283         LASSERT(lu_device_is_osp(&d->dd_lu_dev));
284         return container_of0(d, struct osp_device, opd_dt_dev);
285 }
286
287 static inline struct osp_object *lu2osp_obj(struct lu_object *o)
288 {
289         LASSERT(ergo(o != NULL, lu_device_is_osp(o->lo_dev)));
290         return container_of0(o, struct osp_object, opo_obj.do_lu);
291 }
292
293 static inline struct lu_object *osp2lu_obj(struct osp_object *obj)
294 {
295         return &obj->opo_obj.do_lu;
296 }
297
298 static inline struct osp_object *osp_obj(const struct lu_object *o)
299 {
300         LASSERT(lu_device_is_osp(o->lo_dev));
301         return container_of0(o, struct osp_object, opo_obj.do_lu);
302 }
303
304 static inline struct osp_object *dt2osp_obj(const struct dt_object *d)
305 {
306         return osp_obj(&d->do_lu);
307 }
308
309 static inline struct dt_object *osp_object_child(struct osp_object *o)
310 {
311         return container_of0(lu_object_next(osp2lu_obj(o)),
312                              struct dt_object, do_lu);
313 }
314
315 #define osp_init_rpc_lock(lck) mdc_init_rpc_lock(lck)
316 #define osp_get_rpc_lock(lck, it)  mdc_get_rpc_lock(lck, it)
317 #define osp_put_rpc_lock(lck, it) mdc_put_rpc_lock(lck, it)
318
319 static inline void osp_update_last_fid(struct osp_device *d, struct lu_fid *fid)
320 {
321         int diff = lu_fid_diff(fid, &d->opd_last_used_fid);
322         /*
323          * we might have lost precreated objects due to VBR and precreate
324          * orphans, the gap in objid can be calculated properly only here
325          */
326         if (diff > 0) {
327                 if (diff > 1) {
328                         d->opd_gap_start_fid = d->opd_last_used_fid;
329                         d->opd_gap_start_fid.f_oid++;
330                         d->opd_gap_count = diff - 1;
331                         CDEBUG(D_HA, "Gap in objids: start="DFID", count =%d\n",
332                                PFID(&d->opd_gap_start_fid), d->opd_gap_count);
333                 }
334                 d->opd_last_used_fid = *fid;
335         }
336 }
337
338 static int osp_fid_end_seq(const struct lu_env *env, struct lu_fid *fid)
339 {
340         if (fid_is_idif(fid)) {
341                 struct osp_thread_info *info = osp_env_info(env);
342                 struct ost_id *oi = &info->osi_oi;
343
344                 fid_ostid_pack(fid, oi);
345                 return oi->oi_id == IDIF_MAX_OID;
346         } else {
347                 return fid_oid(fid) == LUSTRE_DATA_SEQ_MAX_WIDTH;
348         }
349 }
350
351 static inline int osp_precreate_end_seq_nolock(const struct lu_env *env,
352                                                struct osp_device *osp)
353 {
354         struct lu_fid *fid = &osp->opd_pre_last_created_fid;
355
356         return osp_fid_end_seq(env, fid);
357 }
358
359 static inline int osp_precreate_end_seq(const struct lu_env *env,
360                                         struct osp_device *osp)
361 {
362         int rc;
363
364         spin_lock(&osp->opd_pre_lock);
365         rc = osp_precreate_end_seq_nolock(env, osp);
366         spin_unlock(&osp->opd_pre_lock);
367         return rc;
368 }
369
370 static inline int osp_is_fid_client(struct osp_device *osp)
371 {
372         struct obd_import *imp = osp->opd_obd->u.cli.cl_import;
373
374         return imp->imp_connect_data.ocd_connect_flags & OBD_CONNECT_FID;
375 }
376
377 /* osp_dev.c */
378 void osp_update_last_id(struct osp_device *d, obd_id objid);
379 extern struct llog_operations osp_mds_ost_orig_logops;
380
381 /* osp_precreate.c */
382 int osp_init_precreate(struct osp_device *d);
383 int osp_precreate_reserve(const struct lu_env *env, struct osp_device *d);
384 __u64 osp_precreate_get_id(struct osp_device *d);
385 int osp_precreate_get_fid(const struct lu_env *env, struct osp_device *d,
386                           struct lu_fid *fid);
387 void osp_precreate_fini(struct osp_device *d);
388 int osp_object_truncate(const struct lu_env *env, struct dt_object *dt, __u64);
389 void osp_pre_update_status(struct osp_device *d, int rc);
390 void osp_statfs_need_now(struct osp_device *d);
391 int osp_reset_last_used(const struct lu_env *env, struct osp_device *osp);
392 int osp_write_last_oid_seq_files(struct lu_env *env, struct osp_device *osp,
393                                  struct lu_fid *fid, int sync);
394
395 /* lproc_osp.c */
396 void lprocfs_osp_init_vars(struct lprocfs_static_vars *lvars);
397 void osp_lprocfs_init(struct osp_device *osp);
398
399 /* osp_sync.c */
400 int osp_sync_declare_add(const struct lu_env *env, struct osp_object *o,
401                          llog_op_type type, struct thandle *th);
402 int osp_sync_add(const struct lu_env *env, struct osp_object *o,
403                  llog_op_type type, struct thandle *th,
404                  const struct lu_attr *attr);
405 int osp_sync_init(const struct lu_env *env, struct osp_device *d);
406 int osp_sync_fini(struct osp_device *d);
407 void __osp_sync_check_for_work(struct osp_device *d);
408
409 /* lwp_dev.c */
410 void lprocfs_lwp_init_vars(struct lprocfs_static_vars *lvars);
411 extern struct obd_ops lwp_obd_device_ops;
412 extern struct lu_device_type lwp_device_type;
413
414 #endif