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