Whamcloud - gitweb
b=13872
[fs/lustre-release.git] / lustre / include / lustre_cfg.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  */
22
23 #ifndef _LUSTRE_CFG_H
24 #define _LUSTRE_CFG_H
25
26 /*
27  * 1cf6
28  * lcfG
29  */
30 #define LUSTRE_CFG_VERSION 0x1cf60001
31 #define LUSTRE_CFG_MAX_BUFCOUNT 8
32
33 #define LCFG_HDR_SIZE(count) \
34     size_round(offsetof (struct lustre_cfg, lcfg_buflens[(count)]))
35
36 /* If not LCFG_REQUIRED, we can ignore this cmd and go on. */
37 #define LCFG_REQUIRED         0x0001000
38
39 enum lcfg_command_type {
40         LCFG_ATTACH         = 0x00cf001,
41         LCFG_DETACH         = 0x00cf002,
42         LCFG_SETUP          = 0x00cf003,
43         LCFG_CLEANUP        = 0x00cf004,
44         LCFG_ADD_UUID       = 0x00cf005,
45         LCFG_DEL_UUID       = 0x00cf006,
46         LCFG_MOUNTOPT       = 0x00cf007,
47         LCFG_DEL_MOUNTOPT   = 0x00cf008,
48         LCFG_SET_TIMEOUT    = 0x00cf009,
49         LCFG_SET_UPCALL     = 0x00cf00a,
50         LCFG_ADD_CONN       = 0x00cf00b,
51         LCFG_DEL_CONN       = 0x00cf00c,
52         LCFG_LOV_ADD_OBD    = 0x00cf00d,
53         LCFG_LOV_DEL_OBD    = 0x00cf00e,
54         LCFG_PARAM          = 0x00cf00f,
55         LCFG_MARKER         = 0x00cf010,
56         LCFG_LOG_START      = 0x00ce011,
57         LCFG_LOG_END        = 0x00ce012,
58         LCFG_LOV_ADD_INA    = 0x00ce013,
59         LCFG_ADD_MDC        = 0x00cf014,
60         LCFG_DEL_MDC        = 0x00cf015,
61         LCFG_SEC_FLAVOR     = 0x00ce016,
62 };
63
64 struct lustre_cfg_bufs {
65         void    *lcfg_buf[LUSTRE_CFG_MAX_BUFCOUNT];
66         __u32    lcfg_buflen[LUSTRE_CFG_MAX_BUFCOUNT];
67         __u32    lcfg_bufcount;
68 };
69
70 /* Mountconf transitional hack, should go away after 1.6 */
71 #define LCFG_FLG_MOUNTCONF 0x400
72
73 struct lustre_cfg {
74         __u32 lcfg_version;
75         __u32 lcfg_command;
76
77         __u32 lcfg_num; 
78         __u32 lcfg_flags;
79         __u64 lcfg_nid;
80         __u32 lcfg_nal;                      /* not used any more */
81
82         __u32 lcfg_bufcount;
83         __u32 lcfg_buflens[0];
84 };
85
86 #define LUSTRE_CFG_BUFLEN(lcfg, idx)            \
87         ((lcfg)->lcfg_bufcount <= (idx)         \
88          ? 0                                    \
89          : (lcfg)->lcfg_buflens[(idx)])
90
91 static inline void lustre_cfg_bufs_set(struct lustre_cfg_bufs *bufs,
92                                        __u32                   index,
93                                        void                   *buf,
94                                        __u32                   buflen)
95 {
96         if (index >= LUSTRE_CFG_MAX_BUFCOUNT)
97                 return;
98         if (bufs == NULL)
99                 return;
100
101         if (bufs->lcfg_bufcount <= index)
102                 bufs->lcfg_bufcount = index + 1;
103
104         bufs->lcfg_buf[index]    = buf;
105         bufs->lcfg_buflen[index] = buflen;
106 }
107
108 static inline void lustre_cfg_bufs_set_string(struct lustre_cfg_bufs *bufs,
109                                               __u32 index,
110                                               char *str)
111 {
112         lustre_cfg_bufs_set(bufs, index, str, str ? strlen(str) + 1 : 0);
113 }
114
115 static inline void lustre_cfg_bufs_reset(struct lustre_cfg_bufs *bufs, char *name)
116 {
117         memset((bufs), 0, sizeof(*bufs));
118         if (name)
119                 lustre_cfg_bufs_set_string(bufs, 0, name);
120 }
121
122 static inline void *lustre_cfg_buf(struct lustre_cfg *lcfg, int index)
123 {
124         int i;
125         int offset;
126         int bufcount;
127         LASSERT (lcfg != NULL);
128         LASSERT (index >= 0);
129
130         bufcount = lcfg->lcfg_bufcount;
131         if (index >= bufcount)
132                 return NULL;
133
134         offset = LCFG_HDR_SIZE(lcfg->lcfg_bufcount);
135         for (i = 0; i < index; i++)
136                 offset += size_round(lcfg->lcfg_buflens[i]);
137         return (char *)lcfg + offset;
138 }
139
140 static inline void lustre_cfg_bufs_init(struct lustre_cfg_bufs *bufs,
141                                         struct lustre_cfg *lcfg)
142 {
143         int i;
144         bufs->lcfg_bufcount = lcfg->lcfg_bufcount;
145         for (i = 0; i < bufs->lcfg_bufcount; i++) {
146                 bufs->lcfg_buflen[i] = lcfg->lcfg_buflens[i];
147                 bufs->lcfg_buf[i] = lustre_cfg_buf(lcfg, i);
148         }
149 }
150
151 static inline char *lustre_cfg_string(struct lustre_cfg *lcfg, int index)
152 {
153         char *s;
154
155         if (!lcfg->lcfg_buflens[index])
156                 return NULL;
157
158         s = lustre_cfg_buf(lcfg, index);
159         if (!s)
160                 return NULL;
161
162         /* make sure it's NULL terminated, even if this kills a char
163          * of data.  Try to use the padding first though.
164          */
165         if (s[lcfg->lcfg_buflens[index] - 1] != '\0') {
166                 int last = min((int)lcfg->lcfg_buflens[index], 
167                                size_round(lcfg->lcfg_buflens[index]) - 1);
168                 char lost = s[last];
169                 s[last] = '\0';
170                 if (lost != '\0') {
171                         CWARN("Truncated buf %d to '%s' (lost '%c'...)\n",
172                               index, s, lost);
173                 }
174         }
175         return s;
176 }
177
178 static inline int lustre_cfg_len(__u32 bufcount, __u32 *buflens)
179 {
180         int i;
181         int len;
182         ENTRY;
183
184         len = LCFG_HDR_SIZE(bufcount);
185         for (i = 0; i < bufcount; i++)
186                 len += size_round(buflens[i]);
187
188         RETURN(size_round(len));
189 }
190
191
192 #include <obd_support.h>
193
194 static inline struct lustre_cfg *lustre_cfg_new(int cmd,
195                                                 struct lustre_cfg_bufs *bufs)
196 {
197         struct lustre_cfg *lcfg;
198         char *ptr;
199         int i;
200
201         ENTRY;
202
203         OBD_ALLOC(lcfg, lustre_cfg_len(bufs->lcfg_bufcount,
204                                        bufs->lcfg_buflen));
205         if (!lcfg)
206                 RETURN(lcfg);
207
208         lcfg->lcfg_version = LUSTRE_CFG_VERSION;
209         lcfg->lcfg_command = cmd;
210         lcfg->lcfg_bufcount = bufs->lcfg_bufcount;
211
212         ptr = (char *)lcfg + LCFG_HDR_SIZE(lcfg->lcfg_bufcount);
213         for (i = 0; i < lcfg->lcfg_bufcount; i++) {
214                 lcfg->lcfg_buflens[i] = bufs->lcfg_buflen[i];
215                 LOGL((char *)bufs->lcfg_buf[i], bufs->lcfg_buflen[i], ptr);
216         }
217         RETURN(lcfg);
218 }
219
220 static inline void lustre_cfg_free(struct lustre_cfg *lcfg)
221 {
222         int len;
223
224         len = lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens);
225
226         OBD_FREE(lcfg, len);
227         EXIT;
228         return;
229 }
230
231 static inline int lustre_cfg_sanity_check(void *buf, int len)
232 {
233         struct lustre_cfg *lcfg = (struct lustre_cfg *)buf;
234         ENTRY;
235         if (!lcfg)
236                 RETURN(-EINVAL);
237
238         /* check that the first bits of the struct are valid */
239         if (len < LCFG_HDR_SIZE(0))
240                 RETURN(-EINVAL);
241
242         if (lcfg->lcfg_version != LUSTRE_CFG_VERSION)
243                 RETURN(-EINVAL);
244         
245         if (lcfg->lcfg_bufcount >= LUSTRE_CFG_MAX_BUFCOUNT)
246                 RETURN(-EINVAL);
247
248         /* check that the buflens are valid */
249         if (len < LCFG_HDR_SIZE(lcfg->lcfg_bufcount))
250                 RETURN(-EINVAL);
251
252         /* make sure all the pointers point inside the data */
253         if (len < lustre_cfg_len(lcfg->lcfg_bufcount, lcfg->lcfg_buflens))
254                 RETURN(-EINVAL);
255
256         RETURN(0);
257 }
258
259 /* default value for nllu/nllg for llite */
260 #define NOBODY_UID      99
261 #define NOBODY_GID      99
262 #define INVALID_UID     (-1)
263
264 #endif // _LUSTRE_CFG_H