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);
115 } else if (fileNamep[0] == fileNamep[1] &&
116 fileNamep[0] == '\\')
118 int count = 0, i = 0;
120 while (count < 4 && fileNamep[i]) {
121 tbuffer[i] = fileNamep[i];
122 if ( tbuffer[i++] == '\\' )
128 strcat(tbuffer, SMB_IOCTL_FILENAME);
132 GetCurrentDirectory(sizeof(curdir), curdir);
133 if ( curdir[1] == ':' ) {
134 tbuffer[0] = curdir[0];
136 strcpy(tbuffer + 2, SMB_IOCTL_FILENAME);
137 } else if (curdir[0] == curdir[1] &&
140 int count = 0, i = 0;
142 while (count < 4 && curdir[i]) {
143 tbuffer[i] = curdir[i];
144 if ( tbuffer[i++] == '\\' )
150 strcat(tbuffer, SMB_IOCTL_FILENAME);
155 /* No file name starting with drive colon specified, use UNC name */
156 lana_GetNetbiosName(netbiosName,LANA_NETBIOS_NAME_FULL);
157 sprintf(tbuffer,"\\\\%s\\all%s",netbiosName,SMB_IOCTL_FILENAME);
161 /* now open the file */
162 fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
163 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
164 FILE_FLAG_WRITE_THROUGH, NULL);
166 if (fh == INVALID_HANDLE_VALUE)
169 /* return fh and success code */
175 Transceive(HANDLE handle, fs_ioctlRequest_t * reqp)
180 rcount = reqp->mp - reqp->data;
182 return EINVAL; /* not supposed to happen */
184 if (!WriteFile(handle, reqp->data, rcount, &ioCount, NULL)) {
185 /* failed to write */
186 return GetLastError();
189 if (!ReadFile(handle, reqp->data, sizeof(reqp->data), &ioCount, NULL)) {
191 return GetLastError();
194 reqp->nbytes = ioCount; /* set # of bytes available */
195 reqp->mp = reqp->data; /* restart marshalling */
202 MarshallLong(fs_ioctlRequest_t * reqp, long val)
204 memcpy(reqp->mp, &val, 4);
210 UnmarshallLong(fs_ioctlRequest_t * reqp, long *valp)
212 /* not enough data left */
213 if (reqp->nbytes < 4) {
217 memcpy(valp, reqp->mp, 4);
223 /* includes marshalling NULL pointer as a null (0 length) string */
225 MarshallString(fs_ioctlRequest_t * reqp, char *stringp)
230 count = strlen(stringp) + 1; /* space required including null */
234 /* watch for buffer overflow */
235 if ((reqp->mp - reqp->data) + count > sizeof(reqp->data))
239 memcpy(reqp->mp, stringp, count);
246 /* take a path with a drive letter, possibly relative, and return a full path
247 * without the drive letter. This is the full path relative to the working
248 * dir for that drive letter. The input and output paths can be the same.
251 fs_GetFullPath(char *pathp, char *outPathp, long outSize)
261 if (pathp[0] != 0 && pathp[1] == ':') {
262 /* there's a drive letter there */
270 if (*firstp == '\\' || *firstp == '/') {
271 /* already an absolute pathname, just copy it back */
272 strcpy(outPathp, firstp);
276 GetCurrentDirectory(sizeof(origPath), origPath);
279 if (pathHasDrive && (*pathp & ~0x20) != (origPath[0] & ~0x20)) {
280 /* a drive has been specified and it isn't our current drive.
281 * to get path, switch to it first. Must case-fold drive letters
282 * for user convenience.
288 if (!SetCurrentDirectory(newPath)) {
289 code = GetLastError();
294 /* now get the absolute path to the current wdir in this drive */
295 GetCurrentDirectory(sizeof(tpath), tpath);
297 strcpy(outPathp, tpath + 2); /* skip drive letter */
299 strcpy(outPathp, tpath); /* copy entire UNC path */
300 /* if there is a non-null name after the drive, append it */
302 int len = strlen(outPathp);
303 if (outPathp[len-1] != '\\' && outPathp[len-1] != '/')
304 strcat(outPathp, "\\");
305 strcat(outPathp, firstp);
308 /* finally, if necessary, switch back to our home drive letter */
310 SetCurrentDirectory(origPath);
317 pioctl(char *pathp, long opcode, struct ViceIoctl *blobp, int follow)
319 fs_ioctlRequest_t preq;
325 code = GetIoctlHandle(pathp, &reqHandle);
334 /* init the request structure */
335 InitFSRequest(&preq);
337 /* marshall the opcode, the path name and the input parameters */
338 MarshallLong(&preq, opcode);
339 /* when marshalling the path, remove the drive letter, since we already
340 * used the drive letter to find the AFS daemon; we don't need it any more.
341 * Eventually we'll expand relative path names here, too, since again, only
342 * we understand those.
345 code = fs_GetFullPath(pathp, fullPath, sizeof(fullPath));
347 CloseHandle(reqHandle);
352 strcpy(fullPath, "");
355 MarshallString(&preq, fullPath);
356 if (blobp->in_size) {
357 memcpy(preq.mp, blobp->in, blobp->in_size);
358 preq.mp += blobp->in_size;
361 /* now make the call */
362 code = Transceive(reqHandle, &preq);
364 CloseHandle(reqHandle);
368 /* now unmarshall the return value */
369 UnmarshallLong(&preq, &temp);
371 CloseHandle(reqHandle);
372 errno = CMtoUNIXerror(temp);
376 /* otherwise, unmarshall the output parameters */
377 if (blobp->out_size) {
378 temp = blobp->out_size;
379 if (preq.nbytes < temp)
381 memcpy(blobp->out, preq.mp, temp);
382 blobp->out_size = temp;
385 /* and return success */
386 CloseHandle(reqHandle);