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