extents.c 132 KB

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