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