yaffsfs.c 65 KB

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