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