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>
36 obdio_iocinit (struct obdio_conn *conn)
38 memset (&conn->oc_data, 0, sizeof (conn->oc_data));
39 conn->oc_data.ioc_version = OBD_IOCTL_VERSION;
40 conn->oc_data.ioc_addr = conn->oc_conn_addr;
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 %d: %s\n",
106 device, strerror (errno));
110 conn->oc_conn_addr = conn->oc_data.ioc_addr;
111 conn->oc_conn_cookie = conn->oc_data.ioc_cookie;
120 obdio_disconnect (struct obdio_conn *conn)
123 /* obdclass will automatically close on last ref */
128 obdio_open (struct obdio_conn *conn, uint64_t oid, struct lustre_handle *fh)
132 obdio_iocinit (conn);
134 conn->oc_data.ioc_obdo1.o_id = oid;
135 conn->oc_data.ioc_obdo1.o_mode = S_IFREG;
136 conn->oc_data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
138 rc = obdio_ioctl (conn, OBD_IOC_OPEN);
141 memcpy (fh, obdo_handle(&conn->oc_data.ioc_obdo1), sizeof (*fh));
147 obdio_close (struct obdio_conn *conn, uint64_t oid, struct lustre_handle *fh)
149 obdio_iocinit (conn);
152 conn->oc_data.ioc_obdo1.o_id = oid;
153 conn->oc_data.ioc_obdo1.o_mode = S_IFREG;
154 memcpy (obdo_handle (&conn->oc_data.ioc_obdo1), fh, sizeof (*fh));
155 conn->oc_data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE |
156 OBD_MD_FLMODE | OBD_MD_FLHANDLE;
158 return (obdio_ioctl (conn, OBD_IOC_CLOSE));
162 obdio_pread (struct obdio_conn *conn, uint64_t oid,
163 char *buffer, uint32_t count, uint64_t offset)
165 obdio_iocinit (conn);
167 conn->oc_data.ioc_obdo1.o_id = oid;
168 conn->oc_data.ioc_obdo1.o_mode = S_IFREG;
169 conn->oc_data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
171 conn->oc_data.ioc_pbuf2 = buffer;
172 conn->oc_data.ioc_plen2 = count;
173 conn->oc_data.ioc_count = count;
174 conn->oc_data.ioc_offset = offset;
176 return (obdio_ioctl (conn, OBD_IOC_BRW_READ));
180 obdio_pwrite (struct obdio_conn *conn, uint64_t oid,
181 char *buffer, uint32_t count, uint64_t offset)
183 obdio_iocinit (conn);
185 conn->oc_data.ioc_obdo1.o_id = oid;
186 conn->oc_data.ioc_obdo1.o_mode = S_IFREG;
187 conn->oc_data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
189 conn->oc_data.ioc_pbuf2 = buffer;
190 conn->oc_data.ioc_plen2 = count;
191 conn->oc_data.ioc_count = count;
192 conn->oc_data.ioc_offset = offset;
194 return (obdio_ioctl (conn, OBD_IOC_BRW_WRITE));
198 obdio_enqueue (struct obdio_conn *conn, uint64_t oid,
199 int mode, uint64_t offset, uint32_t count,
200 struct lustre_handle *lh)
204 obdio_iocinit (conn);
206 conn->oc_data.ioc_obdo1.o_id = oid;
207 conn->oc_data.ioc_obdo1.o_mode = S_IFREG;
208 conn->oc_data.ioc_obdo1.o_valid = OBD_MD_FLID | OBD_MD_FLTYPE | OBD_MD_FLMODE;
210 conn->oc_data.ioc_conn1 = mode;
211 conn->oc_data.ioc_count = count;
212 conn->oc_data.ioc_offset = offset;
214 rc = obdio_ioctl (conn, ECHO_IOC_ENQUEUE);
217 memcpy (lh, obdo_handle (&conn->oc_data.ioc_obdo1), sizeof (*lh));
223 obdio_cancel (struct obdio_conn *conn, struct lustre_handle *lh)
225 obdio_iocinit (conn);
227 memcpy (obdo_handle (&conn->oc_data.ioc_obdo1), lh, sizeof (*lh));
228 conn->oc_data.ioc_obdo1.o_valid = OBD_MD_FLHANDLE;
230 return (obdio_ioctl (conn, ECHO_IOC_CANCEL));
234 obdio_alloc_aligned_buffer (void **spacep, int size)
236 int pagesize = getpagesize();
237 void *space = malloc (size + pagesize - 1);
243 return ((void *)(((unsigned long)space + pagesize - 1) & ~(pagesize - 1)));
246 struct obdio_barrier *
247 obdio_new_barrier (uint64_t oid, uint64_t id, int npeers)
249 struct obdio_barrier *b;
251 b = (struct obdio_barrier *)malloc (sizeof (*b));
253 fprintf (stderr, "obdio_new_barrier "LPX64": Can't allocate\n", oid);
259 b->ob_npeers = npeers;
266 obdio_setup_barrier (struct obdio_conn *conn, struct obdio_barrier *b)
268 struct lustre_handle fh;
269 struct lustre_handle lh;
273 struct obdio_barrier *fileb;
275 if (b->ob_ordinal != 0 ||
277 fprintf (stderr, "obdio_setup_barrier: invalid parameter\n");
281 rc = obdio_open (conn, b->ob_oid, &fh);
283 fprintf (stderr, "obdio_setup_barrier "LPX64": Failed to open object: %s\n",
284 b->ob_oid, strerror (errno));
288 fileb = (struct obdio_barrier *) obdio_alloc_aligned_buffer (&space, getpagesize ());
290 fprintf (stderr, "obdio_setup_barrier "LPX64": Can't allocate page buffer\n",
296 memset (fileb, 0, getpagesize ());
299 rc = obdio_enqueue (conn, b->ob_oid, LCK_PW, 0, getpagesize (), &lh);
301 fprintf (stderr, "obdio_setup_barrier "LPX64": Error on enqueue: %s\n",
302 b->ob_oid, strerror (errno));
306 rc = obdio_pwrite (conn, b->ob_oid, (void *)fileb, getpagesize (), 0);
308 fprintf (stderr, "obdio_setup_barrier "LPX64": Error on write: %s\n",
309 b->ob_oid, strerror (errno));
311 rc2 = obdio_cancel (conn, &lh);
312 if (rc == 0 && rc2 != 0) {
313 fprintf (stderr, "obdio_setup_barrier "LPX64": Error on cancel: %s\n",
314 b->ob_oid, strerror (errno));
320 rc2 = obdio_close (conn, b->ob_oid, &fh);
321 if (rc == 0 && rc2 != 0) {
322 fprintf (stderr, "obdio_setup_barrier "LPX64": Error on close: %s\n",
323 b->ob_oid, strerror (errno));
331 obdio_barrier (struct obdio_conn *conn, struct obdio_barrier *b)
333 struct lustre_handle fh;
334 struct lustre_handle lh;
338 struct obdio_barrier *fileb;
341 rc = obdio_open (conn, b->ob_oid, &fh);
343 fprintf (stderr, "obdio_barrier "LPX64": Error on open: %s\n",
344 b->ob_oid, strerror (errno));
348 fileb = (struct obdio_barrier *) obdio_alloc_aligned_buffer (&space, getpagesize ());
350 fprintf (stderr, "obdio_barrier "LPX64": Can't allocate page buffer\n",
356 rc = obdio_enqueue (conn, b->ob_oid, LCK_PW, 0, getpagesize (), &lh);
358 fprintf (stderr, "obdio_barrier "LPX64": Error on PW enqueue: %s\n",
359 b->ob_oid, strerror (errno));
363 memset (fileb, 0xeb, getpagesize ());
364 rc = obdio_pread (conn, b->ob_oid, (void *)fileb, getpagesize (), 0);
366 fprintf (stderr, "obdio_barrier "LPX64": Error on initial read: %s\n",
367 b->ob_oid, strerror (errno));
371 if (fileb->ob_id != b->ob_id ||
372 fileb->ob_oid != b->ob_oid ||
373 fileb->ob_npeers != b->ob_npeers ||
374 fileb->ob_count >= b->ob_npeers ||
375 fileb->ob_ordinal != b->ob_ordinal) {
376 fprintf (stderr, "obdio_barrier "LPX64": corrupt on initial read\n", b->ob_id);
377 fprintf (stderr, " got ["LPX64","LPX64","LPX64","LPX64","LPX64"]\n",
378 fileb->ob_id, fileb->ob_oid, fileb->ob_npeers,
379 fileb->ob_ordinal, fileb->ob_count);
380 fprintf (stderr, " expected ["LPX64","LPX64","LPX64","LPX64","LPX64"]\n",
381 b->ob_id, b->ob_oid, b->ob_npeers,
382 b->ob_ordinal, b->ob_count);
388 if (fileb->ob_count == fileb->ob_npeers) { /* I'm the last joiner */
389 fileb->ob_count = 0; /* join count for next barrier */
390 fileb->ob_ordinal++; /* signal all joined */
393 rc = obdio_pwrite (conn, b->ob_oid, (void *)fileb, getpagesize (), 0);
395 fprintf (stderr, "obdio_barrier "LPX64": Error on initial write: %s\n",
396 b->ob_oid, strerror (errno));
401 b->ob_ordinal++; /* now I wait... */
402 while (fileb->ob_ordinal != b->ob_ordinal) {
404 rc = obdio_cancel (conn, &lh);
406 fprintf (stderr, "obdio_barrier "LPX64": Error on %s cancel: %s\n",
407 b->ob_oid, mode, strerror (errno));
412 rc = obdio_enqueue (conn, b->ob_oid, LCK_PR, 0, getpagesize (), &lh);
414 fprintf (stderr, "obdio_barrier "LPX64": Error on PR enqueue: %s\n",
415 b->ob_oid, strerror (errno));
419 memset (fileb, 0xeb, getpagesize ());
420 rc = obdio_pread (conn, b->ob_oid, (void *)fileb, getpagesize (), 0);
422 fprintf (stderr, "obdio_barrier "LPX64": Error on read: %s\n",
423 b->ob_oid, strerror (errno));
427 if (fileb->ob_id != b->ob_id ||
428 fileb->ob_oid != b->ob_oid ||
429 fileb->ob_npeers != b->ob_npeers ||
430 fileb->ob_count >= b->ob_npeers ||
431 (fileb->ob_ordinal != b->ob_ordinal - 1 &&
432 fileb->ob_ordinal != b->ob_ordinal)) {
433 fprintf (stderr, "obdio_barrier "LPX64": corrupt\n", b->ob_id);
434 fprintf (stderr, " got ["LPX64","LPX64","LPX64","LPX64","LPX64"]\n",
435 fileb->ob_id, fileb->ob_oid, fileb->ob_npeers,
436 fileb->ob_ordinal, fileb->ob_count);
437 fprintf (stderr, " expected ["LPX64","LPX64","LPX64","LPX64","LPX64"]\n",
438 b->ob_id, b->ob_oid, b->ob_npeers,
439 b->ob_ordinal, b->ob_count);
446 rc2 = obdio_cancel (conn, &lh);
447 if (rc == 0 && rc2 != 0) {
448 fprintf (stderr, "obdio_barrier "LPX64": Error on cancel: %s\n",
449 b->ob_oid, strerror (errno));
455 rc2 = obdio_close (conn, b->ob_oid, &fh);
456 if (rc == 0 && rc2 != 0) {
457 fprintf (stderr, "obdio_barrier "LPX64": Error on close: %s\n",
458 b->ob_oid, strerror (errno));