Whamcloud - gitweb
snap/*.c: get attribtutes from child connection, fixed obdo_fromid() to return
[fs/lustre-release.git] / lustre / include / linux / obd_class.h
1 #ifndef __LINUX_CLASS_OBD_H
2 #define __LINUX_CLASS_OBD_H
3
4 #include <linux/fs.h>
5 #include <linux/ext2_fs.h>
6 #include <linux/time.h>
7 #include <linux/obd.h>
8
9 #include <linux/obd_ext2.h>
10 #include <linux/obd_snap.h>
11 /* #include <linux/obd_fc.h> */
12 #include <linux/obd_raid1.h>
13 #include <linux/obd_rpc.h>
14
15
16 /*
17  *  ======== OBD type Declarations ===========
18  */
19
20 typedef uint64_t        obd_id;
21 typedef uint64_t        obd_gr;
22 typedef uint64_t        obd_time;
23 typedef uint64_t        obd_size;
24 typedef uint64_t        obd_off;
25 typedef uint64_t        obd_blocks;
26 typedef uint32_t        obd_blksize;
27 typedef uint32_t        obd_mode;
28 typedef uint32_t        obd_uid;
29 typedef uint32_t        obd_gid;
30 typedef uint32_t        obd_flag;
31 typedef uint32_t        obd_count;
32
33 #define OBD_FL_INLINEDATA       (1UL)  
34 #define OBD_FL_OBDMDEXISTS      (1UL << 1)
35
36 #define OBD_INLINESZ    60
37 #define OBD_OBDMDSZ     60
38 /* Note: 64-bit types are 64-bit aligned in structure */
39 struct obdo {
40         obd_id                  o_id;
41         obd_gr                  o_gr;
42         obd_time                o_atime;
43         obd_time                o_mtime;
44         obd_time                o_ctime;
45         obd_size                o_size;
46         obd_blocks              o_blocks;
47         obd_blksize             o_blksize;
48         obd_mode                o_mode;
49         obd_uid                 o_uid;
50         obd_gid                 o_gid;
51         obd_flag                o_flags;
52         obd_flag                o_obdflags;
53         obd_count               o_nlink;
54         obd_count               o_generation;
55         obd_flag                o_valid;        /* hot fields in this obdo */
56         char                    o_inline[OBD_INLINESZ];
57         char                    o_obdmd[OBD_OBDMDSZ];
58         struct list_head        o_list;
59         struct obd_ops          *o_op;
60 };
61
62 #define OBD_MD_FLALL    (~0UL)
63 #define OBD_MD_FLID     (0x0001UL)
64 #define OBD_MD_FLATIME  (0x0002UL)
65 #define OBD_MD_FLMTIME  (0x0004UL)
66 #define OBD_MD_FLCTIME  (0x0008UL)
67 #define OBD_MD_FLSIZE   (0x0010UL)
68 #define OBD_MD_FLBLOCKS (0x0020UL)
69 #define OBD_MD_FLBLKSZ  (0x0040UL)
70 #define OBD_MD_FLMODE   (0x0080UL)
71 #define OBD_MD_FLUID    (0x0100UL)
72 #define OBD_MD_FLGID    (0x0200UL)
73 #define OBD_MD_FLFLAGS  (0x0400UL)
74 #define OBD_MD_FLOBDFLG (0x0800UL)
75 #define OBD_MD_FLNLINK  (0x1000UL)
76 #define OBD_MD_FLGENER  (0x2000UL)
77 #define OBD_MD_FLINLINE (0x4000UL)
78 #define OBD_MD_FLOBDMD  (0x8000UL)
79
80 /*
81  *  ======== OBD Device Declarations ===========
82  */
83
84
85 #define OBD_PSDEV_MAJOR 186
86 #define MAX_OBD_DEVICES 8
87 #define MAX_MULTI       16
88
89
90 extern struct obd_device obd_dev[MAX_OBD_DEVICES];
91
92
93 #define OBD_ATTACHED 0x1
94 #define OBD_SET_UP   0x2
95
96 struct obd_conn {
97         struct obd_device *oc_dev;
98         uint32_t oc_id;
99 };
100
101 /* corresponds to one of the obdx */
102 struct obd_device {
103         struct obd_type *obd_type;
104         int obd_minor;
105         int obd_flags;
106         int obd_refcnt; 
107         int obd_multi_count;
108         struct obd_conn obd_multi_conn[MAX_MULTI];
109         unsigned int obd_gen_last_id;
110         unsigned long obd_gen_prealloc_quota;
111         struct list_head obd_gen_clients;
112         union {
113                 struct ext2_obd ext2;
114                 struct raid1_obd raid1;
115                 struct snap_obd snap;
116                 struct rpc_obd rpc;
117                 /* struct fc_obd fc; */
118         } u;
119 };
120
121 /*
122  *  ======== OBD Operations Declarations ===========
123  */
124
125
126 struct obd_ops {
127         int (*o_iocontrol)(int cmd, struct obd_conn *, int len, void *karg,
128                            void *uarg);
129         int (*o_get_info)(struct obd_conn *, obd_count keylen, void *key,
130                           obd_count *vallen, void **val);
131         int (*o_set_info)(struct obd_conn *, obd_count keylen, void *key,
132                           obd_count vallen, void *val);
133         int (*o_attach)(struct obd_device *dev, obd_count len, void *data);
134         int (*o_detach)(struct obd_device *dev);
135         int (*o_setup) (struct obd_device *dev, obd_count len, void *data);
136         int (*o_cleanup)(struct obd_device *dev);
137         int (*o_connect)(struct obd_conn *conn);
138         int (*o_disconnect)(struct obd_conn *conn);
139         int (*o_statfs)(struct obd_conn *conn, struct statfs *statfs);
140         int (*o_preallocate)(struct obd_conn *, obd_count *req, obd_id *ids);
141         int (*o_create)(struct obd_conn *conn,  struct obdo *oa);
142         int (*o_destroy)(struct obd_conn *conn, struct obdo *oa);
143         int (*o_setattr)(struct obd_conn *conn, struct obdo *oa);
144         int (*o_getattr)(struct obd_conn *conn, struct obdo *oa);
145         int (*o_read)(struct obd_conn *conn, struct obdo *oa, char *buf,
146                       obd_size *count, obd_off offset);
147         int (*o_write)(struct obd_conn *conn, struct obdo *oa, char *buf,
148                        obd_size *count, obd_off offset);
149         int (*o_brw)(int rw, struct obd_conn *conn, struct obdo *oa,
150                      char *buf, obd_size *count, obd_off offset, obd_flag flags);
151         int (*o_punch)(struct obd_conn *conn, struct obdo *tgt, obd_size count,
152                        obd_off offset);
153         int (*o_sync)(struct obd_conn *conn, struct obdo *tgt, obd_size count,
154                       obd_off offset);
155         int (*o_migrate)(struct obd_conn *conn, struct obdo *dst,
156                          struct obdo *src, obd_size count, obd_off offset);
157         int (*o_copy)(struct obd_conn *dstconn, struct obdo *dst,
158                       struct obd_conn *srconn, struct obdo *src,
159                       obd_size count, obd_off offset);
160         int (*o_iterate)(struct obd_conn *conn, int (*)(obd_id, obd_gr, void *),
161                          obd_id *startid, obd_gr group, void *data);
162 };
163
164 #define OBT(dev)        dev->obd_type->typ_ops
165 #define OBP(dev,op)     dev->obd_type->typ_ops->o_ ## op
166
167
168 /*
169  *  ======== OBD Metadata Support  ===========
170  */
171
172 extern int obd_init_obdo_cache(void);
173 extern void obd_cleanup_obdo_cache(void);
174
175
176 static inline int obdo_has_inline(struct obdo *obdo)
177 {
178         return (obdo->o_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 /* support routines */
189 extern kmem_cache_t *obdo_cachep;
190
191 static __inline__ struct obdo *obdo_alloc(void)
192 {
193         struct obdo *res = NULL;
194
195         res = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
196         memset(res, 0, sizeof (*res));
197
198         return res;
199 }
200
201 static __inline__ void obdo_free(struct obdo *oa)
202 {
203         if ( !oa ) 
204                 return;
205         kmem_cache_free(obdo_cachep, oa);
206 }
207
208
209
210 static __inline__ struct obdo *obdo_fromid(struct obd_conn *conn, obd_id id)
211 {
212         struct obdo *res = NULL;
213         int err;
214
215         res = kmem_cache_alloc(obdo_cachep, SLAB_KERNEL);
216         if ( !res ) {
217                 EXIT;
218                 return ERR_PTR(-ENOMEM);
219         }
220         memset(res, 0, sizeof(*res));
221         res->o_id = id;
222         res->o_valid = OBD_MD_FLALL;
223         if ((err = OBP(conn->oc_dev, getattr)(conn, res))) {
224                 OBD_FREE(res, sizeof(*res));
225                 EXIT;
226                 return ERR_PTR(err);
227         }
228         EXIT;
229         return res;
230 }
231
232 static inline void obdo_from_iattr(struct obdo *oa, struct iattr *attr)
233 {
234         unsigned int ia_valid = attr->ia_valid;
235
236         if (ia_valid & ATTR_ATIME) {
237                 oa->o_atime = attr->ia_atime;
238                 oa->o_valid |= OBD_MD_FLATIME;
239         }
240         if (ia_valid & ATTR_MTIME) {
241                 oa->o_mtime = attr->ia_mtime;
242                 oa->o_valid |= OBD_MD_FLMTIME;
243         }
244         if (ia_valid & ATTR_CTIME) {
245                 oa->o_ctime = attr->ia_ctime;
246                 oa->o_valid |= OBD_MD_FLCTIME;
247         }
248         if (ia_valid & ATTR_SIZE) {
249                 oa->o_size = attr->ia_size;
250                 oa->o_valid |= OBD_MD_FLSIZE;
251         }
252         if (ia_valid & ATTR_MODE) {
253                 oa->o_mode = attr->ia_mode;
254                 oa->o_valid |= OBD_MD_FLMODE;
255                 if (!in_group_p(oa->o_gid) && !capable(CAP_FSETID))
256                         oa->o_mode &= ~S_ISGID;
257         }
258         if (ia_valid & ATTR_UID)
259         {
260                 oa->o_uid = attr->ia_uid;
261                 oa->o_valid |= OBD_MD_FLUID;
262         }
263         if (ia_valid & ATTR_GID) {
264                 oa->o_gid = attr->ia_gid;
265                 oa->o_valid |= OBD_MD_FLGID;
266         }
267 }
268
269
270 static __inline__ void obdo_cpy_md(struct obdo *dst, struct obdo *src)
271 {
272         CDEBUG(D_INODE, "src obdo %Ld valid 0x%x, dst obdo %Ld\n",
273                src->o_id, src->o_valid, dst->o_id);
274         if ( src->o_valid & OBD_MD_FLATIME ) 
275                 dst->o_atime = src->o_atime;
276         if ( src->o_valid & OBD_MD_FLMTIME ) 
277                 dst->o_mtime = src->o_mtime;
278         if ( src->o_valid & OBD_MD_FLCTIME ) 
279                 dst->o_ctime = src->o_ctime;
280         if ( src->o_valid & OBD_MD_FLSIZE ) 
281                 dst->o_size = src->o_size;
282         if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
283                 dst->o_blocks = src->o_blocks;
284         if ( src->o_valid & OBD_MD_FLBLKSZ )
285                 dst->o_blksize = src->o_blksize;
286         if ( src->o_valid & OBD_MD_FLMODE ) 
287                 dst->o_mode = src->o_mode;
288         if ( src->o_valid & OBD_MD_FLUID ) 
289                 dst->o_uid = src->o_uid;
290         if ( src->o_valid & OBD_MD_FLGID ) 
291                 dst->o_gid = src->o_gid;
292         if ( src->o_valid & OBD_MD_FLFLAGS ) 
293                 dst->o_flags = src->o_flags;
294         if ( src->o_valid & OBD_MD_FLOBDFLG ) 
295                 dst->o_obdflags = src->o_obdflags;
296         if ( src->o_valid & OBD_MD_FLNLINK ) 
297                 dst->o_nlink = src->o_nlink;
298         if ( src->o_valid & OBD_MD_FLGENER ) 
299                 dst->o_generation = src->o_generation;
300         if ( src->o_valid & OBD_MD_FLINLINE ) 
301                 memcpy(dst->o_inline, src->o_inline, sizeof(src->o_inline));
302         if ( src->o_valid & OBD_MD_FLOBDMD ) 
303                 memcpy(dst->o_obdmd, src->o_obdmd, sizeof(src->o_obdmd));
304
305         dst->o_valid |= src->o_valid;
306 }
307
308 static __inline__ void obdo_from_inode(struct obdo *dst, struct inode *src)
309 {
310         CDEBUG(D_INODE, "src inode %ld, dst obdo %Ld valid 0x%08x\n",
311                src->i_ino, dst->o_id, dst->o_valid);
312         if ( dst->o_valid & OBD_MD_FLID )
313                 dst->o_id = src->i_ino;
314         if ( dst->o_valid & OBD_MD_FLATIME )
315                 dst->o_atime = src->i_atime;
316         if ( dst->o_valid & OBD_MD_FLMTIME )
317                 dst->o_mtime = src->i_mtime;
318         if ( dst->o_valid & OBD_MD_FLCTIME )
319                 dst->o_ctime = src->i_ctime;
320         if ( dst->o_valid & OBD_MD_FLSIZE )
321                 dst->o_size = src->i_size;
322         if ( dst->o_valid & OBD_MD_FLBLOCKS )   /* allocation of space */
323                 dst->o_blocks = src->i_blocks;
324         if ( dst->o_valid & OBD_MD_FLBLKSZ )
325                 dst->o_blksize = src->i_blksize;
326         if ( dst->o_valid & OBD_MD_FLMODE )
327                 dst->o_mode = src->i_mode;
328         if ( dst->o_valid & OBD_MD_FLUID )
329                 dst->o_uid = src->i_uid;
330         if ( dst->o_valid & OBD_MD_FLGID )
331                 dst->o_gid = src->i_gid;
332         if ( dst->o_valid & OBD_MD_FLFLAGS )
333                 dst->o_flags = src->i_flags;
334         if ( dst->o_valid & OBD_MD_FLNLINK )
335                 dst->o_nlink = src->i_nlink;
336         if ( dst->o_valid & OBD_MD_FLGENER ) 
337                 dst->o_generation = src->i_generation;
338 }
339
340 static __inline__ void obdo_to_inode(struct inode *dst, struct obdo *src)
341 {
342
343         CDEBUG(D_INODE, "src obdo %Ld valid 0x%08x, dst inode %ld\n",
344                src->o_id, src->o_valid, dst->i_ino);
345         if ( src->o_valid & OBD_MD_FLID )
346                 dst->i_ino = src->o_id;
347         if ( src->o_valid & OBD_MD_FLATIME ) 
348                 dst->i_atime = src->o_atime;
349         if ( src->o_valid & OBD_MD_FLMTIME ) 
350                 dst->i_mtime = src->o_mtime;
351         if ( src->o_valid & OBD_MD_FLCTIME ) 
352                 dst->i_ctime = src->o_ctime;
353         if ( src->o_valid & OBD_MD_FLSIZE ) 
354                 dst->i_size = src->o_size;
355         if ( src->o_valid & OBD_MD_FLBLOCKS ) /* allocation of space */
356                 dst->i_blocks = src->o_blocks;
357         if ( src->o_valid & OBD_MD_FLBLKSZ )
358                 dst->i_blksize = src->o_blksize;
359         if ( src->o_valid & OBD_MD_FLMODE ) 
360                 dst->i_mode = src->o_mode;
361         if ( src->o_valid & OBD_MD_FLUID ) 
362                 dst->i_uid = src->o_uid;
363         if ( src->o_valid & OBD_MD_FLGID ) 
364                 dst->i_gid = src->o_gid;
365         if ( src->o_valid & OBD_MD_FLFLAGS ) 
366                 dst->i_flags = src->o_flags;
367         if ( src->o_valid & OBD_MD_FLNLINK )
368                 dst->i_nlink = src->o_nlink;
369         if ( src->o_valid & OBD_MD_FLGENER )
370                 dst->i_generation = src->o_generation;
371 }
372
373 /* returns FALSE if comparison (by flags) is same, TRUE if changed */
374 static __inline__ int obdo_cmp_md(struct obdo *dst, struct obdo *src,
375                                   obd_flag compare)
376 {
377         int res = 0;
378
379         if ( compare & OBD_MD_FLATIME )
380                 res = (res || (dst->o_atime != src->o_atime));
381         if ( compare & OBD_MD_FLMTIME )
382                 res = (res || (dst->o_mtime != src->o_mtime));
383         if ( compare & OBD_MD_FLCTIME )
384                 res = (res || (dst->o_ctime != src->o_ctime));
385         if ( compare & OBD_MD_FLSIZE )
386                 res = (res || (dst->o_size != src->o_size));
387         if ( compare & OBD_MD_FLBLOCKS ) /* allocation of space */
388                 res = (res || (dst->o_blocks != src->o_blocks));
389         if ( compare & OBD_MD_FLBLKSZ )
390                 res = (res || (dst->o_blksize != src->o_blksize));
391         if ( compare & OBD_MD_FLMODE )
392                 res = (res || (dst->o_mode != src->o_mode));
393         if ( compare & OBD_MD_FLUID )
394                 res = (res || (dst->o_uid != src->o_uid));
395         if ( compare & OBD_MD_FLGID )
396                 res = (res || (dst->o_gid != src->o_gid));
397         if ( compare & OBD_MD_FLFLAGS ) 
398                 res = (res || (dst->o_flags != src->o_flags));
399         if ( compare & OBD_MD_FLNLINK )
400                 res = (res || (dst->o_nlink != src->o_nlink));
401         if ( compare & OBD_MD_FLGENER )
402                 res = (res || (dst->o_generation != src->o_generation));
403         /* XXX Don't know if thses should be included here - wasn't previously
404         if ( compare & OBD_MD_FLINLINE )
405                 res = (res || memcmp(dst->o_inline, src->o_inline));
406         if ( compare & OBD_MD_FLOBDMD )
407                 res = (res || memcmp(dst->o_obdmd, src->o_obdmd));
408         */
409         return res;
410 }
411
412
413
414 int obd_register_type(struct obd_ops *ops, char *nm);
415 int obd_unregister_type(char *nm);
416
417 struct obd_client {
418         struct list_head cli_chain;
419         struct obd_device *cli_obd;
420         unsigned int cli_id;
421         unsigned long cli_prealloc_quota;
422         struct list_head cli_prealloc_inodes;
423 };
424
425
426 struct obd_prealloc_inode {
427         struct list_head obd_prealloc_chain;
428         unsigned long inode;
429 };
430
431 /* generic operations shared by various OBD types */
432 int gen_multi_setup(struct obd_device *obddev, uint32_t len, void *data);
433 int gen_multi_cleanup(struct obd_device *obddev);
434 int gen_multi_attach(struct obd_device *obddev, uint32_t len, void *data);
435 int gen_multi_detach(struct obd_device *obddev);
436 int gen_connect (struct obd_conn *conn);
437 int gen_disconnect(struct obd_conn *conn);
438 struct obd_client *gen_client(struct obd_conn *);
439 int gen_cleanup(struct obd_device *obddev);
440 int gen_copy_data(struct obd_conn *dst_conn, struct obdo *dst,
441                   struct obd_conn *src_conn, struct obdo *src,
442                   obd_size count, obd_off offset);
443
444
445
446 /*
447  *  ======== OBD IOCL Declarations ===========
448  */
449 struct oic_generic {
450         long     att_connid;
451         int      att_typelen;
452         void    *att_type;
453         int      att_datalen;
454         void    *att_data;
455 };
456
457
458 /* for preallocation */
459 struct oic_prealloc_s {
460         uint32_t cli_id;
461         uint32_t alloc; /* user sets it to the number of inodes
462                          * requesting to be preallocated.  kernel
463                          * sets it to the actual number of
464                          * succesfully preallocated inodes */
465         obd_id  ids[32]; /* actual inode numbers */
466 };
467
468 /* for getattr, setattr, create, destroy */
469 struct oic_attr_s {
470         uint32_t conn_id;
471         struct obdo  obdo;
472 };
473
474 /* for copy, migrate */
475 struct ioc_mv_s {
476         uint32_t src_conn_id;
477         struct obdo  src;
478         uint32_t dst_conn_id;
479         struct obdo  dst;
480 };
481
482 /* for read/write */
483 struct oic_rw_s {
484         uint32_t conn_id;
485         struct obdo obdo;
486         char * buf;
487         obd_size count;
488         obd_off offset;
489 };
490
491 /* for punch, sync */
492 struct oic_range_s {
493         uint32_t conn_id;
494         struct obdo obdo;
495         obd_size count;
496         obd_off offset;
497 };
498
499
500 #define OBD_IOC_CREATE                 _IOR ('f',  3, long)
501 #define OBD_IOC_SETUP                  _IOW ('f',  4, long)
502 #define OBD_IOC_CLEANUP                _IO  ('f',  5      )
503 #define OBD_IOC_DESTROY                _IOW ('f',  6, long)
504 #define OBD_IOC_PREALLOCATE            _IOWR('f',  7, long)
505 #define OBD_IOC_DEC_USE_COUNT          _IO  ('f',  8      )
506 #define OBD_IOC_SETATTR                _IOW ('f',  9, long)
507 #define OBD_IOC_GETATTR                _IOR ('f', 10, long)
508 #define OBD_IOC_READ                   _IOWR('f', 11, long)
509 #define OBD_IOC_WRITE                  _IOWR('f', 12, long)
510 #define OBD_IOC_CONNECT                _IOR ('f', 13, long)
511 #define OBD_IOC_DISCONNECT             _IOW ('f', 14, long)
512 #define OBD_IOC_STATFS                 _IOWR('f', 15, long)
513 #define OBD_IOC_SYNC                   _IOR ('f',  16, long)
514 #define OBD_IOC_READ2                  _IOWR('f', 17, long)
515 #define OBD_IOC_FORMAT                 _IOWR('f', 18, long)
516 #define OBD_IOC_PARTITION              _IOWR('f', 19, long)
517 #define OBD_IOC_ATTACH                 _IOWR('f', 20, long)
518 #define OBD_IOC_DETACH                 _IOWR('f', 21, long)
519 #define OBD_IOC_COPY                   _IOWR('f', 22, long)
520 #define OBD_IOC_MIGR                   _IOWR('f', 23, long)
521
522 #define OBD_IOC_DEC_FS_USE_COUNT       _IO  ('f', 32      )
523
524
525 /* sysctl.c */
526 extern void obd_sysctl_init (void);
527 extern void obd_sysctl_clean (void);
528
529 #define CHKCONN(conn)   do { if (!gen_client(conn)) {\
530                 printk("%s %d invalid client %u\n", __FILE__, __LINE__, \
531                        conn->oc_id);\
532                 return -EINVAL; }} while (0) 
533
534
535
536 #endif /* __LINUX_CLASS_OBD_H */