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