11/**
22 * attrib.c - NTFS attribute operations. Part of the Linux-NTFS project.
33 *
4- * Copyright (c) 2001-2004 Anton Altaparmakov
4+ * Copyright (c) 2001-2005 Anton Altaparmakov
55 * Copyright (c) 2002 Richard Russon
66 *
77 * This program/include file is free software; you can redistribute it and/or
3030#include "types.h"
3131
3232/**
33- * ntfs_map_runlist - map (a part of) a runlist of an ntfs inode
33+ * ntfs_map_runlist_nolock - map (a part of) a runlist of an ntfs inode
3434 * @ni: ntfs inode for which to map (part of) a runlist
3535 * @vcn: map runlist part containing this vcn
3636 *
3737 * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
3838 *
3939 * Return 0 on success and -errno on error.
4040 *
41- * Locking: - The runlist must be unlocked on entry and is unlocked on return .
42- * - This function takes the lock for writing and modifies the runlist.
41+ * Locking: - The runlist must be locked for writing .
42+ * - This function modifies the runlist.
4343 */
44- int ntfs_map_runlist (ntfs_inode * ni , VCN vcn )
44+ int ntfs_map_runlist_nolock (ntfs_inode * ni , VCN vcn )
4545{
4646 ntfs_inode * base_ni ;
47- ntfs_attr_search_ctx * ctx ;
4847 MFT_RECORD * mrec ;
48+ ntfs_attr_search_ctx * ctx ;
49+ runlist_element * rl ;
4950 int err = 0 ;
5051
5152 ntfs_debug ("Mapping runlist part containing vcn 0x%llx." ,
5253 (unsigned long long )vcn );
53-
5454 if (!NInoAttr (ni ))
5555 base_ni = ni ;
5656 else
5757 base_ni = ni -> ext .base_ntfs_ino ;
58-
5958 mrec = map_mft_record (base_ni );
6059 if (IS_ERR (mrec ))
6160 return PTR_ERR (mrec );
@@ -66,43 +65,60 @@ int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
6665 }
6766 err = ntfs_attr_lookup (ni -> type , ni -> name , ni -> name_len ,
6867 CASE_SENSITIVE , vcn , NULL , 0 , ctx );
69- if (unlikely (err ))
70- goto put_err_out ;
71-
72- down_write (& ni -> runlist .lock );
73- /* Make sure someone else didn't do the work while we were sleeping. */
74- if (likely (ntfs_rl_vcn_to_lcn (ni -> runlist .rl , vcn ) <=
75- LCN_RL_NOT_MAPPED )) {
76- runlist_element * rl ;
77-
68+ if (likely (!err )) {
7869 rl = ntfs_mapping_pairs_decompress (ni -> vol , ctx -> attr ,
7970 ni -> runlist .rl );
8071 if (IS_ERR (rl ))
8172 err = PTR_ERR (rl );
8273 else
8374 ni -> runlist .rl = rl ;
8475 }
85- up_write (& ni -> runlist .lock );
86-
87- put_err_out :
8876 ntfs_attr_put_search_ctx (ctx );
8977err_out :
9078 unmap_mft_record (base_ni );
9179 return err ;
9280}
9381
9482/**
95- * ntfs_find_vcn - find a vcn in the runlist described by an ntfs inode
96- * @ni: ntfs inode describing the runlist to search
97- * @vcn: vcn to find
98- * @need_write: if false, lock for reading and if true, lock for writing
83+ * ntfs_map_runlist - map (a part of) a runlist of an ntfs inode
84+ * @ni: ntfs inode for which to map (part of) a runlist
85+ * @vcn: map runlist part containing this vcn
86+ *
87+ * Map the part of a runlist containing the @vcn of the ntfs inode @ni.
88+ *
89+ * Return 0 on success and -errno on error.
90+ *
91+ * Locking: - The runlist must be unlocked on entry and is unlocked on return.
92+ * - This function takes the runlist lock for writing and modifies the
93+ * runlist.
94+ */
95+ int ntfs_map_runlist (ntfs_inode * ni , VCN vcn )
96+ {
97+ int err = 0 ;
98+
99+ down_write (& ni -> runlist .lock );
100+ /* Make sure someone else didn't do the work while we were sleeping. */
101+ if (likely (ntfs_rl_vcn_to_lcn (ni -> runlist .rl , vcn ) <=
102+ LCN_RL_NOT_MAPPED ))
103+ err = ntfs_map_runlist_nolock (ni , vcn );
104+ up_write (& ni -> runlist .lock );
105+ return err ;
106+ }
107+
108+ /**
109+ * ntfs_find_vcn_nolock - find a vcn in the runlist described by an ntfs inode
110+ * @ni: ntfs inode describing the runlist to search
111+ * @vcn: vcn to find
112+ * @write_locked: true if the runlist is locked for writing
99113 *
100114 * Find the virtual cluster number @vcn in the runlist described by the ntfs
101115 * inode @ni and return the address of the runlist element containing the @vcn.
102- * The runlist is left locked and the caller has to unlock it. If @need_write
103- * is true, the runlist is locked for writing and if @need_write is false, the
104- * runlist is locked for reading. In the error case, the runlist is not left
105- * locked.
116+ * The runlist is left locked and the caller has to unlock it. In the error
117+ * case, the runlist is left in the same locking state as on entry.
118+ *
119+ * Note if @write_locked is FALSE the lock may be dropped inside the function
120+ * so you cannot rely on the runlist still being the same when this function
121+ * returns.
106122 *
107123 * Note you need to distinguish between the lcn of the returned runlist element
108124 * being >= 0 and LCN_HOLE. In the later case you have to return zeroes on
@@ -124,28 +140,24 @@ int ntfs_map_runlist(ntfs_inode *ni, VCN vcn)
124140 * true, it is locked for writing. Otherwise is is locked for
125141 * reading.
126142 */
127- runlist_element * ntfs_find_vcn (ntfs_inode * ni , const VCN vcn ,
128- const BOOL need_write )
143+ runlist_element * ntfs_find_vcn_nolock (ntfs_inode * ni , const VCN vcn ,
144+ const BOOL write_locked )
129145{
130146 runlist_element * rl ;
131147 int err = 0 ;
132148 BOOL is_retry = FALSE;
133149
134- ntfs_debug ("Entering for i_ino 0x%lx, vcn 0x%llx, lock for %sing ." ,
150+ ntfs_debug ("Entering for i_ino 0x%lx, vcn 0x%llx, %s_locked ." ,
135151 ni -> mft_no , (unsigned long long )vcn ,
136- ! need_write ? "read " : "writ " );
152+ write_locked ? "write " : "read " );
137153 BUG_ON (!ni );
138154 BUG_ON (!NInoNonResident (ni ));
139155 BUG_ON (vcn < 0 );
140- lock_retry_remap :
141- if (!need_write )
142- down_read (& ni -> runlist .lock );
143- else
144- down_write (& ni -> runlist .lock );
156+ retry_remap :
145157 rl = ni -> runlist .rl ;
146158 if (likely (rl && vcn >= rl [0 ].vcn )) {
147159 while (likely (rl -> length )) {
148- if (likely (vcn < rl [1 ].vcn )) {
160+ if (unlikely (vcn < rl [1 ].vcn )) {
149161 if (likely (rl -> lcn >= LCN_HOLE )) {
150162 ntfs_debug ("Done." );
151163 return rl ;
@@ -161,19 +173,23 @@ runlist_element *ntfs_find_vcn(ntfs_inode *ni, const VCN vcn,
161173 err = - EIO ;
162174 }
163175 }
164- if (!need_write )
165- up_read (& ni -> runlist .lock );
166- else
167- up_write (& ni -> runlist .lock );
168176 if (!err && !is_retry ) {
169177 /*
170178 * The @vcn is in an unmapped region, map the runlist and
171179 * retry.
172180 */
173- err = ntfs_map_runlist (ni , vcn );
181+ if (!write_locked ) {
182+ up_read (& ni -> runlist .lock );
183+ down_write (& ni -> runlist .lock );
184+ }
185+ err = ntfs_map_runlist_nolock (ni , vcn );
186+ if (!write_locked ) {
187+ up_write (& ni -> runlist .lock );
188+ down_read (& ni -> runlist .lock );
189+ }
174190 if (likely (!err )) {
175191 is_retry = TRUE;
176- goto lock_retry_remap ;
192+ goto retry_remap ;
177193 }
178194 /*
179195 * -EINVAL and -ENOENT coming from a failed mapping attempt are
@@ -184,7 +200,8 @@ runlist_element *ntfs_find_vcn(ntfs_inode *ni, const VCN vcn,
184200 err = - EIO ;
185201 } else if (!err )
186202 err = - EIO ;
187- ntfs_error (ni -> vol -> sb , "Failed with error code %i." , err );
203+ if (err != - ENOENT )
204+ ntfs_error (ni -> vol -> sb , "Failed with error code %i." , err );
188205 return ERR_PTR (err );
189206}
190207
0 commit comments