segbuf.c 14 KB

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