Lines Matching refs:cc
48 int (*init_compress_ctx)(struct compress_ctx *cc);
49 void (*destroy_compress_ctx)(struct compress_ctx *cc);
50 int (*compress_pages)(struct compress_ctx *cc);
56 static unsigned int offset_in_cluster(struct compress_ctx *cc, pgoff_t index)
58 return index & (cc->cluster_size - 1);
61 static pgoff_t cluster_idx(struct compress_ctx *cc, pgoff_t index)
63 return index >> cc->log_cluster_size;
66 static pgoff_t start_idx_of_cluster(struct compress_ctx *cc)
68 return cc->cluster_idx << cc->log_cluster_size;
102 static void f2fs_drop_rpages(struct compress_ctx *cc, int len, bool unlock)
107 if (!cc->rpages[i])
110 unlock_page(cc->rpages[i]);
112 put_page(cc->rpages[i]);
116 static void f2fs_put_rpages(struct compress_ctx *cc)
118 f2fs_drop_rpages(cc, cc->cluster_size, false);
121 static void f2fs_unlock_rpages(struct compress_ctx *cc, int len)
123 f2fs_drop_rpages(cc, len, true);
126 static void f2fs_put_rpages_wbc(struct compress_ctx *cc,
131 for (i = 0; i < cc->cluster_size; i++) {
132 if (!cc->rpages[i])
135 redirty_page_for_writepage(wbc, cc->rpages[i]);
136 f2fs_put_page(cc->rpages[i], unlock);
145 int f2fs_init_compress_ctx(struct compress_ctx *cc)
147 if (cc->rpages)
150 cc->rpages = page_array_alloc(cc->inode, cc->cluster_size);
151 return cc->rpages ? 0 : -ENOMEM;
154 void f2fs_destroy_compress_ctx(struct compress_ctx *cc, bool reuse)
156 page_array_free(cc->inode, cc->rpages, cc->cluster_size);
157 cc->rpages = NULL;
158 cc->nr_rpages = 0;
159 cc->nr_cpages = 0;
161 cc->cluster_idx = NULL_CLUSTER;
164 void f2fs_compress_ctx_add_page(struct compress_ctx *cc, struct page *page)
168 if (!f2fs_cluster_can_merge_page(cc, page->index))
169 f2fs_bug_on(F2FS_I_SB(cc->inode), 1);
171 cluster_ofs = offset_in_cluster(cc, page->index);
172 cc->rpages[cluster_ofs] = page;
173 cc->nr_rpages++;
174 cc->cluster_idx = cluster_idx(cc, page->index);
178 static int lzo_init_compress_ctx(struct compress_ctx *cc)
180 cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
182 if (!cc->private)
185 cc->clen = lzo1x_worst_compress(PAGE_SIZE << cc->log_cluster_size);
189 static void lzo_destroy_compress_ctx(struct compress_ctx *cc)
191 kvfree(cc->private);
192 cc->private = NULL;
195 static int lzo_compress_pages(struct compress_ctx *cc)
199 ret = lzo1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
200 &cc->clen, cc->private);
203 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
241 static int lz4_init_compress_ctx(struct compress_ctx *cc)
243 cc->private = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
245 if (!cc->private)
249 * we do not change cc->clen to LZ4_compressBound(inputsize) to
253 cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
257 static void lz4_destroy_compress_ctx(struct compress_ctx *cc)
259 kvfree(cc->private);
260 cc->private = NULL;
263 static int lz4_compress_pages(struct compress_ctx *cc)
267 len = LZ4_compress_default(cc->rbuf, cc->cbuf->cdata, cc->rlen,
268 cc->clen, cc->private);
272 cc->clen = len;
309 static int zstd_init_compress_ctx(struct compress_ctx *cc)
316 params = ZSTD_getParams(F2FS_ZSTD_DEFAULT_CLEVEL, cc->rlen, 0);
319 workspace = f2fs_kvmalloc(F2FS_I_SB(cc->inode),
327 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
333 cc->private = workspace;
334 cc->private2 = stream;
336 cc->clen = cc->rlen - PAGE_SIZE - COMPRESS_HEADER_SIZE;
340 static void zstd_destroy_compress_ctx(struct compress_ctx *cc)
342 kvfree(cc->private);
343 cc->private = NULL;
344 cc->private2 = NULL;
347 static int zstd_compress_pages(struct compress_ctx *cc)
349 ZSTD_CStream *stream = cc->private2;
352 int src_size = cc->rlen;
357 inbuf.src = cc->rbuf;
361 outbuf.dst = cc->cbuf->cdata;
367 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
375 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id,
387 cc->clen = outbuf.pos;
475 static int lzorle_compress_pages(struct compress_ctx *cc)
479 ret = lzorle1x_1_compress(cc->rbuf, cc->rlen, cc->cbuf->cdata,
480 &cc->clen, cc->private);
483 KERN_ERR, F2FS_I_SB(cc->inode)->sb->s_id, ret);
585 static int f2fs_compress_pages(struct compress_ctx *cc)
587 struct f2fs_inode_info *fi = F2FS_I(cc->inode);
594 trace_f2fs_compress_pages_start(cc->inode, cc->cluster_idx,
595 cc->cluster_size, fi->i_compress_algorithm);
598 ret = cops->init_compress_ctx(cc);
603 max_len = COMPRESS_HEADER_SIZE + cc->clen;
604 cc->nr_cpages = DIV_ROUND_UP(max_len, PAGE_SIZE);
606 cc->cpages = page_array_alloc(cc->inode, cc->nr_cpages);
607 if (!cc->cpages) {
612 for (i = 0; i < cc->nr_cpages; i++) {
613 cc->cpages[i] = f2fs_compress_alloc_page();
614 if (!cc->cpages[i]) {
620 cc->rbuf = f2fs_vmap(cc->rpages, cc->cluster_size);
621 if (!cc->rbuf) {
626 cc->cbuf = f2fs_vmap(cc->cpages, cc->nr_cpages);
627 if (!cc->cbuf) {
632 ret = cops->compress_pages(cc);
636 max_len = PAGE_SIZE * (cc->cluster_size - 1) - COMPRESS_HEADER_SIZE;
638 if (cc->clen > max_len) {
643 cc->cbuf->clen = cpu_to_le32(cc->clen);
646 cc->cbuf->reserved[i] = cpu_to_le32(0);
648 new_nr_cpages = DIV_ROUND_UP(cc->clen + COMPRESS_HEADER_SIZE, PAGE_SIZE);
651 new_cpages = page_array_alloc(cc->inode, new_nr_cpages);
658 memset(&cc->cbuf->cdata[cc->clen], 0,
660 (cc->clen + COMPRESS_HEADER_SIZE));
662 vm_unmap_ram(cc->cbuf, cc->nr_cpages);
663 vm_unmap_ram(cc->rbuf, cc->cluster_size);
665 for (i = 0; i < cc->nr_cpages; i++) {
667 new_cpages[i] = cc->cpages[i];
670 f2fs_compress_free_page(cc->cpages[i]);
671 cc->cpages[i] = NULL;
675 cops->destroy_compress_ctx(cc);
677 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
678 cc->cpages = new_cpages;
679 cc->nr_cpages = new_nr_cpages;
681 trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
682 cc->clen, ret);
686 vm_unmap_ram(cc->cbuf, cc->nr_cpages);
688 vm_unmap_ram(cc->rbuf, cc->cluster_size);
690 for (i = 0; i < cc->nr_cpages; i++) {
691 if (cc->cpages[i])
692 f2fs_compress_free_page(cc->cpages[i]);
694 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
695 cc->cpages = NULL;
698 cops->destroy_compress_ctx(cc);
700 trace_f2fs_compress_pages_end(cc->inode, cc->cluster_idx,
701 cc->clen, ret);
798 static bool is_page_in_cluster(struct compress_ctx *cc, pgoff_t index)
800 if (cc->cluster_idx == NULL_CLUSTER)
802 return cc->cluster_idx == cluster_idx(cc, index);
805 bool f2fs_cluster_is_empty(struct compress_ctx *cc)
807 return cc->nr_rpages == 0;
810 static bool f2fs_cluster_is_full(struct compress_ctx *cc)
812 return cc->cluster_size == cc->nr_rpages;
815 bool f2fs_cluster_can_merge_page(struct compress_ctx *cc, pgoff_t index)
817 if (f2fs_cluster_is_empty(cc))
819 return is_page_in_cluster(cc, index);
822 static bool __cluster_may_compress(struct compress_ctx *cc)
824 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
825 loff_t i_size = i_size_read(cc->inode);
829 for (i = 0; i < cc->cluster_size; i++) {
830 struct page *page = cc->rpages[i];
846 static int __f2fs_cluster_blocks(struct compress_ctx *cc, bool compr)
851 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
852 ret = f2fs_get_dnode_of_data(&dn, start_idx_of_cluster(cc),
864 for (i = 1; i < cc->cluster_size; i++) {
884 static int f2fs_compressed_blocks(struct compress_ctx *cc)
886 return __f2fs_cluster_blocks(cc, true);
890 static int f2fs_cluster_blocks(struct compress_ctx *cc)
892 return __f2fs_cluster_blocks(cc, false);
897 struct compress_ctx cc = {
904 return f2fs_cluster_blocks(&cc);
907 static bool cluster_may_compress(struct compress_ctx *cc)
909 if (!f2fs_compressed_file(cc->inode))
911 if (f2fs_is_atomic_file(cc->inode))
913 if (f2fs_is_mmap_file(cc->inode))
915 if (!f2fs_cluster_is_full(cc))
917 if (unlikely(f2fs_cp_error(F2FS_I_SB(cc->inode))))
919 return __cluster_may_compress(cc);
922 static void set_cluster_writeback(struct compress_ctx *cc)
926 for (i = 0; i < cc->cluster_size; i++) {
927 if (cc->rpages[i])
928 set_page_writeback(cc->rpages[i]);
932 static void set_cluster_dirty(struct compress_ctx *cc)
936 for (i = 0; i < cc->cluster_size; i++)
937 if (cc->rpages[i])
938 set_page_dirty(cc->rpages[i]);
941 static int prepare_compress_overwrite(struct compress_ctx *cc,
944 struct f2fs_sb_info *sbi = F2FS_I_SB(cc->inode);
945 struct address_space *mapping = cc->inode->i_mapping;
950 pgoff_t start_idx = start_idx_of_cluster(cc);
955 ret = f2fs_cluster_blocks(cc);
960 prealloc = (ret < cc->cluster_size);
962 ret = f2fs_init_compress_ctx(cc);
967 for (i = 0; i < cc->cluster_size; i++) {
978 f2fs_compress_ctx_add_page(cc, page);
981 if (!f2fs_cluster_is_empty(cc)) {
984 ret = f2fs_read_multi_pages(cc, &bio, cc->cluster_size,
986 f2fs_put_rpages(cc);
987 f2fs_destroy_compress_ctx(cc, true);
993 ret = f2fs_init_compress_ctx(cc);
998 for (i = 0; i < cc->cluster_size; i++) {
999 f2fs_bug_on(sbi, cc->rpages[i]);
1008 f2fs_compress_ctx_add_page(cc, page);
1012 f2fs_put_rpages(cc);
1013 f2fs_unlock_rpages(cc, i + 1);
1014 f2fs_destroy_compress_ctx(cc, true);
1022 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
1024 for (i = cc->cluster_size - 1; i > 0; i--) {
1027 i = cc->cluster_size;
1039 *fsdata = cc->rpages;
1040 *pagep = cc->rpages[offset_in_cluster(cc, index)];
1041 return cc->cluster_size;
1045 f2fs_put_rpages(cc);
1046 f2fs_unlock_rpages(cc, i);
1047 f2fs_destroy_compress_ctx(cc, true);
1055 struct compress_ctx cc = {
1064 return prepare_compress_overwrite(&cc, pagep, index, fsdata);
1071 struct compress_ctx cc = {
1077 bool first_index = (index == cc.rpages[0]->index);
1080 set_cluster_dirty(&cc);
1082 f2fs_put_rpages_wbc(&cc, NULL, false, 1);
1083 f2fs_destroy_compress_ctx(&cc, false);
1137 static int f2fs_write_compressed_pages(struct compress_ctx *cc,
1142 struct inode *inode = cc->inode;
1147 .ino = cc->inode->i_ino,
1158 .encrypted = fscrypt_inode_uses_fs_layer_crypto(cc->inode),
1163 pgoff_t start_idx = start_idx_of_cluster(cc);
1164 unsigned int last_index = cc->cluster_size - 1;
1179 set_new_dnode(&dn, cc->inode, NULL, NULL, 0);
1185 for (i = 0; i < cc->cluster_size; i++) {
1191 psize = (loff_t)(cc->rpages[last_index]->index + 1) << PAGE_SHIFT;
1205 atomic_set(&cic->pending_pages, cc->nr_cpages);
1206 cic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
1210 cic->nr_rpages = cc->cluster_size;
1212 for (i = 0; i < cc->nr_cpages; i++) {
1213 f2fs_set_compressed_page(cc->cpages[i], inode,
1214 cc->rpages[i + 1]->index, cic);
1215 fio.compressed_page = cc->cpages[i];
1224 fio.page = cc->rpages[i + 1];
1228 cc->cpages[i] = fio.encrypted_page;
1232 set_cluster_writeback(cc);
1234 for (i = 0; i < cc->cluster_size; i++)
1235 cic->rpages[i] = cc->rpages[i];
1237 for (i = 0; i < cc->cluster_size; i++, dn.ofs_in_node++) {
1241 fio.page = cc->rpages[i];
1257 if (i > cc->nr_cpages) {
1268 fio.encrypted_page = cc->cpages[i - 1];
1270 fio.compressed_page = cc->cpages[i - 1];
1272 cc->cpages[i - 1] = NULL;
1276 inode_dec_dirty_pages(cc->inode);
1282 f2fs_i_compr_blocks_update(inode, cc->nr_cpages, true);
1284 set_inode_flag(cc->inode, FI_APPEND_WRITE);
1285 if (cc->cluster_idx == 0)
1299 f2fs_put_rpages(cc);
1300 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1301 cc->cpages = NULL;
1302 f2fs_destroy_compress_ctx(cc, false);
1306 page_array_free(cc->inode, cic->rpages, cc->cluster_size);
1309 fscrypt_finalize_bounce_page(&cc->cpages[i]);
1320 for (i = 0; i < cc->nr_cpages; i++) {
1321 if (!cc->cpages[i])
1323 f2fs_compress_free_page(cc->cpages[i]);
1324 cc->cpages[i] = NULL;
1326 page_array_free(cc->inode, cc->cpages, cc->nr_cpages);
1327 cc->cpages = NULL;
1358 static int f2fs_write_raw_pages(struct compress_ctx *cc,
1363 struct address_space *mapping = cc->inode->i_mapping;
1366 compr_blocks = f2fs_compressed_blocks(cc);
1368 for (i = 0; i < cc->cluster_size; i++) {
1369 if (!cc->rpages[i])
1372 redirty_page_for_writepage(wbc, cc->rpages[i]);
1373 unlock_page(cc->rpages[i]);
1379 for (i = 0; i < cc->cluster_size; i++) {
1380 if (!cc->rpages[i])
1383 lock_page(cc->rpages[i]);
1385 if (cc->rpages[i]->mapping != mapping) {
1387 unlock_page(cc->rpages[i]);
1391 if (!PageDirty(cc->rpages[i]))
1394 if (PageWriteback(cc->rpages[i])) {
1397 f2fs_wait_on_page_writeback(cc->rpages[i], DATA, true, true);
1400 if (!clear_page_dirty_for_io(cc->rpages[i]))
1403 ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted,
1408 unlock_page(cc->rpages[i]);
1416 if (IS_NOQUOTA(cc->inode))
1435 int f2fs_write_multi_pages(struct compress_ctx *cc,
1443 if (cluster_may_compress(cc)) {
1444 err = f2fs_compress_pages(cc);
1448 f2fs_put_rpages_wbc(cc, wbc, true, 1);
1452 err = f2fs_write_compressed_pages(cc, submitted,
1456 f2fs_bug_on(F2FS_I_SB(cc->inode), err != -EAGAIN);
1459 f2fs_bug_on(F2FS_I_SB(cc->inode), *submitted);
1461 err = f2fs_write_raw_pages(cc, submitted, wbc, io_type);
1462 f2fs_put_rpages_wbc(cc, wbc, false, 0);
1464 f2fs_destroy_compress_ctx(cc, false);
1468 struct decompress_io_ctx *f2fs_alloc_dic(struct compress_ctx *cc)
1471 pgoff_t start_idx = start_idx_of_cluster(cc);
1478 dic->rpages = page_array_alloc(cc->inode, cc->cluster_size);
1485 dic->inode = cc->inode;
1486 atomic_set(&dic->pending_pages, cc->nr_cpages);
1487 dic->cluster_idx = cc->cluster_idx;
1488 dic->cluster_size = cc->cluster_size;
1489 dic->log_cluster_size = cc->log_cluster_size;
1490 dic->nr_cpages = cc->nr_cpages;
1494 dic->rpages[i] = cc->rpages[i];
1495 dic->nr_rpages = cc->cluster_size;
1508 f2fs_set_compressed_page(page, cc->inode,