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