yaffsfs.c 65 KB

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