f2fs.h 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214
  1. /*
  2. * fs/f2fs/f2fs.h
  3. *
  4. * Copyright (c) 2012 Samsung Electronics Co., Ltd.
  5. * http://www.samsung.com/
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. */
  11. #ifndef _LINUX_F2FS_H
  12. #define _LINUX_F2FS_H
  13. #include <linux/types.h>
  14. #include <linux/page-flags.h>
  15. #include <linux/buffer_head.h>
  16. #include <linux/slab.h>
  17. #include <linux/crc32.h>
  18. #include <linux/magic.h>
  19. #include <linux/kobject.h>
  20. #include <linux/sched.h>
  21. #include <linux/vmalloc.h>
  22. #include <linux/bio.h>
  23. #ifdef CONFIG_F2FS_CHECK_FS
  24. #define f2fs_bug_on(sbi, condition) BUG_ON(condition)
  25. #define f2fs_down_write(x, y) down_write_nest_lock(x, y)
  26. #else
  27. #define f2fs_bug_on(sbi, condition) \
  28. do { \
  29. if (unlikely(condition)) { \
  30. WARN_ON(1); \
  31. set_sbi_flag(sbi, SBI_NEED_FSCK); \
  32. } \
  33. } while (0)
  34. #define f2fs_down_write(x, y) down_write(x)
  35. #endif
  36. /*
  37. * For mount options
  38. */
  39. #define F2FS_MOUNT_BG_GC 0x00000001
  40. #define F2FS_MOUNT_DISABLE_ROLL_FORWARD 0x00000002
  41. #define F2FS_MOUNT_DISCARD 0x00000004
  42. #define F2FS_MOUNT_NOHEAP 0x00000008
  43. #define F2FS_MOUNT_XATTR_USER 0x00000010
  44. #define F2FS_MOUNT_POSIX_ACL 0x00000020
  45. #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY 0x00000040
  46. #define F2FS_MOUNT_INLINE_XATTR 0x00000080
  47. #define F2FS_MOUNT_INLINE_DATA 0x00000100
  48. #define F2FS_MOUNT_INLINE_DENTRY 0x00000200
  49. #define F2FS_MOUNT_FLUSH_MERGE 0x00000400
  50. #define F2FS_MOUNT_NOBARRIER 0x00000800
  51. #define F2FS_MOUNT_FASTBOOT 0x00001000
  52. #define F2FS_MOUNT_EXTENT_CACHE 0x00002000
  53. #define F2FS_MOUNT_FORCE_FG_GC 0x00004000
  54. #define clear_opt(sbi, option) (sbi->mount_opt.opt &= ~F2FS_MOUNT_##option)
  55. #define set_opt(sbi, option) (sbi->mount_opt.opt |= F2FS_MOUNT_##option)
  56. #define test_opt(sbi, option) (sbi->mount_opt.opt & F2FS_MOUNT_##option)
  57. #define ver_after(a, b) (typecheck(unsigned long long, a) && \
  58. typecheck(unsigned long long, b) && \
  59. ((long long)((a) - (b)) > 0))
  60. typedef u32 block_t; /*
  61. * should not change u32, since it is the on-disk block
  62. * address format, __le32.
  63. */
  64. typedef u32 nid_t;
  65. struct f2fs_mount_info {
  66. unsigned int opt;
  67. };
  68. #define F2FS_FEATURE_ENCRYPT 0x0001
  69. #define F2FS_HAS_FEATURE(sb, mask) \
  70. ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
  71. #define F2FS_SET_FEATURE(sb, mask) \
  72. F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask)
  73. #define F2FS_CLEAR_FEATURE(sb, mask) \
  74. F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask)
  75. #define CRCPOLY_LE 0xedb88320
  76. static inline __u32 f2fs_crc32(void *buf, size_t len)
  77. {
  78. unsigned char *p = (unsigned char *)buf;
  79. __u32 crc = F2FS_SUPER_MAGIC;
  80. int i;
  81. while (len--) {
  82. crc ^= *p++;
  83. for (i = 0; i < 8; i++)
  84. crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
  85. }
  86. return crc;
  87. }
  88. static inline bool f2fs_crc_valid(__u32 blk_crc, void *buf, size_t buf_size)
  89. {
  90. return f2fs_crc32(buf, buf_size) == blk_crc;
  91. }
  92. /*
  93. * For checkpoint manager
  94. */
  95. enum {
  96. NAT_BITMAP,
  97. SIT_BITMAP
  98. };
  99. enum {
  100. CP_UMOUNT,
  101. CP_FASTBOOT,
  102. CP_SYNC,
  103. CP_RECOVERY,
  104. CP_DISCARD,
  105. };
  106. #define DEF_BATCHED_TRIM_SECTIONS 32
  107. #define BATCHED_TRIM_SEGMENTS(sbi) \
  108. (SM_I(sbi)->trim_sections * (sbi)->segs_per_sec)
  109. #define BATCHED_TRIM_BLOCKS(sbi) \
  110. (BATCHED_TRIM_SEGMENTS(sbi) << (sbi)->log_blocks_per_seg)
  111. #define DEF_CP_INTERVAL 60 /* 60 secs */
  112. struct cp_control {
  113. int reason;
  114. __u64 trim_start;
  115. __u64 trim_end;
  116. __u64 trim_minlen;
  117. __u64 trimmed;
  118. };
  119. /*
  120. * For CP/NAT/SIT/SSA readahead
  121. */
  122. enum {
  123. META_CP,
  124. META_NAT,
  125. META_SIT,
  126. META_SSA,
  127. META_POR,
  128. };
  129. /* for the list of ino */
  130. enum {
  131. ORPHAN_INO, /* for orphan ino list */
  132. APPEND_INO, /* for append ino list */
  133. UPDATE_INO, /* for update ino list */
  134. MAX_INO_ENTRY, /* max. list */
  135. };
  136. struct ino_entry {
  137. struct list_head list; /* list head */
  138. nid_t ino; /* inode number */
  139. };
  140. /*
  141. * for the list of directory inodes or gc inodes.
  142. * NOTE: there are two slab users for this structure, if we add/modify/delete
  143. * fields in structure for one of slab users, it may affect fields or size of
  144. * other one, in this condition, it's better to split both of slab and related
  145. * data structure.
  146. */
  147. struct inode_entry {
  148. struct list_head list; /* list head */
  149. struct inode *inode; /* vfs inode pointer */
  150. };
  151. /* for the list of blockaddresses to be discarded */
  152. struct discard_entry {
  153. struct list_head list; /* list head */
  154. block_t blkaddr; /* block address to be discarded */
  155. int len; /* # of consecutive blocks of the discard */
  156. };
  157. /* for the list of fsync inodes, used only during recovery */
  158. struct fsync_inode_entry {
  159. struct list_head list; /* list head */
  160. struct inode *inode; /* vfs inode pointer */
  161. block_t blkaddr; /* block address locating the last fsync */
  162. block_t last_dentry; /* block address locating the last dentry */
  163. block_t last_inode; /* block address locating the last inode */
  164. };
  165. #define nats_in_cursum(sum) (le16_to_cpu(sum->n_nats))
  166. #define sits_in_cursum(sum) (le16_to_cpu(sum->n_sits))
  167. #define nat_in_journal(sum, i) (sum->nat_j.entries[i].ne)
  168. #define nid_in_journal(sum, i) (sum->nat_j.entries[i].nid)
  169. #define sit_in_journal(sum, i) (sum->sit_j.entries[i].se)
  170. #define segno_in_journal(sum, i) (sum->sit_j.entries[i].segno)
  171. #define MAX_NAT_JENTRIES(sum) (NAT_JOURNAL_ENTRIES - nats_in_cursum(sum))
  172. #define MAX_SIT_JENTRIES(sum) (SIT_JOURNAL_ENTRIES - sits_in_cursum(sum))
  173. static inline int update_nats_in_cursum(struct f2fs_summary_block *rs, int i)
  174. {
  175. int before = nats_in_cursum(rs);
  176. rs->n_nats = cpu_to_le16(before + i);
  177. return before;
  178. }
  179. static inline int update_sits_in_cursum(struct f2fs_summary_block *rs, int i)
  180. {
  181. int before = sits_in_cursum(rs);
  182. rs->n_sits = cpu_to_le16(before + i);
  183. return before;
  184. }
  185. static inline bool __has_cursum_space(struct f2fs_summary_block *sum, int size,
  186. int type)
  187. {
  188. if (type == NAT_JOURNAL)
  189. return size <= MAX_NAT_JENTRIES(sum);
  190. return size <= MAX_SIT_JENTRIES(sum);
  191. }
  192. /*
  193. * ioctl commands
  194. */
  195. #define F2FS_IOC_GETFLAGS FS_IOC_GETFLAGS
  196. #define F2FS_IOC_SETFLAGS FS_IOC_SETFLAGS
  197. #define F2FS_IOC_GETVERSION FS_IOC_GETVERSION
  198. #define F2FS_IOCTL_MAGIC 0xf5
  199. #define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
  200. #define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
  201. #define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
  202. #define F2FS_IOC_RELEASE_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 4)
  203. #define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5)
  204. #define F2FS_IOC_GARBAGE_COLLECT _IO(F2FS_IOCTL_MAGIC, 6)
  205. #define F2FS_IOC_WRITE_CHECKPOINT _IO(F2FS_IOCTL_MAGIC, 7)
  206. #define F2FS_IOC_SET_ENCRYPTION_POLICY \
  207. _IOR('f', 19, struct f2fs_encryption_policy)
  208. #define F2FS_IOC_GET_ENCRYPTION_PWSALT \
  209. _IOW('f', 20, __u8[16])
  210. #define F2FS_IOC_GET_ENCRYPTION_POLICY \
  211. _IOW('f', 21, struct f2fs_encryption_policy)
  212. /*
  213. * should be same as XFS_IOC_GOINGDOWN.
  214. * Flags for going down operation used by FS_IOC_GOINGDOWN
  215. */
  216. #define F2FS_IOC_SHUTDOWN _IOR('X', 125, __u32) /* Shutdown */
  217. #define F2FS_GOING_DOWN_FULLSYNC 0x0 /* going down with full sync */
  218. #define F2FS_GOING_DOWN_METASYNC 0x1 /* going down with metadata */
  219. #define F2FS_GOING_DOWN_NOSYNC 0x2 /* going down */
  220. #define F2FS_GOING_DOWN_METAFLUSH 0x3 /* going down with meta flush */
  221. #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
  222. /*
  223. * ioctl commands in 32 bit emulation
  224. */
  225. #define F2FS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
  226. #define F2FS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
  227. #endif
  228. /*
  229. * For INODE and NODE manager
  230. */
  231. /* for directory operations */
  232. struct f2fs_str {
  233. unsigned char *name;
  234. u32 len;
  235. };
  236. struct f2fs_filename {
  237. const struct qstr *usr_fname;
  238. struct f2fs_str disk_name;
  239. f2fs_hash_t hash;
  240. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  241. struct f2fs_str crypto_buf;
  242. #endif
  243. };
  244. #define FSTR_INIT(n, l) { .name = n, .len = l }
  245. #define FSTR_TO_QSTR(f) QSTR_INIT((f)->name, (f)->len)
  246. #define fname_name(p) ((p)->disk_name.name)
  247. #define fname_len(p) ((p)->disk_name.len)
  248. struct f2fs_dentry_ptr {
  249. struct inode *inode;
  250. const void *bitmap;
  251. struct f2fs_dir_entry *dentry;
  252. __u8 (*filename)[F2FS_SLOT_LEN];
  253. int max;
  254. };
  255. static inline void make_dentry_ptr(struct inode *inode,
  256. struct f2fs_dentry_ptr *d, void *src, int type)
  257. {
  258. d->inode = inode;
  259. if (type == 1) {
  260. struct f2fs_dentry_block *t = (struct f2fs_dentry_block *)src;
  261. d->max = NR_DENTRY_IN_BLOCK;
  262. d->bitmap = &t->dentry_bitmap;
  263. d->dentry = t->dentry;
  264. d->filename = t->filename;
  265. } else {
  266. struct f2fs_inline_dentry *t = (struct f2fs_inline_dentry *)src;
  267. d->max = NR_INLINE_DENTRY;
  268. d->bitmap = &t->dentry_bitmap;
  269. d->dentry = t->dentry;
  270. d->filename = t->filename;
  271. }
  272. }
  273. /*
  274. * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
  275. * as its node offset to distinguish from index node blocks.
  276. * But some bits are used to mark the node block.
  277. */
  278. #define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
  279. >> OFFSET_BIT_SHIFT)
  280. enum {
  281. ALLOC_NODE, /* allocate a new node page if needed */
  282. LOOKUP_NODE, /* look up a node without readahead */
  283. LOOKUP_NODE_RA, /*
  284. * look up a node with readahead called
  285. * by get_data_block.
  286. */
  287. };
  288. #define F2FS_LINK_MAX 0xffffffff /* maximum link count per file */
  289. #define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */
  290. /* vector size for gang look-up from extent cache that consists of radix tree */
  291. #define EXT_TREE_VEC_SIZE 64
  292. /* for in-memory extent cache entry */
  293. #define F2FS_MIN_EXTENT_LEN 64 /* minimum extent length */
  294. /* number of extent info in extent cache we try to shrink */
  295. #define EXTENT_CACHE_SHRINK_NUMBER 128
  296. struct extent_info {
  297. unsigned int fofs; /* start offset in a file */
  298. u32 blk; /* start block address of the extent */
  299. unsigned int len; /* length of the extent */
  300. };
  301. struct extent_node {
  302. struct rb_node rb_node; /* rb node located in rb-tree */
  303. struct list_head list; /* node in global extent list of sbi */
  304. struct extent_info ei; /* extent info */
  305. };
  306. struct extent_tree {
  307. nid_t ino; /* inode number */
  308. struct rb_root root; /* root of extent info rb-tree */
  309. struct extent_node *cached_en; /* recently accessed extent node */
  310. struct extent_info largest; /* largested extent info */
  311. rwlock_t lock; /* protect extent info rb-tree */
  312. atomic_t refcount; /* reference count of rb-tree */
  313. unsigned int count; /* # of extent node in rb-tree*/
  314. };
  315. /*
  316. * This structure is taken from ext4_map_blocks.
  317. *
  318. * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
  319. */
  320. #define F2FS_MAP_NEW (1 << BH_New)
  321. #define F2FS_MAP_MAPPED (1 << BH_Mapped)
  322. #define F2FS_MAP_UNWRITTEN (1 << BH_Unwritten)
  323. #define F2FS_MAP_FLAGS (F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
  324. F2FS_MAP_UNWRITTEN)
  325. struct f2fs_map_blocks {
  326. block_t m_pblk;
  327. block_t m_lblk;
  328. unsigned int m_len;
  329. unsigned int m_flags;
  330. };
  331. /* for flag in get_data_block */
  332. #define F2FS_GET_BLOCK_READ 0
  333. #define F2FS_GET_BLOCK_DIO 1
  334. #define F2FS_GET_BLOCK_FIEMAP 2
  335. #define F2FS_GET_BLOCK_BMAP 3
  336. /*
  337. * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
  338. */
  339. #define FADVISE_COLD_BIT 0x01
  340. #define FADVISE_LOST_PINO_BIT 0x02
  341. #define FADVISE_ENCRYPT_BIT 0x04
  342. #define FADVISE_ENC_NAME_BIT 0x08
  343. #define file_is_cold(inode) is_file(inode, FADVISE_COLD_BIT)
  344. #define file_wrong_pino(inode) is_file(inode, FADVISE_LOST_PINO_BIT)
  345. #define file_set_cold(inode) set_file(inode, FADVISE_COLD_BIT)
  346. #define file_lost_pino(inode) set_file(inode, FADVISE_LOST_PINO_BIT)
  347. #define file_clear_cold(inode) clear_file(inode, FADVISE_COLD_BIT)
  348. #define file_got_pino(inode) clear_file(inode, FADVISE_LOST_PINO_BIT)
  349. #define file_is_encrypt(inode) is_file(inode, FADVISE_ENCRYPT_BIT)
  350. #define file_set_encrypt(inode) set_file(inode, FADVISE_ENCRYPT_BIT)
  351. #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
  352. #define file_enc_name(inode) is_file(inode, FADVISE_ENC_NAME_BIT)
  353. #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
  354. /* Encryption algorithms */
  355. #define F2FS_ENCRYPTION_MODE_INVALID 0
  356. #define F2FS_ENCRYPTION_MODE_AES_256_XTS 1
  357. #define F2FS_ENCRYPTION_MODE_AES_256_GCM 2
  358. #define F2FS_ENCRYPTION_MODE_AES_256_CBC 3
  359. #define F2FS_ENCRYPTION_MODE_AES_256_CTS 4
  360. #include "f2fs_crypto.h"
  361. #define DEF_DIR_LEVEL 0
  362. struct f2fs_inode_info {
  363. struct inode vfs_inode; /* serve a vfs inode */
  364. unsigned long i_flags; /* keep an inode flags for ioctl */
  365. unsigned char i_advise; /* use to give file attribute hints */
  366. unsigned char i_dir_level; /* use for dentry level for large dir */
  367. unsigned int i_current_depth; /* use only in directory structure */
  368. unsigned int i_pino; /* parent inode number */
  369. umode_t i_acl_mode; /* keep file acl mode temporarily */
  370. /* Use below internally in f2fs*/
  371. unsigned long flags; /* use to pass per-file flags */
  372. struct rw_semaphore i_sem; /* protect fi info */
  373. atomic_t dirty_pages; /* # of dirty pages */
  374. f2fs_hash_t chash; /* hash value of given file name */
  375. unsigned int clevel; /* maximum level of given file name */
  376. nid_t i_xattr_nid; /* node id that contains xattrs */
  377. unsigned long long xattr_ver; /* cp version of xattr modification */
  378. struct inode_entry *dirty_dir; /* the pointer of dirty dir */
  379. struct list_head inmem_pages; /* inmemory pages managed by f2fs */
  380. struct mutex inmem_lock; /* lock for inmemory pages */
  381. struct extent_tree *extent_tree; /* cached extent_tree entry */
  382. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  383. /* Encryption params */
  384. struct f2fs_crypt_info *i_crypt_info;
  385. #endif
  386. };
  387. static inline void get_extent_info(struct extent_info *ext,
  388. struct f2fs_extent i_ext)
  389. {
  390. ext->fofs = le32_to_cpu(i_ext.fofs);
  391. ext->blk = le32_to_cpu(i_ext.blk);
  392. ext->len = le32_to_cpu(i_ext.len);
  393. }
  394. static inline void set_raw_extent(struct extent_info *ext,
  395. struct f2fs_extent *i_ext)
  396. {
  397. i_ext->fofs = cpu_to_le32(ext->fofs);
  398. i_ext->blk = cpu_to_le32(ext->blk);
  399. i_ext->len = cpu_to_le32(ext->len);
  400. }
  401. static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
  402. u32 blk, unsigned int len)
  403. {
  404. ei->fofs = fofs;
  405. ei->blk = blk;
  406. ei->len = len;
  407. }
  408. static inline bool __is_extent_same(struct extent_info *ei1,
  409. struct extent_info *ei2)
  410. {
  411. return (ei1->fofs == ei2->fofs && ei1->blk == ei2->blk &&
  412. ei1->len == ei2->len);
  413. }
  414. static inline bool __is_extent_mergeable(struct extent_info *back,
  415. struct extent_info *front)
  416. {
  417. return (back->fofs + back->len == front->fofs &&
  418. back->blk + back->len == front->blk);
  419. }
  420. static inline bool __is_back_mergeable(struct extent_info *cur,
  421. struct extent_info *back)
  422. {
  423. return __is_extent_mergeable(back, cur);
  424. }
  425. static inline bool __is_front_mergeable(struct extent_info *cur,
  426. struct extent_info *front)
  427. {
  428. return __is_extent_mergeable(cur, front);
  429. }
  430. static inline void __try_update_largest_extent(struct extent_tree *et,
  431. struct extent_node *en)
  432. {
  433. if (en->ei.len > et->largest.len)
  434. et->largest = en->ei;
  435. }
  436. struct f2fs_nm_info {
  437. block_t nat_blkaddr; /* base disk address of NAT */
  438. nid_t max_nid; /* maximum possible node ids */
  439. nid_t available_nids; /* maximum available node ids */
  440. nid_t next_scan_nid; /* the next nid to be scanned */
  441. unsigned int ram_thresh; /* control the memory footprint */
  442. unsigned int ra_nid_pages; /* # of nid pages to be readaheaded */
  443. /* NAT cache management */
  444. struct radix_tree_root nat_root;/* root of the nat entry cache */
  445. struct radix_tree_root nat_set_root;/* root of the nat set cache */
  446. struct rw_semaphore nat_tree_lock; /* protect nat_tree_lock */
  447. struct list_head nat_entries; /* cached nat entry list (clean) */
  448. unsigned int nat_cnt; /* the # of cached nat entries */
  449. unsigned int dirty_nat_cnt; /* total num of nat entries in set */
  450. /* free node ids management */
  451. struct radix_tree_root free_nid_root;/* root of the free_nid cache */
  452. struct list_head free_nid_list; /* a list for free nids */
  453. spinlock_t free_nid_list_lock; /* protect free nid list */
  454. unsigned int fcnt; /* the number of free node id */
  455. struct mutex build_lock; /* lock for build free nids */
  456. /* for checkpoint */
  457. char *nat_bitmap; /* NAT bitmap pointer */
  458. int bitmap_size; /* bitmap size */
  459. };
  460. /*
  461. * this structure is used as one of function parameters.
  462. * all the information are dedicated to a given direct node block determined
  463. * by the data offset in a file.
  464. */
  465. struct dnode_of_data {
  466. struct inode *inode; /* vfs inode pointer */
  467. struct page *inode_page; /* its inode page, NULL is possible */
  468. struct page *node_page; /* cached direct node page */
  469. nid_t nid; /* node id of the direct node block */
  470. unsigned int ofs_in_node; /* data offset in the node page */
  471. bool inode_page_locked; /* inode page is locked or not */
  472. block_t data_blkaddr; /* block address of the node block */
  473. };
  474. static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
  475. struct page *ipage, struct page *npage, nid_t nid)
  476. {
  477. memset(dn, 0, sizeof(*dn));
  478. dn->inode = inode;
  479. dn->inode_page = ipage;
  480. dn->node_page = npage;
  481. dn->nid = nid;
  482. }
  483. /*
  484. * For SIT manager
  485. *
  486. * By default, there are 6 active log areas across the whole main area.
  487. * When considering hot and cold data separation to reduce cleaning overhead,
  488. * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
  489. * respectively.
  490. * In the current design, you should not change the numbers intentionally.
  491. * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
  492. * logs individually according to the underlying devices. (default: 6)
  493. * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
  494. * data and 8 for node logs.
  495. */
  496. #define NR_CURSEG_DATA_TYPE (3)
  497. #define NR_CURSEG_NODE_TYPE (3)
  498. #define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
  499. enum {
  500. CURSEG_HOT_DATA = 0, /* directory entry blocks */
  501. CURSEG_WARM_DATA, /* data blocks */
  502. CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
  503. CURSEG_HOT_NODE, /* direct node blocks of directory files */
  504. CURSEG_WARM_NODE, /* direct node blocks of normal files */
  505. CURSEG_COLD_NODE, /* indirect node blocks */
  506. NO_CHECK_TYPE,
  507. CURSEG_DIRECT_IO, /* to use for the direct IO path */
  508. };
  509. struct flush_cmd {
  510. struct completion wait;
  511. struct llist_node llnode;
  512. int ret;
  513. };
  514. struct flush_cmd_control {
  515. struct task_struct *f2fs_issue_flush; /* flush thread */
  516. wait_queue_head_t flush_wait_queue; /* waiting queue for wake-up */
  517. struct llist_head issue_list; /* list for command issue */
  518. struct llist_node *dispatch_list; /* list for command dispatch */
  519. };
  520. struct f2fs_sm_info {
  521. struct sit_info *sit_info; /* whole segment information */
  522. struct free_segmap_info *free_info; /* free segment information */
  523. struct dirty_seglist_info *dirty_info; /* dirty segment information */
  524. struct curseg_info *curseg_array; /* active segment information */
  525. block_t seg0_blkaddr; /* block address of 0'th segment */
  526. block_t main_blkaddr; /* start block address of main area */
  527. block_t ssa_blkaddr; /* start block address of SSA area */
  528. unsigned int segment_count; /* total # of segments */
  529. unsigned int main_segments; /* # of segments in main area */
  530. unsigned int reserved_segments; /* # of reserved segments */
  531. unsigned int ovp_segments; /* # of overprovision segments */
  532. /* a threshold to reclaim prefree segments */
  533. unsigned int rec_prefree_segments;
  534. /* for small discard management */
  535. struct list_head discard_list; /* 4KB discard list */
  536. int nr_discards; /* # of discards in the list */
  537. int max_discards; /* max. discards to be issued */
  538. /* for batched trimming */
  539. unsigned int trim_sections; /* # of sections to trim */
  540. struct list_head sit_entry_set; /* sit entry set list */
  541. unsigned int ipu_policy; /* in-place-update policy */
  542. unsigned int min_ipu_util; /* in-place-update threshold */
  543. unsigned int min_fsync_blocks; /* threshold for fsync */
  544. /* for flush command control */
  545. struct flush_cmd_control *cmd_control_info;
  546. };
  547. /*
  548. * For superblock
  549. */
  550. /*
  551. * COUNT_TYPE for monitoring
  552. *
  553. * f2fs monitors the number of several block types such as on-writeback,
  554. * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
  555. */
  556. enum count_type {
  557. F2FS_WRITEBACK,
  558. F2FS_DIRTY_DENTS,
  559. F2FS_DIRTY_NODES,
  560. F2FS_DIRTY_META,
  561. F2FS_INMEM_PAGES,
  562. NR_COUNT_TYPE,
  563. };
  564. /*
  565. * The below are the page types of bios used in submit_bio().
  566. * The available types are:
  567. * DATA User data pages. It operates as async mode.
  568. * NODE Node pages. It operates as async mode.
  569. * META FS metadata pages such as SIT, NAT, CP.
  570. * NR_PAGE_TYPE The number of page types.
  571. * META_FLUSH Make sure the previous pages are written
  572. * with waiting the bio's completion
  573. * ... Only can be used with META.
  574. */
  575. #define PAGE_TYPE_OF_BIO(type) ((type) > META ? META : (type))
  576. enum page_type {
  577. DATA,
  578. NODE,
  579. META,
  580. NR_PAGE_TYPE,
  581. META_FLUSH,
  582. INMEM, /* the below types are used by tracepoints only. */
  583. INMEM_DROP,
  584. IPU,
  585. OPU,
  586. };
  587. struct f2fs_io_info {
  588. struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */
  589. enum page_type type; /* contains DATA/NODE/META/META_FLUSH */
  590. int rw; /* contains R/RS/W/WS with REQ_META/REQ_PRIO */
  591. block_t blk_addr; /* block address to be written */
  592. struct page *page; /* page to be written */
  593. struct page *encrypted_page; /* encrypted page */
  594. };
  595. #define is_read_io(rw) (((rw) & 1) == READ)
  596. struct f2fs_bio_info {
  597. struct f2fs_sb_info *sbi; /* f2fs superblock */
  598. struct bio *bio; /* bios to merge */
  599. sector_t last_block_in_bio; /* last block number */
  600. struct f2fs_io_info fio; /* store buffered io info. */
  601. struct rw_semaphore io_rwsem; /* blocking op for bio */
  602. };
  603. /* for inner inode cache management */
  604. struct inode_management {
  605. struct radix_tree_root ino_root; /* ino entry array */
  606. spinlock_t ino_lock; /* for ino entry lock */
  607. struct list_head ino_list; /* inode list head */
  608. unsigned long ino_num; /* number of entries */
  609. };
  610. /* For s_flag in struct f2fs_sb_info */
  611. enum {
  612. SBI_IS_DIRTY, /* dirty flag for checkpoint */
  613. SBI_IS_CLOSE, /* specify unmounting */
  614. SBI_NEED_FSCK, /* need fsck.f2fs to fix */
  615. SBI_POR_DOING, /* recovery is doing or not */
  616. };
  617. struct f2fs_sb_info {
  618. struct super_block *sb; /* pointer to VFS super block */
  619. struct proc_dir_entry *s_proc; /* proc entry */
  620. struct buffer_head *raw_super_buf; /* buffer head of raw sb */
  621. struct f2fs_super_block *raw_super; /* raw super block pointer */
  622. int s_flag; /* flags for sbi */
  623. /* for node-related operations */
  624. struct f2fs_nm_info *nm_info; /* node manager */
  625. struct inode *node_inode; /* cache node blocks */
  626. /* for segment-related operations */
  627. struct f2fs_sm_info *sm_info; /* segment manager */
  628. /* for bio operations */
  629. struct f2fs_bio_info read_io; /* for read bios */
  630. struct f2fs_bio_info write_io[NR_PAGE_TYPE]; /* for write bios */
  631. /* for checkpoint */
  632. struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
  633. struct inode *meta_inode; /* cache meta blocks */
  634. struct mutex cp_mutex; /* checkpoint procedure lock */
  635. struct rw_semaphore cp_rwsem; /* blocking FS operations */
  636. struct rw_semaphore node_write; /* locking node writes */
  637. struct mutex writepages; /* mutex for writepages() */
  638. wait_queue_head_t cp_wait;
  639. long cp_expires, cp_interval; /* next expected periodic cp */
  640. struct inode_management im[MAX_INO_ENTRY]; /* manage inode cache */
  641. /* for orphan inode, use 0'th array */
  642. unsigned int max_orphans; /* max orphan inodes */
  643. /* for directory inode management */
  644. struct list_head dir_inode_list; /* dir inode list */
  645. spinlock_t dir_inode_lock; /* for dir inode list lock */
  646. /* for extent tree cache */
  647. struct radix_tree_root extent_tree_root;/* cache extent cache entries */
  648. struct rw_semaphore extent_tree_lock; /* locking extent radix tree */
  649. struct list_head extent_list; /* lru list for shrinker */
  650. spinlock_t extent_lock; /* locking extent lru list */
  651. int total_ext_tree; /* extent tree count */
  652. atomic_t total_ext_node; /* extent info count */
  653. /* basic filesystem units */
  654. unsigned int log_sectors_per_block; /* log2 sectors per block */
  655. unsigned int log_blocksize; /* log2 block size */
  656. unsigned int blocksize; /* block size */
  657. unsigned int root_ino_num; /* root inode number*/
  658. unsigned int node_ino_num; /* node inode number*/
  659. unsigned int meta_ino_num; /* meta inode number*/
  660. unsigned int log_blocks_per_seg; /* log2 blocks per segment */
  661. unsigned int blocks_per_seg; /* blocks per segment */
  662. unsigned int segs_per_sec; /* segments per section */
  663. unsigned int secs_per_zone; /* sections per zone */
  664. unsigned int total_sections; /* total section count */
  665. unsigned int total_node_count; /* total node block count */
  666. unsigned int total_valid_node_count; /* valid node block count */
  667. unsigned int total_valid_inode_count; /* valid inode count */
  668. int active_logs; /* # of active logs */
  669. int dir_level; /* directory level */
  670. block_t user_block_count; /* # of user blocks */
  671. block_t total_valid_block_count; /* # of valid blocks */
  672. block_t alloc_valid_block_count; /* # of allocated blocks */
  673. block_t discard_blks; /* discard command candidats */
  674. block_t last_valid_block_count; /* for recovery */
  675. u32 s_next_generation; /* for NFS support */
  676. atomic_t nr_pages[NR_COUNT_TYPE]; /* # of pages, see count_type */
  677. struct f2fs_mount_info mount_opt; /* mount options */
  678. /* for cleaning operations */
  679. struct mutex gc_mutex; /* mutex for GC */
  680. struct f2fs_gc_kthread *gc_thread; /* GC thread */
  681. unsigned int cur_victim_sec; /* current victim section num */
  682. /* maximum # of trials to find a victim segment for SSR and GC */
  683. unsigned int max_victim_search;
  684. /*
  685. * for stat information.
  686. * one is for the LFS mode, and the other is for the SSR mode.
  687. */
  688. #ifdef CONFIG_F2FS_STAT_FS
  689. struct f2fs_stat_info *stat_info; /* FS status information */
  690. unsigned int segment_count[2]; /* # of allocated segments */
  691. unsigned int block_count[2]; /* # of allocated blocks */
  692. atomic_t inplace_count; /* # of inplace update */
  693. atomic64_t total_hit_ext; /* # of lookup extent cache */
  694. atomic64_t read_hit_rbtree; /* # of hit rbtree extent node */
  695. atomic64_t read_hit_largest; /* # of hit largest extent node */
  696. atomic64_t read_hit_cached; /* # of hit cached extent node */
  697. atomic_t inline_xattr; /* # of inline_xattr inodes */
  698. atomic_t inline_inode; /* # of inline_data inodes */
  699. atomic_t inline_dir; /* # of inline_dentry inodes */
  700. int bg_gc; /* background gc calls */
  701. unsigned int n_dirty_dirs; /* # of dir inodes */
  702. #endif
  703. unsigned int last_victim[2]; /* last victim segment # */
  704. spinlock_t stat_lock; /* lock for stat operations */
  705. /* For sysfs suppport */
  706. struct kobject s_kobj;
  707. struct completion s_kobj_unregister;
  708. /* For shrinker support */
  709. struct list_head s_list;
  710. struct mutex umount_mutex;
  711. unsigned int shrinker_run_no;
  712. };
  713. /*
  714. * Inline functions
  715. */
  716. static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
  717. {
  718. return container_of(inode, struct f2fs_inode_info, vfs_inode);
  719. }
  720. static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
  721. {
  722. return sb->s_fs_info;
  723. }
  724. static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
  725. {
  726. return F2FS_SB(inode->i_sb);
  727. }
  728. static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
  729. {
  730. return F2FS_I_SB(mapping->host);
  731. }
  732. static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
  733. {
  734. return F2FS_M_SB(page->mapping);
  735. }
  736. static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
  737. {
  738. return (struct f2fs_super_block *)(sbi->raw_super);
  739. }
  740. static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
  741. {
  742. return (struct f2fs_checkpoint *)(sbi->ckpt);
  743. }
  744. static inline struct f2fs_node *F2FS_NODE(struct page *page)
  745. {
  746. return (struct f2fs_node *)page_address(page);
  747. }
  748. static inline struct f2fs_inode *F2FS_INODE(struct page *page)
  749. {
  750. return &((struct f2fs_node *)page_address(page))->i;
  751. }
  752. static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
  753. {
  754. return (struct f2fs_nm_info *)(sbi->nm_info);
  755. }
  756. static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
  757. {
  758. return (struct f2fs_sm_info *)(sbi->sm_info);
  759. }
  760. static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
  761. {
  762. return (struct sit_info *)(SM_I(sbi)->sit_info);
  763. }
  764. static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
  765. {
  766. return (struct free_segmap_info *)(SM_I(sbi)->free_info);
  767. }
  768. static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
  769. {
  770. return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
  771. }
  772. static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
  773. {
  774. return sbi->meta_inode->i_mapping;
  775. }
  776. static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
  777. {
  778. return sbi->node_inode->i_mapping;
  779. }
  780. static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
  781. {
  782. return sbi->s_flag & (0x01 << type);
  783. }
  784. static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
  785. {
  786. sbi->s_flag |= (0x01 << type);
  787. }
  788. static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
  789. {
  790. sbi->s_flag &= ~(0x01 << type);
  791. }
  792. static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
  793. {
  794. return le64_to_cpu(cp->checkpoint_ver);
  795. }
  796. static inline bool is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  797. {
  798. unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  799. return ckpt_flags & f;
  800. }
  801. static inline void set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  802. {
  803. unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  804. ckpt_flags |= f;
  805. cp->ckpt_flags = cpu_to_le32(ckpt_flags);
  806. }
  807. static inline void clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  808. {
  809. unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  810. ckpt_flags &= (~f);
  811. cp->ckpt_flags = cpu_to_le32(ckpt_flags);
  812. }
  813. static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
  814. {
  815. down_read(&sbi->cp_rwsem);
  816. }
  817. static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
  818. {
  819. up_read(&sbi->cp_rwsem);
  820. }
  821. static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
  822. {
  823. f2fs_down_write(&sbi->cp_rwsem, &sbi->cp_mutex);
  824. }
  825. static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
  826. {
  827. up_write(&sbi->cp_rwsem);
  828. }
  829. static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
  830. {
  831. int reason = CP_SYNC;
  832. if (test_opt(sbi, FASTBOOT))
  833. reason = CP_FASTBOOT;
  834. if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
  835. reason = CP_UMOUNT;
  836. return reason;
  837. }
  838. static inline bool __remain_node_summaries(int reason)
  839. {
  840. return (reason == CP_UMOUNT || reason == CP_FASTBOOT);
  841. }
  842. static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
  843. {
  844. return (is_set_ckpt_flags(F2FS_CKPT(sbi), CP_UMOUNT_FLAG) ||
  845. is_set_ckpt_flags(F2FS_CKPT(sbi), CP_FASTBOOT_FLAG));
  846. }
  847. /*
  848. * Check whether the given nid is within node id range.
  849. */
  850. static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
  851. {
  852. if (unlikely(nid < F2FS_ROOT_INO(sbi)))
  853. return -EINVAL;
  854. if (unlikely(nid >= NM_I(sbi)->max_nid))
  855. return -EINVAL;
  856. return 0;
  857. }
  858. #define F2FS_DEFAULT_ALLOCATED_BLOCKS 1
  859. /*
  860. * Check whether the inode has blocks or not
  861. */
  862. static inline int F2FS_HAS_BLOCKS(struct inode *inode)
  863. {
  864. if (F2FS_I(inode)->i_xattr_nid)
  865. return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS + 1;
  866. else
  867. return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS;
  868. }
  869. static inline bool f2fs_has_xattr_block(unsigned int ofs)
  870. {
  871. return ofs == XATTR_NODE_OFFSET;
  872. }
  873. static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
  874. struct inode *inode, blkcnt_t count)
  875. {
  876. block_t valid_block_count;
  877. spin_lock(&sbi->stat_lock);
  878. valid_block_count =
  879. sbi->total_valid_block_count + (block_t)count;
  880. if (unlikely(valid_block_count > sbi->user_block_count)) {
  881. spin_unlock(&sbi->stat_lock);
  882. return false;
  883. }
  884. inode->i_blocks += count;
  885. sbi->total_valid_block_count = valid_block_count;
  886. sbi->alloc_valid_block_count += (block_t)count;
  887. spin_unlock(&sbi->stat_lock);
  888. return true;
  889. }
  890. static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
  891. struct inode *inode,
  892. blkcnt_t count)
  893. {
  894. spin_lock(&sbi->stat_lock);
  895. f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
  896. f2fs_bug_on(sbi, inode->i_blocks < count);
  897. inode->i_blocks -= count;
  898. sbi->total_valid_block_count -= (block_t)count;
  899. spin_unlock(&sbi->stat_lock);
  900. }
  901. static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
  902. {
  903. atomic_inc(&sbi->nr_pages[count_type]);
  904. set_sbi_flag(sbi, SBI_IS_DIRTY);
  905. }
  906. static inline void inode_inc_dirty_pages(struct inode *inode)
  907. {
  908. atomic_inc(&F2FS_I(inode)->dirty_pages);
  909. if (S_ISDIR(inode->i_mode))
  910. inc_page_count(F2FS_I_SB(inode), F2FS_DIRTY_DENTS);
  911. }
  912. static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
  913. {
  914. atomic_dec(&sbi->nr_pages[count_type]);
  915. }
  916. static inline void inode_dec_dirty_pages(struct inode *inode)
  917. {
  918. if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
  919. !S_ISLNK(inode->i_mode))
  920. return;
  921. atomic_dec(&F2FS_I(inode)->dirty_pages);
  922. if (S_ISDIR(inode->i_mode))
  923. dec_page_count(F2FS_I_SB(inode), F2FS_DIRTY_DENTS);
  924. }
  925. static inline int get_pages(struct f2fs_sb_info *sbi, int count_type)
  926. {
  927. return atomic_read(&sbi->nr_pages[count_type]);
  928. }
  929. static inline int get_dirty_pages(struct inode *inode)
  930. {
  931. return atomic_read(&F2FS_I(inode)->dirty_pages);
  932. }
  933. static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
  934. {
  935. unsigned int pages_per_sec = sbi->segs_per_sec *
  936. (1 << sbi->log_blocks_per_seg);
  937. return ((get_pages(sbi, block_type) + pages_per_sec - 1)
  938. >> sbi->log_blocks_per_seg) / sbi->segs_per_sec;
  939. }
  940. static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
  941. {
  942. return sbi->total_valid_block_count;
  943. }
  944. static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
  945. {
  946. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  947. /* return NAT or SIT bitmap */
  948. if (flag == NAT_BITMAP)
  949. return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
  950. else if (flag == SIT_BITMAP)
  951. return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
  952. return 0;
  953. }
  954. static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
  955. {
  956. return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
  957. }
  958. static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
  959. {
  960. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  961. int offset;
  962. if (__cp_payload(sbi) > 0) {
  963. if (flag == NAT_BITMAP)
  964. return &ckpt->sit_nat_version_bitmap;
  965. else
  966. return (unsigned char *)ckpt + F2FS_BLKSIZE;
  967. } else {
  968. offset = (flag == NAT_BITMAP) ?
  969. le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
  970. return &ckpt->sit_nat_version_bitmap + offset;
  971. }
  972. }
  973. static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
  974. {
  975. block_t start_addr;
  976. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  977. unsigned long long ckpt_version = cur_cp_version(ckpt);
  978. start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
  979. /*
  980. * odd numbered checkpoint should at cp segment 0
  981. * and even segment must be at cp segment 1
  982. */
  983. if (!(ckpt_version & 1))
  984. start_addr += sbi->blocks_per_seg;
  985. return start_addr;
  986. }
  987. static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
  988. {
  989. return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
  990. }
  991. static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
  992. struct inode *inode)
  993. {
  994. block_t valid_block_count;
  995. unsigned int valid_node_count;
  996. spin_lock(&sbi->stat_lock);
  997. valid_block_count = sbi->total_valid_block_count + 1;
  998. if (unlikely(valid_block_count > sbi->user_block_count)) {
  999. spin_unlock(&sbi->stat_lock);
  1000. return false;
  1001. }
  1002. valid_node_count = sbi->total_valid_node_count + 1;
  1003. if (unlikely(valid_node_count > sbi->total_node_count)) {
  1004. spin_unlock(&sbi->stat_lock);
  1005. return false;
  1006. }
  1007. if (inode)
  1008. inode->i_blocks++;
  1009. sbi->alloc_valid_block_count++;
  1010. sbi->total_valid_node_count++;
  1011. sbi->total_valid_block_count++;
  1012. spin_unlock(&sbi->stat_lock);
  1013. return true;
  1014. }
  1015. static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
  1016. struct inode *inode)
  1017. {
  1018. spin_lock(&sbi->stat_lock);
  1019. f2fs_bug_on(sbi, !sbi->total_valid_block_count);
  1020. f2fs_bug_on(sbi, !sbi->total_valid_node_count);
  1021. f2fs_bug_on(sbi, !inode->i_blocks);
  1022. inode->i_blocks--;
  1023. sbi->total_valid_node_count--;
  1024. sbi->total_valid_block_count--;
  1025. spin_unlock(&sbi->stat_lock);
  1026. }
  1027. static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
  1028. {
  1029. return sbi->total_valid_node_count;
  1030. }
  1031. static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
  1032. {
  1033. spin_lock(&sbi->stat_lock);
  1034. f2fs_bug_on(sbi, sbi->total_valid_inode_count == sbi->total_node_count);
  1035. sbi->total_valid_inode_count++;
  1036. spin_unlock(&sbi->stat_lock);
  1037. }
  1038. static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
  1039. {
  1040. spin_lock(&sbi->stat_lock);
  1041. f2fs_bug_on(sbi, !sbi->total_valid_inode_count);
  1042. sbi->total_valid_inode_count--;
  1043. spin_unlock(&sbi->stat_lock);
  1044. }
  1045. static inline unsigned int valid_inode_count(struct f2fs_sb_info *sbi)
  1046. {
  1047. return sbi->total_valid_inode_count;
  1048. }
  1049. static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
  1050. pgoff_t index, bool for_write)
  1051. {
  1052. if (!for_write)
  1053. return grab_cache_page(mapping, index);
  1054. return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
  1055. }
  1056. static inline void f2fs_copy_page(struct page *src, struct page *dst)
  1057. {
  1058. char *src_kaddr = kmap(src);
  1059. char *dst_kaddr = kmap(dst);
  1060. memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
  1061. kunmap(dst);
  1062. kunmap(src);
  1063. }
  1064. static inline void f2fs_put_page(struct page *page, int unlock)
  1065. {
  1066. if (!page)
  1067. return;
  1068. if (unlock) {
  1069. f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
  1070. unlock_page(page);
  1071. }
  1072. page_cache_release(page);
  1073. }
  1074. static inline void f2fs_put_dnode(struct dnode_of_data *dn)
  1075. {
  1076. if (dn->node_page)
  1077. f2fs_put_page(dn->node_page, 1);
  1078. if (dn->inode_page && dn->node_page != dn->inode_page)
  1079. f2fs_put_page(dn->inode_page, 0);
  1080. dn->node_page = NULL;
  1081. dn->inode_page = NULL;
  1082. }
  1083. static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
  1084. size_t size)
  1085. {
  1086. return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
  1087. }
  1088. static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
  1089. gfp_t flags)
  1090. {
  1091. void *entry;
  1092. entry = kmem_cache_alloc(cachep, flags);
  1093. if (!entry)
  1094. entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
  1095. return entry;
  1096. }
  1097. static inline struct bio *f2fs_bio_alloc(int npages)
  1098. {
  1099. struct bio *bio;
  1100. /* No failure on bio allocation */
  1101. bio = bio_alloc(GFP_NOIO, npages);
  1102. if (!bio)
  1103. bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
  1104. return bio;
  1105. }
  1106. static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
  1107. unsigned long index, void *item)
  1108. {
  1109. while (radix_tree_insert(root, index, item))
  1110. cond_resched();
  1111. }
  1112. #define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
  1113. static inline bool IS_INODE(struct page *page)
  1114. {
  1115. struct f2fs_node *p = F2FS_NODE(page);
  1116. return RAW_IS_INODE(p);
  1117. }
  1118. static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
  1119. {
  1120. return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
  1121. }
  1122. static inline block_t datablock_addr(struct page *node_page,
  1123. unsigned int offset)
  1124. {
  1125. struct f2fs_node *raw_node;
  1126. __le32 *addr_array;
  1127. raw_node = F2FS_NODE(node_page);
  1128. addr_array = blkaddr_in_node(raw_node);
  1129. return le32_to_cpu(addr_array[offset]);
  1130. }
  1131. static inline int f2fs_test_bit(unsigned int nr, char *addr)
  1132. {
  1133. int mask;
  1134. addr += (nr >> 3);
  1135. mask = 1 << (7 - (nr & 0x07));
  1136. return mask & *addr;
  1137. }
  1138. static inline void f2fs_set_bit(unsigned int nr, char *addr)
  1139. {
  1140. int mask;
  1141. addr += (nr >> 3);
  1142. mask = 1 << (7 - (nr & 0x07));
  1143. *addr |= mask;
  1144. }
  1145. static inline void f2fs_clear_bit(unsigned int nr, char *addr)
  1146. {
  1147. int mask;
  1148. addr += (nr >> 3);
  1149. mask = 1 << (7 - (nr & 0x07));
  1150. *addr &= ~mask;
  1151. }
  1152. static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
  1153. {
  1154. int mask;
  1155. int ret;
  1156. addr += (nr >> 3);
  1157. mask = 1 << (7 - (nr & 0x07));
  1158. ret = mask & *addr;
  1159. *addr |= mask;
  1160. return ret;
  1161. }
  1162. static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
  1163. {
  1164. int mask;
  1165. int ret;
  1166. addr += (nr >> 3);
  1167. mask = 1 << (7 - (nr & 0x07));
  1168. ret = mask & *addr;
  1169. *addr &= ~mask;
  1170. return ret;
  1171. }
  1172. static inline void f2fs_change_bit(unsigned int nr, char *addr)
  1173. {
  1174. int mask;
  1175. addr += (nr >> 3);
  1176. mask = 1 << (7 - (nr & 0x07));
  1177. *addr ^= mask;
  1178. }
  1179. /* used for f2fs_inode_info->flags */
  1180. enum {
  1181. FI_NEW_INODE, /* indicate newly allocated inode */
  1182. FI_DIRTY_INODE, /* indicate inode is dirty or not */
  1183. FI_DIRTY_DIR, /* indicate directory has dirty pages */
  1184. FI_INC_LINK, /* need to increment i_nlink */
  1185. FI_ACL_MODE, /* indicate acl mode */
  1186. FI_NO_ALLOC, /* should not allocate any blocks */
  1187. FI_FREE_NID, /* free allocated nide */
  1188. FI_UPDATE_DIR, /* should update inode block for consistency */
  1189. FI_DELAY_IPUT, /* used for the recovery */
  1190. FI_NO_EXTENT, /* not to use the extent cache */
  1191. FI_INLINE_XATTR, /* used for inline xattr */
  1192. FI_INLINE_DATA, /* used for inline data*/
  1193. FI_INLINE_DENTRY, /* used for inline dentry */
  1194. FI_APPEND_WRITE, /* inode has appended data */
  1195. FI_UPDATE_WRITE, /* inode has in-place-update data */
  1196. FI_NEED_IPU, /* used for ipu per file */
  1197. FI_ATOMIC_FILE, /* indicate atomic file */
  1198. FI_VOLATILE_FILE, /* indicate volatile file */
  1199. FI_FIRST_BLOCK_WRITTEN, /* indicate #0 data block was written */
  1200. FI_DROP_CACHE, /* drop dirty page cache */
  1201. FI_DATA_EXIST, /* indicate data exists */
  1202. FI_INLINE_DOTS, /* indicate inline dot dentries */
  1203. };
  1204. static inline void set_inode_flag(struct f2fs_inode_info *fi, int flag)
  1205. {
  1206. if (!test_bit(flag, &fi->flags))
  1207. set_bit(flag, &fi->flags);
  1208. }
  1209. static inline int is_inode_flag_set(struct f2fs_inode_info *fi, int flag)
  1210. {
  1211. return test_bit(flag, &fi->flags);
  1212. }
  1213. static inline void clear_inode_flag(struct f2fs_inode_info *fi, int flag)
  1214. {
  1215. if (test_bit(flag, &fi->flags))
  1216. clear_bit(flag, &fi->flags);
  1217. }
  1218. static inline void set_acl_inode(struct f2fs_inode_info *fi, umode_t mode)
  1219. {
  1220. fi->i_acl_mode = mode;
  1221. set_inode_flag(fi, FI_ACL_MODE);
  1222. }
  1223. static inline void get_inline_info(struct f2fs_inode_info *fi,
  1224. struct f2fs_inode *ri)
  1225. {
  1226. if (ri->i_inline & F2FS_INLINE_XATTR)
  1227. set_inode_flag(fi, FI_INLINE_XATTR);
  1228. if (ri->i_inline & F2FS_INLINE_DATA)
  1229. set_inode_flag(fi, FI_INLINE_DATA);
  1230. if (ri->i_inline & F2FS_INLINE_DENTRY)
  1231. set_inode_flag(fi, FI_INLINE_DENTRY);
  1232. if (ri->i_inline & F2FS_DATA_EXIST)
  1233. set_inode_flag(fi, FI_DATA_EXIST);
  1234. if (ri->i_inline & F2FS_INLINE_DOTS)
  1235. set_inode_flag(fi, FI_INLINE_DOTS);
  1236. }
  1237. static inline void set_raw_inline(struct f2fs_inode_info *fi,
  1238. struct f2fs_inode *ri)
  1239. {
  1240. ri->i_inline = 0;
  1241. if (is_inode_flag_set(fi, FI_INLINE_XATTR))
  1242. ri->i_inline |= F2FS_INLINE_XATTR;
  1243. if (is_inode_flag_set(fi, FI_INLINE_DATA))
  1244. ri->i_inline |= F2FS_INLINE_DATA;
  1245. if (is_inode_flag_set(fi, FI_INLINE_DENTRY))
  1246. ri->i_inline |= F2FS_INLINE_DENTRY;
  1247. if (is_inode_flag_set(fi, FI_DATA_EXIST))
  1248. ri->i_inline |= F2FS_DATA_EXIST;
  1249. if (is_inode_flag_set(fi, FI_INLINE_DOTS))
  1250. ri->i_inline |= F2FS_INLINE_DOTS;
  1251. }
  1252. static inline int f2fs_has_inline_xattr(struct inode *inode)
  1253. {
  1254. return is_inode_flag_set(F2FS_I(inode), FI_INLINE_XATTR);
  1255. }
  1256. static inline unsigned int addrs_per_inode(struct f2fs_inode_info *fi)
  1257. {
  1258. if (f2fs_has_inline_xattr(&fi->vfs_inode))
  1259. return DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS;
  1260. return DEF_ADDRS_PER_INODE;
  1261. }
  1262. static inline void *inline_xattr_addr(struct page *page)
  1263. {
  1264. struct f2fs_inode *ri = F2FS_INODE(page);
  1265. return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
  1266. F2FS_INLINE_XATTR_ADDRS]);
  1267. }
  1268. static inline int inline_xattr_size(struct inode *inode)
  1269. {
  1270. if (f2fs_has_inline_xattr(inode))
  1271. return F2FS_INLINE_XATTR_ADDRS << 2;
  1272. else
  1273. return 0;
  1274. }
  1275. static inline int f2fs_has_inline_data(struct inode *inode)
  1276. {
  1277. return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DATA);
  1278. }
  1279. static inline void f2fs_clear_inline_inode(struct inode *inode)
  1280. {
  1281. clear_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
  1282. clear_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
  1283. }
  1284. static inline int f2fs_exist_data(struct inode *inode)
  1285. {
  1286. return is_inode_flag_set(F2FS_I(inode), FI_DATA_EXIST);
  1287. }
  1288. static inline int f2fs_has_inline_dots(struct inode *inode)
  1289. {
  1290. return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DOTS);
  1291. }
  1292. static inline bool f2fs_is_atomic_file(struct inode *inode)
  1293. {
  1294. return is_inode_flag_set(F2FS_I(inode), FI_ATOMIC_FILE);
  1295. }
  1296. static inline bool f2fs_is_volatile_file(struct inode *inode)
  1297. {
  1298. return is_inode_flag_set(F2FS_I(inode), FI_VOLATILE_FILE);
  1299. }
  1300. static inline bool f2fs_is_first_block_written(struct inode *inode)
  1301. {
  1302. return is_inode_flag_set(F2FS_I(inode), FI_FIRST_BLOCK_WRITTEN);
  1303. }
  1304. static inline bool f2fs_is_drop_cache(struct inode *inode)
  1305. {
  1306. return is_inode_flag_set(F2FS_I(inode), FI_DROP_CACHE);
  1307. }
  1308. static inline void *inline_data_addr(struct page *page)
  1309. {
  1310. struct f2fs_inode *ri = F2FS_INODE(page);
  1311. return (void *)&(ri->i_addr[1]);
  1312. }
  1313. static inline int f2fs_has_inline_dentry(struct inode *inode)
  1314. {
  1315. return is_inode_flag_set(F2FS_I(inode), FI_INLINE_DENTRY);
  1316. }
  1317. static inline void f2fs_dentry_kunmap(struct inode *dir, struct page *page)
  1318. {
  1319. if (!f2fs_has_inline_dentry(dir))
  1320. kunmap(page);
  1321. }
  1322. static inline int is_file(struct inode *inode, int type)
  1323. {
  1324. return F2FS_I(inode)->i_advise & type;
  1325. }
  1326. static inline void set_file(struct inode *inode, int type)
  1327. {
  1328. F2FS_I(inode)->i_advise |= type;
  1329. }
  1330. static inline void clear_file(struct inode *inode, int type)
  1331. {
  1332. F2FS_I(inode)->i_advise &= ~type;
  1333. }
  1334. static inline int f2fs_readonly(struct super_block *sb)
  1335. {
  1336. return sb->s_flags & MS_RDONLY;
  1337. }
  1338. static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
  1339. {
  1340. return is_set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
  1341. }
  1342. static inline void f2fs_stop_checkpoint(struct f2fs_sb_info *sbi)
  1343. {
  1344. set_ckpt_flags(sbi->ckpt, CP_ERROR_FLAG);
  1345. sbi->sb->s_flags |= MS_RDONLY;
  1346. }
  1347. static inline bool is_dot_dotdot(const struct qstr *str)
  1348. {
  1349. if (str->len == 1 && str->name[0] == '.')
  1350. return true;
  1351. if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
  1352. return true;
  1353. return false;
  1354. }
  1355. static inline bool f2fs_may_extent_tree(struct inode *inode)
  1356. {
  1357. mode_t mode = inode->i_mode;
  1358. if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
  1359. is_inode_flag_set(F2FS_I(inode), FI_NO_EXTENT))
  1360. return false;
  1361. return S_ISREG(mode);
  1362. }
  1363. static inline void *f2fs_kvmalloc(size_t size, gfp_t flags)
  1364. {
  1365. void *ret;
  1366. ret = kmalloc(size, flags | __GFP_NOWARN);
  1367. if (!ret)
  1368. ret = __vmalloc(size, flags, PAGE_KERNEL);
  1369. return ret;
  1370. }
  1371. static inline void *f2fs_kvzalloc(size_t size, gfp_t flags)
  1372. {
  1373. void *ret;
  1374. ret = kzalloc(size, flags | __GFP_NOWARN);
  1375. if (!ret)
  1376. ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
  1377. return ret;
  1378. }
  1379. #define get_inode_mode(i) \
  1380. ((is_inode_flag_set(F2FS_I(i), FI_ACL_MODE)) ? \
  1381. (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
  1382. /* get offset of first page in next direct node */
  1383. #define PGOFS_OF_NEXT_DNODE(pgofs, fi) \
  1384. ((pgofs < ADDRS_PER_INODE(fi)) ? ADDRS_PER_INODE(fi) : \
  1385. (pgofs - ADDRS_PER_INODE(fi) + ADDRS_PER_BLOCK) / \
  1386. ADDRS_PER_BLOCK * ADDRS_PER_BLOCK + ADDRS_PER_INODE(fi))
  1387. /*
  1388. * file.c
  1389. */
  1390. int f2fs_sync_file(struct file *, loff_t, loff_t, int);
  1391. void truncate_data_blocks(struct dnode_of_data *);
  1392. int truncate_blocks(struct inode *, u64, bool);
  1393. int f2fs_truncate(struct inode *, bool);
  1394. int f2fs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
  1395. int f2fs_setattr(struct dentry *, struct iattr *);
  1396. int truncate_hole(struct inode *, pgoff_t, pgoff_t);
  1397. int truncate_data_blocks_range(struct dnode_of_data *, int);
  1398. long f2fs_ioctl(struct file *, unsigned int, unsigned long);
  1399. long f2fs_compat_ioctl(struct file *, unsigned int, unsigned long);
  1400. /*
  1401. * inode.c
  1402. */
  1403. void f2fs_set_inode_flags(struct inode *);
  1404. struct inode *f2fs_iget(struct super_block *, unsigned long);
  1405. int try_to_free_nats(struct f2fs_sb_info *, int);
  1406. void update_inode(struct inode *, struct page *);
  1407. void update_inode_page(struct inode *);
  1408. int f2fs_write_inode(struct inode *, struct writeback_control *);
  1409. void f2fs_evict_inode(struct inode *);
  1410. void handle_failed_inode(struct inode *);
  1411. /*
  1412. * namei.c
  1413. */
  1414. struct dentry *f2fs_get_parent(struct dentry *child);
  1415. /*
  1416. * dir.c
  1417. */
  1418. extern unsigned char f2fs_filetype_table[F2FS_FT_MAX];
  1419. void set_de_type(struct f2fs_dir_entry *, umode_t);
  1420. struct f2fs_dir_entry *find_target_dentry(struct f2fs_filename *,
  1421. f2fs_hash_t, int *, struct f2fs_dentry_ptr *);
  1422. bool f2fs_fill_dentries(struct dir_context *, struct f2fs_dentry_ptr *,
  1423. unsigned int, struct f2fs_str *);
  1424. void do_make_empty_dir(struct inode *, struct inode *,
  1425. struct f2fs_dentry_ptr *);
  1426. struct page *init_inode_metadata(struct inode *, struct inode *,
  1427. const struct qstr *, struct page *);
  1428. void update_parent_metadata(struct inode *, struct inode *, unsigned int);
  1429. int room_for_filename(const void *, int, int);
  1430. void f2fs_drop_nlink(struct inode *, struct inode *, struct page *);
  1431. struct f2fs_dir_entry *f2fs_find_entry(struct inode *, struct qstr *,
  1432. struct page **);
  1433. struct f2fs_dir_entry *f2fs_parent_dir(struct inode *, struct page **);
  1434. ino_t f2fs_inode_by_name(struct inode *, struct qstr *);
  1435. void f2fs_set_link(struct inode *, struct f2fs_dir_entry *,
  1436. struct page *, struct inode *);
  1437. int update_dent_inode(struct inode *, struct inode *, const struct qstr *);
  1438. void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *,
  1439. const struct qstr *, f2fs_hash_t , unsigned int);
  1440. int __f2fs_add_link(struct inode *, const struct qstr *, struct inode *, nid_t,
  1441. umode_t);
  1442. void f2fs_delete_entry(struct f2fs_dir_entry *, struct page *, struct inode *,
  1443. struct inode *);
  1444. int f2fs_do_tmpfile(struct inode *, struct inode *);
  1445. bool f2fs_empty_dir(struct inode *);
  1446. static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
  1447. {
  1448. return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
  1449. inode, inode->i_ino, inode->i_mode);
  1450. }
  1451. /*
  1452. * super.c
  1453. */
  1454. int f2fs_commit_super(struct f2fs_sb_info *, bool);
  1455. int f2fs_sync_fs(struct super_block *, int);
  1456. extern __printf(3, 4)
  1457. void f2fs_msg(struct super_block *, const char *, const char *, ...);
  1458. /*
  1459. * hash.c
  1460. */
  1461. f2fs_hash_t f2fs_dentry_hash(const struct qstr *);
  1462. /*
  1463. * node.c
  1464. */
  1465. struct dnode_of_data;
  1466. struct node_info;
  1467. bool available_free_memory(struct f2fs_sb_info *, int);
  1468. int need_dentry_mark(struct f2fs_sb_info *, nid_t);
  1469. bool is_checkpointed_node(struct f2fs_sb_info *, nid_t);
  1470. bool need_inode_block_update(struct f2fs_sb_info *, nid_t);
  1471. void get_node_info(struct f2fs_sb_info *, nid_t, struct node_info *);
  1472. int get_dnode_of_data(struct dnode_of_data *, pgoff_t, int);
  1473. int truncate_inode_blocks(struct inode *, pgoff_t);
  1474. int truncate_xattr_node(struct inode *, struct page *);
  1475. int wait_on_node_pages_writeback(struct f2fs_sb_info *, nid_t);
  1476. int remove_inode_page(struct inode *);
  1477. struct page *new_inode_page(struct inode *);
  1478. struct page *new_node_page(struct dnode_of_data *, unsigned int, struct page *);
  1479. void ra_node_page(struct f2fs_sb_info *, nid_t);
  1480. struct page *get_node_page(struct f2fs_sb_info *, pgoff_t);
  1481. struct page *get_node_page_ra(struct page *, int);
  1482. void sync_inode_page(struct dnode_of_data *);
  1483. int sync_node_pages(struct f2fs_sb_info *, nid_t, struct writeback_control *);
  1484. bool alloc_nid(struct f2fs_sb_info *, nid_t *);
  1485. void alloc_nid_done(struct f2fs_sb_info *, nid_t);
  1486. void alloc_nid_failed(struct f2fs_sb_info *, nid_t);
  1487. int try_to_free_nids(struct f2fs_sb_info *, int);
  1488. void recover_inline_xattr(struct inode *, struct page *);
  1489. void recover_xattr_data(struct inode *, struct page *, block_t);
  1490. int recover_inode_page(struct f2fs_sb_info *, struct page *);
  1491. int restore_node_summary(struct f2fs_sb_info *, unsigned int,
  1492. struct f2fs_summary_block *);
  1493. void flush_nat_entries(struct f2fs_sb_info *);
  1494. int build_node_manager(struct f2fs_sb_info *);
  1495. void destroy_node_manager(struct f2fs_sb_info *);
  1496. int __init create_node_manager_caches(void);
  1497. void destroy_node_manager_caches(void);
  1498. /*
  1499. * segment.c
  1500. */
  1501. void register_inmem_page(struct inode *, struct page *);
  1502. int commit_inmem_pages(struct inode *, bool);
  1503. void f2fs_balance_fs(struct f2fs_sb_info *);
  1504. void f2fs_balance_fs_bg(struct f2fs_sb_info *);
  1505. int f2fs_issue_flush(struct f2fs_sb_info *);
  1506. int create_flush_cmd_control(struct f2fs_sb_info *);
  1507. void destroy_flush_cmd_control(struct f2fs_sb_info *);
  1508. void invalidate_blocks(struct f2fs_sb_info *, block_t);
  1509. bool is_checkpointed_data(struct f2fs_sb_info *, block_t);
  1510. void refresh_sit_entry(struct f2fs_sb_info *, block_t, block_t);
  1511. void clear_prefree_segments(struct f2fs_sb_info *, struct cp_control *);
  1512. void release_discard_addrs(struct f2fs_sb_info *);
  1513. bool discard_next_dnode(struct f2fs_sb_info *, block_t);
  1514. int npages_for_summary_flush(struct f2fs_sb_info *, bool);
  1515. void allocate_new_segments(struct f2fs_sb_info *);
  1516. int f2fs_trim_fs(struct f2fs_sb_info *, struct fstrim_range *);
  1517. struct page *get_sum_page(struct f2fs_sb_info *, unsigned int);
  1518. void update_meta_page(struct f2fs_sb_info *, void *, block_t);
  1519. void write_meta_page(struct f2fs_sb_info *, struct page *);
  1520. void write_node_page(unsigned int, struct f2fs_io_info *);
  1521. void write_data_page(struct dnode_of_data *, struct f2fs_io_info *);
  1522. void rewrite_data_page(struct f2fs_io_info *);
  1523. void f2fs_replace_block(struct f2fs_sb_info *, struct dnode_of_data *,
  1524. block_t, block_t, unsigned char, bool);
  1525. void allocate_data_block(struct f2fs_sb_info *, struct page *,
  1526. block_t, block_t *, struct f2fs_summary *, int);
  1527. void f2fs_wait_on_page_writeback(struct page *, enum page_type);
  1528. void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *, block_t);
  1529. void write_data_summaries(struct f2fs_sb_info *, block_t);
  1530. void write_node_summaries(struct f2fs_sb_info *, block_t);
  1531. int lookup_journal_in_cursum(struct f2fs_summary_block *,
  1532. int, unsigned int, int);
  1533. void flush_sit_entries(struct f2fs_sb_info *, struct cp_control *);
  1534. int build_segment_manager(struct f2fs_sb_info *);
  1535. void destroy_segment_manager(struct f2fs_sb_info *);
  1536. int __init create_segment_manager_caches(void);
  1537. void destroy_segment_manager_caches(void);
  1538. /*
  1539. * checkpoint.c
  1540. */
  1541. struct page *grab_meta_page(struct f2fs_sb_info *, pgoff_t);
  1542. struct page *get_meta_page(struct f2fs_sb_info *, pgoff_t);
  1543. struct page *get_tmp_page(struct f2fs_sb_info *, pgoff_t);
  1544. bool is_valid_blkaddr(struct f2fs_sb_info *, block_t, int);
  1545. int ra_meta_pages(struct f2fs_sb_info *, block_t, int, int, bool);
  1546. void ra_meta_pages_cond(struct f2fs_sb_info *, pgoff_t);
  1547. long sync_meta_pages(struct f2fs_sb_info *, enum page_type, long);
  1548. void add_dirty_inode(struct f2fs_sb_info *, nid_t, int type);
  1549. void remove_dirty_inode(struct f2fs_sb_info *, nid_t, int type);
  1550. void release_dirty_inode(struct f2fs_sb_info *);
  1551. bool exist_written_data(struct f2fs_sb_info *, nid_t, int);
  1552. int acquire_orphan_inode(struct f2fs_sb_info *);
  1553. void release_orphan_inode(struct f2fs_sb_info *);
  1554. void add_orphan_inode(struct f2fs_sb_info *, nid_t);
  1555. void remove_orphan_inode(struct f2fs_sb_info *, nid_t);
  1556. int recover_orphan_inodes(struct f2fs_sb_info *);
  1557. int get_valid_checkpoint(struct f2fs_sb_info *);
  1558. void update_dirty_page(struct inode *, struct page *);
  1559. void add_dirty_dir_inode(struct inode *);
  1560. void remove_dirty_dir_inode(struct inode *);
  1561. void sync_dirty_dir_inodes(struct f2fs_sb_info *);
  1562. void write_checkpoint(struct f2fs_sb_info *, struct cp_control *);
  1563. void init_ino_entry_info(struct f2fs_sb_info *);
  1564. int __init create_checkpoint_caches(void);
  1565. void destroy_checkpoint_caches(void);
  1566. /*
  1567. * data.c
  1568. */
  1569. void f2fs_submit_merged_bio(struct f2fs_sb_info *, enum page_type, int);
  1570. int f2fs_submit_page_bio(struct f2fs_io_info *);
  1571. void f2fs_submit_page_mbio(struct f2fs_io_info *);
  1572. void set_data_blkaddr(struct dnode_of_data *);
  1573. int reserve_new_block(struct dnode_of_data *);
  1574. int f2fs_get_block(struct dnode_of_data *, pgoff_t);
  1575. int f2fs_reserve_block(struct dnode_of_data *, pgoff_t);
  1576. struct page *get_read_data_page(struct inode *, pgoff_t, int, bool);
  1577. struct page *find_data_page(struct inode *, pgoff_t);
  1578. struct page *get_lock_data_page(struct inode *, pgoff_t, bool);
  1579. struct page *get_new_data_page(struct inode *, struct page *, pgoff_t, bool);
  1580. int do_write_data_page(struct f2fs_io_info *);
  1581. int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *, u64, u64);
  1582. void f2fs_invalidate_page(struct page *, unsigned int, unsigned int);
  1583. int f2fs_release_page(struct page *, gfp_t);
  1584. /*
  1585. * gc.c
  1586. */
  1587. int start_gc_thread(struct f2fs_sb_info *);
  1588. void stop_gc_thread(struct f2fs_sb_info *);
  1589. block_t start_bidx_of_node(unsigned int, struct f2fs_inode_info *);
  1590. int f2fs_gc(struct f2fs_sb_info *, bool);
  1591. void build_gc_manager(struct f2fs_sb_info *);
  1592. /*
  1593. * recovery.c
  1594. */
  1595. int recover_fsync_data(struct f2fs_sb_info *);
  1596. bool space_for_roll_forward(struct f2fs_sb_info *);
  1597. /*
  1598. * debug.c
  1599. */
  1600. #ifdef CONFIG_F2FS_STAT_FS
  1601. struct f2fs_stat_info {
  1602. struct list_head stat_list;
  1603. struct f2fs_sb_info *sbi;
  1604. int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
  1605. int main_area_segs, main_area_sections, main_area_zones;
  1606. unsigned long long hit_largest, hit_cached, hit_rbtree;
  1607. unsigned long long hit_total, total_ext;
  1608. int ext_tree, ext_node;
  1609. int ndirty_node, ndirty_dent, ndirty_dirs, ndirty_meta;
  1610. int nats, dirty_nats, sits, dirty_sits, fnids;
  1611. int total_count, utilization;
  1612. int bg_gc, inmem_pages, wb_pages;
  1613. int inline_xattr, inline_inode, inline_dir;
  1614. unsigned int valid_count, valid_node_count, valid_inode_count;
  1615. unsigned int bimodal, avg_vblocks;
  1616. int util_free, util_valid, util_invalid;
  1617. int rsvd_segs, overp_segs;
  1618. int dirty_count, node_pages, meta_pages;
  1619. int prefree_count, call_count, cp_count;
  1620. int tot_segs, node_segs, data_segs, free_segs, free_secs;
  1621. int bg_node_segs, bg_data_segs;
  1622. int tot_blks, data_blks, node_blks;
  1623. int bg_data_blks, bg_node_blks;
  1624. int curseg[NR_CURSEG_TYPE];
  1625. int cursec[NR_CURSEG_TYPE];
  1626. int curzone[NR_CURSEG_TYPE];
  1627. unsigned int segment_count[2];
  1628. unsigned int block_count[2];
  1629. unsigned int inplace_count;
  1630. unsigned long long base_mem, cache_mem, page_mem;
  1631. };
  1632. static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
  1633. {
  1634. return (struct f2fs_stat_info *)sbi->stat_info;
  1635. }
  1636. #define stat_inc_cp_count(si) ((si)->cp_count++)
  1637. #define stat_inc_call_count(si) ((si)->call_count++)
  1638. #define stat_inc_bggc_count(sbi) ((sbi)->bg_gc++)
  1639. #define stat_inc_dirty_dir(sbi) ((sbi)->n_dirty_dirs++)
  1640. #define stat_dec_dirty_dir(sbi) ((sbi)->n_dirty_dirs--)
  1641. #define stat_inc_total_hit(sbi) (atomic64_inc(&(sbi)->total_hit_ext))
  1642. #define stat_inc_rbtree_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_rbtree))
  1643. #define stat_inc_largest_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_largest))
  1644. #define stat_inc_cached_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_cached))
  1645. #define stat_inc_inline_xattr(inode) \
  1646. do { \
  1647. if (f2fs_has_inline_xattr(inode)) \
  1648. (atomic_inc(&F2FS_I_SB(inode)->inline_xattr)); \
  1649. } while (0)
  1650. #define stat_dec_inline_xattr(inode) \
  1651. do { \
  1652. if (f2fs_has_inline_xattr(inode)) \
  1653. (atomic_dec(&F2FS_I_SB(inode)->inline_xattr)); \
  1654. } while (0)
  1655. #define stat_inc_inline_inode(inode) \
  1656. do { \
  1657. if (f2fs_has_inline_data(inode)) \
  1658. (atomic_inc(&F2FS_I_SB(inode)->inline_inode)); \
  1659. } while (0)
  1660. #define stat_dec_inline_inode(inode) \
  1661. do { \
  1662. if (f2fs_has_inline_data(inode)) \
  1663. (atomic_dec(&F2FS_I_SB(inode)->inline_inode)); \
  1664. } while (0)
  1665. #define stat_inc_inline_dir(inode) \
  1666. do { \
  1667. if (f2fs_has_inline_dentry(inode)) \
  1668. (atomic_inc(&F2FS_I_SB(inode)->inline_dir)); \
  1669. } while (0)
  1670. #define stat_dec_inline_dir(inode) \
  1671. do { \
  1672. if (f2fs_has_inline_dentry(inode)) \
  1673. (atomic_dec(&F2FS_I_SB(inode)->inline_dir)); \
  1674. } while (0)
  1675. #define stat_inc_seg_type(sbi, curseg) \
  1676. ((sbi)->segment_count[(curseg)->alloc_type]++)
  1677. #define stat_inc_block_count(sbi, curseg) \
  1678. ((sbi)->block_count[(curseg)->alloc_type]++)
  1679. #define stat_inc_inplace_blocks(sbi) \
  1680. (atomic_inc(&(sbi)->inplace_count))
  1681. #define stat_inc_seg_count(sbi, type, gc_type) \
  1682. do { \
  1683. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  1684. (si)->tot_segs++; \
  1685. if (type == SUM_TYPE_DATA) { \
  1686. si->data_segs++; \
  1687. si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0; \
  1688. } else { \
  1689. si->node_segs++; \
  1690. si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0; \
  1691. } \
  1692. } while (0)
  1693. #define stat_inc_tot_blk_count(si, blks) \
  1694. (si->tot_blks += (blks))
  1695. #define stat_inc_data_blk_count(sbi, blks, gc_type) \
  1696. do { \
  1697. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  1698. stat_inc_tot_blk_count(si, blks); \
  1699. si->data_blks += (blks); \
  1700. si->bg_data_blks += (gc_type == BG_GC) ? (blks) : 0; \
  1701. } while (0)
  1702. #define stat_inc_node_blk_count(sbi, blks, gc_type) \
  1703. do { \
  1704. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  1705. stat_inc_tot_blk_count(si, blks); \
  1706. si->node_blks += (blks); \
  1707. si->bg_node_blks += (gc_type == BG_GC) ? (blks) : 0; \
  1708. } while (0)
  1709. int f2fs_build_stats(struct f2fs_sb_info *);
  1710. void f2fs_destroy_stats(struct f2fs_sb_info *);
  1711. void __init f2fs_create_root_stats(void);
  1712. void f2fs_destroy_root_stats(void);
  1713. #else
  1714. #define stat_inc_cp_count(si)
  1715. #define stat_inc_call_count(si)
  1716. #define stat_inc_bggc_count(si)
  1717. #define stat_inc_dirty_dir(sbi)
  1718. #define stat_dec_dirty_dir(sbi)
  1719. #define stat_inc_total_hit(sb)
  1720. #define stat_inc_rbtree_node_hit(sb)
  1721. #define stat_inc_largest_node_hit(sbi)
  1722. #define stat_inc_cached_node_hit(sbi)
  1723. #define stat_inc_inline_xattr(inode)
  1724. #define stat_dec_inline_xattr(inode)
  1725. #define stat_inc_inline_inode(inode)
  1726. #define stat_dec_inline_inode(inode)
  1727. #define stat_inc_inline_dir(inode)
  1728. #define stat_dec_inline_dir(inode)
  1729. #define stat_inc_seg_type(sbi, curseg)
  1730. #define stat_inc_block_count(sbi, curseg)
  1731. #define stat_inc_inplace_blocks(sbi)
  1732. #define stat_inc_seg_count(sbi, type, gc_type)
  1733. #define stat_inc_tot_blk_count(si, blks)
  1734. #define stat_inc_data_blk_count(sbi, blks, gc_type)
  1735. #define stat_inc_node_blk_count(sbi, blks, gc_type)
  1736. static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
  1737. static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
  1738. static inline void __init f2fs_create_root_stats(void) { }
  1739. static inline void f2fs_destroy_root_stats(void) { }
  1740. #endif
  1741. extern const struct file_operations f2fs_dir_operations;
  1742. extern const struct file_operations f2fs_file_operations;
  1743. extern const struct inode_operations f2fs_file_inode_operations;
  1744. extern const struct address_space_operations f2fs_dblock_aops;
  1745. extern const struct address_space_operations f2fs_node_aops;
  1746. extern const struct address_space_operations f2fs_meta_aops;
  1747. extern const struct inode_operations f2fs_dir_inode_operations;
  1748. extern const struct inode_operations f2fs_symlink_inode_operations;
  1749. extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
  1750. extern const struct inode_operations f2fs_special_inode_operations;
  1751. extern struct kmem_cache *inode_entry_slab;
  1752. /*
  1753. * inline.c
  1754. */
  1755. bool f2fs_may_inline_data(struct inode *);
  1756. bool f2fs_may_inline_dentry(struct inode *);
  1757. void read_inline_data(struct page *, struct page *);
  1758. bool truncate_inline_inode(struct page *, u64);
  1759. int f2fs_read_inline_data(struct inode *, struct page *);
  1760. int f2fs_convert_inline_page(struct dnode_of_data *, struct page *);
  1761. int f2fs_convert_inline_inode(struct inode *);
  1762. int f2fs_write_inline_data(struct inode *, struct page *);
  1763. bool recover_inline_data(struct inode *, struct page *);
  1764. struct f2fs_dir_entry *find_in_inline_dir(struct inode *,
  1765. struct f2fs_filename *, struct page **);
  1766. struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *, struct page **);
  1767. int make_empty_inline_dir(struct inode *inode, struct inode *, struct page *);
  1768. int f2fs_add_inline_entry(struct inode *, const struct qstr *, struct inode *,
  1769. nid_t, umode_t);
  1770. void f2fs_delete_inline_entry(struct f2fs_dir_entry *, struct page *,
  1771. struct inode *, struct inode *);
  1772. bool f2fs_empty_inline_dir(struct inode *);
  1773. int f2fs_read_inline_dir(struct file *, struct dir_context *,
  1774. struct f2fs_str *);
  1775. int f2fs_inline_data_fiemap(struct inode *,
  1776. struct fiemap_extent_info *, __u64, __u64);
  1777. /*
  1778. * shrinker.c
  1779. */
  1780. unsigned long f2fs_shrink_count(struct shrinker *, struct shrink_control *);
  1781. unsigned long f2fs_shrink_scan(struct shrinker *, struct shrink_control *);
  1782. void f2fs_join_shrinker(struct f2fs_sb_info *);
  1783. void f2fs_leave_shrinker(struct f2fs_sb_info *);
  1784. /*
  1785. * extent_cache.c
  1786. */
  1787. unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *, int);
  1788. void f2fs_drop_largest_extent(struct inode *, pgoff_t);
  1789. void f2fs_init_extent_tree(struct inode *, struct f2fs_extent *);
  1790. unsigned int f2fs_destroy_extent_node(struct inode *);
  1791. void f2fs_destroy_extent_tree(struct inode *);
  1792. bool f2fs_lookup_extent_cache(struct inode *, pgoff_t, struct extent_info *);
  1793. void f2fs_update_extent_cache(struct dnode_of_data *);
  1794. void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
  1795. pgoff_t, block_t, unsigned int);
  1796. void init_extent_cache_info(struct f2fs_sb_info *);
  1797. int __init create_extent_cache(void);
  1798. void destroy_extent_cache(void);
  1799. /*
  1800. * crypto support
  1801. */
  1802. static inline int f2fs_encrypted_inode(struct inode *inode)
  1803. {
  1804. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1805. return file_is_encrypt(inode);
  1806. #else
  1807. return 0;
  1808. #endif
  1809. }
  1810. static inline void f2fs_set_encrypted_inode(struct inode *inode)
  1811. {
  1812. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1813. file_set_encrypt(inode);
  1814. #endif
  1815. }
  1816. static inline bool f2fs_bio_encrypted(struct bio *bio)
  1817. {
  1818. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1819. return unlikely(bio->bi_private != NULL);
  1820. #else
  1821. return false;
  1822. #endif
  1823. }
  1824. static inline int f2fs_sb_has_crypto(struct super_block *sb)
  1825. {
  1826. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1827. return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_ENCRYPT);
  1828. #else
  1829. return 0;
  1830. #endif
  1831. }
  1832. static inline bool f2fs_may_encrypt(struct inode *inode)
  1833. {
  1834. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1835. mode_t mode = inode->i_mode;
  1836. return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
  1837. #else
  1838. return 0;
  1839. #endif
  1840. }
  1841. /* crypto_policy.c */
  1842. int f2fs_is_child_context_consistent_with_parent(struct inode *,
  1843. struct inode *);
  1844. int f2fs_inherit_context(struct inode *, struct inode *, struct page *);
  1845. int f2fs_process_policy(const struct f2fs_encryption_policy *, struct inode *);
  1846. int f2fs_get_policy(struct inode *, struct f2fs_encryption_policy *);
  1847. /* crypt.c */
  1848. extern struct kmem_cache *f2fs_crypt_info_cachep;
  1849. bool f2fs_valid_contents_enc_mode(uint32_t);
  1850. uint32_t f2fs_validate_encryption_key_size(uint32_t, uint32_t);
  1851. struct f2fs_crypto_ctx *f2fs_get_crypto_ctx(struct inode *);
  1852. void f2fs_release_crypto_ctx(struct f2fs_crypto_ctx *);
  1853. struct page *f2fs_encrypt(struct inode *, struct page *);
  1854. int f2fs_decrypt(struct f2fs_crypto_ctx *, struct page *);
  1855. int f2fs_decrypt_one(struct inode *, struct page *);
  1856. void f2fs_end_io_crypto_work(struct f2fs_crypto_ctx *, struct bio *);
  1857. /* crypto_key.c */
  1858. void f2fs_free_encryption_info(struct inode *, struct f2fs_crypt_info *);
  1859. int _f2fs_get_encryption_info(struct inode *inode);
  1860. /* crypto_fname.c */
  1861. bool f2fs_valid_filenames_enc_mode(uint32_t);
  1862. u32 f2fs_fname_crypto_round_up(u32, u32);
  1863. int f2fs_fname_crypto_alloc_buffer(struct inode *, u32, struct f2fs_str *);
  1864. int f2fs_fname_disk_to_usr(struct inode *, f2fs_hash_t *,
  1865. const struct f2fs_str *, struct f2fs_str *);
  1866. int f2fs_fname_usr_to_disk(struct inode *, const struct qstr *,
  1867. struct f2fs_str *);
  1868. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  1869. void f2fs_restore_and_release_control_page(struct page **);
  1870. void f2fs_restore_control_page(struct page *);
  1871. int __init f2fs_init_crypto(void);
  1872. int f2fs_crypto_initialize(void);
  1873. void f2fs_exit_crypto(void);
  1874. int f2fs_has_encryption_key(struct inode *);
  1875. static inline int f2fs_get_encryption_info(struct inode *inode)
  1876. {
  1877. struct f2fs_crypt_info *ci = F2FS_I(inode)->i_crypt_info;
  1878. if (!ci ||
  1879. (ci->ci_keyring_key &&
  1880. (ci->ci_keyring_key->flags & ((1 << KEY_FLAG_INVALIDATED) |
  1881. (1 << KEY_FLAG_REVOKED) |
  1882. (1 << KEY_FLAG_DEAD)))))
  1883. return _f2fs_get_encryption_info(inode);
  1884. return 0;
  1885. }
  1886. void f2fs_fname_crypto_free_buffer(struct f2fs_str *);
  1887. int f2fs_fname_setup_filename(struct inode *, const struct qstr *,
  1888. int lookup, struct f2fs_filename *);
  1889. void f2fs_fname_free_filename(struct f2fs_filename *);
  1890. #else
  1891. static inline void f2fs_restore_and_release_control_page(struct page **p) { }
  1892. static inline void f2fs_restore_control_page(struct page *p) { }
  1893. static inline int __init f2fs_init_crypto(void) { return 0; }
  1894. static inline void f2fs_exit_crypto(void) { }
  1895. static inline int f2fs_has_encryption_key(struct inode *i) { return 0; }
  1896. static inline int f2fs_get_encryption_info(struct inode *i) { return 0; }
  1897. static inline void f2fs_fname_crypto_free_buffer(struct f2fs_str *p) { }
  1898. static inline int f2fs_fname_setup_filename(struct inode *dir,
  1899. const struct qstr *iname,
  1900. int lookup, struct f2fs_filename *fname)
  1901. {
  1902. memset(fname, 0, sizeof(struct f2fs_filename));
  1903. fname->usr_fname = iname;
  1904. fname->disk_name.name = (unsigned char *)iname->name;
  1905. fname->disk_name.len = iname->len;
  1906. return 0;
  1907. }
  1908. static inline void f2fs_fname_free_filename(struct f2fs_filename *fname) { }
  1909. #endif
  1910. #endif