3 * Copyright (c) 2013, Intel Corporation
4 * Authors: Huajun Li <huajun.li@intel.com>
5 * Haicheng Li <haicheng.li@intel.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #include <linux/f2fs_fs.h>
17 bool f2fs_may_inline_data(struct inode *inode)
19 if (!test_opt(F2FS_I_SB(inode), INLINE_DATA))
22 if (f2fs_is_atomic_file(inode))
25 if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))
28 if (i_size_read(inode) > MAX_INLINE_DATA)
31 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
37 bool f2fs_may_inline_dentry(struct inode *inode)
39 if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY))
42 if (!S_ISDIR(inode->i_mode))
48 void read_inline_data(struct page *page, struct page *ipage)
50 void *src_addr, *dst_addr;
52 if (PageUptodate(page))
55 f2fs_bug_on(F2FS_P_SB(page), page->index);
57 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
59 /* Copy the whole inline data block */
60 src_addr = inline_data_addr(ipage);
61 dst_addr = kmap_atomic(page);
62 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
63 flush_dcache_page(page);
64 kunmap_atomic(dst_addr);
65 SetPageUptodate(page);
68 bool truncate_inline_inode(struct page *ipage, u64 from)
72 if (from >= MAX_INLINE_DATA)
75 addr = inline_data_addr(ipage);
77 f2fs_wait_on_page_writeback(ipage, NODE);
78 memset(addr + from, 0, MAX_INLINE_DATA - from);
83 int f2fs_read_inline_data(struct inode *inode, struct page *page)
87 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
90 return PTR_ERR(ipage);
93 if (!f2fs_has_inline_data(inode)) {
94 f2fs_put_page(ipage, 1);
99 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
101 read_inline_data(page, ipage);
103 SetPageUptodate(page);
104 f2fs_put_page(ipage, 1);
109 int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
111 void *src_addr, *dst_addr;
112 struct f2fs_io_info fio = {
113 .sbi = F2FS_I_SB(dn->inode),
115 .rw = WRITE_SYNC | REQ_PRIO,
117 .encrypted_page = NULL,
121 f2fs_bug_on(F2FS_I_SB(dn->inode), page->index);
123 if (!f2fs_exist_data(dn->inode))
126 err = f2fs_reserve_block(dn, 0);
130 f2fs_wait_on_page_writeback(page, DATA);
132 if (PageUptodate(page))
135 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
137 /* Copy the whole inline data block */
138 src_addr = inline_data_addr(dn->inode_page);
139 dst_addr = kmap_atomic(page);
140 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
141 flush_dcache_page(page);
142 kunmap_atomic(dst_addr);
143 SetPageUptodate(page);
145 set_page_dirty(page);
147 /* clear dirty state */
148 dirty = clear_page_dirty_for_io(page);
150 /* write data page to try to make data consistent */
151 set_page_writeback(page);
152 fio.blk_addr = dn->data_blkaddr;
153 write_data_page(dn, &fio);
154 set_data_blkaddr(dn);
155 f2fs_update_extent_cache(dn);
156 f2fs_wait_on_page_writeback(page, DATA);
158 inode_dec_dirty_pages(dn->inode);
160 /* this converted inline_data should be recovered. */
161 set_inode_flag(F2FS_I(dn->inode), FI_APPEND_WRITE);
163 /* clear inline data and flag after data writeback */
164 truncate_inline_inode(dn->inode_page, 0);
166 stat_dec_inline_inode(dn->inode);
167 f2fs_clear_inline_inode(dn->inode);
173 int f2fs_convert_inline_inode(struct inode *inode)
175 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
176 struct dnode_of_data dn;
177 struct page *ipage, *page;
180 page = grab_cache_page(inode->i_mapping, 0);
186 ipage = get_node_page(sbi, inode->i_ino);
188 err = PTR_ERR(ipage);
192 set_new_dnode(&dn, inode, ipage, ipage, 0);
194 if (f2fs_has_inline_data(inode))
195 err = f2fs_convert_inline_page(&dn, page);
201 f2fs_put_page(page, 1);
205 int f2fs_write_inline_data(struct inode *inode, struct page *page)
207 void *src_addr, *dst_addr;
208 struct dnode_of_data dn;
211 set_new_dnode(&dn, inode, NULL, NULL, 0);
212 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
216 if (!f2fs_has_inline_data(inode)) {
221 f2fs_bug_on(F2FS_I_SB(inode), page->index);
223 f2fs_wait_on_page_writeback(dn.inode_page, NODE);
224 src_addr = kmap_atomic(page);
225 dst_addr = inline_data_addr(dn.inode_page);
226 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
227 kunmap_atomic(src_addr);
229 set_inode_flag(F2FS_I(inode), FI_APPEND_WRITE);
230 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
232 sync_inode_page(&dn);
237 bool recover_inline_data(struct inode *inode, struct page *npage)
239 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
240 struct f2fs_inode *ri = NULL;
241 void *src_addr, *dst_addr;
245 * The inline_data recovery policy is as follows.
246 * [prev.] [next] of inline_data flag
247 * o o -> recover inline_data
248 * o x -> remove inline_data, and then recover data blocks
249 * x o -> remove inline_data, and then recover inline_data
250 * x x -> recover data blocks
253 ri = F2FS_INODE(npage);
255 if (f2fs_has_inline_data(inode) &&
256 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
258 ipage = get_node_page(sbi, inode->i_ino);
259 f2fs_bug_on(sbi, IS_ERR(ipage));
261 f2fs_wait_on_page_writeback(ipage, NODE);
263 src_addr = inline_data_addr(npage);
264 dst_addr = inline_data_addr(ipage);
265 memcpy(dst_addr, src_addr, MAX_INLINE_DATA);
267 set_inode_flag(F2FS_I(inode), FI_INLINE_DATA);
268 set_inode_flag(F2FS_I(inode), FI_DATA_EXIST);
270 update_inode(inode, ipage);
271 f2fs_put_page(ipage, 1);
275 if (f2fs_has_inline_data(inode)) {
276 ipage = get_node_page(sbi, inode->i_ino);
277 f2fs_bug_on(sbi, IS_ERR(ipage));
278 if (!truncate_inline_inode(ipage, 0))
280 f2fs_clear_inline_inode(inode);
281 update_inode(inode, ipage);
282 f2fs_put_page(ipage, 1);
283 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
284 if (truncate_blocks(inode, 0, false))
291 struct f2fs_dir_entry *find_in_inline_dir(struct inode *dir,
292 struct f2fs_filename *fname, struct page **res_page)
294 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
295 struct f2fs_inline_dentry *inline_dentry;
296 struct qstr name = FSTR_TO_QSTR(&fname->disk_name);
297 struct f2fs_dir_entry *de;
298 struct f2fs_dentry_ptr d;
300 f2fs_hash_t namehash;
302 ipage = get_node_page(sbi, dir->i_ino);
306 namehash = f2fs_dentry_hash(&name);
308 inline_dentry = inline_data_addr(ipage);
310 make_dentry_ptr(NULL, &d, (void *)inline_dentry, 2);
311 de = find_target_dentry(fname, namehash, NULL, &d);
316 f2fs_put_page(ipage, 0);
319 * For the most part, it should be a bug when name_len is zero.
320 * We stop here for figuring out where the bugs has occurred.
322 f2fs_bug_on(sbi, d.max < 0);
326 struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir,
329 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
331 struct f2fs_dir_entry *de;
332 struct f2fs_inline_dentry *dentry_blk;
334 ipage = get_node_page(sbi, dir->i_ino);
338 dentry_blk = inline_data_addr(ipage);
339 de = &dentry_blk->dentry[1];
345 int make_empty_inline_dir(struct inode *inode, struct inode *parent,
348 struct f2fs_inline_dentry *dentry_blk;
349 struct f2fs_dentry_ptr d;
351 dentry_blk = inline_data_addr(ipage);
353 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
354 do_make_empty_dir(inode, parent, &d);
356 set_page_dirty(ipage);
358 /* update i_size to MAX_INLINE_DATA */
359 if (i_size_read(inode) < MAX_INLINE_DATA) {
360 i_size_write(inode, MAX_INLINE_DATA);
361 set_inode_flag(F2FS_I(inode), FI_UPDATE_DIR);
367 * NOTE: ipage is grabbed by caller, but if any error occurs, we should
368 * release ipage in this function.
370 static int f2fs_convert_inline_dir(struct inode *dir, struct page *ipage,
371 struct f2fs_inline_dentry *inline_dentry)
374 struct dnode_of_data dn;
375 struct f2fs_dentry_block *dentry_blk;
378 page = grab_cache_page(dir->i_mapping, 0);
380 f2fs_put_page(ipage, 1);
384 set_new_dnode(&dn, dir, ipage, NULL, 0);
385 err = f2fs_reserve_block(&dn, 0);
389 f2fs_wait_on_page_writeback(page, DATA);
390 zero_user_segment(page, MAX_INLINE_DATA, PAGE_CACHE_SIZE);
392 dentry_blk = kmap_atomic(page);
394 /* copy data from inline dentry block to new dentry block */
395 memcpy(dentry_blk->dentry_bitmap, inline_dentry->dentry_bitmap,
396 INLINE_DENTRY_BITMAP_SIZE);
397 memset(dentry_blk->dentry_bitmap + INLINE_DENTRY_BITMAP_SIZE, 0,
398 SIZE_OF_DENTRY_BITMAP - INLINE_DENTRY_BITMAP_SIZE);
400 * we do not need to zero out remainder part of dentry and filename
401 * field, since we have used bitmap for marking the usage status of
402 * them, besides, we can also ignore copying/zeroing reserved space
403 * of dentry block, because them haven't been used so far.
405 memcpy(dentry_blk->dentry, inline_dentry->dentry,
406 sizeof(struct f2fs_dir_entry) * NR_INLINE_DENTRY);
407 memcpy(dentry_blk->filename, inline_dentry->filename,
408 NR_INLINE_DENTRY * F2FS_SLOT_LEN);
410 kunmap_atomic(dentry_blk);
411 SetPageUptodate(page);
412 set_page_dirty(page);
414 /* clear inline dir and flag after data writeback */
415 truncate_inline_inode(ipage, 0);
417 stat_dec_inline_dir(dir);
418 clear_inode_flag(F2FS_I(dir), FI_INLINE_DENTRY);
420 if (i_size_read(dir) < PAGE_CACHE_SIZE) {
421 i_size_write(dir, PAGE_CACHE_SIZE);
422 set_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
425 sync_inode_page(&dn);
427 f2fs_put_page(page, 1);
431 int f2fs_add_inline_entry(struct inode *dir, const struct qstr *name,
432 struct inode *inode, nid_t ino, umode_t mode)
434 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
436 unsigned int bit_pos;
437 f2fs_hash_t name_hash;
438 size_t namelen = name->len;
439 struct f2fs_inline_dentry *dentry_blk = NULL;
440 struct f2fs_dentry_ptr d;
441 int slots = GET_DENTRY_SLOTS(namelen);
442 struct page *page = NULL;
445 ipage = get_node_page(sbi, dir->i_ino);
447 return PTR_ERR(ipage);
449 dentry_blk = inline_data_addr(ipage);
450 bit_pos = room_for_filename(&dentry_blk->dentry_bitmap,
451 slots, NR_INLINE_DENTRY);
452 if (bit_pos >= NR_INLINE_DENTRY) {
453 err = f2fs_convert_inline_dir(dir, ipage, dentry_blk);
461 down_write(&F2FS_I(inode)->i_sem);
462 page = init_inode_metadata(inode, dir, name, ipage);
469 f2fs_wait_on_page_writeback(ipage, NODE);
471 name_hash = f2fs_dentry_hash(name);
472 make_dentry_ptr(NULL, &d, (void *)dentry_blk, 2);
473 f2fs_update_dentry(ino, mode, &d, name, name_hash, bit_pos);
475 set_page_dirty(ipage);
477 /* we don't need to mark_inode_dirty now */
479 F2FS_I(inode)->i_pino = dir->i_ino;
480 update_inode(inode, page);
481 f2fs_put_page(page, 1);
484 update_parent_metadata(dir, inode, 0);
487 up_write(&F2FS_I(inode)->i_sem);
489 if (is_inode_flag_set(F2FS_I(dir), FI_UPDATE_DIR)) {
490 update_inode(dir, ipage);
491 clear_inode_flag(F2FS_I(dir), FI_UPDATE_DIR);
494 f2fs_put_page(ipage, 1);
498 void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
499 struct inode *dir, struct inode *inode)
501 struct f2fs_inline_dentry *inline_dentry;
502 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
503 unsigned int bit_pos;
507 f2fs_wait_on_page_writeback(page, NODE);
509 inline_dentry = inline_data_addr(page);
510 bit_pos = dentry - inline_dentry->dentry;
511 for (i = 0; i < slots; i++)
512 test_and_clear_bit_le(bit_pos + i,
513 &inline_dentry->dentry_bitmap);
515 set_page_dirty(page);
517 dir->i_ctime = dir->i_mtime = CURRENT_TIME;
520 f2fs_drop_nlink(dir, inode, page);
522 f2fs_put_page(page, 1);
525 bool f2fs_empty_inline_dir(struct inode *dir)
527 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
529 unsigned int bit_pos = 2;
530 struct f2fs_inline_dentry *dentry_blk;
532 ipage = get_node_page(sbi, dir->i_ino);
536 dentry_blk = inline_data_addr(ipage);
537 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
541 f2fs_put_page(ipage, 1);
543 if (bit_pos < NR_INLINE_DENTRY)
549 int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
550 struct f2fs_str *fstr)
552 struct inode *inode = file_inode(file);
553 struct f2fs_inline_dentry *inline_dentry = NULL;
554 struct page *ipage = NULL;
555 struct f2fs_dentry_ptr d;
557 if (ctx->pos == NR_INLINE_DENTRY)
560 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
562 return PTR_ERR(ipage);
564 inline_dentry = inline_data_addr(ipage);
566 make_dentry_ptr(inode, &d, (void *)inline_dentry, 2);
568 if (!f2fs_fill_dentries(ctx, &d, 0, fstr))
569 ctx->pos = NR_INLINE_DENTRY;
571 f2fs_put_page(ipage, 1);
575 int f2fs_inline_data_fiemap(struct inode *inode,
576 struct fiemap_extent_info *fieinfo, __u64 start, __u64 len)
578 __u64 byteaddr, ilen;
579 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
585 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
587 return PTR_ERR(ipage);
589 if (!f2fs_has_inline_data(inode)) {
594 ilen = min_t(size_t, MAX_INLINE_DATA, i_size_read(inode));
597 if (start + len < ilen)
601 get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni);
602 byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits;
603 byteaddr += (char *)inline_data_addr(ipage) - (char *)F2FS_INODE(ipage);
604 err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags);
606 f2fs_put_page(ipage, 1);