0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/buffer_head.h>
0010 #include <linux/slab.h>
0011 #include <linux/swap.h>
0012 #include <linux/bio.h>
0013
0014 #include "attrib.h"
0015 #include "aops.h"
0016 #include "bitmap.h"
0017 #include "debug.h"
0018 #include "dir.h"
0019 #include "lcnalloc.h"
0020 #include "malloc.h"
0021 #include "mft.h"
0022 #include "ntfs.h"
0023
0024 #define MAX_BHS (PAGE_SIZE / NTFS_BLOCK_SIZE)
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 static inline MFT_RECORD *map_mft_record_page(ntfs_inode *ni)
0037 {
0038 loff_t i_size;
0039 ntfs_volume *vol = ni->vol;
0040 struct inode *mft_vi = vol->mft_ino;
0041 struct page *page;
0042 unsigned long index, end_index;
0043 unsigned ofs;
0044
0045 BUG_ON(ni->page);
0046
0047
0048
0049
0050
0051
0052 index = (u64)ni->mft_no << vol->mft_record_size_bits >>
0053 PAGE_SHIFT;
0054 ofs = (ni->mft_no << vol->mft_record_size_bits) & ~PAGE_MASK;
0055
0056 i_size = i_size_read(mft_vi);
0057
0058 end_index = i_size >> PAGE_SHIFT;
0059
0060
0061 if (unlikely(index >= end_index)) {
0062 if (index > end_index || (i_size & ~PAGE_MASK) < ofs +
0063 vol->mft_record_size) {
0064 page = ERR_PTR(-ENOENT);
0065 ntfs_error(vol->sb, "Attempt to read mft record 0x%lx, "
0066 "which is beyond the end of the mft. "
0067 "This is probably a bug in the ntfs "
0068 "driver.", ni->mft_no);
0069 goto err_out;
0070 }
0071 }
0072
0073 page = ntfs_map_page(mft_vi->i_mapping, index);
0074 if (!IS_ERR(page)) {
0075
0076 if (likely(ntfs_is_mft_recordp((le32*)(page_address(page) +
0077 ofs)))) {
0078 ni->page = page;
0079 ni->page_ofs = ofs;
0080 return page_address(page) + ofs;
0081 }
0082 ntfs_error(vol->sb, "Mft record 0x%lx is corrupt. "
0083 "Run chkdsk.", ni->mft_no);
0084 ntfs_unmap_page(page);
0085 page = ERR_PTR(-EIO);
0086 NVolSetErrors(vol);
0087 }
0088 err_out:
0089 ni->page = NULL;
0090 ni->page_ofs = 0;
0091 return (void*)page;
0092 }
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144 MFT_RECORD *map_mft_record(ntfs_inode *ni)
0145 {
0146 MFT_RECORD *m;
0147
0148 ntfs_debug("Entering for mft_no 0x%lx.", ni->mft_no);
0149
0150
0151 atomic_inc(&ni->count);
0152
0153
0154 mutex_lock(&ni->mrec_lock);
0155
0156 m = map_mft_record_page(ni);
0157 if (!IS_ERR(m))
0158 return m;
0159
0160 mutex_unlock(&ni->mrec_lock);
0161 atomic_dec(&ni->count);
0162 ntfs_error(ni->vol->sb, "Failed with error code %lu.", -PTR_ERR(m));
0163 return m;
0164 }
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175
0176
0177
0178
0179
0180 static inline void unmap_mft_record_page(ntfs_inode *ni)
0181 {
0182 BUG_ON(!ni->page);
0183
0184
0185 ntfs_unmap_page(ni->page);
0186 ni->page = NULL;
0187 ni->page_ofs = 0;
0188 return;
0189 }
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
0202 void unmap_mft_record(ntfs_inode *ni)
0203 {
0204 struct page *page = ni->page;
0205
0206 BUG_ON(!page);
0207
0208 ntfs_debug("Entering for mft_no 0x%lx.", ni->mft_no);
0209
0210 unmap_mft_record_page(ni);
0211 mutex_unlock(&ni->mrec_lock);
0212 atomic_dec(&ni->count);
0213
0214
0215
0216
0217
0218
0219 return;
0220 }
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235 MFT_RECORD *map_extent_mft_record(ntfs_inode *base_ni, MFT_REF mref,
0236 ntfs_inode **ntfs_ino)
0237 {
0238 MFT_RECORD *m;
0239 ntfs_inode *ni = NULL;
0240 ntfs_inode **extent_nis = NULL;
0241 int i;
0242 unsigned long mft_no = MREF(mref);
0243 u16 seq_no = MSEQNO(mref);
0244 bool destroy_ni = false;
0245
0246 ntfs_debug("Mapping extent mft record 0x%lx (base mft record 0x%lx).",
0247 mft_no, base_ni->mft_no);
0248
0249 atomic_inc(&base_ni->count);
0250
0251
0252
0253
0254
0255 mutex_lock(&base_ni->extent_lock);
0256 if (base_ni->nr_extents > 0) {
0257 extent_nis = base_ni->ext.extent_ntfs_inos;
0258 for (i = 0; i < base_ni->nr_extents; i++) {
0259 if (mft_no != extent_nis[i]->mft_no)
0260 continue;
0261 ni = extent_nis[i];
0262
0263 atomic_inc(&ni->count);
0264 break;
0265 }
0266 }
0267 if (likely(ni != NULL)) {
0268 mutex_unlock(&base_ni->extent_lock);
0269 atomic_dec(&base_ni->count);
0270
0271 m = map_mft_record(ni);
0272
0273 atomic_dec(&ni->count);
0274 if (!IS_ERR(m)) {
0275
0276 if (likely(le16_to_cpu(m->sequence_number) == seq_no)) {
0277 ntfs_debug("Done 1.");
0278 *ntfs_ino = ni;
0279 return m;
0280 }
0281 unmap_mft_record(ni);
0282 ntfs_error(base_ni->vol->sb, "Found stale extent mft "
0283 "reference! Corrupt filesystem. "
0284 "Run chkdsk.");
0285 return ERR_PTR(-EIO);
0286 }
0287 map_err_out:
0288 ntfs_error(base_ni->vol->sb, "Failed to map extent "
0289 "mft record, error code %ld.", -PTR_ERR(m));
0290 return m;
0291 }
0292
0293 ni = ntfs_new_extent_inode(base_ni->vol->sb, mft_no);
0294 if (unlikely(!ni)) {
0295 mutex_unlock(&base_ni->extent_lock);
0296 atomic_dec(&base_ni->count);
0297 return ERR_PTR(-ENOMEM);
0298 }
0299 ni->vol = base_ni->vol;
0300 ni->seq_no = seq_no;
0301 ni->nr_extents = -1;
0302 ni->ext.base_ntfs_ino = base_ni;
0303
0304 m = map_mft_record(ni);
0305 if (IS_ERR(m)) {
0306 mutex_unlock(&base_ni->extent_lock);
0307 atomic_dec(&base_ni->count);
0308 ntfs_clear_extent_inode(ni);
0309 goto map_err_out;
0310 }
0311
0312 if (seq_no && (le16_to_cpu(m->sequence_number) != seq_no)) {
0313 ntfs_error(base_ni->vol->sb, "Found stale extent mft "
0314 "reference! Corrupt filesystem. Run chkdsk.");
0315 destroy_ni = true;
0316 m = ERR_PTR(-EIO);
0317 goto unm_err_out;
0318 }
0319
0320 if (!(base_ni->nr_extents & 3)) {
0321 ntfs_inode **tmp;
0322 int new_size = (base_ni->nr_extents + 4) * sizeof(ntfs_inode *);
0323
0324 tmp = kmalloc(new_size, GFP_NOFS);
0325 if (unlikely(!tmp)) {
0326 ntfs_error(base_ni->vol->sb, "Failed to allocate "
0327 "internal buffer.");
0328 destroy_ni = true;
0329 m = ERR_PTR(-ENOMEM);
0330 goto unm_err_out;
0331 }
0332 if (base_ni->nr_extents) {
0333 BUG_ON(!base_ni->ext.extent_ntfs_inos);
0334 memcpy(tmp, base_ni->ext.extent_ntfs_inos, new_size -
0335 4 * sizeof(ntfs_inode *));
0336 kfree(base_ni->ext.extent_ntfs_inos);
0337 }
0338 base_ni->ext.extent_ntfs_inos = tmp;
0339 }
0340 base_ni->ext.extent_ntfs_inos[base_ni->nr_extents++] = ni;
0341 mutex_unlock(&base_ni->extent_lock);
0342 atomic_dec(&base_ni->count);
0343 ntfs_debug("Done 2.");
0344 *ntfs_ino = ni;
0345 return m;
0346 unm_err_out:
0347 unmap_mft_record(ni);
0348 mutex_unlock(&base_ni->extent_lock);
0349 atomic_dec(&base_ni->count);
0350
0351
0352
0353
0354 if (destroy_ni)
0355 ntfs_clear_extent_inode(ni);
0356 return m;
0357 }
0358
0359 #ifdef NTFS_RW
0360
0361
0362
0363
0364
0365
0366
0367
0368
0369
0370
0371
0372
0373
0374
0375
0376
0377
0378
0379
0380
0381
0382
0383
0384 void __mark_mft_record_dirty(ntfs_inode *ni)
0385 {
0386 ntfs_inode *base_ni;
0387
0388 ntfs_debug("Entering for inode 0x%lx.", ni->mft_no);
0389 BUG_ON(NInoAttr(ni));
0390 mark_ntfs_record_dirty(ni->page, ni->page_ofs);
0391
0392 mutex_lock(&ni->extent_lock);
0393 if (likely(ni->nr_extents >= 0))
0394 base_ni = ni;
0395 else
0396 base_ni = ni->ext.base_ntfs_ino;
0397 mutex_unlock(&ni->extent_lock);
0398 __mark_inode_dirty(VFS_I(base_ni), I_DIRTY_DATASYNC);
0399 }
0400
0401 static const char *ntfs_please_email = "Please email "
0402 "linux-ntfs-dev@lists.sourceforge.net and say that you saw "
0403 "this message. Thank you.";
0404
0405
0406
0407
0408
0409
0410
0411
0412
0413
0414
0415
0416
0417
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427 static int ntfs_sync_mft_mirror_umount(ntfs_volume *vol,
0428 const unsigned long mft_no, MFT_RECORD *m)
0429 {
0430 BUG_ON(vol->mftmirr_ino);
0431 ntfs_error(vol->sb, "Umount time mft mirror syncing is not "
0432 "implemented yet. %s", ntfs_please_email);
0433 return -EOPNOTSUPP;
0434 }
0435
0436
0437
0438
0439
0440
0441
0442
0443
0444
0445
0446
0447
0448
0449
0450
0451
0452
0453
0454 int ntfs_sync_mft_mirror(ntfs_volume *vol, const unsigned long mft_no,
0455 MFT_RECORD *m, int sync)
0456 {
0457 struct page *page;
0458 unsigned int blocksize = vol->sb->s_blocksize;
0459 int max_bhs = vol->mft_record_size / blocksize;
0460 struct buffer_head *bhs[MAX_BHS];
0461 struct buffer_head *bh, *head;
0462 u8 *kmirr;
0463 runlist_element *rl;
0464 unsigned int block_start, block_end, m_start, m_end, page_ofs;
0465 int i_bhs, nr_bhs, err = 0;
0466 unsigned char blocksize_bits = vol->sb->s_blocksize_bits;
0467
0468 ntfs_debug("Entering for inode 0x%lx.", mft_no);
0469 BUG_ON(!max_bhs);
0470 if (WARN_ON(max_bhs > MAX_BHS))
0471 return -EINVAL;
0472 if (unlikely(!vol->mftmirr_ino)) {
0473
0474 err = ntfs_sync_mft_mirror_umount(vol, mft_no, m);
0475 if (likely(!err))
0476 return err;
0477 goto err_out;
0478 }
0479
0480 page = ntfs_map_page(vol->mftmirr_ino->i_mapping, mft_no >>
0481 (PAGE_SHIFT - vol->mft_record_size_bits));
0482 if (IS_ERR(page)) {
0483 ntfs_error(vol->sb, "Failed to map mft mirror page.");
0484 err = PTR_ERR(page);
0485 goto err_out;
0486 }
0487 lock_page(page);
0488 BUG_ON(!PageUptodate(page));
0489 ClearPageUptodate(page);
0490
0491 page_ofs = (mft_no << vol->mft_record_size_bits) & ~PAGE_MASK;
0492
0493 kmirr = page_address(page) + page_ofs;
0494
0495 memcpy(kmirr, m, vol->mft_record_size);
0496
0497 if (unlikely(!page_has_buffers(page))) {
0498 struct buffer_head *tail;
0499
0500 bh = head = alloc_page_buffers(page, blocksize, true);
0501 do {
0502 set_buffer_uptodate(bh);
0503 tail = bh;
0504 bh = bh->b_this_page;
0505 } while (bh);
0506 tail->b_this_page = head;
0507 attach_page_private(page, head);
0508 }
0509 bh = head = page_buffers(page);
0510 BUG_ON(!bh);
0511 rl = NULL;
0512 nr_bhs = 0;
0513 block_start = 0;
0514 m_start = kmirr - (u8*)page_address(page);
0515 m_end = m_start + vol->mft_record_size;
0516 do {
0517 block_end = block_start + blocksize;
0518
0519 if (block_end <= m_start)
0520 continue;
0521 if (unlikely(block_start >= m_end))
0522 break;
0523
0524 if (unlikely(!buffer_mapped(bh))) {
0525 VCN vcn;
0526 LCN lcn;
0527 unsigned int vcn_ofs;
0528
0529 bh->b_bdev = vol->sb->s_bdev;
0530
0531 vcn = ((VCN)mft_no << vol->mft_record_size_bits) +
0532 (block_start - m_start);
0533 vcn_ofs = vcn & vol->cluster_size_mask;
0534 vcn >>= vol->cluster_size_bits;
0535 if (!rl) {
0536 down_read(&NTFS_I(vol->mftmirr_ino)->
0537 runlist.lock);
0538 rl = NTFS_I(vol->mftmirr_ino)->runlist.rl;
0539
0540
0541
0542
0543 BUG_ON(!rl);
0544 }
0545
0546 while (rl->length && rl[1].vcn <= vcn)
0547 rl++;
0548 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
0549
0550 if (likely(lcn >= 0)) {
0551
0552 bh->b_blocknr = ((lcn <<
0553 vol->cluster_size_bits) +
0554 vcn_ofs) >> blocksize_bits;
0555 set_buffer_mapped(bh);
0556 } else {
0557 bh->b_blocknr = -1;
0558 ntfs_error(vol->sb, "Cannot write mft mirror "
0559 "record 0x%lx because its "
0560 "location on disk could not "
0561 "be determined (error code "
0562 "%lli).", mft_no,
0563 (long long)lcn);
0564 err = -EIO;
0565 }
0566 }
0567 BUG_ON(!buffer_uptodate(bh));
0568 BUG_ON(!nr_bhs && (m_start != block_start));
0569 BUG_ON(nr_bhs >= max_bhs);
0570 bhs[nr_bhs++] = bh;
0571 BUG_ON((nr_bhs >= max_bhs) && (m_end != block_end));
0572 } while (block_start = block_end, (bh = bh->b_this_page) != head);
0573 if (unlikely(rl))
0574 up_read(&NTFS_I(vol->mftmirr_ino)->runlist.lock);
0575 if (likely(!err)) {
0576
0577 for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
0578 struct buffer_head *tbh = bhs[i_bhs];
0579
0580 if (!trylock_buffer(tbh))
0581 BUG();
0582 BUG_ON(!buffer_uptodate(tbh));
0583 clear_buffer_dirty(tbh);
0584 get_bh(tbh);
0585 tbh->b_end_io = end_buffer_write_sync;
0586 submit_bh(REQ_OP_WRITE, tbh);
0587 }
0588
0589 for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
0590 struct buffer_head *tbh = bhs[i_bhs];
0591
0592 wait_on_buffer(tbh);
0593 if (unlikely(!buffer_uptodate(tbh))) {
0594 err = -EIO;
0595
0596
0597
0598
0599 set_buffer_uptodate(tbh);
0600 }
0601 }
0602 } else {
0603
0604 for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++)
0605 clear_buffer_dirty(bhs[i_bhs]);
0606 }
0607
0608
0609 post_write_mst_fixup((NTFS_RECORD*)kmirr);
0610 flush_dcache_page(page);
0611 SetPageUptodate(page);
0612 unlock_page(page);
0613 ntfs_unmap_page(page);
0614 if (likely(!err)) {
0615 ntfs_debug("Done.");
0616 } else {
0617 ntfs_error(vol->sb, "I/O error while writing mft mirror "
0618 "record 0x%lx!", mft_no);
0619 err_out:
0620 ntfs_error(vol->sb, "Failed to synchronize $MFTMirr (error "
0621 "code %i). Volume will be left marked dirty "
0622 "on umount. Run ntfsfix on the partition "
0623 "after umounting to correct this.", -err);
0624 NVolSetErrors(vol);
0625 }
0626 return err;
0627 }
0628
0629
0630
0631
0632
0633
0634
0635
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645
0646
0647
0648
0649
0650
0651
0652
0653
0654
0655
0656
0657
0658
0659
0660 int write_mft_record_nolock(ntfs_inode *ni, MFT_RECORD *m, int sync)
0661 {
0662 ntfs_volume *vol = ni->vol;
0663 struct page *page = ni->page;
0664 unsigned int blocksize = vol->sb->s_blocksize;
0665 unsigned char blocksize_bits = vol->sb->s_blocksize_bits;
0666 int max_bhs = vol->mft_record_size / blocksize;
0667 struct buffer_head *bhs[MAX_BHS];
0668 struct buffer_head *bh, *head;
0669 runlist_element *rl;
0670 unsigned int block_start, block_end, m_start, m_end;
0671 int i_bhs, nr_bhs, err = 0;
0672
0673 ntfs_debug("Entering for inode 0x%lx.", ni->mft_no);
0674 BUG_ON(NInoAttr(ni));
0675 BUG_ON(!max_bhs);
0676 BUG_ON(!PageLocked(page));
0677 if (WARN_ON(max_bhs > MAX_BHS)) {
0678 err = -EINVAL;
0679 goto err_out;
0680 }
0681
0682
0683
0684
0685
0686
0687 if (!NInoTestClearDirty(ni))
0688 goto done;
0689 bh = head = page_buffers(page);
0690 BUG_ON(!bh);
0691 rl = NULL;
0692 nr_bhs = 0;
0693 block_start = 0;
0694 m_start = ni->page_ofs;
0695 m_end = m_start + vol->mft_record_size;
0696 do {
0697 block_end = block_start + blocksize;
0698
0699 if (block_end <= m_start)
0700 continue;
0701 if (unlikely(block_start >= m_end))
0702 break;
0703
0704
0705
0706
0707
0708 if (block_start == m_start) {
0709
0710 if (!buffer_dirty(bh)) {
0711 BUG_ON(nr_bhs);
0712
0713 break;
0714 }
0715 }
0716
0717 if (unlikely(!buffer_mapped(bh))) {
0718 VCN vcn;
0719 LCN lcn;
0720 unsigned int vcn_ofs;
0721
0722 bh->b_bdev = vol->sb->s_bdev;
0723
0724 vcn = ((VCN)ni->mft_no << vol->mft_record_size_bits) +
0725 (block_start - m_start);
0726 vcn_ofs = vcn & vol->cluster_size_mask;
0727 vcn >>= vol->cluster_size_bits;
0728 if (!rl) {
0729 down_read(&NTFS_I(vol->mft_ino)->runlist.lock);
0730 rl = NTFS_I(vol->mft_ino)->runlist.rl;
0731 BUG_ON(!rl);
0732 }
0733
0734 while (rl->length && rl[1].vcn <= vcn)
0735 rl++;
0736 lcn = ntfs_rl_vcn_to_lcn(rl, vcn);
0737
0738 if (likely(lcn >= 0)) {
0739
0740 bh->b_blocknr = ((lcn <<
0741 vol->cluster_size_bits) +
0742 vcn_ofs) >> blocksize_bits;
0743 set_buffer_mapped(bh);
0744 } else {
0745 bh->b_blocknr = -1;
0746 ntfs_error(vol->sb, "Cannot write mft record "
0747 "0x%lx because its location "
0748 "on disk could not be "
0749 "determined (error code %lli).",
0750 ni->mft_no, (long long)lcn);
0751 err = -EIO;
0752 }
0753 }
0754 BUG_ON(!buffer_uptodate(bh));
0755 BUG_ON(!nr_bhs && (m_start != block_start));
0756 BUG_ON(nr_bhs >= max_bhs);
0757 bhs[nr_bhs++] = bh;
0758 BUG_ON((nr_bhs >= max_bhs) && (m_end != block_end));
0759 } while (block_start = block_end, (bh = bh->b_this_page) != head);
0760 if (unlikely(rl))
0761 up_read(&NTFS_I(vol->mft_ino)->runlist.lock);
0762 if (!nr_bhs)
0763 goto done;
0764 if (unlikely(err))
0765 goto cleanup_out;
0766
0767 err = pre_write_mst_fixup((NTFS_RECORD*)m, vol->mft_record_size);
0768 if (err) {
0769 ntfs_error(vol->sb, "Failed to apply mst fixups!");
0770 goto cleanup_out;
0771 }
0772 flush_dcache_mft_record_page(ni);
0773
0774 for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
0775 struct buffer_head *tbh = bhs[i_bhs];
0776
0777 if (!trylock_buffer(tbh))
0778 BUG();
0779 BUG_ON(!buffer_uptodate(tbh));
0780 clear_buffer_dirty(tbh);
0781 get_bh(tbh);
0782 tbh->b_end_io = end_buffer_write_sync;
0783 submit_bh(REQ_OP_WRITE, tbh);
0784 }
0785
0786 if (!sync && ni->mft_no < vol->mftmirr_size)
0787 ntfs_sync_mft_mirror(vol, ni->mft_no, m, sync);
0788
0789 for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++) {
0790 struct buffer_head *tbh = bhs[i_bhs];
0791
0792 wait_on_buffer(tbh);
0793 if (unlikely(!buffer_uptodate(tbh))) {
0794 err = -EIO;
0795
0796
0797
0798
0799 if (PageUptodate(page))
0800 set_buffer_uptodate(tbh);
0801 }
0802 }
0803
0804 if (sync && ni->mft_no < vol->mftmirr_size)
0805 ntfs_sync_mft_mirror(vol, ni->mft_no, m, sync);
0806
0807 post_write_mst_fixup((NTFS_RECORD*)m);
0808 flush_dcache_mft_record_page(ni);
0809 if (unlikely(err)) {
0810
0811 ntfs_error(vol->sb, "I/O error while writing mft record "
0812 "0x%lx! Marking base inode as bad. You "
0813 "should unmount the volume and run chkdsk.",
0814 ni->mft_no);
0815 goto err_out;
0816 }
0817 done:
0818 ntfs_debug("Done.");
0819 return 0;
0820 cleanup_out:
0821
0822 for (i_bhs = 0; i_bhs < nr_bhs; i_bhs++)
0823 clear_buffer_dirty(bhs[i_bhs]);
0824 err_out:
0825
0826
0827
0828
0829
0830
0831 if (err == -ENOMEM) {
0832 ntfs_error(vol->sb, "Not enough memory to write mft record. "
0833 "Redirtying so the write is retried later.");
0834 mark_mft_record_dirty(ni);
0835 err = 0;
0836 } else
0837 NVolSetErrors(vol);
0838 return err;
0839 }
0840
0841
0842
0843
0844
0845
0846
0847
0848
0849
0850
0851
0852
0853
0854
0855
0856
0857
0858
0859
0860
0861
0862
0863
0864
0865
0866
0867
0868
0869
0870
0871
0872
0873
0874
0875
0876
0877
0878
0879
0880
0881
0882
0883
0884
0885
0886
0887
0888
0889
0890
0891
0892
0893
0894
0895
0896
0897
0898
0899
0900
0901
0902
0903
0904
0905
0906
0907
0908
0909
0910
0911
0912
0913
0914
0915
0916
0917
0918
0919
0920 bool ntfs_may_write_mft_record(ntfs_volume *vol, const unsigned long mft_no,
0921 const MFT_RECORD *m, ntfs_inode **locked_ni)
0922 {
0923 struct super_block *sb = vol->sb;
0924 struct inode *mft_vi = vol->mft_ino;
0925 struct inode *vi;
0926 ntfs_inode *ni, *eni, **extent_nis;
0927 int i;
0928 ntfs_attr na;
0929
0930 ntfs_debug("Entering for inode 0x%lx.", mft_no);
0931
0932
0933
0934 BUG_ON(!locked_ni);
0935 *locked_ni = NULL;
0936
0937
0938
0939
0940 ntfs_debug("Looking for inode 0x%lx in icache.", mft_no);
0941 na.mft_no = mft_no;
0942 na.name = NULL;
0943 na.name_len = 0;
0944 na.type = AT_UNUSED;
0945
0946
0947
0948
0949 if (!mft_no) {
0950
0951 vi = igrab(mft_vi);
0952 BUG_ON(vi != mft_vi);
0953 } else {
0954
0955
0956
0957
0958
0959
0960
0961 vi = ilookup5_nowait(sb, mft_no, ntfs_test_inode, &na);
0962 }
0963 if (vi) {
0964 ntfs_debug("Base inode 0x%lx is in icache.", mft_no);
0965
0966 ni = NTFS_I(vi);
0967
0968 atomic_inc(&ni->count);
0969
0970 if (NInoDirty(ni)) {
0971 ntfs_debug("Inode 0x%lx is dirty, do not write it.",
0972 mft_no);
0973 atomic_dec(&ni->count);
0974 iput(vi);
0975 return false;
0976 }
0977 ntfs_debug("Inode 0x%lx is not dirty.", mft_no);
0978
0979 if (unlikely(!mutex_trylock(&ni->mrec_lock))) {
0980 ntfs_debug("Mft record 0x%lx is already locked, do "
0981 "not write it.", mft_no);
0982 atomic_dec(&ni->count);
0983 iput(vi);
0984 return false;
0985 }
0986 ntfs_debug("Managed to lock mft record 0x%lx, write it.",
0987 mft_no);
0988
0989
0990
0991
0992 *locked_ni = ni;
0993 return true;
0994 }
0995 ntfs_debug("Inode 0x%lx is not in icache.", mft_no);
0996
0997
0998 if (!ntfs_is_mft_record(m->magic)) {
0999 ntfs_debug("Mft record 0x%lx is not a FILE record, write it.",
1000 mft_no);
1001 return true;
1002 }
1003
1004 if (!m->base_mft_record) {
1005 ntfs_debug("Mft record 0x%lx is a base record, write it.",
1006 mft_no);
1007 return true;
1008 }
1009
1010
1011
1012
1013
1014 na.mft_no = MREF_LE(m->base_mft_record);
1015 ntfs_debug("Mft record 0x%lx is an extent record. Looking for base "
1016 "inode 0x%lx in icache.", mft_no, na.mft_no);
1017 if (!na.mft_no) {
1018
1019 vi = igrab(mft_vi);
1020 BUG_ON(vi != mft_vi);
1021 } else
1022 vi = ilookup5_nowait(sb, na.mft_no, ntfs_test_inode,
1023 &na);
1024 if (!vi) {
1025
1026
1027
1028
1029 ntfs_debug("Base inode 0x%lx is not in icache, write the "
1030 "extent record.", na.mft_no);
1031 return true;
1032 }
1033 ntfs_debug("Base inode 0x%lx is in icache.", na.mft_no);
1034
1035
1036
1037
1038 ni = NTFS_I(vi);
1039 mutex_lock(&ni->extent_lock);
1040 if (ni->nr_extents <= 0) {
1041
1042
1043
1044
1045 mutex_unlock(&ni->extent_lock);
1046 iput(vi);
1047 ntfs_debug("Base inode 0x%lx has no attached extent inodes, "
1048 "write the extent record.", na.mft_no);
1049 return true;
1050 }
1051
1052 extent_nis = ni->ext.extent_ntfs_inos;
1053 for (eni = NULL, i = 0; i < ni->nr_extents; ++i) {
1054 if (mft_no == extent_nis[i]->mft_no) {
1055
1056
1057
1058
1059 eni = extent_nis[i];
1060 break;
1061 }
1062 }
1063
1064
1065
1066
1067 if (!eni) {
1068 mutex_unlock(&ni->extent_lock);
1069 iput(vi);
1070 ntfs_debug("Extent inode 0x%lx is not attached to its base "
1071 "inode 0x%lx, write the extent record.",
1072 mft_no, na.mft_no);
1073 return true;
1074 }
1075 ntfs_debug("Extent inode 0x%lx is attached to its base inode 0x%lx.",
1076 mft_no, na.mft_no);
1077
1078 atomic_inc(&eni->count);
1079 mutex_unlock(&ni->extent_lock);
1080
1081
1082
1083
1084 if (unlikely(!mutex_trylock(&eni->mrec_lock))) {
1085 atomic_dec(&eni->count);
1086 iput(vi);
1087 ntfs_debug("Extent mft record 0x%lx is already locked, do "
1088 "not write it.", mft_no);
1089 return false;
1090 }
1091 ntfs_debug("Managed to lock extent mft record 0x%lx, write it.",
1092 mft_no);
1093 if (NInoTestClearDirty(eni))
1094 ntfs_debug("Extent inode 0x%lx is dirty, marking it clean.",
1095 mft_no);
1096
1097
1098
1099
1100 *locked_ni = eni;
1101 return true;
1102 }
1103
1104 static const char *es = " Leaving inconsistent metadata. Unmount and run "
1105 "chkdsk.";
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126 static int ntfs_mft_bitmap_find_and_alloc_free_rec_nolock(ntfs_volume *vol,
1127 ntfs_inode *base_ni)
1128 {
1129 s64 pass_end, ll, data_pos, pass_start, ofs, bit;
1130 unsigned long flags;
1131 struct address_space *mftbmp_mapping;
1132 u8 *buf, *byte;
1133 struct page *page;
1134 unsigned int page_ofs, size;
1135 u8 pass, b;
1136
1137 ntfs_debug("Searching for free mft record in the currently "
1138 "initialized mft bitmap.");
1139 mftbmp_mapping = vol->mftbmp_ino->i_mapping;
1140
1141
1142
1143
1144 read_lock_irqsave(&NTFS_I(vol->mft_ino)->size_lock, flags);
1145 pass_end = NTFS_I(vol->mft_ino)->allocated_size >>
1146 vol->mft_record_size_bits;
1147 read_unlock_irqrestore(&NTFS_I(vol->mft_ino)->size_lock, flags);
1148 read_lock_irqsave(&NTFS_I(vol->mftbmp_ino)->size_lock, flags);
1149 ll = NTFS_I(vol->mftbmp_ino)->initialized_size << 3;
1150 read_unlock_irqrestore(&NTFS_I(vol->mftbmp_ino)->size_lock, flags);
1151 if (pass_end > ll)
1152 pass_end = ll;
1153 pass = 1;
1154 if (!base_ni)
1155 data_pos = vol->mft_data_pos;
1156 else
1157 data_pos = base_ni->mft_no + 1;
1158 if (data_pos < 24)
1159 data_pos = 24;
1160 if (data_pos >= pass_end) {
1161 data_pos = 24;
1162 pass = 2;
1163
1164 if (data_pos >= pass_end)
1165 return -ENOSPC;
1166 }
1167 pass_start = data_pos;
1168 ntfs_debug("Starting bitmap search: pass %u, pass_start 0x%llx, "
1169 "pass_end 0x%llx, data_pos 0x%llx.", pass,
1170 (long long)pass_start, (long long)pass_end,
1171 (long long)data_pos);
1172
1173 for (; pass <= 2;) {
1174
1175 ofs = data_pos >> 3;
1176 page_ofs = ofs & ~PAGE_MASK;
1177 size = PAGE_SIZE - page_ofs;
1178 ll = ((pass_end + 7) >> 3) - ofs;
1179 if (size > ll)
1180 size = ll;
1181 size <<= 3;
1182
1183
1184
1185
1186 if (size) {
1187 page = ntfs_map_page(mftbmp_mapping,
1188 ofs >> PAGE_SHIFT);
1189 if (IS_ERR(page)) {
1190 ntfs_error(vol->sb, "Failed to read mft "
1191 "bitmap, aborting.");
1192 return PTR_ERR(page);
1193 }
1194 buf = (u8*)page_address(page) + page_ofs;
1195 bit = data_pos & 7;
1196 data_pos &= ~7ull;
1197 ntfs_debug("Before inner for loop: size 0x%x, "
1198 "data_pos 0x%llx, bit 0x%llx", size,
1199 (long long)data_pos, (long long)bit);
1200 for (; bit < size && data_pos + bit < pass_end;
1201 bit &= ~7ull, bit += 8) {
1202 byte = buf + (bit >> 3);
1203 if (*byte == 0xff)
1204 continue;
1205 b = ffz((unsigned long)*byte);
1206 if (b < 8 && b >= (bit & 7)) {
1207 ll = data_pos + (bit & ~7ull) + b;
1208 if (unlikely(ll > (1ll << 32))) {
1209 ntfs_unmap_page(page);
1210 return -ENOSPC;
1211 }
1212 *byte |= 1 << b;
1213 flush_dcache_page(page);
1214 set_page_dirty(page);
1215 ntfs_unmap_page(page);
1216 ntfs_debug("Done. (Found and "
1217 "allocated mft record "
1218 "0x%llx.)",
1219 (long long)ll);
1220 return ll;
1221 }
1222 }
1223 ntfs_debug("After inner for loop: size 0x%x, "
1224 "data_pos 0x%llx, bit 0x%llx", size,
1225 (long long)data_pos, (long long)bit);
1226 data_pos += size;
1227 ntfs_unmap_page(page);
1228
1229
1230
1231
1232 if (data_pos < pass_end)
1233 continue;
1234 }
1235
1236 if (++pass == 2) {
1237
1238
1239
1240
1241 pass_end = pass_start;
1242 data_pos = pass_start = 24;
1243 ntfs_debug("pass %i, pass_start 0x%llx, pass_end "
1244 "0x%llx.", pass, (long long)pass_start,
1245 (long long)pass_end);
1246 if (data_pos >= pass_end)
1247 break;
1248 }
1249 }
1250
1251 ntfs_debug("Done. (No free mft records left in currently initialized "
1252 "mft bitmap.)");
1253 return -ENOSPC;
1254 }
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273 static int ntfs_mft_bitmap_extend_allocation_nolock(ntfs_volume *vol)
1274 {
1275 LCN lcn;
1276 s64 ll;
1277 unsigned long flags;
1278 struct page *page;
1279 ntfs_inode *mft_ni, *mftbmp_ni;
1280 runlist_element *rl, *rl2 = NULL;
1281 ntfs_attr_search_ctx *ctx = NULL;
1282 MFT_RECORD *mrec;
1283 ATTR_RECORD *a = NULL;
1284 int ret, mp_size;
1285 u32 old_alen = 0;
1286 u8 *b, tb;
1287 struct {
1288 u8 added_cluster:1;
1289 u8 added_run:1;
1290 u8 mp_rebuilt:1;
1291 } status = { 0, 0, 0 };
1292
1293 ntfs_debug("Extending mft bitmap allocation.");
1294 mft_ni = NTFS_I(vol->mft_ino);
1295 mftbmp_ni = NTFS_I(vol->mftbmp_ino);
1296
1297
1298
1299
1300 down_write(&mftbmp_ni->runlist.lock);
1301 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
1302 ll = mftbmp_ni->allocated_size;
1303 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1304 rl = ntfs_attr_find_vcn_nolock(mftbmp_ni,
1305 (ll - 1) >> vol->cluster_size_bits, NULL);
1306 if (IS_ERR(rl) || unlikely(!rl->length || rl->lcn < 0)) {
1307 up_write(&mftbmp_ni->runlist.lock);
1308 ntfs_error(vol->sb, "Failed to determine last allocated "
1309 "cluster of mft bitmap attribute.");
1310 if (!IS_ERR(rl))
1311 ret = -EIO;
1312 else
1313 ret = PTR_ERR(rl);
1314 return ret;
1315 }
1316 lcn = rl->lcn + rl->length;
1317 ntfs_debug("Last lcn of mft bitmap attribute is 0x%llx.",
1318 (long long)lcn);
1319
1320
1321
1322
1323
1324 ll = lcn >> 3;
1325 page = ntfs_map_page(vol->lcnbmp_ino->i_mapping,
1326 ll >> PAGE_SHIFT);
1327 if (IS_ERR(page)) {
1328 up_write(&mftbmp_ni->runlist.lock);
1329 ntfs_error(vol->sb, "Failed to read from lcn bitmap.");
1330 return PTR_ERR(page);
1331 }
1332 b = (u8*)page_address(page) + (ll & ~PAGE_MASK);
1333 tb = 1 << (lcn & 7ull);
1334 down_write(&vol->lcnbmp_lock);
1335 if (*b != 0xff && !(*b & tb)) {
1336
1337 *b |= tb;
1338 flush_dcache_page(page);
1339 set_page_dirty(page);
1340 up_write(&vol->lcnbmp_lock);
1341 ntfs_unmap_page(page);
1342
1343 rl->length++;
1344 rl[1].vcn++;
1345 status.added_cluster = 1;
1346 ntfs_debug("Appending one cluster to mft bitmap.");
1347 } else {
1348 up_write(&vol->lcnbmp_lock);
1349 ntfs_unmap_page(page);
1350
1351 rl2 = ntfs_cluster_alloc(vol, rl[1].vcn, 1, lcn, DATA_ZONE,
1352 true);
1353 if (IS_ERR(rl2)) {
1354 up_write(&mftbmp_ni->runlist.lock);
1355 ntfs_error(vol->sb, "Failed to allocate a cluster for "
1356 "the mft bitmap.");
1357 return PTR_ERR(rl2);
1358 }
1359 rl = ntfs_runlists_merge(mftbmp_ni->runlist.rl, rl2);
1360 if (IS_ERR(rl)) {
1361 up_write(&mftbmp_ni->runlist.lock);
1362 ntfs_error(vol->sb, "Failed to merge runlists for mft "
1363 "bitmap.");
1364 if (ntfs_cluster_free_from_rl(vol, rl2)) {
1365 ntfs_error(vol->sb, "Failed to deallocate "
1366 "allocated cluster.%s", es);
1367 NVolSetErrors(vol);
1368 }
1369 ntfs_free(rl2);
1370 return PTR_ERR(rl);
1371 }
1372 mftbmp_ni->runlist.rl = rl;
1373 status.added_run = 1;
1374 ntfs_debug("Adding one run to mft bitmap.");
1375
1376 for (; rl[1].length; rl++)
1377 ;
1378 }
1379
1380
1381
1382
1383 mrec = map_mft_record(mft_ni);
1384 if (IS_ERR(mrec)) {
1385 ntfs_error(vol->sb, "Failed to map mft record.");
1386 ret = PTR_ERR(mrec);
1387 goto undo_alloc;
1388 }
1389 ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
1390 if (unlikely(!ctx)) {
1391 ntfs_error(vol->sb, "Failed to get search context.");
1392 ret = -ENOMEM;
1393 goto undo_alloc;
1394 }
1395 ret = ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
1396 mftbmp_ni->name_len, CASE_SENSITIVE, rl[1].vcn, NULL,
1397 0, ctx);
1398 if (unlikely(ret)) {
1399 ntfs_error(vol->sb, "Failed to find last attribute extent of "
1400 "mft bitmap attribute.");
1401 if (ret == -ENOENT)
1402 ret = -EIO;
1403 goto undo_alloc;
1404 }
1405 a = ctx->attr;
1406 ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
1407
1408 for (rl2 = rl; rl2 > mftbmp_ni->runlist.rl; rl2--) {
1409 if (ll >= rl2->vcn)
1410 break;
1411 }
1412 BUG_ON(ll < rl2->vcn);
1413 BUG_ON(ll >= rl2->vcn + rl2->length);
1414
1415 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
1416 if (unlikely(mp_size <= 0)) {
1417 ntfs_error(vol->sb, "Get size for mapping pairs failed for "
1418 "mft bitmap attribute extent.");
1419 ret = mp_size;
1420 if (!ret)
1421 ret = -EIO;
1422 goto undo_alloc;
1423 }
1424
1425 old_alen = le32_to_cpu(a->length);
1426 ret = ntfs_attr_record_resize(ctx->mrec, a, mp_size +
1427 le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
1428 if (unlikely(ret)) {
1429 if (ret != -ENOSPC) {
1430 ntfs_error(vol->sb, "Failed to resize attribute "
1431 "record for mft bitmap attribute.");
1432 goto undo_alloc;
1433 }
1434
1435
1436
1437
1438
1439 ntfs_error(vol->sb, "Not enough space in this mft record to "
1440 "accommodate extended mft bitmap attribute "
1441 "extent. Cannot handle this yet.");
1442 ret = -EOPNOTSUPP;
1443 goto undo_alloc;
1444 }
1445 status.mp_rebuilt = 1;
1446
1447 ret = ntfs_mapping_pairs_build(vol, (u8*)a +
1448 le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
1449 mp_size, rl2, ll, -1, NULL);
1450 if (unlikely(ret)) {
1451 ntfs_error(vol->sb, "Failed to build mapping pairs array for "
1452 "mft bitmap attribute.");
1453 goto undo_alloc;
1454 }
1455
1456 a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 1);
1457
1458
1459
1460
1461 if (a->data.non_resident.lowest_vcn) {
1462
1463
1464
1465
1466 flush_dcache_mft_record_page(ctx->ntfs_ino);
1467 mark_mft_record_dirty(ctx->ntfs_ino);
1468 ntfs_attr_reinit_search_ctx(ctx);
1469 ret = ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
1470 mftbmp_ni->name_len, CASE_SENSITIVE, 0, NULL,
1471 0, ctx);
1472 if (unlikely(ret)) {
1473 ntfs_error(vol->sb, "Failed to find first attribute "
1474 "extent of mft bitmap attribute.");
1475 goto restore_undo_alloc;
1476 }
1477 a = ctx->attr;
1478 }
1479 write_lock_irqsave(&mftbmp_ni->size_lock, flags);
1480 mftbmp_ni->allocated_size += vol->cluster_size;
1481 a->data.non_resident.allocated_size =
1482 cpu_to_sle64(mftbmp_ni->allocated_size);
1483 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1484
1485 flush_dcache_mft_record_page(ctx->ntfs_ino);
1486 mark_mft_record_dirty(ctx->ntfs_ino);
1487 ntfs_attr_put_search_ctx(ctx);
1488 unmap_mft_record(mft_ni);
1489 up_write(&mftbmp_ni->runlist.lock);
1490 ntfs_debug("Done.");
1491 return 0;
1492 restore_undo_alloc:
1493 ntfs_attr_reinit_search_ctx(ctx);
1494 if (ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
1495 mftbmp_ni->name_len, CASE_SENSITIVE, rl[1].vcn, NULL,
1496 0, ctx)) {
1497 ntfs_error(vol->sb, "Failed to find last attribute extent of "
1498 "mft bitmap attribute.%s", es);
1499 write_lock_irqsave(&mftbmp_ni->size_lock, flags);
1500 mftbmp_ni->allocated_size += vol->cluster_size;
1501 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1502 ntfs_attr_put_search_ctx(ctx);
1503 unmap_mft_record(mft_ni);
1504 up_write(&mftbmp_ni->runlist.lock);
1505
1506
1507
1508
1509 NVolSetErrors(vol);
1510 return ret;
1511 }
1512 a = ctx->attr;
1513 a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 2);
1514 undo_alloc:
1515 if (status.added_cluster) {
1516
1517 rl->length--;
1518 rl[1].vcn--;
1519 } else if (status.added_run) {
1520 lcn = rl->lcn;
1521
1522 rl->lcn = rl[1].lcn;
1523 rl->length = 0;
1524 }
1525
1526 down_write(&vol->lcnbmp_lock);
1527 if (ntfs_bitmap_clear_bit(vol->lcnbmp_ino, lcn)) {
1528 ntfs_error(vol->sb, "Failed to free allocated cluster.%s", es);
1529 NVolSetErrors(vol);
1530 }
1531 up_write(&vol->lcnbmp_lock);
1532 if (status.mp_rebuilt) {
1533 if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
1534 a->data.non_resident.mapping_pairs_offset),
1535 old_alen - le16_to_cpu(
1536 a->data.non_resident.mapping_pairs_offset),
1537 rl2, ll, -1, NULL)) {
1538 ntfs_error(vol->sb, "Failed to restore mapping pairs "
1539 "array.%s", es);
1540 NVolSetErrors(vol);
1541 }
1542 if (ntfs_attr_record_resize(ctx->mrec, a, old_alen)) {
1543 ntfs_error(vol->sb, "Failed to restore attribute "
1544 "record.%s", es);
1545 NVolSetErrors(vol);
1546 }
1547 flush_dcache_mft_record_page(ctx->ntfs_ino);
1548 mark_mft_record_dirty(ctx->ntfs_ino);
1549 }
1550 if (ctx)
1551 ntfs_attr_put_search_ctx(ctx);
1552 if (!IS_ERR(mrec))
1553 unmap_mft_record(mft_ni);
1554 up_write(&mftbmp_ni->runlist.lock);
1555 return ret;
1556 }
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572 static int ntfs_mft_bitmap_extend_initialized_nolock(ntfs_volume *vol)
1573 {
1574 s64 old_data_size, old_initialized_size;
1575 unsigned long flags;
1576 struct inode *mftbmp_vi;
1577 ntfs_inode *mft_ni, *mftbmp_ni;
1578 ntfs_attr_search_ctx *ctx;
1579 MFT_RECORD *mrec;
1580 ATTR_RECORD *a;
1581 int ret;
1582
1583 ntfs_debug("Extending mft bitmap initiailized (and data) size.");
1584 mft_ni = NTFS_I(vol->mft_ino);
1585 mftbmp_vi = vol->mftbmp_ino;
1586 mftbmp_ni = NTFS_I(mftbmp_vi);
1587
1588 mrec = map_mft_record(mft_ni);
1589 if (IS_ERR(mrec)) {
1590 ntfs_error(vol->sb, "Failed to map mft record.");
1591 return PTR_ERR(mrec);
1592 }
1593 ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
1594 if (unlikely(!ctx)) {
1595 ntfs_error(vol->sb, "Failed to get search context.");
1596 ret = -ENOMEM;
1597 goto unm_err_out;
1598 }
1599 ret = ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
1600 mftbmp_ni->name_len, CASE_SENSITIVE, 0, NULL, 0, ctx);
1601 if (unlikely(ret)) {
1602 ntfs_error(vol->sb, "Failed to find first attribute extent of "
1603 "mft bitmap attribute.");
1604 if (ret == -ENOENT)
1605 ret = -EIO;
1606 goto put_err_out;
1607 }
1608 a = ctx->attr;
1609 write_lock_irqsave(&mftbmp_ni->size_lock, flags);
1610 old_data_size = i_size_read(mftbmp_vi);
1611 old_initialized_size = mftbmp_ni->initialized_size;
1612
1613
1614
1615
1616
1617 mftbmp_ni->initialized_size += 8;
1618 a->data.non_resident.initialized_size =
1619 cpu_to_sle64(mftbmp_ni->initialized_size);
1620 if (mftbmp_ni->initialized_size > old_data_size) {
1621 i_size_write(mftbmp_vi, mftbmp_ni->initialized_size);
1622 a->data.non_resident.data_size =
1623 cpu_to_sle64(mftbmp_ni->initialized_size);
1624 }
1625 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1626
1627 flush_dcache_mft_record_page(ctx->ntfs_ino);
1628 mark_mft_record_dirty(ctx->ntfs_ino);
1629 ntfs_attr_put_search_ctx(ctx);
1630 unmap_mft_record(mft_ni);
1631
1632 ret = ntfs_attr_set(mftbmp_ni, old_initialized_size, 8, 0);
1633 if (likely(!ret)) {
1634 ntfs_debug("Done. (Wrote eight initialized bytes to mft "
1635 "bitmap.");
1636 return 0;
1637 }
1638 ntfs_error(vol->sb, "Failed to write to mft bitmap.");
1639
1640 mrec = map_mft_record(mft_ni);
1641 if (IS_ERR(mrec)) {
1642 ntfs_error(vol->sb, "Failed to map mft record.%s", es);
1643 NVolSetErrors(vol);
1644 return ret;
1645 }
1646 ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
1647 if (unlikely(!ctx)) {
1648 ntfs_error(vol->sb, "Failed to get search context.%s", es);
1649 NVolSetErrors(vol);
1650 goto unm_err_out;
1651 }
1652 if (ntfs_attr_lookup(mftbmp_ni->type, mftbmp_ni->name,
1653 mftbmp_ni->name_len, CASE_SENSITIVE, 0, NULL, 0, ctx)) {
1654 ntfs_error(vol->sb, "Failed to find first attribute extent of "
1655 "mft bitmap attribute.%s", es);
1656 NVolSetErrors(vol);
1657 put_err_out:
1658 ntfs_attr_put_search_ctx(ctx);
1659 unm_err_out:
1660 unmap_mft_record(mft_ni);
1661 goto err_out;
1662 }
1663 a = ctx->attr;
1664 write_lock_irqsave(&mftbmp_ni->size_lock, flags);
1665 mftbmp_ni->initialized_size = old_initialized_size;
1666 a->data.non_resident.initialized_size =
1667 cpu_to_sle64(old_initialized_size);
1668 if (i_size_read(mftbmp_vi) != old_data_size) {
1669 i_size_write(mftbmp_vi, old_data_size);
1670 a->data.non_resident.data_size = cpu_to_sle64(old_data_size);
1671 }
1672 write_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1673 flush_dcache_mft_record_page(ctx->ntfs_ino);
1674 mark_mft_record_dirty(ctx->ntfs_ino);
1675 ntfs_attr_put_search_ctx(ctx);
1676 unmap_mft_record(mft_ni);
1677 #ifdef DEBUG
1678 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
1679 ntfs_debug("Restored status of mftbmp: allocated_size 0x%llx, "
1680 "data_size 0x%llx, initialized_size 0x%llx.",
1681 (long long)mftbmp_ni->allocated_size,
1682 (long long)i_size_read(mftbmp_vi),
1683 (long long)mftbmp_ni->initialized_size);
1684 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
1685 #endif
1686 err_out:
1687 return ret;
1688 }
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709 static int ntfs_mft_data_extend_allocation_nolock(ntfs_volume *vol)
1710 {
1711 LCN lcn;
1712 VCN old_last_vcn;
1713 s64 min_nr, nr, ll;
1714 unsigned long flags;
1715 ntfs_inode *mft_ni;
1716 runlist_element *rl, *rl2;
1717 ntfs_attr_search_ctx *ctx = NULL;
1718 MFT_RECORD *mrec;
1719 ATTR_RECORD *a = NULL;
1720 int ret, mp_size;
1721 u32 old_alen = 0;
1722 bool mp_rebuilt = false;
1723
1724 ntfs_debug("Extending mft data allocation.");
1725 mft_ni = NTFS_I(vol->mft_ino);
1726
1727
1728
1729
1730
1731 down_write(&mft_ni->runlist.lock);
1732 read_lock_irqsave(&mft_ni->size_lock, flags);
1733 ll = mft_ni->allocated_size;
1734 read_unlock_irqrestore(&mft_ni->size_lock, flags);
1735 rl = ntfs_attr_find_vcn_nolock(mft_ni,
1736 (ll - 1) >> vol->cluster_size_bits, NULL);
1737 if (IS_ERR(rl) || unlikely(!rl->length || rl->lcn < 0)) {
1738 up_write(&mft_ni->runlist.lock);
1739 ntfs_error(vol->sb, "Failed to determine last allocated "
1740 "cluster of mft data attribute.");
1741 if (!IS_ERR(rl))
1742 ret = -EIO;
1743 else
1744 ret = PTR_ERR(rl);
1745 return ret;
1746 }
1747 lcn = rl->lcn + rl->length;
1748 ntfs_debug("Last lcn of mft data attribute is 0x%llx.", (long long)lcn);
1749
1750 min_nr = vol->mft_record_size >> vol->cluster_size_bits;
1751 if (!min_nr)
1752 min_nr = 1;
1753
1754 nr = vol->mft_record_size << 4 >> vol->cluster_size_bits;
1755 if (!nr)
1756 nr = min_nr;
1757
1758 read_lock_irqsave(&mft_ni->size_lock, flags);
1759 ll = mft_ni->allocated_size;
1760 read_unlock_irqrestore(&mft_ni->size_lock, flags);
1761 if (unlikely((ll + (nr << vol->cluster_size_bits)) >>
1762 vol->mft_record_size_bits >= (1ll << 32))) {
1763 nr = min_nr;
1764 if (unlikely((ll + (nr << vol->cluster_size_bits)) >>
1765 vol->mft_record_size_bits >= (1ll << 32))) {
1766 ntfs_warning(vol->sb, "Cannot allocate mft record "
1767 "because the maximum number of inodes "
1768 "(2^32) has already been reached.");
1769 up_write(&mft_ni->runlist.lock);
1770 return -ENOSPC;
1771 }
1772 }
1773 ntfs_debug("Trying mft data allocation with %s cluster count %lli.",
1774 nr > min_nr ? "default" : "minimal", (long long)nr);
1775 old_last_vcn = rl[1].vcn;
1776 do {
1777 rl2 = ntfs_cluster_alloc(vol, old_last_vcn, nr, lcn, MFT_ZONE,
1778 true);
1779 if (!IS_ERR(rl2))
1780 break;
1781 if (PTR_ERR(rl2) != -ENOSPC || nr == min_nr) {
1782 ntfs_error(vol->sb, "Failed to allocate the minimal "
1783 "number of clusters (%lli) for the "
1784 "mft data attribute.", (long long)nr);
1785 up_write(&mft_ni->runlist.lock);
1786 return PTR_ERR(rl2);
1787 }
1788
1789
1790
1791
1792
1793 nr = min_nr;
1794 ntfs_debug("Retrying mft data allocation with minimal cluster "
1795 "count %lli.", (long long)nr);
1796 } while (1);
1797 rl = ntfs_runlists_merge(mft_ni->runlist.rl, rl2);
1798 if (IS_ERR(rl)) {
1799 up_write(&mft_ni->runlist.lock);
1800 ntfs_error(vol->sb, "Failed to merge runlists for mft data "
1801 "attribute.");
1802 if (ntfs_cluster_free_from_rl(vol, rl2)) {
1803 ntfs_error(vol->sb, "Failed to deallocate clusters "
1804 "from the mft data attribute.%s", es);
1805 NVolSetErrors(vol);
1806 }
1807 ntfs_free(rl2);
1808 return PTR_ERR(rl);
1809 }
1810 mft_ni->runlist.rl = rl;
1811 ntfs_debug("Allocated %lli clusters.", (long long)nr);
1812
1813 for (; rl[1].length; rl++)
1814 ;
1815
1816 mrec = map_mft_record(mft_ni);
1817 if (IS_ERR(mrec)) {
1818 ntfs_error(vol->sb, "Failed to map mft record.");
1819 ret = PTR_ERR(mrec);
1820 goto undo_alloc;
1821 }
1822 ctx = ntfs_attr_get_search_ctx(mft_ni, mrec);
1823 if (unlikely(!ctx)) {
1824 ntfs_error(vol->sb, "Failed to get search context.");
1825 ret = -ENOMEM;
1826 goto undo_alloc;
1827 }
1828 ret = ntfs_attr_lookup(mft_ni->type, mft_ni->name, mft_ni->name_len,
1829 CASE_SENSITIVE, rl[1].vcn, NULL, 0, ctx);
1830 if (unlikely(ret)) {
1831 ntfs_error(vol->sb, "Failed to find last attribute extent of "
1832 "mft data attribute.");
1833 if (ret == -ENOENT)
1834 ret = -EIO;
1835 goto undo_alloc;
1836 }
1837 a = ctx->attr;
1838 ll = sle64_to_cpu(a->data.non_resident.lowest_vcn);
1839
1840 for (rl2 = rl; rl2 > mft_ni->runlist.rl; rl2--) {
1841 if (ll >= rl2->vcn)
1842 break;
1843 }
1844 BUG_ON(ll < rl2->vcn);
1845 BUG_ON(ll >= rl2->vcn + rl2->length);
1846
1847 mp_size = ntfs_get_size_for_mapping_pairs(vol, rl2, ll, -1);
1848 if (unlikely(mp_size <= 0)) {
1849 ntfs_error(vol->sb, "Get size for mapping pairs failed for "
1850 "mft data attribute extent.");
1851 ret = mp_size;
1852 if (!ret)
1853 ret = -EIO;
1854 goto undo_alloc;
1855 }
1856
1857 old_alen = le32_to_cpu(a->length);
1858 ret = ntfs_attr_record_resize(ctx->mrec, a, mp_size +
1859 le16_to_cpu(a->data.non_resident.mapping_pairs_offset));
1860 if (unlikely(ret)) {
1861 if (ret != -ENOSPC) {
1862 ntfs_error(vol->sb, "Failed to resize attribute "
1863 "record for mft data attribute.");
1864 goto undo_alloc;
1865 }
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876 ntfs_error(vol->sb, "Not enough space in this mft record to "
1877 "accommodate extended mft data attribute "
1878 "extent. Cannot handle this yet.");
1879 ret = -EOPNOTSUPP;
1880 goto undo_alloc;
1881 }
1882 mp_rebuilt = true;
1883
1884 ret = ntfs_mapping_pairs_build(vol, (u8*)a +
1885 le16_to_cpu(a->data.non_resident.mapping_pairs_offset),
1886 mp_size, rl2, ll, -1, NULL);
1887 if (unlikely(ret)) {
1888 ntfs_error(vol->sb, "Failed to build mapping pairs array of "
1889 "mft data attribute.");
1890 goto undo_alloc;
1891 }
1892
1893 a->data.non_resident.highest_vcn = cpu_to_sle64(rl[1].vcn - 1);
1894
1895
1896
1897
1898
1899
1900 if (a->data.non_resident.lowest_vcn) {
1901
1902
1903
1904
1905 flush_dcache_mft_record_page(ctx->ntfs_ino);
1906 mark_mft_record_dirty(ctx->ntfs_ino);
1907 ntfs_attr_reinit_search_ctx(ctx);
1908 ret = ntfs_attr_lookup(mft_ni->type, mft_ni->name,
1909 mft_ni->name_len, CASE_SENSITIVE, 0, NULL, 0,
1910 ctx);
1911 if (unlikely(ret)) {
1912 ntfs_error(vol->sb, "Failed to find first attribute "
1913 "extent of mft data attribute.");
1914 goto restore_undo_alloc;
1915 }
1916 a = ctx->attr;
1917 }
1918 write_lock_irqsave(&mft_ni->size_lock, flags);
1919 mft_ni->allocated_size += nr << vol->cluster_size_bits;
1920 a->data.non_resident.allocated_size =
1921 cpu_to_sle64(mft_ni->allocated_size);
1922 write_unlock_irqrestore(&mft_ni->size_lock, flags);
1923
1924 flush_dcache_mft_record_page(ctx->ntfs_ino);
1925 mark_mft_record_dirty(ctx->ntfs_ino);
1926 ntfs_attr_put_search_ctx(ctx);
1927 unmap_mft_record(mft_ni);
1928 up_write(&mft_ni->runlist.lock);
1929 ntfs_debug("Done.");
1930 return 0;
1931 restore_undo_alloc:
1932 ntfs_attr_reinit_search_ctx(ctx);
1933 if (ntfs_attr_lookup(mft_ni->type, mft_ni->name, mft_ni->name_len,
1934 CASE_SENSITIVE, rl[1].vcn, NULL, 0, ctx)) {
1935 ntfs_error(vol->sb, "Failed to find last attribute extent of "
1936 "mft data attribute.%s", es);
1937 write_lock_irqsave(&mft_ni->size_lock, flags);
1938 mft_ni->allocated_size += nr << vol->cluster_size_bits;
1939 write_unlock_irqrestore(&mft_ni->size_lock, flags);
1940 ntfs_attr_put_search_ctx(ctx);
1941 unmap_mft_record(mft_ni);
1942 up_write(&mft_ni->runlist.lock);
1943
1944
1945
1946
1947 NVolSetErrors(vol);
1948 return ret;
1949 }
1950 ctx->attr->data.non_resident.highest_vcn =
1951 cpu_to_sle64(old_last_vcn - 1);
1952 undo_alloc:
1953 if (ntfs_cluster_free(mft_ni, old_last_vcn, -1, ctx) < 0) {
1954 ntfs_error(vol->sb, "Failed to free clusters from mft data "
1955 "attribute.%s", es);
1956 NVolSetErrors(vol);
1957 }
1958 a = ctx->attr;
1959 if (ntfs_rl_truncate_nolock(vol, &mft_ni->runlist, old_last_vcn)) {
1960 ntfs_error(vol->sb, "Failed to truncate mft data attribute "
1961 "runlist.%s", es);
1962 NVolSetErrors(vol);
1963 }
1964 if (mp_rebuilt && !IS_ERR(ctx->mrec)) {
1965 if (ntfs_mapping_pairs_build(vol, (u8*)a + le16_to_cpu(
1966 a->data.non_resident.mapping_pairs_offset),
1967 old_alen - le16_to_cpu(
1968 a->data.non_resident.mapping_pairs_offset),
1969 rl2, ll, -1, NULL)) {
1970 ntfs_error(vol->sb, "Failed to restore mapping pairs "
1971 "array.%s", es);
1972 NVolSetErrors(vol);
1973 }
1974 if (ntfs_attr_record_resize(ctx->mrec, a, old_alen)) {
1975 ntfs_error(vol->sb, "Failed to restore attribute "
1976 "record.%s", es);
1977 NVolSetErrors(vol);
1978 }
1979 flush_dcache_mft_record_page(ctx->ntfs_ino);
1980 mark_mft_record_dirty(ctx->ntfs_ino);
1981 } else if (IS_ERR(ctx->mrec)) {
1982 ntfs_error(vol->sb, "Failed to restore attribute search "
1983 "context.%s", es);
1984 NVolSetErrors(vol);
1985 }
1986 if (ctx)
1987 ntfs_attr_put_search_ctx(ctx);
1988 if (!IS_ERR(mrec))
1989 unmap_mft_record(mft_ni);
1990 up_write(&mft_ni->runlist.lock);
1991 return ret;
1992 }
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007 static int ntfs_mft_record_layout(const ntfs_volume *vol, const s64 mft_no,
2008 MFT_RECORD *m)
2009 {
2010 ATTR_RECORD *a;
2011
2012 ntfs_debug("Entering for mft record 0x%llx.", (long long)mft_no);
2013 if (mft_no >= (1ll << 32)) {
2014 ntfs_error(vol->sb, "Mft record number 0x%llx exceeds "
2015 "maximum of 2^32.", (long long)mft_no);
2016 return -ERANGE;
2017 }
2018
2019 memset(m, 0, vol->mft_record_size);
2020
2021 if (vol->major_ver < 3 || (vol->major_ver == 3 && !vol->minor_ver))
2022 m->usa_ofs = cpu_to_le16((sizeof(MFT_RECORD_OLD) + 1) & ~1);
2023 else {
2024 m->usa_ofs = cpu_to_le16((sizeof(MFT_RECORD) + 1) & ~1);
2025
2026
2027
2028
2029 m->reserved = 0;
2030 m->mft_record_number = cpu_to_le32((u32)mft_no);
2031 }
2032 m->magic = magic_FILE;
2033 if (vol->mft_record_size >= NTFS_BLOCK_SIZE)
2034 m->usa_count = cpu_to_le16(vol->mft_record_size /
2035 NTFS_BLOCK_SIZE + 1);
2036 else {
2037 m->usa_count = cpu_to_le16(1);
2038 ntfs_warning(vol->sb, "Sector size is bigger than mft record "
2039 "size. Setting usa_count to 1. If chkdsk "
2040 "reports this as corruption, please email "
2041 "linux-ntfs-dev@lists.sourceforge.net stating "
2042 "that you saw this message and that the "
2043 "modified filesystem created was corrupt. "
2044 "Thank you.");
2045 }
2046
2047 *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs)) = cpu_to_le16(1);
2048 m->lsn = 0;
2049 m->sequence_number = cpu_to_le16(1);
2050 m->link_count = 0;
2051
2052
2053
2054
2055 m->attrs_offset = cpu_to_le16((le16_to_cpu(m->usa_ofs) +
2056 (le16_to_cpu(m->usa_count) << 1) + 7) & ~7);
2057 m->flags = 0;
2058
2059
2060
2061
2062
2063 m->bytes_in_use = cpu_to_le32(le16_to_cpu(m->attrs_offset) + 8);
2064 m->bytes_allocated = cpu_to_le32(vol->mft_record_size);
2065 m->base_mft_record = 0;
2066 m->next_attr_instance = 0;
2067
2068 a = (ATTR_RECORD*)((u8*)m + le16_to_cpu(m->attrs_offset));
2069 a->type = AT_END;
2070 a->length = 0;
2071 ntfs_debug("Done.");
2072 return 0;
2073 }
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086 static int ntfs_mft_record_format(const ntfs_volume *vol, const s64 mft_no)
2087 {
2088 loff_t i_size;
2089 struct inode *mft_vi = vol->mft_ino;
2090 struct page *page;
2091 MFT_RECORD *m;
2092 pgoff_t index, end_index;
2093 unsigned int ofs;
2094 int err;
2095
2096 ntfs_debug("Entering for mft record 0x%llx.", (long long)mft_no);
2097
2098
2099
2100
2101 index = mft_no << vol->mft_record_size_bits >> PAGE_SHIFT;
2102 ofs = (mft_no << vol->mft_record_size_bits) & ~PAGE_MASK;
2103
2104 i_size = i_size_read(mft_vi);
2105 end_index = i_size >> PAGE_SHIFT;
2106 if (unlikely(index >= end_index)) {
2107 if (unlikely(index > end_index || ofs + vol->mft_record_size >=
2108 (i_size & ~PAGE_MASK))) {
2109 ntfs_error(vol->sb, "Tried to format non-existing mft "
2110 "record 0x%llx.", (long long)mft_no);
2111 return -ENOENT;
2112 }
2113 }
2114
2115 page = ntfs_map_page(mft_vi->i_mapping, index);
2116 if (IS_ERR(page)) {
2117 ntfs_error(vol->sb, "Failed to map page containing mft record "
2118 "to format 0x%llx.", (long long)mft_no);
2119 return PTR_ERR(page);
2120 }
2121 lock_page(page);
2122 BUG_ON(!PageUptodate(page));
2123 ClearPageUptodate(page);
2124 m = (MFT_RECORD*)((u8*)page_address(page) + ofs);
2125 err = ntfs_mft_record_layout(vol, mft_no, m);
2126 if (unlikely(err)) {
2127 ntfs_error(vol->sb, "Failed to layout mft record 0x%llx.",
2128 (long long)mft_no);
2129 SetPageUptodate(page);
2130 unlock_page(page);
2131 ntfs_unmap_page(page);
2132 return err;
2133 }
2134 flush_dcache_page(page);
2135 SetPageUptodate(page);
2136 unlock_page(page);
2137
2138
2139
2140
2141
2142 mark_ntfs_record_dirty(page, ofs);
2143 ntfs_unmap_page(page);
2144 ntfs_debug("Done.");
2145 return 0;
2146 }
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238 ntfs_inode *ntfs_mft_record_alloc(ntfs_volume *vol, const int mode,
2239 ntfs_inode *base_ni, MFT_RECORD **mrec)
2240 {
2241 s64 ll, bit, old_data_initialized, old_data_size;
2242 unsigned long flags;
2243 struct inode *vi;
2244 struct page *page;
2245 ntfs_inode *mft_ni, *mftbmp_ni, *ni;
2246 ntfs_attr_search_ctx *ctx;
2247 MFT_RECORD *m;
2248 ATTR_RECORD *a;
2249 pgoff_t index;
2250 unsigned int ofs;
2251 int err;
2252 le16 seq_no, usn;
2253 bool record_formatted = false;
2254
2255 if (base_ni) {
2256 ntfs_debug("Entering (allocating an extent mft record for "
2257 "base mft record 0x%llx).",
2258 (long long)base_ni->mft_no);
2259
2260 BUG_ON(mode);
2261 } else
2262 ntfs_debug("Entering (allocating a base mft record).");
2263 if (mode) {
2264
2265 BUG_ON(base_ni);
2266
2267 if (!S_ISREG(mode) && !S_ISDIR(mode))
2268 return ERR_PTR(-EOPNOTSUPP);
2269 }
2270 BUG_ON(!mrec);
2271 mft_ni = NTFS_I(vol->mft_ino);
2272 mftbmp_ni = NTFS_I(vol->mftbmp_ino);
2273 down_write(&vol->mftbmp_lock);
2274 bit = ntfs_mft_bitmap_find_and_alloc_free_rec_nolock(vol, base_ni);
2275 if (bit >= 0) {
2276 ntfs_debug("Found and allocated free record (#1), bit 0x%llx.",
2277 (long long)bit);
2278 goto have_alloc_rec;
2279 }
2280 if (bit != -ENOSPC) {
2281 up_write(&vol->mftbmp_lock);
2282 return ERR_PTR(bit);
2283 }
2284
2285
2286
2287
2288
2289
2290
2291
2292 read_lock_irqsave(&mft_ni->size_lock, flags);
2293 ll = mft_ni->initialized_size >> vol->mft_record_size_bits;
2294 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2295 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
2296 old_data_initialized = mftbmp_ni->initialized_size;
2297 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
2298 if (old_data_initialized << 3 > ll && old_data_initialized > 3) {
2299 bit = ll;
2300 if (bit < 24)
2301 bit = 24;
2302 if (unlikely(bit >= (1ll << 32)))
2303 goto max_err_out;
2304 ntfs_debug("Found free record (#2), bit 0x%llx.",
2305 (long long)bit);
2306 goto found_free_rec;
2307 }
2308
2309
2310
2311
2312
2313 bit = old_data_initialized << 3;
2314 if (unlikely(bit >= (1ll << 32)))
2315 goto max_err_out;
2316 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
2317 old_data_size = mftbmp_ni->allocated_size;
2318 ntfs_debug("Status of mftbmp before extension: allocated_size 0x%llx, "
2319 "data_size 0x%llx, initialized_size 0x%llx.",
2320 (long long)old_data_size,
2321 (long long)i_size_read(vol->mftbmp_ino),
2322 (long long)old_data_initialized);
2323 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
2324 if (old_data_initialized + 8 > old_data_size) {
2325
2326 ntfs_debug("mftbmp: initialized_size + 8 > allocated_size.");
2327 err = ntfs_mft_bitmap_extend_allocation_nolock(vol);
2328 if (unlikely(err)) {
2329 up_write(&vol->mftbmp_lock);
2330 goto err_out;
2331 }
2332 #ifdef DEBUG
2333 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
2334 ntfs_debug("Status of mftbmp after allocation extension: "
2335 "allocated_size 0x%llx, data_size 0x%llx, "
2336 "initialized_size 0x%llx.",
2337 (long long)mftbmp_ni->allocated_size,
2338 (long long)i_size_read(vol->mftbmp_ino),
2339 (long long)mftbmp_ni->initialized_size);
2340 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
2341 #endif
2342 }
2343
2344
2345
2346
2347
2348 err = ntfs_mft_bitmap_extend_initialized_nolock(vol);
2349 if (unlikely(err)) {
2350 up_write(&vol->mftbmp_lock);
2351 goto err_out;
2352 }
2353 #ifdef DEBUG
2354 read_lock_irqsave(&mftbmp_ni->size_lock, flags);
2355 ntfs_debug("Status of mftbmp after initialized extension: "
2356 "allocated_size 0x%llx, data_size 0x%llx, "
2357 "initialized_size 0x%llx.",
2358 (long long)mftbmp_ni->allocated_size,
2359 (long long)i_size_read(vol->mftbmp_ino),
2360 (long long)mftbmp_ni->initialized_size);
2361 read_unlock_irqrestore(&mftbmp_ni->size_lock, flags);
2362 #endif
2363 ntfs_debug("Found free record (#3), bit 0x%llx.", (long long)bit);
2364 found_free_rec:
2365
2366 ntfs_debug("At found_free_rec.");
2367 err = ntfs_bitmap_set_bit(vol->mftbmp_ino, bit);
2368 if (unlikely(err)) {
2369 ntfs_error(vol->sb, "Failed to allocate bit in mft bitmap.");
2370 up_write(&vol->mftbmp_lock);
2371 goto err_out;
2372 }
2373 ntfs_debug("Set bit 0x%llx in mft bitmap.", (long long)bit);
2374 have_alloc_rec:
2375
2376
2377
2378
2379
2380
2381
2382
2383 ll = (bit + 1) << vol->mft_record_size_bits;
2384 read_lock_irqsave(&mft_ni->size_lock, flags);
2385 old_data_initialized = mft_ni->initialized_size;
2386 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2387 if (ll <= old_data_initialized) {
2388 ntfs_debug("Allocated mft record already initialized.");
2389 goto mft_rec_already_initialized;
2390 }
2391 ntfs_debug("Initializing allocated mft record.");
2392
2393
2394
2395
2396
2397
2398 read_lock_irqsave(&mft_ni->size_lock, flags);
2399 ntfs_debug("Status of mft data before extension: "
2400 "allocated_size 0x%llx, data_size 0x%llx, "
2401 "initialized_size 0x%llx.",
2402 (long long)mft_ni->allocated_size,
2403 (long long)i_size_read(vol->mft_ino),
2404 (long long)mft_ni->initialized_size);
2405 while (ll > mft_ni->allocated_size) {
2406 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2407 err = ntfs_mft_data_extend_allocation_nolock(vol);
2408 if (unlikely(err)) {
2409 ntfs_error(vol->sb, "Failed to extend mft data "
2410 "allocation.");
2411 goto undo_mftbmp_alloc_nolock;
2412 }
2413 read_lock_irqsave(&mft_ni->size_lock, flags);
2414 ntfs_debug("Status of mft data after allocation extension: "
2415 "allocated_size 0x%llx, data_size 0x%llx, "
2416 "initialized_size 0x%llx.",
2417 (long long)mft_ni->allocated_size,
2418 (long long)i_size_read(vol->mft_ino),
2419 (long long)mft_ni->initialized_size);
2420 }
2421 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2422
2423
2424
2425
2426
2427
2428
2429 write_lock_irqsave(&mft_ni->size_lock, flags);
2430 old_data_initialized = mft_ni->initialized_size;
2431 old_data_size = vol->mft_ino->i_size;
2432 while (ll > mft_ni->initialized_size) {
2433 s64 new_initialized_size, mft_no;
2434
2435 new_initialized_size = mft_ni->initialized_size +
2436 vol->mft_record_size;
2437 mft_no = mft_ni->initialized_size >> vol->mft_record_size_bits;
2438 if (new_initialized_size > i_size_read(vol->mft_ino))
2439 i_size_write(vol->mft_ino, new_initialized_size);
2440 write_unlock_irqrestore(&mft_ni->size_lock, flags);
2441 ntfs_debug("Initializing mft record 0x%llx.",
2442 (long long)mft_no);
2443 err = ntfs_mft_record_format(vol, mft_no);
2444 if (unlikely(err)) {
2445 ntfs_error(vol->sb, "Failed to format mft record.");
2446 goto undo_data_init;
2447 }
2448 write_lock_irqsave(&mft_ni->size_lock, flags);
2449 mft_ni->initialized_size = new_initialized_size;
2450 }
2451 write_unlock_irqrestore(&mft_ni->size_lock, flags);
2452 record_formatted = true;
2453
2454 m = map_mft_record(mft_ni);
2455 if (IS_ERR(m)) {
2456 ntfs_error(vol->sb, "Failed to map mft record.");
2457 err = PTR_ERR(m);
2458 goto undo_data_init;
2459 }
2460 ctx = ntfs_attr_get_search_ctx(mft_ni, m);
2461 if (unlikely(!ctx)) {
2462 ntfs_error(vol->sb, "Failed to get search context.");
2463 err = -ENOMEM;
2464 unmap_mft_record(mft_ni);
2465 goto undo_data_init;
2466 }
2467 err = ntfs_attr_lookup(mft_ni->type, mft_ni->name, mft_ni->name_len,
2468 CASE_SENSITIVE, 0, NULL, 0, ctx);
2469 if (unlikely(err)) {
2470 ntfs_error(vol->sb, "Failed to find first attribute extent of "
2471 "mft data attribute.");
2472 ntfs_attr_put_search_ctx(ctx);
2473 unmap_mft_record(mft_ni);
2474 goto undo_data_init;
2475 }
2476 a = ctx->attr;
2477 read_lock_irqsave(&mft_ni->size_lock, flags);
2478 a->data.non_resident.initialized_size =
2479 cpu_to_sle64(mft_ni->initialized_size);
2480 a->data.non_resident.data_size =
2481 cpu_to_sle64(i_size_read(vol->mft_ino));
2482 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2483
2484 flush_dcache_mft_record_page(ctx->ntfs_ino);
2485 mark_mft_record_dirty(ctx->ntfs_ino);
2486 ntfs_attr_put_search_ctx(ctx);
2487 unmap_mft_record(mft_ni);
2488 read_lock_irqsave(&mft_ni->size_lock, flags);
2489 ntfs_debug("Status of mft data after mft record initialization: "
2490 "allocated_size 0x%llx, data_size 0x%llx, "
2491 "initialized_size 0x%llx.",
2492 (long long)mft_ni->allocated_size,
2493 (long long)i_size_read(vol->mft_ino),
2494 (long long)mft_ni->initialized_size);
2495 BUG_ON(i_size_read(vol->mft_ino) > mft_ni->allocated_size);
2496 BUG_ON(mft_ni->initialized_size > i_size_read(vol->mft_ino));
2497 read_unlock_irqrestore(&mft_ni->size_lock, flags);
2498 mft_rec_already_initialized:
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508 up_write(&vol->mftbmp_lock);
2509
2510
2511
2512
2513 index = bit << vol->mft_record_size_bits >> PAGE_SHIFT;
2514 ofs = (bit << vol->mft_record_size_bits) & ~PAGE_MASK;
2515
2516 page = ntfs_map_page(vol->mft_ino->i_mapping, index);
2517 if (IS_ERR(page)) {
2518 ntfs_error(vol->sb, "Failed to map page containing allocated "
2519 "mft record 0x%llx.", (long long)bit);
2520 err = PTR_ERR(page);
2521 goto undo_mftbmp_alloc;
2522 }
2523 lock_page(page);
2524 BUG_ON(!PageUptodate(page));
2525 ClearPageUptodate(page);
2526 m = (MFT_RECORD*)((u8*)page_address(page) + ofs);
2527
2528 if (!record_formatted) {
2529
2530 if (ntfs_is_file_record(m->magic) &&
2531 (m->flags & MFT_RECORD_IN_USE)) {
2532 ntfs_error(vol->sb, "Mft record 0x%llx was marked "
2533 "free in mft bitmap but is marked "
2534 "used itself. Corrupt filesystem. "
2535 "Unmount and run chkdsk.",
2536 (long long)bit);
2537 err = -EIO;
2538 SetPageUptodate(page);
2539 unlock_page(page);
2540 ntfs_unmap_page(page);
2541 NVolSetErrors(vol);
2542 goto undo_mftbmp_alloc;
2543 }
2544
2545
2546
2547
2548
2549
2550
2551 seq_no = m->sequence_number;
2552 usn = *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs));
2553 err = ntfs_mft_record_layout(vol, bit, m);
2554 if (unlikely(err)) {
2555 ntfs_error(vol->sb, "Failed to layout allocated mft "
2556 "record 0x%llx.", (long long)bit);
2557 SetPageUptodate(page);
2558 unlock_page(page);
2559 ntfs_unmap_page(page);
2560 goto undo_mftbmp_alloc;
2561 }
2562 if (seq_no)
2563 m->sequence_number = seq_no;
2564 if (usn && le16_to_cpu(usn) != 0xffff)
2565 *(le16*)((u8*)m + le16_to_cpu(m->usa_ofs)) = usn;
2566 }
2567
2568 m->flags |= MFT_RECORD_IN_USE;
2569 if (S_ISDIR(mode))
2570 m->flags |= MFT_RECORD_IS_DIRECTORY;
2571 flush_dcache_page(page);
2572 SetPageUptodate(page);
2573 if (base_ni) {
2574 MFT_RECORD *m_tmp;
2575
2576
2577
2578
2579
2580
2581 m->base_mft_record = MK_LE_MREF(base_ni->mft_no,
2582 base_ni->seq_no);
2583
2584
2585
2586
2587
2588 m_tmp = map_extent_mft_record(base_ni, bit, &ni);
2589 if (IS_ERR(m_tmp)) {
2590 ntfs_error(vol->sb, "Failed to map allocated extent "
2591 "mft record 0x%llx.", (long long)bit);
2592 err = PTR_ERR(m_tmp);
2593
2594 m->flags &= cpu_to_le16(
2595 ~le16_to_cpu(MFT_RECORD_IN_USE));
2596 flush_dcache_page(page);
2597
2598 mark_ntfs_record_dirty(page, ofs);
2599 unlock_page(page);
2600 ntfs_unmap_page(page);
2601 goto undo_mftbmp_alloc;
2602 }
2603 BUG_ON(m != m_tmp);
2604
2605
2606
2607
2608
2609
2610
2611 mark_ntfs_record_dirty(page, ofs);
2612 unlock_page(page);
2613
2614
2615
2616
2617 ntfs_unmap_page(page);
2618 } else {
2619
2620
2621
2622
2623
2624 vi = new_inode(vol->sb);
2625 if (unlikely(!vi)) {
2626 err = -ENOMEM;
2627
2628 m->flags &= cpu_to_le16(
2629 ~le16_to_cpu(MFT_RECORD_IN_USE));
2630 flush_dcache_page(page);
2631
2632 mark_ntfs_record_dirty(page, ofs);
2633 unlock_page(page);
2634 ntfs_unmap_page(page);
2635 goto undo_mftbmp_alloc;
2636 }
2637 vi->i_ino = bit;
2638
2639
2640 vi->i_uid = vol->uid;
2641 vi->i_gid = vol->gid;
2642
2643
2644 ntfs_init_big_inode(vi);
2645 ni = NTFS_I(vi);
2646
2647
2648
2649
2650 if (S_ISDIR(mode)) {
2651 vi->i_mode = S_IFDIR | S_IRWXUGO;
2652 vi->i_mode &= ~vol->dmask;
2653
2654 NInoSetMstProtected(ni);
2655 ni->type = AT_INDEX_ALLOCATION;
2656 ni->name = I30;
2657 ni->name_len = 4;
2658
2659 ni->itype.index.block_size = 4096;
2660 ni->itype.index.block_size_bits = ntfs_ffs(4096) - 1;
2661 ni->itype.index.collation_rule = COLLATION_FILE_NAME;
2662 if (vol->cluster_size <= ni->itype.index.block_size) {
2663 ni->itype.index.vcn_size = vol->cluster_size;
2664 ni->itype.index.vcn_size_bits =
2665 vol->cluster_size_bits;
2666 } else {
2667 ni->itype.index.vcn_size = vol->sector_size;
2668 ni->itype.index.vcn_size_bits =
2669 vol->sector_size_bits;
2670 }
2671 } else {
2672 vi->i_mode = S_IFREG | S_IRWXUGO;
2673 vi->i_mode &= ~vol->fmask;
2674
2675 ni->type = AT_DATA;
2676 ni->name = NULL;
2677 ni->name_len = 0;
2678 }
2679 if (IS_RDONLY(vi))
2680 vi->i_mode &= ~S_IWUGO;
2681
2682
2683 vi->i_atime = vi->i_mtime = vi->i_ctime =
2684 current_time(vi);
2685
2686
2687
2688
2689 vi->i_size = 0;
2690 vi->i_blocks = 0;
2691
2692
2693 vi->i_generation = ni->seq_no = le16_to_cpu(m->sequence_number);
2694
2695
2696
2697
2698 atomic_inc(&ni->count);
2699 mutex_lock(&ni->mrec_lock);
2700 ni->page = page;
2701 ni->page_ofs = ofs;
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712 mark_ntfs_record_dirty(page, ofs);
2713 unlock_page(page);
2714
2715
2716 insert_inode_hash(vi);
2717
2718
2719 vol->mft_data_pos = bit + 1;
2720 }
2721
2722
2723
2724
2725 ntfs_debug("Returning opened, allocated %sinode 0x%llx.",
2726 base_ni ? "extent " : "", (long long)bit);
2727 *mrec = m;
2728 return ni;
2729 undo_data_init:
2730 write_lock_irqsave(&mft_ni->size_lock, flags);
2731 mft_ni->initialized_size = old_data_initialized;
2732 i_size_write(vol->mft_ino, old_data_size);
2733 write_unlock_irqrestore(&mft_ni->size_lock, flags);
2734 goto undo_mftbmp_alloc_nolock;
2735 undo_mftbmp_alloc:
2736 down_write(&vol->mftbmp_lock);
2737 undo_mftbmp_alloc_nolock:
2738 if (ntfs_bitmap_clear_bit(vol->mftbmp_ino, bit)) {
2739 ntfs_error(vol->sb, "Failed to clear bit in mft bitmap.%s", es);
2740 NVolSetErrors(vol);
2741 }
2742 up_write(&vol->mftbmp_lock);
2743 err_out:
2744 return ERR_PTR(err);
2745 max_err_out:
2746 ntfs_warning(vol->sb, "Cannot allocate mft record because the maximum "
2747 "number of inodes (2^32) has already been reached.");
2748 up_write(&vol->mftbmp_lock);
2749 return ERR_PTR(-ENOSPC);
2750 }
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773 int ntfs_extent_mft_record_free(ntfs_inode *ni, MFT_RECORD *m)
2774 {
2775 unsigned long mft_no = ni->mft_no;
2776 ntfs_volume *vol = ni->vol;
2777 ntfs_inode *base_ni;
2778 ntfs_inode **extent_nis;
2779 int i, err;
2780 le16 old_seq_no;
2781 u16 seq_no;
2782
2783 BUG_ON(NInoAttr(ni));
2784 BUG_ON(ni->nr_extents != -1);
2785
2786 mutex_lock(&ni->extent_lock);
2787 base_ni = ni->ext.base_ntfs_ino;
2788 mutex_unlock(&ni->extent_lock);
2789
2790 BUG_ON(base_ni->nr_extents <= 0);
2791
2792 ntfs_debug("Entering for extent inode 0x%lx, base inode 0x%lx.\n",
2793 mft_no, base_ni->mft_no);
2794
2795 mutex_lock(&base_ni->extent_lock);
2796
2797
2798 if (atomic_read(&ni->count) > 2) {
2799 ntfs_error(vol->sb, "Tried to free busy extent inode 0x%lx, "
2800 "not freeing.", base_ni->mft_no);
2801 mutex_unlock(&base_ni->extent_lock);
2802 return -EBUSY;
2803 }
2804
2805
2806 extent_nis = base_ni->ext.extent_ntfs_inos;
2807 err = -ENOENT;
2808 for (i = 0; i < base_ni->nr_extents; i++) {
2809 if (ni != extent_nis[i])
2810 continue;
2811 extent_nis += i;
2812 base_ni->nr_extents--;
2813 memmove(extent_nis, extent_nis + 1, (base_ni->nr_extents - i) *
2814 sizeof(ntfs_inode*));
2815 err = 0;
2816 break;
2817 }
2818
2819 mutex_unlock(&base_ni->extent_lock);
2820
2821 if (unlikely(err)) {
2822 ntfs_error(vol->sb, "Extent inode 0x%lx is not attached to "
2823 "its base inode 0x%lx.", mft_no,
2824 base_ni->mft_no);
2825 BUG();
2826 }
2827
2828
2829
2830
2831
2832
2833
2834 m->flags &= ~MFT_RECORD_IN_USE;
2835
2836
2837 old_seq_no = m->sequence_number;
2838 seq_no = le16_to_cpu(old_seq_no);
2839 if (seq_no == 0xffff)
2840 seq_no = 1;
2841 else if (seq_no)
2842 seq_no++;
2843 m->sequence_number = cpu_to_le16(seq_no);
2844
2845
2846
2847
2848
2849
2850 NInoSetDirty(ni);
2851 err = write_mft_record(ni, m, 0);
2852 if (unlikely(err)) {
2853 ntfs_error(vol->sb, "Failed to write mft record 0x%lx, not "
2854 "freeing.", mft_no);
2855 goto rollback;
2856 }
2857 rollback_error:
2858
2859 unmap_extent_mft_record(ni);
2860 ntfs_clear_extent_inode(ni);
2861
2862
2863 down_write(&vol->mftbmp_lock);
2864 err = ntfs_bitmap_clear_bit(vol->mftbmp_ino, mft_no);
2865 up_write(&vol->mftbmp_lock);
2866 if (unlikely(err)) {
2867
2868
2869
2870
2871
2872 ntfs_error(vol->sb, "Failed to clear bit in mft bitmap.%s", es);
2873 NVolSetErrors(vol);
2874 }
2875 return 0;
2876 rollback:
2877
2878 mutex_lock(&base_ni->extent_lock);
2879 extent_nis = base_ni->ext.extent_ntfs_inos;
2880 if (!(base_ni->nr_extents & 3)) {
2881 int new_size = (base_ni->nr_extents + 4) * sizeof(ntfs_inode*);
2882
2883 extent_nis = kmalloc(new_size, GFP_NOFS);
2884 if (unlikely(!extent_nis)) {
2885 ntfs_error(vol->sb, "Failed to allocate internal "
2886 "buffer during rollback.%s", es);
2887 mutex_unlock(&base_ni->extent_lock);
2888 NVolSetErrors(vol);
2889 goto rollback_error;
2890 }
2891 if (base_ni->nr_extents) {
2892 BUG_ON(!base_ni->ext.extent_ntfs_inos);
2893 memcpy(extent_nis, base_ni->ext.extent_ntfs_inos,
2894 new_size - 4 * sizeof(ntfs_inode*));
2895 kfree(base_ni->ext.extent_ntfs_inos);
2896 }
2897 base_ni->ext.extent_ntfs_inos = extent_nis;
2898 }
2899 m->flags |= MFT_RECORD_IN_USE;
2900 m->sequence_number = old_seq_no;
2901 extent_nis[base_ni->nr_extents++] = ni;
2902 mutex_unlock(&base_ni->extent_lock);
2903 mark_mft_record_dirty(ni);
2904 return err;
2905 }
2906 #endif