b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: LGPL-2.1 |
| 2 | /* |
| 3 | * Copyright (c) 2012 Taobao. |
| 4 | * Written by Tao Ma <boyu.mt@taobao.com> |
| 5 | */ |
| 6 | |
| 7 | #include <linux/iomap.h> |
| 8 | #include <linux/fiemap.h> |
| 9 | #include <linux/iversion.h> |
| 10 | |
| 11 | #include "ext4_jbd2.h" |
| 12 | #include "ext4.h" |
| 13 | #include "xattr.h" |
| 14 | #include "truncate.h" |
| 15 | #include <trace/events/android_fs.h> |
| 16 | |
| 17 | #define EXT4_XATTR_SYSTEM_DATA "data" |
| 18 | #define EXT4_MIN_INLINE_DATA_SIZE ((sizeof(__le32) * EXT4_N_BLOCKS)) |
| 19 | #define EXT4_INLINE_DOTDOT_OFFSET 2 |
| 20 | #define EXT4_INLINE_DOTDOT_SIZE 4 |
| 21 | |
| 22 | static int ext4_get_inline_size(struct inode *inode) |
| 23 | { |
| 24 | if (EXT4_I(inode)->i_inline_off) |
| 25 | return EXT4_I(inode)->i_inline_size; |
| 26 | |
| 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | static int get_max_inline_xattr_value_size(struct inode *inode, |
| 31 | struct ext4_iloc *iloc) |
| 32 | { |
| 33 | struct ext4_xattr_ibody_header *header; |
| 34 | struct ext4_xattr_entry *entry; |
| 35 | struct ext4_inode *raw_inode; |
| 36 | void *end; |
| 37 | int free, min_offs; |
| 38 | |
| 39 | if (!EXT4_INODE_HAS_XATTR_SPACE(inode)) |
| 40 | return 0; |
| 41 | |
| 42 | min_offs = EXT4_SB(inode->i_sb)->s_inode_size - |
| 43 | EXT4_GOOD_OLD_INODE_SIZE - |
| 44 | EXT4_I(inode)->i_extra_isize - |
| 45 | sizeof(struct ext4_xattr_ibody_header); |
| 46 | |
| 47 | /* |
| 48 | * We need to subtract another sizeof(__u32) since an in-inode xattr |
| 49 | * needs an empty 4 bytes to indicate the gap between the xattr entry |
| 50 | * and the name/value pair. |
| 51 | */ |
| 52 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) |
| 53 | return EXT4_XATTR_SIZE(min_offs - |
| 54 | EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) - |
| 55 | EXT4_XATTR_ROUND - sizeof(__u32)); |
| 56 | |
| 57 | raw_inode = ext4_raw_inode(iloc); |
| 58 | header = IHDR(inode, raw_inode); |
| 59 | entry = IFIRST(header); |
| 60 | end = (void *)raw_inode + EXT4_SB(inode->i_sb)->s_inode_size; |
| 61 | |
| 62 | /* Compute min_offs. */ |
| 63 | while (!IS_LAST_ENTRY(entry)) { |
| 64 | void *next = EXT4_XATTR_NEXT(entry); |
| 65 | |
| 66 | if (next >= end) { |
| 67 | EXT4_ERROR_INODE(inode, |
| 68 | "corrupt xattr in inline inode"); |
| 69 | return 0; |
| 70 | } |
| 71 | if (!entry->e_value_inum && entry->e_value_size) { |
| 72 | size_t offs = le16_to_cpu(entry->e_value_offs); |
| 73 | if (offs < min_offs) |
| 74 | min_offs = offs; |
| 75 | } |
| 76 | entry = next; |
| 77 | } |
| 78 | free = min_offs - |
| 79 | ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32); |
| 80 | |
| 81 | if (EXT4_I(inode)->i_inline_off) { |
| 82 | entry = (struct ext4_xattr_entry *) |
| 83 | ((void *)raw_inode + EXT4_I(inode)->i_inline_off); |
| 84 | |
| 85 | free += EXT4_XATTR_SIZE(le32_to_cpu(entry->e_value_size)); |
| 86 | goto out; |
| 87 | } |
| 88 | |
| 89 | free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)); |
| 90 | |
| 91 | if (free > EXT4_XATTR_ROUND) |
| 92 | free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND); |
| 93 | else |
| 94 | free = 0; |
| 95 | |
| 96 | out: |
| 97 | return free; |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Get the maximum size we now can store in an inode. |
| 102 | * If we can't find the space for a xattr entry, don't use the space |
| 103 | * of the extents since we have no space to indicate the inline data. |
| 104 | */ |
| 105 | int ext4_get_max_inline_size(struct inode *inode) |
| 106 | { |
| 107 | int error, max_inline_size; |
| 108 | struct ext4_iloc iloc; |
| 109 | |
| 110 | if (EXT4_I(inode)->i_extra_isize == 0) |
| 111 | return 0; |
| 112 | |
| 113 | error = ext4_get_inode_loc(inode, &iloc); |
| 114 | if (error) { |
| 115 | ext4_error_inode(inode, __func__, __LINE__, 0, |
| 116 | "can't get inode location %lu", |
| 117 | inode->i_ino); |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | down_read(&EXT4_I(inode)->xattr_sem); |
| 122 | max_inline_size = get_max_inline_xattr_value_size(inode, &iloc); |
| 123 | up_read(&EXT4_I(inode)->xattr_sem); |
| 124 | |
| 125 | brelse(iloc.bh); |
| 126 | |
| 127 | if (!max_inline_size) |
| 128 | return 0; |
| 129 | |
| 130 | return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * this function does not take xattr_sem, which is OK because it is |
| 135 | * currently only used in a code path coming form ext4_iget, before |
| 136 | * the new inode has been unlocked |
| 137 | */ |
| 138 | int ext4_find_inline_data_nolock(struct inode *inode) |
| 139 | { |
| 140 | struct ext4_xattr_ibody_find is = { |
| 141 | .s = { .not_found = -ENODATA, }, |
| 142 | }; |
| 143 | struct ext4_xattr_info i = { |
| 144 | .name_index = EXT4_XATTR_INDEX_SYSTEM, |
| 145 | .name = EXT4_XATTR_SYSTEM_DATA, |
| 146 | }; |
| 147 | int error; |
| 148 | |
| 149 | if (EXT4_I(inode)->i_extra_isize == 0) |
| 150 | return 0; |
| 151 | |
| 152 | error = ext4_get_inode_loc(inode, &is.iloc); |
| 153 | if (error) |
| 154 | return error; |
| 155 | |
| 156 | error = ext4_xattr_ibody_find(inode, &i, &is); |
| 157 | if (error) |
| 158 | goto out; |
| 159 | |
| 160 | if (!is.s.not_found) { |
| 161 | if (is.s.here->e_value_inum) { |
| 162 | EXT4_ERROR_INODE(inode, "inline data xattr refers " |
| 163 | "to an external xattr inode"); |
| 164 | error = -EFSCORRUPTED; |
| 165 | goto out; |
| 166 | } |
| 167 | EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here - |
| 168 | (void *)ext4_raw_inode(&is.iloc)); |
| 169 | EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE + |
| 170 | le32_to_cpu(is.s.here->e_value_size); |
| 171 | } |
| 172 | out: |
| 173 | brelse(is.iloc.bh); |
| 174 | return error; |
| 175 | } |
| 176 | |
| 177 | static int ext4_read_inline_data(struct inode *inode, void *buffer, |
| 178 | unsigned int len, |
| 179 | struct ext4_iloc *iloc) |
| 180 | { |
| 181 | struct ext4_xattr_entry *entry; |
| 182 | struct ext4_xattr_ibody_header *header; |
| 183 | int cp_len = 0; |
| 184 | struct ext4_inode *raw_inode; |
| 185 | |
| 186 | if (!len) |
| 187 | return 0; |
| 188 | |
| 189 | BUG_ON(len > EXT4_I(inode)->i_inline_size); |
| 190 | |
| 191 | cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ? |
| 192 | len : EXT4_MIN_INLINE_DATA_SIZE; |
| 193 | |
| 194 | raw_inode = ext4_raw_inode(iloc); |
| 195 | memcpy(buffer, (void *)(raw_inode->i_block), cp_len); |
| 196 | |
| 197 | len -= cp_len; |
| 198 | buffer += cp_len; |
| 199 | |
| 200 | if (!len) |
| 201 | goto out; |
| 202 | |
| 203 | header = IHDR(inode, raw_inode); |
| 204 | entry = (struct ext4_xattr_entry *)((void *)raw_inode + |
| 205 | EXT4_I(inode)->i_inline_off); |
| 206 | len = min_t(unsigned int, len, |
| 207 | (unsigned int)le32_to_cpu(entry->e_value_size)); |
| 208 | |
| 209 | memcpy(buffer, |
| 210 | (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len); |
| 211 | cp_len += len; |
| 212 | |
| 213 | out: |
| 214 | return cp_len; |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * write the buffer to the inline inode. |
| 219 | * If 'create' is set, we don't need to do the extra copy in the xattr |
| 220 | * value since it is already handled by ext4_xattr_ibody_set. |
| 221 | * That saves us one memcpy. |
| 222 | */ |
| 223 | static void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc, |
| 224 | void *buffer, loff_t pos, unsigned int len) |
| 225 | { |
| 226 | struct ext4_xattr_entry *entry; |
| 227 | struct ext4_xattr_ibody_header *header; |
| 228 | struct ext4_inode *raw_inode; |
| 229 | int cp_len = 0; |
| 230 | |
| 231 | if (unlikely(ext4_forced_shutdown(EXT4_SB(inode->i_sb)))) |
| 232 | return; |
| 233 | |
| 234 | BUG_ON(!EXT4_I(inode)->i_inline_off); |
| 235 | BUG_ON(pos + len > EXT4_I(inode)->i_inline_size); |
| 236 | |
| 237 | raw_inode = ext4_raw_inode(iloc); |
| 238 | buffer += pos; |
| 239 | |
| 240 | if (pos < EXT4_MIN_INLINE_DATA_SIZE) { |
| 241 | cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ? |
| 242 | EXT4_MIN_INLINE_DATA_SIZE - pos : len; |
| 243 | memcpy((void *)raw_inode->i_block + pos, buffer, cp_len); |
| 244 | |
| 245 | len -= cp_len; |
| 246 | buffer += cp_len; |
| 247 | pos += cp_len; |
| 248 | } |
| 249 | |
| 250 | if (!len) |
| 251 | return; |
| 252 | |
| 253 | pos -= EXT4_MIN_INLINE_DATA_SIZE; |
| 254 | header = IHDR(inode, raw_inode); |
| 255 | entry = (struct ext4_xattr_entry *)((void *)raw_inode + |
| 256 | EXT4_I(inode)->i_inline_off); |
| 257 | |
| 258 | memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos, |
| 259 | buffer, len); |
| 260 | } |
| 261 | |
| 262 | static int ext4_create_inline_data(handle_t *handle, |
| 263 | struct inode *inode, unsigned len) |
| 264 | { |
| 265 | int error; |
| 266 | void *value = NULL; |
| 267 | struct ext4_xattr_ibody_find is = { |
| 268 | .s = { .not_found = -ENODATA, }, |
| 269 | }; |
| 270 | struct ext4_xattr_info i = { |
| 271 | .name_index = EXT4_XATTR_INDEX_SYSTEM, |
| 272 | .name = EXT4_XATTR_SYSTEM_DATA, |
| 273 | }; |
| 274 | |
| 275 | error = ext4_get_inode_loc(inode, &is.iloc); |
| 276 | if (error) |
| 277 | return error; |
| 278 | |
| 279 | BUFFER_TRACE(is.iloc.bh, "get_write_access"); |
| 280 | error = ext4_journal_get_write_access(handle, is.iloc.bh); |
| 281 | if (error) |
| 282 | goto out; |
| 283 | |
| 284 | if (len > EXT4_MIN_INLINE_DATA_SIZE) { |
| 285 | value = EXT4_ZERO_XATTR_VALUE; |
| 286 | len -= EXT4_MIN_INLINE_DATA_SIZE; |
| 287 | } else { |
| 288 | value = ""; |
| 289 | len = 0; |
| 290 | } |
| 291 | |
| 292 | /* Insert the the xttr entry. */ |
| 293 | i.value = value; |
| 294 | i.value_len = len; |
| 295 | |
| 296 | error = ext4_xattr_ibody_find(inode, &i, &is); |
| 297 | if (error) |
| 298 | goto out; |
| 299 | |
| 300 | BUG_ON(!is.s.not_found); |
| 301 | |
| 302 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); |
| 303 | if (error) { |
| 304 | if (error == -ENOSPC) |
| 305 | ext4_clear_inode_state(inode, |
| 306 | EXT4_STATE_MAY_INLINE_DATA); |
| 307 | goto out; |
| 308 | } |
| 309 | |
| 310 | memset((void *)ext4_raw_inode(&is.iloc)->i_block, |
| 311 | 0, EXT4_MIN_INLINE_DATA_SIZE); |
| 312 | |
| 313 | EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here - |
| 314 | (void *)ext4_raw_inode(&is.iloc)); |
| 315 | EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE; |
| 316 | ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS); |
| 317 | ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA); |
| 318 | get_bh(is.iloc.bh); |
| 319 | error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); |
| 320 | |
| 321 | out: |
| 322 | brelse(is.iloc.bh); |
| 323 | return error; |
| 324 | } |
| 325 | |
| 326 | static int ext4_update_inline_data(handle_t *handle, struct inode *inode, |
| 327 | unsigned int len) |
| 328 | { |
| 329 | int error; |
| 330 | void *value = NULL; |
| 331 | struct ext4_xattr_ibody_find is = { |
| 332 | .s = { .not_found = -ENODATA, }, |
| 333 | }; |
| 334 | struct ext4_xattr_info i = { |
| 335 | .name_index = EXT4_XATTR_INDEX_SYSTEM, |
| 336 | .name = EXT4_XATTR_SYSTEM_DATA, |
| 337 | }; |
| 338 | |
| 339 | /* If the old space is ok, write the data directly. */ |
| 340 | if (len <= EXT4_I(inode)->i_inline_size) |
| 341 | return 0; |
| 342 | |
| 343 | error = ext4_get_inode_loc(inode, &is.iloc); |
| 344 | if (error) |
| 345 | return error; |
| 346 | |
| 347 | error = ext4_xattr_ibody_find(inode, &i, &is); |
| 348 | if (error) |
| 349 | goto out; |
| 350 | |
| 351 | BUG_ON(is.s.not_found); |
| 352 | |
| 353 | len -= EXT4_MIN_INLINE_DATA_SIZE; |
| 354 | value = kzalloc(len, GFP_NOFS); |
| 355 | if (!value) { |
| 356 | error = -ENOMEM; |
| 357 | goto out; |
| 358 | } |
| 359 | |
| 360 | error = ext4_xattr_ibody_get(inode, i.name_index, i.name, |
| 361 | value, len); |
| 362 | if (error < 0) |
| 363 | goto out; |
| 364 | |
| 365 | BUFFER_TRACE(is.iloc.bh, "get_write_access"); |
| 366 | error = ext4_journal_get_write_access(handle, is.iloc.bh); |
| 367 | if (error) |
| 368 | goto out; |
| 369 | |
| 370 | /* Update the xttr entry. */ |
| 371 | i.value = value; |
| 372 | i.value_len = len; |
| 373 | |
| 374 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); |
| 375 | if (error) |
| 376 | goto out; |
| 377 | |
| 378 | EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here - |
| 379 | (void *)ext4_raw_inode(&is.iloc)); |
| 380 | EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE + |
| 381 | le32_to_cpu(is.s.here->e_value_size); |
| 382 | ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); |
| 383 | get_bh(is.iloc.bh); |
| 384 | error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); |
| 385 | |
| 386 | out: |
| 387 | kfree(value); |
| 388 | brelse(is.iloc.bh); |
| 389 | return error; |
| 390 | } |
| 391 | |
| 392 | static int ext4_prepare_inline_data(handle_t *handle, struct inode *inode, |
| 393 | unsigned int len) |
| 394 | { |
| 395 | int ret, size, no_expand; |
| 396 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 397 | |
| 398 | if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) |
| 399 | return -ENOSPC; |
| 400 | |
| 401 | size = ext4_get_max_inline_size(inode); |
| 402 | if (size < len) |
| 403 | return -ENOSPC; |
| 404 | |
| 405 | ext4_write_lock_xattr(inode, &no_expand); |
| 406 | |
| 407 | if (ei->i_inline_off) |
| 408 | ret = ext4_update_inline_data(handle, inode, len); |
| 409 | else |
| 410 | ret = ext4_create_inline_data(handle, inode, len); |
| 411 | |
| 412 | ext4_write_unlock_xattr(inode, &no_expand); |
| 413 | return ret; |
| 414 | } |
| 415 | |
| 416 | static int ext4_destroy_inline_data_nolock(handle_t *handle, |
| 417 | struct inode *inode) |
| 418 | { |
| 419 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 420 | struct ext4_xattr_ibody_find is = { |
| 421 | .s = { .not_found = 0, }, |
| 422 | }; |
| 423 | struct ext4_xattr_info i = { |
| 424 | .name_index = EXT4_XATTR_INDEX_SYSTEM, |
| 425 | .name = EXT4_XATTR_SYSTEM_DATA, |
| 426 | .value = NULL, |
| 427 | .value_len = 0, |
| 428 | }; |
| 429 | int error; |
| 430 | |
| 431 | if (!ei->i_inline_off) |
| 432 | return 0; |
| 433 | |
| 434 | error = ext4_get_inode_loc(inode, &is.iloc); |
| 435 | if (error) |
| 436 | return error; |
| 437 | |
| 438 | error = ext4_xattr_ibody_find(inode, &i, &is); |
| 439 | if (error) |
| 440 | goto out; |
| 441 | |
| 442 | BUFFER_TRACE(is.iloc.bh, "get_write_access"); |
| 443 | error = ext4_journal_get_write_access(handle, is.iloc.bh); |
| 444 | if (error) |
| 445 | goto out; |
| 446 | |
| 447 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); |
| 448 | if (error) |
| 449 | goto out; |
| 450 | |
| 451 | memset((void *)ext4_raw_inode(&is.iloc)->i_block, |
| 452 | 0, EXT4_MIN_INLINE_DATA_SIZE); |
| 453 | memset(ei->i_data, 0, EXT4_MIN_INLINE_DATA_SIZE); |
| 454 | |
| 455 | if (ext4_has_feature_extents(inode->i_sb)) { |
| 456 | if (S_ISDIR(inode->i_mode) || |
| 457 | S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) { |
| 458 | ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS); |
| 459 | ext4_ext_tree_init(handle, inode); |
| 460 | } |
| 461 | } |
| 462 | ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA); |
| 463 | |
| 464 | get_bh(is.iloc.bh); |
| 465 | error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); |
| 466 | |
| 467 | EXT4_I(inode)->i_inline_off = 0; |
| 468 | EXT4_I(inode)->i_inline_size = 0; |
| 469 | ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); |
| 470 | out: |
| 471 | brelse(is.iloc.bh); |
| 472 | if (error == -ENODATA) |
| 473 | error = 0; |
| 474 | return error; |
| 475 | } |
| 476 | |
| 477 | static int ext4_read_inline_page(struct inode *inode, struct page *page) |
| 478 | { |
| 479 | void *kaddr; |
| 480 | int ret = 0; |
| 481 | size_t len; |
| 482 | struct ext4_iloc iloc; |
| 483 | |
| 484 | BUG_ON(!PageLocked(page)); |
| 485 | BUG_ON(!ext4_has_inline_data(inode)); |
| 486 | BUG_ON(page->index); |
| 487 | |
| 488 | if (!EXT4_I(inode)->i_inline_off) { |
| 489 | ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.", |
| 490 | inode->i_ino); |
| 491 | goto out; |
| 492 | } |
| 493 | |
| 494 | ret = ext4_get_inode_loc(inode, &iloc); |
| 495 | if (ret) |
| 496 | goto out; |
| 497 | |
| 498 | len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode)); |
| 499 | kaddr = kmap_atomic(page); |
| 500 | ret = ext4_read_inline_data(inode, kaddr, len, &iloc); |
| 501 | flush_dcache_page(page); |
| 502 | kunmap_atomic(kaddr); |
| 503 | zero_user_segment(page, len, PAGE_SIZE); |
| 504 | SetPageUptodate(page); |
| 505 | brelse(iloc.bh); |
| 506 | |
| 507 | out: |
| 508 | return ret; |
| 509 | } |
| 510 | |
| 511 | int ext4_readpage_inline(struct inode *inode, struct page *page) |
| 512 | { |
| 513 | int ret = 0; |
| 514 | |
| 515 | down_read(&EXT4_I(inode)->xattr_sem); |
| 516 | if (!ext4_has_inline_data(inode)) { |
| 517 | up_read(&EXT4_I(inode)->xattr_sem); |
| 518 | return -EAGAIN; |
| 519 | } |
| 520 | |
| 521 | if (trace_android_fs_dataread_start_enabled()) { |
| 522 | char *path, pathbuf[MAX_TRACE_PATHBUF_LEN]; |
| 523 | |
| 524 | path = android_fstrace_get_pathname(pathbuf, |
| 525 | MAX_TRACE_PATHBUF_LEN, |
| 526 | inode); |
| 527 | trace_android_fs_dataread_start(inode, page_offset(page), |
| 528 | PAGE_SIZE, current->pid, |
| 529 | path, current->comm); |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | * Current inline data can only exist in the 1st page, |
| 534 | * So for all the other pages, just set them uptodate. |
| 535 | */ |
| 536 | if (!page->index) |
| 537 | ret = ext4_read_inline_page(inode, page); |
| 538 | else if (!PageUptodate(page)) { |
| 539 | zero_user_segment(page, 0, PAGE_SIZE); |
| 540 | SetPageUptodate(page); |
| 541 | } |
| 542 | |
| 543 | trace_android_fs_dataread_end(inode, page_offset(page), PAGE_SIZE); |
| 544 | |
| 545 | up_read(&EXT4_I(inode)->xattr_sem); |
| 546 | |
| 547 | unlock_page(page); |
| 548 | return ret >= 0 ? 0 : ret; |
| 549 | } |
| 550 | |
| 551 | static int ext4_convert_inline_data_to_extent(struct address_space *mapping, |
| 552 | struct inode *inode, |
| 553 | unsigned flags) |
| 554 | { |
| 555 | int ret, needed_blocks, no_expand; |
| 556 | handle_t *handle = NULL; |
| 557 | int retries = 0, sem_held = 0; |
| 558 | struct page *page = NULL; |
| 559 | unsigned from, to; |
| 560 | struct ext4_iloc iloc; |
| 561 | |
| 562 | if (!ext4_has_inline_data(inode)) { |
| 563 | /* |
| 564 | * clear the flag so that no new write |
| 565 | * will trap here again. |
| 566 | */ |
| 567 | ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | needed_blocks = ext4_writepage_trans_blocks(inode); |
| 572 | |
| 573 | ret = ext4_get_inode_loc(inode, &iloc); |
| 574 | if (ret) |
| 575 | return ret; |
| 576 | |
| 577 | retry: |
| 578 | handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); |
| 579 | if (IS_ERR(handle)) { |
| 580 | ret = PTR_ERR(handle); |
| 581 | handle = NULL; |
| 582 | goto out; |
| 583 | } |
| 584 | |
| 585 | /* We cannot recurse into the filesystem as the transaction is already |
| 586 | * started */ |
| 587 | flags |= AOP_FLAG_NOFS; |
| 588 | |
| 589 | page = grab_cache_page_write_begin(mapping, 0, flags); |
| 590 | if (!page) { |
| 591 | ret = -ENOMEM; |
| 592 | goto out; |
| 593 | } |
| 594 | |
| 595 | ext4_write_lock_xattr(inode, &no_expand); |
| 596 | sem_held = 1; |
| 597 | /* If some one has already done this for us, just exit. */ |
| 598 | if (!ext4_has_inline_data(inode)) { |
| 599 | ret = 0; |
| 600 | goto out; |
| 601 | } |
| 602 | |
| 603 | from = 0; |
| 604 | to = ext4_get_inline_size(inode); |
| 605 | if (!PageUptodate(page)) { |
| 606 | ret = ext4_read_inline_page(inode, page); |
| 607 | if (ret < 0) |
| 608 | goto out; |
| 609 | } |
| 610 | |
| 611 | ret = ext4_destroy_inline_data_nolock(handle, inode); |
| 612 | if (ret) |
| 613 | goto out; |
| 614 | |
| 615 | if (ext4_should_dioread_nolock(inode)) { |
| 616 | ret = __block_write_begin(page, from, to, |
| 617 | ext4_get_block_unwritten); |
| 618 | } else |
| 619 | ret = __block_write_begin(page, from, to, ext4_get_block); |
| 620 | |
| 621 | if (!ret && ext4_should_journal_data(inode)) { |
| 622 | ret = ext4_walk_page_buffers(handle, page_buffers(page), |
| 623 | from, to, NULL, |
| 624 | do_journal_get_write_access); |
| 625 | } |
| 626 | |
| 627 | if (ret) { |
| 628 | unlock_page(page); |
| 629 | put_page(page); |
| 630 | page = NULL; |
| 631 | ext4_orphan_add(handle, inode); |
| 632 | ext4_write_unlock_xattr(inode, &no_expand); |
| 633 | sem_held = 0; |
| 634 | ext4_journal_stop(handle); |
| 635 | handle = NULL; |
| 636 | ext4_truncate_failed_write(inode); |
| 637 | /* |
| 638 | * If truncate failed early the inode might |
| 639 | * still be on the orphan list; we need to |
| 640 | * make sure the inode is removed from the |
| 641 | * orphan list in that case. |
| 642 | */ |
| 643 | if (inode->i_nlink) |
| 644 | ext4_orphan_del(NULL, inode); |
| 645 | } |
| 646 | |
| 647 | if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) |
| 648 | goto retry; |
| 649 | |
| 650 | if (page) |
| 651 | block_commit_write(page, from, to); |
| 652 | out: |
| 653 | if (page) { |
| 654 | unlock_page(page); |
| 655 | put_page(page); |
| 656 | } |
| 657 | if (sem_held) |
| 658 | ext4_write_unlock_xattr(inode, &no_expand); |
| 659 | if (handle) |
| 660 | ext4_journal_stop(handle); |
| 661 | brelse(iloc.bh); |
| 662 | return ret; |
| 663 | } |
| 664 | |
| 665 | /* |
| 666 | * Try to write data in the inode. |
| 667 | * If the inode has inline data, check whether the new write can be |
| 668 | * in the inode also. If not, create the page the handle, move the data |
| 669 | * to the page make it update and let the later codes create extent for it. |
| 670 | */ |
| 671 | int ext4_try_to_write_inline_data(struct address_space *mapping, |
| 672 | struct inode *inode, |
| 673 | loff_t pos, unsigned len, |
| 674 | unsigned flags, |
| 675 | struct page **pagep) |
| 676 | { |
| 677 | int ret; |
| 678 | handle_t *handle; |
| 679 | struct page *page; |
| 680 | struct ext4_iloc iloc; |
| 681 | |
| 682 | if (pos + len > ext4_get_max_inline_size(inode)) |
| 683 | goto convert; |
| 684 | |
| 685 | ret = ext4_get_inode_loc(inode, &iloc); |
| 686 | if (ret) |
| 687 | return ret; |
| 688 | |
| 689 | /* |
| 690 | * The possible write could happen in the inode, |
| 691 | * so try to reserve the space in inode first. |
| 692 | */ |
| 693 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); |
| 694 | if (IS_ERR(handle)) { |
| 695 | ret = PTR_ERR(handle); |
| 696 | handle = NULL; |
| 697 | goto out; |
| 698 | } |
| 699 | |
| 700 | ret = ext4_prepare_inline_data(handle, inode, pos + len); |
| 701 | if (ret && ret != -ENOSPC) |
| 702 | goto out; |
| 703 | |
| 704 | /* We don't have space in inline inode, so convert it to extent. */ |
| 705 | if (ret == -ENOSPC) { |
| 706 | ext4_journal_stop(handle); |
| 707 | brelse(iloc.bh); |
| 708 | goto convert; |
| 709 | } |
| 710 | |
| 711 | ret = ext4_journal_get_write_access(handle, iloc.bh); |
| 712 | if (ret) |
| 713 | goto out; |
| 714 | |
| 715 | flags |= AOP_FLAG_NOFS; |
| 716 | |
| 717 | page = grab_cache_page_write_begin(mapping, 0, flags); |
| 718 | if (!page) { |
| 719 | ret = -ENOMEM; |
| 720 | goto out; |
| 721 | } |
| 722 | |
| 723 | *pagep = page; |
| 724 | down_read(&EXT4_I(inode)->xattr_sem); |
| 725 | if (!ext4_has_inline_data(inode)) { |
| 726 | ret = 0; |
| 727 | unlock_page(page); |
| 728 | put_page(page); |
| 729 | goto out_up_read; |
| 730 | } |
| 731 | |
| 732 | if (!PageUptodate(page)) { |
| 733 | ret = ext4_read_inline_page(inode, page); |
| 734 | if (ret < 0) { |
| 735 | unlock_page(page); |
| 736 | put_page(page); |
| 737 | goto out_up_read; |
| 738 | } |
| 739 | } |
| 740 | |
| 741 | ret = 1; |
| 742 | handle = NULL; |
| 743 | out_up_read: |
| 744 | up_read(&EXT4_I(inode)->xattr_sem); |
| 745 | out: |
| 746 | if (handle && (ret != 1)) |
| 747 | ext4_journal_stop(handle); |
| 748 | brelse(iloc.bh); |
| 749 | return ret; |
| 750 | convert: |
| 751 | return ext4_convert_inline_data_to_extent(mapping, |
| 752 | inode, flags); |
| 753 | } |
| 754 | |
| 755 | int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len, |
| 756 | unsigned copied, struct page *page) |
| 757 | { |
| 758 | int ret, no_expand; |
| 759 | void *kaddr; |
| 760 | struct ext4_iloc iloc; |
| 761 | |
| 762 | if (unlikely(copied < len) && !PageUptodate(page)) |
| 763 | return 0; |
| 764 | |
| 765 | ret = ext4_get_inode_loc(inode, &iloc); |
| 766 | if (ret) { |
| 767 | ext4_std_error(inode->i_sb, ret); |
| 768 | return ret; |
| 769 | } |
| 770 | |
| 771 | ext4_write_lock_xattr(inode, &no_expand); |
| 772 | BUG_ON(!ext4_has_inline_data(inode)); |
| 773 | |
| 774 | /* |
| 775 | * ei->i_inline_off may have changed since ext4_write_begin() |
| 776 | * called ext4_try_to_write_inline_data() |
| 777 | */ |
| 778 | (void) ext4_find_inline_data_nolock(inode); |
| 779 | |
| 780 | kaddr = kmap_atomic(page); |
| 781 | ext4_write_inline_data(inode, &iloc, kaddr, pos, copied); |
| 782 | kunmap_atomic(kaddr); |
| 783 | SetPageUptodate(page); |
| 784 | /* clear page dirty so that writepages wouldn't work for us. */ |
| 785 | ClearPageDirty(page); |
| 786 | |
| 787 | ext4_write_unlock_xattr(inode, &no_expand); |
| 788 | brelse(iloc.bh); |
| 789 | mark_inode_dirty(inode); |
| 790 | |
| 791 | return copied; |
| 792 | } |
| 793 | |
| 794 | struct buffer_head * |
| 795 | ext4_journalled_write_inline_data(struct inode *inode, |
| 796 | unsigned len, |
| 797 | struct page *page) |
| 798 | { |
| 799 | int ret, no_expand; |
| 800 | void *kaddr; |
| 801 | struct ext4_iloc iloc; |
| 802 | |
| 803 | ret = ext4_get_inode_loc(inode, &iloc); |
| 804 | if (ret) { |
| 805 | ext4_std_error(inode->i_sb, ret); |
| 806 | return NULL; |
| 807 | } |
| 808 | |
| 809 | ext4_write_lock_xattr(inode, &no_expand); |
| 810 | kaddr = kmap_atomic(page); |
| 811 | ext4_write_inline_data(inode, &iloc, kaddr, 0, len); |
| 812 | kunmap_atomic(kaddr); |
| 813 | ext4_write_unlock_xattr(inode, &no_expand); |
| 814 | |
| 815 | return iloc.bh; |
| 816 | } |
| 817 | |
| 818 | /* |
| 819 | * Try to make the page cache and handle ready for the inline data case. |
| 820 | * We can call this function in 2 cases: |
| 821 | * 1. The inode is created and the first write exceeds inline size. We can |
| 822 | * clear the inode state safely. |
| 823 | * 2. The inode has inline data, then we need to read the data, make it |
| 824 | * update and dirty so that ext4_da_writepages can handle it. We don't |
| 825 | * need to start the journal since the file's metatdata isn't changed now. |
| 826 | */ |
| 827 | static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping, |
| 828 | struct inode *inode, |
| 829 | unsigned flags, |
| 830 | void **fsdata) |
| 831 | { |
| 832 | int ret = 0, inline_size; |
| 833 | struct page *page; |
| 834 | |
| 835 | page = grab_cache_page_write_begin(mapping, 0, flags); |
| 836 | if (!page) |
| 837 | return -ENOMEM; |
| 838 | |
| 839 | down_read(&EXT4_I(inode)->xattr_sem); |
| 840 | if (!ext4_has_inline_data(inode)) { |
| 841 | ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); |
| 842 | goto out; |
| 843 | } |
| 844 | |
| 845 | inline_size = ext4_get_inline_size(inode); |
| 846 | |
| 847 | if (!PageUptodate(page)) { |
| 848 | ret = ext4_read_inline_page(inode, page); |
| 849 | if (ret < 0) |
| 850 | goto out; |
| 851 | } |
| 852 | |
| 853 | ret = __block_write_begin(page, 0, inline_size, |
| 854 | ext4_da_get_block_prep); |
| 855 | if (ret) { |
| 856 | up_read(&EXT4_I(inode)->xattr_sem); |
| 857 | unlock_page(page); |
| 858 | put_page(page); |
| 859 | ext4_truncate_failed_write(inode); |
| 860 | return ret; |
| 861 | } |
| 862 | |
| 863 | SetPageDirty(page); |
| 864 | SetPageUptodate(page); |
| 865 | ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); |
| 866 | *fsdata = (void *)CONVERT_INLINE_DATA; |
| 867 | |
| 868 | out: |
| 869 | up_read(&EXT4_I(inode)->xattr_sem); |
| 870 | if (page) { |
| 871 | unlock_page(page); |
| 872 | put_page(page); |
| 873 | } |
| 874 | return ret; |
| 875 | } |
| 876 | |
| 877 | /* |
| 878 | * Prepare the write for the inline data. |
| 879 | * If the the data can be written into the inode, we just read |
| 880 | * the page and make it uptodate, and start the journal. |
| 881 | * Otherwise read the page, makes it dirty so that it can be |
| 882 | * handle in writepages(the i_disksize update is left to the |
| 883 | * normal ext4_da_write_end). |
| 884 | */ |
| 885 | int ext4_da_write_inline_data_begin(struct address_space *mapping, |
| 886 | struct inode *inode, |
| 887 | loff_t pos, unsigned len, |
| 888 | unsigned flags, |
| 889 | struct page **pagep, |
| 890 | void **fsdata) |
| 891 | { |
| 892 | int ret, inline_size; |
| 893 | handle_t *handle; |
| 894 | struct page *page; |
| 895 | struct ext4_iloc iloc; |
| 896 | int retries = 0; |
| 897 | |
| 898 | ret = ext4_get_inode_loc(inode, &iloc); |
| 899 | if (ret) |
| 900 | return ret; |
| 901 | |
| 902 | retry_journal: |
| 903 | handle = ext4_journal_start(inode, EXT4_HT_INODE, 1); |
| 904 | if (IS_ERR(handle)) { |
| 905 | ret = PTR_ERR(handle); |
| 906 | goto out; |
| 907 | } |
| 908 | |
| 909 | inline_size = ext4_get_max_inline_size(inode); |
| 910 | |
| 911 | ret = -ENOSPC; |
| 912 | if (inline_size >= pos + len) { |
| 913 | ret = ext4_prepare_inline_data(handle, inode, pos + len); |
| 914 | if (ret && ret != -ENOSPC) |
| 915 | goto out_journal; |
| 916 | } |
| 917 | |
| 918 | /* |
| 919 | * We cannot recurse into the filesystem as the transaction |
| 920 | * is already started. |
| 921 | */ |
| 922 | flags |= AOP_FLAG_NOFS; |
| 923 | |
| 924 | if (ret == -ENOSPC) { |
| 925 | ext4_journal_stop(handle); |
| 926 | ret = ext4_da_convert_inline_data_to_extent(mapping, |
| 927 | inode, |
| 928 | flags, |
| 929 | fsdata); |
| 930 | if (ret == -ENOSPC && |
| 931 | ext4_should_retry_alloc(inode->i_sb, &retries)) |
| 932 | goto retry_journal; |
| 933 | goto out; |
| 934 | } |
| 935 | |
| 936 | page = grab_cache_page_write_begin(mapping, 0, flags); |
| 937 | if (!page) { |
| 938 | ret = -ENOMEM; |
| 939 | goto out_journal; |
| 940 | } |
| 941 | |
| 942 | down_read(&EXT4_I(inode)->xattr_sem); |
| 943 | if (!ext4_has_inline_data(inode)) { |
| 944 | ret = 0; |
| 945 | goto out_release_page; |
| 946 | } |
| 947 | |
| 948 | if (!PageUptodate(page)) { |
| 949 | ret = ext4_read_inline_page(inode, page); |
| 950 | if (ret < 0) |
| 951 | goto out_release_page; |
| 952 | } |
| 953 | ret = ext4_journal_get_write_access(handle, iloc.bh); |
| 954 | if (ret) |
| 955 | goto out_release_page; |
| 956 | |
| 957 | up_read(&EXT4_I(inode)->xattr_sem); |
| 958 | *pagep = page; |
| 959 | brelse(iloc.bh); |
| 960 | return 1; |
| 961 | out_release_page: |
| 962 | up_read(&EXT4_I(inode)->xattr_sem); |
| 963 | unlock_page(page); |
| 964 | put_page(page); |
| 965 | out_journal: |
| 966 | ext4_journal_stop(handle); |
| 967 | out: |
| 968 | brelse(iloc.bh); |
| 969 | return ret; |
| 970 | } |
| 971 | |
| 972 | int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos, |
| 973 | unsigned len, unsigned copied, |
| 974 | struct page *page) |
| 975 | { |
| 976 | int ret; |
| 977 | |
| 978 | ret = ext4_write_inline_data_end(inode, pos, len, copied, page); |
| 979 | if (ret < 0) { |
| 980 | unlock_page(page); |
| 981 | put_page(page); |
| 982 | return ret; |
| 983 | } |
| 984 | copied = ret; |
| 985 | |
| 986 | /* |
| 987 | * No need to use i_size_read() here, the i_size |
| 988 | * cannot change under us because we hold i_mutex. |
| 989 | * |
| 990 | * But it's important to update i_size while still holding page lock: |
| 991 | * page writeout could otherwise come in and zero beyond i_size. |
| 992 | */ |
| 993 | if (pos+copied > inode->i_size) |
| 994 | i_size_write(inode, pos+copied); |
| 995 | unlock_page(page); |
| 996 | put_page(page); |
| 997 | |
| 998 | /* |
| 999 | * Don't mark the inode dirty under page lock. First, it unnecessarily |
| 1000 | * makes the holding time of page lock longer. Second, it forces lock |
| 1001 | * ordering of page lock and transaction start for journaling |
| 1002 | * filesystems. |
| 1003 | */ |
| 1004 | mark_inode_dirty(inode); |
| 1005 | |
| 1006 | return copied; |
| 1007 | } |
| 1008 | |
| 1009 | #ifdef INLINE_DIR_DEBUG |
| 1010 | void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh, |
| 1011 | void *inline_start, int inline_size) |
| 1012 | { |
| 1013 | int offset; |
| 1014 | unsigned short de_len; |
| 1015 | struct ext4_dir_entry_2 *de = inline_start; |
| 1016 | void *dlimit = inline_start + inline_size; |
| 1017 | |
| 1018 | trace_printk("inode %lu\n", dir->i_ino); |
| 1019 | offset = 0; |
| 1020 | while ((void *)de < dlimit) { |
| 1021 | de_len = ext4_rec_len_from_disk(de->rec_len, inline_size); |
| 1022 | trace_printk("de: off %u rlen %u name %.*s nlen %u ino %u\n", |
| 1023 | offset, de_len, de->name_len, de->name, |
| 1024 | de->name_len, le32_to_cpu(de->inode)); |
| 1025 | if (ext4_check_dir_entry(dir, NULL, de, bh, |
| 1026 | inline_start, inline_size, offset)) |
| 1027 | BUG(); |
| 1028 | |
| 1029 | offset += de_len; |
| 1030 | de = (struct ext4_dir_entry_2 *) ((char *) de + de_len); |
| 1031 | } |
| 1032 | } |
| 1033 | #else |
| 1034 | #define ext4_show_inline_dir(dir, bh, inline_start, inline_size) |
| 1035 | #endif |
| 1036 | |
| 1037 | /* |
| 1038 | * Add a new entry into a inline dir. |
| 1039 | * It will return -ENOSPC if no space is available, and -EIO |
| 1040 | * and -EEXIST if directory entry already exists. |
| 1041 | */ |
| 1042 | static int ext4_add_dirent_to_inline(handle_t *handle, |
| 1043 | struct ext4_filename *fname, |
| 1044 | struct inode *dir, |
| 1045 | struct inode *inode, |
| 1046 | struct ext4_iloc *iloc, |
| 1047 | void *inline_start, int inline_size) |
| 1048 | { |
| 1049 | int err; |
| 1050 | struct ext4_dir_entry_2 *de; |
| 1051 | |
| 1052 | err = ext4_find_dest_de(dir, inode, iloc->bh, inline_start, |
| 1053 | inline_size, fname, &de); |
| 1054 | if (err) |
| 1055 | return err; |
| 1056 | |
| 1057 | BUFFER_TRACE(iloc->bh, "get_write_access"); |
| 1058 | err = ext4_journal_get_write_access(handle, iloc->bh); |
| 1059 | if (err) |
| 1060 | return err; |
| 1061 | ext4_insert_dentry(dir, inode, de, inline_size, fname); |
| 1062 | |
| 1063 | ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size); |
| 1064 | |
| 1065 | /* |
| 1066 | * XXX shouldn't update any times until successful |
| 1067 | * completion of syscall, but too many callers depend |
| 1068 | * on this. |
| 1069 | * |
| 1070 | * XXX similarly, too many callers depend on |
| 1071 | * ext4_new_inode() setting the times, but error |
| 1072 | * recovery deletes the inode, so the worst that can |
| 1073 | * happen is that the times are slightly out of date |
| 1074 | * and/or different from the directory change time. |
| 1075 | */ |
| 1076 | dir->i_mtime = dir->i_ctime = current_time(dir); |
| 1077 | ext4_update_dx_flag(dir); |
| 1078 | inode_inc_iversion(dir); |
| 1079 | return 1; |
| 1080 | } |
| 1081 | |
| 1082 | static void *ext4_get_inline_xattr_pos(struct inode *inode, |
| 1083 | struct ext4_iloc *iloc) |
| 1084 | { |
| 1085 | struct ext4_xattr_entry *entry; |
| 1086 | struct ext4_xattr_ibody_header *header; |
| 1087 | |
| 1088 | BUG_ON(!EXT4_I(inode)->i_inline_off); |
| 1089 | |
| 1090 | header = IHDR(inode, ext4_raw_inode(iloc)); |
| 1091 | entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) + |
| 1092 | EXT4_I(inode)->i_inline_off); |
| 1093 | |
| 1094 | return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs); |
| 1095 | } |
| 1096 | |
| 1097 | /* Set the final de to cover the whole block. */ |
| 1098 | static void ext4_update_final_de(void *de_buf, int old_size, int new_size) |
| 1099 | { |
| 1100 | struct ext4_dir_entry_2 *de, *prev_de; |
| 1101 | void *limit; |
| 1102 | int de_len; |
| 1103 | |
| 1104 | de = (struct ext4_dir_entry_2 *)de_buf; |
| 1105 | if (old_size) { |
| 1106 | limit = de_buf + old_size; |
| 1107 | do { |
| 1108 | prev_de = de; |
| 1109 | de_len = ext4_rec_len_from_disk(de->rec_len, old_size); |
| 1110 | de_buf += de_len; |
| 1111 | de = (struct ext4_dir_entry_2 *)de_buf; |
| 1112 | } while (de_buf < limit); |
| 1113 | |
| 1114 | prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size - |
| 1115 | old_size, new_size); |
| 1116 | } else { |
| 1117 | /* this is just created, so create an empty entry. */ |
| 1118 | de->inode = 0; |
| 1119 | de->rec_len = ext4_rec_len_to_disk(new_size, new_size); |
| 1120 | } |
| 1121 | } |
| 1122 | |
| 1123 | static int ext4_update_inline_dir(handle_t *handle, struct inode *dir, |
| 1124 | struct ext4_iloc *iloc) |
| 1125 | { |
| 1126 | int ret; |
| 1127 | int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE; |
| 1128 | int new_size = get_max_inline_xattr_value_size(dir, iloc); |
| 1129 | |
| 1130 | if (new_size - old_size <= ext4_dir_rec_len(1, NULL)) |
| 1131 | return -ENOSPC; |
| 1132 | |
| 1133 | ret = ext4_update_inline_data(handle, dir, |
| 1134 | new_size + EXT4_MIN_INLINE_DATA_SIZE); |
| 1135 | if (ret) |
| 1136 | return ret; |
| 1137 | |
| 1138 | ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size, |
| 1139 | EXT4_I(dir)->i_inline_size - |
| 1140 | EXT4_MIN_INLINE_DATA_SIZE); |
| 1141 | dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size; |
| 1142 | return 0; |
| 1143 | } |
| 1144 | |
| 1145 | static void ext4_restore_inline_data(handle_t *handle, struct inode *inode, |
| 1146 | struct ext4_iloc *iloc, |
| 1147 | void *buf, int inline_size) |
| 1148 | { |
| 1149 | int ret; |
| 1150 | |
| 1151 | ret = ext4_create_inline_data(handle, inode, inline_size); |
| 1152 | if (ret) { |
| 1153 | ext4_msg(inode->i_sb, KERN_EMERG, |
| 1154 | "error restoring inline_data for inode -- potential data loss! (inode %lu, error %d)", |
| 1155 | inode->i_ino, ret); |
| 1156 | return; |
| 1157 | } |
| 1158 | ext4_write_inline_data(inode, iloc, buf, 0, inline_size); |
| 1159 | ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); |
| 1160 | } |
| 1161 | |
| 1162 | static int ext4_finish_convert_inline_dir(handle_t *handle, |
| 1163 | struct inode *inode, |
| 1164 | struct buffer_head *dir_block, |
| 1165 | void *buf, |
| 1166 | int inline_size) |
| 1167 | { |
| 1168 | int err, csum_size = 0, header_size = 0; |
| 1169 | struct ext4_dir_entry_2 *de; |
| 1170 | void *target = dir_block->b_data; |
| 1171 | |
| 1172 | /* |
| 1173 | * First create "." and ".." and then copy the dir information |
| 1174 | * back to the block. |
| 1175 | */ |
| 1176 | de = (struct ext4_dir_entry_2 *)target; |
| 1177 | de = ext4_init_dot_dotdot(inode, de, |
| 1178 | inode->i_sb->s_blocksize, csum_size, |
| 1179 | le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1); |
| 1180 | header_size = (void *)de - target; |
| 1181 | |
| 1182 | memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE, |
| 1183 | inline_size - EXT4_INLINE_DOTDOT_SIZE); |
| 1184 | |
| 1185 | if (ext4_has_metadata_csum(inode->i_sb)) |
| 1186 | csum_size = sizeof(struct ext4_dir_entry_tail); |
| 1187 | |
| 1188 | inode->i_size = inode->i_sb->s_blocksize; |
| 1189 | i_size_write(inode, inode->i_sb->s_blocksize); |
| 1190 | EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize; |
| 1191 | ext4_update_final_de(dir_block->b_data, |
| 1192 | inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size, |
| 1193 | inode->i_sb->s_blocksize - csum_size); |
| 1194 | |
| 1195 | if (csum_size) |
| 1196 | ext4_initialize_dirent_tail(dir_block, |
| 1197 | inode->i_sb->s_blocksize); |
| 1198 | set_buffer_uptodate(dir_block); |
| 1199 | unlock_buffer(dir_block); |
| 1200 | err = ext4_handle_dirty_dirblock(handle, inode, dir_block); |
| 1201 | if (err) |
| 1202 | return err; |
| 1203 | set_buffer_verified(dir_block); |
| 1204 | return ext4_mark_inode_dirty(handle, inode); |
| 1205 | } |
| 1206 | |
| 1207 | static int ext4_convert_inline_data_nolock(handle_t *handle, |
| 1208 | struct inode *inode, |
| 1209 | struct ext4_iloc *iloc) |
| 1210 | { |
| 1211 | int error; |
| 1212 | void *buf = NULL; |
| 1213 | struct buffer_head *data_bh = NULL; |
| 1214 | struct ext4_map_blocks map; |
| 1215 | int inline_size; |
| 1216 | |
| 1217 | inline_size = ext4_get_inline_size(inode); |
| 1218 | buf = kmalloc(inline_size, GFP_NOFS); |
| 1219 | if (!buf) { |
| 1220 | error = -ENOMEM; |
| 1221 | goto out; |
| 1222 | } |
| 1223 | |
| 1224 | error = ext4_read_inline_data(inode, buf, inline_size, iloc); |
| 1225 | if (error < 0) |
| 1226 | goto out; |
| 1227 | |
| 1228 | /* |
| 1229 | * Make sure the inline directory entries pass checks before we try to |
| 1230 | * convert them, so that we avoid touching stuff that needs fsck. |
| 1231 | */ |
| 1232 | if (S_ISDIR(inode->i_mode)) { |
| 1233 | error = ext4_check_all_de(inode, iloc->bh, |
| 1234 | buf + EXT4_INLINE_DOTDOT_SIZE, |
| 1235 | inline_size - EXT4_INLINE_DOTDOT_SIZE); |
| 1236 | if (error) |
| 1237 | goto out; |
| 1238 | } |
| 1239 | |
| 1240 | error = ext4_destroy_inline_data_nolock(handle, inode); |
| 1241 | if (error) |
| 1242 | goto out; |
| 1243 | |
| 1244 | map.m_lblk = 0; |
| 1245 | map.m_len = 1; |
| 1246 | map.m_flags = 0; |
| 1247 | error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE); |
| 1248 | if (error < 0) |
| 1249 | goto out_restore; |
| 1250 | if (!(map.m_flags & EXT4_MAP_MAPPED)) { |
| 1251 | error = -EIO; |
| 1252 | goto out_restore; |
| 1253 | } |
| 1254 | |
| 1255 | data_bh = sb_getblk(inode->i_sb, map.m_pblk); |
| 1256 | if (!data_bh) { |
| 1257 | error = -ENOMEM; |
| 1258 | goto out_restore; |
| 1259 | } |
| 1260 | |
| 1261 | lock_buffer(data_bh); |
| 1262 | error = ext4_journal_get_create_access(handle, data_bh); |
| 1263 | if (error) { |
| 1264 | unlock_buffer(data_bh); |
| 1265 | error = -EIO; |
| 1266 | goto out_restore; |
| 1267 | } |
| 1268 | memset(data_bh->b_data, 0, inode->i_sb->s_blocksize); |
| 1269 | |
| 1270 | if (!S_ISDIR(inode->i_mode)) { |
| 1271 | memcpy(data_bh->b_data, buf, inline_size); |
| 1272 | set_buffer_uptodate(data_bh); |
| 1273 | unlock_buffer(data_bh); |
| 1274 | error = ext4_handle_dirty_metadata(handle, |
| 1275 | inode, data_bh); |
| 1276 | } else { |
| 1277 | error = ext4_finish_convert_inline_dir(handle, inode, data_bh, |
| 1278 | buf, inline_size); |
| 1279 | } |
| 1280 | |
| 1281 | out_restore: |
| 1282 | if (error) |
| 1283 | ext4_restore_inline_data(handle, inode, iloc, buf, inline_size); |
| 1284 | |
| 1285 | out: |
| 1286 | brelse(data_bh); |
| 1287 | kfree(buf); |
| 1288 | return error; |
| 1289 | } |
| 1290 | |
| 1291 | /* |
| 1292 | * Try to add the new entry to the inline data. |
| 1293 | * If succeeds, return 0. If not, extended the inline dir and copied data to |
| 1294 | * the new created block. |
| 1295 | */ |
| 1296 | int ext4_try_add_inline_entry(handle_t *handle, struct ext4_filename *fname, |
| 1297 | struct inode *dir, struct inode *inode) |
| 1298 | { |
| 1299 | int ret, inline_size, no_expand; |
| 1300 | void *inline_start; |
| 1301 | struct ext4_iloc iloc; |
| 1302 | |
| 1303 | ret = ext4_get_inode_loc(dir, &iloc); |
| 1304 | if (ret) |
| 1305 | return ret; |
| 1306 | |
| 1307 | ext4_write_lock_xattr(dir, &no_expand); |
| 1308 | if (!ext4_has_inline_data(dir)) |
| 1309 | goto out; |
| 1310 | |
| 1311 | inline_start = (void *)ext4_raw_inode(&iloc)->i_block + |
| 1312 | EXT4_INLINE_DOTDOT_SIZE; |
| 1313 | inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE; |
| 1314 | |
| 1315 | ret = ext4_add_dirent_to_inline(handle, fname, dir, inode, &iloc, |
| 1316 | inline_start, inline_size); |
| 1317 | if (ret != -ENOSPC) |
| 1318 | goto out; |
| 1319 | |
| 1320 | /* check whether it can be inserted to inline xattr space. */ |
| 1321 | inline_size = EXT4_I(dir)->i_inline_size - |
| 1322 | EXT4_MIN_INLINE_DATA_SIZE; |
| 1323 | if (!inline_size) { |
| 1324 | /* Try to use the xattr space.*/ |
| 1325 | ret = ext4_update_inline_dir(handle, dir, &iloc); |
| 1326 | if (ret && ret != -ENOSPC) |
| 1327 | goto out; |
| 1328 | |
| 1329 | inline_size = EXT4_I(dir)->i_inline_size - |
| 1330 | EXT4_MIN_INLINE_DATA_SIZE; |
| 1331 | } |
| 1332 | |
| 1333 | if (inline_size) { |
| 1334 | inline_start = ext4_get_inline_xattr_pos(dir, &iloc); |
| 1335 | |
| 1336 | ret = ext4_add_dirent_to_inline(handle, fname, dir, |
| 1337 | inode, &iloc, inline_start, |
| 1338 | inline_size); |
| 1339 | |
| 1340 | if (ret != -ENOSPC) |
| 1341 | goto out; |
| 1342 | } |
| 1343 | |
| 1344 | /* |
| 1345 | * The inline space is filled up, so create a new block for it. |
| 1346 | * As the extent tree will be created, we have to save the inline |
| 1347 | * dir first. |
| 1348 | */ |
| 1349 | ret = ext4_convert_inline_data_nolock(handle, dir, &iloc); |
| 1350 | |
| 1351 | out: |
| 1352 | ext4_write_unlock_xattr(dir, &no_expand); |
| 1353 | ext4_mark_inode_dirty(handle, dir); |
| 1354 | brelse(iloc.bh); |
| 1355 | return ret; |
| 1356 | } |
| 1357 | |
| 1358 | /* |
| 1359 | * This function fills a red-black tree with information from an |
| 1360 | * inlined dir. It returns the number directory entries loaded |
| 1361 | * into the tree. If there is an error it is returned in err. |
| 1362 | */ |
| 1363 | int ext4_inlinedir_to_tree(struct file *dir_file, |
| 1364 | struct inode *dir, ext4_lblk_t block, |
| 1365 | struct dx_hash_info *hinfo, |
| 1366 | __u32 start_hash, __u32 start_minor_hash, |
| 1367 | int *has_inline_data) |
| 1368 | { |
| 1369 | int err = 0, count = 0; |
| 1370 | unsigned int parent_ino; |
| 1371 | int pos; |
| 1372 | struct ext4_dir_entry_2 *de; |
| 1373 | struct inode *inode = file_inode(dir_file); |
| 1374 | int ret, inline_size = 0; |
| 1375 | struct ext4_iloc iloc; |
| 1376 | void *dir_buf = NULL; |
| 1377 | struct ext4_dir_entry_2 fake; |
| 1378 | struct fscrypt_str tmp_str; |
| 1379 | |
| 1380 | ret = ext4_get_inode_loc(inode, &iloc); |
| 1381 | if (ret) |
| 1382 | return ret; |
| 1383 | |
| 1384 | down_read(&EXT4_I(inode)->xattr_sem); |
| 1385 | if (!ext4_has_inline_data(inode)) { |
| 1386 | up_read(&EXT4_I(inode)->xattr_sem); |
| 1387 | *has_inline_data = 0; |
| 1388 | goto out; |
| 1389 | } |
| 1390 | |
| 1391 | inline_size = ext4_get_inline_size(inode); |
| 1392 | dir_buf = kmalloc(inline_size, GFP_NOFS); |
| 1393 | if (!dir_buf) { |
| 1394 | ret = -ENOMEM; |
| 1395 | up_read(&EXT4_I(inode)->xattr_sem); |
| 1396 | goto out; |
| 1397 | } |
| 1398 | |
| 1399 | ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc); |
| 1400 | up_read(&EXT4_I(inode)->xattr_sem); |
| 1401 | if (ret < 0) |
| 1402 | goto out; |
| 1403 | |
| 1404 | pos = 0; |
| 1405 | parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode); |
| 1406 | while (pos < inline_size) { |
| 1407 | /* |
| 1408 | * As inlined dir doesn't store any information about '.' and |
| 1409 | * only the inode number of '..' is stored, we have to handle |
| 1410 | * them differently. |
| 1411 | */ |
| 1412 | if (pos == 0) { |
| 1413 | fake.inode = cpu_to_le32(inode->i_ino); |
| 1414 | fake.name_len = 1; |
| 1415 | strcpy(fake.name, "."); |
| 1416 | fake.rec_len = ext4_rec_len_to_disk( |
| 1417 | ext4_dir_rec_len(fake.name_len, NULL), |
| 1418 | inline_size); |
| 1419 | ext4_set_de_type(inode->i_sb, &fake, S_IFDIR); |
| 1420 | de = &fake; |
| 1421 | pos = EXT4_INLINE_DOTDOT_OFFSET; |
| 1422 | } else if (pos == EXT4_INLINE_DOTDOT_OFFSET) { |
| 1423 | fake.inode = cpu_to_le32(parent_ino); |
| 1424 | fake.name_len = 2; |
| 1425 | strcpy(fake.name, ".."); |
| 1426 | fake.rec_len = ext4_rec_len_to_disk( |
| 1427 | ext4_dir_rec_len(fake.name_len, NULL), |
| 1428 | inline_size); |
| 1429 | ext4_set_de_type(inode->i_sb, &fake, S_IFDIR); |
| 1430 | de = &fake; |
| 1431 | pos = EXT4_INLINE_DOTDOT_SIZE; |
| 1432 | } else { |
| 1433 | de = (struct ext4_dir_entry_2 *)(dir_buf + pos); |
| 1434 | pos += ext4_rec_len_from_disk(de->rec_len, inline_size); |
| 1435 | if (ext4_check_dir_entry(inode, dir_file, de, |
| 1436 | iloc.bh, dir_buf, |
| 1437 | inline_size, pos)) { |
| 1438 | ret = count; |
| 1439 | goto out; |
| 1440 | } |
| 1441 | } |
| 1442 | |
| 1443 | if (ext4_hash_in_dirent(dir)) { |
| 1444 | hinfo->hash = EXT4_DIRENT_HASH(de); |
| 1445 | hinfo->minor_hash = EXT4_DIRENT_MINOR_HASH(de); |
| 1446 | } else { |
| 1447 | ext4fs_dirhash(dir, de->name, de->name_len, hinfo); |
| 1448 | } |
| 1449 | if ((hinfo->hash < start_hash) || |
| 1450 | ((hinfo->hash == start_hash) && |
| 1451 | (hinfo->minor_hash < start_minor_hash))) |
| 1452 | continue; |
| 1453 | if (de->inode == 0) |
| 1454 | continue; |
| 1455 | tmp_str.name = de->name; |
| 1456 | tmp_str.len = de->name_len; |
| 1457 | err = ext4_htree_store_dirent(dir_file, hinfo->hash, |
| 1458 | hinfo->minor_hash, de, &tmp_str); |
| 1459 | if (err) { |
| 1460 | ret = err; |
| 1461 | goto out; |
| 1462 | } |
| 1463 | count++; |
| 1464 | } |
| 1465 | ret = count; |
| 1466 | out: |
| 1467 | kfree(dir_buf); |
| 1468 | brelse(iloc.bh); |
| 1469 | return ret; |
| 1470 | } |
| 1471 | |
| 1472 | /* |
| 1473 | * So this function is called when the volume is mkfsed with |
| 1474 | * dir_index disabled. In order to keep f_pos persistent |
| 1475 | * after we convert from an inlined dir to a blocked based, |
| 1476 | * we just pretend that we are a normal dir and return the |
| 1477 | * offset as if '.' and '..' really take place. |
| 1478 | * |
| 1479 | */ |
| 1480 | int ext4_read_inline_dir(struct file *file, |
| 1481 | struct dir_context *ctx, |
| 1482 | int *has_inline_data) |
| 1483 | { |
| 1484 | unsigned int offset, parent_ino; |
| 1485 | int i; |
| 1486 | struct ext4_dir_entry_2 *de; |
| 1487 | struct super_block *sb; |
| 1488 | struct inode *inode = file_inode(file); |
| 1489 | int ret, inline_size = 0; |
| 1490 | struct ext4_iloc iloc; |
| 1491 | void *dir_buf = NULL; |
| 1492 | int dotdot_offset, dotdot_size, extra_offset, extra_size; |
| 1493 | |
| 1494 | ret = ext4_get_inode_loc(inode, &iloc); |
| 1495 | if (ret) |
| 1496 | return ret; |
| 1497 | |
| 1498 | down_read(&EXT4_I(inode)->xattr_sem); |
| 1499 | if (!ext4_has_inline_data(inode)) { |
| 1500 | up_read(&EXT4_I(inode)->xattr_sem); |
| 1501 | *has_inline_data = 0; |
| 1502 | goto out; |
| 1503 | } |
| 1504 | |
| 1505 | inline_size = ext4_get_inline_size(inode); |
| 1506 | dir_buf = kmalloc(inline_size, GFP_NOFS); |
| 1507 | if (!dir_buf) { |
| 1508 | ret = -ENOMEM; |
| 1509 | up_read(&EXT4_I(inode)->xattr_sem); |
| 1510 | goto out; |
| 1511 | } |
| 1512 | |
| 1513 | ret = ext4_read_inline_data(inode, dir_buf, inline_size, &iloc); |
| 1514 | up_read(&EXT4_I(inode)->xattr_sem); |
| 1515 | if (ret < 0) |
| 1516 | goto out; |
| 1517 | |
| 1518 | ret = 0; |
| 1519 | sb = inode->i_sb; |
| 1520 | parent_ino = le32_to_cpu(((struct ext4_dir_entry_2 *)dir_buf)->inode); |
| 1521 | offset = ctx->pos; |
| 1522 | |
| 1523 | /* |
| 1524 | * dotdot_offset and dotdot_size is the real offset and |
| 1525 | * size for ".." and "." if the dir is block based while |
| 1526 | * the real size for them are only EXT4_INLINE_DOTDOT_SIZE. |
| 1527 | * So we will use extra_offset and extra_size to indicate them |
| 1528 | * during the inline dir iteration. |
| 1529 | */ |
| 1530 | dotdot_offset = ext4_dir_rec_len(1, NULL); |
| 1531 | dotdot_size = dotdot_offset + ext4_dir_rec_len(2, NULL); |
| 1532 | extra_offset = dotdot_size - EXT4_INLINE_DOTDOT_SIZE; |
| 1533 | extra_size = extra_offset + inline_size; |
| 1534 | |
| 1535 | /* |
| 1536 | * If the version has changed since the last call to |
| 1537 | * readdir(2), then we might be pointing to an invalid |
| 1538 | * dirent right now. Scan from the start of the inline |
| 1539 | * dir to make sure. |
| 1540 | */ |
| 1541 | if (!inode_eq_iversion(inode, file->f_version)) { |
| 1542 | for (i = 0; i < extra_size && i < offset;) { |
| 1543 | /* |
| 1544 | * "." is with offset 0 and |
| 1545 | * ".." is dotdot_offset. |
| 1546 | */ |
| 1547 | if (!i) { |
| 1548 | i = dotdot_offset; |
| 1549 | continue; |
| 1550 | } else if (i == dotdot_offset) { |
| 1551 | i = dotdot_size; |
| 1552 | continue; |
| 1553 | } |
| 1554 | /* for other entry, the real offset in |
| 1555 | * the buf has to be tuned accordingly. |
| 1556 | */ |
| 1557 | de = (struct ext4_dir_entry_2 *) |
| 1558 | (dir_buf + i - extra_offset); |
| 1559 | /* It's too expensive to do a full |
| 1560 | * dirent test each time round this |
| 1561 | * loop, but we do have to test at |
| 1562 | * least that it is non-zero. A |
| 1563 | * failure will be detected in the |
| 1564 | * dirent test below. */ |
| 1565 | if (ext4_rec_len_from_disk(de->rec_len, extra_size) |
| 1566 | < ext4_dir_rec_len(1, NULL)) |
| 1567 | break; |
| 1568 | i += ext4_rec_len_from_disk(de->rec_len, |
| 1569 | extra_size); |
| 1570 | } |
| 1571 | offset = i; |
| 1572 | ctx->pos = offset; |
| 1573 | file->f_version = inode_query_iversion(inode); |
| 1574 | } |
| 1575 | |
| 1576 | while (ctx->pos < extra_size) { |
| 1577 | if (ctx->pos == 0) { |
| 1578 | if (!dir_emit(ctx, ".", 1, inode->i_ino, DT_DIR)) |
| 1579 | goto out; |
| 1580 | ctx->pos = dotdot_offset; |
| 1581 | continue; |
| 1582 | } |
| 1583 | |
| 1584 | if (ctx->pos == dotdot_offset) { |
| 1585 | if (!dir_emit(ctx, "..", 2, parent_ino, DT_DIR)) |
| 1586 | goto out; |
| 1587 | ctx->pos = dotdot_size; |
| 1588 | continue; |
| 1589 | } |
| 1590 | |
| 1591 | de = (struct ext4_dir_entry_2 *) |
| 1592 | (dir_buf + ctx->pos - extra_offset); |
| 1593 | if (ext4_check_dir_entry(inode, file, de, iloc.bh, dir_buf, |
| 1594 | extra_size, ctx->pos)) |
| 1595 | goto out; |
| 1596 | if (le32_to_cpu(de->inode)) { |
| 1597 | if (!dir_emit(ctx, de->name, de->name_len, |
| 1598 | le32_to_cpu(de->inode), |
| 1599 | get_dtype(sb, de->file_type))) |
| 1600 | goto out; |
| 1601 | } |
| 1602 | ctx->pos += ext4_rec_len_from_disk(de->rec_len, extra_size); |
| 1603 | } |
| 1604 | out: |
| 1605 | kfree(dir_buf); |
| 1606 | brelse(iloc.bh); |
| 1607 | return ret; |
| 1608 | } |
| 1609 | |
| 1610 | struct buffer_head *ext4_get_first_inline_block(struct inode *inode, |
| 1611 | struct ext4_dir_entry_2 **parent_de, |
| 1612 | int *retval) |
| 1613 | { |
| 1614 | struct ext4_iloc iloc; |
| 1615 | |
| 1616 | *retval = ext4_get_inode_loc(inode, &iloc); |
| 1617 | if (*retval) |
| 1618 | return NULL; |
| 1619 | |
| 1620 | *parent_de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block; |
| 1621 | |
| 1622 | return iloc.bh; |
| 1623 | } |
| 1624 | |
| 1625 | /* |
| 1626 | * Try to create the inline data for the new dir. |
| 1627 | * If it succeeds, return 0, otherwise return the error. |
| 1628 | * In case of ENOSPC, the caller should create the normal disk layout dir. |
| 1629 | */ |
| 1630 | int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent, |
| 1631 | struct inode *inode) |
| 1632 | { |
| 1633 | int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE; |
| 1634 | struct ext4_iloc iloc; |
| 1635 | struct ext4_dir_entry_2 *de; |
| 1636 | |
| 1637 | ret = ext4_get_inode_loc(inode, &iloc); |
| 1638 | if (ret) |
| 1639 | return ret; |
| 1640 | |
| 1641 | ret = ext4_prepare_inline_data(handle, inode, inline_size); |
| 1642 | if (ret) |
| 1643 | goto out; |
| 1644 | |
| 1645 | /* |
| 1646 | * For inline dir, we only save the inode information for the ".." |
| 1647 | * and create a fake dentry to cover the left space. |
| 1648 | */ |
| 1649 | de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block; |
| 1650 | de->inode = cpu_to_le32(parent->i_ino); |
| 1651 | de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE); |
| 1652 | de->inode = 0; |
| 1653 | de->rec_len = ext4_rec_len_to_disk( |
| 1654 | inline_size - EXT4_INLINE_DOTDOT_SIZE, |
| 1655 | inline_size); |
| 1656 | set_nlink(inode, 2); |
| 1657 | inode->i_size = EXT4_I(inode)->i_disksize = inline_size; |
| 1658 | out: |
| 1659 | brelse(iloc.bh); |
| 1660 | return ret; |
| 1661 | } |
| 1662 | |
| 1663 | struct buffer_head *ext4_find_inline_entry(struct inode *dir, |
| 1664 | struct ext4_filename *fname, |
| 1665 | struct ext4_dir_entry_2 **res_dir, |
| 1666 | int *has_inline_data) |
| 1667 | { |
| 1668 | struct ext4_xattr_ibody_find is = { |
| 1669 | .s = { .not_found = -ENODATA, }, |
| 1670 | }; |
| 1671 | struct ext4_xattr_info i = { |
| 1672 | .name_index = EXT4_XATTR_INDEX_SYSTEM, |
| 1673 | .name = EXT4_XATTR_SYSTEM_DATA, |
| 1674 | }; |
| 1675 | int ret; |
| 1676 | void *inline_start; |
| 1677 | int inline_size; |
| 1678 | |
| 1679 | ret = ext4_get_inode_loc(dir, &is.iloc); |
| 1680 | if (ret) |
| 1681 | return ERR_PTR(ret); |
| 1682 | |
| 1683 | down_read(&EXT4_I(dir)->xattr_sem); |
| 1684 | |
| 1685 | ret = ext4_xattr_ibody_find(dir, &i, &is); |
| 1686 | if (ret) |
| 1687 | goto out; |
| 1688 | |
| 1689 | if (!ext4_has_inline_data(dir)) { |
| 1690 | *has_inline_data = 0; |
| 1691 | goto out; |
| 1692 | } |
| 1693 | |
| 1694 | inline_start = (void *)ext4_raw_inode(&is.iloc)->i_block + |
| 1695 | EXT4_INLINE_DOTDOT_SIZE; |
| 1696 | inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE; |
| 1697 | ret = ext4_search_dir(is.iloc.bh, inline_start, inline_size, |
| 1698 | dir, fname, 0, res_dir); |
| 1699 | if (ret == 1) |
| 1700 | goto out_find; |
| 1701 | if (ret < 0) |
| 1702 | goto out; |
| 1703 | |
| 1704 | if (ext4_get_inline_size(dir) == EXT4_MIN_INLINE_DATA_SIZE) |
| 1705 | goto out; |
| 1706 | |
| 1707 | inline_start = ext4_get_inline_xattr_pos(dir, &is.iloc); |
| 1708 | inline_size = ext4_get_inline_size(dir) - EXT4_MIN_INLINE_DATA_SIZE; |
| 1709 | |
| 1710 | ret = ext4_search_dir(is.iloc.bh, inline_start, inline_size, |
| 1711 | dir, fname, 0, res_dir); |
| 1712 | if (ret == 1) |
| 1713 | goto out_find; |
| 1714 | |
| 1715 | out: |
| 1716 | brelse(is.iloc.bh); |
| 1717 | if (ret < 0) |
| 1718 | is.iloc.bh = ERR_PTR(ret); |
| 1719 | else |
| 1720 | is.iloc.bh = NULL; |
| 1721 | out_find: |
| 1722 | up_read(&EXT4_I(dir)->xattr_sem); |
| 1723 | return is.iloc.bh; |
| 1724 | } |
| 1725 | |
| 1726 | int ext4_delete_inline_entry(handle_t *handle, |
| 1727 | struct inode *dir, |
| 1728 | struct ext4_dir_entry_2 *de_del, |
| 1729 | struct buffer_head *bh, |
| 1730 | int *has_inline_data) |
| 1731 | { |
| 1732 | int err, inline_size, no_expand; |
| 1733 | struct ext4_iloc iloc; |
| 1734 | void *inline_start; |
| 1735 | |
| 1736 | err = ext4_get_inode_loc(dir, &iloc); |
| 1737 | if (err) |
| 1738 | return err; |
| 1739 | |
| 1740 | ext4_write_lock_xattr(dir, &no_expand); |
| 1741 | if (!ext4_has_inline_data(dir)) { |
| 1742 | *has_inline_data = 0; |
| 1743 | goto out; |
| 1744 | } |
| 1745 | |
| 1746 | if ((void *)de_del - ((void *)ext4_raw_inode(&iloc)->i_block) < |
| 1747 | EXT4_MIN_INLINE_DATA_SIZE) { |
| 1748 | inline_start = (void *)ext4_raw_inode(&iloc)->i_block + |
| 1749 | EXT4_INLINE_DOTDOT_SIZE; |
| 1750 | inline_size = EXT4_MIN_INLINE_DATA_SIZE - |
| 1751 | EXT4_INLINE_DOTDOT_SIZE; |
| 1752 | } else { |
| 1753 | inline_start = ext4_get_inline_xattr_pos(dir, &iloc); |
| 1754 | inline_size = ext4_get_inline_size(dir) - |
| 1755 | EXT4_MIN_INLINE_DATA_SIZE; |
| 1756 | } |
| 1757 | |
| 1758 | BUFFER_TRACE(bh, "get_write_access"); |
| 1759 | err = ext4_journal_get_write_access(handle, bh); |
| 1760 | if (err) |
| 1761 | goto out; |
| 1762 | |
| 1763 | err = ext4_generic_delete_entry(handle, dir, de_del, bh, |
| 1764 | inline_start, inline_size, 0); |
| 1765 | if (err) |
| 1766 | goto out; |
| 1767 | |
| 1768 | ext4_show_inline_dir(dir, iloc.bh, inline_start, inline_size); |
| 1769 | out: |
| 1770 | ext4_write_unlock_xattr(dir, &no_expand); |
| 1771 | if (likely(err == 0)) |
| 1772 | err = ext4_mark_inode_dirty(handle, dir); |
| 1773 | brelse(iloc.bh); |
| 1774 | if (err != -ENOENT) |
| 1775 | ext4_std_error(dir->i_sb, err); |
| 1776 | return err; |
| 1777 | } |
| 1778 | |
| 1779 | /* |
| 1780 | * Get the inline dentry at offset. |
| 1781 | */ |
| 1782 | static inline struct ext4_dir_entry_2 * |
| 1783 | ext4_get_inline_entry(struct inode *inode, |
| 1784 | struct ext4_iloc *iloc, |
| 1785 | unsigned int offset, |
| 1786 | void **inline_start, |
| 1787 | int *inline_size) |
| 1788 | { |
| 1789 | void *inline_pos; |
| 1790 | |
| 1791 | BUG_ON(offset > ext4_get_inline_size(inode)); |
| 1792 | |
| 1793 | if (offset < EXT4_MIN_INLINE_DATA_SIZE) { |
| 1794 | inline_pos = (void *)ext4_raw_inode(iloc)->i_block; |
| 1795 | *inline_size = EXT4_MIN_INLINE_DATA_SIZE; |
| 1796 | } else { |
| 1797 | inline_pos = ext4_get_inline_xattr_pos(inode, iloc); |
| 1798 | offset -= EXT4_MIN_INLINE_DATA_SIZE; |
| 1799 | *inline_size = ext4_get_inline_size(inode) - |
| 1800 | EXT4_MIN_INLINE_DATA_SIZE; |
| 1801 | } |
| 1802 | |
| 1803 | if (inline_start) |
| 1804 | *inline_start = inline_pos; |
| 1805 | return (struct ext4_dir_entry_2 *)(inline_pos + offset); |
| 1806 | } |
| 1807 | |
| 1808 | bool empty_inline_dir(struct inode *dir, int *has_inline_data) |
| 1809 | { |
| 1810 | int err, inline_size; |
| 1811 | struct ext4_iloc iloc; |
| 1812 | size_t inline_len; |
| 1813 | void *inline_pos; |
| 1814 | unsigned int offset; |
| 1815 | struct ext4_dir_entry_2 *de; |
| 1816 | bool ret = true; |
| 1817 | |
| 1818 | err = ext4_get_inode_loc(dir, &iloc); |
| 1819 | if (err) { |
| 1820 | EXT4_ERROR_INODE(dir, "error %d getting inode %lu block", |
| 1821 | err, dir->i_ino); |
| 1822 | return true; |
| 1823 | } |
| 1824 | |
| 1825 | down_read(&EXT4_I(dir)->xattr_sem); |
| 1826 | if (!ext4_has_inline_data(dir)) { |
| 1827 | *has_inline_data = 0; |
| 1828 | goto out; |
| 1829 | } |
| 1830 | |
| 1831 | de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block; |
| 1832 | if (!le32_to_cpu(de->inode)) { |
| 1833 | ext4_warning(dir->i_sb, |
| 1834 | "bad inline directory (dir #%lu) - no `..'", |
| 1835 | dir->i_ino); |
| 1836 | ret = true; |
| 1837 | goto out; |
| 1838 | } |
| 1839 | |
| 1840 | inline_len = ext4_get_inline_size(dir); |
| 1841 | offset = EXT4_INLINE_DOTDOT_SIZE; |
| 1842 | while (offset < inline_len) { |
| 1843 | de = ext4_get_inline_entry(dir, &iloc, offset, |
| 1844 | &inline_pos, &inline_size); |
| 1845 | if (ext4_check_dir_entry(dir, NULL, de, |
| 1846 | iloc.bh, inline_pos, |
| 1847 | inline_size, offset)) { |
| 1848 | ext4_warning(dir->i_sb, |
| 1849 | "bad inline directory (dir #%lu) - " |
| 1850 | "inode %u, rec_len %u, name_len %d" |
| 1851 | "inline size %d", |
| 1852 | dir->i_ino, le32_to_cpu(de->inode), |
| 1853 | le16_to_cpu(de->rec_len), de->name_len, |
| 1854 | inline_size); |
| 1855 | ret = true; |
| 1856 | goto out; |
| 1857 | } |
| 1858 | if (le32_to_cpu(de->inode)) { |
| 1859 | ret = false; |
| 1860 | goto out; |
| 1861 | } |
| 1862 | offset += ext4_rec_len_from_disk(de->rec_len, inline_size); |
| 1863 | } |
| 1864 | |
| 1865 | out: |
| 1866 | up_read(&EXT4_I(dir)->xattr_sem); |
| 1867 | brelse(iloc.bh); |
| 1868 | return ret; |
| 1869 | } |
| 1870 | |
| 1871 | int ext4_destroy_inline_data(handle_t *handle, struct inode *inode) |
| 1872 | { |
| 1873 | int ret, no_expand; |
| 1874 | |
| 1875 | ext4_write_lock_xattr(inode, &no_expand); |
| 1876 | ret = ext4_destroy_inline_data_nolock(handle, inode); |
| 1877 | ext4_write_unlock_xattr(inode, &no_expand); |
| 1878 | |
| 1879 | return ret; |
| 1880 | } |
| 1881 | |
| 1882 | int ext4_inline_data_iomap(struct inode *inode, struct iomap *iomap) |
| 1883 | { |
| 1884 | __u64 addr; |
| 1885 | int error = -EAGAIN; |
| 1886 | struct ext4_iloc iloc; |
| 1887 | |
| 1888 | down_read(&EXT4_I(inode)->xattr_sem); |
| 1889 | if (!ext4_has_inline_data(inode)) |
| 1890 | goto out; |
| 1891 | |
| 1892 | error = ext4_get_inode_loc(inode, &iloc); |
| 1893 | if (error) |
| 1894 | goto out; |
| 1895 | |
| 1896 | addr = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits; |
| 1897 | addr += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data; |
| 1898 | addr += offsetof(struct ext4_inode, i_block); |
| 1899 | |
| 1900 | brelse(iloc.bh); |
| 1901 | |
| 1902 | iomap->addr = addr; |
| 1903 | iomap->offset = 0; |
| 1904 | iomap->length = min_t(loff_t, ext4_get_inline_size(inode), |
| 1905 | i_size_read(inode)); |
| 1906 | iomap->type = IOMAP_INLINE; |
| 1907 | iomap->flags = 0; |
| 1908 | |
| 1909 | out: |
| 1910 | up_read(&EXT4_I(inode)->xattr_sem); |
| 1911 | return error; |
| 1912 | } |
| 1913 | |
| 1914 | int ext4_inline_data_fiemap(struct inode *inode, |
| 1915 | struct fiemap_extent_info *fieinfo, |
| 1916 | int *has_inline, __u64 start, __u64 len) |
| 1917 | { |
| 1918 | __u64 physical = 0; |
| 1919 | __u64 inline_len; |
| 1920 | __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED | |
| 1921 | FIEMAP_EXTENT_LAST; |
| 1922 | int error = 0; |
| 1923 | struct ext4_iloc iloc; |
| 1924 | |
| 1925 | down_read(&EXT4_I(inode)->xattr_sem); |
| 1926 | if (!ext4_has_inline_data(inode)) { |
| 1927 | *has_inline = 0; |
| 1928 | goto out; |
| 1929 | } |
| 1930 | inline_len = min_t(size_t, ext4_get_inline_size(inode), |
| 1931 | i_size_read(inode)); |
| 1932 | if (start >= inline_len) |
| 1933 | goto out; |
| 1934 | if (start + len < inline_len) |
| 1935 | inline_len = start + len; |
| 1936 | inline_len -= start; |
| 1937 | |
| 1938 | error = ext4_get_inode_loc(inode, &iloc); |
| 1939 | if (error) |
| 1940 | goto out; |
| 1941 | |
| 1942 | physical = (__u64)iloc.bh->b_blocknr << inode->i_sb->s_blocksize_bits; |
| 1943 | physical += (char *)ext4_raw_inode(&iloc) - iloc.bh->b_data; |
| 1944 | physical += offsetof(struct ext4_inode, i_block); |
| 1945 | |
| 1946 | brelse(iloc.bh); |
| 1947 | out: |
| 1948 | up_read(&EXT4_I(inode)->xattr_sem); |
| 1949 | if (physical) |
| 1950 | error = fiemap_fill_next_extent(fieinfo, start, physical, |
| 1951 | inline_len, flags); |
| 1952 | return (error < 0 ? error : 0); |
| 1953 | } |
| 1954 | |
| 1955 | int ext4_inline_data_truncate(struct inode *inode, int *has_inline) |
| 1956 | { |
| 1957 | handle_t *handle; |
| 1958 | int inline_size, value_len, needed_blocks, no_expand, err = 0; |
| 1959 | size_t i_size; |
| 1960 | void *value = NULL; |
| 1961 | struct ext4_xattr_ibody_find is = { |
| 1962 | .s = { .not_found = -ENODATA, }, |
| 1963 | }; |
| 1964 | struct ext4_xattr_info i = { |
| 1965 | .name_index = EXT4_XATTR_INDEX_SYSTEM, |
| 1966 | .name = EXT4_XATTR_SYSTEM_DATA, |
| 1967 | }; |
| 1968 | |
| 1969 | |
| 1970 | needed_blocks = ext4_writepage_trans_blocks(inode); |
| 1971 | handle = ext4_journal_start(inode, EXT4_HT_INODE, needed_blocks); |
| 1972 | if (IS_ERR(handle)) |
| 1973 | return PTR_ERR(handle); |
| 1974 | |
| 1975 | ext4_write_lock_xattr(inode, &no_expand); |
| 1976 | if (!ext4_has_inline_data(inode)) { |
| 1977 | ext4_write_unlock_xattr(inode, &no_expand); |
| 1978 | *has_inline = 0; |
| 1979 | ext4_journal_stop(handle); |
| 1980 | return 0; |
| 1981 | } |
| 1982 | |
| 1983 | if ((err = ext4_orphan_add(handle, inode)) != 0) |
| 1984 | goto out; |
| 1985 | |
| 1986 | if ((err = ext4_get_inode_loc(inode, &is.iloc)) != 0) |
| 1987 | goto out; |
| 1988 | |
| 1989 | down_write(&EXT4_I(inode)->i_data_sem); |
| 1990 | i_size = inode->i_size; |
| 1991 | inline_size = ext4_get_inline_size(inode); |
| 1992 | EXT4_I(inode)->i_disksize = i_size; |
| 1993 | |
| 1994 | if (i_size < inline_size) { |
| 1995 | /* Clear the content in the xattr space. */ |
| 1996 | if (inline_size > EXT4_MIN_INLINE_DATA_SIZE) { |
| 1997 | if ((err = ext4_xattr_ibody_find(inode, &i, &is)) != 0) |
| 1998 | goto out_error; |
| 1999 | |
| 2000 | BUG_ON(is.s.not_found); |
| 2001 | |
| 2002 | value_len = le32_to_cpu(is.s.here->e_value_size); |
| 2003 | value = kmalloc(value_len, GFP_NOFS); |
| 2004 | if (!value) { |
| 2005 | err = -ENOMEM; |
| 2006 | goto out_error; |
| 2007 | } |
| 2008 | |
| 2009 | err = ext4_xattr_ibody_get(inode, i.name_index, |
| 2010 | i.name, value, value_len); |
| 2011 | if (err <= 0) |
| 2012 | goto out_error; |
| 2013 | |
| 2014 | i.value = value; |
| 2015 | i.value_len = i_size > EXT4_MIN_INLINE_DATA_SIZE ? |
| 2016 | i_size - EXT4_MIN_INLINE_DATA_SIZE : 0; |
| 2017 | err = ext4_xattr_ibody_set(handle, inode, &i, &is); |
| 2018 | if (err) |
| 2019 | goto out_error; |
| 2020 | } |
| 2021 | |
| 2022 | /* Clear the content within i_blocks. */ |
| 2023 | if (i_size < EXT4_MIN_INLINE_DATA_SIZE) { |
| 2024 | void *p = (void *) ext4_raw_inode(&is.iloc)->i_block; |
| 2025 | memset(p + i_size, 0, |
| 2026 | EXT4_MIN_INLINE_DATA_SIZE - i_size); |
| 2027 | } |
| 2028 | |
| 2029 | EXT4_I(inode)->i_inline_size = i_size < |
| 2030 | EXT4_MIN_INLINE_DATA_SIZE ? |
| 2031 | EXT4_MIN_INLINE_DATA_SIZE : i_size; |
| 2032 | } |
| 2033 | |
| 2034 | out_error: |
| 2035 | up_write(&EXT4_I(inode)->i_data_sem); |
| 2036 | out: |
| 2037 | brelse(is.iloc.bh); |
| 2038 | ext4_write_unlock_xattr(inode, &no_expand); |
| 2039 | kfree(value); |
| 2040 | if (inode->i_nlink) |
| 2041 | ext4_orphan_del(handle, inode); |
| 2042 | |
| 2043 | if (err == 0) { |
| 2044 | inode->i_mtime = inode->i_ctime = current_time(inode); |
| 2045 | err = ext4_mark_inode_dirty(handle, inode); |
| 2046 | if (IS_SYNC(inode)) |
| 2047 | ext4_handle_sync(handle); |
| 2048 | } |
| 2049 | ext4_journal_stop(handle); |
| 2050 | return err; |
| 2051 | } |
| 2052 | |
| 2053 | int ext4_convert_inline_data(struct inode *inode) |
| 2054 | { |
| 2055 | int error, needed_blocks, no_expand; |
| 2056 | handle_t *handle; |
| 2057 | struct ext4_iloc iloc; |
| 2058 | |
| 2059 | if (!ext4_has_inline_data(inode)) { |
| 2060 | ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); |
| 2061 | return 0; |
| 2062 | } else if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) { |
| 2063 | /* |
| 2064 | * Inode has inline data but EXT4_STATE_MAY_INLINE_DATA is |
| 2065 | * cleared. This means we are in the middle of moving of |
| 2066 | * inline data to delay allocated block. Just force writeout |
| 2067 | * here to finish conversion. |
| 2068 | */ |
| 2069 | error = filemap_flush(inode->i_mapping); |
| 2070 | if (error) |
| 2071 | return error; |
| 2072 | if (!ext4_has_inline_data(inode)) |
| 2073 | return 0; |
| 2074 | } |
| 2075 | |
| 2076 | needed_blocks = ext4_writepage_trans_blocks(inode); |
| 2077 | |
| 2078 | iloc.bh = NULL; |
| 2079 | error = ext4_get_inode_loc(inode, &iloc); |
| 2080 | if (error) |
| 2081 | return error; |
| 2082 | |
| 2083 | handle = ext4_journal_start(inode, EXT4_HT_WRITE_PAGE, needed_blocks); |
| 2084 | if (IS_ERR(handle)) { |
| 2085 | error = PTR_ERR(handle); |
| 2086 | goto out_free; |
| 2087 | } |
| 2088 | |
| 2089 | ext4_write_lock_xattr(inode, &no_expand); |
| 2090 | if (ext4_has_inline_data(inode)) |
| 2091 | error = ext4_convert_inline_data_nolock(handle, inode, &iloc); |
| 2092 | ext4_write_unlock_xattr(inode, &no_expand); |
| 2093 | ext4_journal_stop(handle); |
| 2094 | out_free: |
| 2095 | brelse(iloc.bh); |
| 2096 | return error; |
| 2097 | } |