Whamcloud - gitweb
Working on add_to_page_cache oops.
[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     (0x0001UL)
64 #define OBD_MD_FLATIME  (0x0002UL)
65 #define OBD_MD_FLMTIME  (0x0004UL)
66 #define OBD_MD_FLCTIME  (0x0008UL)
67 #define OBD_MD_FLSIZE   (0x0010UL)
68 #define OBD_MD_FLBLOCKS (0x0020UL)
69 #define OBD_MD_FLBLKSZ  (0x0040UL)
70 #define OBD_MD_FLMODE   (0x0080UL)
71 #define OBD_MD_FLUID    (0x0100UL)
72 #define OBD_MD_FLGID    (0x0200UL)
73 #define OBD_MD_FLFLAGS  (0x0400UL)
74 #define OBD_MD_FLOBDFLG (0x0800UL)
75 #define OBD_MD_FLNLINK  (0x1000UL)
76 #define OBD_MD_FLGENER  (0x2000UL)
77 #define OBD_MD_FLINLINE (0x4000UL)
78 #define OBD_MD_FLOBDMD  (0x8000UL)
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,
128                            void *uarg);
129         int (*o_get_info)(struct obd_conn *, obd_count keylen, void *key,
130                           obd_count *vallen, void **val);
131         int (*o_set_info)(struct obd_conn *, obd_count keylen, void *key,
132                           obd_count vallen, void *val);
133         int (*o_attach)(struct obd_device *dev, obd_count len, void *data);
134         int (*o_detach)(struct obd_device *dev);
135         int (*o_setup) (struct obd_device *dev, obd_count len, void *data);
136         int (*o_cleanup)(struct obd_device *dev);
137         int (*o_connect)(struct obd_conn *conn);
138         int (*o_disconnect)(struct obd_conn *conn);
139         int (*o_statfs)(struct obd_conn *conn, struct statfs *statfs);
140         int (*o_preallocate)(struct obd_conn *, obd_count *req, obd_id *ids);
141         int (*o_create)(struct obd_conn *conn,  struct obdo *oa);
142         int (*o_destroy)(struct obd_conn *conn, struct obdo *oa);
143         int (*o_setattr)(struct obd_conn *conn, struct obdo *oa);
144         int (*o_getattr)(struct obd_conn *conn, struct obdo *oa);
145         int (*o_read)(struct obd_conn *conn, struct obdo *oa, char *buf,
146                       obd_size *count, obd_off offset);
147         int (*o_write)(struct obd_conn *conn, struct obdo *oa, char *buf,
148                        obd_size *count, obd_off offset);
149         int (*o_brw)(int rw, struct obd_conn *conn, struct obdo *oa,
150                      char *buf, obd_size count, obd_off offset, obd_flag flags);
151         int (*o_punch)(struct obd_conn *conn, struct obdo *tgt, obd_size count,
152                        obd_off offset);
153         int (*o_sync)(struct obd_conn *conn, struct obdo *tgt, obd_size count,
154                       obd_off offset);
155         int (*o_migrate)(struct obd_conn *conn, struct obdo *dst,
156                          struct obdo *src, obd_size count, obd_off offset);
157         int (*o_copy)(struct obd_conn *dstconn, struct obdo *dst,
158                       struct obd_conn *srconn, struct obdo *src,
159                       obd_size count, obd_off offset);
160         int (*o_iterate)(struct obd_conn *conn, int (*)(obd_id, obd_gr, void *),
161                          obd_id *startid, obd_gr group, void *data);
162 };
163
164 #define OBT(dev)        dev->obd_type->typ_ops
165 #define OBP(dev,op)     dev->obd_type->typ_ops->o_ ## op
166
167
168 /*
169  *  ======== OBD Metadata Support  ===========
170  */
171
172 extern int obd_init_obdo_cache(void);
173 extern void obd_cleanup_obdo_cache(void);
174
175
176 static inline int obdo_has_inline(struct obdo *obdo)
177 {
178         return obdo->o_obdflags & OBD_FL_INLINEDATA;
179 };
180
181 static inline int obdo_has_obdmd(struct obdo *obdo)
182 {
183         return obdo->o_obdflags & OBD_FL_OBDMDEXISTS;
184 };
185
186 /* support routines */
187 extern kmem_cache_t *obdo_cachep;
188
189 static __inline__ struct obdo *obdo_alloc(void)
190 {
191         struct obdo *res = NULL;
192
193         res = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
194         memset(res, 0, sizeof (*res));
195
196         return res;
197 }
198
199 static __inline__ void obdo_free(struct obdo *oa)
200 {
201         if ( !oa ) 
202                 return;
203         kmem_cache_free(obdo_cachep, oa);
204 }
205
206
207
208 static __inline__ struct obdo *obdo_fromid(struct obd_conn *conn, obd_id id)
209 {
210         struct obdo *res = NULL;
211
212         res = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
213         if ( !res ) {
214                 EXIT;
215                 return NULL;
216         }
217         memset(res, 0, sizeof(*res));
218         res->o_id = id;
219         if (OBP(conn->oc_dev, getattr)(conn, res)) {
220                 OBD_FREE(res, sizeof(*res));
221                 EXIT;
222                 return NULL;
223         }
224         EXIT;
225         return res;
226 }
227
228 static inline void obdo_from_iattr(struct obdo *oa, struct iattr *attr)
229 {
230         unsigned int ia_valid = attr->ia_valid;
231
232         if (ia_valid & ATTR_ATIME) {
233                 oa->o_atime = attr->ia_atime;
234                 oa->o_valid |= OBD_MD_FLATIME;
235         }
236         if (ia_valid & ATTR_MTIME) {
237                 oa->o_mtime = attr->ia_mtime;
238                 oa->o_valid |= OBD_MD_FLMTIME;
239         }
240         if (ia_valid & ATTR_CTIME) {
241                 oa->o_ctime = attr->ia_ctime;
242                 oa->o_valid |= OBD_MD_FLCTIME;
243         }
244         if (ia_valid & ATTR_SIZE) {
245                 oa->o_size = attr->ia_size;
246                 oa->o_valid |= OBD_MD_FLSIZE;
247         }
248         if (ia_valid & ATTR_MODE) {
249                 oa->o_mode = attr->ia_mode;
250                 oa->o_valid |= OBD_MD_FLMODE;
251                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
252                         oa->o_mode &= ~S_ISGID;
253         }
254         if (ia_valid & ATTR_UID)
255         {
256                 oa->o_uid = attr->ia_uid;
257                 oa->o_valid |= OBD_MD_FLUID;
258         }
259         if (ia_valid & ATTR_GID) {
260                 oa->o_gid = attr->ia_gid;
261                 oa->o_valid |= OBD_MD_FLGID;
262         }
263 }
264
265
266 static __inline__ void obdo_cpy_md(struct obdo *dst, struct obdo *src)
267 {
268         CDEBUG(D_INODE, "flags 0x%x\n", src->o_valid);
269         if ( src->o_valid & OBD_MD_FLATIME ) 
270                 dst->o_atime = src->o_atime;
271         if ( src->o_valid & OBD_MD_FLMTIME ) 
272                 dst->o_mtime = src->o_mtime;
273         if ( src->o_valid & OBD_MD_FLCTIME ) 
274                 dst->o_ctime = src->o_ctime;
275         if ( src->o_valid & OBD_MD_FLSIZE ) 
276                 dst->o_size = src->o_size;
277         if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
278                 dst->o_blocks = src->o_blocks;
279         if ( src->o_valid & OBD_MD_FLBLKSZ )
280                 dst->o_blksize = src->o_blksize;
281         if ( src->o_valid & OBD_MD_FLMODE ) 
282                 dst->o_mode = src->o_mode;
283         if ( src->o_valid & OBD_MD_FLUID ) 
284                 dst->o_uid = src->o_uid;
285         if ( src->o_valid & OBD_MD_FLGID ) 
286                 dst->o_gid = src->o_gid;
287         if ( src->o_valid & OBD_MD_FLFLAGS ) 
288                 dst->o_flags = src->o_flags;
289         if ( src->o_valid & OBD_MD_FLOBDFLG ) 
290                 dst->o_obdflags = src->o_obdflags;
291         if ( src->o_valid & OBD_MD_FLNLINK ) 
292                 dst->o_nlink = src->o_nlink;
293         if ( src->o_valid & OBD_MD_FLGENER ) 
294                 dst->o_generation = src->o_generation;
295         if ( src->o_valid & OBD_MD_FLINLINE ) 
296                 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
297         if ( src->o_valid & OBD_MD_FLOBDMD ) 
298                 memcpy(dst->o_obdmd, src->o_obdmd, sizeof(src->o_obdmd));
299
300         dst->o_valid |= src->o_valid;
301 }
302
303 static __inline__ void obdo_from_inode(struct obdo *dst, struct inode *src)
304 {
305         CDEBUG(D_INODE, "flags %x\n", dst->o_valid);
306         if ( dst->o_valid & OBD_MD_FLID )
307                 dst->o_id = src->i_ino;
308         if ( dst->o_valid & OBD_MD_FLATIME )
309                 dst->o_atime = src->i_atime;
310         if ( dst->o_valid & OBD_MD_FLMTIME )
311                 dst->o_mtime = src->i_mtime;
312         if ( dst->o_valid & OBD_MD_FLCTIME )
313                 dst->o_ctime = src->i_ctime;
314         if ( dst->o_valid & OBD_MD_FLSIZE )
315                 dst->o_size = src->i_size;
316         if ( dst->o_valid & OBD_MD_FLBLOCKS )   /* allocation of space */
317                 dst->o_blocks = src->i_blocks;
318         if ( dst->o_valid & OBD_MD_FLBLKSZ )
319                 dst->o_blksize = src->i_blksize;
320         if ( dst->o_valid & OBD_MD_FLMODE )
321                 dst->o_mode = src->i_mode;
322         if ( dst->o_valid & OBD_MD_FLUID )
323                 dst->o_uid = src->i_uid;
324         if ( dst->o_valid & OBD_MD_FLGID )
325                 dst->o_gid = src->i_gid;
326         if ( dst->o_valid & OBD_MD_FLFLAGS )
327                 dst->o_flags = src->i_flags;
328         if ( dst->o_valid & OBD_MD_FLNLINK )
329                 dst->o_nlink = src->i_nlink;
330         if ( dst->o_valid & OBD_MD_FLGENER ) 
331                 dst->o_generation = src->i_generation;
332 }
333
334 static __inline__ void obdo_to_inode(struct inode *dst, struct obdo *src)
335 {
336
337         CDEBUG(D_INODE, "flags %x\n", src->o_valid);
338         if ( src->o_valid & OBD_MD_FLID )
339                 dst->i_ino = src->o_id;
340         if ( src->o_valid & OBD_MD_FLATIME ) 
341                 dst->i_atime = src->o_atime;
342         if ( src->o_valid & OBD_MD_FLMTIME ) 
343                 dst->i_mtime = src->o_mtime;
344         if ( src->o_valid & OBD_MD_FLCTIME ) 
345                 dst->i_ctime = src->o_ctime;
346         if ( src->o_valid & OBD_MD_FLSIZE ) 
347                 dst->i_size = src->o_size;
348         if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
349                 dst->i_blocks = src->o_blocks;
350         if ( src->o_valid & OBD_MD_FLBLKSZ )
351                 dst->i_blksize = src->o_blksize;
352         if ( src->o_valid & OBD_MD_FLMODE ) 
353                 dst->i_mode = src->o_mode;
354         if ( src->o_valid & OBD_MD_FLUID ) 
355                 dst->i_uid = src->o_uid;
356         if ( src->o_valid & OBD_MD_FLGID ) 
357                 dst->i_gid = src->o_gid;
358         if ( src->o_valid & OBD_MD_FLFLAGS ) 
359                 dst->i_flags = src->o_flags;
360         if ( src->o_valid & OBD_MD_FLNLINK )
361                 dst->i_nlink = src->o_nlink;
362         if ( src->o_valid & OBD_MD_FLGENER )
363                 dst->i_generation = src->o_generation;
364 }
365
366 static __inline__ int obdo_cmp_md(struct obdo *dst, struct obdo *src)
367 {
368         int res = 1;
369
370         if ( src->o_valid & OBD_MD_FLMODE )
371                 res = (res && (dst->o_mode == src->o_mode));
372         if ( src->o_valid & OBD_MD_FLUID )
373                 res = (res && (dst->o_uid == src->o_uid));
374         if ( src->o_valid & OBD_MD_FLGID )
375                 res = (res && (dst->o_gid == src->o_gid));
376         if ( src->o_valid & OBD_MD_FLSIZE )
377                 res = (res && (dst->o_size == src->o_size));
378         if ( src->o_valid & OBD_MD_FLATIME )
379                 res = (res && (dst->o_atime == src->o_atime));
380         if ( src->o_valid & OBD_MD_FLMTIME )
381                 res = (res && (dst->o_mtime == src->o_mtime));
382         if ( src->o_valid & OBD_MD_FLCTIME )
383                 res = (res && (dst->o_ctime == src->o_ctime));
384         if ( src->o_valid & OBD_MD_FLFLAGS )
385                 res = (res && (dst->o_flags == src->o_flags));
386         /* allocation of space */
387         if ( src->o_valid & OBD_MD_FLBLOCKS )
388                 res = (res && (dst->o_blocks == src->o_blocks));
389         return res;
390 }
391
392
393
394 int obd_register_type(struct obd_ops *ops, char *nm);
395 int obd_unregister_type(char *nm);
396
397 struct obd_client {
398         struct list_head cli_chain;
399         struct obd_device *cli_obd;
400         unsigned int cli_id;
401         unsigned long cli_prealloc_quota;
402         struct list_head cli_prealloc_inodes;
403 };
404
405
406 struct obd_prealloc_inode {
407         struct list_head obd_prealloc_chain;
408         unsigned long inode;
409 };
410
411 /* generic operations shared by various OBD types */
412 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data);
413 int gen_multi_cleanup(struct obd_device *obddev);
414 int gen_multi_attach(struct obd_device *obddev, uint32_t len, void *data);
415 int gen_multi_detach(struct obd_device *obddev);
416 int gen_connect (struct obd_conn *conn);
417 int gen_disconnect(struct obd_conn *conn);
418 struct obd_client *gen_client(struct obd_conn *);
419 int gen_cleanup(struct obd_device *obddev);
420 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
421                   struct obd_conn *src_conn, struct obdo *src,
422                   obd_size count, obd_off offset);
423
424
425
426 /*
427  *  ======== OBD IOCL Declarations ===========
428  */
429 struct oic_generic {
430         int  att_connid;
431         int  att_typelen;
432         void *att_type;
433         int  att_datalen;
434         void *att_data;
435 };
436
437
438 /* for preallocation */
439 struct oic_prealloc_s {
440         uint32_t cli_id;
441         uint32_t alloc; /* user sets it to the number of inodes
442                          * requesting to be preallocated.  kernel
443                          * sets it to the actual number of
444                          * succesfully preallocated inodes */
445         obd_id  ids[32]; /* actual inode numbers */
446 };
447
448 /* for getattr, setattr, create, destroy */
449 struct oic_attr_s {
450         uint32_t conn_id;
451         struct obdo  obdo;
452 };
453
454 /* for copy, migrate */
455 struct ioc_mv_s {
456         unsigned int src_conn_id;
457         struct obdo  src;
458         unsigned int dst_conn_id;
459         struct obdo  dst;
460 };
461
462 /* for read/write */
463 struct oic_rw_s {
464         uint32_t conn_id;
465         struct obdo obdo;
466         char * buf;
467         obd_size count;
468         obd_off offset;
469 };
470
471 /* for punch, sync */
472 struct oic_range_s {
473         uint32_t conn_id;
474         struct obdo obdo;
475         obd_size count;
476         obd_off offset;
477 };
478
479
480 #define OBD_IOC_CREATE                 _IOR ('f',  3, long)
481 #define OBD_IOC_SETUP                  _IOW ('f',  4, long)
482 #define OBD_IOC_CLEANUP                _IO  ('f',  5      )
483 #define OBD_IOC_DESTROY                _IOW ('f',  6, long)
484 #define OBD_IOC_PREALLOCATE            _IOWR('f',  7, long)
485 #define OBD_IOC_DEC_USE_COUNT          _IO  ('f',  8      )
486 #define OBD_IOC_SETATTR                _IOW ('f',  9, long)
487 #define OBD_IOC_GETATTR                _IOR ('f', 10, long)
488 #define OBD_IOC_READ                   _IOWR('f', 11, long)
489 #define OBD_IOC_WRITE                  _IOWR('f', 12, long)
490 #define OBD_IOC_CONNECT                _IOR ('f', 13, long)
491 #define OBD_IOC_DISCONNECT             _IOW ('f', 14, long)
492 #define OBD_IOC_STATFS                 _IOWR('f', 15, long)
493 #define OBD_IOC_SYNC                   _IOR ('f',  16, long)
494 #define OBD_IOC_READ2                  _IOWR('f', 17, long)
495 #define OBD_IOC_FORMAT                 _IOWR('f', 18, long)
496 #define OBD_IOC_PARTITION              _IOWR('f', 19, long)
497 #define OBD_IOC_ATTACH                 _IOWR('f', 20, long)
498 #define OBD_IOC_DETACH                 _IOWR('f', 21, long)
499 #define OBD_IOC_COPY                   _IOWR('f', 22, long)
500 #define OBD_IOC_MIGR                   _IOWR('f', 23, long)
501
502 #define OBD_IOC_DEC_FS_USE_COUNT       _IO  ('f', 32      )
503
504
505 /* sysctl.c */
506 extern void obd_sysctl_init (void);
507 extern void obd_sysctl_clean (void);
508
509 #define CHKCONN(conn)   do { if (!gen_client(conn)) {\
510                 printk("%s %d invalid client %u\n", __FILE__, __LINE__, \
511                        conn->oc_id);\
512                 return -EINVAL; }} while (0) 
513
514
515
516 #endif /* __LINUX_CLASS_OBD_H */