extents.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167
  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. * ext4_ext_insert_extent:
  982. * tries to merge requsted extent into the existing extent or
  983. * inserts requested extent as new one into the tree,
  984. * creating new leaf in the no-space case.
  985. */
  986. int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
  987. struct ext4_ext_path *path,
  988. struct ext4_extent *newext)
  989. {
  990. struct ext4_extent_header * eh;
  991. struct ext4_extent *ex, *fex;
  992. struct ext4_extent *nearex; /* nearest extent */
  993. struct ext4_ext_path *npath = NULL;
  994. int depth, len, err, next;
  995. BUG_ON(newext->ee_len == 0);
  996. depth = ext_depth(inode);
  997. ex = path[depth].p_ext;
  998. BUG_ON(path[depth].p_hdr == NULL);
  999. /* try to insert block into found extent and return */
  1000. if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
  1001. ext_debug("append %d block to %d:%d (from %llu)\n",
  1002. le16_to_cpu(newext->ee_len),
  1003. le32_to_cpu(ex->ee_block),
  1004. le16_to_cpu(ex->ee_len), ext_pblock(ex));
  1005. err = ext4_ext_get_access(handle, inode, path + depth);
  1006. if (err)
  1007. return err;
  1008. ex->ee_len = cpu_to_le16(le16_to_cpu(ex->ee_len)
  1009. + le16_to_cpu(newext->ee_len));
  1010. eh = path[depth].p_hdr;
  1011. nearex = ex;
  1012. goto merge;
  1013. }
  1014. repeat:
  1015. depth = ext_depth(inode);
  1016. eh = path[depth].p_hdr;
  1017. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
  1018. goto has_space;
  1019. /* probably next leaf has space for us? */
  1020. fex = EXT_LAST_EXTENT(eh);
  1021. next = ext4_ext_next_leaf_block(inode, path);
  1022. if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
  1023. && next != EXT_MAX_BLOCK) {
  1024. ext_debug("next leaf block - %d\n", next);
  1025. BUG_ON(npath != NULL);
  1026. npath = ext4_ext_find_extent(inode, next, NULL);
  1027. if (IS_ERR(npath))
  1028. return PTR_ERR(npath);
  1029. BUG_ON(npath->p_depth != path->p_depth);
  1030. eh = npath[depth].p_hdr;
  1031. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
  1032. ext_debug("next leaf isnt full(%d)\n",
  1033. le16_to_cpu(eh->eh_entries));
  1034. path = npath;
  1035. goto repeat;
  1036. }
  1037. ext_debug("next leaf has no free space(%d,%d)\n",
  1038. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  1039. }
  1040. /*
  1041. * There is no free space in the found leaf.
  1042. * We're gonna add a new leaf in the tree.
  1043. */
  1044. err = ext4_ext_create_new_leaf(handle, inode, path, newext);
  1045. if (err)
  1046. goto cleanup;
  1047. depth = ext_depth(inode);
  1048. eh = path[depth].p_hdr;
  1049. has_space:
  1050. nearex = path[depth].p_ext;
  1051. err = ext4_ext_get_access(handle, inode, path + depth);
  1052. if (err)
  1053. goto cleanup;
  1054. if (!nearex) {
  1055. /* there is no extent in this leaf, create first one */
  1056. ext_debug("first extent in the leaf: %d:%llu:%d\n",
  1057. le32_to_cpu(newext->ee_block),
  1058. ext_pblock(newext),
  1059. le16_to_cpu(newext->ee_len));
  1060. path[depth].p_ext = EXT_FIRST_EXTENT(eh);
  1061. } else if (le32_to_cpu(newext->ee_block)
  1062. > le32_to_cpu(nearex->ee_block)) {
  1063. /* BUG_ON(newext->ee_block == nearex->ee_block); */
  1064. if (nearex != EXT_LAST_EXTENT(eh)) {
  1065. len = EXT_MAX_EXTENT(eh) - nearex;
  1066. len = (len - 1) * sizeof(struct ext4_extent);
  1067. len = len < 0 ? 0 : len;
  1068. ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
  1069. "move %d from 0x%p to 0x%p\n",
  1070. le32_to_cpu(newext->ee_block),
  1071. ext_pblock(newext),
  1072. le16_to_cpu(newext->ee_len),
  1073. nearex, len, nearex + 1, nearex + 2);
  1074. memmove(nearex + 2, nearex + 1, len);
  1075. }
  1076. path[depth].p_ext = nearex + 1;
  1077. } else {
  1078. BUG_ON(newext->ee_block == nearex->ee_block);
  1079. len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
  1080. len = len < 0 ? 0 : len;
  1081. ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
  1082. "move %d from 0x%p to 0x%p\n",
  1083. le32_to_cpu(newext->ee_block),
  1084. ext_pblock(newext),
  1085. le16_to_cpu(newext->ee_len),
  1086. nearex, len, nearex + 1, nearex + 2);
  1087. memmove(nearex + 1, nearex, len);
  1088. path[depth].p_ext = nearex;
  1089. }
  1090. eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)+1);
  1091. nearex = path[depth].p_ext;
  1092. nearex->ee_block = newext->ee_block;
  1093. nearex->ee_start = newext->ee_start;
  1094. nearex->ee_start_hi = newext->ee_start_hi;
  1095. nearex->ee_len = newext->ee_len;
  1096. merge:
  1097. /* try to merge extents to the right */
  1098. while (nearex < EXT_LAST_EXTENT(eh)) {
  1099. if (!ext4_can_extents_be_merged(inode, nearex, nearex + 1))
  1100. break;
  1101. /* merge with next extent! */
  1102. nearex->ee_len = cpu_to_le16(le16_to_cpu(nearex->ee_len)
  1103. + le16_to_cpu(nearex[1].ee_len));
  1104. if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
  1105. len = (EXT_LAST_EXTENT(eh) - nearex - 1)
  1106. * sizeof(struct ext4_extent);
  1107. memmove(nearex + 1, nearex + 2, len);
  1108. }
  1109. eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
  1110. BUG_ON(eh->eh_entries == 0);
  1111. }
  1112. /* try to merge extents to the left */
  1113. /* time to correct all indexes above */
  1114. err = ext4_ext_correct_indexes(handle, inode, path);
  1115. if (err)
  1116. goto cleanup;
  1117. err = ext4_ext_dirty(handle, inode, path + depth);
  1118. cleanup:
  1119. if (npath) {
  1120. ext4_ext_drop_refs(npath);
  1121. kfree(npath);
  1122. }
  1123. ext4_ext_tree_changed(inode);
  1124. ext4_ext_invalidate_cache(inode);
  1125. return err;
  1126. }
  1127. int ext4_ext_walk_space(struct inode *inode, unsigned long block,
  1128. unsigned long num, ext_prepare_callback func,
  1129. void *cbdata)
  1130. {
  1131. struct ext4_ext_path *path = NULL;
  1132. struct ext4_ext_cache cbex;
  1133. struct ext4_extent *ex;
  1134. unsigned long next, start = 0, end = 0;
  1135. unsigned long last = block + num;
  1136. int depth, exists, err = 0;
  1137. BUG_ON(func == NULL);
  1138. BUG_ON(inode == NULL);
  1139. while (block < last && block != EXT_MAX_BLOCK) {
  1140. num = last - block;
  1141. /* find extent for this block */
  1142. path = ext4_ext_find_extent(inode, block, path);
  1143. if (IS_ERR(path)) {
  1144. err = PTR_ERR(path);
  1145. path = NULL;
  1146. break;
  1147. }
  1148. depth = ext_depth(inode);
  1149. BUG_ON(path[depth].p_hdr == NULL);
  1150. ex = path[depth].p_ext;
  1151. next = ext4_ext_next_allocated_block(path);
  1152. exists = 0;
  1153. if (!ex) {
  1154. /* there is no extent yet, so try to allocate
  1155. * all requested space */
  1156. start = block;
  1157. end = block + num;
  1158. } else if (le32_to_cpu(ex->ee_block) > block) {
  1159. /* need to allocate space before found extent */
  1160. start = block;
  1161. end = le32_to_cpu(ex->ee_block);
  1162. if (block + num < end)
  1163. end = block + num;
  1164. } else if (block >=
  1165. le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len)) {
  1166. /* need to allocate space after found extent */
  1167. start = block;
  1168. end = block + num;
  1169. if (end >= next)
  1170. end = next;
  1171. } else if (block >= le32_to_cpu(ex->ee_block)) {
  1172. /*
  1173. * some part of requested space is covered
  1174. * by found extent
  1175. */
  1176. start = block;
  1177. end = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len);
  1178. if (block + num < end)
  1179. end = block + num;
  1180. exists = 1;
  1181. } else {
  1182. BUG();
  1183. }
  1184. BUG_ON(end <= start);
  1185. if (!exists) {
  1186. cbex.ec_block = start;
  1187. cbex.ec_len = end - start;
  1188. cbex.ec_start = 0;
  1189. cbex.ec_type = EXT4_EXT_CACHE_GAP;
  1190. } else {
  1191. cbex.ec_block = le32_to_cpu(ex->ee_block);
  1192. cbex.ec_len = le16_to_cpu(ex->ee_len);
  1193. cbex.ec_start = ext_pblock(ex);
  1194. cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
  1195. }
  1196. BUG_ON(cbex.ec_len == 0);
  1197. err = func(inode, path, &cbex, cbdata);
  1198. ext4_ext_drop_refs(path);
  1199. if (err < 0)
  1200. break;
  1201. if (err == EXT_REPEAT)
  1202. continue;
  1203. else if (err == EXT_BREAK) {
  1204. err = 0;
  1205. break;
  1206. }
  1207. if (ext_depth(inode) != depth) {
  1208. /* depth was changed. we have to realloc path */
  1209. kfree(path);
  1210. path = NULL;
  1211. }
  1212. block = cbex.ec_block + cbex.ec_len;
  1213. }
  1214. if (path) {
  1215. ext4_ext_drop_refs(path);
  1216. kfree(path);
  1217. }
  1218. return err;
  1219. }
  1220. static void
  1221. ext4_ext_put_in_cache(struct inode *inode, __u32 block,
  1222. __u32 len, __u32 start, int type)
  1223. {
  1224. struct ext4_ext_cache *cex;
  1225. BUG_ON(len == 0);
  1226. cex = &EXT4_I(inode)->i_cached_extent;
  1227. cex->ec_type = type;
  1228. cex->ec_block = block;
  1229. cex->ec_len = len;
  1230. cex->ec_start = start;
  1231. }
  1232. /*
  1233. * ext4_ext_put_gap_in_cache:
  1234. * calculate boundaries of the gap that the requested block fits into
  1235. * and cache this gap
  1236. */
  1237. static void
  1238. ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
  1239. unsigned long block)
  1240. {
  1241. int depth = ext_depth(inode);
  1242. unsigned long lblock, len;
  1243. struct ext4_extent *ex;
  1244. ex = path[depth].p_ext;
  1245. if (ex == NULL) {
  1246. /* there is no extent yet, so gap is [0;-] */
  1247. lblock = 0;
  1248. len = EXT_MAX_BLOCK;
  1249. ext_debug("cache gap(whole file):");
  1250. } else if (block < le32_to_cpu(ex->ee_block)) {
  1251. lblock = block;
  1252. len = le32_to_cpu(ex->ee_block) - block;
  1253. ext_debug("cache gap(before): %lu [%lu:%lu]",
  1254. (unsigned long) block,
  1255. (unsigned long) le32_to_cpu(ex->ee_block),
  1256. (unsigned long) le16_to_cpu(ex->ee_len));
  1257. } else if (block >= le32_to_cpu(ex->ee_block)
  1258. + le16_to_cpu(ex->ee_len)) {
  1259. lblock = le32_to_cpu(ex->ee_block)
  1260. + le16_to_cpu(ex->ee_len);
  1261. len = ext4_ext_next_allocated_block(path);
  1262. ext_debug("cache gap(after): [%lu:%lu] %lu",
  1263. (unsigned long) le32_to_cpu(ex->ee_block),
  1264. (unsigned long) le16_to_cpu(ex->ee_len),
  1265. (unsigned long) block);
  1266. BUG_ON(len == lblock);
  1267. len = len - lblock;
  1268. } else {
  1269. lblock = len = 0;
  1270. BUG();
  1271. }
  1272. ext_debug(" -> %lu:%lu\n", (unsigned long) lblock, len);
  1273. ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
  1274. }
  1275. static int
  1276. ext4_ext_in_cache(struct inode *inode, unsigned long block,
  1277. struct ext4_extent *ex)
  1278. {
  1279. struct ext4_ext_cache *cex;
  1280. cex = &EXT4_I(inode)->i_cached_extent;
  1281. /* has cache valid data? */
  1282. if (cex->ec_type == EXT4_EXT_CACHE_NO)
  1283. return EXT4_EXT_CACHE_NO;
  1284. BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
  1285. cex->ec_type != EXT4_EXT_CACHE_EXTENT);
  1286. if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
  1287. ex->ee_block = cpu_to_le32(cex->ec_block);
  1288. ext4_ext_store_pblock(ex, cex->ec_start);
  1289. ex->ee_len = cpu_to_le16(cex->ec_len);
  1290. ext_debug("%lu cached by %lu:%lu:%llu\n",
  1291. (unsigned long) block,
  1292. (unsigned long) cex->ec_block,
  1293. (unsigned long) cex->ec_len,
  1294. cex->ec_start);
  1295. return cex->ec_type;
  1296. }
  1297. /* not in cache */
  1298. return EXT4_EXT_CACHE_NO;
  1299. }
  1300. /*
  1301. * ext4_ext_rm_idx:
  1302. * removes index from the index block.
  1303. * It's used in truncate case only, thus all requests are for
  1304. * last index in the block only.
  1305. */
  1306. int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
  1307. struct ext4_ext_path *path)
  1308. {
  1309. struct buffer_head *bh;
  1310. int err;
  1311. ext4_fsblk_t leaf;
  1312. /* free index block */
  1313. path--;
  1314. leaf = idx_pblock(path->p_idx);
  1315. BUG_ON(path->p_hdr->eh_entries == 0);
  1316. err = ext4_ext_get_access(handle, inode, path);
  1317. if (err)
  1318. return err;
  1319. path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1);
  1320. err = ext4_ext_dirty(handle, inode, path);
  1321. if (err)
  1322. return err;
  1323. ext_debug("index is empty, remove it, free block %llu\n", leaf);
  1324. bh = sb_find_get_block(inode->i_sb, leaf);
  1325. ext4_forget(handle, 1, inode, bh, leaf);
  1326. ext4_free_blocks(handle, inode, leaf, 1);
  1327. return err;
  1328. }
  1329. /*
  1330. * ext4_ext_calc_credits_for_insert:
  1331. * This routine returns max. credits that the extent tree can consume.
  1332. * It should be OK for low-performance paths like ->writepage()
  1333. * To allow many writing processes to fit into a single transaction,
  1334. * the caller should calculate credits under truncate_mutex and
  1335. * pass the actual path.
  1336. */
  1337. int ext4_ext_calc_credits_for_insert(struct inode *inode,
  1338. struct ext4_ext_path *path)
  1339. {
  1340. int depth, needed;
  1341. if (path) {
  1342. /* probably there is space in leaf? */
  1343. depth = ext_depth(inode);
  1344. if (le16_to_cpu(path[depth].p_hdr->eh_entries)
  1345. < le16_to_cpu(path[depth].p_hdr->eh_max))
  1346. return 1;
  1347. }
  1348. /*
  1349. * given 32-bit logical block (4294967296 blocks), max. tree
  1350. * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
  1351. * Let's also add one more level for imbalance.
  1352. */
  1353. depth = 5;
  1354. /* allocation of new data block(s) */
  1355. needed = 2;
  1356. /*
  1357. * tree can be full, so it would need to grow in depth:
  1358. * we need one credit to modify old root, credits for
  1359. * new root will be added in split accounting
  1360. */
  1361. needed += 1;
  1362. /*
  1363. * Index split can happen, we would need:
  1364. * allocate intermediate indexes (bitmap + group)
  1365. * + change two blocks at each level, but root (already included)
  1366. */
  1367. needed += (depth * 2) + (depth * 2);
  1368. /* any allocation modifies superblock */
  1369. needed += 1;
  1370. return needed;
  1371. }
  1372. static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
  1373. struct ext4_extent *ex,
  1374. unsigned long from, unsigned long to)
  1375. {
  1376. struct buffer_head *bh;
  1377. int i;
  1378. #ifdef EXTENTS_STATS
  1379. {
  1380. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1381. unsigned short ee_len = le16_to_cpu(ex->ee_len);
  1382. spin_lock(&sbi->s_ext_stats_lock);
  1383. sbi->s_ext_blocks += ee_len;
  1384. sbi->s_ext_extents++;
  1385. if (ee_len < sbi->s_ext_min)
  1386. sbi->s_ext_min = ee_len;
  1387. if (ee_len > sbi->s_ext_max)
  1388. sbi->s_ext_max = ee_len;
  1389. if (ext_depth(inode) > sbi->s_depth_max)
  1390. sbi->s_depth_max = ext_depth(inode);
  1391. spin_unlock(&sbi->s_ext_stats_lock);
  1392. }
  1393. #endif
  1394. if (from >= le32_to_cpu(ex->ee_block)
  1395. && to == le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1) {
  1396. /* tail removal */
  1397. unsigned long num;
  1398. ext4_fsblk_t start;
  1399. num = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - from;
  1400. start = ext_pblock(ex) + le16_to_cpu(ex->ee_len) - num;
  1401. ext_debug("free last %lu blocks starting %llu\n", num, start);
  1402. for (i = 0; i < num; i++) {
  1403. bh = sb_find_get_block(inode->i_sb, start + i);
  1404. ext4_forget(handle, 0, inode, bh, start + i);
  1405. }
  1406. ext4_free_blocks(handle, inode, start, num);
  1407. } else if (from == le32_to_cpu(ex->ee_block)
  1408. && to <= le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1) {
  1409. printk("strange request: removal %lu-%lu from %u:%u\n",
  1410. from, to, le32_to_cpu(ex->ee_block), le16_to_cpu(ex->ee_len));
  1411. } else {
  1412. printk("strange request: removal(2) %lu-%lu from %u:%u\n",
  1413. from, to, le32_to_cpu(ex->ee_block), le16_to_cpu(ex->ee_len));
  1414. }
  1415. return 0;
  1416. }
  1417. static int
  1418. ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
  1419. struct ext4_ext_path *path, unsigned long start)
  1420. {
  1421. int err = 0, correct_index = 0;
  1422. int depth = ext_depth(inode), credits;
  1423. struct ext4_extent_header *eh;
  1424. unsigned a, b, block, num;
  1425. unsigned long ex_ee_block;
  1426. unsigned short ex_ee_len;
  1427. struct ext4_extent *ex;
  1428. ext_debug("truncate since %lu in leaf\n", start);
  1429. if (!path[depth].p_hdr)
  1430. path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
  1431. eh = path[depth].p_hdr;
  1432. BUG_ON(eh == NULL);
  1433. BUG_ON(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max));
  1434. BUG_ON(eh->eh_magic != EXT4_EXT_MAGIC);
  1435. /* find where to start removing */
  1436. ex = EXT_LAST_EXTENT(eh);
  1437. ex_ee_block = le32_to_cpu(ex->ee_block);
  1438. ex_ee_len = le16_to_cpu(ex->ee_len);
  1439. while (ex >= EXT_FIRST_EXTENT(eh) &&
  1440. ex_ee_block + ex_ee_len > start) {
  1441. ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
  1442. path[depth].p_ext = ex;
  1443. a = ex_ee_block > start ? ex_ee_block : start;
  1444. b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
  1445. ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
  1446. ext_debug(" border %u:%u\n", a, b);
  1447. if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
  1448. block = 0;
  1449. num = 0;
  1450. BUG();
  1451. } else if (a != ex_ee_block) {
  1452. /* remove tail of the extent */
  1453. block = ex_ee_block;
  1454. num = a - block;
  1455. } else if (b != ex_ee_block + ex_ee_len - 1) {
  1456. /* remove head of the extent */
  1457. block = a;
  1458. num = b - a;
  1459. /* there is no "make a hole" API yet */
  1460. BUG();
  1461. } else {
  1462. /* remove whole extent: excellent! */
  1463. block = ex_ee_block;
  1464. num = 0;
  1465. BUG_ON(a != ex_ee_block);
  1466. BUG_ON(b != ex_ee_block + ex_ee_len - 1);
  1467. }
  1468. /* at present, extent can't cross block group: */
  1469. /* leaf + bitmap + group desc + sb + inode */
  1470. credits = 5;
  1471. if (ex == EXT_FIRST_EXTENT(eh)) {
  1472. correct_index = 1;
  1473. credits += (ext_depth(inode)) + 1;
  1474. }
  1475. #ifdef CONFIG_QUOTA
  1476. credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
  1477. #endif
  1478. handle = ext4_ext_journal_restart(handle, credits);
  1479. if (IS_ERR(handle)) {
  1480. err = PTR_ERR(handle);
  1481. goto out;
  1482. }
  1483. err = ext4_ext_get_access(handle, inode, path + depth);
  1484. if (err)
  1485. goto out;
  1486. err = ext4_remove_blocks(handle, inode, ex, a, b);
  1487. if (err)
  1488. goto out;
  1489. if (num == 0) {
  1490. /* this extent is removed; mark slot entirely unused */
  1491. ext4_ext_store_pblock(ex, 0);
  1492. eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
  1493. }
  1494. ex->ee_block = cpu_to_le32(block);
  1495. ex->ee_len = cpu_to_le16(num);
  1496. err = ext4_ext_dirty(handle, inode, path + depth);
  1497. if (err)
  1498. goto out;
  1499. ext_debug("new extent: %u:%u:%llu\n", block, num,
  1500. ext_pblock(ex));
  1501. ex--;
  1502. ex_ee_block = le32_to_cpu(ex->ee_block);
  1503. ex_ee_len = le16_to_cpu(ex->ee_len);
  1504. }
  1505. if (correct_index && eh->eh_entries)
  1506. err = ext4_ext_correct_indexes(handle, inode, path);
  1507. /* if this leaf is free, then we should
  1508. * remove it from index block above */
  1509. if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
  1510. err = ext4_ext_rm_idx(handle, inode, path + depth);
  1511. out:
  1512. return err;
  1513. }
  1514. /*
  1515. * ext4_ext_more_to_rm:
  1516. * returns 1 if current index has to be freed (even partial)
  1517. */
  1518. static int
  1519. ext4_ext_more_to_rm(struct ext4_ext_path *path)
  1520. {
  1521. BUG_ON(path->p_idx == NULL);
  1522. if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
  1523. return 0;
  1524. /*
  1525. * if truncate on deeper level happened, it wasn't partial,
  1526. * so we have to consider current index for truncation
  1527. */
  1528. if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
  1529. return 0;
  1530. return 1;
  1531. }
  1532. int ext4_ext_remove_space(struct inode *inode, unsigned long start)
  1533. {
  1534. struct super_block *sb = inode->i_sb;
  1535. int depth = ext_depth(inode);
  1536. struct ext4_ext_path *path;
  1537. handle_t *handle;
  1538. int i = 0, err = 0;
  1539. ext_debug("truncate since %lu\n", start);
  1540. /* probably first extent we're gonna free will be last in block */
  1541. handle = ext4_journal_start(inode, depth + 1);
  1542. if (IS_ERR(handle))
  1543. return PTR_ERR(handle);
  1544. ext4_ext_invalidate_cache(inode);
  1545. /*
  1546. * We start scanning from right side, freeing all the blocks
  1547. * after i_size and walking into the tree depth-wise.
  1548. */
  1549. path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_KERNEL);
  1550. if (path == NULL) {
  1551. ext4_journal_stop(handle);
  1552. return -ENOMEM;
  1553. }
  1554. path[0].p_hdr = ext_inode_hdr(inode);
  1555. if (ext4_ext_check_header(__FUNCTION__, inode, path[0].p_hdr)) {
  1556. err = -EIO;
  1557. goto out;
  1558. }
  1559. path[0].p_depth = depth;
  1560. while (i >= 0 && err == 0) {
  1561. if (i == depth) {
  1562. /* this is leaf block */
  1563. err = ext4_ext_rm_leaf(handle, inode, path, start);
  1564. /* root level has p_bh == NULL, brelse() eats this */
  1565. brelse(path[i].p_bh);
  1566. path[i].p_bh = NULL;
  1567. i--;
  1568. continue;
  1569. }
  1570. /* this is index block */
  1571. if (!path[i].p_hdr) {
  1572. ext_debug("initialize header\n");
  1573. path[i].p_hdr = ext_block_hdr(path[i].p_bh);
  1574. if (ext4_ext_check_header(__FUNCTION__, inode,
  1575. path[i].p_hdr)) {
  1576. err = -EIO;
  1577. goto out;
  1578. }
  1579. }
  1580. BUG_ON(le16_to_cpu(path[i].p_hdr->eh_entries)
  1581. > le16_to_cpu(path[i].p_hdr->eh_max));
  1582. BUG_ON(path[i].p_hdr->eh_magic != EXT4_EXT_MAGIC);
  1583. if (!path[i].p_idx) {
  1584. /* this level hasn't been touched yet */
  1585. path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
  1586. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
  1587. ext_debug("init index ptr: hdr 0x%p, num %d\n",
  1588. path[i].p_hdr,
  1589. le16_to_cpu(path[i].p_hdr->eh_entries));
  1590. } else {
  1591. /* we were already here, see at next index */
  1592. path[i].p_idx--;
  1593. }
  1594. ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
  1595. i, EXT_FIRST_INDEX(path[i].p_hdr),
  1596. path[i].p_idx);
  1597. if (ext4_ext_more_to_rm(path + i)) {
  1598. /* go to the next level */
  1599. ext_debug("move to level %d (block %llu)\n",
  1600. i + 1, idx_pblock(path[i].p_idx));
  1601. memset(path + i + 1, 0, sizeof(*path));
  1602. path[i+1].p_bh =
  1603. sb_bread(sb, idx_pblock(path[i].p_idx));
  1604. if (!path[i+1].p_bh) {
  1605. /* should we reset i_size? */
  1606. err = -EIO;
  1607. break;
  1608. }
  1609. /* save actual number of indexes since this
  1610. * number is changed at the next iteration */
  1611. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
  1612. i++;
  1613. } else {
  1614. /* we finished processing this index, go up */
  1615. if (path[i].p_hdr->eh_entries == 0 && i > 0) {
  1616. /* index is empty, remove it;
  1617. * handle must be already prepared by the
  1618. * truncatei_leaf() */
  1619. err = ext4_ext_rm_idx(handle, inode, path + i);
  1620. }
  1621. /* root level has p_bh == NULL, brelse() eats this */
  1622. brelse(path[i].p_bh);
  1623. path[i].p_bh = NULL;
  1624. i--;
  1625. ext_debug("return to level %d\n", i);
  1626. }
  1627. }
  1628. /* TODO: flexible tree reduction should be here */
  1629. if (path->p_hdr->eh_entries == 0) {
  1630. /*
  1631. * truncate to zero freed all the tree,
  1632. * so we need to correct eh_depth
  1633. */
  1634. err = ext4_ext_get_access(handle, inode, path);
  1635. if (err == 0) {
  1636. ext_inode_hdr(inode)->eh_depth = 0;
  1637. ext_inode_hdr(inode)->eh_max =
  1638. cpu_to_le16(ext4_ext_space_root(inode));
  1639. err = ext4_ext_dirty(handle, inode, path);
  1640. }
  1641. }
  1642. out:
  1643. ext4_ext_tree_changed(inode);
  1644. ext4_ext_drop_refs(path);
  1645. kfree(path);
  1646. ext4_journal_stop(handle);
  1647. return err;
  1648. }
  1649. /*
  1650. * called at mount time
  1651. */
  1652. void ext4_ext_init(struct super_block *sb)
  1653. {
  1654. /*
  1655. * possible initialization would be here
  1656. */
  1657. if (test_opt(sb, EXTENTS)) {
  1658. printk("EXT4-fs: file extents enabled");
  1659. #ifdef AGGRESSIVE_TEST
  1660. printk(", aggressive tests");
  1661. #endif
  1662. #ifdef CHECK_BINSEARCH
  1663. printk(", check binsearch");
  1664. #endif
  1665. #ifdef EXTENTS_STATS
  1666. printk(", stats");
  1667. #endif
  1668. printk("\n");
  1669. #ifdef EXTENTS_STATS
  1670. spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
  1671. EXT4_SB(sb)->s_ext_min = 1 << 30;
  1672. EXT4_SB(sb)->s_ext_max = 0;
  1673. #endif
  1674. }
  1675. }
  1676. /*
  1677. * called at umount time
  1678. */
  1679. void ext4_ext_release(struct super_block *sb)
  1680. {
  1681. if (!test_opt(sb, EXTENTS))
  1682. return;
  1683. #ifdef EXTENTS_STATS
  1684. if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
  1685. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1686. printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
  1687. sbi->s_ext_blocks, sbi->s_ext_extents,
  1688. sbi->s_ext_blocks / sbi->s_ext_extents);
  1689. printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
  1690. sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
  1691. }
  1692. #endif
  1693. }
  1694. int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
  1695. ext4_fsblk_t iblock,
  1696. unsigned long max_blocks, struct buffer_head *bh_result,
  1697. int create, int extend_disksize)
  1698. {
  1699. struct ext4_ext_path *path = NULL;
  1700. struct ext4_extent newex, *ex;
  1701. ext4_fsblk_t goal, newblock;
  1702. int err = 0, depth;
  1703. unsigned long allocated = 0;
  1704. __clear_bit(BH_New, &bh_result->b_state);
  1705. ext_debug("blocks %d/%lu requested for inode %u\n", (int) iblock,
  1706. max_blocks, (unsigned) inode->i_ino);
  1707. mutex_lock(&EXT4_I(inode)->truncate_mutex);
  1708. /* check in cache */
  1709. goal = ext4_ext_in_cache(inode, iblock, &newex);
  1710. if (goal) {
  1711. if (goal == EXT4_EXT_CACHE_GAP) {
  1712. if (!create) {
  1713. /* block isn't allocated yet and
  1714. * user doesn't want to allocate it */
  1715. goto out2;
  1716. }
  1717. /* we should allocate requested block */
  1718. } else if (goal == EXT4_EXT_CACHE_EXTENT) {
  1719. /* block is already allocated */
  1720. newblock = iblock
  1721. - le32_to_cpu(newex.ee_block)
  1722. + ext_pblock(&newex);
  1723. /* number of remaining blocks in the extent */
  1724. allocated = le16_to_cpu(newex.ee_len) -
  1725. (iblock - le32_to_cpu(newex.ee_block));
  1726. goto out;
  1727. } else {
  1728. BUG();
  1729. }
  1730. }
  1731. /* find extent for this block */
  1732. path = ext4_ext_find_extent(inode, iblock, NULL);
  1733. if (IS_ERR(path)) {
  1734. err = PTR_ERR(path);
  1735. path = NULL;
  1736. goto out2;
  1737. }
  1738. depth = ext_depth(inode);
  1739. /*
  1740. * consistent leaf must not be empty;
  1741. * this situation is possible, though, _during_ tree modification;
  1742. * this is why assert can't be put in ext4_ext_find_extent()
  1743. */
  1744. BUG_ON(path[depth].p_ext == NULL && depth != 0);
  1745. ex = path[depth].p_ext;
  1746. if (ex) {
  1747. unsigned long ee_block = le32_to_cpu(ex->ee_block);
  1748. ext4_fsblk_t ee_start = ext_pblock(ex);
  1749. unsigned short ee_len = le16_to_cpu(ex->ee_len);
  1750. /*
  1751. * Allow future support for preallocated extents to be added
  1752. * as an RO_COMPAT feature:
  1753. * Uninitialized extents are treated as holes, except that
  1754. * we avoid (fail) allocating new blocks during a write.
  1755. */
  1756. if (ee_len > EXT_MAX_LEN)
  1757. goto out2;
  1758. /* if found extent covers block, simply return it */
  1759. if (iblock >= ee_block && iblock < ee_block + ee_len) {
  1760. newblock = iblock - ee_block + ee_start;
  1761. /* number of remaining blocks in the extent */
  1762. allocated = ee_len - (iblock - ee_block);
  1763. ext_debug("%d fit into %lu:%d -> %llu\n", (int) iblock,
  1764. ee_block, ee_len, newblock);
  1765. ext4_ext_put_in_cache(inode, ee_block, ee_len,
  1766. ee_start, EXT4_EXT_CACHE_EXTENT);
  1767. goto out;
  1768. }
  1769. }
  1770. /*
  1771. * requested block isn't allocated yet;
  1772. * we couldn't try to create block if create flag is zero
  1773. */
  1774. if (!create) {
  1775. /* put just found gap into cache to speed up
  1776. * subsequent requests */
  1777. ext4_ext_put_gap_in_cache(inode, path, iblock);
  1778. goto out2;
  1779. }
  1780. /*
  1781. * Okay, we need to do block allocation. Lazily initialize the block
  1782. * allocation info here if necessary.
  1783. */
  1784. if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
  1785. ext4_init_block_alloc_info(inode);
  1786. /* allocate new block */
  1787. goal = ext4_ext_find_goal(inode, path, iblock);
  1788. allocated = max_blocks;
  1789. newblock = ext4_new_blocks(handle, inode, goal, &allocated, &err);
  1790. if (!newblock)
  1791. goto out2;
  1792. ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
  1793. goal, newblock, allocated);
  1794. /* try to insert new extent into found leaf and return */
  1795. newex.ee_block = cpu_to_le32(iblock);
  1796. ext4_ext_store_pblock(&newex, newblock);
  1797. newex.ee_len = cpu_to_le16(allocated);
  1798. err = ext4_ext_insert_extent(handle, inode, path, &newex);
  1799. if (err)
  1800. goto out2;
  1801. if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize)
  1802. EXT4_I(inode)->i_disksize = inode->i_size;
  1803. /* previous routine could use block we allocated */
  1804. newblock = ext_pblock(&newex);
  1805. __set_bit(BH_New, &bh_result->b_state);
  1806. ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
  1807. EXT4_EXT_CACHE_EXTENT);
  1808. out:
  1809. if (allocated > max_blocks)
  1810. allocated = max_blocks;
  1811. ext4_ext_show_leaf(inode, path);
  1812. __set_bit(BH_Mapped, &bh_result->b_state);
  1813. bh_result->b_bdev = inode->i_sb->s_bdev;
  1814. bh_result->b_blocknr = newblock;
  1815. out2:
  1816. if (path) {
  1817. ext4_ext_drop_refs(path);
  1818. kfree(path);
  1819. }
  1820. mutex_unlock(&EXT4_I(inode)->truncate_mutex);
  1821. return err ? err : allocated;
  1822. }
  1823. void ext4_ext_truncate(struct inode * inode, struct page *page)
  1824. {
  1825. struct address_space *mapping = inode->i_mapping;
  1826. struct super_block *sb = inode->i_sb;
  1827. unsigned long last_block;
  1828. handle_t *handle;
  1829. int err = 0;
  1830. /*
  1831. * probably first extent we're gonna free will be last in block
  1832. */
  1833. err = ext4_writepage_trans_blocks(inode) + 3;
  1834. handle = ext4_journal_start(inode, err);
  1835. if (IS_ERR(handle)) {
  1836. if (page) {
  1837. clear_highpage(page);
  1838. flush_dcache_page(page);
  1839. unlock_page(page);
  1840. page_cache_release(page);
  1841. }
  1842. return;
  1843. }
  1844. if (page)
  1845. ext4_block_truncate_page(handle, page, mapping, inode->i_size);
  1846. mutex_lock(&EXT4_I(inode)->truncate_mutex);
  1847. ext4_ext_invalidate_cache(inode);
  1848. /*
  1849. * TODO: optimization is possible here.
  1850. * Probably we need not scan at all,
  1851. * because page truncation is enough.
  1852. */
  1853. if (ext4_orphan_add(handle, inode))
  1854. goto out_stop;
  1855. /* we have to know where to truncate from in crash case */
  1856. EXT4_I(inode)->i_disksize = inode->i_size;
  1857. ext4_mark_inode_dirty(handle, inode);
  1858. last_block = (inode->i_size + sb->s_blocksize - 1)
  1859. >> EXT4_BLOCK_SIZE_BITS(sb);
  1860. err = ext4_ext_remove_space(inode, last_block);
  1861. /* In a multi-transaction truncate, we only make the final
  1862. * transaction synchronous. */
  1863. if (IS_SYNC(inode))
  1864. handle->h_sync = 1;
  1865. out_stop:
  1866. /*
  1867. * If this was a simple ftruncate() and the file will remain alive,
  1868. * then we need to clear up the orphan record which we created above.
  1869. * However, if this was a real unlink then we were called by
  1870. * ext4_delete_inode(), and we allow that function to clean up the
  1871. * orphan info for us.
  1872. */
  1873. if (inode->i_nlink)
  1874. ext4_orphan_del(handle, inode);
  1875. mutex_unlock(&EXT4_I(inode)->truncate_mutex);
  1876. ext4_journal_stop(handle);
  1877. }
  1878. /*
  1879. * ext4_ext_writepage_trans_blocks:
  1880. * calculate max number of blocks we could modify
  1881. * in order to allocate new block for an inode
  1882. */
  1883. int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
  1884. {
  1885. int needed;
  1886. needed = ext4_ext_calc_credits_for_insert(inode, NULL);
  1887. /* caller wants to allocate num blocks, but note it includes sb */
  1888. needed = needed * num - (num - 1);
  1889. #ifdef CONFIG_QUOTA
  1890. needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
  1891. #endif
  1892. return needed;
  1893. }
  1894. EXPORT_SYMBOL(ext4_mark_inode_dirty);
  1895. EXPORT_SYMBOL(ext4_ext_invalidate_cache);
  1896. EXPORT_SYMBOL(ext4_ext_insert_extent);
  1897. EXPORT_SYMBOL(ext4_ext_walk_space);
  1898. EXPORT_SYMBOL(ext4_ext_find_goal);
  1899. EXPORT_SYMBOL(ext4_ext_calc_credits_for_insert);