linux/drivers/staging/erofs/inode.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * linux/drivers/staging/erofs/inode.c
   4 *
   5 * Copyright (C) 2017-2018 HUAWEI, Inc.
   6 *             http://www.huawei.com/
   7 * Created by Gao Xiang <gaoxiang25@huawei.com>
   8 *
   9 * This file is subject to the terms and conditions of the GNU General Public
  10 * License.  See the file COPYING in the main directory of the Linux
  11 * distribution for more details.
  12 */
  13#include "xattr.h"
  14
  15#include <trace/events/erofs.h>
  16
  17/* no locking */
  18static int read_inode(struct inode *inode, void *data)
  19{
  20        struct erofs_vnode *vi = EROFS_V(inode);
  21        struct erofs_inode_v1 *v1 = data;
  22        const unsigned int advise = le16_to_cpu(v1->i_advise);
  23        erofs_blk_t nblks = 0;
  24
  25        vi->datamode = __inode_data_mapping(advise);
  26
  27        if (unlikely(vi->datamode >= EROFS_INODE_LAYOUT_MAX)) {
  28                errln("unsupported data mapping %u of nid %llu",
  29                      vi->datamode, vi->nid);
  30                DBG_BUGON(1);
  31                return -EIO;
  32        }
  33
  34        if (__inode_version(advise) == EROFS_INODE_LAYOUT_V2) {
  35                struct erofs_inode_v2 *v2 = data;
  36
  37                vi->inode_isize = sizeof(struct erofs_inode_v2);
  38                vi->xattr_isize = ondisk_xattr_ibody_size(v2->i_xattr_icount);
  39
  40                inode->i_mode = le16_to_cpu(v2->i_mode);
  41                if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  42                    S_ISLNK(inode->i_mode)) {
  43                        vi->raw_blkaddr = le32_to_cpu(v2->i_u.raw_blkaddr);
  44                } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  45                        inode->i_rdev =
  46                                new_decode_dev(le32_to_cpu(v2->i_u.rdev));
  47                } else if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  48                        inode->i_rdev = 0;
  49                } else {
  50                        return -EIO;
  51                }
  52
  53                i_uid_write(inode, le32_to_cpu(v2->i_uid));
  54                i_gid_write(inode, le32_to_cpu(v2->i_gid));
  55                set_nlink(inode, le32_to_cpu(v2->i_nlink));
  56
  57                /* ns timestamp */
  58                inode->i_mtime.tv_sec = inode->i_ctime.tv_sec =
  59                        le64_to_cpu(v2->i_ctime);
  60                inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec =
  61                        le32_to_cpu(v2->i_ctime_nsec);
  62
  63                inode->i_size = le64_to_cpu(v2->i_size);
  64
  65                /* total blocks for compressed files */
  66                if (is_inode_layout_compression(inode))
  67                        nblks = le32_to_cpu(v2->i_u.compressed_blocks);
  68        } else if (__inode_version(advise) == EROFS_INODE_LAYOUT_V1) {
  69                struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
  70
  71                vi->inode_isize = sizeof(struct erofs_inode_v1);
  72                vi->xattr_isize = ondisk_xattr_ibody_size(v1->i_xattr_icount);
  73
  74                inode->i_mode = le16_to_cpu(v1->i_mode);
  75                if (S_ISREG(inode->i_mode) || S_ISDIR(inode->i_mode) ||
  76                    S_ISLNK(inode->i_mode)) {
  77                        vi->raw_blkaddr = le32_to_cpu(v1->i_u.raw_blkaddr);
  78                } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode)) {
  79                        inode->i_rdev =
  80                                new_decode_dev(le32_to_cpu(v1->i_u.rdev));
  81                } else if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
  82                        inode->i_rdev = 0;
  83                } else {
  84                        return -EIO;
  85                }
  86
  87                i_uid_write(inode, le16_to_cpu(v1->i_uid));
  88                i_gid_write(inode, le16_to_cpu(v1->i_gid));
  89                set_nlink(inode, le16_to_cpu(v1->i_nlink));
  90
  91                /* use build time to derive all file time */
  92                inode->i_mtime.tv_sec = inode->i_ctime.tv_sec =
  93                        sbi->build_time;
  94                inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec =
  95                        sbi->build_time_nsec;
  96
  97                inode->i_size = le32_to_cpu(v1->i_size);
  98                if (is_inode_layout_compression(inode))
  99                        nblks = le32_to_cpu(v1->i_u.compressed_blocks);
 100        } else {
 101                errln("unsupported on-disk inode version %u of nid %llu",
 102                      __inode_version(advise), vi->nid);
 103                DBG_BUGON(1);
 104                return -EIO;
 105        }
 106
 107        if (!nblks)
 108                /* measure inode.i_blocks as generic filesystems */
 109                inode->i_blocks = roundup(inode->i_size, EROFS_BLKSIZ) >> 9;
 110        else
 111                inode->i_blocks = nblks << LOG_SECTORS_PER_BLOCK;
 112        return 0;
 113}
 114
 115/*
 116 * try_lock can be required since locking order is:
 117 *   file data(fs_inode)
 118 *        meta(bd_inode)
 119 * but the majority of the callers is "iget",
 120 * in that case we are pretty sure no deadlock since
 121 * no data operations exist. However I tend to
 122 * try_lock since it takes no much overhead and
 123 * will success immediately.
 124 */
 125static int fill_inline_data(struct inode *inode, void *data,
 126                            unsigned int m_pofs)
 127{
 128        struct erofs_vnode *vi = EROFS_V(inode);
 129        struct erofs_sb_info *sbi = EROFS_I_SB(inode);
 130
 131        /* should be inode inline C */
 132        if (!is_inode_flat_inline(inode))
 133                return 0;
 134
 135        /* fast symlink (following ext4) */
 136        if (S_ISLNK(inode->i_mode) && inode->i_size < PAGE_SIZE) {
 137                char *lnk = erofs_kmalloc(sbi, inode->i_size + 1, GFP_KERNEL);
 138
 139                if (unlikely(!lnk))
 140                        return -ENOMEM;
 141
 142                m_pofs += vi->inode_isize + vi->xattr_isize;
 143
 144                /* inline symlink data shouldn't across page boundary as well */
 145                if (unlikely(m_pofs + inode->i_size > PAGE_SIZE)) {
 146                        DBG_BUGON(1);
 147                        kfree(lnk);
 148                        return -EIO;
 149                }
 150
 151                /* get in-page inline data */
 152                memcpy(lnk, data + m_pofs, inode->i_size);
 153                lnk[inode->i_size] = '\0';
 154
 155                inode->i_link = lnk;
 156                set_inode_fast_symlink(inode);
 157        }
 158        return 0;
 159}
 160
 161static int fill_inode(struct inode *inode, int isdir)
 162{
 163        struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
 164        struct erofs_vnode *vi = EROFS_V(inode);
 165        struct page *page;
 166        void *data;
 167        int err;
 168        erofs_blk_t blkaddr;
 169        unsigned int ofs;
 170
 171        trace_erofs_fill_inode(inode, isdir);
 172
 173        blkaddr = erofs_blknr(iloc(sbi, vi->nid));
 174        ofs = erofs_blkoff(iloc(sbi, vi->nid));
 175
 176        debugln("%s, reading inode nid %llu at %u of blkaddr %u",
 177                __func__, vi->nid, ofs, blkaddr);
 178
 179        page = erofs_get_meta_page(inode->i_sb, blkaddr, isdir);
 180
 181        if (IS_ERR(page)) {
 182                errln("failed to get inode (nid: %llu) page, err %ld",
 183                      vi->nid, PTR_ERR(page));
 184                return PTR_ERR(page);
 185        }
 186
 187        DBG_BUGON(!PageUptodate(page));
 188        data = page_address(page);
 189
 190        err = read_inode(inode, data + ofs);
 191        if (!err) {
 192                /* setup the new inode */
 193                if (S_ISREG(inode->i_mode)) {
 194                        inode->i_op = &erofs_generic_iops;
 195                        inode->i_fop = &generic_ro_fops;
 196                } else if (S_ISDIR(inode->i_mode)) {
 197                        inode->i_op = &erofs_dir_iops;
 198                        inode->i_fop = &erofs_dir_fops;
 199                } else if (S_ISLNK(inode->i_mode)) {
 200                        /* by default, page_get_link is used for symlink */
 201                        inode->i_op = &erofs_symlink_iops;
 202                        inode_nohighmem(inode);
 203                } else if (S_ISCHR(inode->i_mode) || S_ISBLK(inode->i_mode) ||
 204                        S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {
 205                        inode->i_op = &erofs_generic_iops;
 206                        init_special_inode(inode, inode->i_mode, inode->i_rdev);
 207                        goto out_unlock;
 208                } else {
 209                        err = -EIO;
 210                        goto out_unlock;
 211                }
 212
 213                if (is_inode_layout_compression(inode)) {
 214                        err = z_erofs_fill_inode(inode);
 215                        goto out_unlock;
 216                }
 217
 218                inode->i_mapping->a_ops = &erofs_raw_access_aops;
 219
 220                /* fill last page if inline data is available */
 221                err = fill_inline_data(inode, data, ofs);
 222        }
 223
 224out_unlock:
 225        unlock_page(page);
 226        put_page(page);
 227        return err;
 228}
 229
 230/*
 231 * erofs nid is 64bits, but i_ino is 'unsigned long', therefore
 232 * we should do more for 32-bit platform to find the right inode.
 233 */
 234#if BITS_PER_LONG == 32
 235static int erofs_ilookup_test_actor(struct inode *inode, void *opaque)
 236{
 237        const erofs_nid_t nid = *(erofs_nid_t *)opaque;
 238
 239        return EROFS_V(inode)->nid == nid;
 240}
 241
 242static int erofs_iget_set_actor(struct inode *inode, void *opaque)
 243{
 244        const erofs_nid_t nid = *(erofs_nid_t *)opaque;
 245
 246        inode->i_ino = erofs_inode_hash(nid);
 247        return 0;
 248}
 249#endif
 250
 251static inline struct inode *erofs_iget_locked(struct super_block *sb,
 252                                              erofs_nid_t nid)
 253{
 254        const unsigned long hashval = erofs_inode_hash(nid);
 255
 256#if BITS_PER_LONG >= 64
 257        /* it is safe to use iget_locked for >= 64-bit platform */
 258        return iget_locked(sb, hashval);
 259#else
 260        return iget5_locked(sb, hashval, erofs_ilookup_test_actor,
 261                erofs_iget_set_actor, &nid);
 262#endif
 263}
 264
 265struct inode *erofs_iget(struct super_block *sb,
 266                         erofs_nid_t nid,
 267                         bool isdir)
 268{
 269        struct inode *inode = erofs_iget_locked(sb, nid);
 270
 271        if (unlikely(!inode))
 272                return ERR_PTR(-ENOMEM);
 273
 274        if (inode->i_state & I_NEW) {
 275                int err;
 276                struct erofs_vnode *vi = EROFS_V(inode);
 277
 278                vi->nid = nid;
 279
 280                err = fill_inode(inode, isdir);
 281                if (likely(!err))
 282                        unlock_new_inode(inode);
 283                else {
 284                        iget_failed(inode);
 285                        inode = ERR_PTR(err);
 286                }
 287        }
 288        return inode;
 289}
 290
 291int erofs_getattr(const struct path *path, struct kstat *stat,
 292                  u32 request_mask, unsigned int query_flags)
 293{
 294        struct inode *const inode = d_inode(path->dentry);
 295
 296        if (is_inode_layout_compression(inode))
 297                stat->attributes |= STATX_ATTR_COMPRESSED;
 298
 299        stat->attributes |= STATX_ATTR_IMMUTABLE;
 300        stat->attributes_mask |= (STATX_ATTR_COMPRESSED |
 301                                  STATX_ATTR_IMMUTABLE);
 302
 303        generic_fillattr(inode, stat);
 304        return 0;
 305}
 306
 307const struct inode_operations erofs_generic_iops = {
 308        .getattr = erofs_getattr,
 309#ifdef CONFIG_EROFS_FS_XATTR
 310        .listxattr = erofs_listxattr,
 311#endif
 312        .get_acl = erofs_get_acl,
 313};
 314
 315const struct inode_operations erofs_symlink_iops = {
 316        .get_link = page_get_link,
 317        .getattr = erofs_getattr,
 318#ifdef CONFIG_EROFS_FS_XATTR
 319        .listxattr = erofs_listxattr,
 320#endif
 321        .get_acl = erofs_get_acl,
 322};
 323
 324const struct inode_operations erofs_fast_symlink_iops = {
 325        .get_link = simple_get_link,
 326        .getattr = erofs_getattr,
 327#ifdef CONFIG_EROFS_FS_XATTR
 328        .listxattr = erofs_listxattr,
 329#endif
 330        .get_acl = erofs_get_acl,
 331};
 332
 333