Whamcloud - gitweb
b=19778
[fs/lustre-release.git] / lustre / include / obd_class.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #ifndef __CLASS_OBD_H
38 #define __CLASS_OBD_H
39
40 #include <obd_support.h>
41 #include <lustre_import.h>
42 #include <lustre_net.h>
43 #include <obd.h>
44 #include <lustre_lib.h>
45 #include <lustre/lustre_idl.h>
46 #include <lprocfs_status.h>
47
48 #if defined(__linux__)
49 #include <linux/obd_class.h>
50 #elif defined(__APPLE__)
51 #include <darwin/obd_class.h>
52 #elif defined(__WINNT__)
53 #include <winnt/obd_class.h>
54 #else
55 #error Unsupported operating system.
56 #endif
57
58 /* OBD Device Declarations */
59 extern struct obd_device *obd_devs[MAX_OBD_DEVICES];
60 extern spinlock_t obd_dev_lock;
61 extern cfs_mem_cache_t *obd_lvfs_ctxt_cache;
62
63 /* OBD Operations Declarations */
64 extern struct obd_device *class_conn2obd(struct lustre_handle *);
65 extern struct obd_device *class_exp2obd(struct obd_export *);
66
67 /* genops.c */
68 struct obd_export *class_conn2export(struct lustre_handle *);
69 int class_register_type(struct obd_ops *ops, struct lprocfs_vars *,
70                         const char *nm);
71 int class_unregister_type(const char *nm);
72
73 struct obd_device *class_newdev(const char *type_name, const char *name);
74 void class_release_dev(struct obd_device *obd);
75
76 int class_name2dev(const char *name);
77 struct obd_device *class_name2obd(const char *name);
78 int class_uuid2dev(struct obd_uuid *uuid);
79 struct obd_device *class_uuid2obd(struct obd_uuid *uuid);
80 void class_obd_list(void);
81 struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
82                                           const char * typ_name,
83                                           struct obd_uuid *grp_uuid);
84 struct obd_device * class_find_client_notype(struct obd_uuid *tgt_uuid,
85                                              struct obd_uuid *grp_uuid);
86 struct obd_device * class_devices_in_group(struct obd_uuid *grp_uuid,
87                                            int *next);
88 struct obd_device * class_num2obd(int num);
89
90 int oig_init(struct obd_io_group **oig);
91 int oig_add_one(struct obd_io_group *oig, struct oig_callback_context *occ);
92 void oig_complete_one(struct obd_io_group *oig,
93                       struct oig_callback_context *occ, int rc);
94 void oig_release(struct obd_io_group *oig);
95 int oig_wait(struct obd_io_group *oig);
96
97 char *obd_export_nid2str(struct obd_export *exp);
98
99 int obd_export_evict_by_nid(struct obd_device *obd, char *nid);
100 int obd_export_evict_by_uuid(struct obd_device *obd, char *uuid);
101
102 int obd_zombie_impexp_init(void);
103 void obd_zombie_impexp_stop(void);
104 void obd_zombie_impexp_cull(void);
105
106 /* obd_config.c */
107 int class_process_config(struct lustre_cfg *lcfg);
108 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars,
109                              struct lustre_cfg *lcfg, void *data);
110 int class_attach(struct lustre_cfg *lcfg);
111 int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg);
112 int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg);
113 int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg);
114 struct obd_device *class_incref(struct obd_device *obd);
115 void class_decref(struct obd_device *obd);
116
117 /*obdecho*/
118 #ifdef LPROCFS
119 extern void lprocfs_echo_init_vars(struct lprocfs_static_vars *lvars);
120 #else
121 static inline void lprocfs_echo_init_vars(struct lprocfs_static_vars *lvars)
122 {
123         memset(lvars, 0, sizeof(*lvars));
124 }
125 #endif
126
127 #define CFG_F_START     0x01   /* Set when we start updating from a log */
128 #define CFG_F_MARKER    0x02   /* We are within a maker */
129 #define CFG_F_SKIP      0x04   /* We should ignore this cfg command */
130 #define CFG_F_COMPAT146 0x08   /* Allow old-style logs */
131 #define CFG_F_EXCLUDE   0x10   /* OST exclusion list */
132
133 /* Passed as data param to class_config_parse_llog */
134 struct config_llog_instance {
135         char *              cfg_instance;
136         struct super_block *cfg_sb;
137         struct obd_uuid     cfg_uuid;
138         int                 cfg_last_idx; /* for partial llog processing */
139         int                 cfg_flags;
140 };
141 int class_config_parse_llog(struct llog_ctxt *ctxt, char *name,
142                             struct config_llog_instance *cfg);
143 int class_config_dump_llog(struct llog_ctxt *ctxt, char *name,
144                            struct config_llog_instance *cfg);
145
146 /* list of active configuration logs  */
147 struct config_llog_data {
148         char               *cld_logname;
149         struct ldlm_res_id  cld_resid;
150         struct config_llog_instance cld_cfg;
151         struct list_head    cld_list_chain;
152         atomic_t            cld_refcount;
153         struct obd_export  *cld_mgcexp;
154         unsigned int        cld_stopping:1; /* we were told to stop watching */
155         unsigned int        cld_lostlock:1; /* lock not requeued */
156 };
157
158 struct lustre_profile {
159         struct list_head lp_list;
160         char * lp_profile;
161         char * lp_osc;
162         char * lp_mdc;
163 };
164
165 struct lustre_profile *class_get_profile(char * prof);
166 void class_del_profile(char *prof);
167 void class_del_profiles(void);
168
169 #define class_export_rpc_get(exp)                                       \
170 ({                                                                      \
171         atomic_inc(&(exp)->exp_rpc_count);                              \
172         CDEBUG(D_INFO, "RPC GETting export %p : new rpc_count %d\n",    \
173                (exp), atomic_read(&(exp)->exp_rpc_count));              \
174         class_export_get(exp);                                          \
175 })
176
177 #define class_export_rpc_put(exp)                                       \
178 ({                                                                      \
179         atomic_dec(&(exp)->exp_rpc_count);                              \
180         CDEBUG(D_INFO, "RPC PUTting export %p : new rpc_count %d\n",    \
181                (exp), atomic_read(&(exp)->exp_rpc_count));              \
182         class_export_put(exp);                                          \
183 })
184
185 /* genops.c */
186 #define class_export_get(exp)                                                  \
187 ({                                                                             \
188         struct obd_export *exp_ = exp;                                         \
189         atomic_inc(&exp_->exp_refcount);                                       \
190         CDEBUG(D_INFO, "GETting export %p : new refcount %d\n", exp_,          \
191                atomic_read(&exp_->exp_refcount));                              \
192         exp_;                                                                  \
193 })
194
195 /* class_export_put() is non-blocking */
196 #define class_export_put(exp)                                                  \
197 do {                                                                           \
198         LASSERT((exp) != NULL);                                                \
199         CDEBUG(D_INFO, "PUTting export %p : new refcount %d\n", (exp),         \
200                atomic_read(&(exp)->exp_refcount) - 1);                         \
201         LASSERT(atomic_read(&(exp)->exp_refcount) > 0);                        \
202         LASSERT(atomic_read(&(exp)->exp_refcount) < 0x5a5a5a);                 \
203         __class_export_put(exp);                                               \
204 } while (0)
205
206 void __class_export_put(struct obd_export *);
207 struct obd_export *class_new_export(struct obd_device *obddev,
208                                     struct obd_uuid *cluuid);
209 void class_unlink_export(struct obd_export *exp);
210
211 struct obd_import *class_import_get(struct obd_import *);
212 void class_import_put(struct obd_import *);
213 struct obd_import *class_new_import(struct obd_device *obd);
214 void class_destroy_import(struct obd_import *exp);
215
216 struct obd_type *class_search_type(const char *name);
217 struct obd_type *class_get_type(const char *name);
218 void class_put_type(struct obd_type *type);
219 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
220                   struct obd_uuid *cluuid);
221 int class_disconnect(struct obd_export *exp);
222 void class_fail_export(struct obd_export *exp);
223 void class_disconnect_exports(struct obd_device *obddev);
224 void class_set_export_delayed(struct obd_export *exp);
225 void class_handle_stale_exports(struct obd_device *obddev);
226 void class_disconnect_expired_exports(struct obd_device *obd);
227 void class_disconnect_stale_exports(struct obd_device *obddev,
228                                     enum obd_option flags);
229 int class_stale_export_list(struct obd_device *obd, struct obd_ioctl_data *data);
230 int class_manual_cleanup(struct obd_device *obd);
231
232 static inline enum obd_option exp_flags_from_obd(struct obd_device *obd)
233 {
234         return ((obd->obd_fail ? OBD_OPT_FAILOVER : 0) |
235                 (obd->obd_force ? OBD_OPT_FORCE : 0) |
236                 (obd->obd_abort_recovery ? OBD_OPT_ABORT_RECOV : 0) |
237                 0);
238 }
239
240 /* obdo.c */
241 void obdo_cpy_md(struct obdo *dst, struct obdo *src, obd_flag valid);
242 void obdo_to_ioobj(struct obdo *oa, struct obd_ioobj *ioobj);
243
244
245 #define OBT(dev)        (dev)->obd_type
246 #define OBP(dev, op)    (dev)->obd_type->typ_ops->o_ ## op
247 #define CTXTP(ctxt, op) (ctxt)->loc_logops->lop_##op
248
249 /* Ensure obd_setup: used for cleanup which must be called
250    while obd is stopping */
251 #define OBD_CHECK_DEV(obd)                                      \
252 do {                                                            \
253         if (!(obd)) {                                           \
254                 CERROR("NULL device\n");                        \
255                 RETURN(-ENODEV);                                \
256         }                                                       \
257 } while (0)
258
259 /* ensure obd_setup and !obd_stopping */
260 #define OBD_CHECK_DEV_ACTIVE(obd)                               \
261 do {                                                            \
262         OBD_CHECK_DEV(obd);                                     \
263         if (!(obd)->obd_set_up || (obd)->obd_stopping) {        \
264                 CERROR("Device %d not setup\n",                 \
265                        (obd)->obd_minor);                       \
266                 RETURN(-ENODEV);                                \
267         }                                                       \
268 } while (0)
269
270
271 #ifdef LPROCFS
272 #define OBD_COUNTER_OFFSET(op)                                  \
273         ((offsetof(struct obd_ops, o_ ## op) -                  \
274           offsetof(struct obd_ops, o_iocontrol))                \
275          / sizeof(((struct obd_ops *)(0))->o_iocontrol))
276
277 #define OBD_COUNTER_INCREMENT(obdx, op)                           \
278         if ((obdx)->obd_stats != NULL) {                          \
279                 unsigned int coffset;                             \
280                 coffset = (unsigned int)((obdx)->obd_cntr_base) + \
281                         OBD_COUNTER_OFFSET(op);                   \
282                 LASSERT(coffset < (obdx)->obd_stats->ls_num);     \
283                 lprocfs_counter_incr((obdx)->obd_stats, coffset); \
284         }
285
286 #define EXP_COUNTER_INCREMENT(export, op)                                    \
287         if ((export)->exp_obd->obd_stats != NULL) {                          \
288                 unsigned int coffset;                                        \
289                 coffset = (unsigned int)((export)->exp_obd->obd_cntr_base) + \
290                         OBD_COUNTER_OFFSET(op);                              \
291                 LASSERT(coffset < (export)->exp_obd->obd_stats->ls_num);     \
292                 lprocfs_counter_incr((export)->exp_obd->obd_stats, coffset); \
293                 if ((export)->exp_nid_stats != NULL &&                       \
294                     (export)->exp_nid_stats->nid_stats != NULL)              \
295                         lprocfs_counter_incr(                                \
296                                 (export)->exp_nid_stats->nid_stats, coffset);\
297         }
298
299 #else
300 #define OBD_COUNTER_OFFSET(op)
301 #define OBD_COUNTER_INCREMENT(obd, op)
302 #define EXP_COUNTER_INCREMENT(exp, op);
303 #endif
304
305 #define OBD_CHECK_OP(obd, op, err)                              \
306 do {                                                            \
307         if (!OBT(obd) || !OBP((obd), op)) {\
308                 if (err)                                        \
309                         CERROR("obd_" #op ": dev %d no operation\n",    \
310                                obd->obd_minor);                         \
311                 RETURN(err);                                    \
312         }                                                       \
313 } while (0)
314
315 #define EXP_CHECK_OP(exp, op)                                   \
316 do {                                                            \
317         if ((exp) == NULL) {                                    \
318                 CERROR("obd_" #op ": NULL export\n");           \
319                 RETURN(-ENODEV);                                \
320         }                                                       \
321         if ((exp)->exp_obd == NULL || !OBT((exp)->exp_obd)) {   \
322                 CERROR("obd_" #op ": cleaned up obd\n");        \
323                 RETURN(-EOPNOTSUPP);                            \
324         }                                                       \
325         if (!OBT((exp)->exp_obd) || !OBP((exp)->exp_obd, op)) { \
326                 CERROR("obd_" #op ": dev %d no operation\n",    \
327                        (exp)->exp_obd->obd_minor);              \
328                 RETURN(-EOPNOTSUPP);                            \
329         }                                                       \
330 } while (0)
331
332 #define CTXT_CHECK_OP(ctxt, op, err)                                         \
333 do {                                                            \
334         if (!OBT(ctxt->loc_obd) || !CTXTP((ctxt), op)) {                     \
335                 if (err)                                        \
336                         CERROR("lop_" #op ": dev %d no operation\n",    \
337                                ctxt->loc_obd->obd_minor);                         \
338                 RETURN(err);                                    \
339         }                                                       \
340 } while (0)
341
342 static inline int class_devno_max(void)
343 {
344         return MAX_OBD_DEVICES;
345 }
346
347 static inline int obd_get_info(struct obd_export *exp, __u32 keylen,
348                                void *key, __u32 *vallen, void *val,
349                                struct lov_stripe_md *lsm)
350 {
351         int rc;
352         ENTRY;
353
354         EXP_CHECK_OP(exp, get_info);
355         EXP_COUNTER_INCREMENT(exp, get_info);
356
357         rc = OBP(exp->exp_obd, get_info)(exp, keylen, key, vallen, val, lsm);
358         RETURN(rc);
359 }
360
361 static inline int obd_set_info_async(struct obd_export *exp, obd_count keylen,
362                                      void *key, obd_count vallen, void *val,
363                                      struct ptlrpc_request_set *set)
364 {
365         int rc;
366         ENTRY;
367
368         EXP_CHECK_OP(exp, set_info_async);
369         EXP_COUNTER_INCREMENT(exp, set_info_async);
370
371         rc = OBP(exp->exp_obd, set_info_async)(exp, keylen, key, vallen, val,
372                                                set);
373         RETURN(rc);
374 }
375
376 static inline int obd_setup(struct obd_device *obd, int datalen, void *data)
377 {
378         int rc;
379         ENTRY;
380
381         OBD_CHECK_OP(obd, setup, -EOPNOTSUPP);
382         OBD_COUNTER_INCREMENT(obd, setup);
383
384         rc = OBP(obd, setup)(obd, datalen, data);
385         RETURN(rc);
386 }
387
388 static inline int obd_precleanup(struct obd_device *obd,
389                                  enum obd_cleanup_stage cleanup_stage)
390 {
391         int rc;
392         ENTRY;
393
394         OBD_CHECK_OP(obd, precleanup, 0);
395         OBD_COUNTER_INCREMENT(obd, precleanup);
396
397         rc = OBP(obd, precleanup)(obd, cleanup_stage);
398         RETURN(rc);
399 }
400
401 static inline int obd_cleanup(struct obd_device *obd)
402 {
403         int rc;
404         ENTRY;
405
406         OBD_CHECK_DEV(obd);
407         OBD_CHECK_OP(obd, cleanup, 0);
408         OBD_COUNTER_INCREMENT(obd, cleanup);
409
410         rc = OBP(obd, cleanup)(obd);
411         RETURN(rc);
412 }
413
414 static inline int
415 obd_process_config(struct obd_device *obd, int datalen, void *data)
416 {
417         int rc;
418         ENTRY;
419
420         OBD_CHECK_OP(obd, process_config, -EOPNOTSUPP);
421         OBD_COUNTER_INCREMENT(obd, process_config);
422
423         rc = OBP(obd, process_config)(obd, datalen, data);
424         RETURN(rc);
425 }
426
427 /* Pack an in-memory MD struct for storage on disk.
428  * Returns +ve size of packed MD (0 for free), or -ve error.
429  *
430  * If @disk_tgt == NULL, MD size is returned (max size if @mem_src == NULL).
431  * If @*disk_tgt != NULL and @mem_src == NULL, @*disk_tgt will be freed.
432  * If @*disk_tgt == NULL, it will be allocated
433  */
434 static inline int obd_packmd(struct obd_export *exp,
435                              struct lov_mds_md **disk_tgt,
436                              struct lov_stripe_md *mem_src)
437 {
438         int rc;
439         ENTRY;
440
441         EXP_CHECK_OP(exp, packmd);
442         EXP_COUNTER_INCREMENT(exp, packmd);
443
444         rc = OBP(exp->exp_obd, packmd)(exp, disk_tgt, mem_src);
445         RETURN(rc);
446 }
447
448 static inline int obd_size_diskmd(struct obd_export *exp,
449                                   struct lov_stripe_md *mem_src)
450 {
451         return obd_packmd(exp, NULL, mem_src);
452 }
453
454 /* helper functions */
455 static inline int obd_alloc_diskmd(struct obd_export *exp,
456                                    struct lov_mds_md **disk_tgt)
457 {
458         LASSERT(disk_tgt);
459         LASSERT(*disk_tgt == NULL);
460         return obd_packmd(exp, disk_tgt, NULL);
461 }
462
463 static inline int obd_free_diskmd(struct obd_export *exp,
464                                   struct lov_mds_md **disk_tgt)
465 {
466         LASSERT(disk_tgt);
467         LASSERT(*disk_tgt);
468         return obd_packmd(exp, disk_tgt, NULL);
469 }
470
471 /* Unpack an MD struct from disk to in-memory format.
472  * Returns +ve size of unpacked MD (0 for free), or -ve error.
473  *
474  * If @mem_tgt == NULL, MD size is returned (max size if @disk_src == NULL).
475  * If @*mem_tgt != NULL and @disk_src == NULL, @*mem_tgt will be freed.
476  * If @*mem_tgt == NULL, it will be allocated
477  */
478 static inline int obd_unpackmd(struct obd_export *exp,
479                                struct lov_stripe_md **mem_tgt,
480                                struct lov_mds_md *disk_src,
481                                int disk_len)
482 {
483         int rc;
484         ENTRY;
485
486         EXP_CHECK_OP(exp, unpackmd);
487         EXP_COUNTER_INCREMENT(exp, unpackmd);
488
489         rc = OBP(exp->exp_obd, unpackmd)(exp, mem_tgt, disk_src, disk_len);
490         RETURN(rc);
491 }
492
493 /* helper functions */
494 static inline int obd_alloc_memmd(struct obd_export *exp,
495                                   struct lov_stripe_md **mem_tgt)
496 {
497         LASSERT(mem_tgt);
498         LASSERT(*mem_tgt == NULL);
499         return obd_unpackmd(exp, mem_tgt, NULL, 0);
500 }
501
502 static inline int obd_free_memmd(struct obd_export *exp,
503                                  struct lov_stripe_md **mem_tgt)
504 {
505         LASSERT(mem_tgt);
506         LASSERT(*mem_tgt);
507         return obd_unpackmd(exp, mem_tgt, NULL, 0);
508 }
509
510 static inline int obd_checkmd(struct obd_export *exp,
511                               struct obd_export *md_exp,
512                               struct lov_stripe_md *mem_tgt)
513 {
514         int rc;
515         ENTRY;
516
517         EXP_CHECK_OP(exp, checkmd);
518         EXP_COUNTER_INCREMENT(exp, checkmd);
519
520         rc = OBP(exp->exp_obd, checkmd)(exp, md_exp, mem_tgt);
521         RETURN(rc);
522 }
523
524 static inline int obd_precreate(struct obd_export *exp)
525 {
526         int rc;
527         ENTRY;
528
529         EXP_CHECK_OP(exp, precreate);
530         OBD_COUNTER_INCREMENT(exp->exp_obd, precreate);
531
532         rc = OBP(exp->exp_obd, precreate)(exp);
533         RETURN(rc);
534 }
535
536 static inline int obd_create_async(struct obd_export *exp,
537                                    struct obd_info *oinfo,
538                                    struct lov_stripe_md **ea,
539                                    struct obd_trans_info *oti)
540 {
541         int rc;
542         ENTRY;
543
544         EXP_CHECK_OP(exp, create_async);
545         EXP_COUNTER_INCREMENT(exp, create_async);
546
547         rc = OBP(exp->exp_obd, create_async)(exp, oinfo, ea, oti);
548         RETURN(rc);
549 }
550
551 static inline int obd_create(struct obd_export *exp, struct obdo *obdo,
552                              struct lov_stripe_md **ea,
553                              struct obd_trans_info *oti)
554 {
555         int rc;
556         ENTRY;
557
558         EXP_CHECK_OP(exp, create);
559         EXP_COUNTER_INCREMENT(exp, create);
560
561         rc = OBP(exp->exp_obd, create)(exp, obdo, ea, oti);
562         RETURN(rc);
563 }
564
565 static inline int obd_destroy(struct obd_export *exp, struct obdo *obdo,
566                               struct lov_stripe_md *ea,
567                               struct obd_trans_info *oti,
568                               struct obd_export *md_exp)
569 {
570         int rc;
571         ENTRY;
572
573         EXP_CHECK_OP(exp, destroy);
574         EXP_COUNTER_INCREMENT(exp, destroy);
575
576         rc = OBP(exp->exp_obd, destroy)(exp, obdo, ea, oti, md_exp);
577         RETURN(rc);
578 }
579
580 static inline int obd_getattr(struct obd_export *exp, struct obd_info *oinfo)
581 {
582         int rc;
583         ENTRY;
584
585         EXP_CHECK_OP(exp, getattr);
586         EXP_COUNTER_INCREMENT(exp, getattr);
587
588         rc = OBP(exp->exp_obd, getattr)(exp, oinfo);
589         RETURN(rc);
590 }
591
592 static inline int obd_getattr_async(struct obd_export *exp,
593                                     struct obd_info *oinfo,
594                                     struct ptlrpc_request_set *set)
595 {
596         int rc;
597         ENTRY;
598
599         EXP_CHECK_OP(exp, getattr_async);
600         EXP_COUNTER_INCREMENT(exp, getattr_async);
601
602         rc = OBP(exp->exp_obd, getattr_async)(exp, oinfo, set);
603         RETURN(rc);
604 }
605
606 static inline int obd_setattr(struct obd_export *exp, struct obd_info *oinfo,
607                               struct obd_trans_info *oti)
608 {
609         int rc;
610         ENTRY;
611
612         EXP_CHECK_OP(exp, setattr);
613         EXP_COUNTER_INCREMENT(exp, setattr);
614
615         rc = OBP(exp->exp_obd, setattr)(exp, oinfo, oti);
616         RETURN(rc);
617 }
618
619 /* This performs all the requests set init/wait/destroy actions. */
620 static inline int obd_setattr_rqset(struct obd_export *exp,
621                                     struct obd_info *oinfo,
622                                     struct obd_trans_info *oti)
623 {
624         struct ptlrpc_request_set *set = NULL;
625         int rc;
626         ENTRY;
627
628         EXP_CHECK_OP(exp, setattr_async);
629         EXP_COUNTER_INCREMENT(exp, setattr_async);
630
631         set =  ptlrpc_prep_set();
632         if (set == NULL)
633                 RETURN(-ENOMEM);
634
635         rc = OBP(exp->exp_obd, setattr_async)(exp, oinfo, oti, set);
636         if (rc == 0)
637                 rc = ptlrpc_set_wait(set);
638         ptlrpc_set_destroy(set);
639         RETURN(rc);
640 }
641
642 /* This adds all the requests into @set if @set != NULL, otherwise
643    all requests are sent asynchronously without waiting for response. */
644 static inline int obd_setattr_async(struct obd_export *exp,
645                                     struct obd_info *oinfo,
646                                     struct obd_trans_info *oti,
647                                     struct ptlrpc_request_set *set)
648 {
649         int rc;
650         ENTRY;
651
652         EXP_CHECK_OP(exp, setattr_async);
653         EXP_COUNTER_INCREMENT(exp, setattr_async);
654
655         rc = OBP(exp->exp_obd, setattr_async)(exp, oinfo, oti, set);
656         RETURN(rc);
657 }
658
659 static inline int obd_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
660                                int priority)
661 {
662         struct obd_device *obd = imp->imp_obd;
663         int rc;
664         ENTRY;
665
666         OBD_CHECK_DEV_ACTIVE(obd);
667         OBD_CHECK_OP(obd, add_conn, -EOPNOTSUPP);
668         OBD_COUNTER_INCREMENT(obd, add_conn);
669
670         rc = OBP(obd, add_conn)(imp, uuid, priority);
671         RETURN(rc);
672 }
673
674 static inline int obd_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
675 {
676         struct obd_device *obd = imp->imp_obd;
677         int rc;
678         ENTRY;
679
680         OBD_CHECK_DEV_ACTIVE(obd);
681         OBD_CHECK_OP(obd, del_conn, -EOPNOTSUPP);
682         OBD_COUNTER_INCREMENT(obd, del_conn);
683
684         rc = OBP(obd, del_conn)(imp, uuid);
685         RETURN(rc);
686 }
687
688 static inline int obd_connect(struct lustre_handle *conn,struct obd_device *obd,
689                               struct obd_uuid *cluuid,
690                               struct obd_connect_data *d,
691                               void *localdata)
692 {
693         int rc;
694         __u64 ocf = d ? d->ocd_connect_flags : 0; /* for post-condition check */
695         ENTRY;
696
697         OBD_CHECK_DEV_ACTIVE(obd);
698         OBD_CHECK_OP(obd, connect, -EOPNOTSUPP);
699         OBD_COUNTER_INCREMENT(obd, connect);
700
701         rc = OBP(obd, connect)(conn, obd, cluuid, d, localdata);
702         /* check that only subset is granted */
703         LASSERT(ergo(d != NULL,
704                      (d->ocd_connect_flags & ocf) == d->ocd_connect_flags));
705         RETURN(rc);
706 }
707
708 static inline int obd_reconnect(struct obd_export *exp,
709                                 struct obd_device *obd,
710                                 struct obd_uuid *cluuid,
711                                 struct obd_connect_data *d,
712                                 void *localdata)
713 {
714         int rc;
715         __u64 ocf = d ? d->ocd_connect_flags : 0; /* for post-condition check */
716         ENTRY;
717
718         OBD_CHECK_DEV_ACTIVE(obd);
719         OBD_CHECK_OP(obd, reconnect, 0);
720         OBD_COUNTER_INCREMENT(obd, reconnect);
721
722         rc = OBP(obd, reconnect)(exp, obd, cluuid, d, localdata);
723         /* check that only subset is granted */
724         LASSERT(ergo(d != NULL,
725                      (d->ocd_connect_flags & ocf) == d->ocd_connect_flags));
726         RETURN(rc);
727 }
728
729 static inline int obd_disconnect(struct obd_export *exp)
730 {
731         int rc;
732         ENTRY;
733
734         EXP_CHECK_OP(exp, disconnect);
735         EXP_COUNTER_INCREMENT(exp, disconnect);
736
737         rc = OBP(exp->exp_obd, disconnect)(exp);
738         RETURN(rc);
739 }
740
741 static inline int obd_fid_init(struct obd_export *exp)
742 {
743         int rc;
744         ENTRY;
745
746         OBD_CHECK_OP(exp->exp_obd, fid_init, 0);
747         EXP_COUNTER_INCREMENT(exp, fid_init);
748
749         rc = OBP(exp->exp_obd, fid_init)(exp);
750         RETURN(rc);
751 }
752
753 static inline int obd_fid_fini(struct obd_export *exp)
754 {
755         int rc;
756         ENTRY;
757
758         OBD_CHECK_OP(exp->exp_obd, fid_fini, 0);
759         EXP_COUNTER_INCREMENT(exp, fid_fini);
760
761         rc = OBP(exp->exp_obd, fid_fini)(exp);
762         RETURN(rc);
763 }
764
765 static inline int obd_ping(struct obd_export *exp)
766 {
767         int rc;
768         ENTRY;
769
770         OBD_CHECK_OP(exp->exp_obd, ping, 0);
771         EXP_COUNTER_INCREMENT(exp, ping);
772
773         rc = OBP(exp->exp_obd, ping)(exp);
774         RETURN(rc);
775 }
776
777 static inline int obd_pool_new(struct obd_device *obd, char *poolname)
778 {
779         int rc;
780         ENTRY;
781
782         OBD_CHECK_OP(obd, pool_new, -EOPNOTSUPP);
783         OBD_COUNTER_INCREMENT(obd, pool_new);
784
785         rc = OBP(obd, pool_new)(obd, poolname);
786         RETURN(rc);
787 }
788
789 static inline int obd_pool_del(struct obd_device *obd, char *poolname)
790 {
791         int rc;
792         ENTRY;
793
794         OBD_CHECK_OP(obd, pool_del, -EOPNOTSUPP);
795         OBD_COUNTER_INCREMENT(obd, pool_del);
796
797         rc = OBP(obd, pool_del)(obd, poolname);
798         RETURN(rc);
799 }
800
801 static inline int obd_pool_add(struct obd_device *obd, char *poolname, char *ostname)
802 {
803         int rc;
804         ENTRY;
805
806         OBD_CHECK_OP(obd, pool_add, -EOPNOTSUPP);
807         OBD_COUNTER_INCREMENT(obd, pool_add);
808
809         rc = OBP(obd, pool_add)(obd, poolname, ostname);
810         RETURN(rc);
811 }
812
813 static inline int obd_pool_rem(struct obd_device *obd, char *poolname, char *ostname)
814 {
815         int rc;
816         ENTRY;
817
818         OBD_CHECK_OP(obd, pool_rem, -EOPNOTSUPP);
819         OBD_COUNTER_INCREMENT(obd, pool_rem);
820
821         rc = OBP(obd, pool_rem)(obd, poolname, ostname);
822         RETURN(rc);
823 }
824
825 static inline void obd_getref(struct obd_device *obd)
826 {
827         ENTRY;
828         if (OBT(obd) && OBP(obd, getref)) {
829                 OBD_COUNTER_INCREMENT(obd, getref);
830                 OBP(obd, getref)(obd);
831         }
832         EXIT;
833 }
834
835 static inline void obd_putref(struct obd_device *obd)
836 {
837         ENTRY;
838         if (OBT(obd) && OBP(obd, putref)) {
839                 OBD_COUNTER_INCREMENT(obd, putref);
840                 OBP(obd, putref)(obd);
841         }
842         EXIT;
843 }
844
845 static inline int obd_init_export(struct obd_export *exp)
846 {
847         int rc = 0;
848
849         ENTRY;
850         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
851             OBP((exp)->exp_obd, init_export))
852                 rc = OBP(exp->exp_obd, init_export)(exp);
853         RETURN(rc);
854 }
855
856 static inline int obd_destroy_export(struct obd_export *exp)
857 {
858         ENTRY;
859         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
860             OBP((exp)->exp_obd, destroy_export))
861                 OBP(exp->exp_obd, destroy_export)(exp);
862         RETURN(0);
863 }
864
865 static inline int obd_extent_calc(struct obd_export *exp,
866                                   struct lov_stripe_md *md,
867                                   int cmd, obd_off *offset)
868 {
869         int rc;
870         ENTRY;
871         EXP_CHECK_OP(exp, extent_calc);
872         rc = OBP(exp->exp_obd, extent_calc)(exp, md, cmd, offset);
873         RETURN(rc);
874 }
875
876 static inline struct dentry *
877 obd_lvfs_fid2dentry(struct obd_export *exp, __u64 id_ino, __u32 gen, __u64 gr)
878 {
879         LASSERT(exp->exp_obd);
880
881         return lvfs_fid2dentry(&exp->exp_obd->obd_lvfs_ctxt, id_ino, gen, gr,
882                                exp->exp_obd);
883 }
884
885 #ifndef time_before
886 #define time_before(t1, t2) ((long)t2 - (long)t1 > 0)
887 #endif
888
889 /* @max_age is the oldest time in jiffies that we accept using a cached data.
890  * If the cache is older than @max_age we will get a new value from the
891  * target.  Use a value of "cfs_time_current() + HZ" to guarantee freshness. */
892 static inline int obd_statfs_async(struct obd_device *obd,
893                                    struct obd_info *oinfo,
894                                    __u64 max_age,
895                                    struct ptlrpc_request_set *rqset)
896 {
897         int rc = 0;
898         ENTRY;
899
900         if (obd == NULL)
901                 RETURN(-EINVAL);
902
903         OBD_CHECK_OP(obd, statfs, -EOPNOTSUPP);
904         OBD_COUNTER_INCREMENT(obd, statfs);
905
906         CDEBUG(D_SUPER, "%s: osfs %p age "LPU64", max_age "LPU64"\n",
907                obd->obd_name, &obd->obd_osfs, obd->obd_osfs_age, max_age);
908         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
909                 rc = OBP(obd, statfs_async)(obd, oinfo, max_age, rqset);
910         } else {
911                 CDEBUG(D_SUPER,"%s: use %p cache blocks "LPU64"/"LPU64
912                        " objects "LPU64"/"LPU64"\n",
913                        obd->obd_name, &obd->obd_osfs,
914                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
915                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
916                 spin_lock(&obd->obd_osfs_lock);
917                 memcpy(oinfo->oi_osfs, &obd->obd_osfs, sizeof(*oinfo->oi_osfs));
918                 spin_unlock(&obd->obd_osfs_lock);
919                 oinfo->oi_flags |= OBD_STATFS_FROM_CACHE;
920                 if (oinfo->oi_cb_up)
921                         oinfo->oi_cb_up(oinfo, 0);
922         }
923         RETURN(rc);
924 }
925
926 static inline int obd_statfs_rqset(struct obd_device *obd,
927                                    struct obd_statfs *osfs, __u64 max_age,
928                                    __u32 flags)
929 {
930         struct ptlrpc_request_set *set = NULL;
931         struct obd_info oinfo = { { { 0 } } };
932         int rc = 0;
933         ENTRY;
934
935         set = ptlrpc_prep_set();
936         if (set == NULL)
937                 RETURN(-ENOMEM);
938
939         oinfo.oi_osfs = osfs;
940         oinfo.oi_flags = flags;
941         rc = obd_statfs_async(obd, &oinfo, max_age, set);
942         if (rc == 0)
943                 rc = ptlrpc_set_wait(set);
944         ptlrpc_set_destroy(set);
945         RETURN(rc);
946 }
947
948 /* @max_age is the oldest time in jiffies that we accept using a cached data.
949  * If the cache is older than @max_age we will get a new value from the
950  * target.  Use a value of "cfs_time_current() + HZ" to guarantee freshness. */
951 static inline int obd_statfs(struct obd_device *obd, struct obd_statfs *osfs,
952                              __u64 max_age, __u32 flags)
953 {
954         int rc = 0;
955         ENTRY;
956
957         if (obd == NULL)
958                 RETURN(-EINVAL);
959
960         OBD_CHECK_OP(obd, statfs, -EOPNOTSUPP);
961         OBD_COUNTER_INCREMENT(obd, statfs);
962
963         CDEBUG(D_SUPER, "osfs "LPU64", max_age "LPU64"\n",
964                obd->obd_osfs_age, max_age);
965         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
966                 rc = OBP(obd, statfs)(obd, osfs, max_age, flags);
967                 if (rc == 0) {
968                         spin_lock(&obd->obd_osfs_lock);
969                         memcpy(&obd->obd_osfs, osfs, sizeof(obd->obd_osfs));
970                         obd->obd_osfs_age = cfs_time_current_64();
971                         spin_unlock(&obd->obd_osfs_lock);
972                 }
973         } else {
974                 CDEBUG(D_SUPER,"%s: use %p cache blocks "LPU64"/"LPU64
975                        " objects "LPU64"/"LPU64"\n",
976                        obd->obd_name, &obd->obd_osfs,
977                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
978                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
979                 spin_lock(&obd->obd_osfs_lock);
980                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
981                 spin_unlock(&obd->obd_osfs_lock);
982         }
983         RETURN(rc);
984 }
985
986 static inline int obd_sync_rqset(struct obd_export *exp, struct obd_info *oinfo,
987                                  obd_size start, obd_size end)
988 {
989         struct ptlrpc_request_set *set = NULL;
990         int rc;
991         ENTRY;
992
993         OBD_CHECK_OP(exp->exp_obd, sync, -EOPNOTSUPP);
994         EXP_COUNTER_INCREMENT(exp, sync);
995
996         set =  ptlrpc_prep_set();
997         if (set == NULL)
998                 RETURN(-ENOMEM);
999
1000         rc = OBP(exp->exp_obd, sync)(exp, oinfo, start, end, set);
1001         if (rc == 0)
1002                 rc = ptlrpc_set_wait(set);
1003         ptlrpc_set_destroy(set);
1004         RETURN(rc);
1005 }
1006
1007 static inline int obd_sync(struct obd_export *exp, struct obd_info *oinfo,
1008                            obd_size start, obd_size end,
1009                            struct ptlrpc_request_set *set)
1010 {
1011         int rc;
1012         ENTRY;
1013
1014         OBD_CHECK_OP(exp->exp_obd, sync, -EOPNOTSUPP);
1015         EXP_COUNTER_INCREMENT(exp, sync);
1016
1017         rc = OBP(exp->exp_obd, sync)(exp, oinfo, start, end, set);
1018         RETURN(rc);
1019 }
1020
1021 static inline int obd_punch_rqset(struct obd_export *exp,
1022                                   struct obd_info *oinfo,
1023                                   struct obd_trans_info *oti)
1024 {
1025         struct ptlrpc_request_set *set = NULL;
1026         int rc;
1027         ENTRY;
1028
1029         EXP_CHECK_OP(exp, punch);
1030         EXP_COUNTER_INCREMENT(exp, punch);
1031
1032         set =  ptlrpc_prep_set();
1033         if (set == NULL)
1034                 RETURN(-ENOMEM);
1035
1036         rc = OBP(exp->exp_obd, punch)(exp, oinfo, oti, set);
1037         if (rc == 0)
1038                 rc = ptlrpc_set_wait(set);
1039         ptlrpc_set_destroy(set);
1040         RETURN(rc);
1041 }
1042
1043 static inline int obd_punch(struct obd_export *exp, struct obd_info *oinfo,
1044                             struct obd_trans_info *oti,
1045                             struct ptlrpc_request_set *rqset)
1046 {
1047         int rc;
1048         ENTRY;
1049
1050         EXP_CHECK_OP(exp, punch);
1051         EXP_COUNTER_INCREMENT(exp, punch);
1052
1053         rc = OBP(exp->exp_obd, punch)(exp, oinfo, oti, rqset);
1054         RETURN(rc);
1055 }
1056
1057 static inline int obd_brw(int cmd, struct obd_export *exp,
1058                           struct obd_info *oinfo, obd_count oa_bufs,
1059                           struct brw_page *pg, struct obd_trans_info *oti)
1060 {
1061         int rc;
1062         ENTRY;
1063
1064         EXP_CHECK_OP(exp, brw);
1065         EXP_COUNTER_INCREMENT(exp, brw);
1066
1067         if (!(cmd & (OBD_BRW_RWMASK | OBD_BRW_CHECK))) {
1068                 CERROR("obd_brw: cmd must be OBD_BRW_READ, OBD_BRW_WRITE, "
1069                        "or OBD_BRW_CHECK\n");
1070                 LBUG();
1071         }
1072
1073         rc = OBP(exp->exp_obd, brw)(cmd, exp, oinfo, oa_bufs, pg, oti);
1074         RETURN(rc);
1075 }
1076
1077 static inline int obd_brw_async(int cmd, struct obd_export *exp,
1078                                 struct obd_info *oinfo, obd_count oa_bufs,
1079                                 struct brw_page *pg, struct obd_trans_info *oti,
1080                                 struct ptlrpc_request_set *set, int pshift)
1081 {
1082         int rc;
1083         ENTRY;
1084
1085         EXP_CHECK_OP(exp, brw_async);
1086         EXP_COUNTER_INCREMENT(exp, brw_async);
1087
1088         if (!(cmd & OBD_BRW_RWMASK)) {
1089                 CERROR("obd_brw: cmd must be OBD_BRW_READ or OBD_BRW_WRITE\n");
1090                 LBUG();
1091         }
1092
1093         rc = OBP(exp->exp_obd, brw_async)(cmd, exp, oinfo, oa_bufs,
1094                                           pg, oti,set, pshift);
1095         RETURN(rc);
1096 }
1097
1098 static inline int obd_brw_rqset(int cmd, struct obd_export *exp,
1099                                 struct obdo *oa, struct lov_stripe_md *lsm,
1100                                 obd_count oa_bufs, struct brw_page *pg,
1101                                 struct obd_trans_info *oti)
1102 {
1103         struct ptlrpc_request_set *set = NULL;
1104         struct obd_info oinfo = { { { 0 } } };
1105         int rc = 0;
1106         ENTRY;
1107
1108         set =  ptlrpc_prep_set();
1109         if (set == NULL)
1110                 RETURN(-ENOMEM);
1111
1112         oinfo.oi_oa = oa;
1113         oinfo.oi_md = lsm;
1114         rc = obd_brw_async(cmd, exp, &oinfo, oa_bufs, pg, oti, set, 0);
1115         if (rc == 0) {
1116                 rc = ptlrpc_set_wait(set);
1117                 if (rc)
1118                         CERROR("error from callback: rc = %d\n", rc);
1119         } else {
1120                 CDEBUG(rc == -ENOSPC ? D_INODE : D_ERROR,
1121                        "error from obd_brw_async: rc = %d\n", rc);
1122         }
1123         ptlrpc_set_destroy(set);
1124         RETURN(rc);
1125 }
1126
1127 /* flags used by obd_prep_async_page */
1128 #define OBD_PAGE_NO_CACHE  0x00000001 /* don't add to cache */
1129 #define OBD_FAST_LOCK 0x00000002 /* lockh refers to a "fast lock" */
1130
1131 static inline  int obd_prep_async_page(struct obd_export *exp,
1132                                        struct lov_stripe_md *lsm,
1133                                        struct lov_oinfo *loi,
1134                                        cfs_page_t *page, obd_off offset,
1135                                        struct obd_async_page_ops *ops,
1136                                        void *data, void **res, int flags,
1137                                        struct lustre_handle *lockh)
1138 {
1139         int ret;
1140         ENTRY;
1141
1142         OBD_CHECK_OP(exp->exp_obd, prep_async_page, -EOPNOTSUPP);
1143         EXP_COUNTER_INCREMENT(exp, prep_async_page);
1144
1145         ret = OBP(exp->exp_obd, prep_async_page)(exp, lsm, loi, page, offset,
1146                                                  ops, data, res, flags,
1147                                                  lockh);
1148         RETURN(ret);
1149 }
1150
1151 static inline int obd_get_lock(struct obd_export *exp,
1152                                struct lov_stripe_md *lsm, void **res, int rw,
1153                                obd_off start, obd_off end,
1154                                struct lustre_handle *lockh, int flags)
1155 {
1156         ENTRY;
1157
1158         OBD_CHECK_OP(exp->exp_obd, get_lock, -EOPNOTSUPP);
1159         EXP_COUNTER_INCREMENT(exp, get_lock);
1160
1161         RETURN(OBP(exp->exp_obd, get_lock)(exp, lsm, res, rw, start, end,
1162                                            lockh, flags));
1163 }
1164
1165 static inline int obd_queue_async_io(struct obd_export *exp,
1166                                      struct lov_stripe_md *lsm,
1167                                      struct lov_oinfo *loi, void *cookie,
1168                                      int cmd, obd_off off, int count,
1169                                      obd_flag brw_flags, obd_flag async_flags)
1170 {
1171         int rc;
1172         ENTRY;
1173
1174         OBD_CHECK_OP(exp->exp_obd, queue_async_io, -EOPNOTSUPP);
1175         EXP_COUNTER_INCREMENT(exp, queue_async_io);
1176         LASSERT(cmd & OBD_BRW_RWMASK);
1177
1178         rc = OBP(exp->exp_obd, queue_async_io)(exp, lsm, loi, cookie, cmd, off,
1179                                                count, brw_flags, async_flags);
1180         RETURN(rc);
1181 }
1182
1183 static inline int obd_set_async_flags(struct obd_export *exp,
1184                                       struct lov_stripe_md *lsm,
1185                                       struct lov_oinfo *loi, void *cookie,
1186                                       obd_flag async_flags)
1187 {
1188         int rc;
1189         ENTRY;
1190
1191         OBD_CHECK_OP(exp->exp_obd, set_async_flags, -EOPNOTSUPP);
1192         EXP_COUNTER_INCREMENT(exp, set_async_flags);
1193
1194         rc = OBP(exp->exp_obd, set_async_flags)(exp, lsm, loi, cookie,
1195                                                 async_flags);
1196         RETURN(rc);
1197 }
1198
1199 static inline int obd_queue_group_io(struct obd_export *exp,
1200                                      struct lov_stripe_md *lsm,
1201                                      struct lov_oinfo *loi,
1202                                      struct obd_io_group *oig,
1203                                      void *cookie, int cmd, obd_off off,
1204                                      int count, obd_flag brw_flags,
1205                                      obd_flag async_flags)
1206 {
1207         int rc;
1208         ENTRY;
1209
1210         OBD_CHECK_OP(exp->exp_obd, queue_group_io, -EOPNOTSUPP);
1211         EXP_COUNTER_INCREMENT(exp, queue_group_io);
1212         LASSERT(cmd & OBD_BRW_RWMASK);
1213
1214         rc = OBP(exp->exp_obd, queue_group_io)(exp, lsm, loi, oig, cookie,
1215                                                cmd, off, count, brw_flags,
1216                                                async_flags);
1217         RETURN(rc);
1218 }
1219
1220 static inline int obd_trigger_group_io(struct obd_export *exp,
1221                                        struct lov_stripe_md *lsm,
1222                                        struct lov_oinfo *loi,
1223                                        struct obd_io_group *oig)
1224 {
1225         int rc;
1226         ENTRY;
1227
1228         OBD_CHECK_OP(exp->exp_obd, trigger_group_io, -EOPNOTSUPP);
1229         EXP_COUNTER_INCREMENT(exp, trigger_group_io);
1230
1231         rc = OBP(exp->exp_obd, trigger_group_io)(exp, lsm, loi, oig);
1232         RETURN(rc);
1233 }
1234
1235 static inline int obd_teardown_async_page(struct obd_export *exp,
1236                                           struct lov_stripe_md *lsm,
1237                                           struct lov_oinfo *loi, void *cookie)
1238 {
1239         int rc;
1240         ENTRY;
1241
1242         OBD_CHECK_OP(exp->exp_obd, teardown_async_page, -EOPNOTSUPP);
1243         EXP_COUNTER_INCREMENT(exp, teardown_async_page);
1244
1245         rc = OBP(exp->exp_obd, teardown_async_page)(exp, lsm, loi, cookie);
1246         RETURN(rc);
1247 }
1248
1249 static inline int obd_preprw(int cmd, struct obd_export *exp, struct obdo *oa,
1250                              int objcount, struct obd_ioobj *obj,
1251                              struct niobuf_remote *remote, int *pages,
1252                              struct niobuf_local *local,
1253                              struct obd_trans_info *oti)
1254 {
1255         int rc;
1256         ENTRY;
1257
1258         OBD_CHECK_OP(exp->exp_obd, preprw, -EOPNOTSUPP);
1259         EXP_COUNTER_INCREMENT(exp, preprw);
1260
1261         rc = OBP(exp->exp_obd, preprw)(cmd, exp, oa, objcount, obj, remote,
1262                                        pages, local, oti);
1263         RETURN(rc);
1264 }
1265
1266 static inline int obd_commitrw(int cmd, struct obd_export *exp, struct obdo *oa,
1267                                int objcount, struct obd_ioobj *obj,
1268                                struct niobuf_remote *rnb, int pages,
1269                                struct niobuf_local *local,
1270                                struct obd_trans_info *oti, int rc)
1271 {
1272         ENTRY;
1273
1274         OBD_CHECK_OP(exp->exp_obd, commitrw, -EOPNOTSUPP);
1275         EXP_COUNTER_INCREMENT(exp, commitrw);
1276
1277         rc = OBP(exp->exp_obd, commitrw)(cmd, exp, oa, objcount, obj,
1278                                          rnb, pages, local, oti, rc);
1279         RETURN(rc);
1280 }
1281
1282 static inline int obd_merge_lvb(struct obd_export *exp,
1283                                 struct lov_stripe_md *lsm,
1284                                 struct ost_lvb *lvb, int kms_only)
1285 {
1286         int rc;
1287         ENTRY;
1288
1289         OBD_CHECK_OP(exp->exp_obd, merge_lvb, -EOPNOTSUPP);
1290         EXP_COUNTER_INCREMENT(exp, merge_lvb);
1291
1292         rc = OBP(exp->exp_obd, merge_lvb)(exp, lsm, lvb, kms_only);
1293         RETURN(rc);
1294 }
1295
1296 static inline int obd_update_lvb(struct obd_export *exp,
1297                                  struct lov_stripe_md *lsm,
1298                                  struct ost_lvb *lvb, obd_flag valid)
1299 {
1300         int rc;
1301         ENTRY;
1302
1303         OBD_CHECK_OP(exp->exp_obd, update_lvb, -EOPNOTSUPP);
1304         EXP_COUNTER_INCREMENT(exp, update_lvb);
1305
1306         rc = OBP(exp->exp_obd, update_lvb)(exp, lsm, lvb, valid);
1307         RETURN(rc);
1308 }
1309
1310 static inline int obd_adjust_kms(struct obd_export *exp,
1311                                  struct lov_stripe_md *lsm, obd_off size,
1312                                  int shrink)
1313 {
1314         int rc;
1315         ENTRY;
1316
1317         OBD_CHECK_OP(exp->exp_obd, adjust_kms, -EOPNOTSUPP);
1318         EXP_COUNTER_INCREMENT(exp, adjust_kms);
1319
1320         rc = OBP(exp->exp_obd, adjust_kms)(exp, lsm, size, shrink);
1321         RETURN(rc);
1322 }
1323
1324 static inline int obd_iocontrol(unsigned int cmd, struct obd_export *exp,
1325                                 int len, void *karg, void *uarg)
1326 {
1327         int rc;
1328         ENTRY;
1329
1330         EXP_CHECK_OP(exp, iocontrol);
1331         EXP_COUNTER_INCREMENT(exp, iocontrol);
1332
1333         rc = OBP(exp->exp_obd, iocontrol)(cmd, exp, len, karg, uarg);
1334         RETURN(rc);
1335 }
1336
1337 static inline int obd_enqueue_rqset(struct obd_export *exp,
1338                                     struct obd_info *oinfo,
1339                                     struct ldlm_enqueue_info *einfo)
1340 {
1341         struct ptlrpc_request_set *set = NULL;
1342         int rc;
1343         ENTRY;
1344
1345         EXP_CHECK_OP(exp, enqueue);
1346         EXP_COUNTER_INCREMENT(exp, enqueue);
1347
1348         set =  ptlrpc_prep_set();
1349         if (set == NULL)
1350                 RETURN(-ENOMEM);
1351
1352         rc = OBP(exp->exp_obd, enqueue)(exp, oinfo, einfo, set);
1353         if (rc == 0)
1354                 rc = ptlrpc_set_wait(set);
1355         ptlrpc_set_destroy(set);
1356         RETURN(rc);
1357 }
1358
1359 static inline int obd_enqueue(struct obd_export *exp,
1360                               struct obd_info *oinfo,
1361                               struct ldlm_enqueue_info *einfo,
1362                               struct ptlrpc_request_set *set)
1363 {
1364         int rc;
1365         ENTRY;
1366
1367         EXP_CHECK_OP(exp, enqueue);
1368         EXP_COUNTER_INCREMENT(exp, enqueue);
1369
1370         rc = OBP(exp->exp_obd, enqueue)(exp, oinfo, einfo, set);
1371         RETURN(rc);
1372 }
1373
1374 static inline int obd_match(struct obd_export *exp, struct lov_stripe_md *ea,
1375                             __u32 type, ldlm_policy_data_t *policy, __u32 mode,
1376                             int *flags, void *data, struct lustre_handle *lockh,
1377                             int *n_matches)
1378 {
1379         int rc;
1380         ENTRY;
1381
1382         EXP_CHECK_OP(exp, match);
1383         EXP_COUNTER_INCREMENT(exp, match);
1384
1385         rc = OBP(exp->exp_obd, match)(exp, ea, type, policy, mode, flags, data,
1386                                       lockh, n_matches);
1387         RETURN(rc);
1388 }
1389
1390 static inline int obd_change_cbdata(struct obd_export *exp,
1391                                     struct lov_stripe_md *lsm,
1392                                     ldlm_iterator_t it, void *data)
1393 {
1394         int rc;
1395         ENTRY;
1396
1397         EXP_CHECK_OP(exp, change_cbdata);
1398         EXP_COUNTER_INCREMENT(exp, change_cbdata);
1399
1400         rc = OBP(exp->exp_obd, change_cbdata)(exp, lsm, it, data);
1401         RETURN(rc);
1402 }
1403
1404 static inline int obd_cancel(struct obd_export *exp, struct lov_stripe_md *ea,
1405                              __u32 mode, struct lustre_handle *lockh, int flags,
1406                              obd_off end)
1407 {
1408         int rc;
1409         ENTRY;
1410
1411         EXP_CHECK_OP(exp, cancel);
1412         EXP_COUNTER_INCREMENT(exp, cancel);
1413
1414         rc = OBP(exp->exp_obd, cancel)(exp, ea, mode, lockh, flags, end);
1415         RETURN(rc);
1416 }
1417
1418 static inline int obd_cancel_unused(struct obd_export *exp,
1419                                     struct lov_stripe_md *ea, int flags,
1420                                     void *opaque)
1421 {
1422         int rc;
1423         ENTRY;
1424
1425         EXP_CHECK_OP(exp, cancel_unused);
1426         EXP_COUNTER_INCREMENT(exp, cancel_unused);
1427
1428         rc = OBP(exp->exp_obd, cancel_unused)(exp, ea, flags, opaque);
1429         RETURN(rc);
1430 }
1431
1432 static inline int obd_join_lru(struct obd_export *exp,
1433                                struct lov_stripe_md *ea, int join)
1434 {
1435         int rc;
1436         ENTRY;
1437
1438         EXP_CHECK_OP(exp, join_lru);
1439         EXP_COUNTER_INCREMENT(exp, join_lru);
1440
1441         rc = OBP(exp->exp_obd, join_lru)(exp, ea, join);
1442         RETURN(rc);
1443 }
1444
1445 static inline int obd_pin(struct obd_export *exp, struct ll_fid *fid,
1446                           struct obd_client_handle *handle, int flag)
1447 {
1448         int rc;
1449         ENTRY;
1450
1451         EXP_CHECK_OP(exp, pin);
1452         EXP_COUNTER_INCREMENT(exp, pin);
1453
1454         rc = OBP(exp->exp_obd, pin)(exp, fid, handle, flag);
1455         RETURN(rc);
1456 }
1457
1458 static inline int obd_unpin(struct obd_export *exp,
1459                             struct obd_client_handle *handle, int flag)
1460 {
1461         int rc;
1462         ENTRY;
1463
1464         EXP_CHECK_OP(exp, unpin);
1465         EXP_COUNTER_INCREMENT(exp, unpin);
1466
1467         rc = OBP(exp->exp_obd, unpin)(exp, handle, flag);
1468         RETURN(rc);
1469 }
1470
1471
1472 static inline void obd_import_event(struct obd_device *obd,
1473                                     struct obd_import *imp,
1474                                     enum obd_import_event event)
1475 {
1476         ENTRY;
1477         if (!obd) {
1478                 CERROR("NULL device\n");
1479                 EXIT;
1480                 return;
1481         }
1482         if (obd->obd_set_up && OBP(obd, import_event)) {
1483                 OBD_COUNTER_INCREMENT(obd, import_event);
1484                 OBP(obd, import_event)(obd, imp, event);
1485         }
1486         EXIT;
1487 }
1488
1489 static inline int obd_notify(struct obd_device *obd,
1490                              struct obd_device *watched,
1491                              enum obd_notify_event ev, void *data)
1492 {
1493         ENTRY;
1494         OBD_CHECK_DEV(obd);
1495
1496         /* the check for async_recov is a complete hack - I'm hereby
1497            overloading the meaning to also mean "this was called from
1498            mds_postsetup".  I know that my mds is able to handle notifies
1499            by this point, and it needs to get them to execute mds_postrecov. */
1500         if (!obd->obd_set_up && !obd->obd_async_recov) {
1501                 CDEBUG(D_HA, "obd %s not set up\n", obd->obd_name);
1502                 RETURN(-EINVAL);
1503         }
1504
1505         if (!OBP(obd, notify))
1506                 RETURN(-ENOSYS);
1507
1508         OBD_COUNTER_INCREMENT(obd, notify);
1509         RETURN(OBP(obd, notify)(obd, watched, ev, data));
1510 }
1511
1512 static inline int obd_notify_observer(struct obd_device *observer,
1513                                       struct obd_device *observed,
1514                                       enum obd_notify_event ev, void *data)
1515 {
1516         int rc1;
1517         int rc2;
1518
1519         struct obd_notify_upcall *onu;
1520
1521         if (observer->obd_observer)
1522                 rc1 = obd_notify(observer->obd_observer, observed, ev, data);
1523         else
1524                 rc1 = 0;
1525         /*
1526          * Also, call non-obd listener, if any
1527          */
1528         onu = &observer->obd_upcall;
1529         if (onu->onu_upcall != NULL)
1530                 rc2 = onu->onu_upcall(observer, observed, ev, onu->onu_owner);
1531         else
1532                 rc2 = 0;
1533
1534         return rc1 ?: rc2;
1535  }
1536
1537 static inline int obd_quotacheck(struct obd_export *exp,
1538                                  struct obd_quotactl *oqctl)
1539 {
1540         int rc;
1541         ENTRY;
1542
1543         EXP_CHECK_OP(exp, quotacheck);
1544         EXP_COUNTER_INCREMENT(exp, quotacheck);
1545
1546         rc = OBP(exp->exp_obd, quotacheck)(exp, oqctl);
1547         RETURN(rc);
1548 }
1549
1550 static inline int obd_quotactl(struct obd_export *exp,
1551                                struct obd_quotactl *oqctl)
1552 {
1553         int rc;
1554         ENTRY;
1555
1556         EXP_CHECK_OP(exp, quotactl);
1557         EXP_COUNTER_INCREMENT(exp, quotactl);
1558
1559         rc = OBP(exp->exp_obd, quotactl)(exp, oqctl);
1560         RETURN(rc);
1561 }
1562
1563 static inline int obd_quota_adjust_qunit(struct obd_export *exp,
1564                                          struct quota_adjust_qunit *oqaq,
1565                                          struct lustre_quota_ctxt *qctxt)
1566 {
1567 #if defined(LPROCFS) && defined(HAVE_QUOTA_SUPPORT)
1568         struct timeval work_start;
1569         struct timeval work_end;
1570         long timediff;
1571 #endif
1572         int rc;
1573         ENTRY;
1574
1575 #if defined(LPROCFS) && defined(HAVE_QUOTA_SUPPORT)
1576         if (qctxt)
1577                 do_gettimeofday(&work_start);
1578 #endif
1579         EXP_CHECK_OP(exp, quota_adjust_qunit);
1580         EXP_COUNTER_INCREMENT(exp, quota_adjust_qunit);
1581
1582         rc = OBP(exp->exp_obd, quota_adjust_qunit)(exp, oqaq, qctxt);
1583
1584 #if defined(LPROCFS) && defined(HAVE_QUOTA_SUPPORT)
1585         if (qctxt) {
1586                 do_gettimeofday(&work_end);
1587                 timediff = cfs_timeval_sub(&work_end, &work_start, NULL);
1588                 lprocfs_counter_add(qctxt->lqc_stats, LQUOTA_ADJUST_QUNIT,
1589                                     timediff);
1590         }
1591 #endif
1592         RETURN(rc);
1593 }
1594
1595 static inline int obd_health_check(struct obd_device *obd)
1596 {
1597         /* returns: 0 on healthy
1598          *         >0 on unhealthy + reason code/flag
1599          *            however the only suppored reason == 1 right now
1600          *            We'll need to define some better reasons
1601          *            or flags in the future.
1602          *         <0 on error
1603          */
1604         int rc;
1605         ENTRY;
1606
1607         /* don't use EXP_CHECK_OP, because NULL method is normal here */
1608         if (obd == NULL || !OBT(obd)) {
1609                 CERROR("cleaned up obd\n");
1610                 RETURN(-EOPNOTSUPP);
1611         }
1612         if (!obd->obd_set_up || obd->obd_stopping)
1613                 RETURN(0);
1614         if (!OBP(obd, health_check))
1615                 RETURN(0);
1616
1617         rc = OBP(obd, health_check)(obd);
1618         RETURN(rc);
1619 }
1620
1621 static inline int obd_register_observer(struct obd_device *obd,
1622                                         struct obd_device *observer)
1623 {
1624         ENTRY;
1625         OBD_CHECK_DEV(obd);
1626         if (obd->obd_observer && observer)
1627                 RETURN(-EALREADY);
1628         obd->obd_observer = observer;
1629         RETURN(0);
1630 }
1631
1632 static inline int obd_register_page_removal_cb(struct obd_device *obd,
1633                                                obd_page_removal_cb_t cb,
1634                                                obd_pin_extent_cb pin_cb)
1635 {
1636         int rc;
1637         ENTRY;
1638
1639         OBD_CHECK_OP(obd, register_page_removal_cb, 0);
1640         OBD_COUNTER_INCREMENT(obd, register_page_removal_cb);
1641
1642         rc = OBP(obd, register_page_removal_cb)(obd, cb, pin_cb);
1643         RETURN(rc);
1644 }
1645
1646 static inline int obd_unregister_page_removal_cb(struct obd_device *obd,
1647                                                  obd_page_removal_cb_t cb)
1648 {
1649         int rc;
1650         ENTRY;
1651
1652         OBD_CHECK_OP(obd, unregister_page_removal_cb, 0);
1653         OBD_COUNTER_INCREMENT(obd, unregister_page_removal_cb);
1654
1655         rc = OBP(obd, unregister_page_removal_cb)(obd, cb);
1656         RETURN(rc);
1657 }
1658
1659 static inline int obd_register_lock_cancel_cb(struct obd_device *obd,
1660                                               obd_lock_cancel_cb cb)
1661 {
1662         int rc;
1663         ENTRY;
1664
1665         OBD_CHECK_OP(obd, register_lock_cancel_cb, 0);
1666         OBD_COUNTER_INCREMENT(obd, register_lock_cancel_cb);
1667
1668         rc = OBP(obd, register_lock_cancel_cb)(obd, cb);
1669         RETURN(rc);
1670 }
1671
1672 static inline int obd_unregister_lock_cancel_cb(struct obd_device *obd,
1673                                                  obd_lock_cancel_cb cb)
1674 {
1675         int rc;
1676         ENTRY;
1677
1678         OBD_CHECK_OP(obd, unregister_lock_cancel_cb, 0);
1679         OBD_COUNTER_INCREMENT(obd, unregister_lock_cancel_cb);
1680
1681         rc = OBP(obd, unregister_lock_cancel_cb)(obd, cb);
1682         RETURN(rc);
1683 }
1684
1685 /* OBD Metadata Support */
1686
1687 extern int obd_init_caches(void);
1688 extern void obd_cleanup_caches(void);
1689
1690 /* support routines */
1691 extern cfs_mem_cache_t *obdo_cachep;
1692
1693 #define OBDO_ALLOC(ptr)                                                       \
1694 do {                                                                          \
1695         OBD_SLAB_ALLOC_PTR((ptr), obdo_cachep);                               \
1696 } while(0)
1697
1698 #define OBDO_FREE(ptr)                                                        \
1699 do {                                                                          \
1700         OBD_SLAB_FREE_PTR((ptr), obdo_cachep);                                \
1701 } while(0)
1702
1703 /* I'm as embarrassed about this as you are.
1704  *
1705  * <shaver> // XXX do not look into _superhack with remaining eye
1706  * <shaver> // XXX if this were any uglier, I'd get my own show on MTV */
1707 extern int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
1708
1709 /* sysctl.c */
1710 extern void obd_sysctl_init (void);
1711 extern void obd_sysctl_clean (void);
1712
1713 /* uuid.c  */
1714 typedef __u8 class_uuid_t[16];
1715 void class_uuid_unparse(class_uuid_t in, struct obd_uuid *out);
1716
1717 /* lustre_peer.c    */
1718 int lustre_uuid_to_peer(char *uuid, lnet_nid_t *peer_nid, int index);
1719 int class_add_uuid(char *uuid, __u64 nid);
1720 int class_del_uuid (char *uuid);
1721 void class_init_uuidlist(void);
1722 void class_exit_uuidlist(void);
1723
1724 /* prng.c */
1725 void ll_generate_random_uuid(class_uuid_t uuid_out);
1726
1727 #endif /* __LINUX_OBD_CLASS_H */