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>
46 * ======== OBD Device Declarations ===========
48 #define MAX_OBD_DEVICES 128
49 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
51 #define OBD_ATTACHED 0x1
52 #define OBD_SET_UP 0x2
54 extern struct proc_dir_entry *
55 proc_lustre_register_obd_device(struct obd_device *obd);
56 extern void proc_lustre_release_obd_device(struct obd_device *obd);
57 extern void proc_lustre_remove_obd_entry(const char* name,
58 struct obd_device *obd);
61 * ======== OBD Operations Declarations ===========
65 extern struct obd_export *class_conn2export(struct lustre_handle *conn);
66 extern struct obd_device *class_conn2obd(struct lustre_handle *conn);
67 extern int class_rconn2export(struct lustre_handle *conn,
68 struct lustre_handle *rconn);
72 struct lustre_handle exp_rconnh; /* remote connection handle */
73 struct lustre_handle exp_impconnh;
74 struct list_head exp_chain;
75 struct obd_device *exp_obd;
76 struct ptlrpc_connection *exp_connection;
77 void *exp_data; /* device specific data */
84 struct lustre_handle imp_expconnh;
85 struct list_head imp_chain;
86 struct obd_device *imp_obd;
88 void *imp_data; /* device specific data */
91 static inline int obd_check_conn(struct lustre_handle *conn)
93 struct obd_device *obd;
95 CERROR("NULL conn\n");
98 obd = class_conn2obd(conn);
100 CERROR("NULL obd\n");
104 if (!obd->obd_flags & OBD_ATTACHED ) {
105 CERROR("obd %d not attached\n", obd->obd_minor);
109 if (!obd->obd_flags & OBD_SET_UP) {
110 CERROR("obd %d not setup\n", obd->obd_minor);
114 if (!obd->obd_type) {
115 CERROR("obd %d not typed\n", obd->obd_minor);
119 if (!obd->obd_type->typ_ops) {
120 CERROR("obd_check_conn: obd %d no operations\n",
128 #define OBT(dev) (dev)->obd_type
129 #define OBP(dev,op) (dev)->obd_type->typ_ops->o_ ## op
131 #define OBD_CHECK_SETUP(conn, export) \
134 CERROR("NULL connection\n"); \
138 export = class_conn2export(conn); \
140 CERROR("No export\n"); \
144 if (!((export)->exp_obd->obd_flags & OBD_SET_UP)) { \
145 CERROR("Device %d not setup\n", \
146 (export)->exp_obd->obd_minor); \
151 #define OBD_CHECK_DEVSETUP(obd) \
154 CERROR("NULL device\n"); \
158 if ( !((obd)->obd_flags & OBD_SET_UP) ) { \
159 CERROR("Device %d not setup\n", \
165 #define OBD_CHECK_OP(obd,op) \
167 if (!OBP((obd),op)) { \
168 CERROR("obd_" #op ": dev %d no operation\n", \
170 RETURN(-EOPNOTSUPP); \
174 static inline int obd_get_info(struct lustre_handle *conn, obd_count keylen,
175 void *key, obd_count *vallen, void **val)
178 struct obd_export *export;
179 OBD_CHECK_SETUP(conn, export);
180 OBD_CHECK_OP(export->exp_obd,get_info);
182 rc = OBP(export->exp_obd, get_info)(conn, keylen, key, vallen, val);
186 static inline int obd_set_info(struct lustre_handle *conn, obd_count keylen,
187 void *key, obd_count vallen, void *val)
190 struct obd_export *export;
191 OBD_CHECK_SETUP(conn, export);
192 OBD_CHECK_OP(export->exp_obd,set_info);
194 rc = OBP(export->exp_obd, set_info)(conn, keylen, key, vallen, val);
198 static inline int obd_setup(struct obd_device *obd, int datalen, void *data)
202 OBD_CHECK_OP(obd,setup);
204 rc = OBP(obd, setup)(obd, datalen, data);
208 static inline int obd_cleanup(struct obd_device *obd)
212 OBD_CHECK_DEVSETUP(obd);
213 OBD_CHECK_OP(obd,cleanup);
215 rc = OBP(obd, cleanup)(obd);
219 static inline int obd_create(struct lustre_handle *conn, struct obdo *obdo, struct lov_stripe_md **ea)
222 struct obd_export *export;
223 OBD_CHECK_SETUP(conn, export);
224 OBD_CHECK_OP(export->exp_obd,create);
226 rc = OBP(export->exp_obd, create)(conn, obdo, ea);
230 static inline int obd_destroy(struct lustre_handle *conn, struct obdo *obdo, struct lov_stripe_md *ea)
233 struct obd_export *export;
234 OBD_CHECK_SETUP(conn, export);
235 OBD_CHECK_OP(export->exp_obd,destroy);
237 rc = OBP(export->exp_obd, destroy)(conn, obdo, ea);
241 static inline int obd_getattr(struct lustre_handle *conn,
243 struct lov_stripe_md *ea)
246 struct obd_export *export;
247 OBD_CHECK_SETUP(conn, export);
248 OBD_CHECK_OP(export->exp_obd,getattr);
250 rc = OBP(export->exp_obd, getattr)(conn, obdo, ea);
254 static inline int obd_close(struct lustre_handle *conn, struct obdo *obdo, struct lov_stripe_md *md)
257 struct obd_export *export;
258 OBD_CHECK_SETUP(conn, export);
259 OBD_CHECK_OP(export->exp_obd,close);
261 rc = OBP(export->exp_obd, close)(conn, obdo, md);
264 static inline int obd_open(struct lustre_handle *conn, struct obdo *obdo,
265 struct lov_stripe_md *md)
268 struct obd_export *export;
269 OBD_CHECK_SETUP(conn, export);
270 OBD_CHECK_OP(export->exp_obd,open);
272 rc = OBP(export->exp_obd, open) (conn, obdo, md);
276 static inline int obd_setattr(struct lustre_handle *conn,
278 struct lov_stripe_md *ea)
281 struct obd_export *export;
282 OBD_CHECK_SETUP(conn, export);
283 OBD_CHECK_OP(export->exp_obd,setattr);
285 rc = OBP(export->exp_obd, setattr)(conn, obdo, ea);
289 static inline int obd_connect(struct lustre_handle *conn, struct obd_device *obd)
292 OBD_CHECK_DEVSETUP(obd);
293 OBD_CHECK_OP(obd,connect);
295 rc = OBP(obd, connect)(conn, obd);
299 static inline int obd_disconnect(struct lustre_handle *conn)
302 struct obd_export *export;
303 OBD_CHECK_SETUP(conn, export);
304 OBD_CHECK_OP(export->exp_obd,disconnect);
306 rc = OBP(export->exp_obd, disconnect)(conn);
310 static inline int obd_statfs(struct lustre_handle *conn, struct statfs *buf)
313 struct obd_export *export;
314 OBD_CHECK_SETUP(conn, export);
315 OBD_CHECK_OP(export->exp_obd,statfs);
317 rc = OBP(export->exp_obd, statfs)(conn, buf);
321 static inline int obd_punch(struct lustre_handle *conn, struct obdo *tgt,
322 struct lov_stripe_md *md,
323 obd_size count, obd_off offset)
326 struct obd_export *export;
327 OBD_CHECK_SETUP(conn, export);
328 OBD_CHECK_OP(export->exp_obd,punch);
330 rc = OBP(export->exp_obd, punch)(conn, tgt, md, count, offset);
334 static inline int obd_brw(int cmd, struct lustre_handle *conn,
335 struct lov_stripe_md *md,
341 brw_callback_t callback, void *data)
344 struct obd_export *export;
345 OBD_CHECK_SETUP(conn, export);
346 OBD_CHECK_OP(export->exp_obd,brw);
348 if (!(cmd & OBD_BRW_RWMASK)) {
349 CERROR("obd_brw: cmd must be OBD_BRW_READ or OBD_BRW_WRITE\n");
353 rc = OBP(export->exp_obd, brw)(cmd, conn, md, oa_bufs, buf,
354 count, offset, flags, callback, data);
358 static inline int obd_preprw(int cmd, struct lustre_handle *conn,
359 int objcount, struct obd_ioobj *obj,
360 int niocount, struct niobuf_remote *remote,
361 struct niobuf_local *local, void **desc_private)
364 struct obd_export *export;
365 OBD_CHECK_SETUP(conn, export);
366 OBD_CHECK_OP(export->exp_obd,preprw);
368 rc = OBP(export->exp_obd, preprw)(cmd, conn, objcount, obj, niocount,
369 remote, local, desc_private);
373 static inline int obd_commitrw(int cmd, struct lustre_handle *conn,
374 int objcount, struct obd_ioobj *obj,
375 int niocount, struct niobuf_local *local,
379 struct obd_export *export;
380 OBD_CHECK_SETUP(conn, export);
381 OBD_CHECK_OP(export->exp_obd,commitrw);
383 rc = OBP(export->exp_obd, commitrw)(cmd, conn, objcount, obj, niocount,
384 local, desc_private);
388 static inline int obd_iocontrol(int cmd, struct lustre_handle *conn,
389 int len, void *karg, void *uarg)
392 struct obd_export *export;
393 OBD_CHECK_SETUP(conn, export);
394 OBD_CHECK_OP(export->exp_obd,iocontrol);
396 rc = OBP(export->exp_obd, iocontrol)(cmd, conn, len, karg, uarg);
400 static inline int obd_enqueue(struct lustre_handle *conn,
401 struct lustre_handle *parent_lock, __u64 *res_id,
402 __u32 type, void *cookie, int cookielen,
403 __u32 mode, int *flags, void *cb, void *data,
404 int datalen, struct lustre_handle *lockh)
407 struct obd_export *export;
408 OBD_CHECK_SETUP(conn, export);
409 OBD_CHECK_OP(export->exp_obd,enqueue);
411 rc = OBP(export->exp_obd, enqueue)(conn, parent_lock, res_id, type,
412 cookie, cookielen, mode, flags, cb,
413 data, datalen, lockh);
417 static inline int obd_cancel(struct lustre_handle *conn, __u32 mode,
418 struct lustre_handle *lockh)
421 struct obd_export *export;
422 OBD_CHECK_SETUP(conn, export);
423 OBD_CHECK_OP(export->exp_obd,cancel);
425 rc = OBP(export->exp_obd, cancel)(conn, mode, lockh);
432 * ======== OBD Metadata Support ===========
435 extern int obd_init_caches(void);
436 extern void obd_cleanup_caches(void);
438 static inline int obdo_has_inline(struct obdo *obdo)
440 return (obdo->o_valid & OBD_MD_FLINLINE &&
441 obdo->o_obdflags & OBD_FL_INLINEDATA);
445 /* support routines */
446 extern kmem_cache_t *obdo_cachep;
447 static inline struct obdo *obdo_alloc(void)
449 struct obdo *oa = NULL;
451 oa = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
454 memset(oa, 0, sizeof (*oa));
458 static inline void obdo_free(struct obdo *oa)
462 kmem_cache_free(obdo_cachep, oa);
466 static inline void obdo_from_iattr(struct obdo *oa, struct iattr *attr)
468 unsigned int ia_valid = attr->ia_valid;
470 if (ia_valid & ATTR_ATIME) {
471 oa->o_atime = attr->ia_atime;
472 oa->o_valid |= OBD_MD_FLATIME;
474 if (ia_valid & ATTR_MTIME) {
475 oa->o_mtime = attr->ia_mtime;
476 oa->o_valid |= OBD_MD_FLMTIME;
478 if (ia_valid & ATTR_CTIME) {
479 oa->o_ctime = attr->ia_ctime;
480 oa->o_valid |= OBD_MD_FLCTIME;
482 if (ia_valid & ATTR_SIZE) {
483 oa->o_size = attr->ia_size;
484 oa->o_valid |= OBD_MD_FLSIZE;
486 if (ia_valid & ATTR_MODE) {
487 oa->o_mode = attr->ia_mode;
488 oa->o_valid |= OBD_MD_FLMODE;
489 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
490 oa->o_mode &= ~S_ISGID;
492 if (ia_valid & ATTR_UID)
494 oa->o_uid = attr->ia_uid;
495 oa->o_valid |= OBD_MD_FLUID;
497 if (ia_valid & ATTR_GID) {
498 oa->o_gid = attr->ia_gid;
499 oa->o_valid |= OBD_MD_FLGID;
504 static inline void iattr_from_obdo(struct iattr *attr, struct obdo *oa)
506 unsigned int ia_valid = oa->o_valid;
508 memset(attr, 0, sizeof(*attr));
509 if (ia_valid & OBD_MD_FLATIME) {
510 attr->ia_atime = oa->o_atime;
511 attr->ia_valid |= ATTR_ATIME;
513 if (ia_valid & OBD_MD_FLMTIME) {
514 attr->ia_mtime = oa->o_mtime;
515 attr->ia_valid |= ATTR_MTIME;
517 if (ia_valid & OBD_MD_FLCTIME) {
518 attr->ia_ctime = oa->o_ctime;
519 attr->ia_valid |= ATTR_CTIME;
521 if (ia_valid & OBD_MD_FLSIZE) {
522 attr->ia_size = oa->o_size;
523 attr->ia_valid |= ATTR_SIZE;
525 if (ia_valid & OBD_MD_FLMODE) {
526 attr->ia_mode = oa->o_mode;
527 attr->ia_valid |= ATTR_MODE;
528 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
529 attr->ia_mode &= ~S_ISGID;
531 if (ia_valid & OBD_MD_FLUID)
533 attr->ia_uid = oa->o_uid;
534 attr->ia_valid |= ATTR_UID;
536 if (ia_valid & OBD_MD_FLGID) {
537 attr->ia_gid = oa->o_gid;
538 attr->ia_valid |= ATTR_GID;
543 /* WARNING: the file systems must take care not to tinker with
544 attributes they don't manage (such as blocks). */
546 static inline void obdo_from_inode(struct obdo *dst, struct inode *src)
548 if ( dst->o_valid & OBD_MD_FLID )
549 dst->o_id = src->i_ino;
550 if ( dst->o_valid & OBD_MD_FLATIME )
551 dst->o_atime = src->i_atime;
552 if ( dst->o_valid & OBD_MD_FLMTIME )
553 dst->o_mtime = src->i_mtime;
554 if ( dst->o_valid & OBD_MD_FLCTIME )
555 dst->o_ctime = src->i_ctime;
556 if ( dst->o_valid & OBD_MD_FLSIZE )
557 dst->o_size = src->i_size;
558 if ( dst->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
559 dst->o_blocks = src->i_blocks;
560 if ( dst->o_valid & OBD_MD_FLBLKSZ )
561 dst->o_blksize = src->i_blksize;
562 if ( dst->o_valid & OBD_MD_FLMODE )
563 dst->o_mode = src->i_mode;
564 if ( dst->o_valid & OBD_MD_FLUID )
565 dst->o_uid = src->i_uid;
566 if ( dst->o_valid & OBD_MD_FLGID )
567 dst->o_gid = src->i_gid;
568 if ( dst->o_valid & OBD_MD_FLFLAGS )
569 dst->o_flags = src->i_flags;
570 if ( dst->o_valid & OBD_MD_FLNLINK )
571 dst->o_nlink = src->i_nlink;
572 if ( dst->o_valid & OBD_MD_FLGENER )
573 dst->o_generation = src->i_generation;
574 if ( dst->o_valid & OBD_MD_FLRDEV )
575 dst->o_rdev = src->i_rdev;
578 static inline void obdo_to_inode(struct inode *dst, struct obdo *src)
581 if ( src->o_valid & OBD_MD_FLID )
582 dst->i_ino = src->o_id;
583 if ( src->o_valid & OBD_MD_FLATIME )
584 dst->i_atime = src->o_atime;
585 if ( src->o_valid & OBD_MD_FLMTIME )
586 dst->i_mtime = src->o_mtime;
587 if ( src->o_valid & OBD_MD_FLCTIME )
588 dst->i_ctime = src->o_ctime;
589 if ( src->o_valid & OBD_MD_FLSIZE )
590 dst->i_size = src->o_size;
591 if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
592 dst->i_blocks = src->o_blocks;
593 if ( src->o_valid & OBD_MD_FLBLKSZ )
594 dst->i_blksize = src->o_blksize;
595 if ( src->o_valid & OBD_MD_FLMODE )
596 dst->i_mode = src->o_mode;
597 if ( src->o_valid & OBD_MD_FLUID )
598 dst->i_uid = src->o_uid;
599 if ( src->o_valid & OBD_MD_FLGID )
600 dst->i_gid = src->o_gid;
601 if ( src->o_valid & OBD_MD_FLFLAGS )
602 dst->i_flags = src->o_flags;
603 if ( src->o_valid & OBD_MD_FLNLINK )
604 dst->i_nlink = src->o_nlink;
605 if ( src->o_valid & OBD_MD_FLGENER )
606 dst->i_generation = src->o_generation;
607 if ( src->o_valid & OBD_MD_FLRDEV )
608 dst->i_rdev = src->o_rdev;
613 static inline void obdo_cpy_md(struct obdo *dst, struct obdo *src)
616 CDEBUG(D_INODE, "src obdo %Ld valid 0x%x, dst obdo %Ld\n",
617 (unsigned long long)src->o_id, src->o_valid,
618 (unsigned long long)dst->o_id);
620 if ( src->o_valid & OBD_MD_FLATIME )
621 dst->o_atime = src->o_atime;
622 if ( src->o_valid & OBD_MD_FLMTIME )
623 dst->o_mtime = src->o_mtime;
624 if ( src->o_valid & OBD_MD_FLCTIME )
625 dst->o_ctime = src->o_ctime;
626 if ( src->o_valid & OBD_MD_FLSIZE )
627 dst->o_size = src->o_size;
628 if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
629 dst->o_blocks = src->o_blocks;
630 if ( src->o_valid & OBD_MD_FLBLKSZ )
631 dst->o_blksize = src->o_blksize;
632 if ( src->o_valid & OBD_MD_FLMODE )
633 dst->o_mode = src->o_mode;
634 if ( src->o_valid & OBD_MD_FLUID )
635 dst->o_uid = src->o_uid;
636 if ( src->o_valid & OBD_MD_FLGID )
637 dst->o_gid = src->o_gid;
638 if ( src->o_valid & OBD_MD_FLFLAGS )
639 dst->o_flags = src->o_flags;
641 if ( src->o_valid & OBD_MD_FLOBDFLG )
642 dst->o_obdflags = src->o_obdflags;
644 if ( src->o_valid & OBD_MD_FLNLINK )
645 dst->o_nlink = src->o_nlink;
646 if ( src->o_valid & OBD_MD_FLGENER )
647 dst->o_generation = src->o_generation;
648 if ( src->o_valid & OBD_MD_FLRDEV )
649 dst->o_rdev = src->o_rdev;
650 if ( src->o_valid & OBD_MD_FLINLINE &&
651 src->o_obdflags & OBD_FL_INLINEDATA) {
652 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
653 dst->o_obdflags |= OBD_FL_INLINEDATA;
656 dst->o_valid |= src->o_valid;
660 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
661 static inline int obdo_cmp_md(struct obdo *dst, struct obdo *src,
666 if ( compare & OBD_MD_FLATIME )
667 res = (res || (dst->o_atime != src->o_atime));
668 if ( compare & OBD_MD_FLMTIME )
669 res = (res || (dst->o_mtime != src->o_mtime));
670 if ( compare & OBD_MD_FLCTIME )
671 res = (res || (dst->o_ctime != src->o_ctime));
672 if ( compare & OBD_MD_FLSIZE )
673 res = (res || (dst->o_size != src->o_size));
674 if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
675 res = (res || (dst->o_blocks != src->o_blocks));
676 if ( compare & OBD_MD_FLBLKSZ )
677 res = (res || (dst->o_blksize != src->o_blksize));
678 if ( compare & OBD_MD_FLMODE )
679 res = (res || (dst->o_mode != src->o_mode));
680 if ( compare & OBD_MD_FLUID )
681 res = (res || (dst->o_uid != src->o_uid));
682 if ( compare & OBD_MD_FLGID )
683 res = (res || (dst->o_gid != src->o_gid));
684 if ( compare & OBD_MD_FLFLAGS )
685 res = (res || (dst->o_flags != src->o_flags));
686 if ( compare & OBD_MD_FLNLINK )
687 res = (res || (dst->o_nlink != src->o_nlink));
688 if ( compare & OBD_MD_FLGENER )
689 res = (res || (dst->o_generation != src->o_generation));
690 /* XXX Don't know if thses should be included here - wasn't previously
691 if ( compare & OBD_MD_FLINLINE )
692 res = (res || memcmp(dst->o_inline, src->o_inline));
699 int class_register_type(struct obd_ops *ops, char *nm);
700 int class_unregister_type(char *nm);
701 int class_name2dev(char *name);
702 int class_uuid2dev(char *name);
703 struct obd_device *class_uuid2obd(char *name);
704 int class_connect (struct lustre_handle *conn, struct obd_device *obd);
705 int class_disconnect(struct lustre_handle *conn);
706 struct obd_export *class_conn2export(struct lustre_handle *);
708 /* generic operations shared by various OBD types */
709 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data);
710 int class_multi_cleanup(struct obd_device *obddev);
716 extern void obd_sysctl_init (void);
717 extern void obd_sysctl_clean (void);
719 #endif /* __LINUX_CLASS_OBD_H */