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>
45 * ======== OBD Device Declarations ===========
47 #define MAX_OBD_DEVICES 128
48 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
50 #define OBD_ATTACHED 0x1
51 #define OBD_SET_UP 0x2
53 extern struct proc_dir_entry *
54 proc_lustre_register_obd_device(struct obd_device *obd);
55 extern void proc_lustre_release_obd_device(struct obd_device *obd);
56 extern void proc_lustre_remove_obd_entry(const char* name,
57 struct obd_device *obd);
60 * ======== OBD Operations Declarations ===========
63 #define OBD_BRW_READ 1
64 #define OBD_BRW_WRITE 2
65 #define OBD_BRW_RWMASK (OBD_BRW_READ | OBD_BRW_WRITE)
66 #define OBD_BRW_CREATE 4
69 extern struct obd_export *gen_client(struct obd_conn *conn);
70 extern struct obd_device *gen_conn2obd(struct obd_conn *conn);
73 struct lustre_handle export_import; /* client handle */
74 struct list_head export_chain;
75 struct obd_device *export_obd;
76 unsigned int export_id;
77 void *export_data; /* device specific data */
82 struct lustre_handle import_export; /* client handle */
83 struct list_head import_chain;
84 struct obd_device *import_obd;
85 unsigned int import_id;
86 void *import_data; /* device specific data */
92 struct obd_conn *conn;
98 static inline int obd_check_conn(struct obd_conn *conn)
100 struct obd_device *obd;
102 CERROR("NULL conn\n");
105 obd = gen_conn2obd(conn);
107 CERROR("NULL obd\n");
111 if (!obd->obd_flags & OBD_ATTACHED ) {
112 CERROR("obd %d not attached\n", obd->obd_minor);
116 if (!obd->obd_flags & OBD_SET_UP) {
117 CERROR("obd %d not setup\n", obd->obd_minor);
121 if (!obd->obd_type) {
122 CERROR("obd %d not typed\n", obd->obd_minor);
126 if (!obd->obd_type->typ_ops) {
127 CERROR("obd_check_conn: obd %d no operations\n",
135 #define OBT(dev) (dev)->obd_type
136 #define OBP(dev,op) (dev)->obd_type->typ_ops->o_ ## op
138 #define OBD_CHECK_SETUP(conn, export) \
141 CERROR("NULL connection\n"); \
145 export = gen_client(conn);\
147 CERROR("No export\n"); \
151 if ( !((export)->export_obd->obd_flags & OBD_SET_UP) ) { \
152 CERROR("Device %d not setup\n", \
153 (export)->export_obd->obd_minor); \
158 #define OBD_CHECK_DEVSETUP(obd) \
161 CERROR("NULL device\n"); \
165 if ( !((obd)->obd_flags & OBD_SET_UP) ) { \
166 CERROR("Device %d not setup\n", \
172 #define OBD_CHECK_OP(obd,op) \
174 if (!OBP((obd),op)) { \
175 CERROR("obd_" #op ": dev %d no operation\n", \
177 RETURN(-EOPNOTSUPP); \
181 static inline int obd_get_info(struct obd_conn *conn, obd_count keylen,
182 void *key, obd_count *vallen, void **val)
185 struct obd_export *export;
186 OBD_CHECK_SETUP(conn, export);
187 OBD_CHECK_OP(export->export_obd,get_info);
189 rc = OBP(export->export_obd, get_info)(conn, keylen, key, vallen, val);
193 static inline int obd_set_info(struct obd_conn *conn, obd_count keylen,
194 void *key, obd_count vallen, void *val)
197 struct obd_export *export;
198 OBD_CHECK_SETUP(conn, export);
199 OBD_CHECK_OP(export->export_obd,set_info);
201 rc = OBP(export->export_obd, set_info)(conn, keylen, key, vallen, val);
205 static inline int obd_setup(struct obd_device *obd, int datalen, void *data)
209 OBD_CHECK_OP(obd,setup);
211 rc = OBP(obd, setup)(obd, datalen, data);
215 static inline int obd_cleanup(struct obd_device *obd)
219 OBD_CHECK_DEVSETUP(obd);
220 OBD_CHECK_OP(obd,cleanup);
222 rc = OBP(obd, cleanup)(obd);
226 static inline int obd_create(struct obd_conn *conn, struct obdo *obdo)
229 struct obd_export *export;
230 OBD_CHECK_SETUP(conn, export);
231 OBD_CHECK_OP(export->export_obd,create);
233 rc = OBP(export->export_obd, create)(conn, obdo);
237 static inline int obd_destroy(struct obd_conn *conn, struct obdo *obdo)
240 struct obd_export *export;
241 OBD_CHECK_SETUP(conn, export);
242 OBD_CHECK_OP(export->export_obd,destroy);
244 rc = OBP(export->export_obd, destroy)(conn, obdo);
248 static inline int obd_getattr(struct obd_conn *conn, struct obdo *obdo)
251 struct obd_export *export;
252 OBD_CHECK_SETUP(conn, export);
253 OBD_CHECK_OP(export->export_obd,getattr);
255 rc = OBP(export->export_obd, getattr)(conn, obdo);
259 static inline int obd_close(struct obd_conn *conn, struct obdo *obdo)
262 struct obd_export *export;
263 OBD_CHECK_SETUP(conn, export);
264 OBD_CHECK_OP(export->export_obd,close);
266 rc = OBP(export->export_obd, close)(conn, obdo);
269 static inline int obd_open(struct obd_conn *conn, struct obdo *obdo)
272 struct obd_export *export;
273 OBD_CHECK_SETUP(conn, export);
274 OBD_CHECK_OP(export->export_obd,open);
276 rc = OBP(export->export_obd, open) (conn, obdo);
280 static inline int obd_setattr(struct obd_conn *conn, struct obdo *obdo)
283 struct obd_export *export;
284 OBD_CHECK_SETUP(conn, export);
285 OBD_CHECK_OP(export->export_obd,setattr);
287 rc = OBP(export->export_obd, setattr)(conn, obdo);
291 static inline int obd_connect(struct obd_conn *conn, struct obd_device *obd)
294 OBD_CHECK_DEVSETUP(obd);
295 OBD_CHECK_OP(obd,connect);
297 rc = OBP(obd, connect)(conn, obd);
301 static inline int obd_disconnect(struct obd_conn *conn)
304 struct obd_export *export;
305 OBD_CHECK_SETUP(conn, export);
306 OBD_CHECK_OP(export->export_obd,disconnect);
308 rc = OBP(export->export_obd, disconnect)(conn);
312 static inline int obd_statfs(struct obd_conn *conn, struct statfs *buf)
315 struct obd_export *export;
316 OBD_CHECK_SETUP(conn, export);
317 OBD_CHECK_OP(export->export_obd,statfs);
319 rc = OBP(export->export_obd, statfs)(conn, buf);
323 static inline int obd_punch(struct obd_conn *conn, struct obdo *tgt,
324 obd_size count, obd_off offset)
327 struct obd_export *export;
328 OBD_CHECK_SETUP(conn, export);
329 OBD_CHECK_OP(export->export_obd,punch);
331 rc = OBP(export->export_obd, punch)(conn, tgt, count, offset);
335 static inline int obd_brw(int cmd, struct obd_conn *conn, obd_count num_oa,
336 struct obdo **oa, obd_count *oa_bufs,
337 struct page **buf, obd_size *count, obd_off *offset,
338 obd_flag *flags, void *callback)
341 struct obd_export *export;
342 OBD_CHECK_SETUP(conn, export);
343 OBD_CHECK_OP(export->export_obd,brw);
345 if (!(cmd & OBD_BRW_RWMASK)) {
346 CERROR("obd_brw: cmd must be OBD_BRW_READ or OBD_BRW_WRITE\n");
350 rc = OBP(export->export_obd, brw)(cmd, conn, num_oa, oa, oa_bufs, buf,
351 count, offset, flags, callback);
355 static inline int obd_preprw(int cmd, struct obd_conn *conn,
356 int objcount, struct obd_ioobj *obj,
357 int niocount, struct niobuf_remote *remote,
358 struct niobuf_local *local, void **desc_private)
361 struct obd_export *export;
362 OBD_CHECK_SETUP(conn, export);
363 OBD_CHECK_OP(export->export_obd,preprw);
365 rc = OBP(export->export_obd, preprw)(cmd, conn, objcount, obj, niocount,
366 remote, local, desc_private);
370 static inline int obd_commitrw(int cmd, struct obd_conn *conn,
371 int objcount, struct obd_ioobj *obj,
372 int niocount, struct niobuf_local *local,
376 struct obd_export *export;
377 OBD_CHECK_SETUP(conn, export);
378 OBD_CHECK_OP(export->export_obd,commitrw);
380 rc = OBP(export->export_obd, commitrw)(cmd, conn, objcount, obj, niocount,
381 local, desc_private);
385 static inline int obd_iocontrol(int cmd, struct obd_conn *conn,
386 int len, void *karg, void *uarg)
389 struct obd_export *export;
390 OBD_CHECK_SETUP(conn, export);
391 OBD_CHECK_OP(export->export_obd,iocontrol);
393 rc = OBP(export->export_obd, iocontrol)(cmd, conn, len, karg, uarg);
397 static inline int obd_enqueue(struct obd_conn *conn,
398 struct lustre_handle *parent_lock, __u64 *res_id,
399 __u32 type, void *cookie, int cookielen,
400 __u32 mode, int *flags, void *cb, void *data,
401 int datalen, struct lustre_handle *lockh)
404 struct obd_export *export;
405 OBD_CHECK_SETUP(conn, export);
406 OBD_CHECK_OP(export->export_obd,enqueue);
408 rc = OBP(export->export_obd, enqueue)(conn, parent_lock, res_id, type,
409 cookie, cookielen, mode, flags, cb,
410 data, datalen, lockh);
414 static inline int obd_cancel(struct obd_conn *conn, __u32 mode,
415 struct lustre_handle *lockh)
418 struct obd_export *export;
419 OBD_CHECK_SETUP(conn, export);
420 OBD_CHECK_OP(export->export_obd,cancel);
422 rc = OBP(export->export_obd, cancel)(conn, mode, lockh);
429 * ======== OBD Metadata Support ===========
432 extern int obd_init_caches(void);
433 extern void obd_cleanup_caches(void);
436 static inline int obdo_has_inline(struct obdo *obdo)
438 return (obdo->o_valid & OBD_MD_FLINLINE &&
439 obdo->o_obdflags & OBD_FL_INLINEDATA);
442 static inline int obdo_has_obdmd(struct obdo *obdo)
444 return (obdo->o_valid & OBD_MD_FLOBDMD &&
445 obdo->o_obdflags & OBD_FL_OBDMDEXISTS);
449 /* support routines */
450 extern kmem_cache_t *obdo_cachep;
452 static __inline__ struct obdo *obdo_alloc(void)
454 struct obdo *oa = NULL;
456 oa = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
459 memset(oa, 0, sizeof (*oa));
464 static __inline__ void obdo_free(struct obdo *oa)
468 kmem_cache_free(obdo_cachep, oa);
471 static __inline__ struct obdo *obdo_fromid(struct obd_conn *conn, obd_id id,
472 obd_mode mode, obd_flag valid)
480 RETURN(ERR_PTR(-ENOMEM));
486 if ((err = obd_getattr(conn, oa))) {
488 RETURN(ERR_PTR(err));
493 static inline void obdo_from_iattr(struct obdo *oa, struct iattr *attr)
495 unsigned int ia_valid = attr->ia_valid;
497 if (ia_valid & ATTR_ATIME) {
498 oa->o_atime = attr->ia_atime;
499 oa->o_valid |= OBD_MD_FLATIME;
501 if (ia_valid & ATTR_MTIME) {
502 oa->o_mtime = attr->ia_mtime;
503 oa->o_valid |= OBD_MD_FLMTIME;
505 if (ia_valid & ATTR_CTIME) {
506 oa->o_ctime = attr->ia_ctime;
507 oa->o_valid |= OBD_MD_FLCTIME;
509 if (ia_valid & ATTR_SIZE) {
510 oa->o_size = attr->ia_size;
511 oa->o_valid |= OBD_MD_FLSIZE;
513 if (ia_valid & ATTR_MODE) {
514 oa->o_mode = attr->ia_mode;
515 oa->o_valid |= OBD_MD_FLMODE;
516 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
517 oa->o_mode &= ~S_ISGID;
519 if (ia_valid & ATTR_UID)
521 oa->o_uid = attr->ia_uid;
522 oa->o_valid |= OBD_MD_FLUID;
524 if (ia_valid & ATTR_GID) {
525 oa->o_gid = attr->ia_gid;
526 oa->o_valid |= OBD_MD_FLGID;
531 static inline void iattr_from_obdo(struct iattr *attr, struct obdo *oa)
533 unsigned int ia_valid = oa->o_valid;
535 memset(attr, 0, sizeof(*attr));
536 if (ia_valid & OBD_MD_FLATIME) {
537 attr->ia_atime = oa->o_atime;
538 attr->ia_valid |= ATTR_ATIME;
540 if (ia_valid & OBD_MD_FLMTIME) {
541 attr->ia_mtime = oa->o_mtime;
542 attr->ia_valid |= ATTR_MTIME;
544 if (ia_valid & OBD_MD_FLCTIME) {
545 attr->ia_ctime = oa->o_ctime;
546 attr->ia_valid |= ATTR_CTIME;
548 if (ia_valid & OBD_MD_FLSIZE) {
549 attr->ia_size = oa->o_size;
550 attr->ia_valid |= ATTR_SIZE;
552 if (ia_valid & OBD_MD_FLMODE) {
553 attr->ia_mode = oa->o_mode;
554 attr->ia_valid |= ATTR_MODE;
555 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
556 attr->ia_mode &= ~S_ISGID;
558 if (ia_valid & OBD_MD_FLUID)
560 attr->ia_uid = oa->o_uid;
561 attr->ia_valid |= ATTR_UID;
563 if (ia_valid & OBD_MD_FLGID) {
564 attr->ia_gid = oa->o_gid;
565 attr->ia_valid |= ATTR_GID;
570 /* WARNING: the file systems must take care not to tinker with
571 attributes they don't manage (such as blocks). */
573 static __inline__ void obdo_from_inode(struct obdo *dst, struct inode *src)
575 if ( dst->o_valid & OBD_MD_FLID )
576 dst->o_id = src->i_ino;
577 if ( dst->o_valid & OBD_MD_FLATIME )
578 dst->o_atime = src->i_atime;
579 if ( dst->o_valid & OBD_MD_FLMTIME )
580 dst->o_mtime = src->i_mtime;
581 if ( dst->o_valid & OBD_MD_FLCTIME )
582 dst->o_ctime = src->i_ctime;
583 if ( dst->o_valid & OBD_MD_FLSIZE )
584 dst->o_size = src->i_size;
585 if ( dst->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
586 dst->o_blocks = src->i_blocks;
587 if ( dst->o_valid & OBD_MD_FLBLKSZ )
588 dst->o_blksize = src->i_blksize;
589 if ( dst->o_valid & OBD_MD_FLMODE )
590 dst->o_mode = src->i_mode;
591 if ( dst->o_valid & OBD_MD_FLUID )
592 dst->o_uid = src->i_uid;
593 if ( dst->o_valid & OBD_MD_FLGID )
594 dst->o_gid = src->i_gid;
595 if ( dst->o_valid & OBD_MD_FLFLAGS )
596 dst->o_flags = src->i_flags;
597 if ( dst->o_valid & OBD_MD_FLNLINK )
598 dst->o_nlink = src->i_nlink;
599 if ( dst->o_valid & OBD_MD_FLGENER )
600 dst->o_generation = src->i_generation;
603 static __inline__ void obdo_to_inode(struct inode *dst, struct obdo *src)
606 if ( src->o_valid & OBD_MD_FLID )
607 dst->i_ino = src->o_id;
608 if ( src->o_valid & OBD_MD_FLATIME )
609 dst->i_atime = src->o_atime;
610 if ( src->o_valid & OBD_MD_FLMTIME )
611 dst->i_mtime = src->o_mtime;
612 if ( src->o_valid & OBD_MD_FLCTIME )
613 dst->i_ctime = src->o_ctime;
614 if ( src->o_valid & OBD_MD_FLSIZE )
615 dst->i_size = src->o_size;
616 if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
617 dst->i_blocks = src->o_blocks;
618 if ( src->o_valid & OBD_MD_FLBLKSZ )
619 dst->i_blksize = src->o_blksize;
620 if ( src->o_valid & OBD_MD_FLMODE )
621 dst->i_mode = src->o_mode;
622 if ( src->o_valid & OBD_MD_FLUID )
623 dst->i_uid = src->o_uid;
624 if ( src->o_valid & OBD_MD_FLGID )
625 dst->i_gid = src->o_gid;
626 if ( src->o_valid & OBD_MD_FLFLAGS )
627 dst->i_flags = src->o_flags;
628 if ( src->o_valid & OBD_MD_FLNLINK )
629 dst->i_nlink = src->o_nlink;
630 if ( src->o_valid & OBD_MD_FLGENER )
631 dst->i_generation = src->o_generation;
636 static __inline__ void obdo_cpy_md(struct obdo *dst, struct obdo *src)
639 CDEBUG(D_INODE, "src obdo %Ld valid 0x%x, dst obdo %Ld\n",
640 (unsigned long long)src->o_id, src->o_valid,
641 (unsigned long long)dst->o_id);
643 if ( src->o_valid & OBD_MD_FLATIME )
644 dst->o_atime = src->o_atime;
645 if ( src->o_valid & OBD_MD_FLMTIME )
646 dst->o_mtime = src->o_mtime;
647 if ( src->o_valid & OBD_MD_FLCTIME )
648 dst->o_ctime = src->o_ctime;
649 if ( src->o_valid & OBD_MD_FLSIZE )
650 dst->o_size = src->o_size;
651 if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
652 dst->o_blocks = src->o_blocks;
653 if ( src->o_valid & OBD_MD_FLBLKSZ )
654 dst->o_blksize = src->o_blksize;
655 if ( src->o_valid & OBD_MD_FLMODE )
656 dst->o_mode = src->o_mode;
657 if ( src->o_valid & OBD_MD_FLUID )
658 dst->o_uid = src->o_uid;
659 if ( src->o_valid & OBD_MD_FLGID )
660 dst->o_gid = src->o_gid;
661 if ( src->o_valid & OBD_MD_FLFLAGS )
662 dst->o_flags = src->o_flags;
664 if ( src->o_valid & OBD_MD_FLOBDFLG )
665 dst->o_obdflags = src->o_obdflags;
667 if ( src->o_valid & OBD_MD_FLNLINK )
668 dst->o_nlink = src->o_nlink;
669 if ( src->o_valid & OBD_MD_FLGENER )
670 dst->o_generation = src->o_generation;
671 if ( src->o_valid & OBD_MD_FLINLINE &&
672 src->o_obdflags & OBD_FL_INLINEDATA) {
673 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
674 dst->o_obdflags |= OBD_FL_INLINEDATA;
676 if ( src->o_valid & OBD_MD_FLOBDMD &&
677 src->o_obdflags & OBD_FL_OBDMDEXISTS) {
678 memcpy(dst->o_obdmd, src->o_obdmd, sizeof(src->o_obdmd));
679 dst->o_obdflags |= OBD_FL_OBDMDEXISTS;
682 dst->o_valid |= src->o_valid;
686 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
687 static __inline__ int obdo_cmp_md(struct obdo *dst, struct obdo *src,
692 if ( compare & OBD_MD_FLATIME )
693 res = (res || (dst->o_atime != src->o_atime));
694 if ( compare & OBD_MD_FLMTIME )
695 res = (res || (dst->o_mtime != src->o_mtime));
696 if ( compare & OBD_MD_FLCTIME )
697 res = (res || (dst->o_ctime != src->o_ctime));
698 if ( compare & OBD_MD_FLSIZE )
699 res = (res || (dst->o_size != src->o_size));
700 if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
701 res = (res || (dst->o_blocks != src->o_blocks));
702 if ( compare & OBD_MD_FLBLKSZ )
703 res = (res || (dst->o_blksize != src->o_blksize));
704 if ( compare & OBD_MD_FLMODE )
705 res = (res || (dst->o_mode != src->o_mode));
706 if ( compare & OBD_MD_FLUID )
707 res = (res || (dst->o_uid != src->o_uid));
708 if ( compare & OBD_MD_FLGID )
709 res = (res || (dst->o_gid != src->o_gid));
710 if ( compare & OBD_MD_FLFLAGS )
711 res = (res || (dst->o_flags != src->o_flags));
712 if ( compare & OBD_MD_FLNLINK )
713 res = (res || (dst->o_nlink != src->o_nlink));
714 if ( compare & OBD_MD_FLGENER )
715 res = (res || (dst->o_generation != src->o_generation));
716 /* XXX Don't know if thses should be included here - wasn't previously
717 if ( compare & OBD_MD_FLINLINE )
718 res = (res || memcmp(dst->o_inline, src->o_inline));
719 if ( compare & OBD_MD_FLOBDMD )
720 res = (res || memcmp(dst->o_obdmd, src->o_obdmd));
727 int obd_register_type(struct obd_ops *ops, char *nm);
728 int obd_unregister_type(char *nm);
729 int obd_class_name2dev(char *name);
730 int obd_class_uuid2dev(char *name);
733 struct obd_prealloc_inode {
734 struct list_head obd_prealloc_chain;
738 /* generic operations shared by various OBD types */
739 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data);
740 int gen_multi_cleanup(struct obd_device *obddev);
741 int gen_multi_attach(struct obd_device *obddev, uint32_t len, void *data);
742 int gen_multi_detach(struct obd_device *obddev);
743 int gen_connect (struct obd_conn *conn, struct obd_device *obd);
744 int gen_disconnect(struct obd_conn *conn);
745 struct obd_export *gen_client(struct obd_conn *);
746 int gen_cleanup(struct obd_device *obddev);
747 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
748 struct obd_conn *src_conn, struct obdo *src,
749 obd_size count, obd_off offset);
754 extern void obd_sysctl_init (void);
755 extern void obd_sysctl_clean (void);
757 #endif /* __LINUX_CLASS_OBD_H */