f2fs.h 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579
  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. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  14. #undef CONFIG_F2FS_FS_ENCRYPTION
  15. #endif
  16. #include <linux/types.h>
  17. #include <linux/page-flags.h>
  18. #include <linux/buffer_head.h>
  19. #include <linux/slab.h>
  20. #include <linux/crc32.h>
  21. #include <linux/magic.h>
  22. #include <linux/kobject.h>
  23. #include <linux/sched.h>
  24. #include <linux/vmalloc.h>
  25. #include <linux/bio.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/fscrypto.h>
  28. #ifdef CONFIG_F2FS_CHECK_FS
  29. #define f2fs_bug_on(sbi, condition) BUG_ON(condition)
  30. #define f2fs_down_write(x, y) down_write(x)
  31. #else
  32. #define f2fs_bug_on(sbi, condition) \
  33. do { \
  34. if (unlikely(condition)) { \
  35. WARN_ON(1); \
  36. set_sbi_flag(sbi, SBI_NEED_FSCK); \
  37. } \
  38. } while (0)
  39. #endif
  40. #ifdef CONFIG_F2FS_FAULT_INJECTION
  41. enum {
  42. FAULT_KMALLOC,
  43. FAULT_PAGE_ALLOC,
  44. FAULT_ALLOC_NID,
  45. FAULT_ORPHAN,
  46. FAULT_BLOCK,
  47. FAULT_DIR_DEPTH,
  48. FAULT_EVICT_INODE,
  49. FAULT_IO,
  50. FAULT_CHECKPOINT,
  51. FAULT_MAX,
  52. };
  53. struct f2fs_fault_info {
  54. atomic_t inject_ops;
  55. unsigned int inject_rate;
  56. unsigned int inject_type;
  57. };
  58. extern char *fault_name[FAULT_MAX];
  59. #define IS_FAULT_SET(fi, type) (fi->inject_type & (1 << (type)))
  60. #endif
  61. /*
  62. * For mount options
  63. */
  64. #define F2FS_SUPER_MAGIC 0xF2F52010 /* F2FS Magic Number */
  65. #define F2FS_MOUNT_BG_GC 0x00000001
  66. #define F2FS_MOUNT_DISABLE_ROLL_FORWARD 0x00000002
  67. #define F2FS_MOUNT_DISCARD 0x00000004
  68. #define F2FS_MOUNT_NOHEAP 0x00000008
  69. #define F2FS_MOUNT_XATTR_USER 0x00000010
  70. #define F2FS_MOUNT_POSIX_ACL 0x00000020
  71. #define F2FS_MOUNT_DISABLE_EXT_IDENTIFY 0x00000040
  72. #define F2FS_MOUNT_INLINE_XATTR 0x00000080
  73. #define F2FS_MOUNT_INLINE_DATA 0x00000100
  74. #define F2FS_MOUNT_INLINE_DENTRY 0x00000200
  75. #define F2FS_MOUNT_FLUSH_MERGE 0x00000400
  76. #define F2FS_MOUNT_NOBARRIER 0x00000800
  77. #define F2FS_MOUNT_FASTBOOT 0x00001000
  78. #define F2FS_MOUNT_EXTENT_CACHE 0x00002000
  79. #define F2FS_MOUNT_FORCE_FG_GC 0x00004000
  80. #define F2FS_MOUNT_DATA_FLUSH 0x00008000
  81. #define F2FS_MOUNT_FAULT_INJECTION 0x00010000
  82. #define F2FS_MOUNT_ADAPTIVE 0x00020000
  83. #define F2FS_MOUNT_LFS 0x00040000
  84. #define clear_opt(sbi, option) (sbi->mount_opt.opt &= ~F2FS_MOUNT_##option)
  85. #define set_opt(sbi, option) (sbi->mount_opt.opt |= F2FS_MOUNT_##option)
  86. #define test_opt(sbi, option) (sbi->mount_opt.opt & F2FS_MOUNT_##option)
  87. #define ver_after(a, b) (typecheck(unsigned long long, a) && \
  88. typecheck(unsigned long long, b) && \
  89. ((long long)((a) - (b)) > 0))
  90. typedef u32 block_t; /*
  91. * should not change u32, since it is the on-disk block
  92. * address format, __le32.
  93. */
  94. typedef u32 nid_t;
  95. struct f2fs_mount_info {
  96. unsigned int opt;
  97. };
  98. #define F2FS_FEATURE_ENCRYPT 0x0001
  99. #define F2FS_FEATURE_BLKZONED 0x0002
  100. #define F2FS_HAS_FEATURE(sb, mask) \
  101. ((F2FS_SB(sb)->raw_super->feature & cpu_to_le32(mask)) != 0)
  102. #define F2FS_SET_FEATURE(sb, mask) \
  103. F2FS_SB(sb)->raw_super->feature |= cpu_to_le32(mask)
  104. #define F2FS_CLEAR_FEATURE(sb, mask) \
  105. F2FS_SB(sb)->raw_super->feature &= ~cpu_to_le32(mask)
  106. #define CRCPOLY_LE 0xedb88320
  107. static inline __u32 f2fs_crc32(void *buf, size_t len)
  108. {
  109. unsigned char *p = (unsigned char *)buf;
  110. __u32 crc = F2FS_SUPER_MAGIC;
  111. int i;
  112. while (len--) {
  113. crc ^= *p++;
  114. for (i = 0; i < 8; i++)
  115. crc = (crc >> 1) ^ ((crc & 1) ? CRCPOLY_LE : 0);
  116. }
  117. return crc;
  118. }
  119. static inline bool f2fs_crc_valid(__u32 blk_crc, void *buf, size_t buf_size)
  120. {
  121. return f2fs_crc32(buf, buf_size) == blk_crc;
  122. }
  123. static inline void inode_lock(struct inode *inode)
  124. {
  125. mutex_lock(&inode->i_mutex);
  126. }
  127. static inline void inode_unlock(struct inode *inode)
  128. {
  129. mutex_unlock(&inode->i_mutex);
  130. }
  131. /**
  132. * wq_has_sleeper - check if there are any waiting processes
  133. * @wq: wait queue head
  134. *
  135. * Returns true if wq has waiting processes
  136. *
  137. * Please refer to the comment for waitqueue_active.
  138. */
  139. static inline bool wq_has_sleeper(wait_queue_head_t *wq)
  140. {
  141. /*
  142. * We need to be sure we are in sync with the
  143. * add_wait_queue modifications to the wait queue.
  144. *
  145. * This memory barrier should be paired with one on the
  146. * waiting side.
  147. */
  148. smp_mb();
  149. return waitqueue_active(wq);
  150. }
  151. static inline struct inode *d_inode(const struct dentry *dentry)
  152. {
  153. return dentry->d_inode;
  154. }
  155. static inline struct dentry *file_dentry(const struct file *file)
  156. {
  157. return file->f_path.dentry;
  158. }
  159. static inline void inode_nohighmem(struct inode *inode)
  160. {
  161. mapping_set_gfp_mask(inode->i_mapping, GFP_USER);
  162. }
  163. /**
  164. * current_time - Return FS time
  165. * @inode: inode.
  166. *
  167. * Return the current time truncated to the time granularity supported by
  168. * the fs.
  169. *
  170. * Note that inode and inode->sb cannot be NULL.
  171. * Otherwise, the function warns and returns time without truncation.
  172. */
  173. static inline struct timespec current_time(struct inode *inode)
  174. {
  175. struct timespec now = current_kernel_time();
  176. if (unlikely(!inode->i_sb)) {
  177. WARN(1, "current_time() called with uninitialized super_block in the inode");
  178. return now;
  179. }
  180. return timespec_trunc(now, inode->i_sb->s_time_gran);
  181. }
  182. /*
  183. * For checkpoint manager
  184. */
  185. enum {
  186. NAT_BITMAP,
  187. SIT_BITMAP
  188. };
  189. enum {
  190. CP_UMOUNT,
  191. CP_FASTBOOT,
  192. CP_SYNC,
  193. CP_RECOVERY,
  194. CP_DISCARD,
  195. };
  196. #define DEF_BATCHED_TRIM_SECTIONS 2
  197. #define BATCHED_TRIM_SEGMENTS(sbi) \
  198. (SM_I(sbi)->trim_sections * (sbi)->segs_per_sec)
  199. #define BATCHED_TRIM_BLOCKS(sbi) \
  200. (BATCHED_TRIM_SEGMENTS(sbi) << (sbi)->log_blocks_per_seg)
  201. #define DEF_CP_INTERVAL 60 /* 60 secs */
  202. #define DEF_IDLE_INTERVAL 120 /* 2 mins */
  203. struct cp_control {
  204. int reason;
  205. __u64 trim_start;
  206. __u64 trim_end;
  207. __u64 trim_minlen;
  208. __u64 trimmed;
  209. };
  210. /*
  211. * For CP/NAT/SIT/SSA readahead
  212. */
  213. enum {
  214. META_CP,
  215. META_NAT,
  216. META_SIT,
  217. META_SSA,
  218. META_POR,
  219. };
  220. /* for the list of ino */
  221. enum {
  222. ORPHAN_INO, /* for orphan ino list */
  223. APPEND_INO, /* for append ino list */
  224. UPDATE_INO, /* for update ino list */
  225. MAX_INO_ENTRY, /* max. list */
  226. };
  227. struct ino_entry {
  228. struct list_head list; /* list head */
  229. nid_t ino; /* inode number */
  230. };
  231. /* for the list of inodes to be GCed */
  232. struct inode_entry {
  233. struct list_head list; /* list head */
  234. struct inode *inode; /* vfs inode pointer */
  235. };
  236. /* for the list of blockaddresses to be discarded */
  237. struct discard_entry {
  238. struct list_head list; /* list head */
  239. block_t blkaddr; /* block address to be discarded */
  240. int len; /* # of consecutive blocks of the discard */
  241. };
  242. /* for the list of fsync inodes, used only during recovery */
  243. struct fsync_inode_entry {
  244. struct list_head list; /* list head */
  245. struct inode *inode; /* vfs inode pointer */
  246. block_t blkaddr; /* block address locating the last fsync */
  247. block_t last_dentry; /* block address locating the last dentry */
  248. };
  249. #define nats_in_cursum(jnl) (le16_to_cpu(jnl->n_nats))
  250. #define sits_in_cursum(jnl) (le16_to_cpu(jnl->n_sits))
  251. #define nat_in_journal(jnl, i) (jnl->nat_j.entries[i].ne)
  252. #define nid_in_journal(jnl, i) (jnl->nat_j.entries[i].nid)
  253. #define sit_in_journal(jnl, i) (jnl->sit_j.entries[i].se)
  254. #define segno_in_journal(jnl, i) (jnl->sit_j.entries[i].segno)
  255. #define MAX_NAT_JENTRIES(jnl) (NAT_JOURNAL_ENTRIES - nats_in_cursum(jnl))
  256. #define MAX_SIT_JENTRIES(jnl) (SIT_JOURNAL_ENTRIES - sits_in_cursum(jnl))
  257. static inline int update_nats_in_cursum(struct f2fs_journal *journal, int i)
  258. {
  259. int before = nats_in_cursum(journal);
  260. journal->n_nats = cpu_to_le16(before + i);
  261. return before;
  262. }
  263. static inline int update_sits_in_cursum(struct f2fs_journal *journal, int i)
  264. {
  265. int before = sits_in_cursum(journal);
  266. journal->n_sits = cpu_to_le16(before + i);
  267. return before;
  268. }
  269. static inline bool __has_cursum_space(struct f2fs_journal *journal,
  270. int size, int type)
  271. {
  272. if (type == NAT_JOURNAL)
  273. return size <= MAX_NAT_JENTRIES(journal);
  274. return size <= MAX_SIT_JENTRIES(journal);
  275. }
  276. /*
  277. * ioctl commands
  278. */
  279. #define F2FS_IOC_GETFLAGS FS_IOC_GETFLAGS
  280. #define F2FS_IOC_SETFLAGS FS_IOC_SETFLAGS
  281. #define F2FS_IOC_GETVERSION FS_IOC_GETVERSION
  282. #define F2FS_IOCTL_MAGIC 0xf5
  283. #define F2FS_IOC_START_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 1)
  284. #define F2FS_IOC_COMMIT_ATOMIC_WRITE _IO(F2FS_IOCTL_MAGIC, 2)
  285. #define F2FS_IOC_START_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 3)
  286. #define F2FS_IOC_RELEASE_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 4)
  287. #define F2FS_IOC_ABORT_VOLATILE_WRITE _IO(F2FS_IOCTL_MAGIC, 5)
  288. #define F2FS_IOC_GARBAGE_COLLECT _IO(F2FS_IOCTL_MAGIC, 6)
  289. #define F2FS_IOC_WRITE_CHECKPOINT _IO(F2FS_IOCTL_MAGIC, 7)
  290. #define F2FS_IOC_DEFRAGMENT _IO(F2FS_IOCTL_MAGIC, 8)
  291. #define F2FS_IOC_SET_ENCRYPTION_POLICY FS_IOC_SET_ENCRYPTION_POLICY
  292. #define F2FS_IOC_GET_ENCRYPTION_POLICY FS_IOC_GET_ENCRYPTION_POLICY
  293. #define F2FS_IOC_GET_ENCRYPTION_PWSALT FS_IOC_GET_ENCRYPTION_PWSALT
  294. /*
  295. * should be same as XFS_IOC_GOINGDOWN.
  296. * Flags for going down operation used by FS_IOC_GOINGDOWN
  297. */
  298. #define F2FS_IOC_SHUTDOWN _IOR('X', 125, __u32) /* Shutdown */
  299. #define F2FS_GOING_DOWN_FULLSYNC 0x0 /* going down with full sync */
  300. #define F2FS_GOING_DOWN_METASYNC 0x1 /* going down with metadata */
  301. #define F2FS_GOING_DOWN_NOSYNC 0x2 /* going down */
  302. #define F2FS_GOING_DOWN_METAFLUSH 0x3 /* going down with meta flush */
  303. #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
  304. /*
  305. * ioctl commands in 32 bit emulation
  306. */
  307. #define F2FS_IOC32_GETFLAGS FS_IOC32_GETFLAGS
  308. #define F2FS_IOC32_SETFLAGS FS_IOC32_SETFLAGS
  309. #define F2FS_IOC32_GETVERSION FS_IOC32_GETVERSION
  310. #endif
  311. struct f2fs_defragment {
  312. u64 start;
  313. u64 len;
  314. };
  315. /*
  316. * For INODE and NODE manager
  317. */
  318. /* for directory operations */
  319. struct f2fs_dentry_ptr {
  320. struct inode *inode;
  321. const void *bitmap;
  322. struct f2fs_dir_entry *dentry;
  323. __u8 (*filename)[F2FS_SLOT_LEN];
  324. int max;
  325. };
  326. static inline void make_dentry_ptr(struct inode *inode,
  327. struct f2fs_dentry_ptr *d, void *src, int type)
  328. {
  329. d->inode = inode;
  330. if (type == 1) {
  331. struct f2fs_dentry_block *t = (struct f2fs_dentry_block *)src;
  332. d->max = NR_DENTRY_IN_BLOCK;
  333. d->bitmap = &t->dentry_bitmap;
  334. d->dentry = t->dentry;
  335. d->filename = t->filename;
  336. } else {
  337. struct f2fs_inline_dentry *t = (struct f2fs_inline_dentry *)src;
  338. d->max = NR_INLINE_DENTRY;
  339. d->bitmap = &t->dentry_bitmap;
  340. d->dentry = t->dentry;
  341. d->filename = t->filename;
  342. }
  343. }
  344. /*
  345. * XATTR_NODE_OFFSET stores xattrs to one node block per file keeping -1
  346. * as its node offset to distinguish from index node blocks.
  347. * But some bits are used to mark the node block.
  348. */
  349. #define XATTR_NODE_OFFSET ((((unsigned int)-1) << OFFSET_BIT_SHIFT) \
  350. >> OFFSET_BIT_SHIFT)
  351. enum {
  352. ALLOC_NODE, /* allocate a new node page if needed */
  353. LOOKUP_NODE, /* look up a node without readahead */
  354. LOOKUP_NODE_RA, /*
  355. * look up a node with readahead called
  356. * by get_data_block.
  357. */
  358. };
  359. #define F2FS_LINK_MAX 0xffffffff /* maximum link count per file */
  360. #define MAX_DIR_RA_PAGES 4 /* maximum ra pages of dir */
  361. /* vector size for gang look-up from extent cache that consists of radix tree */
  362. #define EXT_TREE_VEC_SIZE 64
  363. /* for in-memory extent cache entry */
  364. #define F2FS_MIN_EXTENT_LEN 64 /* minimum extent length */
  365. /* number of extent info in extent cache we try to shrink */
  366. #define EXTENT_CACHE_SHRINK_NUMBER 128
  367. struct extent_info {
  368. unsigned int fofs; /* start offset in a file */
  369. u32 blk; /* start block address of the extent */
  370. unsigned int len; /* length of the extent */
  371. };
  372. struct extent_node {
  373. struct rb_node rb_node; /* rb node located in rb-tree */
  374. struct list_head list; /* node in global extent list of sbi */
  375. struct extent_info ei; /* extent info */
  376. struct extent_tree *et; /* extent tree pointer */
  377. };
  378. struct extent_tree {
  379. nid_t ino; /* inode number */
  380. struct rb_root root; /* root of extent info rb-tree */
  381. struct extent_node *cached_en; /* recently accessed extent node */
  382. struct extent_info largest; /* largested extent info */
  383. struct list_head list; /* to be used by sbi->zombie_list */
  384. rwlock_t lock; /* protect extent info rb-tree */
  385. atomic_t node_cnt; /* # of extent node in rb-tree*/
  386. };
  387. /*
  388. * This structure is taken from ext4_map_blocks.
  389. *
  390. * Note that, however, f2fs uses NEW and MAPPED flags for f2fs_map_blocks().
  391. */
  392. #define F2FS_MAP_NEW (1 << BH_New)
  393. #define F2FS_MAP_MAPPED (1 << BH_Mapped)
  394. #define F2FS_MAP_UNWRITTEN (1 << BH_Unwritten)
  395. #define F2FS_MAP_FLAGS (F2FS_MAP_NEW | F2FS_MAP_MAPPED |\
  396. F2FS_MAP_UNWRITTEN)
  397. struct f2fs_map_blocks {
  398. block_t m_pblk;
  399. block_t m_lblk;
  400. unsigned int m_len;
  401. unsigned int m_flags;
  402. pgoff_t *m_next_pgofs; /* point next possible non-hole pgofs */
  403. };
  404. /* for flag in get_data_block */
  405. #define F2FS_GET_BLOCK_READ 0
  406. #define F2FS_GET_BLOCK_DIO 1
  407. #define F2FS_GET_BLOCK_FIEMAP 2
  408. #define F2FS_GET_BLOCK_BMAP 3
  409. #define F2FS_GET_BLOCK_PRE_DIO 4
  410. #define F2FS_GET_BLOCK_PRE_AIO 5
  411. /*
  412. * i_advise uses FADVISE_XXX_BIT. We can add additional hints later.
  413. */
  414. #define FADVISE_COLD_BIT 0x01
  415. #define FADVISE_LOST_PINO_BIT 0x02
  416. #define FADVISE_ENCRYPT_BIT 0x04
  417. #define FADVISE_ENC_NAME_BIT 0x08
  418. #define FADVISE_KEEP_SIZE_BIT 0x10
  419. #define file_is_cold(inode) is_file(inode, FADVISE_COLD_BIT)
  420. #define file_wrong_pino(inode) is_file(inode, FADVISE_LOST_PINO_BIT)
  421. #define file_set_cold(inode) set_file(inode, FADVISE_COLD_BIT)
  422. #define file_lost_pino(inode) set_file(inode, FADVISE_LOST_PINO_BIT)
  423. #define file_clear_cold(inode) clear_file(inode, FADVISE_COLD_BIT)
  424. #define file_got_pino(inode) clear_file(inode, FADVISE_LOST_PINO_BIT)
  425. #define file_is_encrypt(inode) is_file(inode, FADVISE_ENCRYPT_BIT)
  426. #define file_set_encrypt(inode) set_file(inode, FADVISE_ENCRYPT_BIT)
  427. #define file_clear_encrypt(inode) clear_file(inode, FADVISE_ENCRYPT_BIT)
  428. #define file_enc_name(inode) is_file(inode, FADVISE_ENC_NAME_BIT)
  429. #define file_set_enc_name(inode) set_file(inode, FADVISE_ENC_NAME_BIT)
  430. #define file_keep_isize(inode) is_file(inode, FADVISE_KEEP_SIZE_BIT)
  431. #define file_set_keep_isize(inode) set_file(inode, FADVISE_KEEP_SIZE_BIT)
  432. #define DEF_DIR_LEVEL 0
  433. struct f2fs_inode_info {
  434. struct inode vfs_inode; /* serve a vfs inode */
  435. unsigned long i_flags; /* keep an inode flags for ioctl */
  436. unsigned char i_advise; /* use to give file attribute hints */
  437. unsigned char i_dir_level; /* use for dentry level for large dir */
  438. unsigned int i_current_depth; /* use only in directory structure */
  439. unsigned int i_pino; /* parent inode number */
  440. umode_t i_acl_mode; /* keep file acl mode temporarily */
  441. /* Use below internally in f2fs*/
  442. unsigned long flags; /* use to pass per-file flags */
  443. struct rw_semaphore i_sem; /* protect fi info */
  444. struct percpu_counter dirty_pages; /* # of dirty pages */
  445. f2fs_hash_t chash; /* hash value of given file name */
  446. unsigned int clevel; /* maximum level of given file name */
  447. nid_t i_xattr_nid; /* node id that contains xattrs */
  448. unsigned long long xattr_ver; /* cp version of xattr modification */
  449. loff_t last_disk_size; /* lastly written file size */
  450. struct list_head dirty_list; /* dirty list for dirs and files */
  451. struct list_head gdirty_list; /* linked in global dirty list */
  452. struct list_head inmem_pages; /* inmemory pages managed by f2fs */
  453. struct mutex inmem_lock; /* lock for inmemory pages */
  454. struct extent_tree *extent_tree; /* cached extent_tree entry */
  455. struct rw_semaphore dio_rwsem[2];/* avoid racing between dio and gc */
  456. };
  457. static inline void get_extent_info(struct extent_info *ext,
  458. struct f2fs_extent *i_ext)
  459. {
  460. ext->fofs = le32_to_cpu(i_ext->fofs);
  461. ext->blk = le32_to_cpu(i_ext->blk);
  462. ext->len = le32_to_cpu(i_ext->len);
  463. }
  464. static inline void set_raw_extent(struct extent_info *ext,
  465. struct f2fs_extent *i_ext)
  466. {
  467. i_ext->fofs = cpu_to_le32(ext->fofs);
  468. i_ext->blk = cpu_to_le32(ext->blk);
  469. i_ext->len = cpu_to_le32(ext->len);
  470. }
  471. static inline void set_extent_info(struct extent_info *ei, unsigned int fofs,
  472. u32 blk, unsigned int len)
  473. {
  474. ei->fofs = fofs;
  475. ei->blk = blk;
  476. ei->len = len;
  477. }
  478. static inline bool __is_extent_same(struct extent_info *ei1,
  479. struct extent_info *ei2)
  480. {
  481. return (ei1->fofs == ei2->fofs && ei1->blk == ei2->blk &&
  482. ei1->len == ei2->len);
  483. }
  484. static inline bool __is_extent_mergeable(struct extent_info *back,
  485. struct extent_info *front)
  486. {
  487. return (back->fofs + back->len == front->fofs &&
  488. back->blk + back->len == front->blk);
  489. }
  490. static inline bool __is_back_mergeable(struct extent_info *cur,
  491. struct extent_info *back)
  492. {
  493. return __is_extent_mergeable(back, cur);
  494. }
  495. static inline bool __is_front_mergeable(struct extent_info *cur,
  496. struct extent_info *front)
  497. {
  498. return __is_extent_mergeable(cur, front);
  499. }
  500. extern void f2fs_mark_inode_dirty_sync(struct inode *, bool);
  501. static inline void __try_update_largest_extent(struct inode *inode,
  502. struct extent_tree *et, struct extent_node *en)
  503. {
  504. if (en->ei.len > et->largest.len) {
  505. et->largest = en->ei;
  506. f2fs_mark_inode_dirty_sync(inode, true);
  507. }
  508. }
  509. enum nid_list {
  510. FREE_NID_LIST,
  511. ALLOC_NID_LIST,
  512. MAX_NID_LIST,
  513. };
  514. struct f2fs_nm_info {
  515. block_t nat_blkaddr; /* base disk address of NAT */
  516. nid_t max_nid; /* maximum possible node ids */
  517. nid_t available_nids; /* # of available node ids */
  518. nid_t next_scan_nid; /* the next nid to be scanned */
  519. unsigned int ram_thresh; /* control the memory footprint */
  520. unsigned int ra_nid_pages; /* # of nid pages to be readaheaded */
  521. unsigned int dirty_nats_ratio; /* control dirty nats ratio threshold */
  522. /* NAT cache management */
  523. struct radix_tree_root nat_root;/* root of the nat entry cache */
  524. struct radix_tree_root nat_set_root;/* root of the nat set cache */
  525. struct rw_semaphore nat_tree_lock; /* protect nat_tree_lock */
  526. struct list_head nat_entries; /* cached nat entry list (clean) */
  527. unsigned int nat_cnt; /* the # of cached nat entries */
  528. unsigned int dirty_nat_cnt; /* total num of nat entries in set */
  529. /* free node ids management */
  530. struct radix_tree_root free_nid_root;/* root of the free_nid cache */
  531. struct list_head nid_list[MAX_NID_LIST];/* lists for free nids */
  532. unsigned int nid_cnt[MAX_NID_LIST]; /* the number of free node id */
  533. spinlock_t nid_list_lock; /* protect nid lists ops */
  534. struct mutex build_lock; /* lock for build free nids */
  535. /* for checkpoint */
  536. char *nat_bitmap; /* NAT bitmap pointer */
  537. int bitmap_size; /* bitmap size */
  538. };
  539. /*
  540. * this structure is used as one of function parameters.
  541. * all the information are dedicated to a given direct node block determined
  542. * by the data offset in a file.
  543. */
  544. struct dnode_of_data {
  545. struct inode *inode; /* vfs inode pointer */
  546. struct page *inode_page; /* its inode page, NULL is possible */
  547. struct page *node_page; /* cached direct node page */
  548. nid_t nid; /* node id of the direct node block */
  549. unsigned int ofs_in_node; /* data offset in the node page */
  550. bool inode_page_locked; /* inode page is locked or not */
  551. bool node_changed; /* is node block changed */
  552. char cur_level; /* level of hole node page */
  553. char max_level; /* level of current page located */
  554. block_t data_blkaddr; /* block address of the node block */
  555. };
  556. static inline void set_new_dnode(struct dnode_of_data *dn, struct inode *inode,
  557. struct page *ipage, struct page *npage, nid_t nid)
  558. {
  559. memset(dn, 0, sizeof(*dn));
  560. dn->inode = inode;
  561. dn->inode_page = ipage;
  562. dn->node_page = npage;
  563. dn->nid = nid;
  564. }
  565. /*
  566. * For SIT manager
  567. *
  568. * By default, there are 6 active log areas across the whole main area.
  569. * When considering hot and cold data separation to reduce cleaning overhead,
  570. * we split 3 for data logs and 3 for node logs as hot, warm, and cold types,
  571. * respectively.
  572. * In the current design, you should not change the numbers intentionally.
  573. * Instead, as a mount option such as active_logs=x, you can use 2, 4, and 6
  574. * logs individually according to the underlying devices. (default: 6)
  575. * Just in case, on-disk layout covers maximum 16 logs that consist of 8 for
  576. * data and 8 for node logs.
  577. */
  578. #define NR_CURSEG_DATA_TYPE (3)
  579. #define NR_CURSEG_NODE_TYPE (3)
  580. #define NR_CURSEG_TYPE (NR_CURSEG_DATA_TYPE + NR_CURSEG_NODE_TYPE)
  581. enum {
  582. CURSEG_HOT_DATA = 0, /* directory entry blocks */
  583. CURSEG_WARM_DATA, /* data blocks */
  584. CURSEG_COLD_DATA, /* multimedia or GCed data blocks */
  585. CURSEG_HOT_NODE, /* direct node blocks of directory files */
  586. CURSEG_WARM_NODE, /* direct node blocks of normal files */
  587. CURSEG_COLD_NODE, /* indirect node blocks */
  588. NO_CHECK_TYPE,
  589. };
  590. struct flush_cmd {
  591. struct completion wait;
  592. struct llist_node llnode;
  593. int ret;
  594. };
  595. struct flush_cmd_control {
  596. struct task_struct *f2fs_issue_flush; /* flush thread */
  597. wait_queue_head_t flush_wait_queue; /* waiting queue for wake-up */
  598. atomic_t submit_flush; /* # of issued flushes */
  599. struct llist_head issue_list; /* list for command issue */
  600. struct llist_node *dispatch_list; /* list for command dispatch */
  601. };
  602. struct f2fs_sm_info {
  603. struct sit_info *sit_info; /* whole segment information */
  604. struct free_segmap_info *free_info; /* free segment information */
  605. struct dirty_seglist_info *dirty_info; /* dirty segment information */
  606. struct curseg_info *curseg_array; /* active segment information */
  607. block_t seg0_blkaddr; /* block address of 0'th segment */
  608. block_t main_blkaddr; /* start block address of main area */
  609. block_t ssa_blkaddr; /* start block address of SSA area */
  610. unsigned int segment_count; /* total # of segments */
  611. unsigned int main_segments; /* # of segments in main area */
  612. unsigned int reserved_segments; /* # of reserved segments */
  613. unsigned int ovp_segments; /* # of overprovision segments */
  614. /* a threshold to reclaim prefree segments */
  615. unsigned int rec_prefree_segments;
  616. /* for small discard management */
  617. struct list_head discard_list; /* 4KB discard list */
  618. int nr_discards; /* # of discards in the list */
  619. int max_discards; /* max. discards to be issued */
  620. /* for batched trimming */
  621. unsigned int trim_sections; /* # of sections to trim */
  622. struct list_head sit_entry_set; /* sit entry set list */
  623. unsigned int ipu_policy; /* in-place-update policy */
  624. unsigned int min_ipu_util; /* in-place-update threshold */
  625. unsigned int min_fsync_blocks; /* threshold for fsync */
  626. /* for flush command control */
  627. struct flush_cmd_control *cmd_control_info;
  628. };
  629. /*
  630. * For superblock
  631. */
  632. /*
  633. * COUNT_TYPE for monitoring
  634. *
  635. * f2fs monitors the number of several block types such as on-writeback,
  636. * dirty dentry blocks, dirty node blocks, and dirty meta blocks.
  637. */
  638. #define WB_DATA_TYPE(p) (__is_cp_guaranteed(p) ? F2FS_WB_CP_DATA : F2FS_WB_DATA)
  639. enum count_type {
  640. F2FS_DIRTY_DENTS,
  641. F2FS_DIRTY_DATA,
  642. F2FS_DIRTY_NODES,
  643. F2FS_DIRTY_META,
  644. F2FS_INMEM_PAGES,
  645. F2FS_DIRTY_IMETA,
  646. F2FS_WB_CP_DATA,
  647. F2FS_WB_DATA,
  648. NR_COUNT_TYPE,
  649. };
  650. /*
  651. * The below are the page types of bios used in submit_bio().
  652. * The available types are:
  653. * DATA User data pages. It operates as async mode.
  654. * NODE Node pages. It operates as async mode.
  655. * META FS metadata pages such as SIT, NAT, CP.
  656. * NR_PAGE_TYPE The number of page types.
  657. * META_FLUSH Make sure the previous pages are written
  658. * with waiting the bio's completion
  659. * ... Only can be used with META.
  660. */
  661. #define PAGE_TYPE_OF_BIO(type) ((type) > META ? META : (type))
  662. enum page_type {
  663. DATA,
  664. NODE,
  665. META,
  666. NR_PAGE_TYPE,
  667. META_FLUSH,
  668. INMEM, /* the below types are used by tracepoints only. */
  669. INMEM_DROP,
  670. INMEM_REVOKE,
  671. IPU,
  672. OPU,
  673. };
  674. struct f2fs_io_info {
  675. struct f2fs_sb_info *sbi; /* f2fs_sb_info pointer */
  676. enum page_type type; /* contains DATA/NODE/META/META_FLUSH */
  677. int rw; /* contains R/RS/W/WS with REQ_META/REQ_PRIO */
  678. block_t new_blkaddr; /* new block address to be written */
  679. block_t old_blkaddr; /* old block address before Cow */
  680. struct page *page; /* page to be written */
  681. struct page *encrypted_page; /* encrypted page */
  682. };
  683. #define is_read_io(rw) (((rw) & 1) == READ)
  684. struct f2fs_bio_info {
  685. struct f2fs_sb_info *sbi; /* f2fs superblock */
  686. struct bio *bio; /* bios to merge */
  687. sector_t last_block_in_bio; /* last block number */
  688. struct f2fs_io_info fio; /* store buffered io info. */
  689. struct rw_semaphore io_rwsem; /* blocking op for bio */
  690. };
  691. enum inode_type {
  692. DIR_INODE, /* for dirty dir inode */
  693. FILE_INODE, /* for dirty regular/symlink inode */
  694. DIRTY_META, /* for all dirtied inode metadata */
  695. NR_INODE_TYPE,
  696. };
  697. /* for inner inode cache management */
  698. struct inode_management {
  699. struct radix_tree_root ino_root; /* ino entry array */
  700. spinlock_t ino_lock; /* for ino entry lock */
  701. struct list_head ino_list; /* inode list head */
  702. unsigned long ino_num; /* number of entries */
  703. };
  704. /* For s_flag in struct f2fs_sb_info */
  705. enum {
  706. SBI_IS_DIRTY, /* dirty flag for checkpoint */
  707. SBI_IS_CLOSE, /* specify unmounting */
  708. SBI_NEED_FSCK, /* need fsck.f2fs to fix */
  709. SBI_POR_DOING, /* recovery is doing or not */
  710. SBI_NEED_SB_WRITE, /* need to recover superblock */
  711. SBI_NEED_CP, /* need to checkpoint */
  712. };
  713. enum {
  714. CP_TIME,
  715. REQ_TIME,
  716. MAX_TIME,
  717. };
  718. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  719. #define F2FS_KEY_DESC_PREFIX "f2fs:"
  720. #define F2FS_KEY_DESC_PREFIX_SIZE 5
  721. #endif
  722. struct f2fs_sb_info {
  723. struct super_block *sb; /* pointer to VFS super block */
  724. struct proc_dir_entry *s_proc; /* proc entry */
  725. struct f2fs_super_block *raw_super; /* raw super block pointer */
  726. int valid_super_block; /* valid super block no */
  727. unsigned long s_flag; /* flags for sbi */
  728. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  729. u8 key_prefix[F2FS_KEY_DESC_PREFIX_SIZE];
  730. u8 key_prefix_size;
  731. #endif
  732. #ifdef CONFIG_BLK_DEV_ZONED
  733. unsigned int nr_blkz; /* Total number of zones */
  734. unsigned int blocks_per_blkz; /* F2FS blocks per zone */
  735. unsigned int log_blocks_per_blkz; /* log2 F2FS blocks per zone */
  736. u8 *blkz_type; /* Array of zones type */
  737. #endif
  738. /* for node-related operations */
  739. struct f2fs_nm_info *nm_info; /* node manager */
  740. struct inode *node_inode; /* cache node blocks */
  741. /* for segment-related operations */
  742. struct f2fs_sm_info *sm_info; /* segment manager */
  743. /* for bio operations */
  744. struct f2fs_bio_info read_io; /* for read bios */
  745. struct f2fs_bio_info write_io[NR_PAGE_TYPE]; /* for write bios */
  746. struct mutex wio_mutex[NODE + 1]; /* bio ordering for NODE/DATA */
  747. /* for checkpoint */
  748. struct f2fs_checkpoint *ckpt; /* raw checkpoint pointer */
  749. int cur_cp_pack; /* remain current cp pack */
  750. spinlock_t cp_lock; /* for flag in ckpt */
  751. struct inode *meta_inode; /* cache meta blocks */
  752. struct mutex cp_mutex; /* checkpoint procedure lock */
  753. struct rw_semaphore cp_rwsem; /* blocking FS operations */
  754. struct rw_semaphore node_write; /* locking node writes */
  755. wait_queue_head_t cp_wait;
  756. unsigned long last_time[MAX_TIME]; /* to store time in jiffies */
  757. long interval_time[MAX_TIME]; /* to store thresholds */
  758. struct inode_management im[MAX_INO_ENTRY]; /* manage inode cache */
  759. /* for orphan inode, use 0'th array */
  760. unsigned int max_orphans; /* max orphan inodes */
  761. /* for inode management */
  762. struct list_head inode_list[NR_INODE_TYPE]; /* dirty inode list */
  763. spinlock_t inode_lock[NR_INODE_TYPE]; /* for dirty inode list lock */
  764. /* for extent tree cache */
  765. struct radix_tree_root extent_tree_root;/* cache extent cache entries */
  766. struct rw_semaphore extent_tree_lock; /* locking extent radix tree */
  767. struct list_head extent_list; /* lru list for shrinker */
  768. spinlock_t extent_lock; /* locking extent lru list */
  769. atomic_t total_ext_tree; /* extent tree count */
  770. struct list_head zombie_list; /* extent zombie tree list */
  771. atomic_t total_zombie_tree; /* extent zombie tree count */
  772. atomic_t total_ext_node; /* extent info count */
  773. /* basic filesystem units */
  774. unsigned int log_sectors_per_block; /* log2 sectors per block */
  775. unsigned int log_blocksize; /* log2 block size */
  776. unsigned int blocksize; /* block size */
  777. unsigned int root_ino_num; /* root inode number*/
  778. unsigned int node_ino_num; /* node inode number*/
  779. unsigned int meta_ino_num; /* meta inode number*/
  780. unsigned int log_blocks_per_seg; /* log2 blocks per segment */
  781. unsigned int blocks_per_seg; /* blocks per segment */
  782. unsigned int segs_per_sec; /* segments per section */
  783. unsigned int secs_per_zone; /* sections per zone */
  784. unsigned int total_sections; /* total section count */
  785. unsigned int total_node_count; /* total node block count */
  786. unsigned int total_valid_node_count; /* valid node block count */
  787. loff_t max_file_blocks; /* max block index of file */
  788. int active_logs; /* # of active logs */
  789. int dir_level; /* directory level */
  790. block_t user_block_count; /* # of user blocks */
  791. block_t total_valid_block_count; /* # of valid blocks */
  792. block_t discard_blks; /* discard command candidats */
  793. block_t last_valid_block_count; /* for recovery */
  794. u32 s_next_generation; /* for NFS support */
  795. /* # of pages, see count_type */
  796. atomic_t nr_pages[NR_COUNT_TYPE];
  797. /* # of allocated blocks */
  798. struct percpu_counter alloc_valid_block_count;
  799. /* valid inode count */
  800. struct percpu_counter total_valid_inode_count;
  801. struct f2fs_mount_info mount_opt; /* mount options */
  802. /* for cleaning operations */
  803. struct mutex gc_mutex; /* mutex for GC */
  804. struct f2fs_gc_kthread *gc_thread; /* GC thread */
  805. unsigned int cur_victim_sec; /* current victim section num */
  806. /* maximum # of trials to find a victim segment for SSR and GC */
  807. unsigned int max_victim_search;
  808. /*
  809. * for stat information.
  810. * one is for the LFS mode, and the other is for the SSR mode.
  811. */
  812. #ifdef CONFIG_F2FS_STAT_FS
  813. struct f2fs_stat_info *stat_info; /* FS status information */
  814. unsigned int segment_count[2]; /* # of allocated segments */
  815. unsigned int block_count[2]; /* # of allocated blocks */
  816. atomic_t inplace_count; /* # of inplace update */
  817. atomic64_t total_hit_ext; /* # of lookup extent cache */
  818. atomic64_t read_hit_rbtree; /* # of hit rbtree extent node */
  819. atomic64_t read_hit_largest; /* # of hit largest extent node */
  820. atomic64_t read_hit_cached; /* # of hit cached extent node */
  821. atomic_t inline_xattr; /* # of inline_xattr inodes */
  822. atomic_t inline_inode; /* # of inline_data inodes */
  823. atomic_t inline_dir; /* # of inline_dentry inodes */
  824. int bg_gc; /* background gc calls */
  825. unsigned int ndirty_inode[NR_INODE_TYPE]; /* # of dirty inodes */
  826. #endif
  827. unsigned int last_victim[2]; /* last victim segment # */
  828. spinlock_t stat_lock; /* lock for stat operations */
  829. /* For sysfs suppport */
  830. struct kobject s_kobj;
  831. struct completion s_kobj_unregister;
  832. /* For shrinker support */
  833. struct list_head s_list;
  834. struct mutex umount_mutex;
  835. unsigned int shrinker_run_no;
  836. /* For write statistics */
  837. u64 sectors_written_start;
  838. u64 kbytes_written;
  839. /* For fault injection */
  840. #ifdef CONFIG_F2FS_FAULT_INJECTION
  841. struct f2fs_fault_info fault_info;
  842. #endif
  843. };
  844. #ifdef CONFIG_F2FS_FAULT_INJECTION
  845. static inline bool time_to_inject(struct f2fs_sb_info *sbi, int type)
  846. {
  847. struct f2fs_fault_info *ffi = &sbi->fault_info;
  848. if (!ffi->inject_rate)
  849. return false;
  850. if (!IS_FAULT_SET(ffi, type))
  851. return false;
  852. atomic_inc(&ffi->inject_ops);
  853. if (atomic_read(&ffi->inject_ops) >= ffi->inject_rate) {
  854. atomic_set(&ffi->inject_ops, 0);
  855. printk("%sF2FS-fs : inject %s in %pF\n",
  856. KERN_INFO,
  857. fault_name[type],
  858. __builtin_return_address(0));
  859. return true;
  860. }
  861. return false;
  862. }
  863. #endif
  864. /* For write statistics. Suppose sector size is 512 bytes,
  865. * and the return value is in kbytes. s is of struct f2fs_sb_info.
  866. */
  867. #define BD_PART_WRITTEN(s) \
  868. (((u64)part_stat_read(s->sb->s_bdev->bd_part, sectors[1]) - \
  869. s->sectors_written_start) >> 1)
  870. static inline void f2fs_update_time(struct f2fs_sb_info *sbi, int type)
  871. {
  872. sbi->last_time[type] = jiffies;
  873. }
  874. static inline bool f2fs_time_over(struct f2fs_sb_info *sbi, int type)
  875. {
  876. struct timespec ts = {sbi->interval_time[type], 0};
  877. unsigned long interval = timespec_to_jiffies(&ts);
  878. return time_after(jiffies, sbi->last_time[type] + interval);
  879. }
  880. static inline bool is_idle(struct f2fs_sb_info *sbi)
  881. {
  882. struct block_device *bdev = sbi->sb->s_bdev;
  883. struct request_queue *q = bdev_get_queue(bdev);
  884. struct request_list *rl = &q->rq;
  885. if (rl->count[BLK_RW_SYNC] || rl->count[BLK_RW_ASYNC])
  886. return 0;
  887. return f2fs_time_over(sbi, REQ_TIME);
  888. }
  889. /*
  890. * Inline functions
  891. */
  892. static inline struct f2fs_inode_info *F2FS_I(struct inode *inode)
  893. {
  894. return container_of(inode, struct f2fs_inode_info, vfs_inode);
  895. }
  896. static inline struct f2fs_sb_info *F2FS_SB(struct super_block *sb)
  897. {
  898. return sb->s_fs_info;
  899. }
  900. static inline struct f2fs_sb_info *F2FS_I_SB(struct inode *inode)
  901. {
  902. return F2FS_SB(inode->i_sb);
  903. }
  904. static inline struct f2fs_sb_info *F2FS_M_SB(struct address_space *mapping)
  905. {
  906. return F2FS_I_SB(mapping->host);
  907. }
  908. static inline struct f2fs_sb_info *F2FS_P_SB(struct page *page)
  909. {
  910. return F2FS_M_SB(page->mapping);
  911. }
  912. static inline struct f2fs_super_block *F2FS_RAW_SUPER(struct f2fs_sb_info *sbi)
  913. {
  914. return (struct f2fs_super_block *)(sbi->raw_super);
  915. }
  916. static inline struct f2fs_checkpoint *F2FS_CKPT(struct f2fs_sb_info *sbi)
  917. {
  918. return (struct f2fs_checkpoint *)(sbi->ckpt);
  919. }
  920. static inline struct f2fs_node *F2FS_NODE(struct page *page)
  921. {
  922. return (struct f2fs_node *)page_address(page);
  923. }
  924. static inline struct f2fs_inode *F2FS_INODE(struct page *page)
  925. {
  926. return &((struct f2fs_node *)page_address(page))->i;
  927. }
  928. static inline struct f2fs_nm_info *NM_I(struct f2fs_sb_info *sbi)
  929. {
  930. return (struct f2fs_nm_info *)(sbi->nm_info);
  931. }
  932. static inline struct f2fs_sm_info *SM_I(struct f2fs_sb_info *sbi)
  933. {
  934. return (struct f2fs_sm_info *)(sbi->sm_info);
  935. }
  936. static inline struct sit_info *SIT_I(struct f2fs_sb_info *sbi)
  937. {
  938. return (struct sit_info *)(SM_I(sbi)->sit_info);
  939. }
  940. static inline struct free_segmap_info *FREE_I(struct f2fs_sb_info *sbi)
  941. {
  942. return (struct free_segmap_info *)(SM_I(sbi)->free_info);
  943. }
  944. static inline struct dirty_seglist_info *DIRTY_I(struct f2fs_sb_info *sbi)
  945. {
  946. return (struct dirty_seglist_info *)(SM_I(sbi)->dirty_info);
  947. }
  948. static inline struct address_space *META_MAPPING(struct f2fs_sb_info *sbi)
  949. {
  950. return sbi->meta_inode->i_mapping;
  951. }
  952. static inline struct address_space *NODE_MAPPING(struct f2fs_sb_info *sbi)
  953. {
  954. return sbi->node_inode->i_mapping;
  955. }
  956. static inline bool is_sbi_flag_set(struct f2fs_sb_info *sbi, unsigned int type)
  957. {
  958. return test_bit(type, &sbi->s_flag);
  959. }
  960. static inline void set_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
  961. {
  962. set_bit(type, &sbi->s_flag);
  963. }
  964. static inline void clear_sbi_flag(struct f2fs_sb_info *sbi, unsigned int type)
  965. {
  966. clear_bit(type, &sbi->s_flag);
  967. }
  968. static inline unsigned long long cur_cp_version(struct f2fs_checkpoint *cp)
  969. {
  970. return le64_to_cpu(cp->checkpoint_ver);
  971. }
  972. static inline bool __is_set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  973. {
  974. unsigned int ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  975. return ckpt_flags & f;
  976. }
  977. static inline bool is_set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
  978. {
  979. return __is_set_ckpt_flags(F2FS_CKPT(sbi), f);
  980. }
  981. static inline void __set_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  982. {
  983. unsigned int ckpt_flags;
  984. ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  985. ckpt_flags |= f;
  986. cp->ckpt_flags = cpu_to_le32(ckpt_flags);
  987. }
  988. static inline void set_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
  989. {
  990. spin_lock(&sbi->cp_lock);
  991. __set_ckpt_flags(F2FS_CKPT(sbi), f);
  992. spin_unlock(&sbi->cp_lock);
  993. }
  994. static inline void __clear_ckpt_flags(struct f2fs_checkpoint *cp, unsigned int f)
  995. {
  996. unsigned int ckpt_flags;
  997. ckpt_flags = le32_to_cpu(cp->ckpt_flags);
  998. ckpt_flags &= (~f);
  999. cp->ckpt_flags = cpu_to_le32(ckpt_flags);
  1000. }
  1001. static inline void clear_ckpt_flags(struct f2fs_sb_info *sbi, unsigned int f)
  1002. {
  1003. spin_lock(&sbi->cp_lock);
  1004. __clear_ckpt_flags(F2FS_CKPT(sbi), f);
  1005. spin_unlock(&sbi->cp_lock);
  1006. }
  1007. static inline void f2fs_lock_op(struct f2fs_sb_info *sbi)
  1008. {
  1009. down_read(&sbi->cp_rwsem);
  1010. }
  1011. static inline void f2fs_unlock_op(struct f2fs_sb_info *sbi)
  1012. {
  1013. up_read(&sbi->cp_rwsem);
  1014. }
  1015. static inline void f2fs_lock_all(struct f2fs_sb_info *sbi)
  1016. {
  1017. down_write(&sbi->cp_rwsem);
  1018. }
  1019. static inline void f2fs_unlock_all(struct f2fs_sb_info *sbi)
  1020. {
  1021. up_write(&sbi->cp_rwsem);
  1022. }
  1023. static inline int __get_cp_reason(struct f2fs_sb_info *sbi)
  1024. {
  1025. int reason = CP_SYNC;
  1026. if (test_opt(sbi, FASTBOOT))
  1027. reason = CP_FASTBOOT;
  1028. if (is_sbi_flag_set(sbi, SBI_IS_CLOSE))
  1029. reason = CP_UMOUNT;
  1030. return reason;
  1031. }
  1032. static inline bool __remain_node_summaries(int reason)
  1033. {
  1034. return (reason == CP_UMOUNT || reason == CP_FASTBOOT);
  1035. }
  1036. static inline bool __exist_node_summaries(struct f2fs_sb_info *sbi)
  1037. {
  1038. return (is_set_ckpt_flags(sbi, CP_UMOUNT_FLAG) ||
  1039. is_set_ckpt_flags(sbi, CP_FASTBOOT_FLAG));
  1040. }
  1041. /*
  1042. * Check whether the given nid is within node id range.
  1043. */
  1044. static inline int check_nid_range(struct f2fs_sb_info *sbi, nid_t nid)
  1045. {
  1046. if (unlikely(nid < F2FS_ROOT_INO(sbi)))
  1047. return -EINVAL;
  1048. if (unlikely(nid >= NM_I(sbi)->max_nid))
  1049. return -EINVAL;
  1050. return 0;
  1051. }
  1052. #define F2FS_DEFAULT_ALLOCATED_BLOCKS 1
  1053. /*
  1054. * Check whether the inode has blocks or not
  1055. */
  1056. static inline int F2FS_HAS_BLOCKS(struct inode *inode)
  1057. {
  1058. if (F2FS_I(inode)->i_xattr_nid)
  1059. return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS + 1;
  1060. else
  1061. return inode->i_blocks > F2FS_DEFAULT_ALLOCATED_BLOCKS;
  1062. }
  1063. static inline bool f2fs_has_xattr_block(unsigned int ofs)
  1064. {
  1065. return ofs == XATTR_NODE_OFFSET;
  1066. }
  1067. static inline void f2fs_i_blocks_write(struct inode *, blkcnt_t, bool);
  1068. static inline bool inc_valid_block_count(struct f2fs_sb_info *sbi,
  1069. struct inode *inode, blkcnt_t *count)
  1070. {
  1071. blkcnt_t diff;
  1072. #ifdef CONFIG_F2FS_FAULT_INJECTION
  1073. if (time_to_inject(sbi, FAULT_BLOCK))
  1074. return false;
  1075. #endif
  1076. /*
  1077. * let's increase this in prior to actual block count change in order
  1078. * for f2fs_sync_file to avoid data races when deciding checkpoint.
  1079. */
  1080. percpu_counter_add(&sbi->alloc_valid_block_count, (*count));
  1081. spin_lock(&sbi->stat_lock);
  1082. sbi->total_valid_block_count += (block_t)(*count);
  1083. if (unlikely(sbi->total_valid_block_count > sbi->user_block_count)) {
  1084. diff = sbi->total_valid_block_count - sbi->user_block_count;
  1085. *count -= diff;
  1086. sbi->total_valid_block_count = sbi->user_block_count;
  1087. if (!*count) {
  1088. spin_unlock(&sbi->stat_lock);
  1089. percpu_counter_sub(&sbi->alloc_valid_block_count, diff);
  1090. return false;
  1091. }
  1092. }
  1093. spin_unlock(&sbi->stat_lock);
  1094. f2fs_i_blocks_write(inode, *count, true);
  1095. return true;
  1096. }
  1097. static inline void dec_valid_block_count(struct f2fs_sb_info *sbi,
  1098. struct inode *inode,
  1099. blkcnt_t count)
  1100. {
  1101. spin_lock(&sbi->stat_lock);
  1102. f2fs_bug_on(sbi, sbi->total_valid_block_count < (block_t) count);
  1103. f2fs_bug_on(sbi, inode->i_blocks < count);
  1104. sbi->total_valid_block_count -= (block_t)count;
  1105. spin_unlock(&sbi->stat_lock);
  1106. f2fs_i_blocks_write(inode, count, false);
  1107. }
  1108. static inline void inc_page_count(struct f2fs_sb_info *sbi, int count_type)
  1109. {
  1110. atomic_inc(&sbi->nr_pages[count_type]);
  1111. if (count_type == F2FS_DIRTY_DATA || count_type == F2FS_INMEM_PAGES ||
  1112. count_type == F2FS_WB_CP_DATA || count_type == F2FS_WB_DATA)
  1113. return;
  1114. set_sbi_flag(sbi, SBI_IS_DIRTY);
  1115. }
  1116. static inline void inode_inc_dirty_pages(struct inode *inode)
  1117. {
  1118. percpu_counter_inc(&F2FS_I(inode)->dirty_pages);
  1119. inc_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
  1120. F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
  1121. }
  1122. static inline void dec_page_count(struct f2fs_sb_info *sbi, int count_type)
  1123. {
  1124. atomic_dec(&sbi->nr_pages[count_type]);
  1125. }
  1126. static inline void inode_dec_dirty_pages(struct inode *inode)
  1127. {
  1128. if (!S_ISDIR(inode->i_mode) && !S_ISREG(inode->i_mode) &&
  1129. !S_ISLNK(inode->i_mode))
  1130. return;
  1131. percpu_counter_dec(&F2FS_I(inode)->dirty_pages);
  1132. dec_page_count(F2FS_I_SB(inode), S_ISDIR(inode->i_mode) ?
  1133. F2FS_DIRTY_DENTS : F2FS_DIRTY_DATA);
  1134. }
  1135. static inline s64 get_pages(struct f2fs_sb_info *sbi, int count_type)
  1136. {
  1137. return atomic_read(&sbi->nr_pages[count_type]);
  1138. }
  1139. static inline s64 get_dirty_pages(struct inode *inode)
  1140. {
  1141. return percpu_counter_sum_positive(&F2FS_I(inode)->dirty_pages);
  1142. }
  1143. static inline int get_blocktype_secs(struct f2fs_sb_info *sbi, int block_type)
  1144. {
  1145. unsigned int pages_per_sec = sbi->segs_per_sec * sbi->blocks_per_seg;
  1146. unsigned int segs = (get_pages(sbi, block_type) + pages_per_sec - 1) >>
  1147. sbi->log_blocks_per_seg;
  1148. return segs / sbi->segs_per_sec;
  1149. }
  1150. static inline block_t valid_user_blocks(struct f2fs_sb_info *sbi)
  1151. {
  1152. return sbi->total_valid_block_count;
  1153. }
  1154. static inline block_t discard_blocks(struct f2fs_sb_info *sbi)
  1155. {
  1156. return sbi->discard_blks;
  1157. }
  1158. static inline unsigned long __bitmap_size(struct f2fs_sb_info *sbi, int flag)
  1159. {
  1160. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1161. /* return NAT or SIT bitmap */
  1162. if (flag == NAT_BITMAP)
  1163. return le32_to_cpu(ckpt->nat_ver_bitmap_bytesize);
  1164. else if (flag == SIT_BITMAP)
  1165. return le32_to_cpu(ckpt->sit_ver_bitmap_bytesize);
  1166. return 0;
  1167. }
  1168. static inline block_t __cp_payload(struct f2fs_sb_info *sbi)
  1169. {
  1170. return le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_payload);
  1171. }
  1172. static inline void *__bitmap_ptr(struct f2fs_sb_info *sbi, int flag)
  1173. {
  1174. struct f2fs_checkpoint *ckpt = F2FS_CKPT(sbi);
  1175. int offset;
  1176. if (__cp_payload(sbi) > 0) {
  1177. if (flag == NAT_BITMAP)
  1178. return &ckpt->sit_nat_version_bitmap;
  1179. else
  1180. return (unsigned char *)ckpt + F2FS_BLKSIZE;
  1181. } else {
  1182. offset = (flag == NAT_BITMAP) ?
  1183. le32_to_cpu(ckpt->sit_ver_bitmap_bytesize) : 0;
  1184. return &ckpt->sit_nat_version_bitmap + offset;
  1185. }
  1186. }
  1187. static inline block_t __start_cp_addr(struct f2fs_sb_info *sbi)
  1188. {
  1189. block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
  1190. if (sbi->cur_cp_pack == 2)
  1191. start_addr += sbi->blocks_per_seg;
  1192. return start_addr;
  1193. }
  1194. static inline block_t __start_cp_next_addr(struct f2fs_sb_info *sbi)
  1195. {
  1196. block_t start_addr = le32_to_cpu(F2FS_RAW_SUPER(sbi)->cp_blkaddr);
  1197. if (sbi->cur_cp_pack == 1)
  1198. start_addr += sbi->blocks_per_seg;
  1199. return start_addr;
  1200. }
  1201. static inline void __set_cp_next_pack(struct f2fs_sb_info *sbi)
  1202. {
  1203. sbi->cur_cp_pack = (sbi->cur_cp_pack == 1) ? 2 : 1;
  1204. }
  1205. static inline block_t __start_sum_addr(struct f2fs_sb_info *sbi)
  1206. {
  1207. return le32_to_cpu(F2FS_CKPT(sbi)->cp_pack_start_sum);
  1208. }
  1209. static inline bool inc_valid_node_count(struct f2fs_sb_info *sbi,
  1210. struct inode *inode)
  1211. {
  1212. block_t valid_block_count;
  1213. unsigned int valid_node_count;
  1214. spin_lock(&sbi->stat_lock);
  1215. valid_block_count = sbi->total_valid_block_count + 1;
  1216. if (unlikely(valid_block_count > sbi->user_block_count)) {
  1217. spin_unlock(&sbi->stat_lock);
  1218. return false;
  1219. }
  1220. valid_node_count = sbi->total_valid_node_count + 1;
  1221. if (unlikely(valid_node_count > sbi->total_node_count)) {
  1222. spin_unlock(&sbi->stat_lock);
  1223. return false;
  1224. }
  1225. if (inode)
  1226. f2fs_i_blocks_write(inode, 1, true);
  1227. sbi->total_valid_node_count++;
  1228. sbi->total_valid_block_count++;
  1229. spin_unlock(&sbi->stat_lock);
  1230. percpu_counter_inc(&sbi->alloc_valid_block_count);
  1231. return true;
  1232. }
  1233. static inline void dec_valid_node_count(struct f2fs_sb_info *sbi,
  1234. struct inode *inode)
  1235. {
  1236. spin_lock(&sbi->stat_lock);
  1237. f2fs_bug_on(sbi, !sbi->total_valid_block_count);
  1238. f2fs_bug_on(sbi, !sbi->total_valid_node_count);
  1239. f2fs_bug_on(sbi, !inode->i_blocks);
  1240. f2fs_i_blocks_write(inode, 1, false);
  1241. sbi->total_valid_node_count--;
  1242. sbi->total_valid_block_count--;
  1243. spin_unlock(&sbi->stat_lock);
  1244. }
  1245. static inline unsigned int valid_node_count(struct f2fs_sb_info *sbi)
  1246. {
  1247. return sbi->total_valid_node_count;
  1248. }
  1249. static inline void inc_valid_inode_count(struct f2fs_sb_info *sbi)
  1250. {
  1251. percpu_counter_inc(&sbi->total_valid_inode_count);
  1252. }
  1253. static inline void dec_valid_inode_count(struct f2fs_sb_info *sbi)
  1254. {
  1255. percpu_counter_dec(&sbi->total_valid_inode_count);
  1256. }
  1257. static inline s64 valid_inode_count(struct f2fs_sb_info *sbi)
  1258. {
  1259. return percpu_counter_sum_positive(&sbi->total_valid_inode_count);
  1260. }
  1261. static inline struct page *f2fs_grab_cache_page(struct address_space *mapping,
  1262. pgoff_t index, bool for_write)
  1263. {
  1264. #ifdef CONFIG_F2FS_FAULT_INJECTION
  1265. struct page *page = find_lock_page(mapping, index);
  1266. if (page)
  1267. return page;
  1268. if (time_to_inject(F2FS_M_SB(mapping), FAULT_PAGE_ALLOC))
  1269. return NULL;
  1270. #endif
  1271. if (!for_write)
  1272. return grab_cache_page(mapping, index);
  1273. return grab_cache_page_write_begin(mapping, index, AOP_FLAG_NOFS);
  1274. }
  1275. static inline void f2fs_copy_page(struct page *src, struct page *dst)
  1276. {
  1277. char *src_kaddr = kmap(src);
  1278. char *dst_kaddr = kmap(dst);
  1279. memcpy(dst_kaddr, src_kaddr, PAGE_SIZE);
  1280. kunmap(dst);
  1281. kunmap(src);
  1282. }
  1283. static inline void f2fs_put_page(struct page *page, int unlock)
  1284. {
  1285. if (!page)
  1286. return;
  1287. if (unlock) {
  1288. f2fs_bug_on(F2FS_P_SB(page), !PageLocked(page));
  1289. unlock_page(page);
  1290. }
  1291. put_page(page);
  1292. }
  1293. static inline void f2fs_put_dnode(struct dnode_of_data *dn)
  1294. {
  1295. if (dn->node_page)
  1296. f2fs_put_page(dn->node_page, 1);
  1297. if (dn->inode_page && dn->node_page != dn->inode_page)
  1298. f2fs_put_page(dn->inode_page, 0);
  1299. dn->node_page = NULL;
  1300. dn->inode_page = NULL;
  1301. }
  1302. static inline struct kmem_cache *f2fs_kmem_cache_create(const char *name,
  1303. size_t size)
  1304. {
  1305. return kmem_cache_create(name, size, 0, SLAB_RECLAIM_ACCOUNT, NULL);
  1306. }
  1307. static inline void *f2fs_kmem_cache_alloc(struct kmem_cache *cachep,
  1308. gfp_t flags)
  1309. {
  1310. void *entry;
  1311. entry = kmem_cache_alloc(cachep, flags);
  1312. if (!entry)
  1313. entry = kmem_cache_alloc(cachep, flags | __GFP_NOFAIL);
  1314. return entry;
  1315. }
  1316. static inline struct bio *f2fs_bio_alloc(int npages)
  1317. {
  1318. struct bio *bio;
  1319. /* No failure on bio allocation */
  1320. bio = bio_alloc(GFP_NOIO, npages);
  1321. if (!bio)
  1322. bio = bio_alloc(GFP_NOIO | __GFP_NOFAIL, npages);
  1323. return bio;
  1324. }
  1325. static inline void f2fs_radix_tree_insert(struct radix_tree_root *root,
  1326. unsigned long index, void *item)
  1327. {
  1328. while (radix_tree_insert(root, index, item))
  1329. cond_resched();
  1330. }
  1331. #define RAW_IS_INODE(p) ((p)->footer.nid == (p)->footer.ino)
  1332. static inline bool IS_INODE(struct page *page)
  1333. {
  1334. struct f2fs_node *p = F2FS_NODE(page);
  1335. return RAW_IS_INODE(p);
  1336. }
  1337. static inline __le32 *blkaddr_in_node(struct f2fs_node *node)
  1338. {
  1339. return RAW_IS_INODE(node) ? node->i.i_addr : node->dn.addr;
  1340. }
  1341. static inline block_t datablock_addr(struct page *node_page,
  1342. unsigned int offset)
  1343. {
  1344. struct f2fs_node *raw_node;
  1345. __le32 *addr_array;
  1346. raw_node = F2FS_NODE(node_page);
  1347. addr_array = blkaddr_in_node(raw_node);
  1348. return le32_to_cpu(addr_array[offset]);
  1349. }
  1350. static inline int f2fs_test_bit(unsigned int nr, char *addr)
  1351. {
  1352. int mask;
  1353. addr += (nr >> 3);
  1354. mask = 1 << (7 - (nr & 0x07));
  1355. return mask & *addr;
  1356. }
  1357. static inline void f2fs_set_bit(unsigned int nr, char *addr)
  1358. {
  1359. int mask;
  1360. addr += (nr >> 3);
  1361. mask = 1 << (7 - (nr & 0x07));
  1362. *addr |= mask;
  1363. }
  1364. static inline void f2fs_clear_bit(unsigned int nr, char *addr)
  1365. {
  1366. int mask;
  1367. addr += (nr >> 3);
  1368. mask = 1 << (7 - (nr & 0x07));
  1369. *addr &= ~mask;
  1370. }
  1371. static inline int f2fs_test_and_set_bit(unsigned int nr, char *addr)
  1372. {
  1373. int mask;
  1374. int ret;
  1375. addr += (nr >> 3);
  1376. mask = 1 << (7 - (nr & 0x07));
  1377. ret = mask & *addr;
  1378. *addr |= mask;
  1379. return ret;
  1380. }
  1381. static inline int f2fs_test_and_clear_bit(unsigned int nr, char *addr)
  1382. {
  1383. int mask;
  1384. int ret;
  1385. addr += (nr >> 3);
  1386. mask = 1 << (7 - (nr & 0x07));
  1387. ret = mask & *addr;
  1388. *addr &= ~mask;
  1389. return ret;
  1390. }
  1391. static inline void f2fs_change_bit(unsigned int nr, char *addr)
  1392. {
  1393. int mask;
  1394. addr += (nr >> 3);
  1395. mask = 1 << (7 - (nr & 0x07));
  1396. *addr ^= mask;
  1397. }
  1398. /* used for f2fs_inode_info->flags */
  1399. enum {
  1400. FI_NEW_INODE, /* indicate newly allocated inode */
  1401. FI_DIRTY_INODE, /* indicate inode is dirty or not */
  1402. FI_AUTO_RECOVER, /* indicate inode is recoverable */
  1403. FI_DIRTY_DIR, /* indicate directory has dirty pages */
  1404. FI_INC_LINK, /* need to increment i_nlink */
  1405. FI_ACL_MODE, /* indicate acl mode */
  1406. FI_NO_ALLOC, /* should not allocate any blocks */
  1407. FI_FREE_NID, /* free allocated nide */
  1408. FI_NO_EXTENT, /* not to use the extent cache */
  1409. FI_INLINE_XATTR, /* used for inline xattr */
  1410. FI_INLINE_DATA, /* used for inline data*/
  1411. FI_INLINE_DENTRY, /* used for inline dentry */
  1412. FI_APPEND_WRITE, /* inode has appended data */
  1413. FI_UPDATE_WRITE, /* inode has in-place-update data */
  1414. FI_NEED_IPU, /* used for ipu per file */
  1415. FI_ATOMIC_FILE, /* indicate atomic file */
  1416. FI_VOLATILE_FILE, /* indicate volatile file */
  1417. FI_FIRST_BLOCK_WRITTEN, /* indicate #0 data block was written */
  1418. FI_DROP_CACHE, /* drop dirty page cache */
  1419. FI_DATA_EXIST, /* indicate data exists */
  1420. FI_INLINE_DOTS, /* indicate inline dot dentries */
  1421. FI_DO_DEFRAG, /* indicate defragment is running */
  1422. FI_DIRTY_FILE, /* indicate regular/symlink has dirty pages */
  1423. };
  1424. static inline void __mark_inode_dirty_flag(struct inode *inode,
  1425. int flag, bool set)
  1426. {
  1427. switch (flag) {
  1428. case FI_INLINE_XATTR:
  1429. case FI_INLINE_DATA:
  1430. case FI_INLINE_DENTRY:
  1431. if (set)
  1432. return;
  1433. case FI_DATA_EXIST:
  1434. case FI_INLINE_DOTS:
  1435. f2fs_mark_inode_dirty_sync(inode, true);
  1436. }
  1437. }
  1438. static inline void set_inode_flag(struct inode *inode, int flag)
  1439. {
  1440. if (!test_bit(flag, &F2FS_I(inode)->flags))
  1441. set_bit(flag, &F2FS_I(inode)->flags);
  1442. __mark_inode_dirty_flag(inode, flag, true);
  1443. }
  1444. static inline int is_inode_flag_set(struct inode *inode, int flag)
  1445. {
  1446. return test_bit(flag, &F2FS_I(inode)->flags);
  1447. }
  1448. static inline void clear_inode_flag(struct inode *inode, int flag)
  1449. {
  1450. if (test_bit(flag, &F2FS_I(inode)->flags))
  1451. clear_bit(flag, &F2FS_I(inode)->flags);
  1452. __mark_inode_dirty_flag(inode, flag, false);
  1453. }
  1454. static inline void set_acl_inode(struct inode *inode, umode_t mode)
  1455. {
  1456. F2FS_I(inode)->i_acl_mode = mode;
  1457. set_inode_flag(inode, FI_ACL_MODE);
  1458. f2fs_mark_inode_dirty_sync(inode, false);
  1459. }
  1460. static inline void f2fs_i_links_write(struct inode *inode, bool inc)
  1461. {
  1462. if (inc)
  1463. inc_nlink(inode);
  1464. else
  1465. drop_nlink(inode);
  1466. f2fs_mark_inode_dirty_sync(inode, true);
  1467. }
  1468. static inline void f2fs_i_blocks_write(struct inode *inode,
  1469. blkcnt_t diff, bool add)
  1470. {
  1471. bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
  1472. bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
  1473. inode->i_blocks = add ? inode->i_blocks + diff :
  1474. inode->i_blocks - diff;
  1475. f2fs_mark_inode_dirty_sync(inode, true);
  1476. if (clean || recover)
  1477. set_inode_flag(inode, FI_AUTO_RECOVER);
  1478. }
  1479. static inline void f2fs_i_size_write(struct inode *inode, loff_t i_size)
  1480. {
  1481. bool clean = !is_inode_flag_set(inode, FI_DIRTY_INODE);
  1482. bool recover = is_inode_flag_set(inode, FI_AUTO_RECOVER);
  1483. if (i_size_read(inode) == i_size)
  1484. return;
  1485. i_size_write(inode, i_size);
  1486. f2fs_mark_inode_dirty_sync(inode, true);
  1487. if (clean || recover)
  1488. set_inode_flag(inode, FI_AUTO_RECOVER);
  1489. }
  1490. static inline void f2fs_i_depth_write(struct inode *inode, unsigned int depth)
  1491. {
  1492. F2FS_I(inode)->i_current_depth = depth;
  1493. f2fs_mark_inode_dirty_sync(inode, true);
  1494. }
  1495. static inline void f2fs_i_xnid_write(struct inode *inode, nid_t xnid)
  1496. {
  1497. F2FS_I(inode)->i_xattr_nid = xnid;
  1498. f2fs_mark_inode_dirty_sync(inode, true);
  1499. }
  1500. static inline void f2fs_i_pino_write(struct inode *inode, nid_t pino)
  1501. {
  1502. F2FS_I(inode)->i_pino = pino;
  1503. f2fs_mark_inode_dirty_sync(inode, true);
  1504. }
  1505. static inline void get_inline_info(struct inode *inode, struct f2fs_inode *ri)
  1506. {
  1507. struct f2fs_inode_info *fi = F2FS_I(inode);
  1508. if (ri->i_inline & F2FS_INLINE_XATTR)
  1509. set_bit(FI_INLINE_XATTR, &fi->flags);
  1510. if (ri->i_inline & F2FS_INLINE_DATA)
  1511. set_bit(FI_INLINE_DATA, &fi->flags);
  1512. if (ri->i_inline & F2FS_INLINE_DENTRY)
  1513. set_bit(FI_INLINE_DENTRY, &fi->flags);
  1514. if (ri->i_inline & F2FS_DATA_EXIST)
  1515. set_bit(FI_DATA_EXIST, &fi->flags);
  1516. if (ri->i_inline & F2FS_INLINE_DOTS)
  1517. set_bit(FI_INLINE_DOTS, &fi->flags);
  1518. }
  1519. static inline void set_raw_inline(struct inode *inode, struct f2fs_inode *ri)
  1520. {
  1521. ri->i_inline = 0;
  1522. if (is_inode_flag_set(inode, FI_INLINE_XATTR))
  1523. ri->i_inline |= F2FS_INLINE_XATTR;
  1524. if (is_inode_flag_set(inode, FI_INLINE_DATA))
  1525. ri->i_inline |= F2FS_INLINE_DATA;
  1526. if (is_inode_flag_set(inode, FI_INLINE_DENTRY))
  1527. ri->i_inline |= F2FS_INLINE_DENTRY;
  1528. if (is_inode_flag_set(inode, FI_DATA_EXIST))
  1529. ri->i_inline |= F2FS_DATA_EXIST;
  1530. if (is_inode_flag_set(inode, FI_INLINE_DOTS))
  1531. ri->i_inline |= F2FS_INLINE_DOTS;
  1532. }
  1533. static inline int f2fs_has_inline_xattr(struct inode *inode)
  1534. {
  1535. return is_inode_flag_set(inode, FI_INLINE_XATTR);
  1536. }
  1537. static inline unsigned int addrs_per_inode(struct inode *inode)
  1538. {
  1539. if (f2fs_has_inline_xattr(inode))
  1540. return DEF_ADDRS_PER_INODE - F2FS_INLINE_XATTR_ADDRS;
  1541. return DEF_ADDRS_PER_INODE;
  1542. }
  1543. static inline void *inline_xattr_addr(struct page *page)
  1544. {
  1545. struct f2fs_inode *ri = F2FS_INODE(page);
  1546. return (void *)&(ri->i_addr[DEF_ADDRS_PER_INODE -
  1547. F2FS_INLINE_XATTR_ADDRS]);
  1548. }
  1549. static inline int inline_xattr_size(struct inode *inode)
  1550. {
  1551. if (f2fs_has_inline_xattr(inode))
  1552. return F2FS_INLINE_XATTR_ADDRS << 2;
  1553. else
  1554. return 0;
  1555. }
  1556. static inline int f2fs_has_inline_data(struct inode *inode)
  1557. {
  1558. return is_inode_flag_set(inode, FI_INLINE_DATA);
  1559. }
  1560. static inline void f2fs_clear_inline_inode(struct inode *inode)
  1561. {
  1562. clear_inode_flag(inode, FI_INLINE_DATA);
  1563. clear_inode_flag(inode, FI_DATA_EXIST);
  1564. }
  1565. static inline int f2fs_exist_data(struct inode *inode)
  1566. {
  1567. return is_inode_flag_set(inode, FI_DATA_EXIST);
  1568. }
  1569. static inline int f2fs_has_inline_dots(struct inode *inode)
  1570. {
  1571. return is_inode_flag_set(inode, FI_INLINE_DOTS);
  1572. }
  1573. static inline bool f2fs_is_atomic_file(struct inode *inode)
  1574. {
  1575. return is_inode_flag_set(inode, FI_ATOMIC_FILE);
  1576. }
  1577. static inline bool f2fs_is_volatile_file(struct inode *inode)
  1578. {
  1579. return is_inode_flag_set(inode, FI_VOLATILE_FILE);
  1580. }
  1581. static inline bool f2fs_is_first_block_written(struct inode *inode)
  1582. {
  1583. return is_inode_flag_set(inode, FI_FIRST_BLOCK_WRITTEN);
  1584. }
  1585. static inline bool f2fs_is_drop_cache(struct inode *inode)
  1586. {
  1587. return is_inode_flag_set(inode, FI_DROP_CACHE);
  1588. }
  1589. static inline void *inline_data_addr(struct page *page)
  1590. {
  1591. struct f2fs_inode *ri = F2FS_INODE(page);
  1592. return (void *)&(ri->i_addr[1]);
  1593. }
  1594. static inline int f2fs_has_inline_dentry(struct inode *inode)
  1595. {
  1596. return is_inode_flag_set(inode, FI_INLINE_DENTRY);
  1597. }
  1598. static inline void f2fs_dentry_kunmap(struct inode *dir, struct page *page)
  1599. {
  1600. if (!f2fs_has_inline_dentry(dir))
  1601. kunmap(page);
  1602. }
  1603. static inline int is_file(struct inode *inode, int type)
  1604. {
  1605. return F2FS_I(inode)->i_advise & type;
  1606. }
  1607. static inline void set_file(struct inode *inode, int type)
  1608. {
  1609. F2FS_I(inode)->i_advise |= type;
  1610. f2fs_mark_inode_dirty_sync(inode, true);
  1611. }
  1612. static inline void clear_file(struct inode *inode, int type)
  1613. {
  1614. F2FS_I(inode)->i_advise &= ~type;
  1615. f2fs_mark_inode_dirty_sync(inode, true);
  1616. }
  1617. static inline bool f2fs_skip_inode_update(struct inode *inode, int dsync)
  1618. {
  1619. if (dsync) {
  1620. struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
  1621. bool ret;
  1622. spin_lock(&sbi->inode_lock[DIRTY_META]);
  1623. ret = list_empty(&F2FS_I(inode)->gdirty_list);
  1624. spin_unlock(&sbi->inode_lock[DIRTY_META]);
  1625. return ret;
  1626. }
  1627. if (!is_inode_flag_set(inode, FI_AUTO_RECOVER) ||
  1628. file_keep_isize(inode) ||
  1629. i_size_read(inode) & PAGE_MASK)
  1630. return false;
  1631. return F2FS_I(inode)->last_disk_size == i_size_read(inode);
  1632. }
  1633. static inline int f2fs_readonly(struct super_block *sb)
  1634. {
  1635. return sb->s_flags & MS_RDONLY;
  1636. }
  1637. static inline bool f2fs_cp_error(struct f2fs_sb_info *sbi)
  1638. {
  1639. return is_set_ckpt_flags(sbi, CP_ERROR_FLAG);
  1640. }
  1641. static inline struct inode *file_inode(struct file *f)
  1642. {
  1643. return f->f_path.dentry->d_inode;
  1644. }
  1645. static inline bool is_dot_dotdot(const struct qstr *str)
  1646. {
  1647. if (str->len == 1 && str->name[0] == '.')
  1648. return true;
  1649. if (str->len == 2 && str->name[0] == '.' && str->name[1] == '.')
  1650. return true;
  1651. return false;
  1652. }
  1653. static inline bool f2fs_may_extent_tree(struct inode *inode)
  1654. {
  1655. mode_t mode = inode->i_mode;
  1656. if (!test_opt(F2FS_I_SB(inode), EXTENT_CACHE) ||
  1657. is_inode_flag_set(inode, FI_NO_EXTENT))
  1658. return false;
  1659. return S_ISREG(mode);
  1660. }
  1661. static inline void *f2fs_kmalloc(struct f2fs_sb_info *sbi,
  1662. size_t size, gfp_t flags)
  1663. {
  1664. #ifdef CONFIG_F2FS_FAULT_INJECTION
  1665. if (time_to_inject(sbi, FAULT_KMALLOC))
  1666. return NULL;
  1667. #endif
  1668. return kmalloc(size, flags);
  1669. }
  1670. static inline void *f2fs_kvmalloc(size_t size, gfp_t flags)
  1671. {
  1672. void *ret;
  1673. ret = kmalloc(size, flags | __GFP_NOWARN);
  1674. if (!ret)
  1675. ret = __vmalloc(size, flags, PAGE_KERNEL);
  1676. return ret;
  1677. }
  1678. static inline void *f2fs_kvzalloc(size_t size, gfp_t flags)
  1679. {
  1680. void *ret;
  1681. ret = kzalloc(size, flags | __GFP_NOWARN);
  1682. if (!ret)
  1683. ret = __vmalloc(size, flags | __GFP_ZERO, PAGE_KERNEL);
  1684. return ret;
  1685. }
  1686. static inline void f2fs_kvfree(void *ptr)
  1687. {
  1688. if (is_vmalloc_addr(ptr))
  1689. vfree(ptr);
  1690. else
  1691. kfree(ptr);
  1692. }
  1693. #define get_inode_mode(i) \
  1694. ((is_inode_flag_set(i, FI_ACL_MODE)) ? \
  1695. (F2FS_I(i)->i_acl_mode) : ((i)->i_mode))
  1696. /* get offset of first page in next direct node */
  1697. #define PGOFS_OF_NEXT_DNODE(pgofs, inode) \
  1698. ((pgofs < ADDRS_PER_INODE(inode)) ? ADDRS_PER_INODE(inode) : \
  1699. (pgofs - ADDRS_PER_INODE(inode) + ADDRS_PER_BLOCK) / \
  1700. ADDRS_PER_BLOCK * ADDRS_PER_BLOCK + ADDRS_PER_INODE(inode))
  1701. /*
  1702. * file.c
  1703. */
  1704. int f2fs_sync_file(struct file *, loff_t, loff_t, int);
  1705. void truncate_data_blocks(struct dnode_of_data *);
  1706. int truncate_blocks(struct inode *, u64, bool);
  1707. int f2fs_truncate(struct inode *);
  1708. int f2fs_getattr(struct vfsmount *, struct dentry *, struct kstat *);
  1709. int f2fs_setattr(struct dentry *, struct iattr *);
  1710. int truncate_hole(struct inode *, pgoff_t, pgoff_t);
  1711. int truncate_data_blocks_range(struct dnode_of_data *, int);
  1712. long f2fs_ioctl(struct file *, unsigned int, unsigned long);
  1713. long f2fs_compat_ioctl(struct file *, unsigned int, unsigned long);
  1714. /*
  1715. * inode.c
  1716. */
  1717. void f2fs_set_inode_flags(struct inode *);
  1718. struct inode *f2fs_iget(struct super_block *, unsigned long);
  1719. struct inode *f2fs_iget_retry(struct super_block *, unsigned long);
  1720. int try_to_free_nats(struct f2fs_sb_info *, int);
  1721. int update_inode(struct inode *, struct page *);
  1722. int update_inode_page(struct inode *);
  1723. int f2fs_write_inode(struct inode *, struct writeback_control *);
  1724. void f2fs_evict_inode(struct inode *);
  1725. void handle_failed_inode(struct inode *);
  1726. /*
  1727. * namei.c
  1728. */
  1729. struct dentry *f2fs_get_parent(struct dentry *child);
  1730. /*
  1731. * dir.c
  1732. */
  1733. void set_de_type(struct f2fs_dir_entry *, umode_t);
  1734. unsigned char get_de_type(struct f2fs_dir_entry *);
  1735. struct f2fs_dir_entry *find_target_dentry(struct fscrypt_name *,
  1736. f2fs_hash_t, int *, struct f2fs_dentry_ptr *);
  1737. bool f2fs_fill_dentries(struct file *, void *, filldir_t,
  1738. struct f2fs_dentry_ptr *, unsigned int,
  1739. unsigned int, struct fscrypt_str *);
  1740. void do_make_empty_dir(struct inode *, struct inode *,
  1741. struct f2fs_dentry_ptr *);
  1742. struct page *init_inode_metadata(struct inode *, struct inode *,
  1743. const struct qstr *, const struct qstr *, struct page *);
  1744. void update_parent_metadata(struct inode *, struct inode *, unsigned int);
  1745. int room_for_filename(const void *, int, int);
  1746. void f2fs_drop_nlink(struct inode *, struct inode *);
  1747. struct f2fs_dir_entry *__f2fs_find_entry(struct inode *, struct fscrypt_name *,
  1748. struct page **);
  1749. struct f2fs_dir_entry *f2fs_find_entry(struct inode *, const struct qstr *,
  1750. struct page **);
  1751. struct f2fs_dir_entry *f2fs_parent_dir(struct inode *, struct page **);
  1752. ino_t f2fs_inode_by_name(struct inode *, struct qstr *, struct page **);
  1753. void f2fs_set_link(struct inode *, struct f2fs_dir_entry *,
  1754. struct page *, struct inode *);
  1755. int update_dent_inode(struct inode *, struct inode *, const struct qstr *);
  1756. void f2fs_update_dentry(nid_t ino, umode_t mode, struct f2fs_dentry_ptr *,
  1757. const struct qstr *, f2fs_hash_t , unsigned int);
  1758. int f2fs_add_regular_entry(struct inode *, const struct qstr *,
  1759. const struct qstr *, struct inode *, nid_t, umode_t);
  1760. int __f2fs_do_add_link(struct inode *, struct fscrypt_name*, struct inode *,
  1761. nid_t, umode_t);
  1762. int __f2fs_add_link(struct inode *, const struct qstr *, struct inode *, nid_t,
  1763. umode_t);
  1764. void f2fs_delete_entry(struct f2fs_dir_entry *, struct page *, struct inode *,
  1765. struct inode *);
  1766. int f2fs_do_tmpfile(struct inode *, struct inode *);
  1767. bool f2fs_empty_dir(struct inode *);
  1768. static inline int f2fs_add_link(struct dentry *dentry, struct inode *inode)
  1769. {
  1770. return __f2fs_add_link(d_inode(dentry->d_parent), &dentry->d_name,
  1771. inode, inode->i_ino, inode->i_mode);
  1772. }
  1773. /*
  1774. * super.c
  1775. */
  1776. int f2fs_inode_dirtied(struct inode *, bool);
  1777. void f2fs_inode_synced(struct inode *);
  1778. int f2fs_commit_super(struct f2fs_sb_info *, bool);
  1779. int f2fs_sync_fs(struct super_block *, int);
  1780. extern __printf(3, 4)
  1781. void f2fs_msg(struct super_block *, const char *, const char *, ...);
  1782. int sanity_check_ckpt(struct f2fs_sb_info *sbi);
  1783. /*
  1784. * hash.c
  1785. */
  1786. f2fs_hash_t f2fs_dentry_hash(const struct qstr *);
  1787. /*
  1788. * node.c
  1789. */
  1790. struct dnode_of_data;
  1791. struct node_info;
  1792. bool available_free_memory(struct f2fs_sb_info *, int);
  1793. int need_dentry_mark(struct f2fs_sb_info *, nid_t);
  1794. bool is_checkpointed_node(struct f2fs_sb_info *, nid_t);
  1795. bool need_inode_block_update(struct f2fs_sb_info *, nid_t);
  1796. void get_node_info(struct f2fs_sb_info *, nid_t, struct node_info *);
  1797. pgoff_t get_next_page_offset(struct dnode_of_data *, pgoff_t);
  1798. int get_dnode_of_data(struct dnode_of_data *, pgoff_t, int);
  1799. int truncate_inode_blocks(struct inode *, pgoff_t);
  1800. int truncate_xattr_node(struct inode *, struct page *);
  1801. int wait_on_node_pages_writeback(struct f2fs_sb_info *, nid_t);
  1802. int remove_inode_page(struct inode *);
  1803. struct page *new_inode_page(struct inode *);
  1804. struct page *new_node_page(struct dnode_of_data *, unsigned int, struct page *);
  1805. void ra_node_page(struct f2fs_sb_info *, nid_t);
  1806. struct page *get_node_page(struct f2fs_sb_info *, pgoff_t);
  1807. struct page *get_node_page_ra(struct page *, int);
  1808. void move_node_page(struct page *, int);
  1809. int fsync_node_pages(struct f2fs_sb_info *, struct inode *,
  1810. struct writeback_control *, bool);
  1811. int sync_node_pages(struct f2fs_sb_info *, struct writeback_control *);
  1812. void build_free_nids(struct f2fs_sb_info *, bool);
  1813. bool alloc_nid(struct f2fs_sb_info *, nid_t *);
  1814. void alloc_nid_done(struct f2fs_sb_info *, nid_t);
  1815. void alloc_nid_failed(struct f2fs_sb_info *, nid_t);
  1816. int try_to_free_nids(struct f2fs_sb_info *, int);
  1817. void recover_inline_xattr(struct inode *, struct page *);
  1818. void recover_xattr_data(struct inode *, struct page *, block_t);
  1819. int recover_inode_page(struct f2fs_sb_info *, struct page *);
  1820. int restore_node_summary(struct f2fs_sb_info *, unsigned int,
  1821. struct f2fs_summary_block *);
  1822. void flush_nat_entries(struct f2fs_sb_info *);
  1823. int build_node_manager(struct f2fs_sb_info *);
  1824. void destroy_node_manager(struct f2fs_sb_info *);
  1825. int __init create_node_manager_caches(void);
  1826. void destroy_node_manager_caches(void);
  1827. /*
  1828. * segment.c
  1829. */
  1830. void register_inmem_page(struct inode *, struct page *);
  1831. void drop_inmem_pages(struct inode *);
  1832. int commit_inmem_pages(struct inode *);
  1833. void f2fs_balance_fs(struct f2fs_sb_info *, bool);
  1834. void f2fs_balance_fs_bg(struct f2fs_sb_info *);
  1835. int f2fs_issue_flush(struct f2fs_sb_info *);
  1836. int create_flush_cmd_control(struct f2fs_sb_info *);
  1837. void destroy_flush_cmd_control(struct f2fs_sb_info *, bool);
  1838. void invalidate_blocks(struct f2fs_sb_info *, block_t);
  1839. bool is_checkpointed_data(struct f2fs_sb_info *, block_t);
  1840. void refresh_sit_entry(struct f2fs_sb_info *, block_t, block_t);
  1841. void clear_prefree_segments(struct f2fs_sb_info *, struct cp_control *);
  1842. void release_discard_addrs(struct f2fs_sb_info *);
  1843. int npages_for_summary_flush(struct f2fs_sb_info *, bool);
  1844. void allocate_new_segments(struct f2fs_sb_info *);
  1845. int f2fs_trim_fs(struct f2fs_sb_info *, struct fstrim_range *);
  1846. struct page *get_sum_page(struct f2fs_sb_info *, unsigned int);
  1847. void update_meta_page(struct f2fs_sb_info *, void *, block_t);
  1848. void write_meta_page(struct f2fs_sb_info *, struct page *);
  1849. void write_node_page(unsigned int, struct f2fs_io_info *);
  1850. void write_data_page(struct dnode_of_data *, struct f2fs_io_info *);
  1851. void rewrite_data_page(struct f2fs_io_info *);
  1852. void __f2fs_replace_block(struct f2fs_sb_info *, struct f2fs_summary *,
  1853. block_t, block_t, bool, bool);
  1854. void f2fs_replace_block(struct f2fs_sb_info *, struct dnode_of_data *,
  1855. block_t, block_t, unsigned char, bool, bool);
  1856. void allocate_data_block(struct f2fs_sb_info *, struct page *,
  1857. block_t, block_t *, struct f2fs_summary *, int);
  1858. void f2fs_wait_on_page_writeback(struct page *, enum page_type, bool);
  1859. void f2fs_wait_on_encrypted_page_writeback(struct f2fs_sb_info *, block_t);
  1860. void write_data_summaries(struct f2fs_sb_info *, block_t);
  1861. void write_node_summaries(struct f2fs_sb_info *, block_t);
  1862. int lookup_journal_in_cursum(struct f2fs_journal *, int, unsigned int, int);
  1863. void flush_sit_entries(struct f2fs_sb_info *, struct cp_control *);
  1864. int build_segment_manager(struct f2fs_sb_info *);
  1865. void destroy_segment_manager(struct f2fs_sb_info *);
  1866. int __init create_segment_manager_caches(void);
  1867. void destroy_segment_manager_caches(void);
  1868. /*
  1869. * checkpoint.c
  1870. */
  1871. void f2fs_stop_checkpoint(struct f2fs_sb_info *, bool);
  1872. struct page *grab_meta_page(struct f2fs_sb_info *, pgoff_t);
  1873. struct page *get_meta_page(struct f2fs_sb_info *, pgoff_t);
  1874. struct page *get_tmp_page(struct f2fs_sb_info *, pgoff_t);
  1875. bool is_valid_blkaddr(struct f2fs_sb_info *, block_t, int);
  1876. int ra_meta_pages(struct f2fs_sb_info *, block_t, int, int, bool);
  1877. void ra_meta_pages_cond(struct f2fs_sb_info *, pgoff_t);
  1878. long sync_meta_pages(struct f2fs_sb_info *, enum page_type, long);
  1879. void add_ino_entry(struct f2fs_sb_info *, nid_t, int type);
  1880. void remove_ino_entry(struct f2fs_sb_info *, nid_t, int type);
  1881. void release_ino_entry(struct f2fs_sb_info *, bool);
  1882. bool exist_written_data(struct f2fs_sb_info *, nid_t, int);
  1883. int f2fs_sync_inode_meta(struct f2fs_sb_info *);
  1884. int acquire_orphan_inode(struct f2fs_sb_info *);
  1885. void release_orphan_inode(struct f2fs_sb_info *);
  1886. void add_orphan_inode(struct inode *);
  1887. void remove_orphan_inode(struct f2fs_sb_info *, nid_t);
  1888. int recover_orphan_inodes(struct f2fs_sb_info *);
  1889. int get_valid_checkpoint(struct f2fs_sb_info *);
  1890. void update_dirty_page(struct inode *, struct page *);
  1891. void remove_dirty_inode(struct inode *);
  1892. int sync_dirty_inodes(struct f2fs_sb_info *, enum inode_type);
  1893. int write_checkpoint(struct f2fs_sb_info *, struct cp_control *);
  1894. void init_ino_entry_info(struct f2fs_sb_info *);
  1895. int __init create_checkpoint_caches(void);
  1896. void destroy_checkpoint_caches(void);
  1897. /*
  1898. * data.c
  1899. */
  1900. void f2fs_submit_merged_bio(struct f2fs_sb_info *, enum page_type, int);
  1901. void f2fs_submit_merged_bio_cond(struct f2fs_sb_info *, struct inode *,
  1902. struct page *, nid_t, enum page_type, int);
  1903. void f2fs_flush_merged_bios(struct f2fs_sb_info *);
  1904. int f2fs_submit_page_bio(struct f2fs_io_info *);
  1905. void f2fs_submit_page_mbio(struct f2fs_io_info *);
  1906. void set_data_blkaddr(struct dnode_of_data *);
  1907. void f2fs_update_data_blkaddr(struct dnode_of_data *, block_t);
  1908. int reserve_new_blocks(struct dnode_of_data *, blkcnt_t);
  1909. int reserve_new_block(struct dnode_of_data *);
  1910. int f2fs_get_block(struct dnode_of_data *, pgoff_t);
  1911. int f2fs_preallocate_blocks(struct inode *, loff_t, size_t, bool);
  1912. int f2fs_reserve_block(struct dnode_of_data *, pgoff_t);
  1913. struct page *get_read_data_page(struct inode *, pgoff_t, int, bool);
  1914. struct page *find_data_page(struct inode *, pgoff_t);
  1915. struct page *get_lock_data_page(struct inode *, pgoff_t, bool);
  1916. struct page *get_new_data_page(struct inode *, struct page *, pgoff_t, bool);
  1917. int do_write_data_page(struct f2fs_io_info *);
  1918. int f2fs_map_blocks(struct inode *, struct f2fs_map_blocks *, int, int);
  1919. int f2fs_fiemap(struct inode *inode, struct fiemap_extent_info *, u64, u64);
  1920. void f2fs_set_page_dirty_nobuffers(struct page *);
  1921. void f2fs_invalidate_page(struct page *, unsigned long);
  1922. int f2fs_release_page(struct page *, gfp_t);
  1923. /*
  1924. * gc.c
  1925. */
  1926. int start_gc_thread(struct f2fs_sb_info *);
  1927. void stop_gc_thread(struct f2fs_sb_info *);
  1928. block_t start_bidx_of_node(unsigned int, struct inode *);
  1929. int f2fs_gc(struct f2fs_sb_info *, bool, bool);
  1930. void build_gc_manager(struct f2fs_sb_info *);
  1931. /*
  1932. * recovery.c
  1933. */
  1934. int recover_fsync_data(struct f2fs_sb_info *, bool);
  1935. bool space_for_roll_forward(struct f2fs_sb_info *);
  1936. /*
  1937. * debug.c
  1938. */
  1939. #ifdef CONFIG_F2FS_STAT_FS
  1940. struct f2fs_stat_info {
  1941. struct list_head stat_list;
  1942. struct f2fs_sb_info *sbi;
  1943. int all_area_segs, sit_area_segs, nat_area_segs, ssa_area_segs;
  1944. int main_area_segs, main_area_sections, main_area_zones;
  1945. unsigned long long hit_largest, hit_cached, hit_rbtree;
  1946. unsigned long long hit_total, total_ext;
  1947. int ext_tree, zombie_tree, ext_node;
  1948. int ndirty_node, ndirty_dent, ndirty_meta, ndirty_data, ndirty_imeta;
  1949. int inmem_pages;
  1950. unsigned int ndirty_dirs, ndirty_files, ndirty_all;
  1951. int nats, dirty_nats, sits, dirty_sits, free_nids, alloc_nids;
  1952. int total_count, utilization;
  1953. int bg_gc, nr_wb_cp_data, nr_wb_data;
  1954. int inline_xattr, inline_inode, inline_dir, orphans;
  1955. unsigned int valid_count, valid_node_count, valid_inode_count, discard_blks;
  1956. unsigned int bimodal, avg_vblocks;
  1957. int util_free, util_valid, util_invalid;
  1958. int rsvd_segs, overp_segs;
  1959. int dirty_count, node_pages, meta_pages;
  1960. int prefree_count, call_count, cp_count, bg_cp_count;
  1961. int tot_segs, node_segs, data_segs, free_segs, free_secs;
  1962. int bg_node_segs, bg_data_segs;
  1963. int tot_blks, data_blks, node_blks;
  1964. int bg_data_blks, bg_node_blks;
  1965. int curseg[NR_CURSEG_TYPE];
  1966. int cursec[NR_CURSEG_TYPE];
  1967. int curzone[NR_CURSEG_TYPE];
  1968. unsigned int segment_count[2];
  1969. unsigned int block_count[2];
  1970. unsigned int inplace_count;
  1971. unsigned long long base_mem, cache_mem, page_mem;
  1972. };
  1973. static inline struct f2fs_stat_info *F2FS_STAT(struct f2fs_sb_info *sbi)
  1974. {
  1975. return (struct f2fs_stat_info *)sbi->stat_info;
  1976. }
  1977. #define stat_inc_cp_count(si) ((si)->cp_count++)
  1978. #define stat_inc_bg_cp_count(si) ((si)->bg_cp_count++)
  1979. #define stat_inc_call_count(si) ((si)->call_count++)
  1980. #define stat_inc_bggc_count(sbi) ((sbi)->bg_gc++)
  1981. #define stat_inc_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]++)
  1982. #define stat_dec_dirty_inode(sbi, type) ((sbi)->ndirty_inode[type]--)
  1983. #define stat_inc_total_hit(sbi) (atomic64_inc(&(sbi)->total_hit_ext))
  1984. #define stat_inc_rbtree_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_rbtree))
  1985. #define stat_inc_largest_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_largest))
  1986. #define stat_inc_cached_node_hit(sbi) (atomic64_inc(&(sbi)->read_hit_cached))
  1987. #define stat_inc_inline_xattr(inode) \
  1988. do { \
  1989. if (f2fs_has_inline_xattr(inode)) \
  1990. (atomic_inc(&F2FS_I_SB(inode)->inline_xattr)); \
  1991. } while (0)
  1992. #define stat_dec_inline_xattr(inode) \
  1993. do { \
  1994. if (f2fs_has_inline_xattr(inode)) \
  1995. (atomic_dec(&F2FS_I_SB(inode)->inline_xattr)); \
  1996. } while (0)
  1997. #define stat_inc_inline_inode(inode) \
  1998. do { \
  1999. if (f2fs_has_inline_data(inode)) \
  2000. (atomic_inc(&F2FS_I_SB(inode)->inline_inode)); \
  2001. } while (0)
  2002. #define stat_dec_inline_inode(inode) \
  2003. do { \
  2004. if (f2fs_has_inline_data(inode)) \
  2005. (atomic_dec(&F2FS_I_SB(inode)->inline_inode)); \
  2006. } while (0)
  2007. #define stat_inc_inline_dir(inode) \
  2008. do { \
  2009. if (f2fs_has_inline_dentry(inode)) \
  2010. (atomic_inc(&F2FS_I_SB(inode)->inline_dir)); \
  2011. } while (0)
  2012. #define stat_dec_inline_dir(inode) \
  2013. do { \
  2014. if (f2fs_has_inline_dentry(inode)) \
  2015. (atomic_dec(&F2FS_I_SB(inode)->inline_dir)); \
  2016. } while (0)
  2017. #define stat_inc_seg_type(sbi, curseg) \
  2018. ((sbi)->segment_count[(curseg)->alloc_type]++)
  2019. #define stat_inc_block_count(sbi, curseg) \
  2020. ((sbi)->block_count[(curseg)->alloc_type]++)
  2021. #define stat_inc_inplace_blocks(sbi) \
  2022. (atomic_inc(&(sbi)->inplace_count))
  2023. #define stat_inc_seg_count(sbi, type, gc_type) \
  2024. do { \
  2025. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  2026. (si)->tot_segs++; \
  2027. if (type == SUM_TYPE_DATA) { \
  2028. si->data_segs++; \
  2029. si->bg_data_segs += (gc_type == BG_GC) ? 1 : 0; \
  2030. } else { \
  2031. si->node_segs++; \
  2032. si->bg_node_segs += (gc_type == BG_GC) ? 1 : 0; \
  2033. } \
  2034. } while (0)
  2035. #define stat_inc_tot_blk_count(si, blks) \
  2036. (si->tot_blks += (blks))
  2037. #define stat_inc_data_blk_count(sbi, blks, gc_type) \
  2038. do { \
  2039. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  2040. stat_inc_tot_blk_count(si, blks); \
  2041. si->data_blks += (blks); \
  2042. si->bg_data_blks += (gc_type == BG_GC) ? (blks) : 0; \
  2043. } while (0)
  2044. #define stat_inc_node_blk_count(sbi, blks, gc_type) \
  2045. do { \
  2046. struct f2fs_stat_info *si = F2FS_STAT(sbi); \
  2047. stat_inc_tot_blk_count(si, blks); \
  2048. si->node_blks += (blks); \
  2049. si->bg_node_blks += (gc_type == BG_GC) ? (blks) : 0; \
  2050. } while (0)
  2051. int f2fs_build_stats(struct f2fs_sb_info *);
  2052. void f2fs_destroy_stats(struct f2fs_sb_info *);
  2053. int __init f2fs_create_root_stats(void);
  2054. void f2fs_destroy_root_stats(void);
  2055. #else
  2056. #define stat_inc_cp_count(si)
  2057. #define stat_inc_bg_cp_count(si)
  2058. #define stat_inc_call_count(si)
  2059. #define stat_inc_bggc_count(si)
  2060. #define stat_inc_dirty_inode(sbi, type)
  2061. #define stat_dec_dirty_inode(sbi, type)
  2062. #define stat_inc_total_hit(sb)
  2063. #define stat_inc_rbtree_node_hit(sb)
  2064. #define stat_inc_largest_node_hit(sbi)
  2065. #define stat_inc_cached_node_hit(sbi)
  2066. #define stat_inc_inline_xattr(inode)
  2067. #define stat_dec_inline_xattr(inode)
  2068. #define stat_inc_inline_inode(inode)
  2069. #define stat_dec_inline_inode(inode)
  2070. #define stat_inc_inline_dir(inode)
  2071. #define stat_dec_inline_dir(inode)
  2072. #define stat_inc_seg_type(sbi, curseg)
  2073. #define stat_inc_block_count(sbi, curseg)
  2074. #define stat_inc_inplace_blocks(sbi)
  2075. #define stat_inc_seg_count(sbi, type, gc_type)
  2076. #define stat_inc_tot_blk_count(si, blks)
  2077. #define stat_inc_data_blk_count(sbi, blks, gc_type)
  2078. #define stat_inc_node_blk_count(sbi, blks, gc_type)
  2079. static inline int f2fs_build_stats(struct f2fs_sb_info *sbi) { return 0; }
  2080. static inline void f2fs_destroy_stats(struct f2fs_sb_info *sbi) { }
  2081. static inline int __init f2fs_create_root_stats(void) { return 0; }
  2082. static inline void f2fs_destroy_root_stats(void) { }
  2083. #endif
  2084. extern const struct file_operations f2fs_dir_operations;
  2085. extern const struct file_operations f2fs_file_operations;
  2086. extern const struct inode_operations f2fs_file_inode_operations;
  2087. extern const struct address_space_operations f2fs_dblock_aops;
  2088. extern const struct address_space_operations f2fs_node_aops;
  2089. extern const struct address_space_operations f2fs_meta_aops;
  2090. extern const struct inode_operations f2fs_dir_inode_operations;
  2091. extern const struct inode_operations f2fs_symlink_inode_operations;
  2092. extern const struct inode_operations f2fs_encrypted_symlink_inode_operations;
  2093. extern const struct inode_operations f2fs_special_inode_operations;
  2094. extern struct kmem_cache *inode_entry_slab;
  2095. /*
  2096. * inline.c
  2097. */
  2098. bool f2fs_may_inline_data(struct inode *);
  2099. bool f2fs_may_inline_dentry(struct inode *);
  2100. void read_inline_data(struct page *, struct page *);
  2101. bool truncate_inline_inode(struct page *, u64);
  2102. int f2fs_read_inline_data(struct inode *, struct page *);
  2103. int f2fs_convert_inline_page(struct dnode_of_data *, struct page *);
  2104. int f2fs_convert_inline_inode(struct inode *);
  2105. int f2fs_write_inline_data(struct inode *, struct page *);
  2106. bool recover_inline_data(struct inode *, struct page *);
  2107. struct f2fs_dir_entry *find_in_inline_dir(struct inode *,
  2108. struct fscrypt_name *, struct page **);
  2109. int make_empty_inline_dir(struct inode *inode, struct inode *, struct page *);
  2110. int f2fs_add_inline_entry(struct inode *, const struct qstr *,
  2111. const struct qstr *, struct inode *, nid_t, umode_t);
  2112. void f2fs_delete_inline_entry(struct f2fs_dir_entry *, struct page *,
  2113. struct inode *, struct inode *);
  2114. bool f2fs_empty_inline_dir(struct inode *);
  2115. int f2fs_read_inline_dir(struct file *, void *, filldir_t,
  2116. struct fscrypt_str *);
  2117. int f2fs_inline_data_fiemap(struct inode *,
  2118. struct fiemap_extent_info *, __u64, __u64);
  2119. /*
  2120. * shrinker.c
  2121. */
  2122. int f2fs_shrink_count(struct shrinker *, struct shrink_control *);
  2123. int f2fs_shrink_scan(struct shrinker *, struct shrink_control *);
  2124. void f2fs_join_shrinker(struct f2fs_sb_info *);
  2125. void f2fs_leave_shrinker(struct f2fs_sb_info *);
  2126. /*
  2127. * extent_cache.c
  2128. */
  2129. unsigned int f2fs_shrink_extent_tree(struct f2fs_sb_info *, int);
  2130. bool f2fs_init_extent_tree(struct inode *, struct f2fs_extent *);
  2131. void f2fs_drop_extent_tree(struct inode *);
  2132. unsigned int f2fs_destroy_extent_node(struct inode *);
  2133. void f2fs_destroy_extent_tree(struct inode *);
  2134. bool f2fs_lookup_extent_cache(struct inode *, pgoff_t, struct extent_info *);
  2135. void f2fs_update_extent_cache(struct dnode_of_data *);
  2136. void f2fs_update_extent_cache_range(struct dnode_of_data *dn,
  2137. pgoff_t, block_t, unsigned int);
  2138. void init_extent_cache_info(struct f2fs_sb_info *);
  2139. int __init create_extent_cache(void);
  2140. void destroy_extent_cache(void);
  2141. /*
  2142. * crypto support
  2143. */
  2144. static inline bool f2fs_encrypted_inode(struct inode *inode)
  2145. {
  2146. return file_is_encrypt(inode);
  2147. }
  2148. static inline void f2fs_set_encrypted_inode(struct inode *inode)
  2149. {
  2150. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  2151. file_set_encrypt(inode);
  2152. #endif
  2153. }
  2154. static inline bool f2fs_bio_encrypted(struct bio *bio)
  2155. {
  2156. return bio->bi_private != NULL;
  2157. }
  2158. static inline int f2fs_sb_has_crypto(struct super_block *sb)
  2159. {
  2160. return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_ENCRYPT);
  2161. }
  2162. static inline int f2fs_sb_mounted_blkzoned(struct super_block *sb)
  2163. {
  2164. return F2FS_HAS_FEATURE(sb, F2FS_FEATURE_BLKZONED);
  2165. }
  2166. #ifdef CONFIG_BLK_DEV_ZONED
  2167. static inline int get_blkz_type(struct f2fs_sb_info *sbi,
  2168. block_t blkaddr)
  2169. {
  2170. unsigned int zno = blkaddr >> sbi->log_blocks_per_blkz;
  2171. return sbi->blkz_type[zno];
  2172. }
  2173. #endif
  2174. static inline bool f2fs_discard_en(struct f2fs_sb_info *sbi)
  2175. {
  2176. struct request_queue *q = bdev_get_queue(sbi->sb->s_bdev);
  2177. return blk_queue_discard(q) || f2fs_sb_mounted_blkzoned(sbi->sb);
  2178. }
  2179. static inline void set_opt_mode(struct f2fs_sb_info *sbi, unsigned int mt)
  2180. {
  2181. clear_opt(sbi, ADAPTIVE);
  2182. clear_opt(sbi, LFS);
  2183. switch (mt) {
  2184. case F2FS_MOUNT_ADAPTIVE:
  2185. set_opt(sbi, ADAPTIVE);
  2186. break;
  2187. case F2FS_MOUNT_LFS:
  2188. set_opt(sbi, LFS);
  2189. break;
  2190. }
  2191. }
  2192. static inline bool f2fs_may_encrypt(struct inode *inode)
  2193. {
  2194. #ifdef CONFIG_F2FS_FS_ENCRYPTION
  2195. mode_t mode = inode->i_mode;
  2196. return (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode));
  2197. #else
  2198. return 0;
  2199. #endif
  2200. }
  2201. #ifndef CONFIG_F2FS_FS_ENCRYPTION
  2202. #define fscrypt_set_d_op(i)
  2203. #define fscrypt_get_ctx fscrypt_notsupp_get_ctx
  2204. #define fscrypt_release_ctx fscrypt_notsupp_release_ctx
  2205. #define fscrypt_encrypt_page fscrypt_notsupp_encrypt_page
  2206. #define fscrypt_decrypt_page fscrypt_notsupp_decrypt_page
  2207. #define fscrypt_decrypt_bio_pages fscrypt_notsupp_decrypt_bio_pages
  2208. #define fscrypt_pullback_bio_page fscrypt_notsupp_pullback_bio_page
  2209. #define fscrypt_restore_control_page fscrypt_notsupp_restore_control_page
  2210. #define fscrypt_zeroout_range fscrypt_notsupp_zeroout_range
  2211. #define fscrypt_process_policy fscrypt_notsupp_process_policy
  2212. #define fscrypt_get_policy fscrypt_notsupp_get_policy
  2213. #define fscrypt_has_permitted_context fscrypt_notsupp_has_permitted_context
  2214. #define fscrypt_inherit_context fscrypt_notsupp_inherit_context
  2215. #define fscrypt_get_encryption_info fscrypt_notsupp_get_encryption_info
  2216. #define fscrypt_put_encryption_info fscrypt_notsupp_put_encryption_info
  2217. #define fscrypt_setup_filename fscrypt_notsupp_setup_filename
  2218. #define fscrypt_free_filename fscrypt_notsupp_free_filename
  2219. #define fscrypt_fname_encrypted_size fscrypt_notsupp_fname_encrypted_size
  2220. #define fscrypt_fname_alloc_buffer fscrypt_notsupp_fname_alloc_buffer
  2221. #define fscrypt_fname_free_buffer fscrypt_notsupp_fname_free_buffer
  2222. #define fscrypt_fname_disk_to_usr fscrypt_notsupp_fname_disk_to_usr
  2223. #define fscrypt_fname_usr_to_disk fscrypt_notsupp_fname_usr_to_disk
  2224. #endif
  2225. #endif