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