extents.c 115 KB

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