1 // SPDX-License-Identifier: GPL-2.0 1 // SPDX-License-Identifier: GPL-2.0 2 /* 2 /* 3 * Copyright (c) 2000-2005 Silicon Graphics, I 3 * Copyright (c) 2000-2005 Silicon Graphics, Inc. 4 * All Rights Reserved. 4 * All Rights Reserved. 5 */ 5 */ 6 #include "xfs.h" 6 #include "xfs.h" 7 #include "xfs_fs.h" 7 #include "xfs_fs.h" 8 #include "xfs_shared.h" 8 #include "xfs_shared.h" 9 #include "xfs_format.h" 9 #include "xfs_format.h" 10 #include "xfs_log_format.h" 10 #include "xfs_log_format.h" 11 #include "xfs_trans_resv.h" 11 #include "xfs_trans_resv.h" 12 #include "xfs_sb.h" 12 #include "xfs_sb.h" 13 #include "xfs_mount.h" 13 #include "xfs_mount.h" 14 #include "xfs_trans.h" 14 #include "xfs_trans.h" 15 #include "xfs_error.h" 15 #include "xfs_error.h" 16 #include "xfs_alloc.h" 16 #include "xfs_alloc.h" 17 #include "xfs_fsops.h" 17 #include "xfs_fsops.h" 18 #include "xfs_trans_space.h" 18 #include "xfs_trans_space.h" 19 #include "xfs_log.h" 19 #include "xfs_log.h" 20 #include "xfs_log_priv.h" << 21 #include "xfs_ag.h" 20 #include "xfs_ag.h" 22 #include "xfs_ag_resv.h" 21 #include "xfs_ag_resv.h" 23 #include "xfs_trace.h" << 24 << 25 /* << 26 * Write new AG headers to disk. Non-transacti << 27 * written and completed prior to the growfs t << 28 * To do this, we use a delayed write buffer l << 29 * submission and IO completion of the list as << 30 * IO subsystem to merge all the AG headers in << 31 * IO and hide most of the latency of the IO f << 32 * << 33 * This also means that if we get an error whi << 34 * list to write, we can cancel the entire lis << 35 * anything. << 36 */ << 37 static int << 38 xfs_resizefs_init_new_ags( << 39 struct xfs_trans *tp, << 40 struct aghdr_init_data *id, << 41 xfs_agnumber_t oagcount, << 42 xfs_agnumber_t nagcount, << 43 xfs_rfsblock_t delta, << 44 struct xfs_perag *last_pag, << 45 bool *lastag_extend << 46 { << 47 struct xfs_mount *mp = tp->t_mo << 48 xfs_rfsblock_t nb = mp->m_sb. << 49 int error; << 50 << 51 *lastag_extended = false; << 52 << 53 INIT_LIST_HEAD(&id->buffer_list); << 54 for (id->agno = nagcount - 1; << 55 id->agno >= oagcount; << 56 id->agno--, delta -= id->agsize) << 57 << 58 if (id->agno == nagcount - 1) << 59 id->agsize = nb - (id- << 60 (xfs_r << 61 else << 62 id->agsize = mp->m_sb. << 63 << 64 error = xfs_ag_init_headers(mp << 65 if (error) { << 66 xfs_buf_delwri_cancel( << 67 return error; << 68 } << 69 } << 70 << 71 error = xfs_buf_delwri_submit(&id->buf << 72 if (error) << 73 return error; << 74 << 75 if (delta) { << 76 *lastag_extended = true; << 77 error = xfs_ag_extend_space(la << 78 } << 79 return error; << 80 } << 81 22 82 /* 23 /* 83 * growfs operations 24 * growfs operations 84 */ 25 */ 85 static int 26 static int 86 xfs_growfs_data_private( 27 xfs_growfs_data_private( 87 struct xfs_mount *mp, !! 28 xfs_mount_t *mp, /* mount point for filesystem */ 88 struct xfs_growfs_data *in) !! 29 xfs_growfs_data_t *in) /* growfs data input struct */ 89 { 30 { 90 xfs_agnumber_t oagcount = mp- !! 31 xfs_buf_t *bp; 91 struct xfs_buf *bp; << 92 int error; 32 int error; 93 xfs_agnumber_t nagcount; 33 xfs_agnumber_t nagcount; 94 xfs_agnumber_t nagimax = 0; 34 xfs_agnumber_t nagimax = 0; 95 xfs_rfsblock_t nb, nb_div, nb !! 35 xfs_rfsblock_t nb, nb_mod; 96 int64_t delta; !! 36 xfs_rfsblock_t new; 97 bool lastag_extende !! 37 xfs_agnumber_t oagcount; 98 struct xfs_trans *tp; !! 38 xfs_trans_t *tp; 99 struct aghdr_init_data id = {}; 39 struct aghdr_init_data id = {}; 100 struct xfs_perag *last_pag; << 101 40 102 nb = in->newblocks; 41 nb = in->newblocks; 103 error = xfs_sb_validate_fsb_count(&mp- !! 42 if (nb < mp->m_sb.sb_dblocks) 104 if (error) !! 43 return -EINVAL; >> 44 if ((error = xfs_sb_validate_fsb_count(&mp->m_sb, nb))) 105 return error; 45 return error; 106 !! 46 error = xfs_buf_read_uncached(mp->m_ddev_targp, 107 if (nb > mp->m_sb.sb_dblocks) { << 108 error = xfs_buf_read_uncached( << 109 XFS_FSB_TO_BB( 47 XFS_FSB_TO_BB(mp, nb) - XFS_FSS_TO_BB(mp, 1), 110 XFS_FSS_TO_BB( 48 XFS_FSS_TO_BB(mp, 1), 0, &bp, NULL); >> 49 if (error) >> 50 return error; >> 51 xfs_buf_relse(bp); >> 52 >> 53 new = nb; /* use new as a temporary here */ >> 54 nb_mod = do_div(new, mp->m_sb.sb_agblocks); >> 55 nagcount = new + (nb_mod != 0); >> 56 if (nb_mod && nb_mod < XFS_MIN_AG_BLOCKS) { >> 57 nagcount--; >> 58 nb = (xfs_rfsblock_t)nagcount * mp->m_sb.sb_agblocks; >> 59 if (nb < mp->m_sb.sb_dblocks) >> 60 return -EINVAL; >> 61 } >> 62 new = nb - mp->m_sb.sb_dblocks; >> 63 oagcount = mp->m_sb.sb_agcount; >> 64 >> 65 /* allocate the new per-ag structures */ >> 66 if (nagcount > oagcount) { >> 67 error = xfs_initialize_perag(mp, nagcount, &nagimax); 111 if (error) 68 if (error) 112 return error; 69 return error; 113 xfs_buf_relse(bp); << 114 } 70 } 115 71 116 nb_div = nb; !! 72 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata, 117 nb_mod = do_div(nb_div, mp->m_sb.sb_ag !! 73 XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, &tp); 118 if (nb_mod && nb_mod >= XFS_MIN_AG_BLO !! 74 if (error) 119 nb_div++; !! 75 return error; 120 else if (nb_mod) << 121 nb = nb_div * mp->m_sb.sb_agbl << 122 76 123 if (nb_div > XFS_MAX_AGNUMBER + 1) { << 124 nb_div = XFS_MAX_AGNUMBER + 1; << 125 nb = nb_div * mp->m_sb.sb_agbl << 126 } << 127 nagcount = nb_div; << 128 delta = nb - mp->m_sb.sb_dblocks; << 129 /* 77 /* 130 * Reject filesystems with a single AG !! 78 * Write new AG headers to disk. Non-transactional, but need to be 131 * supported, and reject a shrink oper !! 79 * written and completed prior to the growfs transaction being logged. 132 * filesystem to become unsupported. !! 80 * To do this, we use a delayed write buffer list and wait for >> 81 * submission and IO completion of the list as a whole. This allows the >> 82 * IO subsystem to merge all the AG headers in a single AG into a single >> 83 * IO and hide most of the latency of the IO from us. >> 84 * >> 85 * This also means that if we get an error whilst building the buffer >> 86 * list to write, we can cancel the entire list without having written >> 87 * anything. 133 */ 88 */ 134 if (delta < 0 && nagcount < 2) !! 89 INIT_LIST_HEAD(&id.buffer_list); 135 return -EINVAL; !! 90 for (id.agno = nagcount - 1; 136 !! 91 id.agno >= oagcount; 137 /* No work to do */ !! 92 id.agno--, new -= id.agsize) { 138 if (delta == 0) !! 93 139 return 0; !! 94 if (id.agno == nagcount - 1) 140 !! 95 id.agsize = nb - 141 /* TODO: shrinking the entire AGs hasn !! 96 (id.agno * (xfs_rfsblock_t)mp->m_sb.sb_agblocks); 142 if (nagcount < oagcount) !! 97 else 143 return -EINVAL; !! 98 id.agsize = mp->m_sb.sb_agblocks; 144 << 145 /* allocate the new per-ag structures << 146 error = xfs_initialize_perag(mp, oagco << 147 if (error) << 148 return error; << 149 99 150 if (delta > 0) !! 100 error = xfs_ag_init_headers(mp, &id); 151 error = xfs_trans_alloc(mp, &M !! 101 if (error) { 152 XFS_GROWFS_SPA !! 102 xfs_buf_delwri_cancel(&id.buffer_list); 153 &tp); !! 103 goto out_trans_cancel; 154 else !! 104 } 155 error = xfs_trans_alloc(mp, &M !! 105 } 156 0, &tp); !! 106 error = xfs_buf_delwri_submit(&id.buffer_list); 157 if (error) 107 if (error) 158 goto out_free_unused_perag; !! 108 goto out_trans_cancel; 159 109 160 last_pag = xfs_perag_get(mp, oagcount !! 110 xfs_trans_agblocks_delta(tp, id.nfree); 161 if (delta > 0) { << 162 error = xfs_resizefs_init_new_ << 163 delta, last_pa << 164 } else { << 165 xfs_warn_mount(mp, XFS_OPSTATE << 166 "EXPERIMENTAL online shrink feature in << 167 111 168 error = xfs_ag_shrink_space(la !! 112 /* If there are new blocks in the old last AG, extend it. */ >> 113 if (new) { >> 114 error = xfs_ag_extend_space(mp, tp, &id, new); >> 115 if (error) >> 116 goto out_trans_cancel; 169 } 117 } 170 xfs_perag_put(last_pag); << 171 if (error) << 172 goto out_trans_cancel; << 173 118 174 /* 119 /* 175 * Update changed superblock fields tr 120 * Update changed superblock fields transactionally. These are not 176 * seen by the rest of the world until 121 * seen by the rest of the world until the transaction commit applies 177 * them atomically to the superblock. 122 * them atomically to the superblock. 178 */ 123 */ 179 if (nagcount > oagcount) 124 if (nagcount > oagcount) 180 xfs_trans_mod_sb(tp, XFS_TRANS 125 xfs_trans_mod_sb(tp, XFS_TRANS_SB_AGCOUNT, nagcount - oagcount); 181 if (delta) !! 126 if (nb > mp->m_sb.sb_dblocks) 182 xfs_trans_mod_sb(tp, XFS_TRANS !! 127 xfs_trans_mod_sb(tp, XFS_TRANS_SB_DBLOCKS, >> 128 nb - mp->m_sb.sb_dblocks); 183 if (id.nfree) 129 if (id.nfree) 184 xfs_trans_mod_sb(tp, XFS_TRANS 130 xfs_trans_mod_sb(tp, XFS_TRANS_SB_FDBLOCKS, id.nfree); 185 << 186 /* << 187 * Sync sb counters now to reflect the << 188 * particularly important for shrink b << 189 * will fail if sb_fdblocks is ever la << 190 */ << 191 if (xfs_has_lazysbcount(mp)) << 192 xfs_log_sb(tp); << 193 << 194 xfs_trans_set_sync(tp); 131 xfs_trans_set_sync(tp); 195 error = xfs_trans_commit(tp); 132 error = xfs_trans_commit(tp); 196 if (error) 133 if (error) 197 return error; 134 return error; 198 135 199 /* New allocation groups fully initial 136 /* New allocation groups fully initialized, so update mount struct */ 200 if (nagimax) 137 if (nagimax) 201 mp->m_maxagi = nagimax; 138 mp->m_maxagi = nagimax; 202 xfs_set_low_space_thresholds(mp); 139 xfs_set_low_space_thresholds(mp); 203 mp->m_alloc_set_aside = xfs_alloc_set_ 140 mp->m_alloc_set_aside = xfs_alloc_set_aside(mp); 204 141 205 if (delta > 0) { !! 142 /* 206 /* !! 143 * If we expanded the last AG, free the per-AG reservation 207 * If we expanded the last AG, !! 144 * so we can reinitialize it with the new size. 208 * so we can reinitialize it w !! 145 */ 209 */ !! 146 if (new) { 210 if (lastag_extended) { !! 147 struct xfs_perag *pag; 211 struct xfs_perag << 212 148 213 pag = xfs_perag_get(mp !! 149 pag = xfs_perag_get(mp, id.agno); 214 xfs_ag_resv_free(pag); !! 150 error = xfs_ag_resv_free(pag); 215 xfs_perag_put(pag); !! 151 xfs_perag_put(pag); 216 } !! 152 if (error) 217 /* !! 153 return error; 218 * Reserve AG metadata blocks. << 219 * was a growfs failure, just << 220 * new user data after the gro << 221 */ << 222 error = xfs_fs_reserve_ag_bloc << 223 if (error == -ENOSPC) << 224 error = 0; << 225 } 154 } >> 155 >> 156 /* >> 157 * Reserve AG metadata blocks. ENOSPC here does not mean there was a >> 158 * growfs failure, just that there still isn't space for new user data >> 159 * after the grow has been run. >> 160 */ >> 161 error = xfs_fs_reserve_ag_blocks(mp); >> 162 if (error == -ENOSPC) >> 163 error = 0; 226 return error; 164 return error; 227 165 228 out_trans_cancel: 166 out_trans_cancel: 229 xfs_trans_cancel(tp); 167 xfs_trans_cancel(tp); 230 out_free_unused_perag: << 231 if (nagcount > oagcount) << 232 xfs_free_perag_range(mp, oagco << 233 return error; 168 return error; 234 } 169 } 235 170 236 static int 171 static int 237 xfs_growfs_log_private( 172 xfs_growfs_log_private( 238 struct xfs_mount *mp, /* mou !! 173 xfs_mount_t *mp, /* mount point for filesystem */ 239 struct xfs_growfs_log *in) /* gro !! 174 xfs_growfs_log_t *in) /* growfs log input struct */ 240 { 175 { 241 xfs_extlen_t nb; 176 xfs_extlen_t nb; 242 177 243 nb = in->newblocks; 178 nb = in->newblocks; 244 if (nb < XFS_MIN_LOG_BLOCKS || nb < XF 179 if (nb < XFS_MIN_LOG_BLOCKS || nb < XFS_B_TO_FSB(mp, XFS_MIN_LOG_BYTES)) 245 return -EINVAL; 180 return -EINVAL; 246 if (nb == mp->m_sb.sb_logblocks && 181 if (nb == mp->m_sb.sb_logblocks && 247 in->isint == (mp->m_sb.sb_logstart 182 in->isint == (mp->m_sb.sb_logstart != 0)) 248 return -EINVAL; 183 return -EINVAL; 249 /* 184 /* 250 * Moving the log is hard, need new in 185 * Moving the log is hard, need new interfaces to sync 251 * the log first, hold off all activit 186 * the log first, hold off all activity while moving it. 252 * Can have shorter or longer log in t 187 * Can have shorter or longer log in the same space, 253 * or transform internal to external l 188 * or transform internal to external log or vice versa. 254 */ 189 */ 255 return -ENOSYS; 190 return -ENOSYS; 256 } 191 } 257 192 258 static int 193 static int 259 xfs_growfs_imaxpct( 194 xfs_growfs_imaxpct( 260 struct xfs_mount *mp, 195 struct xfs_mount *mp, 261 __u32 imaxpct) 196 __u32 imaxpct) 262 { 197 { 263 struct xfs_trans *tp; 198 struct xfs_trans *tp; 264 int dpct; 199 int dpct; 265 int error; 200 int error; 266 201 267 if (imaxpct > 100) 202 if (imaxpct > 100) 268 return -EINVAL; 203 return -EINVAL; 269 204 270 error = xfs_trans_alloc(mp, &M_RES(mp) 205 error = xfs_trans_alloc(mp, &M_RES(mp)->tr_growdata, 271 XFS_GROWFS_SPACE_RES(m 206 XFS_GROWFS_SPACE_RES(mp), 0, XFS_TRANS_RESERVE, &tp); 272 if (error) 207 if (error) 273 return error; 208 return error; 274 209 275 dpct = imaxpct - mp->m_sb.sb_imax_pct; 210 dpct = imaxpct - mp->m_sb.sb_imax_pct; 276 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAX 211 xfs_trans_mod_sb(tp, XFS_TRANS_SB_IMAXPCT, dpct); 277 xfs_trans_set_sync(tp); 212 xfs_trans_set_sync(tp); 278 return xfs_trans_commit(tp); 213 return xfs_trans_commit(tp); 279 } 214 } 280 215 281 /* 216 /* 282 * protected versions of growfs function acqui 217 * protected versions of growfs function acquire and release locks on the mount 283 * point - exported through ioctls: XFS_IOC_FS 218 * point - exported through ioctls: XFS_IOC_FSGROWFSDATA, XFS_IOC_FSGROWFSLOG, 284 * XFS_IOC_FSGROWFSRT 219 * XFS_IOC_FSGROWFSRT 285 */ 220 */ 286 int 221 int 287 xfs_growfs_data( 222 xfs_growfs_data( 288 struct xfs_mount *mp, 223 struct xfs_mount *mp, 289 struct xfs_growfs_data *in) 224 struct xfs_growfs_data *in) 290 { 225 { 291 int error = 0; 226 int error = 0; 292 227 293 if (!capable(CAP_SYS_ADMIN)) 228 if (!capable(CAP_SYS_ADMIN)) 294 return -EPERM; 229 return -EPERM; 295 if (!mutex_trylock(&mp->m_growlock)) 230 if (!mutex_trylock(&mp->m_growlock)) 296 return -EWOULDBLOCK; 231 return -EWOULDBLOCK; 297 232 298 /* update imaxpct separately to the ph 233 /* update imaxpct separately to the physical grow of the filesystem */ 299 if (in->imaxpct != mp->m_sb.sb_imax_pc 234 if (in->imaxpct != mp->m_sb.sb_imax_pct) { 300 error = xfs_growfs_imaxpct(mp, 235 error = xfs_growfs_imaxpct(mp, in->imaxpct); 301 if (error) 236 if (error) 302 goto out_error; 237 goto out_error; 303 } 238 } 304 239 305 if (in->newblocks != mp->m_sb.sb_dbloc 240 if (in->newblocks != mp->m_sb.sb_dblocks) { 306 error = xfs_growfs_data_privat 241 error = xfs_growfs_data_private(mp, in); 307 if (error) 242 if (error) 308 goto out_error; 243 goto out_error; 309 } 244 } 310 245 311 /* Post growfs calculations needed to 246 /* Post growfs calculations needed to reflect new state in operations */ 312 if (mp->m_sb.sb_imax_pct) { 247 if (mp->m_sb.sb_imax_pct) { 313 uint64_t icount = mp->m_sb.sb_ 248 uint64_t icount = mp->m_sb.sb_dblocks * mp->m_sb.sb_imax_pct; 314 do_div(icount, 100); 249 do_div(icount, 100); 315 M_IGEO(mp)->maxicount = XFS_FS 250 M_IGEO(mp)->maxicount = XFS_FSB_TO_INO(mp, icount); 316 } else 251 } else 317 M_IGEO(mp)->maxicount = 0; 252 M_IGEO(mp)->maxicount = 0; 318 253 319 /* Update secondary superblocks now th 254 /* Update secondary superblocks now the physical grow has completed */ 320 error = xfs_update_secondary_sbs(mp); 255 error = xfs_update_secondary_sbs(mp); 321 256 322 out_error: 257 out_error: 323 /* 258 /* 324 * Increment the generation unconditio 259 * Increment the generation unconditionally, the error could be from 325 * updating the secondary superblocks, 260 * updating the secondary superblocks, in which case the new size 326 * is live already. 261 * is live already. 327 */ 262 */ 328 mp->m_generation++; 263 mp->m_generation++; 329 mutex_unlock(&mp->m_growlock); 264 mutex_unlock(&mp->m_growlock); 330 return error; 265 return error; 331 } 266 } 332 267 333 int 268 int 334 xfs_growfs_log( 269 xfs_growfs_log( 335 xfs_mount_t *mp, 270 xfs_mount_t *mp, 336 struct xfs_growfs_log *in) !! 271 xfs_growfs_log_t *in) 337 { 272 { 338 int error; 273 int error; 339 274 340 if (!capable(CAP_SYS_ADMIN)) 275 if (!capable(CAP_SYS_ADMIN)) 341 return -EPERM; 276 return -EPERM; 342 if (!mutex_trylock(&mp->m_growlock)) 277 if (!mutex_trylock(&mp->m_growlock)) 343 return -EWOULDBLOCK; 278 return -EWOULDBLOCK; 344 error = xfs_growfs_log_private(mp, in) 279 error = xfs_growfs_log_private(mp, in); 345 mutex_unlock(&mp->m_growlock); 280 mutex_unlock(&mp->m_growlock); 346 return error; 281 return error; 347 } 282 } 348 283 349 /* 284 /* >> 285 * exported through ioctl XFS_IOC_FSCOUNTS >> 286 */ >> 287 >> 288 void >> 289 xfs_fs_counts( >> 290 xfs_mount_t *mp, >> 291 xfs_fsop_counts_t *cnt) >> 292 { >> 293 cnt->allocino = percpu_counter_read_positive(&mp->m_icount); >> 294 cnt->freeino = percpu_counter_read_positive(&mp->m_ifree); >> 295 cnt->freedata = percpu_counter_read_positive(&mp->m_fdblocks) - >> 296 mp->m_alloc_set_aside; >> 297 >> 298 spin_lock(&mp->m_sb_lock); >> 299 cnt->freertx = mp->m_sb.sb_frextents; >> 300 spin_unlock(&mp->m_sb_lock); >> 301 } >> 302 >> 303 /* >> 304 * exported through ioctl XFS_IOC_SET_RESBLKS & XFS_IOC_GET_RESBLKS >> 305 * >> 306 * xfs_reserve_blocks is called to set m_resblks >> 307 * in the in-core mount table. The number of unused reserved blocks >> 308 * is kept in m_resblks_avail. >> 309 * 350 * Reserve the requested number of blocks if a 310 * Reserve the requested number of blocks if available. Otherwise return 351 * as many as possible to satisfy the request. 311 * as many as possible to satisfy the request. The actual number 352 * reserved are returned in outval. !! 312 * reserved are returned in outval >> 313 * >> 314 * A null inval pointer indicates that only the current reserved blocks >> 315 * available should be returned no settings are changed. 353 */ 316 */ >> 317 354 int 318 int 355 xfs_reserve_blocks( 319 xfs_reserve_blocks( 356 struct xfs_mount *mp, !! 320 xfs_mount_t *mp, 357 uint64_t request) !! 321 uint64_t *inval, >> 322 xfs_fsop_resblks_t *outval) 358 { 323 { 359 int64_t lcounter, delt 324 int64_t lcounter, delta; 360 int64_t fdblks_delta = 325 int64_t fdblks_delta = 0; >> 326 uint64_t request; 361 int64_t free; 327 int64_t free; 362 int error = 0; 328 int error = 0; 363 329 >> 330 /* If inval is null, report current values and return */ >> 331 if (inval == (uint64_t *)NULL) { >> 332 if (!outval) >> 333 return -EINVAL; >> 334 outval->resblks = mp->m_resblks; >> 335 outval->resblks_avail = mp->m_resblks_avail; >> 336 return 0; >> 337 } >> 338 >> 339 request = *inval; >> 340 364 /* 341 /* 365 * With per-cpu counters, this becomes 342 * With per-cpu counters, this becomes an interesting problem. we need 366 * to work out if we are freeing or al 343 * to work out if we are freeing or allocation blocks first, then we can 367 * do the modification as necessary. 344 * do the modification as necessary. 368 * 345 * 369 * We do this under the m_sb_lock so t 346 * We do this under the m_sb_lock so that if we are near ENOSPC, we will 370 * hold out any changes while we work 347 * hold out any changes while we work out what to do. This means that 371 * the amount of free space can change 348 * the amount of free space can change while we do this, so we need to 372 * retry if we end up trying to reserv 349 * retry if we end up trying to reserve more space than is available. 373 */ 350 */ 374 spin_lock(&mp->m_sb_lock); 351 spin_lock(&mp->m_sb_lock); 375 352 376 /* 353 /* 377 * If our previous reservation was lar 354 * If our previous reservation was larger than the current value, 378 * then move any unused blocks back to 355 * then move any unused blocks back to the free pool. Modify the resblks 379 * counters directly since we shouldn' 356 * counters directly since we shouldn't have any problems unreserving 380 * space. 357 * space. 381 */ 358 */ 382 if (mp->m_resblks > request) { 359 if (mp->m_resblks > request) { 383 lcounter = mp->m_resblks_avail 360 lcounter = mp->m_resblks_avail - request; 384 if (lcounter > 0) { !! 361 if (lcounter > 0) { /* release unused blocks */ 385 fdblks_delta = lcounte 362 fdblks_delta = lcounter; 386 mp->m_resblks_avail -= 363 mp->m_resblks_avail -= lcounter; 387 } 364 } 388 mp->m_resblks = request; 365 mp->m_resblks = request; 389 if (fdblks_delta) { 366 if (fdblks_delta) { 390 spin_unlock(&mp->m_sb_ 367 spin_unlock(&mp->m_sb_lock); 391 xfs_add_fdblocks(mp, f !! 368 error = xfs_mod_fdblocks(mp, fdblks_delta, 0); 392 spin_lock(&mp->m_sb_lo 369 spin_lock(&mp->m_sb_lock); 393 } 370 } 394 371 395 goto out; 372 goto out; 396 } 373 } 397 374 398 /* 375 /* 399 * If the request is larger than the c 376 * If the request is larger than the current reservation, reserve the 400 * blocks before we update the reserve 377 * blocks before we update the reserve counters. Sample m_fdblocks and 401 * perform a partial reservation if th 378 * perform a partial reservation if the request exceeds free space. 402 * !! 379 */ 403 * The code below estimates how many b !! 380 error = -ENOSPC; 404 * fdblocks to stash in the reserve po !! 381 do { 405 * race since fdblocks updates are not !! 382 free = percpu_counter_sum(&mp->m_fdblocks) - 406 * m_sb_lock. Set the reserve size ev !! 383 mp->m_alloc_set_aside; 407 * space to fill it because mod_fdbloc !! 384 if (free <= 0) 408 * reserve when it can. !! 385 break; 409 */ !! 386 410 free = percpu_counter_sum(&mp->m_fdblo !! 387 delta = request - mp->m_resblks; 411 !! 388 lcounter = free - delta; 412 delta = request - mp->m_resblks; !! 389 if (lcounter < 0) 413 mp->m_resblks = request; !! 390 /* We can't satisfy the request, just get what we can */ 414 if (delta > 0 && free > 0) { !! 391 fdblks_delta = free; >> 392 else >> 393 fdblks_delta = delta; >> 394 415 /* 395 /* 416 * We'll either succeed in get 396 * We'll either succeed in getting space from the free block 417 * count or we'll get an ENOSP !! 397 * count or we'll get an ENOSPC. If we get a ENOSPC, it means 418 * here - we don't want to res !! 398 * things changed while we were calculating fdblks_delta and so 419 * from the reserve. !! 399 * we should try again to see if there is anything left to >> 400 * reserve. 420 * 401 * 421 * The desired reserve size ca !! 402 * Don't set the reserved flag here - we don't want to reserve 422 * Use mod_fdblocks to put the !! 403 * the extra reserve blocks from the reserve..... 423 * fdblocks as appropriate. << 424 */ 404 */ 425 fdblks_delta = min(free, delta << 426 spin_unlock(&mp->m_sb_lock); 405 spin_unlock(&mp->m_sb_lock); 427 error = xfs_dec_fdblocks(mp, f !! 406 error = xfs_mod_fdblocks(mp, -fdblks_delta, 0); 428 if (!error) << 429 xfs_add_fdblocks(mp, f << 430 spin_lock(&mp->m_sb_lock); 407 spin_lock(&mp->m_sb_lock); >> 408 } while (error == -ENOSPC); >> 409 >> 410 /* >> 411 * Update the reserve counters if blocks have been successfully >> 412 * allocated. >> 413 */ >> 414 if (!error && fdblks_delta) { >> 415 mp->m_resblks += fdblks_delta; >> 416 mp->m_resblks_avail += fdblks_delta; 431 } 417 } >> 418 432 out: 419 out: >> 420 if (outval) { >> 421 outval->resblks = mp->m_resblks; >> 422 outval->resblks_avail = mp->m_resblks_avail; >> 423 } >> 424 433 spin_unlock(&mp->m_sb_lock); 425 spin_unlock(&mp->m_sb_lock); 434 return error; 426 return error; 435 } 427 } 436 428 437 int 429 int 438 xfs_fs_goingdown( 430 xfs_fs_goingdown( 439 xfs_mount_t *mp, 431 xfs_mount_t *mp, 440 uint32_t inflags) 432 uint32_t inflags) 441 { 433 { 442 switch (inflags) { 434 switch (inflags) { 443 case XFS_FSOP_GOING_FLAGS_DEFAULT: { 435 case XFS_FSOP_GOING_FLAGS_DEFAULT: { 444 if (!bdev_freeze(mp->m_super-> !! 436 struct super_block *sb = freeze_bdev(mp->m_super->s_bdev); >> 437 >> 438 if (sb && !IS_ERR(sb)) { 445 xfs_force_shutdown(mp, 439 xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT); 446 bdev_thaw(mp->m_super- !! 440 thaw_bdev(sb->s_bdev, sb); 447 } 441 } >> 442 448 break; 443 break; 449 } 444 } 450 case XFS_FSOP_GOING_FLAGS_LOGFLUSH: 445 case XFS_FSOP_GOING_FLAGS_LOGFLUSH: 451 xfs_force_shutdown(mp, SHUTDOW 446 xfs_force_shutdown(mp, SHUTDOWN_FORCE_UMOUNT); 452 break; 447 break; 453 case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH: 448 case XFS_FSOP_GOING_FLAGS_NOLOGFLUSH: 454 xfs_force_shutdown(mp, 449 xfs_force_shutdown(mp, 455 SHUTDOWN_FORCE 450 SHUTDOWN_FORCE_UMOUNT | SHUTDOWN_LOG_IO_ERROR); 456 break; 451 break; 457 default: 452 default: 458 return -EINVAL; 453 return -EINVAL; 459 } 454 } 460 455 461 return 0; 456 return 0; 462 } 457 } 463 458 464 /* 459 /* 465 * Force a shutdown of the filesystem instantl 460 * Force a shutdown of the filesystem instantly while keeping the filesystem 466 * consistent. We don't do an unmount here; ju 461 * consistent. We don't do an unmount here; just shutdown the shop, make sure 467 * that absolutely nothing persistent happens 462 * that absolutely nothing persistent happens to this filesystem after this 468 * point. 463 * point. 469 * << 470 * The shutdown state change is atomic, result << 471 * first shutdown call processing the shutdown << 472 * log once as it requires, and we don't spam << 473 * shutdowns race to set the shutdown flags. << 474 */ 464 */ 475 void 465 void 476 xfs_do_force_shutdown( 466 xfs_do_force_shutdown( 477 struct xfs_mount *mp, 467 struct xfs_mount *mp, 478 uint32_t flags, !! 468 int flags, 479 char *fname, 469 char *fname, 480 int lnnum) 470 int lnnum) 481 { 471 { 482 int tag; !! 472 bool logerror = flags & SHUTDOWN_LOG_IO_ERROR; 483 const char *why; << 484 473 >> 474 /* >> 475 * No need to duplicate efforts. >> 476 */ >> 477 if (XFS_FORCED_SHUTDOWN(mp) && !logerror) >> 478 return; 485 479 486 if (xfs_set_shutdown(mp)) { !! 480 /* 487 xlog_shutdown_wait(mp->m_log); !! 481 * This flags XFS_MOUNT_FS_SHUTDOWN, makes sure that we don't >> 482 * queue up anybody new on the log reservations, and wakes up >> 483 * everybody who's sleeping on log reservations to tell them >> 484 * the bad news. >> 485 */ >> 486 if (xfs_log_force_umount(mp, logerror)) >> 487 return; >> 488 >> 489 if (flags & SHUTDOWN_FORCE_UMOUNT) { >> 490 xfs_alert(mp, >> 491 "User initiated shutdown received. Shutting down filesystem"); 488 return; 492 return; 489 } 493 } 490 if (mp->m_sb_bp) << 491 mp->m_sb_bp->b_flags |= XBF_DO << 492 494 493 if (flags & SHUTDOWN_FORCE_UMOUNT) !! 495 xfs_notice(mp, 494 xfs_alert(mp, "User initiated !! 496 "%s(0x%x) called from line %d of file %s. Return address = "PTR_FMT, >> 497 __func__, flags, lnnum, fname, __return_address); >> 498 >> 499 if (flags & SHUTDOWN_CORRUPT_INCORE) { >> 500 xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_CORRUPT, >> 501 "Corruption of in-memory data detected. Shutting down filesystem"); >> 502 if (XFS_ERRLEVEL_HIGH <= xfs_error_level) >> 503 xfs_stack_trace(); >> 504 } else if (logerror) { >> 505 xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_LOGERROR, >> 506 "Log I/O Error Detected. Shutting down filesystem"); >> 507 } else if (flags & SHUTDOWN_DEVICE_REQ) { >> 508 xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR, >> 509 "All device paths lost. Shutting down filesystem"); >> 510 } else if (!(flags & SHUTDOWN_REMOTE_REQ)) { >> 511 xfs_alert_tag(mp, XFS_PTAG_SHUTDOWN_IOERROR, >> 512 "I/O Error Detected. Shutting down filesystem"); >> 513 } 495 514 496 if (xlog_force_shutdown(mp->m_log, fla << 497 tag = XFS_PTAG_SHUTDOWN_LOGERR << 498 why = "Log I/O Error"; << 499 } else if (flags & SHUTDOWN_CORRUPT_IN << 500 tag = XFS_PTAG_SHUTDOWN_CORRUP << 501 why = "Corruption of in-memory << 502 } else if (flags & SHUTDOWN_CORRUPT_ON << 503 tag = XFS_PTAG_SHUTDOWN_CORRUP << 504 why = "Corruption of on-disk m << 505 } else if (flags & SHUTDOWN_DEVICE_REM << 506 tag = XFS_PTAG_SHUTDOWN_IOERRO << 507 why = "Block device removal"; << 508 } else { << 509 tag = XFS_PTAG_SHUTDOWN_IOERRO << 510 why = "Metadata I/O Error"; << 511 } << 512 << 513 trace_xfs_force_shutdown(mp, tag, flag << 514 << 515 xfs_alert_tag(mp, tag, << 516 "%s (0x%x) detected at %pS (%s:%d). Shutting << 517 why, flags, __return_a << 518 xfs_alert(mp, 515 xfs_alert(mp, 519 "Please unmount the filesystem 516 "Please unmount the filesystem and rectify the problem(s)"); 520 if (xfs_error_level >= XFS_ERRLEVEL_HI << 521 xfs_stack_trace(); << 522 } 517 } 523 518 524 /* 519 /* 525 * Reserve free space for per-AG metadata. 520 * Reserve free space for per-AG metadata. 526 */ 521 */ 527 int 522 int 528 xfs_fs_reserve_ag_blocks( 523 xfs_fs_reserve_ag_blocks( 529 struct xfs_mount *mp) 524 struct xfs_mount *mp) 530 { 525 { 531 xfs_agnumber_t agno; 526 xfs_agnumber_t agno; 532 struct xfs_perag *pag; 527 struct xfs_perag *pag; 533 int error = 0; 528 int error = 0; 534 int err2; 529 int err2; 535 530 536 mp->m_finobt_nores = false; 531 mp->m_finobt_nores = false; 537 for_each_perag(mp, agno, pag) { !! 532 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) { >> 533 pag = xfs_perag_get(mp, agno); 538 err2 = xfs_ag_resv_init(pag, N 534 err2 = xfs_ag_resv_init(pag, NULL); >> 535 xfs_perag_put(pag); 539 if (err2 && !error) 536 if (err2 && !error) 540 error = err2; 537 error = err2; 541 } 538 } 542 539 543 if (error && error != -ENOSPC) { 540 if (error && error != -ENOSPC) { 544 xfs_warn(mp, 541 xfs_warn(mp, 545 "Error %d reserving per-AG metadata re 542 "Error %d reserving per-AG metadata reserve pool.", error); 546 xfs_force_shutdown(mp, SHUTDOW 543 xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); 547 } 544 } 548 545 549 return error; 546 return error; 550 } 547 } 551 548 552 /* 549 /* 553 * Free space reserved for per-AG metadata. 550 * Free space reserved for per-AG metadata. 554 */ 551 */ 555 void !! 552 int 556 xfs_fs_unreserve_ag_blocks( 553 xfs_fs_unreserve_ag_blocks( 557 struct xfs_mount *mp) 554 struct xfs_mount *mp) 558 { 555 { 559 xfs_agnumber_t agno; 556 xfs_agnumber_t agno; 560 struct xfs_perag *pag; 557 struct xfs_perag *pag; >> 558 int error = 0; >> 559 int err2; 561 560 562 for_each_perag(mp, agno, pag) !! 561 for (agno = 0; agno < mp->m_sb.sb_agcount; agno++) { 563 xfs_ag_resv_free(pag); !! 562 pag = xfs_perag_get(mp, agno); >> 563 err2 = xfs_ag_resv_free(pag); >> 564 xfs_perag_put(pag); >> 565 if (err2 && !error) >> 566 error = err2; >> 567 } >> 568 >> 569 if (error) >> 570 xfs_warn(mp, >> 571 "Error %d freeing per-AG metadata reserve pool.", error); >> 572 >> 573 return error; 564 } 574 } 565 575
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.