yaffs_guts.h 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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 Lesser General Public License version 2.1 as
  11. * published by the Free Software Foundation.
  12. *
  13. * Note: Only YAFFS headers are LGPL, YAFFS C code is covered by GPL.
  14. */
  15. #ifndef __YAFFS_GUTS_H__
  16. #define __YAFFS_GUTS_H__
  17. #include "yportenv.h"
  18. #define YAFFS_OK 1
  19. #define YAFFS_FAIL 0
  20. /* Give us a Y=0x59,
  21. * Give us an A=0x41,
  22. * Give us an FF=0xff
  23. * Give us an S=0x53
  24. * And what have we got...
  25. */
  26. #define YAFFS_MAGIC 0x5941ff53
  27. /*
  28. * Tnodes form a tree with the tnodes in "levels"
  29. * Levels greater than 0 hold 8 slots which point to other tnodes.
  30. * Those at level 0 hold 16 slots which point to chunks in NAND.
  31. *
  32. * A maximum level of 8 thust supports files of size up to:
  33. *
  34. * 2^(3*MAX_LEVEL+4)
  35. *
  36. * Thus a max level of 8 supports files with up to 2^^28 chunks which gives
  37. * a maximum file size of around 512Gbytees with 2k chunks.
  38. */
  39. #define YAFFS_NTNODES_LEVEL0 16
  40. #define YAFFS_TNODES_LEVEL0_BITS 4
  41. #define YAFFS_TNODES_LEVEL0_MASK 0xf
  42. #define YAFFS_NTNODES_INTERNAL (YAFFS_NTNODES_LEVEL0 / 2)
  43. #define YAFFS_TNODES_INTERNAL_BITS (YAFFS_TNODES_LEVEL0_BITS - 1)
  44. #define YAFFS_TNODES_INTERNAL_MASK 0x7
  45. #define YAFFS_TNODES_MAX_LEVEL 8
  46. #define YAFFS_TNODES_MAX_BITS (YAFFS_TNODES_LEVEL0_BITS + \
  47. YAFFS_TNODES_INTERNAL_BITS * \
  48. YAFFS_TNODES_MAX_LEVEL)
  49. #define YAFFS_MAX_CHUNK_ID ((1 << YAFFS_TNODES_MAX_BITS) - 1)
  50. #define YAFFS_MAX_FILE_SIZE_32 0x7fffffff
  51. /* Constants for YAFFS1 mode */
  52. #define YAFFS_BYTES_PER_SPARE 16
  53. #define YAFFS_BYTES_PER_CHUNK 512
  54. #define YAFFS_CHUNK_SIZE_SHIFT 9
  55. #define YAFFS_CHUNKS_PER_BLOCK 32
  56. #define YAFFS_BYTES_PER_BLOCK (YAFFS_CHUNKS_PER_BLOCK*YAFFS_BYTES_PER_CHUNK)
  57. #define YAFFS_MIN_YAFFS2_CHUNK_SIZE 1024
  58. #define YAFFS_MIN_YAFFS2_SPARE_SIZE 32
  59. #define YAFFS_ALLOCATION_NOBJECTS 100
  60. #define YAFFS_ALLOCATION_NTNODES 100
  61. #define YAFFS_ALLOCATION_NLINKS 100
  62. #define YAFFS_NOBJECT_BUCKETS 256
  63. #define YAFFS_OBJECT_SPACE 0x40000
  64. #define YAFFS_MAX_OBJECT_ID (YAFFS_OBJECT_SPACE - 1)
  65. /* Binary data version stamps */
  66. #define YAFFS_SUMMARY_VERSION 1
  67. #define YAFFS_CHECKPOINT_VERSION 7
  68. #ifdef CONFIG_YAFFS_UNICODE
  69. #define YAFFS_MAX_NAME_LENGTH 127
  70. #define YAFFS_MAX_ALIAS_LENGTH 79
  71. #else
  72. #define YAFFS_MAX_NAME_LENGTH 255
  73. #define YAFFS_MAX_ALIAS_LENGTH 159
  74. #endif
  75. #define YAFFS_SHORT_NAME_LENGTH 15
  76. /* Some special object ids for pseudo objects */
  77. #define YAFFS_OBJECTID_ROOT 1
  78. #define YAFFS_OBJECTID_LOSTNFOUND 2
  79. #define YAFFS_OBJECTID_UNLINKED 3
  80. #define YAFFS_OBJECTID_DELETED 4
  81. /* Fake object Id for summary data */
  82. #define YAFFS_OBJECTID_SUMMARY 0x10
  83. /* Pseudo object ids for checkpointing */
  84. #define YAFFS_OBJECTID_CHECKPOINT_DATA 0x20
  85. #define YAFFS_SEQUENCE_CHECKPOINT_DATA 0x21
  86. #define YAFFS_MAX_SHORT_OP_CACHES 20
  87. #define YAFFS_N_TEMP_BUFFERS 6
  88. /* We limit the number attempts at sucessfully saving a chunk of data.
  89. * Small-page devices have 32 pages per block; large-page devices have 64.
  90. * Default to something in the order of 5 to 10 blocks worth of chunks.
  91. */
  92. #define YAFFS_WR_ATTEMPTS (5*64)
  93. /* Sequence numbers are used in YAFFS2 to determine block allocation order.
  94. * The range is limited slightly to help distinguish bad numbers from good.
  95. * This also allows us to perhaps in the future use special numbers for
  96. * special purposes.
  97. * EFFFFF00 allows the allocation of 8 blocks/second (~1Mbytes) for 15 years,
  98. * and is a larger number than the lifetime of a 2GB device.
  99. */
  100. #define YAFFS_LOWEST_SEQUENCE_NUMBER 0x00001000
  101. #define YAFFS_HIGHEST_SEQUENCE_NUMBER 0xefffff00
  102. /* Special sequence number for bad block that failed to be marked bad */
  103. #define YAFFS_SEQUENCE_BAD_BLOCK 0xffff0000
  104. /* ChunkCache is used for short read/write operations.*/
  105. struct yaffs_cache {
  106. struct yaffs_obj *object;
  107. int chunk_id;
  108. int last_use;
  109. int dirty;
  110. int n_bytes; /* Only valid if the cache is dirty */
  111. int locked; /* Can't push out or flush while locked. */
  112. u8 *data;
  113. };
  114. /* yaffs1 tags structures in RAM
  115. * NB This uses bitfield. Bitfields should not straddle a u32 boundary
  116. * otherwise the structure size will get blown out.
  117. */
  118. struct yaffs_tags {
  119. u32 chunk_id:20;
  120. u32 serial_number:2;
  121. u32 n_bytes_lsb:10;
  122. u32 obj_id:18;
  123. u32 ecc:12;
  124. u32 n_bytes_msb:2;
  125. };
  126. union yaffs_tags_union {
  127. struct yaffs_tags as_tags;
  128. u8 as_bytes[8];
  129. };
  130. /* Stuff used for extended tags in YAFFS2 */
  131. enum yaffs_ecc_result {
  132. YAFFS_ECC_RESULT_UNKNOWN,
  133. YAFFS_ECC_RESULT_NO_ERROR,
  134. YAFFS_ECC_RESULT_FIXED,
  135. YAFFS_ECC_RESULT_UNFIXED
  136. };
  137. enum yaffs_obj_type {
  138. YAFFS_OBJECT_TYPE_UNKNOWN,
  139. YAFFS_OBJECT_TYPE_FILE,
  140. YAFFS_OBJECT_TYPE_SYMLINK,
  141. YAFFS_OBJECT_TYPE_DIRECTORY,
  142. YAFFS_OBJECT_TYPE_HARDLINK,
  143. YAFFS_OBJECT_TYPE_SPECIAL
  144. };
  145. #define YAFFS_OBJECT_TYPE_MAX YAFFS_OBJECT_TYPE_SPECIAL
  146. struct yaffs_ext_tags {
  147. unsigned chunk_used; /* Status of the chunk: used or unused */
  148. unsigned obj_id; /* If 0 this is not used */
  149. unsigned chunk_id; /* If 0 this is a header, else a data chunk */
  150. unsigned n_bytes; /* Only valid for data chunks */
  151. /* The following stuff only has meaning when we read */
  152. enum yaffs_ecc_result ecc_result;
  153. unsigned block_bad;
  154. /* YAFFS 1 stuff */
  155. unsigned is_deleted; /* The chunk is marked deleted */
  156. unsigned serial_number; /* Yaffs1 2-bit serial number */
  157. /* YAFFS2 stuff */
  158. unsigned seq_number; /* The sequence number of this block */
  159. /* Extra info if this is an object header (YAFFS2 only) */
  160. unsigned extra_available; /* Extra info available if not zero */
  161. unsigned extra_parent_id; /* The parent object */
  162. unsigned extra_is_shrink; /* Is it a shrink header? */
  163. unsigned extra_shadows; /* Does this shadow another object? */
  164. enum yaffs_obj_type extra_obj_type; /* What object type? */
  165. loff_t extra_file_size; /* Length if it is a file */
  166. unsigned extra_equiv_id; /* Equivalent object for a hard link */
  167. };
  168. /* Spare structure for YAFFS1 */
  169. struct yaffs_spare {
  170. u8 tb0;
  171. u8 tb1;
  172. u8 tb2;
  173. u8 tb3;
  174. u8 page_status; /* set to 0 to delete the chunk */
  175. u8 block_status;
  176. u8 tb4;
  177. u8 tb5;
  178. u8 ecc1[3];
  179. u8 tb6;
  180. u8 tb7;
  181. u8 ecc2[3];
  182. };
  183. /*Special structure for passing through to mtd */
  184. struct yaffs_nand_spare {
  185. struct yaffs_spare spare;
  186. int eccres1;
  187. int eccres2;
  188. };
  189. /* Block data in RAM */
  190. enum yaffs_block_state {
  191. YAFFS_BLOCK_STATE_UNKNOWN = 0,
  192. YAFFS_BLOCK_STATE_SCANNING,
  193. /* Being scanned */
  194. YAFFS_BLOCK_STATE_NEEDS_SCAN,
  195. /* The block might have something on it (ie it is allocating or full,
  196. * perhaps empty) but it needs to be scanned to determine its true
  197. * state.
  198. * This state is only valid during scanning.
  199. * NB We tolerate empty because the pre-scanner might be incapable of
  200. * deciding
  201. * However, if this state is returned on a YAFFS2 device,
  202. * then we expect a sequence number
  203. */
  204. YAFFS_BLOCK_STATE_EMPTY,
  205. /* This block is empty */
  206. YAFFS_BLOCK_STATE_ALLOCATING,
  207. /* This block is partially allocated.
  208. * At least one page holds valid data.
  209. * This is the one currently being used for page
  210. * allocation. Should never be more than one of these.
  211. * If a block is only partially allocated at mount it is treated as
  212. * full.
  213. */
  214. YAFFS_BLOCK_STATE_FULL,
  215. /* All the pages in this block have been allocated.
  216. * If a block was only partially allocated when mounted we treat
  217. * it as fully allocated.
  218. */
  219. YAFFS_BLOCK_STATE_DIRTY,
  220. /* The block was full and now all chunks have been deleted.
  221. * Erase me, reuse me.
  222. */
  223. YAFFS_BLOCK_STATE_CHECKPOINT,
  224. /* This block is assigned to holding checkpoint data. */
  225. YAFFS_BLOCK_STATE_COLLECTING,
  226. /* This block is being garbage collected */
  227. YAFFS_BLOCK_STATE_DEAD
  228. /* This block has failed and is not in use */
  229. };
  230. #define YAFFS_NUMBER_OF_BLOCK_STATES (YAFFS_BLOCK_STATE_DEAD + 1)
  231. struct yaffs_block_info {
  232. s32 soft_del_pages:10; /* number of soft deleted pages */
  233. s32 pages_in_use:10; /* number of pages in use */
  234. u32 block_state:4; /* One of the above block states. */
  235. /* NB use unsigned because enum is sometimes
  236. * an int */
  237. u32 needs_retiring:1; /* Data has failed on this block, */
  238. /*need to get valid data off and retire*/
  239. u32 skip_erased_check:1;/* Skip the erased check on this block */
  240. u32 gc_prioritise:1; /* An ECC check or blank check has failed.
  241. Block should be prioritised for GC */
  242. u32 chunk_error_strikes:3; /* How many times we've had ecc etc
  243. failures on this block and tried to reuse it */
  244. u32 has_summary:1; /* The block has a summary */
  245. u32 has_shrink_hdr:1; /* This block has at least one shrink header */
  246. u32 seq_number; /* block sequence number for yaffs2 */
  247. };
  248. /* -------------------------- Object structure -------------------------------*/
  249. /* This is the object structure as stored on NAND */
  250. struct yaffs_obj_hdr {
  251. enum yaffs_obj_type type;
  252. /* Apply to everything */
  253. int parent_obj_id;
  254. u16 sum_no_longer_used; /* checksum of name. No longer used */
  255. YCHAR name[YAFFS_MAX_NAME_LENGTH + 1];
  256. /* The following apply to all object types except for hard links */
  257. u32 yst_mode; /* protection */
  258. u32 yst_uid;
  259. u32 yst_gid;
  260. u32 yst_atime;
  261. u32 yst_mtime;
  262. u32 yst_ctime;
  263. /* File size applies to files only */
  264. u32 file_size_low;
  265. /* Equivalent object id applies to hard links only. */
  266. int equiv_id;
  267. /* Alias is for symlinks only. */
  268. YCHAR alias[YAFFS_MAX_ALIAS_LENGTH + 1];
  269. u32 yst_rdev; /* stuff for block and char devices (major/min) */
  270. u32 win_ctime[2];
  271. u32 win_atime[2];
  272. u32 win_mtime[2];
  273. u32 inband_shadowed_obj_id;
  274. u32 inband_is_shrink;
  275. u32 file_size_high;
  276. u32 reserved[1];
  277. int shadows_obj; /* This object header shadows the
  278. specified object if > 0 */
  279. /* is_shrink applies to object headers written when wemake a hole. */
  280. u32 is_shrink;
  281. };
  282. /*--------------------------- Tnode -------------------------- */
  283. struct yaffs_tnode {
  284. struct yaffs_tnode *internal[YAFFS_NTNODES_INTERNAL];
  285. };
  286. /*------------------------ Object -----------------------------*/
  287. /* An object can be one of:
  288. * - a directory (no data, has children links
  289. * - a regular file (data.... not prunes :->).
  290. * - a symlink [symbolic link] (the alias).
  291. * - a hard link
  292. */
  293. struct yaffs_file_var {
  294. loff_t file_size;
  295. loff_t scanned_size;
  296. loff_t shrink_size;
  297. int top_level;
  298. struct yaffs_tnode *top;
  299. };
  300. struct yaffs_dir_var {
  301. struct list_head children; /* list of child links */
  302. struct list_head dirty; /* Entry for list of dirty directories */
  303. };
  304. struct yaffs_symlink_var {
  305. YCHAR *alias;
  306. };
  307. struct yaffs_hardlink_var {
  308. struct yaffs_obj *equiv_obj;
  309. u32 equiv_id;
  310. };
  311. union yaffs_obj_var {
  312. struct yaffs_file_var file_variant;
  313. struct yaffs_dir_var dir_variant;
  314. struct yaffs_symlink_var symlink_variant;
  315. struct yaffs_hardlink_var hardlink_variant;
  316. };
  317. struct yaffs_obj {
  318. u8 deleted:1; /* This should only apply to unlinked files. */
  319. u8 soft_del:1; /* it has also been soft deleted */
  320. u8 unlinked:1; /* An unlinked file.*/
  321. u8 fake:1; /* A fake object has no presence on NAND. */
  322. u8 rename_allowed:1; /* Some objects cannot be renamed. */
  323. u8 unlink_allowed:1;
  324. u8 dirty:1; /* the object needs to be written to flash */
  325. u8 valid:1; /* When the file system is being loaded up, this
  326. * object might be created before the data
  327. * is available
  328. * ie. file data chunks encountered before
  329. * the header.
  330. */
  331. u8 lazy_loaded:1; /* This object has been lazy loaded and
  332. * is missing some detail */
  333. u8 defered_free:1; /* Object is removed from NAND, but is
  334. * still in the inode cache.
  335. * Free of object is defered.
  336. * until the inode is released.
  337. */
  338. u8 being_created:1; /* This object is still being created
  339. * so skip some verification checks. */
  340. u8 is_shadowed:1; /* This object is shadowed on the way
  341. * to being renamed. */
  342. u8 xattr_known:1; /* We know if this has object has xattribs
  343. * or not. */
  344. u8 has_xattr:1; /* This object has xattribs.
  345. * Only valid if xattr_known. */
  346. u8 serial; /* serial number of chunk in NAND.*/
  347. u16 sum; /* sum of the name to speed searching */
  348. struct yaffs_dev *my_dev; /* The device I'm on */
  349. struct list_head hash_link; /* list of objects in hash bucket */
  350. struct list_head hard_links; /* hard linked object chain*/
  351. /* directory structure stuff */
  352. /* also used for linking up the free list */
  353. struct yaffs_obj *parent;
  354. struct list_head siblings;
  355. /* Where's my object header in NAND? */
  356. int hdr_chunk;
  357. int n_data_chunks; /* Number of data chunks for this file. */
  358. u32 obj_id; /* the object id value */
  359. u32 yst_mode;
  360. YCHAR short_name[YAFFS_SHORT_NAME_LENGTH + 1];
  361. #ifdef CONFIG_YAFFS_WINCE
  362. u32 win_ctime[2];
  363. u32 win_mtime[2];
  364. u32 win_atime[2];
  365. #else
  366. u32 yst_uid;
  367. u32 yst_gid;
  368. u32 yst_atime;
  369. u32 yst_mtime;
  370. u32 yst_ctime;
  371. #endif
  372. u32 yst_rdev;
  373. void *my_inode;
  374. enum yaffs_obj_type variant_type;
  375. union yaffs_obj_var variant;
  376. };
  377. struct yaffs_obj_bucket {
  378. struct list_head list;
  379. int count;
  380. };
  381. /* yaffs_checkpt_obj holds the definition of an object as dumped
  382. * by checkpointing.
  383. */
  384. struct yaffs_checkpt_obj {
  385. int struct_type;
  386. u32 obj_id;
  387. u32 parent_id;
  388. int hdr_chunk;
  389. enum yaffs_obj_type variant_type:3;
  390. u8 deleted:1;
  391. u8 soft_del:1;
  392. u8 unlinked:1;
  393. u8 fake:1;
  394. u8 rename_allowed:1;
  395. u8 unlink_allowed:1;
  396. u8 serial;
  397. int n_data_chunks;
  398. loff_t size_or_equiv_obj;
  399. };
  400. /*--------------------- Temporary buffers ----------------
  401. *
  402. * These are chunk-sized working buffers. Each device has a few.
  403. */
  404. struct yaffs_buffer {
  405. u8 *buffer;
  406. int in_use;
  407. };
  408. /*----------------- Device ---------------------------------*/
  409. struct yaffs_param {
  410. const YCHAR *name;
  411. /*
  412. * Entry parameters set up way early. Yaffs sets up the rest.
  413. * The structure should be zeroed out before use so that unused
  414. * and default values are zero.
  415. */
  416. int inband_tags; /* Use unband tags */
  417. u32 total_bytes_per_chunk; /* Should be >= 512, does not need to
  418. be a power of 2 */
  419. int chunks_per_block; /* does not need to be a power of 2 */
  420. int spare_bytes_per_chunk; /* spare area size */
  421. int start_block; /* Start block we're allowed to use */
  422. int end_block; /* End block we're allowed to use */
  423. int n_reserved_blocks; /* Tuneable so that we can reduce
  424. * reserved blocks on NOR and RAM. */
  425. int n_caches; /* If <= 0, then short op caching is disabled,
  426. * else the number of short op caches.
  427. */
  428. int cache_bypass_aligned; /* If non-zero then bypass the cache for
  429. * aligned writes.
  430. */
  431. int use_nand_ecc; /* Flag to decide whether or not to use
  432. * NAND driver ECC on data (yaffs1) */
  433. int tags_9bytes; /* Use 9 byte tags */
  434. int no_tags_ecc; /* Flag to decide whether or not to do ECC
  435. * on packed tags (yaffs2) */
  436. int is_yaffs2; /* Use yaffs2 mode on this device */
  437. int empty_lost_n_found; /* Auto-empty lost+found directory on mount */
  438. int refresh_period; /* How often to check for a block refresh */
  439. /* Checkpoint control. Can be set before or after initialisation */
  440. u8 skip_checkpt_rd;
  441. u8 skip_checkpt_wr;
  442. int enable_xattr; /* Enable xattribs */
  443. int max_objects; /*
  444. * Set to limit the number of objects created.
  445. * 0 = no limit.
  446. */
  447. /* The remove_obj_fn function must be supplied by OS flavours that
  448. * need it.
  449. * yaffs direct uses it to implement the faster readdir.
  450. * Linux uses it to protect the directory during unlocking.
  451. */
  452. void (*remove_obj_fn) (struct yaffs_obj *obj);
  453. /* Callback to mark the superblock dirty */
  454. void (*sb_dirty_fn) (struct yaffs_dev *dev);
  455. /* Callback to control garbage collection. */
  456. unsigned (*gc_control_fn) (struct yaffs_dev *dev);
  457. /* Debug control flags. Don't use unless you know what you're doing */
  458. int use_header_file_size; /* Flag to determine if we should use
  459. * file sizes from the header */
  460. int disable_lazy_load; /* Disable lazy loading on this device */
  461. int wide_tnodes_disabled; /* Set to disable wide tnodes */
  462. int disable_soft_del; /* yaffs 1 only: Set to disable the use of
  463. * softdeletion. */
  464. int defered_dir_update; /* Set to defer directory updates */
  465. #ifdef CONFIG_YAFFS_AUTO_UNICODE
  466. int auto_unicode;
  467. #endif
  468. int always_check_erased; /* Force chunk erased check always on */
  469. int disable_summary;
  470. int disable_bad_block_marking;
  471. };
  472. struct yaffs_driver {
  473. int (*drv_write_chunk_fn) (struct yaffs_dev *dev, int nand_chunk,
  474. const u8 *data, int data_len,
  475. const u8 *oob, int oob_len);
  476. int (*drv_read_chunk_fn) (struct yaffs_dev *dev, int nand_chunk,
  477. u8 *data, int data_len,
  478. u8 *oob, int oob_len,
  479. enum yaffs_ecc_result *ecc_result);
  480. int (*drv_erase_fn) (struct yaffs_dev *dev, int block_no);
  481. int (*drv_mark_bad_fn) (struct yaffs_dev *dev, int block_no);
  482. int (*drv_check_bad_fn) (struct yaffs_dev *dev, int block_no);
  483. int (*drv_initialise_fn) (struct yaffs_dev *dev);
  484. int (*drv_deinitialise_fn) (struct yaffs_dev *dev);
  485. };
  486. struct yaffs_tags_handler {
  487. int (*write_chunk_tags_fn) (struct yaffs_dev *dev,
  488. int nand_chunk, const u8 *data,
  489. const struct yaffs_ext_tags *tags);
  490. int (*read_chunk_tags_fn) (struct yaffs_dev *dev,
  491. int nand_chunk, u8 *data,
  492. struct yaffs_ext_tags *tags);
  493. int (*query_block_fn) (struct yaffs_dev *dev, int block_no,
  494. enum yaffs_block_state *state,
  495. u32 *seq_number);
  496. int (*mark_bad_fn) (struct yaffs_dev *dev, int block_no);
  497. };
  498. struct yaffs_dev {
  499. struct yaffs_param param;
  500. struct yaffs_driver drv;
  501. struct yaffs_tags_handler tagger;
  502. /* Context storage. Holds extra OS specific data for this device */
  503. void *os_context;
  504. void *driver_context;
  505. struct list_head dev_list;
  506. int ll_init;
  507. /* Runtime parameters. Set up by YAFFS. */
  508. int data_bytes_per_chunk;
  509. /* Non-wide tnode stuff */
  510. u16 chunk_grp_bits; /* Number of bits that need to be resolved if
  511. * the tnodes are not wide enough.
  512. */
  513. u16 chunk_grp_size; /* == 2^^chunk_grp_bits */
  514. /* Stuff to support wide tnodes */
  515. u32 tnode_width;
  516. u32 tnode_mask;
  517. u32 tnode_size;
  518. /* Stuff for figuring out file offset to chunk conversions */
  519. u32 chunk_shift; /* Shift value */
  520. u32 chunk_div; /* Divisor after shifting: 1 for 2^n sizes */
  521. u32 chunk_mask; /* Mask to use for power-of-2 case */
  522. int is_mounted;
  523. int read_only;
  524. int is_checkpointed;
  525. /* Stuff to support block offsetting to support start block zero */
  526. int internal_start_block;
  527. int internal_end_block;
  528. int block_offset;
  529. int chunk_offset;
  530. /* Runtime checkpointing stuff */
  531. int checkpt_page_seq; /* running sequence number of checkpt pages */
  532. int checkpt_byte_count;
  533. int checkpt_byte_offs;
  534. u8 *checkpt_buffer;
  535. int checkpt_open_write;
  536. int blocks_in_checkpt;
  537. int checkpt_cur_chunk;
  538. int checkpt_cur_block;
  539. int checkpt_next_block;
  540. int *checkpt_block_list;
  541. int checkpt_max_blocks;
  542. u32 checkpt_sum;
  543. u32 checkpt_xor;
  544. int checkpoint_blocks_required; /* Number of blocks needed to store
  545. * current checkpoint set */
  546. /* Block Info */
  547. struct yaffs_block_info *block_info;
  548. u8 *chunk_bits; /* bitmap of chunks in use */
  549. u8 block_info_alt:1; /* allocated using alternative alloc */
  550. u8 chunk_bits_alt:1; /* allocated using alternative alloc */
  551. int chunk_bit_stride; /* Number of bytes of chunk_bits per block.
  552. * Must be consistent with chunks_per_block.
  553. */
  554. int n_erased_blocks;
  555. int alloc_block; /* Current block being allocated off */
  556. u32 alloc_page;
  557. int alloc_block_finder; /* Used to search for next allocation block */
  558. /* Object and Tnode memory management */
  559. void *allocator;
  560. int n_obj;
  561. int n_tnodes;
  562. int n_hardlinks;
  563. struct yaffs_obj_bucket obj_bucket[YAFFS_NOBJECT_BUCKETS];
  564. u32 bucket_finder;
  565. int n_free_chunks;
  566. /* Garbage collection control */
  567. u32 *gc_cleanup_list; /* objects to delete at the end of a GC. */
  568. u32 n_clean_ups;
  569. unsigned has_pending_prioritised_gc; /* We think this device might
  570. have pending prioritised gcs */
  571. unsigned gc_disable;
  572. unsigned gc_block_finder;
  573. unsigned gc_dirtiest;
  574. unsigned gc_pages_in_use;
  575. unsigned gc_not_done;
  576. unsigned gc_block;
  577. unsigned gc_chunk;
  578. unsigned gc_skip;
  579. struct yaffs_summary_tags *gc_sum_tags;
  580. /* Special directories */
  581. struct yaffs_obj *root_dir;
  582. struct yaffs_obj *lost_n_found;
  583. int buffered_block; /* Which block is buffered here? */
  584. int doing_buffered_block_rewrite;
  585. struct yaffs_cache *cache;
  586. int cache_last_use;
  587. /* Stuff for background deletion and unlinked files. */
  588. struct yaffs_obj *unlinked_dir; /* Directory where unlinked and deleted
  589. files live. */
  590. struct yaffs_obj *del_dir; /* Directory where deleted objects are
  591. sent to disappear. */
  592. struct yaffs_obj *unlinked_deletion; /* Current file being
  593. background deleted. */
  594. int n_deleted_files; /* Count of files awaiting deletion; */
  595. int n_unlinked_files; /* Count of unlinked files. */
  596. int n_bg_deletions; /* Count of background deletions. */
  597. /* Temporary buffer management */
  598. struct yaffs_buffer temp_buffer[YAFFS_N_TEMP_BUFFERS];
  599. int max_temp;
  600. int temp_in_use;
  601. int unmanaged_buffer_allocs;
  602. int unmanaged_buffer_deallocs;
  603. /* yaffs2 runtime stuff */
  604. unsigned seq_number; /* Sequence number of currently
  605. allocating block */
  606. unsigned oldest_dirty_seq;
  607. unsigned oldest_dirty_block;
  608. /* Block refreshing */
  609. int refresh_skip; /* A skip down counter.
  610. * Refresh happens when this gets to zero. */
  611. /* Dirty directory handling */
  612. struct list_head dirty_dirs; /* List of dirty directories */
  613. /* Summary */
  614. int chunks_per_summary;
  615. struct yaffs_summary_tags *sum_tags;
  616. /* Statistics */
  617. u32 n_page_writes;
  618. u32 n_page_reads;
  619. u32 n_erasures;
  620. u32 n_bad_queries;
  621. u32 n_bad_markings;
  622. u32 n_erase_failures;
  623. u32 n_gc_copies;
  624. u32 all_gcs;
  625. u32 passive_gc_count;
  626. u32 oldest_dirty_gc_count;
  627. u32 n_gc_blocks;
  628. u32 bg_gcs;
  629. u32 n_retried_writes;
  630. u32 n_retired_blocks;
  631. u32 n_ecc_fixed;
  632. u32 n_ecc_unfixed;
  633. u32 n_tags_ecc_fixed;
  634. u32 n_tags_ecc_unfixed;
  635. u32 n_deletions;
  636. u32 n_unmarked_deletions;
  637. u32 refresh_count;
  638. u32 cache_hits;
  639. u32 tags_used;
  640. u32 summary_used;
  641. };
  642. /* The CheckpointDevice structure holds the device information that changes
  643. *at runtime and must be preserved over unmount/mount cycles.
  644. */
  645. struct yaffs_checkpt_dev {
  646. int struct_type;
  647. int n_erased_blocks;
  648. int alloc_block; /* Current block being allocated off */
  649. u32 alloc_page;
  650. int n_free_chunks;
  651. int n_deleted_files; /* Count of files awaiting deletion; */
  652. int n_unlinked_files; /* Count of unlinked files. */
  653. int n_bg_deletions; /* Count of background deletions. */
  654. /* yaffs2 runtime stuff */
  655. unsigned seq_number; /* Sequence number of currently
  656. * allocating block */
  657. };
  658. struct yaffs_checkpt_validity {
  659. int struct_type;
  660. u32 magic;
  661. u32 version;
  662. u32 head;
  663. };
  664. struct yaffs_shadow_fixer {
  665. int obj_id;
  666. int shadowed_id;
  667. struct yaffs_shadow_fixer *next;
  668. };
  669. /* Structure for doing xattr modifications */
  670. struct yaffs_xattr_mod {
  671. int set; /* If 0 then this is a deletion */
  672. const YCHAR *name;
  673. const void *data;
  674. int size;
  675. int flags;
  676. int result;
  677. };
  678. /*----------------------- YAFFS Functions -----------------------*/
  679. int yaffs_guts_initialise(struct yaffs_dev *dev);
  680. void yaffs_deinitialise(struct yaffs_dev *dev);
  681. int yaffs_get_n_free_chunks(struct yaffs_dev *dev);
  682. int yaffs_rename_obj(struct yaffs_obj *old_dir, const YCHAR * old_name,
  683. struct yaffs_obj *new_dir, const YCHAR * new_name);
  684. int yaffs_unlinker(struct yaffs_obj *dir, const YCHAR * name);
  685. int yaffs_del_obj(struct yaffs_obj *obj);
  686. struct yaffs_obj *yaffs_retype_obj(struct yaffs_obj *obj,
  687. enum yaffs_obj_type type);
  688. int yaffs_get_obj_name(struct yaffs_obj *obj, YCHAR * name, int buffer_size);
  689. loff_t yaffs_get_obj_length(struct yaffs_obj *obj);
  690. int yaffs_get_obj_inode(struct yaffs_obj *obj);
  691. unsigned yaffs_get_obj_type(struct yaffs_obj *obj);
  692. int yaffs_get_obj_link_count(struct yaffs_obj *obj);
  693. /* File operations */
  694. int yaffs_file_rd(struct yaffs_obj *obj, u8 * buffer, loff_t offset,
  695. int n_bytes);
  696. int yaffs_wr_file(struct yaffs_obj *obj, const u8 * buffer, loff_t offset,
  697. int n_bytes, int write_trhrough);
  698. int yaffs_resize_file(struct yaffs_obj *obj, loff_t new_size);
  699. struct yaffs_obj *yaffs_create_file(struct yaffs_obj *parent,
  700. const YCHAR *name, u32 mode, u32 uid,
  701. u32 gid);
  702. int yaffs_flush_file(struct yaffs_obj *in,
  703. int update_time,
  704. int data_sync,
  705. int discard_cache);
  706. /* Flushing and checkpointing */
  707. void yaffs_flush_whole_cache(struct yaffs_dev *dev, int discard);
  708. int yaffs_checkpoint_save(struct yaffs_dev *dev);
  709. int yaffs_checkpoint_restore(struct yaffs_dev *dev);
  710. /* Directory operations */
  711. struct yaffs_obj *yaffs_create_dir(struct yaffs_obj *parent, const YCHAR *name,
  712. u32 mode, u32 uid, u32 gid);
  713. struct yaffs_obj *yaffs_find_by_name(struct yaffs_obj *the_dir,
  714. const YCHAR *name);
  715. struct yaffs_obj *yaffs_find_by_number(struct yaffs_dev *dev, u32 number);
  716. /* Link operations */
  717. struct yaffs_obj *yaffs_link_obj(struct yaffs_obj *parent, const YCHAR *name,
  718. struct yaffs_obj *equiv_obj);
  719. struct yaffs_obj *yaffs_get_equivalent_obj(struct yaffs_obj *obj);
  720. /* Symlink operations */
  721. struct yaffs_obj *yaffs_create_symlink(struct yaffs_obj *parent,
  722. const YCHAR *name, u32 mode, u32 uid,
  723. u32 gid, const YCHAR *alias);
  724. YCHAR *yaffs_get_symlink_alias(struct yaffs_obj *obj);
  725. /* Special inodes (fifos, sockets and devices) */
  726. struct yaffs_obj *yaffs_create_special(struct yaffs_obj *parent,
  727. const YCHAR *name, u32 mode, u32 uid,
  728. u32 gid, u32 rdev);
  729. int yaffs_set_xattrib(struct yaffs_obj *obj, const YCHAR *name,
  730. const void *value, int size, int flags);
  731. int yaffs_get_xattrib(struct yaffs_obj *obj, const YCHAR *name, void *value,
  732. int size);
  733. int yaffs_list_xattrib(struct yaffs_obj *obj, char *buffer, int size);
  734. int yaffs_remove_xattrib(struct yaffs_obj *obj, const YCHAR *name);
  735. /* Special directories */
  736. struct yaffs_obj *yaffs_root(struct yaffs_dev *dev);
  737. struct yaffs_obj *yaffs_lost_n_found(struct yaffs_dev *dev);
  738. void yaffs_handle_defered_free(struct yaffs_obj *obj);
  739. void yaffs_update_dirty_dirs(struct yaffs_dev *dev);
  740. int yaffs_bg_gc(struct yaffs_dev *dev, unsigned urgency);
  741. /* Debug dump */
  742. int yaffs_dump_obj(struct yaffs_obj *obj);
  743. void yaffs_guts_test(struct yaffs_dev *dev);
  744. int yaffs_guts_ll_init(struct yaffs_dev *dev);
  745. /* A few useful functions to be used within the core files*/
  746. void yaffs_chunk_del(struct yaffs_dev *dev, int chunk_id, int mark_flash,
  747. int lyn);
  748. int yaffs_check_ff(u8 *buffer, int n_bytes);
  749. void yaffs_handle_chunk_error(struct yaffs_dev *dev,
  750. struct yaffs_block_info *bi);
  751. u8 *yaffs_get_temp_buffer(struct yaffs_dev *dev);
  752. void yaffs_release_temp_buffer(struct yaffs_dev *dev, u8 *buffer);
  753. struct yaffs_obj *yaffs_find_or_create_by_number(struct yaffs_dev *dev,
  754. int number,
  755. enum yaffs_obj_type type);
  756. int yaffs_put_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  757. int nand_chunk, int in_scan);
  758. void yaffs_set_obj_name(struct yaffs_obj *obj, const YCHAR *name);
  759. void yaffs_set_obj_name_from_oh(struct yaffs_obj *obj,
  760. const struct yaffs_obj_hdr *oh);
  761. void yaffs_add_obj_to_dir(struct yaffs_obj *directory, struct yaffs_obj *obj);
  762. YCHAR *yaffs_clone_str(const YCHAR *str);
  763. void yaffs_link_fixup(struct yaffs_dev *dev, struct list_head *hard_list);
  764. void yaffs_block_became_dirty(struct yaffs_dev *dev, int block_no);
  765. int yaffs_update_oh(struct yaffs_obj *in, const YCHAR *name,
  766. int force, int is_shrink, int shadows,
  767. struct yaffs_xattr_mod *xop);
  768. void yaffs_handle_shadowed_obj(struct yaffs_dev *dev, int obj_id,
  769. int backward_scanning);
  770. int yaffs_check_alloc_available(struct yaffs_dev *dev, int n_chunks);
  771. struct yaffs_tnode *yaffs_get_tnode(struct yaffs_dev *dev);
  772. struct yaffs_tnode *yaffs_add_find_tnode_0(struct yaffs_dev *dev,
  773. struct yaffs_file_var *file_struct,
  774. u32 chunk_id,
  775. struct yaffs_tnode *passed_tn);
  776. int yaffs_do_file_wr(struct yaffs_obj *in, const u8 *buffer, loff_t offset,
  777. int n_bytes, int write_trhrough);
  778. void yaffs_resize_file_down(struct yaffs_obj *obj, loff_t new_size);
  779. void yaffs_skip_rest_of_block(struct yaffs_dev *dev);
  780. int yaffs_count_free_chunks(struct yaffs_dev *dev);
  781. struct yaffs_tnode *yaffs_find_tnode_0(struct yaffs_dev *dev,
  782. struct yaffs_file_var *file_struct,
  783. u32 chunk_id);
  784. u32 yaffs_get_group_base(struct yaffs_dev *dev, struct yaffs_tnode *tn,
  785. unsigned pos);
  786. int yaffs_is_non_empty_dir(struct yaffs_obj *obj);
  787. int yaffs_guts_format_dev(struct yaffs_dev *dev);
  788. void yaffs_addr_to_chunk(struct yaffs_dev *dev, loff_t addr,
  789. int *chunk_out, u32 *offset_out);
  790. /*
  791. * Marshalling functions to get loff_t file sizes into aand out of
  792. * object headers.
  793. */
  794. void yaffs_oh_size_load(struct yaffs_obj_hdr *oh, loff_t fsize);
  795. loff_t yaffs_oh_to_size(struct yaffs_obj_hdr *oh);
  796. loff_t yaffs_max_file_size(struct yaffs_dev *dev);
  797. /*
  798. * Debug function to count number of blocks in each state
  799. * NB Needs to be called with correct number of integers
  800. */
  801. void yaffs_count_blocks_by_state(struct yaffs_dev *dev, int bs[10]);
  802. int yaffs_find_chunk_in_file(struct yaffs_obj *in, int inode_chunk,
  803. struct yaffs_ext_tags *tags);
  804. #endif