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