extents.c 164 KB

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