VM: make mapping types explicit
[minix.git] / servers / ext2 / link.c
blob88ac687ea0e08f89fdd8077529ec5122cd4bb6b4
1 /* Created (MFS based):
2 * February 2010 (Evgeniy Ivanov)
3 */
5 #include "fs.h"
6 #include <sys/stat.h>
7 #include <string.h>
8 #include <minix/com.h>
9 #include "buf.h"
10 #include "inode.h"
11 #include "super.h"
12 #include <minix/vfsif.h>
14 #define SAME 1000
16 static int freesp_inode(struct inode *rip, off_t st, off_t end);
17 static int remove_dir(struct inode *rldirp, struct inode *rip, char
18 dir_name[NAME_MAX + 1]);
19 static int unlink_file(struct inode *dirp, struct inode *rip, char
20 file_name[NAME_MAX + 1]);
21 static off_t nextblock(off_t pos, int blocksize);
22 static void zeroblock_half(struct inode *i, off_t p, int l);
23 static void zeroblock_range(struct inode *i, off_t p, off_t h);
25 /* Args to zeroblock_half() */
26 #define FIRST_HALF 0
27 #define LAST_HALF 1
30 /*===========================================================================*
31 * fs_link *
32 *===========================================================================*/
33 int fs_link()
35 /* Perform the link(name1, name2) system call. */
37 struct inode *ip, *rip;
38 register int r;
39 char string[NAME_MAX + 1];
40 struct inode *new_ip;
41 phys_bytes len;
43 /* Copy the link name's last component */
44 len = fs_m_in.REQ_PATH_LEN; /* including trailing '\0' */
45 if (len > NAME_MAX + 1 || len > EXT2_NAME_MAX + 1)
46 return(ENAMETOOLONG);
48 r = sys_safecopyfrom(VFS_PROC_NR, (cp_grant_id_t) fs_m_in.REQ_GRANT, 0,
49 (vir_bytes) string, (size_t) len);
50 if (r != OK) return r;
51 NUL(string, len, sizeof(string));
53 /* Temporarily open the file. */
54 if( (rip = get_inode(fs_dev, fs_m_in.REQ_INODE_NR)) == NULL)
55 return(EINVAL);
57 /* Check to see if the file has maximum number of links already. */
58 r = OK;
59 if (rip->i_links_count >= USHRT_MAX)
60 r = EMLINK;
61 if(rip->i_links_count >= LINK_MAX)
62 r = EMLINK;
64 /* Only super_user may link to directories. */
65 if(r == OK)
66 if( (rip->i_mode & I_TYPE) == I_DIRECTORY && caller_uid != SU_UID)
67 r = EPERM;
69 /* If error with 'name', return the inode. */
70 if (r != OK) {
71 put_inode(rip);
72 return(r);
75 /* Temporarily open the last dir */
76 if( (ip = get_inode(fs_dev, fs_m_in.REQ_DIR_INO)) == NULL) {
77 put_inode(rip);
78 return(EINVAL);
81 if (ip->i_links_count == NO_LINK) { /* Dir does not actually exist */
82 put_inode(rip);
83 put_inode(ip);
84 return(ENOENT);
87 /* If 'name2' exists in full (even if no space) set 'r' to error. */
88 if ((new_ip = advance(ip, string, IGN_PERM)) == NULL) {
89 r = err_code;
90 if(r == ENOENT)
91 r = OK;
92 } else {
93 put_inode(new_ip);
94 r = EEXIST;
97 /* Try to link. */
98 if(r == OK)
99 r = search_dir(ip, string, &rip->i_num, ENTER, IGN_PERM,
100 rip->i_mode & I_TYPE);
102 /* If success, register the linking. */
103 if(r == OK) {
104 rip->i_links_count++;
105 rip->i_update |= CTIME;
106 rip->i_dirt = DIRTY;
109 /* Done. Release both inodes. */
110 put_inode(rip);
111 put_inode(ip);
112 return(r);
116 /*===========================================================================*
117 * fs_unlink *
118 *===========================================================================*/
119 int fs_unlink()
121 /* Perform the unlink(name) or rmdir(name) system call. The code for these two
122 * is almost the same. They differ only in some condition testing. Unlink()
123 * may be used by the superuser to do dangerous things; rmdir() may not.
125 register struct inode *rip;
126 struct inode *rldirp;
127 int r;
128 char string[NAME_MAX + 1];
129 phys_bytes len;
131 /* Copy the last component */
132 len = fs_m_in.REQ_PATH_LEN; /* including trailing '\0' */
133 if (len > NAME_MAX + 1 || len > EXT2_NAME_MAX + 1)
134 return(ENAMETOOLONG);
136 r = sys_safecopyfrom(VFS_PROC_NR, (cp_grant_id_t) fs_m_in.REQ_GRANT,
137 (vir_bytes) 0, (vir_bytes) string, (size_t) len);
138 if (r != OK) return r;
139 NUL(string, len, sizeof(string));
141 /* Temporarily open the dir. */
142 if( (rldirp = get_inode(fs_dev, (ino_t) fs_m_in.REQ_INODE_NR)) == NULL)
143 return(EINVAL);
145 /* The last directory exists. Does the file also exist? */
146 rip = advance(rldirp, string, IGN_PERM);
147 r = err_code;
149 /* If error, return inode. */
150 if(r != OK) {
151 /* Mount point? */
152 if (r == EENTERMOUNT || r == ELEAVEMOUNT) {
153 put_inode(rip);
154 r = EBUSY;
156 put_inode(rldirp);
157 return(r);
160 /* Now test if the call is allowed, separately for unlink() and rmdir(). */
161 if(fs_m_in.m_type == REQ_UNLINK) {
162 /* Only the su may unlink directories, but the su can unlink any
163 * dir.*/
164 if( (rip->i_mode & I_TYPE) == I_DIRECTORY) r = EPERM;
166 /* Actually try to unlink the file; fails if parent is mode 0 etc. */
167 if (r == OK) r = unlink_file(rldirp, rip, string);
168 } else {
169 r = remove_dir(rldirp, rip, string); /* call is RMDIR */
172 /* If unlink was possible, it has been done, otherwise it has not. */
173 put_inode(rip);
174 put_inode(rldirp);
175 return(r);
179 /*===========================================================================*
180 * fs_rdlink *
181 *===========================================================================*/
182 int fs_rdlink()
184 block_t b; /* block containing link text */
185 struct buf *bp = NULL; /* buffer containing link text */
186 char* link_text; /* either bp->b_data or rip->i_block */
187 register struct inode *rip; /* target inode */
188 register int r; /* return value */
189 size_t copylen;
191 copylen = min( (size_t) fs_m_in.REQ_MEM_SIZE, UMAX_FILE_POS);
193 /* Temporarily open the file. */
194 if( (rip = get_inode(fs_dev, (ino_t) fs_m_in.REQ_INODE_NR)) == NULL)
195 return(EINVAL);
197 if (rip->i_size >= MAX_FAST_SYMLINK_LENGTH) {
198 /* normal symlink */
199 if ((b = read_map(rip, (off_t) 0)) == NO_BLOCK) {
200 r = EIO;
201 } else {
202 bp = get_block(rip->i_dev, b, NORMAL);
203 if (bp != NULL) {
204 link_text = bp->b_data;
205 r = OK;
206 } else {
207 r = EIO;
210 } else {
211 /* fast symlink, stored in inode */
212 link_text = (char*) rip->i_block;
213 r = OK;
215 if (r == OK) {
216 /* Passed all checks */
217 /* We can safely cast to unsigned, because copylen is guaranteed to be
218 below max file size */
219 copylen = min( copylen, (unsigned) rip->i_size);
220 r = sys_safecopyto(VFS_PROC_NR, (cp_grant_id_t) fs_m_in.REQ_GRANT,
221 (vir_bytes) 0, (vir_bytes) link_text,
222 (size_t) copylen);
223 put_block(bp, DIRECTORY_BLOCK);
224 if (r == OK)
225 fs_m_out.RES_NBYTES = copylen;
228 put_inode(rip);
229 return(r);
233 /*===========================================================================*
234 * remove_dir *
235 *===========================================================================*/
236 static int remove_dir(rldirp, rip, dir_name)
237 struct inode *rldirp; /* parent directory */
238 struct inode *rip; /* directory to be removed */
239 char dir_name[NAME_MAX + 1]; /* name of directory to be removed */
241 /* A directory file has to be removed. Five conditions have to met:
242 * - The file must be a directory
243 * - The directory must be empty (except for . and ..)
244 * - The final component of the path must not be . or ..
245 * - The directory must not be the root of a mounted file system (VFS)
246 * - The directory must not be anybody's root/working directory (VFS)
248 int r;
250 /* search_dir checks that rip is a directory too. */
251 if ((r = search_dir(rip, "", NULL, IS_EMPTY, IGN_PERM, 0)) != OK)
252 return r;
254 if (strcmp(dir_name, ".") == 0 || strcmp(dir_name, "..") == 0)return(EINVAL);
255 if (rip->i_num == ROOT_INODE) return(EBUSY); /* can't remove 'root' */
257 /* Actually try to unlink the file; fails if parent is mode 0 etc. */
258 if ((r = unlink_file(rldirp, rip, dir_name)) != OK) return r;
260 /* Unlink . and .. from the dir. The super user can link and unlink any dir,
261 * so don't make too many assumptions about them.
263 (void) unlink_file(rip, NULL, dot1);
264 (void) unlink_file(rip, NULL, dot2);
265 return(OK);
269 /*===========================================================================*
270 * unlink_file *
271 *===========================================================================*/
272 static int unlink_file(dirp, rip, file_name)
273 struct inode *dirp; /* parent directory of file */
274 struct inode *rip; /* inode of file, may be NULL too. */
275 char file_name[NAME_MAX + 1]; /* name of file to be removed */
277 /* Unlink 'file_name'; rip must be the inode of 'file_name' or NULL. */
279 ino_t numb; /* inode number */
280 int r;
282 /* If rip is not NULL, it is used to get faster access to the inode. */
283 if (rip == NULL) {
284 /* Search for file in directory and try to get its inode. */
285 err_code = search_dir(dirp, file_name, &numb, LOOK_UP, IGN_PERM, 0);
286 if (err_code == OK) rip = get_inode(dirp->i_dev, (int) numb);
287 if (err_code != OK || rip == NULL) return(err_code);
288 } else {
289 dup_inode(rip); /* inode will be returned with put_inode */
292 r = search_dir(dirp, file_name, NULL, DELETE, IGN_PERM, 0);
294 if (r == OK) {
295 rip->i_links_count--; /* entry deleted from parent's dir */
296 rip->i_update |= CTIME;
297 rip->i_dirt = DIRTY;
300 put_inode(rip);
301 return(r);
305 /*===========================================================================*
306 * fs_rename *
307 *===========================================================================*/
308 int fs_rename()
310 /* Perform the rename(name1, name2) system call. */
311 struct inode *old_dirp, *old_ip; /* ptrs to old dir, file inodes */
312 struct inode *new_dirp, *new_ip; /* ptrs to new dir, file inodes */
313 struct inode *new_superdirp, *next_new_superdirp;
314 int r = OK; /* error flag; initially no error */
315 int odir, ndir; /* TRUE iff {old|new} file is dir */
316 int same_pdir; /* TRUE iff parent dirs are the same */
317 char old_name[NAME_MAX + 1], new_name[NAME_MAX + 1];
318 ino_t numb;
319 phys_bytes len;
321 /* Copy the last component of the old name */
322 len = fs_m_in.REQ_REN_LEN_OLD; /* including trailing '\0' */
323 if (len > NAME_MAX + 1 || len > EXT2_NAME_MAX + 1)
324 return(ENAMETOOLONG);
326 r = sys_safecopyfrom(VFS_PROC_NR, (cp_grant_id_t) fs_m_in.REQ_REN_GRANT_OLD,
327 (vir_bytes) 0, (vir_bytes) old_name, (size_t) len);
328 if (r != OK) return r;
329 NUL(old_name, len, sizeof(old_name));
331 /* Copy the last component of the new name */
332 len = fs_m_in.REQ_REN_LEN_NEW; /* including trailing '\0' */
333 if (len > NAME_MAX + 1 || len > EXT2_NAME_MAX + 1)
334 return(ENAMETOOLONG);
336 r = sys_safecopyfrom(VFS_PROC_NR, (cp_grant_id_t) fs_m_in.REQ_REN_GRANT_NEW,
337 (vir_bytes) 0, (vir_bytes) new_name, (size_t) len);
338 if (r != OK) return r;
339 NUL(new_name, len, sizeof(new_name));
341 /* Get old dir inode */
342 if( (old_dirp = get_inode(fs_dev, (ino_t) fs_m_in.REQ_REN_OLD_DIR)) == NULL)
343 return(err_code);
345 old_ip = advance(old_dirp, old_name, IGN_PERM);
346 r = err_code;
348 if (r == EENTERMOUNT || r == ELEAVEMOUNT) {
349 put_inode(old_ip);
350 old_ip = NULL;
351 if (r == EENTERMOUNT) r = EXDEV; /* should this fail at all? */
352 else if (r == ELEAVEMOUNT) r = EINVAL; /* rename on dot-dot */
353 } else if (old_ip == NULL) {
354 return(err_code);
357 /* Get new dir inode */
358 if( (new_dirp = get_inode(fs_dev, (ino_t) fs_m_in.REQ_REN_NEW_DIR)) == NULL) {
359 put_inode(old_ip);
360 put_inode(old_dirp);
361 return(err_code);
362 } else {
363 if (new_dirp->i_links_count == NO_LINK) { /* Dir does not actually exist */
364 put_inode(old_ip);
365 put_inode(old_dirp);
366 put_inode(new_dirp);
367 return(ENOENT);
371 new_ip = advance(new_dirp, new_name, IGN_PERM); /* not required to exist */
373 /* However, if the check failed because the file does exist, don't continue.
374 * Note that ELEAVEMOUNT is covered by the dot-dot check later. */
375 if(err_code == EENTERMOUNT) {
376 put_inode(new_ip);
377 new_ip = NULL;
378 r = EBUSY;
381 if(old_ip != NULL)
382 odir = ((old_ip->i_mode & I_TYPE) == I_DIRECTORY); /* TRUE iff dir */
383 else
384 odir = FALSE;
386 /* If it is ok, check for a variety of possible errors. */
387 if(r == OK) {
388 same_pdir = (old_dirp == new_dirp);
390 /* The old inode must not be a superdirectory of the new last dir. */
391 if (odir && !same_pdir) {
392 dup_inode(new_superdirp = new_dirp);
393 while (TRUE) { /* may hang in a file system loop */
394 if (new_superdirp == old_ip) {
395 put_inode(new_superdirp);
396 r = EINVAL;
397 break;
399 next_new_superdirp = advance(new_superdirp, dot2,
400 IGN_PERM);
402 put_inode(new_superdirp);
403 if(next_new_superdirp == new_superdirp) {
404 put_inode(new_superdirp);
405 break;
407 if(err_code == ELEAVEMOUNT) {
408 /* imitate that we are back at the root,
409 * cross device checked already on VFS */
410 put_inode(next_new_superdirp);
411 err_code = OK;
412 break;
414 new_superdirp = next_new_superdirp;
415 if(new_superdirp == NULL) {
416 /* Missing ".." entry. Assume the worst. */
417 r = EINVAL;
418 break;
423 /* The old or new name must not be . or .. */
424 if(strcmp(old_name, ".") == 0 || strcmp(old_name, "..") == 0 ||
425 strcmp(new_name, ".") == 0 || strcmp(new_name, "..") == 0) {
426 r = EINVAL;
428 /* Both parent directories must be on the same device.
429 if(old_dirp->i_dev != new_dirp->i_dev) r = EXDEV; */
431 /* Some tests apply only if the new path exists. */
432 if(new_ip == NULL) {
433 /* don't rename a file with a file system mounted on it.
434 if (old_ip->i_dev != old_dirp->i_dev) r = EXDEV;*/
435 if(odir && (new_dirp->i_links_count >= SHRT_MAX ||
436 new_dirp->i_links_count >= LINK_MAX) &&
437 !same_pdir && r == OK) {
438 r = EMLINK;
440 } else {
441 if(old_ip == new_ip) r = SAME; /* old=new */
443 ndir = ((new_ip->i_mode & I_TYPE) == I_DIRECTORY);/* dir ? */
444 if(odir == TRUE && ndir == FALSE) r = ENOTDIR;
445 if(odir == FALSE && ndir == TRUE) r = EISDIR;
449 /* If a process has another root directory than the system root, we might
450 * "accidently" be moving it's working directory to a place where it's
451 * root directory isn't a super directory of it anymore. This can make
452 * the function chroot useless. If chroot will be used often we should
453 * probably check for it here. */
455 /* The rename will probably work. Only two things can go wrong now:
456 * 1. being unable to remove the new file. (when new file already exists)
457 * 2. being unable to make the new directory entry. (new file doesn't exists)
458 * [directory has to grow by one block and cannot because the disk
459 * is completely full].
461 if(r == OK) {
462 if(new_ip != NULL) {
463 /* There is already an entry for 'new'. Try to remove it. */
464 if(odir)
465 r = remove_dir(new_dirp, new_ip, new_name);
466 else
467 r = unlink_file(new_dirp, new_ip, new_name);
469 /* if r is OK, the rename will succeed, while there is now an
470 * unused entry in the new parent directory. */
473 if(r == OK) {
474 /* If the new name will be in the same parent directory as the old
475 * one, first remove the old name to free an entry for the new name,
476 * otherwise first try to create the new name entry to make sure
477 * the rename will succeed.
479 numb = old_ip->i_num; /* inode number of old file */
481 if(same_pdir) {
482 r = search_dir(old_dirp,old_name, NULL, DELETE,IGN_PERM, 0);
483 /* shouldn't go wrong. */
484 if(r == OK)
485 (void) search_dir(old_dirp, new_name, &numb, ENTER, IGN_PERM,
486 old_ip->i_mode & I_TYPE);
487 } else {
488 r = search_dir(new_dirp, new_name, &numb, ENTER, IGN_PERM,
489 old_ip->i_mode & I_TYPE);
490 if(r == OK)
491 (void) search_dir(old_dirp, old_name, (ino_t *) 0, DELETE,
492 IGN_PERM, 0);
495 /* If r is OK, the ctime and mtime of old_dirp and new_dirp have been marked
496 * for update in search_dir. */
498 if(r == OK && odir && !same_pdir) {
499 /* Update the .. entry in the directory (still points to old_dirp).*/
500 numb = new_dirp->i_num;
501 (void) unlink_file(old_ip, NULL, dot2);
502 if(search_dir(old_ip, dot2, &numb, ENTER, IGN_PERM, I_DIRECTORY) == OK) {
503 /* New link created. */
504 new_dirp->i_links_count++;
505 new_dirp->i_dirt = DIRTY;
509 /* Release the inodes. */
510 put_inode(old_dirp);
511 put_inode(old_ip);
512 put_inode(new_dirp);
513 put_inode(new_ip);
514 return(r == SAME ? OK : r);
518 /*===========================================================================*
519 * fs_ftrunc *
520 *===========================================================================*/
521 int fs_ftrunc(void)
523 struct inode *rip;
524 off_t start, end;
525 int r;
527 if( (rip = find_inode(fs_dev, (ino_t) fs_m_in.REQ_INODE_NR)) == NULL)
528 return(EINVAL);
530 start = fs_m_in.REQ_TRC_START_LO;
531 end = fs_m_in.REQ_TRC_END_LO;
533 if (end == 0)
534 r = truncate_inode(rip, start);
535 else
536 r = freesp_inode(rip, start, end);
538 return(r);
542 /*===========================================================================*
543 * truncate_inode *
544 *===========================================================================*/
545 int truncate_inode(rip, newsize)
546 register struct inode *rip; /* pointer to inode to be truncated */
547 off_t newsize; /* inode must become this size */
549 /* Set inode to a certain size, freeing any blocks no longer referenced
550 * and updating the size in the inode. If the inode is extended, the
551 * extra space is a hole that reads as zeroes.
553 * Nothing special has to happen to file pointers if inode is opened in
554 * O_APPEND mode, as this is different per fd and is checked when
555 * writing is done.
557 int r;
558 mode_t file_type;
560 discard_preallocated_blocks(rip);
562 file_type = rip->i_mode & I_TYPE; /* check to see if file is special */
563 if (file_type == I_CHAR_SPECIAL || file_type == I_BLOCK_SPECIAL)
564 return(EINVAL);
565 if (newsize > rip->i_sp->s_max_size) /* don't let inode grow too big */
566 return(EFBIG);
568 /* Free the actual space if truncating. */
569 if (newsize < rip->i_size) {
570 if ((r = freesp_inode(rip, newsize, rip->i_size)) != OK)
571 return(r);
574 /* Clear the rest of the last block if expanding. */
575 if (newsize > rip->i_size) zeroblock_half(rip, rip->i_size, LAST_HALF);
577 /* Next correct the inode size. */
578 rip->i_size = newsize;
579 rip->i_update |= CTIME | MTIME;
580 rip->i_dirt = DIRTY;
582 return(OK);
586 /*===========================================================================*
587 * freesp_inode *
588 *===========================================================================*/
589 static int freesp_inode(rip, start, end)
590 register struct inode *rip; /* pointer to inode to be partly freed */
591 off_t start, end; /* range of bytes to free (end uninclusive) */
593 /* Cut an arbitrary hole in an inode. The caller is responsible for checking
594 * the reasonableness of the inode type of rip. The reason is this is that
595 * this function can be called for different reasons, for which different
596 * sets of inode types are reasonable. Adjusting the final size of the inode
597 * is to be done by the caller too, if wished.
599 * Consumers of this function currently are truncate_inode() (used to
600 * free indirect and data blocks for any type of inode, but also to
601 * implement the ftruncate() and truncate() system calls) and the F_FREESP
602 * fcntl().
604 off_t p, e;
605 int r;
606 unsigned short block_size = rip->i_sp->s_block_size;
607 int zero_last, zero_first;
609 discard_preallocated_blocks(rip);
611 if (rip->i_blocks == 0) {
612 /* Either hole or symlink. Freeing fast symlink using
613 * write_map() causes segfaults since it doesn't use any
614 * blocks, but uses i_block[] to store target.
616 return(OK);
619 if(end > rip->i_size) /* freeing beyond end makes no sense */
620 end = rip->i_size;
621 if(end <= start) /* end is uninclusive, so start<end */
622 return(EINVAL);
624 /* If freeing doesn't cross a block boundary, then we may only zero
625 * a range of the block.
627 zero_last = start % block_size;
628 zero_first = end % block_size && end < rip->i_size;
629 if (start/block_size == (end-1)/block_size && (zero_last || zero_first)) {
630 zeroblock_range(rip, start, end-start);
631 } else {
632 /* First zero unused part of partly used blocks. */
633 if (zero_last)
634 zeroblock_half(rip, start, LAST_HALF);
635 if (zero_first)
636 zeroblock_half(rip, end, FIRST_HALF);
638 /* Now completely free the completely unused blocks.
639 * write_map() will free unused indirect
640 * blocks too. Converting the range to block numbers avoids
641 * overflow on p when doing e.g. 'p += block_size'.
643 e = end / block_size;
644 if (end == rip->i_size && (end % block_size))
645 e++;
646 for (p = nextblock(start, block_size)/block_size; p < e; p++) {
647 if ((r = write_map(rip, p*block_size, NO_BLOCK, WMAP_FREE)) != OK)
648 return(r);
652 rip->i_update |= CTIME | MTIME;
653 rip->i_dirt = DIRTY;
655 return(OK);
659 /*===========================================================================*
660 * nextblock *
661 *===========================================================================*/
662 static off_t nextblock(pos, block_size)
663 off_t pos;
664 unsigned short block_size;
666 /* Return the first position in the next block after position 'pos'
667 * (unless this is the first position in the current block).
668 * This can be done in one expression, but that can overflow pos.
670 off_t p;
671 p = (pos / block_size) * block_size;
672 if (pos % block_size) p += block_size; /* Round up. */
673 return(p);
677 /*===========================================================================*
678 * zeroblock_half *
679 *===========================================================================*/
680 static void zeroblock_half(rip, pos, half)
681 struct inode *rip;
682 off_t pos;
683 int half;
685 /* Zero the upper or lower 'half' of a block that holds position 'pos'.
686 * half can be FIRST_HALF or LAST_HALF.
688 * FIRST_HALF: 0..pos-1 will be zeroed
689 * LAST_HALF: pos..blocksize-1 will be zeroed
691 off_t offset, len;
693 /* Offset of zeroing boundary. */
694 offset = pos % rip->i_sp->s_block_size;
696 if(half == LAST_HALF) {
697 len = rip->i_sp->s_block_size - offset;
698 } else {
699 len = offset;
700 pos -= offset;
703 zeroblock_range(rip, pos, len);
707 /*===========================================================================*
708 * zeroblock_range *
709 *===========================================================================*/
710 static void zeroblock_range(rip, pos, len)
711 struct inode *rip;
712 off_t pos;
713 off_t len;
715 /* Zero a range in a block.
716 * This function is used to zero a segment of a block.
718 block_t b;
719 struct buf *bp;
720 off_t offset;
722 if (!len) return; /* no zeroing to be done. */
723 if ( (b = read_map(rip, pos)) == NO_BLOCK) return;
724 if ( (bp = get_block(rip->i_dev, b, NORMAL)) == NULL)
725 panic("zeroblock_range: no block");
726 offset = pos % rip->i_sp->s_block_size;
727 if (offset + len > rip->i_sp->s_block_size)
728 panic("zeroblock_range: len too long", len);
729 memset(bp->b_data + offset, 0, len);
730 bp->b_dirt = DIRTY;
731 put_block(bp, FULL_DATA_BLOCK);