extents.c 113 KB

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