extents.c 103 KB

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