extents.c 156 KB

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