ext4.h 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * ext4.h
  4. *
  5. * Copyright (C) 1992, 1993, 1994, 1995
  6. * Remy Card (card@masi.ibp.fr)
  7. * Laboratoire MASI - Institut Blaise Pascal
  8. * Universite Pierre et Marie Curie (Paris VI)
  9. *
  10. * from
  11. *
  12. * linux/include/linux/minix_fs.h
  13. *
  14. * Copyright (C) 1991, 1992 Linus Torvalds
  15. */
  16. #ifndef _EXT4_H
  17. #define _EXT4_H
  18. #include <linux/refcount.h>
  19. #include <linux/types.h>
  20. #include <linux/blkdev.h>
  21. #include <linux/magic.h>
  22. #include <linux/jbd2.h>
  23. #include <linux/quota.h>
  24. #include <linux/rwsem.h>
  25. #include <linux/rbtree.h>
  26. #include <linux/seqlock.h>
  27. #include <linux/mutex.h>
  28. #include <linux/timer.h>
  29. #include <linux/wait.h>
  30. #include <linux/sched/signal.h>
  31. #include <linux/blockgroup_lock.h>
  32. #include <linux/percpu_counter.h>
  33. #include <linux/ratelimit.h>
  34. #include <crypto/hash.h>
  35. #include <linux/falloc.h>
  36. #include <linux/percpu-rwsem.h>
  37. #include <linux/fiemap.h>
  38. #ifdef __KERNEL__
  39. #include <linux/compat.h>
  40. #endif
  41. #include <uapi/linux/ext4.h>
  42. #include <linux/fscrypt.h>
  43. #include <linux/fsverity.h>
  44. #include <linux/compiler.h>
  45. /*
  46. * The fourth extended filesystem constants/structures
  47. */
  48. /*
  49. * with AGGRESSIVE_CHECK allocator runs consistency checks over
  50. * structures. these checks slow things down a lot
  51. */
  52. #define AGGRESSIVE_CHECK__
  53. /*
  54. * with DOUBLE_CHECK defined mballoc creates persistent in-core
  55. * bitmaps, maintains and uses them to check for double allocations
  56. */
  57. #define DOUBLE_CHECK__
  58. /*
  59. * Define EXT4FS_DEBUG to produce debug messages
  60. */
  61. #undef EXT4FS_DEBUG
  62. /*
  63. * Debug code
  64. */
  65. #ifdef EXT4FS_DEBUG
  66. #define ext4_debug(f, a...) \
  67. do { \
  68. printk(KERN_DEBUG "EXT4-fs DEBUG (%s, %d): %s:", \
  69. __FILE__, __LINE__, __func__); \
  70. printk(KERN_DEBUG f, ## a); \
  71. } while (0)
  72. #else
  73. #define ext4_debug(fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  74. #endif
  75. /*
  76. * Turn on EXT_DEBUG to enable ext4_ext_show_path/leaf/move in extents.c
  77. */
  78. #define EXT_DEBUG__
  79. /*
  80. * Dynamic printk for controlled extents debugging.
  81. */
  82. #ifdef CONFIG_EXT4_DEBUG
  83. #define ext_debug(ino, fmt, ...) \
  84. pr_debug("[%s/%d] EXT4-fs (%s): ino %lu: (%s, %d): %s:" fmt, \
  85. current->comm, task_pid_nr(current), \
  86. ino->i_sb->s_id, ino->i_ino, __FILE__, __LINE__, \
  87. __func__, ##__VA_ARGS__)
  88. #else
  89. #define ext_debug(ino, fmt, ...) no_printk(fmt, ##__VA_ARGS__)
  90. #endif
  91. #define ASSERT(assert) \
  92. do { \
  93. if (unlikely(!(assert))) { \
  94. printk(KERN_EMERG \
  95. "Assertion failure in %s() at %s:%d: '%s'\n", \
  96. __func__, __FILE__, __LINE__, #assert); \
  97. BUG(); \
  98. } \
  99. } while (0)
  100. /* data type for block offset of block group */
  101. typedef int ext4_grpblk_t;
  102. /* data type for filesystem-wide blocks number */
  103. typedef unsigned long long ext4_fsblk_t;
  104. /* data type for file logical block number */
  105. typedef __u32 ext4_lblk_t;
  106. /* data type for block group number */
  107. typedef unsigned int ext4_group_t;
  108. enum SHIFT_DIRECTION {
  109. SHIFT_LEFT = 0,
  110. SHIFT_RIGHT,
  111. };
  112. /*
  113. * For each criteria, mballoc has slightly different way of finding
  114. * the required blocks nad usually, higher the criteria the slower the
  115. * allocation. We start at lower criterias and keep falling back to
  116. * higher ones if we are not able to find any blocks. Lower (earlier)
  117. * criteria are faster.
  118. */
  119. enum criteria {
  120. /*
  121. * Used when number of blocks needed is a power of 2. This
  122. * doesn't trigger any disk IO except prefetch and is the
  123. * fastest criteria.
  124. */
  125. CR_POWER2_ALIGNED,
  126. /*
  127. * Tries to lookup in-memory data structures to find the most
  128. * suitable group that satisfies goal request. No disk IO
  129. * except block prefetch.
  130. */
  131. CR_GOAL_LEN_FAST,
  132. /*
  133. * Same as CR_GOAL_LEN_FAST but is allowed to reduce the goal
  134. * length to the best available length for faster allocation.
  135. */
  136. CR_BEST_AVAIL_LEN,
  137. /*
  138. * Reads each block group sequentially, performing disk IO if
  139. * necessary, to find find_suitable block group. Tries to
  140. * allocate goal length but might trim the request if nothing
  141. * is found after enough tries.
  142. */
  143. CR_GOAL_LEN_SLOW,
  144. /*
  145. * Finds the first free set of blocks and allocates
  146. * those. This is only used in rare cases when
  147. * CR_GOAL_LEN_SLOW also fails to allocate anything.
  148. */
  149. CR_ANY_FREE,
  150. /*
  151. * Number of criterias defined.
  152. */
  153. EXT4_MB_NUM_CRS
  154. };
  155. /*
  156. * Flags used in mballoc's allocation_context flags field.
  157. *
  158. * Also used to show what's going on for debugging purposes when the
  159. * flag field is exported via the traceport interface
  160. */
  161. /* prefer goal again. length */
  162. #define EXT4_MB_HINT_MERGE 0x0001
  163. /* blocks already reserved */
  164. #define EXT4_MB_HINT_RESERVED 0x0002
  165. /* metadata is being allocated */
  166. #define EXT4_MB_HINT_METADATA 0x0004
  167. /* first blocks in the file */
  168. #define EXT4_MB_HINT_FIRST 0x0008
  169. /* search for the best chunk */
  170. #define EXT4_MB_HINT_BEST 0x0010
  171. /* data is being allocated */
  172. #define EXT4_MB_HINT_DATA 0x0020
  173. /* don't preallocate (for tails) */
  174. #define EXT4_MB_HINT_NOPREALLOC 0x0040
  175. /* allocate for locality group */
  176. #define EXT4_MB_HINT_GROUP_ALLOC 0x0080
  177. /* allocate goal blocks or none */
  178. #define EXT4_MB_HINT_GOAL_ONLY 0x0100
  179. /* goal is meaningful */
  180. #define EXT4_MB_HINT_TRY_GOAL 0x0200
  181. /* blocks already pre-reserved by delayed allocation */
  182. #define EXT4_MB_DELALLOC_RESERVED 0x0400
  183. /* We are doing stream allocation */
  184. #define EXT4_MB_STREAM_ALLOC 0x0800
  185. /* Use reserved root blocks if needed */
  186. #define EXT4_MB_USE_ROOT_BLOCKS 0x1000
  187. /* Use blocks from reserved pool */
  188. #define EXT4_MB_USE_RESERVED 0x2000
  189. /* Do strict check for free blocks while retrying block allocation */
  190. #define EXT4_MB_STRICT_CHECK 0x4000
  191. /* Large fragment size list lookup succeeded at least once for
  192. * CR_POWER2_ALIGNED */
  193. #define EXT4_MB_CR_POWER2_ALIGNED_OPTIMIZED 0x8000
  194. /* Avg fragment size rb tree lookup succeeded at least once for
  195. * CR_GOAL_LEN_FAST */
  196. #define EXT4_MB_CR_GOAL_LEN_FAST_OPTIMIZED 0x00010000
  197. /* Avg fragment size rb tree lookup succeeded at least once for
  198. * CR_BEST_AVAIL_LEN */
  199. #define EXT4_MB_CR_BEST_AVAIL_LEN_OPTIMIZED 0x00020000
  200. struct ext4_allocation_request {
  201. /* target inode for block we're allocating */
  202. struct inode *inode;
  203. /* how many blocks we want to allocate */
  204. unsigned int len;
  205. /* logical block in target inode */
  206. ext4_lblk_t logical;
  207. /* the closest logical allocated block to the left */
  208. ext4_lblk_t lleft;
  209. /* the closest logical allocated block to the right */
  210. ext4_lblk_t lright;
  211. /* phys. target (a hint) */
  212. ext4_fsblk_t goal;
  213. /* phys. block for the closest logical allocated block to the left */
  214. ext4_fsblk_t pleft;
  215. /* phys. block for the closest logical allocated block to the right */
  216. ext4_fsblk_t pright;
  217. /* flags. see above EXT4_MB_HINT_* */
  218. unsigned int flags;
  219. };
  220. /*
  221. * Logical to physical block mapping, used by ext4_map_blocks()
  222. *
  223. * This structure is used to pass requests into ext4_map_blocks() as
  224. * well as to store the information returned by ext4_map_blocks(). It
  225. * takes less room on the stack than a struct buffer_head.
  226. */
  227. #define EXT4_MAP_NEW BIT(BH_New)
  228. #define EXT4_MAP_MAPPED BIT(BH_Mapped)
  229. #define EXT4_MAP_UNWRITTEN BIT(BH_Unwritten)
  230. #define EXT4_MAP_BOUNDARY BIT(BH_Boundary)
  231. #define EXT4_MAP_DELAYED BIT(BH_Delay)
  232. #define EXT4_MAP_FLAGS (EXT4_MAP_NEW | EXT4_MAP_MAPPED |\
  233. EXT4_MAP_UNWRITTEN | EXT4_MAP_BOUNDARY |\
  234. EXT4_MAP_DELAYED)
  235. struct ext4_map_blocks {
  236. ext4_fsblk_t m_pblk;
  237. ext4_lblk_t m_lblk;
  238. unsigned int m_len;
  239. unsigned int m_flags;
  240. };
  241. /*
  242. * Block validity checking, system zone rbtree.
  243. */
  244. struct ext4_system_blocks {
  245. struct rb_root root;
  246. struct rcu_head rcu;
  247. };
  248. /*
  249. * Flags for ext4_io_end->flags
  250. */
  251. #define EXT4_IO_END_UNWRITTEN 0x0001
  252. #define EXT4_IO_END_FAILED 0x0002
  253. struct ext4_io_end_vec {
  254. struct list_head list; /* list of io_end_vec */
  255. loff_t offset; /* offset in the file */
  256. ssize_t size; /* size of the extent */
  257. };
  258. /*
  259. * For converting unwritten extents on a work queue. 'handle' is used for
  260. * buffered writeback.
  261. */
  262. typedef struct ext4_io_end {
  263. struct list_head list; /* per-file finished IO list */
  264. handle_t *handle; /* handle reserved for extent
  265. * conversion */
  266. struct inode *inode; /* file being written to */
  267. struct bio *bio; /* Linked list of completed
  268. * bios covering the extent */
  269. unsigned int flag; /* unwritten or not */
  270. refcount_t count; /* reference counter */
  271. struct list_head list_vec; /* list of ext4_io_end_vec */
  272. } ext4_io_end_t;
  273. struct ext4_io_submit {
  274. struct writeback_control *io_wbc;
  275. struct bio *io_bio;
  276. ext4_io_end_t *io_end;
  277. sector_t io_next_block;
  278. };
  279. /*
  280. * Special inodes numbers
  281. */
  282. #define EXT4_BAD_INO 1 /* Bad blocks inode */
  283. #define EXT4_ROOT_INO 2 /* Root inode */
  284. #define EXT4_USR_QUOTA_INO 3 /* User quota inode */
  285. #define EXT4_GRP_QUOTA_INO 4 /* Group quota inode */
  286. #define EXT4_BOOT_LOADER_INO 5 /* Boot loader inode */
  287. #define EXT4_UNDEL_DIR_INO 6 /* Undelete directory inode */
  288. #define EXT4_RESIZE_INO 7 /* Reserved group descriptors inode */
  289. #define EXT4_JOURNAL_INO 8 /* Journal inode */
  290. /* First non-reserved inode for old ext4 filesystems */
  291. #define EXT4_GOOD_OLD_FIRST_INO 11
  292. /*
  293. * Maximal count of links to a file
  294. */
  295. #define EXT4_LINK_MAX 65000
  296. /*
  297. * Macro-instructions used to manage several block sizes
  298. */
  299. #define EXT4_MIN_BLOCK_SIZE 1024
  300. #define EXT4_MAX_BLOCK_SIZE 65536
  301. #define EXT4_MIN_BLOCK_LOG_SIZE 10
  302. #define EXT4_MAX_BLOCK_LOG_SIZE 16
  303. #define EXT4_MAX_CLUSTER_LOG_SIZE 30
  304. #ifdef __KERNEL__
  305. # define EXT4_BLOCK_SIZE(s) ((s)->s_blocksize)
  306. #else
  307. # define EXT4_BLOCK_SIZE(s) (EXT4_MIN_BLOCK_SIZE << (s)->s_log_block_size)
  308. #endif
  309. #define EXT4_ADDR_PER_BLOCK(s) (EXT4_BLOCK_SIZE(s) / sizeof(__u32))
  310. #define EXT4_CLUSTER_SIZE(s) (EXT4_BLOCK_SIZE(s) << \
  311. EXT4_SB(s)->s_cluster_bits)
  312. #ifdef __KERNEL__
  313. # define EXT4_BLOCK_SIZE_BITS(s) ((s)->s_blocksize_bits)
  314. # define EXT4_CLUSTER_BITS(s) (EXT4_SB(s)->s_cluster_bits)
  315. #else
  316. # define EXT4_BLOCK_SIZE_BITS(s) ((s)->s_log_block_size + 10)
  317. #endif
  318. #ifdef __KERNEL__
  319. #define EXT4_ADDR_PER_BLOCK_BITS(s) (EXT4_SB(s)->s_addr_per_block_bits)
  320. #define EXT4_INODE_SIZE(s) (EXT4_SB(s)->s_inode_size)
  321. #define EXT4_FIRST_INO(s) (EXT4_SB(s)->s_first_ino)
  322. #else
  323. #define EXT4_INODE_SIZE(s) (((s)->s_rev_level == EXT4_GOOD_OLD_REV) ? \
  324. EXT4_GOOD_OLD_INODE_SIZE : \
  325. (s)->s_inode_size)
  326. #define EXT4_FIRST_INO(s) (((s)->s_rev_level == EXT4_GOOD_OLD_REV) ? \
  327. EXT4_GOOD_OLD_FIRST_INO : \
  328. (s)->s_first_ino)
  329. #endif
  330. #define EXT4_BLOCK_ALIGN(size, blkbits) ALIGN((size), (1 << (blkbits)))
  331. #define EXT4_MAX_BLOCKS(size, offset, blkbits) \
  332. ((EXT4_BLOCK_ALIGN(size + offset, blkbits) >> blkbits) - (offset >> \
  333. blkbits))
  334. #define EXT4_B_TO_LBLK(inode, offset) \
  335. (round_up((offset), i_blocksize(inode)) >> (inode)->i_blkbits)
  336. /* Translate a block number to a cluster number */
  337. #define EXT4_B2C(sbi, blk) ((blk) >> (sbi)->s_cluster_bits)
  338. /* Translate a cluster number to a block number */
  339. #define EXT4_C2B(sbi, cluster) ((cluster) << (sbi)->s_cluster_bits)
  340. /* Translate # of blks to # of clusters */
  341. #define EXT4_NUM_B2C(sbi, blks) (((blks) + (sbi)->s_cluster_ratio - 1) >> \
  342. (sbi)->s_cluster_bits)
  343. /* Mask out the low bits to get the starting block of the cluster */
  344. #define EXT4_PBLK_CMASK(s, pblk) ((pblk) & \
  345. ~((ext4_fsblk_t) (s)->s_cluster_ratio - 1))
  346. #define EXT4_LBLK_CMASK(s, lblk) ((lblk) & \
  347. ~((ext4_lblk_t) (s)->s_cluster_ratio - 1))
  348. /* Fill in the low bits to get the last block of the cluster */
  349. #define EXT4_LBLK_CFILL(sbi, lblk) ((lblk) | \
  350. ((ext4_lblk_t) (sbi)->s_cluster_ratio - 1))
  351. /* Get the cluster offset */
  352. #define EXT4_PBLK_COFF(s, pblk) ((pblk) & \
  353. ((ext4_fsblk_t) (s)->s_cluster_ratio - 1))
  354. #define EXT4_LBLK_COFF(s, lblk) ((lblk) & \
  355. ((ext4_lblk_t) (s)->s_cluster_ratio - 1))
  356. /*
  357. * Structure of a blocks group descriptor
  358. */
  359. struct ext4_group_desc
  360. {
  361. __le32 bg_block_bitmap_lo; /* Blocks bitmap block */
  362. __le32 bg_inode_bitmap_lo; /* Inodes bitmap block */
  363. __le32 bg_inode_table_lo; /* Inodes table block */
  364. __le16 bg_free_blocks_count_lo;/* Free blocks count */
  365. __le16 bg_free_inodes_count_lo;/* Free inodes count */
  366. __le16 bg_used_dirs_count_lo; /* Directories count */
  367. __le16 bg_flags; /* EXT4_BG_flags (INODE_UNINIT, etc) */
  368. __le32 bg_exclude_bitmap_lo; /* Exclude bitmap for snapshots */
  369. __le16 bg_block_bitmap_csum_lo;/* crc32c(s_uuid+grp_num+bbitmap) LE */
  370. __le16 bg_inode_bitmap_csum_lo;/* crc32c(s_uuid+grp_num+ibitmap) LE */
  371. __le16 bg_itable_unused_lo; /* Unused inodes count */
  372. __le16 bg_checksum; /* crc16(sb_uuid+group+desc) */
  373. __le32 bg_block_bitmap_hi; /* Blocks bitmap block MSB */
  374. __le32 bg_inode_bitmap_hi; /* Inodes bitmap block MSB */
  375. __le32 bg_inode_table_hi; /* Inodes table block MSB */
  376. __le16 bg_free_blocks_count_hi;/* Free blocks count MSB */
  377. __le16 bg_free_inodes_count_hi;/* Free inodes count MSB */
  378. __le16 bg_used_dirs_count_hi; /* Directories count MSB */
  379. __le16 bg_itable_unused_hi; /* Unused inodes count MSB */
  380. __le32 bg_exclude_bitmap_hi; /* Exclude bitmap block MSB */
  381. __le16 bg_block_bitmap_csum_hi;/* crc32c(s_uuid+grp_num+bbitmap) BE */
  382. __le16 bg_inode_bitmap_csum_hi;/* crc32c(s_uuid+grp_num+ibitmap) BE */
  383. __u32 bg_reserved;
  384. };
  385. #define EXT4_BG_INODE_BITMAP_CSUM_HI_END \
  386. (offsetof(struct ext4_group_desc, bg_inode_bitmap_csum_hi) + \
  387. sizeof(__le16))
  388. #define EXT4_BG_BLOCK_BITMAP_CSUM_HI_END \
  389. (offsetof(struct ext4_group_desc, bg_block_bitmap_csum_hi) + \
  390. sizeof(__le16))
  391. /*
  392. * Structure of a flex block group info
  393. */
  394. struct flex_groups {
  395. atomic64_t free_clusters;
  396. atomic_t free_inodes;
  397. atomic_t used_dirs;
  398. };
  399. #define EXT4_BG_INODE_UNINIT 0x0001 /* Inode table/bitmap not in use */
  400. #define EXT4_BG_BLOCK_UNINIT 0x0002 /* Block bitmap not in use */
  401. #define EXT4_BG_INODE_ZEROED 0x0004 /* On-disk itable initialized to zero */
  402. /*
  403. * Macro-instructions used to manage group descriptors
  404. */
  405. #define EXT4_MIN_DESC_SIZE 32
  406. #define EXT4_MIN_DESC_SIZE_64BIT 64
  407. #define EXT4_MAX_DESC_SIZE EXT4_MIN_BLOCK_SIZE
  408. #define EXT4_DESC_SIZE(s) (EXT4_SB(s)->s_desc_size)
  409. #ifdef __KERNEL__
  410. # define EXT4_BLOCKS_PER_GROUP(s) (EXT4_SB(s)->s_blocks_per_group)
  411. # define EXT4_CLUSTERS_PER_GROUP(s) (EXT4_SB(s)->s_clusters_per_group)
  412. # define EXT4_DESC_PER_BLOCK(s) (EXT4_SB(s)->s_desc_per_block)
  413. # define EXT4_INODES_PER_GROUP(s) (EXT4_SB(s)->s_inodes_per_group)
  414. # define EXT4_DESC_PER_BLOCK_BITS(s) (EXT4_SB(s)->s_desc_per_block_bits)
  415. #else
  416. # define EXT4_BLOCKS_PER_GROUP(s) ((s)->s_blocks_per_group)
  417. # define EXT4_DESC_PER_BLOCK(s) (EXT4_BLOCK_SIZE(s) / EXT4_DESC_SIZE(s))
  418. # define EXT4_INODES_PER_GROUP(s) ((s)->s_inodes_per_group)
  419. #endif
  420. /*
  421. * Constants relative to the data blocks
  422. */
  423. #define EXT4_NDIR_BLOCKS 12
  424. #define EXT4_IND_BLOCK EXT4_NDIR_BLOCKS
  425. #define EXT4_DIND_BLOCK (EXT4_IND_BLOCK + 1)
  426. #define EXT4_TIND_BLOCK (EXT4_DIND_BLOCK + 1)
  427. #define EXT4_N_BLOCKS (EXT4_TIND_BLOCK + 1)
  428. /*
  429. * Inode flags
  430. */
  431. #define EXT4_SECRM_FL 0x00000001 /* Secure deletion */
  432. #define EXT4_UNRM_FL 0x00000002 /* Undelete */
  433. #define EXT4_COMPR_FL 0x00000004 /* Compress file */
  434. #define EXT4_SYNC_FL 0x00000008 /* Synchronous updates */
  435. #define EXT4_IMMUTABLE_FL 0x00000010 /* Immutable file */
  436. #define EXT4_APPEND_FL 0x00000020 /* writes to file may only append */
  437. #define EXT4_NODUMP_FL 0x00000040 /* do not dump file */
  438. #define EXT4_NOATIME_FL 0x00000080 /* do not update atime */
  439. /* Reserved for compression usage... */
  440. #define EXT4_DIRTY_FL 0x00000100
  441. #define EXT4_COMPRBLK_FL 0x00000200 /* One or more compressed clusters */
  442. #define EXT4_NOCOMPR_FL 0x00000400 /* Don't compress */
  443. /* nb: was previously EXT2_ECOMPR_FL */
  444. #define EXT4_ENCRYPT_FL 0x00000800 /* encrypted file */
  445. /* End compression flags --- maybe not all used */
  446. #define EXT4_INDEX_FL 0x00001000 /* hash-indexed directory */
  447. #define EXT4_IMAGIC_FL 0x00002000 /* AFS directory */
  448. #define EXT4_JOURNAL_DATA_FL 0x00004000 /* file data should be journaled */
  449. #define EXT4_NOTAIL_FL 0x00008000 /* file tail should not be merged */
  450. #define EXT4_DIRSYNC_FL 0x00010000 /* dirsync behaviour (directories only) */
  451. #define EXT4_TOPDIR_FL 0x00020000 /* Top of directory hierarchies*/
  452. #define EXT4_HUGE_FILE_FL 0x00040000 /* Set to each huge file */
  453. #define EXT4_EXTENTS_FL 0x00080000 /* Inode uses extents */
  454. #define EXT4_VERITY_FL 0x00100000 /* Verity protected inode */
  455. #define EXT4_EA_INODE_FL 0x00200000 /* Inode used for large EA */
  456. /* 0x00400000 was formerly EXT4_EOFBLOCKS_FL */
  457. #define EXT4_DAX_FL 0x02000000 /* Inode is DAX */
  458. #define EXT4_INLINE_DATA_FL 0x10000000 /* Inode has inline data. */
  459. #define EXT4_PROJINHERIT_FL 0x20000000 /* Create with parents projid */
  460. #define EXT4_CASEFOLD_FL 0x40000000 /* Casefolded directory */
  461. #define EXT4_RESERVED_FL 0x80000000 /* reserved for ext4 lib */
  462. /* User modifiable flags */
  463. #define EXT4_FL_USER_MODIFIABLE (EXT4_SECRM_FL | \
  464. EXT4_UNRM_FL | \
  465. EXT4_COMPR_FL | \
  466. EXT4_SYNC_FL | \
  467. EXT4_IMMUTABLE_FL | \
  468. EXT4_APPEND_FL | \
  469. EXT4_NODUMP_FL | \
  470. EXT4_NOATIME_FL | \
  471. EXT4_JOURNAL_DATA_FL | \
  472. EXT4_NOTAIL_FL | \
  473. EXT4_DIRSYNC_FL | \
  474. EXT4_TOPDIR_FL | \
  475. EXT4_EXTENTS_FL | \
  476. 0x00400000 /* EXT4_EOFBLOCKS_FL */ | \
  477. EXT4_DAX_FL | \
  478. EXT4_PROJINHERIT_FL | \
  479. EXT4_CASEFOLD_FL)
  480. /* User visible flags */
  481. #define EXT4_FL_USER_VISIBLE (EXT4_FL_USER_MODIFIABLE | \
  482. EXT4_DIRTY_FL | \
  483. EXT4_COMPRBLK_FL | \
  484. EXT4_NOCOMPR_FL | \
  485. EXT4_ENCRYPT_FL | \
  486. EXT4_INDEX_FL | \
  487. EXT4_VERITY_FL | \
  488. EXT4_INLINE_DATA_FL)
  489. /* Flags that should be inherited by new inodes from their parent. */
  490. #define EXT4_FL_INHERITED (EXT4_SECRM_FL | EXT4_UNRM_FL | EXT4_COMPR_FL |\
  491. EXT4_SYNC_FL | EXT4_NODUMP_FL | EXT4_NOATIME_FL |\
  492. EXT4_NOCOMPR_FL | EXT4_JOURNAL_DATA_FL |\
  493. EXT4_NOTAIL_FL | EXT4_DIRSYNC_FL |\
  494. EXT4_PROJINHERIT_FL | EXT4_CASEFOLD_FL |\
  495. EXT4_DAX_FL)
  496. /* Flags that are appropriate for regular files (all but dir-specific ones). */
  497. #define EXT4_REG_FLMASK (~(EXT4_DIRSYNC_FL | EXT4_TOPDIR_FL | EXT4_CASEFOLD_FL |\
  498. EXT4_PROJINHERIT_FL))
  499. /* Flags that are appropriate for non-directories/regular files. */
  500. #define EXT4_OTHER_FLMASK (EXT4_NODUMP_FL | EXT4_NOATIME_FL)
  501. /* The only flags that should be swapped */
  502. #define EXT4_FL_SHOULD_SWAP (EXT4_HUGE_FILE_FL | EXT4_EXTENTS_FL)
  503. /* Flags which are mutually exclusive to DAX */
  504. #define EXT4_DAX_MUT_EXCL (EXT4_VERITY_FL | EXT4_ENCRYPT_FL |\
  505. EXT4_JOURNAL_DATA_FL | EXT4_INLINE_DATA_FL)
  506. /* Mask out flags that are inappropriate for the given type of inode. */
  507. static inline __u32 ext4_mask_flags(umode_t mode, __u32 flags)
  508. {
  509. if (S_ISDIR(mode))
  510. return flags;
  511. else if (S_ISREG(mode))
  512. return flags & EXT4_REG_FLMASK;
  513. else
  514. return flags & EXT4_OTHER_FLMASK;
  515. }
  516. /*
  517. * Inode flags used for atomic set/get
  518. */
  519. enum {
  520. EXT4_INODE_SECRM = 0, /* Secure deletion */
  521. EXT4_INODE_UNRM = 1, /* Undelete */
  522. EXT4_INODE_COMPR = 2, /* Compress file */
  523. EXT4_INODE_SYNC = 3, /* Synchronous updates */
  524. EXT4_INODE_IMMUTABLE = 4, /* Immutable file */
  525. EXT4_INODE_APPEND = 5, /* writes to file may only append */
  526. EXT4_INODE_NODUMP = 6, /* do not dump file */
  527. EXT4_INODE_NOATIME = 7, /* do not update atime */
  528. /* Reserved for compression usage... */
  529. EXT4_INODE_DIRTY = 8,
  530. EXT4_INODE_COMPRBLK = 9, /* One or more compressed clusters */
  531. EXT4_INODE_NOCOMPR = 10, /* Don't compress */
  532. EXT4_INODE_ENCRYPT = 11, /* Encrypted file */
  533. /* End compression flags --- maybe not all used */
  534. EXT4_INODE_INDEX = 12, /* hash-indexed directory */
  535. EXT4_INODE_IMAGIC = 13, /* AFS directory */
  536. EXT4_INODE_JOURNAL_DATA = 14, /* file data should be journaled */
  537. EXT4_INODE_NOTAIL = 15, /* file tail should not be merged */
  538. EXT4_INODE_DIRSYNC = 16, /* dirsync behaviour (directories only) */
  539. EXT4_INODE_TOPDIR = 17, /* Top of directory hierarchies*/
  540. EXT4_INODE_HUGE_FILE = 18, /* Set to each huge file */
  541. EXT4_INODE_EXTENTS = 19, /* Inode uses extents */
  542. EXT4_INODE_VERITY = 20, /* Verity protected inode */
  543. EXT4_INODE_EA_INODE = 21, /* Inode used for large EA */
  544. /* 22 was formerly EXT4_INODE_EOFBLOCKS */
  545. EXT4_INODE_DAX = 25, /* Inode is DAX */
  546. EXT4_INODE_INLINE_DATA = 28, /* Data in inode. */
  547. EXT4_INODE_PROJINHERIT = 29, /* Create with parents projid */
  548. EXT4_INODE_CASEFOLD = 30, /* Casefolded directory */
  549. EXT4_INODE_RESERVED = 31, /* reserved for ext4 lib */
  550. };
  551. /*
  552. * Since it's pretty easy to mix up bit numbers and hex values, we use a
  553. * build-time check to make sure that EXT4_XXX_FL is consistent with respect to
  554. * EXT4_INODE_XXX. If all is well, the macros will be dropped, so, it won't cost
  555. * any extra space in the compiled kernel image, otherwise, the build will fail.
  556. * It's important that these values are the same, since we are using
  557. * EXT4_INODE_XXX to test for flag values, but EXT4_XXX_FL must be consistent
  558. * with the values of FS_XXX_FL defined in include/linux/fs.h and the on-disk
  559. * values found in ext2, ext3 and ext4 filesystems, and of course the values
  560. * defined in e2fsprogs.
  561. *
  562. * It's not paranoia if the Murphy's Law really *is* out to get you. :-)
  563. */
  564. #define TEST_FLAG_VALUE(FLAG) (EXT4_##FLAG##_FL == (1U << EXT4_INODE_##FLAG))
  565. #define CHECK_FLAG_VALUE(FLAG) BUILD_BUG_ON(!TEST_FLAG_VALUE(FLAG))
  566. static inline void ext4_check_flag_values(void)
  567. {
  568. CHECK_FLAG_VALUE(SECRM);
  569. CHECK_FLAG_VALUE(UNRM);
  570. CHECK_FLAG_VALUE(COMPR);
  571. CHECK_FLAG_VALUE(SYNC);
  572. CHECK_FLAG_VALUE(IMMUTABLE);
  573. CHECK_FLAG_VALUE(APPEND);
  574. CHECK_FLAG_VALUE(NODUMP);
  575. CHECK_FLAG_VALUE(NOATIME);
  576. CHECK_FLAG_VALUE(DIRTY);
  577. CHECK_FLAG_VALUE(COMPRBLK);
  578. CHECK_FLAG_VALUE(NOCOMPR);
  579. CHECK_FLAG_VALUE(ENCRYPT);
  580. CHECK_FLAG_VALUE(INDEX);
  581. CHECK_FLAG_VALUE(IMAGIC);
  582. CHECK_FLAG_VALUE(JOURNAL_DATA);
  583. CHECK_FLAG_VALUE(NOTAIL);
  584. CHECK_FLAG_VALUE(DIRSYNC);
  585. CHECK_FLAG_VALUE(TOPDIR);
  586. CHECK_FLAG_VALUE(HUGE_FILE);
  587. CHECK_FLAG_VALUE(EXTENTS);
  588. CHECK_FLAG_VALUE(VERITY);
  589. CHECK_FLAG_VALUE(EA_INODE);
  590. CHECK_FLAG_VALUE(INLINE_DATA);
  591. CHECK_FLAG_VALUE(PROJINHERIT);
  592. CHECK_FLAG_VALUE(CASEFOLD);
  593. CHECK_FLAG_VALUE(RESERVED);
  594. }
  595. #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
  596. struct compat_ext4_new_group_input {
  597. u32 group;
  598. compat_u64 block_bitmap;
  599. compat_u64 inode_bitmap;
  600. compat_u64 inode_table;
  601. u32 blocks_count;
  602. u16 reserved_blocks;
  603. u16 unused;
  604. };
  605. #endif
  606. /* The struct ext4_new_group_input in kernel space, with free_blocks_count */
  607. struct ext4_new_group_data {
  608. __u32 group;
  609. __u64 block_bitmap;
  610. __u64 inode_bitmap;
  611. __u64 inode_table;
  612. __u32 blocks_count;
  613. __u16 reserved_blocks;
  614. __u16 mdata_blocks;
  615. __u32 free_clusters_count;
  616. };
  617. /* Indexes used to index group tables in ext4_new_group_data */
  618. enum {
  619. BLOCK_BITMAP = 0, /* block bitmap */
  620. INODE_BITMAP, /* inode bitmap */
  621. INODE_TABLE, /* inode tables */
  622. GROUP_TABLE_COUNT,
  623. };
  624. /*
  625. * Flags used by ext4_map_blocks()
  626. */
  627. /* Allocate any needed blocks and/or convert an unwritten
  628. extent to be an initialized ext4 */
  629. #define EXT4_GET_BLOCKS_CREATE 0x0001
  630. /* Request the creation of an unwritten extent */
  631. #define EXT4_GET_BLOCKS_UNWRIT_EXT 0x0002
  632. #define EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT (EXT4_GET_BLOCKS_UNWRIT_EXT|\
  633. EXT4_GET_BLOCKS_CREATE)
  634. /* Caller is from the delayed allocation writeout path
  635. * finally doing the actual allocation of delayed blocks */
  636. #define EXT4_GET_BLOCKS_DELALLOC_RESERVE 0x0004
  637. /* caller is from the direct IO path, request to creation of an
  638. unwritten extents if not allocated, split the unwritten
  639. extent if blocks has been preallocated already*/
  640. #define EXT4_GET_BLOCKS_PRE_IO 0x0008
  641. #define EXT4_GET_BLOCKS_CONVERT 0x0010
  642. #define EXT4_GET_BLOCKS_IO_CREATE_EXT (EXT4_GET_BLOCKS_PRE_IO|\
  643. EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT)
  644. /* Convert extent to initialized after IO complete */
  645. #define EXT4_GET_BLOCKS_IO_CONVERT_EXT (EXT4_GET_BLOCKS_CONVERT|\
  646. EXT4_GET_BLOCKS_CREATE_UNWRIT_EXT)
  647. /* Eventual metadata allocation (due to growing extent tree)
  648. * should not fail, so try to use reserved blocks for that.*/
  649. #define EXT4_GET_BLOCKS_METADATA_NOFAIL 0x0020
  650. /* Don't normalize allocation size (used for fallocate) */
  651. #define EXT4_GET_BLOCKS_NO_NORMALIZE 0x0040
  652. /* Convert written extents to unwritten */
  653. #define EXT4_GET_BLOCKS_CONVERT_UNWRITTEN 0x0100
  654. /* Write zeros to newly created written extents */
  655. #define EXT4_GET_BLOCKS_ZERO 0x0200
  656. #define EXT4_GET_BLOCKS_CREATE_ZERO (EXT4_GET_BLOCKS_CREATE |\
  657. EXT4_GET_BLOCKS_ZERO)
  658. /* Caller will submit data before dropping transaction handle. This
  659. * allows jbd2 to avoid submitting data before commit. */
  660. #define EXT4_GET_BLOCKS_IO_SUBMIT 0x0400
  661. /* Caller is in the atomic contex, find extent if it has been cached */
  662. #define EXT4_GET_BLOCKS_CACHED_NOWAIT 0x0800
  663. /*
  664. * The bit position of these flags must not overlap with any of the
  665. * EXT4_GET_BLOCKS_*. They are used by ext4_find_extent(),
  666. * read_extent_tree_block(), ext4_split_extent_at(),
  667. * ext4_ext_insert_extent(), and ext4_ext_create_new_leaf().
  668. * EXT4_EX_NOCACHE is used to indicate that the we shouldn't be
  669. * caching the extents when reading from the extent tree while a
  670. * truncate or punch hole operation is in progress.
  671. */
  672. #define EXT4_EX_NOCACHE 0x40000000
  673. #define EXT4_EX_FORCE_CACHE 0x20000000
  674. #define EXT4_EX_NOFAIL 0x10000000
  675. /*
  676. * Flags used by ext4_free_blocks
  677. */
  678. #define EXT4_FREE_BLOCKS_METADATA 0x0001
  679. #define EXT4_FREE_BLOCKS_FORGET 0x0002
  680. #define EXT4_FREE_BLOCKS_VALIDATED 0x0004
  681. #define EXT4_FREE_BLOCKS_NO_QUOT_UPDATE 0x0008
  682. #define EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER 0x0010
  683. #define EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER 0x0020
  684. #define EXT4_FREE_BLOCKS_RERESERVE_CLUSTER 0x0040
  685. #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
  686. /*
  687. * ioctl commands in 32 bit emulation
  688. */
  689. #define EXT4_IOC32_GETVERSION _IOR('f', 3, int)
  690. #define EXT4_IOC32_SETVERSION _IOW('f', 4, int)
  691. #define EXT4_IOC32_GETRSVSZ _IOR('f', 5, int)
  692. #define EXT4_IOC32_SETRSVSZ _IOW('f', 6, int)
  693. #define EXT4_IOC32_GROUP_EXTEND _IOW('f', 7, unsigned int)
  694. #define EXT4_IOC32_GROUP_ADD _IOW('f', 8, struct compat_ext4_new_group_input)
  695. #define EXT4_IOC32_GETVERSION_OLD FS_IOC32_GETVERSION
  696. #define EXT4_IOC32_SETVERSION_OLD FS_IOC32_SETVERSION
  697. #endif
  698. /* Max physical block we can address w/o extents */
  699. #define EXT4_MAX_BLOCK_FILE_PHYS 0xFFFFFFFF
  700. /* Max logical block we can support */
  701. #define EXT4_MAX_LOGICAL_BLOCK 0xFFFFFFFE
  702. /*
  703. * Structure of an inode on the disk
  704. */
  705. struct ext4_inode {
  706. __le16 i_mode; /* File mode */
  707. __le16 i_uid; /* Low 16 bits of Owner Uid */
  708. __le32 i_size_lo; /* Size in bytes */
  709. __le32 i_atime; /* Access time */
  710. __le32 i_ctime; /* Inode Change time */
  711. __le32 i_mtime; /* Modification time */
  712. __le32 i_dtime; /* Deletion Time */
  713. __le16 i_gid; /* Low 16 bits of Group Id */
  714. __le16 i_links_count; /* Links count */
  715. __le32 i_blocks_lo; /* Blocks count */
  716. __le32 i_flags; /* File flags */
  717. union {
  718. struct {
  719. __le32 l_i_version;
  720. } linux1;
  721. struct {
  722. __u32 h_i_translator;
  723. } hurd1;
  724. struct {
  725. __u32 m_i_reserved1;
  726. } masix1;
  727. } osd1; /* OS dependent 1 */
  728. __le32 i_block[EXT4_N_BLOCKS];/* Pointers to blocks */
  729. __le32 i_generation; /* File version (for NFS) */
  730. __le32 i_file_acl_lo; /* File ACL */
  731. __le32 i_size_high;
  732. __le32 i_obso_faddr; /* Obsoleted fragment address */
  733. union {
  734. struct {
  735. __le16 l_i_blocks_high; /* were l_i_reserved1 */
  736. __le16 l_i_file_acl_high;
  737. __le16 l_i_uid_high; /* these 2 fields */
  738. __le16 l_i_gid_high; /* were reserved2[0] */
  739. __le16 l_i_checksum_lo;/* crc32c(uuid+inum+inode) LE */
  740. __le16 l_i_reserved;
  741. } linux2;
  742. struct {
  743. __le16 h_i_reserved1; /* Obsoleted fragment number/size which are removed in ext4 */
  744. __u16 h_i_mode_high;
  745. __u16 h_i_uid_high;
  746. __u16 h_i_gid_high;
  747. __u32 h_i_author;
  748. } hurd2;
  749. struct {
  750. __le16 h_i_reserved1; /* Obsoleted fragment number/size which are removed in ext4 */
  751. __le16 m_i_file_acl_high;
  752. __u32 m_i_reserved2[2];
  753. } masix2;
  754. } osd2; /* OS dependent 2 */
  755. __le16 i_extra_isize;
  756. __le16 i_checksum_hi; /* crc32c(uuid+inum+inode) BE */
  757. __le32 i_ctime_extra; /* extra Change time (nsec << 2 | epoch) */
  758. __le32 i_mtime_extra; /* extra Modification time(nsec << 2 | epoch) */
  759. __le32 i_atime_extra; /* extra Access time (nsec << 2 | epoch) */
  760. __le32 i_crtime; /* File Creation time */
  761. __le32 i_crtime_extra; /* extra FileCreationtime (nsec << 2 | epoch) */
  762. __le32 i_version_hi; /* high 32 bits for 64-bit version */
  763. __le32 i_projid; /* Project ID */
  764. };
  765. #define EXT4_EPOCH_BITS 2
  766. #define EXT4_EPOCH_MASK ((1 << EXT4_EPOCH_BITS) - 1)
  767. #define EXT4_NSEC_MASK (~0UL << EXT4_EPOCH_BITS)
  768. /*
  769. * Extended fields will fit into an inode if the filesystem was formatted
  770. * with large inodes (-I 256 or larger) and there are not currently any EAs
  771. * consuming all of the available space. For new inodes we always reserve
  772. * enough space for the kernel's known extended fields, but for inodes
  773. * created with an old kernel this might not have been the case. None of
  774. * the extended inode fields is critical for correct filesystem operation.
  775. * This macro checks if a certain field fits in the inode. Note that
  776. * inode-size = GOOD_OLD_INODE_SIZE + i_extra_isize
  777. */
  778. #define EXT4_FITS_IN_INODE(ext4_inode, einode, field) \
  779. ((offsetof(typeof(*ext4_inode), field) + \
  780. sizeof((ext4_inode)->field)) \
  781. <= (EXT4_GOOD_OLD_INODE_SIZE + \
  782. (einode)->i_extra_isize)) \
  783. /*
  784. * We use an encoding that preserves the times for extra epoch "00":
  785. *
  786. * extra msb of adjust for signed
  787. * epoch 32-bit 32-bit tv_sec to
  788. * bits time decoded 64-bit tv_sec 64-bit tv_sec valid time range
  789. * 0 0 1 -0x80000000..-0x00000001 0x000000000 1901-12-13..1969-12-31
  790. * 0 0 0 0x000000000..0x07fffffff 0x000000000 1970-01-01..2038-01-19
  791. * 0 1 1 0x080000000..0x0ffffffff 0x100000000 2038-01-19..2106-02-07
  792. * 0 1 0 0x100000000..0x17fffffff 0x100000000 2106-02-07..2174-02-25
  793. * 1 0 1 0x180000000..0x1ffffffff 0x200000000 2174-02-25..2242-03-16
  794. * 1 0 0 0x200000000..0x27fffffff 0x200000000 2242-03-16..2310-04-04
  795. * 1 1 1 0x280000000..0x2ffffffff 0x300000000 2310-04-04..2378-04-22
  796. * 1 1 0 0x300000000..0x37fffffff 0x300000000 2378-04-22..2446-05-10
  797. *
  798. * Note that previous versions of the kernel on 64-bit systems would
  799. * incorrectly use extra epoch bits 1,1 for dates between 1901 and
  800. * 1970. e2fsck will correct this, assuming that it is run on the
  801. * affected filesystem before 2242.
  802. */
  803. static inline __le32 ext4_encode_extra_time(struct timespec64 ts)
  804. {
  805. u32 extra = ((ts.tv_sec - (s32)ts.tv_sec) >> 32) & EXT4_EPOCH_MASK;
  806. return cpu_to_le32(extra | (ts.tv_nsec << EXT4_EPOCH_BITS));
  807. }
  808. static inline struct timespec64 ext4_decode_extra_time(__le32 base,
  809. __le32 extra)
  810. {
  811. struct timespec64 ts = { .tv_sec = (signed)le32_to_cpu(base) };
  812. if (unlikely(extra & cpu_to_le32(EXT4_EPOCH_MASK)))
  813. ts.tv_sec += (u64)(le32_to_cpu(extra) & EXT4_EPOCH_MASK) << 32;
  814. ts.tv_nsec = (le32_to_cpu(extra) & EXT4_NSEC_MASK) >> EXT4_EPOCH_BITS;
  815. return ts;
  816. }
  817. #define EXT4_INODE_SET_XTIME_VAL(xtime, inode, raw_inode, ts) \
  818. do { \
  819. if (EXT4_FITS_IN_INODE(raw_inode, EXT4_I(inode), xtime ## _extra)) { \
  820. (raw_inode)->xtime = cpu_to_le32((ts).tv_sec); \
  821. (raw_inode)->xtime ## _extra = ext4_encode_extra_time(ts); \
  822. } else \
  823. (raw_inode)->xtime = cpu_to_le32(clamp_t(int32_t, (ts).tv_sec, S32_MIN, S32_MAX)); \
  824. } while (0)
  825. #define EXT4_INODE_SET_ATIME(inode, raw_inode) \
  826. EXT4_INODE_SET_XTIME_VAL(i_atime, inode, raw_inode, inode_get_atime(inode))
  827. #define EXT4_INODE_SET_MTIME(inode, raw_inode) \
  828. EXT4_INODE_SET_XTIME_VAL(i_mtime, inode, raw_inode, inode_get_mtime(inode))
  829. #define EXT4_INODE_SET_CTIME(inode, raw_inode) \
  830. EXT4_INODE_SET_XTIME_VAL(i_ctime, inode, raw_inode, inode_get_ctime(inode))
  831. #define EXT4_EINODE_SET_XTIME(xtime, einode, raw_inode) \
  832. if (EXT4_FITS_IN_INODE(raw_inode, einode, xtime)) \
  833. EXT4_INODE_SET_XTIME_VAL(xtime, &((einode)->vfs_inode), \
  834. raw_inode, (einode)->xtime)
  835. #define EXT4_INODE_GET_XTIME_VAL(xtime, inode, raw_inode) \
  836. (EXT4_FITS_IN_INODE(raw_inode, EXT4_I(inode), xtime ## _extra) ? \
  837. ext4_decode_extra_time((raw_inode)->xtime, \
  838. (raw_inode)->xtime ## _extra) : \
  839. (struct timespec64) { \
  840. .tv_sec = (signed)le32_to_cpu((raw_inode)->xtime) \
  841. })
  842. #define EXT4_INODE_GET_ATIME(inode, raw_inode) \
  843. do { \
  844. inode_set_atime_to_ts(inode, \
  845. EXT4_INODE_GET_XTIME_VAL(i_atime, inode, raw_inode)); \
  846. } while (0)
  847. #define EXT4_INODE_GET_MTIME(inode, raw_inode) \
  848. do { \
  849. inode_set_mtime_to_ts(inode, \
  850. EXT4_INODE_GET_XTIME_VAL(i_mtime, inode, raw_inode)); \
  851. } while (0)
  852. #define EXT4_INODE_GET_CTIME(inode, raw_inode) \
  853. do { \
  854. inode_set_ctime_to_ts(inode, \
  855. EXT4_INODE_GET_XTIME_VAL(i_ctime, inode, raw_inode)); \
  856. } while (0)
  857. #define EXT4_EINODE_GET_XTIME(xtime, einode, raw_inode) \
  858. do { \
  859. if (EXT4_FITS_IN_INODE(raw_inode, einode, xtime)) \
  860. (einode)->xtime = \
  861. EXT4_INODE_GET_XTIME_VAL(xtime, &(einode->vfs_inode), \
  862. raw_inode); \
  863. else \
  864. (einode)->xtime = (struct timespec64){0, 0}; \
  865. } while (0)
  866. #define i_disk_version osd1.linux1.l_i_version
  867. #if defined(__KERNEL__) || defined(__linux__)
  868. #define i_reserved1 osd1.linux1.l_i_reserved1
  869. #define i_file_acl_high osd2.linux2.l_i_file_acl_high
  870. #define i_blocks_high osd2.linux2.l_i_blocks_high
  871. #define i_uid_low i_uid
  872. #define i_gid_low i_gid
  873. #define i_uid_high osd2.linux2.l_i_uid_high
  874. #define i_gid_high osd2.linux2.l_i_gid_high
  875. #define i_checksum_lo osd2.linux2.l_i_checksum_lo
  876. #elif defined(__GNU__)
  877. #define i_translator osd1.hurd1.h_i_translator
  878. #define i_uid_high osd2.hurd2.h_i_uid_high
  879. #define i_gid_high osd2.hurd2.h_i_gid_high
  880. #define i_author osd2.hurd2.h_i_author
  881. #elif defined(__masix__)
  882. #define i_reserved1 osd1.masix1.m_i_reserved1
  883. #define i_file_acl_high osd2.masix2.m_i_file_acl_high
  884. #define i_reserved2 osd2.masix2.m_i_reserved2
  885. #endif /* defined(__KERNEL__) || defined(__linux__) */
  886. #include "extents_status.h"
  887. #include "fast_commit.h"
  888. /*
  889. * Lock subclasses for i_data_sem in the ext4_inode_info structure.
  890. *
  891. * These are needed to avoid lockdep false positives when we need to
  892. * allocate blocks to the quota inode during ext4_map_blocks(), while
  893. * holding i_data_sem for a normal (non-quota) inode. Since we don't
  894. * do quota tracking for the quota inode, this avoids deadlock (as
  895. * well as infinite recursion, since it isn't turtles all the way
  896. * down...)
  897. *
  898. * I_DATA_SEM_NORMAL - Used for most inodes
  899. * I_DATA_SEM_OTHER - Used by move_inode.c for the second normal inode
  900. * where the second inode has larger inode number
  901. * than the first
  902. * I_DATA_SEM_QUOTA - Used for quota inodes only
  903. * I_DATA_SEM_EA - Used for ea_inodes only
  904. */
  905. enum {
  906. I_DATA_SEM_NORMAL = 0,
  907. I_DATA_SEM_OTHER,
  908. I_DATA_SEM_QUOTA,
  909. I_DATA_SEM_EA
  910. };
  911. /*
  912. * fourth extended file system inode data in memory
  913. */
  914. struct ext4_inode_info {
  915. __le32 i_data[15]; /* unconverted */
  916. __u32 i_dtime;
  917. ext4_fsblk_t i_file_acl;
  918. /*
  919. * i_block_group is the number of the block group which contains
  920. * this file's inode. Constant across the lifetime of the inode,
  921. * it is used for making block allocation decisions - we try to
  922. * place a file's data blocks near its inode block, and new inodes
  923. * near to their parent directory's inode.
  924. */
  925. ext4_group_t i_block_group;
  926. ext4_lblk_t i_dir_start_lookup;
  927. #if (BITS_PER_LONG < 64)
  928. unsigned long i_state_flags; /* Dynamic state flags */
  929. #endif
  930. unsigned long i_flags;
  931. /*
  932. * Extended attributes can be read independently of the main file
  933. * data. Taking i_rwsem even when reading would cause contention
  934. * between readers of EAs and writers of regular file data, so
  935. * instead we synchronize on xattr_sem when reading or changing
  936. * EAs.
  937. */
  938. struct rw_semaphore xattr_sem;
  939. /*
  940. * Inodes with EXT4_STATE_ORPHAN_FILE use i_orphan_idx. Otherwise
  941. * i_orphan is used.
  942. */
  943. union {
  944. struct list_head i_orphan; /* unlinked but open inodes */
  945. unsigned int i_orphan_idx; /* Index in orphan file */
  946. };
  947. /* Fast commit related info */
  948. /* For tracking dentry create updates */
  949. struct list_head i_fc_dilist;
  950. struct list_head i_fc_list; /*
  951. * inodes that need fast commit
  952. * protected by sbi->s_fc_lock.
  953. */
  954. /* Start of lblk range that needs to be committed in this fast commit */
  955. ext4_lblk_t i_fc_lblk_start;
  956. /* End of lblk range that needs to be committed in this fast commit */
  957. ext4_lblk_t i_fc_lblk_len;
  958. /* Number of ongoing updates on this inode */
  959. atomic_t i_fc_updates;
  960. atomic_t i_unwritten; /* Nr. of inflight conversions pending */
  961. /* Fast commit wait queue for this inode */
  962. wait_queue_head_t i_fc_wait;
  963. /* Protect concurrent accesses on i_fc_lblk_start, i_fc_lblk_len */
  964. struct mutex i_fc_lock;
  965. /*
  966. * i_disksize keeps track of what the inode size is ON DISK, not
  967. * in memory. During truncate, i_size is set to the new size by
  968. * the VFS prior to calling ext4_truncate(), but the filesystem won't
  969. * set i_disksize to 0 until the truncate is actually under way.
  970. *
  971. * The intent is that i_disksize always represents the blocks which
  972. * are used by this file. This allows recovery to restart truncate
  973. * on orphans if we crash during truncate. We actually write i_disksize
  974. * into the on-disk inode when writing inodes out, instead of i_size.
  975. *
  976. * The only time when i_disksize and i_size may be different is when
  977. * a truncate is in progress. The only things which change i_disksize
  978. * are ext4_get_block (growth) and ext4_truncate (shrinkth).
  979. */
  980. loff_t i_disksize;
  981. /*
  982. * i_data_sem is for serialising ext4_truncate() against
  983. * ext4_getblock(). In the 2.4 ext2 design, great chunks of inode's
  984. * data tree are chopped off during truncate. We can't do that in
  985. * ext4 because whenever we perform intermediate commits during
  986. * truncate, the inode and all the metadata blocks *must* be in a
  987. * consistent state which allows truncation of the orphans to restart
  988. * during recovery. Hence we must fix the get_block-vs-truncate race
  989. * by other means, so we have i_data_sem.
  990. */
  991. struct rw_semaphore i_data_sem;
  992. struct inode vfs_inode;
  993. struct jbd2_inode *jinode;
  994. spinlock_t i_raw_lock; /* protects updates to the raw inode */
  995. /*
  996. * File creation time. Its function is same as that of
  997. * struct timespec64 i_{a,c,m}time in the generic inode.
  998. */
  999. struct timespec64 i_crtime;
  1000. /* mballoc */
  1001. atomic_t i_prealloc_active;
  1002. /* allocation reservation info for delalloc */
  1003. /* In case of bigalloc, this refer to clusters rather than blocks */
  1004. unsigned int i_reserved_data_blocks;
  1005. struct rb_root i_prealloc_node;
  1006. rwlock_t i_prealloc_lock;
  1007. /* extents status tree */
  1008. struct ext4_es_tree i_es_tree;
  1009. rwlock_t i_es_lock;
  1010. struct list_head i_es_list;
  1011. unsigned int i_es_all_nr; /* protected by i_es_lock */
  1012. unsigned int i_es_shk_nr; /* protected by i_es_lock */
  1013. ext4_lblk_t i_es_shrink_lblk; /* Offset where we start searching for
  1014. extents to shrink. Protected by
  1015. i_es_lock */
  1016. /* ialloc */
  1017. ext4_group_t i_last_alloc_group;
  1018. /* pending cluster reservations for bigalloc file systems */
  1019. struct ext4_pending_tree i_pending_tree;
  1020. /* on-disk additional length */
  1021. __u16 i_extra_isize;
  1022. /* Indicate the inline data space. */
  1023. u16 i_inline_off;
  1024. u16 i_inline_size;
  1025. #ifdef CONFIG_QUOTA
  1026. /* quota space reservation, managed internally by quota code */
  1027. qsize_t i_reserved_quota;
  1028. #endif
  1029. /* Lock protecting lists below */
  1030. spinlock_t i_completed_io_lock;
  1031. /*
  1032. * Completed IOs that need unwritten extents handling and have
  1033. * transaction reserved
  1034. */
  1035. struct list_head i_rsv_conversion_list;
  1036. struct work_struct i_rsv_conversion_work;
  1037. spinlock_t i_block_reservation_lock;
  1038. /*
  1039. * Transactions that contain inode's metadata needed to complete
  1040. * fsync and fdatasync, respectively.
  1041. */
  1042. tid_t i_sync_tid;
  1043. tid_t i_datasync_tid;
  1044. #ifdef CONFIG_QUOTA
  1045. struct dquot __rcu *i_dquot[MAXQUOTAS];
  1046. #endif
  1047. /* Precomputed uuid+inum+igen checksum for seeding inode checksums */
  1048. __u32 i_csum_seed;
  1049. kprojid_t i_projid;
  1050. };
  1051. /*
  1052. * File system states
  1053. */
  1054. #define EXT4_VALID_FS 0x0001 /* Unmounted cleanly */
  1055. #define EXT4_ERROR_FS 0x0002 /* Errors detected */
  1056. #define EXT4_ORPHAN_FS 0x0004 /* Orphans being recovered */
  1057. #define EXT4_FC_REPLAY 0x0020 /* Fast commit replay ongoing */
  1058. /*
  1059. * Misc. filesystem flags
  1060. */
  1061. #define EXT2_FLAGS_SIGNED_HASH 0x0001 /* Signed dirhash in use */
  1062. #define EXT2_FLAGS_UNSIGNED_HASH 0x0002 /* Unsigned dirhash in use */
  1063. #define EXT2_FLAGS_TEST_FILESYS 0x0004 /* to test development code */
  1064. /*
  1065. * Mount flags set via mount options or defaults
  1066. */
  1067. #define EXT4_MOUNT_NO_MBCACHE 0x00001 /* Do not use mbcache */
  1068. #define EXT4_MOUNT_GRPID 0x00004 /* Create files with directory's group */
  1069. #define EXT4_MOUNT_DEBUG 0x00008 /* Some debugging messages */
  1070. #define EXT4_MOUNT_ERRORS_CONT 0x00010 /* Continue on errors */
  1071. #define EXT4_MOUNT_ERRORS_RO 0x00020 /* Remount fs ro on errors */
  1072. #define EXT4_MOUNT_ERRORS_PANIC 0x00040 /* Panic on errors */
  1073. #define EXT4_MOUNT_ERRORS_MASK 0x00070
  1074. #define EXT4_MOUNT_MINIX_DF 0x00080 /* Mimics the Minix statfs */
  1075. #define EXT4_MOUNT_NOLOAD 0x00100 /* Don't use existing journal*/
  1076. #ifdef CONFIG_FS_DAX
  1077. #define EXT4_MOUNT_DAX_ALWAYS 0x00200 /* Direct Access */
  1078. #else
  1079. #define EXT4_MOUNT_DAX_ALWAYS 0
  1080. #endif
  1081. #define EXT4_MOUNT_DATA_FLAGS 0x00C00 /* Mode for data writes: */
  1082. #define EXT4_MOUNT_JOURNAL_DATA 0x00400 /* Write data to journal */
  1083. #define EXT4_MOUNT_ORDERED_DATA 0x00800 /* Flush data before commit */
  1084. #define EXT4_MOUNT_WRITEBACK_DATA 0x00C00 /* No data ordering */
  1085. #define EXT4_MOUNT_UPDATE_JOURNAL 0x01000 /* Update the journal format */
  1086. #define EXT4_MOUNT_NO_UID32 0x02000 /* Disable 32-bit UIDs */
  1087. #define EXT4_MOUNT_XATTR_USER 0x04000 /* Extended user attributes */
  1088. #define EXT4_MOUNT_POSIX_ACL 0x08000 /* POSIX Access Control Lists */
  1089. #define EXT4_MOUNT_NO_AUTO_DA_ALLOC 0x10000 /* No auto delalloc mapping */
  1090. #define EXT4_MOUNT_BARRIER 0x20000 /* Use block barriers */
  1091. #define EXT4_MOUNT_QUOTA 0x40000 /* Some quota option set */
  1092. #define EXT4_MOUNT_USRQUOTA 0x80000 /* "old" user quota,
  1093. * enable enforcement for hidden
  1094. * quota files */
  1095. #define EXT4_MOUNT_GRPQUOTA 0x100000 /* "old" group quota, enable
  1096. * enforcement for hidden quota
  1097. * files */
  1098. #define EXT4_MOUNT_PRJQUOTA 0x200000 /* Enable project quota
  1099. * enforcement */
  1100. #define EXT4_MOUNT_DIOREAD_NOLOCK 0x400000 /* Enable support for dio read nolocking */
  1101. #define EXT4_MOUNT_JOURNAL_CHECKSUM 0x800000 /* Journal checksums */
  1102. #define EXT4_MOUNT_JOURNAL_ASYNC_COMMIT 0x1000000 /* Journal Async Commit */
  1103. #define EXT4_MOUNT_WARN_ON_ERROR 0x2000000 /* Trigger WARN_ON on error */
  1104. #define EXT4_MOUNT_NO_PREFETCH_BLOCK_BITMAPS 0x4000000
  1105. #define EXT4_MOUNT_DELALLOC 0x8000000 /* Delalloc support */
  1106. #define EXT4_MOUNT_DATA_ERR_ABORT 0x10000000 /* Abort on file data write */
  1107. #define EXT4_MOUNT_BLOCK_VALIDITY 0x20000000 /* Block validity checking */
  1108. #define EXT4_MOUNT_DISCARD 0x40000000 /* Issue DISCARD requests */
  1109. #define EXT4_MOUNT_INIT_INODE_TABLE 0x80000000 /* Initialize uninitialized itables */
  1110. /*
  1111. * Mount flags set either automatically (could not be set by mount option)
  1112. * based on per file system feature or property or in special cases such as
  1113. * distinguishing between explicit mount option definition and default.
  1114. */
  1115. #define EXT4_MOUNT2_EXPLICIT_DELALLOC 0x00000001 /* User explicitly
  1116. specified delalloc */
  1117. #define EXT4_MOUNT2_STD_GROUP_SIZE 0x00000002 /* We have standard group
  1118. size of blocksize * 8
  1119. blocks */
  1120. #define EXT4_MOUNT2_HURD_COMPAT 0x00000004 /* Support HURD-castrated
  1121. file systems */
  1122. #define EXT4_MOUNT2_EXPLICIT_JOURNAL_CHECKSUM 0x00000008 /* User explicitly
  1123. specified journal checksum */
  1124. #define EXT4_MOUNT2_JOURNAL_FAST_COMMIT 0x00000010 /* Journal fast commit */
  1125. #define EXT4_MOUNT2_DAX_NEVER 0x00000020 /* Do not allow Direct Access */
  1126. #define EXT4_MOUNT2_DAX_INODE 0x00000040 /* For printing options only */
  1127. #define EXT4_MOUNT2_MB_OPTIMIZE_SCAN 0x00000080 /* Optimize group
  1128. * scanning in mballoc
  1129. */
  1130. #define EXT4_MOUNT2_ABORT 0x00000100 /* Abort filesystem */
  1131. #define clear_opt(sb, opt) EXT4_SB(sb)->s_mount_opt &= \
  1132. ~EXT4_MOUNT_##opt
  1133. #define set_opt(sb, opt) EXT4_SB(sb)->s_mount_opt |= \
  1134. EXT4_MOUNT_##opt
  1135. #define test_opt(sb, opt) (EXT4_SB(sb)->s_mount_opt & \
  1136. EXT4_MOUNT_##opt)
  1137. #define clear_opt2(sb, opt) EXT4_SB(sb)->s_mount_opt2 &= \
  1138. ~EXT4_MOUNT2_##opt
  1139. #define set_opt2(sb, opt) EXT4_SB(sb)->s_mount_opt2 |= \
  1140. EXT4_MOUNT2_##opt
  1141. #define test_opt2(sb, opt) (EXT4_SB(sb)->s_mount_opt2 & \
  1142. EXT4_MOUNT2_##opt)
  1143. #define ext4_test_and_set_bit __test_and_set_bit_le
  1144. #define ext4_set_bit __set_bit_le
  1145. #define ext4_test_and_clear_bit __test_and_clear_bit_le
  1146. #define ext4_clear_bit __clear_bit_le
  1147. #define ext4_test_bit test_bit_le
  1148. #define ext4_find_next_zero_bit find_next_zero_bit_le
  1149. #define ext4_find_next_bit find_next_bit_le
  1150. extern void mb_set_bits(void *bm, int cur, int len);
  1151. /*
  1152. * Maximal mount counts between two filesystem checks
  1153. */
  1154. #define EXT4_DFL_MAX_MNT_COUNT 20 /* Allow 20 mounts */
  1155. #define EXT4_DFL_CHECKINTERVAL 0 /* Don't use interval check */
  1156. /*
  1157. * Behaviour when detecting errors
  1158. */
  1159. #define EXT4_ERRORS_CONTINUE 1 /* Continue execution */
  1160. #define EXT4_ERRORS_RO 2 /* Remount fs read-only */
  1161. #define EXT4_ERRORS_PANIC 3 /* Panic */
  1162. #define EXT4_ERRORS_DEFAULT EXT4_ERRORS_CONTINUE
  1163. /* Metadata checksum algorithm codes */
  1164. #define EXT4_CRC32C_CHKSUM 1
  1165. #define EXT4_LABEL_MAX 16
  1166. /*
  1167. * Structure of the super block
  1168. */
  1169. struct ext4_super_block {
  1170. /*00*/ __le32 s_inodes_count; /* Inodes count */
  1171. __le32 s_blocks_count_lo; /* Blocks count */
  1172. __le32 s_r_blocks_count_lo; /* Reserved blocks count */
  1173. __le32 s_free_blocks_count_lo; /* Free blocks count */
  1174. /*10*/ __le32 s_free_inodes_count; /* Free inodes count */
  1175. __le32 s_first_data_block; /* First Data Block */
  1176. __le32 s_log_block_size; /* Block size */
  1177. __le32 s_log_cluster_size; /* Allocation cluster size */
  1178. /*20*/ __le32 s_blocks_per_group; /* # Blocks per group */
  1179. __le32 s_clusters_per_group; /* # Clusters per group */
  1180. __le32 s_inodes_per_group; /* # Inodes per group */
  1181. __le32 s_mtime; /* Mount time */
  1182. /*30*/ __le32 s_wtime; /* Write time */
  1183. __le16 s_mnt_count; /* Mount count */
  1184. __le16 s_max_mnt_count; /* Maximal mount count */
  1185. __le16 s_magic; /* Magic signature */
  1186. __le16 s_state; /* File system state */
  1187. __le16 s_errors; /* Behaviour when detecting errors */
  1188. __le16 s_minor_rev_level; /* minor revision level */
  1189. /*40*/ __le32 s_lastcheck; /* time of last check */
  1190. __le32 s_checkinterval; /* max. time between checks */
  1191. __le32 s_creator_os; /* OS */
  1192. __le32 s_rev_level; /* Revision level */
  1193. /*50*/ __le16 s_def_resuid; /* Default uid for reserved blocks */
  1194. __le16 s_def_resgid; /* Default gid for reserved blocks */
  1195. /*
  1196. * These fields are for EXT4_DYNAMIC_REV superblocks only.
  1197. *
  1198. * Note: the difference between the compatible feature set and
  1199. * the incompatible feature set is that if there is a bit set
  1200. * in the incompatible feature set that the kernel doesn't
  1201. * know about, it should refuse to mount the filesystem.
  1202. *
  1203. * e2fsck's requirements are more strict; if it doesn't know
  1204. * about a feature in either the compatible or incompatible
  1205. * feature set, it must abort and not try to meddle with
  1206. * things it doesn't understand...
  1207. */
  1208. __le32 s_first_ino; /* First non-reserved inode */
  1209. __le16 s_inode_size; /* size of inode structure */
  1210. __le16 s_block_group_nr; /* block group # of this superblock */
  1211. __le32 s_feature_compat; /* compatible feature set */
  1212. /*60*/ __le32 s_feature_incompat; /* incompatible feature set */
  1213. __le32 s_feature_ro_compat; /* readonly-compatible feature set */
  1214. /*68*/ __u8 s_uuid[16]; /* 128-bit uuid for volume */
  1215. /*78*/ char s_volume_name[EXT4_LABEL_MAX] __nonstring; /* volume name */
  1216. /*88*/ char s_last_mounted[64] __nonstring; /* directory where last mounted */
  1217. /*C8*/ __le32 s_algorithm_usage_bitmap; /* For compression */
  1218. /*
  1219. * Performance hints. Directory preallocation should only
  1220. * happen if the EXT4_FEATURE_COMPAT_DIR_PREALLOC flag is on.
  1221. */
  1222. __u8 s_prealloc_blocks; /* Nr of blocks to try to preallocate*/
  1223. __u8 s_prealloc_dir_blocks; /* Nr to preallocate for dirs */
  1224. __le16 s_reserved_gdt_blocks; /* Per group desc for online growth */
  1225. /*
  1226. * Journaling support valid if EXT4_FEATURE_COMPAT_HAS_JOURNAL set.
  1227. */
  1228. /*D0*/ __u8 s_journal_uuid[16]; /* uuid of journal superblock */
  1229. /*E0*/ __le32 s_journal_inum; /* inode number of journal file */
  1230. __le32 s_journal_dev; /* device number of journal file */
  1231. __le32 s_last_orphan; /* start of list of inodes to delete */
  1232. __le32 s_hash_seed[4]; /* HTREE hash seed */
  1233. __u8 s_def_hash_version; /* Default hash version to use */
  1234. __u8 s_jnl_backup_type;
  1235. __le16 s_desc_size; /* size of group descriptor */
  1236. /*100*/ __le32 s_default_mount_opts;
  1237. __le32 s_first_meta_bg; /* First metablock block group */
  1238. __le32 s_mkfs_time; /* When the filesystem was created */
  1239. __le32 s_jnl_blocks[17]; /* Backup of the journal inode */
  1240. /* 64bit support valid if EXT4_FEATURE_INCOMPAT_64BIT */
  1241. /*150*/ __le32 s_blocks_count_hi; /* Blocks count */
  1242. __le32 s_r_blocks_count_hi; /* Reserved blocks count */
  1243. __le32 s_free_blocks_count_hi; /* Free blocks count */
  1244. __le16 s_min_extra_isize; /* All inodes have at least # bytes */
  1245. __le16 s_want_extra_isize; /* New inodes should reserve # bytes */
  1246. __le32 s_flags; /* Miscellaneous flags */
  1247. __le16 s_raid_stride; /* RAID stride */
  1248. __le16 s_mmp_update_interval; /* # seconds to wait in MMP checking */
  1249. __le64 s_mmp_block; /* Block for multi-mount protection */
  1250. __le32 s_raid_stripe_width; /* blocks on all data disks (N*stride)*/
  1251. __u8 s_log_groups_per_flex; /* FLEX_BG group size */
  1252. __u8 s_checksum_type; /* metadata checksum algorithm used */
  1253. __u8 s_encryption_level; /* versioning level for encryption */
  1254. __u8 s_reserved_pad; /* Padding to next 32bits */
  1255. __le64 s_kbytes_written; /* nr of lifetime kilobytes written */
  1256. __le32 s_snapshot_inum; /* Inode number of active snapshot */
  1257. __le32 s_snapshot_id; /* sequential ID of active snapshot */
  1258. __le64 s_snapshot_r_blocks_count; /* reserved blocks for active
  1259. snapshot's future use */
  1260. __le32 s_snapshot_list; /* inode number of the head of the
  1261. on-disk snapshot list */
  1262. #define EXT4_S_ERR_START offsetof(struct ext4_super_block, s_error_count)
  1263. __le32 s_error_count; /* number of fs errors */
  1264. __le32 s_first_error_time; /* first time an error happened */
  1265. __le32 s_first_error_ino; /* inode involved in first error */
  1266. __le64 s_first_error_block; /* block involved of first error */
  1267. __u8 s_first_error_func[32] __nonstring; /* function where the error happened */
  1268. __le32 s_first_error_line; /* line number where error happened */
  1269. __le32 s_last_error_time; /* most recent time of an error */
  1270. __le32 s_last_error_ino; /* inode involved in last error */
  1271. __le32 s_last_error_line; /* line number where error happened */
  1272. __le64 s_last_error_block; /* block involved of last error */
  1273. __u8 s_last_error_func[32] __nonstring; /* function where the error happened */
  1274. #define EXT4_S_ERR_END offsetof(struct ext4_super_block, s_mount_opts)
  1275. __u8 s_mount_opts[64];
  1276. __le32 s_usr_quota_inum; /* inode for tracking user quota */
  1277. __le32 s_grp_quota_inum; /* inode for tracking group quota */
  1278. __le32 s_overhead_clusters; /* overhead blocks/clusters in fs */
  1279. __le32 s_backup_bgs[2]; /* groups with sparse_super2 SBs */
  1280. __u8 s_encrypt_algos[4]; /* Encryption algorithms in use */
  1281. __u8 s_encrypt_pw_salt[16]; /* Salt used for string2key algorithm */
  1282. __le32 s_lpf_ino; /* Location of the lost+found inode */
  1283. __le32 s_prj_quota_inum; /* inode for tracking project quota */
  1284. __le32 s_checksum_seed; /* crc32c(uuid) if csum_seed set */
  1285. __u8 s_wtime_hi;
  1286. __u8 s_mtime_hi;
  1287. __u8 s_mkfs_time_hi;
  1288. __u8 s_lastcheck_hi;
  1289. __u8 s_first_error_time_hi;
  1290. __u8 s_last_error_time_hi;
  1291. __u8 s_first_error_errcode;
  1292. __u8 s_last_error_errcode;
  1293. __le16 s_encoding; /* Filename charset encoding */
  1294. __le16 s_encoding_flags; /* Filename charset encoding flags */
  1295. __le32 s_orphan_file_inum; /* Inode for tracking orphan inodes */
  1296. __le32 s_reserved[94]; /* Padding to the end of the block */
  1297. __le32 s_checksum; /* crc32c(superblock) */
  1298. };
  1299. #define EXT4_S_ERR_LEN (EXT4_S_ERR_END - EXT4_S_ERR_START)
  1300. #ifdef __KERNEL__
  1301. /* Number of quota types we support */
  1302. #define EXT4_MAXQUOTAS 3
  1303. #define EXT4_ENC_UTF8_12_1 1
  1304. /* Types of ext4 journal triggers */
  1305. enum ext4_journal_trigger_type {
  1306. EXT4_JTR_ORPHAN_FILE,
  1307. EXT4_JTR_NONE /* This must be the last entry for indexing to work! */
  1308. };
  1309. #define EXT4_JOURNAL_TRIGGER_COUNT EXT4_JTR_NONE
  1310. struct ext4_journal_trigger {
  1311. struct jbd2_buffer_trigger_type tr_triggers;
  1312. struct super_block *sb;
  1313. };
  1314. static inline struct ext4_journal_trigger *EXT4_TRIGGER(
  1315. struct jbd2_buffer_trigger_type *trigger)
  1316. {
  1317. return container_of(trigger, struct ext4_journal_trigger, tr_triggers);
  1318. }
  1319. #define EXT4_ORPHAN_BLOCK_MAGIC 0x0b10ca04
  1320. /* Structure at the tail of orphan block */
  1321. struct ext4_orphan_block_tail {
  1322. __le32 ob_magic;
  1323. __le32 ob_checksum;
  1324. };
  1325. static inline int ext4_inodes_per_orphan_block(struct super_block *sb)
  1326. {
  1327. return (sb->s_blocksize - sizeof(struct ext4_orphan_block_tail)) /
  1328. sizeof(u32);
  1329. }
  1330. struct ext4_orphan_block {
  1331. atomic_t ob_free_entries; /* Number of free orphan entries in block */
  1332. struct buffer_head *ob_bh; /* Buffer for orphan block */
  1333. };
  1334. /*
  1335. * Info about orphan file.
  1336. */
  1337. struct ext4_orphan_info {
  1338. int of_blocks; /* Number of orphan blocks in a file */
  1339. __u32 of_csum_seed; /* Checksum seed for orphan file */
  1340. struct ext4_orphan_block *of_binfo; /* Array with info about orphan
  1341. * file blocks */
  1342. };
  1343. /*
  1344. * fourth extended-fs super-block data in memory
  1345. */
  1346. struct ext4_sb_info {
  1347. unsigned long s_desc_size; /* Size of a group descriptor in bytes */
  1348. unsigned long s_inodes_per_block;/* Number of inodes per block */
  1349. unsigned long s_blocks_per_group;/* Number of blocks in a group */
  1350. unsigned long s_clusters_per_group; /* Number of clusters in a group */
  1351. unsigned long s_inodes_per_group;/* Number of inodes in a group */
  1352. unsigned long s_itb_per_group; /* Number of inode table blocks per group */
  1353. unsigned long s_gdb_count; /* Number of group descriptor blocks */
  1354. unsigned long s_desc_per_block; /* Number of group descriptors per block */
  1355. ext4_group_t s_groups_count; /* Number of groups in the fs */
  1356. ext4_group_t s_blockfile_groups;/* Groups acceptable for non-extent files */
  1357. unsigned long s_overhead; /* # of fs overhead clusters */
  1358. unsigned int s_cluster_ratio; /* Number of blocks per cluster */
  1359. unsigned int s_cluster_bits; /* log2 of s_cluster_ratio */
  1360. loff_t s_bitmap_maxbytes; /* max bytes for bitmap files */
  1361. struct buffer_head * s_sbh; /* Buffer containing the super block */
  1362. struct ext4_super_block *s_es; /* Pointer to the super block in the buffer */
  1363. /* Array of bh's for the block group descriptors */
  1364. struct buffer_head * __rcu *s_group_desc;
  1365. unsigned int s_mount_opt;
  1366. unsigned int s_mount_opt2;
  1367. unsigned long s_mount_flags;
  1368. unsigned int s_def_mount_opt;
  1369. unsigned int s_def_mount_opt2;
  1370. ext4_fsblk_t s_sb_block;
  1371. atomic64_t s_resv_clusters;
  1372. kuid_t s_resuid;
  1373. kgid_t s_resgid;
  1374. unsigned short s_mount_state;
  1375. unsigned short s_pad;
  1376. int s_addr_per_block_bits;
  1377. int s_desc_per_block_bits;
  1378. int s_inode_size;
  1379. int s_first_ino;
  1380. unsigned int s_inode_readahead_blks;
  1381. unsigned int s_inode_goal;
  1382. u32 s_hash_seed[4];
  1383. int s_def_hash_version;
  1384. int s_hash_unsigned; /* 3 if hash should be unsigned, 0 if not */
  1385. struct percpu_counter s_freeclusters_counter;
  1386. struct percpu_counter s_freeinodes_counter;
  1387. struct percpu_counter s_dirs_counter;
  1388. struct percpu_counter s_dirtyclusters_counter;
  1389. struct percpu_counter s_sra_exceeded_retry_limit;
  1390. struct blockgroup_lock *s_blockgroup_lock;
  1391. struct proc_dir_entry *s_proc;
  1392. struct kobject s_kobj;
  1393. struct completion s_kobj_unregister;
  1394. struct super_block *s_sb;
  1395. struct buffer_head *s_mmp_bh;
  1396. /* Journaling */
  1397. struct journal_s *s_journal;
  1398. unsigned long s_ext4_flags; /* Ext4 superblock flags */
  1399. struct mutex s_orphan_lock; /* Protects on disk list changes */
  1400. struct list_head s_orphan; /* List of orphaned inodes in on disk
  1401. list */
  1402. struct ext4_orphan_info s_orphan_info;
  1403. unsigned long s_commit_interval;
  1404. u32 s_max_batch_time;
  1405. u32 s_min_batch_time;
  1406. struct file *s_journal_bdev_file;
  1407. #ifdef CONFIG_QUOTA
  1408. /* Names of quota files with journalled quota */
  1409. char __rcu *s_qf_names[EXT4_MAXQUOTAS];
  1410. int s_jquota_fmt; /* Format of quota to use */
  1411. #endif
  1412. unsigned int s_want_extra_isize; /* New inodes should reserve # bytes */
  1413. struct ext4_system_blocks __rcu *s_system_blks;
  1414. #ifdef EXTENTS_STATS
  1415. /* ext4 extents stats */
  1416. unsigned long s_ext_min;
  1417. unsigned long s_ext_max;
  1418. unsigned long s_depth_max;
  1419. spinlock_t s_ext_stats_lock;
  1420. unsigned long s_ext_blocks;
  1421. unsigned long s_ext_extents;
  1422. #endif
  1423. /* for buddy allocator */
  1424. struct ext4_group_info ** __rcu *s_group_info;
  1425. struct inode *s_buddy_cache;
  1426. spinlock_t s_md_lock;
  1427. unsigned short *s_mb_offsets;
  1428. unsigned int *s_mb_maxs;
  1429. unsigned int s_group_info_size;
  1430. unsigned int s_mb_free_pending;
  1431. struct list_head s_freed_data_list[2]; /* List of blocks to be freed
  1432. after commit completed */
  1433. struct list_head s_discard_list;
  1434. struct work_struct s_discard_work;
  1435. atomic_t s_retry_alloc_pending;
  1436. struct list_head *s_mb_avg_fragment_size;
  1437. rwlock_t *s_mb_avg_fragment_size_locks;
  1438. struct list_head *s_mb_largest_free_orders;
  1439. rwlock_t *s_mb_largest_free_orders_locks;
  1440. /* tunables */
  1441. unsigned long s_stripe;
  1442. unsigned int s_mb_max_linear_groups;
  1443. unsigned int s_mb_stream_request;
  1444. unsigned int s_mb_max_to_scan;
  1445. unsigned int s_mb_min_to_scan;
  1446. unsigned int s_mb_stats;
  1447. unsigned int s_mb_order2_reqs;
  1448. unsigned int s_mb_group_prealloc;
  1449. unsigned int s_max_dir_size_kb;
  1450. /* where last allocation was done - for stream allocation */
  1451. unsigned long s_mb_last_group;
  1452. unsigned long s_mb_last_start;
  1453. unsigned int s_mb_prefetch;
  1454. unsigned int s_mb_prefetch_limit;
  1455. unsigned int s_mb_best_avail_max_trim_order;
  1456. /* stats for buddy allocator */
  1457. atomic_t s_bal_reqs; /* number of reqs with len > 1 */
  1458. atomic_t s_bal_success; /* we found long enough chunks */
  1459. atomic_t s_bal_allocated; /* in blocks */
  1460. atomic_t s_bal_ex_scanned; /* total extents scanned */
  1461. atomic_t s_bal_cX_ex_scanned[EXT4_MB_NUM_CRS]; /* total extents scanned */
  1462. atomic_t s_bal_groups_scanned; /* number of groups scanned */
  1463. atomic_t s_bal_goals; /* goal hits */
  1464. atomic_t s_bal_len_goals; /* len goal hits */
  1465. atomic_t s_bal_breaks; /* too long searches */
  1466. atomic_t s_bal_2orders; /* 2^order hits */
  1467. atomic_t s_bal_p2_aligned_bad_suggestions;
  1468. atomic_t s_bal_goal_fast_bad_suggestions;
  1469. atomic_t s_bal_best_avail_bad_suggestions;
  1470. atomic64_t s_bal_cX_groups_considered[EXT4_MB_NUM_CRS];
  1471. atomic64_t s_bal_cX_hits[EXT4_MB_NUM_CRS];
  1472. atomic64_t s_bal_cX_failed[EXT4_MB_NUM_CRS]; /* cX loop didn't find blocks */
  1473. atomic_t s_mb_buddies_generated; /* number of buddies generated */
  1474. atomic64_t s_mb_generation_time;
  1475. atomic_t s_mb_lost_chunks;
  1476. atomic_t s_mb_preallocated;
  1477. atomic_t s_mb_discarded;
  1478. atomic_t s_lock_busy;
  1479. /* locality groups */
  1480. struct ext4_locality_group __percpu *s_locality_groups;
  1481. /* for write statistics */
  1482. unsigned long s_sectors_written_start;
  1483. u64 s_kbytes_written;
  1484. /* the size of zero-out chunk */
  1485. unsigned int s_extent_max_zeroout_kb;
  1486. unsigned int s_log_groups_per_flex;
  1487. struct flex_groups * __rcu *s_flex_groups;
  1488. ext4_group_t s_flex_groups_allocated;
  1489. /* workqueue for reserved extent conversions (buffered io) */
  1490. struct workqueue_struct *rsv_conversion_wq;
  1491. /* timer for periodic error stats printing */
  1492. struct timer_list s_err_report;
  1493. /* Lazy inode table initialization info */
  1494. struct ext4_li_request *s_li_request;
  1495. /* Wait multiplier for lazy initialization thread */
  1496. unsigned int s_li_wait_mult;
  1497. /* Kernel thread for multiple mount protection */
  1498. struct task_struct *s_mmp_tsk;
  1499. /* record the last minlen when FITRIM is called. */
  1500. unsigned long s_last_trim_minblks;
  1501. /* Reference to checksum algorithm driver via cryptoapi */
  1502. struct crypto_shash *s_chksum_driver;
  1503. /* Precomputed FS UUID checksum for seeding other checksums */
  1504. __u32 s_csum_seed;
  1505. /* Reclaim extents from extent status tree */
  1506. struct shrinker *s_es_shrinker;
  1507. struct list_head s_es_list; /* List of inodes with reclaimable extents */
  1508. long s_es_nr_inode;
  1509. struct ext4_es_stats s_es_stats;
  1510. struct mb_cache *s_ea_block_cache;
  1511. struct mb_cache *s_ea_inode_cache;
  1512. spinlock_t s_es_lock ____cacheline_aligned_in_smp;
  1513. /* Journal triggers for checksum computation */
  1514. struct ext4_journal_trigger s_journal_triggers[EXT4_JOURNAL_TRIGGER_COUNT];
  1515. /* Ratelimit ext4 messages. */
  1516. struct ratelimit_state s_err_ratelimit_state;
  1517. struct ratelimit_state s_warning_ratelimit_state;
  1518. struct ratelimit_state s_msg_ratelimit_state;
  1519. atomic_t s_warning_count;
  1520. atomic_t s_msg_count;
  1521. /* Encryption policy for '-o test_dummy_encryption' */
  1522. struct fscrypt_dummy_policy s_dummy_enc_policy;
  1523. /*
  1524. * Barrier between writepages ops and changing any inode's JOURNAL_DATA
  1525. * or EXTENTS flag or between writepages ops and changing DELALLOC or
  1526. * DIOREAD_NOLOCK mount options on remount.
  1527. */
  1528. struct percpu_rw_semaphore s_writepages_rwsem;
  1529. struct dax_device *s_daxdev;
  1530. u64 s_dax_part_off;
  1531. #ifdef CONFIG_EXT4_DEBUG
  1532. unsigned long s_simulate_fail;
  1533. #endif
  1534. /* Record the errseq of the backing block device */
  1535. errseq_t s_bdev_wb_err;
  1536. spinlock_t s_bdev_wb_lock;
  1537. /* Information about errors that happened during this mount */
  1538. spinlock_t s_error_lock;
  1539. int s_add_error_count;
  1540. int s_first_error_code;
  1541. __u32 s_first_error_line;
  1542. __u32 s_first_error_ino;
  1543. __u64 s_first_error_block;
  1544. const char *s_first_error_func;
  1545. time64_t s_first_error_time;
  1546. int s_last_error_code;
  1547. __u32 s_last_error_line;
  1548. __u32 s_last_error_ino;
  1549. __u64 s_last_error_block;
  1550. const char *s_last_error_func;
  1551. time64_t s_last_error_time;
  1552. /*
  1553. * If we are in a context where we cannot update the on-disk
  1554. * superblock, we queue the work here. This is used to update
  1555. * the error information in the superblock, and for periodic
  1556. * updates of the superblock called from the commit callback
  1557. * function.
  1558. */
  1559. struct work_struct s_sb_upd_work;
  1560. /* Ext4 fast commit sub transaction ID */
  1561. atomic_t s_fc_subtid;
  1562. /*
  1563. * After commit starts, the main queue gets locked, and the further
  1564. * updates get added in the staging queue.
  1565. */
  1566. #define FC_Q_MAIN 0
  1567. #define FC_Q_STAGING 1
  1568. struct list_head s_fc_q[2]; /* Inodes staged for fast commit
  1569. * that have data changes in them.
  1570. */
  1571. struct list_head s_fc_dentry_q[2]; /* directory entry updates */
  1572. unsigned int s_fc_bytes;
  1573. /*
  1574. * Main fast commit lock. This lock protects accesses to the
  1575. * following fields:
  1576. * ei->i_fc_list, s_fc_dentry_q, s_fc_q, s_fc_bytes, s_fc_bh.
  1577. */
  1578. spinlock_t s_fc_lock;
  1579. struct buffer_head *s_fc_bh;
  1580. struct ext4_fc_stats s_fc_stats;
  1581. tid_t s_fc_ineligible_tid;
  1582. #ifdef CONFIG_EXT4_DEBUG
  1583. int s_fc_debug_max_replay;
  1584. #endif
  1585. struct ext4_fc_replay_state s_fc_replay_state;
  1586. };
  1587. static inline struct ext4_sb_info *EXT4_SB(struct super_block *sb)
  1588. {
  1589. return sb->s_fs_info;
  1590. }
  1591. static inline struct ext4_inode_info *EXT4_I(struct inode *inode)
  1592. {
  1593. return container_of(inode, struct ext4_inode_info, vfs_inode);
  1594. }
  1595. static inline int ext4_writepages_down_read(struct super_block *sb)
  1596. {
  1597. percpu_down_read(&EXT4_SB(sb)->s_writepages_rwsem);
  1598. return memalloc_nofs_save();
  1599. }
  1600. static inline void ext4_writepages_up_read(struct super_block *sb, int ctx)
  1601. {
  1602. memalloc_nofs_restore(ctx);
  1603. percpu_up_read(&EXT4_SB(sb)->s_writepages_rwsem);
  1604. }
  1605. static inline int ext4_writepages_down_write(struct super_block *sb)
  1606. {
  1607. percpu_down_write(&EXT4_SB(sb)->s_writepages_rwsem);
  1608. return memalloc_nofs_save();
  1609. }
  1610. static inline void ext4_writepages_up_write(struct super_block *sb, int ctx)
  1611. {
  1612. memalloc_nofs_restore(ctx);
  1613. percpu_up_write(&EXT4_SB(sb)->s_writepages_rwsem);
  1614. }
  1615. static inline int ext4_valid_inum(struct super_block *sb, unsigned long ino)
  1616. {
  1617. return ino == EXT4_ROOT_INO ||
  1618. (ino >= EXT4_FIRST_INO(sb) &&
  1619. ino <= le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count));
  1620. }
  1621. /*
  1622. * Returns: sbi->field[index]
  1623. * Used to access an array element from the following sbi fields which require
  1624. * rcu protection to avoid dereferencing an invalid pointer due to reassignment
  1625. * - s_group_desc
  1626. * - s_group_info
  1627. * - s_flex_group
  1628. */
  1629. #define sbi_array_rcu_deref(sbi, field, index) \
  1630. ({ \
  1631. typeof(*((sbi)->field)) _v; \
  1632. rcu_read_lock(); \
  1633. _v = ((typeof(_v)*)rcu_dereference((sbi)->field))[index]; \
  1634. rcu_read_unlock(); \
  1635. _v; \
  1636. })
  1637. /*
  1638. * run-time mount flags
  1639. */
  1640. enum {
  1641. EXT4_MF_MNTDIR_SAMPLED,
  1642. EXT4_MF_FC_INELIGIBLE, /* Fast commit ineligible */
  1643. EXT4_MF_JOURNAL_DESTROY /* Journal is in process of destroying */
  1644. };
  1645. static inline void ext4_set_mount_flag(struct super_block *sb, int bit)
  1646. {
  1647. set_bit(bit, &EXT4_SB(sb)->s_mount_flags);
  1648. }
  1649. static inline void ext4_clear_mount_flag(struct super_block *sb, int bit)
  1650. {
  1651. clear_bit(bit, &EXT4_SB(sb)->s_mount_flags);
  1652. }
  1653. static inline int ext4_test_mount_flag(struct super_block *sb, int bit)
  1654. {
  1655. return test_bit(bit, &EXT4_SB(sb)->s_mount_flags);
  1656. }
  1657. /*
  1658. * Simulate_fail codes
  1659. */
  1660. #define EXT4_SIM_BBITMAP_EIO 1
  1661. #define EXT4_SIM_BBITMAP_CRC 2
  1662. #define EXT4_SIM_IBITMAP_EIO 3
  1663. #define EXT4_SIM_IBITMAP_CRC 4
  1664. #define EXT4_SIM_INODE_EIO 5
  1665. #define EXT4_SIM_INODE_CRC 6
  1666. #define EXT4_SIM_DIRBLOCK_EIO 7
  1667. #define EXT4_SIM_DIRBLOCK_CRC 8
  1668. static inline bool ext4_simulate_fail(struct super_block *sb,
  1669. unsigned long code)
  1670. {
  1671. #ifdef CONFIG_EXT4_DEBUG
  1672. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1673. if (unlikely(sbi->s_simulate_fail == code)) {
  1674. sbi->s_simulate_fail = 0;
  1675. return true;
  1676. }
  1677. #endif
  1678. return false;
  1679. }
  1680. /*
  1681. * Error number codes for s_{first,last}_error_errno
  1682. *
  1683. * Linux errno numbers are architecture specific, so we need to translate
  1684. * them into something which is architecture independent. We don't define
  1685. * codes for all errno's; just the ones which are most likely to be the cause
  1686. * of an ext4_error() call.
  1687. */
  1688. #define EXT4_ERR_UNKNOWN 1
  1689. #define EXT4_ERR_EIO 2
  1690. #define EXT4_ERR_ENOMEM 3
  1691. #define EXT4_ERR_EFSBADCRC 4
  1692. #define EXT4_ERR_EFSCORRUPTED 5
  1693. #define EXT4_ERR_ENOSPC 6
  1694. #define EXT4_ERR_ENOKEY 7
  1695. #define EXT4_ERR_EROFS 8
  1696. #define EXT4_ERR_EFBIG 9
  1697. #define EXT4_ERR_EEXIST 10
  1698. #define EXT4_ERR_ERANGE 11
  1699. #define EXT4_ERR_EOVERFLOW 12
  1700. #define EXT4_ERR_EBUSY 13
  1701. #define EXT4_ERR_ENOTDIR 14
  1702. #define EXT4_ERR_ENOTEMPTY 15
  1703. #define EXT4_ERR_ESHUTDOWN 16
  1704. #define EXT4_ERR_EFAULT 17
  1705. /*
  1706. * Inode dynamic state flags
  1707. */
  1708. enum {
  1709. EXT4_STATE_NEW, /* inode is newly created */
  1710. EXT4_STATE_XATTR, /* has in-inode xattrs */
  1711. EXT4_STATE_NO_EXPAND, /* No space for expansion */
  1712. EXT4_STATE_DA_ALLOC_CLOSE, /* Alloc DA blks on close */
  1713. EXT4_STATE_EXT_MIGRATE, /* Inode is migrating */
  1714. EXT4_STATE_NEWENTRY, /* File just added to dir */
  1715. EXT4_STATE_MAY_INLINE_DATA, /* may have in-inode data */
  1716. EXT4_STATE_EXT_PRECACHED, /* extents have been precached */
  1717. EXT4_STATE_LUSTRE_EA_INODE, /* Lustre-style ea_inode */
  1718. EXT4_STATE_VERITY_IN_PROGRESS, /* building fs-verity Merkle tree */
  1719. EXT4_STATE_FC_COMMITTING, /* Fast commit ongoing */
  1720. EXT4_STATE_ORPHAN_FILE, /* Inode orphaned in orphan file */
  1721. };
  1722. #define EXT4_INODE_BIT_FNS(name, field, offset) \
  1723. static inline int ext4_test_inode_##name(struct inode *inode, int bit) \
  1724. { \
  1725. return test_bit(bit + (offset), &EXT4_I(inode)->i_##field); \
  1726. } \
  1727. static inline void ext4_set_inode_##name(struct inode *inode, int bit) \
  1728. { \
  1729. set_bit(bit + (offset), &EXT4_I(inode)->i_##field); \
  1730. } \
  1731. static inline void ext4_clear_inode_##name(struct inode *inode, int bit) \
  1732. { \
  1733. clear_bit(bit + (offset), &EXT4_I(inode)->i_##field); \
  1734. }
  1735. /* Add these declarations here only so that these functions can be
  1736. * found by name. Otherwise, they are very hard to locate. */
  1737. static inline int ext4_test_inode_flag(struct inode *inode, int bit);
  1738. static inline void ext4_set_inode_flag(struct inode *inode, int bit);
  1739. static inline void ext4_clear_inode_flag(struct inode *inode, int bit);
  1740. EXT4_INODE_BIT_FNS(flag, flags, 0)
  1741. /* Add these declarations here only so that these functions can be
  1742. * found by name. Otherwise, they are very hard to locate. */
  1743. static inline int ext4_test_inode_state(struct inode *inode, int bit);
  1744. static inline void ext4_set_inode_state(struct inode *inode, int bit);
  1745. static inline void ext4_clear_inode_state(struct inode *inode, int bit);
  1746. #if (BITS_PER_LONG < 64)
  1747. EXT4_INODE_BIT_FNS(state, state_flags, 0)
  1748. static inline void ext4_clear_state_flags(struct ext4_inode_info *ei)
  1749. {
  1750. (ei)->i_state_flags = 0;
  1751. }
  1752. #else
  1753. EXT4_INODE_BIT_FNS(state, flags, 32)
  1754. static inline void ext4_clear_state_flags(struct ext4_inode_info *ei)
  1755. {
  1756. /* We depend on the fact that callers will set i_flags */
  1757. }
  1758. #endif
  1759. #else
  1760. /* Assume that user mode programs are passing in an ext4fs superblock, not
  1761. * a kernel struct super_block. This will allow us to call the feature-test
  1762. * macros from user land. */
  1763. #define EXT4_SB(sb) (sb)
  1764. #endif
  1765. static inline bool ext4_verity_in_progress(struct inode *inode)
  1766. {
  1767. return IS_ENABLED(CONFIG_FS_VERITY) &&
  1768. ext4_test_inode_state(inode, EXT4_STATE_VERITY_IN_PROGRESS);
  1769. }
  1770. #define NEXT_ORPHAN(inode) EXT4_I(inode)->i_dtime
  1771. /*
  1772. * Check whether the inode is tracked as orphan (either in orphan file or
  1773. * orphan list).
  1774. */
  1775. static inline bool ext4_inode_orphan_tracked(struct inode *inode)
  1776. {
  1777. return ext4_test_inode_state(inode, EXT4_STATE_ORPHAN_FILE) ||
  1778. !list_empty(&EXT4_I(inode)->i_orphan);
  1779. }
  1780. /*
  1781. * Codes for operating systems
  1782. */
  1783. #define EXT4_OS_LINUX 0
  1784. #define EXT4_OS_HURD 1
  1785. #define EXT4_OS_MASIX 2
  1786. #define EXT4_OS_FREEBSD 3
  1787. #define EXT4_OS_LITES 4
  1788. /*
  1789. * Revision levels
  1790. */
  1791. #define EXT4_GOOD_OLD_REV 0 /* The good old (original) format */
  1792. #define EXT4_DYNAMIC_REV 1 /* V2 format w/ dynamic inode sizes */
  1793. #define EXT4_MAX_SUPP_REV EXT4_DYNAMIC_REV
  1794. #define EXT4_GOOD_OLD_INODE_SIZE 128
  1795. #define EXT4_EXTRA_TIMESTAMP_MAX (((s64)1 << 34) - 1 + S32_MIN)
  1796. #define EXT4_NON_EXTRA_TIMESTAMP_MAX S32_MAX
  1797. #define EXT4_TIMESTAMP_MIN S32_MIN
  1798. /*
  1799. * Feature set definitions
  1800. */
  1801. #define EXT4_FEATURE_COMPAT_DIR_PREALLOC 0x0001
  1802. #define EXT4_FEATURE_COMPAT_IMAGIC_INODES 0x0002
  1803. #define EXT4_FEATURE_COMPAT_HAS_JOURNAL 0x0004
  1804. #define EXT4_FEATURE_COMPAT_EXT_ATTR 0x0008
  1805. #define EXT4_FEATURE_COMPAT_RESIZE_INODE 0x0010
  1806. #define EXT4_FEATURE_COMPAT_DIR_INDEX 0x0020
  1807. #define EXT4_FEATURE_COMPAT_SPARSE_SUPER2 0x0200
  1808. /*
  1809. * The reason why "FAST_COMMIT" is a compat feature is that, FS becomes
  1810. * incompatible only if fast commit blocks are present in the FS. Since we
  1811. * clear the journal (and thus the fast commit blocks), we don't mark FS as
  1812. * incompatible. We also have a JBD2 incompat feature, which gets set when
  1813. * there are fast commit blocks present in the journal.
  1814. */
  1815. #define EXT4_FEATURE_COMPAT_FAST_COMMIT 0x0400
  1816. #define EXT4_FEATURE_COMPAT_STABLE_INODES 0x0800
  1817. #define EXT4_FEATURE_COMPAT_ORPHAN_FILE 0x1000 /* Orphan file exists */
  1818. #define EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER 0x0001
  1819. #define EXT4_FEATURE_RO_COMPAT_LARGE_FILE 0x0002
  1820. #define EXT4_FEATURE_RO_COMPAT_BTREE_DIR 0x0004
  1821. #define EXT4_FEATURE_RO_COMPAT_HUGE_FILE 0x0008
  1822. #define EXT4_FEATURE_RO_COMPAT_GDT_CSUM 0x0010
  1823. #define EXT4_FEATURE_RO_COMPAT_DIR_NLINK 0x0020
  1824. #define EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE 0x0040
  1825. #define EXT4_FEATURE_RO_COMPAT_QUOTA 0x0100
  1826. #define EXT4_FEATURE_RO_COMPAT_BIGALLOC 0x0200
  1827. /*
  1828. * METADATA_CSUM also enables group descriptor checksums (GDT_CSUM). When
  1829. * METADATA_CSUM is set, group descriptor checksums use the same algorithm as
  1830. * all other data structures' checksums. However, the METADATA_CSUM and
  1831. * GDT_CSUM bits are mutually exclusive.
  1832. */
  1833. #define EXT4_FEATURE_RO_COMPAT_METADATA_CSUM 0x0400
  1834. #define EXT4_FEATURE_RO_COMPAT_READONLY 0x1000
  1835. #define EXT4_FEATURE_RO_COMPAT_PROJECT 0x2000
  1836. #define EXT4_FEATURE_RO_COMPAT_VERITY 0x8000
  1837. #define EXT4_FEATURE_RO_COMPAT_ORPHAN_PRESENT 0x10000 /* Orphan file may be
  1838. non-empty */
  1839. #define EXT4_FEATURE_INCOMPAT_COMPRESSION 0x0001
  1840. #define EXT4_FEATURE_INCOMPAT_FILETYPE 0x0002
  1841. #define EXT4_FEATURE_INCOMPAT_RECOVER 0x0004 /* Needs recovery */
  1842. #define EXT4_FEATURE_INCOMPAT_JOURNAL_DEV 0x0008 /* Journal device */
  1843. #define EXT4_FEATURE_INCOMPAT_META_BG 0x0010
  1844. #define EXT4_FEATURE_INCOMPAT_EXTENTS 0x0040 /* extents support */
  1845. #define EXT4_FEATURE_INCOMPAT_64BIT 0x0080
  1846. #define EXT4_FEATURE_INCOMPAT_MMP 0x0100
  1847. #define EXT4_FEATURE_INCOMPAT_FLEX_BG 0x0200
  1848. #define EXT4_FEATURE_INCOMPAT_EA_INODE 0x0400 /* EA in inode */
  1849. #define EXT4_FEATURE_INCOMPAT_DIRDATA 0x1000 /* data in dirent */
  1850. #define EXT4_FEATURE_INCOMPAT_CSUM_SEED 0x2000
  1851. #define EXT4_FEATURE_INCOMPAT_LARGEDIR 0x4000 /* >2GB or 3-lvl htree */
  1852. #define EXT4_FEATURE_INCOMPAT_INLINE_DATA 0x8000 /* data in inode */
  1853. #define EXT4_FEATURE_INCOMPAT_ENCRYPT 0x10000
  1854. #define EXT4_FEATURE_INCOMPAT_CASEFOLD 0x20000
  1855. extern void ext4_update_dynamic_rev(struct super_block *sb);
  1856. #define EXT4_FEATURE_COMPAT_FUNCS(name, flagname) \
  1857. static inline bool ext4_has_feature_##name(struct super_block *sb) \
  1858. { \
  1859. return ((EXT4_SB(sb)->s_es->s_feature_compat & \
  1860. cpu_to_le32(EXT4_FEATURE_COMPAT_##flagname)) != 0); \
  1861. } \
  1862. static inline void ext4_set_feature_##name(struct super_block *sb) \
  1863. { \
  1864. ext4_update_dynamic_rev(sb); \
  1865. EXT4_SB(sb)->s_es->s_feature_compat |= \
  1866. cpu_to_le32(EXT4_FEATURE_COMPAT_##flagname); \
  1867. } \
  1868. static inline void ext4_clear_feature_##name(struct super_block *sb) \
  1869. { \
  1870. EXT4_SB(sb)->s_es->s_feature_compat &= \
  1871. ~cpu_to_le32(EXT4_FEATURE_COMPAT_##flagname); \
  1872. }
  1873. #define EXT4_FEATURE_RO_COMPAT_FUNCS(name, flagname) \
  1874. static inline bool ext4_has_feature_##name(struct super_block *sb) \
  1875. { \
  1876. return ((EXT4_SB(sb)->s_es->s_feature_ro_compat & \
  1877. cpu_to_le32(EXT4_FEATURE_RO_COMPAT_##flagname)) != 0); \
  1878. } \
  1879. static inline void ext4_set_feature_##name(struct super_block *sb) \
  1880. { \
  1881. ext4_update_dynamic_rev(sb); \
  1882. EXT4_SB(sb)->s_es->s_feature_ro_compat |= \
  1883. cpu_to_le32(EXT4_FEATURE_RO_COMPAT_##flagname); \
  1884. } \
  1885. static inline void ext4_clear_feature_##name(struct super_block *sb) \
  1886. { \
  1887. EXT4_SB(sb)->s_es->s_feature_ro_compat &= \
  1888. ~cpu_to_le32(EXT4_FEATURE_RO_COMPAT_##flagname); \
  1889. }
  1890. #define EXT4_FEATURE_INCOMPAT_FUNCS(name, flagname) \
  1891. static inline bool ext4_has_feature_##name(struct super_block *sb) \
  1892. { \
  1893. return ((EXT4_SB(sb)->s_es->s_feature_incompat & \
  1894. cpu_to_le32(EXT4_FEATURE_INCOMPAT_##flagname)) != 0); \
  1895. } \
  1896. static inline void ext4_set_feature_##name(struct super_block *sb) \
  1897. { \
  1898. ext4_update_dynamic_rev(sb); \
  1899. EXT4_SB(sb)->s_es->s_feature_incompat |= \
  1900. cpu_to_le32(EXT4_FEATURE_INCOMPAT_##flagname); \
  1901. } \
  1902. static inline void ext4_clear_feature_##name(struct super_block *sb) \
  1903. { \
  1904. EXT4_SB(sb)->s_es->s_feature_incompat &= \
  1905. ~cpu_to_le32(EXT4_FEATURE_INCOMPAT_##flagname); \
  1906. }
  1907. EXT4_FEATURE_COMPAT_FUNCS(dir_prealloc, DIR_PREALLOC)
  1908. EXT4_FEATURE_COMPAT_FUNCS(imagic_inodes, IMAGIC_INODES)
  1909. EXT4_FEATURE_COMPAT_FUNCS(journal, HAS_JOURNAL)
  1910. EXT4_FEATURE_COMPAT_FUNCS(xattr, EXT_ATTR)
  1911. EXT4_FEATURE_COMPAT_FUNCS(resize_inode, RESIZE_INODE)
  1912. EXT4_FEATURE_COMPAT_FUNCS(dir_index, DIR_INDEX)
  1913. EXT4_FEATURE_COMPAT_FUNCS(sparse_super2, SPARSE_SUPER2)
  1914. EXT4_FEATURE_COMPAT_FUNCS(fast_commit, FAST_COMMIT)
  1915. EXT4_FEATURE_COMPAT_FUNCS(stable_inodes, STABLE_INODES)
  1916. EXT4_FEATURE_COMPAT_FUNCS(orphan_file, ORPHAN_FILE)
  1917. EXT4_FEATURE_RO_COMPAT_FUNCS(sparse_super, SPARSE_SUPER)
  1918. EXT4_FEATURE_RO_COMPAT_FUNCS(large_file, LARGE_FILE)
  1919. EXT4_FEATURE_RO_COMPAT_FUNCS(btree_dir, BTREE_DIR)
  1920. EXT4_FEATURE_RO_COMPAT_FUNCS(huge_file, HUGE_FILE)
  1921. EXT4_FEATURE_RO_COMPAT_FUNCS(gdt_csum, GDT_CSUM)
  1922. EXT4_FEATURE_RO_COMPAT_FUNCS(dir_nlink, DIR_NLINK)
  1923. EXT4_FEATURE_RO_COMPAT_FUNCS(extra_isize, EXTRA_ISIZE)
  1924. EXT4_FEATURE_RO_COMPAT_FUNCS(quota, QUOTA)
  1925. EXT4_FEATURE_RO_COMPAT_FUNCS(bigalloc, BIGALLOC)
  1926. EXT4_FEATURE_RO_COMPAT_FUNCS(metadata_csum, METADATA_CSUM)
  1927. EXT4_FEATURE_RO_COMPAT_FUNCS(readonly, READONLY)
  1928. EXT4_FEATURE_RO_COMPAT_FUNCS(project, PROJECT)
  1929. EXT4_FEATURE_RO_COMPAT_FUNCS(verity, VERITY)
  1930. EXT4_FEATURE_RO_COMPAT_FUNCS(orphan_present, ORPHAN_PRESENT)
  1931. EXT4_FEATURE_INCOMPAT_FUNCS(compression, COMPRESSION)
  1932. EXT4_FEATURE_INCOMPAT_FUNCS(filetype, FILETYPE)
  1933. EXT4_FEATURE_INCOMPAT_FUNCS(journal_needs_recovery, RECOVER)
  1934. EXT4_FEATURE_INCOMPAT_FUNCS(journal_dev, JOURNAL_DEV)
  1935. EXT4_FEATURE_INCOMPAT_FUNCS(meta_bg, META_BG)
  1936. EXT4_FEATURE_INCOMPAT_FUNCS(extents, EXTENTS)
  1937. EXT4_FEATURE_INCOMPAT_FUNCS(64bit, 64BIT)
  1938. EXT4_FEATURE_INCOMPAT_FUNCS(mmp, MMP)
  1939. EXT4_FEATURE_INCOMPAT_FUNCS(flex_bg, FLEX_BG)
  1940. EXT4_FEATURE_INCOMPAT_FUNCS(ea_inode, EA_INODE)
  1941. EXT4_FEATURE_INCOMPAT_FUNCS(dirdata, DIRDATA)
  1942. EXT4_FEATURE_INCOMPAT_FUNCS(csum_seed, CSUM_SEED)
  1943. EXT4_FEATURE_INCOMPAT_FUNCS(largedir, LARGEDIR)
  1944. EXT4_FEATURE_INCOMPAT_FUNCS(inline_data, INLINE_DATA)
  1945. EXT4_FEATURE_INCOMPAT_FUNCS(encrypt, ENCRYPT)
  1946. EXT4_FEATURE_INCOMPAT_FUNCS(casefold, CASEFOLD)
  1947. #define EXT2_FEATURE_COMPAT_SUPP EXT4_FEATURE_COMPAT_EXT_ATTR
  1948. #define EXT2_FEATURE_INCOMPAT_SUPP (EXT4_FEATURE_INCOMPAT_FILETYPE| \
  1949. EXT4_FEATURE_INCOMPAT_META_BG)
  1950. #define EXT2_FEATURE_RO_COMPAT_SUPP (EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER| \
  1951. EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \
  1952. EXT4_FEATURE_RO_COMPAT_BTREE_DIR)
  1953. #define EXT3_FEATURE_COMPAT_SUPP EXT4_FEATURE_COMPAT_EXT_ATTR
  1954. #define EXT3_FEATURE_INCOMPAT_SUPP (EXT4_FEATURE_INCOMPAT_FILETYPE| \
  1955. EXT4_FEATURE_INCOMPAT_RECOVER| \
  1956. EXT4_FEATURE_INCOMPAT_META_BG)
  1957. #define EXT3_FEATURE_RO_COMPAT_SUPP (EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER| \
  1958. EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \
  1959. EXT4_FEATURE_RO_COMPAT_BTREE_DIR)
  1960. #define EXT4_FEATURE_COMPAT_SUPP (EXT4_FEATURE_COMPAT_EXT_ATTR| \
  1961. EXT4_FEATURE_COMPAT_ORPHAN_FILE)
  1962. #define EXT4_FEATURE_INCOMPAT_SUPP (EXT4_FEATURE_INCOMPAT_FILETYPE| \
  1963. EXT4_FEATURE_INCOMPAT_RECOVER| \
  1964. EXT4_FEATURE_INCOMPAT_META_BG| \
  1965. EXT4_FEATURE_INCOMPAT_EXTENTS| \
  1966. EXT4_FEATURE_INCOMPAT_64BIT| \
  1967. EXT4_FEATURE_INCOMPAT_FLEX_BG| \
  1968. EXT4_FEATURE_INCOMPAT_EA_INODE| \
  1969. EXT4_FEATURE_INCOMPAT_MMP | \
  1970. EXT4_FEATURE_INCOMPAT_INLINE_DATA | \
  1971. EXT4_FEATURE_INCOMPAT_ENCRYPT | \
  1972. EXT4_FEATURE_INCOMPAT_CASEFOLD | \
  1973. EXT4_FEATURE_INCOMPAT_CSUM_SEED | \
  1974. EXT4_FEATURE_INCOMPAT_LARGEDIR)
  1975. #define EXT4_FEATURE_RO_COMPAT_SUPP (EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER| \
  1976. EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \
  1977. EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
  1978. EXT4_FEATURE_RO_COMPAT_DIR_NLINK | \
  1979. EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE | \
  1980. EXT4_FEATURE_RO_COMPAT_BTREE_DIR |\
  1981. EXT4_FEATURE_RO_COMPAT_HUGE_FILE |\
  1982. EXT4_FEATURE_RO_COMPAT_BIGALLOC |\
  1983. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM|\
  1984. EXT4_FEATURE_RO_COMPAT_QUOTA |\
  1985. EXT4_FEATURE_RO_COMPAT_PROJECT |\
  1986. EXT4_FEATURE_RO_COMPAT_VERITY |\
  1987. EXT4_FEATURE_RO_COMPAT_ORPHAN_PRESENT)
  1988. #define EXTN_FEATURE_FUNCS(ver) \
  1989. static inline bool ext4_has_unknown_ext##ver##_compat_features(struct super_block *sb) \
  1990. { \
  1991. return ((EXT4_SB(sb)->s_es->s_feature_compat & \
  1992. cpu_to_le32(~EXT##ver##_FEATURE_COMPAT_SUPP)) != 0); \
  1993. } \
  1994. static inline bool ext4_has_unknown_ext##ver##_ro_compat_features(struct super_block *sb) \
  1995. { \
  1996. return ((EXT4_SB(sb)->s_es->s_feature_ro_compat & \
  1997. cpu_to_le32(~EXT##ver##_FEATURE_RO_COMPAT_SUPP)) != 0); \
  1998. } \
  1999. static inline bool ext4_has_unknown_ext##ver##_incompat_features(struct super_block *sb) \
  2000. { \
  2001. return ((EXT4_SB(sb)->s_es->s_feature_incompat & \
  2002. cpu_to_le32(~EXT##ver##_FEATURE_INCOMPAT_SUPP)) != 0); \
  2003. }
  2004. EXTN_FEATURE_FUNCS(2)
  2005. EXTN_FEATURE_FUNCS(3)
  2006. EXTN_FEATURE_FUNCS(4)
  2007. static inline bool ext4_has_compat_features(struct super_block *sb)
  2008. {
  2009. return (EXT4_SB(sb)->s_es->s_feature_compat != 0);
  2010. }
  2011. static inline bool ext4_has_ro_compat_features(struct super_block *sb)
  2012. {
  2013. return (EXT4_SB(sb)->s_es->s_feature_ro_compat != 0);
  2014. }
  2015. static inline bool ext4_has_incompat_features(struct super_block *sb)
  2016. {
  2017. return (EXT4_SB(sb)->s_es->s_feature_incompat != 0);
  2018. }
  2019. extern int ext4_feature_set_ok(struct super_block *sb, int readonly);
  2020. /*
  2021. * Superblock flags
  2022. */
  2023. #define EXT4_FLAGS_RESIZING 0
  2024. #define EXT4_FLAGS_SHUTDOWN 1
  2025. #define EXT4_FLAGS_BDEV_IS_DAX 2
  2026. static inline int ext4_forced_shutdown(struct super_block *sb)
  2027. {
  2028. return test_bit(EXT4_FLAGS_SHUTDOWN, &EXT4_SB(sb)->s_ext4_flags);
  2029. }
  2030. /*
  2031. * Default values for user and/or group using reserved blocks
  2032. */
  2033. #define EXT4_DEF_RESUID 0
  2034. #define EXT4_DEF_RESGID 0
  2035. /*
  2036. * Default project ID
  2037. */
  2038. #define EXT4_DEF_PROJID 0
  2039. #define EXT4_DEF_INODE_READAHEAD_BLKS 32
  2040. /*
  2041. * Default mount options
  2042. */
  2043. #define EXT4_DEFM_DEBUG 0x0001
  2044. #define EXT4_DEFM_BSDGROUPS 0x0002
  2045. #define EXT4_DEFM_XATTR_USER 0x0004
  2046. #define EXT4_DEFM_ACL 0x0008
  2047. #define EXT4_DEFM_UID16 0x0010
  2048. #define EXT4_DEFM_JMODE 0x0060
  2049. #define EXT4_DEFM_JMODE_DATA 0x0020
  2050. #define EXT4_DEFM_JMODE_ORDERED 0x0040
  2051. #define EXT4_DEFM_JMODE_WBACK 0x0060
  2052. #define EXT4_DEFM_NOBARRIER 0x0100
  2053. #define EXT4_DEFM_BLOCK_VALIDITY 0x0200
  2054. #define EXT4_DEFM_DISCARD 0x0400
  2055. #define EXT4_DEFM_NODELALLOC 0x0800
  2056. /*
  2057. * Default journal batch times
  2058. */
  2059. #define EXT4_DEF_MIN_BATCH_TIME 0
  2060. #define EXT4_DEF_MAX_BATCH_TIME 15000 /* 15ms */
  2061. /*
  2062. * Minimum number of groups in a flexgroup before we separate out
  2063. * directories into the first block group of a flexgroup
  2064. */
  2065. #define EXT4_FLEX_SIZE_DIR_ALLOC_SCHEME 4
  2066. /*
  2067. * Structure of a directory entry
  2068. */
  2069. #define EXT4_NAME_LEN 255
  2070. /*
  2071. * Base length of the ext4 directory entry excluding the name length
  2072. */
  2073. #define EXT4_BASE_DIR_LEN (sizeof(struct ext4_dir_entry_2) - EXT4_NAME_LEN)
  2074. struct ext4_dir_entry {
  2075. __le32 inode; /* Inode number */
  2076. __le16 rec_len; /* Directory entry length */
  2077. __le16 name_len; /* Name length */
  2078. char name[EXT4_NAME_LEN]; /* File name */
  2079. };
  2080. /*
  2081. * Encrypted Casefolded entries require saving the hash on disk. This structure
  2082. * followed ext4_dir_entry_2's name[name_len] at the next 4 byte aligned
  2083. * boundary.
  2084. */
  2085. struct ext4_dir_entry_hash {
  2086. __le32 hash;
  2087. __le32 minor_hash;
  2088. };
  2089. /*
  2090. * The new version of the directory entry. Since EXT4 structures are
  2091. * stored in intel byte order, and the name_len field could never be
  2092. * bigger than 255 chars, it's safe to reclaim the extra byte for the
  2093. * file_type field.
  2094. */
  2095. struct ext4_dir_entry_2 {
  2096. __le32 inode; /* Inode number */
  2097. __le16 rec_len; /* Directory entry length */
  2098. __u8 name_len; /* Name length */
  2099. __u8 file_type; /* See file type macros EXT4_FT_* below */
  2100. char name[EXT4_NAME_LEN]; /* File name */
  2101. };
  2102. /*
  2103. * Access the hashes at the end of ext4_dir_entry_2
  2104. */
  2105. #define EXT4_DIRENT_HASHES(entry) \
  2106. ((struct ext4_dir_entry_hash *) \
  2107. (((void *)(entry)) + \
  2108. ((8 + (entry)->name_len + EXT4_DIR_ROUND) & ~EXT4_DIR_ROUND)))
  2109. #define EXT4_DIRENT_HASH(entry) le32_to_cpu(EXT4_DIRENT_HASHES(entry)->hash)
  2110. #define EXT4_DIRENT_MINOR_HASH(entry) \
  2111. le32_to_cpu(EXT4_DIRENT_HASHES(entry)->minor_hash)
  2112. static inline bool ext4_hash_in_dirent(const struct inode *inode)
  2113. {
  2114. return IS_CASEFOLDED(inode) && IS_ENCRYPTED(inode);
  2115. }
  2116. /*
  2117. * This is a bogus directory entry at the end of each leaf block that
  2118. * records checksums.
  2119. */
  2120. struct ext4_dir_entry_tail {
  2121. __le32 det_reserved_zero1; /* Pretend to be unused */
  2122. __le16 det_rec_len; /* 12 */
  2123. __u8 det_reserved_zero2; /* Zero name length */
  2124. __u8 det_reserved_ft; /* 0xDE, fake file type */
  2125. __le32 det_checksum; /* crc32c(uuid+inum+dirblock) */
  2126. };
  2127. #define EXT4_DIRENT_TAIL(block, blocksize) \
  2128. ((struct ext4_dir_entry_tail *)(((void *)(block)) + \
  2129. ((blocksize) - \
  2130. sizeof(struct ext4_dir_entry_tail))))
  2131. /*
  2132. * Ext4 directory file types. Only the low 3 bits are used. The
  2133. * other bits are reserved for now.
  2134. */
  2135. #define EXT4_FT_UNKNOWN 0
  2136. #define EXT4_FT_REG_FILE 1
  2137. #define EXT4_FT_DIR 2
  2138. #define EXT4_FT_CHRDEV 3
  2139. #define EXT4_FT_BLKDEV 4
  2140. #define EXT4_FT_FIFO 5
  2141. #define EXT4_FT_SOCK 6
  2142. #define EXT4_FT_SYMLINK 7
  2143. #define EXT4_FT_MAX 8
  2144. #define EXT4_FT_DIR_CSUM 0xDE
  2145. /*
  2146. * EXT4_DIR_PAD defines the directory entries boundaries
  2147. *
  2148. * NOTE: It must be a multiple of 4
  2149. */
  2150. #define EXT4_DIR_PAD 4
  2151. #define EXT4_DIR_ROUND (EXT4_DIR_PAD - 1)
  2152. #define EXT4_MAX_REC_LEN ((1<<16)-1)
  2153. /*
  2154. * The rec_len is dependent on the type of directory. Directories that are
  2155. * casefolded and encrypted need to store the hash as well, so we add room for
  2156. * ext4_extended_dir_entry_2. For all entries related to '.' or '..' you should
  2157. * pass NULL for dir, as those entries do not use the extra fields.
  2158. */
  2159. static inline unsigned int ext4_dir_rec_len(__u8 name_len,
  2160. const struct inode *dir)
  2161. {
  2162. int rec_len = (name_len + 8 + EXT4_DIR_ROUND);
  2163. if (dir && ext4_hash_in_dirent(dir))
  2164. rec_len += sizeof(struct ext4_dir_entry_hash);
  2165. return (rec_len & ~EXT4_DIR_ROUND);
  2166. }
  2167. /*
  2168. * If we ever get support for fs block sizes > page_size, we'll need
  2169. * to remove the #if statements in the next two functions...
  2170. */
  2171. static inline unsigned int
  2172. ext4_rec_len_from_disk(__le16 dlen, unsigned blocksize)
  2173. {
  2174. unsigned len = le16_to_cpu(dlen);
  2175. #if (PAGE_SIZE >= 65536)
  2176. if (len == EXT4_MAX_REC_LEN || len == 0)
  2177. return blocksize;
  2178. return (len & 65532) | ((len & 3) << 16);
  2179. #else
  2180. return len;
  2181. #endif
  2182. }
  2183. static inline __le16 ext4_rec_len_to_disk(unsigned len, unsigned blocksize)
  2184. {
  2185. BUG_ON((len > blocksize) || (blocksize > (1 << 18)) || (len & 3));
  2186. #if (PAGE_SIZE >= 65536)
  2187. if (len < 65536)
  2188. return cpu_to_le16(len);
  2189. if (len == blocksize) {
  2190. if (blocksize == 65536)
  2191. return cpu_to_le16(EXT4_MAX_REC_LEN);
  2192. else
  2193. return cpu_to_le16(0);
  2194. }
  2195. return cpu_to_le16((len & 65532) | ((len >> 16) & 3));
  2196. #else
  2197. return cpu_to_le16(len);
  2198. #endif
  2199. }
  2200. /*
  2201. * Hash Tree Directory indexing
  2202. * (c) Daniel Phillips, 2001
  2203. */
  2204. #define is_dx(dir) (ext4_has_feature_dir_index((dir)->i_sb) && \
  2205. ext4_test_inode_flag((dir), EXT4_INODE_INDEX))
  2206. #define EXT4_DIR_LINK_MAX(dir) unlikely((dir)->i_nlink >= EXT4_LINK_MAX && \
  2207. !(ext4_has_feature_dir_nlink((dir)->i_sb) && is_dx(dir)))
  2208. #define EXT4_DIR_LINK_EMPTY(dir) ((dir)->i_nlink == 2 || (dir)->i_nlink == 1)
  2209. /* Legal values for the dx_root hash_version field: */
  2210. #define DX_HASH_LEGACY 0
  2211. #define DX_HASH_HALF_MD4 1
  2212. #define DX_HASH_TEA 2
  2213. #define DX_HASH_LEGACY_UNSIGNED 3
  2214. #define DX_HASH_HALF_MD4_UNSIGNED 4
  2215. #define DX_HASH_TEA_UNSIGNED 5
  2216. #define DX_HASH_SIPHASH 6
  2217. #define DX_HASH_LAST DX_HASH_SIPHASH
  2218. static inline u32 ext4_chksum(struct ext4_sb_info *sbi, u32 crc,
  2219. const void *address, unsigned int length)
  2220. {
  2221. struct {
  2222. struct shash_desc shash;
  2223. char ctx[4];
  2224. } desc;
  2225. BUG_ON(crypto_shash_descsize(sbi->s_chksum_driver)!=sizeof(desc.ctx));
  2226. desc.shash.tfm = sbi->s_chksum_driver;
  2227. *(u32 *)desc.ctx = crc;
  2228. BUG_ON(crypto_shash_update(&desc.shash, address, length));
  2229. return *(u32 *)desc.ctx;
  2230. }
  2231. #ifdef __KERNEL__
  2232. /* hash info structure used by the directory hash */
  2233. struct dx_hash_info
  2234. {
  2235. u32 hash;
  2236. u32 minor_hash;
  2237. int hash_version;
  2238. u32 *seed;
  2239. };
  2240. /* 32 and 64 bit signed EOF for dx directories */
  2241. #define EXT4_HTREE_EOF_32BIT ((1UL << (32 - 1)) - 1)
  2242. #define EXT4_HTREE_EOF_64BIT ((1ULL << (64 - 1)) - 1)
  2243. /*
  2244. * Control parameters used by ext4_htree_next_block
  2245. */
  2246. #define HASH_NB_ALWAYS 1
  2247. struct ext4_filename {
  2248. const struct qstr *usr_fname;
  2249. struct fscrypt_str disk_name;
  2250. struct dx_hash_info hinfo;
  2251. #ifdef CONFIG_FS_ENCRYPTION
  2252. struct fscrypt_str crypto_buf;
  2253. #endif
  2254. #if IS_ENABLED(CONFIG_UNICODE)
  2255. struct qstr cf_name;
  2256. #endif
  2257. };
  2258. #define fname_name(p) ((p)->disk_name.name)
  2259. #define fname_usr_name(p) ((p)->usr_fname->name)
  2260. #define fname_len(p) ((p)->disk_name.len)
  2261. /*
  2262. * Describe an inode's exact location on disk and in memory
  2263. */
  2264. struct ext4_iloc
  2265. {
  2266. struct buffer_head *bh;
  2267. unsigned long offset;
  2268. ext4_group_t block_group;
  2269. };
  2270. static inline struct ext4_inode *ext4_raw_inode(struct ext4_iloc *iloc)
  2271. {
  2272. return (struct ext4_inode *) (iloc->bh->b_data + iloc->offset);
  2273. }
  2274. static inline bool ext4_is_quota_file(struct inode *inode)
  2275. {
  2276. return IS_NOQUOTA(inode) &&
  2277. !(EXT4_I(inode)->i_flags & EXT4_EA_INODE_FL);
  2278. }
  2279. /*
  2280. * This structure is stuffed into the struct file's private_data field
  2281. * for directories. It is where we put information so that we can do
  2282. * readdir operations in hash tree order.
  2283. */
  2284. struct dir_private_info {
  2285. struct rb_root root;
  2286. struct rb_node *curr_node;
  2287. struct fname *extra_fname;
  2288. loff_t last_pos;
  2289. __u32 curr_hash;
  2290. __u32 curr_minor_hash;
  2291. __u32 next_hash;
  2292. u64 cookie;
  2293. bool initialized;
  2294. };
  2295. /* calculate the first block number of the group */
  2296. static inline ext4_fsblk_t
  2297. ext4_group_first_block_no(struct super_block *sb, ext4_group_t group_no)
  2298. {
  2299. return group_no * (ext4_fsblk_t)EXT4_BLOCKS_PER_GROUP(sb) +
  2300. le32_to_cpu(EXT4_SB(sb)->s_es->s_first_data_block);
  2301. }
  2302. /*
  2303. * Special error return code only used by dx_probe() and its callers.
  2304. */
  2305. #define ERR_BAD_DX_DIR (-(MAX_ERRNO - 1))
  2306. /* htree levels for ext4 */
  2307. #define EXT4_HTREE_LEVEL_COMPAT 2
  2308. #define EXT4_HTREE_LEVEL 3
  2309. static inline int ext4_dir_htree_level(struct super_block *sb)
  2310. {
  2311. return ext4_has_feature_largedir(sb) ?
  2312. EXT4_HTREE_LEVEL : EXT4_HTREE_LEVEL_COMPAT;
  2313. }
  2314. /*
  2315. * Timeout and state flag for lazy initialization inode thread.
  2316. */
  2317. #define EXT4_DEF_LI_WAIT_MULT 10
  2318. #define EXT4_DEF_LI_MAX_START_DELAY 5
  2319. #define EXT4_LAZYINIT_QUIT 0x0001
  2320. #define EXT4_LAZYINIT_RUNNING 0x0002
  2321. /*
  2322. * Lazy inode table initialization info
  2323. */
  2324. struct ext4_lazy_init {
  2325. unsigned long li_state;
  2326. struct list_head li_request_list;
  2327. struct mutex li_list_mtx;
  2328. };
  2329. enum ext4_li_mode {
  2330. EXT4_LI_MODE_PREFETCH_BBITMAP,
  2331. EXT4_LI_MODE_ITABLE,
  2332. };
  2333. struct ext4_li_request {
  2334. struct super_block *lr_super;
  2335. enum ext4_li_mode lr_mode;
  2336. ext4_group_t lr_first_not_zeroed;
  2337. ext4_group_t lr_next_group;
  2338. struct list_head lr_request;
  2339. unsigned long lr_next_sched;
  2340. unsigned long lr_timeout;
  2341. };
  2342. struct ext4_features {
  2343. struct kobject f_kobj;
  2344. struct completion f_kobj_unregister;
  2345. };
  2346. /*
  2347. * This structure will be used for multiple mount protection. It will be
  2348. * written into the block number saved in the s_mmp_block field in the
  2349. * superblock. Programs that check MMP should assume that if
  2350. * SEQ_FSCK (or any unknown code above SEQ_MAX) is present then it is NOT safe
  2351. * to use the filesystem, regardless of how old the timestamp is.
  2352. */
  2353. #define EXT4_MMP_MAGIC 0x004D4D50U /* ASCII for MMP */
  2354. #define EXT4_MMP_SEQ_CLEAN 0xFF4D4D50U /* mmp_seq value for clean unmount */
  2355. #define EXT4_MMP_SEQ_FSCK 0xE24D4D50U /* mmp_seq value when being fscked */
  2356. #define EXT4_MMP_SEQ_MAX 0xE24D4D4FU /* maximum valid mmp_seq value */
  2357. struct mmp_struct {
  2358. __le32 mmp_magic; /* Magic number for MMP */
  2359. __le32 mmp_seq; /* Sequence no. updated periodically */
  2360. /*
  2361. * mmp_time, mmp_nodename & mmp_bdevname are only used for information
  2362. * purposes and do not affect the correctness of the algorithm
  2363. */
  2364. __le64 mmp_time; /* Time last updated */
  2365. char mmp_nodename[64]; /* Node which last updated MMP block */
  2366. char mmp_bdevname[32]; /* Bdev which last updated MMP block */
  2367. /*
  2368. * mmp_check_interval is used to verify if the MMP block has been
  2369. * updated on the block device. The value is updated based on the
  2370. * maximum time to write the MMP block during an update cycle.
  2371. */
  2372. __le16 mmp_check_interval;
  2373. __le16 mmp_pad1;
  2374. __le32 mmp_pad2[226];
  2375. __le32 mmp_checksum; /* crc32c(uuid+mmp_block) */
  2376. };
  2377. /* arguments passed to the mmp thread */
  2378. struct mmpd_data {
  2379. struct buffer_head *bh; /* bh from initial read_mmp_block() */
  2380. struct super_block *sb; /* super block of the fs */
  2381. };
  2382. /*
  2383. * Check interval multiplier
  2384. * The MMP block is written every update interval and initially checked every
  2385. * update interval x the multiplier (the value is then adapted based on the
  2386. * write latency). The reason is that writes can be delayed under load and we
  2387. * don't want readers to incorrectly assume that the filesystem is no longer
  2388. * in use.
  2389. */
  2390. #define EXT4_MMP_CHECK_MULT 2UL
  2391. /*
  2392. * Minimum interval for MMP checking in seconds.
  2393. */
  2394. #define EXT4_MMP_MIN_CHECK_INTERVAL 5UL
  2395. /*
  2396. * Maximum interval for MMP checking in seconds.
  2397. */
  2398. #define EXT4_MMP_MAX_CHECK_INTERVAL 300UL
  2399. /*
  2400. * Function prototypes
  2401. */
  2402. /*
  2403. * Ok, these declarations are also in <linux/kernel.h> but none of the
  2404. * ext4 source programs needs to include it so they are duplicated here.
  2405. */
  2406. # define NORET_TYPE /**/
  2407. # define ATTRIB_NORET __attribute__((noreturn))
  2408. # define NORET_AND noreturn,
  2409. /* bitmap.c */
  2410. extern unsigned int ext4_count_free(char *bitmap, unsigned numchars);
  2411. void ext4_inode_bitmap_csum_set(struct super_block *sb,
  2412. struct ext4_group_desc *gdp,
  2413. struct buffer_head *bh);
  2414. int ext4_inode_bitmap_csum_verify(struct super_block *sb,
  2415. struct ext4_group_desc *gdp,
  2416. struct buffer_head *bh);
  2417. void ext4_block_bitmap_csum_set(struct super_block *sb,
  2418. struct ext4_group_desc *gdp,
  2419. struct buffer_head *bh);
  2420. int ext4_block_bitmap_csum_verify(struct super_block *sb,
  2421. struct ext4_group_desc *gdp,
  2422. struct buffer_head *bh);
  2423. /* balloc.c */
  2424. extern void ext4_get_group_no_and_offset(struct super_block *sb,
  2425. ext4_fsblk_t blocknr,
  2426. ext4_group_t *blockgrpp,
  2427. ext4_grpblk_t *offsetp);
  2428. extern ext4_group_t ext4_get_group_number(struct super_block *sb,
  2429. ext4_fsblk_t block);
  2430. extern int ext4_bg_has_super(struct super_block *sb, ext4_group_t group);
  2431. extern unsigned long ext4_bg_num_gdb(struct super_block *sb,
  2432. ext4_group_t group);
  2433. extern ext4_fsblk_t ext4_new_meta_blocks(handle_t *handle, struct inode *inode,
  2434. ext4_fsblk_t goal,
  2435. unsigned int flags,
  2436. unsigned long *count,
  2437. int *errp);
  2438. extern int ext4_claim_free_clusters(struct ext4_sb_info *sbi,
  2439. s64 nclusters, unsigned int flags);
  2440. extern ext4_fsblk_t ext4_count_free_clusters(struct super_block *);
  2441. extern struct ext4_group_desc * ext4_get_group_desc(struct super_block * sb,
  2442. ext4_group_t block_group,
  2443. struct buffer_head ** bh);
  2444. extern struct ext4_group_info *ext4_get_group_info(struct super_block *sb,
  2445. ext4_group_t group);
  2446. extern int ext4_should_retry_alloc(struct super_block *sb, int *retries);
  2447. extern struct buffer_head *ext4_read_block_bitmap_nowait(struct super_block *sb,
  2448. ext4_group_t block_group,
  2449. bool ignore_locked);
  2450. extern int ext4_wait_block_bitmap(struct super_block *sb,
  2451. ext4_group_t block_group,
  2452. struct buffer_head *bh);
  2453. extern struct buffer_head *ext4_read_block_bitmap(struct super_block *sb,
  2454. ext4_group_t block_group);
  2455. extern unsigned ext4_free_clusters_after_init(struct super_block *sb,
  2456. ext4_group_t block_group,
  2457. struct ext4_group_desc *gdp);
  2458. ext4_fsblk_t ext4_inode_to_goal_block(struct inode *);
  2459. #if IS_ENABLED(CONFIG_UNICODE)
  2460. extern int ext4_fname_setup_ci_filename(struct inode *dir,
  2461. const struct qstr *iname,
  2462. struct ext4_filename *fname);
  2463. static inline void ext4_fname_free_ci_filename(struct ext4_filename *fname)
  2464. {
  2465. kfree(fname->cf_name.name);
  2466. fname->cf_name.name = NULL;
  2467. }
  2468. #else
  2469. static inline int ext4_fname_setup_ci_filename(struct inode *dir,
  2470. const struct qstr *iname,
  2471. struct ext4_filename *fname)
  2472. {
  2473. return 0;
  2474. }
  2475. static inline void ext4_fname_free_ci_filename(struct ext4_filename *fname)
  2476. {
  2477. }
  2478. #endif
  2479. /* ext4 encryption related stuff goes here crypto.c */
  2480. #ifdef CONFIG_FS_ENCRYPTION
  2481. extern const struct fscrypt_operations ext4_cryptops;
  2482. int ext4_fname_setup_filename(struct inode *dir, const struct qstr *iname,
  2483. int lookup, struct ext4_filename *fname);
  2484. int ext4_fname_prepare_lookup(struct inode *dir, struct dentry *dentry,
  2485. struct ext4_filename *fname);
  2486. void ext4_fname_free_filename(struct ext4_filename *fname);
  2487. int ext4_ioctl_get_encryption_pwsalt(struct file *filp, void __user *arg);
  2488. #else /* !CONFIG_FS_ENCRYPTION */
  2489. static inline int ext4_fname_setup_filename(struct inode *dir,
  2490. const struct qstr *iname,
  2491. int lookup,
  2492. struct ext4_filename *fname)
  2493. {
  2494. fname->usr_fname = iname;
  2495. fname->disk_name.name = (unsigned char *) iname->name;
  2496. fname->disk_name.len = iname->len;
  2497. return ext4_fname_setup_ci_filename(dir, iname, fname);
  2498. }
  2499. static inline int ext4_fname_prepare_lookup(struct inode *dir,
  2500. struct dentry *dentry,
  2501. struct ext4_filename *fname)
  2502. {
  2503. return ext4_fname_setup_filename(dir, &dentry->d_name, 1, fname);
  2504. }
  2505. static inline void ext4_fname_free_filename(struct ext4_filename *fname)
  2506. {
  2507. ext4_fname_free_ci_filename(fname);
  2508. }
  2509. static inline int ext4_ioctl_get_encryption_pwsalt(struct file *filp,
  2510. void __user *arg)
  2511. {
  2512. return -EOPNOTSUPP;
  2513. }
  2514. #endif /* !CONFIG_FS_ENCRYPTION */
  2515. /* dir.c */
  2516. extern int __ext4_check_dir_entry(const char *, unsigned int, struct inode *,
  2517. struct file *,
  2518. struct ext4_dir_entry_2 *,
  2519. struct buffer_head *, char *, int,
  2520. unsigned int);
  2521. #define ext4_check_dir_entry(dir, filp, de, bh, buf, size, offset) \
  2522. unlikely(__ext4_check_dir_entry(__func__, __LINE__, (dir), (filp), \
  2523. (de), (bh), (buf), (size), (offset)))
  2524. extern int ext4_htree_store_dirent(struct file *dir_file, __u32 hash,
  2525. __u32 minor_hash,
  2526. struct ext4_dir_entry_2 *dirent,
  2527. struct fscrypt_str *ent_name);
  2528. extern void ext4_htree_free_dir_info(struct dir_private_info *p);
  2529. extern int ext4_find_dest_de(struct inode *dir, struct inode *inode,
  2530. struct buffer_head *bh,
  2531. void *buf, int buf_size,
  2532. struct ext4_filename *fname,
  2533. struct ext4_dir_entry_2 **dest_de);
  2534. void ext4_insert_dentry(struct inode *dir, struct inode *inode,
  2535. struct ext4_dir_entry_2 *de,
  2536. int buf_size,
  2537. struct ext4_filename *fname);
  2538. static inline void ext4_update_dx_flag(struct inode *inode)
  2539. {
  2540. if (!ext4_has_feature_dir_index(inode->i_sb) &&
  2541. ext4_test_inode_flag(inode, EXT4_INODE_INDEX)) {
  2542. /* ext4_iget() should have caught this... */
  2543. WARN_ON_ONCE(ext4_has_feature_metadata_csum(inode->i_sb));
  2544. ext4_clear_inode_flag(inode, EXT4_INODE_INDEX);
  2545. }
  2546. }
  2547. static const unsigned char ext4_filetype_table[] = {
  2548. DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
  2549. };
  2550. static inline unsigned char get_dtype(struct super_block *sb, int filetype)
  2551. {
  2552. if (!ext4_has_feature_filetype(sb) || filetype >= EXT4_FT_MAX)
  2553. return DT_UNKNOWN;
  2554. return ext4_filetype_table[filetype];
  2555. }
  2556. extern int ext4_check_all_de(struct inode *dir, struct buffer_head *bh,
  2557. void *buf, int buf_size);
  2558. /* fsync.c */
  2559. extern int ext4_sync_file(struct file *, loff_t, loff_t, int);
  2560. /* hash.c */
  2561. extern int ext4fs_dirhash(const struct inode *dir, const char *name, int len,
  2562. struct dx_hash_info *hinfo);
  2563. /* ialloc.c */
  2564. extern int ext4_mark_inode_used(struct super_block *sb, int ino);
  2565. extern struct inode *__ext4_new_inode(struct mnt_idmap *, handle_t *,
  2566. struct inode *, umode_t,
  2567. const struct qstr *qstr, __u32 goal,
  2568. uid_t *owner, __u32 i_flags,
  2569. int handle_type, unsigned int line_no,
  2570. int nblocks);
  2571. #define ext4_new_inode(handle, dir, mode, qstr, goal, owner, i_flags) \
  2572. __ext4_new_inode(&nop_mnt_idmap, (handle), (dir), (mode), (qstr), \
  2573. (goal), (owner), i_flags, 0, 0, 0)
  2574. #define ext4_new_inode_start_handle(idmap, dir, mode, qstr, goal, owner, \
  2575. type, nblocks) \
  2576. __ext4_new_inode((idmap), NULL, (dir), (mode), (qstr), (goal), (owner), \
  2577. 0, (type), __LINE__, (nblocks))
  2578. extern void ext4_free_inode(handle_t *, struct inode *);
  2579. extern struct inode * ext4_orphan_get(struct super_block *, unsigned long);
  2580. extern unsigned long ext4_count_free_inodes(struct super_block *);
  2581. extern unsigned long ext4_count_dirs(struct super_block *);
  2582. extern void ext4_mark_bitmap_end(int start_bit, int end_bit, char *bitmap);
  2583. extern int ext4_init_inode_table(struct super_block *sb,
  2584. ext4_group_t group, int barrier);
  2585. extern void ext4_end_bitmap_read(struct buffer_head *bh, int uptodate);
  2586. /* fast_commit.c */
  2587. int ext4_fc_info_show(struct seq_file *seq, void *v);
  2588. void ext4_fc_init(struct super_block *sb, journal_t *journal);
  2589. void ext4_fc_init_inode(struct inode *inode);
  2590. void ext4_fc_track_range(handle_t *handle, struct inode *inode, ext4_lblk_t start,
  2591. ext4_lblk_t end);
  2592. void __ext4_fc_track_unlink(handle_t *handle, struct inode *inode,
  2593. struct dentry *dentry);
  2594. void __ext4_fc_track_link(handle_t *handle, struct inode *inode,
  2595. struct dentry *dentry);
  2596. void ext4_fc_track_unlink(handle_t *handle, struct dentry *dentry);
  2597. void ext4_fc_track_link(handle_t *handle, struct dentry *dentry);
  2598. void __ext4_fc_track_create(handle_t *handle, struct inode *inode,
  2599. struct dentry *dentry);
  2600. void ext4_fc_track_create(handle_t *handle, struct dentry *dentry);
  2601. void ext4_fc_track_inode(handle_t *handle, struct inode *inode);
  2602. void ext4_fc_mark_ineligible(struct super_block *sb, int reason, handle_t *handle);
  2603. void ext4_fc_start_update(struct inode *inode);
  2604. void ext4_fc_stop_update(struct inode *inode);
  2605. void ext4_fc_del(struct inode *inode);
  2606. bool ext4_fc_replay_check_excluded(struct super_block *sb, ext4_fsblk_t block);
  2607. void ext4_fc_replay_cleanup(struct super_block *sb);
  2608. int ext4_fc_commit(journal_t *journal, tid_t commit_tid);
  2609. int __init ext4_fc_init_dentry_cache(void);
  2610. void ext4_fc_destroy_dentry_cache(void);
  2611. int ext4_fc_record_regions(struct super_block *sb, int ino,
  2612. ext4_lblk_t lblk, ext4_fsblk_t pblk,
  2613. int len, int replay);
  2614. /* mballoc.c */
  2615. extern const struct seq_operations ext4_mb_seq_groups_ops;
  2616. extern const struct seq_operations ext4_mb_seq_structs_summary_ops;
  2617. extern int ext4_seq_mb_stats_show(struct seq_file *seq, void *offset);
  2618. extern int ext4_mb_init(struct super_block *);
  2619. extern void ext4_mb_release(struct super_block *);
  2620. extern ext4_fsblk_t ext4_mb_new_blocks(handle_t *,
  2621. struct ext4_allocation_request *, int *);
  2622. extern void ext4_discard_preallocations(struct inode *);
  2623. extern int __init ext4_init_mballoc(void);
  2624. extern void ext4_exit_mballoc(void);
  2625. extern ext4_group_t ext4_mb_prefetch(struct super_block *sb,
  2626. ext4_group_t group,
  2627. unsigned int nr, int *cnt);
  2628. extern void ext4_mb_prefetch_fini(struct super_block *sb, ext4_group_t group,
  2629. unsigned int nr);
  2630. extern void ext4_free_blocks(handle_t *handle, struct inode *inode,
  2631. struct buffer_head *bh, ext4_fsblk_t block,
  2632. unsigned long count, int flags);
  2633. extern int ext4_mb_alloc_groupinfo(struct super_block *sb,
  2634. ext4_group_t ngroups);
  2635. extern int ext4_mb_add_groupinfo(struct super_block *sb,
  2636. ext4_group_t i, struct ext4_group_desc *desc);
  2637. extern int ext4_group_add_blocks(handle_t *handle, struct super_block *sb,
  2638. ext4_fsblk_t block, unsigned long count);
  2639. extern int ext4_trim_fs(struct super_block *, struct fstrim_range *);
  2640. extern void ext4_process_freed_data(struct super_block *sb, tid_t commit_tid);
  2641. extern void ext4_mb_mark_bb(struct super_block *sb, ext4_fsblk_t block,
  2642. int len, bool state);
  2643. static inline bool ext4_mb_cr_expensive(enum criteria cr)
  2644. {
  2645. return cr >= CR_GOAL_LEN_SLOW;
  2646. }
  2647. /* inode.c */
  2648. void ext4_inode_csum_set(struct inode *inode, struct ext4_inode *raw,
  2649. struct ext4_inode_info *ei);
  2650. int ext4_inode_is_fast_symlink(struct inode *inode);
  2651. struct buffer_head *ext4_getblk(handle_t *, struct inode *, ext4_lblk_t, int);
  2652. struct buffer_head *ext4_bread(handle_t *, struct inode *, ext4_lblk_t, int);
  2653. int ext4_bread_batch(struct inode *inode, ext4_lblk_t block, int bh_count,
  2654. bool wait, struct buffer_head **bhs);
  2655. int ext4_get_block_unwritten(struct inode *inode, sector_t iblock,
  2656. struct buffer_head *bh_result, int create);
  2657. int ext4_get_block(struct inode *inode, sector_t iblock,
  2658. struct buffer_head *bh_result, int create);
  2659. int ext4_da_get_block_prep(struct inode *inode, sector_t iblock,
  2660. struct buffer_head *bh, int create);
  2661. int ext4_walk_page_buffers(handle_t *handle,
  2662. struct inode *inode,
  2663. struct buffer_head *head,
  2664. unsigned from,
  2665. unsigned to,
  2666. int *partial,
  2667. int (*fn)(handle_t *handle, struct inode *inode,
  2668. struct buffer_head *bh));
  2669. int do_journal_get_write_access(handle_t *handle, struct inode *inode,
  2670. struct buffer_head *bh);
  2671. #define FALL_BACK_TO_NONDELALLOC 1
  2672. #define CONVERT_INLINE_DATA 2
  2673. typedef enum {
  2674. EXT4_IGET_NORMAL = 0,
  2675. EXT4_IGET_SPECIAL = 0x0001, /* OK to iget a system inode */
  2676. EXT4_IGET_HANDLE = 0x0002, /* Inode # is from a handle */
  2677. EXT4_IGET_BAD = 0x0004, /* Allow to iget a bad inode */
  2678. EXT4_IGET_EA_INODE = 0x0008 /* Inode should contain an EA value */
  2679. } ext4_iget_flags;
  2680. extern struct inode *__ext4_iget(struct super_block *sb, unsigned long ino,
  2681. ext4_iget_flags flags, const char *function,
  2682. unsigned int line);
  2683. #define ext4_iget(sb, ino, flags) \
  2684. __ext4_iget((sb), (ino), (flags), __func__, __LINE__)
  2685. extern int ext4_write_inode(struct inode *, struct writeback_control *);
  2686. extern int ext4_setattr(struct mnt_idmap *, struct dentry *,
  2687. struct iattr *);
  2688. extern u32 ext4_dio_alignment(struct inode *inode);
  2689. extern int ext4_getattr(struct mnt_idmap *, const struct path *,
  2690. struct kstat *, u32, unsigned int);
  2691. extern void ext4_evict_inode(struct inode *);
  2692. extern void ext4_clear_inode(struct inode *);
  2693. extern int ext4_file_getattr(struct mnt_idmap *, const struct path *,
  2694. struct kstat *, u32, unsigned int);
  2695. extern void ext4_dirty_inode(struct inode *, int);
  2696. extern int ext4_change_inode_journal_flag(struct inode *, int);
  2697. extern int ext4_get_inode_loc(struct inode *, struct ext4_iloc *);
  2698. extern int ext4_get_fc_inode_loc(struct super_block *sb, unsigned long ino,
  2699. struct ext4_iloc *iloc);
  2700. extern int ext4_inode_attach_jinode(struct inode *inode);
  2701. extern int ext4_can_truncate(struct inode *inode);
  2702. extern int ext4_truncate(struct inode *);
  2703. extern int ext4_break_layouts(struct inode *);
  2704. extern int ext4_truncate_page_cache_block_range(struct inode *inode,
  2705. loff_t start, loff_t end);
  2706. extern int ext4_punch_hole(struct file *file, loff_t offset, loff_t length);
  2707. extern void ext4_set_inode_flags(struct inode *, bool init);
  2708. extern int ext4_alloc_da_blocks(struct inode *inode);
  2709. extern void ext4_set_aops(struct inode *inode);
  2710. extern int ext4_writepage_trans_blocks(struct inode *);
  2711. extern int ext4_normal_submit_inode_data_buffers(struct jbd2_inode *jinode);
  2712. extern int ext4_chunk_trans_blocks(struct inode *, int nrblocks);
  2713. extern int ext4_zero_partial_blocks(handle_t *handle, struct inode *inode,
  2714. loff_t lstart, loff_t lend);
  2715. extern vm_fault_t ext4_page_mkwrite(struct vm_fault *vmf);
  2716. extern qsize_t *ext4_get_reserved_space(struct inode *inode);
  2717. extern int ext4_get_projid(struct inode *inode, kprojid_t *projid);
  2718. extern void ext4_da_release_space(struct inode *inode, int to_free);
  2719. extern void ext4_da_update_reserve_space(struct inode *inode,
  2720. int used, int quota_claim);
  2721. extern int ext4_issue_zeroout(struct inode *inode, ext4_lblk_t lblk,
  2722. ext4_fsblk_t pblk, ext4_lblk_t len);
  2723. /* indirect.c */
  2724. extern int ext4_ind_map_blocks(handle_t *handle, struct inode *inode,
  2725. struct ext4_map_blocks *map, int flags);
  2726. extern int ext4_ind_trans_blocks(struct inode *inode, int nrblocks);
  2727. extern void ext4_ind_truncate(handle_t *, struct inode *inode);
  2728. extern int ext4_ind_remove_space(handle_t *handle, struct inode *inode,
  2729. ext4_lblk_t start, ext4_lblk_t end);
  2730. /* ioctl.c */
  2731. extern long ext4_ioctl(struct file *, unsigned int, unsigned long);
  2732. extern long ext4_compat_ioctl(struct file *, unsigned int, unsigned long);
  2733. int ext4_fileattr_set(struct mnt_idmap *idmap,
  2734. struct dentry *dentry, struct fileattr *fa);
  2735. int ext4_fileattr_get(struct dentry *dentry, struct fileattr *fa);
  2736. extern void ext4_reset_inode_seed(struct inode *inode);
  2737. int ext4_update_overhead(struct super_block *sb, bool force);
  2738. int ext4_force_shutdown(struct super_block *sb, u32 flags);
  2739. /* migrate.c */
  2740. extern int ext4_ext_migrate(struct inode *);
  2741. extern int ext4_ind_migrate(struct inode *inode);
  2742. /* namei.c */
  2743. extern int ext4_init_new_dir(handle_t *handle, struct inode *dir,
  2744. struct inode *inode);
  2745. extern int ext4_dirblock_csum_verify(struct inode *inode,
  2746. struct buffer_head *bh);
  2747. extern int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash,
  2748. __u32 start_minor_hash, __u32 *next_hash);
  2749. extern int ext4_search_dir(struct buffer_head *bh,
  2750. char *search_buf,
  2751. int buf_size,
  2752. struct inode *dir,
  2753. struct ext4_filename *fname,
  2754. unsigned int offset,
  2755. struct ext4_dir_entry_2 **res_dir);
  2756. extern int ext4_generic_delete_entry(struct inode *dir,
  2757. struct ext4_dir_entry_2 *de_del,
  2758. struct buffer_head *bh,
  2759. void *entry_buf,
  2760. int buf_size,
  2761. int csum_size);
  2762. extern bool ext4_empty_dir(struct inode *inode);
  2763. /* resize.c */
  2764. extern void ext4_kvfree_array_rcu(void *to_free);
  2765. extern int ext4_group_add(struct super_block *sb,
  2766. struct ext4_new_group_data *input);
  2767. extern int ext4_group_extend(struct super_block *sb,
  2768. struct ext4_super_block *es,
  2769. ext4_fsblk_t n_blocks_count);
  2770. extern int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count);
  2771. extern unsigned int ext4_list_backups(struct super_block *sb,
  2772. unsigned int *three, unsigned int *five,
  2773. unsigned int *seven);
  2774. /* super.c */
  2775. extern struct buffer_head *ext4_sb_bread(struct super_block *sb,
  2776. sector_t block, blk_opf_t op_flags);
  2777. extern struct buffer_head *ext4_sb_bread_unmovable(struct super_block *sb,
  2778. sector_t block);
  2779. extern struct buffer_head *ext4_sb_bread_nofail(struct super_block *sb,
  2780. sector_t block);
  2781. extern void ext4_read_bh_nowait(struct buffer_head *bh, blk_opf_t op_flags,
  2782. bh_end_io_t *end_io, bool simu_fail);
  2783. extern int ext4_read_bh(struct buffer_head *bh, blk_opf_t op_flags,
  2784. bh_end_io_t *end_io, bool simu_fail);
  2785. extern int ext4_read_bh_lock(struct buffer_head *bh, blk_opf_t op_flags, bool wait);
  2786. extern void ext4_sb_breadahead_unmovable(struct super_block *sb, sector_t block);
  2787. extern int ext4_seq_options_show(struct seq_file *seq, void *offset);
  2788. extern int ext4_calculate_overhead(struct super_block *sb);
  2789. extern __le32 ext4_superblock_csum(struct super_block *sb,
  2790. struct ext4_super_block *es);
  2791. extern void ext4_superblock_csum_set(struct super_block *sb);
  2792. extern int ext4_alloc_flex_bg_array(struct super_block *sb,
  2793. ext4_group_t ngroup);
  2794. extern const char *ext4_decode_error(struct super_block *sb, int errno,
  2795. char nbuf[16]);
  2796. extern void ext4_mark_group_bitmap_corrupted(struct super_block *sb,
  2797. ext4_group_t block_group,
  2798. unsigned int flags);
  2799. extern unsigned int ext4_num_base_meta_blocks(struct super_block *sb,
  2800. ext4_group_t block_group);
  2801. extern __printf(7, 8)
  2802. void __ext4_error(struct super_block *, const char *, unsigned int, bool,
  2803. int, __u64, const char *, ...);
  2804. extern __printf(6, 7)
  2805. void __ext4_error_inode(struct inode *, const char *, unsigned int,
  2806. ext4_fsblk_t, int, const char *, ...);
  2807. extern __printf(5, 6)
  2808. void __ext4_error_file(struct file *, const char *, unsigned int, ext4_fsblk_t,
  2809. const char *, ...);
  2810. extern void __ext4_std_error(struct super_block *, const char *,
  2811. unsigned int, int);
  2812. extern __printf(4, 5)
  2813. void __ext4_warning(struct super_block *, const char *, unsigned int,
  2814. const char *, ...);
  2815. extern __printf(4, 5)
  2816. void __ext4_warning_inode(const struct inode *inode, const char *function,
  2817. unsigned int line, const char *fmt, ...);
  2818. extern __printf(3, 4)
  2819. void __ext4_msg(struct super_block *, const char *, const char *, ...);
  2820. extern void __dump_mmp_msg(struct super_block *, struct mmp_struct *mmp,
  2821. const char *, unsigned int, const char *);
  2822. extern __printf(7, 8)
  2823. void __ext4_grp_locked_error(const char *, unsigned int,
  2824. struct super_block *, ext4_group_t,
  2825. unsigned long, ext4_fsblk_t,
  2826. const char *, ...);
  2827. #define EXT4_ERROR_INODE(inode, fmt, a...) \
  2828. ext4_error_inode((inode), __func__, __LINE__, 0, (fmt), ## a)
  2829. #define EXT4_ERROR_INODE_ERR(inode, err, fmt, a...) \
  2830. __ext4_error_inode((inode), __func__, __LINE__, 0, (err), (fmt), ## a)
  2831. #define ext4_error_inode_block(inode, block, err, fmt, a...) \
  2832. __ext4_error_inode((inode), __func__, __LINE__, (block), (err), \
  2833. (fmt), ## a)
  2834. #define EXT4_ERROR_FILE(file, block, fmt, a...) \
  2835. ext4_error_file((file), __func__, __LINE__, (block), (fmt), ## a)
  2836. #define ext4_abort(sb, err, fmt, a...) \
  2837. __ext4_error((sb), __func__, __LINE__, true, (err), 0, (fmt), ## a)
  2838. #ifdef CONFIG_PRINTK
  2839. #define ext4_error_inode(inode, func, line, block, fmt, ...) \
  2840. __ext4_error_inode(inode, func, line, block, 0, fmt, ##__VA_ARGS__)
  2841. #define ext4_error_inode_err(inode, func, line, block, err, fmt, ...) \
  2842. __ext4_error_inode((inode), (func), (line), (block), \
  2843. (err), (fmt), ##__VA_ARGS__)
  2844. #define ext4_error_file(file, func, line, block, fmt, ...) \
  2845. __ext4_error_file(file, func, line, block, fmt, ##__VA_ARGS__)
  2846. #define ext4_error(sb, fmt, ...) \
  2847. __ext4_error((sb), __func__, __LINE__, false, 0, 0, (fmt), \
  2848. ##__VA_ARGS__)
  2849. #define ext4_error_err(sb, err, fmt, ...) \
  2850. __ext4_error((sb), __func__, __LINE__, false, (err), 0, (fmt), \
  2851. ##__VA_ARGS__)
  2852. #define ext4_warning(sb, fmt, ...) \
  2853. __ext4_warning(sb, __func__, __LINE__, fmt, ##__VA_ARGS__)
  2854. #define ext4_warning_inode(inode, fmt, ...) \
  2855. __ext4_warning_inode(inode, __func__, __LINE__, fmt, ##__VA_ARGS__)
  2856. #define ext4_msg(sb, level, fmt, ...) \
  2857. __ext4_msg(sb, level, fmt, ##__VA_ARGS__)
  2858. #define dump_mmp_msg(sb, mmp, msg) \
  2859. __dump_mmp_msg(sb, mmp, __func__, __LINE__, msg)
  2860. #define ext4_grp_locked_error(sb, grp, ino, block, fmt, ...) \
  2861. __ext4_grp_locked_error(__func__, __LINE__, sb, grp, ino, block, \
  2862. fmt, ##__VA_ARGS__)
  2863. #else
  2864. #define ext4_error_inode(inode, func, line, block, fmt, ...) \
  2865. do { \
  2866. no_printk(fmt, ##__VA_ARGS__); \
  2867. __ext4_error_inode(inode, "", 0, block, 0, " "); \
  2868. } while (0)
  2869. #define ext4_error_inode_err(inode, func, line, block, err, fmt, ...) \
  2870. do { \
  2871. no_printk(fmt, ##__VA_ARGS__); \
  2872. __ext4_error_inode(inode, "", 0, block, err, " "); \
  2873. } while (0)
  2874. #define ext4_error_file(file, func, line, block, fmt, ...) \
  2875. do { \
  2876. no_printk(fmt, ##__VA_ARGS__); \
  2877. __ext4_error_file(file, "", 0, block, " "); \
  2878. } while (0)
  2879. #define ext4_error(sb, fmt, ...) \
  2880. do { \
  2881. no_printk(fmt, ##__VA_ARGS__); \
  2882. __ext4_error(sb, "", 0, false, 0, 0, " "); \
  2883. } while (0)
  2884. #define ext4_error_err(sb, err, fmt, ...) \
  2885. do { \
  2886. no_printk(fmt, ##__VA_ARGS__); \
  2887. __ext4_error(sb, "", 0, false, err, 0, " "); \
  2888. } while (0)
  2889. #define ext4_warning(sb, fmt, ...) \
  2890. do { \
  2891. no_printk(fmt, ##__VA_ARGS__); \
  2892. __ext4_warning(sb, "", 0, " "); \
  2893. } while (0)
  2894. #define ext4_warning_inode(inode, fmt, ...) \
  2895. do { \
  2896. no_printk(fmt, ##__VA_ARGS__); \
  2897. __ext4_warning_inode(inode, "", 0, " "); \
  2898. } while (0)
  2899. #define ext4_msg(sb, level, fmt, ...) \
  2900. do { \
  2901. no_printk(fmt, ##__VA_ARGS__); \
  2902. __ext4_msg(sb, "", " "); \
  2903. } while (0)
  2904. #define dump_mmp_msg(sb, mmp, msg) \
  2905. __dump_mmp_msg(sb, mmp, "", 0, "")
  2906. #define ext4_grp_locked_error(sb, grp, ino, block, fmt, ...) \
  2907. do { \
  2908. no_printk(fmt, ##__VA_ARGS__); \
  2909. __ext4_grp_locked_error("", 0, sb, grp, ino, block, " "); \
  2910. } while (0)
  2911. #endif
  2912. extern ext4_fsblk_t ext4_block_bitmap(struct super_block *sb,
  2913. struct ext4_group_desc *bg);
  2914. extern ext4_fsblk_t ext4_inode_bitmap(struct super_block *sb,
  2915. struct ext4_group_desc *bg);
  2916. extern ext4_fsblk_t ext4_inode_table(struct super_block *sb,
  2917. struct ext4_group_desc *bg);
  2918. extern __u32 ext4_free_group_clusters(struct super_block *sb,
  2919. struct ext4_group_desc *bg);
  2920. extern __u32 ext4_free_inodes_count(struct super_block *sb,
  2921. struct ext4_group_desc *bg);
  2922. extern __u32 ext4_used_dirs_count(struct super_block *sb,
  2923. struct ext4_group_desc *bg);
  2924. extern __u32 ext4_itable_unused_count(struct super_block *sb,
  2925. struct ext4_group_desc *bg);
  2926. extern void ext4_block_bitmap_set(struct super_block *sb,
  2927. struct ext4_group_desc *bg, ext4_fsblk_t blk);
  2928. extern void ext4_inode_bitmap_set(struct super_block *sb,
  2929. struct ext4_group_desc *bg, ext4_fsblk_t blk);
  2930. extern void ext4_inode_table_set(struct super_block *sb,
  2931. struct ext4_group_desc *bg, ext4_fsblk_t blk);
  2932. extern void ext4_free_group_clusters_set(struct super_block *sb,
  2933. struct ext4_group_desc *bg,
  2934. __u32 count);
  2935. extern void ext4_free_inodes_set(struct super_block *sb,
  2936. struct ext4_group_desc *bg, __u32 count);
  2937. extern void ext4_used_dirs_set(struct super_block *sb,
  2938. struct ext4_group_desc *bg, __u32 count);
  2939. extern void ext4_itable_unused_set(struct super_block *sb,
  2940. struct ext4_group_desc *bg, __u32 count);
  2941. extern int ext4_group_desc_csum_verify(struct super_block *sb, __u32 group,
  2942. struct ext4_group_desc *gdp);
  2943. extern void ext4_group_desc_csum_set(struct super_block *sb, __u32 group,
  2944. struct ext4_group_desc *gdp);
  2945. extern int ext4_register_li_request(struct super_block *sb,
  2946. ext4_group_t first_not_zeroed);
  2947. static inline int ext4_has_metadata_csum(struct super_block *sb)
  2948. {
  2949. WARN_ON_ONCE(ext4_has_feature_metadata_csum(sb) &&
  2950. !EXT4_SB(sb)->s_chksum_driver);
  2951. return ext4_has_feature_metadata_csum(sb) &&
  2952. (EXT4_SB(sb)->s_chksum_driver != NULL);
  2953. }
  2954. static inline int ext4_has_group_desc_csum(struct super_block *sb)
  2955. {
  2956. return ext4_has_feature_gdt_csum(sb) || ext4_has_metadata_csum(sb);
  2957. }
  2958. #define ext4_read_incompat_64bit_val(es, name) \
  2959. (((es)->s_feature_incompat & cpu_to_le32(EXT4_FEATURE_INCOMPAT_64BIT) \
  2960. ? (ext4_fsblk_t)le32_to_cpu(es->name##_hi) << 32 : 0) | \
  2961. le32_to_cpu(es->name##_lo))
  2962. static inline ext4_fsblk_t ext4_blocks_count(struct ext4_super_block *es)
  2963. {
  2964. return ext4_read_incompat_64bit_val(es, s_blocks_count);
  2965. }
  2966. static inline ext4_fsblk_t ext4_r_blocks_count(struct ext4_super_block *es)
  2967. {
  2968. return ext4_read_incompat_64bit_val(es, s_r_blocks_count);
  2969. }
  2970. static inline ext4_fsblk_t ext4_free_blocks_count(struct ext4_super_block *es)
  2971. {
  2972. return ext4_read_incompat_64bit_val(es, s_free_blocks_count);
  2973. }
  2974. static inline void ext4_blocks_count_set(struct ext4_super_block *es,
  2975. ext4_fsblk_t blk)
  2976. {
  2977. es->s_blocks_count_lo = cpu_to_le32((u32)blk);
  2978. es->s_blocks_count_hi = cpu_to_le32(blk >> 32);
  2979. }
  2980. static inline void ext4_free_blocks_count_set(struct ext4_super_block *es,
  2981. ext4_fsblk_t blk)
  2982. {
  2983. es->s_free_blocks_count_lo = cpu_to_le32((u32)blk);
  2984. es->s_free_blocks_count_hi = cpu_to_le32(blk >> 32);
  2985. }
  2986. static inline void ext4_r_blocks_count_set(struct ext4_super_block *es,
  2987. ext4_fsblk_t blk)
  2988. {
  2989. es->s_r_blocks_count_lo = cpu_to_le32((u32)blk);
  2990. es->s_r_blocks_count_hi = cpu_to_le32(blk >> 32);
  2991. }
  2992. static inline loff_t ext4_isize(struct super_block *sb,
  2993. struct ext4_inode *raw_inode)
  2994. {
  2995. if (ext4_has_feature_largedir(sb) ||
  2996. S_ISREG(le16_to_cpu(raw_inode->i_mode)))
  2997. return ((loff_t)le32_to_cpu(raw_inode->i_size_high) << 32) |
  2998. le32_to_cpu(raw_inode->i_size_lo);
  2999. return (loff_t) le32_to_cpu(raw_inode->i_size_lo);
  3000. }
  3001. static inline void ext4_isize_set(struct ext4_inode *raw_inode, loff_t i_size)
  3002. {
  3003. raw_inode->i_size_lo = cpu_to_le32(i_size);
  3004. raw_inode->i_size_high = cpu_to_le32(i_size >> 32);
  3005. }
  3006. /*
  3007. * Reading s_groups_count requires using smp_rmb() afterwards. See
  3008. * the locking protocol documented in the comments of ext4_group_add()
  3009. * in resize.c
  3010. */
  3011. static inline ext4_group_t ext4_get_groups_count(struct super_block *sb)
  3012. {
  3013. ext4_group_t ngroups = EXT4_SB(sb)->s_groups_count;
  3014. smp_rmb();
  3015. return ngroups;
  3016. }
  3017. static inline ext4_group_t ext4_flex_group(struct ext4_sb_info *sbi,
  3018. ext4_group_t block_group)
  3019. {
  3020. return block_group >> sbi->s_log_groups_per_flex;
  3021. }
  3022. static inline unsigned int ext4_flex_bg_size(struct ext4_sb_info *sbi)
  3023. {
  3024. return 1 << sbi->s_log_groups_per_flex;
  3025. }
  3026. static inline loff_t ext4_get_maxbytes(struct inode *inode)
  3027. {
  3028. if (ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  3029. return inode->i_sb->s_maxbytes;
  3030. return EXT4_SB(inode->i_sb)->s_bitmap_maxbytes;
  3031. }
  3032. #define ext4_std_error(sb, errno) \
  3033. do { \
  3034. if ((errno)) \
  3035. __ext4_std_error((sb), __func__, __LINE__, (errno)); \
  3036. } while (0)
  3037. #ifdef CONFIG_SMP
  3038. /* Each CPU can accumulate percpu_counter_batch clusters in their local
  3039. * counters. So we need to make sure we have free clusters more
  3040. * than percpu_counter_batch * nr_cpu_ids. Also add a window of 4 times.
  3041. */
  3042. #define EXT4_FREECLUSTERS_WATERMARK (4 * (percpu_counter_batch * nr_cpu_ids))
  3043. #else
  3044. #define EXT4_FREECLUSTERS_WATERMARK 0
  3045. #endif
  3046. /* Update i_disksize. Requires i_rwsem to avoid races with truncate */
  3047. static inline void ext4_update_i_disksize(struct inode *inode, loff_t newsize)
  3048. {
  3049. WARN_ON_ONCE(S_ISREG(inode->i_mode) &&
  3050. !inode_is_locked(inode));
  3051. down_write(&EXT4_I(inode)->i_data_sem);
  3052. if (newsize > EXT4_I(inode)->i_disksize)
  3053. WRITE_ONCE(EXT4_I(inode)->i_disksize, newsize);
  3054. up_write(&EXT4_I(inode)->i_data_sem);
  3055. }
  3056. /* Update i_size, i_disksize. Requires i_rwsem to avoid races with truncate */
  3057. static inline int ext4_update_inode_size(struct inode *inode, loff_t newsize)
  3058. {
  3059. int changed = 0;
  3060. if (newsize > inode->i_size) {
  3061. i_size_write(inode, newsize);
  3062. changed = 1;
  3063. }
  3064. if (newsize > EXT4_I(inode)->i_disksize) {
  3065. ext4_update_i_disksize(inode, newsize);
  3066. changed |= 2;
  3067. }
  3068. return changed;
  3069. }
  3070. int ext4_update_disksize_before_punch(struct inode *inode, loff_t offset,
  3071. loff_t len);
  3072. struct ext4_group_info {
  3073. unsigned long bb_state;
  3074. #ifdef AGGRESSIVE_CHECK
  3075. unsigned long bb_check_counter;
  3076. #endif
  3077. struct rb_root bb_free_root;
  3078. ext4_grpblk_t bb_first_free; /* first free block */
  3079. ext4_grpblk_t bb_free; /* total free blocks */
  3080. ext4_grpblk_t bb_fragments; /* nr of freespace fragments */
  3081. int bb_avg_fragment_size_order; /* order of average
  3082. fragment in BG */
  3083. ext4_grpblk_t bb_largest_free_order;/* order of largest frag in BG */
  3084. ext4_group_t bb_group; /* Group number */
  3085. struct list_head bb_prealloc_list;
  3086. #ifdef DOUBLE_CHECK
  3087. void *bb_bitmap;
  3088. #endif
  3089. struct rw_semaphore alloc_sem;
  3090. struct list_head bb_avg_fragment_size_node;
  3091. struct list_head bb_largest_free_order_node;
  3092. ext4_grpblk_t bb_counters[]; /* Nr of free power-of-two-block
  3093. * regions, index is order.
  3094. * bb_counters[3] = 5 means
  3095. * 5 free 8-block regions. */
  3096. };
  3097. #define EXT4_GROUP_INFO_NEED_INIT_BIT 0
  3098. #define EXT4_GROUP_INFO_WAS_TRIMMED_BIT 1
  3099. #define EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT 2
  3100. #define EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT 3
  3101. #define EXT4_GROUP_INFO_BBITMAP_CORRUPT \
  3102. (1 << EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT)
  3103. #define EXT4_GROUP_INFO_IBITMAP_CORRUPT \
  3104. (1 << EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT)
  3105. #define EXT4_GROUP_INFO_BBITMAP_READ_BIT 4
  3106. #define EXT4_MB_GRP_NEED_INIT(grp) \
  3107. (test_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &((grp)->bb_state)))
  3108. #define EXT4_MB_GRP_BBITMAP_CORRUPT(grp) \
  3109. (test_bit(EXT4_GROUP_INFO_BBITMAP_CORRUPT_BIT, &((grp)->bb_state)))
  3110. #define EXT4_MB_GRP_IBITMAP_CORRUPT(grp) \
  3111. (test_bit(EXT4_GROUP_INFO_IBITMAP_CORRUPT_BIT, &((grp)->bb_state)))
  3112. #define EXT4_MB_GRP_WAS_TRIMMED(grp) \
  3113. (test_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state)))
  3114. #define EXT4_MB_GRP_SET_TRIMMED(grp) \
  3115. (set_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state)))
  3116. #define EXT4_MB_GRP_CLEAR_TRIMMED(grp) \
  3117. (clear_bit(EXT4_GROUP_INFO_WAS_TRIMMED_BIT, &((grp)->bb_state)))
  3118. #define EXT4_MB_GRP_TEST_AND_SET_READ(grp) \
  3119. (test_and_set_bit(EXT4_GROUP_INFO_BBITMAP_READ_BIT, &((grp)->bb_state)))
  3120. #define EXT4_MAX_CONTENTION 8
  3121. #define EXT4_CONTENTION_THRESHOLD 2
  3122. static inline spinlock_t *ext4_group_lock_ptr(struct super_block *sb,
  3123. ext4_group_t group)
  3124. {
  3125. return bgl_lock_ptr(EXT4_SB(sb)->s_blockgroup_lock, group);
  3126. }
  3127. /*
  3128. * Returns true if the filesystem is busy enough that attempts to
  3129. * access the block group locks has run into contention.
  3130. */
  3131. static inline int ext4_fs_is_busy(struct ext4_sb_info *sbi)
  3132. {
  3133. return (atomic_read(&sbi->s_lock_busy) > EXT4_CONTENTION_THRESHOLD);
  3134. }
  3135. static inline void ext4_lock_group(struct super_block *sb, ext4_group_t group)
  3136. {
  3137. spinlock_t *lock = ext4_group_lock_ptr(sb, group);
  3138. if (spin_trylock(lock))
  3139. /*
  3140. * We're able to grab the lock right away, so drop the
  3141. * lock contention counter.
  3142. */
  3143. atomic_add_unless(&EXT4_SB(sb)->s_lock_busy, -1, 0);
  3144. else {
  3145. /*
  3146. * The lock is busy, so bump the contention counter,
  3147. * and then wait on the spin lock.
  3148. */
  3149. atomic_add_unless(&EXT4_SB(sb)->s_lock_busy, 1,
  3150. EXT4_MAX_CONTENTION);
  3151. spin_lock(lock);
  3152. }
  3153. }
  3154. static inline void ext4_unlock_group(struct super_block *sb,
  3155. ext4_group_t group)
  3156. {
  3157. spin_unlock(ext4_group_lock_ptr(sb, group));
  3158. }
  3159. #ifdef CONFIG_QUOTA
  3160. static inline bool ext4_quota_capable(struct super_block *sb)
  3161. {
  3162. return (test_opt(sb, QUOTA) || ext4_has_feature_quota(sb));
  3163. }
  3164. static inline bool ext4_is_quota_journalled(struct super_block *sb)
  3165. {
  3166. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3167. return (ext4_has_feature_quota(sb) ||
  3168. sbi->s_qf_names[USRQUOTA] || sbi->s_qf_names[GRPQUOTA]);
  3169. }
  3170. int ext4_enable_quotas(struct super_block *sb);
  3171. #endif
  3172. /*
  3173. * Block validity checking
  3174. */
  3175. #define ext4_check_indirect_blockref(inode, bh) \
  3176. ext4_check_blockref(__func__, __LINE__, inode, \
  3177. (__le32 *)(bh)->b_data, \
  3178. EXT4_ADDR_PER_BLOCK((inode)->i_sb))
  3179. #define ext4_ind_check_inode(inode) \
  3180. ext4_check_blockref(__func__, __LINE__, inode, \
  3181. EXT4_I(inode)->i_data, \
  3182. EXT4_NDIR_BLOCKS)
  3183. /*
  3184. * Inodes and files operations
  3185. */
  3186. /* dir.c */
  3187. extern const struct file_operations ext4_dir_operations;
  3188. /* file.c */
  3189. extern const struct inode_operations ext4_file_inode_operations;
  3190. extern const struct file_operations ext4_file_operations;
  3191. extern loff_t ext4_llseek(struct file *file, loff_t offset, int origin);
  3192. /* inline.c */
  3193. extern int ext4_get_max_inline_size(struct inode *inode);
  3194. extern int ext4_find_inline_data_nolock(struct inode *inode);
  3195. extern int ext4_destroy_inline_data(handle_t *handle, struct inode *inode);
  3196. int ext4_readpage_inline(struct inode *inode, struct folio *folio);
  3197. extern int ext4_try_to_write_inline_data(struct address_space *mapping,
  3198. struct inode *inode,
  3199. loff_t pos, unsigned len,
  3200. struct folio **foliop);
  3201. int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len,
  3202. unsigned copied, struct folio *folio);
  3203. extern int ext4_da_write_inline_data_begin(struct address_space *mapping,
  3204. struct inode *inode,
  3205. loff_t pos, unsigned len,
  3206. struct folio **foliop,
  3207. void **fsdata);
  3208. extern int ext4_try_add_inline_entry(handle_t *handle,
  3209. struct ext4_filename *fname,
  3210. struct inode *dir, struct inode *inode);
  3211. extern int ext4_try_create_inline_dir(handle_t *handle,
  3212. struct inode *parent,
  3213. struct inode *inode);
  3214. extern int ext4_read_inline_dir(struct file *filp,
  3215. struct dir_context *ctx,
  3216. int *has_inline_data);
  3217. extern int ext4_inlinedir_to_tree(struct file *dir_file,
  3218. struct inode *dir, ext4_lblk_t block,
  3219. struct dx_hash_info *hinfo,
  3220. __u32 start_hash, __u32 start_minor_hash,
  3221. int *has_inline_data);
  3222. extern struct buffer_head *ext4_find_inline_entry(struct inode *dir,
  3223. struct ext4_filename *fname,
  3224. struct ext4_dir_entry_2 **res_dir,
  3225. int *has_inline_data);
  3226. extern int ext4_delete_inline_entry(handle_t *handle,
  3227. struct inode *dir,
  3228. struct ext4_dir_entry_2 *de_del,
  3229. struct buffer_head *bh,
  3230. int *has_inline_data);
  3231. extern bool empty_inline_dir(struct inode *dir, int *has_inline_data);
  3232. extern struct buffer_head *ext4_get_first_inline_block(struct inode *inode,
  3233. struct ext4_dir_entry_2 **parent_de,
  3234. int *retval);
  3235. extern void *ext4_read_inline_link(struct inode *inode);
  3236. struct iomap;
  3237. extern int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap);
  3238. extern int ext4_inline_data_truncate(struct inode *inode, int *has_inline);
  3239. extern int ext4_convert_inline_data(struct inode *inode);
  3240. static inline int ext4_has_inline_data(struct inode *inode)
  3241. {
  3242. return ext4_test_inode_flag(inode, EXT4_INODE_INLINE_DATA) &&
  3243. EXT4_I(inode)->i_inline_off;
  3244. }
  3245. /* namei.c */
  3246. extern const struct inode_operations ext4_dir_inode_operations;
  3247. extern const struct inode_operations ext4_special_inode_operations;
  3248. extern struct dentry *ext4_get_parent(struct dentry *child);
  3249. extern struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode,
  3250. struct ext4_dir_entry_2 *de,
  3251. int blocksize, int csum_size,
  3252. unsigned int parent_ino, int dotdot_real_len);
  3253. extern void ext4_initialize_dirent_tail(struct buffer_head *bh,
  3254. unsigned int blocksize);
  3255. extern int ext4_handle_dirty_dirblock(handle_t *handle, struct inode *inode,
  3256. struct buffer_head *bh);
  3257. extern int __ext4_unlink(struct inode *dir, const struct qstr *d_name,
  3258. struct inode *inode, struct dentry *dentry);
  3259. extern int __ext4_link(struct inode *dir, struct inode *inode,
  3260. struct dentry *dentry);
  3261. #define S_SHIFT 12
  3262. static const unsigned char ext4_type_by_mode[(S_IFMT >> S_SHIFT) + 1] = {
  3263. [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE,
  3264. [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR,
  3265. [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV,
  3266. [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV,
  3267. [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO,
  3268. [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK,
  3269. [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK,
  3270. };
  3271. static inline void ext4_set_de_type(struct super_block *sb,
  3272. struct ext4_dir_entry_2 *de,
  3273. umode_t mode) {
  3274. if (ext4_has_feature_filetype(sb))
  3275. de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT];
  3276. }
  3277. /* readpages.c */
  3278. extern int ext4_mpage_readpages(struct inode *inode,
  3279. struct readahead_control *rac, struct folio *folio);
  3280. extern int __init ext4_init_post_read_processing(void);
  3281. extern void ext4_exit_post_read_processing(void);
  3282. /* symlink.c */
  3283. extern const struct inode_operations ext4_encrypted_symlink_inode_operations;
  3284. extern const struct inode_operations ext4_symlink_inode_operations;
  3285. extern const struct inode_operations ext4_fast_symlink_inode_operations;
  3286. /* sysfs.c */
  3287. extern void ext4_notify_error_sysfs(struct ext4_sb_info *sbi);
  3288. extern int ext4_register_sysfs(struct super_block *sb);
  3289. extern void ext4_unregister_sysfs(struct super_block *sb);
  3290. extern int __init ext4_init_sysfs(void);
  3291. extern void ext4_exit_sysfs(void);
  3292. /* block_validity */
  3293. extern void ext4_release_system_zone(struct super_block *sb);
  3294. extern int ext4_setup_system_zone(struct super_block *sb);
  3295. extern int __init ext4_init_system_zone(void);
  3296. extern void ext4_exit_system_zone(void);
  3297. extern int ext4_inode_block_valid(struct inode *inode,
  3298. ext4_fsblk_t start_blk,
  3299. unsigned int count);
  3300. extern int ext4_check_blockref(const char *, unsigned int,
  3301. struct inode *, __le32 *, unsigned int);
  3302. extern int ext4_sb_block_valid(struct super_block *sb, struct inode *inode,
  3303. ext4_fsblk_t start_blk, unsigned int count);
  3304. /* extents.c */
  3305. struct ext4_ext_path;
  3306. struct ext4_extent;
  3307. /*
  3308. * Maximum number of logical blocks in a file; ext4_extent's ee_block is
  3309. * __le32.
  3310. */
  3311. #define EXT_MAX_BLOCKS 0xffffffff
  3312. extern void ext4_ext_tree_init(handle_t *handle, struct inode *inode);
  3313. extern int ext4_ext_index_trans_blocks(struct inode *inode, int extents);
  3314. extern int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
  3315. struct ext4_map_blocks *map, int flags);
  3316. extern int ext4_ext_truncate(handle_t *, struct inode *);
  3317. extern int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
  3318. ext4_lblk_t end);
  3319. extern void ext4_ext_init(struct super_block *);
  3320. extern void ext4_ext_release(struct super_block *);
  3321. extern long ext4_fallocate(struct file *file, int mode, loff_t offset,
  3322. loff_t len);
  3323. extern int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
  3324. loff_t offset, ssize_t len);
  3325. extern int ext4_convert_unwritten_io_end_vec(handle_t *handle,
  3326. ext4_io_end_t *io_end);
  3327. extern int ext4_map_blocks(handle_t *handle, struct inode *inode,
  3328. struct ext4_map_blocks *map, int flags);
  3329. extern int ext4_ext_calc_credits_for_single_extent(struct inode *inode,
  3330. int num,
  3331. struct ext4_ext_path *path);
  3332. extern struct ext4_ext_path *ext4_ext_insert_extent(
  3333. handle_t *handle, struct inode *inode,
  3334. struct ext4_ext_path *path,
  3335. struct ext4_extent *newext, int gb_flags);
  3336. extern struct ext4_ext_path *ext4_find_extent(struct inode *, ext4_lblk_t,
  3337. struct ext4_ext_path *,
  3338. int flags);
  3339. extern void ext4_free_ext_path(struct ext4_ext_path *);
  3340. extern int ext4_ext_check_inode(struct inode *inode);
  3341. extern ext4_lblk_t ext4_ext_next_allocated_block(struct ext4_ext_path *path);
  3342. extern int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  3343. __u64 start, __u64 len);
  3344. extern int ext4_get_es_cache(struct inode *inode,
  3345. struct fiemap_extent_info *fieinfo,
  3346. __u64 start, __u64 len);
  3347. extern int ext4_ext_precache(struct inode *inode);
  3348. extern int ext4_swap_extents(handle_t *handle, struct inode *inode1,
  3349. struct inode *inode2, ext4_lblk_t lblk1,
  3350. ext4_lblk_t lblk2, ext4_lblk_t count,
  3351. int mark_unwritten,int *err);
  3352. extern int ext4_clu_mapped(struct inode *inode, ext4_lblk_t lclu);
  3353. extern int ext4_datasem_ensure_credits(handle_t *handle, struct inode *inode,
  3354. int check_cred, int restart_cred,
  3355. int revoke_cred);
  3356. extern void ext4_ext_replay_shrink_inode(struct inode *inode, ext4_lblk_t end);
  3357. extern int ext4_ext_replay_set_iblocks(struct inode *inode);
  3358. extern int ext4_ext_replay_update_ex(struct inode *inode, ext4_lblk_t start,
  3359. int len, int unwritten, ext4_fsblk_t pblk);
  3360. extern int ext4_ext_clear_bb(struct inode *inode);
  3361. /* move_extent.c */
  3362. extern void ext4_double_down_write_data_sem(struct inode *first,
  3363. struct inode *second);
  3364. extern void ext4_double_up_write_data_sem(struct inode *orig_inode,
  3365. struct inode *donor_inode);
  3366. extern int ext4_move_extents(struct file *o_filp, struct file *d_filp,
  3367. __u64 start_orig, __u64 start_donor,
  3368. __u64 len, __u64 *moved_len);
  3369. /* page-io.c */
  3370. extern int __init ext4_init_pageio(void);
  3371. extern void ext4_exit_pageio(void);
  3372. extern ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags);
  3373. extern ext4_io_end_t *ext4_get_io_end(ext4_io_end_t *io_end);
  3374. extern int ext4_put_io_end(ext4_io_end_t *io_end);
  3375. extern void ext4_put_io_end_defer(ext4_io_end_t *io_end);
  3376. extern void ext4_io_submit_init(struct ext4_io_submit *io,
  3377. struct writeback_control *wbc);
  3378. extern void ext4_end_io_rsv_work(struct work_struct *work);
  3379. extern void ext4_io_submit(struct ext4_io_submit *io);
  3380. int ext4_bio_write_folio(struct ext4_io_submit *io, struct folio *page,
  3381. size_t len);
  3382. extern struct ext4_io_end_vec *ext4_alloc_io_end_vec(ext4_io_end_t *io_end);
  3383. extern struct ext4_io_end_vec *ext4_last_io_end_vec(ext4_io_end_t *io_end);
  3384. /* mmp.c */
  3385. extern int ext4_multi_mount_protect(struct super_block *, ext4_fsblk_t);
  3386. /* mmp.c */
  3387. extern void ext4_stop_mmpd(struct ext4_sb_info *sbi);
  3388. /* verity.c */
  3389. extern const struct fsverity_operations ext4_verityops;
  3390. /* orphan.c */
  3391. extern int ext4_orphan_add(handle_t *, struct inode *);
  3392. extern int ext4_orphan_del(handle_t *, struct inode *);
  3393. extern void ext4_orphan_cleanup(struct super_block *sb,
  3394. struct ext4_super_block *es);
  3395. extern void ext4_release_orphan_info(struct super_block *sb);
  3396. extern int ext4_init_orphan_info(struct super_block *sb);
  3397. extern int ext4_orphan_file_empty(struct super_block *sb);
  3398. extern void ext4_orphan_file_block_trigger(
  3399. struct jbd2_buffer_trigger_type *triggers,
  3400. struct buffer_head *bh,
  3401. void *data, size_t size);
  3402. /*
  3403. * Add new method to test whether block and inode bitmaps are properly
  3404. * initialized. With uninit_bg reading the block from disk is not enough
  3405. * to mark the bitmap uptodate. We need to also zero-out the bitmap
  3406. */
  3407. #define BH_BITMAP_UPTODATE BH_JBDPrivateStart
  3408. static inline int bitmap_uptodate(struct buffer_head *bh)
  3409. {
  3410. return (buffer_uptodate(bh) &&
  3411. test_bit(BH_BITMAP_UPTODATE, &(bh)->b_state));
  3412. }
  3413. static inline void set_bitmap_uptodate(struct buffer_head *bh)
  3414. {
  3415. set_bit(BH_BITMAP_UPTODATE, &(bh)->b_state);
  3416. }
  3417. /* For ioend & aio unwritten conversion wait queues */
  3418. #define EXT4_WQ_HASH_SZ 37
  3419. #define ext4_ioend_wq(v) (&ext4__ioend_wq[((unsigned long)(v)) %\
  3420. EXT4_WQ_HASH_SZ])
  3421. extern wait_queue_head_t ext4__ioend_wq[EXT4_WQ_HASH_SZ];
  3422. extern int ext4_resize_begin(struct super_block *sb);
  3423. extern int ext4_resize_end(struct super_block *sb, bool update_backups);
  3424. static inline void ext4_set_io_unwritten_flag(struct inode *inode,
  3425. struct ext4_io_end *io_end)
  3426. {
  3427. if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
  3428. io_end->flag |= EXT4_IO_END_UNWRITTEN;
  3429. atomic_inc(&EXT4_I(inode)->i_unwritten);
  3430. }
  3431. }
  3432. static inline void ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end)
  3433. {
  3434. struct inode *inode = io_end->inode;
  3435. if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
  3436. io_end->flag &= ~EXT4_IO_END_UNWRITTEN;
  3437. /* Wake up anyone waiting on unwritten extent conversion */
  3438. if (atomic_dec_and_test(&EXT4_I(inode)->i_unwritten))
  3439. wake_up_all(ext4_ioend_wq(inode));
  3440. }
  3441. }
  3442. extern const struct iomap_ops ext4_iomap_ops;
  3443. extern const struct iomap_ops ext4_iomap_overwrite_ops;
  3444. extern const struct iomap_ops ext4_iomap_report_ops;
  3445. static inline int ext4_buffer_uptodate(struct buffer_head *bh)
  3446. {
  3447. /*
  3448. * If the buffer has the write error flag, we have failed
  3449. * to write out data in the block. In this case, we don't
  3450. * have to read the block because we may read the old data
  3451. * successfully.
  3452. */
  3453. if (buffer_write_io_error(bh))
  3454. set_buffer_uptodate(bh);
  3455. return buffer_uptodate(bh);
  3456. }
  3457. extern int ext4_block_write_begin(handle_t *handle, struct folio *folio,
  3458. loff_t pos, unsigned len,
  3459. get_block_t *get_block);
  3460. #endif /* __KERNEL__ */
  3461. #define EFSBADCRC EBADMSG /* Bad CRC detected */
  3462. #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
  3463. #endif /* _EXT4_H */