1 /* Created (MFS based):
2 * February 2010 (Evgeniy Ivanov)
12 #include <minix/vfsif.h>
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() */
30 /*===========================================================================*
32 *===========================================================================*/
35 /* Perform the link(name1, name2) system call. */
37 struct inode
*ip
, *rip
;
39 char string
[NAME_MAX
+ 1];
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)
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
)
57 /* Check to see if the file has maximum number of links already. */
59 if (rip
->i_links_count
>= USHRT_MAX
)
61 if(rip
->i_links_count
>= LINK_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_DIR_INO
)) == NULL
) {
81 if (ip
->i_links_count
== NO_LINK
) { /* Dir does not actually exist */
87 /* If 'name2' exists in full (even if no space) set 'r' to error. */
88 if ((new_ip
= advance(ip
, string
, IGN_PERM
)) == NULL
) {
99 r
= search_dir(ip
, string
, &rip
->i_num
, ENTER
, IGN_PERM
,
100 rip
->i_mode
& I_TYPE
);
102 /* If success, register the linking. */
104 rip
->i_links_count
++;
105 rip
->i_update
|= CTIME
;
106 rip
->i_dirt
= IN_DIRTY
;
109 /* Done. Release both inodes. */
116 /*===========================================================================*
118 *===========================================================================*/
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
;
128 char string
[NAME_MAX
+ 1];
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
)
145 /* The last directory exists. Does the file also exist? */
146 rip
= advance(rldirp
, string
, IGN_PERM
);
149 /* If error, return inode. */
152 if (r
== EENTERMOUNT
|| r
== ELEAVEMOUNT
) {
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
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
);
169 r
= remove_dir(rldirp
, rip
, string
); /* call is RMDIR */
172 /* If unlink was possible, it has been done, otherwise it has not. */
179 /*===========================================================================*
181 *===========================================================================*/
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 */
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
)
197 if (rip
->i_size
>= MAX_FAST_SYMLINK_LENGTH
) {
199 if ((b
= read_map(rip
, (off_t
) 0)) == NO_BLOCK
) {
202 bp
= get_block(rip
->i_dev
, b
, NORMAL
);
204 link_text
= b_data(bp
);
211 /* fast symlink, stored in inode */
212 link_text
= (char*) rip
->i_block
;
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
,
223 put_block(bp
, DIRECTORY_BLOCK
);
225 fs_m_out
.RES_NBYTES
= copylen
;
233 /*===========================================================================*
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)
250 /* search_dir checks that rip is a directory too. */
251 if ((r
= search_dir(rip
, "", NULL
, IS_EMPTY
, IGN_PERM
, 0)) != OK
)
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
);
269 /*===========================================================================*
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 */
282 /* If rip is not NULL, it is used to get faster access to the inode. */
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
);
289 dup_inode(rip
); /* inode will be returned with put_inode */
292 r
= search_dir(dirp
, file_name
, NULL
, DELETE
, IGN_PERM
, 0);
295 rip
->i_links_count
--; /* entry deleted from parent's dir */
296 rip
->i_update
|= CTIME
;
297 rip
->i_dirt
= IN_DIRTY
;
305 /*===========================================================================*
307 *===========================================================================*/
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];
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
)
345 old_ip
= advance(old_dirp
, old_name
, IGN_PERM
);
348 if (r
== EENTERMOUNT
|| r
== ELEAVEMOUNT
) {
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
) {
357 /* Get new dir inode */
358 if( (new_dirp
= get_inode(fs_dev
, (ino_t
) fs_m_in
.REQ_REN_NEW_DIR
)) == NULL
) {
363 if (new_dirp
->i_links_count
== NO_LINK
) { /* Dir does not actually exist */
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
) {
382 odir
= ((old_ip
->i_mode
& I_TYPE
) == I_DIRECTORY
); /* TRUE iff dir */
386 /* If it is ok, check for a variety of possible errors. */
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
);
399 next_new_superdirp
= advance(new_superdirp
, dot2
,
402 put_inode(new_superdirp
);
403 if(next_new_superdirp
== new_superdirp
) {
404 put_inode(new_superdirp
);
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
);
414 new_superdirp
= next_new_superdirp
;
415 if(new_superdirp
== NULL
) {
416 /* Missing ".." entry. Assume the worst. */
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) {
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. */
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
) {
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].
463 /* There is already an entry for 'new'. Try to remove it. */
465 r
= remove_dir(new_dirp
, new_ip
, new_name
);
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. */
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 */
482 r
= search_dir(old_dirp
,old_name
, NULL
, DELETE
,IGN_PERM
, 0);
483 /* shouldn't go wrong. */
485 (void) search_dir(old_dirp
, new_name
, &numb
, ENTER
, IGN_PERM
,
486 old_ip
->i_mode
& I_TYPE
);
488 r
= search_dir(new_dirp
, new_name
, &numb
, ENTER
, IGN_PERM
,
489 old_ip
->i_mode
& I_TYPE
);
491 (void) search_dir(old_dirp
, old_name
, (ino_t
*) 0, DELETE
,
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
= IN_DIRTY
;
509 /* Release the inodes. */
514 return(r
== SAME
? OK
: r
);
518 /*===========================================================================*
520 *===========================================================================*/
527 if( (rip
= find_inode(fs_dev
, (ino_t
) fs_m_in
.REQ_INODE_NR
)) == NULL
)
530 start
= fs_m_in
.REQ_TRC_START_LO
;
531 end
= fs_m_in
.REQ_TRC_END_LO
;
534 r
= truncate_inode(rip
, start
);
536 r
= freesp_inode(rip
, start
, end
);
542 /*===========================================================================*
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
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
)
565 if (newsize
> rip
->i_sp
->s_max_size
) /* don't let inode grow too big */
568 /* Free the actual space if truncating. */
569 if (newsize
< rip
->i_size
) {
570 if ((r
= freesp_inode(rip
, newsize
, rip
->i_size
)) != OK
)
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
= IN_DIRTY
;
586 /*===========================================================================*
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
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.
619 if(end
> rip
->i_size
) /* freeing beyond end makes no sense */
621 if(end
<= start
) /* end is uninclusive, so start<end */
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
);
632 /* First zero unused part of partly used blocks. */
634 zeroblock_half(rip
, start
, LAST_HALF
);
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
))
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
)
652 rip
->i_update
|= CTIME
| MTIME
;
653 rip
->i_dirt
= IN_DIRTY
;
659 /*===========================================================================*
661 *===========================================================================*/
662 static off_t
nextblock(pos
, block_size
)
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.
671 p
= (pos
/ block_size
) * block_size
;
672 if (pos
% block_size
) p
+= block_size
; /* Round up. */
677 /*===========================================================================*
679 *===========================================================================*/
680 static void zeroblock_half(rip
, pos
, 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
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
;
703 zeroblock_range(rip
, pos
, len
);
707 /*===========================================================================*
709 *===========================================================================*/
710 static void zeroblock_range(rip
, pos
, len
)
715 /* Zero a range in a block.
716 * This function is used to zero a segment of a block.
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(b_data(bp
) + offset
, 0, len
);
731 put_block(bp
, FULL_DATA_BLOCK
);