Tao Ma | 67cf5b0 | 2012-12-10 14:04:46 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 Taobao. |
| 3 | * Written by Tao Ma <boyu.mt@taobao.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of version 2.1 of the GNU Lesser General Public License |
| 7 | * as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | */ |
| 14 | #include "ext4_jbd2.h" |
| 15 | #include "ext4.h" |
| 16 | #include "xattr.h" |
Tao Ma | f19d587 | 2012-12-10 14:05:51 -0500 | [diff] [blame] | 17 | #include "truncate.h" |
Tao Ma | 67cf5b0 | 2012-12-10 14:04:46 -0500 | [diff] [blame] | 18 | |
| 19 | #define EXT4_XATTR_SYSTEM_DATA "data" |
| 20 | #define EXT4_MIN_INLINE_DATA_SIZE ((sizeof(__le32) * EXT4_N_BLOCKS)) |
Tao Ma | 3c47d54 | 2012-12-10 14:05:59 -0500 | [diff] [blame^] | 21 | #define EXT4_INLINE_DOTDOT_SIZE 4 |
Tao Ma | 67cf5b0 | 2012-12-10 14:04:46 -0500 | [diff] [blame] | 22 | |
| 23 | int ext4_get_inline_size(struct inode *inode) |
| 24 | { |
| 25 | if (EXT4_I(inode)->i_inline_off) |
| 26 | return EXT4_I(inode)->i_inline_size; |
| 27 | |
| 28 | return 0; |
| 29 | } |
| 30 | |
| 31 | static int get_max_inline_xattr_value_size(struct inode *inode, |
| 32 | struct ext4_iloc *iloc) |
| 33 | { |
| 34 | struct ext4_xattr_ibody_header *header; |
| 35 | struct ext4_xattr_entry *entry; |
| 36 | struct ext4_inode *raw_inode; |
| 37 | int free, min_offs; |
| 38 | |
| 39 | min_offs = EXT4_SB(inode->i_sb)->s_inode_size - |
| 40 | EXT4_GOOD_OLD_INODE_SIZE - |
| 41 | EXT4_I(inode)->i_extra_isize - |
| 42 | sizeof(struct ext4_xattr_ibody_header); |
| 43 | |
| 44 | /* |
| 45 | * We need to subtract another sizeof(__u32) since an in-inode xattr |
| 46 | * needs an empty 4 bytes to indicate the gap between the xattr entry |
| 47 | * and the name/value pair. |
| 48 | */ |
| 49 | if (!ext4_test_inode_state(inode, EXT4_STATE_XATTR)) |
| 50 | return EXT4_XATTR_SIZE(min_offs - |
| 51 | EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)) - |
| 52 | EXT4_XATTR_ROUND - sizeof(__u32)); |
| 53 | |
| 54 | raw_inode = ext4_raw_inode(iloc); |
| 55 | header = IHDR(inode, raw_inode); |
| 56 | entry = IFIRST(header); |
| 57 | |
| 58 | /* Compute min_offs. */ |
| 59 | for (; !IS_LAST_ENTRY(entry); entry = EXT4_XATTR_NEXT(entry)) { |
| 60 | if (!entry->e_value_block && entry->e_value_size) { |
| 61 | size_t offs = le16_to_cpu(entry->e_value_offs); |
| 62 | if (offs < min_offs) |
| 63 | min_offs = offs; |
| 64 | } |
| 65 | } |
| 66 | free = min_offs - |
| 67 | ((void *)entry - (void *)IFIRST(header)) - sizeof(__u32); |
| 68 | |
| 69 | if (EXT4_I(inode)->i_inline_off) { |
| 70 | entry = (struct ext4_xattr_entry *) |
| 71 | ((void *)raw_inode + EXT4_I(inode)->i_inline_off); |
| 72 | |
| 73 | free += le32_to_cpu(entry->e_value_size); |
| 74 | goto out; |
| 75 | } |
| 76 | |
| 77 | free -= EXT4_XATTR_LEN(strlen(EXT4_XATTR_SYSTEM_DATA)); |
| 78 | |
| 79 | if (free > EXT4_XATTR_ROUND) |
| 80 | free = EXT4_XATTR_SIZE(free - EXT4_XATTR_ROUND); |
| 81 | else |
| 82 | free = 0; |
| 83 | |
| 84 | out: |
| 85 | return free; |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Get the maximum size we now can store in an inode. |
| 90 | * If we can't find the space for a xattr entry, don't use the space |
| 91 | * of the extents since we have no space to indicate the inline data. |
| 92 | */ |
| 93 | int ext4_get_max_inline_size(struct inode *inode) |
| 94 | { |
| 95 | int error, max_inline_size; |
| 96 | struct ext4_iloc iloc; |
| 97 | |
| 98 | if (EXT4_I(inode)->i_extra_isize == 0) |
| 99 | return 0; |
| 100 | |
| 101 | error = ext4_get_inode_loc(inode, &iloc); |
| 102 | if (error) { |
| 103 | ext4_error_inode(inode, __func__, __LINE__, 0, |
| 104 | "can't get inode location %lu", |
| 105 | inode->i_ino); |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | down_read(&EXT4_I(inode)->xattr_sem); |
| 110 | max_inline_size = get_max_inline_xattr_value_size(inode, &iloc); |
| 111 | up_read(&EXT4_I(inode)->xattr_sem); |
| 112 | |
| 113 | brelse(iloc.bh); |
| 114 | |
| 115 | if (!max_inline_size) |
| 116 | return 0; |
| 117 | |
| 118 | return max_inline_size + EXT4_MIN_INLINE_DATA_SIZE; |
| 119 | } |
| 120 | |
| 121 | int ext4_has_inline_data(struct inode *inode) |
| 122 | { |
| 123 | return ext4_test_inode_flag(inode, EXT4_INODE_INLINE_DATA) && |
| 124 | EXT4_I(inode)->i_inline_off; |
| 125 | } |
| 126 | |
| 127 | /* |
| 128 | * this function does not take xattr_sem, which is OK because it is |
| 129 | * currently only used in a code path coming form ext4_iget, before |
| 130 | * the new inode has been unlocked |
| 131 | */ |
| 132 | int ext4_find_inline_data_nolock(struct inode *inode) |
| 133 | { |
| 134 | struct ext4_xattr_ibody_find is = { |
| 135 | .s = { .not_found = -ENODATA, }, |
| 136 | }; |
| 137 | struct ext4_xattr_info i = { |
| 138 | .name_index = EXT4_XATTR_INDEX_SYSTEM, |
| 139 | .name = EXT4_XATTR_SYSTEM_DATA, |
| 140 | }; |
| 141 | int error; |
| 142 | |
| 143 | if (EXT4_I(inode)->i_extra_isize == 0) |
| 144 | return 0; |
| 145 | |
| 146 | error = ext4_get_inode_loc(inode, &is.iloc); |
| 147 | if (error) |
| 148 | return error; |
| 149 | |
| 150 | error = ext4_xattr_ibody_find(inode, &i, &is); |
| 151 | if (error) |
| 152 | goto out; |
| 153 | |
| 154 | if (!is.s.not_found) { |
| 155 | EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here - |
| 156 | (void *)ext4_raw_inode(&is.iloc)); |
| 157 | EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE + |
| 158 | le32_to_cpu(is.s.here->e_value_size); |
| 159 | ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); |
| 160 | } |
| 161 | out: |
| 162 | brelse(is.iloc.bh); |
| 163 | return error; |
| 164 | } |
| 165 | |
| 166 | static int ext4_read_inline_data(struct inode *inode, void *buffer, |
| 167 | unsigned int len, |
| 168 | struct ext4_iloc *iloc) |
| 169 | { |
| 170 | struct ext4_xattr_entry *entry; |
| 171 | struct ext4_xattr_ibody_header *header; |
| 172 | int cp_len = 0; |
| 173 | struct ext4_inode *raw_inode; |
| 174 | |
| 175 | if (!len) |
| 176 | return 0; |
| 177 | |
| 178 | BUG_ON(len > EXT4_I(inode)->i_inline_size); |
| 179 | |
| 180 | cp_len = len < EXT4_MIN_INLINE_DATA_SIZE ? |
| 181 | len : EXT4_MIN_INLINE_DATA_SIZE; |
| 182 | |
| 183 | raw_inode = ext4_raw_inode(iloc); |
| 184 | memcpy(buffer, (void *)(raw_inode->i_block), cp_len); |
| 185 | |
| 186 | len -= cp_len; |
| 187 | buffer += cp_len; |
| 188 | |
| 189 | if (!len) |
| 190 | goto out; |
| 191 | |
| 192 | header = IHDR(inode, raw_inode); |
| 193 | entry = (struct ext4_xattr_entry *)((void *)raw_inode + |
| 194 | EXT4_I(inode)->i_inline_off); |
| 195 | len = min_t(unsigned int, len, |
| 196 | (unsigned int)le32_to_cpu(entry->e_value_size)); |
| 197 | |
| 198 | memcpy(buffer, |
| 199 | (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs), len); |
| 200 | cp_len += len; |
| 201 | |
| 202 | out: |
| 203 | return cp_len; |
| 204 | } |
| 205 | |
| 206 | /* |
| 207 | * write the buffer to the inline inode. |
| 208 | * If 'create' is set, we don't need to do the extra copy in the xattr |
| 209 | * value since it is already handled by ext4_xattr_ibody_set. That saves |
| 210 | * us one memcpy. |
| 211 | */ |
| 212 | void ext4_write_inline_data(struct inode *inode, struct ext4_iloc *iloc, |
| 213 | void *buffer, loff_t pos, unsigned int len) |
| 214 | { |
| 215 | struct ext4_xattr_entry *entry; |
| 216 | struct ext4_xattr_ibody_header *header; |
| 217 | struct ext4_inode *raw_inode; |
| 218 | int cp_len = 0; |
| 219 | |
| 220 | BUG_ON(!EXT4_I(inode)->i_inline_off); |
| 221 | BUG_ON(pos + len > EXT4_I(inode)->i_inline_size); |
| 222 | |
| 223 | raw_inode = ext4_raw_inode(iloc); |
| 224 | buffer += pos; |
| 225 | |
| 226 | if (pos < EXT4_MIN_INLINE_DATA_SIZE) { |
| 227 | cp_len = pos + len > EXT4_MIN_INLINE_DATA_SIZE ? |
| 228 | EXT4_MIN_INLINE_DATA_SIZE - pos : len; |
| 229 | memcpy((void *)raw_inode->i_block + pos, buffer, cp_len); |
| 230 | |
| 231 | len -= cp_len; |
| 232 | buffer += cp_len; |
| 233 | pos += cp_len; |
| 234 | } |
| 235 | |
| 236 | if (!len) |
| 237 | return; |
| 238 | |
| 239 | pos -= EXT4_MIN_INLINE_DATA_SIZE; |
| 240 | header = IHDR(inode, raw_inode); |
| 241 | entry = (struct ext4_xattr_entry *)((void *)raw_inode + |
| 242 | EXT4_I(inode)->i_inline_off); |
| 243 | |
| 244 | memcpy((void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs) + pos, |
| 245 | buffer, len); |
| 246 | } |
| 247 | |
| 248 | static int ext4_create_inline_data(handle_t *handle, |
| 249 | struct inode *inode, unsigned len) |
| 250 | { |
| 251 | int error; |
| 252 | void *value = NULL; |
| 253 | struct ext4_xattr_ibody_find is = { |
| 254 | .s = { .not_found = -ENODATA, }, |
| 255 | }; |
| 256 | struct ext4_xattr_info i = { |
| 257 | .name_index = EXT4_XATTR_INDEX_SYSTEM, |
| 258 | .name = EXT4_XATTR_SYSTEM_DATA, |
| 259 | }; |
| 260 | |
| 261 | error = ext4_get_inode_loc(inode, &is.iloc); |
| 262 | if (error) |
| 263 | return error; |
| 264 | |
| 265 | error = ext4_journal_get_write_access(handle, is.iloc.bh); |
| 266 | if (error) |
| 267 | goto out; |
| 268 | |
| 269 | if (len > EXT4_MIN_INLINE_DATA_SIZE) { |
| 270 | value = (void *)empty_zero_page; |
| 271 | len -= EXT4_MIN_INLINE_DATA_SIZE; |
| 272 | } else { |
| 273 | value = ""; |
| 274 | len = 0; |
| 275 | } |
| 276 | |
| 277 | /* Insert the the xttr entry. */ |
| 278 | i.value = value; |
| 279 | i.value_len = len; |
| 280 | |
| 281 | error = ext4_xattr_ibody_find(inode, &i, &is); |
| 282 | if (error) |
| 283 | goto out; |
| 284 | |
| 285 | BUG_ON(!is.s.not_found); |
| 286 | |
| 287 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); |
| 288 | if (error) { |
| 289 | if (error == -ENOSPC) |
| 290 | ext4_clear_inode_state(inode, |
| 291 | EXT4_STATE_MAY_INLINE_DATA); |
| 292 | goto out; |
| 293 | } |
| 294 | |
| 295 | memset((void *)ext4_raw_inode(&is.iloc)->i_block, |
| 296 | 0, EXT4_MIN_INLINE_DATA_SIZE); |
| 297 | |
| 298 | EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here - |
| 299 | (void *)ext4_raw_inode(&is.iloc)); |
| 300 | EXT4_I(inode)->i_inline_size = len + EXT4_MIN_INLINE_DATA_SIZE; |
| 301 | ext4_clear_inode_flag(inode, EXT4_INODE_EXTENTS); |
| 302 | ext4_set_inode_flag(inode, EXT4_INODE_INLINE_DATA); |
| 303 | get_bh(is.iloc.bh); |
| 304 | error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); |
| 305 | |
| 306 | out: |
| 307 | brelse(is.iloc.bh); |
| 308 | return error; |
| 309 | } |
| 310 | |
| 311 | static int ext4_update_inline_data(handle_t *handle, struct inode *inode, |
| 312 | unsigned int len) |
| 313 | { |
| 314 | int error; |
| 315 | void *value = NULL; |
| 316 | struct ext4_xattr_ibody_find is = { |
| 317 | .s = { .not_found = -ENODATA, }, |
| 318 | }; |
| 319 | struct ext4_xattr_info i = { |
| 320 | .name_index = EXT4_XATTR_INDEX_SYSTEM, |
| 321 | .name = EXT4_XATTR_SYSTEM_DATA, |
| 322 | }; |
| 323 | |
| 324 | /* If the old space is ok, write the data directly. */ |
| 325 | if (len <= EXT4_I(inode)->i_inline_size) |
| 326 | return 0; |
| 327 | |
| 328 | error = ext4_get_inode_loc(inode, &is.iloc); |
| 329 | if (error) |
| 330 | return error; |
| 331 | |
| 332 | error = ext4_xattr_ibody_find(inode, &i, &is); |
| 333 | if (error) |
| 334 | goto out; |
| 335 | |
| 336 | BUG_ON(is.s.not_found); |
| 337 | |
| 338 | len -= EXT4_MIN_INLINE_DATA_SIZE; |
| 339 | value = kzalloc(len, GFP_NOFS); |
| 340 | if (!value) |
| 341 | goto out; |
| 342 | |
| 343 | error = ext4_xattr_ibody_get(inode, i.name_index, i.name, |
| 344 | value, len); |
| 345 | if (error == -ENODATA) |
| 346 | goto out; |
| 347 | |
| 348 | error = ext4_journal_get_write_access(handle, is.iloc.bh); |
| 349 | if (error) |
| 350 | goto out; |
| 351 | |
| 352 | /* Update the xttr entry. */ |
| 353 | i.value = value; |
| 354 | i.value_len = len; |
| 355 | |
| 356 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); |
| 357 | if (error) |
| 358 | goto out; |
| 359 | |
| 360 | EXT4_I(inode)->i_inline_off = (u16)((void *)is.s.here - |
| 361 | (void *)ext4_raw_inode(&is.iloc)); |
| 362 | EXT4_I(inode)->i_inline_size = EXT4_MIN_INLINE_DATA_SIZE + |
| 363 | le32_to_cpu(is.s.here->e_value_size); |
| 364 | ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); |
| 365 | get_bh(is.iloc.bh); |
| 366 | error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); |
| 367 | |
| 368 | out: |
| 369 | kfree(value); |
| 370 | brelse(is.iloc.bh); |
| 371 | return error; |
| 372 | } |
| 373 | |
| 374 | int ext4_prepare_inline_data(handle_t *handle, struct inode *inode, |
| 375 | unsigned int len) |
| 376 | { |
| 377 | int ret, size; |
| 378 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 379 | |
| 380 | if (!ext4_test_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA)) |
| 381 | return -ENOSPC; |
| 382 | |
| 383 | size = ext4_get_max_inline_size(inode); |
| 384 | if (size < len) |
| 385 | return -ENOSPC; |
| 386 | |
| 387 | down_write(&EXT4_I(inode)->xattr_sem); |
| 388 | |
| 389 | if (ei->i_inline_off) |
| 390 | ret = ext4_update_inline_data(handle, inode, len); |
| 391 | else |
| 392 | ret = ext4_create_inline_data(handle, inode, len); |
| 393 | |
| 394 | up_write(&EXT4_I(inode)->xattr_sem); |
| 395 | |
| 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | static int ext4_destroy_inline_data_nolock(handle_t *handle, |
| 400 | struct inode *inode) |
| 401 | { |
| 402 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 403 | struct ext4_xattr_ibody_find is = { |
| 404 | .s = { .not_found = 0, }, |
| 405 | }; |
| 406 | struct ext4_xattr_info i = { |
| 407 | .name_index = EXT4_XATTR_INDEX_SYSTEM, |
| 408 | .name = EXT4_XATTR_SYSTEM_DATA, |
| 409 | .value = NULL, |
| 410 | .value_len = 0, |
| 411 | }; |
| 412 | int error; |
| 413 | |
| 414 | if (!ei->i_inline_off) |
| 415 | return 0; |
| 416 | |
| 417 | error = ext4_get_inode_loc(inode, &is.iloc); |
| 418 | if (error) |
| 419 | return error; |
| 420 | |
| 421 | error = ext4_xattr_ibody_find(inode, &i, &is); |
| 422 | if (error) |
| 423 | goto out; |
| 424 | |
| 425 | error = ext4_journal_get_write_access(handle, is.iloc.bh); |
| 426 | if (error) |
| 427 | goto out; |
| 428 | |
| 429 | error = ext4_xattr_ibody_set(handle, inode, &i, &is); |
| 430 | if (error) |
| 431 | goto out; |
| 432 | |
| 433 | memset((void *)ext4_raw_inode(&is.iloc)->i_block, |
| 434 | 0, EXT4_MIN_INLINE_DATA_SIZE); |
| 435 | |
| 436 | if (EXT4_HAS_INCOMPAT_FEATURE(inode->i_sb, |
| 437 | EXT4_FEATURE_INCOMPAT_EXTENTS)) { |
| 438 | if (S_ISDIR(inode->i_mode) || |
| 439 | S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) { |
| 440 | ext4_set_inode_flag(inode, EXT4_INODE_EXTENTS); |
| 441 | ext4_ext_tree_init(handle, inode); |
| 442 | } |
| 443 | } |
| 444 | ext4_clear_inode_flag(inode, EXT4_INODE_INLINE_DATA); |
| 445 | |
| 446 | get_bh(is.iloc.bh); |
| 447 | error = ext4_mark_iloc_dirty(handle, inode, &is.iloc); |
| 448 | |
| 449 | EXT4_I(inode)->i_inline_off = 0; |
| 450 | EXT4_I(inode)->i_inline_size = 0; |
| 451 | ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); |
| 452 | out: |
| 453 | brelse(is.iloc.bh); |
| 454 | if (error == -ENODATA) |
| 455 | error = 0; |
| 456 | return error; |
| 457 | } |
| 458 | |
Tao Ma | 46c7f25 | 2012-12-10 14:04:52 -0500 | [diff] [blame] | 459 | static int ext4_read_inline_page(struct inode *inode, struct page *page) |
| 460 | { |
| 461 | void *kaddr; |
| 462 | int ret = 0; |
| 463 | size_t len; |
| 464 | struct ext4_iloc iloc; |
| 465 | |
| 466 | BUG_ON(!PageLocked(page)); |
| 467 | BUG_ON(!ext4_has_inline_data(inode)); |
| 468 | BUG_ON(page->index); |
| 469 | |
| 470 | if (!EXT4_I(inode)->i_inline_off) { |
| 471 | ext4_warning(inode->i_sb, "inode %lu doesn't have inline data.", |
| 472 | inode->i_ino); |
| 473 | goto out; |
| 474 | } |
| 475 | |
| 476 | ret = ext4_get_inode_loc(inode, &iloc); |
| 477 | if (ret) |
| 478 | goto out; |
| 479 | |
| 480 | len = min_t(size_t, ext4_get_inline_size(inode), i_size_read(inode)); |
| 481 | kaddr = kmap_atomic(page); |
| 482 | ret = ext4_read_inline_data(inode, kaddr, len, &iloc); |
| 483 | flush_dcache_page(page); |
| 484 | kunmap_atomic(kaddr); |
| 485 | zero_user_segment(page, len, PAGE_CACHE_SIZE); |
| 486 | SetPageUptodate(page); |
| 487 | brelse(iloc.bh); |
| 488 | |
| 489 | out: |
| 490 | return ret; |
| 491 | } |
| 492 | |
| 493 | int ext4_readpage_inline(struct inode *inode, struct page *page) |
| 494 | { |
| 495 | int ret = 0; |
| 496 | |
| 497 | down_read(&EXT4_I(inode)->xattr_sem); |
| 498 | if (!ext4_has_inline_data(inode)) { |
| 499 | up_read(&EXT4_I(inode)->xattr_sem); |
| 500 | return -EAGAIN; |
| 501 | } |
| 502 | |
| 503 | /* |
| 504 | * Current inline data can only exist in the 1st page, |
| 505 | * So for all the other pages, just set them uptodate. |
| 506 | */ |
| 507 | if (!page->index) |
| 508 | ret = ext4_read_inline_page(inode, page); |
| 509 | else if (!PageUptodate(page)) { |
| 510 | zero_user_segment(page, 0, PAGE_CACHE_SIZE); |
| 511 | SetPageUptodate(page); |
| 512 | } |
| 513 | |
| 514 | up_read(&EXT4_I(inode)->xattr_sem); |
| 515 | |
| 516 | unlock_page(page); |
| 517 | return ret >= 0 ? 0 : ret; |
| 518 | } |
| 519 | |
Tao Ma | f19d587 | 2012-12-10 14:05:51 -0500 | [diff] [blame] | 520 | static int ext4_convert_inline_data_to_extent(struct address_space *mapping, |
| 521 | struct inode *inode, |
| 522 | unsigned flags) |
| 523 | { |
| 524 | int ret, needed_blocks; |
| 525 | handle_t *handle = NULL; |
| 526 | int retries = 0, sem_held = 0; |
| 527 | struct page *page = NULL; |
| 528 | unsigned from, to; |
| 529 | struct ext4_iloc iloc; |
| 530 | |
| 531 | if (!ext4_has_inline_data(inode)) { |
| 532 | /* |
| 533 | * clear the flag so that no new write |
| 534 | * will trap here again. |
| 535 | */ |
| 536 | ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | needed_blocks = ext4_writepage_trans_blocks(inode); |
| 541 | |
| 542 | ret = ext4_get_inode_loc(inode, &iloc); |
| 543 | if (ret) |
| 544 | return ret; |
| 545 | |
| 546 | retry: |
| 547 | handle = ext4_journal_start(inode, needed_blocks); |
| 548 | if (IS_ERR(handle)) { |
| 549 | ret = PTR_ERR(handle); |
| 550 | handle = NULL; |
| 551 | goto out; |
| 552 | } |
| 553 | |
| 554 | /* We cannot recurse into the filesystem as the transaction is already |
| 555 | * started */ |
| 556 | flags |= AOP_FLAG_NOFS; |
| 557 | |
| 558 | page = grab_cache_page_write_begin(mapping, 0, flags); |
| 559 | if (!page) { |
| 560 | ret = -ENOMEM; |
| 561 | goto out; |
| 562 | } |
| 563 | |
| 564 | down_write(&EXT4_I(inode)->xattr_sem); |
| 565 | sem_held = 1; |
| 566 | /* If some one has already done this for us, just exit. */ |
| 567 | if (!ext4_has_inline_data(inode)) { |
| 568 | ret = 0; |
| 569 | goto out; |
| 570 | } |
| 571 | |
| 572 | from = 0; |
| 573 | to = ext4_get_inline_size(inode); |
| 574 | if (!PageUptodate(page)) { |
| 575 | ret = ext4_read_inline_page(inode, page); |
| 576 | if (ret < 0) |
| 577 | goto out; |
| 578 | } |
| 579 | |
| 580 | ret = ext4_destroy_inline_data_nolock(handle, inode); |
| 581 | if (ret) |
| 582 | goto out; |
| 583 | |
| 584 | if (ext4_should_dioread_nolock(inode)) |
| 585 | ret = __block_write_begin(page, from, to, ext4_get_block_write); |
| 586 | else |
| 587 | ret = __block_write_begin(page, from, to, ext4_get_block); |
| 588 | |
| 589 | if (!ret && ext4_should_journal_data(inode)) { |
| 590 | ret = ext4_walk_page_buffers(handle, page_buffers(page), |
| 591 | from, to, NULL, |
| 592 | do_journal_get_write_access); |
| 593 | } |
| 594 | |
| 595 | if (ret) { |
| 596 | unlock_page(page); |
| 597 | page_cache_release(page); |
| 598 | ext4_orphan_add(handle, inode); |
| 599 | up_write(&EXT4_I(inode)->xattr_sem); |
| 600 | sem_held = 0; |
| 601 | ext4_journal_stop(handle); |
| 602 | handle = NULL; |
| 603 | ext4_truncate_failed_write(inode); |
| 604 | /* |
| 605 | * If truncate failed early the inode might |
| 606 | * still be on the orphan list; we need to |
| 607 | * make sure the inode is removed from the |
| 608 | * orphan list in that case. |
| 609 | */ |
| 610 | if (inode->i_nlink) |
| 611 | ext4_orphan_del(NULL, inode); |
| 612 | } |
| 613 | |
| 614 | if (ret == -ENOSPC && ext4_should_retry_alloc(inode->i_sb, &retries)) |
| 615 | goto retry; |
| 616 | |
| 617 | block_commit_write(page, from, to); |
| 618 | out: |
| 619 | if (page) { |
| 620 | unlock_page(page); |
| 621 | page_cache_release(page); |
| 622 | } |
| 623 | if (sem_held) |
| 624 | up_write(&EXT4_I(inode)->xattr_sem); |
| 625 | if (handle) |
| 626 | ext4_journal_stop(handle); |
| 627 | brelse(iloc.bh); |
| 628 | return ret; |
| 629 | } |
| 630 | |
| 631 | /* |
| 632 | * Try to write data in the inode. |
| 633 | * If the inode has inline data, check whether the new write can be |
| 634 | * in the inode also. If not, create the page the handle, move the data |
| 635 | * to the page make it update and let the later codes create extent for it. |
| 636 | */ |
| 637 | int ext4_try_to_write_inline_data(struct address_space *mapping, |
| 638 | struct inode *inode, |
| 639 | loff_t pos, unsigned len, |
| 640 | unsigned flags, |
| 641 | struct page **pagep) |
| 642 | { |
| 643 | int ret; |
| 644 | handle_t *handle; |
| 645 | struct page *page; |
| 646 | struct ext4_iloc iloc; |
| 647 | |
| 648 | if (pos + len > ext4_get_max_inline_size(inode)) |
| 649 | goto convert; |
| 650 | |
| 651 | ret = ext4_get_inode_loc(inode, &iloc); |
| 652 | if (ret) |
| 653 | return ret; |
| 654 | |
| 655 | /* |
| 656 | * The possible write could happen in the inode, |
| 657 | * so try to reserve the space in inode first. |
| 658 | */ |
| 659 | handle = ext4_journal_start(inode, 1); |
| 660 | if (IS_ERR(handle)) { |
| 661 | ret = PTR_ERR(handle); |
| 662 | handle = NULL; |
| 663 | goto out; |
| 664 | } |
| 665 | |
| 666 | ret = ext4_prepare_inline_data(handle, inode, pos + len); |
| 667 | if (ret && ret != -ENOSPC) |
| 668 | goto out; |
| 669 | |
| 670 | /* We don't have space in inline inode, so convert it to extent. */ |
| 671 | if (ret == -ENOSPC) { |
| 672 | ext4_journal_stop(handle); |
| 673 | brelse(iloc.bh); |
| 674 | goto convert; |
| 675 | } |
| 676 | |
| 677 | flags |= AOP_FLAG_NOFS; |
| 678 | |
| 679 | page = grab_cache_page_write_begin(mapping, 0, flags); |
| 680 | if (!page) { |
| 681 | ret = -ENOMEM; |
| 682 | goto out; |
| 683 | } |
| 684 | |
| 685 | *pagep = page; |
| 686 | down_read(&EXT4_I(inode)->xattr_sem); |
| 687 | if (!ext4_has_inline_data(inode)) { |
| 688 | ret = 0; |
| 689 | unlock_page(page); |
| 690 | page_cache_release(page); |
| 691 | goto out_up_read; |
| 692 | } |
| 693 | |
| 694 | if (!PageUptodate(page)) { |
| 695 | ret = ext4_read_inline_page(inode, page); |
| 696 | if (ret < 0) |
| 697 | goto out_up_read; |
| 698 | } |
| 699 | |
| 700 | ret = 1; |
| 701 | handle = NULL; |
| 702 | out_up_read: |
| 703 | up_read(&EXT4_I(inode)->xattr_sem); |
| 704 | out: |
| 705 | if (handle) |
| 706 | ext4_journal_stop(handle); |
| 707 | brelse(iloc.bh); |
| 708 | return ret; |
| 709 | convert: |
| 710 | return ext4_convert_inline_data_to_extent(mapping, |
| 711 | inode, flags); |
| 712 | } |
| 713 | |
| 714 | int ext4_write_inline_data_end(struct inode *inode, loff_t pos, unsigned len, |
| 715 | unsigned copied, struct page *page) |
| 716 | { |
| 717 | int ret; |
| 718 | void *kaddr; |
| 719 | struct ext4_iloc iloc; |
| 720 | |
| 721 | if (unlikely(copied < len)) { |
| 722 | if (!PageUptodate(page)) { |
| 723 | copied = 0; |
| 724 | goto out; |
| 725 | } |
| 726 | } |
| 727 | |
| 728 | ret = ext4_get_inode_loc(inode, &iloc); |
| 729 | if (ret) { |
| 730 | ext4_std_error(inode->i_sb, ret); |
| 731 | copied = 0; |
| 732 | goto out; |
| 733 | } |
| 734 | |
| 735 | down_write(&EXT4_I(inode)->xattr_sem); |
| 736 | BUG_ON(!ext4_has_inline_data(inode)); |
| 737 | |
| 738 | kaddr = kmap_atomic(page); |
| 739 | ext4_write_inline_data(inode, &iloc, kaddr, pos, len); |
| 740 | kunmap_atomic(kaddr); |
| 741 | SetPageUptodate(page); |
| 742 | /* clear page dirty so that writepages wouldn't work for us. */ |
| 743 | ClearPageDirty(page); |
| 744 | |
| 745 | up_write(&EXT4_I(inode)->xattr_sem); |
| 746 | brelse(iloc.bh); |
| 747 | out: |
| 748 | return copied; |
| 749 | } |
| 750 | |
Tao Ma | 3fdcfb6 | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 751 | struct buffer_head * |
| 752 | ext4_journalled_write_inline_data(struct inode *inode, |
| 753 | unsigned len, |
| 754 | struct page *page) |
| 755 | { |
| 756 | int ret; |
| 757 | void *kaddr; |
| 758 | struct ext4_iloc iloc; |
| 759 | |
| 760 | ret = ext4_get_inode_loc(inode, &iloc); |
| 761 | if (ret) { |
| 762 | ext4_std_error(inode->i_sb, ret); |
| 763 | return NULL; |
| 764 | } |
| 765 | |
| 766 | down_write(&EXT4_I(inode)->xattr_sem); |
| 767 | kaddr = kmap_atomic(page); |
| 768 | ext4_write_inline_data(inode, &iloc, kaddr, 0, len); |
| 769 | kunmap_atomic(kaddr); |
| 770 | up_write(&EXT4_I(inode)->xattr_sem); |
| 771 | |
| 772 | return iloc.bh; |
| 773 | } |
| 774 | |
Tao Ma | 9c3569b | 2012-12-10 14:05:57 -0500 | [diff] [blame] | 775 | /* |
| 776 | * Try to make the page cache and handle ready for the inline data case. |
| 777 | * We can call this function in 2 cases: |
| 778 | * 1. The inode is created and the first write exceeds inline size. We can |
| 779 | * clear the inode state safely. |
| 780 | * 2. The inode has inline data, then we need to read the data, make it |
| 781 | * update and dirty so that ext4_da_writepages can handle it. We don't |
| 782 | * need to start the journal since the file's metatdata isn't changed now. |
| 783 | */ |
| 784 | static int ext4_da_convert_inline_data_to_extent(struct address_space *mapping, |
| 785 | struct inode *inode, |
| 786 | unsigned flags, |
| 787 | void **fsdata) |
| 788 | { |
| 789 | int ret = 0, inline_size; |
| 790 | struct page *page; |
| 791 | |
| 792 | page = grab_cache_page_write_begin(mapping, 0, flags); |
| 793 | if (!page) |
| 794 | return -ENOMEM; |
| 795 | |
| 796 | down_read(&EXT4_I(inode)->xattr_sem); |
| 797 | if (!ext4_has_inline_data(inode)) { |
| 798 | ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); |
| 799 | goto out; |
| 800 | } |
| 801 | |
| 802 | inline_size = ext4_get_inline_size(inode); |
| 803 | |
| 804 | if (!PageUptodate(page)) { |
| 805 | ret = ext4_read_inline_page(inode, page); |
| 806 | if (ret < 0) |
| 807 | goto out; |
| 808 | } |
| 809 | |
| 810 | ret = __block_write_begin(page, 0, inline_size, |
| 811 | ext4_da_get_block_prep); |
| 812 | if (ret) { |
| 813 | ext4_truncate_failed_write(inode); |
| 814 | goto out; |
| 815 | } |
| 816 | |
| 817 | SetPageDirty(page); |
| 818 | SetPageUptodate(page); |
| 819 | ext4_clear_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); |
| 820 | *fsdata = (void *)CONVERT_INLINE_DATA; |
| 821 | |
| 822 | out: |
| 823 | up_read(&EXT4_I(inode)->xattr_sem); |
| 824 | if (page) { |
| 825 | unlock_page(page); |
| 826 | page_cache_release(page); |
| 827 | } |
| 828 | return ret; |
| 829 | } |
| 830 | |
| 831 | /* |
| 832 | * Prepare the write for the inline data. |
| 833 | * If the the data can be written into the inode, we just read |
| 834 | * the page and make it uptodate, and start the journal. |
| 835 | * Otherwise read the page, makes it dirty so that it can be |
| 836 | * handle in writepages(the i_disksize update is left to the |
| 837 | * normal ext4_da_write_end). |
| 838 | */ |
| 839 | int ext4_da_write_inline_data_begin(struct address_space *mapping, |
| 840 | struct inode *inode, |
| 841 | loff_t pos, unsigned len, |
| 842 | unsigned flags, |
| 843 | struct page **pagep, |
| 844 | void **fsdata) |
| 845 | { |
| 846 | int ret, inline_size; |
| 847 | handle_t *handle; |
| 848 | struct page *page; |
| 849 | struct ext4_iloc iloc; |
| 850 | |
| 851 | ret = ext4_get_inode_loc(inode, &iloc); |
| 852 | if (ret) |
| 853 | return ret; |
| 854 | |
| 855 | handle = ext4_journal_start(inode, 1); |
| 856 | if (IS_ERR(handle)) { |
| 857 | ret = PTR_ERR(handle); |
| 858 | handle = NULL; |
| 859 | goto out; |
| 860 | } |
| 861 | |
| 862 | inline_size = ext4_get_max_inline_size(inode); |
| 863 | |
| 864 | ret = -ENOSPC; |
| 865 | if (inline_size >= pos + len) { |
| 866 | ret = ext4_prepare_inline_data(handle, inode, pos + len); |
| 867 | if (ret && ret != -ENOSPC) |
| 868 | goto out; |
| 869 | } |
| 870 | |
| 871 | if (ret == -ENOSPC) { |
| 872 | ret = ext4_da_convert_inline_data_to_extent(mapping, |
| 873 | inode, |
| 874 | flags, |
| 875 | fsdata); |
| 876 | goto out; |
| 877 | } |
| 878 | |
| 879 | /* |
| 880 | * We cannot recurse into the filesystem as the transaction |
| 881 | * is already started. |
| 882 | */ |
| 883 | flags |= AOP_FLAG_NOFS; |
| 884 | |
| 885 | page = grab_cache_page_write_begin(mapping, 0, flags); |
| 886 | if (!page) { |
| 887 | ret = -ENOMEM; |
| 888 | goto out; |
| 889 | } |
| 890 | |
| 891 | down_read(&EXT4_I(inode)->xattr_sem); |
| 892 | if (!ext4_has_inline_data(inode)) { |
| 893 | ret = 0; |
| 894 | goto out_release_page; |
| 895 | } |
| 896 | |
| 897 | if (!PageUptodate(page)) { |
| 898 | ret = ext4_read_inline_page(inode, page); |
| 899 | if (ret < 0) |
| 900 | goto out_release_page; |
| 901 | } |
| 902 | |
| 903 | up_read(&EXT4_I(inode)->xattr_sem); |
| 904 | *pagep = page; |
| 905 | handle = NULL; |
| 906 | brelse(iloc.bh); |
| 907 | return 1; |
| 908 | out_release_page: |
| 909 | up_read(&EXT4_I(inode)->xattr_sem); |
| 910 | unlock_page(page); |
| 911 | page_cache_release(page); |
| 912 | out: |
| 913 | if (handle) |
| 914 | ext4_journal_stop(handle); |
| 915 | brelse(iloc.bh); |
| 916 | return ret; |
| 917 | } |
| 918 | |
| 919 | int ext4_da_write_inline_data_end(struct inode *inode, loff_t pos, |
| 920 | unsigned len, unsigned copied, |
| 921 | struct page *page) |
| 922 | { |
| 923 | int i_size_changed = 0; |
| 924 | |
| 925 | copied = ext4_write_inline_data_end(inode, pos, len, copied, page); |
| 926 | |
| 927 | /* |
| 928 | * No need to use i_size_read() here, the i_size |
| 929 | * cannot change under us because we hold i_mutex. |
| 930 | * |
| 931 | * But it's important to update i_size while still holding page lock: |
| 932 | * page writeout could otherwise come in and zero beyond i_size. |
| 933 | */ |
| 934 | if (pos+copied > inode->i_size) { |
| 935 | i_size_write(inode, pos+copied); |
| 936 | i_size_changed = 1; |
| 937 | } |
| 938 | unlock_page(page); |
| 939 | page_cache_release(page); |
| 940 | |
| 941 | /* |
| 942 | * Don't mark the inode dirty under page lock. First, it unnecessarily |
| 943 | * makes the holding time of page lock longer. Second, it forces lock |
| 944 | * ordering of page lock and transaction start for journaling |
| 945 | * filesystems. |
| 946 | */ |
| 947 | if (i_size_changed) |
| 948 | mark_inode_dirty(inode); |
| 949 | |
| 950 | return copied; |
| 951 | } |
Tao Ma | f19d587 | 2012-12-10 14:05:51 -0500 | [diff] [blame] | 952 | |
Tao Ma | 3c47d54 | 2012-12-10 14:05:59 -0500 | [diff] [blame^] | 953 | #ifdef INLINE_DIR_DEBUG |
| 954 | void ext4_show_inline_dir(struct inode *dir, struct buffer_head *bh, |
| 955 | void *inline_start, int inline_size) |
| 956 | { |
| 957 | int offset; |
| 958 | unsigned short de_len; |
| 959 | struct ext4_dir_entry_2 *de = inline_start; |
| 960 | void *dlimit = inline_start + inline_size; |
| 961 | |
| 962 | trace_printk("inode %lu\n", dir->i_ino); |
| 963 | offset = 0; |
| 964 | while ((void *)de < dlimit) { |
| 965 | de_len = ext4_rec_len_from_disk(de->rec_len, inline_size); |
| 966 | trace_printk("de: off %u rlen %u name %*.s nlen %u ino %u\n", |
| 967 | offset, de_len, de->name_len, de->name, |
| 968 | de->name_len, le32_to_cpu(de->inode)); |
| 969 | if (ext4_check_dir_entry(dir, NULL, de, bh, |
| 970 | inline_start, inline_size, offset)) |
| 971 | BUG(); |
| 972 | |
| 973 | offset += de_len; |
| 974 | de = (struct ext4_dir_entry_2 *) ((char *) de + de_len); |
| 975 | } |
| 976 | } |
| 977 | #else |
| 978 | #define ext4_show_inline_dir(dir, bh, inline_start, inline_size) |
| 979 | #endif |
| 980 | |
| 981 | /* |
| 982 | * Add a new entry into a inline dir. |
| 983 | * It will return -ENOSPC if no space is available, and -EIO |
| 984 | * and -EEXIST if directory entry already exists. |
| 985 | */ |
| 986 | static int ext4_add_dirent_to_inline(handle_t *handle, |
| 987 | struct dentry *dentry, |
| 988 | struct inode *inode, |
| 989 | struct ext4_iloc *iloc, |
| 990 | void *inline_start, int inline_size) |
| 991 | { |
| 992 | struct inode *dir = dentry->d_parent->d_inode; |
| 993 | const char *name = dentry->d_name.name; |
| 994 | int namelen = dentry->d_name.len; |
| 995 | unsigned short reclen; |
| 996 | int err; |
| 997 | struct ext4_dir_entry_2 *de; |
| 998 | |
| 999 | reclen = EXT4_DIR_REC_LEN(namelen); |
| 1000 | err = ext4_find_dest_de(dir, inode, iloc->bh, |
| 1001 | inline_start, inline_size, |
| 1002 | name, namelen, &de); |
| 1003 | if (err) |
| 1004 | return err; |
| 1005 | |
| 1006 | err = ext4_journal_get_write_access(handle, iloc->bh); |
| 1007 | if (err) |
| 1008 | return err; |
| 1009 | ext4_insert_dentry(inode, de, inline_size, name, namelen); |
| 1010 | |
| 1011 | ext4_show_inline_dir(dir, iloc->bh, inline_start, inline_size); |
| 1012 | |
| 1013 | /* |
| 1014 | * XXX shouldn't update any times until successful |
| 1015 | * completion of syscall, but too many callers depend |
| 1016 | * on this. |
| 1017 | * |
| 1018 | * XXX similarly, too many callers depend on |
| 1019 | * ext4_new_inode() setting the times, but error |
| 1020 | * recovery deletes the inode, so the worst that can |
| 1021 | * happen is that the times are slightly out of date |
| 1022 | * and/or different from the directory change time. |
| 1023 | */ |
| 1024 | dir->i_mtime = dir->i_ctime = ext4_current_time(dir); |
| 1025 | ext4_update_dx_flag(dir); |
| 1026 | dir->i_version++; |
| 1027 | ext4_mark_inode_dirty(handle, dir); |
| 1028 | return 1; |
| 1029 | } |
| 1030 | |
| 1031 | static void *ext4_get_inline_xattr_pos(struct inode *inode, |
| 1032 | struct ext4_iloc *iloc) |
| 1033 | { |
| 1034 | struct ext4_xattr_entry *entry; |
| 1035 | struct ext4_xattr_ibody_header *header; |
| 1036 | |
| 1037 | BUG_ON(!EXT4_I(inode)->i_inline_off); |
| 1038 | |
| 1039 | header = IHDR(inode, ext4_raw_inode(iloc)); |
| 1040 | entry = (struct ext4_xattr_entry *)((void *)ext4_raw_inode(iloc) + |
| 1041 | EXT4_I(inode)->i_inline_off); |
| 1042 | |
| 1043 | return (void *)IFIRST(header) + le16_to_cpu(entry->e_value_offs); |
| 1044 | } |
| 1045 | |
| 1046 | /* Set the final de to cover the whole block. */ |
| 1047 | static void ext4_update_final_de(void *de_buf, int old_size, int new_size) |
| 1048 | { |
| 1049 | struct ext4_dir_entry_2 *de, *prev_de; |
| 1050 | void *limit; |
| 1051 | int de_len; |
| 1052 | |
| 1053 | de = (struct ext4_dir_entry_2 *)de_buf; |
| 1054 | if (old_size) { |
| 1055 | limit = de_buf + old_size; |
| 1056 | do { |
| 1057 | prev_de = de; |
| 1058 | de_len = ext4_rec_len_from_disk(de->rec_len, old_size); |
| 1059 | de_buf += de_len; |
| 1060 | de = (struct ext4_dir_entry_2 *)de_buf; |
| 1061 | } while (de_buf < limit); |
| 1062 | |
| 1063 | prev_de->rec_len = ext4_rec_len_to_disk(de_len + new_size - |
| 1064 | old_size, new_size); |
| 1065 | } else { |
| 1066 | /* this is just created, so create an empty entry. */ |
| 1067 | de->inode = 0; |
| 1068 | de->rec_len = ext4_rec_len_to_disk(new_size, new_size); |
| 1069 | } |
| 1070 | } |
| 1071 | |
| 1072 | static int ext4_update_inline_dir(handle_t *handle, struct inode *dir, |
| 1073 | struct ext4_iloc *iloc) |
| 1074 | { |
| 1075 | int ret; |
| 1076 | int old_size = EXT4_I(dir)->i_inline_size - EXT4_MIN_INLINE_DATA_SIZE; |
| 1077 | int new_size = get_max_inline_xattr_value_size(dir, iloc); |
| 1078 | |
| 1079 | if (new_size - old_size <= EXT4_DIR_REC_LEN(1)) |
| 1080 | return -ENOSPC; |
| 1081 | |
| 1082 | ret = ext4_update_inline_data(handle, dir, |
| 1083 | new_size + EXT4_MIN_INLINE_DATA_SIZE); |
| 1084 | if (ret) |
| 1085 | return ret; |
| 1086 | |
| 1087 | ext4_update_final_de(ext4_get_inline_xattr_pos(dir, iloc), old_size, |
| 1088 | EXT4_I(dir)->i_inline_size - |
| 1089 | EXT4_MIN_INLINE_DATA_SIZE); |
| 1090 | dir->i_size = EXT4_I(dir)->i_disksize = EXT4_I(dir)->i_inline_size; |
| 1091 | return 0; |
| 1092 | } |
| 1093 | |
| 1094 | static void ext4_restore_inline_data(handle_t *handle, struct inode *inode, |
| 1095 | struct ext4_iloc *iloc, |
| 1096 | void *buf, int inline_size) |
| 1097 | { |
| 1098 | ext4_create_inline_data(handle, inode, inline_size); |
| 1099 | ext4_write_inline_data(inode, iloc, buf, 0, inline_size); |
| 1100 | ext4_set_inode_state(inode, EXT4_STATE_MAY_INLINE_DATA); |
| 1101 | } |
| 1102 | |
| 1103 | static int ext4_finish_convert_inline_dir(handle_t *handle, |
| 1104 | struct inode *inode, |
| 1105 | struct buffer_head *dir_block, |
| 1106 | void *buf, |
| 1107 | int inline_size) |
| 1108 | { |
| 1109 | int err, csum_size = 0, header_size = 0; |
| 1110 | struct ext4_dir_entry_2 *de; |
| 1111 | struct ext4_dir_entry_tail *t; |
| 1112 | void *target = dir_block->b_data; |
| 1113 | |
| 1114 | /* |
| 1115 | * First create "." and ".." and then copy the dir information |
| 1116 | * back to the block. |
| 1117 | */ |
| 1118 | de = (struct ext4_dir_entry_2 *)target; |
| 1119 | de = ext4_init_dot_dotdot(inode, de, |
| 1120 | inode->i_sb->s_blocksize, csum_size, |
| 1121 | le32_to_cpu(((struct ext4_dir_entry_2 *)buf)->inode), 1); |
| 1122 | header_size = (void *)de - target; |
| 1123 | |
| 1124 | memcpy((void *)de, buf + EXT4_INLINE_DOTDOT_SIZE, |
| 1125 | inline_size - EXT4_INLINE_DOTDOT_SIZE); |
| 1126 | |
| 1127 | if (EXT4_HAS_RO_COMPAT_FEATURE(inode->i_sb, |
| 1128 | EXT4_FEATURE_RO_COMPAT_METADATA_CSUM)) |
| 1129 | csum_size = sizeof(struct ext4_dir_entry_tail); |
| 1130 | |
| 1131 | inode->i_size = inode->i_sb->s_blocksize; |
| 1132 | i_size_write(inode, inode->i_sb->s_blocksize); |
| 1133 | EXT4_I(inode)->i_disksize = inode->i_sb->s_blocksize; |
| 1134 | ext4_update_final_de(dir_block->b_data, |
| 1135 | inline_size - EXT4_INLINE_DOTDOT_SIZE + header_size, |
| 1136 | inode->i_sb->s_blocksize - csum_size); |
| 1137 | |
| 1138 | if (csum_size) { |
| 1139 | t = EXT4_DIRENT_TAIL(dir_block->b_data, |
| 1140 | inode->i_sb->s_blocksize); |
| 1141 | initialize_dirent_tail(t, inode->i_sb->s_blocksize); |
| 1142 | } |
| 1143 | set_buffer_uptodate(dir_block); |
| 1144 | err = ext4_handle_dirty_dirent_node(handle, inode, dir_block); |
| 1145 | if (err) |
| 1146 | goto out; |
| 1147 | set_buffer_verified(dir_block); |
| 1148 | out: |
| 1149 | return err; |
| 1150 | } |
| 1151 | |
| 1152 | static int ext4_convert_inline_data_nolock(handle_t *handle, |
| 1153 | struct inode *inode, |
| 1154 | struct ext4_iloc *iloc) |
| 1155 | { |
| 1156 | int error; |
| 1157 | void *buf = NULL; |
| 1158 | struct buffer_head *data_bh = NULL; |
| 1159 | struct ext4_map_blocks map; |
| 1160 | int inline_size; |
| 1161 | |
| 1162 | inline_size = ext4_get_inline_size(inode); |
| 1163 | buf = kmalloc(inline_size, GFP_NOFS); |
| 1164 | if (!buf) { |
| 1165 | error = -ENOMEM; |
| 1166 | goto out; |
| 1167 | } |
| 1168 | |
| 1169 | error = ext4_read_inline_data(inode, buf, inline_size, iloc); |
| 1170 | if (error < 0) |
| 1171 | goto out; |
| 1172 | |
| 1173 | error = ext4_destroy_inline_data_nolock(handle, inode); |
| 1174 | if (error) |
| 1175 | goto out; |
| 1176 | |
| 1177 | map.m_lblk = 0; |
| 1178 | map.m_len = 1; |
| 1179 | map.m_flags = 0; |
| 1180 | error = ext4_map_blocks(handle, inode, &map, EXT4_GET_BLOCKS_CREATE); |
| 1181 | if (error < 0) |
| 1182 | goto out_restore; |
| 1183 | if (!(map.m_flags & EXT4_MAP_MAPPED)) { |
| 1184 | error = -EIO; |
| 1185 | goto out_restore; |
| 1186 | } |
| 1187 | |
| 1188 | data_bh = sb_getblk(inode->i_sb, map.m_pblk); |
| 1189 | if (!data_bh) { |
| 1190 | error = -EIO; |
| 1191 | goto out_restore; |
| 1192 | } |
| 1193 | |
| 1194 | lock_buffer(data_bh); |
| 1195 | error = ext4_journal_get_create_access(handle, data_bh); |
| 1196 | if (error) { |
| 1197 | unlock_buffer(data_bh); |
| 1198 | error = -EIO; |
| 1199 | goto out_restore; |
| 1200 | } |
| 1201 | memset(data_bh->b_data, 0, inode->i_sb->s_blocksize); |
| 1202 | |
| 1203 | if (!S_ISDIR(inode->i_mode)) { |
| 1204 | memcpy(data_bh->b_data, buf, inline_size); |
| 1205 | set_buffer_uptodate(data_bh); |
| 1206 | error = ext4_handle_dirty_metadata(handle, |
| 1207 | inode, data_bh); |
| 1208 | } else { |
| 1209 | error = ext4_finish_convert_inline_dir(handle, inode, data_bh, |
| 1210 | buf, inline_size); |
| 1211 | } |
| 1212 | |
| 1213 | unlock_buffer(data_bh); |
| 1214 | out_restore: |
| 1215 | if (error) |
| 1216 | ext4_restore_inline_data(handle, inode, iloc, buf, inline_size); |
| 1217 | |
| 1218 | out: |
| 1219 | brelse(data_bh); |
| 1220 | kfree(buf); |
| 1221 | return error; |
| 1222 | } |
| 1223 | |
| 1224 | /* |
| 1225 | * Try to add the new entry to the inline data. |
| 1226 | * If succeeds, return 0. If not, extended the inline dir and copied data to |
| 1227 | * the new created block. |
| 1228 | */ |
| 1229 | int ext4_try_add_inline_entry(handle_t *handle, struct dentry *dentry, |
| 1230 | struct inode *inode) |
| 1231 | { |
| 1232 | int ret, inline_size; |
| 1233 | void *inline_start; |
| 1234 | struct ext4_iloc iloc; |
| 1235 | struct inode *dir = dentry->d_parent->d_inode; |
| 1236 | |
| 1237 | ret = ext4_get_inode_loc(dir, &iloc); |
| 1238 | if (ret) |
| 1239 | return ret; |
| 1240 | |
| 1241 | down_write(&EXT4_I(dir)->xattr_sem); |
| 1242 | if (!ext4_has_inline_data(dir)) |
| 1243 | goto out; |
| 1244 | |
| 1245 | inline_start = (void *)ext4_raw_inode(&iloc)->i_block + |
| 1246 | EXT4_INLINE_DOTDOT_SIZE; |
| 1247 | inline_size = EXT4_MIN_INLINE_DATA_SIZE - EXT4_INLINE_DOTDOT_SIZE; |
| 1248 | |
| 1249 | ret = ext4_add_dirent_to_inline(handle, dentry, inode, &iloc, |
| 1250 | inline_start, inline_size); |
| 1251 | if (ret != -ENOSPC) |
| 1252 | goto out; |
| 1253 | |
| 1254 | /* check whether it can be inserted to inline xattr space. */ |
| 1255 | inline_size = EXT4_I(dir)->i_inline_size - |
| 1256 | EXT4_MIN_INLINE_DATA_SIZE; |
| 1257 | if (!inline_size) { |
| 1258 | /* Try to use the xattr space.*/ |
| 1259 | ret = ext4_update_inline_dir(handle, dir, &iloc); |
| 1260 | if (ret && ret != -ENOSPC) |
| 1261 | goto out; |
| 1262 | |
| 1263 | inline_size = EXT4_I(dir)->i_inline_size - |
| 1264 | EXT4_MIN_INLINE_DATA_SIZE; |
| 1265 | } |
| 1266 | |
| 1267 | if (inline_size) { |
| 1268 | inline_start = ext4_get_inline_xattr_pos(dir, &iloc); |
| 1269 | |
| 1270 | ret = ext4_add_dirent_to_inline(handle, dentry, inode, &iloc, |
| 1271 | inline_start, inline_size); |
| 1272 | |
| 1273 | if (ret != -ENOSPC) |
| 1274 | goto out; |
| 1275 | } |
| 1276 | |
| 1277 | /* |
| 1278 | * The inline space is filled up, so create a new block for it. |
| 1279 | * As the extent tree will be created, we have to save the inline |
| 1280 | * dir first. |
| 1281 | */ |
| 1282 | ret = ext4_convert_inline_data_nolock(handle, dir, &iloc); |
| 1283 | |
| 1284 | out: |
| 1285 | ext4_mark_inode_dirty(handle, dir); |
| 1286 | up_write(&EXT4_I(dir)->xattr_sem); |
| 1287 | brelse(iloc.bh); |
| 1288 | return ret; |
| 1289 | } |
| 1290 | |
| 1291 | /* |
| 1292 | * Try to create the inline data for the new dir. |
| 1293 | * If it succeeds, return 0, otherwise return the error. |
| 1294 | * In case of ENOSPC, the caller should create the normal disk layout dir. |
| 1295 | */ |
| 1296 | int ext4_try_create_inline_dir(handle_t *handle, struct inode *parent, |
| 1297 | struct inode *inode) |
| 1298 | { |
| 1299 | int ret, inline_size = EXT4_MIN_INLINE_DATA_SIZE; |
| 1300 | struct ext4_iloc iloc; |
| 1301 | struct ext4_dir_entry_2 *de; |
| 1302 | |
| 1303 | ret = ext4_get_inode_loc(inode, &iloc); |
| 1304 | if (ret) |
| 1305 | return ret; |
| 1306 | |
| 1307 | ret = ext4_prepare_inline_data(handle, inode, inline_size); |
| 1308 | if (ret) |
| 1309 | goto out; |
| 1310 | |
| 1311 | /* |
| 1312 | * For inline dir, we only save the inode information for the ".." |
| 1313 | * and create a fake dentry to cover the left space. |
| 1314 | */ |
| 1315 | de = (struct ext4_dir_entry_2 *)ext4_raw_inode(&iloc)->i_block; |
| 1316 | de->inode = cpu_to_le32(parent->i_ino); |
| 1317 | de = (struct ext4_dir_entry_2 *)((void *)de + EXT4_INLINE_DOTDOT_SIZE); |
| 1318 | de->inode = 0; |
| 1319 | de->rec_len = ext4_rec_len_to_disk( |
| 1320 | inline_size - EXT4_INLINE_DOTDOT_SIZE, |
| 1321 | inline_size); |
| 1322 | set_nlink(inode, 2); |
| 1323 | inode->i_size = EXT4_I(inode)->i_disksize = inline_size; |
| 1324 | out: |
| 1325 | brelse(iloc.bh); |
| 1326 | return ret; |
| 1327 | } |
| 1328 | |
Tao Ma | 67cf5b0 | 2012-12-10 14:04:46 -0500 | [diff] [blame] | 1329 | int ext4_destroy_inline_data(handle_t *handle, struct inode *inode) |
| 1330 | { |
| 1331 | int ret; |
| 1332 | |
| 1333 | down_write(&EXT4_I(inode)->xattr_sem); |
| 1334 | ret = ext4_destroy_inline_data_nolock(handle, inode); |
| 1335 | up_write(&EXT4_I(inode)->xattr_sem); |
| 1336 | |
| 1337 | return ret; |
| 1338 | } |