extents.c 133 KB

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