fs-ecos.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174
  1. /*
  2. * JFFS2 -- Journalling Flash File System, Version 2.
  3. *
  4. * Copyright (C) 2001-2003 Free Software Foundation, Inc.
  5. *
  6. * Created by Dominic Ostrowski <dominic.ostrowski@3glab.com>
  7. * Contributors: David Woodhouse, Nick Garnett, Richard Panton.
  8. *
  9. * For licensing information, see the file 'LICENCE' in this directory.
  10. *
  11. * $Id: fs-ecos.c,v 1.44 2005/07/24 15:29:57 dedekind Exp $
  12. *
  13. */
  14. #include <stdio.h> //prife for SEEK_SET SEEK_CUR SEEK_END
  15. #include <linux/kernel.h>
  16. #include "nodelist.h"
  17. #include <linux/pagemap.h>
  18. #include <linux/crc32.h>
  19. #include "compr.h"
  20. #include <string.h>
  21. #include <rtdevice.h>
  22. //--------------------------------------------
  23. cyg_mtab_entry *cyg_cdir_mtab_entry = NULL;
  24. cyg_dir cyg_cdir_dir = CYG_DIR_NULL;
  25. //==========================================================================
  26. // Default functions
  27. __externC int cyg_fileio_enosys() { return ENOSYS; }
  28. __externC int cyg_fileio_erofs() { return EROFS; }
  29. __externC int cyg_fileio_enoerr() { return ENOERR; }
  30. __externC int cyg_fileio_enotdir() { return ENOTDIR; }
  31. __externC cyg_bool cyg_fileio_seltrue (struct CYG_FILE_TAG *fp, int which, CYG_ADDRWORD info)
  32. { return 1; }
  33. //--------------------------------------------
  34. //==========================================================================
  35. // Forward definitions
  36. // Filesystem operations
  37. int jffs2_mount(cyg_fstab_entry * fste, cyg_mtab_entry * mte);
  38. static int jffs2_umount(cyg_mtab_entry * mte);
  39. int jffs2_open(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
  40. int mode, cyg_file * fte);
  41. #ifdef CYGOPT_FS_JFFS2_WRITE
  42. static int jffs2_ops_unlink(cyg_mtab_entry * mte, cyg_dir dir,
  43. const char *name);
  44. static int jffs2_ops_mkdir(cyg_mtab_entry * mte, cyg_dir dir, const char *name);
  45. static int jffs2_ops_rmdir(cyg_mtab_entry * mte, cyg_dir dir, const char *name);
  46. static int jffs2_ops_rename(cyg_mtab_entry * mte, cyg_dir dir1,
  47. const char *name1, cyg_dir dir2, const char *name2);
  48. static int jffs2_ops_link(cyg_mtab_entry * mte, cyg_dir dir1, const char *name1,
  49. cyg_dir dir2, const char *name2, int type);
  50. #endif
  51. static int jffs2_opendir(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
  52. cyg_file * fte);
  53. static int jffs2_chdir(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
  54. cyg_dir * dir_out);
  55. static int jffs2_stat(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
  56. struct stat *buf);
  57. static int jffs2_getinfo(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
  58. int key, void *buf, int len);
  59. static int jffs2_setinfo(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
  60. int key, void *buf, int len);
  61. // File operations
  62. int jffs2_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  63. #ifdef CYGOPT_FS_JFFS2_WRITE
  64. static int jffs2_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  65. #endif
  66. static int jffs2_fo_lseek(struct CYG_FILE_TAG *fp, off_t * pos, int whence);
  67. static int jffs2_fo_ioctl(struct CYG_FILE_TAG *fp, CYG_ADDRWORD com,
  68. CYG_ADDRWORD data);
  69. static int jffs2_fo_fsync(struct CYG_FILE_TAG *fp, int mode);
  70. static int jffs2_fo_close(struct CYG_FILE_TAG *fp);
  71. static int jffs2_fo_fstat(struct CYG_FILE_TAG *fp, struct stat *buf);
  72. static int jffs2_fo_getinfo(struct CYG_FILE_TAG *fp, int key, void *buf,
  73. int len);
  74. static int jffs2_fo_setinfo(struct CYG_FILE_TAG *fp, int key, void *buf,
  75. int len);
  76. // Directory operations
  77. static int jffs2_fo_dirread(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio);
  78. static int jffs2_fo_dirlseek(struct CYG_FILE_TAG *fp, off_t * pos, int whence);
  79. static int jffs2_read_inode (struct _inode *inode);
  80. static void jffs2_clear_inode (struct _inode *inode);
  81. static int jffs2_truncate_file (struct _inode *inode);
  82. //==========================================================================
  83. // Filesystem table entries
  84. // -------------------------------------------------------------------------
  85. // Fstab entry.
  86. // This defines the entry in the filesystem table.
  87. // For simplicity we use _FILESYSTEM synchronization for all accesses since
  88. // we should never block in any filesystem operations.
  89. #ifdef CYGOPT_FS_JFFS2_WRITE
  90. FSTAB_ENTRY(jffs2_fste, "jffs2", 0,
  91. CYG_SYNCMODE_FILE_FILESYSTEM | CYG_SYNCMODE_IO_FILESYSTEM,
  92. jffs2_mount,
  93. jffs2_umount,
  94. jffs2_open,
  95. jffs2_ops_unlink,
  96. jffs2_ops_mkdir,
  97. jffs2_ops_rmdir,
  98. jffs2_ops_rename,
  99. jffs2_ops_link,
  100. jffs2_opendir,
  101. jffs2_chdir, jffs2_stat, jffs2_getinfo, jffs2_setinfo);
  102. #else
  103. FSTAB_ENTRY(jffs2_fste, "jffs2", 0,
  104. CYG_SYNCMODE_FILE_FILESYSTEM | CYG_SYNCMODE_IO_FILESYSTEM,
  105. jffs2_mount,
  106. jffs2_umount,
  107. jffs2_open,
  108. (cyg_fsop_unlink *)cyg_fileio_erofs,
  109. (cyg_fsop_mkdir *)cyg_fileio_erofs,
  110. (cyg_fsop_rmdir *)cyg_fileio_erofs,
  111. (cyg_fsop_rename *)cyg_fileio_erofs,
  112. (cyg_fsop_link *)cyg_fileio_erofs,
  113. jffs2_opendir,
  114. jffs2_chdir, jffs2_stat, jffs2_getinfo, jffs2_setinfo);
  115. #endif
  116. // -------------------------------------------------------------------------
  117. // File operations.
  118. // This set of file operations are used for normal open files.
  119. cyg_fileops jffs2_fileops = {
  120. jffs2_fo_read,
  121. #ifdef CYGOPT_FS_JFFS2_WRITE
  122. jffs2_fo_write,
  123. #else
  124. (cyg_fileop_write *) cyg_fileio_erofs,
  125. #endif
  126. jffs2_fo_lseek,
  127. jffs2_fo_ioctl,
  128. cyg_fileio_seltrue,
  129. jffs2_fo_fsync,
  130. jffs2_fo_close,
  131. jffs2_fo_fstat,
  132. jffs2_fo_getinfo,
  133. jffs2_fo_setinfo
  134. };
  135. // -------------------------------------------------------------------------
  136. // Directory file operations.
  137. // This set of operations are used for open directories. Most entries
  138. // point to error-returning stub functions. Only the read, lseek and
  139. // close entries are functional.
  140. cyg_fileops jffs2_dirops = {
  141. jffs2_fo_dirread,
  142. (cyg_fileop_write *) cyg_fileio_enosys,
  143. jffs2_fo_dirlseek,
  144. (cyg_fileop_ioctl *) cyg_fileio_enosys,
  145. cyg_fileio_seltrue,
  146. (cyg_fileop_fsync *) cyg_fileio_enosys,
  147. jffs2_fo_close,
  148. (cyg_fileop_fstat *) cyg_fileio_enosys,
  149. (cyg_fileop_getinfo *) cyg_fileio_enosys,
  150. (cyg_fileop_setinfo *) cyg_fileio_enosys
  151. };
  152. //==========================================================================
  153. // STATIC VARIABLES !!!
  154. static unsigned char gc_buffer[PAGE_CACHE_SIZE]; //avoids malloc when user may be under memory pressure
  155. static unsigned char n_fs_mounted = 0; // a counter to track the number of jffs2 instances mounted
  156. //==========================================================================
  157. // Directory operations
  158. struct jffs2_dirsearch {
  159. struct _inode *dir; // directory to search
  160. const unsigned char *path; // path to follow
  161. struct _inode *node; // Node found
  162. const unsigned char *name; // last name fragment used
  163. int namelen; // name fragment length
  164. cyg_bool last; // last name in path?
  165. };
  166. typedef struct jffs2_dirsearch jffs2_dirsearch;
  167. //==========================================================================
  168. // Ref count and nlink management
  169. // FIXME: This seems like real cruft. Wouldn't it be better just to do the
  170. // right thing?
  171. static void icache_evict(struct _inode *root_i, struct _inode *i)
  172. {
  173. struct _inode *this = root_i, *next;
  174. restart:
  175. D2(printf("icache_evict\n"));
  176. // If this is an absolute search path from the root,
  177. // remove all cached inodes with i_count of zero (these are only
  178. // held where needed for dotdot filepaths)
  179. while (this) {
  180. next = this->i_cache_next;
  181. if (this != i && this->i_count == 0) {
  182. struct _inode *parent = this->i_parent;
  183. if (this->i_cache_next)
  184. this->i_cache_next->i_cache_prev = this->i_cache_prev;
  185. if (this->i_cache_prev)
  186. this->i_cache_prev->i_cache_next = this->i_cache_next;
  187. jffs2_clear_inode(this);
  188. memset(this, 0x5a, sizeof(*this));
  189. rt_free(this);
  190. if (parent && parent != this) {
  191. parent->i_count--;
  192. this = root_i;
  193. goto restart;
  194. }
  195. }
  196. this = next;
  197. }
  198. }
  199. //==========================================================================
  200. // Directory search
  201. // -------------------------------------------------------------------------
  202. // init_dirsearch()
  203. // Initialize a dirsearch object to start a search
  204. static void init_dirsearch(jffs2_dirsearch * ds,
  205. struct _inode *dir, const unsigned char *name)
  206. {
  207. D2(printf("init_dirsearch name = %s\n", name));
  208. D2(printf("init_dirsearch dir = %x\n", dir));
  209. dir->i_count++;
  210. ds->dir = dir;
  211. ds->path = name;
  212. ds->node = dir;
  213. ds->name = name;
  214. ds->namelen = 0;
  215. ds->last = false;
  216. }
  217. // -------------------------------------------------------------------------
  218. // find_entry()
  219. // Search a single directory for the next name in a path and update the
  220. // dirsearch object appropriately.
  221. static int find_entry(jffs2_dirsearch * ds)
  222. {
  223. struct _inode *dir = ds->dir;
  224. const unsigned char *name = ds->path;
  225. const unsigned char *n = name;
  226. char namelen = 0;
  227. struct _inode *d;
  228. D2(printf("find_entry\n"));
  229. // check that we really have a directory
  230. if (!S_ISDIR(dir->i_mode))
  231. return ENOTDIR;
  232. // Isolate the next element of the path name.
  233. while (*n != '\0' && *n != '/')
  234. n++, namelen++;
  235. // Check if this is the last path element.
  236. while( *n == '/') n++;
  237. if (*n == '\0')
  238. ds->last = true;
  239. // update name in dirsearch object
  240. ds->name = name;
  241. ds->namelen = namelen;
  242. if (name[0] == '.')
  243. switch (namelen) {
  244. default:
  245. break;
  246. case 2:
  247. // Dot followed by not Dot, treat as any other name
  248. if (name[1] != '.')
  249. break;
  250. // Dot Dot
  251. // Move back up the search path
  252. D2(printf("find_entry found ..\n"));
  253. ds->dir = ds->node;
  254. ds->node = ds->dir->i_parent;
  255. ds->node->i_count++;
  256. return ENOERR;
  257. case 1:
  258. // Dot is consumed
  259. D2(printf("find_entry found .\n"));
  260. ds->node = ds->dir;
  261. ds->dir->i_count++;
  262. return ENOERR;
  263. }
  264. // Here we have the name and its length set up.
  265. // Search the directory for a matching entry
  266. D2(printf("find_entry for name = %s\n", ds->path));
  267. d = jffs2_lookup(dir, name, namelen);
  268. D2(printf("find_entry got dir = %x\n", d));
  269. if (d == NULL)
  270. return ENOENT;
  271. if (IS_ERR(d))
  272. return -PTR_ERR(d);
  273. // If it's a new directory inode, increase refcount on its parent
  274. if (S_ISDIR(d->i_mode) && !d->i_parent) {
  275. d->i_parent = dir;
  276. dir->i_count++;
  277. }
  278. // pass back the node we have found
  279. ds->node = d;
  280. return ENOERR;
  281. }
  282. // -------------------------------------------------------------------------
  283. // jffs2_find()
  284. // Main interface to directory search code. This is used in all file
  285. // level operations to locate the object named by the pathname.
  286. // Returns with use count incremented on both the sought object and
  287. // the directory it was found in
  288. static int jffs2_find(jffs2_dirsearch * d)
  289. {
  290. int err;
  291. D2(printf("jffs2_find for path =%s\n", d->path));
  292. // Short circuit empty paths
  293. if (*(d->path) == '\0') {
  294. d->node->i_count++;
  295. return ENOERR;
  296. }
  297. // iterate down directory tree until we find the object
  298. // we want.
  299. for (;;) {
  300. err = find_entry(d);
  301. if (err != ENOERR)
  302. return err;
  303. if (d->last)
  304. return ENOERR;
  305. /* We're done with it, although it we found a subdir that
  306. will have caused the refcount to have been increased */
  307. jffs2_iput(d->dir);
  308. // Update dirsearch object to search next directory.
  309. d->dir = d->node;
  310. d->path += d->namelen;
  311. while (*(d->path) == '/')
  312. d->path++; // skip dirname separators
  313. }
  314. }
  315. //==========================================================================
  316. // Pathconf support
  317. // This function provides support for pathconf() and fpathconf().
  318. static int jffs2_pathconf(struct _inode *node, struct cyg_pathconf_info *info)
  319. {
  320. int err = ENOERR;
  321. D2(printf("jffs2_pathconf\n"));
  322. switch (info->name) {
  323. case _PC_LINK_MAX:
  324. info->value = LINK_MAX;
  325. break;
  326. case _PC_MAX_CANON:
  327. info->value = -1; // not supported
  328. err = EINVAL;
  329. break;
  330. case _PC_MAX_INPUT:
  331. info->value = -1; // not supported
  332. err = EINVAL;
  333. break;
  334. case _PC_NAME_MAX:
  335. info->value = JFFS2_NAME_MAX;
  336. break;
  337. case _PC_PATH_MAX:
  338. info->value = JFFS2_PATH_MAX;
  339. break;
  340. case _PC_PIPE_BUF:
  341. info->value = -1; // not supported
  342. err = EINVAL;
  343. break;
  344. case _PC_ASYNC_IO:
  345. info->value = -1; // not supported
  346. err = EINVAL;
  347. break;
  348. case _PC_CHOWN_RESTRICTED:
  349. info->value = -1; // not supported
  350. err = EINVAL;
  351. break;
  352. case _PC_NO_TRUNC:
  353. info->value = 0;
  354. break;
  355. case _PC_PRIO_IO:
  356. info->value = 0;
  357. break;
  358. case _PC_SYNC_IO:
  359. info->value = 0;
  360. break;
  361. case _PC_VDISABLE:
  362. info->value = -1; // not supported
  363. err = EINVAL;
  364. break;
  365. default:
  366. err = EINVAL;
  367. break;
  368. }
  369. return err;
  370. }
  371. //==========================================================================
  372. // Filesystem operations
  373. // -------------------------------------------------------------------------
  374. // jffs2_mount()
  375. // Process a mount request. This mainly creates a root for the
  376. // filesystem.
  377. static int jffs2_read_super(struct super_block *sb)
  378. {
  379. Cyg_ErrNo err;
  380. struct jffs2_sb_info *c;
  381. struct rt_mtd_nor_device *device;
  382. c = JFFS2_SB_INFO(sb);
  383. device = RT_MTD_NOR_DEVICE(sb->s_dev);
  384. /* initialize mutex lock */
  385. init_MUTEX(&c->alloc_sem);
  386. init_MUTEX(&c->erase_free_sem);
  387. /* sector size is the erase block size */
  388. c->sector_size = device->block_size;
  389. c->flash_size = (device->block_end - device->block_start) * device->block_size;
  390. c->cleanmarker_size = sizeof(struct jffs2_unknown_node);
  391. err = jffs2_do_mount_fs(c);
  392. if (err) return -err;
  393. D1(printk(KERN_DEBUG "jffs2_read_super(): Getting root inode\n"));
  394. sb->s_root = jffs2_iget(sb, 1);
  395. if (IS_ERR(sb->s_root)) {
  396. D1(printk(KERN_WARNING "get root inode failed\n"));
  397. err = PTR_ERR(sb->s_root);
  398. sb->s_root = NULL;
  399. goto out_nodes;
  400. }
  401. return 0;
  402. out_nodes:
  403. jffs2_free_ino_caches(c);
  404. jffs2_free_raw_node_refs(c);
  405. rt_free(c->blocks);
  406. return err;
  407. }
  408. int jffs2_mount(cyg_fstab_entry * fste, cyg_mtab_entry * mte)
  409. {
  410. // extern cyg_mtab_entry cyg_mtab[], cyg_mtab_end;
  411. struct super_block *jffs2_sb = NULL;
  412. struct jffs2_sb_info *c;
  413. // cyg_mtab_entry *m;
  414. cyg_io_handle_t t;
  415. Cyg_ErrNo err;
  416. D2(printf("jffs2_mount\n"));
  417. //prife
  418. // err = cyg_io_lookup(mte->devname, &t);
  419. // if (err != ENOERR)
  420. // return -err;
  421. // // Iterate through the mount table to see if we're mounted
  422. // // FIXME: this should be done better - perhaps if the superblock
  423. // // can be stored as an inode in the icache.
  424. // for (m = &cyg_mtab[0]; m != &cyg_mtab_end; m++) {
  425. // // stop if there are more than the configured maximum
  426. // if (m - &cyg_mtab[0] >= CYGNUM_FILEIO_MTAB_MAX) {
  427. // m = &cyg_mtab_end;
  428. // break;
  429. // }
  430. // if (m->valid && strcmp(m->fsname, "jffs2") == 0 &&
  431. // strcmp(m->devname, mte->devname) == 0) {
  432. // jffs2_sb = (struct super_block *) m->data;
  433. // }
  434. // }
  435. jffs2_sb = NULL;
  436. t = (cyg_io_handle_t)mte->data; //get from dfs_jffs2;
  437. if (jffs2_sb == NULL) {
  438. jffs2_sb = rt_malloc(sizeof (struct super_block));
  439. if (jffs2_sb == NULL)
  440. return ENOMEM;
  441. c = JFFS2_SB_INFO(jffs2_sb);
  442. memset(jffs2_sb, 0, sizeof (struct super_block));
  443. jffs2_sb->s_dev = t;
  444. c->inocache_list = rt_malloc(sizeof(struct jffs2_inode_cache *) * INOCACHE_HASHSIZE);
  445. if (!c->inocache_list) {
  446. rt_free(jffs2_sb);
  447. return ENOMEM;
  448. }
  449. memset(c->inocache_list, 0, sizeof(struct jffs2_inode_cache *) * INOCACHE_HASHSIZE);
  450. if (n_fs_mounted++ == 0) {
  451. jffs2_create_slab_caches(); // No error check, cannot fail
  452. jffs2_compressors_init();
  453. }
  454. err = jffs2_read_super(jffs2_sb);
  455. if (err) {
  456. if (--n_fs_mounted == 0) {
  457. jffs2_destroy_slab_caches();
  458. jffs2_compressors_exit();
  459. }
  460. rt_free(jffs2_sb);
  461. rt_free(c->inocache_list);
  462. return err;
  463. }
  464. jffs2_sb->s_root->i_parent = jffs2_sb->s_root; // points to itself, no dotdot paths above mountpoint
  465. jffs2_sb->s_root->i_cache_prev = NULL; // root inode, so always null
  466. jffs2_sb->s_root->i_cache_next = NULL;
  467. jffs2_sb->s_root->i_count = 1; // Ensures the root inode is always in ram until umount
  468. D2(printf("jffs2_mount erasing pending blocks\n"));
  469. #ifdef CYGOPT_FS_JFFS2_WRITE
  470. if (!jffs2_is_readonly(c))
  471. jffs2_erase_pending_blocks(c,0);
  472. #endif
  473. #ifdef CYGOPT_FS_JFFS2_GCTHREAD
  474. jffs2_start_garbage_collect_thread(c);
  475. #endif
  476. }
  477. mte->data = (CYG_ADDRWORD) jffs2_sb;
  478. jffs2_sb->s_mount_count++;
  479. mte->root = (cyg_dir) jffs2_sb->s_root;
  480. D2(printf("jffs2_mounted superblock at %x\n", mte->root));
  481. return ENOERR;
  482. }
  483. extern cyg_dir cyg_cdir_dir;
  484. extern cyg_mtab_entry *cyg_cdir_mtab_entry;
  485. // -------------------------------------------------------------------------
  486. // jffs2_umount()
  487. // Unmount the filesystem.
  488. static int jffs2_umount(cyg_mtab_entry * mte)
  489. {
  490. struct _inode *root = (struct _inode *) mte->root;
  491. struct super_block *jffs2_sb = root->i_sb;
  492. struct jffs2_sb_info *c = JFFS2_SB_INFO(jffs2_sb);
  493. struct jffs2_full_dirent *fd, *next;
  494. D2(printf("jffs2_umount\n"));
  495. // Only really umount if this is the only mount
  496. if (jffs2_sb->s_mount_count == 1) {
  497. icache_evict(root, NULL);
  498. if (root->i_cache_next != NULL) {
  499. struct _inode *inode = root;
  500. printf("Refuse to unmount.\n");
  501. while (inode) {
  502. printf("Ino #%u has use count %d\n",
  503. inode->i_ino, inode->i_count);
  504. inode = inode->i_cache_next;
  505. }
  506. // root icount was set to 1 on mount
  507. return EBUSY;
  508. }
  509. if (root->i_count == 2 &&
  510. cyg_cdir_mtab_entry == mte &&
  511. cyg_cdir_dir == (cyg_dir)root &&
  512. !strcmp(mte->name, "/")) {
  513. /* If we were mounted on root, there's no
  514. way for the cwd to change out and free
  515. the file system for unmounting. So we hack
  516. it -- if cwd is '/' we unset it. Perhaps
  517. we should allow chdir(NULL) to unset
  518. cyg_cdir_dir? */
  519. cyg_cdir_dir = CYG_DIR_NULL;
  520. jffs2_iput(root);
  521. }
  522. /* Argh. The fileio code sets this; never clears it */
  523. if (cyg_cdir_mtab_entry == mte)
  524. cyg_cdir_mtab_entry = NULL;
  525. if (root->i_count != 1) {
  526. printf("Ino #1 has use count %d\n",
  527. root->i_count);
  528. return EBUSY;
  529. }
  530. #ifdef CYGOPT_FS_JFFS2_GCTHREAD
  531. jffs2_stop_garbage_collect_thread(c);
  532. #endif
  533. jffs2_iput(root); // Time to free the root inode
  534. // free directory entries
  535. for (fd = root->jffs2_i.dents; fd; fd = next) {
  536. next=fd->next;
  537. jffs2_free_full_dirent(fd);
  538. }
  539. rt_free(root);
  540. //Clear root inode
  541. //root_i = NULL;
  542. // Clean up the super block and root inode
  543. jffs2_free_ino_caches(c);
  544. jffs2_free_raw_node_refs(c);
  545. rt_free(c->blocks);
  546. rt_free(c->inocache_list);
  547. rt_free(jffs2_sb);
  548. // Clear superblock & root pointer
  549. mte->root = CYG_DIR_NULL;
  550. mte->data = 0;
  551. mte->fs->data = 0; // fstab entry, visible to all mounts. No current mount
  552. // That's all folks.
  553. D2(printf("jffs2_umount No current mounts\n"));
  554. } else {
  555. jffs2_sb->s_mount_count--;
  556. }
  557. if (--n_fs_mounted == 0) {
  558. jffs2_destroy_slab_caches();
  559. jffs2_compressors_exit();
  560. }
  561. return ENOERR;
  562. }
  563. // -------------------------------------------------------------------------
  564. // jffs2_open()
  565. // Open a file for reading or writing.
  566. int jffs2_open(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
  567. int mode, cyg_file * file)
  568. {
  569. jffs2_dirsearch ds;
  570. struct _inode *node = NULL;
  571. int err;
  572. D2(printf("jffs2_open\n"));
  573. /* If no chdir has been called and we were the first file system
  574. mounted, we get called with dir == NULL. Deal with it */
  575. if (!dir)
  576. dir = mte->root;
  577. #ifndef CYGOPT_FS_JFFS2_WRITE
  578. if (mode & (O_CREAT|O_TRUNC|O_WRONLY))
  579. return EROFS;
  580. #endif
  581. init_dirsearch(&ds, (struct _inode *) dir,
  582. (const unsigned char *) name);
  583. err = jffs2_find(&ds);
  584. if (err == ENOENT) {
  585. #ifdef CYGOPT_FS_JFFS2_WRITE
  586. if (ds.last && (mode & O_CREAT)) {
  587. // No node there, if the O_CREAT bit is set then we must
  588. // create a new one. The dir and name fields of the dirsearch
  589. // object will have been updated so we know where to put it.
  590. err = jffs2_create(ds.dir, ds.name, S_IRUGO|S_IXUGO|S_IWUSR|S_IFREG, &node);
  591. if (err != 0) {
  592. //Possible orphaned inode on the flash - but will be gc'd
  593. jffs2_iput(ds.dir);
  594. return -err;
  595. }
  596. err = ENOERR;
  597. }
  598. #endif
  599. } else if (err == ENOERR) {
  600. // The node exists. If the O_CREAT and O_EXCL bits are set, we
  601. // must fail the open.
  602. if ((mode & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) {
  603. jffs2_iput(ds.node);
  604. err = EEXIST;
  605. } else
  606. node = ds.node;
  607. }
  608. // Finished with the directory now
  609. jffs2_iput(ds.dir);
  610. if (err != ENOERR)
  611. return err;
  612. // Check that we actually have a file here
  613. if (S_ISDIR(node->i_mode)) {
  614. jffs2_iput(node);
  615. return EISDIR;
  616. }
  617. // If the O_TRUNC bit is set we must clean out the file data.
  618. if (mode & O_TRUNC) {
  619. #ifdef CYGOPT_FS_JFFS2_WRITE
  620. err = jffs2_truncate_file(node);
  621. if (err) {
  622. jffs2_iput(node);
  623. return err;
  624. }
  625. #else
  626. jffs2_iput(node);
  627. return EROFS;
  628. #endif
  629. }
  630. // Initialise the file object
  631. file->f_flag = mode & CYG_FILE_MODE_MASK;
  632. file->f_type = CYG_FILE_TYPE_FILE;
  633. file->f_ops = &jffs2_fileops;
  634. file->f_offset = (mode & O_APPEND) ? node->i_size : 0;
  635. file->f_data = (CYG_ADDRWORD) node;
  636. file->f_xops = 0;
  637. return ENOERR;
  638. }
  639. #ifdef CYGOPT_FS_JFFS2_WRITE
  640. // -------------------------------------------------------------------------
  641. // jffs2_ops_unlink()
  642. // Remove a file link from its directory.
  643. static int jffs2_ops_unlink(cyg_mtab_entry * mte, cyg_dir dir, const char *name)
  644. {
  645. jffs2_dirsearch ds;
  646. int err;
  647. D2(printf("jffs2_ops_unlink\n"));
  648. init_dirsearch(&ds, (struct _inode *) dir,
  649. (const unsigned char *)name);
  650. err = jffs2_find(&ds);
  651. if (err != ENOERR) {
  652. jffs2_iput(ds.dir);
  653. return err;
  654. }
  655. // Cannot unlink directories, use rmdir() instead
  656. if (S_ISDIR(ds.node->i_mode)) {
  657. jffs2_iput(ds.dir);
  658. jffs2_iput(ds.node);
  659. return EPERM;
  660. }
  661. // Delete it from its directory
  662. err = jffs2_unlink(ds.dir, ds.node, ds.name);
  663. jffs2_iput(ds.dir);
  664. jffs2_iput(ds.node);
  665. return -err;
  666. }
  667. // -------------------------------------------------------------------------
  668. // jffs2_ops_mkdir()
  669. // Create a new directory.
  670. static int jffs2_ops_mkdir(cyg_mtab_entry * mte, cyg_dir dir, const char *name)
  671. {
  672. jffs2_dirsearch ds;
  673. int err;
  674. D2(printf("jffs2_ops_mkdir\n"));
  675. init_dirsearch(&ds, (struct _inode *) dir,
  676. (const unsigned char *)name);
  677. err = jffs2_find(&ds);
  678. if (err == ENOENT) {
  679. if (ds.last) {
  680. // The entry does not exist, and it is the last element in
  681. // the pathname, so we can create it here.
  682. err = -jffs2_mkdir(ds.dir, ds.name, S_IRUGO|S_IXUGO|S_IWUSR);
  683. }
  684. // If this was not the last element, then an intermediate
  685. // directory does not exist.
  686. } else {
  687. // If there we no error, something already exists with that
  688. // name, so we cannot create another one.
  689. if (err == ENOERR) {
  690. jffs2_iput(ds.node);
  691. err = EEXIST;
  692. }
  693. }
  694. jffs2_iput(ds.dir);
  695. return err;
  696. }
  697. // -------------------------------------------------------------------------
  698. // jffs2_ops_rmdir()
  699. // Remove a directory.
  700. static int jffs2_ops_rmdir(cyg_mtab_entry * mte, cyg_dir dir, const char *name)
  701. {
  702. jffs2_dirsearch ds;
  703. int err;
  704. D2(printf("jffs2_ops_rmdir\n"));
  705. init_dirsearch(&ds, (struct _inode *) dir,
  706. (const unsigned char *)name);
  707. err = jffs2_find(&ds);
  708. if (err != ENOERR) {
  709. jffs2_iput(ds.dir);
  710. return err;
  711. }
  712. // Check that this is actually a directory.
  713. if (!S_ISDIR(ds.node->i_mode)) {
  714. jffs2_iput(ds.dir);
  715. jffs2_iput(ds.node);
  716. return EPERM;
  717. }
  718. err = jffs2_rmdir(ds.dir, ds.node, ds.name);
  719. jffs2_iput(ds.dir);
  720. jffs2_iput(ds.node);
  721. return -err;
  722. }
  723. // -------------------------------------------------------------------------
  724. // jffs2_ops_rename()
  725. // Rename a file/dir.
  726. static int jffs2_ops_rename(cyg_mtab_entry * mte, cyg_dir dir1,
  727. const char *name1, cyg_dir dir2, const char *name2)
  728. {
  729. jffs2_dirsearch ds1, ds2;
  730. int err;
  731. D2(printf("jffs2_ops_rename\n"));
  732. init_dirsearch(&ds1, (struct _inode *) dir1,
  733. (const unsigned char *)name1);
  734. err = jffs2_find(&ds1);
  735. if (err != ENOERR) {
  736. jffs2_iput(ds1.dir);
  737. return err;
  738. }
  739. init_dirsearch(&ds2, (struct _inode *) dir2,
  740. (const unsigned char *)name2);
  741. err = jffs2_find(&ds2);
  742. // Allow through renames to non-existent objects.
  743. if (ds2.last && err == ENOENT) {
  744. ds2.node = NULL;
  745. err = ENOERR;
  746. }
  747. if (err != ENOERR) {
  748. jffs2_iput(ds1.dir);
  749. jffs2_iput(ds1.node);
  750. jffs2_iput(ds2.dir);
  751. return err;
  752. }
  753. // Null rename, just return
  754. if (ds1.node == ds2.node) {
  755. err = ENOERR;
  756. goto out;
  757. }
  758. // First deal with any entry that is at the destination
  759. if (ds2.node) {
  760. // Check that we are renaming like-for-like
  761. if (!S_ISDIR(ds1.node->i_mode) && S_ISDIR(ds2.node->i_mode)) {
  762. err = EISDIR;
  763. goto out;
  764. }
  765. if (S_ISDIR(ds1.node->i_mode) && !S_ISDIR(ds2.node->i_mode)) {
  766. err = ENOTDIR;
  767. goto out;
  768. }
  769. // Now delete the destination directory entry
  770. /* Er, what happened to atomicity of rename()? */
  771. err = -jffs2_unlink(ds2.dir, ds2.node, ds2.name);
  772. if (err != 0)
  773. goto out;
  774. }
  775. // Now we know that there is no clashing node at the destination,
  776. // make a new direntry at the destination and delete the old entry
  777. // at the source.
  778. err = -jffs2_rename(ds1.dir, ds1.node, ds1.name, ds2.dir, ds2.name);
  779. // Update directory times
  780. if (!err)
  781. ds1.dir->i_ctime =
  782. ds1.dir->i_mtime =
  783. ds2.dir->i_ctime = ds2.dir->i_mtime = jffs2_get_timestamp();
  784. out:
  785. jffs2_iput(ds1.dir);
  786. if (S_ISDIR(ds1.node->i_mode)) {
  787. /* Renamed a directory to elsewhere... so fix up its
  788. i_parent pointer and the i_counts of its old and
  789. new parents. */
  790. jffs2_iput(ds1.node->i_parent);
  791. ds1.node->i_parent = ds2.dir;
  792. /* We effectively increase its use count by not... */
  793. } else {
  794. jffs2_iput(ds2.dir); /* ... doing this */
  795. }
  796. jffs2_iput(ds1.node);
  797. if (ds2.node)
  798. jffs2_iput(ds2.node);
  799. return err;
  800. }
  801. // -------------------------------------------------------------------------
  802. // jffs2_ops_link()
  803. // Make a new directory entry for a file.
  804. static int jffs2_ops_link(cyg_mtab_entry * mte, cyg_dir dir1, const char *name1,
  805. cyg_dir dir2, const char *name2, int type)
  806. {
  807. jffs2_dirsearch ds1, ds2;
  808. int err;
  809. D2(printf("jffs2_ops_link\n"));
  810. // Only do hard links for now in this filesystem
  811. if (type != CYG_FSLINK_HARD)
  812. return EINVAL;
  813. init_dirsearch(&ds1, (struct _inode *) dir1,
  814. (const unsigned char *) name1);
  815. err = jffs2_find(&ds1);
  816. if (err != ENOERR) {
  817. jffs2_iput(ds1.dir);
  818. return err;
  819. }
  820. init_dirsearch(&ds2, (struct _inode *) dir2,
  821. (const unsigned char *) name2);
  822. err = jffs2_find(&ds2);
  823. // Don't allow links to existing objects
  824. if (err == ENOERR) {
  825. jffs2_iput(ds1.dir);
  826. jffs2_iput(ds1.node);
  827. jffs2_iput(ds2.dir);
  828. jffs2_iput(ds2.node);
  829. return EEXIST;
  830. }
  831. // Allow through links to non-existing terminal objects
  832. if (ds2.last && err == ENOENT) {
  833. ds2.node = NULL;
  834. err = ENOERR;
  835. }
  836. if (err != ENOERR) {
  837. jffs2_iput(ds1.dir);
  838. jffs2_iput(ds1.node);
  839. jffs2_iput(ds2.dir);
  840. return err;
  841. }
  842. // Now we know that there is no existing node at the destination,
  843. // make a new direntry at the destination.
  844. err = jffs2_link(ds1.node, ds2.dir, ds2.name);
  845. if (err == 0)
  846. ds1.node->i_ctime =
  847. ds2.dir->i_ctime = ds2.dir->i_mtime = jffs2_get_timestamp();
  848. jffs2_iput(ds1.dir);
  849. jffs2_iput(ds1.node);
  850. jffs2_iput(ds2.dir);
  851. return -err;
  852. }
  853. #endif /* CYGOPT_FS_JFFS2_WRITE */
  854. // -------------------------------------------------------------------------
  855. // jffs2_opendir()
  856. // Open a directory for reading.
  857. static int jffs2_opendir(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
  858. cyg_file * file)
  859. {
  860. jffs2_dirsearch ds;
  861. int err;
  862. D2(printf("jffs2_opendir\n"));
  863. init_dirsearch(&ds, (struct _inode *) dir,
  864. (const unsigned char *) name);
  865. err = jffs2_find(&ds);
  866. jffs2_iput(ds.dir);
  867. if (err != ENOERR)
  868. return err;
  869. // check it is really a directory.
  870. if (!S_ISDIR(ds.node->i_mode)) {
  871. jffs2_iput(ds.node);
  872. return ENOTDIR;
  873. }
  874. // Initialize the file object, setting the f_ops field to a
  875. // special set of file ops.
  876. file->f_flag = 0;
  877. file->f_type = CYG_FILE_TYPE_FILE;
  878. file->f_ops = &jffs2_dirops;
  879. file->f_offset = 0;
  880. file->f_data = (CYG_ADDRWORD) ds.node;
  881. file->f_xops = 0;
  882. return ENOERR;
  883. }
  884. // -------------------------------------------------------------------------
  885. // jffs2_chdir()
  886. // Change directory support.
  887. static int jffs2_chdir(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
  888. cyg_dir * dir_out)
  889. {
  890. D2(printf("jffs2_chdir\n"));
  891. if (dir_out != NULL) {
  892. // This is a request to get a new directory pointer in
  893. // *dir_out.
  894. jffs2_dirsearch ds;
  895. int err;
  896. init_dirsearch(&ds, (struct _inode *) dir,
  897. (const unsigned char *) name);
  898. err = jffs2_find(&ds);
  899. jffs2_iput(ds.dir);
  900. if (err != ENOERR)
  901. return err;
  902. // check it is a directory
  903. if (!S_ISDIR(ds.node->i_mode)) {
  904. jffs2_iput(ds.node);
  905. return ENOTDIR;
  906. }
  907. // Pass it out
  908. *dir_out = (cyg_dir) ds.node;
  909. } else {
  910. // If no output dir is required, this means that the mte and
  911. // dir arguments are the current cdir setting and we should
  912. // forget this fact.
  913. struct _inode *node = (struct _inode *) dir;
  914. // Just decrement directory reference count.
  915. jffs2_iput(node);
  916. }
  917. return ENOERR;
  918. }
  919. // -------------------------------------------------------------------------
  920. // jffs2_stat()
  921. // Get struct stat info for named object.
  922. static int jffs2_stat(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
  923. struct stat *buf)
  924. {
  925. jffs2_dirsearch ds;
  926. int err;
  927. D2(printf("jffs2_stat\n"));
  928. init_dirsearch(&ds, (struct _inode *) dir,
  929. (const unsigned char *) name);
  930. err = jffs2_find(&ds);
  931. jffs2_iput(ds.dir);
  932. if (err != ENOERR)
  933. return err;
  934. // Fill in the status
  935. buf->st_mode = ds.node->i_mode;
  936. buf->st_ino = ds.node->i_ino;
  937. buf->st_dev = 0;
  938. buf->st_nlink = ds.node->i_nlink;
  939. buf->st_uid = ds.node->i_uid;
  940. buf->st_gid = ds.node->i_gid;
  941. buf->st_size = ds.node->i_size;
  942. buf->st_atime = ds.node->i_atime;
  943. buf->st_mtime = ds.node->i_mtime;
  944. buf->st_ctime = ds.node->i_ctime;
  945. jffs2_iput(ds.node);
  946. return ENOERR;
  947. }
  948. // -------------------------------------------------------------------------
  949. // jffs2_getinfo()
  950. // Getinfo. Currently only support pathconf().
  951. static int jffs2_getinfo(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
  952. int key, void *buf, int len)
  953. {
  954. jffs2_dirsearch ds;
  955. int err;
  956. D2(printf("jffs2_getinfo\n"));
  957. init_dirsearch(&ds, (struct _inode *) dir,
  958. (const unsigned char *) name);
  959. err = jffs2_find(&ds);
  960. jffs2_iput(ds.dir);
  961. if (err != ENOERR)
  962. return err;
  963. switch (key) {
  964. case FS_INFO_CONF:
  965. err = jffs2_pathconf(ds.node, (struct cyg_pathconf_info *) buf);
  966. break;
  967. default:
  968. err = EINVAL;
  969. }
  970. jffs2_iput(ds.node);
  971. return err;
  972. }
  973. // -------------------------------------------------------------------------
  974. // jffs2_setinfo()
  975. // Setinfo. Nothing to support here at present.
  976. static int jffs2_setinfo(cyg_mtab_entry * mte, cyg_dir dir, const char *name,
  977. int key, void *buf, int len)
  978. {
  979. // No setinfo keys supported at present
  980. D2(printf("jffs2_setinfo\n"));
  981. return EINVAL;
  982. }
  983. //==========================================================================
  984. // File operations
  985. // -------------------------------------------------------------------------
  986. // jffs2_fo_read()
  987. // Read data from the file.
  988. int jffs2_fo_read(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  989. {
  990. struct _inode *inode = (struct _inode *) fp->f_data;
  991. struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  992. struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
  993. int i;
  994. ssize_t resid = uio->uio_resid;
  995. off_t pos = fp->f_offset;
  996. down(&f->sem);
  997. // Loop over the io vectors until there are none left
  998. for (i = 0; i < uio->uio_iovcnt && pos < inode->i_size; i++) {
  999. int ret;
  1000. cyg_iovec *iov = &uio->uio_iov[i];
  1001. off_t len = min(iov->iov_len, inode->i_size - pos);
  1002. D2(printf("jffs2_fo_read inode size %d\n", inode->i_size));
  1003. ret =
  1004. jffs2_read_inode_range(c, f,
  1005. (unsigned char *) iov->iov_base, pos,
  1006. len);
  1007. if (ret) {
  1008. D1(printf
  1009. ("jffs2_fo_read(): read_inode_range failed %d\n",
  1010. ret));
  1011. uio->uio_resid = resid;
  1012. up(&f->sem);
  1013. return -ret;
  1014. }
  1015. resid -= len;
  1016. pos += len;
  1017. }
  1018. // We successfully read some data, update the node's access time
  1019. // and update the file offset and transfer residue.
  1020. inode->i_atime = jffs2_get_timestamp();
  1021. uio->uio_resid = resid;
  1022. fp->f_offset = pos;
  1023. up(&f->sem);
  1024. return ENOERR;
  1025. }
  1026. #ifdef CYGOPT_FS_JFFS2_WRITE
  1027. // -------------------------------------------------------------------------
  1028. // jffs2_fo_write()
  1029. // Write data to file.
  1030. static int jffs2_extend_file (struct _inode *inode, struct jffs2_raw_inode *ri,
  1031. unsigned long offset)
  1032. {
  1033. struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
  1034. struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  1035. struct jffs2_full_dnode *fn;
  1036. uint32_t phys_ofs, alloc_len;
  1037. int ret = 0;
  1038. /* Make new hole frag from old EOF to new page */
  1039. D1(printk(KERN_DEBUG "Writing new hole frag 0x%x-0x%x between current EOF and new page\n",
  1040. (unsigned int)inode->i_size, offset));
  1041. ret = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloc_len, ALLOC_NORMAL);
  1042. if (ret)
  1043. return ret;
  1044. down(&f->sem);
  1045. ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  1046. ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
  1047. ri->totlen = cpu_to_je32(sizeof(*ri));
  1048. ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
  1049. ri->version = cpu_to_je32(++f->highest_version);
  1050. ri->isize = cpu_to_je32(max((uint32_t)inode->i_size, offset));
  1051. ri->offset = cpu_to_je32(inode->i_size);
  1052. ri->dsize = cpu_to_je32(offset - inode->i_size);
  1053. ri->csize = cpu_to_je32(0);
  1054. ri->compr = JFFS2_COMPR_ZERO;
  1055. ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  1056. ri->data_crc = cpu_to_je32(0);
  1057. fn = jffs2_write_dnode(c, f, ri, NULL, 0, phys_ofs, ALLOC_NORMAL);
  1058. jffs2_complete_reservation(c);
  1059. if (IS_ERR(fn)) {
  1060. ret = PTR_ERR(fn);
  1061. up(&f->sem);
  1062. return ret;
  1063. }
  1064. ret = jffs2_add_full_dnode_to_inode(c, f, fn);
  1065. if (f->metadata) {
  1066. jffs2_mark_node_obsolete(c, f->metadata->raw);
  1067. jffs2_free_full_dnode(f->metadata);
  1068. f->metadata = NULL;
  1069. }
  1070. if (ret) {
  1071. D1(printk(KERN_DEBUG "Eep. add_full_dnode_to_inode() failed in prepare_write, returned %d\n", ret));
  1072. jffs2_mark_node_obsolete(c, fn->raw);
  1073. jffs2_free_full_dnode(fn);
  1074. up(&f->sem);
  1075. return ret;
  1076. }
  1077. inode->i_size = offset;
  1078. up(&f->sem);
  1079. return 0;
  1080. }
  1081. // jffs2_fo_open()
  1082. // Truncate a file
  1083. static int jffs2_truncate_file (struct _inode *inode)
  1084. {
  1085. struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  1086. struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
  1087. struct jffs2_full_dnode *new_metadata, * old_metadata;
  1088. struct jffs2_raw_inode *ri;
  1089. uint32_t phys_ofs, alloclen;
  1090. int err;
  1091. ri = jffs2_alloc_raw_inode();
  1092. if (!ri) {
  1093. return ENOMEM;
  1094. }
  1095. err = jffs2_reserve_space(c, sizeof(*ri), &phys_ofs, &alloclen, ALLOC_NORMAL);
  1096. if (err) {
  1097. jffs2_free_raw_inode(ri);
  1098. return err;
  1099. }
  1100. down(&f->sem);
  1101. ri->magic = cpu_to_je16(JFFS2_MAGIC_BITMASK);
  1102. ri->nodetype = cpu_to_je16(JFFS2_NODETYPE_INODE);
  1103. ri->totlen = cpu_to_je32(sizeof(*ri));
  1104. ri->hdr_crc = cpu_to_je32(crc32(0, ri, sizeof(struct jffs2_unknown_node)-4));
  1105. ri->ino = cpu_to_je32(inode->i_ino);
  1106. ri->version = cpu_to_je32(++f->highest_version);
  1107. ri->uid = cpu_to_je16(inode->i_uid);
  1108. ri->gid = cpu_to_je16(inode->i_gid);
  1109. ri->mode = cpu_to_jemode(inode->i_mode);
  1110. ri->isize = cpu_to_je32(0);
  1111. ri->atime = cpu_to_je32(inode->i_atime);
  1112. ri->mtime = cpu_to_je32(jffs2_get_timestamp());
  1113. ri->offset = cpu_to_je32(0);
  1114. ri->csize = ri->dsize = cpu_to_je32(0);
  1115. ri->compr = JFFS2_COMPR_NONE;
  1116. ri->node_crc = cpu_to_je32(crc32(0, ri, sizeof(*ri)-8));
  1117. ri->data_crc = cpu_to_je32(0);
  1118. new_metadata = jffs2_write_dnode(c, f, ri, NULL, 0,
  1119. phys_ofs, ALLOC_NORMAL);
  1120. if (IS_ERR(new_metadata)) {
  1121. jffs2_complete_reservation(c);
  1122. jffs2_free_raw_inode(ri);
  1123. up(&f->sem);
  1124. return PTR_ERR(new_metadata);
  1125. }
  1126. /* It worked. Update the inode */
  1127. inode->i_mtime = jffs2_get_timestamp();
  1128. inode->i_size = 0;
  1129. old_metadata = f->metadata;
  1130. jffs2_truncate_fragtree (c, &f->fragtree, 0);
  1131. f->metadata = new_metadata;
  1132. if (old_metadata) {
  1133. jffs2_mark_node_obsolete(c, old_metadata->raw);
  1134. jffs2_free_full_dnode(old_metadata);
  1135. }
  1136. jffs2_free_raw_inode(ri);
  1137. up(&f->sem);
  1138. jffs2_complete_reservation(c);
  1139. return 0;
  1140. }
  1141. static int jffs2_fo_write(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1142. {
  1143. struct _inode *inode = (struct _inode *) fp->f_data;
  1144. off_t pos = fp->f_offset;
  1145. ssize_t resid = uio->uio_resid;
  1146. struct jffs2_raw_inode ri;
  1147. struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  1148. struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
  1149. int i;
  1150. // If the APPEND mode bit was supplied, force all writes to
  1151. // the end of the file.
  1152. if (fp->f_flag & CYG_FAPPEND)
  1153. pos = fp->f_offset = inode->i_size;
  1154. if (pos < 0)
  1155. return EINVAL;
  1156. memset(&ri, 0, sizeof(ri));
  1157. ri.ino = cpu_to_je32(f->inocache->ino);
  1158. ri.mode = cpu_to_jemode(inode->i_mode);
  1159. ri.uid = cpu_to_je16(inode->i_uid);
  1160. ri.gid = cpu_to_je16(inode->i_gid);
  1161. ri.atime = ri.ctime = ri.mtime = cpu_to_je32(jffs2_get_timestamp());
  1162. if (pos > inode->i_size) {
  1163. int err;
  1164. ri.version = cpu_to_je32(++f->highest_version);
  1165. err = jffs2_extend_file(inode, &ri, pos);
  1166. if (err)
  1167. return -err;
  1168. }
  1169. ri.isize = cpu_to_je32(inode->i_size);
  1170. // Now loop over the iovecs until they are all done, or
  1171. // we get an error.
  1172. for (i = 0; i < uio->uio_iovcnt; i++) {
  1173. cyg_iovec *iov = &uio->uio_iov[i];
  1174. unsigned char *buf = iov->iov_base;
  1175. off_t len = iov->iov_len;
  1176. uint32_t writtenlen;
  1177. int err;
  1178. D2(printf("jffs2_fo_write page_start_pos %d\n", pos));
  1179. D2(printf("jffs2_fo_write transfer size %d\n", len));
  1180. err = jffs2_write_inode_range(c, f, &ri, buf,
  1181. pos, len, &writtenlen);
  1182. if (err)
  1183. return -err;
  1184. if (writtenlen != len)
  1185. return ENOSPC;
  1186. pos += len;
  1187. resid -= len;
  1188. }
  1189. // We wrote some data successfully, update the modified and access
  1190. // times of the inode, increase its size appropriately, and update
  1191. // the file offset and transfer residue.
  1192. inode->i_mtime = inode->i_ctime = je32_to_cpu(ri.mtime);
  1193. if (pos > inode->i_size)
  1194. inode->i_size = pos;
  1195. uio->uio_resid = resid;
  1196. fp->f_offset = pos;
  1197. return ENOERR;
  1198. }
  1199. #endif /* CYGOPT_FS_JFFS2_WRITE */
  1200. // -------------------------------------------------------------------------
  1201. // jffs2_fo_lseek()
  1202. // Seek to a new file position.
  1203. static int jffs2_fo_lseek(struct CYG_FILE_TAG *fp, off_t * apos, int whence)
  1204. {
  1205. struct _inode *node = (struct _inode *) fp->f_data;
  1206. off_t pos = *apos;
  1207. D2(printf("jffs2_fo_lseek\n"));
  1208. switch (whence) {
  1209. case SEEK_SET:
  1210. // Pos is already where we want to be.
  1211. break;
  1212. case SEEK_CUR:
  1213. // Add pos to current offset.
  1214. pos += fp->f_offset;
  1215. break;
  1216. case SEEK_END:
  1217. // Add pos to file size.
  1218. pos += node->i_size;
  1219. break;
  1220. default:
  1221. return EINVAL;
  1222. }
  1223. if (pos < 0 )
  1224. return EINVAL;
  1225. // All OK, set fp offset and return new position.
  1226. *apos = fp->f_offset = pos;
  1227. return ENOERR;
  1228. }
  1229. // -------------------------------------------------------------------------
  1230. // jffs2_fo_ioctl()
  1231. // Handle ioctls. Currently none are defined.
  1232. static int jffs2_fo_ioctl(struct CYG_FILE_TAG *fp, CYG_ADDRWORD com,
  1233. CYG_ADDRWORD data)
  1234. {
  1235. // No Ioctls currenly defined.
  1236. D2(printf("jffs2_fo_ioctl\n"));
  1237. return EINVAL;
  1238. }
  1239. // -------------------------------------------------------------------------
  1240. // jffs2_fo_fsync().
  1241. // Force the file out to data storage.
  1242. static int jffs2_fo_fsync(struct CYG_FILE_TAG *fp, int mode)
  1243. {
  1244. // Data is always permanently where it belongs, nothing to do
  1245. // here.
  1246. D2(printf("jffs2_fo_fsync\n"));
  1247. return ENOERR;
  1248. }
  1249. // -------------------------------------------------------------------------
  1250. // jffs2_fo_close()
  1251. // Close a file. We just decrement the refcnt and let it go away if
  1252. // that is all that is keeping it here.
  1253. static int jffs2_fo_close(struct CYG_FILE_TAG *fp)
  1254. {
  1255. struct _inode *node = (struct _inode *) fp->f_data;
  1256. D2(printf("jffs2_fo_close\n"));
  1257. jffs2_iput(node);
  1258. fp->f_data = 0; // zero data pointer
  1259. return ENOERR;
  1260. }
  1261. // -------------------------------------------------------------------------
  1262. //jffs2_fo_fstat()
  1263. // Get file status.
  1264. static int jffs2_fo_fstat(struct CYG_FILE_TAG *fp, struct stat *buf)
  1265. {
  1266. struct _inode *node = (struct _inode *) fp->f_data;
  1267. D2(printf("jffs2_fo_fstat\n"));
  1268. // Fill in the status
  1269. buf->st_mode = node->i_mode;
  1270. buf->st_ino = node->i_ino;
  1271. buf->st_dev = 0;
  1272. buf->st_nlink = node->i_nlink;
  1273. buf->st_uid = node->i_uid;
  1274. buf->st_gid = node->i_gid;
  1275. buf->st_size = node->i_size;
  1276. buf->st_atime = node->i_atime;
  1277. buf->st_mtime = node->i_mtime;
  1278. buf->st_ctime = node->i_ctime;
  1279. return ENOERR;
  1280. }
  1281. // -------------------------------------------------------------------------
  1282. // jffs2_fo_getinfo()
  1283. // Get info. Currently only supports fpathconf().
  1284. static int jffs2_fo_getinfo(struct CYG_FILE_TAG *fp, int key, void *buf,
  1285. int len)
  1286. {
  1287. struct _inode *node = (struct _inode *) fp->f_data;
  1288. int err;
  1289. D2(printf("jffs2_fo_getinfo\n"));
  1290. switch (key) {
  1291. case FS_INFO_CONF:
  1292. err = jffs2_pathconf(node, (struct cyg_pathconf_info *) buf);
  1293. break;
  1294. default:
  1295. err = EINVAL;
  1296. }
  1297. return err;
  1298. }
  1299. // -------------------------------------------------------------------------
  1300. // jffs2_fo_setinfo()
  1301. // Set info. Nothing supported here.
  1302. static int jffs2_fo_setinfo(struct CYG_FILE_TAG *fp, int key, void *buf,
  1303. int len)
  1304. {
  1305. // No setinfo key supported at present
  1306. D2(printf("jffs2_fo_setinfo\n"));
  1307. return ENOERR;
  1308. }
  1309. //==========================================================================
  1310. // Directory operations
  1311. // -------------------------------------------------------------------------
  1312. // jffs2_fo_dirread()
  1313. // Read a single directory entry from a file.
  1314. static __inline void filldir(char *nbuf, int nlen, const unsigned char *name, int namlen)
  1315. {
  1316. int len = nlen < namlen ? nlen : namlen;
  1317. memcpy(nbuf, name, len);
  1318. nbuf[len] = '\0';
  1319. }
  1320. static int jffs2_fo_dirread(struct CYG_FILE_TAG *fp, struct CYG_UIO_TAG *uio)
  1321. {
  1322. struct _inode *d_inode = (struct _inode *) fp->f_data;
  1323. struct dirent *ent = (struct dirent *) uio->uio_iov[0].iov_base;
  1324. char *nbuf = ent->d_name;
  1325. #ifdef CYGPKG_FS_JFFS2_RET_DIRENT_DTYPE
  1326. struct _inode *c_ino;
  1327. #endif
  1328. int nlen = sizeof (ent->d_name) - 1;
  1329. off_t len = uio->uio_iov[0].iov_len;
  1330. struct jffs2_inode_info *f;
  1331. struct _inode *inode = d_inode;
  1332. struct jffs2_full_dirent *fd;
  1333. unsigned long offset, curofs;
  1334. int found = 1;
  1335. if (len < sizeof (struct dirent))
  1336. return EINVAL;
  1337. D1(printk
  1338. (KERN_DEBUG "jffs2_readdir() for dir_i #%lu\n", d_inode->i_ino));
  1339. f = JFFS2_INODE_INFO(inode);
  1340. offset = fp->f_offset;
  1341. if (offset == 0) {
  1342. D1(printk
  1343. (KERN_DEBUG "Dirent 0: \".\", ino #%lu\n", inode->i_ino));
  1344. filldir(nbuf, nlen, (const unsigned char *) ".", 1);
  1345. #ifdef CYGPKG_FS_JFFS2_RET_DIRENT_DTYPE
  1346. // Flags here are the same as jffs2_mkdir. Make sure
  1347. // d_type is the same as st_mode of calling stat.
  1348. ent->d_type =
  1349. jemode_to_cpu(cpu_to_jemode(S_IRUGO|S_IXUGO|S_IWUSR|S_IFDIR));
  1350. #endif
  1351. goto out;
  1352. }
  1353. if (offset == 1) {
  1354. filldir(nbuf, nlen, (const unsigned char *) "..", 2);
  1355. #ifdef CYGPKG_FS_JFFS2_RET_DIRENT_DTYPE
  1356. // Flags here are the same as jffs2_mkdir. Make sure
  1357. // d_type is the same as st_mode of calling stat.
  1358. ent->d_type =
  1359. jemode_to_cpu(cpu_to_jemode(S_IRUGO|S_IXUGO|S_IWUSR|S_IFDIR));
  1360. #endif
  1361. goto out;
  1362. }
  1363. curofs = 1;
  1364. down(&f->sem);
  1365. for (fd = f->dents; fd; fd = fd->next) {
  1366. curofs++;
  1367. /* First loop: curofs = 2; offset = 2 */
  1368. if (curofs < offset) {
  1369. D2(printk
  1370. (KERN_DEBUG
  1371. "Skipping dirent: \"%s\", ino #%u, type %d, because curofs %ld < offset %ld\n",
  1372. fd->name, fd->ino, fd->type, curofs, offset));
  1373. continue;
  1374. }
  1375. if (!fd->ino) {
  1376. D2(printk
  1377. (KERN_DEBUG "Skipping deletion dirent \"%s\"\n",
  1378. fd->name));
  1379. offset++;
  1380. continue;
  1381. }
  1382. D2(printk
  1383. (KERN_DEBUG "Dirent %ld: \"%s\", ino #%u, type %d\n", offset,
  1384. fd->name, fd->ino, fd->type));
  1385. filldir(nbuf, nlen, fd->name, strlen((char *)fd->name));
  1386. #ifdef CYGPKG_FS_JFFS2_RET_DIRENT_DTYPE
  1387. c_ino = jffs2_iget(inode->i_sb, fd->ino);
  1388. if(IS_ERR(c_ino)) {
  1389. D1(printk(KERN_WARNING "get entry inode failed\n"));
  1390. // fileio already set it to zero, so not needed here
  1391. // ent->d_type = 0;
  1392. }
  1393. else {
  1394. ent->d_type = c_ino->i_mode;
  1395. jffs2_iput(c_ino);
  1396. }
  1397. #endif
  1398. goto out_sem;
  1399. }
  1400. /* Reached the end of the directory */
  1401. found = 0;
  1402. out_sem:
  1403. up(&f->sem);
  1404. out:
  1405. fp->f_offset = ++offset;
  1406. if (found) {
  1407. uio->uio_resid -= sizeof (struct dirent);
  1408. }
  1409. return ENOERR;
  1410. }
  1411. // -------------------------------------------------------------------------
  1412. // jffs2_fo_dirlseek()
  1413. // Seek directory to start.
  1414. static int jffs2_fo_dirlseek(struct CYG_FILE_TAG *fp, off_t * pos, int whence)
  1415. {
  1416. // Only allow SEEK_SET to zero
  1417. D2(printf("jffs2_fo_dirlseek\n"));
  1418. if (whence != SEEK_SET || *pos != 0)
  1419. return EINVAL;
  1420. *pos = fp->f_offset = 0;
  1421. return ENOERR;
  1422. }
  1423. //==========================================================================
  1424. //
  1425. // Called by JFFS2
  1426. // ===============
  1427. //
  1428. //
  1429. //==========================================================================
  1430. unsigned char *jffs2_gc_fetch_page(struct jffs2_sb_info *c,
  1431. struct jffs2_inode_info *f,
  1432. unsigned long offset,
  1433. unsigned long *priv)
  1434. {
  1435. /* FIXME: This works only with one file system mounted at a time */
  1436. int ret;
  1437. ret = jffs2_read_inode_range(c, f, gc_buffer,
  1438. offset & ~(PAGE_CACHE_SIZE-1), PAGE_CACHE_SIZE);
  1439. if (ret)
  1440. return ERR_PTR(ret);
  1441. return gc_buffer;
  1442. }
  1443. void jffs2_gc_release_page(struct jffs2_sb_info *c,
  1444. unsigned char *ptr,
  1445. unsigned long *priv)
  1446. {
  1447. /* Do nothing */
  1448. }
  1449. static struct _inode *new_inode(struct super_block *sb)
  1450. {
  1451. struct _inode *inode;
  1452. struct _inode *cached_inode;
  1453. inode = rt_malloc(sizeof (struct _inode));
  1454. if (inode == NULL)
  1455. return 0;
  1456. D2(printf("malloc new_inode %x ####################################\n",
  1457. inode));
  1458. memset(inode, 0, sizeof (struct _inode));
  1459. inode->i_sb = sb;
  1460. inode->i_ino = 1;
  1461. inode->i_count = 1;
  1462. inode->i_nlink = 1; // Let JFFS2 manage the link count
  1463. inode->i_size = 0;
  1464. inode->i_cache_next = NULL; // Newest inode, about to be cached
  1465. // Add to the icache
  1466. for (cached_inode = sb->s_root; cached_inode != NULL;
  1467. cached_inode = cached_inode->i_cache_next) {
  1468. if (cached_inode->i_cache_next == NULL) {
  1469. cached_inode->i_cache_next = inode; // Current last in cache points to newcomer
  1470. inode->i_cache_prev = cached_inode; // Newcomer points back to last
  1471. break;
  1472. }
  1473. }
  1474. return inode;
  1475. }
  1476. static struct _inode *ilookup(struct super_block *sb, cyg_uint32 ino)
  1477. {
  1478. struct _inode *inode = NULL;
  1479. D2(printf("ilookup\n"));
  1480. // Check for this inode in the cache
  1481. for (inode = sb->s_root; inode != NULL; inode = inode->i_cache_next) {
  1482. if (inode->i_ino == ino) {
  1483. inode->i_count++;
  1484. break;
  1485. }
  1486. }
  1487. return inode;
  1488. }
  1489. struct _inode *jffs2_iget(struct super_block *sb, cyg_uint32 ino)
  1490. {
  1491. // Called in super.c jffs2_read_super, dir.c jffs2_lookup,
  1492. // and gc.c jffs2_garbage_collect_pass
  1493. // Must first check for cached inode
  1494. // If this fails let new_inode create one
  1495. struct _inode *inode;
  1496. int err;
  1497. D2(printf("jffs2_iget\n"));
  1498. inode = ilookup(sb, ino);
  1499. if (inode)
  1500. return inode;
  1501. // Not cached, so malloc it
  1502. inode = new_inode(sb);
  1503. if (inode == NULL)
  1504. return ERR_PTR(-ENOMEM);
  1505. inode->i_ino = ino;
  1506. err = jffs2_read_inode(inode);
  1507. if (err) {
  1508. printf("jffs2_read_inode() failed\n");
  1509. inode->i_nlink = 0; // free _this_ bad inode right now
  1510. jffs2_iput(inode);
  1511. inode = NULL;
  1512. return ERR_PTR(err);
  1513. }
  1514. return inode;
  1515. }
  1516. // -------------------------------------------------------------------------
  1517. // Decrement the reference count on an inode. If this makes the ref count
  1518. // zero, then this inode can be freed.
  1519. void jffs2_iput(struct _inode *i)
  1520. {
  1521. // Called in jffs2_find
  1522. // (and jffs2_open and jffs2_ops_mkdir?)
  1523. // super.c jffs2_read_super,
  1524. // and gc.c jffs2_garbage_collect_pass
  1525. recurse:
  1526. if (!i) {
  1527. printf("jffs2_iput() called with NULL inode\n");
  1528. // and let it fault...
  1529. }
  1530. i->i_count--;
  1531. if (i->i_count < 0)
  1532. BUG();
  1533. if (i->i_count)
  1534. return;
  1535. if (!i->i_nlink) {
  1536. struct _inode *parent;
  1537. // Remove from the icache linked list and free immediately
  1538. if (i->i_cache_prev)
  1539. i->i_cache_prev->i_cache_next = i->i_cache_next;
  1540. if (i->i_cache_next)
  1541. i->i_cache_next->i_cache_prev = i->i_cache_prev;
  1542. parent = i->i_parent;
  1543. jffs2_clear_inode(i);
  1544. memset(i, 0x5a, sizeof(*i));
  1545. rt_free(i);
  1546. if (parent && parent != i) {
  1547. i = parent;
  1548. goto recurse;
  1549. }
  1550. } else {
  1551. // Evict some _other_ inode with i_count zero, leaving
  1552. // this latest one in the cache for a while
  1553. icache_evict(i->i_sb->s_root, i);
  1554. }
  1555. }
  1556. // -------------------------------------------------------------------------
  1557. // EOF jffs2.c
  1558. static inline void jffs2_init_inode_info(struct jffs2_inode_info *f)
  1559. {
  1560. memset(f, 0, sizeof(*f));
  1561. init_MUTEX_LOCKED(&f->sem);
  1562. }
  1563. static void jffs2_clear_inode (struct _inode *inode)
  1564. {
  1565. /* We can forget about this inode for now - drop all
  1566. * the nodelists associated with it, etc.
  1567. */
  1568. struct jffs2_sb_info *c = JFFS2_SB_INFO(inode->i_sb);
  1569. struct jffs2_inode_info *f = JFFS2_INODE_INFO(inode);
  1570. D1(printk(KERN_DEBUG "jffs2_clear_inode(): ino #%lu mode %o\n", inode->i_ino, inode->i_mode));
  1571. jffs2_do_clear_inode(c, f);
  1572. }
  1573. /* jffs2_new_inode: allocate a new inode and inocache, add it to the hash,
  1574. fill in the raw_inode while you're at it. */
  1575. struct _inode *jffs2_new_inode (struct _inode *dir_i, int mode, struct jffs2_raw_inode *ri)
  1576. {
  1577. struct _inode *inode;
  1578. struct super_block *sb = dir_i->i_sb;
  1579. struct jffs2_sb_info *c;
  1580. struct jffs2_inode_info *f;
  1581. int ret;
  1582. D1(printk(KERN_DEBUG "jffs2_new_inode(): dir_i %ld, mode 0x%x\n", dir_i->i_ino, mode));
  1583. c = JFFS2_SB_INFO(sb);
  1584. inode = new_inode(sb);
  1585. if (!inode)
  1586. return ERR_PTR(-ENOMEM);
  1587. f = JFFS2_INODE_INFO(inode);
  1588. jffs2_init_inode_info(f);
  1589. memset(ri, 0, sizeof(*ri));
  1590. /* Set OS-specific defaults for new inodes */
  1591. ri->uid = ri->gid = cpu_to_je16(0);
  1592. ri->mode = cpu_to_jemode(mode);
  1593. ret = jffs2_do_new_inode (c, f, mode, ri);
  1594. if (ret) {
  1595. // forceful evict: f->sem is locked already, and the
  1596. // inode is bad.
  1597. if (inode->i_cache_prev)
  1598. inode->i_cache_prev->i_cache_next = inode->i_cache_next;
  1599. if (inode->i_cache_next)
  1600. inode->i_cache_next->i_cache_prev = inode->i_cache_prev;
  1601. up(&(f->sem));
  1602. jffs2_clear_inode(inode);
  1603. memset(inode, 0x6a, sizeof(*inode));
  1604. rt_free(inode);
  1605. return ERR_PTR(ret);
  1606. }
  1607. inode->i_nlink = 1;
  1608. inode->i_ino = je32_to_cpu(ri->ino);
  1609. inode->i_mode = jemode_to_cpu(ri->mode);
  1610. inode->i_gid = je16_to_cpu(ri->gid);
  1611. inode->i_uid = je16_to_cpu(ri->uid);
  1612. inode->i_atime = inode->i_ctime = inode->i_mtime = jffs2_get_timestamp();
  1613. ri->atime = ri->mtime = ri->ctime = cpu_to_je32(inode->i_mtime);
  1614. inode->i_size = 0;
  1615. return inode;
  1616. }
  1617. static int jffs2_read_inode (struct _inode *inode)
  1618. {
  1619. struct jffs2_inode_info *f;
  1620. struct jffs2_sb_info *c;
  1621. struct jffs2_raw_inode latest_node;
  1622. int ret;
  1623. D1(printk(KERN_DEBUG "jffs2_read_inode(): inode->i_ino == %lu\n", inode->i_ino));
  1624. f = JFFS2_INODE_INFO(inode);
  1625. c = JFFS2_SB_INFO(inode->i_sb);
  1626. jffs2_init_inode_info(f);
  1627. ret = jffs2_do_read_inode(c, f, inode->i_ino, &latest_node);
  1628. if (ret) {
  1629. up(&f->sem);
  1630. return ret;
  1631. }
  1632. inode->i_mode = jemode_to_cpu(latest_node.mode);
  1633. inode->i_uid = je16_to_cpu(latest_node.uid);
  1634. inode->i_gid = je16_to_cpu(latest_node.gid);
  1635. inode->i_size = je32_to_cpu(latest_node.isize);
  1636. inode->i_atime = je32_to_cpu(latest_node.atime);
  1637. inode->i_mtime = je32_to_cpu(latest_node.mtime);
  1638. inode->i_ctime = je32_to_cpu(latest_node.ctime);
  1639. inode->i_nlink = f->inocache->nlink;
  1640. up(&f->sem);
  1641. D1(printk(KERN_DEBUG "jffs2_read_inode() returning\n"));
  1642. return 0;
  1643. }
  1644. void jffs2_gc_release_inode(struct jffs2_sb_info *c,
  1645. struct jffs2_inode_info *f)
  1646. {
  1647. jffs2_iput(OFNI_EDONI_2SFFJ(f));
  1648. }
  1649. struct jffs2_inode_info *jffs2_gc_fetch_inode(struct jffs2_sb_info *c,
  1650. int inum, int nlink)
  1651. {
  1652. struct _inode *inode;
  1653. struct jffs2_inode_cache *ic;
  1654. if (!nlink) {
  1655. /* The inode has zero nlink but its nodes weren't yet marked
  1656. obsolete. This has to be because we're still waiting for
  1657. the final (close() and) jffs2_iput() to happen.
  1658. There's a possibility that the final jffs2_iput() could have
  1659. happened while we were contemplating. In order to ensure
  1660. that we don't cause a new read_inode() (which would fail)
  1661. for the inode in question, we use ilookup() in this case
  1662. instead of jffs2_iget().
  1663. The nlink can't _become_ zero at this point because we're
  1664. holding the alloc_sem, and jffs2_do_unlink() would also
  1665. need that while decrementing nlink on any inode.
  1666. */
  1667. inode = ilookup(OFNI_BS_2SFFJ(c), inum);
  1668. if (!inode) {
  1669. D1(printk(KERN_DEBUG "ilookup() failed for ino #%u; inode is probably deleted.\n",
  1670. inum));
  1671. spin_lock(&c->inocache_lock);
  1672. ic = jffs2_get_ino_cache(c, inum);
  1673. if (!ic) {
  1674. D1(printk(KERN_DEBUG "Inode cache for ino #%u is gone.\n", inum));
  1675. spin_unlock(&c->inocache_lock);
  1676. return NULL;
  1677. }
  1678. if (ic->state != INO_STATE_CHECKEDABSENT) {
  1679. /* Wait for progress. Don't just loop */
  1680. D1(printk(KERN_DEBUG "Waiting for ino #%u in state %d\n",
  1681. ic->ino, ic->state));
  1682. sleep_on_spinunlock(&c->inocache_wq, &c->inocache_lock);
  1683. } else {
  1684. spin_unlock(&c->inocache_lock);
  1685. }
  1686. return NULL;
  1687. }
  1688. } else {
  1689. /* Inode has links to it still; they're not going away because
  1690. jffs2_do_unlink() would need the alloc_sem and we have it.
  1691. Just jffs2_iget() it, and if read_inode() is necessary that's OK.
  1692. */
  1693. inode = jffs2_iget(OFNI_BS_2SFFJ(c), inum);
  1694. if (IS_ERR(inode))
  1695. return (void *)inode;
  1696. }
  1697. return JFFS2_INODE_INFO(inode);
  1698. }
  1699. uint32_t jffs2_from_os_mode(uint32_t osmode)
  1700. {
  1701. uint32_t jmode = ((osmode & S_IRUSR)?00400:0) |
  1702. ((osmode & S_IWUSR)?00200:0) |
  1703. ((osmode & S_IXUSR)?00100:0) |
  1704. ((osmode & S_IRGRP)?00040:0) |
  1705. ((osmode & S_IWGRP)?00020:0) |
  1706. ((osmode & S_IXGRP)?00010:0) |
  1707. ((osmode & S_IROTH)?00004:0) |
  1708. ((osmode & S_IWOTH)?00002:0) |
  1709. ((osmode & S_IXOTH)?00001:0);
  1710. switch (osmode & S_IFMT) {
  1711. // case S_IFSOCK:
  1712. // return jmode | 0140000;
  1713. // case S_IFLNK:
  1714. // return jmode | 0120000;
  1715. case S_IFREG:
  1716. return jmode | 0100000;
  1717. case S_IFBLK:
  1718. return jmode | 0060000;
  1719. case S_IFDIR:
  1720. return jmode | 0040000;
  1721. case S_IFCHR:
  1722. return jmode | 0020000;
  1723. case S_IFIFO:
  1724. return jmode | 0010000;
  1725. case S_ISUID:
  1726. return jmode | 0004000;
  1727. case S_ISGID:
  1728. return jmode | 0002000;
  1729. #ifdef S_ISVTX
  1730. case S_ISVTX:
  1731. return jmode | 0001000;
  1732. #endif
  1733. }
  1734. printf("os_to_jffs2_mode() cannot convert 0x%x\n", osmode);
  1735. BUG();
  1736. return 0;
  1737. }
  1738. uint32_t jffs2_to_os_mode (uint32_t jmode)
  1739. {
  1740. uint32_t osmode = ((jmode & 00400)?S_IRUSR:0) |
  1741. ((jmode & 00200)?S_IWUSR:0) |
  1742. ((jmode & 00100)?S_IXUSR:0) |
  1743. ((jmode & 00040)?S_IRGRP:0) |
  1744. ((jmode & 00020)?S_IWGRP:0) |
  1745. ((jmode & 00010)?S_IXGRP:0) |
  1746. ((jmode & 00004)?S_IROTH:0) |
  1747. ((jmode & 00002)?S_IWOTH:0) |
  1748. ((jmode & 00001)?S_IXOTH:0);
  1749. switch(jmode & 00170000) {
  1750. // case 0140000: prife
  1751. // return osmode | S_IFSOCK; prife
  1752. // case 0120000: prife
  1753. // return osmode | S_IFLNK; prife
  1754. case 0100000:
  1755. return osmode | S_IFREG;
  1756. case 0060000:
  1757. return osmode | S_IFBLK;
  1758. case 0040000:
  1759. return osmode | S_IFDIR;
  1760. case 0020000:
  1761. return osmode | S_IFCHR;
  1762. case 0010000:
  1763. return osmode | S_IFIFO;
  1764. case 0004000:
  1765. return osmode | S_ISUID;
  1766. case 0002000:
  1767. return osmode | S_ISGID;
  1768. #ifdef S_ISVTX
  1769. case 0001000:
  1770. return osmode | S_ISVTX;
  1771. #endif
  1772. }
  1773. printf("jffs2_to_os_mode() cannot convert 0x%x\n", osmode);
  1774. BUG();
  1775. return 0;
  1776. }