ext4.h 137 KB

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