ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Linux-NBD/NBD.xs
Revision: 1.1
Committed: Thu May 8 23:43:41 2003 UTC (21 years ago) by root
Branch: MAIN
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.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 doexit = 0)
34     CODE:
35     ioctl (dev, NBD_DO_IT);
36     if (doexit)
37     _exit (0);
38    
39     void
40     _disconnect (int dev)
41     CODE:
42     ioctl (dev, NBD_DISCONNECT);
43    
44     void
45     _clear_sock (int dev)
46     CODE:
47     ioctl (dev, NBD_CLEAR_SOCK);
48    
49     void
50     _clear_que (int dev)
51     CODE:
52     ioctl (dev, NBD_CLEAR_QUE);
53    
54     void
55     _set_blksize (int dev, unsigned long blocksize)
56     CODE:
57     ioctl (dev, NBD_SET_BLKSIZE, blocksize);
58    
59     void
60     _set_size (int dev, unsigned long size)
61     CODE:
62     ioctl (dev, NBD_SET_BLKSIZE, size);
63    
64     void
65     _set_size_blocks (int dev, unsigned long nblocks)
66     CODE:
67     ioctl (dev, NBD_SET_SIZE_BLOCKS, nblocks);
68    
69     MODULE = Linux::NBD PACKAGE = Linux::NBD::Server
70    
71     void
72     _one_request (SV *obj, int fd)
73     CODE:
74     struct nbd_request req;
75    
76     if (read (fd, &req, sizeof (req)) == sizeof (req))
77     {
78     if (req.magic == htonl (NBD_REQUEST_MAGIC))
79     {
80     if (req.type < 2)
81     {
82     u64 from = ntohll (req.from);
83    
84     PUSHMARK (SP);
85     EXTEND (SP, 3);
86     PUSHs (obj);
87     PUSHs (sv_2mortal (newSVpvn (req.handle, sizeof (req.handle))));
88     PUSHs (sv_2mortal (sizeof (UV) < 8 && from > (0xffffffffUL)
89     ? newSVnv (from)
90     : newSVuv (from)));
91     PUSHs (sv_2mortal (newSVuv (ntohl (req.len))));
92     PUTBACK;
93     call_method (req.type ? "req_write" : "req_read", G_DISCARD);
94     SPAGAIN;
95    
96     XSRETURN_NO;
97     }
98     }
99     }
100    
101     XSRETURN_YES;
102    
103     SV *
104     _format_reply (SV *handle, unsigned int error = 0, SV *data = 0)
105     CODE:
106     struct nbd_reply rep;
107     STRLEN len;
108     char *h = SvPV (handle, len);
109    
110     if (len != sizeof (rep.handle))
111     croak ("format_reply: illegal handle (length %d, should be %d)", len, sizeof (rep.handle));
112    
113     rep.magic = htonl (NBD_REPLY_MAGIC);
114     rep.error = htonl (error);
115     memcpy (rep.handle, h, sizeof (rep.handle));
116    
117     RETVAL = newSVpvn ((char *)&rep, sizeof (rep));
118    
119     if (data && !error)
120     sv_catsv (RETVAL, data);
121    
122     OUTPUT:
123     RETVAL
124