Whamcloud - gitweb
596e0bec308cec1a818052a389d418e3c968fd64
[fs/lustre-release.git] / lustre / include / linux / obd_class.h
1 #ifndef __LINUX_CLASS_OBD_H
2 #define __LINUX_CLASS_OBD_H
3
4 #include <linux/fs.h>
5 #include <linux/ext2_fs.h>
6 #include <linux/time.h>
7 #include <linux/obd.h>
8
9 #include <linux/obd_ext2.h>
10 #include <linux/obd_snap.h>
11 /* #include <linux/obd_fc.h> */
12 #include <linux/obd_raid1.h>
13 #include <linux/obd_rpc.h>
14
15
16 /*
17  *  ======== OBD type Declarations ===========
18  */
19
20 typedef uint64_t        obd_id;
21 typedef uint64_t        obd_gr;
22 typedef uint64_t        obd_time;
23 typedef uint64_t        obd_size;
24 typedef uint64_t        obd_off;
25 typedef uint64_t        obd_blocks;
26 typedef uint32_t        obd_blksize;
27 typedef uint32_t        obd_mode;
28 typedef uint32_t        obd_uid;
29 typedef uint32_t        obd_gid;
30 typedef uint32_t        obd_flag;
31 typedef uint32_t        obd_count;
32
33 #define OBD_FL_INLINEDATA       (1UL)  
34 #define OBD_FL_OBDMDEXISTS      (1UL << 1)
35
36 #define OBD_INLINESZ    60
37 #define OBD_OBDMDSZ     60
38 /* Note: 64-bit types are 64-bit aligned in structure */
39 struct obdo {
40         obd_id                  o_id;
41         obd_gr                  o_gr;
42         obd_time                o_atime;
43         obd_time                o_mtime;
44         obd_time                o_ctime;
45         obd_size                o_size;
46         obd_blocks              o_blocks;
47         obd_blksize             o_blksize;
48         obd_mode                o_mode;
49         obd_uid                 o_uid;
50         obd_gid                 o_gid;
51         obd_flag                o_flags;
52         obd_flag                o_obdflags;
53         obd_count               o_nlink;
54         obd_count               o_generation;
55         obd_flag                o_valid;        /* hot fields in this obdo */
56         char                    o_inline[OBD_INLINESZ];
57         char                    o_obdmd[OBD_OBDMDSZ];
58         struct list_head        o_list;
59         struct obd_ops          *o_op;
60 };
61
62 #define OBD_MD_FLALL    (~0UL)
63 #define OBD_MD_FLID     (1UL)
64 #define OBD_MD_FLATIME  (1UL<<1)
65 #define OBD_MD_FLMTIME  (1UL<<2)
66 #define OBD_MD_FLCTIME  (1UL<<3)
67 #define OBD_MD_FLSIZE   (1UL<<4)
68 #define OBD_MD_FLBLOCKS (1UL<<5)
69 #define OBD_MD_FLBLKSZ  (1UL<<6)
70 #define OBD_MD_FLMODE   (1UL<<7)
71 #define OBD_MD_FLUID    (1UL<<8)
72 #define OBD_MD_FLGID    (1UL<<9)
73 #define OBD_MD_FLFLAGS  (1UL<<10)
74 #define OBD_MD_FLOBDFLG (1UL<<11)
75 #define OBD_MD_FLNLINK  (1UL<<12)
76 #define OBD_MD_FLGENER  (1UL<<13)
77 #define OBD_MD_FLINLINE (1UL<<14)
78 #define OBD_MD_FLOBDMD  (1UL<<15)
79
80 /*
81  *  ======== OBD Device Declarations ===========
82  */
83
84
85 #define OBD_PSDEV_MAJOR 186
86 #define MAX_OBD_DEVICES 8
87 #define MAX_MULTI 16
88
89
90 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
91
92
93 #define OBD_ATTACHED 0x1
94 #define OBD_SET_UP   0x2
95
96 struct obd_conn {
97         struct obd_device *oc_dev;
98         uint32_t oc_id;
99 };
100
101 /* corresponds to one of the obdx */
102 struct obd_device {
103         struct obd_type *obd_type;
104         int obd_minor;
105         int obd_flags;
106         int obd_refcnt; 
107         int obd_multi_count;
108         struct obd_conn obd_multi_conn[MAX_MULTI];
109         unsigned int obd_gen_last_id;
110         unsigned long obd_gen_prealloc_quota;
111         struct list_head obd_gen_clients;
112         union {
113                 struct ext2_obd ext2;
114                 struct raid1_obd raid1;
115                 struct snap_obd snap;
116                 struct rpc_obd rpc;
117                 /* struct fc_obd fc; */
118         } u;
119 };
120
121 /*
122  *  ======== OBD Operations Declarations ===========
123  */
124
125
126 struct obd_ops {
127         int (*o_iocontrol)(int cmd, struct obd_conn *, int len, void *karg, void *uarg);
128         int (*o_get_info)(struct obd_conn *, obd_count keylen, void *key, obd_count *vallen, void **val);
129         int (*o_set_info)(struct obd_conn *, obd_count keylen, void *key, obd_count vallen, void *val);
130         int (*o_attach)(struct obd_device *, obd_count len, void *);
131         int (*o_detach)(struct obd_device *);
132         int (*o_setup) (struct obd_device *dev, obd_count len, void *data);
133         int (*o_cleanup)(struct obd_device *dev);
134         int (*o_connect)(struct obd_conn *conn);
135         int (*o_disconnect)(struct obd_conn *);
136         int (*o_statfs)(struct obd_conn *, struct statfs *statfs);
137         int (*o_preallocate)(struct obd_conn *, obd_count *req, obd_id *ids);
138         int (*o_create)(struct obd_conn *,  struct obdo *oa);
139         int (*o_destroy)(struct obd_conn *, struct obdo *oa);
140         int (*o_setattr)(struct obd_conn *, struct obdo *oa);
141         int (*o_getattr)(struct obd_conn *, struct obdo *oa);
142         int (*o_read)(struct obd_conn *, struct obdo *oa, char *buf, obd_size *count, obd_off offset);
143         int (*o_write)(struct obd_conn *, struct obdo *oa, char *buf, obd_size *count, obd_off offset);
144         int (*o_brw)(int rw, struct obd_conn * conn, struct obdo *oa, char *buf, obd_size count, obd_off offset, obd_flag flags);
145         int (*o_punch)(struct obd_conn *, struct obdo *tgt, obd_size count, obd_off offset);
146         int (*o_sync)(struct obd_conn *, struct obdo *tgt, obd_size count, obd_off offset);
147         int (*o_migrate)(struct obd_conn *, struct obdo *dst, struct obdo *src, obd_size count, obd_off offset);
148         int (*o_copy)(struct obd_conn *dstconn, struct obdo *dst, struct obd_conn *srconn, struct obdo *src, obd_size count, obd_off offset);
149         int (*o_iterate)(struct obd_conn *, int (*)(obd_id, void *), obd_id start, void *);
150
151 };
152
153 #define OBT(dev)        dev->obd_type->typ_ops
154 #define OBP(dev,op)     dev->obd_type->typ_ops->o_ ## op
155
156
157 /*
158  *  ======== OBD Metadata Support  ===========
159  */
160
161 extern int obd_init_obdo_cache(void);
162 extern void obd_cleanup_obdo_cache(void);
163
164
165 static inline int obdo_has_inline(struct obdo *obdo)
166 {
167         return obdo->o_obdflags & OBD_FL_INLINEDATA;
168 };
169
170 static inline int obdo_has_obdmd(struct obdo *obdo)
171 {
172         return obdo->o_obdflags & OBD_FL_OBDMDEXISTS;
173 };
174
175 /* support routines */
176 extern kmem_cache_t *obdo_cachep;
177
178 static __inline__ struct obdo *obdo_alloc(void)
179 {
180         struct obdo *res = NULL;
181
182         res = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
183         memset(res, 0, sizeof (*res));
184
185         return res;
186 }
187
188 static __inline__ void obdo_free(struct obdo *oa)
189 {
190         if ( !oa ) 
191                 return;
192         kmem_cache_free(obdo_cachep, oa);
193 }
194
195
196
197 static __inline__ struct obdo *obdo_fromid(struct obd_conn *conn, obd_id id)
198 {
199         struct obdo *res = NULL;
200
201         res = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
202         if ( !res ) {
203                 EXIT;
204                 return NULL;
205         }
206         memset(res, 0, sizeof(*res));
207         res->o_id = id;
208         if (OBP(conn->oc_dev, getattr)(conn, res)) {
209                 OBD_FREE(res, sizeof(*res));
210                 EXIT;
211                 return NULL;
212         }
213         EXIT;
214         return res;
215 }
216
217 static inline void obdo_from_iattr(struct obdo *oa, struct iattr *attr)
218 {
219         unsigned int ia_valid = attr->ia_valid;
220
221         if (ia_valid & ATTR_ATIME) {
222                 oa->o_atime = attr->ia_atime;
223                 oa->o_valid |= OBD_MD_FLATIME;
224         }
225         if (ia_valid & ATTR_MTIME) {
226                 oa->o_mtime = attr->ia_mtime;
227                 oa->o_valid |= OBD_MD_FLMTIME;
228         }
229         if (ia_valid & ATTR_CTIME) {
230                 oa->o_ctime = attr->ia_ctime;
231                 oa->o_valid |= OBD_MD_FLCTIME;
232         }
233         if (ia_valid & ATTR_SIZE) {
234                 oa->o_size = attr->ia_size;
235                 oa->o_valid |= OBD_MD_FLSIZE;
236         }
237         if (ia_valid & ATTR_MODE) {
238                 oa->o_mode = attr->ia_mode;
239                 oa->o_valid |= OBD_MD_FLMODE;
240                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
241                         oa->o_mode &= ~S_ISGID;
242         }
243         if (ia_valid & ATTR_UID)
244         {
245                 oa->o_uid = attr->ia_uid;
246                 oa->o_valid |= OBD_MD_FLUID;
247         }
248         if (ia_valid & ATTR_GID) {
249                 oa->o_gid = attr->ia_gid;
250                 oa->o_valid |= OBD_MD_FLGID;
251         }
252 }
253
254
255 static __inline__ void obdo_cpy_md(struct obdo *dst, struct obdo *src)
256 {
257         CDEBUG(D_INODE, "flags %x\n", src->o_valid);
258         if ( src->o_valid & OBD_MD_FLATIME ) 
259                 dst->o_atime = src->o_atime;
260         if ( src->o_valid & OBD_MD_FLMTIME ) 
261                 dst->o_mtime = src->o_mtime;
262         if ( src->o_valid & OBD_MD_FLCTIME ) 
263                 dst->o_ctime = src->o_ctime;
264         if ( src->o_valid & OBD_MD_FLSIZE ) 
265                 dst->o_size = src->o_size;
266         if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
267                 dst->o_blocks = src->o_blocks;
268         if ( src->o_valid & OBD_MD_FLBLKSZ )
269                 dst->o_blksize = src->o_blksize;
270         if ( src->o_valid & OBD_MD_FLMODE ) 
271                 dst->o_mode = src->o_mode;
272         if ( src->o_valid & OBD_MD_FLUID ) 
273                 dst->o_uid = src->o_uid;
274         if ( src->o_valid & OBD_MD_FLGID ) 
275                 dst->o_gid = src->o_gid;
276         if ( src->o_valid & OBD_MD_FLFLAGS ) 
277                 dst->o_flags = src->o_flags;
278         if ( src->o_valid & OBD_MD_FLOBDFLG ) 
279                 dst->o_obdflags = src->o_obdflags;
280         if ( src->o_valid & OBD_MD_FLNLINK ) 
281                 dst->o_nlink = src->o_nlink;
282         if ( src->o_valid & OBD_MD_FLGENER ) 
283                 dst->o_generation = src->o_generation;
284         if ( src->o_valid & OBD_MD_FLINLINE ) 
285                 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
286         if ( src->o_valid & OBD_MD_FLOBDMD ) 
287                 memcpy(dst->o_obdmd, src->o_obdmd, sizeof(src->o_obdmd));
288
289         dst->o_valid |= src->o_valid;
290 }
291
292 static __inline__ void obdo_from_inode(struct obdo *dst, struct inode *src)
293 {
294         CDEBUG(D_INODE, "flags %x\n", dst->o_valid);
295         if ( dst->o_valid & OBD_MD_FLID )
296                 dst->o_id = src->i_ino;
297         if ( dst->o_valid & OBD_MD_FLATIME )
298                 dst->o_atime = src->i_atime;
299         if ( dst->o_valid & OBD_MD_FLMTIME )
300                 dst->o_mtime = src->i_mtime;
301         if ( dst->o_valid & OBD_MD_FLCTIME )
302                 dst->o_ctime = src->i_ctime;
303         if ( dst->o_valid & OBD_MD_FLSIZE )
304                 dst->o_size = src->i_size;
305         if ( dst->o_valid & OBD_MD_FLBLOCKS )   /* allocation of space */
306                 dst->o_blocks = src->i_blocks;
307         if ( dst->o_valid & OBD_MD_FLBLKSZ )
308                 dst->o_blksize = src->i_blksize;
309         if ( dst->o_valid & OBD_MD_FLMODE )
310                 dst->o_mode = src->i_mode;
311         if ( dst->o_valid & OBD_MD_FLUID )
312                 dst->o_uid = src->i_uid;
313         if ( dst->o_valid & OBD_MD_FLGID )
314                 dst->o_gid = src->i_gid;
315         if ( dst->o_valid & OBD_MD_FLFLAGS )
316                 dst->o_flags = src->i_flags;
317         if ( dst->o_valid & OBD_MD_FLNLINK )
318                 dst->o_nlink = src->i_nlink;
319         if ( dst->o_valid & OBD_MD_FLGENER ) 
320                 dst->o_generation = src->i_generation;
321 }
322
323 static __inline__ void obdo_to_inode(struct inode *dst, struct obdo *src)
324 {
325
326         CDEBUG(D_INODE, "flags %x\n", src->o_valid);
327         if ( src->o_valid & OBD_MD_FLID )
328                 dst->i_ino = src->o_id;
329         if ( src->o_valid & OBD_MD_FLATIME ) 
330                 dst->i_atime = src->o_atime;
331         if ( src->o_valid & OBD_MD_FLMTIME ) 
332                 dst->i_mtime = src->o_mtime;
333         if ( src->o_valid & OBD_MD_FLCTIME ) 
334                 dst->i_ctime = src->o_ctime;
335         if ( src->o_valid & OBD_MD_FLSIZE ) 
336                 dst->i_size = src->o_size;
337         if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
338                 dst->i_blocks = src->o_blocks;
339         if ( src->o_valid & OBD_MD_FLBLKSZ )
340                 dst->i_blksize = src->o_blksize;
341         if ( src->o_valid & OBD_MD_FLMODE ) 
342                 dst->i_mode = src->o_mode;
343         if ( src->o_valid & OBD_MD_FLUID ) 
344                 dst->i_uid = src->o_uid;
345         if ( src->o_valid & OBD_MD_FLGID ) 
346                 dst->i_gid = src->o_gid;
347         if ( src->o_valid & OBD_MD_FLFLAGS ) 
348                 dst->i_flags = src->o_flags;
349         if ( src->o_valid & OBD_MD_FLNLINK )
350                 dst->i_nlink = src->o_nlink;
351         if ( src->o_valid & OBD_MD_FLGENER )
352                 dst->i_generation = src->o_generation;
353 }
354
355 static __inline__ int obdo_cmp_md(struct obdo *dst, struct obdo *src)
356 {
357         int res = 1;
358
359         if ( src->o_valid & OBD_MD_FLMODE )
360                 res = (res && (dst->o_mode == src->o_mode));
361         if ( src->o_valid & OBD_MD_FLUID )
362                 res = (res && (dst->o_uid == src->o_uid));
363         if ( src->o_valid & OBD_MD_FLGID )
364                 res = (res && (dst->o_gid == src->o_gid));
365         if ( src->o_valid & OBD_MD_FLSIZE )
366                 res = (res && (dst->o_size == src->o_size));
367         if ( src->o_valid & OBD_MD_FLATIME )
368                 res = (res && (dst->o_atime == src->o_atime));
369         if ( src->o_valid & OBD_MD_FLMTIME )
370                 res = (res && (dst->o_mtime == src->o_mtime));
371         if ( src->o_valid & OBD_MD_FLCTIME )
372                 res = (res && (dst->o_ctime == src->o_ctime));
373         if ( src->o_valid & OBD_MD_FLFLAGS )
374                 res = (res && (dst->o_flags == src->o_flags));
375         /* allocation of space */
376         if ( src->o_valid & OBD_MD_FLBLOCKS )
377                 res = (res && (dst->o_blocks == src->o_blocks));
378         return res;
379 }
380
381
382
383 int obd_register_type(struct obd_ops *ops, char *nm);
384 int obd_unregister_type(char *nm);
385
386 struct obd_client {
387         struct list_head cli_chain;
388         struct obd_device *cli_obd;
389         unsigned int cli_id;
390         unsigned long cli_prealloc_quota;
391         struct list_head cli_prealloc_inodes;
392 };
393
394
395 struct obd_prealloc_inode {
396         struct list_head obd_prealloc_chain;
397         unsigned long inode;
398 };
399
400 /* generic operations shared by various OBD types */
401 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data);
402 int gen_multi_cleanup(struct obd_device *obddev);
403 int gen_multi_attach(struct obd_device *obddev, uint32_t len, void *data);
404 int gen_multi_detach(struct obd_device *obddev);
405 int gen_connect (struct obd_conn *conn);
406 int gen_disconnect(struct obd_conn *conn);
407 struct obd_client *gen_client(struct obd_conn *);
408 int gen_cleanup(struct obd_device *obddev);
409 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
410                   struct obd_conn *src_conn, struct obdo *src,
411                   obd_size count, obd_off offset);
412
413
414
415 /*
416  *  ======== OBD IOCL Declarations ===========
417  */
418 struct oic_generic {
419         int  att_connid;
420         int  att_typelen;
421         void *att_type;
422         int  att_datalen;
423         void *att_data;
424 };
425
426
427 /* for preallocation */
428 struct oic_prealloc_s {
429         uint32_t cli_id;
430         uint32_t alloc; /* user sets it to the number of inodes
431                           * requesting to be preallocated.  kernel
432                           * sets it to the actual number * of
433                           * succesfully preallocated inodes */
434         obd_id  ids[32]; /* actual inode numbers */
435 };
436
437 /* for getattr, setattr, create, destroy */
438 struct oic_attr_s {
439         uint32_t conn_id;
440         struct obdo  obdo;
441 };
442
443 /* for copy, migrate */
444 struct ioc_mv_s {
445         unsigned int src_conn_id;
446         struct obdo  src;
447         unsigned int dst_conn_id;
448         struct obdo  dst;
449 };
450
451 /* for read/write */
452 struct oic_rw_s {
453         uint32_t conn_id;
454         struct obdo obdo;
455         char * buf;
456         obd_size count;
457         obd_off offset;
458 };
459
460 /* for punch, sync */
461 struct oic_range_s {
462         uint32_t conn_id;
463         struct obdo obdo;
464         obd_size count;
465         obd_off offset;
466 };
467
468
469 #define OBD_IOC_CREATE                 _IOR ('f',  3, long)
470 #define OBD_IOC_SETUP                  _IOW ('f',  4, long)
471 #define OBD_IOC_CLEANUP                _IO  ('f',  5      )
472 #define OBD_IOC_DESTROY                _IOW ('f',  6, long)
473 #define OBD_IOC_PREALLOCATE            _IOWR('f',  7, long)
474 #define OBD_IOC_DEC_USE_COUNT          _IO  ('f',  8      )
475 #define OBD_IOC_SETATTR                _IOW ('f',  9, long)
476 #define OBD_IOC_GETATTR                _IOR ('f', 10, long)
477 #define OBD_IOC_READ                   _IOWR('f', 11, long)
478 #define OBD_IOC_WRITE                  _IOWR('f', 12, long)
479 #define OBD_IOC_CONNECT                _IOR ('f', 13, long)
480 #define OBD_IOC_DISCONNECT             _IOW ('f', 14, long)
481 #define OBD_IOC_STATFS                 _IOWR('f', 15, long)
482 #define OBD_IOC_SYNC                   _IOR ('f',  16, long)
483 #define OBD_IOC_READ2                  _IOWR('f', 17, long)
484 #define OBD_IOC_FORMAT                 _IOWR('f', 18, long)
485 #define OBD_IOC_PARTITION              _IOWR('f', 19, long)
486 #define OBD_IOC_ATTACH                 _IOWR('f', 20, long)
487 #define OBD_IOC_DETACH                 _IOWR('f', 21, long)
488 #define OBD_IOC_COPY                   _IOWR('f', 22, long)
489 #define OBD_IOC_MIGR                   _IOWR('f', 23, long)
490
491 #define OBD_IOC_DEC_FS_USE_COUNT       _IO  ('f', 32      )
492
493
494 /* sysctl.c */
495 extern void obd_sysctl_init (void);
496 extern void obd_sysctl_clean (void);
497
498 #define CHKCONN(conn)   do { if (!gen_client(conn)) {\
499                 printk("%s %d invalid client %u\n", __FILE__, __LINE__, \
500                        conn->oc_id);\
501                 return -EINVAL; }} while (0) 
502
503
504
505 #endif /* __LINUX_CLASS_OBD_H */