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