Skip to content

Commit 431339b

Browse files
Gao Xianggregkh
authored andcommitted
staging: erofs: add inode operations
This adds core functions to get, read an inode. Signed-off-by: Miao Xie <miaoxie@huawei.com> Signed-off-by: Chao Yu <yuchao0@huawei.com> Signed-off-by: Gao Xiang <gaoxiang25@huawei.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 81781b0 commit 431339b

File tree

1 file changed

+210
-0
lines changed

1 file changed

+210
-0
lines changed

drivers/staging/erofs/inode.c

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
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 "internal.h"
14+
15+
/* no locking */
16+
static int read_inode(struct inode *inode, void *data)
17+
{
18+
struct erofs_vnode *vi = EROFS_V(inode);
19+
struct erofs_inode_v1 *v1 = data;
20+
const unsigned advise = le16_to_cpu(v1->i_advise);
21+
22+
vi->data_mapping_mode = __inode_data_mapping(advise);
23+
24+
if (unlikely(vi->data_mapping_mode >= EROFS_INODE_LAYOUT_MAX)) {
25+
errln("unknown data mapping mode %u of nid %llu",
26+
vi->data_mapping_mode, vi->nid);
27+
DBG_BUGON(1);
28+
return -EIO;
29+
}
30+
31+
if (__inode_version(advise) == EROFS_INODE_LAYOUT_V2) {
32+
struct erofs_inode_v2 *v2 = data;
33+
34+
vi->inode_isize = sizeof(struct erofs_inode_v2);
35+
vi->xattr_isize = ondisk_xattr_ibody_size(v2->i_xattr_icount);
36+
37+
vi->raw_blkaddr = le32_to_cpu(v2->i_u.raw_blkaddr);
38+
inode->i_mode = le16_to_cpu(v2->i_mode);
39+
40+
i_uid_write(inode, le32_to_cpu(v2->i_uid));
41+
i_gid_write(inode, le32_to_cpu(v2->i_gid));
42+
set_nlink(inode, le32_to_cpu(v2->i_nlink));
43+
44+
/* ns timestamp */
45+
inode->i_mtime.tv_sec = inode->i_ctime.tv_sec =
46+
le64_to_cpu(v2->i_ctime);
47+
inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec =
48+
le32_to_cpu(v2->i_ctime_nsec);
49+
50+
inode->i_size = le64_to_cpu(v2->i_size);
51+
} else if (__inode_version(advise) == EROFS_INODE_LAYOUT_V1) {
52+
struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
53+
54+
vi->inode_isize = sizeof(struct erofs_inode_v1);
55+
vi->xattr_isize = ondisk_xattr_ibody_size(v1->i_xattr_icount);
56+
57+
vi->raw_blkaddr = le32_to_cpu(v1->i_u.raw_blkaddr);
58+
inode->i_mode = le16_to_cpu(v1->i_mode);
59+
60+
i_uid_write(inode, le16_to_cpu(v1->i_uid));
61+
i_gid_write(inode, le16_to_cpu(v1->i_gid));
62+
set_nlink(inode, le16_to_cpu(v1->i_nlink));
63+
64+
/* use build time to derive all file time */
65+
inode->i_mtime.tv_sec = inode->i_ctime.tv_sec =
66+
sbi->build_time;
67+
inode->i_mtime.tv_nsec = inode->i_ctime.tv_nsec =
68+
sbi->build_time_nsec;
69+
70+
inode->i_size = le32_to_cpu(v1->i_size);
71+
} else {
72+
errln("unsupported on-disk inode version %u of nid %llu",
73+
__inode_version(advise), vi->nid);
74+
DBG_BUGON(1);
75+
return -EIO;
76+
}
77+
78+
/* measure inode.i_blocks as the generic filesystem */
79+
inode->i_blocks = ((inode->i_size - 1) >> 9) + 1;
80+
return 0;
81+
}
82+
83+
/*
84+
* try_lock can be required since locking order is:
85+
* file data(fs_inode)
86+
* meta(bd_inode)
87+
* but the majority of the callers is "iget",
88+
* in that case we are pretty sure no deadlock since
89+
* no data operations exist. However I tend to
90+
* try_lock since it takes no much overhead and
91+
* will success immediately.
92+
*/
93+
static int fill_inline_data(struct inode *inode, void *data, unsigned m_pofs)
94+
{
95+
struct erofs_vnode *vi = EROFS_V(inode);
96+
int mode = vi->data_mapping_mode;
97+
98+
DBG_BUGON(mode >= EROFS_INODE_LAYOUT_MAX);
99+
100+
/* should be inode inline C */
101+
if (mode != EROFS_INODE_LAYOUT_INLINE)
102+
return 0;
103+
104+
/* fast symlink (following ext4) */
105+
if (S_ISLNK(inode->i_mode) && inode->i_size < PAGE_SIZE) {
106+
char *lnk = kmalloc(inode->i_size + 1, GFP_KERNEL);
107+
108+
if (unlikely(lnk == NULL))
109+
return -ENOMEM;
110+
111+
m_pofs += vi->inode_isize + vi->xattr_isize;
112+
BUG_ON(m_pofs + inode->i_size > PAGE_SIZE);
113+
114+
/* get in-page inline data */
115+
memcpy(lnk, data + m_pofs, inode->i_size);
116+
lnk[inode->i_size] = '\0';
117+
118+
inode->i_link = lnk;
119+
set_inode_fast_symlink(inode);
120+
}
121+
return -EAGAIN;
122+
}
123+
124+
static int fill_inode(struct inode *inode, int isdir)
125+
{
126+
struct erofs_sb_info *sbi = EROFS_SB(inode->i_sb);
127+
struct erofs_vnode *vi = EROFS_V(inode);
128+
struct page *page;
129+
void *data;
130+
int err;
131+
erofs_blk_t blkaddr;
132+
unsigned ofs;
133+
134+
blkaddr = erofs_blknr(iloc(sbi, vi->nid));
135+
ofs = erofs_blkoff(iloc(sbi, vi->nid));
136+
137+
debugln("%s, reading inode nid %llu at %u of blkaddr %u",
138+
__func__, vi->nid, ofs, blkaddr);
139+
140+
page = erofs_get_meta_page(inode->i_sb, blkaddr, isdir);
141+
142+
if (IS_ERR(page)) {
143+
errln("failed to get inode (nid: %llu) page, err %ld",
144+
vi->nid, PTR_ERR(page));
145+
return PTR_ERR(page);
146+
}
147+
148+
BUG_ON(!PageUptodate(page));
149+
data = page_address(page);
150+
151+
err = read_inode(inode, data + ofs);
152+
if (!err) {
153+
/* setup the new inode */
154+
if (S_ISREG(inode->i_mode)) {
155+
inode->i_fop = &generic_ro_fops;
156+
} else if (S_ISDIR(inode->i_mode)) {
157+
inode->i_op =
158+
&erofs_dir_iops;
159+
inode->i_fop = &erofs_dir_fops;
160+
} else if (S_ISLNK(inode->i_mode)) {
161+
/* by default, page_get_link is used for symlink */
162+
inode->i_op =
163+
&page_symlink_inode_operations;
164+
inode_nohighmem(inode);
165+
} else {
166+
err = -EIO;
167+
goto out_unlock;
168+
}
169+
170+
if (is_inode_layout_compression(inode)) {
171+
err = -ENOTSUPP;
172+
goto out_unlock;
173+
}
174+
175+
inode->i_mapping->a_ops = &erofs_raw_access_aops;
176+
177+
/* fill last page if inline data is available */
178+
fill_inline_data(inode, data, ofs);
179+
}
180+
181+
out_unlock:
182+
unlock_page(page);
183+
put_page(page);
184+
return err;
185+
}
186+
187+
struct inode *erofs_iget(struct super_block *sb,
188+
erofs_nid_t nid, bool isdir)
189+
{
190+
struct inode *inode = iget_locked(sb, nid);
191+
192+
if (unlikely(inode == NULL))
193+
return ERR_PTR(-ENOMEM);
194+
195+
if (inode->i_state & I_NEW) {
196+
int err;
197+
struct erofs_vnode *vi = EROFS_V(inode);
198+
vi->nid = nid;
199+
200+
err = fill_inode(inode, isdir);
201+
if (likely(!err))
202+
unlock_new_inode(inode);
203+
else {
204+
iget_failed(inode);
205+
inode = ERR_PTR(err);
206+
}
207+
}
208+
return inode;
209+
}
210+

0 commit comments

Comments
 (0)