1 /* 2 * Copyright (C) 2016 Oracle. All Rights Reserved. 3 * 4 * Author: Darrick J. Wong <darrick.wong@oracle.com> 5 * 6 * This program is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU General Public License 8 * as published by the Free Software Foundation; either version 2 9 * of the License, or (at your option) any later version. 10 * 11 * This program is distributed in the hope that it would be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. 19 */ 20 #include "xfs.h" 21 #include "xfs_fs.h" 22 #include "xfs_format.h" 23 #include "xfs_log_format.h" 24 #include "xfs_trans_resv.h" 25 #include "xfs_bit.h" 26 #include "xfs_mount.h" 27 #include "xfs_defer.h" 28 #include "xfs_trans.h" 29 #include "xfs_trans_priv.h" 30 #include "xfs_buf_item.h" 31 #include "xfs_rmap_item.h" 32 #include "xfs_log.h" 33 #include "xfs_rmap.h" 34 35 36 kmem_zone_t *xfs_rui_zone; 37 kmem_zone_t *xfs_rud_zone; 38 39 static inline struct xfs_rui_log_item *RUI_ITEM(struct xfs_log_item *lip) 40 { 41 return container_of(lip, struct xfs_rui_log_item, rui_item); 42 } 43 44 void 45 xfs_rui_item_free( 46 struct xfs_rui_log_item *ruip) 47 { 48 if (ruip->rui_format.rui_nextents > XFS_RUI_MAX_FAST_EXTENTS) 49 kmem_free(ruip); 50 else 51 kmem_zone_free(xfs_rui_zone, ruip); 52 } 53 54 /* 55 * This returns the number of iovecs needed to log the given rui item. 56 * We only need 1 iovec for an rui item. It just logs the rui_log_format 57 * structure. 58 */ 59 static inline int 60 xfs_rui_item_sizeof( 61 struct xfs_rui_log_item *ruip) 62 { 63 return sizeof(struct xfs_rui_log_format) + 64 (ruip->rui_format.rui_nextents - 1) * 65 sizeof(struct xfs_map_extent); 66 } 67 68 STATIC void 69 xfs_rui_item_size( 70 struct xfs_log_item *lip, 71 int *nvecs, 72 int *nbytes) 73 { 74 *nvecs += 1; 75 *nbytes += xfs_rui_item_sizeof(RUI_ITEM(lip)); 76 } 77 78 /* 79 * This is called to fill in the vector of log iovecs for the 80 * given rui log item. We use only 1 iovec, and we point that 81 * at the rui_log_format structure embedded in the rui item. 82 * It is at this point that we assert that all of the extent 83 * slots in the rui item have been filled. 84 */ 85 STATIC void 86 xfs_rui_item_format( 87 struct xfs_log_item *lip, 88 struct xfs_log_vec *lv) 89 { 90 struct xfs_rui_log_item *ruip = RUI_ITEM(lip); 91 struct xfs_log_iovec *vecp = NULL; 92 93 ASSERT(atomic_read(&ruip->rui_next_extent) == 94 ruip->rui_format.rui_nextents); 95 96 ruip->rui_format.rui_type = XFS_LI_RUI; 97 ruip->rui_format.rui_size = 1; 98 99 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUI_FORMAT, &ruip->rui_format, 100 xfs_rui_item_sizeof(ruip)); 101 } 102 103 /* 104 * Pinning has no meaning for an rui item, so just return. 105 */ 106 STATIC void 107 xfs_rui_item_pin( 108 struct xfs_log_item *lip) 109 { 110 } 111 112 /* 113 * The unpin operation is the last place an RUI is manipulated in the log. It is 114 * either inserted in the AIL or aborted in the event of a log I/O error. In 115 * either case, the RUI transaction has been successfully committed to make it 116 * this far. Therefore, we expect whoever committed the RUI to either construct 117 * and commit the RUD or drop the RUD's reference in the event of error. Simply 118 * drop the log's RUI reference now that the log is done with it. 119 */ 120 STATIC void 121 xfs_rui_item_unpin( 122 struct xfs_log_item *lip, 123 int remove) 124 { 125 struct xfs_rui_log_item *ruip = RUI_ITEM(lip); 126 127 xfs_rui_release(ruip); 128 } 129 130 /* 131 * RUI items have no locking or pushing. However, since RUIs are pulled from 132 * the AIL when their corresponding RUDs are committed to disk, their situation 133 * is very similar to being pinned. Return XFS_ITEM_PINNED so that the caller 134 * will eventually flush the log. This should help in getting the RUI out of 135 * the AIL. 136 */ 137 STATIC uint 138 xfs_rui_item_push( 139 struct xfs_log_item *lip, 140 struct list_head *buffer_list) 141 { 142 return XFS_ITEM_PINNED; 143 } 144 145 /* 146 * The RUI has been either committed or aborted if the transaction has been 147 * cancelled. If the transaction was cancelled, an RUD isn't going to be 148 * constructed and thus we free the RUI here directly. 149 */ 150 STATIC void 151 xfs_rui_item_unlock( 152 struct xfs_log_item *lip) 153 { 154 if (lip->li_flags & XFS_LI_ABORTED) 155 xfs_rui_item_free(RUI_ITEM(lip)); 156 } 157 158 /* 159 * The RUI is logged only once and cannot be moved in the log, so simply return 160 * the lsn at which it's been logged. 161 */ 162 STATIC xfs_lsn_t 163 xfs_rui_item_committed( 164 struct xfs_log_item *lip, 165 xfs_lsn_t lsn) 166 { 167 return lsn; 168 } 169 170 /* 171 * The RUI dependency tracking op doesn't do squat. It can't because 172 * it doesn't know where the free extent is coming from. The dependency 173 * tracking has to be handled by the "enclosing" metadata object. For 174 * example, for inodes, the inode is locked throughout the extent freeing 175 * so the dependency should be recorded there. 176 */ 177 STATIC void 178 xfs_rui_item_committing( 179 struct xfs_log_item *lip, 180 xfs_lsn_t lsn) 181 { 182 } 183 184 /* 185 * This is the ops vector shared by all rui log items. 186 */ 187 static const struct xfs_item_ops xfs_rui_item_ops = { 188 .iop_size = xfs_rui_item_size, 189 .iop_format = xfs_rui_item_format, 190 .iop_pin = xfs_rui_item_pin, 191 .iop_unpin = xfs_rui_item_unpin, 192 .iop_unlock = xfs_rui_item_unlock, 193 .iop_committed = xfs_rui_item_committed, 194 .iop_push = xfs_rui_item_push, 195 .iop_committing = xfs_rui_item_committing, 196 }; 197 198 /* 199 * Allocate and initialize an rui item with the given number of extents. 200 */ 201 struct xfs_rui_log_item * 202 xfs_rui_init( 203 struct xfs_mount *mp, 204 uint nextents) 205 206 { 207 struct xfs_rui_log_item *ruip; 208 uint size; 209 210 ASSERT(nextents > 0); 211 if (nextents > XFS_RUI_MAX_FAST_EXTENTS) { 212 size = (uint)(sizeof(struct xfs_rui_log_item) + 213 ((nextents - 1) * sizeof(struct xfs_map_extent))); 214 ruip = kmem_zalloc(size, KM_SLEEP); 215 } else { 216 ruip = kmem_zone_zalloc(xfs_rui_zone, KM_SLEEP); 217 } 218 219 xfs_log_item_init(mp, &ruip->rui_item, XFS_LI_RUI, &xfs_rui_item_ops); 220 ruip->rui_format.rui_nextents = nextents; 221 ruip->rui_format.rui_id = (uintptr_t)(void *)ruip; 222 atomic_set(&ruip->rui_next_extent, 0); 223 atomic_set(&ruip->rui_refcount, 2); 224 225 return ruip; 226 } 227 228 /* 229 * Copy an RUI format buffer from the given buf, and into the destination 230 * RUI format structure. The RUI/RUD items were designed not to need any 231 * special alignment handling. 232 */ 233 int 234 xfs_rui_copy_format( 235 struct xfs_log_iovec *buf, 236 struct xfs_rui_log_format *dst_rui_fmt) 237 { 238 struct xfs_rui_log_format *src_rui_fmt; 239 uint len; 240 241 src_rui_fmt = buf->i_addr; 242 len = sizeof(struct xfs_rui_log_format) + 243 (src_rui_fmt->rui_nextents - 1) * 244 sizeof(struct xfs_map_extent); 245 246 if (buf->i_len != len) 247 return -EFSCORRUPTED; 248 249 memcpy((char *)dst_rui_fmt, (char *)src_rui_fmt, len); 250 return 0; 251 } 252 253 /* 254 * Freeing the RUI requires that we remove it from the AIL if it has already 255 * been placed there. However, the RUI may not yet have been placed in the AIL 256 * when called by xfs_rui_release() from RUD processing due to the ordering of 257 * committed vs unpin operations in bulk insert operations. Hence the reference 258 * count to ensure only the last caller frees the RUI. 259 */ 260 void 261 xfs_rui_release( 262 struct xfs_rui_log_item *ruip) 263 { 264 if (atomic_dec_and_test(&ruip->rui_refcount)) { 265 xfs_trans_ail_remove(&ruip->rui_item, SHUTDOWN_LOG_IO_ERROR); 266 xfs_rui_item_free(ruip); 267 } 268 } 269 270 static inline struct xfs_rud_log_item *RUD_ITEM(struct xfs_log_item *lip) 271 { 272 return container_of(lip, struct xfs_rud_log_item, rud_item); 273 } 274 275 STATIC void 276 xfs_rud_item_size( 277 struct xfs_log_item *lip, 278 int *nvecs, 279 int *nbytes) 280 { 281 *nvecs += 1; 282 *nbytes += sizeof(struct xfs_rud_log_format); 283 } 284 285 /* 286 * This is called to fill in the vector of log iovecs for the 287 * given rud log item. We use only 1 iovec, and we point that 288 * at the rud_log_format structure embedded in the rud item. 289 * It is at this point that we assert that all of the extent 290 * slots in the rud item have been filled. 291 */ 292 STATIC void 293 xfs_rud_item_format( 294 struct xfs_log_item *lip, 295 struct xfs_log_vec *lv) 296 { 297 struct xfs_rud_log_item *rudp = RUD_ITEM(lip); 298 struct xfs_log_iovec *vecp = NULL; 299 300 rudp->rud_format.rud_type = XFS_LI_RUD; 301 rudp->rud_format.rud_size = 1; 302 303 xlog_copy_iovec(lv, &vecp, XLOG_REG_TYPE_RUD_FORMAT, &rudp->rud_format, 304 sizeof(struct xfs_rud_log_format)); 305 } 306 307 /* 308 * Pinning has no meaning for an rud item, so just return. 309 */ 310 STATIC void 311 xfs_rud_item_pin( 312 struct xfs_log_item *lip) 313 { 314 } 315 316 /* 317 * Since pinning has no meaning for an rud item, unpinning does 318 * not either. 319 */ 320 STATIC void 321 xfs_rud_item_unpin( 322 struct xfs_log_item *lip, 323 int remove) 324 { 325 } 326 327 /* 328 * There isn't much you can do to push on an rud item. It is simply stuck 329 * waiting for the log to be flushed to disk. 330 */ 331 STATIC uint 332 xfs_rud_item_push( 333 struct xfs_log_item *lip, 334 struct list_head *buffer_list) 335 { 336 return XFS_ITEM_PINNED; 337 } 338 339 /* 340 * The RUD is either committed or aborted if the transaction is cancelled. If 341 * the transaction is cancelled, drop our reference to the RUI and free the 342 * RUD. 343 */ 344 STATIC void 345 xfs_rud_item_unlock( 346 struct xfs_log_item *lip) 347 { 348 struct xfs_rud_log_item *rudp = RUD_ITEM(lip); 349 350 if (lip->li_flags & XFS_LI_ABORTED) { 351 xfs_rui_release(rudp->rud_ruip); 352 kmem_zone_free(xfs_rud_zone, rudp); 353 } 354 } 355 356 /* 357 * When the rud item is committed to disk, all we need to do is delete our 358 * reference to our partner rui item and then free ourselves. Since we're 359 * freeing ourselves we must return -1 to keep the transaction code from 360 * further referencing this item. 361 */ 362 STATIC xfs_lsn_t 363 xfs_rud_item_committed( 364 struct xfs_log_item *lip, 365 xfs_lsn_t lsn) 366 { 367 struct xfs_rud_log_item *rudp = RUD_ITEM(lip); 368 369 /* 370 * Drop the RUI reference regardless of whether the RUD has been 371 * aborted. Once the RUD transaction is constructed, it is the sole 372 * responsibility of the RUD to release the RUI (even if the RUI is 373 * aborted due to log I/O error). 374 */ 375 xfs_rui_release(rudp->rud_ruip); 376 kmem_zone_free(xfs_rud_zone, rudp); 377 378 return (xfs_lsn_t)-1; 379 } 380 381 /* 382 * The RUD dependency tracking op doesn't do squat. It can't because 383 * it doesn't know where the free extent is coming from. The dependency 384 * tracking has to be handled by the "enclosing" metadata object. For 385 * example, for inodes, the inode is locked throughout the extent freeing 386 * so the dependency should be recorded there. 387 */ 388 STATIC void 389 xfs_rud_item_committing( 390 struct xfs_log_item *lip, 391 xfs_lsn_t lsn) 392 { 393 } 394 395 /* 396 * This is the ops vector shared by all rud log items. 397 */ 398 static const struct xfs_item_ops xfs_rud_item_ops = { 399 .iop_size = xfs_rud_item_size, 400 .iop_format = xfs_rud_item_format, 401 .iop_pin = xfs_rud_item_pin, 402 .iop_unpin = xfs_rud_item_unpin, 403 .iop_unlock = xfs_rud_item_unlock, 404 .iop_committed = xfs_rud_item_committed, 405 .iop_push = xfs_rud_item_push, 406 .iop_committing = xfs_rud_item_committing, 407 }; 408 409 /* 410 * Allocate and initialize an rud item with the given number of extents. 411 */ 412 struct xfs_rud_log_item * 413 xfs_rud_init( 414 struct xfs_mount *mp, 415 struct xfs_rui_log_item *ruip) 416 417 { 418 struct xfs_rud_log_item *rudp; 419 420 rudp = kmem_zone_zalloc(xfs_rud_zone, KM_SLEEP); 421 xfs_log_item_init(mp, &rudp->rud_item, XFS_LI_RUD, &xfs_rud_item_ops); 422 rudp->rud_ruip = ruip; 423 rudp->rud_format.rud_rui_id = ruip->rui_format.rui_id; 424 425 return rudp; 426 } 427 428 /* 429 * Process an rmap update intent item that was recovered from the log. 430 * We need to update the rmapbt. 431 */ 432 int 433 xfs_rui_recover( 434 struct xfs_mount *mp, 435 struct xfs_rui_log_item *ruip) 436 { 437 int i; 438 int error = 0; 439 struct xfs_map_extent *rmap; 440 xfs_fsblock_t startblock_fsb; 441 bool op_ok; 442 struct xfs_rud_log_item *rudp; 443 enum xfs_rmap_intent_type type; 444 int whichfork; 445 xfs_exntst_t state; 446 struct xfs_trans *tp; 447 struct xfs_btree_cur *rcur = NULL; 448 449 ASSERT(!test_bit(XFS_RUI_RECOVERED, &ruip->rui_flags)); 450 451 /* 452 * First check the validity of the extents described by the 453 * RUI. If any are bad, then assume that all are bad and 454 * just toss the RUI. 455 */ 456 for (i = 0; i < ruip->rui_format.rui_nextents; i++) { 457 rmap = &ruip->rui_format.rui_extents[i]; 458 startblock_fsb = XFS_BB_TO_FSB(mp, 459 XFS_FSB_TO_DADDR(mp, rmap->me_startblock)); 460 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) { 461 case XFS_RMAP_EXTENT_MAP: 462 case XFS_RMAP_EXTENT_UNMAP: 463 case XFS_RMAP_EXTENT_CONVERT: 464 case XFS_RMAP_EXTENT_ALLOC: 465 case XFS_RMAP_EXTENT_FREE: 466 op_ok = true; 467 break; 468 default: 469 op_ok = false; 470 break; 471 } 472 if (!op_ok || startblock_fsb == 0 || 473 rmap->me_len == 0 || 474 startblock_fsb >= mp->m_sb.sb_dblocks || 475 rmap->me_len >= mp->m_sb.sb_agblocks || 476 (rmap->me_flags & ~XFS_RMAP_EXTENT_FLAGS)) { 477 /* 478 * This will pull the RUI from the AIL and 479 * free the memory associated with it. 480 */ 481 set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags); 482 xfs_rui_release(ruip); 483 return -EIO; 484 } 485 } 486 487 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_itruncate, 0, 0, 0, &tp); 488 if (error) 489 return error; 490 rudp = xfs_trans_get_rud(tp, ruip); 491 492 for (i = 0; i < ruip->rui_format.rui_nextents; i++) { 493 rmap = &ruip->rui_format.rui_extents[i]; 494 state = (rmap->me_flags & XFS_RMAP_EXTENT_UNWRITTEN) ? 495 XFS_EXT_UNWRITTEN : XFS_EXT_NORM; 496 whichfork = (rmap->me_flags & XFS_RMAP_EXTENT_ATTR_FORK) ? 497 XFS_ATTR_FORK : XFS_DATA_FORK; 498 switch (rmap->me_flags & XFS_RMAP_EXTENT_TYPE_MASK) { 499 case XFS_RMAP_EXTENT_MAP: 500 type = XFS_RMAP_MAP; 501 break; 502 case XFS_RMAP_EXTENT_UNMAP: 503 type = XFS_RMAP_UNMAP; 504 break; 505 case XFS_RMAP_EXTENT_CONVERT: 506 type = XFS_RMAP_CONVERT; 507 break; 508 case XFS_RMAP_EXTENT_ALLOC: 509 type = XFS_RMAP_ALLOC; 510 break; 511 case XFS_RMAP_EXTENT_FREE: 512 type = XFS_RMAP_FREE; 513 break; 514 default: 515 error = -EFSCORRUPTED; 516 goto abort_error; 517 } 518 error = xfs_trans_log_finish_rmap_update(tp, rudp, type, 519 rmap->me_owner, whichfork, 520 rmap->me_startoff, rmap->me_startblock, 521 rmap->me_len, state, &rcur); 522 if (error) 523 goto abort_error; 524 525 } 526 527 xfs_rmap_finish_one_cleanup(tp, rcur, error); 528 set_bit(XFS_RUI_RECOVERED, &ruip->rui_flags); 529 error = xfs_trans_commit(tp); 530 return error; 531 532 abort_error: 533 xfs_rmap_finish_one_cleanup(tp, rcur, error); 534 xfs_trans_cancel(tp); 535 return error; 536 } 537