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