diff options
Diffstat (limited to 'fs/ntfs/lcnalloc.c')
| -rw-r--r-- | fs/ntfs/lcnalloc.c | 56 |
1 files changed, 47 insertions, 9 deletions
diff --git a/fs/ntfs/lcnalloc.c b/fs/ntfs/lcnalloc.c index 5af3bf0b7eee..29cabf93d2d2 100644 --- a/fs/ntfs/lcnalloc.c +++ b/fs/ntfs/lcnalloc.c | |||
| @@ -76,6 +76,7 @@ int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol, | |||
| 76 | * @count: number of clusters to allocate | 76 | * @count: number of clusters to allocate |
| 77 | * @start_lcn: starting lcn at which to allocate the clusters (or -1 if none) | 77 | * @start_lcn: starting lcn at which to allocate the clusters (or -1 if none) |
| 78 | * @zone: zone from which to allocate the clusters | 78 | * @zone: zone from which to allocate the clusters |
| 79 | * @is_extension: if TRUE, this is an attribute extension | ||
| 79 | * | 80 | * |
| 80 | * Allocate @count clusters preferably starting at cluster @start_lcn or at the | 81 | * Allocate @count clusters preferably starting at cluster @start_lcn or at the |
| 81 | * current allocator position if @start_lcn is -1, on the mounted ntfs volume | 82 | * current allocator position if @start_lcn is -1, on the mounted ntfs volume |
| @@ -86,6 +87,13 @@ int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol, | |||
| 86 | * @start_vcn specifies the vcn of the first allocated cluster. This makes | 87 | * @start_vcn specifies the vcn of the first allocated cluster. This makes |
| 87 | * merging the resulting runlist with the old runlist easier. | 88 | * merging the resulting runlist with the old runlist easier. |
| 88 | * | 89 | * |
| 90 | * If @is_extension is TRUE, the caller is allocating clusters to extend an | ||
| 91 | * attribute and if it is FALSE, the caller is allocating clusters to fill a | ||
| 92 | * hole in an attribute. Practically the difference is that if @is_extension | ||
| 93 | * is TRUE the returned runlist will be terminated with LCN_ENOENT and if | ||
| 94 | * @is_extension is FALSE the runlist will be terminated with | ||
| 95 | * LCN_RL_NOT_MAPPED. | ||
| 96 | * | ||
| 89 | * You need to check the return value with IS_ERR(). If this is false, the | 97 | * You need to check the return value with IS_ERR(). If this is false, the |
| 90 | * function was successful and the return value is a runlist describing the | 98 | * function was successful and the return value is a runlist describing the |
| 91 | * allocated cluster(s). If IS_ERR() is true, the function failed and | 99 | * allocated cluster(s). If IS_ERR() is true, the function failed and |
| @@ -137,7 +145,8 @@ int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol, | |||
| 137 | */ | 145 | */ |
| 138 | runlist_element *ntfs_cluster_alloc(ntfs_volume *vol, const VCN start_vcn, | 146 | runlist_element *ntfs_cluster_alloc(ntfs_volume *vol, const VCN start_vcn, |
| 139 | const s64 count, const LCN start_lcn, | 147 | const s64 count, const LCN start_lcn, |
| 140 | const NTFS_CLUSTER_ALLOCATION_ZONES zone) | 148 | const NTFS_CLUSTER_ALLOCATION_ZONES zone, |
| 149 | const BOOL is_extension) | ||
| 141 | { | 150 | { |
| 142 | LCN zone_start, zone_end, bmp_pos, bmp_initial_pos, last_read_pos, lcn; | 151 | LCN zone_start, zone_end, bmp_pos, bmp_initial_pos, last_read_pos, lcn; |
| 143 | LCN prev_lcn = 0, prev_run_len = 0, mft_zone_size; | 152 | LCN prev_lcn = 0, prev_run_len = 0, mft_zone_size; |
| @@ -310,7 +319,7 @@ runlist_element *ntfs_cluster_alloc(ntfs_volume *vol, const VCN start_vcn, | |||
| 310 | continue; | 319 | continue; |
| 311 | } | 320 | } |
| 312 | bit = 1 << (lcn & 7); | 321 | bit = 1 << (lcn & 7); |
| 313 | ntfs_debug("bit %i.", bit); | 322 | ntfs_debug("bit 0x%x.", bit); |
| 314 | /* If the bit is already set, go onto the next one. */ | 323 | /* If the bit is already set, go onto the next one. */ |
| 315 | if (*byte & bit) { | 324 | if (*byte & bit) { |
| 316 | lcn++; | 325 | lcn++; |
| @@ -729,7 +738,7 @@ out: | |||
| 729 | /* Add runlist terminator element. */ | 738 | /* Add runlist terminator element. */ |
| 730 | if (likely(rl)) { | 739 | if (likely(rl)) { |
| 731 | rl[rlpos].vcn = rl[rlpos - 1].vcn + rl[rlpos - 1].length; | 740 | rl[rlpos].vcn = rl[rlpos - 1].vcn + rl[rlpos - 1].length; |
| 732 | rl[rlpos].lcn = LCN_RL_NOT_MAPPED; | 741 | rl[rlpos].lcn = is_extension ? LCN_ENOENT : LCN_RL_NOT_MAPPED; |
| 733 | rl[rlpos].length = 0; | 742 | rl[rlpos].length = 0; |
| 734 | } | 743 | } |
| 735 | if (likely(page && !IS_ERR(page))) { | 744 | if (likely(page && !IS_ERR(page))) { |
| @@ -782,6 +791,7 @@ out: | |||
| 782 | * @ni: ntfs inode whose runlist describes the clusters to free | 791 | * @ni: ntfs inode whose runlist describes the clusters to free |
| 783 | * @start_vcn: vcn in the runlist of @ni at which to start freeing clusters | 792 | * @start_vcn: vcn in the runlist of @ni at which to start freeing clusters |
| 784 | * @count: number of clusters to free or -1 for all clusters | 793 | * @count: number of clusters to free or -1 for all clusters |
| 794 | * @ctx: active attribute search context if present or NULL if not | ||
| 785 | * @is_rollback: true if this is a rollback operation | 795 | * @is_rollback: true if this is a rollback operation |
| 786 | * | 796 | * |
| 787 | * Free @count clusters starting at the cluster @start_vcn in the runlist | 797 | * Free @count clusters starting at the cluster @start_vcn in the runlist |
| @@ -791,15 +801,39 @@ out: | |||
| 791 | * deallocated. Thus, to completely free all clusters in a runlist, use | 801 | * deallocated. Thus, to completely free all clusters in a runlist, use |
| 792 | * @start_vcn = 0 and @count = -1. | 802 | * @start_vcn = 0 and @count = -1. |
| 793 | * | 803 | * |
| 804 | * If @ctx is specified, it is an active search context of @ni and its base mft | ||
| 805 | * record. This is needed when __ntfs_cluster_free() encounters unmapped | ||
| 806 | * runlist fragments and allows their mapping. If you do not have the mft | ||
| 807 | * record mapped, you can specify @ctx as NULL and __ntfs_cluster_free() will | ||
| 808 | * perform the necessary mapping and unmapping. | ||
| 809 | * | ||
| 810 | * Note, __ntfs_cluster_free() saves the state of @ctx on entry and restores it | ||
| 811 | * before returning. Thus, @ctx will be left pointing to the same attribute on | ||
| 812 | * return as on entry. However, the actual pointers in @ctx may point to | ||
| 813 | * different memory locations on return, so you must remember to reset any | ||
| 814 | * cached pointers from the @ctx, i.e. after the call to __ntfs_cluster_free(), | ||
| 815 | * you will probably want to do: | ||
| 816 | * m = ctx->mrec; | ||
| 817 | * a = ctx->attr; | ||
| 818 | * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that | ||
| 819 | * you cache ctx->mrec in a variable @m of type MFT_RECORD *. | ||
| 820 | * | ||
| 794 | * @is_rollback should always be FALSE, it is for internal use to rollback | 821 | * @is_rollback should always be FALSE, it is for internal use to rollback |
| 795 | * errors. You probably want to use ntfs_cluster_free() instead. | 822 | * errors. You probably want to use ntfs_cluster_free() instead. |
| 796 | * | 823 | * |
| 797 | * Note, ntfs_cluster_free() does not modify the runlist at all, so the caller | 824 | * Note, __ntfs_cluster_free() does not modify the runlist, so you have to |
| 798 | * has to deal with it later. | 825 | * remove from the runlist or mark sparse the freed runs later. |
| 799 | * | 826 | * |
| 800 | * Return the number of deallocated clusters (not counting sparse ones) on | 827 | * Return the number of deallocated clusters (not counting sparse ones) on |
| 801 | * success and -errno on error. | 828 | * success and -errno on error. |
| 802 | * | 829 | * |
| 830 | * WARNING: If @ctx is supplied, regardless of whether success or failure is | ||
| 831 | * returned, you need to check IS_ERR(@ctx->mrec) and if TRUE the @ctx | ||
| 832 | * is no longer valid, i.e. you need to either call | ||
| 833 | * ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it. | ||
| 834 | * In that case PTR_ERR(@ctx->mrec) will give you the error code for | ||
| 835 | * why the mapping of the old inode failed. | ||
| 836 | * | ||
| 803 | * Locking: - The runlist described by @ni must be locked for writing on entry | 837 | * Locking: - The runlist described by @ni must be locked for writing on entry |
| 804 | * and is locked on return. Note the runlist may be modified when | 838 | * and is locked on return. Note the runlist may be modified when |
| 805 | * needed runlist fragments need to be mapped. | 839 | * needed runlist fragments need to be mapped. |
| @@ -807,9 +841,13 @@ out: | |||
| 807 | * on return. | 841 | * on return. |
| 808 | * - This function takes the volume lcn bitmap lock for writing and | 842 | * - This function takes the volume lcn bitmap lock for writing and |
| 809 | * modifies the bitmap contents. | 843 | * modifies the bitmap contents. |
| 844 | * - If @ctx is NULL, the base mft record of @ni must not be mapped on | ||
| 845 | * entry and it will be left unmapped on return. | ||
| 846 | * - If @ctx is not NULL, the base mft record must be mapped on entry | ||
| 847 | * and it will be left mapped on return. | ||
| 810 | */ | 848 | */ |
| 811 | s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn, s64 count, | 849 | s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn, s64 count, |
| 812 | const BOOL is_rollback) | 850 | ntfs_attr_search_ctx *ctx, const BOOL is_rollback) |
| 813 | { | 851 | { |
| 814 | s64 delta, to_free, total_freed, real_freed; | 852 | s64 delta, to_free, total_freed, real_freed; |
| 815 | ntfs_volume *vol; | 853 | ntfs_volume *vol; |
| @@ -839,7 +877,7 @@ s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn, s64 count, | |||
| 839 | 877 | ||
| 840 | total_freed = real_freed = 0; | 878 | total_freed = real_freed = 0; |
| 841 | 879 | ||
| 842 | rl = ntfs_attr_find_vcn_nolock(ni, start_vcn, TRUE); | 880 | rl = ntfs_attr_find_vcn_nolock(ni, start_vcn, ctx); |
| 843 | if (IS_ERR(rl)) { | 881 | if (IS_ERR(rl)) { |
| 844 | if (!is_rollback) | 882 | if (!is_rollback) |
| 845 | ntfs_error(vol->sb, "Failed to find first runlist " | 883 | ntfs_error(vol->sb, "Failed to find first runlist " |
| @@ -893,7 +931,7 @@ s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn, s64 count, | |||
| 893 | 931 | ||
| 894 | /* Attempt to map runlist. */ | 932 | /* Attempt to map runlist. */ |
| 895 | vcn = rl->vcn; | 933 | vcn = rl->vcn; |
| 896 | rl = ntfs_attr_find_vcn_nolock(ni, vcn, TRUE); | 934 | rl = ntfs_attr_find_vcn_nolock(ni, vcn, ctx); |
| 897 | if (IS_ERR(rl)) { | 935 | if (IS_ERR(rl)) { |
| 898 | err = PTR_ERR(rl); | 936 | err = PTR_ERR(rl); |
| 899 | if (!is_rollback) | 937 | if (!is_rollback) |
| @@ -961,7 +999,7 @@ err_out: | |||
| 961 | * If rollback fails, set the volume errors flag, emit an error | 999 | * If rollback fails, set the volume errors flag, emit an error |
| 962 | * message, and return the error code. | 1000 | * message, and return the error code. |
| 963 | */ | 1001 | */ |
| 964 | delta = __ntfs_cluster_free(ni, start_vcn, total_freed, TRUE); | 1002 | delta = __ntfs_cluster_free(ni, start_vcn, total_freed, ctx, TRUE); |
| 965 | if (delta < 0) { | 1003 | if (delta < 0) { |
| 966 | ntfs_error(vol->sb, "Failed to rollback (error %i). Leaving " | 1004 | ntfs_error(vol->sb, "Failed to rollback (error %i). Leaving " |
| 967 | "inconsistent metadata! Unmount and run " | 1005 | "inconsistent metadata! Unmount and run " |
