extents.c 114 KB

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