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