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