extents.c 136 KB

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