Whamcloud - gitweb
class/class_obd.c: small OBD_ATTACHED sanity cleanup; OBD_SET_UP fix.
[fs/lustre-release.git] / lustre / include / linux / lustre_idl.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001 Cluster File Systems, Inc. <braam@clusterfs.com>
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  * (Un)packing of OST requests
22  */
23
24 #ifndef __LUSTRE_IDL_H__
25 #define __LUSTRE_IDL_H__
26 #ifdef __KERNEL__
27 #include <linux/ioctl.h>
28 #include <asm/types.h>
29
30 #include <linux/types.h>
31 #else 
32 #define __KERNEL__
33 #include <linux/list.h>
34 #undef __KERNEL__
35 #include <stdint.h>
36 #endif
37 /* 
38  * this file contains all data structures used in Lustre interfaces:
39  * - obdo and obd_request records
40  * - mds_request records
41  * - ioctl's
42  */ 
43
44
45 /* 
46  *   OST requests: OBDO & OBD request records
47  */
48
49
50 /* opcodes */
51 #define OST_GET_INFO  6
52 #define OST_CONNECT  7
53 #define OST_DISCONNECT 8
54 #define OST_GETATTR  1
55 #define OST_SETATTR  2
56 #define OST_PREPW    3
57 #define OST_CREATE   4
58 #define OST_DESTROY  5
59
60 /* packet types */
61 #define OST_TYPE_REQ 1
62 #define OST_TYPE_REP 2
63 #define OST_TYPE_ERR 3
64
65 struct ptlreq_hdr { 
66         __u32 opc;
67         __u64 seqno;
68         __u32 status;
69         __u32 type;
70 };
71
72 struct ptlrep_hdr { 
73         __u32 opc;
74         __u64 seqno;
75         __u32 status;
76         __u32 type;
77 };
78
79 typedef uint64_t        obd_id;
80 typedef uint64_t        obd_gr;
81 typedef uint64_t        obd_time;
82 typedef uint64_t        obd_size;
83 typedef uint64_t        obd_off;
84 typedef uint64_t        obd_blocks;
85 typedef uint32_t        obd_blksize;
86 typedef uint32_t        obd_mode;
87 typedef uint32_t        obd_uid;
88 typedef uint32_t        obd_gid;
89 typedef uint32_t        obd_rdev;
90 typedef uint32_t        obd_flag;
91 typedef uint32_t        obd_count;
92
93 #define OBD_FL_INLINEDATA       (0x00000001UL)  
94 #define OBD_FL_OBDMDEXISTS      (0x00000002UL)
95
96 #define OBD_INLINESZ    60
97 #define OBD_OBDMDSZ     60
98 /* Note: 64-bit types are 64-bit aligned in structure */
99 struct obdo {
100         obd_id                  o_id;
101         obd_gr                  o_gr;
102         obd_time                o_atime;
103         obd_time                o_mtime;
104         obd_time                o_ctime;
105         obd_size                o_size;
106         obd_blocks              o_blocks;
107         obd_blksize             o_blksize;
108         obd_mode                o_mode;
109         obd_uid                 o_uid;
110         obd_gid                 o_gid;
111         obd_flag                o_flags;
112         obd_flag                o_obdflags;
113         obd_count               o_nlink;
114         obd_count               o_generation;
115         obd_flag                o_valid;        /* hot fields in this obdo */
116         char                    o_inline[OBD_INLINESZ];
117         char                    o_obdmd[OBD_OBDMDSZ];
118         struct list_head        o_list;
119         struct obd_ops          *o_op;
120 };
121
122 #define OBD_MD_FLALL    (~0UL)
123 #define OBD_MD_FLID     (0x00000001UL)
124 #define OBD_MD_FLATIME  (0x00000002UL)
125 #define OBD_MD_FLMTIME  (0x00000004UL)
126 #define OBD_MD_FLCTIME  (0x00000008UL)
127 #define OBD_MD_FLSIZE   (0x00000010UL)
128 #define OBD_MD_FLBLOCKS (0x00000020UL)
129 #define OBD_MD_FLBLKSZ  (0x00000040UL)
130 #define OBD_MD_FLMODE   (0x00000080UL)
131 #define OBD_MD_FLTYPE   (0x00000100UL)
132 #define OBD_MD_FLUID    (0x00000200UL)
133 #define OBD_MD_FLGID    (0x00000400UL)
134 #define OBD_MD_FLFLAGS  (0x00000800UL)
135 #define OBD_MD_FLOBDFLG (0x00001000UL)
136 #define OBD_MD_FLNLINK  (0x00002000UL)
137 #define OBD_MD_FLGENER  (0x00004000UL)
138 #define OBD_MD_FLINLINE (0x00008000UL)
139 #define OBD_MD_FLOBDMD  (0x00010000UL)
140 #define OBD_MD_FLOBJID  (0x00020000UL)
141 #define OBD_MD_FLNOTOBD (~(OBD_MD_FLOBDMD | OBD_MD_FLOBDFLG | OBD_MD_FLBLOCKS))
142
143 /* request structure for OST's */
144
145 #define OST_REQ_HAS_OA1  0x1
146
147 struct ost_req { 
148         __u32   connid;
149         __u32   cmd; 
150         struct  obdo oa;
151         __u32   buflen1;
152         __u32   buflen2;
153 };
154
155 struct ost_rep {
156         __u32   result;
157         __u32   connid;
158         struct  obdo oa;
159         __u32   buflen1;
160         __u32   buflen2;
161 };
162
163 struct obd_ioobj { 
164         obd_id    ioo_id;
165         obd_gr    ioo_gr;
166         __u32     ioo_type;
167         __u32     ioo_bufcnt;
168 };
169
170
171 /* 
172  *   MDS REQ RECORDS
173  */
174
175
176 #define MDS_TYPE_REQ 1
177 #define MDS_TYPE_REP 2
178 #define MDS_TYPE_ERR 3
179
180 #define MDS_GETATTR   1
181 #define MDS_REINT     2
182 #define MDS_READPAGE  3
183
184 #define REINT_SETATTR 0
185 #define REINT_CREATE  1
186 #define REINT_MAX     1
187
188 struct ll_fid { 
189         __u64 id;
190         __u32 generation;
191         __u32 f_type;
192 };
193
194 struct niobuf { 
195         __u64 addr;
196         __u64 offset; 
197         __u32 len;
198         __u32 flags;
199 };
200
201 struct mds_req {
202         struct ll_fid        fid1;
203         struct ll_fid        fid2;
204         __u32                       namelen;
205         __u32                       tgtlen;
206         __u32                       opcode;
207         __u32                       valid;
208         __u32                       mode;
209         __u32                       uid;
210         __u32                       gid;
211         __u64                       size;
212         __u32                       mtime;
213         __u32                       ctime;
214         __u32                       atime;
215         __u32                       flags;
216         __u32                       major;
217         __u32                       minor;
218         __u32                       ino;
219         __u32                       nlink;
220         __u32                       generation;
221         __u64                       objid;
222 };
223
224 struct mds_rep {
225         struct ll_fid               fid1;
226         struct ll_fid               fid2;
227         __u32                       namelen;
228         __u32                       tgtlen;
229         __u32                       valid;
230         __u32                       mode;
231         __u32                       uid;
232         __u32                       gid;
233         __u64                       size;
234         __u32                       mtime;
235         __u32                       ctime;
236         __u32                       atime;
237         __u32                       flags;
238         __u32                       major;
239         __u32                       minor;
240         __u32                       ino;
241         __u32                       nlink;
242         __u32                       generation;
243         __u64                       objid;
244 };
245
246
247 /* MDS update records */ 
248
249 struct mds_update_record_hdr { 
250         __u32 ur_reclen;
251         __u32 ur_opcode;
252 };
253
254 struct mds_rec_setattr { 
255         __u32           sa_reclen;
256         __u32           sa_opcode;
257         struct ll_fid   sa_fid;
258         __u32           sa_valid;
259         __u32           sa_mode;
260         __u32           sa_uid;
261         __u32           sa_gid;
262         __u64           sa_size;
263         __u64           sa_atime;
264         __u64           sa_mtime;
265         __u64           sa_ctime;
266         __u32           sa_attr_flags;
267 };
268
269 struct mds_rec_create { 
270         __u32           cr_reclen;
271         __u32           cr_opcode;
272         struct ll_fid   cr_fid;
273         __u32           cr_uid;
274         __u32           cr_gid;
275         __u64           cr_time;
276         __u32           cr_mode;
277         /* overloaded: id for create, tgtlen for symlink, rdev for mknod */ 
278         __u64           cr_id; 
279         __u32           cr_namelen;
280         /* name here */
281 };
282
283 #ifdef __KERNEL__ 
284
285 static inline void ll_ino2fid(struct ll_fid *fid, ino_t ino, __u32 generation, int type)
286 {
287         fid->id = HTON__u64((__u64)ino);
288         fid->generation = HTON__u32(generation);
289         fid->f_type = HTON__u32(type);
290 }
291
292 static inline void ll_inode2fid(struct ll_fid *fid, struct inode *inode)
293 {
294         fid->id = HTON__u64((__u64)inode->i_ino);
295         fid->generation = HTON__u32(inode->i_generation);
296         fid->f_type = HTON__u32(inode->i_mode & S_IFMT);
297 }
298
299 #endif 
300
301 /* 
302  *   OBD IOCTLS
303  */
304
305
306 #define OBD_IOCTL_VERSION 0x00010001
307
308 struct obd_ioctl_data { 
309         uint32_t ioc_len;
310         uint32_t ioc_version;
311         uint32_t ioc_conn1;
312         uint32_t ioc_conn2;
313         struct obdo ioc_obdo1;
314         struct obdo ioc_obdo2;
315         obd_size         ioc_count;
316         obd_off          ioc_offset;
317         uint32_t         ioc_dev;
318
319         /* buffers the kernel will treat as user pointers */
320         uint32_t ioc_plen1;
321         char    *ioc_pbuf1;
322         uint32_t ioc_plen2;
323         char    *ioc_pbuf2;
324
325         /* two inline buffers */
326         uint32_t ioc_inllen1;
327         char    *ioc_inlbuf1;
328         uint32_t ioc_inllen2;
329         char    *ioc_inlbuf2;
330
331         char    ioc_bulk[0];
332 };
333
334 struct obd_ioctl_hdr { 
335         uint32_t ioc_len;
336         uint32_t ioc_version;
337 };
338
339 static inline int obd_ioctl_packlen(struct obd_ioctl_data *data)
340 {
341         int len = sizeof(struct obd_ioctl_data);
342         len += size_round(data->ioc_inllen1);
343         len += size_round(data->ioc_inllen2);
344         return len;
345 }
346
347 static inline int obd_ioctl_is_invalid(struct obd_ioctl_data *data)
348 {
349         if (data->ioc_len > (1<<30)) { 
350                 printk("OBD ioctl: ioc_len larger than 1<<30\n");
351                 return 1;
352         }
353         if (data->ioc_inllen1 > (1<<30)) { 
354                 printk("OBD ioctl: ioc_inllen1 larger than 1<<30\n");
355                 return 1;
356         }
357         if (data->ioc_inllen2 > (1<<30)) { 
358                 printk("OBD ioctl: ioc_inllen2 larger than 1<<30\n");
359                 return 1;
360         }
361         if (data->ioc_inlbuf1 && !data->ioc_inllen1) {
362                 printk("OBD ioctl: inlbuf1 pointer but 0 length\n");
363                 return 1;
364         }
365         if (data->ioc_inlbuf2 && !data->ioc_inllen2) {
366                 printk("OBD ioctl: inlbuf2 pointer but 0 length\n");
367                 return 1;
368         }
369         if (data->ioc_pbuf1 && !data->ioc_plen1) {
370                 printk("OBD ioctl: pbuf1 pointer but 0 length\n");
371                 return 1;
372         }
373         if (data->ioc_pbuf2 && !data->ioc_plen2) {
374                 printk("OBD ioctl: pbuf2 pointer but 0 length\n");
375                 return 1;
376         }
377         if (obd_ioctl_packlen(data) != data->ioc_len ) {
378                 printk("OBD ioctl: packlen exceeds ioc_len\n");
379                 return 1;
380         }
381         if (data->ioc_inllen1 && 
382             data->ioc_bulk[data->ioc_inllen1 - 1] != '\0') { 
383                 printk("OBD ioctl: inlbuf1 not 0 terminated\n");
384                 return 1;
385         }
386         if (data->ioc_inllen2 && 
387             data->ioc_bulk[size_round(data->ioc_inllen1) + data->ioc_inllen2 - 1] != '\0') { 
388                 printk("OBD ioctl: inlbuf2 not 0 terminated\n");
389                 return 1;
390         }
391         return 0;
392 }
393
394 #ifndef __KERNEL__
395 static inline int obd_ioctl_pack(struct obd_ioctl_data *data, char **pbuf, int max)
396 {
397         char *ptr;
398         struct obd_ioctl_data *overlay;
399         data->ioc_len = obd_ioctl_packlen(data);
400         data->ioc_version = OBD_IOCTL_VERSION;
401
402         if (*pbuf && obd_ioctl_packlen(data) > max) 
403                 return 1;
404         if (*pbuf == NULL) { 
405                 *pbuf = malloc(data->ioc_len);
406         }
407         if (!*pbuf)
408                 return 1;
409         overlay = (struct obd_ioctl_data *)*pbuf;
410         memcpy(*pbuf, data, sizeof(*data));
411
412         ptr = overlay->ioc_bulk;
413         if (data->ioc_inlbuf1)
414                 LOGL(data->ioc_inlbuf1, data->ioc_inllen1, ptr);
415         if (data->ioc_inlbuf2)
416                 LOGL(data->ioc_inlbuf2, data->ioc_inllen2, ptr);
417         if (obd_ioctl_is_invalid(overlay))
418                 return 1;
419
420         return 0;
421 }
422 #else
423
424
425 /* buffer MUST be at least the size of obd_ioctl_hdr */
426 static inline int obd_ioctl_getdata(char *buf, char *end, void *arg)
427 {
428         struct obd_ioctl_hdr *hdr;
429         struct obd_ioctl_data *data;
430         int err;
431         ENTRY;
432
433         hdr = (struct obd_ioctl_hdr *)buf;
434         data = (struct obd_ioctl_data *)buf;
435
436         err = copy_from_user(buf, (void *)arg, sizeof(*hdr));
437         if ( err ) {
438                 EXIT;
439                 return err;
440         }
441
442         if (hdr->ioc_version != OBD_IOCTL_VERSION) { 
443                 printk("OBD: version mismatch kernel vs application\n");
444                 return -EINVAL;
445         }
446
447         if (hdr->ioc_len + buf >= end) { 
448                 printk("OBD: user buffer exceeds kernel buffer\n");
449                 return -EINVAL;
450         }
451
452
453         if (hdr->ioc_len < sizeof(struct obd_ioctl_data)) { 
454                 printk("OBD: user buffer too small for ioctl\n");
455                 return -EINVAL;
456         }
457
458         err = copy_from_user(buf, (void *)arg, hdr->ioc_len);
459         if ( err ) {
460                 EXIT;
461                 return err;
462         }
463
464         if (obd_ioctl_is_invalid(data)) { 
465                 printk("OBD: ioctl not correctly formatted\n");
466                 return -EINVAL;
467         }
468
469         if (data->ioc_inllen1) { 
470                 data->ioc_inlbuf1 = &data->ioc_bulk[0];
471         }
472
473         if (data->ioc_inllen2) { 
474                 data->ioc_inlbuf2 = &data->ioc_bulk[0] + size_round(data->ioc_inllen1);
475         }
476
477         EXIT;
478         return 0;
479 }
480 #endif
481
482
483 #define OBD_IOC_CREATE                 _IOR ('f',  3, long)
484 #define OBD_IOC_SETUP                  _IOW ('f',  4, long)
485 #define OBD_IOC_CLEANUP                _IO  ('f',  5      )
486 #define OBD_IOC_DESTROY                _IOW ('f',  6, long)
487 #define OBD_IOC_PREALLOCATE            _IOWR('f',  7, long)
488 #define OBD_IOC_DEC_USE_COUNT          _IO  ('f',  8      )
489 #define OBD_IOC_SETATTR                _IOW ('f',  9, long)
490 #define OBD_IOC_GETATTR                _IOR ('f', 10, long)
491 #define OBD_IOC_READ                   _IOWR('f', 11, long)
492 #define OBD_IOC_WRITE                  _IOWR('f', 12, long)
493 #define OBD_IOC_CONNECT                _IOR ('f', 13, long)
494 #define OBD_IOC_DISCONNECT             _IOW ('f', 14, long)
495 #define OBD_IOC_STATFS                 _IOWR('f', 15, long)
496 #define OBD_IOC_SYNC                   _IOR ('f', 16, long)
497 #define OBD_IOC_READ2                  _IOWR('f', 17, long)
498 #define OBD_IOC_FORMAT                 _IOWR('f', 18, long)
499 #define OBD_IOC_PARTITION              _IOWR('f', 19, long)
500 #define OBD_IOC_ATTACH                 _IOWR('f', 20, long)
501 #define OBD_IOC_DETACH                 _IOWR('f', 21, long)
502 #define OBD_IOC_COPY                   _IOWR('f', 22, long)
503 #define OBD_IOC_MIGR                   _IOWR('f', 23, long)
504 #define OBD_IOC_PUNCH                  _IOWR('f', 24, long)
505 #define OBD_IOC_DEVICE                 _IOWR('f', 25, long)
506
507 #define OBD_IOC_DEC_FS_USE_COUNT       _IO  ('f', 32      )
508
509
510 /* GENERAL THINGS */
511 union ptl_rep { 
512         struct mds_rep *mds;
513         struct ost_rep *ost;
514 };
515
516 union ptl_req { 
517         struct mds_req *mds;
518         struct ost_req *ost;
519 };
520
521
522 #endif