extents.c 134 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870
  1. /*
  2. * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
  3. * Written by Alex Tomas <alex@clusterfs.com>
  4. *
  5. * Architecture independence:
  6. * Copyright (c) 2005, Bull S.A.
  7. * Written by Pierre Peiffer <pierre.peiffer@bull.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public Licens
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  21. */
  22. /*
  23. * Extents support for EXT4
  24. *
  25. * TODO:
  26. * - ext4*_error() should be used in some situations
  27. * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
  28. * - smart tree reduction
  29. */
  30. #include <linux/fs.h>
  31. #include <linux/time.h>
  32. #include <linux/jbd2.h>
  33. #include <linux/highuid.h>
  34. #include <linux/pagemap.h>
  35. #include <linux/quotaops.h>
  36. #include <linux/string.h>
  37. #include <linux/slab.h>
  38. #include <linux/falloc.h>
  39. #include <asm/uaccess.h>
  40. #include <linux/fiemap.h>
  41. #include "ext4_jbd2.h"
  42. #include "ext4_extents.h"
  43. #include "xattr.h"
  44. #include <trace/events/ext4.h>
  45. /*
  46. * used by extent splitting.
  47. */
  48. #define EXT4_EXT_MAY_ZEROOUT 0x1 /* safe to zeroout if split fails \
  49. due to ENOSPC */
  50. #define EXT4_EXT_MARK_UNINIT1 0x2 /* mark first half uninitialized */
  51. #define EXT4_EXT_MARK_UNINIT2 0x4 /* mark second half uninitialized */
  52. #define EXT4_EXT_DATA_VALID1 0x8 /* first half contains valid data */
  53. #define EXT4_EXT_DATA_VALID2 0x10 /* second half contains valid data */
  54. static __le32 ext4_extent_block_csum(struct inode *inode,
  55. struct ext4_extent_header *eh)
  56. {
  57. struct ext4_inode_info *ei = EXT4_I(inode);
  58. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  59. __u32 csum;
  60. csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)eh,
  61. EXT4_EXTENT_TAIL_OFFSET(eh));
  62. return cpu_to_le32(csum);
  63. }
  64. static int ext4_extent_block_csum_verify(struct inode *inode,
  65. struct ext4_extent_header *eh)
  66. {
  67. struct ext4_extent_tail *et;
  68. if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  69. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  70. return 1;
  71. et = find_ext4_extent_tail(eh);
  72. if (et->et_checksum != ext4_extent_block_csum(inode, eh))
  73. return 0;
  74. return 1;
  75. }
  76. static void ext4_extent_block_csum_set(struct inode *inode,
  77. struct ext4_extent_header *eh)
  78. {
  79. struct ext4_extent_tail *et;
  80. if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb,
  81. EXT4_FEATURE_RO_COMPAT_METADATA_CSUM))
  82. return;
  83. et = find_ext4_extent_tail(eh);
  84. et->et_checksum = ext4_extent_block_csum(inode, eh);
  85. }
  86. static int ext4_split_extent(handle_t *handle,
  87. struct inode *inode,
  88. struct ext4_ext_path *path,
  89. struct ext4_map_blocks *map,
  90. int split_flag,
  91. int flags);
  92. static int ext4_split_extent_at(handle_t *handle,
  93. struct inode *inode,
  94. struct ext4_ext_path *path,
  95. ext4_lblk_t split,
  96. int split_flag,
  97. int flags);
  98. static int ext4_find_delayed_extent(struct inode *inode,
  99. struct extent_status *newes);
  100. static int ext4_ext_truncate_extend_restart(handle_t *handle,
  101. struct inode *inode,
  102. int needed)
  103. {
  104. int err;
  105. if (!ext4_handle_valid(handle))
  106. return 0;
  107. if (handle->h_buffer_credits > needed)
  108. return 0;
  109. err = ext4_journal_extend(handle, needed);
  110. if (err <= 0)
  111. return err;
  112. err = ext4_truncate_restart_trans(handle, inode, needed);
  113. if (err == 0)
  114. err = -EAGAIN;
  115. return err;
  116. }
  117. /*
  118. * could return:
  119. * - EROFS
  120. * - ENOMEM
  121. */
  122. static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
  123. struct ext4_ext_path *path)
  124. {
  125. if (path->p_bh) {
  126. /* path points to block */
  127. return ext4_journal_get_write_access(handle, path->p_bh);
  128. }
  129. /* path points to leaf/index in inode body */
  130. /* we use in-core data, no need to protect them */
  131. return 0;
  132. }
  133. /*
  134. * could return:
  135. * - EROFS
  136. * - ENOMEM
  137. * - EIO
  138. */
  139. int __ext4_ext_dirty(const char *where, unsigned int line, handle_t *handle,
  140. struct inode *inode, struct ext4_ext_path *path)
  141. {
  142. int err;
  143. if (path->p_bh) {
  144. ext4_extent_block_csum_set(inode, ext_block_hdr(path->p_bh));
  145. /* path points to block */
  146. err = __ext4_handle_dirty_metadata(where, line, handle,
  147. inode, path->p_bh);
  148. } else {
  149. /* path points to leaf/index in inode body */
  150. err = ext4_mark_inode_dirty(handle, inode);
  151. }
  152. return err;
  153. }
  154. static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
  155. struct ext4_ext_path *path,
  156. ext4_lblk_t block)
  157. {
  158. if (path) {
  159. int depth = path->p_depth;
  160. struct ext4_extent *ex;
  161. /*
  162. * Try to predict block placement assuming that we are
  163. * filling in a file which will eventually be
  164. * non-sparse --- i.e., in the case of libbfd writing
  165. * an ELF object sections out-of-order but in a way
  166. * the eventually results in a contiguous object or
  167. * executable file, or some database extending a table
  168. * space file. However, this is actually somewhat
  169. * non-ideal if we are writing a sparse file such as
  170. * qemu or KVM writing a raw image file that is going
  171. * to stay fairly sparse, since it will end up
  172. * fragmenting the file system's free space. Maybe we
  173. * should have some hueristics or some way to allow
  174. * userspace to pass a hint to file system,
  175. * especially if the latter case turns out to be
  176. * common.
  177. */
  178. ex = path[depth].p_ext;
  179. if (ex) {
  180. ext4_fsblk_t ext_pblk = ext4_ext_pblock(ex);
  181. ext4_lblk_t ext_block = le32_to_cpu(ex->ee_block);
  182. if (block > ext_block)
  183. return ext_pblk + (block - ext_block);
  184. else
  185. return ext_pblk - (ext_block - block);
  186. }
  187. /* it looks like index is empty;
  188. * try to find starting block from index itself */
  189. if (path[depth].p_bh)
  190. return path[depth].p_bh->b_blocknr;
  191. }
  192. /* OK. use inode's group */
  193. return ext4_inode_to_goal_block(inode);
  194. }
  195. /*
  196. * Allocation for a meta data block
  197. */
  198. static ext4_fsblk_t
  199. ext4_ext_new_meta_block(handle_t *handle, struct inode *inode,
  200. struct ext4_ext_path *path,
  201. struct ext4_extent *ex, int *err, unsigned int flags)
  202. {
  203. ext4_fsblk_t goal, newblock;
  204. goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
  205. newblock = ext4_new_meta_blocks(handle, inode, goal, flags,
  206. NULL, err);
  207. return newblock;
  208. }
  209. static inline int ext4_ext_space_block(struct inode *inode, int check)
  210. {
  211. int size;
  212. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  213. / sizeof(struct ext4_extent);
  214. #ifdef AGGRESSIVE_TEST
  215. if (!check && size > 6)
  216. size = 6;
  217. #endif
  218. return size;
  219. }
  220. static inline int ext4_ext_space_block_idx(struct inode *inode, int check)
  221. {
  222. int size;
  223. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  224. / sizeof(struct ext4_extent_idx);
  225. #ifdef AGGRESSIVE_TEST
  226. if (!check && size > 5)
  227. size = 5;
  228. #endif
  229. return size;
  230. }
  231. static inline int ext4_ext_space_root(struct inode *inode, int check)
  232. {
  233. int size;
  234. size = sizeof(EXT4_I(inode)->i_data);
  235. size -= sizeof(struct ext4_extent_header);
  236. size /= sizeof(struct ext4_extent);
  237. #ifdef AGGRESSIVE_TEST
  238. if (!check && size > 3)
  239. size = 3;
  240. #endif
  241. return size;
  242. }
  243. static inline int ext4_ext_space_root_idx(struct inode *inode, int check)
  244. {
  245. int size;
  246. size = sizeof(EXT4_I(inode)->i_data);
  247. size -= sizeof(struct ext4_extent_header);
  248. size /= sizeof(struct ext4_extent_idx);
  249. #ifdef AGGRESSIVE_TEST
  250. if (!check && size > 4)
  251. size = 4;
  252. #endif
  253. return size;
  254. }
  255. /*
  256. * Calculate the number of metadata blocks needed
  257. * to allocate @blocks
  258. * Worse case is one block per extent
  259. */
  260. int ext4_ext_calc_metadata_amount(struct inode *inode, ext4_lblk_t lblock)
  261. {
  262. struct ext4_inode_info *ei = EXT4_I(inode);
  263. int idxs;
  264. idxs = ((inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  265. / sizeof(struct ext4_extent_idx));
  266. /*
  267. * If the new delayed allocation block is contiguous with the
  268. * previous da block, it can share index blocks with the
  269. * previous block, so we only need to allocate a new index
  270. * block every idxs leaf blocks. At ldxs**2 blocks, we need
  271. * an additional index block, and at ldxs**3 blocks, yet
  272. * another index blocks.
  273. */
  274. if (ei->i_da_metadata_calc_len &&
  275. ei->i_da_metadata_calc_last_lblock+1 == lblock) {
  276. int num = 0;
  277. if ((ei->i_da_metadata_calc_len % idxs) == 0)
  278. num++;
  279. if ((ei->i_da_metadata_calc_len % (idxs*idxs)) == 0)
  280. num++;
  281. if ((ei->i_da_metadata_calc_len % (idxs*idxs*idxs)) == 0) {
  282. num++;
  283. ei->i_da_metadata_calc_len = 0;
  284. } else
  285. ei->i_da_metadata_calc_len++;
  286. ei->i_da_metadata_calc_last_lblock++;
  287. return num;
  288. }
  289. /*
  290. * In the worst case we need a new set of index blocks at
  291. * every level of the inode's extent tree.
  292. */
  293. ei->i_da_metadata_calc_len = 1;
  294. ei->i_da_metadata_calc_last_lblock = lblock;
  295. return ext_depth(inode) + 1;
  296. }
  297. static int
  298. ext4_ext_max_entries(struct inode *inode, int depth)
  299. {
  300. int max;
  301. if (depth == ext_depth(inode)) {
  302. if (depth == 0)
  303. max = ext4_ext_space_root(inode, 1);
  304. else
  305. max = ext4_ext_space_root_idx(inode, 1);
  306. } else {
  307. if (depth == 0)
  308. max = ext4_ext_space_block(inode, 1);
  309. else
  310. max = ext4_ext_space_block_idx(inode, 1);
  311. }
  312. return max;
  313. }
  314. static int ext4_valid_extent(struct inode *inode, struct ext4_extent *ext)
  315. {
  316. ext4_fsblk_t block = ext4_ext_pblock(ext);
  317. int len = ext4_ext_get_actual_len(ext);
  318. if (len == 0)
  319. return 0;
  320. return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, len);
  321. }
  322. static int ext4_valid_extent_idx(struct inode *inode,
  323. struct ext4_extent_idx *ext_idx)
  324. {
  325. ext4_fsblk_t block = ext4_idx_pblock(ext_idx);
  326. return ext4_data_block_valid(EXT4_SB(inode->i_sb), block, 1);
  327. }
  328. static int ext4_valid_extent_entries(struct inode *inode,
  329. struct ext4_extent_header *eh,
  330. int depth)
  331. {
  332. unsigned short entries;
  333. if (eh->eh_entries == 0)
  334. return 1;
  335. entries = le16_to_cpu(eh->eh_entries);
  336. if (depth == 0) {
  337. /* leaf entries */
  338. struct ext4_extent *ext = EXT_FIRST_EXTENT(eh);
  339. while (entries) {
  340. if (!ext4_valid_extent(inode, ext))
  341. return 0;
  342. ext++;
  343. entries--;
  344. }
  345. } else {
  346. struct ext4_extent_idx *ext_idx = EXT_FIRST_INDEX(eh);
  347. while (entries) {
  348. if (!ext4_valid_extent_idx(inode, ext_idx))
  349. return 0;
  350. ext_idx++;
  351. entries--;
  352. }
  353. }
  354. return 1;
  355. }
  356. static int __ext4_ext_check(const char *function, unsigned int line,
  357. struct inode *inode, struct ext4_extent_header *eh,
  358. int depth, ext4_fsblk_t pblk)
  359. {
  360. const char *error_msg;
  361. int max = 0;
  362. if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
  363. error_msg = "invalid magic";
  364. goto corrupted;
  365. }
  366. if (unlikely(le16_to_cpu(eh->eh_depth) != depth)) {
  367. error_msg = "unexpected eh_depth";
  368. goto corrupted;
  369. }
  370. if (unlikely(eh->eh_max == 0)) {
  371. error_msg = "invalid eh_max";
  372. goto corrupted;
  373. }
  374. max = ext4_ext_max_entries(inode, depth);
  375. if (unlikely(le16_to_cpu(eh->eh_max) > max)) {
  376. error_msg = "too large eh_max";
  377. goto corrupted;
  378. }
  379. if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
  380. error_msg = "invalid eh_entries";
  381. goto corrupted;
  382. }
  383. if (!ext4_valid_extent_entries(inode, eh, depth)) {
  384. error_msg = "invalid extent entries";
  385. goto corrupted;
  386. }
  387. /* Verify checksum on non-root extent tree nodes */
  388. if (ext_depth(inode) != depth &&
  389. !ext4_extent_block_csum_verify(inode, eh)) {
  390. error_msg = "extent tree corrupted";
  391. goto corrupted;
  392. }
  393. return 0;
  394. corrupted:
  395. ext4_error_inode(inode, function, line, 0,
  396. "pblk %llu bad header/extent: %s - magic %x, "
  397. "entries %u, max %u(%u), depth %u(%u)",
  398. (unsigned long long) pblk, error_msg,
  399. le16_to_cpu(eh->eh_magic),
  400. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
  401. max, le16_to_cpu(eh->eh_depth), depth);
  402. return -EIO;
  403. }
  404. #define ext4_ext_check(inode, eh, depth, pblk) \
  405. __ext4_ext_check(__func__, __LINE__, (inode), (eh), (depth), (pblk))
  406. int ext4_ext_check_inode(struct inode *inode)
  407. {
  408. return ext4_ext_check(inode, ext_inode_hdr(inode), ext_depth(inode), 0);
  409. }
  410. static struct buffer_head *
  411. __read_extent_tree_block(const char *function, unsigned int line,
  412. struct inode *inode, ext4_fsblk_t pblk, int depth,
  413. int flags)
  414. {
  415. struct buffer_head *bh;
  416. int err;
  417. bh = sb_getblk(inode->i_sb, pblk);
  418. if (unlikely(!bh))
  419. return ERR_PTR(-ENOMEM);
  420. if (!bh_uptodate_or_lock(bh)) {
  421. trace_ext4_ext_load_extent(inode, pblk, _RET_IP_);
  422. err = bh_submit_read(bh);
  423. if (err < 0)
  424. goto errout;
  425. }
  426. if (buffer_verified(bh) && !(flags & EXT4_EX_FORCE_CACHE))
  427. return bh;
  428. err = __ext4_ext_check(function, line, inode,
  429. ext_block_hdr(bh), depth, pblk);
  430. if (err)
  431. goto errout;
  432. set_buffer_verified(bh);
  433. /*
  434. * If this is a leaf block, cache all of its entries
  435. */
  436. if (!(flags & EXT4_EX_NOCACHE) && depth == 0) {
  437. struct ext4_extent_header *eh = ext_block_hdr(bh);
  438. struct ext4_extent *ex = EXT_FIRST_EXTENT(eh);
  439. ext4_lblk_t prev = 0;
  440. int i;
  441. for (i = le16_to_cpu(eh->eh_entries); i > 0; i--, ex++) {
  442. unsigned int status = EXTENT_STATUS_WRITTEN;
  443. ext4_lblk_t lblk = le32_to_cpu(ex->ee_block);
  444. int len = ext4_ext_get_actual_len(ex);
  445. if (prev && (prev != lblk))
  446. ext4_es_cache_extent(inode, prev,
  447. lblk - prev, ~0,
  448. EXTENT_STATUS_HOLE);
  449. if (ext4_ext_is_uninitialized(ex))
  450. status = EXTENT_STATUS_UNWRITTEN;
  451. ext4_es_cache_extent(inode, lblk, len,
  452. ext4_ext_pblock(ex), status);
  453. prev = lblk + len;
  454. }
  455. }
  456. return bh;
  457. errout:
  458. put_bh(bh);
  459. return ERR_PTR(err);
  460. }
  461. #define read_extent_tree_block(inode, pblk, depth, flags) \
  462. __read_extent_tree_block(__func__, __LINE__, (inode), (pblk), \
  463. (depth), (flags))
  464. /*
  465. * This function is called to cache a file's extent information in the
  466. * extent status tree
  467. */
  468. int ext4_ext_precache(struct inode *inode)
  469. {
  470. struct ext4_inode_info *ei = EXT4_I(inode);
  471. struct ext4_ext_path *path = NULL;
  472. struct buffer_head *bh;
  473. int i = 0, depth, ret = 0;
  474. if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS))
  475. return 0; /* not an extent-mapped inode */
  476. down_read(&ei->i_data_sem);
  477. depth = ext_depth(inode);
  478. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1),
  479. GFP_NOFS);
  480. if (path == NULL) {
  481. up_read(&ei->i_data_sem);
  482. return -ENOMEM;
  483. }
  484. /* Don't cache anything if there are no external extent blocks */
  485. if (depth == 0)
  486. goto out;
  487. path[0].p_hdr = ext_inode_hdr(inode);
  488. ret = ext4_ext_check(inode, path[0].p_hdr, depth, 0);
  489. if (ret)
  490. goto out;
  491. path[0].p_idx = EXT_FIRST_INDEX(path[0].p_hdr);
  492. while (i >= 0) {
  493. /*
  494. * If this is a leaf block or we've reached the end of
  495. * the index block, go up
  496. */
  497. if ((i == depth) ||
  498. path[i].p_idx > EXT_LAST_INDEX(path[i].p_hdr)) {
  499. brelse(path[i].p_bh);
  500. path[i].p_bh = NULL;
  501. i--;
  502. continue;
  503. }
  504. bh = read_extent_tree_block(inode,
  505. ext4_idx_pblock(path[i].p_idx++),
  506. depth - i - 1,
  507. EXT4_EX_FORCE_CACHE);
  508. if (IS_ERR(bh)) {
  509. ret = PTR_ERR(bh);
  510. break;
  511. }
  512. i++;
  513. path[i].p_bh = bh;
  514. path[i].p_hdr = ext_block_hdr(bh);
  515. path[i].p_idx = EXT_FIRST_INDEX(path[i].p_hdr);
  516. }
  517. ext4_set_inode_state(inode, EXT4_STATE_EXT_PRECACHED);
  518. out:
  519. up_read(&ei->i_data_sem);
  520. ext4_ext_drop_refs(path);
  521. kfree(path);
  522. return ret;
  523. }
  524. #ifdef EXT_DEBUG
  525. static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
  526. {
  527. int k, l = path->p_depth;
  528. ext_debug("path:");
  529. for (k = 0; k <= l; k++, path++) {
  530. if (path->p_idx) {
  531. ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
  532. ext4_idx_pblock(path->p_idx));
  533. } else if (path->p_ext) {
  534. ext_debug(" %d:[%d]%d:%llu ",
  535. le32_to_cpu(path->p_ext->ee_block),
  536. ext4_ext_is_uninitialized(path->p_ext),
  537. ext4_ext_get_actual_len(path->p_ext),
  538. ext4_ext_pblock(path->p_ext));
  539. } else
  540. ext_debug(" []");
  541. }
  542. ext_debug("\n");
  543. }
  544. static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
  545. {
  546. int depth = ext_depth(inode);
  547. struct ext4_extent_header *eh;
  548. struct ext4_extent *ex;
  549. int i;
  550. if (!path)
  551. return;
  552. eh = path[depth].p_hdr;
  553. ex = EXT_FIRST_EXTENT(eh);
  554. ext_debug("Displaying leaf extents for inode %lu\n", inode->i_ino);
  555. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
  556. ext_debug("%d:[%d]%d:%llu ", le32_to_cpu(ex->ee_block),
  557. ext4_ext_is_uninitialized(ex),
  558. ext4_ext_get_actual_len(ex), ext4_ext_pblock(ex));
  559. }
  560. ext_debug("\n");
  561. }
  562. static void ext4_ext_show_move(struct inode *inode, struct ext4_ext_path *path,
  563. ext4_fsblk_t newblock, int level)
  564. {
  565. int depth = ext_depth(inode);
  566. struct ext4_extent *ex;
  567. if (depth != level) {
  568. struct ext4_extent_idx *idx;
  569. idx = path[level].p_idx;
  570. while (idx <= EXT_MAX_INDEX(path[level].p_hdr)) {
  571. ext_debug("%d: move %d:%llu in new index %llu\n", level,
  572. le32_to_cpu(idx->ei_block),
  573. ext4_idx_pblock(idx),
  574. newblock);
  575. idx++;
  576. }
  577. return;
  578. }
  579. ex = path[depth].p_ext;
  580. while (ex <= EXT_MAX_EXTENT(path[depth].p_hdr)) {
  581. ext_debug("move %d:%llu:[%d]%d in new leaf %llu\n",
  582. le32_to_cpu(ex->ee_block),
  583. ext4_ext_pblock(ex),
  584. ext4_ext_is_uninitialized(ex),
  585. ext4_ext_get_actual_len(ex),
  586. newblock);
  587. ex++;
  588. }
  589. }
  590. #else
  591. #define ext4_ext_show_path(inode, path)
  592. #define ext4_ext_show_leaf(inode, path)
  593. #define ext4_ext_show_move(inode, path, newblock, level)
  594. #endif
  595. void ext4_ext_drop_refs(struct ext4_ext_path *path)
  596. {
  597. int depth = path->p_depth;
  598. int i;
  599. for (i = 0; i <= depth; i++, path++)
  600. if (path->p_bh) {
  601. brelse(path->p_bh);
  602. path->p_bh = NULL;
  603. }
  604. }
  605. /*
  606. * ext4_ext_binsearch_idx:
  607. * binary search for the closest index of the given block
  608. * the header must be checked before calling this
  609. */
  610. static void
  611. ext4_ext_binsearch_idx(struct inode *inode,
  612. struct ext4_ext_path *path, ext4_lblk_t block)
  613. {
  614. struct ext4_extent_header *eh = path->p_hdr;
  615. struct ext4_extent_idx *r, *l, *m;
  616. ext_debug("binsearch for %u(idx): ", block);
  617. l = EXT_FIRST_INDEX(eh) + 1;
  618. r = EXT_LAST_INDEX(eh);
  619. while (l <= r) {
  620. m = l + (r - l) / 2;
  621. if (block < le32_to_cpu(m->ei_block))
  622. r = m - 1;
  623. else
  624. l = m + 1;
  625. ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ei_block),
  626. m, le32_to_cpu(m->ei_block),
  627. r, le32_to_cpu(r->ei_block));
  628. }
  629. path->p_idx = l - 1;
  630. ext_debug(" -> %u->%lld ", le32_to_cpu(path->p_idx->ei_block),
  631. ext4_idx_pblock(path->p_idx));
  632. #ifdef CHECK_BINSEARCH
  633. {
  634. struct ext4_extent_idx *chix, *ix;
  635. int k;
  636. chix = ix = EXT_FIRST_INDEX(eh);
  637. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
  638. if (k != 0 &&
  639. le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
  640. printk(KERN_DEBUG "k=%d, ix=0x%p, "
  641. "first=0x%p\n", k,
  642. ix, EXT_FIRST_INDEX(eh));
  643. printk(KERN_DEBUG "%u <= %u\n",
  644. le32_to_cpu(ix->ei_block),
  645. le32_to_cpu(ix[-1].ei_block));
  646. }
  647. BUG_ON(k && le32_to_cpu(ix->ei_block)
  648. <= le32_to_cpu(ix[-1].ei_block));
  649. if (block < le32_to_cpu(ix->ei_block))
  650. break;
  651. chix = ix;
  652. }
  653. BUG_ON(chix != path->p_idx);
  654. }
  655. #endif
  656. }
  657. /*
  658. * ext4_ext_binsearch:
  659. * binary search for closest extent of the given block
  660. * the header must be checked before calling this
  661. */
  662. static void
  663. ext4_ext_binsearch(struct inode *inode,
  664. struct ext4_ext_path *path, ext4_lblk_t block)
  665. {
  666. struct ext4_extent_header *eh = path->p_hdr;
  667. struct ext4_extent *r, *l, *m;
  668. if (eh->eh_entries == 0) {
  669. /*
  670. * this leaf is empty:
  671. * we get such a leaf in split/add case
  672. */
  673. return;
  674. }
  675. ext_debug("binsearch for %u: ", block);
  676. l = EXT_FIRST_EXTENT(eh) + 1;
  677. r = EXT_LAST_EXTENT(eh);
  678. while (l <= r) {
  679. m = l + (r - l) / 2;
  680. if (block < le32_to_cpu(m->ee_block))
  681. r = m - 1;
  682. else
  683. l = m + 1;
  684. ext_debug("%p(%u):%p(%u):%p(%u) ", l, le32_to_cpu(l->ee_block),
  685. m, le32_to_cpu(m->ee_block),
  686. r, le32_to_cpu(r->ee_block));
  687. }
  688. path->p_ext = l - 1;
  689. ext_debug(" -> %d:%llu:[%d]%d ",
  690. le32_to_cpu(path->p_ext->ee_block),
  691. ext4_ext_pblock(path->p_ext),
  692. ext4_ext_is_uninitialized(path->p_ext),
  693. ext4_ext_get_actual_len(path->p_ext));
  694. #ifdef CHECK_BINSEARCH
  695. {
  696. struct ext4_extent *chex, *ex;
  697. int k;
  698. chex = ex = EXT_FIRST_EXTENT(eh);
  699. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
  700. BUG_ON(k && le32_to_cpu(ex->ee_block)
  701. <= le32_to_cpu(ex[-1].ee_block));
  702. if (block < le32_to_cpu(ex->ee_block))
  703. break;
  704. chex = ex;
  705. }
  706. BUG_ON(chex != path->p_ext);
  707. }
  708. #endif
  709. }
  710. int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
  711. {
  712. struct ext4_extent_header *eh;
  713. eh = ext_inode_hdr(inode);
  714. eh->eh_depth = 0;
  715. eh->eh_entries = 0;
  716. eh->eh_magic = EXT4_EXT_MAGIC;
  717. eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode, 0));
  718. ext4_mark_inode_dirty(handle, inode);
  719. return 0;
  720. }
  721. struct ext4_ext_path *
  722. ext4_ext_find_extent(struct inode *inode, ext4_lblk_t block,
  723. struct ext4_ext_path *path, int flags)
  724. {
  725. struct ext4_extent_header *eh;
  726. struct buffer_head *bh;
  727. short int depth, i, ppos = 0, alloc = 0;
  728. int ret;
  729. eh = ext_inode_hdr(inode);
  730. depth = ext_depth(inode);
  731. /* account possible depth increase */
  732. if (!path) {
  733. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
  734. GFP_NOFS);
  735. if (!path)
  736. return ERR_PTR(-ENOMEM);
  737. alloc = 1;
  738. }
  739. path[0].p_hdr = eh;
  740. path[0].p_bh = NULL;
  741. i = depth;
  742. /* walk through the tree */
  743. while (i) {
  744. ext_debug("depth %d: num %d, max %d\n",
  745. ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  746. ext4_ext_binsearch_idx(inode, path + ppos, block);
  747. path[ppos].p_block = ext4_idx_pblock(path[ppos].p_idx);
  748. path[ppos].p_depth = i;
  749. path[ppos].p_ext = NULL;
  750. bh = read_extent_tree_block(inode, path[ppos].p_block, --i,
  751. flags);
  752. if (IS_ERR(bh)) {
  753. ret = PTR_ERR(bh);
  754. goto err;
  755. }
  756. eh = ext_block_hdr(bh);
  757. ppos++;
  758. if (unlikely(ppos > depth)) {
  759. put_bh(bh);
  760. EXT4_ERROR_INODE(inode,
  761. "ppos %d > depth %d", ppos, depth);
  762. ret = -EIO;
  763. goto err;
  764. }
  765. path[ppos].p_bh = bh;
  766. path[ppos].p_hdr = eh;
  767. }
  768. path[ppos].p_depth = i;
  769. path[ppos].p_ext = NULL;
  770. path[ppos].p_idx = NULL;
  771. /* find extent */
  772. ext4_ext_binsearch(inode, path + ppos, block);
  773. /* if not an empty leaf */
  774. if (path[ppos].p_ext)
  775. path[ppos].p_block = ext4_ext_pblock(path[ppos].p_ext);
  776. ext4_ext_show_path(inode, path);
  777. return path;
  778. err:
  779. ext4_ext_drop_refs(path);
  780. if (alloc)
  781. kfree(path);
  782. return ERR_PTR(ret);
  783. }
  784. /*
  785. * ext4_ext_insert_index:
  786. * insert new index [@logical;@ptr] into the block at @curp;
  787. * check where to insert: before @curp or after @curp
  788. */
  789. static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
  790. struct ext4_ext_path *curp,
  791. int logical, ext4_fsblk_t ptr)
  792. {
  793. struct ext4_extent_idx *ix;
  794. int len, err;
  795. err = ext4_ext_get_access(handle, inode, curp);
  796. if (err)
  797. return err;
  798. if (unlikely(logical == le32_to_cpu(curp->p_idx->ei_block))) {
  799. EXT4_ERROR_INODE(inode,
  800. "logical %d == ei_block %d!",
  801. logical, le32_to_cpu(curp->p_idx->ei_block));
  802. return -EIO;
  803. }
  804. if (unlikely(le16_to_cpu(curp->p_hdr->eh_entries)
  805. >= le16_to_cpu(curp->p_hdr->eh_max))) {
  806. EXT4_ERROR_INODE(inode,
  807. "eh_entries %d >= eh_max %d!",
  808. le16_to_cpu(curp->p_hdr->eh_entries),
  809. le16_to_cpu(curp->p_hdr->eh_max));
  810. return -EIO;
  811. }
  812. if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
  813. /* insert after */
  814. ext_debug("insert new index %d after: %llu\n", logical, ptr);
  815. ix = curp->p_idx + 1;
  816. } else {
  817. /* insert before */
  818. ext_debug("insert new index %d before: %llu\n", logical, ptr);
  819. ix = curp->p_idx;
  820. }
  821. len = EXT_LAST_INDEX(curp->p_hdr) - ix + 1;
  822. BUG_ON(len < 0);
  823. if (len > 0) {
  824. ext_debug("insert new index %d: "
  825. "move %d indices from 0x%p to 0x%p\n",
  826. logical, len, ix, ix + 1);
  827. memmove(ix + 1, ix, len * sizeof(struct ext4_extent_idx));
  828. }
  829. if (unlikely(ix > EXT_MAX_INDEX(curp->p_hdr))) {
  830. EXT4_ERROR_INODE(inode, "ix > EXT_MAX_INDEX!");
  831. return -EIO;
  832. }
  833. ix->ei_block = cpu_to_le32(logical);
  834. ext4_idx_store_pblock(ix, ptr);
  835. le16_add_cpu(&curp->p_hdr->eh_entries, 1);
  836. if (unlikely(ix > EXT_LAST_INDEX(curp->p_hdr))) {
  837. EXT4_ERROR_INODE(inode, "ix > EXT_LAST_INDEX!");
  838. return -EIO;
  839. }
  840. err = ext4_ext_dirty(handle, inode, curp);
  841. ext4_std_error(inode->i_sb, err);
  842. return err;
  843. }
  844. /*
  845. * ext4_ext_split:
  846. * inserts new subtree into the path, using free index entry
  847. * at depth @at:
  848. * - allocates all needed blocks (new leaf and all intermediate index blocks)
  849. * - makes decision where to split
  850. * - moves remaining extents and index entries (right to the split point)
  851. * into the newly allocated blocks
  852. * - initializes subtree
  853. */
  854. static int ext4_ext_split(handle_t *handle, struct inode *inode,
  855. unsigned int flags,
  856. struct ext4_ext_path *path,
  857. struct ext4_extent *newext, int at)
  858. {
  859. struct buffer_head *bh = NULL;
  860. int depth = ext_depth(inode);
  861. struct ext4_extent_header *neh;
  862. struct ext4_extent_idx *fidx;
  863. int i = at, k, m, a;
  864. ext4_fsblk_t newblock, oldblock;
  865. __le32 border;
  866. ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
  867. int err = 0;
  868. /* make decision: where to split? */
  869. /* FIXME: now decision is simplest: at current extent */
  870. /* if current leaf will be split, then we should use
  871. * border from split point */
  872. if (unlikely(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr))) {
  873. EXT4_ERROR_INODE(inode, "p_ext > EXT_MAX_EXTENT!");
  874. return -EIO;
  875. }
  876. if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
  877. border = path[depth].p_ext[1].ee_block;
  878. ext_debug("leaf will be split."
  879. " next leaf starts at %d\n",
  880. le32_to_cpu(border));
  881. } else {
  882. border = newext->ee_block;
  883. ext_debug("leaf will be added."
  884. " next leaf starts at %d\n",
  885. le32_to_cpu(border));
  886. }
  887. /*
  888. * If error occurs, then we break processing
  889. * and mark filesystem read-only. index won't
  890. * be inserted and tree will be in consistent
  891. * state. Next mount will repair buffers too.
  892. */
  893. /*
  894. * Get array to track all allocated blocks.
  895. * We need this to handle errors and free blocks
  896. * upon them.
  897. */
  898. ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
  899. if (!ablocks)
  900. return -ENOMEM;
  901. /* allocate all needed blocks */
  902. ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
  903. for (a = 0; a < depth - at; a++) {
  904. newblock = ext4_ext_new_meta_block(handle, inode, path,
  905. newext, &err, flags);
  906. if (newblock == 0)
  907. goto cleanup;
  908. ablocks[a] = newblock;
  909. }
  910. /* initialize new leaf */
  911. newblock = ablocks[--a];
  912. if (unlikely(newblock == 0)) {
  913. EXT4_ERROR_INODE(inode, "newblock == 0!");
  914. err = -EIO;
  915. goto cleanup;
  916. }
  917. bh = sb_getblk(inode->i_sb, newblock);
  918. if (unlikely(!bh)) {
  919. err = -ENOMEM;
  920. goto cleanup;
  921. }
  922. lock_buffer(bh);
  923. err = ext4_journal_get_create_access(handle, bh);
  924. if (err)
  925. goto cleanup;
  926. neh = ext_block_hdr(bh);
  927. neh->eh_entries = 0;
  928. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
  929. neh->eh_magic = EXT4_EXT_MAGIC;
  930. neh->eh_depth = 0;
  931. /* move remainder of path[depth] to the new leaf */
  932. if (unlikely(path[depth].p_hdr->eh_entries !=
  933. path[depth].p_hdr->eh_max)) {
  934. EXT4_ERROR_INODE(inode, "eh_entries %d != eh_max %d!",
  935. path[depth].p_hdr->eh_entries,
  936. path[depth].p_hdr->eh_max);
  937. err = -EIO;
  938. goto cleanup;
  939. }
  940. /* start copy from next extent */
  941. m = EXT_MAX_EXTENT(path[depth].p_hdr) - path[depth].p_ext++;
  942. ext4_ext_show_move(inode, path, newblock, depth);
  943. if (m) {
  944. struct ext4_extent *ex;
  945. ex = EXT_FIRST_EXTENT(neh);
  946. memmove(ex, path[depth].p_ext, sizeof(struct ext4_extent) * m);
  947. le16_add_cpu(&neh->eh_entries, m);
  948. }
  949. ext4_extent_block_csum_set(inode, neh);
  950. set_buffer_uptodate(bh);
  951. unlock_buffer(bh);
  952. err = ext4_handle_dirty_metadata(handle, inode, bh);
  953. if (err)
  954. goto cleanup;
  955. brelse(bh);
  956. bh = NULL;
  957. /* correct old leaf */
  958. if (m) {
  959. err = ext4_ext_get_access(handle, inode, path + depth);
  960. if (err)
  961. goto cleanup;
  962. le16_add_cpu(&path[depth].p_hdr->eh_entries, -m);
  963. err = ext4_ext_dirty(handle, inode, path + depth);
  964. if (err)
  965. goto cleanup;
  966. }
  967. /* create intermediate indexes */
  968. k = depth - at - 1;
  969. if (unlikely(k < 0)) {
  970. EXT4_ERROR_INODE(inode, "k %d < 0!", k);
  971. err = -EIO;
  972. goto cleanup;
  973. }
  974. if (k)
  975. ext_debug("create %d intermediate indices\n", k);
  976. /* insert new index into current index block */
  977. /* current depth stored in i var */
  978. i = depth - 1;
  979. while (k--) {
  980. oldblock = newblock;
  981. newblock = ablocks[--a];
  982. bh = sb_getblk(inode->i_sb, newblock);
  983. if (unlikely(!bh)) {
  984. err = -ENOMEM;
  985. goto cleanup;
  986. }
  987. lock_buffer(bh);
  988. err = ext4_journal_get_create_access(handle, bh);
  989. if (err)
  990. goto cleanup;
  991. neh = ext_block_hdr(bh);
  992. neh->eh_entries = cpu_to_le16(1);
  993. neh->eh_magic = EXT4_EXT_MAGIC;
  994. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
  995. neh->eh_depth = cpu_to_le16(depth - i);
  996. fidx = EXT_FIRST_INDEX(neh);
  997. fidx->ei_block = border;
  998. ext4_idx_store_pblock(fidx, oldblock);
  999. ext_debug("int.index at %d (block %llu): %u -> %llu\n",
  1000. i, newblock, le32_to_cpu(border), oldblock);
  1001. /* move remainder of path[i] to the new index block */
  1002. if (unlikely(EXT_MAX_INDEX(path[i].p_hdr) !=
  1003. EXT_LAST_INDEX(path[i].p_hdr))) {
  1004. EXT4_ERROR_INODE(inode,
  1005. "EXT_MAX_INDEX != EXT_LAST_INDEX ee_block %d!",
  1006. le32_to_cpu(path[i].p_ext->ee_block));
  1007. err = -EIO;
  1008. goto cleanup;
  1009. }
  1010. /* start copy indexes */
  1011. m = EXT_MAX_INDEX(path[i].p_hdr) - path[i].p_idx++;
  1012. ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
  1013. EXT_MAX_INDEX(path[i].p_hdr));
  1014. ext4_ext_show_move(inode, path, newblock, i);
  1015. if (m) {
  1016. memmove(++fidx, path[i].p_idx,
  1017. sizeof(struct ext4_extent_idx) * m);
  1018. le16_add_cpu(&neh->eh_entries, m);
  1019. }
  1020. ext4_extent_block_csum_set(inode, neh);
  1021. set_buffer_uptodate(bh);
  1022. unlock_buffer(bh);
  1023. err = ext4_handle_dirty_metadata(handle, inode, bh);
  1024. if (err)
  1025. goto cleanup;
  1026. brelse(bh);
  1027. bh = NULL;
  1028. /* correct old index */
  1029. if (m) {
  1030. err = ext4_ext_get_access(handle, inode, path + i);
  1031. if (err)
  1032. goto cleanup;
  1033. le16_add_cpu(&path[i].p_hdr->eh_entries, -m);
  1034. err = ext4_ext_dirty(handle, inode, path + i);
  1035. if (err)
  1036. goto cleanup;
  1037. }
  1038. i--;
  1039. }
  1040. /* insert new index */
  1041. err = ext4_ext_insert_index(handle, inode, path + at,
  1042. le32_to_cpu(border), newblock);
  1043. cleanup:
  1044. if (bh) {
  1045. if (buffer_locked(bh))
  1046. unlock_buffer(bh);
  1047. brelse(bh);
  1048. }
  1049. if (err) {
  1050. /* free all allocated blocks in error case */
  1051. for (i = 0; i < depth; i++) {
  1052. if (!ablocks[i])
  1053. continue;
  1054. ext4_free_blocks(handle, inode, NULL, ablocks[i], 1,
  1055. EXT4_FREE_BLOCKS_METADATA);
  1056. }
  1057. }
  1058. kfree(ablocks);
  1059. return err;
  1060. }
  1061. /*
  1062. * ext4_ext_grow_indepth:
  1063. * implements tree growing procedure:
  1064. * - allocates new block
  1065. * - moves top-level data (index block or leaf) into the new block
  1066. * - initializes new top-level, creating index that points to the
  1067. * just created block
  1068. */
  1069. static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
  1070. unsigned int flags,
  1071. struct ext4_extent *newext)
  1072. {
  1073. struct ext4_extent_header *neh;
  1074. struct buffer_head *bh;
  1075. ext4_fsblk_t newblock;
  1076. int err = 0;
  1077. newblock = ext4_ext_new_meta_block(handle, inode, NULL,
  1078. newext, &err, flags);
  1079. if (newblock == 0)
  1080. return err;
  1081. bh = sb_getblk(inode->i_sb, newblock);
  1082. if (unlikely(!bh))
  1083. return -ENOMEM;
  1084. lock_buffer(bh);
  1085. err = ext4_journal_get_create_access(handle, bh);
  1086. if (err) {
  1087. unlock_buffer(bh);
  1088. goto out;
  1089. }
  1090. /* move top-level index/leaf into new block */
  1091. memmove(bh->b_data, EXT4_I(inode)->i_data,
  1092. sizeof(EXT4_I(inode)->i_data));
  1093. /* set size of new block */
  1094. neh = ext_block_hdr(bh);
  1095. /* old root could have indexes or leaves
  1096. * so calculate e_max right way */
  1097. if (ext_depth(inode))
  1098. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode, 0));
  1099. else
  1100. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode, 0));
  1101. neh->eh_magic = EXT4_EXT_MAGIC;
  1102. ext4_extent_block_csum_set(inode, neh);
  1103. set_buffer_uptodate(bh);
  1104. unlock_buffer(bh);
  1105. err = ext4_handle_dirty_metadata(handle, inode, bh);
  1106. if (err)
  1107. goto out;
  1108. /* Update top-level index: num,max,pointer */
  1109. neh = ext_inode_hdr(inode);
  1110. neh->eh_entries = cpu_to_le16(1);
  1111. ext4_idx_store_pblock(EXT_FIRST_INDEX(neh), newblock);
  1112. if (neh->eh_depth == 0) {
  1113. /* Root extent block becomes index block */
  1114. neh->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode, 0));
  1115. EXT_FIRST_INDEX(neh)->ei_block =
  1116. EXT_FIRST_EXTENT(neh)->ee_block;
  1117. }
  1118. ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
  1119. le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
  1120. le32_to_cpu(EXT_FIRST_INDEX(neh)->ei_block),
  1121. ext4_idx_pblock(EXT_FIRST_INDEX(neh)));
  1122. le16_add_cpu(&neh->eh_depth, 1);
  1123. ext4_mark_inode_dirty(handle, inode);
  1124. out:
  1125. brelse(bh);
  1126. return err;
  1127. }
  1128. /*
  1129. * ext4_ext_create_new_leaf:
  1130. * finds empty index and adds new leaf.
  1131. * if no free index is found, then it requests in-depth growing.
  1132. */
  1133. static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
  1134. unsigned int mb_flags,
  1135. unsigned int gb_flags,
  1136. struct ext4_ext_path *path,
  1137. struct ext4_extent *newext)
  1138. {
  1139. struct ext4_ext_path *curp;
  1140. int depth, i, err = 0;
  1141. repeat:
  1142. i = depth = ext_depth(inode);
  1143. /* walk up to the tree and look for free index entry */
  1144. curp = path + depth;
  1145. while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
  1146. i--;
  1147. curp--;
  1148. }
  1149. /* we use already allocated block for index block,
  1150. * so subsequent data blocks should be contiguous */
  1151. if (EXT_HAS_FREE_INDEX(curp)) {
  1152. /* if we found index with free entry, then use that
  1153. * entry: create all needed subtree and add new leaf */
  1154. err = ext4_ext_split(handle, inode, mb_flags, path, newext, i);
  1155. if (err)
  1156. goto out;
  1157. /* refill path */
  1158. ext4_ext_drop_refs(path);
  1159. path = ext4_ext_find_extent(inode,
  1160. (ext4_lblk_t)le32_to_cpu(newext->ee_block),
  1161. path, gb_flags);
  1162. if (IS_ERR(path))
  1163. err = PTR_ERR(path);
  1164. } else {
  1165. /* tree is full, time to grow in depth */
  1166. err = ext4_ext_grow_indepth(handle, inode, mb_flags, newext);
  1167. if (err)
  1168. goto out;
  1169. /* refill path */
  1170. ext4_ext_drop_refs(path);
  1171. path = ext4_ext_find_extent(inode,
  1172. (ext4_lblk_t)le32_to_cpu(newext->ee_block),
  1173. path, gb_flags);
  1174. if (IS_ERR(path)) {
  1175. err = PTR_ERR(path);
  1176. goto out;
  1177. }
  1178. /*
  1179. * only first (depth 0 -> 1) produces free space;
  1180. * in all other cases we have to split the grown tree
  1181. */
  1182. depth = ext_depth(inode);
  1183. if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
  1184. /* now we need to split */
  1185. goto repeat;
  1186. }
  1187. }
  1188. out:
  1189. return err;
  1190. }
  1191. /*
  1192. * search the closest allocated block to the left for *logical
  1193. * and returns it at @logical + it's physical address at @phys
  1194. * if *logical is the smallest allocated block, the function
  1195. * returns 0 at @phys
  1196. * return value contains 0 (success) or error code
  1197. */
  1198. static int ext4_ext_search_left(struct inode *inode,
  1199. struct ext4_ext_path *path,
  1200. ext4_lblk_t *logical, ext4_fsblk_t *phys)
  1201. {
  1202. struct ext4_extent_idx *ix;
  1203. struct ext4_extent *ex;
  1204. int depth, ee_len;
  1205. if (unlikely(path == NULL)) {
  1206. EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
  1207. return -EIO;
  1208. }
  1209. depth = path->p_depth;
  1210. *phys = 0;
  1211. if (depth == 0 && path->p_ext == NULL)
  1212. return 0;
  1213. /* usually extent in the path covers blocks smaller
  1214. * then *logical, but it can be that extent is the
  1215. * first one in the file */
  1216. ex = path[depth].p_ext;
  1217. ee_len = ext4_ext_get_actual_len(ex);
  1218. if (*logical < le32_to_cpu(ex->ee_block)) {
  1219. if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
  1220. EXT4_ERROR_INODE(inode,
  1221. "EXT_FIRST_EXTENT != ex *logical %d ee_block %d!",
  1222. *logical, le32_to_cpu(ex->ee_block));
  1223. return -EIO;
  1224. }
  1225. while (--depth >= 0) {
  1226. ix = path[depth].p_idx;
  1227. if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
  1228. EXT4_ERROR_INODE(inode,
  1229. "ix (%d) != EXT_FIRST_INDEX (%d) (depth %d)!",
  1230. ix != NULL ? le32_to_cpu(ix->ei_block) : 0,
  1231. EXT_FIRST_INDEX(path[depth].p_hdr) != NULL ?
  1232. le32_to_cpu(EXT_FIRST_INDEX(path[depth].p_hdr)->ei_block) : 0,
  1233. depth);
  1234. return -EIO;
  1235. }
  1236. }
  1237. return 0;
  1238. }
  1239. if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
  1240. EXT4_ERROR_INODE(inode,
  1241. "logical %d < ee_block %d + ee_len %d!",
  1242. *logical, le32_to_cpu(ex->ee_block), ee_len);
  1243. return -EIO;
  1244. }
  1245. *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
  1246. *phys = ext4_ext_pblock(ex) + ee_len - 1;
  1247. return 0;
  1248. }
  1249. /*
  1250. * search the closest allocated block to the right for *logical
  1251. * and returns it at @logical + it's physical address at @phys
  1252. * if *logical is the largest allocated block, the function
  1253. * returns 0 at @phys
  1254. * return value contains 0 (success) or error code
  1255. */
  1256. static int ext4_ext_search_right(struct inode *inode,
  1257. struct ext4_ext_path *path,
  1258. ext4_lblk_t *logical, ext4_fsblk_t *phys,
  1259. struct ext4_extent **ret_ex)
  1260. {
  1261. struct buffer_head *bh = NULL;
  1262. struct ext4_extent_header *eh;
  1263. struct ext4_extent_idx *ix;
  1264. struct ext4_extent *ex;
  1265. ext4_fsblk_t block;
  1266. int depth; /* Note, NOT eh_depth; depth from top of tree */
  1267. int ee_len;
  1268. if (unlikely(path == NULL)) {
  1269. EXT4_ERROR_INODE(inode, "path == NULL *logical %d!", *logical);
  1270. return -EIO;
  1271. }
  1272. depth = path->p_depth;
  1273. *phys = 0;
  1274. if (depth == 0 && path->p_ext == NULL)
  1275. return 0;
  1276. /* usually extent in the path covers blocks smaller
  1277. * then *logical, but it can be that extent is the
  1278. * first one in the file */
  1279. ex = path[depth].p_ext;
  1280. ee_len = ext4_ext_get_actual_len(ex);
  1281. if (*logical < le32_to_cpu(ex->ee_block)) {
  1282. if (unlikely(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex)) {
  1283. EXT4_ERROR_INODE(inode,
  1284. "first_extent(path[%d].p_hdr) != ex",
  1285. depth);
  1286. return -EIO;
  1287. }
  1288. while (--depth >= 0) {
  1289. ix = path[depth].p_idx;
  1290. if (unlikely(ix != EXT_FIRST_INDEX(path[depth].p_hdr))) {
  1291. EXT4_ERROR_INODE(inode,
  1292. "ix != EXT_FIRST_INDEX *logical %d!",
  1293. *logical);
  1294. return -EIO;
  1295. }
  1296. }
  1297. goto found_extent;
  1298. }
  1299. if (unlikely(*logical < (le32_to_cpu(ex->ee_block) + ee_len))) {
  1300. EXT4_ERROR_INODE(inode,
  1301. "logical %d < ee_block %d + ee_len %d!",
  1302. *logical, le32_to_cpu(ex->ee_block), ee_len);
  1303. return -EIO;
  1304. }
  1305. if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
  1306. /* next allocated block in this leaf */
  1307. ex++;
  1308. goto found_extent;
  1309. }
  1310. /* go up and search for index to the right */
  1311. while (--depth >= 0) {
  1312. ix = path[depth].p_idx;
  1313. if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
  1314. goto got_index;
  1315. }
  1316. /* we've gone up to the root and found no index to the right */
  1317. return 0;
  1318. got_index:
  1319. /* we've found index to the right, let's
  1320. * follow it and find the closest allocated
  1321. * block to the right */
  1322. ix++;
  1323. block = ext4_idx_pblock(ix);
  1324. while (++depth < path->p_depth) {
  1325. /* subtract from p_depth to get proper eh_depth */
  1326. bh = read_extent_tree_block(inode, block,
  1327. path->p_depth - depth, 0);
  1328. if (IS_ERR(bh))
  1329. return PTR_ERR(bh);
  1330. eh = ext_block_hdr(bh);
  1331. ix = EXT_FIRST_INDEX(eh);
  1332. block = ext4_idx_pblock(ix);
  1333. put_bh(bh);
  1334. }
  1335. bh = read_extent_tree_block(inode, block, path->p_depth - depth, 0);
  1336. if (IS_ERR(bh))
  1337. return PTR_ERR(bh);
  1338. eh = ext_block_hdr(bh);
  1339. ex = EXT_FIRST_EXTENT(eh);
  1340. found_extent:
  1341. *logical = le32_to_cpu(ex->ee_block);
  1342. *phys = ext4_ext_pblock(ex);
  1343. *ret_ex = ex;
  1344. if (bh)
  1345. put_bh(bh);
  1346. return 0;
  1347. }
  1348. /*
  1349. * ext4_ext_next_allocated_block:
  1350. * returns allocated block in subsequent extent or EXT_MAX_BLOCKS.
  1351. * NOTE: it considers block number from index entry as
  1352. * allocated block. Thus, index entries have to be consistent
  1353. * with leaves.
  1354. */
  1355. static ext4_lblk_t
  1356. ext4_ext_next_allocated_block(struct ext4_ext_path *path)
  1357. {
  1358. int depth;
  1359. BUG_ON(path == NULL);
  1360. depth = path->p_depth;
  1361. if (depth == 0 && path->p_ext == NULL)
  1362. return EXT_MAX_BLOCKS;
  1363. while (depth >= 0) {
  1364. if (depth == path->p_depth) {
  1365. /* leaf */
  1366. if (path[depth].p_ext &&
  1367. path[depth].p_ext !=
  1368. EXT_LAST_EXTENT(path[depth].p_hdr))
  1369. return le32_to_cpu(path[depth].p_ext[1].ee_block);
  1370. } else {
  1371. /* index */
  1372. if (path[depth].p_idx !=
  1373. EXT_LAST_INDEX(path[depth].p_hdr))
  1374. return le32_to_cpu(path[depth].p_idx[1].ei_block);
  1375. }
  1376. depth--;
  1377. }
  1378. return EXT_MAX_BLOCKS;
  1379. }
  1380. /*
  1381. * ext4_ext_next_leaf_block:
  1382. * returns first allocated block from next leaf or EXT_MAX_BLOCKS
  1383. */
  1384. static ext4_lblk_t ext4_ext_next_leaf_block(struct ext4_ext_path *path)
  1385. {
  1386. int depth;
  1387. BUG_ON(path == NULL);
  1388. depth = path->p_depth;
  1389. /* zero-tree has no leaf blocks at all */
  1390. if (depth == 0)
  1391. return EXT_MAX_BLOCKS;
  1392. /* go to index block */
  1393. depth--;
  1394. while (depth >= 0) {
  1395. if (path[depth].p_idx !=
  1396. EXT_LAST_INDEX(path[depth].p_hdr))
  1397. return (ext4_lblk_t)
  1398. le32_to_cpu(path[depth].p_idx[1].ei_block);
  1399. depth--;
  1400. }
  1401. return EXT_MAX_BLOCKS;
  1402. }
  1403. /*
  1404. * ext4_ext_correct_indexes:
  1405. * if leaf gets modified and modified extent is first in the leaf,
  1406. * then we have to correct all indexes above.
  1407. * TODO: do we need to correct tree in all cases?
  1408. */
  1409. static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
  1410. struct ext4_ext_path *path)
  1411. {
  1412. struct ext4_extent_header *eh;
  1413. int depth = ext_depth(inode);
  1414. struct ext4_extent *ex;
  1415. __le32 border;
  1416. int k, err = 0;
  1417. eh = path[depth].p_hdr;
  1418. ex = path[depth].p_ext;
  1419. if (unlikely(ex == NULL || eh == NULL)) {
  1420. EXT4_ERROR_INODE(inode,
  1421. "ex %p == NULL or eh %p == NULL", ex, eh);
  1422. return -EIO;
  1423. }
  1424. if (depth == 0) {
  1425. /* there is no tree at all */
  1426. return 0;
  1427. }
  1428. if (ex != EXT_FIRST_EXTENT(eh)) {
  1429. /* we correct tree if first leaf got modified only */
  1430. return 0;
  1431. }
  1432. /*
  1433. * TODO: we need correction if border is smaller than current one
  1434. */
  1435. k = depth - 1;
  1436. border = path[depth].p_ext->ee_block;
  1437. err = ext4_ext_get_access(handle, inode, path + k);
  1438. if (err)
  1439. return err;
  1440. path[k].p_idx->ei_block = border;
  1441. err = ext4_ext_dirty(handle, inode, path + k);
  1442. if (err)
  1443. return err;
  1444. while (k--) {
  1445. /* change all left-side indexes */
  1446. if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
  1447. break;
  1448. err = ext4_ext_get_access(handle, inode, path + k);
  1449. if (err)
  1450. break;
  1451. path[k].p_idx->ei_block = border;
  1452. err = ext4_ext_dirty(handle, inode, path + k);
  1453. if (err)
  1454. break;
  1455. }
  1456. return err;
  1457. }
  1458. int
  1459. ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
  1460. struct ext4_extent *ex2)
  1461. {
  1462. unsigned short ext1_ee_len, ext2_ee_len, max_len;
  1463. /*
  1464. * Make sure that both extents are initialized. We don't merge
  1465. * uninitialized extents so that we can be sure that end_io code has
  1466. * the extent that was written properly split out and conversion to
  1467. * initialized is trivial.
  1468. */
  1469. if (ext4_ext_is_uninitialized(ex1) || ext4_ext_is_uninitialized(ex2))
  1470. return 0;
  1471. if (ext4_ext_is_uninitialized(ex1))
  1472. max_len = EXT_UNINIT_MAX_LEN;
  1473. else
  1474. max_len = EXT_INIT_MAX_LEN;
  1475. ext1_ee_len = ext4_ext_get_actual_len(ex1);
  1476. ext2_ee_len = ext4_ext_get_actual_len(ex2);
  1477. if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
  1478. le32_to_cpu(ex2->ee_block))
  1479. return 0;
  1480. /*
  1481. * To allow future support for preallocated extents to be added
  1482. * as an RO_COMPAT feature, refuse to merge to extents if
  1483. * this can result in the top bit of ee_len being set.
  1484. */
  1485. if (ext1_ee_len + ext2_ee_len > max_len)
  1486. return 0;
  1487. #ifdef AGGRESSIVE_TEST
  1488. if (ext1_ee_len >= 4)
  1489. return 0;
  1490. #endif
  1491. if (ext4_ext_pblock(ex1) + ext1_ee_len == ext4_ext_pblock(ex2))
  1492. return 1;
  1493. return 0;
  1494. }
  1495. /*
  1496. * This function tries to merge the "ex" extent to the next extent in the tree.
  1497. * It always tries to merge towards right. If you want to merge towards
  1498. * left, pass "ex - 1" as argument instead of "ex".
  1499. * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
  1500. * 1 if they got merged.
  1501. */
  1502. static int ext4_ext_try_to_merge_right(struct inode *inode,
  1503. struct ext4_ext_path *path,
  1504. struct ext4_extent *ex)
  1505. {
  1506. struct ext4_extent_header *eh;
  1507. unsigned int depth, len;
  1508. int merge_done = 0;
  1509. int uninitialized = 0;
  1510. depth = ext_depth(inode);
  1511. BUG_ON(path[depth].p_hdr == NULL);
  1512. eh = path[depth].p_hdr;
  1513. while (ex < EXT_LAST_EXTENT(eh)) {
  1514. if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
  1515. break;
  1516. /* merge with next extent! */
  1517. if (ext4_ext_is_uninitialized(ex))
  1518. uninitialized = 1;
  1519. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1520. + ext4_ext_get_actual_len(ex + 1));
  1521. if (uninitialized)
  1522. ext4_ext_mark_uninitialized(ex);
  1523. if (ex + 1 < EXT_LAST_EXTENT(eh)) {
  1524. len = (EXT_LAST_EXTENT(eh) - ex - 1)
  1525. * sizeof(struct ext4_extent);
  1526. memmove(ex + 1, ex + 2, len);
  1527. }
  1528. le16_add_cpu(&eh->eh_entries, -1);
  1529. merge_done = 1;
  1530. WARN_ON(eh->eh_entries == 0);
  1531. if (!eh->eh_entries)
  1532. EXT4_ERROR_INODE(inode, "eh->eh_entries = 0!");
  1533. }
  1534. return merge_done;
  1535. }
  1536. /*
  1537. * This function does a very simple check to see if we can collapse
  1538. * an extent tree with a single extent tree leaf block into the inode.
  1539. */
  1540. static void ext4_ext_try_to_merge_up(handle_t *handle,
  1541. struct inode *inode,
  1542. struct ext4_ext_path *path)
  1543. {
  1544. size_t s;
  1545. unsigned max_root = ext4_ext_space_root(inode, 0);
  1546. ext4_fsblk_t blk;
  1547. if ((path[0].p_depth != 1) ||
  1548. (le16_to_cpu(path[0].p_hdr->eh_entries) != 1) ||
  1549. (le16_to_cpu(path[1].p_hdr->eh_entries) > max_root))
  1550. return;
  1551. /*
  1552. * We need to modify the block allocation bitmap and the block
  1553. * group descriptor to release the extent tree block. If we
  1554. * can't get the journal credits, give up.
  1555. */
  1556. if (ext4_journal_extend(handle, 2))
  1557. return;
  1558. /*
  1559. * Copy the extent data up to the inode
  1560. */
  1561. blk = ext4_idx_pblock(path[0].p_idx);
  1562. s = le16_to_cpu(path[1].p_hdr->eh_entries) *
  1563. sizeof(struct ext4_extent_idx);
  1564. s += sizeof(struct ext4_extent_header);
  1565. memcpy(path[0].p_hdr, path[1].p_hdr, s);
  1566. path[0].p_depth = 0;
  1567. path[0].p_ext = EXT_FIRST_EXTENT(path[0].p_hdr) +
  1568. (path[1].p_ext - EXT_FIRST_EXTENT(path[1].p_hdr));
  1569. path[0].p_hdr->eh_max = cpu_to_le16(max_root);
  1570. brelse(path[1].p_bh);
  1571. ext4_free_blocks(handle, inode, NULL, blk, 1,
  1572. EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET |
  1573. EXT4_FREE_BLOCKS_RESERVE);
  1574. }
  1575. /*
  1576. * This function tries to merge the @ex extent to neighbours in the tree.
  1577. * return 1 if merge left else 0.
  1578. */
  1579. static void ext4_ext_try_to_merge(handle_t *handle,
  1580. struct inode *inode,
  1581. struct ext4_ext_path *path,
  1582. struct ext4_extent *ex) {
  1583. struct ext4_extent_header *eh;
  1584. unsigned int depth;
  1585. int merge_done = 0;
  1586. depth = ext_depth(inode);
  1587. BUG_ON(path[depth].p_hdr == NULL);
  1588. eh = path[depth].p_hdr;
  1589. if (ex > EXT_FIRST_EXTENT(eh))
  1590. merge_done = ext4_ext_try_to_merge_right(inode, path, ex - 1);
  1591. if (!merge_done)
  1592. (void) ext4_ext_try_to_merge_right(inode, path, ex);
  1593. ext4_ext_try_to_merge_up(handle, inode, path);
  1594. }
  1595. /*
  1596. * check if a portion of the "newext" extent overlaps with an
  1597. * existing extent.
  1598. *
  1599. * If there is an overlap discovered, it updates the length of the newext
  1600. * such that there will be no overlap, and then returns 1.
  1601. * If there is no overlap found, it returns 0.
  1602. */
  1603. static unsigned int ext4_ext_check_overlap(struct ext4_sb_info *sbi,
  1604. struct inode *inode,
  1605. struct ext4_extent *newext,
  1606. struct ext4_ext_path *path)
  1607. {
  1608. ext4_lblk_t b1, b2;
  1609. unsigned int depth, len1;
  1610. unsigned int ret = 0;
  1611. b1 = le32_to_cpu(newext->ee_block);
  1612. len1 = ext4_ext_get_actual_len(newext);
  1613. depth = ext_depth(inode);
  1614. if (!path[depth].p_ext)
  1615. goto out;
  1616. b2 = le32_to_cpu(path[depth].p_ext->ee_block);
  1617. b2 &= ~(sbi->s_cluster_ratio - 1);
  1618. /*
  1619. * get the next allocated block if the extent in the path
  1620. * is before the requested block(s)
  1621. */
  1622. if (b2 < b1) {
  1623. b2 = ext4_ext_next_allocated_block(path);
  1624. if (b2 == EXT_MAX_BLOCKS)
  1625. goto out;
  1626. b2 &= ~(sbi->s_cluster_ratio - 1);
  1627. }
  1628. /* check for wrap through zero on extent logical start block*/
  1629. if (b1 + len1 < b1) {
  1630. len1 = EXT_MAX_BLOCKS - b1;
  1631. newext->ee_len = cpu_to_le16(len1);
  1632. ret = 1;
  1633. }
  1634. /* check for overlap */
  1635. if (b1 + len1 > b2) {
  1636. newext->ee_len = cpu_to_le16(b2 - b1);
  1637. ret = 1;
  1638. }
  1639. out:
  1640. return ret;
  1641. }
  1642. /*
  1643. * ext4_ext_insert_extent:
  1644. * tries to merge requsted extent into the existing extent or
  1645. * inserts requested extent as new one into the tree,
  1646. * creating new leaf in the no-space case.
  1647. */
  1648. int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
  1649. struct ext4_ext_path *path,
  1650. struct ext4_extent *newext, int gb_flags)
  1651. {
  1652. struct ext4_extent_header *eh;
  1653. struct ext4_extent *ex, *fex;
  1654. struct ext4_extent *nearex; /* nearest extent */
  1655. struct ext4_ext_path *npath = NULL;
  1656. int depth, len, err;
  1657. ext4_lblk_t next;
  1658. unsigned uninitialized = 0;
  1659. int mb_flags = 0;
  1660. if (unlikely(ext4_ext_get_actual_len(newext) == 0)) {
  1661. EXT4_ERROR_INODE(inode, "ext4_ext_get_actual_len(newext) == 0");
  1662. return -EIO;
  1663. }
  1664. depth = ext_depth(inode);
  1665. ex = path[depth].p_ext;
  1666. eh = path[depth].p_hdr;
  1667. if (unlikely(path[depth].p_hdr == NULL)) {
  1668. EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
  1669. return -EIO;
  1670. }
  1671. /* try to insert block into found extent and return */
  1672. if (ex && !(gb_flags & EXT4_GET_BLOCKS_PRE_IO)) {
  1673. /*
  1674. * Try to see whether we should rather test the extent on
  1675. * right from ex, or from the left of ex. This is because
  1676. * ext4_ext_find_extent() can return either extent on the
  1677. * left, or on the right from the searched position. This
  1678. * will make merging more effective.
  1679. */
  1680. if (ex < EXT_LAST_EXTENT(eh) &&
  1681. (le32_to_cpu(ex->ee_block) +
  1682. ext4_ext_get_actual_len(ex) <
  1683. le32_to_cpu(newext->ee_block))) {
  1684. ex += 1;
  1685. goto prepend;
  1686. } else if ((ex > EXT_FIRST_EXTENT(eh)) &&
  1687. (le32_to_cpu(newext->ee_block) +
  1688. ext4_ext_get_actual_len(newext) <
  1689. le32_to_cpu(ex->ee_block)))
  1690. ex -= 1;
  1691. /* Try to append newex to the ex */
  1692. if (ext4_can_extents_be_merged(inode, ex, newext)) {
  1693. ext_debug("append [%d]%d block to %u:[%d]%d"
  1694. "(from %llu)\n",
  1695. ext4_ext_is_uninitialized(newext),
  1696. ext4_ext_get_actual_len(newext),
  1697. le32_to_cpu(ex->ee_block),
  1698. ext4_ext_is_uninitialized(ex),
  1699. ext4_ext_get_actual_len(ex),
  1700. ext4_ext_pblock(ex));
  1701. err = ext4_ext_get_access(handle, inode,
  1702. path + depth);
  1703. if (err)
  1704. return err;
  1705. /*
  1706. * ext4_can_extents_be_merged should have checked
  1707. * that either both extents are uninitialized, or
  1708. * both aren't. Thus we need to check only one of
  1709. * them here.
  1710. */
  1711. if (ext4_ext_is_uninitialized(ex))
  1712. uninitialized = 1;
  1713. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1714. + ext4_ext_get_actual_len(newext));
  1715. if (uninitialized)
  1716. ext4_ext_mark_uninitialized(ex);
  1717. eh = path[depth].p_hdr;
  1718. nearex = ex;
  1719. goto merge;
  1720. }
  1721. prepend:
  1722. /* Try to prepend newex to the ex */
  1723. if (ext4_can_extents_be_merged(inode, newext, ex)) {
  1724. ext_debug("prepend %u[%d]%d block to %u:[%d]%d"
  1725. "(from %llu)\n",
  1726. le32_to_cpu(newext->ee_block),
  1727. ext4_ext_is_uninitialized(newext),
  1728. ext4_ext_get_actual_len(newext),
  1729. le32_to_cpu(ex->ee_block),
  1730. ext4_ext_is_uninitialized(ex),
  1731. ext4_ext_get_actual_len(ex),
  1732. ext4_ext_pblock(ex));
  1733. err = ext4_ext_get_access(handle, inode,
  1734. path + depth);
  1735. if (err)
  1736. return err;
  1737. /*
  1738. * ext4_can_extents_be_merged should have checked
  1739. * that either both extents are uninitialized, or
  1740. * both aren't. Thus we need to check only one of
  1741. * them here.
  1742. */
  1743. if (ext4_ext_is_uninitialized(ex))
  1744. uninitialized = 1;
  1745. ex->ee_block = newext->ee_block;
  1746. ext4_ext_store_pblock(ex, ext4_ext_pblock(newext));
  1747. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1748. + ext4_ext_get_actual_len(newext));
  1749. if (uninitialized)
  1750. ext4_ext_mark_uninitialized(ex);
  1751. eh = path[depth].p_hdr;
  1752. nearex = ex;
  1753. goto merge;
  1754. }
  1755. }
  1756. depth = ext_depth(inode);
  1757. eh = path[depth].p_hdr;
  1758. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
  1759. goto has_space;
  1760. /* probably next leaf has space for us? */
  1761. fex = EXT_LAST_EXTENT(eh);
  1762. next = EXT_MAX_BLOCKS;
  1763. if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block))
  1764. next = ext4_ext_next_leaf_block(path);
  1765. if (next != EXT_MAX_BLOCKS) {
  1766. ext_debug("next leaf block - %u\n", next);
  1767. BUG_ON(npath != NULL);
  1768. npath = ext4_ext_find_extent(inode, next, NULL, 0);
  1769. if (IS_ERR(npath))
  1770. return PTR_ERR(npath);
  1771. BUG_ON(npath->p_depth != path->p_depth);
  1772. eh = npath[depth].p_hdr;
  1773. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
  1774. ext_debug("next leaf isn't full(%d)\n",
  1775. le16_to_cpu(eh->eh_entries));
  1776. path = npath;
  1777. goto has_space;
  1778. }
  1779. ext_debug("next leaf has no free space(%d,%d)\n",
  1780. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  1781. }
  1782. /*
  1783. * There is no free space in the found leaf.
  1784. * We're gonna add a new leaf in the tree.
  1785. */
  1786. if (gb_flags & EXT4_GET_BLOCKS_METADATA_NOFAIL)
  1787. mb_flags = EXT4_MB_USE_RESERVED;
  1788. err = ext4_ext_create_new_leaf(handle, inode, mb_flags, gb_flags,
  1789. path, newext);
  1790. if (err)
  1791. goto cleanup;
  1792. depth = ext_depth(inode);
  1793. eh = path[depth].p_hdr;
  1794. has_space:
  1795. nearex = path[depth].p_ext;
  1796. err = ext4_ext_get_access(handle, inode, path + depth);
  1797. if (err)
  1798. goto cleanup;
  1799. if (!nearex) {
  1800. /* there is no extent in this leaf, create first one */
  1801. ext_debug("first extent in the leaf: %u:%llu:[%d]%d\n",
  1802. le32_to_cpu(newext->ee_block),
  1803. ext4_ext_pblock(newext),
  1804. ext4_ext_is_uninitialized(newext),
  1805. ext4_ext_get_actual_len(newext));
  1806. nearex = EXT_FIRST_EXTENT(eh);
  1807. } else {
  1808. if (le32_to_cpu(newext->ee_block)
  1809. > le32_to_cpu(nearex->ee_block)) {
  1810. /* Insert after */
  1811. ext_debug("insert %u:%llu:[%d]%d before: "
  1812. "nearest %p\n",
  1813. le32_to_cpu(newext->ee_block),
  1814. ext4_ext_pblock(newext),
  1815. ext4_ext_is_uninitialized(newext),
  1816. ext4_ext_get_actual_len(newext),
  1817. nearex);
  1818. nearex++;
  1819. } else {
  1820. /* Insert before */
  1821. BUG_ON(newext->ee_block == nearex->ee_block);
  1822. ext_debug("insert %u:%llu:[%d]%d after: "
  1823. "nearest %p\n",
  1824. le32_to_cpu(newext->ee_block),
  1825. ext4_ext_pblock(newext),
  1826. ext4_ext_is_uninitialized(newext),
  1827. ext4_ext_get_actual_len(newext),
  1828. nearex);
  1829. }
  1830. len = EXT_LAST_EXTENT(eh) - nearex + 1;
  1831. if (len > 0) {
  1832. ext_debug("insert %u:%llu:[%d]%d: "
  1833. "move %d extents from 0x%p to 0x%p\n",
  1834. le32_to_cpu(newext->ee_block),
  1835. ext4_ext_pblock(newext),
  1836. ext4_ext_is_uninitialized(newext),
  1837. ext4_ext_get_actual_len(newext),
  1838. len, nearex, nearex + 1);
  1839. memmove(nearex + 1, nearex,
  1840. len * sizeof(struct ext4_extent));
  1841. }
  1842. }
  1843. le16_add_cpu(&eh->eh_entries, 1);
  1844. path[depth].p_ext = nearex;
  1845. nearex->ee_block = newext->ee_block;
  1846. ext4_ext_store_pblock(nearex, ext4_ext_pblock(newext));
  1847. nearex->ee_len = newext->ee_len;
  1848. merge:
  1849. /* try to merge extents */
  1850. if (!(gb_flags & EXT4_GET_BLOCKS_PRE_IO))
  1851. ext4_ext_try_to_merge(handle, inode, path, nearex);
  1852. /* time to correct all indexes above */
  1853. err = ext4_ext_correct_indexes(handle, inode, path);
  1854. if (err)
  1855. goto cleanup;
  1856. err = ext4_ext_dirty(handle, inode, path + path->p_depth);
  1857. cleanup:
  1858. if (npath) {
  1859. ext4_ext_drop_refs(npath);
  1860. kfree(npath);
  1861. }
  1862. return err;
  1863. }
  1864. static int ext4_fill_fiemap_extents(struct inode *inode,
  1865. ext4_lblk_t block, ext4_lblk_t num,
  1866. struct fiemap_extent_info *fieinfo)
  1867. {
  1868. struct ext4_ext_path *path = NULL;
  1869. struct ext4_extent *ex;
  1870. struct extent_status es;
  1871. ext4_lblk_t next, next_del, start = 0, end = 0;
  1872. ext4_lblk_t last = block + num;
  1873. int exists, depth = 0, err = 0;
  1874. unsigned int flags = 0;
  1875. unsigned char blksize_bits = inode->i_sb->s_blocksize_bits;
  1876. while (block < last && block != EXT_MAX_BLOCKS) {
  1877. num = last - block;
  1878. /* find extent for this block */
  1879. down_read(&EXT4_I(inode)->i_data_sem);
  1880. if (path && ext_depth(inode) != depth) {
  1881. /* depth was changed. we have to realloc path */
  1882. kfree(path);
  1883. path = NULL;
  1884. }
  1885. path = ext4_ext_find_extent(inode, block, path, 0);
  1886. if (IS_ERR(path)) {
  1887. up_read(&EXT4_I(inode)->i_data_sem);
  1888. err = PTR_ERR(path);
  1889. path = NULL;
  1890. break;
  1891. }
  1892. depth = ext_depth(inode);
  1893. if (unlikely(path[depth].p_hdr == NULL)) {
  1894. up_read(&EXT4_I(inode)->i_data_sem);
  1895. EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
  1896. err = -EIO;
  1897. break;
  1898. }
  1899. ex = path[depth].p_ext;
  1900. next = ext4_ext_next_allocated_block(path);
  1901. ext4_ext_drop_refs(path);
  1902. flags = 0;
  1903. exists = 0;
  1904. if (!ex) {
  1905. /* there is no extent yet, so try to allocate
  1906. * all requested space */
  1907. start = block;
  1908. end = block + num;
  1909. } else if (le32_to_cpu(ex->ee_block) > block) {
  1910. /* need to allocate space before found extent */
  1911. start = block;
  1912. end = le32_to_cpu(ex->ee_block);
  1913. if (block + num < end)
  1914. end = block + num;
  1915. } else if (block >= le32_to_cpu(ex->ee_block)
  1916. + ext4_ext_get_actual_len(ex)) {
  1917. /* need to allocate space after found extent */
  1918. start = block;
  1919. end = block + num;
  1920. if (end >= next)
  1921. end = next;
  1922. } else if (block >= le32_to_cpu(ex->ee_block)) {
  1923. /*
  1924. * some part of requested space is covered
  1925. * by found extent
  1926. */
  1927. start = block;
  1928. end = le32_to_cpu(ex->ee_block)
  1929. + ext4_ext_get_actual_len(ex);
  1930. if (block + num < end)
  1931. end = block + num;
  1932. exists = 1;
  1933. } else {
  1934. BUG();
  1935. }
  1936. BUG_ON(end <= start);
  1937. if (!exists) {
  1938. es.es_lblk = start;
  1939. es.es_len = end - start;
  1940. es.es_pblk = 0;
  1941. } else {
  1942. es.es_lblk = le32_to_cpu(ex->ee_block);
  1943. es.es_len = ext4_ext_get_actual_len(ex);
  1944. es.es_pblk = ext4_ext_pblock(ex);
  1945. if (ext4_ext_is_uninitialized(ex))
  1946. flags |= FIEMAP_EXTENT_UNWRITTEN;
  1947. }
  1948. /*
  1949. * Find delayed extent and update es accordingly. We call
  1950. * it even in !exists case to find out whether es is the
  1951. * last existing extent or not.
  1952. */
  1953. next_del = ext4_find_delayed_extent(inode, &es);
  1954. if (!exists && next_del) {
  1955. exists = 1;
  1956. flags |= (FIEMAP_EXTENT_DELALLOC |
  1957. FIEMAP_EXTENT_UNKNOWN);
  1958. }
  1959. up_read(&EXT4_I(inode)->i_data_sem);
  1960. if (unlikely(es.es_len == 0)) {
  1961. EXT4_ERROR_INODE(inode, "es.es_len == 0");
  1962. err = -EIO;
  1963. break;
  1964. }
  1965. /*
  1966. * This is possible iff next == next_del == EXT_MAX_BLOCKS.
  1967. * we need to check next == EXT_MAX_BLOCKS because it is
  1968. * possible that an extent is with unwritten and delayed
  1969. * status due to when an extent is delayed allocated and
  1970. * is allocated by fallocate status tree will track both of
  1971. * them in a extent.
  1972. *
  1973. * So we could return a unwritten and delayed extent, and
  1974. * its block is equal to 'next'.
  1975. */
  1976. if (next == next_del && next == EXT_MAX_BLOCKS) {
  1977. flags |= FIEMAP_EXTENT_LAST;
  1978. if (unlikely(next_del != EXT_MAX_BLOCKS ||
  1979. next != EXT_MAX_BLOCKS)) {
  1980. EXT4_ERROR_INODE(inode,
  1981. "next extent == %u, next "
  1982. "delalloc extent = %u",
  1983. next, next_del);
  1984. err = -EIO;
  1985. break;
  1986. }
  1987. }
  1988. if (exists) {
  1989. err = fiemap_fill_next_extent(fieinfo,
  1990. (__u64)es.es_lblk << blksize_bits,
  1991. (__u64)es.es_pblk << blksize_bits,
  1992. (__u64)es.es_len << blksize_bits,
  1993. flags);
  1994. if (err < 0)
  1995. break;
  1996. if (err == 1) {
  1997. err = 0;
  1998. break;
  1999. }
  2000. }
  2001. block = es.es_lblk + es.es_len;
  2002. }
  2003. if (path) {
  2004. ext4_ext_drop_refs(path);
  2005. kfree(path);
  2006. }
  2007. return err;
  2008. }
  2009. /*
  2010. * ext4_ext_put_gap_in_cache:
  2011. * calculate boundaries of the gap that the requested block fits into
  2012. * and cache this gap
  2013. */
  2014. static void
  2015. ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
  2016. ext4_lblk_t block)
  2017. {
  2018. int depth = ext_depth(inode);
  2019. unsigned long len;
  2020. ext4_lblk_t lblock;
  2021. struct ext4_extent *ex;
  2022. ex = path[depth].p_ext;
  2023. if (ex == NULL) {
  2024. /*
  2025. * there is no extent yet, so gap is [0;-] and we
  2026. * don't cache it
  2027. */
  2028. ext_debug("cache gap(whole file):");
  2029. } else if (block < le32_to_cpu(ex->ee_block)) {
  2030. lblock = block;
  2031. len = le32_to_cpu(ex->ee_block) - block;
  2032. ext_debug("cache gap(before): %u [%u:%u]",
  2033. block,
  2034. le32_to_cpu(ex->ee_block),
  2035. ext4_ext_get_actual_len(ex));
  2036. if (!ext4_find_delalloc_range(inode, lblock, lblock + len - 1))
  2037. ext4_es_insert_extent(inode, lblock, len, ~0,
  2038. EXTENT_STATUS_HOLE);
  2039. } else if (block >= le32_to_cpu(ex->ee_block)
  2040. + ext4_ext_get_actual_len(ex)) {
  2041. ext4_lblk_t next;
  2042. lblock = le32_to_cpu(ex->ee_block)
  2043. + ext4_ext_get_actual_len(ex);
  2044. next = ext4_ext_next_allocated_block(path);
  2045. ext_debug("cache gap(after): [%u:%u] %u",
  2046. le32_to_cpu(ex->ee_block),
  2047. ext4_ext_get_actual_len(ex),
  2048. block);
  2049. BUG_ON(next == lblock);
  2050. len = next - lblock;
  2051. if (!ext4_find_delalloc_range(inode, lblock, lblock + len - 1))
  2052. ext4_es_insert_extent(inode, lblock, len, ~0,
  2053. EXTENT_STATUS_HOLE);
  2054. } else {
  2055. lblock = len = 0;
  2056. BUG();
  2057. }
  2058. ext_debug(" -> %u:%lu\n", lblock, len);
  2059. }
  2060. /*
  2061. * ext4_ext_rm_idx:
  2062. * removes index from the index block.
  2063. */
  2064. static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
  2065. struct ext4_ext_path *path, int depth)
  2066. {
  2067. int err;
  2068. ext4_fsblk_t leaf;
  2069. /* free index block */
  2070. depth--;
  2071. path = path + depth;
  2072. leaf = ext4_idx_pblock(path->p_idx);
  2073. if (unlikely(path->p_hdr->eh_entries == 0)) {
  2074. EXT4_ERROR_INODE(inode, "path->p_hdr->eh_entries == 0");
  2075. return -EIO;
  2076. }
  2077. err = ext4_ext_get_access(handle, inode, path);
  2078. if (err)
  2079. return err;
  2080. if (path->p_idx != EXT_LAST_INDEX(path->p_hdr)) {
  2081. int len = EXT_LAST_INDEX(path->p_hdr) - path->p_idx;
  2082. len *= sizeof(struct ext4_extent_idx);
  2083. memmove(path->p_idx, path->p_idx + 1, len);
  2084. }
  2085. le16_add_cpu(&path->p_hdr->eh_entries, -1);
  2086. err = ext4_ext_dirty(handle, inode, path);
  2087. if (err)
  2088. return err;
  2089. ext_debug("index is empty, remove it, free block %llu\n", leaf);
  2090. trace_ext4_ext_rm_idx(inode, leaf);
  2091. ext4_free_blocks(handle, inode, NULL, leaf, 1,
  2092. EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
  2093. while (--depth >= 0) {
  2094. if (path->p_idx != EXT_FIRST_INDEX(path->p_hdr))
  2095. break;
  2096. path--;
  2097. err = ext4_ext_get_access(handle, inode, path);
  2098. if (err)
  2099. break;
  2100. path->p_idx->ei_block = (path+1)->p_idx->ei_block;
  2101. err = ext4_ext_dirty(handle, inode, path);
  2102. if (err)
  2103. break;
  2104. }
  2105. return err;
  2106. }
  2107. /*
  2108. * ext4_ext_calc_credits_for_single_extent:
  2109. * This routine returns max. credits that needed to insert an extent
  2110. * to the extent tree.
  2111. * When pass the actual path, the caller should calculate credits
  2112. * under i_data_sem.
  2113. */
  2114. int ext4_ext_calc_credits_for_single_extent(struct inode *inode, int nrblocks,
  2115. struct ext4_ext_path *path)
  2116. {
  2117. if (path) {
  2118. int depth = ext_depth(inode);
  2119. int ret = 0;
  2120. /* probably there is space in leaf? */
  2121. if (le16_to_cpu(path[depth].p_hdr->eh_entries)
  2122. < le16_to_cpu(path[depth].p_hdr->eh_max)) {
  2123. /*
  2124. * There are some space in the leaf tree, no
  2125. * need to account for leaf block credit
  2126. *
  2127. * bitmaps and block group descriptor blocks
  2128. * and other metadata blocks still need to be
  2129. * accounted.
  2130. */
  2131. /* 1 bitmap, 1 block group descriptor */
  2132. ret = 2 + EXT4_META_TRANS_BLOCKS(inode->i_sb);
  2133. return ret;
  2134. }
  2135. }
  2136. return ext4_chunk_trans_blocks(inode, nrblocks);
  2137. }
  2138. /*
  2139. * How many index/leaf blocks need to change/allocate to add @extents extents?
  2140. *
  2141. * If we add a single extent, then in the worse case, each tree level
  2142. * index/leaf need to be changed in case of the tree split.
  2143. *
  2144. * If more extents are inserted, they could cause the whole tree split more
  2145. * than once, but this is really rare.
  2146. */
  2147. int ext4_ext_index_trans_blocks(struct inode *inode, int extents)
  2148. {
  2149. int index;
  2150. int depth;
  2151. /* If we are converting the inline data, only one is needed here. */
  2152. if (ext4_has_inline_data(inode))
  2153. return 1;
  2154. depth = ext_depth(inode);
  2155. if (extents <= 1)
  2156. index = depth * 2;
  2157. else
  2158. index = depth * 3;
  2159. return index;
  2160. }
  2161. static inline int get_default_free_blocks_flags(struct inode *inode)
  2162. {
  2163. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  2164. return EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET;
  2165. else if (ext4_should_journal_data(inode))
  2166. return EXT4_FREE_BLOCKS_FORGET;
  2167. return 0;
  2168. }
  2169. static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
  2170. struct ext4_extent *ex,
  2171. long long *partial_cluster,
  2172. ext4_lblk_t from, ext4_lblk_t to)
  2173. {
  2174. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2175. unsigned short ee_len = ext4_ext_get_actual_len(ex);
  2176. ext4_fsblk_t pblk;
  2177. int flags = get_default_free_blocks_flags(inode);
  2178. /*
  2179. * For bigalloc file systems, we never free a partial cluster
  2180. * at the beginning of the extent. Instead, we make a note
  2181. * that we tried freeing the cluster, and check to see if we
  2182. * need to free it on a subsequent call to ext4_remove_blocks,
  2183. * or at the end of the ext4_truncate() operation.
  2184. */
  2185. flags |= EXT4_FREE_BLOCKS_NOFREE_FIRST_CLUSTER;
  2186. trace_ext4_remove_blocks(inode, ex, from, to, *partial_cluster);
  2187. /*
  2188. * If we have a partial cluster, and it's different from the
  2189. * cluster of the last block, we need to explicitly free the
  2190. * partial cluster here.
  2191. */
  2192. pblk = ext4_ext_pblock(ex) + ee_len - 1;
  2193. if ((*partial_cluster > 0) &&
  2194. (EXT4_B2C(sbi, pblk) != *partial_cluster)) {
  2195. ext4_free_blocks(handle, inode, NULL,
  2196. EXT4_C2B(sbi, *partial_cluster),
  2197. sbi->s_cluster_ratio, flags);
  2198. *partial_cluster = 0;
  2199. }
  2200. #ifdef EXTENTS_STATS
  2201. {
  2202. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2203. spin_lock(&sbi->s_ext_stats_lock);
  2204. sbi->s_ext_blocks += ee_len;
  2205. sbi->s_ext_extents++;
  2206. if (ee_len < sbi->s_ext_min)
  2207. sbi->s_ext_min = ee_len;
  2208. if (ee_len > sbi->s_ext_max)
  2209. sbi->s_ext_max = ee_len;
  2210. if (ext_depth(inode) > sbi->s_depth_max)
  2211. sbi->s_depth_max = ext_depth(inode);
  2212. spin_unlock(&sbi->s_ext_stats_lock);
  2213. }
  2214. #endif
  2215. if (from >= le32_to_cpu(ex->ee_block)
  2216. && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
  2217. /* tail removal */
  2218. ext4_lblk_t num;
  2219. unsigned int unaligned;
  2220. num = le32_to_cpu(ex->ee_block) + ee_len - from;
  2221. pblk = ext4_ext_pblock(ex) + ee_len - num;
  2222. /*
  2223. * Usually we want to free partial cluster at the end of the
  2224. * extent, except for the situation when the cluster is still
  2225. * used by any other extent (partial_cluster is negative).
  2226. */
  2227. if (*partial_cluster < 0 &&
  2228. -(*partial_cluster) == EXT4_B2C(sbi, pblk + num - 1))
  2229. flags |= EXT4_FREE_BLOCKS_NOFREE_LAST_CLUSTER;
  2230. ext_debug("free last %u blocks starting %llu partial %lld\n",
  2231. num, pblk, *partial_cluster);
  2232. ext4_free_blocks(handle, inode, NULL, pblk, num, flags);
  2233. /*
  2234. * If the block range to be freed didn't start at the
  2235. * beginning of a cluster, and we removed the entire
  2236. * extent and the cluster is not used by any other extent,
  2237. * save the partial cluster here, since we might need to
  2238. * delete if we determine that the truncate operation has
  2239. * removed all of the blocks in the cluster.
  2240. *
  2241. * On the other hand, if we did not manage to free the whole
  2242. * extent, we have to mark the cluster as used (store negative
  2243. * cluster number in partial_cluster).
  2244. */
  2245. unaligned = pblk & (sbi->s_cluster_ratio - 1);
  2246. if (unaligned && (ee_len == num) &&
  2247. (*partial_cluster != -((long long)EXT4_B2C(sbi, pblk))))
  2248. *partial_cluster = EXT4_B2C(sbi, pblk);
  2249. else if (unaligned)
  2250. *partial_cluster = -((long long)EXT4_B2C(sbi, pblk));
  2251. else if (*partial_cluster > 0)
  2252. *partial_cluster = 0;
  2253. } else
  2254. ext4_error(sbi->s_sb, "strange request: removal(2) "
  2255. "%u-%u from %u:%u\n",
  2256. from, to, le32_to_cpu(ex->ee_block), ee_len);
  2257. return 0;
  2258. }
  2259. /*
  2260. * ext4_ext_rm_leaf() Removes the extents associated with the
  2261. * blocks appearing between "start" and "end", and splits the extents
  2262. * if "start" and "end" appear in the same extent
  2263. *
  2264. * @handle: The journal handle
  2265. * @inode: The files inode
  2266. * @path: The path to the leaf
  2267. * @partial_cluster: The cluster which we'll have to free if all extents
  2268. * has been released from it. It gets negative in case
  2269. * that the cluster is still used.
  2270. * @start: The first block to remove
  2271. * @end: The last block to remove
  2272. */
  2273. static int
  2274. ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
  2275. struct ext4_ext_path *path,
  2276. long long *partial_cluster,
  2277. ext4_lblk_t start, ext4_lblk_t end)
  2278. {
  2279. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  2280. int err = 0, correct_index = 0;
  2281. int depth = ext_depth(inode), credits;
  2282. struct ext4_extent_header *eh;
  2283. ext4_lblk_t a, b;
  2284. unsigned num;
  2285. ext4_lblk_t ex_ee_block;
  2286. unsigned short ex_ee_len;
  2287. unsigned uninitialized = 0;
  2288. struct ext4_extent *ex;
  2289. ext4_fsblk_t pblk;
  2290. /* the header must be checked already in ext4_ext_remove_space() */
  2291. ext_debug("truncate since %u in leaf to %u\n", start, end);
  2292. if (!path[depth].p_hdr)
  2293. path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
  2294. eh = path[depth].p_hdr;
  2295. if (unlikely(path[depth].p_hdr == NULL)) {
  2296. EXT4_ERROR_INODE(inode, "path[%d].p_hdr == NULL", depth);
  2297. return -EIO;
  2298. }
  2299. /* find where to start removing */
  2300. ex = path[depth].p_ext;
  2301. if (!ex)
  2302. ex = EXT_LAST_EXTENT(eh);
  2303. ex_ee_block = le32_to_cpu(ex->ee_block);
  2304. ex_ee_len = ext4_ext_get_actual_len(ex);
  2305. trace_ext4_ext_rm_leaf(inode, start, ex, *partial_cluster);
  2306. while (ex >= EXT_FIRST_EXTENT(eh) &&
  2307. ex_ee_block + ex_ee_len > start) {
  2308. if (ext4_ext_is_uninitialized(ex))
  2309. uninitialized = 1;
  2310. else
  2311. uninitialized = 0;
  2312. ext_debug("remove ext %u:[%d]%d\n", ex_ee_block,
  2313. uninitialized, ex_ee_len);
  2314. path[depth].p_ext = ex;
  2315. a = ex_ee_block > start ? ex_ee_block : start;
  2316. b = ex_ee_block+ex_ee_len - 1 < end ?
  2317. ex_ee_block+ex_ee_len - 1 : end;
  2318. ext_debug(" border %u:%u\n", a, b);
  2319. /* If this extent is beyond the end of the hole, skip it */
  2320. if (end < ex_ee_block) {
  2321. /*
  2322. * We're going to skip this extent and move to another,
  2323. * so if this extent is not cluster aligned we have
  2324. * to mark the current cluster as used to avoid
  2325. * accidentally freeing it later on
  2326. */
  2327. pblk = ext4_ext_pblock(ex);
  2328. if (pblk & (sbi->s_cluster_ratio - 1))
  2329. *partial_cluster =
  2330. -((long long)EXT4_B2C(sbi, pblk));
  2331. ex--;
  2332. ex_ee_block = le32_to_cpu(ex->ee_block);
  2333. ex_ee_len = ext4_ext_get_actual_len(ex);
  2334. continue;
  2335. } else if (b != ex_ee_block + ex_ee_len - 1) {
  2336. EXT4_ERROR_INODE(inode,
  2337. "can not handle truncate %u:%u "
  2338. "on extent %u:%u",
  2339. start, end, ex_ee_block,
  2340. ex_ee_block + ex_ee_len - 1);
  2341. err = -EIO;
  2342. goto out;
  2343. } else if (a != ex_ee_block) {
  2344. /* remove tail of the extent */
  2345. num = a - ex_ee_block;
  2346. } else {
  2347. /* remove whole extent: excellent! */
  2348. num = 0;
  2349. }
  2350. /*
  2351. * 3 for leaf, sb, and inode plus 2 (bmap and group
  2352. * descriptor) for each block group; assume two block
  2353. * groups plus ex_ee_len/blocks_per_block_group for
  2354. * the worst case
  2355. */
  2356. credits = 7 + 2*(ex_ee_len/EXT4_BLOCKS_PER_GROUP(inode->i_sb));
  2357. if (ex == EXT_FIRST_EXTENT(eh)) {
  2358. correct_index = 1;
  2359. credits += (ext_depth(inode)) + 1;
  2360. }
  2361. credits += EXT4_MAXQUOTAS_TRANS_BLOCKS(inode->i_sb);
  2362. err = ext4_ext_truncate_extend_restart(handle, inode, credits);
  2363. if (err)
  2364. goto out;
  2365. err = ext4_ext_get_access(handle, inode, path + depth);
  2366. if (err)
  2367. goto out;
  2368. err = ext4_remove_blocks(handle, inode, ex, partial_cluster,
  2369. a, b);
  2370. if (err)
  2371. goto out;
  2372. if (num == 0)
  2373. /* this extent is removed; mark slot entirely unused */
  2374. ext4_ext_store_pblock(ex, 0);
  2375. ex->ee_len = cpu_to_le16(num);
  2376. /*
  2377. * Do not mark uninitialized if all the blocks in the
  2378. * extent have been removed.
  2379. */
  2380. if (uninitialized && num)
  2381. ext4_ext_mark_uninitialized(ex);
  2382. /*
  2383. * If the extent was completely released,
  2384. * we need to remove it from the leaf
  2385. */
  2386. if (num == 0) {
  2387. if (end != EXT_MAX_BLOCKS - 1) {
  2388. /*
  2389. * For hole punching, we need to scoot all the
  2390. * extents up when an extent is removed so that
  2391. * we dont have blank extents in the middle
  2392. */
  2393. memmove(ex, ex+1, (EXT_LAST_EXTENT(eh) - ex) *
  2394. sizeof(struct ext4_extent));
  2395. /* Now get rid of the one at the end */
  2396. memset(EXT_LAST_EXTENT(eh), 0,
  2397. sizeof(struct ext4_extent));
  2398. }
  2399. le16_add_cpu(&eh->eh_entries, -1);
  2400. } else if (*partial_cluster > 0)
  2401. *partial_cluster = 0;
  2402. err = ext4_ext_dirty(handle, inode, path + depth);
  2403. if (err)
  2404. goto out;
  2405. ext_debug("new extent: %u:%u:%llu\n", ex_ee_block, num,
  2406. ext4_ext_pblock(ex));
  2407. ex--;
  2408. ex_ee_block = le32_to_cpu(ex->ee_block);
  2409. ex_ee_len = ext4_ext_get_actual_len(ex);
  2410. }
  2411. if (correct_index && eh->eh_entries)
  2412. err = ext4_ext_correct_indexes(handle, inode, path);
  2413. /*
  2414. * Free the partial cluster only if the current extent does not
  2415. * reference it. Otherwise we might free used cluster.
  2416. */
  2417. if (*partial_cluster > 0 &&
  2418. (EXT4_B2C(sbi, ext4_ext_pblock(ex) + ex_ee_len - 1) !=
  2419. *partial_cluster)) {
  2420. int flags = get_default_free_blocks_flags(inode);
  2421. ext4_free_blocks(handle, inode, NULL,
  2422. EXT4_C2B(sbi, *partial_cluster),
  2423. sbi->s_cluster_ratio, flags);
  2424. *partial_cluster = 0;
  2425. }
  2426. /* if this leaf is free, then we should
  2427. * remove it from index block above */
  2428. if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
  2429. err = ext4_ext_rm_idx(handle, inode, path, depth);
  2430. out:
  2431. return err;
  2432. }
  2433. /*
  2434. * ext4_ext_more_to_rm:
  2435. * returns 1 if current index has to be freed (even partial)
  2436. */
  2437. static int
  2438. ext4_ext_more_to_rm(struct ext4_ext_path *path)
  2439. {
  2440. BUG_ON(path->p_idx == NULL);
  2441. if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
  2442. return 0;
  2443. /*
  2444. * if truncate on deeper level happened, it wasn't partial,
  2445. * so we have to consider current index for truncation
  2446. */
  2447. if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
  2448. return 0;
  2449. return 1;
  2450. }
  2451. int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start,
  2452. ext4_lblk_t end)
  2453. {
  2454. struct super_block *sb = inode->i_sb;
  2455. int depth = ext_depth(inode);
  2456. struct ext4_ext_path *path = NULL;
  2457. long long partial_cluster = 0;
  2458. handle_t *handle;
  2459. int i = 0, err = 0;
  2460. ext_debug("truncate since %u to %u\n", start, end);
  2461. /* probably first extent we're gonna free will be last in block */
  2462. handle = ext4_journal_start(inode, EXT4_HT_TRUNCATE, depth + 1);
  2463. if (IS_ERR(handle))
  2464. return PTR_ERR(handle);
  2465. again:
  2466. trace_ext4_ext_remove_space(inode, start, end, depth);
  2467. /*
  2468. * Check if we are removing extents inside the extent tree. If that
  2469. * is the case, we are going to punch a hole inside the extent tree
  2470. * so we have to check whether we need to split the extent covering
  2471. * the last block to remove so we can easily remove the part of it
  2472. * in ext4_ext_rm_leaf().
  2473. */
  2474. if (end < EXT_MAX_BLOCKS - 1) {
  2475. struct ext4_extent *ex;
  2476. ext4_lblk_t ee_block;
  2477. /* find extent for this block */
  2478. path = ext4_ext_find_extent(inode, end, NULL, EXT4_EX_NOCACHE);
  2479. if (IS_ERR(path)) {
  2480. ext4_journal_stop(handle);
  2481. return PTR_ERR(path);
  2482. }
  2483. depth = ext_depth(inode);
  2484. /* Leaf not may not exist only if inode has no blocks at all */
  2485. ex = path[depth].p_ext;
  2486. if (!ex) {
  2487. if (depth) {
  2488. EXT4_ERROR_INODE(inode,
  2489. "path[%d].p_hdr == NULL",
  2490. depth);
  2491. err = -EIO;
  2492. }
  2493. goto out;
  2494. }
  2495. ee_block = le32_to_cpu(ex->ee_block);
  2496. /*
  2497. * See if the last block is inside the extent, if so split
  2498. * the extent at 'end' block so we can easily remove the
  2499. * tail of the first part of the split extent in
  2500. * ext4_ext_rm_leaf().
  2501. */
  2502. if (end >= ee_block &&
  2503. end < ee_block + ext4_ext_get_actual_len(ex) - 1) {
  2504. int split_flag = 0;
  2505. if (ext4_ext_is_uninitialized(ex))
  2506. split_flag = EXT4_EXT_MARK_UNINIT1 |
  2507. EXT4_EXT_MARK_UNINIT2;
  2508. /*
  2509. * Split the extent in two so that 'end' is the last
  2510. * block in the first new extent. Also we should not
  2511. * fail removing space due to ENOSPC so try to use
  2512. * reserved block if that happens.
  2513. */
  2514. err = ext4_split_extent_at(handle, inode, path,
  2515. end + 1, split_flag,
  2516. EXT4_EX_NOCACHE |
  2517. EXT4_GET_BLOCKS_PRE_IO |
  2518. EXT4_GET_BLOCKS_METADATA_NOFAIL);
  2519. if (err < 0)
  2520. goto out;
  2521. }
  2522. }
  2523. /*
  2524. * We start scanning from right side, freeing all the blocks
  2525. * after i_size and walking into the tree depth-wise.
  2526. */
  2527. depth = ext_depth(inode);
  2528. if (path) {
  2529. int k = i = depth;
  2530. while (--k > 0)
  2531. path[k].p_block =
  2532. le16_to_cpu(path[k].p_hdr->eh_entries)+1;
  2533. } else {
  2534. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1),
  2535. GFP_NOFS);
  2536. if (path == NULL) {
  2537. ext4_journal_stop(handle);
  2538. return -ENOMEM;
  2539. }
  2540. path[0].p_depth = depth;
  2541. path[0].p_hdr = ext_inode_hdr(inode);
  2542. i = 0;
  2543. if (ext4_ext_check(inode, path[0].p_hdr, depth, 0)) {
  2544. err = -EIO;
  2545. goto out;
  2546. }
  2547. }
  2548. err = 0;
  2549. while (i >= 0 && err == 0) {
  2550. if (i == depth) {
  2551. /* this is leaf block */
  2552. err = ext4_ext_rm_leaf(handle, inode, path,
  2553. &partial_cluster, start,
  2554. end);
  2555. /* root level has p_bh == NULL, brelse() eats this */
  2556. brelse(path[i].p_bh);
  2557. path[i].p_bh = NULL;
  2558. i--;
  2559. continue;
  2560. }
  2561. /* this is index block */
  2562. if (!path[i].p_hdr) {
  2563. ext_debug("initialize header\n");
  2564. path[i].p_hdr = ext_block_hdr(path[i].p_bh);
  2565. }
  2566. if (!path[i].p_idx) {
  2567. /* this level hasn't been touched yet */
  2568. path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
  2569. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
  2570. ext_debug("init index ptr: hdr 0x%p, num %d\n",
  2571. path[i].p_hdr,
  2572. le16_to_cpu(path[i].p_hdr->eh_entries));
  2573. } else {
  2574. /* we were already here, see at next index */
  2575. path[i].p_idx--;
  2576. }
  2577. ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
  2578. i, EXT_FIRST_INDEX(path[i].p_hdr),
  2579. path[i].p_idx);
  2580. if (ext4_ext_more_to_rm(path + i)) {
  2581. struct buffer_head *bh;
  2582. /* go to the next level */
  2583. ext_debug("move to level %d (block %llu)\n",
  2584. i + 1, ext4_idx_pblock(path[i].p_idx));
  2585. memset(path + i + 1, 0, sizeof(*path));
  2586. bh = read_extent_tree_block(inode,
  2587. ext4_idx_pblock(path[i].p_idx), depth - i - 1,
  2588. EXT4_EX_NOCACHE);
  2589. if (IS_ERR(bh)) {
  2590. /* should we reset i_size? */
  2591. err = PTR_ERR(bh);
  2592. break;
  2593. }
  2594. /* Yield here to deal with large extent trees.
  2595. * Should be a no-op if we did IO above. */
  2596. cond_resched();
  2597. if (WARN_ON(i + 1 > depth)) {
  2598. err = -EIO;
  2599. break;
  2600. }
  2601. path[i + 1].p_bh = bh;
  2602. /* save actual number of indexes since this
  2603. * number is changed at the next iteration */
  2604. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
  2605. i++;
  2606. } else {
  2607. /* we finished processing this index, go up */
  2608. if (path[i].p_hdr->eh_entries == 0 && i > 0) {
  2609. /* index is empty, remove it;
  2610. * handle must be already prepared by the
  2611. * truncatei_leaf() */
  2612. err = ext4_ext_rm_idx(handle, inode, path, i);
  2613. }
  2614. /* root level has p_bh == NULL, brelse() eats this */
  2615. brelse(path[i].p_bh);
  2616. path[i].p_bh = NULL;
  2617. i--;
  2618. ext_debug("return to level %d\n", i);
  2619. }
  2620. }
  2621. trace_ext4_ext_remove_space_done(inode, start, end, depth,
  2622. partial_cluster, path->p_hdr->eh_entries);
  2623. /* If we still have something in the partial cluster and we have removed
  2624. * even the first extent, then we should free the blocks in the partial
  2625. * cluster as well. */
  2626. if (partial_cluster > 0 && path->p_hdr->eh_entries == 0) {
  2627. int flags = get_default_free_blocks_flags(inode);
  2628. ext4_free_blocks(handle, inode, NULL,
  2629. EXT4_C2B(EXT4_SB(sb), partial_cluster),
  2630. EXT4_SB(sb)->s_cluster_ratio, flags);
  2631. partial_cluster = 0;
  2632. }
  2633. /* TODO: flexible tree reduction should be here */
  2634. if (path->p_hdr->eh_entries == 0) {
  2635. /*
  2636. * truncate to zero freed all the tree,
  2637. * so we need to correct eh_depth
  2638. */
  2639. err = ext4_ext_get_access(handle, inode, path);
  2640. if (err == 0) {
  2641. ext_inode_hdr(inode)->eh_depth = 0;
  2642. ext_inode_hdr(inode)->eh_max =
  2643. cpu_to_le16(ext4_ext_space_root(inode, 0));
  2644. err = ext4_ext_dirty(handle, inode, path);
  2645. }
  2646. }
  2647. out:
  2648. ext4_ext_drop_refs(path);
  2649. kfree(path);
  2650. if (err == -EAGAIN) {
  2651. path = NULL;
  2652. goto again;
  2653. }
  2654. ext4_journal_stop(handle);
  2655. return err;
  2656. }
  2657. /*
  2658. * called at mount time
  2659. */
  2660. void ext4_ext_init(struct super_block *sb)
  2661. {
  2662. /*
  2663. * possible initialization would be here
  2664. */
  2665. if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS)) {
  2666. #if defined(AGGRESSIVE_TEST) || defined(CHECK_BINSEARCH) || defined(EXTENTS_STATS)
  2667. printk(KERN_INFO "EXT4-fs: file extents enabled"
  2668. #ifdef AGGRESSIVE_TEST
  2669. ", aggressive tests"
  2670. #endif
  2671. #ifdef CHECK_BINSEARCH
  2672. ", check binsearch"
  2673. #endif
  2674. #ifdef EXTENTS_STATS
  2675. ", stats"
  2676. #endif
  2677. "\n");
  2678. #endif
  2679. #ifdef EXTENTS_STATS
  2680. spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
  2681. EXT4_SB(sb)->s_ext_min = 1 << 30;
  2682. EXT4_SB(sb)->s_ext_max = 0;
  2683. #endif
  2684. }
  2685. }
  2686. /*
  2687. * called at umount time
  2688. */
  2689. void ext4_ext_release(struct super_block *sb)
  2690. {
  2691. if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_EXTENTS))
  2692. return;
  2693. #ifdef EXTENTS_STATS
  2694. if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
  2695. struct ext4_sb_info *sbi = EXT4_SB(sb);
  2696. printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
  2697. sbi->s_ext_blocks, sbi->s_ext_extents,
  2698. sbi->s_ext_blocks / sbi->s_ext_extents);
  2699. printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
  2700. sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
  2701. }
  2702. #endif
  2703. }
  2704. /* FIXME!! we need to try to merge to left or right after zero-out */
  2705. static int ext4_ext_zeroout(struct inode *inode, struct ext4_extent *ex)
  2706. {
  2707. ext4_fsblk_t ee_pblock;
  2708. unsigned int ee_len;
  2709. int ret;
  2710. ee_len = ext4_ext_get_actual_len(ex);
  2711. ee_pblock = ext4_ext_pblock(ex);
  2712. ret = sb_issue_zeroout(inode->i_sb, ee_pblock, ee_len, GFP_NOFS);
  2713. if (ret > 0)
  2714. ret = 0;
  2715. return ret;
  2716. }
  2717. /*
  2718. * ext4_split_extent_at() splits an extent at given block.
  2719. *
  2720. * @handle: the journal handle
  2721. * @inode: the file inode
  2722. * @path: the path to the extent
  2723. * @split: the logical block where the extent is splitted.
  2724. * @split_flags: indicates if the extent could be zeroout if split fails, and
  2725. * the states(init or uninit) of new extents.
  2726. * @flags: flags used to insert new extent to extent tree.
  2727. *
  2728. *
  2729. * Splits extent [a, b] into two extents [a, @split) and [@split, b], states
  2730. * of which are deterimined by split_flag.
  2731. *
  2732. * There are two cases:
  2733. * a> the extent are splitted into two extent.
  2734. * b> split is not needed, and just mark the extent.
  2735. *
  2736. * return 0 on success.
  2737. */
  2738. static int ext4_split_extent_at(handle_t *handle,
  2739. struct inode *inode,
  2740. struct ext4_ext_path *path,
  2741. ext4_lblk_t split,
  2742. int split_flag,
  2743. int flags)
  2744. {
  2745. ext4_fsblk_t newblock;
  2746. ext4_lblk_t ee_block;
  2747. struct ext4_extent *ex, newex, orig_ex, zero_ex;
  2748. struct ext4_extent *ex2 = NULL;
  2749. unsigned int ee_len, depth;
  2750. int err = 0;
  2751. BUG_ON((split_flag & (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2)) ==
  2752. (EXT4_EXT_DATA_VALID1 | EXT4_EXT_DATA_VALID2));
  2753. ext_debug("ext4_split_extents_at: inode %lu, logical"
  2754. "block %llu\n", inode->i_ino, (unsigned long long)split);
  2755. ext4_ext_show_leaf(inode, path);
  2756. depth = ext_depth(inode);
  2757. ex = path[depth].p_ext;
  2758. ee_block = le32_to_cpu(ex->ee_block);
  2759. ee_len = ext4_ext_get_actual_len(ex);
  2760. newblock = split - ee_block + ext4_ext_pblock(ex);
  2761. BUG_ON(split < ee_block || split >= (ee_block + ee_len));
  2762. BUG_ON(!ext4_ext_is_uninitialized(ex) &&
  2763. split_flag & (EXT4_EXT_MAY_ZEROOUT |
  2764. EXT4_EXT_MARK_UNINIT1 |
  2765. EXT4_EXT_MARK_UNINIT2));
  2766. err = ext4_ext_get_access(handle, inode, path + depth);
  2767. if (err)
  2768. goto out;
  2769. if (split == ee_block) {
  2770. /*
  2771. * case b: block @split is the block that the extent begins with
  2772. * then we just change the state of the extent, and splitting
  2773. * is not needed.
  2774. */
  2775. if (split_flag & EXT4_EXT_MARK_UNINIT2)
  2776. ext4_ext_mark_uninitialized(ex);
  2777. else
  2778. ext4_ext_mark_initialized(ex);
  2779. if (!(flags & EXT4_GET_BLOCKS_PRE_IO))
  2780. ext4_ext_try_to_merge(handle, inode, path, ex);
  2781. err = ext4_ext_dirty(handle, inode, path + path->p_depth);
  2782. goto out;
  2783. }
  2784. /* case a */
  2785. memcpy(&orig_ex, ex, sizeof(orig_ex));
  2786. ex->ee_len = cpu_to_le16(split - ee_block);
  2787. if (split_flag & EXT4_EXT_MARK_UNINIT1)
  2788. ext4_ext_mark_uninitialized(ex);
  2789. /*
  2790. * path may lead to new leaf, not to original leaf any more
  2791. * after ext4_ext_insert_extent() returns,
  2792. */
  2793. err = ext4_ext_dirty(handle, inode, path + depth);
  2794. if (err)
  2795. goto fix_extent_len;
  2796. ex2 = &newex;
  2797. ex2->ee_block = cpu_to_le32(split);
  2798. ex2->ee_len = cpu_to_le16(ee_len - (split - ee_block));
  2799. ext4_ext_store_pblock(ex2, newblock);
  2800. if (split_flag & EXT4_EXT_MARK_UNINIT2)
  2801. ext4_ext_mark_uninitialized(ex2);
  2802. err = ext4_ext_insert_extent(handle, inode, path, &newex, flags);
  2803. if (err == -ENOSPC && (EXT4_EXT_MAY_ZEROOUT & split_flag)) {
  2804. if (split_flag & (EXT4_EXT_DATA_VALID1|EXT4_EXT_DATA_VALID2)) {
  2805. if (split_flag & EXT4_EXT_DATA_VALID1) {
  2806. err = ext4_ext_zeroout(inode, ex2);
  2807. zero_ex.ee_block = ex2->ee_block;
  2808. zero_ex.ee_len = cpu_to_le16(
  2809. ext4_ext_get_actual_len(ex2));
  2810. ext4_ext_store_pblock(&zero_ex,
  2811. ext4_ext_pblock(ex2));
  2812. } else {
  2813. err = ext4_ext_zeroout(inode, ex);
  2814. zero_ex.ee_block = ex->ee_block;
  2815. zero_ex.ee_len = cpu_to_le16(
  2816. ext4_ext_get_actual_len(ex));
  2817. ext4_ext_store_pblock(&zero_ex,
  2818. ext4_ext_pblock(ex));
  2819. }
  2820. } else {
  2821. err = ext4_ext_zeroout(inode, &orig_ex);
  2822. zero_ex.ee_block = orig_ex.ee_block;
  2823. zero_ex.ee_len = cpu_to_le16(
  2824. ext4_ext_get_actual_len(&orig_ex));
  2825. ext4_ext_store_pblock(&zero_ex,
  2826. ext4_ext_pblock(&orig_ex));
  2827. }
  2828. if (err)
  2829. goto fix_extent_len;
  2830. /* update the extent length and mark as initialized */
  2831. ex->ee_len = cpu_to_le16(ee_len);
  2832. ext4_ext_try_to_merge(handle, inode, path, ex);
  2833. err = ext4_ext_dirty(handle, inode, path + path->p_depth);
  2834. if (err)
  2835. goto fix_extent_len;
  2836. /* update extent status tree */
  2837. err = ext4_es_zeroout(inode, &zero_ex);
  2838. goto out;
  2839. } else if (err)
  2840. goto fix_extent_len;
  2841. out:
  2842. ext4_ext_show_leaf(inode, path);
  2843. return err;
  2844. fix_extent_len:
  2845. ex->ee_len = orig_ex.ee_len;
  2846. ext4_ext_dirty(handle, inode, path + depth);
  2847. return err;
  2848. }
  2849. /*
  2850. * ext4_split_extents() splits an extent and mark extent which is covered
  2851. * by @map as split_flags indicates
  2852. *
  2853. * It may result in splitting the extent into multiple extents (upto three)
  2854. * There are three possibilities:
  2855. * a> There is no split required
  2856. * b> Splits in two extents: Split is happening at either end of the extent
  2857. * c> Splits in three extents: Somone is splitting in middle of the extent
  2858. *
  2859. */
  2860. static int ext4_split_extent(handle_t *handle,
  2861. struct inode *inode,
  2862. struct ext4_ext_path *path,
  2863. struct ext4_map_blocks *map,
  2864. int split_flag,
  2865. int flags)
  2866. {
  2867. ext4_lblk_t ee_block;
  2868. struct ext4_extent *ex;
  2869. unsigned int ee_len, depth;
  2870. int err = 0;
  2871. int uninitialized;
  2872. int split_flag1, flags1;
  2873. int allocated = map->m_len;
  2874. depth = ext_depth(inode);
  2875. ex = path[depth].p_ext;
  2876. ee_block = le32_to_cpu(ex->ee_block);
  2877. ee_len = ext4_ext_get_actual_len(ex);
  2878. uninitialized = ext4_ext_is_uninitialized(ex);
  2879. if (map->m_lblk + map->m_len < ee_block + ee_len) {
  2880. split_flag1 = split_flag & EXT4_EXT_MAY_ZEROOUT;
  2881. flags1 = flags | EXT4_GET_BLOCKS_PRE_IO;
  2882. if (uninitialized)
  2883. split_flag1 |= EXT4_EXT_MARK_UNINIT1 |
  2884. EXT4_EXT_MARK_UNINIT2;
  2885. if (split_flag & EXT4_EXT_DATA_VALID2)
  2886. split_flag1 |= EXT4_EXT_DATA_VALID1;
  2887. err = ext4_split_extent_at(handle, inode, path,
  2888. map->m_lblk + map->m_len, split_flag1, flags1);
  2889. if (err)
  2890. goto out;
  2891. } else {
  2892. allocated = ee_len - (map->m_lblk - ee_block);
  2893. }
  2894. /*
  2895. * Update path is required because previous ext4_split_extent_at() may
  2896. * result in split of original leaf or extent zeroout.
  2897. */
  2898. ext4_ext_drop_refs(path);
  2899. path = ext4_ext_find_extent(inode, map->m_lblk, path, 0);
  2900. if (IS_ERR(path))
  2901. return PTR_ERR(path);
  2902. depth = ext_depth(inode);
  2903. ex = path[depth].p_ext;
  2904. uninitialized = ext4_ext_is_uninitialized(ex);
  2905. split_flag1 = 0;
  2906. if (map->m_lblk >= ee_block) {
  2907. split_flag1 = split_flag & EXT4_EXT_DATA_VALID2;
  2908. if (uninitialized) {
  2909. split_flag1 |= EXT4_EXT_MARK_UNINIT1;
  2910. split_flag1 |= split_flag & (EXT4_EXT_MAY_ZEROOUT |
  2911. EXT4_EXT_MARK_UNINIT2);
  2912. }
  2913. err = ext4_split_extent_at(handle, inode, path,
  2914. map->m_lblk, split_flag1, flags);
  2915. if (err)
  2916. goto out;
  2917. }
  2918. ext4_ext_show_leaf(inode, path);
  2919. out:
  2920. return err ? err : allocated;
  2921. }
  2922. /*
  2923. * This function is called by ext4_ext_map_blocks() if someone tries to write
  2924. * to an uninitialized extent. It may result in splitting the uninitialized
  2925. * extent into multiple extents (up to three - one initialized and two
  2926. * uninitialized).
  2927. * There are three possibilities:
  2928. * a> There is no split required: Entire extent should be initialized
  2929. * b> Splits in two extents: Write is happening at either end of the extent
  2930. * c> Splits in three extents: Somone is writing in middle of the extent
  2931. *
  2932. * Pre-conditions:
  2933. * - The extent pointed to by 'path' is uninitialized.
  2934. * - The extent pointed to by 'path' contains a superset
  2935. * of the logical span [map->m_lblk, map->m_lblk + map->m_len).
  2936. *
  2937. * Post-conditions on success:
  2938. * - the returned value is the number of blocks beyond map->l_lblk
  2939. * that are allocated and initialized.
  2940. * It is guaranteed to be >= map->m_len.
  2941. */
  2942. static int ext4_ext_convert_to_initialized(handle_t *handle,
  2943. struct inode *inode,
  2944. struct ext4_map_blocks *map,
  2945. struct ext4_ext_path *path,
  2946. int flags)
  2947. {
  2948. struct ext4_sb_info *sbi;
  2949. struct ext4_extent_header *eh;
  2950. struct ext4_map_blocks split_map;
  2951. struct ext4_extent zero_ex;
  2952. struct ext4_extent *ex, *abut_ex;
  2953. ext4_lblk_t ee_block, eof_block;
  2954. unsigned int ee_len, depth, map_len = map->m_len;
  2955. int allocated = 0, max_zeroout = 0;
  2956. int err = 0;
  2957. int split_flag = 0;
  2958. ext_debug("ext4_ext_convert_to_initialized: inode %lu, logical"
  2959. "block %llu, max_blocks %u\n", inode->i_ino,
  2960. (unsigned long long)map->m_lblk, map_len);
  2961. sbi = EXT4_SB(inode->i_sb);
  2962. eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
  2963. inode->i_sb->s_blocksize_bits;
  2964. if (eof_block < map->m_lblk + map_len)
  2965. eof_block = map->m_lblk + map_len;
  2966. depth = ext_depth(inode);
  2967. eh = path[depth].p_hdr;
  2968. ex = path[depth].p_ext;
  2969. ee_block = le32_to_cpu(ex->ee_block);
  2970. ee_len = ext4_ext_get_actual_len(ex);
  2971. zero_ex.ee_len = 0;
  2972. trace_ext4_ext_convert_to_initialized_enter(inode, map, ex);
  2973. /* Pre-conditions */
  2974. BUG_ON(!ext4_ext_is_uninitialized(ex));
  2975. BUG_ON(!in_range(map->m_lblk, ee_block, ee_len));
  2976. /*
  2977. * Attempt to transfer newly initialized blocks from the currently
  2978. * uninitialized extent to its neighbor. This is much cheaper
  2979. * than an insertion followed by a merge as those involve costly
  2980. * memmove() calls. Transferring to the left is the common case in
  2981. * steady state for workloads doing fallocate(FALLOC_FL_KEEP_SIZE)
  2982. * followed by append writes.
  2983. *
  2984. * Limitations of the current logic:
  2985. * - L1: we do not deal with writes covering the whole extent.
  2986. * This would require removing the extent if the transfer
  2987. * is possible.
  2988. * - L2: we only attempt to merge with an extent stored in the
  2989. * same extent tree node.
  2990. */
  2991. if ((map->m_lblk == ee_block) &&
  2992. /* See if we can merge left */
  2993. (map_len < ee_len) && /*L1*/
  2994. (ex > EXT_FIRST_EXTENT(eh))) { /*L2*/
  2995. ext4_lblk_t prev_lblk;
  2996. ext4_fsblk_t prev_pblk, ee_pblk;
  2997. unsigned int prev_len;
  2998. abut_ex = ex - 1;
  2999. prev_lblk = le32_to_cpu(abut_ex->ee_block);
  3000. prev_len = ext4_ext_get_actual_len(abut_ex);
  3001. prev_pblk = ext4_ext_pblock(abut_ex);
  3002. ee_pblk = ext4_ext_pblock(ex);
  3003. /*
  3004. * A transfer of blocks from 'ex' to 'abut_ex' is allowed
  3005. * upon those conditions:
  3006. * - C1: abut_ex is initialized,
  3007. * - C2: abut_ex is logically abutting ex,
  3008. * - C3: abut_ex is physically abutting ex,
  3009. * - C4: abut_ex can receive the additional blocks without
  3010. * overflowing the (initialized) length limit.
  3011. */
  3012. if ((!ext4_ext_is_uninitialized(abut_ex)) && /*C1*/
  3013. ((prev_lblk + prev_len) == ee_block) && /*C2*/
  3014. ((prev_pblk + prev_len) == ee_pblk) && /*C3*/
  3015. (prev_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
  3016. err = ext4_ext_get_access(handle, inode, path + depth);
  3017. if (err)
  3018. goto out;
  3019. trace_ext4_ext_convert_to_initialized_fastpath(inode,
  3020. map, ex, abut_ex);
  3021. /* Shift the start of ex by 'map_len' blocks */
  3022. ex->ee_block = cpu_to_le32(ee_block + map_len);
  3023. ext4_ext_store_pblock(ex, ee_pblk + map_len);
  3024. ex->ee_len = cpu_to_le16(ee_len - map_len);
  3025. ext4_ext_mark_uninitialized(ex); /* Restore the flag */
  3026. /* Extend abut_ex by 'map_len' blocks */
  3027. abut_ex->ee_len = cpu_to_le16(prev_len + map_len);
  3028. /* Result: number of initialized blocks past m_lblk */
  3029. allocated = map_len;
  3030. }
  3031. } else if (((map->m_lblk + map_len) == (ee_block + ee_len)) &&
  3032. (map_len < ee_len) && /*L1*/
  3033. ex < EXT_LAST_EXTENT(eh)) { /*L2*/
  3034. /* See if we can merge right */
  3035. ext4_lblk_t next_lblk;
  3036. ext4_fsblk_t next_pblk, ee_pblk;
  3037. unsigned int next_len;
  3038. abut_ex = ex + 1;
  3039. next_lblk = le32_to_cpu(abut_ex->ee_block);
  3040. next_len = ext4_ext_get_actual_len(abut_ex);
  3041. next_pblk = ext4_ext_pblock(abut_ex);
  3042. ee_pblk = ext4_ext_pblock(ex);
  3043. /*
  3044. * A transfer of blocks from 'ex' to 'abut_ex' is allowed
  3045. * upon those conditions:
  3046. * - C1: abut_ex is initialized,
  3047. * - C2: abut_ex is logically abutting ex,
  3048. * - C3: abut_ex is physically abutting ex,
  3049. * - C4: abut_ex can receive the additional blocks without
  3050. * overflowing the (initialized) length limit.
  3051. */
  3052. if ((!ext4_ext_is_uninitialized(abut_ex)) && /*C1*/
  3053. ((map->m_lblk + map_len) == next_lblk) && /*C2*/
  3054. ((ee_pblk + ee_len) == next_pblk) && /*C3*/
  3055. (next_len < (EXT_INIT_MAX_LEN - map_len))) { /*C4*/
  3056. err = ext4_ext_get_access(handle, inode, path + depth);
  3057. if (err)
  3058. goto out;
  3059. trace_ext4_ext_convert_to_initialized_fastpath(inode,
  3060. map, ex, abut_ex);
  3061. /* Shift the start of abut_ex by 'map_len' blocks */
  3062. abut_ex->ee_block = cpu_to_le32(next_lblk - map_len);
  3063. ext4_ext_store_pblock(abut_ex, next_pblk - map_len);
  3064. ex->ee_len = cpu_to_le16(ee_len - map_len);
  3065. ext4_ext_mark_uninitialized(ex); /* Restore the flag */
  3066. /* Extend abut_ex by 'map_len' blocks */
  3067. abut_ex->ee_len = cpu_to_le16(next_len + map_len);
  3068. /* Result: number of initialized blocks past m_lblk */
  3069. allocated = map_len;
  3070. }
  3071. }
  3072. if (allocated) {
  3073. /* Mark the block containing both extents as dirty */
  3074. ext4_ext_dirty(handle, inode, path + depth);
  3075. /* Update path to point to the right extent */
  3076. path[depth].p_ext = abut_ex;
  3077. goto out;
  3078. } else
  3079. allocated = ee_len - (map->m_lblk - ee_block);
  3080. WARN_ON(map->m_lblk < ee_block);
  3081. /*
  3082. * It is safe to convert extent to initialized via explicit
  3083. * zeroout only if extent is fully insde i_size or new_size.
  3084. */
  3085. split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
  3086. if (EXT4_EXT_MAY_ZEROOUT & split_flag)
  3087. max_zeroout = sbi->s_extent_max_zeroout_kb >>
  3088. (inode->i_sb->s_blocksize_bits - 10);
  3089. /* If extent is less than s_max_zeroout_kb, zeroout directly */
  3090. if (max_zeroout && (ee_len <= max_zeroout)) {
  3091. err = ext4_ext_zeroout(inode, ex);
  3092. if (err)
  3093. goto out;
  3094. zero_ex.ee_block = ex->ee_block;
  3095. zero_ex.ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex));
  3096. ext4_ext_store_pblock(&zero_ex, ext4_ext_pblock(ex));
  3097. err = ext4_ext_get_access(handle, inode, path + depth);
  3098. if (err)
  3099. goto out;
  3100. ext4_ext_mark_initialized(ex);
  3101. ext4_ext_try_to_merge(handle, inode, path, ex);
  3102. err = ext4_ext_dirty(handle, inode, path + path->p_depth);
  3103. goto out;
  3104. }
  3105. /*
  3106. * four cases:
  3107. * 1. split the extent into three extents.
  3108. * 2. split the extent into two extents, zeroout the first half.
  3109. * 3. split the extent into two extents, zeroout the second half.
  3110. * 4. split the extent into two extents with out zeroout.
  3111. */
  3112. split_map.m_lblk = map->m_lblk;
  3113. split_map.m_len = map->m_len;
  3114. if (max_zeroout && (allocated > map->m_len)) {
  3115. if (allocated <= max_zeroout) {
  3116. /* case 3 */
  3117. zero_ex.ee_block =
  3118. cpu_to_le32(map->m_lblk);
  3119. zero_ex.ee_len = cpu_to_le16(allocated);
  3120. ext4_ext_store_pblock(&zero_ex,
  3121. ext4_ext_pblock(ex) + map->m_lblk - ee_block);
  3122. err = ext4_ext_zeroout(inode, &zero_ex);
  3123. if (err)
  3124. goto out;
  3125. split_map.m_lblk = map->m_lblk;
  3126. split_map.m_len = allocated;
  3127. } else if (map->m_lblk - ee_block + map->m_len < max_zeroout) {
  3128. /* case 2 */
  3129. if (map->m_lblk != ee_block) {
  3130. zero_ex.ee_block = ex->ee_block;
  3131. zero_ex.ee_len = cpu_to_le16(map->m_lblk -
  3132. ee_block);
  3133. ext4_ext_store_pblock(&zero_ex,
  3134. ext4_ext_pblock(ex));
  3135. err = ext4_ext_zeroout(inode, &zero_ex);
  3136. if (err)
  3137. goto out;
  3138. }
  3139. split_map.m_lblk = ee_block;
  3140. split_map.m_len = map->m_lblk - ee_block + map->m_len;
  3141. allocated = map->m_len;
  3142. }
  3143. }
  3144. allocated = ext4_split_extent(handle, inode, path,
  3145. &split_map, split_flag, flags);
  3146. if (allocated < 0)
  3147. err = allocated;
  3148. out:
  3149. /* If we have gotten a failure, don't zero out status tree */
  3150. if (!err)
  3151. err = ext4_es_zeroout(inode, &zero_ex);
  3152. return err ? err : allocated;
  3153. }
  3154. /*
  3155. * This function is called by ext4_ext_map_blocks() from
  3156. * ext4_get_blocks_dio_write() when DIO to write
  3157. * to an uninitialized extent.
  3158. *
  3159. * Writing to an uninitialized extent may result in splitting the uninitialized
  3160. * extent into multiple initialized/uninitialized extents (up to three)
  3161. * There are three possibilities:
  3162. * a> There is no split required: Entire extent should be uninitialized
  3163. * b> Splits in two extents: Write is happening at either end of the extent
  3164. * c> Splits in three extents: Somone is writing in middle of the extent
  3165. *
  3166. * One of more index blocks maybe needed if the extent tree grow after
  3167. * the uninitialized extent split. To prevent ENOSPC occur at the IO
  3168. * complete, we need to split the uninitialized extent before DIO submit
  3169. * the IO. The uninitialized extent called at this time will be split
  3170. * into three uninitialized extent(at most). After IO complete, the part
  3171. * being filled will be convert to initialized by the end_io callback function
  3172. * via ext4_convert_unwritten_extents().
  3173. *
  3174. * Returns the size of uninitialized extent to be written on success.
  3175. */
  3176. static int ext4_split_unwritten_extents(handle_t *handle,
  3177. struct inode *inode,
  3178. struct ext4_map_blocks *map,
  3179. struct ext4_ext_path *path,
  3180. int flags)
  3181. {
  3182. ext4_lblk_t eof_block;
  3183. ext4_lblk_t ee_block;
  3184. struct ext4_extent *ex;
  3185. unsigned int ee_len;
  3186. int split_flag = 0, depth;
  3187. ext_debug("ext4_split_unwritten_extents: inode %lu, logical"
  3188. "block %llu, max_blocks %u\n", inode->i_ino,
  3189. (unsigned long long)map->m_lblk, map->m_len);
  3190. eof_block = (inode->i_size + inode->i_sb->s_blocksize - 1) >>
  3191. inode->i_sb->s_blocksize_bits;
  3192. if (eof_block < map->m_lblk + map->m_len)
  3193. eof_block = map->m_lblk + map->m_len;
  3194. /*
  3195. * It is safe to convert extent to initialized via explicit
  3196. * zeroout only if extent is fully insde i_size or new_size.
  3197. */
  3198. depth = ext_depth(inode);
  3199. ex = path[depth].p_ext;
  3200. ee_block = le32_to_cpu(ex->ee_block);
  3201. ee_len = ext4_ext_get_actual_len(ex);
  3202. split_flag |= ee_block + ee_len <= eof_block ? EXT4_EXT_MAY_ZEROOUT : 0;
  3203. split_flag |= EXT4_EXT_MARK_UNINIT2;
  3204. if (flags & EXT4_GET_BLOCKS_CONVERT)
  3205. split_flag |= EXT4_EXT_DATA_VALID2;
  3206. flags |= EXT4_GET_BLOCKS_PRE_IO;
  3207. return ext4_split_extent(handle, inode, path, map, split_flag, flags);
  3208. }
  3209. static int ext4_convert_unwritten_extents_endio(handle_t *handle,
  3210. struct inode *inode,
  3211. struct ext4_map_blocks *map,
  3212. struct ext4_ext_path *path)
  3213. {
  3214. struct ext4_extent *ex;
  3215. ext4_lblk_t ee_block;
  3216. unsigned int ee_len;
  3217. int depth;
  3218. int err = 0;
  3219. depth = ext_depth(inode);
  3220. ex = path[depth].p_ext;
  3221. ee_block = le32_to_cpu(ex->ee_block);
  3222. ee_len = ext4_ext_get_actual_len(ex);
  3223. ext_debug("ext4_convert_unwritten_extents_endio: inode %lu, logical"
  3224. "block %llu, max_blocks %u\n", inode->i_ino,
  3225. (unsigned long long)ee_block, ee_len);
  3226. /* If extent is larger than requested it is a clear sign that we still
  3227. * have some extent state machine issues left. So extent_split is still
  3228. * required.
  3229. * TODO: Once all related issues will be fixed this situation should be
  3230. * illegal.
  3231. */
  3232. if (ee_block != map->m_lblk || ee_len > map->m_len) {
  3233. #ifdef EXT4_DEBUG
  3234. ext4_warning("Inode (%ld) finished: extent logical block %llu,"
  3235. " len %u; IO logical block %llu, len %u\n",
  3236. inode->i_ino, (unsigned long long)ee_block, ee_len,
  3237. (unsigned long long)map->m_lblk, map->m_len);
  3238. #endif
  3239. err = ext4_split_unwritten_extents(handle, inode, map, path,
  3240. EXT4_GET_BLOCKS_CONVERT);
  3241. if (err < 0)
  3242. goto out;
  3243. ext4_ext_drop_refs(path);
  3244. path = ext4_ext_find_extent(inode, map->m_lblk, path, 0);
  3245. if (IS_ERR(path)) {
  3246. err = PTR_ERR(path);
  3247. goto out;
  3248. }
  3249. depth = ext_depth(inode);
  3250. ex = path[depth].p_ext;
  3251. }
  3252. err = ext4_ext_get_access(handle, inode, path + depth);
  3253. if (err)
  3254. goto out;
  3255. /* first mark the extent as initialized */
  3256. ext4_ext_mark_initialized(ex);
  3257. /* note: ext4_ext_correct_indexes() isn't needed here because
  3258. * borders are not changed
  3259. */
  3260. ext4_ext_try_to_merge(handle, inode, path, ex);
  3261. /* Mark modified extent as dirty */
  3262. err = ext4_ext_dirty(handle, inode, path + path->p_depth);
  3263. out:
  3264. ext4_ext_show_leaf(inode, path);
  3265. return err;
  3266. }
  3267. static void unmap_underlying_metadata_blocks(struct block_device *bdev,
  3268. sector_t block, int count)
  3269. {
  3270. int i;
  3271. for (i = 0; i < count; i++)
  3272. unmap_underlying_metadata(bdev, block + i);
  3273. }
  3274. /*
  3275. * Handle EOFBLOCKS_FL flag, clearing it if necessary
  3276. */
  3277. static int check_eofblocks_fl(handle_t *handle, struct inode *inode,
  3278. ext4_lblk_t lblk,
  3279. struct ext4_ext_path *path,
  3280. unsigned int len)
  3281. {
  3282. int i, depth;
  3283. struct ext4_extent_header *eh;
  3284. struct ext4_extent *last_ex;
  3285. if (!ext4_test_inode_flag(inode, EXT4_INODE_EOFBLOCKS))
  3286. return 0;
  3287. depth = ext_depth(inode);
  3288. eh = path[depth].p_hdr;
  3289. /*
  3290. * We're going to remove EOFBLOCKS_FL entirely in future so we
  3291. * do not care for this case anymore. Simply remove the flag
  3292. * if there are no extents.
  3293. */
  3294. if (unlikely(!eh->eh_entries))
  3295. goto out;
  3296. last_ex = EXT_LAST_EXTENT(eh);
  3297. /*
  3298. * We should clear the EOFBLOCKS_FL flag if we are writing the
  3299. * last block in the last extent in the file. We test this by
  3300. * first checking to see if the caller to
  3301. * ext4_ext_get_blocks() was interested in the last block (or
  3302. * a block beyond the last block) in the current extent. If
  3303. * this turns out to be false, we can bail out from this
  3304. * function immediately.
  3305. */
  3306. if (lblk + len < le32_to_cpu(last_ex->ee_block) +
  3307. ext4_ext_get_actual_len(last_ex))
  3308. return 0;
  3309. /*
  3310. * If the caller does appear to be planning to write at or
  3311. * beyond the end of the current extent, we then test to see
  3312. * if the current extent is the last extent in the file, by
  3313. * checking to make sure it was reached via the rightmost node
  3314. * at each level of the tree.
  3315. */
  3316. for (i = depth-1; i >= 0; i--)
  3317. if (path[i].p_idx != EXT_LAST_INDEX(path[i].p_hdr))
  3318. return 0;
  3319. out:
  3320. ext4_clear_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
  3321. return ext4_mark_inode_dirty(handle, inode);
  3322. }
  3323. /**
  3324. * ext4_find_delalloc_range: find delayed allocated block in the given range.
  3325. *
  3326. * Return 1 if there is a delalloc block in the range, otherwise 0.
  3327. */
  3328. int ext4_find_delalloc_range(struct inode *inode,
  3329. ext4_lblk_t lblk_start,
  3330. ext4_lblk_t lblk_end)
  3331. {
  3332. struct extent_status es;
  3333. ext4_es_find_delayed_extent_range(inode, lblk_start, lblk_end, &es);
  3334. if (es.es_len == 0)
  3335. return 0; /* there is no delay extent in this tree */
  3336. else if (es.es_lblk <= lblk_start &&
  3337. lblk_start < es.es_lblk + es.es_len)
  3338. return 1;
  3339. else if (lblk_start <= es.es_lblk && es.es_lblk <= lblk_end)
  3340. return 1;
  3341. else
  3342. return 0;
  3343. }
  3344. int ext4_find_delalloc_cluster(struct inode *inode, ext4_lblk_t lblk)
  3345. {
  3346. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  3347. ext4_lblk_t lblk_start, lblk_end;
  3348. lblk_start = lblk & (~(sbi->s_cluster_ratio - 1));
  3349. lblk_end = lblk_start + sbi->s_cluster_ratio - 1;
  3350. return ext4_find_delalloc_range(inode, lblk_start, lblk_end);
  3351. }
  3352. /**
  3353. * Determines how many complete clusters (out of those specified by the 'map')
  3354. * are under delalloc and were reserved quota for.
  3355. * This function is called when we are writing out the blocks that were
  3356. * originally written with their allocation delayed, but then the space was
  3357. * allocated using fallocate() before the delayed allocation could be resolved.
  3358. * The cases to look for are:
  3359. * ('=' indicated delayed allocated blocks
  3360. * '-' indicates non-delayed allocated blocks)
  3361. * (a) partial clusters towards beginning and/or end outside of allocated range
  3362. * are not delalloc'ed.
  3363. * Ex:
  3364. * |----c---=|====c====|====c====|===-c----|
  3365. * |++++++ allocated ++++++|
  3366. * ==> 4 complete clusters in above example
  3367. *
  3368. * (b) partial cluster (outside of allocated range) towards either end is
  3369. * marked for delayed allocation. In this case, we will exclude that
  3370. * cluster.
  3371. * Ex:
  3372. * |----====c========|========c========|
  3373. * |++++++ allocated ++++++|
  3374. * ==> 1 complete clusters in above example
  3375. *
  3376. * Ex:
  3377. * |================c================|
  3378. * |++++++ allocated ++++++|
  3379. * ==> 0 complete clusters in above example
  3380. *
  3381. * The ext4_da_update_reserve_space will be called only if we
  3382. * determine here that there were some "entire" clusters that span
  3383. * this 'allocated' range.
  3384. * In the non-bigalloc case, this function will just end up returning num_blks
  3385. * without ever calling ext4_find_delalloc_range.
  3386. */
  3387. static unsigned int
  3388. get_reserved_cluster_alloc(struct inode *inode, ext4_lblk_t lblk_start,
  3389. unsigned int num_blks)
  3390. {
  3391. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  3392. ext4_lblk_t alloc_cluster_start, alloc_cluster_end;
  3393. ext4_lblk_t lblk_from, lblk_to, c_offset;
  3394. unsigned int allocated_clusters = 0;
  3395. alloc_cluster_start = EXT4_B2C(sbi, lblk_start);
  3396. alloc_cluster_end = EXT4_B2C(sbi, lblk_start + num_blks - 1);
  3397. /* max possible clusters for this allocation */
  3398. allocated_clusters = alloc_cluster_end - alloc_cluster_start + 1;
  3399. trace_ext4_get_reserved_cluster_alloc(inode, lblk_start, num_blks);
  3400. /* Check towards left side */
  3401. c_offset = lblk_start & (sbi->s_cluster_ratio - 1);
  3402. if (c_offset) {
  3403. lblk_from = lblk_start & (~(sbi->s_cluster_ratio - 1));
  3404. lblk_to = lblk_from + c_offset - 1;
  3405. if (ext4_find_delalloc_range(inode, lblk_from, lblk_to))
  3406. allocated_clusters--;
  3407. }
  3408. /* Now check towards right. */
  3409. c_offset = (lblk_start + num_blks) & (sbi->s_cluster_ratio - 1);
  3410. if (allocated_clusters && c_offset) {
  3411. lblk_from = lblk_start + num_blks;
  3412. lblk_to = lblk_from + (sbi->s_cluster_ratio - c_offset) - 1;
  3413. if (ext4_find_delalloc_range(inode, lblk_from, lblk_to))
  3414. allocated_clusters--;
  3415. }
  3416. return allocated_clusters;
  3417. }
  3418. static int
  3419. ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
  3420. struct ext4_map_blocks *map,
  3421. struct ext4_ext_path *path, int flags,
  3422. unsigned int allocated, ext4_fsblk_t newblock)
  3423. {
  3424. int ret = 0;
  3425. int err = 0;
  3426. ext4_io_end_t *io = ext4_inode_aio(inode);
  3427. ext_debug("ext4_ext_handle_uninitialized_extents: inode %lu, logical "
  3428. "block %llu, max_blocks %u, flags %x, allocated %u\n",
  3429. inode->i_ino, (unsigned long long)map->m_lblk, map->m_len,
  3430. flags, allocated);
  3431. ext4_ext_show_leaf(inode, path);
  3432. /*
  3433. * When writing into uninitialized space, we should not fail to
  3434. * allocate metadata blocks for the new extent block if needed.
  3435. */
  3436. flags |= EXT4_GET_BLOCKS_METADATA_NOFAIL;
  3437. trace_ext4_ext_handle_uninitialized_extents(inode, map, flags,
  3438. allocated, newblock);
  3439. /* get_block() before submit the IO, split the extent */
  3440. if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
  3441. ret = ext4_split_unwritten_extents(handle, inode, map,
  3442. path, flags);
  3443. if (ret <= 0)
  3444. goto out;
  3445. /*
  3446. * Flag the inode(non aio case) or end_io struct (aio case)
  3447. * that this IO needs to conversion to written when IO is
  3448. * completed
  3449. */
  3450. if (io)
  3451. ext4_set_io_unwritten_flag(inode, io);
  3452. else
  3453. ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
  3454. map->m_flags |= EXT4_MAP_UNWRITTEN;
  3455. if (ext4_should_dioread_nolock(inode))
  3456. map->m_flags |= EXT4_MAP_UNINIT;
  3457. goto out;
  3458. }
  3459. /* IO end_io complete, convert the filled extent to written */
  3460. if ((flags & EXT4_GET_BLOCKS_CONVERT)) {
  3461. ret = ext4_convert_unwritten_extents_endio(handle, inode, map,
  3462. path);
  3463. if (ret >= 0) {
  3464. ext4_update_inode_fsync_trans(handle, inode, 1);
  3465. err = check_eofblocks_fl(handle, inode, map->m_lblk,
  3466. path, map->m_len);
  3467. } else
  3468. err = ret;
  3469. map->m_flags |= EXT4_MAP_MAPPED;
  3470. if (allocated > map->m_len)
  3471. allocated = map->m_len;
  3472. map->m_len = allocated;
  3473. goto out2;
  3474. }
  3475. /* buffered IO case */
  3476. /*
  3477. * repeat fallocate creation request
  3478. * we already have an unwritten extent
  3479. */
  3480. if (flags & EXT4_GET_BLOCKS_UNINIT_EXT) {
  3481. map->m_flags |= EXT4_MAP_UNWRITTEN;
  3482. goto map_out;
  3483. }
  3484. /* buffered READ or buffered write_begin() lookup */
  3485. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
  3486. /*
  3487. * We have blocks reserved already. We
  3488. * return allocated blocks so that delalloc
  3489. * won't do block reservation for us. But
  3490. * the buffer head will be unmapped so that
  3491. * a read from the block returns 0s.
  3492. */
  3493. map->m_flags |= EXT4_MAP_UNWRITTEN;
  3494. goto out1;
  3495. }
  3496. /* buffered write, writepage time, convert*/
  3497. ret = ext4_ext_convert_to_initialized(handle, inode, map, path, flags);
  3498. if (ret >= 0)
  3499. ext4_update_inode_fsync_trans(handle, inode, 1);
  3500. out:
  3501. if (ret <= 0) {
  3502. err = ret;
  3503. goto out2;
  3504. } else
  3505. allocated = ret;
  3506. map->m_flags |= EXT4_MAP_NEW;
  3507. /*
  3508. * if we allocated more blocks than requested
  3509. * we need to make sure we unmap the extra block
  3510. * allocated. The actual needed block will get
  3511. * unmapped later when we find the buffer_head marked
  3512. * new.
  3513. */
  3514. if (allocated > map->m_len) {
  3515. unmap_underlying_metadata_blocks(inode->i_sb->s_bdev,
  3516. newblock + map->m_len,
  3517. allocated - map->m_len);
  3518. allocated = map->m_len;
  3519. }
  3520. map->m_len = allocated;
  3521. /*
  3522. * If we have done fallocate with the offset that is already
  3523. * delayed allocated, we would have block reservation
  3524. * and quota reservation done in the delayed write path.
  3525. * But fallocate would have already updated quota and block
  3526. * count for this offset. So cancel these reservation
  3527. */
  3528. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
  3529. unsigned int reserved_clusters;
  3530. reserved_clusters = get_reserved_cluster_alloc(inode,
  3531. map->m_lblk, map->m_len);
  3532. if (reserved_clusters)
  3533. ext4_da_update_reserve_space(inode,
  3534. reserved_clusters,
  3535. 0);
  3536. }
  3537. map_out:
  3538. map->m_flags |= EXT4_MAP_MAPPED;
  3539. if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0) {
  3540. err = check_eofblocks_fl(handle, inode, map->m_lblk, path,
  3541. map->m_len);
  3542. if (err < 0)
  3543. goto out2;
  3544. }
  3545. out1:
  3546. if (allocated > map->m_len)
  3547. allocated = map->m_len;
  3548. ext4_ext_show_leaf(inode, path);
  3549. map->m_pblk = newblock;
  3550. map->m_len = allocated;
  3551. out2:
  3552. if (path) {
  3553. ext4_ext_drop_refs(path);
  3554. kfree(path);
  3555. }
  3556. return err ? err : allocated;
  3557. }
  3558. /*
  3559. * get_implied_cluster_alloc - check to see if the requested
  3560. * allocation (in the map structure) overlaps with a cluster already
  3561. * allocated in an extent.
  3562. * @sb The filesystem superblock structure
  3563. * @map The requested lblk->pblk mapping
  3564. * @ex The extent structure which might contain an implied
  3565. * cluster allocation
  3566. *
  3567. * This function is called by ext4_ext_map_blocks() after we failed to
  3568. * find blocks that were already in the inode's extent tree. Hence,
  3569. * we know that the beginning of the requested region cannot overlap
  3570. * the extent from the inode's extent tree. There are three cases we
  3571. * want to catch. The first is this case:
  3572. *
  3573. * |--- cluster # N--|
  3574. * |--- extent ---| |---- requested region ---|
  3575. * |==========|
  3576. *
  3577. * The second case that we need to test for is this one:
  3578. *
  3579. * |--------- cluster # N ----------------|
  3580. * |--- requested region --| |------- extent ----|
  3581. * |=======================|
  3582. *
  3583. * The third case is when the requested region lies between two extents
  3584. * within the same cluster:
  3585. * |------------- cluster # N-------------|
  3586. * |----- ex -----| |---- ex_right ----|
  3587. * |------ requested region ------|
  3588. * |================|
  3589. *
  3590. * In each of the above cases, we need to set the map->m_pblk and
  3591. * map->m_len so it corresponds to the return the extent labelled as
  3592. * "|====|" from cluster #N, since it is already in use for data in
  3593. * cluster EXT4_B2C(sbi, map->m_lblk). We will then return 1 to
  3594. * signal to ext4_ext_map_blocks() that map->m_pblk should be treated
  3595. * as a new "allocated" block region. Otherwise, we will return 0 and
  3596. * ext4_ext_map_blocks() will then allocate one or more new clusters
  3597. * by calling ext4_mb_new_blocks().
  3598. */
  3599. static int get_implied_cluster_alloc(struct super_block *sb,
  3600. struct ext4_map_blocks *map,
  3601. struct ext4_extent *ex,
  3602. struct ext4_ext_path *path)
  3603. {
  3604. struct ext4_sb_info *sbi = EXT4_SB(sb);
  3605. ext4_lblk_t c_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
  3606. ext4_lblk_t ex_cluster_start, ex_cluster_end;
  3607. ext4_lblk_t rr_cluster_start;
  3608. ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
  3609. ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
  3610. unsigned short ee_len = ext4_ext_get_actual_len(ex);
  3611. /* The extent passed in that we are trying to match */
  3612. ex_cluster_start = EXT4_B2C(sbi, ee_block);
  3613. ex_cluster_end = EXT4_B2C(sbi, ee_block + ee_len - 1);
  3614. /* The requested region passed into ext4_map_blocks() */
  3615. rr_cluster_start = EXT4_B2C(sbi, map->m_lblk);
  3616. if ((rr_cluster_start == ex_cluster_end) ||
  3617. (rr_cluster_start == ex_cluster_start)) {
  3618. if (rr_cluster_start == ex_cluster_end)
  3619. ee_start += ee_len - 1;
  3620. map->m_pblk = (ee_start & ~(sbi->s_cluster_ratio - 1)) +
  3621. c_offset;
  3622. map->m_len = min(map->m_len,
  3623. (unsigned) sbi->s_cluster_ratio - c_offset);
  3624. /*
  3625. * Check for and handle this case:
  3626. *
  3627. * |--------- cluster # N-------------|
  3628. * |------- extent ----|
  3629. * |--- requested region ---|
  3630. * |===========|
  3631. */
  3632. if (map->m_lblk < ee_block)
  3633. map->m_len = min(map->m_len, ee_block - map->m_lblk);
  3634. /*
  3635. * Check for the case where there is already another allocated
  3636. * block to the right of 'ex' but before the end of the cluster.
  3637. *
  3638. * |------------- cluster # N-------------|
  3639. * |----- ex -----| |---- ex_right ----|
  3640. * |------ requested region ------|
  3641. * |================|
  3642. */
  3643. if (map->m_lblk > ee_block) {
  3644. ext4_lblk_t next = ext4_ext_next_allocated_block(path);
  3645. map->m_len = min(map->m_len, next - map->m_lblk);
  3646. }
  3647. trace_ext4_get_implied_cluster_alloc_exit(sb, map, 1);
  3648. return 1;
  3649. }
  3650. trace_ext4_get_implied_cluster_alloc_exit(sb, map, 0);
  3651. return 0;
  3652. }
  3653. /*
  3654. * Block allocation/map/preallocation routine for extents based files
  3655. *
  3656. *
  3657. * Need to be called with
  3658. * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
  3659. * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
  3660. *
  3661. * return > 0, number of of blocks already mapped/allocated
  3662. * if create == 0 and these are pre-allocated blocks
  3663. * buffer head is unmapped
  3664. * otherwise blocks are mapped
  3665. *
  3666. * return = 0, if plain look up failed (blocks have not been allocated)
  3667. * buffer head is unmapped
  3668. *
  3669. * return < 0, error case.
  3670. */
  3671. int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
  3672. struct ext4_map_blocks *map, int flags)
  3673. {
  3674. struct ext4_ext_path *path = NULL;
  3675. struct ext4_extent newex, *ex, *ex2;
  3676. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  3677. ext4_fsblk_t newblock = 0;
  3678. int free_on_err = 0, err = 0, depth;
  3679. unsigned int allocated = 0, offset = 0;
  3680. unsigned int allocated_clusters = 0;
  3681. struct ext4_allocation_request ar;
  3682. ext4_io_end_t *io = ext4_inode_aio(inode);
  3683. ext4_lblk_t cluster_offset;
  3684. int set_unwritten = 0;
  3685. ext_debug("blocks %u/%u requested for inode %lu\n",
  3686. map->m_lblk, map->m_len, inode->i_ino);
  3687. trace_ext4_ext_map_blocks_enter(inode, map->m_lblk, map->m_len, flags);
  3688. /* find extent for this block */
  3689. path = ext4_ext_find_extent(inode, map->m_lblk, NULL, 0);
  3690. if (IS_ERR(path)) {
  3691. err = PTR_ERR(path);
  3692. path = NULL;
  3693. goto out2;
  3694. }
  3695. depth = ext_depth(inode);
  3696. /*
  3697. * consistent leaf must not be empty;
  3698. * this situation is possible, though, _during_ tree modification;
  3699. * this is why assert can't be put in ext4_ext_find_extent()
  3700. */
  3701. if (unlikely(path[depth].p_ext == NULL && depth != 0)) {
  3702. EXT4_ERROR_INODE(inode, "bad extent address "
  3703. "lblock: %lu, depth: %d pblock %lld",
  3704. (unsigned long) map->m_lblk, depth,
  3705. path[depth].p_block);
  3706. err = -EIO;
  3707. goto out2;
  3708. }
  3709. ex = path[depth].p_ext;
  3710. if (ex) {
  3711. ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
  3712. ext4_fsblk_t ee_start = ext4_ext_pblock(ex);
  3713. unsigned short ee_len;
  3714. /*
  3715. * Uninitialized extents are treated as holes, except that
  3716. * we split out initialized portions during a write.
  3717. */
  3718. ee_len = ext4_ext_get_actual_len(ex);
  3719. trace_ext4_ext_show_extent(inode, ee_block, ee_start, ee_len);
  3720. /* if found extent covers block, simply return it */
  3721. if (in_range(map->m_lblk, ee_block, ee_len)) {
  3722. newblock = map->m_lblk - ee_block + ee_start;
  3723. /* number of remaining blocks in the extent */
  3724. allocated = ee_len - (map->m_lblk - ee_block);
  3725. ext_debug("%u fit into %u:%d -> %llu\n", map->m_lblk,
  3726. ee_block, ee_len, newblock);
  3727. if (!ext4_ext_is_uninitialized(ex))
  3728. goto out;
  3729. allocated = ext4_ext_handle_uninitialized_extents(
  3730. handle, inode, map, path, flags,
  3731. allocated, newblock);
  3732. goto out3;
  3733. }
  3734. }
  3735. if ((sbi->s_cluster_ratio > 1) &&
  3736. ext4_find_delalloc_cluster(inode, map->m_lblk))
  3737. map->m_flags |= EXT4_MAP_FROM_CLUSTER;
  3738. /*
  3739. * requested block isn't allocated yet;
  3740. * we couldn't try to create block if create flag is zero
  3741. */
  3742. if ((flags & EXT4_GET_BLOCKS_CREATE) == 0) {
  3743. /*
  3744. * put just found gap into cache to speed up
  3745. * subsequent requests
  3746. */
  3747. if ((flags & EXT4_GET_BLOCKS_NO_PUT_HOLE) == 0)
  3748. ext4_ext_put_gap_in_cache(inode, path, map->m_lblk);
  3749. goto out2;
  3750. }
  3751. /*
  3752. * Okay, we need to do block allocation.
  3753. */
  3754. map->m_flags &= ~EXT4_MAP_FROM_CLUSTER;
  3755. newex.ee_block = cpu_to_le32(map->m_lblk);
  3756. cluster_offset = map->m_lblk & (sbi->s_cluster_ratio-1);
  3757. /*
  3758. * If we are doing bigalloc, check to see if the extent returned
  3759. * by ext4_ext_find_extent() implies a cluster we can use.
  3760. */
  3761. if (cluster_offset && ex &&
  3762. get_implied_cluster_alloc(inode->i_sb, map, ex, path)) {
  3763. ar.len = allocated = map->m_len;
  3764. newblock = map->m_pblk;
  3765. map->m_flags |= EXT4_MAP_FROM_CLUSTER;
  3766. goto got_allocated_blocks;
  3767. }
  3768. /* find neighbour allocated blocks */
  3769. ar.lleft = map->m_lblk;
  3770. err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
  3771. if (err)
  3772. goto out2;
  3773. ar.lright = map->m_lblk;
  3774. ex2 = NULL;
  3775. err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright, &ex2);
  3776. if (err)
  3777. goto out2;
  3778. /* Check if the extent after searching to the right implies a
  3779. * cluster we can use. */
  3780. if ((sbi->s_cluster_ratio > 1) && ex2 &&
  3781. get_implied_cluster_alloc(inode->i_sb, map, ex2, path)) {
  3782. ar.len = allocated = map->m_len;
  3783. newblock = map->m_pblk;
  3784. map->m_flags |= EXT4_MAP_FROM_CLUSTER;
  3785. goto got_allocated_blocks;
  3786. }
  3787. /*
  3788. * See if request is beyond maximum number of blocks we can have in
  3789. * a single extent. For an initialized extent this limit is
  3790. * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
  3791. * EXT_UNINIT_MAX_LEN.
  3792. */
  3793. if (map->m_len > EXT_INIT_MAX_LEN &&
  3794. !(flags & EXT4_GET_BLOCKS_UNINIT_EXT))
  3795. map->m_len = EXT_INIT_MAX_LEN;
  3796. else if (map->m_len > EXT_UNINIT_MAX_LEN &&
  3797. (flags & EXT4_GET_BLOCKS_UNINIT_EXT))
  3798. map->m_len = EXT_UNINIT_MAX_LEN;
  3799. /* Check if we can really insert (m_lblk)::(m_lblk + m_len) extent */
  3800. newex.ee_len = cpu_to_le16(map->m_len);
  3801. err = ext4_ext_check_overlap(sbi, inode, &newex, path);
  3802. if (err)
  3803. allocated = ext4_ext_get_actual_len(&newex);
  3804. else
  3805. allocated = map->m_len;
  3806. /* allocate new block */
  3807. ar.inode = inode;
  3808. ar.goal = ext4_ext_find_goal(inode, path, map->m_lblk);
  3809. ar.logical = map->m_lblk;
  3810. /*
  3811. * We calculate the offset from the beginning of the cluster
  3812. * for the logical block number, since when we allocate a
  3813. * physical cluster, the physical block should start at the
  3814. * same offset from the beginning of the cluster. This is
  3815. * needed so that future calls to get_implied_cluster_alloc()
  3816. * work correctly.
  3817. */
  3818. offset = map->m_lblk & (sbi->s_cluster_ratio - 1);
  3819. ar.len = EXT4_NUM_B2C(sbi, offset+allocated);
  3820. ar.goal -= offset;
  3821. ar.logical -= offset;
  3822. if (S_ISREG(inode->i_mode))
  3823. ar.flags = EXT4_MB_HINT_DATA;
  3824. else
  3825. /* disable in-core preallocation for non-regular files */
  3826. ar.flags = 0;
  3827. if (flags & EXT4_GET_BLOCKS_NO_NORMALIZE)
  3828. ar.flags |= EXT4_MB_HINT_NOPREALLOC;
  3829. newblock = ext4_mb_new_blocks(handle, &ar, &err);
  3830. if (!newblock)
  3831. goto out2;
  3832. ext_debug("allocate new block: goal %llu, found %llu/%u\n",
  3833. ar.goal, newblock, allocated);
  3834. free_on_err = 1;
  3835. allocated_clusters = ar.len;
  3836. ar.len = EXT4_C2B(sbi, ar.len) - offset;
  3837. if (ar.len > allocated)
  3838. ar.len = allocated;
  3839. got_allocated_blocks:
  3840. /* try to insert new extent into found leaf and return */
  3841. ext4_ext_store_pblock(&newex, newblock + offset);
  3842. newex.ee_len = cpu_to_le16(ar.len);
  3843. /* Mark uninitialized */
  3844. if (flags & EXT4_GET_BLOCKS_UNINIT_EXT){
  3845. ext4_ext_mark_uninitialized(&newex);
  3846. map->m_flags |= EXT4_MAP_UNWRITTEN;
  3847. /*
  3848. * io_end structure was created for every IO write to an
  3849. * uninitialized extent. To avoid unnecessary conversion,
  3850. * here we flag the IO that really needs the conversion.
  3851. * For non asycn direct IO case, flag the inode state
  3852. * that we need to perform conversion when IO is done.
  3853. */
  3854. if ((flags & EXT4_GET_BLOCKS_PRE_IO))
  3855. set_unwritten = 1;
  3856. if (ext4_should_dioread_nolock(inode))
  3857. map->m_flags |= EXT4_MAP_UNINIT;
  3858. }
  3859. err = 0;
  3860. if ((flags & EXT4_GET_BLOCKS_KEEP_SIZE) == 0)
  3861. err = check_eofblocks_fl(handle, inode, map->m_lblk,
  3862. path, ar.len);
  3863. if (!err)
  3864. err = ext4_ext_insert_extent(handle, inode, path,
  3865. &newex, flags);
  3866. if (!err && set_unwritten) {
  3867. if (io)
  3868. ext4_set_io_unwritten_flag(inode, io);
  3869. else
  3870. ext4_set_inode_state(inode,
  3871. EXT4_STATE_DIO_UNWRITTEN);
  3872. }
  3873. if (err && free_on_err) {
  3874. int fb_flags = flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE ?
  3875. EXT4_FREE_BLOCKS_NO_QUOT_UPDATE : 0;
  3876. /* free data blocks we just allocated */
  3877. /* not a good idea to call discard here directly,
  3878. * but otherwise we'd need to call it every free() */
  3879. ext4_discard_preallocations(inode);
  3880. ext4_free_blocks(handle, inode, NULL, newblock,
  3881. EXT4_C2B(sbi, allocated_clusters), fb_flags);
  3882. goto out2;
  3883. }
  3884. /* previous routine could use block we allocated */
  3885. newblock = ext4_ext_pblock(&newex);
  3886. allocated = ext4_ext_get_actual_len(&newex);
  3887. if (allocated > map->m_len)
  3888. allocated = map->m_len;
  3889. map->m_flags |= EXT4_MAP_NEW;
  3890. /*
  3891. * Update reserved blocks/metadata blocks after successful
  3892. * block allocation which had been deferred till now.
  3893. */
  3894. if (flags & EXT4_GET_BLOCKS_DELALLOC_RESERVE) {
  3895. unsigned int reserved_clusters;
  3896. /*
  3897. * Check how many clusters we had reserved this allocated range
  3898. */
  3899. reserved_clusters = get_reserved_cluster_alloc(inode,
  3900. map->m_lblk, allocated);
  3901. if (map->m_flags & EXT4_MAP_FROM_CLUSTER) {
  3902. if (reserved_clusters) {
  3903. /*
  3904. * We have clusters reserved for this range.
  3905. * But since we are not doing actual allocation
  3906. * and are simply using blocks from previously
  3907. * allocated cluster, we should release the
  3908. * reservation and not claim quota.
  3909. */
  3910. ext4_da_update_reserve_space(inode,
  3911. reserved_clusters, 0);
  3912. }
  3913. } else {
  3914. BUG_ON(allocated_clusters < reserved_clusters);
  3915. if (reserved_clusters < allocated_clusters) {
  3916. struct ext4_inode_info *ei = EXT4_I(inode);
  3917. int reservation = allocated_clusters -
  3918. reserved_clusters;
  3919. /*
  3920. * It seems we claimed few clusters outside of
  3921. * the range of this allocation. We should give
  3922. * it back to the reservation pool. This can
  3923. * happen in the following case:
  3924. *
  3925. * * Suppose s_cluster_ratio is 4 (i.e., each
  3926. * cluster has 4 blocks. Thus, the clusters
  3927. * are [0-3],[4-7],[8-11]...
  3928. * * First comes delayed allocation write for
  3929. * logical blocks 10 & 11. Since there were no
  3930. * previous delayed allocated blocks in the
  3931. * range [8-11], we would reserve 1 cluster
  3932. * for this write.
  3933. * * Next comes write for logical blocks 3 to 8.
  3934. * In this case, we will reserve 2 clusters
  3935. * (for [0-3] and [4-7]; and not for [8-11] as
  3936. * that range has a delayed allocated blocks.
  3937. * Thus total reserved clusters now becomes 3.
  3938. * * Now, during the delayed allocation writeout
  3939. * time, we will first write blocks [3-8] and
  3940. * allocate 3 clusters for writing these
  3941. * blocks. Also, we would claim all these
  3942. * three clusters above.
  3943. * * Now when we come here to writeout the
  3944. * blocks [10-11], we would expect to claim
  3945. * the reservation of 1 cluster we had made
  3946. * (and we would claim it since there are no
  3947. * more delayed allocated blocks in the range
  3948. * [8-11]. But our reserved cluster count had
  3949. * already gone to 0.
  3950. *
  3951. * Thus, at the step 4 above when we determine
  3952. * that there are still some unwritten delayed
  3953. * allocated blocks outside of our current
  3954. * block range, we should increment the
  3955. * reserved clusters count so that when the
  3956. * remaining blocks finally gets written, we
  3957. * could claim them.
  3958. */
  3959. dquot_reserve_block(inode,
  3960. EXT4_C2B(sbi, reservation));
  3961. spin_lock(&ei->i_block_reservation_lock);
  3962. ei->i_reserved_data_blocks += reservation;
  3963. spin_unlock(&ei->i_block_reservation_lock);
  3964. }
  3965. /*
  3966. * We will claim quota for all newly allocated blocks.
  3967. * We're updating the reserved space *after* the
  3968. * correction above so we do not accidentally free
  3969. * all the metadata reservation because we might
  3970. * actually need it later on.
  3971. */
  3972. ext4_da_update_reserve_space(inode, allocated_clusters,
  3973. 1);
  3974. }
  3975. }
  3976. /*
  3977. * Cache the extent and update transaction to commit on fdatasync only
  3978. * when it is _not_ an uninitialized extent.
  3979. */
  3980. if ((flags & EXT4_GET_BLOCKS_UNINIT_EXT) == 0)
  3981. ext4_update_inode_fsync_trans(handle, inode, 1);
  3982. else
  3983. ext4_update_inode_fsync_trans(handle, inode, 0);
  3984. out:
  3985. if (allocated > map->m_len)
  3986. allocated = map->m_len;
  3987. ext4_ext_show_leaf(inode, path);
  3988. map->m_flags |= EXT4_MAP_MAPPED;
  3989. map->m_pblk = newblock;
  3990. map->m_len = allocated;
  3991. out2:
  3992. if (path) {
  3993. ext4_ext_drop_refs(path);
  3994. kfree(path);
  3995. }
  3996. out3:
  3997. trace_ext4_ext_map_blocks_exit(inode, flags, map,
  3998. err ? err : allocated);
  3999. ext4_es_lru_add(inode);
  4000. return err ? err : allocated;
  4001. }
  4002. void ext4_ext_truncate(handle_t *handle, struct inode *inode)
  4003. {
  4004. struct super_block *sb = inode->i_sb;
  4005. ext4_lblk_t last_block;
  4006. int err = 0;
  4007. /*
  4008. * TODO: optimization is possible here.
  4009. * Probably we need not scan at all,
  4010. * because page truncation is enough.
  4011. */
  4012. /* we have to know where to truncate from in crash case */
  4013. EXT4_I(inode)->i_disksize = inode->i_size;
  4014. ext4_mark_inode_dirty(handle, inode);
  4015. last_block = (inode->i_size + sb->s_blocksize - 1)
  4016. >> EXT4_BLOCK_SIZE_BITS(sb);
  4017. retry:
  4018. err = ext4_es_remove_extent(inode, last_block,
  4019. EXT_MAX_BLOCKS - last_block);
  4020. if (err == -ENOMEM) {
  4021. cond_resched();
  4022. congestion_wait(BLK_RW_ASYNC, HZ/50);
  4023. goto retry;
  4024. }
  4025. if (err) {
  4026. ext4_std_error(inode->i_sb, err);
  4027. return;
  4028. }
  4029. err = ext4_ext_remove_space(inode, last_block, EXT_MAX_BLOCKS - 1);
  4030. ext4_std_error(inode->i_sb, err);
  4031. }
  4032. static void ext4_falloc_update_inode(struct inode *inode,
  4033. int mode, loff_t new_size, int update_ctime)
  4034. {
  4035. struct timespec now;
  4036. if (update_ctime) {
  4037. now = current_fs_time(inode->i_sb);
  4038. if (!timespec_equal(&inode->i_ctime, &now))
  4039. inode->i_ctime = now;
  4040. }
  4041. /*
  4042. * Update only when preallocation was requested beyond
  4043. * the file size.
  4044. */
  4045. if (!(mode & FALLOC_FL_KEEP_SIZE)) {
  4046. if (new_size > i_size_read(inode))
  4047. i_size_write(inode, new_size);
  4048. if (new_size > EXT4_I(inode)->i_disksize)
  4049. ext4_update_i_disksize(inode, new_size);
  4050. } else {
  4051. /*
  4052. * Mark that we allocate beyond EOF so the subsequent truncate
  4053. * can proceed even if the new size is the same as i_size.
  4054. */
  4055. if (new_size > i_size_read(inode))
  4056. ext4_set_inode_flag(inode, EXT4_INODE_EOFBLOCKS);
  4057. }
  4058. }
  4059. /*
  4060. * preallocate space for a file. This implements ext4's fallocate file
  4061. * operation, which gets called from sys_fallocate system call.
  4062. * For block-mapped files, posix_fallocate should fall back to the method
  4063. * of writing zeroes to the required new blocks (the same behavior which is
  4064. * expected for file systems which do not support fallocate() system call).
  4065. */
  4066. long ext4_fallocate(struct file *file, int mode, loff_t offset, loff_t len)
  4067. {
  4068. struct inode *inode = file_inode(file);
  4069. handle_t *handle;
  4070. loff_t new_size;
  4071. unsigned int max_blocks;
  4072. int ret = 0;
  4073. int ret2 = 0;
  4074. int retries = 0;
  4075. int flags;
  4076. struct ext4_map_blocks map;
  4077. unsigned int credits, blkbits = inode->i_blkbits;
  4078. /* Return error if mode is not supported */
  4079. if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
  4080. return -EOPNOTSUPP;
  4081. if (mode & FALLOC_FL_PUNCH_HOLE)
  4082. return ext4_punch_hole(inode, offset, len);
  4083. ret = ext4_convert_inline_data(inode);
  4084. if (ret)
  4085. return ret;
  4086. /*
  4087. * currently supporting (pre)allocate mode for extent-based
  4088. * files _only_
  4089. */
  4090. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  4091. return -EOPNOTSUPP;
  4092. trace_ext4_fallocate_enter(inode, offset, len, mode);
  4093. map.m_lblk = offset >> blkbits;
  4094. /*
  4095. * We can't just convert len to max_blocks because
  4096. * If blocksize = 4096 offset = 3072 and len = 2048
  4097. */
  4098. max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
  4099. - map.m_lblk;
  4100. /*
  4101. * credits to insert 1 extent into extent tree
  4102. */
  4103. credits = ext4_chunk_trans_blocks(inode, max_blocks);
  4104. mutex_lock(&inode->i_mutex);
  4105. ret = inode_newsize_ok(inode, (len + offset));
  4106. if (ret) {
  4107. mutex_unlock(&inode->i_mutex);
  4108. trace_ext4_fallocate_exit(inode, offset, max_blocks, ret);
  4109. return ret;
  4110. }
  4111. flags = EXT4_GET_BLOCKS_CREATE_UNINIT_EXT;
  4112. if (mode & FALLOC_FL_KEEP_SIZE)
  4113. flags |= EXT4_GET_BLOCKS_KEEP_SIZE;
  4114. /*
  4115. * Don't normalize the request if it can fit in one extent so
  4116. * that it doesn't get unnecessarily split into multiple
  4117. * extents.
  4118. */
  4119. if (len <= EXT_UNINIT_MAX_LEN << blkbits)
  4120. flags |= EXT4_GET_BLOCKS_NO_NORMALIZE;
  4121. retry:
  4122. while (ret >= 0 && ret < max_blocks) {
  4123. map.m_lblk = map.m_lblk + ret;
  4124. map.m_len = max_blocks = max_blocks - ret;
  4125. handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
  4126. credits);
  4127. if (IS_ERR(handle)) {
  4128. ret = PTR_ERR(handle);
  4129. break;
  4130. }
  4131. ret = ext4_map_blocks(handle, inode, &map, flags);
  4132. if (ret <= 0) {
  4133. #ifdef EXT4FS_DEBUG
  4134. ext4_warning(inode->i_sb,
  4135. "inode #%lu: block %u: len %u: "
  4136. "ext4_ext_map_blocks returned %d",
  4137. inode->i_ino, map.m_lblk,
  4138. map.m_len, ret);
  4139. #endif
  4140. ext4_mark_inode_dirty(handle, inode);
  4141. ret2 = ext4_journal_stop(handle);
  4142. break;
  4143. }
  4144. if ((map.m_lblk + ret) >= (EXT4_BLOCK_ALIGN(offset + len,
  4145. blkbits) >> blkbits))
  4146. new_size = offset + len;
  4147. else
  4148. new_size = ((loff_t) map.m_lblk + ret) << blkbits;
  4149. ext4_falloc_update_inode(inode, mode, new_size,
  4150. (map.m_flags & EXT4_MAP_NEW));
  4151. ext4_mark_inode_dirty(handle, inode);
  4152. if ((file->f_flags & O_SYNC) && ret >= max_blocks)
  4153. ext4_handle_sync(handle);
  4154. ret2 = ext4_journal_stop(handle);
  4155. if (ret2)
  4156. break;
  4157. }
  4158. if (ret == -ENOSPC &&
  4159. ext4_should_retry_alloc(inode->i_sb, &retries)) {
  4160. ret = 0;
  4161. goto retry;
  4162. }
  4163. mutex_unlock(&inode->i_mutex);
  4164. trace_ext4_fallocate_exit(inode, offset, max_blocks,
  4165. ret > 0 ? ret2 : ret);
  4166. return ret > 0 ? ret2 : ret;
  4167. }
  4168. /*
  4169. * This function convert a range of blocks to written extents
  4170. * The caller of this function will pass the start offset and the size.
  4171. * all unwritten extents within this range will be converted to
  4172. * written extents.
  4173. *
  4174. * This function is called from the direct IO end io call back
  4175. * function, to convert the fallocated extents after IO is completed.
  4176. * Returns 0 on success.
  4177. */
  4178. int ext4_convert_unwritten_extents(handle_t *handle, struct inode *inode,
  4179. loff_t offset, ssize_t len)
  4180. {
  4181. unsigned int max_blocks;
  4182. int ret = 0;
  4183. int ret2 = 0;
  4184. struct ext4_map_blocks map;
  4185. unsigned int credits, blkbits = inode->i_blkbits;
  4186. map.m_lblk = offset >> blkbits;
  4187. /*
  4188. * We can't just convert len to max_blocks because
  4189. * If blocksize = 4096 offset = 3072 and len = 2048
  4190. */
  4191. max_blocks = ((EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits) -
  4192. map.m_lblk);
  4193. /*
  4194. * This is somewhat ugly but the idea is clear: When transaction is
  4195. * reserved, everything goes into it. Otherwise we rather start several
  4196. * smaller transactions for conversion of each extent separately.
  4197. */
  4198. if (handle) {
  4199. handle = ext4_journal_start_reserved(handle,
  4200. EXT4_HT_EXT_CONVERT);
  4201. if (IS_ERR(handle))
  4202. return PTR_ERR(handle);
  4203. credits = 0;
  4204. } else {
  4205. /*
  4206. * credits to insert 1 extent into extent tree
  4207. */
  4208. credits = ext4_chunk_trans_blocks(inode, max_blocks);
  4209. }
  4210. while (ret >= 0 && ret < max_blocks) {
  4211. map.m_lblk += ret;
  4212. map.m_len = (max_blocks -= ret);
  4213. if (credits) {
  4214. handle = ext4_journal_start(inode, EXT4_HT_MAP_BLOCKS,
  4215. credits);
  4216. if (IS_ERR(handle)) {
  4217. ret = PTR_ERR(handle);
  4218. break;
  4219. }
  4220. }
  4221. ret = ext4_map_blocks(handle, inode, &map,
  4222. EXT4_GET_BLOCKS_IO_CONVERT_EXT);
  4223. if (ret <= 0)
  4224. ext4_warning(inode->i_sb,
  4225. "inode #%lu: block %u: len %u: "
  4226. "ext4_ext_map_blocks returned %d",
  4227. inode->i_ino, map.m_lblk,
  4228. map.m_len, ret);
  4229. ext4_mark_inode_dirty(handle, inode);
  4230. if (credits)
  4231. ret2 = ext4_journal_stop(handle);
  4232. if (ret <= 0 || ret2)
  4233. break;
  4234. }
  4235. if (!credits)
  4236. ret2 = ext4_journal_stop(handle);
  4237. return ret > 0 ? ret2 : ret;
  4238. }
  4239. /*
  4240. * If newes is not existing extent (newes->ec_pblk equals zero) find
  4241. * delayed extent at start of newes and update newes accordingly and
  4242. * return start of the next delayed extent.
  4243. *
  4244. * If newes is existing extent (newes->ec_pblk is not equal zero)
  4245. * return start of next delayed extent or EXT_MAX_BLOCKS if no delayed
  4246. * extent found. Leave newes unmodified.
  4247. */
  4248. static int ext4_find_delayed_extent(struct inode *inode,
  4249. struct extent_status *newes)
  4250. {
  4251. struct extent_status es;
  4252. ext4_lblk_t block, next_del;
  4253. if (newes->es_pblk == 0) {
  4254. ext4_es_find_delayed_extent_range(inode, newes->es_lblk,
  4255. newes->es_lblk + newes->es_len - 1, &es);
  4256. /*
  4257. * No extent in extent-tree contains block @newes->es_pblk,
  4258. * then the block may stay in 1)a hole or 2)delayed-extent.
  4259. */
  4260. if (es.es_len == 0)
  4261. /* A hole found. */
  4262. return 0;
  4263. if (es.es_lblk > newes->es_lblk) {
  4264. /* A hole found. */
  4265. newes->es_len = min(es.es_lblk - newes->es_lblk,
  4266. newes->es_len);
  4267. return 0;
  4268. }
  4269. newes->es_len = es.es_lblk + es.es_len - newes->es_lblk;
  4270. }
  4271. block = newes->es_lblk + newes->es_len;
  4272. ext4_es_find_delayed_extent_range(inode, block, EXT_MAX_BLOCKS, &es);
  4273. if (es.es_len == 0)
  4274. next_del = EXT_MAX_BLOCKS;
  4275. else
  4276. next_del = es.es_lblk;
  4277. return next_del;
  4278. }
  4279. /* fiemap flags we can handle specified here */
  4280. #define EXT4_FIEMAP_FLAGS (FIEMAP_FLAG_SYNC|FIEMAP_FLAG_XATTR)
  4281. static int ext4_xattr_fiemap(struct inode *inode,
  4282. struct fiemap_extent_info *fieinfo)
  4283. {
  4284. __u64 physical = 0;
  4285. __u64 length;
  4286. __u32 flags = FIEMAP_EXTENT_LAST;
  4287. int blockbits = inode->i_sb->s_blocksize_bits;
  4288. int error = 0;
  4289. /* in-inode? */
  4290. if (ext4_test_inode_state(inode, EXT4_STATE_XATTR)) {
  4291. struct ext4_iloc iloc;
  4292. int offset; /* offset of xattr in inode */
  4293. error = ext4_get_inode_loc(inode, &iloc);
  4294. if (error)
  4295. return error;
  4296. physical = (__u64)iloc.bh->b_blocknr << blockbits;
  4297. offset = EXT4_GOOD_OLD_INODE_SIZE +
  4298. EXT4_I(inode)->i_extra_isize;
  4299. physical += offset;
  4300. length = EXT4_SB(inode->i_sb)->s_inode_size - offset;
  4301. flags |= FIEMAP_EXTENT_DATA_INLINE;
  4302. brelse(iloc.bh);
  4303. } else { /* external block */
  4304. physical = (__u64)EXT4_I(inode)->i_file_acl << blockbits;
  4305. length = inode->i_sb->s_blocksize;
  4306. }
  4307. if (physical)
  4308. error = fiemap_fill_next_extent(fieinfo, 0, physical,
  4309. length, flags);
  4310. return (error < 0 ? error : 0);
  4311. }
  4312. int ext4_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
  4313. __u64 start, __u64 len)
  4314. {
  4315. ext4_lblk_t start_blk;
  4316. int error = 0;
  4317. if (ext4_has_inline_data(inode)) {
  4318. int has_inline = 1;
  4319. error = ext4_inline_data_fiemap(inode, fieinfo, &has_inline);
  4320. if (has_inline)
  4321. return error;
  4322. }
  4323. if (fieinfo->fi_flags & FIEMAP_FLAG_CACHE) {
  4324. error = ext4_ext_precache(inode);
  4325. if (error)
  4326. return error;
  4327. }
  4328. /* fallback to generic here if not in extents fmt */
  4329. if (!(ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS)))
  4330. return generic_block_fiemap(inode, fieinfo, start, len,
  4331. ext4_get_block);
  4332. if (fiemap_check_flags(fieinfo, EXT4_FIEMAP_FLAGS))
  4333. return -EBADR;
  4334. if (fieinfo->fi_flags & FIEMAP_FLAG_XATTR) {
  4335. error = ext4_xattr_fiemap(inode, fieinfo);
  4336. } else {
  4337. ext4_lblk_t len_blks;
  4338. __u64 last_blk;
  4339. start_blk = start >> inode->i_sb->s_blocksize_bits;
  4340. last_blk = (start + len - 1) >> inode->i_sb->s_blocksize_bits;
  4341. if (last_blk >= EXT_MAX_BLOCKS)
  4342. last_blk = EXT_MAX_BLOCKS-1;
  4343. len_blks = ((ext4_lblk_t) last_blk) - start_blk + 1;
  4344. /*
  4345. * Walk the extent tree gathering extent information
  4346. * and pushing extents back to the user.
  4347. */
  4348. error = ext4_fill_fiemap_extents(inode, start_blk,
  4349. len_blks, fieinfo);
  4350. }
  4351. ext4_es_lru_add(inode);
  4352. return error;
  4353. }