Whamcloud - gitweb
LU-8851 nodemap: add uid/gid only flags to control mapping
[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_fail ? OBD_OPT_FAILOVER : 0) |
321                 (obd->obd_force ? OBD_OPT_FORCE : 0) |
322                 (obd->obd_abort_recovery ? OBD_OPT_ABORT_RECOV : 0) |
323                 0);
324 }
325
326 #ifdef HAVE_SERVER_SUPPORT
327 static inline struct lu_target *class_exp2tgt(struct obd_export *exp)
328 {
329         LASSERT(exp->exp_obd);
330         if (exp->exp_obd->u.obt.obt_magic != OBT_MAGIC)
331                 return NULL;
332         return exp->exp_obd->u.obt.obt_lut;
333 }
334
335 static inline struct lr_server_data *class_server_data(struct obd_device *obd)
336 {
337         LASSERT(obd->u.obt.obt_lut);
338         return &obd->u.obt.obt_lut->lut_lsd;
339 }
340 #endif
341
342 /* obdo.c */
343 struct lu_attr;
344 struct inode;
345
346 void obdo_from_la(struct obdo *dst, const struct lu_attr *la, u64 valid);
347 void la_from_obdo(struct lu_attr *la, const struct obdo *dst, u64 valid);
348
349 void obdo_cpy_md(struct obdo *dst, const struct obdo *src, u64 valid);
350 void obdo_to_ioobj(const struct obdo *oa, struct obd_ioobj *ioobj);
351
352 #define OBT(dev)        (dev)->obd_type
353 #define OBP(dev, op)    (dev)->obd_type->typ_dt_ops->o_ ## op
354 #define MDP(dev, op)    (dev)->obd_type->typ_md_ops->m_ ## op
355 #define CTXTP(ctxt, op) (ctxt)->loc_logops->lop_##op
356
357 /* Ensure obd_setup: used for cleanup which must be called
358    while obd is stopping */
359 #define OBD_CHECK_DEV(obd)                                      \
360 do {                                                            \
361         if (!(obd)) {                                           \
362                 CERROR("NULL device\n");                        \
363                 RETURN(-ENODEV);                                \
364         }                                                       \
365 } while (0)
366
367 /* ensure obd_setup and !obd_stopping */
368 #define OBD_CHECK_DEV_ACTIVE(obd)                               \
369 do {                                                            \
370         OBD_CHECK_DEV(obd);                                     \
371         if (!(obd)->obd_set_up || (obd)->obd_stopping) {        \
372                 CERROR("Device %d not setup\n",                 \
373                        (obd)->obd_minor);                       \
374                 RETURN(-ENODEV);                                \
375         }                                                       \
376 } while (0)
377
378
379 #ifdef CONFIG_PROC_FS
380 #define OBD_COUNTER_OFFSET(op)                                                 \
381         ((offsetof(struct obd_ops, o_ ## op) -                                 \
382           offsetof(struct obd_ops, o_iocontrol))                               \
383          / sizeof(((struct obd_ops *)NULL)->o_iocontrol))
384
385 /* The '- 1' below is for o_owner. */
386 #define NUM_OBD_STATS                                                          \
387         (sizeof(struct obd_ops) /                                              \
388          sizeof(((struct obd_ops *)NULL)->o_iocontrol) - 1)
389
390 #define OBD_COUNTER_INCREMENT(obd, op)                                         \
391         lprocfs_counter_incr((obd)->obd_stats,                                 \
392                              (obd)->obd_cntr_base + OBD_COUNTER_OFFSET(op))
393
394 #define EXP_COUNTER_INCREMENT(exp, op)                                         \
395         do {                                                                   \
396                 unsigned int _off;                                             \
397                 _off = (exp)->exp_obd->obd_cntr_base + OBD_COUNTER_OFFSET(op); \
398                 lprocfs_counter_incr((exp)->exp_obd->obd_stats, _off);         \
399                 if ((exp)->exp_obd->obd_uses_nid_stats &&                      \
400                     (exp)->exp_nid_stats != NULL)                              \
401                         lprocfs_counter_incr((exp)->exp_nid_stats->nid_stats,  \
402                                              _off);                            \
403         } while (0)
404
405 #define _MD_COUNTER_OFFSET(m_op)                                               \
406         ((offsetof(struct md_ops, m_op) -                                      \
407           offsetof(struct md_ops, MD_STATS_FIRST_OP)) /                        \
408          sizeof(((struct md_ops *)NULL)->MD_STATS_FIRST_OP))
409
410 #define MD_COUNTER_OFFSET(op) _MD_COUNTER_OFFSET(m_ ## op)
411
412 #define NUM_MD_STATS                                                           \
413         (_MD_COUNTER_OFFSET(MD_STATS_LAST_OP) -                                \
414          _MD_COUNTER_OFFSET(MD_STATS_FIRST_OP) + 1)
415
416 /* Note that we only increment md counters for ops whose offset is less
417  * than NUM_MD_STATS. This is explained in a comment in the definition
418  * of struct md_ops. */
419 #define EXP_MD_COUNTER_INCREMENT(exp, op)                                      \
420         do {                                                                   \
421                 if (MD_COUNTER_OFFSET(op) < NUM_MD_STATS)                      \
422                         lprocfs_counter_incr((exp)->exp_obd->obd_md_stats,     \
423                                         (exp)->exp_obd->obd_md_cntr_base +     \
424                                         MD_COUNTER_OFFSET(op));                \
425         } while (0)
426
427 #else
428 #define OBD_COUNTER_OFFSET(op)
429 #define OBD_COUNTER_INCREMENT(obd, op)
430 #define EXP_COUNTER_INCREMENT(exp, op)
431 #define EXP_MD_COUNTER_INCREMENT(exp, op)
432 #endif
433
434 static inline int lprocfs_nid_ldlm_stats_init(struct nid_stat* tmp)
435 {
436         /* Always add in ldlm_stats */
437         tmp->nid_ldlm_stats =
438                 lprocfs_alloc_stats(LDLM_LAST_OPC - LDLM_FIRST_OPC,
439                                     LPROCFS_STATS_FLAG_NOPERCPU);
440         if (tmp->nid_ldlm_stats == NULL)
441                 return -ENOMEM;
442
443         lprocfs_init_ldlm_stats(tmp->nid_ldlm_stats);
444
445         return lprocfs_register_stats(tmp->nid_proc, "ldlm_stats",
446                                       tmp->nid_ldlm_stats);
447 }
448
449 #define EXP_CHECK_MD_OP(exp, op)                                        \
450 do {                                                                    \
451         if ((exp) == NULL) {                                            \
452                 CERROR("obd_" #op ": NULL export\n");                   \
453                 RETURN(-ENODEV);                                        \
454         }                                                               \
455         if ((exp)->exp_obd == NULL || !OBT((exp)->exp_obd)) {           \
456                 CERROR("obd_" #op ": cleaned up obd\n");                \
457                 RETURN(-EOPNOTSUPP);                                    \
458         }                                                               \
459         if (!OBT((exp)->exp_obd) || !MDP((exp)->exp_obd, op)) {         \
460                 CERROR("%s: obd_" #op ": dev %d no operation\n",        \
461                        (exp)->exp_obd->obd_name,                        \
462                        (exp)->exp_obd->obd_minor);                      \
463                 RETURN(-EOPNOTSUPP);                                    \
464         }                                                               \
465 } while (0)
466
467
468 #define OBD_CHECK_DT_OP(obd, op, err)                                   \
469 do {                                                                    \
470         if (!OBT(obd) || !OBP((obd), op)) {                             \
471                 if (err)                                                \
472                         CERROR("%s: no obd_" #op " operation\n",        \
473                                obd->obd_name);                          \
474                 RETURN(err);                                            \
475         }                                                               \
476 } while (0)
477
478 #define EXP_CHECK_DT_OP(exp, op)                                        \
479 do {                                                                    \
480         if ((exp) == NULL) {                                            \
481                 CERROR("obd_" #op ": NULL export\n");                   \
482                 RETURN(-ENODEV);                                        \
483         }                                                               \
484         if ((exp)->exp_obd == NULL || !OBT((exp)->exp_obd)) {           \
485                 CERROR("obd_" #op ": cleaned up obd\n");                \
486                 RETURN(-EOPNOTSUPP);                                    \
487         }                                                               \
488         OBD_CHECK_DT_OP((exp)->exp_obd, op, -EOPNOTSUPP);               \
489 } while (0)
490
491 #define CTXT_CHECK_OP(ctxt, op, err)                                    \
492 do {                                                                    \
493         if (!OBT(ctxt->loc_obd) || !CTXTP((ctxt), op)) {                \
494                 if (err)                                                \
495                         CERROR("%s: no lop_" #op "operation\n",         \
496                                ctxt->loc_obd->obd_name);                \
497                 RETURN(err);                                            \
498         }                                                               \
499 } while (0)
500
501 static inline int class_devno_max(void)
502 {
503         return MAX_OBD_DEVICES;
504 }
505
506 static inline int obd_get_info(const struct lu_env *env, struct obd_export *exp,
507                                __u32 keylen, void *key,
508                                __u32 *vallen, void *val)
509 {
510         int rc;
511         ENTRY;
512
513         EXP_CHECK_DT_OP(exp, get_info);
514         EXP_COUNTER_INCREMENT(exp, get_info);
515
516         rc = OBP(exp->exp_obd, get_info)(env, exp, keylen, key, vallen, val);
517         RETURN(rc);
518 }
519
520 static inline int obd_set_info_async(const struct lu_env *env,
521                                      struct obd_export *exp,
522                                      __u32 keylen, void *key,
523                                      __u32 vallen, void *val,
524                                      struct ptlrpc_request_set *set)
525 {
526         int rc;
527         ENTRY;
528
529         EXP_CHECK_DT_OP(exp, set_info_async);
530         EXP_COUNTER_INCREMENT(exp, set_info_async);
531
532         rc = OBP(exp->exp_obd, set_info_async)(env, exp, keylen, key, vallen,
533                                                val, set);
534         RETURN(rc);
535 }
536
537 /*
538  * obd-lu integration.
539  *
540  * Functionality is being moved into new lu_device-based layering, but some
541  * pieces of configuration process are still based on obd devices.
542  *
543  * Specifically, lu_device_type_operations::ldto_device_alloc() methods fully
544  * subsume ->o_setup() methods of obd devices they replace. The same for
545  * lu_device_operations::ldo_process_config() and ->o_process_config(). As a
546  * result, obd_setup() and obd_process_config() branch and call one XOR
547  * another.
548  *
549  * Yet neither lu_device_type_operations::ldto_device_fini() nor
550  * lu_device_type_operations::ldto_device_free() fully implement the
551  * functionality of ->o_precleanup() and ->o_cleanup() they override. Hence,
552  * obd_precleanup() and obd_cleanup() call both lu_device and obd operations.
553  */
554
555 #define DECLARE_LU_VARS(ldt, d)                 \
556         struct lu_device_type *ldt;       \
557         struct lu_device *d
558
559 static inline int obd_setup(struct obd_device *obd, struct lustre_cfg *cfg)
560 {
561         int rc;
562         DECLARE_LU_VARS(ldt, d);
563         ENTRY;
564
565         ldt = obd->obd_type->typ_lu;
566         if (ldt != NULL) {
567                 struct lu_context  session_ctx;
568                 struct lu_env env;
569                 lu_context_init(&session_ctx, LCT_SESSION | LCT_SERVER_SESSION);
570                 session_ctx.lc_thread = NULL;
571                 lu_context_enter(&session_ctx);
572
573                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
574                 if (rc == 0) {
575                         env.le_ses = &session_ctx;
576                         d = ldt->ldt_ops->ldto_device_alloc(&env, ldt, cfg);
577                         lu_env_fini(&env);
578                         if (!IS_ERR(d)) {
579                                 obd->obd_lu_dev = d;
580                                 d->ld_obd = obd;
581                                 rc = 0;
582                         } else
583                                 rc = PTR_ERR(d);
584                 }
585                 lu_context_exit(&session_ctx);
586                 lu_context_fini(&session_ctx);
587
588         } else {
589                 OBD_CHECK_DT_OP(obd, setup, -EOPNOTSUPP);
590                 OBD_COUNTER_INCREMENT(obd, setup);
591                 rc = OBP(obd, setup)(obd, cfg);
592         }
593         RETURN(rc);
594 }
595
596 static inline int obd_precleanup(struct obd_device *obd)
597 {
598         int rc;
599         DECLARE_LU_VARS(ldt, d);
600         ENTRY;
601
602         OBD_CHECK_DEV(obd);
603         ldt = obd->obd_type->typ_lu;
604         d = obd->obd_lu_dev;
605         if (ldt != NULL && d != NULL) {
606                 struct lu_env env;
607
608                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
609                 if (rc == 0) {
610                         ldt->ldt_ops->ldto_device_fini(&env, d);
611                         lu_env_fini(&env);
612                 }
613         }
614         OBD_CHECK_DT_OP(obd, precleanup, 0);
615         OBD_COUNTER_INCREMENT(obd, precleanup);
616
617         rc = OBP(obd, precleanup)(obd);
618         RETURN(rc);
619 }
620
621 static inline int obd_cleanup(struct obd_device *obd)
622 {
623         int rc;
624         DECLARE_LU_VARS(ldt, d);
625         ENTRY;
626
627         OBD_CHECK_DEV(obd);
628
629         ldt = obd->obd_type->typ_lu;
630         d = obd->obd_lu_dev;
631         if (ldt != NULL && d != NULL) {
632                 struct lu_env env;
633
634                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
635                 if (rc == 0) {
636                         ldt->ldt_ops->ldto_device_free(&env, d);
637                         lu_env_fini(&env);
638                         obd->obd_lu_dev = NULL;
639                 }
640         }
641         OBD_CHECK_DT_OP(obd, cleanup, 0);
642         OBD_COUNTER_INCREMENT(obd, cleanup);
643
644         rc = OBP(obd, cleanup)(obd);
645         RETURN(rc);
646 }
647
648 static inline void obd_cleanup_client_import(struct obd_device *obd)
649 {
650         ENTRY;
651
652         /* If we set up but never connected, the
653            client import will not have been cleaned. */
654         down_write(&obd->u.cli.cl_sem);
655         if (obd->u.cli.cl_import) {
656                 struct obd_import *imp;
657                 imp = obd->u.cli.cl_import;
658                 CDEBUG(D_CONFIG, "%s: client import never connected\n",
659                        obd->obd_name);
660                 ptlrpc_invalidate_import(imp);
661                 client_destroy_import(imp);
662                 obd->u.cli.cl_import = NULL;
663         }
664         up_write(&obd->u.cli.cl_sem);
665
666         EXIT;
667 }
668
669 static inline int
670 obd_process_config(struct obd_device *obd, int datalen, void *data)
671 {
672         int rc;
673         DECLARE_LU_VARS(ldt, d);
674         ENTRY;
675
676         OBD_CHECK_DEV(obd);
677
678         obd->obd_process_conf = 1;
679         ldt = obd->obd_type->typ_lu;
680         d = obd->obd_lu_dev;
681         if (ldt != NULL && d != NULL) {
682                 struct lu_env env;
683
684                 rc = lu_env_init(&env, ldt->ldt_ctx_tags);
685                 if (rc == 0) {
686                         rc = d->ld_ops->ldo_process_config(&env, d, data);
687                         lu_env_fini(&env);
688                 }
689         } else {
690                 OBD_CHECK_DT_OP(obd, process_config, -EOPNOTSUPP);
691                 rc = OBP(obd, process_config)(obd, datalen, data);
692         }
693         OBD_COUNTER_INCREMENT(obd, process_config);
694         obd->obd_process_conf = 0;
695
696         RETURN(rc);
697 }
698
699 static inline int obd_create(const struct lu_env *env, struct obd_export *exp,
700                              struct obdo *obdo)
701 {
702         int rc;
703         ENTRY;
704
705         EXP_CHECK_DT_OP(exp, create);
706         EXP_COUNTER_INCREMENT(exp, create);
707
708         rc = OBP(exp->exp_obd, create)(env, exp, obdo);
709         RETURN(rc);
710 }
711
712 static inline int obd_destroy(const struct lu_env *env, struct obd_export *exp,
713                               struct obdo *obdo)
714 {
715         int rc;
716         ENTRY;
717
718         EXP_CHECK_DT_OP(exp, destroy);
719         EXP_COUNTER_INCREMENT(exp, destroy);
720
721         rc = OBP(exp->exp_obd, destroy)(env, exp, obdo);
722         RETURN(rc);
723 }
724
725 static inline int obd_getattr(const struct lu_env *env, struct obd_export *exp,
726                               struct obdo *oa)
727 {
728         int rc;
729
730         ENTRY;
731         EXP_CHECK_DT_OP(exp, getattr);
732         EXP_COUNTER_INCREMENT(exp, getattr);
733         rc = OBP(exp->exp_obd, getattr)(env, exp, oa);
734
735         RETURN(rc);
736 }
737
738 static inline int obd_setattr(const struct lu_env *env, struct obd_export *exp,
739                               struct obdo *oa)
740 {
741         int rc;
742
743         ENTRY;
744         EXP_CHECK_DT_OP(exp, setattr);
745         EXP_COUNTER_INCREMENT(exp, setattr);
746         rc = OBP(exp->exp_obd, setattr)(env, exp, oa);
747
748         RETURN(rc);
749 }
750
751 static inline int obd_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
752                                int priority)
753 {
754         struct obd_device *obd = imp->imp_obd;
755         int rc;
756         ENTRY;
757
758         OBD_CHECK_DEV_ACTIVE(obd);
759         OBD_CHECK_DT_OP(obd, add_conn, -EOPNOTSUPP);
760         OBD_COUNTER_INCREMENT(obd, add_conn);
761
762         rc = OBP(obd, add_conn)(imp, uuid, priority);
763         RETURN(rc);
764 }
765
766 static inline int obd_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
767 {
768         struct obd_device *obd = imp->imp_obd;
769         int rc;
770         ENTRY;
771
772         OBD_CHECK_DEV_ACTIVE(obd);
773         OBD_CHECK_DT_OP(obd, del_conn, -EOPNOTSUPP);
774         OBD_COUNTER_INCREMENT(obd, del_conn);
775
776         rc = OBP(obd, del_conn)(imp, uuid);
777         RETURN(rc);
778 }
779
780 static inline struct obd_uuid *obd_get_uuid(struct obd_export *exp)
781 {
782         struct obd_uuid *uuid;
783         ENTRY;
784
785         OBD_CHECK_DT_OP(exp->exp_obd, get_uuid, NULL);
786         EXP_COUNTER_INCREMENT(exp, get_uuid);
787
788         uuid = OBP(exp->exp_obd, get_uuid)(exp);
789         RETURN(uuid);
790 }
791
792 /** Create a new /a exp on device /a obd for the uuid /a cluuid
793  * @param exp New export handle
794  * @param d Connect data, supported flags are set, flags also understood
795  *    by obd are returned.
796  */
797 static inline int obd_connect(const struct lu_env *env,
798                               struct obd_export **exp,struct obd_device *obd,
799                               struct obd_uuid *cluuid,
800                               struct obd_connect_data *data,
801                               void *localdata)
802 {
803         int rc;
804         __u64 ocf = data ? data->ocd_connect_flags : 0; /* for post-condition
805                                                    * check */
806         ENTRY;
807
808         OBD_CHECK_DEV_ACTIVE(obd);
809         OBD_CHECK_DT_OP(obd, connect, -EOPNOTSUPP);
810         OBD_COUNTER_INCREMENT(obd, connect);
811
812         rc = OBP(obd, connect)(env, exp, obd, cluuid, data, localdata);
813         /* check that only subset is granted */
814         LASSERT(ergo(data != NULL, (data->ocd_connect_flags & ocf) ==
815                                     data->ocd_connect_flags));
816         RETURN(rc);
817 }
818
819 static inline int obd_reconnect(const struct lu_env *env,
820                                 struct obd_export *exp,
821                                 struct obd_device *obd,
822                                 struct obd_uuid *cluuid,
823                                 struct obd_connect_data *d,
824                                 void *localdata)
825 {
826         int rc;
827         __u64 ocf = d ? d->ocd_connect_flags : 0; /* for post-condition
828                                                    * check */
829
830         ENTRY;
831
832         OBD_CHECK_DEV_ACTIVE(obd);
833         OBD_CHECK_DT_OP(obd, reconnect, 0);
834         OBD_COUNTER_INCREMENT(obd, reconnect);
835
836         rc = OBP(obd, reconnect)(env, exp, obd, cluuid, d, localdata);
837         /* check that only subset is granted */
838         LASSERT(ergo(d != NULL,
839                      (d->ocd_connect_flags & ocf) == d->ocd_connect_flags));
840         RETURN(rc);
841 }
842
843 static inline int obd_disconnect(struct obd_export *exp)
844 {
845         int rc;
846         ENTRY;
847
848         EXP_CHECK_DT_OP(exp, disconnect);
849         EXP_COUNTER_INCREMENT(exp, disconnect);
850
851         rc = OBP(exp->exp_obd, disconnect)(exp);
852         RETURN(rc);
853 }
854
855 static inline int obd_fid_init(struct obd_device *obd, struct obd_export *exp,
856                                enum lu_cli_type type)
857 {
858         int rc;
859         ENTRY;
860
861         OBD_CHECK_DT_OP(obd, fid_init, 0);
862         OBD_COUNTER_INCREMENT(obd, fid_init);
863
864         rc = OBP(obd, fid_init)(obd, exp, type);
865         RETURN(rc);
866 }
867
868 static inline int obd_fid_fini(struct obd_device *obd)
869 {
870         int rc;
871         ENTRY;
872
873         OBD_CHECK_DT_OP(obd, fid_fini, 0);
874         OBD_COUNTER_INCREMENT(obd, fid_fini);
875
876         rc = OBP(obd, fid_fini)(obd);
877         RETURN(rc);
878 }
879
880 static inline int obd_fid_alloc(const struct lu_env *env,
881                                 struct obd_export *exp,
882                                 struct lu_fid *fid,
883                                 struct md_op_data *op_data)
884 {
885         int rc;
886         ENTRY;
887
888         EXP_CHECK_DT_OP(exp, fid_alloc);
889         EXP_COUNTER_INCREMENT(exp, fid_alloc);
890
891         rc = OBP(exp->exp_obd, fid_alloc)(env, exp, fid, op_data);
892         RETURN(rc);
893 }
894
895 static inline int obd_ping(const struct lu_env *env, struct obd_export *exp)
896 {
897         int rc;
898         ENTRY;
899
900         OBD_CHECK_DT_OP(exp->exp_obd, ping, 0);
901         EXP_COUNTER_INCREMENT(exp, ping);
902
903         rc = OBP(exp->exp_obd, ping)(env, exp);
904         RETURN(rc);
905 }
906
907 static inline int obd_pool_new(struct obd_device *obd, char *poolname)
908 {
909         int rc;
910         ENTRY;
911
912         OBD_CHECK_DT_OP(obd, pool_new, -EOPNOTSUPP);
913         OBD_COUNTER_INCREMENT(obd, pool_new);
914
915         rc = OBP(obd, pool_new)(obd, poolname);
916         RETURN(rc);
917 }
918
919 static inline int obd_pool_del(struct obd_device *obd, char *poolname)
920 {
921         int rc;
922         ENTRY;
923
924         OBD_CHECK_DT_OP(obd, pool_del, -EOPNOTSUPP);
925         OBD_COUNTER_INCREMENT(obd, pool_del);
926
927         rc = OBP(obd, pool_del)(obd, poolname);
928         RETURN(rc);
929 }
930
931 static inline int obd_pool_add(struct obd_device *obd, char *poolname, char *ostname)
932 {
933         int rc;
934         ENTRY;
935
936         OBD_CHECK_DT_OP(obd, pool_add, -EOPNOTSUPP);
937         OBD_COUNTER_INCREMENT(obd, pool_add);
938
939         rc = OBP(obd, pool_add)(obd, poolname, ostname);
940         RETURN(rc);
941 }
942
943 static inline int obd_pool_rem(struct obd_device *obd, char *poolname, char *ostname)
944 {
945         int rc;
946         ENTRY;
947
948         OBD_CHECK_DT_OP(obd, pool_rem, -EOPNOTSUPP);
949         OBD_COUNTER_INCREMENT(obd, pool_rem);
950
951         rc = OBP(obd, pool_rem)(obd, poolname, ostname);
952         RETURN(rc);
953 }
954
955 static inline void obd_getref(struct obd_device *obd)
956 {
957         ENTRY;
958         if (OBT(obd) && OBP(obd, getref)) {
959                 OBD_COUNTER_INCREMENT(obd, getref);
960                 OBP(obd, getref)(obd);
961         }
962         EXIT;
963 }
964
965 static inline void obd_putref(struct obd_device *obd)
966 {
967         ENTRY;
968         if (OBT(obd) && OBP(obd, putref)) {
969                 OBD_COUNTER_INCREMENT(obd, putref);
970                 OBP(obd, putref)(obd);
971         }
972         EXIT;
973 }
974
975 static inline int obd_init_export(struct obd_export *exp)
976 {
977         int rc = 0;
978
979         ENTRY;
980         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
981             OBP((exp)->exp_obd, init_export))
982                 rc = OBP(exp->exp_obd, init_export)(exp);
983         RETURN(rc);
984 }
985
986 static inline int obd_destroy_export(struct obd_export *exp)
987 {
988         ENTRY;
989         if ((exp)->exp_obd != NULL && OBT((exp)->exp_obd) &&
990             OBP((exp)->exp_obd, destroy_export))
991                 OBP(exp->exp_obd, destroy_export)(exp);
992         RETURN(0);
993 }
994
995 /* @max_age is the oldest time in jiffies that we accept using a cached data.
996  * If the cache is older than @max_age we will get a new value from the
997  * target.  Use a value of "cfs_time_current() + HZ" to guarantee freshness. */
998 static inline int obd_statfs_async(struct obd_export *exp,
999                                    struct obd_info *oinfo,
1000                                    __u64 max_age,
1001                                    struct ptlrpc_request_set *rqset)
1002 {
1003         int rc = 0;
1004         struct obd_device *obd;
1005         ENTRY;
1006
1007         if (exp == NULL || exp->exp_obd == NULL)
1008                 RETURN(-EINVAL);
1009
1010         obd = exp->exp_obd;
1011         OBD_CHECK_DT_OP(obd, statfs, -EOPNOTSUPP);
1012         OBD_COUNTER_INCREMENT(obd, statfs);
1013
1014         CDEBUG(D_SUPER, "%s: osfs %p age %llu, max_age %llu\n",
1015                obd->obd_name, &obd->obd_osfs, obd->obd_osfs_age, max_age);
1016         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
1017                 rc = OBP(obd, statfs_async)(exp, oinfo, max_age, rqset);
1018         } else {
1019                 CDEBUG(D_SUPER, "%s: use %p cache blocks %llu/%llu objects %llu/%llu\n",
1020                        obd->obd_name, &obd->obd_osfs,
1021                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
1022                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
1023                 spin_lock(&obd->obd_osfs_lock);
1024                 memcpy(oinfo->oi_osfs, &obd->obd_osfs, sizeof(*oinfo->oi_osfs));
1025                 spin_unlock(&obd->obd_osfs_lock);
1026                 oinfo->oi_flags |= OBD_STATFS_FROM_CACHE;
1027                 if (oinfo->oi_cb_up)
1028                         oinfo->oi_cb_up(oinfo, 0);
1029         }
1030         RETURN(rc);
1031 }
1032
1033 static inline int obd_statfs_rqset(struct obd_export *exp,
1034                                    struct obd_statfs *osfs, __u64 max_age,
1035                                    __u32 flags)
1036 {
1037         struct ptlrpc_request_set *set = NULL;
1038         struct obd_info oinfo = {
1039                 .oi_osfs = osfs,
1040                 .oi_flags = flags,
1041         };
1042         int rc = 0;
1043
1044         ENTRY;
1045
1046         set = ptlrpc_prep_set();
1047         if (set == NULL)
1048                 RETURN(-ENOMEM);
1049
1050         rc = obd_statfs_async(exp, &oinfo, max_age, set);
1051         if (rc == 0)
1052                 rc = ptlrpc_set_wait(set);
1053
1054         ptlrpc_set_destroy(set);
1055
1056         RETURN(rc);
1057 }
1058
1059 /* @max_age is the oldest time in jiffies that we accept using a cached data.
1060  * If the cache is older than @max_age we will get a new value from the
1061  * target.  Use a value of "cfs_time_current() + HZ" to guarantee freshness. */
1062 static inline int obd_statfs(const struct lu_env *env, struct obd_export *exp,
1063                              struct obd_statfs *osfs, __u64 max_age,
1064                              __u32 flags)
1065 {
1066         int rc = 0;
1067         struct obd_device *obd = exp->exp_obd;
1068         ENTRY;
1069
1070         if (obd == NULL)
1071                 RETURN(-EINVAL);
1072
1073         OBD_CHECK_DT_OP(obd, statfs, -EOPNOTSUPP);
1074         OBD_COUNTER_INCREMENT(obd, statfs);
1075
1076         CDEBUG(D_SUPER, "osfs %llu, max_age %llu\n",
1077                obd->obd_osfs_age, max_age);
1078         if (cfs_time_before_64(obd->obd_osfs_age, max_age)) {
1079                 rc = OBP(obd, statfs)(env, exp, osfs, max_age, flags);
1080                 if (rc == 0) {
1081                         spin_lock(&obd->obd_osfs_lock);
1082                         memcpy(&obd->obd_osfs, osfs, sizeof(obd->obd_osfs));
1083                         obd->obd_osfs_age = cfs_time_current_64();
1084                         spin_unlock(&obd->obd_osfs_lock);
1085                 }
1086         } else {
1087                 CDEBUG(D_SUPER, "%s: use %p cache blocks %llu/%llu"
1088                        " objects %llu/%llu\n",
1089                        obd->obd_name, &obd->obd_osfs,
1090                        obd->obd_osfs.os_bavail, obd->obd_osfs.os_blocks,
1091                        obd->obd_osfs.os_ffree, obd->obd_osfs.os_files);
1092                 spin_lock(&obd->obd_osfs_lock);
1093                 memcpy(osfs, &obd->obd_osfs, sizeof(*osfs));
1094                 spin_unlock(&obd->obd_osfs_lock);
1095         }
1096         RETURN(rc);
1097 }
1098
1099 static inline int obd_preprw(const struct lu_env *env, int cmd,
1100                              struct obd_export *exp, struct obdo *oa,
1101                              int objcount, struct obd_ioobj *obj,
1102                              struct niobuf_remote *remote, int *pages,
1103                              struct niobuf_local *local)
1104 {
1105         int rc;
1106
1107         ENTRY;
1108         EXP_CHECK_DT_OP(exp, preprw);
1109         EXP_COUNTER_INCREMENT(exp, preprw);
1110         rc = OBP(exp->exp_obd, preprw)(env, cmd, exp, oa, objcount, obj, remote,
1111                                        pages, local);
1112
1113         RETURN(rc);
1114 }
1115
1116 static inline int obd_commitrw(const struct lu_env *env, int cmd,
1117                                struct obd_export *exp, struct obdo *oa,
1118                                int objcount, struct obd_ioobj *obj,
1119                                struct niobuf_remote *rnb, int pages,
1120                                struct niobuf_local *local, int rc)
1121 {
1122         ENTRY;
1123
1124         EXP_CHECK_DT_OP(exp, commitrw);
1125         EXP_COUNTER_INCREMENT(exp, commitrw);
1126         rc = OBP(exp->exp_obd, commitrw)(env, cmd, exp, oa, objcount, obj,
1127                                          rnb, pages, local, rc);
1128
1129         RETURN(rc);
1130 }
1131
1132 static inline int obd_iocontrol(unsigned int cmd, struct obd_export *exp,
1133                                 int len, void *karg, void __user *uarg)
1134 {
1135         int rc;
1136         ENTRY;
1137
1138         EXP_CHECK_DT_OP(exp, iocontrol);
1139         EXP_COUNTER_INCREMENT(exp, iocontrol);
1140
1141         rc = OBP(exp->exp_obd, iocontrol)(cmd, exp, len, karg, uarg);
1142         RETURN(rc);
1143 }
1144
1145 static inline void obd_import_event(struct obd_device *obd,
1146                                     struct obd_import *imp,
1147                                     enum obd_import_event event)
1148 {
1149         ENTRY;
1150         if (!obd) {
1151                 CERROR("NULL device\n");
1152                 EXIT;
1153                 return;
1154         }
1155         if (obd->obd_set_up && OBP(obd, import_event)) {
1156                 OBD_COUNTER_INCREMENT(obd, import_event);
1157                 OBP(obd, import_event)(obd, imp, event);
1158         }
1159         EXIT;
1160 }
1161
1162 static inline int obd_notify(struct obd_device *obd,
1163                              struct obd_device *watched,
1164                              enum obd_notify_event ev)
1165 {
1166         int rc;
1167         ENTRY;
1168         OBD_CHECK_DEV(obd);
1169
1170         if (!obd->obd_set_up) {
1171                 CDEBUG(D_HA, "obd %s not set up\n", obd->obd_name);
1172                 RETURN(-EINVAL);
1173         }
1174
1175         if (!OBP(obd, notify)) {
1176                 CDEBUG(D_HA, "obd %s has no notify handler\n", obd->obd_name);
1177                 RETURN(-ENOSYS);
1178         }
1179
1180         OBD_COUNTER_INCREMENT(obd, notify);
1181         rc = OBP(obd, notify)(obd, watched, ev);
1182
1183         RETURN(rc);
1184 }
1185
1186 static inline int obd_notify_observer(struct obd_device *observer,
1187                                       struct obd_device *observed,
1188                                       enum obd_notify_event ev)
1189 {
1190         int rc = 0;
1191         int rc2 = 0;
1192         struct obd_notify_upcall *onu;
1193
1194         if (observer->obd_observer)
1195                 rc = obd_notify(observer->obd_observer, observed, ev);
1196
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, onu->onu_owner);
1203
1204         return rc ? rc : rc2;
1205 }
1206
1207 static inline int obd_quotactl(struct obd_export *exp,
1208                                struct obd_quotactl *oqctl)
1209 {
1210         int rc;
1211         ENTRY;
1212
1213         EXP_CHECK_DT_OP(exp, quotactl);
1214         EXP_COUNTER_INCREMENT(exp, quotactl);
1215
1216         rc = OBP(exp->exp_obd, quotactl)(exp->exp_obd, exp, oqctl);
1217         RETURN(rc);
1218 }
1219
1220 static inline int obd_health_check(const struct lu_env *env,
1221                                    struct obd_device *obd)
1222 {
1223         /* returns: 0 on healthy
1224          *         >0 on unhealthy + reason code/flag
1225          *            however the only suppored reason == 1 right now
1226          *            We'll need to define some better reasons
1227          *            or flags in the future.
1228          *         <0 on error
1229          */
1230         int rc;
1231         ENTRY;
1232
1233         /* don't use EXP_CHECK_DT_OP, because NULL method is normal here */
1234         if (obd == NULL || !OBT(obd)) {
1235                 CERROR("cleaned up obd\n");
1236                 RETURN(-EOPNOTSUPP);
1237         }
1238         if (!obd->obd_set_up || obd->obd_stopping)
1239                 RETURN(0);
1240         if (!OBP(obd, health_check))
1241                 RETURN(0);
1242
1243         rc = OBP(obd, health_check)(env, obd);
1244         RETURN(rc);
1245 }
1246
1247 static inline int obd_register_observer(struct obd_device *obd,
1248                                         struct obd_device *observer)
1249 {
1250         ENTRY;
1251         OBD_CHECK_DEV(obd);
1252         down_write(&obd->obd_observer_link_sem);
1253         if (obd->obd_observer && observer) {
1254                 up_write(&obd->obd_observer_link_sem);
1255                 RETURN(-EALREADY);
1256         }
1257         obd->obd_observer = observer;
1258         up_write(&obd->obd_observer_link_sem);
1259         RETURN(0);
1260 }
1261
1262 /* metadata helpers */
1263 static inline int md_get_root(struct obd_export *exp, const char *fileset,
1264                               struct lu_fid *fid)
1265 {
1266         int rc;
1267
1268         ENTRY;
1269         EXP_CHECK_MD_OP(exp, get_root);
1270         EXP_MD_COUNTER_INCREMENT(exp, get_root);
1271         rc = MDP(exp->exp_obd, get_root)(exp, fileset, fid);
1272
1273         RETURN(rc);
1274 }
1275
1276 static inline int md_getattr(struct obd_export *exp, struct md_op_data *op_data,
1277                              struct ptlrpc_request **request)
1278 {
1279         int rc;
1280         ENTRY;
1281         EXP_CHECK_MD_OP(exp, getattr);
1282         EXP_MD_COUNTER_INCREMENT(exp, getattr);
1283         rc = MDP(exp->exp_obd, getattr)(exp, op_data, request);
1284         RETURN(rc);
1285 }
1286
1287 static inline int md_null_inode(struct obd_export *exp,
1288                                    const struct lu_fid *fid)
1289 {
1290         int rc;
1291         ENTRY;
1292         EXP_CHECK_MD_OP(exp, null_inode);
1293         EXP_MD_COUNTER_INCREMENT(exp, null_inode);
1294         rc = MDP(exp->exp_obd, null_inode)(exp, fid);
1295         RETURN(rc);
1296 }
1297
1298 static inline int md_close(struct obd_export *exp, struct md_op_data *op_data,
1299                            struct md_open_data *mod,
1300                            struct ptlrpc_request **request)
1301 {
1302         int rc;
1303         ENTRY;
1304         EXP_CHECK_MD_OP(exp, close);
1305         EXP_MD_COUNTER_INCREMENT(exp, close);
1306         rc = MDP(exp->exp_obd, close)(exp, op_data, mod, request);
1307         RETURN(rc);
1308 }
1309
1310 static inline int md_create(struct obd_export *exp, struct md_op_data *op_data,
1311                             const void *data, size_t datalen, umode_t mode,
1312                             uid_t uid, gid_t gid, cfs_cap_t cap_effective,
1313                             __u64 rdev, struct ptlrpc_request **request)
1314 {
1315         int rc;
1316         ENTRY;
1317         EXP_CHECK_MD_OP(exp, create);
1318         EXP_MD_COUNTER_INCREMENT(exp, create);
1319         rc = MDP(exp->exp_obd, create)(exp, op_data, data, datalen, mode,
1320                                        uid, gid, cap_effective, rdev, request);
1321         RETURN(rc);
1322 }
1323
1324 static inline int md_enqueue(struct obd_export *exp,
1325                              struct ldlm_enqueue_info *einfo,
1326                              const union ldlm_policy_data *policy,
1327                              struct md_op_data *op_data,
1328                              struct lustre_handle *lockh,
1329                              __u64 extra_lock_flags)
1330 {
1331         int rc;
1332         ENTRY;
1333         EXP_CHECK_MD_OP(exp, enqueue);
1334         EXP_MD_COUNTER_INCREMENT(exp, enqueue);
1335         rc = MDP(exp->exp_obd, enqueue)(exp, einfo, policy, op_data, lockh,
1336                                         extra_lock_flags);
1337         RETURN(rc);
1338 }
1339
1340 static inline int md_getattr_name(struct obd_export *exp,
1341                                   struct md_op_data *op_data,
1342                                   struct ptlrpc_request **request)
1343 {
1344         int rc;
1345         ENTRY;
1346         EXP_CHECK_MD_OP(exp, getattr_name);
1347         EXP_MD_COUNTER_INCREMENT(exp, getattr_name);
1348         rc = MDP(exp->exp_obd, getattr_name)(exp, op_data, request);
1349         RETURN(rc);
1350 }
1351
1352 static inline int md_intent_lock(struct obd_export *exp,
1353                                  struct md_op_data *op_data,
1354                                  struct lookup_intent *it,
1355                                  struct ptlrpc_request **reqp,
1356                                  ldlm_blocking_callback cb_blocking,
1357                                  __u64 extra_lock_flags)
1358 {
1359         int rc;
1360         ENTRY;
1361         EXP_CHECK_MD_OP(exp, intent_lock);
1362         EXP_MD_COUNTER_INCREMENT(exp, intent_lock);
1363         rc = MDP(exp->exp_obd, intent_lock)(exp, op_data, it, reqp, cb_blocking,
1364                                             extra_lock_flags);
1365         RETURN(rc);
1366 }
1367
1368 static inline int md_link(struct obd_export *exp, struct md_op_data *op_data,
1369                           struct ptlrpc_request **request)
1370 {
1371         int rc;
1372         ENTRY;
1373         EXP_CHECK_MD_OP(exp, link);
1374         EXP_MD_COUNTER_INCREMENT(exp, link);
1375         rc = MDP(exp->exp_obd, link)(exp, op_data, request);
1376         RETURN(rc);
1377 }
1378
1379 static inline int md_rename(struct obd_export *exp, struct md_op_data *op_data,
1380                             const char *old, size_t oldlen, const char *new,
1381                             size_t newlen, struct ptlrpc_request **request)
1382 {
1383         int rc;
1384         ENTRY;
1385         EXP_CHECK_MD_OP(exp, rename);
1386         EXP_MD_COUNTER_INCREMENT(exp, rename);
1387         rc = MDP(exp->exp_obd, rename)(exp, op_data, old, oldlen, new,
1388                                        newlen, request);
1389         RETURN(rc);
1390 }
1391
1392 static inline int md_setattr(struct obd_export *exp, struct md_op_data *op_data,
1393                              void *ea, size_t ealen,
1394                              struct ptlrpc_request **request)
1395 {
1396         int rc;
1397         ENTRY;
1398         EXP_CHECK_MD_OP(exp, setattr);
1399         EXP_MD_COUNTER_INCREMENT(exp, setattr);
1400         rc = MDP(exp->exp_obd, setattr)(exp, op_data, ea, ealen, request);
1401         RETURN(rc);
1402 }
1403
1404 static inline int md_fsync(struct obd_export *exp, const struct lu_fid *fid,
1405                            struct ptlrpc_request **request)
1406 {
1407         int rc;
1408
1409         ENTRY;
1410         EXP_CHECK_MD_OP(exp, fsync);
1411         EXP_MD_COUNTER_INCREMENT(exp, fsync);
1412         rc = MDP(exp->exp_obd, fsync)(exp, fid, request);
1413
1414         RETURN(rc);
1415 }
1416
1417 static inline int md_read_page(struct obd_export *exp,
1418                                struct md_op_data *op_data,
1419                                struct md_callback *cb_op,
1420                                __u64  hash_offset,
1421                                struct page **ppage)
1422 {
1423         int rc;
1424         ENTRY;
1425         EXP_CHECK_MD_OP(exp, read_page);
1426         EXP_MD_COUNTER_INCREMENT(exp, read_page);
1427         rc = MDP(exp->exp_obd, read_page)(exp, op_data, cb_op, hash_offset,
1428                                           ppage);
1429         RETURN(rc);
1430 }
1431
1432 static inline int md_unlink(struct obd_export *exp, struct md_op_data *op_data,
1433                             struct ptlrpc_request **request)
1434 {
1435         int rc;
1436         ENTRY;
1437         EXP_CHECK_MD_OP(exp, unlink);
1438         EXP_MD_COUNTER_INCREMENT(exp, unlink);
1439         rc = MDP(exp->exp_obd, unlink)(exp, op_data, request);
1440         RETURN(rc);
1441 }
1442
1443 static inline int md_get_lustre_md(struct obd_export *exp,
1444                                    struct ptlrpc_request *req,
1445                                    struct obd_export *dt_exp,
1446                                    struct obd_export *md_exp,
1447                                    struct lustre_md *md)
1448 {
1449         ENTRY;
1450         EXP_CHECK_MD_OP(exp, get_lustre_md);
1451         EXP_MD_COUNTER_INCREMENT(exp, get_lustre_md);
1452         RETURN(MDP(exp->exp_obd, get_lustre_md)(exp, req, dt_exp, md_exp, md));
1453 }
1454
1455 static inline int md_free_lustre_md(struct obd_export *exp,
1456                                     struct lustre_md *md)
1457 {
1458         ENTRY;
1459         EXP_CHECK_MD_OP(exp, free_lustre_md);
1460         EXP_MD_COUNTER_INCREMENT(exp, free_lustre_md);
1461         RETURN(MDP(exp->exp_obd, free_lustre_md)(exp, md));
1462 }
1463
1464 static inline int md_merge_attr(struct obd_export *exp,
1465                                 const struct lmv_stripe_md *lsm,
1466                                 struct cl_attr *attr,
1467                                 ldlm_blocking_callback cb)
1468 {
1469         ENTRY;
1470         EXP_CHECK_MD_OP(exp, merge_attr);
1471         EXP_MD_COUNTER_INCREMENT(exp, merge_attr);
1472         RETURN(MDP(exp->exp_obd, merge_attr)(exp, lsm, attr, cb));
1473 }
1474
1475 static inline int md_setxattr(struct obd_export *exp, const struct lu_fid *fid,
1476                               u64 valid, const char *name,
1477                               const char *input, int input_size,
1478                               int output_size, int flags, __u32 suppgid,
1479                               struct ptlrpc_request **request)
1480 {
1481         ENTRY;
1482         EXP_CHECK_MD_OP(exp, setxattr);
1483         EXP_MD_COUNTER_INCREMENT(exp, setxattr);
1484         RETURN(MDP(exp->exp_obd, setxattr)(exp, fid, valid, name, input,
1485                                            input_size, output_size, flags,
1486                                            suppgid, request));
1487 }
1488
1489 static inline int md_getxattr(struct obd_export *exp, const struct lu_fid *fid,
1490                               u64 valid, const char *name,
1491                               const char *input, int input_size,
1492                               int output_size, int flags,
1493                               struct ptlrpc_request **request)
1494 {
1495         ENTRY;
1496         EXP_CHECK_MD_OP(exp, getxattr);
1497         EXP_MD_COUNTER_INCREMENT(exp, getxattr);
1498         RETURN(MDP(exp->exp_obd, getxattr)(exp, fid, valid, name, input,
1499                                            input_size, output_size, flags,
1500                                            request));
1501 }
1502
1503 static inline int md_set_open_replay_data(struct obd_export *exp,
1504                                           struct obd_client_handle *och,
1505                                           struct lookup_intent *it)
1506 {
1507         ENTRY;
1508         EXP_CHECK_MD_OP(exp, set_open_replay_data);
1509         EXP_MD_COUNTER_INCREMENT(exp, set_open_replay_data);
1510         RETURN(MDP(exp->exp_obd, set_open_replay_data)(exp, och, it));
1511 }
1512
1513 static inline int md_clear_open_replay_data(struct obd_export *exp,
1514                                             struct obd_client_handle *och)
1515 {
1516         ENTRY;
1517         EXP_CHECK_MD_OP(exp, clear_open_replay_data);
1518         EXP_MD_COUNTER_INCREMENT(exp, clear_open_replay_data);
1519         RETURN(MDP(exp->exp_obd, clear_open_replay_data)(exp, och));
1520 }
1521
1522 static inline int md_set_lock_data(struct obd_export *exp,
1523                                    const struct lustre_handle *lockh,
1524                                    void *data, __u64 *bits)
1525 {
1526         ENTRY;
1527         EXP_CHECK_MD_OP(exp, set_lock_data);
1528         EXP_MD_COUNTER_INCREMENT(exp, set_lock_data);
1529         RETURN(MDP(exp->exp_obd, set_lock_data)(exp, lockh, data, bits));
1530 }
1531
1532 static inline
1533 int md_cancel_unused(struct obd_export *exp, const struct lu_fid *fid,
1534                      union ldlm_policy_data *policy, enum ldlm_mode mode,
1535                      enum ldlm_cancel_flags cancel_flags, void *opaque)
1536 {
1537         int rc;
1538         ENTRY;
1539
1540         EXP_CHECK_MD_OP(exp, cancel_unused);
1541         EXP_MD_COUNTER_INCREMENT(exp, cancel_unused);
1542
1543         rc = MDP(exp->exp_obd, cancel_unused)(exp, fid, policy, mode,
1544                                               cancel_flags, opaque);
1545         RETURN(rc);
1546 }
1547
1548 static inline enum ldlm_mode md_lock_match(struct obd_export *exp, __u64 flags,
1549                                            const struct lu_fid *fid,
1550                                            enum ldlm_type type,
1551                                            union ldlm_policy_data *policy,
1552                                            enum ldlm_mode mode,
1553                                            struct lustre_handle *lockh)
1554 {
1555         ENTRY;
1556         EXP_CHECK_MD_OP(exp, lock_match);
1557         EXP_MD_COUNTER_INCREMENT(exp, lock_match);
1558         RETURN(MDP(exp->exp_obd, lock_match)(exp, flags, fid, type,
1559                                              policy, mode, lockh));
1560 }
1561
1562 static inline int md_init_ea_size(struct obd_export *exp, __u32 ea_size,
1563                                   __u32 def_ea_size)
1564 {
1565         ENTRY;
1566         EXP_CHECK_MD_OP(exp, init_ea_size);
1567         EXP_MD_COUNTER_INCREMENT(exp, init_ea_size);
1568         RETURN(MDP(exp->exp_obd, init_ea_size)(exp, ea_size, def_ea_size));
1569 }
1570
1571 static inline int md_intent_getattr_async(struct obd_export *exp,
1572                                           struct md_enqueue_info *minfo)
1573 {
1574         int rc;
1575         ENTRY;
1576         EXP_CHECK_MD_OP(exp, intent_getattr_async);
1577         EXP_MD_COUNTER_INCREMENT(exp, intent_getattr_async);
1578         rc = MDP(exp->exp_obd, intent_getattr_async)(exp, minfo);
1579         RETURN(rc);
1580 }
1581
1582 static inline int md_revalidate_lock(struct obd_export *exp,
1583                                      struct lookup_intent *it,
1584                                      struct lu_fid *fid, __u64 *bits)
1585 {
1586         int rc;
1587         ENTRY;
1588         EXP_CHECK_MD_OP(exp, revalidate_lock);
1589         EXP_MD_COUNTER_INCREMENT(exp, revalidate_lock);
1590         rc = MDP(exp->exp_obd, revalidate_lock)(exp, it, fid, bits);
1591         RETURN(rc);
1592 }
1593
1594 static inline int md_get_fid_from_lsm(struct obd_export *exp,
1595                                       const struct lmv_stripe_md *lsm,
1596                                       const char *name, int namelen,
1597                                       struct lu_fid *fid)
1598 {
1599         int rc;
1600         ENTRY;
1601         EXP_CHECK_MD_OP(exp, get_fid_from_lsm);
1602         EXP_MD_COUNTER_INCREMENT(exp, get_fid_from_lsm);
1603         rc = MDP(exp->exp_obd, get_fid_from_lsm)(exp, lsm, name, namelen, fid);
1604         RETURN(rc);
1605 }
1606
1607
1608 /* Unpack an MD struct from disk to in-memory format.
1609  * Returns +ve size of unpacked MD (0 for free), or -ve error.
1610  *
1611  * If *plsm != NULL and lmm == NULL then *lsm will be freed.
1612  * If *plsm == NULL then it will be allocated.
1613  */
1614 static inline int md_unpackmd(struct obd_export *exp,
1615                               struct lmv_stripe_md **plsm,
1616                               const union lmv_mds_md *lmm, size_t lmm_size)
1617 {
1618         int rc;
1619         ENTRY;
1620         EXP_CHECK_MD_OP(exp, unpackmd);
1621         EXP_MD_COUNTER_INCREMENT(exp, unpackmd);
1622         rc = MDP(exp->exp_obd, unpackmd)(exp, plsm, lmm, lmm_size);
1623         RETURN(rc);
1624 }
1625
1626 /* OBD Metadata Support */
1627
1628 extern int obd_init_caches(void);
1629 extern void obd_cleanup_caches(void);
1630
1631 /* support routines */
1632 extern struct kmem_cache *obdo_cachep;
1633
1634 #define OBDO_ALLOC(ptr)                                                       \
1635 do {                                                                          \
1636         OBD_SLAB_ALLOC_PTR_GFP((ptr), obdo_cachep, GFP_NOFS);             \
1637 } while(0)
1638
1639 #define OBDO_FREE(ptr)                                                        \
1640 do {                                                                          \
1641         OBD_SLAB_FREE_PTR((ptr), obdo_cachep);                                \
1642 } while(0)
1643
1644
1645 typedef int (*register_lwp_cb)(void *data);
1646
1647 struct lwp_register_item {
1648         struct obd_export **lri_exp;
1649         register_lwp_cb     lri_cb_func;
1650         void               *lri_cb_data;
1651         struct list_head    lri_list;
1652         atomic_t            lri_ref;
1653         char                lri_name[MTI_NAME_MAXLEN];
1654 };
1655
1656 /* I'm as embarrassed about this as you are.
1657  *
1658  * <shaver> // XXX do not look into _superhack with remaining eye
1659  * <shaver> // XXX if this were any uglier, I'd get my own show on MTV */
1660 extern int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
1661
1662 /* obd_mount.c */
1663 #ifdef HAVE_SERVER_SUPPORT
1664 int lustre_register_lwp_item(const char *lwpname, struct obd_export **exp,
1665                              register_lwp_cb cb_func, void *cb_data);
1666 void lustre_deregister_lwp_item(struct obd_export **exp);
1667 struct obd_export *lustre_find_lwp_by_index(const char *dev, __u32 idx);
1668 void lustre_notify_lwp_list(struct obd_export *exp);
1669 int tgt_name2lwp_name(const char *tgt_name, char *lwp_name, int len, __u32 idx);
1670 #endif /* HAVE_SERVER_SUPPORT */
1671 int lustre_register_fs(void);
1672 int lustre_unregister_fs(void);
1673 int lustre_check_exclusion(struct super_block *sb, char *svname);
1674
1675 /* sysctl.c */
1676 extern int obd_sysctl_init(void);
1677 extern void obd_sysctl_clean(void);
1678
1679 /* uuid.c  */
1680 typedef __u8 class_uuid_t[16];
1681 void class_uuid_unparse(class_uuid_t in, struct obd_uuid *out);
1682
1683 /* lustre_peer.c    */
1684 int lustre_uuid_to_peer(const char *uuid, lnet_nid_t *peer_nid, int index);
1685 int class_add_uuid(const char *uuid, __u64 nid);
1686 int class_del_uuid (const char *uuid);
1687 int class_check_uuid(struct obd_uuid *uuid, __u64 nid);
1688
1689 /* class_obd.c */
1690 extern char obd_jobid_node[];
1691
1692 /* prng.c */
1693 #define ll_generate_random_uuid(uuid_out) cfs_get_random_bytes(uuid_out, sizeof(class_uuid_t))
1694
1695 /* statfs_pack.c */
1696 struct kstatfs;
1697 void statfs_pack(struct obd_statfs *osfs, struct kstatfs *sfs);
1698 void statfs_unpack(struct kstatfs *sfs, struct obd_statfs *osfs);
1699
1700 /* root squash info */
1701 struct rw_semaphore;
1702 struct root_squash_info {
1703         uid_t                   rsi_uid;
1704         gid_t                   rsi_gid;
1705         struct list_head        rsi_nosquash_nids;
1706         struct rw_semaphore     rsi_sem;
1707 };
1708
1709 int server_name2index(const char *svname, __u32 *idx, const char **endptr);
1710
1711 /* linux-module.c */
1712 extern struct miscdevice obd_psdev;
1713 int class_procfs_init(void);
1714 int class_procfs_clean(void);
1715
1716 #endif /* __LINUX_OBD_CLASS_H */