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