8 #include <minix/vfsif.h>
13 static int freesp_inode(struct inode
*rip
, off_t st
, off_t end
);
14 static int remove_dir(struct inode
*rldirp
, struct inode
*rip
, char
15 dir_name
[MFS_NAME_MAX
]);
16 static int unlink_file(struct inode
*dirp
, struct inode
*rip
, char
17 file_name
[MFS_NAME_MAX
]);
18 static off_t
nextblock(off_t pos
, int zone_size
);
19 static void zerozone_half(struct inode
*rip
, off_t pos
, int half
, int
21 static void zerozone_range(struct inode
*rip
, off_t pos
, off_t len
);
23 /* Args to zerozone_half() */
28 /*===========================================================================*
30 *===========================================================================*/
33 /* Perform the link(name1, name2) system call. */
35 struct inode
*ip
, *rip
;
37 char string
[MFS_NAME_MAX
];
41 len
= min( (unsigned) fs_m_in
.REQ_PATH_LEN
, sizeof(string
));
42 /* Copy the link name's last component */
43 r
= sys_safecopyfrom(VFS_PROC_NR
, (cp_grant_id_t
) fs_m_in
.REQ_GRANT
,
44 (vir_bytes
) 0, (vir_bytes
) string
, (size_t) len
);
45 if (r
!= OK
) return r
;
46 NUL(string
, len
, sizeof(string
));
48 /* Temporarily open the file. */
49 if( (rip
= get_inode(fs_dev
, (ino_t
) fs_m_in
.REQ_INODE_NR
)) == NULL
)
52 /* Check to see if the file has maximum number of links already. */
54 if(rip
->i_nlinks
>= LINK_MAX
)
57 /* Only super_user may link to directories. */
59 if( (rip
->i_mode
& I_TYPE
) == I_DIRECTORY
&& caller_uid
!= SU_UID
)
62 /* If error with 'name', return the inode. */
68 /* Temporarily open the last dir */
69 if( (ip
= get_inode(fs_dev
, (ino_t
) fs_m_in
.REQ_DIR_INO
)) == NULL
) {
74 if (ip
->i_nlinks
== NO_LINK
) { /* Dir does not actually exist */
80 /* If 'name2' exists in full (even if no space) set 'r' to error. */
81 if((new_ip
= advance(ip
, string
, IGN_PERM
)) == NULL
) {
92 r
= search_dir(ip
, string
, &rip
->i_num
, ENTER
, IGN_PERM
);
94 /* If success, register the linking. */
97 rip
->i_update
|= CTIME
;
101 /* Done. Release both inodes. */
108 /*===========================================================================*
110 *===========================================================================*/
113 /* Perform the unlink(name) or rmdir(name) system call. The code for these two
114 * is almost the same. They differ only in some condition testing. Unlink()
115 * may be used by the superuser to do dangerous things; rmdir() may not.
117 register struct inode
*rip
;
118 struct inode
*rldirp
;
120 char string
[MFS_NAME_MAX
];
123 /* Copy the last component */
124 len
= min( (unsigned) fs_m_in
.REQ_PATH_LEN
, sizeof(string
));
125 r
= sys_safecopyfrom(VFS_PROC_NR
, (cp_grant_id_t
) fs_m_in
.REQ_GRANT
,
126 (vir_bytes
) 0, (vir_bytes
) string
, (size_t) len
);
127 if (r
!= OK
) return r
;
128 NUL(string
, len
, sizeof(string
));
130 /* Temporarily open the dir. */
131 if( (rldirp
= get_inode(fs_dev
, (ino_t
) fs_m_in
.REQ_INODE_NR
)) == NULL
)
134 /* The last directory exists. Does the file also exist? */
135 rip
= advance(rldirp
, string
, IGN_PERM
);
138 /* If error, return inode. */
141 if (r
== EENTERMOUNT
|| r
== ELEAVEMOUNT
) {
149 if(rip
->i_sp
->s_rd_only
) {
151 } else if(fs_m_in
.m_type
== REQ_UNLINK
) {
152 /* Now test if the call is allowed, separately for unlink() and rmdir(). */
153 /* Only the su may unlink directories, but the su can unlink any
155 if( (rip
->i_mode
& I_TYPE
) == I_DIRECTORY
) r
= EPERM
;
157 /* Actually try to unlink the file; fails if parent is mode 0 etc. */
158 if (r
== OK
) r
= unlink_file(rldirp
, rip
, string
);
160 r
= remove_dir(rldirp
, rip
, string
); /* call is RMDIR */
163 /* If unlink was possible, it has been done, otherwise it has not. */
170 /*===========================================================================*
172 *===========================================================================*/
175 block_t b
; /* block containing link text */
176 struct buf
*bp
; /* buffer containing link text */
177 register struct inode
*rip
; /* target inode */
178 register int r
; /* return value */
181 copylen
= min( (size_t) fs_m_in
.REQ_MEM_SIZE
, UMAX_FILE_POS
);
183 /* Temporarily open the file. */
184 if( (rip
= get_inode(fs_dev
, (ino_t
) fs_m_in
.REQ_INODE_NR
)) == NULL
)
187 if(!S_ISLNK(rip
->i_mode
))
189 else if ((b
= read_map(rip
, (off_t
) 0)) == NO_BLOCK
)
192 /* Passed all checks */
193 /* We can safely cast to unsigned, because copylen is guaranteed to be
194 below max file size */
195 copylen
= min( copylen
, (unsigned) rip
->i_size
);
196 bp
= get_block(rip
->i_dev
, b
, NORMAL
);
197 r
= sys_safecopyto(VFS_PROC_NR
, (cp_grant_id_t
) fs_m_in
.REQ_GRANT
,
198 (vir_bytes
) 0, (vir_bytes
) b_data(bp
),
200 put_block(bp
, DIRECTORY_BLOCK
);
202 fs_m_out
.RES_NBYTES
= copylen
;
210 /*===========================================================================*
212 *===========================================================================*/
213 static int remove_dir(rldirp
, rip
, dir_name
)
214 struct inode
*rldirp
; /* parent directory */
215 struct inode
*rip
; /* directory to be removed */
216 char dir_name
[MFS_NAME_MAX
]; /* name of directory to be removed */
218 /* A directory file has to be removed. Five conditions have to met:
219 * - The file must be a directory
220 * - The directory must be empty (except for . and ..)
221 * - The final component of the path must not be . or ..
222 * - The directory must not be the root of a mounted file system (VFS)
223 * - The directory must not be anybody's root/working directory (VFS)
227 /* search_dir checks that rip is a directory too. */
228 if ((r
= search_dir(rip
, "", NULL
, IS_EMPTY
, IGN_PERM
)) != OK
)
231 if (strcmp(dir_name
, ".") == 0 || strcmp(dir_name
, "..") == 0)return(EINVAL
);
232 if (rip
->i_num
== ROOT_INODE
) return(EBUSY
); /* can't remove 'root' */
234 /* Actually try to unlink the file; fails if parent is mode 0 etc. */
235 if ((r
= unlink_file(rldirp
, rip
, dir_name
)) != OK
) return r
;
237 /* Unlink . and .. from the dir. The super user can link and unlink any dir,
238 * so don't make too many assumptions about them.
240 (void) unlink_file(rip
, NULL
, dot1
);
241 (void) unlink_file(rip
, NULL
, dot2
);
246 /*===========================================================================*
248 *===========================================================================*/
249 static int unlink_file(dirp
, rip
, file_name
)
250 struct inode
*dirp
; /* parent directory of file */
251 struct inode
*rip
; /* inode of file, may be NULL too. */
252 char file_name
[MFS_NAME_MAX
]; /* name of file to be removed */
254 /* Unlink 'file_name'; rip must be the inode of 'file_name' or NULL. */
256 ino_t numb
; /* inode number */
259 /* If rip is not NULL, it is used to get faster access to the inode. */
261 /* Search for file in directory and try to get its inode. */
262 err_code
= search_dir(dirp
, file_name
, &numb
, LOOK_UP
, IGN_PERM
);
263 if (err_code
== OK
) rip
= get_inode(dirp
->i_dev
, (int) numb
);
264 if (err_code
!= OK
|| rip
== NULL
) return(err_code
);
266 dup_inode(rip
); /* inode will be returned with put_inode */
269 r
= search_dir(dirp
, file_name
, NULL
, DELETE
, IGN_PERM
);
272 rip
->i_nlinks
--; /* entry deleted from parent's dir */
273 rip
->i_update
|= CTIME
;
282 /*===========================================================================*
284 *===========================================================================*/
287 /* Perform the rename(name1, name2) system call. */
288 struct inode
*old_dirp
, *old_ip
; /* ptrs to old dir, file inodes */
289 struct inode
*new_dirp
, *new_ip
; /* ptrs to new dir, file inodes */
290 struct inode
*new_superdirp
, *next_new_superdirp
;
291 int r
= OK
; /* error flag; initially no error */
292 int odir
, ndir
; /* TRUE iff {old|new} file is dir */
293 int same_pdir
; /* TRUE iff parent dirs are the same */
294 char old_name
[MFS_NAME_MAX
], new_name
[MFS_NAME_MAX
];
298 /* Copy the last component of the old name */
299 len
= min( (unsigned) fs_m_in
.REQ_REN_LEN_OLD
, sizeof(old_name
));
300 r
= sys_safecopyfrom(VFS_PROC_NR
, (cp_grant_id_t
) fs_m_in
.REQ_REN_GRANT_OLD
,
301 (vir_bytes
) 0, (vir_bytes
) old_name
, (size_t) len
);
302 if (r
!= OK
) return r
;
303 NUL(old_name
, len
, sizeof(old_name
));
305 /* Copy the last component of the new name */
306 len
= min( (unsigned) fs_m_in
.REQ_REN_LEN_NEW
, sizeof(new_name
));
307 r
= sys_safecopyfrom(VFS_PROC_NR
, (cp_grant_id_t
) fs_m_in
.REQ_REN_GRANT_NEW
,
308 (vir_bytes
) 0, (vir_bytes
) new_name
, (size_t) len
);
309 if (r
!= OK
) return r
;
310 NUL(new_name
, len
, sizeof(new_name
));
312 /* Get old dir inode */
313 if( (old_dirp
= get_inode(fs_dev
, (ino_t
) fs_m_in
.REQ_REN_OLD_DIR
)) == NULL
)
316 old_ip
= advance(old_dirp
, old_name
, IGN_PERM
);
319 if (r
== EENTERMOUNT
|| r
== ELEAVEMOUNT
) {
322 if (r
== EENTERMOUNT
) r
= EXDEV
; /* should this fail at all? */
323 else if (r
== ELEAVEMOUNT
) r
= EINVAL
; /* rename on dot-dot */
326 if (old_ip
== NULL
) {
331 /* Get new dir inode */
332 if( (new_dirp
= get_inode(fs_dev
, (ino_t
) fs_m_in
.REQ_REN_NEW_DIR
)) == NULL
) {
337 if (new_dirp
->i_nlinks
== NO_LINK
) { /* Dir does not actually exist */
345 new_ip
= advance(new_dirp
, new_name
, IGN_PERM
); /* not required to exist */
347 /* However, if the check failed because the file does exist, don't continue.
348 * Note that ELEAVEMOUNT is covered by the dot-dot check later. */
349 if(err_code
== EENTERMOUNT
) {
355 odir
= ((old_ip
->i_mode
& I_TYPE
) == I_DIRECTORY
); /* TRUE iff dir */
357 /* If it is ok, check for a variety of possible errors. */
359 same_pdir
= (old_dirp
== new_dirp
);
361 /* The old inode must not be a superdirectory of the new last dir. */
362 if (odir
&& !same_pdir
) {
363 dup_inode(new_superdirp
= new_dirp
);
364 while (TRUE
) { /* may hang in a file system loop */
365 if (new_superdirp
== old_ip
) {
366 put_inode(new_superdirp
);
370 next_new_superdirp
= advance(new_superdirp
, dot2
,
373 put_inode(new_superdirp
);
374 if(next_new_superdirp
== new_superdirp
) {
375 put_inode(new_superdirp
);
378 if(err_code
== ELEAVEMOUNT
) {
379 /* imitate that we are back at the root,
380 * cross device checked already on VFS */
381 put_inode(next_new_superdirp
);
385 new_superdirp
= next_new_superdirp
;
386 if(new_superdirp
== NULL
) {
387 /* Missing ".." entry. Assume the worst. */
394 /* The old or new name must not be . or .. */
395 if(strcmp(old_name
, ".") == 0 || strcmp(old_name
, "..") == 0 ||
396 strcmp(new_name
, ".") == 0 || strcmp(new_name
, "..") == 0) {
399 /* Both parent directories must be on the same device.
400 if(old_dirp->i_dev != new_dirp->i_dev) r = EXDEV; */
402 /* Some tests apply only if the new path exists. */
404 /* don't rename a file with a file system mounted on it.
405 if (old_ip->i_dev != old_dirp->i_dev) r = EXDEV;*/
406 if (odir
&& new_dirp
->i_nlinks
>= LINK_MAX
&&
407 !same_pdir
&& r
== OK
) {
411 if(old_ip
== new_ip
) r
= SAME
; /* old=new */
413 ndir
= ((new_ip
->i_mode
& I_TYPE
) == I_DIRECTORY
);/* dir ? */
414 if(odir
== TRUE
&& ndir
== FALSE
) r
= ENOTDIR
;
415 if(odir
== FALSE
&& ndir
== TRUE
) 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. */
425 /* The rename will probably work. Only two things can go wrong now:
426 * 1. being unable to remove the new file. (when new file already exists)
427 * 2. being unable to make the new directory entry. (new file doesn't exists)
428 * [directory has to grow by one block and cannot because the disk
429 * is completely full].
433 /* There is already an entry for 'new'. Try to remove it. */
435 r
= remove_dir(new_dirp
, new_ip
, new_name
);
437 r
= unlink_file(new_dirp
, new_ip
, new_name
);
439 /* if r is OK, the rename will succeed, while there is now an
440 * unused entry in the new parent directory. */
444 /* If the new name will be in the same parent directory as the old
445 * one, first remove the old name to free an entry for the new name,
446 * otherwise first try to create the new name entry to make sure
447 * the rename will succeed.
449 numb
= old_ip
->i_num
; /* inode number of old file */
452 r
= search_dir(old_dirp
, old_name
, NULL
, DELETE
, IGN_PERM
);
453 /* shouldn't go wrong. */
455 (void) search_dir(old_dirp
, new_name
, &numb
, ENTER
,
458 r
= search_dir(new_dirp
, new_name
, &numb
, ENTER
, IGN_PERM
);
460 (void) search_dir(old_dirp
, old_name
, NULL
, DELETE
,
464 /* If r is OK, the ctime and mtime of old_dirp and new_dirp have been marked
465 * 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
, NULL
, dot2
);
471 if(search_dir(old_ip
, dot2
, &numb
, ENTER
, IGN_PERM
) == OK
) {
472 /* New link created. */
473 new_dirp
->i_nlinks
++;
474 IN_MARKDIRTY(new_dirp
);
478 /* Release the inodes. */
483 return(r
== SAME
? OK
: r
);
487 /*===========================================================================*
489 *===========================================================================*/
496 if( (rip
= find_inode(fs_dev
, (ino_t
) fs_m_in
.REQ_INODE_NR
)) == NULL
)
499 if(rip
->i_sp
->s_rd_only
) {
502 start
= fs_m_in
.REQ_TRC_START_LO
;
503 end
= fs_m_in
.REQ_TRC_END_LO
;
506 r
= truncate_inode(rip
, start
);
508 r
= freesp_inode(rip
, start
, end
);
515 /*===========================================================================*
517 *===========================================================================*/
518 int truncate_inode(rip
, newsize
)
519 register struct inode
*rip
; /* pointer to inode to be truncated */
520 off_t newsize
; /* inode must become this size */
522 /* Set inode to a certain size, freeing any zones no longer referenced
523 * and updating the size in the inode. If the inode is extended, the
524 * extra space is a hole that reads as zeroes.
526 * Nothing special has to happen to file pointers if inode is opened in
527 * O_APPEND mode, as this is different per fd and is checked when
533 file_type
= rip
->i_mode
& I_TYPE
; /* check to see if file is special */
534 if (file_type
== I_CHAR_SPECIAL
|| file_type
== I_BLOCK_SPECIAL
)
536 if (newsize
> rip
->i_sp
->s_max_size
) /* don't let inode grow too big */
539 /* Free the actual space if truncating. */
540 if (newsize
< rip
->i_size
) {
541 if ((r
= freesp_inode(rip
, newsize
, rip
->i_size
)) != OK
)
545 /* Clear the rest of the last zone if expanding. */
546 if (newsize
> rip
->i_size
) clear_zone(rip
, rip
->i_size
, 0);
548 /* Next correct the inode size. */
549 rip
->i_size
= newsize
;
550 rip
->i_update
|= CTIME
| MTIME
;
557 /*===========================================================================*
559 *===========================================================================*/
560 static int freesp_inode(rip
, start
, end
)
561 register struct inode
*rip
; /* pointer to inode to be partly freed */
562 off_t start
, end
; /* range of bytes to free (end uninclusive) */
564 /* Cut an arbitrary hole in an inode. The caller is responsible for checking
565 * the reasonableness of the inode type of rip. The reason is this is that
566 * this function can be called for different reasons, for which different
567 * sets of inode types are reasonable. Adjusting the final size of the inode
568 * is to be done by the caller too, if wished.
570 * Consumers of this function currently are truncate_inode() (used to
571 * free indirect and data blocks for any type of inode, but also to
572 * implement the ftruncate() and truncate() system calls) and the F_FREESP
577 int zero_last
, zero_first
;
579 if(end
> rip
->i_size
) /* freeing beyond end makes no sense */
581 if(end
<= start
) /* end is uninclusive, so start<end */
584 zone_size
= rip
->i_sp
->s_block_size
<< rip
->i_sp
->s_log_zone_size
;
586 /* If freeing doesn't cross a zone boundary, then we may only zero
587 * a range of the zone, unless we are freeing up that entire zone.
589 zero_last
= start
% zone_size
;
590 zero_first
= end
% zone_size
&& end
< rip
->i_size
;
591 if(start
/zone_size
== (end
-1)/zone_size
&& (zero_last
|| zero_first
)) {
592 zerozone_range(rip
, start
, end
-start
);
594 /* First zero unused part of partly used zones. */
596 zerozone_half(rip
, start
, LAST_HALF
, zone_size
);
598 zerozone_half(rip
, end
, FIRST_HALF
, zone_size
);
600 /* Now completely free the completely unused zones.
601 * write_map() will free unused (double) indirect
602 * blocks too. Converting the range to zone numbers avoids
603 * overflow on p when doing e.g. 'p += zone_size'.
606 if(end
== rip
->i_size
&& (end
% zone_size
)) e
++;
607 for(p
= nextblock(start
, zone_size
)/zone_size
; p
< e
; p
++) {
608 if((r
= write_map(rip
, p
*zone_size
, NO_ZONE
, WMAP_FREE
)) != OK
)
614 rip
->i_update
|= CTIME
| MTIME
;
621 /*===========================================================================*
623 *===========================================================================*/
624 static off_t
nextblock(pos
, zone_size
)
628 /* Return the first position in the next block after position 'pos'
629 * (unless this is the first position in the current block).
630 * This can be done in one expression, but that can overflow pos.
633 p
= (pos
/zone_size
)*zone_size
;
634 if((pos
% zone_size
)) p
+= zone_size
; /* Round up. */
639 /*===========================================================================*
641 *===========================================================================*/
642 static void zerozone_half(rip
, pos
, half
, zone_size
)
648 /* Zero the upper or lower 'half' of a zone that holds position 'pos'.
649 * half can be FIRST_HALF or LAST_HALF.
651 * FIRST_HALF: 0..pos-1 will be zeroed
652 * LAST_HALF: pos..zone_size-1 will be zeroed
656 /* Offset of zeroing boundary. */
657 offset
= pos
% zone_size
;
659 if(half
== LAST_HALF
) {
660 len
= zone_size
- offset
;
666 zerozone_range(rip
, pos
, len
);
670 /*===========================================================================*
672 *===========================================================================*/
673 static void zerozone_range(rip
, pos
, len
)
678 /* Zero an arbitrary byte range in a zone, possibly spanning multiple blocks.
683 unsigned short block_size
;
686 block_size
= rip
->i_sp
->s_block_size
;
688 if(!len
) return; /* no zeroing to be done. */
689 if( (b
= read_map(rip
, pos
)) == NO_BLOCK
) return;
691 if( (bp
= get_block(rip
->i_dev
, b
, NORMAL
)) == NULL
)
692 panic("zerozone_range: no block");
693 offset
= pos
% block_size
;
694 bytes
= block_size
- offset
;
695 if (bytes
> (size_t) len
)
697 memset(b_data(bp
) + offset
, 0, bytes
);
699 put_block(bp
, FULL_DATA_BLOCK
);