24 #include <linux/errno.h>
59 static void ntfs_end_buffer_async_read(
struct buffer_head *bh,
int uptodate)
66 int page_uptodate = 1;
76 set_buffer_uptodate(bh);
82 i_size = i_size_read(vi);
89 if (
unlikely(file_ofs + bh->b_size > init_size)) {
94 if (file_ofs < init_size)
95 ofs = init_size - file_ofs;
98 memset(kaddr + bh_offset(bh) + ofs, 0,
105 clear_buffer_uptodate(bh);
108 "0x%llx.", (
unsigned long long)bh->b_blocknr);
110 first = page_buffers(page);
112 bit_spin_lock(BH_Uptodate_Lock, &first->b_state);
113 clear_buffer_async_read(bh);
117 if (!buffer_uptodate(tmp))
119 if (buffer_async_read(tmp)) {
120 if (
likely(buffer_locked(tmp)))
125 tmp = tmp->b_this_page;
127 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
137 if (!NInoMstProtected(ni)) {
138 if (
likely(page_uptodate && !PageError(page)))
139 SetPageUptodate(page);
142 unsigned int i, recs;
151 for (i = 0; i < recs; i++)
153 i * rec_size), rec_size);
157 if (
likely(page_uptodate && !PageError(page)))
158 SetPageUptodate(page);
163 bit_spin_unlock(BH_Uptodate_Lock, &first->b_state);
185 static int ntfs_read_block(
struct page *page)
195 struct buffer_head *bh, *
head, *arr[MAX_BUF_PER_PAGE];
198 unsigned int blocksize, vcn_ofs;
200 unsigned char blocksize_bits;
209 blocksize = vol->
sb->s_blocksize;
210 blocksize_bits = vol->
sb->s_blocksize_bits;
212 if (!page_has_buffers(page)) {
214 if (
unlikely(!page_has_buffers(page))) {
219 bh = head = page_buffers(page);
237 i_size = i_size_read(vi);
243 zblock = (init_size + blocksize - 1) >> blocksize_bits;
257 bh->b_bdev = vol->
sb->s_bdev;
259 if (iblock < lblock) {
260 bool is_retry =
false;
263 vcn = (
VCN)iblock << blocksize_bits >>
265 vcn_ofs = ((
VCN)iblock << blocksize_bits) &
283 + vcn_ofs) >> blocksize_bits;
284 set_buffer_mapped(bh);
286 if (iblock < zblock) {
306 goto lock_retry_remap;
325 "attribute type 0x%x, vcn 0x%llx, "
326 "offset 0x%x because its location on "
327 "disk could not be determined%s "
328 "(error code %i).", ni->
mft_no,
329 ni->
type, (
unsigned long long)vcn,
330 vcn_ofs, is_retry ?
" even after "
331 "retrying" :
"", err);
339 bh->b_blocknr = -1
UL;
340 clear_buffer_mapped(bh);
342 zero_user(page, i * blocksize, blocksize);
344 set_buffer_uptodate(bh);
345 }
while (i++, iblock++, (bh = bh->b_this_page) != head);
353 struct buffer_head *tbh;
356 for (i = 0; i <
nr; i++) {
359 tbh->b_end_io = ntfs_end_buffer_async_read;
360 set_buffer_async_read(tbh);
363 for (i = 0; i <
nr; i++) {
365 if (
likely(!buffer_uptodate(tbh)))
368 ntfs_end_buffer_async_read(tbh, 1);
373 if (
likely(!PageError(page)))
374 SetPageUptodate(page);
399 static int ntfs_readpage(
struct file *
file,
struct page *page)
412 BUG_ON(!PageLocked(page));
414 i_size = i_size_read(vi);
419 ntfs_debug(
"Read outside i_size - truncated?");
426 if (PageUptodate(page)) {
441 if (NInoEncrypted(ni)) {
447 if (NInoNonResident(ni) && NInoCompressed(ni)) {
454 if (NInoNonResident(ni)) {
456 return ntfs_read_block(page);
484 if (
unlikely(NInoNonResident(ni))) {
496 goto put_unm_err_out;
501 i_size = i_size_read(vi);
521 SetPageUptodate(page);
562 struct buffer_head *bh, *
head;
564 unsigned int blocksize, vcn_ofs;
566 bool need_end_writeback;
567 unsigned char blocksize_bits;
573 ntfs_debug(
"Entering for inode 0x%lx, attribute type 0x%x, page index "
576 BUG_ON(!NInoNonResident(ni));
577 BUG_ON(NInoMstProtected(ni));
578 blocksize = vol->
sb->s_blocksize;
579 blocksize_bits = vol->
sb->s_blocksize_bits;
580 if (!page_has_buffers(page)) {
581 BUG_ON(!PageUptodate(page));
583 (1 << BH_Uptodate) | (1 << BH_Dirty));
584 if (
unlikely(!page_has_buffers(page))) {
586 "buffers. Redirtying page so we try "
597 bh = head = page_buffers(page);
606 i_size = i_size_read(vi);
611 dblock = (i_size + blocksize - 1) >> blocksize_bits;
614 iblock = initialized_size >> blocksize_bits;
634 bool is_retry =
false;
648 clear_buffer_dirty(bh);
649 set_buffer_uptodate(bh);
654 if (!buffer_dirty(bh))
659 (initialized_size < i_size))) {
666 if (block > iblock) {
691 if (!PageUptodate(page)) {
703 "is not supported yet. Sorry.");
713 if (buffer_mapped(bh))
717 bh->b_bdev = vol->
sb->s_bdev;
720 vcn = (
VCN)block << blocksize_bits;
739 vcn_ofs) >> blocksize_bits;
740 set_buffer_mapped(bh);
746 unsigned long *
bpos, *bend;
750 bpos = (
unsigned long *)(kaddr + bh_offset(bh));
751 bend = (
unsigned long *)((
u8*)bpos + blocksize);
755 }
while (
likely(++bpos < bend));
763 clear_buffer_dirty(bh);
770 "not supported yet. Sorry.");
784 goto lock_retry_remap;
795 clear_buffer_dirty(bh);
796 zero_user(page, bh_offset(bh), blocksize);
797 set_buffer_uptodate(bh);
806 "attribute type 0x%x, vcn 0x%llx, offset 0x%x "
807 "because its location on disk could not be "
808 "determined%s (error code %i).", ni->
mft_no,
809 ni->
type, (
unsigned long long)vcn,
810 vcn_ofs, is_retry ?
" even after "
811 "retrying" :
"", err);
813 }
while (block++, (bh = bh->b_this_page) != head);
823 if (
unlikely(!PageUptodate(page))) {
826 if (!buffer_uptodate(bh)) {
831 }
while ((bh = bh->b_this_page) != head);
833 SetPageUptodate(page);
838 if (buffer_mapped(bh) && buffer_dirty(bh)) {
840 if (test_clear_buffer_dirty(bh)) {
841 BUG_ON(!buffer_uptodate(bh));
851 clear_buffer_dirty(bh);
853 }
while ((bh = bh->b_this_page) != head);
859 else if (err == -
ENOMEM) {
861 "Redirtying page so we try again "
873 BUG_ON(PageWriteback(page));
874 set_page_writeback(page);
877 need_end_writeback =
true;
879 struct buffer_head *
next = bh->b_this_page;
880 if (buffer_async_write(bh)) {
882 need_end_writeback =
false;
885 }
while (bh != head);
920 static int ntfs_write_mst_block(
struct page *page,
928 unsigned int rec_size = ni->
itype.
index.block_size;
930 struct buffer_head *bh, *
head, *tbh, *rec_start_bh;
931 struct buffer_head *bhs[MAX_BUF_PER_PAGE];
933 int i, nr_locked_nis, nr_recs, nr_bhs, max_bhs, bhs_per_rec,
err, err2;
934 unsigned bh_size, rec_size_bits;
935 bool sync, is_mft, page_is_dirty, rec_is_dirty;
936 unsigned char bh_size_bits;
938 ntfs_debug(
"Entering for inode 0x%lx, attribute type 0x%x, page index "
940 BUG_ON(!NInoNonResident(ni));
941 BUG_ON(!NInoMstProtected(ni));
951 bh_size = vol->
sb->s_blocksize;
952 bh_size_bits = vol->
sb->s_blocksize_bits;
955 BUG_ON(max_bhs > MAX_BUF_PER_PAGE);
961 bh = head = page_buffers(page);
964 rec_size_bits = ni->
itype.
index.block_size_bits;
966 bhs_per_rec = rec_size >> bh_size_bits;
974 dblock = (i_size_read(vi) + bh_size - 1) >> bh_size_bits;
977 err = err2 = nr_bhs = nr_recs = nr_locked_nis = 0;
978 page_is_dirty = rec_is_dirty =
false;
981 bool is_retry =
false;
983 if (
likely(block < rec_block)) {
985 clear_buffer_dirty(bh);
986 set_buffer_uptodate(bh);
998 clear_buffer_dirty(bh);
1002 BUG_ON(block > rec_block);
1004 rec_block += bhs_per_rec;
1007 clear_buffer_dirty(bh);
1010 if (!buffer_dirty(bh)) {
1012 rec_is_dirty =
false;
1015 rec_is_dirty =
true;
1019 if (
unlikely(!buffer_mapped(bh))) {
1022 unsigned int vcn_ofs;
1024 bh->b_bdev = vol->
sb->s_bdev;
1026 vcn = (
VCN)block << bh_size_bits;
1044 bh->b_blocknr = ((lcn <<
1046 vcn_ofs) >> bh_size_bits;
1047 set_buffer_mapped(bh);
1054 if (!is_mft && !is_retry &&
1064 goto lock_retry_remap;
1066 page_is_dirty =
true;
1074 if (!err || err == -
ENOMEM)
1078 "0x%llx (inode 0x%lx, "
1079 "attribute type 0x%x) because "
1080 "its location on disk could "
1081 "not be determined (error "
1094 if (rec_start_bh != bh) {
1095 while (bhs[--nr_bhs] != rec_start_bh)
1101 }
while ((rec_start_bh =
1110 BUG_ON(!buffer_uptodate(bh));
1111 BUG_ON(nr_bhs >= max_bhs);
1113 }
while (block++, (bh = bh->b_this_page) != head);
1122 BUG_ON(!PageUptodate(page));
1123 ClearPageUptodate(page);
1124 for (i = 0; i < nr_bhs; i++) {
1128 if (i % bhs_per_rec)
1131 ofs = bh_offset(tbh);
1134 unsigned long mft_no;
1141 if (!ntfs_may_write_mft_record(vol, mft_no,
1142 (MFT_RECORD*)(kaddr + ofs), &tni)) {
1148 page_is_dirty =
true;
1155 }
while (++i % bhs_per_rec);
1164 locked_nis[nr_locked_nis++] = tni;
1170 if (!err || err == -
ENOMEM)
1173 "(inode 0x%lx, attribute type 0x%x, "
1174 "page index 0x%lx, page offset 0x%x)!"
1175 " Unmount and run chkdsk.", vi->
i_ino,
1182 clear_buffer_dirty(bhs[i]);
1184 }
while (++i % bhs_per_rec);
1194 for (i = 0; i < nr_bhs; i++) {
1198 if (!trylock_buffer(tbh))
1201 clear_buffer_dirty(tbh);
1202 BUG_ON(!buffer_uptodate(tbh));
1203 BUG_ON(!buffer_mapped(tbh));
1209 if (is_mft && !sync)
1213 for (i = 0; i < nr_bhs; i++) {
1217 wait_on_buffer(tbh);
1218 if (
unlikely(!buffer_uptodate(tbh))) {
1220 "record buffer (inode 0x%lx, "
1221 "attribute type 0x%x, page index "
1222 "0x%lx, page offset 0x%lx)! Unmount "
1224 page->
index, bh_offset(tbh));
1225 if (!err || err == -
ENOMEM)
1231 set_buffer_uptodate(tbh);
1235 if (is_mft && sync) {
1237 for (i = 0; i < nr_bhs; i++) {
1238 unsigned long mft_no;
1245 if (i % bhs_per_rec)
1251 ofs = bh_offset(tbh);
1255 if (mft_no < vol->mftmirr_size)
1256 ntfs_sync_mft_mirror(vol, mft_no,
1257 (MFT_RECORD*)(kaddr + ofs),
1264 for (i = 0; i < nr_bhs; i++) {
1265 if (!(i % bhs_per_rec)) {
1276 while (nr_locked_nis-- > 0) {
1279 tni = locked_nis[nr_locked_nis];
1290 tni == base_tni ?
"base" :
"extent",
1294 iput(VFS_I(base_tni));
1296 SetPageUptodate(page);
1308 if (page_is_dirty) {
1309 ntfs_debug(
"Page still contains one or more dirty ntfs "
1310 "records. Redirtying the page starting at "
1311 "record 0x%lx.", page->
index <<
1321 BUG_ON(PageWriteback(page));
1322 set_page_writeback(page);
1361 MFT_RECORD *
m =
NULL;
1366 BUG_ON(!PageLocked(page));
1367 i_size = i_size_read(vi);
1377 ntfs_debug(
"Write outside i_size - truncated?");
1390 if (NInoEncrypted(ni)) {
1393 ntfs_debug(
"Denying write access to encrypted file.");
1397 if (NInoNonResident(ni) && NInoCompressed(ni)) {
1404 "not supported yet. Sorry.");
1408 if (NInoNonResident(ni) && NInoSparse(ni)) {
1411 "supported yet. Sorry.");
1416 if (NInoNonResident(ni)) {
1424 if (NInoMstProtected(ni))
1425 return ntfs_write_mst_block(page, wbc);
1427 return ntfs_write_block(page, wbc);
1437 BUG_ON(page_has_buffers(page));
1438 BUG_ON(!PageUptodate(page));
1441 "Aborting write.", page->
index);
1442 BUG_ON(PageWriteback(page));
1443 set_page_writeback(page);
1464 if (
unlikely(NInoNonResident(ni))) {
1466 goto retry_writepage;
1481 BUG_ON(PageWriteback(page));
1482 set_page_writeback(page);
1485 i_size = i_size_read(vi);
1493 err = ntfs_resident_attr_value_resize(ctx->
mrec, ctx->
attr,
1507 flush_dcache_mft_record_page(ctx->
ntfs_ino);
1511 mark_mft_record_dirty(ctx->
ntfs_ino);
1518 "page so we try again later.");
1529 NVolSetErrors(ni->vol);
1545 .readpage = ntfs_readpage,
1547 .writepage = ntfs_writepage,
1560 .readpage = ntfs_readpage,
1562 .writepage = ntfs_writepage,
1590 void mark_ntfs_record_dirty(
struct page *page,
const unsigned int ofs) {
1593 struct buffer_head *bh, *
head, *buffers_to_free =
NULL;
1594 unsigned int end, bh_size, bh_ofs;
1596 BUG_ON(!PageUptodate(page));
1598 bh_size = VFS_I(ni)->i_sb->s_blocksize;
1600 if (
unlikely(!page_has_buffers(page))) {
1604 if (
likely(!page_has_buffers(page))) {
1605 struct buffer_head *
tail;
1608 set_buffer_uptodate(bh);
1610 bh = bh->b_this_page;
1612 tail->b_this_page =
head;
1613 attach_page_buffers(page, head);
1615 buffers_to_free = bh;
1617 bh = head = page_buffers(page);
1620 bh_ofs = bh_offset(bh);
1621 if (bh_ofs + bh_size <= ofs)
1625 set_buffer_dirty(bh);
1626 }
while ((bh = bh->b_this_page) != head);
1631 bh = buffers_to_free->b_this_page;
1633 buffers_to_free = bh;
1634 }
while (buffers_to_free);