1 // SPDX-License-Identifier: GPL-2.0+
4 int erofs_read_inode_from_disk(struct erofs_inode *vi)
7 char buf[sizeof(struct erofs_inode_extended)];
8 struct erofs_inode_compact *dic;
9 struct erofs_inode_extended *die;
10 const erofs_off_t inode_loc = iloc(vi->nid);
12 ret = erofs_dev_read(0, buf, inode_loc, sizeof(*dic));
16 dic = (struct erofs_inode_compact *)buf;
17 ifmt = le16_to_cpu(dic->i_format);
19 vi->datalayout = erofs_inode_datalayout(ifmt);
20 if (vi->datalayout >= EROFS_INODE_DATALAYOUT_MAX) {
21 erofs_err("unsupported datalayout %u of nid %llu",
22 vi->datalayout, vi->nid | 0ULL);
25 switch (erofs_inode_version(ifmt)) {
26 case EROFS_INODE_LAYOUT_EXTENDED:
27 vi->inode_isize = sizeof(struct erofs_inode_extended);
29 ret = erofs_dev_read(0, buf + sizeof(*dic), inode_loc + sizeof(*dic),
30 sizeof(*die) - sizeof(*dic));
34 die = (struct erofs_inode_extended *)buf;
35 vi->xattr_isize = erofs_xattr_ibody_size(die->i_xattr_icount);
36 vi->i_mode = le16_to_cpu(die->i_mode);
38 switch (vi->i_mode & S_IFMT) {
42 vi->u.i_blkaddr = le32_to_cpu(die->i_u.raw_blkaddr);
56 vi->i_uid = le32_to_cpu(die->i_uid);
57 vi->i_gid = le32_to_cpu(die->i_gid);
58 vi->i_nlink = le32_to_cpu(die->i_nlink);
60 vi->i_ctime = le64_to_cpu(die->i_ctime);
61 vi->i_ctime_nsec = le64_to_cpu(die->i_ctime_nsec);
62 vi->i_size = le64_to_cpu(die->i_size);
63 if (vi->datalayout == EROFS_INODE_CHUNK_BASED)
64 /* fill chunked inode summary info */
65 vi->u.chunkformat = le16_to_cpu(die->i_u.c.format);
67 case EROFS_INODE_LAYOUT_COMPACT:
68 vi->inode_isize = sizeof(struct erofs_inode_compact);
69 vi->xattr_isize = erofs_xattr_ibody_size(dic->i_xattr_icount);
70 vi->i_mode = le16_to_cpu(dic->i_mode);
72 switch (vi->i_mode & S_IFMT) {
76 vi->u.i_blkaddr = le32_to_cpu(dic->i_u.raw_blkaddr);
90 vi->i_uid = le16_to_cpu(dic->i_uid);
91 vi->i_gid = le16_to_cpu(dic->i_gid);
92 vi->i_nlink = le16_to_cpu(dic->i_nlink);
94 vi->i_ctime = sbi.build_time;
95 vi->i_ctime_nsec = sbi.build_time_nsec;
97 vi->i_size = le32_to_cpu(dic->i_size);
98 if (vi->datalayout == EROFS_INODE_CHUNK_BASED)
99 vi->u.chunkformat = le16_to_cpu(dic->i_u.c.format);
102 erofs_err("unsupported on-disk inode version %u of nid %llu",
103 erofs_inode_version(ifmt), vi->nid | 0ULL);
108 if (vi->datalayout == EROFS_INODE_CHUNK_BASED) {
109 if (vi->u.chunkformat & ~EROFS_CHUNK_FORMAT_ALL) {
110 erofs_err("unsupported chunk format %x of nid %llu",
111 vi->u.chunkformat, vi->nid | 0ULL);
114 vi->u.chunkbits = LOG_BLOCK_SIZE +
115 (vi->u.chunkformat & EROFS_CHUNK_FORMAT_BLKBITS_MASK);
116 } else if (erofs_inode_is_data_compressed(vi->datalayout))
117 z_erofs_fill_inode(vi);
120 erofs_err("bogus i_mode (%o) @ nid %llu", vi->i_mode, vi->nid | 0ULL);
121 return -EFSCORRUPTED;
124 struct erofs_dirent *find_target_dirent(erofs_nid_t pnid,
126 const char *name, unsigned int len,
127 unsigned int nameoff,
128 unsigned int maxsize)
130 struct erofs_dirent *de = dentry_blk;
131 const struct erofs_dirent *end = dentry_blk + nameoff;
135 unsigned int de_namelen;
137 nameoff = le16_to_cpu(de->nameoff);
138 de_name = (char *)dentry_blk + nameoff;
140 /* the last dirent in the block? */
142 de_namelen = strnlen(de_name, maxsize - nameoff);
144 de_namelen = le16_to_cpu(de[1].nameoff) - nameoff;
146 /* a corrupted entry is found */
147 if (nameoff + de_namelen > maxsize ||
148 de_namelen > EROFS_NAME_LEN) {
149 erofs_err("bogus dirent @ nid %llu", pnid | 0ULL);
151 return ERR_PTR(-EFSCORRUPTED);
154 if (len == de_namelen && !memcmp(de_name, name, de_namelen))
166 int erofs_namei(struct nameidata *nd,
167 const char *name, unsigned int len)
169 erofs_nid_t nid = nd->nid;
171 char buf[EROFS_BLKSIZ];
172 struct erofs_inode vi = { .nid = nid };
175 ret = erofs_read_inode_from_disk(&vi);
180 while (offset < vi.i_size) {
181 erofs_off_t maxsize = min_t(erofs_off_t,
182 vi.i_size - offset, EROFS_BLKSIZ);
183 struct erofs_dirent *de = (void *)buf;
184 unsigned int nameoff;
186 ret = erofs_pread(&vi, buf, maxsize, offset);
190 nameoff = le16_to_cpu(de->nameoff);
191 if (nameoff < sizeof(struct erofs_dirent) ||
192 nameoff >= PAGE_SIZE) {
193 erofs_err("invalid de[0].nameoff %u @ nid %llu",
194 nameoff, nid | 0ULL);
195 return -EFSCORRUPTED;
198 de = find_target_dirent(nid, buf, name, len,
204 nd->nid = le64_to_cpu(de->nid);
212 static int link_path_walk(const char *name, struct nameidata *nd)
214 nd->nid = sbi.root_nid;
219 /* At this point we know we have a real path component. */
220 while (*name != '\0') {
221 const char *p = name;
226 } while (*p != '\0' && *p != '/');
228 DBG_BUGON(p <= name);
229 ret = erofs_namei(nd, name, p - name);
234 /* Skip until no more slashes. */
235 for (name = p; *name == '/'; ++name)
241 int erofs_ilookup(const char *path, struct erofs_inode *vi)
246 ret = link_path_walk(path, &nd);
251 return erofs_read_inode_from_disk(vi);