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