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