extents.c 161 KB

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