Whamcloud - gitweb
* Landed portals:b_port_step as follows...
[fs/lustre-release.git] / lnet / libcfs / darwin / darwin-module.c
1 #include <mach/mach_types.h>
2 #include <string.h>
3 #include <sys/file.h>
4 #include <sys/conf.h>
5 #include <miscfs/devfs/devfs.h>
6
7 #define DEBUG_SUBSYSTEM S_PORTALS
8 #include <libcfs/libcfs.h>
9 #include <libcfs/kp30.h>
10
11 int portal_ioctl_getdata(char *buf, char *end, void *arg)
12 {
13         struct portal_ioctl_hdr *hdr;
14         struct portal_ioctl_data *data;
15         int err = 0;
16         ENTRY;
17
18         hdr = (struct portal_ioctl_hdr *)buf; 
19         data = (struct portal_ioctl_data *)buf;
20         /* portals_ioctl_data has been copied in by ioctl of osx */
21         memcpy(buf, arg, sizeof(struct portal_ioctl_data));
22
23         if (hdr->ioc_version != PORTAL_IOCTL_VERSION) {
24                 CERROR("PORTALS: version mismatch kernel vs application\n");
25                 RETURN(-EINVAL);
26         }
27
28         if (hdr->ioc_len + buf >= end) {
29                 CERROR("PORTALS: user buffer exceeds kernel buffer\n");
30                 RETURN(-EINVAL);
31         }
32
33         if (hdr->ioc_len < sizeof(struct portal_ioctl_data)) {
34                 CERROR("PORTALS: user buffer too small for ioctl\n");
35                 RETURN(-EINVAL);
36         }
37         buf += size_round(sizeof(*data));
38
39         if (data->ioc_inllen1) { 
40                 err = copy_from_user(buf, data->ioc_inlbuf1, size_round(data->ioc_inllen1)); 
41                 if (err)
42                         RETURN(err);
43                 data->ioc_inlbuf1 = buf; 
44                 buf += size_round(data->ioc_inllen1); 
45         } 
46         
47         if (data->ioc_inllen2) { 
48                 copy_from_user(buf, data->ioc_inlbuf2, size_round(data->ioc_inllen2)); 
49                 if (err)
50                         RETURN(err);
51                 data->ioc_inlbuf2 = buf; 
52         } 
53
54         RETURN(err);
55 }
56
57 extern struct cfs_psdev_ops             libcfs_psdev_ops;
58 struct portals_device_userstate         *mdev_state[16];
59
60 static int 
61 libcfs_psdev_open(dev_t dev, int flags, int devtype, struct proc *p)
62
63         struct  portals_device_userstate *mstat = NULL;
64         int     rc = 0;
65         int     devid; 
66         devid = minor(dev);    
67
68         if (devid > 16) return (-ENXIO);
69
70         if (libcfs_psdev_ops.p_open != NULL)
71                 rc = libcfs_psdev_ops.p_open(0, &mstat);
72         else
73                 rc = -EPERM;
74         if (!rc)
75                 return rc;
76         mdev_state[devid] = mstat;
77         return rc;
78 }
79
80 static int 
81 libcfs_psdev_close(dev_t dev, int flags, int mode, struct proc *p)
82 {
83         int     devid; 
84         devid = minor(dev);    
85         int     rc = 0;
86
87         if (devid > 16) return (-ENXIO);
88
89         if (libcfs_psdev_ops.p_close != NULL)
90                 rc = libcfs_psdev_ops.p_close(0, mdev_state[devid]);
91         else
92                 rc = -EPERM;
93         if (rc)
94                 return rc;
95         mdev_state[devid] = NULL;
96         return rc;
97 }
98
99 static int 
100 libcfs_ioctl (dev_t dev, u_long cmd, caddr_t arg, int flag, struct proc *p)
101
102         int rc = 0; 
103         struct cfs_psdev_file    pfile; 
104         int     devid; 
105         devid = minor(dev); 
106         
107         if (devid > 16) return (-ENXIO);
108
109         if (suser(p->p_ucred, &p->p_acflag)) 
110                 return (-EPERM); 
111         
112         pfile.off = 0;
113         pfile.private_data = mdev_state[devid];
114
115         if (libcfs_psdev_ops.p_ioctl != NULL) 
116                 rc = libcfs_psdev_ops.p_ioctl(&pfile, cmd, (void *)arg);
117         else 
118                 rc = -EPERM;
119         return rc;
120 }
121
122 static struct cdevsw libcfs_devsw =
123
124         libcfs_psdev_open,            /* open */ 
125         libcfs_psdev_close,           /* close */ 
126         NULL,                   /* read */ 
127         NULL,                   /* write */ 
128         libcfs_ioctl,           /* ioctl */ 
129         NULL,                   /* stop */ 
130         NULL,                   /* reset */ 
131         NULL,                   /* tty's */ 
132         NULL,                   /* select */ 
133         NULL,                   /* mmap */ 
134         NULL,                   /* strategy */ 
135         NULL,                   /* getc */ 
136         NULL,                   /* putc */ 
137         0                       /* type */ 
138 };
139
140 cfs_psdev_t libcfs_dev = { 
141         -1, 
142         NULL, 
143         "portals", 
144         &libcfs_devsw, 
145         NULL
146 };
147
148 void
149 kportal_daemonize (char *str)
150 {
151         printf("Daemonize request: %s.\n", str);
152         return;
153 }
154
155 void 
156 kportal_blockallsigs(void)
157 {
158         return;
159 }