extents.c 163 KB

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