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[100]="";
110 drivep = strchr(fileNamep, ':');
111 if (drivep && (drivep - fileNamep) >= 1) {
112 tbuffer[0] = *(drivep - 1);
114 strcpy(tbuffer + 2, SMB_IOCTL_FILENAME);
118 /* No file name starting with drive colon specified, use UNC name */
119 lana_GetNetbiosName(netbiosName,LANA_NETBIOS_NAME_FULL);
120 sprintf(tbuffer,"\\\\%s\\all%s",netbiosName,SMB_IOCTL_FILENAME);
124 /* now open the file */
125 fh = CreateFile(tbuffer, GENERIC_READ | GENERIC_WRITE,
126 FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,
127 FILE_FLAG_WRITE_THROUGH, NULL);
129 if (fh == INVALID_HANDLE_VALUE)
132 /* return fh and success code */
138 Transceive(HANDLE handle, fs_ioctlRequest_t * reqp)
143 rcount = reqp->mp - reqp->data;
145 return EINVAL; /* not supposed to happen */
147 if (!WriteFile(handle, reqp->data, rcount, &ioCount, NULL)) {
148 /* failed to write */
149 return GetLastError();
152 if (!ReadFile(handle, reqp->data, sizeof(reqp->data), &ioCount, NULL)) {
154 return GetLastError();
157 reqp->nbytes = ioCount; /* set # of bytes available */
158 reqp->mp = reqp->data; /* restart marshalling */
165 MarshallLong(fs_ioctlRequest_t * reqp, long val)
167 memcpy(reqp->mp, &val, 4);
173 UnmarshallLong(fs_ioctlRequest_t * reqp, long *valp)
175 /* not enough data left */
176 if (reqp->nbytes < 4) {
180 memcpy(valp, reqp->mp, 4);
186 /* includes marshalling NULL pointer as a null (0 length) string */
188 MarshallString(fs_ioctlRequest_t * reqp, char *stringp)
193 count = strlen(stringp) + 1; /* space required including null */
197 /* watch for buffer overflow */
198 if ((reqp->mp - reqp->data) + count > sizeof(reqp->data))
202 memcpy(reqp->mp, stringp, count);
209 /* take a path with a drive letter, possibly relative, and return a full path
210 * without the drive letter. This is the full path relative to the working
211 * dir for that drive letter. The input and output paths can be the same.
214 fs_GetFullPath(char *pathp, char *outPathp, long outSize)
224 if (pathp[0] != 0 && pathp[1] == ':') {
225 /* there's a drive letter there */
233 if (*firstp == '\\' || *firstp == '/') {
234 /* already an absolute pathname, just copy it back */
235 strcpy(outPathp, firstp);
239 GetCurrentDirectory(sizeof(origPath), origPath);
242 if (pathHasDrive && (*pathp & ~0x20) != (origPath[0] & ~0x20)) {
243 /* a drive has been specified and it isn't our current drive.
244 * to get path, switch to it first. Must case-fold drive letters
245 * for user convenience.
251 if (!SetCurrentDirectory(newPath)) {
252 code = GetLastError();
257 /* now get the absolute path to the current wdir in this drive */
258 GetCurrentDirectory(sizeof(tpath), tpath);
260 strcpy(outPathp, tpath + 2); /* skip drive letter */
262 strcpy(outPathp, tpath); /* copy entire UNC path */
263 /* if there is a non-null name after the drive, append it */
265 int len = strlen(outPathp);
266 if (outPathp[len-1] != '\\' && outPathp[len-1] != '/')
267 strcat(outPathp, "\\");
268 strcat(outPathp, firstp);
271 /* finally, if necessary, switch back to our home drive letter */
273 SetCurrentDirectory(origPath);
280 pioctl(char *pathp, long opcode, struct ViceIoctl *blobp, int follow)
282 fs_ioctlRequest_t preq;
288 code = GetIoctlHandle(pathp, &reqHandle);
297 /* init the request structure */
298 InitFSRequest(&preq);
300 /* marshall the opcode, the path name and the input parameters */
301 MarshallLong(&preq, opcode);
302 /* when marshalling the path, remove the drive letter, since we already
303 * used the drive letter to find the AFS daemon; we don't need it any more.
304 * Eventually we'll expand relative path names here, too, since again, only
305 * we understand those.
308 code = fs_GetFullPath(pathp, fullPath, sizeof(fullPath));
310 CloseHandle(reqHandle);
315 strcpy(fullPath, "");
318 MarshallString(&preq, fullPath);
319 if (blobp->in_size) {
320 memcpy(preq.mp, blobp->in, blobp->in_size);
321 preq.mp += blobp->in_size;
324 /* now make the call */
325 code = Transceive(reqHandle, &preq);
327 CloseHandle(reqHandle);
331 /* now unmarshall the return value */
332 UnmarshallLong(&preq, &temp);
334 CloseHandle(reqHandle);
335 errno = CMtoUNIXerror(temp);
339 /* otherwise, unmarshall the output parameters */
340 if (blobp->out_size) {
341 temp = blobp->out_size;
342 if (preq.nbytes < temp)
344 memcpy(blobp->out, preq.mp, temp);
345 blobp->out_size = temp;
348 /* and return success */
349 CloseHandle(reqHandle);