Whamcloud - gitweb
Numerous changes:
[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_FLUID    (0x00000100UL)
188 #define OBD_MD_FLGID    (0x00000200UL)
189 #define OBD_MD_FLFLAGS  (0x00000400UL)
190 #define OBD_MD_FLOBDFLG (0x00000800UL)
191 #define OBD_MD_FLNLINK  (0x00001000UL)
192 #define OBD_MD_FLGENER  (0x00002000UL)
193 #define OBD_MD_FLINLINE (0x00004000UL)
194 #define OBD_MD_FLOBDMD  (0x00008000UL)
195 #define OBD_MD_FLNOTOBD (~(OBD_MD_FLOBDMD | OBD_MD_FLOBDFLG | OBD_MD_FLBLOCKS))
196
197 /*
198  *  ======== OBD Device Declarations ===========
199  */
200
201
202 #define OBD_PSDEV_MAJOR 186
203 #define MAX_OBD_DEVICES 8
204 #define MAX_MULTI       16
205
206
207 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
208
209
210 #define OBD_ATTACHED 0x1
211 #define OBD_SET_UP   0x2
212
213 struct obd_conn {
214         struct obd_device *oc_dev;
215         uint32_t oc_id;
216 };
217
218 typedef struct {
219         uint32_t len;
220         char *   name;
221         struct dentry *dentry;   /* file system obd device names */
222         __u8           _uuid[16]; /* uuid obd device names */
223 } obd_devicename;
224
225
226 #ifdef __KERNEL__
227 /* corresponds to one of the obdx */
228 struct obd_device {
229         struct obd_type *obd_type;
230         int obd_minor;
231         int obd_flags;
232         int obd_refcnt; 
233         obd_devicename obd_fsname; 
234         struct proc_dir_entry *obd_proc_entry;
235         int obd_multi_count;
236         struct obd_conn obd_multi_conn[MAX_MULTI];
237         unsigned int obd_gen_last_id;
238         unsigned long obd_gen_prealloc_quota;
239         struct list_head obd_gen_clients;
240         union {
241                 struct ext2_obd ext2;
242                 struct filter_obd filter;
243                 struct raid1_obd raid1;
244                 struct snap_obd snap;
245                 struct trace_obd trace;
246                 /* struct fc_obd fc; */
247         } u;
248 };
249
250 extern struct proc_dir_entry *proc_lustre_register_obd_device(struct obd_device *obd);
251 extern void proc_lustre_release_obd_device(struct obd_device *obd);
252 extern void proc_lustre_remove_obd_entry(const char* name, struct obd_device *obd);
253
254 /*
255  *  ======== OBD Operations Declarations ===========
256  */
257
258 #define OBD_BRW_READ    (READ)
259 #define OBD_BRW_WRITE   (WRITE)
260 #define OBD_BRW_RWMASK  (READ | WRITE)
261 #define OBD_BRW_CREATE  (0x00000010UL)
262
263 struct obd_ops {
264         int (*o_iocontrol)(int cmd, struct obd_conn *, int len, void *karg,
265                            void *uarg);
266         int (*o_get_info)(struct obd_conn *, obd_count keylen, void *key,
267                           obd_count *vallen, void **val);
268         int (*o_set_info)(struct obd_conn *, obd_count keylen, void *key,
269                           obd_count vallen, void *val);
270         int (*o_attach)(struct obd_device *dev, struct obd_ioctl_data *data);
271         int (*o_detach)(struct obd_device *dev);
272         int (*o_setup) (struct obd_device *dev, struct obd_ioctl_data *data);
273         int (*o_cleanup)(struct obd_device *dev);
274         int (*o_connect)(struct obd_conn *conn);
275         int (*o_disconnect)(struct obd_conn *conn);
276         int (*o_statfs)(struct obd_conn *conn, struct statfs *statfs);
277         int (*o_preallocate)(struct obd_conn *, obd_count *req, obd_id *ids);
278         int (*o_create)(struct obd_conn *conn,  struct obdo *oa);
279         int (*o_destroy)(struct obd_conn *conn, struct obdo *oa);
280         int (*o_setattr)(struct obd_conn *conn, struct obdo *oa);
281         int (*o_getattr)(struct obd_conn *conn, struct obdo *oa);
282         int (*o_read)(struct obd_conn *conn, struct obdo *oa, char *buf,
283                       obd_size *count, obd_off offset);
284         int (*o_write)(struct obd_conn *conn, struct obdo *oa, char *buf,
285                        obd_size *count, obd_off offset);
286         int (*o_brw)(int rw, struct obd_conn *conn, obd_count num_oa,
287                      struct obdo **oa, obd_count *oa_bufs, struct page **buf,
288                      obd_size *count, obd_off *offset, obd_flag *flags);
289         int (*o_punch)(struct obd_conn *conn, struct obdo *tgt, obd_size count,
290                        obd_off offset);
291         int (*o_sync)(struct obd_conn *conn, struct obdo *tgt, obd_size count,
292                       obd_off offset);
293         int (*o_migrate)(struct obd_conn *conn, struct obdo *dst,
294                          struct obdo *src, obd_size count, obd_off offset);
295         int (*o_copy)(struct obd_conn *dstconn, struct obdo *dst,
296                       struct obd_conn *srconn, struct obdo *src,
297                       obd_size count, obd_off offset);
298         int (*o_iterate)(struct obd_conn *conn, int (*)(obd_id, obd_gr, void *),
299                          obd_id *startid, obd_gr group, void *data);
300 };
301
302 #define OBT(dev)        dev->obd_type->typ_ops
303 #define OBP(dev,op)     dev->obd_type->typ_ops->o_ ## op
304
305 #endif 
306
307 /* This value is not arbitrarily chosen.  KIO_STATIC_PAGES from linux/iobuf.h */
308 #define MAX_IOVEC       (KIO_STATIC_PAGES - 1)
309
310
311 /*
312  *  ======== OBD Metadata Support  ===========
313  */
314
315 extern int obd_init_obdo_cache(void);
316 extern void obd_cleanup_obdo_cache(void);
317
318
319 static inline int obdo_has_inline(struct obdo *obdo)
320 {
321         return (obdo->o_valid & OBD_MD_FLINLINE &&
322                 obdo->o_obdflags & OBD_FL_INLINEDATA);
323 };
324
325 static inline int obdo_has_obdmd(struct obdo *obdo)
326 {
327         return (obdo->o_valid & OBD_MD_FLOBDMD &&
328                 obdo->o_obdflags & OBD_FL_OBDMDEXISTS);
329 };
330
331 #ifdef __KERNEL__
332 /* support routines */
333 extern kmem_cache_t *obdo_cachep;
334
335 static __inline__ struct obdo *obdo_alloc(void)
336 {
337         struct obdo *oa = NULL;
338
339         oa = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
340         memset(oa, 0, sizeof (*oa));
341
342         return oa;
343 }
344
345 static __inline__ void obdo_free(struct obdo *oa)
346 {
347         if ( !oa ) 
348                 return;
349         kmem_cache_free(obdo_cachep, oa);
350 }
351
352
353
354 static __inline__ struct obdo *obdo_fromid(struct obd_conn *conn, obd_id id,
355                                            obd_flag valid)
356 {
357         struct obdo *oa;
358         int err;
359
360         ENTRY;
361         oa = obdo_alloc();
362         if ( !oa ) {
363                 EXIT;
364                 return ERR_PTR(-ENOMEM);
365         }
366
367         oa->o_id = id;
368         oa->o_valid = valid;
369         if ((err = OBP(conn->oc_dev, getattr)(conn, oa))) {
370                 obdo_free(oa);
371                 EXIT;
372                 return ERR_PTR(err);
373         }
374         EXIT;
375         return oa;
376 }
377
378 static inline void obdo_from_iattr(struct obdo *oa, struct iattr *attr)
379 {
380         unsigned int ia_valid = attr->ia_valid;
381
382         if (ia_valid & ATTR_ATIME) {
383                 oa->o_atime = attr->ia_atime;
384                 oa->o_valid |= OBD_MD_FLATIME;
385         }
386         if (ia_valid & ATTR_MTIME) {
387                 oa->o_mtime = attr->ia_mtime;
388                 oa->o_valid |= OBD_MD_FLMTIME;
389         }
390         if (ia_valid & ATTR_CTIME) {
391                 oa->o_ctime = attr->ia_ctime;
392                 oa->o_valid |= OBD_MD_FLCTIME;
393         }
394         if (ia_valid & ATTR_SIZE) {
395                 oa->o_size = attr->ia_size;
396                 oa->o_valid |= OBD_MD_FLSIZE;
397         }
398         if (ia_valid & ATTR_MODE) {
399                 oa->o_mode = attr->ia_mode;
400                 oa->o_valid |= OBD_MD_FLMODE;
401                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
402                         oa->o_mode &= ~S_ISGID;
403         }
404         if (ia_valid & ATTR_UID)
405         {
406                 oa->o_uid = attr->ia_uid;
407                 oa->o_valid |= OBD_MD_FLUID;
408         }
409         if (ia_valid & ATTR_GID) {
410                 oa->o_gid = attr->ia_gid;
411                 oa->o_valid |= OBD_MD_FLGID;
412         }
413 }
414
415
416 static inline void iattr_from_obdo(struct iattr *attr, struct obdo *oa)
417 {
418         unsigned int ia_valid = oa->o_valid;
419
420         if (ia_valid & OBD_MD_FLATIME) {
421                 attr->ia_atime = oa->o_atime;
422                 attr->ia_valid |= ATTR_ATIME;
423         }
424         if (ia_valid & OBD_MD_FLMTIME) {
425                 attr->ia_mtime = oa->o_mtime;
426                 attr->ia_valid |= ATTR_MTIME;
427         }
428         if (ia_valid & OBD_MD_FLCTIME) {
429                 attr->ia_ctime = oa->o_ctime;
430                 attr->ia_valid |= ATTR_CTIME;
431         }
432         if (ia_valid & OBD_MD_FLSIZE) {
433                 attr->ia_size = oa->o_size;
434                 attr->ia_valid |= ATTR_SIZE;
435         }
436         if (ia_valid & OBD_MD_FLMODE) {
437                 attr->ia_mode = oa->o_mode;
438                 attr->ia_valid |= ATTR_MODE;
439                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
440                         attr->ia_mode &= ~S_ISGID;
441         }
442         if (ia_valid & OBD_MD_FLUID)
443         {
444                 attr->ia_uid = oa->o_uid;
445                 attr->ia_valid |= ATTR_UID;
446         }
447         if (ia_valid & OBD_MD_FLGID) {
448                 attr->ia_gid = oa->o_gid;
449                 attr->ia_valid |= ATTR_GID;
450         }
451 }
452
453
454 /* WARNING: the file systems must take care not to tinker with
455    attributes they don't manage (such as blocks). */
456
457 static __inline__ void obdo_from_inode(struct obdo *dst, struct inode *src)
458 {
459         if ( dst->o_valid & OBD_MD_FLID )
460                 dst->o_id = src->i_ino;
461         if ( dst->o_valid & OBD_MD_FLATIME )
462                 dst->o_atime = src->i_atime;
463         if ( dst->o_valid & OBD_MD_FLMTIME )
464                 dst->o_mtime = src->i_mtime;
465         if ( dst->o_valid & OBD_MD_FLCTIME )
466                 dst->o_ctime = src->i_ctime;
467         if ( dst->o_valid & OBD_MD_FLSIZE )
468                 dst->o_size = src->i_size;
469         if ( dst->o_valid & OBD_MD_FLBLOCKS )   /* allocation of space */
470                 dst->o_blocks = src->i_blocks;
471         if ( dst->o_valid & OBD_MD_FLBLKSZ )
472                 dst->o_blksize = src->i_blksize;
473         if ( dst->o_valid & OBD_MD_FLMODE )
474                 dst->o_mode = src->i_mode;
475         if ( dst->o_valid & OBD_MD_FLUID )
476                 dst->o_uid = src->i_uid;
477         if ( dst->o_valid & OBD_MD_FLGID )
478                 dst->o_gid = src->i_gid;
479         if ( dst->o_valid & OBD_MD_FLFLAGS )
480                 dst->o_flags = src->i_flags;
481         if ( dst->o_valid & OBD_MD_FLNLINK )
482                 dst->o_nlink = src->i_nlink;
483         if ( dst->o_valid & OBD_MD_FLGENER ) 
484                 dst->o_generation = src->i_generation;
485 }
486
487 static __inline__ void obdo_to_inode(struct inode *dst, struct obdo *src)
488 {
489
490         if ( src->o_valid & OBD_MD_FLID )
491                 dst->i_ino = src->o_id;
492         if ( src->o_valid & OBD_MD_FLATIME ) 
493                 dst->i_atime = src->o_atime;
494         if ( src->o_valid & OBD_MD_FLMTIME ) 
495                 dst->i_mtime = src->o_mtime;
496         if ( src->o_valid & OBD_MD_FLCTIME ) 
497                 dst->i_ctime = src->o_ctime;
498         if ( src->o_valid & OBD_MD_FLSIZE ) 
499                 dst->i_size = src->o_size;
500         if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
501                 dst->i_blocks = src->o_blocks;
502         if ( src->o_valid & OBD_MD_FLBLKSZ )
503                 dst->i_blksize = src->o_blksize;
504         if ( src->o_valid & OBD_MD_FLMODE ) 
505                 dst->i_mode = src->o_mode;
506         if ( src->o_valid & OBD_MD_FLUID ) 
507                 dst->i_uid = src->o_uid;
508         if ( src->o_valid & OBD_MD_FLGID ) 
509                 dst->i_gid = src->o_gid;
510         if ( src->o_valid & OBD_MD_FLFLAGS ) 
511                 dst->i_flags = src->o_flags;
512         if ( src->o_valid & OBD_MD_FLNLINK )
513                 dst->i_nlink = src->o_nlink;
514         if ( src->o_valid & OBD_MD_FLGENER )
515                 dst->i_generation = src->o_generation;
516 }
517
518 #endif 
519
520 static __inline__ void obdo_cpy_md(struct obdo *dst, struct obdo *src)
521 {
522 #ifdef __KERNEL__
523         CDEBUG(D_INODE, "src obdo %Ld valid 0x%x, dst obdo %Ld\n",
524                src->o_id, src->o_valid, dst->o_id);
525 #endif
526         if ( src->o_valid & OBD_MD_FLATIME ) 
527                 dst->o_atime = src->o_atime;
528         if ( src->o_valid & OBD_MD_FLMTIME ) 
529                 dst->o_mtime = src->o_mtime;
530         if ( src->o_valid & OBD_MD_FLCTIME ) 
531                 dst->o_ctime = src->o_ctime;
532         if ( src->o_valid & OBD_MD_FLSIZE ) 
533                 dst->o_size = src->o_size;
534         if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
535                 dst->o_blocks = src->o_blocks;
536         if ( src->o_valid & OBD_MD_FLBLKSZ )
537                 dst->o_blksize = src->o_blksize;
538         if ( src->o_valid & OBD_MD_FLMODE ) 
539                 dst->o_mode = src->o_mode;
540         if ( src->o_valid & OBD_MD_FLUID ) 
541                 dst->o_uid = src->o_uid;
542         if ( src->o_valid & OBD_MD_FLGID ) 
543                 dst->o_gid = src->o_gid;
544         if ( src->o_valid & OBD_MD_FLFLAGS ) 
545                 dst->o_flags = src->o_flags;
546         /*
547         if ( src->o_valid & OBD_MD_FLOBDFLG ) 
548                 dst->o_obdflags = src->o_obdflags;
549         */
550         if ( src->o_valid & OBD_MD_FLNLINK ) 
551                 dst->o_nlink = src->o_nlink;
552         if ( src->o_valid & OBD_MD_FLGENER ) 
553                 dst->o_generation = src->o_generation;
554         if ( src->o_valid & OBD_MD_FLINLINE &&
555              src->o_obdflags & OBD_FL_INLINEDATA) {
556                 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
557                 dst->o_obdflags |= OBD_FL_INLINEDATA;
558         }
559         if ( src->o_valid & OBD_MD_FLOBDMD &&
560              src->o_obdflags & OBD_FL_OBDMDEXISTS) {
561                 memcpy(dst->o_obdmd, src->o_obdmd, sizeof(src->o_obdmd));
562                 dst->o_obdflags |= OBD_FL_OBDMDEXISTS;
563         }
564
565         dst->o_valid |= src->o_valid;
566 }
567
568
569 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
570 static __inline__ int obdo_cmp_md(struct obdo *dst, struct obdo *src,
571                                   obd_flag compare)
572 {
573         int res = 0;
574
575         if ( compare & OBD_MD_FLATIME )
576                 res = (res || (dst->o_atime != src->o_atime));
577         if ( compare & OBD_MD_FLMTIME )
578                 res = (res || (dst->o_mtime != src->o_mtime));
579         if ( compare & OBD_MD_FLCTIME )
580                 res = (res || (dst->o_ctime != src->o_ctime));
581         if ( compare & OBD_MD_FLSIZE )
582                 res = (res || (dst->o_size != src->o_size));
583         if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
584                 res = (res || (dst->o_blocks != src->o_blocks));
585         if ( compare & OBD_MD_FLBLKSZ )
586                 res = (res || (dst->o_blksize != src->o_blksize));
587         if ( compare & OBD_MD_FLMODE )
588                 res = (res || (dst->o_mode != src->o_mode));
589         if ( compare & OBD_MD_FLUID )
590                 res = (res || (dst->o_uid != src->o_uid));
591         if ( compare & OBD_MD_FLGID )
592                 res = (res || (dst->o_gid != src->o_gid));
593         if ( compare & OBD_MD_FLFLAGS ) 
594                 res = (res || (dst->o_flags != src->o_flags));
595         if ( compare & OBD_MD_FLNLINK )
596                 res = (res || (dst->o_nlink != src->o_nlink));
597         if ( compare & OBD_MD_FLGENER )
598                 res = (res || (dst->o_generation != src->o_generation));
599         /* XXX Don't know if thses should be included here - wasn't previously
600         if ( compare & OBD_MD_FLINLINE )
601                 res = (res || memcmp(dst->o_inline, src->o_inline));
602         if ( compare & OBD_MD_FLOBDMD )
603                 res = (res || memcmp(dst->o_obdmd, src->o_obdmd));
604         */
605         return res;
606 }
607
608
609 #ifdef __KERNEL__
610 int obd_register_type(struct obd_ops *ops, char *nm);
611 int obd_unregister_type(char *nm);
612
613 struct obd_client {
614         struct list_head cli_chain;
615         struct obd_device *cli_obd;
616         unsigned int cli_id;
617         unsigned long cli_prealloc_quota;
618         struct list_head cli_prealloc_inodes;
619 };
620
621
622 struct obd_prealloc_inode {
623         struct list_head obd_prealloc_chain;
624         unsigned long inode;
625 };
626
627 /* generic operations shared by various OBD types */
628 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data);
629 int gen_multi_cleanup(struct obd_device *obddev);
630 int gen_multi_attach(struct obd_device *obddev, uint32_t len, void *data);
631 int gen_multi_detach(struct obd_device *obddev);
632 int gen_connect (struct obd_conn *conn);
633 int gen_disconnect(struct obd_conn *conn);
634 struct obd_client *gen_client(const struct obd_conn *);
635 int gen_cleanup(struct obd_device *obddev);
636 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
637                   struct obd_conn *src_conn, struct obdo *src,
638                   obd_size count, obd_off offset);
639
640 #endif
641
642 /*
643  *  ======== OBD IOCL Declarations ===========
644  */
645
646 #define OBD_IOCTL_VERSION 0x00010001
647
648 struct obd_ioctl_hdr { 
649         uint32_t ioc_len;
650         uint32_t ioc_version;
651 };
652
653 static inline int obd_ioctl_packlen(struct obd_ioctl_data *data)
654 {
655         int len = sizeof(struct obd_ioctl_data);
656         len += size_round(data->ioc_inllen1);
657         len += size_round(data->ioc_inllen2);
658         return len;
659 }
660
661 static inline int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
662 {
663         if (data->ioc_len > (1<<30)) { 
664                 printk("OBD ioctl: ioc_len larger than 1<<30\n");
665                 return 1;
666         }
667         if (data->ioc_inllen1 > (1<<30)) { 
668                 printk("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
669                 return 1;
670         }
671         if (data->ioc_inllen2 > (1<<30)) { 
672                 printk("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
673                 return 1;
674         }
675         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
676                 printk("OBD ioctl: inlbuf1 pointer but 0 length\n");
677                 return 1;
678         }
679         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
680                 printk("OBD ioctl: inlbuf2 pointer but 0 length\n");
681                 return 1;
682         }
683         if (data->ioc_pbuf1 && !data->ioc_plen1) {
684                 printk("OBD ioctl: pbuf1 pointer but 0 length\n");
685                 return 1;
686         }
687         if (data->ioc_pbuf2 && !data->ioc_plen2) {
688                 printk("OBD ioctl: pbuf2 pointer but 0 length\n");
689                 return 1;
690         }
691         if (obd_ioctl_packlen(data) != data->ioc_len ) {
692                 printk("OBD ioctl: packlen exceeds ioc_len\n");
693                 return 1;
694         }
695         if (data->ioc_inllen1 && 
696             data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') { 
697                 printk("OBD ioctl: inlbuf1 not 0 terminated\n");
698                 return 1;
699         }
700         if (data->ioc_inllen2 && 
701             data->ioc_bulk[size_round(data->ioc_inllen1) + data->ioc_inllen2 - 1] != '\0') { 
702                 printk("OBD ioctl: inlbuf2 not 0 terminated\n");
703                 return 1;
704         }
705         return 0;
706 }
707
708 #ifndef __KERNEL__
709 static inline int obd_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max)
710 {
711         char *ptr;
712         struct obd_ioctl_data *overlay;
713         data->ioc_len = obd_ioctl_packlen(data);
714         data->ioc_version = OBD_IOCTL_VERSION;
715
716         if (*pbuf && obd_ioctl_packlen(data) > max) 
717                 return 1;
718         if (*pbuf == NULL) { 
719                 *pbuf = malloc(data->ioc_len);
720         }
721         if (!*pbuf)
722                 return 1;
723         overlay = (struct obd_ioctl_data *)*pbuf;
724         memcpy(*pbuf, data, sizeof(*data));
725
726         ptr = overlay->ioc_bulk;
727         if (data->ioc_inlbuf1)
728                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
729         if (data->ioc_inlbuf2)
730                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
731         if (obd_ioctl_is_invalid(overlay))
732                 return 1;
733
734         return 0;
735 }
736 #else
737
738
739 /* buffer MUST be at least the size of obd_ioctl_hdr */
740 static inline int obd_ioctl_getdata(char *buf, char *end, void *arg)
741 {
742         struct obd_ioctl_hdr *hdr;
743         struct obd_ioctl_data *data;
744         int err;
745         ENTRY;
746
747         hdr = (struct obd_ioctl_hdr *)buf;
748         data = (struct obd_ioctl_data *)buf;
749
750         err = copy_from_user(buf, (void *)arg, sizeof(*hdr));
751         if ( err ) {
752                 EXIT;
753                 return err;
754         }
755
756         if (hdr->ioc_version != OBD_IOCTL_VERSION) { 
757                 printk("OBD: version mismatch kernel vs application\n");
758                 return -EINVAL;
759         }
760
761         if (hdr->ioc_len + buf >= end) { 
762                 printk("OBD: user buffer exceeds kernel buffer\n");
763                 return -EINVAL;
764         }
765
766
767         if (hdr->ioc_len < sizeof(struct obd_ioctl_data)) { 
768                 printk("OBD: user buffer too small for ioctl\n");
769                 return -EINVAL;
770         }
771
772         err = copy_from_user(buf, (void *)arg, hdr->ioc_len);
773         if ( err ) {
774                 EXIT;
775                 return err;
776         }
777
778         if (obd_ioctl_is_invalid(data)) { 
779                 printk("OBD: ioctl not correctly formatted\n");
780                 return -EINVAL;
781         }
782
783         if (data->ioc_inllen1) { 
784                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
785         }
786
787         if (data->ioc_inllen2) { 
788                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + size_round(data->ioc_inllen1);
789         }
790
791         EXIT;
792         return 0;
793 }
794 #endif
795
796
797 #define OBD_IOC_CREATE                 _IOR ('f',  3, long)
798 #define OBD_IOC_SETUP                  _IOW ('f',  4, long)
799 #define OBD_IOC_CLEANUP                _IO  ('f',  5      )
800 #define OBD_IOC_DESTROY                _IOW ('f',  6, long)
801 #define OBD_IOC_PREALLOCATE            _IOWR('f',  7, long)
802 #define OBD_IOC_DEC_USE_COUNT          _IO  ('f',  8      )
803 #define OBD_IOC_SETATTR                _IOW ('f',  9, long)
804 #define OBD_IOC_GETATTR                _IOR ('f', 10, long)
805 #define OBD_IOC_READ                   _IOWR('f', 11, long)
806 #define OBD_IOC_WRITE                  _IOWR('f', 12, long)
807 #define OBD_IOC_CONNECT                _IOR ('f', 13, long)
808 #define OBD_IOC_DISCONNECT             _IOW ('f', 14, long)
809 #define OBD_IOC_STATFS                 _IOWR('f', 15, long)
810 #define OBD_IOC_SYNC                   _IOR ('f', 16, long)
811 #define OBD_IOC_READ2                  _IOWR('f', 17, long)
812 #define OBD_IOC_FORMAT                 _IOWR('f', 18, long)
813 #define OBD_IOC_PARTITION              _IOWR('f', 19, long)
814 #define OBD_IOC_ATTACH                 _IOWR('f', 20, long)
815 #define OBD_IOC_DETACH                 _IOWR('f', 21, long)
816 #define OBD_IOC_COPY                   _IOWR('f', 22, long)
817 #define OBD_IOC_MIGR                   _IOWR('f', 23, long)
818 #define OBD_IOC_PUNCH                  _IOWR('f', 24, long)
819 #define OBD_IOC_DEVICE                 _IOWR('f', 25, long)
820
821 #define OBD_IOC_DEC_FS_USE_COUNT       _IO  ('f', 32      )
822
823
824 /* sysctl.c */
825 extern void obd_sysctl_init (void);
826 extern void obd_sysctl_clean (void);
827
828 #define CHKCONN(conn)   do { if (!gen_client(conn)) {\
829                 printk("%s %d invalid client %u\n", __FILE__, __LINE__, \
830                        conn->oc_id);\
831                 return -EINVAL; }} while (0) 
832
833
834
835 #endif /* __LINUX_CLASS_OBD_H */