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