@@ -54,6 +54,8 @@ int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol,
5454 int ret = 0 ;
5555
5656 ntfs_debug ("Entering." );
57+ if (!rl )
58+ return 0 ;
5759 for (; rl -> length ; rl ++ ) {
5860 int err ;
5961
@@ -163,17 +165,9 @@ runlist_element *ntfs_cluster_alloc(ntfs_volume *vol, const VCN start_vcn,
163165 BUG_ON (zone < FIRST_ZONE );
164166 BUG_ON (zone > LAST_ZONE );
165167
166- /* Return empty runlist if @count == 0 */
167- // FIXME: Do we want to just return NULL instead? (AIA)
168- if (!count ) {
169- rl = ntfs_malloc_nofs (PAGE_SIZE );
170- if (!rl )
171- return ERR_PTR (- ENOMEM );
172- rl [0 ].vcn = start_vcn ;
173- rl [0 ].lcn = LCN_RL_NOT_MAPPED ;
174- rl [0 ].length = 0 ;
175- return rl ;
176- }
168+ /* Return NULL if @count is zero. */
169+ if (!count )
170+ return NULL ;
177171 /* Take the lcnbmp lock for writing. */
178172 down_write (& vol -> lcnbmp_lock );
179173 /*
@@ -788,7 +782,8 @@ switch_to_data1_zone: search_zone = 2;
788782 * @vi: vfs inode whose runlist describes the clusters to free
789783 * @start_vcn: vcn in the runlist of @vi at which to start freeing clusters
790784 * @count: number of clusters to free or -1 for all clusters
791- * @is_rollback: if TRUE this is a rollback operation
785+ * @write_locked: true if the runlist is locked for writing
786+ * @is_rollback: true if this is a rollback operation
792787 *
793788 * Free @count clusters starting at the cluster @start_vcn in the runlist
794789 * described by the vfs inode @vi.
@@ -806,17 +801,17 @@ switch_to_data1_zone: search_zone = 2;
806801 * Return the number of deallocated clusters (not counting sparse ones) on
807802 * success and -errno on error.
808803 *
809- * Locking: - The runlist described by @vi must be unlocked on entry and is
810- * unlocked on return.
811- * - This function takes the runlist lock of @vi for reading and
812- * sometimes for writing and sometimes modifies the runlist .
804+ * Locking: - The runlist described by @vi must be locked on entry and is
805+ * locked on return. Note if the runlist is locked for reading the
806+ * lock may be dropped and reacquired. Note the runlist may be
807+ * modified when needed runlist fragments need to be mapped .
813808 * - The volume lcn bitmap must be unlocked on entry and is unlocked
814809 * on return.
815810 * - This function takes the volume lcn bitmap lock for writing and
816811 * modifies the bitmap contents.
817812 */
818813s64 __ntfs_cluster_free (struct inode * vi , const VCN start_vcn , s64 count ,
819- const BOOL is_rollback )
814+ const BOOL write_locked , const BOOL is_rollback )
820815{
821816 s64 delta , to_free , total_freed , real_freed ;
822817 ntfs_inode * ni ;
@@ -848,8 +843,7 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
848843
849844 total_freed = real_freed = 0 ;
850845
851- down_read (& ni -> runlist .lock );
852- rl = ntfs_attr_find_vcn_nolock (ni , start_vcn , FALSE);
846+ rl = ntfs_attr_find_vcn_nolock (ni , start_vcn , write_locked );
853847 if (IS_ERR (rl )) {
854848 if (!is_rollback )
855849 ntfs_error (vol -> sb , "Failed to find first runlist "
@@ -903,7 +897,7 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
903897
904898 /* Attempt to map runlist. */
905899 vcn = rl -> vcn ;
906- rl = ntfs_attr_find_vcn_nolock (ni , vcn , FALSE );
900+ rl = ntfs_attr_find_vcn_nolock (ni , vcn , write_locked );
907901 if (IS_ERR (rl )) {
908902 err = PTR_ERR (rl );
909903 if (!is_rollback )
@@ -950,7 +944,6 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
950944 /* Update the total done clusters. */
951945 total_freed += to_free ;
952946 }
953- up_read (& ni -> runlist .lock );
954947 if (likely (!is_rollback ))
955948 up_write (& vol -> lcnbmp_lock );
956949
@@ -960,7 +953,6 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
960953 ntfs_debug ("Done." );
961954 return real_freed ;
962955err_out :
963- up_read (& ni -> runlist .lock );
964956 if (is_rollback )
965957 return err ;
966958 /* If no real clusters were freed, no need to rollback. */
@@ -973,7 +965,8 @@ s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
973965 * If rollback fails, set the volume errors flag, emit an error
974966 * message, and return the error code.
975967 */
976- delta = __ntfs_cluster_free (vi , start_vcn , total_freed , TRUE);
968+ delta = __ntfs_cluster_free (vi , start_vcn , total_freed , write_locked ,
969+ TRUE);
977970 if (delta < 0 ) {
978971 ntfs_error (vol -> sb , "Failed to rollback (error %i). Leaving "
979972 "inconsistent metadata! Unmount and run "
0 commit comments