extents.c 67 KB

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