7 #include <minix/callnr.h>
13 #include <minix/vfsif.h>
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() */
30 /*===========================================================================*
32 *===========================================================================*/
35 /* Perform the link(name1, name2) system call. */
37 struct inode
*ip
, *rip
;
39 char string
[NAME_MAX
];
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
);
59 /* Check to see if the file has maximum number of links already. */
61 if (rip
->i_nlinks
>= (rip
->i_sp
->s_version
== V1
? CHAR_MAX
: SHRT_MAX
))
64 /* Only super_user may link to directories. */
66 if ( (rip
->i_mode
& I_TYPE
) == I_DIRECTORY
&& caller_uid
!= SU_UID
)
69 /* If error with 'name', return the inode. */
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
);
81 /* If 'name2' exists in full (even if no space) set 'r' to error. */
83 if ( (new_ip
= advance(&ip
, string
)) == NIL_INODE
) {
85 if (r
== ENOENT
) r
= OK
;
94 r
= search_dir(ip
, string
, &rip
->i_num
, ENTER
);
96 /* If success, register the linking. */
99 rip
->i_update
|= CTIME
;
103 /* Done. Release both inodes. */
110 /*===========================================================================*
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
;
122 char string
[NAME_MAX
];
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
) {
140 /* The last directory exists. Does the file also exist? */
142 if ( (rip
= advance(&rldirp
, string
)) == NIL_INODE
) r
= err_code
;
144 /* If error, return inode. */
147 if (r
== EENTERMOUNT
|| r
== ELEAVEMOUNT
)
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
);
167 r
= remove_dir(rldirp
, rip
, string
); /* call is RMDIR */
170 /* If unlink was possible, it has been done, otherwise it has not. */
178 /*===========================================================================*
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 */
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
) {
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
;
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
);
219 /*===========================================================================*
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)
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
);
254 /*===========================================================================*
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 */
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
);
274 dup_inode(rip
); /* inode will be returned with put_inode */
277 r
= search_dir(dirp
, file_name
, (ino_t
*) 0, DELETE
);
280 rip
->i_nlinks
--; /* entry deleted from parent's dir */
281 rip
->i_update
|= CTIME
;
290 /*===========================================================================*
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
];
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
)
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
)
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. */
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
);
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);
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;*/
366 new_superdirp
= next_new_superdirp
;
367 if (new_superdirp
== NIL_INODE
) {
368 /* Missing ".." entry. Assume the worst. */
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) {
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
) {
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
) {
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
) {
413 if (odir
== FALSE
&& ndir
== TRUE
) {
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].
433 if (new_ip
!= NIL_INODE
) {
434 /* There is already an entry for 'new'. Try to remove it. */
436 r
= remove_dir(new_dirp
, new_ip
, new_name
);
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.
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 */
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
);
458 r
= search_dir(new_dirp
, new_name
, &numb
, ENTER
);
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. */
483 return(r
== SAME
? OK
: r
);
487 /*===========================================================================*
489 *===========================================================================*/
490 PUBLIC
int fs_trunc()
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
);
504 if ( (rip
->i_mode
& I_TYPE
) != I_REGULAR
)
507 r
= truncate_inode(rip
, fs_m_in
.REQ_LENGTH
);
514 /*===========================================================================*
516 *===========================================================================*/
517 PUBLIC
int fs_ftrunc(void)
523 if ( (rip
= find_inode(fs_dev
, fs_m_in
.REQ_FD_INODE_NR
))
525 printf("FSfreesp: couldn't find inode %d\n",
526 fs_m_in
.REQ_FD_INODE_NR
);
530 start
= fs_m_in
.REQ_FD_START
;
531 end
= fs_m_in
.REQ_FD_END
;
534 r
= truncate_inode(rip
, start
);
537 r
= freesp_inode(rip
, start
, end
);
545 /*===========================================================================*
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
561 int scale
, file_type
, waspipe
;
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
)
567 if(newsize
> rip
->i_sp
->s_max_size
) /* don't let inode grow too big */
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 */
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
;
595 /*===========================================================================*
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
616 if(end
> rip
->i_size
) /* freeing beyond end makes no sense */
618 if(end
<= start
) /* end is uninclusive, so start<end */
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
);
629 /* First zero unused part of partly used blocks. */
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'.
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
;
652 /*===========================================================================*
654 *===========================================================================*/
655 PRIVATE off_t
nextblock(pos
, 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.
664 p
= (pos
/zone_size
)*zone_size
;
665 if((pos
% zone_size
)) p
+= zone_size
; /* Round up. */
669 /*===========================================================================*
671 *===========================================================================*/
672 PRIVATE
void zeroblock_half(rip
, pos
, 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
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
;
696 zeroblock_range(rip
, pos
, len
);
699 /*===========================================================================*
701 *===========================================================================*/
702 PRIVATE
void zeroblock_range(rip
, pos
, 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.
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
);
725 put_block(bp
, FULL_DATA_BLOCK
);