// SPDX-License-Identifier: GPL-2.0
/*
* linux/fs/hfsplus/inode.c
*
* Copyright (C) 2001
* Brad Boyer (flar@allandria.com)
* (C) 2003 Ardis Technologies <roman@ardistech.com>
*
* Inode handling routines
*/
#include <linux/blkdev.h>
#include <linux/mm.h>
#include <linux/fs.h>
#include <linux/pagemap.h>
#include <linux/mpage.h>
#include <linux/sched.h>
#include <linux/cred.h>
#include <linux/uio.h>
#include <linux/fileattr.h>
#include "hfsplus_fs.h"
#include "hfsplus_raw.h"
#include "xattr.h"
static int hfsplus_read_folio(struct file *file, struct folio *folio)
{
return block_read_full_folio(folio, hfsplus_get_block);
}
static void hfsplus_write_failed(struct address_space *mapping, loff_t to)
{
struct inode *inode = mapping->host;
if (to > inode->i_size) {
truncate_pagecache(inode, inode->i_size);
hfsplus_file_truncate(inode);
}
}
int hfsplus_write_begin(const struct kiocb *iocb,
struct address_space *mapping, loff_t pos,
unsigned len, struct folio **foliop,
void **fsdata)
{
int ret;
ret = cont_write_begin(iocb, mapping, pos, len, foliop, fsdata,
hfsplus_get_block,
&HFSPLUS_I(mapping->host)->phys_size);
if (unlikely(ret))
hfsplus_write_failed(mapping, pos + len);
return ret;
}
static sector_t hfsplus_bmap(struct address_space *mapping, sector_t block)
{
return generic_block_bmap(mapping, block, hfsplus_get_block);
}
static bool hfsplus_release_folio(struct folio *folio, gfp_t mask)
{
struct inode *inode = folio->mapping->host;
struct super_block *sb = inode->i_sb;
struct hfs_btree *tree;
struct hfs_bnode *node;
u32 nidx;
int i;
bool res = true;
switch (inode->i_ino) {
case HFSPLUS_EXT_CNID:
tree = HFSPLUS_SB(sb)->ext_tree;
break;
case HFSPLUS_CAT_CNID:
tree = HFSPLUS_SB(sb)->cat_tree;
break;
case HFSPLUS_ATTR_CNID:
tree = HFSPLUS_SB(sb)->attr_tree;
break;
default:
BUG();
return false;
}
if (!tree)
return false;
if (tree->node_size >= PAGE_SIZE) {
nidx = folio->index >>
(tree->node_size_shift - PAGE_SHIFT);
spin_lock(&tree->hash_lock);
node = hfs_bnode_findhash(tree, nidx);
if (!node)
;
else if (atomic_read(&node->refcnt))
res = false;
if (res && node) {
hfs_bnode_unhash(node);
hfs_bnode_free(node);
}
spin_unlock(&tree->hash_lock);
} else {
nidx = folio->index <<
(PAGE_SHIFT - tree->node_size_shift);
i = 1 << (PAGE_SHIFT - tree->node_size_shift);
spin_lock(&tree->hash_lock);
do {
node = hfs_bnode_findhash(tree, nidx++);
if (!node)
continue;
if (atomic_read(&node->refcnt)) {
res = false;
break;
}
hfs_bnode_unhash(node);
hfs_bnode_free(node);
} while (--i && nidx < tree->node_count);
spin_unlock(&tree->hash_lock);
}
return res ? try_to_free_buffers(folio) : false;
}
static ssize_t hfsplus_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
{
struct file *file = iocb->ki_filp;
struct address_space *mapping = file->f_mapping;
struct inode *inode = mapping->host;
loff_t isize;
size_t count = iov_iter_count(iter);
loff_t end = iocb->ki_pos + count;
ssize_t ret;
/*
* The hfsplus_get_block() only allows creating the next sequential block.
* For direct writes beyond EOF, expand the file first.
*/
if (iov_iter_rw(iter) == WRITE && iocb->ki_pos > i_size_read(inode)) {
loff_t start_off, end_off;
loff_t start_page, end_page;
isize = i_size_read(inode);
/*
* Wait for any in-flight DIO on this inode to finish before
* calling generic_cont_expand_simple().
*/
inode_dio_wait(inode);
ret = generic_cont_expand_simple(inode, iocb->ki_pos);
if (ret)
return ret;
start_off = isize;
end_off = (end > 0) ? end - 1 : end;
ret = filemap_write_and_wait_range(mapping, start_off, end_off);
if (ret)
return ret;
start_page = start_off >> PAGE_SHIFT;
end_page = end_off >> PAGE_SHIFT;
invalidate_inode_pages2_range(mapping, start_page, end_page);
}
ret = blockdev_direct_IO