yaffs_guts.h 28 KB

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