2005-04-16 16:20:36 -06:00
|
|
|
/*
|
|
|
|
* linux/fs/hfsplus/bitmap.c
|
|
|
|
*
|
|
|
|
* Copyright (C) 2001
|
|
|
|
* Brad Boyer (flar@allandria.com)
|
|
|
|
* (C) 2003 Ardis Technologies <roman@ardistech.com>
|
|
|
|
*
|
|
|
|
* Handling of allocation file
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/pagemap.h>
|
|
|
|
|
|
|
|
#include "hfsplus_fs.h"
|
|
|
|
#include "hfsplus_raw.h"
|
|
|
|
|
mm, fs: get rid of PAGE_CACHE_* and page_cache_{get,release} macros
PAGE_CACHE_{SIZE,SHIFT,MASK,ALIGN} macros were introduced *long* time
ago with promise that one day it will be possible to implement page
cache with bigger chunks than PAGE_SIZE.
This promise never materialized. And unlikely will.
We have many places where PAGE_CACHE_SIZE assumed to be equal to
PAGE_SIZE. And it's constant source of confusion on whether
PAGE_CACHE_* or PAGE_* constant should be used in a particular case,
especially on the border between fs and mm.
Global switching to PAGE_CACHE_SIZE != PAGE_SIZE would cause to much
breakage to be doable.
Let's stop pretending that pages in page cache are special. They are
not.
The changes are pretty straight-forward:
- <foo> << (PAGE_CACHE_SHIFT - PAGE_SHIFT) -> <foo>;
- <foo> >> (PAGE_CACHE_SHIFT - PAGE_SHIFT) -> <foo>;
- PAGE_CACHE_{SIZE,SHIFT,MASK,ALIGN} -> PAGE_{SIZE,SHIFT,MASK,ALIGN};
- page_cache_get() -> get_page();
- page_cache_release() -> put_page();
This patch contains automated changes generated with coccinelle using
script below. For some reason, coccinelle doesn't patch header files.
I've called spatch for them manually.
The only adjustment after coccinelle is revert of changes to
PAGE_CAHCE_ALIGN definition: we are going to drop it later.
There are few places in the code where coccinelle didn't reach. I'll
fix them manually in a separate patch. Comments and documentation also
will be addressed with the separate patch.
virtual patch
@@
expression E;
@@
- E << (PAGE_CACHE_SHIFT - PAGE_SHIFT)
+ E
@@
expression E;
@@
- E >> (PAGE_CACHE_SHIFT - PAGE_SHIFT)
+ E
@@
@@
- PAGE_CACHE_SHIFT
+ PAGE_SHIFT
@@
@@
- PAGE_CACHE_SIZE
+ PAGE_SIZE
@@
@@
- PAGE_CACHE_MASK
+ PAGE_MASK
@@
expression E;
@@
- PAGE_CACHE_ALIGN(E)
+ PAGE_ALIGN(E)
@@
expression E;
@@
- page_cache_get(E)
+ get_page(E)
@@
expression E;
@@
- page_cache_release(E)
+ put_page(E)
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
Acked-by: Michal Hocko <mhocko@suse.com>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
2016-04-01 06:29:47 -06:00
|
|
|
#define PAGE_CACHE_BITS (PAGE_SIZE * 8)
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2010-12-16 09:08:38 -07:00
|
|
|
int hfsplus_block_allocate(struct super_block *sb, u32 size,
|
|
|
|
u32 offset, u32 *max)
|
2005-04-16 16:20:36 -06:00
|
|
|
{
|
2010-09-30 21:42:59 -06:00
|
|
|
struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
|
2005-04-16 16:20:36 -06:00
|
|
|
struct page *page;
|
|
|
|
struct address_space *mapping;
|
|
|
|
__be32 *pptr, *curr, *end;
|
|
|
|
u32 mask, start, len, n;
|
|
|
|
__be32 val;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
len = *max;
|
|
|
|
if (!len)
|
|
|
|
return size;
|
|
|
|
|
2013-04-30 16:27:54 -06:00
|
|
|
hfs_dbg(BITMAP, "block_allocate: %u,%u,%u\n", size, offset, len);
|
2010-09-30 21:42:59 -06:00
|
|
|
mutex_lock(&sbi->alloc_mutex);
|
|
|
|
mapping = sbi->alloc_file->i_mapping;
|
2006-06-23 03:05:08 -06:00
|
|
|
page = read_mapping_page(mapping, offset / PAGE_CACHE_BITS, NULL);
|
2008-10-15 23:04:10 -06:00
|
|
|
if (IS_ERR(page)) {
|
|
|
|
start = size;
|
|
|
|
goto out;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
pptr = kmap(page);
|
|
|
|
curr = pptr + (offset & (PAGE_CACHE_BITS - 1)) / 32;
|
|
|
|
i = offset % 32;
|
|
|
|
offset &= ~(PAGE_CACHE_BITS - 1);
|
|
|
|
if ((size ^ offset) / PAGE_CACHE_BITS)
|
|
|
|
end = pptr + PAGE_CACHE_BITS / 32;
|
|
|
|
else
|
|
|
|
end = pptr + ((size + 31) & (PAGE_CACHE_BITS - 1)) / 32;
|
|
|
|
|
|
|
|
/* scan the first partial u32 for zero bits */
|
|
|
|
val = *curr;
|
|
|
|
if (~val) {
|
|
|
|
n = be32_to_cpu(val);
|
|
|
|
mask = (1U << 31) >> i;
|
|
|
|
for (; i < 32; mask >>= 1, i++) {
|
|
|
|
if (!(n & mask))
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curr++;
|
|
|
|
|
|
|
|
/* scan complete u32s for the first zero bit */
|
|
|
|
while (1) {
|
|
|
|
while (curr < end) {
|
|
|
|
val = *curr;
|
|
|
|
if (~val) {
|
|
|
|
n = be32_to_cpu(val);
|
|
|
|
mask = 1 << 31;
|
|
|
|
for (i = 0; i < 32; mask >>= 1, i++) {
|
|
|
|
if (!(n & mask))
|
|
|
|
goto found;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
curr++;
|
|
|
|
}
|
|
|
|
kunmap(page);
|
|
|
|
offset += PAGE_CACHE_BITS;
|
|
|
|
if (offset >= size)
|
|
|
|
break;
|
2006-06-23 03:05:08 -06:00
|
|
|
page = read_mapping_page(mapping, offset / PAGE_CACHE_BITS,
|
|
|
|
NULL);
|
2008-10-15 23:04:10 -06:00
|
|
|
if (IS_ERR(page)) {
|
|
|
|
start = size;
|
|
|
|
goto out;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
curr = pptr = kmap(page);
|
|
|
|
if ((size ^ offset) / PAGE_CACHE_BITS)
|
|
|
|
end = pptr + PAGE_CACHE_BITS / 32;
|
|
|
|
else
|
|
|
|
end = pptr + ((size + 31) & (PAGE_CACHE_BITS - 1)) / 32;
|
|
|
|
}
|
2013-04-30 16:27:54 -06:00
|
|
|
hfs_dbg(BITMAP, "bitmap full\n");
|
2005-04-16 16:20:36 -06:00
|
|
|
start = size;
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
found:
|
|
|
|
start = offset + (curr - pptr) * 32 + i;
|
|
|
|
if (start >= size) {
|
2013-04-30 16:27:54 -06:00
|
|
|
hfs_dbg(BITMAP, "bitmap full\n");
|
2005-04-16 16:20:36 -06:00
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
/* do any partial u32 at the start */
|
|
|
|
len = min(size - start, len);
|
|
|
|
while (1) {
|
|
|
|
n |= mask;
|
|
|
|
if (++i >= 32)
|
|
|
|
break;
|
|
|
|
mask >>= 1;
|
|
|
|
if (!--len || n & mask)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
if (!--len)
|
|
|
|
goto done;
|
|
|
|
*curr++ = cpu_to_be32(n);
|
|
|
|
/* do full u32s */
|
|
|
|
while (1) {
|
|
|
|
while (curr < end) {
|
|
|
|
n = be32_to_cpu(*curr);
|
|
|
|
if (len < 32)
|
|
|
|
goto last;
|
|
|
|
if (n) {
|
|
|
|
len = 32;
|
|
|
|
goto last;
|
|
|
|
}
|
|
|
|
*curr++ = cpu_to_be32(0xffffffff);
|
|
|
|
len -= 32;
|
|
|
|
}
|
|
|
|
set_page_dirty(page);
|
|
|
|
kunmap(page);
|
|
|
|
offset += PAGE_CACHE_BITS;
|
2006-06-23 03:05:08 -06:00
|
|
|
page = read_mapping_page(mapping, offset / PAGE_CACHE_BITS,
|
|
|
|
NULL);
|
2008-10-15 23:04:10 -06:00
|
|
|
if (IS_ERR(page)) {
|
|
|
|
start = size;
|
|
|
|
goto out;
|
|
|
|
}
|
2005-04-16 16:20:36 -06:00
|
|
|
pptr = kmap(page);
|
|
|
|
curr = pptr;
|
|
|
|
end = pptr + PAGE_CACHE_BITS / 32;
|
|
|
|
}
|
|
|
|
last:
|
|
|
|
/* do any partial u32 at end */
|
|
|
|
mask = 1U << 31;
|
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
if (n & mask)
|
|
|
|
break;
|
|
|
|
n |= mask;
|
|
|
|
mask >>= 1;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
*curr = cpu_to_be32(n);
|
|
|
|
set_page_dirty(page);
|
|
|
|
kunmap(page);
|
|
|
|
*max = offset + (curr - pptr) * 32 + i - start;
|
2010-09-30 21:42:59 -06:00
|
|
|
sbi->free_blocks -= *max;
|
2012-07-12 08:26:31 -06:00
|
|
|
hfsplus_mark_mdb_dirty(sb);
|
2013-04-30 16:27:54 -06:00
|
|
|
hfs_dbg(BITMAP, "-> %u,%u\n", start, *max);
|
2005-04-16 16:20:36 -06:00
|
|
|
out:
|
2010-09-30 21:42:59 -06:00
|
|
|
mutex_unlock(&sbi->alloc_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
return start;
|
|
|
|
}
|
|
|
|
|
|
|
|
int hfsplus_block_free(struct super_block *sb, u32 offset, u32 count)
|
|
|
|
{
|
2010-09-30 21:42:59 -06:00
|
|
|
struct hfsplus_sb_info *sbi = HFSPLUS_SB(sb);
|
2005-04-16 16:20:36 -06:00
|
|
|
struct page *page;
|
|
|
|
struct address_space *mapping;
|
|
|
|
__be32 *pptr, *curr, *end;
|
|
|
|
u32 mask, len, pnr;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
/* is there any actual work to be done? */
|
|
|
|
if (!count)
|
|
|
|
return 0;
|
|
|
|
|
2013-04-30 16:27:54 -06:00
|
|
|
hfs_dbg(BITMAP, "block_free: %u,%u\n", offset, count);
|
2005-04-16 16:20:36 -06:00
|
|
|
/* are all of the bits in range? */
|
2010-09-30 21:42:59 -06:00
|
|
|
if ((offset + count) > sbi->total_blocks)
|
2012-12-20 16:05:24 -07:00
|
|
|
return -ENOENT;
|
2005-04-16 16:20:36 -06:00
|
|
|
|
2010-09-30 21:42:59 -06:00
|
|
|
mutex_lock(&sbi->alloc_mutex);
|
|
|
|
mapping = sbi->alloc_file->i_mapping;
|
2005-04-16 16:20:36 -06:00
|
|
|
pnr = offset / PAGE_CACHE_BITS;
|
2006-06-23 03:05:08 -06:00
|
|
|
page = read_mapping_page(mapping, pnr, NULL);
|
2012-12-20 16:05:24 -07:00
|
|
|
if (IS_ERR(page))
|
|
|
|
goto kaboom;
|
2005-04-16 16:20:36 -06:00
|
|
|
pptr = kmap(page);
|
|
|
|
curr = pptr + (offset & (PAGE_CACHE_BITS - 1)) / 32;
|
|
|
|
end = pptr + PAGE_CACHE_BITS / 32;
|
|
|
|
len = count;
|
|
|
|
|
|
|
|
/* do any partial u32 at the start */
|
|
|
|
i = offset % 32;
|
|
|
|
if (i) {
|
|
|
|
int j = 32 - i;
|
|
|
|
mask = 0xffffffffU << j;
|
|
|
|
if (j > count) {
|
|
|
|
mask |= 0xffffffffU >> (i + count);
|
|
|
|
*curr++ &= cpu_to_be32(mask);
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
*curr++ &= cpu_to_be32(mask);
|
|
|
|
count -= j;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* do full u32s */
|
|
|
|
while (1) {
|
|
|
|
while (curr < end) {
|
|
|
|
if (count < 32)
|
|
|
|
goto done;
|
|
|
|
*curr++ = 0;
|
|
|
|
count -= 32;
|
|
|
|
}
|
|
|
|
if (!count)
|
|
|
|
break;
|
|
|
|
set_page_dirty(page);
|
|
|
|
kunmap(page);
|
2006-06-23 03:05:08 -06:00
|
|
|
page = read_mapping_page(mapping, ++pnr, NULL);
|
2012-12-20 16:05:24 -07:00
|
|
|
if (IS_ERR(page))
|
|
|
|
goto kaboom;
|
2005-04-16 16:20:36 -06:00
|
|
|
pptr = kmap(page);
|
|
|
|
curr = pptr;
|
|
|
|
end = pptr + PAGE_CACHE_BITS / 32;
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
/* do any partial u32 at end */
|
|
|
|
if (count) {
|
|
|
|
mask = 0xffffffffU >> count;
|
|
|
|
*curr &= cpu_to_be32(mask);
|
|
|
|
}
|
|
|
|
out:
|
|
|
|
set_page_dirty(page);
|
|
|
|
kunmap(page);
|
2010-09-30 21:42:59 -06:00
|
|
|
sbi->free_blocks += len;
|
2012-07-12 08:26:31 -06:00
|
|
|
hfsplus_mark_mdb_dirty(sb);
|
2010-09-30 21:42:59 -06:00
|
|
|
mutex_unlock(&sbi->alloc_mutex);
|
2005-04-16 16:20:36 -06:00
|
|
|
|
|
|
|
return 0;
|
2012-12-20 16:05:24 -07:00
|
|
|
|
|
|
|
kaboom:
|
2013-04-30 16:27:58 -06:00
|
|
|
pr_crit("unable to mark blocks free: error %ld\n", PTR_ERR(page));
|
2012-12-20 16:05:24 -07:00
|
|
|
mutex_unlock(&sbi->alloc_mutex);
|
|
|
|
|
|
|
|
return -EIO;
|
2005-04-16 16:20:36 -06:00
|
|
|
}
|