extents.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149
  1. /*
  2. * Copyright (c) 2003-2006, Cluster File Systems, Inc, info@clusterfs.com
  3. * Written by Alex Tomas <alex@clusterfs.com>
  4. *
  5. * Architecture independence:
  6. * Copyright (c) 2005, Bull S.A.
  7. * Written by Pierre Peiffer <pierre.peiffer@bull.net>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public Licens
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-
  21. */
  22. /*
  23. * Extents support for EXT4
  24. *
  25. * TODO:
  26. * - ext4*_error() should be used in some situations
  27. * - analyze all BUG()/BUG_ON(), use -EIO where appropriate
  28. * - smart tree reduction
  29. */
  30. #include <linux/module.h>
  31. #include <linux/fs.h>
  32. #include <linux/time.h>
  33. #include <linux/ext4_jbd2.h>
  34. #include <linux/jbd.h>
  35. #include <linux/smp_lock.h>
  36. #include <linux/highuid.h>
  37. #include <linux/pagemap.h>
  38. #include <linux/quotaops.h>
  39. #include <linux/string.h>
  40. #include <linux/slab.h>
  41. #include <linux/ext4_fs_extents.h>
  42. #include <asm/uaccess.h>
  43. /*
  44. * ext_pblock:
  45. * combine low and high parts of physical block number into ext4_fsblk_t
  46. */
  47. static inline ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
  48. {
  49. ext4_fsblk_t block;
  50. block = le32_to_cpu(ex->ee_start);
  51. 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 inline 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 inline 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 inline void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
  81. {
  82. ix->ei_leaf = cpu_to_le32((unsigned long) (pb & 0xffffffff));
  83. ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
  84. }
  85. static int ext4_ext_check_header(const char *function, struct inode *inode,
  86. struct ext4_extent_header *eh)
  87. {
  88. const char *error_msg = NULL;
  89. if (unlikely(eh->eh_magic != EXT4_EXT_MAGIC)) {
  90. error_msg = "invalid magic";
  91. goto corrupted;
  92. }
  93. if (unlikely(eh->eh_max == 0)) {
  94. error_msg = "invalid eh_max";
  95. goto corrupted;
  96. }
  97. if (unlikely(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max))) {
  98. error_msg = "invalid eh_entries";
  99. goto corrupted;
  100. }
  101. return 0;
  102. corrupted:
  103. ext4_error(inode->i_sb, function,
  104. "bad header in inode #%lu: %s - magic %x, "
  105. "entries %u, max %u, depth %u",
  106. inode->i_ino, error_msg, le16_to_cpu(eh->eh_magic),
  107. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max),
  108. le16_to_cpu(eh->eh_depth));
  109. return -EIO;
  110. }
  111. static handle_t *ext4_ext_journal_restart(handle_t *handle, int needed)
  112. {
  113. int err;
  114. if (handle->h_buffer_credits > needed)
  115. return handle;
  116. if (!ext4_journal_extend(handle, needed))
  117. return handle;
  118. err = ext4_journal_restart(handle, needed);
  119. return handle;
  120. }
  121. /*
  122. * could return:
  123. * - EROFS
  124. * - ENOMEM
  125. */
  126. static int ext4_ext_get_access(handle_t *handle, struct inode *inode,
  127. struct ext4_ext_path *path)
  128. {
  129. if (path->p_bh) {
  130. /* path points to block */
  131. return ext4_journal_get_write_access(handle, path->p_bh);
  132. }
  133. /* path points to leaf/index in inode body */
  134. /* we use in-core data, no need to protect them */
  135. return 0;
  136. }
  137. /*
  138. * could return:
  139. * - EROFS
  140. * - ENOMEM
  141. * - EIO
  142. */
  143. static int ext4_ext_dirty(handle_t *handle, struct inode *inode,
  144. struct ext4_ext_path *path)
  145. {
  146. int err;
  147. if (path->p_bh) {
  148. /* path points to block */
  149. err = ext4_journal_dirty_metadata(handle, path->p_bh);
  150. } else {
  151. /* path points to leaf/index in inode body */
  152. err = ext4_mark_inode_dirty(handle, inode);
  153. }
  154. return err;
  155. }
  156. static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
  157. struct ext4_ext_path *path,
  158. ext4_fsblk_t block)
  159. {
  160. struct ext4_inode_info *ei = EXT4_I(inode);
  161. ext4_fsblk_t bg_start;
  162. ext4_grpblk_t colour;
  163. int depth;
  164. if (path) {
  165. struct ext4_extent *ex;
  166. depth = path->p_depth;
  167. /* try to predict block placement */
  168. if ((ex = path[depth].p_ext))
  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 inline 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 AGRESSIVE_TEST
  198. if (size > 6)
  199. size = 6;
  200. #endif
  201. return size;
  202. }
  203. static inline 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 AGRESSIVE_TEST
  209. if (size > 5)
  210. size = 5;
  211. #endif
  212. return size;
  213. }
  214. static inline 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 AGRESSIVE_TEST
  221. if (size > 3)
  222. size = 3;
  223. #endif
  224. return size;
  225. }
  226. static inline 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 AGRESSIVE_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 = kmalloc(sizeof(struct ext4_ext_path) * (depth + 2),
  415. GFP_NOFS);
  416. if (!path)
  417. return ERR_PTR(-ENOMEM);
  418. alloc = 1;
  419. }
  420. memset(path, 0, sizeof(struct ext4_ext_path) * (depth + 1));
  421. path[0].p_hdr = eh;
  422. /* walk through the tree */
  423. while (i) {
  424. ext_debug("depth %d: num %d, max %d\n",
  425. ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  426. ext4_ext_binsearch_idx(inode, path + ppos, block);
  427. path[ppos].p_block = idx_pblock(path[ppos].p_idx);
  428. path[ppos].p_depth = i;
  429. path[ppos].p_ext = NULL;
  430. bh = sb_bread(inode->i_sb, path[ppos].p_block);
  431. if (!bh)
  432. goto err;
  433. eh = ext_block_hdr(bh);
  434. ppos++;
  435. BUG_ON(ppos > depth);
  436. path[ppos].p_bh = bh;
  437. path[ppos].p_hdr = eh;
  438. i--;
  439. if (ext4_ext_check_header(__FUNCTION__, inode, eh))
  440. goto err;
  441. }
  442. path[ppos].p_depth = i;
  443. path[ppos].p_hdr = eh;
  444. path[ppos].p_ext = NULL;
  445. path[ppos].p_idx = NULL;
  446. if (ext4_ext_check_header(__FUNCTION__, inode, eh))
  447. goto err;
  448. /* find extent */
  449. ext4_ext_binsearch(inode, path + ppos, block);
  450. ext4_ext_show_path(inode, path);
  451. return path;
  452. err:
  453. ext4_ext_drop_refs(path);
  454. if (alloc)
  455. kfree(path);
  456. return ERR_PTR(-EIO);
  457. }
  458. /*
  459. * ext4_ext_insert_index:
  460. * insert new index [@logical;@ptr] into the block at @curp;
  461. * check where to insert: before @curp or after @curp
  462. */
  463. static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
  464. struct ext4_ext_path *curp,
  465. int logical, ext4_fsblk_t ptr)
  466. {
  467. struct ext4_extent_idx *ix;
  468. int len, err;
  469. if ((err = ext4_ext_get_access(handle, inode, curp)))
  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 = kmalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
  558. if (!ablocks)
  559. return -ENOMEM;
  560. memset(ablocks, 0, sizeof(ext4_fsblk_t) * depth);
  561. /* allocate all needed blocks */
  562. ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
  563. for (a = 0; a < depth - at; a++) {
  564. newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
  565. if (newblock == 0)
  566. goto cleanup;
  567. ablocks[a] = newblock;
  568. }
  569. /* initialize new leaf */
  570. newblock = ablocks[--a];
  571. BUG_ON(newblock == 0);
  572. bh = sb_getblk(inode->i_sb, newblock);
  573. if (!bh) {
  574. err = -EIO;
  575. goto cleanup;
  576. }
  577. lock_buffer(bh);
  578. if ((err = ext4_journal_get_create_access(handle, bh)))
  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. if ((err = ext4_journal_dirty_metadata(handle, bh)))
  612. goto cleanup;
  613. brelse(bh);
  614. bh = NULL;
  615. /* correct old leaf */
  616. if (m) {
  617. if ((err = ext4_ext_get_access(handle, inode, path + depth)))
  618. goto cleanup;
  619. path[depth].p_hdr->eh_entries =
  620. cpu_to_le16(le16_to_cpu(path[depth].p_hdr->eh_entries)-m);
  621. if ((err = ext4_ext_dirty(handle, inode, path + depth)))
  622. goto cleanup;
  623. }
  624. /* create intermediate indexes */
  625. k = depth - at - 1;
  626. BUG_ON(k < 0);
  627. if (k)
  628. ext_debug("create %d intermediate indices\n", k);
  629. /* insert new index into current index block */
  630. /* current depth stored in i var */
  631. i = depth - 1;
  632. while (k--) {
  633. oldblock = newblock;
  634. newblock = ablocks[--a];
  635. bh = sb_getblk(inode->i_sb, (ext4_fsblk_t)newblock);
  636. if (!bh) {
  637. err = -EIO;
  638. goto cleanup;
  639. }
  640. lock_buffer(bh);
  641. if ((err = ext4_journal_get_create_access(handle, bh)))
  642. goto cleanup;
  643. neh = ext_block_hdr(bh);
  644. neh->eh_entries = cpu_to_le16(1);
  645. neh->eh_magic = EXT4_EXT_MAGIC;
  646. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
  647. neh->eh_depth = cpu_to_le16(depth - i);
  648. fidx = EXT_FIRST_INDEX(neh);
  649. fidx->ei_block = border;
  650. ext4_idx_store_pblock(fidx, oldblock);
  651. ext_debug("int.index at %d (block %llu): %lu -> %llu\n", i,
  652. newblock, (unsigned long) le32_to_cpu(border),
  653. oldblock);
  654. /* copy indexes */
  655. m = 0;
  656. path[i].p_idx++;
  657. ext_debug("cur 0x%p, last 0x%p\n", path[i].p_idx,
  658. EXT_MAX_INDEX(path[i].p_hdr));
  659. BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
  660. EXT_LAST_INDEX(path[i].p_hdr));
  661. while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
  662. ext_debug("%d: move %d:%d in new index %llu\n", i,
  663. le32_to_cpu(path[i].p_idx->ei_block),
  664. idx_pblock(path[i].p_idx),
  665. newblock);
  666. /*memmove(++fidx, path[i].p_idx++,
  667. sizeof(struct ext4_extent_idx));
  668. neh->eh_entries++;
  669. BUG_ON(neh->eh_entries > neh->eh_max);*/
  670. path[i].p_idx++;
  671. m++;
  672. }
  673. if (m) {
  674. memmove(++fidx, path[i].p_idx - m,
  675. sizeof(struct ext4_extent_idx) * m);
  676. neh->eh_entries =
  677. cpu_to_le16(le16_to_cpu(neh->eh_entries) + m);
  678. }
  679. set_buffer_uptodate(bh);
  680. unlock_buffer(bh);
  681. if ((err = ext4_journal_dirty_metadata(handle, bh)))
  682. goto cleanup;
  683. brelse(bh);
  684. bh = NULL;
  685. /* correct old index */
  686. if (m) {
  687. err = ext4_ext_get_access(handle, inode, path + i);
  688. if (err)
  689. goto cleanup;
  690. path[i].p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path[i].p_hdr->eh_entries)-m);
  691. err = ext4_ext_dirty(handle, inode, path + i);
  692. if (err)
  693. goto cleanup;
  694. }
  695. i--;
  696. }
  697. /* insert new index */
  698. err = ext4_ext_insert_index(handle, inode, path + at,
  699. le32_to_cpu(border), newblock);
  700. cleanup:
  701. if (bh) {
  702. if (buffer_locked(bh))
  703. unlock_buffer(bh);
  704. brelse(bh);
  705. }
  706. if (err) {
  707. /* free all allocated blocks in error case */
  708. for (i = 0; i < depth; i++) {
  709. if (!ablocks[i])
  710. continue;
  711. ext4_free_blocks(handle, inode, ablocks[i], 1);
  712. }
  713. }
  714. kfree(ablocks);
  715. return err;
  716. }
  717. /*
  718. * ext4_ext_grow_indepth:
  719. * implements tree growing procedure:
  720. * - allocates new block
  721. * - moves top-level data (index block or leaf) into the new block
  722. * - initializes new top-level, creating index that points to the
  723. * just created block
  724. */
  725. static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
  726. struct ext4_ext_path *path,
  727. struct ext4_extent *newext)
  728. {
  729. struct ext4_ext_path *curp = path;
  730. struct ext4_extent_header *neh;
  731. struct ext4_extent_idx *fidx;
  732. struct buffer_head *bh;
  733. ext4_fsblk_t newblock;
  734. int err = 0;
  735. newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
  736. if (newblock == 0)
  737. return err;
  738. bh = sb_getblk(inode->i_sb, newblock);
  739. if (!bh) {
  740. err = -EIO;
  741. ext4_std_error(inode->i_sb, err);
  742. return err;
  743. }
  744. lock_buffer(bh);
  745. if ((err = ext4_journal_get_create_access(handle, bh))) {
  746. unlock_buffer(bh);
  747. goto out;
  748. }
  749. /* move top-level index/leaf into new block */
  750. memmove(bh->b_data, curp->p_hdr, sizeof(EXT4_I(inode)->i_data));
  751. /* set size of new block */
  752. neh = ext_block_hdr(bh);
  753. /* old root could have indexes or leaves
  754. * so calculate e_max right way */
  755. if (ext_depth(inode))
  756. neh->eh_max = cpu_to_le16(ext4_ext_space_block_idx(inode));
  757. else
  758. neh->eh_max = cpu_to_le16(ext4_ext_space_block(inode));
  759. neh->eh_magic = EXT4_EXT_MAGIC;
  760. set_buffer_uptodate(bh);
  761. unlock_buffer(bh);
  762. if ((err = ext4_journal_dirty_metadata(handle, bh)))
  763. goto out;
  764. /* create index in new top-level index: num,max,pointer */
  765. if ((err = ext4_ext_get_access(handle, inode, curp)))
  766. goto out;
  767. curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
  768. curp->p_hdr->eh_max = cpu_to_le16(ext4_ext_space_root_idx(inode));
  769. curp->p_hdr->eh_entries = cpu_to_le16(1);
  770. curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
  771. /* FIXME: it works, but actually path[0] can be index */
  772. curp->p_idx->ei_block = EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
  773. ext4_idx_store_pblock(curp->p_idx, newblock);
  774. neh = ext_inode_hdr(inode);
  775. fidx = EXT_FIRST_INDEX(neh);
  776. ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
  777. le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
  778. le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
  779. neh->eh_depth = cpu_to_le16(path->p_depth + 1);
  780. err = ext4_ext_dirty(handle, inode, curp);
  781. out:
  782. brelse(bh);
  783. return err;
  784. }
  785. /*
  786. * ext4_ext_create_new_leaf:
  787. * finds empty index and adds new leaf.
  788. * if no free index is found, then it requests in-depth growing.
  789. */
  790. static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
  791. struct ext4_ext_path *path,
  792. struct ext4_extent *newext)
  793. {
  794. struct ext4_ext_path *curp;
  795. int depth, i, err = 0;
  796. repeat:
  797. i = depth = ext_depth(inode);
  798. /* walk up to the tree and look for free index entry */
  799. curp = path + depth;
  800. while (i > 0 && !EXT_HAS_FREE_INDEX(curp)) {
  801. i--;
  802. curp--;
  803. }
  804. /* we use already allocated block for index block,
  805. * so subsequent data blocks should be contiguous */
  806. if (EXT_HAS_FREE_INDEX(curp)) {
  807. /* if we found index with free entry, then use that
  808. * entry: create all needed subtree and add new leaf */
  809. err = ext4_ext_split(handle, inode, path, newext, i);
  810. /* refill path */
  811. ext4_ext_drop_refs(path);
  812. path = ext4_ext_find_extent(inode,
  813. le32_to_cpu(newext->ee_block),
  814. path);
  815. if (IS_ERR(path))
  816. err = PTR_ERR(path);
  817. } else {
  818. /* tree is full, time to grow in depth */
  819. err = ext4_ext_grow_indepth(handle, inode, path, newext);
  820. if (err)
  821. goto out;
  822. /* refill path */
  823. ext4_ext_drop_refs(path);
  824. path = ext4_ext_find_extent(inode,
  825. le32_to_cpu(newext->ee_block),
  826. path);
  827. if (IS_ERR(path)) {
  828. err = PTR_ERR(path);
  829. goto out;
  830. }
  831. /*
  832. * only first (depth 0 -> 1) produces free space;
  833. * in all other cases we have to split the grown tree
  834. */
  835. depth = ext_depth(inode);
  836. if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
  837. /* now we need to split */
  838. goto repeat;
  839. }
  840. }
  841. out:
  842. return err;
  843. }
  844. /*
  845. * ext4_ext_next_allocated_block:
  846. * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
  847. * NOTE: it considers block number from index entry as
  848. * allocated block. Thus, index entries have to be consistent
  849. * with leaves.
  850. */
  851. static unsigned long
  852. ext4_ext_next_allocated_block(struct ext4_ext_path *path)
  853. {
  854. int depth;
  855. BUG_ON(path == NULL);
  856. depth = path->p_depth;
  857. if (depth == 0 && path->p_ext == NULL)
  858. return EXT_MAX_BLOCK;
  859. while (depth >= 0) {
  860. if (depth == path->p_depth) {
  861. /* leaf */
  862. if (path[depth].p_ext !=
  863. EXT_LAST_EXTENT(path[depth].p_hdr))
  864. return le32_to_cpu(path[depth].p_ext[1].ee_block);
  865. } else {
  866. /* index */
  867. if (path[depth].p_idx !=
  868. EXT_LAST_INDEX(path[depth].p_hdr))
  869. return le32_to_cpu(path[depth].p_idx[1].ei_block);
  870. }
  871. depth--;
  872. }
  873. return EXT_MAX_BLOCK;
  874. }
  875. /*
  876. * ext4_ext_next_leaf_block:
  877. * returns first allocated block from next leaf or EXT_MAX_BLOCK
  878. */
  879. static unsigned ext4_ext_next_leaf_block(struct inode *inode,
  880. struct ext4_ext_path *path)
  881. {
  882. int depth;
  883. BUG_ON(path == NULL);
  884. depth = path->p_depth;
  885. /* zero-tree has no leaf blocks at all */
  886. if (depth == 0)
  887. return EXT_MAX_BLOCK;
  888. /* go to index block */
  889. depth--;
  890. while (depth >= 0) {
  891. if (path[depth].p_idx !=
  892. EXT_LAST_INDEX(path[depth].p_hdr))
  893. return le32_to_cpu(path[depth].p_idx[1].ei_block);
  894. depth--;
  895. }
  896. return EXT_MAX_BLOCK;
  897. }
  898. /*
  899. * ext4_ext_correct_indexes:
  900. * if leaf gets modified and modified extent is first in the leaf,
  901. * then we have to correct all indexes above.
  902. * TODO: do we need to correct tree in all cases?
  903. */
  904. int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
  905. struct ext4_ext_path *path)
  906. {
  907. struct ext4_extent_header *eh;
  908. int depth = ext_depth(inode);
  909. struct ext4_extent *ex;
  910. __le32 border;
  911. int k, err = 0;
  912. eh = path[depth].p_hdr;
  913. ex = path[depth].p_ext;
  914. BUG_ON(ex == NULL);
  915. BUG_ON(eh == NULL);
  916. if (depth == 0) {
  917. /* there is no tree at all */
  918. return 0;
  919. }
  920. if (ex != EXT_FIRST_EXTENT(eh)) {
  921. /* we correct tree if first leaf got modified only */
  922. return 0;
  923. }
  924. /*
  925. * TODO: we need correction if border is smaller than current one
  926. */
  927. k = depth - 1;
  928. border = path[depth].p_ext->ee_block;
  929. if ((err = ext4_ext_get_access(handle, inode, path + k)))
  930. return err;
  931. path[k].p_idx->ei_block = border;
  932. if ((err = ext4_ext_dirty(handle, inode, path + k)))
  933. return err;
  934. while (k--) {
  935. /* change all left-side indexes */
  936. if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
  937. break;
  938. if ((err = ext4_ext_get_access(handle, inode, path + k)))
  939. break;
  940. path[k].p_idx->ei_block = border;
  941. if ((err = ext4_ext_dirty(handle, inode, path + k)))
  942. break;
  943. }
  944. return err;
  945. }
  946. static int inline
  947. ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
  948. struct ext4_extent *ex2)
  949. {
  950. if (le32_to_cpu(ex1->ee_block) + le16_to_cpu(ex1->ee_len) !=
  951. le32_to_cpu(ex2->ee_block))
  952. return 0;
  953. /*
  954. * To allow future support for preallocated extents to be added
  955. * as an RO_COMPAT feature, refuse to merge to extents if
  956. * this can result in the top bit of ee_len being set.
  957. */
  958. if (le16_to_cpu(ex1->ee_len) + le16_to_cpu(ex2->ee_len) > EXT_MAX_LEN)
  959. return 0;
  960. #ifdef AGRESSIVE_TEST
  961. if (le16_to_cpu(ex1->ee_len) >= 4)
  962. return 0;
  963. #endif
  964. if (ext_pblock(ex1) + le16_to_cpu(ex1->ee_len) == ext_pblock(ex2))
  965. return 1;
  966. return 0;
  967. }
  968. /*
  969. * ext4_ext_insert_extent:
  970. * tries to merge requsted extent into the existing extent or
  971. * inserts requested extent as new one into the tree,
  972. * creating new leaf in the no-space case.
  973. */
  974. int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
  975. struct ext4_ext_path *path,
  976. struct ext4_extent *newext)
  977. {
  978. struct ext4_extent_header * eh;
  979. struct ext4_extent *ex, *fex;
  980. struct ext4_extent *nearex; /* nearest extent */
  981. struct ext4_ext_path *npath = NULL;
  982. int depth, len, err, next;
  983. BUG_ON(newext->ee_len == 0);
  984. depth = ext_depth(inode);
  985. ex = path[depth].p_ext;
  986. BUG_ON(path[depth].p_hdr == NULL);
  987. /* try to insert block into found extent and return */
  988. if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
  989. ext_debug("append %d block to %d:%d (from %llu)\n",
  990. le16_to_cpu(newext->ee_len),
  991. le32_to_cpu(ex->ee_block),
  992. le16_to_cpu(ex->ee_len), ext_pblock(ex));
  993. if ((err = ext4_ext_get_access(handle, inode, path + depth)))
  994. return err;
  995. ex->ee_len = cpu_to_le16(le16_to_cpu(ex->ee_len)
  996. + le16_to_cpu(newext->ee_len));
  997. eh = path[depth].p_hdr;
  998. nearex = ex;
  999. goto merge;
  1000. }
  1001. repeat:
  1002. depth = ext_depth(inode);
  1003. eh = path[depth].p_hdr;
  1004. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max))
  1005. goto has_space;
  1006. /* probably next leaf has space for us? */
  1007. fex = EXT_LAST_EXTENT(eh);
  1008. next = ext4_ext_next_leaf_block(inode, path);
  1009. if (le32_to_cpu(newext->ee_block) > le32_to_cpu(fex->ee_block)
  1010. && next != EXT_MAX_BLOCK) {
  1011. ext_debug("next leaf block - %d\n", next);
  1012. BUG_ON(npath != NULL);
  1013. npath = ext4_ext_find_extent(inode, next, NULL);
  1014. if (IS_ERR(npath))
  1015. return PTR_ERR(npath);
  1016. BUG_ON(npath->p_depth != path->p_depth);
  1017. eh = npath[depth].p_hdr;
  1018. if (le16_to_cpu(eh->eh_entries) < le16_to_cpu(eh->eh_max)) {
  1019. ext_debug("next leaf isnt full(%d)\n",
  1020. le16_to_cpu(eh->eh_entries));
  1021. path = npath;
  1022. goto repeat;
  1023. }
  1024. ext_debug("next leaf has no free space(%d,%d)\n",
  1025. le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
  1026. }
  1027. /*
  1028. * There is no free space in the found leaf.
  1029. * We're gonna add a new leaf in the tree.
  1030. */
  1031. err = ext4_ext_create_new_leaf(handle, inode, path, newext);
  1032. if (err)
  1033. goto cleanup;
  1034. depth = ext_depth(inode);
  1035. eh = path[depth].p_hdr;
  1036. has_space:
  1037. nearex = path[depth].p_ext;
  1038. if ((err = ext4_ext_get_access(handle, inode, path + depth)))
  1039. goto cleanup;
  1040. if (!nearex) {
  1041. /* there is no extent in this leaf, create first one */
  1042. ext_debug("first extent in the leaf: %d:%llu:%d\n",
  1043. le32_to_cpu(newext->ee_block),
  1044. ext_pblock(newext),
  1045. le16_to_cpu(newext->ee_len));
  1046. path[depth].p_ext = EXT_FIRST_EXTENT(eh);
  1047. } else if (le32_to_cpu(newext->ee_block)
  1048. > le32_to_cpu(nearex->ee_block)) {
  1049. /* BUG_ON(newext->ee_block == nearex->ee_block); */
  1050. if (nearex != EXT_LAST_EXTENT(eh)) {
  1051. len = EXT_MAX_EXTENT(eh) - nearex;
  1052. len = (len - 1) * sizeof(struct ext4_extent);
  1053. len = len < 0 ? 0 : len;
  1054. ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
  1055. "move %d from 0x%p to 0x%p\n",
  1056. le32_to_cpu(newext->ee_block),
  1057. ext_pblock(newext),
  1058. le16_to_cpu(newext->ee_len),
  1059. nearex, len, nearex + 1, nearex + 2);
  1060. memmove(nearex + 2, nearex + 1, len);
  1061. }
  1062. path[depth].p_ext = nearex + 1;
  1063. } else {
  1064. BUG_ON(newext->ee_block == nearex->ee_block);
  1065. len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
  1066. len = len < 0 ? 0 : len;
  1067. ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
  1068. "move %d from 0x%p to 0x%p\n",
  1069. le32_to_cpu(newext->ee_block),
  1070. ext_pblock(newext),
  1071. le16_to_cpu(newext->ee_len),
  1072. nearex, len, nearex + 1, nearex + 2);
  1073. memmove(nearex + 1, nearex, len);
  1074. path[depth].p_ext = nearex;
  1075. }
  1076. eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)+1);
  1077. nearex = path[depth].p_ext;
  1078. nearex->ee_block = newext->ee_block;
  1079. nearex->ee_start = newext->ee_start;
  1080. nearex->ee_start_hi = newext->ee_start_hi;
  1081. nearex->ee_len = newext->ee_len;
  1082. merge:
  1083. /* try to merge extents to the right */
  1084. while (nearex < EXT_LAST_EXTENT(eh)) {
  1085. if (!ext4_can_extents_be_merged(inode, nearex, nearex + 1))
  1086. break;
  1087. /* merge with next extent! */
  1088. nearex->ee_len = cpu_to_le16(le16_to_cpu(nearex->ee_len)
  1089. + le16_to_cpu(nearex[1].ee_len));
  1090. if (nearex + 1 < EXT_LAST_EXTENT(eh)) {
  1091. len = (EXT_LAST_EXTENT(eh) - nearex - 1)
  1092. * sizeof(struct ext4_extent);
  1093. memmove(nearex + 1, nearex + 2, len);
  1094. }
  1095. eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
  1096. BUG_ON(eh->eh_entries == 0);
  1097. }
  1098. /* try to merge extents to the left */
  1099. /* time to correct all indexes above */
  1100. err = ext4_ext_correct_indexes(handle, inode, path);
  1101. if (err)
  1102. goto cleanup;
  1103. err = ext4_ext_dirty(handle, inode, path + depth);
  1104. cleanup:
  1105. if (npath) {
  1106. ext4_ext_drop_refs(npath);
  1107. kfree(npath);
  1108. }
  1109. ext4_ext_tree_changed(inode);
  1110. ext4_ext_invalidate_cache(inode);
  1111. return err;
  1112. }
  1113. int ext4_ext_walk_space(struct inode *inode, unsigned long block,
  1114. unsigned long num, ext_prepare_callback func,
  1115. void *cbdata)
  1116. {
  1117. struct ext4_ext_path *path = NULL;
  1118. struct ext4_ext_cache cbex;
  1119. struct ext4_extent *ex;
  1120. unsigned long next, start = 0, end = 0;
  1121. unsigned long last = block + num;
  1122. int depth, exists, err = 0;
  1123. BUG_ON(func == NULL);
  1124. BUG_ON(inode == NULL);
  1125. while (block < last && block != EXT_MAX_BLOCK) {
  1126. num = last - block;
  1127. /* find extent for this block */
  1128. path = ext4_ext_find_extent(inode, block, path);
  1129. if (IS_ERR(path)) {
  1130. err = PTR_ERR(path);
  1131. path = NULL;
  1132. break;
  1133. }
  1134. depth = ext_depth(inode);
  1135. BUG_ON(path[depth].p_hdr == NULL);
  1136. ex = path[depth].p_ext;
  1137. next = ext4_ext_next_allocated_block(path);
  1138. exists = 0;
  1139. if (!ex) {
  1140. /* there is no extent yet, so try to allocate
  1141. * all requested space */
  1142. start = block;
  1143. end = block + num;
  1144. } else if (le32_to_cpu(ex->ee_block) > block) {
  1145. /* need to allocate space before found extent */
  1146. start = block;
  1147. end = le32_to_cpu(ex->ee_block);
  1148. if (block + num < end)
  1149. end = block + num;
  1150. } else if (block >=
  1151. le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len)) {
  1152. /* need to allocate space after found extent */
  1153. start = block;
  1154. end = block + num;
  1155. if (end >= next)
  1156. end = next;
  1157. } else if (block >= le32_to_cpu(ex->ee_block)) {
  1158. /*
  1159. * some part of requested space is covered
  1160. * by found extent
  1161. */
  1162. start = block;
  1163. end = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len);
  1164. if (block + num < end)
  1165. end = block + num;
  1166. exists = 1;
  1167. } else {
  1168. BUG();
  1169. }
  1170. BUG_ON(end <= start);
  1171. if (!exists) {
  1172. cbex.ec_block = start;
  1173. cbex.ec_len = end - start;
  1174. cbex.ec_start = 0;
  1175. cbex.ec_type = EXT4_EXT_CACHE_GAP;
  1176. } else {
  1177. cbex.ec_block = le32_to_cpu(ex->ee_block);
  1178. cbex.ec_len = le16_to_cpu(ex->ee_len);
  1179. cbex.ec_start = ext_pblock(ex);
  1180. cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
  1181. }
  1182. BUG_ON(cbex.ec_len == 0);
  1183. err = func(inode, path, &cbex, cbdata);
  1184. ext4_ext_drop_refs(path);
  1185. if (err < 0)
  1186. break;
  1187. if (err == EXT_REPEAT)
  1188. continue;
  1189. else if (err == EXT_BREAK) {
  1190. err = 0;
  1191. break;
  1192. }
  1193. if (ext_depth(inode) != depth) {
  1194. /* depth was changed. we have to realloc path */
  1195. kfree(path);
  1196. path = NULL;
  1197. }
  1198. block = cbex.ec_block + cbex.ec_len;
  1199. }
  1200. if (path) {
  1201. ext4_ext_drop_refs(path);
  1202. kfree(path);
  1203. }
  1204. return err;
  1205. }
  1206. static inline void
  1207. ext4_ext_put_in_cache(struct inode *inode, __u32 block,
  1208. __u32 len, __u32 start, int type)
  1209. {
  1210. struct ext4_ext_cache *cex;
  1211. BUG_ON(len == 0);
  1212. cex = &EXT4_I(inode)->i_cached_extent;
  1213. cex->ec_type = type;
  1214. cex->ec_block = block;
  1215. cex->ec_len = len;
  1216. cex->ec_start = start;
  1217. }
  1218. /*
  1219. * ext4_ext_put_gap_in_cache:
  1220. * calculate boundaries of the gap that the requested block fits into
  1221. * and cache this gap
  1222. */
  1223. static inline void
  1224. ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
  1225. unsigned long block)
  1226. {
  1227. int depth = ext_depth(inode);
  1228. unsigned long lblock, len;
  1229. struct ext4_extent *ex;
  1230. ex = path[depth].p_ext;
  1231. if (ex == NULL) {
  1232. /* there is no extent yet, so gap is [0;-] */
  1233. lblock = 0;
  1234. len = EXT_MAX_BLOCK;
  1235. ext_debug("cache gap(whole file):");
  1236. } else if (block < le32_to_cpu(ex->ee_block)) {
  1237. lblock = block;
  1238. len = le32_to_cpu(ex->ee_block) - block;
  1239. ext_debug("cache gap(before): %lu [%lu:%lu]",
  1240. (unsigned long) block,
  1241. (unsigned long) le32_to_cpu(ex->ee_block),
  1242. (unsigned long) le16_to_cpu(ex->ee_len));
  1243. } else if (block >= le32_to_cpu(ex->ee_block)
  1244. + le16_to_cpu(ex->ee_len)) {
  1245. lblock = le32_to_cpu(ex->ee_block)
  1246. + le16_to_cpu(ex->ee_len);
  1247. len = ext4_ext_next_allocated_block(path);
  1248. ext_debug("cache gap(after): [%lu:%lu] %lu",
  1249. (unsigned long) le32_to_cpu(ex->ee_block),
  1250. (unsigned long) le16_to_cpu(ex->ee_len),
  1251. (unsigned long) block);
  1252. BUG_ON(len == lblock);
  1253. len = len - lblock;
  1254. } else {
  1255. lblock = len = 0;
  1256. BUG();
  1257. }
  1258. ext_debug(" -> %lu:%lu\n", (unsigned long) lblock, len);
  1259. ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
  1260. }
  1261. static inline int
  1262. ext4_ext_in_cache(struct inode *inode, unsigned long block,
  1263. struct ext4_extent *ex)
  1264. {
  1265. struct ext4_ext_cache *cex;
  1266. cex = &EXT4_I(inode)->i_cached_extent;
  1267. /* has cache valid data? */
  1268. if (cex->ec_type == EXT4_EXT_CACHE_NO)
  1269. return EXT4_EXT_CACHE_NO;
  1270. BUG_ON(cex->ec_type != EXT4_EXT_CACHE_GAP &&
  1271. cex->ec_type != EXT4_EXT_CACHE_EXTENT);
  1272. if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
  1273. ex->ee_block = cpu_to_le32(cex->ec_block);
  1274. ext4_ext_store_pblock(ex, cex->ec_start);
  1275. ex->ee_len = cpu_to_le16(cex->ec_len);
  1276. ext_debug("%lu cached by %lu:%lu:%llu\n",
  1277. (unsigned long) block,
  1278. (unsigned long) cex->ec_block,
  1279. (unsigned long) cex->ec_len,
  1280. cex->ec_start);
  1281. return cex->ec_type;
  1282. }
  1283. /* not in cache */
  1284. return EXT4_EXT_CACHE_NO;
  1285. }
  1286. /*
  1287. * ext4_ext_rm_idx:
  1288. * removes index from the index block.
  1289. * It's used in truncate case only, thus all requests are for
  1290. * last index in the block only.
  1291. */
  1292. int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
  1293. struct ext4_ext_path *path)
  1294. {
  1295. struct buffer_head *bh;
  1296. int err;
  1297. ext4_fsblk_t leaf;
  1298. /* free index block */
  1299. path--;
  1300. leaf = idx_pblock(path->p_idx);
  1301. BUG_ON(path->p_hdr->eh_entries == 0);
  1302. if ((err = ext4_ext_get_access(handle, inode, path)))
  1303. return err;
  1304. path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1);
  1305. if ((err = ext4_ext_dirty(handle, inode, path)))
  1306. return err;
  1307. ext_debug("index is empty, remove it, free block %llu\n", leaf);
  1308. bh = sb_find_get_block(inode->i_sb, leaf);
  1309. ext4_forget(handle, 1, inode, bh, leaf);
  1310. ext4_free_blocks(handle, inode, leaf, 1);
  1311. return err;
  1312. }
  1313. /*
  1314. * ext4_ext_calc_credits_for_insert:
  1315. * This routine returns max. credits that the extent tree can consume.
  1316. * It should be OK for low-performance paths like ->writepage()
  1317. * To allow many writing processes to fit into a single transaction,
  1318. * the caller should calculate credits under truncate_mutex and
  1319. * pass the actual path.
  1320. */
  1321. int inline ext4_ext_calc_credits_for_insert(struct inode *inode,
  1322. struct ext4_ext_path *path)
  1323. {
  1324. int depth, needed;
  1325. if (path) {
  1326. /* probably there is space in leaf? */
  1327. depth = ext_depth(inode);
  1328. if (le16_to_cpu(path[depth].p_hdr->eh_entries)
  1329. < le16_to_cpu(path[depth].p_hdr->eh_max))
  1330. return 1;
  1331. }
  1332. /*
  1333. * given 32-bit logical block (4294967296 blocks), max. tree
  1334. * can be 4 levels in depth -- 4 * 340^4 == 53453440000.
  1335. * Let's also add one more level for imbalance.
  1336. */
  1337. depth = 5;
  1338. /* allocation of new data block(s) */
  1339. needed = 2;
  1340. /*
  1341. * tree can be full, so it would need to grow in depth:
  1342. * allocation + old root + new root
  1343. */
  1344. needed += 2 + 1 + 1;
  1345. /*
  1346. * Index split can happen, we would need:
  1347. * allocate intermediate indexes (bitmap + group)
  1348. * + change two blocks at each level, but root (already included)
  1349. */
  1350. needed = (depth * 2) + (depth * 2);
  1351. /* any allocation modifies superblock */
  1352. needed += 1;
  1353. return needed;
  1354. }
  1355. static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
  1356. struct ext4_extent *ex,
  1357. unsigned long from, unsigned long to)
  1358. {
  1359. struct buffer_head *bh;
  1360. int i;
  1361. #ifdef EXTENTS_STATS
  1362. {
  1363. struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
  1364. unsigned short ee_len = le16_to_cpu(ex->ee_len);
  1365. spin_lock(&sbi->s_ext_stats_lock);
  1366. sbi->s_ext_blocks += ee_len;
  1367. sbi->s_ext_extents++;
  1368. if (ee_len < sbi->s_ext_min)
  1369. sbi->s_ext_min = ee_len;
  1370. if (ee_len > sbi->s_ext_max)
  1371. sbi->s_ext_max = ee_len;
  1372. if (ext_depth(inode) > sbi->s_depth_max)
  1373. sbi->s_depth_max = ext_depth(inode);
  1374. spin_unlock(&sbi->s_ext_stats_lock);
  1375. }
  1376. #endif
  1377. if (from >= le32_to_cpu(ex->ee_block)
  1378. && to == le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1) {
  1379. /* tail removal */
  1380. unsigned long num;
  1381. ext4_fsblk_t start;
  1382. num = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - from;
  1383. start = ext_pblock(ex) + le16_to_cpu(ex->ee_len) - num;
  1384. ext_debug("free last %lu blocks starting %llu\n", num, start);
  1385. for (i = 0; i < num; i++) {
  1386. bh = sb_find_get_block(inode->i_sb, start + i);
  1387. ext4_forget(handle, 0, inode, bh, start + i);
  1388. }
  1389. ext4_free_blocks(handle, inode, start, num);
  1390. } else if (from == le32_to_cpu(ex->ee_block)
  1391. && to <= le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1) {
  1392. printk("strange request: removal %lu-%lu from %u:%u\n",
  1393. from, to, le32_to_cpu(ex->ee_block), le16_to_cpu(ex->ee_len));
  1394. } else {
  1395. printk("strange request: removal(2) %lu-%lu from %u:%u\n",
  1396. from, to, le32_to_cpu(ex->ee_block), le16_to_cpu(ex->ee_len));
  1397. }
  1398. return 0;
  1399. }
  1400. static int
  1401. ext4_ext_rm_leaf(handle_t *handle, struct inode *inode,
  1402. struct ext4_ext_path *path, unsigned long start)
  1403. {
  1404. int err = 0, correct_index = 0;
  1405. int depth = ext_depth(inode), credits;
  1406. struct ext4_extent_header *eh;
  1407. unsigned a, b, block, num;
  1408. unsigned long ex_ee_block;
  1409. unsigned short ex_ee_len;
  1410. struct ext4_extent *ex;
  1411. ext_debug("truncate since %lu in leaf\n", start);
  1412. if (!path[depth].p_hdr)
  1413. path[depth].p_hdr = ext_block_hdr(path[depth].p_bh);
  1414. eh = path[depth].p_hdr;
  1415. BUG_ON(eh == NULL);
  1416. BUG_ON(le16_to_cpu(eh->eh_entries) > le16_to_cpu(eh->eh_max));
  1417. BUG_ON(eh->eh_magic != EXT4_EXT_MAGIC);
  1418. /* find where to start removing */
  1419. ex = EXT_LAST_EXTENT(eh);
  1420. ex_ee_block = le32_to_cpu(ex->ee_block);
  1421. ex_ee_len = le16_to_cpu(ex->ee_len);
  1422. while (ex >= EXT_FIRST_EXTENT(eh) &&
  1423. ex_ee_block + ex_ee_len > start) {
  1424. ext_debug("remove ext %lu:%u\n", ex_ee_block, ex_ee_len);
  1425. path[depth].p_ext = ex;
  1426. a = ex_ee_block > start ? ex_ee_block : start;
  1427. b = ex_ee_block + ex_ee_len - 1 < EXT_MAX_BLOCK ?
  1428. ex_ee_block + ex_ee_len - 1 : EXT_MAX_BLOCK;
  1429. ext_debug(" border %u:%u\n", a, b);
  1430. if (a != ex_ee_block && b != ex_ee_block + ex_ee_len - 1) {
  1431. block = 0;
  1432. num = 0;
  1433. BUG();
  1434. } else if (a != ex_ee_block) {
  1435. /* remove tail of the extent */
  1436. block = ex_ee_block;
  1437. num = a - block;
  1438. } else if (b != ex_ee_block + ex_ee_len - 1) {
  1439. /* remove head of the extent */
  1440. block = a;
  1441. num = b - a;
  1442. /* there is no "make a hole" API yet */
  1443. BUG();
  1444. } else {
  1445. /* remove whole extent: excellent! */
  1446. block = ex_ee_block;
  1447. num = 0;
  1448. BUG_ON(a != ex_ee_block);
  1449. BUG_ON(b != ex_ee_block + ex_ee_len - 1);
  1450. }
  1451. /* at present, extent can't cross block group: */
  1452. /* leaf + bitmap + group desc + sb + inode */
  1453. credits = 5;
  1454. if (ex == EXT_FIRST_EXTENT(eh)) {
  1455. correct_index = 1;
  1456. credits += (ext_depth(inode)) + 1;
  1457. }
  1458. #ifdef CONFIG_QUOTA
  1459. credits += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
  1460. #endif
  1461. handle = ext4_ext_journal_restart(handle, credits);
  1462. if (IS_ERR(handle)) {
  1463. err = PTR_ERR(handle);
  1464. goto out;
  1465. }
  1466. err = ext4_ext_get_access(handle, inode, path + depth);
  1467. if (err)
  1468. goto out;
  1469. err = ext4_remove_blocks(handle, inode, ex, a, b);
  1470. if (err)
  1471. goto out;
  1472. if (num == 0) {
  1473. /* this extent is removed; mark slot entirely unused */
  1474. ext4_ext_store_pblock(ex, 0);
  1475. eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
  1476. }
  1477. ex->ee_block = cpu_to_le32(block);
  1478. ex->ee_len = cpu_to_le16(num);
  1479. err = ext4_ext_dirty(handle, inode, path + depth);
  1480. if (err)
  1481. goto out;
  1482. ext_debug("new extent: %u:%u:%llu\n", block, num,
  1483. ext_pblock(ex));
  1484. ex--;
  1485. ex_ee_block = le32_to_cpu(ex->ee_block);
  1486. ex_ee_len = le16_to_cpu(ex->ee_len);
  1487. }
  1488. if (correct_index && eh->eh_entries)
  1489. err = ext4_ext_correct_indexes(handle, inode, path);
  1490. /* if this leaf is free, then we should
  1491. * remove it from index block above */
  1492. if (err == 0 && eh->eh_entries == 0 && path[depth].p_bh != NULL)
  1493. err = ext4_ext_rm_idx(handle, inode, path + depth);
  1494. out:
  1495. return err;
  1496. }
  1497. /*
  1498. * ext4_ext_more_to_rm:
  1499. * returns 1 if current index has to be freed (even partial)
  1500. */
  1501. static int inline
  1502. ext4_ext_more_to_rm(struct ext4_ext_path *path)
  1503. {
  1504. BUG_ON(path->p_idx == NULL);
  1505. if (path->p_idx < EXT_FIRST_INDEX(path->p_hdr))
  1506. return 0;
  1507. /*
  1508. * if truncate on deeper level happened, it wasn't partial,
  1509. * so we have to consider current index for truncation
  1510. */
  1511. if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
  1512. return 0;
  1513. return 1;
  1514. }
  1515. int ext4_ext_remove_space(struct inode *inode, unsigned long start)
  1516. {
  1517. struct super_block *sb = inode->i_sb;
  1518. int depth = ext_depth(inode);
  1519. struct ext4_ext_path *path;
  1520. handle_t *handle;
  1521. int i = 0, err = 0;
  1522. ext_debug("truncate since %lu\n", start);
  1523. /* probably first extent we're gonna free will be last in block */
  1524. handle = ext4_journal_start(inode, depth + 1);
  1525. if (IS_ERR(handle))
  1526. return PTR_ERR(handle);
  1527. ext4_ext_invalidate_cache(inode);
  1528. /*
  1529. * We start scanning from right side, freeing all the blocks
  1530. * after i_size and walking into the tree depth-wise.
  1531. */
  1532. path = kmalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_KERNEL);
  1533. if (path == NULL) {
  1534. ext4_journal_stop(handle);
  1535. return -ENOMEM;
  1536. }
  1537. memset(path, 0, sizeof(struct ext4_ext_path) * (depth + 1));
  1538. path[0].p_hdr = ext_inode_hdr(inode);
  1539. if (ext4_ext_check_header(__FUNCTION__, inode, path[0].p_hdr)) {
  1540. err = -EIO;
  1541. goto out;
  1542. }
  1543. path[0].p_depth = depth;
  1544. while (i >= 0 && err == 0) {
  1545. if (i == depth) {
  1546. /* this is leaf block */
  1547. err = ext4_ext_rm_leaf(handle, inode, path, start);
  1548. /* root level has p_bh == NULL, brelse() eats this */
  1549. brelse(path[i].p_bh);
  1550. path[i].p_bh = NULL;
  1551. i--;
  1552. continue;
  1553. }
  1554. /* this is index block */
  1555. if (!path[i].p_hdr) {
  1556. ext_debug("initialize header\n");
  1557. path[i].p_hdr = ext_block_hdr(path[i].p_bh);
  1558. if (ext4_ext_check_header(__FUNCTION__, inode,
  1559. path[i].p_hdr)) {
  1560. err = -EIO;
  1561. goto out;
  1562. }
  1563. }
  1564. BUG_ON(le16_to_cpu(path[i].p_hdr->eh_entries)
  1565. > le16_to_cpu(path[i].p_hdr->eh_max));
  1566. BUG_ON(path[i].p_hdr->eh_magic != EXT4_EXT_MAGIC);
  1567. if (!path[i].p_idx) {
  1568. /* this level hasn't been touched yet */
  1569. path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
  1570. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
  1571. ext_debug("init index ptr: hdr 0x%p, num %d\n",
  1572. path[i].p_hdr,
  1573. le16_to_cpu(path[i].p_hdr->eh_entries));
  1574. } else {
  1575. /* we were already here, see at next index */
  1576. path[i].p_idx--;
  1577. }
  1578. ext_debug("level %d - index, first 0x%p, cur 0x%p\n",
  1579. i, EXT_FIRST_INDEX(path[i].p_hdr),
  1580. path[i].p_idx);
  1581. if (ext4_ext_more_to_rm(path + i)) {
  1582. /* go to the next level */
  1583. ext_debug("move to level %d (block %llu)\n",
  1584. i + 1, idx_pblock(path[i].p_idx));
  1585. memset(path + i + 1, 0, sizeof(*path));
  1586. path[i+1].p_bh =
  1587. sb_bread(sb, idx_pblock(path[i].p_idx));
  1588. if (!path[i+1].p_bh) {
  1589. /* should we reset i_size? */
  1590. err = -EIO;
  1591. break;
  1592. }
  1593. /* save actual number of indexes since this
  1594. * number is changed at the next iteration */
  1595. path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
  1596. i++;
  1597. } else {
  1598. /* we finished processing this index, go up */
  1599. if (path[i].p_hdr->eh_entries == 0 && i > 0) {
  1600. /* index is empty, remove it;
  1601. * handle must be already prepared by the
  1602. * truncatei_leaf() */
  1603. err = ext4_ext_rm_idx(handle, inode, path + i);
  1604. }
  1605. /* root level has p_bh == NULL, brelse() eats this */
  1606. brelse(path[i].p_bh);
  1607. path[i].p_bh = NULL;
  1608. i--;
  1609. ext_debug("return to level %d\n", i);
  1610. }
  1611. }
  1612. /* TODO: flexible tree reduction should be here */
  1613. if (path->p_hdr->eh_entries == 0) {
  1614. /*
  1615. * truncate to zero freed all the tree,
  1616. * so we need to correct eh_depth
  1617. */
  1618. err = ext4_ext_get_access(handle, inode, path);
  1619. if (err == 0) {
  1620. ext_inode_hdr(inode)->eh_depth = 0;
  1621. ext_inode_hdr(inode)->eh_max =
  1622. cpu_to_le16(ext4_ext_space_root(inode));
  1623. err = ext4_ext_dirty(handle, inode, path);
  1624. }
  1625. }
  1626. out:
  1627. ext4_ext_tree_changed(inode);
  1628. ext4_ext_drop_refs(path);
  1629. kfree(path);
  1630. ext4_journal_stop(handle);
  1631. return err;
  1632. }
  1633. /*
  1634. * called at mount time
  1635. */
  1636. void ext4_ext_init(struct super_block *sb)
  1637. {
  1638. /*
  1639. * possible initialization would be here
  1640. */
  1641. if (test_opt(sb, EXTENTS)) {
  1642. printk("EXT4-fs: file extents enabled");
  1643. #ifdef AGRESSIVE_TEST
  1644. printk(", agressive tests");
  1645. #endif
  1646. #ifdef CHECK_BINSEARCH
  1647. printk(", check binsearch");
  1648. #endif
  1649. #ifdef EXTENTS_STATS
  1650. printk(", stats");
  1651. #endif
  1652. printk("\n");
  1653. #ifdef EXTENTS_STATS
  1654. spin_lock_init(&EXT4_SB(sb)->s_ext_stats_lock);
  1655. EXT4_SB(sb)->s_ext_min = 1 << 30;
  1656. EXT4_SB(sb)->s_ext_max = 0;
  1657. #endif
  1658. }
  1659. }
  1660. /*
  1661. * called at umount time
  1662. */
  1663. void ext4_ext_release(struct super_block *sb)
  1664. {
  1665. if (!test_opt(sb, EXTENTS))
  1666. return;
  1667. #ifdef EXTENTS_STATS
  1668. if (EXT4_SB(sb)->s_ext_blocks && EXT4_SB(sb)->s_ext_extents) {
  1669. struct ext4_sb_info *sbi = EXT4_SB(sb);
  1670. printk(KERN_ERR "EXT4-fs: %lu blocks in %lu extents (%lu ave)\n",
  1671. sbi->s_ext_blocks, sbi->s_ext_extents,
  1672. sbi->s_ext_blocks / sbi->s_ext_extents);
  1673. printk(KERN_ERR "EXT4-fs: extents: %lu min, %lu max, max depth %lu\n",
  1674. sbi->s_ext_min, sbi->s_ext_max, sbi->s_depth_max);
  1675. }
  1676. #endif
  1677. }
  1678. int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
  1679. ext4_fsblk_t iblock,
  1680. unsigned long max_blocks, struct buffer_head *bh_result,
  1681. int create, int extend_disksize)
  1682. {
  1683. struct ext4_ext_path *path = NULL;
  1684. struct ext4_extent newex, *ex;
  1685. ext4_fsblk_t goal, newblock;
  1686. int err = 0, depth;
  1687. unsigned long allocated = 0;
  1688. __clear_bit(BH_New, &bh_result->b_state);
  1689. ext_debug("blocks %d/%lu requested for inode %u\n", (int) iblock,
  1690. max_blocks, (unsigned) inode->i_ino);
  1691. mutex_lock(&EXT4_I(inode)->truncate_mutex);
  1692. /* check in cache */
  1693. if ((goal = ext4_ext_in_cache(inode, iblock, &newex))) {
  1694. if (goal == EXT4_EXT_CACHE_GAP) {
  1695. if (!create) {
  1696. /* block isn't allocated yet and
  1697. * user doesn't want to allocate it */
  1698. goto out2;
  1699. }
  1700. /* we should allocate requested block */
  1701. } else if (goal == EXT4_EXT_CACHE_EXTENT) {
  1702. /* block is already allocated */
  1703. newblock = iblock
  1704. - le32_to_cpu(newex.ee_block)
  1705. + ext_pblock(&newex);
  1706. /* number of remaining blocks in the extent */
  1707. allocated = le16_to_cpu(newex.ee_len) -
  1708. (iblock - le32_to_cpu(newex.ee_block));
  1709. goto out;
  1710. } else {
  1711. BUG();
  1712. }
  1713. }
  1714. /* find extent for this block */
  1715. path = ext4_ext_find_extent(inode, iblock, NULL);
  1716. if (IS_ERR(path)) {
  1717. err = PTR_ERR(path);
  1718. path = NULL;
  1719. goto out2;
  1720. }
  1721. depth = ext_depth(inode);
  1722. /*
  1723. * consistent leaf must not be empty;
  1724. * this situation is possible, though, _during_ tree modification;
  1725. * this is why assert can't be put in ext4_ext_find_extent()
  1726. */
  1727. BUG_ON(path[depth].p_ext == NULL && depth != 0);
  1728. if ((ex = path[depth].p_ext)) {
  1729. unsigned long ee_block = le32_to_cpu(ex->ee_block);
  1730. ext4_fsblk_t ee_start = ext_pblock(ex);
  1731. unsigned short ee_len = le16_to_cpu(ex->ee_len);
  1732. /*
  1733. * Allow future support for preallocated extents to be added
  1734. * as an RO_COMPAT feature:
  1735. * Uninitialized extents are treated as holes, except that
  1736. * we avoid (fail) allocating new blocks during a write.
  1737. */
  1738. if (ee_len > EXT_MAX_LEN)
  1739. goto out2;
  1740. /* if found extent covers block, simply return it */
  1741. if (iblock >= ee_block && iblock < ee_block + ee_len) {
  1742. newblock = iblock - ee_block + ee_start;
  1743. /* number of remaining blocks in the extent */
  1744. allocated = ee_len - (iblock - ee_block);
  1745. ext_debug("%d fit into %lu:%d -> %llu\n", (int) iblock,
  1746. ee_block, ee_len, newblock);
  1747. ext4_ext_put_in_cache(inode, ee_block, ee_len,
  1748. ee_start, EXT4_EXT_CACHE_EXTENT);
  1749. goto out;
  1750. }
  1751. }
  1752. /*
  1753. * requested block isn't allocated yet;
  1754. * we couldn't try to create block if create flag is zero
  1755. */
  1756. if (!create) {
  1757. /* put just found gap into cache to speed up
  1758. * subsequent requests */
  1759. ext4_ext_put_gap_in_cache(inode, path, iblock);
  1760. goto out2;
  1761. }
  1762. /*
  1763. * Okay, we need to do block allocation. Lazily initialize the block
  1764. * allocation info here if necessary.
  1765. */
  1766. if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
  1767. ext4_init_block_alloc_info(inode);
  1768. /* allocate new block */
  1769. goal = ext4_ext_find_goal(inode, path, iblock);
  1770. allocated = max_blocks;
  1771. newblock = ext4_new_blocks(handle, inode, goal, &allocated, &err);
  1772. if (!newblock)
  1773. goto out2;
  1774. ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
  1775. goal, newblock, allocated);
  1776. /* try to insert new extent into found leaf and return */
  1777. newex.ee_block = cpu_to_le32(iblock);
  1778. ext4_ext_store_pblock(&newex, newblock);
  1779. newex.ee_len = cpu_to_le16(allocated);
  1780. err = ext4_ext_insert_extent(handle, inode, path, &newex);
  1781. if (err)
  1782. goto out2;
  1783. if (extend_disksize && inode->i_size > EXT4_I(inode)->i_disksize)
  1784. EXT4_I(inode)->i_disksize = inode->i_size;
  1785. /* previous routine could use block we allocated */
  1786. newblock = ext_pblock(&newex);
  1787. __set_bit(BH_New, &bh_result->b_state);
  1788. ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
  1789. EXT4_EXT_CACHE_EXTENT);
  1790. out:
  1791. if (allocated > max_blocks)
  1792. allocated = max_blocks;
  1793. ext4_ext_show_leaf(inode, path);
  1794. __set_bit(BH_Mapped, &bh_result->b_state);
  1795. bh_result->b_bdev = inode->i_sb->s_bdev;
  1796. bh_result->b_blocknr = newblock;
  1797. out2:
  1798. if (path) {
  1799. ext4_ext_drop_refs(path);
  1800. kfree(path);
  1801. }
  1802. mutex_unlock(&EXT4_I(inode)->truncate_mutex);
  1803. return err ? err : allocated;
  1804. }
  1805. void ext4_ext_truncate(struct inode * inode, struct page *page)
  1806. {
  1807. struct address_space *mapping = inode->i_mapping;
  1808. struct super_block *sb = inode->i_sb;
  1809. unsigned long last_block;
  1810. handle_t *handle;
  1811. int err = 0;
  1812. /*
  1813. * probably first extent we're gonna free will be last in block
  1814. */
  1815. err = ext4_writepage_trans_blocks(inode) + 3;
  1816. handle = ext4_journal_start(inode, err);
  1817. if (IS_ERR(handle)) {
  1818. if (page) {
  1819. clear_highpage(page);
  1820. flush_dcache_page(page);
  1821. unlock_page(page);
  1822. page_cache_release(page);
  1823. }
  1824. return;
  1825. }
  1826. if (page)
  1827. ext4_block_truncate_page(handle, page, mapping, inode->i_size);
  1828. mutex_lock(&EXT4_I(inode)->truncate_mutex);
  1829. ext4_ext_invalidate_cache(inode);
  1830. /*
  1831. * TODO: optimization is possible here.
  1832. * Probably we need not scan at all,
  1833. * because page truncation is enough.
  1834. */
  1835. if (ext4_orphan_add(handle, inode))
  1836. goto out_stop;
  1837. /* we have to know where to truncate from in crash case */
  1838. EXT4_I(inode)->i_disksize = inode->i_size;
  1839. ext4_mark_inode_dirty(handle, inode);
  1840. last_block = (inode->i_size + sb->s_blocksize - 1)
  1841. >> EXT4_BLOCK_SIZE_BITS(sb);
  1842. err = ext4_ext_remove_space(inode, last_block);
  1843. /* In a multi-transaction truncate, we only make the final
  1844. * transaction synchronous. */
  1845. if (IS_SYNC(inode))
  1846. handle->h_sync = 1;
  1847. out_stop:
  1848. /*
  1849. * If this was a simple ftruncate() and the file will remain alive,
  1850. * then we need to clear up the orphan record which we created above.
  1851. * However, if this was a real unlink then we were called by
  1852. * ext4_delete_inode(), and we allow that function to clean up the
  1853. * orphan info for us.
  1854. */
  1855. if (inode->i_nlink)
  1856. ext4_orphan_del(handle, inode);
  1857. mutex_unlock(&EXT4_I(inode)->truncate_mutex);
  1858. ext4_journal_stop(handle);
  1859. }
  1860. /*
  1861. * ext4_ext_writepage_trans_blocks:
  1862. * calculate max number of blocks we could modify
  1863. * in order to allocate new block for an inode
  1864. */
  1865. int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
  1866. {
  1867. int needed;
  1868. needed = ext4_ext_calc_credits_for_insert(inode, NULL);
  1869. /* caller wants to allocate num blocks, but note it includes sb */
  1870. needed = needed * num - (num - 1);
  1871. #ifdef CONFIG_QUOTA
  1872. needed += 2 * EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
  1873. #endif
  1874. return needed;
  1875. }
  1876. EXPORT_SYMBOL(ext4_mark_inode_dirty);
  1877. EXPORT_SYMBOL(ext4_ext_invalidate_cache);
  1878. EXPORT_SYMBOL(ext4_ext_insert_extent);
  1879. EXPORT_SYMBOL(ext4_ext_walk_space);
  1880. EXPORT_SYMBOL(ext4_ext_find_goal);
  1881. EXPORT_SYMBOL(ext4_ext_calc_credits_for_insert);