extents.c 76 KB

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