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