extents.c 56 KB

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