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