extents.c 55 KB

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