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