. service tells you which device it couldn't stat
[minix3.git] / servers / mfs / link.c
blob13db3af031164d2274450ce530f40a4e03ce0ee4
3 #include "fs.h"
4 #include <sys/stat.h>
5 #include <string.h>
6 #include <minix/com.h>
7 #include <minix/callnr.h>
9 #include "buf.h"
10 #include "inode.h"
11 #include "super.h"
13 #include <minix/vfsif.h>
15 #define SAME 1000
17 FORWARD _PROTOTYPE( int remove_dir, (struct inode *rldirp, struct inode *rip,
18 char dir_name[NAME_MAX]) );
19 FORWARD _PROTOTYPE( int unlink_file, (struct inode *dirp, struct inode *rip,
20 char file_name[NAME_MAX]) );
21 FORWARD _PROTOTYPE( off_t nextblock, (off_t pos, int zonesize) );
22 FORWARD _PROTOTYPE( void zeroblock_half, (struct inode *i, off_t p, int l));
23 FORWARD _PROTOTYPE( 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 PUBLIC 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];
40 struct inode *new_ip;
41 phys_bytes len;
43 caller_uid = fs_m_in.REQ_UID;
44 caller_gid = fs_m_in.REQ_GID;
46 len = MFS_MIN(fs_m_in.REQ_PATH_LEN, sizeof(string));
47 /* Copy the link name's last component */
48 r = sys_datacopy(FS_PROC_NR, (vir_bytes) fs_m_in.REQ_PATH,
49 SELF, (vir_bytes) string, (phys_bytes) len);
50 if (r != OK) return r;
51 MFS_NUL(string, len, sizeof(string));
53 /* Temporarily open the file. */
54 if ( (rip = get_inode(fs_dev, fs_m_in.REQ_LINKED_FILE)) == NIL_INODE) {
55 printf("MFS(%d) get_inode by fs_link() failed\n", SELF_E);
56 return(EINVAL);
59 /* Check to see if the file has maximum number of links already. */
60 r = OK;
61 if (rip->i_nlinks >= (rip->i_sp->s_version == V1 ? CHAR_MAX : SHRT_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_LINK_PARENT)) == NIL_INODE) {
77 printf("MFS(%d) get_inode by fs_link() failed\n", SELF_E);
78 return(EINVAL);
81 /* If 'name2' exists in full (even if no space) set 'r' to error. */
82 if (r == OK) {
83 if ( (new_ip = advance(&ip, string)) == NIL_INODE) {
84 r = err_code;
85 if (r == ENOENT) r = OK;
86 } else {
87 put_inode(new_ip);
88 r = EEXIST;
92 /* Try to link. */
93 if (r == OK)
94 r = search_dir(ip, string, &rip->i_num, ENTER);
96 /* If success, register the linking. */
97 if (r == OK) {
98 rip->i_nlinks++;
99 rip->i_update |= CTIME;
100 rip->i_dirt = DIRTY;
103 /* Done. Release both inodes. */
104 put_inode(rip);
105 put_inode(ip);
106 return(r);
110 /*===========================================================================*
111 * fs_unlink *
112 *===========================================================================*/
113 PUBLIC int fs_unlink()
115 /* Perform the unlink(name) or rmdir(name) system call. The code for these two
116 * is almost the same. They differ only in some condition testing. Unlink()
117 * may be used by the superuser to do dangerous things; rmdir() may not.
119 register struct inode *rip;
120 struct inode *rldirp;
121 int r;
122 char string[NAME_MAX];
123 phys_bytes len;
125 caller_uid = fs_m_in.REQ_UID;
126 caller_gid = fs_m_in.REQ_GID;
128 /* Copy the last component */
129 len = MFS_MIN(fs_m_in.REQ_PATH_LEN, sizeof(string));
130 r = sys_datacopy(FS_PROC_NR, (vir_bytes) fs_m_in.REQ_PATH,
131 SELF, (vir_bytes) string, (phys_bytes) len);
132 if (r != OK) return r;
133 MFS_NUL(string, len, sizeof(string));
135 /* Temporarily open the dir. */
136 if ( (rldirp = get_inode(fs_dev, fs_m_in.REQ_INODE_NR)) == NIL_INODE) {
137 return(EINVAL);
140 /* The last directory exists. Does the file also exist? */
141 r = OK;
142 if ( (rip = advance(&rldirp, string)) == NIL_INODE) r = err_code;
144 /* If error, return inode. */
145 if (r != OK) {
146 /* Mount point? */
147 if (r == EENTERMOUNT || r == ELEAVEMOUNT)
148 r = EBUSY;
149 put_inode(rldirp);
150 return(r);
153 /* Now test if the call is allowed, separately for unlink() and rmdir(). */
154 if (fs_m_in.m_type == REQ_UNLINK) {
155 /* Only the su may unlink directories, but the su can unlink any dir.*/
156 if ( (rip->i_mode & I_TYPE) == I_DIRECTORY
157 && caller_uid != SU_UID) r = EPERM;
159 /* Don't unlink a file if it is the root of a mounted file system. */
160 if (rip->i_num == ROOT_INODE) r = EBUSY;
162 /* Actually try to unlink the file; fails if parent is mode 0 etc. */
163 if (r == OK) r = unlink_file(rldirp, rip, string);
166 else {
167 r = remove_dir(rldirp, rip, string); /* call is RMDIR */
170 /* If unlink was possible, it has been done, otherwise it has not. */
171 put_inode(rip);
172 put_inode(rldirp);
173 return(r);
178 /*===========================================================================*
179 * fs_rdlink *
180 *===========================================================================*/
181 PUBLIC int fs_rdlink()
183 block_t b; /* block containing link text */
184 struct buf *bp; /* buffer containing link text */
185 register struct inode *rip; /* target inode */
186 register int r; /* return value */
187 int copylen;
189 copylen = fs_m_in.REQ_SLENGTH;
190 caller_uid = fs_m_in.REQ_UID;
191 caller_gid = fs_m_in.REQ_GID;
193 /* Temporarily open the file. */
194 if ( (rip = get_inode(fs_dev, fs_m_in.REQ_INODE_NR)) == NIL_INODE) {
195 return(EINVAL);
198 r = EACCES;
199 if (S_ISLNK(rip->i_mode) && (b = read_map(rip, (off_t) 0)) != NO_BLOCK) {
200 if (copylen <= 0) r = EINVAL;
201 else if (copylen < rip->i_size) r = ERANGE;
202 else {
203 if(rip->i_size < copylen) copylen = rip->i_size;
204 bp = get_block(rip->i_dev, b, NORMAL);
205 r = sys_vircopy(SELF, D, (vir_bytes) bp->b_data,
206 fs_m_in.REQ_WHO_E, D, (vir_bytes) fs_m_in.REQ_USER_ADDR,
207 (vir_bytes) copylen);
209 if (r == OK) r = copylen;
210 put_block(bp, DIRECTORY_BLOCK);
214 put_inode(rip);
215 return(r);
219 /*===========================================================================*
220 * remove_dir *
221 *===========================================================================*/
222 PRIVATE int remove_dir(rldirp, rip, dir_name)
223 struct inode *rldirp; /* parent directory */
224 struct inode *rip; /* directory to be removed */
225 char dir_name[NAME_MAX]; /* name of directory to be removed */
227 /* A directory file has to be removed. Five conditions have to met:
228 * - The file must be a directory
229 * - The directory must be empty (except for . and ..)
230 * - The final component of the path must not be . or ..
231 * - The directory must not be the root of a mounted file system (VFS)
232 * - The directory must not be anybody's root/working directory (VFS)
234 int r;
236 /* search_dir checks that rip is a directory too. */
237 if ((r = search_dir(rip, "", (ino_t *) 0, IS_EMPTY)) != OK) return r;
239 if (strcmp(dir_name, ".") == 0 || strcmp(dir_name, "..") == 0)return(EINVAL);
240 if (rip->i_num == ROOT_INODE) return(EBUSY); /* can't remove 'root' */
242 /* Actually try to unlink the file; fails if parent is mode 0 etc. */
243 if ((r = unlink_file(rldirp, rip, dir_name)) != OK) return r;
245 /* Unlink . and .. from the dir. The super user can link and unlink any dir,
246 * so don't make too many assumptions about them.
248 (void) unlink_file(rip, NIL_INODE, dot1);
249 (void) unlink_file(rip, NIL_INODE, dot2);
250 return(OK);
254 /*===========================================================================*
255 * unlink_file *
256 *===========================================================================*/
257 PRIVATE int unlink_file(dirp, rip, file_name)
258 struct inode *dirp; /* parent directory of file */
259 struct inode *rip; /* inode of file, may be NIL_INODE too. */
260 char file_name[NAME_MAX]; /* name of file to be removed */
262 /* Unlink 'file_name'; rip must be the inode of 'file_name' or NIL_INODE. */
264 ino_t numb; /* inode number */
265 int r;
267 /* If rip is not NIL_INODE, it is used to get faster access to the inode. */
268 if (rip == NIL_INODE) {
269 /* Search for file in directory and try to get its inode. */
270 err_code = search_dir(dirp, file_name, &numb, LOOK_UP);
271 if (err_code == OK) rip = get_inode(dirp->i_dev, (int) numb);
272 if (err_code != OK || rip == NIL_INODE) return(err_code);
273 } else {
274 dup_inode(rip); /* inode will be returned with put_inode */
277 r = search_dir(dirp, file_name, (ino_t *) 0, DELETE);
279 if (r == OK) {
280 rip->i_nlinks--; /* entry deleted from parent's dir */
281 rip->i_update |= CTIME;
282 rip->i_dirt = DIRTY;
285 put_inode(rip);
286 return(r);
290 /*===========================================================================*
291 * fs_rename *
292 *===========================================================================*/
293 PUBLIC int fs_rename()
295 /* Perform the rename(name1, name2) system call. */
296 struct inode *old_dirp, *old_ip; /* ptrs to old dir, file inodes */
297 struct inode *new_dirp, *new_ip; /* ptrs to new dir, file inodes */
298 struct inode *new_superdirp, *next_new_superdirp;
299 int r = OK; /* error flag; initially no error */
300 int odir, ndir; /* TRUE iff {old|new} file is dir */
301 int same_pdir; /* TRUE iff parent dirs are the same */
302 char old_name[NAME_MAX], new_name[NAME_MAX];
303 ino_t numb;
304 phys_bytes len;
305 int r1;
307 caller_uid = fs_m_in.REQ_UID;
308 caller_gid = fs_m_in.REQ_GID;
310 /* Copy the last component of the old name */
311 len = MFS_MIN(fs_m_in.REQ_PATH_LEN, sizeof(old_name));
312 r = sys_datacopy(FS_PROC_NR, (vir_bytes) fs_m_in.REQ_PATH,
313 SELF, (vir_bytes) old_name, (phys_bytes) len);
314 if (r != OK) return r;
315 MFS_NUL(old_name, len, sizeof(old_name));
317 /* Copy the last component of the new name */
318 len = MFS_MIN(fs_m_in.REQ_SLENGTH, sizeof(new_name));
319 r = sys_datacopy(FS_PROC_NR, (vir_bytes) fs_m_in.REQ_USER_ADDR,
320 SELF, (vir_bytes) new_name, (phys_bytes) len);
321 if (r != OK) return r;
322 MFS_NUL(new_name, len, sizeof(new_name));
324 /* Get old dir inode */
325 if ( (old_dirp = get_inode(fs_dev, fs_m_in.REQ_OLD_DIR)) == NIL_INODE)
326 return(err_code);
328 if ( (old_ip = advance(&old_dirp, old_name)) == NIL_INODE) r = err_code;
330 /* Get new dir inode */
331 if ( (new_dirp = get_inode(fs_dev, fs_m_in.REQ_NEW_DIR)) == NIL_INODE)
332 r = err_code;
333 new_ip = advance(&new_dirp, new_name); /* not required to exist */
335 if (old_ip != NIL_INODE)
336 odir = ((old_ip->i_mode & I_TYPE) == I_DIRECTORY); /* TRUE iff dir */
338 /* If it is ok, check for a variety of possible errors. */
339 if (r == OK) {
340 same_pdir = (old_dirp == new_dirp);
342 /* The old inode must not be a superdirectory of the new last dir. */
343 if (odir && !same_pdir) {
344 dup_inode(new_superdirp = new_dirp);
345 while (TRUE) { /* may hang in a file system loop */
346 if (new_superdirp == old_ip) {
347 put_inode(new_superdirp);
348 r = EINVAL;
349 break;
351 next_new_superdirp = advance(&new_superdirp, dot2);
352 put_inode(new_superdirp);
354 if (next_new_superdirp == new_superdirp) {
355 put_inode(new_superdirp);
356 break;
359 if (err_code == ELEAVEMOUNT) {
360 /* imitate that we are back at the root,
361 * cross device checked already on VFS */
362 /*next_new_superdirp = new_superdirp;*/
363 err_code = OK;
364 break;
366 new_superdirp = next_new_superdirp;
367 if (new_superdirp == NIL_INODE) {
368 /* Missing ".." entry. Assume the worst. */
369 r = EINVAL;
370 break;
373 /*put_inode(new_superdirp);*/
376 /* The old or new name must not be . or .. */
377 if (strcmp(old_name, ".")==0 || strcmp(old_name, "..")==0 ||
378 strcmp(new_name, ".")==0 || strcmp(new_name, "..")==0) {
379 r = EINVAL;
381 /* Both parent directories must be on the same device.
382 if (old_dirp->i_dev != new_dirp->i_dev) r = EXDEV; */
384 /* Parent dirs must be writable, searchable and on a writable device */
385 if ((r1 = forbidden(old_dirp, W_BIT | X_BIT)) != OK ||
386 (r1 = forbidden(new_dirp, W_BIT | X_BIT)) != OK) {
387 r = r1;
390 /* Some tests apply only if the new path exists. */
391 if (new_ip == NIL_INODE) {
392 /* don't rename a file with a file system mounted on it.
393 if (old_ip->i_dev != old_dirp->i_dev) r = EXDEV;*/
394 if (odir && new_dirp->i_nlinks >=
395 (new_dirp->i_sp->s_version == V1 ? CHAR_MAX : SHRT_MAX) &&
396 !same_pdir && r == OK) {
397 r = EMLINK;
400 else {
401 if (old_ip == new_ip) {
402 r = SAME; /* old=new */
405 /* has the old file or new file a file system mounted on it?
406 if (old_ip->i_dev != new_ip->i_dev) r = EXDEV;
409 ndir = ((new_ip->i_mode & I_TYPE) == I_DIRECTORY); /* dir ? */
410 if (odir == TRUE && ndir == FALSE) {
411 r = ENOTDIR;
413 if (odir == FALSE && ndir == TRUE) {
414 r = EISDIR;
419 /* If a process has another root directory than the system root, we might
420 * "accidently" be moving it's working directory to a place where it's
421 * root directory isn't a super directory of it anymore. This can make
422 * the function chroot useless. If chroot will be used often we should
423 * probably check for it here.
426 /* The rename will probably work. Only two things can go wrong now:
427 * 1. being unable to remove the new file. (when new file already exists)
428 * 2. being unable to make the new directory entry. (new file doesn't exists)
429 * [directory has to grow by one block and cannot because the disk
430 * is completely full].
432 if (r == OK) {
433 if (new_ip != NIL_INODE) {
434 /* There is already an entry for 'new'. Try to remove it. */
435 if (odir)
436 r = remove_dir(new_dirp, new_ip, new_name);
437 else
438 r = unlink_file(new_dirp, new_ip, new_name);
440 /* if r is OK, the rename will succeed, while there is now an
441 * unused entry in the new parent directory.
445 if (r == OK) {
446 /* If the new name will be in the same parent directory as the old one,
447 * first remove the old name to free an entry for the new name,
448 * otherwise first try to create the new name entry to make sure
449 * the rename will succeed.
451 numb = old_ip->i_num; /* inode number of old file */
453 if (same_pdir) {
454 r = search_dir(old_dirp, old_name, (ino_t *) 0, DELETE);
455 /* shouldn't go wrong. */
456 if (r==OK) (void) search_dir(old_dirp, new_name, &numb, ENTER);
457 } else {
458 r = search_dir(new_dirp, new_name, &numb, ENTER);
459 if (r == OK)
460 (void) search_dir(old_dirp, old_name, (ino_t *) 0, DELETE);
463 /* If r is OK, the ctime and mtime of old_dirp and new_dirp have been marked
464 * for update in search_dir.
467 if (r == OK && odir && !same_pdir) {
468 /* Update the .. entry in the directory (still points to old_dirp). */
469 numb = new_dirp->i_num;
470 (void) unlink_file(old_ip, NIL_INODE, dot2);
471 if (search_dir(old_ip, dot2, &numb, ENTER) == OK) {
472 /* New link created. */
473 new_dirp->i_nlinks++;
474 new_dirp->i_dirt = DIRTY;
478 /* Release the inodes. */
479 put_inode(old_dirp);
480 put_inode(old_ip);
481 put_inode(new_dirp);
482 put_inode(new_ip);
483 return(r == SAME ? OK : r);
487 /*===========================================================================*
488 * fs_trunc *
489 *===========================================================================*/
490 PUBLIC int fs_trunc()
492 struct inode *rip;
493 int r = OK;
495 caller_uid = fs_m_in.REQ_UID;
496 caller_gid = fs_m_in.REQ_GID;
498 /* Temporarily open the file. */
499 if ( (rip = get_inode(fs_dev, fs_m_in.REQ_INODE_NR)) == NIL_INODE) {
500 printf("MFS(%d) get_inode by fs_chmod() failed\n", SELF_E);
501 return(EINVAL);
504 if ( (rip->i_mode & I_TYPE) != I_REGULAR)
505 r = EINVAL;
506 else
507 r = truncate_inode(rip, fs_m_in.REQ_LENGTH);
509 put_inode(rip);
511 return r;
514 /*===========================================================================*
515 * fs_ftrunc *
516 *===========================================================================*/
517 PUBLIC int fs_ftrunc(void)
519 struct inode *rip;
520 off_t start, end;
521 int r;
523 if ( (rip = find_inode(fs_dev, fs_m_in.REQ_FD_INODE_NR))
524 == NIL_INODE) {
525 printf("FSfreesp: couldn't find inode %d\n",
526 fs_m_in.REQ_FD_INODE_NR);
527 return EINVAL;
530 start = fs_m_in.REQ_FD_START;
531 end = fs_m_in.REQ_FD_END;
533 if (end == 0) {
534 r = truncate_inode(rip, start);
536 else {
537 r = freesp_inode(rip, start, end);
540 return r;
545 /*===========================================================================*
546 * truncate_inode *
547 *===========================================================================*/
548 PUBLIC int truncate_inode(rip, newsize)
549 register struct inode *rip; /* pointer to inode to be truncated */
550 off_t newsize; /* inode must become this size */
552 /* Set inode to a certain size, freeing any zones no longer referenced
553 * and updating the size in the inode. If the inode is extended, the
554 * extra space is a hole that reads as zeroes.
556 * Nothing special has to happen to file pointers if inode is opened in
557 * O_APPEND mode, as this is different per fd and is checked when
558 * writing is done.
560 zone_t zone_size;
561 int scale, file_type, waspipe;
562 dev_t dev;
564 file_type = rip->i_mode & I_TYPE; /* check to see if file is special */
565 if (file_type == I_CHAR_SPECIAL || file_type == I_BLOCK_SPECIAL)
566 return EINVAL;
567 if(newsize > rip->i_sp->s_max_size) /* don't let inode grow too big */
568 return EFBIG;
570 dev = rip->i_dev; /* device on which inode resides */
571 scale = rip->i_sp->s_log_zone_size;
572 zone_size = (zone_t) rip->i_sp->s_block_size << scale;
574 /* Pipes can shrink, so adjust size to make sure all zones are removed. */
575 waspipe = rip->i_pipe == I_PIPE; /* TRUE if this was a pipe */
576 if (waspipe) {
577 if(newsize != 0)
578 return EINVAL; /* Only truncate pipes to 0. */
579 rip->i_size = PIPE_SIZE(rip->i_sp->s_block_size);
582 /* Free the actual space if relevant. */
583 if(newsize < rip->i_size)
584 freesp_inode(rip, newsize, rip->i_size);
586 /* Next correct the inode size. */
587 if(!waspipe) rip->i_size = newsize;
588 else wipe_inode(rip); /* Pipes can only be truncated to 0. */
589 rip->i_update |= CTIME | MTIME;
590 rip->i_dirt = DIRTY;
592 return OK;
595 /*===========================================================================*
596 * freesp_inode *
597 *===========================================================================*/
598 PUBLIC int freesp_inode(rip, start, end)
599 register struct inode *rip; /* pointer to inode to be partly freed */
600 off_t start, end; /* range of bytes to free (end uninclusive) */
602 /* Cut an arbitrary hole in an inode. The caller is responsible for checking
603 * the reasonableness of the inode type of rip. The reason is this is that
604 * this function can be called for different reasons, for which different
605 * sets of inode types are reasonable. Adjusting the final size of the inode
606 * is to be done by the caller too, if wished.
608 * Consumers of this function currently are truncate_inode() (used to
609 * free indirect and data blocks for any type of inode, but also to
610 * implement the ftruncate() and truncate() system calls) and the F_FREESP
611 * fcntl().
613 off_t p, e;
614 int zone_size, dev;
616 if(end > rip->i_size) /* freeing beyond end makes no sense */
617 end = rip->i_size;
618 if(end <= start) /* end is uninclusive, so start<end */
619 return EINVAL;
620 zone_size = rip->i_sp->s_block_size << rip->i_sp->s_log_zone_size;
621 dev = rip->i_dev; /* device on which inode resides */
623 /* If freeing doesn't cross a zone boundary, then we may only zero
624 * a range of the block.
626 if(start/zone_size == (end-1)/zone_size) {
627 zeroblock_range(rip, start, end-start);
628 } else {
629 /* First zero unused part of partly used blocks. */
630 if(start%zone_size)
631 zeroblock_half(rip, start, LAST_HALF);
632 if(end%zone_size && end < rip->i_size)
633 zeroblock_half(rip, end, FIRST_HALF);
636 /* Now completely free the completely unused blocks.
637 * write_map() will free unused (double) indirect
638 * blocks too. Converting the range to zone numbers avoids
639 * overflow on p when doing e.g. 'p += zone_size'.
641 e = end/zone_size;
642 if(end == rip->i_size && (end % zone_size)) e++;
643 for(p = nextblock(start, zone_size)/zone_size; p < e; p ++)
644 write_map(rip, p*zone_size, NO_ZONE, WMAP_FREE);
646 rip->i_update |= CTIME | MTIME;
647 rip->i_dirt = DIRTY;
649 return OK;
652 /*===========================================================================*
653 * nextblock *
654 *===========================================================================*/
655 PRIVATE off_t nextblock(pos, zone_size)
656 off_t pos;
657 int zone_size;
659 /* Return the first position in the next block after position 'pos'
660 * (unless this is the first position in the current block).
661 * This can be done in one expression, but that can overflow pos.
663 off_t p;
664 p = (pos/zone_size)*zone_size;
665 if((pos % zone_size)) p += zone_size; /* Round up. */
666 return p;
669 /*===========================================================================*
670 * zeroblock_half *
671 *===========================================================================*/
672 PRIVATE void zeroblock_half(rip, pos, half)
673 struct inode *rip;
674 off_t pos;
675 int half;
677 /* Zero the upper or lower 'half' of a block that holds position 'pos'.
678 * half can be FIRST_HALF or LAST_HALF.
680 * FIRST_HALF: 0..pos-1 will be zeroed
681 * LAST_HALF: pos..blocksize-1 will be zeroed
683 int offset, len;
685 /* Offset of zeroing boundary. */
686 offset = pos % rip->i_sp->s_block_size;
688 if(half == LAST_HALF) {
689 len = rip->i_sp->s_block_size - offset;
690 } else {
691 len = offset;
692 pos -= offset;
693 offset = 0;
696 zeroblock_range(rip, pos, len);
699 /*===========================================================================*
700 * zeroblock_range *
701 *===========================================================================*/
702 PRIVATE void zeroblock_range(rip, pos, len)
703 struct inode *rip;
704 off_t pos;
705 off_t len;
707 /* Zero a range in a block.
708 * This function is used to zero a segment of a block, either
709 * FIRST_HALF of LAST_HALF.
712 block_t b;
713 struct buf *bp;
714 off_t offset;
716 if(!len) return; /* no zeroing to be done. */
717 if( (b = read_map(rip, pos)) == NO_BLOCK) return;
718 if( (bp = get_block(rip->i_dev, b, NORMAL)) == NIL_BUF)
719 panic(__FILE__, "zeroblock_range: no block", NO_NUM);
720 offset = pos % rip->i_sp->s_block_size;
721 if(offset + len > rip->i_sp->s_block_size)
722 panic(__FILE__, "zeroblock_range: len too long", len);
723 memset(bp->b_data + offset, 0, len);
724 bp->b_dirt = DIRTY;
725 put_block(bp, FULL_DATA_BLOCK);