PageRenderTime 64ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/fs/xfs/xfs_log.c

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