extents.c 163 KB

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