summaryrefslogtreecommitdiff
path: root/ufs/sizes.c
blob: a349eb40e09d1f51d4f16a502ef0348a3c1b1b8e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
/* File growth and truncation
   Copyright (C) 1993, 1994 Free Software Foundation

This file is part of the GNU Hurd.

The GNU Hurd is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

The GNU Hurd is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with the GNU Hurd; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* Written by Michael I. Bushnell.  */

#include "ufs.h"
#include "fs.h"
#include "dinode.h"
#include <string.h>

#ifdef DONT_CACHE_MEMORY_OBJECTS
#define MAY_CACHE 0
#else
#define MAY_CACHE 1
#endif


/* Implement the diskfs_truncate callback; sse <hurd/diskfs.h> for the
   interface description. */
error_t
diskfs_truncate (struct node *np,
		 off_t length)
{
  int offset;
  daddr_t lastiblock[NIADDR], lastblock, bn;
  struct dinode *di = dino (np->dn->number);
  int blocksfreed = 0;
  
  if (length >= np->dn_stat.st_size)
    return 0;

  assert (!diskfs_readonly);

  /* First check to see if this is a kludged symlink; if so
     this is special. */
  if (direct_symlink_extension && S_ISLNK (np->dn_stat.st_mode)
      && np->dn_stat.st_size < sblock->fs_maxsymlinklen)
    {
      error_t err;

      if (err = diskfs_catch_exception ())
	return err;
      bzero (di->di_shortlink + length, np->dn_stat.st_size - length);
      diskfs_end_catch_exception ();
      np->dn_stat.st_size = length;
      np->dn_set_ctime = np->dn_set_mtime = 1;
      return 0;
    }
  
  /* If the file is not being trucated to a block boundary,
     the zero the partial bit in the new last block. */
  offset = blkoff (sblock, length);
  if (offset)
    {
      int bsize;			/* size of new last block */
      int savesize = np->allocsize;
      
      np->allocsize = length;	/* temporary */
      bsize = blksize (sblock, np, lbkno (sblock, length));
      np->allocsize = savesize;
      diskfs_node_rdwr (np, zeroblock, length, bsize - offset, 1, 0, 0);
      diskfs_file_update (np, 1);
    }

  rwlock_writer_lock (&np->allocptrlock);

  /* Now flush all the data past the new size from the kernel.
     Also force any delayed copies of this data to take place
     immediately.  (We are changing the data implicitly to zeros
     and doing it without the kernels immediate knowledge;
     this forces us to help out the kernel thusly.) */
  if (np->dn->fileinfo)
    {
      pager_change_attributes (np->dn->fileinfo->p, MAY_CACHE,
			       MEMORY_OBJECT_COPY_NONE, 1);
      obj = diskfs_get_filemap (np);
      mach_port_insert_right (mach_task_self (), obj, obj,
			      MACH_MSG_TYPE_MAKE_SEND);
      poke_pages (obj, round_page (length), round_page (np->allocsize));
      mach_port_deallocate (mach_task_self (), obj);
      pager_flush_some (np->fileinfo->p, round_page (length),
			np->allocsize - length, 1);
    }

  /* Calculate index into node's block list of direct
     and indirect blocks which we want to keep.  Lastblock
     is -1 when the file is truncated to 0. */
  lastblock = lblkno (sblock, length + sblock->fs_bsize - 1) - 1;
  lastiblock[INDIR_SINGLE] = lastblock - NDADDR;
  lastiblock[INDIR_DOUBLE] = lastiblock[INDIR_SINGLE] - NINDIR (sblock);
  lastiblock[INDIR_TRIPLE] = (lastiblock[INDIR_DOUBLE]
			      - NINDIR (sblock) * NINDIR (sblock));
  
  /* Normalize to -1 indicating that this block will not be needed. */
  for (level = INDIR_TRIPLE; level >= INDIR_SINGLE; level--)
    if (lastiblock[level] < 0)
      lastiblock[level] = -1;

  /* Update the size on disk; fsck will finish freeing blocks if necessary
     should we crash. */
  np->dn_stat.st_size = length;
  np->dn_set_mtime = 1;
  np->dn_set_ctime = 1;
  diskfs_node_update (np, 1);

  /* Free the blocks. */
  
  err = diskfs_catch_exception ();
  if (err)
    {
      rwlock_writer_unlock (&np->allocptrlock);
      return err;
    }

  /* Indirect blocks first. */
  for (level = INDIR_TRIPLE; level >= INDIR_SINGLE; level--)
    if (lastiblock[level] == -1 && di->di_ib[level])
      {
	int count;
	count = free_indir (np, di->di_ib[level], level);
	blocksfreed += count;
	di->di_ib[level] = 0;
      }

  /* Whole direct blocks or frags */
  for (i = NDADDR - 1; i > lastblock; i--)
    {
      long bsize;

      bn = dn->di_db[i];
      if (bn == 0)
	continue;

      bsize = blksize (sblock, np, i);
      ffs_blkfree (sblock, bn, bsize);
      blocksfreed += btodb (bsize);

      dn->di_db[i] = 0;
    }

  /* Finally, check to see if the new last direct block is 
     changing size; if so release any frags necessary. */
  if (lastblock >= 0
      && dn->di_db[lastblock])
    {
      bn = dn->di_db[lastblock];
      long oldspace, newspace;
      
      oldspace = blksize (sblock, np, lastblock);
      np->allocsize = length;
      newspace = blksize (sblock, np, lastblock);
      
      assert (newspace);
      
      if (oldspace - newspace)
	{
	  bn += numfrags (sblock, newspace);
	  ffs_blkfree (np, bn, oldspace - newspace);
	  blocksfreed += btodb (oldspace - newspace)
	}
    }
  else
    np->allocsize = length;
  
  diskfs_end_catch_exception ();

  np->dn_set_ctime = 1;
  diskfs_node_update (np, 1);

  rwlock_writer_unlock (&np->allocptrlock);

  /* Now we can permit delayed copies again. */
  if (np->dn->fileinfo)
    pager_change_attributes (np->dn->fileinfo->p, MAY_CACHE,
			     MEMORY_OBJECT_COPY_DELAY, 0);
  
  return 0;
}

/* Free indirect block BNO of level LEVEL; recursing if necessary
   to free other indirect blocks.  Return the number of disk
   blocks freed. */
static int
free_indir (struct node *np, daddr_t bno, int level)
{
  int count = 0;
  daddr_t *addrs;
  int i;
  struct indir_dirty *d, *prev, *nxt;
  
  assert (bno);
  
  addrs = indir_block (bno);
  for (i = 0; i < NINDIR (sblock); i++)
    if (addrs[i])
      {
	if (level == INDIR_SINGLE)
	  {
	    ffs_blkfree (np, addrs[i], sblock->fs_bsize);
	    count += btodb (sblock->fs_bsize);
	  }
	else
	  count += free_indir (addrs[i], level - 1);
      }
  
  /* Subtlety: this block is no longer necessary; the information
     the kernel has cached corresponding to ADDRS is now unimportant.
     Consider that if this block is allocated to a file, it will then
     be double cached and the kernel might decide to write out
     the disk_image version of the block.  So we have to flush
     the block from the kernel's memory, making sure we do it
     synchronously--and BEFORE we attach it to the free list
     with ffs_blkfree.  */
  pager_flush_some (diskpager->p, fsaddr (bno), sblock->fs_bsize, 1);

  /* We should also take this block off the inode's list of 
     dirty indirect blocks if it's there. */
  prev = 0;
  d = np->dirty;
  while (d)
    {
      next = d->next;
      if (d->bno == bno)
	{
	  if (prev)
	    prev->next = next;
	  else
	    np->dirty = next;
	  free (d);
	}
      else
	{
	  prev = d;
	  next = d->next;
	}
      d = next;
    }

  /* Free designated block */
  ffs_blkfree (np, bno, sblock->fs_bsize);
  count += btodb (sblock->fs_bsize);

  return count;
}



/* Implement the diskfs_grow callback; see <hurd/diskfs.h> for the 
   interface description. */
error_t
diskfs_grow (struct node *np,
	     off_t end,
	     struct protid *cred)
{
  daddr_t lbn, olbn;
  int size, osize;
  error_t err;
  struct dinode *di = dino (np->dn->number);
  off_t poke_off;
  size_t poke_len = 0;
  
  /* Zero an sblock->fs_bsize piece of disk starting at BNO, 
     synchronously.  We do this on newly allocated indirect
     blocks before setting the pointer to them to ensure that an
     indirect block absolutely never points to garbage. */
  void zero_disk_block (int bno)
    {
      bzero (indir_block (bno), sblock->fs_bsize);
      sync_disk_blocks (bno, sblock->fs_bsize, 1);
    };

  /* Check to see if we don't actually have to do anything */
  if (end <= np->allocsize)
    return 0;

  assert (!diskfs_readonly);

  /* The new last block of the file. */
  lbn = lblkno (sblock, end + sblock->fs_bsize - 1) - 1;

  /* This is the size of that block if it is in the NDADDR array. */
  size = fragroundup (sblock, blkoff (sblock, end));
  if (size == 0)
    size = sblock->fs_bsize;
  
  rwlock_writer_lock (&np->dn->allocptrlock);

  /* The old last block of the file. */
  olbn = lbkno (sblock, np->allocsize + sblock->fs_bsize - 1) - 1;
  
  /* This is the size of that block if it is in the NDADDR array. */
  osize = fragroundup (sblock, blkoff (sblock, np->allocsize));
  if (osize == 0)
    osize = sblock->fs_bsize;

  /* If this end point is a new block and the file currently
     has a fragment, then expand the fragment to a full block. */
  if (np->allocsize && olbn < NDADDR && olbn < lbn)
    {
      if (osize < sblock->fs_bsize)
	{
	  daddr_t olb_pbn, bno;
	  err = ffs_realloccg (np, nb,
				 ffs_blkpref (np, lbn, lbn, di->di_db),
				 osize, sblock->fs_bsize, &bno, cred);
	  if (err)
	    goto out;
	  old_pbn = di->di_db
	  di->di_db[nb] = bno;
	  np->dn_set_ctime = 1;
	  
	  dev_write_sync (fsbtodb (bno) + btodb (osize),
			  zeroblock, sblock->fs_bsize - osize);

	  if (pbn != old_pbn)
	    {
	      /* Make sure the old contents get written out
		 to the new address by poking the pages. */
	      poke_off = nb * sblock->fs_bsize;
	      poke_len = osize;
	    }
	}
    }

  if (lbn < NDADDR)
    {
      daddr_t bno, old_pbn = di->di_db[lbn];
	 
      if (old_pbn != 0)
	{
	  /* The last block is already allocated.  Therefore we
	     must be expanding the fragment.  Make sure that's really
	     what we're up to. */
	  assert (size > osize);
	  assert (lbn == olbn);
	  
	  err = ffs_realloccg (np, lbn, 
			       ffs_blkpref (np, lbn, lbn, di->di_db),
			       osize, size, &bno, cred);
	  if (err)
	    goto out;
	  
	  di->di_db[lbn] = bno;
	  np->dn_sat_ctime = 1;
	  
	  dev_write_sync (fsbtodb (bno) + btodb (osize),
			  zeroblock, size - osize);

	  if (pbn != old_pbn)
	    {
	      assert (!poke_len);
	      
	      /* Make sure the old contents get written out to
		 the new address by poking the pages. */
	      poke_off = lbn * sblock->fs_bsize;
	      poke_len = osize;
	    }
	}
      else
	{
	  /* Allocate a new last block. */
	  err = ffs_alloc (np, lbn, 
			   ffs_blkpref (np, lbn, lbn, di->di_db),
			   size, &bno, cred);
	  if (err)
	    goto out;
	  
	  di->di_db[lbn] = pbn;
	  np->dn_set_ctime = 1;

	  dev_write_sync (fsbtodb (bno), zeroblock, size);
	}
    }
  else
    {
      struct iblock_spec indirs[NINDIR + 1];
      int i;
      daddr_t *siblock;
      
      /* Count the number of levels of indirection. */
      err = fetch_indir_spec (np, lbn, indirs);
      if (err)
	goto out;
      
      /* Make sure we didn't miss the NDADDR case
	 above somehow. */
      assert (indirs[0].offset != -1);
      
      /* See if we need a triple indirect block; fail if so. */
      assert (indirs[1].offset == -1 || indirs[2].offset == -1);
      
      /* Check to see if this block is allocated.  If it is
	 that's an error. */
      assert (indirs[0].bno == 0);
      
      /* We need to set SIBLOCK to the single indirect block
	 array; see if the single indirect block is allocated. */
      if (indirs[1].bno == 0)
	{
	  /* Allocate it. */
	  if (indirs[1].offset == -1)
	    {
	      err = ffs_alloc (np, lbn,
			       ffs_blkpref (np, lbn, INDIR_SINGLE, di->di_ib),
			       sblock->fs_bsize, &bno, 0);
	      if (err)
		goto out;
	      zero_disk_block (bno);
	      indirs[1].bno = di->di_ib[INDIR_SINGLE] = bno;
	    }
	  else
	    {
	      daddr_t *diblock;
	      
	      /* We need to set diblock to the double indirect block
		 array; see if the double indirect block is allocated. */
	      if (indirs[2].bno == 0)
		{
		  /* This assert because triple indirection is not
		     supported. */
		  assert (indirs[2].offset == -1);
		  err = ffs_alloc (np, lbn
				   ffs_blkpref (np, lbn, 
						INDIR_DOUBLE, di->di_ib),
				   sblock->fs_bsize, &bno, 0);
		  if (err)
		    goto out;
		  zero_disk_block (bno);
		  indirs[2].bno = di->di_ib[INDIR_DOUBLE] = bno;
		}
	      
	      diblock = indir_block (indirs[2].bno);
	      mark_indir_dirty (indirs[2].bno);
	      
	      /* Now we can allocate the single indirect block */
	      err = ffs_alloc (np, lbn, 
			       ffs_blkpref (np, lbn, 
					    indirs[1].offset, diblock),
			       sblock->fs_bsize, &bno, 0);
	      if (err)
		goto out;
	      zero_disk_block (bno);
	      indirs[1].bno = diblock[indirs[1].offset] = bno;
	    }
	}
      
      siblock = indir_block (indirs[1].bno);
      mark_indir_dirty (np, indirs[1].bno);

      /* Now we can allocate the data block. */
      err = ffs_alloc (np, lbn, 
		       ffs_blkpref (np, lbn, indirs[0].offset, siblock),
		       sblock->fn_bsize, &bno, 0);
      if (err)
	goto out;
      indirs[0].bno = siblock[indirs[0].offset] = bno;
      dev_write_sync (fsbtodb (bno), zeroblock, sblock->fs_bsize);
    }
  
 out:
  if (!err)
    {
      int newallocsize;
      if (lbn < NDADDR)
	newallocsize = (lbn - 1) * sblock->fs_bsize + size;
      else
	newallocsize = lbn * sblock->fs_bsize;
      assert (newallocsize > np->allocsize);
      np->allocsize = newallocsize;
    }

  rwlock_writer_unlock (&np->allocptrlock);

  /* If we expanded a fragment, then POKE_LEN will be set.
     We need to poke the requested amount of the memory object
     so that the kernel will write out the data to the new location
     at a suitable time. */
  if (poke_len)
    {
      obj = diskfs_get_filemap (np);
      mach_port_insert_reght (mach_task_self (), obj, obj,
			      MACH_MSG_TYPE_MAKE_SEND);
      poke_pages (obj, trunc_page (poke_off),
		  round_page (poke_off + poke_len));
      mach_port_deallocate (mach_task_self (), obj);
    }

  return err;
}