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