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>
34 #include <cm_scache.h>
40 #include <pioctl_nt.h>
42 #include <lanahelper.h>
44 static char AFSConfigKeyName[] =
45 "SYSTEM\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters";
47 #define FS_IOCTLREQUEST_MAXSIZE 8192
48 /* big structure for representing and storing an IOCTL request */
49 typedef struct fs_ioctlRequest {
50 char *mp; /* marshalling/unmarshalling ptr */
51 long nbytes; /* bytes received (when unmarshalling) */
52 char data[FS_IOCTLREQUEST_MAXSIZE]; /* data we're marshalling */
56 CMtoUNIXerror(int cm_code)
59 case CM_ERROR_TIMEDOUT:
61 case CM_ERROR_NOACCESS:
63 case CM_ERROR_NOSUCHFILE:
71 case CM_ERROR_CROSSDEVLINK:
77 case CM_ERROR_READONLY:
79 case CM_ERROR_WOULDBLOCK:
81 case CM_ERROR_NOSUCHCELL:
82 return ESRCH; /* hack */
83 case CM_ERROR_NOSUCHVOLUME:
84 return EPIPE; /* hack */
85 case CM_ERROR_NOMORETOKENS:
86 return EDOM; /* hack */
87 case CM_ERROR_TOOMANYBUFS:
88 return EFBIG; /* hack */
95 InitFSRequest(fs_ioctlRequest_t * rp)
102 GetIoctlHandle(char *fileNamep, HANDLE * handlep)
105 char netbiosName[MAX_NB_NAME_LENGTH];
106 char tbuffer[256]="";
110 drivep = strchr(fileNamep, ':');
111 if (drivep && (drivep - fileNamep) >= 1) {
112 tbuffer[0] = *(drivep - 1);
114 strcpy(tbuffer + 2, SMB_IOCTL_FILENAME);
118 GetCurrentDirectory(sizeof(curdir), curdir);
119 if ( curdir[1] == ':' ) {
120 tbuffer[0] = curdir[0];
122 strcpy(tbuffer + 2, SMB_IOCTL_FILENAME);
127 /* No file name starting with drive colon specified, use UNC name */
128 lana_GetNetbiosName(netbiosName,LANA_NETBIOS_NAME_FULL);
129 sprintf(tbuffer,"\\\\%s\\all%s",netbiosName,SMB_IOCTL_FILENAME);
133 /* now open the file */
134 fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
135 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
136 FILE_FLAG_WRITE_THROUGH, NULL);
138 if (fh == INVALID_HANDLE_VALUE)
141 /* return fh and success code */
147 Transceive(HANDLE handle, fs_ioctlRequest_t * reqp)
152 rcount = reqp->mp - reqp->data;
154 return EINVAL; /* not supposed to happen */
156 if (!WriteFile(handle, reqp->data, rcount, &ioCount, NULL)) {
157 /* failed to write */
158 return GetLastError();
161 if (!ReadFile(handle, reqp->data, sizeof(reqp->data), &ioCount, NULL)) {
163 return GetLastError();
166 reqp->nbytes = ioCount; /* set # of bytes available */
167 reqp->mp = reqp->data; /* restart marshalling */
174 MarshallLong(fs_ioctlRequest_t * reqp, long val)
176 memcpy(reqp->mp, &val, 4);
182 UnmarshallLong(fs_ioctlRequest_t * reqp, long *valp)
184 /* not enough data left */
185 if (reqp->nbytes < 4) {
189 memcpy(valp, reqp->mp, 4);
195 /* includes marshalling NULL pointer as a null (0 length) string */
197 MarshallString(fs_ioctlRequest_t * reqp, char *stringp)
202 count = strlen(stringp) + 1; /* space required including null */
206 /* watch for buffer overflow */
207 if ((reqp->mp - reqp->data) + count > sizeof(reqp->data))
211 memcpy(reqp->mp, stringp, count);
218 /* take a path with a drive letter, possibly relative, and return a full path
219 * without the drive letter. This is the full path relative to the working
220 * dir for that drive letter. The input and output paths can be the same.
223 fs_GetFullPath(char *pathp, char *outPathp, long outSize)
233 if (pathp[0] != 0 && pathp[1] == ':') {
234 /* there's a drive letter there */
242 if (*firstp == '\\' || *firstp == '/') {
243 /* already an absolute pathname, just copy it back */
244 strcpy(outPathp, firstp);
248 GetCurrentDirectory(sizeof(origPath), origPath);
251 if (pathHasDrive && (*pathp & ~0x20) != (origPath[0] & ~0x20)) {
252 /* a drive has been specified and it isn't our current drive.
253 * to get path, switch to it first. Must case-fold drive letters
254 * for user convenience.
260 if (!SetCurrentDirectory(newPath)) {
261 code = GetLastError();
266 /* now get the absolute path to the current wdir in this drive */
267 GetCurrentDirectory(sizeof(tpath), tpath);
269 strcpy(outPathp, tpath + 2); /* skip drive letter */
271 strcpy(outPathp, tpath); /* copy entire UNC path */
272 /* if there is a non-null name after the drive, append it */
274 int len = strlen(outPathp);
275 if (outPathp[len-1] != '\\' && outPathp[len-1] != '/')
276 strcat(outPathp, "\\");
277 strcat(outPathp, firstp);
280 /* finally, if necessary, switch back to our home drive letter */
282 SetCurrentDirectory(origPath);
289 pioctl(char *pathp, long opcode, struct ViceIoctl *blobp, int follow)
291 fs_ioctlRequest_t preq;
297 code = GetIoctlHandle(pathp, &reqHandle);
306 /* init the request structure */
307 InitFSRequest(&preq);
309 /* marshall the opcode, the path name and the input parameters */
310 MarshallLong(&preq, opcode);
311 /* when marshalling the path, remove the drive letter, since we already
312 * used the drive letter to find the AFS daemon; we don't need it any more.
313 * Eventually we'll expand relative path names here, too, since again, only
314 * we understand those.
317 code = fs_GetFullPath(pathp, fullPath, sizeof(fullPath));
319 CloseHandle(reqHandle);
324 strcpy(fullPath, "");
327 MarshallString(&preq, fullPath);
328 if (blobp->in_size) {
329 memcpy(preq.mp, blobp->in, blobp->in_size);
330 preq.mp += blobp->in_size;
333 /* now make the call */
334 code = Transceive(reqHandle, &preq);
336 CloseHandle(reqHandle);
340 /* now unmarshall the return value */
341 UnmarshallLong(&preq, &temp);
343 CloseHandle(reqHandle);
344 errno = CMtoUNIXerror(temp);
348 /* otherwise, unmarshall the output parameters */
349 if (blobp->out_size) {
350 temp = blobp->out_size;
351 if (preq.nbytes < temp)
353 memcpy(blobp->out, preq.mp, temp);
354 blobp->out_size = temp;
357 /* and return success */
358 CloseHandle(reqHandle);