Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2000-2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
9 | * This program is distributed in the hope that it would be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
1da177e4 | 13 | * |
7b718769 NS |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write the Free Software Foundation, | |
16 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
1da177e4 | 17 | */ |
1da177e4 | 18 | #include "xfs.h" |
a844f451 | 19 | #include "xfs_fs.h" |
1da177e4 | 20 | #include "xfs_types.h" |
a844f451 | 21 | #include "xfs_bit.h" |
1da177e4 | 22 | #include "xfs_log.h" |
a844f451 | 23 | #include "xfs_inum.h" |
1da177e4 | 24 | #include "xfs_trans.h" |
a844f451 NS |
25 | #include "xfs_sb.h" |
26 | #include "xfs_ag.h" | |
a844f451 | 27 | #include "xfs_dir2.h" |
1da177e4 LT |
28 | #include "xfs_dmapi.h" |
29 | #include "xfs_mount.h" | |
30 | #include "xfs_error.h" | |
31 | #include "xfs_log_priv.h" | |
32 | #include "xfs_buf_item.h" | |
a844f451 | 33 | #include "xfs_bmap_btree.h" |
1da177e4 | 34 | #include "xfs_alloc_btree.h" |
a844f451 | 35 | #include "xfs_ialloc_btree.h" |
1da177e4 | 36 | #include "xfs_log_recover.h" |
1da177e4 | 37 | #include "xfs_trans_priv.h" |
a844f451 NS |
38 | #include "xfs_dir2_sf.h" |
39 | #include "xfs_attr_sf.h" | |
40 | #include "xfs_dinode.h" | |
41 | #include "xfs_inode.h" | |
42 | #include "xfs_rw.h" | |
0b1b213f | 43 | #include "xfs_trace.h" |
1da177e4 | 44 | |
eb01c9cd | 45 | kmem_zone_t *xfs_log_ticket_zone; |
1da177e4 LT |
46 | |
47 | #define xlog_write_adv_cnt(ptr, len, off, bytes) \ | |
48 | { (ptr) += (bytes); \ | |
49 | (len) -= (bytes); \ | |
50 | (off) += (bytes);} | |
51 | ||
52 | /* Local miscellaneous function prototypes */ | |
1da177e4 LT |
53 | STATIC int xlog_commit_record(xfs_mount_t *mp, xlog_ticket_t *ticket, |
54 | xlog_in_core_t **, xfs_lsn_t *); | |
55 | STATIC xlog_t * xlog_alloc_log(xfs_mount_t *mp, | |
56 | xfs_buftarg_t *log_target, | |
57 | xfs_daddr_t blk_offset, | |
58 | int num_bblks); | |
59 | STATIC int xlog_space_left(xlog_t *log, int cycle, int bytes); | |
60 | STATIC int xlog_sync(xlog_t *log, xlog_in_core_t *iclog); | |
c41564b5 | 61 | STATIC void xlog_dealloc_log(xlog_t *log); |
1da177e4 | 62 | STATIC int xlog_write(xfs_mount_t *mp, xfs_log_iovec_t region[], |
35a8a72f | 63 | int nentries, struct xlog_ticket *tic, |
1da177e4 LT |
64 | xfs_lsn_t *start_lsn, |
65 | xlog_in_core_t **commit_iclog, | |
66 | uint flags); | |
67 | ||
68 | /* local state machine functions */ | |
69 | STATIC void xlog_state_done_syncing(xlog_in_core_t *iclog, int); | |
70 | STATIC void xlog_state_do_callback(xlog_t *log,int aborted, xlog_in_core_t *iclog); | |
71 | STATIC int xlog_state_get_iclog_space(xlog_t *log, | |
72 | int len, | |
73 | xlog_in_core_t **iclog, | |
74 | xlog_ticket_t *ticket, | |
75 | int *continued_write, | |
76 | int *logoffsetp); | |
1da177e4 LT |
77 | STATIC int xlog_state_release_iclog(xlog_t *log, |
78 | xlog_in_core_t *iclog); | |
79 | STATIC void xlog_state_switch_iclogs(xlog_t *log, | |
80 | xlog_in_core_t *iclog, | |
81 | int eventual_size); | |
1da177e4 LT |
82 | STATIC void xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog); |
83 | ||
84 | /* local functions to manipulate grant head */ | |
85 | STATIC int xlog_grant_log_space(xlog_t *log, | |
86 | xlog_ticket_t *xtic); | |
87 | STATIC void xlog_grant_push_ail(xfs_mount_t *mp, | |
88 | int need_bytes); | |
89 | STATIC void xlog_regrant_reserve_log_space(xlog_t *log, | |
90 | xlog_ticket_t *ticket); | |
91 | STATIC int xlog_regrant_write_log_space(xlog_t *log, | |
92 | xlog_ticket_t *ticket); | |
93 | STATIC void xlog_ungrant_log_space(xlog_t *log, | |
94 | xlog_ticket_t *ticket); | |
95 | ||
96 | ||
97 | /* local ticket functions */ | |
cc09c0dc | 98 | STATIC xlog_ticket_t *xlog_ticket_alloc(xlog_t *log, |
1da177e4 LT |
99 | int unit_bytes, |
100 | int count, | |
101 | char clientid, | |
102 | uint flags); | |
1da177e4 | 103 | |
cfcbbbd0 | 104 | #if defined(DEBUG) |
1da177e4 LT |
105 | STATIC void xlog_verify_dest_ptr(xlog_t *log, __psint_t ptr); |
106 | STATIC void xlog_verify_grant_head(xlog_t *log, int equals); | |
107 | STATIC void xlog_verify_iclog(xlog_t *log, xlog_in_core_t *iclog, | |
108 | int count, boolean_t syncing); | |
109 | STATIC void xlog_verify_tail_lsn(xlog_t *log, xlog_in_core_t *iclog, | |
110 | xfs_lsn_t tail_lsn); | |
111 | #else | |
112 | #define xlog_verify_dest_ptr(a,b) | |
113 | #define xlog_verify_grant_head(a,b) | |
114 | #define xlog_verify_iclog(a,b,c,d) | |
115 | #define xlog_verify_tail_lsn(a,b,c) | |
116 | #endif | |
117 | ||
ba0f32d4 | 118 | STATIC int xlog_iclogs_empty(xlog_t *log); |
1da177e4 | 119 | |
dd954c69 CH |
120 | |
121 | static void | |
122 | xlog_ins_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic) | |
123 | { | |
124 | if (*qp) { | |
125 | tic->t_next = (*qp); | |
126 | tic->t_prev = (*qp)->t_prev; | |
127 | (*qp)->t_prev->t_next = tic; | |
128 | (*qp)->t_prev = tic; | |
129 | } else { | |
130 | tic->t_prev = tic->t_next = tic; | |
131 | *qp = tic; | |
132 | } | |
133 | ||
134 | tic->t_flags |= XLOG_TIC_IN_Q; | |
135 | } | |
136 | ||
137 | static void | |
138 | xlog_del_ticketq(struct xlog_ticket **qp, struct xlog_ticket *tic) | |
139 | { | |
140 | if (tic == tic->t_next) { | |
141 | *qp = NULL; | |
142 | } else { | |
143 | *qp = tic->t_next; | |
144 | tic->t_next->t_prev = tic->t_prev; | |
145 | tic->t_prev->t_next = tic->t_next; | |
146 | } | |
147 | ||
148 | tic->t_next = tic->t_prev = NULL; | |
149 | tic->t_flags &= ~XLOG_TIC_IN_Q; | |
150 | } | |
151 | ||
152 | static void | |
153 | xlog_grant_sub_space(struct log *log, int bytes) | |
154 | { | |
155 | log->l_grant_write_bytes -= bytes; | |
156 | if (log->l_grant_write_bytes < 0) { | |
157 | log->l_grant_write_bytes += log->l_logsize; | |
158 | log->l_grant_write_cycle--; | |
159 | } | |
160 | ||
161 | log->l_grant_reserve_bytes -= bytes; | |
162 | if ((log)->l_grant_reserve_bytes < 0) { | |
163 | log->l_grant_reserve_bytes += log->l_logsize; | |
164 | log->l_grant_reserve_cycle--; | |
165 | } | |
166 | ||
167 | } | |
168 | ||
169 | static void | |
170 | xlog_grant_add_space_write(struct log *log, int bytes) | |
171 | { | |
d729eae8 MN |
172 | int tmp = log->l_logsize - log->l_grant_write_bytes; |
173 | if (tmp > bytes) | |
174 | log->l_grant_write_bytes += bytes; | |
175 | else { | |
dd954c69 | 176 | log->l_grant_write_cycle++; |
d729eae8 | 177 | log->l_grant_write_bytes = bytes - tmp; |
dd954c69 CH |
178 | } |
179 | } | |
180 | ||
181 | static void | |
182 | xlog_grant_add_space_reserve(struct log *log, int bytes) | |
183 | { | |
d729eae8 MN |
184 | int tmp = log->l_logsize - log->l_grant_reserve_bytes; |
185 | if (tmp > bytes) | |
186 | log->l_grant_reserve_bytes += bytes; | |
187 | else { | |
dd954c69 | 188 | log->l_grant_reserve_cycle++; |
d729eae8 | 189 | log->l_grant_reserve_bytes = bytes - tmp; |
dd954c69 CH |
190 | } |
191 | } | |
192 | ||
193 | static inline void | |
194 | xlog_grant_add_space(struct log *log, int bytes) | |
195 | { | |
196 | xlog_grant_add_space_write(log, bytes); | |
197 | xlog_grant_add_space_reserve(log, bytes); | |
198 | } | |
199 | ||
0adba536 CH |
200 | static void |
201 | xlog_tic_reset_res(xlog_ticket_t *tic) | |
202 | { | |
203 | tic->t_res_num = 0; | |
204 | tic->t_res_arr_sum = 0; | |
205 | tic->t_res_num_ophdrs = 0; | |
206 | } | |
207 | ||
208 | static void | |
209 | xlog_tic_add_region(xlog_ticket_t *tic, uint len, uint type) | |
210 | { | |
211 | if (tic->t_res_num == XLOG_TIC_LEN_MAX) { | |
212 | /* add to overflow and start again */ | |
213 | tic->t_res_o_flow += tic->t_res_arr_sum; | |
214 | tic->t_res_num = 0; | |
215 | tic->t_res_arr_sum = 0; | |
216 | } | |
217 | ||
218 | tic->t_res_arr[tic->t_res_num].r_len = len; | |
219 | tic->t_res_arr[tic->t_res_num].r_type = type; | |
220 | tic->t_res_arr_sum += len; | |
221 | tic->t_res_num++; | |
222 | } | |
dd954c69 | 223 | |
1da177e4 LT |
224 | /* |
225 | * NOTES: | |
226 | * | |
227 | * 1. currblock field gets updated at startup and after in-core logs | |
228 | * marked as with WANT_SYNC. | |
229 | */ | |
230 | ||
231 | /* | |
232 | * This routine is called when a user of a log manager ticket is done with | |
233 | * the reservation. If the ticket was ever used, then a commit record for | |
234 | * the associated transaction is written out as a log operation header with | |
235 | * no data. The flag XLOG_TIC_INITED is set when the first write occurs with | |
236 | * a given ticket. If the ticket was one with a permanent reservation, then | |
237 | * a few operations are done differently. Permanent reservation tickets by | |
238 | * default don't release the reservation. They just commit the current | |
239 | * transaction with the belief that the reservation is still needed. A flag | |
240 | * must be passed in before permanent reservations are actually released. | |
241 | * When these type of tickets are not released, they need to be set into | |
242 | * the inited state again. By doing this, a start record will be written | |
243 | * out when the next write occurs. | |
244 | */ | |
245 | xfs_lsn_t | |
35a8a72f CH |
246 | xfs_log_done( |
247 | struct xfs_mount *mp, | |
248 | struct xlog_ticket *ticket, | |
249 | struct xlog_in_core **iclog, | |
250 | uint flags) | |
1da177e4 | 251 | { |
35a8a72f CH |
252 | struct log *log = mp->m_log; |
253 | xfs_lsn_t lsn = 0; | |
1da177e4 | 254 | |
1da177e4 LT |
255 | if (XLOG_FORCED_SHUTDOWN(log) || |
256 | /* | |
257 | * If nothing was ever written, don't write out commit record. | |
258 | * If we get an error, just continue and give back the log ticket. | |
259 | */ | |
260 | (((ticket->t_flags & XLOG_TIC_INITED) == 0) && | |
35a8a72f | 261 | (xlog_commit_record(mp, ticket, iclog, &lsn)))) { |
1da177e4 LT |
262 | lsn = (xfs_lsn_t) -1; |
263 | if (ticket->t_flags & XLOG_TIC_PERM_RESERV) { | |
264 | flags |= XFS_LOG_REL_PERM_RESERV; | |
265 | } | |
266 | } | |
267 | ||
268 | ||
269 | if ((ticket->t_flags & XLOG_TIC_PERM_RESERV) == 0 || | |
270 | (flags & XFS_LOG_REL_PERM_RESERV)) { | |
0b1b213f CH |
271 | trace_xfs_log_done_nonperm(log, ticket); |
272 | ||
1da177e4 | 273 | /* |
c41564b5 | 274 | * Release ticket if not permanent reservation or a specific |
1da177e4 LT |
275 | * request has been made to release a permanent reservation. |
276 | */ | |
277 | xlog_ungrant_log_space(log, ticket); | |
cc09c0dc | 278 | xfs_log_ticket_put(ticket); |
1da177e4 | 279 | } else { |
0b1b213f CH |
280 | trace_xfs_log_done_perm(log, ticket); |
281 | ||
1da177e4 | 282 | xlog_regrant_reserve_log_space(log, ticket); |
c6a7b0f8 LM |
283 | /* If this ticket was a permanent reservation and we aren't |
284 | * trying to release it, reset the inited flags; so next time | |
285 | * we write, a start record will be written out. | |
286 | */ | |
1da177e4 | 287 | ticket->t_flags |= XLOG_TIC_INITED; |
c6a7b0f8 | 288 | } |
1da177e4 LT |
289 | |
290 | return lsn; | |
35a8a72f | 291 | } |
1da177e4 | 292 | |
1da177e4 LT |
293 | /* |
294 | * Attaches a new iclog I/O completion callback routine during | |
295 | * transaction commit. If the log is in error state, a non-zero | |
296 | * return code is handed back and the caller is responsible for | |
297 | * executing the callback at an appropriate time. | |
298 | */ | |
299 | int | |
35a8a72f CH |
300 | xfs_log_notify( |
301 | struct xfs_mount *mp, | |
302 | struct xlog_in_core *iclog, | |
303 | xfs_log_callback_t *cb) | |
1da177e4 | 304 | { |
b22cd72c | 305 | int abortflg; |
1da177e4 | 306 | |
114d23aa | 307 | spin_lock(&iclog->ic_callback_lock); |
1da177e4 LT |
308 | abortflg = (iclog->ic_state & XLOG_STATE_IOERROR); |
309 | if (!abortflg) { | |
310 | ASSERT_ALWAYS((iclog->ic_state == XLOG_STATE_ACTIVE) || | |
311 | (iclog->ic_state == XLOG_STATE_WANT_SYNC)); | |
312 | cb->cb_next = NULL; | |
313 | *(iclog->ic_callback_tail) = cb; | |
314 | iclog->ic_callback_tail = &(cb->cb_next); | |
315 | } | |
114d23aa | 316 | spin_unlock(&iclog->ic_callback_lock); |
1da177e4 | 317 | return abortflg; |
35a8a72f | 318 | } |
1da177e4 LT |
319 | |
320 | int | |
35a8a72f CH |
321 | xfs_log_release_iclog( |
322 | struct xfs_mount *mp, | |
323 | struct xlog_in_core *iclog) | |
1da177e4 | 324 | { |
35a8a72f | 325 | if (xlog_state_release_iclog(mp->m_log, iclog)) { |
7d04a335 | 326 | xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR); |
014c2544 | 327 | return EIO; |
1da177e4 LT |
328 | } |
329 | ||
330 | return 0; | |
331 | } | |
332 | ||
333 | /* | |
334 | * 1. Reserve an amount of on-disk log space and return a ticket corresponding | |
335 | * to the reservation. | |
336 | * 2. Potentially, push buffers at tail of log to disk. | |
337 | * | |
338 | * Each reservation is going to reserve extra space for a log record header. | |
339 | * When writes happen to the on-disk log, we don't subtract the length of the | |
340 | * log record header from any reservation. By wasting space in each | |
341 | * reservation, we prevent over allocation problems. | |
342 | */ | |
343 | int | |
35a8a72f CH |
344 | xfs_log_reserve( |
345 | struct xfs_mount *mp, | |
346 | int unit_bytes, | |
347 | int cnt, | |
348 | struct xlog_ticket **ticket, | |
349 | __uint8_t client, | |
350 | uint flags, | |
351 | uint t_type) | |
1da177e4 | 352 | { |
35a8a72f CH |
353 | struct log *log = mp->m_log; |
354 | struct xlog_ticket *internal_ticket; | |
355 | int retval = 0; | |
1da177e4 | 356 | |
1da177e4 LT |
357 | ASSERT(client == XFS_TRANSACTION || client == XFS_LOG); |
358 | ASSERT((flags & XFS_LOG_NOSLEEP) == 0); | |
359 | ||
360 | if (XLOG_FORCED_SHUTDOWN(log)) | |
361 | return XFS_ERROR(EIO); | |
362 | ||
363 | XFS_STATS_INC(xs_try_logspace); | |
364 | ||
0b1b213f | 365 | |
1da177e4 LT |
366 | if (*ticket != NULL) { |
367 | ASSERT(flags & XFS_LOG_PERM_RESERV); | |
35a8a72f | 368 | internal_ticket = *ticket; |
0b1b213f CH |
369 | |
370 | trace_xfs_log_reserve(log, internal_ticket); | |
371 | ||
1da177e4 LT |
372 | xlog_grant_push_ail(mp, internal_ticket->t_unit_res); |
373 | retval = xlog_regrant_write_log_space(log, internal_ticket); | |
374 | } else { | |
375 | /* may sleep if need to allocate more tickets */ | |
cc09c0dc | 376 | internal_ticket = xlog_ticket_alloc(log, unit_bytes, cnt, |
1da177e4 | 377 | client, flags); |
eb01c9cd DC |
378 | if (!internal_ticket) |
379 | return XFS_ERROR(ENOMEM); | |
7e9c6396 | 380 | internal_ticket->t_trans_type = t_type; |
1da177e4 | 381 | *ticket = internal_ticket; |
0b1b213f CH |
382 | |
383 | trace_xfs_log_reserve(log, internal_ticket); | |
384 | ||
1da177e4 LT |
385 | xlog_grant_push_ail(mp, |
386 | (internal_ticket->t_unit_res * | |
387 | internal_ticket->t_cnt)); | |
388 | retval = xlog_grant_log_space(log, internal_ticket); | |
389 | } | |
390 | ||
391 | return retval; | |
392 | } /* xfs_log_reserve */ | |
393 | ||
394 | ||
395 | /* | |
396 | * Mount a log filesystem | |
397 | * | |
398 | * mp - ubiquitous xfs mount point structure | |
399 | * log_target - buftarg of on-disk log device | |
400 | * blk_offset - Start block # where block size is 512 bytes (BBSIZE) | |
401 | * num_bblocks - Number of BBSIZE blocks in on-disk log | |
402 | * | |
403 | * Return error or zero. | |
404 | */ | |
405 | int | |
249a8c11 DC |
406 | xfs_log_mount( |
407 | xfs_mount_t *mp, | |
408 | xfs_buftarg_t *log_target, | |
409 | xfs_daddr_t blk_offset, | |
410 | int num_bblks) | |
1da177e4 | 411 | { |
249a8c11 DC |
412 | int error; |
413 | ||
1da177e4 LT |
414 | if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) |
415 | cmn_err(CE_NOTE, "XFS mounting filesystem %s", mp->m_fsname); | |
416 | else { | |
417 | cmn_err(CE_NOTE, | |
418 | "!Mounting filesystem \"%s\" in no-recovery mode. Filesystem will be inconsistent.", | |
419 | mp->m_fsname); | |
bd186aa9 | 420 | ASSERT(mp->m_flags & XFS_MOUNT_RDONLY); |
1da177e4 LT |
421 | } |
422 | ||
423 | mp->m_log = xlog_alloc_log(mp, log_target, blk_offset, num_bblks); | |
a6cb767e DC |
424 | if (IS_ERR(mp->m_log)) { |
425 | error = -PTR_ERR(mp->m_log); | |
644c3567 DC |
426 | goto out; |
427 | } | |
1da177e4 | 428 | |
249a8c11 DC |
429 | /* |
430 | * Initialize the AIL now we have a log. | |
431 | */ | |
249a8c11 DC |
432 | error = xfs_trans_ail_init(mp); |
433 | if (error) { | |
434 | cmn_err(CE_WARN, "XFS: AIL initialisation failed: error %d", error); | |
26430752 | 435 | goto out_free_log; |
249a8c11 | 436 | } |
a9c21c1b | 437 | mp->m_log->l_ailp = mp->m_ail; |
249a8c11 | 438 | |
1da177e4 LT |
439 | /* |
440 | * skip log recovery on a norecovery mount. pretend it all | |
441 | * just worked. | |
442 | */ | |
443 | if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) { | |
249a8c11 | 444 | int readonly = (mp->m_flags & XFS_MOUNT_RDONLY); |
1da177e4 LT |
445 | |
446 | if (readonly) | |
bd186aa9 | 447 | mp->m_flags &= ~XFS_MOUNT_RDONLY; |
1da177e4 | 448 | |
65be6054 | 449 | error = xlog_recover(mp->m_log); |
1da177e4 LT |
450 | |
451 | if (readonly) | |
bd186aa9 | 452 | mp->m_flags |= XFS_MOUNT_RDONLY; |
1da177e4 LT |
453 | if (error) { |
454 | cmn_err(CE_WARN, "XFS: log mount/recovery failed: error %d", error); | |
26430752 | 455 | goto out_destroy_ail; |
1da177e4 LT |
456 | } |
457 | } | |
458 | ||
459 | /* Normal transactions can now occur */ | |
460 | mp->m_log->l_flags &= ~XLOG_ACTIVE_RECOVERY; | |
461 | ||
1da177e4 | 462 | return 0; |
26430752 CH |
463 | |
464 | out_destroy_ail: | |
465 | xfs_trans_ail_destroy(mp); | |
466 | out_free_log: | |
467 | xlog_dealloc_log(mp->m_log); | |
644c3567 | 468 | out: |
249a8c11 | 469 | return error; |
26430752 | 470 | } |
1da177e4 LT |
471 | |
472 | /* | |
473 | * Finish the recovery of the file system. This is separate from | |
474 | * the xfs_log_mount() call, because it depends on the code in | |
475 | * xfs_mountfs() to read in the root and real-time bitmap inodes | |
476 | * between calling xfs_log_mount() and here. | |
477 | * | |
478 | * mp - ubiquitous xfs mount point structure | |
479 | */ | |
480 | int | |
4249023a | 481 | xfs_log_mount_finish(xfs_mount_t *mp) |
1da177e4 LT |
482 | { |
483 | int error; | |
484 | ||
485 | if (!(mp->m_flags & XFS_MOUNT_NORECOVERY)) | |
4249023a | 486 | error = xlog_recover_finish(mp->m_log); |
1da177e4 LT |
487 | else { |
488 | error = 0; | |
bd186aa9 | 489 | ASSERT(mp->m_flags & XFS_MOUNT_RDONLY); |
1da177e4 LT |
490 | } |
491 | ||
492 | return error; | |
493 | } | |
494 | ||
1da177e4 LT |
495 | /* |
496 | * Final log writes as part of unmount. | |
497 | * | |
498 | * Mark the filesystem clean as unmount happens. Note that during relocation | |
499 | * this routine needs to be executed as part of source-bag while the | |
500 | * deallocation must not be done until source-end. | |
501 | */ | |
502 | ||
503 | /* | |
504 | * Unmount record used to have a string "Unmount filesystem--" in the | |
505 | * data section where the "Un" was really a magic number (XLOG_UNMOUNT_TYPE). | |
506 | * We just write the magic number now since that particular field isn't | |
507 | * currently architecture converted and "nUmount" is a bit foo. | |
508 | * As far as I know, there weren't any dependencies on the old behaviour. | |
509 | */ | |
510 | ||
511 | int | |
512 | xfs_log_unmount_write(xfs_mount_t *mp) | |
513 | { | |
514 | xlog_t *log = mp->m_log; | |
515 | xlog_in_core_t *iclog; | |
516 | #ifdef DEBUG | |
517 | xlog_in_core_t *first_iclog; | |
518 | #endif | |
519 | xfs_log_iovec_t reg[1]; | |
35a8a72f | 520 | xlog_ticket_t *tic = NULL; |
1da177e4 LT |
521 | xfs_lsn_t lsn; |
522 | int error; | |
1da177e4 LT |
523 | |
524 | /* the data section must be 32 bit size aligned */ | |
525 | struct { | |
526 | __uint16_t magic; | |
527 | __uint16_t pad1; | |
528 | __uint32_t pad2; /* may as well make it 64 bits */ | |
529 | } magic = { XLOG_UNMOUNT_TYPE, 0, 0 }; | |
530 | ||
1da177e4 LT |
531 | /* |
532 | * Don't write out unmount record on read-only mounts. | |
533 | * Or, if we are doing a forced umount (typically because of IO errors). | |
534 | */ | |
bd186aa9 | 535 | if (mp->m_flags & XFS_MOUNT_RDONLY) |
1da177e4 LT |
536 | return 0; |
537 | ||
a14a348b | 538 | error = _xfs_log_force(mp, XFS_LOG_SYNC, NULL); |
b911ca04 | 539 | ASSERT(error || !(XLOG_FORCED_SHUTDOWN(log))); |
1da177e4 LT |
540 | |
541 | #ifdef DEBUG | |
542 | first_iclog = iclog = log->l_iclog; | |
543 | do { | |
544 | if (!(iclog->ic_state & XLOG_STATE_IOERROR)) { | |
545 | ASSERT(iclog->ic_state & XLOG_STATE_ACTIVE); | |
546 | ASSERT(iclog->ic_offset == 0); | |
547 | } | |
548 | iclog = iclog->ic_next; | |
549 | } while (iclog != first_iclog); | |
550 | #endif | |
551 | if (! (XLOG_FORCED_SHUTDOWN(log))) { | |
552 | reg[0].i_addr = (void*)&magic; | |
553 | reg[0].i_len = sizeof(magic); | |
4139b3b3 | 554 | reg[0].i_type = XLOG_REG_TYPE_UNMOUNT; |
1da177e4 | 555 | |
955e47ad TS |
556 | error = xfs_log_reserve(mp, 600, 1, &tic, |
557 | XFS_LOG, 0, XLOG_UNMOUNT_REC_TYPE); | |
1da177e4 LT |
558 | if (!error) { |
559 | /* remove inited flag */ | |
560 | ((xlog_ticket_t *)tic)->t_flags = 0; | |
561 | error = xlog_write(mp, reg, 1, tic, &lsn, | |
562 | NULL, XLOG_UNMOUNT_TRANS); | |
563 | /* | |
564 | * At this point, we're umounting anyway, | |
565 | * so there's no point in transitioning log state | |
566 | * to IOERROR. Just continue... | |
567 | */ | |
568 | } | |
569 | ||
570 | if (error) { | |
571 | xfs_fs_cmn_err(CE_ALERT, mp, | |
572 | "xfs_log_unmount: unmount record failed"); | |
573 | } | |
574 | ||
575 | ||
b22cd72c | 576 | spin_lock(&log->l_icloglock); |
1da177e4 | 577 | iclog = log->l_iclog; |
155cc6b7 | 578 | atomic_inc(&iclog->ic_refcnt); |
1da177e4 | 579 | xlog_state_want_sync(log, iclog); |
39e2defe | 580 | spin_unlock(&log->l_icloglock); |
1bb7d6b5 | 581 | error = xlog_state_release_iclog(log, iclog); |
1da177e4 | 582 | |
b22cd72c | 583 | spin_lock(&log->l_icloglock); |
1da177e4 LT |
584 | if (!(iclog->ic_state == XLOG_STATE_ACTIVE || |
585 | iclog->ic_state == XLOG_STATE_DIRTY)) { | |
586 | if (!XLOG_FORCED_SHUTDOWN(log)) { | |
12017faf | 587 | sv_wait(&iclog->ic_force_wait, PMEM, |
1da177e4 LT |
588 | &log->l_icloglock, s); |
589 | } else { | |
b22cd72c | 590 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
591 | } |
592 | } else { | |
b22cd72c | 593 | spin_unlock(&log->l_icloglock); |
1da177e4 | 594 | } |
955e47ad | 595 | if (tic) { |
0b1b213f | 596 | trace_xfs_log_umount_write(log, tic); |
955e47ad | 597 | xlog_ungrant_log_space(log, tic); |
cc09c0dc | 598 | xfs_log_ticket_put(tic); |
955e47ad | 599 | } |
1da177e4 LT |
600 | } else { |
601 | /* | |
602 | * We're already in forced_shutdown mode, couldn't | |
603 | * even attempt to write out the unmount transaction. | |
604 | * | |
605 | * Go through the motions of sync'ing and releasing | |
606 | * the iclog, even though no I/O will actually happen, | |
c41564b5 | 607 | * we need to wait for other log I/Os that may already |
1da177e4 LT |
608 | * be in progress. Do this as a separate section of |
609 | * code so we'll know if we ever get stuck here that | |
610 | * we're in this odd situation of trying to unmount | |
611 | * a file system that went into forced_shutdown as | |
612 | * the result of an unmount.. | |
613 | */ | |
b22cd72c | 614 | spin_lock(&log->l_icloglock); |
1da177e4 | 615 | iclog = log->l_iclog; |
155cc6b7 | 616 | atomic_inc(&iclog->ic_refcnt); |
1da177e4 LT |
617 | |
618 | xlog_state_want_sync(log, iclog); | |
39e2defe | 619 | spin_unlock(&log->l_icloglock); |
1bb7d6b5 | 620 | error = xlog_state_release_iclog(log, iclog); |
1da177e4 | 621 | |
b22cd72c | 622 | spin_lock(&log->l_icloglock); |
1da177e4 LT |
623 | |
624 | if ( ! ( iclog->ic_state == XLOG_STATE_ACTIVE | |
625 | || iclog->ic_state == XLOG_STATE_DIRTY | |
626 | || iclog->ic_state == XLOG_STATE_IOERROR) ) { | |
627 | ||
12017faf | 628 | sv_wait(&iclog->ic_force_wait, PMEM, |
1da177e4 LT |
629 | &log->l_icloglock, s); |
630 | } else { | |
b22cd72c | 631 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
632 | } |
633 | } | |
634 | ||
1bb7d6b5 | 635 | return error; |
1da177e4 LT |
636 | } /* xfs_log_unmount_write */ |
637 | ||
638 | /* | |
639 | * Deallocate log structures for unmount/relocation. | |
249a8c11 DC |
640 | * |
641 | * We need to stop the aild from running before we destroy | |
642 | * and deallocate the log as the aild references the log. | |
1da177e4 LT |
643 | */ |
644 | void | |
21b699c8 | 645 | xfs_log_unmount(xfs_mount_t *mp) |
1da177e4 | 646 | { |
249a8c11 | 647 | xfs_trans_ail_destroy(mp); |
c41564b5 | 648 | xlog_dealloc_log(mp->m_log); |
1da177e4 LT |
649 | } |
650 | ||
651 | /* | |
652 | * Write region vectors to log. The write happens using the space reservation | |
653 | * of the ticket (tic). It is not a requirement that all writes for a given | |
654 | * transaction occur with one call to xfs_log_write(). | |
655 | */ | |
656 | int | |
35a8a72f CH |
657 | xfs_log_write( |
658 | struct xfs_mount *mp, | |
659 | struct xfs_log_iovec reg[], | |
660 | int nentries, | |
661 | struct xlog_ticket *tic, | |
662 | xfs_lsn_t *start_lsn) | |
1da177e4 | 663 | { |
35a8a72f CH |
664 | struct log *log = mp->m_log; |
665 | int error; | |
1da177e4 | 666 | |
1da177e4 LT |
667 | if (XLOG_FORCED_SHUTDOWN(log)) |
668 | return XFS_ERROR(EIO); | |
669 | ||
35a8a72f CH |
670 | error = xlog_write(mp, reg, nentries, tic, start_lsn, NULL, 0); |
671 | if (error) | |
7d04a335 | 672 | xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR); |
014c2544 | 673 | return error; |
35a8a72f | 674 | } |
1da177e4 LT |
675 | |
676 | void | |
677 | xfs_log_move_tail(xfs_mount_t *mp, | |
678 | xfs_lsn_t tail_lsn) | |
679 | { | |
680 | xlog_ticket_t *tic; | |
681 | xlog_t *log = mp->m_log; | |
682 | int need_bytes, free_bytes, cycle, bytes; | |
1da177e4 | 683 | |
1da177e4 LT |
684 | if (XLOG_FORCED_SHUTDOWN(log)) |
685 | return; | |
1da177e4 LT |
686 | |
687 | if (tail_lsn == 0) { | |
688 | /* needed since sync_lsn is 64 bits */ | |
b22cd72c | 689 | spin_lock(&log->l_icloglock); |
1da177e4 | 690 | tail_lsn = log->l_last_sync_lsn; |
b22cd72c | 691 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
692 | } |
693 | ||
c8b5ea28 | 694 | spin_lock(&log->l_grant_lock); |
1da177e4 LT |
695 | |
696 | /* Also an invalid lsn. 1 implies that we aren't passing in a valid | |
697 | * tail_lsn. | |
698 | */ | |
699 | if (tail_lsn != 1) { | |
700 | log->l_tail_lsn = tail_lsn; | |
701 | } | |
702 | ||
703 | if ((tic = log->l_write_headq)) { | |
704 | #ifdef DEBUG | |
705 | if (log->l_flags & XLOG_ACTIVE_RECOVERY) | |
706 | panic("Recovery problem"); | |
707 | #endif | |
708 | cycle = log->l_grant_write_cycle; | |
709 | bytes = log->l_grant_write_bytes; | |
710 | free_bytes = xlog_space_left(log, cycle, bytes); | |
711 | do { | |
712 | ASSERT(tic->t_flags & XLOG_TIC_PERM_RESERV); | |
713 | ||
714 | if (free_bytes < tic->t_unit_res && tail_lsn != 1) | |
715 | break; | |
716 | tail_lsn = 0; | |
717 | free_bytes -= tic->t_unit_res; | |
12017faf | 718 | sv_signal(&tic->t_wait); |
1da177e4 LT |
719 | tic = tic->t_next; |
720 | } while (tic != log->l_write_headq); | |
721 | } | |
722 | if ((tic = log->l_reserve_headq)) { | |
723 | #ifdef DEBUG | |
724 | if (log->l_flags & XLOG_ACTIVE_RECOVERY) | |
725 | panic("Recovery problem"); | |
726 | #endif | |
727 | cycle = log->l_grant_reserve_cycle; | |
728 | bytes = log->l_grant_reserve_bytes; | |
729 | free_bytes = xlog_space_left(log, cycle, bytes); | |
730 | do { | |
731 | if (tic->t_flags & XLOG_TIC_PERM_RESERV) | |
732 | need_bytes = tic->t_unit_res*tic->t_cnt; | |
733 | else | |
734 | need_bytes = tic->t_unit_res; | |
735 | if (free_bytes < need_bytes && tail_lsn != 1) | |
736 | break; | |
737 | tail_lsn = 0; | |
738 | free_bytes -= need_bytes; | |
12017faf | 739 | sv_signal(&tic->t_wait); |
1da177e4 LT |
740 | tic = tic->t_next; |
741 | } while (tic != log->l_reserve_headq); | |
742 | } | |
c8b5ea28 | 743 | spin_unlock(&log->l_grant_lock); |
1da177e4 LT |
744 | } /* xfs_log_move_tail */ |
745 | ||
746 | /* | |
747 | * Determine if we have a transaction that has gone to disk | |
b6f8dd49 DC |
748 | * that needs to be covered. To begin the transition to the idle state |
749 | * firstly the log needs to be idle (no AIL and nothing in the iclogs). | |
750 | * If we are then in a state where covering is needed, the caller is informed | |
751 | * that dummy transactions are required to move the log into the idle state. | |
752 | * | |
753 | * Because this is called as part of the sync process, we should also indicate | |
754 | * that dummy transactions should be issued in anything but the covered or | |
755 | * idle states. This ensures that the log tail is accurately reflected in | |
756 | * the log at the end of the sync, hence if a crash occurrs avoids replay | |
757 | * of transactions where the metadata is already on disk. | |
1da177e4 LT |
758 | */ |
759 | int | |
760 | xfs_log_need_covered(xfs_mount_t *mp) | |
761 | { | |
27d8d5fe | 762 | int needed = 0; |
1da177e4 | 763 | xlog_t *log = mp->m_log; |
1da177e4 | 764 | |
92821e2b | 765 | if (!xfs_fs_writable(mp)) |
1da177e4 LT |
766 | return 0; |
767 | ||
b22cd72c | 768 | spin_lock(&log->l_icloglock); |
b6f8dd49 DC |
769 | switch (log->l_covered_state) { |
770 | case XLOG_STATE_COVER_DONE: | |
771 | case XLOG_STATE_COVER_DONE2: | |
772 | case XLOG_STATE_COVER_IDLE: | |
773 | break; | |
774 | case XLOG_STATE_COVER_NEED: | |
775 | case XLOG_STATE_COVER_NEED2: | |
776 | if (!xfs_trans_ail_tail(log->l_ailp) && | |
777 | xlog_iclogs_empty(log)) { | |
778 | if (log->l_covered_state == XLOG_STATE_COVER_NEED) | |
779 | log->l_covered_state = XLOG_STATE_COVER_DONE; | |
780 | else | |
781 | log->l_covered_state = XLOG_STATE_COVER_DONE2; | |
1da177e4 | 782 | } |
b6f8dd49 DC |
783 | /* FALLTHRU */ |
784 | default: | |
1da177e4 | 785 | needed = 1; |
b6f8dd49 | 786 | break; |
1da177e4 | 787 | } |
b22cd72c | 788 | spin_unlock(&log->l_icloglock); |
014c2544 | 789 | return needed; |
1da177e4 LT |
790 | } |
791 | ||
792 | /****************************************************************************** | |
793 | * | |
794 | * local routines | |
795 | * | |
796 | ****************************************************************************** | |
797 | */ | |
798 | ||
799 | /* xfs_trans_tail_ail returns 0 when there is nothing in the list. | |
800 | * The log manager must keep track of the last LR which was committed | |
801 | * to disk. The lsn of this LR will become the new tail_lsn whenever | |
802 | * xfs_trans_tail_ail returns 0. If we don't do this, we run into | |
803 | * the situation where stuff could be written into the log but nothing | |
804 | * was ever in the AIL when asked. Eventually, we panic since the | |
805 | * tail hits the head. | |
806 | * | |
807 | * We may be holding the log iclog lock upon entering this routine. | |
808 | */ | |
809 | xfs_lsn_t | |
810 | xlog_assign_tail_lsn(xfs_mount_t *mp) | |
811 | { | |
812 | xfs_lsn_t tail_lsn; | |
1da177e4 LT |
813 | xlog_t *log = mp->m_log; |
814 | ||
5b00f14f | 815 | tail_lsn = xfs_trans_ail_tail(mp->m_ail); |
c8b5ea28 | 816 | spin_lock(&log->l_grant_lock); |
1da177e4 LT |
817 | if (tail_lsn != 0) { |
818 | log->l_tail_lsn = tail_lsn; | |
819 | } else { | |
820 | tail_lsn = log->l_tail_lsn = log->l_last_sync_lsn; | |
821 | } | |
c8b5ea28 | 822 | spin_unlock(&log->l_grant_lock); |
1da177e4 LT |
823 | |
824 | return tail_lsn; | |
825 | } /* xlog_assign_tail_lsn */ | |
826 | ||
827 | ||
828 | /* | |
829 | * Return the space in the log between the tail and the head. The head | |
830 | * is passed in the cycle/bytes formal parms. In the special case where | |
831 | * the reserve head has wrapped passed the tail, this calculation is no | |
832 | * longer valid. In this case, just return 0 which means there is no space | |
833 | * in the log. This works for all places where this function is called | |
834 | * with the reserve head. Of course, if the write head were to ever | |
835 | * wrap the tail, we should blow up. Rather than catch this case here, | |
836 | * we depend on other ASSERTions in other parts of the code. XXXmiken | |
837 | * | |
838 | * This code also handles the case where the reservation head is behind | |
839 | * the tail. The details of this case are described below, but the end | |
840 | * result is that we return the size of the log as the amount of space left. | |
841 | */ | |
a8272ce0 | 842 | STATIC int |
1da177e4 LT |
843 | xlog_space_left(xlog_t *log, int cycle, int bytes) |
844 | { | |
845 | int free_bytes; | |
846 | int tail_bytes; | |
847 | int tail_cycle; | |
848 | ||
849 | tail_bytes = BBTOB(BLOCK_LSN(log->l_tail_lsn)); | |
850 | tail_cycle = CYCLE_LSN(log->l_tail_lsn); | |
851 | if ((tail_cycle == cycle) && (bytes >= tail_bytes)) { | |
852 | free_bytes = log->l_logsize - (bytes - tail_bytes); | |
853 | } else if ((tail_cycle + 1) < cycle) { | |
854 | return 0; | |
855 | } else if (tail_cycle < cycle) { | |
856 | ASSERT(tail_cycle == (cycle - 1)); | |
857 | free_bytes = tail_bytes - bytes; | |
858 | } else { | |
859 | /* | |
860 | * The reservation head is behind the tail. | |
861 | * In this case we just want to return the size of the | |
862 | * log as the amount of space left. | |
863 | */ | |
864 | xfs_fs_cmn_err(CE_ALERT, log->l_mp, | |
865 | "xlog_space_left: head behind tail\n" | |
866 | " tail_cycle = %d, tail_bytes = %d\n" | |
867 | " GH cycle = %d, GH bytes = %d", | |
868 | tail_cycle, tail_bytes, cycle, bytes); | |
869 | ASSERT(0); | |
870 | free_bytes = log->l_logsize; | |
871 | } | |
872 | return free_bytes; | |
873 | } /* xlog_space_left */ | |
874 | ||
875 | ||
876 | /* | |
877 | * Log function which is called when an io completes. | |
878 | * | |
879 | * The log manager needs its own routine, in order to control what | |
880 | * happens with the buffer after the write completes. | |
881 | */ | |
882 | void | |
883 | xlog_iodone(xfs_buf_t *bp) | |
884 | { | |
885 | xlog_in_core_t *iclog; | |
886 | xlog_t *l; | |
887 | int aborted; | |
888 | ||
889 | iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *); | |
890 | ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long) 2); | |
891 | XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1); | |
892 | aborted = 0; | |
1da177e4 LT |
893 | l = iclog->ic_log; |
894 | ||
0bfefc46 | 895 | /* |
73f6aa4d CH |
896 | * If the _XFS_BARRIER_FAILED flag was set by a lower |
897 | * layer, it means the underlying device no longer supports | |
0bfefc46 DC |
898 | * barrier I/O. Warn loudly and turn off barriers. |
899 | */ | |
73f6aa4d CH |
900 | if (bp->b_flags & _XFS_BARRIER_FAILED) { |
901 | bp->b_flags &= ~_XFS_BARRIER_FAILED; | |
0bfefc46 DC |
902 | l->l_mp->m_flags &= ~XFS_MOUNT_BARRIER; |
903 | xfs_fs_cmn_err(CE_WARN, l->l_mp, | |
904 | "xlog_iodone: Barriers are no longer supported" | |
905 | " by device. Disabling barriers\n"); | |
0bfefc46 DC |
906 | } |
907 | ||
1da177e4 LT |
908 | /* |
909 | * Race to shutdown the filesystem if we see an error. | |
910 | */ | |
911 | if (XFS_TEST_ERROR((XFS_BUF_GETERROR(bp)), l->l_mp, | |
912 | XFS_ERRTAG_IODONE_IOERR, XFS_RANDOM_IODONE_IOERR)) { | |
913 | xfs_ioerror_alert("xlog_iodone", l->l_mp, bp, XFS_BUF_ADDR(bp)); | |
914 | XFS_BUF_STALE(bp); | |
7d04a335 | 915 | xfs_force_shutdown(l->l_mp, SHUTDOWN_LOG_IO_ERROR); |
1da177e4 LT |
916 | /* |
917 | * This flag will be propagated to the trans-committed | |
918 | * callback routines to let them know that the log-commit | |
919 | * didn't succeed. | |
920 | */ | |
921 | aborted = XFS_LI_ABORTED; | |
922 | } else if (iclog->ic_state & XLOG_STATE_IOERROR) { | |
923 | aborted = XFS_LI_ABORTED; | |
924 | } | |
3db296f3 DC |
925 | |
926 | /* log I/O is always issued ASYNC */ | |
927 | ASSERT(XFS_BUF_ISASYNC(bp)); | |
1da177e4 | 928 | xlog_state_done_syncing(iclog, aborted); |
3db296f3 DC |
929 | /* |
930 | * do not reference the buffer (bp) here as we could race | |
931 | * with it being freed after writing the unmount record to the | |
932 | * log. | |
933 | */ | |
934 | ||
1da177e4 LT |
935 | } /* xlog_iodone */ |
936 | ||
1da177e4 LT |
937 | /* |
938 | * Return size of each in-core log record buffer. | |
939 | * | |
9da096fd | 940 | * All machines get 8 x 32kB buffers by default, unless tuned otherwise. |
1da177e4 LT |
941 | * |
942 | * If the filesystem blocksize is too large, we may need to choose a | |
943 | * larger size since the directory code currently logs entire blocks. | |
944 | */ | |
945 | ||
946 | STATIC void | |
947 | xlog_get_iclog_buffer_size(xfs_mount_t *mp, | |
948 | xlog_t *log) | |
949 | { | |
950 | int size; | |
951 | int xhdrs; | |
952 | ||
1cb51258 ES |
953 | if (mp->m_logbufs <= 0) |
954 | log->l_iclog_bufs = XLOG_MAX_ICLOGS; | |
955 | else | |
cfcbbbd0 | 956 | log->l_iclog_bufs = mp->m_logbufs; |
1da177e4 LT |
957 | |
958 | /* | |
959 | * Buffer size passed in from mount system call. | |
960 | */ | |
cfcbbbd0 | 961 | if (mp->m_logbsize > 0) { |
1da177e4 LT |
962 | size = log->l_iclog_size = mp->m_logbsize; |
963 | log->l_iclog_size_log = 0; | |
964 | while (size != 1) { | |
965 | log->l_iclog_size_log++; | |
966 | size >>= 1; | |
967 | } | |
968 | ||
62118709 | 969 | if (xfs_sb_version_haslogv2(&mp->m_sb)) { |
9da096fd MP |
970 | /* # headers = size / 32k |
971 | * one header holds cycles from 32k of data | |
1da177e4 LT |
972 | */ |
973 | ||
974 | xhdrs = mp->m_logbsize / XLOG_HEADER_CYCLE_SIZE; | |
975 | if (mp->m_logbsize % XLOG_HEADER_CYCLE_SIZE) | |
976 | xhdrs++; | |
977 | log->l_iclog_hsize = xhdrs << BBSHIFT; | |
978 | log->l_iclog_heads = xhdrs; | |
979 | } else { | |
980 | ASSERT(mp->m_logbsize <= XLOG_BIG_RECORD_BSIZE); | |
981 | log->l_iclog_hsize = BBSIZE; | |
982 | log->l_iclog_heads = 1; | |
983 | } | |
cfcbbbd0 | 984 | goto done; |
1da177e4 LT |
985 | } |
986 | ||
9da096fd | 987 | /* All machines use 32kB buffers by default. */ |
1cb51258 ES |
988 | log->l_iclog_size = XLOG_BIG_RECORD_BSIZE; |
989 | log->l_iclog_size_log = XLOG_BIG_RECORD_BSHIFT; | |
1da177e4 LT |
990 | |
991 | /* the default log size is 16k or 32k which is one header sector */ | |
992 | log->l_iclog_hsize = BBSIZE; | |
993 | log->l_iclog_heads = 1; | |
994 | ||
7153f8ba CH |
995 | done: |
996 | /* are we being asked to make the sizes selected above visible? */ | |
cfcbbbd0 NS |
997 | if (mp->m_logbufs == 0) |
998 | mp->m_logbufs = log->l_iclog_bufs; | |
999 | if (mp->m_logbsize == 0) | |
1000 | mp->m_logbsize = log->l_iclog_size; | |
1da177e4 LT |
1001 | } /* xlog_get_iclog_buffer_size */ |
1002 | ||
1003 | ||
1004 | /* | |
1005 | * This routine initializes some of the log structure for a given mount point. | |
1006 | * Its primary purpose is to fill in enough, so recovery can occur. However, | |
1007 | * some other stuff may be filled in too. | |
1008 | */ | |
1009 | STATIC xlog_t * | |
1010 | xlog_alloc_log(xfs_mount_t *mp, | |
1011 | xfs_buftarg_t *log_target, | |
1012 | xfs_daddr_t blk_offset, | |
1013 | int num_bblks) | |
1014 | { | |
1015 | xlog_t *log; | |
1016 | xlog_rec_header_t *head; | |
1017 | xlog_in_core_t **iclogp; | |
1018 | xlog_in_core_t *iclog, *prev_iclog=NULL; | |
1019 | xfs_buf_t *bp; | |
1020 | int i; | |
1021 | int iclogsize; | |
a6cb767e | 1022 | int error = ENOMEM; |
1da177e4 | 1023 | |
644c3567 | 1024 | log = kmem_zalloc(sizeof(xlog_t), KM_MAYFAIL); |
a6cb767e DC |
1025 | if (!log) { |
1026 | xlog_warn("XFS: Log allocation failed: No memory!"); | |
1027 | goto out; | |
1028 | } | |
1da177e4 LT |
1029 | |
1030 | log->l_mp = mp; | |
1031 | log->l_targ = log_target; | |
1032 | log->l_logsize = BBTOB(num_bblks); | |
1033 | log->l_logBBstart = blk_offset; | |
1034 | log->l_logBBsize = num_bblks; | |
1035 | log->l_covered_state = XLOG_STATE_COVER_IDLE; | |
1036 | log->l_flags |= XLOG_ACTIVE_RECOVERY; | |
1037 | ||
1038 | log->l_prev_block = -1; | |
03bea6fe | 1039 | log->l_tail_lsn = xlog_assign_lsn(1, 0); |
1da177e4 LT |
1040 | /* log->l_tail_lsn = 0x100000000LL; cycle = 1; current block = 0 */ |
1041 | log->l_last_sync_lsn = log->l_tail_lsn; | |
1042 | log->l_curr_cycle = 1; /* 0 is bad since this is initial value */ | |
1043 | log->l_grant_reserve_cycle = 1; | |
1044 | log->l_grant_write_cycle = 1; | |
1045 | ||
a6cb767e | 1046 | error = EFSCORRUPTED; |
62118709 | 1047 | if (xfs_sb_version_hassector(&mp->m_sb)) { |
1da177e4 | 1048 | log->l_sectbb_log = mp->m_sb.sb_logsectlog - BBSHIFT; |
a6cb767e DC |
1049 | if (log->l_sectbb_log < 0 || |
1050 | log->l_sectbb_log > mp->m_sectbb_log) { | |
1051 | xlog_warn("XFS: Log sector size (0x%x) out of range.", | |
1052 | log->l_sectbb_log); | |
1053 | goto out_free_log; | |
1054 | } | |
1055 | ||
1da177e4 | 1056 | /* for larger sector sizes, must have v2 or external log */ |
a6cb767e DC |
1057 | if (log->l_sectbb_log != 0 && |
1058 | (log->l_logBBstart != 0 && | |
1059 | !xfs_sb_version_haslogv2(&mp->m_sb))) { | |
1060 | xlog_warn("XFS: log sector size (0x%x) invalid " | |
1061 | "for configuration.", log->l_sectbb_log); | |
1062 | goto out_free_log; | |
1063 | } | |
1064 | if (mp->m_sb.sb_logsectlog < BBSHIFT) { | |
1065 | xlog_warn("XFS: Log sector log (0x%x) too small.", | |
1066 | mp->m_sb.sb_logsectlog); | |
1067 | goto out_free_log; | |
1068 | } | |
1da177e4 LT |
1069 | } |
1070 | log->l_sectbb_mask = (1 << log->l_sectbb_log) - 1; | |
1071 | ||
1072 | xlog_get_iclog_buffer_size(mp, log); | |
1073 | ||
a6cb767e | 1074 | error = ENOMEM; |
1da177e4 | 1075 | bp = xfs_buf_get_empty(log->l_iclog_size, mp->m_logdev_targp); |
644c3567 DC |
1076 | if (!bp) |
1077 | goto out_free_log; | |
1da177e4 | 1078 | XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone); |
1da177e4 LT |
1079 | XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1); |
1080 | ASSERT(XFS_BUF_ISBUSY(bp)); | |
1081 | ASSERT(XFS_BUF_VALUSEMA(bp) <= 0); | |
1082 | log->l_xbuf = bp; | |
1083 | ||
007c61c6 ES |
1084 | spin_lock_init(&log->l_icloglock); |
1085 | spin_lock_init(&log->l_grant_lock); | |
d748c623 | 1086 | sv_init(&log->l_flush_wait, 0, "flush_wait"); |
1da177e4 LT |
1087 | |
1088 | /* log record size must be multiple of BBSIZE; see xlog_rec_header_t */ | |
1089 | ASSERT((XFS_BUF_SIZE(bp) & BBMASK) == 0); | |
1090 | ||
1091 | iclogp = &log->l_iclog; | |
1092 | /* | |
1093 | * The amount of memory to allocate for the iclog structure is | |
1094 | * rather funky due to the way the structure is defined. It is | |
1095 | * done this way so that we can use different sizes for machines | |
1096 | * with different amounts of memory. See the definition of | |
1097 | * xlog_in_core_t in xfs_log_priv.h for details. | |
1098 | */ | |
1099 | iclogsize = log->l_iclog_size; | |
1100 | ASSERT(log->l_iclog_size >= 4096); | |
1101 | for (i=0; i < log->l_iclog_bufs; i++) { | |
644c3567 DC |
1102 | *iclogp = kmem_zalloc(sizeof(xlog_in_core_t), KM_MAYFAIL); |
1103 | if (!*iclogp) | |
1104 | goto out_free_iclog; | |
1105 | ||
1da177e4 | 1106 | iclog = *iclogp; |
1da177e4 LT |
1107 | iclog->ic_prev = prev_iclog; |
1108 | prev_iclog = iclog; | |
1fa40b01 CH |
1109 | |
1110 | bp = xfs_buf_get_noaddr(log->l_iclog_size, mp->m_logdev_targp); | |
644c3567 DC |
1111 | if (!bp) |
1112 | goto out_free_iclog; | |
1fa40b01 CH |
1113 | if (!XFS_BUF_CPSEMA(bp)) |
1114 | ASSERT(0); | |
1115 | XFS_BUF_SET_IODONE_FUNC(bp, xlog_iodone); | |
1fa40b01 CH |
1116 | XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)1); |
1117 | iclog->ic_bp = bp; | |
b28708d6 | 1118 | iclog->ic_data = bp->b_addr; |
4679b2d3 | 1119 | #ifdef DEBUG |
1da177e4 | 1120 | log->l_iclog_bak[i] = (xfs_caddr_t)&(iclog->ic_header); |
4679b2d3 | 1121 | #endif |
1da177e4 LT |
1122 | head = &iclog->ic_header; |
1123 | memset(head, 0, sizeof(xlog_rec_header_t)); | |
b53e675d CH |
1124 | head->h_magicno = cpu_to_be32(XLOG_HEADER_MAGIC_NUM); |
1125 | head->h_version = cpu_to_be32( | |
62118709 | 1126 | xfs_sb_version_haslogv2(&log->l_mp->m_sb) ? 2 : 1); |
b53e675d | 1127 | head->h_size = cpu_to_be32(log->l_iclog_size); |
1da177e4 | 1128 | /* new fields */ |
b53e675d | 1129 | head->h_fmt = cpu_to_be32(XLOG_FMT); |
1da177e4 LT |
1130 | memcpy(&head->h_fs_uuid, &mp->m_sb.sb_uuid, sizeof(uuid_t)); |
1131 | ||
1da177e4 LT |
1132 | iclog->ic_size = XFS_BUF_SIZE(bp) - log->l_iclog_hsize; |
1133 | iclog->ic_state = XLOG_STATE_ACTIVE; | |
1134 | iclog->ic_log = log; | |
114d23aa DC |
1135 | atomic_set(&iclog->ic_refcnt, 0); |
1136 | spin_lock_init(&iclog->ic_callback_lock); | |
1da177e4 | 1137 | iclog->ic_callback_tail = &(iclog->ic_callback); |
b28708d6 | 1138 | iclog->ic_datap = (char *)iclog->ic_data + log->l_iclog_hsize; |
1da177e4 LT |
1139 | |
1140 | ASSERT(XFS_BUF_ISBUSY(iclog->ic_bp)); | |
1141 | ASSERT(XFS_BUF_VALUSEMA(iclog->ic_bp) <= 0); | |
12017faf DC |
1142 | sv_init(&iclog->ic_force_wait, SV_DEFAULT, "iclog-force"); |
1143 | sv_init(&iclog->ic_write_wait, SV_DEFAULT, "iclog-write"); | |
1da177e4 LT |
1144 | |
1145 | iclogp = &iclog->ic_next; | |
1146 | } | |
1147 | *iclogp = log->l_iclog; /* complete ring */ | |
1148 | log->l_iclog->ic_prev = prev_iclog; /* re-write 1st prev ptr */ | |
1149 | ||
1150 | return log; | |
644c3567 DC |
1151 | |
1152 | out_free_iclog: | |
1153 | for (iclog = log->l_iclog; iclog; iclog = prev_iclog) { | |
1154 | prev_iclog = iclog->ic_next; | |
1155 | if (iclog->ic_bp) { | |
1156 | sv_destroy(&iclog->ic_force_wait); | |
1157 | sv_destroy(&iclog->ic_write_wait); | |
1158 | xfs_buf_free(iclog->ic_bp); | |
644c3567 DC |
1159 | } |
1160 | kmem_free(iclog); | |
1161 | } | |
1162 | spinlock_destroy(&log->l_icloglock); | |
1163 | spinlock_destroy(&log->l_grant_lock); | |
644c3567 DC |
1164 | xfs_buf_free(log->l_xbuf); |
1165 | out_free_log: | |
1166 | kmem_free(log); | |
a6cb767e DC |
1167 | out: |
1168 | return ERR_PTR(-error); | |
1da177e4 LT |
1169 | } /* xlog_alloc_log */ |
1170 | ||
1171 | ||
1172 | /* | |
1173 | * Write out the commit record of a transaction associated with the given | |
1174 | * ticket. Return the lsn of the commit record. | |
1175 | */ | |
1176 | STATIC int | |
1177 | xlog_commit_record(xfs_mount_t *mp, | |
1178 | xlog_ticket_t *ticket, | |
1179 | xlog_in_core_t **iclog, | |
1180 | xfs_lsn_t *commitlsnp) | |
1181 | { | |
1182 | int error; | |
1183 | xfs_log_iovec_t reg[1]; | |
1184 | ||
1185 | reg[0].i_addr = NULL; | |
1186 | reg[0].i_len = 0; | |
4139b3b3 | 1187 | reg[0].i_type = XLOG_REG_TYPE_COMMIT; |
1da177e4 LT |
1188 | |
1189 | ASSERT_ALWAYS(iclog); | |
1190 | if ((error = xlog_write(mp, reg, 1, ticket, commitlsnp, | |
1191 | iclog, XLOG_COMMIT_TRANS))) { | |
7d04a335 | 1192 | xfs_force_shutdown(mp, SHUTDOWN_LOG_IO_ERROR); |
1da177e4 | 1193 | } |
014c2544 | 1194 | return error; |
1da177e4 LT |
1195 | } /* xlog_commit_record */ |
1196 | ||
1197 | ||
1198 | /* | |
1199 | * Push on the buffer cache code if we ever use more than 75% of the on-disk | |
1200 | * log space. This code pushes on the lsn which would supposedly free up | |
1201 | * the 25% which we want to leave free. We may need to adopt a policy which | |
1202 | * pushes on an lsn which is further along in the log once we reach the high | |
1203 | * water mark. In this manner, we would be creating a low water mark. | |
1204 | */ | |
a8272ce0 | 1205 | STATIC void |
1da177e4 LT |
1206 | xlog_grant_push_ail(xfs_mount_t *mp, |
1207 | int need_bytes) | |
1208 | { | |
1209 | xlog_t *log = mp->m_log; /* pointer to the log */ | |
1210 | xfs_lsn_t tail_lsn; /* lsn of the log tail */ | |
1211 | xfs_lsn_t threshold_lsn = 0; /* lsn we'd like to be at */ | |
1212 | int free_blocks; /* free blocks left to write to */ | |
1213 | int free_bytes; /* free bytes left to write to */ | |
1214 | int threshold_block; /* block in lsn we'd like to be at */ | |
1215 | int threshold_cycle; /* lsn cycle we'd like to be at */ | |
1216 | int free_threshold; | |
1da177e4 LT |
1217 | |
1218 | ASSERT(BTOBB(need_bytes) < log->l_logBBsize); | |
1219 | ||
c8b5ea28 | 1220 | spin_lock(&log->l_grant_lock); |
1da177e4 LT |
1221 | free_bytes = xlog_space_left(log, |
1222 | log->l_grant_reserve_cycle, | |
1223 | log->l_grant_reserve_bytes); | |
1224 | tail_lsn = log->l_tail_lsn; | |
1225 | free_blocks = BTOBBT(free_bytes); | |
1226 | ||
1227 | /* | |
1228 | * Set the threshold for the minimum number of free blocks in the | |
1229 | * log to the maximum of what the caller needs, one quarter of the | |
1230 | * log, and 256 blocks. | |
1231 | */ | |
1232 | free_threshold = BTOBB(need_bytes); | |
1233 | free_threshold = MAX(free_threshold, (log->l_logBBsize >> 2)); | |
1234 | free_threshold = MAX(free_threshold, 256); | |
1235 | if (free_blocks < free_threshold) { | |
1236 | threshold_block = BLOCK_LSN(tail_lsn) + free_threshold; | |
1237 | threshold_cycle = CYCLE_LSN(tail_lsn); | |
1238 | if (threshold_block >= log->l_logBBsize) { | |
1239 | threshold_block -= log->l_logBBsize; | |
1240 | threshold_cycle += 1; | |
1241 | } | |
03bea6fe | 1242 | threshold_lsn = xlog_assign_lsn(threshold_cycle, threshold_block); |
1da177e4 LT |
1243 | |
1244 | /* Don't pass in an lsn greater than the lsn of the last | |
1245 | * log record known to be on disk. | |
1246 | */ | |
1247 | if (XFS_LSN_CMP(threshold_lsn, log->l_last_sync_lsn) > 0) | |
1248 | threshold_lsn = log->l_last_sync_lsn; | |
1249 | } | |
c8b5ea28 | 1250 | spin_unlock(&log->l_grant_lock); |
1da177e4 LT |
1251 | |
1252 | /* | |
1253 | * Get the transaction layer to kick the dirty buffers out to | |
1254 | * disk asynchronously. No point in trying to do this if | |
1255 | * the filesystem is shutting down. | |
1256 | */ | |
1257 | if (threshold_lsn && | |
1258 | !XLOG_FORCED_SHUTDOWN(log)) | |
783a2f65 | 1259 | xfs_trans_ail_push(log->l_ailp, threshold_lsn); |
1da177e4 LT |
1260 | } /* xlog_grant_push_ail */ |
1261 | ||
873ff550 CH |
1262 | /* |
1263 | * The bdstrat callback function for log bufs. This gives us a central | |
1264 | * place to trap bufs in case we get hit by a log I/O error and need to | |
1265 | * shutdown. Actually, in practice, even when we didn't get a log error, | |
1266 | * we transition the iclogs to IOERROR state *after* flushing all existing | |
1267 | * iclogs to disk. This is because we don't want anymore new transactions to be | |
1268 | * started or completed afterwards. | |
1269 | */ | |
1270 | STATIC int | |
1271 | xlog_bdstrat( | |
1272 | struct xfs_buf *bp) | |
1273 | { | |
1274 | struct xlog_in_core *iclog; | |
1275 | ||
1276 | iclog = XFS_BUF_FSPRIVATE(bp, xlog_in_core_t *); | |
1277 | if (iclog->ic_state & XLOG_STATE_IOERROR) { | |
1278 | XFS_BUF_ERROR(bp, EIO); | |
1279 | XFS_BUF_STALE(bp); | |
1280 | xfs_biodone(bp); | |
1281 | /* | |
1282 | * It would seem logical to return EIO here, but we rely on | |
1283 | * the log state machine to propagate I/O errors instead of | |
1284 | * doing it here. | |
1285 | */ | |
1286 | return 0; | |
1287 | } | |
1288 | ||
1289 | bp->b_flags |= _XBF_RUN_QUEUES; | |
1290 | xfs_buf_iorequest(bp); | |
1291 | return 0; | |
1292 | } | |
1da177e4 LT |
1293 | |
1294 | /* | |
1295 | * Flush out the in-core log (iclog) to the on-disk log in an asynchronous | |
1296 | * fashion. Previously, we should have moved the current iclog | |
1297 | * ptr in the log to point to the next available iclog. This allows further | |
1298 | * write to continue while this code syncs out an iclog ready to go. | |
1299 | * Before an in-core log can be written out, the data section must be scanned | |
1300 | * to save away the 1st word of each BBSIZE block into the header. We replace | |
1301 | * it with the current cycle count. Each BBSIZE block is tagged with the | |
1302 | * cycle count because there in an implicit assumption that drives will | |
1303 | * guarantee that entire 512 byte blocks get written at once. In other words, | |
1304 | * we can't have part of a 512 byte block written and part not written. By | |
1305 | * tagging each block, we will know which blocks are valid when recovering | |
1306 | * after an unclean shutdown. | |
1307 | * | |
1308 | * This routine is single threaded on the iclog. No other thread can be in | |
1309 | * this routine with the same iclog. Changing contents of iclog can there- | |
1310 | * fore be done without grabbing the state machine lock. Updating the global | |
1311 | * log will require grabbing the lock though. | |
1312 | * | |
1313 | * The entire log manager uses a logical block numbering scheme. Only | |
1314 | * log_sync (and then only bwrite()) know about the fact that the log may | |
1315 | * not start with block zero on a given device. The log block start offset | |
1316 | * is added immediately before calling bwrite(). | |
1317 | */ | |
1318 | ||
a8272ce0 | 1319 | STATIC int |
1da177e4 LT |
1320 | xlog_sync(xlog_t *log, |
1321 | xlog_in_core_t *iclog) | |
1322 | { | |
1323 | xfs_caddr_t dptr; /* pointer to byte sized element */ | |
1324 | xfs_buf_t *bp; | |
b53e675d | 1325 | int i; |
1da177e4 LT |
1326 | uint count; /* byte count of bwrite */ |
1327 | uint count_init; /* initial count before roundup */ | |
1328 | int roundoff; /* roundoff to BB or stripe */ | |
1329 | int split = 0; /* split write into two regions */ | |
1330 | int error; | |
62118709 | 1331 | int v2 = xfs_sb_version_haslogv2(&log->l_mp->m_sb); |
1da177e4 LT |
1332 | |
1333 | XFS_STATS_INC(xs_log_writes); | |
155cc6b7 | 1334 | ASSERT(atomic_read(&iclog->ic_refcnt) == 0); |
1da177e4 LT |
1335 | |
1336 | /* Add for LR header */ | |
1337 | count_init = log->l_iclog_hsize + iclog->ic_offset; | |
1338 | ||
1339 | /* Round out the log write size */ | |
1340 | if (v2 && log->l_mp->m_sb.sb_logsunit > 1) { | |
1341 | /* we have a v2 stripe unit to use */ | |
1342 | count = XLOG_LSUNITTOB(log, XLOG_BTOLSUNIT(log, count_init)); | |
1343 | } else { | |
1344 | count = BBTOB(BTOBB(count_init)); | |
1345 | } | |
1346 | roundoff = count - count_init; | |
1347 | ASSERT(roundoff >= 0); | |
1348 | ASSERT((v2 && log->l_mp->m_sb.sb_logsunit > 1 && | |
1349 | roundoff < log->l_mp->m_sb.sb_logsunit) | |
1350 | || | |
1351 | (log->l_mp->m_sb.sb_logsunit <= 1 && | |
1352 | roundoff < BBTOB(1))); | |
1353 | ||
1354 | /* move grant heads by roundoff in sync */ | |
c8b5ea28 | 1355 | spin_lock(&log->l_grant_lock); |
dd954c69 | 1356 | xlog_grant_add_space(log, roundoff); |
c8b5ea28 | 1357 | spin_unlock(&log->l_grant_lock); |
1da177e4 LT |
1358 | |
1359 | /* put cycle number in every block */ | |
1360 | xlog_pack_data(log, iclog, roundoff); | |
1361 | ||
1362 | /* real byte length */ | |
1363 | if (v2) { | |
b53e675d CH |
1364 | iclog->ic_header.h_len = |
1365 | cpu_to_be32(iclog->ic_offset + roundoff); | |
1da177e4 | 1366 | } else { |
b53e675d CH |
1367 | iclog->ic_header.h_len = |
1368 | cpu_to_be32(iclog->ic_offset); | |
1da177e4 LT |
1369 | } |
1370 | ||
f5faad79 | 1371 | bp = iclog->ic_bp; |
1da177e4 LT |
1372 | ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == (unsigned long)1); |
1373 | XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2); | |
b53e675d | 1374 | XFS_BUF_SET_ADDR(bp, BLOCK_LSN(be64_to_cpu(iclog->ic_header.h_lsn))); |
1da177e4 LT |
1375 | |
1376 | XFS_STATS_ADD(xs_log_blocks, BTOBB(count)); | |
1377 | ||
1378 | /* Do we need to split this write into 2 parts? */ | |
1379 | if (XFS_BUF_ADDR(bp) + BTOBB(count) > log->l_logBBsize) { | |
1380 | split = count - (BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp))); | |
1381 | count = BBTOB(log->l_logBBsize - XFS_BUF_ADDR(bp)); | |
1382 | iclog->ic_bwritecnt = 2; /* split into 2 writes */ | |
1383 | } else { | |
1384 | iclog->ic_bwritecnt = 1; | |
1385 | } | |
511105b3 | 1386 | XFS_BUF_SET_COUNT(bp, count); |
1da177e4 | 1387 | XFS_BUF_SET_FSPRIVATE(bp, iclog); /* save for later */ |
f5faad79 | 1388 | XFS_BUF_ZEROFLAGS(bp); |
1da177e4 LT |
1389 | XFS_BUF_BUSY(bp); |
1390 | XFS_BUF_ASYNC(bp); | |
2ee1abad | 1391 | bp->b_flags |= XBF_LOG_BUFFER; |
1da177e4 | 1392 | /* |
f538d4da | 1393 | * Do an ordered write for the log block. |
f5faad79 | 1394 | * Its unnecessary to flush the first split block in the log wrap case. |
1da177e4 | 1395 | */ |
f5faad79 | 1396 | if (!split && (log->l_mp->m_flags & XFS_MOUNT_BARRIER)) |
f538d4da | 1397 | XFS_BUF_ORDERED(bp); |
1da177e4 LT |
1398 | |
1399 | ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1); | |
1400 | ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize); | |
1401 | ||
1402 | xlog_verify_iclog(log, iclog, count, B_TRUE); | |
1403 | ||
1404 | /* account for log which doesn't start at block #0 */ | |
1405 | XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart); | |
1406 | /* | |
1407 | * Don't call xfs_bwrite here. We do log-syncs even when the filesystem | |
1408 | * is shutting down. | |
1409 | */ | |
1410 | XFS_BUF_WRITE(bp); | |
1411 | ||
873ff550 | 1412 | if ((error = xlog_bdstrat(bp))) { |
1da177e4 LT |
1413 | xfs_ioerror_alert("xlog_sync", log->l_mp, bp, |
1414 | XFS_BUF_ADDR(bp)); | |
014c2544 | 1415 | return error; |
1da177e4 LT |
1416 | } |
1417 | if (split) { | |
f5faad79 | 1418 | bp = iclog->ic_log->l_xbuf; |
1da177e4 LT |
1419 | ASSERT(XFS_BUF_FSPRIVATE2(bp, unsigned long) == |
1420 | (unsigned long)1); | |
1421 | XFS_BUF_SET_FSPRIVATE2(bp, (unsigned long)2); | |
1422 | XFS_BUF_SET_ADDR(bp, 0); /* logical 0 */ | |
1423 | XFS_BUF_SET_PTR(bp, (xfs_caddr_t)((__psint_t)&(iclog->ic_header)+ | |
1424 | (__psint_t)count), split); | |
1425 | XFS_BUF_SET_FSPRIVATE(bp, iclog); | |
f5faad79 | 1426 | XFS_BUF_ZEROFLAGS(bp); |
1da177e4 LT |
1427 | XFS_BUF_BUSY(bp); |
1428 | XFS_BUF_ASYNC(bp); | |
2ee1abad | 1429 | bp->b_flags |= XBF_LOG_BUFFER; |
f538d4da CH |
1430 | if (log->l_mp->m_flags & XFS_MOUNT_BARRIER) |
1431 | XFS_BUF_ORDERED(bp); | |
1da177e4 LT |
1432 | dptr = XFS_BUF_PTR(bp); |
1433 | /* | |
1434 | * Bump the cycle numbers at the start of each block | |
1435 | * since this part of the buffer is at the start of | |
1436 | * a new cycle. Watch out for the header magic number | |
1437 | * case, though. | |
1438 | */ | |
b53e675d | 1439 | for (i = 0; i < split; i += BBSIZE) { |
413d57c9 | 1440 | be32_add_cpu((__be32 *)dptr, 1); |
b53e675d | 1441 | if (be32_to_cpu(*(__be32 *)dptr) == XLOG_HEADER_MAGIC_NUM) |
413d57c9 | 1442 | be32_add_cpu((__be32 *)dptr, 1); |
1da177e4 LT |
1443 | dptr += BBSIZE; |
1444 | } | |
1445 | ||
1446 | ASSERT(XFS_BUF_ADDR(bp) <= log->l_logBBsize-1); | |
1447 | ASSERT(XFS_BUF_ADDR(bp) + BTOBB(count) <= log->l_logBBsize); | |
1448 | ||
c41564b5 | 1449 | /* account for internal log which doesn't start at block #0 */ |
1da177e4 LT |
1450 | XFS_BUF_SET_ADDR(bp, XFS_BUF_ADDR(bp) + log->l_logBBstart); |
1451 | XFS_BUF_WRITE(bp); | |
873ff550 | 1452 | if ((error = xlog_bdstrat(bp))) { |
1da177e4 LT |
1453 | xfs_ioerror_alert("xlog_sync (split)", log->l_mp, |
1454 | bp, XFS_BUF_ADDR(bp)); | |
014c2544 | 1455 | return error; |
1da177e4 LT |
1456 | } |
1457 | } | |
014c2544 | 1458 | return 0; |
1da177e4 LT |
1459 | } /* xlog_sync */ |
1460 | ||
1461 | ||
1462 | /* | |
c41564b5 | 1463 | * Deallocate a log structure |
1da177e4 | 1464 | */ |
a8272ce0 | 1465 | STATIC void |
c41564b5 | 1466 | xlog_dealloc_log(xlog_t *log) |
1da177e4 LT |
1467 | { |
1468 | xlog_in_core_t *iclog, *next_iclog; | |
1da177e4 LT |
1469 | int i; |
1470 | ||
1da177e4 LT |
1471 | iclog = log->l_iclog; |
1472 | for (i=0; i<log->l_iclog_bufs; i++) { | |
12017faf DC |
1473 | sv_destroy(&iclog->ic_force_wait); |
1474 | sv_destroy(&iclog->ic_write_wait); | |
1da177e4 | 1475 | xfs_buf_free(iclog->ic_bp); |
1da177e4 | 1476 | next_iclog = iclog->ic_next; |
f0e2d93c | 1477 | kmem_free(iclog); |
1da177e4 LT |
1478 | iclog = next_iclog; |
1479 | } | |
1da177e4 LT |
1480 | spinlock_destroy(&log->l_icloglock); |
1481 | spinlock_destroy(&log->l_grant_lock); | |
1482 | ||
1da177e4 | 1483 | xfs_buf_free(log->l_xbuf); |
1da177e4 | 1484 | log->l_mp->m_log = NULL; |
f0e2d93c | 1485 | kmem_free(log); |
c41564b5 | 1486 | } /* xlog_dealloc_log */ |
1da177e4 LT |
1487 | |
1488 | /* | |
1489 | * Update counters atomically now that memcpy is done. | |
1490 | */ | |
1491 | /* ARGSUSED */ | |
1492 | static inline void | |
1493 | xlog_state_finish_copy(xlog_t *log, | |
1494 | xlog_in_core_t *iclog, | |
1495 | int record_cnt, | |
1496 | int copy_bytes) | |
1497 | { | |
b22cd72c | 1498 | spin_lock(&log->l_icloglock); |
1da177e4 | 1499 | |
413d57c9 | 1500 | be32_add_cpu(&iclog->ic_header.h_num_logops, record_cnt); |
1da177e4 LT |
1501 | iclog->ic_offset += copy_bytes; |
1502 | ||
b22cd72c | 1503 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
1504 | } /* xlog_state_finish_copy */ |
1505 | ||
1506 | ||
1507 | ||
1508 | ||
7e9c6396 TS |
1509 | /* |
1510 | * print out info relating to regions written which consume | |
1511 | * the reservation | |
1512 | */ | |
7e9c6396 TS |
1513 | STATIC void |
1514 | xlog_print_tic_res(xfs_mount_t *mp, xlog_ticket_t *ticket) | |
1515 | { | |
1516 | uint i; | |
1517 | uint ophdr_spc = ticket->t_res_num_ophdrs * (uint)sizeof(xlog_op_header_t); | |
1518 | ||
1519 | /* match with XLOG_REG_TYPE_* in xfs_log.h */ | |
1520 | static char *res_type_str[XLOG_REG_TYPE_MAX] = { | |
1521 | "bformat", | |
1522 | "bchunk", | |
1523 | "efi_format", | |
1524 | "efd_format", | |
1525 | "iformat", | |
1526 | "icore", | |
1527 | "iext", | |
1528 | "ibroot", | |
1529 | "ilocal", | |
1530 | "iattr_ext", | |
1531 | "iattr_broot", | |
1532 | "iattr_local", | |
1533 | "qformat", | |
1534 | "dquot", | |
1535 | "quotaoff", | |
1536 | "LR header", | |
1537 | "unmount", | |
1538 | "commit", | |
1539 | "trans header" | |
1540 | }; | |
1541 | static char *trans_type_str[XFS_TRANS_TYPE_MAX] = { | |
1542 | "SETATTR_NOT_SIZE", | |
1543 | "SETATTR_SIZE", | |
1544 | "INACTIVE", | |
1545 | "CREATE", | |
1546 | "CREATE_TRUNC", | |
1547 | "TRUNCATE_FILE", | |
1548 | "REMOVE", | |
1549 | "LINK", | |
1550 | "RENAME", | |
1551 | "MKDIR", | |
1552 | "RMDIR", | |
1553 | "SYMLINK", | |
1554 | "SET_DMATTRS", | |
1555 | "GROWFS", | |
1556 | "STRAT_WRITE", | |
1557 | "DIOSTRAT", | |
1558 | "WRITE_SYNC", | |
1559 | "WRITEID", | |
1560 | "ADDAFORK", | |
1561 | "ATTRINVAL", | |
1562 | "ATRUNCATE", | |
1563 | "ATTR_SET", | |
1564 | "ATTR_RM", | |
1565 | "ATTR_FLAG", | |
1566 | "CLEAR_AGI_BUCKET", | |
1567 | "QM_SBCHANGE", | |
1568 | "DUMMY1", | |
1569 | "DUMMY2", | |
1570 | "QM_QUOTAOFF", | |
1571 | "QM_DQALLOC", | |
1572 | "QM_SETQLIM", | |
1573 | "QM_DQCLUSTER", | |
1574 | "QM_QINOCREATE", | |
1575 | "QM_QUOTAOFF_END", | |
1576 | "SB_UNIT", | |
1577 | "FSYNC_TS", | |
1578 | "GROWFSRT_ALLOC", | |
1579 | "GROWFSRT_ZERO", | |
1580 | "GROWFSRT_FREE", | |
1581 | "SWAPEXT" | |
1582 | }; | |
1583 | ||
1584 | xfs_fs_cmn_err(CE_WARN, mp, | |
1585 | "xfs_log_write: reservation summary:\n" | |
1586 | " trans type = %s (%u)\n" | |
1587 | " unit res = %d bytes\n" | |
1588 | " current res = %d bytes\n" | |
1589 | " total reg = %u bytes (o/flow = %u bytes)\n" | |
1590 | " ophdrs = %u (ophdr space = %u bytes)\n" | |
1591 | " ophdr + reg = %u bytes\n" | |
1592 | " num regions = %u\n", | |
1593 | ((ticket->t_trans_type <= 0 || | |
1594 | ticket->t_trans_type > XFS_TRANS_TYPE_MAX) ? | |
1595 | "bad-trans-type" : trans_type_str[ticket->t_trans_type-1]), | |
1596 | ticket->t_trans_type, | |
1597 | ticket->t_unit_res, | |
1598 | ticket->t_curr_res, | |
1599 | ticket->t_res_arr_sum, ticket->t_res_o_flow, | |
1600 | ticket->t_res_num_ophdrs, ophdr_spc, | |
1601 | ticket->t_res_arr_sum + | |
1259845d | 1602 | ticket->t_res_o_flow + ophdr_spc, |
7e9c6396 TS |
1603 | ticket->t_res_num); |
1604 | ||
1605 | for (i = 0; i < ticket->t_res_num; i++) { | |
1259845d | 1606 | uint r_type = ticket->t_res_arr[i].r_type; |
7e9c6396 TS |
1607 | cmn_err(CE_WARN, |
1608 | "region[%u]: %s - %u bytes\n", | |
1609 | i, | |
1610 | ((r_type <= 0 || r_type > XLOG_REG_TYPE_MAX) ? | |
1611 | "bad-rtype" : res_type_str[r_type-1]), | |
1612 | ticket->t_res_arr[i].r_len); | |
1613 | } | |
1614 | } | |
7e9c6396 | 1615 | |
1da177e4 LT |
1616 | /* |
1617 | * Write some region out to in-core log | |
1618 | * | |
1619 | * This will be called when writing externally provided regions or when | |
1620 | * writing out a commit record for a given transaction. | |
1621 | * | |
1622 | * General algorithm: | |
1623 | * 1. Find total length of this write. This may include adding to the | |
1624 | * lengths passed in. | |
1625 | * 2. Check whether we violate the tickets reservation. | |
1626 | * 3. While writing to this iclog | |
1627 | * A. Reserve as much space in this iclog as can get | |
1628 | * B. If this is first write, save away start lsn | |
1629 | * C. While writing this region: | |
1630 | * 1. If first write of transaction, write start record | |
1631 | * 2. Write log operation header (header per region) | |
1632 | * 3. Find out if we can fit entire region into this iclog | |
1633 | * 4. Potentially, verify destination memcpy ptr | |
1634 | * 5. Memcpy (partial) region | |
1635 | * 6. If partial copy, release iclog; otherwise, continue | |
1636 | * copying more regions into current iclog | |
1637 | * 4. Mark want sync bit (in simulation mode) | |
1638 | * 5. Release iclog for potential flush to on-disk log. | |
1639 | * | |
1640 | * ERRORS: | |
1641 | * 1. Panic if reservation is overrun. This should never happen since | |
1642 | * reservation amounts are generated internal to the filesystem. | |
1643 | * NOTES: | |
1644 | * 1. Tickets are single threaded data structures. | |
1645 | * 2. The XLOG_END_TRANS & XLOG_CONTINUE_TRANS flags are passed down to the | |
1646 | * syncing routine. When a single log_write region needs to span | |
1647 | * multiple in-core logs, the XLOG_CONTINUE_TRANS bit should be set | |
1648 | * on all log operation writes which don't contain the end of the | |
1649 | * region. The XLOG_END_TRANS bit is used for the in-core log | |
1650 | * operation which contains the end of the continued log_write region. | |
1651 | * 3. When xlog_state_get_iclog_space() grabs the rest of the current iclog, | |
1652 | * we don't really know exactly how much space will be used. As a result, | |
1653 | * we don't update ic_offset until the end when we know exactly how many | |
1654 | * bytes have been written out. | |
1655 | */ | |
a8272ce0 | 1656 | STATIC int |
35a8a72f CH |
1657 | xlog_write( |
1658 | struct xfs_mount *mp, | |
1659 | struct xfs_log_iovec reg[], | |
1660 | int nentries, | |
1661 | struct xlog_ticket *ticket, | |
1662 | xfs_lsn_t *start_lsn, | |
1663 | struct xlog_in_core **commit_iclog, | |
1664 | uint flags) | |
1da177e4 | 1665 | { |
5493a0fc | 1666 | xlog_t *log = mp->m_log; |
5493a0fc | 1667 | xlog_in_core_t *iclog = NULL; /* ptr to current in-core log */ |
1da177e4 | 1668 | xlog_op_header_t *logop_head; /* ptr to log operation header */ |
1da177e4 LT |
1669 | __psint_t ptr; /* copy address into data region */ |
1670 | int len; /* # xlog_write() bytes 2 still copy */ | |
1671 | int index; /* region index currently copying */ | |
1672 | int log_offset; /* offset (from 0) into data region */ | |
1673 | int start_rec_copy; /* # bytes to copy for start record */ | |
1674 | int partial_copy; /* did we split a region? */ | |
1675 | int partial_copy_len;/* # bytes copied if split region */ | |
1676 | int need_copy; /* # bytes need to memcpy this region */ | |
1677 | int copy_len; /* # bytes actually memcpy'ing */ | |
1678 | int copy_off; /* # bytes from entry start */ | |
1679 | int contwr; /* continued write of in-core log? */ | |
1680 | int error; | |
1681 | int record_cnt = 0, data_cnt = 0; | |
1682 | ||
1683 | partial_copy_len = partial_copy = 0; | |
1684 | ||
1685 | /* Calculate potential maximum space. Each region gets its own | |
1686 | * xlog_op_header_t and may need to be double word aligned. | |
1687 | */ | |
1688 | len = 0; | |
7e9c6396 | 1689 | if (ticket->t_flags & XLOG_TIC_INITED) { /* acct for start rec of xact */ |
1da177e4 | 1690 | len += sizeof(xlog_op_header_t); |
0adba536 | 1691 | ticket->t_res_num_ophdrs++; |
7e9c6396 | 1692 | } |
1da177e4 LT |
1693 | |
1694 | for (index = 0; index < nentries; index++) { | |
1695 | len += sizeof(xlog_op_header_t); /* each region gets >= 1 */ | |
0adba536 | 1696 | ticket->t_res_num_ophdrs++; |
1da177e4 | 1697 | len += reg[index].i_len; |
0adba536 | 1698 | xlog_tic_add_region(ticket, reg[index].i_len, reg[index].i_type); |
1da177e4 LT |
1699 | } |
1700 | contwr = *start_lsn = 0; | |
1701 | ||
1702 | if (ticket->t_curr_res < len) { | |
7e9c6396 | 1703 | xlog_print_tic_res(mp, ticket); |
1da177e4 LT |
1704 | #ifdef DEBUG |
1705 | xlog_panic( | |
1706 | "xfs_log_write: reservation ran out. Need to up reservation"); | |
1707 | #else | |
1708 | /* Customer configurable panic */ | |
1709 | xfs_cmn_err(XFS_PTAG_LOGRES, CE_ALERT, mp, | |
1710 | "xfs_log_write: reservation ran out. Need to up reservation"); | |
1711 | /* If we did not panic, shutdown the filesystem */ | |
7d04a335 | 1712 | xfs_force_shutdown(mp, SHUTDOWN_CORRUPT_INCORE); |
1da177e4 LT |
1713 | #endif |
1714 | } else | |
1715 | ticket->t_curr_res -= len; | |
1716 | ||
1717 | for (index = 0; index < nentries; ) { | |
1718 | if ((error = xlog_state_get_iclog_space(log, len, &iclog, ticket, | |
1719 | &contwr, &log_offset))) | |
014c2544 | 1720 | return error; |
1da177e4 LT |
1721 | |
1722 | ASSERT(log_offset <= iclog->ic_size - 1); | |
1723 | ptr = (__psint_t) ((char *)iclog->ic_datap+log_offset); | |
1724 | ||
1725 | /* start_lsn is the first lsn written to. That's all we need. */ | |
1726 | if (! *start_lsn) | |
b53e675d | 1727 | *start_lsn = be64_to_cpu(iclog->ic_header.h_lsn); |
1da177e4 LT |
1728 | |
1729 | /* This loop writes out as many regions as can fit in the amount | |
1730 | * of space which was allocated by xlog_state_get_iclog_space(). | |
1731 | */ | |
1732 | while (index < nentries) { | |
1733 | ASSERT(reg[index].i_len % sizeof(__int32_t) == 0); | |
1734 | ASSERT((__psint_t)ptr % sizeof(__int32_t) == 0); | |
1735 | start_rec_copy = 0; | |
1736 | ||
1737 | /* If first write for transaction, insert start record. | |
1738 | * We can't be trying to commit if we are inited. We can't | |
1739 | * have any "partial_copy" if we are inited. | |
1740 | */ | |
1741 | if (ticket->t_flags & XLOG_TIC_INITED) { | |
1742 | logop_head = (xlog_op_header_t *)ptr; | |
67fcb7bf | 1743 | logop_head->oh_tid = cpu_to_be32(ticket->t_tid); |
1da177e4 LT |
1744 | logop_head->oh_clientid = ticket->t_clientid; |
1745 | logop_head->oh_len = 0; | |
1746 | logop_head->oh_flags = XLOG_START_TRANS; | |
1747 | logop_head->oh_res2 = 0; | |
1748 | ticket->t_flags &= ~XLOG_TIC_INITED; /* clear bit */ | |
1749 | record_cnt++; | |
1750 | ||
1751 | start_rec_copy = sizeof(xlog_op_header_t); | |
1752 | xlog_write_adv_cnt(ptr, len, log_offset, start_rec_copy); | |
1753 | } | |
1754 | ||
1755 | /* Copy log operation header directly into data section */ | |
1756 | logop_head = (xlog_op_header_t *)ptr; | |
67fcb7bf | 1757 | logop_head->oh_tid = cpu_to_be32(ticket->t_tid); |
1da177e4 LT |
1758 | logop_head->oh_clientid = ticket->t_clientid; |
1759 | logop_head->oh_res2 = 0; | |
1760 | ||
1761 | /* header copied directly */ | |
1762 | xlog_write_adv_cnt(ptr, len, log_offset, sizeof(xlog_op_header_t)); | |
1763 | ||
1764 | /* are we copying a commit or unmount record? */ | |
1765 | logop_head->oh_flags = flags; | |
1766 | ||
1767 | /* | |
1768 | * We've seen logs corrupted with bad transaction client | |
1769 | * ids. This makes sure that XFS doesn't generate them on. | |
1770 | * Turn this into an EIO and shut down the filesystem. | |
1771 | */ | |
1772 | switch (logop_head->oh_clientid) { | |
1773 | case XFS_TRANSACTION: | |
1774 | case XFS_VOLUME: | |
1775 | case XFS_LOG: | |
1776 | break; | |
1777 | default: | |
1778 | xfs_fs_cmn_err(CE_WARN, mp, | |
1779 | "Bad XFS transaction clientid 0x%x in ticket 0x%p", | |
35a8a72f | 1780 | logop_head->oh_clientid, ticket); |
1da177e4 LT |
1781 | return XFS_ERROR(EIO); |
1782 | } | |
1783 | ||
1784 | /* Partial write last time? => (partial_copy != 0) | |
1785 | * need_copy is the amount we'd like to copy if everything could | |
1786 | * fit in the current memcpy. | |
1787 | */ | |
1788 | need_copy = reg[index].i_len - partial_copy_len; | |
1789 | ||
1790 | copy_off = partial_copy_len; | |
1791 | if (need_copy <= iclog->ic_size - log_offset) { /*complete write */ | |
67fcb7bf CH |
1792 | copy_len = need_copy; |
1793 | logop_head->oh_len = cpu_to_be32(copy_len); | |
1da177e4 LT |
1794 | if (partial_copy) |
1795 | logop_head->oh_flags|= (XLOG_END_TRANS|XLOG_WAS_CONT_TRANS); | |
1796 | partial_copy_len = partial_copy = 0; | |
1797 | } else { /* partial write */ | |
1798 | copy_len = iclog->ic_size - log_offset; | |
67fcb7bf | 1799 | logop_head->oh_len = cpu_to_be32(copy_len); |
1da177e4 LT |
1800 | logop_head->oh_flags |= XLOG_CONTINUE_TRANS; |
1801 | if (partial_copy) | |
1802 | logop_head->oh_flags |= XLOG_WAS_CONT_TRANS; | |
1803 | partial_copy_len += copy_len; | |
1804 | partial_copy++; | |
1805 | len += sizeof(xlog_op_header_t); /* from splitting of region */ | |
1806 | /* account for new log op header */ | |
1807 | ticket->t_curr_res -= sizeof(xlog_op_header_t); | |
0adba536 | 1808 | ticket->t_res_num_ophdrs++; |
1da177e4 LT |
1809 | } |
1810 | xlog_verify_dest_ptr(log, ptr); | |
1811 | ||
1812 | /* copy region */ | |
1813 | ASSERT(copy_len >= 0); | |
1814 | memcpy((xfs_caddr_t)ptr, reg[index].i_addr + copy_off, copy_len); | |
1815 | xlog_write_adv_cnt(ptr, len, log_offset, copy_len); | |
1816 | ||
1817 | /* make copy_len total bytes copied, including headers */ | |
1818 | copy_len += start_rec_copy + sizeof(xlog_op_header_t); | |
1819 | record_cnt++; | |
1820 | data_cnt += contwr ? copy_len : 0; | |
1821 | if (partial_copy) { /* copied partial region */ | |
1822 | /* already marked WANT_SYNC by xlog_state_get_iclog_space */ | |
1823 | xlog_state_finish_copy(log, iclog, record_cnt, data_cnt); | |
1824 | record_cnt = data_cnt = 0; | |
1825 | if ((error = xlog_state_release_iclog(log, iclog))) | |
014c2544 | 1826 | return error; |
1da177e4 LT |
1827 | break; /* don't increment index */ |
1828 | } else { /* copied entire region */ | |
1829 | index++; | |
1830 | partial_copy_len = partial_copy = 0; | |
1831 | ||
1832 | if (iclog->ic_size - log_offset <= sizeof(xlog_op_header_t)) { | |
1833 | xlog_state_finish_copy(log, iclog, record_cnt, data_cnt); | |
1834 | record_cnt = data_cnt = 0; | |
39e2defe | 1835 | spin_lock(&log->l_icloglock); |
1da177e4 | 1836 | xlog_state_want_sync(log, iclog); |
39e2defe | 1837 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
1838 | if (commit_iclog) { |
1839 | ASSERT(flags & XLOG_COMMIT_TRANS); | |
1840 | *commit_iclog = iclog; | |
1841 | } else if ((error = xlog_state_release_iclog(log, iclog))) | |
014c2544 | 1842 | return error; |
1da177e4 LT |
1843 | if (index == nentries) |
1844 | return 0; /* we are done */ | |
1845 | else | |
1846 | break; | |
1847 | } | |
1848 | } /* if (partial_copy) */ | |
1849 | } /* while (index < nentries) */ | |
1850 | } /* for (index = 0; index < nentries; ) */ | |
1851 | ASSERT(len == 0); | |
1852 | ||
1853 | xlog_state_finish_copy(log, iclog, record_cnt, data_cnt); | |
1854 | if (commit_iclog) { | |
1855 | ASSERT(flags & XLOG_COMMIT_TRANS); | |
1856 | *commit_iclog = iclog; | |
1857 | return 0; | |
1858 | } | |
014c2544 | 1859 | return xlog_state_release_iclog(log, iclog); |
1da177e4 LT |
1860 | } /* xlog_write */ |
1861 | ||
1862 | ||
1863 | /***************************************************************************** | |
1864 | * | |
1865 | * State Machine functions | |
1866 | * | |
1867 | ***************************************************************************** | |
1868 | */ | |
1869 | ||
1870 | /* Clean iclogs starting from the head. This ordering must be | |
1871 | * maintained, so an iclog doesn't become ACTIVE beyond one that | |
1872 | * is SYNCING. This is also required to maintain the notion that we use | |
12017faf | 1873 | * a ordered wait queue to hold off would be writers to the log when every |
1da177e4 LT |
1874 | * iclog is trying to sync to disk. |
1875 | * | |
1876 | * State Change: DIRTY -> ACTIVE | |
1877 | */ | |
ba0f32d4 | 1878 | STATIC void |
1da177e4 LT |
1879 | xlog_state_clean_log(xlog_t *log) |
1880 | { | |
1881 | xlog_in_core_t *iclog; | |
1882 | int changed = 0; | |
1883 | ||
1884 | iclog = log->l_iclog; | |
1885 | do { | |
1886 | if (iclog->ic_state == XLOG_STATE_DIRTY) { | |
1887 | iclog->ic_state = XLOG_STATE_ACTIVE; | |
1888 | iclog->ic_offset = 0; | |
114d23aa | 1889 | ASSERT(iclog->ic_callback == NULL); |
1da177e4 LT |
1890 | /* |
1891 | * If the number of ops in this iclog indicate it just | |
1892 | * contains the dummy transaction, we can | |
1893 | * change state into IDLE (the second time around). | |
1894 | * Otherwise we should change the state into | |
1895 | * NEED a dummy. | |
1896 | * We don't need to cover the dummy. | |
1897 | */ | |
1898 | if (!changed && | |
b53e675d CH |
1899 | (be32_to_cpu(iclog->ic_header.h_num_logops) == |
1900 | XLOG_COVER_OPS)) { | |
1da177e4 LT |
1901 | changed = 1; |
1902 | } else { | |
1903 | /* | |
1904 | * We have two dirty iclogs so start over | |
1905 | * This could also be num of ops indicates | |
1906 | * this is not the dummy going out. | |
1907 | */ | |
1908 | changed = 2; | |
1909 | } | |
1910 | iclog->ic_header.h_num_logops = 0; | |
1911 | memset(iclog->ic_header.h_cycle_data, 0, | |
1912 | sizeof(iclog->ic_header.h_cycle_data)); | |
1913 | iclog->ic_header.h_lsn = 0; | |
1914 | } else if (iclog->ic_state == XLOG_STATE_ACTIVE) | |
1915 | /* do nothing */; | |
1916 | else | |
1917 | break; /* stop cleaning */ | |
1918 | iclog = iclog->ic_next; | |
1919 | } while (iclog != log->l_iclog); | |
1920 | ||
1921 | /* log is locked when we are called */ | |
1922 | /* | |
1923 | * Change state for the dummy log recording. | |
1924 | * We usually go to NEED. But we go to NEED2 if the changed indicates | |
1925 | * we are done writing the dummy record. | |
1926 | * If we are done with the second dummy recored (DONE2), then | |
1927 | * we go to IDLE. | |
1928 | */ | |
1929 | if (changed) { | |
1930 | switch (log->l_covered_state) { | |
1931 | case XLOG_STATE_COVER_IDLE: | |
1932 | case XLOG_STATE_COVER_NEED: | |
1933 | case XLOG_STATE_COVER_NEED2: | |
1934 | log->l_covered_state = XLOG_STATE_COVER_NEED; | |
1935 | break; | |
1936 | ||
1937 | case XLOG_STATE_COVER_DONE: | |
1938 | if (changed == 1) | |
1939 | log->l_covered_state = XLOG_STATE_COVER_NEED2; | |
1940 | else | |
1941 | log->l_covered_state = XLOG_STATE_COVER_NEED; | |
1942 | break; | |
1943 | ||
1944 | case XLOG_STATE_COVER_DONE2: | |
1945 | if (changed == 1) | |
1946 | log->l_covered_state = XLOG_STATE_COVER_IDLE; | |
1947 | else | |
1948 | log->l_covered_state = XLOG_STATE_COVER_NEED; | |
1949 | break; | |
1950 | ||
1951 | default: | |
1952 | ASSERT(0); | |
1953 | } | |
1954 | } | |
1955 | } /* xlog_state_clean_log */ | |
1956 | ||
1957 | STATIC xfs_lsn_t | |
1958 | xlog_get_lowest_lsn( | |
1959 | xlog_t *log) | |
1960 | { | |
1961 | xlog_in_core_t *lsn_log; | |
1962 | xfs_lsn_t lowest_lsn, lsn; | |
1963 | ||
1964 | lsn_log = log->l_iclog; | |
1965 | lowest_lsn = 0; | |
1966 | do { | |
1967 | if (!(lsn_log->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY))) { | |
b53e675d | 1968 | lsn = be64_to_cpu(lsn_log->ic_header.h_lsn); |
1da177e4 LT |
1969 | if ((lsn && !lowest_lsn) || |
1970 | (XFS_LSN_CMP(lsn, lowest_lsn) < 0)) { | |
1971 | lowest_lsn = lsn; | |
1972 | } | |
1973 | } | |
1974 | lsn_log = lsn_log->ic_next; | |
1975 | } while (lsn_log != log->l_iclog); | |
014c2544 | 1976 | return lowest_lsn; |
1da177e4 LT |
1977 | } |
1978 | ||
1979 | ||
1980 | STATIC void | |
1981 | xlog_state_do_callback( | |
1982 | xlog_t *log, | |
1983 | int aborted, | |
1984 | xlog_in_core_t *ciclog) | |
1985 | { | |
1986 | xlog_in_core_t *iclog; | |
1987 | xlog_in_core_t *first_iclog; /* used to know when we've | |
1988 | * processed all iclogs once */ | |
1989 | xfs_log_callback_t *cb, *cb_next; | |
1990 | int flushcnt = 0; | |
1991 | xfs_lsn_t lowest_lsn; | |
1992 | int ioerrors; /* counter: iclogs with errors */ | |
1993 | int loopdidcallbacks; /* flag: inner loop did callbacks*/ | |
1994 | int funcdidcallbacks; /* flag: function did callbacks */ | |
1995 | int repeats; /* for issuing console warnings if | |
1996 | * looping too many times */ | |
d748c623 | 1997 | int wake = 0; |
1da177e4 | 1998 | |
b22cd72c | 1999 | spin_lock(&log->l_icloglock); |
1da177e4 LT |
2000 | first_iclog = iclog = log->l_iclog; |
2001 | ioerrors = 0; | |
2002 | funcdidcallbacks = 0; | |
2003 | repeats = 0; | |
2004 | ||
2005 | do { | |
2006 | /* | |
2007 | * Scan all iclogs starting with the one pointed to by the | |
2008 | * log. Reset this starting point each time the log is | |
2009 | * unlocked (during callbacks). | |
2010 | * | |
2011 | * Keep looping through iclogs until one full pass is made | |
2012 | * without running any callbacks. | |
2013 | */ | |
2014 | first_iclog = log->l_iclog; | |
2015 | iclog = log->l_iclog; | |
2016 | loopdidcallbacks = 0; | |
2017 | repeats++; | |
2018 | ||
2019 | do { | |
2020 | ||
2021 | /* skip all iclogs in the ACTIVE & DIRTY states */ | |
2022 | if (iclog->ic_state & | |
2023 | (XLOG_STATE_ACTIVE|XLOG_STATE_DIRTY)) { | |
2024 | iclog = iclog->ic_next; | |
2025 | continue; | |
2026 | } | |
2027 | ||
2028 | /* | |
2029 | * Between marking a filesystem SHUTDOWN and stopping | |
2030 | * the log, we do flush all iclogs to disk (if there | |
2031 | * wasn't a log I/O error). So, we do want things to | |
2032 | * go smoothly in case of just a SHUTDOWN w/o a | |
2033 | * LOG_IO_ERROR. | |
2034 | */ | |
2035 | if (!(iclog->ic_state & XLOG_STATE_IOERROR)) { | |
2036 | /* | |
2037 | * Can only perform callbacks in order. Since | |
2038 | * this iclog is not in the DONE_SYNC/ | |
2039 | * DO_CALLBACK state, we skip the rest and | |
2040 | * just try to clean up. If we set our iclog | |
2041 | * to DO_CALLBACK, we will not process it when | |
2042 | * we retry since a previous iclog is in the | |
2043 | * CALLBACK and the state cannot change since | |
b22cd72c | 2044 | * we are holding the l_icloglock. |
1da177e4 LT |
2045 | */ |
2046 | if (!(iclog->ic_state & | |
2047 | (XLOG_STATE_DONE_SYNC | | |
2048 | XLOG_STATE_DO_CALLBACK))) { | |
2049 | if (ciclog && (ciclog->ic_state == | |
2050 | XLOG_STATE_DONE_SYNC)) { | |
2051 | ciclog->ic_state = XLOG_STATE_DO_CALLBACK; | |
2052 | } | |
2053 | break; | |
2054 | } | |
2055 | /* | |
2056 | * We now have an iclog that is in either the | |
2057 | * DO_CALLBACK or DONE_SYNC states. The other | |
2058 | * states (WANT_SYNC, SYNCING, or CALLBACK were | |
2059 | * caught by the above if and are going to | |
2060 | * clean (i.e. we aren't doing their callbacks) | |
2061 | * see the above if. | |
2062 | */ | |
2063 | ||
2064 | /* | |
2065 | * We will do one more check here to see if we | |
2066 | * have chased our tail around. | |
2067 | */ | |
2068 | ||
2069 | lowest_lsn = xlog_get_lowest_lsn(log); | |
b53e675d CH |
2070 | if (lowest_lsn && |
2071 | XFS_LSN_CMP(lowest_lsn, | |
2072 | be64_to_cpu(iclog->ic_header.h_lsn)) < 0) { | |
1da177e4 LT |
2073 | iclog = iclog->ic_next; |
2074 | continue; /* Leave this iclog for | |
2075 | * another thread */ | |
2076 | } | |
2077 | ||
2078 | iclog->ic_state = XLOG_STATE_CALLBACK; | |
2079 | ||
b22cd72c | 2080 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
2081 | |
2082 | /* l_last_sync_lsn field protected by | |
c8b5ea28 | 2083 | * l_grant_lock. Don't worry about iclog's lsn. |
1da177e4 LT |
2084 | * No one else can be here except us. |
2085 | */ | |
c8b5ea28 | 2086 | spin_lock(&log->l_grant_lock); |
b53e675d CH |
2087 | ASSERT(XFS_LSN_CMP(log->l_last_sync_lsn, |
2088 | be64_to_cpu(iclog->ic_header.h_lsn)) <= 0); | |
2089 | log->l_last_sync_lsn = | |
2090 | be64_to_cpu(iclog->ic_header.h_lsn); | |
c8b5ea28 | 2091 | spin_unlock(&log->l_grant_lock); |
1da177e4 | 2092 | |
1da177e4 | 2093 | } else { |
114d23aa | 2094 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
2095 | ioerrors++; |
2096 | } | |
1da177e4 | 2097 | |
114d23aa DC |
2098 | /* |
2099 | * Keep processing entries in the callback list until | |
2100 | * we come around and it is empty. We need to | |
2101 | * atomically see that the list is empty and change the | |
2102 | * state to DIRTY so that we don't miss any more | |
2103 | * callbacks being added. | |
2104 | */ | |
2105 | spin_lock(&iclog->ic_callback_lock); | |
2106 | cb = iclog->ic_callback; | |
4b80916b | 2107 | while (cb) { |
1da177e4 LT |
2108 | iclog->ic_callback_tail = &(iclog->ic_callback); |
2109 | iclog->ic_callback = NULL; | |
114d23aa | 2110 | spin_unlock(&iclog->ic_callback_lock); |
1da177e4 LT |
2111 | |
2112 | /* perform callbacks in the order given */ | |
4b80916b | 2113 | for (; cb; cb = cb_next) { |
1da177e4 LT |
2114 | cb_next = cb->cb_next; |
2115 | cb->cb_func(cb->cb_arg, aborted); | |
2116 | } | |
114d23aa | 2117 | spin_lock(&iclog->ic_callback_lock); |
1da177e4 LT |
2118 | cb = iclog->ic_callback; |
2119 | } | |
2120 | ||
2121 | loopdidcallbacks++; | |
2122 | funcdidcallbacks++; | |
2123 | ||
114d23aa | 2124 | spin_lock(&log->l_icloglock); |
4b80916b | 2125 | ASSERT(iclog->ic_callback == NULL); |
114d23aa | 2126 | spin_unlock(&iclog->ic_callback_lock); |
1da177e4 LT |
2127 | if (!(iclog->ic_state & XLOG_STATE_IOERROR)) |
2128 | iclog->ic_state = XLOG_STATE_DIRTY; | |
2129 | ||
2130 | /* | |
2131 | * Transition from DIRTY to ACTIVE if applicable. | |
2132 | * NOP if STATE_IOERROR. | |
2133 | */ | |
2134 | xlog_state_clean_log(log); | |
2135 | ||
2136 | /* wake up threads waiting in xfs_log_force() */ | |
12017faf | 2137 | sv_broadcast(&iclog->ic_force_wait); |
1da177e4 LT |
2138 | |
2139 | iclog = iclog->ic_next; | |
2140 | } while (first_iclog != iclog); | |
a3c6685e NS |
2141 | |
2142 | if (repeats > 5000) { | |
2143 | flushcnt += repeats; | |
2144 | repeats = 0; | |
1da177e4 | 2145 | xfs_fs_cmn_err(CE_WARN, log->l_mp, |
a3c6685e | 2146 | "%s: possible infinite loop (%d iterations)", |
34a622b2 | 2147 | __func__, flushcnt); |
1da177e4 LT |
2148 | } |
2149 | } while (!ioerrors && loopdidcallbacks); | |
2150 | ||
2151 | /* | |
2152 | * make one last gasp attempt to see if iclogs are being left in | |
2153 | * limbo.. | |
2154 | */ | |
2155 | #ifdef DEBUG | |
2156 | if (funcdidcallbacks) { | |
2157 | first_iclog = iclog = log->l_iclog; | |
2158 | do { | |
2159 | ASSERT(iclog->ic_state != XLOG_STATE_DO_CALLBACK); | |
2160 | /* | |
2161 | * Terminate the loop if iclogs are found in states | |
2162 | * which will cause other threads to clean up iclogs. | |
2163 | * | |
2164 | * SYNCING - i/o completion will go through logs | |
2165 | * DONE_SYNC - interrupt thread should be waiting for | |
b22cd72c | 2166 | * l_icloglock |
1da177e4 LT |
2167 | * IOERROR - give up hope all ye who enter here |
2168 | */ | |
2169 | if (iclog->ic_state == XLOG_STATE_WANT_SYNC || | |
2170 | iclog->ic_state == XLOG_STATE_SYNCING || | |
2171 | iclog->ic_state == XLOG_STATE_DONE_SYNC || | |
2172 | iclog->ic_state == XLOG_STATE_IOERROR ) | |
2173 | break; | |
2174 | iclog = iclog->ic_next; | |
2175 | } while (first_iclog != iclog); | |
2176 | } | |
2177 | #endif | |
2178 | ||
d748c623 MW |
2179 | if (log->l_iclog->ic_state & (XLOG_STATE_ACTIVE|XLOG_STATE_IOERROR)) |
2180 | wake = 1; | |
b22cd72c | 2181 | spin_unlock(&log->l_icloglock); |
d748c623 MW |
2182 | |
2183 | if (wake) | |
2184 | sv_broadcast(&log->l_flush_wait); | |
2185 | } | |
1da177e4 LT |
2186 | |
2187 | ||
2188 | /* | |
2189 | * Finish transitioning this iclog to the dirty state. | |
2190 | * | |
2191 | * Make sure that we completely execute this routine only when this is | |
2192 | * the last call to the iclog. There is a good chance that iclog flushes, | |
2193 | * when we reach the end of the physical log, get turned into 2 separate | |
2194 | * calls to bwrite. Hence, one iclog flush could generate two calls to this | |
2195 | * routine. By using the reference count bwritecnt, we guarantee that only | |
2196 | * the second completion goes through. | |
2197 | * | |
2198 | * Callbacks could take time, so they are done outside the scope of the | |
12017faf | 2199 | * global state machine log lock. |
1da177e4 | 2200 | */ |
a8272ce0 | 2201 | STATIC void |
1da177e4 LT |
2202 | xlog_state_done_syncing( |
2203 | xlog_in_core_t *iclog, | |
2204 | int aborted) | |
2205 | { | |
2206 | xlog_t *log = iclog->ic_log; | |
1da177e4 | 2207 | |
b22cd72c | 2208 | spin_lock(&log->l_icloglock); |
1da177e4 LT |
2209 | |
2210 | ASSERT(iclog->ic_state == XLOG_STATE_SYNCING || | |
2211 | iclog->ic_state == XLOG_STATE_IOERROR); | |
155cc6b7 | 2212 | ASSERT(atomic_read(&iclog->ic_refcnt) == 0); |
1da177e4 LT |
2213 | ASSERT(iclog->ic_bwritecnt == 1 || iclog->ic_bwritecnt == 2); |
2214 | ||
2215 | ||
2216 | /* | |
2217 | * If we got an error, either on the first buffer, or in the case of | |
2218 | * split log writes, on the second, we mark ALL iclogs STATE_IOERROR, | |
2219 | * and none should ever be attempted to be written to disk | |
2220 | * again. | |
2221 | */ | |
2222 | if (iclog->ic_state != XLOG_STATE_IOERROR) { | |
2223 | if (--iclog->ic_bwritecnt == 1) { | |
b22cd72c | 2224 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
2225 | return; |
2226 | } | |
2227 | iclog->ic_state = XLOG_STATE_DONE_SYNC; | |
2228 | } | |
2229 | ||
2230 | /* | |
2231 | * Someone could be sleeping prior to writing out the next | |
2232 | * iclog buffer, we wake them all, one will get to do the | |
2233 | * I/O, the others get to wait for the result. | |
2234 | */ | |
12017faf | 2235 | sv_broadcast(&iclog->ic_write_wait); |
b22cd72c | 2236 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
2237 | xlog_state_do_callback(log, aborted, iclog); /* also cleans log */ |
2238 | } /* xlog_state_done_syncing */ | |
2239 | ||
2240 | ||
2241 | /* | |
2242 | * If the head of the in-core log ring is not (ACTIVE or DIRTY), then we must | |
12017faf DC |
2243 | * sleep. We wait on the flush queue on the head iclog as that should be |
2244 | * the first iclog to complete flushing. Hence if all iclogs are syncing, | |
2245 | * we will wait here and all new writes will sleep until a sync completes. | |
1da177e4 LT |
2246 | * |
2247 | * The in-core logs are used in a circular fashion. They are not used | |
2248 | * out-of-order even when an iclog past the head is free. | |
2249 | * | |
2250 | * return: | |
2251 | * * log_offset where xlog_write() can start writing into the in-core | |
2252 | * log's data space. | |
2253 | * * in-core log pointer to which xlog_write() should write. | |
2254 | * * boolean indicating this is a continued write to an in-core log. | |
2255 | * If this is the last write, then the in-core log's offset field | |
2256 | * needs to be incremented, depending on the amount of data which | |
2257 | * is copied. | |
2258 | */ | |
a8272ce0 | 2259 | STATIC int |
1da177e4 LT |
2260 | xlog_state_get_iclog_space(xlog_t *log, |
2261 | int len, | |
2262 | xlog_in_core_t **iclogp, | |
2263 | xlog_ticket_t *ticket, | |
2264 | int *continued_write, | |
2265 | int *logoffsetp) | |
2266 | { | |
1da177e4 LT |
2267 | int log_offset; |
2268 | xlog_rec_header_t *head; | |
2269 | xlog_in_core_t *iclog; | |
2270 | int error; | |
2271 | ||
2272 | restart: | |
b22cd72c | 2273 | spin_lock(&log->l_icloglock); |
1da177e4 | 2274 | if (XLOG_FORCED_SHUTDOWN(log)) { |
b22cd72c | 2275 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
2276 | return XFS_ERROR(EIO); |
2277 | } | |
2278 | ||
2279 | iclog = log->l_iclog; | |
d748c623 | 2280 | if (iclog->ic_state != XLOG_STATE_ACTIVE) { |
1da177e4 | 2281 | XFS_STATS_INC(xs_log_noiclogs); |
d748c623 MW |
2282 | |
2283 | /* Wait for log writes to have flushed */ | |
2284 | sv_wait(&log->l_flush_wait, 0, &log->l_icloglock, 0); | |
1da177e4 LT |
2285 | goto restart; |
2286 | } | |
d748c623 | 2287 | |
1da177e4 LT |
2288 | head = &iclog->ic_header; |
2289 | ||
155cc6b7 | 2290 | atomic_inc(&iclog->ic_refcnt); /* prevents sync */ |
1da177e4 LT |
2291 | log_offset = iclog->ic_offset; |
2292 | ||
2293 | /* On the 1st write to an iclog, figure out lsn. This works | |
2294 | * if iclogs marked XLOG_STATE_WANT_SYNC always write out what they are | |
2295 | * committing to. If the offset is set, that's how many blocks | |
2296 | * must be written. | |
2297 | */ | |
2298 | if (log_offset == 0) { | |
2299 | ticket->t_curr_res -= log->l_iclog_hsize; | |
0adba536 | 2300 | xlog_tic_add_region(ticket, |
7e9c6396 TS |
2301 | log->l_iclog_hsize, |
2302 | XLOG_REG_TYPE_LRHEADER); | |
b53e675d CH |
2303 | head->h_cycle = cpu_to_be32(log->l_curr_cycle); |
2304 | head->h_lsn = cpu_to_be64( | |
03bea6fe | 2305 | xlog_assign_lsn(log->l_curr_cycle, log->l_curr_block)); |
1da177e4 LT |
2306 | ASSERT(log->l_curr_block >= 0); |
2307 | } | |
2308 | ||
2309 | /* If there is enough room to write everything, then do it. Otherwise, | |
2310 | * claim the rest of the region and make sure the XLOG_STATE_WANT_SYNC | |
2311 | * bit is on, so this will get flushed out. Don't update ic_offset | |
2312 | * until you know exactly how many bytes get copied. Therefore, wait | |
2313 | * until later to update ic_offset. | |
2314 | * | |
2315 | * xlog_write() algorithm assumes that at least 2 xlog_op_header_t's | |
2316 | * can fit into remaining data section. | |
2317 | */ | |
2318 | if (iclog->ic_size - iclog->ic_offset < 2*sizeof(xlog_op_header_t)) { | |
2319 | xlog_state_switch_iclogs(log, iclog, iclog->ic_size); | |
2320 | ||
49641f1a DC |
2321 | /* |
2322 | * If I'm the only one writing to this iclog, sync it to disk. | |
2323 | * We need to do an atomic compare and decrement here to avoid | |
2324 | * racing with concurrent atomic_dec_and_lock() calls in | |
2325 | * xlog_state_release_iclog() when there is more than one | |
2326 | * reference to the iclog. | |
2327 | */ | |
2328 | if (!atomic_add_unless(&iclog->ic_refcnt, -1, 1)) { | |
2329 | /* we are the only one */ | |
b22cd72c | 2330 | spin_unlock(&log->l_icloglock); |
49641f1a DC |
2331 | error = xlog_state_release_iclog(log, iclog); |
2332 | if (error) | |
014c2544 | 2333 | return error; |
1da177e4 | 2334 | } else { |
b22cd72c | 2335 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
2336 | } |
2337 | goto restart; | |
2338 | } | |
2339 | ||
2340 | /* Do we have enough room to write the full amount in the remainder | |
2341 | * of this iclog? Or must we continue a write on the next iclog and | |
2342 | * mark this iclog as completely taken? In the case where we switch | |
2343 | * iclogs (to mark it taken), this particular iclog will release/sync | |
2344 | * to disk in xlog_write(). | |
2345 | */ | |
2346 | if (len <= iclog->ic_size - iclog->ic_offset) { | |
2347 | *continued_write = 0; | |
2348 | iclog->ic_offset += len; | |
2349 | } else { | |
2350 | *continued_write = 1; | |
2351 | xlog_state_switch_iclogs(log, iclog, iclog->ic_size); | |
2352 | } | |
2353 | *iclogp = iclog; | |
2354 | ||
2355 | ASSERT(iclog->ic_offset <= iclog->ic_size); | |
b22cd72c | 2356 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
2357 | |
2358 | *logoffsetp = log_offset; | |
2359 | return 0; | |
2360 | } /* xlog_state_get_iclog_space */ | |
2361 | ||
2362 | /* | |
2363 | * Atomically get the log space required for a log ticket. | |
2364 | * | |
2365 | * Once a ticket gets put onto the reserveq, it will only return after | |
2366 | * the needed reservation is satisfied. | |
2367 | */ | |
2368 | STATIC int | |
2369 | xlog_grant_log_space(xlog_t *log, | |
2370 | xlog_ticket_t *tic) | |
2371 | { | |
2372 | int free_bytes; | |
2373 | int need_bytes; | |
1da177e4 LT |
2374 | #ifdef DEBUG |
2375 | xfs_lsn_t tail_lsn; | |
2376 | #endif | |
2377 | ||
2378 | ||
2379 | #ifdef DEBUG | |
2380 | if (log->l_flags & XLOG_ACTIVE_RECOVERY) | |
2381 | panic("grant Recovery problem"); | |
2382 | #endif | |
2383 | ||
2384 | /* Is there space or do we need to sleep? */ | |
c8b5ea28 | 2385 | spin_lock(&log->l_grant_lock); |
0b1b213f CH |
2386 | |
2387 | trace_xfs_log_grant_enter(log, tic); | |
1da177e4 LT |
2388 | |
2389 | /* something is already sleeping; insert new transaction at end */ | |
2390 | if (log->l_reserve_headq) { | |
dd954c69 | 2391 | xlog_ins_ticketq(&log->l_reserve_headq, tic); |
0b1b213f CH |
2392 | |
2393 | trace_xfs_log_grant_sleep1(log, tic); | |
2394 | ||
1da177e4 LT |
2395 | /* |
2396 | * Gotta check this before going to sleep, while we're | |
2397 | * holding the grant lock. | |
2398 | */ | |
2399 | if (XLOG_FORCED_SHUTDOWN(log)) | |
2400 | goto error_return; | |
2401 | ||
2402 | XFS_STATS_INC(xs_sleep_logspace); | |
12017faf | 2403 | sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s); |
1da177e4 LT |
2404 | /* |
2405 | * If we got an error, and the filesystem is shutting down, | |
2406 | * we'll catch it down below. So just continue... | |
2407 | */ | |
0b1b213f | 2408 | trace_xfs_log_grant_wake1(log, tic); |
c8b5ea28 | 2409 | spin_lock(&log->l_grant_lock); |
1da177e4 LT |
2410 | } |
2411 | if (tic->t_flags & XFS_LOG_PERM_RESERV) | |
2412 | need_bytes = tic->t_unit_res*tic->t_ocnt; | |
2413 | else | |
2414 | need_bytes = tic->t_unit_res; | |
2415 | ||
2416 | redo: | |
2417 | if (XLOG_FORCED_SHUTDOWN(log)) | |
2418 | goto error_return; | |
2419 | ||
2420 | free_bytes = xlog_space_left(log, log->l_grant_reserve_cycle, | |
2421 | log->l_grant_reserve_bytes); | |
2422 | if (free_bytes < need_bytes) { | |
2423 | if ((tic->t_flags & XLOG_TIC_IN_Q) == 0) | |
dd954c69 | 2424 | xlog_ins_ticketq(&log->l_reserve_headq, tic); |
0b1b213f CH |
2425 | |
2426 | trace_xfs_log_grant_sleep2(log, tic); | |
2427 | ||
9d7fef74 DC |
2428 | spin_unlock(&log->l_grant_lock); |
2429 | xlog_grant_push_ail(log->l_mp, need_bytes); | |
2430 | spin_lock(&log->l_grant_lock); | |
2431 | ||
1da177e4 | 2432 | XFS_STATS_INC(xs_sleep_logspace); |
12017faf | 2433 | sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s); |
1da177e4 | 2434 | |
9d7fef74 DC |
2435 | spin_lock(&log->l_grant_lock); |
2436 | if (XLOG_FORCED_SHUTDOWN(log)) | |
1da177e4 | 2437 | goto error_return; |
1da177e4 | 2438 | |
0b1b213f CH |
2439 | trace_xfs_log_grant_wake2(log, tic); |
2440 | ||
1da177e4 LT |
2441 | goto redo; |
2442 | } else if (tic->t_flags & XLOG_TIC_IN_Q) | |
dd954c69 | 2443 | xlog_del_ticketq(&log->l_reserve_headq, tic); |
1da177e4 LT |
2444 | |
2445 | /* we've got enough space */ | |
dd954c69 | 2446 | xlog_grant_add_space(log, need_bytes); |
1da177e4 LT |
2447 | #ifdef DEBUG |
2448 | tail_lsn = log->l_tail_lsn; | |
2449 | /* | |
2450 | * Check to make sure the grant write head didn't just over lap the | |
2451 | * tail. If the cycles are the same, we can't be overlapping. | |
2452 | * Otherwise, make sure that the cycles differ by exactly one and | |
2453 | * check the byte count. | |
2454 | */ | |
2455 | if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) { | |
2456 | ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn)); | |
2457 | ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn))); | |
2458 | } | |
2459 | #endif | |
0b1b213f | 2460 | trace_xfs_log_grant_exit(log, tic); |
1da177e4 | 2461 | xlog_verify_grant_head(log, 1); |
c8b5ea28 | 2462 | spin_unlock(&log->l_grant_lock); |
1da177e4 LT |
2463 | return 0; |
2464 | ||
2465 | error_return: | |
2466 | if (tic->t_flags & XLOG_TIC_IN_Q) | |
dd954c69 | 2467 | xlog_del_ticketq(&log->l_reserve_headq, tic); |
0b1b213f CH |
2468 | |
2469 | trace_xfs_log_grant_error(log, tic); | |
2470 | ||
1da177e4 LT |
2471 | /* |
2472 | * If we are failing, make sure the ticket doesn't have any | |
2473 | * current reservations. We don't want to add this back when | |
2474 | * the ticket/transaction gets cancelled. | |
2475 | */ | |
2476 | tic->t_curr_res = 0; | |
2477 | tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */ | |
c8b5ea28 | 2478 | spin_unlock(&log->l_grant_lock); |
1da177e4 LT |
2479 | return XFS_ERROR(EIO); |
2480 | } /* xlog_grant_log_space */ | |
2481 | ||
2482 | ||
2483 | /* | |
2484 | * Replenish the byte reservation required by moving the grant write head. | |
2485 | * | |
2486 | * | |
2487 | */ | |
2488 | STATIC int | |
2489 | xlog_regrant_write_log_space(xlog_t *log, | |
2490 | xlog_ticket_t *tic) | |
2491 | { | |
1da177e4 LT |
2492 | int free_bytes, need_bytes; |
2493 | xlog_ticket_t *ntic; | |
2494 | #ifdef DEBUG | |
2495 | xfs_lsn_t tail_lsn; | |
2496 | #endif | |
2497 | ||
2498 | tic->t_curr_res = tic->t_unit_res; | |
0adba536 | 2499 | xlog_tic_reset_res(tic); |
1da177e4 LT |
2500 | |
2501 | if (tic->t_cnt > 0) | |
014c2544 | 2502 | return 0; |
1da177e4 LT |
2503 | |
2504 | #ifdef DEBUG | |
2505 | if (log->l_flags & XLOG_ACTIVE_RECOVERY) | |
2506 | panic("regrant Recovery problem"); | |
2507 | #endif | |
2508 | ||
c8b5ea28 | 2509 | spin_lock(&log->l_grant_lock); |
0b1b213f CH |
2510 | |
2511 | trace_xfs_log_regrant_write_enter(log, tic); | |
1da177e4 LT |
2512 | |
2513 | if (XLOG_FORCED_SHUTDOWN(log)) | |
2514 | goto error_return; | |
2515 | ||
2516 | /* If there are other waiters on the queue then give them a | |
2517 | * chance at logspace before us. Wake up the first waiters, | |
2518 | * if we do not wake up all the waiters then go to sleep waiting | |
2519 | * for more free space, otherwise try to get some space for | |
2520 | * this transaction. | |
2521 | */ | |
9d7fef74 | 2522 | need_bytes = tic->t_unit_res; |
1da177e4 LT |
2523 | if ((ntic = log->l_write_headq)) { |
2524 | free_bytes = xlog_space_left(log, log->l_grant_write_cycle, | |
2525 | log->l_grant_write_bytes); | |
2526 | do { | |
2527 | ASSERT(ntic->t_flags & XLOG_TIC_PERM_RESERV); | |
2528 | ||
2529 | if (free_bytes < ntic->t_unit_res) | |
2530 | break; | |
2531 | free_bytes -= ntic->t_unit_res; | |
12017faf | 2532 | sv_signal(&ntic->t_wait); |
1da177e4 LT |
2533 | ntic = ntic->t_next; |
2534 | } while (ntic != log->l_write_headq); | |
2535 | ||
2536 | if (ntic != log->l_write_headq) { | |
2537 | if ((tic->t_flags & XLOG_TIC_IN_Q) == 0) | |
dd954c69 | 2538 | xlog_ins_ticketq(&log->l_write_headq, tic); |
1da177e4 | 2539 | |
0b1b213f CH |
2540 | trace_xfs_log_regrant_write_sleep1(log, tic); |
2541 | ||
9d7fef74 DC |
2542 | spin_unlock(&log->l_grant_lock); |
2543 | xlog_grant_push_ail(log->l_mp, need_bytes); | |
2544 | spin_lock(&log->l_grant_lock); | |
2545 | ||
1da177e4 | 2546 | XFS_STATS_INC(xs_sleep_logspace); |
12017faf | 2547 | sv_wait(&tic->t_wait, PINOD|PLTWAIT, |
1da177e4 LT |
2548 | &log->l_grant_lock, s); |
2549 | ||
2550 | /* If we're shutting down, this tic is already | |
2551 | * off the queue */ | |
9d7fef74 DC |
2552 | spin_lock(&log->l_grant_lock); |
2553 | if (XLOG_FORCED_SHUTDOWN(log)) | |
1da177e4 | 2554 | goto error_return; |
1da177e4 | 2555 | |
0b1b213f | 2556 | trace_xfs_log_regrant_write_wake1(log, tic); |
1da177e4 LT |
2557 | } |
2558 | } | |
2559 | ||
1da177e4 LT |
2560 | redo: |
2561 | if (XLOG_FORCED_SHUTDOWN(log)) | |
2562 | goto error_return; | |
2563 | ||
2564 | free_bytes = xlog_space_left(log, log->l_grant_write_cycle, | |
2565 | log->l_grant_write_bytes); | |
2566 | if (free_bytes < need_bytes) { | |
2567 | if ((tic->t_flags & XLOG_TIC_IN_Q) == 0) | |
dd954c69 | 2568 | xlog_ins_ticketq(&log->l_write_headq, tic); |
9d7fef74 DC |
2569 | spin_unlock(&log->l_grant_lock); |
2570 | xlog_grant_push_ail(log->l_mp, need_bytes); | |
2571 | spin_lock(&log->l_grant_lock); | |
2572 | ||
1da177e4 | 2573 | XFS_STATS_INC(xs_sleep_logspace); |
0b1b213f CH |
2574 | trace_xfs_log_regrant_write_sleep2(log, tic); |
2575 | ||
12017faf | 2576 | sv_wait(&tic->t_wait, PINOD|PLTWAIT, &log->l_grant_lock, s); |
1da177e4 LT |
2577 | |
2578 | /* If we're shutting down, this tic is already off the queue */ | |
9d7fef74 DC |
2579 | spin_lock(&log->l_grant_lock); |
2580 | if (XLOG_FORCED_SHUTDOWN(log)) | |
1da177e4 | 2581 | goto error_return; |
1da177e4 | 2582 | |
0b1b213f | 2583 | trace_xfs_log_regrant_write_wake2(log, tic); |
1da177e4 LT |
2584 | goto redo; |
2585 | } else if (tic->t_flags & XLOG_TIC_IN_Q) | |
dd954c69 | 2586 | xlog_del_ticketq(&log->l_write_headq, tic); |
1da177e4 | 2587 | |
dd954c69 CH |
2588 | /* we've got enough space */ |
2589 | xlog_grant_add_space_write(log, need_bytes); | |
1da177e4 LT |
2590 | #ifdef DEBUG |
2591 | tail_lsn = log->l_tail_lsn; | |
2592 | if (CYCLE_LSN(tail_lsn) != log->l_grant_write_cycle) { | |
2593 | ASSERT(log->l_grant_write_cycle-1 == CYCLE_LSN(tail_lsn)); | |
2594 | ASSERT(log->l_grant_write_bytes <= BBTOB(BLOCK_LSN(tail_lsn))); | |
2595 | } | |
2596 | #endif | |
2597 | ||
0b1b213f CH |
2598 | trace_xfs_log_regrant_write_exit(log, tic); |
2599 | ||
1da177e4 | 2600 | xlog_verify_grant_head(log, 1); |
c8b5ea28 | 2601 | spin_unlock(&log->l_grant_lock); |
014c2544 | 2602 | return 0; |
1da177e4 LT |
2603 | |
2604 | ||
2605 | error_return: | |
2606 | if (tic->t_flags & XLOG_TIC_IN_Q) | |
dd954c69 | 2607 | xlog_del_ticketq(&log->l_reserve_headq, tic); |
0b1b213f CH |
2608 | |
2609 | trace_xfs_log_regrant_write_error(log, tic); | |
2610 | ||
1da177e4 LT |
2611 | /* |
2612 | * If we are failing, make sure the ticket doesn't have any | |
2613 | * current reservations. We don't want to add this back when | |
2614 | * the ticket/transaction gets cancelled. | |
2615 | */ | |
2616 | tic->t_curr_res = 0; | |
2617 | tic->t_cnt = 0; /* ungrant will give back unit_res * t_cnt. */ | |
c8b5ea28 | 2618 | spin_unlock(&log->l_grant_lock); |
1da177e4 LT |
2619 | return XFS_ERROR(EIO); |
2620 | } /* xlog_regrant_write_log_space */ | |
2621 | ||
2622 | ||
2623 | /* The first cnt-1 times through here we don't need to | |
2624 | * move the grant write head because the permanent | |
2625 | * reservation has reserved cnt times the unit amount. | |
2626 | * Release part of current permanent unit reservation and | |
2627 | * reset current reservation to be one units worth. Also | |
2628 | * move grant reservation head forward. | |
2629 | */ | |
2630 | STATIC void | |
2631 | xlog_regrant_reserve_log_space(xlog_t *log, | |
2632 | xlog_ticket_t *ticket) | |
2633 | { | |
0b1b213f CH |
2634 | trace_xfs_log_regrant_reserve_enter(log, ticket); |
2635 | ||
1da177e4 LT |
2636 | if (ticket->t_cnt > 0) |
2637 | ticket->t_cnt--; | |
2638 | ||
c8b5ea28 | 2639 | spin_lock(&log->l_grant_lock); |
dd954c69 | 2640 | xlog_grant_sub_space(log, ticket->t_curr_res); |
1da177e4 | 2641 | ticket->t_curr_res = ticket->t_unit_res; |
0adba536 | 2642 | xlog_tic_reset_res(ticket); |
0b1b213f CH |
2643 | |
2644 | trace_xfs_log_regrant_reserve_sub(log, ticket); | |
2645 | ||
1da177e4 LT |
2646 | xlog_verify_grant_head(log, 1); |
2647 | ||
2648 | /* just return if we still have some of the pre-reserved space */ | |
2649 | if (ticket->t_cnt > 0) { | |
c8b5ea28 | 2650 | spin_unlock(&log->l_grant_lock); |
1da177e4 LT |
2651 | return; |
2652 | } | |
2653 | ||
dd954c69 | 2654 | xlog_grant_add_space_reserve(log, ticket->t_unit_res); |
0b1b213f CH |
2655 | |
2656 | trace_xfs_log_regrant_reserve_exit(log, ticket); | |
2657 | ||
1da177e4 | 2658 | xlog_verify_grant_head(log, 0); |
c8b5ea28 | 2659 | spin_unlock(&log->l_grant_lock); |
1da177e4 | 2660 | ticket->t_curr_res = ticket->t_unit_res; |
0adba536 | 2661 | xlog_tic_reset_res(ticket); |
1da177e4 LT |
2662 | } /* xlog_regrant_reserve_log_space */ |
2663 | ||
2664 | ||
2665 | /* | |
2666 | * Give back the space left from a reservation. | |
2667 | * | |
2668 | * All the information we need to make a correct determination of space left | |
2669 | * is present. For non-permanent reservations, things are quite easy. The | |
2670 | * count should have been decremented to zero. We only need to deal with the | |
2671 | * space remaining in the current reservation part of the ticket. If the | |
2672 | * ticket contains a permanent reservation, there may be left over space which | |
2673 | * needs to be released. A count of N means that N-1 refills of the current | |
2674 | * reservation can be done before we need to ask for more space. The first | |
2675 | * one goes to fill up the first current reservation. Once we run out of | |
2676 | * space, the count will stay at zero and the only space remaining will be | |
2677 | * in the current reservation field. | |
2678 | */ | |
2679 | STATIC void | |
2680 | xlog_ungrant_log_space(xlog_t *log, | |
2681 | xlog_ticket_t *ticket) | |
2682 | { | |
1da177e4 LT |
2683 | if (ticket->t_cnt > 0) |
2684 | ticket->t_cnt--; | |
2685 | ||
c8b5ea28 | 2686 | spin_lock(&log->l_grant_lock); |
0b1b213f | 2687 | trace_xfs_log_ungrant_enter(log, ticket); |
1da177e4 | 2688 | |
dd954c69 | 2689 | xlog_grant_sub_space(log, ticket->t_curr_res); |
1da177e4 | 2690 | |
0b1b213f | 2691 | trace_xfs_log_ungrant_sub(log, ticket); |
1da177e4 LT |
2692 | |
2693 | /* If this is a permanent reservation ticket, we may be able to free | |
2694 | * up more space based on the remaining count. | |
2695 | */ | |
2696 | if (ticket->t_cnt > 0) { | |
2697 | ASSERT(ticket->t_flags & XLOG_TIC_PERM_RESERV); | |
dd954c69 | 2698 | xlog_grant_sub_space(log, ticket->t_unit_res*ticket->t_cnt); |
1da177e4 LT |
2699 | } |
2700 | ||
0b1b213f CH |
2701 | trace_xfs_log_ungrant_exit(log, ticket); |
2702 | ||
1da177e4 | 2703 | xlog_verify_grant_head(log, 1); |
c8b5ea28 | 2704 | spin_unlock(&log->l_grant_lock); |
1da177e4 LT |
2705 | xfs_log_move_tail(log->l_mp, 1); |
2706 | } /* xlog_ungrant_log_space */ | |
2707 | ||
2708 | ||
1da177e4 LT |
2709 | /* |
2710 | * Flush iclog to disk if this is the last reference to the given iclog and | |
2711 | * the WANT_SYNC bit is set. | |
2712 | * | |
2713 | * When this function is entered, the iclog is not necessarily in the | |
2714 | * WANT_SYNC state. It may be sitting around waiting to get filled. | |
2715 | * | |
2716 | * | |
2717 | */ | |
a8272ce0 | 2718 | STATIC int |
b589334c DC |
2719 | xlog_state_release_iclog( |
2720 | xlog_t *log, | |
2721 | xlog_in_core_t *iclog) | |
1da177e4 | 2722 | { |
1da177e4 LT |
2723 | int sync = 0; /* do we sync? */ |
2724 | ||
155cc6b7 DC |
2725 | if (iclog->ic_state & XLOG_STATE_IOERROR) |
2726 | return XFS_ERROR(EIO); | |
2727 | ||
2728 | ASSERT(atomic_read(&iclog->ic_refcnt) > 0); | |
2729 | if (!atomic_dec_and_lock(&iclog->ic_refcnt, &log->l_icloglock)) | |
2730 | return 0; | |
2731 | ||
1da177e4 | 2732 | if (iclog->ic_state & XLOG_STATE_IOERROR) { |
b22cd72c | 2733 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
2734 | return XFS_ERROR(EIO); |
2735 | } | |
1da177e4 LT |
2736 | ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE || |
2737 | iclog->ic_state == XLOG_STATE_WANT_SYNC); | |
2738 | ||
155cc6b7 | 2739 | if (iclog->ic_state == XLOG_STATE_WANT_SYNC) { |
b589334c DC |
2740 | /* update tail before writing to iclog */ |
2741 | xlog_assign_tail_lsn(log->l_mp); | |
1da177e4 LT |
2742 | sync++; |
2743 | iclog->ic_state = XLOG_STATE_SYNCING; | |
b53e675d | 2744 | iclog->ic_header.h_tail_lsn = cpu_to_be64(log->l_tail_lsn); |
1da177e4 LT |
2745 | xlog_verify_tail_lsn(log, iclog, log->l_tail_lsn); |
2746 | /* cycle incremented when incrementing curr_block */ | |
2747 | } | |
b22cd72c | 2748 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
2749 | |
2750 | /* | |
2751 | * We let the log lock go, so it's possible that we hit a log I/O | |
c41564b5 | 2752 | * error or some other SHUTDOWN condition that marks the iclog |
1da177e4 LT |
2753 | * as XLOG_STATE_IOERROR before the bwrite. However, we know that |
2754 | * this iclog has consistent data, so we ignore IOERROR | |
2755 | * flags after this point. | |
2756 | */ | |
b589334c | 2757 | if (sync) |
1da177e4 | 2758 | return xlog_sync(log, iclog); |
014c2544 | 2759 | return 0; |
1da177e4 LT |
2760 | } /* xlog_state_release_iclog */ |
2761 | ||
2762 | ||
2763 | /* | |
2764 | * This routine will mark the current iclog in the ring as WANT_SYNC | |
2765 | * and move the current iclog pointer to the next iclog in the ring. | |
2766 | * When this routine is called from xlog_state_get_iclog_space(), the | |
2767 | * exact size of the iclog has not yet been determined. All we know is | |
2768 | * that every data block. We have run out of space in this log record. | |
2769 | */ | |
2770 | STATIC void | |
2771 | xlog_state_switch_iclogs(xlog_t *log, | |
2772 | xlog_in_core_t *iclog, | |
2773 | int eventual_size) | |
2774 | { | |
2775 | ASSERT(iclog->ic_state == XLOG_STATE_ACTIVE); | |
2776 | if (!eventual_size) | |
2777 | eventual_size = iclog->ic_offset; | |
2778 | iclog->ic_state = XLOG_STATE_WANT_SYNC; | |
b53e675d | 2779 | iclog->ic_header.h_prev_block = cpu_to_be32(log->l_prev_block); |
1da177e4 LT |
2780 | log->l_prev_block = log->l_curr_block; |
2781 | log->l_prev_cycle = log->l_curr_cycle; | |
2782 | ||
2783 | /* roll log?: ic_offset changed later */ | |
2784 | log->l_curr_block += BTOBB(eventual_size)+BTOBB(log->l_iclog_hsize); | |
2785 | ||
2786 | /* Round up to next log-sunit */ | |
62118709 | 2787 | if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) && |
1da177e4 LT |
2788 | log->l_mp->m_sb.sb_logsunit > 1) { |
2789 | __uint32_t sunit_bb = BTOBB(log->l_mp->m_sb.sb_logsunit); | |
2790 | log->l_curr_block = roundup(log->l_curr_block, sunit_bb); | |
2791 | } | |
2792 | ||
2793 | if (log->l_curr_block >= log->l_logBBsize) { | |
2794 | log->l_curr_cycle++; | |
2795 | if (log->l_curr_cycle == XLOG_HEADER_MAGIC_NUM) | |
2796 | log->l_curr_cycle++; | |
2797 | log->l_curr_block -= log->l_logBBsize; | |
2798 | ASSERT(log->l_curr_block >= 0); | |
2799 | } | |
2800 | ASSERT(iclog == log->l_iclog); | |
2801 | log->l_iclog = iclog->ic_next; | |
2802 | } /* xlog_state_switch_iclogs */ | |
2803 | ||
1da177e4 LT |
2804 | /* |
2805 | * Write out all data in the in-core log as of this exact moment in time. | |
2806 | * | |
2807 | * Data may be written to the in-core log during this call. However, | |
2808 | * we don't guarantee this data will be written out. A change from past | |
2809 | * implementation means this routine will *not* write out zero length LRs. | |
2810 | * | |
2811 | * Basically, we try and perform an intelligent scan of the in-core logs. | |
2812 | * If we determine there is no flushable data, we just return. There is no | |
2813 | * flushable data if: | |
2814 | * | |
2815 | * 1. the current iclog is active and has no data; the previous iclog | |
2816 | * is in the active or dirty state. | |
2817 | * 2. the current iclog is drity, and the previous iclog is in the | |
2818 | * active or dirty state. | |
2819 | * | |
12017faf | 2820 | * We may sleep if: |
1da177e4 LT |
2821 | * |
2822 | * 1. the current iclog is not in the active nor dirty state. | |
2823 | * 2. the current iclog dirty, and the previous iclog is not in the | |
2824 | * active nor dirty state. | |
2825 | * 3. the current iclog is active, and there is another thread writing | |
2826 | * to this particular iclog. | |
2827 | * 4. a) the current iclog is active and has no other writers | |
2828 | * b) when we return from flushing out this iclog, it is still | |
2829 | * not in the active nor dirty state. | |
2830 | */ | |
a14a348b CH |
2831 | int |
2832 | _xfs_log_force( | |
2833 | struct xfs_mount *mp, | |
2834 | uint flags, | |
2835 | int *log_flushed) | |
1da177e4 | 2836 | { |
a14a348b CH |
2837 | struct log *log = mp->m_log; |
2838 | struct xlog_in_core *iclog; | |
2839 | xfs_lsn_t lsn; | |
2840 | ||
2841 | XFS_STATS_INC(xs_log_force); | |
1da177e4 | 2842 | |
b22cd72c | 2843 | spin_lock(&log->l_icloglock); |
1da177e4 LT |
2844 | |
2845 | iclog = log->l_iclog; | |
2846 | if (iclog->ic_state & XLOG_STATE_IOERROR) { | |
b22cd72c | 2847 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
2848 | return XFS_ERROR(EIO); |
2849 | } | |
2850 | ||
2851 | /* If the head iclog is not active nor dirty, we just attach | |
2852 | * ourselves to the head and go to sleep. | |
2853 | */ | |
2854 | if (iclog->ic_state == XLOG_STATE_ACTIVE || | |
2855 | iclog->ic_state == XLOG_STATE_DIRTY) { | |
2856 | /* | |
2857 | * If the head is dirty or (active and empty), then | |
2858 | * we need to look at the previous iclog. If the previous | |
2859 | * iclog is active or dirty we are done. There is nothing | |
2860 | * to sync out. Otherwise, we attach ourselves to the | |
2861 | * previous iclog and go to sleep. | |
2862 | */ | |
2863 | if (iclog->ic_state == XLOG_STATE_DIRTY || | |
155cc6b7 DC |
2864 | (atomic_read(&iclog->ic_refcnt) == 0 |
2865 | && iclog->ic_offset == 0)) { | |
1da177e4 LT |
2866 | iclog = iclog->ic_prev; |
2867 | if (iclog->ic_state == XLOG_STATE_ACTIVE || | |
2868 | iclog->ic_state == XLOG_STATE_DIRTY) | |
2869 | goto no_sleep; | |
2870 | else | |
2871 | goto maybe_sleep; | |
2872 | } else { | |
155cc6b7 | 2873 | if (atomic_read(&iclog->ic_refcnt) == 0) { |
1da177e4 LT |
2874 | /* We are the only one with access to this |
2875 | * iclog. Flush it out now. There should | |
2876 | * be a roundoff of zero to show that someone | |
2877 | * has already taken care of the roundoff from | |
2878 | * the previous sync. | |
2879 | */ | |
155cc6b7 | 2880 | atomic_inc(&iclog->ic_refcnt); |
b53e675d | 2881 | lsn = be64_to_cpu(iclog->ic_header.h_lsn); |
1da177e4 | 2882 | xlog_state_switch_iclogs(log, iclog, 0); |
b22cd72c | 2883 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
2884 | |
2885 | if (xlog_state_release_iclog(log, iclog)) | |
2886 | return XFS_ERROR(EIO); | |
a14a348b CH |
2887 | |
2888 | if (log_flushed) | |
2889 | *log_flushed = 1; | |
b22cd72c | 2890 | spin_lock(&log->l_icloglock); |
b53e675d | 2891 | if (be64_to_cpu(iclog->ic_header.h_lsn) == lsn && |
1da177e4 LT |
2892 | iclog->ic_state != XLOG_STATE_DIRTY) |
2893 | goto maybe_sleep; | |
2894 | else | |
2895 | goto no_sleep; | |
2896 | } else { | |
2897 | /* Someone else is writing to this iclog. | |
2898 | * Use its call to flush out the data. However, | |
2899 | * the other thread may not force out this LR, | |
2900 | * so we mark it WANT_SYNC. | |
2901 | */ | |
2902 | xlog_state_switch_iclogs(log, iclog, 0); | |
2903 | goto maybe_sleep; | |
2904 | } | |
2905 | } | |
2906 | } | |
2907 | ||
2908 | /* By the time we come around again, the iclog could've been filled | |
2909 | * which would give it another lsn. If we have a new lsn, just | |
2910 | * return because the relevant data has been flushed. | |
2911 | */ | |
2912 | maybe_sleep: | |
2913 | if (flags & XFS_LOG_SYNC) { | |
2914 | /* | |
2915 | * We must check if we're shutting down here, before | |
b22cd72c | 2916 | * we wait, while we're holding the l_icloglock. |
1da177e4 LT |
2917 | * Then we check again after waking up, in case our |
2918 | * sleep was disturbed by a bad news. | |
2919 | */ | |
2920 | if (iclog->ic_state & XLOG_STATE_IOERROR) { | |
b22cd72c | 2921 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
2922 | return XFS_ERROR(EIO); |
2923 | } | |
2924 | XFS_STATS_INC(xs_log_force_sleep); | |
12017faf | 2925 | sv_wait(&iclog->ic_force_wait, PINOD, &log->l_icloglock, s); |
1da177e4 LT |
2926 | /* |
2927 | * No need to grab the log lock here since we're | |
2928 | * only deciding whether or not to return EIO | |
2929 | * and the memory read should be atomic. | |
2930 | */ | |
2931 | if (iclog->ic_state & XLOG_STATE_IOERROR) | |
2932 | return XFS_ERROR(EIO); | |
a14a348b CH |
2933 | if (log_flushed) |
2934 | *log_flushed = 1; | |
1da177e4 LT |
2935 | } else { |
2936 | ||
2937 | no_sleep: | |
b22cd72c | 2938 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
2939 | } |
2940 | return 0; | |
a14a348b | 2941 | } |
1da177e4 | 2942 | |
a14a348b CH |
2943 | /* |
2944 | * Wrapper for _xfs_log_force(), to be used when caller doesn't care | |
2945 | * about errors or whether the log was flushed or not. This is the normal | |
2946 | * interface to use when trying to unpin items or move the log forward. | |
2947 | */ | |
2948 | void | |
2949 | xfs_log_force( | |
2950 | xfs_mount_t *mp, | |
2951 | uint flags) | |
2952 | { | |
2953 | int error; | |
2954 | ||
2955 | error = _xfs_log_force(mp, flags, NULL); | |
2956 | if (error) { | |
2957 | xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: " | |
2958 | "error %d returned.", error); | |
2959 | } | |
2960 | } | |
1da177e4 LT |
2961 | |
2962 | /* | |
a14a348b | 2963 | * Force the in-core log to disk for a specific LSN. |
1da177e4 LT |
2964 | * |
2965 | * Find in-core log with lsn. | |
2966 | * If it is in the DIRTY state, just return. | |
2967 | * If it is in the ACTIVE state, move the in-core log into the WANT_SYNC | |
2968 | * state and go to sleep or return. | |
2969 | * If it is in any other state, go to sleep or return. | |
2970 | * | |
a14a348b CH |
2971 | * Synchronous forces are implemented with a signal variable. All callers |
2972 | * to force a given lsn to disk will wait on a the sv attached to the | |
2973 | * specific in-core log. When given in-core log finally completes its | |
2974 | * write to disk, that thread will wake up all threads waiting on the | |
2975 | * sv. | |
1da177e4 | 2976 | */ |
a14a348b CH |
2977 | int |
2978 | _xfs_log_force_lsn( | |
2979 | struct xfs_mount *mp, | |
2980 | xfs_lsn_t lsn, | |
2981 | uint flags, | |
2982 | int *log_flushed) | |
1da177e4 | 2983 | { |
a14a348b CH |
2984 | struct log *log = mp->m_log; |
2985 | struct xlog_in_core *iclog; | |
2986 | int already_slept = 0; | |
1da177e4 | 2987 | |
a14a348b | 2988 | ASSERT(lsn != 0); |
1da177e4 | 2989 | |
a14a348b | 2990 | XFS_STATS_INC(xs_log_force); |
1da177e4 | 2991 | |
a14a348b CH |
2992 | try_again: |
2993 | spin_lock(&log->l_icloglock); | |
2994 | iclog = log->l_iclog; | |
2995 | if (iclog->ic_state & XLOG_STATE_IOERROR) { | |
b22cd72c | 2996 | spin_unlock(&log->l_icloglock); |
a14a348b | 2997 | return XFS_ERROR(EIO); |
1da177e4 LT |
2998 | } |
2999 | ||
a14a348b CH |
3000 | do { |
3001 | if (be64_to_cpu(iclog->ic_header.h_lsn) != lsn) { | |
3002 | iclog = iclog->ic_next; | |
3003 | continue; | |
3004 | } | |
3005 | ||
3006 | if (iclog->ic_state == XLOG_STATE_DIRTY) { | |
3007 | spin_unlock(&log->l_icloglock); | |
3008 | return 0; | |
3009 | } | |
3010 | ||
3011 | if (iclog->ic_state == XLOG_STATE_ACTIVE) { | |
3012 | /* | |
3013 | * We sleep here if we haven't already slept (e.g. | |
3014 | * this is the first time we've looked at the correct | |
3015 | * iclog buf) and the buffer before us is going to | |
3016 | * be sync'ed. The reason for this is that if we | |
3017 | * are doing sync transactions here, by waiting for | |
3018 | * the previous I/O to complete, we can allow a few | |
3019 | * more transactions into this iclog before we close | |
3020 | * it down. | |
3021 | * | |
3022 | * Otherwise, we mark the buffer WANT_SYNC, and bump | |
3023 | * up the refcnt so we can release the log (which | |
3024 | * drops the ref count). The state switch keeps new | |
3025 | * transaction commits from using this buffer. When | |
3026 | * the current commits finish writing into the buffer, | |
3027 | * the refcount will drop to zero and the buffer will | |
3028 | * go out then. | |
3029 | */ | |
3030 | if (!already_slept && | |
3031 | (iclog->ic_prev->ic_state & | |
3032 | (XLOG_STATE_WANT_SYNC | XLOG_STATE_SYNCING))) { | |
3033 | ASSERT(!(iclog->ic_state & XLOG_STATE_IOERROR)); | |
3034 | ||
3035 | XFS_STATS_INC(xs_log_force_sleep); | |
3036 | ||
3037 | sv_wait(&iclog->ic_prev->ic_write_wait, | |
3038 | PSWP, &log->l_icloglock, s); | |
3039 | if (log_flushed) | |
3040 | *log_flushed = 1; | |
3041 | already_slept = 1; | |
3042 | goto try_again; | |
3043 | } | |
155cc6b7 | 3044 | atomic_inc(&iclog->ic_refcnt); |
1da177e4 | 3045 | xlog_state_switch_iclogs(log, iclog, 0); |
b22cd72c | 3046 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
3047 | if (xlog_state_release_iclog(log, iclog)) |
3048 | return XFS_ERROR(EIO); | |
a14a348b CH |
3049 | if (log_flushed) |
3050 | *log_flushed = 1; | |
b22cd72c | 3051 | spin_lock(&log->l_icloglock); |
1da177e4 | 3052 | } |
1da177e4 | 3053 | |
a14a348b CH |
3054 | if ((flags & XFS_LOG_SYNC) && /* sleep */ |
3055 | !(iclog->ic_state & | |
3056 | (XLOG_STATE_ACTIVE | XLOG_STATE_DIRTY))) { | |
3057 | /* | |
3058 | * Don't wait on completion if we know that we've | |
3059 | * gotten a log write error. | |
3060 | */ | |
3061 | if (iclog->ic_state & XLOG_STATE_IOERROR) { | |
3062 | spin_unlock(&log->l_icloglock); | |
3063 | return XFS_ERROR(EIO); | |
3064 | } | |
3065 | XFS_STATS_INC(xs_log_force_sleep); | |
3066 | sv_wait(&iclog->ic_force_wait, PSWP, &log->l_icloglock, s); | |
3067 | /* | |
3068 | * No need to grab the log lock here since we're | |
3069 | * only deciding whether or not to return EIO | |
3070 | * and the memory read should be atomic. | |
3071 | */ | |
3072 | if (iclog->ic_state & XLOG_STATE_IOERROR) | |
3073 | return XFS_ERROR(EIO); | |
1da177e4 | 3074 | |
a14a348b CH |
3075 | if (log_flushed) |
3076 | *log_flushed = 1; | |
3077 | } else { /* just return */ | |
b22cd72c | 3078 | spin_unlock(&log->l_icloglock); |
1da177e4 | 3079 | } |
1da177e4 | 3080 | |
a14a348b CH |
3081 | return 0; |
3082 | } while (iclog != log->l_iclog); | |
1da177e4 | 3083 | |
a14a348b CH |
3084 | spin_unlock(&log->l_icloglock); |
3085 | return 0; | |
3086 | } | |
3087 | ||
3088 | /* | |
3089 | * Wrapper for _xfs_log_force_lsn(), to be used when caller doesn't care | |
3090 | * about errors or whether the log was flushed or not. This is the normal | |
3091 | * interface to use when trying to unpin items or move the log forward. | |
3092 | */ | |
3093 | void | |
3094 | xfs_log_force_lsn( | |
3095 | xfs_mount_t *mp, | |
3096 | xfs_lsn_t lsn, | |
3097 | uint flags) | |
3098 | { | |
3099 | int error; | |
1da177e4 | 3100 | |
a14a348b CH |
3101 | error = _xfs_log_force_lsn(mp, lsn, flags, NULL); |
3102 | if (error) { | |
3103 | xfs_fs_cmn_err(CE_WARN, mp, "xfs_log_force: " | |
3104 | "error %d returned.", error); | |
3105 | } | |
3106 | } | |
1da177e4 LT |
3107 | |
3108 | /* | |
3109 | * Called when we want to mark the current iclog as being ready to sync to | |
3110 | * disk. | |
3111 | */ | |
a8272ce0 | 3112 | STATIC void |
1da177e4 LT |
3113 | xlog_state_want_sync(xlog_t *log, xlog_in_core_t *iclog) |
3114 | { | |
a8914f3a | 3115 | assert_spin_locked(&log->l_icloglock); |
1da177e4 LT |
3116 | |
3117 | if (iclog->ic_state == XLOG_STATE_ACTIVE) { | |
3118 | xlog_state_switch_iclogs(log, iclog, 0); | |
3119 | } else { | |
3120 | ASSERT(iclog->ic_state & | |
3121 | (XLOG_STATE_WANT_SYNC|XLOG_STATE_IOERROR)); | |
3122 | } | |
39e2defe | 3123 | } |
1da177e4 LT |
3124 | |
3125 | ||
3126 | /***************************************************************************** | |
3127 | * | |
3128 | * TICKET functions | |
3129 | * | |
3130 | ***************************************************************************** | |
3131 | */ | |
3132 | ||
3133 | /* | |
9da096fd | 3134 | * Free a used ticket when its refcount falls to zero. |
1da177e4 | 3135 | */ |
cc09c0dc DC |
3136 | void |
3137 | xfs_log_ticket_put( | |
3138 | xlog_ticket_t *ticket) | |
1da177e4 | 3139 | { |
cc09c0dc DC |
3140 | ASSERT(atomic_read(&ticket->t_ref) > 0); |
3141 | if (atomic_dec_and_test(&ticket->t_ref)) { | |
3142 | sv_destroy(&ticket->t_wait); | |
3143 | kmem_zone_free(xfs_log_ticket_zone, ticket); | |
3144 | } | |
3145 | } | |
1da177e4 | 3146 | |
cc09c0dc DC |
3147 | xlog_ticket_t * |
3148 | xfs_log_ticket_get( | |
3149 | xlog_ticket_t *ticket) | |
3150 | { | |
3151 | ASSERT(atomic_read(&ticket->t_ref) > 0); | |
3152 | atomic_inc(&ticket->t_ref); | |
3153 | return ticket; | |
3154 | } | |
1da177e4 LT |
3155 | |
3156 | /* | |
eb01c9cd | 3157 | * Allocate and initialise a new log ticket. |
1da177e4 | 3158 | */ |
a8272ce0 | 3159 | STATIC xlog_ticket_t * |
cc09c0dc | 3160 | xlog_ticket_alloc(xlog_t *log, |
1da177e4 LT |
3161 | int unit_bytes, |
3162 | int cnt, | |
3163 | char client, | |
3164 | uint xflags) | |
3165 | { | |
3166 | xlog_ticket_t *tic; | |
3167 | uint num_headers; | |
1da177e4 | 3168 | |
eb01c9cd DC |
3169 | tic = kmem_zone_zalloc(xfs_log_ticket_zone, KM_SLEEP|KM_MAYFAIL); |
3170 | if (!tic) | |
3171 | return NULL; | |
1da177e4 LT |
3172 | |
3173 | /* | |
3174 | * Permanent reservations have up to 'cnt'-1 active log operations | |
3175 | * in the log. A unit in this case is the amount of space for one | |
3176 | * of these log operations. Normal reservations have a cnt of 1 | |
3177 | * and their unit amount is the total amount of space required. | |
3178 | * | |
3179 | * The following lines of code account for non-transaction data | |
32fb9b57 TS |
3180 | * which occupy space in the on-disk log. |
3181 | * | |
3182 | * Normal form of a transaction is: | |
3183 | * <oph><trans-hdr><start-oph><reg1-oph><reg1><reg2-oph>...<commit-oph> | |
3184 | * and then there are LR hdrs, split-recs and roundoff at end of syncs. | |
3185 | * | |
3186 | * We need to account for all the leadup data and trailer data | |
3187 | * around the transaction data. | |
3188 | * And then we need to account for the worst case in terms of using | |
3189 | * more space. | |
3190 | * The worst case will happen if: | |
3191 | * - the placement of the transaction happens to be such that the | |
3192 | * roundoff is at its maximum | |
3193 | * - the transaction data is synced before the commit record is synced | |
3194 | * i.e. <transaction-data><roundoff> | <commit-rec><roundoff> | |
3195 | * Therefore the commit record is in its own Log Record. | |
3196 | * This can happen as the commit record is called with its | |
3197 | * own region to xlog_write(). | |
3198 | * This then means that in the worst case, roundoff can happen for | |
3199 | * the commit-rec as well. | |
3200 | * The commit-rec is smaller than padding in this scenario and so it is | |
3201 | * not added separately. | |
1da177e4 LT |
3202 | */ |
3203 | ||
32fb9b57 TS |
3204 | /* for trans header */ |
3205 | unit_bytes += sizeof(xlog_op_header_t); | |
3206 | unit_bytes += sizeof(xfs_trans_header_t); | |
3207 | ||
1da177e4 | 3208 | /* for start-rec */ |
32fb9b57 TS |
3209 | unit_bytes += sizeof(xlog_op_header_t); |
3210 | ||
3211 | /* for LR headers */ | |
3212 | num_headers = ((unit_bytes + log->l_iclog_size-1) >> log->l_iclog_size_log); | |
3213 | unit_bytes += log->l_iclog_hsize * num_headers; | |
1da177e4 | 3214 | |
32fb9b57 TS |
3215 | /* for commit-rec LR header - note: padding will subsume the ophdr */ |
3216 | unit_bytes += log->l_iclog_hsize; | |
3217 | ||
3218 | /* for split-recs - ophdrs added when data split over LRs */ | |
3219 | unit_bytes += sizeof(xlog_op_header_t) * num_headers; | |
3220 | ||
3221 | /* for roundoff padding for transaction data and one for commit record */ | |
62118709 | 3222 | if (xfs_sb_version_haslogv2(&log->l_mp->m_sb) && |
32fb9b57 | 3223 | log->l_mp->m_sb.sb_logsunit > 1) { |
1da177e4 | 3224 | /* log su roundoff */ |
32fb9b57 | 3225 | unit_bytes += 2*log->l_mp->m_sb.sb_logsunit; |
1da177e4 LT |
3226 | } else { |
3227 | /* BB roundoff */ | |
32fb9b57 | 3228 | unit_bytes += 2*BBSIZE; |
1da177e4 LT |
3229 | } |
3230 | ||
cc09c0dc | 3231 | atomic_set(&tic->t_ref, 1); |
1da177e4 LT |
3232 | tic->t_unit_res = unit_bytes; |
3233 | tic->t_curr_res = unit_bytes; | |
3234 | tic->t_cnt = cnt; | |
3235 | tic->t_ocnt = cnt; | |
3236 | tic->t_tid = (xlog_tid_t)((__psint_t)tic & 0xffffffff); | |
3237 | tic->t_clientid = client; | |
3238 | tic->t_flags = XLOG_TIC_INITED; | |
7e9c6396 | 3239 | tic->t_trans_type = 0; |
1da177e4 LT |
3240 | if (xflags & XFS_LOG_PERM_RESERV) |
3241 | tic->t_flags |= XLOG_TIC_PERM_RESERV; | |
12017faf | 3242 | sv_init(&(tic->t_wait), SV_DEFAULT, "logtick"); |
1da177e4 | 3243 | |
0adba536 | 3244 | xlog_tic_reset_res(tic); |
7e9c6396 | 3245 | |
1da177e4 | 3246 | return tic; |
cc09c0dc | 3247 | } |
1da177e4 LT |
3248 | |
3249 | ||
3250 | /****************************************************************************** | |
3251 | * | |
3252 | * Log debug routines | |
3253 | * | |
3254 | ****************************************************************************** | |
3255 | */ | |
cfcbbbd0 | 3256 | #if defined(DEBUG) |
1da177e4 LT |
3257 | /* |
3258 | * Make sure that the destination ptr is within the valid data region of | |
3259 | * one of the iclogs. This uses backup pointers stored in a different | |
3260 | * part of the log in case we trash the log structure. | |
3261 | */ | |
3262 | void | |
3263 | xlog_verify_dest_ptr(xlog_t *log, | |
3264 | __psint_t ptr) | |
3265 | { | |
3266 | int i; | |
3267 | int good_ptr = 0; | |
3268 | ||
3269 | for (i=0; i < log->l_iclog_bufs; i++) { | |
3270 | if (ptr >= (__psint_t)log->l_iclog_bak[i] && | |
3271 | ptr <= (__psint_t)log->l_iclog_bak[i]+log->l_iclog_size) | |
3272 | good_ptr++; | |
3273 | } | |
3274 | if (! good_ptr) | |
3275 | xlog_panic("xlog_verify_dest_ptr: invalid ptr"); | |
3276 | } /* xlog_verify_dest_ptr */ | |
3277 | ||
3278 | STATIC void | |
3279 | xlog_verify_grant_head(xlog_t *log, int equals) | |
3280 | { | |
3281 | if (log->l_grant_reserve_cycle == log->l_grant_write_cycle) { | |
3282 | if (equals) | |
3283 | ASSERT(log->l_grant_reserve_bytes >= log->l_grant_write_bytes); | |
3284 | else | |
3285 | ASSERT(log->l_grant_reserve_bytes > log->l_grant_write_bytes); | |
3286 | } else { | |
3287 | ASSERT(log->l_grant_reserve_cycle-1 == log->l_grant_write_cycle); | |
3288 | ASSERT(log->l_grant_write_bytes >= log->l_grant_reserve_bytes); | |
3289 | } | |
3290 | } /* xlog_verify_grant_head */ | |
3291 | ||
3292 | /* check if it will fit */ | |
3293 | STATIC void | |
3294 | xlog_verify_tail_lsn(xlog_t *log, | |
3295 | xlog_in_core_t *iclog, | |
3296 | xfs_lsn_t tail_lsn) | |
3297 | { | |
3298 | int blocks; | |
3299 | ||
3300 | if (CYCLE_LSN(tail_lsn) == log->l_prev_cycle) { | |
3301 | blocks = | |
3302 | log->l_logBBsize - (log->l_prev_block - BLOCK_LSN(tail_lsn)); | |
3303 | if (blocks < BTOBB(iclog->ic_offset)+BTOBB(log->l_iclog_hsize)) | |
3304 | xlog_panic("xlog_verify_tail_lsn: ran out of log space"); | |
3305 | } else { | |
3306 | ASSERT(CYCLE_LSN(tail_lsn)+1 == log->l_prev_cycle); | |
3307 | ||
3308 | if (BLOCK_LSN(tail_lsn) == log->l_prev_block) | |
3309 | xlog_panic("xlog_verify_tail_lsn: tail wrapped"); | |
3310 | ||
3311 | blocks = BLOCK_LSN(tail_lsn) - log->l_prev_block; | |
3312 | if (blocks < BTOBB(iclog->ic_offset) + 1) | |
3313 | xlog_panic("xlog_verify_tail_lsn: ran out of log space"); | |
3314 | } | |
3315 | } /* xlog_verify_tail_lsn */ | |
3316 | ||
3317 | /* | |
3318 | * Perform a number of checks on the iclog before writing to disk. | |
3319 | * | |
3320 | * 1. Make sure the iclogs are still circular | |
3321 | * 2. Make sure we have a good magic number | |
3322 | * 3. Make sure we don't have magic numbers in the data | |
3323 | * 4. Check fields of each log operation header for: | |
3324 | * A. Valid client identifier | |
3325 | * B. tid ptr value falls in valid ptr space (user space code) | |
3326 | * C. Length in log record header is correct according to the | |
3327 | * individual operation headers within record. | |
3328 | * 5. When a bwrite will occur within 5 blocks of the front of the physical | |
3329 | * log, check the preceding blocks of the physical log to make sure all | |
3330 | * the cycle numbers agree with the current cycle number. | |
3331 | */ | |
3332 | STATIC void | |
3333 | xlog_verify_iclog(xlog_t *log, | |
3334 | xlog_in_core_t *iclog, | |
3335 | int count, | |
3336 | boolean_t syncing) | |
3337 | { | |
3338 | xlog_op_header_t *ophead; | |
3339 | xlog_in_core_t *icptr; | |
3340 | xlog_in_core_2_t *xhdr; | |
3341 | xfs_caddr_t ptr; | |
3342 | xfs_caddr_t base_ptr; | |
3343 | __psint_t field_offset; | |
3344 | __uint8_t clientid; | |
3345 | int len, i, j, k, op_len; | |
3346 | int idx; | |
1da177e4 LT |
3347 | |
3348 | /* check validity of iclog pointers */ | |
b22cd72c | 3349 | spin_lock(&log->l_icloglock); |
1da177e4 LT |
3350 | icptr = log->l_iclog; |
3351 | for (i=0; i < log->l_iclog_bufs; i++) { | |
4b80916b | 3352 | if (icptr == NULL) |
1da177e4 LT |
3353 | xlog_panic("xlog_verify_iclog: invalid ptr"); |
3354 | icptr = icptr->ic_next; | |
3355 | } | |
3356 | if (icptr != log->l_iclog) | |
3357 | xlog_panic("xlog_verify_iclog: corrupt iclog ring"); | |
b22cd72c | 3358 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
3359 | |
3360 | /* check log magic numbers */ | |
b53e675d | 3361 | if (be32_to_cpu(iclog->ic_header.h_magicno) != XLOG_HEADER_MAGIC_NUM) |
1da177e4 LT |
3362 | xlog_panic("xlog_verify_iclog: invalid magic num"); |
3363 | ||
b53e675d CH |
3364 | ptr = (xfs_caddr_t) &iclog->ic_header; |
3365 | for (ptr += BBSIZE; ptr < ((xfs_caddr_t)&iclog->ic_header) + count; | |
1da177e4 | 3366 | ptr += BBSIZE) { |
b53e675d | 3367 | if (be32_to_cpu(*(__be32 *)ptr) == XLOG_HEADER_MAGIC_NUM) |
1da177e4 LT |
3368 | xlog_panic("xlog_verify_iclog: unexpected magic num"); |
3369 | } | |
3370 | ||
3371 | /* check fields */ | |
b53e675d | 3372 | len = be32_to_cpu(iclog->ic_header.h_num_logops); |
1da177e4 LT |
3373 | ptr = iclog->ic_datap; |
3374 | base_ptr = ptr; | |
3375 | ophead = (xlog_op_header_t *)ptr; | |
b28708d6 | 3376 | xhdr = iclog->ic_data; |
1da177e4 LT |
3377 | for (i = 0; i < len; i++) { |
3378 | ophead = (xlog_op_header_t *)ptr; | |
3379 | ||
3380 | /* clientid is only 1 byte */ | |
3381 | field_offset = (__psint_t) | |
3382 | ((xfs_caddr_t)&(ophead->oh_clientid) - base_ptr); | |
3383 | if (syncing == B_FALSE || (field_offset & 0x1ff)) { | |
3384 | clientid = ophead->oh_clientid; | |
3385 | } else { | |
3386 | idx = BTOBBT((xfs_caddr_t)&(ophead->oh_clientid) - iclog->ic_datap); | |
3387 | if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) { | |
3388 | j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE); | |
3389 | k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE); | |
03bea6fe CH |
3390 | clientid = xlog_get_client_id( |
3391 | xhdr[j].hic_xheader.xh_cycle_data[k]); | |
1da177e4 | 3392 | } else { |
03bea6fe CH |
3393 | clientid = xlog_get_client_id( |
3394 | iclog->ic_header.h_cycle_data[idx]); | |
1da177e4 LT |
3395 | } |
3396 | } | |
3397 | if (clientid != XFS_TRANSACTION && clientid != XFS_LOG) | |
da1650a5 CH |
3398 | cmn_err(CE_WARN, "xlog_verify_iclog: " |
3399 | "invalid clientid %d op 0x%p offset 0x%lx", | |
3400 | clientid, ophead, (unsigned long)field_offset); | |
1da177e4 LT |
3401 | |
3402 | /* check length */ | |
3403 | field_offset = (__psint_t) | |
3404 | ((xfs_caddr_t)&(ophead->oh_len) - base_ptr); | |
3405 | if (syncing == B_FALSE || (field_offset & 0x1ff)) { | |
67fcb7bf | 3406 | op_len = be32_to_cpu(ophead->oh_len); |
1da177e4 LT |
3407 | } else { |
3408 | idx = BTOBBT((__psint_t)&ophead->oh_len - | |
3409 | (__psint_t)iclog->ic_datap); | |
3410 | if (idx >= (XLOG_HEADER_CYCLE_SIZE / BBSIZE)) { | |
3411 | j = idx / (XLOG_HEADER_CYCLE_SIZE / BBSIZE); | |
3412 | k = idx % (XLOG_HEADER_CYCLE_SIZE / BBSIZE); | |
b53e675d | 3413 | op_len = be32_to_cpu(xhdr[j].hic_xheader.xh_cycle_data[k]); |
1da177e4 | 3414 | } else { |
b53e675d | 3415 | op_len = be32_to_cpu(iclog->ic_header.h_cycle_data[idx]); |
1da177e4 LT |
3416 | } |
3417 | } | |
3418 | ptr += sizeof(xlog_op_header_t) + op_len; | |
3419 | } | |
3420 | } /* xlog_verify_iclog */ | |
cfcbbbd0 | 3421 | #endif |
1da177e4 LT |
3422 | |
3423 | /* | |
b22cd72c | 3424 | * Mark all iclogs IOERROR. l_icloglock is held by the caller. |
1da177e4 LT |
3425 | */ |
3426 | STATIC int | |
3427 | xlog_state_ioerror( | |
3428 | xlog_t *log) | |
3429 | { | |
3430 | xlog_in_core_t *iclog, *ic; | |
3431 | ||
3432 | iclog = log->l_iclog; | |
3433 | if (! (iclog->ic_state & XLOG_STATE_IOERROR)) { | |
3434 | /* | |
3435 | * Mark all the incore logs IOERROR. | |
3436 | * From now on, no log flushes will result. | |
3437 | */ | |
3438 | ic = iclog; | |
3439 | do { | |
3440 | ic->ic_state = XLOG_STATE_IOERROR; | |
3441 | ic = ic->ic_next; | |
3442 | } while (ic != iclog); | |
014c2544 | 3443 | return 0; |
1da177e4 LT |
3444 | } |
3445 | /* | |
3446 | * Return non-zero, if state transition has already happened. | |
3447 | */ | |
014c2544 | 3448 | return 1; |
1da177e4 LT |
3449 | } |
3450 | ||
3451 | /* | |
3452 | * This is called from xfs_force_shutdown, when we're forcibly | |
3453 | * shutting down the filesystem, typically because of an IO error. | |
3454 | * Our main objectives here are to make sure that: | |
3455 | * a. the filesystem gets marked 'SHUTDOWN' for all interested | |
3456 | * parties to find out, 'atomically'. | |
3457 | * b. those who're sleeping on log reservations, pinned objects and | |
3458 | * other resources get woken up, and be told the bad news. | |
3459 | * c. nothing new gets queued up after (a) and (b) are done. | |
3460 | * d. if !logerror, flush the iclogs to disk, then seal them off | |
3461 | * for business. | |
3462 | */ | |
3463 | int | |
3464 | xfs_log_force_umount( | |
3465 | struct xfs_mount *mp, | |
3466 | int logerror) | |
3467 | { | |
3468 | xlog_ticket_t *tic; | |
3469 | xlog_t *log; | |
3470 | int retval; | |
1da177e4 LT |
3471 | |
3472 | log = mp->m_log; | |
3473 | ||
3474 | /* | |
3475 | * If this happens during log recovery, don't worry about | |
3476 | * locking; the log isn't open for business yet. | |
3477 | */ | |
3478 | if (!log || | |
3479 | log->l_flags & XLOG_ACTIVE_RECOVERY) { | |
3480 | mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN; | |
bac8dca9 CH |
3481 | if (mp->m_sb_bp) |
3482 | XFS_BUF_DONE(mp->m_sb_bp); | |
014c2544 | 3483 | return 0; |
1da177e4 LT |
3484 | } |
3485 | ||
3486 | /* | |
3487 | * Somebody could've already done the hard work for us. | |
3488 | * No need to get locks for this. | |
3489 | */ | |
3490 | if (logerror && log->l_iclog->ic_state & XLOG_STATE_IOERROR) { | |
3491 | ASSERT(XLOG_FORCED_SHUTDOWN(log)); | |
014c2544 | 3492 | return 1; |
1da177e4 LT |
3493 | } |
3494 | retval = 0; | |
3495 | /* | |
3496 | * We must hold both the GRANT lock and the LOG lock, | |
3497 | * before we mark the filesystem SHUTDOWN and wake | |
3498 | * everybody up to tell the bad news. | |
3499 | */ | |
b22cd72c | 3500 | spin_lock(&log->l_icloglock); |
6b1d1a73 | 3501 | spin_lock(&log->l_grant_lock); |
1da177e4 | 3502 | mp->m_flags |= XFS_MOUNT_FS_SHUTDOWN; |
bac8dca9 CH |
3503 | if (mp->m_sb_bp) |
3504 | XFS_BUF_DONE(mp->m_sb_bp); | |
3505 | ||
1da177e4 LT |
3506 | /* |
3507 | * This flag is sort of redundant because of the mount flag, but | |
3508 | * it's good to maintain the separation between the log and the rest | |
3509 | * of XFS. | |
3510 | */ | |
3511 | log->l_flags |= XLOG_IO_ERROR; | |
3512 | ||
3513 | /* | |
3514 | * If we hit a log error, we want to mark all the iclogs IOERROR | |
3515 | * while we're still holding the loglock. | |
3516 | */ | |
3517 | if (logerror) | |
3518 | retval = xlog_state_ioerror(log); | |
b22cd72c | 3519 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
3520 | |
3521 | /* | |
3522 | * We don't want anybody waiting for log reservations | |
3523 | * after this. That means we have to wake up everybody | |
3524 | * queued up on reserve_headq as well as write_headq. | |
3525 | * In addition, we make sure in xlog_{re}grant_log_space | |
3526 | * that we don't enqueue anything once the SHUTDOWN flag | |
3527 | * is set, and this action is protected by the GRANTLOCK. | |
3528 | */ | |
3529 | if ((tic = log->l_reserve_headq)) { | |
3530 | do { | |
12017faf | 3531 | sv_signal(&tic->t_wait); |
1da177e4 LT |
3532 | tic = tic->t_next; |
3533 | } while (tic != log->l_reserve_headq); | |
3534 | } | |
3535 | ||
3536 | if ((tic = log->l_write_headq)) { | |
3537 | do { | |
12017faf | 3538 | sv_signal(&tic->t_wait); |
1da177e4 LT |
3539 | tic = tic->t_next; |
3540 | } while (tic != log->l_write_headq); | |
3541 | } | |
c8b5ea28 | 3542 | spin_unlock(&log->l_grant_lock); |
1da177e4 | 3543 | |
a14a348b | 3544 | if (!(log->l_iclog->ic_state & XLOG_STATE_IOERROR)) { |
1da177e4 LT |
3545 | ASSERT(!logerror); |
3546 | /* | |
3547 | * Force the incore logs to disk before shutting the | |
3548 | * log down completely. | |
3549 | */ | |
a14a348b CH |
3550 | _xfs_log_force(mp, XFS_LOG_SYNC, NULL); |
3551 | ||
b22cd72c | 3552 | spin_lock(&log->l_icloglock); |
1da177e4 | 3553 | retval = xlog_state_ioerror(log); |
b22cd72c | 3554 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
3555 | } |
3556 | /* | |
3557 | * Wake up everybody waiting on xfs_log_force. | |
3558 | * Callback all log item committed functions as if the | |
3559 | * log writes were completed. | |
3560 | */ | |
3561 | xlog_state_do_callback(log, XFS_LI_ABORTED, NULL); | |
3562 | ||
3563 | #ifdef XFSERRORDEBUG | |
3564 | { | |
3565 | xlog_in_core_t *iclog; | |
3566 | ||
b22cd72c | 3567 | spin_lock(&log->l_icloglock); |
1da177e4 LT |
3568 | iclog = log->l_iclog; |
3569 | do { | |
3570 | ASSERT(iclog->ic_callback == 0); | |
3571 | iclog = iclog->ic_next; | |
3572 | } while (iclog != log->l_iclog); | |
b22cd72c | 3573 | spin_unlock(&log->l_icloglock); |
1da177e4 LT |
3574 | } |
3575 | #endif | |
3576 | /* return non-zero if log IOERROR transition had already happened */ | |
014c2544 | 3577 | return retval; |
1da177e4 LT |
3578 | } |
3579 | ||
ba0f32d4 | 3580 | STATIC int |
1da177e4 LT |
3581 | xlog_iclogs_empty(xlog_t *log) |
3582 | { | |
3583 | xlog_in_core_t *iclog; | |
3584 | ||
3585 | iclog = log->l_iclog; | |
3586 | do { | |
3587 | /* endianness does not matter here, zero is zero in | |
3588 | * any language. | |
3589 | */ | |
3590 | if (iclog->ic_header.h_num_logops) | |
014c2544 | 3591 | return 0; |
1da177e4 LT |
3592 | iclog = iclog->ic_next; |
3593 | } while (iclog != log->l_iclog); | |
014c2544 | 3594 | return 1; |
1da177e4 | 3595 | } |