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
10 #include <afsconfig.h>
11 #include <afs/param.h>
33 #include <cm_scache.h>
39 #include <pioctl_nt.h>
41 static char AFSConfigKeyName[] =
42 "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters";
44 #define FS_IOCTLREQUEST_MAXSIZE 8192
45 /* big structure for representing and storing an IOCTL request */
46 typedef struct fs_ioctlRequest {
47 char *mp; /* marshalling/unmarshalling ptr */
48 long nbytes; /* bytes received (when unmarshalling) */
49 char data[FS_IOCTLREQUEST_MAXSIZE]; /* data we're marshalling */
52 static int CMtoUNIXerror(int cm_code)
55 case CM_ERROR_TIMEDOUT: return ETIMEDOUT;
56 case CM_ERROR_NOACCESS: return EACCES;
57 case CM_ERROR_NOSUCHFILE: return ENOENT;
58 case CM_ERROR_INVAL: return EINVAL;
59 case CM_ERROR_BADFD: return EBADF;
60 case CM_ERROR_EXISTS: return EEXIST;
61 case CM_ERROR_CROSSDEVLINK: return EXDEV;
62 case CM_ERROR_NOTDIR: return ENOTDIR;
63 case CM_ERROR_ISDIR: return EISDIR;
64 case CM_ERROR_READONLY: return EROFS;
65 case CM_ERROR_WOULDBLOCK: return EWOULDBLOCK;
66 case CM_ERROR_NOSUCHCELL: return ESRCH; /* hack */
67 case CM_ERROR_NOSUCHVOLUME: return EPIPE; /* hack */
68 case CM_ERROR_NOMORETOKENS: return EDOM; /* hack */
69 case CM_ERROR_TOOMANYBUFS: return EFBIG; /* hack */
70 default: return ENOTTY;
74 static void InitFSRequest(fs_ioctlRequest_t *rp)
80 static long GetIoctlHandle(char *fileNamep, HANDLE *handlep)
92 drivep = strchr(fileNamep, ':');
93 if (drivep && (drivep - fileNamep) >= 1) {
94 tbuffer[0] = *(drivep-1);
96 strcpy(tbuffer+2, SMB_IOCTL_FILENAME);
98 strcpy(tbuffer, SMB_IOCTL_FILENAME);
101 /* No file name specified, use UNC name */
102 /* First look for gateway host in Registry */
103 code = RegOpenKeyEx(HKEY_LOCAL_MACHINE, AFSConfigKeyName,
104 0, KEY_QUERY_VALUE, &parmKey);
105 if (code != ERROR_SUCCESS)
107 dummyLen = sizeof(hostName);
108 code = RegQueryValueEx(parmKey, "Gateway", NULL, NULL,
109 hostName, &dummyLen);
110 RegCloseKey (parmKey);
111 if (code == ERROR_SUCCESS)
114 /* No gateway name in registry; use ourself */
115 #ifndef AFS_WIN95_ENV
116 gethostname(hostName, sizeof(hostName));
120 /* DJGPP version of gethostname gets the NetBIOS
121 name of the machine, so that is what we are using for
122 the AFS server name instead of the DNS name. */
123 hostsize = sizeof(hostName);
124 GetComputerName(hostName, &hostsize);
126 #endif /* AFS_WIN95_ENV */
129 ctemp = strchr(hostName, '.'); /* turn ntafs.* into ntafs */
130 if (ctemp) *ctemp = 0;
134 sprintf(tbuffer, "\\\\%s-AFS\\all%s",
135 hostName, SMB_IOCTL_FILENAME);
139 /* now open the file */
140 fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
141 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
142 OPEN_EXISTING, FILE_FLAG_WRITE_THROUGH, NULL);
144 if (fh == INVALID_HANDLE_VALUE)
147 /* return fh and success code */
152 static long Transceive(HANDLE handle, fs_ioctlRequest_t *reqp)
157 rcount = reqp->mp - reqp->data;
158 if (rcount <= 0) return EINVAL; /* not supposed to happen */
160 if (!WriteFile(handle, reqp->data, rcount, &ioCount, NULL)) {
161 /* failed to write */
162 return GetLastError();
165 if (!ReadFile(handle, reqp->data, sizeof(reqp->data), &ioCount, NULL)) {
167 return GetLastError();
170 reqp->nbytes = ioCount; /* set # of bytes available */
171 reqp->mp = reqp->data; /* restart marshalling */
177 static long MarshallLong(fs_ioctlRequest_t *reqp, long val)
179 memcpy(reqp->mp, &val, 4);
184 static long UnmarshallLong(fs_ioctlRequest_t *reqp, long *valp)
186 /* not enough data left */
187 if (reqp->nbytes < 4) { return -1; }
189 memcpy(valp, reqp->mp, 4);
195 /* includes marshalling NULL pointer as a null (0 length) string */
196 static long MarshallString(fs_ioctlRequest_t *reqp, char *stringp)
201 count = strlen(stringp) + 1; /* space required including null */
204 /* watch for buffer overflow */
205 if ((reqp->mp - reqp->data) + count > sizeof(reqp->data)) return -1;
207 if (stringp) memcpy(reqp->mp, stringp, count);
208 else *(reqp->mp) = 0;
213 /* take a path with a drive letter, possibly relative, and return a full path
214 * without the drive letter. This is the full path relative to the working
215 * dir for that drive letter. The input and output paths can be the same.
217 static long fs_GetFullPath(char *pathp, char *outPathp, long outSize)
227 if (pathp[0] != 0 && pathp[1] == ':') {
228 /* there's a drive letter there */
237 if (*firstp == '\\' || *firstp == '/') {
238 /* already an absolute pathname, just copy it back */
239 strcpy(outPathp, firstp);
243 GetCurrentDirectory(sizeof(origPath), origPath);
246 if (pathHasDrive && (*pathp & ~0x20) != (origPath[0] & ~0x20)) {
247 /* a drive has been specified and it isn't our current drive.
248 * to get path, switch to it first. Must case-fold drive letters
249 * for user convenience.
255 if (!SetCurrentDirectory(newPath)) {
256 code = GetLastError();
261 /* now get the absolute path to the current wdir in this drive */
262 GetCurrentDirectory(sizeof(tpath), tpath);
263 strcpy(outPathp, tpath+2); /* skip drive letter */
264 /* if there is a non-null name after the drive, append it */
266 strcat(outPathp, "\\");
267 strcat(outPathp, firstp);
270 /* finally, if necessary, switch back to our home drive letter */
272 SetCurrentDirectory(origPath);
278 long pioctl(char *pathp, long opcode, struct ViceIoctl *blobp, int follow)
280 fs_ioctlRequest_t preq;
286 code = GetIoctlHandle(pathp, &reqHandle);
295 /* init the request structure */
296 InitFSRequest(&preq);
298 /* marshall the opcode, the path name and the input parameters */
299 MarshallLong(&preq, opcode);
300 /* when marshalling the path, remove the drive letter, since we already
301 * used the drive letter to find the AFS daemon; we don't need it any more.
302 * Eventually we'll expand relative path names here, too, since again, only
303 * we understand those.
306 code = fs_GetFullPath(pathp, fullPath, sizeof(fullPath));
308 CloseHandle(reqHandle);
314 strcpy(fullPath, "");
317 MarshallString(&preq, fullPath);
318 if (blobp->in_size) {
319 memcpy(preq.mp, blobp->in, blobp->in_size);
320 preq.mp += blobp->in_size;
323 /* now make the call */
324 code = Transceive(reqHandle, &preq);
326 CloseHandle(reqHandle);
330 /* now unmarshall the return value */
331 UnmarshallLong(&preq, &temp);
333 CloseHandle(reqHandle);
334 errno = CMtoUNIXerror(temp);
338 /* otherwise, unmarshall the output parameters */
339 if (blobp->out_size) {
340 temp = blobp->out_size;
341 if (preq.nbytes < temp) temp = preq.nbytes;
342 memcpy(blobp->out, preq.mp, temp);
343 blobp->out_size = temp;
346 /* and return success */
347 CloseHandle(reqHandle);