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