extents.c 149 KB

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