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