extents.c 133 KB

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