extents.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712
  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, 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, ee_len;
  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. ee_len = ext4_ext_get_actual_len(ex);
  910. if (*logical < le32_to_cpu(ex->ee_block)) {
  911. BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
  912. while (--depth >= 0) {
  913. ix = path[depth].p_idx;
  914. BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
  915. }
  916. return 0;
  917. }
  918. BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
  919. *logical = le32_to_cpu(ex->ee_block) + ee_len - 1;
  920. *phys = ext_pblock(ex) + ee_len - 1;
  921. return 0;
  922. }
  923. /*
  924. * search the closest allocated block to the right for *logical
  925. * and returns it at @logical + it's physical address at @phys
  926. * if *logical is the smallest allocated block, the function
  927. * returns 0 at @phys
  928. * return value contains 0 (success) or error code
  929. */
  930. int
  931. ext4_ext_search_right(struct inode *inode, struct ext4_ext_path *path,
  932. ext4_lblk_t *logical, ext4_fsblk_t *phys)
  933. {
  934. struct buffer_head *bh = NULL;
  935. struct ext4_extent_header *eh;
  936. struct ext4_extent_idx *ix;
  937. struct ext4_extent *ex;
  938. ext4_fsblk_t block;
  939. int depth, ee_len;
  940. BUG_ON(path == NULL);
  941. depth = path->p_depth;
  942. *phys = 0;
  943. if (depth == 0 && path->p_ext == NULL)
  944. return 0;
  945. /* usually extent in the path covers blocks smaller
  946. * then *logical, but it can be that extent is the
  947. * first one in the file */
  948. ex = path[depth].p_ext;
  949. ee_len = ext4_ext_get_actual_len(ex);
  950. if (*logical < le32_to_cpu(ex->ee_block)) {
  951. BUG_ON(EXT_FIRST_EXTENT(path[depth].p_hdr) != ex);
  952. while (--depth >= 0) {
  953. ix = path[depth].p_idx;
  954. BUG_ON(ix != EXT_FIRST_INDEX(path[depth].p_hdr));
  955. }
  956. *logical = le32_to_cpu(ex->ee_block);
  957. *phys = ext_pblock(ex);
  958. return 0;
  959. }
  960. BUG_ON(*logical < (le32_to_cpu(ex->ee_block) + ee_len));
  961. if (ex != EXT_LAST_EXTENT(path[depth].p_hdr)) {
  962. /* next allocated block in this leaf */
  963. ex++;
  964. *logical = le32_to_cpu(ex->ee_block);
  965. *phys = ext_pblock(ex);
  966. return 0;
  967. }
  968. /* go up and search for index to the right */
  969. while (--depth >= 0) {
  970. ix = path[depth].p_idx;
  971. if (ix != EXT_LAST_INDEX(path[depth].p_hdr))
  972. break;
  973. }
  974. if (depth < 0) {
  975. /* we've gone up to the root and
  976. * found no index to the right */
  977. return 0;
  978. }
  979. /* we've found index to the right, let's
  980. * follow it and find the closest allocated
  981. * block to the right */
  982. ix++;
  983. block = idx_pblock(ix);
  984. while (++depth < path->p_depth) {
  985. bh = sb_bread(inode->i_sb, block);
  986. if (bh == NULL)
  987. return -EIO;
  988. eh = ext_block_hdr(bh);
  989. if (ext4_ext_check_header(inode, eh, depth)) {
  990. put_bh(bh);
  991. return -EIO;
  992. }
  993. ix = EXT_FIRST_INDEX(eh);
  994. block = idx_pblock(ix);
  995. put_bh(bh);
  996. }
  997. bh = sb_bread(inode->i_sb, block);
  998. if (bh == NULL)
  999. return -EIO;
  1000. eh = ext_block_hdr(bh);
  1001. if (ext4_ext_check_header(inode, eh, path->p_depth - depth)) {
  1002. put_bh(bh);
  1003. return -EIO;
  1004. }
  1005. ex = EXT_FIRST_EXTENT(eh);
  1006. *logical = le32_to_cpu(ex->ee_block);
  1007. *phys = ext_pblock(ex);
  1008. put_bh(bh);
  1009. return 0;
  1010. }
  1011. /*
  1012. * ext4_ext_next_allocated_block:
  1013. * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
  1014. * NOTE: it considers block number from index entry as
  1015. * allocated block. Thus, index entries have to be consistent
  1016. * with leaves.
  1017. */
  1018. static ext4_lblk_t
  1019. ext4_ext_next_allocated_block(struct ext4_ext_path *path)
  1020. {
  1021. int depth;
  1022. BUG_ON(path == NULL);
  1023. depth = path->p_depth;
  1024. if (depth == 0 && path->p_ext == NULL)
  1025. return EXT_MAX_BLOCK;
  1026. while (depth >= 0) {
  1027. if (depth == path->p_depth) {
  1028. /* leaf */
  1029. if (path[depth].p_ext !=
  1030. EXT_LAST_EXTENT(path[depth].p_hdr))
  1031. return le32_to_cpu(path[depth].p_ext[1].ee_block);
  1032. } else {
  1033. /* index */
  1034. if (path[depth].p_idx !=
  1035. EXT_LAST_INDEX(path[depth].p_hdr))
  1036. return le32_to_cpu(path[depth].p_idx[1].ei_block);
  1037. }
  1038. depth--;
  1039. }
  1040. return EXT_MAX_BLOCK;
  1041. }
  1042. /*
  1043. * ext4_ext_next_leaf_block:
  1044. * returns first allocated block from next leaf or EXT_MAX_BLOCK
  1045. */
  1046. static ext4_lblk_t ext4_ext_next_leaf_block(struct inode *inode,
  1047. struct ext4_ext_path *path)
  1048. {
  1049. int depth;
  1050. BUG_ON(path == NULL);
  1051. depth = path->p_depth;
  1052. /* zero-tree has no leaf blocks at all */
  1053. if (depth == 0)
  1054. return EXT_MAX_BLOCK;
  1055. /* go to index block */
  1056. depth--;
  1057. while (depth >= 0) {
  1058. if (path[depth].p_idx !=
  1059. EXT_LAST_INDEX(path[depth].p_hdr))
  1060. return (ext4_lblk_t)
  1061. le32_to_cpu(path[depth].p_idx[1].ei_block);
  1062. depth--;
  1063. }
  1064. return EXT_MAX_BLOCK;
  1065. }
  1066. /*
  1067. * ext4_ext_correct_indexes:
  1068. * if leaf gets modified and modified extent is first in the leaf,
  1069. * then we have to correct all indexes above.
  1070. * TODO: do we need to correct tree in all cases?
  1071. */
  1072. static int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
  1073. struct ext4_ext_path *path)
  1074. {
  1075. struct ext4_extent_header *eh;
  1076. int depth = ext_depth(inode);
  1077. struct ext4_extent *ex;
  1078. __le32 border;
  1079. int k, err = 0;
  1080. eh = path[depth].p_hdr;
  1081. ex = path[depth].p_ext;
  1082. BUG_ON(ex == NULL);
  1083. BUG_ON(eh == NULL);
  1084. if (depth == 0) {
  1085. /* there is no tree at all */
  1086. return 0;
  1087. }
  1088. if (ex != EXT_FIRST_EXTENT(eh)) {
  1089. /* we correct tree if first leaf got modified only */
  1090. return 0;
  1091. }
  1092. /*
  1093. * TODO: we need correction if border is smaller than current one
  1094. */
  1095. k = depth - 1;
  1096. border = path[depth].p_ext->ee_block;
  1097. err = ext4_ext_get_access(handle, inode, path + k);
  1098. if (err)
  1099. return err;
  1100. path[k].p_idx->ei_block = border;
  1101. err = ext4_ext_dirty(handle, inode, path + k);
  1102. if (err)
  1103. return err;
  1104. while (k--) {
  1105. /* change all left-side indexes */
  1106. if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
  1107. break;
  1108. err = ext4_ext_get_access(handle, inode, path + k);
  1109. if (err)
  1110. break;
  1111. path[k].p_idx->ei_block = border;
  1112. err = ext4_ext_dirty(handle, inode, path + k);
  1113. if (err)
  1114. break;
  1115. }
  1116. return err;
  1117. }
  1118. static int
  1119. ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
  1120. struct ext4_extent *ex2)
  1121. {
  1122. unsigned short ext1_ee_len, ext2_ee_len, max_len;
  1123. /*
  1124. * Make sure that either both extents are uninitialized, or
  1125. * both are _not_.
  1126. */
  1127. if (ext4_ext_is_uninitialized(ex1) ^ ext4_ext_is_uninitialized(ex2))
  1128. return 0;
  1129. if (ext4_ext_is_uninitialized(ex1))
  1130. max_len = EXT_UNINIT_MAX_LEN;
  1131. else
  1132. max_len = EXT_INIT_MAX_LEN;
  1133. ext1_ee_len = ext4_ext_get_actual_len(ex1);
  1134. ext2_ee_len = ext4_ext_get_actual_len(ex2);
  1135. if (le32_to_cpu(ex1->ee_block) + ext1_ee_len !=
  1136. le32_to_cpu(ex2->ee_block))
  1137. return 0;
  1138. /*
  1139. * To allow future support for preallocated extents to be added
  1140. * as an RO_COMPAT feature, refuse to merge to extents if
  1141. * this can result in the top bit of ee_len being set.
  1142. */
  1143. if (ext1_ee_len + ext2_ee_len > max_len)
  1144. return 0;
  1145. #ifdef AGGRESSIVE_TEST
  1146. if (ext1_ee_len >= 4)
  1147. return 0;
  1148. #endif
  1149. if (ext_pblock(ex1) + ext1_ee_len == ext_pblock(ex2))
  1150. return 1;
  1151. return 0;
  1152. }
  1153. /*
  1154. * This function tries to merge the "ex" extent to the next extent in the tree.
  1155. * It always tries to merge towards right. If you want to merge towards
  1156. * left, pass "ex - 1" as argument instead of "ex".
  1157. * Returns 0 if the extents (ex and ex+1) were _not_ merged and returns
  1158. * 1 if they got merged.
  1159. */
  1160. int ext4_ext_try_to_merge(struct inode *inode,
  1161. struct ext4_ext_path *path,
  1162. struct ext4_extent *ex)
  1163. {
  1164. struct ext4_extent_header *eh;
  1165. unsigned int depth, len;
  1166. int merge_done = 0;
  1167. int uninitialized = 0;
  1168. depth = ext_depth(inode);
  1169. BUG_ON(path[depth].p_hdr == NULL);
  1170. eh = path[depth].p_hdr;
  1171. while (ex < EXT_LAST_EXTENT(eh)) {
  1172. if (!ext4_can_extents_be_merged(inode, ex, ex + 1))
  1173. break;
  1174. /* merge with next extent! */
  1175. if (ext4_ext_is_uninitialized(ex))
  1176. uninitialized = 1;
  1177. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1178. + ext4_ext_get_actual_len(ex + 1));
  1179. if (uninitialized)
  1180. ext4_ext_mark_uninitialized(ex);
  1181. if (ex + 1 < EXT_LAST_EXTENT(eh)) {
  1182. len = (EXT_LAST_EXTENT(eh) - ex - 1)
  1183. * sizeof(struct ext4_extent);
  1184. memmove(ex + 1, ex + 2, len);
  1185. }
  1186. eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries) - 1);
  1187. merge_done = 1;
  1188. WARN_ON(eh->eh_entries == 0);
  1189. if (!eh->eh_entries)
  1190. ext4_error(inode->i_sb, "ext4_ext_try_to_merge",
  1191. "inode#%lu, eh->eh_entries = 0!", inode->i_ino);
  1192. }
  1193. return merge_done;
  1194. }
  1195. /*
  1196. * check if a portion of the "newext" extent overlaps with an
  1197. * existing extent.
  1198. *
  1199. * If there is an overlap discovered, it updates the length of the newext
  1200. * such that there will be no overlap, and then returns 1.
  1201. * If there is no overlap found, it returns 0.
  1202. */
  1203. unsigned int ext4_ext_check_overlap(struct inode *inode,
  1204. struct ext4_extent *newext,
  1205. struct ext4_ext_path *path)
  1206. {
  1207. ext4_lblk_t b1, b2;
  1208. unsigned int depth, len1;
  1209. unsigned int ret = 0;
  1210. b1 = le32_to_cpu(newext->ee_block);
  1211. len1 = ext4_ext_get_actual_len(newext);
  1212. depth = ext_depth(inode);
  1213. if (!path[depth].p_ext)
  1214. goto out;
  1215. b2 = le32_to_cpu(path[depth].p_ext->ee_block);
  1216. /*
  1217. * get the next allocated block if the extent in the path
  1218. * is before the requested block(s)
  1219. */
  1220. if (b2 < b1) {
  1221. b2 = ext4_ext_next_allocated_block(path);
  1222. if (b2 == EXT_MAX_BLOCK)
  1223. goto out;
  1224. }
  1225. /* check for wrap through zero on extent logical start block*/
  1226. if (b1 + len1 < b1) {
  1227. len1 = EXT_MAX_BLOCK - b1;
  1228. newext->ee_len = cpu_to_le16(len1);
  1229. ret = 1;
  1230. }
  1231. /* check for overlap */
  1232. if (b1 + len1 > b2) {
  1233. newext->ee_len = cpu_to_le16(b2 - b1);
  1234. ret = 1;
  1235. }
  1236. out:
  1237. return ret;
  1238. }
  1239. /*
  1240. * ext4_ext_insert_extent:
  1241. * tries to merge requsted extent into the existing extent or
  1242. * inserts requested extent as new one into the tree,
  1243. * creating new leaf in the no-space case.
  1244. */
  1245. int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
  1246. struct ext4_ext_path *path,
  1247. struct ext4_extent *newext)
  1248. {
  1249. struct ext4_extent_header * eh;
  1250. struct ext4_extent *ex, *fex;
  1251. struct ext4_extent *nearex; /* nearest extent */
  1252. struct ext4_ext_path *npath = NULL;
  1253. int depth, len, err;
  1254. ext4_lblk_t next;
  1255. unsigned uninitialized = 0;
  1256. BUG_ON(ext4_ext_get_actual_len(newext) == 0);
  1257. depth = ext_depth(inode);
  1258. ex = path[depth].p_ext;
  1259. BUG_ON(path[depth].p_hdr == NULL);
  1260. /* try to insert block into found extent and return */
  1261. if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
  1262. ext_debug("append %d block to %d:%d (from %llu)\n",
  1263. ext4_ext_get_actual_len(newext),
  1264. le32_to_cpu(ex->ee_block),
  1265. ext4_ext_get_actual_len(ex), ext_pblock(ex));
  1266. err = ext4_ext_get_access(handle, inode, path + depth);
  1267. if (err)
  1268. return err;
  1269. /*
  1270. * ext4_can_extents_be_merged should have checked that either
  1271. * both extents are uninitialized, or both aren't. Thus we
  1272. * need to check only one of them here.
  1273. */
  1274. if (ext4_ext_is_uninitialized(ex))
  1275. uninitialized = 1;
  1276. ex->ee_len = cpu_to_le16(ext4_ext_get_actual_len(ex)
  1277. + ext4_ext_get_actual_len(newext));
  1278. if (uninitialized)
  1279. ext4_ext_mark_uninitialized(ex);
  1280. eh = path[depth].p_hdr;
  1281. nearex = ex;
  1282. goto merge;
  1283. }
  1284. repeat:
  1285. depth = ext_depth(inode);
  1286. eh = path[depth].p_hdr;
  1287. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
  1288. goto has_space;
  1289. /* probably next leaf has space for us? */
  1290. fex = EXT_LAST_EXTENT(eh);
  1291. next = ext4_ext_next_leaf_block(inode, path);
  1292. if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
  1293. && next != EXT_MAX_BLOCK) {
  1294. ext_debug("next leaf block - %d\n", next);
  1295. BUG_ON(npath != NULL);
  1296. npath = ext4_ext_find_extent(inode, next, NULL);
  1297. if (IS_ERR(npath))
  1298. return PTR_ERR(npath);
  1299. BUG_ON(npath->p_depth != path->p_depth);
  1300. eh = npath[depth].p_hdr;
  1301. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
  1302. ext_debug("next leaf isnt full(%d)\n",
  1303. le16_to_cpu(eh->eh_entries));
  1304. path = npath;
  1305. goto repeat;
  1306. }
  1307. ext_debug("next leaf has no free space(%d,%d)\n",
  1308. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  1309. }
  1310. /*
  1311. * There is no free space in the found leaf.
  1312. * We're gonna add a new leaf in the tree.
  1313. */
  1314. err = ext4_ext_create_new_leaf(handle, inode, path, newext);
  1315. if (err)
  1316. goto cleanup;
  1317. depth = ext_depth(inode);
  1318. eh = path[depth].p_hdr;
  1319. has_space:
  1320. nearex = path[depth].p_ext;
  1321. err = ext4_ext_get_access(handle, inode, path + depth);
  1322. if (err)
  1323. goto cleanup;
  1324. if (!nearex) {
  1325. /* there is no extent in this leaf, create first one */
  1326. ext_debug("first extent in the leaf: %d:%llu:%d\n",
  1327. le32_to_cpu(newext->ee_block),
  1328. ext_pblock(newext),
  1329. ext4_ext_get_actual_len(newext));
  1330. path[depth].p_ext = EXT_FIRST_EXTENT(eh);
  1331. } else if (le32_to_cpu(newext->ee_block)
  1332. > le32_to_cpu(nearex->ee_block)) {
  1333. /* BUG_ON(newext->ee_block == nearex->ee_block); */
  1334. if (nearex != EXT_LAST_EXTENT(eh)) {
  1335. len = EXT_MAX_EXTENT(eh) - nearex;
  1336. len = (len - 1) * sizeof(struct ext4_extent);
  1337. len = len < 0 ? 0 : len;
  1338. ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
  1339. "move %d from 0x%p to 0x%p\n",
  1340. le32_to_cpu(newext->ee_block),
  1341. ext_pblock(newext),
  1342. ext4_ext_get_actual_len(newext),
  1343. nearex, len, nearex + 1, nearex + 2);
  1344. memmove(nearex + 2, nearex + 1, len);
  1345. }
  1346. path[depth].p_ext = nearex + 1;
  1347. } else {
  1348. BUG_ON(newext->ee_block == nearex->ee_block);
  1349. len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
  1350. len = len < 0 ? 0 : len;
  1351. ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
  1352. "move %d from 0x%p to 0x%p\n",
  1353. le32_to_cpu(newext->ee_block),
  1354. ext_pblock(newext),
  1355. ext4_ext_get_actual_len(newext),
  1356. nearex, len, nearex + 1, nearex + 2);
  1357. memmove(nearex + 1, nearex, len);
  1358. path[depth].p_ext = nearex;
  1359. }
  1360. eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)+1);
  1361. nearex = path[depth].p_ext;
  1362. nearex->ee_block = newext->ee_block;
  1363. ext4_ext_store_pblock(nearex, ext_pblock(newext));
  1364. nearex->ee_len = newext->ee_len;
  1365. merge:
  1366. /* try to merge extents to the right */
  1367. ext4_ext_try_to_merge(inode, path, nearex);
  1368. /* try to merge extents to the left */
  1369. /* time to correct all indexes above */
  1370. err = ext4_ext_correct_indexes(handle, inode, path);
  1371. if (err)
  1372. goto cleanup;
  1373. err = ext4_ext_dirty(handle, inode, path + depth);
  1374. cleanup:
  1375. if (npath) {
  1376. ext4_ext_drop_refs(npath);
  1377. kfree(npath);
  1378. }
  1379. ext4_ext_tree_changed(inode);
  1380. ext4_ext_invalidate_cache(inode);
  1381. return err;
  1382. }
  1383. static void
  1384. ext4_ext_put_in_cache(struct inode *inode, ext4_lblk_t block,
  1385. __u32 len, ext4_fsblk_t start, int type)
  1386. {
  1387. struct ext4_ext_cache *cex;
  1388. BUG_ON(len == 0);
  1389. cex = &EXT4_I(inode)->i_cached_extent;
  1390. cex->ec_type = type;
  1391. cex->ec_block = block;
  1392. cex->ec_len = len;
  1393. cex->ec_start = start;
  1394. }
  1395. /*
  1396. * ext4_ext_put_gap_in_cache:
  1397. * calculate boundaries of the gap that the requested block fits into
  1398. * and cache this gap
  1399. */
  1400. static void
  1401. ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
  1402. ext4_lblk_t block)
  1403. {
  1404. int depth = ext_depth(inode);
  1405. unsigned long len;
  1406. ext4_lblk_t lblock;
  1407. struct ext4_extent *ex;
  1408. ex = path[depth].p_ext;
  1409. if (ex == NULL) {
  1410. /* there is no extent yet, so gap is [0;-] */
  1411. lblock = 0;
  1412. len = EXT_MAX_BLOCK;
  1413. ext_debug("cache gap(whole file):");
  1414. } else if (block < le32_to_cpu(ex->ee_block)) {
  1415. lblock = block;
  1416. len = le32_to_cpu(ex->ee_block) - block;
  1417. ext_debug("cache gap(before): %u [%u:%u]",
  1418. block,
  1419. le32_to_cpu(ex->ee_block),
  1420. ext4_ext_get_actual_len(ex));
  1421. } else if (block >= le32_to_cpu(ex->ee_block)
  1422. + ext4_ext_get_actual_len(ex)) {
  1423. ext4_lblk_t next;
  1424. lblock = le32_to_cpu(ex->ee_block)
  1425. + ext4_ext_get_actual_len(ex);
  1426. next = ext4_ext_next_allocated_block(path);
  1427. ext_debug("cache gap(after): [%u:%u] %u",
  1428. le32_to_cpu(ex->ee_block),
  1429. ext4_ext_get_actual_len(ex),
  1430. block);
  1431. BUG_ON(next == lblock);
  1432. len = next - lblock;
  1433. } else {
  1434. lblock = len = 0;
  1435. BUG();
  1436. }
  1437. ext_debug(" -> %u:%lu\n", lblock, len);
  1438. ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
  1439. }
  1440. static int
  1441. ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
  1442. struct ext4_extent *ex)
  1443. {
  1444. struct ext4_ext_cache *cex;
  1445. cex = &EXT4_I(inode)->i_cached_extent;
  1446. /* has cache valid data? */
  1447. if (cex->ec_type == EXT4_EXT_CACHE_NO)
  1448. return EXT4_EXT_CACHE_NO;
  1449. BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
  1450. cex->ec_type != EXT4_EXT_CACHE_EXTENT);
  1451. if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
  1452. ex->ee_block = cpu_to_le32(cex->ec_block);
  1453. ext4_ext_store_pblock(ex, cex->ec_start);
  1454. ex->ee_len = cpu_to_le16(cex->ec_len);
  1455. ext_debug("%u cached by %u:%u:%llu\n",
  1456. block,
  1457. cex->ec_block, cex->ec_len, cex->ec_start);
  1458. return cex->ec_type;
  1459. }
  1460. /* not in cache */
  1461. return EXT4_EXT_CACHE_NO;
  1462. }
  1463. /*
  1464. * ext4_ext_rm_idx:
  1465. * removes index from the index block.
  1466. * It's used in truncate case only, thus all requests are for
  1467. * last index in the block only.
  1468. */
  1469. static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
  1470. struct ext4_ext_path *path)
  1471. {
  1472. struct buffer_head *bh;
  1473. int err;
  1474. ext4_fsblk_t leaf;
  1475. /* free index block */
  1476. path--;
  1477. leaf = idx_pblock(path->p_idx);
  1478. BUG_ON(path->p_hdr->eh_entries == 0);
  1479. err = ext4_ext_get_access(handle, inode, path);
  1480. if (err)
  1481. return err;
  1482. path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1);
  1483. err = ext4_ext_dirty(handle, inode, path);
  1484. if (err)
  1485. return err;
  1486. ext_debug("index is empty, remove it, free block %llu\n", leaf);
  1487. bh = sb_find_get_block(inode->i_sb, leaf);
  1488. ext4_forget(handle, 1, inode, bh, leaf);
  1489. ext4_free_blocks(handle, inode, leaf, 1, 1);
  1490. return err;
  1491. }
  1492. /*
  1493. * ext4_ext_calc_credits_for_insert:
  1494. * This routine returns max. credits that the extent tree can consume.
  1495. * It should be OK for low-performance paths like ->writepage()
  1496. * To allow many writing processes to fit into a single transaction,
  1497. * the caller should calculate credits under i_data_sem and
  1498. * pass the actual path.
  1499. */
  1500. int ext4_ext_calc_credits_for_insert(struct inode *inode,
  1501. struct ext4_ext_path *path)
  1502. {
  1503. int depth, needed;
  1504. if (path) {
  1505. /* probably there is space in leaf? */
  1506. depth = ext_depth(inode);
  1507. if (le16_to_cpu(path[depth].p_hdr->eh_entries)
  1508. < le16_to_cpu(path[depth].p_hdr->eh_max))
  1509. return 1;
  1510. }
  1511. /*
  1512. * given 32-bit logical block (4294967296 blocks), max. tree
  1513. * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
  1514. * Let's also add one more level for imbalance.
  1515. */
  1516. depth = 5;
  1517. /* allocation of new data block(s) */
  1518. needed = 2;
  1519. /*
  1520. * tree can be full, so it would need to grow in depth:
  1521. * we need one credit to modify old root, credits for
  1522. * new root will be added in split accounting
  1523. */
  1524. needed += 1;
  1525. /*
  1526. * Index split can happen, we would need:
  1527. * allocate intermediate indexes (bitmap + group)
  1528. * + change two blocks at each level, but root (already included)
  1529. */
  1530. needed += (depth * 2) + (depth * 2);
  1531. /* any allocation modifies superblock */
  1532. needed += 1;
  1533. return needed;
  1534. }
  1535. static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
  1536. struct ext4_extent *ex,
  1537. ext4_lblk_t from, ext4_lblk_t to)
  1538. {
  1539. struct buffer_head *bh;
  1540. unsigned short ee_len = ext4_ext_get_actual_len(ex);
  1541. int i, metadata = 0;
  1542. if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
  1543. metadata = 1;
  1544. #ifdef EXTENTS_STATS
  1545. {
  1546. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1547. spin_lock(&sbi->s_ext_stats_lock);
  1548. sbi->s_ext_blocks += ee_len;
  1549. sbi->s_ext_extents++;
  1550. if (ee_len < sbi->s_ext_min)
  1551. sbi->s_ext_min = ee_len;
  1552. if (ee_len > sbi->s_ext_max)
  1553. sbi->s_ext_max = ee_len;
  1554. if (ext_depth(inode) > sbi->s_depth_max)
  1555. sbi->s_depth_max = ext_depth(inode);
  1556. spin_unlock(&sbi->s_ext_stats_lock);
  1557. }
  1558. #endif
  1559. if (from >= le32_to_cpu(ex->ee_block)
  1560. && to == le32_to_cpu(ex->ee_block) + ee_len - 1) {
  1561. /* tail removal */
  1562. ext4_lblk_t num;
  1563. ext4_fsblk_t start;
  1564. num = le32_to_cpu(ex->ee_block) + ee_len - from;
  1565. start = ext_pblock(ex) + ee_len - num;
  1566. ext_debug("free last %u blocks starting %llu\n", num, start);
  1567. for (i = 0; i < num; i++) {
  1568. bh = sb_find_get_block(inode->i_sb, start + i);
  1569. ext4_forget(handle, 0, inode, bh, start + i);
  1570. }
  1571. ext4_free_blocks(handle, inode, start, num, metadata);
  1572. } else if (from == le32_to_cpu(ex->ee_block)
  1573. && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
  1574. printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
  1575. from, to, le32_to_cpu(ex->ee_block), ee_len);
  1576. } else {
  1577. printk(KERN_INFO "strange request: removal(2) "
  1578. "%u-%u from %u:%u\n",
  1579. from, to, le32_to_cpu(ex->ee_block), ee_len);
  1580. }
  1581. return 0;
  1582. }
  1583. static int
  1584. ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
  1585. struct ext4_ext_path *path, ext4_lblk_t start)
  1586. {
  1587. int err = 0, correct_index = 0;
  1588. int depth = ext_depth(inode), credits;
  1589. struct ext4_extent_header *eh;
  1590. ext4_lblk_t a, b, block;
  1591. unsigned num;
  1592. ext4_lblk_t ex_ee_block;
  1593. unsigned short ex_ee_len;
  1594. unsigned uninitialized = 0;
  1595. struct ext4_extent *ex;
  1596. /* the header must be checked already in ext4_ext_remove_space() */
  1597. ext_debug("truncate since %u in leaf\n", start);
  1598. if (!path[depth].p_hdr)
  1599. path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
  1600. eh = path[depth].p_hdr;
  1601. BUG_ON(eh == NULL);
  1602. /* find where to start removing */
  1603. ex = EXT_LAST_EXTENT(eh);
  1604. ex_ee_block = le32_to_cpu(ex->ee_block);
  1605. if (ext4_ext_is_uninitialized(ex))
  1606. uninitialized = 1;
  1607. ex_ee_len = ext4_ext_get_actual_len(ex);
  1608. while (ex >= EXT_FIRST_EXTENT(eh) &&
  1609. ex_ee_block + ex_ee_len > start) {
  1610. ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
  1611. path[depth].p_ext = ex;
  1612. a = ex_ee_block > start ? ex_ee_block : start;
  1613. b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
  1614. ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
  1615. ext_debug(" border %u:%u\n", a, b);
  1616. if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
  1617. block = 0;
  1618. num = 0;
  1619. BUG();
  1620. } else if (a != ex_ee_block) {
  1621. /* remove tail of the extent */
  1622. block = ex_ee_block;
  1623. num = a - block;
  1624. } else if (b != ex_ee_block + ex_ee_len - 1) {
  1625. /* remove head of the extent */
  1626. block = a;
  1627. num = b - a;
  1628. /* there is no "make a hole" API yet */
  1629. BUG();
  1630. } else {
  1631. /* remove whole extent: excellent! */
  1632. block = ex_ee_block;
  1633. num = 0;
  1634. BUG_ON(a != ex_ee_block);
  1635. BUG_ON(b != ex_ee_block + ex_ee_len - 1);
  1636. }
  1637. /* at present, extent can't cross block group: */
  1638. /* leaf + bitmap + group desc + sb + inode */
  1639. credits = 5;
  1640. if (ex == EXT_FIRST_EXTENT(eh)) {
  1641. correct_index = 1;
  1642. credits += (ext_depth(inode)) + 1;
  1643. }
  1644. #ifdef CONFIG_QUOTA
  1645. credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
  1646. #endif
  1647. handle = ext4_ext_journal_restart(handle, credits);
  1648. if (IS_ERR(handle)) {
  1649. err = PTR_ERR(handle);
  1650. goto out;
  1651. }
  1652. err = ext4_ext_get_access(handle, inode, path + depth);
  1653. if (err)
  1654. goto out;
  1655. err = ext4_remove_blocks(handle, inode, ex, a, b);
  1656. if (err)
  1657. goto out;
  1658. if (num == 0) {
  1659. /* this extent is removed; mark slot entirely unused */
  1660. ext4_ext_store_pblock(ex, 0);
  1661. eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
  1662. }
  1663. ex->ee_block = cpu_to_le32(block);
  1664. ex->ee_len = cpu_to_le16(num);
  1665. /*
  1666. * Do not mark uninitialized if all the blocks in the
  1667. * extent have been removed.
  1668. */
  1669. if (uninitialized && num)
  1670. ext4_ext_mark_uninitialized(ex);
  1671. err = ext4_ext_dirty(handle, inode, path + depth);
  1672. if (err)
  1673. goto out;
  1674. ext_debug("new extent: %u:%u:%llu\n", block, num,
  1675. ext_pblock(ex));
  1676. ex--;
  1677. ex_ee_block = le32_to_cpu(ex->ee_block);
  1678. ex_ee_len = ext4_ext_get_actual_len(ex);
  1679. }
  1680. if (correct_index && eh->eh_entries)
  1681. err = ext4_ext_correct_indexes(handle, inode, path);
  1682. /* if this leaf is free, then we should
  1683. * remove it from index block above */
  1684. if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
  1685. err = ext4_ext_rm_idx(handle, inode, path + depth);
  1686. out:
  1687. return err;
  1688. }
  1689. /*
  1690. * ext4_ext_more_to_rm:
  1691. * returns 1 if current index has to be freed (even partial)
  1692. */
  1693. static int
  1694. ext4_ext_more_to_rm(struct ext4_ext_path *path)
  1695. {
  1696. BUG_ON(path->p_idx == NULL);
  1697. if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
  1698. return 0;
  1699. /*
  1700. * if truncate on deeper level happened, it wasn't partial,
  1701. * so we have to consider current index for truncation
  1702. */
  1703. if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
  1704. return 0;
  1705. return 1;
  1706. }
  1707. static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
  1708. {
  1709. struct super_block *sb = inode->i_sb;
  1710. int depth = ext_depth(inode);
  1711. struct ext4_ext_path *path;
  1712. handle_t *handle;
  1713. int i = 0, err = 0;
  1714. ext_debug("truncate since %u\n", start);
  1715. /* probably first extent we're gonna free will be last in block */
  1716. handle = ext4_journal_start(inode, depth + 1);
  1717. if (IS_ERR(handle))
  1718. return PTR_ERR(handle);
  1719. ext4_ext_invalidate_cache(inode);
  1720. /*
  1721. * We start scanning from right side, freeing all the blocks
  1722. * after i_size and walking into the tree depth-wise.
  1723. */
  1724. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_KERNEL);
  1725. if (path == NULL) {
  1726. ext4_journal_stop(handle);
  1727. return -ENOMEM;
  1728. }
  1729. path[0].p_hdr = ext_inode_hdr(inode);
  1730. if (ext4_ext_check_header(inode, path[0].p_hdr, depth)) {
  1731. err = -EIO;
  1732. goto out;
  1733. }
  1734. path[0].p_depth = depth;
  1735. while (i >= 0 && err == 0) {
  1736. if (i == depth) {
  1737. /* this is leaf block */
  1738. err = ext4_ext_rm_leaf(handle, inode, path, start);
  1739. /* root level has p_bh == NULL, brelse() eats this */
  1740. brelse(path[i].p_bh);
  1741. path[i].p_bh = NULL;
  1742. i--;
  1743. continue;
  1744. }
  1745. /* this is index block */
  1746. if (!path[i].p_hdr) {
  1747. ext_debug("initialize header\n");
  1748. path[i].p_hdr = ext_block_hdr(path[i].p_bh);
  1749. }
  1750. if (!path[i].p_idx) {
  1751. /* this level hasn't been touched yet */
  1752. path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
  1753. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
  1754. ext_debug("init index ptr: hdr 0x%p, num %d\n",
  1755. path[i].p_hdr,
  1756. le16_to_cpu(path[i].p_hdr->eh_entries));
  1757. } else {
  1758. /* we were already here, see at next index */
  1759. path[i].p_idx--;
  1760. }
  1761. ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
  1762. i, EXT_FIRST_INDEX(path[i].p_hdr),
  1763. path[i].p_idx);
  1764. if (ext4_ext_more_to_rm(path + i)) {
  1765. struct buffer_head *bh;
  1766. /* go to the next level */
  1767. ext_debug("move to level %d (block %llu)\n",
  1768. i + 1, idx_pblock(path[i].p_idx));
  1769. memset(path + i + 1, 0, sizeof(*path));
  1770. bh = sb_bread(sb, idx_pblock(path[i].p_idx));
  1771. if (!bh) {
  1772. /* should we reset i_size? */
  1773. err = -EIO;
  1774. break;
  1775. }
  1776. if (WARN_ON(i + 1 > depth)) {
  1777. err = -EIO;
  1778. break;
  1779. }
  1780. if (ext4_ext_check_header(inode, ext_block_hdr(bh),
  1781. depth - i - 1)) {
  1782. err = -EIO;
  1783. break;
  1784. }
  1785. path[i + 1].p_bh = bh;
  1786. /* save actual number of indexes since this
  1787. * number is changed at the next iteration */
  1788. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
  1789. i++;
  1790. } else {
  1791. /* we finished processing this index, go up */
  1792. if (path[i].p_hdr->eh_entries == 0 && i > 0) {
  1793. /* index is empty, remove it;
  1794. * handle must be already prepared by the
  1795. * truncatei_leaf() */
  1796. err = ext4_ext_rm_idx(handle, inode, path + i);
  1797. }
  1798. /* root level has p_bh == NULL, brelse() eats this */
  1799. brelse(path[i].p_bh);
  1800. path[i].p_bh = NULL;
  1801. i--;
  1802. ext_debug("return to level %d\n", i);
  1803. }
  1804. }
  1805. /* TODO: flexible tree reduction should be here */
  1806. if (path->p_hdr->eh_entries == 0) {
  1807. /*
  1808. * truncate to zero freed all the tree,
  1809. * so we need to correct eh_depth
  1810. */
  1811. err = ext4_ext_get_access(handle, inode, path);
  1812. if (err == 0) {
  1813. ext_inode_hdr(inode)->eh_depth = 0;
  1814. ext_inode_hdr(inode)->eh_max =
  1815. cpu_to_le16(ext4_ext_space_root(inode));
  1816. err = ext4_ext_dirty(handle, inode, path);
  1817. }
  1818. }
  1819. out:
  1820. ext4_ext_tree_changed(inode);
  1821. ext4_ext_drop_refs(path);
  1822. kfree(path);
  1823. ext4_journal_stop(handle);
  1824. return err;
  1825. }
  1826. /*
  1827. * called at mount time
  1828. */
  1829. void ext4_ext_init(struct super_block *sb)
  1830. {
  1831. /*
  1832. * possible initialization would be here
  1833. */
  1834. if (test_opt(sb, EXTENTS)) {
  1835. printk("EXT4-fs: file extents enabled");
  1836. #ifdef AGGRESSIVE_TEST
  1837. printk(", aggressive tests");
  1838. #endif
  1839. #ifdef CHECK_BINSEARCH
  1840. printk(", check binsearch");
  1841. #endif
  1842. #ifdef EXTENTS_STATS
  1843. printk(", stats");
  1844. #endif
  1845. printk("\n");
  1846. #ifdef EXTENTS_STATS
  1847. spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
  1848. EXT4_SB(sb)->s_ext_min = 1 << 30;
  1849. EXT4_SB(sb)->s_ext_max = 0;
  1850. #endif
  1851. }
  1852. }
  1853. /*
  1854. * called at umount time
  1855. */
  1856. void ext4_ext_release(struct super_block *sb)
  1857. {
  1858. if (!test_opt(sb, EXTENTS))
  1859. return;
  1860. #ifdef EXTENTS_STATS
  1861. if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
  1862. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1863. printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
  1864. sbi->s_ext_blocks, sbi->s_ext_extents,
  1865. sbi->s_ext_blocks / sbi->s_ext_extents);
  1866. printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
  1867. sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
  1868. }
  1869. #endif
  1870. }
  1871. /*
  1872. * This function is called by ext4_ext_get_blocks() if someone tries to write
  1873. * to an uninitialized extent. It may result in splitting the uninitialized
  1874. * extent into multiple extents (upto three - one initialized and two
  1875. * uninitialized).
  1876. * There are three possibilities:
  1877. * a> There is no split required: Entire extent should be initialized
  1878. * b> Splits in two extents: Write is happening at either end of the extent
  1879. * c> Splits in three extents: Somone is writing in middle of the extent
  1880. */
  1881. static int ext4_ext_convert_to_initialized(handle_t *handle,
  1882. struct inode *inode,
  1883. struct ext4_ext_path *path,
  1884. ext4_lblk_t iblock,
  1885. unsigned long max_blocks)
  1886. {
  1887. struct ext4_extent *ex, newex;
  1888. struct ext4_extent *ex1 = NULL;
  1889. struct ext4_extent *ex2 = NULL;
  1890. struct ext4_extent *ex3 = NULL;
  1891. struct ext4_extent_header *eh;
  1892. ext4_lblk_t ee_block;
  1893. unsigned int allocated, ee_len, depth;
  1894. ext4_fsblk_t newblock;
  1895. int err = 0;
  1896. int ret = 0;
  1897. depth = ext_depth(inode);
  1898. eh = path[depth].p_hdr;
  1899. ex = path[depth].p_ext;
  1900. ee_block = le32_to_cpu(ex->ee_block);
  1901. ee_len = ext4_ext_get_actual_len(ex);
  1902. allocated = ee_len - (iblock - ee_block);
  1903. newblock = iblock - ee_block + ext_pblock(ex);
  1904. ex2 = ex;
  1905. /* ex1: ee_block to iblock - 1 : uninitialized */
  1906. if (iblock > ee_block) {
  1907. ex1 = ex;
  1908. ex1->ee_len = cpu_to_le16(iblock - ee_block);
  1909. ext4_ext_mark_uninitialized(ex1);
  1910. ex2 = &newex;
  1911. }
  1912. /*
  1913. * for sanity, update the length of the ex2 extent before
  1914. * we insert ex3, if ex1 is NULL. This is to avoid temporary
  1915. * overlap of blocks.
  1916. */
  1917. if (!ex1 && allocated > max_blocks)
  1918. ex2->ee_len = cpu_to_le16(max_blocks);
  1919. /* ex3: to ee_block + ee_len : uninitialised */
  1920. if (allocated > max_blocks) {
  1921. unsigned int newdepth;
  1922. ex3 = &newex;
  1923. ex3->ee_block = cpu_to_le32(iblock + max_blocks);
  1924. ext4_ext_store_pblock(ex3, newblock + max_blocks);
  1925. ex3->ee_len = cpu_to_le16(allocated - max_blocks);
  1926. ext4_ext_mark_uninitialized(ex3);
  1927. err = ext4_ext_insert_extent(handle, inode, path, ex3);
  1928. if (err)
  1929. goto out;
  1930. /*
  1931. * The depth, and hence eh & ex might change
  1932. * as part of the insert above.
  1933. */
  1934. newdepth = ext_depth(inode);
  1935. if (newdepth != depth) {
  1936. depth = newdepth;
  1937. path = ext4_ext_find_extent(inode, iblock, NULL);
  1938. if (IS_ERR(path)) {
  1939. err = PTR_ERR(path);
  1940. path = NULL;
  1941. goto out;
  1942. }
  1943. eh = path[depth].p_hdr;
  1944. ex = path[depth].p_ext;
  1945. if (ex2 != &newex)
  1946. ex2 = ex;
  1947. }
  1948. allocated = max_blocks;
  1949. }
  1950. /*
  1951. * If there was a change of depth as part of the
  1952. * insertion of ex3 above, we need to update the length
  1953. * of the ex1 extent again here
  1954. */
  1955. if (ex1 && ex1 != ex) {
  1956. ex1 = ex;
  1957. ex1->ee_len = cpu_to_le16(iblock - ee_block);
  1958. ext4_ext_mark_uninitialized(ex1);
  1959. ex2 = &newex;
  1960. }
  1961. /* ex2: iblock to iblock + maxblocks-1 : initialised */
  1962. ex2->ee_block = cpu_to_le32(iblock);
  1963. ext4_ext_store_pblock(ex2, newblock);
  1964. ex2->ee_len = cpu_to_le16(allocated);
  1965. if (ex2 != ex)
  1966. goto insert;
  1967. err = ext4_ext_get_access(handle, inode, path + depth);
  1968. if (err)
  1969. goto out;
  1970. /*
  1971. * New (initialized) extent starts from the first block
  1972. * in the current extent. i.e., ex2 == ex
  1973. * We have to see if it can be merged with the extent
  1974. * on the left.
  1975. */
  1976. if (ex2 > EXT_FIRST_EXTENT(eh)) {
  1977. /*
  1978. * To merge left, pass "ex2 - 1" to try_to_merge(),
  1979. * since it merges towards right _only_.
  1980. */
  1981. ret = ext4_ext_try_to_merge(inode, path, ex2 - 1);
  1982. if (ret) {
  1983. err = ext4_ext_correct_indexes(handle, inode, path);
  1984. if (err)
  1985. goto out;
  1986. depth = ext_depth(inode);
  1987. ex2--;
  1988. }
  1989. }
  1990. /*
  1991. * Try to Merge towards right. This might be required
  1992. * only when the whole extent is being written to.
  1993. * i.e. ex2 == ex and ex3 == NULL.
  1994. */
  1995. if (!ex3) {
  1996. ret = ext4_ext_try_to_merge(inode, path, ex2);
  1997. if (ret) {
  1998. err = ext4_ext_correct_indexes(handle, inode, path);
  1999. if (err)
  2000. goto out;
  2001. }
  2002. }
  2003. /* Mark modified extent as dirty */
  2004. err = ext4_ext_dirty(handle, inode, path + depth);
  2005. goto out;
  2006. insert:
  2007. err = ext4_ext_insert_extent(handle, inode, path, &newex);
  2008. out:
  2009. return err ? err : allocated;
  2010. }
  2011. /*
  2012. * Need to be called with
  2013. * down_read(&EXT4_I(inode)->i_data_sem) if not allocating file system block
  2014. * (ie, create is zero). Otherwise down_write(&EXT4_I(inode)->i_data_sem)
  2015. */
  2016. int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
  2017. ext4_lblk_t iblock,
  2018. unsigned long max_blocks, struct buffer_head *bh_result,
  2019. int create, int extend_disksize)
  2020. {
  2021. struct ext4_ext_path *path = NULL;
  2022. struct ext4_extent_header *eh;
  2023. struct ext4_extent newex, *ex;
  2024. ext4_fsblk_t goal, newblock;
  2025. int err = 0, depth, ret;
  2026. unsigned long allocated = 0;
  2027. struct ext4_allocation_request ar;
  2028. __clear_bit(BH_New, &bh_result->b_state);
  2029. ext_debug("blocks %u/%lu requested for inode %u\n",
  2030. iblock, max_blocks, inode->i_ino);
  2031. /* check in cache */
  2032. goal = ext4_ext_in_cache(inode, iblock, &newex);
  2033. if (goal) {
  2034. if (goal == EXT4_EXT_CACHE_GAP) {
  2035. if (!create) {
  2036. /*
  2037. * block isn't allocated yet and
  2038. * user doesn't want to allocate it
  2039. */
  2040. goto out2;
  2041. }
  2042. /* we should allocate requested block */
  2043. } else if (goal == EXT4_EXT_CACHE_EXTENT) {
  2044. /* block is already allocated */
  2045. newblock = iblock
  2046. - le32_to_cpu(newex.ee_block)
  2047. + ext_pblock(&newex);
  2048. /* number of remaining blocks in the extent */
  2049. allocated = ext4_ext_get_actual_len(&newex) -
  2050. (iblock - le32_to_cpu(newex.ee_block));
  2051. goto out;
  2052. } else {
  2053. BUG();
  2054. }
  2055. }
  2056. /* find extent for this block */
  2057. path = ext4_ext_find_extent(inode, iblock, NULL);
  2058. if (IS_ERR(path)) {
  2059. err = PTR_ERR(path);
  2060. path = NULL;
  2061. goto out2;
  2062. }
  2063. depth = ext_depth(inode);
  2064. /*
  2065. * consistent leaf must not be empty;
  2066. * this situation is possible, though, _during_ tree modification;
  2067. * this is why assert can't be put in ext4_ext_find_extent()
  2068. */
  2069. BUG_ON(path[depth].p_ext == NULL && depth != 0);
  2070. eh = path[depth].p_hdr;
  2071. ex = path[depth].p_ext;
  2072. if (ex) {
  2073. ext4_lblk_t ee_block = le32_to_cpu(ex->ee_block);
  2074. ext4_fsblk_t ee_start = ext_pblock(ex);
  2075. unsigned short ee_len;
  2076. /*
  2077. * Uninitialized extents are treated as holes, except that
  2078. * we split out initialized portions during a write.
  2079. */
  2080. ee_len = ext4_ext_get_actual_len(ex);
  2081. /* if found extent covers block, simply return it */
  2082. if (iblock >= ee_block && iblock < ee_block + ee_len) {
  2083. newblock = iblock - ee_block + ee_start;
  2084. /* number of remaining blocks in the extent */
  2085. allocated = ee_len - (iblock - ee_block);
  2086. ext_debug("%u fit into %lu:%d -> %llu\n", iblock,
  2087. ee_block, ee_len, newblock);
  2088. /* Do not put uninitialized extent in the cache */
  2089. if (!ext4_ext_is_uninitialized(ex)) {
  2090. ext4_ext_put_in_cache(inode, ee_block,
  2091. ee_len, ee_start,
  2092. EXT4_EXT_CACHE_EXTENT);
  2093. goto out;
  2094. }
  2095. if (create == EXT4_CREATE_UNINITIALIZED_EXT)
  2096. goto out;
  2097. if (!create)
  2098. goto out2;
  2099. ret = ext4_ext_convert_to_initialized(handle, inode,
  2100. path, iblock,
  2101. max_blocks);
  2102. if (ret <= 0) {
  2103. err = ret;
  2104. goto out2;
  2105. } else
  2106. allocated = ret;
  2107. goto outnew;
  2108. }
  2109. }
  2110. /*
  2111. * requested block isn't allocated yet;
  2112. * we couldn't try to create block if create flag is zero
  2113. */
  2114. if (!create) {
  2115. /*
  2116. * put just found gap into cache to speed up
  2117. * subsequent requests
  2118. */
  2119. ext4_ext_put_gap_in_cache(inode, path, iblock);
  2120. goto out2;
  2121. }
  2122. /*
  2123. * Okay, we need to do block allocation. Lazily initialize the block
  2124. * allocation info here if necessary.
  2125. */
  2126. if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
  2127. ext4_init_block_alloc_info(inode);
  2128. /* find neighbour allocated blocks */
  2129. ar.lleft = iblock;
  2130. err = ext4_ext_search_left(inode, path, &ar.lleft, &ar.pleft);
  2131. if (err)
  2132. goto out2;
  2133. ar.lright = iblock;
  2134. err = ext4_ext_search_right(inode, path, &ar.lright, &ar.pright);
  2135. if (err)
  2136. goto out2;
  2137. /*
  2138. * See if request is beyond maximum number of blocks we can have in
  2139. * a single extent. For an initialized extent this limit is
  2140. * EXT_INIT_MAX_LEN and for an uninitialized extent this limit is
  2141. * EXT_UNINIT_MAX_LEN.
  2142. */
  2143. if (max_blocks > EXT_INIT_MAX_LEN &&
  2144. create != EXT4_CREATE_UNINITIALIZED_EXT)
  2145. max_blocks = EXT_INIT_MAX_LEN;
  2146. else if (max_blocks > EXT_UNINIT_MAX_LEN &&
  2147. create == EXT4_CREATE_UNINITIALIZED_EXT)
  2148. max_blocks = EXT_UNINIT_MAX_LEN;
  2149. /* Check if we can really insert (iblock)::(iblock+max_blocks) extent */
  2150. newex.ee_block = cpu_to_le32(iblock);
  2151. newex.ee_len = cpu_to_le16(max_blocks);
  2152. err = ext4_ext_check_overlap(inode, &newex, path);
  2153. if (err)
  2154. allocated = ext4_ext_get_actual_len(&newex);
  2155. else
  2156. allocated = max_blocks;
  2157. /* allocate new block */
  2158. ar.inode = inode;
  2159. ar.goal = ext4_ext_find_goal(inode, path, iblock);
  2160. ar.logical = iblock;
  2161. ar.len = allocated;
  2162. if (S_ISREG(inode->i_mode))
  2163. ar.flags = EXT4_MB_HINT_DATA;
  2164. else
  2165. /* disable in-core preallocation for non-regular files */
  2166. ar.flags = 0;
  2167. newblock = ext4_mb_new_blocks(handle, &ar, &err);
  2168. if (!newblock)
  2169. goto out2;
  2170. ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
  2171. goal, newblock, allocated);
  2172. /* try to insert new extent into found leaf and return */
  2173. ext4_ext_store_pblock(&newex, newblock);
  2174. newex.ee_len = cpu_to_le16(ar.len);
  2175. if (create == EXT4_CREATE_UNINITIALIZED_EXT) /* Mark uninitialized */
  2176. ext4_ext_mark_uninitialized(&newex);
  2177. err = ext4_ext_insert_extent(handle, inode, path, &newex);
  2178. if (err) {
  2179. /* free data blocks we just allocated */
  2180. /* not a good idea to call discard here directly,
  2181. * but otherwise we'd need to call it every free() */
  2182. ext4_mb_discard_inode_preallocations(inode);
  2183. ext4_free_blocks(handle, inode, ext_pblock(&newex),
  2184. ext4_ext_get_actual_len(&newex), 0);
  2185. goto out2;
  2186. }
  2187. if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize)
  2188. EXT4_I(inode)->i_disksize = inode->i_size;
  2189. /* previous routine could use block we allocated */
  2190. newblock = ext_pblock(&newex);
  2191. allocated = ext4_ext_get_actual_len(&newex);
  2192. outnew:
  2193. __set_bit(BH_New, &bh_result->b_state);
  2194. /* Cache only when it is _not_ an uninitialized extent */
  2195. if (create != EXT4_CREATE_UNINITIALIZED_EXT)
  2196. ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
  2197. EXT4_EXT_CACHE_EXTENT);
  2198. out:
  2199. if (allocated > max_blocks)
  2200. allocated = max_blocks;
  2201. ext4_ext_show_leaf(inode, path);
  2202. __set_bit(BH_Mapped, &bh_result->b_state);
  2203. bh_result->b_bdev = inode->i_sb->s_bdev;
  2204. bh_result->b_blocknr = newblock;
  2205. out2:
  2206. if (path) {
  2207. ext4_ext_drop_refs(path);
  2208. kfree(path);
  2209. }
  2210. return err ? err : allocated;
  2211. }
  2212. void ext4_ext_truncate(struct inode * inode, struct page *page)
  2213. {
  2214. struct address_space *mapping = inode->i_mapping;
  2215. struct super_block *sb = inode->i_sb;
  2216. ext4_lblk_t last_block;
  2217. handle_t *handle;
  2218. int err = 0;
  2219. /*
  2220. * probably first extent we're gonna free will be last in block
  2221. */
  2222. err = ext4_writepage_trans_blocks(inode) + 3;
  2223. handle = ext4_journal_start(inode, err);
  2224. if (IS_ERR(handle)) {
  2225. if (page) {
  2226. clear_highpage(page);
  2227. flush_dcache_page(page);
  2228. unlock_page(page);
  2229. page_cache_release(page);
  2230. }
  2231. return;
  2232. }
  2233. if (page)
  2234. ext4_block_truncate_page(handle, page, mapping, inode->i_size);
  2235. down_write(&EXT4_I(inode)->i_data_sem);
  2236. ext4_ext_invalidate_cache(inode);
  2237. ext4_mb_discard_inode_preallocations(inode);
  2238. /*
  2239. * TODO: optimization is possible here.
  2240. * Probably we need not scan at all,
  2241. * because page truncation is enough.
  2242. */
  2243. if (ext4_orphan_add(handle, inode))
  2244. goto out_stop;
  2245. /* we have to know where to truncate from in crash case */
  2246. EXT4_I(inode)->i_disksize = inode->i_size;
  2247. ext4_mark_inode_dirty(handle, inode);
  2248. last_block = (inode->i_size + sb->s_blocksize - 1)
  2249. >> EXT4_BLOCK_SIZE_BITS(sb);
  2250. err = ext4_ext_remove_space(inode, last_block);
  2251. /* In a multi-transaction truncate, we only make the final
  2252. * transaction synchronous.
  2253. */
  2254. if (IS_SYNC(inode))
  2255. handle->h_sync = 1;
  2256. out_stop:
  2257. /*
  2258. * If this was a simple ftruncate() and the file will remain alive,
  2259. * then we need to clear up the orphan record which we created above.
  2260. * However, if this was a real unlink then we were called by
  2261. * ext4_delete_inode(), and we allow that function to clean up the
  2262. * orphan info for us.
  2263. */
  2264. if (inode->i_nlink)
  2265. ext4_orphan_del(handle, inode);
  2266. up_write(&EXT4_I(inode)->i_data_sem);
  2267. ext4_journal_stop(handle);
  2268. }
  2269. /*
  2270. * ext4_ext_writepage_trans_blocks:
  2271. * calculate max number of blocks we could modify
  2272. * in order to allocate new block for an inode
  2273. */
  2274. int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
  2275. {
  2276. int needed;
  2277. needed = ext4_ext_calc_credits_for_insert(inode, NULL);
  2278. /* caller wants to allocate num blocks, but note it includes sb */
  2279. needed = needed * num - (num - 1);
  2280. #ifdef CONFIG_QUOTA
  2281. needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
  2282. #endif
  2283. return needed;
  2284. }
  2285. /*
  2286. * preallocate space for a file. This implements ext4's fallocate inode
  2287. * operation, which gets called from sys_fallocate system call.
  2288. * For block-mapped files, posix_fallocate should fall back to the method
  2289. * of writing zeroes to the required new blocks (the same behavior which is
  2290. * expected for file systems which do not support fallocate() system call).
  2291. */
  2292. long ext4_fallocate(struct inode *inode, int mode, loff_t offset, loff_t len)
  2293. {
  2294. handle_t *handle;
  2295. ext4_lblk_t block;
  2296. unsigned long max_blocks;
  2297. ext4_fsblk_t nblocks = 0;
  2298. int ret = 0;
  2299. int ret2 = 0;
  2300. int retries = 0;
  2301. struct buffer_head map_bh;
  2302. unsigned int credits, blkbits = inode->i_blkbits;
  2303. /*
  2304. * currently supporting (pre)allocate mode for extent-based
  2305. * files _only_
  2306. */
  2307. if (!(EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
  2308. return -EOPNOTSUPP;
  2309. /* preallocation to directories is currently not supported */
  2310. if (S_ISDIR(inode->i_mode))
  2311. return -ENODEV;
  2312. block = offset >> blkbits;
  2313. max_blocks = (EXT4_BLOCK_ALIGN(len + offset, blkbits) >> blkbits)
  2314. - block;
  2315. /*
  2316. * credits to insert 1 extent into extent tree + buffers to be able to
  2317. * modify 1 super block, 1 block bitmap and 1 group descriptor.
  2318. */
  2319. credits = EXT4_DATA_TRANS_BLOCKS(inode->i_sb) + 3;
  2320. down_write((&EXT4_I(inode)->i_data_sem));
  2321. retry:
  2322. while (ret >= 0 && ret < max_blocks) {
  2323. block = block + ret;
  2324. max_blocks = max_blocks - ret;
  2325. handle = ext4_journal_start(inode, credits);
  2326. if (IS_ERR(handle)) {
  2327. ret = PTR_ERR(handle);
  2328. break;
  2329. }
  2330. ret = ext4_ext_get_blocks(handle, inode, block,
  2331. max_blocks, &map_bh,
  2332. EXT4_CREATE_UNINITIALIZED_EXT, 0);
  2333. WARN_ON(ret <= 0);
  2334. if (ret <= 0) {
  2335. ext4_error(inode->i_sb, "ext4_fallocate",
  2336. "ext4_ext_get_blocks returned error: "
  2337. "inode#%lu, block=%u, max_blocks=%lu",
  2338. inode->i_ino, block, max_blocks);
  2339. ret = -EIO;
  2340. ext4_mark_inode_dirty(handle, inode);
  2341. ret2 = ext4_journal_stop(handle);
  2342. break;
  2343. }
  2344. if (ret > 0) {
  2345. /* check wrap through sign-bit/zero here */
  2346. if ((block + ret) < 0 || (block + ret) < block) {
  2347. ret = -EIO;
  2348. ext4_mark_inode_dirty(handle, inode);
  2349. ret2 = ext4_journal_stop(handle);
  2350. break;
  2351. }
  2352. if (buffer_new(&map_bh) && ((block + ret) >
  2353. (EXT4_BLOCK_ALIGN(i_size_read(inode), blkbits)
  2354. >> blkbits)))
  2355. nblocks = nblocks + ret;
  2356. }
  2357. /* Update ctime if new blocks get allocated */
  2358. if (nblocks) {
  2359. struct timespec now;
  2360. now = current_fs_time(inode->i_sb);
  2361. if (!timespec_equal(&inode->i_ctime, &now))
  2362. inode->i_ctime = now;
  2363. }
  2364. ext4_mark_inode_dirty(handle, inode);
  2365. ret2 = ext4_journal_stop(handle);
  2366. if (ret2)
  2367. break;
  2368. }
  2369. if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries))
  2370. goto retry;
  2371. up_write((&EXT4_I(inode)->i_data_sem));
  2372. /*
  2373. * Time to update the file size.
  2374. * Update only when preallocation was requested beyond the file size.
  2375. */
  2376. if (!(mode & FALLOC_FL_KEEP_SIZE) &&
  2377. (offset + len) > i_size_read(inode)) {
  2378. if (ret > 0) {
  2379. /*
  2380. * if no error, we assume preallocation succeeded
  2381. * completely
  2382. */
  2383. mutex_lock(&inode->i_mutex);
  2384. i_size_write(inode, offset + len);
  2385. EXT4_I(inode)->i_disksize = i_size_read(inode);
  2386. mutex_unlock(&inode->i_mutex);
  2387. } else if (ret < 0 && nblocks) {
  2388. /* Handle partial allocation scenario */
  2389. loff_t newsize;
  2390. mutex_lock(&inode->i_mutex);
  2391. newsize = (nblocks << blkbits) + i_size_read(inode);
  2392. i_size_write(inode, EXT4_BLOCK_ALIGN(newsize, blkbits));
  2393. EXT4_I(inode)->i_disksize = i_size_read(inode);
  2394. mutex_unlock(&inode->i_mutex);
  2395. }
  2396. }
  2397. return ret > 0 ? ret2 : ret;
  2398. }