Whamcloud - gitweb
Ensure that the file type is valid.
[fs/lustre-release.git] / lustre / lib / pack_generic.c
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
25 #include <linux/module.h>
26 #include <linux/errno.h>
27 #include <linux/kernel.h>
28 #include <linux/major.h>
29 #include <linux/sched.h>
30 #include <linux/lp.h>
31 #include <linux/slab.h>
32 #include <linux/ioport.h>
33 #include <linux/fcntl.h>
34 #include <linux/delay.h>
35 #include <linux/skbuff.h>
36 #include <linux/proc_fs.h>
37 #include <linux/fs.h>
38 #include <linux/poll.h>
39 #include <linux/init.h>
40 #include <linux/list.h>
41 #include <asm/io.h>
42 #include <asm/segment.h>
43 #include <asm/system.h>
44 #include <asm/poll.h>
45 #include <asm/uaccess.h>
46
47 #define DEBUG_SUBSYSTEM S_CLASS
48
49 #include <linux/obd_class.h>
50 #include <linux/lustre_net.h>
51
52 int lustre_pack_msg(int count, int *lens, char **bufs, int *len, char **buf)
53 {
54         char *ptr;
55         struct lustre_msg *m;
56         int size = 0;
57         int i;
58
59         for (i=0 ; i<count; i++) { 
60                 size += size_round(lens[i]);
61         }
62         *len = sizeof(*m) + size; 
63
64         OBD_ALLOC(*buf, *len);
65         if (!*buf) {
66                 EXIT;
67                 return -ENOMEM;
68         }
69
70         memset(*buf, 0, *len); 
71         m = (struct lustre_msg *)(*buf);
72         m->type = PTL_RPC_REQUEST;
73
74         m->bufcount = HTON__u32(count);
75         for (i=0 ; i<count; i++) { 
76                 m->buflens[i] = HTON__u32(lens[i]);
77         }
78         
79         ptr = *buf + sizeof(*m) + sizeof(__u32) * count;
80         for (i=0 ; i<count ; i++) { 
81                 LOGL(buf[i], lens[i], ptr); 
82         }
83
84         return 0;
85 }
86
87 int lustre_unpack_msg(char *buf, int len)
88 {
89         struct lustre_msg *m = (struct lustre_msg *)buf;
90         int required_len, i;
91
92         required_len = sizeof(*m);
93         if (len < required_len) { 
94                 RETURN(-EINVAL);
95         }
96
97         m->bufcount = NTOH__u32(m->bufcount); 
98
99         required_len += m->bufcount * sizeof(__u32); 
100         if (len < required_len) { 
101                 RETURN(-EINVAL);
102         }
103
104         for (i=0; i<m->bufcount; i++) { 
105                 m->buflens[i] = NTOH__u32(m->buflens[i]);
106                 required_len += size_round(m->buflens[i]);
107         }
108
109         if (len < required_len) { 
110                 RETURN(-EINVAL);
111         }
112
113         EXIT;
114         return 0;
115 }
116
117 void *lustre_msg_buf(int n, struct lustre_msg *m)
118 {
119         int i;
120         int offset;
121
122         if (n >= m->bufcount || n < 0) { 
123                 CERROR("referencing bad sub buffer!\n"); 
124                 return NULL;
125         }
126
127         if (m->buflens[n] == 0)
128                 return NULL;
129
130         offset = sizeof(*m) + m->bufcount * sizeof(__u32);
131
132         for (i=0; i < n;  i++ ) 
133                 offset += size_round(m->buflens[i]); 
134
135         return (char *)m + offset;
136 }