extents.c 67 KB

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