1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/f2fs/inline.c
4 * Copyright (c) 2013, Intel Corporation
5 * Authors: Huajun Li <huajun.li@intel.com>
6 * Haicheng Li <haicheng.li@intel.com>
7 */
8
9 #include <linux/fs.h>
10 #include <linux/f2fs_fs.h>
11 #include <linux/fiemap.h>
12
13 #include "f2fs.h"
14 #include "node.h"
15 #include <trace/events/f2fs.h>
16 #include <trace/events/android_fs.h>
17
support_inline_data(struct inode * inode)18 static bool support_inline_data(struct inode *inode)
19 {
20 if (f2fs_is_atomic_file(inode))
21 return false;
22 if (!S_ISREG(inode->i_mode) && !S_ISLNK(inode->i_mode))
23 return false;
24 if (i_size_read(inode) > MAX_INLINE_DATA(inode))
25 return false;
26 return true;
27 }
28
f2fs_may_inline_data(struct inode * inode)29 bool f2fs_may_inline_data(struct inode *inode)
30 {
31 if (!support_inline_data(inode))
32 return false;
33
34 return !f2fs_post_read_required(inode);
35 }
36
f2fs_sanity_check_inline_data(struct inode * inode)37 bool f2fs_sanity_check_inline_data(struct inode *inode)
38 {
39 if (!f2fs_has_inline_data(inode))
40 return false;
41
42 if (!support_inline_data(inode))
43 return true;
44
45 /*
46 * used by sanity_check_inode(), when disk layout fields has not
47 * been synchronized to inmem fields.
48 */
49 return (S_ISREG(inode->i_mode) &&
50 (file_is_encrypt(inode) || file_is_verity(inode) ||
51 (F2FS_I(inode)->i_flags & F2FS_COMPR_FL)));
52 }
53
f2fs_may_inline_dentry(struct inode * inode)54 bool f2fs_may_inline_dentry(struct inode *inode)
55 {
56 if (!test_opt(F2FS_I_SB(inode), INLINE_DENTRY))
57 return false;
58
59 if (!S_ISDIR(inode->i_mode))
60 return false;
61
62 return true;
63 }
64
f2fs_do_read_inline_data(struct page * page,struct page * ipage)65 void f2fs_do_read_inline_data(struct page *page, struct page *ipage)
66 {
67 struct inode *inode = page->mapping->host;
68
69 if (PageUptodate(page))
70 return;
71
72 f2fs_bug_on(F2FS_P_SB(page), page->index);
73
74 zero_user_segment(page, MAX_INLINE_DATA(inode), PAGE_SIZE);
75
76 /* Copy the whole inline data block */
77 memcpy_to_page(page, 0, inline_data_addr(inode, ipage),
78 MAX_INLINE_DATA(inode));
79 if (!PageUptodate(page))
80 SetPageUptodate(page);
81 }
82
f2fs_truncate_inline_inode(struct inode * inode,struct page * ipage,u64 from)83 void f2fs_truncate_inline_inode(struct inode *inode,
84 struct page *ipage, u64 from)
85 {
86 void *addr;
87
88 if (from >= MAX_INLINE_DATA(inode))
89 return;
90
91 addr = inline_data_addr(inode, ipage);
92
93 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
94 memset(addr + from, 0, MAX_INLINE_DATA(inode) - from);
95 set_page_dirty(ipage);
96
97 if (from == 0)
98 clear_inode_flag(inode, FI_DATA_EXIST);
99 }
100
f2fs_read_inline_data(struct inode * inode,struct page * page)101 int f2fs_read_inline_data(struct inode *inode, struct page *page)
102 {
103 struct page *ipage;
104
105 if (trace_android_fs_dataread_start_enabled()) {
106 char *path, pathbuf[MAX_TRACE_PATHBUF_LEN];
107
108 path = android_fstrace_get_pathname(pathbuf,
109 MAX_TRACE_PATHBUF_LEN,
110 inode);
111 trace_android_fs_dataread_start(inode, page_offset(page),
112 PAGE_SIZE, current->pid,
113 path, current->comm);
114 }
115
116 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
117 if (IS_ERR(ipage)) {
118 trace_android_fs_dataread_end(inode, page_offset(page),
119 PAGE_SIZE);
120 unlock_page(page);
121 return PTR_ERR(ipage);
122 }
123
124 if (!f2fs_has_inline_data(inode)) {
125 f2fs_put_page(ipage, 1);
126 trace_android_fs_dataread_end(inode, page_offset(page),
127 PAGE_SIZE);
128 return -EAGAIN;
129 }
130
131 if (page->index)
132 zero_user_segment(page, 0, PAGE_SIZE);
133 else
134 f2fs_do_read_inline_data(page, ipage);
135
136 if (!PageUptodate(page))
137 SetPageUptodate(page);
138 f2fs_put_page(ipage, 1);
139 trace_android_fs_dataread_end(inode, page_offset(page),
140 PAGE_SIZE);
141 unlock_page(page);
142 return 0;
143 }
144
f2fs_convert_inline_page(struct dnode_of_data * dn,struct page * page)145 int f2fs_convert_inline_page(struct dnode_of_data *dn, struct page *page)
146 {
147 struct f2fs_io_info fio = {
148 .sbi = F2FS_I_SB(dn->inode),
149 .ino = dn->inode->i_ino,
150 .type = DATA,
151 .op = REQ_OP_WRITE,
152 .op_flags = REQ_SYNC | REQ_PRIO,
153 .page = page,
154 .encrypted_page = NULL,
155 .io_type = FS_DATA_IO,
156 };
157 struct node_info ni;
158 int dirty, err;
159
160 if (!f2fs_exist_data(dn->inode))
161 goto clear_out;
162
163 err = f2fs_reserve_block(dn, 0);
164 if (err)
165 return err;
166
167 err = f2fs_get_node_info(fio.sbi, dn->nid, &ni, false);
168 if (err) {
169 f2fs_truncate_data_blocks_range(dn, 1);
170 f2fs_put_dnode(dn);
171 return err;
172 }
173
174 fio.version = ni.version;
175
176 if (unlikely(dn->data_blkaddr != NEW_ADDR)) {
177 f2fs_put_dnode(dn);
178 set_sbi_flag(fio.sbi, SBI_NEED_FSCK);
179 f2fs_warn(fio.sbi, "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.",
180 __func__, dn->inode->i_ino, dn->data_blkaddr);
181 return -EFSCORRUPTED;
182 }
183
184 f2fs_bug_on(F2FS_P_SB(page), PageWriteback(page));
185
186 f2fs_do_read_inline_data(page, dn->inode_page);
187 set_page_dirty(page);
188
189 /* clear dirty state */
190 dirty = clear_page_dirty_for_io(page);
191
192 /* write data page to try to make data consistent */
193 set_page_writeback(page);
194 ClearPageError(page);
195 fio.old_blkaddr = dn->data_blkaddr;
196 set_inode_flag(dn->inode, FI_HOT_DATA);
197 f2fs_outplace_write_data(dn, &fio);
198 f2fs_wait_on_page_writeback(page, DATA, true, true);
199 if (dirty) {
200 inode_dec_dirty_pages(dn->inode);
201 f2fs_remove_dirty_inode(dn->inode);
202 }
203
204 /* this converted inline_data should be recovered. */
205 set_inode_flag(dn->inode, FI_APPEND_WRITE);
206
207 /* clear inline data and flag after data writeback */
208 f2fs_truncate_inline_inode(dn->inode, dn->inode_page, 0);
209 clear_page_private_inline(dn->inode_page);
210 clear_out:
211 stat_dec_inline_inode(dn->inode);
212 clear_inode_flag(dn->inode, FI_INLINE_DATA);
213 f2fs_put_dnode(dn);
214 return 0;
215 }
216
f2fs_convert_inline_inode(struct inode * inode)217 int f2fs_convert_inline_inode(struct inode *inode)
218 {
219 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
220 struct dnode_of_data dn;
221 struct page *ipage, *page;
222 int err = 0;
223
224 if (!f2fs_has_inline_data(inode) ||
225 f2fs_hw_is_readonly(sbi) || f2fs_readonly(sbi->sb))
226 return 0;
227
228 err = f2fs_dquot_initialize(inode);
229 if (err)
230 return err;
231
232 page = f2fs_grab_cache_page(inode->i_mapping, 0, false);
233 if (!page)
234 return -ENOMEM;
235
236 f2fs_lock_op(sbi);
237
238 ipage = f2fs_get_node_page(sbi, inode->i_ino);
239 if (IS_ERR(ipage)) {
240 err = PTR_ERR(ipage);
241 goto out;
242 }
243
244 set_new_dnode(&dn, inode, ipage, ipage, 0);
245
246 if (f2fs_has_inline_data(inode))
247 err = f2fs_convert_inline_page(&dn, page);
248
249 f2fs_put_dnode(&dn);
250 out:
251 f2fs_unlock_op(sbi);
252
253 f2fs_put_page(page, 1);
254
255 if (!err)
256 f2fs_balance_fs(sbi, dn.node_changed);
257
258 return err;
259 }
260
f2fs_write_inline_data(struct inode * inode,struct page * page)261 int f2fs_write_inline_data(struct inode *inode, struct page *page)
262 {
263 struct dnode_of_data dn;
264 int err;
265
266 set_new_dnode(&dn, inode, NULL, NULL, 0);
267 err = f2fs_get_dnode_of_data(&dn, 0, LOOKUP_NODE);
268 if (err)
269 return err;
270
271 if (!f2fs_has_inline_data(inode)) {
272 f2fs_put_dnode(&dn);
273 return -EAGAIN;
274 }
275
276 f2fs_bug_on(F2FS_I_SB(inode), page->index);
277
278 f2fs_wait_on_page_writeback(dn.inode_page, NODE, true, true);
279 memcpy_from_page(inline_data_addr(inode, dn.inode_page),
280 page, 0, MAX_INLINE_DATA(inode));
281 set_page_dirty(dn.inode_page);
282
283 f2fs_clear_page_cache_dirty_tag(page);
284
285 set_inode_flag(inode, FI_APPEND_WRITE);
286 set_inode_flag(inode, FI_DATA_EXIST);
287
288 clear_page_private_inline(dn.inode_page);
289 f2fs_put_dnode(&dn);
290 return 0;
291 }
292
f2fs_recover_inline_data(struct inode * inode,struct page * npage)293 int f2fs_recover_inline_data(struct inode *inode, struct page *npage)
294 {
295 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
296 struct f2fs_inode *ri = NULL;
297 void *src_addr, *dst_addr;
298 struct page *ipage;
299
300 /*
301 * The inline_data recovery policy is as follows.
302 * [prev.] [next] of inline_data flag
303 * o o -> recover inline_data
304 * o x -> remove inline_data, and then recover data blocks
305 * x o -> remove data blocks, and then recover inline_data
306 * x x -> recover data blocks
307 */
308 if (IS_INODE(npage))
309 ri = F2FS_INODE(npage);
310
311 if (f2fs_has_inline_data(inode) &&
312 ri && (ri->i_inline & F2FS_INLINE_DATA)) {
313 process_inline:
314 ipage = f2fs_get_node_page(sbi, inode->i_ino);
315 if (IS_ERR(ipage))
316 return PTR_ERR(ipage);
317
318 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
319
320 src_addr = inline_data_addr(inode, npage);
321 dst_addr = inline_data_addr(inode, ipage);
322 memcpy(dst_addr, src_addr, MAX_INLINE_DATA(inode));
323
324 set_inode_flag(inode, FI_INLINE_DATA);
325 set_inode_flag(inode, FI_DATA_EXIST);
326
327 set_page_dirty(ipage);
328 f2fs_put_page(ipage, 1);
329 return 1;
330 }
331
332 if (f2fs_has_inline_data(inode)) {
333 ipage = f2fs_get_node_page(sbi, inode->i_ino);
334 if (IS_ERR(ipage))
335 return PTR_ERR(ipage);
336 f2fs_truncate_inline_inode(inode, ipage, 0);
337 stat_dec_inline_inode(inode);
338 clear_inode_flag(inode, FI_INLINE_DATA);
339 f2fs_put_page(ipage, 1);
340 } else if (ri && (ri->i_inline & F2FS_INLINE_DATA)) {
341 int ret;
342
343 ret = f2fs_truncate_blocks(inode, 0, false);
344 if (ret)
345 return ret;
346 stat_inc_inline_inode(inode);
347 goto process_inline;
348 }
349 return 0;
350 }
351
f2fs_find_in_inline_dir(struct inode * dir,const struct f2fs_filename * fname,struct page ** res_page)352 struct f2fs_dir_entry *f2fs_find_in_inline_dir(struct inode *dir,
353 const struct f2fs_filename *fname,
354 struct page **res_page)
355 {
356 struct f2fs_sb_info *sbi = F2FS_SB(dir->i_sb);
357 struct f2fs_dir_entry *de;
358 struct f2fs_dentry_ptr d;
359 struct page *ipage;
360 void *inline_dentry;
361
362 ipage = f2fs_get_node_page(sbi, dir->i_ino);
363 if (IS_ERR(ipage)) {
364 *res_page = ipage;
365 return NULL;
366 }
367
368 inline_dentry = inline_data_addr(dir, ipage);
369
370 make_dentry_ptr_inline(dir, &d, inline_dentry);
371 de = f2fs_find_target_dentry(&d, fname, NULL);
372 unlock_page(ipage);
373 if (IS_ERR(de)) {
374 *res_page = ERR_CAST(de);
375 de = NULL;
376 }
377 if (de)
378 *res_page = ipage;
379 else
380 f2fs_put_page(ipage, 0);
381
382 return de;
383 }
384
f2fs_make_empty_inline_dir(struct inode * inode,struct inode * parent,struct page * ipage)385 int f2fs_make_empty_inline_dir(struct inode *inode, struct inode *parent,
386 struct page *ipage)
387 {
388 struct f2fs_dentry_ptr d;
389 void *inline_dentry;
390
391 inline_dentry = inline_data_addr(inode, ipage);
392
393 make_dentry_ptr_inline(inode, &d, inline_dentry);
394 f2fs_do_make_empty_dir(inode, parent, &d);
395
396 set_page_dirty(ipage);
397
398 /* update i_size to MAX_INLINE_DATA */
399 if (i_size_read(inode) < MAX_INLINE_DATA(inode))
400 f2fs_i_size_write(inode, MAX_INLINE_DATA(inode));
401 return 0;
402 }
403
404 /*
405 * NOTE: ipage is grabbed by caller, but if any error occurs, we should
406 * release ipage in this function.
407 */
f2fs_move_inline_dirents(struct inode * dir,struct page * ipage,void * inline_dentry)408 static int f2fs_move_inline_dirents(struct inode *dir, struct page *ipage,
409 void *inline_dentry)
410 {
411 struct page *page;
412 struct dnode_of_data dn;
413 struct f2fs_dentry_block *dentry_blk;
414 struct f2fs_dentry_ptr src, dst;
415 int err;
416
417 page = f2fs_grab_cache_page(dir->i_mapping, 0, true);
418 if (!page) {
419 f2fs_put_page(ipage, 1);
420 return -ENOMEM;
421 }
422
423 set_new_dnode(&dn, dir, ipage, NULL, 0);
424 err = f2fs_reserve_block(&dn, 0);
425 if (err)
426 goto out;
427
428 if (unlikely(dn.data_blkaddr != NEW_ADDR)) {
429 f2fs_put_dnode(&dn);
430 set_sbi_flag(F2FS_P_SB(page), SBI_NEED_FSCK);
431 f2fs_warn(F2FS_P_SB(page), "%s: corrupted inline inode ino=%lx, i_addr[0]:0x%x, run fsck to fix.",
432 __func__, dir->i_ino, dn.data_blkaddr);
433 err = -EFSCORRUPTED;
434 goto out;
435 }
436
437 f2fs_wait_on_page_writeback(page, DATA, true, true);
438
439 dentry_blk = page_address(page);
440
441 /*
442 * Start by zeroing the full block, to ensure that all unused space is
443 * zeroed and no uninitialized memory is leaked to disk.
444 */
445 memset(dentry_blk, 0, F2FS_BLKSIZE);
446
447 make_dentry_ptr_inline(dir, &src, inline_dentry);
448 make_dentry_ptr_block(dir, &dst, dentry_blk);
449
450 /* copy data from inline dentry block to new dentry block */
451 memcpy(dst.bitmap, src.bitmap, src.nr_bitmap);
452 memcpy(dst.dentry, src.dentry, SIZE_OF_DIR_ENTRY * src.max);
453 memcpy(dst.filename, src.filename, src.max * F2FS_SLOT_LEN);
454
455 if (!PageUptodate(page))
456 SetPageUptodate(page);
457 set_page_dirty(page);
458
459 /* clear inline dir and flag after data writeback */
460 f2fs_truncate_inline_inode(dir, ipage, 0);
461
462 stat_dec_inline_dir(dir);
463 clear_inode_flag(dir, FI_INLINE_DENTRY);
464
465 /*
466 * should retrieve reserved space which was used to keep
467 * inline_dentry's structure for backward compatibility.
468 */
469 if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir)) &&
470 !f2fs_has_inline_xattr(dir))
471 F2FS_I(dir)->i_inline_xattr_size = 0;
472
473 f2fs_i_depth_write(dir, 1);
474 if (i_size_read(dir) < PAGE_SIZE)
475 f2fs_i_size_write(dir, PAGE_SIZE);
476 out:
477 f2fs_put_page(page, 1);
478 return err;
479 }
480
f2fs_add_inline_entries(struct inode * dir,void * inline_dentry)481 static int f2fs_add_inline_entries(struct inode *dir, void *inline_dentry)
482 {
483 struct f2fs_dentry_ptr d;
484 unsigned long bit_pos = 0;
485 int err = 0;
486
487 make_dentry_ptr_inline(dir, &d, inline_dentry);
488
489 while (bit_pos < d.max) {
490 struct f2fs_dir_entry *de;
491 struct f2fs_filename fname;
492 nid_t ino;
493 umode_t fake_mode;
494
495 if (!test_bit_le(bit_pos, d.bitmap)) {
496 bit_pos++;
497 continue;
498 }
499
500 de = &d.dentry[bit_pos];
501
502 if (unlikely(!de->name_len)) {
503 bit_pos++;
504 continue;
505 }
506
507 /*
508 * We only need the disk_name and hash to move the dentry.
509 * We don't need the original or casefolded filenames.
510 */
511 memset(&fname, 0, sizeof(fname));
512 fname.disk_name.name = d.filename[bit_pos];
513 fname.disk_name.len = le16_to_cpu(de->name_len);
514 fname.hash = de->hash_code;
515
516 ino = le32_to_cpu(de->ino);
517 fake_mode = f2fs_get_de_type(de) << S_SHIFT;
518
519 err = f2fs_add_regular_entry(dir, &fname, NULL, ino, fake_mode);
520 if (err)
521 goto punch_dentry_pages;
522
523 bit_pos += GET_DENTRY_SLOTS(le16_to_cpu(de->name_len));
524 }
525 return 0;
526 punch_dentry_pages:
527 truncate_inode_pages(&dir->i_data, 0);
528 f2fs_truncate_blocks(dir, 0, false);
529 f2fs_remove_dirty_inode(dir);
530 return err;
531 }
532
f2fs_move_rehashed_dirents(struct inode * dir,struct page * ipage,void * inline_dentry)533 static int f2fs_move_rehashed_dirents(struct inode *dir, struct page *ipage,
534 void *inline_dentry)
535 {
536 void *backup_dentry;
537 int err;
538
539 backup_dentry = f2fs_kmalloc(F2FS_I_SB(dir),
540 MAX_INLINE_DATA(dir), GFP_F2FS_ZERO);
541 if (!backup_dentry) {
542 f2fs_put_page(ipage, 1);
543 return -ENOMEM;
544 }
545
546 memcpy(backup_dentry, inline_dentry, MAX_INLINE_DATA(dir));
547 f2fs_truncate_inline_inode(dir, ipage, 0);
548
549 unlock_page(ipage);
550
551 err = f2fs_add_inline_entries(dir, backup_dentry);
552 if (err)
553 goto recover;
554
555 lock_page(ipage);
556
557 stat_dec_inline_dir(dir);
558 clear_inode_flag(dir, FI_INLINE_DENTRY);
559
560 /*
561 * should retrieve reserved space which was used to keep
562 * inline_dentry's structure for backward compatibility.
563 */
564 if (!f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(dir)) &&
565 !f2fs_has_inline_xattr(dir))
566 F2FS_I(dir)->i_inline_xattr_size = 0;
567
568 kfree(backup_dentry);
569 return 0;
570 recover:
571 lock_page(ipage);
572 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
573 memcpy(inline_dentry, backup_dentry, MAX_INLINE_DATA(dir));
574 f2fs_i_depth_write(dir, 0);
575 f2fs_i_size_write(dir, MAX_INLINE_DATA(dir));
576 set_page_dirty(ipage);
577 f2fs_put_page(ipage, 1);
578
579 kfree(backup_dentry);
580 return err;
581 }
582
do_convert_inline_dir(struct inode * dir,struct page * ipage,void * inline_dentry)583 static int do_convert_inline_dir(struct inode *dir, struct page *ipage,
584 void *inline_dentry)
585 {
586 if (!F2FS_I(dir)->i_dir_level)
587 return f2fs_move_inline_dirents(dir, ipage, inline_dentry);
588 else
589 return f2fs_move_rehashed_dirents(dir, ipage, inline_dentry);
590 }
591
f2fs_try_convert_inline_dir(struct inode * dir,struct dentry * dentry)592 int f2fs_try_convert_inline_dir(struct inode *dir, struct dentry *dentry)
593 {
594 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
595 struct page *ipage;
596 struct f2fs_filename fname;
597 void *inline_dentry = NULL;
598 int err = 0;
599
600 if (!f2fs_has_inline_dentry(dir))
601 return 0;
602
603 f2fs_lock_op(sbi);
604
605 err = f2fs_setup_filename(dir, &dentry->d_name, 0, &fname);
606 if (err)
607 goto out;
608
609 ipage = f2fs_get_node_page(sbi, dir->i_ino);
610 if (IS_ERR(ipage)) {
611 err = PTR_ERR(ipage);
612 goto out_fname;
613 }
614
615 if (f2fs_has_enough_room(dir, ipage, &fname)) {
616 f2fs_put_page(ipage, 1);
617 goto out_fname;
618 }
619
620 inline_dentry = inline_data_addr(dir, ipage);
621
622 err = do_convert_inline_dir(dir, ipage, inline_dentry);
623 if (!err)
624 f2fs_put_page(ipage, 1);
625 out_fname:
626 f2fs_free_filename(&fname);
627 out:
628 f2fs_unlock_op(sbi);
629 return err;
630 }
631
f2fs_add_inline_entry(struct inode * dir,const struct f2fs_filename * fname,struct inode * inode,nid_t ino,umode_t mode)632 int f2fs_add_inline_entry(struct inode *dir, const struct f2fs_filename *fname,
633 struct inode *inode, nid_t ino, umode_t mode)
634 {
635 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
636 struct page *ipage;
637 unsigned int bit_pos;
638 void *inline_dentry = NULL;
639 struct f2fs_dentry_ptr d;
640 int slots = GET_DENTRY_SLOTS(fname->disk_name.len);
641 struct page *page = NULL;
642 int err = 0;
643
644 ipage = f2fs_get_node_page(sbi, dir->i_ino);
645 if (IS_ERR(ipage))
646 return PTR_ERR(ipage);
647
648 inline_dentry = inline_data_addr(dir, ipage);
649 make_dentry_ptr_inline(dir, &d, inline_dentry);
650
651 bit_pos = f2fs_room_for_filename(d.bitmap, slots, d.max);
652 if (bit_pos >= d.max) {
653 err = do_convert_inline_dir(dir, ipage, inline_dentry);
654 if (err)
655 return err;
656 err = -EAGAIN;
657 goto out;
658 }
659
660 if (inode) {
661 f2fs_down_write(&F2FS_I(inode)->i_sem);
662 page = f2fs_init_inode_metadata(inode, dir, fname, ipage);
663 if (IS_ERR(page)) {
664 err = PTR_ERR(page);
665 goto fail;
666 }
667 }
668
669 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
670
671 f2fs_update_dentry(ino, mode, &d, &fname->disk_name, fname->hash,
672 bit_pos);
673
674 set_page_dirty(ipage);
675
676 /* we don't need to mark_inode_dirty now */
677 if (inode) {
678 f2fs_i_pino_write(inode, dir->i_ino);
679
680 /* synchronize inode page's data from inode cache */
681 if (is_inode_flag_set(inode, FI_NEW_INODE))
682 f2fs_update_inode(inode, page);
683
684 f2fs_put_page(page, 1);
685 }
686
687 f2fs_update_parent_metadata(dir, inode, 0);
688 fail:
689 if (inode)
690 f2fs_up_write(&F2FS_I(inode)->i_sem);
691 out:
692 f2fs_put_page(ipage, 1);
693 return err;
694 }
695
f2fs_delete_inline_entry(struct f2fs_dir_entry * dentry,struct page * page,struct inode * dir,struct inode * inode)696 void f2fs_delete_inline_entry(struct f2fs_dir_entry *dentry, struct page *page,
697 struct inode *dir, struct inode *inode)
698 {
699 struct f2fs_dentry_ptr d;
700 void *inline_dentry;
701 int slots = GET_DENTRY_SLOTS(le16_to_cpu(dentry->name_len));
702 unsigned int bit_pos;
703 int i;
704
705 lock_page(page);
706 f2fs_wait_on_page_writeback(page, NODE, true, true);
707
708 inline_dentry = inline_data_addr(dir, page);
709 make_dentry_ptr_inline(dir, &d, inline_dentry);
710
711 bit_pos = dentry - d.dentry;
712 for (i = 0; i < slots; i++)
713 __clear_bit_le(bit_pos + i, d.bitmap);
714
715 set_page_dirty(page);
716 f2fs_put_page(page, 1);
717
718 dir->i_ctime = dir->i_mtime = current_time(dir);
719 f2fs_mark_inode_dirty_sync(dir, false);
720
721 if (inode)
722 f2fs_drop_nlink(dir, inode);
723 }
724
f2fs_empty_inline_dir(struct inode * dir)725 bool f2fs_empty_inline_dir(struct inode *dir)
726 {
727 struct f2fs_sb_info *sbi = F2FS_I_SB(dir);
728 struct page *ipage;
729 unsigned int bit_pos = 2;
730 void *inline_dentry;
731 struct f2fs_dentry_ptr d;
732
733 ipage = f2fs_get_node_page(sbi, dir->i_ino);
734 if (IS_ERR(ipage))
735 return false;
736
737 inline_dentry = inline_data_addr(dir, ipage);
738 make_dentry_ptr_inline(dir, &d, inline_dentry);
739
740 bit_pos = find_next_bit_le(d.bitmap, d.max, bit_pos);
741
742 f2fs_put_page(ipage, 1);
743
744 if (bit_pos < d.max)
745 return false;
746
747 return true;
748 }
749
f2fs_read_inline_dir(struct file * file,struct dir_context * ctx,struct fscrypt_str * fstr)750 int f2fs_read_inline_dir(struct file *file, struct dir_context *ctx,
751 struct fscrypt_str *fstr)
752 {
753 struct inode *inode = file_inode(file);
754 struct page *ipage = NULL;
755 struct f2fs_dentry_ptr d;
756 void *inline_dentry = NULL;
757 int err;
758
759 make_dentry_ptr_inline(inode, &d, inline_dentry);
760
761 if (ctx->pos == d.max)
762 return 0;
763
764 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
765 if (IS_ERR(ipage))
766 return PTR_ERR(ipage);
767
768 /*
769 * f2fs_readdir was protected by inode.i_rwsem, it is safe to access
770 * ipage without page's lock held.
771 */
772 unlock_page(ipage);
773
774 inline_dentry = inline_data_addr(inode, ipage);
775
776 make_dentry_ptr_inline(inode, &d, inline_dentry);
777
778 err = f2fs_fill_dentries(ctx, &d, 0, fstr);
779 if (!err)
780 ctx->pos = d.max;
781
782 f2fs_put_page(ipage, 0);
783 return err < 0 ? err : 0;
784 }
785
f2fs_inline_data_fiemap(struct inode * inode,struct fiemap_extent_info * fieinfo,__u64 start,__u64 len)786 int f2fs_inline_data_fiemap(struct inode *inode,
787 struct fiemap_extent_info *fieinfo, __u64 start, __u64 len)
788 {
789 __u64 byteaddr, ilen;
790 __u32 flags = FIEMAP_EXTENT_DATA_INLINE | FIEMAP_EXTENT_NOT_ALIGNED |
791 FIEMAP_EXTENT_LAST;
792 struct node_info ni;
793 struct page *ipage;
794 int err = 0;
795
796 ipage = f2fs_get_node_page(F2FS_I_SB(inode), inode->i_ino);
797 if (IS_ERR(ipage))
798 return PTR_ERR(ipage);
799
800 if ((S_ISREG(inode->i_mode) || S_ISLNK(inode->i_mode)) &&
801 !f2fs_has_inline_data(inode)) {
802 err = -EAGAIN;
803 goto out;
804 }
805
806 if (S_ISDIR(inode->i_mode) && !f2fs_has_inline_dentry(inode)) {
807 err = -EAGAIN;
808 goto out;
809 }
810
811 ilen = min_t(size_t, MAX_INLINE_DATA(inode), i_size_read(inode));
812 if (start >= ilen)
813 goto out;
814 if (start + len < ilen)
815 ilen = start + len;
816 ilen -= start;
817
818 err = f2fs_get_node_info(F2FS_I_SB(inode), inode->i_ino, &ni, false);
819 if (err)
820 goto out;
821
822 byteaddr = (__u64)ni.blk_addr << inode->i_sb->s_blocksize_bits;
823 byteaddr += (char *)inline_data_addr(inode, ipage) -
824 (char *)F2FS_INODE(ipage);
825 err = fiemap_fill_next_extent(fieinfo, start, byteaddr, ilen, flags);
826 trace_f2fs_fiemap(inode, start, byteaddr, ilen, flags, err);
827 out:
828 f2fs_put_page(ipage, 1);
829 return err;
830 }
831