1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. 4 * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. 5 */ 6 7 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 8 9 #include <linux/spinlock.h> 10 #include <linux/completion.h> 11 #include <linux/buffer_head.h> 12 #include <linux/kthread.h> 13 #include <linux/crc32.h> 14 #include <linux/gfs2_ondisk.h> 15 #include <linux/delay.h> 16 #include <linux/uaccess.h> 17 18 #include "gfs2.h" 19 #include "incore.h" 20 #include "glock.h" 21 #include "glops.h" 22 #include "log.h" 23 #include "lops.h" 24 #include "recovery.h" 25 #include "rgrp.h" 26 #include "super.h" 27 #include "util.h" 28 29 struct kmem_cache *gfs2_glock_cachep __read_mostly; 30 struct kmem_cache *gfs2_glock_aspace_cachep __read_mostly; 31 struct kmem_cache *gfs2_inode_cachep __read_mostly; 32 struct kmem_cache *gfs2_bufdata_cachep __read_mostly; 33 struct kmem_cache *gfs2_rgrpd_cachep __read_mostly; 34 struct kmem_cache *gfs2_quotad_cachep __read_mostly; 35 struct kmem_cache *gfs2_qadata_cachep __read_mostly; 36 struct kmem_cache *gfs2_trans_cachep __read_mostly; 37 mempool_t *gfs2_page_pool __read_mostly; 38 39 void gfs2_assert_i(struct gfs2_sbd *sdp) 40 { 41 fs_emerg(sdp, "fatal assertion failed\n"); 42 } 43 44 /** 45 * check_journal_clean - Make sure a journal is clean for a spectator mount 46 * @sdp: The GFS2 superblock 47 * @jd: The journal descriptor 48 * @verbose: Show more prints in the log 49 * 50 * Returns: 0 if the journal is clean or locked, else an error 51 */ 52 int check_journal_clean(struct gfs2_sbd *sdp, struct gfs2_jdesc *jd, 53 bool verbose) 54 { 55 int error; 56 struct gfs2_holder j_gh; 57 struct gfs2_log_header_host head; 58 struct gfs2_glock *gl = gfs2_inode_glock(jd->jd_inode); 59 60 error = gfs2_glock_nq_init(gl, LM_ST_SHARED, LM_FLAG_RECOVER | 61 GL_EXACT | GL_NOCACHE, &j_gh); 62 if (error) { 63 if (verbose) 64 fs_err(sdp, "Error %d locking journal for spectator " 65 "mount.\n", error); 66 return -EPERM; 67 } 68 error = gfs2_jdesc_check(jd); 69 if (error) { 70 if (verbose) 71 fs_err(sdp, "Error checking journal for spectator " 72 "mount.\n"); 73 goto out_unlock; 74 } 75 error = gfs2_find_jhead(jd, &head); 76 if (error) { 77 if (verbose) 78 fs_err(sdp, "Error parsing journal for spectator " 79 "mount.\n"); 80 goto out_unlock; 81 } 82 if (!(head.lh_flags & GFS2_LOG_HEAD_UNMOUNT)) { 83 error = -EPERM; 84 if (verbose) 85 fs_err(sdp, "jid=%u: Journal is dirty, so the first " 86 "mounter must not be a spectator.\n", 87 jd->jd_jid); 88 } 89 90 out_unlock: 91 gfs2_glock_dq_uninit(&j_gh); 92 return error; 93 } 94 95 /** 96 * gfs2_freeze_lock_shared - hold the freeze glock 97 * @sdp: the superblock 98 */ 99 int gfs2_freeze_lock_shared(struct gfs2_sbd *sdp) 100 { 101 int flags = LM_FLAG_RECOVER | GL_EXACT; 102 int error; 103 104 error = gfs2_glock_nq_init(sdp->sd_freeze_gl, LM_ST_SHARED, flags, 105 &sdp->sd_freeze_gh); 106 if (error) 107 fs_err(sdp, "can't lock the freeze glock: %d\n", error); 108 return error; 109 } 110 111 void gfs2_freeze_unlock(struct gfs2_sbd *sdp) 112 { 113 if (gfs2_holder_initialized(&sdp->sd_freeze_gh)) 114 gfs2_glock_dq_uninit(&sdp->sd_freeze_gh); 115 } 116 117 static void do_withdraw(struct gfs2_sbd *sdp) 118 { 119 down_write(&sdp->sd_log_flush_lock); 120 if (!test_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags)) { 121 up_write(&sdp->sd_log_flush_lock); 122 return; 123 } 124 clear_bit(SDF_JOURNAL_LIVE, &sdp->sd_flags); 125 gfs2_ail_drain(sdp); /* frees all transactions */ 126 up_write(&sdp->sd_log_flush_lock); 127 128 wake_up(&sdp->sd_logd_waitq); 129 wake_up(&sdp->sd_quota_wait); 130 131 wait_event_timeout(sdp->sd_log_waitq, 132 gfs2_log_is_empty(sdp), 133 HZ * 5); 134 135 sdp->sd_vfs->s_flags |= SB_RDONLY; 136 137 /* 138 * Dequeue any pending non-system glock holders that can no 139 * longer be granted because the file system is withdrawn. 140 */ 141 gfs2_withdraw_glocks(sdp); 142 } 143 144 void gfs2_lm(struct gfs2_sbd *sdp, const char *fmt, ...) 145 { 146 struct va_format vaf; 147 va_list args; 148 149 if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW && 150 test_bit(SDF_WITHDRAWN, &sdp->sd_flags)) 151 return; 152 153 va_start(args, fmt); 154 vaf.fmt = fmt; 155 vaf.va = &args; 156 fs_err(sdp, "%pV", &vaf); 157 va_end(args); 158 } 159 160 /** 161 * gfs2_offline_uevent - run gfs2_withdraw_helper 162 * @sdp: The GFS2 superblock 163 */ 164 static bool gfs2_offline_uevent(struct gfs2_sbd *sdp) 165 { 166 struct lm_lockstruct *ls = &sdp->sd_lockstruct; 167 long timeout; 168 169 /* Skip protocol "lock_nolock" which doesn't require shared storage. */ 170 if (!ls->ls_ops->lm_lock) 171 return false; 172 173 /* 174 * The gfs2_withdraw_helper replies by writing one of the following 175 * status codes to "/sys$DEVPATH/lock_module/withdraw": 176 * 177 * 0 - The shared block device has been marked inactive. Future write 178 * operations will fail. 179 * 180 * 1 - The shared block device may still be active and carry out 181 * write operations. 182 * 183 * If the "offline" uevent isn't reacted upon in time, the event 184 * handler is assumed to have failed. 185 */ 186 187 sdp->sd_withdraw_helper_status = -1; 188 kobject_uevent(&sdp->sd_kobj, KOBJ_OFFLINE); 189 timeout = gfs2_tune_get(sdp, gt_withdraw_helper_timeout) * HZ; 190 wait_for_completion_timeout(&sdp->sd_withdraw_helper, timeout); 191 if (sdp->sd_withdraw_helper_status == -1) { 192 fs_err(sdp, "%s timed out\n", "gfs2_withdraw_helper"); 193 } else { 194 fs_err(sdp, "%s %s with status %d\n", 195 "gfs2_withdraw_helper", 196 sdp->sd_withdraw_helper_status == 0 ? 197 "succeeded" : "failed", 198 sdp->sd_withdraw_helper_status); 199 } 200 return sdp->sd_withdraw_helper_status == 0; 201 } 202 203 void gfs2_withdraw_func(struct work_struct *work) 204 { 205 struct gfs2_sbd *sdp = container_of(work, struct gfs2_sbd, sd_withdraw_work); 206 struct lm_lockstruct *ls = &sdp->sd_lockstruct; 207 const struct lm_lockops *lm = ls->ls_ops; 208 bool device_inactive; 209 210 if (test_bit(SDF_KILL, &sdp->sd_flags)) 211 return; 212 213 BUG_ON(sdp->sd_args.ar_debug); 214 215 /* 216 * Try to deactivate the shared block device so that no more I/O will 217 * go through. If successful, we can immediately trigger remote 218 * recovery. Otherwise, we must first empty out all our local caches. 219 */ 220 221 device_inactive = gfs2_offline_uevent(sdp); 222 223 if (sdp->sd_args.ar_errors == GFS2_ERRORS_DEACTIVATE && !device_inactive) 224 panic("GFS2: fsid=%s: panic requested\n", sdp->sd_fsname); 225 226 if (lm->lm_unmount) { 227 if (device_inactive) { 228 lm->lm_unmount(sdp, false); 229 do_withdraw(sdp); 230 } else { 231 do_withdraw(sdp); 232 lm->lm_unmount(sdp, false); 233 } 234 } else { 235 do_withdraw(sdp); 236 } 237 238 fs_err(sdp, "file system withdrawn\n"); 239 } 240 241 void gfs2_withdraw(struct gfs2_sbd *sdp) 242 { 243 if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW || 244 sdp->sd_args.ar_errors == GFS2_ERRORS_DEACTIVATE) { 245 if (test_and_set_bit(SDF_WITHDRAWN, &sdp->sd_flags)) 246 return; 247 248 dump_stack(); 249 /* 250 * There is no need to withdraw when the superblock hasn't been 251 * fully initialized, yet. 252 */ 253 if (!(sdp->sd_vfs->s_flags & SB_BORN)) 254 return; 255 fs_err(sdp, "about to withdraw this file system\n"); 256 schedule_work(&sdp->sd_withdraw_work); 257 return; 258 } 259 260 if (sdp->sd_args.ar_errors == GFS2_ERRORS_PANIC) 261 panic("GFS2: fsid=%s: panic requested\n", sdp->sd_fsname); 262 } 263 264 /* 265 * gfs2_assert_withdraw_i - Cause the machine to withdraw if @assertion is false 266 */ 267 268 void gfs2_assert_withdraw_i(struct gfs2_sbd *sdp, char *assertion, 269 const char *function, char *file, unsigned int line) 270 { 271 if (gfs2_withdrawn(sdp)) 272 return; 273 274 fs_err(sdp, 275 "fatal: assertion \"%s\" failed - " 276 "function = %s, file = %s, line = %u\n", 277 assertion, function, file, line); 278 279 gfs2_withdraw(sdp); 280 dump_stack(); 281 } 282 283 /* 284 * gfs2_assert_warn_i - Print a message to the console if @assertion is false 285 */ 286 287 void gfs2_assert_warn_i(struct gfs2_sbd *sdp, char *assertion, 288 const char *function, char *file, unsigned int line) 289 { 290 if (time_before(jiffies, 291 sdp->sd_last_warning + 292 gfs2_tune_get(sdp, gt_complain_secs) * HZ)) 293 return; 294 295 if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW) 296 fs_warn(sdp, "warning: assertion \"%s\" failed - " 297 "function = %s, file = %s, line = %u\n", 298 assertion, function, file, line); 299 300 if (sdp->sd_args.ar_debug) 301 BUG(); 302 else 303 dump_stack(); 304 305 if (sdp->sd_args.ar_errors == GFS2_ERRORS_PANIC) 306 panic("GFS2: fsid=%s: warning: assertion \"%s\" failed - " 307 "function = %s, file = %s, line = %u\n", 308 sdp->sd_fsname, assertion, 309 function, file, line); 310 311 sdp->sd_last_warning = jiffies; 312 } 313 314 /* 315 * gfs2_consist_i - Flag a filesystem consistency error and withdraw 316 */ 317 318 void gfs2_consist_i(struct gfs2_sbd *sdp, const char *function, 319 char *file, unsigned int line) 320 { 321 gfs2_lm(sdp, 322 "fatal: filesystem consistency error - " 323 "function = %s, file = %s, line = %u\n", 324 function, file, line); 325 gfs2_withdraw(sdp); 326 } 327 328 /* 329 * gfs2_consist_inode_i - Flag an inode consistency error and withdraw 330 */ 331 332 void gfs2_consist_inode_i(struct gfs2_inode *ip, 333 const char *function, char *file, unsigned int line) 334 { 335 struct gfs2_glock *gl = gfs2_inode_glock(&ip->i_inode); 336 struct gfs2_sbd *sdp = GFS2_SB(&ip->i_inode); 337 338 gfs2_lm(sdp, 339 "fatal: filesystem consistency error - " 340 "inode = %llu %llu, " 341 "function = %s, file = %s, line = %u\n", 342 (unsigned long long)ip->i_no_formal_ino, 343 (unsigned long long)ip->i_no_addr, 344 function, file, line); 345 gfs2_dump_glock(NULL, gl, 1); 346 gfs2_withdraw(sdp); 347 } 348 349 /* 350 * gfs2_consist_rgrpd_i - Flag a RG consistency error and withdraw 351 */ 352 353 void gfs2_consist_rgrpd_i(struct gfs2_rgrpd *rgd, 354 const char *function, char *file, unsigned int line) 355 { 356 struct gfs2_sbd *sdp = rgd->rd_sbd; 357 char fs_id_buf[sizeof(sdp->sd_fsname) + 7]; 358 359 sprintf(fs_id_buf, "fsid=%s: ", sdp->sd_fsname); 360 gfs2_rgrp_dump(NULL, rgd, fs_id_buf); 361 gfs2_lm(sdp, 362 "fatal: filesystem consistency error - " 363 "RG = %llu, " 364 "function = %s, file = %s, line = %u\n", 365 (unsigned long long)rgd->rd_addr, 366 function, file, line); 367 gfs2_dump_glock(NULL, rgd->rd_gl, 1); 368 gfs2_withdraw(sdp); 369 } 370 371 /* 372 * gfs2_meta_check_ii - Flag a magic number consistency error and withdraw 373 */ 374 375 void gfs2_meta_check_ii(struct gfs2_sbd *sdp, struct buffer_head *bh, 376 const char *function, char *file, 377 unsigned int line) 378 { 379 gfs2_lm(sdp, 380 "fatal: invalid metadata block - " 381 "bh = %llu (bad magic number), " 382 "function = %s, file = %s, line = %u\n", 383 (unsigned long long)bh->b_blocknr, 384 function, file, line); 385 gfs2_withdraw(sdp); 386 } 387 388 /* 389 * gfs2_metatype_check_ii - Flag a metadata type consistency error and withdraw 390 */ 391 392 void gfs2_metatype_check_ii(struct gfs2_sbd *sdp, struct buffer_head *bh, 393 u16 type, u16 t, const char *function, 394 char *file, unsigned int line) 395 { 396 gfs2_lm(sdp, 397 "fatal: invalid metadata block - " 398 "bh = %llu (type: exp=%u, found=%u), " 399 "function = %s, file = %s, line = %u\n", 400 (unsigned long long)bh->b_blocknr, type, t, 401 function, file, line); 402 gfs2_withdraw(sdp); 403 } 404 405 /* 406 * gfs2_io_error_i - Flag an I/O error and withdraw 407 * Returns: -1 if this call withdrew the machine, 408 * 0 if it was already withdrawn 409 */ 410 411 void gfs2_io_error_i(struct gfs2_sbd *sdp, const char *function, char *file, 412 unsigned int line) 413 { 414 gfs2_lm(sdp, 415 "fatal: I/O error - " 416 "function = %s, file = %s, line = %u\n", 417 function, file, line); 418 gfs2_withdraw(sdp); 419 } 420 421 /* 422 * gfs2_io_error_bh_i - Flag a buffer I/O error and withdraw 423 */ 424 425 void gfs2_io_error_bh_i(struct gfs2_sbd *sdp, struct buffer_head *bh, 426 const char *function, char *file, unsigned int line) 427 { 428 if (gfs2_withdrawn(sdp)) 429 return; 430 431 fs_err(sdp, "fatal: I/O error - " 432 "block = %llu, " 433 "function = %s, file = %s, line = %u\n", 434 (unsigned long long)bh->b_blocknr, function, file, line); 435 gfs2_withdraw(sdp); 436 } 437