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