fs_unionfs.c 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675
  1. /****************************************************************************
  2. * fs/unionfs/fs_unionfs.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <sys/types.h>
  25. #include <sys/statfs.h>
  26. #include <sys/stat.h>
  27. #include <sys/mount.h>
  28. #include <stdint.h>
  29. #include <stdbool.h>
  30. #include <stdio.h>
  31. #include <string.h>
  32. #include <fcntl.h>
  33. #include <assert.h>
  34. #include <errno.h>
  35. #include <fixedmath.h>
  36. #include <debug.h>
  37. #include <nuttx/kmalloc.h>
  38. #include <nuttx/fs/fs.h>
  39. #include <nuttx/fs/unionfs.h>
  40. #include <nuttx/fs/dirent.h>
  41. #include <nuttx/fs/ioctl.h>
  42. #include <nuttx/semaphore.h>
  43. #include "inode/inode.h"
  44. #if !defined(CONFIG_DISABLE_MOUNTPOINT) && defined(CONFIG_FS_UNIONFS)
  45. /****************************************************************************
  46. * Pre-processor Definitions
  47. ****************************************************************************/
  48. #undef MIN
  49. #undef MAX
  50. #define MIN(a,b) (((a) < (b)) ? (a) : (b))
  51. #define MAX(a,b) (((a) > (b)) ? (a) : (b))
  52. /****************************************************************************
  53. * Private Types
  54. ****************************************************************************/
  55. /* This structure describes one contained file system mountpoint */
  56. struct unionfs_mountpt_s
  57. {
  58. FAR struct inode *um_node; /* Filesystem inode */
  59. FAR char *um_prefix; /* Path prefix to filesystem */
  60. };
  61. /* This structure describes the union file system */
  62. struct unionfs_inode_s
  63. {
  64. struct unionfs_mountpt_s ui_fs[2]; /* Contained file systems */
  65. sem_t ui_exclsem; /* Enforces mutually exclusive access */
  66. int16_t ui_nopen; /* Number of open references */
  67. bool ui_unmounted; /* File system has been unmounted */
  68. };
  69. /* This structure descries one opened file */
  70. struct unionfs_file_s
  71. {
  72. uint8_t uf_ndx; /* Filesystem index */
  73. FAR struct file uf_file; /* Filesystem open file description */
  74. };
  75. /****************************************************************************
  76. * Private Function Prototypes
  77. ****************************************************************************/
  78. /* Helper functions */
  79. static int unionfs_semtake(FAR struct unionfs_inode_s *ui, bool noint);
  80. #define unionfs_semgive(ui) (void)nxsem_post(&(ui)->ui_exclsem)
  81. static FAR const char *unionfs_offsetpath(FAR const char *relpath,
  82. FAR const char *prefix);
  83. static bool unionfs_ispartprefix(FAR const char *partprefix,
  84. FAR const char *prefix);
  85. static int unionfs_tryopen(FAR struct file *filep,
  86. FAR const char *relpath, FAR const char *prefix, int oflags,
  87. mode_t mode);
  88. static int unionfs_tryopendir(FAR struct inode *inode,
  89. FAR const char *relpath, FAR const char *prefix,
  90. FAR struct fs_dirent_s *dir);
  91. static int unionfs_trymkdir(FAR struct inode *inode,
  92. FAR const char *relpath, FAR const char *prefix,
  93. mode_t mode);
  94. static int unionfs_tryrmdir(FAR struct inode *inode,
  95. FAR const char *relpath, FAR const char *prefix);
  96. static int unionfs_tryunlink(FAR struct inode *inode,
  97. FAR const char *relpath, FAR const char *prefix);
  98. static int unionfs_tryrename(FAR struct inode *mountpt,
  99. FAR const char *oldrelpath, FAR const char *newrelpath,
  100. FAR const char *prefix);
  101. static int unionfs_trystat(FAR struct inode *inode,
  102. FAR const char *relpath, FAR const char *prefix,
  103. FAR struct stat *buf);
  104. static int unionfs_trystatdir(FAR struct inode *inode,
  105. FAR const char *relpath, FAR const char *prefix);
  106. static int unionfs_trystatfile(FAR struct inode *inode,
  107. FAR const char *relpath, FAR const char *prefix);
  108. static FAR char *unionfs_relpath(FAR const char *path,
  109. FAR const char *name);
  110. static int unionfs_unbind_child(FAR struct unionfs_mountpt_s *um);
  111. static void unionfs_destroy(FAR struct unionfs_inode_s *ui);
  112. /* Operations on opened files (with struct file) */
  113. static int unionfs_open(FAR struct file *filep, const char *relpath,
  114. int oflags, mode_t mode);
  115. static int unionfs_close(FAR struct file *filep);
  116. static ssize_t unionfs_read(FAR struct file *filep, FAR char *buffer,
  117. size_t buflen);
  118. static ssize_t unionfs_write(FAR struct file *filep, FAR const char *buffer,
  119. size_t buflen);
  120. static off_t unionfs_seek(FAR struct file *filep, off_t offset,
  121. int whence);
  122. static int unionfs_ioctl(FAR struct file *filep, int cmd,
  123. unsigned long arg);
  124. static int unionfs_sync(FAR struct file *filep);
  125. static int unionfs_dup(FAR const struct file *oldp,
  126. FAR struct file *newp);
  127. static int unionfs_fstat(FAR const struct file *filep,
  128. FAR struct stat *buf);
  129. static int unionfs_truncate(FAR struct file *filep, off_t length);
  130. /* Operations on directories */
  131. static int unionfs_opendir(struct inode *mountpt, const char *relpath,
  132. FAR struct fs_dirent_s *dir);
  133. static int unionfs_closedir(FAR struct inode *mountpt,
  134. FAR struct fs_dirent_s *dir);
  135. static int unionfs_readdir(FAR struct inode *mountpt,
  136. FAR struct fs_dirent_s *dir);
  137. static int unionfs_rewinddir(FAR struct inode *mountpt,
  138. FAR struct fs_dirent_s *dir);
  139. static int unionfs_bind(FAR struct inode *blkdriver,
  140. FAR const void *data, FAR void **handle);
  141. static int unionfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
  142. unsigned int flags);
  143. static int unionfs_statfs(FAR struct inode *mountpt,
  144. FAR struct statfs *buf);
  145. /* Operations on paths */
  146. static int unionfs_unlink(FAR struct inode *mountpt,
  147. FAR const char *relpath);
  148. static int unionfs_mkdir(FAR struct inode *mountpt,
  149. FAR const char *relpath, mode_t mode);
  150. static int unionfs_rmdir(FAR struct inode *mountpt,
  151. FAR const char *relpath);
  152. static int unionfs_rename(FAR struct inode *mountpt,
  153. FAR const char *oldrelpath, FAR const char *newrelpath);
  154. static int unionfs_stat(FAR struct inode *mountpt,
  155. FAR const char *relpath, FAR struct stat *buf);
  156. /* Initialization */
  157. static int unionfs_getmount(FAR const char *path,
  158. FAR struct inode **inode);
  159. static int unionfs_dobind(FAR const char *fspath1,
  160. FAR const char *prefix1, FAR const char *fspath2,
  161. FAR const char *prefix2, FAR void **handle);
  162. /****************************************************************************
  163. * Public Data
  164. ****************************************************************************/
  165. /* See fs_mount.c -- this structure is explicitly extern'ed there.
  166. * We use the old-fashioned kind of initializers so that this will compile
  167. * with any compiler.
  168. */
  169. const struct mountpt_operations unionfs_operations =
  170. {
  171. unionfs_open, /* open */
  172. unionfs_close, /* close */
  173. unionfs_read, /* read */
  174. unionfs_write, /* write */
  175. unionfs_seek, /* seek */
  176. unionfs_ioctl, /* ioctl */
  177. unionfs_sync, /* sync */
  178. unionfs_dup, /* dup */
  179. unionfs_fstat, /* fstat */
  180. unionfs_truncate, /* truncate */
  181. unionfs_opendir, /* opendir */
  182. unionfs_closedir, /* closedir */
  183. unionfs_readdir, /* readdir */
  184. unionfs_rewinddir, /* rewinddir */
  185. unionfs_bind, /* bind */
  186. unionfs_unbind, /* unbind */
  187. unionfs_statfs, /* statfs */
  188. unionfs_unlink, /* unlink */
  189. unionfs_mkdir, /* mkdir */
  190. unionfs_rmdir, /* rmdir */
  191. unionfs_rename, /* rename */
  192. unionfs_stat /* stat */
  193. };
  194. /****************************************************************************
  195. * Private Functions
  196. ****************************************************************************/
  197. /****************************************************************************
  198. * Name: unionfs_semtake
  199. ****************************************************************************/
  200. static int unionfs_semtake(FAR struct unionfs_inode_s *ui, bool noint)
  201. {
  202. if (noint)
  203. {
  204. return nxsem_wait_uninterruptible(&ui->ui_exclsem);
  205. }
  206. else
  207. {
  208. return nxsem_wait(&ui->ui_exclsem);
  209. }
  210. }
  211. /****************************************************************************
  212. * Name: unionfs_offsetpath
  213. ****************************************************************************/
  214. static FAR const char *unionfs_offsetpath(FAR const char *relpath,
  215. FAR const char *prefix)
  216. {
  217. FAR const char *trypath;
  218. int pfxlen;
  219. /* Is there a prefix on the path to this file system? */
  220. if (prefix && (pfxlen = strlen(prefix)) > 0)
  221. {
  222. /* Does the prefix match? */
  223. if (strncmp(prefix, relpath, pfxlen) != 0)
  224. {
  225. /* No, then this relative cannot be within this file system */
  226. return NULL;
  227. }
  228. /* Skip over the prefix */
  229. trypath = relpath + pfxlen;
  230. /* Make sure that what is left is a valid, relative path */
  231. for (; *trypath == '/'; trypath++);
  232. }
  233. else
  234. {
  235. /* No.. use the full, relative path */
  236. trypath = relpath;
  237. }
  238. return trypath;
  239. }
  240. /****************************************************************************
  241. * Name: unionfs_ispartprefix
  242. ****************************************************************************/
  243. static bool unionfs_ispartprefix(FAR const char *partprefix,
  244. FAR const char *prefix)
  245. {
  246. int partlen = 0;
  247. int pfxlen = 0;
  248. /* Trim any '/' characters in the partial prefix */
  249. if (partprefix != NULL)
  250. {
  251. /* Skip over any leading '/' */
  252. for (; *partprefix == '/'; partprefix++);
  253. /* Skip over any tailing '/' */
  254. partlen = strlen(partprefix);
  255. for (; partlen > 1 && partprefix[partlen - 1] == '/'; partlen--);
  256. }
  257. /* Check for NUL or empty partial prefix */
  258. if (partprefix == NULL || *partprefix == '\0')
  259. {
  260. /* A NUL partial prefix is always contained in the full prefix, even
  261. * if there is no prefix.
  262. */
  263. return true;
  264. }
  265. /* Trim any '/' characters in the partial prefix */
  266. if (prefix != NULL)
  267. {
  268. /* Skip over any leading '/' */
  269. for (; *prefix == '/'; prefix++);
  270. /* Skip over any tailing '/' */
  271. pfxlen = strlen(prefix);
  272. for (; pfxlen > 1 && prefix[pfxlen - 1] == '/'; pfxlen--);
  273. }
  274. /* Check for NUL or empty full prefix */
  275. if (prefix == NULL || *prefix == '\0')
  276. {
  277. /* A non-NUL partial path cannot be a contained in a NUL prefix */
  278. return false;
  279. }
  280. #if 0 /* Only whole offset is currently supported */
  281. /* Both the partial path and the prefix are non-NULL. Check if the partial
  282. * path is contained in the prefix.
  283. */
  284. if (partlen > pfxlen)
  285. {
  286. return false;
  287. }
  288. #else
  289. /* Check if the trimmed offsets are identical */
  290. if (partlen != pfxlen)
  291. {
  292. return false;
  293. }
  294. #endif
  295. if (strncmp(partprefix, prefix, partlen) == 0)
  296. {
  297. return true;
  298. }
  299. else
  300. {
  301. return false;
  302. }
  303. }
  304. /****************************************************************************
  305. * Name: unionfs_tryopen
  306. ****************************************************************************/
  307. static int unionfs_tryopen(FAR struct file *filep, FAR const char *relpath,
  308. FAR const char *prefix, int oflags, mode_t mode)
  309. {
  310. FAR const struct mountpt_operations *ops;
  311. FAR const char *trypath;
  312. /* Is this path valid on this file system? */
  313. trypath = unionfs_offsetpath(relpath, prefix);
  314. if (trypath == NULL)
  315. {
  316. /* No.. return -ENOENT */
  317. return -ENOENT;
  318. }
  319. /* Yes.. try to open this directory */
  320. DEBUGASSERT(filep->f_inode != NULL && filep->f_inode->u.i_mops != NULL);
  321. ops = filep->f_inode->u.i_mops;
  322. if (!ops->open)
  323. {
  324. return -ENOSYS;
  325. }
  326. return ops->open(filep, trypath, oflags, mode);
  327. }
  328. /****************************************************************************
  329. * Name: unionfs_tryopendir
  330. ****************************************************************************/
  331. static int unionfs_tryopendir(FAR struct inode *inode,
  332. FAR const char *relpath,
  333. FAR const char *prefix,
  334. FAR struct fs_dirent_s *dir)
  335. {
  336. FAR const struct mountpt_operations *ops;
  337. FAR const char *trypath;
  338. /* Is this path valid on this file system? */
  339. trypath = unionfs_offsetpath(relpath, prefix);
  340. if (trypath == NULL)
  341. {
  342. /* No.. return -ENOENT */
  343. return -ENOENT;
  344. }
  345. /* Yes.. Try to open this directory */
  346. ops = inode->u.i_mops;
  347. DEBUGASSERT(ops && ops->opendir);
  348. if (!ops->opendir)
  349. {
  350. return -ENOSYS;
  351. }
  352. return ops->opendir(inode, trypath, dir);
  353. }
  354. /****************************************************************************
  355. * Name: unionfs_trymkdir
  356. ****************************************************************************/
  357. static int unionfs_trymkdir(FAR struct inode *inode, FAR const char *relpath,
  358. FAR const char *prefix, mode_t mode)
  359. {
  360. FAR const struct mountpt_operations *ops;
  361. FAR const char *trypath;
  362. /* Is this path valid on this file system? */
  363. trypath = unionfs_offsetpath(relpath, prefix);
  364. if (trypath == NULL)
  365. {
  366. /* No.. return -ENOENT */
  367. return -ENOENT;
  368. }
  369. /* Yes.. Try to create the directory */
  370. ops = inode->u.i_mops;
  371. if (!ops->mkdir)
  372. {
  373. return -ENOSYS;
  374. }
  375. return ops->mkdir(inode, trypath, mode);
  376. }
  377. /****************************************************************************
  378. * Name: unionfs_tryrename
  379. ****************************************************************************/
  380. static int unionfs_tryrename(FAR struct inode *mountpt,
  381. FAR const char *oldrelpath,
  382. FAR const char *newrelpath,
  383. FAR const char *prefix)
  384. {
  385. FAR const struct mountpt_operations *ops;
  386. FAR const char *tryoldpath;
  387. FAR const char *trynewpath;
  388. /* Is source path valid on this file system? */
  389. tryoldpath = unionfs_offsetpath(oldrelpath, prefix);
  390. if (tryoldpath == NULL)
  391. {
  392. /* No.. return -ENOENT. This should not fail because the existence
  393. * of the file has already been verified.
  394. */
  395. return -ENOENT;
  396. }
  397. /* Is source path valid on this file system?
  398. * REVISIT: There is no logic currently to rename the file by copying i
  399. * to a different file system. So we just fail if the destination name
  400. * is not within the same file system. I might, however, be on the other
  401. * file system and that rename should be supported as a file copy and
  402. * delete.
  403. */
  404. trynewpath = unionfs_offsetpath(newrelpath, prefix);
  405. if (trynewpath == NULL)
  406. {
  407. /* No.. return -ENOSYS. We should be able to do this, but we can't
  408. * yet.
  409. */
  410. return -ENOSYS;
  411. }
  412. /* Yes.. Try to rename the file */
  413. ops = mountpt->u.i_mops;
  414. if (!ops->rename)
  415. {
  416. return -ENOSYS;
  417. }
  418. return ops->rename(mountpt, tryoldpath, trynewpath);
  419. }
  420. /****************************************************************************
  421. * Name: unionfs_trystat
  422. ****************************************************************************/
  423. static int unionfs_trystat(FAR struct inode *inode, FAR const char *relpath,
  424. FAR const char *prefix, FAR struct stat *buf)
  425. {
  426. FAR const struct mountpt_operations *ops;
  427. FAR const char *trypath;
  428. /* Is this path valid on this file system? */
  429. trypath = unionfs_offsetpath(relpath, prefix);
  430. if (trypath == NULL)
  431. {
  432. /* No.. return -ENOENT */
  433. return -ENOENT;
  434. }
  435. /* Yes.. Try to create the directory */
  436. ops = inode->u.i_mops;
  437. if (!ops->stat)
  438. {
  439. return -ENOSYS;
  440. }
  441. return ops->stat(inode, trypath, buf);
  442. }
  443. /****************************************************************************
  444. * Name: unionfs_trystatdir
  445. ****************************************************************************/
  446. static int unionfs_trystatdir(FAR struct inode *inode,
  447. FAR const char *relpath,
  448. FAR const char *prefix)
  449. {
  450. FAR struct stat buf;
  451. int ret;
  452. /* Check if relative path refers to a directory. */
  453. ret = unionfs_trystat(inode, relpath, prefix, &buf);
  454. if (ret >= 0 && !S_ISDIR(buf.st_mode))
  455. {
  456. return -ENOTDIR;
  457. }
  458. return ret;
  459. }
  460. /****************************************************************************
  461. * Name: unionfs_trystatfile
  462. ****************************************************************************/
  463. static int unionfs_trystatfile(FAR struct inode *inode,
  464. FAR const char *relpath,
  465. FAR const char *prefix)
  466. {
  467. FAR struct stat buf;
  468. int ret;
  469. /* Check if relative path refers to a regular file. We specifically
  470. * exclude directories but neither do we expect any kind of special file
  471. * to reside on the mounted filesystem.
  472. */
  473. ret = unionfs_trystat(inode, relpath, prefix, &buf);
  474. if (ret >= 0 && !S_ISREG(buf.st_mode))
  475. {
  476. return -EISDIR;
  477. }
  478. return ret;
  479. }
  480. /****************************************************************************
  481. * Name: unionfs_tryrmdir
  482. ****************************************************************************/
  483. static int unionfs_tryrmdir(FAR struct inode *inode, FAR const char *relpath,
  484. FAR const char *prefix)
  485. {
  486. FAR const struct mountpt_operations *ops;
  487. FAR const char *trypath;
  488. /* Is this path valid on this file system? */
  489. trypath = unionfs_offsetpath(relpath, prefix);
  490. if (trypath == NULL)
  491. {
  492. /* No.. return -ENOENT */
  493. return -ENOENT;
  494. }
  495. /* Yes.. Try to remove the directory */
  496. ops = inode->u.i_mops;
  497. if (!ops->rmdir)
  498. {
  499. return -ENOSYS;
  500. }
  501. return ops->rmdir(inode, trypath);
  502. }
  503. /****************************************************************************
  504. * Name: unionfs_tryunlink
  505. ****************************************************************************/
  506. static int unionfs_tryunlink(FAR struct inode *inode,
  507. FAR const char *relpath,
  508. FAR const char *prefix)
  509. {
  510. FAR const struct mountpt_operations *ops;
  511. FAR const char *trypath;
  512. /* Is this path valid on this file system? */
  513. trypath = unionfs_offsetpath(relpath, prefix);
  514. if (trypath == NULL)
  515. {
  516. /* No.. return -ENOENT */
  517. return -ENOENT;
  518. }
  519. /* Yes.. Try to unlink the file */
  520. ops = inode->u.i_mops;
  521. if (!ops->unlink)
  522. {
  523. return -ENOSYS;
  524. }
  525. return ops->unlink(inode, trypath);
  526. }
  527. /****************************************************************************
  528. * Name: unionfs_relpath
  529. ****************************************************************************/
  530. static FAR char *unionfs_relpath(FAR const char *path, FAR const char *name)
  531. {
  532. FAR char *relpath;
  533. int pathlen;
  534. int ret;
  535. /* Check if there is a valid, non-zero-legnth path */
  536. if (path && (pathlen = strlen(path)) > 0)
  537. {
  538. /* Yes.. extend the file name by prepending the path */
  539. if (path[pathlen - 1] == '/')
  540. {
  541. ret = asprintf(&relpath, "%s%s", path, name);
  542. }
  543. else
  544. {
  545. ret = asprintf(&relpath, "%s/%s", path, name);
  546. }
  547. /* Handle errors */
  548. if (ret < 0)
  549. {
  550. return NULL;
  551. }
  552. else
  553. {
  554. return relpath;
  555. }
  556. }
  557. else
  558. {
  559. /* There is no path... just duplicate the name (so that kmm_free()
  560. * will work later).
  561. */
  562. return strdup(name);
  563. }
  564. }
  565. /****************************************************************************
  566. * Name: unionfs_unbind_child
  567. ****************************************************************************/
  568. static int unionfs_unbind_child(FAR struct unionfs_mountpt_s *um)
  569. {
  570. FAR struct inode *mpinode = um->um_node;
  571. FAR struct inode *bdinode = NULL;
  572. int ret;
  573. /* Unbind the block driver from the file system (destroying any fs
  574. * private data). This logic is essentially the same as the logic in
  575. * nuttx/fs/mount/fs_umount2.c.
  576. */
  577. if (!mpinode->u.i_mops->unbind)
  578. {
  579. /* The filesystem does not support the unbind operation ??? */
  580. return -EINVAL;
  581. }
  582. /* The unbind method returns the number of references to the file system
  583. * (i.e., open files), zero if the unbind was performed, or a negated
  584. * error code on a failure.
  585. */
  586. ret = mpinode->u.i_mops->unbind(mpinode->i_private, &bdinode, MNT_FORCE);
  587. if (ret < 0)
  588. {
  589. /* Some failure occurred */
  590. return ret;
  591. }
  592. else if (ret > 0)
  593. {
  594. /* REVISIT: This is bad if the file system cannot support a deferred
  595. * unmount. Ideally it would perform the unmount when the last file
  596. * is closed. But I don't think any file system do that.
  597. */
  598. return -EBUSY;
  599. }
  600. /* Successfully unbound */
  601. mpinode->i_private = NULL;
  602. /* Release the mountpoint inode and any block driver inode
  603. * returned by the file system unbind above. This should cause
  604. * the inode to be deleted (unless there are other references)
  605. */
  606. inode_release(mpinode);
  607. /* Did the unbind method return a contained block driver */
  608. if (bdinode)
  609. {
  610. inode_release(bdinode);
  611. }
  612. return OK;
  613. }
  614. /****************************************************************************
  615. * Name: unionfs_destroy
  616. ****************************************************************************/
  617. static void unionfs_destroy(FAR struct unionfs_inode_s *ui)
  618. {
  619. DEBUGASSERT(ui != NULL && ui->ui_fs[0].um_node != NULL &&
  620. ui->ui_fs[1].um_node != NULL && ui->ui_nopen == 0);
  621. /* Unbind the contained file systems */
  622. unionfs_unbind_child(&ui->ui_fs[0]);
  623. unionfs_unbind_child(&ui->ui_fs[1]);
  624. /* Free any allocated prefix strings */
  625. if (ui->ui_fs[0].um_prefix)
  626. {
  627. kmm_free(ui->ui_fs[0].um_prefix);
  628. }
  629. if (ui->ui_fs[1].um_prefix)
  630. {
  631. kmm_free(ui->ui_fs[1].um_prefix);
  632. }
  633. /* And finally free the allocated unionfs state structure as well */
  634. nxsem_destroy(&ui->ui_exclsem);
  635. kmm_free(ui);
  636. }
  637. /****************************************************************************
  638. * Name: unionfs_open
  639. ****************************************************************************/
  640. static int unionfs_open(FAR struct file *filep, FAR const char *relpath,
  641. int oflags, mode_t mode)
  642. {
  643. FAR struct unionfs_inode_s *ui;
  644. FAR struct unionfs_file_s *uf;
  645. FAR struct unionfs_mountpt_s *um;
  646. int ret;
  647. /* Recover the open file data from the struct file instance */
  648. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  649. ui = (FAR struct unionfs_inode_s *)filep->f_inode->i_private;
  650. finfo("Opening: ui_nopen=%d\n", ui->ui_nopen);
  651. /* Get exclusive access to the file system data structures */
  652. ret = unionfs_semtake(ui, false);
  653. if (ret < 0)
  654. {
  655. return ret;
  656. }
  657. /* Allocate a container to hold the open file system information */
  658. uf = (FAR struct unionfs_file_s *)
  659. kmm_malloc(sizeof(struct unionfs_file_s));
  660. if (uf == NULL)
  661. {
  662. ret = -ENOMEM;
  663. goto errout_with_semaphore;
  664. }
  665. /* Try to open the file on file system 1 */
  666. um = &ui->ui_fs[0];
  667. DEBUGASSERT(um != NULL && um->um_node != NULL &&
  668. um->um_node->u.i_mops != NULL);
  669. uf->uf_file.f_oflags = filep->f_oflags;
  670. uf->uf_file.f_pos = 0;
  671. uf->uf_file.f_inode = um->um_node;
  672. uf->uf_file.f_priv = NULL;
  673. ret = unionfs_tryopen(&uf->uf_file, relpath, um->um_prefix, oflags, mode);
  674. if (ret >= 0)
  675. {
  676. /* Successfully opened on file system 1 */
  677. uf->uf_ndx = 0;
  678. }
  679. else
  680. {
  681. /* Try to open the file on file system 1 */
  682. um = &ui->ui_fs[1];
  683. uf->uf_file.f_oflags = filep->f_oflags;
  684. uf->uf_file.f_pos = 0;
  685. uf->uf_file.f_inode = um->um_node;
  686. uf->uf_file.f_priv = NULL;
  687. ret = unionfs_tryopen(&uf->uf_file, relpath, um->um_prefix, oflags,
  688. mode);
  689. if (ret < 0)
  690. {
  691. goto errout_with_semaphore;
  692. }
  693. /* Successfully opened on file system 1 */
  694. uf->uf_ndx = 1;
  695. }
  696. /* Increment the open reference count */
  697. ui->ui_nopen++;
  698. DEBUGASSERT(ui->ui_nopen > 0);
  699. /* Save our private data in the file structure */
  700. filep->f_priv = (FAR void *)uf;
  701. ret = OK;
  702. errout_with_semaphore:
  703. unionfs_semgive(ui);
  704. return ret;
  705. }
  706. /****************************************************************************
  707. * Name: unionfs_close
  708. ****************************************************************************/
  709. static int unionfs_close(FAR struct file *filep)
  710. {
  711. FAR struct unionfs_inode_s *ui;
  712. FAR struct unionfs_file_s *uf;
  713. FAR struct unionfs_mountpt_s *um;
  714. FAR const struct mountpt_operations *ops;
  715. int ret = OK;
  716. /* Recover the open file data from the struct file instance */
  717. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  718. ui = (FAR struct unionfs_inode_s *)filep->f_inode->i_private;
  719. /* Get exclusive access to the file system data structures */
  720. ret = unionfs_semtake(ui, false);
  721. if (ret < 0)
  722. {
  723. return ret;
  724. }
  725. DEBUGASSERT(ui != NULL && filep->f_priv != NULL);
  726. uf = (FAR struct unionfs_file_s *)filep->f_priv;
  727. DEBUGASSERT(uf->uf_ndx == 0 || uf->uf_ndx == 1);
  728. um = &ui->ui_fs[uf->uf_ndx];
  729. DEBUGASSERT(um != NULL && um->um_node != NULL &&
  730. um->um_node->u.i_mops != NULL);
  731. ops = um->um_node->u.i_mops;
  732. finfo("Closing: ui_nopen=%d\n", ui->ui_nopen);
  733. /* Perform the lower level close operation */
  734. if (ops->close != NULL)
  735. {
  736. ret = ops->close(&uf->uf_file);
  737. }
  738. /* Decrement the count of open reference. If that count would go to zero
  739. * and if the file system has been unmounted, then destroy the file system
  740. * now.
  741. */
  742. if (--ui->ui_nopen <= 0 && ui->ui_unmounted)
  743. {
  744. unionfs_destroy(ui);
  745. }
  746. /* Free the open file container */
  747. kmm_free(uf);
  748. filep->f_priv = NULL;
  749. unionfs_semgive(ui);
  750. return ret;
  751. }
  752. /****************************************************************************
  753. * Name: unionfs_read
  754. ****************************************************************************/
  755. static ssize_t unionfs_read(FAR struct file *filep, FAR char *buffer,
  756. size_t buflen)
  757. {
  758. FAR struct unionfs_inode_s *ui;
  759. FAR struct unionfs_file_s *uf;
  760. FAR struct unionfs_mountpt_s *um;
  761. FAR const struct mountpt_operations *ops;
  762. finfo("buflen: %lu\n", (unsigned long)buflen);
  763. /* Recover the open file data from the struct file instance */
  764. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  765. ui = (FAR struct unionfs_inode_s *)filep->f_inode->i_private;
  766. DEBUGASSERT(ui != NULL && filep->f_priv != NULL);
  767. uf = (FAR struct unionfs_file_s *)filep->f_priv;
  768. DEBUGASSERT(uf->uf_ndx == 0 || uf->uf_ndx == 1);
  769. um = &ui->ui_fs[uf->uf_ndx];
  770. DEBUGASSERT(um != NULL && um->um_node != NULL &&
  771. um->um_node->u.i_mops != NULL);
  772. ops = um->um_node->u.i_mops;
  773. /* Perform the lower level read operation */
  774. return ops->read ? ops->read(&uf->uf_file, buffer, buflen) : -EPERM;
  775. }
  776. /****************************************************************************
  777. * Name: unionfs_write
  778. ****************************************************************************/
  779. static ssize_t unionfs_write(FAR struct file *filep, FAR const char *buffer,
  780. size_t buflen)
  781. {
  782. FAR struct unionfs_inode_s *ui;
  783. FAR struct unionfs_file_s *uf;
  784. FAR struct unionfs_mountpt_s *um;
  785. FAR const struct mountpt_operations *ops;
  786. finfo("buflen: %lu\n", (unsigned long)buflen);
  787. /* Recover the open file data from the struct file instance */
  788. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  789. ui = (FAR struct unionfs_inode_s *)filep->f_inode->i_private;
  790. DEBUGASSERT(ui != NULL && filep->f_priv != NULL);
  791. uf = (FAR struct unionfs_file_s *)filep->f_priv;
  792. DEBUGASSERT(uf->uf_ndx == 0 || uf->uf_ndx == 1);
  793. um = &ui->ui_fs[uf->uf_ndx];
  794. DEBUGASSERT(um != NULL && um->um_node != NULL &&
  795. um->um_node->u.i_mops != NULL);
  796. ops = um->um_node->u.i_mops;
  797. /* Perform the lower level write operation */
  798. return ops->write ? ops->write(&uf->uf_file, buffer, buflen) : -EPERM;
  799. }
  800. /****************************************************************************
  801. * Name: unionfs_seek
  802. ****************************************************************************/
  803. static off_t unionfs_seek(FAR struct file *filep, off_t offset, int whence)
  804. {
  805. FAR struct unionfs_inode_s *ui;
  806. FAR struct unionfs_file_s *uf;
  807. FAR struct unionfs_mountpt_s *um;
  808. FAR const struct mountpt_operations *ops;
  809. finfo("offset: %lu whence: %d\n", (unsigned long)offset, whence);
  810. /* Recover the open file data from the struct file instance */
  811. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  812. ui = (FAR struct unionfs_inode_s *)filep->f_inode->i_private;
  813. DEBUGASSERT(ui != NULL && filep->f_priv != NULL);
  814. uf = (FAR struct unionfs_file_s *)filep->f_priv;
  815. DEBUGASSERT(uf->uf_ndx == 0 || uf->uf_ndx == 1);
  816. um = &ui->ui_fs[uf->uf_ndx];
  817. DEBUGASSERT(um != NULL && um->um_node != NULL &&
  818. um->um_node->u.i_mops != NULL);
  819. ops = um->um_node->u.i_mops;
  820. /* Invoke the file seek method if available */
  821. if (ops->seek != NULL)
  822. {
  823. offset = ops->seek(&uf->uf_file, offset, whence);
  824. }
  825. else
  826. {
  827. int ret;
  828. /* Get exclusive access to the file system data structures */
  829. ret = unionfs_semtake(ui, false);
  830. if (ret < 0)
  831. {
  832. return ret;
  833. }
  834. /* No... Just set the common file position value */
  835. switch (whence)
  836. {
  837. case SEEK_CUR:
  838. offset += filep->f_pos;
  839. case SEEK_SET:
  840. if (offset >= 0)
  841. {
  842. filep->f_pos = offset; /* Might be beyond the end-of-file */
  843. }
  844. else
  845. {
  846. offset = (off_t)-EINVAL;
  847. }
  848. break;
  849. case SEEK_END:
  850. offset = (off_t)-ENOSYS;
  851. break;
  852. default:
  853. offset = (off_t)-EINVAL;
  854. break;
  855. }
  856. unionfs_semgive(ui);
  857. }
  858. return offset;
  859. }
  860. /****************************************************************************
  861. * Name: unionfs_ioctl
  862. ****************************************************************************/
  863. static int unionfs_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
  864. {
  865. FAR struct unionfs_inode_s *ui;
  866. FAR struct unionfs_file_s *uf;
  867. FAR struct unionfs_mountpt_s *um;
  868. FAR const struct mountpt_operations *ops;
  869. finfo("cmd: %d arg: %lu\n", cmd, arg);
  870. /* Recover the open file data from the struct file instance */
  871. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  872. ui = (FAR struct unionfs_inode_s *)filep->f_inode->i_private;
  873. DEBUGASSERT(ui != NULL && filep->f_priv != NULL);
  874. uf = (FAR struct unionfs_file_s *)filep->f_priv;
  875. DEBUGASSERT(uf->uf_ndx == 0 || uf->uf_ndx == 1);
  876. um = &ui->ui_fs[uf->uf_ndx];
  877. DEBUGASSERT(um != NULL && um->um_node != NULL &&
  878. um->um_node->u.i_mops != NULL);
  879. ops = um->um_node->u.i_mops;
  880. /* Perform the lower level ioctl operation */
  881. return ops->ioctl ? ops->ioctl(&uf->uf_file, cmd, arg) : -ENOTTY;
  882. }
  883. /****************************************************************************
  884. * Name: unionfs_sync
  885. ****************************************************************************/
  886. static int unionfs_sync(FAR struct file *filep)
  887. {
  888. FAR struct unionfs_inode_s *ui;
  889. FAR struct unionfs_file_s *uf;
  890. FAR struct unionfs_mountpt_s *um;
  891. FAR const struct mountpt_operations *ops;
  892. finfo("filep=%p\n", filep);
  893. /* Recover the open file data from the struct file instance */
  894. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  895. ui = (FAR struct unionfs_inode_s *)filep->f_inode->i_private;
  896. DEBUGASSERT(ui != NULL && filep->f_priv != NULL);
  897. uf = (FAR struct unionfs_file_s *)filep->f_priv;
  898. DEBUGASSERT(uf->uf_ndx == 0 || uf->uf_ndx == 1);
  899. um = &ui->ui_fs[uf->uf_ndx];
  900. DEBUGASSERT(um != NULL && um->um_node != NULL &&
  901. um->um_node->u.i_mops != NULL);
  902. ops = um->um_node->u.i_mops;
  903. /* Perform the lower level sync operation */
  904. return ops->sync ? ops->sync(&uf->uf_file) : -EINVAL;
  905. }
  906. /****************************************************************************
  907. * Name: unionfs_dup
  908. ****************************************************************************/
  909. static int unionfs_dup(FAR const struct file *oldp, FAR struct file *newp)
  910. {
  911. FAR struct unionfs_file_s *oldpriv;
  912. FAR struct unionfs_file_s *newpriv;
  913. FAR struct unionfs_inode_s *ui;
  914. FAR struct unionfs_mountpt_s *um;
  915. FAR const struct mountpt_operations *ops;
  916. int ret = -ENOMEM;
  917. finfo("oldp=%p newp=%p\n", oldp, newp);
  918. /* Recover the open file data from the struct file instance */
  919. DEBUGASSERT(oldp != NULL && oldp->f_inode != NULL);
  920. ui = (FAR struct unionfs_inode_s *)oldp->f_inode->i_private;
  921. DEBUGASSERT(ui != NULL && oldp->f_priv != NULL);
  922. oldpriv = (FAR struct unionfs_file_s *)oldp->f_priv;
  923. DEBUGASSERT(oldpriv->uf_ndx == 0 || oldpriv->uf_ndx == 1);
  924. um = &ui->ui_fs[oldpriv->uf_ndx];
  925. DEBUGASSERT(um != NULL && um->um_node != NULL &&
  926. um->um_node->u.i_mops != NULL);
  927. ops = um->um_node->u.i_mops;
  928. DEBUGASSERT(newp != NULL && newp->f_priv == NULL);
  929. /* Allocate a new container for the union FS open file */
  930. newpriv = (FAR struct unionfs_file_s *)
  931. kmm_malloc(sizeof(struct unionfs_file_s));
  932. if (newpriv != NULL)
  933. {
  934. /* Clone the old file structure into the newly allocated one */
  935. memcpy(newpriv, oldpriv, sizeof(struct unionfs_file_s));
  936. newpriv->uf_file.f_priv = NULL;
  937. /* Then perform the lower lowel dup operation */
  938. ret = OK;
  939. if (ops->dup != NULL)
  940. {
  941. ret = ops->dup(&oldpriv->uf_file, &newpriv->uf_file);
  942. if (ret < 0)
  943. {
  944. kmm_free(newpriv);
  945. newpriv = NULL;
  946. }
  947. }
  948. /* Save the new container in the new file structure */
  949. newp->f_priv = newpriv;
  950. }
  951. return ret;
  952. }
  953. /****************************************************************************
  954. * Name: unionfs_fstat
  955. *
  956. * Description:
  957. * Obtain information about an open file associated with the file
  958. * descriptor 'fd', and will write it to the area pointed to by 'buf'.
  959. *
  960. ****************************************************************************/
  961. static int unionfs_fstat(FAR const struct file *filep, FAR struct stat *buf)
  962. {
  963. FAR struct unionfs_inode_s *ui;
  964. FAR struct unionfs_file_s *uf;
  965. FAR struct unionfs_mountpt_s *um;
  966. FAR const struct mountpt_operations *ops;
  967. finfo("filep=%p buf=%p\n", filep, buf);
  968. /* Recover the open file data from the struct file instance */
  969. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  970. ui = (FAR struct unionfs_inode_s *)filep->f_inode->i_private;
  971. DEBUGASSERT(ui != NULL && filep->f_priv != NULL);
  972. uf = (FAR struct unionfs_file_s *)filep->f_priv;
  973. DEBUGASSERT(uf->uf_ndx == 0 || uf->uf_ndx == 1);
  974. um = &ui->ui_fs[uf->uf_ndx];
  975. DEBUGASSERT(um != NULL && um->um_node != NULL &&
  976. um->um_node->u.i_mops != NULL);
  977. ops = um->um_node->u.i_mops;
  978. /* Perform the lower level write operation */
  979. return ops->fstat ? ops->fstat(&uf->uf_file, buf) : -EPERM;
  980. }
  981. /****************************************************************************
  982. * Name: unionfs_truncate
  983. *
  984. * Description:
  985. * Set the size of the file references by 'filep' to 'length'.
  986. *
  987. ****************************************************************************/
  988. static int unionfs_truncate(FAR struct file *filep, off_t length)
  989. {
  990. FAR struct unionfs_inode_s *ui;
  991. FAR struct unionfs_file_s *uf;
  992. FAR struct unionfs_mountpt_s *um;
  993. FAR const struct mountpt_operations *ops;
  994. finfo("filep=%p length=%ld\n", filep, (long)length);
  995. /* Recover the open file data from the struct file instance */
  996. DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
  997. ui = (FAR struct unionfs_inode_s *)filep->f_inode->i_private;
  998. DEBUGASSERT(ui != NULL && filep->f_priv != NULL);
  999. uf = (FAR struct unionfs_file_s *)filep->f_priv;
  1000. DEBUGASSERT(uf->uf_ndx == 0 || uf->uf_ndx == 1);
  1001. um = &ui->ui_fs[uf->uf_ndx];
  1002. DEBUGASSERT(um != NULL && um->um_node != NULL &&
  1003. um->um_node->u.i_mops != NULL);
  1004. ops = um->um_node->u.i_mops;
  1005. /* Perform the lower level write operation */
  1006. return ops->truncate ? ops->truncate(&uf->uf_file, length) : -EPERM;
  1007. }
  1008. /****************************************************************************
  1009. * Name: unionfs_opendir
  1010. ****************************************************************************/
  1011. static int unionfs_opendir(FAR struct inode *mountpt,
  1012. FAR const char *relpath,
  1013. FAR struct fs_dirent_s *dir)
  1014. {
  1015. FAR struct unionfs_inode_s *ui;
  1016. FAR struct unionfs_mountpt_s *um;
  1017. FAR struct fs_unionfsdir_s *fu;
  1018. FAR const struct mountpt_operations *ops;
  1019. FAR struct fs_dirent_s *lowerdir;
  1020. int ret;
  1021. finfo("relpath: \"%s\"\n", relpath ? relpath : "NULL");
  1022. /* Recover the filesystem data from the struct inode instance */
  1023. DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
  1024. ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
  1025. /* Get exclusive access to the file system data structures */
  1026. ret = unionfs_semtake(ui, false);
  1027. if (ret < 0)
  1028. {
  1029. return ret;
  1030. }
  1031. DEBUGASSERT(dir);
  1032. fu = &dir->u.unionfs;
  1033. /* Clone the path. We will need this when we traverse file system 2 to
  1034. * omit duplicates on file system 1.
  1035. */
  1036. if (relpath && strlen(relpath) > 0)
  1037. {
  1038. fu->fu_relpath = strdup(relpath);
  1039. if (!fu->fu_relpath)
  1040. {
  1041. goto errout_with_semaphore;
  1042. }
  1043. }
  1044. /* Allocate another dirent structure for the lower file system */
  1045. lowerdir = (FAR struct fs_dirent_s *)
  1046. kmm_zalloc(sizeof(struct fs_dirent_s));
  1047. if (lowerdir == NULL)
  1048. {
  1049. ret = -ENOMEM;
  1050. goto errout_with_relpath;
  1051. }
  1052. /* Check file system 2 first. */
  1053. um = &ui->ui_fs[1];
  1054. lowerdir->fd_root = um->um_node;
  1055. ret = unionfs_tryopendir(um->um_node, relpath, um->um_prefix, lowerdir);
  1056. if (ret >= 0)
  1057. {
  1058. /* Save the file system 2 access info */
  1059. fu->fu_ndx = 1;
  1060. fu->fu_lower[1] = lowerdir;
  1061. /* Allocate yet another dirent structure for the lower file system 1 */
  1062. lowerdir = (FAR struct fs_dirent_s *)
  1063. kmm_zalloc(sizeof(struct fs_dirent_s));
  1064. if (lowerdir == NULL)
  1065. {
  1066. ret = -ENOMEM;
  1067. goto errout_with_fs2open;
  1068. }
  1069. }
  1070. /* Check if the user is stat'ing some "fake" node between the unionfs root
  1071. * and the file system 1/2 root directory.
  1072. */
  1073. else if (unionfs_ispartprefix(relpath, ui->ui_fs[1].um_prefix))
  1074. {
  1075. /* File system 2 prefix includes this relpath */
  1076. fu->fu_ndx = 1;
  1077. fu->fu_prefix[1] = true;
  1078. }
  1079. /* Check file system 1 last, possibly overwriting fu_ndx */
  1080. um = &ui->ui_fs[0];
  1081. lowerdir->fd_root = um->um_node;
  1082. ret = unionfs_tryopendir(um->um_node, relpath, um->um_prefix, lowerdir);
  1083. if (ret >= 0)
  1084. {
  1085. /* Save the file system 1 access info */
  1086. fu->fu_ndx = 0;
  1087. fu->fu_lower[0] = lowerdir;
  1088. }
  1089. else
  1090. {
  1091. /* File system 1 was not opened... then we won't be needing that last
  1092. * localdir allocation after all.
  1093. */
  1094. kmm_free(lowerdir);
  1095. /* Check if the user is stat'ing some "fake" node between the unionfs
  1096. * root and the file system 1 root directory.
  1097. */
  1098. if (unionfs_ispartprefix(relpath, ui->ui_fs[0].um_prefix))
  1099. {
  1100. /* File system 1 offset includes this relpath. Make sure that only
  1101. * one
  1102. */
  1103. fu->fu_ndx = 0;
  1104. fu->fu_prefix[0] = true;
  1105. fu->fu_prefix[1] = false;
  1106. }
  1107. /* If the directory was not found on either file system, then we have
  1108. * failed to open this path on either file system.
  1109. */
  1110. else if (fu->fu_lower[1] == NULL && !fu->fu_prefix[1])
  1111. {
  1112. /* Neither of the two path file systems include this relpath */
  1113. ret = -ENOENT;
  1114. goto errout_with_relpath;
  1115. }
  1116. }
  1117. /* Increment the number of open references and return success */
  1118. ui->ui_nopen++;
  1119. DEBUGASSERT(ui->ui_nopen > 0);
  1120. unionfs_semgive(ui);
  1121. return OK;
  1122. errout_with_fs2open:
  1123. ops = ui->ui_fs[1].um_node->u.i_mops;
  1124. DEBUGASSERT(ops != NULL);
  1125. if (ops->closedir != NULL)
  1126. {
  1127. ret = ops->closedir(um->um_node, fu->fu_lower[1]);
  1128. }
  1129. kmm_free(fu->fu_lower[1]);
  1130. errout_with_relpath:
  1131. if (fu->fu_relpath != NULL)
  1132. {
  1133. kmm_free(fu->fu_relpath);
  1134. }
  1135. errout_with_semaphore:
  1136. unionfs_semgive(ui);
  1137. return ret;
  1138. }
  1139. /****************************************************************************
  1140. * Name: unionfs_closedir
  1141. ****************************************************************************/
  1142. static int unionfs_closedir(FAR struct inode *mountpt,
  1143. FAR struct fs_dirent_s *dir)
  1144. {
  1145. FAR struct unionfs_inode_s *ui;
  1146. FAR struct unionfs_mountpt_s *um;
  1147. FAR const struct mountpt_operations *ops;
  1148. FAR struct fs_unionfsdir_s *fu;
  1149. int ret = OK;
  1150. int i;
  1151. finfo("mountpt=%p dir=%p\n", mountpt, dir);
  1152. /* Recover the union file system data from the struct inode instance */
  1153. DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
  1154. ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
  1155. /* Get exclusive access to the file system data structures */
  1156. ret = unionfs_semtake(ui, true);
  1157. if (ret < 0)
  1158. {
  1159. return ret;
  1160. }
  1161. DEBUGASSERT(dir);
  1162. fu = &dir->u.unionfs;
  1163. /* Close both contained file systems */
  1164. for (i = 0; i < 2; i++)
  1165. {
  1166. /* Was this file system opened? */
  1167. if (fu->fu_lower[i] != NULL)
  1168. {
  1169. um = &ui->ui_fs[i];
  1170. DEBUGASSERT(um != NULL && um->um_node != NULL &&
  1171. um->um_node->u.i_mops != NULL);
  1172. ops = um->um_node->u.i_mops;
  1173. /* Perform the lower level closedir operation */
  1174. if (ops->closedir != NULL)
  1175. {
  1176. ret = ops->closedir(um->um_node, fu->fu_lower[i]);
  1177. }
  1178. /* Free the lower dirent structure */
  1179. kmm_free(fu->fu_lower[i]);
  1180. }
  1181. }
  1182. /* Free any allocated path */
  1183. if (fu->fu_relpath != NULL)
  1184. {
  1185. kmm_free(fu->fu_relpath);
  1186. }
  1187. fu->fu_ndx = 0;
  1188. fu->fu_relpath = NULL;
  1189. fu->fu_lower[0] = NULL;
  1190. fu->fu_lower[1] = NULL;
  1191. /* Decrement the count of open reference. If that count would go to zero
  1192. * and if the file system has been unmounted, then destroy the file system
  1193. * now.
  1194. */
  1195. if (--ui->ui_nopen <= 0 && ui->ui_unmounted)
  1196. {
  1197. unionfs_destroy(ui);
  1198. }
  1199. else
  1200. {
  1201. unionfs_semgive(ui);
  1202. }
  1203. return ret;
  1204. }
  1205. /****************************************************************************
  1206. * Name: unionfs_readdir
  1207. ****************************************************************************/
  1208. static int unionfs_readdir(struct inode *mountpt, struct fs_dirent_s *dir)
  1209. {
  1210. FAR struct unionfs_inode_s *ui;
  1211. FAR struct unionfs_mountpt_s *um;
  1212. FAR struct unionfs_mountpt_s *um0;
  1213. FAR const struct mountpt_operations *ops;
  1214. FAR struct fs_unionfsdir_s *fu;
  1215. FAR char *relpath;
  1216. struct stat buf;
  1217. bool duplicate;
  1218. int ret = -ENOSYS;
  1219. /* Recover the union file system data from the struct inode instance */
  1220. DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
  1221. ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
  1222. DEBUGASSERT(dir);
  1223. fu = &dir->u.unionfs;
  1224. /* Check if we are at the end of the directory listing. */
  1225. if (fu->fu_eod)
  1226. {
  1227. /* End of file and error conditions are not distinguishable
  1228. * with readdir. Here we return -ENOENT to signal the end
  1229. * of the directory.
  1230. */
  1231. return -ENOENT;
  1232. }
  1233. DEBUGASSERT(fu->fu_ndx == 0 || fu->fu_ndx == 1);
  1234. um = &ui->ui_fs[fu->fu_ndx];
  1235. /* Special case: If the open directory is a 'fake' node in the prefix on
  1236. * one of the mounted file system, then we must also fake the return value.
  1237. */
  1238. if (fu->fu_prefix[fu->fu_ndx])
  1239. {
  1240. DEBUGASSERT(fu->fu_lower[fu->fu_ndx] == NULL && um->um_prefix != NULL);
  1241. /* Copy the file system offset into the dirent structure.
  1242. * REVISIT: This will not handle the case where the prefix contains
  1243. * the '/' character the so the offset appears to be multiple
  1244. * directories.
  1245. */
  1246. strncpy(dir->fd_dir.d_name, um->um_prefix, NAME_MAX);
  1247. /* Describe this as a read only directory */
  1248. dir->fd_dir.d_type = DTYPE_DIRECTORY;
  1249. /* Increment the index to file system 2 (maybe) */
  1250. if (fu->fu_ndx == 0 && (fu->fu_prefix[1] || fu->fu_lower[1] != NULL))
  1251. {
  1252. /* Yes.. set up to do file system 2 next time */
  1253. fu->fu_ndx++;
  1254. }
  1255. else
  1256. {
  1257. /* No.. we are finished */
  1258. fu->fu_eod = true;
  1259. }
  1260. return OK;
  1261. }
  1262. /* This is a normal, mediated file system readdir() */
  1263. DEBUGASSERT(fu->fu_lower[fu->fu_ndx] != NULL);
  1264. DEBUGASSERT(um->um_node != NULL && um->um_node->u.i_mops != NULL);
  1265. ops = um->um_node->u.i_mops;
  1266. finfo("fu_ndx: %d\n", fu->fu_ndx);
  1267. /* Perform the lower level readdir operation */
  1268. if (ops->readdir != NULL)
  1269. {
  1270. /* Loop if we discard duplicate directory entries in filey system 2 */
  1271. do
  1272. {
  1273. /* Read the directory entry */
  1274. ret = ops->readdir(um->um_node, fu->fu_lower[fu->fu_ndx]);
  1275. /* Did the read operation fail because we reached the end of the
  1276. * directory? In that case, the error would be -ENOENT. If we
  1277. * hit the end-of-directory on file system, we need to seamlessly
  1278. * move to the second file system (if there is one).
  1279. */
  1280. if (ret == -ENOENT && fu->fu_ndx == 0)
  1281. {
  1282. /* Special case: If the open directory is a 'fake' node in the
  1283. * prefix on file system2, then we must also fake the return
  1284. * value.
  1285. */
  1286. if (fu->fu_prefix[1])
  1287. {
  1288. DEBUGASSERT(fu->fu_lower[1] == NULL);
  1289. /* Switch to the second file system */
  1290. fu->fu_ndx = 1;
  1291. um = &ui->ui_fs[1];
  1292. DEBUGASSERT(um != NULL && um->um_prefix != NULL);
  1293. /* Copy the file system offset into the dirent structure.
  1294. * REVISIT: This will not handle the case where the prefix
  1295. * contains the '/' character the so the offset appears to
  1296. * be multiple directories.
  1297. */
  1298. strncpy(dir->fd_dir.d_name, um->um_prefix, NAME_MAX);
  1299. /* Describe this as a read only directory */
  1300. dir->fd_dir.d_type = DTYPE_DIRECTORY;
  1301. /* Mark the end of the directory listing */
  1302. fu->fu_eod = true;
  1303. /* Check if have already reported something of this name
  1304. * in file system 1.
  1305. */
  1306. relpath = unionfs_relpath(fu->fu_relpath, um->um_prefix);
  1307. if (relpath)
  1308. {
  1309. int tmp;
  1310. /* Check if anything exists at this path on file
  1311. * system 1
  1312. */
  1313. um0 = &ui->ui_fs[0];
  1314. tmp = unionfs_trystat(um0->um_node, relpath,
  1315. um0->um_prefix, &buf);
  1316. /* Free the allocated relpath */
  1317. kmm_free(relpath);
  1318. /* Check for a duplicate */
  1319. if (tmp >= 0)
  1320. {
  1321. /* There is something there!
  1322. * REVISIT: We could allow files and directories to
  1323. * have duplicate names.
  1324. */
  1325. return -ENOENT;
  1326. }
  1327. }
  1328. return OK;
  1329. }
  1330. /* No.. check for a normal directory access */
  1331. else if (fu->fu_lower[1] != NULL)
  1332. {
  1333. /* Switch to the second file system */
  1334. fu->fu_ndx = 1;
  1335. um = &ui->ui_fs[1];
  1336. DEBUGASSERT(um != NULL && um->um_node != NULL &&
  1337. um->um_node->u.i_mops != NULL);
  1338. ops = um->um_node->u.i_mops;
  1339. /* Make sure that the second file system directory
  1340. * enumeration is rewound to the beginning of the
  1341. * directory.
  1342. */
  1343. if (ops->rewinddir != NULL)
  1344. {
  1345. ret = ops->rewinddir(um->um_node, fu->fu_lower[1]);
  1346. }
  1347. /* Then try the read operation again */
  1348. ret = ops->readdir(um->um_node, fu->fu_lower[1]);
  1349. }
  1350. }
  1351. /* Did we successfully read a directory from file system 2? If
  1352. * so, we need to omit an duplicates that should be occluded by
  1353. * the matching file on file system 1 (if we are enumerating
  1354. * file system 1).
  1355. */
  1356. duplicate = false;
  1357. if (ret >= 0 && fu->fu_ndx == 1 && fu->fu_lower[0] != NULL)
  1358. {
  1359. /* Get the relative path to the same file on file system 1.
  1360. * NOTE: the on any failures we just assume that the filep
  1361. * is not a duplicate.
  1362. */
  1363. relpath = unionfs_relpath(fu->fu_relpath,
  1364. fu->fu_lower[1]->fd_dir.d_name);
  1365. if (relpath)
  1366. {
  1367. int tmp;
  1368. /* Check if anything exists at this path on file system 1 */
  1369. um0 = &ui->ui_fs[0];
  1370. tmp = unionfs_trystat(um0->um_node, relpath,
  1371. um0->um_prefix, &buf);
  1372. if (tmp >= 0)
  1373. {
  1374. /* There is something there!
  1375. * REVISIT: We could allow files and directories to
  1376. * have duplicate names.
  1377. */
  1378. duplicate = true;
  1379. }
  1380. /* Free the allocated relpath */
  1381. kmm_free(relpath);
  1382. }
  1383. }
  1384. }
  1385. while (duplicate);
  1386. /* Copy the return information into the dirent structure that the
  1387. * application will see.
  1388. */
  1389. dir->fd_position = fu->fu_lower[fu->fu_ndx]->fd_position;
  1390. memcpy(&dir->fd_dir, &fu->fu_lower[fu->fu_ndx]->fd_dir,
  1391. sizeof(struct dirent));
  1392. }
  1393. return ret;
  1394. }
  1395. /****************************************************************************
  1396. * Name: unionfs_rewindir
  1397. ****************************************************************************/
  1398. static int unionfs_rewinddir(struct inode *mountpt, struct fs_dirent_s *dir)
  1399. {
  1400. FAR struct unionfs_inode_s *ui;
  1401. FAR struct unionfs_mountpt_s *um;
  1402. FAR const struct mountpt_operations *ops;
  1403. FAR struct fs_unionfsdir_s *fu;
  1404. int ret = -EINVAL;
  1405. finfo("mountpt=%p dir=%p\n", mountpt, dir);
  1406. /* Recover the union file system data from the struct inode instance */
  1407. DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
  1408. ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
  1409. DEBUGASSERT(dir);
  1410. fu = &dir->u.unionfs;
  1411. /* Were we currently enumerating on file system 1? If not, is an
  1412. * enumeration possible on file system 1?
  1413. */
  1414. DEBUGASSERT(fu->fu_ndx == 0 || fu->fu_ndx == 1);
  1415. if (/* fu->fu_ndx != 0 && */ fu->fu_prefix[0] || fu->fu_lower[0] != NULL)
  1416. {
  1417. /* Yes.. switch to file system 1 */
  1418. fu->fu_ndx = 0;
  1419. }
  1420. if (!fu->fu_prefix[fu->fu_ndx])
  1421. {
  1422. DEBUGASSERT(fu->fu_lower[fu->fu_ndx] != NULL);
  1423. um = &ui->ui_fs[fu->fu_ndx];
  1424. DEBUGASSERT(um != NULL && um->um_node != NULL &&
  1425. um->um_node->u.i_mops != NULL);
  1426. ops = um->um_node->u.i_mops;
  1427. /* Perform the file system rewind operation */
  1428. if (ops->rewinddir != NULL)
  1429. {
  1430. ret = ops->rewinddir(um->um_node, fu->fu_lower[fu->fu_ndx]);
  1431. dir->fd_position = fu->fu_lower[fu->fu_ndx]->fd_position;
  1432. }
  1433. }
  1434. return ret;
  1435. }
  1436. /****************************************************************************
  1437. * Name: unionfs_bind
  1438. ****************************************************************************/
  1439. static int unionfs_bind(FAR struct inode *blkdriver, FAR const void *data,
  1440. FAR void **handle)
  1441. {
  1442. FAR const char *fspath1 = "";
  1443. FAR const char *prefix1 = "";
  1444. FAR const char *fspath2 = "";
  1445. FAR const char *prefix2 = "";
  1446. FAR char *dup;
  1447. FAR char *tmp;
  1448. FAR char *tok;
  1449. int ret;
  1450. /* Parse options from mount syscall */
  1451. dup = tmp = strdup(data);
  1452. if (!dup)
  1453. {
  1454. return -ENOMEM;
  1455. }
  1456. while ((tok = strsep(&tmp, ",")))
  1457. {
  1458. if (tok == strstr(tok, "fspath1="))
  1459. {
  1460. fspath1 = tok + 8;
  1461. }
  1462. else if (tok == strstr(tok, "prefix1="))
  1463. {
  1464. prefix1 = tok + 8;
  1465. }
  1466. else if (tok == strstr(tok, "fspath2="))
  1467. {
  1468. fspath2 = tok + 8;
  1469. }
  1470. else if (tok == strstr(tok, "prefix2="))
  1471. {
  1472. prefix2 = tok + 8;
  1473. }
  1474. }
  1475. /* Call unionfs_dobind to do the real work. */
  1476. ret = unionfs_dobind(fspath1, prefix1, fspath2, prefix2, handle);
  1477. kmm_free(dup);
  1478. return ret;
  1479. }
  1480. /****************************************************************************
  1481. * Name: unionfs_unbind
  1482. ****************************************************************************/
  1483. static int unionfs_unbind(FAR void *handle, FAR struct inode **blkdriver,
  1484. unsigned int flags)
  1485. {
  1486. FAR struct unionfs_inode_s *ui;
  1487. int ret;
  1488. finfo("handle=%p blkdriver=%p flags=%x\n", handle, blkdriver, flags);
  1489. /* Recover the union file system data from the struct inode instance */
  1490. DEBUGASSERT(handle != NULL);
  1491. ui = (FAR struct unionfs_inode_s *)handle;
  1492. /* Get exclusive access to the file system data structures */
  1493. ret = unionfs_semtake(ui, true);
  1494. if (ret < 0)
  1495. {
  1496. return ret;
  1497. }
  1498. /* Mark the file system as unmounted. */
  1499. ui->ui_unmounted = true;
  1500. /* If there are no open references, then we can destroy the file system
  1501. * now.
  1502. */
  1503. if (ui->ui_nopen <= 0)
  1504. {
  1505. unionfs_semgive(ui);
  1506. unionfs_destroy(ui);
  1507. }
  1508. else
  1509. {
  1510. unionfs_semgive(ui);
  1511. }
  1512. return OK;
  1513. }
  1514. /****************************************************************************
  1515. * Name: unionfs_statfs
  1516. ****************************************************************************/
  1517. static int unionfs_statfs(FAR struct inode *mountpt, FAR struct statfs *buf)
  1518. {
  1519. FAR struct unionfs_inode_s *ui;
  1520. FAR struct unionfs_mountpt_s *um1;
  1521. FAR struct unionfs_mountpt_s *um2;
  1522. FAR const struct mountpt_operations *ops1;
  1523. FAR const struct mountpt_operations *ops2;
  1524. FAR struct statfs *adj;
  1525. struct statfs buf1;
  1526. struct statfs buf2;
  1527. uint64_t tmp;
  1528. uint32_t ratiob16;
  1529. int ret;
  1530. finfo("mountpt=%p buf=%p\n", mountpt, buf);
  1531. /* Recover the union file system data from the struct inode instance */
  1532. DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL && buf != NULL);
  1533. ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
  1534. memset(buf, 0, sizeof(struct statfs));
  1535. /* Get statfs info from file system 1.
  1536. *
  1537. * REVISIT: What would it mean if one file system did not support statfs?
  1538. * Perhaps we could simplify the following by simply insisting that both
  1539. * file systems support the statfs method.
  1540. */
  1541. um1 = &ui->ui_fs[0];
  1542. DEBUGASSERT(um1 != NULL && um1->um_node != NULL &&
  1543. um1->um_node->u.i_mops != NULL);
  1544. ops1 = um1->um_node->u.i_mops;
  1545. um2 = &ui->ui_fs[1];
  1546. DEBUGASSERT(um2 != NULL && um2->um_node != NULL &&
  1547. um2->um_node->u.i_mops != NULL);
  1548. ops2 = um2->um_node->u.i_mops;
  1549. if (ops1->statfs != NULL && ops2->statfs != NULL)
  1550. {
  1551. ret = ops1->statfs(um1->um_node, &buf1);
  1552. if (ret < 0)
  1553. {
  1554. return ret;
  1555. }
  1556. /* Get stafs info from file system 2 */
  1557. ret = ops2->statfs(um2->um_node, &buf2);
  1558. if (ret < 0)
  1559. {
  1560. return ret;
  1561. }
  1562. }
  1563. else if (ops1->statfs != NULL)
  1564. {
  1565. /* We have statfs for file system 1 only */
  1566. return ops1->statfs(um1->um_node, buf);
  1567. }
  1568. else if (ops2->statfs != NULL)
  1569. {
  1570. /* We have statfs for file system 2 only */
  1571. return ops2->statfs(um2->um_node, buf);
  1572. }
  1573. else
  1574. {
  1575. /* We could not get stafs info from either file system */
  1576. return -ENOSYS;
  1577. }
  1578. /* We get here is we successfully obtained statfs info from both file
  1579. * systems. Now combine those results into one statfs report, trying to
  1580. * reconcile any conflicts between the file system geometries.
  1581. */
  1582. buf->f_type = UNIONFS_MAGIC;
  1583. buf->f_namelen = MIN(buf1.f_namelen, buf2.f_namelen);
  1584. buf->f_files = buf1.f_files + buf2.f_files;
  1585. buf->f_ffree = buf1.f_ffree + buf2.f_ffree;
  1586. /* Things expressed in units of blocks are the only tricky ones. We will
  1587. * depend on a uint64_t * temporary to avoid arithmetic overflow.
  1588. */
  1589. buf->f_bsize = buf1.f_bsize;
  1590. if (buf1.f_bsize != buf2.f_bsize)
  1591. {
  1592. if (buf1.f_bsize < buf2.f_bsize)
  1593. {
  1594. tmp = (((uint64_t)buf2.f_blocks *
  1595. (uint64_t)buf2.f_bsize) << 16);
  1596. ratiob16 = (uint32_t)(tmp / buf1.f_bsize);
  1597. adj = &buf2;
  1598. }
  1599. else
  1600. {
  1601. buf->f_bsize = buf2.f_bsize;
  1602. tmp = (((uint64_t)buf1.f_blocks *
  1603. (uint64_t)buf1.f_bsize) << 16);
  1604. ratiob16 = (uint32_t)(tmp / buf2.f_bsize);
  1605. adj = &buf1;
  1606. }
  1607. tmp = (uint64_t)adj->f_blocks * ratiob16;
  1608. adj->f_blocks = (off_t)(tmp >> 16);
  1609. tmp = (uint64_t)adj->f_bfree * ratiob16;
  1610. adj->f_bfree = (off_t)(tmp >> 16);
  1611. tmp = (uint64_t)adj->f_bavail * ratiob16;
  1612. adj->f_bavail = (off_t)(tmp >> 16);
  1613. }
  1614. /* Then we can just sum the adjusted sizes */
  1615. buf->f_blocks = buf1.f_blocks + buf2.f_blocks;
  1616. buf->f_bfree = buf1.f_bfree + buf2.f_bfree;
  1617. buf->f_bavail = buf1.f_bavail + buf2.f_bavail;
  1618. return OK;
  1619. }
  1620. /****************************************************************************
  1621. * Name: unionfs_unlink
  1622. ****************************************************************************/
  1623. static int unionfs_unlink(FAR struct inode *mountpt,
  1624. FAR const char *relpath)
  1625. {
  1626. FAR struct unionfs_inode_s *ui;
  1627. FAR struct unionfs_mountpt_s *um;
  1628. struct stat buf;
  1629. int ret;
  1630. finfo("relpath: %s\n", relpath);
  1631. /* Recover the union file system data from the struct inode instance */
  1632. DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL &&
  1633. relpath != NULL);
  1634. ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
  1635. /* Check if some exists at this path on file system 1. This might be
  1636. * a file or a directory
  1637. */
  1638. um = &ui->ui_fs[0];
  1639. ret = unionfs_trystat(um->um_node, relpath, um->um_prefix, &buf);
  1640. if (ret >= 0)
  1641. {
  1642. /* Yes.. Try to unlink the file on file system 1 (perhaps exposing
  1643. * a file of the same name on file system 2). This would fail
  1644. * with -ENOSYS if file system 1 is a read-only only file system or
  1645. * -EISDIR if the path is not a file.
  1646. */
  1647. ret = unionfs_tryunlink(um->um_node, relpath, um->um_prefix);
  1648. }
  1649. /* There is nothing at this path on file system 1 */
  1650. else
  1651. {
  1652. /* Check if the file exists with name on file system 2. The only
  1653. * reason that we check here is so that we can return the more
  1654. * meaningful -ENOSYS if file system 2 is a read-only file system.
  1655. */
  1656. um = &ui->ui_fs[1];
  1657. ret = unionfs_trystat(um->um_node, relpath, um->um_prefix, &buf);
  1658. if (ret >= 0)
  1659. {
  1660. /* Yes.. Try to unlink the file on file system 1. This would fail
  1661. * with -ENOSYS if file system 2 is a read-only only file system or
  1662. * -EISDIR if the path is not a file.
  1663. * */
  1664. ret = unionfs_tryunlink(um->um_node, relpath, um->um_prefix);
  1665. }
  1666. }
  1667. return ret;
  1668. }
  1669. /****************************************************************************
  1670. * Name: unionfs_mkdir
  1671. ****************************************************************************/
  1672. static int unionfs_mkdir(FAR struct inode *mountpt, FAR const char *relpath,
  1673. mode_t mode)
  1674. {
  1675. FAR struct unionfs_inode_s *ui;
  1676. FAR struct unionfs_mountpt_s *um;
  1677. struct stat buf;
  1678. int ret1;
  1679. int ret2;
  1680. int ret;
  1681. finfo("relpath: %s\n", relpath);
  1682. /* Recover the union file system data from the struct inode instance */
  1683. DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL &&
  1684. relpath != NULL);
  1685. ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
  1686. /* Is there anything with this name on either file system? */
  1687. um = &ui->ui_fs[0];
  1688. ret = unionfs_trystat(um->um_node, relpath, um->um_prefix, &buf);
  1689. if (ret >= 0)
  1690. {
  1691. return -EEXIST;
  1692. }
  1693. um = &ui->ui_fs[1];
  1694. ret = unionfs_trystat(um->um_node, relpath, um->um_prefix, &buf);
  1695. if (ret >= 0)
  1696. {
  1697. return -EEXIST;
  1698. }
  1699. /* Try to create the directory on both file systems. */
  1700. um = &ui->ui_fs[0];
  1701. ret1 = unionfs_trymkdir(um->um_node, relpath, um->um_prefix, mode);
  1702. um = &ui->ui_fs[1];
  1703. ret2 = unionfs_trymkdir(um->um_node, relpath, um->um_prefix, mode);
  1704. /* We will say we were successful if we were able to create the
  1705. * directory on either file system. Perhaps one file system is
  1706. * read-only and the other is write-able?
  1707. */
  1708. return (ret1 >= 0 || ret2 >= 0) ? OK : ret1;
  1709. }
  1710. /****************************************************************************
  1711. * Name: unionfs_rmdir
  1712. ****************************************************************************/
  1713. static int unionfs_rmdir(FAR struct inode *mountpt, FAR const char *relpath)
  1714. {
  1715. FAR struct unionfs_inode_s *ui;
  1716. FAR struct unionfs_mountpt_s *um;
  1717. int ret = -ENOENT;
  1718. int tmp;
  1719. finfo("relpath: %s\n", relpath);
  1720. /* Recover the union file system data from the struct inode instance */
  1721. DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL &&
  1722. relpath != NULL);
  1723. ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
  1724. /* We really don't know any better so we will try to remove the directory
  1725. * from both file systems.
  1726. */
  1727. /* Is there a directory with this name on file system 1 */
  1728. um = &ui->ui_fs[0];
  1729. tmp = unionfs_trystatdir(um->um_node, relpath, um->um_prefix);
  1730. if (tmp >= 0)
  1731. {
  1732. /* Yes.. remove it. Since we know that the directory exists, any
  1733. * failure to remove it is a showstopper.
  1734. */
  1735. ret = unionfs_tryrmdir(um->um_node, relpath, um->um_prefix);
  1736. if (ret < 0)
  1737. {
  1738. return ret;
  1739. }
  1740. }
  1741. /* Either the directory does not exist on file system 1, or we
  1742. * successfully removed it. Try again on file system 2.
  1743. */
  1744. um = &ui->ui_fs[1];
  1745. tmp = unionfs_trystatdir(um->um_node, relpath, um->um_prefix);
  1746. if (tmp >= 0)
  1747. {
  1748. /* Yes.. remove it. Since we know that the directory exists, any
  1749. * failure to remove it is a showstopper.
  1750. */
  1751. ret = unionfs_tryrmdir(um->um_node, relpath, um->um_prefix);
  1752. /* REVISIT: Should we try to restore the directory on file system 1
  1753. * if we failure to removed the directory on file system 2?
  1754. */
  1755. }
  1756. return ret;
  1757. }
  1758. /****************************************************************************
  1759. * Name: unionfs_rename
  1760. ****************************************************************************/
  1761. static int unionfs_rename(FAR struct inode *mountpt,
  1762. FAR const char *oldrelpath,
  1763. FAR const char *newrelpath)
  1764. {
  1765. FAR struct unionfs_inode_s *ui;
  1766. FAR struct unionfs_mountpt_s *um;
  1767. int ret = -ENOENT;
  1768. int tmp;
  1769. finfo("oldrelpath: %s newrelpath: %s\n", oldrelpath, newrelpath);
  1770. /* Recover the union file system data from the struct inode instance */
  1771. DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL);
  1772. ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
  1773. DEBUGASSERT(oldrelpath != NULL && oldrelpath != NULL);
  1774. /* Is there a file with this name on file system 1 */
  1775. um = &ui->ui_fs[0];
  1776. tmp = unionfs_trystatfile(um->um_node, oldrelpath, um->um_prefix);
  1777. if (tmp >= 0)
  1778. {
  1779. /* Yes.. rename it. Since we know that the directory exists, any
  1780. * failure to remove it is a showstopper.
  1781. */
  1782. ret = unionfs_tryrename(um->um_node, oldrelpath, newrelpath,
  1783. um->um_prefix);
  1784. if (ret >= 0)
  1785. {
  1786. /* Return immediately on success. In the event that the file
  1787. * exists in both file systems, this will produce the odd behavior
  1788. * that one file on file system 1 was renamed but another obscured
  1789. * file of the same relative path will become visible.
  1790. */
  1791. return OK;
  1792. }
  1793. }
  1794. /* Either the file does not exist on file system 1, or we failed to rename
  1795. * it (perhaps because the file system was read-only). Try again on file
  1796. * system 2.
  1797. */
  1798. um = &ui->ui_fs[1];
  1799. tmp = unionfs_trystatfile(um->um_node, oldrelpath, um->um_prefix);
  1800. if (tmp >= 0)
  1801. {
  1802. /* Yes.. remove it. Since we know that the directory exists, any
  1803. * failure to remove it is a showstopper.
  1804. */
  1805. ret = unionfs_tryrename(um->um_node, oldrelpath, newrelpath,
  1806. um->um_prefix);
  1807. }
  1808. return ret;
  1809. }
  1810. /****************************************************************************
  1811. * Name: unionfs_stat
  1812. ****************************************************************************/
  1813. static int unionfs_stat(FAR struct inode *mountpt, FAR const char *relpath,
  1814. FAR struct stat *buf)
  1815. {
  1816. FAR struct unionfs_inode_s *ui;
  1817. FAR struct unionfs_mountpt_s *um;
  1818. int ret;
  1819. finfo("relpath: %s\n", relpath);
  1820. /* Recover the union file system data from the struct inode instance */
  1821. DEBUGASSERT(mountpt != NULL && mountpt->i_private != NULL &&
  1822. relpath != NULL);
  1823. ui = (FAR struct unionfs_inode_s *)mountpt->i_private;
  1824. /* stat this path on file system 1 */
  1825. um = &ui->ui_fs[0];
  1826. ret = unionfs_trystat(um->um_node, relpath, um->um_prefix, buf);
  1827. if (ret >= 0)
  1828. {
  1829. /* Return on the first success. The first instance of the file will
  1830. * shadow the second anyway.
  1831. */
  1832. return OK;
  1833. }
  1834. /* stat failed on the file system 1. Try again on file system 2. */
  1835. um = &ui->ui_fs[1];
  1836. ret = unionfs_trystat(um->um_node, relpath, um->um_prefix, buf);
  1837. if (ret >= 0)
  1838. {
  1839. /* Return on the first success. The first instance of the file will
  1840. * shadow the second anyway.
  1841. */
  1842. return OK;
  1843. }
  1844. /* Special case the unionfs root directory when both file systems are
  1845. * offset. In that case, both of the above trystat calls will fail.
  1846. */
  1847. if (ui->ui_fs[0].um_prefix != NULL && ui->ui_fs[1].um_prefix != NULL)
  1848. {
  1849. /* Most of the stat entries just do not apply */
  1850. memset(buf, 0, sizeof(struct stat));
  1851. /* Claim that this is a read-only directory */
  1852. buf->st_mode = S_IFDIR | S_IROTH | S_IRGRP | S_IRUSR;
  1853. /* Check if the user is stat'ing some "fake" node between the unionfs
  1854. * root and the file system 1 root directory.
  1855. */
  1856. if (unionfs_ispartprefix(relpath, ui->ui_fs[0].um_prefix) ||
  1857. unionfs_ispartprefix(relpath, ui->ui_fs[1].um_prefix))
  1858. {
  1859. ret = OK;
  1860. }
  1861. else
  1862. {
  1863. ret = -ENOENT;
  1864. }
  1865. }
  1866. return ret;
  1867. }
  1868. /****************************************************************************
  1869. * Name: unionfs_getmount
  1870. ****************************************************************************/
  1871. static int unionfs_getmount(FAR const char *path, FAR struct inode **inode)
  1872. {
  1873. FAR struct inode *minode;
  1874. struct inode_search_s desc;
  1875. int ret;
  1876. /* Find the mountpt */
  1877. SETUP_SEARCH(&desc, path, false);
  1878. ret = inode_find(&desc);
  1879. if (ret < 0)
  1880. {
  1881. /* Mountpoint inode not found */
  1882. goto errout_with_search;
  1883. }
  1884. /* Get the search results */
  1885. minode = desc.node;
  1886. DEBUGASSERT(minode != NULL);
  1887. /* Verify that the inode is a mountpoint.
  1888. *
  1889. * REVISIT: If desc.relpath points to a non-empty string, then the path
  1890. * does not really refer to a mountpoint but, rather, to a some entity
  1891. * within the mounted volume.
  1892. */
  1893. if (!INODE_IS_MOUNTPT(minode))
  1894. {
  1895. /* Inode was found, but is it is not a mounpoint */
  1896. ret = -EINVAL;
  1897. goto errout_with_inode;
  1898. }
  1899. /* Success! */
  1900. *inode = minode;
  1901. RELEASE_SEARCH(&desc);
  1902. return OK;
  1903. errout_with_inode:
  1904. inode_release(minode);
  1905. errout_with_search:
  1906. RELEASE_SEARCH(&desc);
  1907. return ret;
  1908. }
  1909. /****************************************************************************
  1910. * Name: unionfs_dobind
  1911. ****************************************************************************/
  1912. static int unionfs_dobind(FAR const char *fspath1, FAR const char *prefix1,
  1913. FAR const char *fspath2, FAR const char *prefix2,
  1914. FAR void **handle)
  1915. {
  1916. FAR struct unionfs_inode_s *ui;
  1917. int ret;
  1918. DEBUGASSERT(fspath1 != NULL && fspath2 != NULL && handle != NULL);
  1919. /* Allocate a structure a structure that will describe the union file
  1920. * system.
  1921. */
  1922. ui = (FAR struct unionfs_inode_s *)
  1923. kmm_zalloc(sizeof(struct unionfs_inode_s));
  1924. if (!ui)
  1925. {
  1926. ferr("ERROR: Failed to allocated union FS state structure\n");
  1927. return -ENOMEM;
  1928. }
  1929. nxsem_init(&ui->ui_exclsem, 0, 1);
  1930. /* Get the inodes associated with fspath1 and fspath2 */
  1931. ret = unionfs_getmount(fspath1, &ui->ui_fs[0].um_node);
  1932. if (ret < 0)
  1933. {
  1934. ferr("ERROR: unionfs_getmount(fspath1) failed: %d\n", ret);
  1935. goto errout_with_uinode;
  1936. }
  1937. ret = unionfs_getmount(fspath2, &ui->ui_fs[1].um_node);
  1938. if (ret < 0)
  1939. {
  1940. ferr("ERROR: unionfs_getmount(fspath2) failed: %d\n", ret);
  1941. goto errout_with_fs1;
  1942. }
  1943. /* Duplicate the prefix strings */
  1944. if (prefix1 && strlen(prefix1) > 0)
  1945. {
  1946. ui->ui_fs[0].um_prefix = strdup(prefix1);
  1947. if (ui->ui_fs[0].um_prefix == NULL)
  1948. {
  1949. ferr("ERROR: strdup(prefix1) failed\n");
  1950. ret = -ENOMEM;
  1951. goto errout_with_fs2;
  1952. }
  1953. }
  1954. if (prefix2 && strlen(prefix2) > 0)
  1955. {
  1956. ui->ui_fs[1].um_prefix = strdup(prefix2);
  1957. if (ui->ui_fs[1].um_prefix == NULL)
  1958. {
  1959. ferr("ERROR: strdup(prefix2) failed\n");
  1960. ret = -ENOMEM;
  1961. goto errout_with_prefix1;
  1962. }
  1963. }
  1964. /* Unlink the contained mountpoint inodes from the pseudo file system.
  1965. * The inodes will be marked as deleted so that they will be removed when
  1966. * the reference count decrements to zero in inode_release(). Because we
  1967. * hold a reference count on the inodes, they will not be deleted until
  1968. * this logic calls inode_release() in the unionfs_destroy() function.
  1969. */
  1970. inode_remove(fspath1);
  1971. inode_remove(fspath2);
  1972. *handle = ui;
  1973. return OK;
  1974. errout_with_prefix1:
  1975. if (ui->ui_fs[0].um_prefix != NULL)
  1976. {
  1977. kmm_free(ui->ui_fs[0].um_prefix);
  1978. }
  1979. errout_with_fs2:
  1980. inode_release(ui->ui_fs[1].um_node);
  1981. errout_with_fs1:
  1982. inode_release(ui->ui_fs[0].um_node);
  1983. errout_with_uinode:
  1984. nxsem_destroy(&ui->ui_exclsem);
  1985. kmm_free(ui);
  1986. return ret;
  1987. }
  1988. /****************************************************************************
  1989. * Public Functions
  1990. ****************************************************************************/
  1991. /****************************************************************************
  1992. * Name: unionfs_mount
  1993. *
  1994. * Description:
  1995. * Create and mount a union file system
  1996. *
  1997. * Input Parameters:
  1998. * fspath1 - The full path to the first file system mountpoint
  1999. * prefix1 - An optiona prefix that may be applied to make the first
  2000. * file system appear a some path below the unionfs mountpoint,
  2001. * fspath2 - The full path to the second file system mountpoint
  2002. * prefix2 - An optiona prefix that may be applied to make the first
  2003. * file system appear a some path below the unionfs mountpoint,
  2004. * mountpt - The full path to the mountpoint for the union file system
  2005. *
  2006. * Returned Value:
  2007. * Zero (OK) is returned if the union file system was correctly created and
  2008. * mounted. On any failure, a negated error value will be returned to
  2009. * indicate the nature of the failure.
  2010. *
  2011. ****************************************************************************/
  2012. int unionfs_mount(FAR const char *fspath1, FAR const char *prefix1,
  2013. FAR const char *fspath2, FAR const char *prefix2,
  2014. FAR const char *mountpt)
  2015. {
  2016. FAR struct inode *mpinode;
  2017. int ret;
  2018. DEBUGASSERT(mountpt != NULL);
  2019. /* Mount the union FS. We should adapt the standard mount to do
  2020. * this using optional parameters. This custom mount should do the job
  2021. * for now, however.
  2022. */
  2023. ret = inode_reserve(mountpt, &mpinode);
  2024. if (ret < 0)
  2025. {
  2026. /* inode_reserve can fail for a couple of reasons, but the most likely
  2027. * one is that the inode already exists. inode_reserve may return:
  2028. *
  2029. * -EINVAL - 'path' is invalid for this operation
  2030. * -EEXIST - An inode already exists at 'path'
  2031. * -ENOMEM - Failed to allocate in-memory resources for the operation
  2032. */
  2033. ferr("ERROR: Failed to reserve inode\n");
  2034. return ret;
  2035. }
  2036. /* Populate the inode with driver specific information. */
  2037. INODE_SET_MOUNTPT(mpinode);
  2038. mpinode->u.i_mops = &unionfs_operations;
  2039. #ifdef CONFIG_FILE_MODE
  2040. mpinode->i_mode = 0755;
  2041. #endif
  2042. /* Call unionfs_dobind to do the real work. */
  2043. ret = unionfs_dobind(fspath1, prefix1, fspath2, prefix2,
  2044. &mpinode->i_private);
  2045. if (ret < 0)
  2046. {
  2047. goto errout_with_mountpt;
  2048. }
  2049. return OK;
  2050. errout_with_mountpt:
  2051. inode_release(mpinode);
  2052. return ret;
  2053. }
  2054. #endif /* !CONFIG_DISABLE_MOUNTPOINT && CONFIG_FS_UNIONFS */