extents.c 76 KB

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