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