extents.c 56 KB

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