extents.c 97 KB

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