USB: serial: option: add support for D-Link DWM-157 C1
[linux/fpc-iii.git] / fs / f2fs / inline.c
blobad80f916b64d4abc4433f732a4fa24bc42bcb5b4
1 /*
2 * fs/f2fs/inline.c
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.
9 */
11 #include <linux/fs.h>
12 #include <linux/f2fs_fs.h>
14 #include "f2fs.h"
15 #include "node.h"
17 bool f2fs_may_inline_data(struct inode *inode)
19 if (!test_opt(F2FS_I_SB(inode), INLINE_DATA))
20 return false;
22 if (f2fs_is_atomic_file(inode))
23 return false;
25 if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))
26 return false;
28 if (i_size_read(inode) > MAX_INLINE_DATA)
29 return false;
31 if (f2fs_encrypted_inode(inode) && S_ISREG(inode->i_mode))
32 return false;
34 return true;
37 bool f2fs_may_inline_dentry(struct inode *inode)
39 if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY))
40 return false;
42 if (!S_ISDIR(inode->i_mode))
43 return false;
45 return true;
48 void read_inline_data(struct page *page, struct page *ipage)
50 void *src_addr, *dst_addr;
52 if (PageUptodate(page))
53 return;
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)
70 void *addr;
72 if (from >= MAX_INLINE_DATA)
73 return false;
75 addr = inline_data_addr(ipage);
77 f2fs_wait_on_page_writeback(ipage, NODE);
78 memset(addr + from, 0, MAX_INLINE_DATA - from);
80 return true;
83 int f2fs_read_inline_data(struct inode *inode, struct page *page)
85 struct page *ipage;
87 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
88 if (IS_ERR(ipage)) {
89 unlock_page(page);
90 return PTR_ERR(ipage);
93 if (!f2fs_has_inline_data(inode)) {
94 f2fs_put_page(ipage, 1);
95 return -EAGAIN;
98 if (page->index)
99 zero_user_segment(page, 0, PAGE_CACHE_SIZE);
100 else
101 read_inline_data(page, ipage);
103 SetPageUptodate(page);
104 f2fs_put_page(ipage, 1);
105 unlock_page(page);
106 return 0;
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),
114 .type = DATA,
115 .rw = WRITE_SYNC | REQ_PRIO,
116 .page = page,
117 .encrypted_page = NULL,
119 int dirty, err;
121 f2fs_bug_on(F2FS_I_SB(dn->inode), page->index);
123 if (!f2fs_exist_data(dn->inode))
124 goto clear_out;
126 err = f2fs_reserve_block(dn, 0);
127 if (err)
128 return err;
130 f2fs_wait_on_page_writeback(page, DATA);
132 if (PageUptodate(page))
133 goto no_update;
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);
144 no_update:
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);
157 if (dirty)
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);
165 clear_out:
166 stat_dec_inline_inode(dn->inode);
167 f2fs_clear_inline_inode(dn->inode);
168 sync_inode_page(dn);
169 f2fs_put_dnode(dn);
170 return 0;
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;
178 int err = 0;
180 page = grab_cache_page(inode->i_mapping, 0);
181 if (!page)
182 return -ENOMEM;
184 f2fs_lock_op(sbi);
186 ipage = get_node_page(sbi, inode->i_ino);
187 if (IS_ERR(ipage)) {
188 err = PTR_ERR(ipage);
189 goto out;
192 set_new_dnode(&dn, inode, ipage, ipage, 0);
194 if (f2fs_has_inline_data(inode))
195 err = f2fs_convert_inline_page(&dn, page);
197 f2fs_put_dnode(&dn);
198 out:
199 f2fs_unlock_op(sbi);
201 f2fs_put_page(page, 1);
202 return err;
205 int f2fs_write_inline_data(struct inode *inode, struct page *page)
207 void *src_addr, *dst_addr;
208 struct dnode_of_data dn;
209 int err;
211 set_new_dnode(&dn, inode, NULL, NULL, 0);
212 err = get_dnode_of_data(&dn, 0, LOOKUP_NODE);
213 if (err)
214 return err;
216 if (!f2fs_has_inline_data(inode)) {
217 f2fs_put_dnode(&dn);
218 return -EAGAIN;
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);
233 f2fs_put_dnode(&dn);
234 return 0;
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;
242 struct page *ipage;
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
252 if (IS_INODE(npage))
253 ri = F2FS_INODE(npage);
255 if (f2fs_has_inline_data(inode) &&
256 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
257 process_inline:
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);
272 return true;
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))
279 return false;
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))
285 return false;
286 goto process_inline;
288 return 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;
299 struct page *ipage;
300 f2fs_hash_t namehash;
302 ipage = get_node_page(sbi, dir->i_ino);
303 if (IS_ERR(ipage))
304 return NULL;
306 namehash = f2fs_dentry_hash(&name, fname);
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);
312 unlock_page(ipage);
313 if (de)
314 *res_page = ipage;
315 else
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);
323 return de;
326 struct f2fs_dir_entry *f2fs_parent_inline_dir(struct inode *dir,
327 struct page **p)
329 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
330 struct page *ipage;
331 struct f2fs_dir_entry *de;
332 struct f2fs_inline_dentry *dentry_blk;
334 ipage = get_node_page(sbi, dir->i_ino);
335 if (IS_ERR(ipage))
336 return NULL;
338 dentry_blk = inline_data_addr(ipage);
339 de = &dentry_blk->dentry[1];
340 *p = ipage;
341 unlock_page(ipage);
342 return de;
345 int make_empty_inline_dir(struct inode *inode, struct inode *parent,
346 struct page *ipage)
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);
363 return 0;
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)
373 struct page *page;
374 struct dnode_of_data dn;
375 struct f2fs_dentry_block *dentry_blk;
376 int err;
378 page = grab_cache_page(dir->i_mapping, 0);
379 if (!page) {
380 f2fs_put_page(ipage, 1);
381 return -ENOMEM;
384 set_new_dnode(&dn, dir, ipage, NULL, 0);
385 err = f2fs_reserve_block(&dn, 0);
386 if (err)
387 goto out;
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);
426 out:
427 f2fs_put_page(page, 1);
428 return err;
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);
435 struct page *ipage;
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;
443 int err = 0;
445 ipage = get_node_page(sbi, dir->i_ino);
446 if (IS_ERR(ipage))
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);
454 if (err)
455 return err;
456 err = -EAGAIN;
457 goto out;
460 if (inode) {
461 down_write(&F2FS_I(inode)->i_sem);
462 page = init_inode_metadata(inode, dir, name, ipage);
463 if (IS_ERR(page)) {
464 err = PTR_ERR(page);
465 goto fail;
469 f2fs_wait_on_page_writeback(ipage, NODE);
471 name_hash = f2fs_dentry_hash(name, NULL);
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 */
478 if (inode) {
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);
485 fail:
486 if (inode)
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);
493 out:
494 f2fs_put_page(ipage, 1);
495 return err;
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;
504 int i;
506 lock_page(page);
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;
519 if (inode)
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);
528 struct page *ipage;
529 unsigned int bit_pos = 2;
530 struct f2fs_inline_dentry *dentry_blk;
532 ipage = get_node_page(sbi, dir->i_ino);
533 if (IS_ERR(ipage))
534 return false;
536 dentry_blk = inline_data_addr(ipage);
537 bit_pos = find_next_bit_le(&dentry_blk->dentry_bitmap,
538 NR_INLINE_DENTRY,
539 bit_pos);
541 f2fs_put_page(ipage, 1);
543 if (bit_pos < NR_INLINE_DENTRY)
544 return false;
546 return true;
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)
558 return 0;
560 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
561 if (IS_ERR(ipage))
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);
572 return 0;
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 |
580 FIEMAP_EXTENT_LAST;
581 struct node_info ni;
582 struct page *ipage;
583 int err = 0;
585 ipage = get_node_page(F2FS_I_SB(inode), inode->i_ino);
586 if (IS_ERR(ipage))
587 return PTR_ERR(ipage);
589 if (!f2fs_has_inline_data(inode)) {
590 err = -EAGAIN;
591 goto out;
594 ilen = min_t(size_t, MAX_INLINE_DATA, i_size_read(inode));
595 if (start >= ilen)
596 goto out;
597 if (start + len < ilen)
598 ilen = start + len;
599 ilen -= start;
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);
605 out:
606 f2fs_put_page(ipage, 1);
607 return err;