Whamcloud - gitweb
small changes and fixes to get the filter OBD working fully.
[fs/lustre-release.git] / lustre / include / linux / obd_class.h
1 #ifndef __LINUX_CLASS_OBD_H
2 #define __LINUX_CLASS_OBD_H
3 /*
4  * Copyright (C) 2001  Cluster File Systems, Inc.
5  *
6  * This code is issued under the GNU General Public License.
7  * See the file COPYING in this distribution
8  */
9
10 #ifndef __KERNEL__
11 #include <stdint.h>
12 #define __KERNEL__
13 #include <linux/list.h>
14 #undef __KERNEL__
15 #else 
16 #include <asm/segment.h>
17 #include <asm/uaccess.h>
18 #include <linux/types.h>
19 #include <linux/fs.h>
20 #include <linux/time.h>
21 #include <linux/obd.h>
22
23 #include <linux/obd_ext2.h>
24 #include <linux/obd_filter.h>
25 #include <linux/obd_snap.h>
26 #include <linux/obd_trace.h>
27 /* #include <linux/obd_fc.h> */
28 #include <linux/obd_raid1.h>
29 #include <linux/obd_rpc.h>
30 #endif
31
32
33 /*
34  *  ======== OBD type Declarations ===========
35  */
36 #define MIN(a,b) (((a)<(b)) ? (a): (b))
37 #define MAX(a,b) (((a)>(b)) ? (a): (b))
38 #define MKSTR(ptr) ((ptr))? (ptr) : ""
39
40 static inline int size_round(int val)
41 {
42         return (val + 3) & (~0x3);
43 }
44
45 static inline size_t round_strlen(char *str)
46 {
47         return size_round(strlen(str) + 1);
48 }
49
50 #ifdef __KERNEL__
51 static inline char *strdup(char *str)
52 {
53         char *tmp = kmalloc(strlen(str) + 1, GFP_KERNEL);
54         if (tmp)
55                 memcpy(tmp, str, strlen(str) + 1);
56                 
57         return NULL;
58 }
59 #endif
60 /* 
61  * copy sizeof(type) bytes from pointer to var and move ptr forward.
62  * return EFAULT if pointer goes beyond end
63  */
64 #define UNLOGV(var,type,ptr,end) do {var = *(type *)ptr; ptr += sizeof(type); if (ptr > end ) return -EFAULT;} while (0)
65
66
67 /* the following two macros convert to little endian */
68 /* type must be 32 or 64 */
69 #define LUNLOGV(var,type,ptr,end)                \
70 do {                                            \
71         var = le_to_cpu##type(*(type *)ptr);         \
72         ptr += sizeof(type);                    \
73         if (ptr > end )                         \
74                 return -EFAULT;                 \
75 } while (0)
76
77 /* now log values */
78 #define LOGV(var,type,ptr)                      \
79 do {                                            \
80         *((type *)ptr) = var;                   \
81         ptr += sizeof(type);                    \
82 } while (0)
83
84 /* and in network order */
85 #define LLOGV(var,type,ptr)                     \
86 do {                                            \
87         *((type *)ptr) = cpu_to_le##type(var);  \
88         ptr += sizeof(type);                    \
89 } while (0)
90
91 /* 
92  * set var to point at (type *)ptr, move ptr forward with sizeof(type)
93  * return from function with EFAULT if ptr goes beyond end
94  */
95 #define UNLOGP(var,type,ptr,end) do {var = (type *)ptr; ptr += sizeof(type); if (ptr > end ) return -EFAULT; } while (0)
96
97 /* 
98  * set var to point at (char *)ptr, move ptr forward by size_round(len);
99  * return from function with EFAULT if ptr goes beyond end
100  */
101 #define UNLOGL(var,type,len,ptr,end) do {var = (type *)ptr; ptr += size_round(len * sizeof(type)); if (ptr > end ) return -EFAULT; } while (0)
102
103
104 #define LOGL(var,len,ptr) do {memcpy((char *)ptr, (const char *)var, len); ptr += size_round(len);} while (0)
105
106 typedef uint64_t        obd_id;
107 typedef uint64_t        obd_gr;
108 typedef uint64_t        obd_time;
109 typedef uint64_t        obd_size;
110 typedef uint64_t        obd_off;
111 typedef uint64_t        obd_blocks;
112 typedef uint32_t        obd_blksize;
113 typedef uint32_t        obd_mode;
114 typedef uint32_t        obd_uid;
115 typedef uint32_t        obd_gid;
116 typedef uint32_t        obd_rdev;
117 typedef uint32_t        obd_flag;
118 typedef uint32_t        obd_count;
119
120
121
122 #define OBD_FL_INLINEDATA       (0x00000001UL)  
123 #define OBD_FL_OBDMDEXISTS      (0x00000002UL)
124
125 #define OBD_INLINESZ    60
126 #define OBD_OBDMDSZ     60
127 /* Note: 64-bit types are 64-bit aligned in structure */
128 struct obdo {
129         obd_id                  o_id;
130         obd_gr                  o_gr;
131         obd_time                o_atime;
132         obd_time                o_mtime;
133         obd_time                o_ctime;
134         obd_size                o_size;
135         obd_blocks              o_blocks;
136         obd_blksize             o_blksize;
137         obd_mode                o_mode;
138         obd_uid                 o_uid;
139         obd_gid                 o_gid;
140         obd_flag                o_flags;
141         obd_flag                o_obdflags;
142         obd_count               o_nlink;
143         obd_count               o_generation;
144         obd_flag                o_valid;        /* hot fields in this obdo */
145         char                    o_inline[OBD_INLINESZ];
146         char                    o_obdmd[OBD_OBDMDSZ];
147         struct list_head        o_list;
148         struct obd_ops          *o_op;
149 };
150
151
152 struct obd_ioctl_data { 
153         uint32_t ioc_len;
154         uint32_t ioc_version;
155         uint32_t ioc_conn1;
156         uint32_t ioc_conn2;
157         struct obdo ioc_obdo1;
158         struct obdo ioc_obdo2;
159         obd_size         ioc_count;
160         obd_off          ioc_offset;
161         uint32_t         ioc_dev;
162
163         /* buffers the kernel will treat as user pointers */
164         uint32_t ioc_plen1;
165         char    *ioc_pbuf1;
166         uint32_t ioc_plen2;
167         char    *ioc_pbuf2;
168
169         /* two inline buffers */
170         uint32_t ioc_inllen1;
171         char    *ioc_inlbuf1;
172         uint32_t ioc_inllen2;
173         char    *ioc_inlbuf2;
174
175         char    ioc_bulk[0];
176 };
177
178 #define OBD_MD_FLALL    (~0UL)
179 #define OBD_MD_FLID     (0x00000001UL)
180 #define OBD_MD_FLATIME  (0x00000002UL)
181 #define OBD_MD_FLMTIME  (0x00000004UL)
182 #define OBD_MD_FLCTIME  (0x00000008UL)
183 #define OBD_MD_FLSIZE   (0x00000010UL)
184 #define OBD_MD_FLBLOCKS (0x00000020UL)
185 #define OBD_MD_FLBLKSZ  (0x00000040UL)
186 #define OBD_MD_FLMODE   (0x00000080UL)
187 #define OBD_MD_FLTYPE   (0x00000100UL)
188 #define OBD_MD_FLUID    (0x00000200UL)
189 #define OBD_MD_FLGID    (0x00000400UL)
190 #define OBD_MD_FLFLAGS  (0x00000800UL)
191 #define OBD_MD_FLOBDFLG (0x00001000UL)
192 #define OBD_MD_FLNLINK  (0x00002000UL)
193 #define OBD_MD_FLGENER  (0x00004000UL)
194 #define OBD_MD_FLINLINE (0x00008000UL)
195 #define OBD_MD_FLOBDMD  (0x00010000UL)
196 #define OBD_MD_FLNOTOBD (~(OBD_MD_FLOBDMD | OBD_MD_FLOBDFLG | OBD_MD_FLBLOCKS))
197
198 /*
199  *  ======== OBD Device Declarations ===========
200  */
201
202
203 #define OBD_PSDEV_MAJOR 186
204 #define MAX_OBD_DEVICES 8
205 #define MAX_MULTI       16
206
207
208 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
209
210
211 #define OBD_ATTACHED 0x1
212 #define OBD_SET_UP   0x2
213
214 struct obd_conn {
215         struct obd_device *oc_dev;
216         uint32_t oc_id;
217 };
218
219 typedef struct {
220         uint32_t len;
221         char *   name;
222         struct dentry *dentry;   /* file system obd device names */
223         __u8           _uuid[16]; /* uuid obd device names */
224 } obd_devicename;
225
226
227 #ifdef __KERNEL__
228 /* corresponds to one of the obdx */
229 struct obd_device {
230         struct obd_type *obd_type;
231         int obd_minor;
232         int obd_flags;
233         int obd_refcnt; 
234         obd_devicename obd_fsname; 
235         struct proc_dir_entry *obd_proc_entry;
236         int obd_multi_count;
237         struct obd_conn obd_multi_conn[MAX_MULTI];
238         unsigned int obd_gen_last_id;
239         unsigned long obd_gen_prealloc_quota;
240         struct list_head obd_gen_clients;
241         union {
242                 struct ext2_obd ext2;
243                 struct filter_obd filter;
244                 struct raid1_obd raid1;
245                 struct snap_obd snap;
246                 struct trace_obd trace;
247                 /* struct fc_obd fc; */
248         } u;
249 };
250
251 extern struct proc_dir_entry *proc_lustre_register_obd_device(struct obd_device *obd);
252 extern void proc_lustre_release_obd_device(struct obd_device *obd);
253 extern void proc_lustre_remove_obd_entry(const char* name, struct obd_device *obd);
254
255 /*
256  *  ======== OBD Operations Declarations ===========
257  */
258
259 #define OBD_BRW_READ    (READ)
260 #define OBD_BRW_WRITE   (WRITE)
261 #define OBD_BRW_RWMASK  (READ | WRITE)
262 #define OBD_BRW_CREATE  (0x00000010UL)
263
264 struct obd_ops {
265         int (*o_iocontrol)(int cmd, struct obd_conn *, int len, void *karg,
266                            void *uarg);
267         int (*o_get_info)(struct obd_conn *, obd_count keylen, void *key,
268                           obd_count *vallen, void **val);
269         int (*o_set_info)(struct obd_conn *, obd_count keylen, void *key,
270                           obd_count vallen, void *val);
271         int (*o_attach)(struct obd_device *dev, obd_count len, void *data);
272         int (*o_detach)(struct obd_device *dev);
273         int (*o_setup) (struct obd_device *dev, obd_count len, void *data);
274         int (*o_cleanup)(struct obd_device *dev);
275         int (*o_connect)(struct obd_conn *conn);
276         int (*o_disconnect)(struct obd_conn *conn);
277         int (*o_statfs)(struct obd_conn *conn, struct statfs *statfs);
278         int (*o_preallocate)(struct obd_conn *, obd_count *req, obd_id *ids);
279         int (*o_create)(struct obd_conn *conn,  struct obdo *oa);
280         int (*o_destroy)(struct obd_conn *conn, struct obdo *oa);
281         int (*o_setattr)(struct obd_conn *conn, struct obdo *oa);
282         int (*o_getattr)(struct obd_conn *conn, struct obdo *oa);
283         int (*o_read)(struct obd_conn *conn, struct obdo *oa, char *buf,
284                       obd_size *count, obd_off offset);
285         int (*o_write)(struct obd_conn *conn, struct obdo *oa, char *buf,
286                        obd_size *count, obd_off offset);
287         int (*o_brw)(int rw, struct obd_conn *conn, obd_count num_oa,
288                      struct obdo **oa, obd_count *oa_bufs, struct page **buf,
289                      obd_size *count, obd_off *offset, obd_flag *flags);
290         int (*o_punch)(struct obd_conn *conn, struct obdo *tgt, obd_size count,
291                        obd_off offset);
292         int (*o_sync)(struct obd_conn *conn, struct obdo *tgt, obd_size count,
293                       obd_off offset);
294         int (*o_migrate)(struct obd_conn *conn, struct obdo *dst,
295                          struct obdo *src, obd_size count, obd_off offset);
296         int (*o_copy)(struct obd_conn *dstconn, struct obdo *dst,
297                       struct obd_conn *srconn, struct obdo *src,
298                       obd_size count, obd_off offset);
299         int (*o_iterate)(struct obd_conn *conn, int (*)(obd_id, obd_gr, void *),
300                          obd_id *startid, obd_gr group, void *data);
301 };
302
303 #define OBT(dev)        dev->obd_type->typ_ops
304 #define OBP(dev,op)     dev->obd_type->typ_ops->o_ ## op
305
306 #endif 
307
308 /* This value is not arbitrarily chosen.  KIO_STATIC_PAGES from linux/iobuf.h */
309 #define MAX_IOVEC       (KIO_STATIC_PAGES - 1)
310
311
312 /*
313  *  ======== OBD Metadata Support  ===========
314  */
315
316 extern int obd_init_obdo_cache(void);
317 extern void obd_cleanup_obdo_cache(void);
318
319
320 static inline int obdo_has_inline(struct obdo *obdo)
321 {
322         return (obdo->o_valid & OBD_MD_FLINLINE &&
323                 obdo->o_obdflags & OBD_FL_INLINEDATA);
324 };
325
326 static inline int obdo_has_obdmd(struct obdo *obdo)
327 {
328         return (obdo->o_valid & OBD_MD_FLOBDMD &&
329                 obdo->o_obdflags & OBD_FL_OBDMDEXISTS);
330 };
331
332 #ifdef __KERNEL__
333 /* support routines */
334 extern kmem_cache_t *obdo_cachep;
335
336 static __inline__ struct obdo *obdo_alloc(void)
337 {
338         struct obdo *oa = NULL;
339
340         oa = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
341         memset(oa, 0, sizeof (*oa));
342
343         return oa;
344 }
345
346 static __inline__ void obdo_free(struct obdo *oa)
347 {
348         if ( !oa ) 
349                 return;
350         kmem_cache_free(obdo_cachep, oa);
351 }
352
353
354
355 static __inline__ struct obdo *obdo_fromid(struct obd_conn *conn, obd_id id,
356                                            obd_mode mode, obd_flag valid)
357 {
358         struct obdo *oa;
359         int err;
360
361         ENTRY;
362         oa = obdo_alloc();
363         if ( !oa ) {
364                 EXIT;
365                 return ERR_PTR(-ENOMEM);
366         }
367
368         oa->o_id = id;
369         oa->o_mode = mode;
370         oa->o_valid = valid;
371         if ((err = OBP(conn->oc_dev, getattr)(conn, oa))) {
372                 obdo_free(oa);
373                 EXIT;
374                 return ERR_PTR(err);
375         }
376         EXIT;
377         return oa;
378 }
379
380 static inline void obdo_from_iattr(struct obdo *oa, struct iattr *attr)
381 {
382         unsigned int ia_valid = attr->ia_valid;
383
384         if (ia_valid & ATTR_ATIME) {
385                 oa->o_atime = attr->ia_atime;
386                 oa->o_valid |= OBD_MD_FLATIME;
387         }
388         if (ia_valid & ATTR_MTIME) {
389                 oa->o_mtime = attr->ia_mtime;
390                 oa->o_valid |= OBD_MD_FLMTIME;
391         }
392         if (ia_valid & ATTR_CTIME) {
393                 oa->o_ctime = attr->ia_ctime;
394                 oa->o_valid |= OBD_MD_FLCTIME;
395         }
396         if (ia_valid & ATTR_SIZE) {
397                 oa->o_size = attr->ia_size;
398                 oa->o_valid |= OBD_MD_FLSIZE;
399         }
400         if (ia_valid & ATTR_MODE) {
401                 oa->o_mode = attr->ia_mode;
402                 oa->o_valid |= OBD_MD_FLMODE;
403                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
404                         oa->o_mode &= ~S_ISGID;
405         }
406         if (ia_valid & ATTR_UID)
407         {
408                 oa->o_uid = attr->ia_uid;
409                 oa->o_valid |= OBD_MD_FLUID;
410         }
411         if (ia_valid & ATTR_GID) {
412                 oa->o_gid = attr->ia_gid;
413                 oa->o_valid |= OBD_MD_FLGID;
414         }
415 }
416
417
418 static inline void iattr_from_obdo(struct iattr *attr, struct obdo *oa)
419 {
420         unsigned int ia_valid = oa->o_valid;
421         
422         memset(attr, 0, sizeof(*attr));
423         if (ia_valid & OBD_MD_FLATIME) {
424                 attr->ia_atime = oa->o_atime;
425                 attr->ia_valid |= ATTR_ATIME;
426         }
427         if (ia_valid & OBD_MD_FLMTIME) {
428                 attr->ia_mtime = oa->o_mtime;
429                 attr->ia_valid |= ATTR_MTIME;
430         }
431         if (ia_valid & OBD_MD_FLCTIME) {
432                 attr->ia_ctime = oa->o_ctime;
433                 attr->ia_valid |= ATTR_CTIME;
434         }
435         if (ia_valid & OBD_MD_FLSIZE) {
436                 attr->ia_size = oa->o_size;
437                 attr->ia_valid |= ATTR_SIZE;
438         }
439         if (ia_valid & OBD_MD_FLMODE) {
440                 attr->ia_mode = oa->o_mode;
441                 attr->ia_valid |= ATTR_MODE;
442                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
443                         attr->ia_mode &= ~S_ISGID;
444         }
445         if (ia_valid & OBD_MD_FLUID)
446         {
447                 attr->ia_uid = oa->o_uid;
448                 attr->ia_valid |= ATTR_UID;
449         }
450         if (ia_valid & OBD_MD_FLGID) {
451                 attr->ia_gid = oa->o_gid;
452                 attr->ia_valid |= ATTR_GID;
453         }
454 }
455
456
457 /* WARNING: the file systems must take care not to tinker with
458    attributes they don't manage (such as blocks). */
459
460 static __inline__ void obdo_from_inode(struct obdo *dst, struct inode *src)
461 {
462         if ( dst->o_valid & OBD_MD_FLID )
463                 dst->o_id = src->i_ino;
464         if ( dst->o_valid & OBD_MD_FLATIME )
465                 dst->o_atime = src->i_atime;
466         if ( dst->o_valid & OBD_MD_FLMTIME )
467                 dst->o_mtime = src->i_mtime;
468         if ( dst->o_valid & OBD_MD_FLCTIME )
469                 dst->o_ctime = src->i_ctime;
470         if ( dst->o_valid & OBD_MD_FLSIZE )
471                 dst->o_size = src->i_size;
472         if ( dst->o_valid & OBD_MD_FLBLOCKS )   /* allocation of space */
473                 dst->o_blocks = src->i_blocks;
474         if ( dst->o_valid & OBD_MD_FLBLKSZ )
475                 dst->o_blksize = src->i_blksize;
476         if ( dst->o_valid & OBD_MD_FLMODE )
477                 dst->o_mode = src->i_mode;
478         if ( dst->o_valid & OBD_MD_FLUID )
479                 dst->o_uid = src->i_uid;
480         if ( dst->o_valid & OBD_MD_FLGID )
481                 dst->o_gid = src->i_gid;
482         if ( dst->o_valid & OBD_MD_FLFLAGS )
483                 dst->o_flags = src->i_flags;
484         if ( dst->o_valid & OBD_MD_FLNLINK )
485                 dst->o_nlink = src->i_nlink;
486         if ( dst->o_valid & OBD_MD_FLGENER ) 
487                 dst->o_generation = src->i_generation;
488 }
489
490 static __inline__ void obdo_to_inode(struct inode *dst, struct obdo *src)
491 {
492
493         if ( src->o_valid & OBD_MD_FLID )
494                 dst->i_ino = src->o_id;
495         if ( src->o_valid & OBD_MD_FLATIME ) 
496                 dst->i_atime = src->o_atime;
497         if ( src->o_valid & OBD_MD_FLMTIME ) 
498                 dst->i_mtime = src->o_mtime;
499         if ( src->o_valid & OBD_MD_FLCTIME ) 
500                 dst->i_ctime = src->o_ctime;
501         if ( src->o_valid & OBD_MD_FLSIZE ) 
502                 dst->i_size = src->o_size;
503         if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
504                 dst->i_blocks = src->o_blocks;
505         if ( src->o_valid & OBD_MD_FLBLKSZ )
506                 dst->i_blksize = src->o_blksize;
507         if ( src->o_valid & OBD_MD_FLMODE ) 
508                 dst->i_mode = src->o_mode;
509         if ( src->o_valid & OBD_MD_FLUID ) 
510                 dst->i_uid = src->o_uid;
511         if ( src->o_valid & OBD_MD_FLGID ) 
512                 dst->i_gid = src->o_gid;
513         if ( src->o_valid & OBD_MD_FLFLAGS ) 
514                 dst->i_flags = src->o_flags;
515         if ( src->o_valid & OBD_MD_FLNLINK )
516                 dst->i_nlink = src->o_nlink;
517         if ( src->o_valid & OBD_MD_FLGENER )
518                 dst->i_generation = src->o_generation;
519 }
520
521 #endif 
522
523 static __inline__ void obdo_cpy_md(struct obdo *dst, struct obdo *src)
524 {
525 #ifdef __KERNEL__
526         CDEBUG(D_INODE, "src obdo %Ld valid 0x%x, dst obdo %Ld\n",
527                src->o_id, src->o_valid, dst->o_id);
528 #endif
529         if ( src->o_valid & OBD_MD_FLATIME ) 
530                 dst->o_atime = src->o_atime;
531         if ( src->o_valid & OBD_MD_FLMTIME ) 
532                 dst->o_mtime = src->o_mtime;
533         if ( src->o_valid & OBD_MD_FLCTIME ) 
534                 dst->o_ctime = src->o_ctime;
535         if ( src->o_valid & OBD_MD_FLSIZE ) 
536                 dst->o_size = src->o_size;
537         if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
538                 dst->o_blocks = src->o_blocks;
539         if ( src->o_valid & OBD_MD_FLBLKSZ )
540                 dst->o_blksize = src->o_blksize;
541         if ( src->o_valid & OBD_MD_FLMODE ) 
542                 dst->o_mode = src->o_mode;
543         if ( src->o_valid & OBD_MD_FLUID ) 
544                 dst->o_uid = src->o_uid;
545         if ( src->o_valid & OBD_MD_FLGID ) 
546                 dst->o_gid = src->o_gid;
547         if ( src->o_valid & OBD_MD_FLFLAGS ) 
548                 dst->o_flags = src->o_flags;
549         /*
550         if ( src->o_valid & OBD_MD_FLOBDFLG ) 
551                 dst->o_obdflags = src->o_obdflags;
552         */
553         if ( src->o_valid & OBD_MD_FLNLINK ) 
554                 dst->o_nlink = src->o_nlink;
555         if ( src->o_valid & OBD_MD_FLGENER ) 
556                 dst->o_generation = src->o_generation;
557         if ( src->o_valid & OBD_MD_FLINLINE &&
558              src->o_obdflags & OBD_FL_INLINEDATA) {
559                 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
560                 dst->o_obdflags |= OBD_FL_INLINEDATA;
561         }
562         if ( src->o_valid & OBD_MD_FLOBDMD &&
563              src->o_obdflags & OBD_FL_OBDMDEXISTS) {
564                 memcpy(dst->o_obdmd, src->o_obdmd, sizeof(src->o_obdmd));
565                 dst->o_obdflags |= OBD_FL_OBDMDEXISTS;
566         }
567
568         dst->o_valid |= src->o_valid;
569 }
570
571
572 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
573 static __inline__ int obdo_cmp_md(struct obdo *dst, struct obdo *src,
574                                   obd_flag compare)
575 {
576         int res = 0;
577
578         if ( compare & OBD_MD_FLATIME )
579                 res = (res || (dst->o_atime != src->o_atime));
580         if ( compare & OBD_MD_FLMTIME )
581                 res = (res || (dst->o_mtime != src->o_mtime));
582         if ( compare & OBD_MD_FLCTIME )
583                 res = (res || (dst->o_ctime != src->o_ctime));
584         if ( compare & OBD_MD_FLSIZE )
585                 res = (res || (dst->o_size != src->o_size));
586         if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
587                 res = (res || (dst->o_blocks != src->o_blocks));
588         if ( compare & OBD_MD_FLBLKSZ )
589                 res = (res || (dst->o_blksize != src->o_blksize));
590         if ( compare & OBD_MD_FLMODE )
591                 res = (res || (dst->o_mode != src->o_mode));
592         if ( compare & OBD_MD_FLUID )
593                 res = (res || (dst->o_uid != src->o_uid));
594         if ( compare & OBD_MD_FLGID )
595                 res = (res || (dst->o_gid != src->o_gid));
596         if ( compare & OBD_MD_FLFLAGS ) 
597                 res = (res || (dst->o_flags != src->o_flags));
598         if ( compare & OBD_MD_FLNLINK )
599                 res = (res || (dst->o_nlink != src->o_nlink));
600         if ( compare & OBD_MD_FLGENER )
601                 res = (res || (dst->o_generation != src->o_generation));
602         /* XXX Don't know if thses should be included here - wasn't previously
603         if ( compare & OBD_MD_FLINLINE )
604                 res = (res || memcmp(dst->o_inline, src->o_inline));
605         if ( compare & OBD_MD_FLOBDMD )
606                 res = (res || memcmp(dst->o_obdmd, src->o_obdmd));
607         */
608         return res;
609 }
610
611
612 #ifdef __KERNEL__
613 int obd_register_type(struct obd_ops *ops, char *nm);
614 int obd_unregister_type(char *nm);
615
616 struct obd_client {
617         struct list_head cli_chain;
618         struct obd_device *cli_obd;
619         unsigned int cli_id;
620         unsigned long cli_prealloc_quota;
621         struct list_head cli_prealloc_inodes;
622 };
623
624
625 struct obd_prealloc_inode {
626         struct list_head obd_prealloc_chain;
627         unsigned long inode;
628 };
629
630 /* generic operations shared by various OBD types */
631 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data);
632 int gen_multi_cleanup(struct obd_device *obddev);
633 int gen_multi_attach(struct obd_device *obddev, uint32_t len, void *data);
634 int gen_multi_detach(struct obd_device *obddev);
635 int gen_connect (struct obd_conn *conn);
636 int gen_disconnect(struct obd_conn *conn);
637 struct obd_client *gen_client(const struct obd_conn *);
638 int gen_cleanup(struct obd_device *obddev);
639 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
640                   struct obd_conn *src_conn, struct obdo *src,
641                   obd_size count, obd_off offset);
642
643 #endif
644
645 /*
646  *  ======== OBD IOCL Declarations ===========
647  */
648
649 #define OBD_IOCTL_VERSION 0x00010001
650
651 struct obd_ioctl_hdr { 
652         uint32_t ioc_len;
653         uint32_t ioc_version;
654 };
655
656 static inline int obd_ioctl_packlen(struct obd_ioctl_data *data)
657 {
658         int len = sizeof(struct obd_ioctl_data);
659         len += size_round(data->ioc_inllen1);
660         len += size_round(data->ioc_inllen2);
661         return len;
662 }
663
664 static inline int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
665 {
666         if (data->ioc_len > (1<<30)) { 
667                 printk("OBD ioctl: ioc_len larger than 1<<30\n");
668                 return 1;
669         }
670         if (data->ioc_inllen1 > (1<<30)) { 
671                 printk("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
672                 return 1;
673         }
674         if (data->ioc_inllen2 > (1<<30)) { 
675                 printk("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
676                 return 1;
677         }
678         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
679                 printk("OBD ioctl: inlbuf1 pointer but 0 length\n");
680                 return 1;
681         }
682         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
683                 printk("OBD ioctl: inlbuf2 pointer but 0 length\n");
684                 return 1;
685         }
686         if (data->ioc_pbuf1 && !data->ioc_plen1) {
687                 printk("OBD ioctl: pbuf1 pointer but 0 length\n");
688                 return 1;
689         }
690         if (data->ioc_pbuf2 && !data->ioc_plen2) {
691                 printk("OBD ioctl: pbuf2 pointer but 0 length\n");
692                 return 1;
693         }
694         if (obd_ioctl_packlen(data) != data->ioc_len ) {
695                 printk("OBD ioctl: packlen exceeds ioc_len\n");
696                 return 1;
697         }
698         if (data->ioc_inllen1 && 
699             data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') { 
700                 printk("OBD ioctl: inlbuf1 not 0 terminated\n");
701                 return 1;
702         }
703         if (data->ioc_inllen2 && 
704             data->ioc_bulk[size_round(data->ioc_inllen1) + data->ioc_inllen2 - 1] != '\0') { 
705                 printk("OBD ioctl: inlbuf2 not 0 terminated\n");
706                 return 1;
707         }
708         return 0;
709 }
710
711 #ifndef __KERNEL__
712 static inline int obd_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max)
713 {
714         char *ptr;
715         struct obd_ioctl_data *overlay;
716         data->ioc_len = obd_ioctl_packlen(data);
717         data->ioc_version = OBD_IOCTL_VERSION;
718
719         if (*pbuf && obd_ioctl_packlen(data) > max) 
720                 return 1;
721         if (*pbuf == NULL) { 
722                 *pbuf = malloc(data->ioc_len);
723         }
724         if (!*pbuf)
725                 return 1;
726         overlay = (struct obd_ioctl_data *)*pbuf;
727         memcpy(*pbuf, data, sizeof(*data));
728
729         ptr = overlay->ioc_bulk;
730         if (data->ioc_inlbuf1)
731                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
732         if (data->ioc_inlbuf2)
733                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
734         if (obd_ioctl_is_invalid(overlay))
735                 return 1;
736
737         return 0;
738 }
739 #else
740
741
742 /* buffer MUST be at least the size of obd_ioctl_hdr */
743 static inline int obd_ioctl_getdata(char *buf, char *end, void *arg)
744 {
745         struct obd_ioctl_hdr *hdr;
746         struct obd_ioctl_data *data;
747         int err;
748         ENTRY;
749
750         hdr = (struct obd_ioctl_hdr *)buf;
751         data = (struct obd_ioctl_data *)buf;
752
753         err = copy_from_user(buf, (void *)arg, sizeof(*hdr));
754         if ( err ) {
755                 EXIT;
756                 return err;
757         }
758
759         if (hdr->ioc_version != OBD_IOCTL_VERSION) { 
760                 printk("OBD: version mismatch kernel vs application\n");
761                 return -EINVAL;
762         }
763
764         if (hdr->ioc_len + buf >= end) { 
765                 printk("OBD: user buffer exceeds kernel buffer\n");
766                 return -EINVAL;
767         }
768
769
770         if (hdr->ioc_len < sizeof(struct obd_ioctl_data)) { 
771                 printk("OBD: user buffer too small for ioctl\n");
772                 return -EINVAL;
773         }
774
775         err = copy_from_user(buf, (void *)arg, hdr->ioc_len);
776         if ( err ) {
777                 EXIT;
778                 return err;
779         }
780
781         if (obd_ioctl_is_invalid(data)) { 
782                 printk("OBD: ioctl not correctly formatted\n");
783                 return -EINVAL;
784         }
785
786         if (data->ioc_inllen1) { 
787                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
788         }
789
790         if (data->ioc_inllen2) { 
791                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + size_round(data->ioc_inllen1);
792         }
793
794         EXIT;
795         return 0;
796 }
797 #endif
798
799
800 #define OBD_IOC_CREATE                 _IOR ('f',  3, long)
801 #define OBD_IOC_SETUP                  _IOW ('f',  4, long)
802 #define OBD_IOC_CLEANUP                _IO  ('f',  5      )
803 #define OBD_IOC_DESTROY                _IOW ('f',  6, long)
804 #define OBD_IOC_PREALLOCATE            _IOWR('f',  7, long)
805 #define OBD_IOC_DEC_USE_COUNT          _IO  ('f',  8      )
806 #define OBD_IOC_SETATTR                _IOW ('f',  9, long)
807 #define OBD_IOC_GETATTR                _IOR ('f', 10, long)
808 #define OBD_IOC_READ                   _IOWR('f', 11, long)
809 #define OBD_IOC_WRITE                  _IOWR('f', 12, long)
810 #define OBD_IOC_CONNECT                _IOR ('f', 13, long)
811 #define OBD_IOC_DISCONNECT             _IOW ('f', 14, long)
812 #define OBD_IOC_STATFS                 _IOWR('f', 15, long)
813 #define OBD_IOC_SYNC                   _IOR ('f', 16, long)
814 #define OBD_IOC_READ2                  _IOWR('f', 17, long)
815 #define OBD_IOC_FORMAT                 _IOWR('f', 18, long)
816 #define OBD_IOC_PARTITION              _IOWR('f', 19, long)
817 #define OBD_IOC_ATTACH                 _IOWR('f', 20, long)
818 #define OBD_IOC_DETACH                 _IOWR('f', 21, long)
819 #define OBD_IOC_COPY                   _IOWR('f', 22, long)
820 #define OBD_IOC_MIGR                   _IOWR('f', 23, long)
821 #define OBD_IOC_PUNCH                  _IOWR('f', 24, long)
822 #define OBD_IOC_DEVICE                 _IOWR('f', 25, long)
823
824 #define OBD_IOC_DEC_FS_USE_COUNT       _IO  ('f', 32      )
825
826
827 /* sysctl.c */
828 extern void obd_sysctl_init (void);
829 extern void obd_sysctl_clean (void);
830
831 #define CHKCONN(conn)   do { if (!gen_client(conn)) {\
832                 printk("%s %d invalid client %u\n", __FILE__, __LINE__, \
833                        conn->oc_id);\
834                 return -EINVAL; }} while (0) 
835
836
837
838 #endif /* __LINUX_CLASS_OBD_H */