extents.c 133 KB

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