extents.c 85 KB

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