ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Linux-NBD/NBD.xs
Revision: 1.4
Committed: Thu Aug 24 12:13:40 2006 UTC (17 years, 8 months ago) by root
Branch: MAIN
CVS Tags: rel-0_91, rel-0_9
Changes since 1.3: +2 -0 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 {
81 struct nbd_request req;
82
83 if (read (fd, &req, sizeof (req)) == sizeof (req))
84 {
85 if (req.magic == htonl (NBD_REQUEST_MAGIC))
86 {
87 req.type = htonl (req.type);
88
89 if (req.type < 2)
90 {
91 u64 from = ntohll (req.from);
92
93 PUSHMARK (SP);
94 EXTEND (SP, 3);
95 PUSHs (obj);
96 PUSHs (sv_2mortal (newSVpvn (req.handle, sizeof (req.handle))));
97 PUSHs (sv_2mortal (sizeof (UV) < 8 && from > (0xffffffffUL)
98 ? newSVnv (from)
99 : newSVuv (from)));
100 PUSHs (sv_2mortal (newSVuv (ntohl (req.len))));
101 PUTBACK;
102 call_method (req.type ? "req_write" : "req_read", G_DISCARD);
103 SPAGAIN;
104
105 XSRETURN_NO;
106 }
107 }
108 }
109
110 XSRETURN_YES;
111 }
112
113 SV *
114 _format_reply (SV *handle, unsigned int error = 0, SV *data = 0)
115 CODE:
116 {
117 struct nbd_reply rep;
118 STRLEN len;
119 char *h = SvPV (handle, len);
120
121 if (len != sizeof (rep.handle))
122 croak ("format_reply: illegal handle (length %d, should be %d)", len, sizeof (rep.handle));
123
124 rep.magic = htonl (NBD_REPLY_MAGIC);
125 rep.error = htonl (error);
126 memcpy (rep.handle, h, sizeof (rep.handle));
127
128 RETVAL = newSVpvn ((char *)&rep, sizeof (rep));
129
130 if (data && !error)
131 sv_catsv (RETVAL, data);
132 }
133 OUTPUT:
134 RETVAL
135