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_import2export(struct lustre_handle *conn,
68 struct lustre_handle *imp);
72 struct lustre_handle export_import; /* client handle */
73 struct list_head export_chain;
74 struct obd_device *export_obd;
75 struct ptlrpc_connection *export_connection;
76 void *export_data; /* device specific data */
83 struct lustre_handle import_export; /* server handle */
84 struct list_head import_chain;
85 struct obd_device *import_obd;
86 unsigned int import_id;
87 void *import_data; /* device specific data */
90 static inline int obd_check_conn(struct lustre_handle *conn)
92 struct obd_device *obd;
94 CERROR("NULL conn\n");
97 obd = class_conn2obd(conn);
103 if (!obd->obd_flags & OBD_ATTACHED ) {
104 CERROR("obd %d not attached\n", obd->obd_minor);
108 if (!obd->obd_flags & OBD_SET_UP) {
109 CERROR("obd %d not setup\n", obd->obd_minor);
113 if (!obd->obd_type) {
114 CERROR("obd %d not typed\n", obd->obd_minor);
118 if (!obd->obd_type->typ_ops) {
119 CERROR("obd_check_conn: obd %d no operations\n",
127 #define OBT(dev) (dev)->obd_type
128 #define OBP(dev,op) (dev)->obd_type->typ_ops->o_ ## op
130 #define OBD_CHECK_SETUP(conn, export) \
133 CERROR("NULL connection\n"); \
137 export = class_conn2export(conn);\
139 CERROR("No export\n"); \
143 if ( !((export)->export_obd->obd_flags & OBD_SET_UP) ) { \
144 CERROR("Device %d not setup\n", \
145 (export)->export_obd->obd_minor); \
150 #define OBD_CHECK_DEVSETUP(obd) \
153 CERROR("NULL device\n"); \
157 if ( !((obd)->obd_flags & OBD_SET_UP) ) { \
158 CERROR("Device %d not setup\n", \
164 #define OBD_CHECK_OP(obd,op) \
166 if (!OBP((obd),op)) { \
167 CERROR("obd_" #op ": dev %d no operation\n", \
169 RETURN(-EOPNOTSUPP); \
173 static inline int obd_get_info(struct lustre_handle *conn, obd_count keylen,
174 void *key, obd_count *vallen, void **val)
177 struct obd_export *export;
178 OBD_CHECK_SETUP(conn, export);
179 OBD_CHECK_OP(export->export_obd,get_info);
181 rc = OBP(export->export_obd, get_info)(conn, keylen, key, vallen, val);
185 static inline int obd_set_info(struct lustre_handle *conn, obd_count keylen,
186 void *key, obd_count vallen, void *val)
189 struct obd_export *export;
190 OBD_CHECK_SETUP(conn, export);
191 OBD_CHECK_OP(export->export_obd,set_info);
193 rc = OBP(export->export_obd, set_info)(conn, keylen, key, vallen, val);
197 static inline int obd_setup(struct obd_device *obd, int datalen, void *data)
201 OBD_CHECK_OP(obd,setup);
203 rc = OBP(obd, setup)(obd, datalen, data);
207 static inline int obd_cleanup(struct obd_device *obd)
211 OBD_CHECK_DEVSETUP(obd);
212 OBD_CHECK_OP(obd,cleanup);
214 rc = OBP(obd, cleanup)(obd);
218 static inline int obd_create(struct lustre_handle *conn, struct obdo *obdo, struct lov_stripe_md **ea)
221 struct obd_export *export;
222 OBD_CHECK_SETUP(conn, export);
223 OBD_CHECK_OP(export->export_obd,create);
225 rc = OBP(export->export_obd, create)(conn, obdo, ea);
229 static inline int obd_destroy(struct lustre_handle *conn, struct obdo *obdo, struct lov_stripe_md *ea)
232 struct obd_export *export;
233 OBD_CHECK_SETUP(conn, export);
234 OBD_CHECK_OP(export->export_obd,destroy);
236 rc = OBP(export->export_obd, destroy)(conn, obdo, ea);
240 static inline int obd_getattr(struct lustre_handle *conn, struct obdo *obdo)
243 struct obd_export *export;
244 OBD_CHECK_SETUP(conn, export);
245 OBD_CHECK_OP(export->export_obd,getattr);
247 rc = OBP(export->export_obd, getattr)(conn, obdo);
251 static inline int obd_close(struct lustre_handle *conn, struct obdo *obdo, struct lov_stripe_md *md)
254 struct obd_export *export;
255 OBD_CHECK_SETUP(conn, export);
256 OBD_CHECK_OP(export->export_obd,close);
258 rc = OBP(export->export_obd, close)(conn, obdo, md);
261 static inline int obd_open(struct lustre_handle *conn, struct obdo *obdo,
262 struct lov_stripe_md *md)
265 struct obd_export *export;
266 OBD_CHECK_SETUP(conn, export);
267 OBD_CHECK_OP(export->export_obd,open);
269 rc = OBP(export->export_obd, open) (conn, obdo, md);
273 static inline int obd_setattr(struct lustre_handle *conn, struct obdo *obdo)
276 struct obd_export *export;
277 OBD_CHECK_SETUP(conn, export);
278 OBD_CHECK_OP(export->export_obd,setattr);
280 rc = OBP(export->export_obd, setattr)(conn, obdo);
284 static inline int obd_connect(struct lustre_handle *conn, struct obd_device *obd)
287 OBD_CHECK_DEVSETUP(obd);
288 OBD_CHECK_OP(obd,connect);
290 rc = OBP(obd, connect)(conn, obd);
294 static inline int obd_disconnect(struct lustre_handle *conn)
297 struct obd_export *export;
298 OBD_CHECK_SETUP(conn, export);
299 OBD_CHECK_OP(export->export_obd,disconnect);
301 rc = OBP(export->export_obd, disconnect)(conn);
305 static inline int obd_statfs(struct lustre_handle *conn, struct statfs *buf)
308 struct obd_export *export;
309 OBD_CHECK_SETUP(conn, export);
310 OBD_CHECK_OP(export->export_obd,statfs);
312 rc = OBP(export->export_obd, statfs)(conn, buf);
316 static inline int obd_punch(struct lustre_handle *conn, struct obdo *tgt,
317 struct lov_stripe_md *md,
318 obd_size count, obd_off offset)
321 struct obd_export *export;
322 OBD_CHECK_SETUP(conn, export);
323 OBD_CHECK_OP(export->export_obd,punch);
325 rc = OBP(export->export_obd, punch)(conn, tgt, md, count, offset);
329 static inline int obd_brw(int cmd, struct lustre_handle *conn,
330 struct lov_stripe_md *md,
339 struct obd_export *export;
340 OBD_CHECK_SETUP(conn, export);
341 OBD_CHECK_OP(export->export_obd,brw);
343 if (!(cmd & OBD_BRW_RWMASK)) {
344 CERROR("obd_brw: cmd must be OBD_BRW_READ or OBD_BRW_WRITE\n");
348 rc = OBP(export->export_obd, brw)(cmd, conn, md, oa_bufs, buf,
349 count, offset, flags, callback);
353 static inline int obd_preprw(int cmd, struct lustre_handle *conn,
354 int objcount, struct obd_ioobj *obj,
355 int niocount, struct niobuf_remote *remote,
356 struct niobuf_local *local, void **desc_private)
359 struct obd_export *export;
360 OBD_CHECK_SETUP(conn, export);
361 OBD_CHECK_OP(export->export_obd,preprw);
363 rc = OBP(export->export_obd, preprw)(cmd, conn, objcount, obj, niocount,
364 remote, local, desc_private);
368 static inline int obd_commitrw(int cmd, struct lustre_handle *conn,
369 int objcount, struct obd_ioobj *obj,
370 int niocount, struct niobuf_local *local,
374 struct obd_export *export;
375 OBD_CHECK_SETUP(conn, export);
376 OBD_CHECK_OP(export->export_obd,commitrw);
378 rc = OBP(export->export_obd, commitrw)(cmd, conn, objcount, obj, niocount,
379 local, desc_private);
383 static inline int obd_iocontrol(int cmd, struct lustre_handle *conn,
384 int len, void *karg, void *uarg)
387 struct obd_export *export;
388 OBD_CHECK_SETUP(conn, export);
389 OBD_CHECK_OP(export->export_obd,iocontrol);
391 rc = OBP(export->export_obd, iocontrol)(cmd, conn, len, karg, uarg);
395 static inline int obd_enqueue(struct lustre_handle *conn,
396 struct lustre_handle *parent_lock, __u64 *res_id,
397 __u32 type, void *cookie, int cookielen,
398 __u32 mode, int *flags, void *cb, void *data,
399 int datalen, struct lustre_handle *lockh)
402 struct obd_export *export;
403 OBD_CHECK_SETUP(conn, export);
404 OBD_CHECK_OP(export->export_obd,enqueue);
406 rc = OBP(export->export_obd, enqueue)(conn, parent_lock, res_id, type,
407 cookie, cookielen, mode, flags, cb,
408 data, datalen, lockh);
412 static inline int obd_cancel(struct lustre_handle *conn, __u32 mode,
413 struct lustre_handle *lockh)
416 struct obd_export *export;
417 OBD_CHECK_SETUP(conn, export);
418 OBD_CHECK_OP(export->export_obd,cancel);
420 rc = OBP(export->export_obd, cancel)(conn, mode, lockh);
427 * ======== OBD Metadata Support ===========
430 extern int obd_init_caches(void);
431 extern void obd_cleanup_caches(void);
433 static inline int obdo_has_inline(struct obdo *obdo)
435 return (obdo->o_valid & OBD_MD_FLINLINE &&
436 obdo->o_obdflags & OBD_FL_INLINEDATA);
440 /* support routines */
441 extern kmem_cache_t *obdo_cachep;
442 static inline struct obdo *obdo_alloc(void)
444 struct obdo *oa = NULL;
446 oa = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
449 memset(oa, 0, sizeof (*oa));
453 static inline void obdo_free(struct obdo *oa)
457 kmem_cache_free(obdo_cachep, oa);
461 static inline void obdo_from_iattr(struct obdo *oa, struct iattr *attr)
463 unsigned int ia_valid = attr->ia_valid;
465 if (ia_valid & ATTR_ATIME) {
466 oa->o_atime = attr->ia_atime;
467 oa->o_valid |= OBD_MD_FLATIME;
469 if (ia_valid & ATTR_MTIME) {
470 oa->o_mtime = attr->ia_mtime;
471 oa->o_valid |= OBD_MD_FLMTIME;
473 if (ia_valid & ATTR_CTIME) {
474 oa->o_ctime = attr->ia_ctime;
475 oa->o_valid |= OBD_MD_FLCTIME;
477 if (ia_valid & ATTR_SIZE) {
478 oa->o_size = attr->ia_size;
479 oa->o_valid |= OBD_MD_FLSIZE;
481 if (ia_valid & ATTR_MODE) {
482 oa->o_mode = attr->ia_mode;
483 oa->o_valid |= OBD_MD_FLMODE;
484 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
485 oa->o_mode &= ~S_ISGID;
487 if (ia_valid & ATTR_UID)
489 oa->o_uid = attr->ia_uid;
490 oa->o_valid |= OBD_MD_FLUID;
492 if (ia_valid & ATTR_GID) {
493 oa->o_gid = attr->ia_gid;
494 oa->o_valid |= OBD_MD_FLGID;
499 static inline void iattr_from_obdo(struct iattr *attr, struct obdo *oa)
501 unsigned int ia_valid = oa->o_valid;
503 memset(attr, 0, sizeof(*attr));
504 if (ia_valid & OBD_MD_FLATIME) {
505 attr->ia_atime = oa->o_atime;
506 attr->ia_valid |= ATTR_ATIME;
508 if (ia_valid & OBD_MD_FLMTIME) {
509 attr->ia_mtime = oa->o_mtime;
510 attr->ia_valid |= ATTR_MTIME;
512 if (ia_valid & OBD_MD_FLCTIME) {
513 attr->ia_ctime = oa->o_ctime;
514 attr->ia_valid |= ATTR_CTIME;
516 if (ia_valid & OBD_MD_FLSIZE) {
517 attr->ia_size = oa->o_size;
518 attr->ia_valid |= ATTR_SIZE;
520 if (ia_valid & OBD_MD_FLMODE) {
521 attr->ia_mode = oa->o_mode;
522 attr->ia_valid |= ATTR_MODE;
523 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
524 attr->ia_mode &= ~S_ISGID;
526 if (ia_valid & OBD_MD_FLUID)
528 attr->ia_uid = oa->o_uid;
529 attr->ia_valid |= ATTR_UID;
531 if (ia_valid & OBD_MD_FLGID) {
532 attr->ia_gid = oa->o_gid;
533 attr->ia_valid |= ATTR_GID;
538 /* WARNING: the file systems must take care not to tinker with
539 attributes they don't manage (such as blocks). */
541 static inline void obdo_from_inode(struct obdo *dst, struct inode *src)
543 if ( dst->o_valid & OBD_MD_FLID )
544 dst->o_id = src->i_ino;
545 if ( dst->o_valid & OBD_MD_FLATIME )
546 dst->o_atime = src->i_atime;
547 if ( dst->o_valid & OBD_MD_FLMTIME )
548 dst->o_mtime = src->i_mtime;
549 if ( dst->o_valid & OBD_MD_FLCTIME )
550 dst->o_ctime = src->i_ctime;
551 if ( dst->o_valid & OBD_MD_FLSIZE )
552 dst->o_size = src->i_size;
553 if ( dst->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
554 dst->o_blocks = src->i_blocks;
555 if ( dst->o_valid & OBD_MD_FLBLKSZ )
556 dst->o_blksize = src->i_blksize;
557 if ( dst->o_valid & OBD_MD_FLMODE )
558 dst->o_mode = src->i_mode;
559 if ( dst->o_valid & OBD_MD_FLUID )
560 dst->o_uid = src->i_uid;
561 if ( dst->o_valid & OBD_MD_FLGID )
562 dst->o_gid = src->i_gid;
563 if ( dst->o_valid & OBD_MD_FLFLAGS )
564 dst->o_flags = src->i_flags;
565 if ( dst->o_valid & OBD_MD_FLNLINK )
566 dst->o_nlink = src->i_nlink;
567 if ( dst->o_valid & OBD_MD_FLGENER )
568 dst->o_generation = src->i_generation;
569 if ( dst->o_valid & OBD_MD_FLRDEV )
570 dst->o_rdev = src->i_rdev;
573 static inline void obdo_to_inode(struct inode *dst, struct obdo *src)
576 if ( src->o_valid & OBD_MD_FLID )
577 dst->i_ino = src->o_id;
578 if ( src->o_valid & OBD_MD_FLATIME )
579 dst->i_atime = src->o_atime;
580 if ( src->o_valid & OBD_MD_FLMTIME )
581 dst->i_mtime = src->o_mtime;
582 if ( src->o_valid & OBD_MD_FLCTIME )
583 dst->i_ctime = src->o_ctime;
584 if ( src->o_valid & OBD_MD_FLSIZE )
585 dst->i_size = src->o_size;
586 if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
587 dst->i_blocks = src->o_blocks;
588 if ( src->o_valid & OBD_MD_FLBLKSZ )
589 dst->i_blksize = src->o_blksize;
590 if ( src->o_valid & OBD_MD_FLMODE )
591 dst->i_mode = src->o_mode;
592 if ( src->o_valid & OBD_MD_FLUID )
593 dst->i_uid = src->o_uid;
594 if ( src->o_valid & OBD_MD_FLGID )
595 dst->i_gid = src->o_gid;
596 if ( src->o_valid & OBD_MD_FLFLAGS )
597 dst->i_flags = src->o_flags;
598 if ( src->o_valid & OBD_MD_FLNLINK )
599 dst->i_nlink = src->o_nlink;
600 if ( src->o_valid & OBD_MD_FLGENER )
601 dst->i_generation = src->o_generation;
602 if ( src->o_valid & OBD_MD_FLRDEV )
603 dst->i_rdev = src->o_rdev;
608 static inline void obdo_cpy_md(struct obdo *dst, struct obdo *src)
611 CDEBUG(D_INODE, "src obdo %Ld valid 0x%x, dst obdo %Ld\n",
612 (unsigned long long)src->o_id, src->o_valid,
613 (unsigned long long)dst->o_id);
615 if ( src->o_valid & OBD_MD_FLATIME )
616 dst->o_atime = src->o_atime;
617 if ( src->o_valid & OBD_MD_FLMTIME )
618 dst->o_mtime = src->o_mtime;
619 if ( src->o_valid & OBD_MD_FLCTIME )
620 dst->o_ctime = src->o_ctime;
621 if ( src->o_valid & OBD_MD_FLSIZE )
622 dst->o_size = src->o_size;
623 if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
624 dst->o_blocks = src->o_blocks;
625 if ( src->o_valid & OBD_MD_FLBLKSZ )
626 dst->o_blksize = src->o_blksize;
627 if ( src->o_valid & OBD_MD_FLMODE )
628 dst->o_mode = src->o_mode;
629 if ( src->o_valid & OBD_MD_FLUID )
630 dst->o_uid = src->o_uid;
631 if ( src->o_valid & OBD_MD_FLGID )
632 dst->o_gid = src->o_gid;
633 if ( src->o_valid & OBD_MD_FLFLAGS )
634 dst->o_flags = src->o_flags;
636 if ( src->o_valid & OBD_MD_FLOBDFLG )
637 dst->o_obdflags = src->o_obdflags;
639 if ( src->o_valid & OBD_MD_FLNLINK )
640 dst->o_nlink = src->o_nlink;
641 if ( src->o_valid & OBD_MD_FLGENER )
642 dst->o_generation = src->o_generation;
643 if ( src->o_valid & OBD_MD_FLRDEV )
644 dst->o_rdev = src->o_rdev;
645 if ( src->o_valid & OBD_MD_FLINLINE &&
646 src->o_obdflags & OBD_FL_INLINEDATA) {
647 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
648 dst->o_obdflags |= OBD_FL_INLINEDATA;
651 dst->o_valid |= src->o_valid;
655 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
656 static inline int obdo_cmp_md(struct obdo *dst, struct obdo *src,
661 if ( compare & OBD_MD_FLATIME )
662 res = (res || (dst->o_atime != src->o_atime));
663 if ( compare & OBD_MD_FLMTIME )
664 res = (res || (dst->o_mtime != src->o_mtime));
665 if ( compare & OBD_MD_FLCTIME )
666 res = (res || (dst->o_ctime != src->o_ctime));
667 if ( compare & OBD_MD_FLSIZE )
668 res = (res || (dst->o_size != src->o_size));
669 if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
670 res = (res || (dst->o_blocks != src->o_blocks));
671 if ( compare & OBD_MD_FLBLKSZ )
672 res = (res || (dst->o_blksize != src->o_blksize));
673 if ( compare & OBD_MD_FLMODE )
674 res = (res || (dst->o_mode != src->o_mode));
675 if ( compare & OBD_MD_FLUID )
676 res = (res || (dst->o_uid != src->o_uid));
677 if ( compare & OBD_MD_FLGID )
678 res = (res || (dst->o_gid != src->o_gid));
679 if ( compare & OBD_MD_FLFLAGS )
680 res = (res || (dst->o_flags != src->o_flags));
681 if ( compare & OBD_MD_FLNLINK )
682 res = (res || (dst->o_nlink != src->o_nlink));
683 if ( compare & OBD_MD_FLGENER )
684 res = (res || (dst->o_generation != src->o_generation));
685 /* XXX Don't know if thses should be included here - wasn't previously
686 if ( compare & OBD_MD_FLINLINE )
687 res = (res || memcmp(dst->o_inline, src->o_inline));
694 int class_register_type(struct obd_ops *ops, char *nm);
695 int class_unregister_type(char *nm);
696 int class_name2dev(char *name);
697 int class_uuid2dev(char *name);
698 struct obd_device *class_uuid2obd(char *name);
699 int class_connect (struct lustre_handle *conn, struct obd_device *obd);
700 int class_disconnect(struct lustre_handle *conn);
701 struct obd_export *class_conn2export(struct lustre_handle *);
703 /* generic operations shared by various OBD types */
704 int class_multi_setup(struct obd_device *obddev, uint32_t len, void *data);
705 int class_multi_cleanup(struct obd_device *obddev);
711 extern void obd_sysctl_init (void);
712 extern void obd_sysctl_clean (void);
714 #endif /* __LINUX_CLASS_OBD_H */