123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989 |
- /****************************************************************************
- * fs/userfs/fs_userfs.c
- *
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership. The
- * ASF licenses this file to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the
- * License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- ****************************************************************************/
- /****************************************************************************
- * Included Files
- ****************************************************************************/
- #include <nuttx/config.h>
- #include <sys/types.h>
- #include <sys/statfs.h>
- #include <sys/stat.h>
- #include <sys/socket.h>
- #include <stdint.h>
- #include <stdbool.h>
- #include <string.h>
- #include <fcntl.h>
- #include <assert.h>
- #include <errno.h>
- #include <debug.h>
- #include <arpa/inet.h>
- #include <netinet/in.h>
- #include <nuttx/kmalloc.h>
- #include <nuttx/fs/fs.h>
- #include <nuttx/fs/userfs.h>
- #include <nuttx/fs/dirent.h>
- #include <nuttx/fs/ioctl.h>
- #include <nuttx/net/net.h>
- #include <nuttx/semaphore.h>
- /****************************************************************************
- * Pre-processor Definitions
- ****************************************************************************/
- #define IOBUFFER_SIZE(p) (USERFS_REQ_MAXSIZE + (p)->mxwrite)
- /****************************************************************************
- * Private Types
- ****************************************************************************/
- /* This structure holds the internal state of the UserFS proxy */
- struct userfs_state_s
- {
- /* Fields copied from struct userfs_config_s */
- size_t mxwrite; /* The max size of a write data */
- /* Internal state */
- struct socket psock; /* Client socket instance */
- struct sockaddr_in server; /* Server address */
- sem_t exclsem; /* Exclusive access for request-response sequence */
- /* I/O Buffer (actual size depends on USERFS_REQ_MAXSIZE and the configured
- * mxwrite).
- */
- uint8_t iobuffer[1];
- };
- #define SIZEOF_USERFS_STATE_S(n) (sizeof(struct userfs_state_s) + (n) - 1)
- /****************************************************************************
- * Private Function Prototypes
- ****************************************************************************/
- static int userfs_open(FAR struct file *filep, const char *relpath,
- int oflags, mode_t mode);
- static int userfs_close(FAR struct file *filep);
- static ssize_t userfs_read(FAR struct file *filep, char *buffer,
- size_t buflen);
- static ssize_t userfs_read(FAR struct file *filep, FAR char *buffer,
- size_t buflen);
- static ssize_t userfs_write(FAR struct file *filep, FAR const char *buffer,
- size_t buflen);
- static off_t userfs_seek(FAR struct file *filep, off_t offset, int whence);
- static int userfs_ioctl(FAR struct file *filep, int cmd,
- unsigned long arg);
- static int userfs_sync(FAR struct file *filep);
- static int userfs_dup(FAR const struct file *oldp,
- FAR struct file *newp);
- static int userfs_fstat(FAR const struct file *filep,
- FAR struct stat *buf);
- static int userfs_truncate(FAR struct file *filep, off_t length);
- static int userfs_opendir(FAR struct inode *mountpt,
- FAR const char *relpath, FAR struct fs_dirent_s *dir);
- static int userfs_closedir(FAR struct inode *mountpt,
- FAR struct fs_dirent_s *dir);
- static int userfs_readdir(FAR struct inode *mountpt,
- FAR struct fs_dirent_s *dir);
- static int userfs_rewinddir(FAR struct inode *mountpt,
- FAR struct fs_dirent_s *dir);
- static int userfs_bind(FAR struct inode *blkdriver, FAR const void *data,
- FAR void **handle);
- static int userfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
- unsigned int flags);
- static int userfs_statfs(FAR struct inode *mountpt,
- FAR struct statfs *buf);
- static int userfs_unlink(FAR struct inode *mountpt,
- FAR const char *relpath);
- static int userfs_mkdir(FAR struct inode *mountpt,
- FAR const char *relpath, mode_t mode);
- static int userfs_rmdir(FAR struct inode *mountpt,
- FAR const char *relpath);
- static int userfs_rename(FAR struct inode *mountpt,
- FAR const char *oldrelpath, FAR const char *newrelpath);
- static int userfs_stat(FAR struct inode *mountpt,
- FAR const char *relpath, FAR struct stat *buf);
- /****************************************************************************
- * Public Data
- ****************************************************************************/
- /* See fs_mount.c -- this structure is explicitly extern'ed there.
- * We use the old-fashioned kind of initializers so that this will compile
- * with any compiler.
- */
- const struct mountpt_operations userfs_operations =
- {
- userfs_open, /* open */
- userfs_close, /* close */
- userfs_read, /* read */
- userfs_write, /* write */
- userfs_seek, /* seek */
- userfs_ioctl, /* ioctl */
- userfs_sync, /* sync */
- userfs_dup, /* dup */
- userfs_fstat, /* fstat */
- userfs_truncate, /* truncate */
- userfs_opendir, /* opendir */
- userfs_closedir, /* closedir */
- userfs_readdir, /* readdir */
- userfs_rewinddir, /* rewinddir */
- userfs_bind, /* bind */
- userfs_unbind, /* unbind */
- userfs_statfs, /* statfs */
- userfs_unlink, /* unlink */
- userfs_mkdir, /* mkdir */
- userfs_rmdir, /* rmdir */
- userfs_rename, /* rename */
- userfs_stat /* stat */
- };
- /****************************************************************************
- * Private Functions
- ****************************************************************************/
- /****************************************************************************
- * Name: userfs_open
- ****************************************************************************/
- static int userfs_open(FAR struct file *filep, FAR const char *relpath,
- int oflags, mode_t mode)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_open_request_s *req;
- FAR struct userfs_open_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int pathlen;
- int ret;
- finfo("Open '%s'\n", relpath);
- DEBUGASSERT(filep != NULL &&
- filep->f_inode != NULL &&
- filep->f_inode->i_private != NULL);
- priv = filep->f_inode->i_private;
- /* Check the path length */
- DEBUGASSERT(relpath != NULL);
- pathlen = strlen(relpath);
- if (pathlen > priv->mxwrite)
- {
- return -E2BIG;
- }
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_open_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_OPEN;
- req->oflags = oflags;
- req->mode = mode;
- strncpy(req->relpath, relpath, priv->mxwrite);
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- SIZEOF_USERFS_OPEN_REQUEST_S(pathlen + 1), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_open_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- /* Save the returned openinfo as the filep private data. */
- resp = (FAR struct userfs_open_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_OPEN)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- filep->f_priv = resp->openinfo;
- return resp->ret;
- }
- /****************************************************************************
- * Name: userfs_close
- ****************************************************************************/
- static int userfs_close(FAR struct file *filep)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_close_request_s *req;
- FAR struct userfs_close_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int ret;
- DEBUGASSERT(filep != NULL &&
- filep->f_inode != NULL &&
- filep->f_inode->i_private != NULL);
- priv = filep->f_inode->i_private;
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_close_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_CLOSE;
- req->openinfo = filep->f_priv;
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- sizeof(struct userfs_close_request_s), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_close_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_close_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_CLOSE)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- if (resp->ret >= 0)
- {
- filep->f_priv = NULL;
- }
- return resp->ret;
- }
- /****************************************************************************
- * Name: userfs_read
- ****************************************************************************/
- static ssize_t userfs_read(FAR struct file *filep, char *buffer,
- size_t buflen)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_read_request_s *req;
- FAR struct userfs_read_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int respsize;
- int ret;
- finfo("Read %zu bytes from offset %jd\n", buflen, (intmax_t)filep->f_pos);
- DEBUGASSERT(filep != NULL &&
- filep->f_inode != NULL &&
- filep->f_inode->i_private != NULL);
- priv = filep->f_inode->i_private;
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_read_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_READ;
- req->openinfo = filep->f_priv;
- req->readlen = buflen;
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- sizeof(struct userfs_read_request_s), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd < SIZEOF_USERFS_READ_RESPONSE_S(0))
- {
- ferr("ERROR: Response too small: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_read_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_READ)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- if (resp->nread > buflen)
- {
- ferr("ERROR: Response size too large: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- respsize = SIZEOF_USERFS_READ_RESPONSE_S(resp->nread);
- if (respsize != nrecvd)
- {
- ferr("ERROR: Incorrect response size: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- /* Copy the received data to the user buffer */
- memcpy(buffer, resp->rddata, resp->nread);
- return resp->nread;
- }
- /****************************************************************************
- * Name: userfs_write
- ****************************************************************************/
- static ssize_t userfs_write(FAR struct file *filep, FAR const char *buffer,
- size_t buflen)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_write_request_s *req;
- FAR struct userfs_write_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int ret;
- finfo("Write %zu bytes to offset %jd\n", buflen, (intmax_t)filep->f_pos);
- DEBUGASSERT(filep != NULL &&
- filep->f_inode != NULL &&
- filep->f_inode->i_private != NULL);
- priv = filep->f_inode->i_private;
- /* Perform multiple writes if the write length exceeds the configured
- * maximum (mxwrite).
- */
- if (buflen > priv->mxwrite)
- {
- return -E2BIG; /* No implemented yet */
- }
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_write_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_WRITE;
- req->openinfo = filep->f_priv;
- req->writelen = buflen;
- memcpy(req->wrdata, buffer, buflen);
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- SIZEOF_USERFS_WRITE_REQUEST_S(buflen), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_write_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_write_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_WRITE)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- return resp->nwritten;
- }
- /****************************************************************************
- * Name: userfs_seek
- ****************************************************************************/
- static off_t userfs_seek(FAR struct file *filep, off_t offset, int whence)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_seek_request_s *req;
- FAR struct userfs_seek_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int ret;
- finfo("Offset %lu bytes to whence=%d\n", (unsigned long)offset, whence);
- DEBUGASSERT(filep != NULL &&
- filep->f_inode != NULL &&
- filep->f_inode->i_private != NULL);
- priv = filep->f_inode->i_private;
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_seek_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_SEEK;
- req->openinfo = filep->f_priv;
- req->offset = offset;
- req->whence = whence;
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- sizeof(struct userfs_seek_request_s), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_seek_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_seek_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_SEEK)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- return resp->ret;
- }
- /****************************************************************************
- * Name: userfs_ioctl
- ****************************************************************************/
- static int userfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_ioctl_request_s *req;
- FAR struct userfs_ioctl_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int ret;
- finfo("cmd: %d arg: %08lx\n", cmd, arg);
- DEBUGASSERT(filep != NULL &&
- filep->f_inode != NULL &&
- filep->f_inode->i_private != NULL);
- priv = filep->f_inode->i_private;
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_ioctl_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_IOCTL;
- req->openinfo = filep->f_priv;
- req->cmd = cmd;
- req->arg = arg;
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- sizeof(struct userfs_ioctl_request_s), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_ioctl_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_ioctl_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_IOCTL)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- return resp->ret;
- }
- /****************************************************************************
- * Name: userfs_sync
- ****************************************************************************/
- static int userfs_sync(FAR struct file *filep)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_sync_request_s *req;
- FAR struct userfs_sync_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int ret;
- DEBUGASSERT(filep != NULL &&
- filep->f_inode != NULL &&
- filep->f_inode->i_private != NULL);
- priv = filep->f_inode->i_private;
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_sync_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_SYNC;
- req->openinfo = filep->f_priv;
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- sizeof(struct userfs_sync_request_s), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_sync_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_sync_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_SYNC)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- return resp->ret;
- }
- /****************************************************************************
- * Name: userfs_dup
- *
- * Description:
- * Duplicate open file data in the new file structure.
- *
- ****************************************************************************/
- static int userfs_dup(FAR const struct file *oldp, FAR struct file *newp)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_dup_request_s *req;
- FAR struct userfs_dup_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int ret;
- finfo("Dup %p->%p\n", oldp, newp);
- DEBUGASSERT(oldp != NULL &&
- oldp->f_inode != NULL &&
- oldp->f_inode->i_private != NULL);
- priv = oldp->f_inode->i_private;
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_dup_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_DUP;
- req->openinfo = oldp->f_priv;
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- sizeof(struct userfs_dup_request_s), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_dup_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_dup_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_DUP)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- newp->f_priv = resp->openinfo;
- return resp->ret;
- }
- /****************************************************************************
- * Name: userfs_fstat
- *
- * Description:
- * Obtain information about an open file associated with the file
- * descriptor 'fd', and will write it to the area pointed to by 'buf'.
- *
- ****************************************************************************/
- static int userfs_fstat(FAR const struct file *filep, FAR struct stat *buf)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_fstat_request_s *req;
- FAR struct userfs_fstat_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int ret;
- DEBUGASSERT(filep != NULL &&
- filep->f_inode != NULL &&
- filep->f_inode->i_private != NULL);
- priv = filep->f_inode->i_private;
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_fstat_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_FSTAT;
- req->openinfo = filep->f_priv;
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- sizeof(struct userfs_fstat_request_s), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_fstat_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_fstat_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_FSTAT)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- /* Return the status of the directory entry */
- DEBUGASSERT(buf != NULL);
- memcpy(buf, &resp->buf, sizeof(struct stat));
- return resp->ret;
- }
- /****************************************************************************
- * Name: userfs_truncate
- *
- * Description:
- * Set the size of the regular file referred to by 'filep' to 'length'
- *
- ****************************************************************************/
- static int userfs_truncate(FAR struct file *filep, off_t length)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_truncate_request_s *req;
- FAR struct userfs_truncate_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int ret;
- DEBUGASSERT(filep != NULL &&
- filep->f_inode != NULL &&
- filep->f_inode->i_private != NULL);
- priv = filep->f_inode->i_private;
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_truncate_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_TRUNCATE;
- req->openinfo = filep->f_priv;
- req->length = length;
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- sizeof(struct userfs_truncate_request_s), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_truncate_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_truncate_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_FSTAT)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- /* Return the result of truncate operation */
- return resp->ret;
- }
- /****************************************************************************
- * Name: userfs_opendir
- *
- * Description:
- * Open a directory
- *
- ****************************************************************************/
- static int userfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
- FAR struct fs_dirent_s *dir)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_opendir_request_s *req;
- FAR struct userfs_opendir_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int pathlen;
- int ret;
- finfo("relpath: \"%s\"\n", relpath ? relpath : "NULL");
- DEBUGASSERT(mountpt != NULL &&
- mountpt->i_private != NULL);
- priv = mountpt->i_private;
- /* Check the path length */
- DEBUGASSERT(relpath != NULL);
- pathlen = strlen(relpath);
- if (pathlen > priv->mxwrite)
- {
- return -E2BIG;
- }
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_opendir_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_OPENDIR;
- strncpy(req->relpath, relpath, priv->mxwrite);
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- SIZEOF_USERFS_OPENDIR_REQUEST_S(pathlen + 1), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_opendir_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_opendir_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_OPENDIR)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- /* Save the opaque dir reference in struct fs_dirent_s */
- DEBUGASSERT(dir != NULL);
- dir->u.userfs.fs_dir = resp->dir;
- return resp->ret;
- }
- /****************************************************************************
- * Name: userfs_closedir
- *
- * Description:
- * Close a directory
- *
- ****************************************************************************/
- static int userfs_closedir(FAR struct inode *mountpt,
- FAR struct fs_dirent_s *dir)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_closedir_request_s *req;
- FAR struct userfs_closedir_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int ret;
- DEBUGASSERT(mountpt != NULL &&
- mountpt->i_private != NULL);
- priv = mountpt->i_private;
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_closedir_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_CLOSEDIR;
- req->dir = dir->u.userfs.fs_dir;
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- sizeof(struct userfs_closedir_request_s), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_closedir_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_closedir_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_CLOSEDIR)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- return resp->ret;
- }
- /****************************************************************************
- * Name: userfs_readdir
- *
- * Description: Read the next directory entry
- *
- ****************************************************************************/
- static int userfs_readdir(FAR struct inode *mountpt,
- FAR struct fs_dirent_s *dir)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_readdir_request_s *req;
- FAR struct userfs_readdir_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int ret;
- DEBUGASSERT(mountpt != NULL &&
- mountpt->i_private != NULL);
- priv = mountpt->i_private;
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_readdir_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_READDIR;
- req->dir = dir->u.userfs.fs_dir;
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- sizeof(struct userfs_readdir_request_s), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_readdir_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_readdir_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_READDIR)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- /* Return the dirent */
- DEBUGASSERT(dir != NULL);
- memcpy(&dir->fd_dir, &resp->entry, sizeof(struct dirent));
- return resp->ret;
- }
- /****************************************************************************
- * Name: userfs_rewindir
- *
- * Description: Reset directory read to the first entry
- *
- ****************************************************************************/
- static int userfs_rewinddir(FAR struct inode *mountpt,
- FAR struct fs_dirent_s *dir)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_rewinddir_request_s *req;
- FAR struct userfs_rewinddir_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int ret;
- DEBUGASSERT(mountpt != NULL &&
- mountpt->i_private != NULL);
- priv = mountpt->i_private;
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_rewinddir_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_REWINDDIR;
- req->dir = dir->u.userfs.fs_dir;
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- sizeof(struct userfs_rewinddir_request_s), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_rewinddir_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_rewinddir_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_REWINDDIR)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- return resp->ret;
- }
- /****************************************************************************
- * Name: userfs_bind
- *
- * Description: This implements a portion of the mount operation. This
- * function allocates and initializes the mountpoint private data and
- * binds the blockdriver inode to the filesystem private data. The final
- * binding of the private data (containing the blockdriver) to the
- * mountpoint is performed by mount().
- *
- ****************************************************************************/
- static int userfs_bind(FAR struct inode *blkdriver, FAR const void *data,
- FAR void **handle)
- {
- FAR struct userfs_state_s *priv;
- FAR const struct userfs_config_s *config;
- struct sockaddr_in client;
- unsigned int iolen;
- int ret;
- DEBUGASSERT(data != NULL && handle != NULL);
- config = (FAR const struct userfs_config_s *)data;
- /* Allocate an instance of the UserFS state structure */
- iolen = USERFS_REQ_MAXSIZE + config->mxwrite;
- priv = kmm_malloc(SIZEOF_USERFS_STATE_S(iolen));
- if (priv == NULL)
- {
- ferr("ERROR: Failed to allocate state structure\n");
- return -ENOMEM;
- }
- /* Initialize the semaphore that assures mutually exclusive access through
- * the entire request-response sequence.
- */
- nxsem_init(&priv->exclsem, 0, 1);
- /* Copy the configuration data into the allocated structure. Why? First
- * we can't be certain of the life time of the memory underlying the config
- * reference. Also, in the KERNEL build, the config data will like in
- * process-specific memory and cannot be shared across processes.
- */
- priv->mxwrite = config->mxwrite;
- /* Preset the server address */
- priv->server.sin_family = AF_INET;
- priv->server.sin_port = htons(config->portno);
- priv->server.sin_addr.s_addr = HTONL(INADDR_LOOPBACK);
- /* Create a LocalHost UDP client socket */
- ret = psock_socket(PF_INET, SOCK_DGRAM, 0, &priv->psock);
- if (ret < 0)
- {
- ferr("ERROR: socket() failed: %d\n", ret);
- goto errout_with_alloc;
- }
- /* Bind the socket to the client address */
- client.sin_family = AF_INET;
- client.sin_port = 0;
- client.sin_addr.s_addr = HTONL(INADDR_LOOPBACK);
- ret = psock_bind(&priv->psock, (FAR struct sockaddr *)&client,
- sizeof(struct sockaddr_in));
- if (ret < 0)
- {
- ferr("ERROR: bind() failed: %d\n", ret);
- goto errout_with_psock;
- }
- /* Mounted! */
- *handle = (FAR void *)priv;
- return OK;
- errout_with_psock:
- psock_close(&priv->psock);
- errout_with_alloc:
- kmm_free(priv);
- return ret;
- }
- /****************************************************************************
- * Name: userfs_unbind
- *
- * Description: This implements the filesystem portion of the umount
- * operation.
- *
- ****************************************************************************/
- static int userfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
- unsigned int flags)
- {
- FAR struct userfs_state_s *priv = (FAR struct userfs_state_s *)handle;
- FAR struct userfs_destroy_request_s *req;
- FAR struct userfs_destroy_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int ret;
- DEBUGASSERT(priv != NULL);
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_destroy_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_DESTROY;
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- sizeof(struct userfs_destroy_request_s), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_destroy_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_destroy_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_DESTROY)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- /* If the destruction failed, then refuse to unmount at this time */
- if (resp->ret < 0)
- {
- return resp->ret;
- }
- /* Free resources and return success */
- psock_close(&priv->psock);
- kmm_free(priv);
- return OK;
- }
- /****************************************************************************
- * Name: userfs_statfs
- *
- * Description:
- * Return filesystem statistics
- *
- ****************************************************************************/
- static int userfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_statfs_request_s *req;
- FAR struct userfs_statfs_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int ret;
- DEBUGASSERT(mountpt != NULL &&
- mountpt->i_private != NULL);
- priv = mountpt->i_private;
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_statfs_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_STATFS;
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- sizeof(struct userfs_statfs_request_s), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_statfs_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_statfs_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_STATFS)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- /* Return the status of the file system */
- DEBUGASSERT(buf != NULL);
- memcpy(buf, &resp->buf, sizeof(struct statfs));
- return resp->ret;
- }
- /****************************************************************************
- * Name: userfs_unlink
- *
- * Description:
- * Remove a directory entry
- *
- ****************************************************************************/
- static int userfs_unlink(FAR struct inode *mountpt,
- FAR const char *relpath)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_unlink_request_s *req;
- FAR struct userfs_unlink_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int pathlen;
- int ret;
- DEBUGASSERT(mountpt != NULL &&
- mountpt->i_private != NULL);
- priv = mountpt->i_private;
- /* Check the path length */
- DEBUGASSERT(relpath != NULL);
- pathlen = strlen(relpath);
- if (pathlen > priv->mxwrite)
- {
- return -E2BIG;
- }
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_unlink_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_UNLINK;
- strncpy(req->relpath, relpath, priv->mxwrite);
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- SIZEOF_USERFS_UNLINK_REQUEST_S(pathlen + 1), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_unlink_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_unlink_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_UNLINK)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- return resp->ret;
- }
- /****************************************************************************
- * Name: userfs_mkdir
- *
- * Description:
- * Create a new directory
- *
- ****************************************************************************/
- static int userfs_mkdir(FAR struct inode *mountpt,
- FAR const char *relpath, mode_t mode)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_mkdir_request_s *req;
- FAR struct userfs_mkdir_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int pathlen;
- int ret;
- DEBUGASSERT(mountpt != NULL &&
- mountpt->i_private != NULL);
- priv = mountpt->i_private;
- /* Check the path length */
- DEBUGASSERT(relpath != NULL);
- pathlen = strlen(relpath);
- if (pathlen > priv->mxwrite)
- {
- return -E2BIG;
- }
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_mkdir_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_MKDIR;
- req->mode = mode;
- strncpy(req->relpath, relpath, priv->mxwrite);
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- SIZEOF_USERFS_MKDIR_REQUEST_S(pathlen + 1), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_mkdir_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_mkdir_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_MKDIR)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- return resp->ret;
- }
- /****************************************************************************
- * Name: userfs_rmdir
- *
- * Description:
- * Remove a directory
- *
- ****************************************************************************/
- static int userfs_rmdir(FAR struct inode *mountpt,
- FAR const char *relpath)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_rmdir_request_s *req;
- FAR struct userfs_rmdir_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int pathlen;
- int ret;
- DEBUGASSERT(mountpt != NULL &&
- mountpt->i_private != NULL);
- priv = mountpt->i_private;
- /* Check the path length */
- DEBUGASSERT(relpath != NULL);
- pathlen = strlen(relpath);
- if (pathlen > priv->mxwrite)
- {
- return -E2BIG;
- }
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_rmdir_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_RMDIR;
- strncpy(req->relpath, relpath, priv->mxwrite);
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- SIZEOF_USERFS_RMDIR_REQUEST_S(pathlen + 1), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_rmdir_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_rmdir_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_RMDIR)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- return resp->ret;
- }
- /****************************************************************************
- * Name: userfs_rename
- *
- * Description:
- * Rename a directory entry
- *
- ****************************************************************************/
- static int userfs_rename(FAR struct inode *mountpt,
- FAR const char *oldrelpath,
- FAR const char *newrelpath)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_rename_request_s *req;
- FAR struct userfs_rename_response_s *resp;
- int oldpathlen;
- int newpathlen;
- ssize_t nsent;
- ssize_t nrecvd;
- int ret;
- DEBUGASSERT(mountpt != NULL &&
- mountpt->i_private != NULL);
- priv = mountpt->i_private;
- /* Check the path lengths */
- DEBUGASSERT(oldrelpath != NULL && newrelpath != NULL);
- oldpathlen = strlen(oldrelpath) + 1;
- newpathlen = strlen(newrelpath) + 1;
- if ((oldpathlen + newpathlen) > priv->mxwrite)
- {
- return -E2BIG;
- }
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_rename_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_RENAME;
- req->newoffset = oldpathlen;
- strncpy(req->oldrelpath, oldrelpath, oldpathlen);
- strncpy(&req->oldrelpath[oldpathlen], newrelpath, newpathlen);
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- SIZEOF_USERFS_RENAME_REQUEST_S(oldpathlen, newpathlen),
- 0, (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_rename_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_rename_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_RENAME)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- return resp->ret;
- }
- /****************************************************************************
- * Name: userfs_stat
- *
- * Description:
- * Return information about a file or directory
- *
- ****************************************************************************/
- static int userfs_stat(FAR struct inode *mountpt, FAR const char *relpath,
- FAR struct stat *buf)
- {
- FAR struct userfs_state_s *priv;
- FAR struct userfs_stat_request_s *req;
- FAR struct userfs_stat_response_s *resp;
- ssize_t nsent;
- ssize_t nrecvd;
- int pathlen;
- int ret;
- DEBUGASSERT(mountpt != NULL &&
- mountpt->i_private != NULL);
- priv = mountpt->i_private;
- /* Check the path length */
- DEBUGASSERT(relpath != NULL);
- pathlen = strlen(relpath);
- if (pathlen > priv->mxwrite)
- {
- return -E2BIG;
- }
- /* Get exclusive access */
- ret = nxsem_wait(&priv->exclsem);
- if (ret < 0)
- {
- return ret;
- }
- /* Construct and send the request to the server */
- req = (FAR struct userfs_stat_request_s *)priv->iobuffer;
- req->req = USERFS_REQ_STAT;
- strncpy(req->relpath, relpath, priv->mxwrite);
- nsent = psock_sendto(&priv->psock, priv->iobuffer,
- SIZEOF_USERFS_STAT_REQUEST_S(pathlen + 1), 0,
- (FAR struct sockaddr *)&priv->server,
- sizeof(struct sockaddr_in));
- if (nsent < 0)
- {
- ferr("ERROR: psock_sendto failed: %d\n", (int)nsent);
- nxsem_post(&priv->exclsem);
- return (int)nsent;
- }
- /* Then get the response from the server */
- nrecvd = psock_recvfrom(&priv->psock, priv->iobuffer, IOBUFFER_SIZE(priv),
- 0, NULL, NULL);
- nxsem_post(&priv->exclsem);
- if (nrecvd < 0)
- {
- ferr("ERROR: psock_recvfrom failed: %d\n", (int)nrecvd);
- return (int)nrecvd;
- }
- if (nrecvd != sizeof(struct userfs_stat_response_s))
- {
- ferr("ERROR: Response size incorrect: %u\n", (unsigned int)nrecvd);
- return -EIO;
- }
- resp = (FAR struct userfs_stat_response_s *)priv->iobuffer;
- if (resp->resp != USERFS_RESP_STAT)
- {
- ferr("ERROR: Incorrect response: %u\n", resp->resp);
- return -EIO;
- }
- /* Return the directory entry status */
- DEBUGASSERT(buf != NULL);
- memcpy(buf, &resp->buf, sizeof(struct stat));
- return resp->ret;
- }
- /****************************************************************************
- * Public Functions
- ****************************************************************************/
|