extents.c 134 KB

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