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