extents.c 131 KB

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