License cleanup: add SPDX GPL-2.0 license identifier to files with no license
Many source files in the tree are missing licensing information, which
makes it harder for compliance tools to determine the correct license.
By default all files without license information are under the default
license of the kernel, which is GPL version 2.
Update the files which contain no license information with the 'GPL-2.0'
SPDX license identifier. The SPDX identifier is a legally binding
shorthand, which can be used instead of the full boiler plate text.
This patch is based on work done by Thomas Gleixner and Kate Stewart and
Philippe Ombredanne.
How this work was done:
Patches were generated and checked against linux-4.14-rc6 for a subset of
the use cases:
- file had no licensing information it it.
- file was a */uapi/* one with no licensing information in it,
- file was a */uapi/* one with existing licensing information,
Further patches will be generated in subsequent months to fix up cases
where non-standard license headers were used, and references to license
had to be inferred by heuristics based on keywords.
The analysis to determine which SPDX License Identifier to be applied to
a file was done in a spreadsheet of side by side results from of the
output of two independent scanners (ScanCode & Windriver) producing SPDX
tag:value files created by Philippe Ombredanne. Philippe prepared the
base worksheet, and did an initial spot review of a few 1000 files.
The 4.13 kernel was the starting point of the analysis with 60,537 files
assessed. Kate Stewart did a file by file comparison of the scanner
results in the spreadsheet to determine which SPDX license identifier(s)
to be applied to the file. She confirmed any determination that was not
immediately clear with lawyers working with the Linux Foundation.
Criteria used to select files for SPDX license identifier tagging was:
- Files considered eligible had to be source code files.
- Make and config files were included as candidates if they contained >5
lines of source
- File already had some variant of a license header in it (even if <5
lines).
All documentation files were explicitly excluded.
The following heuristics were used to determine which SPDX license
identifiers to apply.
- when both scanners couldn't find any license traces, file was
considered to have no license information in it, and the top level
COPYING file license applied.
For non */uapi/* files that summary was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 11139
and resulted in the first patch in this series.
If that file was a */uapi/* path one, it was "GPL-2.0 WITH
Linux-syscall-note" otherwise it was "GPL-2.0". Results of that was:
SPDX license identifier # files
---------------------------------------------------|-------
GPL-2.0 WITH Linux-syscall-note 930
and resulted in the second patch in this series.
- if a file had some form of licensing information in it, and was one
of the */uapi/* ones, it was denoted with the Linux-syscall-note if
any GPL family license was found in the file or had no licensing in
it (per prior point). Results summary:
SPDX license identifier # files
---------------------------------------------------|------
GPL-2.0 WITH Linux-syscall-note 270
GPL-2.0+ WITH Linux-syscall-note 169
((GPL-2.0 WITH Linux-syscall-note) OR BSD-2-Clause) 21
((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) 17
LGPL-2.1+ WITH Linux-syscall-note 15
GPL-1.0+ WITH Linux-syscall-note 14
((GPL-2.0+ WITH Linux-syscall-note) OR BSD-3-Clause) 5
LGPL-2.0+ WITH Linux-syscall-note 4
LGPL-2.1 WITH Linux-syscall-note 3
((GPL-2.0 WITH Linux-syscall-note) OR MIT) 3
((GPL-2.0 WITH Linux-syscall-note) AND MIT) 1
and that resulted in the third patch in this series.
- when the two scanners agreed on the detected license(s), that became
the concluded license(s).
- when there was disagreement between the two scanners (one detected a
license but the other didn't, or they both detected different
licenses) a manual inspection of the file occurred.
- In most cases a manual inspection of the information in the file
resulted in a clear resolution of the license that should apply (and
which scanner probably needed to revisit its heuristics).
- When it was not immediately clear, the license identifier was
confirmed with lawyers working with the Linux Foundation.
- If there was any question as to the appropriate license identifier,
the file was flagged for further research and to be revisited later
in time.
In total, over 70 hours of logged manual review was done on the
spreadsheet to determine the SPDX license identifiers to apply to the
source files by Kate, Philippe, Thomas and, in some cases, confirmation
by lawyers working with the Linux Foundation.
Kate also obtained a third independent scan of the 4.13 code base from
FOSSology, and compared selected files where the other two scanners
disagreed against that SPDX file, to see if there was new insights. The
Windriver scanner is based on an older version of FOSSology in part, so
they are related.
Thomas did random spot checks in about 500 files from the spreadsheets
for the uapi headers and agreed with SPDX license identifier in the
files he inspected. For the non-uapi files Thomas did random spot checks
in about 15000 files.
In initial set of patches against 4.14-rc6, 3 files were found to have
copy/paste license identifier errors, and have been fixed to reflect the
correct identifier.
Additionally Philippe spent 10 hours this week doing a detailed manual
inspection and review of the 12,461 patched files from the initial patch
version early this week with:
- a full scancode scan run, collecting the matched texts, detected
license ids and scores
- reviewing anything where there was a license detected (about 500+
files) to ensure that the applied SPDX license was correct
- reviewing anything where there was no detection but the patch license
was not GPL-2.0 WITH Linux-syscall-note to ensure that the applied
SPDX license was correct
This produced a worksheet with 20 files needing minor correction. This
worksheet was then exported into 3 different .csv files for the
different types of files to be modified.
These .csv files were then reviewed by Greg. Thomas wrote a script to
parse the csv files and add the proper SPDX tag to the file, in the
format that the file expected. This script was further refined by Greg
based on the output to detect more types of files automatically and to
distinguish between header and source .c files (which need different
comment types.) Finally Greg ran the script using the .csv files to
generate the patches.
Reviewed-by: Kate Stewart <kstewart@linuxfoundation.org>
Reviewed-by: Philippe Ombredanne <pombredanne@nexb.com>
Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
2017-11-01 22:07:57 +08:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* linux/fs/hfsplus/extents.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001
|
|
|
|
* Brad Boyer (flar@allandria.com)
|
|
|
|
* (C) 2003 Ardis Technologies <roman@ardistech.com>
|
|
|
|
*
|
|
|
|
* Handling of Extents both in catalog and extents overflow trees
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/errno.h>
|
|
|
|
#include <linux/fs.h>
|
|
|
|
#include <linux/pagemap.h>
|
|
|
|
|
|
|
|
#include "hfsplus_fs.h"
|
|
|
|
#include "hfsplus_raw.h"
|
|
|
|
|
|
|
|
/* Compare two extents keys, returns 0 on same, pos/neg for difference */
|
2006-01-19 09:43:08 +08:00
|
|
|
int hfsplus_ext_cmp_key(const hfsplus_btree_key *k1,
|
|
|
|
const hfsplus_btree_key *k2)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
__be32 k1id, k2id;
|
|
|
|
__be32 k1s, k2s;
|
|
|
|
|
|
|
|
k1id = k1->ext.cnid;
|
|
|
|
k2id = k2->ext.cnid;
|
|
|
|
if (k1id != k2id)
|
|
|
|
return be32_to_cpu(k1id) < be32_to_cpu(k2id) ? -1 : 1;
|
|
|
|
|
|
|
|
if (k1->ext.fork_type != k2->ext.fork_type)
|
|
|
|
return k1->ext.fork_type < k2->ext.fork_type ? -1 : 1;
|
|
|
|
|
|
|
|
k1s = k1->ext.start_block;
|
|
|
|
k2s = k2->ext.start_block;
|
|
|
|
if (k1s == k2s)
|
|
|
|
return 0;
|
|
|
|
return be32_to_cpu(k1s) < be32_to_cpu(k2s) ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hfsplus_ext_build_key(hfsplus_btree_key *key, u32 cnid,
|
|
|
|
u32 block, u8 type)
|
|
|
|
{
|
|
|
|
key->key_len = cpu_to_be16(HFSPLUS_EXT_KEYLEN - 2);
|
|
|
|
key->ext.cnid = cpu_to_be32(cnid);
|
|
|
|
key->ext.start_block = cpu_to_be32(block);
|
|
|
|
key->ext.fork_type = type;
|
|
|
|
key->ext.pad = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 hfsplus_ext_find_block(struct hfsplus_extent *ext, u32 off)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u32 count;
|
|
|
|
|
|
|
|
for (i = 0; i < 8; ext++, i++) {
|
|
|
|
count = be32_to_cpu(ext->block_count);
|
|
|
|
if (off < count)
|
|
|
|
return be32_to_cpu(ext->start_block) + off;
|
|
|
|
off -= count;
|
|
|
|
}
|
|
|
|
/* panic? */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hfsplus_ext_block_count(struct hfsplus_extent *ext)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
u32 count = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < 8; ext++, i++)
|
|
|
|
count += be32_to_cpu(ext->block_count);
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u32 hfsplus_ext_lastblock(struct hfsplus_extent *ext)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
ext += 7;
|
|
|
|
for (i = 0; i < 7; ext--, i++)
|
|
|
|
if (ext->block_count)
|
|
|
|
break;
|
|
|
|
return be32_to_cpu(ext->start_block) + be32_to_cpu(ext->block_count);
|
|
|
|
}
|
|
|
|
|
2013-05-01 06:27:56 +08:00
|
|
|
static int __hfsplus_ext_write_extent(struct inode *inode,
|
2010-12-17 00:08:38 +08:00
|
|
|
struct hfs_find_data *fd)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2010-10-01 11:43:31 +08:00
|
|
|
struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
|
2005-04-17 06:20:36 +08:00
|
|
|
int res;
|
|
|
|
|
2010-10-01 11:46:31 +08:00
|
|
|
WARN_ON(!mutex_is_locked(&hip->extents_lock));
|
|
|
|
|
2010-10-01 11:43:31 +08:00
|
|
|
hfsplus_ext_build_key(fd->search_key, inode->i_ino, hip->cached_start,
|
|
|
|
HFSPLUS_IS_RSRC(inode) ?
|
|
|
|
HFSPLUS_TYPE_RSRC : HFSPLUS_TYPE_DATA);
|
|
|
|
|
2013-02-28 09:03:04 +08:00
|
|
|
res = hfs_brec_find(fd, hfs_find_rec_by_key);
|
2010-11-23 21:38:13 +08:00
|
|
|
if (hip->extent_state & HFSPLUS_EXT_NEW) {
|
2005-04-17 06:20:36 +08:00
|
|
|
if (res != -ENOENT)
|
2013-05-01 06:27:56 +08:00
|
|
|
return res;
|
2018-10-31 06:06:14 +08:00
|
|
|
/* Fail early and avoid ENOSPC during the btree operation */
|
|
|
|
res = hfs_bmap_reserve(fd->tree, fd->tree->depth + 1);
|
|
|
|
if (res)
|
|
|
|
return res;
|
2010-10-01 11:43:31 +08:00
|
|
|
hfs_brec_insert(fd, hip->cached_extents,
|
|
|
|
sizeof(hfsplus_extent_rec));
|
2010-11-23 21:38:13 +08:00
|
|
|
hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW);
|
2005-04-17 06:20:36 +08:00
|
|
|
} else {
|
|
|
|
if (res)
|
2013-05-01 06:27:56 +08:00
|
|
|
return res;
|
2010-10-01 11:43:31 +08:00
|
|
|
hfs_bnode_write(fd->bnode, hip->cached_extents,
|
|
|
|
fd->entryoffset, fd->entrylength);
|
2010-11-23 21:38:13 +08:00
|
|
|
hip->extent_state &= ~HFSPLUS_EXT_DIRTY;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2010-11-23 21:38:15 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We can't just use hfsplus_mark_inode_dirty here, because we
|
|
|
|
* also get called from hfsplus_write_inode, which should not
|
|
|
|
* redirty the inode. Instead the callers have to be careful
|
|
|
|
* to explicily mark the inode dirty, too.
|
|
|
|
*/
|
|
|
|
set_bit(HFSPLUS_I_EXT_DIRTY, &hip->flags);
|
2013-05-01 06:27:56 +08:00
|
|
|
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2011-07-06 06:30:00 +08:00
|
|
|
static int hfsplus_ext_write_extent_locked(struct inode *inode)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2013-05-01 06:27:56 +08:00
|
|
|
int res = 0;
|
2011-07-06 06:30:00 +08:00
|
|
|
|
2010-11-23 21:38:13 +08:00
|
|
|
if (HFSPLUS_I(inode)->extent_state & HFSPLUS_EXT_DIRTY) {
|
2005-04-17 06:20:36 +08:00
|
|
|
struct hfs_find_data fd;
|
|
|
|
|
2011-07-06 06:30:00 +08:00
|
|
|
res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd);
|
|
|
|
if (res)
|
|
|
|
return res;
|
2013-05-01 06:27:56 +08:00
|
|
|
res = __hfsplus_ext_write_extent(inode, &fd);
|
2011-07-06 06:30:00 +08:00
|
|
|
hfs_find_exit(&fd);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2013-05-01 06:27:56 +08:00
|
|
|
return res;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2011-07-06 06:30:00 +08:00
|
|
|
int hfsplus_ext_write_extent(struct inode *inode)
|
2010-10-01 11:46:31 +08:00
|
|
|
{
|
2011-07-06 06:30:00 +08:00
|
|
|
int res;
|
|
|
|
|
2010-10-01 11:46:31 +08:00
|
|
|
mutex_lock(&HFSPLUS_I(inode)->extents_lock);
|
2011-07-06 06:30:00 +08:00
|
|
|
res = hfsplus_ext_write_extent_locked(inode);
|
2010-10-01 11:46:31 +08:00
|
|
|
mutex_unlock(&HFSPLUS_I(inode)->extents_lock);
|
2011-07-06 06:30:00 +08:00
|
|
|
|
|
|
|
return res;
|
2010-10-01 11:46:31 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static inline int __hfsplus_ext_read_extent(struct hfs_find_data *fd,
|
|
|
|
struct hfsplus_extent *extent,
|
|
|
|
u32 cnid, u32 block, u8 type)
|
|
|
|
{
|
|
|
|
int res;
|
|
|
|
|
|
|
|
hfsplus_ext_build_key(fd->search_key, cnid, block, type);
|
|
|
|
fd->key->ext.cnid = 0;
|
2013-02-28 09:03:04 +08:00
|
|
|
res = hfs_brec_find(fd, hfs_find_rec_by_key);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (res && res != -ENOENT)
|
|
|
|
return res;
|
|
|
|
if (fd->key->ext.cnid != fd->search_key->ext.cnid ||
|
|
|
|
fd->key->ext.fork_type != fd->search_key->ext.fork_type)
|
|
|
|
return -ENOENT;
|
|
|
|
if (fd->entrylength != sizeof(hfsplus_extent_rec))
|
|
|
|
return -EIO;
|
2010-12-17 00:08:38 +08:00
|
|
|
hfs_bnode_read(fd->bnode, extent, fd->entryoffset,
|
|
|
|
sizeof(hfsplus_extent_rec));
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-12-17 00:08:38 +08:00
|
|
|
static inline int __hfsplus_ext_cache_extent(struct hfs_find_data *fd,
|
|
|
|
struct inode *inode, u32 block)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2010-10-01 11:43:31 +08:00
|
|
|
struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
|
2005-04-17 06:20:36 +08:00
|
|
|
int res;
|
|
|
|
|
2010-10-01 11:46:31 +08:00
|
|
|
WARN_ON(!mutex_is_locked(&hip->extents_lock));
|
|
|
|
|
2013-05-01 06:27:56 +08:00
|
|
|
if (hip->extent_state & HFSPLUS_EXT_DIRTY) {
|
|
|
|
res = __hfsplus_ext_write_extent(inode, fd);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-10-01 11:43:31 +08:00
|
|
|
res = __hfsplus_ext_read_extent(fd, hip->cached_extents, inode->i_ino,
|
|
|
|
block, HFSPLUS_IS_RSRC(inode) ?
|
|
|
|
HFSPLUS_TYPE_RSRC :
|
|
|
|
HFSPLUS_TYPE_DATA);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!res) {
|
2010-10-01 11:43:31 +08:00
|
|
|
hip->cached_start = be32_to_cpu(fd->key->ext.start_block);
|
2010-12-17 00:08:38 +08:00
|
|
|
hip->cached_blocks =
|
|
|
|
hfsplus_ext_block_count(hip->cached_extents);
|
2005-04-17 06:20:36 +08:00
|
|
|
} else {
|
2010-10-01 11:43:31 +08:00
|
|
|
hip->cached_start = hip->cached_blocks = 0;
|
2010-11-23 21:38:13 +08:00
|
|
|
hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hfsplus_ext_read_extent(struct inode *inode, u32 block)
|
|
|
|
{
|
2010-10-01 11:43:31 +08:00
|
|
|
struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
|
2005-04-17 06:20:36 +08:00
|
|
|
struct hfs_find_data fd;
|
|
|
|
int res;
|
|
|
|
|
2010-10-01 11:43:31 +08:00
|
|
|
if (block >= hip->cached_start &&
|
|
|
|
block < hip->cached_start + hip->cached_blocks)
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
|
2011-07-06 06:29:59 +08:00
|
|
|
res = hfs_find_init(HFSPLUS_SB(inode->i_sb)->ext_tree, &fd);
|
|
|
|
if (!res) {
|
|
|
|
res = __hfsplus_ext_cache_extent(&fd, inode, block);
|
|
|
|
hfs_find_exit(&fd);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get a block at iblock for inode, possibly allocating if create */
|
|
|
|
int hfsplus_get_block(struct inode *inode, sector_t iblock,
|
|
|
|
struct buffer_head *bh_result, int create)
|
|
|
|
{
|
2010-10-01 11:42:59 +08:00
|
|
|
struct super_block *sb = inode->i_sb;
|
|
|
|
struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
|
2010-10-01 11:43:31 +08:00
|
|
|
struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
|
2005-04-17 06:20:36 +08:00
|
|
|
int res = -EIO;
|
|
|
|
u32 ablock, dblock, mask;
|
2011-02-16 16:34:17 +08:00
|
|
|
sector_t sector;
|
2010-11-23 21:38:15 +08:00
|
|
|
int was_dirty = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
/* Convert inode block to disk allocation block */
|
2010-10-01 11:42:59 +08:00
|
|
|
ablock = iblock >> sbi->fs_shift;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-10-01 11:43:31 +08:00
|
|
|
if (iblock >= hip->fs_blocks) {
|
2018-10-31 06:06:21 +08:00
|
|
|
if (!create)
|
|
|
|
return 0;
|
|
|
|
if (iblock > hip->fs_blocks)
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EIO;
|
2010-10-01 11:43:31 +08:00
|
|
|
if (ablock >= hip->alloc_blocks) {
|
2014-06-07 05:36:28 +08:00
|
|
|
res = hfsplus_file_extend(inode, false);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
create = 0;
|
|
|
|
|
2010-10-01 11:43:31 +08:00
|
|
|
if (ablock < hip->first_blocks) {
|
|
|
|
dblock = hfsplus_ext_find_block(hip->first_extents, ablock);
|
2005-04-17 06:20:36 +08:00
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2008-10-19 11:28:02 +08:00
|
|
|
if (inode->i_ino == HFSPLUS_EXT_CNID)
|
|
|
|
return -EIO;
|
|
|
|
|
2010-10-01 11:43:31 +08:00
|
|
|
mutex_lock(&hip->extents_lock);
|
2010-11-23 21:38:15 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* hfsplus_ext_read_extent will write out a cached extent into
|
|
|
|
* the extents btree. In that case we may have to mark the inode
|
|
|
|
* dirty even for a pure read of an extent here.
|
|
|
|
*/
|
|
|
|
was_dirty = (hip->extent_state & HFSPLUS_EXT_DIRTY);
|
2005-04-17 06:20:36 +08:00
|
|
|
res = hfsplus_ext_read_extent(inode, ablock);
|
2010-11-23 21:38:15 +08:00
|
|
|
if (res) {
|
2010-10-01 11:43:31 +08:00
|
|
|
mutex_unlock(&hip->extents_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
return -EIO;
|
|
|
|
}
|
2010-11-23 21:38:15 +08:00
|
|
|
dblock = hfsplus_ext_find_block(hip->cached_extents,
|
|
|
|
ablock - hip->cached_start);
|
2010-10-01 11:43:31 +08:00
|
|
|
mutex_unlock(&hip->extents_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
done:
|
2013-05-01 06:27:54 +08:00
|
|
|
hfs_dbg(EXTENT, "get_block(%lu): %llu - %u\n",
|
2010-12-17 00:08:38 +08:00
|
|
|
inode->i_ino, (long long)iblock, dblock);
|
2011-02-16 16:34:17 +08:00
|
|
|
|
2010-10-01 11:42:59 +08:00
|
|
|
mask = (1 << sbi->fs_shift) - 1;
|
2011-02-16 16:34:17 +08:00
|
|
|
sector = ((sector_t)dblock << sbi->fs_shift) +
|
|
|
|
sbi->blockoffset + (iblock & mask);
|
|
|
|
map_bh(bh_result, sb, sector);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
if (create) {
|
|
|
|
set_buffer_new(bh_result);
|
2010-10-01 11:43:31 +08:00
|
|
|
hip->phys_size += sb->s_blocksize;
|
|
|
|
hip->fs_blocks++;
|
2005-04-17 06:20:36 +08:00
|
|
|
inode_add_bytes(inode, sb->s_blocksize);
|
|
|
|
}
|
2010-11-23 21:38:15 +08:00
|
|
|
if (create || was_dirty)
|
|
|
|
mark_inode_dirty(inode);
|
2005-04-17 06:20:36 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void hfsplus_dump_extent(struct hfsplus_extent *extent)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
2013-05-01 06:27:54 +08:00
|
|
|
hfs_dbg(EXTENT, " ");
|
2005-04-17 06:20:36 +08:00
|
|
|
for (i = 0; i < 8; i++)
|
2013-05-01 06:27:54 +08:00
|
|
|
hfs_dbg_cont(EXTENT, " %u:%u",
|
|
|
|
be32_to_cpu(extent[i].start_block),
|
|
|
|
be32_to_cpu(extent[i].block_count));
|
|
|
|
hfs_dbg_cont(EXTENT, "\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int hfsplus_add_extent(struct hfsplus_extent *extent, u32 offset,
|
|
|
|
u32 alloc_block, u32 block_count)
|
|
|
|
{
|
|
|
|
u32 count, start;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
hfsplus_dump_extent(extent);
|
|
|
|
for (i = 0; i < 8; extent++, i++) {
|
|
|
|
count = be32_to_cpu(extent->block_count);
|
|
|
|
if (offset == count) {
|
|
|
|
start = be32_to_cpu(extent->start_block);
|
|
|
|
if (alloc_block != start + count) {
|
|
|
|
if (++i >= 8)
|
|
|
|
return -ENOSPC;
|
|
|
|
extent++;
|
|
|
|
extent->start_block = cpu_to_be32(alloc_block);
|
|
|
|
} else
|
|
|
|
block_count += count;
|
|
|
|
extent->block_count = cpu_to_be32(block_count);
|
|
|
|
return 0;
|
|
|
|
} else if (offset < count)
|
|
|
|
break;
|
|
|
|
offset -= count;
|
|
|
|
}
|
|
|
|
/* panic? */
|
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int hfsplus_free_extents(struct super_block *sb,
|
|
|
|
struct hfsplus_extent *extent,
|
|
|
|
u32 offset, u32 block_nr)
|
|
|
|
{
|
|
|
|
u32 count, start;
|
|
|
|
int i;
|
2012-12-21 07:05:25 +08:00
|
|
|
int err = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-08-22 12:59:16 +08:00
|
|
|
/* Mapping the allocation file may lock the extent tree */
|
|
|
|
WARN_ON(mutex_is_locked(&HFSPLUS_SB(sb)->ext_tree->tree_lock));
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
hfsplus_dump_extent(extent);
|
|
|
|
for (i = 0; i < 8; extent++, i++) {
|
|
|
|
count = be32_to_cpu(extent->block_count);
|
|
|
|
if (offset == count)
|
|
|
|
goto found;
|
|
|
|
else if (offset < count)
|
|
|
|
break;
|
|
|
|
offset -= count;
|
|
|
|
}
|
|
|
|
/* panic? */
|
|
|
|
return -EIO;
|
|
|
|
found:
|
|
|
|
for (;;) {
|
|
|
|
start = be32_to_cpu(extent->start_block);
|
|
|
|
if (count <= block_nr) {
|
2012-12-21 07:05:25 +08:00
|
|
|
err = hfsplus_block_free(sb, start, count);
|
|
|
|
if (err) {
|
2013-05-01 06:27:55 +08:00
|
|
|
pr_err("can't free extent\n");
|
2013-05-01 06:27:54 +08:00
|
|
|
hfs_dbg(EXTENT, " start: %u count: %u\n",
|
2012-12-21 07:05:25 +08:00
|
|
|
start, count);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
extent->block_count = 0;
|
|
|
|
extent->start_block = 0;
|
|
|
|
block_nr -= count;
|
|
|
|
} else {
|
|
|
|
count -= block_nr;
|
2012-12-21 07:05:25 +08:00
|
|
|
err = hfsplus_block_free(sb, start + count, block_nr);
|
|
|
|
if (err) {
|
2013-05-01 06:27:55 +08:00
|
|
|
pr_err("can't free extent\n");
|
2013-05-01 06:27:54 +08:00
|
|
|
hfs_dbg(EXTENT, " start: %u count: %u\n",
|
2012-12-21 07:05:25 +08:00
|
|
|
start, count);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
extent->block_count = cpu_to_be32(count);
|
|
|
|
block_nr = 0;
|
|
|
|
}
|
2012-12-21 07:05:25 +08:00
|
|
|
if (!block_nr || !i) {
|
|
|
|
/*
|
|
|
|
* Try to free all extents and
|
|
|
|
* return only last error
|
|
|
|
*/
|
|
|
|
return err;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
i--;
|
|
|
|
extent--;
|
|
|
|
count = be32_to_cpu(extent->block_count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-12-17 00:08:38 +08:00
|
|
|
int hfsplus_free_fork(struct super_block *sb, u32 cnid,
|
|
|
|
struct hfsplus_fork_raw *fork, int type)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct hfs_find_data fd;
|
|
|
|
hfsplus_extent_rec ext_entry;
|
|
|
|
u32 total_blocks, blocks, start;
|
|
|
|
int res, i;
|
|
|
|
|
|
|
|
total_blocks = be32_to_cpu(fork->total_blocks);
|
|
|
|
if (!total_blocks)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
blocks = 0;
|
|
|
|
for (i = 0; i < 8; i++)
|
|
|
|
blocks += be32_to_cpu(fork->extents[i].block_count);
|
|
|
|
|
|
|
|
res = hfsplus_free_extents(sb, fork->extents, blocks, blocks);
|
|
|
|
if (res)
|
|
|
|
return res;
|
|
|
|
if (total_blocks == blocks)
|
|
|
|
return 0;
|
|
|
|
|
2011-07-06 06:29:59 +08:00
|
|
|
res = hfs_find_init(HFSPLUS_SB(sb)->ext_tree, &fd);
|
|
|
|
if (res)
|
|
|
|
return res;
|
2005-04-17 06:20:36 +08:00
|
|
|
do {
|
|
|
|
res = __hfsplus_ext_read_extent(&fd, ext_entry, cnid,
|
|
|
|
total_blocks, type);
|
|
|
|
if (res)
|
|
|
|
break;
|
|
|
|
start = be32_to_cpu(fd.key->ext.start_block);
|
|
|
|
hfs_brec_remove(&fd);
|
2018-08-22 12:59:16 +08:00
|
|
|
|
|
|
|
mutex_unlock(&fd.tree->tree_lock);
|
|
|
|
hfsplus_free_extents(sb, ext_entry, total_blocks - start,
|
|
|
|
total_blocks);
|
2005-04-17 06:20:36 +08:00
|
|
|
total_blocks = start;
|
2018-08-22 12:59:16 +08:00
|
|
|
mutex_lock(&fd.tree->tree_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
} while (total_blocks > blocks);
|
|
|
|
hfs_find_exit(&fd);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2014-06-07 05:36:28 +08:00
|
|
|
int hfsplus_file_extend(struct inode *inode, bool zeroout)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
struct super_block *sb = inode->i_sb;
|
2010-10-01 11:42:59 +08:00
|
|
|
struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
|
2010-10-01 11:43:31 +08:00
|
|
|
struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
|
2005-04-17 06:20:36 +08:00
|
|
|
u32 start, len, goal;
|
|
|
|
int res;
|
|
|
|
|
2011-02-03 00:40:33 +08:00
|
|
|
if (sbi->alloc_file->i_size * 8 <
|
|
|
|
sbi->total_blocks - sbi->free_blocks + 8) {
|
2010-12-17 00:08:39 +08:00
|
|
|
/* extend alloc file */
|
2023-07-19 20:17:35 +08:00
|
|
|
pr_err_ratelimited("extend alloc file! (%llu,%u,%u)\n",
|
|
|
|
sbi->alloc_file->i_size * 8,
|
|
|
|
sbi->total_blocks, sbi->free_blocks);
|
2005-04-17 06:20:36 +08:00
|
|
|
return -ENOSPC;
|
|
|
|
}
|
|
|
|
|
2010-10-01 11:43:31 +08:00
|
|
|
mutex_lock(&hip->extents_lock);
|
|
|
|
if (hip->alloc_blocks == hip->first_blocks)
|
|
|
|
goal = hfsplus_ext_lastblock(hip->first_extents);
|
2005-04-17 06:20:36 +08:00
|
|
|
else {
|
2010-10-01 11:43:31 +08:00
|
|
|
res = hfsplus_ext_read_extent(inode, hip->alloc_blocks);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (res)
|
|
|
|
goto out;
|
2010-10-01 11:43:31 +08:00
|
|
|
goal = hfsplus_ext_lastblock(hip->cached_extents);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2010-10-01 11:43:31 +08:00
|
|
|
len = hip->clump_blocks;
|
2010-10-01 11:42:59 +08:00
|
|
|
start = hfsplus_block_allocate(sb, sbi->total_blocks, goal, &len);
|
|
|
|
if (start >= sbi->total_blocks) {
|
2005-04-17 06:20:36 +08:00
|
|
|
start = hfsplus_block_allocate(sb, goal, 0, &len);
|
|
|
|
if (start >= goal) {
|
|
|
|
res = -ENOSPC;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-07 05:36:28 +08:00
|
|
|
if (zeroout) {
|
|
|
|
res = sb_issue_zeroout(sb, start, len, GFP_NOFS);
|
|
|
|
if (res)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2013-05-01 06:27:54 +08:00
|
|
|
hfs_dbg(EXTENT, "extend %lu: %u,%u\n", inode->i_ino, start, len);
|
2010-10-01 11:43:31 +08:00
|
|
|
|
|
|
|
if (hip->alloc_blocks <= hip->first_blocks) {
|
|
|
|
if (!hip->first_blocks) {
|
2013-05-01 06:27:54 +08:00
|
|
|
hfs_dbg(EXTENT, "first extents\n");
|
2005-04-17 06:20:36 +08:00
|
|
|
/* no extents yet */
|
2010-10-01 11:43:31 +08:00
|
|
|
hip->first_extents[0].start_block = cpu_to_be32(start);
|
|
|
|
hip->first_extents[0].block_count = cpu_to_be32(len);
|
2005-04-17 06:20:36 +08:00
|
|
|
res = 0;
|
|
|
|
} else {
|
|
|
|
/* try to append to extents in inode */
|
2010-10-01 11:43:31 +08:00
|
|
|
res = hfsplus_add_extent(hip->first_extents,
|
|
|
|
hip->alloc_blocks,
|
2005-04-17 06:20:36 +08:00
|
|
|
start, len);
|
|
|
|
if (res == -ENOSPC)
|
|
|
|
goto insert_extent;
|
|
|
|
}
|
|
|
|
if (!res) {
|
2010-10-01 11:43:31 +08:00
|
|
|
hfsplus_dump_extent(hip->first_extents);
|
|
|
|
hip->first_blocks += len;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
} else {
|
2010-10-01 11:43:31 +08:00
|
|
|
res = hfsplus_add_extent(hip->cached_extents,
|
|
|
|
hip->alloc_blocks - hip->cached_start,
|
2005-04-17 06:20:36 +08:00
|
|
|
start, len);
|
|
|
|
if (!res) {
|
2010-10-01 11:43:31 +08:00
|
|
|
hfsplus_dump_extent(hip->cached_extents);
|
2010-11-23 21:38:13 +08:00
|
|
|
hip->extent_state |= HFSPLUS_EXT_DIRTY;
|
2010-10-01 11:43:31 +08:00
|
|
|
hip->cached_blocks += len;
|
2005-04-17 06:20:36 +08:00
|
|
|
} else if (res == -ENOSPC)
|
|
|
|
goto insert_extent;
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
if (!res) {
|
2010-10-01 11:43:31 +08:00
|
|
|
hip->alloc_blocks += len;
|
2014-04-04 05:50:34 +08:00
|
|
|
mutex_unlock(&hip->extents_lock);
|
2010-11-23 21:38:15 +08:00
|
|
|
hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ALLOC_DIRTY);
|
2014-04-04 05:50:34 +08:00
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2014-04-04 05:50:34 +08:00
|
|
|
mutex_unlock(&hip->extents_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
return res;
|
|
|
|
|
|
|
|
insert_extent:
|
2013-05-01 06:27:54 +08:00
|
|
|
hfs_dbg(EXTENT, "insert new extent\n");
|
2011-07-06 06:30:00 +08:00
|
|
|
res = hfsplus_ext_write_extent_locked(inode);
|
|
|
|
if (res)
|
|
|
|
goto out;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2010-10-01 11:43:31 +08:00
|
|
|
memset(hip->cached_extents, 0, sizeof(hfsplus_extent_rec));
|
|
|
|
hip->cached_extents[0].start_block = cpu_to_be32(start);
|
|
|
|
hip->cached_extents[0].block_count = cpu_to_be32(len);
|
|
|
|
hfsplus_dump_extent(hip->cached_extents);
|
2010-11-23 21:38:13 +08:00
|
|
|
hip->extent_state |= HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW;
|
2010-10-01 11:43:31 +08:00
|
|
|
hip->cached_start = hip->alloc_blocks;
|
|
|
|
hip->cached_blocks = len;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
res = 0;
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
void hfsplus_file_truncate(struct inode *inode)
|
|
|
|
{
|
|
|
|
struct super_block *sb = inode->i_sb;
|
2010-10-01 11:43:31 +08:00
|
|
|
struct hfsplus_inode_info *hip = HFSPLUS_I(inode);
|
2005-04-17 06:20:36 +08:00
|
|
|
struct hfs_find_data fd;
|
|
|
|
u32 alloc_cnt, blk_cnt, start;
|
|
|
|
int res;
|
|
|
|
|
2013-05-01 06:27:54 +08:00
|
|
|
hfs_dbg(INODE, "truncate: %lu, %llu -> %llu\n",
|
|
|
|
inode->i_ino, (long long)hip->phys_size, inode->i_size);
|
2010-10-01 11:43:31 +08:00
|
|
|
|
|
|
|
if (inode->i_size > hip->phys_size) {
|
2005-04-17 06:20:36 +08:00
|
|
|
struct address_space *mapping = inode->i_mapping;
|
|
|
|
struct page *page;
|
2022-11-21 19:21:34 +08:00
|
|
|
void *fsdata = NULL;
|
2013-04-18 06:58:33 +08:00
|
|
|
loff_t size = inode->i_size;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2022-03-04 02:43:29 +08:00
|
|
|
res = hfsplus_write_begin(NULL, mapping, size, 0,
|
|
|
|
&page, &fsdata);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (res)
|
2007-10-16 16:25:09 +08:00
|
|
|
return;
|
2022-03-04 02:43:29 +08:00
|
|
|
res = generic_write_end(NULL, mapping, size, 0, 0,
|
|
|
|
page, fsdata);
|
2007-10-16 16:25:09 +08:00
|
|
|
if (res < 0)
|
|
|
|
return;
|
2005-04-17 06:20:36 +08:00
|
|
|
mark_inode_dirty(inode);
|
|
|
|
return;
|
2010-10-01 11:43:31 +08:00
|
|
|
} else if (inode->i_size == hip->phys_size)
|
2005-08-02 12:11:40 +08:00
|
|
|
return;
|
|
|
|
|
2010-10-01 11:42:59 +08:00
|
|
|
blk_cnt = (inode->i_size + HFSPLUS_SB(sb)->alloc_blksz - 1) >>
|
|
|
|
HFSPLUS_SB(sb)->alloc_blksz_shift;
|
2014-04-04 05:50:34 +08:00
|
|
|
|
|
|
|
mutex_lock(&hip->extents_lock);
|
|
|
|
|
2010-10-01 11:43:31 +08:00
|
|
|
alloc_cnt = hip->alloc_blocks;
|
2005-04-17 06:20:36 +08:00
|
|
|
if (blk_cnt == alloc_cnt)
|
2014-04-04 05:50:34 +08:00
|
|
|
goto out_unlock;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-07-06 06:29:59 +08:00
|
|
|
res = hfs_find_init(HFSPLUS_SB(sb)->ext_tree, &fd);
|
|
|
|
if (res) {
|
|
|
|
mutex_unlock(&hip->extents_lock);
|
|
|
|
/* XXX: We lack error handling of hfsplus_file_truncate() */
|
|
|
|
return;
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
while (1) {
|
2010-10-01 11:43:31 +08:00
|
|
|
if (alloc_cnt == hip->first_blocks) {
|
2018-08-22 12:59:16 +08:00
|
|
|
mutex_unlock(&fd.tree->tree_lock);
|
2010-10-01 11:43:31 +08:00
|
|
|
hfsplus_free_extents(sb, hip->first_extents,
|
2005-04-17 06:20:36 +08:00
|
|
|
alloc_cnt, alloc_cnt - blk_cnt);
|
2010-10-01 11:43:31 +08:00
|
|
|
hfsplus_dump_extent(hip->first_extents);
|
|
|
|
hip->first_blocks = blk_cnt;
|
2018-08-22 12:59:16 +08:00
|
|
|
mutex_lock(&fd.tree->tree_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
res = __hfsplus_ext_cache_extent(&fd, inode, alloc_cnt);
|
|
|
|
if (res)
|
|
|
|
break;
|
2018-08-22 12:59:16 +08:00
|
|
|
|
2010-10-01 11:43:31 +08:00
|
|
|
start = hip->cached_start;
|
hfsplus: prevent corruption in shrinking truncate
I believe there are some issues introduced by commit 31651c607151
("hfsplus: avoid deadlock on file truncation")
HFS+ has extent records which always contains 8 extents. In case the
first extent record in catalog file gets full, new ones are allocated from
extents overflow file.
In case shrinking truncate happens to middle of an extent record which
locates in extents overflow file, the logic in hfsplus_file_truncate() was
changed so that call to hfs_brec_remove() is not guarded any more.
Right action would be just freeing the extents that exceed the new size
inside extent record by calling hfsplus_free_extents(), and then check if
the whole extent record should be removed. However since the guard
(blk_cnt > start) is now after the call to hfs_brec_remove(), this has
unfortunate effect that the last matching extent record is removed
unconditionally.
To reproduce this issue, create a file which has at least 10 extents, and
then perform shrinking truncate into middle of the last extent record, so
that the number of remaining extents is not under or divisible by 8. This
causes the last extent record (8 extents) to be removed totally instead of
truncating into middle of it. Thus this causes corruption, and lost data.
Fix for this is simply checking if the new truncated end is below the
start of this extent record, making it safe to remove the full extent
record. However call to hfs_brec_remove() can't be moved to it's previous
place since we're dropping ->tree_lock and it can cause a race condition
and the cached info being invalidated possibly corrupting the node data.
Another issue is related to this one. When entering into the block
(blk_cnt > start) we are not holding the ->tree_lock. We break out from
the loop not holding the lock, but hfs_find_exit() does unlock it. Not
sure if it's possible for someone else to take the lock under our feet,
but it can cause hard to debug errors and premature unlocking. Even if
there's no real risk of it, the locking should still always be kept in
balance. Thus taking the lock now just before the check.
Link: https://lkml.kernel.org/r/20210429165139.3082828-1-jouni.roivas@tuxera.com
Fixes: 31651c607151f ("hfsplus: avoid deadlock on file truncation")
Signed-off-by: Jouni Roivas <jouni.roivas@tuxera.com>
Reviewed-by: Anton Altaparmakov <anton@tuxera.com>
Cc: Anatoly Trosinenko <anatoly.trosinenko@gmail.com>
Cc: Viacheslav Dubeyko <slava@dubeyko.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2021-05-15 08:27:33 +08:00
|
|
|
if (blk_cnt <= start)
|
|
|
|
hfs_brec_remove(&fd);
|
|
|
|
mutex_unlock(&fd.tree->tree_lock);
|
2010-10-01 11:43:31 +08:00
|
|
|
hfsplus_free_extents(sb, hip->cached_extents,
|
2005-04-17 06:20:36 +08:00
|
|
|
alloc_cnt - start, alloc_cnt - blk_cnt);
|
2010-10-01 11:43:31 +08:00
|
|
|
hfsplus_dump_extent(hip->cached_extents);
|
hfsplus: prevent corruption in shrinking truncate
I believe there are some issues introduced by commit 31651c607151
("hfsplus: avoid deadlock on file truncation")
HFS+ has extent records which always contains 8 extents. In case the
first extent record in catalog file gets full, new ones are allocated from
extents overflow file.
In case shrinking truncate happens to middle of an extent record which
locates in extents overflow file, the logic in hfsplus_file_truncate() was
changed so that call to hfs_brec_remove() is not guarded any more.
Right action would be just freeing the extents that exceed the new size
inside extent record by calling hfsplus_free_extents(), and then check if
the whole extent record should be removed. However since the guard
(blk_cnt > start) is now after the call to hfs_brec_remove(), this has
unfortunate effect that the last matching extent record is removed
unconditionally.
To reproduce this issue, create a file which has at least 10 extents, and
then perform shrinking truncate into middle of the last extent record, so
that the number of remaining extents is not under or divisible by 8. This
causes the last extent record (8 extents) to be removed totally instead of
truncating into middle of it. Thus this causes corruption, and lost data.
Fix for this is simply checking if the new truncated end is below the
start of this extent record, making it safe to remove the full extent
record. However call to hfs_brec_remove() can't be moved to it's previous
place since we're dropping ->tree_lock and it can cause a race condition
and the cached info being invalidated possibly corrupting the node data.
Another issue is related to this one. When entering into the block
(blk_cnt > start) we are not holding the ->tree_lock. We break out from
the loop not holding the lock, but hfs_find_exit() does unlock it. Not
sure if it's possible for someone else to take the lock under our feet,
but it can cause hard to debug errors and premature unlocking. Even if
there's no real risk of it, the locking should still always be kept in
balance. Thus taking the lock now just before the check.
Link: https://lkml.kernel.org/r/20210429165139.3082828-1-jouni.roivas@tuxera.com
Fixes: 31651c607151f ("hfsplus: avoid deadlock on file truncation")
Signed-off-by: Jouni Roivas <jouni.roivas@tuxera.com>
Reviewed-by: Anton Altaparmakov <anton@tuxera.com>
Cc: Anatoly Trosinenko <anatoly.trosinenko@gmail.com>
Cc: Viacheslav Dubeyko <slava@dubeyko.com>
Cc: <stable@vger.kernel.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2021-05-15 08:27:33 +08:00
|
|
|
mutex_lock(&fd.tree->tree_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
if (blk_cnt > start) {
|
2010-11-23 21:38:13 +08:00
|
|
|
hip->extent_state |= HFSPLUS_EXT_DIRTY;
|
2005-04-17 06:20:36 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
alloc_cnt = start;
|
2010-10-01 11:43:31 +08:00
|
|
|
hip->cached_start = hip->cached_blocks = 0;
|
2010-11-23 21:38:13 +08:00
|
|
|
hip->extent_state &= ~(HFSPLUS_EXT_DIRTY | HFSPLUS_EXT_NEW);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
hfs_find_exit(&fd);
|
|
|
|
|
2010-10-01 11:43:31 +08:00
|
|
|
hip->alloc_blocks = blk_cnt;
|
2014-04-04 05:50:34 +08:00
|
|
|
out_unlock:
|
|
|
|
mutex_unlock(&hip->extents_lock);
|
2010-10-01 11:43:31 +08:00
|
|
|
hip->phys_size = inode->i_size;
|
2010-12-17 00:08:38 +08:00
|
|
|
hip->fs_blocks = (inode->i_size + sb->s_blocksize - 1) >>
|
|
|
|
sb->s_blocksize_bits;
|
2010-10-01 11:43:31 +08:00
|
|
|
inode_set_bytes(inode, hip->fs_blocks << sb->s_blocksize_bits);
|
2010-11-23 21:38:15 +08:00
|
|
|
hfsplus_mark_inode_dirty(inode, HFSPLUS_I_ALLOC_DIRTY);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|