55 #include <linux/slab.h>
65 data_key_init(c, &
key, inode->
i_ino, block);
66 err = ubifs_tnc_lookup(c, &
key, dn);
84 if (err || len != out_len)
92 if (len < UBIFS_BLOCK_SIZE)
93 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
98 ubifs_err(
"bad data node (block %u, inode %lu)",
104 static int do_readpage(
struct page *
page)
108 unsigned int block, beyond;
110 struct inode *inode = page->
mapping->host;
111 loff_t
i_size = i_size_read(inode);
113 dbg_gen(
"ino %lu, pg %lu, i_size %lld, flags %#lx",
122 if (block >= beyond) {
124 SetPageChecked(page);
139 if (block >= beyond) {
142 memset(addr, 0, UBIFS_BLOCK_SIZE);
144 ret = read_block(inode, addr, block, dn);
149 }
else if (block + 1 == beyond) {
151 int ilen = i_size & (UBIFS_BLOCK_SIZE - 1);
153 if (ilen && ilen < dlen)
154 memset(addr + ilen, 0, dlen - ilen);
165 SetPageChecked(page);
169 ubifs_err(
"cannot read page %lu of inode %lu, error %d",
177 SetPageUptodate(page);
178 ClearPageError(page);
185 ClearPageUptodate(page);
199 static void release_new_page_budget(
struct ubifs_info *c)
213 static void release_existing_page_budget(
struct ubifs_info *c)
221 loff_t
pos,
unsigned len,
struct page **pagep,
224 struct inode *inode = mapping->
host;
231 dbg_gen(
"ino %lu, pos %llu, len %u, i_size %lld",
256 if (!PageUptodate(page)) {
258 SetPageChecked(page);
260 err = do_readpage(page);
268 SetPageUptodate(page);
269 ClearPageError(page);
272 if (PagePrivate(page))
283 release_new_page_budget(c);
284 else if (!PageChecked(page))
327 static int allocate_budget(
struct ubifs_info *c,
struct page *page,
332 if (PagePrivate(page)) {
359 if (PageChecked(page))
424 loff_t pos,
unsigned len,
unsigned flags,
425 struct page **pagep,
void **fsdata)
427 struct inode *inode = mapping->
host;
432 int skipped_read = 0;
446 if (!PageUptodate(page)) {
458 SetPageChecked(page);
461 err = do_readpage(page);
469 SetPageUptodate(page);
470 ClearPageError(page);
473 err = allocate_budget(c, page, ui, appending);
481 ClearPageChecked(page);
482 ClearPageUptodate(page);
498 return write_begin_slow(mapping, pos, len, pagep, flags);
522 static void cancel_budget(
struct ubifs_info *c,
struct page *page,
530 if (!PagePrivate(page)) {
531 if (PageChecked(page))
532 release_new_page_budget(c);
534 release_existing_page_budget(c);
538 static int ubifs_write_end(
struct file *file,
struct address_space *mapping,
539 loff_t pos,
unsigned len,
unsigned copied,
540 struct page *page,
void *fsdata)
542 struct inode *inode = mapping->
host;
545 loff_t end_pos = pos + len;
546 int appending = !!(end_pos > inode->
i_size);
548 dbg_gen(
"ino %lu, pos %llu, pg %lu, len %u, copied %d, i_size %lld",
561 dbg_gen(
"copied %d instead of %d, read page and repeat",
563 cancel_budget(c, page, ui, appending);
564 ClearPageChecked(page);
570 copied = do_readpage(page);
574 if (!PagePrivate(page)) {
575 SetPagePrivate(page);
581 i_size_write(inode, end_pos);
608 static int populate_page(
struct ubifs_info *c,
struct page *page,
612 struct inode *inode = page->
mapping->host;
613 loff_t i_size = i_size_read(inode);
614 unsigned int page_block;
618 dbg_gen(
"ino %lu, pg %lu, i_size %lld, flags %#lx",
621 addr = zaddr =
kmap(page);
623 end_index = (i_size - 1) >> PAGE_CACHE_SHIFT;
624 if (!i_size || page->
index > end_index) {
636 memset(addr, 0, UBIFS_BLOCK_SIZE);
637 }
else if (key_block(c, &bu->
zbranch[
nn].key) == page_block) {
646 if (len <= 0 || len > UBIFS_BLOCK_SIZE)
653 if (err || len != out_len)
656 if (len < UBIFS_BLOCK_SIZE)
657 memset(addr + len, 0, UBIFS_BLOCK_SIZE - len);
661 }
else if (key_block(c, &bu->
zbranch[
nn].key) < page_block) {
666 memset(addr, 0, UBIFS_BLOCK_SIZE);
674 if (end_index == page->
index) {
677 if (len && len <
read)
683 SetPageChecked(page);
687 SetPageUptodate(page);
688 ClearPageError(page);
695 ClearPageUptodate(page);
699 ubifs_err(
"bad data node (block %u, inode %lu)",
700 page_block, inode->
i_ino);
717 struct inode *inode = mapping->
host;
719 int err, page_idx, page_cnt, ret = 0, n = 0;
720 int allocate = bu->
buf ? 0 : 1;
765 err = populate_page(c, page1, bu, &n);
772 isize = i_size_read(inode);
775 end_index = ((isize - 1) >> PAGE_CACHE_SHIFT);
777 for (page_idx = 1; page_idx < page_cnt; page_idx++) {
781 if (page_offset > end_index)
787 if (!PageUptodate(page))
788 err = populate_page(c, page, bu, &n);
803 ubifs_warn(
"ignoring error %d and skipping bulk-read", err);
820 static int ubifs_bulk_read(
struct page *page)
822 struct inode *inode = page->
mapping->host;
827 int err = 0, allocated = 0;
840 if (index != last_page_read + 1) {
872 data_key_init(c, &bu->
key, inode->
i_ino,
874 err = ubifs_do_bulk_read(c, bu, page);
886 static int ubifs_readpage(
struct file *file,
struct page *page)
888 if (ubifs_bulk_read(page))
895 static int do_writepage(
struct page *page,
int len)
897 int err = 0,
i, blen;
901 struct inode *inode = page->
mapping->host;
911 set_page_writeback(page);
917 blen =
min_t(
int, len, UBIFS_BLOCK_SIZE);
918 data_key_init(c, &
key, inode->
i_ino, block);
930 ubifs_err(
"cannot write page %lu of inode %lu, error %d",
936 if (PageChecked(page))
937 release_new_page_budget(c);
939 release_existing_page_budget(c);
942 ClearPagePrivate(page);
943 ClearPageChecked(page);
999 struct inode *inode = page->
mapping->host;
1006 dbg_gen(
"ino %lu, pg %lu, pg flags %#lx",
1011 if (page->
index > end_index || (page->
index == end_index && !len)) {
1021 if (page->
index < end_index) {
1023 err = inode->
i_sb->s_op->write_inode(inode,
NULL);
1051 err = inode->
i_sb->s_op->write_inode(inode,
NULL);
1056 return do_writepage(page, len);
1068 static void do_attr_changes(
struct inode *inode,
const struct iattr *
attr)
1076 inode->
i_sb->s_time_gran);
1079 inode->
i_sb->s_time_gran);
1082 inode->
i_sb->s_time_gran);
1102 static int do_truncation(
struct ubifs_info *c,
struct inode *inode,
1103 const struct iattr *attr)
1108 int offset = new_size & (UBIFS_BLOCK_SIZE - 1), budgeted = 1;
1111 dbg_gen(
"ino %lu, size %lld -> %lld", inode->
i_ino, old_size, new_size);
1119 if (new_size & (UBIFS_BLOCK_SIZE - 1))
1131 if (new_size || err != -
ENOSPC)
1144 if (PageDirty(page)) {
1159 err = do_writepage(page, offset);
1184 do_attr_changes(inode, attr);
1192 c->
bi.nospace = c->
bi.nospace_rp = 0;
1208 static int do_setattr(
struct ubifs_info *c,
struct inode *inode,
1209 const struct iattr *attr)
1212 loff_t new_size = attr->
ia_size;
1234 do_attr_changes(inode, attr);
1236 release = ui->
dirty;
1244 mark_inode_dirty_sync(inode);
1250 err = inode->
i_sb->s_op->write_inode(inode,
NULL);
1257 struct inode *inode = dentry->
d_inode;
1260 dbg_gen(
"ino %lu, mode %#x, ia_valid %#x",
1272 err = do_truncation(c, inode, attr);
1274 err = do_setattr(c, inode, attr);
1279 static void ubifs_invalidatepage(
struct page *page,
unsigned long offset)
1281 struct inode *inode = page->
mapping->host;
1289 if (PageChecked(page))
1290 release_new_page_budget(c);
1292 release_existing_page_budget(c);
1295 ClearPagePrivate(page);
1296 ClearPageChecked(page);
1303 nd_set_link(nd, ui->
data);
1309 struct inode *inode = file->
f_mapping->host;
1329 err = inode->
i_sb->s_op->write_inode(inode,
NULL);
1353 static inline int mctime_update_needed(
const struct inode *inode,
1356 if (!timespec_equal(&inode->
i_mtime, now) ||
1357 !timespec_equal(&inode->
i_ctime, now))
1371 static int update_mctime(
struct ubifs_info *c,
struct inode *inode)
1373 struct timespec now = ubifs_current_time(inode);
1376 if (mctime_update_needed(inode, &now)) {
1387 release = ui->
dirty;
1388 mark_inode_dirty_sync(inode);
1398 unsigned long nr_segs, loff_t pos)
1401 struct inode *inode = iocb->
ki_filp->f_mapping->host;
1404 err = update_mctime(c, inode);
1411 static int ubifs_set_page_dirty(
struct page *page)
1424 static int ubifs_releasepage(
struct page *page,
gfp_t unused_gfp_flags)
1430 if (PageWriteback(page))
1434 ClearPagePrivate(page);
1435 ClearPageChecked(page);
1444 struct vm_fault *vmf)
1446 struct page *page = vmf->page;
1447 struct inode *inode = vma->
vm_file->f_path.dentry->d_inode;
1449 struct timespec now = ubifs_current_time(inode);
1451 int err, update_time;
1454 i_size_read(inode));
1458 return VM_FAULT_SIGBUS;
1478 update_time = mctime_update_needed(inode, &now);
1489 ubifs_warn(
"out of space for mmapped file (inode number %lu)",
1491 return VM_FAULT_SIGBUS;
1496 page_offset(page) > i_size_read(inode))) {
1502 if (PagePrivate(page))
1503 release_new_page_budget(c);
1505 if (!PageChecked(page))
1507 SetPagePrivate(page);
1518 release = ui->
dirty;
1519 mark_inode_dirty_sync(inode);
1532 err = VM_FAULT_SIGBUS;
1536 static const struct vm_operations_struct ubifs_file_vm_ops = {
1538 .page_mkwrite = ubifs_vm_page_mkwrite,
1542 static int ubifs_file_mmap(
struct file *file,
struct vm_area_struct *vma)
1549 vma->
vm_ops = &ubifs_file_vm_ops;
1554 .readpage = ubifs_readpage,
1555 .writepage = ubifs_writepage,
1556 .write_begin = ubifs_write_begin,
1557 .write_end = ubifs_write_end,
1558 .invalidatepage = ubifs_invalidatepage,
1559 .set_page_dirty = ubifs_set_page_dirty,
1560 .releasepage = ubifs_releasepage,
1574 .follow_link = ubifs_follow_link,
1584 .aio_write = ubifs_aio_write,
1585 .mmap = ubifs_file_mmap,
1590 #ifdef CONFIG_COMPAT
1591 .compat_ioctl = ubifs_compat_ioctl,