1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * fs/f2fs/inode.c
4 *
5 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
6 * http://www.samsung.com/
7 */
8 #include <linux/fs.h>
9 #include <linux/f2fs_fs.h>
10 #include <linux/buffer_head.h>
11 #include <linux/writeback.h>
12 #include <linux/sched/mm.h>
13 #include <linux/lz4.h>
14 #include <linux/zstd.h>
15
16 #include "f2fs.h"
17 #include "node.h"
18 #include "segment.h"
19 #include "xattr.h"
20
21 #include <trace/events/f2fs.h>
22
23 #ifdef CONFIG_F2FS_FS_COMPRESSION
24 extern const struct address_space_operations f2fs_compress_aops;
25 #endif
26
f2fs_mark_inode_dirty_sync(struct inode * inode,bool sync)27 void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync)
28 {
29 if (is_inode_flag_set(inode, FI_NEW_INODE))
30 return;
31
32 if (f2fs_inode_dirtied(inode, sync))
33 return;
34
35 mark_inode_dirty_sync(inode);
36 }
37
f2fs_set_inode_flags(struct inode * inode)38 void f2fs_set_inode_flags(struct inode *inode)
39 {
40 unsigned int flags = F2FS_I(inode)->i_flags;
41 unsigned int new_fl = 0;
42
43 if (flags & F2FS_SYNC_FL)
44 new_fl |= S_SYNC;
45 if (flags & F2FS_APPEND_FL)
46 new_fl |= S_APPEND;
47 if (flags & F2FS_IMMUTABLE_FL)
48 new_fl |= S_IMMUTABLE;
49 if (flags & F2FS_NOATIME_FL)
50 new_fl |= S_NOATIME;
51 if (flags & F2FS_DIRSYNC_FL)
52 new_fl |= S_DIRSYNC;
53 if (file_is_encrypt(inode))
54 new_fl |= S_ENCRYPTED;
55 if (file_is_verity(inode))
56 new_fl |= S_VERITY;
57 if (flags & F2FS_CASEFOLD_FL)
58 new_fl |= S_CASEFOLD;
59 inode_set_flags(inode, new_fl,
60 S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|
61 S_ENCRYPTED|S_VERITY|S_CASEFOLD);
62 }
63
__get_inode_rdev(struct inode * inode,struct page * node_page)64 static void __get_inode_rdev(struct inode *inode, struct page *node_page)
65 {
66 __le32 *addr = get_dnode_addr(inode, node_page);
67
68 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
69 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
70 if (addr[0])
71 inode->i_rdev = old_decode_dev(le32_to_cpu(addr[0]));
72 else
73 inode->i_rdev = new_decode_dev(le32_to_cpu(addr[1]));
74 }
75 }
76
__set_inode_rdev(struct inode * inode,struct page * node_page)77 static void __set_inode_rdev(struct inode *inode, struct page *node_page)
78 {
79 __le32 *addr = get_dnode_addr(inode, node_page);
80
81 if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
82 if (old_valid_dev(inode->i_rdev)) {
83 addr[0] = cpu_to_le32(old_encode_dev(inode->i_rdev));
84 addr[1] = 0;
85 } else {
86 addr[0] = 0;
87 addr[1] = cpu_to_le32(new_encode_dev(inode->i_rdev));
88 addr[2] = 0;
89 }
90 }
91 }
92
__recover_inline_status(struct inode * inode,struct page * ipage)93 static void __recover_inline_status(struct inode *inode, struct page *ipage)
94 {
95 void *inline_data = inline_data_addr(inode, ipage);
96 __le32 *start = inline_data;
97 __le32 *end = start + MAX_INLINE_DATA(inode) / sizeof(__le32);
98
99 while (start < end) {
100 if (*start++) {
101 f2fs_wait_on_page_writeback(ipage, NODE, true, true);
102
103 set_inode_flag(inode, FI_DATA_EXIST);
104 set_raw_inline(inode, F2FS_INODE(ipage));
105 set_page_dirty(ipage);
106 return;
107 }
108 }
109 return;
110 }
111
f2fs_enable_inode_chksum(struct f2fs_sb_info * sbi,struct page * page)112 static bool f2fs_enable_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
113 {
114 struct f2fs_inode *ri = &F2FS_NODE(page)->i;
115
116 if (!f2fs_sb_has_inode_chksum(sbi))
117 return false;
118
119 if (!IS_INODE(page) || !(ri->i_inline & F2FS_EXTRA_ATTR))
120 return false;
121
122 if (!F2FS_FITS_IN_INODE(ri, le16_to_cpu(ri->i_extra_isize),
123 i_inode_checksum))
124 return false;
125
126 return true;
127 }
128
f2fs_inode_chksum(struct f2fs_sb_info * sbi,struct page * page)129 static __u32 f2fs_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
130 {
131 struct f2fs_node *node = F2FS_NODE(page);
132 struct f2fs_inode *ri = &node->i;
133 __le32 ino = node->footer.ino;
134 __le32 gen = ri->i_generation;
135 __u32 chksum, chksum_seed;
136 __u32 dummy_cs = 0;
137 unsigned int offset = offsetof(struct f2fs_inode, i_inode_checksum);
138 unsigned int cs_size = sizeof(dummy_cs);
139
140 chksum = f2fs_chksum(sbi, sbi->s_chksum_seed, (__u8 *)&ino,
141 sizeof(ino));
142 chksum_seed = f2fs_chksum(sbi, chksum, (__u8 *)&gen, sizeof(gen));
143
144 chksum = f2fs_chksum(sbi, chksum_seed, (__u8 *)ri, offset);
145 chksum = f2fs_chksum(sbi, chksum, (__u8 *)&dummy_cs, cs_size);
146 offset += cs_size;
147 chksum = f2fs_chksum(sbi, chksum, (__u8 *)ri + offset,
148 F2FS_BLKSIZE - offset);
149 return chksum;
150 }
151
f2fs_inode_chksum_verify(struct f2fs_sb_info * sbi,struct page * page)152 bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page)
153 {
154 struct f2fs_inode *ri;
155 __u32 provided, calculated;
156
157 if (unlikely(is_sbi_flag_set(sbi, SBI_IS_SHUTDOWN)))
158 return true;
159
160 #ifdef CONFIG_F2FS_CHECK_FS
161 if (!f2fs_enable_inode_chksum(sbi, page))
162 #else
163 if (!f2fs_enable_inode_chksum(sbi, page) ||
164 PageDirty(page) || PageWriteback(page))
165 #endif
166 return true;
167
168 ri = &F2FS_NODE(page)->i;
169 provided = le32_to_cpu(ri->i_inode_checksum);
170 calculated = f2fs_inode_chksum(sbi, page);
171
172 if (provided != calculated)
173 f2fs_warn(sbi, "checksum invalid, nid = %lu, ino_of_node = %x, %x vs. %x",
174 page->index, ino_of_node(page), provided, calculated);
175
176 return provided == calculated;
177 }
178
f2fs_inode_chksum_set(struct f2fs_sb_info * sbi,struct page * page)179 void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page)
180 {
181 struct f2fs_inode *ri = &F2FS_NODE(page)->i;
182
183 if (!f2fs_enable_inode_chksum(sbi, page))
184 return;
185
186 ri->i_inode_checksum = cpu_to_le32(f2fs_inode_chksum(sbi, page));
187 }
188
sanity_check_compress_inode(struct inode * inode,struct f2fs_inode * ri)189 static bool sanity_check_compress_inode(struct inode *inode,
190 struct f2fs_inode *ri)
191 {
192 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
193 unsigned char clevel;
194
195 if (ri->i_compress_algorithm >= COMPRESS_MAX) {
196 f2fs_warn(sbi,
197 "%s: inode (ino=%lx) has unsupported compress algorithm: %u, run fsck to fix",
198 __func__, inode->i_ino, ri->i_compress_algorithm);
199 return false;
200 }
201 if (le64_to_cpu(ri->i_compr_blocks) >
202 SECTOR_TO_BLOCK(inode->i_blocks)) {
203 f2fs_warn(sbi,
204 "%s: inode (ino=%lx) has inconsistent i_compr_blocks:%llu, i_blocks:%llu, run fsck to fix",
205 __func__, inode->i_ino, le64_to_cpu(ri->i_compr_blocks),
206 SECTOR_TO_BLOCK(inode->i_blocks));
207 return false;
208 }
209 if (ri->i_log_cluster_size < MIN_COMPRESS_LOG_SIZE ||
210 ri->i_log_cluster_size > MAX_COMPRESS_LOG_SIZE) {
211 f2fs_warn(sbi,
212 "%s: inode (ino=%lx) has unsupported log cluster size: %u, run fsck to fix",
213 __func__, inode->i_ino, ri->i_log_cluster_size);
214 return false;
215 }
216
217 clevel = le16_to_cpu(ri->i_compress_flag) >>
218 COMPRESS_LEVEL_OFFSET;
219 switch (ri->i_compress_algorithm) {
220 case COMPRESS_LZO:
221 #ifdef CONFIG_F2FS_FS_LZO
222 if (clevel)
223 goto err_level;
224 #endif
225 break;
226 case COMPRESS_LZORLE:
227 #ifdef CONFIG_F2FS_FS_LZORLE
228 if (clevel)
229 goto err_level;
230 #endif
231 break;
232 case COMPRESS_LZ4:
233 #ifdef CONFIG_F2FS_FS_LZ4
234 #ifdef CONFIG_F2FS_FS_LZ4HC
235 if (clevel &&
236 (clevel < LZ4HC_MIN_CLEVEL || clevel > LZ4HC_MAX_CLEVEL))
237 goto err_level;
238 #else
239 if (clevel)
240 goto err_level;
241 #endif
242 #endif
243 break;
244 case COMPRESS_ZSTD:
245 #ifdef CONFIG_F2FS_FS_ZSTD
246 if (clevel < zstd_min_clevel() || clevel > zstd_max_clevel())
247 goto err_level;
248 #endif
249 break;
250 default:
251 goto err_level;
252 }
253
254 return true;
255 err_level:
256 f2fs_warn(sbi, "%s: inode (ino=%lx) has unsupported compress level: %u, run fsck to fix",
257 __func__, inode->i_ino, clevel);
258 return false;
259 }
260
sanity_check_inode(struct inode * inode,struct page * node_page)261 static bool sanity_check_inode(struct inode *inode, struct page *node_page)
262 {
263 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
264 struct f2fs_inode_info *fi = F2FS_I(inode);
265 struct f2fs_inode *ri = F2FS_INODE(node_page);
266 unsigned long long iblocks;
267
268 iblocks = le64_to_cpu(F2FS_INODE(node_page)->i_blocks);
269 if (!iblocks) {
270 f2fs_warn(sbi, "%s: corrupted inode i_blocks i_ino=%lx iblocks=%llu, run fsck to fix.",
271 __func__, inode->i_ino, iblocks);
272 return false;
273 }
274
275 if (ino_of_node(node_page) != nid_of_node(node_page)) {
276 f2fs_warn(sbi, "%s: corrupted inode footer i_ino=%lx, ino,nid: [%u, %u] run fsck to fix.",
277 __func__, inode->i_ino,
278 ino_of_node(node_page), nid_of_node(node_page));
279 return false;
280 }
281
282 if (f2fs_has_extra_attr(inode)) {
283 if (!f2fs_sb_has_extra_attr(sbi)) {
284 f2fs_warn(sbi, "%s: inode (ino=%lx) is with extra_attr, but extra_attr feature is off",
285 __func__, inode->i_ino);
286 return false;
287 }
288 if (fi->i_extra_isize > F2FS_TOTAL_EXTRA_ATTR_SIZE ||
289 fi->i_extra_isize < F2FS_MIN_EXTRA_ATTR_SIZE ||
290 fi->i_extra_isize % sizeof(__le32)) {
291 f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_extra_isize: %d, max: %zu",
292 __func__, inode->i_ino, fi->i_extra_isize,
293 F2FS_TOTAL_EXTRA_ATTR_SIZE);
294 return false;
295 }
296 if (f2fs_sb_has_flexible_inline_xattr(sbi) &&
297 f2fs_has_inline_xattr(inode) &&
298 (!fi->i_inline_xattr_size ||
299 fi->i_inline_xattr_size > MAX_INLINE_XATTR_SIZE)) {
300 f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_inline_xattr_size: %d, max: %zu",
301 __func__, inode->i_ino, fi->i_inline_xattr_size,
302 MAX_INLINE_XATTR_SIZE);
303 return false;
304 }
305 if (f2fs_sb_has_compression(sbi) &&
306 fi->i_flags & F2FS_COMPR_FL &&
307 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize,
308 i_compress_flag)) {
309 if (!sanity_check_compress_inode(inode, ri))
310 return false;
311 }
312 } else if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
313 f2fs_warn(sbi, "%s: corrupted inode ino=%lx, run fsck to fix.",
314 __func__, inode->i_ino);
315 return false;
316 }
317
318 if (!f2fs_sb_has_extra_attr(sbi)) {
319 if (f2fs_sb_has_project_quota(sbi)) {
320 f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.",
321 __func__, inode->i_ino, F2FS_FEATURE_PRJQUOTA);
322 return false;
323 }
324 if (f2fs_sb_has_inode_chksum(sbi)) {
325 f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.",
326 __func__, inode->i_ino, F2FS_FEATURE_INODE_CHKSUM);
327 return false;
328 }
329 if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
330 f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.",
331 __func__, inode->i_ino, F2FS_FEATURE_FLEXIBLE_INLINE_XATTR);
332 return false;
333 }
334 if (f2fs_sb_has_inode_crtime(sbi)) {
335 f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.",
336 __func__, inode->i_ino, F2FS_FEATURE_INODE_CRTIME);
337 return false;
338 }
339 if (f2fs_sb_has_compression(sbi)) {
340 f2fs_warn(sbi, "%s: corrupted inode ino=%lx, wrong feature flag: %u, run fsck to fix.",
341 __func__, inode->i_ino, F2FS_FEATURE_COMPRESSION);
342 return false;
343 }
344 }
345
346 if (f2fs_sanity_check_inline_data(inode)) {
347 f2fs_warn(sbi, "%s: inode (ino=%lx, mode=%u) should not have inline_data, run fsck to fix",
348 __func__, inode->i_ino, inode->i_mode);
349 return false;
350 }
351
352 if (f2fs_has_inline_dentry(inode) && !S_ISDIR(inode->i_mode)) {
353 f2fs_warn(sbi, "%s: inode (ino=%lx, mode=%u) should not have inline_dentry, run fsck to fix",
354 __func__, inode->i_ino, inode->i_mode);
355 return false;
356 }
357
358 if ((fi->i_flags & F2FS_CASEFOLD_FL) && !f2fs_sb_has_casefold(sbi)) {
359 f2fs_warn(sbi, "%s: inode (ino=%lx) has casefold flag, but casefold feature is off",
360 __func__, inode->i_ino);
361 return false;
362 }
363
364 if (fi->i_xattr_nid && f2fs_check_nid_range(sbi, fi->i_xattr_nid)) {
365 f2fs_warn(sbi, "%s: inode (ino=%lx) has corrupted i_xattr_nid: %u, run fsck to fix.",
366 __func__, inode->i_ino, fi->i_xattr_nid);
367 return false;
368 }
369
370 return true;
371 }
372
init_idisk_time(struct inode * inode)373 static void init_idisk_time(struct inode *inode)
374 {
375 struct f2fs_inode_info *fi = F2FS_I(inode);
376
377 fi->i_disk_time[0] = inode->i_atime;
378 fi->i_disk_time[1] = inode_get_ctime(inode);
379 fi->i_disk_time[2] = inode->i_mtime;
380 }
381
do_read_inode(struct inode * inode)382 static int do_read_inode(struct inode *inode)
383 {
384 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
385 struct f2fs_inode_info *fi = F2FS_I(inode);
386 struct page *node_page;
387 struct f2fs_inode *ri;
388 projid_t i_projid;
389
390 /* Check if ino is within scope */
391 if (f2fs_check_nid_range(sbi, inode->i_ino))
392 return -EINVAL;
393
394 node_page = f2fs_get_node_page(sbi, inode->i_ino);
395 if (IS_ERR(node_page))
396 return PTR_ERR(node_page);
397
398 ri = F2FS_INODE(node_page);
399
400 inode->i_mode = le16_to_cpu(ri->i_mode);
401 i_uid_write(inode, le32_to_cpu(ri->i_uid));
402 i_gid_write(inode, le32_to_cpu(ri->i_gid));
403 set_nlink(inode, le32_to_cpu(ri->i_links));
404 inode->i_size = le64_to_cpu(ri->i_size);
405 inode->i_blocks = SECTOR_FROM_BLOCK(le64_to_cpu(ri->i_blocks) - 1);
406
407 inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime);
408 inode_set_ctime(inode, le64_to_cpu(ri->i_ctime),
409 le32_to_cpu(ri->i_ctime_nsec));
410 inode->i_mtime.tv_sec = le64_to_cpu(ri->i_mtime);
411 inode->i_atime.tv_nsec = le32_to_cpu(ri->i_atime_nsec);
412 inode->i_mtime.tv_nsec = le32_to_cpu(ri->i_mtime_nsec);
413 inode->i_generation = le32_to_cpu(ri->i_generation);
414 if (S_ISDIR(inode->i_mode))
415 fi->i_current_depth = le32_to_cpu(ri->i_current_depth);
416 else if (S_ISREG(inode->i_mode))
417 fi->i_gc_failures[GC_FAILURE_PIN] =
418 le16_to_cpu(ri->i_gc_failures);
419 fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid);
420 fi->i_flags = le32_to_cpu(ri->i_flags);
421 if (S_ISREG(inode->i_mode))
422 fi->i_flags &= ~F2FS_PROJINHERIT_FL;
423 bitmap_zero(fi->flags, FI_MAX);
424 fi->i_advise = ri->i_advise;
425 fi->i_pino = le32_to_cpu(ri->i_pino);
426 fi->i_dir_level = ri->i_dir_level;
427
428 get_inline_info(inode, ri);
429
430 fi->i_extra_isize = f2fs_has_extra_attr(inode) ?
431 le16_to_cpu(ri->i_extra_isize) : 0;
432
433 if (f2fs_sb_has_flexible_inline_xattr(sbi)) {
434 fi->i_inline_xattr_size = le16_to_cpu(ri->i_inline_xattr_size);
435 } else if (f2fs_has_inline_xattr(inode) ||
436 f2fs_has_inline_dentry(inode)) {
437 fi->i_inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
438 } else {
439
440 /*
441 * Previous inline data or directory always reserved 200 bytes
442 * in inode layout, even if inline_xattr is disabled. In order
443 * to keep inline_dentry's structure for backward compatibility,
444 * we get the space back only from inline_data.
445 */
446 fi->i_inline_xattr_size = 0;
447 }
448
449 if (!sanity_check_inode(inode, node_page)) {
450 f2fs_put_page(node_page, 1);
451 set_sbi_flag(sbi, SBI_NEED_FSCK);
452 f2fs_handle_error(sbi, ERROR_CORRUPTED_INODE);
453 return -EFSCORRUPTED;
454 }
455
456 /* check data exist */
457 if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode))
458 __recover_inline_status(inode, node_page);
459
460 /* try to recover cold bit for non-dir inode */
461 if (!S_ISDIR(inode->i_mode) && !is_cold_node(node_page)) {
462 f2fs_wait_on_page_writeback(node_page, NODE, true, true);
463 set_cold_node(node_page, false);
464 set_page_dirty(node_page);
465 }
466
467 /* get rdev by using inline_info */
468 __get_inode_rdev(inode, node_page);
469
470 if (!f2fs_need_inode_block_update(sbi, inode->i_ino))
471 fi->last_disk_size = inode->i_size;
472
473 if (fi->i_flags & F2FS_PROJINHERIT_FL)
474 set_inode_flag(inode, FI_PROJ_INHERIT);
475
476 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_project_quota(sbi) &&
477 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
478 i_projid = (projid_t)le32_to_cpu(ri->i_projid);
479 else
480 i_projid = F2FS_DEF_PROJID;
481 fi->i_projid = make_kprojid(&init_user_ns, i_projid);
482
483 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_inode_crtime(sbi) &&
484 F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
485 fi->i_crtime.tv_sec = le64_to_cpu(ri->i_crtime);
486 fi->i_crtime.tv_nsec = le32_to_cpu(ri->i_crtime_nsec);
487 }
488
489 if (f2fs_has_extra_attr(inode) && f2fs_sb_has_compression(sbi) &&
490 (fi->i_flags & F2FS_COMPR_FL)) {
491 if (F2FS_FITS_IN_INODE(ri, fi->i_extra_isize,
492 i_compress_flag)) {
493 unsigned short compress_flag;
494
495 atomic_set(&fi->i_compr_blocks,
496 le64_to_cpu(ri->i_compr_blocks));
497 fi->i_compress_algorithm = ri->i_compress_algorithm;
498 fi->i_log_cluster_size = ri->i_log_cluster_size;
499 compress_flag = le16_to_cpu(ri->i_compress_flag);
500 fi->i_compress_level = compress_flag >>
501 COMPRESS_LEVEL_OFFSET;
502 fi->i_compress_flag = compress_flag &
503 GENMASK(COMPRESS_LEVEL_OFFSET - 1, 0);
504 fi->i_cluster_size = BIT(fi->i_log_cluster_size);
505 set_inode_flag(inode, FI_COMPRESSED_FILE);
506 }
507 }
508
509 init_idisk_time(inode);
510
511 /* Need all the flag bits */
512 f2fs_init_read_extent_tree(inode, node_page);
513 f2fs_init_age_extent_tree(inode);
514
515 if (!sanity_check_extent_cache(inode)) {
516 f2fs_put_page(node_page, 1);
517 f2fs_handle_error(sbi, ERROR_CORRUPTED_INODE);
518 return -EFSCORRUPTED;
519 }
520
521 f2fs_put_page(node_page, 1);
522
523 stat_inc_inline_xattr(inode);
524 stat_inc_inline_inode(inode);
525 stat_inc_inline_dir(inode);
526 stat_inc_compr_inode(inode);
527 stat_add_compr_blocks(inode, atomic_read(&fi->i_compr_blocks));
528
529 return 0;
530 }
531
is_meta_ino(struct f2fs_sb_info * sbi,unsigned int ino)532 static bool is_meta_ino(struct f2fs_sb_info *sbi, unsigned int ino)
533 {
534 return ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi) ||
535 ino == F2FS_COMPRESS_INO(sbi);
536 }
537
f2fs_iget(struct super_block * sb,unsigned long ino)538 struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
539 {
540 struct f2fs_sb_info *sbi = F2FS_SB(sb);
541 struct inode *inode;
542 int ret = 0;
543
544 inode = iget_locked(sb, ino);
545 if (!inode)
546 return ERR_PTR(-ENOMEM);
547
548 if (!(inode->i_state & I_NEW)) {
549 if (is_meta_ino(sbi, ino)) {
550 f2fs_err(sbi, "inaccessible inode: %lu, run fsck to repair", ino);
551 set_sbi_flag(sbi, SBI_NEED_FSCK);
552 ret = -EFSCORRUPTED;
553 trace_f2fs_iget_exit(inode, ret);
554 iput(inode);
555 f2fs_handle_error(sbi, ERROR_CORRUPTED_INODE);
556 return ERR_PTR(ret);
557 }
558
559 trace_f2fs_iget(inode);
560 return inode;
561 }
562
563 if (is_meta_ino(sbi, ino))
564 goto make_now;
565
566 ret = do_read_inode(inode);
567 if (ret)
568 goto bad_inode;
569 make_now:
570 if (ino == F2FS_NODE_INO(sbi)) {
571 inode->i_mapping->a_ops = &f2fs_node_aops;
572 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
573 } else if (ino == F2FS_META_INO(sbi)) {
574 inode->i_mapping->a_ops = &f2fs_meta_aops;
575 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
576 } else if (ino == F2FS_COMPRESS_INO(sbi)) {
577 #ifdef CONFIG_F2FS_FS_COMPRESSION
578 inode->i_mapping->a_ops = &f2fs_compress_aops;
579 /*
580 * generic_error_remove_page only truncates pages of regular
581 * inode
582 */
583 inode->i_mode |= S_IFREG;
584 #endif
585 mapping_set_gfp_mask(inode->i_mapping,
586 GFP_NOFS | __GFP_HIGHMEM | __GFP_MOVABLE);
587 } else if (S_ISREG(inode->i_mode)) {
588 inode->i_op = &f2fs_file_inode_operations;
589 inode->i_fop = &f2fs_file_operations;
590 inode->i_mapping->a_ops = &f2fs_dblock_aops;
591 } else if (S_ISDIR(inode->i_mode)) {
592 inode->i_op = &f2fs_dir_inode_operations;
593 inode->i_fop = &f2fs_dir_operations;
594 inode->i_mapping->a_ops = &f2fs_dblock_aops;
595 mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
596 } else if (S_ISLNK(inode->i_mode)) {
597 if (file_is_encrypt(inode))
598 inode->i_op = &f2fs_encrypted_symlink_inode_operations;
599 else
600 inode->i_op = &f2fs_symlink_inode_operations;
601 inode_nohighmem(inode);
602 inode->i_mapping->a_ops = &f2fs_dblock_aops;
603 } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
604 S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
605 inode->i_op = &f2fs_special_inode_operations;
606 init_special_inode(inode, inode->i_mode, inode->i_rdev);
607 } else {
608 ret = -EIO;
609 goto bad_inode;
610 }
611 f2fs_set_inode_flags(inode);
612
613 if (file_should_truncate(inode) &&
614 !is_sbi_flag_set(sbi, SBI_POR_DOING)) {
615 ret = f2fs_truncate(inode);
616 if (ret)
617 goto bad_inode;
618 file_dont_truncate(inode);
619 }
620
621 unlock_new_inode(inode);
622 trace_f2fs_iget(inode);
623 return inode;
624
625 bad_inode:
626 f2fs_inode_synced(inode);
627 iget_failed(inode);
628 trace_f2fs_iget_exit(inode, ret);
629 return ERR_PTR(ret);
630 }
631
f2fs_iget_retry(struct super_block * sb,unsigned long ino)632 struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino)
633 {
634 struct inode *inode;
635 retry:
636 inode = f2fs_iget(sb, ino);
637 if (IS_ERR(inode)) {
638 if (PTR_ERR(inode) == -ENOMEM) {
639 memalloc_retry_wait(GFP_NOFS);
640 goto retry;
641 }
642 }
643 return inode;
644 }
645
f2fs_update_inode(struct inode * inode,struct page * node_page)646 void f2fs_update_inode(struct inode *inode, struct page *node_page)
647 {
648 struct f2fs_inode *ri;
649 struct extent_tree *et = F2FS_I(inode)->extent_tree[EX_READ];
650
651 f2fs_wait_on_page_writeback(node_page, NODE, true, true);
652 set_page_dirty(node_page);
653
654 f2fs_inode_synced(inode);
655
656 ri = F2FS_INODE(node_page);
657
658 ri->i_mode = cpu_to_le16(inode->i_mode);
659 ri->i_advise = F2FS_I(inode)->i_advise;
660 ri->i_uid = cpu_to_le32(i_uid_read(inode));
661 ri->i_gid = cpu_to_le32(i_gid_read(inode));
662 ri->i_links = cpu_to_le32(inode->i_nlink);
663 ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks) + 1);
664
665 if (!f2fs_is_atomic_file(inode) ||
666 is_inode_flag_set(inode, FI_ATOMIC_COMMITTED))
667 ri->i_size = cpu_to_le64(i_size_read(inode));
668
669 if (et) {
670 read_lock(&et->lock);
671 set_raw_read_extent(&et->largest, &ri->i_ext);
672 read_unlock(&et->lock);
673 } else {
674 memset(&ri->i_ext, 0, sizeof(ri->i_ext));
675 }
676 set_raw_inline(inode, ri);
677
678 ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
679 ri->i_ctime = cpu_to_le64(inode_get_ctime(inode).tv_sec);
680 ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
681 ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
682 ri->i_ctime_nsec = cpu_to_le32(inode_get_ctime(inode).tv_nsec);
683 ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
684 if (S_ISDIR(inode->i_mode))
685 ri->i_current_depth =
686 cpu_to_le32(F2FS_I(inode)->i_current_depth);
687 else if (S_ISREG(inode->i_mode))
688 ri->i_gc_failures =
689 cpu_to_le16(F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN]);
690 ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid);
691 ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags);
692 ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino);
693 ri->i_generation = cpu_to_le32(inode->i_generation);
694 ri->i_dir_level = F2FS_I(inode)->i_dir_level;
695
696 if (f2fs_has_extra_attr(inode)) {
697 ri->i_extra_isize = cpu_to_le16(F2FS_I(inode)->i_extra_isize);
698
699 if (f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(inode)))
700 ri->i_inline_xattr_size =
701 cpu_to_le16(F2FS_I(inode)->i_inline_xattr_size);
702
703 if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)) &&
704 F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
705 i_projid)) {
706 projid_t i_projid;
707
708 i_projid = from_kprojid(&init_user_ns,
709 F2FS_I(inode)->i_projid);
710 ri->i_projid = cpu_to_le32(i_projid);
711 }
712
713 if (f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)) &&
714 F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
715 i_crtime)) {
716 ri->i_crtime =
717 cpu_to_le64(F2FS_I(inode)->i_crtime.tv_sec);
718 ri->i_crtime_nsec =
719 cpu_to_le32(F2FS_I(inode)->i_crtime.tv_nsec);
720 }
721
722 if (f2fs_sb_has_compression(F2FS_I_SB(inode)) &&
723 F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
724 i_compress_flag)) {
725 unsigned short compress_flag;
726
727 ri->i_compr_blocks =
728 cpu_to_le64(atomic_read(
729 &F2FS_I(inode)->i_compr_blocks));
730 ri->i_compress_algorithm =
731 F2FS_I(inode)->i_compress_algorithm;
732 compress_flag = F2FS_I(inode)->i_compress_flag |
733 F2FS_I(inode)->i_compress_level <<
734 COMPRESS_LEVEL_OFFSET;
735 ri->i_compress_flag = cpu_to_le16(compress_flag);
736 ri->i_log_cluster_size =
737 F2FS_I(inode)->i_log_cluster_size;
738 }
739 }
740
741 __set_inode_rdev(inode, node_page);
742
743 /* deleted inode */
744 if (inode->i_nlink == 0)
745 clear_page_private_inline(node_page);
746
747 init_idisk_time(inode);
748 #ifdef CONFIG_F2FS_CHECK_FS
749 f2fs_inode_chksum_set(F2FS_I_SB(inode), node_page);
750 #endif
751 }
752
f2fs_update_inode_page(struct inode * inode)753 void f2fs_update_inode_page(struct inode *inode)
754 {
755 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
756 struct page *node_page;
757 int count = 0;
758 retry:
759 node_page = f2fs_get_node_page(sbi, inode->i_ino);
760 if (IS_ERR(node_page)) {
761 int err = PTR_ERR(node_page);
762
763 /* The node block was truncated. */
764 if (err == -ENOENT)
765 return;
766
767 if (err == -ENOMEM || ++count <= DEFAULT_RETRY_IO_COUNT)
768 goto retry;
769 f2fs_stop_checkpoint(sbi, false, STOP_CP_REASON_UPDATE_INODE);
770 return;
771 }
772 f2fs_update_inode(inode, node_page);
773 f2fs_put_page(node_page, 1);
774 }
775
f2fs_write_inode(struct inode * inode,struct writeback_control * wbc)776 int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
777 {
778 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
779
780 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
781 inode->i_ino == F2FS_META_INO(sbi))
782 return 0;
783
784 /*
785 * atime could be updated without dirtying f2fs inode in lazytime mode
786 */
787 if (f2fs_is_time_consistent(inode) &&
788 !is_inode_flag_set(inode, FI_DIRTY_INODE))
789 return 0;
790
791 if (!f2fs_is_checkpoint_ready(sbi))
792 return -ENOSPC;
793
794 /*
795 * We need to balance fs here to prevent from producing dirty node pages
796 * during the urgent cleaning time when running out of free sections.
797 */
798 f2fs_update_inode_page(inode);
799 if (wbc && wbc->nr_to_write)
800 f2fs_balance_fs(sbi, true);
801 return 0;
802 }
803
804 /*
805 * Called at the last iput() if i_nlink is zero
806 */
f2fs_evict_inode(struct inode * inode)807 void f2fs_evict_inode(struct inode *inode)
808 {
809 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
810 struct f2fs_inode_info *fi = F2FS_I(inode);
811 nid_t xnid = fi->i_xattr_nid;
812 int err = 0;
813
814 f2fs_abort_atomic_write(inode, true);
815
816 if (fi->cow_inode) {
817 clear_inode_flag(fi->cow_inode, FI_COW_FILE);
818 iput(fi->cow_inode);
819 fi->cow_inode = NULL;
820 }
821
822 trace_f2fs_evict_inode(inode);
823 truncate_inode_pages_final(&inode->i_data);
824
825 if ((inode->i_nlink || is_bad_inode(inode)) &&
826 test_opt(sbi, COMPRESS_CACHE) && f2fs_compressed_file(inode))
827 f2fs_invalidate_compress_pages(sbi, inode->i_ino);
828
829 if (inode->i_ino == F2FS_NODE_INO(sbi) ||
830 inode->i_ino == F2FS_META_INO(sbi) ||
831 inode->i_ino == F2FS_COMPRESS_INO(sbi))
832 goto out_clear;
833
834 f2fs_bug_on(sbi, get_dirty_pages(inode));
835 f2fs_remove_dirty_inode(inode);
836
837 f2fs_destroy_extent_tree(inode);
838
839 if (inode->i_nlink || is_bad_inode(inode))
840 goto no_delete;
841
842 err = f2fs_dquot_initialize(inode);
843 if (err) {
844 err = 0;
845 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
846 }
847
848 f2fs_remove_ino_entry(sbi, inode->i_ino, APPEND_INO);
849 f2fs_remove_ino_entry(sbi, inode->i_ino, UPDATE_INO);
850 f2fs_remove_ino_entry(sbi, inode->i_ino, FLUSH_INO);
851
852 if (!is_sbi_flag_set(sbi, SBI_IS_FREEZING))
853 sb_start_intwrite(inode->i_sb);
854 set_inode_flag(inode, FI_NO_ALLOC);
855 i_size_write(inode, 0);
856 retry:
857 if (F2FS_HAS_BLOCKS(inode))
858 err = f2fs_truncate(inode);
859
860 if (time_to_inject(sbi, FAULT_EVICT_INODE))
861 err = -EIO;
862
863 if (!err) {
864 f2fs_lock_op(sbi);
865 err = f2fs_remove_inode_page(inode);
866 f2fs_unlock_op(sbi);
867 if (err == -ENOENT) {
868 err = 0;
869
870 /*
871 * in fuzzed image, another node may has the same
872 * block address as inode's, if it was truncated
873 * previously, truncation of inode node will fail.
874 */
875 if (is_inode_flag_set(inode, FI_DIRTY_INODE)) {
876 f2fs_warn(F2FS_I_SB(inode),
877 "f2fs_evict_inode: inconsistent node id, ino:%lu",
878 inode->i_ino);
879 f2fs_inode_synced(inode);
880 set_sbi_flag(sbi, SBI_NEED_FSCK);
881 }
882 }
883 }
884
885 /* give more chances, if ENOMEM case */
886 if (err == -ENOMEM) {
887 err = 0;
888 goto retry;
889 }
890
891 if (err) {
892 f2fs_update_inode_page(inode);
893 if (dquot_initialize_needed(inode))
894 set_sbi_flag(sbi, SBI_QUOTA_NEED_REPAIR);
895 }
896 if (!is_sbi_flag_set(sbi, SBI_IS_FREEZING))
897 sb_end_intwrite(inode->i_sb);
898 no_delete:
899 dquot_drop(inode);
900
901 stat_dec_inline_xattr(inode);
902 stat_dec_inline_dir(inode);
903 stat_dec_inline_inode(inode);
904 stat_dec_compr_inode(inode);
905 stat_sub_compr_blocks(inode,
906 atomic_read(&fi->i_compr_blocks));
907
908 if (likely(!f2fs_cp_error(sbi) &&
909 !is_sbi_flag_set(sbi, SBI_CP_DISABLED)))
910 f2fs_bug_on(sbi, is_inode_flag_set(inode, FI_DIRTY_INODE));
911 else
912 f2fs_inode_synced(inode);
913
914 /* for the case f2fs_new_inode() was failed, .i_ino is zero, skip it */
915 if (inode->i_ino)
916 invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino,
917 inode->i_ino);
918 if (xnid)
919 invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid);
920 if (inode->i_nlink) {
921 if (is_inode_flag_set(inode, FI_APPEND_WRITE))
922 f2fs_add_ino_entry(sbi, inode->i_ino, APPEND_INO);
923 if (is_inode_flag_set(inode, FI_UPDATE_WRITE))
924 f2fs_add_ino_entry(sbi, inode->i_ino, UPDATE_INO);
925 }
926 if (is_inode_flag_set(inode, FI_FREE_NID)) {
927 f2fs_alloc_nid_failed(sbi, inode->i_ino);
928 clear_inode_flag(inode, FI_FREE_NID);
929 } else {
930 /*
931 * If xattr nid is corrupted, we can reach out error condition,
932 * err & !f2fs_exist_written_data(sbi, inode->i_ino, ORPHAN_INO)).
933 * In that case, f2fs_check_nid_range() is enough to give a clue.
934 */
935 }
936 out_clear:
937 fscrypt_put_encryption_info(inode);
938 fsverity_cleanup_inode(inode);
939 clear_inode(inode);
940 }
941
942 /* caller should call f2fs_lock_op() */
f2fs_handle_failed_inode(struct inode * inode)943 void f2fs_handle_failed_inode(struct inode *inode)
944 {
945 struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
946 struct node_info ni;
947 int err;
948
949 /*
950 * clear nlink of inode in order to release resource of inode
951 * immediately.
952 */
953 clear_nlink(inode);
954
955 /*
956 * we must call this to avoid inode being remained as dirty, resulting
957 * in a panic when flushing dirty inodes in gdirty_list.
958 */
959 f2fs_update_inode_page(inode);
960 f2fs_inode_synced(inode);
961
962 /* don't make bad inode, since it becomes a regular file. */
963 unlock_new_inode(inode);
964
965 /*
966 * Note: we should add inode to orphan list before f2fs_unlock_op()
967 * so we can prevent losing this orphan when encoutering checkpoint
968 * and following suddenly power-off.
969 */
970 err = f2fs_get_node_info(sbi, inode->i_ino, &ni, false);
971 if (err) {
972 set_sbi_flag(sbi, SBI_NEED_FSCK);
973 set_inode_flag(inode, FI_FREE_NID);
974 f2fs_warn(sbi, "May loss orphan inode, run fsck to fix.");
975 goto out;
976 }
977
978 if (ni.blk_addr != NULL_ADDR) {
979 err = f2fs_acquire_orphan_inode(sbi);
980 if (err) {
981 set_sbi_flag(sbi, SBI_NEED_FSCK);
982 f2fs_warn(sbi, "Too many orphan inodes, run fsck to fix.");
983 } else {
984 f2fs_add_orphan_inode(inode);
985 }
986 f2fs_alloc_nid_done(sbi, inode->i_ino);
987 } else {
988 set_inode_flag(inode, FI_FREE_NID);
989 }
990
991 out:
992 f2fs_unlock_op(sbi);
993
994 /* iput will drop the inode object */
995 iput(inode);
996 }
997