extents.c 131 KB

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