Whamcloud - gitweb
93a533817b063808afaa8ec41f8986562256825d
[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/time.h>
6 #include <linux/obd.h>
7
8 #include <linux/obd_ext2.h>
9 #include <linux/obd_snap.h>
10 /* #include <linux/obd_fc.h> */
11 #include <linux/obd_raid1.h>
12 #include <linux/obd_rpc.h>
13
14
15 /*
16  *  ======== OBD type Declarations ===========
17  */
18
19 typedef uint64_t        obd_id;
20 typedef uint64_t        obd_gr;
21 typedef uint64_t        obd_time;
22 typedef uint64_t        obd_size;
23 typedef uint64_t        obd_off;
24 typedef uint64_t        obd_blocks;
25 typedef uint32_t        obd_blksize;
26 typedef uint32_t        obd_mode;
27 typedef uint32_t        obd_uid;
28 typedef uint32_t        obd_gid;
29 typedef uint32_t        obd_flag;
30 typedef uint32_t        obd_count;
31
32 #define OBD_FL_INLINEDATA       (0x00000001UL)  
33 #define OBD_FL_OBDMDEXISTS      (0x00000002UL)
34
35 #define OBD_INLINESZ    60
36 #define OBD_OBDMDSZ     60
37 /* Note: 64-bit types are 64-bit aligned in structure */
38 struct obdo {
39         obd_id                  o_id;
40         obd_gr                  o_gr;
41         obd_time                o_atime;
42         obd_time                o_mtime;
43         obd_time                o_ctime;
44         obd_size                o_size;
45         obd_blocks              o_blocks;
46         obd_blksize             o_blksize;
47         obd_mode                o_mode;
48         obd_uid                 o_uid;
49         obd_gid                 o_gid;
50         obd_flag                o_flags;
51         obd_flag                o_obdflags;
52         obd_count               o_nlink;
53         obd_count               o_generation;
54         obd_flag                o_valid;        /* hot fields in this obdo */
55         char                    o_inline[OBD_INLINESZ];
56         char                    o_obdmd[OBD_OBDMDSZ];
57         struct list_head        o_list;
58         struct obd_ops          *o_op;
59 };
60
61 #define OBD_MD_FLALL    (~0UL)
62 #define OBD_MD_FLID     (0x00000001UL)
63 #define OBD_MD_FLATIME  (0x00000002UL)
64 #define OBD_MD_FLMTIME  (0x00000004UL)
65 #define OBD_MD_FLCTIME  (0x00000008UL)
66 #define OBD_MD_FLSIZE   (0x00000010UL)
67 #define OBD_MD_FLBLOCKS (0x00000020UL)
68 #define OBD_MD_FLBLKSZ  (0x00000040UL)
69 #define OBD_MD_FLMODE   (0x00000080UL)
70 #define OBD_MD_FLUID    (0x00000100UL)
71 #define OBD_MD_FLGID    (0x00000200UL)
72 #define OBD_MD_FLFLAGS  (0x00000400UL)
73 #define OBD_MD_FLOBDFLG (0x00000800UL)
74 #define OBD_MD_FLNLINK  (0x00001000UL)
75 #define OBD_MD_FLGENER  (0x00002000UL)
76 #define OBD_MD_FLINLINE (0x00004000UL)
77 #define OBD_MD_FLOBDMD  (0x00008000UL)
78 #define OBD_MD_FLNOTOBD (~(OBD_MD_FLOBDMD | OBD_MD_FLOBDFLG | OBD_MD_FLBLOCKS))
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 #define OBD_BRW_READ    (READ)
126 #define OBD_BRW_WRITE   (WRITE)
127 #define OBD_BRW_RWMASK  (READ | WRITE)
128 #define OBD_BRW_CREATE  (0x00000010UL)
129
130 struct obd_ops {
131         int (*o_iocontrol)(int cmd, struct obd_conn *, int len, void *karg,
132                            void *uarg);
133         int (*o_get_info)(struct obd_conn *, obd_count keylen, void *key,
134                           obd_count *vallen, void **val);
135         int (*o_set_info)(struct obd_conn *, obd_count keylen, void *key,
136                           obd_count vallen, void *val);
137         int (*o_attach)(struct obd_device *dev, obd_count len, void *data);
138         int (*o_detach)(struct obd_device *dev);
139         int (*o_setup) (struct obd_device *dev, obd_count len, void *data);
140         int (*o_cleanup)(struct obd_device *dev);
141         int (*o_connect)(struct obd_conn *conn);
142         int (*o_disconnect)(struct obd_conn *conn);
143         int (*o_statfs)(struct obd_conn *conn, struct statfs *statfs);
144         int (*o_preallocate)(struct obd_conn *, obd_count *req, obd_id *ids);
145         int (*o_create)(struct obd_conn *conn,  struct obdo *oa);
146         int (*o_destroy)(struct obd_conn *conn, struct obdo *oa);
147         int (*o_setattr)(struct obd_conn *conn, struct obdo *oa);
148         int (*o_getattr)(struct obd_conn *conn, struct obdo *oa);
149         int (*o_read)(struct obd_conn *conn, struct obdo *oa, char *buf,
150                       obd_size *count, obd_off offset);
151         int (*o_write)(struct obd_conn *conn, struct obdo *oa, char *buf,
152                        obd_size *count, obd_off offset);
153         int (*o_brw)(int rw, struct obd_conn *conn, obd_count num_oa,
154                      struct obdo **oa, obd_count *oa_bufs, char **buf,
155                      obd_size *count, obd_off *offset, obd_flag *flags);
156         int (*o_punch)(struct obd_conn *conn, struct obdo *tgt, obd_size count,
157                        obd_off offset);
158         int (*o_sync)(struct obd_conn *conn, struct obdo *tgt, obd_size count,
159                       obd_off offset);
160         int (*o_migrate)(struct obd_conn *conn, struct obdo *dst,
161                          struct obdo *src, obd_size count, obd_off offset);
162         int (*o_copy)(struct obd_conn *dstconn, struct obdo *dst,
163                       struct obd_conn *srconn, struct obdo *src,
164                       obd_size count, obd_off offset);
165         int (*o_iterate)(struct obd_conn *conn, int (*)(obd_id, obd_gr, void *),
166                          obd_id *startid, obd_gr group, void *data);
167 };
168
169 #define OBT(dev)        dev->obd_type->typ_ops
170 #define OBP(dev,op)     dev->obd_type->typ_ops->o_ ## op
171
172 /* This value is not arbitrarily chosen.  KIO_STATIC_PAGES from linux/iobuf.h */
173 #define MAX_IOVEC       (KIO_STATIC_PAGES - 1)
174
175
176 /*
177  *  ======== OBD Metadata Support  ===========
178  */
179
180 extern int obd_init_obdo_cache(void);
181 extern void obd_cleanup_obdo_cache(void);
182
183
184 static inline int obdo_has_inline(struct obdo *obdo)
185 {
186         return (obdo->o_valid & OBD_MD_FLINLINE &&
187                 obdo->o_obdflags & OBD_FL_INLINEDATA);
188 };
189
190 static inline int obdo_has_obdmd(struct obdo *obdo)
191 {
192         return (obdo->o_valid & OBD_MD_FLOBDMD &&
193                 obdo->o_obdflags & OBD_FL_OBDMDEXISTS);
194 };
195
196 /* support routines */
197 extern kmem_cache_t *obdo_cachep;
198
199 static __inline__ struct obdo *obdo_alloc(void)
200 {
201         struct obdo *oa = NULL;
202
203         oa = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
204         memset(oa, 0, sizeof (*oa));
205
206         return oa;
207 }
208
209 static __inline__ void obdo_free(struct obdo *oa)
210 {
211         if ( !oa ) 
212                 return;
213         kmem_cache_free(obdo_cachep, oa);
214 }
215
216
217
218 static __inline__ struct obdo *obdo_fromid(struct obd_conn *conn, obd_id id,
219                                            obd_flag valid)
220 {
221         struct obdo *oa;
222         int err;
223
224         ENTRY;
225         oa = obdo_alloc();
226         if ( !oa ) {
227                 EXIT;
228                 return ERR_PTR(-ENOMEM);
229         }
230         memset(oa, 0, sizeof(*oa));
231         oa->o_id = id;
232         oa->o_valid = valid;
233         if ((err = OBP(conn->oc_dev, getattr)(conn, oa))) {
234                 obdo_free(oa);
235                 EXIT;
236                 return ERR_PTR(err);
237         }
238         EXIT;
239         return oa;
240 }
241
242 static inline void obdo_from_iattr(struct obdo *oa, struct iattr *attr)
243 {
244         unsigned int ia_valid = attr->ia_valid;
245
246         if (ia_valid & ATTR_ATIME) {
247                 oa->o_atime = attr->ia_atime;
248                 oa->o_valid |= OBD_MD_FLATIME;
249         }
250         if (ia_valid & ATTR_MTIME) {
251                 oa->o_mtime = attr->ia_mtime;
252                 oa->o_valid |= OBD_MD_FLMTIME;
253         }
254         if (ia_valid & ATTR_CTIME) {
255                 oa->o_ctime = attr->ia_ctime;
256                 oa->o_valid |= OBD_MD_FLCTIME;
257         }
258         if (ia_valid & ATTR_SIZE) {
259                 oa->o_size = attr->ia_size;
260                 oa->o_valid |= OBD_MD_FLSIZE;
261         }
262         if (ia_valid & ATTR_MODE) {
263                 oa->o_mode = attr->ia_mode;
264                 oa->o_valid |= OBD_MD_FLMODE;
265                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
266                         oa->o_mode &= ~S_ISGID;
267         }
268         if (ia_valid & ATTR_UID)
269         {
270                 oa->o_uid = attr->ia_uid;
271                 oa->o_valid |= OBD_MD_FLUID;
272         }
273         if (ia_valid & ATTR_GID) {
274                 oa->o_gid = attr->ia_gid;
275                 oa->o_valid |= OBD_MD_FLGID;
276         }
277 }
278
279
280 static __inline__ void obdo_cpy_md(struct obdo *dst, struct obdo *src)
281 {
282         CDEBUG(D_INODE, "src obdo %Ld valid 0x%x, dst obdo %Ld\n",
283                src->o_id, src->o_valid, dst->o_id);
284         if ( src->o_valid & OBD_MD_FLATIME ) 
285                 dst->o_atime = src->o_atime;
286         if ( src->o_valid & OBD_MD_FLMTIME ) 
287                 dst->o_mtime = src->o_mtime;
288         if ( src->o_valid & OBD_MD_FLCTIME ) 
289                 dst->o_ctime = src->o_ctime;
290         if ( src->o_valid & OBD_MD_FLSIZE ) 
291                 dst->o_size = src->o_size;
292         if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
293                 dst->o_blocks = src->o_blocks;
294         if ( src->o_valid & OBD_MD_FLBLKSZ )
295                 dst->o_blksize = src->o_blksize;
296         if ( src->o_valid & OBD_MD_FLMODE ) 
297                 dst->o_mode = src->o_mode;
298         if ( src->o_valid & OBD_MD_FLUID ) 
299                 dst->o_uid = src->o_uid;
300         if ( src->o_valid & OBD_MD_FLGID ) 
301                 dst->o_gid = src->o_gid;
302         if ( src->o_valid & OBD_MD_FLFLAGS ) 
303                 dst->o_flags = src->o_flags;
304         /*
305         if ( src->o_valid & OBD_MD_FLOBDFLG ) 
306                 dst->o_obdflags = src->o_obdflags;
307         */
308         if ( src->o_valid & OBD_MD_FLNLINK ) 
309                 dst->o_nlink = src->o_nlink;
310         if ( src->o_valid & OBD_MD_FLGENER ) 
311                 dst->o_generation = src->o_generation;
312         if ( src->o_valid & OBD_MD_FLINLINE &&
313              src->o_obdflags & OBD_FL_INLINEDATA) {
314                 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
315                 dst->o_obdflags |= OBD_FL_INLINEDATA;
316         }
317         if ( src->o_valid & OBD_MD_FLOBDMD &&
318              src->o_obdflags & OBD_FL_OBDMDEXISTS) {
319                 memcpy(dst->o_obdmd, src->o_obdmd, sizeof(src->o_obdmd));
320                 dst->o_obdflags |= OBD_FL_OBDMDEXISTS;
321         }
322
323         dst->o_valid |= src->o_valid;
324 }
325
326 static __inline__ void obdo_from_inode(struct obdo *dst, struct inode *src)
327 {
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         if ( src->o_valid & OBD_MD_FLID )
360                 dst->i_ino = src->o_id;
361         if ( src->o_valid & OBD_MD_FLATIME ) 
362                 dst->i_atime = src->o_atime;
363         if ( src->o_valid & OBD_MD_FLMTIME ) 
364                 dst->i_mtime = src->o_mtime;
365         if ( src->o_valid & OBD_MD_FLCTIME ) 
366                 dst->i_ctime = src->o_ctime;
367         if ( src->o_valid & OBD_MD_FLSIZE ) 
368                 dst->i_size = src->o_size;
369         if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
370                 dst->i_blocks = src->o_blocks;
371         if ( src->o_valid & OBD_MD_FLBLKSZ )
372                 dst->i_blksize = src->o_blksize;
373         if ( src->o_valid & OBD_MD_FLMODE ) 
374                 dst->i_mode = src->o_mode;
375         if ( src->o_valid & OBD_MD_FLUID ) 
376                 dst->i_uid = src->o_uid;
377         if ( src->o_valid & OBD_MD_FLGID ) 
378                 dst->i_gid = src->o_gid;
379         if ( src->o_valid & OBD_MD_FLFLAGS ) 
380                 dst->i_flags = src->o_flags;
381         if ( src->o_valid & OBD_MD_FLNLINK )
382                 dst->i_nlink = src->o_nlink;
383         if ( src->o_valid & OBD_MD_FLGENER )
384                 dst->i_generation = src->o_generation;
385 }
386
387 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
388 static __inline__ int obdo_cmp_md(struct obdo *dst, struct obdo *src,
389                                   obd_flag compare)
390 {
391         int res = 0;
392
393         if ( compare & OBD_MD_FLATIME )
394                 res = (res || (dst->o_atime != src->o_atime));
395         if ( compare & OBD_MD_FLMTIME )
396                 res = (res || (dst->o_mtime != src->o_mtime));
397         if ( compare & OBD_MD_FLCTIME )
398                 res = (res || (dst->o_ctime != src->o_ctime));
399         if ( compare & OBD_MD_FLSIZE )
400                 res = (res || (dst->o_size != src->o_size));
401         if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
402                 res = (res || (dst->o_blocks != src->o_blocks));
403         if ( compare & OBD_MD_FLBLKSZ )
404                 res = (res || (dst->o_blksize != src->o_blksize));
405         if ( compare & OBD_MD_FLMODE )
406                 res = (res || (dst->o_mode != src->o_mode));
407         if ( compare & OBD_MD_FLUID )
408                 res = (res || (dst->o_uid != src->o_uid));
409         if ( compare & OBD_MD_FLGID )
410                 res = (res || (dst->o_gid != src->o_gid));
411         if ( compare & OBD_MD_FLFLAGS ) 
412                 res = (res || (dst->o_flags != src->o_flags));
413         if ( compare & OBD_MD_FLNLINK )
414                 res = (res || (dst->o_nlink != src->o_nlink));
415         if ( compare & OBD_MD_FLGENER )
416                 res = (res || (dst->o_generation != src->o_generation));
417         /* XXX Don't know if thses should be included here - wasn't previously
418         if ( compare & OBD_MD_FLINLINE )
419                 res = (res || memcmp(dst->o_inline, src->o_inline));
420         if ( compare & OBD_MD_FLOBDMD )
421                 res = (res || memcmp(dst->o_obdmd, src->o_obdmd));
422         */
423         return res;
424 }
425
426
427
428 int obd_register_type(struct obd_ops *ops, char *nm);
429 int obd_unregister_type(char *nm);
430
431 struct obd_client {
432         struct list_head cli_chain;
433         struct obd_device *cli_obd;
434         unsigned int cli_id;
435         unsigned long cli_prealloc_quota;
436         struct list_head cli_prealloc_inodes;
437 };
438
439
440 struct obd_prealloc_inode {
441         struct list_head obd_prealloc_chain;
442         unsigned long inode;
443 };
444
445 /* generic operations shared by various OBD types */
446 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data);
447 int gen_multi_cleanup(struct obd_device *obddev);
448 int gen_multi_attach(struct obd_device *obddev, uint32_t len, void *data);
449 int gen_multi_detach(struct obd_device *obddev);
450 int gen_connect (struct obd_conn *conn);
451 int gen_disconnect(struct obd_conn *conn);
452 struct obd_client *gen_client(struct obd_conn *);
453 int gen_cleanup(struct obd_device *obddev);
454 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
455                   struct obd_conn *src_conn, struct obdo *src,
456                   obd_size count, obd_off offset);
457
458
459
460 /*
461  *  ======== OBD IOCL Declarations ===========
462  */
463 struct oic_generic {
464         uint32_t att_connid;
465         int      att_typelen;
466         void    *att_type;
467         int      att_datalen;
468         void    *att_data;
469 };
470
471
472 /* for preallocation */
473 struct oic_prealloc_s {
474         uint32_t         conn_id;
475         uint32_t         alloc; /* user sets it to the number of inodes
476                                  * requesting to be preallocated.  kernel
477                                  * sets it to the actual number of
478                                  * succesfully preallocated inodes */
479         obd_id           ids[32]; /* actual inode numbers */
480 };
481
482 /* for getattr, setattr, create, destroy */
483 struct oic_attr_s {
484         uint32_t         conn_id;
485         struct obdo      obdo;
486 };
487
488 /* for copy, migrate */
489 struct ioc_mv_s {
490         uint32_t         src_conn_id;
491         struct obdo      src;
492         uint32_t         dst_conn_id;
493         struct obdo      dst;
494 };
495
496 /* for read/write */
497 struct oic_rw_s {
498         uint32_t         conn_id;
499         struct obdo      obdo;
500         char            *buf;
501         obd_size         count;
502         obd_off          offset;
503 };
504
505 /* for punch, sync */
506 struct oic_range_s {
507         uint32_t         conn_id;
508         struct obdo      obdo;
509         obd_size         count;
510         obd_off          offset;
511 };
512
513
514 #define OBD_IOC_CREATE                 _IOR ('f',  3, long)
515 #define OBD_IOC_SETUP                  _IOW ('f',  4, long)
516 #define OBD_IOC_CLEANUP                _IO  ('f',  5      )
517 #define OBD_IOC_DESTROY                _IOW ('f',  6, long)
518 #define OBD_IOC_PREALLOCATE            _IOWR('f',  7, long)
519 #define OBD_IOC_DEC_USE_COUNT          _IO  ('f',  8      )
520 #define OBD_IOC_SETATTR                _IOW ('f',  9, long)
521 #define OBD_IOC_GETATTR                _IOR ('f', 10, long)
522 #define OBD_IOC_READ                   _IOWR('f', 11, long)
523 #define OBD_IOC_WRITE                  _IOWR('f', 12, long)
524 #define OBD_IOC_CONNECT                _IOR ('f', 13, long)
525 #define OBD_IOC_DISCONNECT             _IOW ('f', 14, long)
526 #define OBD_IOC_STATFS                 _IOWR('f', 15, long)
527 #define OBD_IOC_SYNC                   _IOR ('f', 16, long)
528 #define OBD_IOC_READ2                  _IOWR('f', 17, long)
529 #define OBD_IOC_FORMAT                 _IOWR('f', 18, long)
530 #define OBD_IOC_PARTITION              _IOWR('f', 19, long)
531 #define OBD_IOC_ATTACH                 _IOWR('f', 20, long)
532 #define OBD_IOC_DETACH                 _IOWR('f', 21, long)
533 #define OBD_IOC_COPY                   _IOWR('f', 22, long)
534 #define OBD_IOC_MIGR                   _IOWR('f', 23, long)
535 #define OBD_IOC_PUNCH                  _IOWR('f', 24, long)
536
537 #define OBD_IOC_DEC_FS_USE_COUNT       _IO  ('f', 32      )
538
539
540 /* sysctl.c */
541 extern void obd_sysctl_init (void);
542 extern void obd_sysctl_clean (void);
543
544 #define CHKCONN(conn)   do { if (!gen_client(conn)) {\
545                 printk("%s %d invalid client %u\n", __FILE__, __LINE__, \
546                        conn->oc_id);\
547                 return -EINVAL; }} while (0) 
548
549
550
551 #endif /* __LINUX_CLASS_OBD_H */