extents.c 100 KB

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