Whamcloud - gitweb
LU-3963 obdclass: convert to linux list api
[fs/lustre-release.git] / lustre / include / obd_class.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) 2011, 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 #ifndef __CLASS_OBD_H
37 #define __CLASS_OBD_H
38
39 #ifndef __KERNEL__
40 # include <liblustre.h>
41 #endif
42
43 #include <obd_support.h>
44 #include <lustre_import.h>
45 #include <lustre_net.h>
46 #include <obd.h>
47 #include <lustre_lib.h>
48 #include <lustre/lustre_idl.h>
49 #include <lprocfs_status.h>
50
51 #if defined(__linux__)
52 #include <linux/obd_class.h>
53 #elif defined(__APPLE__)
54 #include <darwin/obd_class.h>
55 #elif defined(__WINNT__)
56 #include <winnt/obd_class.h>
57 #else
58 #error Unsupported operating system.
59 #endif
60
61 #define OBD_STATFS_NODELAY      0x0001  /* requests should be send without delay
62                                          * and resends for avoid deadlocks */
63 #define OBD_STATFS_FROM_CACHE   0x0002  /* the statfs callback should not update
64                                          * obd_osfs_age */
65 #define OBD_STATFS_PTLRPCD      0x0004  /* requests will be sent via ptlrpcd
66                                          * instead of a specific set. This
67                                          * means that we cannot rely on the set
68                                          * interpret routine to be called.
69                                          * lov_statfs_fini() must thus be called
70                                          * by the request interpret routine */
71 #define OBD_STATFS_FOR_MDT0     0x0008  /* The statfs is only for retrieving
72                                          * information from MDT0. */
73
74 /* OBD Device Declarations */
75 extern struct obd_device *obd_devs[MAX_OBD_DEVICES];
76 extern struct list_head obd_types;
77 extern spinlock_t obd_types_lock;
78 extern rwlock_t obd_dev_lock;
79
80 /* OBD Operations Declarations */
81 extern struct obd_device *class_conn2obd(struct lustre_handle *);
82 extern struct obd_device *class_exp2obd(struct obd_export *);
83 extern int class_handle_ioctl(unsigned int cmd, unsigned long arg);
84 extern int lustre_get_jobid(char *jobid);
85
86 struct lu_device_type;
87
88 /* genops.c */
89 struct obd_export *class_conn2export(struct lustre_handle *);
90 int class_register_type(struct obd_ops *, struct md_ops *, bool enable_proc,
91                         struct lprocfs_seq_vars *module_vars,
92 #ifndef HAVE_ONLY_PROCFS_SEQ
93                         struct lprocfs_vars *,
94 #endif
95                         const char *nm, struct lu_device_type *ldt);
96 int class_unregister_type(const char *nm);
97
98 struct obd_device *class_newdev(const char *type_name, const char *name);
99 void class_release_dev(struct obd_device *obd);
100
101 int class_name2dev(const char *name);
102 struct obd_device *class_name2obd(const char *name);
103 int class_uuid2dev(struct obd_uuid *uuid);
104 struct obd_device *class_uuid2obd(struct obd_uuid *uuid);
105 void class_obd_list(void);
106 struct obd_device * class_find_client_obd(struct obd_uuid *tgt_uuid,
107                                           const char * typ_name,
108                                           struct obd_uuid *grp_uuid);
109 struct obd_device * class_devices_in_group(struct obd_uuid *grp_uuid,
110                                            int *next);
111 struct obd_device * class_num2obd(int num);
112 int get_devices_count(void);
113
114 int class_notify_sptlrpc_conf(const char *fsname, int namelen);
115
116 char *obd_export_nid2str(struct obd_export *exp);
117
118 int obd_export_evict_by_nid(struct obd_device *obd, const char *nid);
119 int obd_export_evict_by_uuid(struct obd_device *obd, const char *uuid);
120 int obd_connect_flags2str(char *page, int count, __u64 flags, char *sep);
121
122 int obd_zombie_impexp_init(void);
123 void obd_zombie_impexp_stop(void);
124 void obd_zombie_impexp_cull(void);
125 void obd_zombie_barrier(void);
126 void obd_exports_barrier(struct obd_device *obd);
127 int kuc_len(int payload_len);
128 struct kuc_hdr * kuc_ptr(void *p);
129 int kuc_ispayload(void *p);
130 void *kuc_alloc(int payload_len, int transport, int type);
131 void kuc_free(void *p, int payload_len);
132 int obd_get_request_slot(struct client_obd *cli);
133 void obd_put_request_slot(struct client_obd *cli);
134 __u32 obd_get_max_rpcs_in_flight(struct client_obd *cli);
135 int obd_set_max_rpcs_in_flight(struct client_obd *cli, __u32 max);
136
137 struct llog_handle;
138 struct llog_rec_hdr;
139 typedef int (*llog_cb_t)(const struct lu_env *, struct llog_handle *,
140                          struct llog_rec_hdr *, void *);
141 /* obd_config.c */
142 struct lustre_cfg *lustre_cfg_rename(struct lustre_cfg *cfg,
143                                      const char *new_name);
144 int class_process_config(struct lustre_cfg *lcfg);
145 #ifndef HAVE_ONLY_PROCFS_SEQ
146 int class_process_proc_param(char *prefix, struct lprocfs_vars *lvars,
147                              struct lustre_cfg *lcfg, void *data);
148 #endif
149 int class_process_proc_seq_param(char *prefix, struct lprocfs_seq_vars *lvars,
150                                  struct lustre_cfg *lcfg, void *data);
151 int class_attach(struct lustre_cfg *lcfg);
152 int class_setup(struct obd_device *obd, struct lustre_cfg *lcfg);
153 int class_cleanup(struct obd_device *obd, struct lustre_cfg *lcfg);
154 int class_detach(struct obd_device *obd, struct lustre_cfg *lcfg);
155 struct obd_device *class_incref(struct obd_device *obd,
156                                 const char *scope, const void *source);
157 void class_decref(struct obd_device *obd,
158                   const char *scope, const void *source);
159 void dump_exports(struct obd_device *obd, int locks);
160 int class_config_llog_handler(const struct lu_env *env,
161                               struct llog_handle *handle,
162                               struct llog_rec_hdr *rec, void *data);
163 int class_add_conn(struct obd_device *obd, struct lustre_cfg *lcfg);
164 int class_add_uuid(const char *uuid, __u64 nid);
165
166 #define CFG_F_START     0x01   /* Set when we start updating from a log */
167 #define CFG_F_MARKER    0x02   /* We are within a maker */
168 #define CFG_F_SKIP      0x04   /* We should ignore this cfg command */
169 #define CFG_F_COMPAT146 0x08   /* Allow old-style logs */
170 #define CFG_F_EXCLUDE   0x10   /* OST exclusion list */
171
172 /* Passed as data param to class_config_parse_llog */
173 struct config_llog_instance {
174         char                    *cfg_obdname;
175         void                    *cfg_instance;
176         struct super_block      *cfg_sb;
177         struct obd_uuid          cfg_uuid;
178         llog_cb_t                cfg_callback;
179         int                      cfg_last_idx; /* for partial llog processing */
180         int                      cfg_flags;
181         __u32                    cfg_lwp_idx;
182 };
183 int class_config_parse_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
184                             char *name, struct config_llog_instance *cfg);
185 int class_config_dump_llog(const struct lu_env *env, struct llog_ctxt *ctxt,
186                            char *name, struct config_llog_instance *cfg);
187
188 enum {
189         CONFIG_T_CONFIG  = 0,
190         CONFIG_T_SPTLRPC = 1,
191         CONFIG_T_RECOVER = 2,
192         CONFIG_T_PARAMS  = 3,
193         CONFIG_T_MAX     = 4
194 };
195
196 #define PARAMS_FILENAME "params"
197 #define LCTL_UPCALL     "lctl"
198
199 /* list of active configuration logs  */
200 struct config_llog_data {
201         struct ldlm_res_id          cld_resid;
202         struct config_llog_instance cld_cfg;
203         struct list_head            cld_list_chain;
204         atomic_t                    cld_refcount;
205         struct config_llog_data    *cld_sptlrpc;/* depended sptlrpc log */
206         struct config_llog_data    *cld_params; /* common parameters log */
207         struct config_llog_data    *cld_recover;/* imperative recover log */
208         struct obd_export          *cld_mgcexp;
209         struct mutex                cld_lock;
210         int                         cld_type;
211         unsigned int                cld_stopping:1, /* we were told to stop
212                                                      * watching */
213                                     cld_lostlock:1; /* lock not requeued */
214         char                        cld_logname[0];
215 };
216
217 struct lustre_profile {
218         struct list_head         lp_list;
219         char                    *lp_profile;
220         char                    *lp_dt;
221         char                    *lp_md;
222 };
223
224 struct lustre_profile *class_get_profile(const char * prof);
225 void class_del_profile(const char *prof);
226 void class_del_profiles(void);
227
228 #if LUSTRE_TRACKS_LOCK_EXP_REFS
229
230 void __class_export_add_lock_ref(struct obd_export *, struct ldlm_lock *);
231 void __class_export_del_lock_ref(struct obd_export *, struct ldlm_lock *);
232 extern void (*class_export_dump_hook)(struct obd_export *);
233
234 #else
235
236 #define __class_export_add_lock_ref(exp, lock)             do {} while(0)
237 #define __class_export_del_lock_ref(exp, lock)             do {} while(0)
238
239 #endif
240
241 #define class_export_rpc_inc(exp)                                       \
242 ({                                                                      \
243         atomic_inc(&(exp)->exp_rpc_count);                              \
244         CDEBUG(D_INFO, "RPC GETting export %p : new rpc_count %d\n",    \
245                (exp), atomic_read(&(exp)->exp_rpc_count));              \
246 })
247
248 #define class_export_rpc_dec(exp)                                       \
249 ({                                                                      \
250         LASSERT_ATOMIC_POS(&exp->exp_rpc_count);                        \
251         atomic_dec(&(exp)->exp_rpc_count);                              \
252         CDEBUG(D_INFO, "RPC PUTting export %p : new rpc_count %d\n",    \
253                (exp), atomic_read(&(exp)->exp_rpc_count));              \
254 })
255
256 #define class_export_lock_get(exp, lock)                                \
257 ({                                                                      \
258         atomic_inc(&(exp)->exp_locks_count);                            \
259         __class_export_add_lock_ref(exp, lock);                         \
260         CDEBUG(D_INFO, "lock GETting export %p : new locks_count %d\n", \
261                (exp), atomic_read(&(exp)->exp_locks_count));            \
262         class_export_get(exp);                                          \
263 })
264
265 #define class_export_lock_put(exp, lock)                                \
266 ({                                                                      \
267         LASSERT_ATOMIC_POS(&exp->exp_locks_count);                      \
268         atomic_dec(&(exp)->exp_locks_count);                            \
269         __class_export_del_lock_ref(exp, lock);                         \
270         CDEBUG(D_INFO, "lock PUTting export %p : new locks_count %d\n", \
271                (exp), atomic_read(&(exp)->exp_locks_count));            \
272         class_export_put(exp);                                          \
273 })
274
275 #define class_export_cb_get(exp)                                        \
276 ({                                                                      \
277         atomic_inc(&(exp)->exp_cb_count);                               \
278         CDEBUG(D_INFO, "callback GETting export %p : new cb_count %d\n",\
279                (exp), atomic_read(&(exp)->exp_cb_count));               \
280         class_export_get(exp);                                          \
281 })
282
283 #define class_export_cb_put(exp)                                        \
284 ({                                                                      \
285         LASSERT_ATOMIC_POS(&exp->exp_cb_count);                         \
286         atomic_dec(&(exp)->exp_cb_count);                               \
287         CDEBUG(D_INFO, "callback PUTting export %p : new cb_count %d\n",\
288                (exp), atomic_read(&(exp)->exp_cb_count));               \
289         class_export_put(exp);                                          \
290 })
291
292 /* genops.c */
293 struct obd_export *class_export_get(struct obd_export *exp);
294 void class_export_put(struct obd_export *exp);
295 struct obd_export *class_new_export(struct obd_device *obddev,
296                                     struct obd_uuid *cluuid);
297 void class_unlink_export(struct obd_export *exp);
298
299 struct obd_import *class_import_get(struct obd_import *);
300 void class_import_put(struct obd_import *);
301 struct obd_import *class_new_import(struct obd_device *obd);
302 void class_destroy_import(struct obd_import *exp);
303
304 struct obd_type *class_search_type(const char *name);
305 struct obd_type *class_get_type(const char *name);
306 void class_put_type(struct obd_type *type);
307 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
308                   struct obd_uuid *cluuid);
309 int class_disconnect(struct obd_export *exp);
310 void class_fail_export(struct obd_export *exp);
311 int class_connected_export(struct obd_export *exp);
312 void class_disconnect_exports(struct obd_device *obddev);
313 int class_manual_cleanup(struct obd_device *obd);
314 void class_disconnect_stale_exports(struct obd_device *,
315                                     int (*test_export)(struct obd_export *));
316 static inline enum obd_option exp_flags_from_obd(struct obd_device *obd)
317 {
318         return ((obd->obd_fail ? OBD_OPT_FAILOVER : 0) |
319                 (obd->obd_force ? OBD_OPT_FORCE : 0) |
320                 (obd->obd_abort_recovery ? OBD_OPT_ABORT_RECOV : 0) |
321                 0);
322 }
323
324 #ifdef HAVE_SERVER_SUPPORT
325 static inline struct lu_target *class_exp2tgt(struct obd_export *exp)
326 {
327         LASSERT(exp->exp_obd);
328         return exp->exp_obd->u.obt.obt_lut;
329 }
330
331 static inline struct lr_server_data *class_server_data(struct obd_device *obd)
332 {
333         LASSERT(obd->u.obt.obt_lut);
334         return &obd->u.obt.obt_lut->lut_lsd;
335 }
336 #endif
337
338 void obdo_cpy_md(struct obdo *dst, const struct obdo *src, obd_flag valid);
339 void obdo_to_ioobj(const struct obdo *oa, struct obd_ioobj *ioobj);
340 void md_from_obdo(struct md_op_data *op_data, const struct obdo *oa,
341                   obd_flag valid);
342
343 #define OBT(dev)        (dev)->obd_type
344 #define OBP(dev, op)    (dev)->obd_type->typ_dt_ops->o_ ## op
345 #define MDP(dev, op)    (dev)->obd_type->typ_md_ops->m_ ## op
346 #define CTXTP(ctxt, op) (ctxt)->loc_logops->lop_##op
347
348 /* Ensure obd_setup: used for cleanup which must be called
349    while obd is stopping */
350 #define OBD_CHECK_DEV(obd)                                      \
351 do {                                                            \
352         if (!(obd)) {                                           \
353                 CERROR("NULL device\n");                        \
354                 RETURN(-ENODEV);                                \
355         }                                                       \
356 } while (0)
357
358 /* ensure obd_setup and !obd_stopping */
359 #define OBD_CHECK_DEV_ACTIVE(obd)                               \
360 do {                                                            \
361         OBD_CHECK_DEV(obd);                                     \
362         if (!(obd)->obd_set_up || (obd)->obd_stopping) {        \
363                 CERROR("Device %d not setup\n",                 \
364                        (obd)->obd_minor);                       \
365                 RETURN(-ENODEV);                                \
366         }                                                       \
367 } while (0)
368
369
370 #ifdef LPROCFS
371 #define OBD_COUNTER_OFFSET(op)                                                 \
372         ((offsetof(struct obd_ops, o_ ## op) -                                 \
373           offsetof(struct obd_ops, o_iocontrol))                               \
374          / sizeof(((struct obd_ops *)NULL)->o_iocontrol))
375
376 /* The '- 1' below is for o_owner. */
377 #define NUM_OBD_STATS                                                          \
378         (sizeof(struct obd_ops) /                                              \
379          sizeof(((struct obd_ops *)NULL)->o_iocontrol) - 1)
380
381 #define OBD_COUNTER_INCREMENT(obd, op)                                         \
382         lprocfs_counter_incr((obd)->obd_stats,                                 \
383                              (obd)->obd_cntr_base + OBD_COUNTER_OFFSET(op))
384
385 #define EXP_COUNTER_INCREMENT(exp, op)                                         \
386         do {                                                                   \
387                 unsigned int _off;                                             \
388                 _off = (exp)->exp_obd->obd_cntr_base + OBD_COUNTER_OFFSET(op); \
389                 lprocfs_counter_incr((exp)->exp_obd->obd_stats, _off);         \
390                 if ((exp)->exp_obd->obd_uses_nid_stats &&                      \
391                     (exp)->exp_nid_stats != NULL)                              \
392                         lprocfs_counter_incr((exp)->exp_nid_stats->nid_stats,  \
393                                              _off);                            \
394         } while (0)
395
396 #define _MD_COUNTER_OFFSET(m_op)                                               \
397         ((offsetof(struct md_ops, m_op) -                                      \
398           offsetof(struct md_ops, MD_STATS_FIRST_OP)) /                        \
399          sizeof(((struct md_ops *)NULL)->MD_STATS_FIRST_OP))
400
401 #define MD_COUNTER_OFFSET(op) _MD_COUNTER_OFFSET(m_ ## op)
402
403 #define NUM_MD_STATS                                                           \
404         (_MD_COUNTER_OFFSET(MD_STATS_LAST_OP) -                                \
405          _MD_COUNTER_OFFSET(MD_STATS_FIRST_OP) + 1)
406
407 /* Note that we only increment md counters for ops whose offset is less
408  * than NUM_MD_STATS. This is explained in a comment in the definition
409  * of struct md_ops. */
410 #define EXP_MD_COUNTER_INCREMENT(exp, op)                                      \
411         do {                                                                   \
412                 if (MD_COUNTER_OFFSET(op) < NUM_MD_STATS)                      \
413                         lprocfs_counter_incr((exp)->exp_obd->obd_md_stats,     \
414                                         (exp)->exp_obd->obd_md_cntr_base +     \
415                                         MD_COUNTER_OFFSET(op));                \
416         } while (0)
417
418 #else
419 #define OBD_COUNTER_OFFSET(op)
420 #define OBD_COUNTER_INCREMENT(obd, op)
421 #define EXP_COUNTER_INCREMENT(exp, op)
422 #define EXP_MD_COUNTER_INCREMENT(exp, op)
423 #endif
424
425 static inline int lprocfs_nid_ldlm_stats_init(struct nid_stat* tmp)
426 {
427         /* Always add in ldlm_stats */
428         tmp->nid_ldlm_stats = lprocfs_alloc_stats(LDLM_LAST_OPC - LDLM_FIRST_OPC
429                                                   ,LPROCFS_STATS_FLAG_NOPERCPU);
430         if (tmp->nid_ldlm_stats == NULL)
431                 return -ENOMEM;
432
433         lprocfs_init_ldlm_stats(tmp->nid_ldlm_stats);
434
435         return lprocfs_register_stats(tmp->nid_proc, "ldlm_stats",
436                                       tmp->nid_ldlm_stats);
437 }
438
439 #define EXP_CHECK_MD_OP(exp, op)                                \
440 do {                                                            \
441         if ((exp) == NULL) {                                    \
442                 CERROR("obd_" #op ": NULL export\n");           \
443                 RETURN(-ENODEV);                                \
444         }                                                       \
445         if ((exp)->exp_obd == NULL || !OBT((exp)->exp_obd)) {   \
446                 CERROR("obd_" #op ": cleaned up obd\n");        \
447                 RETURN(-EOPNOTSUPP);                            \
448         }                                                       \
449         if (!OBT((exp)->exp_obd) || !MDP((exp)->exp_obd, op)) { \
450                 CERROR("obd_" #op ": dev %s/%d no operation\n", \
451                        (exp)->exp_obd->obd_name,                \
452                        (exp)->exp_obd->obd_minor);              \
453                 RETURN(-EOPNOTSUPP);                            \
454         }                                                       \
455 } while (0)
456
457
458 #define OBD_CHECK_DT_OP(obd, op, err)                           \
459 do {                                                            \
460         if (!OBT(obd) || !OBP((obd), op)) {                     \
461                 if (err)                                        \
462                         CERROR("obd_" #op ": dev %d no operation\n",    \
463                                obd->obd_minor);                 \
464                 RETURN(err);                                    \
465         }                                                       \
466 } while (0)
467
468 #define EXP_CHECK_DT_OP(exp, op)                                \
469 do {                                                            \
470         if ((exp) == NULL) {                                    \
471                 CERROR("obd_" #op ": NULL export\n");           \
472                 RETURN(-ENODEV);                                \
473         }                                                       \
474         if ((exp)->exp_obd == NULL || !OBT((exp)->exp_obd)) {   \
475                 CERROR("obd_" #op ": cleaned up obd\n");        \
476                 RETURN(-EOPNOTSUPP);                            \
477         }                                                       \
478         if (!OBT((exp)->exp_obd) || !OBP((exp)->exp_obd, op)) { \
479                 CERROR("obd_" #op ": dev %d no operation\n",    \
480                        (exp)->exp_obd->obd_minor);              \
481                 RETURN(-EOPNOTSUPP);                            \
482         }                                                       \
483 } while (0)
484
485 #define CTXT_CHECK_OP(ctxt, op, err)                                 \
486 do {                                                                 \
487         if (!OBT(ctxt->loc_obd) || !CTXTP((ctxt), op)) {             \
488                 if (err)                                             \
489                         CERROR("lop_" #op ": dev %d no operation\n", \
490                                ctxt->loc_obd->obd_minor);            \
491                 RETURN(err);                                         \
492         }                                                            \
493 } while (0)
494
495 static inline int class_devno_max(void)
496 {
497         return MAX_OBD_DEVICES;
498 }
499
500 static inline int obd_get_info(const struct lu_env *env,
501                                struct obd_export *exp, __u32 keylen,
502                                void *key, __u32 *vallen, void *val,
503                                struct lov_stripe_md *lsm)
504 {
505         int rc;
506         ENTRY;
507
508         EXP_CHECK_DT_OP(exp, get_info);
509         EXP_COUNTER_INCREMENT(exp, get_info);
510
511         rc = OBP(exp->exp_obd, get_info)(env, exp, keylen, key, vallen, val,
512                                          lsm);
513         RETURN(rc);
514 }
515
516 static inline int obd_set_info_async(const struct lu_env *env,
517                                      struct obd_export *exp, obd_count keylen,
518                                      void *key, obd_count vallen, void *val,
519                                      struct ptlrpc_request_set *set)
520 {
521         int rc;
522         ENTRY;
523
524         EXP_CHECK_DT_OP(exp, set_info_async);
525         EXP_COUNTER_INCREMENT(exp, set_info_async);
526
527         rc = OBP(exp->exp_obd, set_info_async)(env, exp, keylen, key, vallen,
528                                                val, set);
529         RETURN(rc);
530 }
531
532 /*
533  * obd-lu integration.
534  *
535  * Functionality is being moved into new lu_device-based layering, but some
536  * pieces of configuration process are still based on obd devices.
537  *
538  * Specifically, lu_device_type_operations::ldto_device_alloc() methods fully
539  * subsume ->o_setup() methods of obd devices they replace. The same for
540  * lu_device_operations::ldo_process_config() and ->o_process_config(). As a
541  * result, obd_setup() and obd_process_config() branch and call one XOR
542  * another.
543  *
544  * Yet neither lu_device_type_operations::ldto_device_fini() nor
545  * lu_device_type_operations::ldto_device_free() fully implement the
546  * functionality of ->o_precleanup() and ->o_cleanup() they override. Hence,
547  * obd_precleanup() and obd_cleanup() call both lu_device and obd operations.
548  */
549
550 #define DECLARE_LU_VARS(ldt, d)                 \
551         struct lu_device_type *ldt;       \
552         struct lu_device *d
553
554 static inline int obd_setup(struct obd_device *obd, struct lustre_cfg *cfg)
555 {
556         int rc;
557         DECLARE_LU_VARS(ldt, d);
558         ENTRY;
559
560         ldt = obd->obd_type->typ_lu;
561         if (ldt != NULL) {
562                 struct lu_context  session_ctx;
563                 struct lu_env env;
564                 lu_context_init(&session_ctx, LCT_SESSION | LCT_SERVER_SESSION);
565                 session_ctx.lc_thread = NULL;
566                 lu_context_enter(&session_ctx);
567
568                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
569                 if (rc == 0) {
570                         env.le_ses = &session_ctx;
571                         d = ldt->ldt_ops->ldto_device_alloc(&env, ldt, cfg);
572                         lu_env_fini(&env);
573                         if (!IS_ERR(d)) {
574                                 obd->obd_lu_dev = d;
575                                 d->ld_obd = obd;
576                                 rc = 0;
577                         } else
578                                 rc = PTR_ERR(d);
579                 }
580                 lu_context_exit(&session_ctx);
581                 lu_context_fini(&session_ctx);
582
583         } else {
584                 OBD_CHECK_DT_OP(obd, setup, -EOPNOTSUPP);
585                 OBD_COUNTER_INCREMENT(obd, setup);
586                 rc = OBP(obd, setup)(obd, cfg);
587         }
588         RETURN(rc);
589 }
590
591 static inline int obd_precleanup(struct obd_device *obd,
592                                  enum obd_cleanup_stage cleanup_stage)
593 {
594         int rc;
595         DECLARE_LU_VARS(ldt, d);
596         ENTRY;
597
598         OBD_CHECK_DEV(obd);
599         ldt = obd->obd_type->typ_lu;
600         d = obd->obd_lu_dev;
601         if (ldt != NULL && d != NULL) {
602                 if (cleanup_stage == OBD_CLEANUP_EXPORTS) {
603                         struct lu_env env;
604
605                         rc = lu_env_init(&env, ldt->ldt_ctx_tags);
606                         if (rc == 0) {
607                                 ldt->ldt_ops->ldto_device_fini(&env, d);
608                                 lu_env_fini(&env);
609                         }
610                 }
611         }
612         OBD_CHECK_DT_OP(obd, precleanup, 0);
613         OBD_COUNTER_INCREMENT(obd, precleanup);
614
615         rc = OBP(obd, precleanup)(obd, cleanup_stage);
616         RETURN(rc);
617 }
618
619 static inline int obd_cleanup(struct obd_device *obd)
620 {
621         int rc;
622         DECLARE_LU_VARS(ldt, d);
623         ENTRY;
624
625         OBD_CHECK_DEV(obd);
626
627         ldt = obd->obd_type->typ_lu;
628         d = obd->obd_lu_dev;
629         if (ldt != NULL && d != NULL) {
630                 struct lu_env env;
631
632                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
633                 if (rc == 0) {
634                         ldt->ldt_ops->ldto_device_free(&env, d);
635                         lu_env_fini(&env);
636                         obd->obd_lu_dev = NULL;
637                 }
638         }
639         OBD_CHECK_DT_OP(obd, cleanup, 0);
640         OBD_COUNTER_INCREMENT(obd, cleanup);
641
642         rc = OBP(obd, cleanup)(obd);
643         RETURN(rc);
644 }
645
646 static inline void obd_cleanup_client_import(struct obd_device *obd)
647 {
648         ENTRY;
649
650         /* If we set up but never connected, the
651            client import will not have been cleaned. */
652         down_write(&obd->u.cli.cl_sem);
653         if (obd->u.cli.cl_import) {
654                 struct obd_import *imp;
655                 imp = obd->u.cli.cl_import;
656                 CDEBUG(D_CONFIG, "%s: client import never connected\n",
657                        obd->obd_name);
658                 ptlrpc_invalidate_import(imp);
659                 if (imp->imp_rq_pool) {
660                         ptlrpc_free_rq_pool(imp->imp_rq_pool);
661                         imp->imp_rq_pool = NULL;
662                 }
663                 client_destroy_import(imp);
664                 obd->u.cli.cl_import = NULL;
665         }
666         up_write(&obd->u.cli.cl_sem);
667
668         EXIT;
669 }
670
671 static inline int
672 obd_process_config(struct obd_device *obd, int datalen, void *data)
673 {
674         int rc;
675         DECLARE_LU_VARS(ldt, d);
676         ENTRY;
677
678         OBD_CHECK_DEV(obd);
679
680         obd->obd_process_conf = 1;
681         ldt = obd->obd_type->typ_lu;
682         d = obd->obd_lu_dev;
683         if (ldt != NULL && d != NULL) {
684                 struct lu_env env;
685
686                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
687                 if (rc == 0) {
688                         rc = d->ld_ops->ldo_process_config(&env, d, data);
689                         lu_env_fini(&env);
690                 }
691         } else {
692                 OBD_CHECK_DT_OP(obd, process_config, -EOPNOTSUPP);
693                 rc = OBP(obd, process_config)(obd, datalen, data);
694         }
695         OBD_COUNTER_INCREMENT(obd, process_config);
696         obd->obd_process_conf = 0;
697
698         RETURN(rc);
699 }
700
701 /* Pack an in-memory MD struct for storage on disk.
702  * Returns +ve size of packed MD (0 for free), or -ve error.
703  *
704  * If @disk_tgt == NULL, MD size is returned (max size if @mem_src == NULL).
705  * If @*disk_tgt != NULL and @mem_src == NULL, @*disk_tgt will be freed.
706  * If @*disk_tgt == NULL, it will be allocated
707  */
708 static inline int obd_packmd(struct obd_export *exp,
709                              struct lov_mds_md **disk_tgt,
710                              struct lov_stripe_md *mem_src)
711 {
712         int rc;
713         ENTRY;
714
715         EXP_CHECK_DT_OP(exp, packmd);
716         EXP_COUNTER_INCREMENT(exp, packmd);
717
718         rc = OBP(exp->exp_obd, packmd)(exp, disk_tgt, mem_src);
719         RETURN(rc);
720 }
721
722 static inline int obd_size_diskmd(struct obd_export *exp,
723                                   struct lov_stripe_md *mem_src)
724 {
725         return obd_packmd(exp, NULL, mem_src);
726 }
727
728 static inline int obd_free_diskmd(struct obd_export *exp,
729                                   struct lov_mds_md **disk_tgt)
730 {
731         LASSERT(disk_tgt);
732         LASSERT(*disk_tgt);
733         /*
734          * LU-2590, for caller's convenience, *disk_tgt could be host
735          * endianness, it needs swab to LE if necessary, while just
736          * lov_mds_md header needs it for figuring out how much memory
737          * needs to be freed.
738          */
739         if ((cpu_to_le32(LOV_MAGIC) != LOV_MAGIC) &&
740             (((*disk_tgt)->lmm_magic == LOV_MAGIC_V1) ||
741              ((*disk_tgt)->lmm_magic == LOV_MAGIC_V3)))
742                 lustre_swab_lov_mds_md(*disk_tgt);
743         return obd_packmd(exp, disk_tgt, NULL);
744 }
745
746 /* Unpack an MD struct from disk to in-memory format.
747  * Returns +ve size of unpacked MD (0 for free), or -ve error.
748  *
749  * If @mem_tgt == NULL, MD size is returned (max size if @disk_src == NULL).
750  * If @*mem_tgt != NULL and @disk_src == NULL, @*mem_tgt will be freed.
751  * If @*mem_tgt == NULL, it will be allocated
752  */
753 static inline int obd_unpackmd(struct obd_export *exp,
754                                struct lov_stripe_md **mem_tgt,
755                                struct lov_mds_md *disk_src,
756                                int disk_len)
757 {
758         int rc;
759         ENTRY;
760
761         EXP_CHECK_DT_OP(exp, unpackmd);
762         EXP_COUNTER_INCREMENT(exp, unpackmd);
763
764         rc = OBP(exp->exp_obd, unpackmd)(exp, mem_tgt, disk_src, disk_len);
765         RETURN(rc);
766 }
767
768 /* helper functions */
769 static inline int obd_alloc_memmd(struct obd_export *exp,
770                                   struct lov_stripe_md **mem_tgt)
771 {
772         LASSERT(mem_tgt);
773         LASSERT(*mem_tgt == NULL);
774         return obd_unpackmd(exp, mem_tgt, NULL, 0);
775 }
776
777 static inline int obd_free_memmd(struct obd_export *exp,
778                                  struct lov_stripe_md **mem_tgt)
779 {
780         int rc;
781
782         LASSERT(mem_tgt);
783         LASSERT(*mem_tgt);
784         rc = obd_unpackmd(exp, mem_tgt, NULL, 0);
785         *mem_tgt = NULL;
786         return rc;
787 }
788
789 static inline int obd_create(const struct lu_env *env, struct obd_export *exp,
790                              struct obdo *obdo, struct lov_stripe_md **ea,
791                              struct obd_trans_info *oti)
792 {
793         int rc;
794         ENTRY;
795
796         EXP_CHECK_DT_OP(exp, create);
797         EXP_COUNTER_INCREMENT(exp, create);
798
799         rc = OBP(exp->exp_obd, create)(env, exp, obdo, ea, oti);
800         RETURN(rc);
801 }
802
803 static inline int obd_destroy(const struct lu_env *env, struct obd_export *exp,
804                               struct obdo *obdo, struct lov_stripe_md *ea,
805                               struct obd_trans_info *oti,
806                               struct obd_export *md_exp, void *capa)
807 {
808         int rc;
809         ENTRY;
810
811         EXP_CHECK_DT_OP(exp, destroy);
812         EXP_COUNTER_INCREMENT(exp, destroy);
813
814         rc = OBP(exp->exp_obd, destroy)(env, exp, obdo, ea, oti, md_exp, capa);
815         RETURN(rc);
816 }
817
818 static inline int obd_getattr(const struct lu_env *env, struct obd_export *exp,
819                               struct obd_info *oinfo)
820 {
821         int rc;
822         ENTRY;
823
824         EXP_CHECK_DT_OP(exp, getattr);
825         EXP_COUNTER_INCREMENT(exp, getattr);
826
827         rc = OBP(exp->exp_obd, getattr)(env, exp, oinfo);
828         RETURN(rc);
829 }
830
831 static inline int obd_getattr_async(struct obd_export *exp,
832                                     struct obd_info *oinfo,
833                                     struct ptlrpc_request_set *set)
834 {
835         int rc;
836         ENTRY;
837
838         EXP_CHECK_DT_OP(exp, getattr_async);
839         EXP_COUNTER_INCREMENT(exp, getattr_async);
840
841         rc = OBP(exp->exp_obd, getattr_async)(exp, oinfo, set);
842         RETURN(rc);
843 }
844
845 static inline int obd_setattr(const struct lu_env *env, struct obd_export *exp,
846                               struct obd_info *oinfo,
847                               struct obd_trans_info *oti)
848 {
849         int rc;
850         ENTRY;
851
852         EXP_CHECK_DT_OP(exp, setattr);
853         EXP_COUNTER_INCREMENT(exp, setattr);
854
855         rc = OBP(exp->exp_obd, setattr)(env, exp, oinfo, oti);
856         RETURN(rc);
857 }
858
859 /* This performs all the requests set init/wait/destroy actions. */
860 static inline int obd_setattr_rqset(struct obd_export *exp,
861                                     struct obd_info *oinfo,
862                                     struct obd_trans_info *oti)
863 {
864         struct ptlrpc_request_set *set = NULL;
865         int rc;
866         ENTRY;
867
868         EXP_CHECK_DT_OP(exp, setattr_async);
869         EXP_COUNTER_INCREMENT(exp, setattr_async);
870
871         set =  ptlrpc_prep_set();
872         if (set == NULL)
873                 RETURN(-ENOMEM);
874
875         rc = OBP(exp->exp_obd, setattr_async)(exp, oinfo, oti, set);
876         if (rc == 0)
877                 rc = ptlrpc_set_wait(set);
878         ptlrpc_set_destroy(set);
879         RETURN(rc);
880 }
881
882 /* This adds all the requests into @set if @set != NULL, otherwise
883    all requests are sent asynchronously without waiting for response. */
884 static inline int obd_setattr_async(struct obd_export *exp,
885                                     struct obd_info *oinfo,
886                                     struct obd_trans_info *oti,
887                                     struct ptlrpc_request_set *set)
888 {
889         int rc;
890         ENTRY;
891
892         EXP_CHECK_DT_OP(exp, setattr_async);
893         EXP_COUNTER_INCREMENT(exp, setattr_async);
894
895         rc = OBP(exp->exp_obd, setattr_async)(exp, oinfo, oti, set);
896         RETURN(rc);
897 }
898
899 static inline int obd_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
900                                int priority)
901 {
902         struct obd_device *obd = imp->imp_obd;
903         int rc;
904         ENTRY;
905
906         OBD_CHECK_DEV_ACTIVE(obd);
907         OBD_CHECK_DT_OP(obd, add_conn, -EOPNOTSUPP);
908         OBD_COUNTER_INCREMENT(obd, add_conn);
909
910         rc = OBP(obd, add_conn)(imp, uuid, priority);
911         RETURN(rc);
912 }
913
914 static inline int obd_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
915 {
916         struct obd_device *obd = imp->imp_obd;
917         int rc;
918         ENTRY;
919
920         OBD_CHECK_DEV_ACTIVE(obd);
921         OBD_CHECK_DT_OP(obd, del_conn, -EOPNOTSUPP);
922         OBD_COUNTER_INCREMENT(obd, del_conn);
923
924         rc = OBP(obd, del_conn)(imp, uuid);
925         RETURN(rc);
926 }
927
928 static inline struct obd_uuid *obd_get_uuid(struct obd_export *exp)
929 {
930         struct obd_uuid *uuid;
931         ENTRY;
932
933         OBD_CHECK_DT_OP(exp->exp_obd, get_uuid, NULL);
934         EXP_COUNTER_INCREMENT(exp, get_uuid);
935
936         uuid = OBP(exp->exp_obd, get_uuid)(exp);
937         RETURN(uuid);
938 }
939
940 /** Create a new /a exp on device /a obd for the uuid /a cluuid
941  * @param exp New export handle
942  * @param d Connect data, supported flags are set, flags also understood
943  *    by obd are returned.
944  */
945 static inline int obd_connect(const struct lu_env *env,
946                               struct obd_export **exp,struct obd_device *obd,
947                               struct obd_uuid *cluuid,
948                               struct obd_connect_data *data,
949                               void *localdata)
950 {
951         int rc;
952         __u64 ocf = data ? data->ocd_connect_flags : 0; /* for post-condition
953                                                    * check */
954         ENTRY;
955
956         OBD_CHECK_DEV_ACTIVE(obd);
957         OBD_CHECK_DT_OP(obd, connect, -EOPNOTSUPP);
958         OBD_COUNTER_INCREMENT(obd, connect);
959
960         rc = OBP(obd, connect)(env, exp, obd, cluuid, data, localdata);
961         /* check that only subset is granted */
962         LASSERT(ergo(data != NULL, (data->ocd_connect_flags & ocf) ==
963                                     data->ocd_connect_flags));
964         RETURN(rc);
965 }
966
967 static inline int obd_reconnect(const struct lu_env *env,
968                                 struct obd_export *exp,
969                                 struct obd_device *obd,
970                                 struct obd_uuid *cluuid,
971                                 struct obd_connect_data *d,
972                                 void *localdata)
973 {
974         int rc;
975         __u64 ocf = d ? d->ocd_connect_flags : 0; /* for post-condition
976                                                    * check */
977
978         ENTRY;
979
980         OBD_CHECK_DEV_ACTIVE(obd);
981         OBD_CHECK_DT_OP(obd, reconnect, 0);
982         OBD_COUNTER_INCREMENT(obd, reconnect);
983
984         rc = OBP(obd, reconnect)(env, exp, obd, cluuid, d, localdata);
985         /* check that only subset is granted */
986         LASSERT(ergo(d != NULL,
987                      (d->ocd_connect_flags & ocf) == d->ocd_connect_flags));
988         RETURN(rc);
989 }
990
991 static inline int obd_disconnect(struct obd_export *exp)
992 {
993         int rc;
994         ENTRY;
995
996         EXP_CHECK_DT_OP(exp, disconnect);
997         EXP_COUNTER_INCREMENT(exp, disconnect);
998
999         rc = OBP(exp->exp_obd, disconnect)(exp);
1000         RETURN(rc);
1001 }
1002
1003 static inline int obd_fid_init(struct obd_device *obd, struct obd_export *exp,
1004                                enum lu_cli_type type)
1005 {
1006         int rc;
1007         ENTRY;
1008
1009         OBD_CHECK_DT_OP(obd, fid_init, 0);
1010         OBD_COUNTER_INCREMENT(obd, fid_init);
1011
1012         rc = OBP(obd, fid_init)(obd, exp, type);
1013         RETURN(rc);
1014 }
1015
1016 static inline int obd_fid_fini(struct obd_device *obd)
1017 {
1018         int rc;
1019         ENTRY;
1020
1021         OBD_CHECK_DT_OP(obd, fid_fini, 0);
1022         OBD_COUNTER_INCREMENT(obd, fid_fini);
1023
1024         rc = OBP(obd, fid_fini)(obd);
1025         RETURN(rc);
1026 }
1027
1028 static inline int obd_fid_alloc(const struct lu_env *env,
1029                                 struct obd_export *exp,
1030                                 struct lu_fid *fid,
1031                                 struct md_op_data *op_data)
1032 {
1033         int rc;
1034         ENTRY;
1035
1036         EXP_CHECK_DT_OP(exp, fid_alloc);
1037         EXP_COUNTER_INCREMENT(exp, fid_alloc);
1038
1039         rc = OBP(exp->exp_obd, fid_alloc)(env, exp, fid, op_data);
1040         RETURN(rc);
1041 }
1042
1043 static inline int obd_ping(const struct lu_env *env, struct obd_export *exp)
1044 {
1045         int rc;
1046         ENTRY;
1047
1048         OBD_CHECK_DT_OP(exp->exp_obd, ping, 0);
1049         EXP_COUNTER_INCREMENT(exp, ping);
1050
1051         rc = OBP(exp->exp_obd, ping)(env, exp);
1052         RETURN(rc);
1053 }
1054
1055 static inline int obd_pool_new(struct obd_device *obd, char *poolname)
1056 {
1057         int rc;
1058         ENTRY;
1059
1060         OBD_CHECK_DT_OP(obd, pool_new, -EOPNOTSUPP);
1061         OBD_COUNTER_INCREMENT(obd, pool_new);
1062
1063         rc = OBP(obd, pool_new)(obd, poolname);
1064         RETURN(rc);
1065 }
1066
1067 static inline int obd_pool_del(struct obd_device *obd, char *poolname)
1068 {
1069         int rc;
1070         ENTRY;
1071
1072         OBD_CHECK_DT_OP(obd, pool_del, -EOPNOTSUPP);
1073         OBD_COUNTER_INCREMENT(obd, pool_del);
1074
1075         rc = OBP(obd, pool_del)(obd, poolname);
1076         RETURN(rc);
1077 }
1078
1079 static inline int obd_pool_add(struct obd_device *obd, char *poolname, char *ostname)
1080 {
1081         int rc;
1082         ENTRY;
1083
1084         OBD_CHECK_DT_OP(obd, pool_add, -EOPNOTSUPP);
1085         OBD_COUNTER_INCREMENT(obd, pool_add);
1086
1087         rc = OBP(obd, pool_add)(obd, poolname, ostname);
1088         RETURN(rc);
1089 }
1090
1091 static inline int obd_pool_rem(struct obd_device *obd, char *poolname, char *ostname)
1092 {
1093         int rc;
1094         ENTRY;
1095
1096         OBD_CHECK_DT_OP(obd, pool_rem, -EOPNOTSUPP);
1097         OBD_COUNTER_INCREMENT(obd, pool_rem);
1098
1099         rc = OBP(obd, pool_rem)(obd, poolname, ostname);
1100         RETURN(rc);
1101 }
1102
1103 static inline void obd_getref(struct obd_device *obd)
1104 {
1105         ENTRY;
1106         if (OBT(obd) && OBP(obd, getref)) {
1107                 OBD_COUNTER_INCREMENT(obd, getref);
1108                 OBP(obd, getref)(obd);
1109         }
1110         EXIT;
1111 }
1112
1113 static inline void obd_putref(struct obd_device *obd)
1114 {
1115         ENTRY;
1116         if (OBT(obd) && OBP(obd, putref)) {
1117                 OBD_COUNTER_INCREMENT(obd, putref);
1118                 OBP(obd, putref)(obd);
1119         }
1120         EXIT;
1121 }
1122
1123 static inline int obd_init_export(struct obd_export *exp)
1124 {
1125         int rc = 0;
1126
1127         ENTRY;
1128         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
1129             OBP((exp)->exp_obd, init_export))
1130                 rc = OBP(exp->exp_obd, init_export)(exp);
1131         RETURN(rc);
1132 }
1133
1134 static inline int obd_destroy_export(struct obd_export *exp)
1135 {
1136         ENTRY;
1137         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
1138             OBP((exp)->exp_obd, destroy_export))
1139                 OBP(exp->exp_obd, destroy_export)(exp);
1140         RETURN(0);
1141 }
1142
1143 /* @max_age is the oldest time in jiffies that we accept using a cached data.
1144  * If the cache is older than @max_age we will get a new value from the
1145  * target.  Use a value of "cfs_time_current() + HZ" to guarantee freshness. */
1146 static inline int obd_statfs_async(struct obd_export *exp,
1147                                    struct obd_info *oinfo,
1148                                    __u64 max_age,
1149                                    struct ptlrpc_request_set *rqset)
1150 {
1151         int rc = 0;
1152         struct obd_device *obd;
1153         ENTRY;
1154
1155         if (exp == NULL || exp->exp_obd == NULL)
1156                 RETURN(-EINVAL);
1157
1158         obd = exp->exp_obd;
1159         OBD_CHECK_DT_OP(obd, statfs, -EOPNOTSUPP);
1160         OBD_COUNTER_INCREMENT(obd, statfs);
1161
1162         CDEBUG(D_SUPER, "%s: osfs %p age "LPU64", max_age "LPU64"\n",
1163                obd->obd_name, &obd->obd_osfs, obd->obd_osfs_age, max_age);
1164         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
1165                 rc = OBP(obd, statfs_async)(exp, oinfo, max_age, rqset);
1166         } else {
1167                 CDEBUG(D_SUPER,"%s: use %p cache blocks "LPU64"/"LPU64
1168                        " objects "LPU64"/"LPU64"\n",
1169                        obd->obd_name, &obd->obd_osfs,
1170                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
1171                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
1172                 spin_lock(&obd->obd_osfs_lock);
1173                 memcpy(oinfo->oi_osfs, &obd->obd_osfs, sizeof(*oinfo->oi_osfs));
1174                 spin_unlock(&obd->obd_osfs_lock);
1175                 oinfo->oi_flags |= OBD_STATFS_FROM_CACHE;
1176                 if (oinfo->oi_cb_up)
1177                         oinfo->oi_cb_up(oinfo, 0);
1178         }
1179         RETURN(rc);
1180 }
1181
1182 static inline int obd_statfs_rqset(struct obd_export *exp,
1183                                    struct obd_statfs *osfs, __u64 max_age,
1184                                    __u32 flags)
1185 {
1186         struct ptlrpc_request_set *set = NULL;
1187         struct obd_info oinfo = { { { 0 } } };
1188         int rc = 0;
1189         ENTRY;
1190
1191         set =  ptlrpc_prep_set();
1192         if (set == NULL)
1193                 RETURN(-ENOMEM);
1194
1195         oinfo.oi_osfs = osfs;
1196         oinfo.oi_flags = flags;
1197         rc = obd_statfs_async(exp, &oinfo, max_age, set);
1198         if (rc == 0)
1199                 rc = ptlrpc_set_wait(set);
1200         ptlrpc_set_destroy(set);
1201         RETURN(rc);
1202 }
1203
1204 /* @max_age is the oldest time in jiffies that we accept using a cached data.
1205  * If the cache is older than @max_age we will get a new value from the
1206  * target.  Use a value of "cfs_time_current() + HZ" to guarantee freshness. */
1207 static inline int obd_statfs(const struct lu_env *env, struct obd_export *exp,
1208                              struct obd_statfs *osfs, __u64 max_age,
1209                              __u32 flags)
1210 {
1211         int rc = 0;
1212         struct obd_device *obd = exp->exp_obd;
1213         ENTRY;
1214
1215         if (obd == NULL)
1216                 RETURN(-EINVAL);
1217
1218         OBD_CHECK_DT_OP(obd, statfs, -EOPNOTSUPP);
1219         OBD_COUNTER_INCREMENT(obd, statfs);
1220
1221         CDEBUG(D_SUPER, "osfs "LPU64", max_age "LPU64"\n",
1222                obd->obd_osfs_age, max_age);
1223         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
1224                 rc = OBP(obd, statfs)(env, exp, osfs, max_age, flags);
1225                 if (rc == 0) {
1226                         spin_lock(&obd->obd_osfs_lock);
1227                         memcpy(&obd->obd_osfs, osfs, sizeof(obd->obd_osfs));
1228                         obd->obd_osfs_age = cfs_time_current_64();
1229                         spin_unlock(&obd->obd_osfs_lock);
1230                 }
1231         } else {
1232                 CDEBUG(D_SUPER, "%s: use %p cache blocks "LPU64"/"LPU64
1233                        " objects "LPU64"/"LPU64"\n",
1234                        obd->obd_name, &obd->obd_osfs,
1235                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
1236                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
1237                 spin_lock(&obd->obd_osfs_lock);
1238                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1239                 spin_unlock(&obd->obd_osfs_lock);
1240         }
1241         RETURN(rc);
1242 }
1243
1244 static inline int obd_preprw(const struct lu_env *env, int cmd,
1245                              struct obd_export *exp, struct obdo *oa,
1246                              int objcount, struct obd_ioobj *obj,
1247                              struct niobuf_remote *remote, int *pages,
1248                              struct niobuf_local *local,
1249                              struct obd_trans_info *oti,
1250                              struct lustre_capa *capa)
1251 {
1252         int rc;
1253         ENTRY;
1254
1255         EXP_CHECK_DT_OP(exp, preprw);
1256         EXP_COUNTER_INCREMENT(exp, preprw);
1257
1258         rc = OBP(exp->exp_obd, preprw)(env, cmd, exp, oa, objcount, obj, remote,
1259                                        pages, local, oti, capa);
1260         RETURN(rc);
1261 }
1262
1263 static inline int obd_commitrw(const struct lu_env *env, int cmd,
1264                                struct obd_export *exp, struct obdo *oa,
1265                                int objcount, struct obd_ioobj *obj,
1266                                struct niobuf_remote *rnb, int pages,
1267                                struct niobuf_local *local,
1268                                struct obd_trans_info *oti, int rc)
1269 {
1270         ENTRY;
1271
1272         EXP_CHECK_DT_OP(exp, commitrw);
1273         EXP_COUNTER_INCREMENT(exp, commitrw);
1274
1275         rc = OBP(exp->exp_obd, commitrw)(env, cmd, exp, oa, objcount, obj,
1276                                          rnb, pages, local, oti, rc);
1277         RETURN(rc);
1278 }
1279
1280 static inline int obd_iocontrol(unsigned int cmd, struct obd_export *exp,
1281                                 int len, void *karg, void *uarg)
1282 {
1283         int rc;
1284         ENTRY;
1285
1286         EXP_CHECK_DT_OP(exp, iocontrol);
1287         EXP_COUNTER_INCREMENT(exp, iocontrol);
1288
1289         rc = OBP(exp->exp_obd, iocontrol)(cmd, exp, len, karg, uarg);
1290         RETURN(rc);
1291 }
1292
1293 static inline int obd_change_cbdata(struct obd_export *exp,
1294                                     struct lov_stripe_md *lsm,
1295                                     ldlm_iterator_t it, void *data)
1296 {
1297         int rc;
1298         ENTRY;
1299
1300         EXP_CHECK_DT_OP(exp, change_cbdata);
1301         EXP_COUNTER_INCREMENT(exp, change_cbdata);
1302
1303         rc = OBP(exp->exp_obd, change_cbdata)(exp, lsm, it, data);
1304         RETURN(rc);
1305 }
1306
1307 static inline int obd_find_cbdata(struct obd_export *exp,
1308                                   struct lov_stripe_md *lsm,
1309                                   ldlm_iterator_t it, void *data)
1310 {
1311         int rc;
1312         ENTRY;
1313
1314         EXP_CHECK_DT_OP(exp, find_cbdata);
1315         EXP_COUNTER_INCREMENT(exp, find_cbdata);
1316
1317         rc = OBP(exp->exp_obd, find_cbdata)(exp, lsm, it, data);
1318         RETURN(rc);
1319 }
1320
1321 static inline void obd_import_event(struct obd_device *obd,
1322                                     struct obd_import *imp,
1323                                     enum obd_import_event event)
1324 {
1325         ENTRY;
1326         if (!obd) {
1327                 CERROR("NULL device\n");
1328                 EXIT;
1329                 return;
1330         }
1331         if (obd->obd_set_up && OBP(obd, import_event)) {
1332                 OBD_COUNTER_INCREMENT(obd, import_event);
1333                 OBP(obd, import_event)(obd, imp, event);
1334         }
1335         EXIT;
1336 }
1337
1338 static inline int obd_notify(struct obd_device *obd,
1339                              struct obd_device *watched,
1340                              enum obd_notify_event ev,
1341                              void *data)
1342 {
1343         int rc;
1344         ENTRY;
1345         OBD_CHECK_DEV(obd);
1346
1347         if (!obd->obd_set_up) {
1348                 CDEBUG(D_HA, "obd %s not set up\n", obd->obd_name);
1349                 RETURN(-EINVAL);
1350         }
1351
1352         if (!OBP(obd, notify)) {
1353                 CDEBUG(D_HA, "obd %s has no notify handler\n", obd->obd_name);
1354                 RETURN(-ENOSYS);
1355         }
1356
1357         OBD_COUNTER_INCREMENT(obd, notify);
1358         rc = OBP(obd, notify)(obd, watched, ev, data);
1359         RETURN(rc);
1360 }
1361
1362 static inline int obd_notify_observer(struct obd_device *observer,
1363                                       struct obd_device *observed,
1364                                       enum obd_notify_event ev,
1365                                       void *data)
1366 {
1367         int rc1;
1368         int rc2;
1369
1370         struct obd_notify_upcall *onu;
1371
1372         if (observer->obd_observer)
1373                 rc1 = obd_notify(observer->obd_observer, observed, ev, data);
1374         else
1375                 rc1 = 0;
1376         /*
1377          * Also, call non-obd listener, if any
1378          */
1379         onu = &observer->obd_upcall;
1380         if (onu->onu_upcall != NULL)
1381                 rc2 = onu->onu_upcall(observer, observed, ev,
1382                                       onu->onu_owner, NULL);
1383         else
1384                 rc2 = 0;
1385
1386         return rc1 ? rc1 : rc2;
1387 }
1388
1389 static inline int obd_quotacheck(struct obd_export *exp,
1390                                  struct obd_quotactl *oqctl)
1391 {
1392         int rc;
1393         ENTRY;
1394
1395         EXP_CHECK_DT_OP(exp, quotacheck);
1396         EXP_COUNTER_INCREMENT(exp, quotacheck);
1397
1398         rc = OBP(exp->exp_obd, quotacheck)(exp->exp_obd, exp, oqctl);
1399         RETURN(rc);
1400 }
1401
1402 static inline int obd_quotactl(struct obd_export *exp,
1403                                struct obd_quotactl *oqctl)
1404 {
1405         int rc;
1406         ENTRY;
1407
1408         EXP_CHECK_DT_OP(exp, quotactl);
1409         EXP_COUNTER_INCREMENT(exp, quotactl);
1410
1411         rc = OBP(exp->exp_obd, quotactl)(exp->exp_obd, exp, oqctl);
1412         RETURN(rc);
1413 }
1414
1415 static inline int obd_health_check(const struct lu_env *env,
1416                                    struct obd_device *obd)
1417 {
1418         /* returns: 0 on healthy
1419          *         >0 on unhealthy + reason code/flag
1420          *            however the only suppored reason == 1 right now
1421          *            We'll need to define some better reasons
1422          *            or flags in the future.
1423          *         <0 on error
1424          */
1425         int rc;
1426         ENTRY;
1427
1428         /* don't use EXP_CHECK_DT_OP, because NULL method is normal here */
1429         if (obd == NULL || !OBT(obd)) {
1430                 CERROR("cleaned up obd\n");
1431                 RETURN(-EOPNOTSUPP);
1432         }
1433         if (!obd->obd_set_up || obd->obd_stopping)
1434                 RETURN(0);
1435         if (!OBP(obd, health_check))
1436                 RETURN(0);
1437
1438         rc = OBP(obd, health_check)(env, obd);
1439         RETURN(rc);
1440 }
1441
1442 static inline int obd_register_observer(struct obd_device *obd,
1443                                         struct obd_device *observer)
1444 {
1445         ENTRY;
1446         OBD_CHECK_DEV(obd);
1447         down_write(&obd->obd_observer_link_sem);
1448         if (obd->obd_observer && observer) {
1449                 up_write(&obd->obd_observer_link_sem);
1450                 RETURN(-EALREADY);
1451         }
1452         obd->obd_observer = observer;
1453         up_write(&obd->obd_observer_link_sem);
1454         RETURN(0);
1455 }
1456
1457 /* metadata helpers */
1458 static inline int md_getstatus(struct obd_export *exp,
1459                                struct lu_fid *fid, struct obd_capa **pc)
1460 {
1461         int rc;
1462         ENTRY;
1463
1464         EXP_CHECK_MD_OP(exp, getstatus);
1465         EXP_MD_COUNTER_INCREMENT(exp, getstatus);
1466         rc = MDP(exp->exp_obd, getstatus)(exp, fid, pc);
1467         RETURN(rc);
1468 }
1469
1470 static inline int md_getattr(struct obd_export *exp, struct md_op_data *op_data,
1471                              struct ptlrpc_request **request)
1472 {
1473         int rc;
1474         ENTRY;
1475         EXP_CHECK_MD_OP(exp, getattr);
1476         EXP_MD_COUNTER_INCREMENT(exp, getattr);
1477         rc = MDP(exp->exp_obd, getattr)(exp, op_data, request);
1478         RETURN(rc);
1479 }
1480
1481 static inline int md_null_inode(struct obd_export *exp,
1482                                    const struct lu_fid *fid)
1483 {
1484         int rc;
1485         ENTRY;
1486         EXP_CHECK_MD_OP(exp, null_inode);
1487         EXP_MD_COUNTER_INCREMENT(exp, null_inode);
1488         rc = MDP(exp->exp_obd, null_inode)(exp, fid);
1489         RETURN(rc);
1490 }
1491
1492 static inline int md_find_cbdata(struct obd_export *exp,
1493                                  const struct lu_fid *fid,
1494                                  ldlm_iterator_t it, void *data)
1495 {
1496         int rc;
1497         ENTRY;
1498         EXP_CHECK_MD_OP(exp, find_cbdata);
1499         EXP_MD_COUNTER_INCREMENT(exp, find_cbdata);
1500         rc = MDP(exp->exp_obd, find_cbdata)(exp, fid, it, data);
1501         RETURN(rc);
1502 }
1503
1504 static inline int md_close(struct obd_export *exp, struct md_op_data *op_data,
1505                            struct md_open_data *mod,
1506                            struct ptlrpc_request **request)
1507 {
1508         int rc;
1509         ENTRY;
1510         EXP_CHECK_MD_OP(exp, close);
1511         EXP_MD_COUNTER_INCREMENT(exp, close);
1512         rc = MDP(exp->exp_obd, close)(exp, op_data, mod, request);
1513         RETURN(rc);
1514 }
1515
1516 static inline int md_create(struct obd_export *exp, struct md_op_data *op_data,
1517                             const void *data, int datalen, int mode, __u32 uid,
1518                             __u32 gid, cfs_cap_t cap_effective, __u64 rdev,
1519                             struct ptlrpc_request **request)
1520 {
1521         int rc;
1522         ENTRY;
1523         EXP_CHECK_MD_OP(exp, create);
1524         EXP_MD_COUNTER_INCREMENT(exp, create);
1525         rc = MDP(exp->exp_obd, create)(exp, op_data, data, datalen, mode,
1526                                        uid, gid, cap_effective, rdev, request);
1527         RETURN(rc);
1528 }
1529
1530 static inline int md_done_writing(struct obd_export *exp,
1531                                   struct md_op_data *op_data,
1532                                   struct md_open_data *mod)
1533 {
1534         int rc;
1535         ENTRY;
1536         EXP_CHECK_MD_OP(exp, done_writing);
1537         EXP_MD_COUNTER_INCREMENT(exp, done_writing);
1538         rc = MDP(exp->exp_obd, done_writing)(exp, op_data, mod);
1539         RETURN(rc);
1540 }
1541
1542 static inline int md_enqueue(struct obd_export *exp,
1543                              struct ldlm_enqueue_info *einfo,
1544                              const union ldlm_policy_data *policy,
1545                              struct lookup_intent *it,
1546                              struct md_op_data *op_data,
1547                              struct lustre_handle *lockh,
1548                              __u64 extra_lock_flags)
1549 {
1550         int rc;
1551         ENTRY;
1552         EXP_CHECK_MD_OP(exp, enqueue);
1553         EXP_MD_COUNTER_INCREMENT(exp, enqueue);
1554         rc = MDP(exp->exp_obd, enqueue)(exp, einfo, policy, it, op_data, lockh,
1555                                         extra_lock_flags);
1556         RETURN(rc);
1557 }
1558
1559 static inline int md_getattr_name(struct obd_export *exp,
1560                                   struct md_op_data *op_data,
1561                                   struct ptlrpc_request **request)
1562 {
1563         int rc;
1564         ENTRY;
1565         EXP_CHECK_MD_OP(exp, getattr_name);
1566         EXP_MD_COUNTER_INCREMENT(exp, getattr_name);
1567         rc = MDP(exp->exp_obd, getattr_name)(exp, op_data, request);
1568         RETURN(rc);
1569 }
1570
1571 static inline int md_intent_lock(struct obd_export *exp,
1572                                  struct md_op_data *op_data,
1573                                  struct lookup_intent *it,
1574                                  struct ptlrpc_request **reqp,
1575                                  ldlm_blocking_callback cb_blocking,
1576                                  __u64 extra_lock_flags)
1577 {
1578         int rc;
1579         ENTRY;
1580         EXP_CHECK_MD_OP(exp, intent_lock);
1581         EXP_MD_COUNTER_INCREMENT(exp, intent_lock);
1582         rc = MDP(exp->exp_obd, intent_lock)(exp, op_data, it, reqp, cb_blocking,
1583                                             extra_lock_flags);
1584         RETURN(rc);
1585 }
1586
1587 static inline int md_link(struct obd_export *exp, struct md_op_data *op_data,
1588                           struct ptlrpc_request **request)
1589 {
1590         int rc;
1591         ENTRY;
1592         EXP_CHECK_MD_OP(exp, link);
1593         EXP_MD_COUNTER_INCREMENT(exp, link);
1594         rc = MDP(exp->exp_obd, link)(exp, op_data, request);
1595         RETURN(rc);
1596 }
1597
1598 static inline int md_rename(struct obd_export *exp, struct md_op_data *op_data,
1599                             const char *old, int oldlen, const char *new,
1600                             int newlen, struct ptlrpc_request **request)
1601 {
1602         int rc;
1603         ENTRY;
1604         EXP_CHECK_MD_OP(exp, rename);
1605         EXP_MD_COUNTER_INCREMENT(exp, rename);
1606         rc = MDP(exp->exp_obd, rename)(exp, op_data, old, oldlen, new,
1607                                        newlen, request);
1608         RETURN(rc);
1609 }
1610
1611 static inline int md_setattr(struct obd_export *exp, struct md_op_data *op_data,
1612                              void *ea, int ealen, void *ea2, int ea2len,
1613                              struct ptlrpc_request **request,
1614                              struct md_open_data **mod)
1615 {
1616         int rc;
1617         ENTRY;
1618         EXP_CHECK_MD_OP(exp, setattr);
1619         EXP_MD_COUNTER_INCREMENT(exp, setattr);
1620         rc = MDP(exp->exp_obd, setattr)(exp, op_data, ea, ealen,
1621                                         ea2, ea2len, request, mod);
1622         RETURN(rc);
1623 }
1624
1625 static inline int md_fsync(struct obd_export *exp, const struct lu_fid *fid,
1626                            struct obd_capa *oc, struct ptlrpc_request **request)
1627 {
1628         int rc;
1629         ENTRY;
1630         EXP_CHECK_MD_OP(exp, fsync);
1631         EXP_MD_COUNTER_INCREMENT(exp, fsync);
1632         rc = MDP(exp->exp_obd, fsync)(exp, fid, oc, request);
1633         RETURN(rc);
1634 }
1635
1636 static inline int md_read_page(struct obd_export *exp,
1637                                struct md_op_data *op_data,
1638                                struct md_callback *cb_op,
1639                                __u64  hash_offset,
1640                                struct page **ppage)
1641 {
1642         int rc;
1643         ENTRY;
1644         EXP_CHECK_MD_OP(exp, read_page);
1645         EXP_MD_COUNTER_INCREMENT(exp, read_page);
1646         rc = MDP(exp->exp_obd, read_page)(exp, op_data, cb_op, hash_offset,
1647                                           ppage);
1648         RETURN(rc);
1649 }
1650
1651 static inline int md_unlink(struct obd_export *exp, struct md_op_data *op_data,
1652                             struct ptlrpc_request **request)
1653 {
1654         int rc;
1655         ENTRY;
1656         EXP_CHECK_MD_OP(exp, unlink);
1657         EXP_MD_COUNTER_INCREMENT(exp, unlink);
1658         rc = MDP(exp->exp_obd, unlink)(exp, op_data, request);
1659         RETURN(rc);
1660 }
1661
1662 static inline int md_get_lustre_md(struct obd_export *exp,
1663                                    struct ptlrpc_request *req,
1664                                    struct obd_export *dt_exp,
1665                                    struct obd_export *md_exp,
1666                                    struct lustre_md *md)
1667 {
1668         ENTRY;
1669         EXP_CHECK_MD_OP(exp, get_lustre_md);
1670         EXP_MD_COUNTER_INCREMENT(exp, get_lustre_md);
1671         RETURN(MDP(exp->exp_obd, get_lustre_md)(exp, req, dt_exp, md_exp, md));
1672 }
1673
1674 static inline int md_free_lustre_md(struct obd_export *exp,
1675                                     struct lustre_md *md)
1676 {
1677         ENTRY;
1678         EXP_CHECK_MD_OP(exp, free_lustre_md);
1679         EXP_MD_COUNTER_INCREMENT(exp, free_lustre_md);
1680         RETURN(MDP(exp->exp_obd, free_lustre_md)(exp, md));
1681 }
1682
1683 static inline int md_update_lsm_md(struct obd_export *exp,
1684                                    struct lmv_stripe_md *lsm,
1685                                    struct mdt_body *body,
1686                                    ldlm_blocking_callback cb)
1687 {
1688         ENTRY;
1689         EXP_CHECK_MD_OP(exp, update_lsm_md);
1690         EXP_MD_COUNTER_INCREMENT(exp, update_lsm_md);
1691         RETURN(MDP(exp->exp_obd, update_lsm_md)(exp, lsm, body, cb));
1692 }
1693
1694 static inline int md_merge_attr(struct obd_export *exp,
1695                                 const struct lmv_stripe_md *lsm,
1696                                 struct cl_attr *attr)
1697 {
1698         ENTRY;
1699         EXP_CHECK_MD_OP(exp, merge_attr);
1700         EXP_MD_COUNTER_INCREMENT(exp, merge_attr);
1701         RETURN(MDP(exp->exp_obd, merge_attr)(exp, lsm, attr));
1702 }
1703
1704 static inline int md_setxattr(struct obd_export *exp,
1705                               const struct lu_fid *fid, struct obd_capa *oc,
1706                               obd_valid valid, const char *name,
1707                               const char *input, int input_size,
1708                               int output_size, int flags, __u32 suppgid,
1709                               struct ptlrpc_request **request)
1710 {
1711         ENTRY;
1712         EXP_CHECK_MD_OP(exp, setxattr);
1713         EXP_MD_COUNTER_INCREMENT(exp, setxattr);
1714         RETURN(MDP(exp->exp_obd, setxattr)(exp, fid, oc, valid, name, input,
1715                                            input_size, output_size, flags,
1716                                            suppgid, request));
1717 }
1718
1719 static inline int md_getxattr(struct obd_export *exp,
1720                               const struct lu_fid *fid, struct obd_capa *oc,
1721                               obd_valid valid, const char *name,
1722                               const char *input, int input_size,
1723                               int output_size, int flags,
1724                               struct ptlrpc_request **request)
1725 {
1726         ENTRY;
1727         EXP_CHECK_MD_OP(exp, getxattr);
1728         EXP_MD_COUNTER_INCREMENT(exp, getxattr);
1729         RETURN(MDP(exp->exp_obd, getxattr)(exp, fid, oc, valid, name, input,
1730                                            input_size, output_size, flags,
1731                                            request));
1732 }
1733
1734 static inline int md_set_open_replay_data(struct obd_export *exp,
1735                                           struct obd_client_handle *och,
1736                                           struct lookup_intent *it)
1737 {
1738         ENTRY;
1739         EXP_CHECK_MD_OP(exp, set_open_replay_data);
1740         EXP_MD_COUNTER_INCREMENT(exp, set_open_replay_data);
1741         RETURN(MDP(exp->exp_obd, set_open_replay_data)(exp, och, it));
1742 }
1743
1744 static inline int md_clear_open_replay_data(struct obd_export *exp,
1745                                             struct obd_client_handle *och)
1746 {
1747         ENTRY;
1748         EXP_CHECK_MD_OP(exp, clear_open_replay_data);
1749         EXP_MD_COUNTER_INCREMENT(exp, clear_open_replay_data);
1750         RETURN(MDP(exp->exp_obd, clear_open_replay_data)(exp, och));
1751 }
1752
1753 static inline int md_set_lock_data(struct obd_export *exp,
1754                                    __u64 *lockh, void *data, __u64 *bits)
1755 {
1756         ENTRY;
1757         EXP_CHECK_MD_OP(exp, set_lock_data);
1758         EXP_MD_COUNTER_INCREMENT(exp, set_lock_data);
1759         RETURN(MDP(exp->exp_obd, set_lock_data)(exp, lockh, data, bits));
1760 }
1761
1762 static inline int md_cancel_unused(struct obd_export *exp,
1763                                    const struct lu_fid *fid,
1764                                    ldlm_policy_data_t *policy,
1765                                    ldlm_mode_t mode,
1766                                    ldlm_cancel_flags_t flags,
1767                                    void *opaque)
1768 {
1769         int rc;
1770         ENTRY;
1771
1772         EXP_CHECK_MD_OP(exp, cancel_unused);
1773         EXP_MD_COUNTER_INCREMENT(exp, cancel_unused);
1774
1775         rc = MDP(exp->exp_obd, cancel_unused)(exp, fid, policy, mode,
1776                                               flags, opaque);
1777         RETURN(rc);
1778 }
1779
1780 static inline ldlm_mode_t md_lock_match(struct obd_export *exp, __u64 flags,
1781                                         const struct lu_fid *fid,
1782                                         ldlm_type_t type,
1783                                         ldlm_policy_data_t *policy,
1784                                         ldlm_mode_t mode,
1785                                         struct lustre_handle *lockh)
1786 {
1787         ENTRY;
1788         EXP_CHECK_MD_OP(exp, lock_match);
1789         EXP_MD_COUNTER_INCREMENT(exp, lock_match);
1790         RETURN(MDP(exp->exp_obd, lock_match)(exp, flags, fid, type,
1791                                              policy, mode, lockh));
1792 }
1793
1794 static inline int md_init_ea_size(struct obd_export *exp, int easize,
1795                                   int def_asize, int cookiesize,
1796                                   int def_cookiesize)
1797 {
1798         ENTRY;
1799         EXP_CHECK_MD_OP(exp, init_ea_size);
1800         EXP_MD_COUNTER_INCREMENT(exp, init_ea_size);
1801         RETURN(MDP(exp->exp_obd, init_ea_size)(exp, easize, def_asize,
1802                                                cookiesize, def_cookiesize));
1803 }
1804
1805 static inline int md_get_remote_perm(struct obd_export *exp,
1806                                      const struct lu_fid *fid,
1807                                      struct obd_capa *oc, __u32 suppgid,
1808                                      struct ptlrpc_request **request)
1809 {
1810         ENTRY;
1811         EXP_CHECK_MD_OP(exp, get_remote_perm);
1812         EXP_MD_COUNTER_INCREMENT(exp, get_remote_perm);
1813         RETURN(MDP(exp->exp_obd, get_remote_perm)(exp, fid, oc, suppgid,
1814                                                   request));
1815 }
1816
1817 static inline int md_renew_capa(struct obd_export *exp, struct obd_capa *ocapa,
1818                                 renew_capa_cb_t cb)
1819 {
1820         int rc;
1821         ENTRY;
1822         EXP_CHECK_MD_OP(exp, renew_capa);
1823         EXP_MD_COUNTER_INCREMENT(exp, renew_capa);
1824         rc = MDP(exp->exp_obd, renew_capa)(exp, ocapa, cb);
1825         RETURN(rc);
1826 }
1827
1828 static inline int md_unpack_capa(struct obd_export *exp,
1829                                  struct ptlrpc_request *req,
1830                                  const struct req_msg_field *field,
1831                                  struct obd_capa **oc)
1832 {
1833         int rc;
1834         ENTRY;
1835         EXP_CHECK_MD_OP(exp, unpack_capa);
1836         EXP_MD_COUNTER_INCREMENT(exp, unpack_capa);
1837         rc = MDP(exp->exp_obd, unpack_capa)(exp, req, field, oc);
1838         RETURN(rc);
1839 }
1840
1841 static inline int md_intent_getattr_async(struct obd_export *exp,
1842                                           struct md_enqueue_info *minfo,
1843                                           struct ldlm_enqueue_info *einfo)
1844 {
1845         int rc;
1846         ENTRY;
1847         EXP_CHECK_MD_OP(exp, intent_getattr_async);
1848         EXP_MD_COUNTER_INCREMENT(exp, intent_getattr_async);
1849         rc = MDP(exp->exp_obd, intent_getattr_async)(exp, minfo, einfo);
1850         RETURN(rc);
1851 }
1852
1853 static inline int md_revalidate_lock(struct obd_export *exp,
1854                                      struct lookup_intent *it,
1855                                      struct lu_fid *fid, __u64 *bits)
1856 {
1857         int rc;
1858         ENTRY;
1859         EXP_CHECK_MD_OP(exp, revalidate_lock);
1860         EXP_MD_COUNTER_INCREMENT(exp, revalidate_lock);
1861         rc = MDP(exp->exp_obd, revalidate_lock)(exp, it, fid, bits);
1862         RETURN(rc);
1863 }
1864
1865 static inline int md_get_fid_from_lsm(struct obd_export *exp,
1866                                       const struct lmv_stripe_md *lsm,
1867                                       const char *name, int namelen,
1868                                       struct lu_fid *fid)
1869 {
1870         int rc;
1871         ENTRY;
1872         EXP_CHECK_MD_OP(exp, get_fid_from_lsm);
1873         EXP_MD_COUNTER_INCREMENT(exp, get_fid_from_lsm);
1874         rc = MDP(exp->exp_obd, get_fid_from_lsm)(exp, lsm, name, namelen, fid);
1875         RETURN(rc);
1876 }
1877
1878 /* OBD Metadata Support */
1879
1880 extern int obd_init_caches(void);
1881 extern void obd_cleanup_caches(void);
1882
1883 /* support routines */
1884 extern struct kmem_cache *obdo_cachep;
1885
1886 #define OBDO_ALLOC(ptr)                                                       \
1887 do {                                                                          \
1888         OBD_SLAB_ALLOC_PTR_GFP((ptr), obdo_cachep, GFP_NOFS);             \
1889 } while(0)
1890
1891 #define OBDO_FREE(ptr)                                                        \
1892 do {                                                                          \
1893         OBD_SLAB_FREE_PTR((ptr), obdo_cachep);                                \
1894 } while(0)
1895
1896
1897 typedef int (*register_lwp_cb)(void *data);
1898
1899 struct lwp_register_item {
1900         struct obd_export **lri_exp;
1901         register_lwp_cb     lri_cb_func;
1902         void               *lri_cb_data;
1903         struct list_head            lri_list;
1904         char                lri_name[MTI_NAME_MAXLEN];
1905 };
1906
1907 /* I'm as embarrassed about this as you are.
1908  *
1909  * <shaver> // XXX do not look into _superhack with remaining eye
1910  * <shaver> // XXX if this were any uglier, I'd get my own show on MTV */
1911 extern int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
1912
1913 /* obd_mount.c */
1914 #ifdef HAVE_SERVER_SUPPORT
1915 int lustre_register_lwp_item(const char *lwpname, struct obd_export **exp,
1916                              register_lwp_cb cb_func, void *cb_data);
1917 void lustre_deregister_lwp_item(struct obd_export **exp);
1918 struct obd_export *lustre_find_lwp_by_index(const char *dev, __u32 idx);
1919 int tgt_name2lwp_name(const char *tgt_name, char *lwp_name, int len, __u32 idx);
1920 #endif /* HAVE_SERVER_SUPPORT */
1921
1922 /* sysctl.c */
1923 extern void obd_sysctl_init (void);
1924 extern void obd_sysctl_clean (void);
1925
1926 /* uuid.c  */
1927 typedef __u8 class_uuid_t[16];
1928 void class_uuid_unparse(class_uuid_t in, struct obd_uuid *out);
1929
1930 /* lustre_peer.c    */
1931 int lustre_uuid_to_peer(const char *uuid, lnet_nid_t *peer_nid, int index);
1932 int class_add_uuid(const char *uuid, __u64 nid);
1933 int class_del_uuid (const char *uuid);
1934 int class_check_uuid(struct obd_uuid *uuid, __u64 nid);
1935 void class_init_uuidlist(void);
1936 void class_exit_uuidlist(void);
1937
1938 /* prng.c */
1939 #define ll_generate_random_uuid(uuid_out) cfs_get_random_bytes(uuid_out, sizeof(class_uuid_t))
1940
1941 /* root squash info */
1942 struct rw_semaphore;
1943 struct root_squash_info {
1944         uid_t                   rsi_uid;
1945         gid_t                   rsi_gid;
1946         struct list_head        rsi_nosquash_nids;
1947         struct rw_semaphore     rsi_sem;
1948 };
1949
1950 #ifdef __KERNEL__
1951 int server_name2index(const char *svname, __u32 *idx, const char **endptr);
1952 #else
1953 # define server_name2index(name, idx, ptr)      do {} while (0)
1954 #endif
1955
1956 #endif /* __LINUX_OBD_CLASS_H */