| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
#include <linux/config.h> |
|---|
| 22 |
#include <linux/kernel.h> |
|---|
| 23 |
#include <linux/fs.h> |
|---|
| 24 |
#include <linux/mm.h> |
|---|
| 25 |
#include <linux/percpu.h> |
|---|
| 26 |
#include <linux/slab.h> |
|---|
| 27 |
#include <linux/smp_lock.h> |
|---|
| 28 |
#include <linux/blkdev.h> |
|---|
| 29 |
#include <linux/file.h> |
|---|
| 30 |
#include <linux/quotaops.h> |
|---|
| 31 |
#include <linux/highmem.h> |
|---|
| 32 |
#include <linux/module.h> |
|---|
| 33 |
#include <linux/writeback.h> |
|---|
| 34 |
#include <linux/hash.h> |
|---|
| 35 |
#include <linux/suspend.h> |
|---|
| 36 |
#include <linux/buffer_head.h> |
|---|
| 37 |
#include <linux/bio.h> |
|---|
| 38 |
#include <linux/notifier.h> |
|---|
| 39 |
#include <linux/cpu.h> |
|---|
| 40 |
#include <asm/bitops.h> |
|---|
| 41 |
|
|---|
| 42 |
static void invalidate_bh_lrus(void); |
|---|
| 43 |
|
|---|
| 44 |
#define BH_ENTRY(list) list_entry((list), struct buffer_head, b_assoc_buffers) |
|---|
| 45 |
|
|---|
| 46 |
struct bh_wait_queue { |
|---|
| 47 |
struct buffer_head *bh; |
|---|
| 48 |
wait_queue_t wait; |
|---|
| 49 |
}; |
|---|
| 50 |
|
|---|
| 51 |
#define __DEFINE_BH_WAIT(name, b, f) \ |
|---|
| 52 |
struct bh_wait_queue name = { \ |
|---|
| 53 |
.bh = b, \ |
|---|
| 54 |
.wait = { \ |
|---|
| 55 |
.task = current, \ |
|---|
| 56 |
.flags = f, \ |
|---|
| 57 |
.func = bh_wake_function, \ |
|---|
| 58 |
.task_list = \ |
|---|
| 59 |
LIST_HEAD_INIT(name.wait.task_list),\ |
|---|
| 60 |
}, \ |
|---|
| 61 |
} |
|---|
| 62 |
#define DEFINE_BH_WAIT(name, bh) __DEFINE_BH_WAIT(name, bh, 0) |
|---|
| 63 |
#define DEFINE_BH_WAIT_EXCLUSIVE(name, bh) \ |
|---|
| 64 |
__DEFINE_BH_WAIT(name, bh, WQ_FLAG_EXCLUSIVE) |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
#define BH_WAIT_TABLE_ORDER 7 |
|---|
| 70 |
static struct bh_wait_queue_head { |
|---|
| 71 |
wait_queue_head_t wqh; |
|---|
| 72 |
} ____cacheline_aligned_in_smp bh_wait_queue_heads[1<<BH_WAIT_TABLE_ORDER]; |
|---|
| 73 |
|
|---|
| 74 |
inline void |
|---|
| 75 |
init_buffer(struct buffer_head *bh, bh_end_io_t *handler, void *private) |
|---|
| 76 |
{ |
|---|
| 77 |
bh->b_end_io = handler; |
|---|
| 78 |
bh->b_private = private; |
|---|
| 79 |
} |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
wait_queue_head_t *bh_waitq_head(struct buffer_head *bh) |
|---|
| 86 |
{ |
|---|
| 87 |
return &bh_wait_queue_heads[hash_ptr(bh, BH_WAIT_TABLE_ORDER)].wqh; |
|---|
| 88 |
} |
|---|
| 89 |
EXPORT_SYMBOL(bh_waitq_head); |
|---|
| 90 |
|
|---|
| 91 |
void wake_up_buffer(struct buffer_head *bh) |
|---|
| 92 |
{ |
|---|
| 93 |
wait_queue_head_t *wq = bh_waitq_head(bh); |
|---|
| 94 |
|
|---|
| 95 |
smp_mb(); |
|---|
| 96 |
if (waitqueue_active(wq)) |
|---|
| 97 |
__wake_up(wq, TASK_INTERRUPTIBLE|TASK_UNINTERRUPTIBLE, 1, bh); |
|---|
| 98 |
} |
|---|
| 99 |
EXPORT_SYMBOL(wake_up_buffer); |
|---|
| 100 |
|
|---|
| 101 |
static int bh_wake_function(wait_queue_t *wait, unsigned mode, |
|---|
| 102 |
int sync, void *key) |
|---|
| 103 |
{ |
|---|
| 104 |
struct buffer_head *bh = key; |
|---|
| 105 |
struct bh_wait_queue *wq; |
|---|
| 106 |
|
|---|
| 107 |
wq = container_of(wait, struct bh_wait_queue, wait); |
|---|
| 108 |
if (wq->bh != bh || buffer_locked(bh)) |
|---|
| 109 |
return 0; |
|---|
| 110 |
else |
|---|
| 111 |
return autoremove_wake_function(wait, mode, sync, key); |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
static void sync_buffer(struct buffer_head *bh) |
|---|
| 115 |
{ |
|---|
| 116 |
struct block_device *bd; |
|---|
| 117 |
|
|---|
| 118 |
smp_mb(); |
|---|
| 119 |
bd = bh->b_bdev; |
|---|
| 120 |
if (bd) |
|---|
| 121 |
blk_run_address_space(bd->bd_inode->i_mapping); |
|---|
| 122 |
} |
|---|
| 123 |
|
|---|
| 124 |
void fastcall __lock_buffer(struct buffer_head *bh) |
|---|
| 125 |
{ |
|---|
| 126 |
wait_queue_head_t *wqh = bh_waitq_head(bh); |
|---|
| 127 |
DEFINE_BH_WAIT_EXCLUSIVE(wait, bh); |
|---|
| 128 |
|
|---|
| 129 |
do { |
|---|
| 130 |
prepare_to_wait_exclusive(wqh, &wait.wait, |
|---|
| 131 |
TASK_UNINTERRUPTIBLE); |
|---|
| 132 |
if (buffer_locked(bh)) { |
|---|
| 133 |
sync_buffer(bh); |
|---|
| 134 |
io_schedule(); |
|---|
| 135 |
} |
|---|
| 136 |
} while (test_set_buffer_locked(bh)); |
|---|
| 137 |
finish_wait(wqh, &wait.wait); |
|---|
| 138 |
} |
|---|
| 139 |
EXPORT_SYMBOL(__lock_buffer); |
|---|
| 140 |
|
|---|
| 141 |
void fastcall unlock_buffer(struct buffer_head *bh) |
|---|
| 142 |
{ |
|---|
| 143 |
clear_buffer_locked(bh); |
|---|
| 144 |
smp_mb__after_clear_bit(); |
|---|
| 145 |
wake_up_buffer(bh); |
|---|
| 146 |
} |
|---|
| 147 |
|
|---|
| 148 |
|
|---|
| 149 |
|
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
void __wait_on_buffer(struct buffer_head * bh) |
|---|
| 154 |
{ |
|---|
| 155 |
wait_queue_head_t *wqh = bh_waitq_head(bh); |
|---|
| 156 |
DEFINE_BH_WAIT(wait, bh); |
|---|
| 157 |
|
|---|
| 158 |
do { |
|---|
| 159 |
prepare_to_wait(wqh, &wait.wait, TASK_UNINTERRUPTIBLE); |
|---|
| 160 |
if (buffer_locked(bh)) { |
|---|
| 161 |
sync_buffer(bh); |
|---|
| 162 |
io_schedule(); |
|---|
| 163 |
} |
|---|
| 164 |
} while (buffer_locked(bh)); |
|---|
| 165 |
finish_wait(wqh, &wait.wait); |
|---|
| 166 |
} |
|---|
| 167 |
|
|---|
| 168 |
static void |
|---|
| 169 |
__set_page_buffers(struct page *page, struct buffer_head *head) |
|---|
| 170 |
{ |
|---|
| 171 |
page_cache_get(page); |
|---|
| 172 |
SetPagePrivate(page); |
|---|
| 173 |
page->private = (unsigned long)head; |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
static void |
|---|
| 177 |
__clear_page_buffers(struct page *page) |
|---|
| 178 |
{ |
|---|
| 179 |
ClearPagePrivate(page); |
|---|
| 180 |
page->private = 0; |
|---|
| 181 |
page_cache_release(page); |
|---|
| 182 |
} |
|---|
| 183 |
|
|---|
| 184 |
static void buffer_io_error(struct buffer_head *bh) |
|---|
| 185 |
{ |
|---|
| 186 |
char b[BDEVNAME_SIZE]; |
|---|
| 187 |
|
|---|
| 188 |
printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n", |
|---|
| 189 |
bdevname(bh->b_bdev, b), |
|---|
| 190 |
(unsigned long long)bh->b_blocknr); |
|---|
| 191 |
} |
|---|
| 192 |
|
|---|
| 193 |
|
|---|
| 194 |
|
|---|
| 195 |
|
|---|
| 196 |
|
|---|
| 197 |
void end_buffer_read_sync(struct buffer_head *bh, int uptodate) |
|---|
| 198 |
{ |
|---|
| 199 |
if (uptodate) { |
|---|
| 200 |
set_buffer_uptodate(bh); |
|---|
| 201 |
} else { |
|---|
| 202 |
|
|---|
| 203 |
clear_buffer_uptodate(bh); |
|---|
| 204 |
} |
|---|
| 205 |
unlock_buffer(bh); |
|---|
| 206 |
put_bh(bh); |
|---|
| 207 |
} |
|---|
| 208 |
|
|---|
| 209 |
void end_buffer_write_sync(struct buffer_head *bh, int uptodate) |
|---|
| 210 |
{ |
|---|
| 211 |
char b[BDEVNAME_SIZE]; |
|---|
| 212 |
|
|---|
| 213 |
if (uptodate) { |
|---|
| 214 |
set_buffer_uptodate(bh); |
|---|
| 215 |
} else { |
|---|
| 216 |
if (printk_ratelimit()) { |
|---|
| 217 |
buffer_io_error(bh); |
|---|
| 218 |
printk(KERN_WARNING "lost page write due to " |
|---|
| 219 |
"I/O error on %s\n", |
|---|
| 220 |
bdevname(bh->b_bdev, b)); |
|---|
| 221 |
} |
|---|
| 222 |
set_buffer_write_io_error(bh); |
|---|
| 223 |
clear_buffer_uptodate(bh); |
|---|
| 224 |
} |
|---|
| 225 |
unlock_buffer(bh); |
|---|
| 226 |
put_bh(bh); |
|---|
| 227 |
} |
|---|
| 228 |
|
|---|
| 229 |
|
|---|
| 230 |
|
|---|
| 231 |
|
|---|
| 232 |
|
|---|
| 233 |
int sync_blockdev(struct block_device *bdev) |
|---|
| 234 |
{ |
|---|
| 235 |
int ret = 0; |
|---|
| 236 |
|
|---|
| 237 |
if (bdev) { |
|---|
| 238 |
int err; |
|---|
| 239 |
|
|---|
| 240 |
ret = filemap_fdatawrite(bdev->bd_inode->i_mapping); |
|---|
| 241 |
err = filemap_fdatawait(bdev->bd_inode->i_mapping); |
|---|
| 242 |
if (!ret) |
|---|
| 243 |
ret = err; |
|---|
| 244 |
} |
|---|
| 245 |
return ret; |
|---|
| 246 |
} |
|---|
| 247 |
EXPORT_SYMBOL(sync_blockdev); |
|---|
| 248 |
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 |
|
|---|
| 252 |
|
|---|
| 253 |
|
|---|
| 254 |
int fsync_super(struct super_block *sb) |
|---|
| 255 |
{ |
|---|
| 256 |
sync_inodes_sb(sb, 0); |
|---|
| 257 |
DQUOT_SYNC(sb); |
|---|
| 258 |
lock_super(sb); |
|---|
| 259 |
if (sb->s_dirt && sb->s_op->write_super) |
|---|
| 260 |
sb->s_op->write_super(sb); |
|---|
| 261 |
unlock_super(sb); |
|---|
| 262 |
if (sb->s_op->sync_fs) |
|---|
| 263 |
sb->s_op->sync_fs(sb, 1); |
|---|
| 264 |
sync_blockdev(sb->s_bdev); |
|---|
| 265 |
sync_inodes_sb(sb, 1); |
|---|
| 266 |
|
|---|
| 267 |
return sync_blockdev(sb->s_bdev); |
|---|
| 268 |
} |
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
|
|---|
| 274 |
|
|---|
| 275 |
int fsync_bdev(struct block_device *bdev) |
|---|
| 276 |
{ |
|---|
| 277 |
struct super_block *sb = get_super(bdev); |
|---|
| 278 |
if (sb) { |
|---|
| 279 |
int res = fsync_super(sb); |
|---|
| 280 |
drop_super(sb); |
|---|
| 281 |
return res; |
|---|
| 282 |
} |
|---|
| 283 |
return sync_blockdev(bdev); |
|---|
| 284 |
} |
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
|
|---|
| 288 |
|
|---|
| 289 |
|
|---|
| 290 |
|
|---|
| 291 |
|
|---|
| 292 |
|
|---|
| 293 |
|
|---|
| 294 |
|
|---|
| 295 |
struct super_block *freeze_bdev(struct block_device *bdev) |
|---|
| 296 |
{ |
|---|
| 297 |
struct super_block *sb; |
|---|
| 298 |
|
|---|
| 299 |
down(&bdev->bd_mount_sem); |
|---|
| 300 |
sb = get_super(bdev); |
|---|
| 301 |
if (sb && !(sb->s_flags & MS_RDONLY)) { |
|---|
| 302 |
sb->s_frozen = SB_FREEZE_WRITE; |
|---|
| 303 |
wmb(); |
|---|
| 304 |
|
|---|
| 305 |
sync_inodes_sb(sb, 0); |
|---|
| 306 |
DQUOT_SYNC(sb); |
|---|
| 307 |
|
|---|
| 308 |
lock_super(sb); |
|---|
| 309 |
if (sb->s_dirt && sb->s_op->write_super) |
|---|
| 310 |
sb->s_op->write_super(sb); |
|---|
| 311 |
unlock_super(sb); |
|---|
| 312 |
|
|---|
| 313 |
if (sb->s_op->sync_fs) |
|---|
| 314 |
sb->s_op->sync_fs(sb, 1); |
|---|
| 315 |
|
|---|
| 316 |
sync_blockdev(sb->s_bdev); |
|---|
| 317 |
sync_inodes_sb(sb, 1); |
|---|
| 318 |
|
|---|
| 319 |
sb->s_frozen = SB_FREEZE_TRANS; |
|---|
| 320 |
wmb(); |
|---|
| 321 |
|
|---|
| 322 |
sync_blockdev(sb->s_bdev); |
|---|
| 323 |
|
|---|
| 324 |
if (sb->s_op->write_super_lockfs) |
|---|
| 325 |
sb->s_op->write_super_lockfs(sb); |
|---|
| 326 |
} |
|---|
| 327 |
|
|---|
| 328 |
sync_blockdev(bdev); |
|---|
| 329 |
return sb; |
|---|
| 330 |
} |
|---|
| 331 |
EXPORT_SYMBOL(freeze_bdev); |
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 |
|
|---|
| 337 |
|
|---|
| 338 |
|
|---|
| 339 |
|
|---|
| 340 |
void thaw_bdev(struct block_device *bdev, struct super_block *sb) |
|---|
| 341 |
{ |
|---|
| 342 |
if (sb) { |
|---|
| 343 |
BUG_ON(sb->s_bdev != bdev); |
|---|
| 344 |
|
|---|
| 345 |
if (sb->s_op->unlockfs) |
|---|
| 346 |
sb->s_op->unlockfs(sb); |
|---|
| 347 |
sb->s_frozen = SB_UNFROZEN; |
|---|
| 348 |
wmb(); |
|---|
| 349 |
wake_up(&sb->s_wait_unfrozen); |
|---|
| 350 |
drop_super(sb); |
|---|
| 351 |
} |
|---|
| 352 |
|
|---|
| 353 |
up(&bdev->bd_mount_sem); |
|---|
| 354 |
} |
|---|
| 355 |
EXPORT_SYMBOL(thaw_bdev); |
|---|
| 356 |
|
|---|
| 357 |
|
|---|
| 358 |
|
|---|
| 359 |
|
|---|
| 360 |
|
|---|
| 361 |
static void do_sync(unsigned long wait) |
|---|
| 362 |
{ |
|---|
| 363 |
wakeup_bdflush(0); |
|---|
| 364 |
sync_inodes(0); |
|---|
| 365 |
DQUOT_SYNC(NULL); |
|---|
| 366 |
sync_supers(); |
|---|
| 367 |
sync_filesystems(0); |
|---|
| 368 |
sync_filesystems(wait); |
|---|
| 369 |
sync_inodes(wait); |
|---|
| 370 |
if (!wait) |
|---|
| 371 |
printk("Emergency Sync complete\n"); |
|---|
| 372 |
if (unlikely(laptop_mode)) |
|---|
| 373 |
laptop_sync_completion(); |
|---|
| 374 |
} |
|---|
| 375 |
|
|---|
| 376 |
asmlinkage long sys_sync(void) |
|---|
| 377 |
{ |
|---|
| 378 |
do_sync(1); |
|---|
| 379 |
return 0; |
|---|
| 380 |
} |
|---|
| 381 |
|
|---|
| 382 |
void emergency_sync(void) |
|---|
| 383 |
{ |
|---|
| 384 |
pdflush_operation(do_sync, 0); |
|---|
| 385 |
} |
|---|
| 386 |
|
|---|
| 387 |
|
|---|
| 388 |
|
|---|
| 389 |
|
|---|
| 390 |
|
|---|
| 391 |
|
|---|
| 392 |
|
|---|
| 393 |
int file_fsync(struct file *filp, struct dentry *dentry, int datasync) |
|---|
| 394 |
{ |
|---|
| 395 |
struct inode * inode = dentry->d_inode; |
|---|
| 396 |
struct super_block * sb; |
|---|
| 397 |
int ret; |
|---|
| 398 |
|
|---|
| 399 |
|
|---|
| 400 |
write_inode_now(inode, 0); |
|---|
| 401 |
|
|---|
| 402 |
|
|---|
| 403 |
sb = inode->i_sb; |
|---|
| 404 |
lock_super(sb); |
|---|
| 405 |
if (sb->s_op->write_super) |
|---|
| 406 |
sb->s_op->write_super(sb); |
|---|
| 407 |
unlock_super(sb); |
|---|
| 408 |
|
|---|
| 409 |
|
|---|
| 410 |
ret = sync_blockdev(sb->s_bdev); |
|---|
| 411 |
return ret; |
|---|
| 412 |
} |
|---|
| 413 |
|
|---|
| 414 |
asmlinkage long sys_fsync(unsigned int fd) |
|---|
| 415 |
{ |
|---|
| 416 |
struct file * file; |
|---|
| 417 |
struct address_space *mapping; |
|---|
| 418 |
int ret, err; |
|---|
| 419 |
|
|---|
| 420 |
ret = -EBADF; |
|---|
| 421 |
file = fget(fd); |
|---|
| 422 |
if (!file) |
|---|
| 423 |
goto out; |
|---|
| 424 |
|
|---|
| 425 |
mapping = file->f_mapping; |
|---|
| 426 |
|
|---|
| 427 |
ret = -EINVAL; |
|---|
| 428 |
if (!file->f_op || !file->f_op->fsync) { |
|---|
| 429 |
|
|---|
| 430 |
goto out_putf; |
|---|
| 431 |
} |
|---|
| 432 |
|
|---|
| 433 |
|
|---|
| 434 |
down(&mapping->host->i_sem); |
|---|
| 435 |
current->flags |= PF_SYNCWRITE; |
|---|
| 436 |
ret = filemap_fdatawrite(mapping); |
|---|
| 437 |
err = file->f_op->fsync(file, file->f_dentry, 0); |
|---|
| 438 |
if (!ret) |
|---|
| 439 |
ret = err; |
|---|
| 440 |
err = filemap_fdatawait(mapping); |
|---|
| 441 |
if (!ret) |
|---|
| 442 |
ret = err; |
|---|
| 443 |
current->flags &= ~PF_SYNCWRITE; |
|---|
| 444 |
up(&mapping->host->i_sem); |
|---|
| 445 |
|
|---|
| 446 |
out_putf: |
|---|
| 447 |
fput(file); |
|---|
| 448 |
out: |
|---|
| 449 |
return ret; |
|---|
| 450 |
} |
|---|
| 451 |
|
|---|
| 452 |
asmlinkage long sys_fdatasync(unsigned int fd) |
|---|
| 453 |
{ |
|---|
| 454 |
struct file * file; |
|---|
| 455 |
struct address_space *mapping; |
|---|
| 456 |
int ret, err; |
|---|
| 457 |
|
|---|
| 458 |
ret = -EBADF; |
|---|
| 459 |
file = fget(fd); |
|---|
| 460 |
if (!file) |
|---|
| 461 |
goto out; |
|---|
| 462 |
|
|---|
| 463 |
ret = -EINVAL; |
|---|
| 464 |
if (!file->f_op || !file->f_op->fsync) |
|---|
| 465 |
goto out_putf; |
|---|
| 466 |
|
|---|
| 467 |
mapping = file->f_mapping; |
|---|
| 468 |
|
|---|
| 469 |
down(&mapping->host->i_sem); |
|---|
| 470 |
current->flags |= PF_SYNCWRITE; |
|---|
| 471 |
ret = filemap_fdatawrite(mapping); |
|---|
| 472 |
err = file->f_op->fsync(file, file->f_dentry, 1); |
|---|
| 473 |
if (!ret) |
|---|
| 474 |
ret = err; |
|---|
| 475 |
err = filemap_fdatawait(mapping); |
|---|
| 476 |
if (!ret) |
|---|
| 477 |
ret = err; |
|---|
| 478 |
current->flags &= ~PF_SYNCWRITE; |
|---|
| 479 |
up(&mapping->host->i_sem); |
|---|
| 480 |
|
|---|
| 481 |
out_putf: |
|---|
| 482 |
fput(file); |
|---|
| 483 |
out: |
|---|
| 484 |
return ret; |
|---|
| 485 |
} |
|---|
| 486 |
|
|---|
| 487 |
|
|---|
| 488 |
|
|---|
| 489 |
|
|---|
| 490 |
|
|---|
| 491 |
|
|---|
| 492 |
|
|---|
| 493 |
|
|---|
| 494 |
|
|---|
| 495 |
|
|---|
| 496 |
|
|---|
| 497 |
|
|---|
| 498 |
static struct buffer_head * |
|---|
| 499 |
__find_get_block_slow(struct block_device *bdev, sector_t block, int unused) |
|---|
| 500 |
{ |
|---|
| 501 |
struct inode *bd_inode = bdev->bd_inode; |
|---|
| 502 |
struct address_space *bd_mapping = bd_inode->i_mapping; |
|---|
| 503 |
struct buffer_head *ret = NULL; |
|---|
| 504 |
pgoff_t index; |
|---|
| 505 |
struct buffer_head *bh; |
|---|
| 506 |
struct buffer_head *head; |
|---|
| 507 |
struct page *page; |
|---|
| 508 |
|
|---|
| 509 |
index = block >> (PAGE_CACHE_SHIFT - bd_inode->i_blkbits); |
|---|
| 510 |
page = find_get_page(bd_mapping, index); |
|---|
| 511 |
if (!page) |
|---|
| 512 |
goto out; |
|---|
| 513 |
|
|---|
| 514 |
spin_lock(&bd_mapping->private_lock); |
|---|
| 515 |
if (!page_has_buffers(page)) |
|---|
| 516 |
goto out_unlock; |
|---|
| 517 |
head = page_buffers(page); |
|---|
| 518 |
bh = head; |
|---|
| 519 |
do { |
|---|
| 520 |
if (bh->b_blocknr == block) { |
|---|
| 521 |
ret = bh; |
|---|
| 522 |
get_bh(bh); |
|---|
| 523 |
goto out_unlock; |
|---|
| 524 |
} |
|---|
| 525 |
bh = bh->b_this_page; |
|---|
| 526 |
} while (bh != head); |
|---|
| 527 |
|
|---|
| 528 |
printk("__find_get_block_slow() failed. " |
|---|
| 529 |
"block=%llu, b_blocknr=%llu\n", |
|---|
| 530 |
(unsigned long long)block, (unsigned long long)bh->b_blocknr); |
|---|
| 531 |
printk("b_state=0x%08lx, b_size=%u\n", bh->b_state, bh->b_size); |
|---|
| 532 |
printk("device blocksize: %d\n", 1 << bd_inode->i_blkbits); |
|---|
| 533 |
out_unlock: |
|---|
| 534 |
spin_unlock(&bd_mapping->private_lock); |
|---|
| 535 |
page_cache_release(page); |
|---|
| 536 |
out: |
|---|
| 537 |
return ret; |
|---|
| 538 |
} |
|---|
| 539 |
|
|---|
| 540 |
|
|---|
| 541 |
|
|---|
| 542 |
|
|---|
| 543 |
|
|---|
| 544 |
|
|---|
| 545 |
|
|---|
| 546 |
|
|---|
| 547 |
|
|---|
| 548 |
|
|---|
| 549 |
|
|---|
| 550 |
|
|---|
| 551 |
|
|---|
| 552 |
|
|---|
| 553 |
|
|---|
| 554 |
|
|---|
| 555 |
|
|---|
| 556 |
|
|---|
| 557 |
|
|---|
| 558 |
|
|---|
| 559 |
|
|---|
| 560 |
|
|---|
| 561 |
|
|---|
| 562 |
|
|---|
| 563 |
|
|---|
| 564 |
|
|---|
| 565 |
|
|---|
| 566 |
|
|---|
| 567 |
|
|---|
| 568 |
|
|---|
| 569 |
|
|---|
| 570 |
|
|---|
| 571 |
|
|---|
| 572 |
void invalidate_bdev(struct block_device *bdev, int destroy_dirty_buffers) |
|---|
| 573 |
{ |
|---|
| 574 |
invalidate_bh_lrus(); |
|---|
| 575 |
|
|---|
| 576 |
|
|---|
| 577 |
|
|---|
| 578 |
|
|---|
| 579 |
|
|---|
| 580 |
invalidate_inode_pages(bdev->bd_inode->i_mapping); |
|---|
| 581 |
} |
|---|
| 582 |
|
|---|
| 583 |
|
|---|
| 584 |
|
|---|
| 585 |
|
|---|
| 586 |
static void free_more_memory(void) |
|---|
| 587 |
{ |
|---|
| 588 |
struct zone **zones; |
|---|
| 589 |
pg_data_t *pgdat; |
|---|
| 590 |
|
|---|
| 591 |
wakeup_bdflush(1024); |
|---|
| 592 |
yield(); |
|---|
| 593 |
|
|---|
| 594 |
for_each_pgdat(pgdat) { |
|---|
| 595 |
zones = pgdat->node_zonelists[GFP_NOFS&GFP_ZONEMASK].zones; |
|---|
| 596 |
if (*zones) |
|---|
| 597 |
try_to_free_pages(zones, GFP_NOFS, 0); |
|---|
| 598 |
} |
|---|
| 599 |
} |
|---|
| 600 |
|
|---|
| 601 |
|
|---|
| 602 |
|
|---|
| 603 |
|
|---|
| 604 |
|
|---|
| 605 |
static void end_buffer_async_read(struct buffer_head *bh, int uptodate) |
|---|
| 606 |
{ |
|---|
| 607 |
static spinlock_t page_uptodate_lock = SPIN_LOCK_UNLOCKED; |
|---|
| 608 |
unsigned long flags; |
|---|
| 609 |
struct buffer_head *tmp; |
|---|
| 610 |
struct page *page; |
|---|
| 611 |
int page_uptodate = 1; |
|---|
| 612 |
|
|---|
| 613 |
BUG_ON(!buffer_async_read(bh)); |
|---|
| 614 |
|
|---|
| 615 |
page = bh->b_page; |
|---|
| 616 |
if (uptodate) { |
|---|
| 617 |
set_buffer_uptodate(bh); |
|---|
| 618 |
} else { |
|---|
| 619 |
clear_buffer_uptodate(bh); |
|---|
| 620 |
buffer_io_error(bh); |
|---|
| 621 |
SetPageError(page); |
|---|
| 622 |
} |
|---|
| 623 |
|
|---|
| 624 |
|
|---|
| 625 |
|
|---|
| 626 |
|
|---|
| 627 |
|
|---|
| 628 |
|
|---|
| 629 |
spin_lock_irqsave(&page_uptodate_lock, flags); |
|---|
| 630 |
clear_buffer_async_read(bh); |
|---|
| 631 |
unlock_buffer(bh); |
|---|
| 632 |
tmp = bh; |
|---|
| 633 |
do { |
|---|
| 634 |
if (!buffer_uptodate(tmp)) |
|---|
| 635 |
page_uptodate = 0; |
|---|
| 636 |
if (buffer_async_read(tmp)) { |
|---|
| 637 |
BUG_ON(!buffer_locked(tmp)); |
|---|
| 638 |
goto still_busy; |
|---|
| 639 |
} |
|---|
| 640 |
tmp = tmp->b_this_page; |
|---|
| 641 |
} while (tmp != bh); |
|---|
| 642 |
spin_unlock_irqrestore(&page_uptodate_lock, flags); |
|---|
| 643 |
|
|---|
| 644 |
|
|---|
| 645 |
|
|---|
| 646 |
|
|---|
| 647 |
|
|---|
| 648 |
if (page_uptodate && !PageError(page)) |
|---|
| 649 |
SetPageUptodate(page); |
|---|
| 650 |
unlock_page(page); |
|---|
| 651 |
return; |
|---|
| 652 |
|
|---|
| 653 |
still_busy: |
|---|
| 654 |
spin_unlock_irqrestore(&page_uptodate_lock, flags); |
|---|
| 655 |
return; |
|---|
| 656 |
} |
|---|
| 657 |
|
|---|
| 658 |
|
|---|
| 659 |
|
|---|
| 660 |
|
|---|
| 661 |
|
|---|
| 662 |
void end_buffer_async_write(struct buffer_head *bh, int uptodate) |
|---|
| 663 |
{ |
|---|
| 664 |
char b[BDEVNAME_SIZE]; |
|---|
| 665 |
static spinlock_t page_uptodate_lock = SPIN_LOCK_UNLOCKED; |
|---|
| 666 |
unsigned long flags; |
|---|
| 667 |
struct buffer_head *tmp; |
|---|
| 668 |
struct page *page; |
|---|
| 669 |
|
|---|
| 670 |
BUG_ON(!buffer_async_write(bh)); |
|---|
| 671 |
|
|---|
| 672 |
page = bh->b_page; |
|---|
| 673 |
if (uptodate) { |
|---|
| 674 |
set_buffer_uptodate(bh); |
|---|
| 675 |
} else { |
|---|
| 676 |
if (printk_ratelimit()) { |
|---|
| 677 |
buffer_io_error(bh); |
|---|
| 678 |
printk(KERN_WARNING "lost page write due to " |
|---|
| 679 |
"I/O error on %s\n", |
|---|
| 680 |
bdevname(bh->b_bdev, b)); |
|---|
| 681 |
} |
|---|
| 682 |
set_bit(AS_EIO, &page->mapping->flags); |
|---|
| 683 |
clear_buffer_uptodate(bh); |
|---|
| 684 |
SetPageError(page); |
|---|
| 685 |
} |
|---|
| 686 |
|
|---|
| 687 |
spin_lock_irqsave(&page_uptodate_lock, flags); |
|---|
| 688 |
clear_buffer_async_write(bh); |
|---|
| 689 |
unlock_buffer(bh); |
|---|
| 690 |
tmp = bh->b_this_page; |
|---|
| 691 |
while (tmp != bh) { |
|---|
| 692 |
if (buffer_async_write(tmp)) { |
|---|
| 693 |
BUG_ON(!buffer_locked(tmp)); |
|---|
| 694 |
goto still_busy; |
|---|
| 695 |
} |
|---|
| 696 |
tmp = tmp->b_this_page; |
|---|
| 697 |
} |
|---|
| 698 |
spin_unlock_irqrestore(&page_uptodate_lock, flags); |
|---|
| 699 |
end_page_writeback(page); |
|---|
| 700 |
return; |
|---|
| 701 |
|
|---|
| 702 |
still_busy: |
|---|
| 703 |
spin_unlock_irqrestore(&page_uptodate_lock, flags); |
|---|
| 704 |
return; |
|---|
| 705 |
} |
|---|
| 706 |
|
|---|
| 707 |
|
|---|
| 708 |
|
|---|
| 709 |
|
|---|
| 710 |
|
|---|
| 711 |
|
|---|
| 712 |
|
|---|
| 713 |
|
|---|
| 714 |
|
|---|
| 715 |
|
|---|
| 716 |
|
|---|
| 717 |
|
|---|
| 718 |
|
|---|
| 719 |
|
|---|
| 720 |
|
|---|
| 721 |
|
|---|
| 722 |
|
|---|
| 723 |
|
|---|
| 724 |
|
|---|
| 725 |
|
|---|
| 726 |
|
|---|
| 727 |
|
|---|
| 728 |
void mark_buffer_async_read(struct buffer_head *bh) |
|---|
| 729 |
{ |
|---|
| 730 |
bh->b_end_io = end_buffer_async_read; |
|---|
| 731 |
set_buffer_async_read(bh); |
|---|
| 732 |
} |
|---|
| 733 |
EXPORT_SYMBOL(mark_buffer_async_read); |
|---|
| 734 |
|
|---|
| 735 |
void mark_buffer_async_write(struct buffer_head *bh) |
|---|
| 736 |
{ |
|---|
| 737 |
bh->b_end_io = end_buffer_async_write; |
|---|
| 738 |
set_buffer_async_write(bh); |
|---|
| 739 |
} |
|---|
| 740 |
EXPORT_SYMBOL(mark_buffer_async_write); |
|---|
| 741 |
|
|---|
| 742 |
|
|---|
| 743 |
|
|---|
| 744 |
|
|---|
| 745 |
|
|---|
| 746 |
|
|---|
| 747 |
|
|---|
| 748 |
|
|---|
| 749 |
|
|---|
| 750 |
|
|---|
| 751 |
|
|---|
| 752 |
|
|---|
| 753 |
|
|---|
| 754 |
|
|---|
| 755 |
|
|---|
| 756 |
|
|---|
| 757 |
|
|---|
| 758 |
|
|---|
| 759 |
|
|---|
| 760 |
|
|---|
| 761 |
|
|---|
| 762 |
|
|---|
| 763 |
|
|---|
| 764 |
|
|---|
| 765 |
|
|---|
| 766 |
|
|---|
| 767 |
|
|---|
| 768 |
|
|---|
| 769 |
|
|---|
| 770 |
|
|---|
| 771 |
|
|---|
| 772 |
|
|---|
| 773 |
|
|---|
| 774 |
|
|---|
| 775 |
|
|---|
| 776 |
|
|---|
| 777 |
|
|---|
| 778 |
|
|---|
| 779 |
|
|---|
| 780 |
|
|---|
| 781 |
|
|---|
| 782 |
|
|---|
| 783 |
|
|---|
| 784 |
|
|---|
| 785 |
|
|---|
| 786 |
|
|---|
| 787 |
|
|---|
| 788 |
|
|---|
| 789 |
|
|---|
| 790 |
|
|---|
| 791 |
|
|---|
| 792 |
void buffer_insert_list(spinlock_t *lock, |
|---|
| 793 |
struct buffer_head *bh, struct list_head *list) |
|---|
| 794 |
{ |
|---|
| 795 |
spin_lock(lock); |
|---|
| 796 |
list_move_tail(&bh->b_assoc_buffers, list); |
|---|
| 797 |
spin_unlock(lock); |
|---|
| 798 |
} |
|---|
| 799 |
|
|---|
| 800 |
|
|---|
| 801 |
|
|---|
| 802 |
|
|---|
| 803 |
static inline void __remove_assoc_queue(struct buffer_head *bh) |
|---|
| 804 |
{ |
|---|
| 805 |
list_del_init(&bh->b_assoc_buffers); |
|---|
| 806 |
} |
|---|
| 807 |
|
|---|
| 808 |
int inode_has_buffers(struct inode *inode) |
|---|
| 809 |
{ |
|---|
| 810 |
return !list_empty(&inode->i_data.private_list); |
|---|
| 811 |
} |
|---|
| 812 |
|
|---|
| 813 |
|
|---|
| 814 |
|
|---|
| 815 |
|
|---|
| 816 |
|
|---|
| 817 |
|
|---|
| 818 |
|
|---|
| 819 |
|
|---|
| 820 |
|
|---|
| 821 |
|
|---|
| 822 |
|
|---|
| 823 |
static int osync_buffers_list(spinlock_t *lock, struct list_head *list) |
|---|
| 824 |
{ |
|---|
| 825 |
struct buffer_head *bh; |
|---|
| 826 |
struct list_head *p; |
|---|
| 827 |
int err = 0; |
|---|
| 828 |
|
|---|
| 829 |
spin_lock(lock); |
|---|
| 830 |
repeat: |
|---|
| 831 |
list_for_each_prev(p, list) { |
|---|
| 832 |
bh = BH_ENTRY(p); |
|---|
| 833 |
if (buffer_locked(bh)) { |
|---|
| 834 |
get_bh(bh); |
|---|
| 835 |
spin_unlock(lock); |
|---|
| 836 |
wait_on_buffer(bh); |
|---|
| 837 |
if (!buffer_uptodate(bh)) |
|---|
| 838 |
err = -EIO; |
|---|
| 839 |
brelse(bh); |
|---|
| 840 |
spin_lock(lock); |
|---|
| 841 |
goto repeat; |
|---|
| 842 |
} |
|---|
| 843 |
} |
|---|
| 844 |
spin_unlock(lock); |
|---|
| 845 |
return err; |
|---|
| 846 |
} |
|---|
| 847 |
|
|---|
| 848 |
|
|---|
| 849 |
|
|---|
| 850 |
|
|---|
| 851 |
|
|---|
| 852 |
|
|---|
| 853 |
|
|---|
| 854 |
|
|---|
| 855 |
|
|---|
| 856 |
|
|---|
| 857 |
|
|---|
| 858 |
|
|---|
| 859 |
|
|---|
| 860 |
|
|---|
| 861 |
int sync_mapping_buffers(struct address_space *mapping) |
|---|
| 862 |
{ |
|---|
| 863 |
struct address_space *buffer_mapping = mapping->assoc_mapping; |
|---|
| 864 |
|
|---|
| 865 |
if (buffer_mapping == NULL || list_empty(&mapping->private_list)) |
|---|
| 866 |
return 0; |
|---|
| 867 |
|
|---|
| 868 |
return fsync_buffers_list(&buffer_mapping->private_lock, |
|---|
| 869 |
&mapping->private_list); |
|---|
| 870 |
} |
|---|
| 871 |
EXPORT_SYMBOL(sync_mapping_buffers); |
|---|
| 872 |
|
|---|
| 873 |
|
|---|
| 874 |
|
|---|
| 875 |
|
|---|
| 876 |
|
|---|
| 877 |
|
|---|
| 878 |
|
|---|
| 879 |
void write_boundary_block(struct block_device *bdev, |
|---|
| 880 |
sector_t bblock, unsigned blocksize) |
|---|
| 881 |
{ |
|---|
| 882 |
struct buffer_head *bh = __find_get_block(bdev, bblock + 1, blocksize); |
|---|
| 883 |
if (bh) { |
|---|
| 884 |
if (buffer_dirty(bh)) |
|---|
| 885 |
ll_rw_block(WRITE, 1, &bh); |
|---|
| 886 |
put_bh(bh); |
|---|
| 887 |
} |
|---|
| 888 |
} |
|---|
| 889 |
|
|---|
| 890 |
void mark_buffer_dirty_inode(struct buffer_head *bh, struct inode *inode) |
|---|
| 891 |
{ |
|---|
| 892 |
struct address_space *mapping = inode->i_mapping; |
|---|
| 893 |
struct address_space *buffer_mapping = bh->b_page->mapping; |
|---|
| 894 |
|
|---|
| 895 |
mark_buffer_dirty(bh); |
|---|
| 896 |
if (!mapping->assoc_mapping) { |
|---|
| 897 |
mapping->assoc_mapping = buffer_mapping; |
|---|
| 898 |
} else { |
|---|
| 899 |
if (mapping->assoc_mapping != buffer_mapping) |
|---|
| 900 |
BUG(); |
|---|
| 901 |
} |
|---|
| 902 |
if (list_empty(&bh->b_assoc_buffers)) |
|---|
| 903 |
buffer_insert_list(&buffer_mapping->private_lock, |
|---|
| 904 |
bh, &mapping->private_list); |
|---|
| 905 |
} |
|---|
| 906 |
EXPORT_SYMBOL(mark_buffer_dirty_inode); |
|---|
| 907 |
|
|---|
| 908 |
|
|---|
| 909 |
|
|---|
| 910 |
|
|---|
| 911 |
|
|---|
| 912 |
|
|---|
| 913 |
|
|---|
| 914 |
|
|---|
| 915 |
|
|---|
| 916 |
|
|---|
| 917 |
|
|---|
| 918 |
|
|---|
| 919 |
|
|---|
| 920 |
|
|---|
| 921 |
|
|---|
| 922 |
|
|---|
| 923 |
|
|---|
| 924 |
|
|---|
| 925 |
|
|---|
| 926 |
|
|---|
| 927 |
|
|---|
| 928 |
|
|---|
| 929 |
|
|---|
| 930 |
|
|---|
| 931 |
|
|---|
| 932 |
|
|---|
| 933 |
int __set_page_dirty_buffers(struct page *page) |
|---|
| 934 |
{ |
|---|
| 935 |
struct address_space * const mapping = page->mapping; |
|---|
| 936 |
|
|---|
| 937 |
spin_lock(&mapping->private_lock); |
|---|
| 938 |
if (page_has_buffers(page)) { |
|---|
| 939 |
struct buffer_head *head = page_buffers(page); |
|---|
| 940 |
struct buffer_head *bh = head; |
|---|
| 941 |
|
|---|
| 942 |
do { |
|---|
| 943 |
set_buffer_dirty(bh); |
|---|
| 944 |
bh = bh->b_this_page; |
|---|
| 945 |
} while (bh != head); |
|---|
| 946 |
} |
|---|
| 947 |
spin_unlock(&mapping->private_lock); |
|---|
| 948 |
|
|---|
| 949 |
if (!TestSetPageDirty(page)) { |
|---|
| 950 |
spin_lock_irq(&mapping->tree_lock); |
|---|
| 951 |
if (page->mapping) { |
|---|
| 952 |
if (!mapping->backing_dev_info->memory_backed) |
|---|
| 953 |
inc_page_state(nr_dirty); |
|---|
| 954 |
radix_tree_tag_set(&mapping->page_tree, |
|---|
| 955 |
page_index(page), |
|---|
| 956 |
PAGECACHE_TAG_DIRTY); |
|---|
| 957 |
} |
|---|
| 958 |
spin_unlock_irq(&mapping->tree_lock); |
|---|
| 959 |
__mark_inode_dirty(mapping->host, I_DIRTY_PAGES); |
|---|
| 960 |
} |
|---|
| 961 |
|
|---|
| 962 |
return 0; |
|---|
| 963 |
} |
|---|
| 964 |
EXPORT_SYMBOL(__set_page_dirty_buffers); |
|---|
| 965 |
|
|---|
| 966 |
|
|---|
| 967 |
|
|---|
| 968 |
|
|---|
| 969 |
|
|---|
| 970 |
|
|---|
| 971 |
|
|---|
| 972 |
|
|---|
| 973 |
|
|---|
| 974 |
|
|---|
| 975 |
|
|---|
| 976 |
|
|---|
| 977 |
|
|---|
| 978 |
|
|---|
| 979 |
|
|---|
| 980 |
|
|---|
| 981 |
|
|---|
| 982 |
|
|---|
| 983 |
|
|---|
| 984 |
|
|---|
| 985 |
int fsync_buffers_list(spinlock_t *lock, struct list_head *list) |
|---|
| 986 |
{ |
|---|
| 987 |
struct buffer_head *bh; |
|---|
| 988 |
struct list_head tmp; |
|---|
| 989 |
int err = 0, err2; |
|---|
| 990 |
|
|---|
| 991 |
INIT_LIST_HEAD(&tmp); |
|---|
| 992 |
|
|---|
| 993 |
spin_lock(lock); |
|---|
| 994 |
while (!list_empty(list)) { |
|---|
| 995 |
bh = BH_ENTRY(list->next); |
|---|
| 996 |
list_del_init(&bh->b_assoc_buffers); |
|---|
| 997 |
if (buffer_dirty(bh) || buffer_locked(bh)) { |
|---|
| 998 |
list_add(&bh->b_assoc_buffers, &tmp); |
|---|
| 999 |
if (buffer_dirty(bh)) { |
|---|
| 1000 |
get_bh(bh); |
|---|
| 1001 |
spin_unlock(lock); |
|---|
| 1002 |
|
|---|
| 1003 |
|
|---|
| 1004 |
|
|---|
| 1005 |
|
|---|
| 1006 |
|
|---|
| 1007 |
|
|---|
| 1008 |
wait_on_buffer(bh); |
|---|
| 1009 |
ll_rw_block(WRITE, 1, &bh); |
|---|
| 1010 |
brelse(bh); |
|---|
| 1011 |
spin_lock(lock); |
|---|
| 1012 |
} |
|---|
| 1013 |
} |
|---|
| 1014 |
} |
|---|
| 1015 |
|
|---|
| 1016 |
while (!list_empty(&tmp)) { |
|---|
| 1017 |
bh = BH_ENTRY(tmp.prev); |
|---|
| 1018 |
__remove_assoc_queue(bh); |
|---|
| 1019 |
get_bh(bh); |
|---|
| 1020 |
spin_unlock(lock); |
|---|
| 1021 |
wait_on_buffer(bh); |
|---|
| 1022 |
if (!buffer_uptodate(bh)) |
|---|
| 1023 |
err = -EIO; |
|---|
| 1024 |
brelse(bh); |
|---|
| 1025 |
spin_lock(lock); |
|---|
| 1026 |
} |
|---|
| 1027 |
|
|---|
| 1028 |
spin_unlock(lock); |
|---|
| 1029 |
err2 = osync_buffers_list(lock, list); |
|---|
| 1030 |
if (err) |
|---|
| 1031 |
return err; |
|---|
| 1032 |
else |
|---|
| 1033 |
return err2; |
|---|
| 1034 |
} |
|---|
| 1035 |
|
|---|
| 1036 |
|
|---|
| 1037 |
|
|---|
| 1038 |
|
|---|
| 1039 |
|
|---|
| 1040 |
|
|---|
| 1041 |
|
|---|
| 1042 |
|
|---|
| 1043 |
|
|---|
| 1044 |
|
|---|
| 1045 |
void invalidate_inode_buffers(struct inode *inode) |
|---|
| 1046 |
{ |
|---|
| 1047 |
if (inode_has_buffers(inode)) { |
|---|
| 1048 |
struct address_space *mapping = &inode->i_data; |
|---|
| 1049 |
struct list_head *list = &mapping->private_list; |
|---|
| 1050 |
struct address_space *buffer_mapping = mapping->assoc_mapping; |
|---|
| 1051 |
|
|---|
| 1052 |
spin_lock(&buffer_mapping->private_lock); |
|---|
| 1053 |
while (!list_empty(list)) |
|---|
| 1054 |
__remove_assoc_queue(BH_ENTRY(list->next)); |
|---|
| 1055 |
spin_unlock(&buffer_mapping->private_lock); |
|---|
| 1056 |
} |
|---|
| 1057 |
} |
|---|
| 1058 |
|
|---|
| 1059 |
|
|---|
| 1060 |
|
|---|
| 1061 |
|
|---|
| 1062 |
|
|---|
| 1063 |
|
|---|
| 1064 |
|
|---|
| 1065 |
int remove_inode_buffers(struct inode *inode) |
|---|
| 1066 |
{ |
|---|
| 1067 |
int ret = 1; |
|---|
| 1068 |
|
|---|
| 1069 |
if (inode_has_buffers(inode)) { |
|---|
| 1070 |
struct address_space *mapping = &inode->i_data; |
|---|
| 1071 |
struct list_head *list = &mapping->private_list; |
|---|
| 1072 |
struct address_space *buffer_mapping = mapping->assoc_mapping; |
|---|
| 1073 |
|
|---|
| 1074 |
spin_lock(&buffer_mapping->private_lock); |
|---|
| 1075 |
while (!list_empty(list)) { |
|---|
| 1076 |
struct buffer_head *bh = BH_ENTRY(list->next); |
|---|
| 1077 |
if (buffer_dirty(bh)) { |
|---|
| 1078 |
ret = 0; |
|---|
| 1079 |
break; |
|---|
| 1080 |
} |
|---|
| 1081 |
__remove_assoc_queue(bh); |
|---|
| 1082 |
} |
|---|
| 1083 |
spin_unlock(&buffer_mapping->private_lock); |
|---|
| 1084 |
} |
|---|
| 1085 |
return ret; |
|---|
| 1086 |
} |
|---|
| 1087 |
|
|---|
| 1088 |
|
|---|
| 1089 |
|
|---|
| 1090 |
|
|---|
| 1091 |
|
|---|
| 1092 |
|
|---|
| 1093 |
|
|---|
| 1094 |
|
|---|
| 1095 |
|
|---|
| 1096 |
|
|---|
| 1097 |
static struct buffer_head * |
|---|
| 1098 |
create_buffers(struct page * page, unsigned long size, int retry) |
|---|
| 1099 |
{ |
|---|
| 1100 |
struct buffer_head *bh, *head; |
|---|
| 1101 |
long offset; |
|---|
| 1102 |
|
|---|
| 1103 |
try_again: |
|---|
| 1104 |
head = NULL; |
|---|
| 1105 |
offset = PAGE_SIZE; |
|---|
| 1106 |
while ((offset -= size) >= 0) { |
|---|
| 1107 |
bh = alloc_buffer_head(GFP_NOFS); |
|---|
| 1108 |
if (!bh) |
|---|
| 1109 |
goto no_grow; |
|---|
| 1110 |
|
|---|
| 1111 |
bh->b_bdev = NULL; |
|---|
| 1112 |
bh->b_this_page = head; |
|---|
| 1113 |
bh->b_blocknr = -1; |
|---|
| 1114 |
head = bh; |
|---|
| 1115 |
|
|---|
| 1116 |
bh->b_state = 0; |
|---|
| 1117 |
atomic_set(&bh->b_count, 0); |
|---|
| 1118 |
bh->b_size = size; |
|---|
| 1119 |
|
|---|
| 1120 |
|
|---|
| 1121 |
set_bh_page(bh, page, offset); |
|---|
| 1122 |
|
|---|
| 1123 |
bh->b_end_io = NULL; |
|---|
| 1124 |
} |
|---|
| 1125 |
return head; |
|---|
| 1126 |
|
|---|
| 1127 |
|
|---|
| 1128 |
|
|---|
| 1129 |
no_grow: |
|---|
| 1130 |
if (head) { |
|---|
| 1131 |
do { |
|---|
| 1132 |
bh = head; |
|---|
| 1133 |
head = head->b_this_page; |
|---|
| 1134 |
free_buffer_head(bh); |
|---|
| 1135 |
} while (head); |
|---|
| 1136 |
} |
|---|
| 1137 |
|
|---|
| 1138 |
|
|---|
| 1139 |
|
|---|
| 1140 |
|
|---|
| 1141 |
|
|---|
| 1142 |
|
|---|
| 1143 |
|
|---|
| 1144 |
if (!retry) |
|---|
| 1145 |
return NULL; |
|---|
| 1146 |
|
|---|
| 1147 |
|
|---|
| 1148 |
|
|---|
| 1149 |
|
|---|
| 1150 |
|
|---|
| 1151 |
|
|---|
| 1152 |
|
|---|
| 1153 |
free_more_memory(); |
|---|
| 1154 |
goto try_again; |
|---|
| 1155 |
} |
|---|
| 1156 |
|
|---|
| 1157 |
static inline void |
|---|
| 1158 |
link_dev_buffers(struct page *page, struct buffer_head *head) |
|---|
| 1159 |
{ |
|---|
| 1160 |
struct buffer_head *bh, *tail; |
|---|
| 1161 |
|
|---|
| 1162 |
bh = head; |
|---|
| 1163 |
do { |
|---|
| 1164 |
tail = bh; |
|---|
| 1165 |
bh = bh->b_this_page; |
|---|
| 1166 |
} while (bh); |
|---|
| 1167 |
tail->b_this_page = head; |
|---|
| 1168 |
__set_page_buffers(page, head); |
|---|
| 1169 |
} |
|---|
| 1170 |
|
|---|
| 1171 |
|
|---|
| 1172 |
|
|---|
| 1173 |
|
|---|
| 1174 |
static void |
|---|
| 1175 |
init_page_buffers(struct page *page, struct block_device *bdev, |
|---|
| 1176 |
sector_t block, int size) |
|---|
| 1177 |
{ |
|---|
| 1178 |
struct buffer_head *head = page_buffers(page); |
|---|
| 1179 |
struct buffer_head *bh = head; |
|---|
| 1180 |
unsigned int b_state; |
|---|
| 1181 |
|
|---|
| 1182 |
b_state = 1 << BH_Mapped; |
|---|
| 1183 |
if (PageUptodate(page)) |
|---|
| 1184 |
b_state |= 1 << BH_Uptodate; |
|---|
| 1185 |
|
|---|
| 1186 |
do { |
|---|
| 1187 |
if (!(bh->b_state & (1 << BH_Mapped))) { |
|---|
| 1188 |
init_buffer(bh, NULL, NULL); |
|---|
| 1189 |
bh->b_bdev = bdev; |
|---|
| 1190 |
bh->b_blocknr = block; |
|---|
| 1191 |
bh->b_state = b_state; |
|---|
| 1192 |
} |
|---|
| 1193 |
block++; |
|---|
| 1194 |
bh = bh->b_this_page; |
|---|
| 1195 |
} while (bh != head); |
|---|
| 1196 |
} |
|---|
| 1197 |
|
|---|
| 1198 |
|
|---|
| 1199 |
|
|---|
| 1200 |
|
|---|
| 1201 |
|
|---|
| 1202 |
|
|---|
| 1203 |
static struct page * |
|---|
| 1204 |
grow_dev_page(struct block_device *bdev, sector_t block, |
|---|
| 1205 |
pgoff_t index, int size) |
|---|
| 1206 |
{ |
|---|
| 1207 |
struct inode *inode = bdev->bd_inode; |
|---|
| 1208 |
struct page *page; |
|---|
| 1209 |
struct buffer_head *bh; |
|---|
| 1210 |
|
|---|
| 1211 |
page = find_or_create_page(inode->i_mapping, index, GFP_NOFS); |
|---|
| 1212 |
if (!page) |
|---|
| 1213 |
return NULL; |
|---|
| 1214 |
|
|---|
| 1215 |
if (!PageLocked(page)) |
|---|
| 1216 |
BUG(); |
|---|
| 1217 |
|
|---|
| 1218 |
if (page_has_buffers(page)) { |
|---|
| 1219 |
bh = page_buffers(page); |
|---|
| 1220 |
if (bh->b_size == size) |
|---|
| 1221 |
return page; |
|---|
| 1222 |
if (!try_to_free_buffers(page)) |
|---|
| 1223 |
goto failed; |
|---|
| 1224 |
} |
|---|
| 1225 |
|
|---|
| 1226 |
|
|---|
| 1227 |
|
|---|
| 1228 |
|
|---|
| 1229 |
bh = create_buffers(page, size, 0); |
|---|
| 1230 |
if (!bh) |
|---|
| 1231 |
goto failed; |
|---|
| 1232 |
|
|---|
| 1233 |
|
|---|
| 1234 |
|
|---|
| 1235 |
|
|---|
| 1236 |
|
|---|
| 1237 |
|
|---|
| 1238 |
spin_lock(&inode->i_mapping->private_lock); |
|---|
| 1239 |
link_dev_buffers(page, bh); |
|---|
| 1240 |
init_page_buffers(page, bdev, block, size); |
|---|
| 1241 |
spin_unlock(&inode->i_mapping->private_lock); |
|---|
| 1242 |
return page; |
|---|
| 1243 |
|
|---|
| 1244 |
failed: |
|---|
| 1245 |
BUG(); |
|---|
| 1246 |
unlock_page(page); |
|---|
| 1247 |
page_cache_release(page); |
|---|
| 1248 |
return NULL; |
|---|
| 1249 |
} |
|---|
| 1250 |
|
|---|
| 1251 |
|
|---|
| 1252 |
|
|---|
| 1253 |
|
|---|
| 1254 |
|
|---|
| 1255 |
|
|---|
| 1256 |
|
|---|
| 1257 |
|
|---|
| 1258 |
|
|---|
| 1259 |
|
|---|
| 1260 |
static inline int |
|---|
| 1261 |
grow_buffers(struct block_device *bdev, sector_t block, int size) |
|---|
| 1262 |
{ |
|---|
| 1263 |
struct page *page; |
|---|
| 1264 |
pgoff_t index; |
|---|
| 1265 |
int sizebits; |
|---|
| 1266 |
|
|---|
| 1267 |
sizebits = -1; |
|---|
| 1268 |
do { |
|---|
| 1269 |
sizebits++; |
|---|
| 1270 |
} while ((size << sizebits) < PAGE_SIZE); |
|---|
| 1271 |
|
|---|
| 1272 |
index = block >> sizebits; |
|---|
| 1273 |
block = index << sizebits; |
|---|
| 1274 |
|
|---|
| 1275 |
|
|---|
| 1276 |
page = grow_dev_page(bdev, block, index, size); |
|---|
| 1277 |
if (!page) |
|---|
| 1278 |
return 0; |
|---|
| 1279 |
unlock_page(page); |
|---|
| 1280 |
page_cache_release(page); |
|---|
| 1281 |
return 1; |
|---|
| 1282 |
} |
|---|
| 1283 |
|
|---|
| 1284 |
struct buffer_head * |
|---|
| 1285 |
__getblk_slow(struct block_device *bdev, sector_t block, int size) |
|---|
| 1286 |
{ |
|---|
| 1287 |
|
|---|
| 1288 |
if (unlikely(size & (bdev_hardsect_size(bdev)-1) || |
|---|
| 1289 |
(size < 512 || size > PAGE_SIZE))) { |
|---|
| 1290 |
printk(KERN_ERR "getblk(): invalid block size %d requested\n", |
|---|
| 1291 |
size); |
|---|
| 1292 |
printk(KERN_ERR "hardsect size: %d\n", |
|---|
| 1293 |
bdev_hardsect_size(bdev)); |
|---|
| 1294 |
|
|---|
| 1295 |
dump_stack(); |
|---|
| 1296 |
return NULL; |
|---|
| 1297 |
} |
|---|
| 1298 |
|
|---|
| 1299 |
for (;;) { |
|---|
| 1300 |
struct buffer_head * bh; |
|---|
| 1301 |
|
|---|
| 1302 |
bh = __find_get_block(bdev, block, size); |
|---|
| 1303 |
if (bh) |
|---|
| 1304 |
return bh; |
|---|
| 1305 |
|
|---|
| 1306 |
if (!grow_buffers(bdev, block, size)) |
|---|
| 1307 |
free_more_memory(); |
|---|
| 1308 |
} |
|---|
| 1309 |
} |
|---|
| 1310 |
|
|---|
| 1311 |
|
|---|
| 1312 |
|
|---|
| 1313 |
|
|---|
| 1314 |
|
|---|
| 1315 |
|
|---|
| 1316 |
|
|---|
| 1317 |
|
|---|
| 1318 |
|
|---|
| 1319 |
|
|---|
| 1320 |
|
|---|
| 1321 |
|
|---|
| 1322 |
|
|---|
| 1323 |
|
|---|
| 1324 |
|
|---|
| 1325 |
|
|---|
| 1326 |
|
|---|
| 1327 |
|
|---|
| 1328 |
|
|---|
| 1329 |
|
|---|
| 1330 |
|
|---|
| 1331 |
|
|---|
| 1332 |
|
|---|
| 1333 |
|
|---|
| 1334 |
|
|---|
| 1335 |
|
|---|
| 1336 |
|
|---|
| 1337 |
|
|---|
| 1338 |
|
|---|
| 1339 |
|
|---|
| 1340 |
|
|---|
| 1341 |
|
|---|
| 1342 |
|
|---|
| 1343 |
|
|---|
| 1344 |
|
|---|
| 1345 |
void fastcall mark_buffer_dirty(struct buffer_head *bh) |
|---|
| 1346 |
{ |
|---|
| 1347 |
if (!buffer_dirty(bh) && !test_set_buffer_dirty(bh)) |
|---|
| 1348 |
__set_page_dirty_nobuffers(bh->b_page); |
|---|
| 1349 |
} |
|---|
| 1350 |
|
|---|
| 1351 |
|
|---|
| 1352 |
|
|---|
| 1353 |
|
|---|
| 1354 |
|
|---|
| 1355 |
|
|---|
| 1356 |
|
|---|
| 1357 |
|
|---|
| 1358 |
void __brelse(struct buffer_head * buf) |
|---|
| 1359 |
{ |
|---|
| 1360 |
if (atomic_read(&buf->b_count)) { |
|---|
| 1361 |
put_bh(buf); |
|---|
| 1362 |
return; |
|---|
| 1363 |
} |
|---|
| 1364 |
printk(KERN_ERR "VFS: brelse: Trying to free free buffer\n"); |
|---|
| 1365 |
WARN_ON(1); |
|---|
| 1366 |
} |
|---|
| 1367 |
|
|---|
| 1368 |
|
|---|
| 1369 |
|
|---|
| 1370 |
|
|---|
| 1371 |
|
|---|
| 1372 |
void __bforget(struct buffer_head *bh) |
|---|
| 1373 |
{ |
|---|
| 1374 |
clear_buffer_dirty(bh); |
|---|
| 1375 |
if (!list_empty(&bh->b_assoc_buffers)) { |
|---|
| 1376 |
struct address_space *buffer_mapping = bh->b_page->mapping; |
|---|
| 1377 |
|
|---|
| 1378 |
spin_lock(&buffer_mapping->private_lock); |
|---|
| 1379 |
list_del_init(&bh->b_assoc_buffers); |
|---|
| 1380 |
spin_unlock(&buffer_mapping->private_lock); |
|---|
| 1381 |
} |
|---|
| 1382 |
__brelse(bh); |
|---|
| 1383 |
} |
|---|
| 1384 |
|
|---|
| 1385 |
static struct buffer_head *__bread_slow(struct buffer_head *bh) |
|---|
| 1386 |
{ |
|---|
| 1387 |
lock_buffer(bh); |
|---|
| 1388 |
if (buffer_uptodate(bh)) { |
|---|
| 1389 |
unlock_buffer(bh); |
|---|
| 1390 |
return bh; |
|---|
| 1391 |
} else { |
|---|
| 1392 |
get_bh(bh); |
|---|
| 1393 |
bh->b_end_io = end_buffer_read_sync; |
|---|
| 1394 |
submit_bh(READ, bh); |
|---|
| 1395 |
wait_on_buffer(bh); |
|---|
| 1396 |
if (buffer_uptodate(bh)) |
|---|
| 1397 |
return bh; |
|---|
| 1398 |
} |
|---|
| 1399 |
brelse(bh); |
|---|
| 1400 |
return NULL; |
|---|
| 1401 |
} |
|---|
| 1402 |
|
|---|
| 1403 |
|
|---|
| 1404 |
|
|---|
| 1405 |
|
|---|
| 1406 |
|
|---|
| 1407 |
|
|---|
| 1408 |
|
|---|
| 1409 |
|
|---|
| 1410 |
|
|---|
| 1411 |
|
|---|
| 1412 |
|
|---|
| 1413 |
|
|---|
| 1414 |
|
|---|
| 1415 |
|
|---|
| 1416 |
|
|---|
| 1417 |
#define BH_LRU_SIZE 8 |
|---|
| 1418 |
|
|---|
| 1419 |
struct bh_lru { |
|---|
| 1420 |
struct buffer_head *bhs[BH_LRU_SIZE]; |
|---|
| 1421 |
}; |
|---|
| 1422 |
|
|---|
| 1423 |
static DEFINE_PER_CPU(struct bh_lru, bh_lrus) = {{0}}; |
|---|
| 1424 |
|
|---|
| 1425 |
#ifdef CONFIG_SMP |
|---|
| 1426 |
#define bh_lru_lock() local_irq_disable() |
|---|
| 1427 |
#define bh_lru_unlock() local_irq_enable() |
|---|
| 1428 |
#else |
|---|
| 1429 |
#define bh_lru_lock() preempt_disable() |
|---|
| 1430 |
#define bh_lru_unlock() preempt_enable() |
|---|
| 1431 |
#endif |
|---|
| 1432 |
|
|---|
| 1433 |
static inline void check_irqs_on(void) |
|---|
| 1434 |
{ |
|---|
| 1435 |
#ifdef irqs_disabled |
|---|
| 1436 |
BUG_ON(irqs_disabled()); |
|---|
| 1437 |
#endif |
|---|
| 1438 |
} |
|---|
| 1439 |
|
|---|
| 1440 |
|
|---|
| 1441 |
|
|---|
| 1442 |
|
|---|
| 1443 |
static void bh_lru_install(struct buffer_head *bh) |
|---|
| 1444 |
{ |
|---|
| 1445 |
struct buffer_head *evictee = NULL; |
|---|
| 1446 |
struct bh_lru *lru; |
|---|
| 1447 |
|
|---|
| 1448 |
check_irqs_on(); |
|---|
| 1449 |
bh_lru_lock(); |
|---|
| 1450 |
lru = &__get_cpu_var(bh_lrus); |
|---|
| 1451 |
if (lru->bhs[0] != bh) { |
|---|
| 1452 |
struct buffer_head *bhs[BH_LRU_SIZE]; |
|---|
| 1453 |
int in; |
|---|
| 1454 |
int out = 0; |
|---|
| 1455 |
|
|---|
| 1456 |
get_bh(bh); |
|---|
| 1457 |
bhs[out++] = bh; |
|---|
| 1458 |
for (in = 0; in < BH_LRU_SIZE; in++) { |
|---|
| 1459 |
struct buffer_head *bh2 = lru->bhs[in]; |
|---|
| 1460 |
|
|---|
| 1461 |
if (bh2 == bh) { |
|---|
| 1462 |
__brelse(bh2); |
|---|
| 1463 |
} else { |
|---|
| 1464 |
if (out >= BH_LRU_SIZE) { |
|---|
| 1465 |
BUG_ON(evictee != NULL); |
|---|
| 1466 |
evictee = bh2; |
|---|
| 1467 |
} else { |
|---|
| 1468 |
bhs[out++] = bh2; |
|---|
| 1469 |
} |
|---|
| 1470 |
} |
|---|
| 1471 |
} |
|---|
| 1472 |
while (out < BH_LRU_SIZE) |
|---|
| 1473 |
bhs[out++] = NULL; |
|---|
| 1474 |
memcpy(lru->bhs, bhs, sizeof(bhs)); |
|---|
| 1475 |
} |
|---|
| 1476 |
bh_lru_unlock(); |
|---|
| 1477 |
|
|---|
| 1478 |
if (evictee) |
|---|
| 1479 |
__brelse(evictee); |
|---|
| 1480 |
} |
|---|
| 1481 |
|
|---|
| 1482 |
|
|---|
| 1483 |
|
|---|
| 1484 |
|
|---|
| 1485 |
static inline struct buffer_head * |
|---|
| 1486 |
lookup_bh_lru(struct block_device *bdev, sector_t block, int size) |
|---|
| 1487 |
{ |
|---|
| 1488 |
struct buffer_head *ret = NULL; |
|---|
| 1489 |
struct bh_lru *lru; |
|---|
| 1490 |
int i; |
|---|
| 1491 |
|
|---|
| 1492 |
check_irqs_on(); |
|---|
| 1493 |
bh_lru_lock(); |
|---|
| 1494 |
lru = &__get_cpu_var(bh_lrus); |
|---|
| 1495 |
for (i = 0; i < BH_LRU_SIZE; i++) { |
|---|
| 1496 |
struct buffer_head *bh = lru->bhs[i]; |
|---|
| 1497 |
|
|---|
| 1498 |
if (bh && bh->b_bdev == bdev && |
|---|
| 1499 |
bh->b_blocknr == block && bh->b_size == size) { |
|---|
| 1500 |
if (i) { |
|---|
| 1501 |
while (i) { |
|---|
| 1502 |
lru->bhs[i] = lru->bhs[i - 1]; |
|---|
| 1503 |
i--; |
|---|
| 1504 |
} |
|---|
| 1505 |
lru->bhs[0] = bh; |
|---|
| 1506 |
} |
|---|
| 1507 |
get_bh(bh); |
|---|
| 1508 |
ret = bh; |
|---|
| 1509 |
break; |
|---|
| 1510 |
} |
|---|
| 1511 |
} |
|---|
| 1512 |
bh_lru_unlock(); |
|---|
| 1513 |
return ret; |
|---|
| 1514 |
} |
|---|
| 1515 |
|
|---|
| 1516 |
|
|---|
| 1517 |
|
|---|
| 1518 |
|
|---|
| 1519 |
|
|---|
| 1520 |
|
|---|
| 1521 |
struct buffer_head * |
|---|
| 1522 |
__find_get_block(struct block_device *bdev, sector_t block, int size) |
|---|
| 1523 |
{ |
|---|
| 1524 |
struct buffer_head *bh = lookup_bh_lru(bdev, block, size); |
|---|
| 1525 |
|
|---|
| 1526 |
if (bh == NULL) { |
|---|
| 1527 |
bh = __find_get_block_slow(bdev, block, size); |
|---|
| 1528 |
if (bh) |
|---|
| 1529 |
bh_lru_install(bh); |
|---|
| 1530 |
} |
|---|
| 1531 |
if (bh) |
|---|
| 1532 |
touch_buffer(bh); |
|---|
| 1533 |
return bh; |
|---|
| 1534 |
} |
|---|
| 1535 |
EXPORT_SYMBOL(__find_get_block); |
|---|
| 1536 |
|
|---|
| 1537 |
|
|---|
| 1538 |
|
|---|
| 1539 |
|
|---|
| 1540 |
|
|---|
| 1541 |
|
|---|
| 1542 |
|
|---|
| 1543 |
|
|---|
| 1544 |
|
|---|
| 1545 |
|
|---|
| 1546 |
|
|---|
| 1547 |
|
|---|
| 1548 |
|
|---|
| 1549 |
struct buffer_head * |
|---|
| 1550 |
__getblk(struct block_device *bdev, sector_t block, int size) |
|---|
| 1551 |
{ |
|---|
| 1552 |
struct buffer_head *bh = __find_get_block(bdev, block, size); |
|---|
| 1553 |
|
|---|
| 1554 |
if (bh == NULL) |
|---|
| 1555 |
bh = __getblk_slow(bdev, block, size); |
|---|
| 1556 |
return bh; |
|---|
| 1557 |
} |
|---|
| 1558 |
EXPORT_SYMBOL(__getblk); |
|---|
| 1559 |
|
|---|
| 1560 |
|
|---|
| 1561 |
|
|---|
| 1562 |
|
|---|
| 1563 |
void __breadahead(struct block_device *bdev, sector_t block, int size) |
|---|
| 1564 |
{ |
|---|
| 1565 |
struct buffer_head *bh = __getblk(bdev, block, size); |
|---|
| 1566 |
ll_rw_block(READA, 1, &bh); |
|---|
| 1567 |
brelse(bh); |
|---|
| 1568 |
} |
|---|
| 1569 |
EXPORT_SYMBOL(__breadahead); |
|---|
| 1570 |
|
|---|
| 1571 |
|
|---|
| 1572 |
|
|---|
| 1573 |
|
|---|
| 1574 |
|
|---|
| 1575 |
|
|---|
| 1576 |
|
|---|
| 1577 |
|
|---|
| 1578 |
|
|---|
| 1579 |
struct buffer_head * |
|---|
| 1580 |
__bread(struct block_device *bdev, sector_t block, int size) |
|---|
| 1581 |
{ |
|---|
| 1582 |
struct buffer_head *bh = __getblk(bdev, block, size); |
|---|
| 1583 |
|
|---|
| 1584 |
if (!buffer_uptodate(bh)) |
|---|
| 1585 |
bh = __bread_slow(bh); |
|---|
| 1586 |
return bh; |
|---|
| 1587 |
} |
|---|
| 1588 |
EXPORT_SYMBOL(__bread); |
|---|
| 1589 |
|
|---|
| 1590 |
|
|---|
| 1591 |
|
|---|
| 1592 |
|
|---|
| 1593 |
|
|---|
| 1594 |
|
|---|
| 1595 |
|
|---|
| 1596 |
static void invalidate_bh_lru(void *arg) |
|---|
| 1597 |
{ |
|---|
| 1598 |
struct bh_lru *b = &get_cpu_var(bh_lrus); |
|---|
| 1599 |
int i; |
|---|
| 1600 |
|
|---|
| 1601 |
for (i = 0; i < BH_LRU_SIZE; i++) { |
|---|
| 1602 |
brelse(b->bhs[i]); |
|---|
| 1603 |
b->bhs[i] = NULL; |
|---|
| 1604 |
} |
|---|
| 1605 |
put_cpu_var(bh_lrus); |
|---|
| 1606 |
} |
|---|
| 1607 |
|
|---|
| 1608 |
static void invalidate_bh_lrus(void) |
|---|
| 1609 |
{ |
|---|
| 1610 |
on_each_cpu(invalidate_bh_lru, NULL, 1, 1); |
|---|
| 1611 |
} |
|---|
| 1612 |
|
|---|
| 1613 |
void set_bh_page(struct buffer_head *bh, |
|---|
| 1614 |
struct page *page, unsigned long offset) |
|---|
| 1615 |
{ |
|---|
| 1616 |
bh->b_page = page; |
|---|
| 1617 |
if (offset >= PAGE_SIZE) |
|---|
| 1618 |
BUG(); |
|---|
| 1619 |
if (PageHighMem(page)) |
|---|
| 1620 |
|
|---|
| 1621 |
|
|---|
| 1622 |
|
|---|
| 1623 |
bh->b_data = (char *)(0 + offset); |
|---|
| 1624 |
else |
|---|
| 1625 |
bh->b_data = page_address(page) + offset; |
|---|
| 1626 |
} |
|---|
| 1627 |
EXPORT_SYMBOL(set_bh_page); |
|---|
| 1628 |
|
|---|
| 1629 |
|
|---|
| 1630 |
|
|---|
| 1631 |
|
|---|
| 1632 |
static inline void discard_buffer(struct buffer_head * bh) |
|---|
| 1633 |
{ |
|---|
| 1634 |
lock_buffer(bh); |
|---|
| 1635 |
clear_buffer_dirty(bh); |
|---|
| 1636 |
bh->b_bdev = NULL; |
|---|
| 1637 |
clear_buffer_mapped(bh); |
|---|
| 1638 |
clear_buffer_req(bh); |
|---|
| 1639 |
clear_buffer_new(bh); |
|---|
| 1640 |
clear_buffer_delay(bh); |
|---|
| 1641 |
unlock_buffer(bh); |
|---|
| 1642 |
} |
|---|
| 1643 |
|
|---|
| 1644 |
|
|---|
| 1645 |
|
|---|
| 1646 |
|
|---|
| 1647 |
|
|---|
| 1648 |
|
|---|
| 1649 |
|
|---|
| 1650 |
|
|---|
| 1651 |
|
|---|
| 1652 |
|
|---|
| 1653 |
|
|---|
| 1654 |
|
|---|
| 1655 |
|
|---|
| 1656 |
|
|---|
| 1657 |
|
|---|
| 1658 |
|
|---|
| 1659 |
int try_to_release_page(struct page *page, int gfp_mask) |
|---|
| 1660 |
{ |
|---|
| 1661 |
struct address_space * const mapping = page->mapping; |
|---|
| 1662 |
|
|---|
| 1663 |
BUG_ON(!PageLocked(page)); |
|---|
| 1664 |
if (PageWriteback(page)) |
|---|
| 1665 |
return 0; |
|---|
| 1666 |
|
|---|
| 1667 |
if (mapping && mapping->a_ops->releasepage) |
|---|
| 1668 |
return mapping->a_ops->releasepage(page, gfp_mask); |
|---|
| 1669 |
return try_to_free_buffers(page); |
|---|
| 1670 |
} |
|---|
| 1671 |
EXPORT_SYMBOL(try_to_release_page); |
|---|
| 1672 |
|
|---|
| 1673 |
|
|---|
| 1674 |
|
|---|
| 1675 |
|
|---|
| 1676 |
|
|---|
| 1677 |
|
|---|
| 1678 |
|
|---|
| 1679 |
|
|---|
| 1680 |
|
|---|
| 1681 |
|
|---|
| 1682 |
|
|---|
| 1683 |
|
|---|
| 1684 |
|
|---|
| 1685 |
|
|---|
| 1686 |
|
|---|
| 1687 |
|
|---|
| 1688 |
int block_invalidatepage(struct page *page, unsigned long offset) |
|---|
| 1689 |
{ |
|---|
| 1690 |
struct buffer_head *head, *bh, *next; |
|---|
| 1691 |
unsigned int curr_off = 0; |
|---|
| 1692 |
int ret = 1; |
|---|
| 1693 |
|
|---|
| 1694 |
BUG_ON(!PageLocked(page)); |
|---|
| 1695 |
if (!page_has_buffers(page)) |
|---|
| 1696 |
goto out; |
|---|
| 1697 |
|
|---|
| 1698 |
head = page_buffers(page); |
|---|
| 1699 |
bh = head; |
|---|
| 1700 |
do { |
|---|
| 1701 |
unsigned int next_off = curr_off + bh->b_size; |
|---|
| 1702 |
next = bh->b_this_page; |
|---|
| 1703 |
|
|---|
| 1704 |
|
|---|
| 1705 |
|
|---|
| 1706 |
|
|---|
| 1707 |
if (offset <= curr_off) |
|---|
| 1708 |
discard_buffer(bh); |
|---|
| 1709 |
curr_off = next_off; |
|---|
| 1710 |
bh = next; |
|---|
| 1711 |
} while (bh != head); |
|---|
| 1712 |
|
|---|
| 1713 |
|
|---|
| 1714 |
|
|---|
| 1715 |
|
|---|
| 1716 |
|
|---|
| 1717 |
|
|---|
| 1718 |
if (offset == 0) |
|---|
| 1719 |
ret = try_to_release_page(page, 0); |
|---|
| 1720 |
out: |
|---|
| 1721 |
return ret; |
|---|
| 1722 |
} |
|---|
| 1723 |
EXPORT_SYMBOL(block_invalidatepage); |
|---|
| 1724 |
|
|---|
| 1725 |
|
|---|
| 1726 |
|
|---|
| 1727 |
|
|---|
| 1728 |
|
|---|
| 1729 |
|
|---|
| 1730 |
void create_empty_buffers(struct page *page, |
|---|
| 1731 |
unsigned long blocksize, unsigned long b_state) |
|---|
| 1732 |
{ |
|---|
| 1733 |
struct buffer_head *bh, *head, *tail; |
|---|
| 1734 |
|
|---|
| 1735 |
head = create_buffers(page, blocksize, 1); |
|---|
| 1736 |
bh = head; |
|---|
| 1737 |
do { |
|---|
| 1738 |
bh->b_state |= b_state; |
|---|
| 1739 |
tail = bh; |
|---|
| 1740 |
bh = bh->b_this_page; |
|---|
| 1741 |
} while (bh); |
|---|
| 1742 |
tail->b_this_page = head; |
|---|
| 1743 |
|
|---|
| 1744 |
spin_lock(&page->mapping->private_lock); |
|---|
| 1745 |
if (PageUptodate(page) || PageDirty(page)) { |
|---|
| 1746 |
bh = head; |
|---|
| 1747 |
do { |
|---|
| 1748 |
if (PageDirty(page)) |
|---|
| 1749 |
set_buffer_dirty(bh); |
|---|
| 1750 |
if (PageUptodate(page)) |
|---|
| 1751 |
set_buffer_uptodate(bh); |
|---|
| 1752 |
bh = bh->b_this_page; |
|---|
| 1753 |
} while (bh != head); |
|---|
| 1754 |
} |
|---|
| 1755 |
__set_page_buffers(page, head); |
|---|
| 1756 |
spin_unlock(&page->mapping->private_lock); |
|---|
| 1757 |
} |
|---|
| 1758 |
EXPORT_SYMBOL(create_empty_buffers); |
|---|
| 1759 |
|
|---|
| 1760 |
|
|---|
| 1761 |
|
|---|
| 1762 |
|
|---|
| 1763 |
|
|---|
| 1764 |
|
|---|
| 1765 |
|
|---|
| 1766 |
|
|---|
| 1767 |
|
|---|
| 1768 |
|
|---|
| 1769 |
|
|---|
| 1770 |
|
|---|
| 1771 |
|
|---|
| 1772 |
|
|---|
| 1773 |
|
|---|
| 1774 |
|
|---|
| 1775 |
|
|---|
| 1776 |
void unmap_underlying_metadata(struct block_device *bdev, sector_t block) |
|---|
| 1777 |
{ |
|---|
| 1778 |
struct buffer_head *old_bh; |
|---|
| 1779 |
|
|---|
| 1780 |
old_bh = __find_get_block_slow(bdev, block, 0); |
|---|
| 1781 |
if (old_bh) { |
|---|
| 1782 |
clear_buffer_dirty(old_bh); |
|---|
| 1783 |
wait_on_buffer(old_bh); |
|---|
| 1784 |
clear_buffer_req(old_bh); |
|---|
| 1785 |
__brelse(old_bh); |
|---|
| 1786 |
} |
|---|
| 1787 |
} |
|---|
| 1788 |
EXPORT_SYMBOL(unmap_underlying_metadata); |
|---|
| 1789 |
|
|---|
| 1790 |
|
|---|
| 1791 |
|
|---|
| 1792 |
|
|---|
| 1793 |
|
|---|
| 1794 |
|
|---|
| 1795 |
|
|---|
| 1796 |
|
|---|
| 1797 |
|
|---|
| 1798 |
|
|---|
| 1799 |
|
|---|
| 1800 |
|
|---|
| 1801 |
|
|---|
| 1802 |
|
|---|
| 1803 |
|
|---|
| 1804 |
|
|---|
| 1805 |
|
|---|
| 1806 |
|
|---|
| 1807 |
|
|---|
| 1808 |
|
|---|
| 1809 |
|
|---|
| 1810 |
|
|---|
| 1811 |
|
|---|
| 1812 |
|
|---|
| 1813 |
|
|---|
| 1814 |
|
|---|
| 1815 |
static int __block_write_full_page(struct inode *inode, struct page *page, |
|---|
| 1816 |
get_block_t *get_block, struct writeback_control *wbc) |
|---|
| 1817 |
{ |
|---|
| 1818 |
int err; |
|---|
| 1819 |
sector_t block; |
|---|
| 1820 |
sector_t last_block; |
|---|
| 1821 |
struct buffer_head *bh, *head; |
|---|
| 1822 |
int nr_underway = 0; |
|---|
| 1823 |
|
|---|
| 1824 |
BUG_ON(!PageLocked(page)); |
|---|
| 1825 |
|
|---|
| 1826 |
last_block = (i_size_read(inode) - 1) >> inode->i_blkbits; |
|---|
| 1827 |
|
|---|
| 1828 |
if (!page_has_buffers(page)) { |
|---|
| 1829 |
create_empty_buffers(page, 1 << inode->i_blkbits, |
|---|
| 1830 |
(1 << BH_Dirty)|(1 << BH_Uptodate)); |
|---|
| 1831 |
} |
|---|
| 1832 |
|
|---|
| 1833 |
|
|---|
| 1834 |
|
|---|
| 1835 |
|
|---|
| 1836 |
|
|---|
| 1837 |
|
|---|
| 1838 |
|
|---|
| 1839 |
|
|---|
| 1840 |
|
|---|
| 1841 |
|
|---|
| 1842 |
|
|---|
| 1843 |
block = page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); |
|---|
| 1844 |
head = page_buffers(page); |
|---|
| 1845 |
bh = head; |
|---|
| 1846 |
|
|---|
| 1847 |
|
|---|
| 1848 |
|
|---|
| 1849 |
|
|---|
| 1850 |
|
|---|
| 1851 |
do { |
|---|
| 1852 |
if (block > last_block) { |
|---|
| 1853 |
|
|---|
| 1854 |
|
|---|
| 1855 |
|
|---|
| 1856 |
|
|---|
| 1857 |
|
|---|
| 1858 |
|
|---|
| 1859 |
|
|---|
| 1860 |
|
|---|
| 1861 |
clear_buffer_dirty(bh); |
|---|
| 1862 |
set_buffer_uptodate(bh); |
|---|
| 1863 |
} else if (!buffer_mapped(bh) && buffer_dirty(bh)) { |
|---|
| 1864 |
err = get_block(inode, block, bh, 1); |
|---|
| 1865 |
if (err) |
|---|
| 1866 |
goto recover; |
|---|
| 1867 |
if (buffer_new(bh)) { |
|---|
| 1868 |
|
|---|
| 1869 |
clear_buffer_new(bh); |
|---|
| 1870 |
unmap_underlying_metadata(bh->b_bdev, |
|---|
| 1871 |
bh->b_blocknr); |
|---|
| 1872 |
} |
|---|
| 1873 |
} |
|---|
| 1874 |
bh = bh->b_this_page; |
|---|
| 1875 |
block++; |
|---|
| 1876 |
} while (bh != head); |
|---|
| 1877 |
|
|---|
| 1878 |
do { |
|---|
| 1879 |
get_bh(bh); |
|---|
| 1880 |
if (!buffer_mapped(bh)) |
|---|
| 1881 |
continue; |
|---|
| 1882 |
|
|---|
| 1883 |
|
|---|
| 1884 |
|
|---|
| 1885 |
|
|---|
| 1886 |
|
|---|
| 1887 |
|
|---|
| 1888 |
|
|---|
| 1889 |
if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) { |
|---|
| 1890 |
lock_buffer(bh); |
|---|
| 1891 |
} else if (test_set_buffer_locked(bh)) { |
|---|
| 1892 |
redirty_page_for_writepage(wbc, page); |
|---|
| 1893 |
continue; |
|---|
| 1894 |
} |
|---|
| 1895 |
if (test_clear_buffer_dirty(bh)) { |
|---|
| 1896 |
mark_buffer_async_write(bh); |
|---|
| 1897 |
} else { |
|---|
| 1898 |
unlock_buffer(bh); |
|---|
| 1899 |
} |
|---|
| 1900 |
} while ((bh = bh->b_this_page) != head); |
|---|
| 1901 |
|
|---|
| 1902 |
BUG_ON(PageWriteback(page)); |
|---|
| 1903 |
set_page_writeback(page); |
|---|
| 1904 |
unlock_page(page); |
|---|
| 1905 |
|
|---|
| 1906 |
|
|---|
| 1907 |
|
|---|
| 1908 |
|
|---|
| 1909 |
|
|---|
| 1910 |
do { |
|---|
| 1911 |
struct buffer_head *next = bh->b_this_page; |
|---|
| 1912 |
if (buffer_async_write(bh)) { |
|---|
| 1913 |
submit_bh(WRITE, bh); |
|---|
| 1914 |
nr_underway++; |
|---|
| 1915 |
} |
|---|
| 1916 |
put_bh(bh); |
|---|
| 1917 |
bh = next; |
|---|
| 1918 |
} while (bh != head); |
|---|
| 1919 |
|
|---|
| 1920 |
err = 0; |
|---|
| 1921 |
done: |
|---|
| 1922 |
if (nr_underway == 0) { |
|---|
| 1923 |
|
|---|
| 1924 |
|
|---|
| 1925 |
|
|---|
| 1926 |
|
|---|
| 1927 |
|
|---|
| 1928 |
int uptodate = 1; |
|---|
| 1929 |
do { |
|---|
| 1930 |
if (!buffer_uptodate(bh)) { |
|---|
| 1931 |
uptodate = 0; |
|---|
| 1932 |
break; |
|---|
| 1933 |
} |
|---|
| 1934 |
bh = bh->b_this_page; |
|---|
| 1935 |
} while (bh != head); |
|---|
| 1936 |
if (uptodate) |
|---|
| 1937 |
SetPageUptodate(page); |
|---|
| 1938 |
end_page_writeback(page); |
|---|
| 1939 |
wbc->pages_skipped++; |
|---|
| 1940 |
} |
|---|
| 1941 |
return err; |
|---|
| 1942 |
|
|---|
| 1943 |
recover: |
|---|
| 1944 |
|
|---|
| 1945 |
|
|---|
| 1946 |
|
|---|
| 1947 |
|
|---|
| 1948 |
|
|---|
| 1949 |
|
|---|
| 1950 |
bh = head; |
|---|
| 1951 |
|
|---|
| 1952 |
do { |
|---|
| 1953 |
get_bh(bh); |
|---|
| 1954 |
if (buffer_mapped(bh) && buffer_dirty(bh)) { |
|---|
| 1955 |
lock_buffer(bh); |
|---|
| 1956 |
mark_buffer_async_write(bh); |
|---|
| 1957 |
} else { |
|---|
| 1958 |
|
|---|
| 1959 |
|
|---|
| 1960 |
|
|---|
| 1961 |
|
|---|
| 1962 |
clear_buffer_dirty(bh); |
|---|
| 1963 |
} |
|---|
| 1964 |
} while ((bh = bh->b_this_page) != head); |
|---|
| 1965 |
SetPageError(page); |
|---|
| 1966 |
BUG_ON(PageWriteback(page)); |
|---|
| 1967 |
set_page_writeback(page); |
|---|
| 1968 |
unlock_page(page); |
|---|
| 1969 |
do { |
|---|
| 1970 |
struct buffer_head *next = bh->b_this_page; |
|---|
| 1971 |
if (buffer_async_write(bh)) { |
|---|
| 1972 |
clear_buffer_dirty(bh); |
|---|
| 1973 |
submit_bh(WRITE, bh); |
|---|
| 1974 |
nr_underway++; |
|---|
| 1975 |
} |
|---|
| 1976 |
put_bh(bh); |
|---|
| 1977 |
bh = next; |
|---|
| 1978 |
} while (bh != head); |
|---|
| 1979 |
goto done; |
|---|
| 1980 |
} |
|---|
| 1981 |
|
|---|
| 1982 |
static int __block_prepare_write(struct inode *inode, struct page *page, |
|---|
| 1983 |
unsigned from, unsigned to, get_block_t *get_block) |
|---|
| 1984 |
{ |
|---|
| 1985 |
unsigned block_start, block_end; |
|---|
| 1986 |
sector_t block; |
|---|
| 1987 |
int err = 0; |
|---|
| 1988 |
unsigned blocksize, bbits; |
|---|
| 1989 |
struct buffer_head *bh, *head, *wait[2], **wait_bh=wait; |
|---|
| 1990 |
|
|---|
| 1991 |
BUG_ON(!PageLocked(page)); |
|---|
| 1992 |
BUG_ON(from > PAGE_CACHE_SIZE); |
|---|
| 1993 |
BUG_ON(to > PAGE_CACHE_SIZE); |
|---|
| 1994 |
BUG_ON(from > to); |
|---|
| 1995 |
|
|---|
| 1996 |
blocksize = 1 << inode->i_blkbits; |
|---|
| 1997 |
if (!page_has_buffers(page)) |
|---|
| 1998 |
create_empty_buffers(page, blocksize, 0); |
|---|
| 1999 |
head = page_buffers(page); |
|---|
| 2000 |
|
|---|
| 2001 |
bbits = inode->i_blkbits; |
|---|
| 2002 |
block = (sector_t)page->index << (PAGE_CACHE_SHIFT - bbits); |
|---|
| 2003 |
|
|---|
| 2004 |
for(bh = head, block_start = 0; bh != head || !block_start; |
|---|
| 2005 |
block++, block_start=block_end, bh = bh->b_this_page) { |
|---|
| 2006 |
block_end = block_start + blocksize; |
|---|
| 2007 |
if (block_end <= from || block_start >= to) { |
|---|
| 2008 |
if (PageUptodate(page)) { |
|---|
| 2009 |
if (!buffer_uptodate(bh)) |
|---|
| 2010 |
set_buffer_uptodate(bh); |
|---|
| 2011 |
} |
|---|
| 2012 |
continue; |
|---|
| 2013 |
} |
|---|
| 2014 |
if (buffer_new(bh)) |
|---|
| 2015 |
clear_buffer_new(bh); |
|---|
| 2016 |
if (!buffer_mapped(bh)) { |
|---|
| 2017 |
err = get_block(inode, block, bh, 1); |
|---|
| 2018 |
if (err) |
|---|
| 2019 |
goto out; |
|---|
| 2020 |
if (buffer_new(bh)) { |
|---|
| 2021 |
clear_buffer_new(bh); |
|---|
| 2022 |
unmap_underlying_metadata(bh->b_bdev, |
|---|
| 2023 |
bh->b_blocknr); |
|---|
| 2024 |
if (PageUptodate(page)) { |
|---|
| 2025 |
set_buffer_uptodate(bh); |
|---|
| 2026 |
continue; |
|---|
| 2027 |
} |
|---|
| 2028 |
if (block_end > to || block_start < from) { |
|---|
| 2029 |
void *kaddr; |
|---|
| 2030 |
|
|---|
| 2031 |
kaddr = kmap_atomic(page, KM_USER0); |
|---|
| 2032 |
if (block_end > to) |
|---|
| 2033 |
memset(kaddr+to, 0, |
|---|
| 2034 |
block_end-to); |
|---|
| 2035 |
if (block_start < from) |
|---|
| 2036 |
memset(kaddr+block_start, |
|---|
| 2037 |
0, from-block_start); |
|---|
| 2038 |
flush_dcache_page(page); |
|---|
| 2039 |
kunmap_atomic(kaddr, KM_USER0); |
|---|
| 2040 |
} |
|---|
| 2041 |
continue; |
|---|
| 2042 |
} |
|---|
| 2043 |
} |
|---|
| 2044 |
if (PageUptodate(page)) { |
|---|
| 2045 |
if (!buffer_uptodate(bh)) |
|---|
| 2046 |
set_buffer_uptodate(bh); |
|---|
| 2047 |
continue; |
|---|
| 2048 |
} |
|---|
| 2049 |
if (!buffer_uptodate(bh) && !buffer_delay(bh) && |
|---|
| 2050 |
(block_start < from || block_end > to)) { |
|---|
| 2051 |
ll_rw_block(READ, 1, &bh); |
|---|
| 2052 |
*wait_bh++=bh; |
|---|
| 2053 |
} |
|---|
| 2054 |
} |
|---|
| 2055 |
|
|---|
| 2056 |
|
|---|
| 2057 |
|
|---|
| 2058 |
while(wait_bh > wait) { |
|---|
| 2059 |
wait_on_buffer(*--wait_bh); |
|---|
| 2060 |
if (!buffer_uptodate(*wait_bh)) |
|---|
| 2061 |
return -EIO; |
|---|
| 2062 |
} |
|---|
| 2063 |
return 0; |
|---|
| 2064 |
out: |
|---|
| 2065 |
|
|---|
| 2066 |
|
|---|
| 2067 |
|
|---|
| 2068 |
|
|---|
| 2069 |
|
|---|
| 2070 |
bh = head; |
|---|
| 2071 |
block_start = 0; |
|---|
| 2072 |
do { |
|---|
| 2073 |
block_end = block_start+blocksize; |
|---|
| 2074 |
if (block_end <= from) |
|---|
| 2075 |
goto next_bh; |
|---|
| 2076 |
if (block_start >= to) |
|---|
| 2077 |
break; |
|---|
| 2078 |
if (buffer_new(bh)) { |
|---|
| 2079 |
void *kaddr; |
|---|
| 2080 |
|
|---|
| 2081 |
clear_buffer_new(bh); |
|---|
| 2082 |
kaddr = kmap_atomic(page, KM_USER0); |
|---|
| 2083 |
memset(kaddr+block_start, 0, bh->b_size); |
|---|
| 2084 |
kunmap_atomic(kaddr, KM_USER0); |
|---|
| 2085 |
set_buffer_uptodate(bh); |
|---|
| 2086 |
mark_buffer_dirty(bh); |
|---|
| 2087 |
} |
|---|
| 2088 |
next_bh: |
|---|
| 2089 |
block_start = block_end; |
|---|
| 2090 |
bh = bh->b_this_page; |
|---|
| 2091 |
} while (bh != head); |
|---|
| 2092 |
return err; |
|---|
| 2093 |
} |
|---|
| 2094 |
|
|---|
| 2095 |
static int __block_commit_write(struct inode *inode, struct page *page, |
|---|
| 2096 |
unsigned from, unsigned to) |
|---|
| 2097 |
{ |
|---|
| 2098 |
unsigned block_start, block_end; |
|---|
| 2099 |
int partial = 0; |
|---|
| 2100 |
unsigned blocksize; |
|---|
| 2101 |
struct buffer_head *bh, *head; |
|---|
| 2102 |
|
|---|
| 2103 |
blocksize = 1 << inode->i_blkbits; |
|---|
| 2104 |
|
|---|
| 2105 |
for(bh = head = page_buffers(page), block_start = 0; |
|---|
| 2106 |
bh != head || !block_start; |
|---|
| 2107 |
block_start=block_end, bh = bh->b_this_page) { |
|---|
| 2108 |
block_end = block_start + blocksize; |
|---|
| 2109 |
if (block_end <= from || block_start >= to) { |
|---|
| 2110 |
if (!buffer_uptodate(bh)) |
|---|
| 2111 |
partial = 1; |
|---|
| 2112 |
} else { |
|---|
| 2113 |
set_buffer_uptodate(bh); |
|---|
| 2114 |
mark_buffer_dirty(bh); |
|---|
| 2115 |
} |
|---|
| 2116 |
} |
|---|
| 2117 |
|
|---|
| 2118 |
|
|---|
| 2119 |
|
|---|
| 2120 |
|
|---|
| 2121 |
|
|---|
| 2122 |
|
|---|
| 2123 |
|
|---|
| 2124 |
if (!partial) |
|---|
| 2125 |
SetPageUptodate(page); |
|---|
| 2126 |
return 0; |
|---|
| 2127 |
} |
|---|
| 2128 |
|
|---|
| 2129 |
|
|---|
| 2130 |
|
|---|
| 2131 |
|
|---|
| 2132 |
|
|---|
| 2133 |
|
|---|
| 2134 |
|
|---|
| 2135 |
|
|---|
| 2136 |
int block_read_full_page(struct page *page, get_block_t *get_block) |
|---|
| 2137 |
{ |
|---|
| 2138 |
struct inode *inode = page->mapping->host; |
|---|
| 2139 |
sector_t iblock, lblock; |
|---|
| 2140 |
struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE]; |
|---|
| 2141 |
unsigned int blocksize; |
|---|
| 2142 |
int nr, i; |
|---|
| 2143 |
int fully_mapped = 1; |
|---|
| 2144 |
|
|---|
| 2145 |
if (!PageLocked(page)) |
|---|
| 2146 |
PAGE_BUG(page); |
|---|
| 2147 |
blocksize = 1 << inode->i_blkbits; |
|---|
| 2148 |
if (!page_has_buffers(page)) |
|---|
| 2149 |
create_empty_buffers(page, blocksize, 0); |
|---|
| 2150 |
head = page_buffers(page); |
|---|
| 2151 |
|
|---|
| 2152 |
iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); |
|---|
| 2153 |
lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits; |
|---|
| 2154 |
bh = head; |
|---|
| 2155 |
nr = 0; |
|---|
| 2156 |
i = 0; |
|---|
| 2157 |
|
|---|
| 2158 |
do { |
|---|
| 2159 |
if (buffer_uptodate(bh)) |
|---|
| 2160 |
continue; |
|---|
| 2161 |
|
|---|
| 2162 |
if (!buffer_mapped(bh)) { |
|---|
| 2163 |
fully_mapped = 0; |
|---|
| 2164 |
if (iblock < lblock) { |
|---|
| 2165 |
if (get_block(inode, iblock, bh, 0)) |
|---|
| 2166 |
SetPageError(page); |
|---|
| 2167 |
} |
|---|
| 2168 |
if (!buffer_mapped(bh)) { |
|---|
| 2169 |
void *kaddr = kmap_atomic(page, KM_USER0); |
|---|
| 2170 |
memset(kaddr + i * blocksize, 0, blocksize); |
|---|
| 2171 |
flush_dcache_page(page); |
|---|
| 2172 |
kunmap_atomic(kaddr, KM_USER0); |
|---|
| 2173 |
set_buffer_uptodate(bh); |
|---|
| 2174 |
continue; |
|---|
| 2175 |
} |
|---|
| 2176 |
|
|---|
| 2177 |
|
|---|
| 2178 |
|
|---|
| 2179 |
|
|---|
| 2180 |
if (buffer_uptodate(bh)) |
|---|
| 2181 |
continue; |
|---|
| 2182 |
} |
|---|
| 2183 |
arr[nr++] = bh; |
|---|
| 2184 |
} while (i++, iblock++, (bh = bh->b_this_page) != head); |
|---|
| 2185 |
|
|---|
| 2186 |
if (fully_mapped) |
|---|
| 2187 |
SetPageMappedToDisk(page); |
|---|
| 2188 |
|
|---|
| 2189 |
if (!nr) { |
|---|
| 2190 |
|
|---|
| 2191 |
|
|---|
| 2192 |
|
|---|
| 2193 |
|
|---|
| 2194 |
if (!PageError(page)) |
|---|
| 2195 |
SetPageUptodate(page); |
|---|
| 2196 |
unlock_page(page); |
|---|
| 2197 |
return 0; |
|---|
| 2198 |
} |
|---|
| 2199 |
|
|---|
| 2200 |
|
|---|
| 2201 |
for (i = 0; i < nr; i++) { |
|---|
| 2202 |
bh = arr[i]; |
|---|
| 2203 |
lock_buffer(bh); |
|---|
| 2204 |
mark_buffer_async_read(bh); |
|---|
| 2205 |
} |
|---|
| 2206 |
|
|---|
| 2207 |
|
|---|
| 2208 |
|
|---|
| 2209 |
|
|---|
| 2210 |
|
|---|
| 2211 |
|
|---|
| 2212 |
for (i = 0; i < nr; i++) { |
|---|
| 2213 |
bh = arr[i]; |
|---|
| 2214 |
if (buffer_uptodate(bh)) |
|---|
| 2215 |
end_buffer_async_read(bh, 1); |
|---|
| 2216 |
else |
|---|
| 2217 |
submit_bh(READ, bh); |
|---|
| 2218 |
} |
|---|
| 2219 |
return 0; |
|---|
| 2220 |
} |
|---|
| 2221 |
|
|---|
| 2222 |
|
|---|
| 2223 |
|
|---|
| 2224 |
|
|---|
| 2225 |
|
|---|
| 2226 |
int generic_cont_expand(struct inode *inode, loff_t size) |
|---|
| 2227 |
{ |
|---|
| 2228 |
struct address_space *mapping = inode->i_mapping; |
|---|
| 2229 |
struct page *page; |
|---|
| 2230 |
unsigned long index, offset, limit; |
|---|
| 2231 |
int err; |
|---|
| 2232 |
|
|---|
| 2233 |
err = -EFBIG; |
|---|
| 2234 |
limit = current->rlim[RLIMIT_FSIZE].rlim_cur; |
|---|
| 2235 |
if (limit != RLIM_INFINITY && size > (loff_t)limit) { |
|---|
| 2236 |
send_sig(SIGXFSZ, current, 0); |
|---|
| 2237 |
goto out; |
|---|
| 2238 |
} |
|---|
| 2239 |
if (size > inode->i_sb->s_maxbytes) |
|---|
| 2240 |
goto out; |
|---|
| 2241 |
|
|---|
| 2242 |
offset = (size & (PAGE_CACHE_SIZE-1)); |
|---|
| 2243 |
|
|---|
| 2244 |
|
|---|
| 2245 |
|
|---|
| 2246 |
|
|---|
| 2247 |
|
|---|
| 2248 |
if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) { |
|---|
| 2249 |
offset++; |
|---|
| 2250 |
} |
|---|
| 2251 |
index = size >> PAGE_CACHE_SHIFT; |
|---|
| 2252 |
err = -ENOMEM; |
|---|
| 2253 |
page = grab_cache_page(mapping, index); |
|---|
| 2254 |
if (!page) |
|---|
| 2255 |
goto out; |
|---|
| 2256 |
err = mapping->a_ops->prepare_write(NULL, page, offset, offset); |
|---|
| 2257 |
if (!err) { |
|---|
| 2258 |
err = mapping->a_ops->commit_write(NULL, page, offset, offset); |
|---|
| 2259 |
} |
|---|
| 2260 |
unlock_page(page); |
|---|
| 2261 |
page_cache_release(page); |
|---|
| 2262 |
if (err > 0) |
|---|
| 2263 |
err = 0; |
|---|
| 2264 |
out: |
|---|
| 2265 |
return err; |
|---|
| 2266 |
} |
|---|
| 2267 |
|
|---|
| 2268 |
|
|---|
| 2269 |
|
|---|
| 2270 |
|
|---|
| 2271 |
|
|---|
| 2272 |
|
|---|
| 2273 |
int cont_prepare_write(struct page *page, unsigned offset, |
|---|
| 2274 |
unsigned to, get_block_t *get_block, loff_t *bytes) |
|---|
| 2275 |
{ |
|---|
| 2276 |
struct address_space *mapping = page->mapping; |
|---|
| 2277 |
struct inode *inode = mapping->host; |
|---|
| 2278 |
struct page *new_page; |
|---|
| 2279 |
pgoff_t pgpos; |
|---|
| 2280 |
long status; |
|---|
| 2281 |
unsigned zerofrom; |
|---|
| 2282 |
unsigned blocksize = 1 << inode->i_blkbits; |
|---|
| 2283 |
void *kaddr; |
|---|
| 2284 |
|
|---|
| 2285 |
while(page->index > (pgpos = *bytes>>PAGE_CACHE_SHIFT)) { |
|---|
| 2286 |
status = -ENOMEM; |
|---|
| 2287 |
new_page = grab_cache_page(mapping, pgpos); |
|---|
| 2288 |
if (!new_page) |
|---|
| 2289 |
goto out; |
|---|
| 2290 |
|
|---|
| 2291 |
if (*bytes>>PAGE_CACHE_SHIFT != pgpos) { |
|---|
| 2292 |
unlock_page(new_page); |
|---|
| 2293 |
page_cache_release(new_page); |
|---|
| 2294 |
continue; |
|---|
| 2295 |
} |
|---|
| 2296 |
zerofrom = *bytes & ~PAGE_CACHE_MASK; |
|---|
| 2297 |
if (zerofrom & (blocksize-1)) { |
|---|
| 2298 |
*bytes |= (blocksize-1); |
|---|
| 2299 |
(*bytes)++; |
|---|
| 2300 |
} |
|---|
| 2301 |
status = __block_prepare_write(inode, new_page, zerofrom, |
|---|
| 2302 |
PAGE_CACHE_SIZE, get_block); |
|---|
| 2303 |
if (status) |
|---|
| 2304 |
goto out_unmap; |
|---|
| 2305 |
kaddr = kmap_atomic(new_page, KM_USER0); |
|---|
| 2306 |
memset(kaddr+zerofrom, 0, PAGE_CACHE_SIZE-zerofrom); |
|---|
| 2307 |
flush_dcache_page(new_page); |
|---|
| 2308 |
kunmap_atomic(kaddr, KM_USER0); |
|---|
| 2309 |
__block_commit_write(inode, new_page, |
|---|
| 2310 |
zerofrom, PAGE_CACHE_SIZE); |
|---|
| 2311 |
unlock_page(new_page); |
|---|
| 2312 |
page_cache_release(new_page); |
|---|
| 2313 |
} |
|---|
| 2314 |
|
|---|
| 2315 |
if (page->index < pgpos) { |
|---|
| 2316 |
|
|---|
| 2317 |
zerofrom = offset; |
|---|
| 2318 |
} else { |
|---|
| 2319 |
|
|---|
| 2320 |
zerofrom = *bytes & ~PAGE_CACHE_MASK; |
|---|
| 2321 |
|
|---|
| 2322 |
|
|---|
| 2323 |
if (to > zerofrom && (zerofrom & (blocksize-1))) { |
|---|
| 2324 |
*bytes |= (blocksize-1); |
|---|
| 2325 |
(*bytes)++; |
|---|
| 2326 |
} |
|---|
| 2327 |
|
|---|
| 2328 |
|
|---|
| 2329 |
if (offset <= zerofrom) |
|---|
| 2330 |
zerofrom = offset; |
|---|
| 2331 |
} |
|---|
| 2332 |
status = __block_prepare_write(inode, page, zerofrom, to, get_block); |
|---|
| 2333 |
if (status) |
|---|
| 2334 |
goto out1; |
|---|
| 2335 |
if (zerofrom < offset) { |
|---|
| 2336 |
kaddr = kmap_atomic(page, KM_USER0); |
|---|
| 2337 |
memset(kaddr+zerofrom, 0, offset-zerofrom); |
|---|
| 2338 |
flush_dcache_page(page); |
|---|
| 2339 |
kunmap_atomic(kaddr, KM_USER0); |
|---|
| 2340 |
__block_commit_write(inode, page, zerofrom, offset); |
|---|
| 2341 |
} |
|---|
| 2342 |
return 0; |
|---|
| 2343 |
out1: |
|---|
| 2344 |
ClearPageUptodate(page); |
|---|
| 2345 |
return status; |
|---|
| 2346 |
|
|---|
| 2347 |
out_unmap: |
|---|
| 2348 |
ClearPageUptodate(new_page); |
|---|
| 2349 |
unlock_page(new_page); |
|---|
| 2350 |
page_cache_release(new_page); |
|---|
| 2351 |
out: |
|---|
| 2352 |
return status; |
|---|
| 2353 |
} |
|---|
| 2354 |
|
|---|
| 2355 |
int block_prepare_write(struct page *page, unsigned from, unsigned to, |
|---|
| 2356 |
get_block_t *get_block) |
|---|
| 2357 |
{ |
|---|
| 2358 |
struct inode *inode = page->mapping->host; |
|---|
| 2359 |
int err = __block_prepare_write(inode, page, from, to, get_block); |
|---|
| 2360 |
if (err) |
|---|
| 2361 |
ClearPageUptodate(page); |
|---|
| 2362 |
return err; |
|---|
| 2363 |
} |
|---|
| 2364 |
|
|---|
| 2365 |
int block_commit_write(struct page *page, unsigned from, unsigned to) |
|---|
| 2366 |
{ |
|---|
| 2367 |
struct inode *inode = page->mapping->host; |
|---|
| 2368 |
__block_commit_write(inode,page,from,to); |
|---|
| 2369 |
return 0; |
|---|
| 2370 |
} |
|---|
| 2371 |
|
|---|
| 2372 |
int generic_commit_write(struct file *file, struct page *page, |
|---|
| 2373 |
unsigned from, unsigned to) |
|---|
| 2374 |
{ |
|---|
| 2375 |
struct inode *inode = page->mapping->host; |
|---|
| 2376 |
loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to; |
|---|
| 2377 |
__block_commit_write(inode,page,from,to); |
|---|
| 2378 |
|
|---|
| 2379 |
|
|---|
| 2380 |
|
|---|
| 2381 |
|
|---|
| 2382 |
if (pos > inode->i_size) { |
|---|
| 2383 |
i_size_write(inode, pos); |
|---|
| 2384 |
mark_inode_dirty(inode); |
|---|
| 2385 |
} |
|---|
| 2386 |
return 0; |
|---|
| 2387 |
} |
|---|
| 2388 |
|
|---|
| 2389 |
|
|---|
| 2390 |
|
|---|
| 2391 |
|
|---|
| 2392 |
|
|---|
| 2393 |
|
|---|
| 2394 |
|
|---|
| 2395 |
|
|---|
| 2396 |
|
|---|
| 2397 |
|
|---|
| 2398 |
|
|---|
| 2399 |
|
|---|
| 2400 |
static void end_buffer_read_nobh(struct buffer_head *bh, int uptodate) |
|---|
| 2401 |
{ |
|---|
| 2402 |
if (uptodate) { |
|---|
| 2403 |
set_buffer_uptodate(bh); |
|---|
| 2404 |
} else { |
|---|
| 2405 |
|
|---|
| 2406 |
clear_buffer_uptodate(bh); |
|---|
| 2407 |
} |
|---|
| 2408 |
unlock_buffer(bh); |
|---|
| 2409 |
} |
|---|
| 2410 |
|
|---|
| 2411 |
|
|---|
| 2412 |
|
|---|
| 2413 |
|
|---|
| 2414 |
|
|---|
| 2415 |
int nobh_prepare_write(struct page *page, unsigned from, unsigned to, |
|---|
| 2416 |
get_block_t *get_block) |
|---|
| 2417 |
{ |
|---|
| 2418 |
struct inode *inode = page->mapping->host; |
|---|
| 2419 |
const unsigned blkbits = inode->i_blkbits; |
|---|
| 2420 |
const unsigned blocksize = 1 << blkbits; |
|---|
| 2421 |
struct buffer_head map_bh; |
|---|
| 2422 |
struct buffer_head *read_bh[MAX_BUF_PER_PAGE]; |
|---|
| 2423 |
unsigned block_in_page; |
|---|
| 2424 |
unsigned block_start; |
|---|
| 2425 |
sector_t block_in_file; |
|---|
| 2426 |
char *kaddr; |
|---|
| 2427 |
int nr_reads = 0; |
|---|
| 2428 |
int i; |
|---|
| 2429 |
int ret = 0; |
|---|
| 2430 |
int is_mapped_to_disk = 1; |
|---|
| 2431 |
int dirtied_it = 0; |
|---|
| 2432 |
|
|---|
| 2433 |
if (PageMappedToDisk(page)) |
|---|
| 2434 |
return 0; |
|---|
| 2435 |
|
|---|
| 2436 |
block_in_file = (sector_t)page->index << (PAGE_CACHE_SHIFT - blkbits); |
|---|
| 2437 |
map_bh.b_page = page; |
|---|
| 2438 |
|
|---|
| 2439 |
|
|---|
| 2440 |
|
|---|
| 2441 |
|
|---|
| 2442 |
|
|---|
| 2443 |
|
|---|
| 2444 |
for (block_start = 0, block_in_page = 0; |
|---|
| 2445 |
block_start < PAGE_CACHE_SIZE; |
|---|
| 2446 |
block_in_page++, block_start += blocksize) { |
|---|
| 2447 |
unsigned block_end = block_start + blocksize; |
|---|
| 2448 |
int create; |
|---|
| 2449 |
|
|---|
| 2450 |
map_bh.b_state = 0; |
|---|
| 2451 |
create = 1; |
|---|
| 2452 |
if (block_start >= to) |
|---|
| 2453 |
create = 0; |
|---|
| 2454 |
ret = get_block(inode, block_in_file + block_in_page, |
|---|
| 2455 |
&map_bh, create); |
|---|
| 2456 |
if (ret) |
|---|
| 2457 |
goto failed; |
|---|
| 2458 |
if (!buffer_mapped(&map_bh)) |
|---|
| 2459 |
is_mapped_to_disk = 0; |
|---|
| 2460 |
if (buffer_new(&map_bh)) |
|---|
| 2461 |
unmap_underlying_metadata(map_bh.b_bdev, |
|---|
| 2462 |
map_bh.b_blocknr); |
|---|
| 2463 |
if (PageUptodate(page)) |
|---|
| 2464 |
continue; |
|---|
| 2465 |
if (buffer_new(&map_bh) || !buffer_mapped(&map_bh)) { |
|---|
| 2466 |
kaddr = kmap_atomic(page, KM_USER0); |
|---|
| 2467 |
if (block_start < from) { |
|---|
| 2468 |
memset(kaddr+block_start, 0, from-block_start); |
|---|
| 2469 |
dirtied_it = 1; |
|---|
| 2470 |
} |
|---|
| 2471 |
if (block_end > to) { |
|---|
| 2472 |
memset(kaddr + to, 0, block_end - to); |
|---|
| 2473 |
dirtied_it = 1; |
|---|
| 2474 |
} |
|---|
| 2475 |
flush_dcache_page(page); |
|---|
| 2476 |
kunmap_atomic(kaddr, KM_USER0); |
|---|
| 2477 |
continue; |
|---|
| 2478 |
} |
|---|
| 2479 |
if (buffer_uptodate(&map_bh)) |
|---|
| 2480 |
continue; |
|---|
| 2481 |
if (block_start < from || block_end > to) { |
|---|
| 2482 |
struct buffer_head *bh = alloc_buffer_head(GFP_NOFS); |
|---|
| 2483 |
|
|---|
| 2484 |
if (!bh) { |
|---|
| 2485 |
ret = -ENOMEM; |
|---|
| 2486 |
goto failed; |
|---|
| 2487 |
} |
|---|
| 2488 |
bh->b_state = map_bh.b_state; |
|---|
| 2489 |
atomic_set(&bh->b_count, 0); |
|---|
| 2490 |
bh->b_this_page = 0; |
|---|
| 2491 |
bh->b_page = page; |
|---|
| 2492 |
bh->b_blocknr = map_bh.b_blocknr; |
|---|
| 2493 |
bh->b_size = blocksize; |
|---|
| 2494 |
bh->b_data = (char *)(long)block_start; |
|---|
| 2495 |
bh->b_bdev = map_bh.b_bdev; |
|---|
| 2496 |
bh->b_private = NULL; |
|---|
| 2497 |
read_bh[nr_reads++] = bh; |
|---|
| 2498 |
} |
|---|
| 2499 |
} |
|---|
| 2500 |
|
|---|
| 2501 |
if (nr_reads) { |
|---|
| 2502 |
struct buffer_head *bh; |
|---|
| 2503 |
|
|---|
| 2504 |
|
|---|
| 2505 |
|
|---|
| 2506 |
|
|---|
| 2507 |
|
|---|
| 2508 |
|
|---|
| 2509 |
for (i = 0; i < nr_reads; i++) { |
|---|
| 2510 |
bh = read_bh[i]; |
|---|
| 2511 |
lock_buffer(bh); |
|---|
| 2512 |
bh->b_end_io = end_buffer_read_nobh; |
|---|
| 2513 |
submit_bh(READ, bh); |
|---|
| 2514 |
} |
|---|
| 2515 |
for (i = 0; i < nr_reads; i++) { |
|---|
| 2516 |
bh = read_bh[i]; |
|---|
| 2517 |
wait_on_buffer(bh); |
|---|
| 2518 |
if (!buffer_uptodate(bh)) |
|---|
| 2519 |
ret = -EIO; |
|---|
| 2520 |
free_buffer_head(bh); |
|---|
| 2521 |
read_bh[i] = NULL; |
|---|
| 2522 |
} |
|---|
| 2523 |
if (ret) |
|---|
| 2524 |
goto failed; |
|---|
| 2525 |
} |
|---|
| 2526 |
|
|---|
| 2527 |
if (is_mapped_to_disk) |
|---|
| 2528 |
SetPageMappedToDisk(page); |
|---|
| 2529 |
SetPageUptodate(page); |
|---|
| 2530 |
|
|---|
| 2531 |
|
|---|
| 2532 |
|
|---|
| 2533 |
|
|---|
| 2534 |
|
|---|
| 2535 |
|
|---|
| 2536 |
|
|---|
| 2537 |
|
|---|
| 2538 |
if (dirtied_it) |
|---|
| 2539 |
set_page_dirty(page); |
|---|
| 2540 |
|
|---|
| 2541 |
return 0; |
|---|
| 2542 |
|
|---|
| 2543 |
failed: |
|---|
| 2544 |
for (i = 0; i < nr_reads; i++) { |
|---|
| 2545 |
if (read_bh[i]) |
|---|
| 2546 |
free_buffer_head(read_bh[i]); |
|---|
| 2547 |
} |
|---|
| 2548 |
|
|---|
| 2549 |
|
|---|
| 2550 |
|
|---|
| 2551 |
|
|---|
| 2552 |
|
|---|
| 2553 |
kaddr = kmap_atomic(page, KM_USER0); |
|---|
| 2554 |
memset(kaddr, 0, PAGE_CACHE_SIZE); |
|---|
| 2555 |
kunmap_atomic(kaddr, KM_USER0); |
|---|
| 2556 |
SetPageUptodate(page); |
|---|
| 2557 |
set_page_dirty(page); |
|---|
| 2558 |
return ret; |
|---|
| 2559 |
} |
|---|
| 2560 |
EXPORT_SYMBOL(nobh_prepare_write); |
|---|
| 2561 |
|
|---|
| 2562 |
int nobh_commit_write(struct file *file, struct page *page, |
|---|
| 2563 |
unsigned from, unsigned to) |
|---|
| 2564 |
{ |
|---|
| 2565 |
struct inode *inode = page->mapping->host; |
|---|
| 2566 |
loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to; |
|---|
| 2567 |
|
|---|
| 2568 |
set_page_dirty(page); |
|---|
| 2569 |
if (pos > inode->i_size) { |
|---|
| 2570 |
i_size_write(inode, pos); |
|---|
| 2571 |
mark_inode_dirty(inode); |
|---|
| 2572 |
} |
|---|
| 2573 |
return 0; |
|---|
| 2574 |
} |
|---|
| 2575 |
EXPORT_SYMBOL(nobh_commit_write); |
|---|
| 2576 |
|
|---|
| 2577 |
|
|---|
| 2578 |
|
|---|
| 2579 |
|
|---|
| 2580 |
int nobh_truncate_page(struct address_space *mapping, loff_t from) |
|---|
| 2581 |
{ |
|---|
| 2582 |
struct inode *inode = mapping->host; |
|---|
| 2583 |
unsigned blocksize = 1 << inode->i_blkbits; |
|---|
| 2584 |
pgoff_t index = from >> PAGE_CACHE_SHIFT; |
|---|
| 2585 |
unsigned offset = from & (PAGE_CACHE_SIZE-1); |
|---|
| 2586 |
unsigned to; |
|---|
| 2587 |
struct page *page; |
|---|
| 2588 |
struct address_space_operations *a_ops = mapping->a_ops; |
|---|
| 2589 |
char *kaddr; |
|---|
| 2590 |
int ret = 0; |
|---|
| 2591 |
|
|---|
| 2592 |
if ((offset & (blocksize - 1)) == 0) |
|---|
| 2593 |
goto out; |
|---|
| 2594 |
|
|---|
| 2595 |
ret = -ENOMEM; |
|---|
| 2596 |
page = grab_cache_page(mapping, index); |
|---|
| 2597 |
if (!page) |
|---|
| 2598 |
goto out; |
|---|
| 2599 |
|
|---|
| 2600 |
to = (offset + blocksize) & ~(blocksize - 1); |
|---|
| 2601 |
ret = a_ops->prepare_write(NULL, page, offset, to); |
|---|
| 2602 |
if (ret == 0) { |
|---|
| 2603 |
kaddr = kmap_atomic(page, KM_USER0); |
|---|
| 2604 |
memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
|---|
| 2605 |
flush_dcache_page(page); |
|---|
| 2606 |
kunmap_atomic(kaddr, KM_USER0); |
|---|
| 2607 |
set_page_dirty(page); |
|---|
| 2608 |
} |
|---|
| 2609 |
unlock_page(page); |
|---|
| 2610 |
page_cache_release(page); |
|---|
| 2611 |
out: |
|---|
| 2612 |
return ret; |
|---|
| 2613 |
} |
|---|
| 2614 |
EXPORT_SYMBOL(nobh_truncate_page); |
|---|
| 2615 |
|
|---|
| 2616 |
int block_truncate_page(struct address_space *mapping, |
|---|
| 2617 |
loff_t from, get_block_t *get_block) |
|---|
| 2618 |
{ |
|---|
| 2619 |
pgoff_t index = from >> PAGE_CACHE_SHIFT; |
|---|
| 2620 |
unsigned offset = from & (PAGE_CACHE_SIZE-1); |
|---|
| 2621 |
unsigned blocksize; |
|---|
| 2622 |
pgoff_t iblock; |
|---|
| 2623 |
unsigned length, pos; |
|---|
| 2624 |
struct inode *inode = mapping->host; |
|---|
| 2625 |
struct page *page; |
|---|
| 2626 |
struct buffer_head *bh; |
|---|
| 2627 |
void *kaddr; |
|---|
| 2628 |
int err; |
|---|
| 2629 |
|
|---|
| 2630 |
blocksize = 1 << inode->i_blkbits; |
|---|
| 2631 |
length = offset & (blocksize - 1); |
|---|
| 2632 |
|
|---|
| 2633 |
|
|---|
| 2634 |
if (!length) |
|---|
| 2635 |
return 0; |
|---|
| 2636 |
|
|---|
| 2637 |
length = blocksize - length; |
|---|
| 2638 |
iblock = index << (PAGE_CACHE_SHIFT - inode->i_blkbits); |
|---|
| 2639 |
|
|---|
| 2640 |
page = grab_cache_page(mapping, index); |
|---|
| 2641 |
err = -ENOMEM; |
|---|
| 2642 |
if (!page) |
|---|
| 2643 |
goto out; |
|---|
| 2644 |
|
|---|
| 2645 |
if (!page_has_buffers(page)) |
|---|
| 2646 |
create_empty_buffers(page, blocksize, 0); |
|---|
| 2647 |
|
|---|
| 2648 |
|
|---|
| 2649 |
bh = page_buffers(page); |
|---|
| 2650 |
pos = blocksize; |
|---|
| 2651 |
while (offset >= pos) { |
|---|
| 2652 |
bh = bh->b_this_page; |
|---|
| 2653 |
iblock++; |
|---|
| 2654 |
pos += blocksize; |
|---|
| 2655 |
} |
|---|
| 2656 |
|
|---|
| 2657 |
err = 0; |
|---|
| 2658 |
if (!buffer_mapped(bh)) { |
|---|
| 2659 |
err = get_block(inode, iblock, bh, 0); |
|---|
| 2660 |
if (err) |
|---|
| 2661 |
goto unlock; |
|---|
| 2662 |
|
|---|
| 2663 |
if (!buffer_mapped(bh)) |
|---|
| 2664 |
goto unlock; |
|---|
| 2665 |
} |
|---|
| 2666 |
|
|---|
| 2667 |
|
|---|
| 2668 |
if (PageUptodate(page)) |
|---|
| 2669 |
set_buffer_uptodate(bh); |
|---|
| 2670 |
|
|---|
| 2671 |
if (!buffer_uptodate(bh) && !buffer_delay(bh)) { |
|---|
| 2672 |
err = -EIO; |
|---|
| 2673 |
ll_rw_block(READ, 1, &bh); |
|---|
| 2674 |
wait_on_buffer(bh); |
|---|
| 2675 |
|
|---|
| 2676 |
if (!buffer_uptodate(bh)) |
|---|
| 2677 |
goto unlock; |
|---|
| 2678 |
} |
|---|
| 2679 |
|
|---|
| 2680 |
kaddr = kmap_atomic(page, KM_USER0); |
|---|
| 2681 |
memset(kaddr + offset, 0, length); |
|---|
| 2682 |
flush_dcache_page(page); |
|---|
| 2683 |
kunmap_atomic(kaddr, KM_USER0); |
|---|
| 2684 |
|
|---|
| 2685 |
mark_buffer_dirty(bh); |
|---|
| 2686 |
err = 0; |
|---|
| 2687 |
|
|---|
| 2688 |
unlock: |
|---|
| 2689 |
unlock_page(page); |
|---|
| 2690 |
page_cache_release(page); |
|---|
| 2691 |
out: |
|---|
| 2692 |
return err; |
|---|
| 2693 |
} |
|---|
| 2694 |
|
|---|
| 2695 |
|
|---|
| 2696 |
|
|---|
| 2697 |
|
|---|
| 2698 |
int block_write_full_page(struct page *page, get_block_t *get_block, |
|---|
| 2699 |
struct writeback_control *wbc) |
|---|
| 2700 |
{ |
|---|
| 2701 |
struct inode * const inode = page->mapping->host; |
|---|
| 2702 |
loff_t i_size = i_size_read(inode); |
|---|
| 2703 |
const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; |
|---|
| 2704 |
unsigned offset; |
|---|
| 2705 |
void *kaddr; |
|---|
| 2706 |
|
|---|
| 2707 |
|
|---|
| 2708 |
if (page->index < end_index) |
|---|
| 2709 |
return __block_write_full_page(inode, page, get_block, wbc); |
|---|
| 2710 |
|
|---|
| 2711 |
|
|---|
| 2712 |
offset = i_size & (PAGE_CACHE_SIZE-1); |
|---|
| 2713 |
if (page->index >= end_index+1 || !offset) { |
|---|
| 2714 |
|
|---|
| 2715 |
|
|---|
| 2716 |
|
|---|
| 2717 |
|
|---|
| 2718 |
|
|---|
| 2719 |
block_invalidatepage(page, 0); |
|---|
| 2720 |
unlock_page(page); |
|---|
| 2721 |
return 0; |
|---|
| 2722 |
} |
|---|
| 2723 |
|
|---|
| 2724 |
|
|---|
| 2725 |
|
|---|
| 2726 |
|
|---|
| 2727 |
|
|---|
| 2728 |
|
|---|
| 2729 |
|
|---|
| 2730 |
|
|---|
| 2731 |
kaddr = kmap_atomic(page, KM_USER0); |
|---|
| 2732 |
memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
|---|
| 2733 |
flush_dcache_page(page); |
|---|
| 2734 |
kunmap_atomic(kaddr, KM_USER0); |
|---|
| 2735 |
return __block_write_full_page(inode, page, get_block, wbc); |
|---|
| 2736 |
} |
|---|
| 2737 |
|
|---|
| 2738 |
sector_t generic_block_bmap(struct address_space *mapping, sector_t block, |
|---|
| 2739 |
get_block_t *get_block) |
|---|
| 2740 |
{ |
|---|
| 2741 |
struct buffer_head tmp; |
|---|
| 2742 |
struct inode *inode = mapping->host; |
|---|
| 2743 |
tmp.b_state = 0; |
|---|
| 2744 |
tmp.b_blocknr = 0; |
|---|
| 2745 |
get_block(inode, block, &tmp, 0); |
|---|
| 2746 |
return tmp.b_blocknr; |
|---|
| 2747 |
} |
|---|
| 2748 |
|
|---|
| 2749 |
static int end_bio_bh_io_sync(struct bio *bio, unsigned int bytes_done, int err) |
|---|
| 2750 |
{ |
|---|
| 2751 |
struct buffer_head *bh = bio->bi_private; |
|---|
| 2752 |
|
|---|
| 2753 |
if (bio->bi_size) |
|---|
| 2754 |
return 1; |
|---|
| 2755 |
|
|---|
| 2756 |
bh->b_end_io(bh, test_bit(BIO_UPTODATE, &bio->bi_flags)); |
|---|
| 2757 |
bio_put(bio); |
|---|
| 2758 |
return 0; |
|---|
| 2759 |
} |
|---|
| 2760 |
|
|---|
| 2761 |
void submit_bh(int rw, struct buffer_head * bh) |
|---|
| 2762 |
{ |
|---|
| 2763 |
struct bio *bio; |
|---|
| 2764 |
|
|---|
| 2765 |
BUG_ON(!buffer_locked(bh)); |
|---|
| 2766 |
BUG_ON(!buffer_mapped(bh)); |
|---|
| 2767 |
BUG_ON(!bh->b_end_io); |
|---|
| 2768 |
|
|---|
| 2769 |
|
|---|
| 2770 |
if (test_set_buffer_req(bh) && rw == WRITE) |
|---|
| 2771 |
clear_buffer_write_io_error(bh); |
|---|
| 2772 |
|
|---|
| 2773 |
|
|---|
| 2774 |
|
|---|
| 2775 |
|
|---|
| 2776 |
|
|---|
| 2777 |
bio = bio_alloc(GFP_NOIO, 1); |
|---|
| 2778 |
|
|---|
| 2779 |
bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9); |
|---|
| 2780 |
bio->bi_bdev = bh->b_bdev; |
|---|
| 2781 |
bio->bi_io_vec[0].bv_page = bh->b_page; |
|---|
| 2782 |
bio->bi_io_vec[0].bv_len = bh->b_size; |
|---|
| 2783 |
bio->bi_io_vec[0].bv_offset = bh_offset(bh); |
|---|
| 2784 |
|
|---|
| 2785 |
bio->bi_vcnt = 1; |
|---|
| 2786 |
bio->bi_idx = 0; |
|---|
| 2787 |
bio->bi_size = bh->b_size; |
|---|
| 2788 |
|
|---|
| 2789 |
bio->bi_end_io = end_bio_bh_io_sync; |
|---|
| 2790 |
bio->bi_private = bh; |
|---|
| 2791 |
|
|---|
| 2792 |
submit_bio(rw, bio); |
|---|
| 2793 |
} |
|---|
| 2794 |
|
|---|
| 2795 |
|
|---|
| 2796 |
|
|---|
| 2797 |
|
|---|
| 2798 |
|
|---|
| 2799 |
|
|---|
| 2800 |
|
|---|
| 2801 |
|
|---|
| 2802 |
|
|---|
| 2803 |
|
|---|
| 2804 |
|
|---|
| 2805 |
|
|---|
| 2806 |
|
|---|
| 2807 |
|
|---|
| 2808 |
|
|---|
| 2809 |
|
|---|
| 2810 |
|
|---|
| 2811 |
|
|---|
| 2812 |
|
|---|
| 2813 |
|
|---|
| 2814 |
|
|---|
| 2815 |
|
|---|
| 2816 |
|
|---|
| 2817 |
|
|---|
| 2818 |
|
|---|
| 2819 |
|
|---|
| 2820 |
void ll_rw_block(int rw, int nr, struct buffer_head *bhs[]) |
|---|
| 2821 |
{ |
|---|
| 2822 |
int i; |
|---|
| 2823 |
|
|---|
| 2824 |
for (i = 0; i < nr; i++) { |
|---|
| 2825 |
struct buffer_head *bh = bhs[i]; |
|---|
| 2826 |
|
|---|
| 2827 |
if (test_set_buffer_locked(bh)) |
|---|
| 2828 |
continue; |
|---|
| 2829 |
|
|---|
| 2830 |
get_bh(bh); |
|---|
| 2831 |
if (rw == WRITE) { |
|---|
| 2832 |
bh->b_end_io = end_buffer_write_sync; |
|---|
| 2833 |
if (test_clear_buffer_dirty(bh)) { |
|---|
| 2834 |
submit_bh(WRITE, bh); |
|---|
| 2835 |
continue; |
|---|
| 2836 |
} |
|---|
| 2837 |
} else { |
|---|
| 2838 |
bh->b_end_io = end_buffer_read_sync; |
|---|
| 2839 |
if (!buffer_uptodate(bh)) { |
|---|
| 2840 |
submit_bh(rw, bh); |
|---|
| 2841 |
continue; |
|---|
| 2842 |
} |
|---|
| 2843 |
} |
|---|
| 2844 |
unlock_buffer(bh); |
|---|
| 2845 |
put_bh(bh); |
|---|
| 2846 |
} |
|---|
| 2847 |
} |
|---|
| 2848 |
|
|---|
| 2849 |
|
|---|
| 2850 |
|
|---|
| 2851 |
|
|---|
| 2852 |
|
|---|
| 2853 |
void sync_dirty_buffer(struct buffer_head *bh) |
|---|
| 2854 |
{ |
|---|
| 2855 |
WARN_ON(atomic_read(&bh->b_count) < 1); |
|---|
| 2856 |
lock_buffer(bh); |
|---|
| 2857 |
if (test_clear_buffer_dirty(bh)) { |
|---|
| 2858 |
get_bh(bh); |
|---|
| 2859 |
bh->b_end_io = end_buffer_write_sync; |
|---|
| 2860 |
submit_bh(WRITE, bh); |
|---|
| 2861 |
wait_on_buffer(bh); |
|---|
| 2862 |
} else { |
|---|
| 2863 |
unlock_buffer(bh); |
|---|
| 2864 |
} |
|---|
| 2865 |
} |
|---|
| 2866 |
|
|---|
| 2867 |
|
|---|
| 2868 |
|
|---|
| 2869 |
|
|---|
| 2870 |
|
|---|
| 2871 |
|
|---|
| 2872 |
|
|---|
| 2873 |
|
|---|
| 2874 |
|
|---|
| 2875 |
|
|---|
| 2876 |
|
|---|
| 2877 |
|
|---|
| 2878 |
|
|---|
| 2879 |
|
|---|
| 2880 |
|
|---|
| 2881 |
|
|---|
| 2882 |
|
|---|
| 2883 |
|
|---|
| 2884 |
|
|---|
| 2885 |
|
|---|
| 2886 |
|
|---|
| 2887 |
static inline int buffer_busy(struct buffer_head *bh) |
|---|
| 2888 |
{ |
|---|
| 2889 |
return atomic_read(&bh->b_count) | |
|---|
| 2890 |
(bh->b_state & ((1 << BH_Dirty) | (1 << BH_Lock))); |
|---|
| 2891 |
} |
|---|
| 2892 |
|
|---|
| 2893 |
static int |
|---|
| 2894 |
drop_buffers(struct page *page, struct buffer_head **buffers_to_free) |
|---|
| 2895 |
{ |
|---|
| 2896 |
struct buffer_head *head = page_buffers(page); |
|---|
| 2897 |
struct buffer_head *bh; |
|---|
| 2898 |
int was_uptodate = 1; |
|---|
| 2899 |
|
|---|
| 2900 |
bh = head; |
|---|
| 2901 |
do { |
|---|
| 2902 |
if (buffer_write_io_error(bh)) |
|---|
| 2903 |
set_bit(AS_EIO, &page->mapping->flags); |
|---|
| 2904 |
if (buffer_busy(bh)) |
|---|
| 2905 |
goto failed; |
|---|
| 2906 |
if (!buffer_uptodate(bh) && !buffer_req(bh)) |
|---|
| 2907 |
was_uptodate = 0; |
|---|
| 2908 |
bh = bh->b_this_page; |
|---|
| 2909 |
} while (bh != head); |
|---|
| 2910 |
|
|---|
| 2911 |
do { |
|---|
| 2912 |
struct buffer_head *next = bh->b_this_page; |
|---|
| 2913 |
|
|---|
| 2914 |
if (!list_empty(&bh->b_assoc_buffers)) |
|---|
| 2915 |
__remove_assoc_queue(bh); |
|---|
| 2916 |
bh = next; |
|---|
| 2917 |
} while (bh != head); |
|---|
| 2918 |
*buffers_to_free = head; |
|---|
| 2919 |
__clear_page_buffers(page); |
|---|
| 2920 |
return 1; |
|---|
| 2921 |
failed: |
|---|
| 2922 |
return 0; |
|---|
| 2923 |
} |
|---|
| 2924 |
|
|---|
| 2925 |
int try_to_free_buffers(struct page *page) |
|---|
| 2926 |
{ |
|---|
| 2927 |
struct address_space * const mapping = page->mapping; |
|---|
| 2928 |
struct buffer_head *buffers_to_free = NULL; |
|---|
| 2929 |
int ret = 0; |
|---|
| 2930 |
|
|---|
| 2931 |
BUG_ON(!PageLocked(page)); |
|---|
| 2932 |
if (PageWriteback(page)) |
|---|
| 2933 |
return 0; |
|---|
| 2934 |
|
|---|
| 2935 |
if (mapping == NULL) { |
|---|
| 2936 |
ret = drop_buffers(page, &buffers_to_free); |
|---|
| 2937 |
goto out; |
|---|
| 2938 |
} |
|---|
| 2939 |
|
|---|
| 2940 |
spin_lock(&mapping->private_lock); |
|---|
| 2941 |
ret = drop_buffers(page, &buffers_to_free); |
|---|
| 2942 |
if (ret) { |
|---|
| 2943 |
|
|---|
| 2944 |
|
|---|
| 2945 |
|
|---|
| 2946 |
|
|---|
| 2947 |
|
|---|
| 2948 |
|
|---|
| 2949 |
|
|---|
| 2950 |
|
|---|
| 2951 |
clear_page_dirty(page); |
|---|
| 2952 |
} |
|---|
| 2953 |
spin_unlock(&mapping->private_lock); |
|---|
| 2954 |
out: |
|---|
| 2955 |
if (buffers_to_free) { |
|---|
| 2956 |
struct buffer_head *bh = buffers_to_free; |
|---|
| 2957 |
|
|---|
| 2958 |
do { |
|---|
| 2959 |
struct buffer_head *next = bh->b_this_page; |
|---|
| 2960 |
free_buffer_head(bh); |
|---|
| 2961 |
bh = next; |
|---|
| 2962 |
} while (bh != buffers_to_free); |
|---|
| 2963 |
} |
|---|
| 2964 |
return ret; |
|---|
| 2965 |
} |
|---|
| 2966 |
EXPORT_SYMBOL(try_to_free_buffers); |
|---|
| 2967 |
|
|---|
| 2968 |
int block_sync_page(struct page *page) |
|---|
| 2969 |
{ |
|---|
| 2970 |
struct address_space *mapping; |
|---|
| 2971 |
|
|---|
| 2972 |
smp_mb(); |
|---|
| 2973 |
mapping = page_mapping(page); |
|---|
| 2974 |
if (mapping) |
|---|
| 2975 |
blk_run_backing_dev(mapping->backing_dev_info, page); |
|---|
| 2976 |
return 0; |
|---|
| 2977 |
} |
|---|
| 2978 |
|
|---|
| 2979 |
|
|---|
| 2980 |
|
|---|
| 2981 |
|
|---|
| 2982 |
|
|---|
| 2983 |
|
|---|
| 2984 |
|
|---|
| 2985 |
|
|---|
| 2986 |
asmlinkage long sys_bdflush(int func, long data) |
|---|
| 2987 |
{ |
|---|
| 2988 |
static int msg_count; |
|---|
| 2989 |
|
|---|
| 2990 |
if (!capable(CAP_SYS_ADMIN)) |
|---|
| 2991 |
return -EPERM; |
|---|
| 2992 |
|
|---|
| 2993 |
if (msg_count < 5) { |
|---|
| 2994 |
msg_count++; |
|---|
| 2995 |
printk(KERN_INFO |
|---|
| 2996 |
"warning: process `%s' used the obsolete bdflush" |
|---|
| 2997 |
" system call\n", current->comm); |
|---|
| 2998 |
printk(KERN_INFO "Fix your initscripts?\n"); |
|---|
| 2999 |
} |
|---|
| 3000 |
|
|---|
| 3001 |
if (func == 1) |
|---|
| 3002 |
do_exit(0); |
|---|
| 3003 |
return 0; |
|---|
| 3004 |
} |
|---|
| 3005 |
|
|---|
| 3006 |
|
|---|
| 3007 |
|
|---|
| 3008 |
|
|---|
| 3009 |
static kmem_cache_t *bh_cachep; |
|---|
| 3010 |
|
|---|
| 3011 |
|
|---|
| 3012 |
|
|---|
| 3013 |
|
|---|
| 3014 |
|
|---|
| 3015 |
static int max_buffer_heads; |
|---|
| 3016 |
|
|---|
| 3017 |
int buffer_heads_over_limit; |
|---|
| 3018 |
|
|---|
| 3019 |
struct bh_accounting { |
|---|
| 3020 |
int nr; |
|---|
| 3021 |
int ratelimit; |
|---|
| 3022 |
}; |
|---|
| 3023 |
|
|---|
| 3024 |
static DEFINE_PER_CPU(struct bh_accounting, bh_accounting) = {0, 0}; |
|---|
| 3025 |
|
|---|
| 3026 |
static void recalc_bh_state(void) |
|---|
| 3027 |
{ |
|---|
| 3028 |
int i; |
|---|
| 3029 |
int tot = 0; |
|---|
| 3030 |
|
|---|
| 3031 |
if (__get_cpu_var(bh_accounting).ratelimit++ < 4096) |
|---|
| 3032 |
return; |
|---|
| 3033 |
__get_cpu_var(bh_accounting).ratelimit = 0; |
|---|
| 3034 |
for_each_cpu(i) |
|---|
| 3035 |
tot += per_cpu(bh_accounting, i).nr; |
|---|
| 3036 |
buffer_heads_over_limit = (tot > max_buffer_heads); |
|---|
| 3037 |
} |
|---|
| 3038 |
|
|---|
| 3039 |
struct buffer_head *alloc_buffer_head(int gfp_flags) |
|---|
| 3040 |
{ |
|---|
| 3041 |
struct buffer_head *ret = kmem_cache_alloc(bh_cachep, gfp_flags); |
|---|
| 3042 |
if (ret) { |
|---|
| 3043 |
preempt_disable(); |
|---|
| 3044 |
__get_cpu_var(bh_accounting).nr++; |
|---|
| 3045 |
recalc_bh_state(); |
|---|
| 3046 |
preempt_enable(); |
|---|
| 3047 |
} |
|---|
| 3048 |
return ret; |
|---|
| 3049 |
} |
|---|
| 3050 |
EXPORT_SYMBOL(alloc_buffer_head); |
|---|
| 3051 |
|
|---|
| 3052 |
void free_buffer_head(struct buffer_head *bh) |
|---|
| 3053 |
{ |
|---|
| 3054 |
BUG_ON(!list_empty(&bh->b_assoc_buffers)); |
|---|
| 3055 |
kmem_cache_free(bh_cachep, bh); |
|---|
| 3056 |
preempt_disable(); |
|---|
| 3057 |
__get_cpu_var(bh_accounting).nr--; |
|---|
| 3058 |
recalc_bh_state(); |
|---|
| 3059 |
preempt_enable(); |
|---|
| 3060 |
} |
|---|
| 3061 |
EXPORT_SYMBOL(free_buffer_head); |
|---|
| 3062 |
|
|---|
| 3063 |
static void |
|---|
| 3064 |
init_buffer_head(void *data, kmem_cache_t *cachep, unsigned long flags) |
|---|
| 3065 |
{ |
|---|
| 3066 |
if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == |
|---|
| 3067 |
SLAB_CTOR_CONSTRUCTOR) { |
|---|
| 3068 |
struct buffer_head * bh = (struct buffer_head *)data; |
|---|
| 3069 |
|
|---|
| 3070 |
memset(bh, 0, sizeof(*bh)); |
|---|
| 3071 |
INIT_LIST_HEAD(&bh->b_assoc_buffers); |
|---|
| 3072 |
} |
|---|
| 3073 |
} |
|---|
| 3074 |
|
|---|
| 3075 |
#ifdef CONFIG_HOTPLUG_CPU |
|---|
| 3076 |
static void buffer_exit_cpu(int cpu) |
|---|
| 3077 |
{ |
|---|
| 3078 |
int i; |
|---|
| 3079 |
struct bh_lru *b = &per_cpu(bh_lrus, cpu); |
|---|
| 3080 |
|
|---|
| 3081 |
for (i = 0; i < BH_LRU_SIZE; i++) { |
|---|
| 3082 |
brelse(b->bhs[i]); |
|---|
| 3083 |
b->bhs[i] = NULL; |
|---|
| 3084 |
} |
|---|
| 3085 |
} |
|---|
| 3086 |
|
|---|
| 3087 |
static int buffer_cpu_notify(struct notifier_block *self, |
|---|
| 3088 |
unsigned long action, void *hcpu) |
|---|
| 3089 |
{ |
|---|
| 3090 |
if (action == CPU_DEAD) |
|---|
| 3091 |
buffer_exit_cpu((unsigned long)hcpu); |
|---|
| 3092 |
return NOTIFY_OK; |
|---|
| 3093 |
} |
|---|
| 3094 |
#endif |
|---|
| 3095 |
|
|---|
| 3096 |
void __init buffer_init(void) |
|---|
| 3097 |
{ |
|---|
| 3098 |
int i; |
|---|
| 3099 |
int nrpages; |
|---|
| 3100 |
|
|---|
| 3101 |
bh_cachep = kmem_cache_create("buffer_head", |
|---|
| 3102 |
sizeof(struct buffer_head), 0, |
|---|
| 3103 |
SLAB_PANIC, init_buffer_head, NULL); |
|---|
| 3104 |
for (i = 0; i < ARRAY_SIZE(bh_wait_queue_heads); i++) |
|---|
| 3105 |
init_waitqueue_head(&bh_wait_queue_heads[i].wqh); |
|---|
| 3106 |
|
|---|
| 3107 |
|
|---|
| 3108 |
|
|---|
| 3109 |
|
|---|
| 3110 |
nrpages = (nr_free_buffer_pages() * 10) / 100; |
|---|
| 3111 |
max_buffer_heads = nrpages * (PAGE_SIZE / sizeof(struct buffer_head)); |
|---|
| 3112 |
hotcpu_notifier(buffer_cpu_notify, 0); |
|---|
| 3113 |
} |
|---|
| 3114 |
|
|---|
| 3115 |
EXPORT_SYMBOL(__bforget); |
|---|
| 3116 |
EXPORT_SYMBOL(__brelse); |
|---|
| 3117 |
EXPORT_SYMBOL(__wait_on_buffer); |
|---|
| 3118 |
EXPORT_SYMBOL(block_commit_write); |
|---|
| 3119 |
EXPORT_SYMBOL(block_prepare_write); |
|---|
| 3120 |
EXPORT_SYMBOL(block_read_full_page); |
|---|
| 3121 |
EXPORT_SYMBOL(block_sync_page); |
|---|
| 3122 |
EXPORT_SYMBOL(block_truncate_page); |
|---|
| 3123 |
EXPORT_SYMBOL(block_write_full_page); |
|---|
| 3124 |
EXPORT_SYMBOL(buffer_insert_list); |
|---|
| 3125 |
EXPORT_SYMBOL(cont_prepare_write); |
|---|
| 3126 |
EXPORT_SYMBOL(end_buffer_async_write); |
|---|
| 3127 |
EXPORT_SYMBOL(end_buffer_read_sync); |
|---|
| 3128 |
EXPORT_SYMBOL(end_buffer_write_sync); |
|---|
| 3129 |
EXPORT_SYMBOL(file_fsync); |
|---|
| 3130 |
EXPORT_SYMBOL(fsync_bdev); |
|---|
| 3131 |
EXPORT_SYMBOL(fsync_buffers_list); |
|---|
| 3132 |
EXPORT_SYMBOL(generic_block_bmap); |
|---|
| 3133 |
EXPORT_SYMBOL(generic_commit_write); |
|---|
| 3134 |
EXPORT_SYMBOL(generic_cont_expand); |
|---|
| 3135 |
EXPORT_SYMBOL(init_buffer); |
|---|
| 3136 |
EXPORT_SYMBOL(invalidate_bdev); |
|---|
| 3137 |
EXPORT_SYMBOL(ll_rw_block); |
|---|
| 3138 |
EXPORT_SYMBOL(mark_buffer_dirty); |
|---|
| 3139 |
EXPORT_SYMBOL(submit_bh); |
|---|
| 3140 |
EXPORT_SYMBOL(sync_dirty_buffer); |
|---|
| 3141 |
EXPORT_SYMBOL(unlock_buffer); |
|---|