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