extents.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168
  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/jbd.h>
  35. #include <linux/smp_lock.h>
  36. #include <linux/highuid.h>
  37. #include <linux/pagemap.h>
  38. #include <linux/quotaops.h>
  39. #include <linux/string.h>
  40. #include <linux/slab.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);
  51. block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
  52. return block;
  53. }
  54. /*
  55. * idx_pblock:
  56. * combine low and high parts of a leaf physical block number into ext4_fsblk_t
  57. */
  58. static ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
  59. {
  60. ext4_fsblk_t block;
  61. block = le32_to_cpu(ix->ei_leaf);
  62. block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
  63. return block;
  64. }
  65. /*
  66. * ext4_ext_store_pblock:
  67. * stores a large physical block number into an extent struct,
  68. * breaking it into parts
  69. */
  70. static void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
  71. {
  72. ex->ee_start = 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 = cpu_to_le32((unsigned long) (pb & 0xffffffff));
  83. ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
  84. }
  85. static int ext4_ext_check_header(const char *function, struct inode *inode,
  86. struct ext4_extent_header *eh)
  87. {
  88. const char *error_msg = NULL;
  89. if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
  90. error_msg = "invalid magic";
  91. goto corrupted;
  92. }
  93. if (unlikely(eh->eh_max == 0)) {
  94. error_msg = "invalid eh_max";
  95. goto corrupted;
  96. }
  97. if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
  98. error_msg = "invalid eh_entries";
  99. goto corrupted;
  100. }
  101. return 0;
  102. corrupted:
  103. ext4_error(inode->i_sb, function,
  104. "bad header in inode #%lu: %s - magic %x, "
  105. "entries %u, max %u, depth %u",
  106. inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
  107. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
  108. le16_to_cpu(eh->eh_depth));
  109. return -EIO;
  110. }
  111. static handle_t *ext4_ext_journal_restart(handle_t *handle, int needed)
  112. {
  113. int err;
  114. if (handle->h_buffer_credits > needed)
  115. return handle;
  116. if (!ext4_journal_extend(handle, needed))
  117. return handle;
  118. err = ext4_journal_restart(handle, needed);
  119. return handle;
  120. }
  121. /*
  122. * could return:
  123. * - EROFS
  124. * - ENOMEM
  125. */
  126. static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
  127. struct ext4_ext_path *path)
  128. {
  129. if (path->p_bh) {
  130. /* path points to block */
  131. return ext4_journal_get_write_access(handle, path->p_bh);
  132. }
  133. /* path points to leaf/index in inode body */
  134. /* we use in-core data, no need to protect them */
  135. return 0;
  136. }
  137. /*
  138. * could return:
  139. * - EROFS
  140. * - ENOMEM
  141. * - EIO
  142. */
  143. static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
  144. struct ext4_ext_path *path)
  145. {
  146. int err;
  147. if (path->p_bh) {
  148. /* path points to block */
  149. err = ext4_journal_dirty_metadata(handle, path->p_bh);
  150. } else {
  151. /* path points to leaf/index in inode body */
  152. err = ext4_mark_inode_dirty(handle, inode);
  153. }
  154. return err;
  155. }
  156. static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
  157. struct ext4_ext_path *path,
  158. ext4_fsblk_t block)
  159. {
  160. struct ext4_inode_info *ei = EXT4_I(inode);
  161. ext4_fsblk_t bg_start;
  162. ext4_grpblk_t colour;
  163. int depth;
  164. if (path) {
  165. struct ext4_extent *ex;
  166. depth = path->p_depth;
  167. /* try to predict block placement */
  168. ex = path[depth].p_ext;
  169. if (ex)
  170. return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
  171. /* it looks like index is empty;
  172. * try to find starting block from index itself */
  173. if (path[depth].p_bh)
  174. return path[depth].p_bh->b_blocknr;
  175. }
  176. /* OK. use inode's group */
  177. bg_start = (ei->i_block_group * EXT4_BLOCKS_PER_GROUP(inode->i_sb)) +
  178. le32_to_cpu(EXT4_SB(inode->i_sb)->s_es->s_first_data_block);
  179. colour = (current->pid % 16) *
  180. (EXT4_BLOCKS_PER_GROUP(inode->i_sb) / 16);
  181. return bg_start + colour + block;
  182. }
  183. static ext4_fsblk_t
  184. ext4_ext_new_block(handle_t *handle, struct inode *inode,
  185. struct ext4_ext_path *path,
  186. struct ext4_extent *ex, int *err)
  187. {
  188. ext4_fsblk_t goal, newblock;
  189. goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
  190. newblock = ext4_new_block(handle, inode, goal, err);
  191. return newblock;
  192. }
  193. static int ext4_ext_space_block(struct inode *inode)
  194. {
  195. int size;
  196. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  197. / sizeof(struct ext4_extent);
  198. #ifdef AGGRESSIVE_TEST
  199. if (size > 6)
  200. size = 6;
  201. #endif
  202. return size;
  203. }
  204. static int ext4_ext_space_block_idx(struct inode *inode)
  205. {
  206. int size;
  207. size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
  208. / sizeof(struct ext4_extent_idx);
  209. #ifdef AGGRESSIVE_TEST
  210. if (size > 5)
  211. size = 5;
  212. #endif
  213. return size;
  214. }
  215. static int ext4_ext_space_root(struct inode *inode)
  216. {
  217. int size;
  218. size = sizeof(EXT4_I(inode)->i_data);
  219. size -= sizeof(struct ext4_extent_header);
  220. size /= sizeof(struct ext4_extent);
  221. #ifdef AGGRESSIVE_TEST
  222. if (size > 3)
  223. size = 3;
  224. #endif
  225. return size;
  226. }
  227. static int ext4_ext_space_root_idx(struct inode *inode)
  228. {
  229. int size;
  230. size = sizeof(EXT4_I(inode)->i_data);
  231. size -= sizeof(struct ext4_extent_header);
  232. size /= sizeof(struct ext4_extent_idx);
  233. #ifdef AGGRESSIVE_TEST
  234. if (size > 4)
  235. size = 4;
  236. #endif
  237. return size;
  238. }
  239. #ifdef EXT_DEBUG
  240. static void ext4_ext_show_path(struct inode *inode, struct ext4_ext_path *path)
  241. {
  242. int k, l = path->p_depth;
  243. ext_debug("path:");
  244. for (k = 0; k <= l; k++, path++) {
  245. if (path->p_idx) {
  246. ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
  247. idx_pblock(path->p_idx));
  248. } else if (path->p_ext) {
  249. ext_debug(" %d:%d:%llu ",
  250. le32_to_cpu(path->p_ext->ee_block),
  251. le16_to_cpu(path->p_ext->ee_len),
  252. ext_pblock(path->p_ext));
  253. } else
  254. ext_debug(" []");
  255. }
  256. ext_debug("\n");
  257. }
  258. static void ext4_ext_show_leaf(struct inode *inode, struct ext4_ext_path *path)
  259. {
  260. int depth = ext_depth(inode);
  261. struct ext4_extent_header *eh;
  262. struct ext4_extent *ex;
  263. int i;
  264. if (!path)
  265. return;
  266. eh = path[depth].p_hdr;
  267. ex = EXT_FIRST_EXTENT(eh);
  268. for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
  269. ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
  270. le16_to_cpu(ex->ee_len), ext_pblock(ex));
  271. }
  272. ext_debug("\n");
  273. }
  274. #else
  275. #define ext4_ext_show_path(inode,path)
  276. #define ext4_ext_show_leaf(inode,path)
  277. #endif
  278. static void ext4_ext_drop_refs(struct ext4_ext_path *path)
  279. {
  280. int depth = path->p_depth;
  281. int i;
  282. for (i = 0; i <= depth; i++, path++)
  283. if (path->p_bh) {
  284. brelse(path->p_bh);
  285. path->p_bh = NULL;
  286. }
  287. }
  288. /*
  289. * ext4_ext_binsearch_idx:
  290. * binary search for the closest index of the given block
  291. */
  292. static void
  293. ext4_ext_binsearch_idx(struct inode *inode, struct ext4_ext_path *path, int block)
  294. {
  295. struct ext4_extent_header *eh = path->p_hdr;
  296. struct ext4_extent_idx *r, *l, *m;
  297. BUG_ON(eh->eh_magic != EXT4_EXT_MAGIC);
  298. BUG_ON(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max));
  299. BUG_ON(le16_to_cpu(eh->eh_entries) <= 0);
  300. ext_debug("binsearch for %d(idx): ", block);
  301. l = EXT_FIRST_INDEX(eh) + 1;
  302. r = EXT_FIRST_INDEX(eh) + le16_to_cpu(eh->eh_entries) - 1;
  303. while (l <= r) {
  304. m = l + (r - l) / 2;
  305. if (block < le32_to_cpu(m->ei_block))
  306. r = m - 1;
  307. else
  308. l = m + 1;
  309. ext_debug("%p(%u):%p(%u):%p(%u) ", l, l->ei_block,
  310. m, m->ei_block, r, r->ei_block);
  311. }
  312. path->p_idx = l - 1;
  313. ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
  314. idx_block(path->p_idx));
  315. #ifdef CHECK_BINSEARCH
  316. {
  317. struct ext4_extent_idx *chix, *ix;
  318. int k;
  319. chix = ix = EXT_FIRST_INDEX(eh);
  320. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ix++) {
  321. if (k != 0 &&
  322. le32_to_cpu(ix->ei_block) <= le32_to_cpu(ix[-1].ei_block)) {
  323. printk("k=%d, ix=0x%p, first=0x%p\n", k,
  324. ix, EXT_FIRST_INDEX(eh));
  325. printk("%u <= %u\n",
  326. le32_to_cpu(ix->ei_block),
  327. le32_to_cpu(ix[-1].ei_block));
  328. }
  329. BUG_ON(k && le32_to_cpu(ix->ei_block)
  330. <= le32_to_cpu(ix[-1].ei_block));
  331. if (block < le32_to_cpu(ix->ei_block))
  332. break;
  333. chix = ix;
  334. }
  335. BUG_ON(chix != path->p_idx);
  336. }
  337. #endif
  338. }
  339. /*
  340. * ext4_ext_binsearch:
  341. * binary search for closest extent of the given block
  342. */
  343. static void
  344. ext4_ext_binsearch(struct inode *inode, struct ext4_ext_path *path, int block)
  345. {
  346. struct ext4_extent_header *eh = path->p_hdr;
  347. struct ext4_extent *r, *l, *m;
  348. BUG_ON(eh->eh_magic != EXT4_EXT_MAGIC);
  349. BUG_ON(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max));
  350. if (eh->eh_entries == 0) {
  351. /*
  352. * this leaf is empty:
  353. * we get such a leaf in split/add case
  354. */
  355. return;
  356. }
  357. ext_debug("binsearch for %d: ", block);
  358. l = EXT_FIRST_EXTENT(eh) + 1;
  359. r = EXT_FIRST_EXTENT(eh) + le16_to_cpu(eh->eh_entries) - 1;
  360. while (l <= r) {
  361. m = l + (r - l) / 2;
  362. if (block < le32_to_cpu(m->ee_block))
  363. r = m - 1;
  364. else
  365. l = m + 1;
  366. ext_debug("%p(%u):%p(%u):%p(%u) ", l, l->ee_block,
  367. m, m->ee_block, r, r->ee_block);
  368. }
  369. path->p_ext = l - 1;
  370. ext_debug(" -> %d:%llu:%d ",
  371. le32_to_cpu(path->p_ext->ee_block),
  372. ext_pblock(path->p_ext),
  373. le16_to_cpu(path->p_ext->ee_len));
  374. #ifdef CHECK_BINSEARCH
  375. {
  376. struct ext4_extent *chex, *ex;
  377. int k;
  378. chex = ex = EXT_FIRST_EXTENT(eh);
  379. for (k = 0; k < le16_to_cpu(eh->eh_entries); k++, ex++) {
  380. BUG_ON(k && le32_to_cpu(ex->ee_block)
  381. <= le32_to_cpu(ex[-1].ee_block));
  382. if (block < le32_to_cpu(ex->ee_block))
  383. break;
  384. chex = ex;
  385. }
  386. BUG_ON(chex != path->p_ext);
  387. }
  388. #endif
  389. }
  390. int ext4_ext_tree_init(handle_t *handle, struct inode *inode)
  391. {
  392. struct ext4_extent_header *eh;
  393. eh = ext_inode_hdr(inode);
  394. eh->eh_depth = 0;
  395. eh->eh_entries = 0;
  396. eh->eh_magic = EXT4_EXT_MAGIC;
  397. eh->eh_max = cpu_to_le16(ext4_ext_space_root(inode));
  398. ext4_mark_inode_dirty(handle, inode);
  399. ext4_ext_invalidate_cache(inode);
  400. return 0;
  401. }
  402. struct ext4_ext_path *
  403. ext4_ext_find_extent(struct inode *inode, int block, struct ext4_ext_path *path)
  404. {
  405. struct ext4_extent_header *eh;
  406. struct buffer_head *bh;
  407. short int depth, i, ppos = 0, alloc = 0;
  408. eh = ext_inode_hdr(inode);
  409. BUG_ON(eh == NULL);
  410. if (ext4_ext_check_header(__FUNCTION__, inode, eh))
  411. return ERR_PTR(-EIO);
  412. i = depth = ext_depth(inode);
  413. /* account possible depth increase */
  414. if (!path) {
  415. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
  416. GFP_NOFS);
  417. if (!path)
  418. return ERR_PTR(-ENOMEM);
  419. alloc = 1;
  420. }
  421. path[0].p_hdr = eh;
  422. /* walk through the tree */
  423. while (i) {
  424. ext_debug("depth %d: num %d, max %d\n",
  425. ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  426. ext4_ext_binsearch_idx(inode, path + ppos, block);
  427. path[ppos].p_block = idx_pblock(path[ppos].p_idx);
  428. path[ppos].p_depth = i;
  429. path[ppos].p_ext = NULL;
  430. bh = sb_bread(inode->i_sb, path[ppos].p_block);
  431. if (!bh)
  432. goto err;
  433. eh = ext_block_hdr(bh);
  434. ppos++;
  435. BUG_ON(ppos > depth);
  436. path[ppos].p_bh = bh;
  437. path[ppos].p_hdr = eh;
  438. i--;
  439. if (ext4_ext_check_header(__FUNCTION__, inode, eh))
  440. goto err;
  441. }
  442. path[ppos].p_depth = i;
  443. path[ppos].p_hdr = eh;
  444. path[ppos].p_ext = NULL;
  445. path[ppos].p_idx = NULL;
  446. if (ext4_ext_check_header(__FUNCTION__, inode, eh))
  447. goto err;
  448. /* find extent */
  449. ext4_ext_binsearch(inode, path + ppos, block);
  450. ext4_ext_show_path(inode, path);
  451. return path;
  452. err:
  453. ext4_ext_drop_refs(path);
  454. if (alloc)
  455. kfree(path);
  456. return ERR_PTR(-EIO);
  457. }
  458. /*
  459. * ext4_ext_insert_index:
  460. * insert new index [@logical;@ptr] into the block at @curp;
  461. * check where to insert: before @curp or after @curp
  462. */
  463. static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
  464. struct ext4_ext_path *curp,
  465. int logical, ext4_fsblk_t ptr)
  466. {
  467. struct ext4_extent_idx *ix;
  468. int len, err;
  469. err = ext4_ext_get_access(handle, inode, curp);
  470. if (err)
  471. return err;
  472. BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
  473. len = EXT_MAX_INDEX(curp->p_hdr) - curp->p_idx;
  474. if (logical > le32_to_cpu(curp->p_idx->ei_block)) {
  475. /* insert after */
  476. if (curp->p_idx != EXT_LAST_INDEX(curp->p_hdr)) {
  477. len = (len - 1) * sizeof(struct ext4_extent_idx);
  478. len = len < 0 ? 0 : len;
  479. ext_debug("insert new index %d after: %d. "
  480. "move %d from 0x%p to 0x%p\n",
  481. logical, ptr, len,
  482. (curp->p_idx + 1), (curp->p_idx + 2));
  483. memmove(curp->p_idx + 2, curp->p_idx + 1, len);
  484. }
  485. ix = curp->p_idx + 1;
  486. } else {
  487. /* insert before */
  488. len = len * sizeof(struct ext4_extent_idx);
  489. len = len < 0 ? 0 : len;
  490. ext_debug("insert new index %d before: %d. "
  491. "move %d from 0x%p to 0x%p\n",
  492. logical, ptr, len,
  493. curp->p_idx, (curp->p_idx + 1));
  494. memmove(curp->p_idx + 1, curp->p_idx, len);
  495. ix = curp->p_idx;
  496. }
  497. ix->ei_block = cpu_to_le32(logical);
  498. ext4_idx_store_pblock(ix, ptr);
  499. curp->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(curp->p_hdr->eh_entries)+1);
  500. BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
  501. > le16_to_cpu(curp->p_hdr->eh_max));
  502. BUG_ON(ix > EXT_LAST_INDEX(curp->p_hdr));
  503. err = ext4_ext_dirty(handle, inode, curp);
  504. ext4_std_error(inode->i_sb, err);
  505. return err;
  506. }
  507. /*
  508. * ext4_ext_split:
  509. * inserts new subtree into the path, using free index entry
  510. * at depth @at:
  511. * - allocates all needed blocks (new leaf and all intermediate index blocks)
  512. * - makes decision where to split
  513. * - moves remaining extents and index entries (right to the split point)
  514. * into the newly allocated blocks
  515. * - initializes subtree
  516. */
  517. static int ext4_ext_split(handle_t *handle, struct inode *inode,
  518. struct ext4_ext_path *path,
  519. struct ext4_extent *newext, int at)
  520. {
  521. struct buffer_head *bh = NULL;
  522. int depth = ext_depth(inode);
  523. struct ext4_extent_header *neh;
  524. struct ext4_extent_idx *fidx;
  525. struct ext4_extent *ex;
  526. int i = at, k, m, a;
  527. ext4_fsblk_t newblock, oldblock;
  528. __le32 border;
  529. ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
  530. int err = 0;
  531. /* make decision: where to split? */
  532. /* FIXME: now decision is simplest: at current extent */
  533. /* if current leaf will be split, then we should use
  534. * border from split point */
  535. BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
  536. if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
  537. border = path[depth].p_ext[1].ee_block;
  538. ext_debug("leaf will be split."
  539. " next leaf starts at %d\n",
  540. le32_to_cpu(border));
  541. } else {
  542. border = newext->ee_block;
  543. ext_debug("leaf will be added."
  544. " next leaf starts at %d\n",
  545. le32_to_cpu(border));
  546. }
  547. /*
  548. * If error occurs, then we break processing
  549. * and mark filesystem read-only. index won't
  550. * be inserted and tree will be in consistent
  551. * state. Next mount will repair buffers too.
  552. */
  553. /*
  554. * Get array to track all allocated blocks.
  555. * We need this to handle errors and free blocks
  556. * upon them.
  557. */
  558. ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
  559. if (!ablocks)
  560. return -ENOMEM;
  561. /* allocate all needed blocks */
  562. ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
  563. for (a = 0; a < depth - at; a++) {
  564. newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
  565. if (newblock == 0)
  566. goto cleanup;
  567. ablocks[a] = newblock;
  568. }
  569. /* initialize new leaf */
  570. newblock = ablocks[--a];
  571. BUG_ON(newblock == 0);
  572. bh = sb_getblk(inode->i_sb, newblock);
  573. if (!bh) {
  574. err = -EIO;
  575. goto cleanup;
  576. }
  577. lock_buffer(bh);
  578. err = ext4_journal_get_create_access(handle, bh);
  579. if (err)
  580. goto cleanup;
  581. neh = ext_block_hdr(bh);
  582. neh->eh_entries = 0;
  583. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
  584. neh->eh_magic = EXT4_EXT_MAGIC;
  585. neh->eh_depth = 0;
  586. ex = EXT_FIRST_EXTENT(neh);
  587. /* move remainder of path[depth] to the new leaf */
  588. BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
  589. /* start copy from next extent */
  590. /* TODO: we could do it by single memmove */
  591. m = 0;
  592. path[depth].p_ext++;
  593. while (path[depth].p_ext <=
  594. EXT_MAX_EXTENT(path[depth].p_hdr)) {
  595. ext_debug("move %d:%llu:%d in new leaf %llu\n",
  596. le32_to_cpu(path[depth].p_ext->ee_block),
  597. ext_pblock(path[depth].p_ext),
  598. le16_to_cpu(path[depth].p_ext->ee_len),
  599. newblock);
  600. /*memmove(ex++, path[depth].p_ext++,
  601. sizeof(struct ext4_extent));
  602. neh->eh_entries++;*/
  603. path[depth].p_ext++;
  604. m++;
  605. }
  606. if (m) {
  607. memmove(ex, path[depth].p_ext-m, sizeof(struct ext4_extent)*m);
  608. neh->eh_entries = cpu_to_le16(le16_to_cpu(neh->eh_entries)+m);
  609. }
  610. set_buffer_uptodate(bh);
  611. unlock_buffer(bh);
  612. err = ext4_journal_dirty_metadata(handle, bh);
  613. if (err)
  614. goto cleanup;
  615. brelse(bh);
  616. bh = NULL;
  617. /* correct old leaf */
  618. if (m) {
  619. err = ext4_ext_get_access(handle, inode, path + depth);
  620. if (err)
  621. goto cleanup;
  622. path[depth].p_hdr->eh_entries =
  623. cpu_to_le16(le16_to_cpu(path[depth].p_hdr->eh_entries)-m);
  624. err = ext4_ext_dirty(handle, inode, path + depth);
  625. if (err)
  626. goto cleanup;
  627. }
  628. /* create intermediate indexes */
  629. k = depth - at - 1;
  630. BUG_ON(k < 0);
  631. if (k)
  632. ext_debug("create %d intermediate indices\n", k);
  633. /* insert new index into current index block */
  634. /* current depth stored in i var */
  635. i = depth - 1;
  636. while (k--) {
  637. oldblock = newblock;
  638. newblock = ablocks[--a];
  639. bh = sb_getblk(inode->i_sb, (ext4_fsblk_t)newblock);
  640. if (!bh) {
  641. err = -EIO;
  642. goto cleanup;
  643. }
  644. lock_buffer(bh);
  645. err = ext4_journal_get_create_access(handle, bh);
  646. if (err)
  647. goto cleanup;
  648. neh = ext_block_hdr(bh);
  649. neh->eh_entries = cpu_to_le16(1);
  650. neh->eh_magic = EXT4_EXT_MAGIC;
  651. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
  652. neh->eh_depth = cpu_to_le16(depth - i);
  653. fidx = EXT_FIRST_INDEX(neh);
  654. fidx->ei_block = border;
  655. ext4_idx_store_pblock(fidx, oldblock);
  656. ext_debug("int.index at %d (block %llu): %lu -> %llu\n", i,
  657. newblock, (unsigned long) le32_to_cpu(border),
  658. oldblock);
  659. /* copy indexes */
  660. m = 0;
  661. path[i].p_idx++;
  662. ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
  663. EXT_MAX_INDEX(path[i].p_hdr));
  664. BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
  665. EXT_LAST_INDEX(path[i].p_hdr));
  666. while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
  667. ext_debug("%d: move %d:%d in new index %llu\n", i,
  668. le32_to_cpu(path[i].p_idx->ei_block),
  669. idx_pblock(path[i].p_idx),
  670. newblock);
  671. /*memmove(++fidx, path[i].p_idx++,
  672. sizeof(struct ext4_extent_idx));
  673. neh->eh_entries++;
  674. BUG_ON(neh->eh_entries > neh->eh_max);*/
  675. path[i].p_idx++;
  676. m++;
  677. }
  678. if (m) {
  679. memmove(++fidx, path[i].p_idx - m,
  680. sizeof(struct ext4_extent_idx) * m);
  681. neh->eh_entries =
  682. cpu_to_le16(le16_to_cpu(neh->eh_entries) + m);
  683. }
  684. set_buffer_uptodate(bh);
  685. unlock_buffer(bh);
  686. err = ext4_journal_dirty_metadata(handle, bh);
  687. if (err)
  688. goto cleanup;
  689. brelse(bh);
  690. bh = NULL;
  691. /* correct old index */
  692. if (m) {
  693. err = ext4_ext_get_access(handle, inode, path + i);
  694. if (err)
  695. goto cleanup;
  696. path[i].p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path[i].p_hdr->eh_entries)-m);
  697. err = ext4_ext_dirty(handle, inode, path + i);
  698. if (err)
  699. goto cleanup;
  700. }
  701. i--;
  702. }
  703. /* insert new index */
  704. err = ext4_ext_insert_index(handle, inode, path + at,
  705. le32_to_cpu(border), newblock);
  706. cleanup:
  707. if (bh) {
  708. if (buffer_locked(bh))
  709. unlock_buffer(bh);
  710. brelse(bh);
  711. }
  712. if (err) {
  713. /* free all allocated blocks in error case */
  714. for (i = 0; i < depth; i++) {
  715. if (!ablocks[i])
  716. continue;
  717. ext4_free_blocks(handle, inode, ablocks[i], 1);
  718. }
  719. }
  720. kfree(ablocks);
  721. return err;
  722. }
  723. /*
  724. * ext4_ext_grow_indepth:
  725. * implements tree growing procedure:
  726. * - allocates new block
  727. * - moves top-level data (index block or leaf) into the new block
  728. * - initializes new top-level, creating index that points to the
  729. * just created block
  730. */
  731. static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
  732. struct ext4_ext_path *path,
  733. struct ext4_extent *newext)
  734. {
  735. struct ext4_ext_path *curp = path;
  736. struct ext4_extent_header *neh;
  737. struct ext4_extent_idx *fidx;
  738. struct buffer_head *bh;
  739. ext4_fsblk_t newblock;
  740. int err = 0;
  741. newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
  742. if (newblock == 0)
  743. return err;
  744. bh = sb_getblk(inode->i_sb, newblock);
  745. if (!bh) {
  746. err = -EIO;
  747. ext4_std_error(inode->i_sb, err);
  748. return err;
  749. }
  750. lock_buffer(bh);
  751. err = ext4_journal_get_create_access(handle, bh);
  752. if (err) {
  753. unlock_buffer(bh);
  754. goto out;
  755. }
  756. /* move top-level index/leaf into new block */
  757. memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
  758. /* set size of new block */
  759. neh = ext_block_hdr(bh);
  760. /* old root could have indexes or leaves
  761. * so calculate e_max right way */
  762. if (ext_depth(inode))
  763. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
  764. else
  765. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
  766. neh->eh_magic = EXT4_EXT_MAGIC;
  767. set_buffer_uptodate(bh);
  768. unlock_buffer(bh);
  769. err = ext4_journal_dirty_metadata(handle, bh);
  770. if (err)
  771. goto out;
  772. /* create index in new top-level index: num,max,pointer */
  773. err = ext4_ext_get_access(handle, inode, curp);
  774. if (err)
  775. goto out;
  776. curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
  777. curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
  778. curp->p_hdr->eh_entries = cpu_to_le16(1);
  779. curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
  780. /* FIXME: it works, but actually path[0] can be index */
  781. curp->p_idx->ei_block = EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
  782. ext4_idx_store_pblock(curp->p_idx, newblock);
  783. neh = ext_inode_hdr(inode);
  784. fidx = EXT_FIRST_INDEX(neh);
  785. ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
  786. le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
  787. le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
  788. neh->eh_depth = cpu_to_le16(path->p_depth + 1);
  789. err = ext4_ext_dirty(handle, inode, curp);
  790. out:
  791. brelse(bh);
  792. return err;
  793. }
  794. /*
  795. * ext4_ext_create_new_leaf:
  796. * finds empty index and adds new leaf.
  797. * if no free index is found, then it requests in-depth growing.
  798. */
  799. static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
  800. struct ext4_ext_path *path,
  801. struct ext4_extent *newext)
  802. {
  803. struct ext4_ext_path *curp;
  804. int depth, i, err = 0;
  805. repeat:
  806. i = depth = ext_depth(inode);
  807. /* walk up to the tree and look for free index entry */
  808. curp = path + depth;
  809. while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
  810. i--;
  811. curp--;
  812. }
  813. /* we use already allocated block for index block,
  814. * so subsequent data blocks should be contiguous */
  815. if (EXT_HAS_FREE_INDEX(curp)) {
  816. /* if we found index with free entry, then use that
  817. * entry: create all needed subtree and add new leaf */
  818. err = ext4_ext_split(handle, inode, path, newext, i);
  819. /* refill path */
  820. ext4_ext_drop_refs(path);
  821. path = ext4_ext_find_extent(inode,
  822. le32_to_cpu(newext->ee_block),
  823. path);
  824. if (IS_ERR(path))
  825. err = PTR_ERR(path);
  826. } else {
  827. /* tree is full, time to grow in depth */
  828. err = ext4_ext_grow_indepth(handle, inode, path, newext);
  829. if (err)
  830. goto out;
  831. /* refill path */
  832. ext4_ext_drop_refs(path);
  833. path = ext4_ext_find_extent(inode,
  834. le32_to_cpu(newext->ee_block),
  835. path);
  836. if (IS_ERR(path)) {
  837. err = PTR_ERR(path);
  838. goto out;
  839. }
  840. /*
  841. * only first (depth 0 -> 1) produces free space;
  842. * in all other cases we have to split the grown tree
  843. */
  844. depth = ext_depth(inode);
  845. if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
  846. /* now we need to split */
  847. goto repeat;
  848. }
  849. }
  850. out:
  851. return err;
  852. }
  853. /*
  854. * ext4_ext_next_allocated_block:
  855. * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
  856. * NOTE: it considers block number from index entry as
  857. * allocated block. Thus, index entries have to be consistent
  858. * with leaves.
  859. */
  860. static unsigned long
  861. ext4_ext_next_allocated_block(struct ext4_ext_path *path)
  862. {
  863. int depth;
  864. BUG_ON(path == NULL);
  865. depth = path->p_depth;
  866. if (depth == 0 && path->p_ext == NULL)
  867. return EXT_MAX_BLOCK;
  868. while (depth >= 0) {
  869. if (depth == path->p_depth) {
  870. /* leaf */
  871. if (path[depth].p_ext !=
  872. EXT_LAST_EXTENT(path[depth].p_hdr))
  873. return le32_to_cpu(path[depth].p_ext[1].ee_block);
  874. } else {
  875. /* index */
  876. if (path[depth].p_idx !=
  877. EXT_LAST_INDEX(path[depth].p_hdr))
  878. return le32_to_cpu(path[depth].p_idx[1].ei_block);
  879. }
  880. depth--;
  881. }
  882. return EXT_MAX_BLOCK;
  883. }
  884. /*
  885. * ext4_ext_next_leaf_block:
  886. * returns first allocated block from next leaf or EXT_MAX_BLOCK
  887. */
  888. static unsigned ext4_ext_next_leaf_block(struct inode *inode,
  889. struct ext4_ext_path *path)
  890. {
  891. int depth;
  892. BUG_ON(path == NULL);
  893. depth = path->p_depth;
  894. /* zero-tree has no leaf blocks at all */
  895. if (depth == 0)
  896. return EXT_MAX_BLOCK;
  897. /* go to index block */
  898. depth--;
  899. while (depth >= 0) {
  900. if (path[depth].p_idx !=
  901. EXT_LAST_INDEX(path[depth].p_hdr))
  902. return le32_to_cpu(path[depth].p_idx[1].ei_block);
  903. depth--;
  904. }
  905. return EXT_MAX_BLOCK;
  906. }
  907. /*
  908. * ext4_ext_correct_indexes:
  909. * if leaf gets modified and modified extent is first in the leaf,
  910. * then we have to correct all indexes above.
  911. * TODO: do we need to correct tree in all cases?
  912. */
  913. int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
  914. struct ext4_ext_path *path)
  915. {
  916. struct ext4_extent_header *eh;
  917. int depth = ext_depth(inode);
  918. struct ext4_extent *ex;
  919. __le32 border;
  920. int k, err = 0;
  921. eh = path[depth].p_hdr;
  922. ex = path[depth].p_ext;
  923. BUG_ON(ex == NULL);
  924. BUG_ON(eh == NULL);
  925. if (depth == 0) {
  926. /* there is no tree at all */
  927. return 0;
  928. }
  929. if (ex != EXT_FIRST_EXTENT(eh)) {
  930. /* we correct tree if first leaf got modified only */
  931. return 0;
  932. }
  933. /*
  934. * TODO: we need correction if border is smaller than current one
  935. */
  936. k = depth - 1;
  937. border = path[depth].p_ext->ee_block;
  938. err = ext4_ext_get_access(handle, inode, path + k);
  939. if (err)
  940. return err;
  941. path[k].p_idx->ei_block = border;
  942. err = ext4_ext_dirty(handle, inode, path + k);
  943. if (err)
  944. return err;
  945. while (k--) {
  946. /* change all left-side indexes */
  947. if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
  948. break;
  949. err = ext4_ext_get_access(handle, inode, path + k);
  950. if (err)
  951. break;
  952. path[k].p_idx->ei_block = border;
  953. err = ext4_ext_dirty(handle, inode, path + k);
  954. if (err)
  955. break;
  956. }
  957. return err;
  958. }
  959. static int
  960. ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
  961. struct ext4_extent *ex2)
  962. {
  963. if (le32_to_cpu(ex1->ee_block) + le16_to_cpu(ex1->ee_len) !=
  964. le32_to_cpu(ex2->ee_block))
  965. return 0;
  966. /*
  967. * To allow future support for preallocated extents to be added
  968. * as an RO_COMPAT feature, refuse to merge to extents if
  969. * this can result in the top bit of ee_len being set.
  970. */
  971. if (le16_to_cpu(ex1->ee_len) + le16_to_cpu(ex2->ee_len) > EXT_MAX_LEN)
  972. return 0;
  973. #ifdef AGGRESSIVE_TEST
  974. if (le16_to_cpu(ex1->ee_len) >= 4)
  975. return 0;
  976. #endif
  977. if (ext_pblock(ex1) + le16_to_cpu(ex1->ee_len) == ext_pblock(ex2))
  978. return 1;
  979. return 0;
  980. }
  981. /*
  982. * ext4_ext_insert_extent:
  983. * tries to merge requsted extent into the existing extent or
  984. * inserts requested extent as new one into the tree,
  985. * creating new leaf in the no-space case.
  986. */
  987. int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
  988. struct ext4_ext_path *path,
  989. struct ext4_extent *newext)
  990. {
  991. struct ext4_extent_header * eh;
  992. struct ext4_extent *ex, *fex;
  993. struct ext4_extent *nearex; /* nearest extent */
  994. struct ext4_ext_path *npath = NULL;
  995. int depth, len, err, next;
  996. BUG_ON(newext->ee_len == 0);
  997. depth = ext_depth(inode);
  998. ex = path[depth].p_ext;
  999. BUG_ON(path[depth].p_hdr == NULL);
  1000. /* try to insert block into found extent and return */
  1001. if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
  1002. ext_debug("append %d block to %d:%d (from %llu)\n",
  1003. le16_to_cpu(newext->ee_len),
  1004. le32_to_cpu(ex->ee_block),
  1005. le16_to_cpu(ex->ee_len), ext_pblock(ex));
  1006. err = ext4_ext_get_access(handle, inode, path + depth);
  1007. if (err)
  1008. return err;
  1009. ex->ee_len = cpu_to_le16(le16_to_cpu(ex->ee_len)
  1010. + le16_to_cpu(newext->ee_len));
  1011. eh = path[depth].p_hdr;
  1012. nearex = ex;
  1013. goto merge;
  1014. }
  1015. repeat:
  1016. depth = ext_depth(inode);
  1017. eh = path[depth].p_hdr;
  1018. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
  1019. goto has_space;
  1020. /* probably next leaf has space for us? */
  1021. fex = EXT_LAST_EXTENT(eh);
  1022. next = ext4_ext_next_leaf_block(inode, path);
  1023. if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
  1024. && next != EXT_MAX_BLOCK) {
  1025. ext_debug("next leaf block - %d\n", next);
  1026. BUG_ON(npath != NULL);
  1027. npath = ext4_ext_find_extent(inode, next, NULL);
  1028. if (IS_ERR(npath))
  1029. return PTR_ERR(npath);
  1030. BUG_ON(npath->p_depth != path->p_depth);
  1031. eh = npath[depth].p_hdr;
  1032. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
  1033. ext_debug("next leaf isnt full(%d)\n",
  1034. le16_to_cpu(eh->eh_entries));
  1035. path = npath;
  1036. goto repeat;
  1037. }
  1038. ext_debug("next leaf has no free space(%d,%d)\n",
  1039. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  1040. }
  1041. /*
  1042. * There is no free space in the found leaf.
  1043. * We're gonna add a new leaf in the tree.
  1044. */
  1045. err = ext4_ext_create_new_leaf(handle, inode, path, newext);
  1046. if (err)
  1047. goto cleanup;
  1048. depth = ext_depth(inode);
  1049. eh = path[depth].p_hdr;
  1050. has_space:
  1051. nearex = path[depth].p_ext;
  1052. err = ext4_ext_get_access(handle, inode, path + depth);
  1053. if (err)
  1054. goto cleanup;
  1055. if (!nearex) {
  1056. /* there is no extent in this leaf, create first one */
  1057. ext_debug("first extent in the leaf: %d:%llu:%d\n",
  1058. le32_to_cpu(newext->ee_block),
  1059. ext_pblock(newext),
  1060. le16_to_cpu(newext->ee_len));
  1061. path[depth].p_ext = EXT_FIRST_EXTENT(eh);
  1062. } else if (le32_to_cpu(newext->ee_block)
  1063. > le32_to_cpu(nearex->ee_block)) {
  1064. /* BUG_ON(newext->ee_block == nearex->ee_block); */
  1065. if (nearex != EXT_LAST_EXTENT(eh)) {
  1066. len = EXT_MAX_EXTENT(eh) - nearex;
  1067. len = (len - 1) * sizeof(struct ext4_extent);
  1068. len = len < 0 ? 0 : len;
  1069. ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
  1070. "move %d from 0x%p to 0x%p\n",
  1071. le32_to_cpu(newext->ee_block),
  1072. ext_pblock(newext),
  1073. le16_to_cpu(newext->ee_len),
  1074. nearex, len, nearex + 1, nearex + 2);
  1075. memmove(nearex + 2, nearex + 1, len);
  1076. }
  1077. path[depth].p_ext = nearex + 1;
  1078. } else {
  1079. BUG_ON(newext->ee_block == nearex->ee_block);
  1080. len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
  1081. len = len < 0 ? 0 : len;
  1082. ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
  1083. "move %d from 0x%p to 0x%p\n",
  1084. le32_to_cpu(newext->ee_block),
  1085. ext_pblock(newext),
  1086. le16_to_cpu(newext->ee_len),
  1087. nearex, len, nearex + 1, nearex + 2);
  1088. memmove(nearex + 1, nearex, len);
  1089. path[depth].p_ext = nearex;
  1090. }
  1091. eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)+1);
  1092. nearex = path[depth].p_ext;
  1093. nearex->ee_block = newext->ee_block;
  1094. nearex->ee_start = newext->ee_start;
  1095. nearex->ee_start_hi = newext->ee_start_hi;
  1096. nearex->ee_len = newext->ee_len;
  1097. merge:
  1098. /* try to merge extents to the right */
  1099. while (nearex < EXT_LAST_EXTENT(eh)) {
  1100. if (!ext4_can_extents_be_merged(inode, nearex, nearex + 1))
  1101. break;
  1102. /* merge with next extent! */
  1103. nearex->ee_len = cpu_to_le16(le16_to_cpu(nearex->ee_len)
  1104. + le16_to_cpu(nearex[1].ee_len));
  1105. if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
  1106. len = (EXT_LAST_EXTENT(eh) - nearex - 1)
  1107. * sizeof(struct ext4_extent);
  1108. memmove(nearex + 1, nearex + 2, len);
  1109. }
  1110. eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
  1111. BUG_ON(eh->eh_entries == 0);
  1112. }
  1113. /* try to merge extents to the left */
  1114. /* time to correct all indexes above */
  1115. err = ext4_ext_correct_indexes(handle, inode, path);
  1116. if (err)
  1117. goto cleanup;
  1118. err = ext4_ext_dirty(handle, inode, path + depth);
  1119. cleanup:
  1120. if (npath) {
  1121. ext4_ext_drop_refs(npath);
  1122. kfree(npath);
  1123. }
  1124. ext4_ext_tree_changed(inode);
  1125. ext4_ext_invalidate_cache(inode);
  1126. return err;
  1127. }
  1128. int ext4_ext_walk_space(struct inode *inode, unsigned long block,
  1129. unsigned long num, ext_prepare_callback func,
  1130. void *cbdata)
  1131. {
  1132. struct ext4_ext_path *path = NULL;
  1133. struct ext4_ext_cache cbex;
  1134. struct ext4_extent *ex;
  1135. unsigned long next, start = 0, end = 0;
  1136. unsigned long last = block + num;
  1137. int depth, exists, err = 0;
  1138. BUG_ON(func == NULL);
  1139. BUG_ON(inode == NULL);
  1140. while (block < last && block != EXT_MAX_BLOCK) {
  1141. num = last - block;
  1142. /* find extent for this block */
  1143. path = ext4_ext_find_extent(inode, block, path);
  1144. if (IS_ERR(path)) {
  1145. err = PTR_ERR(path);
  1146. path = NULL;
  1147. break;
  1148. }
  1149. depth = ext_depth(inode);
  1150. BUG_ON(path[depth].p_hdr == NULL);
  1151. ex = path[depth].p_ext;
  1152. next = ext4_ext_next_allocated_block(path);
  1153. exists = 0;
  1154. if (!ex) {
  1155. /* there is no extent yet, so try to allocate
  1156. * all requested space */
  1157. start = block;
  1158. end = block + num;
  1159. } else if (le32_to_cpu(ex->ee_block) > block) {
  1160. /* need to allocate space before found extent */
  1161. start = block;
  1162. end = le32_to_cpu(ex->ee_block);
  1163. if (block + num < end)
  1164. end = block + num;
  1165. } else if (block >=
  1166. le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len)) {
  1167. /* need to allocate space after found extent */
  1168. start = block;
  1169. end = block + num;
  1170. if (end >= next)
  1171. end = next;
  1172. } else if (block >= le32_to_cpu(ex->ee_block)) {
  1173. /*
  1174. * some part of requested space is covered
  1175. * by found extent
  1176. */
  1177. start = block;
  1178. end = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len);
  1179. if (block + num < end)
  1180. end = block + num;
  1181. exists = 1;
  1182. } else {
  1183. BUG();
  1184. }
  1185. BUG_ON(end <= start);
  1186. if (!exists) {
  1187. cbex.ec_block = start;
  1188. cbex.ec_len = end - start;
  1189. cbex.ec_start = 0;
  1190. cbex.ec_type = EXT4_EXT_CACHE_GAP;
  1191. } else {
  1192. cbex.ec_block = le32_to_cpu(ex->ee_block);
  1193. cbex.ec_len = le16_to_cpu(ex->ee_len);
  1194. cbex.ec_start = ext_pblock(ex);
  1195. cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
  1196. }
  1197. BUG_ON(cbex.ec_len == 0);
  1198. err = func(inode, path, &cbex, cbdata);
  1199. ext4_ext_drop_refs(path);
  1200. if (err < 0)
  1201. break;
  1202. if (err == EXT_REPEAT)
  1203. continue;
  1204. else if (err == EXT_BREAK) {
  1205. err = 0;
  1206. break;
  1207. }
  1208. if (ext_depth(inode) != depth) {
  1209. /* depth was changed. we have to realloc path */
  1210. kfree(path);
  1211. path = NULL;
  1212. }
  1213. block = cbex.ec_block + cbex.ec_len;
  1214. }
  1215. if (path) {
  1216. ext4_ext_drop_refs(path);
  1217. kfree(path);
  1218. }
  1219. return err;
  1220. }
  1221. static void
  1222. ext4_ext_put_in_cache(struct inode *inode, __u32 block,
  1223. __u32 len, __u32 start, int type)
  1224. {
  1225. struct ext4_ext_cache *cex;
  1226. BUG_ON(len == 0);
  1227. cex = &EXT4_I(inode)->i_cached_extent;
  1228. cex->ec_type = type;
  1229. cex->ec_block = block;
  1230. cex->ec_len = len;
  1231. cex->ec_start = start;
  1232. }
  1233. /*
  1234. * ext4_ext_put_gap_in_cache:
  1235. * calculate boundaries of the gap that the requested block fits into
  1236. * and cache this gap
  1237. */
  1238. static void
  1239. ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
  1240. unsigned long block)
  1241. {
  1242. int depth = ext_depth(inode);
  1243. unsigned long lblock, len;
  1244. struct ext4_extent *ex;
  1245. ex = path[depth].p_ext;
  1246. if (ex == NULL) {
  1247. /* there is no extent yet, so gap is [0;-] */
  1248. lblock = 0;
  1249. len = EXT_MAX_BLOCK;
  1250. ext_debug("cache gap(whole file):");
  1251. } else if (block < le32_to_cpu(ex->ee_block)) {
  1252. lblock = block;
  1253. len = le32_to_cpu(ex->ee_block) - block;
  1254. ext_debug("cache gap(before): %lu [%lu:%lu]",
  1255. (unsigned long) block,
  1256. (unsigned long) le32_to_cpu(ex->ee_block),
  1257. (unsigned long) le16_to_cpu(ex->ee_len));
  1258. } else if (block >= le32_to_cpu(ex->ee_block)
  1259. + le16_to_cpu(ex->ee_len)) {
  1260. lblock = le32_to_cpu(ex->ee_block)
  1261. + le16_to_cpu(ex->ee_len);
  1262. len = ext4_ext_next_allocated_block(path);
  1263. ext_debug("cache gap(after): [%lu:%lu] %lu",
  1264. (unsigned long) le32_to_cpu(ex->ee_block),
  1265. (unsigned long) le16_to_cpu(ex->ee_len),
  1266. (unsigned long) block);
  1267. BUG_ON(len == lblock);
  1268. len = len - lblock;
  1269. } else {
  1270. lblock = len = 0;
  1271. BUG();
  1272. }
  1273. ext_debug(" -> %lu:%lu\n", (unsigned long) lblock, len);
  1274. ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
  1275. }
  1276. static int
  1277. ext4_ext_in_cache(struct inode *inode, unsigned long block,
  1278. struct ext4_extent *ex)
  1279. {
  1280. struct ext4_ext_cache *cex;
  1281. cex = &EXT4_I(inode)->i_cached_extent;
  1282. /* has cache valid data? */
  1283. if (cex->ec_type == EXT4_EXT_CACHE_NO)
  1284. return EXT4_EXT_CACHE_NO;
  1285. BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
  1286. cex->ec_type != EXT4_EXT_CACHE_EXTENT);
  1287. if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
  1288. ex->ee_block = cpu_to_le32(cex->ec_block);
  1289. ext4_ext_store_pblock(ex, cex->ec_start);
  1290. ex->ee_len = cpu_to_le16(cex->ec_len);
  1291. ext_debug("%lu cached by %lu:%lu:%llu\n",
  1292. (unsigned long) block,
  1293. (unsigned long) cex->ec_block,
  1294. (unsigned long) cex->ec_len,
  1295. cex->ec_start);
  1296. return cex->ec_type;
  1297. }
  1298. /* not in cache */
  1299. return EXT4_EXT_CACHE_NO;
  1300. }
  1301. /*
  1302. * ext4_ext_rm_idx:
  1303. * removes index from the index block.
  1304. * It's used in truncate case only, thus all requests are for
  1305. * last index in the block only.
  1306. */
  1307. int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
  1308. struct ext4_ext_path *path)
  1309. {
  1310. struct buffer_head *bh;
  1311. int err;
  1312. ext4_fsblk_t leaf;
  1313. /* free index block */
  1314. path--;
  1315. leaf = idx_pblock(path->p_idx);
  1316. BUG_ON(path->p_hdr->eh_entries == 0);
  1317. err = ext4_ext_get_access(handle, inode, path);
  1318. if (err)
  1319. return err;
  1320. path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1);
  1321. err = ext4_ext_dirty(handle, inode, path);
  1322. if (err)
  1323. return err;
  1324. ext_debug("index is empty, remove it, free block %llu\n", leaf);
  1325. bh = sb_find_get_block(inode->i_sb, leaf);
  1326. ext4_forget(handle, 1, inode, bh, leaf);
  1327. ext4_free_blocks(handle, inode, leaf, 1);
  1328. return err;
  1329. }
  1330. /*
  1331. * ext4_ext_calc_credits_for_insert:
  1332. * This routine returns max. credits that the extent tree can consume.
  1333. * It should be OK for low-performance paths like ->writepage()
  1334. * To allow many writing processes to fit into a single transaction,
  1335. * the caller should calculate credits under truncate_mutex and
  1336. * pass the actual path.
  1337. */
  1338. int ext4_ext_calc_credits_for_insert(struct inode *inode,
  1339. struct ext4_ext_path *path)
  1340. {
  1341. int depth, needed;
  1342. if (path) {
  1343. /* probably there is space in leaf? */
  1344. depth = ext_depth(inode);
  1345. if (le16_to_cpu(path[depth].p_hdr->eh_entries)
  1346. < le16_to_cpu(path[depth].p_hdr->eh_max))
  1347. return 1;
  1348. }
  1349. /*
  1350. * given 32-bit logical block (4294967296 blocks), max. tree
  1351. * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
  1352. * Let's also add one more level for imbalance.
  1353. */
  1354. depth = 5;
  1355. /* allocation of new data block(s) */
  1356. needed = 2;
  1357. /*
  1358. * tree can be full, so it would need to grow in depth:
  1359. * we need one credit to modify old root, credits for
  1360. * new root will be added in split accounting
  1361. */
  1362. needed += 1;
  1363. /*
  1364. * Index split can happen, we would need:
  1365. * allocate intermediate indexes (bitmap + group)
  1366. * + change two blocks at each level, but root (already included)
  1367. */
  1368. needed += (depth * 2) + (depth * 2);
  1369. /* any allocation modifies superblock */
  1370. needed += 1;
  1371. return needed;
  1372. }
  1373. static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
  1374. struct ext4_extent *ex,
  1375. unsigned long from, unsigned long to)
  1376. {
  1377. struct buffer_head *bh;
  1378. int i;
  1379. #ifdef EXTENTS_STATS
  1380. {
  1381. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1382. unsigned short ee_len = le16_to_cpu(ex->ee_len);
  1383. spin_lock(&sbi->s_ext_stats_lock);
  1384. sbi->s_ext_blocks += ee_len;
  1385. sbi->s_ext_extents++;
  1386. if (ee_len < sbi->s_ext_min)
  1387. sbi->s_ext_min = ee_len;
  1388. if (ee_len > sbi->s_ext_max)
  1389. sbi->s_ext_max = ee_len;
  1390. if (ext_depth(inode) > sbi->s_depth_max)
  1391. sbi->s_depth_max = ext_depth(inode);
  1392. spin_unlock(&sbi->s_ext_stats_lock);
  1393. }
  1394. #endif
  1395. if (from >= le32_to_cpu(ex->ee_block)
  1396. && to == le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1) {
  1397. /* tail removal */
  1398. unsigned long num;
  1399. ext4_fsblk_t start;
  1400. num = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - from;
  1401. start = ext_pblock(ex) + le16_to_cpu(ex->ee_len) - num;
  1402. ext_debug("free last %lu blocks starting %llu\n", num, start);
  1403. for (i = 0; i < num; i++) {
  1404. bh = sb_find_get_block(inode->i_sb, start + i);
  1405. ext4_forget(handle, 0, inode, bh, start + i);
  1406. }
  1407. ext4_free_blocks(handle, inode, start, num);
  1408. } else if (from == le32_to_cpu(ex->ee_block)
  1409. && to <= le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1) {
  1410. printk("strange request: removal %lu-%lu from %u:%u\n",
  1411. from, to, le32_to_cpu(ex->ee_block), le16_to_cpu(ex->ee_len));
  1412. } else {
  1413. printk("strange request: removal(2) %lu-%lu from %u:%u\n",
  1414. from, to, le32_to_cpu(ex->ee_block), le16_to_cpu(ex->ee_len));
  1415. }
  1416. return 0;
  1417. }
  1418. static int
  1419. ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
  1420. struct ext4_ext_path *path, unsigned long start)
  1421. {
  1422. int err = 0, correct_index = 0;
  1423. int depth = ext_depth(inode), credits;
  1424. struct ext4_extent_header *eh;
  1425. unsigned a, b, block, num;
  1426. unsigned long ex_ee_block;
  1427. unsigned short ex_ee_len;
  1428. struct ext4_extent *ex;
  1429. ext_debug("truncate since %lu in leaf\n", start);
  1430. if (!path[depth].p_hdr)
  1431. path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
  1432. eh = path[depth].p_hdr;
  1433. BUG_ON(eh == NULL);
  1434. BUG_ON(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max));
  1435. BUG_ON(eh->eh_magic != EXT4_EXT_MAGIC);
  1436. /* find where to start removing */
  1437. ex = EXT_LAST_EXTENT(eh);
  1438. ex_ee_block = le32_to_cpu(ex->ee_block);
  1439. ex_ee_len = le16_to_cpu(ex->ee_len);
  1440. while (ex >= EXT_FIRST_EXTENT(eh) &&
  1441. ex_ee_block + ex_ee_len > start) {
  1442. ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
  1443. path[depth].p_ext = ex;
  1444. a = ex_ee_block > start ? ex_ee_block : start;
  1445. b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
  1446. ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
  1447. ext_debug(" border %u:%u\n", a, b);
  1448. if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
  1449. block = 0;
  1450. num = 0;
  1451. BUG();
  1452. } else if (a != ex_ee_block) {
  1453. /* remove tail of the extent */
  1454. block = ex_ee_block;
  1455. num = a - block;
  1456. } else if (b != ex_ee_block + ex_ee_len - 1) {
  1457. /* remove head of the extent */
  1458. block = a;
  1459. num = b - a;
  1460. /* there is no "make a hole" API yet */
  1461. BUG();
  1462. } else {
  1463. /* remove whole extent: excellent! */
  1464. block = ex_ee_block;
  1465. num = 0;
  1466. BUG_ON(a != ex_ee_block);
  1467. BUG_ON(b != ex_ee_block + ex_ee_len - 1);
  1468. }
  1469. /* at present, extent can't cross block group: */
  1470. /* leaf + bitmap + group desc + sb + inode */
  1471. credits = 5;
  1472. if (ex == EXT_FIRST_EXTENT(eh)) {
  1473. correct_index = 1;
  1474. credits += (ext_depth(inode)) + 1;
  1475. }
  1476. #ifdef CONFIG_QUOTA
  1477. credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
  1478. #endif
  1479. handle = ext4_ext_journal_restart(handle, credits);
  1480. if (IS_ERR(handle)) {
  1481. err = PTR_ERR(handle);
  1482. goto out;
  1483. }
  1484. err = ext4_ext_get_access(handle, inode, path + depth);
  1485. if (err)
  1486. goto out;
  1487. err = ext4_remove_blocks(handle, inode, ex, a, b);
  1488. if (err)
  1489. goto out;
  1490. if (num == 0) {
  1491. /* this extent is removed; mark slot entirely unused */
  1492. ext4_ext_store_pblock(ex, 0);
  1493. eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
  1494. }
  1495. ex->ee_block = cpu_to_le32(block);
  1496. ex->ee_len = cpu_to_le16(num);
  1497. err = ext4_ext_dirty(handle, inode, path + depth);
  1498. if (err)
  1499. goto out;
  1500. ext_debug("new extent: %u:%u:%llu\n", block, num,
  1501. ext_pblock(ex));
  1502. ex--;
  1503. ex_ee_block = le32_to_cpu(ex->ee_block);
  1504. ex_ee_len = le16_to_cpu(ex->ee_len);
  1505. }
  1506. if (correct_index && eh->eh_entries)
  1507. err = ext4_ext_correct_indexes(handle, inode, path);
  1508. /* if this leaf is free, then we should
  1509. * remove it from index block above */
  1510. if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
  1511. err = ext4_ext_rm_idx(handle, inode, path + depth);
  1512. out:
  1513. return err;
  1514. }
  1515. /*
  1516. * ext4_ext_more_to_rm:
  1517. * returns 1 if current index has to be freed (even partial)
  1518. */
  1519. static int
  1520. ext4_ext_more_to_rm(struct ext4_ext_path *path)
  1521. {
  1522. BUG_ON(path->p_idx == NULL);
  1523. if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
  1524. return 0;
  1525. /*
  1526. * if truncate on deeper level happened, it wasn't partial,
  1527. * so we have to consider current index for truncation
  1528. */
  1529. if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
  1530. return 0;
  1531. return 1;
  1532. }
  1533. int ext4_ext_remove_space(struct inode *inode, unsigned long start)
  1534. {
  1535. struct super_block *sb = inode->i_sb;
  1536. int depth = ext_depth(inode);
  1537. struct ext4_ext_path *path;
  1538. handle_t *handle;
  1539. int i = 0, err = 0;
  1540. ext_debug("truncate since %lu\n", start);
  1541. /* probably first extent we're gonna free will be last in block */
  1542. handle = ext4_journal_start(inode, depth + 1);
  1543. if (IS_ERR(handle))
  1544. return PTR_ERR(handle);
  1545. ext4_ext_invalidate_cache(inode);
  1546. /*
  1547. * We start scanning from right side, freeing all the blocks
  1548. * after i_size and walking into the tree depth-wise.
  1549. */
  1550. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_KERNEL);
  1551. if (path == NULL) {
  1552. ext4_journal_stop(handle);
  1553. return -ENOMEM;
  1554. }
  1555. path[0].p_hdr = ext_inode_hdr(inode);
  1556. if (ext4_ext_check_header(__FUNCTION__, inode, path[0].p_hdr)) {
  1557. err = -EIO;
  1558. goto out;
  1559. }
  1560. path[0].p_depth = depth;
  1561. while (i >= 0 && err == 0) {
  1562. if (i == depth) {
  1563. /* this is leaf block */
  1564. err = ext4_ext_rm_leaf(handle, inode, path, start);
  1565. /* root level has p_bh == NULL, brelse() eats this */
  1566. brelse(path[i].p_bh);
  1567. path[i].p_bh = NULL;
  1568. i--;
  1569. continue;
  1570. }
  1571. /* this is index block */
  1572. if (!path[i].p_hdr) {
  1573. ext_debug("initialize header\n");
  1574. path[i].p_hdr = ext_block_hdr(path[i].p_bh);
  1575. if (ext4_ext_check_header(__FUNCTION__, inode,
  1576. path[i].p_hdr)) {
  1577. err = -EIO;
  1578. goto out;
  1579. }
  1580. }
  1581. BUG_ON(le16_to_cpu(path[i].p_hdr->eh_entries)
  1582. > le16_to_cpu(path[i].p_hdr->eh_max));
  1583. BUG_ON(path[i].p_hdr->eh_magic != EXT4_EXT_MAGIC);
  1584. if (!path[i].p_idx) {
  1585. /* this level hasn't been touched yet */
  1586. path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
  1587. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
  1588. ext_debug("init index ptr: hdr 0x%p, num %d\n",
  1589. path[i].p_hdr,
  1590. le16_to_cpu(path[i].p_hdr->eh_entries));
  1591. } else {
  1592. /* we were already here, see at next index */
  1593. path[i].p_idx--;
  1594. }
  1595. ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
  1596. i, EXT_FIRST_INDEX(path[i].p_hdr),
  1597. path[i].p_idx);
  1598. if (ext4_ext_more_to_rm(path + i)) {
  1599. /* go to the next level */
  1600. ext_debug("move to level %d (block %llu)\n",
  1601. i + 1, idx_pblock(path[i].p_idx));
  1602. memset(path + i + 1, 0, sizeof(*path));
  1603. path[i+1].p_bh =
  1604. sb_bread(sb, idx_pblock(path[i].p_idx));
  1605. if (!path[i+1].p_bh) {
  1606. /* should we reset i_size? */
  1607. err = -EIO;
  1608. break;
  1609. }
  1610. /* save actual number of indexes since this
  1611. * number is changed at the next iteration */
  1612. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
  1613. i++;
  1614. } else {
  1615. /* we finished processing this index, go up */
  1616. if (path[i].p_hdr->eh_entries == 0 && i > 0) {
  1617. /* index is empty, remove it;
  1618. * handle must be already prepared by the
  1619. * truncatei_leaf() */
  1620. err = ext4_ext_rm_idx(handle, inode, path + i);
  1621. }
  1622. /* root level has p_bh == NULL, brelse() eats this */
  1623. brelse(path[i].p_bh);
  1624. path[i].p_bh = NULL;
  1625. i--;
  1626. ext_debug("return to level %d\n", i);
  1627. }
  1628. }
  1629. /* TODO: flexible tree reduction should be here */
  1630. if (path->p_hdr->eh_entries == 0) {
  1631. /*
  1632. * truncate to zero freed all the tree,
  1633. * so we need to correct eh_depth
  1634. */
  1635. err = ext4_ext_get_access(handle, inode, path);
  1636. if (err == 0) {
  1637. ext_inode_hdr(inode)->eh_depth = 0;
  1638. ext_inode_hdr(inode)->eh_max =
  1639. cpu_to_le16(ext4_ext_space_root(inode));
  1640. err = ext4_ext_dirty(handle, inode, path);
  1641. }
  1642. }
  1643. out:
  1644. ext4_ext_tree_changed(inode);
  1645. ext4_ext_drop_refs(path);
  1646. kfree(path);
  1647. ext4_journal_stop(handle);
  1648. return err;
  1649. }
  1650. /*
  1651. * called at mount time
  1652. */
  1653. void ext4_ext_init(struct super_block *sb)
  1654. {
  1655. /*
  1656. * possible initialization would be here
  1657. */
  1658. if (test_opt(sb, EXTENTS)) {
  1659. printk("EXT4-fs: file extents enabled");
  1660. #ifdef AGGRESSIVE_TEST
  1661. printk(", aggressive tests");
  1662. #endif
  1663. #ifdef CHECK_BINSEARCH
  1664. printk(", check binsearch");
  1665. #endif
  1666. #ifdef EXTENTS_STATS
  1667. printk(", stats");
  1668. #endif
  1669. printk("\n");
  1670. #ifdef EXTENTS_STATS
  1671. spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
  1672. EXT4_SB(sb)->s_ext_min = 1 << 30;
  1673. EXT4_SB(sb)->s_ext_max = 0;
  1674. #endif
  1675. }
  1676. }
  1677. /*
  1678. * called at umount time
  1679. */
  1680. void ext4_ext_release(struct super_block *sb)
  1681. {
  1682. if (!test_opt(sb, EXTENTS))
  1683. return;
  1684. #ifdef EXTENTS_STATS
  1685. if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
  1686. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1687. printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
  1688. sbi->s_ext_blocks, sbi->s_ext_extents,
  1689. sbi->s_ext_blocks / sbi->s_ext_extents);
  1690. printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
  1691. sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
  1692. }
  1693. #endif
  1694. }
  1695. int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
  1696. ext4_fsblk_t iblock,
  1697. unsigned long max_blocks, struct buffer_head *bh_result,
  1698. int create, int extend_disksize)
  1699. {
  1700. struct ext4_ext_path *path = NULL;
  1701. struct ext4_extent newex, *ex;
  1702. ext4_fsblk_t goal, newblock;
  1703. int err = 0, depth;
  1704. unsigned long allocated = 0;
  1705. __clear_bit(BH_New, &bh_result->b_state);
  1706. ext_debug("blocks %d/%lu requested for inode %u\n", (int) iblock,
  1707. max_blocks, (unsigned) inode->i_ino);
  1708. mutex_lock(&EXT4_I(inode)->truncate_mutex);
  1709. /* check in cache */
  1710. goal = ext4_ext_in_cache(inode, iblock, &newex);
  1711. if (goal) {
  1712. if (goal == EXT4_EXT_CACHE_GAP) {
  1713. if (!create) {
  1714. /* block isn't allocated yet and
  1715. * user doesn't want to allocate it */
  1716. goto out2;
  1717. }
  1718. /* we should allocate requested block */
  1719. } else if (goal == EXT4_EXT_CACHE_EXTENT) {
  1720. /* block is already allocated */
  1721. newblock = iblock
  1722. - le32_to_cpu(newex.ee_block)
  1723. + ext_pblock(&newex);
  1724. /* number of remaining blocks in the extent */
  1725. allocated = le16_to_cpu(newex.ee_len) -
  1726. (iblock - le32_to_cpu(newex.ee_block));
  1727. goto out;
  1728. } else {
  1729. BUG();
  1730. }
  1731. }
  1732. /* find extent for this block */
  1733. path = ext4_ext_find_extent(inode, iblock, NULL);
  1734. if (IS_ERR(path)) {
  1735. err = PTR_ERR(path);
  1736. path = NULL;
  1737. goto out2;
  1738. }
  1739. depth = ext_depth(inode);
  1740. /*
  1741. * consistent leaf must not be empty;
  1742. * this situation is possible, though, _during_ tree modification;
  1743. * this is why assert can't be put in ext4_ext_find_extent()
  1744. */
  1745. BUG_ON(path[depth].p_ext == NULL && depth != 0);
  1746. ex = path[depth].p_ext;
  1747. if (ex) {
  1748. unsigned long ee_block = le32_to_cpu(ex->ee_block);
  1749. ext4_fsblk_t ee_start = ext_pblock(ex);
  1750. unsigned short ee_len = le16_to_cpu(ex->ee_len);
  1751. /*
  1752. * Allow future support for preallocated extents to be added
  1753. * as an RO_COMPAT feature:
  1754. * Uninitialized extents are treated as holes, except that
  1755. * we avoid (fail) allocating new blocks during a write.
  1756. */
  1757. if (ee_len > EXT_MAX_LEN)
  1758. goto out2;
  1759. /* if found extent covers block, simply return it */
  1760. if (iblock >= ee_block && iblock < ee_block + ee_len) {
  1761. newblock = iblock - ee_block + ee_start;
  1762. /* number of remaining blocks in the extent */
  1763. allocated = ee_len - (iblock - ee_block);
  1764. ext_debug("%d fit into %lu:%d -> %llu\n", (int) iblock,
  1765. ee_block, ee_len, newblock);
  1766. ext4_ext_put_in_cache(inode, ee_block, ee_len,
  1767. ee_start, EXT4_EXT_CACHE_EXTENT);
  1768. goto out;
  1769. }
  1770. }
  1771. /*
  1772. * requested block isn't allocated yet;
  1773. * we couldn't try to create block if create flag is zero
  1774. */
  1775. if (!create) {
  1776. /* put just found gap into cache to speed up
  1777. * subsequent requests */
  1778. ext4_ext_put_gap_in_cache(inode, path, iblock);
  1779. goto out2;
  1780. }
  1781. /*
  1782. * Okay, we need to do block allocation. Lazily initialize the block
  1783. * allocation info here if necessary.
  1784. */
  1785. if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
  1786. ext4_init_block_alloc_info(inode);
  1787. /* allocate new block */
  1788. goal = ext4_ext_find_goal(inode, path, iblock);
  1789. allocated = max_blocks;
  1790. newblock = ext4_new_blocks(handle, inode, goal, &allocated, &err);
  1791. if (!newblock)
  1792. goto out2;
  1793. ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
  1794. goal, newblock, allocated);
  1795. /* try to insert new extent into found leaf and return */
  1796. newex.ee_block = cpu_to_le32(iblock);
  1797. ext4_ext_store_pblock(&newex, newblock);
  1798. newex.ee_len = cpu_to_le16(allocated);
  1799. err = ext4_ext_insert_extent(handle, inode, path, &newex);
  1800. if (err)
  1801. goto out2;
  1802. if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize)
  1803. EXT4_I(inode)->i_disksize = inode->i_size;
  1804. /* previous routine could use block we allocated */
  1805. newblock = ext_pblock(&newex);
  1806. __set_bit(BH_New, &bh_result->b_state);
  1807. ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
  1808. EXT4_EXT_CACHE_EXTENT);
  1809. out:
  1810. if (allocated > max_blocks)
  1811. allocated = max_blocks;
  1812. ext4_ext_show_leaf(inode, path);
  1813. __set_bit(BH_Mapped, &bh_result->b_state);
  1814. bh_result->b_bdev = inode->i_sb->s_bdev;
  1815. bh_result->b_blocknr = newblock;
  1816. out2:
  1817. if (path) {
  1818. ext4_ext_drop_refs(path);
  1819. kfree(path);
  1820. }
  1821. mutex_unlock(&EXT4_I(inode)->truncate_mutex);
  1822. return err ? err : allocated;
  1823. }
  1824. void ext4_ext_truncate(struct inode * inode, struct page *page)
  1825. {
  1826. struct address_space *mapping = inode->i_mapping;
  1827. struct super_block *sb = inode->i_sb;
  1828. unsigned long last_block;
  1829. handle_t *handle;
  1830. int err = 0;
  1831. /*
  1832. * probably first extent we're gonna free will be last in block
  1833. */
  1834. err = ext4_writepage_trans_blocks(inode) + 3;
  1835. handle = ext4_journal_start(inode, err);
  1836. if (IS_ERR(handle)) {
  1837. if (page) {
  1838. clear_highpage(page);
  1839. flush_dcache_page(page);
  1840. unlock_page(page);
  1841. page_cache_release(page);
  1842. }
  1843. return;
  1844. }
  1845. if (page)
  1846. ext4_block_truncate_page(handle, page, mapping, inode->i_size);
  1847. mutex_lock(&EXT4_I(inode)->truncate_mutex);
  1848. ext4_ext_invalidate_cache(inode);
  1849. /*
  1850. * TODO: optimization is possible here.
  1851. * Probably we need not scan at all,
  1852. * because page truncation is enough.
  1853. */
  1854. if (ext4_orphan_add(handle, inode))
  1855. goto out_stop;
  1856. /* we have to know where to truncate from in crash case */
  1857. EXT4_I(inode)->i_disksize = inode->i_size;
  1858. ext4_mark_inode_dirty(handle, inode);
  1859. last_block = (inode->i_size + sb->s_blocksize - 1)
  1860. >> EXT4_BLOCK_SIZE_BITS(sb);
  1861. err = ext4_ext_remove_space(inode, last_block);
  1862. /* In a multi-transaction truncate, we only make the final
  1863. * transaction synchronous. */
  1864. if (IS_SYNC(inode))
  1865. handle->h_sync = 1;
  1866. out_stop:
  1867. /*
  1868. * If this was a simple ftruncate() and the file will remain alive,
  1869. * then we need to clear up the orphan record which we created above.
  1870. * However, if this was a real unlink then we were called by
  1871. * ext4_delete_inode(), and we allow that function to clean up the
  1872. * orphan info for us.
  1873. */
  1874. if (inode->i_nlink)
  1875. ext4_orphan_del(handle, inode);
  1876. mutex_unlock(&EXT4_I(inode)->truncate_mutex);
  1877. ext4_journal_stop(handle);
  1878. }
  1879. /*
  1880. * ext4_ext_writepage_trans_blocks:
  1881. * calculate max number of blocks we could modify
  1882. * in order to allocate new block for an inode
  1883. */
  1884. int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
  1885. {
  1886. int needed;
  1887. needed = ext4_ext_calc_credits_for_insert(inode, NULL);
  1888. /* caller wants to allocate num blocks, but note it includes sb */
  1889. needed = needed * num - (num - 1);
  1890. #ifdef CONFIG_QUOTA
  1891. needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
  1892. #endif
  1893. return needed;
  1894. }
  1895. EXPORT_SYMBOL(ext4_mark_inode_dirty);
  1896. EXPORT_SYMBOL(ext4_ext_invalidate_cache);
  1897. EXPORT_SYMBOL(ext4_ext_insert_extent);
  1898. EXPORT_SYMBOL(ext4_ext_walk_space);
  1899. EXPORT_SYMBOL(ext4_ext_find_goal);
  1900. EXPORT_SYMBOL(ext4_ext_calc_credits_for_insert);