ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Linux-NBD/NBD.xs
Revision: 1.2
Committed: Fri May 9 00:15:14 2003 UTC (21 years ago) by root
Branch: MAIN
Changes since 1.1: +8 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4
5 #include <inttypes.h>
6 #include <unistd.h>
7 #include <endian.h>
8
9 #include <sys/ioctl.h>
10 #include <netinet/in.h>
11 #include <byteswap.h>
12
13 typedef uint32_t u32;
14 typedef uint64_t u64;
15 #include <linux/nbd.h>
16
17 #if __BYTE_ORDER == __BIG_ENDIAN
18 #define ntohll(netlong) (netlong)
19 #elif __BYTE_ORDER == __LITTLE_ENDIAN
20 #define ntohll(netlong) __bswap_64(netlong)
21 #else
22 error, you should not exist
23 #endif
24
25 MODULE = Linux::NBD PACKAGE = Linux::NBD::Client
26
27 void
28 _set_sock (int dev, int fd)
29 CODE:
30 ioctl (dev, NBD_SET_SOCK, (unsigned long)fd);
31
32 void
33 _doit (int dev, int server = 0)
34 CODE:
35 if (server)
36 for (server = 0; server < 4095; server++)
37 if (server != dev)
38 close (server);
39
40 ioctl (dev, NBD_DO_IT);
41
42 if (server)
43 _exit (0);
44
45 void
46 _disconnect (int dev)
47 CODE:
48 ioctl (dev, NBD_DISCONNECT);
49
50 void
51 _clear_sock (int dev)
52 CODE:
53 ioctl (dev, NBD_CLEAR_SOCK);
54
55 void
56 _clear_que (int dev)
57 CODE:
58 ioctl (dev, NBD_CLEAR_QUE);
59
60 void
61 _set_blksize (int dev, unsigned long blocksize)
62 CODE:
63 ioctl (dev, NBD_SET_BLKSIZE, blocksize);
64
65 void
66 _set_size (int dev, unsigned long size)
67 CODE:
68 ioctl (dev, NBD_SET_BLKSIZE, size);
69
70 void
71 _set_size_blocks (int dev, unsigned long nblocks)
72 CODE:
73 ioctl (dev, NBD_SET_SIZE_BLOCKS, nblocks);
74
75 MODULE = Linux::NBD PACKAGE = Linux::NBD::Server
76
77 void
78 _one_request (SV *obj, int fd)
79 CODE:
80 struct nbd_request req;
81
82 if (read (fd, &req, sizeof (req)) == sizeof (req))
83 {
84 if (req.magic == htonl (NBD_REQUEST_MAGIC))
85 {
86 if (req.type < 2)
87 {
88 u64 from = ntohll (req.from);
89
90 PUSHMARK (SP);
91 EXTEND (SP, 3);
92 PUSHs (obj);
93 PUSHs (sv_2mortal (newSVpvn (req.handle, sizeof (req.handle))));
94 PUSHs (sv_2mortal (sizeof (UV) < 8 && from > (0xffffffffUL)
95 ? newSVnv (from)
96 : newSVuv (from)));
97 PUSHs (sv_2mortal (newSVuv (ntohl (req.len))));
98 PUTBACK;
99 call_method (req.type ? "req_write" : "req_read", G_DISCARD);
100 SPAGAIN;
101
102 XSRETURN_NO;
103 }
104 }
105 }
106
107 XSRETURN_YES;
108
109 SV *
110 _format_reply (SV *handle, unsigned int error = 0, SV *data = 0)
111 CODE:
112 struct nbd_reply rep;
113 STRLEN len;
114 char *h = SvPV (handle, len);
115
116 if (len != sizeof (rep.handle))
117 croak ("format_reply: illegal handle (length %d, should be %d)", len, sizeof (rep.handle));
118
119 rep.magic = htonl (NBD_REPLY_MAGIC);
120 rep.error = htonl (error);
121 memcpy (rep.handle, h, sizeof (rep.handle));
122
123 RETVAL = newSVpvn ((char *)&rep, sizeof (rep));
124
125 if (data && !error)
126 sv_catsv (RETVAL, data);
127
128 OUTPUT:
129 RETVAL
130