| 1 | |
| 2 | |
| 3 | * Copyright (C) 2017-2018 HUAWEI, Inc.
|
| 4 | * https://www.huawei.com/
|
| 5 | * Copyright (C) 2021, Alibaba Cloud
|
| 6 |
|
| 7 | #include "internal.h"
|
| 8 | #include <linux/filelock.h>
|
| 9 | #include <linux/sched/mm.h>
|
| 10 | #include <trace/events/erofs.h>
|
| 11 |
|
| 12 | void erofs_unmap_metabuf(struct erofs_buf *buf)
|
| 13 | {
|
| 14 | if (!buf->base)
|
| 15 | return;
|
| 16 | kunmap_local(buf->base);
|
| 17 | buf->base = NULL;
|
| 18 | }
|
| 19 |
|
| 20 | void erofs_put_metabuf(struct erofs_buf *buf)
|
| 21 | {
|
| 22 | if (!buf->page)
|
| 23 | return;
|
| 24 | erofs_unmap_metabuf(buf);
|
| 25 | folio_put(page_folio(buf->page));
|
| 26 | buf->page = NULL;
|
| 27 | }
|
| 28 |
|
| 29 | void *erofs_bread(struct erofs_buf *buf, erofs_off_t offset, bool need_kmap)
|
| 30 | {
|
| 31 | pgoff_t index = (buf->off + offset) >> PAGE_SHIFT;
|
| 32 | struct folio *folio = NULL;
|
| 33 |
|
| 34 | if (buf->page) {
|
| 35 | folio = page_folio(buf->page);
|
| 36 | if (folio->mapping != buf->mapping)
|
| 37 |
|
| 38 | folio = NULL;
|
| 39 | else if (folio_file_page(folio, index) != buf->page)
|
| 40 | erofs_unmap_metabuf(buf);
|
| 41 | }
|
| 42 | if (!folio || !folio_contains(folio, index)) {
|
| 43 | erofs_put_metabuf(buf);
|
| 44 | folio = read_cache_folio(buf->mapping, index,
|
| 45 | buf->mc ? erofs_read_meta_folio : NULL, NULL);
|
| 46 | if (IS_ERR(folio))
|
| 47 | return folio;
|
| 48 | }
|
| 49 | buf->page = folio_file_page(folio, index);
|
| 50 | if (!need_kmap)
|
| 51 | return NULL;
|
| 52 | if (!buf->base)
|
| 53 | buf->base = kmap_local_page(buf->page);
|
| 54 | return buf->base + ((buf->off + offset) & ~PAGE_MASK);
|
| 55 | }
|
| 56 |
|
| 57 | int erofs_init_metabuf(struct erofs_buf *buf, struct super_block *sb,
|
| 58 | bool in_metabox)
|
| 59 | {
|
| 60 | struct erofs_sb_info *sbi = EROFS_SB(sb);
|
| 61 |
|
| 62 | buf->mc = false;
|
| 63 | if (in_metabox) {
|
| 64 | if (unlikely(!sbi->metabox_inode))
|
| 65 | return -EFSCORRUPTED;
|
| 66 | buf->mapping = sbi->metabox_inode->i_mapping;
|
| 67 | return 0;
|
| 68 | }
|
| 69 | if (erofs_is_fileio_mode(sbi)) {
|
| 70 | buf->mapping = sbi->managed_cache->i_mapping;
|
| 71 | buf->mc = true;
|
| 72 | } else {
|
| 73 | buf->off = sbi->dif0.fsoff;
|
| 74 | buf->mapping = sb->s_bdev->bd_mapping;
|
| 75 | }
|
| 76 | return 0;
|
| 77 | }
|
| 78 |
|
| 79 | void *erofs_read_metabuf(struct erofs_buf *buf, struct super_block *sb,
|
| 80 | erofs_off_t offset, bool in_metabox)
|
| 81 | {
|
| 82 | int err;
|
| 83 |
|
| 84 | err = erofs_init_metabuf(buf, sb, in_metabox);
|
| 85 | if (err)
|
| 86 | return ERR_PTR(err);
|
| 87 | return erofs_bread(buf, offset, true);
|
| 88 | }
|
| 89 |
|
| 90 | static int erofs_map_chunks(struct inode *inode, struct erofs_map_blocks *map)
|
| 91 | {
|
| 92 | struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
|
| 93 | struct super_block *sb = inode->i_sb;
|
| 94 | struct erofs_inode *vi = EROFS_I(inode);
|
| 95 | struct erofs_inode_chunk_index *idx;
|
| 96 | unsigned int unit = vi->chunkformat & EROFS_CHUNK_FORMAT_INDEXES ?
|
| 97 | sizeof(*idx) : EROFS_BLOCK_MAP_ENTRY_SIZE;
|
| 98 | erofs_blk_t addrmask = (vi->chunkformat & EROFS_CHUNK_FORMAT_48BIT) ?
|
| 99 | BIT_ULL(48) - 1 : BIT_ULL(32) - 1;
|
| 100 | u64 nr = map->m_la >> vi->chunkbits, chunksize = 1ULL << vi->chunkbits;
|
| 101 | erofs_off_t pos = ALIGN(erofs_iloc(inode) + vi->inode_isize +
|
| 102 | vi->xattr_isize, unit) + unit * nr;
|
| 103 |
|
| 104 | erofs_off_t endpos = round_up(pos + 1, sb->s_blocksize);
|
| 105 | u64 last, addr;
|
| 106 |
|
| 107 | idx = erofs_read_metabuf(&buf, sb, pos, erofs_inode_in_metabox(inode));
|
| 108 | if (IS_ERR(idx))
|
| 109 | return PTR_ERR(idx);
|
| 110 |
|
| 111 | map->m_la = nr << vi->chunkbits;
|
| 112 | map->m_llen = 0;
|
| 113 | nr = 0;
|
| 114 | do {
|
| 115 | if (unit == EROFS_BLOCK_MAP_ENTRY_SIZE) {
|
| 116 | addr = le32_to_cpu(((__le32 *)idx)[nr]);
|
| 117 | if (addr == (u32)EROFS_NULL_ADDR)
|
| 118 | addr = EROFS_NULL_ADDR;
|
| 119 | } else {
|
| 120 | addr = (((u64)le16_to_cpu(idx[nr].startblk_hi) << 32) |
|
| 121 | le32_to_cpu(idx[nr].startblk_lo)) & addrmask;
|
| 122 | if (addr ^ (EROFS_NULL_ADDR & addrmask))
|
| 123 | addr |= (u64)(le16_to_cpu(idx[nr].device_id) &
|
| 124 | EROFS_SB(sb)->device_id_mask) << 48;
|
| 125 | else
|
| 126 | addr = EROFS_NULL_ADDR;
|
| 127 | }
|
| 128 | if (!nr) {
|
| 129 | last = addr;
|
| 130 | continue;
|
| 131 | }
|
| 132 |
|
| 133 | map->m_llen += chunksize;
|
| 134 | if (last != EROFS_NULL_ADDR)
|
| 135 | last += erofs_blknr(sb, chunksize);
|
| 136 | } while (addr == last && pos + (++nr) * unit < endpos);
|
| 137 |
|
| 138 | if (last != EROFS_NULL_ADDR) {
|
| 139 | map->m_pa = erofs_pos(sb, last & addrmask) - map->m_llen;
|
| 140 | map->m_deviceid = last >> 48;
|
| 141 | map->m_flags = EROFS_MAP_MAPPED;
|
| 142 | }
|
| 143 | if (addr == last)
|
| 144 | map->m_llen += chunksize;
|
| 145 | map->m_llen = min_t(erofs_off_t, map->m_llen,
|
| 146 | round_up(inode->i_size - map->m_la, sb->s_blocksize));
|
| 147 | erofs_put_metabuf(&buf);
|
| 148 | return 0;
|
| 149 | }
|
| 150 |
|
| 151 | int erofs_map_blocks(struct inode *inode, struct erofs_map_blocks *map)
|
| 152 | {
|
| 153 | struct super_block *sb = inode->i_sb;
|
| 154 | struct erofs_inode *vi = EROFS_I(inode);
|
| 155 | bool tailinline = (vi->datalayout == EROFS_INODE_FLAT_INLINE);
|
| 156 | erofs_off_t pos;
|
| 157 | int err = 0;
|
| 158 |
|
| 159 | trace_erofs_map_blocks_enter(inode, map, 0);
|
| 160 | map->m_deviceid = 0;
|
| 161 | map->m_flags = 0;
|
| 162 | if (map->m_la >= inode->i_size)
|
| 163 | goto out;
|
| 164 | if (vi->datalayout == EROFS_INODE_CHUNK_BASED) {
|
| 165 | err = erofs_map_chunks(inode, map);
|
| 166 | } else if (tailinline || vi->startblk != EROFS_NULL_ADDR) {
|
| 167 | pos = erofs_pos(sb, erofs_iblks(inode) - tailinline);
|
| 168 | map->m_flags = EROFS_MAP_MAPPED;
|
| 169 | if (map->m_la < pos) {
|
| 170 | map->m_pa = erofs_pos(sb, vi->startblk) + map->m_la;
|
| 171 | map->m_llen = pos - map->m_la;
|
| 172 | } else {
|
| 173 | map->m_pa = erofs_iloc(inode) + vi->inode_isize +
|
| 174 | vi->xattr_isize + erofs_blkoff(sb, map->m_la);
|
| 175 | map->m_llen = inode->i_size - map->m_la;
|
| 176 | map->m_flags |= EROFS_MAP_META;
|
| 177 | if (erofs_blkoff(sb, map->m_pa) + map->m_llen >
|
| 178 | sb->s_blocksize) {
|
| 179 | erofs_err(sb, "inline data across blocks @ nid %llu", vi->nid);
|
| 180 | return -EFSCORRUPTED;
|
| 181 | }
|
| 182 | }
|
| 183 | }
|
| 184 | out:
|
| 185 | map->m_plen = err ? 0 : map->m_llen;
|
| 186 | trace_erofs_map_blocks_exit(inode, map, 0, err);
|
| 187 | return err;
|
| 188 | }
|
| 189 |
|
| 190 | static void erofs_fill_from_devinfo(struct erofs_map_dev *map,
|
| 191 | struct super_block *sb, struct erofs_device_info *dif)
|
| 192 | {
|
| 193 | map->m_sb = sb;
|
| 194 | map->m_dif = dif;
|
| 195 | map->m_bdev = NULL;
|
| 196 | if (dif->file && S_ISBLK(file_inode(dif->file)->i_mode))
|
| 197 | map->m_bdev = file_bdev(dif->file);
|
| 198 | }
|
| 199 |
|
| 200 | int erofs_map_dev(struct super_block *sb, struct erofs_map_dev *map)
|
| 201 | {
|
| 202 | struct erofs_dev_context *devs = EROFS_SB(sb)->devs;
|
| 203 | struct erofs_device_info *dif;
|
| 204 | erofs_off_t startoff;
|
| 205 | int id;
|
| 206 |
|
| 207 | erofs_fill_from_devinfo(map, sb, &EROFS_SB(sb)->dif0);
|
| 208 | map->m_bdev = sb->s_bdev;
|
| 209 | if (map->m_deviceid) {
|
| 210 | down_read(&devs->rwsem);
|
| 211 | dif = idr_find(&devs->tree, map->m_deviceid - 1);
|
| 212 | if (!dif) {
|
| 213 | up_read(&devs->rwsem);
|
| 214 | return -ENODEV;
|
| 215 | }
|
| 216 | if (devs->flatdev) {
|
| 217 | map->m_pa += erofs_pos(sb, dif->uniaddr);
|
| 218 | up_read(&devs->rwsem);
|
| 219 | return 0;
|
| 220 | }
|
| 221 | erofs_fill_from_devinfo(map, sb, dif);
|
| 222 | up_read(&devs->rwsem);
|
| 223 | } else if (devs->extra_devices && !devs->flatdev) {
|
| 224 | down_read(&devs->rwsem);
|
| 225 | idr_for_each_entry(&devs->tree, dif, id) {
|
| 226 | if (!dif->uniaddr)
|
| 227 | continue;
|
| 228 |
|
| 229 | startoff = erofs_pos(sb, dif->uniaddr);
|
| 230 | if (map->m_pa >= startoff &&
|
| 231 | map->m_pa < startoff + erofs_pos(sb, dif->blocks)) {
|
| 232 | map->m_pa -= startoff;
|
| 233 | erofs_fill_from_devinfo(map, sb, dif);
|
| 234 | break;
|
| 235 | }
|
| 236 | }
|
| 237 | up_read(&devs->rwsem);
|
| 238 | }
|
| 239 | return 0;
|
| 240 | }
|
| 241 |
|
| 242 | |
| 243 | * bit 30: I/O error occurred on this folio
|
| 244 | * bit 29: CPU has dirty data in D-cache (needs aliasing handling);
|
| 245 | * bit 0 - 29: remaining parts to complete this folio
|
| 246 |
|
| 247 | #define EROFS_ONLINEFOLIO_EIO 30
|
| 248 | #define EROFS_ONLINEFOLIO_DIRTY 29
|
| 249 |
|
| 250 | void erofs_onlinefolio_init(struct folio *folio)
|
| 251 | {
|
| 252 | union {
|
| 253 | atomic_t o;
|
| 254 | void *v;
|
| 255 | } u = { .o = ATOMIC_INIT(1) };
|
| 256 |
|
| 257 | folio->private = u.v;
|
| 258 | }
|
| 259 |
|
| 260 | void erofs_onlinefolio_split(struct folio *folio)
|
| 261 | {
|
| 262 | atomic_inc((atomic_t *)&folio->private);
|
| 263 | }
|
| 264 |
|
| 265 | void erofs_onlinefolio_end(struct folio *folio, int err, bool dirty)
|
| 266 | {
|
| 267 | int orig, v;
|
| 268 |
|
| 269 | do {
|
| 270 | orig = atomic_read((atomic_t *)&folio->private);
|
| 271 | DBG_BUGON(orig <= 0);
|
| 272 | v = dirty << EROFS_ONLINEFOLIO_DIRTY;
|
| 273 | v |= (orig - 1) | (!!err << EROFS_ONLINEFOLIO_EIO);
|
| 274 | } while (atomic_cmpxchg((atomic_t *)&folio->private, orig, v) != orig);
|
| 275 |
|
| 276 | if (v & (BIT(EROFS_ONLINEFOLIO_DIRTY) - 1))
|
| 277 | return;
|
| 278 | folio->private = 0;
|
| 279 | if (v & BIT(EROFS_ONLINEFOLIO_DIRTY))
|
| 280 | flush_dcache_folio(folio);
|
| 281 | folio_end_read(folio, !(v & BIT(EROFS_ONLINEFOLIO_EIO)));
|
| 282 | }
|
| 283 |
|
| 284 | static int erofs_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
|
| 285 | unsigned int flags, struct iomap *iomap, struct iomap *srcmap)
|
| 286 | {
|
| 287 | struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap);
|
| 288 | struct erofs_iomap_iter_ctx *ctx = iter->private;
|
| 289 | struct inode *realinode = ctx ? ctx->realinode : inode;
|
| 290 | struct super_block *sb = realinode->i_sb;
|
| 291 | struct erofs_map_blocks map;
|
| 292 | struct erofs_map_dev mdev;
|
| 293 | int ret;
|
| 294 |
|
| 295 | map.m_la = offset;
|
| 296 | map.m_llen = length;
|
| 297 | ret = erofs_map_blocks(realinode, &map);
|
| 298 | if (ret < 0)
|
| 299 | return ret;
|
| 300 |
|
| 301 | iomap->offset = map.m_la;
|
| 302 | iomap->length = map.m_llen;
|
| 303 | iomap->flags = 0;
|
| 304 | iomap->addr = IOMAP_NULL_ADDR;
|
| 305 | iomap->private = NULL;
|
| 306 | if (!(map.m_flags & EROFS_MAP_MAPPED)) {
|
| 307 | iomap->type = IOMAP_HOLE;
|
| 308 | return 0;
|
| 309 | }
|
| 310 |
|
| 311 | if (!(map.m_flags & EROFS_MAP_META) || !erofs_inode_in_metabox(realinode)) {
|
| 312 | mdev = (struct erofs_map_dev) {
|
| 313 | .m_deviceid = map.m_deviceid,
|
| 314 | .m_pa = map.m_pa,
|
| 315 | };
|
| 316 | ret = erofs_map_dev(sb, &mdev);
|
| 317 | if (ret)
|
| 318 | return ret;
|
| 319 |
|
| 320 | iomap->private = mdev.m_dif;
|
| 321 | if (flags & IOMAP_DAX)
|
| 322 | iomap->dax_dev = mdev.m_dif->dax_dev;
|
| 323 | else
|
| 324 | iomap->bdev = mdev.m_bdev;
|
| 325 | iomap->addr = mdev.m_dif->fsoff + mdev.m_pa;
|
| 326 | if (flags & IOMAP_DAX)
|
| 327 | iomap->addr += mdev.m_dif->dax_part_off;
|
| 328 | }
|
| 329 |
|
| 330 | if (map.m_flags & EROFS_MAP_META) {
|
| 331 | iomap->type = IOMAP_INLINE;
|
| 332 |
|
| 333 | if (ctx) {
|
| 334 | struct erofs_buf buf = __EROFS_BUF_INITIALIZER;
|
| 335 | void *ptr;
|
| 336 |
|
| 337 | ptr = erofs_read_metabuf(&buf, sb, map.m_pa,
|
| 338 | erofs_inode_in_metabox(realinode));
|
| 339 | if (IS_ERR(ptr))
|
| 340 | return PTR_ERR(ptr);
|
| 341 | iomap->inline_data = ptr;
|
| 342 | ctx->page = buf.page;
|
| 343 | ctx->base = buf.base;
|
| 344 | }
|
| 345 | } else {
|
| 346 | iomap->type = IOMAP_MAPPED;
|
| 347 | }
|
| 348 | return 0;
|
| 349 | }
|
| 350 |
|
| 351 | static int erofs_iomap_end(struct inode *inode, loff_t pos, loff_t length,
|
| 352 | ssize_t written, unsigned int flags, struct iomap *iomap)
|
| 353 | {
|
| 354 | struct iomap_iter *iter = container_of(iomap, struct iomap_iter, iomap);
|
| 355 | struct erofs_iomap_iter_ctx *ctx = iter->private;
|
| 356 |
|
| 357 | if (ctx && ctx->base) {
|
| 358 | struct erofs_buf buf = {
|
| 359 | .page = ctx->page,
|
| 360 | .base = ctx->base,
|
| 361 | };
|
| 362 |
|
| 363 | DBG_BUGON(iomap->type != IOMAP_INLINE);
|
| 364 | erofs_put_metabuf(&buf);
|
| 365 | ctx->base = NULL;
|
| 366 | }
|
| 367 | return written;
|
| 368 | }
|
| 369 |
|
| 370 | static DEFINE_IOMAP_ITER_NEXT_END(erofs_iomap_next, erofs_iomap_begin,
|
| 371 | erofs_iomap_end);
|
| 372 |
|
| 373 | static const struct iomap_ops erofs_iomap_ops = {
|
| 374 | .iomap_next = erofs_iomap_next,
|
| 375 | };
|
| 376 |
|
| 377 | int erofs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
|
| 378 | u64 start, u64 len)
|
| 379 | {
|
| 380 | if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) {
|
| 381 | if (!IS_ENABLED(CONFIG_EROFS_FS_ZIP))
|
| 382 | return -EOPNOTSUPP;
|
| 383 | return iomap_fiemap(inode, fieinfo, start, len,
|
| 384 | &z_erofs_iomap_report_ops);
|
| 385 | }
|
| 386 | return iomap_fiemap(inode, fieinfo, start, len, &erofs_iomap_ops);
|
| 387 | }
|
| 388 |
|
| 389 | |
| 390 | * since we dont have write or truncate flows, so no inode
|
| 391 | * locking needs to be held at the moment.
|
| 392 |
|
| 393 | static int erofs_read_folio(struct file *file, struct folio *folio)
|
| 394 | {
|
| 395 | bool need_iput;
|
| 396 | struct erofs_iomap_iter_ctx iter_ctx = {
|
| 397 | .realinode = erofs_real_inode(folio_inode(folio), &need_iput),
|
| 398 | };
|
| 399 | struct erofs_sb_info *sbi = EROFS_I_SB(iter_ctx.realinode);
|
| 400 | struct iomap_read_folio_ctx read_ctx = {
|
| 401 | .ops = erofs_is_fileio_mode(sbi) ?
|
| 402 | &erofs_fileio_iomap_read_ops : &iomap_bio_read_ops,
|
| 403 | .cur_folio = folio,
|
| 404 | };
|
| 405 |
|
| 406 | trace_erofs_read_folio(iter_ctx.realinode, folio, true);
|
| 407 | iomap_read_folio(&erofs_iomap_ops, &read_ctx, &iter_ctx);
|
| 408 | if (need_iput)
|
| 409 | iput(iter_ctx.realinode);
|
| 410 | return 0;
|
| 411 | }
|
| 412 |
|
| 413 | static void erofs_readahead(struct readahead_control *rac)
|
| 414 | {
|
| 415 | bool need_iput;
|
| 416 | struct erofs_iomap_iter_ctx iter_ctx = {
|
| 417 | .realinode = erofs_real_inode(rac->mapping->host, &need_iput),
|
| 418 | };
|
| 419 | struct erofs_sb_info *sbi = EROFS_I_SB(iter_ctx.realinode);
|
| 420 | struct iomap_read_folio_ctx read_ctx = {
|
| 421 | .ops = erofs_is_fileio_mode(sbi) ?
|
| 422 | &erofs_fileio_iomap_read_ops : &iomap_bio_read_ops,
|
| 423 | .rac = rac,
|
| 424 | };
|
| 425 |
|
| 426 | trace_erofs_readahead(iter_ctx.realinode, readahead_index(rac),
|
| 427 | readahead_count(rac), true);
|
| 428 | iomap_readahead(&erofs_iomap_ops, &read_ctx, &iter_ctx);
|
| 429 | if (need_iput)
|
| 430 | iput(iter_ctx.realinode);
|
| 431 | }
|
| 432 |
|
| 433 | static sector_t erofs_bmap(struct address_space *mapping, sector_t block)
|
| 434 | {
|
| 435 | return iomap_bmap(mapping, block, &erofs_iomap_ops);
|
| 436 | }
|
| 437 |
|
| 438 | static ssize_t erofs_file_read_iter(struct kiocb *iocb, struct iov_iter *to)
|
| 439 | {
|
| 440 | struct inode *inode = file_inode(iocb->ki_filp);
|
| 441 |
|
| 442 |
|
| 443 | if (!iov_iter_count(to))
|
| 444 | return 0;
|
| 445 |
|
| 446 | if (IS_ENABLED(CONFIG_FS_DAX) && IS_DAX(inode))
|
| 447 | return dax_iomap_rw(iocb, to, &erofs_iomap_ops);
|
| 448 |
|
| 449 | if ((iocb->ki_flags & IOCB_DIRECT) && inode->i_sb->s_bdev) {
|
| 450 | struct erofs_iomap_iter_ctx iter_ctx = {
|
| 451 | .realinode = inode,
|
| 452 | };
|
| 453 |
|
| 454 | return iomap_dio_rw(iocb, to, &erofs_iomap_ops,
|
| 455 | NULL, 0, &iter_ctx, 0);
|
| 456 | }
|
| 457 | return filemap_read(iocb, to, 0);
|
| 458 | }
|
| 459 |
|
| 460 | const struct address_space_operations erofs_aops = {
|
| 461 | .read_folio = erofs_read_folio,
|
| 462 | .readahead = erofs_readahead,
|
| 463 | .bmap = erofs_bmap,
|
| 464 | .direct_IO = noop_direct_IO,
|
| 465 | .release_folio = iomap_release_folio,
|
| 466 | .invalidate_folio = iomap_invalidate_folio,
|
| 467 | };
|
| 468 |
|
| 469 | #ifdef CONFIG_FS_DAX
|
| 470 | static vm_fault_t erofs_dax_huge_fault(struct vm_fault *vmf,
|
| 471 | unsigned int order)
|
| 472 | {
|
| 473 | return dax_iomap_fault(vmf, order, NULL, NULL, &erofs_iomap_ops);
|
| 474 | }
|
| 475 |
|
| 476 | static vm_fault_t erofs_dax_fault(struct vm_fault *vmf)
|
| 477 | {
|
| 478 | return erofs_dax_huge_fault(vmf, 0);
|
| 479 | }
|
| 480 |
|
| 481 | static const struct vm_operations_struct erofs_dax_vm_ops = {
|
| 482 | .fault = erofs_dax_fault,
|
| 483 | .huge_fault = erofs_dax_huge_fault,
|
| 484 | };
|
| 485 |
|
| 486 | static int erofs_file_mmap_prepare(struct vm_area_desc *desc)
|
| 487 | {
|
| 488 | if (!IS_DAX(file_inode(desc->file)))
|
| 489 | return generic_file_readonly_mmap_prepare(desc);
|
| 490 |
|
| 491 | if (vma_desc_test_all(desc, VMA_SHARED_BIT, VMA_MAYWRITE_BIT))
|
| 492 | return -EINVAL;
|
| 493 |
|
| 494 | desc->vm_ops = &erofs_dax_vm_ops;
|
| 495 | vma_desc_set_flags(desc, VMA_HUGEPAGE_BIT);
|
| 496 | return 0;
|
| 497 | }
|
| 498 | #else
|
| 499 | #define erofs_file_mmap_prepare generic_file_readonly_mmap_prepare
|
| 500 | #endif
|
| 501 |
|
| 502 | loff_t erofs_file_llseek(struct file *file, loff_t offset, int whence)
|
| 503 | {
|
| 504 | struct inode *inode = file->f_mapping->host;
|
| 505 | const struct iomap_ops *ops = &erofs_iomap_ops;
|
| 506 |
|
| 507 | if (erofs_inode_is_data_compressed(EROFS_I(inode)->datalayout)) {
|
| 508 | if (!IS_ENABLED(CONFIG_EROFS_FS_ZIP))
|
| 509 | return generic_file_llseek(file, offset, whence);
|
| 510 | ops = &z_erofs_iomap_report_ops;
|
| 511 | }
|
| 512 |
|
| 513 | if (whence == SEEK_HOLE)
|
| 514 | offset = iomap_seek_hole(inode, offset, ops);
|
| 515 | else if (whence == SEEK_DATA)
|
| 516 | offset = iomap_seek_data(inode, offset, ops);
|
| 517 | else
|
| 518 | return generic_file_llseek(file, offset, whence);
|
| 519 |
|
| 520 | if (offset < 0)
|
| 521 | return offset;
|
| 522 | return vfs_setpos(file, offset, inode->i_sb->s_maxbytes);
|
| 523 | }
|
| 524 |
|
| 525 | const struct file_operations erofs_file_fops = {
|
| 526 | .llseek = erofs_file_llseek,
|
| 527 | .read_iter = erofs_file_read_iter,
|
| 528 | .unlocked_ioctl = erofs_ioctl,
|
| 529 | #ifdef CONFIG_COMPAT
|
| 530 | .compat_ioctl = erofs_compat_ioctl,
|
| 531 | #endif
|
| 532 | .mmap_prepare = erofs_file_mmap_prepare,
|
| 533 | .get_unmapped_area = thp_get_unmapped_area,
|
| 534 | .splice_read = filemap_splice_read,
|
| 535 | .setlease = generic_setlease,
|
| 536 | };
|