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