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