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