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