1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2003 Cluster File Systems, Inc.
5 * Author: Eric Barton <eeb@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <sys/ioctl.h>
30 #include <sys/types.h>
33 #include <liblustre.h>
37 obdio_iocinit (struct obdio_conn *conn)
39 memset (&conn->oc_data, 0, sizeof (conn->oc_data));
40 conn->oc_data.ioc_version = OBD_IOCTL_VERSION;
41 conn->oc_data.ioc_cookie = conn->oc_conn_cookie;
42 conn->oc_data.ioc_len = sizeof (conn->oc_data);
46 obdio_ioctl (struct obdio_conn *conn, int cmd)
48 char *buf = conn->oc_buffer;
52 rc = obd_ioctl_pack (&conn->oc_data, &buf, sizeof (conn->oc_buffer));
54 fprintf (stderr, "obdio_ioctl: obd_ioctl_pack: %d (%s)\n",
55 rc, strerror (errno));
59 rc = ioctl (conn->oc_fd, cmd, buf);
63 rc2 = obd_ioctl_unpack (&conn->oc_data, buf, sizeof (conn->oc_buffer));
65 fprintf (stderr, "obdio_ioctl: obd_ioctl_unpack: %d (%s)\n",
66 rc2, strerror (errno));
74 obdio_connect (int device)
76 struct obdio_conn *conn;
79 conn = malloc (sizeof (*conn));
81 fprintf (stderr, "obdio_connect: no memory\n");
84 memset (conn, 0, sizeof (*conn));
86 conn->oc_fd = open ("/dev/obd", O_RDWR);
87 if (conn->oc_fd < 0) {
88 fprintf (stderr, "obdio_connect: Can't open /dev/obd: %s\n",
94 conn->oc_data.ioc_dev = device;
95 rc = obdio_ioctl (conn, OBD_IOC_DEVICE);
97 fprintf (stderr, "obdio_connect: Can't set device %d: %s\n",
98 device, strerror (errno));
102 obdio_iocinit (conn);
103 rc = obdio_ioctl (conn, OBD_IOC_CONNECT);
105 fprintf(stderr, "obdio_connect: Can't connect to device "
106 "%d: %s\n", device, strerror (errno));
110 conn->oc_conn_cookie = conn->oc_data.ioc_cookie;
119 obdio_disconnect (struct obdio_conn *conn, int flags)
122 /* obdclass will automatically close on last ref */
127 obdio_open (struct obdio_conn *conn, uint64_t oid, struct lustre_handle *fh)
131 obdio_iocinit (conn);
133 conn->oc_data.ioc_obdo1.o_id = oid;
134 conn->oc_data.ioc_obdo1.o_mode = S_IFREG;
135 conn->oc_data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
137 rc = obdio_ioctl (conn, OBD_IOC_OPEN);
140 memcpy (fh, obdo_handle(&conn->oc_data.ioc_obdo1), sizeof (*fh));
146 obdio_close (struct obdio_conn *conn, uint64_t oid, struct lustre_handle *fh)
148 obdio_iocinit (conn);
151 conn->oc_data.ioc_obdo1.o_id = oid;
152 conn->oc_data.ioc_obdo1.o_mode = S_IFREG;
153 memcpy (obdo_handle (&conn->oc_data.ioc_obdo1), fh, sizeof (*fh));
154 conn->oc_data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
155 OBD_MD_FLMODE | OBD_MD_FLHANDLE;
157 return (obdio_ioctl (conn, OBD_IOC_CLOSE));
161 obdio_pread (struct obdio_conn *conn, uint64_t oid,
162 char *buffer, uint32_t count, uint64_t offset)
164 obdio_iocinit (conn);
166 conn->oc_data.ioc_obdo1.o_id = oid;
167 conn->oc_data.ioc_obdo1.o_mode = S_IFREG;
168 conn->oc_data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
170 conn->oc_data.ioc_pbuf2 = buffer;
171 conn->oc_data.ioc_plen2 = count;
172 conn->oc_data.ioc_count = count;
173 conn->oc_data.ioc_offset = offset;
175 return (obdio_ioctl (conn, OBD_IOC_BRW_READ));
179 obdio_pwrite (struct obdio_conn *conn, uint64_t oid,
180 char *buffer, uint32_t count, uint64_t offset)
182 obdio_iocinit (conn);
184 conn->oc_data.ioc_obdo1.o_id = oid;
185 conn->oc_data.ioc_obdo1.o_mode = S_IFREG;
186 conn->oc_data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
188 conn->oc_data.ioc_pbuf2 = buffer;
189 conn->oc_data.ioc_plen2 = count;
190 conn->oc_data.ioc_count = count;
191 conn->oc_data.ioc_offset = offset;
193 return (obdio_ioctl (conn, OBD_IOC_BRW_WRITE));
197 obdio_enqueue (struct obdio_conn *conn, uint64_t oid,
198 int mode, uint64_t offset, uint32_t count,
199 struct lustre_handle *lh)
203 obdio_iocinit (conn);
205 conn->oc_data.ioc_obdo1.o_id = oid;
206 conn->oc_data.ioc_obdo1.o_mode = S_IFREG;
207 conn->oc_data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
209 conn->oc_data.ioc_conn1 = mode;
210 conn->oc_data.ioc_count = count;
211 conn->oc_data.ioc_offset = offset;
213 rc = obdio_ioctl (conn, ECHO_IOC_ENQUEUE);
216 memcpy (lh, obdo_handle (&conn->oc_data.ioc_obdo1), sizeof (*lh));
222 obdio_cancel (struct obdio_conn *conn, struct lustre_handle *lh)
224 obdio_iocinit (conn);
226 memcpy (obdo_handle (&conn->oc_data.ioc_obdo1), lh, sizeof (*lh));
227 conn->oc_data.ioc_obdo1.o_valid = OBD_MD_FLHANDLE;
229 return (obdio_ioctl (conn, ECHO_IOC_CANCEL));
233 obdio_alloc_aligned_buffer (void **spacep, int size)
235 int pagesize = getpagesize();
236 void *space = malloc (size + pagesize - 1);
242 return ((void *)(((unsigned long)space + pagesize - 1) & ~(pagesize - 1)));
245 struct obdio_barrier *
246 obdio_new_barrier (uint64_t oid, uint64_t id, int npeers)
248 struct obdio_barrier *b;
250 b = (struct obdio_barrier *)malloc (sizeof (*b));
252 fprintf (stderr, "obdio_new_barrier "LPX64": Can't allocate\n", oid);
258 b->ob_npeers = npeers;
265 obdio_setup_barrier (struct obdio_conn *conn, struct obdio_barrier *b)
267 struct lustre_handle fh;
268 struct lustre_handle lh;
272 struct obdio_barrier *fileb;
274 if (b->ob_ordinal != 0 ||
276 fprintf (stderr, "obdio_setup_barrier: invalid parameter\n");
280 rc = obdio_open (conn, b->ob_oid, &fh);
282 fprintf (stderr, "obdio_setup_barrier "LPX64": Failed to open object: %s\n",
283 b->ob_oid, strerror (errno));
287 fileb = (struct obdio_barrier *) obdio_alloc_aligned_buffer (&space, getpagesize ());
289 fprintf (stderr, "obdio_setup_barrier "LPX64": Can't allocate page buffer\n",
295 memset (fileb, 0, getpagesize ());
298 rc = obdio_enqueue (conn, b->ob_oid, LCK_PW, 0, getpagesize (), &lh);
300 fprintf (stderr, "obdio_setup_barrier "LPX64": Error on enqueue: %s\n",
301 b->ob_oid, strerror (errno));
305 rc = obdio_pwrite (conn, b->ob_oid, (void *)fileb, getpagesize (), 0);
307 fprintf (stderr, "obdio_setup_barrier "LPX64": Error on write: %s\n",
308 b->ob_oid, strerror (errno));
310 rc2 = obdio_cancel (conn, &lh);
311 if (rc == 0 && rc2 != 0) {
312 fprintf (stderr, "obdio_setup_barrier "LPX64": Error on cancel: %s\n",
313 b->ob_oid, strerror (errno));
319 rc2 = obdio_close (conn, b->ob_oid, &fh);
320 if (rc == 0 && rc2 != 0) {
321 fprintf (stderr, "obdio_setup_barrier "LPX64": Error on close: %s\n",
322 b->ob_oid, strerror (errno));
330 obdio_barrier (struct obdio_conn *conn, struct obdio_barrier *b)
332 struct lustre_handle fh;
333 struct lustre_handle lh;
337 struct obdio_barrier *fileb;
340 rc = obdio_open (conn, b->ob_oid, &fh);
342 fprintf (stderr, "obdio_barrier "LPX64": Error on open: %s\n",
343 b->ob_oid, strerror (errno));
347 fileb = (struct obdio_barrier *) obdio_alloc_aligned_buffer (&space, getpagesize ());
349 fprintf (stderr, "obdio_barrier "LPX64": Can't allocate page buffer\n",
355 rc = obdio_enqueue (conn, b->ob_oid, LCK_PW, 0, getpagesize (), &lh);
357 fprintf (stderr, "obdio_barrier "LPX64": Error on PW enqueue: %s\n",
358 b->ob_oid, strerror (errno));
362 memset (fileb, 0xeb, getpagesize ());
363 rc = obdio_pread (conn, b->ob_oid, (void *)fileb, getpagesize (), 0);
365 fprintf (stderr, "obdio_barrier "LPX64": Error on initial read: %s\n",
366 b->ob_oid, strerror (errno));
370 if (fileb->ob_id != b->ob_id ||
371 fileb->ob_oid != b->ob_oid ||
372 fileb->ob_npeers != b->ob_npeers ||
373 fileb->ob_count >= b->ob_npeers ||
374 fileb->ob_ordinal != b->ob_ordinal) {
375 fprintf (stderr, "obdio_barrier "LPX64": corrupt on initial read\n", b->ob_id);
376 fprintf (stderr, " got ["LPX64","LPX64","LPX64","LPX64","LPX64"]\n",
377 fileb->ob_id, fileb->ob_oid, fileb->ob_npeers,
378 fileb->ob_ordinal, fileb->ob_count);
379 fprintf (stderr, " expected ["LPX64","LPX64","LPX64","LPX64","LPX64"]\n",
380 b->ob_id, b->ob_oid, b->ob_npeers,
381 b->ob_ordinal, b->ob_count);
387 if (fileb->ob_count == fileb->ob_npeers) { /* I'm the last joiner */
388 fileb->ob_count = 0; /* join count for next barrier */
389 fileb->ob_ordinal++; /* signal all joined */
392 rc = obdio_pwrite (conn, b->ob_oid, (void *)fileb, getpagesize (), 0);
394 fprintf (stderr, "obdio_barrier "LPX64": Error on initial write: %s\n",
395 b->ob_oid, strerror (errno));
400 b->ob_ordinal++; /* now I wait... */
401 while (fileb->ob_ordinal != b->ob_ordinal) {
403 rc = obdio_cancel (conn, &lh);
405 fprintf (stderr, "obdio_barrier "LPX64": Error on %s cancel: %s\n",
406 b->ob_oid, mode, strerror (errno));
411 rc = obdio_enqueue (conn, b->ob_oid, LCK_PR, 0, getpagesize (), &lh);
413 fprintf (stderr, "obdio_barrier "LPX64": Error on PR enqueue: %s\n",
414 b->ob_oid, strerror (errno));
418 memset (fileb, 0xeb, getpagesize ());
419 rc = obdio_pread (conn, b->ob_oid, (void *)fileb, getpagesize (), 0);
421 fprintf (stderr, "obdio_barrier "LPX64": Error on read: %s\n",
422 b->ob_oid, strerror (errno));
426 if (fileb->ob_id != b->ob_id ||
427 fileb->ob_oid != b->ob_oid ||
428 fileb->ob_npeers != b->ob_npeers ||
429 fileb->ob_count >= b->ob_npeers ||
430 (fileb->ob_ordinal != b->ob_ordinal - 1 &&
431 fileb->ob_ordinal != b->ob_ordinal)) {
432 fprintf (stderr, "obdio_barrier "LPX64": corrupt\n", b->ob_id);
433 fprintf (stderr, " got ["LPX64","LPX64","LPX64","LPX64","LPX64"]\n",
434 fileb->ob_id, fileb->ob_oid, fileb->ob_npeers,
435 fileb->ob_ordinal, fileb->ob_count);
436 fprintf (stderr, " expected ["LPX64","LPX64","LPX64","LPX64","LPX64"]\n",
437 b->ob_id, b->ob_oid, b->ob_npeers,
438 b->ob_ordinal, b->ob_count);
445 rc2 = obdio_cancel (conn, &lh);
446 if (rc == 0 && rc2 != 0) {
447 fprintf (stderr, "obdio_barrier "LPX64": Error on cancel: %s\n",
448 b->ob_oid, strerror (errno));
454 rc2 = obdio_close (conn, b->ob_oid, &fh);
455 if (rc == 0 && rc2 != 0) {
456 fprintf (stderr, "obdio_barrier "LPX64": Error on close: %s\n",
457 b->ob_oid, strerror (errno));