extents.c 133 KB

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