extents.c 104 KB

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