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 gfs2_ail_drain(sdp); /* frees all transactions */ 127 up_write(&sdp->sd_log_flush_lock); 128 129 wake_up(&sdp->sd_logd_waitq); 130 wake_up(&sdp->sd_quota_wait); 131 132 wait_event_timeout(sdp->sd_log_waitq, 133 gfs2_log_is_empty(sdp), 134 HZ * 5); 135 136 sdp->sd_vfs->s_flags |= SB_RDONLY; 137 138 /* 139 * Dequeue any pending non-system glock holders that can no 140 * longer be granted because the file system is withdrawn. 141 */ 142 gfs2_withdraw_glocks(sdp); 143 } 144 145 void gfs2_lm(struct gfs2_sbd *sdp, const char *fmt, ...) 146 { 147 struct va_format vaf; 148 va_list args; 149 150 if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW && 151 test_bit(SDF_WITHDRAWN, &sdp->sd_flags)) 152 return; 153 154 va_start(args, fmt); 155 vaf.fmt = fmt; 156 vaf.va = &args; 157 fs_err(sdp, "%pV", &vaf); 158 va_end(args); 159 } 160 161 /** 162 * gfs2_offline_uevent - run gfs2_withdraw_helper 163 * @sdp: The GFS2 superblock 164 */ 165 static bool gfs2_offline_uevent(struct gfs2_sbd *sdp) 166 { 167 struct lm_lockstruct *ls = &sdp->sd_lockstruct; 168 long timeout; 169 170 /* Skip protocol "lock_nolock" which doesn't require shared storage. */ 171 if (!ls->ls_ops->lm_lock) 172 return false; 173 174 /* 175 * The gfs2_withdraw_helper replies by writing one of the following 176 * status codes to "/sys$DEVPATH/lock_module/withdraw": 177 * 178 * 0 - The shared block device has been marked inactive. Future write 179 * operations will fail. 180 * 181 * 1 - The shared block device may still be active and carry out 182 * write operations. 183 * 184 * If the "offline" uevent isn't reacted upon in time, the event 185 * handler is assumed to have failed. 186 */ 187 188 sdp->sd_withdraw_helper_status = -1; 189 kobject_uevent(&sdp->sd_kobj, KOBJ_OFFLINE); 190 timeout = gfs2_tune_get(sdp, gt_withdraw_helper_timeout) * HZ; 191 wait_for_completion_timeout(&sdp->sd_withdraw_helper, timeout); 192 if (sdp->sd_withdraw_helper_status == -1) { 193 fs_err(sdp, "%s timed out\n", "gfs2_withdraw_helper"); 194 } else { 195 fs_err(sdp, "%s %s with status %d\n", 196 "gfs2_withdraw_helper", 197 sdp->sd_withdraw_helper_status == 0 ? 198 "succeeded" : "failed", 199 sdp->sd_withdraw_helper_status); 200 } 201 return sdp->sd_withdraw_helper_status == 0; 202 } 203 204 void gfs2_withdraw_func(struct work_struct *work) 205 { 206 struct gfs2_sbd *sdp = container_of(work, struct gfs2_sbd, sd_withdraw_work); 207 struct lm_lockstruct *ls = &sdp->sd_lockstruct; 208 const struct lm_lockops *lm = ls->ls_ops; 209 bool device_inactive; 210 211 if (test_bit(SDF_KILL, &sdp->sd_flags)) 212 return; 213 214 BUG_ON(sdp->sd_args.ar_debug); 215 216 /* 217 * Try to deactivate the shared block device so that no more I/O will 218 * go through. If successful, we can immediately trigger remote 219 * recovery. Otherwise, we must first empty out all our local caches. 220 */ 221 222 device_inactive = gfs2_offline_uevent(sdp); 223 224 if (sdp->sd_args.ar_errors == GFS2_ERRORS_DEACTIVATE && !device_inactive) 225 panic("GFS2: fsid=%s: panic requested\n", sdp->sd_fsname); 226 227 if (lm->lm_unmount) { 228 if (device_inactive) { 229 lm->lm_unmount(sdp, false); 230 do_withdraw(sdp); 231 } else { 232 do_withdraw(sdp); 233 lm->lm_unmount(sdp, false); 234 } 235 } else { 236 do_withdraw(sdp); 237 } 238 239 fs_err(sdp, "file system withdrawn\n"); 240 } 241 242 void gfs2_withdraw(struct gfs2_sbd *sdp) 243 { 244 if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW || 245 sdp->sd_args.ar_errors == GFS2_ERRORS_DEACTIVATE) { 246 if (test_and_set_bit(SDF_WITHDRAWN, &sdp->sd_flags)) 247 return; 248 249 dump_stack(); 250 /* 251 * There is no need to withdraw when the superblock hasn't been 252 * fully initialized, yet. 253 */ 254 if (!(sdp->sd_vfs->s_flags & SB_BORN)) 255 return; 256 fs_err(sdp, "about to withdraw this file system\n"); 257 schedule_work(&sdp->sd_withdraw_work); 258 return; 259 } 260 261 if (sdp->sd_args.ar_errors == GFS2_ERRORS_PANIC) 262 panic("GFS2: fsid=%s: panic requested\n", sdp->sd_fsname); 263 } 264 265 /* 266 * gfs2_assert_withdraw_i - Cause the machine to withdraw if @assertion is false 267 */ 268 269 void gfs2_assert_withdraw_i(struct gfs2_sbd *sdp, char *assertion, 270 const char *function, char *file, unsigned int line) 271 { 272 if (gfs2_withdrawn(sdp)) 273 return; 274 275 fs_err(sdp, 276 "fatal: assertion \"%s\" failed - " 277 "function = %s, file = %s, line = %u\n", 278 assertion, function, file, line); 279 280 gfs2_withdraw(sdp); 281 dump_stack(); 282 } 283 284 /* 285 * gfs2_assert_warn_i - Print a message to the console if @assertion is false 286 */ 287 288 void gfs2_assert_warn_i(struct gfs2_sbd *sdp, char *assertion, 289 const char *function, char *file, unsigned int line) 290 { 291 if (time_before(jiffies, 292 sdp->sd_last_warning + 293 gfs2_tune_get(sdp, gt_complain_secs) * HZ)) 294 return; 295 296 if (sdp->sd_args.ar_errors == GFS2_ERRORS_WITHDRAW) 297 fs_warn(sdp, "warning: assertion \"%s\" failed - " 298 "function = %s, file = %s, line = %u\n", 299 assertion, function, file, line); 300 301 if (sdp->sd_args.ar_debug) 302 BUG(); 303 else 304 dump_stack(); 305 306 if (sdp->sd_args.ar_errors == GFS2_ERRORS_PANIC) 307 panic("GFS2: fsid=%s: warning: assertion \"%s\" failed - " 308 "function = %s, file = %s, line = %u\n", 309 sdp->sd_fsname, assertion, 310 function, file, line); 311 312 sdp->sd_last_warning = jiffies; 313 } 314 315 /* 316 * gfs2_consist_i - Flag a filesystem consistency error and withdraw 317 */ 318 319 void gfs2_consist_i(struct gfs2_sbd *sdp, const char *function, 320 char *file, unsigned int line) 321 { 322 gfs2_lm(sdp, 323 "fatal: filesystem consistency error - " 324 "function = %s, file = %s, line = %u\n", 325 function, file, line); 326 gfs2_withdraw(sdp); 327 } 328 329 /* 330 * gfs2_consist_inode_i - Flag an inode consistency error and withdraw 331 */ 332 333 void gfs2_consist_inode_i(struct gfs2_inode *ip, 334 const char *function, char *file, unsigned int line) 335 { 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, ip->i_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