@@ -782,6 +782,7 @@ switch_to_data1_zone: search_zone = 2;
782782 * @ni: ntfs inode whose runlist describes the clusters to free
783783 * @start_vcn: vcn in the runlist of @ni at which to start freeing clusters
784784 * @count: number of clusters to free or -1 for all clusters
785+ * @ctx: active attribute search context if present or NULL if not
785786 * @is_rollback: true if this is a rollback operation
786787 *
787788 * Free @count clusters starting at the cluster @start_vcn in the runlist
@@ -791,25 +792,53 @@ switch_to_data1_zone: search_zone = 2;
791792 * deallocated. Thus, to completely free all clusters in a runlist, use
792793 * @start_vcn = 0 and @count = -1.
793794 *
795+ * If @ctx is specified, it is an active search context of @ni and its base mft
796+ * record. This is needed when __ntfs_cluster_free() encounters unmapped
797+ * runlist fragments and allows their mapping. If you do not have the mft
798+ * record mapped, you can specify @ctx as NULL and __ntfs_cluster_free() will
799+ * perform the necessary mapping and unmapping.
800+ *
801+ * Note, __ntfs_cluster_free() saves the state of @ctx on entry and restores it
802+ * before returning. Thus, @ctx will be left pointing to the same attribute on
803+ * return as on entry. However, the actual pointers in @ctx may point to
804+ * different memory locations on return, so you must remember to reset any
805+ * cached pointers from the @ctx, i.e. after the call to __ntfs_cluster_free(),
806+ * you will probably want to do:
807+ * m = ctx->mrec;
808+ * a = ctx->attr;
809+ * Assuming you cache ctx->attr in a variable @a of type ATTR_RECORD * and that
810+ * you cache ctx->mrec in a variable @m of type MFT_RECORD *.
811+ *
794812 * @is_rollback should always be FALSE, it is for internal use to rollback
795813 * errors. You probably want to use ntfs_cluster_free() instead.
796814 *
797- * Note, ntfs_cluster_free () does not modify the runlist at all , so the caller
798- * has to deal with it later.
815+ * Note, __ntfs_cluster_free () does not modify the runlist, so you have to
816+ * remove from the runlist or mark sparse the freed runs later.
799817 *
800818 * Return the number of deallocated clusters (not counting sparse ones) on
801819 * success and -errno on error.
802820 *
821+ * WARNING: If @ctx is supplied, regardless of whether success or failure is
822+ * returned, you need to check IS_ERR(@ctx->mrec) and if TRUE the @ctx
823+ * is no longer valid, i.e. you need to either call
824+ * ntfs_attr_reinit_search_ctx() or ntfs_attr_put_search_ctx() on it.
825+ * In that case PTR_ERR(@ctx->mrec) will give you the error code for
826+ * why the mapping of the old inode failed.
827+ *
803828 * Locking: - The runlist described by @ni must be locked for writing on entry
804829 * and is locked on return. Note the runlist may be modified when
805830 * needed runlist fragments need to be mapped.
806831 * - The volume lcn bitmap must be unlocked on entry and is unlocked
807832 * on return.
808833 * - This function takes the volume lcn bitmap lock for writing and
809834 * modifies the bitmap contents.
835+ * - If @ctx is NULL, the base mft record of @ni must not be mapped on
836+ * entry and it will be left unmapped on return.
837+ * - If @ctx is not NULL, the base mft record must be mapped on entry
838+ * and it will be left mapped on return.
810839 */
811840s64 __ntfs_cluster_free (ntfs_inode * ni , const VCN start_vcn , s64 count ,
812- const BOOL is_rollback )
841+ ntfs_attr_search_ctx * ctx , const BOOL is_rollback )
813842{
814843 s64 delta , to_free , total_freed , real_freed ;
815844 ntfs_volume * vol ;
@@ -839,7 +868,7 @@ s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn, s64 count,
839868
840869 total_freed = real_freed = 0 ;
841870
842- rl = ntfs_attr_find_vcn_nolock (ni , start_vcn , NULL );
871+ rl = ntfs_attr_find_vcn_nolock (ni , start_vcn , ctx );
843872 if (IS_ERR (rl )) {
844873 if (!is_rollback )
845874 ntfs_error (vol -> sb , "Failed to find first runlist "
@@ -893,7 +922,7 @@ s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn, s64 count,
893922
894923 /* Attempt to map runlist. */
895924 vcn = rl -> vcn ;
896- rl = ntfs_attr_find_vcn_nolock (ni , vcn , NULL );
925+ rl = ntfs_attr_find_vcn_nolock (ni , vcn , ctx );
897926 if (IS_ERR (rl )) {
898927 err = PTR_ERR (rl );
899928 if (!is_rollback )
@@ -961,7 +990,7 @@ s64 __ntfs_cluster_free(ntfs_inode *ni, const VCN start_vcn, s64 count,
961990 * If rollback fails, set the volume errors flag, emit an error
962991 * message, and return the error code.
963992 */
964- delta = __ntfs_cluster_free (ni , start_vcn , total_freed , TRUE);
993+ delta = __ntfs_cluster_free (ni , start_vcn , total_freed , ctx , TRUE);
965994 if (delta < 0 ) {
966995 ntfs_error (vol -> sb , "Failed to rollback (error %i). Leaving "
967996 "inconsistent metadata! Unmount and run "
0 commit comments