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