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