1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * inode.c 4 * 5 * vfs' aops, fops, dops and iops 6 * 7 * Copyright (C) 2002, 2004 Oracle. All rights reserved. 8 */ 9 10 #include <linux/fs.h> 11 #include <linux/types.h> 12 #include <linux/highmem.h> 13 #include <linux/pagemap.h> 14 #include <linux/quotaops.h> 15 #include <linux/iversion.h> 16 17 #include <asm/byteorder.h> 18 19 #include <cluster/masklog.h> 20 21 #include "ocfs2.h" 22 23 #include "alloc.h" 24 #include "dir.h" 25 #include "blockcheck.h" 26 #include "dlmglue.h" 27 #include "extent_map.h" 28 #include "file.h" 29 #include "heartbeat.h" 30 #include "inode.h" 31 #include "journal.h" 32 #include "namei.h" 33 #include "suballoc.h" 34 #include "super.h" 35 #include "symlink.h" 36 #include "sysfile.h" 37 #include "uptodate.h" 38 #include "xattr.h" 39 #include "refcounttree.h" 40 #include "ocfs2_trace.h" 41 #include "filecheck.h" 42 43 #include "buffer_head_io.h" 44 45 struct ocfs2_find_inode_args 46 { 47 u64 fi_blkno; 48 unsigned long fi_ino; 49 unsigned int fi_flags; 50 unsigned int fi_sysfile_type; 51 }; 52 53 static struct lock_class_key ocfs2_sysfile_lock_key[NUM_SYSTEM_INODES]; 54 55 static int ocfs2_read_locked_inode(struct inode *inode, 56 struct ocfs2_find_inode_args *args); 57 static int ocfs2_init_locked_inode(struct inode *inode, void *opaque); 58 static int ocfs2_find_actor(struct inode *inode, void *opaque); 59 static int ocfs2_truncate_for_delete(struct ocfs2_super *osb, 60 struct inode *inode, 61 struct buffer_head *fe_bh); 62 63 static int ocfs2_filecheck_read_inode_block_full(struct inode *inode, 64 struct buffer_head **bh, 65 int flags, int type); 66 static int ocfs2_filecheck_validate_inode_block(struct super_block *sb, 67 struct buffer_head *bh); 68 static int ocfs2_filecheck_repair_inode_block(struct super_block *sb, 69 struct buffer_head *bh); 70 71 void ocfs2_set_inode_flags(struct inode *inode) 72 { 73 unsigned int flags = OCFS2_I(inode)->ip_attr; 74 75 inode->i_flags &= ~(S_IMMUTABLE | 76 S_SYNC | S_APPEND | S_NOATIME | S_DIRSYNC); 77 78 if (flags & OCFS2_IMMUTABLE_FL) 79 inode->i_flags |= S_IMMUTABLE; 80 81 if (flags & OCFS2_SYNC_FL) 82 inode->i_flags |= S_SYNC; 83 if (flags & OCFS2_APPEND_FL) 84 inode->i_flags |= S_APPEND; 85 if (flags & OCFS2_NOATIME_FL) 86 inode->i_flags |= S_NOATIME; 87 if (flags & OCFS2_DIRSYNC_FL) 88 inode->i_flags |= S_DIRSYNC; 89 } 90 91 /* Propagate flags from i_flags to OCFS2_I(inode)->ip_attr */ 92 void ocfs2_get_inode_flags(struct ocfs2_inode_info *oi) 93 { 94 unsigned int flags = oi->vfs_inode.i_flags; 95 96 oi->ip_attr &= ~(OCFS2_SYNC_FL|OCFS2_APPEND_FL| 97 OCFS2_IMMUTABLE_FL|OCFS2_NOATIME_FL|OCFS2_DIRSYNC_FL); 98 if (flags & S_SYNC) 99 oi->ip_attr |= OCFS2_SYNC_FL; 100 if (flags & S_APPEND) 101 oi->ip_attr |= OCFS2_APPEND_FL; 102 if (flags & S_IMMUTABLE) 103 oi->ip_attr |= OCFS2_IMMUTABLE_FL; 104 if (flags & S_NOATIME) 105 oi->ip_attr |= OCFS2_NOATIME_FL; 106 if (flags & S_DIRSYNC) 107 oi->ip_attr |= OCFS2_DIRSYNC_FL; 108 } 109 110 struct inode *ocfs2_ilookup(struct super_block *sb, u64 blkno) 111 { 112 struct ocfs2_find_inode_args args; 113 114 args.fi_blkno = blkno; 115 args.fi_flags = 0; 116 args.fi_ino = ino_from_blkno(sb, blkno); 117 args.fi_sysfile_type = 0; 118 119 return ilookup5(sb, blkno, ocfs2_find_actor, &args); 120 } 121 struct inode *ocfs2_iget(struct ocfs2_super *osb, u64 blkno, unsigned flags, 122 int sysfile_type) 123 { 124 int rc = -ESTALE; 125 struct inode *inode = NULL; 126 struct super_block *sb = osb->sb; 127 struct ocfs2_find_inode_args args; 128 journal_t *journal = osb->journal->j_journal; 129 130 trace_ocfs2_iget_begin((unsigned long long)blkno, flags, 131 sysfile_type); 132 133 /* Ok. By now we've either got the offsets passed to us by the 134 * caller, or we just pulled them off the bh. Lets do some 135 * sanity checks to make sure they're OK. */ 136 if (blkno == 0) { 137 inode = ERR_PTR(-EINVAL); 138 mlog_errno(PTR_ERR(inode)); 139 goto bail; 140 } 141 142 args.fi_blkno = blkno; 143 args.fi_flags = flags; 144 args.fi_ino = ino_from_blkno(sb, blkno); 145 args.fi_sysfile_type = sysfile_type; 146 147 inode = iget5_locked(sb, args.fi_ino, ocfs2_find_actor, 148 ocfs2_init_locked_inode, &args); 149 /* inode was *not* in the inode cache. 2.6.x requires 150 * us to do our own read_inode call and unlock it 151 * afterwards. */ 152 if (inode == NULL) { 153 inode = ERR_PTR(-ENOMEM); 154 mlog_errno(PTR_ERR(inode)); 155 goto bail; 156 } 157 trace_ocfs2_iget5_locked(inode->i_state); 158 if (inode->i_state & I_NEW) { 159 rc = ocfs2_read_locked_inode(inode, &args); 160 unlock_new_inode(inode); 161 } 162 if (is_bad_inode(inode)) { 163 iput(inode); 164 inode = ERR_PTR(rc); 165 goto bail; 166 } 167 168 /* 169 * Set transaction id's of transactions that have to be committed 170 * to finish f[data]sync. We set them to currently running transaction 171 * as we cannot be sure that the inode or some of its metadata isn't 172 * part of the transaction - the inode could have been reclaimed and 173 * now it is reread from disk. 174 */ 175 if (journal) { 176 transaction_t *transaction; 177 tid_t tid; 178 struct ocfs2_inode_info *oi = OCFS2_I(inode); 179 180 read_lock(&journal->j_state_lock); 181 if (journal->j_running_transaction) 182 transaction = journal->j_running_transaction; 183 else 184 transaction = journal->j_committing_transaction; 185 if (transaction) 186 tid = transaction->t_tid; 187 else 188 tid = journal->j_commit_sequence; 189 read_unlock(&journal->j_state_lock); 190 oi->i_sync_tid = tid; 191 oi->i_datasync_tid = tid; 192 } 193 194 bail: 195 if (!IS_ERR(inode)) { 196 trace_ocfs2_iget_end(inode, 197 (unsigned long long)OCFS2_I(inode)->ip_blkno); 198 } 199 200 return inode; 201 } 202 203 static int ocfs2_dinode_has_extents(struct ocfs2_dinode *di) 204 { 205 /* inodes flagged with other stuff in id2 */ 206 if (di->i_flags & (OCFS2_SUPER_BLOCK_FL | OCFS2_LOCAL_ALLOC_FL | 207 OCFS2_CHAIN_FL | OCFS2_DEALLOC_FL)) 208 return 0; 209 /* i_flags doesn't indicate when id2 is a fast symlink */ 210 if (S_ISLNK(di->i_mode) && di->i_size && di->i_clusters == 0) 211 return 0; 212 if (di->i_dyn_features & OCFS2_INLINE_DATA_FL) 213 return 0; 214 215 return 1; 216 } 217 218 /* 219 * here's how inodes get read from disk: 220 * iget5_locked -> find_actor -> OCFS2_FIND_ACTOR 221 * found? : return the in-memory inode 222 * not found? : get_new_inode -> OCFS2_INIT_LOCKED_INODE 223 */ 224 225 static int ocfs2_find_actor(struct inode *inode, void *opaque) 226 { 227 struct ocfs2_find_inode_args *args = NULL; 228 struct ocfs2_inode_info *oi = OCFS2_I(inode); 229 int ret = 0; 230 231 args = opaque; 232 233 mlog_bug_on_msg(!inode, "No inode in find actor!\n"); 234 235 trace_ocfs2_find_actor(inode, inode->i_ino, opaque, args->fi_blkno); 236 237 if (oi->ip_blkno != args->fi_blkno) 238 goto bail; 239 240 ret = 1; 241 bail: 242 return ret; 243 } 244 245 /* 246 * initialize the new inode, but don't do anything that would cause 247 * us to sleep. 248 * return 0 on success, 1 on failure 249 */ 250 static int ocfs2_init_locked_inode(struct inode *inode, void *opaque) 251 { 252 struct ocfs2_find_inode_args *args = opaque; 253 static struct lock_class_key ocfs2_quota_ip_alloc_sem_key, 254 ocfs2_file_ip_alloc_sem_key; 255 256 inode->i_ino = args->fi_ino; 257 OCFS2_I(inode)->ip_blkno = args->fi_blkno; 258 if (args->fi_sysfile_type != 0) 259 lockdep_set_class(&inode->i_rwsem, 260 &ocfs2_sysfile_lock_key[args->fi_sysfile_type]); 261 if (args->fi_sysfile_type == USER_QUOTA_SYSTEM_INODE || 262 args->fi_sysfile_type == GROUP_QUOTA_SYSTEM_INODE || 263 args->fi_sysfile_type == LOCAL_USER_QUOTA_SYSTEM_INODE || 264 args->fi_sysfile_type == LOCAL_GROUP_QUOTA_SYSTEM_INODE) 265 lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem, 266 &ocfs2_quota_ip_alloc_sem_key); 267 else 268 lockdep_set_class(&OCFS2_I(inode)->ip_alloc_sem, 269 &ocfs2_file_ip_alloc_sem_key); 270 271 return 0; 272 } 273 274 void ocfs2_populate_inode(struct inode *inode, struct ocfs2_dinode *fe, 275 int create_ino) 276 { 277 struct super_block *sb; 278 struct ocfs2_super *osb; 279 int use_plocks = 1; 280 281 sb = inode->i_sb; 282 osb = OCFS2_SB(sb); 283 284 if ((osb->s_mount_opt & OCFS2_MOUNT_LOCALFLOCKS) || 285 ocfs2_mount_local(osb) || !ocfs2_stack_supports_plocks()) 286 use_plocks = 0; 287 288 /* 289 * These have all been checked by ocfs2_read_inode_block() or set 290 * by ocfs2_mknod_locked(), so a failure is a code bug. 291 */ 292 BUG_ON(!OCFS2_IS_VALID_DINODE(fe)); /* This means that read_inode 293 cannot create a superblock 294 inode today. change if 295 that is needed. */ 296 BUG_ON(!(fe->i_flags & cpu_to_le32(OCFS2_VALID_FL))); 297 BUG_ON(le32_to_cpu(fe->i_fs_generation) != osb->fs_generation); 298 299 300 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters); 301 OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr); 302 OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features); 303 304 inode_set_iversion(inode, 1); 305 inode->i_generation = le32_to_cpu(fe->i_generation); 306 inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev)); 307 inode->i_mode = le16_to_cpu(fe->i_mode); 308 i_uid_write(inode, le32_to_cpu(fe->i_uid)); 309 i_gid_write(inode, le32_to_cpu(fe->i_gid)); 310 311 /* Fast symlinks will have i_size but no allocated clusters. */ 312 if (S_ISLNK(inode->i_mode) && !fe->i_clusters) { 313 inode->i_blocks = 0; 314 inode->i_mapping->a_ops = &ocfs2_fast_symlink_aops; 315 } else { 316 inode->i_blocks = ocfs2_inode_sector_count(inode); 317 inode->i_mapping->a_ops = &ocfs2_aops; 318 } 319 inode_set_atime(inode, le64_to_cpu(fe->i_atime), 320 le32_to_cpu(fe->i_atime_nsec)); 321 inode_set_mtime(inode, le64_to_cpu(fe->i_mtime), 322 le32_to_cpu(fe->i_mtime_nsec)); 323 inode_set_ctime(inode, le64_to_cpu(fe->i_ctime), 324 le32_to_cpu(fe->i_ctime_nsec)); 325 326 if (OCFS2_I(inode)->ip_blkno != le64_to_cpu(fe->i_blkno)) 327 mlog(ML_ERROR, 328 "ip_blkno %llu != i_blkno %llu!\n", 329 (unsigned long long)OCFS2_I(inode)->ip_blkno, 330 (unsigned long long)le64_to_cpu(fe->i_blkno)); 331 332 set_nlink(inode, ocfs2_read_links_count(fe)); 333 334 trace_ocfs2_populate_inode(OCFS2_I(inode)->ip_blkno, 335 le32_to_cpu(fe->i_flags)); 336 if (fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) { 337 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_SYSTEM_FILE; 338 inode->i_flags |= S_NOQUOTA; 339 } 340 341 if (fe->i_flags & cpu_to_le32(OCFS2_LOCAL_ALLOC_FL)) { 342 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP; 343 } else if (fe->i_flags & cpu_to_le32(OCFS2_BITMAP_FL)) { 344 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_BITMAP; 345 } else if (fe->i_flags & cpu_to_le32(OCFS2_QUOTA_FL)) { 346 inode->i_flags |= S_NOQUOTA; 347 } else if (fe->i_flags & cpu_to_le32(OCFS2_SUPER_BLOCK_FL)) { 348 /* we can't actually hit this as read_inode can't 349 * handle superblocks today ;-) */ 350 BUG(); 351 } 352 353 switch (inode->i_mode & S_IFMT) { 354 case S_IFREG: 355 if (use_plocks) 356 inode->i_fop = &ocfs2_fops; 357 else 358 inode->i_fop = &ocfs2_fops_no_plocks; 359 inode->i_op = &ocfs2_file_iops; 360 i_size_write(inode, le64_to_cpu(fe->i_size)); 361 break; 362 case S_IFDIR: 363 inode->i_op = &ocfs2_dir_iops; 364 if (use_plocks) 365 inode->i_fop = &ocfs2_dops; 366 else 367 inode->i_fop = &ocfs2_dops_no_plocks; 368 i_size_write(inode, le64_to_cpu(fe->i_size)); 369 OCFS2_I(inode)->ip_dir_lock_gen = 1; 370 break; 371 case S_IFLNK: 372 inode->i_op = &ocfs2_symlink_inode_operations; 373 inode_nohighmem(inode); 374 i_size_write(inode, le64_to_cpu(fe->i_size)); 375 break; 376 default: 377 inode->i_op = &ocfs2_special_file_iops; 378 init_special_inode(inode, inode->i_mode, 379 inode->i_rdev); 380 break; 381 } 382 383 if (create_ino) { 384 inode->i_ino = ino_from_blkno(inode->i_sb, 385 le64_to_cpu(fe->i_blkno)); 386 387 /* 388 * If we ever want to create system files from kernel, 389 * the generation argument to 390 * ocfs2_inode_lock_res_init() will have to change. 391 */ 392 BUG_ON(le32_to_cpu(fe->i_flags) & OCFS2_SYSTEM_FL); 393 394 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres, 395 OCFS2_LOCK_TYPE_META, 0, inode); 396 397 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres, 398 OCFS2_LOCK_TYPE_OPEN, 0, inode); 399 } 400 401 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_rw_lockres, 402 OCFS2_LOCK_TYPE_RW, inode->i_generation, 403 inode); 404 405 ocfs2_set_inode_flags(inode); 406 407 OCFS2_I(inode)->ip_last_used_slot = 0; 408 OCFS2_I(inode)->ip_last_used_group = 0; 409 410 if (S_ISDIR(inode->i_mode)) 411 ocfs2_resv_set_type(&OCFS2_I(inode)->ip_la_data_resv, 412 OCFS2_RESV_FLAG_DIR); 413 } 414 415 static int ocfs2_read_locked_inode(struct inode *inode, 416 struct ocfs2_find_inode_args *args) 417 { 418 struct super_block *sb; 419 struct ocfs2_super *osb; 420 struct ocfs2_dinode *fe; 421 struct buffer_head *bh = NULL; 422 int status, can_lock, lock_level = 0; 423 u32 generation = 0; 424 425 status = -EINVAL; 426 sb = inode->i_sb; 427 osb = OCFS2_SB(sb); 428 429 /* 430 * To improve performance of cold-cache inode stats, we take 431 * the cluster lock here if possible. 432 * 433 * Generally, OCFS2 never trusts the contents of an inode 434 * unless it's holding a cluster lock, so taking it here isn't 435 * a correctness issue as much as it is a performance 436 * improvement. 437 * 438 * There are three times when taking the lock is not a good idea: 439 * 440 * 1) During startup, before we have initialized the DLM. 441 * 442 * 2) If we are reading certain system files which never get 443 * cluster locks (local alloc, truncate log). 444 * 445 * 3) If the process doing the iget() is responsible for 446 * orphan dir recovery. We're holding the orphan dir lock and 447 * can get into a deadlock with another process on another 448 * node in ->delete_inode(). 449 * 450 * #1 and #2 can be simply solved by never taking the lock 451 * here for system files (which are the only type we read 452 * during mount). It's a heavier approach, but our main 453 * concern is user-accessible files anyway. 454 * 455 * #3 works itself out because we'll eventually take the 456 * cluster lock before trusting anything anyway. 457 */ 458 can_lock = !(args->fi_flags & OCFS2_FI_FLAG_SYSFILE) 459 && !(args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) 460 && !ocfs2_mount_local(osb); 461 462 trace_ocfs2_read_locked_inode( 463 (unsigned long long)OCFS2_I(inode)->ip_blkno, can_lock); 464 465 /* 466 * To maintain backwards compatibility with older versions of 467 * ocfs2-tools, we still store the generation value for system 468 * files. The only ones that actually matter to userspace are 469 * the journals, but it's easier and inexpensive to just flag 470 * all system files similarly. 471 */ 472 if (args->fi_flags & OCFS2_FI_FLAG_SYSFILE) 473 generation = osb->fs_generation; 474 475 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_inode_lockres, 476 OCFS2_LOCK_TYPE_META, 477 generation, inode); 478 479 ocfs2_inode_lock_res_init(&OCFS2_I(inode)->ip_open_lockres, 480 OCFS2_LOCK_TYPE_OPEN, 481 0, inode); 482 483 if (can_lock) { 484 status = ocfs2_open_lock(inode); 485 if (status) { 486 make_bad_inode(inode); 487 mlog_errno(status); 488 return status; 489 } 490 status = ocfs2_inode_lock(inode, NULL, lock_level); 491 if (status) { 492 make_bad_inode(inode); 493 mlog_errno(status); 494 return status; 495 } 496 } 497 498 if (args->fi_flags & OCFS2_FI_FLAG_ORPHAN_RECOVERY) { 499 status = ocfs2_try_open_lock(inode, 0); 500 if (status) { 501 make_bad_inode(inode); 502 return status; 503 } 504 } 505 506 if (can_lock) { 507 if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_CHK) 508 status = ocfs2_filecheck_read_inode_block_full(inode, 509 &bh, OCFS2_BH_IGNORE_CACHE, 0); 510 else if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_FIX) 511 status = ocfs2_filecheck_read_inode_block_full(inode, 512 &bh, OCFS2_BH_IGNORE_CACHE, 1); 513 else 514 status = ocfs2_read_inode_block_full(inode, 515 &bh, OCFS2_BH_IGNORE_CACHE); 516 } else { 517 status = ocfs2_read_blocks_sync(osb, args->fi_blkno, 1, &bh); 518 /* 519 * If buffer is in jbd, then its checksum may not have been 520 * computed as yet. 521 */ 522 if (!status && !buffer_jbd(bh)) { 523 if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_CHK) 524 status = ocfs2_filecheck_validate_inode_block( 525 osb->sb, bh); 526 else if (args->fi_flags & OCFS2_FI_FLAG_FILECHECK_FIX) 527 status = ocfs2_filecheck_repair_inode_block( 528 osb->sb, bh); 529 else 530 status = ocfs2_validate_inode_block( 531 osb->sb, bh); 532 } 533 } 534 if (status < 0) { 535 mlog_errno(status); 536 goto bail; 537 } 538 539 status = -EINVAL; 540 fe = (struct ocfs2_dinode *) bh->b_data; 541 542 /* 543 * This is a code bug. Right now the caller needs to 544 * understand whether it is asking for a system file inode or 545 * not so the proper lock names can be built. 546 */ 547 mlog_bug_on_msg(!!(fe->i_flags & cpu_to_le32(OCFS2_SYSTEM_FL)) != 548 !!(args->fi_flags & OCFS2_FI_FLAG_SYSFILE), 549 "Inode %llu: system file state is ambiguous\n", 550 (unsigned long long)args->fi_blkno); 551 552 if (S_ISCHR(le16_to_cpu(fe->i_mode)) || 553 S_ISBLK(le16_to_cpu(fe->i_mode))) 554 inode->i_rdev = huge_decode_dev(le64_to_cpu(fe->id1.dev1.i_rdev)); 555 556 ocfs2_populate_inode(inode, fe, 0); 557 558 BUG_ON(args->fi_blkno != le64_to_cpu(fe->i_blkno)); 559 560 if (buffer_dirty(bh) && !buffer_jbd(bh)) { 561 if (can_lock) { 562 ocfs2_inode_unlock(inode, lock_level); 563 lock_level = 1; 564 ocfs2_inode_lock(inode, NULL, lock_level); 565 } 566 status = ocfs2_write_block(osb, bh, INODE_CACHE(inode)); 567 if (status < 0) { 568 mlog_errno(status); 569 goto bail; 570 } 571 } 572 573 status = 0; 574 575 bail: 576 if (can_lock) 577 ocfs2_inode_unlock(inode, lock_level); 578 579 if (status < 0) 580 make_bad_inode(inode); 581 582 brelse(bh); 583 584 return status; 585 } 586 587 void ocfs2_sync_blockdev(struct super_block *sb) 588 { 589 sync_blockdev(sb->s_bdev); 590 } 591 592 static int ocfs2_truncate_for_delete(struct ocfs2_super *osb, 593 struct inode *inode, 594 struct buffer_head *fe_bh) 595 { 596 int status = 0; 597 struct ocfs2_dinode *fe; 598 handle_t *handle = NULL; 599 600 fe = (struct ocfs2_dinode *) fe_bh->b_data; 601 602 /* 603 * This check will also skip truncate of inodes with inline 604 * data and fast symlinks. 605 */ 606 if (fe->i_clusters) { 607 if (ocfs2_should_order_data(inode)) 608 ocfs2_begin_ordered_truncate(inode, 0); 609 610 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS); 611 if (IS_ERR(handle)) { 612 status = PTR_ERR(handle); 613 handle = NULL; 614 mlog_errno(status); 615 goto out; 616 } 617 618 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), 619 fe_bh, 620 OCFS2_JOURNAL_ACCESS_WRITE); 621 if (status < 0) { 622 mlog_errno(status); 623 goto out; 624 } 625 626 i_size_write(inode, 0); 627 628 status = ocfs2_mark_inode_dirty(handle, inode, fe_bh); 629 if (status < 0) { 630 mlog_errno(status); 631 goto out; 632 } 633 634 ocfs2_commit_trans(osb, handle); 635 handle = NULL; 636 637 status = ocfs2_commit_truncate(osb, inode, fe_bh); 638 if (status < 0) 639 mlog_errno(status); 640 } 641 642 out: 643 if (handle) 644 ocfs2_commit_trans(osb, handle); 645 return status; 646 } 647 648 static int ocfs2_remove_inode(struct inode *inode, 649 struct buffer_head *di_bh, 650 struct inode *orphan_dir_inode, 651 struct buffer_head *orphan_dir_bh) 652 { 653 int status; 654 struct inode *inode_alloc_inode = NULL; 655 struct buffer_head *inode_alloc_bh = NULL; 656 handle_t *handle; 657 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 658 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data; 659 660 inode_alloc_inode = 661 ocfs2_get_system_file_inode(osb, INODE_ALLOC_SYSTEM_INODE, 662 le16_to_cpu(di->i_suballoc_slot)); 663 if (!inode_alloc_inode) { 664 status = -ENOENT; 665 mlog_errno(status); 666 goto bail; 667 } 668 669 inode_lock(inode_alloc_inode); 670 status = ocfs2_inode_lock(inode_alloc_inode, &inode_alloc_bh, 1); 671 if (status < 0) { 672 inode_unlock(inode_alloc_inode); 673 674 mlog_errno(status); 675 goto bail; 676 } 677 678 handle = ocfs2_start_trans(osb, OCFS2_DELETE_INODE_CREDITS + 679 ocfs2_quota_trans_credits(inode->i_sb)); 680 if (IS_ERR(handle)) { 681 status = PTR_ERR(handle); 682 mlog_errno(status); 683 goto bail_unlock; 684 } 685 686 if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) { 687 status = ocfs2_orphan_del(osb, handle, orphan_dir_inode, inode, 688 orphan_dir_bh, false); 689 if (status < 0) { 690 mlog_errno(status); 691 goto bail_commit; 692 } 693 } 694 695 /* set the inodes dtime */ 696 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh, 697 OCFS2_JOURNAL_ACCESS_WRITE); 698 if (status < 0) { 699 mlog_errno(status); 700 goto bail_commit; 701 } 702 703 di->i_dtime = cpu_to_le64(ktime_get_real_seconds()); 704 di->i_flags &= cpu_to_le32(~(OCFS2_VALID_FL | OCFS2_ORPHANED_FL)); 705 ocfs2_journal_dirty(handle, di_bh); 706 707 ocfs2_remove_from_cache(INODE_CACHE(inode), di_bh); 708 dquot_free_inode(inode); 709 710 status = ocfs2_free_dinode(handle, inode_alloc_inode, 711 inode_alloc_bh, di); 712 if (status < 0) 713 mlog_errno(status); 714 715 bail_commit: 716 ocfs2_commit_trans(osb, handle); 717 bail_unlock: 718 ocfs2_inode_unlock(inode_alloc_inode, 1); 719 inode_unlock(inode_alloc_inode); 720 brelse(inode_alloc_bh); 721 bail: 722 iput(inode_alloc_inode); 723 724 return status; 725 } 726 727 /* 728 * Serialize with orphan dir recovery. If the process doing 729 * recovery on this orphan dir does an iget() with the dir 730 * i_rwsem held, we'll deadlock here. Instead we detect this 731 * and exit early - recovery will wipe this inode for us. 732 */ 733 static int ocfs2_check_orphan_recovery_state(struct ocfs2_super *osb, 734 int slot) 735 { 736 int ret = 0; 737 738 spin_lock(&osb->osb_lock); 739 if (ocfs2_node_map_test_bit(osb, &osb->osb_recovering_orphan_dirs, slot)) { 740 ret = -EDEADLK; 741 goto out; 742 } 743 /* This signals to the orphan recovery process that it should 744 * wait for us to handle the wipe. */ 745 osb->osb_orphan_wipes[slot]++; 746 out: 747 spin_unlock(&osb->osb_lock); 748 trace_ocfs2_check_orphan_recovery_state(slot, ret); 749 return ret; 750 } 751 752 static void ocfs2_signal_wipe_completion(struct ocfs2_super *osb, 753 int slot) 754 { 755 spin_lock(&osb->osb_lock); 756 osb->osb_orphan_wipes[slot]--; 757 spin_unlock(&osb->osb_lock); 758 759 wake_up(&osb->osb_wipe_event); 760 } 761 762 static int ocfs2_wipe_inode(struct inode *inode, 763 struct buffer_head *di_bh) 764 { 765 int status, orphaned_slot = -1; 766 struct inode *orphan_dir_inode = NULL; 767 struct buffer_head *orphan_dir_bh = NULL; 768 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 769 struct ocfs2_dinode *di = (struct ocfs2_dinode *) di_bh->b_data; 770 771 if (!(OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) { 772 orphaned_slot = le16_to_cpu(di->i_orphaned_slot); 773 774 status = ocfs2_check_orphan_recovery_state(osb, orphaned_slot); 775 if (status) 776 return status; 777 778 orphan_dir_inode = ocfs2_get_system_file_inode(osb, 779 ORPHAN_DIR_SYSTEM_INODE, 780 orphaned_slot); 781 if (!orphan_dir_inode) { 782 status = -ENOENT; 783 mlog_errno(status); 784 goto bail; 785 } 786 787 /* Lock the orphan dir. The lock will be held for the entire 788 * delete_inode operation. We do this now to avoid races with 789 * recovery completion on other nodes. */ 790 inode_lock(orphan_dir_inode); 791 status = ocfs2_inode_lock(orphan_dir_inode, &orphan_dir_bh, 1); 792 if (status < 0) { 793 inode_unlock(orphan_dir_inode); 794 795 mlog_errno(status); 796 goto bail; 797 } 798 } 799 800 /* we do this while holding the orphan dir lock because we 801 * don't want recovery being run from another node to try an 802 * inode delete underneath us -- this will result in two nodes 803 * truncating the same file! */ 804 status = ocfs2_truncate_for_delete(osb, inode, di_bh); 805 if (status < 0) { 806 mlog_errno(status); 807 goto bail_unlock_dir; 808 } 809 810 /* Remove any dir index tree */ 811 if (S_ISDIR(inode->i_mode)) { 812 status = ocfs2_dx_dir_truncate(inode, di_bh); 813 if (status) { 814 mlog_errno(status); 815 goto bail_unlock_dir; 816 } 817 } 818 819 /*Free extended attribute resources associated with this inode.*/ 820 status = ocfs2_xattr_remove(inode, di_bh); 821 if (status < 0) { 822 mlog_errno(status); 823 goto bail_unlock_dir; 824 } 825 826 status = ocfs2_remove_refcount_tree(inode, di_bh); 827 if (status < 0) { 828 mlog_errno(status); 829 goto bail_unlock_dir; 830 } 831 832 status = ocfs2_remove_inode(inode, di_bh, orphan_dir_inode, 833 orphan_dir_bh); 834 if (status < 0) 835 mlog_errno(status); 836 837 bail_unlock_dir: 838 if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR) 839 return status; 840 841 ocfs2_inode_unlock(orphan_dir_inode, 1); 842 inode_unlock(orphan_dir_inode); 843 brelse(orphan_dir_bh); 844 bail: 845 iput(orphan_dir_inode); 846 ocfs2_signal_wipe_completion(osb, orphaned_slot); 847 848 return status; 849 } 850 851 /* There is a series of simple checks that should be done before a 852 * trylock is even considered. Encapsulate those in this function. */ 853 static int ocfs2_inode_is_valid_to_delete(struct inode *inode) 854 { 855 int ret = 0; 856 struct ocfs2_inode_info *oi = OCFS2_I(inode); 857 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 858 859 trace_ocfs2_inode_is_valid_to_delete(current, osb->dc_task, 860 (unsigned long long)oi->ip_blkno, 861 oi->ip_flags); 862 863 /* We shouldn't be getting here for the root directory 864 * inode.. */ 865 if (inode == osb->root_inode) { 866 mlog(ML_ERROR, "Skipping delete of root inode.\n"); 867 goto bail; 868 } 869 870 /* 871 * If we're coming from downconvert_thread we can't go into our own 872 * voting [hello, deadlock city!] so we cannot delete the inode. But 873 * since we dropped last inode ref when downconverting dentry lock, 874 * we cannot have the file open and thus the node doing unlink will 875 * take care of deleting the inode. 876 */ 877 if (current == osb->dc_task) 878 goto bail; 879 880 spin_lock(&oi->ip_lock); 881 /* OCFS2 *never* deletes system files. This should technically 882 * never get here as system file inodes should always have a 883 * positive link count. */ 884 if (oi->ip_flags & OCFS2_INODE_SYSTEM_FILE) { 885 mlog(ML_ERROR, "Skipping delete of system file %llu\n", 886 (unsigned long long)oi->ip_blkno); 887 goto bail_unlock; 888 } 889 890 ret = 1; 891 bail_unlock: 892 spin_unlock(&oi->ip_lock); 893 bail: 894 return ret; 895 } 896 897 /* Query the cluster to determine whether we should wipe an inode from 898 * disk or not. 899 * 900 * Requires the inode to have the cluster lock. */ 901 static int ocfs2_query_inode_wipe(struct inode *inode, 902 struct buffer_head *di_bh, 903 int *wipe) 904 { 905 int status = 0, reason = 0; 906 struct ocfs2_inode_info *oi = OCFS2_I(inode); 907 struct ocfs2_dinode *di; 908 909 *wipe = 0; 910 911 trace_ocfs2_query_inode_wipe_begin((unsigned long long)oi->ip_blkno, 912 inode->i_nlink); 913 914 /* While we were waiting for the cluster lock in 915 * ocfs2_delete_inode, another node might have asked to delete 916 * the inode. Recheck our flags to catch this. */ 917 if (!ocfs2_inode_is_valid_to_delete(inode)) { 918 reason = 1; 919 goto bail; 920 } 921 922 /* Now that we have an up to date inode, we can double check 923 * the link count. */ 924 if (inode->i_nlink) 925 goto bail; 926 927 /* Do some basic inode verification... */ 928 di = (struct ocfs2_dinode *) di_bh->b_data; 929 if (!(di->i_flags & cpu_to_le32(OCFS2_ORPHANED_FL)) && 930 !(oi->ip_flags & OCFS2_INODE_SKIP_ORPHAN_DIR)) { 931 /* 932 * Inodes in the orphan dir must have ORPHANED_FL. The only 933 * inodes that come back out of the orphan dir are reflink 934 * targets. A reflink target may be moved out of the orphan 935 * dir between the time we scan the directory and the time we 936 * process it. This would lead to HAS_REFCOUNT_FL being set but 937 * ORPHANED_FL not. 938 */ 939 if (di->i_dyn_features & cpu_to_le16(OCFS2_HAS_REFCOUNT_FL)) { 940 reason = 2; 941 goto bail; 942 } 943 944 /* for lack of a better error? */ 945 status = -EEXIST; 946 mlog(ML_ERROR, 947 "Inode %llu (on-disk %llu) not orphaned! " 948 "Disk flags 0x%x, inode flags 0x%x\n", 949 (unsigned long long)oi->ip_blkno, 950 (unsigned long long)le64_to_cpu(di->i_blkno), 951 le32_to_cpu(di->i_flags), oi->ip_flags); 952 goto bail; 953 } 954 955 /* has someone already deleted us?! baaad... */ 956 if (di->i_dtime) { 957 status = -EEXIST; 958 mlog_errno(status); 959 goto bail; 960 } 961 962 /* 963 * This is how ocfs2 determines whether an inode is still live 964 * within the cluster. Every node takes a shared read lock on 965 * the inode open lock in ocfs2_read_locked_inode(). When we 966 * get to ->delete_inode(), each node tries to convert it's 967 * lock to an exclusive. Trylocks are serialized by the inode 968 * meta data lock. If the upconvert succeeds, we know the inode 969 * is no longer live and can be deleted. 970 * 971 * Though we call this with the meta data lock held, the 972 * trylock keeps us from ABBA deadlock. 973 */ 974 status = ocfs2_try_open_lock(inode, 1); 975 if (status == -EAGAIN) { 976 status = 0; 977 reason = 3; 978 goto bail; 979 } 980 if (status < 0) { 981 mlog_errno(status); 982 goto bail; 983 } 984 985 *wipe = 1; 986 trace_ocfs2_query_inode_wipe_succ(le16_to_cpu(di->i_orphaned_slot)); 987 988 bail: 989 trace_ocfs2_query_inode_wipe_end(status, reason); 990 return status; 991 } 992 993 /* Support function for ocfs2_delete_inode. Will help us keep the 994 * inode data in a consistent state for clear_inode. Always truncates 995 * pages, optionally sync's them first. */ 996 static void ocfs2_cleanup_delete_inode(struct inode *inode, 997 int sync_data) 998 { 999 trace_ocfs2_cleanup_delete_inode( 1000 (unsigned long long)OCFS2_I(inode)->ip_blkno, sync_data); 1001 if (sync_data) 1002 filemap_write_and_wait(inode->i_mapping); 1003 truncate_inode_pages_final(&inode->i_data); 1004 } 1005 1006 static void ocfs2_delete_inode(struct inode *inode) 1007 { 1008 int wipe, status; 1009 sigset_t oldset; 1010 struct buffer_head *di_bh = NULL; 1011 struct ocfs2_dinode *di = NULL; 1012 1013 trace_ocfs2_delete_inode(inode->i_ino, 1014 (unsigned long long)OCFS2_I(inode)->ip_blkno, 1015 is_bad_inode(inode)); 1016 1017 /* When we fail in read_inode() we mark inode as bad. The second test 1018 * catches the case when inode allocation fails before allocating 1019 * a block for inode. */ 1020 if (is_bad_inode(inode) || !OCFS2_I(inode)->ip_blkno) 1021 goto bail; 1022 1023 if (!ocfs2_inode_is_valid_to_delete(inode)) { 1024 /* It's probably not necessary to truncate_inode_pages 1025 * here but we do it for safety anyway (it will most 1026 * likely be a no-op anyway) */ 1027 ocfs2_cleanup_delete_inode(inode, 0); 1028 goto bail; 1029 } 1030 1031 dquot_initialize(inode); 1032 1033 /* We want to block signals in delete_inode as the lock and 1034 * messaging paths may return us -ERESTARTSYS. Which would 1035 * cause us to exit early, resulting in inodes being orphaned 1036 * forever. */ 1037 ocfs2_block_signals(&oldset); 1038 1039 /* 1040 * Synchronize us against ocfs2_get_dentry. We take this in 1041 * shared mode so that all nodes can still concurrently 1042 * process deletes. 1043 */ 1044 status = ocfs2_nfs_sync_lock(OCFS2_SB(inode->i_sb), 0); 1045 if (status < 0) { 1046 mlog(ML_ERROR, "getting nfs sync lock(PR) failed %d\n", status); 1047 ocfs2_cleanup_delete_inode(inode, 0); 1048 goto bail_unblock; 1049 } 1050 /* Lock down the inode. This gives us an up to date view of 1051 * it's metadata (for verification), and allows us to 1052 * serialize delete_inode on multiple nodes. 1053 * 1054 * Even though we might be doing a truncate, we don't take the 1055 * allocation lock here as it won't be needed - nobody will 1056 * have the file open. 1057 */ 1058 status = ocfs2_inode_lock(inode, &di_bh, 1); 1059 if (status < 0) { 1060 if (status != -ENOENT) 1061 mlog_errno(status); 1062 ocfs2_cleanup_delete_inode(inode, 0); 1063 goto bail_unlock_nfs_sync; 1064 } 1065 1066 di = (struct ocfs2_dinode *)di_bh->b_data; 1067 /* Skip inode deletion and wait for dio orphan entry recovered 1068 * first */ 1069 if (unlikely(di->i_flags & cpu_to_le32(OCFS2_DIO_ORPHANED_FL))) { 1070 ocfs2_cleanup_delete_inode(inode, 0); 1071 goto bail_unlock_inode; 1072 } 1073 1074 /* Query the cluster. This will be the final decision made 1075 * before we go ahead and wipe the inode. */ 1076 status = ocfs2_query_inode_wipe(inode, di_bh, &wipe); 1077 if (!wipe || status < 0) { 1078 /* Error and remote inode busy both mean we won't be 1079 * removing the inode, so they take almost the same 1080 * path. */ 1081 if (status < 0) 1082 mlog_errno(status); 1083 1084 /* Someone in the cluster has disallowed a wipe of 1085 * this inode, or it was never completely 1086 * orphaned. Write out the pages and exit now. */ 1087 ocfs2_cleanup_delete_inode(inode, 1); 1088 goto bail_unlock_inode; 1089 } 1090 1091 ocfs2_cleanup_delete_inode(inode, 0); 1092 1093 status = ocfs2_wipe_inode(inode, di_bh); 1094 if (status < 0) { 1095 if (status != -EDEADLK) 1096 mlog_errno(status); 1097 goto bail_unlock_inode; 1098 } 1099 1100 /* 1101 * Mark the inode as successfully deleted. 1102 * 1103 * This is important for ocfs2_clear_inode() as it will check 1104 * this flag and skip any checkpointing work 1105 * 1106 * ocfs2_stuff_meta_lvb() also uses this flag to invalidate 1107 * the LVB for other nodes. 1108 */ 1109 OCFS2_I(inode)->ip_flags |= OCFS2_INODE_DELETED; 1110 1111 bail_unlock_inode: 1112 ocfs2_inode_unlock(inode, 1); 1113 brelse(di_bh); 1114 1115 bail_unlock_nfs_sync: 1116 ocfs2_nfs_sync_unlock(OCFS2_SB(inode->i_sb), 0); 1117 1118 bail_unblock: 1119 ocfs2_unblock_signals(&oldset); 1120 bail: 1121 return; 1122 } 1123 1124 static void ocfs2_clear_inode(struct inode *inode) 1125 { 1126 int status; 1127 struct ocfs2_inode_info *oi = OCFS2_I(inode); 1128 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); 1129 1130 clear_inode(inode); 1131 trace_ocfs2_clear_inode((unsigned long long)oi->ip_blkno, 1132 inode->i_nlink); 1133 1134 mlog_bug_on_msg(osb == NULL, 1135 "Inode=%lu\n", inode->i_ino); 1136 1137 dquot_drop(inode); 1138 1139 /* To prevent remote deletes we hold open lock before, now it 1140 * is time to unlock PR and EX open locks. */ 1141 ocfs2_open_unlock(inode); 1142 1143 /* Do these before all the other work so that we don't bounce 1144 * the downconvert thread while waiting to destroy the locks. */ 1145 ocfs2_mark_lockres_freeing(osb, &oi->ip_rw_lockres); 1146 ocfs2_mark_lockres_freeing(osb, &oi->ip_inode_lockres); 1147 ocfs2_mark_lockres_freeing(osb, &oi->ip_open_lockres); 1148 1149 ocfs2_resv_discard(&osb->osb_la_resmap, 1150 &oi->ip_la_data_resv); 1151 ocfs2_resv_init_once(&oi->ip_la_data_resv); 1152 1153 /* We very well may get a clear_inode before all an inodes 1154 * metadata has hit disk. Of course, we can't drop any cluster 1155 * locks until the journal has finished with it. The only 1156 * exception here are successfully wiped inodes - their 1157 * metadata can now be considered to be part of the system 1158 * inodes from which it came. */ 1159 if (!(oi->ip_flags & OCFS2_INODE_DELETED)) 1160 ocfs2_checkpoint_inode(inode); 1161 1162 mlog_bug_on_msg(!list_empty(&oi->ip_io_markers), 1163 "Clear inode of %llu, inode has io markers\n", 1164 (unsigned long long)oi->ip_blkno); 1165 mlog_bug_on_msg(!list_empty(&oi->ip_unwritten_list), 1166 "Clear inode of %llu, inode has unwritten extents\n", 1167 (unsigned long long)oi->ip_blkno); 1168 1169 ocfs2_extent_map_trunc(inode, 0); 1170 1171 status = ocfs2_drop_inode_locks(inode); 1172 if (status < 0) 1173 mlog_errno(status); 1174 1175 ocfs2_lock_res_free(&oi->ip_rw_lockres); 1176 ocfs2_lock_res_free(&oi->ip_inode_lockres); 1177 ocfs2_lock_res_free(&oi->ip_open_lockres); 1178 1179 ocfs2_metadata_cache_exit(INODE_CACHE(inode)); 1180 1181 mlog_bug_on_msg(INODE_CACHE(inode)->ci_num_cached, 1182 "Clear inode of %llu, inode has %u cache items\n", 1183 (unsigned long long)oi->ip_blkno, 1184 INODE_CACHE(inode)->ci_num_cached); 1185 1186 mlog_bug_on_msg(!(INODE_CACHE(inode)->ci_flags & OCFS2_CACHE_FL_INLINE), 1187 "Clear inode of %llu, inode has a bad flag\n", 1188 (unsigned long long)oi->ip_blkno); 1189 1190 mlog_bug_on_msg(spin_is_locked(&oi->ip_lock), 1191 "Clear inode of %llu, inode is locked\n", 1192 (unsigned long long)oi->ip_blkno); 1193 1194 mlog_bug_on_msg(!mutex_trylock(&oi->ip_io_mutex), 1195 "Clear inode of %llu, io_mutex is locked\n", 1196 (unsigned long long)oi->ip_blkno); 1197 mutex_unlock(&oi->ip_io_mutex); 1198 1199 /* 1200 * down_trylock() returns 0, down_write_trylock() returns 1 1201 * kernel 1, world 0 1202 */ 1203 mlog_bug_on_msg(!down_write_trylock(&oi->ip_alloc_sem), 1204 "Clear inode of %llu, alloc_sem is locked\n", 1205 (unsigned long long)oi->ip_blkno); 1206 up_write(&oi->ip_alloc_sem); 1207 1208 mlog_bug_on_msg(oi->ip_open_count, 1209 "Clear inode of %llu has open count %d\n", 1210 (unsigned long long)oi->ip_blkno, oi->ip_open_count); 1211 1212 /* Clear all other flags. */ 1213 oi->ip_flags = 0; 1214 oi->ip_dir_start_lookup = 0; 1215 oi->ip_blkno = 0ULL; 1216 1217 /* 1218 * ip_jinode is used to track txns against this inode. We ensure that 1219 * the journal is flushed before journal shutdown. Thus it is safe to 1220 * have inodes get cleaned up after journal shutdown. 1221 */ 1222 jbd2_journal_release_jbd_inode(osb->journal->j_journal, 1223 &oi->ip_jinode); 1224 } 1225 1226 void ocfs2_evict_inode(struct inode *inode) 1227 { 1228 if (!inode->i_nlink || 1229 (OCFS2_I(inode)->ip_flags & OCFS2_INODE_MAYBE_ORPHANED)) { 1230 ocfs2_delete_inode(inode); 1231 } else { 1232 truncate_inode_pages_final(&inode->i_data); 1233 } 1234 ocfs2_clear_inode(inode); 1235 } 1236 1237 /* Called under inode_lock, with no more references on the 1238 * struct inode, so it's safe here to check the flags field 1239 * and to manipulate i_nlink without any other locks. */ 1240 int ocfs2_drop_inode(struct inode *inode) 1241 { 1242 struct ocfs2_inode_info *oi = OCFS2_I(inode); 1243 1244 trace_ocfs2_drop_inode((unsigned long long)oi->ip_blkno, 1245 inode->i_nlink, oi->ip_flags); 1246 1247 assert_spin_locked(&inode->i_lock); 1248 inode->i_state |= I_WILL_FREE; 1249 spin_unlock(&inode->i_lock); 1250 write_inode_now(inode, 1); 1251 spin_lock(&inode->i_lock); 1252 WARN_ON(inode->i_state & I_NEW); 1253 inode->i_state &= ~I_WILL_FREE; 1254 1255 return 1; 1256 } 1257 1258 /* 1259 * This is called from our getattr. 1260 */ 1261 int ocfs2_inode_revalidate(struct dentry *dentry) 1262 { 1263 struct inode *inode = d_inode(dentry); 1264 int status = 0; 1265 1266 trace_ocfs2_inode_revalidate(inode, 1267 inode ? (unsigned long long)OCFS2_I(inode)->ip_blkno : 0ULL, 1268 inode ? (unsigned long long)OCFS2_I(inode)->ip_flags : 0); 1269 1270 if (!inode) { 1271 status = -ENOENT; 1272 goto bail; 1273 } 1274 1275 spin_lock(&OCFS2_I(inode)->ip_lock); 1276 if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) { 1277 spin_unlock(&OCFS2_I(inode)->ip_lock); 1278 status = -ENOENT; 1279 goto bail; 1280 } 1281 spin_unlock(&OCFS2_I(inode)->ip_lock); 1282 1283 /* Let ocfs2_inode_lock do the work of updating our struct 1284 * inode for us. */ 1285 status = ocfs2_inode_lock(inode, NULL, 0); 1286 if (status < 0) { 1287 if (status != -ENOENT) 1288 mlog_errno(status); 1289 goto bail; 1290 } 1291 ocfs2_inode_unlock(inode, 0); 1292 bail: 1293 return status; 1294 } 1295 1296 /* 1297 * Updates a disk inode from a 1298 * struct inode. 1299 * Only takes ip_lock. 1300 */ 1301 int ocfs2_mark_inode_dirty(handle_t *handle, 1302 struct inode *inode, 1303 struct buffer_head *bh) 1304 { 1305 int status; 1306 struct ocfs2_dinode *fe = (struct ocfs2_dinode *) bh->b_data; 1307 1308 trace_ocfs2_mark_inode_dirty((unsigned long long)OCFS2_I(inode)->ip_blkno); 1309 1310 status = ocfs2_journal_access_di(handle, INODE_CACHE(inode), bh, 1311 OCFS2_JOURNAL_ACCESS_WRITE); 1312 if (status < 0) { 1313 mlog_errno(status); 1314 goto leave; 1315 } 1316 1317 spin_lock(&OCFS2_I(inode)->ip_lock); 1318 fe->i_clusters = cpu_to_le32(OCFS2_I(inode)->ip_clusters); 1319 ocfs2_get_inode_flags(OCFS2_I(inode)); 1320 fe->i_attr = cpu_to_le32(OCFS2_I(inode)->ip_attr); 1321 fe->i_dyn_features = cpu_to_le16(OCFS2_I(inode)->ip_dyn_features); 1322 spin_unlock(&OCFS2_I(inode)->ip_lock); 1323 1324 fe->i_size = cpu_to_le64(i_size_read(inode)); 1325 ocfs2_set_links_count(fe, inode->i_nlink); 1326 fe->i_uid = cpu_to_le32(i_uid_read(inode)); 1327 fe->i_gid = cpu_to_le32(i_gid_read(inode)); 1328 fe->i_mode = cpu_to_le16(inode->i_mode); 1329 fe->i_atime = cpu_to_le64(inode_get_atime_sec(inode)); 1330 fe->i_atime_nsec = cpu_to_le32(inode_get_atime_nsec(inode)); 1331 fe->i_ctime = cpu_to_le64(inode_get_ctime_sec(inode)); 1332 fe->i_ctime_nsec = cpu_to_le32(inode_get_ctime_nsec(inode)); 1333 fe->i_mtime = cpu_to_le64(inode_get_mtime_sec(inode)); 1334 fe->i_mtime_nsec = cpu_to_le32(inode_get_mtime_nsec(inode)); 1335 1336 ocfs2_journal_dirty(handle, bh); 1337 ocfs2_update_inode_fsync_trans(handle, inode, 1); 1338 leave: 1339 return status; 1340 } 1341 1342 /* 1343 * 1344 * Updates a struct inode from a disk inode. 1345 * does no i/o, only takes ip_lock. 1346 */ 1347 void ocfs2_refresh_inode(struct inode *inode, 1348 struct ocfs2_dinode *fe) 1349 { 1350 spin_lock(&OCFS2_I(inode)->ip_lock); 1351 1352 OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters); 1353 OCFS2_I(inode)->ip_attr = le32_to_cpu(fe->i_attr); 1354 OCFS2_I(inode)->ip_dyn_features = le16_to_cpu(fe->i_dyn_features); 1355 ocfs2_set_inode_flags(inode); 1356 i_size_write(inode, le64_to_cpu(fe->i_size)); 1357 set_nlink(inode, ocfs2_read_links_count(fe)); 1358 i_uid_write(inode, le32_to_cpu(fe->i_uid)); 1359 i_gid_write(inode, le32_to_cpu(fe->i_gid)); 1360 inode->i_mode = le16_to_cpu(fe->i_mode); 1361 if (S_ISLNK(inode->i_mode) && le32_to_cpu(fe->i_clusters) == 0) 1362 inode->i_blocks = 0; 1363 else 1364 inode->i_blocks = ocfs2_inode_sector_count(inode); 1365 inode_set_atime(inode, le64_to_cpu(fe->i_atime), 1366 le32_to_cpu(fe->i_atime_nsec)); 1367 inode_set_mtime(inode, le64_to_cpu(fe->i_mtime), 1368 le32_to_cpu(fe->i_mtime_nsec)); 1369 inode_set_ctime(inode, le64_to_cpu(fe->i_ctime), 1370 le32_to_cpu(fe->i_ctime_nsec)); 1371 1372 spin_unlock(&OCFS2_I(inode)->ip_lock); 1373 } 1374 1375 int ocfs2_validate_inode_block(struct super_block *sb, 1376 struct buffer_head *bh) 1377 { 1378 int rc; 1379 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data; 1380 1381 trace_ocfs2_validate_inode_block((unsigned long long)bh->b_blocknr); 1382 1383 BUG_ON(!buffer_uptodate(bh)); 1384 1385 /* 1386 * If the ecc fails, we return the error but otherwise 1387 * leave the filesystem running. We know any error is 1388 * local to this block. 1389 */ 1390 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check); 1391 if (rc) { 1392 mlog(ML_ERROR, "Checksum failed for dinode %llu\n", 1393 (unsigned long long)bh->b_blocknr); 1394 goto bail; 1395 } 1396 1397 /* 1398 * Errors after here are fatal. 1399 */ 1400 1401 rc = -EINVAL; 1402 1403 if (!OCFS2_IS_VALID_DINODE(di)) { 1404 rc = ocfs2_error(sb, "Invalid dinode #%llu: signature = %.*s\n", 1405 (unsigned long long)bh->b_blocknr, 7, 1406 di->i_signature); 1407 goto bail; 1408 } 1409 1410 if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) { 1411 rc = ocfs2_error(sb, "Invalid dinode #%llu: i_blkno is %llu\n", 1412 (unsigned long long)bh->b_blocknr, 1413 (unsigned long long)le64_to_cpu(di->i_blkno)); 1414 goto bail; 1415 } 1416 1417 if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) { 1418 rc = ocfs2_error(sb, 1419 "Invalid dinode #%llu: OCFS2_VALID_FL not set\n", 1420 (unsigned long long)bh->b_blocknr); 1421 goto bail; 1422 } 1423 1424 if (le32_to_cpu(di->i_fs_generation) != 1425 OCFS2_SB(sb)->fs_generation) { 1426 rc = ocfs2_error(sb, 1427 "Invalid dinode #%llu: fs_generation is %u\n", 1428 (unsigned long long)bh->b_blocknr, 1429 le32_to_cpu(di->i_fs_generation)); 1430 goto bail; 1431 } 1432 1433 rc = 0; 1434 1435 bail: 1436 return rc; 1437 } 1438 1439 static int ocfs2_filecheck_validate_inode_block(struct super_block *sb, 1440 struct buffer_head *bh) 1441 { 1442 int rc = 0; 1443 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data; 1444 1445 trace_ocfs2_filecheck_validate_inode_block( 1446 (unsigned long long)bh->b_blocknr); 1447 1448 BUG_ON(!buffer_uptodate(bh)); 1449 1450 /* 1451 * Call ocfs2_validate_meta_ecc() first since it has ecc repair 1452 * function, but we should not return error immediately when ecc 1453 * validation fails, because the reason is quite likely the invalid 1454 * inode number inputted. 1455 */ 1456 rc = ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check); 1457 if (rc) { 1458 mlog(ML_ERROR, 1459 "Filecheck: checksum failed for dinode %llu\n", 1460 (unsigned long long)bh->b_blocknr); 1461 rc = -OCFS2_FILECHECK_ERR_BLOCKECC; 1462 } 1463 1464 if (!OCFS2_IS_VALID_DINODE(di)) { 1465 mlog(ML_ERROR, 1466 "Filecheck: invalid dinode #%llu: signature = %.*s\n", 1467 (unsigned long long)bh->b_blocknr, 7, di->i_signature); 1468 rc = -OCFS2_FILECHECK_ERR_INVALIDINO; 1469 goto bail; 1470 } else if (rc) 1471 goto bail; 1472 1473 if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) { 1474 mlog(ML_ERROR, 1475 "Filecheck: invalid dinode #%llu: i_blkno is %llu\n", 1476 (unsigned long long)bh->b_blocknr, 1477 (unsigned long long)le64_to_cpu(di->i_blkno)); 1478 rc = -OCFS2_FILECHECK_ERR_BLOCKNO; 1479 goto bail; 1480 } 1481 1482 if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) { 1483 mlog(ML_ERROR, 1484 "Filecheck: invalid dinode #%llu: OCFS2_VALID_FL " 1485 "not set\n", 1486 (unsigned long long)bh->b_blocknr); 1487 rc = -OCFS2_FILECHECK_ERR_VALIDFLAG; 1488 goto bail; 1489 } 1490 1491 if (le32_to_cpu(di->i_fs_generation) != 1492 OCFS2_SB(sb)->fs_generation) { 1493 mlog(ML_ERROR, 1494 "Filecheck: invalid dinode #%llu: fs_generation is %u\n", 1495 (unsigned long long)bh->b_blocknr, 1496 le32_to_cpu(di->i_fs_generation)); 1497 rc = -OCFS2_FILECHECK_ERR_GENERATION; 1498 } 1499 1500 bail: 1501 return rc; 1502 } 1503 1504 static int ocfs2_filecheck_repair_inode_block(struct super_block *sb, 1505 struct buffer_head *bh) 1506 { 1507 int changed = 0; 1508 struct ocfs2_dinode *di = (struct ocfs2_dinode *)bh->b_data; 1509 1510 if (!ocfs2_filecheck_validate_inode_block(sb, bh)) 1511 return 0; 1512 1513 trace_ocfs2_filecheck_repair_inode_block( 1514 (unsigned long long)bh->b_blocknr); 1515 1516 if (ocfs2_is_hard_readonly(OCFS2_SB(sb)) || 1517 ocfs2_is_soft_readonly(OCFS2_SB(sb))) { 1518 mlog(ML_ERROR, 1519 "Filecheck: cannot repair dinode #%llu " 1520 "on readonly filesystem\n", 1521 (unsigned long long)bh->b_blocknr); 1522 return -OCFS2_FILECHECK_ERR_READONLY; 1523 } 1524 1525 if (buffer_jbd(bh)) { 1526 mlog(ML_ERROR, 1527 "Filecheck: cannot repair dinode #%llu, " 1528 "its buffer is in jbd\n", 1529 (unsigned long long)bh->b_blocknr); 1530 return -OCFS2_FILECHECK_ERR_INJBD; 1531 } 1532 1533 if (!OCFS2_IS_VALID_DINODE(di)) { 1534 /* Cannot fix invalid inode block */ 1535 return -OCFS2_FILECHECK_ERR_INVALIDINO; 1536 } 1537 1538 if (!(di->i_flags & cpu_to_le32(OCFS2_VALID_FL))) { 1539 /* Cannot just add VALID_FL flag back as a fix, 1540 * need more things to check here. 1541 */ 1542 return -OCFS2_FILECHECK_ERR_VALIDFLAG; 1543 } 1544 1545 if (le64_to_cpu(di->i_blkno) != bh->b_blocknr) { 1546 di->i_blkno = cpu_to_le64(bh->b_blocknr); 1547 changed = 1; 1548 mlog(ML_ERROR, 1549 "Filecheck: reset dinode #%llu: i_blkno to %llu\n", 1550 (unsigned long long)bh->b_blocknr, 1551 (unsigned long long)le64_to_cpu(di->i_blkno)); 1552 } 1553 1554 if (le32_to_cpu(di->i_fs_generation) != 1555 OCFS2_SB(sb)->fs_generation) { 1556 di->i_fs_generation = cpu_to_le32(OCFS2_SB(sb)->fs_generation); 1557 changed = 1; 1558 mlog(ML_ERROR, 1559 "Filecheck: reset dinode #%llu: fs_generation to %u\n", 1560 (unsigned long long)bh->b_blocknr, 1561 le32_to_cpu(di->i_fs_generation)); 1562 } 1563 1564 if (ocfs2_dinode_has_extents(di) && 1565 le16_to_cpu(di->id2.i_list.l_next_free_rec) > le16_to_cpu(di->id2.i_list.l_count)) { 1566 di->id2.i_list.l_next_free_rec = di->id2.i_list.l_count; 1567 changed = 1; 1568 mlog(ML_ERROR, 1569 "Filecheck: reset dinode #%llu: l_next_free_rec to %u\n", 1570 (unsigned long long)bh->b_blocknr, 1571 le16_to_cpu(di->id2.i_list.l_next_free_rec)); 1572 } 1573 1574 if (changed || ocfs2_validate_meta_ecc(sb, bh->b_data, &di->i_check)) { 1575 ocfs2_compute_meta_ecc(sb, bh->b_data, &di->i_check); 1576 mark_buffer_dirty(bh); 1577 mlog(ML_ERROR, 1578 "Filecheck: reset dinode #%llu: compute meta ecc\n", 1579 (unsigned long long)bh->b_blocknr); 1580 } 1581 1582 return 0; 1583 } 1584 1585 static int 1586 ocfs2_filecheck_read_inode_block_full(struct inode *inode, 1587 struct buffer_head **bh, 1588 int flags, int type) 1589 { 1590 int rc; 1591 struct buffer_head *tmp = *bh; 1592 1593 if (!type) /* Check inode block */ 1594 rc = ocfs2_read_blocks(INODE_CACHE(inode), 1595 OCFS2_I(inode)->ip_blkno, 1596 1, &tmp, flags, 1597 ocfs2_filecheck_validate_inode_block); 1598 else /* Repair inode block */ 1599 rc = ocfs2_read_blocks(INODE_CACHE(inode), 1600 OCFS2_I(inode)->ip_blkno, 1601 1, &tmp, flags, 1602 ocfs2_filecheck_repair_inode_block); 1603 1604 /* If ocfs2_read_blocks() got us a new bh, pass it up. */ 1605 if (!rc && !*bh) 1606 *bh = tmp; 1607 1608 return rc; 1609 } 1610 1611 int ocfs2_read_inode_block_full(struct inode *inode, struct buffer_head **bh, 1612 int flags) 1613 { 1614 int rc; 1615 struct buffer_head *tmp = *bh; 1616 1617 rc = ocfs2_read_blocks(INODE_CACHE(inode), OCFS2_I(inode)->ip_blkno, 1618 1, &tmp, flags, ocfs2_validate_inode_block); 1619 1620 /* If ocfs2_read_blocks() got us a new bh, pass it up. */ 1621 if (!rc && !*bh) 1622 *bh = tmp; 1623 1624 return rc; 1625 } 1626 1627 int ocfs2_read_inode_block(struct inode *inode, struct buffer_head **bh) 1628 { 1629 return ocfs2_read_inode_block_full(inode, bh, 0); 1630 } 1631 1632 1633 static u64 ocfs2_inode_cache_owner(struct ocfs2_caching_info *ci) 1634 { 1635 struct ocfs2_inode_info *oi = cache_info_to_inode(ci); 1636 1637 return oi->ip_blkno; 1638 } 1639 1640 static struct super_block *ocfs2_inode_cache_get_super(struct ocfs2_caching_info *ci) 1641 { 1642 struct ocfs2_inode_info *oi = cache_info_to_inode(ci); 1643 1644 return oi->vfs_inode.i_sb; 1645 } 1646 1647 static void ocfs2_inode_cache_lock(struct ocfs2_caching_info *ci) 1648 __acquires(&oi->ip_lock) 1649 { 1650 struct ocfs2_inode_info *oi = cache_info_to_inode(ci); 1651 1652 spin_lock(&oi->ip_lock); 1653 } 1654 1655 static void ocfs2_inode_cache_unlock(struct ocfs2_caching_info *ci) 1656 __releases(&oi->ip_lock) 1657 { 1658 struct ocfs2_inode_info *oi = cache_info_to_inode(ci); 1659 1660 spin_unlock(&oi->ip_lock); 1661 } 1662 1663 static void ocfs2_inode_cache_io_lock(struct ocfs2_caching_info *ci) 1664 { 1665 struct ocfs2_inode_info *oi = cache_info_to_inode(ci); 1666 1667 mutex_lock(&oi->ip_io_mutex); 1668 } 1669 1670 static void ocfs2_inode_cache_io_unlock(struct ocfs2_caching_info *ci) 1671 { 1672 struct ocfs2_inode_info *oi = cache_info_to_inode(ci); 1673 1674 mutex_unlock(&oi->ip_io_mutex); 1675 } 1676 1677 const struct ocfs2_caching_operations ocfs2_inode_caching_ops = { 1678 .co_owner = ocfs2_inode_cache_owner, 1679 .co_get_super = ocfs2_inode_cache_get_super, 1680 .co_cache_lock = ocfs2_inode_cache_lock, 1681 .co_cache_unlock = ocfs2_inode_cache_unlock, 1682 .co_io_lock = ocfs2_inode_cache_io_lock, 1683 .co_io_unlock = ocfs2_inode_cache_io_unlock, 1684 }; 1685 1686