This commit was manufactured by cvs2svn to create branch 'vserver'.
[linux-2.6.git] / fs / ntfs / lcnalloc.c
1 /*
2  * lcnalloc.c - Cluster (de)allocation code.  Part of the Linux-NTFS project.
3  *
4  * Copyright (c) 2004 Anton Altaparmakov
5  *
6  * This program/include file is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as published
8  * by the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program/include file is distributed in the hope that it will be
12  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program (in the main directory of the Linux-NTFS
18  * distribution in the file COPYING); if not, write to the Free Software
19  * Foundation,Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #ifdef NTFS_RW
23
24 #include <linux/pagemap.h>
25
26 #include "lcnalloc.h"
27 #include "debug.h"
28 #include "bitmap.h"
29 #include "inode.h"
30 #include "volume.h"
31 #include "attrib.h"
32 #include "malloc.h"
33 #include "ntfs.h"
34
35 /**
36  * ntfs_cluster_free_from_rl_nolock - free clusters from runlist
37  * @vol:        mounted ntfs volume on which to free the clusters
38  * @rl:         runlist describing the clusters to free
39  *
40  * Free all the clusters described by the runlist @rl on the volume @vol.  In
41  * the case of an error being returned, at least some of the clusters were not
42  * freed.
43  *
44  * Return 0 on success and -errno on error.
45  *
46  * Locking: - The volume lcn bitmap must be locked for writing on entry and is
47  *            left locked on return.
48  */
49 static int ntfs_cluster_free_from_rl_nolock(ntfs_volume *vol,
50                 const runlist_element *rl)
51 {
52         struct inode *lcnbmp_vi = vol->lcnbmp_ino;
53         int ret = 0;
54
55         ntfs_debug("Entering.");
56         for (; rl->length; rl++) {
57                 int err;
58
59                 if (rl->lcn < 0)
60                         continue;
61                 err = ntfs_bitmap_clear_run(lcnbmp_vi, rl->lcn, rl->length);
62                 if (unlikely(err && (!ret || ret == ENOMEM) && ret != err))
63                         ret = err;
64         }
65         ntfs_debug("Done.");
66         return ret;
67 }
68
69 /**
70  * ntfs_cluster_alloc - allocate clusters on an ntfs volume
71  * @vol:        mounted ntfs volume on which to allocate the clusters
72  * @start_vcn:  vcn to use for the first allocated cluster
73  * @count:      number of clusters to allocate
74  * @start_lcn:  starting lcn at which to allocate the clusters (or -1 if none)
75  * @zone:       zone from which to allocate the clusters
76  *
77  * Allocate @count clusters preferably starting at cluster @start_lcn or at the
78  * current allocator position if @start_lcn is -1, on the mounted ntfs volume
79  * @vol. @zone is either DATA_ZONE for allocation of normal clusters or
80  * MFT_ZONE for allocation of clusters for the master file table, i.e. the
81  * $MFT/$DATA attribute.
82  *
83  * @start_vcn specifies the vcn of the first allocated cluster.  This makes
84  * merging the resulting runlist with the old runlist easier.
85  *
86  * You need to check the return value with IS_ERR().  If this is false, the
87  * function was successful and the return value is a runlist describing the
88  * allocated cluster(s).  If IS_ERR() is true, the function failed and
89  * PTR_ERR() gives you the error code.
90  *
91  * Notes on the allocation algorithm
92  * =================================
93  *
94  * There are two data zones.  First is the area between the end of the mft zone
95  * and the end of the volume, and second is the area between the start of the
96  * volume and the start of the mft zone.  On unmodified/standard NTFS 1.x
97  * volumes, the second data zone does not exist due to the mft zone being
98  * expanded to cover the start of the volume in order to reserve space for the
99  * mft bitmap attribute.
100  *
101  * This is not the prettiest function but the complexity stems from the need of
102  * implementing the mft vs data zoned approach and from the fact that we have
103  * access to the lcn bitmap in portions of up to 8192 bytes at a time, so we
104  * need to cope with crossing over boundaries of two buffers.  Further, the
105  * fact that the allocator allows for caller supplied hints as to the location
106  * of where allocation should begin and the fact that the allocator keeps track
107  * of where in the data zones the next natural allocation should occur,
108  * contribute to the complexity of the function.  But it should all be
109  * worthwhile, because this allocator should: 1) be a full implementation of
110  * the MFT zone approach used by Windows NT, 2) cause reduction in
111  * fragmentation, and 3) be speedy in allocations (the code is not optimized
112  * for speed, but the algorithm is, so further speed improvements are probably
113  * possible).
114  *
115  * FIXME: We should be monitoring cluster allocation and increment the MFT zone
116  * size dynamically but this is something for the future.  We will just cause
117  * heavier fragmentation by not doing it and I am not even sure Windows would
118  * grow the MFT zone dynamically, so it might even be correct not to do this.
119  * The overhead in doing dynamic MFT zone expansion would be very large and
120  * unlikely worth the effort. (AIA)
121  *
122  * TODO: I have added in double the required zone position pointer wrap around
123  * logic which can be optimized to having only one of the two logic sets.
124  * However, having the double logic will work fine, but if we have only one of
125  * the sets and we get it wrong somewhere, then we get into trouble, so
126  * removing the duplicate logic requires _very_ careful consideration of _all_
127  * possible code paths.  So at least for now, I am leaving the double logic -
128  * better safe than sorry... (AIA)
129  *
130  * Locking: - The volume lcn bitmap must be unlocked on entry and is unlocked
131  *            on return.
132  *          - This function takes the volume lcn bitmap lock for writing and
133  *            modifies the bitmap contents.
134  */
135 runlist_element *ntfs_cluster_alloc(ntfs_volume *vol, const VCN start_vcn,
136                 const s64 count, const LCN start_lcn,
137                 const NTFS_CLUSTER_ALLOCATION_ZONES zone)
138 {
139         LCN zone_start, zone_end, bmp_pos, bmp_initial_pos, last_read_pos, lcn;
140         LCN prev_lcn = 0, prev_run_len = 0, mft_zone_size;
141         s64 clusters;
142         struct inode *lcnbmp_vi;
143         runlist_element *rl = NULL;
144         struct address_space *mapping;
145         struct page *page = NULL;
146         u8 *buf, *byte;
147         int err = 0, rlpos, rlsize, buf_size;
148         u8 pass, done_zones, search_zone, need_writeback = 0, bit;
149
150         ntfs_debug("Entering for start_vcn 0x%llx, count 0x%llx, start_lcn "
151                         "0x%llx, zone %s_ZONE.", (unsigned long long)start_vcn,
152                         (unsigned long long)count,
153                         (unsigned long long)start_lcn,
154                         zone == MFT_ZONE ? "MFT" : "DATA");
155         BUG_ON(!vol);
156         lcnbmp_vi = vol->lcnbmp_ino;
157         BUG_ON(!lcnbmp_vi);
158         BUG_ON(start_vcn < 0);
159         BUG_ON(count < 0);
160         BUG_ON(start_lcn < -1);
161         BUG_ON(zone < FIRST_ZONE);
162         BUG_ON(zone > LAST_ZONE);
163
164         /* Return empty runlist if @count == 0 */
165         // FIXME: Do we want to just return NULL instead? (AIA)
166         if (!count) {
167                 rl = ntfs_malloc_nofs(PAGE_SIZE);
168                 if (!rl)
169                         return ERR_PTR(-ENOMEM);
170                 rl[0].vcn = start_vcn;
171                 rl[0].lcn = LCN_RL_NOT_MAPPED;
172                 rl[0].length = 0;
173                 return rl;
174         }
175         /* Take the lcnbmp lock for writing. */
176         down_write(&vol->lcnbmp_lock);
177         /*
178          * If no specific @start_lcn was requested, use the current data zone
179          * position, otherwise use the requested @start_lcn but make sure it
180          * lies outside the mft zone.  Also set done_zones to 0 (no zones done)
181          * and pass depending on whether we are starting inside a zone (1) or
182          * at the beginning of a zone (2).  If requesting from the MFT_ZONE,
183          * we either start at the current position within the mft zone or at
184          * the specified position.  If the latter is out of bounds then we start
185          * at the beginning of the MFT_ZONE.
186          */
187         done_zones = 0;
188         pass = 1;
189         /*
190          * zone_start and zone_end are the current search range.  search_zone
191          * is 1 for mft zone, 2 for data zone 1 (end of mft zone till end of
192          * volume) and 4 for data zone 2 (start of volume till start of mft
193          * zone).
194          */
195         zone_start = start_lcn;
196         if (zone_start < 0) {
197                 if (zone == DATA_ZONE)
198                         zone_start = vol->data1_zone_pos;
199                 else
200                         zone_start = vol->mft_zone_pos;
201                 if (!zone_start) {
202                         /*
203                          * Zone starts at beginning of volume which means a
204                          * single pass is sufficient.
205                          */
206                         pass = 2;
207                 }
208         } else if (zone == DATA_ZONE && zone_start >= vol->mft_zone_start &&
209                         zone_start < vol->mft_zone_end) {
210                 zone_start = vol->mft_zone_end;
211                 /*
212                  * Starting at beginning of data1_zone which means a single
213                  * pass in this zone is sufficient.
214                  */
215                 pass = 2;
216         } else if (zone == MFT_ZONE && (zone_start < vol->mft_zone_start ||
217                         zone_start >= vol->mft_zone_end)) {
218                 zone_start = vol->mft_lcn;
219                 if (!vol->mft_zone_end)
220                         zone_start = 0;
221                 /*
222                  * Starting at beginning of volume which means a single pass
223                  * is sufficient.
224                  */
225                 pass = 2;
226         }
227         if (zone == MFT_ZONE) {
228                 zone_end = vol->mft_zone_end;
229                 search_zone = 1;
230         } else /* if (zone == DATA_ZONE) */ {
231                 /* Skip searching the mft zone. */
232                 done_zones |= 1;
233                 if (zone_start >= vol->mft_zone_end) {
234                         zone_end = vol->nr_clusters;
235                         search_zone = 2;
236                 } else {
237                         zone_end = vol->mft_zone_start;
238                         search_zone = 4;
239                 }
240         }
241         /*
242          * bmp_pos is the current bit position inside the bitmap.  We use
243          * bmp_initial_pos to determine whether or not to do a zone switch.
244          */
245         bmp_pos = bmp_initial_pos = zone_start;
246
247         /* Loop until all clusters are allocated, i.e. clusters == 0. */
248         clusters = count;
249         rlpos = rlsize = 0;
250         mapping = lcnbmp_vi->i_mapping;
251         while (1) {
252                 ntfs_debug("Start of outer while loop: done_zones 0x%x, "
253                                 "search_zone %i, pass %i, zone_start 0x%llx, "
254                                 "zone_end 0x%llx, bmp_initial_pos 0x%llx, "
255                                 "bmp_pos 0x%llx, rlpos %i, rlsize %i.",
256                                 done_zones, search_zone, pass,
257                                 (unsigned long long)zone_start,
258                                 (unsigned long long)zone_end,
259                                 (unsigned long long)bmp_initial_pos,
260                                 (unsigned long long)bmp_pos, rlpos, rlsize);
261                 /* Loop until we run out of free clusters. */
262                 last_read_pos = bmp_pos >> 3;
263                 ntfs_debug("last_read_pos 0x%llx.",
264                                 (unsigned long long)last_read_pos);
265                 if (last_read_pos > lcnbmp_vi->i_size) {
266                         ntfs_debug("End of attribute reached.  "
267                                         "Skipping to zone_pass_done.");
268                         goto zone_pass_done;
269                 }
270                 if (likely(page)) {
271                         if (need_writeback) {
272                                 ntfs_debug("Marking page dirty.");
273                                 flush_dcache_page(page);
274                                 set_page_dirty(page);
275                                 need_writeback = 0;
276                         }
277                         ntfs_unmap_page(page);
278                 }
279                 page = ntfs_map_page(mapping, last_read_pos >>
280                                 PAGE_CACHE_SHIFT);
281                 if (IS_ERR(page)) {
282                         err = PTR_ERR(page);
283                         ntfs_error(vol->sb, "Failed to map page.");
284                         goto out;
285                 }
286                 buf_size = last_read_pos & ~PAGE_CACHE_MASK;
287                 buf = page_address(page) + buf_size;
288                 buf_size = PAGE_CACHE_SIZE - buf_size;
289                 if (unlikely(last_read_pos + buf_size > lcnbmp_vi->i_size))
290                         buf_size = lcnbmp_vi->i_size - last_read_pos;
291                 buf_size <<= 3;
292                 lcn = bmp_pos & 7;
293                 bmp_pos &= ~7;
294                 ntfs_debug("Before inner while loop: buf_size %i, lcn 0x%llx, "
295                                 "bmp_pos 0x%llx, need_writeback %i.", buf_size,
296                                 (unsigned long long)lcn,
297                                 (unsigned long long)bmp_pos, need_writeback);
298                 while (lcn < buf_size && lcn + bmp_pos < zone_end) {
299                         byte = buf + (lcn >> 3);
300                         ntfs_debug("In inner while loop: buf_size %i, "
301                                         "lcn 0x%llx, bmp_pos 0x%llx, "
302                                         "need_writeback %i, byte ofs 0x%x, "
303                                         "*byte 0x%x.", buf_size,
304                                         (unsigned long long)lcn,
305                                         (unsigned long long)bmp_pos,
306                                         need_writeback,
307                                         (unsigned int)(lcn >> 3),
308                                         (unsigned int)*byte);
309                         /* Skip full bytes. */
310                         if (*byte == 0xff) {
311                                 lcn = (lcn + 8) & ~7;
312                                 ntfs_debug("Continuing while loop 1.");
313                                 continue;
314                         }
315                         bit = 1 << (lcn & 7);
316                         ntfs_debug("bit %i.", bit);
317                         /* If the bit is already set, go onto the next one. */
318                         if (*byte & bit) {
319                                 lcn++;
320                                 ntfs_debug("Continuing while loop 2.");
321                                 continue;
322                         }
323                         /*
324                          * Allocate more memory if needed, including space for
325                          * the terminator element.
326                          * ntfs_malloc_nofs() operates on whole pages only.
327                          */
328                         if ((rlpos + 2) * sizeof(*rl) > rlsize) {
329                                 runlist_element *rl2;
330
331                                 ntfs_debug("Reallocating memory.");
332                                 if (!rl)
333                                         ntfs_debug("First free bit is at LCN "
334                                                         "0x%llx.",
335                                                         (unsigned long long)
336                                                         (lcn + bmp_pos));
337                                 rl2 = ntfs_malloc_nofs(rlsize + (int)PAGE_SIZE);
338                                 if (unlikely(!rl2)) {
339                                         err = -ENOMEM;
340                                         ntfs_error(vol->sb, "Failed to "
341                                                         "allocate memory.");
342                                         goto out;
343                                 }
344                                 memcpy(rl2, rl, rlsize);
345                                 ntfs_free(rl);
346                                 rl = rl2;
347                                 rlsize += PAGE_SIZE;
348                                 ntfs_debug("Reallocated memory, rlsize 0x%x.",
349                                                 rlsize);
350                         }
351                         /* Allocate the bitmap bit. */
352                         *byte |= bit;
353                         /* We need to write this bitmap page to disk. */
354                         need_writeback = 1;
355                         ntfs_debug("*byte 0x%x, need_writeback is set.",
356                                         (unsigned int)*byte);
357                         /*
358                          * Coalesce with previous run if adjacent LCNs.
359                          * Otherwise, append a new run.
360                          */
361                         ntfs_debug("Adding run (lcn 0x%llx, len 0x%llx), "
362                                         "prev_lcn 0x%llx, lcn 0x%llx, "
363                                         "bmp_pos 0x%llx, prev_run_len 0x%llx, "
364                                         "rlpos %i.",
365                                         (unsigned long long)(lcn + bmp_pos),
366                                         1ULL, (unsigned long long)prev_lcn,
367                                         (unsigned long long)lcn,
368                                         (unsigned long long)bmp_pos,
369                                         (unsigned long long)prev_run_len,
370                                         rlpos);
371                         if (prev_lcn == lcn + bmp_pos - prev_run_len && rlpos) {
372                                 ntfs_debug("Coalescing to run (lcn 0x%llx, "
373                                                 "len 0x%llx).",
374                                                 (unsigned long long)
375                                                 rl[rlpos - 1].lcn,
376                                                 (unsigned long long)
377                                                 rl[rlpos - 1].length);
378                                 rl[rlpos - 1].length = ++prev_run_len;
379                                 ntfs_debug("Run now (lcn 0x%llx, len 0x%llx), "
380                                                 "prev_run_len 0x%llx.",
381                                                 (unsigned long long)
382                                                 rl[rlpos - 1].lcn,
383                                                 (unsigned long long)
384                                                 rl[rlpos - 1].length,
385                                                 (unsigned long long)
386                                                 prev_run_len);
387                         } else {
388                                 if (likely(rlpos)) {
389                                         ntfs_debug("Adding new run, (previous "
390                                                         "run lcn 0x%llx, "
391                                                         "len 0x%llx).",
392                                                         (unsigned long long)
393                                                         rl[rlpos - 1].lcn,
394                                                         (unsigned long long)
395                                                         rl[rlpos - 1].length);
396                                         rl[rlpos].vcn = rl[rlpos - 1].vcn +
397                                                         prev_run_len;
398                                 } else {
399                                         ntfs_debug("Adding new run, is first "
400                                                         "run.");
401                                         rl[rlpos].vcn = start_vcn;
402                                 }
403                                 rl[rlpos].lcn = prev_lcn = lcn + bmp_pos;
404                                 rl[rlpos].length = prev_run_len = 1;
405                                 rlpos++;
406                         }
407                         /* Done? */
408                         if (!--clusters) {
409                                 LCN tc;
410                                 /*
411                                  * Update the current zone position.  Positions
412                                  * of already scanned zones have been updated
413                                  * during the respective zone switches.
414                                  */
415                                 tc = lcn + bmp_pos + 1;
416                                 ntfs_debug("Done. Updating current zone "
417                                                 "position, tc 0x%llx, "
418                                                 "search_zone %i.",
419                                                 (unsigned long long)tc,
420                                                 search_zone);
421                                 switch (search_zone) {
422                                 case 1:
423                                         ntfs_debug("Before checks, "
424                                                         "vol->mft_zone_pos "
425                                                         "0x%llx.",
426                                                         (unsigned long long)
427                                                         vol->mft_zone_pos);
428                                         if (tc >= vol->mft_zone_end) {
429                                                 vol->mft_zone_pos =
430                                                                 vol->mft_lcn;
431                                                 if (!vol->mft_zone_end)
432                                                         vol->mft_zone_pos = 0;
433                                         } else if ((bmp_initial_pos >=
434                                                         vol->mft_zone_pos ||
435                                                         tc > vol->mft_zone_pos)
436                                                         && tc >= vol->mft_lcn)
437                                                 vol->mft_zone_pos = tc;
438                                         ntfs_debug("After checks, "
439                                                         "vol->mft_zone_pos "
440                                                         "0x%llx.",
441                                                         (unsigned long long)
442                                                         vol->mft_zone_pos);
443                                         break;
444                                 case 2:
445                                         ntfs_debug("Before checks, "
446                                                         "vol->data1_zone_pos "
447                                                         "0x%llx.",
448                                                         (unsigned long long)
449                                                         vol->data1_zone_pos);
450                                         if (tc >= vol->nr_clusters)
451                                                 vol->data1_zone_pos =
452                                                              vol->mft_zone_end;
453                                         else if ((bmp_initial_pos >=
454                                                     vol->data1_zone_pos ||
455                                                     tc > vol->data1_zone_pos)
456                                                     && tc >= vol->mft_zone_end)
457                                                 vol->data1_zone_pos = tc;
458                                         ntfs_debug("After checks, "
459                                                         "vol->data1_zone_pos "
460                                                         "0x%llx.",
461                                                         (unsigned long long)
462                                                         vol->data1_zone_pos);
463                                         break;
464                                 case 4:
465                                         ntfs_debug("Before checks, "
466                                                         "vol->data2_zone_pos "
467                                                         "0x%llx.",
468                                                         (unsigned long long)
469                                                         vol->data2_zone_pos);
470                                         if (tc >= vol->mft_zone_start)
471                                                 vol->data2_zone_pos = 0;
472                                         else if (bmp_initial_pos >=
473                                                       vol->data2_zone_pos ||
474                                                       tc > vol->data2_zone_pos)
475                                                 vol->data2_zone_pos = tc;
476                                         ntfs_debug("After checks, "
477                                                         "vol->data2_zone_pos "
478                                                         "0x%llx.",
479                                                         (unsigned long long)
480                                                         vol->data2_zone_pos);
481                                         break;
482                                 default:
483                                         BUG();
484                                 }
485                                 ntfs_debug("Finished.  Going to out.");
486                                 goto out;
487                         }
488                         lcn++;
489                 }
490                 bmp_pos += buf_size;
491                 ntfs_debug("After inner while loop: buf_size 0x%x, lcn "
492                                 "0x%llx, bmp_pos 0x%llx, need_writeback %i.",
493                                 buf_size, (unsigned long long)lcn,
494                                 (unsigned long long)bmp_pos, need_writeback);
495                 if (bmp_pos < zone_end) {
496                         ntfs_debug("Continuing outer while loop, "
497                                         "bmp_pos 0x%llx, zone_end 0x%llx.",
498                                         (unsigned long long)bmp_pos,
499                                         (unsigned long long)zone_end);
500                         continue;
501                 }
502 zone_pass_done: /* Finished with the current zone pass. */
503                 ntfs_debug("At zone_pass_done, pass %i.", pass);
504                 if (pass == 1) {
505                         /*
506                          * Now do pass 2, scanning the first part of the zone
507                          * we omitted in pass 1.
508                          */
509                         pass = 2;
510                         zone_end = zone_start;
511                         switch (search_zone) {
512                         case 1: /* mft_zone */
513                                 zone_start = vol->mft_zone_start;
514                                 break;
515                         case 2: /* data1_zone */
516                                 zone_start = vol->mft_zone_end;
517                                 break;
518                         case 4: /* data2_zone */
519                                 zone_start = 0;
520                                 break;
521                         default:
522                                 BUG();
523                         }
524                         /* Sanity check. */
525                         if (zone_end < zone_start)
526                                 zone_end = zone_start;
527                         bmp_pos = zone_start;
528                         ntfs_debug("Continuing outer while loop, pass 2, "
529                                         "zone_start 0x%llx, zone_end 0x%llx, "
530                                         "bmp_pos 0x%llx.",
531                                         (unsigned long long)zone_start,
532                                         (unsigned long long)zone_end,
533                                         (unsigned long long)bmp_pos);
534                         continue;
535                 } /* pass == 2 */
536 done_zones_check:
537                 ntfs_debug("At done_zones_check, search_zone %i, done_zones "
538                                 "before 0x%x, done_zones after 0x%x.",
539                                 search_zone, done_zones,
540                                 done_zones | search_zone);
541                 done_zones |= search_zone;
542                 if (done_zones < 7) {
543                         ntfs_debug("Switching zone.");
544                         /* Now switch to the next zone we haven't done yet. */
545                         pass = 1;
546                         switch (search_zone) {
547                         case 1:
548                                 ntfs_debug("Switching from mft zone to data1 "
549                                                 "zone.");
550                                 /* Update mft zone position. */
551                                 if (rlpos) {
552                                         LCN tc;
553
554                                         ntfs_debug("Before checks, "
555                                                         "vol->mft_zone_pos "
556                                                         "0x%llx.",
557                                                         (unsigned long long)
558                                                         vol->mft_zone_pos);
559                                         tc = rl[rlpos - 1].lcn +
560                                                         rl[rlpos - 1].length;
561                                         if (tc >= vol->mft_zone_end) {
562                                                 vol->mft_zone_pos =
563                                                                 vol->mft_lcn;
564                                                 if (!vol->mft_zone_end)
565                                                         vol->mft_zone_pos = 0;
566                                         } else if ((bmp_initial_pos >=
567                                                         vol->mft_zone_pos ||
568                                                         tc > vol->mft_zone_pos)
569                                                         && tc >= vol->mft_lcn)
570                                                 vol->mft_zone_pos = tc;
571                                         ntfs_debug("After checks, "
572                                                         "vol->mft_zone_pos "
573                                                         "0x%llx.",
574                                                         (unsigned long long)
575                                                         vol->mft_zone_pos);
576                                 }
577                                 /* Switch from mft zone to data1 zone. */
578 switch_to_data1_zone:           search_zone = 2;
579                                 zone_start = bmp_initial_pos =
580                                                 vol->data1_zone_pos;
581                                 zone_end = vol->nr_clusters;
582                                 if (zone_start == vol->mft_zone_end)
583                                         pass = 2;
584                                 if (zone_start >= zone_end) {
585                                         vol->data1_zone_pos = zone_start =
586                                                         vol->mft_zone_end;
587                                         pass = 2;
588                                 }
589                                 break;
590                         case 2:
591                                 ntfs_debug("Switching from data1 zone to "
592                                                 "data2 zone.");
593                                 /* Update data1 zone position. */
594                                 if (rlpos) {
595                                         LCN tc;
596
597                                         ntfs_debug("Before checks, "
598                                                         "vol->data1_zone_pos "
599                                                         "0x%llx.",
600                                                         (unsigned long long)
601                                                         vol->data1_zone_pos);
602                                         tc = rl[rlpos - 1].lcn +
603                                                         rl[rlpos - 1].length;
604                                         if (tc >= vol->nr_clusters)
605                                                 vol->data1_zone_pos =
606                                                              vol->mft_zone_end;
607                                         else if ((bmp_initial_pos >=
608                                                     vol->data1_zone_pos ||
609                                                     tc > vol->data1_zone_pos)
610                                                     && tc >= vol->mft_zone_end)
611                                                 vol->data1_zone_pos = tc;
612                                         ntfs_debug("After checks, "
613                                                         "vol->data1_zone_pos "
614                                                         "0x%llx.",
615                                                         (unsigned long long)
616                                                         vol->data1_zone_pos);
617                                 }
618                                 /* Switch from data1 zone to data2 zone. */
619                                 search_zone = 4;
620                                 zone_start = bmp_initial_pos =
621                                                 vol->data2_zone_pos;
622                                 zone_end = vol->mft_zone_start;
623                                 if (!zone_start)
624                                         pass = 2;
625                                 if (zone_start >= zone_end) {
626                                         vol->data2_zone_pos = zone_start =
627                                                         bmp_initial_pos = 0;
628                                         pass = 2;
629                                 }
630                                 break;
631                         case 4:
632                                 ntfs_debug("Switching from data2 zone to "
633                                                 "data1 zone.");
634                                 /* Update data2 zone position. */
635                                 if (rlpos) {
636                                         LCN tc;
637
638                                         ntfs_debug("Before checks, "
639                                                         "vol->data2_zone_pos "
640                                                         "0x%llx.",
641                                                         (unsigned long long)
642                                                         vol->data2_zone_pos);
643                                         tc = rl[rlpos - 1].lcn +
644                                                         rl[rlpos - 1].length;
645                                         if (tc >= vol->mft_zone_start)
646                                                 vol->data2_zone_pos = 0;
647                                         else if (bmp_initial_pos >=
648                                                       vol->data2_zone_pos ||
649                                                       tc > vol->data2_zone_pos)
650                                                 vol->data2_zone_pos = tc;
651                                         ntfs_debug("After checks, "
652                                                         "vol->data2_zone_pos "
653                                                         "0x%llx.",
654                                                         (unsigned long long)
655                                                         vol->data2_zone_pos);
656                                 }
657                                 /* Switch from data2 zone to data1 zone. */
658                                 goto switch_to_data1_zone;
659                         default:
660                                 BUG();
661                         }
662                         ntfs_debug("After zone switch, search_zone %i, "
663                                         "pass %i, bmp_initial_pos 0x%llx, "
664                                         "zone_start 0x%llx, zone_end 0x%llx.",
665                                         search_zone, pass,
666                                         (unsigned long long)bmp_initial_pos,
667                                         (unsigned long long)zone_start,
668                                         (unsigned long long)zone_end);
669                         bmp_pos = zone_start;
670                         if (zone_start == zone_end) {
671                                 ntfs_debug("Empty zone, going to "
672                                                 "done_zones_check.");
673                                 /* Empty zone. Don't bother searching it. */
674                                 goto done_zones_check;
675                         }
676                         ntfs_debug("Continuing outer while loop.");
677                         continue;
678                 } /* done_zones == 7 */
679                 ntfs_debug("All zones are finished.");
680                 /*
681                  * All zones are finished!  If DATA_ZONE, shrink mft zone.  If
682                  * MFT_ZONE, we have really run out of space.
683                  */
684                 mft_zone_size = vol->mft_zone_end - vol->mft_zone_start;
685                 ntfs_debug("vol->mft_zone_start 0x%llx, vol->mft_zone_end "
686                                 "0x%llx, mft_zone_size 0x%llx.",
687                                 (unsigned long long)vol->mft_zone_start,
688                                 (unsigned long long)vol->mft_zone_end,
689                                 (unsigned long long)mft_zone_size);
690                 if (zone == MFT_ZONE || mft_zone_size <= 0) {
691                         ntfs_debug("No free clusters left, going to out.");
692                         /* Really no more space left on device. */
693                         err = ENOSPC;
694                         goto out;
695                 } /* zone == DATA_ZONE && mft_zone_size > 0 */
696                 ntfs_debug("Shrinking mft zone.");
697                 zone_end = vol->mft_zone_end;
698                 mft_zone_size >>= 1;
699                 if (mft_zone_size > 0)
700                         vol->mft_zone_end = vol->mft_zone_start + mft_zone_size;
701                 else /* mft zone and data2 zone no longer exist. */
702                         vol->data2_zone_pos = vol->mft_zone_start =
703                                         vol->mft_zone_end = 0;
704                 if (vol->mft_zone_pos >= vol->mft_zone_end) {
705                         vol->mft_zone_pos = vol->mft_lcn;
706                         if (!vol->mft_zone_end)
707                                 vol->mft_zone_pos = 0;
708                 }
709                 bmp_pos = zone_start = bmp_initial_pos =
710                                 vol->data1_zone_pos = vol->mft_zone_end;
711                 search_zone = 2;
712                 pass = 2;
713                 done_zones &= ~2;
714                 ntfs_debug("After shrinking mft zone, mft_zone_size 0x%llx, "
715                                 "vol->mft_zone_start 0x%llx, "
716                                 "vol->mft_zone_end 0x%llx, "
717                                 "vol->mft_zone_pos 0x%llx, search_zone 2, "
718                                 "pass 2, dones_zones 0x%x, zone_start 0x%llx, "
719                                 "zone_end 0x%llx, vol->data1_zone_pos 0x%llx, "
720                                 "continuing outer while loop.",
721                                 (unsigned long long)mft_zone_size,
722                                 (unsigned long long)vol->mft_zone_start,
723                                 (unsigned long long)vol->mft_zone_end,
724                                 (unsigned long long)vol->mft_zone_pos,
725                                 done_zones, (unsigned long long)zone_start,
726                                 (unsigned long long)zone_end,
727                                 (unsigned long long)vol->data1_zone_pos);
728         }
729         ntfs_debug("After outer while loop.");
730 out:
731         ntfs_debug("At out.");
732         /* Add runlist terminator element. */
733         if (likely(rl)) {
734                 rl[rlpos].vcn = rl[rlpos - 1].vcn + rl[rlpos - 1].length;
735                 rl[rlpos].lcn = LCN_RL_NOT_MAPPED;
736                 rl[rlpos].length = 0;
737         }
738         if (likely(page && !IS_ERR(page))) {
739                 if (need_writeback) {
740                         ntfs_debug("Marking page dirty.");
741                         flush_dcache_page(page);
742                         set_page_dirty(page);
743                         need_writeback = 0;
744                 }
745                 ntfs_unmap_page(page);
746         }
747         if (likely(!err)) {
748                 up_write(&vol->lcnbmp_lock);
749                 ntfs_debug("Done.");
750                 return rl;
751         }
752         ntfs_error(vol->sb, "Failed to allocate clusters, aborting "
753                         "(error %i).", err);
754         if (rl) {
755                 int err2;
756
757                 if (err == ENOSPC)
758                         ntfs_debug("Not enough space to complete allocation, "
759                                         "err ENOSPC, first free lcn 0x%llx, "
760                                         "could allocate up to 0x%llx "
761                                         "clusters.",
762                                         (unsigned long long)rl[0].lcn,
763                                         (unsigned long long)count - clusters);
764                 /* Deallocate all allocated clusters. */
765                 ntfs_debug("Attempting rollback...");
766                 err2 = ntfs_cluster_free_from_rl_nolock(vol, rl);
767                 if (err2) {
768                         ntfs_error(vol->sb, "Failed to rollback (error %i).  "
769                                         "Leaving inconsistent metadata!  "
770                                         "Unmount and run chkdsk.", err2);
771                         NVolSetErrors(vol);
772                 }
773                 /* Free the runlist. */
774                 ntfs_free(rl);
775         } else if (err == ENOSPC)
776                 ntfs_debug("No space left at all, err = ENOSPC, "
777                                 "first free lcn = 0x%llx.",
778                                 (unsigned long long)vol->data1_zone_pos);
779         up_write(&vol->lcnbmp_lock);
780         return ERR_PTR(err);
781 }
782
783 /**
784  * __ntfs_cluster_free - free clusters on an ntfs volume
785  * @vi:         vfs inode whose runlist describes the clusters to free
786  * @start_vcn:  vcn in the runlist of @vi at which to start freeing clusters
787  * @count:      number of clusters to free or -1 for all clusters
788  * @is_rollback:        if TRUE this is a rollback operation
789  *
790  * Free @count clusters starting at the cluster @start_vcn in the runlist
791  * described by the vfs inode @vi.
792  *
793  * If @count is -1, all clusters from @start_vcn to the end of the runlist are
794  * deallocated.  Thus, to completely free all clusters in a runlist, use
795  * @start_vcn = 0 and @count = -1.
796  *
797  * @is_rollback should always be FALSE, it is for internal use to rollback
798  * errors.  You probably want to use ntfs_cluster_free() instead.
799  *
800  * Note, ntfs_cluster_free() does not modify the runlist at all, so the caller
801  * has to deal with it later.
802  *
803  * Return the number of deallocated clusters (not counting sparse ones) on
804  * success and -errno on error.
805  *
806  * Locking: - The runlist described by @vi must be unlocked on entry and is
807  *            unlocked on return.
808  *          - This function takes the runlist lock of @vi for reading and
809  *            sometimes for writing and sometimes modifies the runlist.
810  *          - The volume lcn bitmap must be unlocked on entry and is unlocked
811  *            on return.
812  *          - This function takes the volume lcn bitmap lock for writing and
813  *            modifies the bitmap contents.
814  */
815 s64 __ntfs_cluster_free(struct inode *vi, const VCN start_vcn, s64 count,
816                 const BOOL is_rollback)
817 {
818         s64 delta, to_free, total_freed, real_freed;
819         ntfs_inode *ni;
820         ntfs_volume *vol;
821         struct inode *lcnbmp_vi;
822         runlist_element *rl;
823         int err;
824
825         BUG_ON(!vi);
826         ntfs_debug("Entering for i_ino 0x%lx, start_vcn 0x%llx, count "
827                         "0x%llx.%s", vi->i_ino, (unsigned long long)start_vcn,
828                         (unsigned long long)count,
829                         is_rollback ? " (rollback)" : "");
830         ni = NTFS_I(vi);
831         vol = ni->vol;
832         lcnbmp_vi = vol->lcnbmp_ino;
833         BUG_ON(!lcnbmp_vi);
834         BUG_ON(start_vcn < 0);
835         BUG_ON(count < -1);
836         /*
837          * Lock the lcn bitmap for writing but only if not rolling back.  We
838          * must hold the lock all the way including through rollback otherwise
839          * rollback is not possible because once we have cleared a bit and
840          * dropped the lock, anyone could have set the bit again, thus
841          * allocating the cluster for another use.
842          */
843         if (likely(!is_rollback))
844                 down_write(&vol->lcnbmp_lock);
845
846         total_freed = real_freed = 0;
847
848         /* This returns with ni->runlist locked for reading on success. */
849         rl = ntfs_find_vcn(ni, start_vcn, FALSE);
850         if (IS_ERR(rl)) {
851                 if (!is_rollback)
852                         ntfs_error(vol->sb, "Failed to find first runlist "
853                                         "element (error %li), aborting.",
854                                         PTR_ERR(rl));
855                 err = PTR_ERR(rl);
856                 goto err_out;
857         }
858         if (unlikely(rl->lcn < (LCN)LCN_HOLE)) {
859                 if (!is_rollback)
860                         ntfs_error(vol->sb, "First runlist element has "
861                                         "invalid lcn, aborting.");
862                 err = -EIO;
863                 goto unl_err_out;
864         }
865         /* Find the starting cluster inside the run that needs freeing. */
866         delta = start_vcn - rl->vcn;
867
868         /* The number of clusters in this run that need freeing. */
869         to_free = rl->length - delta;
870         if (count >= 0 && to_free > count)
871                 to_free = count;
872
873         if (likely(rl->lcn >= 0)) {
874                 /* Do the actual freeing of the clusters in this run. */
875                 err = ntfs_bitmap_set_bits_in_run(lcnbmp_vi, rl->lcn + delta,
876                                 to_free, likely(!is_rollback) ? 0 : 1);
877                 if (unlikely(err)) {
878                         if (!is_rollback)
879                                 ntfs_error(vol->sb, "Failed to clear first run "
880                                                 "(error %i), aborting.", err);
881                         goto unl_err_out;
882                 }
883                 /* We have freed @to_free real clusters. */
884                 real_freed = to_free;
885         };
886         /* Go to the next run and adjust the number of clusters left to free. */
887         ++rl;
888         if (count >= 0)
889                 count -= to_free;
890
891         /* Keep track of the total "freed" clusters, including sparse ones. */
892         total_freed = to_free;
893         /*
894          * Loop over the remaining runs, using @count as a capping value, and
895          * free them.
896          */
897         for (; rl->length && count != 0; ++rl) {
898                 if (unlikely(rl->lcn < (LCN)LCN_HOLE)) {
899                         VCN vcn;
900
901                         /*
902                          * Attempt to map runlist, dropping runlist lock for
903                          * the duration.
904                          */
905                         up_read(&ni->runlist.lock);
906                         vcn = rl->vcn;
907                         err = ntfs_map_runlist(ni, vcn);
908                         if (err) {
909                                 if (!is_rollback)
910                                         ntfs_error(vol->sb, "Failed to map "
911                                                         "runlist fragment.");
912                                 if (err == -EINVAL || err == -ENOENT)
913                                         err = -EIO;
914                                 goto err_out;
915                         }
916                         /*
917                          * This returns with ni->runlist locked for reading on
918                          * success.
919                          */
920                         rl = ntfs_find_vcn(ni, vcn, FALSE);
921                         if (IS_ERR(rl)) {
922                                 err = PTR_ERR(rl);
923                                 if (!is_rollback)
924                                         ntfs_error(vol->sb, "Failed to find "
925                                                         "subsequent runlist "
926                                                         "element.");
927                                 goto err_out;
928                         }
929                         if (unlikely(rl->lcn < (LCN)LCN_HOLE)) {
930                                 if (!is_rollback)
931                                         ntfs_error(vol->sb, "Runlist element "
932                                                         "has invalid lcn "
933                                                         "(0x%llx).",
934                                                         (unsigned long long)
935                                                         rl->lcn);
936                                 err = -EIO;
937                                 goto unl_err_out;
938                         }
939                 }
940                 /* The number of clusters in this run that need freeing. */
941                 to_free = rl->length;
942                 if (count >= 0 && to_free > count)
943                         to_free = count;
944
945                 if (likely(rl->lcn >= 0)) {
946                         /* Do the actual freeing of the clusters in the run. */
947                         err = ntfs_bitmap_set_bits_in_run(lcnbmp_vi, rl->lcn,
948                                         to_free, likely(!is_rollback) ? 0 : 1);
949                         if (unlikely(err)) {
950                                 if (!is_rollback)
951                                         ntfs_error(vol->sb, "Failed to clear "
952                                                         "subsequent run.");
953                                 goto unl_err_out;
954                         }
955                         /* We have freed @to_free real clusters. */
956                         real_freed += to_free;
957                 }
958                 /* Adjust the number of clusters left to free. */
959                 if (count >= 0)
960                         count -= to_free;
961         
962                 /* Update the total done clusters. */
963                 total_freed += to_free;
964         }
965         up_read(&ni->runlist.lock);
966         if (likely(!is_rollback))
967                 up_write(&vol->lcnbmp_lock);
968
969         BUG_ON(count > 0);
970
971         /* We are done.  Return the number of actually freed clusters. */
972         ntfs_debug("Done.");
973         return real_freed;
974 unl_err_out:
975         up_read(&ni->runlist.lock);
976 err_out:
977         if (is_rollback)
978                 return err;
979         /* If no real clusters were freed, no need to rollback. */
980         if (!real_freed) {
981                 up_write(&vol->lcnbmp_lock);
982                 return err;
983         }
984         /*
985          * Attempt to rollback and if that succeeds just return the error code.
986          * If rollback fails, set the volume errors flag, emit an error
987          * message, and return the error code.
988          */
989         delta = __ntfs_cluster_free(vi, start_vcn, total_freed, TRUE);
990         if (delta < 0) {
991                 ntfs_error(vol->sb, "Failed to rollback (error %i).  Leaving "
992                                 "inconsistent metadata!  Unmount and run "
993                                 "chkdsk.", (int)delta);
994                 NVolSetErrors(vol);
995         }
996         up_write(&vol->lcnbmp_lock);
997         ntfs_error(vol->sb, "Aborting (error %i).", err);
998         return err;
999 }
1000
1001 #endif /* NTFS_RW */