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