Whamcloud - gitweb
merge b_devel into HEAD (20030703)
[fs/lustre-release.git] / lustre / include / linux / obd_class.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  */
22
23 #ifndef __LINUX_CLASS_OBD_H
24 #define __LINUX_CLASS_OBD_H
25
26 #ifndef __KERNEL__
27 #include <sys/types.h>
28 #include <portals/list.h>
29 #else
30 #include <asm/segment.h>
31 #include <asm/uaccess.h>
32 #include <linux/types.h>
33 #include <linux/fs.h>
34 #include <linux/time.h>
35 #endif
36
37 #include <linux/obd_support.h>
38 #include <linux/lustre_import.h>
39 #include <linux/lustre_net.h>
40 #include <linux/obd.h>
41 #include <linux/lustre_lib.h>
42 #include <linux/lustre_idl.h>
43 #include <linux/lustre_mds.h>
44 #include <linux/lustre_dlm.h>
45 #include <linux/lprocfs_status.h>
46
47
48 /* OBD Device Declarations */
49 #define MAX_OBD_DEVICES 128
50 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
51
52 /* OBD Operations Declarations */
53 extern struct obd_device *class_conn2obd(struct lustre_handle *);
54
55 /* genops.c */
56 struct obd_export *class_conn2export(struct lustre_handle *);
57 int class_register_type(struct obd_ops *ops, struct lprocfs_vars *, char *nm);
58 int class_unregister_type(char *nm);
59 int class_name2dev(char *name);
60 struct obd_device *class_name2obd(char *name);
61 int class_uuid2dev(struct obd_uuid *uuid);
62 struct obd_device *class_uuid2obd(struct obd_uuid *uuid);
63
64 struct obd_export *class_export_get(struct obd_export *);
65 void class_export_put(struct obd_export *);
66 struct obd_export *class_new_export(struct obd_device *obddev);
67 void class_unlink_export(struct obd_export *exp);
68
69 struct obd_import *class_import_get(struct obd_import *);
70 void class_import_put(struct obd_import *);
71 struct obd_import *class_new_import(void);
72 void class_destroy_import(struct obd_import *exp);
73
74 struct obd_type *class_get_type(char *name);
75 void class_put_type(struct obd_type *type);
76 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
77                   struct obd_uuid *cluuid);
78 int class_disconnect(struct lustre_handle *conn, int failover);
79 void class_disconnect_exports(struct obd_device *obddev, int failover);
80 /* generic operations shared by various OBD types */
81 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data);
82 int class_multi_cleanup(struct obd_device *obddev);
83
84 static inline int obd_check_conn(struct lustre_handle *conn)
85 {
86         struct obd_device *obd;
87         if (!conn) {
88                 CERROR("NULL conn\n");
89                 RETURN(-ENOTCONN);
90         }
91
92         obd = class_conn2obd(conn);
93         if (!obd) {
94                 CERROR("NULL obd\n");
95                 RETURN(-ENODEV);
96         }
97
98         if (!obd->obd_attached) {
99                 CERROR("obd %d not attached\n", obd->obd_minor);
100                 RETURN(-ENODEV);
101         }
102
103         if (!obd->obd_set_up) {
104                 CERROR("obd %d not setup\n", obd->obd_minor);
105                 RETURN(-ENODEV);
106         }
107
108         if (!obd->obd_type) {
109                 CERROR("obd %d not typed\n", obd->obd_minor);
110                 RETURN(-ENODEV);
111         }
112
113         if (!obd->obd_type->typ_ops) {
114                 CERROR("obd_check_conn: obd %d no operations\n",
115                        obd->obd_minor);
116                 RETURN(-EOPNOTSUPP);
117         }
118         return 0;
119 }
120
121
122 #define OBT(dev)        (dev)->obd_type
123 #define OBP(dev, op)    (dev)->obd_type->typ_ops->o_ ## op
124
125 /* Ensure obd_setup: used for disconnect which might be called while
126    an obd is stopping. */
127 #define OBD_CHECK_SETUP(conn, exp)                                      \
128 do {                                                                    \
129         if (!(conn)) {                                                  \
130                 CERROR("NULL connection\n");                            \
131                 RETURN(-EINVAL);                                        \
132         }                                                               \
133                                                                         \
134         exp = class_conn2export(conn);                                  \
135         if (!(exp)) {                                                   \
136                 CERROR("No export for conn "LPX64"\n", (conn)->cookie); \
137                 RETURN(-EINVAL);                                        \
138         }                                                               \
139                                                                         \
140         if (!(exp)->exp_obd->obd_set_up) {                              \
141                 CERROR("Device %d not setup\n",                         \
142                        (exp)->exp_obd->obd_minor);                      \
143                 class_export_put(exp);                                  \
144                 RETURN(-EINVAL);                                        \
145         }                                                               \
146 } while (0)
147
148 /* Ensure obd_setup and !obd_stopping. */
149 #define OBD_CHECK_ACTIVE(conn, exp)                                     \
150 do {                                                                    \
151         if (!(conn)) {                                                  \
152                 CERROR("NULL connection\n");                            \
153                 RETURN(-EINVAL);                                        \
154         }                                                               \
155                                                                         \
156         exp = class_conn2export(conn);                                  \
157         if (!(exp)) {                                                   \
158                 CERROR("No export for conn "LPX64"\n", (conn)->cookie); \
159                 RETURN(-EINVAL);                                        \
160         }                                                               \
161                                                                         \
162         if (!(exp)->exp_obd->obd_set_up || (exp)->exp_obd->obd_stopping) { \
163                 CERROR("Device %d not setup\n",                         \
164                        (exp)->exp_obd->obd_minor);                      \
165                 class_export_put(exp);                                  \
166                 RETURN(-EINVAL);                                        \
167         }                                                               \
168 } while (0)
169
170 /* Ensure obd_setup: used for cleanup which must be called
171    while obd is stopping */
172 #define OBD_CHECK_DEV_STOPPING(obd)                             \
173 do {                                                            \
174         if (!(obd)) {                                           \
175                 CERROR("NULL device\n");                        \
176                 RETURN(-ENODEV);                                \
177         }                                                       \
178                                                                 \
179         if (!(obd)->obd_set_up) {                               \
180                 CERROR("Device %d not setup\n",                 \
181                        (obd)->obd_minor);                       \
182                 RETURN(-ENODEV);                                \
183         }                                                       \
184                                                                 \
185         if (!(obd)->obd_stopping) {                             \
186                 CERROR("Device %d not stopping\n",              \
187                        (obd)->obd_minor);                       \
188                 RETURN(-ENODEV);                                \
189         }                                                       \
190 } while (0)
191
192 /* ensure obd_setup and !obd_stopping */
193 #define OBD_CHECK_DEV_ACTIVE(obd)                               \
194 do {                                                            \
195         if (!(obd)) {                                           \
196                 CERROR("NULL device\n");                        \
197                 RETURN(-ENODEV);                                \
198         }                                                       \
199                                                                 \
200         if (!(obd)->obd_set_up || (obd)->obd_stopping) {        \
201                 CERROR("Device %d not setup\n",                 \
202                        (obd)->obd_minor);                       \
203                 RETURN(-ENODEV);                                \
204         }                                                       \
205 } while (0)
206
207
208 #ifdef LPROCFS
209 #define OBD_COUNTER_OFFSET(op)                                  \
210         ((offsetof(struct obd_ops, o_ ## op) -                  \
211           offsetof(struct obd_ops, o_iocontrol))                \
212          / sizeof(((struct obd_ops *)(0))->o_iocontrol))
213
214 #define OBD_COUNTER_INCREMENT(obd, op)                          \
215         if ((obd)->obd_stats != NULL) {                         \
216                 unsigned int coffset;                           \
217                 coffset = (unsigned int)(obd)->obd_cntr_base +  \
218                         OBD_COUNTER_OFFSET(op);                 \
219                 LASSERT(coffset < obd->obd_stats->ls_num);      \
220                 lprocfs_counter_incr(obd->obd_stats, coffset);  \
221         }
222 #else
223 #define OBD_COUNTER_OFFSET(op)
224 #define OBD_COUNTER_INCREMENT(obd, op)
225 #endif
226
227 #define OBD_CHECK_OP(obd, op)                                   \
228 do {                                                            \
229         if (!OBP((obd), op)) {                                  \
230                 CERROR("obd_" #op ": dev %d no operation\n",    \
231                        obd->obd_minor);                         \
232                 RETURN(-EOPNOTSUPP);                            \
233         }                                                       \
234 } while (0)
235
236 static inline int obd_get_info(struct lustre_handle *conn, __u32 keylen,
237                                void *key, __u32 *vallen, void *val)
238 {
239         struct obd_export *exp;
240         int rc;
241         ENTRY;
242
243         OBD_CHECK_ACTIVE(conn, exp);
244         OBD_CHECK_OP(exp->exp_obd, get_info);
245         OBD_COUNTER_INCREMENT(exp->exp_obd, get_info);
246
247         rc = OBP(exp->exp_obd, get_info)(conn, keylen, key, vallen, val);
248         class_export_put(exp);
249         RETURN(rc);
250 }
251
252 static inline int obd_set_info(struct lustre_handle *conn, obd_count keylen,
253                                void *key, obd_count vallen, void *val)
254 {
255         struct obd_export *exp;
256         int rc;
257         ENTRY;
258
259         OBD_CHECK_ACTIVE(conn, exp);
260         OBD_CHECK_OP(exp->exp_obd, set_info);
261         OBD_COUNTER_INCREMENT(exp->exp_obd, set_info);
262
263         rc = OBP(exp->exp_obd, set_info)(conn, keylen, key, vallen, val);
264         class_export_put(exp);
265         RETURN(rc);
266 }
267
268 static inline int obd_setup(struct obd_device *obd, int datalen, void *data)
269 {
270         int rc;
271         ENTRY;
272
273         OBD_CHECK_OP(obd, setup);
274         OBD_COUNTER_INCREMENT(obd, setup);
275
276         rc = OBP(obd, setup)(obd, datalen, data);
277         RETURN(rc);
278 }
279
280 static inline int obd_cleanup(struct obd_device *obd, int force, int failover)
281 {
282         int rc;
283         ENTRY;
284
285         OBD_CHECK_DEV_STOPPING(obd);
286         OBD_CHECK_OP(obd, cleanup);
287         OBD_COUNTER_INCREMENT(obd, cleanup);
288
289         rc = OBP(obd, cleanup)(obd, force, failover);
290         RETURN(rc);
291 }
292
293 /* Pack an in-memory MD struct for storage on disk.
294  * Returns +ve size of packed MD (0 for free), or -ve error.
295  *
296  * If @disk_tgt == NULL, MD size is returned (max size if @mem_src == NULL).
297  * If @*disk_tgt != NULL and @mem_src == NULL, @*disk_tgt will be freed.
298  * If @*disk_tgt == NULL, it will be allocated
299  */
300 static inline int obd_packmd(struct lustre_handle *conn,
301                              struct lov_mds_md **disk_tgt,
302                              struct lov_stripe_md *mem_src)
303 {
304         struct obd_export *exp;
305         int rc;
306         ENTRY;
307
308         OBD_CHECK_ACTIVE(conn, exp);
309         OBD_CHECK_OP(exp->exp_obd, packmd);
310         OBD_COUNTER_INCREMENT(exp->exp_obd, packmd);
311
312         rc = OBP(exp->exp_obd, packmd)(conn, disk_tgt, mem_src);
313         class_export_put(exp);
314         RETURN(rc);
315 }
316
317 static inline int obd_size_diskmd(struct lustre_handle *conn,
318                                   struct lov_stripe_md *mem_src)
319 {
320         return obd_packmd(conn, NULL, mem_src);
321 }
322
323 /* helper functions */
324 static inline int obd_alloc_diskmd(struct lustre_handle *conn,
325                                    struct lov_mds_md **disk_tgt)
326 {
327         LASSERT(disk_tgt);
328         LASSERT(*disk_tgt == NULL);
329         return obd_packmd(conn, disk_tgt, NULL);
330 }
331
332 static inline int obd_free_diskmd(struct lustre_handle *conn,
333                                   struct lov_mds_md **disk_tgt)
334 {
335         LASSERT(disk_tgt);
336         LASSERT(*disk_tgt);
337         return obd_packmd(conn, disk_tgt, NULL);
338 }
339
340 /* Unpack an MD struct from disk to in-memory format.
341  * Returns +ve size of unpacked MD (0 for free), or -ve error.
342  *
343  * If @mem_tgt == NULL, MD size is returned (max size if @disk_src == NULL).
344  * If @*mem_tgt != NULL and @disk_src == NULL, @*mem_tgt will be freed.
345  * If @*mem_tgt == NULL, it will be allocated
346  */
347 static inline int obd_unpackmd(struct lustre_handle *conn,
348                                struct lov_stripe_md **mem_tgt,
349                                struct lov_mds_md *disk_src,
350                                int disk_len)
351 {
352         struct obd_export *exp;
353         int rc;
354         ENTRY;
355
356         OBD_CHECK_ACTIVE(conn, exp);
357         OBD_CHECK_OP(exp->exp_obd, unpackmd);
358         OBD_COUNTER_INCREMENT(exp->exp_obd, unpackmd);
359
360         rc = OBP(exp->exp_obd, unpackmd)(conn, mem_tgt, disk_src, disk_len);
361         class_export_put(exp);
362         RETURN(rc);
363 }
364
365 static inline int obd_size_memmd(struct lustre_handle *conn,
366                                  struct lov_mds_md *disk_src,
367                                  int disk_len)
368 {
369         return obd_unpackmd(conn, NULL, disk_src, disk_len);
370 }
371
372 /* helper functions */
373 static inline int obd_alloc_memmd(struct lustre_handle *conn,
374                                   struct lov_stripe_md **mem_tgt)
375 {
376         LASSERT(mem_tgt);
377         LASSERT(*mem_tgt == NULL);
378         return obd_unpackmd(conn, mem_tgt, NULL, 0);
379 }
380
381 static inline int obd_free_memmd(struct lustre_handle *conn,
382                                  struct lov_stripe_md **mem_tgt)
383 {
384         LASSERT(mem_tgt);
385         LASSERT(*mem_tgt);
386         return obd_unpackmd(conn, mem_tgt, NULL, 0);
387 }
388
389 static inline int obd_create(struct lustre_handle *conn, struct obdo *obdo,
390                              struct lov_stripe_md **ea,
391                              struct obd_trans_info *oti)
392 {
393         struct obd_export *exp;
394         int rc;
395         ENTRY;
396
397         OBD_CHECK_ACTIVE(conn, exp);
398         OBD_CHECK_OP(exp->exp_obd, create);
399         OBD_COUNTER_INCREMENT(exp->exp_obd, create);
400
401         rc = OBP(exp->exp_obd, create)(conn, obdo, ea, oti);
402         class_export_put(exp);
403         RETURN(rc);
404 }
405
406 static inline int obd_destroy(struct lustre_handle *conn, struct obdo *obdo,
407                               struct lov_stripe_md *ea,
408                               struct obd_trans_info *oti)
409 {
410         struct obd_export *exp;
411         int rc;
412         ENTRY;
413
414         OBD_CHECK_ACTIVE(conn, exp);
415         OBD_CHECK_OP(exp->exp_obd, destroy);
416         OBD_COUNTER_INCREMENT(exp->exp_obd, destroy);
417
418         rc = OBP(exp->exp_obd, destroy)(conn, obdo, ea, oti);
419         class_export_put(exp);
420         RETURN(rc);
421 }
422
423 static inline int obd_getattr(struct lustre_handle *conn, struct obdo *obdo,
424                               struct lov_stripe_md *ea)
425 {
426         struct obd_export *exp;
427         int rc;
428         ENTRY;
429
430         OBD_CHECK_ACTIVE(conn, exp);
431         OBD_CHECK_OP(exp->exp_obd, getattr);
432         OBD_COUNTER_INCREMENT(exp->exp_obd, getattr);
433
434         rc = OBP(exp->exp_obd, getattr)(conn, obdo, ea);
435         class_export_put(exp);
436         RETURN(rc);
437 }
438
439 static inline int obd_getattr_async(struct lustre_handle *conn,
440                                     struct obdo *obdo, struct lov_stripe_md *ea,
441                                     struct ptlrpc_request_set *set)
442 {
443         struct obd_export *exp;
444         int rc;
445         ENTRY;
446
447         OBD_CHECK_SETUP(conn, exp);
448         OBD_CHECK_OP(exp->exp_obd, getattr);
449         OBD_COUNTER_INCREMENT(exp->exp_obd, getattr);
450
451         rc = OBP(exp->exp_obd, getattr_async)(conn, obdo, ea, set);
452         class_export_put(exp);
453         RETURN(rc);
454 }
455
456 static inline int obd_close(struct lustre_handle *conn, struct obdo *obdo,
457                             struct lov_stripe_md *ea,
458                             struct obd_trans_info *oti)
459 {
460         struct obd_export *exp;
461         int rc;
462         ENTRY;
463
464         OBD_CHECK_ACTIVE(conn, exp);
465         OBD_CHECK_OP(exp->exp_obd, close);
466         OBD_COUNTER_INCREMENT(exp->exp_obd, close);
467
468         rc = OBP(exp->exp_obd, close)(conn, obdo, ea, oti);
469         class_export_put(exp);
470         RETURN(rc);
471 }
472
473 static inline int obd_open(struct lustre_handle *conn, struct obdo *obdo,
474                            struct lov_stripe_md *ea, struct obd_trans_info *oti,
475                            struct obd_client_handle *och)
476 {
477         struct obd_export *exp;
478         int rc;
479         ENTRY;
480
481         OBD_CHECK_ACTIVE(conn, exp);
482         OBD_CHECK_OP(exp->exp_obd, open);
483         OBD_COUNTER_INCREMENT(exp->exp_obd, open);
484
485         rc = OBP(exp->exp_obd, open)(conn, obdo, ea, oti, och);
486         class_export_put(exp);
487         RETURN(rc);
488 }
489
490 static inline int obd_setattr(struct lustre_handle *conn, struct obdo *obdo,
491                               struct lov_stripe_md *ea,
492                               struct obd_trans_info *oti)
493 {
494         struct obd_export *exp;
495         int rc;
496         ENTRY;
497
498         OBD_CHECK_ACTIVE(conn, exp);
499         OBD_CHECK_OP(exp->exp_obd, setattr);
500         OBD_COUNTER_INCREMENT(exp->exp_obd, setattr);
501
502         rc = OBP(exp->exp_obd, setattr)(conn, obdo, ea, oti);
503         class_export_put(exp);
504         RETURN(rc);
505 }
506
507 static inline int obd_connect(struct lustre_handle *conn,
508                               struct obd_device *obd, struct obd_uuid *cluuid)
509 {
510         int rc;
511         ENTRY;
512
513         OBD_CHECK_DEV_ACTIVE(obd);
514         OBD_CHECK_OP(obd, connect);
515         OBD_COUNTER_INCREMENT(obd, connect);
516
517         rc = OBP(obd, connect)(conn, obd, cluuid);
518         RETURN(rc);
519 }
520
521 static inline int obd_disconnect(struct lustre_handle *conn, int failover)
522 {
523         struct obd_export *exp;
524         int rc;
525         ENTRY;
526
527         OBD_CHECK_SETUP(conn, exp);
528         OBD_CHECK_OP(exp->exp_obd, disconnect);
529         OBD_COUNTER_INCREMENT(exp->exp_obd, disconnect);
530
531         rc = OBP(exp->exp_obd, disconnect)(conn, failover);
532         class_export_put(exp);
533         RETURN(rc);
534 }
535
536 static inline void obd_destroy_export(struct obd_export *exp)
537 {
538         ENTRY;
539         if (OBP(exp->exp_obd, destroy_export))
540                 OBP(exp->exp_obd, destroy_export)(exp);
541         EXIT;
542 }
543
544 static inline int obd_statfs(struct obd_export *exp, struct obd_statfs *osfs)
545 {
546         int rc;
547         ENTRY;
548
549         OBD_CHECK_OP(exp->exp_obd, statfs);
550         OBD_COUNTER_INCREMENT(exp->exp_obd, statfs);
551
552         rc = OBP(exp->exp_obd, statfs)(exp, osfs);
553         RETURN(rc);
554 }
555
556 static inline int obd_syncfs(struct obd_export *exp)
557 {
558         int rc;
559         ENTRY;
560
561         OBD_CHECK_OP(exp->exp_obd, syncfs);
562         OBD_COUNTER_INCREMENT(exp->exp_obd, syncfs);
563
564         rc = OBP(exp->exp_obd, syncfs)(exp);
565         RETURN(rc);
566 }
567
568 static inline int obd_punch(struct lustre_handle *conn, struct obdo *oa,
569                             struct lov_stripe_md *ea, obd_size start,
570                             obd_size end, struct obd_trans_info *oti)
571 {
572         struct obd_export *exp;
573         int rc;
574         ENTRY;
575
576         OBD_CHECK_ACTIVE(conn, exp);
577         OBD_CHECK_OP(exp->exp_obd, punch);
578         OBD_COUNTER_INCREMENT(exp->exp_obd, punch);
579
580         rc = OBP(exp->exp_obd, punch)(conn, oa, ea, start, end, oti);
581         class_export_put(exp);
582         RETURN(rc);
583 }
584
585 static inline int obd_brw(int cmd, struct lustre_handle *conn,
586                           struct lov_stripe_md *ea, obd_count oa_bufs,
587                           struct brw_page *pg, struct obd_trans_info *oti)
588 {
589         struct obd_export *exp;
590         int rc;
591         ENTRY;
592
593         OBD_CHECK_ACTIVE(conn, exp);
594         OBD_CHECK_OP(exp->exp_obd, brw);
595         OBD_COUNTER_INCREMENT(exp->exp_obd, brw);
596
597         if (!(cmd & (OBD_BRW_RWMASK | OBD_BRW_CHECK))) {
598                 CERROR("obd_brw: cmd must be OBD_BRW_READ, OBD_BRW_WRITE, "
599                        "or OBD_BRW_CHECK\n");
600                 LBUG();
601         }
602
603         rc = OBP(exp->exp_obd, brw)(cmd, conn, ea, oa_bufs, pg, oti);
604         class_export_put(exp);
605         RETURN(rc);
606 }
607
608 static inline int obd_brw_async(int cmd, struct lustre_handle *conn,
609                                 struct lov_stripe_md *ea, obd_count oa_bufs,
610                                 struct brw_page *pg,
611                                 struct ptlrpc_request_set *set,
612                                 struct obd_trans_info *oti)
613 {
614         struct obd_export *exp;
615         int rc;
616         ENTRY;
617
618         OBD_CHECK_ACTIVE(conn, exp);
619         OBD_CHECK_OP(exp->exp_obd, brw_async);
620         OBD_COUNTER_INCREMENT(exp->exp_obd, brw_async);
621
622         if (!(cmd & OBD_BRW_RWMASK)) {
623                 CERROR("obd_brw: cmd must be OBD_BRW_READ or OBD_BRW_WRITE\n");
624                 LBUG();
625         }
626
627         rc = OBP(exp->exp_obd, brw_async)(cmd, conn, ea, oa_bufs, pg, set, oti);
628         class_export_put(exp);
629         RETURN(rc);
630 }
631
632 static inline int obd_preprw(int cmd, struct obd_export *exp, struct obdo *obdo,
633                              int objcount, struct obd_ioobj *obj,
634                              int niocount, struct niobuf_remote *remote,
635                              struct niobuf_local *local, void **desc_private,
636                              struct obd_trans_info *oti)
637 {
638         int rc;
639         ENTRY;
640
641         OBD_CHECK_OP(exp->exp_obd, preprw);
642         OBD_COUNTER_INCREMENT(exp->exp_obd, preprw);
643
644         rc = OBP(exp->exp_obd, preprw)(cmd, exp, obdo, objcount, obj, niocount,
645                                        remote, local, desc_private, oti);
646         RETURN(rc);
647 }
648
649 static inline int obd_commitrw(int cmd, struct obd_export *exp,
650                                int objcount, struct obd_ioobj *obj,
651                                int niocount, struct niobuf_local *local,
652                                void *desc_private, struct obd_trans_info *oti)
653 {
654         int rc;
655         ENTRY;
656
657         OBD_CHECK_OP(exp->exp_obd, commitrw);
658         OBD_COUNTER_INCREMENT(exp->exp_obd, commitrw);
659
660         rc = OBP(exp->exp_obd, commitrw)(cmd, exp, objcount, obj, niocount,
661                                          local, desc_private, oti);
662         RETURN(rc);
663 }
664
665 static inline int obd_iocontrol(unsigned int cmd, struct lustre_handle *conn,
666                                 int len, void *karg, void *uarg)
667 {
668         struct obd_export *exp;
669         int rc;
670         ENTRY;
671
672         OBD_CHECK_ACTIVE(conn, exp);
673         OBD_CHECK_OP(exp->exp_obd, iocontrol);
674         OBD_COUNTER_INCREMENT(exp->exp_obd, iocontrol);
675
676         rc = OBP(exp->exp_obd, iocontrol)(cmd, conn, len, karg, uarg);
677         class_export_put(exp);
678         RETURN(rc);
679 }
680
681 static inline int obd_enqueue(struct lustre_handle *conn,
682                               struct lov_stripe_md *ea,
683                               struct lustre_handle *parent_lock,
684                               __u32 type, void *cookie, int cookielen,
685                               __u32 mode, int *flags, void *cb, void *data,
686                               struct lustre_handle *lockh)
687 {
688         struct obd_export *exp;
689         int rc;
690         ENTRY;
691
692         OBD_CHECK_ACTIVE(conn, exp);
693         OBD_CHECK_OP(exp->exp_obd, enqueue);
694         OBD_COUNTER_INCREMENT(exp->exp_obd, enqueue);
695
696         rc = OBP(exp->exp_obd, enqueue)(conn, ea, parent_lock, type,
697                                         cookie, cookielen, mode, flags, cb,
698                                         data, lockh);
699         class_export_put(exp);
700         RETURN(rc);
701 }
702
703 static inline int obd_match(struct lustre_handle *conn,
704                             struct lov_stripe_md *ea, __u32 type, void *cookie,
705                             int cookielen, __u32 mode, int *flags, void *data,
706                             struct lustre_handle *lockh)
707 {
708         struct obd_export *exp;
709         int rc;
710         ENTRY;
711
712         OBD_CHECK_ACTIVE(conn, exp);
713         OBD_CHECK_OP(exp->exp_obd, match);
714         OBD_COUNTER_INCREMENT(exp->exp_obd, match);
715
716         rc = OBP(exp->exp_obd, match)(conn, ea, type, cookie, cookielen, mode,
717                                       flags, data, lockh);
718         class_export_put(exp);
719         RETURN(rc);
720 }
721
722
723 static inline int obd_cancel(struct lustre_handle *conn,
724                              struct lov_stripe_md *ea, __u32 mode,
725                              struct lustre_handle *lockh)
726 {
727         struct obd_export *exp;
728         int rc;
729         ENTRY;
730
731         OBD_CHECK_ACTIVE(conn, exp);
732         OBD_CHECK_OP(exp->exp_obd, cancel);
733         OBD_COUNTER_INCREMENT(exp->exp_obd, cancel);
734
735         rc = OBP(exp->exp_obd, cancel)(conn, ea, mode, lockh);
736         class_export_put(exp);
737         RETURN(rc);
738 }
739
740 static inline int obd_cancel_unused(struct lustre_handle *conn,
741                                     struct lov_stripe_md *ea, int flags,
742                                     void *opaque)
743 {
744         struct obd_export *exp;
745         int rc;
746         ENTRY;
747
748         OBD_CHECK_ACTIVE(conn, exp);
749         OBD_CHECK_OP(exp->exp_obd, cancel_unused);
750         OBD_COUNTER_INCREMENT(exp->exp_obd, cancel_unused);
751
752         rc = OBP(exp->exp_obd, cancel_unused)(conn, ea, flags, opaque);
753         class_export_put(exp);
754         RETURN(rc);
755 }
756
757 static inline int obd_san_preprw(int cmd, struct lustre_handle *conn,
758                                  int objcount, struct obd_ioobj *obj,
759                                  int niocount, struct niobuf_remote *remote)
760 {
761         struct obd_export *exp;
762         int rc;
763
764         OBD_CHECK_ACTIVE(conn, exp);
765         OBD_CHECK_OP(exp->exp_obd, preprw);
766         OBD_COUNTER_INCREMENT(exp->exp_obd, preprw);
767
768         rc = OBP(exp->exp_obd, san_preprw)(cmd, conn, objcount, obj,
769                                            niocount, remote);
770         class_export_put(exp);
771         RETURN(rc);
772 }
773
774 static inline int obd_mark_page_dirty(struct lustre_handle *conn,
775                                       struct lov_stripe_md *lsm,  
776                                       unsigned long offset)
777 {
778         struct obd_export *exp;
779         int rc;
780
781         OBD_CHECK_SETUP(conn, exp);
782         OBD_CHECK_OP(exp->exp_obd, mark_page_dirty);
783
784         rc = OBP(exp->exp_obd, mark_page_dirty)(conn, lsm, offset);
785         class_export_put(exp);
786         RETURN(rc);
787 }
788
789 static inline int obd_clear_dirty_pages(struct lustre_handle *conn,
790                                         struct lov_stripe_md *lsm,  
791                                         unsigned long start,
792                                         unsigned long end,
793                                         unsigned long *cleared)
794 {
795         struct obd_export *exp;
796         int rc;
797
798         OBD_CHECK_SETUP(conn, exp);
799         OBD_CHECK_OP(exp->exp_obd, clear_dirty_pages);
800
801         rc = OBP(exp->exp_obd, clear_dirty_pages)(conn, lsm, start, end,
802                                                   cleared);
803         class_export_put(exp);
804         RETURN(rc);
805 }
806
807 static inline int obd_last_dirty_offset(struct lustre_handle *conn,
808                                       struct lov_stripe_md *lsm,
809                                       unsigned long *offset)
810 {
811         struct obd_export *exp;
812         int rc;
813
814         OBD_CHECK_SETUP(conn, exp);
815         OBD_CHECK_OP(exp->exp_obd, last_dirty_offset);
816
817         rc = OBP(exp->exp_obd, last_dirty_offset)(conn, lsm, offset);
818         class_export_put(exp);
819         RETURN(rc);
820 }
821
822 /* OBD Metadata Support */
823
824 extern int obd_init_caches(void);
825 extern void obd_cleanup_caches(void);
826
827 static inline struct lustre_handle *obdo_handle(struct obdo *oa)
828 {
829         return (struct lustre_handle *)&oa->o_inline;
830 }
831
832 /* support routines */
833 extern kmem_cache_t *obdo_cachep;
834 static inline struct obdo *obdo_alloc(void)
835 {
836         struct obdo *oa;
837
838         oa = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
839         if (oa == NULL)
840                 LBUG();
841         memset(oa, 0, sizeof (*oa));
842
843         return oa;
844 }
845
846 static inline void obdo_free(struct obdo *oa)
847 {
848         if (!oa)
849                 return;
850         kmem_cache_free(obdo_cachep, oa);
851 }
852
853 #if !defined(__KERNEL__) || (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
854 #define to_kdev_t(dev) dev
855 #define kdev_t_to_nr(dev) dev
856 #endif
857
858 #ifdef __KERNEL__
859 static inline void obdo_from_iattr(struct obdo *oa, struct iattr *attr)
860 {
861         unsigned int ia_valid = attr->ia_valid;
862
863         if (ia_valid & ATTR_ATIME) {
864                 oa->o_atime = LTIME_S(attr->ia_atime);
865                 oa->o_valid |= OBD_MD_FLATIME;
866         }
867         if (ia_valid & ATTR_MTIME) {
868                 oa->o_mtime = LTIME_S(attr->ia_mtime);
869                 oa->o_valid |= OBD_MD_FLMTIME;
870         }
871         if (ia_valid & ATTR_CTIME) {
872                 oa->o_ctime = LTIME_S(attr->ia_ctime);
873                 oa->o_valid |= OBD_MD_FLCTIME;
874         }
875         if (ia_valid & ATTR_SIZE) {
876                 oa->o_size = attr->ia_size;
877                 oa->o_valid |= OBD_MD_FLSIZE;
878         }
879         if (ia_valid & ATTR_MODE) {
880                 oa->o_mode = attr->ia_mode;
881                 oa->o_valid |= OBD_MD_FLTYPE | OBD_MD_FLMODE;
882                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
883                         oa->o_mode &= ~S_ISGID;
884         }
885         if (ia_valid & ATTR_UID) {
886                 oa->o_uid = attr->ia_uid;
887                 oa->o_valid |= OBD_MD_FLUID;
888         }
889         if (ia_valid & ATTR_GID) {
890                 oa->o_gid = attr->ia_gid;
891                 oa->o_valid |= OBD_MD_FLGID;
892         }
893 }
894
895
896 static inline void iattr_from_obdo(struct iattr *attr, struct obdo *oa,
897                                    obd_flag valid)
898 {
899         memset(attr, 0, sizeof(*attr));
900         if (valid & OBD_MD_FLATIME) {
901                 LTIME_S(attr->ia_atime) = oa->o_atime;
902                 attr->ia_valid |= ATTR_ATIME;
903         }
904         if (valid & OBD_MD_FLMTIME) {
905                 LTIME_S(attr->ia_mtime) = oa->o_mtime;
906                 attr->ia_valid |= ATTR_MTIME;
907         }
908         if (valid & OBD_MD_FLCTIME) {
909                 LTIME_S(attr->ia_ctime) = oa->o_ctime;
910                 attr->ia_valid |= ATTR_CTIME;
911         }
912         if (valid & OBD_MD_FLSIZE) {
913                 attr->ia_size = oa->o_size;
914                 attr->ia_valid |= ATTR_SIZE;
915         }
916         if (valid & OBD_MD_FLTYPE) {
917                 attr->ia_mode = (attr->ia_mode & ~S_IFMT)|(oa->o_mode & S_IFMT);
918                 attr->ia_valid |= ATTR_MODE;
919         }
920         if (valid & OBD_MD_FLMODE) {
921                 attr->ia_mode = (attr->ia_mode & S_IFMT)|(oa->o_mode & ~S_IFMT);
922                 attr->ia_valid |= ATTR_MODE;
923                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
924                         attr->ia_mode &= ~S_ISGID;
925         }
926         if (valid & OBD_MD_FLUID)
927         {
928                 attr->ia_uid = oa->o_uid;
929                 attr->ia_valid |= ATTR_UID;
930         }
931         if (valid & OBD_MD_FLGID) {
932                 attr->ia_gid = oa->o_gid;
933                 attr->ia_valid |= ATTR_GID;
934         }
935 }
936
937
938 /* WARNING: the file systems must take care not to tinker with
939    attributes they don't manage (such as blocks). */
940
941
942 static inline void obdo_from_inode(struct obdo *dst, struct inode *src,
943                                    obd_flag valid)
944 {
945         if (valid & OBD_MD_FLATIME)
946                 dst->o_atime = LTIME_S(src->i_atime);
947         if (valid & OBD_MD_FLMTIME)
948                 dst->o_mtime = LTIME_S(src->i_mtime);
949         if (valid & OBD_MD_FLCTIME)
950                 dst->o_ctime = LTIME_S(src->i_ctime);
951         if (valid & OBD_MD_FLSIZE)
952                 dst->o_size = src->i_size;
953         if (valid & OBD_MD_FLBLOCKS)   /* allocation of space */
954                 dst->o_blocks = src->i_blocks;
955         if (valid & OBD_MD_FLBLKSZ)
956                 dst->o_blksize = src->i_blksize;
957         if (valid & OBD_MD_FLTYPE)
958                 dst->o_mode = (dst->o_mode & ~S_IFMT) | (src->i_mode & S_IFMT);
959         if (valid & OBD_MD_FLMODE)
960                 dst->o_mode = (dst->o_mode & S_IFMT) | (src->i_mode & ~S_IFMT);
961         if (valid & OBD_MD_FLUID)
962                 dst->o_uid = src->i_uid;
963         if (valid & OBD_MD_FLGID)
964                 dst->o_gid = src->i_gid;
965         if (valid & OBD_MD_FLFLAGS)
966                 dst->o_flags = src->i_flags;
967         if (valid & OBD_MD_FLNLINK)
968                 dst->o_nlink = src->i_nlink;
969         if (valid & OBD_MD_FLGENER)
970                 dst->o_generation = src->i_generation;
971         if (valid & OBD_MD_FLRDEV)
972                 dst->o_rdev = (__u32)kdev_t_to_nr(src->i_rdev);
973
974         dst->o_valid |= (valid & ~OBD_MD_FLID);
975 }
976
977 static inline void obdo_refresh_inode(struct inode *dst, struct obdo *src,
978                                       obd_flag valid)
979 {
980         valid &= src->o_valid;
981
982         if (valid & OBD_MD_FLATIME && src->o_atime > LTIME_S(dst->i_atime))
983                 LTIME_S(dst->i_atime) = src->o_atime;
984         if (valid & OBD_MD_FLMTIME && src->o_mtime > LTIME_S(dst->i_mtime))
985                 LTIME_S(dst->i_mtime) = src->o_mtime;
986         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
987                 LTIME_S(dst->i_ctime) = src->o_ctime;
988         if (valid & OBD_MD_FLSIZE && src->o_size > dst->i_size)
989                 dst->i_size = src->o_size;
990         /* allocation of space */
991         if (valid & OBD_MD_FLBLOCKS && src->o_blocks > dst->i_blocks)
992                 dst->i_blocks = src->o_blocks;
993 }
994
995 static inline void obdo_to_inode(struct inode *dst, struct obdo *src,
996                                  obd_flag valid)
997 {
998         valid &= src->o_valid;
999
1000         if (valid & OBD_MD_FLATIME)
1001                 LTIME_S(dst->i_atime) = src->o_atime;
1002         if (valid & OBD_MD_FLMTIME)
1003                 LTIME_S(dst->i_mtime) = src->o_mtime;
1004         if (valid & OBD_MD_FLCTIME && src->o_ctime > LTIME_S(dst->i_ctime))
1005                 LTIME_S(dst->i_ctime) = src->o_ctime;
1006         if (valid & OBD_MD_FLSIZE)
1007                 dst->i_size = src->o_size;
1008         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
1009                 dst->i_blocks = src->o_blocks;
1010         if (valid & OBD_MD_FLBLKSZ)
1011                 dst->i_blksize = src->o_blksize;
1012         if (valid & OBD_MD_FLTYPE)
1013                 dst->i_mode = (dst->i_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
1014         if (valid & OBD_MD_FLMODE)
1015                 dst->i_mode = (dst->i_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
1016         if (valid & OBD_MD_FLUID)
1017                 dst->i_uid = src->o_uid;
1018         if (valid & OBD_MD_FLGID)
1019                 dst->i_gid = src->o_gid;
1020         if (valid & OBD_MD_FLFLAGS)
1021                 dst->i_flags = src->o_flags;
1022         if (valid & OBD_MD_FLNLINK)
1023                 dst->i_nlink = src->o_nlink;
1024         if (valid & OBD_MD_FLGENER)
1025                 dst->i_generation = src->o_generation;
1026         if (valid & OBD_MD_FLRDEV)
1027                 dst->i_rdev = to_kdev_t(src->o_rdev);
1028 }
1029 #endif
1030
1031 static inline void obdo_cpy_md(struct obdo *dst, struct obdo *src,
1032                                obd_flag valid)
1033 {
1034 #ifdef __KERNEL__
1035         CDEBUG(D_INODE, "src obdo %Ld valid 0x%x, dst obdo %Ld\n",
1036                (unsigned long long)src->o_id, src->o_valid,
1037                (unsigned long long)dst->o_id);
1038 #endif
1039         if (valid & OBD_MD_FLATIME)
1040                 dst->o_atime = src->o_atime;
1041         if (valid & OBD_MD_FLMTIME)
1042                 dst->o_mtime = src->o_mtime;
1043         if (valid & OBD_MD_FLCTIME)
1044                 dst->o_ctime = src->o_ctime;
1045         if (valid & OBD_MD_FLSIZE)
1046                 dst->o_size = src->o_size;
1047         if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
1048                 dst->o_blocks = src->o_blocks;
1049         if (valid & OBD_MD_FLBLKSZ)
1050                 dst->o_blksize = src->o_blksize;
1051         if (valid & OBD_MD_FLTYPE)
1052                 dst->o_mode = (dst->o_mode & ~S_IFMT) | (src->o_mode & S_IFMT);
1053         if (valid & OBD_MD_FLMODE)
1054                 dst->o_mode = (dst->o_mode & S_IFMT) | (src->o_mode & ~S_IFMT);
1055         if (valid & OBD_MD_FLUID)
1056                 dst->o_uid = src->o_uid;
1057         if (valid & OBD_MD_FLGID)
1058                 dst->o_gid = src->o_gid;
1059         if (valid & OBD_MD_FLFLAGS)
1060                 dst->o_flags = src->o_flags;
1061         /*
1062         if (valid & OBD_MD_FLOBDFLG)
1063                 dst->o_obdflags = src->o_obdflags;
1064         */
1065         if (valid & OBD_MD_FLNLINK)
1066                 dst->o_nlink = src->o_nlink;
1067         if (valid & OBD_MD_FLGENER)
1068                 dst->o_generation = src->o_generation;
1069         if (valid & OBD_MD_FLRDEV)
1070                 dst->o_rdev = src->o_rdev;
1071         if (valid & OBD_MD_FLINLINE &&
1072              src->o_obdflags & OBD_FL_INLINEDATA) {
1073                 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
1074                 dst->o_obdflags |= OBD_FL_INLINEDATA;
1075         }
1076
1077         dst->o_valid |= valid;
1078 }
1079
1080
1081 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
1082 static inline int obdo_cmp_md(struct obdo *dst, struct obdo *src,
1083                               obd_flag compare)
1084 {
1085         int res = 0;
1086
1087         if ( compare & OBD_MD_FLATIME )
1088                 res = (res || (dst->o_atime != src->o_atime));
1089         if ( compare & OBD_MD_FLMTIME )
1090                 res = (res || (dst->o_mtime != src->o_mtime));
1091         if ( compare & OBD_MD_FLCTIME )
1092                 res = (res || (dst->o_ctime != src->o_ctime));
1093         if ( compare & OBD_MD_FLSIZE )
1094                 res = (res || (dst->o_size != src->o_size));
1095         if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
1096                 res = (res || (dst->o_blocks != src->o_blocks));
1097         if ( compare & OBD_MD_FLBLKSZ )
1098                 res = (res || (dst->o_blksize != src->o_blksize));
1099         if ( compare & OBD_MD_FLTYPE )
1100                 res = (res || (((dst->o_mode ^ src->o_mode) & S_IFMT) != 0));
1101         if ( compare & OBD_MD_FLMODE )
1102                 res = (res || (((dst->o_mode ^ src->o_mode) & ~S_IFMT) != 0));
1103         if ( compare & OBD_MD_FLUID )
1104                 res = (res || (dst->o_uid != src->o_uid));
1105         if ( compare & OBD_MD_FLGID )
1106                 res = (res || (dst->o_gid != src->o_gid));
1107         if ( compare & OBD_MD_FLFLAGS )
1108                 res = (res || (dst->o_flags != src->o_flags));
1109         if ( compare & OBD_MD_FLNLINK )
1110                 res = (res || (dst->o_nlink != src->o_nlink));
1111         if ( compare & OBD_MD_FLGENER )
1112                 res = (res || (dst->o_generation != src->o_generation));
1113         /* XXX Don't know if thses should be included here - wasn't previously
1114         if ( compare & OBD_MD_FLINLINE )
1115                 res = (res || memcmp(dst->o_inline, src->o_inline));
1116         */
1117         return res;
1118 }
1119
1120 /* I'm as embarrassed about this as you are.
1121  *
1122  * <shaver> // XXX do not look into _superhack with remaining eye
1123  * <shaver> // XXX if this were any uglier, I'd get my own show on MTV */
1124 extern int (*ptlrpc_put_connection_superhack)(struct ptlrpc_connection *c);
1125 extern void (*ptlrpc_abort_inflight_superhack)(struct obd_import *imp);
1126
1127 struct obd_statfs;
1128 struct statfs;
1129 void statfs_pack(struct obd_statfs *osfs, struct statfs *sfs);
1130 void statfs_unpack(struct statfs *sfs, struct obd_statfs *osfs);
1131
1132 struct obd_class_user_state {
1133         struct obd_device     *ocus_current_obd;
1134         struct list_head       ocus_conns;
1135 };
1136
1137 struct obd_class_user_conn {
1138         struct list_head       ocuc_chain;
1139         struct lustre_handle   ocuc_conn;
1140 };
1141
1142
1143 /* sysctl.c */
1144 extern void obd_sysctl_init (void);
1145 extern void obd_sysctl_clean (void);
1146
1147 /* uuid.c  */
1148 typedef __u8 class_uuid_t[16];
1149 //int class_uuid_parse(struct obd_uuid in, class_uuid_t out);
1150 void class_uuid_unparse(class_uuid_t in, struct obd_uuid *out);
1151
1152 /* lustre_peer.c    */
1153 int lustre_uuid_to_peer(char *uuid, struct lustre_peer *peer);
1154 int class_add_uuid(char *uuid, __u64 nid, __u32 nal);
1155 int class_del_uuid (char *uuid);
1156 void class_init_uuidlist(void);
1157 void class_exit_uuidlist(void);
1158
1159 #endif /* __LINUX_OBD_CLASS_H */