extents.c 162 KB

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