~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/f2fs/inode.c

Version: ~ [ linux-5.18-rc6 ] ~ [ linux-5.17.6 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.38 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.114 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.192 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.241 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.277 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.312 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp