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