Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2 | * linux/fs/ext4/namei.c |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 1992, 1993, 1994, 1995 |
| 5 | * Remy Card (card@masi.ibp.fr) |
| 6 | * Laboratoire MASI - Institut Blaise Pascal |
| 7 | * Universite Pierre et Marie Curie (Paris VI) |
| 8 | * |
| 9 | * from |
| 10 | * |
| 11 | * linux/fs/minix/namei.c |
| 12 | * |
| 13 | * Copyright (C) 1991, 1992 Linus Torvalds |
| 14 | * |
| 15 | * Big-endian to little-endian byte-swapping/bitmaps by |
| 16 | * David S. Miller (davem@caip.rutgers.edu), 1995 |
| 17 | * Directory entry file type support and forward compatibility hooks |
| 18 | * for B-tree directories by Theodore Ts'o (tytso@mit.edu), 1998 |
| 19 | * Hash Tree Directory indexing (c) |
| 20 | * Daniel Phillips, 2001 |
| 21 | * Hash Tree Directory indexing porting |
| 22 | * Christopher Li, 2002 |
| 23 | * Hash Tree Directory indexing cleanup |
| 24 | * Theodore Ts'o, 2002 |
| 25 | */ |
| 26 | |
| 27 | #include <linux/fs.h> |
| 28 | #include <linux/pagemap.h> |
Mingming Cao | dab291a | 2006-10-11 01:21:01 -0700 | [diff] [blame] | 29 | #include <linux/jbd2.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 30 | #include <linux/time.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 31 | #include <linux/fcntl.h> |
| 32 | #include <linux/stat.h> |
| 33 | #include <linux/string.h> |
| 34 | #include <linux/quotaops.h> |
| 35 | #include <linux/buffer_head.h> |
| 36 | #include <linux/bio.h> |
Christoph Hellwig | 3dcf545 | 2008-04-29 18:13:32 -0400 | [diff] [blame] | 37 | #include "ext4.h" |
| 38 | #include "ext4_jbd2.h" |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 39 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 40 | #include "xattr.h" |
| 41 | #include "acl.h" |
| 42 | |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 43 | #include <trace/events/ext4.h> |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 44 | /* |
| 45 | * define how far ahead to read directories while searching them. |
| 46 | */ |
| 47 | #define NAMEI_RA_CHUNKS 2 |
| 48 | #define NAMEI_RA_BLOCKS 4 |
Dave Kleikamp | 8c55e20 | 2007-05-24 13:04:54 -0400 | [diff] [blame] | 49 | #define NAMEI_RA_SIZE (NAMEI_RA_CHUNKS * NAMEI_RA_BLOCKS) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 50 | #define NAMEI_RA_INDEX(c,b) (((c) * NAMEI_RA_BLOCKS) + (b)) |
| 51 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 52 | static struct buffer_head *ext4_append(handle_t *handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 53 | struct inode *inode, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 54 | ext4_lblk_t *block, int *err) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 55 | { |
| 56 | struct buffer_head *bh; |
| 57 | |
Theodore Ts'o | df981d0 | 2012-08-17 09:48:17 -0400 | [diff] [blame] | 58 | if (unlikely(EXT4_SB(inode->i_sb)->s_max_dir_size_kb && |
| 59 | ((inode->i_size >> 10) >= |
| 60 | EXT4_SB(inode->i_sb)->s_max_dir_size_kb))) { |
| 61 | *err = -ENOSPC; |
| 62 | return NULL; |
| 63 | } |
| 64 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 65 | *block = inode->i_size >> inode->i_sb->s_blocksize_bits; |
| 66 | |
Akinobu Mita | a871611 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 67 | bh = ext4_bread(handle, inode, *block, 1, err); |
| 68 | if (bh) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 69 | inode->i_size += inode->i_sb->s_blocksize; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 70 | EXT4_I(inode)->i_disksize = inode->i_size; |
Akinobu Mita | a871611 | 2008-04-17 10:38:59 -0400 | [diff] [blame] | 71 | *err = ext4_journal_get_write_access(handle, bh); |
| 72 | if (*err) { |
| 73 | brelse(bh); |
| 74 | bh = NULL; |
| 75 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 76 | } |
Carlos Maiolino | 6d1ab10 | 2012-09-27 09:31:33 -0400 | [diff] [blame] | 77 | if (!bh && !(*err)) { |
| 78 | *err = -EIO; |
| 79 | ext4_error(inode->i_sb, |
| 80 | "Directory hole detected on inode %lu\n", |
| 81 | inode->i_ino); |
| 82 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 83 | return bh; |
| 84 | } |
| 85 | |
| 86 | #ifndef assert |
| 87 | #define assert(test) J_ASSERT(test) |
| 88 | #endif |
| 89 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 90 | #ifdef DX_DEBUG |
| 91 | #define dxtrace(command) command |
| 92 | #else |
| 93 | #define dxtrace(command) |
| 94 | #endif |
| 95 | |
| 96 | struct fake_dirent |
| 97 | { |
| 98 | __le32 inode; |
| 99 | __le16 rec_len; |
| 100 | u8 name_len; |
| 101 | u8 file_type; |
| 102 | }; |
| 103 | |
| 104 | struct dx_countlimit |
| 105 | { |
| 106 | __le16 limit; |
| 107 | __le16 count; |
| 108 | }; |
| 109 | |
| 110 | struct dx_entry |
| 111 | { |
| 112 | __le32 hash; |
| 113 | __le32 block; |
| 114 | }; |
| 115 | |
| 116 | /* |
| 117 | * dx_root_info is laid out so that if it should somehow get overlaid by a |
| 118 | * dirent the two low bits of the hash version will be zero. Therefore, the |
| 119 | * hash version mod 4 should never be 0. Sincerely, the paranoia department. |
| 120 | */ |
| 121 | |
| 122 | struct dx_root |
| 123 | { |
| 124 | struct fake_dirent dot; |
| 125 | char dot_name[4]; |
| 126 | struct fake_dirent dotdot; |
| 127 | char dotdot_name[4]; |
| 128 | struct dx_root_info |
| 129 | { |
| 130 | __le32 reserved_zero; |
| 131 | u8 hash_version; |
| 132 | u8 info_length; /* 8 */ |
| 133 | u8 indirect_levels; |
| 134 | u8 unused_flags; |
| 135 | } |
| 136 | info; |
| 137 | struct dx_entry entries[0]; |
| 138 | }; |
| 139 | |
| 140 | struct dx_node |
| 141 | { |
| 142 | struct fake_dirent fake; |
| 143 | struct dx_entry entries[0]; |
| 144 | }; |
| 145 | |
| 146 | |
| 147 | struct dx_frame |
| 148 | { |
| 149 | struct buffer_head *bh; |
| 150 | struct dx_entry *entries; |
| 151 | struct dx_entry *at; |
| 152 | }; |
| 153 | |
| 154 | struct dx_map_entry |
| 155 | { |
| 156 | u32 hash; |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 157 | u16 offs; |
| 158 | u16 size; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 159 | }; |
| 160 | |
Darrick J. Wong | e615391 | 2012-04-29 18:23:10 -0400 | [diff] [blame] | 161 | /* |
| 162 | * This goes at the end of each htree block. |
| 163 | */ |
| 164 | struct dx_tail { |
| 165 | u32 dt_reserved; |
| 166 | __le32 dt_checksum; /* crc32c(uuid+inum+dirblock) */ |
| 167 | }; |
| 168 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 169 | static inline ext4_lblk_t dx_get_block(struct dx_entry *entry); |
| 170 | static void dx_set_block(struct dx_entry *entry, ext4_lblk_t value); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 171 | static inline unsigned dx_get_hash(struct dx_entry *entry); |
| 172 | static void dx_set_hash(struct dx_entry *entry, unsigned value); |
| 173 | static unsigned dx_get_count(struct dx_entry *entries); |
| 174 | static unsigned dx_get_limit(struct dx_entry *entries); |
| 175 | static void dx_set_count(struct dx_entry *entries, unsigned value); |
| 176 | static void dx_set_limit(struct dx_entry *entries, unsigned value); |
| 177 | static unsigned dx_root_limit(struct inode *dir, unsigned infosize); |
| 178 | static unsigned dx_node_limit(struct inode *dir); |
Theodore Ts'o | f702ba0 | 2008-09-22 15:21:01 -0400 | [diff] [blame] | 179 | static struct dx_frame *dx_probe(const struct qstr *d_name, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 180 | struct inode *dir, |
| 181 | struct dx_hash_info *hinfo, |
| 182 | struct dx_frame *frame, |
| 183 | int *err); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 184 | static void dx_release(struct dx_frame *frames); |
Theodore Ts'o | 8bad459 | 2009-02-14 21:46:54 -0500 | [diff] [blame] | 185 | static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize, |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 186 | struct dx_hash_info *hinfo, struct dx_map_entry map[]); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 187 | static void dx_sort_map(struct dx_map_entry *map, unsigned count); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 188 | static struct ext4_dir_entry_2 *dx_move_dirents(char *from, char *to, |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 189 | struct dx_map_entry *offsets, int count, unsigned blocksize); |
Theodore Ts'o | 8bad459 | 2009-02-14 21:46:54 -0500 | [diff] [blame] | 190 | static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize); |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 191 | static void dx_insert_block(struct dx_frame *frame, |
| 192 | u32 hash, ext4_lblk_t block); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 193 | static int ext4_htree_next_block(struct inode *dir, __u32 hash, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 194 | struct dx_frame *frame, |
| 195 | struct dx_frame *frames, |
| 196 | __u32 *start_hash); |
Theodore Ts'o | f702ba0 | 2008-09-22 15:21:01 -0400 | [diff] [blame] | 197 | static struct buffer_head * ext4_dx_find_entry(struct inode *dir, |
| 198 | const struct qstr *d_name, |
| 199 | struct ext4_dir_entry_2 **res_dir, |
| 200 | int *err); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 201 | static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 202 | struct inode *inode); |
| 203 | |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 204 | /* checksumming functions */ |
Tao Ma | 3c47d54 | 2012-12-10 14:05:59 -0500 | [diff] [blame] | 205 | void initialize_dirent_tail(struct ext4_dir_entry_tail *t, |
| 206 | unsigned int blocksize) |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 207 | { |
| 208 | memset(t, 0, sizeof(struct ext4_dir_entry_tail)); |
| 209 | t->det_rec_len = ext4_rec_len_to_disk( |
| 210 | sizeof(struct ext4_dir_entry_tail), blocksize); |
| 211 | t->det_reserved_ft = EXT4_FT_DIR_CSUM; |
| 212 | } |
| 213 | |
| 214 | /* Walk through a dirent block to find a checksum "dirent" at the tail */ |
| 215 | static struct ext4_dir_entry_tail *get_dirent_tail(struct inode *inode, |
| 216 | struct ext4_dir_entry *de) |
| 217 | { |
| 218 | struct ext4_dir_entry_tail *t; |
| 219 | |
| 220 | #ifdef PARANOID |
| 221 | struct ext4_dir_entry *d, *top; |
| 222 | |
| 223 | d = de; |
| 224 | top = (struct ext4_dir_entry *)(((void *)de) + |
| 225 | (EXT4_BLOCK_SIZE(inode->i_sb) - |
| 226 | sizeof(struct ext4_dir_entry_tail))); |
| 227 | while (d < top && d->rec_len) |
| 228 | d = (struct ext4_dir_entry *)(((void *)d) + |
| 229 | le16_to_cpu(d->rec_len)); |
| 230 | |
| 231 | if (d != top) |
| 232 | return NULL; |
| 233 | |
| 234 | t = (struct ext4_dir_entry_tail *)d; |
| 235 | #else |
| 236 | t = EXT4_DIRENT_TAIL(de, EXT4_BLOCK_SIZE(inode->i_sb)); |
| 237 | #endif |
| 238 | |
| 239 | if (t->det_reserved_zero1 || |
| 240 | le16_to_cpu(t->det_rec_len) != sizeof(struct ext4_dir_entry_tail) || |
| 241 | t->det_reserved_zero2 || |
| 242 | t->det_reserved_ft != EXT4_FT_DIR_CSUM) |
| 243 | return NULL; |
| 244 | |
| 245 | return t; |
| 246 | } |
| 247 | |
| 248 | static __le32 ext4_dirent_csum(struct inode *inode, |
| 249 | struct ext4_dir_entry *dirent, int size) |
| 250 | { |
| 251 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 252 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 253 | __u32 csum; |
| 254 | |
| 255 | csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size); |
| 256 | return cpu_to_le32(csum); |
| 257 | } |
| 258 | |
Theodore Ts'o | dffe9d8 | 2012-11-10 22:20:05 -0500 | [diff] [blame] | 259 | static void warn_no_space_for_csum(struct inode *inode) |
| 260 | { |
| 261 | ext4_warning(inode->i_sb, "no space in directory inode %lu leaf for " |
| 262 | "checksum. Please run e2fsck -D.", inode->i_ino); |
| 263 | } |
| 264 | |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 265 | int ext4_dirent_csum_verify(struct inode *inode, struct ext4_dir_entry *dirent) |
| 266 | { |
| 267 | struct ext4_dir_entry_tail *t; |
| 268 | |
| 269 | if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, |
| 270 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) |
| 271 | return 1; |
| 272 | |
| 273 | t = get_dirent_tail(inode, dirent); |
| 274 | if (!t) { |
Theodore Ts'o | dffe9d8 | 2012-11-10 22:20:05 -0500 | [diff] [blame] | 275 | warn_no_space_for_csum(inode); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | if (t->det_checksum != ext4_dirent_csum(inode, dirent, |
| 280 | (void *)t - (void *)dirent)) |
| 281 | return 0; |
| 282 | |
| 283 | return 1; |
| 284 | } |
| 285 | |
| 286 | static void ext4_dirent_csum_set(struct inode *inode, |
| 287 | struct ext4_dir_entry *dirent) |
| 288 | { |
| 289 | struct ext4_dir_entry_tail *t; |
| 290 | |
| 291 | if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, |
| 292 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) |
| 293 | return; |
| 294 | |
| 295 | t = get_dirent_tail(inode, dirent); |
| 296 | if (!t) { |
Theodore Ts'o | dffe9d8 | 2012-11-10 22:20:05 -0500 | [diff] [blame] | 297 | warn_no_space_for_csum(inode); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 298 | return; |
| 299 | } |
| 300 | |
| 301 | t->det_checksum = ext4_dirent_csum(inode, dirent, |
| 302 | (void *)t - (void *)dirent); |
| 303 | } |
| 304 | |
Tao Ma | 3c47d54 | 2012-12-10 14:05:59 -0500 | [diff] [blame] | 305 | int ext4_handle_dirty_dirent_node(handle_t *handle, |
| 306 | struct inode *inode, |
| 307 | struct buffer_head *bh) |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 308 | { |
| 309 | ext4_dirent_csum_set(inode, (struct ext4_dir_entry *)bh->b_data); |
| 310 | return ext4_handle_dirty_metadata(handle, inode, bh); |
| 311 | } |
| 312 | |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 313 | static struct dx_countlimit *get_dx_countlimit(struct inode *inode, |
| 314 | struct ext4_dir_entry *dirent, |
| 315 | int *offset) |
| 316 | { |
| 317 | struct ext4_dir_entry *dp; |
| 318 | struct dx_root_info *root; |
| 319 | int count_offset; |
| 320 | |
| 321 | if (le16_to_cpu(dirent->rec_len) == EXT4_BLOCK_SIZE(inode->i_sb)) |
| 322 | count_offset = 8; |
| 323 | else if (le16_to_cpu(dirent->rec_len) == 12) { |
| 324 | dp = (struct ext4_dir_entry *)(((void *)dirent) + 12); |
| 325 | if (le16_to_cpu(dp->rec_len) != |
| 326 | EXT4_BLOCK_SIZE(inode->i_sb) - 12) |
| 327 | return NULL; |
| 328 | root = (struct dx_root_info *)(((void *)dp + 12)); |
| 329 | if (root->reserved_zero || |
| 330 | root->info_length != sizeof(struct dx_root_info)) |
| 331 | return NULL; |
| 332 | count_offset = 32; |
| 333 | } else |
| 334 | return NULL; |
| 335 | |
| 336 | if (offset) |
| 337 | *offset = count_offset; |
| 338 | return (struct dx_countlimit *)(((void *)dirent) + count_offset); |
| 339 | } |
| 340 | |
| 341 | static __le32 ext4_dx_csum(struct inode *inode, struct ext4_dir_entry *dirent, |
| 342 | int count_offset, int count, struct dx_tail *t) |
| 343 | { |
| 344 | struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); |
| 345 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 346 | __u32 csum, old_csum; |
| 347 | int size; |
| 348 | |
| 349 | size = count_offset + (count * sizeof(struct dx_entry)); |
| 350 | old_csum = t->dt_checksum; |
| 351 | t->dt_checksum = 0; |
| 352 | csum = ext4_chksum(sbi, ei->i_csum_seed, (__u8 *)dirent, size); |
| 353 | csum = ext4_chksum(sbi, csum, (__u8 *)t, sizeof(struct dx_tail)); |
| 354 | t->dt_checksum = old_csum; |
| 355 | |
| 356 | return cpu_to_le32(csum); |
| 357 | } |
| 358 | |
| 359 | static int ext4_dx_csum_verify(struct inode *inode, |
| 360 | struct ext4_dir_entry *dirent) |
| 361 | { |
| 362 | struct dx_countlimit *c; |
| 363 | struct dx_tail *t; |
| 364 | int count_offset, limit, count; |
| 365 | |
| 366 | if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, |
| 367 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) |
| 368 | return 1; |
| 369 | |
| 370 | c = get_dx_countlimit(inode, dirent, &count_offset); |
| 371 | if (!c) { |
| 372 | EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D."); |
| 373 | return 1; |
| 374 | } |
| 375 | limit = le16_to_cpu(c->limit); |
| 376 | count = le16_to_cpu(c->count); |
| 377 | if (count_offset + (limit * sizeof(struct dx_entry)) > |
| 378 | EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) { |
Theodore Ts'o | dffe9d8 | 2012-11-10 22:20:05 -0500 | [diff] [blame] | 379 | warn_no_space_for_csum(inode); |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 380 | return 1; |
| 381 | } |
| 382 | t = (struct dx_tail *)(((struct dx_entry *)c) + limit); |
| 383 | |
| 384 | if (t->dt_checksum != ext4_dx_csum(inode, dirent, count_offset, |
| 385 | count, t)) |
| 386 | return 0; |
| 387 | return 1; |
| 388 | } |
| 389 | |
| 390 | static void ext4_dx_csum_set(struct inode *inode, struct ext4_dir_entry *dirent) |
| 391 | { |
| 392 | struct dx_countlimit *c; |
| 393 | struct dx_tail *t; |
| 394 | int count_offset, limit, count; |
| 395 | |
| 396 | if (!EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, |
| 397 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) |
| 398 | return; |
| 399 | |
| 400 | c = get_dx_countlimit(inode, dirent, &count_offset); |
| 401 | if (!c) { |
| 402 | EXT4_ERROR_INODE(inode, "dir seems corrupt? Run e2fsck -D."); |
| 403 | return; |
| 404 | } |
| 405 | limit = le16_to_cpu(c->limit); |
| 406 | count = le16_to_cpu(c->count); |
| 407 | if (count_offset + (limit * sizeof(struct dx_entry)) > |
| 408 | EXT4_BLOCK_SIZE(inode->i_sb) - sizeof(struct dx_tail)) { |
Theodore Ts'o | dffe9d8 | 2012-11-10 22:20:05 -0500 | [diff] [blame] | 409 | warn_no_space_for_csum(inode); |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 410 | return; |
| 411 | } |
| 412 | t = (struct dx_tail *)(((struct dx_entry *)c) + limit); |
| 413 | |
| 414 | t->dt_checksum = ext4_dx_csum(inode, dirent, count_offset, count, t); |
| 415 | } |
| 416 | |
| 417 | static inline int ext4_handle_dirty_dx_node(handle_t *handle, |
| 418 | struct inode *inode, |
| 419 | struct buffer_head *bh) |
| 420 | { |
| 421 | ext4_dx_csum_set(inode, (struct ext4_dir_entry *)bh->b_data); |
| 422 | return ext4_handle_dirty_metadata(handle, inode, bh); |
| 423 | } |
| 424 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 425 | /* |
Li Zefan | f795e14 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 426 | * p is at least 6 bytes before the end of page |
| 427 | */ |
| 428 | static inline struct ext4_dir_entry_2 * |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 429 | ext4_next_entry(struct ext4_dir_entry_2 *p, unsigned long blocksize) |
Li Zefan | f795e14 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 430 | { |
| 431 | return (struct ext4_dir_entry_2 *)((char *)p + |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 432 | ext4_rec_len_from_disk(p->rec_len, blocksize)); |
Li Zefan | f795e14 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | /* |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 436 | * Future: use high four bits of block for coalesce-on-delete flags |
| 437 | * Mask them off for now. |
| 438 | */ |
| 439 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 440 | static inline ext4_lblk_t dx_get_block(struct dx_entry *entry) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 441 | { |
| 442 | return le32_to_cpu(entry->block) & 0x00ffffff; |
| 443 | } |
| 444 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 445 | static inline void dx_set_block(struct dx_entry *entry, ext4_lblk_t value) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 446 | { |
| 447 | entry->block = cpu_to_le32(value); |
| 448 | } |
| 449 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 450 | static inline unsigned dx_get_hash(struct dx_entry *entry) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 451 | { |
| 452 | return le32_to_cpu(entry->hash); |
| 453 | } |
| 454 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 455 | static inline void dx_set_hash(struct dx_entry *entry, unsigned value) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 456 | { |
| 457 | entry->hash = cpu_to_le32(value); |
| 458 | } |
| 459 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 460 | static inline unsigned dx_get_count(struct dx_entry *entries) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 461 | { |
| 462 | return le16_to_cpu(((struct dx_countlimit *) entries)->count); |
| 463 | } |
| 464 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 465 | static inline unsigned dx_get_limit(struct dx_entry *entries) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 466 | { |
| 467 | return le16_to_cpu(((struct dx_countlimit *) entries)->limit); |
| 468 | } |
| 469 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 470 | static inline void dx_set_count(struct dx_entry *entries, unsigned value) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 471 | { |
| 472 | ((struct dx_countlimit *) entries)->count = cpu_to_le16(value); |
| 473 | } |
| 474 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 475 | static inline void dx_set_limit(struct dx_entry *entries, unsigned value) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 476 | { |
| 477 | ((struct dx_countlimit *) entries)->limit = cpu_to_le16(value); |
| 478 | } |
| 479 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 480 | static inline unsigned dx_root_limit(struct inode *dir, unsigned infosize) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 481 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 482 | unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(1) - |
| 483 | EXT4_DIR_REC_LEN(2) - infosize; |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 484 | |
| 485 | if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb, |
| 486 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) |
| 487 | entry_space -= sizeof(struct dx_tail); |
Li Zefan | d9c769b | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 488 | return entry_space / sizeof(struct dx_entry); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 491 | static inline unsigned dx_node_limit(struct inode *dir) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 492 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 493 | unsigned entry_space = dir->i_sb->s_blocksize - EXT4_DIR_REC_LEN(0); |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 494 | |
| 495 | if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb, |
| 496 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) |
| 497 | entry_space -= sizeof(struct dx_tail); |
Li Zefan | d9c769b | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 498 | return entry_space / sizeof(struct dx_entry); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | /* |
| 502 | * Debug |
| 503 | */ |
| 504 | #ifdef DX_DEBUG |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 505 | static void dx_show_index(char * label, struct dx_entry *entries) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 506 | { |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 507 | int i, n = dx_get_count (entries); |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 508 | printk(KERN_DEBUG "%s index ", label); |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 509 | for (i = 0; i < n; i++) { |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 510 | printk("%x->%lu ", i ? dx_get_hash(entries + i) : |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 511 | 0, (unsigned long)dx_get_block(entries + i)); |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 512 | } |
| 513 | printk("\n"); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | struct stats |
| 517 | { |
| 518 | unsigned names; |
| 519 | unsigned space; |
| 520 | unsigned bcount; |
| 521 | }; |
| 522 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 523 | static struct stats dx_show_leaf(struct dx_hash_info *hinfo, struct ext4_dir_entry_2 *de, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 524 | int size, int show_names) |
| 525 | { |
| 526 | unsigned names = 0, space = 0; |
| 527 | char *base = (char *) de; |
| 528 | struct dx_hash_info h = *hinfo; |
| 529 | |
| 530 | printk("names: "); |
| 531 | while ((char *) de < base + size) |
| 532 | { |
| 533 | if (de->inode) |
| 534 | { |
| 535 | if (show_names) |
| 536 | { |
| 537 | int len = de->name_len; |
| 538 | char *name = de->name; |
| 539 | while (len--) printk("%c", *name++); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 540 | ext4fs_dirhash(de->name, de->name_len, &h); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 541 | printk(":%x.%u ", h.hash, |
Bernd Schubert | 265c6a0 | 2011-07-16 19:41:23 -0400 | [diff] [blame] | 542 | (unsigned) ((char *) de - base)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 543 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 544 | space += EXT4_DIR_REC_LEN(de->name_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 545 | names++; |
| 546 | } |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 547 | de = ext4_next_entry(de, size); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 548 | } |
| 549 | printk("(%i)\n", names); |
| 550 | return (struct stats) { names, space, 1 }; |
| 551 | } |
| 552 | |
| 553 | struct stats dx_show_entries(struct dx_hash_info *hinfo, struct inode *dir, |
| 554 | struct dx_entry *entries, int levels) |
| 555 | { |
| 556 | unsigned blocksize = dir->i_sb->s_blocksize; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 557 | unsigned count = dx_get_count(entries), names = 0, space = 0, i; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 558 | unsigned bcount = 0; |
| 559 | struct buffer_head *bh; |
| 560 | int err; |
| 561 | printk("%i indexed blocks...\n", count); |
| 562 | for (i = 0; i < count; i++, entries++) |
| 563 | { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 564 | ext4_lblk_t block = dx_get_block(entries); |
| 565 | ext4_lblk_t hash = i ? dx_get_hash(entries): 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 566 | u32 range = i < count - 1? (dx_get_hash(entries + 1) - hash): ~hash; |
| 567 | struct stats stats; |
| 568 | printk("%s%3u:%03u hash %8x/%8x ",levels?"":" ", i, block, hash, range); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 569 | if (!(bh = ext4_bread (NULL,dir, block, 0,&err))) continue; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 570 | stats = levels? |
| 571 | dx_show_entries(hinfo, dir, ((struct dx_node *) bh->b_data)->entries, levels - 1): |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 572 | dx_show_leaf(hinfo, (struct ext4_dir_entry_2 *) bh->b_data, blocksize, 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 573 | names += stats.names; |
| 574 | space += stats.space; |
| 575 | bcount += stats.bcount; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 576 | brelse(bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 577 | } |
| 578 | if (bcount) |
Theodore Ts'o | 60e6679 | 2010-05-17 07:00:00 -0400 | [diff] [blame] | 579 | printk(KERN_DEBUG "%snames %u, fullness %u (%u%%)\n", |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 580 | levels ? "" : " ", names, space/bcount, |
| 581 | (space/bcount)*100/blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 582 | return (struct stats) { names, space, bcount}; |
| 583 | } |
| 584 | #endif /* DX_DEBUG */ |
| 585 | |
| 586 | /* |
| 587 | * Probe for a directory leaf block to search. |
| 588 | * |
| 589 | * dx_probe can return ERR_BAD_DX_DIR, which means there was a format |
| 590 | * error in the directory index, and the caller should fall back to |
| 591 | * searching the directory normally. The callers of dx_probe **MUST** |
| 592 | * check for this error code, and make sure it never gets reflected |
| 593 | * back to userspace. |
| 594 | */ |
| 595 | static struct dx_frame * |
Theodore Ts'o | f702ba0 | 2008-09-22 15:21:01 -0400 | [diff] [blame] | 596 | dx_probe(const struct qstr *d_name, struct inode *dir, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 597 | struct dx_hash_info *hinfo, struct dx_frame *frame_in, int *err) |
| 598 | { |
| 599 | unsigned count, indirect; |
| 600 | struct dx_entry *at, *entries, *p, *q, *m; |
| 601 | struct dx_root *root; |
| 602 | struct buffer_head *bh; |
| 603 | struct dx_frame *frame = frame_in; |
| 604 | u32 hash; |
| 605 | |
| 606 | frame->bh = NULL; |
Carlos Maiolino | 6d1ab10 | 2012-09-27 09:31:33 -0400 | [diff] [blame] | 607 | if (!(bh = ext4_bread(NULL, dir, 0, 0, err))) { |
| 608 | if (*err == 0) |
| 609 | *err = ERR_BAD_DX_DIR; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 610 | goto fail; |
Carlos Maiolino | 6d1ab10 | 2012-09-27 09:31:33 -0400 | [diff] [blame] | 611 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 612 | root = (struct dx_root *) bh->b_data; |
| 613 | if (root->info.hash_version != DX_HASH_TEA && |
| 614 | root->info.hash_version != DX_HASH_HALF_MD4 && |
| 615 | root->info.hash_version != DX_HASH_LEGACY) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 616 | ext4_warning(dir->i_sb, "Unrecognised inode hash code %d", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 617 | root->info.hash_version); |
| 618 | brelse(bh); |
| 619 | *err = ERR_BAD_DX_DIR; |
| 620 | goto fail; |
| 621 | } |
| 622 | hinfo->hash_version = root->info.hash_version; |
Theodore Ts'o | f99b258 | 2008-10-28 13:21:44 -0400 | [diff] [blame] | 623 | if (hinfo->hash_version <= DX_HASH_TEA) |
| 624 | hinfo->hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 625 | hinfo->seed = EXT4_SB(dir->i_sb)->s_hash_seed; |
Theodore Ts'o | f702ba0 | 2008-09-22 15:21:01 -0400 | [diff] [blame] | 626 | if (d_name) |
| 627 | ext4fs_dirhash(d_name->name, d_name->len, hinfo); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 628 | hash = hinfo->hash; |
| 629 | |
| 630 | if (root->info.unused_flags & 1) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 631 | ext4_warning(dir->i_sb, "Unimplemented inode hash flags: %#06x", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 632 | root->info.unused_flags); |
| 633 | brelse(bh); |
| 634 | *err = ERR_BAD_DX_DIR; |
| 635 | goto fail; |
| 636 | } |
| 637 | |
| 638 | if ((indirect = root->info.indirect_levels) > 1) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 639 | ext4_warning(dir->i_sb, "Unimplemented inode hash depth: %#06x", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 640 | root->info.indirect_levels); |
| 641 | brelse(bh); |
| 642 | *err = ERR_BAD_DX_DIR; |
| 643 | goto fail; |
| 644 | } |
| 645 | |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 646 | if (!buffer_verified(bh) && |
| 647 | !ext4_dx_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data)) { |
| 648 | ext4_warning(dir->i_sb, "Root failed checksum"); |
| 649 | brelse(bh); |
| 650 | *err = ERR_BAD_DX_DIR; |
| 651 | goto fail; |
| 652 | } |
| 653 | set_buffer_verified(bh); |
| 654 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 655 | entries = (struct dx_entry *) (((char *)&root->info) + |
| 656 | root->info.info_length); |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 657 | |
| 658 | if (dx_get_limit(entries) != dx_root_limit(dir, |
| 659 | root->info.info_length)) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 660 | ext4_warning(dir->i_sb, "dx entry: limit != root limit"); |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 661 | brelse(bh); |
| 662 | *err = ERR_BAD_DX_DIR; |
| 663 | goto fail; |
| 664 | } |
| 665 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 666 | dxtrace(printk("Look up %x", hash)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 667 | while (1) |
| 668 | { |
| 669 | count = dx_get_count(entries); |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 670 | if (!count || count > dx_get_limit(entries)) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 671 | ext4_warning(dir->i_sb, |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 672 | "dx entry: no count or count > limit"); |
| 673 | brelse(bh); |
| 674 | *err = ERR_BAD_DX_DIR; |
| 675 | goto fail2; |
| 676 | } |
| 677 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 678 | p = entries + 1; |
| 679 | q = entries + count - 1; |
| 680 | while (p <= q) |
| 681 | { |
| 682 | m = p + (q - p)/2; |
| 683 | dxtrace(printk(".")); |
| 684 | if (dx_get_hash(m) > hash) |
| 685 | q = m - 1; |
| 686 | else |
| 687 | p = m + 1; |
| 688 | } |
| 689 | |
| 690 | if (0) // linear search cross check |
| 691 | { |
| 692 | unsigned n = count - 1; |
| 693 | at = entries; |
| 694 | while (n--) |
| 695 | { |
| 696 | dxtrace(printk(",")); |
| 697 | if (dx_get_hash(++at) > hash) |
| 698 | { |
| 699 | at--; |
| 700 | break; |
| 701 | } |
| 702 | } |
| 703 | assert (at == p - 1); |
| 704 | } |
| 705 | |
| 706 | at = p - 1; |
| 707 | dxtrace(printk(" %x->%u\n", at == entries? 0: dx_get_hash(at), dx_get_block(at))); |
| 708 | frame->bh = bh; |
| 709 | frame->entries = entries; |
| 710 | frame->at = at; |
| 711 | if (!indirect--) return frame; |
Carlos Maiolino | 6d1ab10 | 2012-09-27 09:31:33 -0400 | [diff] [blame] | 712 | if (!(bh = ext4_bread(NULL, dir, dx_get_block(at), 0, err))) { |
| 713 | if (!(*err)) |
| 714 | *err = ERR_BAD_DX_DIR; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 715 | goto fail2; |
Carlos Maiolino | 6d1ab10 | 2012-09-27 09:31:33 -0400 | [diff] [blame] | 716 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 717 | at = entries = ((struct dx_node *) bh->b_data)->entries; |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 718 | |
| 719 | if (!buffer_verified(bh) && |
| 720 | !ext4_dx_csum_verify(dir, |
| 721 | (struct ext4_dir_entry *)bh->b_data)) { |
| 722 | ext4_warning(dir->i_sb, "Node failed checksum"); |
| 723 | brelse(bh); |
| 724 | *err = ERR_BAD_DX_DIR; |
| 725 | goto fail; |
| 726 | } |
| 727 | set_buffer_verified(bh); |
| 728 | |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 729 | if (dx_get_limit(entries) != dx_node_limit (dir)) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 730 | ext4_warning(dir->i_sb, |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 731 | "dx entry: limit != node limit"); |
| 732 | brelse(bh); |
| 733 | *err = ERR_BAD_DX_DIR; |
| 734 | goto fail2; |
| 735 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 736 | frame++; |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 737 | frame->bh = NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 738 | } |
| 739 | fail2: |
| 740 | while (frame >= frame_in) { |
| 741 | brelse(frame->bh); |
| 742 | frame--; |
| 743 | } |
| 744 | fail: |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 745 | if (*err == ERR_BAD_DX_DIR) |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 746 | ext4_warning(dir->i_sb, |
Zheng Liu | 9ee4930 | 2012-02-20 23:09:36 -0500 | [diff] [blame] | 747 | "Corrupt dir inode %lu, running e2fsck is " |
Eric Sandeen | 3d82aba | 2007-09-18 22:46:38 -0700 | [diff] [blame] | 748 | "recommended.", dir->i_ino); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 749 | return NULL; |
| 750 | } |
| 751 | |
| 752 | static void dx_release (struct dx_frame *frames) |
| 753 | { |
| 754 | if (frames[0].bh == NULL) |
| 755 | return; |
| 756 | |
| 757 | if (((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels) |
| 758 | brelse(frames[1].bh); |
| 759 | brelse(frames[0].bh); |
| 760 | } |
| 761 | |
| 762 | /* |
| 763 | * This function increments the frame pointer to search the next leaf |
| 764 | * block, and reads in the necessary intervening nodes if the search |
| 765 | * should be necessary. Whether or not the search is necessary is |
| 766 | * controlled by the hash parameter. If the hash value is even, then |
| 767 | * the search is only continued if the next block starts with that |
| 768 | * hash value. This is used if we are searching for a specific file. |
| 769 | * |
| 770 | * If the hash value is HASH_NB_ALWAYS, then always go to the next block. |
| 771 | * |
| 772 | * This function returns 1 if the caller should continue to search, |
| 773 | * or 0 if it should not. If there is an error reading one of the |
| 774 | * index blocks, it will a negative error code. |
| 775 | * |
| 776 | * If start_hash is non-null, it will be filled in with the starting |
| 777 | * hash of the next page. |
| 778 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 779 | static int ext4_htree_next_block(struct inode *dir, __u32 hash, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 780 | struct dx_frame *frame, |
| 781 | struct dx_frame *frames, |
| 782 | __u32 *start_hash) |
| 783 | { |
| 784 | struct dx_frame *p; |
| 785 | struct buffer_head *bh; |
| 786 | int err, num_frames = 0; |
| 787 | __u32 bhash; |
| 788 | |
| 789 | p = frame; |
| 790 | /* |
| 791 | * Find the next leaf page by incrementing the frame pointer. |
| 792 | * If we run out of entries in the interior node, loop around and |
| 793 | * increment pointer in the parent node. When we break out of |
| 794 | * this loop, num_frames indicates the number of interior |
| 795 | * nodes need to be read. |
| 796 | */ |
| 797 | while (1) { |
| 798 | if (++(p->at) < p->entries + dx_get_count(p->entries)) |
| 799 | break; |
| 800 | if (p == frames) |
| 801 | return 0; |
| 802 | num_frames++; |
| 803 | p--; |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | * If the hash is 1, then continue only if the next page has a |
| 808 | * continuation hash of any value. This is used for readdir |
| 809 | * handling. Otherwise, check to see if the hash matches the |
| 810 | * desired contiuation hash. If it doesn't, return since |
| 811 | * there's no point to read in the successive index pages. |
| 812 | */ |
| 813 | bhash = dx_get_hash(p->at); |
| 814 | if (start_hash) |
| 815 | *start_hash = bhash; |
| 816 | if ((hash & 1) == 0) { |
| 817 | if ((bhash & ~1) != hash) |
| 818 | return 0; |
| 819 | } |
| 820 | /* |
| 821 | * If the hash is HASH_NB_ALWAYS, we always go to the next |
| 822 | * block so no check is necessary |
| 823 | */ |
| 824 | while (num_frames--) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 825 | if (!(bh = ext4_bread(NULL, dir, dx_get_block(p->at), |
Carlos Maiolino | 6d1ab10 | 2012-09-27 09:31:33 -0400 | [diff] [blame] | 826 | 0, &err))) { |
| 827 | if (!err) { |
| 828 | ext4_error(dir->i_sb, |
| 829 | "Directory hole detected on inode %lu\n", |
| 830 | dir->i_ino); |
| 831 | return -EIO; |
| 832 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 833 | return err; /* Failure */ |
Carlos Maiolino | 6d1ab10 | 2012-09-27 09:31:33 -0400 | [diff] [blame] | 834 | } |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 835 | |
| 836 | if (!buffer_verified(bh) && |
| 837 | !ext4_dx_csum_verify(dir, |
| 838 | (struct ext4_dir_entry *)bh->b_data)) { |
| 839 | ext4_warning(dir->i_sb, "Node failed checksum"); |
| 840 | return -EIO; |
| 841 | } |
| 842 | set_buffer_verified(bh); |
| 843 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 844 | p++; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 845 | brelse(p->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 846 | p->bh = bh; |
| 847 | p->at = p->entries = ((struct dx_node *) bh->b_data)->entries; |
| 848 | } |
| 849 | return 1; |
| 850 | } |
| 851 | |
| 852 | |
| 853 | /* |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 854 | * This function fills a red-black tree with information from a |
| 855 | * directory block. It returns the number directory entries loaded |
| 856 | * into the tree. If there is an error it is returned in err. |
| 857 | */ |
| 858 | static int htree_dirblock_to_tree(struct file *dir_file, |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 859 | struct inode *dir, ext4_lblk_t block, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 860 | struct dx_hash_info *hinfo, |
| 861 | __u32 start_hash, __u32 start_minor_hash) |
| 862 | { |
| 863 | struct buffer_head *bh; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 864 | struct ext4_dir_entry_2 *de, *top; |
Carlos Maiolino | 90b0a97 | 2012-09-17 23:39:12 -0400 | [diff] [blame] | 865 | int err = 0, count = 0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 866 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 867 | dxtrace(printk(KERN_INFO "In htree dirblock_to_tree: block %lu\n", |
| 868 | (unsigned long)block)); |
Carlos Maiolino | 6d1ab10 | 2012-09-27 09:31:33 -0400 | [diff] [blame] | 869 | if (!(bh = ext4_bread(NULL, dir, block, 0, &err))) { |
| 870 | if (!err) { |
| 871 | err = -EIO; |
| 872 | ext4_error(dir->i_sb, |
| 873 | "Directory hole detected on inode %lu\n", |
| 874 | dir->i_ino); |
| 875 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 876 | return err; |
Carlos Maiolino | 6d1ab10 | 2012-09-27 09:31:33 -0400 | [diff] [blame] | 877 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 878 | |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 879 | if (!buffer_verified(bh) && |
| 880 | !ext4_dirent_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data)) |
| 881 | return -EIO; |
| 882 | set_buffer_verified(bh); |
| 883 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 884 | de = (struct ext4_dir_entry_2 *) bh->b_data; |
| 885 | top = (struct ext4_dir_entry_2 *) ((char *) de + |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 886 | dir->i_sb->s_blocksize - |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 887 | EXT4_DIR_REC_LEN(0)); |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 888 | for (; de < top; de = ext4_next_entry(de, dir->i_sb->s_blocksize)) { |
Theodore Ts'o | f7c2117 | 2011-01-10 12:10:55 -0500 | [diff] [blame] | 889 | if (ext4_check_dir_entry(dir, NULL, de, bh, |
Tao Ma | 226ba97 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 890 | bh->b_data, bh->b_size, |
Theodore Ts'o | cad3f00 | 2010-12-19 22:07:02 -0500 | [diff] [blame] | 891 | (block<<EXT4_BLOCK_SIZE_BITS(dir->i_sb)) |
| 892 | + ((char *)de - bh->b_data))) { |
Eric Sandeen | e6c4021 | 2006-12-06 20:36:28 -0800 | [diff] [blame] | 893 | /* On error, skip the f_pos to the next block. */ |
| 894 | dir_file->f_pos = (dir_file->f_pos | |
| 895 | (dir->i_sb->s_blocksize - 1)) + 1; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 896 | brelse(bh); |
Eric Sandeen | e6c4021 | 2006-12-06 20:36:28 -0800 | [diff] [blame] | 897 | return count; |
| 898 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 899 | ext4fs_dirhash(de->name, de->name_len, hinfo); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 900 | if ((hinfo->hash < start_hash) || |
| 901 | ((hinfo->hash == start_hash) && |
| 902 | (hinfo->minor_hash < start_minor_hash))) |
| 903 | continue; |
| 904 | if (de->inode == 0) |
| 905 | continue; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 906 | if ((err = ext4_htree_store_dirent(dir_file, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 907 | hinfo->hash, hinfo->minor_hash, de)) != 0) { |
| 908 | brelse(bh); |
| 909 | return err; |
| 910 | } |
| 911 | count++; |
| 912 | } |
| 913 | brelse(bh); |
| 914 | return count; |
| 915 | } |
| 916 | |
| 917 | |
| 918 | /* |
| 919 | * This function fills a red-black tree with information from a |
| 920 | * directory. We start scanning the directory in hash order, starting |
| 921 | * at start_hash and start_minor_hash. |
| 922 | * |
| 923 | * This function returns the number of entries inserted into the tree, |
| 924 | * or a negative error code. |
| 925 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 926 | int ext4_htree_fill_tree(struct file *dir_file, __u32 start_hash, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 927 | __u32 start_minor_hash, __u32 *next_hash) |
| 928 | { |
| 929 | struct dx_hash_info hinfo; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 930 | struct ext4_dir_entry_2 *de; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 931 | struct dx_frame frames[2], *frame; |
| 932 | struct inode *dir; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 933 | ext4_lblk_t block; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 934 | int count = 0; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 935 | int ret, err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 936 | __u32 hashval; |
| 937 | |
Theodore Ts'o | 60e6679 | 2010-05-17 07:00:00 -0400 | [diff] [blame] | 938 | dxtrace(printk(KERN_DEBUG "In htree_fill_tree, start hash: %x:%x\n", |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 939 | start_hash, start_minor_hash)); |
Josef "Jeff" Sipek | 9d54989 | 2006-12-08 02:36:38 -0800 | [diff] [blame] | 940 | dir = dir_file->f_path.dentry->d_inode; |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 941 | if (!(ext4_test_inode_flag(dir, EXT4_INODE_INDEX))) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 942 | hinfo.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version; |
Theodore Ts'o | f99b258 | 2008-10-28 13:21:44 -0400 | [diff] [blame] | 943 | if (hinfo.hash_version <= DX_HASH_TEA) |
| 944 | hinfo.hash_version += |
| 945 | EXT4_SB(dir->i_sb)->s_hash_unsigned; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 946 | hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 947 | count = htree_dirblock_to_tree(dir_file, dir, 0, &hinfo, |
| 948 | start_hash, start_minor_hash); |
| 949 | *next_hash = ~0; |
| 950 | return count; |
| 951 | } |
| 952 | hinfo.hash = start_hash; |
| 953 | hinfo.minor_hash = 0; |
Theodore Ts'o | f702ba0 | 2008-09-22 15:21:01 -0400 | [diff] [blame] | 954 | frame = dx_probe(NULL, dir, &hinfo, frames, &err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 955 | if (!frame) |
| 956 | return err; |
| 957 | |
| 958 | /* Add '.' and '..' from the htree header */ |
| 959 | if (!start_hash && !start_minor_hash) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 960 | de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data; |
| 961 | if ((err = ext4_htree_store_dirent(dir_file, 0, 0, de)) != 0) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 962 | goto errout; |
| 963 | count++; |
| 964 | } |
| 965 | if (start_hash < 2 || (start_hash ==2 && start_minor_hash==0)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 966 | de = (struct ext4_dir_entry_2 *) frames[0].bh->b_data; |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 967 | de = ext4_next_entry(de, dir->i_sb->s_blocksize); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 968 | if ((err = ext4_htree_store_dirent(dir_file, 2, 0, de)) != 0) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 969 | goto errout; |
| 970 | count++; |
| 971 | } |
| 972 | |
| 973 | while (1) { |
| 974 | block = dx_get_block(frame->at); |
| 975 | ret = htree_dirblock_to_tree(dir_file, dir, block, &hinfo, |
| 976 | start_hash, start_minor_hash); |
| 977 | if (ret < 0) { |
| 978 | err = ret; |
| 979 | goto errout; |
| 980 | } |
| 981 | count += ret; |
| 982 | hashval = ~0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 983 | ret = ext4_htree_next_block(dir, HASH_NB_ALWAYS, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 984 | frame, frames, &hashval); |
| 985 | *next_hash = hashval; |
| 986 | if (ret < 0) { |
| 987 | err = ret; |
| 988 | goto errout; |
| 989 | } |
| 990 | /* |
| 991 | * Stop if: (a) there are no more entries, or |
| 992 | * (b) we have inserted at least one entry and the |
| 993 | * next hash value is not a continuation |
| 994 | */ |
| 995 | if ((ret == 0) || |
| 996 | (count && ((hashval & 1) == 0))) |
| 997 | break; |
| 998 | } |
| 999 | dx_release(frames); |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 1000 | dxtrace(printk(KERN_DEBUG "Fill tree: returned %d entries, " |
| 1001 | "next hash: %x\n", count, *next_hash)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1002 | return count; |
| 1003 | errout: |
| 1004 | dx_release(frames); |
| 1005 | return (err); |
| 1006 | } |
| 1007 | |
Tao Ma | 7335cd3 | 2012-12-10 14:05:59 -0500 | [diff] [blame] | 1008 | static inline int search_dirblock(struct buffer_head *bh, |
| 1009 | struct inode *dir, |
| 1010 | const struct qstr *d_name, |
| 1011 | unsigned int offset, |
| 1012 | struct ext4_dir_entry_2 **res_dir) |
| 1013 | { |
| 1014 | return search_dir(bh, bh->b_data, dir->i_sb->s_blocksize, dir, |
| 1015 | d_name, offset, res_dir); |
| 1016 | } |
| 1017 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1018 | /* |
| 1019 | * Directory block splitting, compacting |
| 1020 | */ |
| 1021 | |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 1022 | /* |
| 1023 | * Create map of hash values, offsets, and sizes, stored at end of block. |
| 1024 | * Returns number of entries mapped. |
| 1025 | */ |
Theodore Ts'o | 8bad459 | 2009-02-14 21:46:54 -0500 | [diff] [blame] | 1026 | static int dx_make_map(struct ext4_dir_entry_2 *de, unsigned blocksize, |
| 1027 | struct dx_hash_info *hinfo, |
| 1028 | struct dx_map_entry *map_tail) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1029 | { |
| 1030 | int count = 0; |
| 1031 | char *base = (char *) de; |
| 1032 | struct dx_hash_info h = *hinfo; |
| 1033 | |
Theodore Ts'o | 8bad459 | 2009-02-14 21:46:54 -0500 | [diff] [blame] | 1034 | while ((char *) de < base + blocksize) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1035 | if (de->name_len && de->inode) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1036 | ext4fs_dirhash(de->name, de->name_len, &h); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1037 | map_tail--; |
| 1038 | map_tail->hash = h.hash; |
Toshiyuki Okajima | 9aee228 | 2009-06-08 12:41:35 -0400 | [diff] [blame] | 1039 | map_tail->offs = ((char *) de - base)>>2; |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 1040 | map_tail->size = le16_to_cpu(de->rec_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1041 | count++; |
| 1042 | cond_resched(); |
| 1043 | } |
| 1044 | /* XXX: do we need to check rec_len == 0 case? -Chris */ |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1045 | de = ext4_next_entry(de, blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1046 | } |
| 1047 | return count; |
| 1048 | } |
| 1049 | |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 1050 | /* Sort map by hash value */ |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1051 | static void dx_sort_map (struct dx_map_entry *map, unsigned count) |
| 1052 | { |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 1053 | struct dx_map_entry *p, *q, *top = map + count - 1; |
| 1054 | int more; |
| 1055 | /* Combsort until bubble sort doesn't suck */ |
| 1056 | while (count > 2) { |
| 1057 | count = count*10/13; |
| 1058 | if (count - 9 < 2) /* 9, 10 -> 11 */ |
| 1059 | count = 11; |
| 1060 | for (p = top, q = p - count; q >= map; p--, q--) |
| 1061 | if (p->hash < q->hash) |
| 1062 | swap(*p, *q); |
| 1063 | } |
| 1064 | /* Garden variety bubble sort */ |
| 1065 | do { |
| 1066 | more = 0; |
| 1067 | q = top; |
| 1068 | while (q-- > map) { |
| 1069 | if (q[1].hash >= q[0].hash) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1070 | continue; |
Andrew Morton | 63f5793 | 2006-10-11 01:21:24 -0700 | [diff] [blame] | 1071 | swap(*(q+1), *q); |
| 1072 | more = 1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1073 | } |
| 1074 | } while(more); |
| 1075 | } |
| 1076 | |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1077 | static void dx_insert_block(struct dx_frame *frame, u32 hash, ext4_lblk_t block) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1078 | { |
| 1079 | struct dx_entry *entries = frame->entries; |
| 1080 | struct dx_entry *old = frame->at, *new = old + 1; |
| 1081 | int count = dx_get_count(entries); |
| 1082 | |
| 1083 | assert(count < dx_get_limit(entries)); |
| 1084 | assert(old < entries + count); |
| 1085 | memmove(new + 1, new, (char *)(entries + count) - (char *)(new)); |
| 1086 | dx_set_hash(new, hash); |
| 1087 | dx_set_block(new, block); |
| 1088 | dx_set_count(entries, count + 1); |
| 1089 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1090 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1091 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1092 | * NOTE! unlike strncmp, ext4_match returns 1 for success, 0 for failure. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1093 | * |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1094 | * `len <= EXT4_NAME_LEN' is guaranteed by caller. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1095 | * `de != NULL' is guaranteed by caller. |
| 1096 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1097 | static inline int ext4_match (int len, const char * const name, |
| 1098 | struct ext4_dir_entry_2 * de) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1099 | { |
| 1100 | if (len != de->name_len) |
| 1101 | return 0; |
| 1102 | if (!de->inode) |
| 1103 | return 0; |
| 1104 | return !memcmp(name, de->name, len); |
| 1105 | } |
| 1106 | |
| 1107 | /* |
| 1108 | * Returns 0 if not found, -1 on failure, and 1 on success |
| 1109 | */ |
Tao Ma | 7335cd3 | 2012-12-10 14:05:59 -0500 | [diff] [blame] | 1110 | int search_dir(struct buffer_head *bh, |
| 1111 | char *search_buf, |
| 1112 | int buf_size, |
| 1113 | struct inode *dir, |
| 1114 | const struct qstr *d_name, |
| 1115 | unsigned int offset, |
| 1116 | struct ext4_dir_entry_2 **res_dir) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1117 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1118 | struct ext4_dir_entry_2 * de; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1119 | char * dlimit; |
| 1120 | int de_len; |
Theodore Ts'o | f702ba0 | 2008-09-22 15:21:01 -0400 | [diff] [blame] | 1121 | const char *name = d_name->name; |
| 1122 | int namelen = d_name->len; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1123 | |
Tao Ma | 7335cd3 | 2012-12-10 14:05:59 -0500 | [diff] [blame] | 1124 | de = (struct ext4_dir_entry_2 *)search_buf; |
| 1125 | dlimit = search_buf + buf_size; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1126 | while ((char *) de < dlimit) { |
| 1127 | /* this code is executed quadratically often */ |
| 1128 | /* do minimal checking `by hand' */ |
| 1129 | |
| 1130 | if ((char *) de + namelen <= dlimit && |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1131 | ext4_match (namelen, name, de)) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1132 | /* found a match - just to be sure, do a full check */ |
Tao Ma | 226ba97 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 1133 | if (ext4_check_dir_entry(dir, NULL, de, bh, bh->b_data, |
| 1134 | bh->b_size, offset)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1135 | return -1; |
| 1136 | *res_dir = de; |
| 1137 | return 1; |
| 1138 | } |
| 1139 | /* prevent looping on a bad block */ |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1140 | de_len = ext4_rec_len_from_disk(de->rec_len, |
| 1141 | dir->i_sb->s_blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1142 | if (de_len <= 0) |
| 1143 | return -1; |
| 1144 | offset += de_len; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1145 | de = (struct ext4_dir_entry_2 *) ((char *) de + de_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1146 | } |
| 1147 | return 0; |
| 1148 | } |
| 1149 | |
Darrick J. Wong | c6af8803 | 2012-11-12 23:51:02 -0500 | [diff] [blame] | 1150 | static int is_dx_internal_node(struct inode *dir, ext4_lblk_t block, |
| 1151 | struct ext4_dir_entry *de) |
| 1152 | { |
| 1153 | struct super_block *sb = dir->i_sb; |
| 1154 | |
| 1155 | if (!is_dx(dir)) |
| 1156 | return 0; |
| 1157 | if (block == 0) |
| 1158 | return 1; |
| 1159 | if (de->inode == 0 && |
| 1160 | ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) == |
| 1161 | sb->s_blocksize) |
| 1162 | return 1; |
| 1163 | return 0; |
| 1164 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1165 | |
| 1166 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1167 | * ext4_find_entry() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1168 | * |
| 1169 | * finds an entry in the specified directory with the wanted name. It |
| 1170 | * returns the cache buffer in which the entry was found, and the entry |
| 1171 | * itself (as a parameter - res_dir). It does NOT read the inode of the |
| 1172 | * entry - you'll have to do that yourself if you want to. |
| 1173 | * |
| 1174 | * The returned buffer_head has ->b_count elevated. The caller is expected |
| 1175 | * to brelse() it when appropriate. |
| 1176 | */ |
Theodore Ts'o | f702ba0 | 2008-09-22 15:21:01 -0400 | [diff] [blame] | 1177 | static struct buffer_head * ext4_find_entry (struct inode *dir, |
| 1178 | const struct qstr *d_name, |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame^] | 1179 | struct ext4_dir_entry_2 **res_dir, |
| 1180 | int *inlined) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1181 | { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1182 | struct super_block *sb; |
| 1183 | struct buffer_head *bh_use[NAMEI_RA_SIZE]; |
| 1184 | struct buffer_head *bh, *ret = NULL; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1185 | ext4_lblk_t start, block, b; |
Theodore Ts'o | 8941ec8 | 2010-10-27 21:30:08 -0400 | [diff] [blame] | 1186 | const u8 *name = d_name->name; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1187 | int ra_max = 0; /* Number of bh's in the readahead |
| 1188 | buffer, bh_use[] */ |
| 1189 | int ra_ptr = 0; /* Current index into readahead |
| 1190 | buffer */ |
| 1191 | int num = 0; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1192 | ext4_lblk_t nblocks; |
| 1193 | int i, err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1194 | int namelen; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1195 | |
| 1196 | *res_dir = NULL; |
| 1197 | sb = dir->i_sb; |
Theodore Ts'o | f702ba0 | 2008-09-22 15:21:01 -0400 | [diff] [blame] | 1198 | namelen = d_name->len; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1199 | if (namelen > EXT4_NAME_LEN) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1200 | return NULL; |
Tao Ma | e8e948e | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 1201 | |
| 1202 | if (ext4_has_inline_data(dir)) { |
| 1203 | int has_inline_data = 1; |
| 1204 | ret = ext4_find_inline_entry(dir, d_name, res_dir, |
| 1205 | &has_inline_data); |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame^] | 1206 | if (has_inline_data) { |
| 1207 | if (inlined) |
| 1208 | *inlined = 1; |
Tao Ma | e8e948e | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 1209 | return ret; |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame^] | 1210 | } |
Tao Ma | e8e948e | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 1211 | } |
| 1212 | |
Theodore Ts'o | 8941ec8 | 2010-10-27 21:30:08 -0400 | [diff] [blame] | 1213 | if ((namelen <= 2) && (name[0] == '.') && |
Aaro Koskinen | 6d5c3aa | 2010-12-14 21:45:31 -0500 | [diff] [blame] | 1214 | (name[1] == '.' || name[1] == '\0')) { |
Theodore Ts'o | 8941ec8 | 2010-10-27 21:30:08 -0400 | [diff] [blame] | 1215 | /* |
| 1216 | * "." or ".." will only be in the first block |
| 1217 | * NFS may look up ".."; "." should be handled by the VFS |
| 1218 | */ |
| 1219 | block = start = 0; |
| 1220 | nblocks = 1; |
| 1221 | goto restart; |
| 1222 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1223 | if (is_dx(dir)) { |
Theodore Ts'o | f702ba0 | 2008-09-22 15:21:01 -0400 | [diff] [blame] | 1224 | bh = ext4_dx_find_entry(dir, d_name, res_dir, &err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1225 | /* |
| 1226 | * On success, or if the error was file not found, |
| 1227 | * return. Otherwise, fall back to doing a search the |
| 1228 | * old fashioned way. |
| 1229 | */ |
| 1230 | if (bh || (err != ERR_BAD_DX_DIR)) |
| 1231 | return bh; |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 1232 | dxtrace(printk(KERN_DEBUG "ext4_find_entry: dx failed, " |
| 1233 | "falling back\n")); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1234 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1235 | nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb); |
| 1236 | start = EXT4_I(dir)->i_dir_start_lookup; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1237 | if (start >= nblocks) |
| 1238 | start = 0; |
| 1239 | block = start; |
| 1240 | restart: |
| 1241 | do { |
| 1242 | /* |
| 1243 | * We deal with the read-ahead logic here. |
| 1244 | */ |
| 1245 | if (ra_ptr >= ra_max) { |
| 1246 | /* Refill the readahead buffer */ |
| 1247 | ra_ptr = 0; |
| 1248 | b = block; |
| 1249 | for (ra_max = 0; ra_max < NAMEI_RA_SIZE; ra_max++) { |
| 1250 | /* |
| 1251 | * Terminate if we reach the end of the |
| 1252 | * directory and must wrap, or if our |
| 1253 | * search has finished at this block. |
| 1254 | */ |
| 1255 | if (b >= nblocks || (num && block == start)) { |
| 1256 | bh_use[ra_max] = NULL; |
| 1257 | break; |
| 1258 | } |
| 1259 | num++; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1260 | bh = ext4_getblk(NULL, dir, b++, 0, &err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1261 | bh_use[ra_max] = bh; |
| 1262 | if (bh) |
Christoph Hellwig | 65299a3 | 2011-08-23 14:50:29 +0200 | [diff] [blame] | 1263 | ll_rw_block(READ | REQ_META | REQ_PRIO, |
| 1264 | 1, &bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1265 | } |
| 1266 | } |
| 1267 | if ((bh = bh_use[ra_ptr++]) == NULL) |
| 1268 | goto next; |
| 1269 | wait_on_buffer(bh); |
| 1270 | if (!buffer_uptodate(bh)) { |
| 1271 | /* read error, skip block & hope for the best */ |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1272 | EXT4_ERROR_INODE(dir, "reading directory lblock %lu", |
| 1273 | (unsigned long) block); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1274 | brelse(bh); |
| 1275 | goto next; |
| 1276 | } |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1277 | if (!buffer_verified(bh) && |
Darrick J. Wong | c6af8803 | 2012-11-12 23:51:02 -0500 | [diff] [blame] | 1278 | !is_dx_internal_node(dir, block, |
| 1279 | (struct ext4_dir_entry *)bh->b_data) && |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1280 | !ext4_dirent_csum_verify(dir, |
| 1281 | (struct ext4_dir_entry *)bh->b_data)) { |
| 1282 | EXT4_ERROR_INODE(dir, "checksumming directory " |
| 1283 | "block %lu", (unsigned long)block); |
| 1284 | brelse(bh); |
| 1285 | goto next; |
| 1286 | } |
| 1287 | set_buffer_verified(bh); |
Theodore Ts'o | f702ba0 | 2008-09-22 15:21:01 -0400 | [diff] [blame] | 1288 | i = search_dirblock(bh, dir, d_name, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1289 | block << EXT4_BLOCK_SIZE_BITS(sb), res_dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1290 | if (i == 1) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1291 | EXT4_I(dir)->i_dir_start_lookup = block; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1292 | ret = bh; |
| 1293 | goto cleanup_and_exit; |
| 1294 | } else { |
| 1295 | brelse(bh); |
| 1296 | if (i < 0) |
| 1297 | goto cleanup_and_exit; |
| 1298 | } |
| 1299 | next: |
| 1300 | if (++block >= nblocks) |
| 1301 | block = 0; |
| 1302 | } while (block != start); |
| 1303 | |
| 1304 | /* |
| 1305 | * If the directory has grown while we were searching, then |
| 1306 | * search the last part of the directory before giving up. |
| 1307 | */ |
| 1308 | block = nblocks; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1309 | nblocks = dir->i_size >> EXT4_BLOCK_SIZE_BITS(sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1310 | if (block < nblocks) { |
| 1311 | start = 0; |
| 1312 | goto restart; |
| 1313 | } |
| 1314 | |
| 1315 | cleanup_and_exit: |
| 1316 | /* Clean up the read-ahead blocks */ |
| 1317 | for (; ra_ptr < ra_max; ra_ptr++) |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1318 | brelse(bh_use[ra_ptr]); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1319 | return ret; |
| 1320 | } |
| 1321 | |
Theodore Ts'o | f702ba0 | 2008-09-22 15:21:01 -0400 | [diff] [blame] | 1322 | static struct buffer_head * ext4_dx_find_entry(struct inode *dir, const struct qstr *d_name, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1323 | struct ext4_dir_entry_2 **res_dir, int *err) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1324 | { |
Theodore Ts'o | 8941ec8 | 2010-10-27 21:30:08 -0400 | [diff] [blame] | 1325 | struct super_block * sb = dir->i_sb; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1326 | struct dx_hash_info hinfo; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1327 | struct dx_frame frames[2], *frame; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1328 | struct buffer_head *bh; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1329 | ext4_lblk_t block; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1330 | int retval; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1331 | |
Theodore Ts'o | 8941ec8 | 2010-10-27 21:30:08 -0400 | [diff] [blame] | 1332 | if (!(frame = dx_probe(d_name, dir, &hinfo, frames, err))) |
| 1333 | return NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1334 | do { |
| 1335 | block = dx_get_block(frame->at); |
Carlos Maiolino | 6d1ab10 | 2012-09-27 09:31:33 -0400 | [diff] [blame] | 1336 | if (!(bh = ext4_bread(NULL, dir, block, 0, err))) { |
| 1337 | if (!(*err)) { |
| 1338 | *err = -EIO; |
| 1339 | ext4_error(dir->i_sb, |
| 1340 | "Directory hole detected on inode %lu\n", |
| 1341 | dir->i_ino); |
| 1342 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1343 | goto errout; |
Carlos Maiolino | 6d1ab10 | 2012-09-27 09:31:33 -0400 | [diff] [blame] | 1344 | } |
Duane Griffin | f3b35f0 | 2008-07-11 19:27:31 -0400 | [diff] [blame] | 1345 | |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1346 | if (!buffer_verified(bh) && |
| 1347 | !ext4_dirent_csum_verify(dir, |
| 1348 | (struct ext4_dir_entry *)bh->b_data)) { |
| 1349 | EXT4_ERROR_INODE(dir, "checksumming directory " |
| 1350 | "block %lu", (unsigned long)block); |
| 1351 | brelse(bh); |
| 1352 | *err = -EIO; |
| 1353 | goto errout; |
| 1354 | } |
| 1355 | set_buffer_verified(bh); |
Theodore Ts'o | 7845c04 | 2010-10-27 21:30:08 -0400 | [diff] [blame] | 1356 | retval = search_dirblock(bh, dir, d_name, |
| 1357 | block << EXT4_BLOCK_SIZE_BITS(sb), |
| 1358 | res_dir); |
| 1359 | if (retval == 1) { /* Success! */ |
| 1360 | dx_release(frames); |
| 1361 | return bh; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1362 | } |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1363 | brelse(bh); |
Theodore Ts'o | 7845c04 | 2010-10-27 21:30:08 -0400 | [diff] [blame] | 1364 | if (retval == -1) { |
| 1365 | *err = ERR_BAD_DX_DIR; |
| 1366 | goto errout; |
| 1367 | } |
| 1368 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1369 | /* Check to see if we should continue to search */ |
Theodore Ts'o | 8941ec8 | 2010-10-27 21:30:08 -0400 | [diff] [blame] | 1370 | retval = ext4_htree_next_block(dir, hinfo.hash, frame, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1371 | frames, NULL); |
| 1372 | if (retval < 0) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 1373 | ext4_warning(sb, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1374 | "error reading index page in directory #%lu", |
| 1375 | dir->i_ino); |
| 1376 | *err = retval; |
| 1377 | goto errout; |
| 1378 | } |
| 1379 | } while (retval == 1); |
| 1380 | |
| 1381 | *err = -ENOENT; |
| 1382 | errout: |
Bernd Schubert | 265c6a0 | 2011-07-16 19:41:23 -0400 | [diff] [blame] | 1383 | dxtrace(printk(KERN_DEBUG "%s not found\n", d_name->name)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1384 | dx_release (frames); |
| 1385 | return NULL; |
| 1386 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1387 | |
Al Viro | 00cd8dd | 2012-06-10 17:13:09 -0400 | [diff] [blame] | 1388 | static struct dentry *ext4_lookup(struct inode *dir, struct dentry *dentry, unsigned int flags) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1389 | { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1390 | struct inode *inode; |
| 1391 | struct ext4_dir_entry_2 *de; |
| 1392 | struct buffer_head *bh; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1393 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1394 | if (dentry->d_name.len > EXT4_NAME_LEN) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1395 | return ERR_PTR(-ENAMETOOLONG); |
| 1396 | |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame^] | 1397 | bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1398 | inode = NULL; |
| 1399 | if (bh) { |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 1400 | __u32 ino = le32_to_cpu(de->inode); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1401 | brelse(bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1402 | if (!ext4_valid_inum(dir->i_sb, ino)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1403 | EXT4_ERROR_INODE(dir, "bad inode number: %u", ino); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 1404 | return ERR_PTR(-EIO); |
Vasily Averin | a6c15c2 | 2007-07-15 23:40:46 -0700 | [diff] [blame] | 1405 | } |
Andreas Dilger | 7e936b7 | 2012-05-28 17:02:25 -0400 | [diff] [blame] | 1406 | if (unlikely(ino == dir->i_ino)) { |
| 1407 | EXT4_ERROR_INODE(dir, "'%.*s' linked to parent dir", |
| 1408 | dentry->d_name.len, |
| 1409 | dentry->d_name.name); |
| 1410 | return ERR_PTR(-EIO); |
| 1411 | } |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 1412 | inode = ext4_iget(dir->i_sb, ino); |
Al Viro | a904937 | 2011-07-08 21:20:11 -0400 | [diff] [blame] | 1413 | if (inode == ERR_PTR(-ESTALE)) { |
| 1414 | EXT4_ERROR_INODE(dir, |
| 1415 | "deleted inode referenced: %u", |
| 1416 | ino); |
| 1417 | return ERR_PTR(-EIO); |
Bryan Donlan | e6f009b | 2009-02-22 21:20:25 -0500 | [diff] [blame] | 1418 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1419 | } |
| 1420 | return d_splice_alias(inode, dentry); |
| 1421 | } |
| 1422 | |
| 1423 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1424 | struct dentry *ext4_get_parent(struct dentry *child) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1425 | { |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 1426 | __u32 ino; |
Linus Torvalds | 26fe575 | 2012-05-10 13:14:12 -0700 | [diff] [blame] | 1427 | static const struct qstr dotdot = QSTR_INIT("..", 2); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1428 | struct ext4_dir_entry_2 * de; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1429 | struct buffer_head *bh; |
| 1430 | |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame^] | 1431 | bh = ext4_find_entry(child->d_inode, &dotdot, &de, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1432 | if (!bh) |
| 1433 | return ERR_PTR(-ENOENT); |
| 1434 | ino = le32_to_cpu(de->inode); |
| 1435 | brelse(bh); |
| 1436 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1437 | if (!ext4_valid_inum(child->d_inode->i_sb, ino)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1438 | EXT4_ERROR_INODE(child->d_inode, |
| 1439 | "bad parent inode number: %u", ino); |
David Howells | 1d1fe1e | 2008-02-07 00:15:37 -0800 | [diff] [blame] | 1440 | return ERR_PTR(-EIO); |
Vasily Averin | a6c15c2 | 2007-07-15 23:40:46 -0700 | [diff] [blame] | 1441 | } |
| 1442 | |
Christoph Hellwig | 4400372 | 2008-08-11 15:49:04 +0200 | [diff] [blame] | 1443 | return d_obtain_alias(ext4_iget(child->d_inode->i_sb, ino)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | #define S_SHIFT 12 |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1447 | static unsigned char ext4_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 1448 | [S_IFREG >> S_SHIFT] = EXT4_FT_REG_FILE, |
| 1449 | [S_IFDIR >> S_SHIFT] = EXT4_FT_DIR, |
| 1450 | [S_IFCHR >> S_SHIFT] = EXT4_FT_CHRDEV, |
| 1451 | [S_IFBLK >> S_SHIFT] = EXT4_FT_BLKDEV, |
| 1452 | [S_IFIFO >> S_SHIFT] = EXT4_FT_FIFO, |
| 1453 | [S_IFSOCK >> S_SHIFT] = EXT4_FT_SOCK, |
| 1454 | [S_IFLNK >> S_SHIFT] = EXT4_FT_SYMLINK, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1455 | }; |
| 1456 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1457 | static inline void ext4_set_de_type(struct super_block *sb, |
| 1458 | struct ext4_dir_entry_2 *de, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1459 | umode_t mode) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1460 | if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FILETYPE)) |
| 1461 | de->file_type = ext4_type_by_mode[(mode & S_IFMT)>>S_SHIFT]; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1462 | } |
| 1463 | |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 1464 | /* |
| 1465 | * Move count entries from end of map between two memory locations. |
| 1466 | * Returns pointer to last entry moved. |
| 1467 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1468 | static struct ext4_dir_entry_2 * |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1469 | dx_move_dirents(char *from, char *to, struct dx_map_entry *map, int count, |
| 1470 | unsigned blocksize) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1471 | { |
| 1472 | unsigned rec_len = 0; |
| 1473 | |
| 1474 | while (count--) { |
Theodore Ts'o | 60e6679 | 2010-05-17 07:00:00 -0400 | [diff] [blame] | 1475 | struct ext4_dir_entry_2 *de = (struct ext4_dir_entry_2 *) |
Toshiyuki Okajima | 9aee228 | 2009-06-08 12:41:35 -0400 | [diff] [blame] | 1476 | (from + (map->offs<<2)); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1477 | rec_len = EXT4_DIR_REC_LEN(de->name_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1478 | memcpy (to, de, rec_len); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1479 | ((struct ext4_dir_entry_2 *) to)->rec_len = |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1480 | ext4_rec_len_to_disk(rec_len, blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1481 | de->inode = 0; |
| 1482 | map++; |
| 1483 | to += rec_len; |
| 1484 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1485 | return (struct ext4_dir_entry_2 *) (to - rec_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1486 | } |
| 1487 | |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 1488 | /* |
| 1489 | * Compact each dir entry in the range to the minimal rec_len. |
| 1490 | * Returns pointer to last entry in range. |
| 1491 | */ |
Theodore Ts'o | 8bad459 | 2009-02-14 21:46:54 -0500 | [diff] [blame] | 1492 | static struct ext4_dir_entry_2* dx_pack_dirents(char *base, unsigned blocksize) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1493 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1494 | struct ext4_dir_entry_2 *next, *to, *prev, *de = (struct ext4_dir_entry_2 *) base; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1495 | unsigned rec_len = 0; |
| 1496 | |
| 1497 | prev = to = de; |
Theodore Ts'o | 8bad459 | 2009-02-14 21:46:54 -0500 | [diff] [blame] | 1498 | while ((char*)de < base + blocksize) { |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1499 | next = ext4_next_entry(de, blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1500 | if (de->inode && de->name_len) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1501 | rec_len = EXT4_DIR_REC_LEN(de->name_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1502 | if (de > to) |
| 1503 | memmove(to, de, rec_len); |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1504 | to->rec_len = ext4_rec_len_to_disk(rec_len, blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1505 | prev = to; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1506 | to = (struct ext4_dir_entry_2 *) (((char *) to) + rec_len); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1507 | } |
| 1508 | de = next; |
| 1509 | } |
| 1510 | return prev; |
| 1511 | } |
| 1512 | |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 1513 | /* |
| 1514 | * Split a full leaf block to make room for a new dir entry. |
| 1515 | * Allocate a new block, and move entries so that they are approx. equally full. |
| 1516 | * Returns pointer to de in block into which the new entry will be inserted. |
| 1517 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1518 | static struct ext4_dir_entry_2 *do_split(handle_t *handle, struct inode *dir, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1519 | struct buffer_head **bh,struct dx_frame *frame, |
| 1520 | struct dx_hash_info *hinfo, int *error) |
| 1521 | { |
| 1522 | unsigned blocksize = dir->i_sb->s_blocksize; |
| 1523 | unsigned count, continued; |
| 1524 | struct buffer_head *bh2; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1525 | ext4_lblk_t newblock; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1526 | u32 hash2; |
| 1527 | struct dx_map_entry *map; |
| 1528 | char *data1 = (*bh)->b_data, *data2; |
Theodore Ts'o | 59e315b | 2008-12-06 16:58:39 -0500 | [diff] [blame] | 1529 | unsigned split, move, size; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1530 | struct ext4_dir_entry_2 *de = NULL, *de2; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1531 | struct ext4_dir_entry_tail *t; |
| 1532 | int csum_size = 0; |
Theodore Ts'o | 59e315b | 2008-12-06 16:58:39 -0500 | [diff] [blame] | 1533 | int err = 0, i; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1534 | |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1535 | if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb, |
| 1536 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) |
| 1537 | csum_size = sizeof(struct ext4_dir_entry_tail); |
| 1538 | |
Dmitriy Monakhov | fedee54 | 2007-05-08 00:25:34 -0700 | [diff] [blame] | 1539 | bh2 = ext4_append (handle, dir, &newblock, &err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1540 | if (!(bh2)) { |
| 1541 | brelse(*bh); |
| 1542 | *bh = NULL; |
| 1543 | goto errout; |
| 1544 | } |
| 1545 | |
| 1546 | BUFFER_TRACE(*bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1547 | err = ext4_journal_get_write_access(handle, *bh); |
Dmitriy Monakhov | fedee54 | 2007-05-08 00:25:34 -0700 | [diff] [blame] | 1548 | if (err) |
| 1549 | goto journal_error; |
| 1550 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1551 | BUFFER_TRACE(frame->bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1552 | err = ext4_journal_get_write_access(handle, frame->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1553 | if (err) |
| 1554 | goto journal_error; |
| 1555 | |
| 1556 | data2 = bh2->b_data; |
| 1557 | |
| 1558 | /* create map in the end of data2 block */ |
| 1559 | map = (struct dx_map_entry *) (data2 + blocksize); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1560 | count = dx_make_map((struct ext4_dir_entry_2 *) data1, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1561 | blocksize, hinfo, map); |
| 1562 | map -= count; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1563 | dx_sort_map(map, count); |
Eric Sandeen | ef2b02d | 2007-09-18 22:46:42 -0700 | [diff] [blame] | 1564 | /* Split the existing block in the middle, size-wise */ |
| 1565 | size = 0; |
| 1566 | move = 0; |
| 1567 | for (i = count-1; i >= 0; i--) { |
| 1568 | /* is more than half of this entry in 2nd half of the block? */ |
| 1569 | if (size + map[i].size/2 > blocksize/2) |
| 1570 | break; |
| 1571 | size += map[i].size; |
| 1572 | move++; |
| 1573 | } |
| 1574 | /* map index at which we will split */ |
| 1575 | split = count - move; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1576 | hash2 = map[split].hash; |
| 1577 | continued = hash2 == map[split - 1].hash; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1578 | dxtrace(printk(KERN_INFO "Split block %lu at %x, %i/%i\n", |
| 1579 | (unsigned long)dx_get_block(frame->at), |
| 1580 | hash2, split, count-split)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1581 | |
| 1582 | /* Fancy dance to stay within two buffers */ |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1583 | de2 = dx_move_dirents(data1, data2, map + split, count - split, blocksize); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1584 | de = dx_pack_dirents(data1, blocksize); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1585 | de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) - |
| 1586 | (char *) de, |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1587 | blocksize); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1588 | de2->rec_len = ext4_rec_len_to_disk(data2 + (blocksize - csum_size) - |
| 1589 | (char *) de2, |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1590 | blocksize); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1591 | if (csum_size) { |
| 1592 | t = EXT4_DIRENT_TAIL(data2, blocksize); |
| 1593 | initialize_dirent_tail(t, blocksize); |
| 1594 | |
| 1595 | t = EXT4_DIRENT_TAIL(data1, blocksize); |
| 1596 | initialize_dirent_tail(t, blocksize); |
| 1597 | } |
| 1598 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1599 | dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data1, blocksize, 1)); |
| 1600 | dxtrace(dx_show_leaf (hinfo, (struct ext4_dir_entry_2 *) data2, blocksize, 1)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1601 | |
| 1602 | /* Which block gets the new entry? */ |
| 1603 | if (hinfo->hash >= hash2) |
| 1604 | { |
| 1605 | swap(*bh, bh2); |
| 1606 | de = de2; |
| 1607 | } |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1608 | dx_insert_block(frame, hash2 + continued, newblock); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1609 | err = ext4_handle_dirty_dirent_node(handle, dir, bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1610 | if (err) |
| 1611 | goto journal_error; |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 1612 | err = ext4_handle_dirty_dx_node(handle, dir, frame->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1613 | if (err) |
| 1614 | goto journal_error; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1615 | brelse(bh2); |
| 1616 | dxtrace(dx_show_index("frame", frame->entries)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1617 | return de; |
Dmitriy Monakhov | fedee54 | 2007-05-08 00:25:34 -0700 | [diff] [blame] | 1618 | |
| 1619 | journal_error: |
| 1620 | brelse(*bh); |
| 1621 | brelse(bh2); |
| 1622 | *bh = NULL; |
| 1623 | ext4_std_error(dir->i_sb, err); |
| 1624 | errout: |
| 1625 | *error = err; |
| 1626 | return NULL; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1627 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1628 | |
Tao Ma | 978fef9 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 1629 | int ext4_find_dest_de(struct inode *dir, struct inode *inode, |
| 1630 | struct buffer_head *bh, |
| 1631 | void *buf, int buf_size, |
| 1632 | const char *name, int namelen, |
| 1633 | struct ext4_dir_entry_2 **dest_de) |
| 1634 | { |
| 1635 | struct ext4_dir_entry_2 *de; |
| 1636 | unsigned short reclen = EXT4_DIR_REC_LEN(namelen); |
| 1637 | int nlen, rlen; |
| 1638 | unsigned int offset = 0; |
| 1639 | char *top; |
| 1640 | |
| 1641 | de = (struct ext4_dir_entry_2 *)buf; |
| 1642 | top = buf + buf_size - reclen; |
| 1643 | while ((char *) de <= top) { |
| 1644 | if (ext4_check_dir_entry(dir, NULL, de, bh, |
| 1645 | buf, buf_size, offset)) |
| 1646 | return -EIO; |
| 1647 | if (ext4_match(namelen, name, de)) |
| 1648 | return -EEXIST; |
| 1649 | nlen = EXT4_DIR_REC_LEN(de->name_len); |
| 1650 | rlen = ext4_rec_len_from_disk(de->rec_len, buf_size); |
| 1651 | if ((de->inode ? rlen - nlen : rlen) >= reclen) |
| 1652 | break; |
| 1653 | de = (struct ext4_dir_entry_2 *)((char *)de + rlen); |
| 1654 | offset += rlen; |
| 1655 | } |
| 1656 | if ((char *) de > top) |
| 1657 | return -ENOSPC; |
| 1658 | |
| 1659 | *dest_de = de; |
| 1660 | return 0; |
| 1661 | } |
| 1662 | |
| 1663 | void ext4_insert_dentry(struct inode *inode, |
| 1664 | struct ext4_dir_entry_2 *de, |
| 1665 | int buf_size, |
| 1666 | const char *name, int namelen) |
| 1667 | { |
| 1668 | |
| 1669 | int nlen, rlen; |
| 1670 | |
| 1671 | nlen = EXT4_DIR_REC_LEN(de->name_len); |
| 1672 | rlen = ext4_rec_len_from_disk(de->rec_len, buf_size); |
| 1673 | if (de->inode) { |
| 1674 | struct ext4_dir_entry_2 *de1 = |
| 1675 | (struct ext4_dir_entry_2 *)((char *)de + nlen); |
| 1676 | de1->rec_len = ext4_rec_len_to_disk(rlen - nlen, buf_size); |
| 1677 | de->rec_len = ext4_rec_len_to_disk(nlen, buf_size); |
| 1678 | de = de1; |
| 1679 | } |
| 1680 | de->file_type = EXT4_FT_UNKNOWN; |
| 1681 | de->inode = cpu_to_le32(inode->i_ino); |
| 1682 | ext4_set_de_type(inode->i_sb, de, inode->i_mode); |
| 1683 | de->name_len = namelen; |
| 1684 | memcpy(de->name, name, namelen); |
| 1685 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1686 | /* |
| 1687 | * Add a new entry into a directory (leaf) block. If de is non-NULL, |
| 1688 | * it points to a directory entry which is guaranteed to be large |
| 1689 | * enough for new directory entry. If de is NULL, then |
| 1690 | * add_dirent_to_buf will attempt search the directory block for |
| 1691 | * space. It will return -ENOSPC if no space is available, and -EIO |
| 1692 | * and -EEXIST if directory entry already exists. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1693 | */ |
| 1694 | static int add_dirent_to_buf(handle_t *handle, struct dentry *dentry, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1695 | struct inode *inode, struct ext4_dir_entry_2 *de, |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1696 | struct buffer_head *bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1697 | { |
| 1698 | struct inode *dir = dentry->d_parent->d_inode; |
| 1699 | const char *name = dentry->d_name.name; |
| 1700 | int namelen = dentry->d_name.len; |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1701 | unsigned int blocksize = dir->i_sb->s_blocksize; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1702 | unsigned short reclen; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1703 | int csum_size = 0; |
Tao Ma | 978fef9 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 1704 | int err; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1705 | |
| 1706 | if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, |
| 1707 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) |
| 1708 | csum_size = sizeof(struct ext4_dir_entry_tail); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1709 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1710 | reclen = EXT4_DIR_REC_LEN(namelen); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1711 | if (!de) { |
Tao Ma | 978fef9 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 1712 | err = ext4_find_dest_de(dir, inode, |
| 1713 | bh, bh->b_data, blocksize - csum_size, |
| 1714 | name, namelen, &de); |
| 1715 | if (err) |
| 1716 | return err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1717 | } |
| 1718 | BUFFER_TRACE(bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1719 | err = ext4_journal_get_write_access(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1720 | if (err) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1721 | ext4_std_error(dir->i_sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1722 | return err; |
| 1723 | } |
| 1724 | |
| 1725 | /* By now the buffer is marked for journaling */ |
Tao Ma | 978fef9 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 1726 | ext4_insert_dentry(inode, de, blocksize, name, namelen); |
| 1727 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1728 | /* |
| 1729 | * XXX shouldn't update any times until successful |
| 1730 | * completion of syscall, but too many callers depend |
| 1731 | * on this. |
| 1732 | * |
| 1733 | * XXX similarly, too many callers depend on |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1734 | * ext4_new_inode() setting the times, but error |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1735 | * recovery deletes the inode, so the worst that can |
| 1736 | * happen is that the times are slightly out of date |
| 1737 | * and/or different from the directory change time. |
| 1738 | */ |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 1739 | dir->i_mtime = dir->i_ctime = ext4_current_time(dir); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1740 | ext4_update_dx_flag(dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1741 | dir->i_version++; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1742 | ext4_mark_inode_dirty(handle, dir); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 1743 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1744 | err = ext4_handle_dirty_dirent_node(handle, dir, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1745 | if (err) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1746 | ext4_std_error(dir->i_sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1747 | return 0; |
| 1748 | } |
| 1749 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1750 | /* |
| 1751 | * This converts a one block unindexed directory to a 3 block indexed |
| 1752 | * directory, and adds the dentry to the indexed directory. |
| 1753 | */ |
| 1754 | static int make_indexed_dir(handle_t *handle, struct dentry *dentry, |
| 1755 | struct inode *inode, struct buffer_head *bh) |
| 1756 | { |
| 1757 | struct inode *dir = dentry->d_parent->d_inode; |
| 1758 | const char *name = dentry->d_name.name; |
| 1759 | int namelen = dentry->d_name.len; |
| 1760 | struct buffer_head *bh2; |
| 1761 | struct dx_root *root; |
| 1762 | struct dx_frame frames[2], *frame; |
| 1763 | struct dx_entry *entries; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1764 | struct ext4_dir_entry_2 *de, *de2; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1765 | struct ext4_dir_entry_tail *t; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1766 | char *data1, *top; |
| 1767 | unsigned len; |
| 1768 | int retval; |
| 1769 | unsigned blocksize; |
| 1770 | struct dx_hash_info hinfo; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1771 | ext4_lblk_t block; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1772 | struct fake_dirent *fde; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1773 | int csum_size = 0; |
| 1774 | |
| 1775 | if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, |
| 1776 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) |
| 1777 | csum_size = sizeof(struct ext4_dir_entry_tail); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1778 | |
| 1779 | blocksize = dir->i_sb->s_blocksize; |
Theodore Ts'o | e6b8bc0 | 2009-01-16 11:13:40 -0500 | [diff] [blame] | 1780 | dxtrace(printk(KERN_DEBUG "Creating index: inode %lu\n", dir->i_ino)); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1781 | retval = ext4_journal_get_write_access(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1782 | if (retval) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1783 | ext4_std_error(dir->i_sb, retval); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1784 | brelse(bh); |
| 1785 | return retval; |
| 1786 | } |
| 1787 | root = (struct dx_root *) bh->b_data; |
| 1788 | |
Theodore Ts'o | e6b8bc0 | 2009-01-16 11:13:40 -0500 | [diff] [blame] | 1789 | /* The 0th block becomes the root, move the dirents out */ |
| 1790 | fde = &root->dotdot; |
| 1791 | de = (struct ext4_dir_entry_2 *)((char *)fde + |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1792 | ext4_rec_len_from_disk(fde->rec_len, blocksize)); |
Theodore Ts'o | e6b8bc0 | 2009-01-16 11:13:40 -0500 | [diff] [blame] | 1793 | if ((char *) de >= (((char *) root) + blocksize)) { |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 1794 | EXT4_ERROR_INODE(dir, "invalid rec_len for '..'"); |
Theodore Ts'o | e6b8bc0 | 2009-01-16 11:13:40 -0500 | [diff] [blame] | 1795 | brelse(bh); |
| 1796 | return -EIO; |
| 1797 | } |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1798 | len = ((char *) root) + (blocksize - csum_size) - (char *) de; |
Theodore Ts'o | e6b8bc0 | 2009-01-16 11:13:40 -0500 | [diff] [blame] | 1799 | |
| 1800 | /* Allocate new block for the 0th block's dirents */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1801 | bh2 = ext4_append(handle, dir, &block, &retval); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1802 | if (!(bh2)) { |
| 1803 | brelse(bh); |
| 1804 | return retval; |
| 1805 | } |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 1806 | ext4_set_inode_flag(dir, EXT4_INODE_INDEX); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1807 | data1 = bh2->b_data; |
| 1808 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1809 | memcpy (data1, de, len); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1810 | de = (struct ext4_dir_entry_2 *) data1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1811 | top = data1 + len; |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1812 | while ((char *)(de2 = ext4_next_entry(de, blocksize)) < top) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1813 | de = de2; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1814 | de->rec_len = ext4_rec_len_to_disk(data1 + (blocksize - csum_size) - |
| 1815 | (char *) de, |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1816 | blocksize); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1817 | |
| 1818 | if (csum_size) { |
| 1819 | t = EXT4_DIRENT_TAIL(data1, blocksize); |
| 1820 | initialize_dirent_tail(t, blocksize); |
| 1821 | } |
| 1822 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1823 | /* Initialize the root; the dot dirents already exist */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1824 | de = (struct ext4_dir_entry_2 *) (&root->dotdot); |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 1825 | de->rec_len = ext4_rec_len_to_disk(blocksize - EXT4_DIR_REC_LEN(2), |
| 1826 | blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1827 | memset (&root->info, 0, sizeof(root->info)); |
| 1828 | root->info.info_length = sizeof(root->info); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1829 | root->info.hash_version = EXT4_SB(dir->i_sb)->s_def_hash_version; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1830 | entries = root->entries; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1831 | dx_set_block(entries, 1); |
| 1832 | dx_set_count(entries, 1); |
| 1833 | dx_set_limit(entries, dx_root_limit(dir, sizeof(root->info))); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1834 | |
| 1835 | /* Initialize as for dx_probe */ |
| 1836 | hinfo.hash_version = root->info.hash_version; |
Theodore Ts'o | f99b258 | 2008-10-28 13:21:44 -0400 | [diff] [blame] | 1837 | if (hinfo.hash_version <= DX_HASH_TEA) |
| 1838 | hinfo.hash_version += EXT4_SB(dir->i_sb)->s_hash_unsigned; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1839 | hinfo.seed = EXT4_SB(dir->i_sb)->s_hash_seed; |
| 1840 | ext4fs_dirhash(name, namelen, &hinfo); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1841 | frame = frames; |
| 1842 | frame->entries = entries; |
| 1843 | frame->at = entries; |
| 1844 | frame->bh = bh; |
| 1845 | bh = bh2; |
Allison Henderson | 6976a6f | 2011-05-15 00:19:41 -0400 | [diff] [blame] | 1846 | |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 1847 | ext4_handle_dirty_dx_node(handle, dir, frame->bh); |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1848 | ext4_handle_dirty_dirent_node(handle, dir, bh); |
Allison Henderson | 6976a6f | 2011-05-15 00:19:41 -0400 | [diff] [blame] | 1849 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1850 | de = do_split(handle,dir, &bh, frame, &hinfo, &retval); |
Jan Kara | 7ad8e4e | 2011-05-03 11:05:55 -0400 | [diff] [blame] | 1851 | if (!de) { |
| 1852 | /* |
| 1853 | * Even if the block split failed, we have to properly write |
| 1854 | * out all the changes we did so far. Otherwise we can end up |
| 1855 | * with corrupted filesystem. |
| 1856 | */ |
| 1857 | ext4_mark_inode_dirty(handle, dir); |
Jan Kara | 7ad8e4e | 2011-05-03 11:05:55 -0400 | [diff] [blame] | 1858 | dx_release(frames); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1859 | return retval; |
Jan Kara | 7ad8e4e | 2011-05-03 11:05:55 -0400 | [diff] [blame] | 1860 | } |
| 1861 | dx_release(frames); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1862 | |
Theodore Ts'o | 2de770a | 2009-11-23 07:25:49 -0500 | [diff] [blame] | 1863 | retval = add_dirent_to_buf(handle, dentry, inode, de, bh); |
| 1864 | brelse(bh); |
| 1865 | return retval; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1866 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1867 | |
| 1868 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1869 | * ext4_add_entry() |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1870 | * |
| 1871 | * adds a file entry to the specified directory, using the same |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1872 | * semantics as ext4_find_entry(). It returns NULL if it failed. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1873 | * |
| 1874 | * NOTE!! The inode part of 'de' is left at 0 - which means you |
| 1875 | * may not sleep between calling this and putting something into |
| 1876 | * the entry, as someone else might have used it while you slept. |
| 1877 | */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1878 | static int ext4_add_entry(handle_t *handle, struct dentry *dentry, |
| 1879 | struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1880 | { |
| 1881 | struct inode *dir = dentry->d_parent->d_inode; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1882 | struct buffer_head *bh; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1883 | struct ext4_dir_entry_2 *de; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1884 | struct ext4_dir_entry_tail *t; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1885 | struct super_block *sb; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1886 | int retval; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1887 | int dx_fallback=0; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1888 | unsigned blocksize; |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 1889 | ext4_lblk_t block, blocks; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1890 | int csum_size = 0; |
| 1891 | |
| 1892 | if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, |
| 1893 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) |
| 1894 | csum_size = sizeof(struct ext4_dir_entry_tail); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1895 | |
| 1896 | sb = dir->i_sb; |
| 1897 | blocksize = sb->s_blocksize; |
| 1898 | if (!dentry->d_name.len) |
| 1899 | return -EINVAL; |
Tao Ma | 3c47d54 | 2012-12-10 14:05:59 -0500 | [diff] [blame] | 1900 | |
| 1901 | if (ext4_has_inline_data(dir)) { |
| 1902 | retval = ext4_try_add_inline_entry(handle, dentry, inode); |
| 1903 | if (retval < 0) |
| 1904 | return retval; |
| 1905 | if (retval == 1) { |
| 1906 | retval = 0; |
| 1907 | return retval; |
| 1908 | } |
| 1909 | } |
| 1910 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1911 | if (is_dx(dir)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1912 | retval = ext4_dx_add_entry(handle, dentry, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1913 | if (!retval || (retval != ERR_BAD_DX_DIR)) |
| 1914 | return retval; |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 1915 | ext4_clear_inode_flag(dir, EXT4_INODE_INDEX); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1916 | dx_fallback++; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1917 | ext4_mark_inode_dirty(handle, dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1918 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1919 | blocks = dir->i_size >> sb->s_blocksize_bits; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 1920 | for (block = 0; block < blocks; block++) { |
Carlos Maiolino | 6d1ab10 | 2012-09-27 09:31:33 -0400 | [diff] [blame] | 1921 | if (!(bh = ext4_bread(handle, dir, block, 0, &retval))) { |
| 1922 | if (!retval) { |
| 1923 | retval = -EIO; |
| 1924 | ext4_error(inode->i_sb, |
| 1925 | "Directory hole detected on inode %lu\n", |
| 1926 | inode->i_ino); |
| 1927 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1928 | return retval; |
Carlos Maiolino | 6d1ab10 | 2012-09-27 09:31:33 -0400 | [diff] [blame] | 1929 | } |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1930 | if (!buffer_verified(bh) && |
| 1931 | !ext4_dirent_csum_verify(dir, |
| 1932 | (struct ext4_dir_entry *)bh->b_data)) |
| 1933 | return -EIO; |
| 1934 | set_buffer_verified(bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1935 | retval = add_dirent_to_buf(handle, dentry, inode, NULL, bh); |
Theodore Ts'o | 2de770a | 2009-11-23 07:25:49 -0500 | [diff] [blame] | 1936 | if (retval != -ENOSPC) { |
| 1937 | brelse(bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1938 | return retval; |
Theodore Ts'o | 2de770a | 2009-11-23 07:25:49 -0500 | [diff] [blame] | 1939 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1940 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1941 | if (blocks == 1 && !dx_fallback && |
Theodore Ts'o | 1e424a348 | 2009-11-08 15:45:44 -0500 | [diff] [blame] | 1942 | EXT4_HAS_COMPAT_FEATURE(sb, EXT4_FEATURE_COMPAT_DIR_INDEX)) |
| 1943 | return make_indexed_dir(handle, dentry, inode, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1944 | brelse(bh); |
| 1945 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1946 | bh = ext4_append(handle, dir, &block, &retval); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1947 | if (!bh) |
| 1948 | return retval; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1949 | de = (struct ext4_dir_entry_2 *) bh->b_data; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1950 | de->inode = 0; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1951 | de->rec_len = ext4_rec_len_to_disk(blocksize - csum_size, blocksize); |
| 1952 | |
| 1953 | if (csum_size) { |
| 1954 | t = EXT4_DIRENT_TAIL(bh->b_data, blocksize); |
| 1955 | initialize_dirent_tail(t, blocksize); |
| 1956 | } |
| 1957 | |
Theodore Ts'o | 2de770a | 2009-11-23 07:25:49 -0500 | [diff] [blame] | 1958 | retval = add_dirent_to_buf(handle, dentry, inode, de, bh); |
| 1959 | brelse(bh); |
Frank Mayhar | 14ece10 | 2010-05-17 08:00:00 -0400 | [diff] [blame] | 1960 | if (retval == 0) |
| 1961 | ext4_set_inode_state(inode, EXT4_STATE_NEWENTRY); |
Theodore Ts'o | 2de770a | 2009-11-23 07:25:49 -0500 | [diff] [blame] | 1962 | return retval; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1963 | } |
| 1964 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1965 | /* |
| 1966 | * Returns 0 for success, or a negative error value |
| 1967 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1968 | static int ext4_dx_add_entry(handle_t *handle, struct dentry *dentry, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1969 | struct inode *inode) |
| 1970 | { |
| 1971 | struct dx_frame frames[2], *frame; |
| 1972 | struct dx_entry *entries, *at; |
| 1973 | struct dx_hash_info hinfo; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1974 | struct buffer_head *bh; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1975 | struct inode *dir = dentry->d_parent->d_inode; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 1976 | struct super_block *sb = dir->i_sb; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 1977 | struct ext4_dir_entry_2 *de; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1978 | int err; |
| 1979 | |
Theodore Ts'o | f702ba0 | 2008-09-22 15:21:01 -0400 | [diff] [blame] | 1980 | frame = dx_probe(&dentry->d_name, dir, &hinfo, frames, &err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1981 | if (!frame) |
| 1982 | return err; |
| 1983 | entries = frame->entries; |
| 1984 | at = frame->at; |
| 1985 | |
Carlos Maiolino | 6d1ab10 | 2012-09-27 09:31:33 -0400 | [diff] [blame] | 1986 | if (!(bh = ext4_bread(handle, dir, dx_get_block(frame->at), 0, &err))) { |
| 1987 | if (!err) { |
| 1988 | err = -EIO; |
| 1989 | ext4_error(dir->i_sb, |
| 1990 | "Directory hole detected on inode %lu\n", |
| 1991 | dir->i_ino); |
| 1992 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1993 | goto cleanup; |
Carlos Maiolino | 6d1ab10 | 2012-09-27 09:31:33 -0400 | [diff] [blame] | 1994 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 1995 | |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 1996 | if (!buffer_verified(bh) && |
| 1997 | !ext4_dirent_csum_verify(dir, (struct ext4_dir_entry *)bh->b_data)) |
| 1998 | goto journal_error; |
| 1999 | set_buffer_verified(bh); |
| 2000 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2001 | BUFFER_TRACE(bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2002 | err = ext4_journal_get_write_access(handle, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2003 | if (err) |
| 2004 | goto journal_error; |
| 2005 | |
| 2006 | err = add_dirent_to_buf(handle, dentry, inode, NULL, bh); |
Theodore Ts'o | 2de770a | 2009-11-23 07:25:49 -0500 | [diff] [blame] | 2007 | if (err != -ENOSPC) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2008 | goto cleanup; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2009 | |
| 2010 | /* Block full, should compress but for now just split */ |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 2011 | dxtrace(printk(KERN_DEBUG "using %u of %u node entries\n", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2012 | dx_get_count(entries), dx_get_limit(entries))); |
| 2013 | /* Need to split index? */ |
| 2014 | if (dx_get_count(entries) == dx_get_limit(entries)) { |
Aneesh Kumar K.V | 725d26d | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2015 | ext4_lblk_t newblock; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2016 | unsigned icount = dx_get_count(entries); |
| 2017 | int levels = frame - frames; |
| 2018 | struct dx_entry *entries2; |
| 2019 | struct dx_node *node2; |
| 2020 | struct buffer_head *bh2; |
| 2021 | |
| 2022 | if (levels && (dx_get_count(frames->entries) == |
| 2023 | dx_get_limit(frames->entries))) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 2024 | ext4_warning(sb, "Directory index full!"); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2025 | err = -ENOSPC; |
| 2026 | goto cleanup; |
| 2027 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2028 | bh2 = ext4_append (handle, dir, &newblock, &err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2029 | if (!(bh2)) |
| 2030 | goto cleanup; |
| 2031 | node2 = (struct dx_node *)(bh2->b_data); |
| 2032 | entries2 = node2->entries; |
Andreas Schlick | 1f7bebb | 2009-09-10 23:16:07 -0400 | [diff] [blame] | 2033 | memset(&node2->fake, 0, sizeof(struct fake_dirent)); |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 2034 | node2->fake.rec_len = ext4_rec_len_to_disk(sb->s_blocksize, |
| 2035 | sb->s_blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2036 | BUFFER_TRACE(frame->bh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2037 | err = ext4_journal_get_write_access(handle, frame->bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2038 | if (err) |
| 2039 | goto journal_error; |
| 2040 | if (levels) { |
| 2041 | unsigned icount1 = icount/2, icount2 = icount - icount1; |
| 2042 | unsigned hash2 = dx_get_hash(entries + icount1); |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 2043 | dxtrace(printk(KERN_DEBUG "Split index %i/%i\n", |
| 2044 | icount1, icount2)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2045 | |
| 2046 | BUFFER_TRACE(frame->bh, "get_write_access"); /* index root */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2047 | err = ext4_journal_get_write_access(handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2048 | frames[0].bh); |
| 2049 | if (err) |
| 2050 | goto journal_error; |
| 2051 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2052 | memcpy((char *) entries2, (char *) (entries + icount1), |
| 2053 | icount2 * sizeof(struct dx_entry)); |
| 2054 | dx_set_count(entries, icount1); |
| 2055 | dx_set_count(entries2, icount2); |
| 2056 | dx_set_limit(entries2, dx_node_limit(dir)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2057 | |
| 2058 | /* Which index block gets the new entry? */ |
| 2059 | if (at - entries >= icount1) { |
| 2060 | frame->at = at = at - entries - icount1 + entries2; |
| 2061 | frame->entries = entries = entries2; |
| 2062 | swap(frame->bh, bh2); |
| 2063 | } |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2064 | dx_insert_block(frames + 0, hash2, newblock); |
| 2065 | dxtrace(dx_show_index("node", frames[1].entries)); |
| 2066 | dxtrace(dx_show_index("node", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2067 | ((struct dx_node *) bh2->b_data)->entries)); |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 2068 | err = ext4_handle_dirty_dx_node(handle, dir, bh2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2069 | if (err) |
| 2070 | goto journal_error; |
| 2071 | brelse (bh2); |
| 2072 | } else { |
Theodore Ts'o | 4776004f | 2008-09-08 23:00:52 -0400 | [diff] [blame] | 2073 | dxtrace(printk(KERN_DEBUG |
| 2074 | "Creating second level index...\n")); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2075 | memcpy((char *) entries2, (char *) entries, |
| 2076 | icount * sizeof(struct dx_entry)); |
| 2077 | dx_set_limit(entries2, dx_node_limit(dir)); |
| 2078 | |
| 2079 | /* Set up root */ |
| 2080 | dx_set_count(entries, 1); |
| 2081 | dx_set_block(entries + 0, newblock); |
| 2082 | ((struct dx_root *) frames[0].bh->b_data)->info.indirect_levels = 1; |
| 2083 | |
| 2084 | /* Add new access path frame */ |
| 2085 | frame = frames + 1; |
| 2086 | frame->at = at = at - entries + entries2; |
| 2087 | frame->entries = entries = entries2; |
| 2088 | frame->bh = bh2; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2089 | err = ext4_journal_get_write_access(handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2090 | frame->bh); |
| 2091 | if (err) |
| 2092 | goto journal_error; |
| 2093 | } |
Darrick J. Wong | dbe8944 | 2012-04-29 18:39:10 -0400 | [diff] [blame] | 2094 | err = ext4_handle_dirty_dx_node(handle, dir, frames[0].bh); |
Theodore Ts'o | b409714 | 2011-01-10 12:46:59 -0500 | [diff] [blame] | 2095 | if (err) { |
| 2096 | ext4_std_error(inode->i_sb, err); |
| 2097 | goto cleanup; |
| 2098 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2099 | } |
| 2100 | de = do_split(handle, dir, &bh, frame, &hinfo, &err); |
| 2101 | if (!de) |
| 2102 | goto cleanup; |
| 2103 | err = add_dirent_to_buf(handle, dentry, inode, de, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2104 | goto cleanup; |
| 2105 | |
| 2106 | journal_error: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2107 | ext4_std_error(dir->i_sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2108 | cleanup: |
| 2109 | if (bh) |
| 2110 | brelse(bh); |
| 2111 | dx_release(frames); |
| 2112 | return err; |
| 2113 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2114 | |
| 2115 | /* |
Tao Ma | 05019a9 | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 2116 | * ext4_generic_delete_entry deletes a directory entry by merging it |
| 2117 | * with the previous entry |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2118 | */ |
Tao Ma | 05019a9 | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 2119 | int ext4_generic_delete_entry(handle_t *handle, |
| 2120 | struct inode *dir, |
| 2121 | struct ext4_dir_entry_2 *de_del, |
| 2122 | struct buffer_head *bh, |
| 2123 | void *entry_buf, |
| 2124 | int buf_size, |
| 2125 | int csum_size) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2126 | { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2127 | struct ext4_dir_entry_2 *de, *pde; |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 2128 | unsigned int blocksize = dir->i_sb->s_blocksize; |
Tao Ma | 05019a9 | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 2129 | int i; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2130 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2131 | i = 0; |
| 2132 | pde = NULL; |
Tao Ma | 05019a9 | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 2133 | de = (struct ext4_dir_entry_2 *)entry_buf; |
| 2134 | while (i < buf_size - csum_size) { |
Tao Ma | 226ba97 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 2135 | if (ext4_check_dir_entry(dir, NULL, de, bh, |
| 2136 | bh->b_data, bh->b_size, i)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2137 | return -EIO; |
| 2138 | if (de == de_del) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2139 | if (pde) |
Jan Kara | a72d7f8 | 2008-01-28 23:58:27 -0500 | [diff] [blame] | 2140 | pde->rec_len = ext4_rec_len_to_disk( |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 2141 | ext4_rec_len_from_disk(pde->rec_len, |
| 2142 | blocksize) + |
| 2143 | ext4_rec_len_from_disk(de->rec_len, |
| 2144 | blocksize), |
| 2145 | blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2146 | else |
| 2147 | de->inode = 0; |
| 2148 | dir->i_version++; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2149 | return 0; |
| 2150 | } |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 2151 | i += ext4_rec_len_from_disk(de->rec_len, blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2152 | pde = de; |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 2153 | de = ext4_next_entry(de, blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2154 | } |
| 2155 | return -ENOENT; |
| 2156 | } |
| 2157 | |
Tao Ma | 05019a9 | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 2158 | static int ext4_delete_entry(handle_t *handle, |
| 2159 | struct inode *dir, |
| 2160 | struct ext4_dir_entry_2 *de_del, |
| 2161 | struct buffer_head *bh) |
| 2162 | { |
| 2163 | int err, csum_size = 0; |
| 2164 | |
Tao Ma | 9f40fe5 | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 2165 | if (ext4_has_inline_data(dir)) { |
| 2166 | int has_inline_data = 1; |
| 2167 | err = ext4_delete_inline_entry(handle, dir, de_del, bh, |
| 2168 | &has_inline_data); |
| 2169 | if (has_inline_data) |
| 2170 | return err; |
| 2171 | } |
| 2172 | |
Tao Ma | 05019a9 | 2012-12-10 14:06:00 -0500 | [diff] [blame] | 2173 | if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb, |
| 2174 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) |
| 2175 | csum_size = sizeof(struct ext4_dir_entry_tail); |
| 2176 | |
| 2177 | BUFFER_TRACE(bh, "get_write_access"); |
| 2178 | err = ext4_journal_get_write_access(handle, bh); |
| 2179 | if (unlikely(err)) |
| 2180 | goto out; |
| 2181 | |
| 2182 | err = ext4_generic_delete_entry(handle, dir, de_del, |
| 2183 | bh, bh->b_data, |
| 2184 | dir->i_sb->s_blocksize, csum_size); |
| 2185 | if (err) |
| 2186 | goto out; |
| 2187 | |
| 2188 | BUFFER_TRACE(bh, "call ext4_handle_dirty_metadata"); |
| 2189 | err = ext4_handle_dirty_dirent_node(handle, dir, bh); |
| 2190 | if (unlikely(err)) |
| 2191 | goto out; |
| 2192 | |
| 2193 | return 0; |
| 2194 | out: |
| 2195 | if (err != -ENOENT) |
| 2196 | ext4_std_error(dir->i_sb, err); |
| 2197 | return err; |
| 2198 | } |
| 2199 | |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 2200 | /* |
| 2201 | * DIR_NLINK feature is set if 1) nlinks > EXT4_LINK_MAX or 2) nlinks == 2, |
| 2202 | * since this indicates that nlinks count was previously 1. |
| 2203 | */ |
| 2204 | static void ext4_inc_count(handle_t *handle, struct inode *inode) |
| 2205 | { |
| 2206 | inc_nlink(inode); |
| 2207 | if (is_dx(inode) && inode->i_nlink > 1) { |
| 2208 | /* limit is 16-bit i_links_count */ |
| 2209 | if (inode->i_nlink >= EXT4_LINK_MAX || inode->i_nlink == 2) { |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 2210 | set_nlink(inode, 1); |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 2211 | EXT4_SET_RO_COMPAT_FEATURE(inode->i_sb, |
| 2212 | EXT4_FEATURE_RO_COMPAT_DIR_NLINK); |
| 2213 | } |
| 2214 | } |
| 2215 | } |
| 2216 | |
| 2217 | /* |
| 2218 | * If a directory had nlink == 1, then we should let it be 1. This indicates |
| 2219 | * directory has >EXT4_LINK_MAX subdirs. |
| 2220 | */ |
| 2221 | static void ext4_dec_count(handle_t *handle, struct inode *inode) |
| 2222 | { |
Andreas Dilger | 909a4cf | 2011-10-26 03:22:31 -0400 | [diff] [blame] | 2223 | if (!S_ISDIR(inode->i_mode) || inode->i_nlink > 2) |
| 2224 | drop_nlink(inode); |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 2225 | } |
| 2226 | |
| 2227 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2228 | static int ext4_add_nondir(handle_t *handle, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2229 | struct dentry *dentry, struct inode *inode) |
| 2230 | { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2231 | int err = ext4_add_entry(handle, dentry, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2232 | if (!err) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2233 | ext4_mark_inode_dirty(handle, inode); |
Al Viro | 6b38e84 | 2008-12-30 02:03:31 -0500 | [diff] [blame] | 2234 | unlock_new_inode(inode); |
Al Viro | 8fc37ec | 2012-07-19 09:18:15 +0400 | [diff] [blame] | 2235 | d_instantiate(dentry, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2236 | return 0; |
| 2237 | } |
Eric Sandeen | 731b9a5 | 2007-02-10 01:46:16 -0800 | [diff] [blame] | 2238 | drop_nlink(inode); |
Al Viro | 6b38e84 | 2008-12-30 02:03:31 -0500 | [diff] [blame] | 2239 | unlock_new_inode(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2240 | iput(inode); |
| 2241 | return err; |
| 2242 | } |
| 2243 | |
| 2244 | /* |
| 2245 | * By the time this is called, we already have created |
| 2246 | * the directory cache entry for the new file, but it |
| 2247 | * is so far negative - it has no inode. |
| 2248 | * |
| 2249 | * If the create succeeds, we fill in the inode information |
| 2250 | * with d_instantiate(). |
| 2251 | */ |
Al Viro | 4acdaf2 | 2011-07-26 01:42:34 -0400 | [diff] [blame] | 2252 | static int ext4_create(struct inode *dir, struct dentry *dentry, umode_t mode, |
Al Viro | ebfc3b4 | 2012-06-10 18:05:36 -0400 | [diff] [blame] | 2253 | bool excl) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2254 | { |
| 2255 | handle_t *handle; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2256 | struct inode *inode; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2257 | int err, retries = 0; |
| 2258 | |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 2259 | dquot_initialize(dir); |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 2260 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2261 | retry: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2262 | handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + |
| 2263 | EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 + |
Dmitry Monakhov | 5aca07e | 2009-12-08 22:42:15 -0500 | [diff] [blame] | 2264 | EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2265 | if (IS_ERR(handle)) |
| 2266 | return PTR_ERR(handle); |
| 2267 | |
| 2268 | if (IS_DIRSYNC(dir)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2269 | ext4_handle_sync(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2270 | |
Dmitry Monakhov | 5cb81da | 2011-10-29 09:05:00 -0400 | [diff] [blame] | 2271 | inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2272 | err = PTR_ERR(inode); |
| 2273 | if (!IS_ERR(inode)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2274 | inode->i_op = &ext4_file_inode_operations; |
| 2275 | inode->i_fop = &ext4_file_operations; |
| 2276 | ext4_set_aops(inode); |
| 2277 | err = ext4_add_nondir(handle, dentry, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2278 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2279 | ext4_journal_stop(handle); |
| 2280 | if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2281 | goto retry; |
| 2282 | return err; |
| 2283 | } |
| 2284 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2285 | static int ext4_mknod(struct inode *dir, struct dentry *dentry, |
Al Viro | 1a67aaf | 2011-07-26 01:52:52 -0400 | [diff] [blame] | 2286 | umode_t mode, dev_t rdev) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2287 | { |
| 2288 | handle_t *handle; |
| 2289 | struct inode *inode; |
| 2290 | int err, retries = 0; |
| 2291 | |
| 2292 | if (!new_valid_dev(rdev)) |
| 2293 | return -EINVAL; |
| 2294 | |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 2295 | dquot_initialize(dir); |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 2296 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2297 | retry: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2298 | handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + |
| 2299 | EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 + |
Dmitry Monakhov | 5aca07e | 2009-12-08 22:42:15 -0500 | [diff] [blame] | 2300 | EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2301 | if (IS_ERR(handle)) |
| 2302 | return PTR_ERR(handle); |
| 2303 | |
| 2304 | if (IS_DIRSYNC(dir)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2305 | ext4_handle_sync(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2306 | |
Dmitry Monakhov | 5cb81da | 2011-10-29 09:05:00 -0400 | [diff] [blame] | 2307 | inode = ext4_new_inode(handle, dir, mode, &dentry->d_name, 0, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2308 | err = PTR_ERR(inode); |
| 2309 | if (!IS_ERR(inode)) { |
| 2310 | init_special_inode(inode, inode->i_mode, rdev); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2311 | inode->i_op = &ext4_special_inode_operations; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2312 | err = ext4_add_nondir(handle, dentry, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2313 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2314 | ext4_journal_stop(handle); |
| 2315 | if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2316 | goto retry; |
| 2317 | return err; |
| 2318 | } |
| 2319 | |
Tao Ma | a774f9c | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 2320 | struct ext4_dir_entry_2 *ext4_init_dot_dotdot(struct inode *inode, |
| 2321 | struct ext4_dir_entry_2 *de, |
| 2322 | int blocksize, int csum_size, |
| 2323 | unsigned int parent_ino, int dotdot_real_len) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2324 | { |
Tao Ma | a774f9c | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 2325 | de->inode = cpu_to_le32(inode->i_ino); |
| 2326 | de->name_len = 1; |
| 2327 | de->rec_len = ext4_rec_len_to_disk(EXT4_DIR_REC_LEN(de->name_len), |
| 2328 | blocksize); |
| 2329 | strcpy(de->name, "."); |
| 2330 | ext4_set_de_type(inode->i_sb, de, S_IFDIR); |
| 2331 | |
| 2332 | de = ext4_next_entry(de, blocksize); |
| 2333 | de->inode = cpu_to_le32(parent_ino); |
| 2334 | de->name_len = 2; |
| 2335 | if (!dotdot_real_len) |
| 2336 | de->rec_len = ext4_rec_len_to_disk(blocksize - |
| 2337 | (csum_size + EXT4_DIR_REC_LEN(1)), |
| 2338 | blocksize); |
| 2339 | else |
| 2340 | de->rec_len = ext4_rec_len_to_disk( |
| 2341 | EXT4_DIR_REC_LEN(de->name_len), blocksize); |
| 2342 | strcpy(de->name, ".."); |
| 2343 | ext4_set_de_type(inode->i_sb, de, S_IFDIR); |
| 2344 | |
| 2345 | return ext4_next_entry(de, blocksize); |
| 2346 | } |
| 2347 | |
| 2348 | static int ext4_init_new_dir(handle_t *handle, struct inode *dir, |
| 2349 | struct inode *inode) |
| 2350 | { |
Namhyung Kim | dabd991 | 2011-01-10 12:11:16 -0500 | [diff] [blame] | 2351 | struct buffer_head *dir_block = NULL; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2352 | struct ext4_dir_entry_2 *de; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2353 | struct ext4_dir_entry_tail *t; |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 2354 | unsigned int blocksize = dir->i_sb->s_blocksize; |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2355 | int csum_size = 0; |
Tao Ma | a774f9c | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 2356 | int err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2357 | |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2358 | if (EXT4_HAS_RO_COMPAT_FEATURE(dir->i_sb, |
| 2359 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) |
| 2360 | csum_size = sizeof(struct ext4_dir_entry_tail); |
| 2361 | |
Tao Ma | 3c47d54 | 2012-12-10 14:05:59 -0500 | [diff] [blame] | 2362 | if (ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { |
| 2363 | err = ext4_try_create_inline_dir(handle, dir, inode); |
| 2364 | if (err < 0 && err != -ENOSPC) |
| 2365 | goto out; |
| 2366 | if (!err) |
| 2367 | goto out; |
| 2368 | } |
| 2369 | |
Tao Ma | a774f9c | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 2370 | inode->i_size = EXT4_I(inode)->i_disksize = blocksize; |
| 2371 | dir_block = ext4_bread(handle, inode, 0, 1, &err); |
| 2372 | if (!(dir_block = ext4_bread(handle, inode, 0, 1, &err))) { |
| 2373 | if (!err) { |
| 2374 | err = -EIO; |
| 2375 | ext4_error(inode->i_sb, |
| 2376 | "Directory hole detected on inode %lu\n", |
| 2377 | inode->i_ino); |
| 2378 | } |
| 2379 | goto out; |
| 2380 | } |
| 2381 | BUFFER_TRACE(dir_block, "get_write_access"); |
| 2382 | err = ext4_journal_get_write_access(handle, dir_block); |
| 2383 | if (err) |
| 2384 | goto out; |
| 2385 | de = (struct ext4_dir_entry_2 *)dir_block->b_data; |
| 2386 | ext4_init_dot_dotdot(inode, de, blocksize, csum_size, dir->i_ino, 0); |
| 2387 | set_nlink(inode, 2); |
| 2388 | if (csum_size) { |
| 2389 | t = EXT4_DIRENT_TAIL(dir_block->b_data, blocksize); |
| 2390 | initialize_dirent_tail(t, blocksize); |
| 2391 | } |
| 2392 | |
| 2393 | BUFFER_TRACE(dir_block, "call ext4_handle_dirty_metadata"); |
| 2394 | err = ext4_handle_dirty_dirent_node(handle, inode, dir_block); |
| 2395 | if (err) |
| 2396 | goto out; |
| 2397 | set_buffer_verified(dir_block); |
| 2398 | out: |
| 2399 | brelse(dir_block); |
| 2400 | return err; |
| 2401 | } |
| 2402 | |
| 2403 | static int ext4_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode) |
| 2404 | { |
| 2405 | handle_t *handle; |
| 2406 | struct inode *inode; |
| 2407 | int err, retries = 0; |
| 2408 | |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 2409 | if (EXT4_DIR_LINK_MAX(dir)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2410 | return -EMLINK; |
| 2411 | |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 2412 | dquot_initialize(dir); |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 2413 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2414 | retry: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2415 | handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + |
| 2416 | EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 + |
Dmitry Monakhov | 5aca07e | 2009-12-08 22:42:15 -0500 | [diff] [blame] | 2417 | EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2418 | if (IS_ERR(handle)) |
| 2419 | return PTR_ERR(handle); |
| 2420 | |
| 2421 | if (IS_DIRSYNC(dir)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2422 | ext4_handle_sync(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2423 | |
Andreas Dilger | 1101391 | 2009-06-13 11:45:35 -0400 | [diff] [blame] | 2424 | inode = ext4_new_inode(handle, dir, S_IFDIR | mode, |
Dmitry Monakhov | 5cb81da | 2011-10-29 09:05:00 -0400 | [diff] [blame] | 2425 | &dentry->d_name, 0, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2426 | err = PTR_ERR(inode); |
| 2427 | if (IS_ERR(inode)) |
| 2428 | goto out_stop; |
| 2429 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2430 | inode->i_op = &ext4_dir_inode_operations; |
| 2431 | inode->i_fop = &ext4_dir_operations; |
Tao Ma | a774f9c | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 2432 | err = ext4_init_new_dir(handle, dir, inode); |
Namhyung Kim | dabd991 | 2011-01-10 12:11:16 -0500 | [diff] [blame] | 2433 | if (err) |
| 2434 | goto out_clear_inode; |
Namhyung Kim | dabd991 | 2011-01-10 12:11:16 -0500 | [diff] [blame] | 2435 | err = ext4_mark_inode_dirty(handle, inode); |
| 2436 | if (!err) |
| 2437 | err = ext4_add_entry(handle, dentry, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2438 | if (err) { |
Aneesh Kumar K.V | 4cdeed8 | 2008-02-22 06:17:31 -0500 | [diff] [blame] | 2439 | out_clear_inode: |
| 2440 | clear_nlink(inode); |
Al Viro | 6b38e84 | 2008-12-30 02:03:31 -0500 | [diff] [blame] | 2441 | unlock_new_inode(inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2442 | ext4_mark_inode_dirty(handle, inode); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2443 | iput(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2444 | goto out_stop; |
| 2445 | } |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 2446 | ext4_inc_count(handle, dir); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2447 | ext4_update_dx_flag(dir); |
Namhyung Kim | dabd991 | 2011-01-10 12:11:16 -0500 | [diff] [blame] | 2448 | err = ext4_mark_inode_dirty(handle, dir); |
| 2449 | if (err) |
| 2450 | goto out_clear_inode; |
Al Viro | 6b38e84 | 2008-12-30 02:03:31 -0500 | [diff] [blame] | 2451 | unlock_new_inode(inode); |
Al Viro | 8fc37ec | 2012-07-19 09:18:15 +0400 | [diff] [blame] | 2452 | d_instantiate(dentry, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2453 | out_stop: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2454 | ext4_journal_stop(handle); |
| 2455 | if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2456 | goto retry; |
| 2457 | return err; |
| 2458 | } |
| 2459 | |
| 2460 | /* |
| 2461 | * routine to check that the specified directory is empty (for rmdir) |
| 2462 | */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2463 | static int empty_dir(struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2464 | { |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2465 | unsigned int offset; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2466 | struct buffer_head *bh; |
| 2467 | struct ext4_dir_entry_2 *de, *de1; |
| 2468 | struct super_block *sb; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2469 | int err = 0; |
| 2470 | |
Tao Ma | 61f8663 | 2012-12-10 14:06:01 -0500 | [diff] [blame] | 2471 | if (ext4_has_inline_data(inode)) { |
| 2472 | int has_inline_data = 1; |
| 2473 | |
| 2474 | err = empty_inline_dir(inode, &has_inline_data); |
| 2475 | if (has_inline_data) |
| 2476 | return err; |
| 2477 | } |
| 2478 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2479 | sb = inode->i_sb; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2480 | if (inode->i_size < EXT4_DIR_REC_LEN(1) + EXT4_DIR_REC_LEN(2) || |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2481 | !(bh = ext4_bread(NULL, inode, 0, 0, &err))) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2482 | if (err) |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 2483 | EXT4_ERROR_INODE(inode, |
| 2484 | "error %d reading directory lblock 0", err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2485 | else |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 2486 | ext4_warning(inode->i_sb, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2487 | "bad directory (dir #%lu) - no data block", |
| 2488 | inode->i_ino); |
| 2489 | return 1; |
| 2490 | } |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2491 | if (!buffer_verified(bh) && |
| 2492 | !ext4_dirent_csum_verify(inode, |
| 2493 | (struct ext4_dir_entry *)bh->b_data)) { |
| 2494 | EXT4_ERROR_INODE(inode, "checksum error reading directory " |
| 2495 | "lblock 0"); |
| 2496 | return -EIO; |
| 2497 | } |
| 2498 | set_buffer_verified(bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2499 | de = (struct ext4_dir_entry_2 *) bh->b_data; |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 2500 | de1 = ext4_next_entry(de, sb->s_blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2501 | if (le32_to_cpu(de->inode) != inode->i_ino || |
| 2502 | !le32_to_cpu(de1->inode) || |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2503 | strcmp(".", de->name) || |
| 2504 | strcmp("..", de1->name)) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 2505 | ext4_warning(inode->i_sb, |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2506 | "bad directory (dir #%lu) - no `.' or `..'", |
| 2507 | inode->i_ino); |
| 2508 | brelse(bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2509 | return 1; |
| 2510 | } |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 2511 | offset = ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize) + |
| 2512 | ext4_rec_len_from_disk(de1->rec_len, sb->s_blocksize); |
| 2513 | de = ext4_next_entry(de1, sb->s_blocksize); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2514 | while (offset < inode->i_size) { |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2515 | if (!bh || |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 2516 | (void *) de >= (void *) (bh->b_data+sb->s_blocksize)) { |
| 2517 | unsigned int lblock; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2518 | err = 0; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2519 | brelse(bh); |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 2520 | lblock = offset >> EXT4_BLOCK_SIZE_BITS(sb); |
| 2521 | bh = ext4_bread(NULL, inode, lblock, 0, &err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2522 | if (!bh) { |
| 2523 | if (err) |
Theodore Ts'o | 24676da | 2010-05-16 21:00:00 -0400 | [diff] [blame] | 2524 | EXT4_ERROR_INODE(inode, |
| 2525 | "error %d reading directory " |
| 2526 | "lblock %u", err, lblock); |
Carlos Maiolino | 6d1ab10 | 2012-09-27 09:31:33 -0400 | [diff] [blame] | 2527 | else |
| 2528 | ext4_warning(inode->i_sb, |
| 2529 | "bad directory (dir #%lu) - no data block", |
| 2530 | inode->i_ino); |
| 2531 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2532 | offset += sb->s_blocksize; |
| 2533 | continue; |
| 2534 | } |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 2535 | if (!buffer_verified(bh) && |
| 2536 | !ext4_dirent_csum_verify(inode, |
| 2537 | (struct ext4_dir_entry *)bh->b_data)) { |
| 2538 | EXT4_ERROR_INODE(inode, "checksum error " |
| 2539 | "reading directory lblock 0"); |
| 2540 | return -EIO; |
| 2541 | } |
| 2542 | set_buffer_verified(bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2543 | de = (struct ext4_dir_entry_2 *) bh->b_data; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2544 | } |
Tao Ma | 226ba97 | 2012-12-10 14:05:58 -0500 | [diff] [blame] | 2545 | if (ext4_check_dir_entry(inode, NULL, de, bh, |
| 2546 | bh->b_data, bh->b_size, offset)) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2547 | de = (struct ext4_dir_entry_2 *)(bh->b_data + |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2548 | sb->s_blocksize); |
| 2549 | offset = (offset | (sb->s_blocksize - 1)) + 1; |
| 2550 | continue; |
| 2551 | } |
| 2552 | if (le32_to_cpu(de->inode)) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2553 | brelse(bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2554 | return 0; |
| 2555 | } |
Wei Yongjun | 3d0518f | 2009-02-14 23:01:36 -0500 | [diff] [blame] | 2556 | offset += ext4_rec_len_from_disk(de->rec_len, sb->s_blocksize); |
| 2557 | de = ext4_next_entry(de, sb->s_blocksize); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2558 | } |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2559 | brelse(bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2560 | return 1; |
| 2561 | } |
| 2562 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2563 | /* ext4_orphan_add() links an unlinked or truncated inode into a list of |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2564 | * such inodes, starting at the superblock, in case we crash before the |
| 2565 | * file is closed/deleted, or in case the inode truncate spans multiple |
| 2566 | * transactions and the last transaction is not recovered after a crash. |
| 2567 | * |
| 2568 | * At filesystem recovery time, we walk this list deleting unlinked |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2569 | * inodes and truncating linked inodes in ext4_orphan_cleanup(). |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2570 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2571 | int ext4_orphan_add(handle_t *handle, struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2572 | { |
| 2573 | struct super_block *sb = inode->i_sb; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2574 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2575 | int err = 0, rc; |
| 2576 | |
Anatol Pomozov | c9b92530 | 2012-09-18 13:38:59 -0400 | [diff] [blame] | 2577 | if (!EXT4_SB(sb)->s_journal) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2578 | return 0; |
| 2579 | |
Theodore Ts'o | 3b9d4ed | 2009-04-25 22:54:04 -0400 | [diff] [blame] | 2580 | mutex_lock(&EXT4_SB(sb)->s_orphan_lock); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2581 | if (!list_empty(&EXT4_I(inode)->i_orphan)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2582 | goto out_unlock; |
| 2583 | |
Lukas Czerner | afb8617 | 2011-07-11 18:47:04 -0400 | [diff] [blame] | 2584 | /* |
| 2585 | * Orphan handling is only valid for files with data blocks |
| 2586 | * being truncated, or files being unlinked. Note that we either |
| 2587 | * hold i_mutex, or the inode can not be referenced from outside, |
| 2588 | * so i_nlink should not be bumped due to race |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2589 | */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2590 | J_ASSERT((S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) || |
| 2591 | S_ISLNK(inode->i_mode)) || inode->i_nlink == 0); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2592 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2593 | BUFFER_TRACE(EXT4_SB(sb)->s_sbh, "get_write_access"); |
| 2594 | err = ext4_journal_get_write_access(handle, EXT4_SB(sb)->s_sbh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2595 | if (err) |
| 2596 | goto out_unlock; |
| 2597 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2598 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2599 | if (err) |
| 2600 | goto out_unlock; |
Dmitry Monakhov | 6e3617e | 2010-03-01 23:29:39 -0500 | [diff] [blame] | 2601 | /* |
| 2602 | * Due to previous errors inode may be already a part of on-disk |
| 2603 | * orphan list. If so skip on-disk list modification. |
| 2604 | */ |
| 2605 | if (NEXT_ORPHAN(inode) && NEXT_ORPHAN(inode) <= |
| 2606 | (le32_to_cpu(EXT4_SB(sb)->s_es->s_inodes_count))) |
| 2607 | goto mem_insert; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2608 | |
| 2609 | /* Insert this inode at the head of the on-disk orphan list... */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2610 | NEXT_ORPHAN(inode) = le32_to_cpu(EXT4_SB(sb)->s_es->s_last_orphan); |
| 2611 | EXT4_SB(sb)->s_es->s_last_orphan = cpu_to_le32(inode->i_ino); |
Artem Bityutskiy | b50924c | 2012-07-22 20:37:31 -0400 | [diff] [blame] | 2612 | err = ext4_handle_dirty_super(handle, sb); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2613 | rc = ext4_mark_iloc_dirty(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2614 | if (!err) |
| 2615 | err = rc; |
| 2616 | |
| 2617 | /* Only add to the head of the in-memory list if all the |
| 2618 | * previous operations succeeded. If the orphan_add is going to |
| 2619 | * fail (possibly taking the journal offline), we can't risk |
| 2620 | * leaving the inode on the orphan list: stray orphan-list |
| 2621 | * entries can cause panics at unmount time. |
| 2622 | * |
| 2623 | * This is safe: on error we're going to ignore the orphan list |
| 2624 | * anyway on the next recovery. */ |
Dmitry Monakhov | 6e3617e | 2010-03-01 23:29:39 -0500 | [diff] [blame] | 2625 | mem_insert: |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2626 | if (!err) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2627 | list_add(&EXT4_I(inode)->i_orphan, &EXT4_SB(sb)->s_orphan); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2628 | |
| 2629 | jbd_debug(4, "superblock will point to %lu\n", inode->i_ino); |
| 2630 | jbd_debug(4, "orphan inode %lu will point to %d\n", |
| 2631 | inode->i_ino, NEXT_ORPHAN(inode)); |
| 2632 | out_unlock: |
Theodore Ts'o | 3b9d4ed | 2009-04-25 22:54:04 -0400 | [diff] [blame] | 2633 | mutex_unlock(&EXT4_SB(sb)->s_orphan_lock); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2634 | ext4_std_error(inode->i_sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2635 | return err; |
| 2636 | } |
| 2637 | |
| 2638 | /* |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2639 | * ext4_orphan_del() removes an unlinked or truncated inode from the list |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2640 | * of such inodes stored on disk, because it is finally being cleaned up. |
| 2641 | */ |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2642 | int ext4_orphan_del(handle_t *handle, struct inode *inode) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2643 | { |
| 2644 | struct list_head *prev; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2645 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 2646 | struct ext4_sb_info *sbi; |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2647 | __u32 ino_next; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2648 | struct ext4_iloc iloc; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2649 | int err = 0; |
| 2650 | |
Anatol Pomozov | c9b92530 | 2012-09-18 13:38:59 -0400 | [diff] [blame] | 2651 | if (!EXT4_SB(inode->i_sb)->s_journal) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2652 | return 0; |
| 2653 | |
Theodore Ts'o | 3b9d4ed | 2009-04-25 22:54:04 -0400 | [diff] [blame] | 2654 | mutex_lock(&EXT4_SB(inode->i_sb)->s_orphan_lock); |
| 2655 | if (list_empty(&ei->i_orphan)) |
| 2656 | goto out; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2657 | |
| 2658 | ino_next = NEXT_ORPHAN(inode); |
| 2659 | prev = ei->i_orphan.prev; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2660 | sbi = EXT4_SB(inode->i_sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2661 | |
| 2662 | jbd_debug(4, "remove inode %lu from orphan list\n", inode->i_ino); |
| 2663 | |
| 2664 | list_del_init(&ei->i_orphan); |
| 2665 | |
| 2666 | /* If we're on an error path, we may not have a valid |
| 2667 | * transaction handle with which to update the orphan list on |
| 2668 | * disk, but we still need to remove the inode from the linked |
| 2669 | * list in memory. */ |
Anatol Pomozov | c9b92530 | 2012-09-18 13:38:59 -0400 | [diff] [blame] | 2670 | if (!handle) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2671 | goto out; |
| 2672 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2673 | err = ext4_reserve_inode_write(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2674 | if (err) |
| 2675 | goto out_err; |
| 2676 | |
| 2677 | if (prev == &sbi->s_orphan) { |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2678 | jbd_debug(4, "superblock will point to %u\n", ino_next); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2679 | BUFFER_TRACE(sbi->s_sbh, "get_write_access"); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2680 | err = ext4_journal_get_write_access(handle, sbi->s_sbh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2681 | if (err) |
| 2682 | goto out_brelse; |
| 2683 | sbi->s_es->s_last_orphan = cpu_to_le32(ino_next); |
Artem Bityutskiy | b50924c | 2012-07-22 20:37:31 -0400 | [diff] [blame] | 2684 | err = ext4_handle_dirty_super(handle, inode->i_sb); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2685 | } else { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2686 | struct ext4_iloc iloc2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2687 | struct inode *i_prev = |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2688 | &list_entry(prev, struct ext4_inode_info, i_orphan)->vfs_inode; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2689 | |
Theodore Ts'o | 498e5f2 | 2008-11-05 00:14:04 -0500 | [diff] [blame] | 2690 | jbd_debug(4, "orphan inode %lu will point to %u\n", |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2691 | i_prev->i_ino, ino_next); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2692 | err = ext4_reserve_inode_write(handle, i_prev, &iloc2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2693 | if (err) |
| 2694 | goto out_brelse; |
| 2695 | NEXT_ORPHAN(i_prev) = ino_next; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2696 | err = ext4_mark_iloc_dirty(handle, i_prev, &iloc2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2697 | } |
| 2698 | if (err) |
| 2699 | goto out_brelse; |
| 2700 | NEXT_ORPHAN(inode) = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2701 | err = ext4_mark_iloc_dirty(handle, inode, &iloc); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2702 | |
| 2703 | out_err: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2704 | ext4_std_error(inode->i_sb, err); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2705 | out: |
Theodore Ts'o | 3b9d4ed | 2009-04-25 22:54:04 -0400 | [diff] [blame] | 2706 | mutex_unlock(&EXT4_SB(inode->i_sb)->s_orphan_lock); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2707 | return err; |
| 2708 | |
| 2709 | out_brelse: |
| 2710 | brelse(iloc.bh); |
| 2711 | goto out_err; |
| 2712 | } |
| 2713 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2714 | static int ext4_rmdir(struct inode *dir, struct dentry *dentry) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2715 | { |
| 2716 | int retval; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2717 | struct inode *inode; |
| 2718 | struct buffer_head *bh; |
| 2719 | struct ext4_dir_entry_2 *de; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2720 | handle_t *handle; |
| 2721 | |
| 2722 | /* Initialize quotas before so that eventual writes go in |
| 2723 | * separate transaction */ |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 2724 | dquot_initialize(dir); |
| 2725 | dquot_initialize(dentry->d_inode); |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 2726 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2727 | handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2728 | if (IS_ERR(handle)) |
| 2729 | return PTR_ERR(handle); |
| 2730 | |
| 2731 | retval = -ENOENT; |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame^] | 2732 | bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2733 | if (!bh) |
| 2734 | goto end_rmdir; |
| 2735 | |
| 2736 | if (IS_DIRSYNC(dir)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2737 | ext4_handle_sync(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2738 | |
| 2739 | inode = dentry->d_inode; |
| 2740 | |
| 2741 | retval = -EIO; |
| 2742 | if (le32_to_cpu(de->inode) != inode->i_ino) |
| 2743 | goto end_rmdir; |
| 2744 | |
| 2745 | retval = -ENOTEMPTY; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2746 | if (!empty_dir(inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2747 | goto end_rmdir; |
| 2748 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2749 | retval = ext4_delete_entry(handle, dir, de, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2750 | if (retval) |
| 2751 | goto end_rmdir; |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 2752 | if (!EXT4_DIR_LINK_EMPTY(inode)) |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 2753 | ext4_warning(inode->i_sb, |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2754 | "empty directory has too many links (%d)", |
| 2755 | inode->i_nlink); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2756 | inode->i_version++; |
| 2757 | clear_nlink(inode); |
| 2758 | /* There's no need to set i_disksize: the fact that i_nlink is |
| 2759 | * zero will ensure that the right thing happens during any |
| 2760 | * recovery. */ |
| 2761 | inode->i_size = 0; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2762 | ext4_orphan_add(handle, inode); |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 2763 | inode->i_ctime = dir->i_ctime = dir->i_mtime = ext4_current_time(inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2764 | ext4_mark_inode_dirty(handle, inode); |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 2765 | ext4_dec_count(handle, dir); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2766 | ext4_update_dx_flag(dir); |
| 2767 | ext4_mark_inode_dirty(handle, dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2768 | |
| 2769 | end_rmdir: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2770 | ext4_journal_stop(handle); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2771 | brelse(bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2772 | return retval; |
| 2773 | } |
| 2774 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2775 | static int ext4_unlink(struct inode *dir, struct dentry *dentry) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2776 | { |
| 2777 | int retval; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2778 | struct inode *inode; |
| 2779 | struct buffer_head *bh; |
| 2780 | struct ext4_dir_entry_2 *de; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2781 | handle_t *handle; |
| 2782 | |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 2783 | trace_ext4_unlink_enter(dir, dentry); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2784 | /* Initialize quotas before so that eventual writes go |
| 2785 | * in separate transaction */ |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 2786 | dquot_initialize(dir); |
| 2787 | dquot_initialize(dentry->d_inode); |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 2788 | |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2789 | handle = ext4_journal_start(dir, EXT4_DELETE_TRANS_BLOCKS(dir->i_sb)); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2790 | if (IS_ERR(handle)) |
| 2791 | return PTR_ERR(handle); |
| 2792 | |
| 2793 | if (IS_DIRSYNC(dir)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2794 | ext4_handle_sync(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2795 | |
| 2796 | retval = -ENOENT; |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame^] | 2797 | bh = ext4_find_entry(dir, &dentry->d_name, &de, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2798 | if (!bh) |
| 2799 | goto end_unlink; |
| 2800 | |
| 2801 | inode = dentry->d_inode; |
| 2802 | |
| 2803 | retval = -EIO; |
| 2804 | if (le32_to_cpu(de->inode) != inode->i_ino) |
| 2805 | goto end_unlink; |
| 2806 | |
| 2807 | if (!inode->i_nlink) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 2808 | ext4_warning(inode->i_sb, |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2809 | "Deleting nonexistent file (%lu), %d", |
| 2810 | inode->i_ino, inode->i_nlink); |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 2811 | set_nlink(inode, 1); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2812 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2813 | retval = ext4_delete_entry(handle, dir, de, bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2814 | if (retval) |
| 2815 | goto end_unlink; |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 2816 | dir->i_ctime = dir->i_mtime = ext4_current_time(dir); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2817 | ext4_update_dx_flag(dir); |
| 2818 | ext4_mark_inode_dirty(handle, dir); |
Theodore Ts'o | 825f148 | 2008-02-15 15:00:38 -0500 | [diff] [blame] | 2819 | drop_nlink(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2820 | if (!inode->i_nlink) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2821 | ext4_orphan_add(handle, inode); |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 2822 | inode->i_ctime = ext4_current_time(inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2823 | ext4_mark_inode_dirty(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2824 | retval = 0; |
| 2825 | |
| 2826 | end_unlink: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2827 | ext4_journal_stop(handle); |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2828 | brelse(bh); |
Jiaying Zhang | 0562e0b | 2011-03-21 21:38:05 -0400 | [diff] [blame] | 2829 | trace_ext4_unlink_exit(dentry, retval); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2830 | return retval; |
| 2831 | } |
| 2832 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2833 | static int ext4_symlink(struct inode *dir, |
| 2834 | struct dentry *dentry, const char *symname) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2835 | { |
| 2836 | handle_t *handle; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2837 | struct inode *inode; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2838 | int l, err, retries = 0; |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 2839 | int credits; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2840 | |
| 2841 | l = strlen(symname)+1; |
| 2842 | if (l > dir->i_sb->s_blocksize) |
| 2843 | return -ENAMETOOLONG; |
| 2844 | |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 2845 | dquot_initialize(dir); |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 2846 | |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 2847 | if (l > EXT4_N_BLOCKS * 4) { |
| 2848 | /* |
| 2849 | * For non-fast symlinks, we just allocate inode and put it on |
| 2850 | * orphan list in the first transaction => we need bitmap, |
Eric Sandeen | 8c20871 | 2011-08-11 09:54:31 -0500 | [diff] [blame] | 2851 | * group descriptor, sb, inode block, quota blocks, and |
| 2852 | * possibly selinux xattr blocks. |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 2853 | */ |
Eric Sandeen | 8c20871 | 2011-08-11 09:54:31 -0500 | [diff] [blame] | 2854 | credits = 4 + EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb) + |
| 2855 | EXT4_XATTR_TRANS_BLOCKS; |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 2856 | } else { |
| 2857 | /* |
| 2858 | * Fast symlink. We have to add entry to directory |
| 2859 | * (EXT4_DATA_TRANS_BLOCKS + EXT4_INDEX_EXTRA_TRANS_BLOCKS), |
| 2860 | * allocate new inode (bitmap, group descriptor, inode block, |
| 2861 | * quota blocks, sb is already counted in previous macros). |
| 2862 | */ |
| 2863 | credits = EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + |
| 2864 | EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 + |
| 2865 | EXT4_MAXQUOTAS_INIT_BLOCKS(dir->i_sb); |
| 2866 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2867 | retry: |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 2868 | handle = ext4_journal_start(dir, credits); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2869 | if (IS_ERR(handle)) |
| 2870 | return PTR_ERR(handle); |
| 2871 | |
| 2872 | if (IS_DIRSYNC(dir)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2873 | ext4_handle_sync(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2874 | |
Andreas Dilger | 1101391 | 2009-06-13 11:45:35 -0400 | [diff] [blame] | 2875 | inode = ext4_new_inode(handle, dir, S_IFLNK|S_IRWXUGO, |
Dmitry Monakhov | 5cb81da | 2011-10-29 09:05:00 -0400 | [diff] [blame] | 2876 | &dentry->d_name, 0, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2877 | err = PTR_ERR(inode); |
| 2878 | if (IS_ERR(inode)) |
| 2879 | goto out_stop; |
| 2880 | |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 2881 | if (l > EXT4_N_BLOCKS * 4) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2882 | inode->i_op = &ext4_symlink_inode_operations; |
| 2883 | ext4_set_aops(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2884 | /* |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 2885 | * We cannot call page_symlink() with transaction started |
| 2886 | * because it calls into ext4_write_begin() which can wait |
| 2887 | * for transaction commit if we are running out of space |
| 2888 | * and thus we deadlock. So we have to stop transaction now |
| 2889 | * and restart it when symlink contents is written. |
| 2890 | * |
| 2891 | * To keep fs consistent in case of crash, we have to put inode |
| 2892 | * to orphan list in the mean time. |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2893 | */ |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 2894 | drop_nlink(inode); |
| 2895 | err = ext4_orphan_add(handle, inode); |
| 2896 | ext4_journal_stop(handle); |
| 2897 | if (err) |
| 2898 | goto err_drop_inode; |
Nick Piggin | 54566b2 | 2009-01-04 12:00:53 -0800 | [diff] [blame] | 2899 | err = __page_symlink(inode, symname, l, 1); |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 2900 | if (err) |
| 2901 | goto err_drop_inode; |
| 2902 | /* |
| 2903 | * Now inode is being linked into dir (EXT4_DATA_TRANS_BLOCKS |
| 2904 | * + EXT4_INDEX_EXTRA_TRANS_BLOCKS), inode is also modified |
| 2905 | */ |
| 2906 | handle = ext4_journal_start(dir, |
| 2907 | EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + |
| 2908 | EXT4_INDEX_EXTRA_TRANS_BLOCKS + 1); |
| 2909 | if (IS_ERR(handle)) { |
| 2910 | err = PTR_ERR(handle); |
| 2911 | goto err_drop_inode; |
| 2912 | } |
Al Viro | 0ce8c010 | 2012-01-08 19:50:23 -0500 | [diff] [blame] | 2913 | set_nlink(inode, 1); |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 2914 | err = ext4_orphan_del(handle, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2915 | if (err) { |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 2916 | ext4_journal_stop(handle); |
Theodore Ts'o | 825f148 | 2008-02-15 15:00:38 -0500 | [diff] [blame] | 2917 | clear_nlink(inode); |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 2918 | goto err_drop_inode; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2919 | } |
| 2920 | } else { |
Aneesh Kumar K.V | e65187e | 2008-04-29 08:11:12 -0400 | [diff] [blame] | 2921 | /* clear the extent format for fast symlink */ |
Dmitry Monakhov | 12e9b89 | 2010-05-16 22:00:00 -0400 | [diff] [blame] | 2922 | ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2923 | inode->i_op = &ext4_fast_symlink_inode_operations; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2924 | memcpy((char *)&EXT4_I(inode)->i_data, symname, l); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2925 | inode->i_size = l-1; |
| 2926 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2927 | EXT4_I(inode)->i_disksize = inode->i_size; |
| 2928 | err = ext4_add_nondir(handle, dentry, inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2929 | out_stop: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2930 | ext4_journal_stop(handle); |
| 2931 | if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2932 | goto retry; |
| 2933 | return err; |
Jan Kara | df5e622 | 2011-05-03 11:12:58 -0400 | [diff] [blame] | 2934 | err_drop_inode: |
| 2935 | unlock_new_inode(inode); |
| 2936 | iput(inode); |
| 2937 | return err; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2938 | } |
| 2939 | |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 2940 | static int ext4_link(struct dentry *old_dentry, |
| 2941 | struct inode *dir, struct dentry *dentry) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2942 | { |
| 2943 | handle_t *handle; |
| 2944 | struct inode *inode = old_dentry->d_inode; |
| 2945 | int err, retries = 0; |
| 2946 | |
Theodore Ts'o | b05ab1d | 2009-08-29 21:08:08 -0400 | [diff] [blame] | 2947 | if (inode->i_nlink >= EXT4_LINK_MAX) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2948 | return -EMLINK; |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 2949 | |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 2950 | dquot_initialize(dir); |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 2951 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2952 | retry: |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2953 | handle = ext4_journal_start(dir, EXT4_DATA_TRANS_BLOCKS(dir->i_sb) + |
| 2954 | EXT4_INDEX_EXTRA_TRANS_BLOCKS); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2955 | if (IS_ERR(handle)) |
| 2956 | return PTR_ERR(handle); |
| 2957 | |
| 2958 | if (IS_DIRSYNC(dir)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 2959 | ext4_handle_sync(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2960 | |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 2961 | inode->i_ctime = ext4_current_time(inode); |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 2962 | ext4_inc_count(handle, inode); |
Al Viro | 7de9c6ee | 2010-10-23 11:11:40 -0400 | [diff] [blame] | 2963 | ihold(inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2964 | |
Al Viro | 6b38e84 | 2008-12-30 02:03:31 -0500 | [diff] [blame] | 2965 | err = ext4_add_entry(handle, dentry, inode); |
| 2966 | if (!err) { |
| 2967 | ext4_mark_inode_dirty(handle, inode); |
| 2968 | d_instantiate(dentry, inode); |
| 2969 | } else { |
| 2970 | drop_nlink(inode); |
| 2971 | iput(inode); |
| 2972 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 2973 | ext4_journal_stop(handle); |
| 2974 | if (err == -ENOSPC && ext4_should_retry_alloc(dir->i_sb, &retries)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 2975 | goto retry; |
| 2976 | return err; |
| 2977 | } |
| 2978 | |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame^] | 2979 | |
| 2980 | /* |
| 2981 | * Try to find buffer head where contains the parent block. |
| 2982 | * It should be the inode block if it is inlined or the 1st block |
| 2983 | * if it is a normal dir. |
| 2984 | */ |
| 2985 | static struct buffer_head *ext4_get_first_dir_block(handle_t *handle, |
| 2986 | struct inode *inode, |
| 2987 | int *retval, |
| 2988 | struct ext4_dir_entry_2 **parent_de, |
| 2989 | int *inlined) |
| 2990 | { |
| 2991 | struct buffer_head *bh; |
| 2992 | |
| 2993 | if (!ext4_has_inline_data(inode)) { |
| 2994 | if (!(bh = ext4_bread(handle, inode, 0, 0, retval))) { |
| 2995 | if (!*retval) { |
| 2996 | *retval = -EIO; |
| 2997 | ext4_error(inode->i_sb, |
| 2998 | "Directory hole detected on inode %lu\n", |
| 2999 | inode->i_ino); |
| 3000 | } |
| 3001 | return NULL; |
| 3002 | } |
| 3003 | *parent_de = ext4_next_entry( |
| 3004 | (struct ext4_dir_entry_2 *)bh->b_data, |
| 3005 | inode->i_sb->s_blocksize); |
| 3006 | return bh; |
| 3007 | } |
| 3008 | |
| 3009 | *inlined = 1; |
| 3010 | return ext4_get_first_inline_block(inode, parent_de, retval); |
| 3011 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3012 | |
| 3013 | /* |
| 3014 | * Anybody can rename anything with this: the permission checks are left to the |
| 3015 | * higher-level routines. |
| 3016 | */ |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3017 | static int ext4_rename(struct inode *old_dir, struct dentry *old_dentry, |
| 3018 | struct inode *new_dir, struct dentry *new_dentry) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3019 | { |
| 3020 | handle_t *handle; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3021 | struct inode *old_inode, *new_inode; |
| 3022 | struct buffer_head *old_bh, *new_bh, *dir_bh; |
| 3023 | struct ext4_dir_entry_2 *old_de, *new_de; |
Theodore Ts'o | 8750c6d | 2009-02-23 23:05:27 -0500 | [diff] [blame] | 3024 | int retval, force_da_alloc = 0; |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame^] | 3025 | int inlined = 0, new_inlined = 0; |
| 3026 | struct ext4_dir_entry_2 *parent_de; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3027 | |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 3028 | dquot_initialize(old_dir); |
| 3029 | dquot_initialize(new_dir); |
Christoph Hellwig | 907f455 | 2010-03-03 09:05:06 -0500 | [diff] [blame] | 3030 | |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3031 | old_bh = new_bh = dir_bh = NULL; |
| 3032 | |
| 3033 | /* Initialize quotas before so that eventual writes go |
| 3034 | * in separate transaction */ |
| 3035 | if (new_dentry->d_inode) |
Christoph Hellwig | 871a293 | 2010-03-03 09:05:07 -0500 | [diff] [blame] | 3036 | dquot_initialize(new_dentry->d_inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3037 | handle = ext4_journal_start(old_dir, 2 * |
| 3038 | EXT4_DATA_TRANS_BLOCKS(old_dir->i_sb) + |
| 3039 | EXT4_INDEX_EXTRA_TRANS_BLOCKS + 2); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3040 | if (IS_ERR(handle)) |
| 3041 | return PTR_ERR(handle); |
| 3042 | |
| 3043 | if (IS_DIRSYNC(old_dir) || IS_DIRSYNC(new_dir)) |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3044 | ext4_handle_sync(handle); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3045 | |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame^] | 3046 | old_bh = ext4_find_entry(old_dir, &old_dentry->d_name, &old_de, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3047 | /* |
| 3048 | * Check for inode number is _not_ due to possible IO errors. |
| 3049 | * We might rmdir the source, keep it as pwd of some process |
| 3050 | * and merrily kill the link to whatever was created under the |
| 3051 | * same name. Goodbye sticky bit ;-< |
| 3052 | */ |
| 3053 | old_inode = old_dentry->d_inode; |
| 3054 | retval = -ENOENT; |
| 3055 | if (!old_bh || le32_to_cpu(old_de->inode) != old_inode->i_ino) |
| 3056 | goto end_rename; |
| 3057 | |
| 3058 | new_inode = new_dentry->d_inode; |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame^] | 3059 | new_bh = ext4_find_entry(new_dir, &new_dentry->d_name, |
| 3060 | &new_de, &new_inlined); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3061 | if (new_bh) { |
| 3062 | if (!new_inode) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3063 | brelse(new_bh); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3064 | new_bh = NULL; |
| 3065 | } |
| 3066 | } |
| 3067 | if (S_ISDIR(old_inode->i_mode)) { |
| 3068 | if (new_inode) { |
| 3069 | retval = -ENOTEMPTY; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3070 | if (!empty_dir(new_inode)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3071 | goto end_rename; |
| 3072 | } |
| 3073 | retval = -EIO; |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame^] | 3074 | dir_bh = ext4_get_first_dir_block(handle, old_inode, |
| 3075 | &retval, &parent_de, |
| 3076 | &inlined); |
| 3077 | if (!dir_bh) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3078 | goto end_rename; |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame^] | 3079 | if (!inlined && !buffer_verified(dir_bh) && |
Darrick J. Wong | b0336e8 | 2012-04-29 18:41:10 -0400 | [diff] [blame] | 3080 | !ext4_dirent_csum_verify(old_inode, |
| 3081 | (struct ext4_dir_entry *)dir_bh->b_data)) |
| 3082 | goto end_rename; |
| 3083 | set_buffer_verified(dir_bh); |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame^] | 3084 | if (le32_to_cpu(parent_de->inode) != old_dir->i_ino) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3085 | goto end_rename; |
| 3086 | retval = -EMLINK; |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3087 | if (!new_inode && new_dir != old_dir && |
Aneesh Kumar K.V | 2c94eb8 | 2009-08-28 21:43:15 -0400 | [diff] [blame] | 3088 | EXT4_DIR_LINK_MAX(new_dir)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3089 | goto end_rename; |
Amir Goldstein | ef60789 | 2011-03-20 21:18:44 -0400 | [diff] [blame] | 3090 | BUFFER_TRACE(dir_bh, "get_write_access"); |
| 3091 | retval = ext4_journal_get_write_access(handle, dir_bh); |
| 3092 | if (retval) |
| 3093 | goto end_rename; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3094 | } |
| 3095 | if (!new_bh) { |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3096 | retval = ext4_add_entry(handle, new_dentry, old_inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3097 | if (retval) |
| 3098 | goto end_rename; |
| 3099 | } else { |
| 3100 | BUFFER_TRACE(new_bh, "get write access"); |
Amir Goldstein | ef60789 | 2011-03-20 21:18:44 -0400 | [diff] [blame] | 3101 | retval = ext4_journal_get_write_access(handle, new_bh); |
| 3102 | if (retval) |
| 3103 | goto end_rename; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3104 | new_de->inode = cpu_to_le32(old_inode->i_ino); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3105 | if (EXT4_HAS_INCOMPAT_FEATURE(new_dir->i_sb, |
| 3106 | EXT4_FEATURE_INCOMPAT_FILETYPE)) |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3107 | new_de->file_type = old_de->file_type; |
| 3108 | new_dir->i_version++; |
Jan Kara | 53b7e9f | 2008-04-29 22:02:11 -0400 | [diff] [blame] | 3109 | new_dir->i_ctime = new_dir->i_mtime = |
| 3110 | ext4_current_time(new_dir); |
| 3111 | ext4_mark_inode_dirty(handle, new_dir); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3112 | BUFFER_TRACE(new_bh, "call ext4_handle_dirty_metadata"); |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame^] | 3113 | if (!new_inlined) { |
| 3114 | retval = ext4_handle_dirty_dirent_node(handle, |
| 3115 | new_dir, new_bh); |
| 3116 | if (unlikely(retval)) { |
| 3117 | ext4_std_error(new_dir->i_sb, retval); |
| 3118 | goto end_rename; |
| 3119 | } |
Theodore Ts'o | b409714 | 2011-01-10 12:46:59 -0500 | [diff] [blame] | 3120 | } |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3121 | brelse(new_bh); |
| 3122 | new_bh = NULL; |
| 3123 | } |
| 3124 | |
| 3125 | /* |
| 3126 | * Like most other Unix systems, set the ctime for inodes on a |
| 3127 | * rename. |
| 3128 | */ |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 3129 | old_inode->i_ctime = ext4_current_time(old_inode); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3130 | ext4_mark_inode_dirty(handle, old_inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3131 | |
| 3132 | /* |
| 3133 | * ok, that's it |
| 3134 | */ |
| 3135 | if (le32_to_cpu(old_de->inode) != old_inode->i_ino || |
| 3136 | old_de->name_len != old_dentry->d_name.len || |
| 3137 | strncmp(old_de->name, old_dentry->d_name.name, old_de->name_len) || |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3138 | (retval = ext4_delete_entry(handle, old_dir, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3139 | old_de, old_bh)) == -ENOENT) { |
| 3140 | /* old_de could have moved from under us during htree split, so |
| 3141 | * make sure that we are deleting the right entry. We might |
| 3142 | * also be pointing to a stale entry in the unused part of |
| 3143 | * old_bh so just checking inum and the name isn't enough. */ |
| 3144 | struct buffer_head *old_bh2; |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3145 | struct ext4_dir_entry_2 *old_de2; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3146 | |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame^] | 3147 | old_bh2 = ext4_find_entry(old_dir, &old_dentry->d_name, |
| 3148 | &old_de2, NULL); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3149 | if (old_bh2) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3150 | retval = ext4_delete_entry(handle, old_dir, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3151 | old_de2, old_bh2); |
| 3152 | brelse(old_bh2); |
| 3153 | } |
| 3154 | } |
| 3155 | if (retval) { |
Eric Sandeen | 12062dd | 2010-02-15 14:19:27 -0500 | [diff] [blame] | 3156 | ext4_warning(old_dir->i_sb, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3157 | "Deleting old file (%lu), %d, error=%d", |
| 3158 | old_dir->i_ino, old_dir->i_nlink, retval); |
| 3159 | } |
| 3160 | |
| 3161 | if (new_inode) { |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 3162 | ext4_dec_count(handle, new_inode); |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 3163 | new_inode->i_ctime = ext4_current_time(new_inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3164 | } |
Kalpak Shah | ef7f383 | 2007-07-18 09:15:20 -0400 | [diff] [blame] | 3165 | old_dir->i_ctime = old_dir->i_mtime = ext4_current_time(old_dir); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3166 | ext4_update_dx_flag(old_dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3167 | if (dir_bh) { |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame^] | 3168 | parent_de->inode = cpu_to_le32(new_dir->i_ino); |
Frank Mayhar | 0390131 | 2009-01-07 00:06:22 -0500 | [diff] [blame] | 3169 | BUFFER_TRACE(dir_bh, "call ext4_handle_dirty_metadata"); |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame^] | 3170 | if (!inlined) { |
| 3171 | if (is_dx(old_inode)) { |
| 3172 | retval = ext4_handle_dirty_dx_node(handle, |
| 3173 | old_inode, |
| 3174 | dir_bh); |
| 3175 | } else { |
| 3176 | retval = ext4_handle_dirty_dirent_node(handle, |
| 3177 | old_inode, dir_bh); |
| 3178 | } |
Tao Ma | ef58f69 | 2012-07-09 16:29:05 -0400 | [diff] [blame] | 3179 | } else { |
Tao Ma | 32f7f22 | 2012-12-10 14:06:01 -0500 | [diff] [blame^] | 3180 | retval = ext4_mark_inode_dirty(handle, old_inode); |
Tao Ma | ef58f69 | 2012-07-09 16:29:05 -0400 | [diff] [blame] | 3181 | } |
Theodore Ts'o | b409714 | 2011-01-10 12:46:59 -0500 | [diff] [blame] | 3182 | if (retval) { |
| 3183 | ext4_std_error(old_dir->i_sb, retval); |
| 3184 | goto end_rename; |
| 3185 | } |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 3186 | ext4_dec_count(handle, old_dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3187 | if (new_inode) { |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 3188 | /* checked empty_dir above, can't have another parent, |
Theodore Ts'o | 825f148 | 2008-02-15 15:00:38 -0500 | [diff] [blame] | 3189 | * ext4_dec_count() won't work for many-linked dirs */ |
Miklos Szeredi | 6d6b77f | 2011-10-28 14:13:28 +0200 | [diff] [blame] | 3190 | clear_nlink(new_inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3191 | } else { |
Andreas Dilger | f8628a1 | 2007-07-18 08:38:01 -0400 | [diff] [blame] | 3192 | ext4_inc_count(handle, new_dir); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3193 | ext4_update_dx_flag(new_dir); |
| 3194 | ext4_mark_inode_dirty(handle, new_dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3195 | } |
| 3196 | } |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3197 | ext4_mark_inode_dirty(handle, old_dir); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3198 | if (new_inode) { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3199 | ext4_mark_inode_dirty(handle, new_inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3200 | if (!new_inode->i_nlink) |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3201 | ext4_orphan_add(handle, new_inode); |
Theodore Ts'o | afd4672 | 2009-03-16 23:12:23 -0400 | [diff] [blame] | 3202 | if (!test_opt(new_dir->i_sb, NO_AUTO_DA_ALLOC)) |
| 3203 | force_da_alloc = 1; |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3204 | } |
| 3205 | retval = 0; |
| 3206 | |
| 3207 | end_rename: |
Theodore Ts'o | af5bc92 | 2008-09-08 22:25:24 -0400 | [diff] [blame] | 3208 | brelse(dir_bh); |
| 3209 | brelse(old_bh); |
| 3210 | brelse(new_bh); |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3211 | ext4_journal_stop(handle); |
Theodore Ts'o | 8750c6d | 2009-02-23 23:05:27 -0500 | [diff] [blame] | 3212 | if (retval == 0 && force_da_alloc) |
| 3213 | ext4_alloc_da_blocks(old_inode); |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3214 | return retval; |
| 3215 | } |
| 3216 | |
| 3217 | /* |
| 3218 | * directories can handle most operations... |
| 3219 | */ |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 3220 | const struct inode_operations ext4_dir_inode_operations = { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3221 | .create = ext4_create, |
| 3222 | .lookup = ext4_lookup, |
| 3223 | .link = ext4_link, |
| 3224 | .unlink = ext4_unlink, |
| 3225 | .symlink = ext4_symlink, |
| 3226 | .mkdir = ext4_mkdir, |
| 3227 | .rmdir = ext4_rmdir, |
| 3228 | .mknod = ext4_mknod, |
| 3229 | .rename = ext4_rename, |
| 3230 | .setattr = ext4_setattr, |
Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 3231 | #ifdef CONFIG_EXT4_FS_XATTR |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3232 | .setxattr = generic_setxattr, |
| 3233 | .getxattr = generic_getxattr, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3234 | .listxattr = ext4_listxattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3235 | .removexattr = generic_removexattr, |
| 3236 | #endif |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 3237 | .get_acl = ext4_get_acl, |
Aneesh Kumar K.V | abc8746 | 2009-05-02 22:54:32 -0400 | [diff] [blame] | 3238 | .fiemap = ext4_fiemap, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3239 | }; |
| 3240 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 3241 | const struct inode_operations ext4_special_inode_operations = { |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3242 | .setattr = ext4_setattr, |
Theodore Ts'o | 03010a3 | 2008-10-10 20:02:48 -0400 | [diff] [blame] | 3243 | #ifdef CONFIG_EXT4_FS_XATTR |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3244 | .setxattr = generic_setxattr, |
| 3245 | .getxattr = generic_getxattr, |
Mingming Cao | 617ba13 | 2006-10-11 01:20:53 -0700 | [diff] [blame] | 3246 | .listxattr = ext4_listxattr, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3247 | .removexattr = generic_removexattr, |
| 3248 | #endif |
Christoph Hellwig | 4e34e71 | 2011-07-23 17:37:31 +0200 | [diff] [blame] | 3249 | .get_acl = ext4_get_acl, |
Dave Kleikamp | ac27a0e | 2006-10-11 01:20:50 -0700 | [diff] [blame] | 3250 | }; |