📄 nfsdlib.c
字号:
/* nfsdLib.c - Network File System (NFS) server library *//* Copyright 1994 - 2001 Wind River Systems, Inc. */#include "copyright_wrs.h"/*modification history--------------------01s,07may02,kbw man page edits01r,06nov01,vvv made max path length configurable (SPR #63551)01q,15oct01,rae merge from truestack ver 01r, base 01o (cleanup)01p,21jun00,rsh upgrade to dosFs 2.001o,05apr00,zl fixed the fix in 01m (use retVal)01m,15nov98,rjc modifications for dosfs2 compatibility.01o,03dec00,ijm increased nfsdStackSize (SPR# 22650). Corrected return value if file exists (SPR# 31536)01n,16mar99,spm recovered orphaned code from tor1_0_1.sens1_1 (SPR #25770)01m,06oct98,sgv fixed nfsproc_create_2 to return NFSERR_EXIST when the file exists01m,14mar99,jdi doc: removed refs to config.h and/or configAll.h (SPR 25663).01l,11mar97,dvs added test for udp in nfsd (SPR #8183).01k,10feb97,dbt close file descriptors before leaving in nfsproc_setattr_2 () routine before leaving (SPR #6615).01j,05jul96,ms fixed SPR 6579 - only NFSPROC_CREATE if file doen't exist.01i,27feb95,jdi doc: changed dosFsMode to dosFsFileMode (doc only) (SPR 4085).01h,11feb95,jdi doc format repair.01g,24jan95,jdi doc cleanup.01f,08sep94,jmm final changes01e,11may94,jmm integrated Roland's doc changes01d,25apr94,jmm added nfsdFsReadOnly(), all routines that write now call it 1st added module documentation01c,21apr94,jmm reordered routines, documentation cleanup01b,20apr94,jmm formatting cleanup, nfsdFhNtoh and Hton calls fixed01a,07mar94,jmm written*//*DESCRIPTIONThis library is an implementation of version 2 of the Network FileSystem Protocol Specification as defined in RFC 1094. It isclosely connected with version 1 of the mount protocol, also definedin RFC 1094 and implemented in turn by mountLib.The NFS server is initialized by calling nfsdInit(). This is done automatically at boot time if INCLUDE_NFS_SERVER is defined.Currently, only the dosFsLib file system is supported.File systems are exported with the nfsExport() call.To create and export a file system, define INCLUDE_NFS_SERVER and rebuildVxWorks.To export VxWorks file systems via NFS, you need facilities from boththis library and from mountLib. To include both, defineINCLUDE_NFS_SERVER and rebuild VxWorks.Use the mountLib routine nfsExport() to export file systems. For anexample, see the manual page for mountLib.VxWorks does not normally provide authentication services for NFSrequests, and the DOS file system does not provide file permissions.If you need to authenticate incoming requests, see the documentationfor nfsdInit() and mountdInit() for information about authorizationhooks.The following requests are accepted from clients. For details oftheir use, see RFC 1094, "NFS: Network File System ProtocolSpecification.".TScenter,tab(|);lf3 lf3l n.Procedure Name | Procedure Number_ NFSPROC_NULL | 0 NFSPROC_GETATTR | 1 NFSPROC_SETATTR | 2 NFSPROC_ROOT | 3 NFSPROC_LOOKUP | 4 NFSPROC_READLINK | 5 NFSPROC_READ | 6 NFSPROC_WRITE | 8 NFSPROC_CREATE | 9 NFSPROC_REMOVE | 10 NFSPROC_RENAME | 11 NFSPROC_LINK | 12 NFSPROC_SYMLINK | 13 NFSPROC_MKDIR | 14 NFSPROC_RMDIR | 15 NFSPROC_READDIR | 16 NFSPROC_STATFS | 17.TEAUTHENTICATION AND PERMISSIONSCurrently, no authentication is done on NFS requests. nfsdInit()describes the authentication hooks that can be added shouldauthentication be necessary.Note that the DOS file system does not provide information about ownershipor permissions on individual files. Before initializing a dosFs filesystem, three global variables--`dosFsUserId', `dosFsGroupId', and`dosFsFileMode'--can be set to define the user ID, group ID, and permissionsbyte for all files in all dosFs volumes initialized after setting thesevariables. To arrange for different dosFs volumes to use different userand group ID numbers, reset these variables before each volume isinitialized. See the manual entry for dosFsLib for more information.TASKSSeveral NFS tasks are created by nfsdInit(). They are:.iP tMountd 11 3The mount daemon, which handles all incoming mount requests.This daemon is created by mountdInit(), which is automaticallycalled from nfsdInit()..iP tNfsdThe NFS daemon, which queues all incoming NFS requests..iP tNfsdXThe NFS request handlers, which dequeues and processes all incomingNFS requests..LPPerformance of the NFS file system can be improved by increasing thenumber of servers specified in the nfsdInit() call, if there areseveral different dosFs volumes exported from the same target system.The spy() utility can be called to determine whether this is useful fora particular configuration.INTERNAL:All of the nfsproc_*_2 routines follow the RPC naming conventionrather than the WRS naming convention. They return a pointer tomalloced space, which is freed by nfsdRequestProcess().*/#include "vxWorks.h"#include "dirent.h"#include "dosFsLib.h"#include "errno.h"#include "fcntl.h"#include "ioLib.h"#include "limits.h"#include "mountLib.h"#include "msgQLib.h"#include "netinet/in.h"#include "nfsdLib.h"#include "pathLib.h"#include "rpcLib.h"#include "rpc/pmap_clnt.h"#include "rpc/rpc.h"#include "semLib.h"#include "sockLib.h"#include "stdio.h"#include "stdio.h"#include "stdlib.h"#include "string.h"#include "sys/socket.h"#include "sys/stat.h"#include "sys/types.h"#include "taskLib.h"#include "tickLib.h"#include "utime.h"#include "xdr_nfs.h"#include "iosLib.h"#include "private/nfsHashP.h"#include "memPartLib.h"/* defines */#define NAME_LEN 40#if !defined(S_dosFsLib_FILE_EXISTS)#define S_dosFsLib_FILE_EXISTS (S_dosFsLib_FILE_ALREADY_EXISTS)#endif#define QLEN_PER_SRVR 10#ifdef __GNUC__# ifndef alloca# define alloca __builtin_alloca# endif#endif/* DATA STRUCTURES *//* svcudp_data is copied directly from svc_udp.c - DO NOT MODIFY */struct svcudp_data { u_int su_iosz; /* byte size of send.recv buffer */ u_long su_xid; /* transaction id */ XDR su_xdrs; /* XDR handle */ char su_verfbody[MAX_AUTH_BYTES]; /* verifier body */ char *su_cache; /* cached data, NULL if no cache - 4.0 */};typedef struct { FUNCPTR routine; /* nfsproc_*_2 routine to call */ int procNum; /* NFS procedure number */ NFSD_ARGUMENT * argument; /* argument to pass to nfsproc_*_2 */ FUNCPTR xdrArg; /* XDR function pointer to convert argument */ FUNCPTR xdrResult; /* XDR function pointer to convert result */ struct sockaddr_in sockAddr; /* Address of the client socket */ int xid; /* RPC XID of client request */ int socket; /* Socket to use to send reply */ } NFS_Q_REQUEST;/* IMPORTS */IMPORT int nfsMaxPath; /* Max. file path length *//* GLOBALS */int nfsdStackSize = 14000; /* Default stack size for NFS processes */int nfsdNServers = 4; /* Default number of NFS servers */int nfsdPriorityDefault = 55; /* Default priority of the NFS server */int nfsdNFilesystemsDefault = 10; /* Default max. num. filesystems *//* LOCALS */LOCAL NFS_SERVER_STATUS nfsdServerStatus; /* Status of the NFS server */LOCAL FUNCPTR nfsdAuthHook = NULL; /* Authentication hook */MSG_Q_ID nfsRequestQ; /* Message Q for NFS requests *//* forward LOCAL functions */LOCAL void nfsdRequestEnqueue (struct svc_req * rqstp, SVCXPRT * transp);LOCAL int nfsdFsReadOnly (NFS_FILE_HANDLE * fh);/******************************************************************************** nfsdInit - initialize the NFS server* * This routine initializes the NFS server. <nServers> specifies the number of* tasks to be spawned to handle NFS requests. <priority> is the priority that* those tasks will run at. <authHook> is a pointer to an authorization* routine. <mountAuthHook> is a pointer to a similar routine, passed to* mountdInit(). <options> is provided for future expansion.* * Normally, no authorization is performed by either mountd or nfsd.* If you want to add authorization, set <authHook> to a* function pointer to a routine declared as follows:* .CS* nfsstat routine* (* int progNum, /@ RPC program number @/* int versNum, /@ RPC program version number @/* int procNum, /@ RPC procedure number @/* struct sockaddr_in clientAddr, /@ address of the client @/* NFSD_ARGUMENT * nfsdArg /@ argument of the call @/* )* .CE* * The <authHook> routine should return NFS_OK if the request is authorized,* and NFSERR_ACCES if not. (NFSERR_ACCES is not required; any legitimate* NFS error code can be returned.)* * See mountdInit() for documentation on <mountAuthHook>. Note that* <mountAuthHook> and <authHook> can point to the same routine.* Simply use the <progNum>, <versNum>, and <procNum> fields to decide* whether the request is an NFS request or a mountd request.* * VXWORKS AE PROTECTION DOMAINS* Under VxWorks AE, you can call this function from within the kernel * protection domain only. In addition, all arguments to this function can * reference only that data which is valid in the kernel protection domain. * This restriction does not apply under non-AE versions of VxWorks. ** RETURNS: OK, or ERROR if the NFS server cannot be started.** SEE ALSO: nfsExport(), mountdInit()*/STATUS nfsdInit ( int nServers, /* the number of NFS servers to create */ int nExportedFs, /* maximum number of exported file systems */ int priority, /* the priority for the NFS servers */ FUNCPTR authHook, /* authentication hook */ FUNCPTR mountAuthHook, /* authentication hook for mount daemon */ int options /* currently unused */ ) { char serverName [50]; /* Synthetic name for NFS servers */ int mountTask; /* taskId of the mountd task */ int queuingTask; /* taskId of the task that queues NFS calls */ /* * Scaling stack with change in NFS_MAXPATH since multiple arrays of size * NFS_MAXPATH are allocated from stack. Difference is computed from * default max. path of 255. */ nfsdStackSize += 4 * (nfsMaxPath - 255); /* Set up call statistics */ memset (&nfsdServerStatus, 0, sizeof (nfsdServerStatus)); /* Set up authorization hook */ nfsdAuthHook = authHook; /* If number and priority of servers isn't specified, set it to default */ if (nServers == 0) nServers = nfsdNServers; if (priority == 0) priority = nfsdPriorityDefault; if (nExportedFs == 0) nExportedFs = nfsdNFilesystemsDefault; /* Create the request queue */ if ((nfsRequestQ = msgQCreate (nServers * QLEN_PER_SRVR, sizeof (NFS_Q_REQUEST), MSG_Q_FIFO)) == NULL) return (ERROR); /* Spawn the mount task */ if ((mountTask = mountdInit (0, 0, mountAuthHook, 0, 0)) == ERROR) { msgQDelete (nfsRequestQ); return (ERROR); } /* spawn the queuing task */ if ((queuingTask = taskSpawn ("tNfsd", priority, VX_FP_TASK, nfsdStackSize, (FUNCPTR) nfsd, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) == ERROR) { taskDelete (mountTask); msgQDelete (nfsRequestQ); return (ERROR); } /* spawn the call processing tasks */ while (nServers-- > 0) { /* Create names of the form tNfsdX */ sprintf (serverName, "tNfsd%d", nServers); if ((taskSpawn (serverName, priority + 5, VX_FP_TASK, nfsdStackSize, (FUNCPTR) nfsdRequestProcess, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0)) == ERROR) { taskDelete (mountTask); taskDelete (queuingTask); msgQDelete (nfsRequestQ); return (ERROR); } } return (OK); }/******************************************************************************** nfsdRequestEnqueue - queue up NFS requests from clients** Called only via svc_run(), via nfsd(). Puts an incoming NFS request* into the message queue for processing by nfsdRequestProcess().** Some of this routine was generated by rpcgen.* * RETURNS: N/A* * NOMANUAL*/LOCAL void nfsdRequestEnqueue ( struct svc_req * rqstp, /* Request */ SVCXPRT * transp /* Transport */ ) { NFSD_ARGUMENT * argument; /* Client call argument */ FUNCPTR xdr_result; /* XDR result conversion routine */ FUNCPTR xdr_argument; /* XDR argument conversion routine */ char * (*local)(); /* Local routine to call */ NFS_Q_REQUEST request; /* Request to queue up */ /* Allocate space for the argument */ argument = KHEAP_ALLOC((NFS_MAXDATA + sizeof (argument) + nfsMaxPath)); if (NULL != argument) { bzero((char *) argument,(NFS_MAXDATA + sizeof (argument) + nfsMaxPath)); } /* Beginning of code generated by rpcgen */ switch (rqstp->rq_proc) { case NFSPROC_NULL: xdr_argument = xdr_void; xdr_result = xdr_void; local = (char *(*)()) nfsproc_null_2; break; case NFSPROC_GETATTR: xdr_argument = xdr_nfs_fh; xdr_result = xdr_attrstat; local = (char *(*)()) nfsproc_getattr_2; break; case NFSPROC_SETATTR: xdr_argument = xdr_sattrargs; xdr_result = xdr_attrstat; local = (char *(*)()) nfsproc_setattr_2; break; case NFSPROC_ROOT: xdr_argument = xdr_void; xdr_result = xdr_void; local = (char *(*)()) nfsproc_root_2; break; case NFSPROC_LOOKUP: xdr_argument = xdr_diropargs; xdr_result = xdr_diropres; local = (char *(*)()) nfsproc_lookup_2; break; case NFSPROC_READLINK: xdr_argument = xdr_nfs_fh; xdr_result = xdr_readlinkres; local = (char *(*)()) nfsproc_readlink_2; break; case NFSPROC_READ: xdr_argument = xdr_readargs; xdr_result = xdr_readres; local = (char *(*)()) nfsproc_read_2; break; case NFSPROC_WRITECACHE: xdr_argument = xdr_void; xdr_result = xdr_void; local = (char *(*)()) nfsproc_writecache_2; break; case NFSPROC_WRITE: xdr_argument = xdr_writeargs; xdr_result = xdr_attrstat; local = (char *(*)()) nfsproc_write_2; break; case NFSPROC_CREATE: xdr_argument = xdr_createargs; xdr_result = xdr_diropres; local = (char *(*)()) nfsproc_create_2; break; case NFSPROC_REMOVE: xdr_argument = xdr_diropargs; xdr_result = xdr_nfsstat; local = (char *(*)()) nfsproc_remove_2; break; case NFSPROC_RENAME: xdr_argument = xdr_renameargs; xdr_result = xdr_nfsstat; local = (char *(*)()) nfsproc_rename_2; break; case NFSPROC_LINK: xdr_argument = xdr_linkargs; xdr_result = xdr_nfsstat; local = (char *(*)()) nfsproc_link_2; break; case NFSPROC_SYMLINK: xdr_argument = xdr_symlinkargs; xdr_result = xdr_nfsstat; local = (char *(*)()) nfsproc_symlink_2; break; case NFSPROC_MKDIR: xdr_argument = xdr_createargs; xdr_result = xdr_diropres; local = (char *(*)()) nfsproc_mkdir_2; break; case NFSPROC_RMDIR: xdr_argument = xdr_diropargs; xdr_result = xdr_nfsstat; local = (char *(*)()) nfsproc_rmdir_2; break; case NFSPROC_READDIR: xdr_argument = xdr_readdirargs; xdr_result = xdr_readdirres; local = (char *(*)()) nfsproc_readdir_2; break; case NFSPROC_STATFS: xdr_argument = xdr_nfs_fh; xdr_result = xdr_statfsres; local = (char *(*)()) nfsproc_statfs_2; break; default: KHEAP_FREE((char *)argument); svcerr_noproc(transp); return; } if (!svc_getargs(transp, xdr_argument, argument)) { svcerr_decode(transp); return; } /* end of code generated by rpcgen */ /* Build the request and put it into the message queue */ request.routine = (FUNCPTR) local; request.procNum = rqstp->rq_proc; request.argument = argument; request.xdrArg = xdr_argument; request.xdrResult = xdr_result; request.sockAddr = transp->xp_raddr; /* only use xp_p2 for udp requests */ if (transp->xp_p2 != NULL) request.xid = ((struct svcudp_data *) transp->xp_p2)->su_xid; request.socket = transp->xp_sock; if (msgQSend (nfsRequestQ, (char *) &request, sizeof (request), WAIT_FOREVER, MSG_PRI_NORMAL) != OK) { KHEAP_FREE((char *)argument); perror ("nfsd aborted"); return; } }/******************************************************************************** nfsdRequestProcess - process client NFS requests* * nfsdRequestProcess is started as a task by nfsdInit(). It sits in a loop* reading nfsdRequestQ and responding to all requests that are put into this* queue.* * Multiple processes can be spawned with this entry point. Normally,* nfsdNServers are started when nfsdInit() is called.* * RETURNS: N/A** NOMANUAL*/void nfsdRequestProcess ( void ) { SVCXPRT * transp; /* Dummy transport for RPC */ int sock; /* Transmission socket for reply */ NFS_Q_REQUEST request; /* Client request */ NFSD_ARGUMENT * argument; /* Argument from client request */ char * result; /* Result of NFS call */ FUNCPTR xdr_argument; /* XDR argument conversion routine */ FUNCPTR xdr_result; /* XDR result conversion routine */ FUNCPTR local; /* Local NFS routine to call */ struct sockaddr_in * addr; /* Address of client socket */ int xid; /* XID from client request */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -