iPXE
nfs.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Marin Hannache <ipxe@mareo.fr>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  */
00019 
00020 #include <stdint.h>
00021 #include <stdlib.h>
00022 #include <stdio.h>
00023 #include <string.h>
00024 #include <assert.h>
00025 #include <errno.h>
00026 #include <libgen.h>
00027 #include <byteswap.h>
00028 #include <ipxe/time.h>
00029 #include <ipxe/iobuf.h>
00030 #include <ipxe/open.h>
00031 #include <ipxe/features.h>
00032 #include <ipxe/nfs.h>
00033 #include <ipxe/oncrpc.h>
00034 #include <ipxe/oncrpc_iob.h>
00035 #include <ipxe/portmap.h>
00036 #include <ipxe/mount.h>
00037 #include <ipxe/settings.h>
00038 
00039 /** @file
00040  *
00041  * Network File System protocol
00042  *
00043  */
00044 
00045 /** NFS LOOKUP procedure */
00046 #define NFS_LOOKUP      3
00047 /** NFS READLINK procedure */
00048 #define NFS_READLINK    5
00049 /** NFS READ procedure */
00050 #define NFS_READ        6
00051 
00052 /**
00053  * Extract a file handle from the beginning of an I/O buffer
00054  *
00055  * @v io_buf            I/O buffer
00056  * @v fh                File handle
00057  * @ret size            Size of the data read
00058  */
00059 size_t nfs_iob_get_fh ( struct io_buffer *io_buf, struct nfs_fh *fh ) {
00060         fh->size = oncrpc_iob_get_int ( io_buf );
00061 
00062         if ( fh->size > 64 )
00063                 return sizeof ( uint32_t );
00064 
00065         memcpy (fh->fh, io_buf->data, fh->size );
00066         iob_pull ( io_buf, fh->size );
00067 
00068         return fh->size + sizeof ( uint32_t );
00069 }
00070 
00071 /**
00072  * Add a file handle to the end of an I/O buffer
00073  *
00074  * @v io_buf            I/O buffer
00075  * @v fh                File handle
00076  * @ret size            Size of the data written
00077  */
00078 size_t nfs_iob_add_fh ( struct io_buffer *io_buf, const struct nfs_fh *fh ) {
00079         size_t s;
00080 
00081         s = oncrpc_iob_add_int ( io_buf, fh->size );
00082         memcpy ( iob_put ( io_buf, fh->size ), &fh->fh, fh->size );
00083 
00084         return s + fh->size;
00085 }
00086 
00087 /**
00088  * Send a LOOKUP request
00089  *
00090  * @v intf              Interface to send the request on
00091  * @v session           ONC RPC session
00092  * @v fh                The file handle of the the directory
00093  * @v filename          The file name
00094  * @ret rc              Return status code
00095  */
00096 int nfs_lookup ( struct interface *intf, struct oncrpc_session *session,
00097                  const struct nfs_fh *fh, const char *filename ) {
00098         struct oncrpc_field fields[] = {
00099                 ONCRPC_SUBFIELD ( array, fh->size, &fh->fh ),
00100                 ONCRPC_FIELD ( str, filename ),
00101                 ONCRPC_FIELD_END,
00102         };
00103 
00104         return oncrpc_call ( intf, session, NFS_LOOKUP, fields );
00105 }
00106 
00107 /**
00108  * Send a READLINK request
00109  *
00110  * @v intf              Interface to send the request on
00111  * @v session           ONC RPC session
00112  * @v fh                The symlink file handle
00113  * @ret rc              Return status code
00114  */
00115 int nfs_readlink ( struct interface *intf, struct oncrpc_session *session,
00116                    const struct nfs_fh *fh ) {
00117         struct oncrpc_field fields[] = {
00118                 ONCRPC_SUBFIELD ( array, fh->size, &fh->fh ),
00119                 ONCRPC_FIELD_END,
00120         };
00121 
00122         return oncrpc_call ( intf, session, NFS_READLINK, fields );
00123 }
00124 
00125 /**
00126  * Send a READ request
00127  *
00128  * @v intf              Interface to send the request on
00129  * @v session           ONC RPC session
00130  * @v fh                The file handle
00131  * @v offset            Offset
00132  * @v count             Byte count
00133  * @ret rc              Return status code
00134  */
00135 int nfs_read ( struct interface *intf, struct oncrpc_session *session,
00136                const struct nfs_fh *fh, uint64_t offset, uint32_t count ) {
00137         struct oncrpc_field fields[] = {
00138                 ONCRPC_SUBFIELD ( array, fh->size, &fh->fh ),
00139                 ONCRPC_FIELD ( int64, offset ),
00140                 ONCRPC_FIELD ( int32, count ),
00141                 ONCRPC_FIELD_END,
00142         };
00143 
00144         return oncrpc_call ( intf, session, NFS_READ, fields );
00145 }
00146 
00147 /**
00148  * Parse a LOOKUP reply
00149  *
00150  * @v lookup_reply      A structure where the data will be saved
00151  * @v reply             The ONC RPC reply to get data from
00152  * @ret rc              Return status code
00153  */
00154 int nfs_get_lookup_reply ( struct nfs_lookup_reply *lookup_reply,
00155                            struct oncrpc_reply *reply ) {
00156         if ( ! lookup_reply || ! reply )
00157                 return -EINVAL;
00158 
00159         lookup_reply->status = oncrpc_iob_get_int ( reply->data );
00160         switch ( lookup_reply->status )
00161         {
00162         case NFS3_OK:
00163                 break;
00164         case NFS3ERR_PERM:
00165                 return -EPERM;
00166         case NFS3ERR_NOENT:
00167                 return -ENOENT;
00168         case NFS3ERR_IO:
00169                 return -EIO;
00170         case NFS3ERR_ACCES:
00171                 return -EACCES;
00172         case NFS3ERR_NOTDIR:
00173                 return -ENOTDIR;
00174         case NFS3ERR_NAMETOOLONG:
00175                 return -ENAMETOOLONG;
00176         case NFS3ERR_STALE:
00177                 return -ESTALE;
00178         case NFS3ERR_BADHANDLE:
00179         case NFS3ERR_SERVERFAULT:
00180         default:
00181                 return -EPROTO;
00182         }
00183 
00184         nfs_iob_get_fh ( reply->data, &lookup_reply->fh );
00185 
00186         if ( oncrpc_iob_get_int ( reply->data ) == 1 )
00187                 lookup_reply->ent_type = oncrpc_iob_get_int ( reply->data );
00188 
00189         return 0;
00190 }
00191 /**
00192  * Parse a READLINK reply
00193  *
00194  * @v readlink_reply    A structure where the data will be saved
00195  * @v reply             The ONC RPC reply to get data from
00196  * @ret rc              Return status code
00197  */
00198 int nfs_get_readlink_reply ( struct nfs_readlink_reply *readlink_reply,
00199                              struct oncrpc_reply *reply ) {
00200         if ( ! readlink_reply || ! reply )
00201                 return -EINVAL;
00202 
00203         readlink_reply->status = oncrpc_iob_get_int ( reply->data );
00204         switch ( readlink_reply->status )
00205         {
00206         case NFS3_OK:
00207                  break;
00208         case NFS3ERR_IO:
00209                 return -EIO;
00210         case NFS3ERR_ACCES:
00211                 return -EACCES;
00212         case NFS3ERR_INVAL:
00213                 return -EINVAL;
00214         case NFS3ERR_NOTSUPP:
00215                 return -ENOTSUP;
00216         case NFS3ERR_STALE:
00217                 return -ESTALE;
00218         case NFS3ERR_BADHANDLE:
00219         case NFS3ERR_SERVERFAULT:
00220         default:
00221                 return -EPROTO;
00222         }
00223 
00224         if ( oncrpc_iob_get_int ( reply->data ) == 1 )
00225                 iob_pull ( reply->data, 5 * sizeof ( uint32_t ) +
00226                                         8 * sizeof ( uint64_t ) );
00227 
00228         readlink_reply->path_len = oncrpc_iob_get_int ( reply->data );
00229         readlink_reply->path     = reply->data->data;
00230 
00231         return 0;
00232 }
00233 
00234 /**
00235  * Parse a READ reply
00236  *
00237  * @v read_reply        A structure where the data will be saved
00238  * @v reply             The ONC RPC reply to get data from
00239  * @ret rc              Return status code
00240  */
00241 int nfs_get_read_reply ( struct nfs_read_reply *read_reply,
00242                          struct oncrpc_reply *reply ) {
00243         if ( ! read_reply || ! reply )
00244                 return -EINVAL;
00245 
00246         read_reply->status = oncrpc_iob_get_int ( reply->data );
00247         switch ( read_reply->status )
00248         {
00249         case NFS3_OK:
00250                  break;
00251         case NFS3ERR_PERM:
00252                 return -EPERM;
00253         case NFS3ERR_NOENT:
00254                 return -ENOENT;
00255         case NFS3ERR_IO:
00256                 return -EIO;
00257         case NFS3ERR_NXIO:
00258                 return -ENXIO;
00259         case NFS3ERR_ACCES:
00260                 return -EACCES;
00261         case NFS3ERR_INVAL:
00262                 return -EINVAL;
00263         case NFS3ERR_STALE:
00264                 return -ESTALE;
00265         case NFS3ERR_BADHANDLE:
00266         case NFS3ERR_SERVERFAULT:
00267         default:
00268                 return -EPROTO;
00269         }
00270 
00271         if ( oncrpc_iob_get_int ( reply->data ) == 1 )
00272         {
00273                 iob_pull ( reply->data, 5 * sizeof ( uint32_t ) );
00274                 read_reply->filesize = oncrpc_iob_get_int64 ( reply->data );
00275                 iob_pull ( reply->data, 7 * sizeof ( uint64_t ) );
00276         }
00277 
00278         read_reply->count    = oncrpc_iob_get_int ( reply->data );
00279         read_reply->eof      = oncrpc_iob_get_int ( reply->data );
00280         read_reply->data_len = oncrpc_iob_get_int ( reply->data );
00281         read_reply->data     = reply->data->data;
00282 
00283         if ( read_reply->count != read_reply->data_len )
00284                 return -EPROTO;
00285 
00286         return 0;
00287 }
00288