1240 lines
33 KiB
C
1240 lines
33 KiB
C
|
// SPDX-License-Identifier: GPL-2.0
|
||
|
/*
|
||
|
* linux/fs/ufs/inode.c
|
||
|
*
|
||
|
* Copyright (C) 1998
|
||
|
* Daniel Pirkl <daniel.pirkl@email.cz>
|
||
|
* Charles University, Faculty of Mathematics and Physics
|
||
|
*
|
||
|
* from
|
||
|
*
|
||
|
* linux/fs/ext2/inode.c
|
||
|
*
|
||
|
* Copyright (C) 1992, 1993, 1994, 1995
|
||
|
* Remy Card (card@masi.ibp.fr)
|
||
|
* Laboratoire MASI - Institut Blaise Pascal
|
||
|
* Universite Pierre et Marie Curie (Paris VI)
|
||
|
*
|
||
|
* from
|
||
|
*
|
||
|
* linux/fs/minix/inode.c
|
||
|
*
|
||
|
* Copyright (C) 1991, 1992 Linus Torvalds
|
||
|
*
|
||
|
* Goal-directed block allocation by Stephen Tweedie (sct@dcs.ed.ac.uk), 1993
|
||
|
* Big-endian to little-endian byte-swapping/bitmaps by
|
||
|
* David S. Miller (davem@caip.rutgers.edu), 1995
|
||
|
*/
|
||
|
|
||
|
#include <linux/uaccess.h>
|
||
|
|
||
|
#include <linux/errno.h>
|
||
|
#include <linux/fs.h>
|
||
|
#include <linux/time.h>
|
||
|
#include <linux/stat.h>
|
||
|
#include <linux/string.h>
|
||
|
#include <linux/mm.h>
|
||
|
#include <linux/buffer_head.h>
|
||
|
#include <linux/writeback.h>
|
||
|
#include <linux/iversion.h>
|
||
|
|
||
|
#include "ufs_fs.h"
|
||
|
#include "ufs.h"
|
||
|
#include "swab.h"
|
||
|
#include "util.h"
|
||
|
|
||
|
static int ufs_block_to_path(struct inode *inode, sector_t i_block, unsigned offsets[4])
|
||
|
{
|
||
|
struct ufs_sb_private_info *uspi = UFS_SB(inode->i_sb)->s_uspi;
|
||
|
int ptrs = uspi->s_apb;
|
||
|
int ptrs_bits = uspi->s_apbshift;
|
||
|
const long direct_blocks = UFS_NDADDR,
|
||
|
indirect_blocks = ptrs,
|
||
|
double_blocks = (1 << (ptrs_bits * 2));
|
||
|
int n = 0;
|
||
|
|
||
|
|
||
|
UFSD("ptrs=uspi->s_apb = %d,double_blocks=%ld \n",ptrs,double_blocks);
|
||
|
if (i_block < direct_blocks) {
|
||
|
offsets[n++] = i_block;
|
||
|
} else if ((i_block -= direct_blocks) < indirect_blocks) {
|
||
|
offsets[n++] = UFS_IND_BLOCK;
|
||
|
offsets[n++] = i_block;
|
||
|
} else if ((i_block -= indirect_blocks) < double_blocks) {
|
||
|
offsets[n++] = UFS_DIND_BLOCK;
|
||
|
offsets[n++] = i_block >> ptrs_bits;
|
||
|
offsets[n++] = i_block & (ptrs - 1);
|
||
|
} else if (((i_block -= double_blocks) >> (ptrs_bits * 2)) < ptrs) {
|
||
|
offsets[n++] = UFS_TIND_BLOCK;
|
||
|
offsets[n++] = i_block >> (ptrs_bits * 2);
|
||
|
offsets[n++] = (i_block >> ptrs_bits) & (ptrs - 1);
|
||
|
offsets[n++] = i_block & (ptrs - 1);
|
||
|
} else {
|
||
|
ufs_warning(inode->i_sb, "ufs_block_to_path", "block > big");
|
||
|
}
|
||
|
return n;
|
||
|
}
|
||
|
|
||
|
typedef struct {
|
||
|
void *p;
|
||
|
union {
|
||
|
__fs32 key32;
|
||
|
__fs64 key64;
|
||
|
};
|
||
|
struct buffer_head *bh;
|
||
|
} Indirect;
|
||
|
|
||
|
static inline int grow_chain32(struct ufs_inode_info *ufsi,
|
||
|
struct buffer_head *bh, __fs32 *v,
|
||
|
Indirect *from, Indirect *to)
|
||
|
{
|
||
|
Indirect *p;
|
||
|
unsigned seq;
|
||
|
to->bh = bh;
|
||
|
do {
|
||
|
seq = read_seqbegin(&ufsi->meta_lock);
|
||
|
to->key32 = *(__fs32 *)(to->p = v);
|
||
|
for (p = from; p <= to && p->key32 == *(__fs32 *)p->p; p++)
|
||
|
;
|
||
|
} while (read_seqretry(&ufsi->meta_lock, seq));
|
||
|
return (p > to);
|
||
|
}
|
||
|
|
||
|
static inline int grow_chain64(struct ufs_inode_info *ufsi,
|
||
|
struct buffer_head *bh, __fs64 *v,
|
||
|
Indirect *from, Indirect *to)
|
||
|
{
|
||
|
Indirect *p;
|
||
|
unsigned seq;
|
||
|
to->bh = bh;
|
||
|
do {
|
||
|
seq = read_seqbegin(&ufsi->meta_lock);
|
||
|
to->key64 = *(__fs64 *)(to->p = v);
|
||
|
for (p = from; p <= to && p->key64 == *(__fs64 *)p->p; p++)
|
||
|
;
|
||
|
} while (read_seqretry(&ufsi->meta_lock, seq));
|
||
|
return (p > to);
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Returns the location of the fragment from
|
||
|
* the beginning of the filesystem.
|
||
|
*/
|
||
|
|
||
|
static u64 ufs_frag_map(struct inode *inode, unsigned offsets[4], int depth)
|
||
|
{
|
||
|
struct ufs_inode_info *ufsi = UFS_I(inode);
|
||
|
struct super_block *sb = inode->i_sb;
|
||
|
struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
|
||
|
u64 mask = (u64) uspi->s_apbmask>>uspi->s_fpbshift;
|
||
|
int shift = uspi->s_apbshift-uspi->s_fpbshift;
|
||
|
Indirect chain[4], *q = chain;
|
||
|
unsigned *p;
|
||
|
unsigned flags = UFS_SB(sb)->s_flags;
|
||
|
u64 res = 0;
|
||
|
|
||
|
UFSD(": uspi->s_fpbshift = %d ,uspi->s_apbmask = %x, mask=%llx\n",
|
||
|
uspi->s_fpbshift, uspi->s_apbmask,
|
||
|
(unsigned long long)mask);
|
||
|
|
||
|
if (depth == 0)
|
||
|
goto no_block;
|
||
|
|
||
|
again:
|
||
|
p = offsets;
|
||
|
|
||
|
if ((flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
|
||
|
goto ufs2;
|
||
|
|
||
|
if (!grow_chain32(ufsi, NULL, &ufsi->i_u1.i_data[*p++], chain, q))
|
||
|
goto changed;
|
||
|
if (!q->key32)
|
||
|
goto no_block;
|
||
|
while (--depth) {
|
||
|
__fs32 *ptr;
|
||
|
struct buffer_head *bh;
|
||
|
unsigned n = *p++;
|
||
|
|
||
|
bh = sb_bread(sb, uspi->s_sbbase +
|
||
|
fs32_to_cpu(sb, q->key32) + (n>>shift));
|
||
|
if (!bh)
|
||
|
goto no_block;
|
||
|
ptr = (__fs32 *)bh->b_data + (n & mask);
|
||
|
if (!grow_chain32(ufsi, bh, ptr, chain, ++q))
|
||
|
goto changed;
|
||
|
if (!q->key32)
|
||
|
goto no_block;
|
||
|
}
|
||
|
res = fs32_to_cpu(sb, q->key32);
|
||
|
goto found;
|
||
|
|
||
|
ufs2:
|
||
|
if (!grow_chain64(ufsi, NULL, &ufsi->i_u1.u2_i_data[*p++], chain, q))
|
||
|
goto changed;
|
||
|
if (!q->key64)
|
||
|
goto no_block;
|
||
|
|
||
|
while (--depth) {
|
||
|
__fs64 *ptr;
|
||
|
struct buffer_head *bh;
|
||
|
unsigned n = *p++;
|
||
|
|
||
|
bh = sb_bread(sb, uspi->s_sbbase +
|
||
|
fs64_to_cpu(sb, q->key64) + (n>>shift));
|
||
|
if (!bh)
|
||
|
goto no_block;
|
||
|
ptr = (__fs64 *)bh->b_data + (n & mask);
|
||
|
if (!grow_chain64(ufsi, bh, ptr, chain, ++q))
|
||
|
goto changed;
|
||
|
if (!q->key64)
|
||
|
goto no_block;
|
||
|
}
|
||
|
res = fs64_to_cpu(sb, q->key64);
|
||
|
found:
|
||
|
res += uspi->s_sbbase;
|
||
|
no_block:
|
||
|
while (q > chain) {
|
||
|
brelse(q->bh);
|
||
|
q--;
|
||
|
}
|
||
|
return res;
|
||
|
|
||
|
changed:
|
||
|
while (q > chain) {
|
||
|
brelse(q->bh);
|
||
|
q--;
|
||
|
}
|
||
|
goto again;
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
* Unpacking tails: we have a file with partial final block and
|
||
|
* we had been asked to extend it. If the fragment being written
|
||
|
* is within the same block, we need to extend the tail just to cover
|
||
|
* that fragment. Otherwise the tail is extended to full block.
|
||
|
*
|
||
|
* Note that we might need to create a _new_ tail, but that will
|
||
|
* be handled elsewhere; this is strictly for resizing old
|
||
|
* ones.
|
||
|
*/
|
||
|
static bool
|
||
|
ufs_extend_tail(struct inode *inode, u64 writes_to,
|
||
|
int *err, struct page *locked_page)
|
||
|
{
|
||
|
struct ufs_inode_info *ufsi = UFS_I(inode);
|
||
|
struct super_block *sb = inode->i_sb;
|
||
|
struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
|
||
|
unsigned lastfrag = ufsi->i_lastfrag; /* it's a short file, so unsigned is enough */
|
||
|
unsigned block = ufs_fragstoblks(lastfrag);
|
||
|
unsigned new_size;
|
||
|
void *p;
|
||
|
u64 tmp;
|
||
|
|
||
|
if (writes_to < (lastfrag | uspi->s_fpbmask))
|
||
|
new_size = (writes_to & uspi->s_fpbmask) + 1;
|
||
|
else
|
||
|
new_size = uspi->s_fpb;
|
||
|
|
||
|
p = ufs_get_direct_data_ptr(uspi, ufsi, block);
|
||
|
tmp = ufs_new_fragments(inode, p, lastfrag, ufs_data_ptr_to_cpu(sb, p),
|
||
|
new_size - (lastfrag & uspi->s_fpbmask), err,
|
||
|
locked_page);
|
||
|
return tmp != 0;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* ufs_inode_getfrag() - allocate new fragment(s)
|
||
|
* @inode: pointer to inode
|
||
|
* @index: number of block pointer within the inode's array.
|
||
|
* @new_fragment: number of new allocated fragment(s)
|
||
|
* @err: we set it if something wrong
|
||
|
* @new: we set it if we allocate new block
|
||
|
* @locked_page: for ufs_new_fragments()
|
||
|
*/
|
||
|
static u64
|
||
|
ufs_inode_getfrag(struct inode *inode, unsigned index,
|
||
|
sector_t new_fragment, int *err,
|
||
|
int *new, struct page *locked_page)
|
||
|
{
|
||
|
struct ufs_inode_info *ufsi = UFS_I(inode);
|
||
|
struct super_block *sb = inode->i_sb;
|
||
|
struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
|
||
|
u64 tmp, goal, lastfrag;
|
||
|
unsigned nfrags = uspi->s_fpb;
|
||
|
void *p;
|
||
|
|
||
|
/* TODO : to be done for write support
|
||
|
if ( (flags & UFS_TYPE_MASK) == UFS_TYPE_UFS2)
|
||
|
goto ufs2;
|
||
|
*/
|
||
|
|
||
|
p = ufs_get_direct_data_ptr(uspi, ufsi, index);
|
||
|
tmp = ufs_data_ptr_to_cpu(sb, p);
|
||
|
if (tmp)
|
||
|
goto out;
|
||
|
|
||
|
lastfrag = ufsi->i_lastfrag;
|
||
|
|
||
|
/* will that be a new tail? */
|
||
|
if (new_fragment < UFS_NDIR_FRAGMENT && new_fragment >= lastfrag)
|
||
|
nfrags = (new_fragment & uspi->s_fpbmask) + 1;
|
||
|
|
||
|
goal = 0;
|
||
|
if (index) {
|
||
|
goal = ufs_data_ptr_to_cpu(sb,
|
||
|
ufs_get_direct_data_ptr(uspi, ufsi, index - 1));
|
||
|
if (goal)
|
||
|
goal += uspi->s_fpb;
|
||
|
}
|
||
|
tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment),
|
||
|
goal, nfrags, err, locked_page);
|
||
|
|
||
|
if (!tmp) {
|
||
|
*err = -ENOSPC;
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
if (new)
|
||
|
*new = 1;
|
||
|
inode->i_ctime = current_time(inode);
|
||
|
if (IS_SYNC(inode))
|
||
|
ufs_sync_inode (inode);
|
||
|
mark_inode_dirty(inode);
|
||
|
out:
|
||
|
return tmp + uspi->s_sbbase;
|
||
|
|
||
|
/* This part : To be implemented ....
|
||
|
Required only for writing, not required for READ-ONLY.
|
||
|
ufs2:
|
||
|
|
||
|
u2_block = ufs_fragstoblks(fragment);
|
||
|
u2_blockoff = ufs_fragnum(fragment);
|
||
|
p = ufsi->i_u1.u2_i_data + block;
|
||
|
goal = 0;
|
||
|
|
||
|
repeat2:
|
||
|
tmp = fs32_to_cpu(sb, *p);
|
||
|
lastfrag = ufsi->i_lastfrag;
|
||
|
|
||
|
*/
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* ufs_inode_getblock() - allocate new block
|
||
|
* @inode: pointer to inode
|
||
|
* @ind_block: block number of the indirect block
|
||
|
* @index: number of pointer within the indirect block
|
||
|
* @new_fragment: number of new allocated fragment
|
||
|
* (block will hold this fragment and also uspi->s_fpb-1)
|
||
|
* @err: see ufs_inode_getfrag()
|
||
|
* @new: see ufs_inode_getfrag()
|
||
|
* @locked_page: see ufs_inode_getfrag()
|
||
|
*/
|
||
|
static u64
|
||
|
ufs_inode_getblock(struct inode *inode, u64 ind_block,
|
||
|
unsigned index, sector_t new_fragment, int *err,
|
||
|
int *new, struct page *locked_page)
|
||
|
{
|
||
|
struct super_block *sb = inode->i_sb;
|
||
|
struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
|
||
|
int shift = uspi->s_apbshift - uspi->s_fpbshift;
|
||
|
u64 tmp = 0, goal;
|
||
|
struct buffer_head *bh;
|
||
|
void *p;
|
||
|
|
||
|
if (!ind_block)
|
||
|
return 0;
|
||
|
|
||
|
bh = sb_bread(sb, ind_block + (index >> shift));
|
||
|
if (unlikely(!bh)) {
|
||
|
*err = -EIO;
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
index &= uspi->s_apbmask >> uspi->s_fpbshift;
|
||
|
if (uspi->fs_magic == UFS2_MAGIC)
|
||
|
p = (__fs64 *)bh->b_data + index;
|
||
|
else
|
||
|
p = (__fs32 *)bh->b_data + index;
|
||
|
|
||
|
tmp = ufs_data_ptr_to_cpu(sb, p);
|
||
|
if (tmp)
|
||
|
goto out;
|
||
|
|
||
|
if (index && (uspi->fs_magic == UFS2_MAGIC ?
|
||
|
(tmp = fs64_to_cpu(sb, ((__fs64 *)bh->b_data)[index-1])) :
|
||
|
(tmp = fs32_to_cpu(sb, ((__fs32 *)bh->b_data)[index-1]))))
|
||
|
goal = tmp + uspi->s_fpb;
|
||
|
else
|
||
|
goal = bh->b_blocknr + uspi->s_fpb;
|
||
|
tmp = ufs_new_fragments(inode, p, ufs_blknum(new_fragment), goal,
|
||
|
uspi->s_fpb, err, locked_page);
|
||
|
if (!tmp)
|
||
|
goto out;
|
||
|
|
||
|
if (new)
|
||
|
*new = 1;
|
||
|
|
||
|
mark_buffer_dirty(bh);
|
||
|
if (IS_SYNC(inode))
|
||
|
sync_dirty_buffer(bh);
|
||
|
inode->i_ctime = current_time(inode);
|
||
|
mark_inode_dirty(inode);
|
||
|
out:
|
||
|
brelse (bh);
|
||
|
UFSD("EXIT\n");
|
||
|
if (tmp)
|
||
|
tmp += uspi->s_sbbase;
|
||
|
return tmp;
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* ufs_getfrag_block() - `get_block_t' function, interface between UFS and
|
||
|
* read_folio, writepage and so on
|
||
|
*/
|
||
|
|
||
|
static int ufs_getfrag_block(struct inode *inode, sector_t fragment, struct buffer_head *bh_result, int create)
|
||
|
{
|
||
|
struct super_block *sb = inode->i_sb;
|
||
|
struct ufs_sb_private_info *uspi = UFS_SB(sb)->s_uspi;
|
||
|
int err = 0, new = 0;
|
||
|
unsigned offsets[4];
|
||
|
int depth = ufs_block_to_path(inode, fragment >> uspi->s_fpbshift, offsets);
|
||
|
u64 phys64 = 0;
|
||
|
unsigned frag = fragment & uspi->s_fpbmask;
|
||
|
|
||
|
phys64 = ufs_frag_map(inode, offsets, depth);
|
||
|
if (!create)
|
||
|
goto done;
|
||
|
|
||
|
if (phys64) {
|
||
|
if (fragment >= UFS_NDIR_FRAGMENT)
|
||
|
goto done;
|
||
|
read_seqlock_excl(&UFS_I(inode)->meta_lock);
|
||
|
if (fragment < UFS_I(inode)->i_lastfrag) {
|
||
|
read_sequnlock_excl(&UFS_I(inode)->meta_lock);
|
||
|
goto done;
|
||
|
}
|
||
|
read_sequnlock_excl(&UFS_I(inode)->meta_lock);
|
||
|
}
|
||
|
/* This code entered only while writing ....? */
|
||
|
|
||
|
mutex_lock(&UFS_I(inode)->truncate_mutex);
|
||
|
|
||
|
UFSD("ENTER, ino %lu, fragment %llu\n", inode->i_ino, (unsigned long long)fragment);
|
||
|
if (unlikely(!depth)) {
|
||
|
ufs_warning(sb, "ufs_get_block", "block > big");
|
||
|
err = -EIO;
|
||
|
goto out;
|
||
|
}
|
||
|
|
||
|
if (UFS_I(inode)->i_lastfrag < UFS_NDIR_FRAGMENT) {
|
||
|
unsigned lastfrag = UFS_I(inode)->i_lastfrag;
|
||
|
unsigned tailfrags = lastfrag & |