Whamcloud - gitweb
LU-2558 test: recovery-small 19a FAIL no eviction
[fs/lustre-release.git] / lustre / utils / obdiolib.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/utils/obdiolib.c
35  *
36  * Author: Eric Barton <eeb@clusterfs.com>
37  */
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <errno.h>
42 #include <string.h>
43 #include <fcntl.h>
44 #include <sys/ioctl.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47
48 #include <liblustre.h>
49 #include "obdiolib.h"
50
51 void
52 obdio_iocinit (struct obdio_conn *conn)
53 {
54         memset (&conn->oc_data, 0, sizeof (conn->oc_data));
55         conn->oc_data.ioc_version = OBD_IOCTL_VERSION;
56         conn->oc_data.ioc_dev = conn->oc_device;
57         conn->oc_data.ioc_len = sizeof (conn->oc_data);
58 }
59
60 int
61 obdio_ioctl (struct obdio_conn *conn, int cmd)
62 {
63         char *buf = conn->oc_buffer;
64         int   rc;
65         int   rc2;
66
67         rc = obd_ioctl_pack (&conn->oc_data, &buf, sizeof (conn->oc_buffer));
68         if (rc != 0) {
69                 fprintf(stderr, "%s: obd_ioctl_pack: %d (%s)\n",
70                         __FUNCTION__, rc, strerror(errno));
71                 abort();
72         }
73
74         rc = ioctl (conn->oc_fd, cmd, buf);
75         if (rc != 0)
76                 return (rc);
77
78         rc2 = obd_ioctl_unpack (&conn->oc_data, buf, sizeof (conn->oc_buffer));
79         if (rc2 != 0) {
80                 fprintf(stderr, "%s: obd_ioctl_unpack: %d (%s)\n",
81                         __FUNCTION__, rc2, strerror(errno));
82                 abort ();
83         }
84
85         return (rc);
86 }
87
88 struct obdio_conn *
89 obdio_connect (int device)
90 {
91         struct obdio_conn  *conn;
92
93         conn = malloc (sizeof (*conn));
94         if (conn == NULL) {
95                 fprintf (stderr, "%s: no memory\n", __FUNCTION__);
96                 return (NULL);
97         }
98         memset (conn, 0, sizeof (*conn));
99
100         conn->oc_fd = open ("/dev/obd", O_RDWR);
101         if (conn->oc_fd < 0) {
102                 fprintf(stderr, "%s: Can't open /dev/obd: %s\n",
103                         __FUNCTION__, strerror(errno));
104                 goto failed;
105         }
106
107         conn->oc_device = device;
108         return (conn);
109
110  failed:
111         free (conn);
112         return (NULL);
113 }
114
115 void
116 obdio_disconnect (struct obdio_conn *conn, int flags)
117 {
118         close (conn->oc_fd);
119         /* obdclass will automatically close on last ref */
120         free (conn);
121 }
122
123 int
124 obdio_pread (struct obdio_conn *conn, __u64 oid,
125              void *buffer, __u32 count, __u64 offset)
126 {
127         obdio_iocinit (conn);
128
129         conn->oc_data.ioc_obdo1.o_id = oid;
130         conn->oc_data.ioc_obdo1.o_mode = S_IFREG;
131         conn->oc_data.ioc_obdo1.o_valid =
132                 OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
133
134         conn->oc_data.ioc_pbuf2 = buffer;
135         conn->oc_data.ioc_plen2 = count;
136         conn->oc_data.ioc_count = count;
137         conn->oc_data.ioc_offset = offset;
138
139         return (obdio_ioctl (conn, OBD_IOC_BRW_READ));
140 }
141
142 int
143 obdio_pwrite (struct obdio_conn *conn, __u64 oid,
144               void *buffer, __u32 count, __u64 offset)
145 {
146         obdio_iocinit (conn);
147
148         conn->oc_data.ioc_obdo1.o_id = oid;
149         conn->oc_data.ioc_obdo1.o_mode = S_IFREG;
150         conn->oc_data.ioc_obdo1.o_valid =
151                 OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
152
153         conn->oc_data.ioc_pbuf1 = (void*)1;
154         conn->oc_data.ioc_plen1 = 1;
155         conn->oc_data.ioc_pbuf2 = buffer;
156         conn->oc_data.ioc_plen2 = count;
157         conn->oc_data.ioc_count = count;
158         conn->oc_data.ioc_offset = offset;
159
160         return (obdio_ioctl (conn, OBD_IOC_BRW_WRITE));
161 }
162
163 int
164 obdio_enqueue (struct obdio_conn *conn, __u64 oid,
165                int mode, __u64 offset, __u32 count,
166                struct lustre_handle *lh)
167 {
168         int   rc;
169
170         obdio_iocinit (conn);
171
172         conn->oc_data.ioc_obdo1.o_id = oid;
173         conn->oc_data.ioc_obdo1.o_mode = S_IFREG;
174         conn->oc_data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
175
176         conn->oc_data.ioc_conn1 = mode;
177         conn->oc_data.ioc_count = count;
178         conn->oc_data.ioc_offset = offset;
179
180         rc = obdio_ioctl (conn, ECHO_IOC_ENQUEUE);
181
182         if (rc == 0)
183                 *lh = conn->oc_data.ioc_obdo1.o_handle;
184
185         return (rc);
186 }
187
188 int
189 obdio_cancel (struct obdio_conn *conn, struct lustre_handle *lh)
190 {
191         obdio_iocinit (conn);
192
193         conn->oc_data.ioc_obdo1.o_handle = *lh;
194         conn->oc_data.ioc_obdo1.o_valid = OBD_MD_FLHANDLE;
195
196         return (obdio_ioctl (conn, ECHO_IOC_CANCEL));
197 }
198
199 void *
200 obdio_alloc_aligned_buffer (void **spacep, int size)
201 {
202         int   pagemask = getpagesize() - 1;
203         void *space = malloc(size + pagemask);
204
205         if (space == NULL)
206                 return (NULL);
207
208         *spacep = (void *)(((unsigned long)space + pagemask) & ~pagemask);
209         return space;
210 }
211
212 struct obdio_barrier *
213 obdio_new_barrier (__u64 oid, __u64 id, int npeers)
214 {
215         struct obdio_barrier *b;
216
217         b = malloc(sizeof(*b));
218         if (b == NULL) {
219                 fprintf(stderr, "%s "LPX64": Can't allocate\n",
220                         __FUNCTION__, oid);
221                 return(NULL);
222         }
223
224         b->ob_id = id;
225         b->ob_oid = oid;
226         b->ob_npeers = npeers;
227         b->ob_ordinal = 0;
228         b->ob_count = 0;
229         return (b);
230 }
231
232 int
233 obdio_setup_barrier (struct obdio_conn *conn, struct obdio_barrier *b)
234 {
235         struct lustre_handle    lh;
236         int                     rc;
237         int                     rc2;
238         void                   *space, *fileptr;
239         struct obdio_barrier   *fileb;
240
241         if (b->ob_ordinal != 0 ||
242             b->ob_count != 0) {
243                 fprintf(stderr, "%s: invalid parameter\n", __FUNCTION__);
244                 abort ();
245         }
246
247         space = obdio_alloc_aligned_buffer(&fileptr, getpagesize());
248         if (space == NULL) {
249                 fprintf(stderr, "%s "LPX64": Can't allocate page buffer\n",
250                         __FUNCTION__, b->ob_oid);
251                 return (-1);
252         }
253
254         fileb = fileptr;
255         memset(fileb, 0, getpagesize());
256         *fileb = *b;
257
258         rc = obdio_enqueue(conn, b->ob_oid, LCK_PW, 0, getpagesize(), &lh);
259         if (rc != 0) {
260                 fprintf(stderr, "%s "LPX64": Error on enqueue: %s\n",
261                         __FUNCTION__, b->ob_oid, strerror(errno));
262                 goto out;
263         }
264
265         rc = obdio_pwrite(conn, b->ob_oid, fileb, getpagesize(), 0);
266         if (rc != 0)
267                 fprintf(stderr, "%s "LPX64": Error on write: %s\n",
268                         __FUNCTION__, b->ob_oid, strerror(errno));
269
270         rc2 = obdio_cancel (conn, &lh);
271         if (rc == 0 && rc2 != 0) {
272                 fprintf(stderr, "%s "LPX64": Error on cancel: %s\n",
273                         __FUNCTION__, b->ob_oid, strerror(errno));
274                 rc = rc2;
275         }
276  out:
277         free (space);
278         return (rc);
279 }
280
281 int
282 obdio_barrier (struct obdio_conn *conn, struct obdio_barrier *b)
283 {
284         struct lustre_handle   lh;
285         int                    rc;
286         int                    rc2;
287         void                  *space, *fileptr;
288         struct obdio_barrier  *fileb;
289         char                  *mode;
290
291         space = obdio_alloc_aligned_buffer(&fileptr, getpagesize());
292         if (space == NULL) {
293                 fprintf(stderr, "%s "LPX64": Can't allocate page buffer\n",
294                         __FUNCTION__, b->ob_oid);
295                 return (-1);
296         }
297
298         rc = obdio_enqueue(conn, b->ob_oid, LCK_PW, 0, getpagesize(), &lh);
299         if (rc != 0) {
300                 fprintf(stderr, "%s "LPX64": Error on PW enqueue: %s\n",
301                         __FUNCTION__, b->ob_oid, strerror(errno));
302                 goto out_1;
303         }
304
305         fileb = fileptr;
306         memset(fileb, 0xeb, getpagesize());
307         rc = obdio_pread(conn, b->ob_oid, fileb, getpagesize(), 0);
308         if (rc != 0) {
309                 fprintf(stderr, "%s "LPX64": Error on initial read: %s\n",
310                         __FUNCTION__, b->ob_oid, strerror(errno));
311                 goto out_2;
312         }
313
314         if (fileb->ob_id != b->ob_id ||
315             fileb->ob_oid != b->ob_oid ||
316             fileb->ob_npeers != b->ob_npeers ||
317             fileb->ob_count >= b->ob_npeers ||
318             fileb->ob_ordinal != b->ob_ordinal) {
319                 fprintf(stderr, "%s "LPX64": corrupt on initial read\n",
320                         __FUNCTION__, b->ob_id);
321                 fprintf(stderr,
322                         "  got ["LPX64","LPX64","LPX64","LPX64","LPX64"]\n",
323                         fileb->ob_id, fileb->ob_oid, fileb->ob_npeers,
324                         fileb->ob_ordinal, fileb->ob_count);
325                 fprintf(stderr,
326                        "  expected ["LPX64","LPX64","LPX64","LPX64","LPX64"]\n",
327                         b->ob_id, b->ob_oid, b->ob_npeers,
328                         b->ob_ordinal, b->ob_count);
329                 rc = -1;
330                 goto out_2;
331         }
332
333         fileb->ob_count++;
334         if (fileb->ob_count == fileb->ob_npeers) { /* I'm the last joiner */
335                 fileb->ob_count = 0;       /* join count for next barrier */
336                 fileb->ob_ordinal++;                 /* signal all joined */
337         }
338
339         rc = obdio_pwrite(conn, b->ob_oid, fileb, getpagesize(), 0);
340         if (rc != 0) {
341                 fprintf (stderr, "%s "LPX64": Error on initial write: %s\n",
342                          __FUNCTION__, b->ob_oid, strerror(errno));
343                 goto out_2;
344         }
345
346         mode = "PW";
347         b->ob_ordinal++;           /* now I wait... */
348         while (fileb->ob_ordinal != b->ob_ordinal) {
349                 rc = obdio_cancel (conn, &lh);
350                 if (rc != 0) {
351                         fprintf(stderr, "%s "LPX64": Error on %s cancel: %s\n",
352                                 __FUNCTION__, b->ob_oid, mode, strerror(errno));
353                         goto out_1;
354                 }
355
356                 mode = "PR";
357                 rc = obdio_enqueue(conn, b->ob_oid, LCK_PR,0,getpagesize(),&lh);
358                 if (rc != 0) {
359                         fprintf(stderr, "%s "LPX64": Error on PR enqueue: %s\n",
360                                 __FUNCTION__, b->ob_oid, strerror(errno));
361                         goto out_1;
362                 }
363
364                 memset (fileb, 0xeb, getpagesize());
365                 rc = obdio_pread(conn, b->ob_oid, fileb, getpagesize(), 0);
366                 if (rc != 0) {
367                         fprintf(stderr, "%s "LPX64": Error on read: %s\n",
368                                 __FUNCTION__, b->ob_oid, strerror(errno));
369                         goto out_2;
370                 }
371
372                 if (fileb->ob_id != b->ob_id ||
373                     fileb->ob_oid != b->ob_oid ||
374                     fileb->ob_npeers != b->ob_npeers ||
375                     fileb->ob_count >= b->ob_npeers ||
376                     (fileb->ob_ordinal != b->ob_ordinal - 1 &&
377                      fileb->ob_ordinal != b->ob_ordinal)) {
378                         fprintf(stderr, "%s "LPX64": corrupt\n",
379                                 __FUNCTION__, b->ob_id);
380                         fprintf(stderr, "  got ["LPX64","LPX64","LPX64","
381                                 LPX64","LPX64"]\n",
382                                 fileb->ob_id, fileb->ob_oid, fileb->ob_npeers,
383                                 fileb->ob_ordinal, fileb->ob_count);
384                         fprintf(stderr, "  expected ["LPX64","LPX64","LPX64
385                                 ","LPX64","LPX64"]\n",
386                                 b->ob_id, b->ob_oid, b->ob_npeers,
387                                 b->ob_ordinal, b->ob_count);
388                         rc = -1;
389                         goto out_2;
390                 }
391         }
392
393  out_2:
394         rc2 = obdio_cancel (conn, &lh);
395         if (rc == 0 && rc2 != 0) {
396                 fprintf(stderr, "%s "LPX64": Error on cancel: %s\n",
397                         __FUNCTION__, b->ob_oid, strerror(errno));
398                 rc = rc2;
399         }
400  out_1:
401         free (space);
402         return (rc);
403 }