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