segbuf.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * segbuf.c - NILFS segment buffer
  3. *
  4. * Copyright (C) 2005-2008 Nippon Telegraph and Telephone Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * Written by Ryusuke Konishi.
  17. *
  18. */
  19. #include <linux/buffer_head.h>
  20. #include <linux/writeback.h>
  21. #include <linux/crc32.h>
  22. #include <linux/backing-dev.h>
  23. #include <linux/slab.h>
  24. #include "page.h"
  25. #include "segbuf.h"
  26. struct nilfs_write_info {
  27. struct the_nilfs *nilfs;
  28. struct bio *bio;
  29. int start, end; /* The region to be submitted */
  30. int rest_blocks;
  31. int max_pages;
  32. int nr_vecs;
  33. sector_t blocknr;
  34. };
  35. static int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf,
  36. struct the_nilfs *nilfs);
  37. static int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf);
  38. struct nilfs_segment_buffer *nilfs_segbuf_new(struct super_block *sb)
  39. {
  40. struct nilfs_segment_buffer *segbuf;
  41. segbuf = kmem_cache_alloc(nilfs_segbuf_cachep, GFP_NOFS);
  42. if (unlikely(!segbuf))
  43. return NULL;
  44. segbuf->sb_super = sb;
  45. INIT_LIST_HEAD(&segbuf->sb_list);
  46. INIT_LIST_HEAD(&segbuf->sb_segsum_buffers);
  47. INIT_LIST_HEAD(&segbuf->sb_payload_buffers);
  48. segbuf->sb_super_root = NULL;
  49. init_completion(&segbuf->sb_bio_event);
  50. atomic_set(&segbuf->sb_err, 0);
  51. segbuf->sb_nbio = 0;
  52. return segbuf;
  53. }
  54. void nilfs_segbuf_free(struct nilfs_segment_buffer *segbuf)
  55. {
  56. kmem_cache_free(nilfs_segbuf_cachep, segbuf);
  57. }
  58. void nilfs_segbuf_map(struct nilfs_segment_buffer *segbuf, __u64 segnum,
  59. unsigned long offset, struct the_nilfs *nilfs)
  60. {
  61. segbuf->sb_segnum = segnum;
  62. nilfs_get_segment_range(nilfs, segnum, &segbuf->sb_fseg_start,
  63. &segbuf->sb_fseg_end);
  64. segbuf->sb_pseg_start = segbuf->sb_fseg_start + offset;
  65. segbuf->sb_rest_blocks =
  66. segbuf->sb_fseg_end - segbuf->sb_pseg_start + 1;
  67. }
  68. /**
  69. * nilfs_segbuf_map_cont - map a new log behind a given log
  70. * @segbuf: new segment buffer
  71. * @prev: segment buffer containing a log to be continued
  72. */
  73. void nilfs_segbuf_map_cont(struct nilfs_segment_buffer *segbuf,
  74. struct nilfs_segment_buffer *prev)
  75. {
  76. segbuf->sb_segnum = prev->sb_segnum;
  77. segbuf->sb_fseg_start = prev->sb_fseg_start;
  78. segbuf->sb_fseg_end = prev->sb_fseg_end;
  79. segbuf->sb_pseg_start = prev->sb_pseg_start + prev->sb_sum.nblocks;
  80. segbuf->sb_rest_blocks =
  81. segbuf->sb_fseg_end - segbuf->sb_pseg_start + 1;
  82. }
  83. void nilfs_segbuf_set_next_segnum(struct nilfs_segment_buffer *segbuf,
  84. __u64 nextnum, struct the_nilfs *nilfs)
  85. {
  86. segbuf->sb_nextnum = nextnum;
  87. segbuf->sb_sum.next = nilfs_get_segment_start_blocknr(nilfs, nextnum);
  88. }
  89. int nilfs_segbuf_extend_segsum(struct nilfs_segment_buffer *segbuf)
  90. {
  91. struct buffer_head *bh;
  92. bh = sb_getblk(segbuf->sb_super,
  93. segbuf->sb_pseg_start + segbuf->sb_sum.nsumblk);
  94. if (unlikely(!bh))
  95. return -ENOMEM;
  96. nilfs_segbuf_add_segsum_buffer(segbuf, bh);
  97. return 0;
  98. }
  99. int nilfs_segbuf_extend_payload(struct nilfs_segment_buffer *segbuf,
  100. struct buffer_head **bhp)
  101. {
  102. struct buffer_head *bh;
  103. bh = sb_getblk(segbuf->sb_super,
  104. segbuf->sb_pseg_start + segbuf->sb_sum.nblocks);
  105. if (unlikely(!bh))
  106. return -ENOMEM;
  107. nilfs_segbuf_add_payload_buffer(segbuf, bh);
  108. *bhp = bh;
  109. return 0;
  110. }
  111. int nilfs_segbuf_reset(struct nilfs_segment_buffer *segbuf, unsigned int flags,
  112. time_t ctime, __u64 cno)
  113. {
  114. int err;
  115. segbuf->sb_sum.nblocks = segbuf->sb_sum.nsumblk = 0;
  116. err = nilfs_segbuf_extend_segsum(segbuf);
  117. if (unlikely(err))
  118. return err;
  119. segbuf->sb_sum.flags = flags;
  120. segbuf->sb_sum.sumbytes = sizeof(struct nilfs_segment_summary);
  121. segbuf->sb_sum.nfinfo = segbuf->sb_sum.nfileblk = 0;
  122. segbuf->sb_sum.ctime = ctime;
  123. segbuf->sb_sum.cno = cno;
  124. return 0;
  125. }
  126. /*
  127. * Setup segment summary
  128. */
  129. void nilfs_segbuf_fill_in_segsum(struct nilfs_segment_buffer *segbuf)
  130. {
  131. struct nilfs_segment_summary *raw_sum;
  132. struct buffer_head *bh_sum;
  133. bh_sum = list_entry(segbuf->sb_segsum_buffers.next,
  134. struct buffer_head, b_assoc_buffers);
  135. raw_sum = (struct nilfs_segment_summary *)bh_sum->b_data;
  136. raw_sum->ss_magic = cpu_to_le32(NILFS_SEGSUM_MAGIC);
  137. raw_sum->ss_bytes = cpu_to_le16(sizeof(*raw_sum));
  138. raw_sum->ss_flags = cpu_to_le16(segbuf->sb_sum.flags);
  139. raw_sum->ss_seq = cpu_to_le64(segbuf->sb_sum.seg_seq);
  140. raw_sum->ss_create = cpu_to_le64(segbuf->sb_sum.ctime);
  141. raw_sum->ss_next = cpu_to_le64(segbuf->sb_sum.next);
  142. raw_sum->ss_nblocks = cpu_to_le32(segbuf->sb_sum.nblocks);
  143. raw_sum->ss_nfinfo = cpu_to_le32(segbuf->sb_sum.nfinfo);
  144. raw_sum->ss_sumbytes = cpu_to_le32(segbuf->sb_sum.sumbytes);
  145. raw_sum->ss_pad = 0;
  146. raw_sum->ss_cno = cpu_to_le64(segbuf->sb_sum.cno);
  147. }
  148. /*
  149. * CRC calculation routines
  150. */
  151. static void
  152. nilfs_segbuf_fill_in_segsum_crc(struct nilfs_segment_buffer *segbuf, u32 seed)
  153. {
  154. struct buffer_head *bh;
  155. struct nilfs_segment_summary *raw_sum;
  156. unsigned long size, bytes = segbuf->sb_sum.sumbytes;
  157. u32 crc;
  158. bh = list_entry(segbuf->sb_segsum_buffers.next, struct buffer_head,
  159. b_assoc_buffers);
  160. raw_sum = (struct nilfs_segment_summary *)bh->b_data;
  161. size = min_t(unsigned long, bytes, bh->b_size);
  162. crc = crc32_le(seed,
  163. (unsigned char *)raw_sum +
  164. sizeof(raw_sum->ss_datasum) + sizeof(raw_sum->ss_sumsum),
  165. size - (sizeof(raw_sum->ss_datasum) +
  166. sizeof(raw_sum->ss_sumsum)));
  167. list_for_each_entry_continue(bh, &segbuf->sb_segsum_buffers,
  168. b_assoc_buffers) {
  169. bytes -= size;
  170. size = min_t(unsigned long, bytes, bh->b_size);
  171. crc = crc32_le(crc, bh->b_data, size);
  172. }
  173. raw_sum->ss_sumsum = cpu_to_le32(crc);
  174. }
  175. static void nilfs_segbuf_fill_in_data_crc(struct nilfs_segment_buffer *segbuf,
  176. u32 seed)
  177. {
  178. struct buffer_head *bh;
  179. struct nilfs_segment_summary *raw_sum;
  180. void *kaddr;
  181. u32 crc;
  182. bh = list_entry(segbuf->sb_segsum_buffers.next, struct buffer_head,
  183. b_assoc_buffers);
  184. raw_sum = (struct nilfs_segment_summary *)bh->b_data;
  185. crc = crc32_le(seed,
  186. (unsigned char *)raw_sum + sizeof(raw_sum->ss_datasum),
  187. bh->b_size - sizeof(raw_sum->ss_datasum));
  188. list_for_each_entry_continue(bh, &segbuf->sb_segsum_buffers,
  189. b_assoc_buffers) {
  190. crc = crc32_le(crc, bh->b_data, bh->b_size);
  191. }
  192. list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
  193. kaddr = kmap_atomic(bh->b_page);
  194. crc = crc32_le(crc, kaddr + bh_offset(bh), bh->b_size);
  195. kunmap_atomic(kaddr);
  196. }
  197. raw_sum->ss_datasum = cpu_to_le32(crc);
  198. }
  199. static void
  200. nilfs_segbuf_fill_in_super_root_crc(struct nilfs_segment_buffer *segbuf,
  201. u32 seed)
  202. {
  203. struct nilfs_super_root *raw_sr;
  204. struct the_nilfs *nilfs = segbuf->sb_super->s_fs_info;
  205. unsigned int srsize;
  206. u32 crc;
  207. raw_sr = (struct nilfs_super_root *)segbuf->sb_super_root->b_data;
  208. srsize = NILFS_SR_BYTES(nilfs->ns_inode_size);
  209. crc = crc32_le(seed,
  210. (unsigned char *)raw_sr + sizeof(raw_sr->sr_sum),
  211. srsize - sizeof(raw_sr->sr_sum));
  212. raw_sr->sr_sum = cpu_to_le32(crc);
  213. }
  214. static void nilfs_release_buffers(struct list_head *list)
  215. {
  216. struct buffer_head *bh, *n;
  217. list_for_each_entry_safe(bh, n, list, b_assoc_buffers) {
  218. list_del_init(&bh->b_assoc_buffers);
  219. brelse(bh);
  220. }
  221. }
  222. static void nilfs_segbuf_clear(struct nilfs_segment_buffer *segbuf)
  223. {
  224. nilfs_release_buffers(&segbuf->sb_segsum_buffers);
  225. nilfs_release_buffers(&segbuf->sb_payload_buffers);
  226. segbuf->sb_super_root = NULL;
  227. }
  228. /*
  229. * Iterators for segment buffers
  230. */
  231. void nilfs_clear_logs(struct list_head *logs)
  232. {
  233. struct nilfs_segment_buffer *segbuf;
  234. list_for_each_entry(segbuf, logs, sb_list)
  235. nilfs_segbuf_clear(segbuf);
  236. }
  237. void nilfs_truncate_logs(struct list_head *logs,
  238. struct nilfs_segment_buffer *last)
  239. {
  240. struct nilfs_segment_buffer *n, *segbuf;
  241. segbuf = list_prepare_entry(last, logs, sb_list);
  242. list_for_each_entry_safe_continue(segbuf, n, logs, sb_list) {
  243. list_del_init(&segbuf->sb_list);
  244. nilfs_segbuf_clear(segbuf);
  245. nilfs_segbuf_free(segbuf);
  246. }
  247. }
  248. int nilfs_write_logs(struct list_head *logs, struct the_nilfs *nilfs)
  249. {
  250. struct nilfs_segment_buffer *segbuf;
  251. int ret = 0;
  252. list_for_each_entry(segbuf, logs, sb_list) {
  253. ret = nilfs_segbuf_write(segbuf, nilfs);
  254. if (ret)
  255. break;
  256. }
  257. return ret;
  258. }
  259. int nilfs_wait_on_logs(struct list_head *logs)
  260. {
  261. struct nilfs_segment_buffer *segbuf;
  262. int err, ret = 0;
  263. list_for_each_entry(segbuf, logs, sb_list) {
  264. err = nilfs_segbuf_wait(segbuf);
  265. if (err && !ret)
  266. ret = err;
  267. }
  268. return ret;
  269. }
  270. /**
  271. * nilfs_add_checksums_on_logs - add checksums on the logs
  272. * @logs: list of segment buffers storing target logs
  273. * @seed: checksum seed value
  274. */
  275. void nilfs_add_checksums_on_logs(struct list_head *logs, u32 seed)
  276. {
  277. struct nilfs_segment_buffer *segbuf;
  278. list_for_each_entry(segbuf, logs, sb_list) {
  279. if (segbuf->sb_super_root)
  280. nilfs_segbuf_fill_in_super_root_crc(segbuf, seed);
  281. nilfs_segbuf_fill_in_segsum_crc(segbuf, seed);
  282. nilfs_segbuf_fill_in_data_crc(segbuf, seed);
  283. }
  284. }
  285. /*
  286. * BIO operations
  287. */
  288. static void nilfs_end_bio_write(struct bio *bio)
  289. {
  290. struct nilfs_segment_buffer *segbuf = bio->bi_private;
  291. if (bio->bi_error)
  292. atomic_inc(&segbuf->sb_err);
  293. bio_put(bio);
  294. complete(&segbuf->sb_bio_event);
  295. }
  296. static int nilfs_segbuf_submit_bio(struct nilfs_segment_buffer *segbuf,
  297. struct nilfs_write_info *wi, int mode,
  298. int mode_flags)
  299. {
  300. struct bio *bio = wi->bio;
  301. int err;
  302. if (segbuf->sb_nbio > 0 &&
  303. bdi_write_congested(segbuf->sb_super->s_bdi)) {
  304. wait_for_completion(&segbuf->sb_bio_event);
  305. segbuf->sb_nbio--;
  306. if (unlikely(atomic_read(&segbuf->sb_err))) {
  307. bio_put(bio);
  308. err = -EIO;
  309. goto failed;
  310. }
  311. }
  312. bio->bi_end_io = nilfs_end_bio_write;
  313. bio->bi_private = segbuf;
  314. bio_set_op_attrs(bio, mode, mode_flags);
  315. submit_bio(bio);
  316. segbuf->sb_nbio++;
  317. wi->bio = NULL;
  318. wi->rest_blocks -= wi->end - wi->start;
  319. wi->nr_vecs = min(wi->max_pages, wi->rest_blocks);
  320. wi->start = wi->end;
  321. return 0;
  322. failed:
  323. wi->bio = NULL;
  324. return err;
  325. }
  326. /**
  327. * nilfs_alloc_seg_bio - allocate a new bio for writing log
  328. * @nilfs: nilfs object
  329. * @start: start block number of the bio
  330. * @nr_vecs: request size of page vector.
  331. *
  332. * Return Value: On success, pointer to the struct bio is returned.
  333. * On error, NULL is returned.
  334. */
  335. static struct bio *nilfs_alloc_seg_bio(struct the_nilfs *nilfs, sector_t start,
  336. int nr_vecs)
  337. {
  338. struct bio *bio;
  339. bio = bio_alloc(GFP_NOIO, nr_vecs);
  340. if (bio == NULL) {
  341. while (!bio && (nr_vecs >>= 1))
  342. bio = bio_alloc(GFP_NOIO, nr_vecs);
  343. }
  344. if (likely(bio)) {
  345. bio->bi_bdev = nilfs->ns_bdev;
  346. bio->bi_iter.bi_sector =
  347. start << (nilfs->ns_blocksize_bits - 9);
  348. }
  349. return bio;
  350. }
  351. static void nilfs_segbuf_prepare_write(struct nilfs_segment_buffer *segbuf,
  352. struct nilfs_write_info *wi)
  353. {
  354. wi->bio = NULL;
  355. wi->rest_blocks = segbuf->sb_sum.nblocks;
  356. wi->max_pages = BIO_MAX_PAGES;
  357. wi->nr_vecs = min(wi->max_pages, wi->rest_blocks);
  358. wi->start = wi->end = 0;
  359. wi->blocknr = segbuf->sb_pseg_start;
  360. }
  361. static int nilfs_segbuf_submit_bh(struct nilfs_segment_buffer *segbuf,
  362. struct nilfs_write_info *wi,
  363. struct buffer_head *bh, int mode)
  364. {
  365. int len, err;
  366. BUG_ON(wi->nr_vecs <= 0);
  367. repeat:
  368. if (!wi->bio) {
  369. wi->bio = nilfs_alloc_seg_bio(wi->nilfs, wi->blocknr + wi->end,
  370. wi->nr_vecs);
  371. if (unlikely(!wi->bio))
  372. return -ENOMEM;
  373. }
  374. len = bio_add_page(wi->bio, bh->b_page, bh->b_size, bh_offset(bh));
  375. if (len == bh->b_size) {
  376. wi->end++;
  377. return 0;
  378. }
  379. /* bio is FULL */
  380. err = nilfs_segbuf_submit_bio(segbuf, wi, mode, 0);
  381. /* never submit current bh */
  382. if (likely(!err))
  383. goto repeat;
  384. return err;
  385. }
  386. /**
  387. * nilfs_segbuf_write - submit write requests of a log
  388. * @segbuf: buffer storing a log to be written
  389. * @nilfs: nilfs object
  390. *
  391. * Return Value: On Success, 0 is returned. On Error, one of the following
  392. * negative error code is returned.
  393. *
  394. * %-EIO - I/O error
  395. *
  396. * %-ENOMEM - Insufficient memory available.
  397. */
  398. static int nilfs_segbuf_write(struct nilfs_segment_buffer *segbuf,
  399. struct the_nilfs *nilfs)
  400. {
  401. struct nilfs_write_info wi;
  402. struct buffer_head *bh;
  403. int res = 0;
  404. wi.nilfs = nilfs;
  405. nilfs_segbuf_prepare_write(segbuf, &wi);
  406. list_for_each_entry(bh, &segbuf->sb_segsum_buffers, b_assoc_buffers) {
  407. res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, REQ_OP_WRITE);
  408. if (unlikely(res))
  409. goto failed_bio;
  410. }
  411. list_for_each_entry(bh, &segbuf->sb_payload_buffers, b_assoc_buffers) {
  412. res = nilfs_segbuf_submit_bh(segbuf, &wi, bh, REQ_OP_WRITE);
  413. if (unlikely(res))
  414. goto failed_bio;
  415. }
  416. if (wi.bio) {
  417. /*
  418. * Last BIO is always sent through the following
  419. * submission.
  420. */
  421. res = nilfs_segbuf_submit_bio(segbuf, &wi, REQ_OP_WRITE,
  422. REQ_SYNC);
  423. }
  424. failed_bio:
  425. return res;
  426. }
  427. /**
  428. * nilfs_segbuf_wait - wait for completion of requested BIOs
  429. * @segbuf: segment buffer
  430. *
  431. * Return Value: On Success, 0 is returned. On Error, one of the following
  432. * negative error code is returned.
  433. *
  434. * %-EIO - I/O error
  435. */
  436. static int nilfs_segbuf_wait(struct nilfs_segment_buffer *segbuf)
  437. {
  438. int err = 0;
  439. if (!segbuf->sb_nbio)
  440. return 0;
  441. do {
  442. wait_for_completion(&segbuf->sb_bio_event);
  443. } while (--segbuf->sb_nbio > 0);
  444. if (unlikely(atomic_read(&segbuf->sb_err) > 0)) {
  445. nilfs_msg(segbuf->sb_super, KERN_ERR,
  446. "I/O error writing log (start-blocknr=%llu, block-count=%lu) in segment %llu",
  447. (unsigned long long)segbuf->sb_pseg_start,
  448. segbuf->sb_sum.nblocks,
  449. (unsigned long long)segbuf->sb_segnum);
  450. err = -EIO;
  451. }
  452. return err;
  453. }