📄 mountlib.c
字号:
if(mountPoints == NULL || lstCount(mountPoints) == 0) { if(exportList != NULL) { KHEAP_FREE((char *)exportList); exportList = NULL; } return (&exportList); } if (exportList == NULL) { exportList = KHEAP_ALLOC((mountdNExports * (sizeof (exportnode) + nfsMaxPath))); if (exportList == NULL) { return (&exportList); } else { bzero ((char *)exportList,(mountdNExports * (sizeof (exportnode) + nfsMaxPath))); } } strTbl = (char *) &exportList[mountdNExports]; /* Bogus group information */ aGroup.gr_name = NULL; aGroup.gr_next = NULL; semTake (mountSem, WAIT_FOREVER); for (exportEntry = (NFS_EXPORT_ENTRY *) lstFirst (mountPoints); exportEntry != NULL; exportEntry = (NFS_EXPORT_ENTRY *) lstNext ((NODE *) exportEntry)) { nextEntry = (NFS_EXPORT_ENTRY *) lstNext ((NODE *) exportEntry); exportList [lstPos].ex_dir = strTbl; strncpy (strTbl, exportEntry->dirName, nfsMaxPath - 1); strTbl += strlen (strTbl); *strTbl++ = EOS; exportList [lstPos].ex_groups = NULL; if (nextEntry != NULL) exportList [lstPos].ex_next = &exportList [lstPos + 1]; else exportList [lstPos].ex_next = NULL; lstPos++; } semGive (mountSem); return ((exports *) &exportList); }/******************************************************************************** nfsExport - specify a file system to be NFS exported** This routine makes a file system available for mounting by a client.* The client should be in the local host table (see hostAdd()),* although this is not required.* * The <id> parameter can either be set to a specific value, or to 0.* If it is set to 0, an ID number is assigned sequentially.* Every time a file system is exported, it must have the same ID* number, or clients currently mounting the file system will not be* able to access files.** To display a list of exported file systems, use:* .CS* -> nfsExportShow "localhost"* .CE* * RETURNS: OK, or ERROR if the file system could not be exported.** SEE ALSO: nfsLib, nfsExportShow(), nfsUnexport()*/STATUS nfsExport ( char * directory, /* Directory to export - FS must support NFS */ int id, /* ID number for file system */ BOOL readOnly, /* TRUE if file system is exported read-only */ int options /* Reserved for future use - set to 0 */ ) { NFS_EXPORT_ENTRY * exportFs; /* The file system to be exported */ int exportDirFd; /* File descriptor for the exported dir */ struct stat statInfo; /* information from fstat() call */ if (strlen (directory) >= nfsMaxPath) return (ERROR); /* Create the export point */ if ((exportFs = KHEAP_ALLOC (sizeof (*exportFs) + nfsMaxPath)) == NULL) return (ERROR); if ((exportDirFd = open (directory, O_RDONLY, 0666)) == ERROR) { KHEAP_FREE ((char *)exportFs); return (ERROR); } /* Set up the NFS_EXPORT_ENTRY structure */ strncpy (exportFs->dirName, directory, nfsMaxPath - 1); exportFs->dirFd = exportDirFd; /* Set the ID number for the exported system */ if (id == 0) exportFs->volumeId = fsIdNumber++; else exportFs->volumeId = id; exportFs->readOnly = readOnly; /* Add it to the list */ semTake (mountSem, WAIT_FOREVER); lstAdd (mountPoints, (NODE *)exportFs); semGive (mountSem); if (fstat (exportDirFd, &statInfo) != OK) { semTake (mountSem, WAIT_FOREVER); lstDelete (mountPoints, (NODE *)exportFs); semGive (mountSem); KHEAP_FREE ((char *) exportFs); close (exportDirFd); return (ERROR); } else { nfsHashTblSet (exportFs->dirName, exportFs->volumeId, statInfo.st_dev); } if ((exportFs->fsId = nameToInode (exportFs->volumeId, directory)) == ERROR) { semTake (mountSem, WAIT_FOREVER); lstDelete (mountPoints, (NODE *) exportFs); semGive (mountSem); nfsHashTblUnset (exportFs->dirName, exportFs->volumeId); KHEAP_FREE ((char *)exportFs); close (exportDirFd); return (ERROR); } return (OK); }/******************************************************************************** nfsUnexport - remove a file system from the list of exported file systems** This routine removes a file system from the list of file systems* exported from the target. Any client attempting to mount a file* system that is not exported will receive an error (NFSERR_ACCESS).* * RETURNS: OK, or ERROR if the file system could not be removed from* the exports list.* * ERRNO: ENOENT** SEE ALSO: nfsLib, nfsExportShow(), nfsExport()*/STATUS nfsUnexport ( char * dirName /* Name of the directory to unexport */ ) { NFS_EXPORT_ENTRY * mntPoint; /* Mount point from mountPoints list */ /* Make sure the directory name is mentioned */ if ((dirName == NULL) || (strlen (dirName) >= nfsMaxPath)) return(ERROR); /* Make sure there's a list of file systems */ if (mountPoints == NULL) return (ERROR); /* Find it and take it off the list */ if ((mntPoint = nfsExportFindByName (dirName)) != NULL) { semTake (mountSem, WAIT_FOREVER); lstDelete (mountPoints, (NODE *) mntPoint); nfsHashTblUnset (mntPoint->dirName, mntPoint->volumeId); semGive (mountSem); close(mntPoint->dirFd); KHEAP_FREE ((char *)mntPoint); return (OK); } else { /* Didn't find it, set errno and return ERROR */ semGive (mountSem); errno = ENOENT; return (ERROR); } }/******************************************************************************** nfsExportFindByName - find a pointer to an exported file system** Given a directory name, return a pointer to the corresponding* NFS_EXPORT_ENTRY structure.* * RETURNS: OK, or NULL if the file system could not be found.* * NOMANUAL */NFS_EXPORT_ENTRY * nfsExportFindByName ( char * dirName /* name of file system to find */ ) { NFS_EXPORT_ENTRY * mntPoint = NULL; /* mount point from mountPoints list */ char * matchName; /* fs name to find */ /* Make sure the list exists */ if (mountPoints == NULL) return (NULL); if ((matchName = (char *) alloca (nfsMaxPath)) == NULL) return (NULL); /* Make sure matchName doesn't have ending slash */ strcpy (matchName, dirName); if (matchName[strlen (matchName) - 1] == '/') matchName[strlen (matchName) - 1] = EOS; /* Search through the list */ semTake (mountSem, WAIT_FOREVER); for (mntPoint = (NFS_EXPORT_ENTRY *) lstFirst (mountPoints); mntPoint != NULL; mntPoint = (NFS_EXPORT_ENTRY *) lstNext ((NODE *) mntPoint)) { if (strcmp(mntPoint->dirName, matchName) == 0) { /* Found it, give the semaphore and return */ semGive (mountSem); return (mntPoint); } } /* Didn't find a match, return NULL */ semGive (mountSem); return (NULL); }/******************************************************************************** nfsExportFindById - find a pointer to an exported file system* * Given a directory ID number, return a pointer to the corresponding* NFS_EXPORT_ENTRY structure.* * RETURNS: OK, or NULL if the file system could not be found.* * NOMANUAL*/NFS_EXPORT_ENTRY * nfsExportFindById ( int volumeId /* volume ID number */ ) { NFS_EXPORT_ENTRY * mntPoint; /* mount point from mountPoints list */ /* Make sure the list exists */ if (mountPoints == NULL) return (NULL); /* Search through the list */ semTake (mountSem, WAIT_FOREVER); for (mntPoint = (NFS_EXPORT_ENTRY *) lstFirst (mountPoints); mntPoint != NULL; mntPoint = (NFS_EXPORT_ENTRY *) lstNext ((NODE *) mntPoint)) { if (volumeId == mntPoint->volumeId) { /* Found it, give the semaphore and return */ semGive (mountSem); return (mntPoint); } } /* Didn't find a match, return NULL */ semGive (mountSem); return (NULL); }/******************************************************************************** nameToInode - file name to inode number* * Given a file name, return the inode number associated with it.* * RETURNS: the inode number for the file descriptor, or ERROR.** NOMANUAL*/int nameToInode ( int mntId, /* mount id */ char * fileName /* name of the file */ ) { int fd; /* File descriptor */ int inode; /* inode of file */ if ((fd = open (fileName, O_RDONLY, 0)) == ERROR) return (ERROR); else { inode = nfsNmLkupIns (mntId, fileName); } close (fd); return (inode); }/******************************************************************************** svc_reqToHostName - convert svc_req struct to host name* * Given a pointer to a svc_req structure, fill in a string with the* host name of the client associated with that svc_req structure.* The calling routine is responsible for allocating space for the* name string, which should be NAME_MAX characters long.** RETURNS: A pointer to the name of the client.* * NOMANUAL*/LOCAL char * svc_reqToHostName ( struct svc_req * rqstp, /* Client information */ char * hostName /* String to fill in with client name */ ) { /* Convert the callers address to a host name. If hostGetByAddr() * returns ERROR, convert the address to a numeric string */ if (hostGetByAddr (rqstp->rq_xprt->xp_raddr.sin_addr.s_addr, hostName)!= OK) inet_ntoa_b (rqstp->rq_xprt->xp_raddr.sin_addr, hostName); return (hostName); }/******************************************************************************** mountListFind - find an entry in the NFS mount list** Given a pointer to a svc_req structure and a path name, search* the client mount list to see if the client already has this* particular path mounted.** RETURNS: A pointer to the mount list entry which we found,* or NULL if there was no match.* * NOMANUAL*/LOCAL MOUNT_CLIENT * mountListFind ( struct svc_req * rqstp, /* Client information */ dirpath * path /* path to search for */ ) { MOUNT_CLIENT * clientEntry; char clientName [MAXHOSTNAMELEN + 1]; semTake (clientsSem, WAIT_FOREVER); for (clientEntry = (MOUNT_CLIENT *) lstFirst (nfsClients); clientEntry != NULL; clientEntry = (MOUNT_CLIENT *) lstNext ((NODE *) clientEntry)) { svc_reqToHostName (rqstp, clientName); if ((strncmp(clientName, clientEntry->clientName, MAXHOSTNAMELEN) == 0) && strncmp (*path, clientEntry->directory, PATH_MAX) == 0) { semGive (clientsSem); return (clientEntry); } } semGive (clientsSem); return(NULL); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -