2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
27 #include <cm_scache.h>
34 #include <pioctl_nt.h>
36 static char AFSConfigKeyName[] =
37 "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters";
39 #define FS_IOCTLREQUEST_MAXSIZE 8192
40 /* big structure for representing and storing an IOCTL request */
41 typedef struct fs_ioctlRequest {
42 char *mp; /* marshalling/unmarshalling ptr */
43 long nbytes; /* bytes received (when unmarshalling) */
44 char data[FS_IOCTLREQUEST_MAXSIZE]; /* data we're marshalling */
47 static int CMtoUNIXerror(int cm_code)
50 case CM_ERROR_TIMEDOUT: return ETIMEDOUT;
51 case CM_ERROR_NOACCESS: return EACCES;
52 case CM_ERROR_NOSUCHFILE: return ENOENT;
53 case CM_ERROR_INVAL: return EINVAL;
54 case CM_ERROR_BADFD: return EBADF;
55 case CM_ERROR_EXISTS: return EEXIST;
56 case CM_ERROR_CROSSDEVLINK: return EXDEV;
57 case CM_ERROR_NOTDIR: return ENOTDIR;
58 case CM_ERROR_ISDIR: return EISDIR;
59 case CM_ERROR_READONLY: return EROFS;
60 case CM_ERROR_WOULDBLOCK: return EWOULDBLOCK;
61 case CM_ERROR_NOSUCHCELL: return ESRCH; /* hack */
62 case CM_ERROR_NOSUCHVOLUME: return EPIPE; /* hack */
63 case CM_ERROR_NOMORETOKENS: return EDOM; /* hack */
64 case CM_ERROR_TOOMANYBUFS: return EFBIG; /* hack */
65 default: return ENOTTY;
69 static void InitFSRequest(fs_ioctlRequest_t *rp)
75 static long GetIoctlHandle(char *fileNamep, HANDLE *handlep)
87 drivep = strchr(fileNamep, ':');
88 if (drivep && (drivep - fileNamep) >= 1) {
89 tbuffer[0] = *(drivep-1);
91 strcpy(tbuffer+2, SMB_IOCTL_FILENAME);
93 strcpy(tbuffer, SMB_IOCTL_FILENAME);
96 /* No file name specified, use UNC name */
97 /* First look for gateway host in Registry */
98 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
99 0, KEY_QUERY_VALUE, &parmKey);
100 if (code != ERROR_SUCCESS)
102 dummyLen = sizeof(hostName);
103 code = RegQueryValueEx(parmKey, "Gateway", NULL, NULL,
104 hostName, &dummyLen);
105 RegCloseKey (parmKey);
106 if (code == ERROR_SUCCESS)
109 /* No gateway name in registry; use ourself */
110 gethostname(hostName, sizeof(hostName));
112 ctemp = strchr(hostName, '.'); /* turn ntafs.* into ntafs */
113 if (ctemp) *ctemp = 0;
117 sprintf(tbuffer, "\\\\%s-AFS\\all%s",
118 hostName, SMB_IOCTL_FILENAME);
121 /* now open the file */
122 fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
123 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
124 OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL);
125 if (fh == INVALID_HANDLE_VALUE)
128 /* return fh and success code */
133 static long Transceive(HANDLE handle, fs_ioctlRequest_t *reqp)
138 rcount = reqp->mp - reqp->data;
139 if (rcount <= 0) return EINVAL; /* not supposed to happen */
141 if (!WriteFile(handle, reqp->data, rcount, &ioCount, NULL)) {
142 /* failed to write */
143 return GetLastError();
146 if (!ReadFile(handle, reqp->data, sizeof(reqp->data), &ioCount, NULL)) {
148 return GetLastError();
151 reqp->nbytes = ioCount; /* set # of bytes available */
152 reqp->mp = reqp->data; /* restart marshalling */
158 static long MarshallLong(fs_ioctlRequest_t *reqp, long val)
160 memcpy(reqp->mp, &val, 4);
165 static long UnmarshallLong(fs_ioctlRequest_t *reqp, long *valp)
167 /* not enough data left */
168 if (reqp->nbytes < 4) return -1;
170 memcpy(valp, reqp->mp, 4);
176 /* includes marshalling NULL pointer as a null (0 length) string */
177 static long MarshallString(fs_ioctlRequest_t *reqp, char *stringp)
182 count = strlen(stringp) + 1; /* space required including null */
185 /* watch for buffer overflow */
186 if ((reqp->mp - reqp->data) + count > sizeof(reqp->data)) return -1;
188 if (stringp) memcpy(reqp->mp, stringp, count);
189 else *(reqp->mp) = 0;
194 /* take a path with a drive letter, possibly relative, and return a full path
195 * without the drive letter. This is the full path relative to the working
196 * dir for that drive letter. The input and output paths can be the same.
198 static long fs_GetFullPath(char *pathp, char *outPathp, long outSize)
208 if (pathp[0] != 0 && pathp[1] == ':') {
209 /* there's a drive letter there */
218 if (*firstp == '\\' || *firstp == '/') {
219 /* already an absolute pathname, just copy it back */
220 strcpy(outPathp, firstp);
224 GetCurrentDirectory(sizeof(origPath), origPath);
227 if (pathHasDrive && (*pathp & ~0x20) != (origPath[0] & ~0x20)) {
228 /* a drive has been specified and it isn't our current drive.
229 * to get path, switch to it first. Must case-fold drive letters
230 * for user convenience.
236 if (!SetCurrentDirectory(newPath)) {
237 code = GetLastError();
242 /* now get the absolute path to the current wdir in this drive */
243 GetCurrentDirectory(sizeof(tpath), tpath);
244 strcpy(outPathp, tpath+2); /* skip drive letter */
245 /* if there is a non-null name after the drive, append it */
247 strcat(outPathp, "\\");
248 strcat(outPathp, firstp);
251 /* finally, if necessary, switch back to our home drive letter */
253 SetCurrentDirectory(origPath);
259 long pioctl(char *pathp, long opcode, struct ViceIoctl *blobp, int follow)
261 fs_ioctlRequest_t preq;
267 code = GetIoctlHandle(pathp, &reqHandle);
276 /* init the request structure */
277 InitFSRequest(&preq);
279 /* marshall the opcode, the path name and the input parameters */
280 MarshallLong(&preq, opcode);
281 /* when marshalling the path, remove the drive letter, since we already
282 * used the drive letter to find the AFS daemon; we don't need it any more.
283 * Eventually we'll expand relative path names here, too, since again, only
284 * we understand those.
287 code = fs_GetFullPath(pathp, fullPath, sizeof(fullPath));
289 CloseHandle(reqHandle);
295 strcpy(fullPath, "");
298 MarshallString(&preq, fullPath);
299 if (blobp->in_size) {
300 memcpy(preq.mp, blobp->in, blobp->in_size);
301 preq.mp += blobp->in_size;
304 /* now make the call */
305 code = Transceive(reqHandle, &preq);
307 CloseHandle(reqHandle);
311 /* now unmarshall the return value */
312 UnmarshallLong(&preq, &temp);
314 CloseHandle(reqHandle);
315 errno = CMtoUNIXerror(temp);
319 /* otherwise, unmarshall the output parameters */
320 if (blobp->out_size) {
321 temp = blobp->out_size;
322 if (preq.nbytes < temp) temp = preq.nbytes;
323 memcpy(blobp->out, preq.mp, temp);
324 blobp->out_size = temp;
327 /* and return success */
328 CloseHandle(reqHandle);