Whamcloud - gitweb
Most of the code for the OST target.
[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
22 #include <linux/lustre_lib.h>
23 #include <linux/lustre_idl.h>
24 #include <linux/obd.h>
25 #endif
26
27
28
29
30
31
32 /*
33  *  ======== OBD Device Declarations ===========
34  */
35
36
37 #define OBD_PSDEV_MAJOR 186
38 #define MAX_OBD_DEVICES 8
39 #define MAX_MULTI       16
40
41
42 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
43
44
45 #define OBD_ATTACHED 0x1
46 #define OBD_SET_UP   0x2
47
48 struct obd_conn {
49         struct obd_device *oc_dev;
50         uint32_t oc_id;
51 };
52
53
54
55
56 typedef struct {
57         uint32_t len;
58         char *   name;
59         struct dentry *dentry;   /* file system obd device names */
60         __u8           _uuid[16]; /* uuid obd device names */
61 } obd_devicename;
62
63 #include <linux/obd_ext2.h>
64 #include <linux/obd_filter.h>
65 #include <linux/lustre_mds.h>
66 #include <linux/obd_snap.h>
67 #include <linux/obd_trace.h>
68 /* #include <linux/obd_fc.h> */
69 #include <linux/obd_raid1.h>
70 #include <linux/obd_ost.h>
71
72 #ifdef __KERNEL__
73 /* corresponds to one of the obdx */
74 struct obd_device {
75         struct obd_type *obd_type;
76         int obd_minor;
77         int obd_flags;
78         int obd_refcnt; 
79         obd_devicename obd_fsname; 
80         struct proc_dir_entry *obd_proc_entry;
81         int obd_multi_count;
82         struct obd_conn obd_multi_conn[MAX_MULTI];
83         unsigned int obd_gen_last_id;
84         unsigned long obd_gen_prealloc_quota;
85         struct list_head obd_gen_clients;
86         union {
87                 struct ext2_obd ext2;
88                 struct filter_obd filter;
89                 struct mds_obd mds;
90                 struct raid1_obd raid1;
91                 struct snap_obd snap;
92                 struct trace_obd trace;
93                 struct ost_obd ost;
94         } u;
95 };
96
97 extern struct proc_dir_entry *proc_lustre_register_obd_device(struct obd_device *obd);
98 extern void proc_lustre_release_obd_device(struct obd_device *obd);
99 extern void proc_lustre_remove_obd_entry(const char* name, struct obd_device *obd);
100
101 /*
102  *  ======== OBD Operations Declarations ===========
103  */
104
105 #define OBD_BRW_READ    (READ)
106 #define OBD_BRW_WRITE   (WRITE)
107 #define OBD_BRW_RWMASK  (READ | WRITE)
108 #define OBD_BRW_CREATE  (0x00000010UL)
109
110 struct obd_ops {
111         int (*o_iocontrol)(int cmd, struct obd_conn *, int len, void *karg,
112                            void *uarg);
113         int (*o_get_info)(struct obd_conn *, obd_count keylen, void *key,
114                           obd_count *vallen, void **val);
115         int (*o_set_info)(struct obd_conn *, obd_count keylen, void *key,
116                           obd_count vallen, void *val);
117         int (*o_attach)(struct obd_device *dev, obd_count len, void *data);
118         int (*o_detach)(struct obd_device *dev);
119         int (*o_setup) (struct obd_device *dev, obd_count len, void *data);
120         int (*o_cleanup)(struct obd_device *dev);
121         int (*o_connect)(struct obd_conn *conn);
122         int (*o_disconnect)(struct obd_conn *conn);
123         int (*o_statfs)(struct obd_conn *conn, struct statfs *statfs);
124         int (*o_preallocate)(struct obd_conn *, obd_count *req, obd_id *ids);
125         int (*o_create)(struct obd_conn *conn,  struct obdo *oa);
126         int (*o_destroy)(struct obd_conn *conn, struct obdo *oa);
127         int (*o_setattr)(struct obd_conn *conn, struct obdo *oa);
128         int (*o_getattr)(struct obd_conn *conn, struct obdo *oa);
129         int (*o_read)(struct obd_conn *conn, struct obdo *oa, char *buf,
130                       obd_size *count, obd_off offset);
131         int (*o_write)(struct obd_conn *conn, struct obdo *oa, char *buf,
132                        obd_size *count, obd_off offset);
133         int (*o_brw)(int rw, struct obd_conn *conn, obd_count num_oa,
134                      struct obdo **oa, obd_count *oa_bufs, struct page **buf,
135                      obd_size *count, obd_off *offset, obd_flag *flags);
136         int (*o_punch)(struct obd_conn *conn, struct obdo *tgt, obd_size count,
137                        obd_off offset);
138         int (*o_sync)(struct obd_conn *conn, struct obdo *tgt, obd_size count,
139                       obd_off offset);
140         int (*o_migrate)(struct obd_conn *conn, struct obdo *dst,
141                          struct obdo *src, obd_size count, obd_off offset);
142         int (*o_copy)(struct obd_conn *dstconn, struct obdo *dst,
143                       struct obd_conn *srconn, struct obdo *src,
144                       obd_size count, obd_off offset);
145         int (*o_iterate)(struct obd_conn *conn, int (*)(obd_id, obd_gr, void *),
146                          obd_id *startid, obd_gr group, void *data);
147 };
148
149 struct obd_request {
150         struct obdo *oa;
151         struct obd_conn *conn;
152         __u32 plen1;
153         char *pbuf1;
154 };
155
156
157
158
159 #define OBT(dev)        dev->obd_type->typ_ops
160 #define OBP(dev,op)     dev->obd_type->typ_ops->o_ ## op
161
162 #endif 
163
164 /* This value is not arbitrarily chosen.  KIO_STATIC_PAGES from linux/iobuf.h */
165 #define MAX_IOVEC       (KIO_STATIC_PAGES - 1)
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_valid & OBD_MD_FLINLINE &&
179                 obdo->o_obdflags & OBD_FL_INLINEDATA);
180 };
181
182 static inline int obdo_has_obdmd(struct obdo *obdo)
183 {
184         return (obdo->o_valid & OBD_MD_FLOBDMD &&
185                 obdo->o_obdflags & OBD_FL_OBDMDEXISTS);
186 };
187
188 #ifdef __KERNEL__
189 /* support routines */
190 extern kmem_cache_t *obdo_cachep;
191
192 static __inline__ struct obdo *obdo_alloc(void)
193 {
194         struct obdo *oa = NULL;
195
196         oa = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
197         memset(oa, 0, sizeof (*oa));
198
199         return oa;
200 }
201
202 static __inline__ void obdo_free(struct obdo *oa)
203 {
204         if ( !oa ) 
205                 return;
206         kmem_cache_free(obdo_cachep, oa);
207 }
208
209
210
211 static __inline__ struct obdo *obdo_fromid(struct obd_conn *conn, obd_id id,
212                                            obd_mode mode, obd_flag valid)
213 {
214         struct obdo *oa;
215         int err;
216
217         ENTRY;
218         oa = obdo_alloc();
219         if ( !oa ) {
220                 EXIT;
221                 return ERR_PTR(-ENOMEM);
222         }
223
224         oa->o_id = id;
225         oa->o_mode = mode;
226         oa->o_valid = valid;
227         if ((err = OBP(conn->oc_dev, getattr)(conn, oa))) {
228                 obdo_free(oa);
229                 EXIT;
230                 return ERR_PTR(err);
231         }
232         EXIT;
233         return oa;
234 }
235
236 static inline void obdo_from_iattr(struct obdo *oa, struct iattr *attr)
237 {
238         unsigned int ia_valid = attr->ia_valid;
239
240         if (ia_valid & ATTR_ATIME) {
241                 oa->o_atime = attr->ia_atime;
242                 oa->o_valid |= OBD_MD_FLATIME;
243         }
244         if (ia_valid & ATTR_MTIME) {
245                 oa->o_mtime = attr->ia_mtime;
246                 oa->o_valid |= OBD_MD_FLMTIME;
247         }
248         if (ia_valid & ATTR_CTIME) {
249                 oa->o_ctime = attr->ia_ctime;
250                 oa->o_valid |= OBD_MD_FLCTIME;
251         }
252         if (ia_valid & ATTR_SIZE) {
253                 oa->o_size = attr->ia_size;
254                 oa->o_valid |= OBD_MD_FLSIZE;
255         }
256         if (ia_valid & ATTR_MODE) {
257                 oa->o_mode = attr->ia_mode;
258                 oa->o_valid |= OBD_MD_FLMODE;
259                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
260                         oa->o_mode &= ~S_ISGID;
261         }
262         if (ia_valid & ATTR_UID)
263         {
264                 oa->o_uid = attr->ia_uid;
265                 oa->o_valid |= OBD_MD_FLUID;
266         }
267         if (ia_valid & ATTR_GID) {
268                 oa->o_gid = attr->ia_gid;
269                 oa->o_valid |= OBD_MD_FLGID;
270         }
271 }
272
273
274 static inline void iattr_from_obdo(struct iattr *attr, struct obdo *oa)
275 {
276         unsigned int ia_valid = oa->o_valid;
277         
278         memset(attr, 0, sizeof(*attr));
279         if (ia_valid & OBD_MD_FLATIME) {
280                 attr->ia_atime = oa->o_atime;
281                 attr->ia_valid |= ATTR_ATIME;
282         }
283         if (ia_valid & OBD_MD_FLMTIME) {
284                 attr->ia_mtime = oa->o_mtime;
285                 attr->ia_valid |= ATTR_MTIME;
286         }
287         if (ia_valid & OBD_MD_FLCTIME) {
288                 attr->ia_ctime = oa->o_ctime;
289                 attr->ia_valid |= ATTR_CTIME;
290         }
291         if (ia_valid & OBD_MD_FLSIZE) {
292                 attr->ia_size = oa->o_size;
293                 attr->ia_valid |= ATTR_SIZE;
294         }
295         if (ia_valid & OBD_MD_FLMODE) {
296                 attr->ia_mode = oa->o_mode;
297                 attr->ia_valid |= ATTR_MODE;
298                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
299                         attr->ia_mode &= ~S_ISGID;
300         }
301         if (ia_valid & OBD_MD_FLUID)
302         {
303                 attr->ia_uid = oa->o_uid;
304                 attr->ia_valid |= ATTR_UID;
305         }
306         if (ia_valid & OBD_MD_FLGID) {
307                 attr->ia_gid = oa->o_gid;
308                 attr->ia_valid |= ATTR_GID;
309         }
310 }
311
312
313 /* WARNING: the file systems must take care not to tinker with
314    attributes they don't manage (such as blocks). */
315
316 static __inline__ void obdo_from_inode(struct obdo *dst, struct inode *src)
317 {
318         if ( dst->o_valid & OBD_MD_FLID )
319                 dst->o_id = src->i_ino;
320         if ( dst->o_valid & OBD_MD_FLATIME )
321                 dst->o_atime = src->i_atime;
322         if ( dst->o_valid & OBD_MD_FLMTIME )
323                 dst->o_mtime = src->i_mtime;
324         if ( dst->o_valid & OBD_MD_FLCTIME )
325                 dst->o_ctime = src->i_ctime;
326         if ( dst->o_valid & OBD_MD_FLSIZE )
327                 dst->o_size = src->i_size;
328         if ( dst->o_valid & OBD_MD_FLBLOCKS )   /* allocation of space */
329                 dst->o_blocks = src->i_blocks;
330         if ( dst->o_valid & OBD_MD_FLBLKSZ )
331                 dst->o_blksize = src->i_blksize;
332         if ( dst->o_valid & OBD_MD_FLMODE )
333                 dst->o_mode = src->i_mode;
334         if ( dst->o_valid & OBD_MD_FLUID )
335                 dst->o_uid = src->i_uid;
336         if ( dst->o_valid & OBD_MD_FLGID )
337                 dst->o_gid = src->i_gid;
338         if ( dst->o_valid & OBD_MD_FLFLAGS )
339                 dst->o_flags = src->i_flags;
340         if ( dst->o_valid & OBD_MD_FLNLINK )
341                 dst->o_nlink = src->i_nlink;
342         if ( dst->o_valid & OBD_MD_FLGENER ) 
343                 dst->o_generation = src->i_generation;
344 }
345
346 static __inline__ void obdo_to_inode(struct inode *dst, struct obdo *src)
347 {
348
349         if ( src->o_valid & OBD_MD_FLID )
350                 dst->i_ino = src->o_id;
351         if ( src->o_valid & OBD_MD_FLATIME ) 
352                 dst->i_atime = src->o_atime;
353         if ( src->o_valid & OBD_MD_FLMTIME ) 
354                 dst->i_mtime = src->o_mtime;
355         if ( src->o_valid & OBD_MD_FLCTIME ) 
356                 dst->i_ctime = src->o_ctime;
357         if ( src->o_valid & OBD_MD_FLSIZE ) 
358                 dst->i_size = src->o_size;
359         if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
360                 dst->i_blocks = src->o_blocks;
361         if ( src->o_valid & OBD_MD_FLBLKSZ )
362                 dst->i_blksize = src->o_blksize;
363         if ( src->o_valid & OBD_MD_FLMODE ) 
364                 dst->i_mode = src->o_mode;
365         if ( src->o_valid & OBD_MD_FLUID ) 
366                 dst->i_uid = src->o_uid;
367         if ( src->o_valid & OBD_MD_FLGID ) 
368                 dst->i_gid = src->o_gid;
369         if ( src->o_valid & OBD_MD_FLFLAGS ) 
370                 dst->i_flags = src->o_flags;
371         if ( src->o_valid & OBD_MD_FLNLINK )
372                 dst->i_nlink = src->o_nlink;
373         if ( src->o_valid & OBD_MD_FLGENER )
374                 dst->i_generation = src->o_generation;
375 }
376
377 #endif 
378
379 static __inline__ void obdo_cpy_md(struct obdo *dst, struct obdo *src)
380 {
381 #ifdef __KERNEL__
382         CDEBUG(D_INODE, "src obdo %Ld valid 0x%x, dst obdo %Ld\n",
383                src->o_id, src->o_valid, dst->o_id);
384 #endif
385         if ( src->o_valid & OBD_MD_FLATIME ) 
386                 dst->o_atime = src->o_atime;
387         if ( src->o_valid & OBD_MD_FLMTIME ) 
388                 dst->o_mtime = src->o_mtime;
389         if ( src->o_valid & OBD_MD_FLCTIME ) 
390                 dst->o_ctime = src->o_ctime;
391         if ( src->o_valid & OBD_MD_FLSIZE ) 
392                 dst->o_size = src->o_size;
393         if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
394                 dst->o_blocks = src->o_blocks;
395         if ( src->o_valid & OBD_MD_FLBLKSZ )
396                 dst->o_blksize = src->o_blksize;
397         if ( src->o_valid & OBD_MD_FLMODE ) 
398                 dst->o_mode = src->o_mode;
399         if ( src->o_valid & OBD_MD_FLUID ) 
400                 dst->o_uid = src->o_uid;
401         if ( src->o_valid & OBD_MD_FLGID ) 
402                 dst->o_gid = src->o_gid;
403         if ( src->o_valid & OBD_MD_FLFLAGS ) 
404                 dst->o_flags = src->o_flags;
405         /*
406         if ( src->o_valid & OBD_MD_FLOBDFLG ) 
407                 dst->o_obdflags = src->o_obdflags;
408         */
409         if ( src->o_valid & OBD_MD_FLNLINK ) 
410                 dst->o_nlink = src->o_nlink;
411         if ( src->o_valid & OBD_MD_FLGENER ) 
412                 dst->o_generation = src->o_generation;
413         if ( src->o_valid & OBD_MD_FLINLINE &&
414              src->o_obdflags & OBD_FL_INLINEDATA) {
415                 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
416                 dst->o_obdflags |= OBD_FL_INLINEDATA;
417         }
418         if ( src->o_valid & OBD_MD_FLOBDMD &&
419              src->o_obdflags & OBD_FL_OBDMDEXISTS) {
420                 memcpy(dst->o_obdmd, src->o_obdmd, sizeof(src->o_obdmd));
421                 dst->o_obdflags |= OBD_FL_OBDMDEXISTS;
422         }
423
424         dst->o_valid |= src->o_valid;
425 }
426
427
428 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
429 static __inline__ int obdo_cmp_md(struct obdo *dst, struct obdo *src,
430                                   obd_flag compare)
431 {
432         int res = 0;
433
434         if ( compare & OBD_MD_FLATIME )
435                 res = (res || (dst->o_atime != src->o_atime));
436         if ( compare & OBD_MD_FLMTIME )
437                 res = (res || (dst->o_mtime != src->o_mtime));
438         if ( compare & OBD_MD_FLCTIME )
439                 res = (res || (dst->o_ctime != src->o_ctime));
440         if ( compare & OBD_MD_FLSIZE )
441                 res = (res || (dst->o_size != src->o_size));
442         if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
443                 res = (res || (dst->o_blocks != src->o_blocks));
444         if ( compare & OBD_MD_FLBLKSZ )
445                 res = (res || (dst->o_blksize != src->o_blksize));
446         if ( compare & OBD_MD_FLMODE )
447                 res = (res || (dst->o_mode != src->o_mode));
448         if ( compare & OBD_MD_FLUID )
449                 res = (res || (dst->o_uid != src->o_uid));
450         if ( compare & OBD_MD_FLGID )
451                 res = (res || (dst->o_gid != src->o_gid));
452         if ( compare & OBD_MD_FLFLAGS ) 
453                 res = (res || (dst->o_flags != src->o_flags));
454         if ( compare & OBD_MD_FLNLINK )
455                 res = (res || (dst->o_nlink != src->o_nlink));
456         if ( compare & OBD_MD_FLGENER )
457                 res = (res || (dst->o_generation != src->o_generation));
458         /* XXX Don't know if thses should be included here - wasn't previously
459         if ( compare & OBD_MD_FLINLINE )
460                 res = (res || memcmp(dst->o_inline, src->o_inline));
461         if ( compare & OBD_MD_FLOBDMD )
462                 res = (res || memcmp(dst->o_obdmd, src->o_obdmd));
463         */
464         return res;
465 }
466
467
468 #ifdef __KERNEL__
469 int obd_register_type(struct obd_ops *ops, char *nm);
470 int obd_unregister_type(char *nm);
471
472 struct obd_client {
473         struct list_head cli_chain;
474         struct obd_device *cli_obd;
475         unsigned int cli_id;
476         unsigned long cli_prealloc_quota;
477         struct list_head cli_prealloc_inodes;
478 };
479
480
481 struct obd_prealloc_inode {
482         struct list_head obd_prealloc_chain;
483         unsigned long inode;
484 };
485
486 /* generic operations shared by various OBD types */
487 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data);
488 int gen_multi_cleanup(struct obd_device *obddev);
489 int gen_multi_attach(struct obd_device *obddev, uint32_t len, void *data);
490 int gen_multi_detach(struct obd_device *obddev);
491 int gen_connect (struct obd_conn *conn);
492 int gen_disconnect(struct obd_conn *conn);
493 struct obd_client *gen_client(const struct obd_conn *);
494 int gen_cleanup(struct obd_device *obddev);
495 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
496                   struct obd_conn *src_conn, struct obdo *src,
497                   obd_size count, obd_off offset);
498
499 #endif
500
501 /* sysctl.c */
502 extern void obd_sysctl_init (void);
503 extern void obd_sysctl_clean (void);
504
505 #define CHKCONN(conn)   do { if (!gen_client(conn)) {\
506                 printk("%s %d invalid client %u\n", __FILE__, __LINE__, \
507                        conn->oc_id);\
508                 return -EINVAL; }} while (0) 
509
510
511
512 #endif /* __LINUX_CLASS_OBD_H */