1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001, 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
23 #ifndef __LINUX_CLASS_OBD_H
24 #define __LINUX_CLASS_OBD_H
29 # include <linux/list.h>
32 #include <asm/segment.h>
33 #include <asm/uaccess.h>
34 #include <linux/types.h>
36 #include <linux/time.h>
38 #include <linux/obd_support.h>
39 #include <linux/obd.h>
40 #include <linux/lustre_lib.h>
41 #include <linux/lustre_idl.h>
42 #include <linux/lustre_mds.h>
43 #include <linux/lustre_dlm.h>
48 * ======== OBD Device Declarations ===========
50 #define MAX_OBD_DEVICES 128
51 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
53 #define OBD_ATTACHED 0x1
54 #define OBD_SET_UP 0x2
56 extern struct proc_dir_entry *
57 proc_lustre_register_obd_device(struct obd_device *obd);
58 extern void proc_lustre_release_obd_device(struct obd_device *obd);
59 extern void proc_lustre_remove_obd_entry(const char* name,
60 struct obd_device *obd);
63 * ======== OBD Operations Declarations ===========
69 struct lustre_handle exp_rconnh; /* remote connection handle */
70 struct lustre_handle exp_impconnh;
71 struct list_head exp_chain;
72 struct obd_device *exp_obd;
73 struct ptlrpc_connection *exp_connection;
74 struct mds_export_data exp_mds_data;
75 struct ldlm_export_data exp_ldlm_data;
77 struct ost_export_data exp_ost_data;
79 void *exp_data; /* device specific data */
85 extern struct obd_export *class_conn2export(struct lustre_handle *conn);
86 extern struct obd_device *class_conn2obd(struct lustre_handle *conn);
87 extern int class_rconn2export(struct lustre_handle *conn,
88 struct lustre_handle *rconn);
92 struct lustre_handle imp_expconnh;
93 struct list_head imp_chain;
94 struct obd_device *imp_obd;
96 void *imp_data; /* device specific data */
99 static inline int obd_check_conn(struct lustre_handle *conn)
101 struct obd_device *obd;
103 CERROR("NULL conn\n");
106 obd = class_conn2obd(conn);
108 CERROR("NULL obd\n");
112 if (!obd->obd_flags & OBD_ATTACHED ) {
113 CERROR("obd %d not attached\n", obd->obd_minor);
117 if (!obd->obd_flags & OBD_SET_UP) {
118 CERROR("obd %d not setup\n", obd->obd_minor);
122 if (!obd->obd_type) {
123 CERROR("obd %d not typed\n", obd->obd_minor);
127 if (!obd->obd_type->typ_ops) {
128 CERROR("obd_check_conn: obd %d no operations\n",
136 #define OBT(dev) (dev)->obd_type
137 #define OBP(dev,op) (dev)->obd_type->typ_ops->o_ ## op
139 #define OBD_CHECK_SETUP(conn, export) \
142 CERROR("NULL connection\n"); \
146 export = class_conn2export(conn); \
148 CERROR("No export\n"); \
152 if (!((export)->exp_obd->obd_flags & OBD_SET_UP)) { \
153 CERROR("Device %d not setup\n", \
154 (export)->exp_obd->obd_minor); \
159 #define OBD_CHECK_DEVSETUP(obd) \
162 CERROR("NULL device\n"); \
166 if ( !((obd)->obd_flags & OBD_SET_UP) ) { \
167 CERROR("Device %d not setup\n", \
173 #define OBD_CHECK_OP(obd,op) \
175 if (!OBP((obd),op)) { \
176 CERROR("obd_" #op ": dev %d no operation\n", \
178 RETURN(-EOPNOTSUPP); \
182 static inline int obd_get_info(struct lustre_handle *conn, obd_count keylen,
183 void *key, obd_count *vallen, void **val)
186 struct obd_export *export;
187 OBD_CHECK_SETUP(conn, export);
188 OBD_CHECK_OP(export->exp_obd,get_info);
190 rc = OBP(export->exp_obd, get_info)(conn, keylen, key, vallen, val);
194 static inline int obd_set_info(struct lustre_handle *conn, obd_count keylen,
195 void *key, obd_count vallen, void *val)
198 struct obd_export *export;
199 OBD_CHECK_SETUP(conn, export);
200 OBD_CHECK_OP(export->exp_obd,set_info);
202 rc = OBP(export->exp_obd, set_info)(conn, keylen, key, vallen, val);
206 static inline int obd_setup(struct obd_device *obd, int datalen, void *data)
210 OBD_CHECK_OP(obd,setup);
212 rc = OBP(obd, setup)(obd, datalen, data);
216 static inline int obd_cleanup(struct obd_device *obd)
220 OBD_CHECK_DEVSETUP(obd);
221 OBD_CHECK_OP(obd,cleanup);
223 rc = OBP(obd, cleanup)(obd);
227 static inline int obd_create(struct lustre_handle *conn, struct obdo *obdo, struct lov_stripe_md **ea)
230 struct obd_export *export;
231 OBD_CHECK_SETUP(conn, export);
232 OBD_CHECK_OP(export->exp_obd,create);
234 #define OBD_MD_FLNEEDED (OBD_MD_FLID | OBD_MD_FLMODE)
235 //if (obdo->o_valid & OBD_MD_FLNEEDED != OBD_MD_FLNEEDED)
237 #undef OBD_MD_FLNEEDED
238 rc = OBP(export->exp_obd, create)(conn, obdo, ea);
242 static inline int obd_destroy(struct lustre_handle *conn, struct obdo *obdo, struct lov_stripe_md *ea)
245 struct obd_export *export;
246 OBD_CHECK_SETUP(conn, export);
247 OBD_CHECK_OP(export->exp_obd,destroy);
249 rc = OBP(export->exp_obd, destroy)(conn, obdo, ea);
253 static inline int obd_getattr(struct lustre_handle *conn,
255 struct lov_stripe_md *ea)
258 struct obd_export *export;
259 OBD_CHECK_SETUP(conn, export);
260 OBD_CHECK_OP(export->exp_obd,getattr);
262 rc = OBP(export->exp_obd, getattr)(conn, obdo, ea);
266 static inline int obd_close(struct lustre_handle *conn, struct obdo *obdo, struct lov_stripe_md *md)
269 struct obd_export *export;
270 OBD_CHECK_SETUP(conn, export);
271 OBD_CHECK_OP(export->exp_obd,close);
273 rc = OBP(export->exp_obd, close)(conn, obdo, md);
276 static inline int obd_open(struct lustre_handle *conn, struct obdo *obdo,
277 struct lov_stripe_md *md)
280 struct obd_export *export;
281 OBD_CHECK_SETUP(conn, export);
282 OBD_CHECK_OP(export->exp_obd,open);
284 rc = OBP(export->exp_obd, open) (conn, obdo, md);
288 static inline int obd_setattr(struct lustre_handle *conn,
290 struct lov_stripe_md *ea)
293 struct obd_export *export;
294 OBD_CHECK_SETUP(conn, export);
295 OBD_CHECK_OP(export->exp_obd,setattr);
297 rc = OBP(export->exp_obd, setattr)(conn, obdo, ea);
301 static inline int obd_connect(struct lustre_handle *conn, struct obd_device *obd,
305 OBD_CHECK_DEVSETUP(obd);
306 OBD_CHECK_OP(obd,connect);
308 rc = OBP(obd, connect)(conn, obd, cluuid);
312 static inline int obd_disconnect(struct lustre_handle *conn)
315 struct obd_export *export;
316 OBD_CHECK_SETUP(conn, export);
317 OBD_CHECK_OP(export->exp_obd,disconnect);
319 rc = OBP(export->exp_obd, disconnect)(conn);
323 static inline int obd_statfs(struct lustre_handle *conn, struct statfs *buf)
326 struct obd_export *export;
327 OBD_CHECK_SETUP(conn, export);
328 OBD_CHECK_OP(export->exp_obd,statfs);
330 rc = OBP(export->exp_obd, statfs)(conn, buf);
334 static inline int obd_punch(struct lustre_handle *conn, struct obdo *tgt,
335 struct lov_stripe_md *md,
336 obd_size count, obd_off offset)
339 struct obd_export *export;
340 OBD_CHECK_SETUP(conn, export);
341 OBD_CHECK_OP(export->exp_obd,punch);
343 rc = OBP(export->exp_obd, punch)(conn, tgt, md, count, offset);
347 static inline int obd_brw(int cmd, struct lustre_handle *conn,
348 struct lov_stripe_md *md,
351 brw_callback_t callback, void *data)
354 struct obd_export *export;
355 OBD_CHECK_SETUP(conn, export);
356 OBD_CHECK_OP(export->exp_obd,brw);
358 if (!(cmd & OBD_BRW_RWMASK)) {
359 CERROR("obd_brw: cmd must be OBD_BRW_READ or OBD_BRW_WRITE\n");
363 rc = OBP(export->exp_obd, brw)(cmd, conn, md, oa_bufs, pg, callback, data);
367 static inline int obd_preprw(int cmd, struct lustre_handle *conn,
368 int objcount, struct obd_ioobj *obj,
369 int niocount, struct niobuf_remote *remote,
370 struct niobuf_local *local, void **desc_private)
373 struct obd_export *export;
374 OBD_CHECK_SETUP(conn, export);
375 OBD_CHECK_OP(export->exp_obd,preprw);
377 rc = OBP(export->exp_obd, preprw)(cmd, conn, objcount, obj, niocount,
378 remote, local, desc_private);
382 static inline int obd_commitrw(int cmd, struct lustre_handle *conn,
383 int objcount, struct obd_ioobj *obj,
384 int niocount, struct niobuf_local *local,
388 struct obd_export *export;
389 OBD_CHECK_SETUP(conn, export);
390 OBD_CHECK_OP(export->exp_obd,commitrw);
392 rc = OBP(export->exp_obd, commitrw)(cmd, conn, objcount, obj, niocount,
393 local, desc_private);
397 static inline int obd_iocontrol(int cmd, struct lustre_handle *conn,
398 int len, void *karg, void *uarg)
401 struct obd_export *export;
402 OBD_CHECK_SETUP(conn, export);
403 OBD_CHECK_OP(export->exp_obd,iocontrol);
405 rc = OBP(export->exp_obd, iocontrol)(cmd, conn, len, karg, uarg);
409 static inline int obd_enqueue(struct lustre_handle *conn,
410 struct lov_stripe_md *md,
411 struct lustre_handle *parent_lock,
412 __u32 type, void *cookie, int cookielen,
413 __u32 mode, int *flags, void *cb, void *data,
414 int datalen, struct lustre_handle *lockh)
417 struct obd_export *export;
418 OBD_CHECK_SETUP(conn, export);
419 OBD_CHECK_OP(export->exp_obd,enqueue);
421 rc = OBP(export->exp_obd, enqueue)(conn, md, parent_lock, type,
422 cookie, cookielen, mode, flags, cb,
423 data, datalen, lockh);
427 static inline int obd_cancel(struct lustre_handle *conn, struct lov_stripe_md *md, __u32 mode,
428 struct lustre_handle *lockh)
431 struct obd_export *export;
432 OBD_CHECK_SETUP(conn, export);
433 OBD_CHECK_OP(export->exp_obd,cancel);
435 rc = OBP(export->exp_obd, cancel)(conn, md, mode, lockh);
442 * ======== OBD Metadata Support ===========
445 extern int obd_init_caches(void);
446 extern void obd_cleanup_caches(void);
448 static inline int obdo_has_inline(struct obdo *obdo)
450 return (obdo->o_valid & OBD_MD_FLINLINE &&
451 obdo->o_obdflags & OBD_FL_INLINEDATA);
455 /* support routines */
456 extern kmem_cache_t *obdo_cachep;
457 static inline struct obdo *obdo_alloc(void)
459 struct obdo *oa = NULL;
461 oa = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
464 memset(oa, 0, sizeof (*oa));
468 static inline void obdo_free(struct obdo *oa)
472 kmem_cache_free(obdo_cachep, oa);
476 static inline void obdo_from_iattr(struct obdo *oa, struct iattr *attr)
478 unsigned int ia_valid = attr->ia_valid;
480 if (ia_valid & ATTR_ATIME) {
481 oa->o_atime = attr->ia_atime;
482 oa->o_valid |= OBD_MD_FLATIME;
484 if (ia_valid & ATTR_MTIME) {
485 oa->o_mtime = attr->ia_mtime;
486 oa->o_valid |= OBD_MD_FLMTIME;
488 if (ia_valid & ATTR_CTIME) {
489 oa->o_ctime = attr->ia_ctime;
490 oa->o_valid |= OBD_MD_FLCTIME;
492 if (ia_valid & ATTR_SIZE) {
493 oa->o_size = attr->ia_size;
494 oa->o_valid |= OBD_MD_FLSIZE;
496 if (ia_valid & ATTR_MODE) {
497 oa->o_mode = attr->ia_mode;
498 oa->o_valid |= OBD_MD_FLMODE;
499 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
500 oa->o_mode &= ~S_ISGID;
502 if (ia_valid & ATTR_UID)
504 oa->o_uid = attr->ia_uid;
505 oa->o_valid |= OBD_MD_FLUID;
507 if (ia_valid & ATTR_GID) {
508 oa->o_gid = attr->ia_gid;
509 oa->o_valid |= OBD_MD_FLGID;
514 static inline void iattr_from_obdo(struct iattr *attr, struct obdo *oa,
517 memset(attr, 0, sizeof(*attr));
518 if (valid & OBD_MD_FLATIME) {
519 attr->ia_atime = oa->o_atime;
520 attr->ia_valid |= ATTR_ATIME;
522 if (valid & OBD_MD_FLMTIME) {
523 attr->ia_mtime = oa->o_mtime;
524 attr->ia_valid |= ATTR_MTIME;
526 if (valid & OBD_MD_FLCTIME) {
527 attr->ia_ctime = oa->o_ctime;
528 attr->ia_valid |= ATTR_CTIME;
530 if (valid & OBD_MD_FLSIZE) {
531 attr->ia_size = oa->o_size;
532 attr->ia_valid |= ATTR_SIZE;
534 if (valid & OBD_MD_FLMODE) {
535 attr->ia_mode = oa->o_mode;
536 attr->ia_valid |= ATTR_MODE;
537 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
538 attr->ia_mode &= ~S_ISGID;
540 if (valid & OBD_MD_FLUID)
542 attr->ia_uid = oa->o_uid;
543 attr->ia_valid |= ATTR_UID;
545 if (valid & OBD_MD_FLGID) {
546 attr->ia_gid = oa->o_gid;
547 attr->ia_valid |= ATTR_GID;
552 /* WARNING: the file systems must take care not to tinker with
553 attributes they don't manage (such as blocks). */
555 static inline void obdo_from_inode(struct obdo *dst, struct inode *src,
558 // if (valid & OBD_MD_FLID)
559 // dst->o_id = src->i_ino;
560 if (valid & OBD_MD_FLATIME)
561 dst->o_atime = src->i_atime;
562 if (valid & OBD_MD_FLMTIME)
563 dst->o_mtime = src->i_mtime;
564 if (valid & OBD_MD_FLCTIME)
565 dst->o_ctime = src->i_ctime;
566 if (valid & OBD_MD_FLSIZE)
567 dst->o_size = src->i_size;
568 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
569 dst->o_blocks = src->i_blocks;
570 if (valid & OBD_MD_FLBLKSZ)
571 dst->o_blksize = src->i_blksize;
572 if (valid & OBD_MD_FLMODE)
573 dst->o_mode = src->i_mode;
574 if (valid & OBD_MD_FLUID)
575 dst->o_uid = src->i_uid;
576 if (valid & OBD_MD_FLGID)
577 dst->o_gid = src->i_gid;
578 if (valid & OBD_MD_FLFLAGS)
579 dst->o_flags = src->i_flags;
580 if (valid & OBD_MD_FLNLINK)
581 dst->o_nlink = src->i_nlink;
582 if (valid & OBD_MD_FLGENER)
583 dst->o_generation = src->i_generation;
584 if (valid & OBD_MD_FLRDEV)
585 dst->o_rdev = src->i_rdev;
587 dst->o_valid |= (valid & ~OBD_MD_FLID);
590 static inline void obdo_to_inode(struct inode *dst, struct obdo *src,
593 // if (valid & OBD_MD_FLID)
594 // dst->i_ino = src->o_id;
595 if (valid & OBD_MD_FLATIME)
596 dst->i_atime = src->o_atime;
597 if (valid & OBD_MD_FLMTIME)
598 dst->i_mtime = src->o_mtime;
599 if (valid & OBD_MD_FLCTIME)
600 dst->i_ctime = src->o_ctime;
601 if (valid & OBD_MD_FLSIZE)
602 dst->i_size = src->o_size;
603 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
604 dst->i_blocks = src->o_blocks;
605 if (valid & OBD_MD_FLBLKSZ)
606 dst->i_blksize = src->o_blksize;
607 if (valid & OBD_MD_FLMODE)
608 dst->i_mode = src->o_mode;
609 if (valid & OBD_MD_FLUID)
610 dst->i_uid = src->o_uid;
611 if (valid & OBD_MD_FLGID)
612 dst->i_gid = src->o_gid;
613 if (valid & OBD_MD_FLFLAGS)
614 dst->i_flags = src->o_flags;
615 if (valid & OBD_MD_FLNLINK)
616 dst->i_nlink = src->o_nlink;
617 if (valid & OBD_MD_FLGENER)
618 dst->i_generation = src->o_generation;
619 if (valid & OBD_MD_FLRDEV)
620 dst->i_rdev = src->o_rdev;
624 static inline void obdo_cpy_md(struct obdo *dst, struct obdo *src,
628 CDEBUG(D_INODE, "src obdo %Ld valid 0x%x, dst obdo %Ld\n",
629 (unsigned long long)src->o_id, src->o_valid,
630 (unsigned long long)dst->o_id);
632 if (valid & OBD_MD_FLATIME)
633 dst->o_atime = src->o_atime;
634 if (valid & OBD_MD_FLMTIME)
635 dst->o_mtime = src->o_mtime;
636 if (valid & OBD_MD_FLCTIME)
637 dst->o_ctime = src->o_ctime;
638 if (valid & OBD_MD_FLSIZE)
639 dst->o_size = src->o_size;
640 if (valid & OBD_MD_FLBLOCKS) /* allocation of space */
641 dst->o_blocks = src->o_blocks;
642 if (valid & OBD_MD_FLBLKSZ)
643 dst->o_blksize = src->o_blksize;
644 if (valid & OBD_MD_FLMODE)
645 dst->o_mode = src->o_mode;
646 if (valid & OBD_MD_FLUID)
647 dst->o_uid = src->o_uid;
648 if (valid & OBD_MD_FLGID)
649 dst->o_gid = src->o_gid;
650 if (valid & OBD_MD_FLFLAGS)
651 dst->o_flags = src->o_flags;
653 if (valid & OBD_MD_FLOBDFLG)
654 dst->o_obdflags = src->o_obdflags;
656 if (valid & OBD_MD_FLNLINK)
657 dst->o_nlink = src->o_nlink;
658 if (valid & OBD_MD_FLGENER)
659 dst->o_generation = src->o_generation;
660 if (valid & OBD_MD_FLRDEV)
661 dst->o_rdev = src->o_rdev;
662 if (valid & OBD_MD_FLINLINE &&
663 src->o_obdflags & OBD_FL_INLINEDATA) {
664 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
665 dst->o_obdflags |= OBD_FL_INLINEDATA;
668 dst->o_valid |= valid;
672 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
673 static inline int obdo_cmp_md(struct obdo *dst, struct obdo *src,
678 if ( compare & OBD_MD_FLATIME )
679 res = (res || (dst->o_atime != src->o_atime));
680 if ( compare & OBD_MD_FLMTIME )
681 res = (res || (dst->o_mtime != src->o_mtime));
682 if ( compare & OBD_MD_FLCTIME )
683 res = (res || (dst->o_ctime != src->o_ctime));
684 if ( compare & OBD_MD_FLSIZE )
685 res = (res || (dst->o_size != src->o_size));
686 if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
687 res = (res || (dst->o_blocks != src->o_blocks));
688 if ( compare & OBD_MD_FLBLKSZ )
689 res = (res || (dst->o_blksize != src->o_blksize));
690 if ( compare & OBD_MD_FLMODE )
691 res = (res || (dst->o_mode != src->o_mode));
692 if ( compare & OBD_MD_FLUID )
693 res = (res || (dst->o_uid != src->o_uid));
694 if ( compare & OBD_MD_FLGID )
695 res = (res || (dst->o_gid != src->o_gid));
696 if ( compare & OBD_MD_FLFLAGS )
697 res = (res || (dst->o_flags != src->o_flags));
698 if ( compare & OBD_MD_FLNLINK )
699 res = (res || (dst->o_nlink != src->o_nlink));
700 if ( compare & OBD_MD_FLGENER )
701 res = (res || (dst->o_generation != src->o_generation));
702 /* XXX Don't know if thses should be included here - wasn't previously
703 if ( compare & OBD_MD_FLINLINE )
704 res = (res || memcmp(dst->o_inline, src->o_inline));
711 int class_register_type(struct obd_ops *ops, char *nm);
712 int class_unregister_type(char *nm);
713 int class_name2dev(char *name);
714 int class_uuid2dev(char *name);
715 struct obd_device *class_uuid2obd(char *name);
716 struct obd_export *class_new_export(struct obd_device *obddev);
717 void class_destroy_export(struct obd_export *exp);
718 int class_connect(struct lustre_handle *conn, struct obd_device *obd,
720 int class_disconnect(struct lustre_handle *conn);
721 void class_disconnect_all(struct obd_device *obddev);
722 struct obd_export *class_conn2export(struct lustre_handle *);
724 /* generic operations shared by various OBD types */
725 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data);
726 int class_multi_cleanup(struct obd_device *obddev);
728 extern void (*class_signal_connection_failure)(struct ptlrpc_connection *);
730 /* == mds_client_free if MDS running here */
731 extern int (*mds_destroy_export)(struct obd_export *exp);
732 /* == ldlm_client_free if(?) DLM running here */
733 extern int (*ldlm_destroy_export)(struct obd_export *exp);
738 extern void obd_sysctl_init (void);
739 extern void obd_sysctl_clean (void);
742 /* XXX - should use uuid_t here, but already defined as char[37] */
743 typedef unsigned char class_uuid_t[16];
744 int class_uuid_parse(char *in, class_uuid_t out);
745 void class_uuid_unparse(class_uuid_t in, char *out);
746 #endif /* __LINUX_CLASS_OBD_H */