1 /* Created (MFS based):
2 * February 2010 (Evgeniy Ivanov)
12 #include <minix/vfsif.h>
13 #include <sys/param.h>
17 static int freesp_inode(struct inode
*rip
, off_t st
, off_t end
);
18 static int remove_dir(struct inode
*rldirp
, struct inode
*rip
,
19 const char *dir_name
);
20 static int unlink_file(struct inode
*dirp
, struct inode
*rip
,
21 const char *file_name
);
22 static off_t
nextblock(off_t pos
, int blocksize
);
23 static void zeroblock_half(struct inode
*i
, off_t p
, int l
);
24 static void zeroblock_range(struct inode
*i
, off_t p
, off_t h
);
26 /* Args to zeroblock_half() */
31 /*===========================================================================*
33 *===========================================================================*/
34 int fs_link(ino_t dir_nr
, char *name
, ino_t ino_nr
)
36 /* Perform the link(name1, name2) system call. */
38 struct inode
*ip
, *rip
;
42 /* Temporarily open the file. */
43 if( (rip
= get_inode(fs_dev
, ino_nr
)) == NULL
)
46 /* Check to see if the file has maximum number of links already. */
48 if (rip
->i_links_count
>= USHRT_MAX
)
50 if(rip
->i_links_count
>= LINK_MAX
)
53 /* Linking to directories is too dangerous to allow. */
55 if( (rip
->i_mode
& I_TYPE
) == I_DIRECTORY
)
58 /* If error with 'name', return the inode. */
64 /* Temporarily open the last dir */
65 if( (ip
= get_inode(fs_dev
, dir_nr
)) == NULL
) {
70 if (ip
->i_links_count
== NO_LINK
) { /* Dir does not actually exist */
76 /* If 'name2' exists in full (even if no space) set 'r' to error. */
77 if ((new_ip
= advance(ip
, name
)) == NULL
) {
88 r
= search_dir(ip
, name
, &rip
->i_num
, ENTER
, rip
->i_mode
& I_TYPE
);
90 /* If success, register the linking. */
93 rip
->i_update
|= CTIME
;
94 rip
->i_dirt
= IN_DIRTY
;
97 /* Done. Release both inodes. */
104 /*===========================================================================*
106 *===========================================================================*/
107 int fs_unlink(ino_t dir_nr
, char *name
, int call
)
109 /* Perform the unlink(name) or rmdir(name) system call. The code for these two
110 * is almost the same. They differ only in some condition testing.
112 register struct inode
*rip
;
113 struct inode
*rldirp
;
116 /* Temporarily open the dir. */
117 if((rldirp
= get_inode(fs_dev
, dir_nr
)) == NULL
)
120 /* The last directory exists. Does the file also exist? */
121 rip
= advance(rldirp
, name
);
124 /* If error, return inode. */
129 if (rip
->i_mountpoint
) {
135 /* Now test if the call is allowed, separately for unlink() and rmdir(). */
136 if (call
== FSC_UNLINK
) {
137 if( (rip
->i_mode
& I_TYPE
) == I_DIRECTORY
) r
= EPERM
;
139 /* Actually try to unlink the file; fails if parent is mode 0 etc. */
140 if (r
== OK
) r
= unlink_file(rldirp
, rip
, name
);
142 r
= remove_dir(rldirp
, rip
, name
); /* call is RMDIR */
145 /* If unlink was possible, it has been done, otherwise it has not. */
152 /*===========================================================================*
154 *===========================================================================*/
155 ssize_t
fs_rdlink(ino_t ino_nr
, struct fsdriver_data
*data
, size_t bytes
)
157 struct buf
*bp
= NULL
; /* buffer containing link text */
158 char* link_text
; /* either bp->b_data or rip->i_block */
159 register struct inode
*rip
; /* target inode */
160 register int r
; /* return value */
162 /* Temporarily open the file. */
163 if( (rip
= get_inode(fs_dev
, ino_nr
)) == NULL
)
166 if (rip
->i_size
>= MAX_FAST_SYMLINK_LENGTH
) {
168 if(!(bp
= get_block_map(rip
, 0))) {
171 link_text
= b_data(bp
);
175 /* fast symlink, stored in inode */
176 link_text
= (char*) rip
->i_block
;
180 /* Passed all checks */
181 if (bytes
> rip
->i_size
)
183 r
= fsdriver_copyout(data
, 0, link_text
, bytes
);
194 /*===========================================================================*
196 *===========================================================================*/
197 static int remove_dir(rldirp
, rip
, dir_name
)
198 struct inode
*rldirp
; /* parent directory */
199 struct inode
*rip
; /* directory to be removed */
200 const char *dir_name
; /* name of directory to be removed */
202 /* A directory file has to be removed. Five conditions have to met:
203 * - The file must be a directory
204 * - The directory must be empty (except for . and ..)
205 * - The final component of the path must not be . or ..
206 * - The directory must not be the root of a mounted file system (VFS)
207 * - The directory must not be anybody's root/working directory (VFS)
211 /* search_dir checks that rip is a directory too. */
212 if ((r
= search_dir(rip
, "", NULL
, IS_EMPTY
, 0)) != OK
)
215 if (rip
->i_num
== ROOT_INODE
) return(EBUSY
); /* can't remove 'root' */
217 /* Actually try to unlink the file; fails if parent is mode 0 etc. */
218 if ((r
= unlink_file(rldirp
, rip
, dir_name
)) != OK
) return r
;
220 /* Unlink . and .. from the dir. The super user can link and unlink any dir,
221 * so don't make too many assumptions about them.
223 (void) unlink_file(rip
, NULL
, ".");
224 (void) unlink_file(rip
, NULL
, "..");
229 /*===========================================================================*
231 *===========================================================================*/
232 static int unlink_file(dirp
, rip
, file_name
)
233 struct inode
*dirp
; /* parent directory of file */
234 struct inode
*rip
; /* inode of file, may be NULL too. */
235 const char *file_name
; /* name of file to be removed */
237 /* Unlink 'file_name'; rip must be the inode of 'file_name' or NULL. */
239 ino_t numb
; /* inode number */
242 /* If rip is not NULL, it is used to get faster access to the inode. */
244 /* Search for file in directory and try to get its inode. */
245 err_code
= search_dir(dirp
, file_name
, &numb
, LOOK_UP
, 0);
246 if (err_code
== OK
) rip
= get_inode(dirp
->i_dev
, (int) numb
);
247 if (err_code
!= OK
|| rip
== NULL
) return(err_code
);
249 dup_inode(rip
); /* inode will be returned with put_inode */
252 r
= search_dir(dirp
, file_name
, NULL
, DELETE
, 0);
255 rip
->i_links_count
--; /* entry deleted from parent's dir */
256 rip
->i_update
|= CTIME
;
257 rip
->i_dirt
= IN_DIRTY
;
265 /*===========================================================================*
267 *===========================================================================*/
268 int fs_rename(ino_t old_dir_nr
, char *old_name
, ino_t new_dir_nr
,
271 /* Perform the rename(name1, name2) system call. */
272 struct inode
*old_dirp
, *old_ip
; /* ptrs to old dir, file inodes */
273 struct inode
*new_dirp
, *new_ip
; /* ptrs to new dir, file inodes */
274 struct inode
*new_superdirp
, *next_new_superdirp
;
275 int r
= OK
; /* error flag; initially no error */
276 int odir
, ndir
; /* TRUE iff {old|new} file is dir */
277 int same_pdir
= 0; /* TRUE iff parent dirs are the same */
280 /* Get old dir inode */
281 if( (old_dirp
= get_inode(fs_dev
, old_dir_nr
)) == NULL
)
284 old_ip
= advance(old_dirp
, old_name
);
287 if (old_ip
== NULL
) {
292 if (old_ip
->i_mountpoint
) {
298 /* Get new dir inode */
299 if ((new_dirp
= get_inode(fs_dev
, new_dir_nr
)) == NULL
){
304 if (new_dirp
->i_links_count
== NO_LINK
) { /* Dir does not exist */
312 new_ip
= advance(new_dirp
, new_name
); /* not required to exist */
314 /* If the node does exist, make sure it's not a mountpoint. */
315 if (new_ip
!= NULL
&& new_ip
->i_mountpoint
) {
322 odir
= ((old_ip
->i_mode
& I_TYPE
) == I_DIRECTORY
); /* TRUE iff dir */
326 /* If it is ok, check for a variety of possible errors. */
328 same_pdir
= (old_dirp
== new_dirp
);
330 /* The old inode must not be a superdirectory of the new last dir. */
331 if (odir
&& !same_pdir
) {
332 dup_inode(new_superdirp
= new_dirp
);
333 while (TRUE
) { /* may hang in a file system loop */
334 if (new_superdirp
== old_ip
) {
335 put_inode(new_superdirp
);
339 next_new_superdirp
= advance(new_superdirp
, "..");
341 put_inode(new_superdirp
);
342 if(next_new_superdirp
== new_superdirp
) {
343 put_inode(new_superdirp
);
346 if(next_new_superdirp
->i_num
== ROOT_INODE
) {
347 /* imitate that we are back at the root,
348 * cross device checked already on VFS */
349 put_inode(next_new_superdirp
);
353 new_superdirp
= next_new_superdirp
;
354 if(new_superdirp
== NULL
) {
355 /* Missing ".." entry. Assume the worst. */
362 /* Some tests apply only if the new path exists. */
364 if(odir
&& (new_dirp
->i_links_count
>= SHRT_MAX
||
365 new_dirp
->i_links_count
>= LINK_MAX
) &&
366 !same_pdir
&& r
== OK
) {
370 if(old_ip
== new_ip
) r
= SAME
; /* old=new */
372 ndir
= ((new_ip
->i_mode
& I_TYPE
) == I_DIRECTORY
);/* dir ? */
373 if(odir
== TRUE
&& ndir
== FALSE
) r
= ENOTDIR
;
374 if(odir
== FALSE
&& ndir
== TRUE
) r
= EISDIR
;
378 /* If a process has another root directory than the system root, we might
379 * "accidently" be moving it's working directory to a place where it's
380 * root directory isn't a super directory of it anymore. This can make
381 * the function chroot useless. If chroot will be used often we should
382 * probably check for it here. */
384 /* The rename will probably work. Only two things can go wrong now:
385 * 1. being unable to remove the new file. (when new file already exists)
386 * 2. being unable to make the new directory entry. (new file doesn't exists)
387 * [directory has to grow by one block and cannot because the disk
388 * is completely full].
392 /* There is already an entry for 'new'. Try to remove it. */
394 r
= remove_dir(new_dirp
, new_ip
, new_name
);
396 r
= unlink_file(new_dirp
, new_ip
, new_name
);
398 /* if r is OK, the rename will succeed, while there is now an
399 * unused entry in the new parent directory. */
403 /* If the new name will be in the same parent directory as the old
404 * one, first remove the old name to free an entry for the new name,
405 * otherwise first try to create the new name entry to make sure
406 * the rename will succeed.
408 numb
= old_ip
->i_num
; /* inode number of old file */
411 r
= search_dir(old_dirp
,old_name
, NULL
, DELETE
, 0);
412 /* shouldn't go wrong. */
414 (void) search_dir(old_dirp
, new_name
, &numb
, ENTER
,
415 old_ip
->i_mode
& I_TYPE
);
417 r
= search_dir(new_dirp
, new_name
, &numb
, ENTER
,
418 old_ip
->i_mode
& I_TYPE
);
420 (void) search_dir(old_dirp
, old_name
, NULL
,
425 /* If r is OK, the ctime and mtime of old_dirp and new_dirp have been marked
426 * for update in search_dir. */
428 if(r
== OK
&& odir
&& !same_pdir
) {
429 /* Update the .. entry in the directory (still points to old_dirp).*/
430 numb
= new_dirp
->i_num
;
431 (void) unlink_file(old_ip
, NULL
, "..");
432 if(search_dir(old_ip
, "..", &numb
, ENTER
, I_DIRECTORY
) == OK
) {
433 /* New link created. */
434 new_dirp
->i_links_count
++;
435 new_dirp
->i_dirt
= IN_DIRTY
;
439 /* Release the inodes. */
444 return(r
== SAME
? OK
: r
);
448 /*===========================================================================*
450 *===========================================================================*/
451 int fs_trunc(ino_t ino_nr
, off_t start
, off_t end
)
456 if( (rip
= find_inode(fs_dev
, ino_nr
)) == NULL
)
460 r
= truncate_inode(rip
, start
);
462 r
= freesp_inode(rip
, start
, end
);
468 /*===========================================================================*
470 *===========================================================================*/
471 int truncate_inode(rip
, newsize
)
472 register struct inode
*rip
; /* pointer to inode to be truncated */
473 off_t newsize
; /* inode must become this size */
475 /* Set inode to a certain size, freeing any blocks no longer referenced
476 * and updating the size in the inode. If the inode is extended, the
477 * extra space is a hole that reads as zeroes.
479 * Nothing special has to happen to file pointers if inode is opened in
480 * O_APPEND mode, as this is different per fd and is checked when
486 discard_preallocated_blocks(rip
);
488 file_type
= rip
->i_mode
& I_TYPE
; /* check to see if file is special */
489 if (file_type
== I_CHAR_SPECIAL
|| file_type
== I_BLOCK_SPECIAL
)
491 if (newsize
> rip
->i_sp
->s_max_size
) /* don't let inode grow too big */
494 /* Free the actual space if truncating. */
495 if (newsize
< rip
->i_size
) {
496 if ((r
= freesp_inode(rip
, newsize
, rip
->i_size
)) != OK
)
500 /* Clear the rest of the last block if expanding. */
501 if (newsize
> rip
->i_size
) zeroblock_half(rip
, rip
->i_size
, LAST_HALF
);
503 /* Next correct the inode size. */
504 rip
->i_size
= newsize
;
505 rip
->i_update
|= CTIME
| MTIME
;
506 rip
->i_dirt
= IN_DIRTY
;
512 /*===========================================================================*
514 *===========================================================================*/
515 static int freesp_inode(rip
, start
, end
)
516 register struct inode
*rip
; /* pointer to inode to be partly freed */
517 off_t start
, end
; /* range of bytes to free (end uninclusive) */
519 /* Cut an arbitrary hole in an inode. The caller is responsible for checking
520 * the reasonableness of the inode type of rip. The reason is this is that
521 * this function can be called for different reasons, for which different
522 * sets of inode types are reasonable. Adjusting the final size of the inode
523 * is to be done by the caller too, if wished.
525 * Consumers of this function currently are truncate_inode() (used to
526 * free indirect and data blocks for any type of inode, but also to
527 * implement the ftruncate() and truncate() system calls) and the F_FREESP
532 unsigned short block_size
= rip
->i_sp
->s_block_size
;
533 int zero_last
, zero_first
;
535 discard_preallocated_blocks(rip
);
537 if (rip
->i_blocks
== 0) {
538 /* Either hole or symlink. Freeing fast symlink using
539 * write_map() causes segfaults since it doesn't use any
540 * blocks, but uses i_block[] to store target.
545 if(end
> rip
->i_size
) /* freeing beyond end makes no sense */
547 if(end
<= start
) /* end is uninclusive, so start<end */
550 /* If freeing doesn't cross a block boundary, then we may only zero
551 * a range of the block.
553 zero_last
= start
% block_size
;
554 zero_first
= end
% block_size
&& end
< rip
->i_size
;
555 if (start
/block_size
== (end
-1)/block_size
&& (zero_last
|| zero_first
)) {
556 zeroblock_range(rip
, start
, end
-start
);
558 /* First zero unused part of partly used blocks. */
560 zeroblock_half(rip
, start
, LAST_HALF
);
562 zeroblock_half(rip
, end
, FIRST_HALF
);
564 /* Now completely free the completely unused blocks.
565 * write_map() will free unused indirect
566 * blocks too. Converting the range to block numbers avoids
567 * overflow on p when doing e.g. 'p += block_size'.
569 e
= end
/ block_size
;
570 if (end
== rip
->i_size
&& (end
% block_size
))
572 for (p
= nextblock(start
, block_size
)/block_size
; p
< e
; p
++) {
573 if ((r
= write_map(rip
, p
*block_size
, NO_BLOCK
, WMAP_FREE
)) != OK
)
578 rip
->i_update
|= CTIME
| MTIME
;
579 rip
->i_dirt
= IN_DIRTY
;
585 /*===========================================================================*
587 *===========================================================================*/
588 static off_t
nextblock(pos
, block_size
)
590 unsigned short block_size
;
592 /* Return the first position in the next block after position 'pos'
593 * (unless this is the first position in the current block).
594 * This can be done in one expression, but that can overflow pos.
597 p
= (pos
/ block_size
) * block_size
;
598 if (pos
% block_size
) p
+= block_size
; /* Round up. */
603 /*===========================================================================*
605 *===========================================================================*/
606 static void zeroblock_half(rip
, pos
, half
)
611 /* Zero the upper or lower 'half' of a block that holds position 'pos'.
612 * half can be FIRST_HALF or LAST_HALF.
614 * FIRST_HALF: 0..pos-1 will be zeroed
615 * LAST_HALF: pos..blocksize-1 will be zeroed
619 /* Offset of zeroing boundary. */
620 offset
= pos
% rip
->i_sp
->s_block_size
;
622 if(half
== LAST_HALF
) {
623 len
= rip
->i_sp
->s_block_size
- offset
;
629 zeroblock_range(rip
, pos
, len
);
633 /*===========================================================================*
635 *===========================================================================*/
636 static void zeroblock_range(rip
, pos
, len
)
641 /* Zero a range in a block.
642 * This function is used to zero a segment of a block.
647 if (!len
) return; /* no zeroing to be done. */
648 if (!(bp
= get_block_map(rip
, rounddown(pos
, rip
->i_sp
->s_block_size
))))
649 return; /* skip holes */
650 offset
= pos
% rip
->i_sp
->s_block_size
;
651 if (offset
+ len
> rip
->i_sp
->s_block_size
)
652 panic("zeroblock_range: len too long: %lld", len
);
653 memset(b_data(bp
) + offset
, 0, len
);