extents.c 103 KB

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