extents.c 68 KB

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