/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

Large files are truncated click here to view the full file

  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 cu…