linux/fs/f2fs/inode.c
<<
>>
Prefs
   1/*
   2 * fs/f2fs/inode.c
   3 *
   4 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
   5 *             http://www.samsung.com/
   6 *
   7 * This program is free software; you can redistribute it and/or modify
   8 * it under the terms of the GNU General Public License version 2 as
   9 * published by the Free Software Foundation.
  10 */
  11#include <linux/fs.h>
  12#include <linux/f2fs_fs.h>
  13#include <linux/buffer_head.h>
  14#include <linux/backing-dev.h>
  15#include <linux/writeback.h>
  16
  17#include "f2fs.h"
  18#include "node.h"
  19#include "segment.h"
  20
  21#include <trace/events/f2fs.h>
  22
  23void f2fs_mark_inode_dirty_sync(struct inode *inode, bool sync)
  24{
  25        if (is_inode_flag_set(inode, FI_NEW_INODE))
  26                return;
  27
  28        if (f2fs_inode_dirtied(inode, sync))
  29                return;
  30
  31        mark_inode_dirty_sync(inode);
  32}
  33
  34void f2fs_set_inode_flags(struct inode *inode)
  35{
  36        unsigned int flags = F2FS_I(inode)->i_flags;
  37        unsigned int new_fl = 0;
  38
  39        if (flags & F2FS_SYNC_FL)
  40                new_fl |= S_SYNC;
  41        if (flags & F2FS_APPEND_FL)
  42                new_fl |= S_APPEND;
  43        if (flags & F2FS_IMMUTABLE_FL)
  44                new_fl |= S_IMMUTABLE;
  45        if (flags & F2FS_NOATIME_FL)
  46                new_fl |= S_NOATIME;
  47        if (flags & F2FS_DIRSYNC_FL)
  48                new_fl |= S_DIRSYNC;
  49        if (f2fs_encrypted_inode(inode))
  50                new_fl |= S_ENCRYPTED;
  51        inode_set_flags(inode, new_fl,
  52                        S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC|
  53                        S_ENCRYPTED);
  54}
  55
  56static void __get_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
  57{
  58        int extra_size = get_extra_isize(inode);
  59
  60        if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
  61                        S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  62                if (ri->i_addr[extra_size])
  63                        inode->i_rdev = old_decode_dev(
  64                                le32_to_cpu(ri->i_addr[extra_size]));
  65                else
  66                        inode->i_rdev = new_decode_dev(
  67                                le32_to_cpu(ri->i_addr[extra_size + 1]));
  68        }
  69}
  70
  71static bool __written_first_block(struct f2fs_inode *ri)
  72{
  73        block_t addr = le32_to_cpu(ri->i_addr[offset_in_addr(ri)]);
  74
  75        if (is_valid_blkaddr(addr))
  76                return true;
  77        return false;
  78}
  79
  80static void __set_inode_rdev(struct inode *inode, struct f2fs_inode *ri)
  81{
  82        int extra_size = get_extra_isize(inode);
  83
  84        if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  85                if (old_valid_dev(inode->i_rdev)) {
  86                        ri->i_addr[extra_size] =
  87                                cpu_to_le32(old_encode_dev(inode->i_rdev));
  88                        ri->i_addr[extra_size + 1] = 0;
  89                } else {
  90                        ri->i_addr[extra_size] = 0;
  91                        ri->i_addr[extra_size + 1] =
  92                                cpu_to_le32(new_encode_dev(inode->i_rdev));
  93                        ri->i_addr[extra_size + 2] = 0;
  94                }
  95        }
  96}
  97
  98static void __recover_inline_status(struct inode *inode, struct page *ipage)
  99{
 100        void *inline_data = inline_data_addr(inode, ipage);
 101        __le32 *start = inline_data;
 102        __le32 *end = start + MAX_INLINE_DATA(inode) / sizeof(__le32);
 103
 104        while (start < end) {
 105                if (*start++) {
 106                        f2fs_wait_on_page_writeback(ipage, NODE, true);
 107
 108                        set_inode_flag(inode, FI_DATA_EXIST);
 109                        set_raw_inline(inode, F2FS_INODE(ipage));
 110                        set_page_dirty(ipage);
 111                        return;
 112                }
 113        }
 114        return;
 115}
 116
 117static bool f2fs_enable_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
 118{
 119        struct f2fs_inode *ri = &F2FS_NODE(page)->i;
 120
 121        if (!f2fs_sb_has_inode_chksum(sbi->sb))
 122                return false;
 123
 124        if (!RAW_IS_INODE(F2FS_NODE(page)) || !(ri->i_inline & F2FS_EXTRA_ATTR))
 125                return false;
 126
 127        if (!F2FS_FITS_IN_INODE(ri, le16_to_cpu(ri->i_extra_isize),
 128                                i_inode_checksum))
 129                return false;
 130
 131        return true;
 132}
 133
 134static __u32 f2fs_inode_chksum(struct f2fs_sb_info *sbi, struct page *page)
 135{
 136        struct f2fs_node *node = F2FS_NODE(page);
 137        struct f2fs_inode *ri = &node->i;
 138        __le32 ino = node->footer.ino;
 139        __le32 gen = ri->i_generation;
 140        __u32 chksum, chksum_seed;
 141        __u32 dummy_cs = 0;
 142        unsigned int offset = offsetof(struct f2fs_inode, i_inode_checksum);
 143        unsigned int cs_size = sizeof(dummy_cs);
 144
 145        chksum = f2fs_chksum(sbi, sbi->s_chksum_seed, (__u8 *)&ino,
 146                                                        sizeof(ino));
 147        chksum_seed = f2fs_chksum(sbi, chksum, (__u8 *)&gen, sizeof(gen));
 148
 149        chksum = f2fs_chksum(sbi, chksum_seed, (__u8 *)ri, offset);
 150        chksum = f2fs_chksum(sbi, chksum, (__u8 *)&dummy_cs, cs_size);
 151        offset += cs_size;
 152        chksum = f2fs_chksum(sbi, chksum, (__u8 *)ri + offset,
 153                                                F2FS_BLKSIZE - offset);
 154        return chksum;
 155}
 156
 157bool f2fs_inode_chksum_verify(struct f2fs_sb_info *sbi, struct page *page)
 158{
 159        struct f2fs_inode *ri;
 160        __u32 provided, calculated;
 161
 162        if (!f2fs_enable_inode_chksum(sbi, page) ||
 163                        PageDirty(page) || PageWriteback(page))
 164                return true;
 165
 166        ri = &F2FS_NODE(page)->i;
 167        provided = le32_to_cpu(ri->i_inode_checksum);
 168        calculated = f2fs_inode_chksum(sbi, page);
 169
 170        if (provided != calculated)
 171                f2fs_msg(sbi->sb, KERN_WARNING,
 172                        "checksum invalid, ino = %x, %x vs. %x",
 173                        ino_of_node(page), provided, calculated);
 174
 175        return provided == calculated;
 176}
 177
 178void f2fs_inode_chksum_set(struct f2fs_sb_info *sbi, struct page *page)
 179{
 180        struct f2fs_inode *ri = &F2FS_NODE(page)->i;
 181
 182        if (!f2fs_enable_inode_chksum(sbi, page))
 183                return;
 184
 185        ri->i_inode_checksum = cpu_to_le32(f2fs_inode_chksum(sbi, page));
 186}
 187
 188static bool sanity_check_inode(struct inode *inode)
 189{
 190        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 191
 192        if (f2fs_sb_has_flexible_inline_xattr(sbi->sb)
 193                        && !f2fs_has_extra_attr(inode)) {
 194                set_sbi_flag(sbi, SBI_NEED_FSCK);
 195                f2fs_msg(sbi->sb, KERN_WARNING,
 196                        "%s: corrupted inode ino=%lx, run fsck to fix.",
 197                        __func__, inode->i_ino);
 198                return false;
 199        }
 200        return true;
 201}
 202
 203static int do_read_inode(struct inode *inode)
 204{
 205        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 206        struct f2fs_inode_info *fi = F2FS_I(inode);
 207        struct page *node_page;
 208        struct f2fs_inode *ri;
 209        projid_t i_projid;
 210
 211        /* Check if ino is within scope */
 212        if (f2fs_check_nid_range(sbi, inode->i_ino))
 213                return -EINVAL;
 214
 215        node_page = f2fs_get_node_page(sbi, inode->i_ino);
 216        if (IS_ERR(node_page))
 217                return PTR_ERR(node_page);
 218
 219        ri = F2FS_INODE(node_page);
 220
 221        inode->i_mode = le16_to_cpu(ri->i_mode);
 222        i_uid_write(inode, le32_to_cpu(ri->i_uid));
 223        i_gid_write(inode, le32_to_cpu(ri->i_gid));
 224        set_nlink(inode, le32_to_cpu(ri->i_links));
 225        inode->i_size = le64_to_cpu(ri->i_size);
 226        inode->i_blocks = SECTOR_FROM_BLOCK(le64_to_cpu(ri->i_blocks) - 1);
 227
 228        inode->i_atime.tv_sec = le64_to_cpu(ri->i_atime);
 229        inode->i_ctime.tv_sec = le64_to_cpu(ri->i_ctime);
 230        inode->i_mtime.tv_sec = le64_to_cpu(ri->i_mtime);
 231        inode->i_atime.tv_nsec = le32_to_cpu(ri->i_atime_nsec);
 232        inode->i_ctime.tv_nsec = le32_to_cpu(ri->i_ctime_nsec);
 233        inode->i_mtime.tv_nsec = le32_to_cpu(ri->i_mtime_nsec);
 234        inode->i_generation = le32_to_cpu(ri->i_generation);
 235        if (S_ISDIR(inode->i_mode))
 236                fi->i_current_depth = le32_to_cpu(ri->i_current_depth);
 237        else if (S_ISREG(inode->i_mode))
 238                fi->i_gc_failures[GC_FAILURE_PIN] =
 239                                        le16_to_cpu(ri->i_gc_failures);
 240        fi->i_xattr_nid = le32_to_cpu(ri->i_xattr_nid);
 241        fi->i_flags = le32_to_cpu(ri->i_flags);
 242        fi->flags = 0;
 243        fi->i_advise = ri->i_advise;
 244        fi->i_pino = le32_to_cpu(ri->i_pino);
 245        fi->i_dir_level = ri->i_dir_level;
 246
 247        if (f2fs_init_extent_tree(inode, &ri->i_ext))
 248                set_page_dirty(node_page);
 249
 250        get_inline_info(inode, ri);
 251
 252        fi->i_extra_isize = f2fs_has_extra_attr(inode) ?
 253                                        le16_to_cpu(ri->i_extra_isize) : 0;
 254
 255        if (f2fs_sb_has_flexible_inline_xattr(sbi->sb)) {
 256                fi->i_inline_xattr_size = le16_to_cpu(ri->i_inline_xattr_size);
 257        } else if (f2fs_has_inline_xattr(inode) ||
 258                                f2fs_has_inline_dentry(inode)) {
 259                fi->i_inline_xattr_size = DEFAULT_INLINE_XATTR_ADDRS;
 260        } else {
 261
 262                /*
 263                 * Previous inline data or directory always reserved 200 bytes
 264                 * in inode layout, even if inline_xattr is disabled. In order
 265                 * to keep inline_dentry's structure for backward compatibility,
 266                 * we get the space back only from inline_data.
 267                 */
 268                fi->i_inline_xattr_size = 0;
 269        }
 270
 271        /* check data exist */
 272        if (f2fs_has_inline_data(inode) && !f2fs_exist_data(inode))
 273                __recover_inline_status(inode, node_page);
 274
 275        /* get rdev by using inline_info */
 276        __get_inode_rdev(inode, ri);
 277
 278        if (__written_first_block(ri))
 279                set_inode_flag(inode, FI_FIRST_BLOCK_WRITTEN);
 280
 281        if (!f2fs_need_inode_block_update(sbi, inode->i_ino))
 282                fi->last_disk_size = inode->i_size;
 283
 284        if (fi->i_flags & F2FS_PROJINHERIT_FL)
 285                set_inode_flag(inode, FI_PROJ_INHERIT);
 286
 287        if (f2fs_has_extra_attr(inode) && f2fs_sb_has_project_quota(sbi->sb) &&
 288                        F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_projid))
 289                i_projid = (projid_t)le32_to_cpu(ri->i_projid);
 290        else
 291                i_projid = F2FS_DEF_PROJID;
 292        fi->i_projid = make_kprojid(&init_user_ns, i_projid);
 293
 294        if (f2fs_has_extra_attr(inode) && f2fs_sb_has_inode_crtime(sbi->sb) &&
 295                        F2FS_FITS_IN_INODE(ri, fi->i_extra_isize, i_crtime)) {
 296                fi->i_crtime.tv_sec = le64_to_cpu(ri->i_crtime);
 297                fi->i_crtime.tv_nsec = le32_to_cpu(ri->i_crtime_nsec);
 298        }
 299
 300        F2FS_I(inode)->i_disk_time[0] = timespec64_to_timespec(inode->i_atime);
 301        F2FS_I(inode)->i_disk_time[1] = timespec64_to_timespec(inode->i_ctime);
 302        F2FS_I(inode)->i_disk_time[2] = timespec64_to_timespec(inode->i_mtime);
 303        F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime;
 304        f2fs_put_page(node_page, 1);
 305
 306        stat_inc_inline_xattr(inode);
 307        stat_inc_inline_inode(inode);
 308        stat_inc_inline_dir(inode);
 309
 310        return 0;
 311}
 312
 313struct inode *f2fs_iget(struct super_block *sb, unsigned long ino)
 314{
 315        struct f2fs_sb_info *sbi = F2FS_SB(sb);
 316        struct inode *inode;
 317        int ret = 0;
 318
 319        inode = iget_locked(sb, ino);
 320        if (!inode)
 321                return ERR_PTR(-ENOMEM);
 322
 323        if (!(inode->i_state & I_NEW)) {
 324                trace_f2fs_iget(inode);
 325                return inode;
 326        }
 327        if (ino == F2FS_NODE_INO(sbi) || ino == F2FS_META_INO(sbi))
 328                goto make_now;
 329
 330        ret = do_read_inode(inode);
 331        if (ret)
 332                goto bad_inode;
 333        if (!sanity_check_inode(inode)) {
 334                ret = -EINVAL;
 335                goto bad_inode;
 336        }
 337make_now:
 338        if (ino == F2FS_NODE_INO(sbi)) {
 339                inode->i_mapping->a_ops = &f2fs_node_aops;
 340                mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
 341        } else if (ino == F2FS_META_INO(sbi)) {
 342                inode->i_mapping->a_ops = &f2fs_meta_aops;
 343                mapping_set_gfp_mask(inode->i_mapping, GFP_NOFS);
 344        } else if (S_ISREG(inode->i_mode)) {
 345                inode->i_op = &f2fs_file_inode_operations;
 346                inode->i_fop = &f2fs_file_operations;
 347                inode->i_mapping->a_ops = &f2fs_dblock_aops;
 348        } else if (S_ISDIR(inode->i_mode)) {
 349                inode->i_op = &f2fs_dir_inode_operations;
 350                inode->i_fop = &f2fs_dir_operations;
 351                inode->i_mapping->a_ops = &f2fs_dblock_aops;
 352                inode_nohighmem(inode);
 353        } else if (S_ISLNK(inode->i_mode)) {
 354                if (f2fs_encrypted_inode(inode))
 355                        inode->i_op = &f2fs_encrypted_symlink_inode_operations;
 356                else
 357                        inode->i_op = &f2fs_symlink_inode_operations;
 358                inode_nohighmem(inode);
 359                inode->i_mapping->a_ops = &f2fs_dblock_aops;
 360        } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
 361                        S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
 362                inode->i_op = &f2fs_special_inode_operations;
 363                init_special_inode(inode, inode->i_mode, inode->i_rdev);
 364        } else {
 365                ret = -EIO;
 366                goto bad_inode;
 367        }
 368        f2fs_set_inode_flags(inode);
 369        unlock_new_inode(inode);
 370        trace_f2fs_iget(inode);
 371        return inode;
 372
 373bad_inode:
 374        iget_failed(inode);
 375        trace_f2fs_iget_exit(inode, ret);
 376        return ERR_PTR(ret);
 377}
 378
 379struct inode *f2fs_iget_retry(struct super_block *sb, unsigned long ino)
 380{
 381        struct inode *inode;
 382retry:
 383        inode = f2fs_iget(sb, ino);
 384        if (IS_ERR(inode)) {
 385                if (PTR_ERR(inode) == -ENOMEM) {
 386                        congestion_wait(BLK_RW_ASYNC, HZ/50);
 387                        goto retry;
 388                }
 389        }
 390        return inode;
 391}
 392
 393void f2fs_update_inode(struct inode *inode, struct page *node_page)
 394{
 395        struct f2fs_inode *ri;
 396        struct extent_tree *et = F2FS_I(inode)->extent_tree;
 397
 398        f2fs_wait_on_page_writeback(node_page, NODE, true);
 399        set_page_dirty(node_page);
 400
 401        f2fs_inode_synced(inode);
 402
 403        ri = F2FS_INODE(node_page);
 404
 405        ri->i_mode = cpu_to_le16(inode->i_mode);
 406        ri->i_advise = F2FS_I(inode)->i_advise;
 407        ri->i_uid = cpu_to_le32(i_uid_read(inode));
 408        ri->i_gid = cpu_to_le32(i_gid_read(inode));
 409        ri->i_links = cpu_to_le32(inode->i_nlink);
 410        ri->i_size = cpu_to_le64(i_size_read(inode));
 411        ri->i_blocks = cpu_to_le64(SECTOR_TO_BLOCK(inode->i_blocks) + 1);
 412
 413        if (et) {
 414                read_lock(&et->lock);
 415                set_raw_extent(&et->largest, &ri->i_ext);
 416                read_unlock(&et->lock);
 417        } else {
 418                memset(&ri->i_ext, 0, sizeof(ri->i_ext));
 419        }
 420        set_raw_inline(inode, ri);
 421
 422        ri->i_atime = cpu_to_le64(inode->i_atime.tv_sec);
 423        ri->i_ctime = cpu_to_le64(inode->i_ctime.tv_sec);
 424        ri->i_mtime = cpu_to_le64(inode->i_mtime.tv_sec);
 425        ri->i_atime_nsec = cpu_to_le32(inode->i_atime.tv_nsec);
 426        ri->i_ctime_nsec = cpu_to_le32(inode->i_ctime.tv_nsec);
 427        ri->i_mtime_nsec = cpu_to_le32(inode->i_mtime.tv_nsec);
 428        if (S_ISDIR(inode->i_mode))
 429                ri->i_current_depth =
 430                        cpu_to_le32(F2FS_I(inode)->i_current_depth);
 431        else if (S_ISREG(inode->i_mode))
 432                ri->i_gc_failures =
 433                        cpu_to_le16(F2FS_I(inode)->i_gc_failures[GC_FAILURE_PIN]);
 434        ri->i_xattr_nid = cpu_to_le32(F2FS_I(inode)->i_xattr_nid);
 435        ri->i_flags = cpu_to_le32(F2FS_I(inode)->i_flags);
 436        ri->i_pino = cpu_to_le32(F2FS_I(inode)->i_pino);
 437        ri->i_generation = cpu_to_le32(inode->i_generation);
 438        ri->i_dir_level = F2FS_I(inode)->i_dir_level;
 439
 440        if (f2fs_has_extra_attr(inode)) {
 441                ri->i_extra_isize = cpu_to_le16(F2FS_I(inode)->i_extra_isize);
 442
 443                if (f2fs_sb_has_flexible_inline_xattr(F2FS_I_SB(inode)->sb))
 444                        ri->i_inline_xattr_size =
 445                                cpu_to_le16(F2FS_I(inode)->i_inline_xattr_size);
 446
 447                if (f2fs_sb_has_project_quota(F2FS_I_SB(inode)->sb) &&
 448                        F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
 449                                                                i_projid)) {
 450                        projid_t i_projid;
 451
 452                        i_projid = from_kprojid(&init_user_ns,
 453                                                F2FS_I(inode)->i_projid);
 454                        ri->i_projid = cpu_to_le32(i_projid);
 455                }
 456
 457                if (f2fs_sb_has_inode_crtime(F2FS_I_SB(inode)->sb) &&
 458                        F2FS_FITS_IN_INODE(ri, F2FS_I(inode)->i_extra_isize,
 459                                                                i_crtime)) {
 460                        ri->i_crtime =
 461                                cpu_to_le64(F2FS_I(inode)->i_crtime.tv_sec);
 462                        ri->i_crtime_nsec =
 463                                cpu_to_le32(F2FS_I(inode)->i_crtime.tv_nsec);
 464                }
 465        }
 466
 467        __set_inode_rdev(inode, ri);
 468
 469        /* deleted inode */
 470        if (inode->i_nlink == 0)
 471                clear_inline_node(node_page);
 472
 473        F2FS_I(inode)->i_disk_time[0] = timespec64_to_timespec(inode->i_atime);
 474        F2FS_I(inode)->i_disk_time[1] = timespec64_to_timespec(inode->i_ctime);
 475        F2FS_I(inode)->i_disk_time[2] = timespec64_to_timespec(inode->i_mtime);
 476        F2FS_I(inode)->i_disk_time[3] = F2FS_I(inode)->i_crtime;
 477}
 478
 479void f2fs_update_inode_page(struct inode *inode)
 480{
 481        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 482        struct page *node_page;
 483retry:
 484        node_page = f2fs_get_node_page(sbi, inode->i_ino);
 485        if (IS_ERR(node_page)) {
 486                int err = PTR_ERR(node_page);
 487                if (err == -ENOMEM) {
 488                        cond_resched();
 489                        goto retry;
 490                } else if (err != -ENOENT) {
 491                        f2fs_stop_checkpoint(sbi, false);
 492                }
 493                return;
 494        }
 495        f2fs_update_inode(inode, node_page);
 496        f2fs_put_page(node_page, 1);
 497}
 498
 499int f2fs_write_inode(struct inode *inode, struct writeback_control *wbc)
 500{
 501        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 502
 503        if (inode->i_ino == F2FS_NODE_INO(sbi) ||
 504                        inode->i_ino == F2FS_META_INO(sbi))
 505                return 0;
 506
 507        if (!is_inode_flag_set(inode, FI_DIRTY_INODE))
 508                return 0;
 509
 510        /*
 511         * We need to balance fs here to prevent from producing dirty node pages
 512         * during the urgent cleaning time when runing out of free sections.
 513         */
 514        f2fs_update_inode_page(inode);
 515        if (wbc && wbc->nr_to_write)
 516                f2fs_balance_fs(sbi, true);
 517        return 0;
 518}
 519
 520/*
 521 * Called at the last iput() if i_nlink is zero
 522 */
 523void f2fs_evict_inode(struct inode *inode)
 524{
 525        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 526        nid_t xnid = F2FS_I(inode)->i_xattr_nid;
 527        int err = 0;
 528
 529        /* some remained atomic pages should discarded */
 530        if (f2fs_is_atomic_file(inode))
 531                f2fs_drop_inmem_pages(inode);
 532
 533        trace_f2fs_evict_inode(inode);
 534        truncate_inode_pages_final(&inode->i_data);
 535
 536        if (inode->i_ino == F2FS_NODE_INO(sbi) ||
 537                        inode->i_ino == F2FS_META_INO(sbi))
 538                goto out_clear;
 539
 540        f2fs_bug_on(sbi, get_dirty_pages(inode));
 541        f2fs_remove_dirty_inode(inode);
 542
 543        f2fs_destroy_extent_tree(inode);
 544
 545        if (inode->i_nlink || is_bad_inode(inode))
 546                goto no_delete;
 547
 548        dquot_initialize(inode);
 549
 550        f2fs_remove_ino_entry(sbi, inode->i_ino, APPEND_INO);
 551        f2fs_remove_ino_entry(sbi, inode->i_ino, UPDATE_INO);
 552        f2fs_remove_ino_entry(sbi, inode->i_ino, FLUSH_INO);
 553
 554        sb_start_intwrite(inode->i_sb);
 555        set_inode_flag(inode, FI_NO_ALLOC);
 556        i_size_write(inode, 0);
 557retry:
 558        if (F2FS_HAS_BLOCKS(inode))
 559                err = f2fs_truncate(inode);
 560
 561#ifdef CONFIG_F2FS_FAULT_INJECTION
 562        if (time_to_inject(sbi, FAULT_EVICT_INODE)) {
 563                f2fs_show_injection_info(FAULT_EVICT_INODE);
 564                err = -EIO;
 565        }
 566#endif
 567        if (!err) {
 568                f2fs_lock_op(sbi);
 569                err = f2fs_remove_inode_page(inode);
 570                f2fs_unlock_op(sbi);
 571                if (err == -ENOENT)
 572                        err = 0;
 573        }
 574
 575        /* give more chances, if ENOMEM case */
 576        if (err == -ENOMEM) {
 577                err = 0;
 578                goto retry;
 579        }
 580
 581        if (err)
 582                f2fs_update_inode_page(inode);
 583        dquot_free_inode(inode);
 584        sb_end_intwrite(inode->i_sb);
 585no_delete:
 586        dquot_drop(inode);
 587
 588        stat_dec_inline_xattr(inode);
 589        stat_dec_inline_dir(inode);
 590        stat_dec_inline_inode(inode);
 591
 592        if (likely(!is_set_ckpt_flags(sbi, CP_ERROR_FLAG)))
 593                f2fs_bug_on(sbi, is_inode_flag_set(inode, FI_DIRTY_INODE));
 594        else
 595                f2fs_inode_synced(inode);
 596
 597        /* ino == 0, if f2fs_new_inode() was failed t*/
 598        if (inode->i_ino)
 599                invalidate_mapping_pages(NODE_MAPPING(sbi), inode->i_ino,
 600                                                        inode->i_ino);
 601        if (xnid)
 602                invalidate_mapping_pages(NODE_MAPPING(sbi), xnid, xnid);
 603        if (inode->i_nlink) {
 604                if (is_inode_flag_set(inode, FI_APPEND_WRITE))
 605                        f2fs_add_ino_entry(sbi, inode->i_ino, APPEND_INO);
 606                if (is_inode_flag_set(inode, FI_UPDATE_WRITE))
 607                        f2fs_add_ino_entry(sbi, inode->i_ino, UPDATE_INO);
 608        }
 609        if (is_inode_flag_set(inode, FI_FREE_NID)) {
 610                f2fs_alloc_nid_failed(sbi, inode->i_ino);
 611                clear_inode_flag(inode, FI_FREE_NID);
 612        } else {
 613                /*
 614                 * If xattr nid is corrupted, we can reach out error condition,
 615                 * err & !f2fs_exist_written_data(sbi, inode->i_ino, ORPHAN_INO)).
 616                 * In that case, f2fs_check_nid_range() is enough to give a clue.
 617                 */
 618        }
 619out_clear:
 620        fscrypt_put_encryption_info(inode);
 621        clear_inode(inode);
 622}
 623
 624/* caller should call f2fs_lock_op() */
 625void f2fs_handle_failed_inode(struct inode *inode)
 626{
 627        struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 628        struct node_info ni;
 629
 630        /*
 631         * clear nlink of inode in order to release resource of inode
 632         * immediately.
 633         */
 634        clear_nlink(inode);
 635
 636        /*
 637         * we must call this to avoid inode being remained as dirty, resulting
 638         * in a panic when flushing dirty inodes in gdirty_list.
 639         */
 640        f2fs_update_inode_page(inode);
 641        f2fs_inode_synced(inode);
 642
 643        /* don't make bad inode, since it becomes a regular file. */
 644        unlock_new_inode(inode);
 645
 646        /*
 647         * Note: we should add inode to orphan list before f2fs_unlock_op()
 648         * so we can prevent losing this orphan when encoutering checkpoint
 649         * and following suddenly power-off.
 650         */
 651        f2fs_get_node_info(sbi, inode->i_ino, &ni);
 652
 653        if (ni.blk_addr != NULL_ADDR) {
 654                int err = f2fs_acquire_orphan_inode(sbi);
 655                if (err) {
 656                        set_sbi_flag(sbi, SBI_NEED_FSCK);
 657                        f2fs_msg(sbi->sb, KERN_WARNING,
 658                                "Too many orphan inodes, run fsck to fix.");
 659                } else {
 660                        f2fs_add_orphan_inode(inode);
 661                }
 662                f2fs_alloc_nid_done(sbi, inode->i_ino);
 663        } else {
 664                set_inode_flag(inode, FI_FREE_NID);
 665        }
 666
 667        f2fs_unlock_op(sbi);
 668
 669        /* iput will drop the inode object */
 670        iput(inode);
 671}
 672