fs_tmpfs.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679
  1. /****************************************************************************
  2. * fs/tmpfs/fs_tmpfs.c
  3. *
  4. * Copyright (C) 2015, 2017-2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <sys/stat.h>
  40. #include <sys/statfs.h>
  41. #include <stdint.h>
  42. #include <string.h>
  43. #include <fcntl.h>
  44. #include <dirent.h>
  45. #include <semaphore.h>
  46. #include <errno.h>
  47. #include <assert.h>
  48. #include <debug.h>
  49. #include <nuttx/kmalloc.h>
  50. #include <nuttx/fs/fs.h>
  51. #include <nuttx/fs/dirent.h>
  52. #include <nuttx/fs/ioctl.h>
  53. #include "fs_tmpfs.h"
  54. #ifndef CONFIG_DISABLE_MOUNTPOINT
  55. /****************************************************************************
  56. * Pre-processor Definitions
  57. ****************************************************************************/
  58. #if CONFIG_FS_TMPFS_DIRECTORY_FREEGUARD <= CONFIG_FS_TMPFS_DIRECTORY_ALLOCGUARD
  59. # warning CONFIG_FS_TMPFS_DIRECTORY_FREEGUARD needs to be > ALLOCGUARD
  60. #endif
  61. #if CONFIG_FS_TMPFS_FILE_FREEGUARD <= CONFIG_FS_TMPFS_FILE_ALLOCGUARD
  62. # warning CONFIG_FS_TMPFS_FILE_FREEGUARD needs to be > ALLOCGUARD
  63. #endif
  64. #define tmpfs_lock_file(tfo) \
  65. (tmpfs_lock_object((FAR struct tmpfs_object_s *)tfo))
  66. #define tmpfs_lock_directory(tdo) \
  67. (tmpfs_lock_object((FAR struct tmpfs_object_s *)tdo))
  68. #define tmpfs_unlock_file(tfo) \
  69. (tmpfs_unlock_object((FAR struct tmpfs_object_s *)tfo))
  70. #define tmpfs_unlock_directory(tdo) \
  71. (tmpfs_unlock_object((FAR struct tmpfs_object_s *)tdo))
  72. /****************************************************************************
  73. * Private Function Prototypes
  74. ****************************************************************************/
  75. /* TMPFS helpers */
  76. static void tmpfs_lock_reentrant(FAR struct tmpfs_sem_s *sem);
  77. static void tmpfs_lock(FAR struct tmpfs_s *fs);
  78. static void tmpfs_unlock_reentrant(FAR struct tmpfs_sem_s *sem);
  79. static void tmpfs_unlock(FAR struct tmpfs_s *fs);
  80. static void tmpfs_lock_object(FAR struct tmpfs_object_s *to);
  81. static void tmpfs_unlock_object(FAR struct tmpfs_object_s *to);
  82. static int tmpfs_realloc_directory(FAR struct tmpfs_directory_s **tdo,
  83. unsigned int nentries);
  84. static int tmpfs_realloc_file(FAR struct tmpfs_file_s **tfo,
  85. size_t newsize);
  86. static void tmpfs_release_lockedobject(FAR struct tmpfs_object_s *to);
  87. static void tmpfs_release_lockedfile(FAR struct tmpfs_file_s *tfo);
  88. static int tmpfs_find_dirent(FAR struct tmpfs_directory_s *tdo,
  89. FAR const char *name);
  90. static int tmpfs_remove_dirent(FAR struct tmpfs_directory_s *tdo,
  91. FAR const char *name);
  92. static int tmpfs_add_dirent(FAR struct tmpfs_directory_s **tdo,
  93. FAR struct tmpfs_object_s *to, FAR const char *name);
  94. static FAR struct tmpfs_file_s *tmpfs_alloc_file(void);
  95. static int tmpfs_create_file(FAR struct tmpfs_s *fs,
  96. FAR const char *relpath, FAR struct tmpfs_file_s **tfo);
  97. static FAR struct tmpfs_directory_s *tmpfs_alloc_directory(void);
  98. static int tmpfs_create_directory(FAR struct tmpfs_s *fs,
  99. FAR const char *relpath, FAR struct tmpfs_directory_s **tdo);
  100. static int tmpfs_find_object(FAR struct tmpfs_s *fs,
  101. FAR const char *relpath, FAR struct tmpfs_object_s **object,
  102. FAR struct tmpfs_directory_s **parent);
  103. static int tmpfs_find_file(FAR struct tmpfs_s *fs,
  104. FAR const char *relpath,
  105. FAR struct tmpfs_file_s **tfo,
  106. FAR struct tmpfs_directory_s **parent);
  107. static int tmpfs_find_directory(FAR struct tmpfs_s *fs,
  108. FAR const char *relpath,
  109. FAR struct tmpfs_directory_s **tdo,
  110. FAR struct tmpfs_directory_s **parent);
  111. static int tmpfs_statfs_callout(FAR struct tmpfs_directory_s *tdo,
  112. unsigned int index, FAR void *arg);
  113. static int tmpfs_free_callout(FAR struct tmpfs_directory_s *tdo,
  114. unsigned int index, FAR void *arg);
  115. static int tmpfs_foreach(FAR struct tmpfs_directory_s *tdo,
  116. tmpfs_foreach_t callout, FAR void *arg);
  117. /* File system operations */
  118. static int tmpfs_open(FAR struct file *filep, FAR const char *relpath,
  119. int oflags, mode_t mode);
  120. static int tmpfs_close(FAR struct file *filep);
  121. static ssize_t tmpfs_read(FAR struct file *filep, FAR char *buffer,
  122. size_t buflen);
  123. static ssize_t tmpfs_write(FAR struct file *filep, FAR const char *buffer,
  124. size_t buflen);
  125. static off_t tmpfs_seek(FAR struct file *filep, off_t offset, int whence);
  126. static int tmpfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg);
  127. static int tmpfs_dup(FAR const struct file *oldp, FAR struct file *newp);
  128. static int tmpfs_fstat(FAR const struct file *filep, FAR struct stat *buf);
  129. static int tmpfs_truncate(FAR struct file *filep, off_t length);
  130. static int tmpfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
  131. FAR struct fs_dirent_s *dir);
  132. static int tmpfs_closedir(FAR struct inode *mountpt,
  133. FAR struct fs_dirent_s *dir);
  134. static int tmpfs_readdir(FAR struct inode *mountpt,
  135. FAR struct fs_dirent_s *dir);
  136. static int tmpfs_rewinddir(FAR struct inode *mountpt,
  137. FAR struct fs_dirent_s *dir);
  138. static int tmpfs_bind(FAR struct inode *blkdriver, FAR const void *data,
  139. FAR void **handle);
  140. static int tmpfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
  141. unsigned int flags);
  142. static int tmpfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf);
  143. static int tmpfs_unlink(FAR struct inode *mountpt, FAR const char *relpath);
  144. static int tmpfs_mkdir(FAR struct inode *mountpt, FAR const char *relpath,
  145. mode_t mode);
  146. static int tmpfs_rmdir(FAR struct inode *mountpt, FAR const char *relpath);
  147. static int tmpfs_rename(FAR struct inode *mountpt, FAR const char *oldrelpath,
  148. FAR const char *newrelpath);
  149. static void tmpfs_stat_common(FAR struct tmpfs_object_s *to,
  150. FAR struct stat *buf);
  151. static int tmpfs_stat(FAR struct inode *mountpt, FAR const char *relpath,
  152. FAR struct stat *buf);
  153. /****************************************************************************
  154. * Public Data
  155. ****************************************************************************/
  156. const struct mountpt_operations tmpfs_operations =
  157. {
  158. tmpfs_open, /* open */
  159. tmpfs_close, /* close */
  160. tmpfs_read, /* read */
  161. tmpfs_write, /* write */
  162. tmpfs_seek, /* seek */
  163. tmpfs_ioctl, /* ioctl */
  164. NULL, /* sync */
  165. tmpfs_dup, /* dup */
  166. tmpfs_fstat, /* fstat */
  167. tmpfs_truncate, /* truncate */
  168. tmpfs_opendir, /* opendir */
  169. tmpfs_closedir, /* closedir */
  170. tmpfs_readdir, /* readdir */
  171. tmpfs_rewinddir, /* rewinddir */
  172. tmpfs_bind, /* bind */
  173. tmpfs_unbind, /* unbind */
  174. tmpfs_statfs, /* statfs */
  175. tmpfs_unlink, /* unlink */
  176. tmpfs_mkdir, /* mkdir */
  177. tmpfs_rmdir, /* rmdir */
  178. tmpfs_rename, /* rename */
  179. tmpfs_stat, /* stat */
  180. };
  181. /****************************************************************************
  182. * Private Functions
  183. ****************************************************************************/
  184. /****************************************************************************
  185. * Name: tmpfs_lock_reentrant
  186. ****************************************************************************/
  187. static void tmpfs_lock_reentrant(FAR struct tmpfs_sem_s *sem)
  188. {
  189. pid_t me;
  190. /* Do we already hold the semaphore? */
  191. me = getpid();
  192. if (me == sem->ts_holder)
  193. {
  194. /* Yes... just increment the count */
  195. sem->ts_count++;
  196. DEBUGASSERT(sem->ts_count > 0);
  197. }
  198. /* Take the semaphore (perhaps waiting) */
  199. else
  200. {
  201. int ret;
  202. do
  203. {
  204. ret = nxsem_wait(&sem->ts_sem);
  205. /* The only case that an error should occur here is if the wait
  206. * was awakened by a signal.
  207. */
  208. DEBUGASSERT(ret == OK || ret == -EINTR);
  209. }
  210. while (ret == -EINTR);
  211. /* No we hold the semaphore */
  212. sem->ts_holder = me;
  213. sem->ts_count = 1;
  214. }
  215. }
  216. /****************************************************************************
  217. * Name: tmpfs_lock
  218. ****************************************************************************/
  219. static void tmpfs_lock(FAR struct tmpfs_s *fs)
  220. {
  221. tmpfs_lock_reentrant(&fs->tfs_exclsem);
  222. }
  223. /****************************************************************************
  224. * Name: tmpfs_lock_object
  225. ****************************************************************************/
  226. static void tmpfs_lock_object(FAR struct tmpfs_object_s *to)
  227. {
  228. tmpfs_lock_reentrant(&to->to_exclsem);
  229. }
  230. /****************************************************************************
  231. * Name: tmpfs_unlock_reentrant
  232. ****************************************************************************/
  233. static void tmpfs_unlock_reentrant(FAR struct tmpfs_sem_s *sem)
  234. {
  235. DEBUGASSERT(sem->ts_holder == getpid());
  236. /* Is this our last count on the semaphore? */
  237. if (sem->ts_count > 1)
  238. {
  239. /* No.. just decrement the count */
  240. sem->ts_count--;
  241. }
  242. /* Yes.. then we can really release the semaphore */
  243. else
  244. {
  245. sem->ts_holder = TMPFS_NO_HOLDER;
  246. sem->ts_count = 0;
  247. nxsem_post(&sem->ts_sem);
  248. }
  249. }
  250. /****************************************************************************
  251. * Name: tmpfs_unlock
  252. ****************************************************************************/
  253. static void tmpfs_unlock(FAR struct tmpfs_s *fs)
  254. {
  255. tmpfs_unlock_reentrant(&fs->tfs_exclsem);
  256. }
  257. /****************************************************************************
  258. * Name: tmpfs_unlock_object
  259. ****************************************************************************/
  260. static void tmpfs_unlock_object(FAR struct tmpfs_object_s *to)
  261. {
  262. tmpfs_unlock_reentrant(&to->to_exclsem);
  263. }
  264. /****************************************************************************
  265. * Name: tmpfs_realloc_directory
  266. ****************************************************************************/
  267. static int tmpfs_realloc_directory(FAR struct tmpfs_directory_s **tdo,
  268. unsigned int nentries)
  269. {
  270. FAR struct tmpfs_directory_s *oldtdo = *tdo;
  271. FAR struct tmpfs_directory_s *newtdo;
  272. size_t objsize;
  273. int ret = oldtdo->tdo_nentries;
  274. /* Get the new object size */
  275. objsize = SIZEOF_TMPFS_DIRECTORY(nentries);
  276. if (objsize <= oldtdo->tdo_alloc)
  277. {
  278. /* Already big enough.
  279. * REVISIT: Missing logic to shrink directory objects.
  280. */
  281. oldtdo->tdo_nentries = nentries;
  282. return ret;
  283. }
  284. /* Added some additional amount to the new size to account frequent
  285. * reallocations.
  286. */
  287. objsize += CONFIG_FS_TMPFS_DIRECTORY_ALLOCGUARD;
  288. /* Realloc the directory object */
  289. newtdo = (FAR struct tmpfs_directory_s *)kmm_realloc(oldtdo, objsize);
  290. if (newtdo == NULL)
  291. {
  292. return -ENOMEM;
  293. }
  294. /* Adjust the reference in the parent directory entry */
  295. DEBUGASSERT(newtdo->tdo_dirent);
  296. newtdo->tdo_dirent->tde_object = (FAR struct tmpfs_object_s *)newtdo;
  297. /* Return the new address of the reallocated directory object */
  298. newtdo->tdo_alloc = objsize;
  299. newtdo->tdo_nentries = nentries;
  300. *tdo = newtdo;
  301. /* Adjust the reference in the parent directory entry */
  302. DEBUGASSERT(newtdo->tdo_dirent);
  303. newtdo->tdo_dirent->tde_object = (FAR struct tmpfs_object_s *)newtdo;
  304. /* Return the index to the first, newly allocated directory entry */
  305. return ret;
  306. }
  307. /****************************************************************************
  308. * Name: tmpfs_realloc_file
  309. ****************************************************************************/
  310. static int tmpfs_realloc_file(FAR struct tmpfs_file_s **tfo,
  311. size_t newsize)
  312. {
  313. FAR struct tmpfs_file_s *oldtfo = *tfo;
  314. FAR struct tmpfs_file_s *newtfo;
  315. size_t objsize;
  316. size_t allocsize;
  317. size_t delta;
  318. /* Check if the current allocation is sufficient */
  319. objsize = SIZEOF_TMPFS_FILE(newsize);
  320. /* Are we growing or shrinking the object? */
  321. if (objsize <= oldtfo->tfo_alloc)
  322. {
  323. /* Shrinking ... Shrink unconditionally if the size is shrinking to
  324. * zero.
  325. */
  326. if (newsize > 0)
  327. {
  328. /* Otherwise, don't realloc unless the object has shrunk by a
  329. * lot.
  330. */
  331. delta = oldtfo->tfo_alloc - objsize;
  332. if (delta <= CONFIG_FS_TMPFS_FILE_FREEGUARD)
  333. {
  334. /* Hasn't shrunk enough.. Return doing nothing for now */
  335. oldtfo->tfo_size = newsize;
  336. return OK;
  337. }
  338. }
  339. }
  340. /* Added some additional amount to the new size to account frequent
  341. * reallocations.
  342. */
  343. allocsize = objsize + CONFIG_FS_TMPFS_FILE_ALLOCGUARD;
  344. /* Realloc the file object */
  345. newtfo = (FAR struct tmpfs_file_s *)kmm_realloc(oldtfo, allocsize);
  346. if (newtfo == NULL)
  347. {
  348. return -ENOMEM;
  349. }
  350. /* Adjust the reference in the parent directory entry */
  351. DEBUGASSERT(newtfo->tfo_dirent);
  352. newtfo->tfo_dirent->tde_object = (FAR struct tmpfs_object_s *)newtfo;
  353. /* Return the new address of the reallocated file object */
  354. newtfo->tfo_alloc = allocsize;
  355. newtfo->tfo_size = newsize;
  356. *tfo = newtfo;
  357. return OK;
  358. }
  359. /****************************************************************************
  360. * Name: tmpfs_release_lockedobject
  361. ****************************************************************************/
  362. static void tmpfs_release_lockedobject(FAR struct tmpfs_object_s *to)
  363. {
  364. DEBUGASSERT(to && to->to_refs > 0);
  365. /* Is this a file object? */
  366. if (to->to_type == TMPFS_REGULAR)
  367. {
  368. tmpfs_release_lockedfile((FAR struct tmpfs_file_s *)to);
  369. }
  370. else
  371. {
  372. to->to_refs--;
  373. tmpfs_unlock_object(to);
  374. }
  375. }
  376. /****************************************************************************
  377. * Name: tmpfs_release_lockedfile
  378. ****************************************************************************/
  379. static void tmpfs_release_lockedfile(FAR struct tmpfs_file_s *tfo)
  380. {
  381. DEBUGASSERT(tfo && tfo->tfo_refs > 0);
  382. /* If there are no longer any references to the file and the file has been
  383. * unlinked from its parent directory, then free the file object now.
  384. */
  385. if (tfo->tfo_refs == 1 && (tfo->tfo_flags & TFO_FLAG_UNLINKED) != 0)
  386. {
  387. nxsem_destroy(&tfo->tfo_exclsem.ts_sem);
  388. kmm_free(tfo);
  389. }
  390. /* Otherwise, just decrement the reference count on the file object */
  391. else
  392. {
  393. tfo->tfo_refs--;
  394. tmpfs_unlock_file(tfo);
  395. }
  396. }
  397. /****************************************************************************
  398. * Name: tmpfs_find_dirent
  399. ****************************************************************************/
  400. static int tmpfs_find_dirent(FAR struct tmpfs_directory_s *tdo,
  401. FAR const char *name)
  402. {
  403. int i;
  404. /* Search the list of directory entries for a match */
  405. for (i = 0;
  406. i < tdo->tdo_nentries &&
  407. strcmp(tdo->tdo_entry[i].tde_name, name) != 0;
  408. i++);
  409. /* Return what we found, if anything */
  410. return i < tdo->tdo_nentries ? i : -ENOENT;
  411. }
  412. /****************************************************************************
  413. * Name: tmpfs_remove_dirent
  414. ****************************************************************************/
  415. static int tmpfs_remove_dirent(FAR struct tmpfs_directory_s *tdo,
  416. FAR const char *name)
  417. {
  418. int index;
  419. int last;
  420. /* Search the list of directory entries for a match */
  421. index = tmpfs_find_dirent(tdo, name);
  422. if (index < 0)
  423. {
  424. return index;
  425. }
  426. /* Free the object name */
  427. if (tdo->tdo_entry[index].tde_name != NULL)
  428. {
  429. kmm_free(tdo->tdo_entry[index].tde_name);
  430. }
  431. /* Remove by replacing this entry with the final directory entry */
  432. last = tdo->tdo_nentries - 1;
  433. if (index != last)
  434. {
  435. FAR struct tmpfs_dirent_s *newtde;
  436. FAR struct tmpfs_dirent_s *oldtde;
  437. FAR struct tmpfs_object_s *to;
  438. /* Move the directory entry */
  439. newtde = &tdo->tdo_entry[index];
  440. oldtde = &tdo->tdo_entry[last];
  441. to = oldtde->tde_object;
  442. newtde->tde_object = to;
  443. newtde->tde_name = oldtde->tde_name;
  444. /* Reset the backward link to the directory entry */
  445. to->to_dirent = newtde;
  446. }
  447. /* And decrement the count of directory entries */
  448. tdo->tdo_nentries = last;
  449. return OK;
  450. }
  451. /****************************************************************************
  452. * Name: tmpfs_add_dirent
  453. ****************************************************************************/
  454. static int tmpfs_add_dirent(FAR struct tmpfs_directory_s **tdo,
  455. FAR struct tmpfs_object_s *to,
  456. FAR const char *name)
  457. {
  458. FAR struct tmpfs_directory_s *oldtdo;
  459. FAR struct tmpfs_directory_s *newtdo;
  460. FAR struct tmpfs_dirent_s *tde;
  461. FAR char *newname;
  462. unsigned int nentries;
  463. int index;
  464. /* Copy the name string so that it will persist as long as the
  465. * directory entry.
  466. */
  467. newname = strdup(name);
  468. if (newname == NULL)
  469. {
  470. return -ENOMEM;
  471. }
  472. /* Get the new number of entries */
  473. oldtdo = *tdo;
  474. nentries = oldtdo->tdo_nentries + 1;
  475. /* Reallocate the directory object (if necessary) */
  476. index = tmpfs_realloc_directory(tdo, nentries);
  477. if (index < 0)
  478. {
  479. kmm_free(newname);
  480. return index;
  481. }
  482. /* Save the new object info in the new directory entry */
  483. newtdo = *tdo;
  484. tde = &newtdo->tdo_entry[index];
  485. tde->tde_object = to;
  486. tde->tde_name = newname;
  487. /* Add backward link to the directory entry to the object */
  488. to->to_dirent = tde;
  489. return OK;
  490. }
  491. /****************************************************************************
  492. * Name: tmpfs_alloc_file
  493. ****************************************************************************/
  494. static FAR struct tmpfs_file_s *tmpfs_alloc_file(void)
  495. {
  496. FAR struct tmpfs_file_s *tfo;
  497. size_t allocsize;
  498. /* Create a new zero length file object */
  499. allocsize = SIZEOF_TMPFS_FILE(CONFIG_FS_TMPFS_FILE_ALLOCGUARD);
  500. tfo = (FAR struct tmpfs_file_s *)kmm_malloc(allocsize);
  501. if (tfo == NULL)
  502. {
  503. return NULL;
  504. }
  505. /* Initialize the new file object. NOTE that the initial state is
  506. * locked with one reference count.
  507. */
  508. tfo->tfo_alloc = allocsize;
  509. tfo->tfo_type = TMPFS_REGULAR;
  510. tfo->tfo_refs = 1;
  511. tfo->tfo_flags = 0;
  512. tfo->tfo_size = 0;
  513. tfo->tfo_exclsem.ts_holder = getpid();
  514. tfo->tfo_exclsem.ts_count = 1;
  515. nxsem_init(&tfo->tfo_exclsem.ts_sem, 0, 0);
  516. return tfo;
  517. }
  518. /****************************************************************************
  519. * Name: tmpfs_create_file
  520. ****************************************************************************/
  521. static int tmpfs_create_file(FAR struct tmpfs_s *fs,
  522. FAR const char *relpath,
  523. FAR struct tmpfs_file_s **tfo)
  524. {
  525. FAR struct tmpfs_directory_s *parent;
  526. FAR struct tmpfs_file_s *newtfo;
  527. FAR char *copy;
  528. FAR char *name;
  529. int ret;
  530. /* Duplicate the path variable so that we can modify it */
  531. copy = strdup(relpath);
  532. if (copy == NULL)
  533. {
  534. return -ENOMEM;
  535. }
  536. /* Separate the path into the file name and the path to the parent
  537. * directory.
  538. */
  539. name = strrchr(copy, '/');
  540. if (name == NULL)
  541. {
  542. /* No subdirectories... use the root directory */
  543. name = copy;
  544. parent = (FAR struct tmpfs_directory_s *)fs->tfs_root.tde_object;
  545. /* Lock the root directory to emulate the behavior of tmpfs_find_directory() */
  546. tmpfs_lock_directory(parent);
  547. parent->tdo_refs++;
  548. }
  549. else
  550. {
  551. /* Terminate the parent directory path */
  552. *name++ = '\0';
  553. /* Locate the parent directory that should contain this name.
  554. * On success, tmpfs_find_directory() will lock the parent
  555. * directory and increment the reference count.
  556. */
  557. ret = tmpfs_find_directory(fs, copy, &parent, NULL);
  558. if (ret < 0)
  559. {
  560. goto errout_with_copy;
  561. }
  562. }
  563. /* Verify that no object of this name already exists in the directory */
  564. ret = tmpfs_find_dirent(parent, name);
  565. if (ret != -ENOENT)
  566. {
  567. /* Something with this name already exists in the directory.
  568. * OR perhaps some fatal error occurred.
  569. */
  570. if (ret >= 0)
  571. {
  572. ret = -EEXIST;
  573. }
  574. goto errout_with_parent;
  575. }
  576. /* Allocate an empty file. The initial state of the file is locked with one
  577. * reference count.
  578. */
  579. newtfo = tmpfs_alloc_file();
  580. if (newtfo == NULL)
  581. {
  582. ret = -ENOMEM;
  583. goto errout_with_parent;
  584. }
  585. /* Then add the new, empty file to the directory */
  586. ret = tmpfs_add_dirent(&parent, (FAR struct tmpfs_object_s *)newtfo, name);
  587. if (ret < 0)
  588. {
  589. goto errout_with_file;
  590. }
  591. /* Release the reference and lock on the parent directory */
  592. parent->tdo_refs--;
  593. tmpfs_unlock_directory(parent);
  594. /* Free the copy of the relpath and return success */
  595. kmm_free(copy);
  596. *tfo = newtfo;
  597. return OK;
  598. /* Error exits */
  599. errout_with_file:
  600. nxsem_destroy(&newtfo->tfo_exclsem.ts_sem);
  601. kmm_free(newtfo);
  602. errout_with_parent:
  603. parent->tdo_refs--;
  604. tmpfs_unlock_directory(parent);
  605. errout_with_copy:
  606. kmm_free(copy);
  607. return ret;
  608. }
  609. /****************************************************************************
  610. * Name: tmpfs_alloc_directory
  611. ****************************************************************************/
  612. static FAR struct tmpfs_directory_s *tmpfs_alloc_directory(void)
  613. {
  614. FAR struct tmpfs_directory_s *tdo;
  615. size_t allocsize;
  616. unsigned int nentries;
  617. /* Convert the pre-allocated memory to a number of directory entries */
  618. nentries = (CONFIG_FS_TMPFS_DIRECTORY_ALLOCGUARD +
  619. sizeof(struct tmpfs_dirent_s) - 1) /
  620. sizeof(struct tmpfs_dirent_s);
  621. /* Create a new zero length directory object */
  622. allocsize = SIZEOF_TMPFS_DIRECTORY(nentries);
  623. tdo = (FAR struct tmpfs_directory_s *)kmm_malloc(allocsize);
  624. if (tdo == NULL)
  625. {
  626. return NULL;
  627. }
  628. /* Initialize the new directory object */
  629. tdo->tdo_alloc = allocsize;
  630. tdo->tdo_type = TMPFS_DIRECTORY;
  631. tdo->tdo_refs = 0;
  632. tdo->tdo_nentries = 0;
  633. tdo->tdo_exclsem.ts_holder = TMPFS_NO_HOLDER;
  634. tdo->tdo_exclsem.ts_count = 0;
  635. nxsem_init(&tdo->tdo_exclsem.ts_sem, 0, 1);
  636. return tdo;
  637. }
  638. /****************************************************************************
  639. * Name: tmpfs_create_directory
  640. ****************************************************************************/
  641. static int tmpfs_create_directory(FAR struct tmpfs_s *fs,
  642. FAR const char *relpath,
  643. FAR struct tmpfs_directory_s **tdo)
  644. {
  645. FAR struct tmpfs_directory_s *parent;
  646. FAR struct tmpfs_directory_s *newtdo;
  647. FAR char *copy;
  648. FAR char *name;
  649. int ret;
  650. /* Duplicate the path variable so that we can modify it */
  651. copy = strdup(relpath);
  652. if (copy == NULL)
  653. {
  654. return -ENOMEM;
  655. }
  656. /* Separate the path into the file name and the path to the parent
  657. * directory.
  658. */
  659. name = strrchr(copy, '/');
  660. if (name == NULL)
  661. {
  662. /* No subdirectories... use the root directory */
  663. name = copy;
  664. parent = (FAR struct tmpfs_directory_s *)fs->tfs_root.tde_object;
  665. tmpfs_lock_directory(parent);
  666. parent->tdo_refs++;
  667. }
  668. else
  669. {
  670. /* Terminate the parent directory path */
  671. *name++ = '\0';
  672. /* Locate the parent directory that should contain this name.
  673. * On success, tmpfs_find_directory() will lockthe parent
  674. * directory and increment the reference count.
  675. */
  676. ret = tmpfs_find_directory(fs, copy, &parent, NULL);
  677. if (ret < 0)
  678. {
  679. goto errout_with_copy;
  680. }
  681. }
  682. /* Verify that no object of this name already exists in the directory */
  683. ret = tmpfs_find_dirent(parent, name);
  684. if (ret != -ENOENT)
  685. {
  686. /* Something with this name already exists in the directory.
  687. * OR perhaps some fatal error occurred.
  688. */
  689. if (ret >= 0)
  690. {
  691. ret = -EEXIST;
  692. }
  693. goto errout_with_parent;
  694. }
  695. /* Allocate an empty directory object. NOTE that there is no reference on
  696. * the new directory and the object is not locked.
  697. */
  698. newtdo = tmpfs_alloc_directory();
  699. if (newtdo == NULL)
  700. {
  701. ret = -ENOMEM;
  702. goto errout_with_parent;
  703. }
  704. /* Then add the new, empty file to the directory */
  705. ret = tmpfs_add_dirent(&parent, (FAR struct tmpfs_object_s *)newtdo, name);
  706. if (ret < 0)
  707. {
  708. goto errout_with_directory;
  709. }
  710. /* Free the copy of the relpath, release our reference to the parent directory,
  711. * and return success
  712. */
  713. parent->tdo_refs--;
  714. tmpfs_unlock_directory(parent);
  715. kmm_free(copy);
  716. /* Return the (unlocked, unreferenced) directory object to the caller */
  717. if (tdo != NULL)
  718. {
  719. *tdo = newtdo;
  720. }
  721. return OK;
  722. /* Error exits */
  723. errout_with_directory:
  724. nxsem_destroy(&newtdo->tdo_exclsem.ts_sem);
  725. kmm_free(newtdo);
  726. errout_with_parent:
  727. parent->tdo_refs--;
  728. tmpfs_unlock_directory(parent);
  729. errout_with_copy:
  730. kmm_free(copy);
  731. return ret;
  732. }
  733. /****************************************************************************
  734. * Name: tmpfs_find_object
  735. ****************************************************************************/
  736. static int tmpfs_find_object(FAR struct tmpfs_s *fs,
  737. FAR const char *relpath,
  738. FAR struct tmpfs_object_s **object,
  739. FAR struct tmpfs_directory_s **parent)
  740. {
  741. FAR struct tmpfs_object_s *to = NULL;
  742. FAR struct tmpfs_directory_s *tdo = NULL;
  743. FAR struct tmpfs_directory_s *next_tdo;
  744. FAR char *segment;
  745. FAR char *next_segment;
  746. FAR char *tkptr;
  747. FAR char *copy;
  748. int index;
  749. /* Make a copy of the path (so that we can modify it via strtok) */
  750. copy = strdup(relpath);
  751. if (copy == NULL)
  752. {
  753. return -ENOMEM;
  754. }
  755. /* Traverse the file system for any object with the matching name */
  756. to = fs->tfs_root.tde_object;
  757. next_tdo = (FAR struct tmpfs_directory_s *)fs->tfs_root.tde_object;
  758. for (segment = strtok_r(copy, "/", &tkptr);
  759. segment != NULL;
  760. segment = next_segment)
  761. {
  762. /* Get the next segment after the one we are currently working on.
  763. * This will be NULL is we are working on the final segment of the
  764. * relpath.
  765. */
  766. next_segment = strtok_r(NULL, "/", &tkptr);
  767. /* Search the next directory. */
  768. tdo = next_tdo;
  769. /* Find the TMPFS object with the next segment name in the current
  770. * directory.
  771. */
  772. index = tmpfs_find_dirent(tdo, segment);
  773. if (index < 0)
  774. {
  775. /* No object with this name exists in the directory. */
  776. kmm_free(copy);
  777. return index;
  778. }
  779. to = tdo->tdo_entry[index].tde_object;
  780. /* Is this object another directory? */
  781. if (to->to_type != TMPFS_DIRECTORY)
  782. {
  783. /* No. Was this the final segment in the path? */
  784. if (next_segment == NULL)
  785. {
  786. /* Then we can break out of the loop now */
  787. break;
  788. }
  789. /* No, this was not the final segement of the relpath.
  790. * We cannot continue the search if any of the intermediate
  791. * segments do no correspond to directories.
  792. */
  793. kmm_free(copy);
  794. return -ENOTDIR;
  795. }
  796. /* Search this directory for the next segement. If we
  797. * exit the loop, tdo will still refer to the parent
  798. * directory of to.
  799. */
  800. next_tdo = (FAR struct tmpfs_directory_s *)to;
  801. }
  802. /* When we exit this loop (successfully), to will point to the TMPFS
  803. * object associated with the terminal segment of the relpath.
  804. * Increment the reference count on the located object.
  805. */
  806. /* Free the dup'ed string */
  807. kmm_free(copy);
  808. /* Return what we found */
  809. if (parent)
  810. {
  811. if (tdo != NULL)
  812. {
  813. /* Get exclusive access to the parent and increment the reference
  814. * count on the object.
  815. */
  816. tmpfs_lock_directory(tdo);
  817. tdo->tdo_refs++;
  818. }
  819. *parent = tdo;
  820. }
  821. if (object)
  822. {
  823. if (to != NULL)
  824. {
  825. /* Get exclusive access to the object and increment the reference
  826. * count on the object.
  827. */
  828. tmpfs_lock_object(to);
  829. to->to_refs++;
  830. }
  831. *object = to;
  832. }
  833. return OK;
  834. }
  835. /****************************************************************************
  836. * Name: tmpfs_find_file
  837. ****************************************************************************/
  838. static int tmpfs_find_file(FAR struct tmpfs_s *fs,
  839. FAR const char *relpath,
  840. FAR struct tmpfs_file_s **tfo,
  841. FAR struct tmpfs_directory_s **parent)
  842. {
  843. FAR struct tmpfs_object_s *to;
  844. int ret;
  845. /* Find the object at this path. If successful, tmpfs_find_object() will
  846. * lock both the object and the parent directory and will increment the
  847. * reference count on both.
  848. */
  849. ret = tmpfs_find_object(fs, relpath, &to, parent);
  850. if (ret >= 0)
  851. {
  852. /* We found it... but is it a regular file? */
  853. if (to->to_type != TMPFS_REGULAR)
  854. {
  855. /* No... unlock the object and its parent and return an error */
  856. tmpfs_release_lockedobject(to);
  857. if (parent)
  858. {
  859. FAR struct tmpfs_directory_s *tdo = *parent;
  860. tdo->tdo_refs--;
  861. tmpfs_unlock_directory(tdo);
  862. }
  863. ret = -EISDIR;
  864. }
  865. /* Return the verified file object */
  866. *tfo = (FAR struct tmpfs_file_s *)to;
  867. }
  868. return ret;
  869. }
  870. /****************************************************************************
  871. * Name: tmpfs_find_directory
  872. ****************************************************************************/
  873. static int tmpfs_find_directory(FAR struct tmpfs_s *fs,
  874. FAR const char *relpath,
  875. FAR struct tmpfs_directory_s **tdo,
  876. FAR struct tmpfs_directory_s **parent)
  877. {
  878. FAR struct tmpfs_object_s *to;
  879. int ret;
  880. /* Find the object at this path */
  881. ret = tmpfs_find_object(fs, relpath, &to, parent);
  882. if (ret >= 0)
  883. {
  884. /* We found it... but is it a regular file? */
  885. if (to->to_type != TMPFS_DIRECTORY)
  886. {
  887. /* No... unlock the object and its parent and return an error */
  888. tmpfs_release_lockedobject(to);
  889. if (parent)
  890. {
  891. FAR struct tmpfs_directory_s *tmptdo = *parent;
  892. tmptdo->tdo_refs--;
  893. tmpfs_unlock_directory(tmptdo);
  894. }
  895. ret = -ENOTDIR;
  896. }
  897. /* Return the verified file object */
  898. *tdo = (FAR struct tmpfs_directory_s *)to;
  899. }
  900. return ret;
  901. }
  902. /****************************************************************************
  903. * Name: tmpfs_statfs_callout
  904. ****************************************************************************/
  905. static int tmpfs_statfs_callout(FAR struct tmpfs_directory_s *tdo,
  906. unsigned int index, FAR void *arg)
  907. {
  908. FAR struct tmpfs_object_s *to;
  909. FAR struct tmpfs_statfs_s *tmpbuf;
  910. DEBUGASSERT(tdo != NULL && arg != NULL && index < tdo->tdo_nentries);
  911. to = tdo->tdo_entry[index].tde_object;
  912. tmpbuf = (FAR struct tmpfs_statfs_s *)arg;
  913. DEBUGASSERT(to != NULL);
  914. /* Accumulate statistics. Save the total memory allocted for this object. */
  915. tmpbuf->tsf_alloc += to->to_alloc;
  916. /* Is this directory entry a file object? */
  917. if (to->to_type == TMPFS_REGULAR)
  918. {
  919. FAR struct tmpfs_file_s *tmptfo;
  920. /* It is a file object. Increment the number of files and update the
  921. * amount of memory in use.
  922. */
  923. tmptfo = (FAR struct tmpfs_file_s *)to;
  924. tmpbuf->tsf_inuse += tmptfo->tfo_size;
  925. tmpbuf->tsf_files++;
  926. }
  927. else /* if (to->to_type == TMPFS_DIRECTORY) */
  928. {
  929. FAR struct tmpfs_directory_s *tmptdo;
  930. size_t inuse;
  931. size_t avail;
  932. /* It is a directory object. Update the amount of memory in use
  933. * for the directory and estimate the number of free directory nodes.
  934. */
  935. tmptdo = (FAR struct tmpfs_directory_s *)to;
  936. inuse = SIZEOF_TMPFS_DIRECTORY(tmptdo->tdo_nentries);
  937. avail = tmptdo->tdo_alloc - inuse;
  938. tmpbuf->tsf_inuse += inuse;
  939. tmpbuf->tsf_ffree += avail / sizeof(struct tmpfs_dirent_s);
  940. }
  941. return TMPFS_CONTINUE;
  942. }
  943. /****************************************************************************
  944. * Name: tmpfs_free_callout
  945. ****************************************************************************/
  946. static int tmpfs_free_callout(FAR struct tmpfs_directory_s *tdo,
  947. unsigned int index, FAR void *arg)
  948. {
  949. FAR struct tmpfs_dirent_s *tde;
  950. FAR struct tmpfs_object_s *to;
  951. FAR struct tmpfs_file_s *tfo;
  952. unsigned int last;
  953. /* Free the object name */
  954. if (tdo->tdo_entry[index].tde_name != NULL)
  955. {
  956. kmm_free(tdo->tdo_entry[index].tde_name);
  957. }
  958. /* Remove by replacing this entry with the final directory entry */
  959. tde = &tdo->tdo_entry[index];
  960. to = tde->tde_object;
  961. last = tdo->tdo_nentries - 1;
  962. if (index != last)
  963. {
  964. FAR struct tmpfs_dirent_s *oldtde;
  965. FAR struct tmpfs_object_s *oldto;
  966. /* Move the directory entry */
  967. oldtde = &tdo->tdo_entry[last];
  968. oldto = oldtde->tde_object;
  969. tde->tde_object = oldto;
  970. tde->tde_name = oldtde->tde_name;
  971. /* Reset the backward link to the directory entry */
  972. oldto->to_dirent = tde;
  973. }
  974. /* And decrement the count of directory entries */
  975. tdo->tdo_nentries = last;
  976. /* Is this directory entry a file object? */
  977. if (to->to_type == TMPFS_REGULAR)
  978. {
  979. tfo = (FAR struct tmpfs_file_s *)to;
  980. /* Are there references to the file? */
  981. if (tfo->tfo_refs > 0)
  982. {
  983. /* Yes.. We cannot delete the file now. Just mark it as unlinked. */
  984. tfo->tfo_flags |= TFO_FLAG_UNLINKED;
  985. return TMPFS_UNLINKED;
  986. }
  987. }
  988. /* Free the object now */
  989. nxsem_destroy(&to->to_exclsem.ts_sem);
  990. kmm_free(to);
  991. return TMPFS_DELETED;
  992. }
  993. /****************************************************************************
  994. * Name: tmpfs_foreach
  995. ****************************************************************************/
  996. static int tmpfs_foreach(FAR struct tmpfs_directory_s *tdo,
  997. tmpfs_foreach_t callout, FAR void *arg)
  998. {
  999. FAR struct tmpfs_object_s *to;
  1000. unsigned int index;
  1001. int ret;
  1002. /* Visit each directory entry */
  1003. for (index = 0; index < tdo->tdo_nentries; )
  1004. {
  1005. /* Lock the object and take a reference */
  1006. to = tdo->tdo_entry[index].tde_object;
  1007. tmpfs_lock_object(to);
  1008. to->to_refs++;
  1009. /* Is the next entry a directory? */
  1010. if (to->to_type == TMPFS_DIRECTORY)
  1011. {
  1012. FAR struct tmpfs_directory_s *next =
  1013. (FAR struct tmpfs_directory_s *)to;
  1014. /* Yes.. traverse its children first in the case the final
  1015. * action will be to delete the directory.
  1016. */
  1017. ret = tmpfs_foreach(next, tmpfs_free_callout, NULL);
  1018. if (ret < 0)
  1019. {
  1020. return -ECANCELED;
  1021. }
  1022. }
  1023. /* Perform the callout */
  1024. ret = callout(tdo, index, arg);
  1025. switch (ret)
  1026. {
  1027. case TMPFS_CONTINUE: /* Continue enumeration */
  1028. /* Release the object and index to the next entry */
  1029. tmpfs_release_lockedobject(to);
  1030. index++;
  1031. break;
  1032. case TMPFS_HALT: /* Stop enumeration */
  1033. /* Release the object and cancel the traversal */
  1034. tmpfs_release_lockedobject(to);
  1035. return -ECANCELED;
  1036. case TMPFS_UNLINKED: /* Only the directory entry was deleted */
  1037. /* Release the object and continue with the same index */
  1038. tmpfs_release_lockedobject(to);
  1039. case TMPFS_DELETED: /* Object and directory entry deleted */
  1040. break; /* Continue with the same index */
  1041. }
  1042. }
  1043. return OK;
  1044. }
  1045. /****************************************************************************
  1046. * Name: tmpfs_open
  1047. ****************************************************************************/
  1048. static int tmpfs_open(FAR struct file *filep, FAR const char *relpath,
  1049. int oflags, mode_t mode)
  1050. {
  1051. FAR struct inode *inode;
  1052. FAR struct tmpfs_s *fs;
  1053. FAR struct tmpfs_file_s *tfo;
  1054. off_t offset;
  1055. int ret;
  1056. finfo("filep: %p\n", filep);
  1057. DEBUGASSERT(filep->f_priv == NULL && filep->f_inode != NULL);
  1058. /* Get the mountpoint inode reference from the file structure and the
  1059. * mountpoint private data from the inode structure
  1060. */
  1061. inode = filep->f_inode;
  1062. fs = inode->i_private;
  1063. DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
  1064. /* Get exclusive access to the file system */
  1065. tmpfs_lock(fs);
  1066. /* Skip over any leading directory separators (shouldn't be any) */
  1067. for (; *relpath == '/'; relpath++);
  1068. /* Find the file object associated with this relative path.
  1069. * If successful, this action will lock both the parent directory and
  1070. * the file object, adding one to the reference count of both.
  1071. * In the event that -ENOENT, there will still be a reference and
  1072. * lock on the returned directory.
  1073. */
  1074. ret = tmpfs_find_file(fs, relpath, &tfo, NULL);
  1075. if (ret >= 0)
  1076. {
  1077. /* The file exists. We hold the lock and one reference count
  1078. * on the file object.
  1079. *
  1080. * It would be an error if we are asked to create it exclusively
  1081. */
  1082. if ((oflags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL))
  1083. {
  1084. /* Already exists -- can't create it exclusively */
  1085. ret = -EEXIST;
  1086. goto errout_with_filelock;
  1087. }
  1088. /* Check if the caller has sufficient privileges to open the file.
  1089. * REVISIT: No file protection implemented
  1090. */
  1091. /* If O_TRUNC is specified and the file is opened for writing,
  1092. * then truncate the file. This operation requires that the file is
  1093. * writeable, but we have already checked that. O_TRUNC without write
  1094. * access is ignored.
  1095. */
  1096. if ((oflags & (O_TRUNC | O_WRONLY)) == (O_TRUNC | O_WRONLY))
  1097. {
  1098. /* Truncate the file to zero length (if it is not already
  1099. * zero length)
  1100. */
  1101. if (tfo->tfo_size > 0)
  1102. {
  1103. ret = tmpfs_realloc_file(&tfo, 0);
  1104. if (ret < 0)
  1105. {
  1106. goto errout_with_filelock;
  1107. }
  1108. }
  1109. }
  1110. }
  1111. /* ENOENT would be returned by tmpfs_find_file() if the full directory
  1112. * path was found, but the file was not found in the final directory.
  1113. */
  1114. else if (ret == -ENOENT)
  1115. {
  1116. /* The file does not exist. Were we asked to create it? */
  1117. if ((oflags & O_CREAT) == 0)
  1118. {
  1119. /* No.. then we fail with -ENOENT */
  1120. ret = -ENOENT;
  1121. goto errout_with_fslock;
  1122. }
  1123. /* Yes.. create the file object. There will be a reference and a lock
  1124. * on the new file object.
  1125. */
  1126. ret = tmpfs_create_file(fs, relpath, &tfo);
  1127. if (ret < 0)
  1128. {
  1129. goto errout_with_fslock;
  1130. }
  1131. }
  1132. /* Some other error occurred */
  1133. else
  1134. {
  1135. goto errout_with_fslock;
  1136. }
  1137. /* Save the struct tmpfs_file_s instance as the file private data */
  1138. filep->f_priv = tfo;
  1139. /* In write/append mode, we need to set the file pointer to the end of the
  1140. * file.
  1141. */
  1142. offset = 0;
  1143. if ((oflags & (O_APPEND | O_WRONLY)) == (O_APPEND | O_WRONLY))
  1144. {
  1145. offset = tfo->tfo_size;
  1146. }
  1147. filep->f_pos = offset;
  1148. /* Unlock the file file object, but retain the reference count */
  1149. tmpfs_unlock_file(tfo);
  1150. tmpfs_unlock(fs);
  1151. return OK;
  1152. /* Error exits */
  1153. errout_with_filelock:
  1154. tmpfs_release_lockedfile(tfo);
  1155. errout_with_fslock:
  1156. tmpfs_unlock(fs);
  1157. return ret;
  1158. }
  1159. /****************************************************************************
  1160. * Name: tmpfs_close
  1161. ****************************************************************************/
  1162. static int tmpfs_close(FAR struct file *filep)
  1163. {
  1164. FAR struct tmpfs_file_s *tfo;
  1165. finfo("filep: %p\n", filep);
  1166. DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
  1167. /* Recover our private data from the struct file instance */
  1168. tfo = filep->f_priv;
  1169. /* Get exclusive access to the file */
  1170. tmpfs_lock_file(tfo);
  1171. /* Decrement the reference count on the file */
  1172. DEBUGASSERT(tfo->tfo_refs > 0);
  1173. if (tfo->tfo_refs > 0)
  1174. {
  1175. tfo->tfo_refs--;
  1176. }
  1177. filep->f_priv = NULL;
  1178. /* If the reference count decremented to zero and the file has been
  1179. * unlinked, then free the file allocation now.
  1180. */
  1181. if (tfo->tfo_refs == 0 && (tfo->tfo_flags & TFO_FLAG_UNLINKED) != 0)
  1182. {
  1183. /* Free the file object while we hold the lock? Weird but this
  1184. * should be safe because the object is unlinked and could not
  1185. * have any other references.
  1186. */
  1187. kmm_free(tfo);
  1188. return OK;
  1189. }
  1190. /* Release the lock on the file */
  1191. tmpfs_unlock_file(tfo);
  1192. return OK;
  1193. }
  1194. /****************************************************************************
  1195. * Name: tmpfs_read
  1196. ****************************************************************************/
  1197. static ssize_t tmpfs_read(FAR struct file *filep, FAR char *buffer,
  1198. size_t buflen)
  1199. {
  1200. FAR struct tmpfs_file_s *tfo;
  1201. ssize_t nread;
  1202. off_t startpos;
  1203. off_t endpos;
  1204. finfo("filep: %p buffer: %p buflen: %lu\n",
  1205. filep, buffer, (unsigned long)buflen);
  1206. DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
  1207. /* Recover our private data from the struct file instance */
  1208. tfo = filep->f_priv;
  1209. /* Get exclusive access to the file */
  1210. tmpfs_lock_file(tfo);
  1211. /* Handle attempts to read beyond the end of the file. */
  1212. startpos = filep->f_pos;
  1213. nread = buflen;
  1214. endpos = startpos + buflen;
  1215. if (endpos > tfo->tfo_size)
  1216. {
  1217. endpos = tfo->tfo_size;
  1218. nread = endpos - startpos;
  1219. }
  1220. /* Copy data from the memory object to the user buffer */
  1221. memcpy(buffer, &tfo->tfo_data[startpos], nread);
  1222. filep->f_pos += nread;
  1223. /* Release the lock on the file */
  1224. tmpfs_unlock_file(tfo);
  1225. return nread;
  1226. }
  1227. /****************************************************************************
  1228. * Name: tmpfs_write
  1229. ****************************************************************************/
  1230. static ssize_t tmpfs_write(FAR struct file *filep, FAR const char *buffer,
  1231. size_t buflen)
  1232. {
  1233. FAR struct tmpfs_file_s *tfo;
  1234. ssize_t nwritten;
  1235. off_t startpos;
  1236. off_t endpos;
  1237. int ret;
  1238. finfo("filep: %p buffer: %p buflen: %lu\n",
  1239. filep, buffer, (unsigned long)buflen);
  1240. DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
  1241. /* Recover our private data from the struct file instance */
  1242. tfo = filep->f_priv;
  1243. /* Get exclusive access to the file */
  1244. tmpfs_lock_file(tfo);
  1245. /* Handle attempts to write beyond the end of the file */
  1246. startpos = filep->f_pos;
  1247. nwritten = buflen;
  1248. endpos = startpos + buflen;
  1249. if (endpos > tfo->tfo_size)
  1250. {
  1251. /* Reallocate the file to handle the write past the end of the file. */
  1252. ret = tmpfs_realloc_file(&tfo, (size_t)endpos);
  1253. if (ret < 0)
  1254. {
  1255. goto errout_with_lock;
  1256. }
  1257. filep->f_priv = tfo;
  1258. }
  1259. /* Copy data from the memory object to the user buffer */
  1260. memcpy(&tfo->tfo_data[startpos], buffer, nwritten);
  1261. filep->f_pos += nwritten;
  1262. /* Release the lock on the file */
  1263. tmpfs_unlock_file(tfo);
  1264. return nwritten;
  1265. errout_with_lock:
  1266. tmpfs_unlock_file(tfo);
  1267. return (ssize_t)ret;
  1268. }
  1269. /****************************************************************************
  1270. * Name: tmpfs_seek
  1271. ****************************************************************************/
  1272. static off_t tmpfs_seek(FAR struct file *filep, off_t offset, int whence)
  1273. {
  1274. FAR struct tmpfs_file_s *tfo;
  1275. off_t position;
  1276. finfo("filep: %p\n", filep);
  1277. DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
  1278. /* Recover our private data from the struct file instance */
  1279. tfo = filep->f_priv;
  1280. /* Map the offset according to the whence option */
  1281. switch (whence)
  1282. {
  1283. case SEEK_SET: /* The offset is set to offset bytes. */
  1284. position = offset;
  1285. break;
  1286. case SEEK_CUR: /* The offset is set to its current location plus
  1287. * offset bytes. */
  1288. position = offset + filep->f_pos;
  1289. break;
  1290. case SEEK_END: /* The offset is set to the size of the file plus
  1291. * offset bytes. */
  1292. position = offset + tfo->tfo_size;
  1293. break;
  1294. default:
  1295. return -EINVAL;
  1296. }
  1297. /* Attempts to set the position beyond the end of file will
  1298. * work if the file is open for write access.
  1299. *
  1300. * REVISIT: This simple implementation has no per-open storage that
  1301. * would be needed to retain the open flags.
  1302. */
  1303. #if 0
  1304. if (position > tfo->tfo_size && (tfo->tfo_oflags & O_WROK) == 0)
  1305. {
  1306. /* Otherwise, the position is limited to the file size */
  1307. position = tfo->tfo_size;
  1308. }
  1309. #endif
  1310. /* Save the new file position */
  1311. filep->f_pos = position;
  1312. return position;
  1313. }
  1314. /****************************************************************************
  1315. * Name: tmpfs_ioctl
  1316. ****************************************************************************/
  1317. static int tmpfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
  1318. {
  1319. FAR struct tmpfs_file_s *tfo;
  1320. FAR void **ppv = (FAR void**)arg;
  1321. finfo("filep: %p cmd: %d arg: %08lx\n", filep, cmd, arg);
  1322. DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
  1323. /* Recover our private data from the struct file instance */
  1324. tfo = filep->f_priv;
  1325. DEBUGASSERT(tfo != NULL);
  1326. /* Only one ioctl command is supported */
  1327. if (cmd == FIOC_MMAP && ppv != NULL)
  1328. {
  1329. /* Return the address on the media corresponding to the start of
  1330. * the file.
  1331. */
  1332. *ppv = (FAR void *)tfo->tfo_data;
  1333. return OK;
  1334. }
  1335. ferr("ERROR: Invalid cmd: %d\n", cmd);
  1336. return -ENOTTY;
  1337. }
  1338. /****************************************************************************
  1339. * Name: tmpfs_dup
  1340. ****************************************************************************/
  1341. static int tmpfs_dup(FAR const struct file *oldp, FAR struct file *newp)
  1342. {
  1343. FAR struct tmpfs_file_s *tfo;
  1344. finfo("Dup %p->%p\n", oldp, newp);
  1345. DEBUGASSERT(oldp->f_priv != NULL && oldp->f_inode != NULL &&
  1346. newp->f_priv == NULL && newp->f_inode != NULL);
  1347. /* Recover our private data from the struct file instance */
  1348. tfo = oldp->f_priv;
  1349. DEBUGASSERT(tfo != NULL);
  1350. /* Increment the reference count (atomically) */
  1351. tmpfs_lock_file(tfo);
  1352. tfo->tfo_refs++;
  1353. tmpfs_unlock_file(tfo);
  1354. /* Save a copy of the file object as the dup'ed file. This
  1355. * simple implementation does not many any per-open data
  1356. * structures so there is not really much to the dup operation.
  1357. */
  1358. newp->f_priv = tfo;
  1359. return OK;
  1360. }
  1361. /****************************************************************************
  1362. * Name: tmpfs_fstat
  1363. *
  1364. * Description:
  1365. * Obtain information about an open file associated with the file
  1366. * descriptor 'fd', and will write it to the area pointed to by 'buf'.
  1367. *
  1368. ****************************************************************************/
  1369. static int tmpfs_fstat(FAR const struct file *filep, FAR struct stat *buf)
  1370. {
  1371. FAR struct tmpfs_file_s *tfo;
  1372. finfo("Fstat %p\n", buf);
  1373. DEBUGASSERT(filep != NULL && buf != NULL);
  1374. /* Recover our private data from the struct file instance */
  1375. DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL);
  1376. tfo = filep->f_priv;
  1377. /* Get exclusive access to the file */
  1378. tmpfs_lock_file(tfo);
  1379. /* Return information about the file in the stat buffer. */
  1380. tmpfs_stat_common((FAR struct tmpfs_object_s *)tfo, buf);
  1381. /* Release the lock on the file and return success. */
  1382. tmpfs_unlock_file(tfo);
  1383. return OK;
  1384. }
  1385. /****************************************************************************
  1386. * Name: tmpfs_truncate
  1387. ****************************************************************************/
  1388. static int tmpfs_truncate(FAR struct file *filep, off_t length)
  1389. {
  1390. FAR struct tmpfs_file_s *tfo;
  1391. size_t oldsize;
  1392. int ret = OK;
  1393. finfo("filep: %p length: %ld\n", filep, (long)length);
  1394. DEBUGASSERT(filep != NULL && length >= 0);
  1395. /* Recover our private data from the struct file instance */
  1396. tfo = filep->f_priv;
  1397. /* Get exclusive access to the file */
  1398. tmpfs_lock_file(tfo);
  1399. /* Get the old size of the file. Do nothing if the file size is not
  1400. * changing.
  1401. */
  1402. oldsize = tfo->tfo_size;
  1403. if (oldsize != length)
  1404. {
  1405. /* The size is changing.. up or down. Reallocate the file memory. */
  1406. ret = tmpfs_realloc_file(&tfo, (size_t)length);
  1407. if (ret < 0)
  1408. {
  1409. goto errout_with_lock;
  1410. }
  1411. filep->f_priv = tfo;
  1412. /* If the size has increased, then we need to zero the newly added
  1413. * memory.
  1414. */
  1415. if (length > oldsize)
  1416. {
  1417. memset(&tfo->tfo_data[oldsize], 0, length - oldsize);
  1418. }
  1419. ret = OK;
  1420. }
  1421. /* Release the lock on the file */
  1422. errout_with_lock:
  1423. tmpfs_unlock_file(tfo);
  1424. return ret;
  1425. }
  1426. /****************************************************************************
  1427. * Name: tmpfs_opendir
  1428. ****************************************************************************/
  1429. static int tmpfs_opendir(FAR struct inode *mountpt, FAR const char *relpath,
  1430. FAR struct fs_dirent_s *dir)
  1431. {
  1432. FAR struct tmpfs_s *fs;
  1433. FAR struct tmpfs_directory_s *tdo;
  1434. int ret;
  1435. finfo("mountpt: %p relpath: %s dir: %p\n",
  1436. mountpt, relpath, dir);
  1437. DEBUGASSERT(mountpt != NULL && relpath != NULL && dir != NULL);
  1438. /* Get the mountpoint private data from the inode structure */
  1439. fs = mountpt->i_private;
  1440. DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
  1441. /* Get exclusive access to the file system */
  1442. tmpfs_lock(fs);
  1443. /* Skip over any leading directory separators (shouldn't be any) */
  1444. for (; *relpath == '/'; relpath++);
  1445. /* Find the directory object associated with this relative path.
  1446. * If successful, this action will lock both the parent directory and
  1447. * the file object, adding one to the reference count of both.
  1448. * In the event that -ENOENT, there will still be a reference and
  1449. * lock on the returned directory.
  1450. */
  1451. ret = tmpfs_find_directory(fs, relpath, &tdo, NULL);
  1452. if (ret >= 0)
  1453. {
  1454. dir->u.tmpfs.tf_tdo = tdo;
  1455. dir->u.tmpfs.tf_index = 0;
  1456. tmpfs_unlock_directory(tdo);
  1457. }
  1458. /* Release the lock on the file system and return the result */
  1459. tmpfs_unlock(fs);
  1460. return ret;
  1461. }
  1462. /****************************************************************************
  1463. * Name: tmpfs_closedir
  1464. ****************************************************************************/
  1465. static int tmpfs_closedir(FAR struct inode *mountpt,
  1466. FAR struct fs_dirent_s *dir)
  1467. {
  1468. FAR struct tmpfs_directory_s *tdo;
  1469. finfo("mountpt: %p dir: %p\n", mountpt, dir);
  1470. DEBUGASSERT(mountpt != NULL && dir != NULL);
  1471. /* Get the directory structure from the dir argument */
  1472. tdo = dir->u.tmpfs.tf_tdo;
  1473. DEBUGASSERT(tdo != NULL);
  1474. /* Decrement the reference count on the directory object */
  1475. tmpfs_lock_directory(tdo);
  1476. tdo->tdo_refs--;
  1477. tmpfs_unlock_directory(tdo);
  1478. return OK;
  1479. }
  1480. /****************************************************************************
  1481. * Name: tmpfs_readdir
  1482. ****************************************************************************/
  1483. static int tmpfs_readdir(FAR struct inode *mountpt,
  1484. FAR struct fs_dirent_s *dir)
  1485. {
  1486. FAR struct tmpfs_directory_s *tdo;
  1487. unsigned int index;
  1488. int ret;
  1489. finfo("mountpt: %p dir: %p\n", mountpt, dir);
  1490. DEBUGASSERT(mountpt != NULL && dir != NULL);
  1491. /* Get the directory structure from the dir argument and lock it */
  1492. tdo = dir->u.tmpfs.tf_tdo;
  1493. DEBUGASSERT(tdo != NULL);
  1494. tmpfs_lock_directory(tdo);
  1495. /* Have we reached the end of the directory? */
  1496. index = dir->u.tmpfs.tf_index;
  1497. if (index >= tdo->tdo_nentries)
  1498. {
  1499. /* We signal the end of the directory by returning the special error:
  1500. * -ENOENT
  1501. */
  1502. finfo("End of directory\n");
  1503. ret = -ENOENT;
  1504. }
  1505. else
  1506. {
  1507. FAR struct tmpfs_dirent_s *tde;
  1508. FAR struct tmpfs_object_s *to;
  1509. /* Does this entry refer to a file or a directory object? */
  1510. tde = &tdo->tdo_entry[index];
  1511. to = tde->tde_object;
  1512. DEBUGASSERT(to != NULL);
  1513. if (to->to_type == TMPFS_DIRECTORY)
  1514. {
  1515. /* A directory */
  1516. dir->fd_dir.d_type = DTYPE_DIRECTORY;
  1517. }
  1518. else /* to->to_type == TMPFS_REGULAR) */
  1519. {
  1520. /* A regular file */
  1521. dir->fd_dir.d_type = DTYPE_FILE;
  1522. }
  1523. /* Copy the entry name */
  1524. strncpy(dir->fd_dir.d_name, tde->tde_name, NAME_MAX + 1);
  1525. /* Increment the index for next time */
  1526. dir->u.tmpfs.tf_index = index + 1;
  1527. ret = OK;
  1528. }
  1529. tmpfs_unlock_directory(tdo);
  1530. return ret;
  1531. }
  1532. /****************************************************************************
  1533. * Name: tmpfs_rewinddir
  1534. ****************************************************************************/
  1535. static int tmpfs_rewinddir(FAR struct inode *mountpt,
  1536. FAR struct fs_dirent_s *dir)
  1537. {
  1538. finfo("mountpt: %p dir: %p\n", mountpt, dir);
  1539. DEBUGASSERT(mountpt != NULL && dir != NULL);
  1540. /* Set the readdir index to zero */
  1541. dir->u.tmpfs.tf_index = 0;
  1542. return OK;
  1543. }
  1544. /****************************************************************************
  1545. * Name: tmpfs_bind
  1546. ****************************************************************************/
  1547. static int tmpfs_bind(FAR struct inode *blkdriver, FAR const void *data,
  1548. FAR void **handle)
  1549. {
  1550. FAR struct tmpfs_directory_s *tdo;
  1551. FAR struct tmpfs_s *fs;
  1552. finfo("blkdriver: %p data: %p handle: %p\n", blkdriver, data, handle);
  1553. DEBUGASSERT(blkdriver == NULL && handle != NULL);
  1554. /* Create an instance of the tmpfs file system */
  1555. fs = (FAR struct tmpfs_s *)kmm_zalloc(sizeof(struct tmpfs_s));
  1556. if (fs == NULL)
  1557. {
  1558. return -ENOMEM;
  1559. }
  1560. /* Create a root file system. This is like a single directory entry in
  1561. * the file system structure.
  1562. */
  1563. tdo = tmpfs_alloc_directory();
  1564. if (tdo == NULL)
  1565. {
  1566. kmm_free(fs);
  1567. return -ENOMEM;
  1568. }
  1569. fs->tfs_root.tde_object = (FAR struct tmpfs_object_s *)tdo;
  1570. fs->tfs_root.tde_name = "";
  1571. /* Set up the backward link (to support reallocation) */
  1572. tdo->tdo_dirent = &fs->tfs_root;
  1573. /* Initialize the file system state */
  1574. fs->tfs_exclsem.ts_holder = TMPFS_NO_HOLDER;
  1575. fs->tfs_exclsem.ts_count = 0;
  1576. nxsem_init(&fs->tfs_exclsem.ts_sem, 0, 1);
  1577. /* Return the new file system handle */
  1578. *handle = (FAR void *)fs;
  1579. return OK;
  1580. }
  1581. /****************************************************************************
  1582. * Name: tmpfs_unbind
  1583. ****************************************************************************/
  1584. static int tmpfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
  1585. unsigned int flags)
  1586. {
  1587. FAR struct tmpfs_s *fs = (FAR struct tmpfs_s *)handle;
  1588. FAR struct tmpfs_directory_s *tdo;
  1589. int ret;
  1590. finfo("handle: %p blkdriver: %p flags: %02x\n",
  1591. handle, blkdriver, flags);
  1592. DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
  1593. /* Lock the file system */
  1594. tmpfs_lock(fs);
  1595. /* Traverse all directory entries (recursively), freeing all resources. */
  1596. tdo = (FAR struct tmpfs_directory_s *)fs->tfs_root.tde_object;
  1597. ret = tmpfs_foreach(tdo, tmpfs_free_callout, NULL);
  1598. /* Now we can destroy the root file system and the file system itself. */
  1599. nxsem_destroy(&tdo->tdo_exclsem.ts_sem);
  1600. kmm_free(tdo);
  1601. nxsem_destroy(&fs->tfs_exclsem.ts_sem);
  1602. kmm_free(fs);
  1603. return ret;
  1604. }
  1605. /****************************************************************************
  1606. * Name: tmpfs_statfs
  1607. ****************************************************************************/
  1608. static int tmpfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf)
  1609. {
  1610. FAR struct tmpfs_s *fs;
  1611. FAR struct tmpfs_directory_s *tdo;
  1612. struct tmpfs_statfs_s tmpbuf;
  1613. size_t inuse;
  1614. size_t avail;
  1615. off_t blkalloc;
  1616. off_t blkused;
  1617. int ret;
  1618. finfo("mountpt: %p buf: %p\n", mountpt, buf);
  1619. DEBUGASSERT(mountpt != NULL && buf != NULL);
  1620. /* Get the file system structure from the inode reference. */
  1621. fs = mountpt->i_private;
  1622. DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
  1623. /* Get exclusive access to the file system */
  1624. tmpfs_lock(fs);
  1625. /* Set up the memory use for the file system and root directory object */
  1626. tdo = (FAR struct tmpfs_directory_s *)fs->tfs_root.tde_object;
  1627. inuse = sizeof(struct tmpfs_s) +
  1628. SIZEOF_TMPFS_DIRECTORY(tdo->tdo_nentries);
  1629. avail = sizeof(struct tmpfs_s) +
  1630. tdo->tdo_alloc - inuse;
  1631. tmpbuf.tsf_alloc = tdo->tdo_alloc;
  1632. tmpbuf.tsf_inuse = inuse;
  1633. tmpbuf.tsf_files = 0;
  1634. tmpbuf.tsf_ffree = avail / sizeof(struct tmpfs_dirent_s);
  1635. /* Traverse the file system to accurmulate statistics */
  1636. ret = tmpfs_foreach(tdo, tmpfs_statfs_callout, (FAR void *)&tmpbuf);
  1637. if (ret < 0)
  1638. {
  1639. return -ECANCELED;
  1640. }
  1641. /* Return something for the file system description */
  1642. blkalloc = (tmpbuf.tsf_alloc + CONFIG_FS_TMPFS_BLOCKSIZE - 1) /
  1643. CONFIG_FS_TMPFS_BLOCKSIZE;
  1644. blkused = (tmpbuf.tsf_inuse + CONFIG_FS_TMPFS_BLOCKSIZE - 1) /
  1645. CONFIG_FS_TMPFS_BLOCKSIZE;
  1646. buf->f_type = TMPFS_MAGIC;
  1647. buf->f_namelen = NAME_MAX;
  1648. buf->f_bsize = CONFIG_FS_TMPFS_BLOCKSIZE;
  1649. buf->f_blocks = blkalloc;
  1650. buf->f_bfree = blkalloc - blkused;
  1651. buf->f_bavail = blkalloc - blkused;
  1652. buf->f_files = tmpbuf.tsf_files;
  1653. buf->f_ffree = tmpbuf.tsf_ffree;
  1654. /* Release the lock on the file system */
  1655. tmpfs_unlock(fs);
  1656. return OK;
  1657. }
  1658. /****************************************************************************
  1659. * Name: tmpfs_unlink
  1660. ****************************************************************************/
  1661. static int tmpfs_unlink(FAR struct inode *mountpt, FAR const char *relpath)
  1662. {
  1663. FAR struct tmpfs_s *fs;
  1664. FAR struct tmpfs_directory_s *tdo;
  1665. FAR struct tmpfs_file_s *tfo = NULL;
  1666. FAR const char *name;
  1667. int ret;
  1668. finfo("mountpt: %p relpath: %s\n", mountpt, relpath);
  1669. DEBUGASSERT(mountpt != NULL && relpath != NULL);
  1670. /* Get the file system structure from the inode reference. */
  1671. fs = mountpt->i_private;
  1672. DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
  1673. /* Get exclusive access to the file system */
  1674. tmpfs_lock(fs);
  1675. /* Find the file object and parent directory associated with this relative
  1676. * path. If successful, tmpfs_find_file will lock both the file object
  1677. * and the parent directory and take one reference count on each.
  1678. */
  1679. ret = tmpfs_find_file(fs, relpath, &tfo, &tdo);
  1680. if (ret < 0)
  1681. {
  1682. goto errout_with_lock;
  1683. }
  1684. DEBUGASSERT(tfo != NULL);
  1685. /* Get the file name from the relative path */
  1686. name = strrchr(relpath, '/');
  1687. if (name != NULL)
  1688. {
  1689. /* Skip over the file '/' character */
  1690. name++;
  1691. }
  1692. else
  1693. {
  1694. /* The name must lie in the root directory */
  1695. name = relpath;
  1696. }
  1697. /* Remove the file from parent directory */
  1698. ret = tmpfs_remove_dirent(tdo, name);
  1699. if (ret < 0)
  1700. {
  1701. goto errout_with_objects;
  1702. }
  1703. /* If the reference count is not one, then just mark the file as
  1704. * unlinked
  1705. */
  1706. if (tfo->tfo_refs > 1)
  1707. {
  1708. /* Make the file object as unlinked */
  1709. tfo->tfo_flags |= TFO_FLAG_UNLINKED;
  1710. /* Release the reference count on the file object */
  1711. tfo->tfo_refs--;
  1712. tmpfs_unlock_file(tfo);
  1713. }
  1714. /* Otherwise we can free the object now */
  1715. else
  1716. {
  1717. nxsem_destroy(&tfo->tfo_exclsem.ts_sem);
  1718. kmm_free(tfo);
  1719. }
  1720. /* Release the reference and lock on the parent directory */
  1721. tdo->tdo_refs--;
  1722. tmpfs_unlock_directory(tdo);
  1723. tmpfs_unlock(fs);
  1724. return OK;
  1725. errout_with_objects:
  1726. tmpfs_release_lockedfile(tfo);
  1727. tdo->tdo_refs--;
  1728. tmpfs_unlock_directory(tdo);
  1729. errout_with_lock:
  1730. tmpfs_unlock(fs);
  1731. return ret;
  1732. }
  1733. /****************************************************************************
  1734. * Name: tmpfs_mkdir
  1735. ****************************************************************************/
  1736. static int tmpfs_mkdir(FAR struct inode *mountpt, FAR const char *relpath,
  1737. mode_t mode)
  1738. {
  1739. FAR struct tmpfs_s *fs;
  1740. int ret;
  1741. finfo("mountpt: %p relpath: %s mode: %04x\n", mountpt, relpath, mode);
  1742. DEBUGASSERT(mountpt != NULL && relpath != NULL);
  1743. /* Get the file system structure from the inode reference. */
  1744. fs = mountpt->i_private;
  1745. DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
  1746. /* Get exclusive access to the file system */
  1747. tmpfs_lock(fs);
  1748. /* Create the directory. */
  1749. ret = tmpfs_create_directory(fs, relpath, NULL);
  1750. tmpfs_unlock(fs);
  1751. return ret;
  1752. }
  1753. /****************************************************************************
  1754. * Name: tmpfs_rmdir
  1755. ****************************************************************************/
  1756. static int tmpfs_rmdir(FAR struct inode *mountpt, FAR const char *relpath)
  1757. {
  1758. FAR struct tmpfs_s *fs;
  1759. FAR struct tmpfs_directory_s *parent;
  1760. FAR struct tmpfs_directory_s *tdo;
  1761. FAR const char *name;
  1762. int ret;
  1763. finfo("mountpt: %p relpath: %s\n", mountpt, relpath);
  1764. DEBUGASSERT(mountpt != NULL && relpath != NULL);
  1765. /* Get the file system structure from the inode reference. */
  1766. fs = mountpt->i_private;
  1767. DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
  1768. /* Get exclusive access to the file system */
  1769. tmpfs_lock(fs);
  1770. /* Find the directory object and parent directory associated with this
  1771. * relative path. If successful, tmpfs_find_file will lock both the
  1772. * directory object and the parent directory and take one reference count
  1773. * on each.
  1774. */
  1775. ret = tmpfs_find_directory(fs, relpath, &tdo, &parent);
  1776. if (ret < 0)
  1777. {
  1778. goto errout_with_lock;
  1779. }
  1780. /* Is the directory empty? We cannot remove directories that still
  1781. * contain references to file system objects. No can we remove the
  1782. * directory if there are outstanding references on it (other than
  1783. * our reference).
  1784. */
  1785. if (tdo->tdo_nentries > 0 || tdo->tdo_refs > 1)
  1786. {
  1787. ret = -EBUSY;
  1788. goto errout_with_objects;
  1789. }
  1790. /* Get the directory name from the relative path */
  1791. name = strrchr(relpath, '/');
  1792. if (name != NULL)
  1793. {
  1794. /* Skip over the fidirectoryle '/' character */
  1795. name++;
  1796. }
  1797. else
  1798. {
  1799. /* The name must lie in the root directory */
  1800. name = relpath;
  1801. }
  1802. /* Remove the directory from parent directory */
  1803. ret = tmpfs_remove_dirent(parent, name);
  1804. if (ret < 0)
  1805. {
  1806. goto errout_with_objects;
  1807. }
  1808. /* Free the directory object */
  1809. nxsem_destroy(&tdo->tdo_exclsem.ts_sem);
  1810. kmm_free(tdo);
  1811. /* Release the reference and lock on the parent directory */
  1812. parent->tdo_refs--;
  1813. tmpfs_unlock_directory(parent);
  1814. tmpfs_unlock(fs);
  1815. return OK;
  1816. errout_with_objects:
  1817. tdo->tdo_refs--;
  1818. tmpfs_unlock_directory(tdo);
  1819. parent->tdo_refs--;
  1820. tmpfs_unlock_directory(parent);
  1821. errout_with_lock:
  1822. tmpfs_unlock(fs);
  1823. return ret;
  1824. }
  1825. /****************************************************************************
  1826. * Name: tmpfs_rename
  1827. ****************************************************************************/
  1828. static int tmpfs_rename(FAR struct inode *mountpt, FAR const char *oldrelpath,
  1829. FAR const char *newrelpath)
  1830. {
  1831. FAR struct tmpfs_directory_s *oldparent;
  1832. FAR struct tmpfs_directory_s *newparent;
  1833. FAR struct tmpfs_object_s *to;
  1834. FAR struct tmpfs_s *fs;
  1835. FAR const char *oldname;
  1836. FAR char *newname;
  1837. FAR char *copy;
  1838. int ret;
  1839. finfo("mountpt: %p oldrelpath: %s newrelpath: %s\n",
  1840. mountpt, oldrelpath, newrelpath);
  1841. DEBUGASSERT(mountpt != NULL && oldrelpath != NULL && newrelpath != NULL);
  1842. /* Get the file system structure from the inode reference. */
  1843. fs = mountpt->i_private;
  1844. DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
  1845. /* Duplicate the newpath variable so that we can modify it */
  1846. copy = strdup(newrelpath);
  1847. if (copy == NULL)
  1848. {
  1849. return -ENOMEM;
  1850. }
  1851. /* Get exclusive access to the file system */
  1852. tmpfs_lock(fs);
  1853. /* Separate the new path into the new file name and the path to the new
  1854. * parent directory.
  1855. */
  1856. newname = strrchr(copy, '/');
  1857. if (newname == NULL)
  1858. {
  1859. /* No subdirectories... use the root directory */
  1860. newname = copy;
  1861. newparent = (FAR struct tmpfs_directory_s *)fs->tfs_root.tde_object;
  1862. tmpfs_lock_directory(newparent);
  1863. newparent->tdo_refs++;
  1864. }
  1865. else
  1866. {
  1867. /* Terminate the parent directory path */
  1868. *newname++ = '\0';
  1869. /* Locate the parent directory that should contain this name.
  1870. * On success, tmpfs_find_directory() will lockthe parent
  1871. * directory and increment the reference count.
  1872. */
  1873. ret = tmpfs_find_directory(fs, copy, &newparent, NULL);
  1874. if (ret < 0)
  1875. {
  1876. goto errout_with_lock;
  1877. }
  1878. }
  1879. /* Verify that no object of this name already exists in the destination
  1880. * directory.
  1881. */
  1882. ret = tmpfs_find_dirent(newparent, newname);
  1883. if (ret != -ENOENT)
  1884. {
  1885. /* Something with this name already exists in the directory.
  1886. * OR perhaps some fatal error occurred.
  1887. */
  1888. if (ret >= 0)
  1889. {
  1890. ret = -EEXIST;
  1891. }
  1892. goto errout_with_newparent;
  1893. }
  1894. /* Find the old object at oldpath. If successful, tmpfs_find_object()
  1895. * will lock both the object and the parent directory and will increment
  1896. * the reference count on both.
  1897. */
  1898. ret = tmpfs_find_object(fs, oldrelpath, &to, &oldparent);
  1899. if (ret < 0)
  1900. {
  1901. goto errout_with_newparent;
  1902. }
  1903. /* Get the old file name from the relative path */
  1904. oldname = strrchr(oldrelpath, '/');
  1905. if (oldname != NULL)
  1906. {
  1907. /* Skip over the file '/' character */
  1908. oldname++;
  1909. }
  1910. else
  1911. {
  1912. /* The name must lie in the root directory */
  1913. oldname = oldrelpath;
  1914. }
  1915. /* Remove the entry from the parent directory */
  1916. ret = tmpfs_remove_dirent(oldparent, oldname);
  1917. if (ret < 0)
  1918. {
  1919. goto errout_with_oldparent;
  1920. }
  1921. /* Add an entry to the new parent directory. */
  1922. ret = tmpfs_add_dirent(&newparent, to, newname);
  1923. errout_with_oldparent:
  1924. oldparent->tdo_refs--;
  1925. tmpfs_unlock_directory(oldparent);
  1926. tmpfs_release_lockedobject(to);
  1927. errout_with_newparent:
  1928. newparent->tdo_refs--;
  1929. tmpfs_unlock_directory(newparent);
  1930. errout_with_lock:
  1931. tmpfs_unlock(fs);
  1932. kmm_free(copy);
  1933. return ret;
  1934. }
  1935. /****************************************************************************
  1936. * Name: tmpfs_stat_common
  1937. ****************************************************************************/
  1938. static void tmpfs_stat_common(FAR struct tmpfs_object_s *to,
  1939. FAR struct stat *buf)
  1940. {
  1941. size_t objsize;
  1942. /* Is the tmpfs object a regular file? */
  1943. memset(buf, 0, sizeof(struct stat));
  1944. if (to->to_type == TMPFS_REGULAR)
  1945. {
  1946. FAR struct tmpfs_file_s *tfo =
  1947. (FAR struct tmpfs_file_s *)to;
  1948. /* -rwxrwxrwx */
  1949. buf->st_mode = S_IRWXO | S_IRWXG | S_IRWXU | S_IFREG;
  1950. /* Get the size of the object */
  1951. objsize = tfo->tfo_size;
  1952. }
  1953. else /* if (to->to_type == TMPFS_DIRECTORY) */
  1954. {
  1955. FAR struct tmpfs_directory_s *tdo =
  1956. (FAR struct tmpfs_directory_s *)to;
  1957. /* drwxrwxrwx */
  1958. buf->st_mode = S_IRWXO | S_IRWXG | S_IRWXU | S_IFDIR;
  1959. /* Get the size of the object */
  1960. objsize = SIZEOF_TMPFS_DIRECTORY(tdo->tdo_nentries);
  1961. }
  1962. /* Fake the rest of the information */
  1963. buf->st_size = objsize;
  1964. buf->st_blksize = CONFIG_FS_TMPFS_BLOCKSIZE;
  1965. buf->st_blocks = (objsize + CONFIG_FS_TMPFS_BLOCKSIZE - 1) /
  1966. CONFIG_FS_TMPFS_BLOCKSIZE;
  1967. }
  1968. /****************************************************************************
  1969. * Name: tmpfs_stat
  1970. ****************************************************************************/
  1971. static int tmpfs_stat(FAR struct inode *mountpt, FAR const char *relpath,
  1972. FAR struct stat *buf)
  1973. {
  1974. FAR struct tmpfs_s *fs;
  1975. FAR struct tmpfs_object_s *to;
  1976. int ret;
  1977. finfo("mountpt=%p relpath=%s buf=%p\n", mountpt, relpath, buf);
  1978. DEBUGASSERT(mountpt != NULL && relpath != NULL && buf != NULL);
  1979. /* Get the file system structure from the inode reference. */
  1980. fs = mountpt->i_private;
  1981. DEBUGASSERT(fs != NULL && fs->tfs_root.tde_object != NULL);
  1982. /* Get exclusive access to the file system */
  1983. tmpfs_lock(fs);
  1984. /* Find the tmpfs object at the relpath. If successful,
  1985. * tmpfs_find_object() will lock the object and increment the
  1986. * reference count on the object.
  1987. */
  1988. ret = tmpfs_find_object(fs, relpath, &to, NULL);
  1989. if (ret < 0)
  1990. {
  1991. goto errout_with_fslock;
  1992. }
  1993. /* We found it... Return information about the file object in the stat
  1994. * buffer.
  1995. */
  1996. DEBUGASSERT(to != NULL);
  1997. tmpfs_stat_common(to, buf);
  1998. /* Unlock the object and return success */
  1999. tmpfs_release_lockedobject(to);
  2000. ret = OK;
  2001. errout_with_fslock:
  2002. tmpfs_unlock(fs);
  2003. return ret;
  2004. }
  2005. /****************************************************************************
  2006. * Public Functions
  2007. ****************************************************************************/
  2008. #endif /* CONFIG_DISABLE_MOUNTPOINT */