yaffsfs.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209
  1. /*
  2. * YAFFS: Yet Another Flash File System. A NAND-flash specific file system.
  3. *
  4. * Copyright (C) 2002-2011 Aleph One Ltd.
  5. * for Toby Churchill Ltd and Brightstar Engineering
  6. *
  7. * Created by Charles Manning <charles@aleph1.co.uk>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. */
  13. #include <div64.h>
  14. #include "yaffsfs.h"
  15. #include "yaffs_guts.h"
  16. #include "yaffscfg.h"
  17. #include "yportenv.h"
  18. #include "yaffs_trace.h"
  19. #include <dm/devres.h>
  20. #define YAFFSFS_MAX_SYMLINK_DEREFERENCES 5
  21. #ifndef NULL
  22. #define NULL ((void *)0)
  23. #endif
  24. /* YAFFSFS_RW_SIZE must be a power of 2 */
  25. #define YAFFSFS_RW_SHIFT (13)
  26. #define YAFFSFS_RW_SIZE (1<<YAFFSFS_RW_SHIFT)
  27. /* Some forward references */
  28. static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relativeDirectory,
  29. const YCHAR *path,
  30. int symDepth, int getEquiv,
  31. struct yaffs_obj **dirOut,
  32. int *notDir, int *loop);
  33. static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj);
  34. unsigned int yaffs_wr_attempts;
  35. /*
  36. * Handle management.
  37. * There are open inodes in struct yaffsfs_Inode.
  38. * There are open file descriptors in yaffsfs_FileDes.
  39. * There are open handles in yaffsfs_FileDes.
  40. *
  41. * Things are structured this way to be like the Linux VFS model
  42. * so that interactions with the yaffs guts calls are similar.
  43. * That means more common code paths and less special code.
  44. * That means better testing etc.
  45. *
  46. * We have 3 layers because:
  47. * A handle is different than an fd because you can use dup()
  48. * to create a new handle that accesses the *same* fd. The two
  49. * handles will use the same offset (part of the fd). We only close
  50. * down the fd when there are no more handles accessing it.
  51. *
  52. * More than one fd can currently access one file, but each fd
  53. * has its own permsiions and offset.
  54. */
  55. struct yaffsfs_Inode {
  56. int count; /* Number of handles accessing this inode */
  57. struct yaffs_obj *iObj;
  58. };
  59. struct yaffsfs_FileDes {
  60. u8 reading:1;
  61. u8 writing:1;
  62. u8 append:1;
  63. u8 shareRead:1;
  64. u8 shareWrite:1;
  65. int inodeId:12; /* Index to corresponding yaffsfs_Inode */
  66. int handleCount:10; /* Number of handles for this fd */
  67. loff_t position; /* current position in file */
  68. };
  69. struct yaffsfs_Handle {
  70. short int fdId;
  71. short int useCount;
  72. };
  73. struct yaffsfs_DirSearchContxt {
  74. struct yaffs_dirent de; /* directory entry */
  75. YCHAR name[NAME_MAX + 1]; /* name of directory being searched */
  76. struct yaffs_obj *dirObj; /* ptr to directory being searched */
  77. struct yaffs_obj *nextReturn; /* obj returned by next readddir */
  78. struct list_head others;
  79. int offset:20;
  80. unsigned inUse:1;
  81. };
  82. static struct yaffsfs_DirSearchContxt yaffsfs_dsc[YAFFSFS_N_DSC];
  83. static struct yaffsfs_Inode yaffsfs_inode[YAFFSFS_N_HANDLES];
  84. static struct yaffsfs_FileDes yaffsfs_fd[YAFFSFS_N_HANDLES];
  85. static struct yaffsfs_Handle yaffsfs_handle[YAFFSFS_N_HANDLES];
  86. static int yaffsfs_handlesInitialised;
  87. unsigned yaffs_set_trace(unsigned tm)
  88. {
  89. yaffs_trace_mask = tm;
  90. return yaffs_trace_mask;
  91. }
  92. unsigned yaffs_get_trace(void)
  93. {
  94. return yaffs_trace_mask;
  95. }
  96. /*
  97. * yaffsfs_InitHandle
  98. * Inilitalise handle management on start-up.
  99. */
  100. static void yaffsfs_InitHandles(void)
  101. {
  102. int i;
  103. if (yaffsfs_handlesInitialised)
  104. return;
  105. memset(yaffsfs_inode, 0, sizeof(yaffsfs_inode));
  106. memset(yaffsfs_fd, 0, sizeof(yaffsfs_fd));
  107. memset(yaffsfs_handle, 0, sizeof(yaffsfs_handle));
  108. memset(yaffsfs_dsc, 0, sizeof(yaffsfs_dsc));
  109. for (i = 0; i < YAFFSFS_N_HANDLES; i++)
  110. yaffsfs_fd[i].inodeId = -1;
  111. for (i = 0; i < YAFFSFS_N_HANDLES; i++)
  112. yaffsfs_handle[i].fdId = -1;
  113. }
  114. static struct yaffsfs_Handle *yaffsfs_HandleToPointer(int h)
  115. {
  116. if (h >= 0 && h < YAFFSFS_N_HANDLES)
  117. return &yaffsfs_handle[h];
  118. return NULL;
  119. }
  120. static struct yaffsfs_FileDes *yaffsfs_HandleToFileDes(int handle)
  121. {
  122. struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
  123. if (h && h->useCount > 0 && h->fdId >= 0 && h->fdId < YAFFSFS_N_HANDLES)
  124. return &yaffsfs_fd[h->fdId];
  125. return NULL;
  126. }
  127. static struct yaffsfs_Inode *yaffsfs_HandleToInode(int handle)
  128. {
  129. struct yaffsfs_FileDes *fd = yaffsfs_HandleToFileDes(handle);
  130. if (fd && fd->handleCount > 0 &&
  131. fd->inodeId >= 0 && fd->inodeId < YAFFSFS_N_HANDLES)
  132. return &yaffsfs_inode[fd->inodeId];
  133. return NULL;
  134. }
  135. static struct yaffs_obj *yaffsfs_HandleToObject(int handle)
  136. {
  137. struct yaffsfs_Inode *in = yaffsfs_HandleToInode(handle);
  138. if (in)
  139. return in->iObj;
  140. return NULL;
  141. }
  142. /*
  143. * yaffsfs_FindInodeIdForObject
  144. * Find the inode entry for an object, if it exists.
  145. */
  146. static int yaffsfs_FindInodeIdForObject(struct yaffs_obj *obj)
  147. {
  148. int i;
  149. int ret = -1;
  150. if (obj)
  151. obj = yaffs_get_equivalent_obj(obj);
  152. /* Look for it in open inode table */
  153. for (i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++) {
  154. if (yaffsfs_inode[i].iObj == obj)
  155. ret = i;
  156. }
  157. return ret;
  158. }
  159. /*
  160. * yaffsfs_GetInodeIdForObject
  161. * Grab an inode entry when opening a new inode.
  162. */
  163. static int yaffsfs_GetInodeIdForObject(struct yaffs_obj *obj)
  164. {
  165. int i;
  166. int ret;
  167. struct yaffsfs_Inode *in = NULL;
  168. if (obj)
  169. obj = yaffs_get_equivalent_obj(obj);
  170. ret = yaffsfs_FindInodeIdForObject(obj);
  171. for (i = 0; i < YAFFSFS_N_HANDLES && ret < 0; i++) {
  172. if (!yaffsfs_inode[i].iObj)
  173. ret = i;
  174. }
  175. if (ret >= 0) {
  176. in = &yaffsfs_inode[ret];
  177. if (!in->iObj)
  178. in->count = 0;
  179. in->iObj = obj;
  180. in->count++;
  181. }
  182. return ret;
  183. }
  184. static int yaffsfs_CountHandles(struct yaffs_obj *obj)
  185. {
  186. int i = yaffsfs_FindInodeIdForObject(obj);
  187. if (i >= 0)
  188. return yaffsfs_inode[i].count;
  189. else
  190. return 0;
  191. }
  192. static void yaffsfs_ReleaseInode(struct yaffsfs_Inode *in)
  193. {
  194. struct yaffs_obj *obj;
  195. obj = in->iObj;
  196. if (obj->unlinked)
  197. yaffs_del_obj(obj);
  198. obj->my_inode = NULL;
  199. in->iObj = NULL;
  200. }
  201. static void yaffsfs_PutInode(int inodeId)
  202. {
  203. if (inodeId >= 0 && inodeId < YAFFSFS_N_HANDLES) {
  204. struct yaffsfs_Inode *in = &yaffsfs_inode[inodeId];
  205. in->count--;
  206. if (in->count <= 0) {
  207. yaffsfs_ReleaseInode(in);
  208. in->count = 0;
  209. }
  210. }
  211. }
  212. static int yaffsfs_NewHandle(struct yaffsfs_Handle **hptr)
  213. {
  214. int i;
  215. struct yaffsfs_Handle *h;
  216. for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
  217. h = &yaffsfs_handle[i];
  218. if (h->useCount < 1) {
  219. memset(h, 0, sizeof(struct yaffsfs_Handle));
  220. h->fdId = -1;
  221. h->useCount = 1;
  222. if (hptr)
  223. *hptr = h;
  224. return i;
  225. }
  226. }
  227. return -1;
  228. }
  229. static int yaffsfs_NewHandleAndFileDes(void)
  230. {
  231. int i;
  232. struct yaffsfs_FileDes *fd;
  233. struct yaffsfs_Handle *h = NULL;
  234. int handle = yaffsfs_NewHandle(&h);
  235. if (handle < 0)
  236. return -1;
  237. for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
  238. fd = &yaffsfs_fd[i];
  239. if (fd->handleCount < 1) {
  240. memset(fd, 0, sizeof(struct yaffsfs_FileDes));
  241. fd->inodeId = -1;
  242. fd->handleCount = 1;
  243. h->fdId = i;
  244. return handle;
  245. }
  246. }
  247. /* Dump the handle because we could not get a fd */
  248. h->useCount = 0;
  249. return -1;
  250. }
  251. /*
  252. * yaffs_get_handle
  253. * Increase use of handle when reading/writing a file
  254. * Also gets the file descriptor.
  255. */
  256. static int yaffsfs_GetHandle(int handle)
  257. {
  258. struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
  259. if (h && h->useCount > 0) {
  260. h->useCount++;
  261. return 0;
  262. }
  263. return -1;
  264. }
  265. /*
  266. * yaffs_put_handle
  267. * Let go of a handle when closing a file or aborting an open or
  268. * ending a read or write.
  269. */
  270. static int yaffsfs_PutFileDes(int fdId)
  271. {
  272. struct yaffsfs_FileDes *fd;
  273. if (fdId >= 0 && fdId < YAFFSFS_N_HANDLES) {
  274. fd = &yaffsfs_fd[fdId];
  275. fd->handleCount--;
  276. if (fd->handleCount < 1) {
  277. if (fd->inodeId >= 0) {
  278. yaffsfs_PutInode(fd->inodeId);
  279. fd->inodeId = -1;
  280. }
  281. }
  282. }
  283. return 0;
  284. }
  285. static int yaffsfs_PutHandle(int handle)
  286. {
  287. struct yaffsfs_Handle *h = yaffsfs_HandleToPointer(handle);
  288. if (h && h->useCount > 0) {
  289. h->useCount--;
  290. if (h->useCount < 1) {
  291. yaffsfs_PutFileDes(h->fdId);
  292. h->fdId = -1;
  293. }
  294. }
  295. return 0;
  296. }
  297. static void yaffsfs_BreakDeviceHandles(struct yaffs_dev *dev)
  298. {
  299. struct yaffsfs_FileDes *fd;
  300. struct yaffsfs_Handle *h;
  301. struct yaffs_obj *obj;
  302. int i;
  303. for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
  304. h = yaffsfs_HandleToPointer(i);
  305. fd = yaffsfs_HandleToFileDes(i);
  306. obj = yaffsfs_HandleToObject(i);
  307. if (h && h->useCount > 0) {
  308. h->useCount = 0;
  309. h->fdId = 0;
  310. }
  311. if (fd && fd->handleCount > 0 && obj && obj->my_dev == dev) {
  312. fd->handleCount = 0;
  313. yaffsfs_PutInode(fd->inodeId);
  314. fd->inodeId = -1;
  315. }
  316. }
  317. }
  318. /*
  319. * Stuff to handle names.
  320. */
  321. #ifdef CONFIG_YAFFS_CASE_INSENSITIVE
  322. static int yaffs_toupper(YCHAR a)
  323. {
  324. if (a >= 'a' && a <= 'z')
  325. return (a - 'a') + 'A';
  326. else
  327. return a;
  328. }
  329. int yaffsfs_Match(YCHAR a, YCHAR b)
  330. {
  331. return (yaffs_toupper(a) == yaffs_toupper(b));
  332. }
  333. #else
  334. int yaffsfs_Match(YCHAR a, YCHAR b)
  335. {
  336. /* case sensitive */
  337. return (a == b);
  338. }
  339. #endif
  340. int yaffsfs_IsPathDivider(YCHAR ch)
  341. {
  342. const YCHAR *str = YAFFS_PATH_DIVIDERS;
  343. while (*str) {
  344. if (*str == ch)
  345. return 1;
  346. str++;
  347. }
  348. return 0;
  349. }
  350. int yaffsfs_CheckNameLength(const char *name)
  351. {
  352. int retVal = 0;
  353. int nameLength = yaffs_strnlen(name, YAFFS_MAX_NAME_LENGTH + 1);
  354. if (nameLength == 0) {
  355. yaffsfs_SetError(-ENOENT);
  356. retVal = -1;
  357. } else if (nameLength > YAFFS_MAX_NAME_LENGTH) {
  358. yaffsfs_SetError(-ENAMETOOLONG);
  359. retVal = -1;
  360. }
  361. return retVal;
  362. }
  363. static int yaffsfs_alt_dir_path(const YCHAR *path, YCHAR **ret_path)
  364. {
  365. YCHAR *alt_path = NULL;
  366. int path_length;
  367. int i;
  368. /*
  369. * We don't have a definition for max path length.
  370. * We will use 3 * max name length instead.
  371. */
  372. *ret_path = NULL;
  373. path_length = yaffs_strnlen(path, (YAFFS_MAX_NAME_LENGTH + 1) * 3 + 1);
  374. /* If the last character is a path divider, then we need to
  375. * trim it back so that the name look-up works properly.
  376. * eg. /foo/new_dir/ -> /foo/newdir
  377. * Curveball: Need to handle multiple path dividers:
  378. * eg. /foof/sdfse///// -> /foo/sdfse
  379. */
  380. if (path_length > 0 && yaffsfs_IsPathDivider(path[path_length - 1])) {
  381. alt_path = kmalloc(path_length + 1, 0);
  382. if (!alt_path)
  383. return -1;
  384. yaffs_strcpy(alt_path, path);
  385. for (i = path_length - 1;
  386. i >= 0 && yaffsfs_IsPathDivider(alt_path[i]); i--)
  387. alt_path[i] = (YCHAR) 0;
  388. }
  389. *ret_path = alt_path;
  390. return 0;
  391. }
  392. LIST_HEAD(yaffsfs_deviceList);
  393. /*
  394. * yaffsfs_FindDevice
  395. * yaffsfs_FindRoot
  396. * Scan the configuration list to find the device
  397. * Curveballs: Should match paths that end in '/' too
  398. * Curveball2 Might have "/x/ and "/x/y". Need to return the longest match
  399. */
  400. static struct yaffs_dev *yaffsfs_FindDevice(const YCHAR *path,
  401. YCHAR **restOfPath)
  402. {
  403. struct list_head *cfg;
  404. const YCHAR *leftOver;
  405. const YCHAR *p;
  406. struct yaffs_dev *retval = NULL;
  407. struct yaffs_dev *dev = NULL;
  408. int thisMatchLength;
  409. int longestMatch = -1;
  410. int matching;
  411. /*
  412. * Check all configs, choose the one that:
  413. * 1) Actually matches a prefix (ie /a amd /abc will not match
  414. * 2) Matches the longest.
  415. */
  416. list_for_each(cfg, &yaffsfs_deviceList) {
  417. dev = list_entry(cfg, struct yaffs_dev, dev_list);
  418. leftOver = path;
  419. p = dev->param.name;
  420. thisMatchLength = 0;
  421. matching = 1;
  422. while (matching && *p && *leftOver) {
  423. /* Skip over any /s */
  424. while (yaffsfs_IsPathDivider(*p))
  425. p++;
  426. /* Skip over any /s */
  427. while (yaffsfs_IsPathDivider(*leftOver))
  428. leftOver++;
  429. /* Now match the text part */
  430. while (matching &&
  431. *p && !yaffsfs_IsPathDivider(*p) &&
  432. *leftOver && !yaffsfs_IsPathDivider(*leftOver)) {
  433. if (yaffsfs_Match(*p, *leftOver)) {
  434. p++;
  435. leftOver++;
  436. thisMatchLength++;
  437. } else {
  438. matching = 0;
  439. }
  440. }
  441. }
  442. /* Skip over any /s in leftOver */
  443. while (yaffsfs_IsPathDivider(*leftOver))
  444. leftOver++;
  445. /*Skip over any /s in p */
  446. while (yaffsfs_IsPathDivider(*p))
  447. p++;
  448. /* p should now be at the end of the string if fully matched */
  449. if (*p)
  450. matching = 0;
  451. if (matching && (thisMatchLength > longestMatch)) {
  452. /* Matched prefix */
  453. *restOfPath = (YCHAR *) leftOver;
  454. retval = dev;
  455. longestMatch = thisMatchLength;
  456. }
  457. }
  458. return retval;
  459. }
  460. static int yaffsfs_CheckPath(const YCHAR *path)
  461. {
  462. int n = 0;
  463. int divs = 0;
  464. while (*path && n < YAFFS_MAX_NAME_LENGTH && divs < 100) {
  465. if (yaffsfs_IsPathDivider(*path)) {
  466. n = 0;
  467. divs++;
  468. } else
  469. n++;
  470. path++;
  471. }
  472. return (*path) ? -1 : 0;
  473. }
  474. /* FindMountPoint only returns a dev entry if the path is a mount point */
  475. static struct yaffs_dev *yaffsfs_FindMountPoint(const YCHAR *path)
  476. {
  477. struct yaffs_dev *dev;
  478. YCHAR *restOfPath = NULL;
  479. dev = yaffsfs_FindDevice(path, &restOfPath);
  480. if (dev && restOfPath && *restOfPath)
  481. dev = NULL;
  482. return dev;
  483. }
  484. static struct yaffs_obj *yaffsfs_FindRoot(const YCHAR *path,
  485. YCHAR **restOfPath)
  486. {
  487. struct yaffs_dev *dev;
  488. dev = yaffsfs_FindDevice(path, restOfPath);
  489. if (dev && dev->is_mounted)
  490. return dev->root_dir;
  491. return NULL;
  492. }
  493. static struct yaffs_obj *yaffsfs_FollowLink(struct yaffs_obj *obj,
  494. int symDepth, int *loop)
  495. {
  496. if (obj)
  497. obj = yaffs_get_equivalent_obj(obj);
  498. while (obj && obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK) {
  499. YCHAR *alias = obj->variant.symlink_variant.alias;
  500. if (yaffsfs_IsPathDivider(*alias))
  501. /* Starts with a /, need to scan from root up */
  502. obj = yaffsfs_FindObject(NULL, alias, symDepth++,
  503. 1, NULL, NULL, loop);
  504. else
  505. /*
  506. * Relative to here so use the parent of the
  507. * symlink as a start
  508. */
  509. obj = yaffsfs_FindObject(obj->parent, alias, symDepth++,
  510. 1, NULL, NULL, loop);
  511. }
  512. return obj;
  513. }
  514. /*
  515. * yaffsfs_FindDirectory
  516. * Parse a path to determine the directory and the name within the directory.
  517. *
  518. * eg. "/data/xx/ff" --> puts name="ff" and returns the directory "/data/xx"
  519. */
  520. static struct yaffs_obj *yaffsfs_DoFindDirectory(struct yaffs_obj *startDir,
  521. const YCHAR *path,
  522. YCHAR **name, int symDepth,
  523. int *notDir, int *loop)
  524. {
  525. struct yaffs_obj *dir;
  526. YCHAR *restOfPath;
  527. YCHAR str[YAFFS_MAX_NAME_LENGTH + 1];
  528. int i;
  529. if (symDepth > YAFFSFS_MAX_SYMLINK_DEREFERENCES) {
  530. if (loop)
  531. *loop = 1;
  532. return NULL;
  533. }
  534. if (startDir) {
  535. dir = startDir;
  536. restOfPath = (YCHAR *) path;
  537. } else
  538. dir = yaffsfs_FindRoot(path, &restOfPath);
  539. while (dir) {
  540. /*
  541. * parse off /.
  542. * curve ball: also throw away surplus '/'
  543. * eg. "/ram/x////ff" gets treated the same as "/ram/x/ff"
  544. */
  545. while (yaffsfs_IsPathDivider(*restOfPath))
  546. restOfPath++; /* get rid of '/' */
  547. *name = restOfPath;
  548. i = 0;
  549. while (*restOfPath && !yaffsfs_IsPathDivider(*restOfPath)) {
  550. if (i < YAFFS_MAX_NAME_LENGTH) {
  551. str[i] = *restOfPath;
  552. str[i + 1] = '\0';
  553. i++;
  554. }
  555. restOfPath++;
  556. }
  557. if (!*restOfPath)
  558. /* got to the end of the string */
  559. return dir;
  560. else {
  561. if (yaffs_strcmp(str, _Y(".")) == 0) {
  562. /* Do nothing */
  563. } else if (yaffs_strcmp(str, _Y("..")) == 0) {
  564. dir = dir->parent;
  565. } else {
  566. dir = yaffs_find_by_name(dir, str);
  567. dir = yaffsfs_FollowLink(dir, symDepth, loop);
  568. if (dir && dir->variant_type !=
  569. YAFFS_OBJECT_TYPE_DIRECTORY) {
  570. if (notDir)
  571. *notDir = 1;
  572. dir = NULL;
  573. }
  574. }
  575. }
  576. }
  577. /* directory did not exist. */
  578. return NULL;
  579. }
  580. static struct yaffs_obj *yaffsfs_FindDirectory(struct yaffs_obj *relDir,
  581. const YCHAR *path,
  582. YCHAR **name,
  583. int symDepth,
  584. int *notDir, int *loop)
  585. {
  586. return yaffsfs_DoFindDirectory(relDir, path, name, symDepth, notDir,
  587. loop);
  588. }
  589. /*
  590. * yaffsfs_FindObject turns a path for an existing object into the object
  591. */
  592. static struct yaffs_obj *yaffsfs_FindObject(struct yaffs_obj *relDir,
  593. const YCHAR *path, int symDepth,
  594. int getEquiv,
  595. struct yaffs_obj **dirOut,
  596. int *notDir, int *loop)
  597. {
  598. struct yaffs_obj *dir;
  599. struct yaffs_obj *obj;
  600. YCHAR *name;
  601. dir =
  602. yaffsfs_FindDirectory(relDir, path, &name, symDepth, notDir, loop);
  603. if (dirOut)
  604. *dirOut = dir;
  605. if (dir && *name)
  606. obj = yaffs_find_by_name(dir, name);
  607. else
  608. obj = dir;
  609. if (getEquiv)
  610. obj = yaffs_get_equivalent_obj(obj);
  611. return obj;
  612. }
  613. /*************************************************************************
  614. * Start of yaffsfs visible functions.
  615. *************************************************************************/
  616. int yaffs_dup(int handle)
  617. {
  618. int newHandleNumber = -1;
  619. struct yaffsfs_FileDes *existingFD = NULL;
  620. struct yaffsfs_Handle *existingHandle = NULL;
  621. struct yaffsfs_Handle *newHandle = NULL;
  622. yaffsfs_Lock();
  623. existingHandle = yaffsfs_HandleToPointer(handle);
  624. existingFD = yaffsfs_HandleToFileDes(handle);
  625. if (existingFD)
  626. newHandleNumber = yaffsfs_NewHandle(&newHandle);
  627. if (newHandle) {
  628. newHandle->fdId = existingHandle->fdId;
  629. existingFD->handleCount++;
  630. }
  631. yaffsfs_Unlock();
  632. if (!existingFD)
  633. yaffsfs_SetError(-EBADF);
  634. else if (!newHandle)
  635. yaffsfs_SetError(-ENOMEM);
  636. return newHandleNumber;
  637. }
  638. static int yaffsfs_TooManyObjects(struct yaffs_dev *dev)
  639. {
  640. int current_objects = dev->n_obj - dev->n_deleted_files;
  641. if (dev->param.max_objects && current_objects > dev->param.max_objects)
  642. return 1;
  643. else
  644. return 0;
  645. }
  646. int yaffs_open_sharing(const YCHAR *path, int oflag, int mode, int sharing)
  647. {
  648. struct yaffs_obj *obj = NULL;
  649. struct yaffs_obj *dir = NULL;
  650. YCHAR *name;
  651. int handle = -1;
  652. struct yaffsfs_FileDes *fd = NULL;
  653. int openDenied = 0;
  654. int symDepth = 0;
  655. int errorReported = 0;
  656. int rwflags = oflag & (O_RDWR | O_RDONLY | O_WRONLY);
  657. u8 shareRead = (sharing & YAFFS_SHARE_READ) ? 1 : 0;
  658. u8 shareWrite = (sharing & YAFFS_SHARE_WRITE) ? 1 : 0;
  659. u8 sharedReadAllowed;
  660. u8 sharedWriteAllowed;
  661. u8 alreadyReading;
  662. u8 alreadyWriting;
  663. u8 readRequested;
  664. u8 writeRequested;
  665. int notDir = 0;
  666. int loop = 0;
  667. if (!path) {
  668. yaffsfs_SetError(-EFAULT);
  669. return -1;
  670. }
  671. if (yaffsfs_CheckPath(path) < 0) {
  672. yaffsfs_SetError(-ENAMETOOLONG);
  673. return -1;
  674. }
  675. /* O_EXCL only has meaning if O_CREAT is specified */
  676. if (!(oflag & O_CREAT))
  677. oflag &= ~(O_EXCL);
  678. /* O_TRUNC has no meaning if (O_CREAT | O_EXCL) is specified */
  679. if ((oflag & O_CREAT) & (oflag & O_EXCL))
  680. oflag &= ~(O_TRUNC);
  681. /* Todo: Are there any more flag combos to sanitise ? */
  682. /* Figure out if reading or writing is requested */
  683. readRequested = (rwflags == O_RDWR || rwflags == O_RDONLY) ? 1 : 0;
  684. writeRequested = (rwflags == O_RDWR || rwflags == O_WRONLY) ? 1 : 0;
  685. yaffsfs_Lock();
  686. handle = yaffsfs_NewHandleAndFileDes();
  687. if (handle < 0) {
  688. yaffsfs_SetError(-ENFILE);
  689. errorReported = 1;
  690. } else {
  691. fd = yaffsfs_HandleToFileDes(handle);
  692. /* try to find the exisiting object */
  693. obj = yaffsfs_FindObject(NULL, path, 0, 1, NULL, NULL, NULL);
  694. obj = yaffsfs_FollowLink(obj, symDepth++, &loop);
  695. if (obj &&
  696. obj->variant_type != YAFFS_OBJECT_TYPE_FILE &&
  697. obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
  698. obj = NULL;
  699. if (obj) {
  700. /* The file already exists or it might be a directory */
  701. /* A directory can't be opened as a file */
  702. if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
  703. openDenied = 1;
  704. yaffsfs_SetError(-EISDIR);
  705. errorReported = 1;
  706. }
  707. /* Open should fail if O_CREAT and O_EXCL are specified
  708. * for a file that exists.
  709. */
  710. if (!errorReported &&
  711. (oflag & O_EXCL) && (oflag & O_CREAT)) {
  712. openDenied = 1;
  713. yaffsfs_SetError(-EEXIST);
  714. errorReported = 1;
  715. }
  716. /* Check file permissions */
  717. if (readRequested && !(obj->yst_mode & S_IREAD))
  718. openDenied = 1;
  719. if (writeRequested && !(obj->yst_mode & S_IWRITE))
  720. openDenied = 1;
  721. if (!errorReported && writeRequested &&
  722. obj->my_dev->read_only) {
  723. openDenied = 1;
  724. yaffsfs_SetError(-EROFS);
  725. errorReported = 1;
  726. }
  727. if (openDenied && !errorReported) {
  728. yaffsfs_SetError(-EACCES);
  729. errorReported = 1;
  730. }
  731. /* Check sharing of an existing object. */
  732. if (!openDenied) {
  733. struct yaffsfs_FileDes *fdx;
  734. int i;
  735. sharedReadAllowed = 1;
  736. sharedWriteAllowed = 1;
  737. alreadyReading = 0;
  738. alreadyWriting = 0;
  739. for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
  740. fdx = &yaffsfs_fd[i];
  741. if (fdx->handleCount > 0 &&
  742. fdx->inodeId >= 0 &&
  743. yaffsfs_inode[fdx->inodeId].iObj
  744. == obj) {
  745. if (!fdx->shareRead)
  746. sharedReadAllowed = 0;
  747. if (!fdx->shareWrite)
  748. sharedWriteAllowed = 0;
  749. if (fdx->reading)
  750. alreadyReading = 1;
  751. if (fdx->writing)
  752. alreadyWriting = 1;
  753. }
  754. }
  755. if ((!sharedReadAllowed && readRequested) ||
  756. (!shareRead && alreadyReading) ||
  757. (!sharedWriteAllowed && writeRequested) ||
  758. (!shareWrite && alreadyWriting)) {
  759. openDenied = 1;
  760. yaffsfs_SetError(-EBUSY);
  761. errorReported = 1;
  762. }
  763. }
  764. }
  765. /* If we could not open an existing object, then let's see if
  766. * the directory exists. If not, error.
  767. */
  768. if (!obj && !errorReported) {
  769. dir = yaffsfs_FindDirectory(NULL, path, &name, 0,
  770. &notDir, &loop);
  771. if (!dir && notDir) {
  772. yaffsfs_SetError(-ENOTDIR);
  773. errorReported = 1;
  774. } else if (loop) {
  775. yaffsfs_SetError(-ELOOP);
  776. errorReported = 1;
  777. } else if (!dir) {
  778. yaffsfs_SetError(-ENOENT);
  779. errorReported = 1;
  780. }
  781. }
  782. if (!obj && dir && !errorReported && (oflag & O_CREAT)) {
  783. /* Let's see if we can create this file */
  784. if (dir->my_dev->read_only) {
  785. yaffsfs_SetError(-EROFS);
  786. errorReported = 1;
  787. } else if (yaffsfs_TooManyObjects(dir->my_dev)) {
  788. yaffsfs_SetError(-ENFILE);
  789. errorReported = 1;
  790. } else
  791. obj = yaffs_create_file(dir, name, mode, 0, 0);
  792. if (!obj && !errorReported) {
  793. yaffsfs_SetError(-ENOSPC);
  794. errorReported = 1;
  795. }
  796. }
  797. if (!obj && dir && !errorReported && !(oflag & O_CREAT)) {
  798. yaffsfs_SetError(-ENOENT);
  799. errorReported = 1;
  800. }
  801. if (obj && !openDenied) {
  802. int inodeId = yaffsfs_GetInodeIdForObject(obj);
  803. if (inodeId < 0) {
  804. /*
  805. * Todo: Fix any problem if inodes run out,
  806. * That can't happen if the number of inode
  807. * items >= number of handles.
  808. */
  809. }
  810. fd->inodeId = inodeId;
  811. fd->reading = readRequested;
  812. fd->writing = writeRequested;
  813. fd->append = (oflag & O_APPEND) ? 1 : 0;
  814. fd->position = 0;
  815. fd->shareRead = shareRead;
  816. fd->shareWrite = shareWrite;
  817. /* Hook inode to object */
  818. obj->my_inode = (void *)&yaffsfs_inode[inodeId];
  819. if ((oflag & O_TRUNC) && fd->writing)
  820. yaffs_resize_file(obj, 0);
  821. } else {
  822. yaffsfs_PutHandle(handle);
  823. if (!errorReported)
  824. yaffsfs_SetError(0); /* Problem */
  825. handle = -1;
  826. }
  827. }
  828. yaffsfs_Unlock();
  829. return handle;
  830. }
  831. int yaffs_open(const YCHAR *path, int oflag, int mode)
  832. {
  833. return yaffs_open_sharing(path, oflag, mode,
  834. YAFFS_SHARE_READ | YAFFS_SHARE_WRITE);
  835. }
  836. int yaffs_Dofsync(int handle, int datasync)
  837. {
  838. int retVal = -1;
  839. struct yaffs_obj *obj;
  840. yaffsfs_Lock();
  841. obj = yaffsfs_HandleToObject(handle);
  842. if (!obj)
  843. yaffsfs_SetError(-EBADF);
  844. else if (obj->my_dev->read_only)
  845. yaffsfs_SetError(-EROFS);
  846. else {
  847. yaffs_flush_file(obj, 1, datasync);
  848. retVal = 0;
  849. }
  850. yaffsfs_Unlock();
  851. return retVal;
  852. }
  853. int yaffs_fsync(int handle)
  854. {
  855. return yaffs_Dofsync(handle, 0);
  856. }
  857. int yaffs_flush(int handle)
  858. {
  859. return yaffs_fsync(handle);
  860. }
  861. int yaffs_fdatasync(int handle)
  862. {
  863. return yaffs_Dofsync(handle, 1);
  864. }
  865. int yaffs_close(int handle)
  866. {
  867. struct yaffsfs_Handle *h = NULL;
  868. struct yaffs_obj *obj = NULL;
  869. int retVal = -1;
  870. yaffsfs_Lock();
  871. h = yaffsfs_HandleToPointer(handle);
  872. obj = yaffsfs_HandleToObject(handle);
  873. if (!h || !obj)
  874. yaffsfs_SetError(-EBADF);
  875. else {
  876. /* clean up */
  877. yaffs_flush_file(obj, 1, 0);
  878. yaffsfs_PutHandle(handle);
  879. retVal = 0;
  880. }
  881. yaffsfs_Unlock();
  882. return retVal;
  883. }
  884. int yaffsfs_do_read(int handle, void *vbuf, unsigned int nbyte,
  885. int isPread, loff_t offset)
  886. {
  887. struct yaffsfs_FileDes *fd = NULL;
  888. struct yaffs_obj *obj = NULL;
  889. loff_t pos = 0;
  890. loff_t startPos = 0;
  891. loff_t endPos = 0;
  892. int nRead = 0;
  893. int nToRead = 0;
  894. int totalRead = 0;
  895. loff_t maxRead;
  896. u8 *buf = (u8 *) vbuf;
  897. if (!vbuf) {
  898. yaffsfs_SetError(-EFAULT);
  899. return -1;
  900. }
  901. yaffsfs_Lock();
  902. fd = yaffsfs_HandleToFileDes(handle);
  903. obj = yaffsfs_HandleToObject(handle);
  904. if (!fd || !obj) {
  905. /* bad handle */
  906. yaffsfs_SetError(-EBADF);
  907. totalRead = -1;
  908. } else if (!fd->reading) {
  909. /* Not a reading handle */
  910. yaffsfs_SetError(-EINVAL);
  911. totalRead = -1;
  912. } else if (nbyte > YAFFS_MAX_FILE_SIZE) {
  913. yaffsfs_SetError(-EINVAL);
  914. totalRead = -1;
  915. } else {
  916. if (isPread)
  917. startPos = offset;
  918. else
  919. startPos = fd->position;
  920. pos = startPos;
  921. if (yaffs_get_obj_length(obj) > pos)
  922. maxRead = yaffs_get_obj_length(obj) - pos;
  923. else
  924. maxRead = 0;
  925. if (nbyte > maxRead)
  926. nbyte = maxRead;
  927. yaffsfs_GetHandle(handle);
  928. endPos = pos + nbyte;
  929. if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
  930. nbyte > YAFFS_MAX_FILE_SIZE ||
  931. endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
  932. totalRead = -1;
  933. nbyte = 0;
  934. }
  935. while (nbyte > 0) {
  936. nToRead = YAFFSFS_RW_SIZE -
  937. (pos & (YAFFSFS_RW_SIZE - 1));
  938. if (nToRead > nbyte)
  939. nToRead = nbyte;
  940. /* Tricky bit...
  941. * Need to reverify object in case the device was
  942. * unmounted in another thread.
  943. */
  944. obj = yaffsfs_HandleToObject(handle);
  945. if (!obj)
  946. nRead = 0;
  947. else
  948. nRead = yaffs_file_rd(obj, buf, pos, nToRead);
  949. if (nRead > 0) {
  950. totalRead += nRead;
  951. pos += nRead;
  952. buf += nRead;
  953. }
  954. if (nRead == nToRead)
  955. nbyte -= nRead;
  956. else
  957. nbyte = 0; /* no more to read */
  958. if (nbyte > 0) {
  959. yaffsfs_Unlock();
  960. yaffsfs_Lock();
  961. }
  962. }
  963. yaffsfs_PutHandle(handle);
  964. if (!isPread) {
  965. if (totalRead >= 0)
  966. fd->position = startPos + totalRead;
  967. else
  968. yaffsfs_SetError(-EINVAL);
  969. }
  970. }
  971. yaffsfs_Unlock();
  972. return (totalRead >= 0) ? totalRead : -1;
  973. }
  974. int yaffs_read(int handle, void *buf, unsigned int nbyte)
  975. {
  976. return yaffsfs_do_read(handle, buf, nbyte, 0, 0);
  977. }
  978. int yaffs_pread(int handle, void *buf, unsigned int nbyte, loff_t offset)
  979. {
  980. return yaffsfs_do_read(handle, buf, nbyte, 1, offset);
  981. }
  982. int yaffsfs_do_write(int handle, const void *vbuf, unsigned int nbyte,
  983. int isPwrite, loff_t offset)
  984. {
  985. struct yaffsfs_FileDes *fd = NULL;
  986. struct yaffs_obj *obj = NULL;
  987. loff_t pos = 0;
  988. loff_t startPos = 0;
  989. loff_t endPos;
  990. int nWritten = 0;
  991. int totalWritten = 0;
  992. int write_trhrough = 0;
  993. int nToWrite = 0;
  994. const u8 *buf = (const u8 *)vbuf;
  995. if (!vbuf) {
  996. yaffsfs_SetError(-EFAULT);
  997. return -1;
  998. }
  999. yaffsfs_Lock();
  1000. fd = yaffsfs_HandleToFileDes(handle);
  1001. obj = yaffsfs_HandleToObject(handle);
  1002. if (!fd || !obj) {
  1003. /* bad handle */
  1004. yaffsfs_SetError(-EBADF);
  1005. totalWritten = -1;
  1006. } else if (!fd->writing) {
  1007. yaffsfs_SetError(-EINVAL);
  1008. totalWritten = -1;
  1009. } else if (obj->my_dev->read_only) {
  1010. yaffsfs_SetError(-EROFS);
  1011. totalWritten = -1;
  1012. } else {
  1013. if (fd->append)
  1014. startPos = yaffs_get_obj_length(obj);
  1015. else if (isPwrite)
  1016. startPos = offset;
  1017. else
  1018. startPos = fd->position;
  1019. yaffsfs_GetHandle(handle);
  1020. pos = startPos;
  1021. endPos = pos + nbyte;
  1022. if (pos < 0 || pos > YAFFS_MAX_FILE_SIZE ||
  1023. nbyte > YAFFS_MAX_FILE_SIZE ||
  1024. endPos < 0 || endPos > YAFFS_MAX_FILE_SIZE) {
  1025. totalWritten = -1;
  1026. nbyte = 0;
  1027. }
  1028. while (nbyte > 0) {
  1029. nToWrite = YAFFSFS_RW_SIZE -
  1030. (pos & (YAFFSFS_RW_SIZE - 1));
  1031. if (nToWrite > nbyte)
  1032. nToWrite = nbyte;
  1033. /* Tricky bit...
  1034. * Need to reverify object in case the device was
  1035. * remounted or unmounted in another thread.
  1036. */
  1037. obj = yaffsfs_HandleToObject(handle);
  1038. if (!obj || obj->my_dev->read_only)
  1039. nWritten = 0;
  1040. else
  1041. nWritten =
  1042. yaffs_wr_file(obj, buf, pos, nToWrite,
  1043. write_trhrough);
  1044. if (nWritten > 0) {
  1045. totalWritten += nWritten;
  1046. pos += nWritten;
  1047. buf += nWritten;
  1048. }
  1049. if (nWritten == nToWrite)
  1050. nbyte -= nToWrite;
  1051. else
  1052. nbyte = 0;
  1053. if (nWritten < 1 && totalWritten < 1) {
  1054. yaffsfs_SetError(-ENOSPC);
  1055. totalWritten = -1;
  1056. }
  1057. if (nbyte > 0) {
  1058. yaffsfs_Unlock();
  1059. yaffsfs_Lock();
  1060. }
  1061. }
  1062. yaffsfs_PutHandle(handle);
  1063. if (!isPwrite) {
  1064. if (totalWritten > 0)
  1065. fd->position = startPos + totalWritten;
  1066. else
  1067. yaffsfs_SetError(-EINVAL);
  1068. }
  1069. }
  1070. yaffsfs_Unlock();
  1071. return (totalWritten >= 0) ? totalWritten : -1;
  1072. }
  1073. int yaffs_write(int fd, const void *buf, unsigned int nbyte)
  1074. {
  1075. return yaffsfs_do_write(fd, buf, nbyte, 0, 0);
  1076. }
  1077. int yaffs_pwrite(int fd, const void *buf, unsigned int nbyte, loff_t offset)
  1078. {
  1079. return yaffsfs_do_write(fd, buf, nbyte, 1, offset);
  1080. }
  1081. int yaffs_truncate(const YCHAR *path, loff_t new_size)
  1082. {
  1083. struct yaffs_obj *obj = NULL;
  1084. struct yaffs_obj *dir = NULL;
  1085. int result = YAFFS_FAIL;
  1086. int notDir = 0;
  1087. int loop = 0;
  1088. if (!path) {
  1089. yaffsfs_SetError(-EFAULT);
  1090. return -1;
  1091. }
  1092. if (yaffsfs_CheckPath(path) < 0) {
  1093. yaffsfs_SetError(-ENAMETOOLONG);
  1094. return -1;
  1095. }
  1096. yaffsfs_Lock();
  1097. obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
  1098. obj = yaffsfs_FollowLink(obj, 0, &loop);
  1099. if (!dir && notDir)
  1100. yaffsfs_SetError(-ENOTDIR);
  1101. else if (loop)
  1102. yaffsfs_SetError(-ELOOP);
  1103. else if (!dir || !obj)
  1104. yaffsfs_SetError(-ENOENT);
  1105. else if (obj->my_dev->read_only)
  1106. yaffsfs_SetError(-EROFS);
  1107. else if (obj->variant_type != YAFFS_OBJECT_TYPE_FILE)
  1108. yaffsfs_SetError(-EISDIR);
  1109. else if (obj->my_dev->read_only)
  1110. yaffsfs_SetError(-EROFS);
  1111. else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
  1112. yaffsfs_SetError(-EINVAL);
  1113. else
  1114. result = yaffs_resize_file(obj, new_size);
  1115. yaffsfs_Unlock();
  1116. return (result) ? 0 : -1;
  1117. }
  1118. int yaffs_ftruncate(int handle, loff_t new_size)
  1119. {
  1120. struct yaffsfs_FileDes *fd = NULL;
  1121. struct yaffs_obj *obj = NULL;
  1122. int result = 0;
  1123. yaffsfs_Lock();
  1124. fd = yaffsfs_HandleToFileDes(handle);
  1125. obj = yaffsfs_HandleToObject(handle);
  1126. if (!fd || !obj)
  1127. /* bad handle */
  1128. yaffsfs_SetError(-EBADF);
  1129. else if (!fd->writing)
  1130. yaffsfs_SetError(-EINVAL);
  1131. else if (obj->my_dev->read_only)
  1132. yaffsfs_SetError(-EROFS);
  1133. else if (new_size < 0 || new_size > YAFFS_MAX_FILE_SIZE)
  1134. yaffsfs_SetError(-EINVAL);
  1135. else
  1136. /* resize the file */
  1137. result = yaffs_resize_file(obj, new_size);
  1138. yaffsfs_Unlock();
  1139. return (result) ? 0 : -1;
  1140. }
  1141. loff_t yaffs_lseek(int handle, loff_t offset, int whence)
  1142. {
  1143. struct yaffsfs_FileDes *fd = NULL;
  1144. struct yaffs_obj *obj = NULL;
  1145. loff_t pos = -1;
  1146. loff_t fSize = -1;
  1147. yaffsfs_Lock();
  1148. fd = yaffsfs_HandleToFileDes(handle);
  1149. obj = yaffsfs_HandleToObject(handle);
  1150. if (!fd || !obj)
  1151. yaffsfs_SetError(-EBADF);
  1152. else if (offset > YAFFS_MAX_FILE_SIZE)
  1153. yaffsfs_SetError(-EINVAL);
  1154. else {
  1155. if (whence == SEEK_SET) {
  1156. if (offset >= 0)
  1157. pos = offset;
  1158. } else if (whence == SEEK_CUR) {
  1159. if ((fd->position + offset) >= 0)
  1160. pos = (fd->position + offset);
  1161. } else if (whence == SEEK_END) {
  1162. fSize = yaffs_get_obj_length(obj);
  1163. if (fSize >= 0 && (fSize + offset) >= 0)
  1164. pos = fSize + offset;
  1165. }
  1166. if (pos >= 0 && pos <= YAFFS_MAX_FILE_SIZE)
  1167. fd->position = pos;
  1168. else {
  1169. yaffsfs_SetError(-EINVAL);
  1170. pos = -1;
  1171. }
  1172. }
  1173. yaffsfs_Unlock();
  1174. return pos;
  1175. }
  1176. int yaffsfs_DoUnlink(const YCHAR *path, int isDirectory)
  1177. {
  1178. struct yaffs_obj *dir = NULL;
  1179. struct yaffs_obj *obj = NULL;
  1180. YCHAR *name;
  1181. int result = YAFFS_FAIL;
  1182. int notDir = 0;
  1183. int loop = 0;
  1184. if (!path) {
  1185. yaffsfs_SetError(-EFAULT);
  1186. return -1;
  1187. }
  1188. if (yaffsfs_CheckPath(path) < 0) {
  1189. yaffsfs_SetError(-ENAMETOOLONG);
  1190. return -1;
  1191. }
  1192. yaffsfs_Lock();
  1193. obj = yaffsfs_FindObject(NULL, path, 0, 0, NULL, NULL, NULL);
  1194. dir = yaffsfs_FindDirectory(NULL, path, &name, 0, &notDir, &loop);
  1195. if (!dir && notDir)
  1196. yaffsfs_SetError(-ENOTDIR);
  1197. else if (loop)
  1198. yaffsfs_SetError(-ELOOP);
  1199. else if (!dir)
  1200. yaffsfs_SetError(-ENOENT);
  1201. else if (yaffs_strncmp(name, _Y("."), 2) == 0)
  1202. yaffsfs_SetError(-EINVAL);
  1203. else if (!obj)
  1204. yaffsfs_SetError(-ENOENT);
  1205. else if (obj->my_dev->read_only)
  1206. yaffsfs_SetError(-EROFS);
  1207. else if (!isDirectory &&
  1208. obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
  1209. yaffsfs_SetError(-EISDIR);
  1210. else if (isDirectory &&
  1211. obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
  1212. yaffsfs_SetError(-ENOTDIR);
  1213. else if (isDirectory && obj == obj->my_dev->root_dir)
  1214. yaffsfs_SetError(-EBUSY); /* Can't rmdir a root */
  1215. else {
  1216. result = yaffs_unlinker(dir, name);
  1217. if (result == YAFFS_FAIL && isDirectory)
  1218. yaffsfs_SetError(-ENOTEMPTY);
  1219. }
  1220. yaffsfs_Unlock();
  1221. return (result == YAFFS_FAIL) ? -1 : 0;
  1222. }
  1223. int yaffs_unlink(const YCHAR *path)
  1224. {
  1225. return yaffsfs_DoUnlink(path, 0);
  1226. }
  1227. int yaffs_rename(const YCHAR *oldPath, const YCHAR *newPath)
  1228. {
  1229. struct yaffs_obj *olddir = NULL;
  1230. struct yaffs_obj *newdir = NULL;
  1231. struct yaffs_obj *obj = NULL;
  1232. struct yaffs_obj *newobj = NULL;
  1233. YCHAR *oldname;
  1234. YCHAR *newname;
  1235. int result = YAFFS_FAIL;
  1236. int rename_allowed = 1;
  1237. int notOldDir = 0;
  1238. int notNewDir = 0;
  1239. int oldLoop = 0;
  1240. int newLoop = 0;
  1241. YCHAR *alt_newpath = NULL;
  1242. if (!oldPath || !newPath) {
  1243. yaffsfs_SetError(-EFAULT);
  1244. return -1;
  1245. }
  1246. if (yaffsfs_CheckPath(oldPath) < 0 || yaffsfs_CheckPath(newPath) < 0) {
  1247. yaffsfs_SetError(-ENAMETOOLONG);
  1248. return -1;
  1249. }
  1250. if (yaffsfs_alt_dir_path(newPath, &alt_newpath) < 0) {
  1251. yaffsfs_SetError(-ENOMEM);
  1252. return -1;
  1253. }
  1254. if (alt_newpath)
  1255. newPath = alt_newpath;
  1256. yaffsfs_Lock();
  1257. olddir = yaffsfs_FindDirectory(NULL, oldPath, &oldname, 0,
  1258. &notOldDir, &oldLoop);
  1259. newdir = yaffsfs_FindDirectory(NULL, newPath, &newname, 0,
  1260. &notNewDir, &newLoop);
  1261. obj = yaffsfs_FindObject(NULL, oldPath, 0, 0, NULL, NULL, NULL);
  1262. newobj = yaffsfs_FindObject(NULL, newPath, 0, 0, NULL, NULL, NULL);
  1263. /* If the object being renamed is a directory and the
  1264. * path ended with a "/" then the olddir == obj.
  1265. * We pass through NULL for the old name to tell the lower layers
  1266. * to use olddir as the object.
  1267. */
  1268. if (olddir == obj)
  1269. oldname = NULL;
  1270. if ((!olddir && notOldDir) || (!newdir && notNewDir)) {
  1271. yaffsfs_SetError(-ENOTDIR);
  1272. rename_allowed = 0;
  1273. } else if (oldLoop || newLoop) {
  1274. yaffsfs_SetError(-ELOOP);
  1275. rename_allowed = 0;
  1276. } else if (olddir && oldname &&
  1277. yaffs_strncmp(oldname, _Y("."), 2) == 0) {
  1278. yaffsfs_SetError(-EINVAL);
  1279. rename_allowed = 0;
  1280. } else if (!olddir || !newdir || !obj) {
  1281. yaffsfs_SetError(-ENOENT);
  1282. rename_allowed = 0;
  1283. } else if (obj->my_dev->read_only) {
  1284. yaffsfs_SetError(-EROFS);
  1285. rename_allowed = 0;
  1286. } else if (yaffs_is_non_empty_dir(newobj)) {
  1287. yaffsfs_SetError(-ENOTEMPTY);
  1288. rename_allowed = 0;
  1289. } else if (olddir->my_dev != newdir->my_dev) {
  1290. /* Rename must be on same device */
  1291. yaffsfs_SetError(-EXDEV);
  1292. rename_allowed = 0;
  1293. } else if (obj && obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
  1294. /*
  1295. * It is a directory, check that it is not being renamed to
  1296. * being its own decendent.
  1297. * Do this by tracing from the new directory back to the root,
  1298. * checking for obj
  1299. */
  1300. struct yaffs_obj *xx = newdir;
  1301. while (rename_allowed && xx) {
  1302. if (xx == obj)
  1303. rename_allowed = 0;
  1304. xx = xx->parent;
  1305. }
  1306. if (!rename_allowed)
  1307. yaffsfs_SetError(-EINVAL);
  1308. }
  1309. if (rename_allowed)
  1310. result = yaffs_rename_obj(olddir, oldname, newdir, newname);
  1311. yaffsfs_Unlock();
  1312. kfree(alt_newpath);
  1313. return (result == YAFFS_FAIL) ? -1 : 0;
  1314. }
  1315. static int yaffsfs_DoStat(struct yaffs_obj *obj, struct yaffs_stat *buf)
  1316. {
  1317. int retVal = -1;
  1318. obj = yaffs_get_equivalent_obj(obj);
  1319. if (obj && buf) {
  1320. buf->st_dev = (int)obj->my_dev->os_context;
  1321. buf->st_ino = obj->obj_id;
  1322. buf->st_mode = obj->yst_mode & ~S_IFMT;
  1323. if (obj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY)
  1324. buf->st_mode |= S_IFDIR;
  1325. else if (obj->variant_type == YAFFS_OBJECT_TYPE_SYMLINK)
  1326. buf->st_mode |= S_IFLNK;
  1327. else if (obj->variant_type == YAFFS_OBJECT_TYPE_FILE)
  1328. buf->st_mode |= S_IFREG;
  1329. buf->st_nlink = yaffs_get_obj_link_count(obj);
  1330. buf->st_uid = 0;
  1331. buf->st_gid = 0;
  1332. buf->st_rdev = obj->yst_rdev;
  1333. buf->st_size = yaffs_get_obj_length(obj);
  1334. buf->st_blksize = obj->my_dev->data_bytes_per_chunk;
  1335. buf->st_blocks = lldiv(buf->st_size + buf->st_blksize - 1,
  1336. buf->st_blksize);
  1337. #if CONFIG_YAFFS_WINCE
  1338. buf->yst_wince_atime[0] = obj->win_atime[0];
  1339. buf->yst_wince_atime[1] = obj->win_atime[1];
  1340. buf->yst_wince_ctime[0] = obj->win_ctime[0];
  1341. buf->yst_wince_ctime[1] = obj->win_ctime[1];
  1342. buf->yst_wince_mtime[0] = obj->win_mtime[0];
  1343. buf->yst_wince_mtime[1] = obj->win_mtime[1];
  1344. #else
  1345. buf->yst_atime = obj->yst_atime;
  1346. buf->yst_ctime = obj->yst_ctime;
  1347. buf->yst_mtime = obj->yst_mtime;
  1348. #endif
  1349. retVal = 0;
  1350. }
  1351. return retVal;
  1352. }
  1353. static int yaffsfs_DoStatOrLStat(const YCHAR *path,
  1354. struct yaffs_stat *buf, int doLStat)
  1355. {
  1356. struct yaffs_obj *obj = NULL;
  1357. struct yaffs_obj *dir = NULL;
  1358. int retVal = -1;
  1359. int notDir = 0;
  1360. int loop = 0;
  1361. if (!path || !buf) {
  1362. yaffsfs_SetError(-EFAULT);
  1363. return -1;
  1364. }
  1365. if (yaffsfs_CheckPath(path) < 0) {
  1366. yaffsfs_SetError(-ENAMETOOLONG);
  1367. return -1;
  1368. }
  1369. yaffsfs_Lock();
  1370. obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
  1371. if (!doLStat && obj)
  1372. obj = yaffsfs_FollowLink(obj, 0, &loop);
  1373. if (!dir && notDir)
  1374. yaffsfs_SetError(-ENOTDIR);
  1375. else if (loop)
  1376. yaffsfs_SetError(-ELOOP);
  1377. else if (!dir || !obj)
  1378. yaffsfs_SetError(-ENOENT);
  1379. else
  1380. retVal = yaffsfs_DoStat(obj, buf);
  1381. yaffsfs_Unlock();
  1382. return retVal;
  1383. }
  1384. int yaffs_stat(const YCHAR *path, struct yaffs_stat *buf)
  1385. {
  1386. return yaffsfs_DoStatOrLStat(path, buf, 0);
  1387. }
  1388. int yaffs_lstat(const YCHAR *path, struct yaffs_stat *buf)
  1389. {
  1390. return yaffsfs_DoStatOrLStat(path, buf, 1);
  1391. }
  1392. int yaffs_fstat(int fd, struct yaffs_stat *buf)
  1393. {
  1394. struct yaffs_obj *obj;
  1395. int retVal = -1;
  1396. if (!buf) {
  1397. yaffsfs_SetError(-EFAULT);
  1398. return -1;
  1399. }
  1400. yaffsfs_Lock();
  1401. obj = yaffsfs_HandleToObject(fd);
  1402. if (obj)
  1403. retVal = yaffsfs_DoStat(obj, buf);
  1404. else
  1405. /* bad handle */
  1406. yaffsfs_SetError(-EBADF);
  1407. yaffsfs_Unlock();
  1408. return retVal;
  1409. }
  1410. static int yaffsfs_DoUtime(struct yaffs_obj *obj,
  1411. const struct yaffs_utimbuf *buf)
  1412. {
  1413. int retVal = -1;
  1414. int result;
  1415. struct yaffs_utimbuf local;
  1416. obj = yaffs_get_equivalent_obj(obj);
  1417. if (obj && obj->my_dev->read_only) {
  1418. yaffsfs_SetError(-EROFS);
  1419. return -1;
  1420. }
  1421. if (!buf) {
  1422. local.actime = Y_CURRENT_TIME;
  1423. local.modtime = local.actime;
  1424. buf = &local;
  1425. }
  1426. if (obj) {
  1427. obj->yst_atime = buf->actime;
  1428. obj->yst_mtime = buf->modtime;
  1429. obj->dirty = 1;
  1430. result = yaffs_flush_file(obj, 0, 0);
  1431. retVal = result == YAFFS_OK ? 0 : -1;
  1432. }
  1433. return retVal;
  1434. }
  1435. int yaffs_utime(const YCHAR *path, const struct yaffs_utimbuf *buf)
  1436. {
  1437. struct yaffs_obj *obj = NULL;
  1438. struct yaffs_obj *dir = NULL;
  1439. int retVal = -1;
  1440. int notDir = 0;
  1441. int loop = 0;
  1442. if (!path) {
  1443. yaffsfs_SetError(-EFAULT);
  1444. return -1;
  1445. }
  1446. if (yaffsfs_CheckPath(path) < 0) {
  1447. yaffsfs_SetError(-ENAMETOOLONG);
  1448. return -1;
  1449. }
  1450. yaffsfs_Lock();
  1451. obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
  1452. if (!dir && notDir)
  1453. yaffsfs_SetError(-ENOTDIR);
  1454. else if (loop)
  1455. yaffsfs_SetError(-ELOOP);
  1456. else if (!dir || !obj)
  1457. yaffsfs_SetError(-ENOENT);
  1458. else
  1459. retVal = yaffsfs_DoUtime(obj, buf);
  1460. yaffsfs_Unlock();
  1461. return retVal;
  1462. }
  1463. int yaffs_futime(int fd, const struct yaffs_utimbuf *buf)
  1464. {
  1465. struct yaffs_obj *obj;
  1466. int retVal = -1;
  1467. yaffsfs_Lock();
  1468. obj = yaffsfs_HandleToObject(fd);
  1469. if (obj)
  1470. retVal = yaffsfs_DoUtime(obj, buf);
  1471. else
  1472. /* bad handle */
  1473. yaffsfs_SetError(-EBADF);
  1474. yaffsfs_Unlock();
  1475. return retVal;
  1476. }
  1477. #ifndef CONFIG_YAFFS_WINCE
  1478. /* xattrib functions */
  1479. static int yaffs_do_setxattr(const YCHAR *path, const char *name,
  1480. const void *data, int size, int flags, int follow)
  1481. {
  1482. struct yaffs_obj *obj;
  1483. struct yaffs_obj *dir;
  1484. int notDir = 0;
  1485. int loop = 0;
  1486. int retVal = -1;
  1487. if (!path || !name || !data) {
  1488. yaffsfs_SetError(-EFAULT);
  1489. return -1;
  1490. }
  1491. if (yaffsfs_CheckPath(path) < 0) {
  1492. yaffsfs_SetError(-ENAMETOOLONG);
  1493. return -1;
  1494. }
  1495. yaffsfs_Lock();
  1496. obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
  1497. if (follow)
  1498. obj = yaffsfs_FollowLink(obj, 0, &loop);
  1499. if (!dir && notDir)
  1500. yaffsfs_SetError(-ENOTDIR);
  1501. else if (loop)
  1502. yaffsfs_SetError(-ELOOP);
  1503. else if (!dir || !obj)
  1504. yaffsfs_SetError(-ENOENT);
  1505. else {
  1506. retVal = yaffs_set_xattrib(obj, name, data, size, flags);
  1507. if (retVal < 0) {
  1508. yaffsfs_SetError(retVal);
  1509. retVal = -1;
  1510. }
  1511. }
  1512. yaffsfs_Unlock();
  1513. return retVal;
  1514. }
  1515. int yaffs_setxattr(const YCHAR *path, const char *name,
  1516. const void *data, int size, int flags)
  1517. {
  1518. return yaffs_do_setxattr(path, name, data, size, flags, 1);
  1519. }
  1520. int yaffs_lsetxattr(const YCHAR *path, const char *name,
  1521. const void *data, int size, int flags)
  1522. {
  1523. return yaffs_do_setxattr(path, name, data, size, flags, 0);
  1524. }
  1525. int yaffs_fsetxattr(int fd, const char *name,
  1526. const void *data, int size, int flags)
  1527. {
  1528. struct yaffs_obj *obj;
  1529. int retVal = -1;
  1530. if (!name || !data) {
  1531. yaffsfs_SetError(-EFAULT);
  1532. return -1;
  1533. }
  1534. yaffsfs_Lock();
  1535. obj = yaffsfs_HandleToObject(fd);
  1536. if (!obj)
  1537. yaffsfs_SetError(-EBADF);
  1538. else {
  1539. retVal = yaffs_set_xattrib(obj, name, data, size, flags);
  1540. if (retVal < 0) {
  1541. yaffsfs_SetError(retVal);
  1542. retVal = -1;
  1543. }
  1544. }
  1545. yaffsfs_Unlock();
  1546. return retVal;
  1547. }
  1548. static int yaffs_do_getxattr(const YCHAR *path, const char *name,
  1549. void *data, int size, int follow)
  1550. {
  1551. struct yaffs_obj *obj;
  1552. struct yaffs_obj *dir;
  1553. int retVal = -1;
  1554. int notDir = 0;
  1555. int loop = 0;
  1556. if (!path || !name || !data) {
  1557. yaffsfs_SetError(-EFAULT);
  1558. return -1;
  1559. }
  1560. if (yaffsfs_CheckPath(path) < 0) {
  1561. yaffsfs_SetError(-ENAMETOOLONG);
  1562. return -1;
  1563. }
  1564. yaffsfs_Lock();
  1565. obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
  1566. if (follow)
  1567. obj = yaffsfs_FollowLink(obj, 0, &loop);
  1568. if (!dir && notDir)
  1569. yaffsfs_SetError(-ENOTDIR);
  1570. else if (loop)
  1571. yaffsfs_SetError(-ELOOP);
  1572. else if (!dir || !obj)
  1573. yaffsfs_SetError(-ENOENT);
  1574. else {
  1575. retVal = yaffs_get_xattrib(obj, name, data, size);
  1576. if (retVal < 0) {
  1577. yaffsfs_SetError(retVal);
  1578. retVal = -1;
  1579. }
  1580. }
  1581. yaffsfs_Unlock();
  1582. return retVal;
  1583. }
  1584. int yaffs_getxattr(const YCHAR *path, const char *name, void *data, int size)
  1585. {
  1586. return yaffs_do_getxattr(path, name, data, size, 1);
  1587. }
  1588. int yaffs_lgetxattr(const YCHAR *path, const char *name, void *data, int size)
  1589. {
  1590. return yaffs_do_getxattr(path, name, data, size, 0);
  1591. }
  1592. int yaffs_fgetxattr(int fd, const char *name, void *data, int size)
  1593. {
  1594. struct yaffs_obj *obj;
  1595. int retVal = -1;
  1596. if (!name || !data) {
  1597. yaffsfs_SetError(-EFAULT);
  1598. return -1;
  1599. }
  1600. yaffsfs_Lock();
  1601. obj = yaffsfs_HandleToObject(fd);
  1602. if (obj) {
  1603. retVal = yaffs_get_xattrib(obj, name, data, size);
  1604. if (retVal < 0) {
  1605. yaffsfs_SetError(retVal);
  1606. retVal = -1;
  1607. }
  1608. } else
  1609. /* bad handle */
  1610. yaffsfs_SetError(-EBADF);
  1611. yaffsfs_Unlock();
  1612. return retVal;
  1613. }
  1614. static int yaffs_do_listxattr(const YCHAR *path, char *data,
  1615. int size, int follow)
  1616. {
  1617. struct yaffs_obj *obj = NULL;
  1618. struct yaffs_obj *dir = NULL;
  1619. int retVal = -1;
  1620. int notDir = 0;
  1621. int loop = 0;
  1622. if (!path || !data) {
  1623. yaffsfs_SetError(-EFAULT);
  1624. return -1;
  1625. }
  1626. if (yaffsfs_CheckPath(path) < 0) {
  1627. yaffsfs_SetError(-ENAMETOOLONG);
  1628. return -1;
  1629. }
  1630. yaffsfs_Lock();
  1631. obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
  1632. if (follow)
  1633. obj = yaffsfs_FollowLink(obj, 0, &loop);
  1634. if (!dir && notDir)
  1635. yaffsfs_SetError(-ENOTDIR);
  1636. else if (loop)
  1637. yaffsfs_SetError(-ELOOP);
  1638. else if (!dir || !obj)
  1639. yaffsfs_SetError(-ENOENT);
  1640. else {
  1641. retVal = yaffs_list_xattrib(obj, data, size);
  1642. if (retVal < 0) {
  1643. yaffsfs_SetError(retVal);
  1644. retVal = -1;
  1645. }
  1646. }
  1647. yaffsfs_Unlock();
  1648. return retVal;
  1649. }
  1650. int yaffs_listxattr(const YCHAR *path, char *data, int size)
  1651. {
  1652. return yaffs_do_listxattr(path, data, size, 1);
  1653. }
  1654. int yaffs_llistxattr(const YCHAR *path, char *data, int size)
  1655. {
  1656. return yaffs_do_listxattr(path, data, size, 0);
  1657. }
  1658. int yaffs_flistxattr(int fd, char *data, int size)
  1659. {
  1660. struct yaffs_obj *obj;
  1661. int retVal = -1;
  1662. if (!data) {
  1663. yaffsfs_SetError(-EFAULT);
  1664. return -1;
  1665. }
  1666. yaffsfs_Lock();
  1667. obj = yaffsfs_HandleToObject(fd);
  1668. if (obj) {
  1669. retVal = yaffs_list_xattrib(obj, data, size);
  1670. if (retVal < 0) {
  1671. yaffsfs_SetError(retVal);
  1672. retVal = -1;
  1673. }
  1674. } else
  1675. /* bad handle */
  1676. yaffsfs_SetError(-EBADF);
  1677. yaffsfs_Unlock();
  1678. return retVal;
  1679. }
  1680. static int yaffs_do_removexattr(const YCHAR *path, const char *name,
  1681. int follow)
  1682. {
  1683. struct yaffs_obj *obj = NULL;
  1684. struct yaffs_obj *dir = NULL;
  1685. int notDir = 0;
  1686. int loop = 0;
  1687. int retVal = -1;
  1688. if (!path || !name) {
  1689. yaffsfs_SetError(-EFAULT);
  1690. return -1;
  1691. }
  1692. if (yaffsfs_CheckPath(path) < 0) {
  1693. yaffsfs_SetError(-ENAMETOOLONG);
  1694. return -1;
  1695. }
  1696. yaffsfs_Lock();
  1697. obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
  1698. if (follow)
  1699. obj = yaffsfs_FollowLink(obj, 0, &loop);
  1700. if (!dir && notDir)
  1701. yaffsfs_SetError(-ENOTDIR);
  1702. else if (loop)
  1703. yaffsfs_SetError(-ELOOP);
  1704. else if (!dir || !obj)
  1705. yaffsfs_SetError(-ENOENT);
  1706. else {
  1707. retVal = yaffs_remove_xattrib(obj, name);
  1708. if (retVal < 0) {
  1709. yaffsfs_SetError(retVal);
  1710. retVal = -1;
  1711. }
  1712. }
  1713. yaffsfs_Unlock();
  1714. return retVal;
  1715. }
  1716. int yaffs_removexattr(const YCHAR *path, const char *name)
  1717. {
  1718. return yaffs_do_removexattr(path, name, 1);
  1719. }
  1720. int yaffs_lremovexattr(const YCHAR *path, const char *name)
  1721. {
  1722. return yaffs_do_removexattr(path, name, 0);
  1723. }
  1724. int yaffs_fremovexattr(int fd, const char *name)
  1725. {
  1726. struct yaffs_obj *obj;
  1727. int retVal = -1;
  1728. if (!name) {
  1729. yaffsfs_SetError(-EFAULT);
  1730. return -1;
  1731. }
  1732. yaffsfs_Lock();
  1733. obj = yaffsfs_HandleToObject(fd);
  1734. if (obj) {
  1735. retVal = yaffs_remove_xattrib(obj, name);
  1736. if (retVal < 0) {
  1737. yaffsfs_SetError(retVal);
  1738. retVal = -1;
  1739. }
  1740. } else
  1741. /* bad handle */
  1742. yaffsfs_SetError(-EBADF);
  1743. yaffsfs_Unlock();
  1744. return retVal;
  1745. }
  1746. #endif
  1747. #ifdef CONFIG_YAFFS_WINCE
  1748. int yaffs_get_wince_times(int fd, unsigned *wctime,
  1749. unsigned *watime, unsigned *wmtime)
  1750. {
  1751. struct yaffs_obj *obj;
  1752. int retVal = -1;
  1753. yaffsfs_Lock();
  1754. obj = yaffsfs_HandleToObject(fd);
  1755. if (obj) {
  1756. if (wctime) {
  1757. wctime[0] = obj->win_ctime[0];
  1758. wctime[1] = obj->win_ctime[1];
  1759. }
  1760. if (watime) {
  1761. watime[0] = obj->win_atime[0];
  1762. watime[1] = obj->win_atime[1];
  1763. }
  1764. if (wmtime) {
  1765. wmtime[0] = obj->win_mtime[0];
  1766. wmtime[1] = obj->win_mtime[1];
  1767. }
  1768. retVal = 0;
  1769. } else
  1770. /* bad handle */
  1771. yaffsfs_SetError(-EBADF);
  1772. yaffsfs_Unlock();
  1773. return retVal;
  1774. }
  1775. int yaffs_set_wince_times(int fd,
  1776. const unsigned *wctime,
  1777. const unsigned *watime, const unsigned *wmtime)
  1778. {
  1779. struct yaffs_obj *obj;
  1780. int result;
  1781. int retVal = -1;
  1782. yaffsfs_Lock();
  1783. obj = yaffsfs_HandleToObject(fd);
  1784. if (obj) {
  1785. if (wctime) {
  1786. obj->win_ctime[0] = wctime[0];
  1787. obj->win_ctime[1] = wctime[1];
  1788. }
  1789. if (watime) {
  1790. obj->win_atime[0] = watime[0];
  1791. obj->win_atime[1] = watime[1];
  1792. }
  1793. if (wmtime) {
  1794. obj->win_mtime[0] = wmtime[0];
  1795. obj->win_mtime[1] = wmtime[1];
  1796. }
  1797. obj->dirty = 1;
  1798. result = yaffs_flush_file(obj, 0, 0);
  1799. retVal = 0;
  1800. } else
  1801. /* bad handle */
  1802. yaffsfs_SetError(-EBADF);
  1803. yaffsfs_Unlock();
  1804. return retVal;
  1805. }
  1806. #endif
  1807. static int yaffsfs_DoChMod(struct yaffs_obj *obj, mode_t mode)
  1808. {
  1809. int result = -1;
  1810. if (obj)
  1811. obj = yaffs_get_equivalent_obj(obj);
  1812. if (obj) {
  1813. obj->yst_mode = mode;
  1814. obj->dirty = 1;
  1815. result = yaffs_flush_file(obj, 0, 0);
  1816. }
  1817. return result == YAFFS_OK ? 0 : -1;
  1818. }
  1819. int yaffs_access(const YCHAR *path, int amode)
  1820. {
  1821. struct yaffs_obj *obj = NULL;
  1822. struct yaffs_obj *dir = NULL;
  1823. int notDir = 0;
  1824. int loop = 0;
  1825. int retval = -1;
  1826. if (!path) {
  1827. yaffsfs_SetError(-EFAULT);
  1828. return -1;
  1829. }
  1830. if (yaffsfs_CheckPath(path) < 0) {
  1831. yaffsfs_SetError(-ENAMETOOLONG);
  1832. return -1;
  1833. }
  1834. if (amode & ~(R_OK | W_OK | X_OK)) {
  1835. yaffsfs_SetError(-EINVAL);
  1836. return -1;
  1837. }
  1838. yaffsfs_Lock();
  1839. obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
  1840. obj = yaffsfs_FollowLink(obj, 0, &loop);
  1841. if (!dir && notDir)
  1842. yaffsfs_SetError(-ENOTDIR);
  1843. else if (loop)
  1844. yaffsfs_SetError(-ELOOP);
  1845. else if (!dir || !obj)
  1846. yaffsfs_SetError(-ENOENT);
  1847. else if ((amode & W_OK) && obj->my_dev->read_only)
  1848. yaffsfs_SetError(-EROFS);
  1849. else {
  1850. int access_ok = 1;
  1851. if ((amode & R_OK) && !(obj->yst_mode & S_IREAD))
  1852. access_ok = 0;
  1853. if ((amode & W_OK) && !(obj->yst_mode & S_IWRITE))
  1854. access_ok = 0;
  1855. if ((amode & X_OK) && !(obj->yst_mode & S_IEXEC))
  1856. access_ok = 0;
  1857. if (!access_ok)
  1858. yaffsfs_SetError(-EACCES);
  1859. else
  1860. retval = 0;
  1861. }
  1862. yaffsfs_Unlock();
  1863. return retval;
  1864. }
  1865. int yaffs_chmod(const YCHAR *path, mode_t mode)
  1866. {
  1867. struct yaffs_obj *obj = NULL;
  1868. struct yaffs_obj *dir = NULL;
  1869. int retVal = -1;
  1870. int notDir = 0;
  1871. int loop = 0;
  1872. if (!path) {
  1873. yaffsfs_SetError(-EFAULT);
  1874. return -1;
  1875. }
  1876. if (yaffsfs_CheckPath(path) < 0) {
  1877. yaffsfs_SetError(-ENAMETOOLONG);
  1878. return -1;
  1879. }
  1880. if (mode & ~(0777)) {
  1881. yaffsfs_SetError(-EINVAL);
  1882. return -1;
  1883. }
  1884. yaffsfs_Lock();
  1885. obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
  1886. obj = yaffsfs_FollowLink(obj, 0, &loop);
  1887. if (!dir && notDir)
  1888. yaffsfs_SetError(-ENOTDIR);
  1889. else if (loop)
  1890. yaffsfs_SetError(-ELOOP);
  1891. else if (!dir || !obj)
  1892. yaffsfs_SetError(-ENOENT);
  1893. else if (obj->my_dev->read_only)
  1894. yaffsfs_SetError(-EROFS);
  1895. else
  1896. retVal = yaffsfs_DoChMod(obj, mode);
  1897. yaffsfs_Unlock();
  1898. return retVal;
  1899. }
  1900. int yaffs_fchmod(int fd, mode_t mode)
  1901. {
  1902. struct yaffs_obj *obj;
  1903. int retVal = -1;
  1904. if (mode & ~(0777)) {
  1905. yaffsfs_SetError(-EINVAL);
  1906. return -1;
  1907. }
  1908. yaffsfs_Lock();
  1909. obj = yaffsfs_HandleToObject(fd);
  1910. if (!obj)
  1911. yaffsfs_SetError(-EBADF);
  1912. else if (obj->my_dev->read_only)
  1913. yaffsfs_SetError(-EROFS);
  1914. else
  1915. retVal = yaffsfs_DoChMod(obj, mode);
  1916. yaffsfs_Unlock();
  1917. return retVal;
  1918. }
  1919. int yaffs_mkdir(const YCHAR *path, mode_t mode)
  1920. {
  1921. struct yaffs_obj *parent = NULL;
  1922. struct yaffs_obj *dir = NULL;
  1923. YCHAR *name;
  1924. YCHAR *alt_path = NULL;
  1925. int retVal = -1;
  1926. int notDir = 0;
  1927. int loop = 0;
  1928. if (!path) {
  1929. yaffsfs_SetError(-EFAULT);
  1930. return -1;
  1931. }
  1932. if (yaffsfs_CheckPath(path) < 0) {
  1933. yaffsfs_SetError(-ENAMETOOLONG);
  1934. return -1;
  1935. }
  1936. if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
  1937. yaffsfs_SetError(-ENOMEM);
  1938. return -1;
  1939. }
  1940. if (alt_path)
  1941. path = alt_path;
  1942. yaffsfs_Lock();
  1943. parent = yaffsfs_FindDirectory(NULL, path, &name, 0, &notDir, &loop);
  1944. if (!parent && notDir)
  1945. yaffsfs_SetError(-ENOTDIR);
  1946. else if (loop)
  1947. yaffsfs_SetError(-ELOOP);
  1948. else if (!parent)
  1949. yaffsfs_SetError(-ENOENT);
  1950. else if (yaffsfs_TooManyObjects(parent->my_dev))
  1951. yaffsfs_SetError(-ENFILE);
  1952. else if (yaffs_strnlen(name, 5) == 0) {
  1953. /* Trying to make the root itself */
  1954. yaffsfs_SetError(-EEXIST);
  1955. } else if (parent->my_dev->read_only)
  1956. yaffsfs_SetError(-EROFS);
  1957. else {
  1958. dir = yaffs_create_dir(parent, name, mode, 0, 0);
  1959. if (dir)
  1960. retVal = 0;
  1961. else if (yaffs_find_by_name(parent, name))
  1962. yaffsfs_SetError(-EEXIST); /* name exists */
  1963. else
  1964. yaffsfs_SetError(-ENOSPC); /* assume no space */
  1965. }
  1966. yaffsfs_Unlock();
  1967. kfree(alt_path);
  1968. return retVal;
  1969. }
  1970. int yaffs_rmdir(const YCHAR *path)
  1971. {
  1972. int result;
  1973. YCHAR *alt_path;
  1974. if (!path) {
  1975. yaffsfs_SetError(-EFAULT);
  1976. return -1;
  1977. }
  1978. if (yaffsfs_CheckPath(path) < 0) {
  1979. yaffsfs_SetError(-ENAMETOOLONG);
  1980. return -1;
  1981. }
  1982. if (yaffsfs_alt_dir_path(path, &alt_path) < 0) {
  1983. yaffsfs_SetError(-ENOMEM);
  1984. return -1;
  1985. }
  1986. if (alt_path)
  1987. path = alt_path;
  1988. result = yaffsfs_DoUnlink(path, 1);
  1989. kfree(alt_path);
  1990. return result;
  1991. }
  1992. void *yaffs_getdev(const YCHAR *path)
  1993. {
  1994. struct yaffs_dev *dev = NULL;
  1995. YCHAR *dummy;
  1996. dev = yaffsfs_FindDevice(path, &dummy);
  1997. return (void *)dev;
  1998. }
  1999. int yaffs_mount_common(const YCHAR *path, int read_only, int skip_checkpt)
  2000. {
  2001. int retVal = -1;
  2002. int result = YAFFS_FAIL;
  2003. struct yaffs_dev *dev = NULL;
  2004. if (!path) {
  2005. yaffsfs_SetError(-EFAULT);
  2006. return -1;
  2007. }
  2008. yaffs_trace(YAFFS_TRACE_MOUNT, "yaffs: Mounting %s", path);
  2009. if (yaffsfs_CheckPath(path) < 0) {
  2010. yaffsfs_SetError(-ENAMETOOLONG);
  2011. return -1;
  2012. }
  2013. yaffsfs_Lock();
  2014. yaffsfs_InitHandles();
  2015. dev = yaffsfs_FindMountPoint(path);
  2016. if (dev) {
  2017. if (!dev->is_mounted) {
  2018. dev->read_only = read_only ? 1 : 0;
  2019. if (skip_checkpt) {
  2020. u8 skip = dev->param.skip_checkpt_rd;
  2021. dev->param.skip_checkpt_rd = 1;
  2022. result = yaffs_guts_initialise(dev);
  2023. dev->param.skip_checkpt_rd = skip;
  2024. } else {
  2025. result = yaffs_guts_initialise(dev);
  2026. }
  2027. if (result == YAFFS_FAIL)
  2028. yaffsfs_SetError(-ENOMEM);
  2029. retVal = result ? 0 : -1;
  2030. } else
  2031. yaffsfs_SetError(-EBUSY);
  2032. } else
  2033. yaffsfs_SetError(-ENODEV);
  2034. yaffsfs_Unlock();
  2035. return retVal;
  2036. }
  2037. int yaffs_mount2(const YCHAR *path, int readonly)
  2038. {
  2039. return yaffs_mount_common(path, readonly, 0);
  2040. }
  2041. int yaffs_mount(const YCHAR *path)
  2042. {
  2043. return yaffs_mount_common(path, 0, 0);
  2044. }
  2045. int yaffs_sync(const YCHAR *path)
  2046. {
  2047. int retVal = -1;
  2048. struct yaffs_dev *dev = NULL;
  2049. YCHAR *dummy;
  2050. if (!path) {
  2051. yaffsfs_SetError(-EFAULT);
  2052. return -1;
  2053. }
  2054. if (yaffsfs_CheckPath(path) < 0) {
  2055. yaffsfs_SetError(-ENAMETOOLONG);
  2056. return -1;
  2057. }
  2058. yaffsfs_Lock();
  2059. dev = yaffsfs_FindDevice(path, &dummy);
  2060. if (dev) {
  2061. if (!dev->is_mounted)
  2062. yaffsfs_SetError(-EINVAL);
  2063. else if (dev->read_only)
  2064. yaffsfs_SetError(-EROFS);
  2065. else {
  2066. yaffs_flush_whole_cache(dev);
  2067. yaffs_checkpoint_save(dev);
  2068. retVal = 0;
  2069. }
  2070. } else
  2071. yaffsfs_SetError(-ENODEV);
  2072. yaffsfs_Unlock();
  2073. return retVal;
  2074. }
  2075. static int yaffsfs_IsDevBusy(struct yaffs_dev *dev)
  2076. {
  2077. int i;
  2078. struct yaffs_obj *obj;
  2079. for (i = 0; i < YAFFSFS_N_HANDLES; i++) {
  2080. obj = yaffsfs_HandleToObject(i);
  2081. if (obj && obj->my_dev == dev)
  2082. return 1;
  2083. }
  2084. return 0;
  2085. }
  2086. int yaffs_remount(const YCHAR *path, int force, int read_only)
  2087. {
  2088. int retVal = -1;
  2089. struct yaffs_dev *dev = NULL;
  2090. if (!path) {
  2091. yaffsfs_SetError(-EFAULT);
  2092. return -1;
  2093. }
  2094. if (yaffsfs_CheckPath(path) < 0) {
  2095. yaffsfs_SetError(-ENAMETOOLONG);
  2096. return -1;
  2097. }
  2098. yaffsfs_Lock();
  2099. dev = yaffsfs_FindMountPoint(path);
  2100. if (dev) {
  2101. if (dev->is_mounted) {
  2102. yaffs_flush_whole_cache(dev);
  2103. if (force || !yaffsfs_IsDevBusy(dev)) {
  2104. if (read_only)
  2105. yaffs_checkpoint_save(dev);
  2106. dev->read_only = read_only ? 1 : 0;
  2107. retVal = 0;
  2108. } else
  2109. yaffsfs_SetError(-EBUSY);
  2110. } else
  2111. yaffsfs_SetError(-EINVAL);
  2112. } else
  2113. yaffsfs_SetError(-ENODEV);
  2114. yaffsfs_Unlock();
  2115. return retVal;
  2116. }
  2117. int yaffs_unmount2(const YCHAR *path, int force)
  2118. {
  2119. int retVal = -1;
  2120. struct yaffs_dev *dev = NULL;
  2121. if (!path) {
  2122. yaffsfs_SetError(-EFAULT);
  2123. return -1;
  2124. }
  2125. if (yaffsfs_CheckPath(path) < 0) {
  2126. yaffsfs_SetError(-ENAMETOOLONG);
  2127. return -1;
  2128. }
  2129. yaffsfs_Lock();
  2130. dev = yaffsfs_FindMountPoint(path);
  2131. if (dev) {
  2132. if (dev->is_mounted) {
  2133. int inUse;
  2134. yaffs_flush_whole_cache(dev);
  2135. yaffs_checkpoint_save(dev);
  2136. inUse = yaffsfs_IsDevBusy(dev);
  2137. if (!inUse || force) {
  2138. if (inUse)
  2139. yaffsfs_BreakDeviceHandles(dev);
  2140. yaffs_deinitialise(dev);
  2141. retVal = 0;
  2142. } else
  2143. yaffsfs_SetError(-EBUSY);
  2144. } else
  2145. yaffsfs_SetError(-EINVAL);
  2146. } else
  2147. yaffsfs_SetError(-ENODEV);
  2148. yaffsfs_Unlock();
  2149. return retVal;
  2150. }
  2151. int yaffs_unmount(const YCHAR *path)
  2152. {
  2153. return yaffs_unmount2(path, 0);
  2154. }
  2155. loff_t yaffs_freespace(const YCHAR *path)
  2156. {
  2157. loff_t retVal = -1;
  2158. struct yaffs_dev *dev = NULL;
  2159. YCHAR *dummy;
  2160. if (!path) {
  2161. yaffsfs_SetError(-EFAULT);
  2162. return -1;
  2163. }
  2164. if (yaffsfs_CheckPath(path) < 0) {
  2165. yaffsfs_SetError(-ENAMETOOLONG);
  2166. return -1;
  2167. }
  2168. yaffsfs_Lock();
  2169. dev = yaffsfs_FindDevice(path, &dummy);
  2170. if (dev && dev->is_mounted) {
  2171. retVal = yaffs_get_n_free_chunks(dev);
  2172. retVal *= dev->data_bytes_per_chunk;
  2173. } else
  2174. yaffsfs_SetError(-EINVAL);
  2175. yaffsfs_Unlock();
  2176. return retVal;
  2177. }
  2178. loff_t yaffs_totalspace(const YCHAR *path)
  2179. {
  2180. loff_t retVal = -1;
  2181. struct yaffs_dev *dev = NULL;
  2182. YCHAR *dummy;
  2183. if (!path) {
  2184. yaffsfs_SetError(-EFAULT);
  2185. return -1;
  2186. }
  2187. if (yaffsfs_CheckPath(path) < 0) {
  2188. yaffsfs_SetError(-ENAMETOOLONG);
  2189. return -1;
  2190. }
  2191. yaffsfs_Lock();
  2192. dev = yaffsfs_FindDevice(path, &dummy);
  2193. if (dev && dev->is_mounted) {
  2194. retVal = (dev->param.end_block - dev->param.start_block + 1) -
  2195. dev->param.n_reserved_blocks;
  2196. retVal *= dev->param.chunks_per_block;
  2197. retVal *= dev->data_bytes_per_chunk;
  2198. } else
  2199. yaffsfs_SetError(-EINVAL);
  2200. yaffsfs_Unlock();
  2201. return retVal;
  2202. }
  2203. int yaffs_inodecount(const YCHAR *path)
  2204. {
  2205. loff_t retVal = -1;
  2206. struct yaffs_dev *dev = NULL;
  2207. YCHAR *dummy;
  2208. if (!path) {
  2209. yaffsfs_SetError(-EFAULT);
  2210. return -1;
  2211. }
  2212. if (yaffsfs_CheckPath(path) < 0) {
  2213. yaffsfs_SetError(-ENAMETOOLONG);
  2214. return -1;
  2215. }
  2216. yaffsfs_Lock();
  2217. dev = yaffsfs_FindDevice(path, &dummy);
  2218. if (dev && dev->is_mounted) {
  2219. int n_obj = dev->n_obj;
  2220. if (n_obj > dev->n_hardlinks)
  2221. retVal = n_obj - dev->n_hardlinks;
  2222. }
  2223. if (retVal < 0)
  2224. yaffsfs_SetError(-EINVAL);
  2225. yaffsfs_Unlock();
  2226. return retVal;
  2227. }
  2228. void yaffs_add_device(struct yaffs_dev *dev)
  2229. {
  2230. struct list_head *cfg;
  2231. /* First check that the device is not in the list. */
  2232. list_for_each(cfg, &yaffsfs_deviceList) {
  2233. if (dev == list_entry(cfg, struct yaffs_dev, dev_list))
  2234. return;
  2235. }
  2236. dev->is_mounted = 0;
  2237. dev->param.remove_obj_fn = yaffsfs_RemoveObjectCallback;
  2238. if (!dev->dev_list.next)
  2239. INIT_LIST_HEAD(&dev->dev_list);
  2240. list_add(&dev->dev_list, &yaffsfs_deviceList);
  2241. }
  2242. void yaffs_remove_device(struct yaffs_dev *dev)
  2243. {
  2244. list_del_init(&dev->dev_list);
  2245. }
  2246. /* Functions to iterate through devices. NB Use with extreme care! */
  2247. static struct list_head *dev_iterator;
  2248. void yaffs_dev_rewind(void)
  2249. {
  2250. dev_iterator = yaffsfs_deviceList.next;
  2251. }
  2252. struct yaffs_dev *yaffs_next_dev(void)
  2253. {
  2254. struct yaffs_dev *retval;
  2255. if (!dev_iterator)
  2256. return NULL;
  2257. if (dev_iterator == &yaffsfs_deviceList)
  2258. return NULL;
  2259. retval = list_entry(dev_iterator, struct yaffs_dev, dev_list);
  2260. dev_iterator = dev_iterator->next;
  2261. return retval;
  2262. }
  2263. /* Directory search stuff. */
  2264. static struct list_head search_contexts;
  2265. static void yaffsfs_SetDirRewound(struct yaffsfs_DirSearchContxt *dsc)
  2266. {
  2267. if (dsc &&
  2268. dsc->dirObj &&
  2269. dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
  2270. dsc->offset = 0;
  2271. if (list_empty(&dsc->dirObj->variant.dir_variant.children))
  2272. dsc->nextReturn = NULL;
  2273. else
  2274. dsc->nextReturn =
  2275. list_entry(dsc->dirObj->variant.dir_variant.
  2276. children.next, struct yaffs_obj,
  2277. siblings);
  2278. } else {
  2279. /* Hey someone isn't playing nice! */
  2280. }
  2281. }
  2282. static void yaffsfs_DirAdvance(struct yaffsfs_DirSearchContxt *dsc)
  2283. {
  2284. if (dsc &&
  2285. dsc->dirObj &&
  2286. dsc->dirObj->variant_type == YAFFS_OBJECT_TYPE_DIRECTORY) {
  2287. if (dsc->nextReturn == NULL ||
  2288. list_empty(&dsc->dirObj->variant.dir_variant.children))
  2289. dsc->nextReturn = NULL;
  2290. else {
  2291. struct list_head *next = dsc->nextReturn->siblings.next;
  2292. if (next == &dsc->dirObj->variant.dir_variant.children)
  2293. dsc->nextReturn = NULL; /* end of list */
  2294. else
  2295. dsc->nextReturn = list_entry(next,
  2296. struct yaffs_obj,
  2297. siblings);
  2298. }
  2299. } else {
  2300. /* Hey someone isn't playing nice! */
  2301. }
  2302. }
  2303. static void yaffsfs_RemoveObjectCallback(struct yaffs_obj *obj)
  2304. {
  2305. struct list_head *i;
  2306. struct yaffsfs_DirSearchContxt *dsc;
  2307. /* if search contexts not initilised then skip */
  2308. if (!search_contexts.next)
  2309. return;
  2310. /* Iterate through the directory search contexts.
  2311. * If any are the one being removed, then advance the dsc to
  2312. * the next one to prevent a hanging ptr.
  2313. */
  2314. list_for_each(i, &search_contexts) {
  2315. dsc = list_entry(i, struct yaffsfs_DirSearchContxt, others);
  2316. if (dsc->nextReturn == obj)
  2317. yaffsfs_DirAdvance(dsc);
  2318. }
  2319. }
  2320. yaffs_DIR *yaffs_opendir(const YCHAR *dirname)
  2321. {
  2322. yaffs_DIR *dir = NULL;
  2323. struct yaffs_obj *obj = NULL;
  2324. struct yaffsfs_DirSearchContxt *dsc = NULL;
  2325. int notDir = 0;
  2326. int loop = 0;
  2327. if (!dirname) {
  2328. yaffsfs_SetError(-EFAULT);
  2329. return NULL;
  2330. }
  2331. if (yaffsfs_CheckPath(dirname) < 0) {
  2332. yaffsfs_SetError(-ENAMETOOLONG);
  2333. return NULL;
  2334. }
  2335. yaffsfs_Lock();
  2336. obj = yaffsfs_FindObject(NULL, dirname, 0, 1, NULL, &notDir, &loop);
  2337. if (!obj && notDir)
  2338. yaffsfs_SetError(-ENOTDIR);
  2339. else if (loop)
  2340. yaffsfs_SetError(-ELOOP);
  2341. else if (!obj)
  2342. yaffsfs_SetError(-ENOENT);
  2343. else if (obj->variant_type != YAFFS_OBJECT_TYPE_DIRECTORY)
  2344. yaffsfs_SetError(-ENOTDIR);
  2345. else {
  2346. int i;
  2347. for (i = 0, dsc = NULL; i < YAFFSFS_N_DSC && !dsc; i++) {
  2348. if (!yaffsfs_dsc[i].inUse)
  2349. dsc = &yaffsfs_dsc[i];
  2350. }
  2351. dir = (yaffs_DIR *) dsc;
  2352. if (dsc) {
  2353. memset(dsc, 0, sizeof(struct yaffsfs_DirSearchContxt));
  2354. dsc->inUse = 1;
  2355. dsc->dirObj = obj;
  2356. yaffs_strncpy(dsc->name, dirname, NAME_MAX);
  2357. INIT_LIST_HEAD(&dsc->others);
  2358. if (!search_contexts.next)
  2359. INIT_LIST_HEAD(&search_contexts);
  2360. list_add(&dsc->others, &search_contexts);
  2361. yaffsfs_SetDirRewound(dsc);
  2362. }
  2363. }
  2364. yaffsfs_Unlock();
  2365. return dir;
  2366. }
  2367. struct yaffs_dirent *yaffs_readdir(yaffs_DIR * dirp)
  2368. {
  2369. struct yaffsfs_DirSearchContxt *dsc;
  2370. struct yaffs_dirent *retVal = NULL;
  2371. dsc = (struct yaffsfs_DirSearchContxt *) dirp;
  2372. yaffsfs_Lock();
  2373. if (dsc && dsc->inUse) {
  2374. yaffsfs_SetError(0);
  2375. if (dsc->nextReturn) {
  2376. dsc->de.d_ino =
  2377. yaffs_get_equivalent_obj(dsc->nextReturn)->obj_id;
  2378. dsc->de.d_dont_use = (unsigned)dsc->nextReturn;
  2379. dsc->de.d_off = dsc->offset++;
  2380. yaffs_get_obj_name(dsc->nextReturn,
  2381. dsc->de.d_name, NAME_MAX);
  2382. if (yaffs_strnlen(dsc->de.d_name, NAME_MAX + 1) == 0) {
  2383. /* this should not happen! */
  2384. yaffs_strcpy(dsc->de.d_name, _Y("zz"));
  2385. }
  2386. dsc->de.d_reclen = sizeof(struct yaffs_dirent);
  2387. retVal = &dsc->de;
  2388. yaffsfs_DirAdvance(dsc);
  2389. } else
  2390. retVal = NULL;
  2391. } else
  2392. yaffsfs_SetError(-EBADF);
  2393. yaffsfs_Unlock();
  2394. return retVal;
  2395. }
  2396. void yaffs_rewinddir(yaffs_DIR *dirp)
  2397. {
  2398. struct yaffsfs_DirSearchContxt *dsc;
  2399. dsc = (struct yaffsfs_DirSearchContxt *) dirp;
  2400. yaffsfs_Lock();
  2401. yaffsfs_SetDirRewound(dsc);
  2402. yaffsfs_Unlock();
  2403. }
  2404. int yaffs_closedir(yaffs_DIR *dirp)
  2405. {
  2406. struct yaffsfs_DirSearchContxt *dsc;
  2407. dsc = (struct yaffsfs_DirSearchContxt *) dirp;
  2408. if (!dsc) {
  2409. yaffsfs_SetError(-EFAULT);
  2410. return -1;
  2411. }
  2412. yaffsfs_Lock();
  2413. dsc->inUse = 0;
  2414. list_del(&dsc->others); /* unhook from list */
  2415. yaffsfs_Unlock();
  2416. return 0;
  2417. }
  2418. /* End of directory stuff */
  2419. int yaffs_symlink(const YCHAR *oldpath, const YCHAR *newpath)
  2420. {
  2421. struct yaffs_obj *parent = NULL;
  2422. struct yaffs_obj *obj;
  2423. YCHAR *name;
  2424. int retVal = -1;
  2425. int mode = 0; /* ignore for now */
  2426. int notDir = 0;
  2427. int loop = 0;
  2428. if (!oldpath || !newpath) {
  2429. yaffsfs_SetError(-EFAULT);
  2430. return -1;
  2431. }
  2432. if (yaffsfs_CheckPath(newpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
  2433. yaffsfs_SetError(-ENAMETOOLONG);
  2434. return -1;
  2435. }
  2436. yaffsfs_Lock();
  2437. parent = yaffsfs_FindDirectory(NULL, newpath, &name, 0, &notDir, &loop);
  2438. if (!parent && notDir)
  2439. yaffsfs_SetError(-ENOTDIR);
  2440. else if (loop)
  2441. yaffsfs_SetError(-ELOOP);
  2442. else if (!parent || yaffs_strnlen(name, 5) < 1)
  2443. yaffsfs_SetError(-ENOENT);
  2444. else if (yaffsfs_TooManyObjects(parent->my_dev))
  2445. yaffsfs_SetError(-ENFILE);
  2446. else if (parent->my_dev->read_only)
  2447. yaffsfs_SetError(-EROFS);
  2448. else {
  2449. obj = yaffs_create_symlink(parent, name, mode, 0, 0, oldpath);
  2450. if (obj)
  2451. retVal = 0;
  2452. else if (yaffsfs_FindObject
  2453. (NULL, newpath, 0, 0, NULL, NULL, NULL))
  2454. yaffsfs_SetError(-EEXIST);
  2455. else
  2456. yaffsfs_SetError(-ENOSPC);
  2457. }
  2458. yaffsfs_Unlock();
  2459. return retVal;
  2460. }
  2461. int yaffs_readlink(const YCHAR *path, YCHAR *buf, int bufsiz)
  2462. {
  2463. struct yaffs_obj *obj = NULL;
  2464. struct yaffs_obj *dir = NULL;
  2465. int retVal = -1;
  2466. int notDir = 0;
  2467. int loop = 0;
  2468. if (!path || !buf) {
  2469. yaffsfs_SetError(-EFAULT);
  2470. return -1;
  2471. }
  2472. yaffsfs_Lock();
  2473. obj = yaffsfs_FindObject(NULL, path, 0, 1, &dir, &notDir, &loop);
  2474. if (!dir && notDir)
  2475. yaffsfs_SetError(-ENOTDIR);
  2476. else if (loop)
  2477. yaffsfs_SetError(-ELOOP);
  2478. else if (!dir || !obj)
  2479. yaffsfs_SetError(-ENOENT);
  2480. else if (obj->variant_type != YAFFS_OBJECT_TYPE_SYMLINK)
  2481. yaffsfs_SetError(-EINVAL);
  2482. else {
  2483. YCHAR *alias = obj->variant.symlink_variant.alias;
  2484. memset(buf, 0, bufsiz);
  2485. yaffs_strncpy(buf, alias, bufsiz - 1);
  2486. retVal = 0;
  2487. }
  2488. yaffsfs_Unlock();
  2489. return retVal;
  2490. }
  2491. int yaffs_link(const YCHAR *oldpath, const YCHAR *linkpath)
  2492. {
  2493. /* Creates a link called newpath to existing oldpath */
  2494. struct yaffs_obj *obj = NULL;
  2495. struct yaffs_obj *lnk = NULL;
  2496. struct yaffs_obj *obj_dir = NULL;
  2497. struct yaffs_obj *lnk_dir = NULL;
  2498. int retVal = -1;
  2499. int notDirObj = 0;
  2500. int notDirLnk = 0;
  2501. int objLoop = 0;
  2502. int lnkLoop = 0;
  2503. YCHAR *newname;
  2504. if (!oldpath || !linkpath) {
  2505. yaffsfs_SetError(-EFAULT);
  2506. return -1;
  2507. }
  2508. if (yaffsfs_CheckPath(linkpath) < 0 || yaffsfs_CheckPath(oldpath) < 0) {
  2509. yaffsfs_SetError(-ENAMETOOLONG);
  2510. return -1;
  2511. }
  2512. yaffsfs_Lock();
  2513. obj = yaffsfs_FindObject(NULL, oldpath, 0, 1,
  2514. &obj_dir, &notDirObj, &objLoop);
  2515. lnk = yaffsfs_FindObject(NULL, linkpath, 0, 0, NULL, NULL, NULL);
  2516. lnk_dir = yaffsfs_FindDirectory(NULL, linkpath, &newname,
  2517. 0, &notDirLnk, &lnkLoop);
  2518. if ((!obj_dir && notDirObj) || (!lnk_dir && notDirLnk))
  2519. yaffsfs_SetError(-ENOTDIR);
  2520. else if (objLoop || lnkLoop)
  2521. yaffsfs_SetError(-ELOOP);
  2522. else if (!obj_dir || !lnk_dir || !obj)
  2523. yaffsfs_SetError(-ENOENT);
  2524. else if (obj->my_dev->read_only)
  2525. yaffsfs_SetError(-EROFS);
  2526. else if (yaffsfs_TooManyObjects(obj->my_dev))
  2527. yaffsfs_SetError(-ENFILE);
  2528. else if (lnk)
  2529. yaffsfs_SetError(-EEXIST);
  2530. else if (lnk_dir->my_dev != obj->my_dev)
  2531. yaffsfs_SetError(-EXDEV);
  2532. else {
  2533. retVal = yaffsfs_CheckNameLength(newname);
  2534. if (retVal == 0) {
  2535. lnk = yaffs_link_obj(lnk_dir, newname, obj);
  2536. if (lnk)
  2537. retVal = 0;
  2538. else {
  2539. yaffsfs_SetError(-ENOSPC);
  2540. retVal = -1;
  2541. }
  2542. }
  2543. }
  2544. yaffsfs_Unlock();
  2545. return retVal;
  2546. }
  2547. int yaffs_mknod(const YCHAR *pathname, mode_t mode, dev_t dev)
  2548. {
  2549. yaffsfs_SetError(-EINVAL);
  2550. return -1;
  2551. }
  2552. /*
  2553. * D E B U G F U N C T I O N S
  2554. */
  2555. /*
  2556. * yaffs_n_handles()
  2557. * Returns number of handles attached to the object
  2558. */
  2559. int yaffs_n_handles(const YCHAR *path)
  2560. {
  2561. struct yaffs_obj *obj;
  2562. if (!path) {
  2563. yaffsfs_SetError(-EFAULT);
  2564. return -1;
  2565. }
  2566. if (yaffsfs_CheckPath(path) < 0) {
  2567. yaffsfs_SetError(-ENAMETOOLONG);
  2568. return -1;
  2569. }
  2570. obj = yaffsfs_FindObject(NULL, path, 0, 1, NULL, NULL, NULL);
  2571. if (obj)
  2572. return yaffsfs_CountHandles(obj);
  2573. else
  2574. return -1;
  2575. }
  2576. int yaffs_get_error(void)
  2577. {
  2578. return yaffsfs_GetLastError();
  2579. }
  2580. int yaffs_set_error(int error)
  2581. {
  2582. yaffsfs_SetError(error);
  2583. return 0;
  2584. }
  2585. int yaffs_dump_dev(const YCHAR *path)
  2586. {
  2587. #if 0
  2588. YCHAR *rest;
  2589. struct yaffs_obj *obj = yaffsfs_FindRoot(path, &rest);
  2590. if (obj) {
  2591. struct yaffs_dev *dev = obj->my_dev;
  2592. printf("\n"
  2593. "n_page_writes.......... %d\n"
  2594. "n_page_reads........... %d\n"
  2595. "n_erasures....... %d\n"
  2596. "n_gc_copies............ %d\n"
  2597. "garbageCollections... %d\n"
  2598. "passiveGarbageColl'ns %d\n"
  2599. "\n",
  2600. dev->n_page_writes,
  2601. dev->n_page_reads,
  2602. dev->n_erasures,
  2603. dev->n_gc_copies,
  2604. dev->garbageCollections, dev->passiveGarbageCollections);
  2605. }
  2606. #endif
  2607. return 0;
  2608. }