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