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