libexec exec fix
[minix.git] / servers / mfs / link.c
blobd148d5214da86a0fc7f19e1d6f9e4ef7c2257e07
1 #include "fs.h"
2 #include <sys/stat.h>
3 #include <string.h>
4 #include <minix/com.h>
5 #include "buf.h"
6 #include "inode.h"
7 #include "super.h"
8 #include <minix/vfsif.h>
10 #define SAME 1000
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
20 zone_size);
21 static void zerozone_range(struct inode *rip, off_t pos, off_t len);
23 /* Args to zerozone_half() */
24 #define FIRST_HALF 0
25 #define LAST_HALF 1
28 /*===========================================================================*
29 * fs_link *
30 *===========================================================================*/
31 int fs_link()
33 /* Perform the link(name1, name2) system call. */
35 struct inode *ip, *rip;
36 register int r;
37 char string[MFS_NAME_MAX];
38 struct inode *new_ip;
39 phys_bytes len;
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)
50 return(EINVAL);
52 /* Check to see if the file has maximum number of links already. */
53 r = OK;
54 if(rip->i_nlinks >= LINK_MAX)
55 r = EMLINK;
57 /* Only super_user may link to directories. */
58 if(r == OK)
59 if( (rip->i_mode & I_TYPE) == I_DIRECTORY && caller_uid != SU_UID)
60 r = EPERM;
62 /* If error with 'name', return the inode. */
63 if (r != OK) {
64 put_inode(rip);
65 return(r);
68 /* Temporarily open the last dir */
69 if( (ip = get_inode(fs_dev, (ino_t) fs_m_in.REQ_DIR_INO)) == NULL) {
70 put_inode(rip);
71 return(EINVAL);
74 if (ip->i_nlinks == NO_LINK) { /* Dir does not actually exist */
75 put_inode(rip);
76 put_inode(ip);
77 return(ENOENT);
80 /* If 'name2' exists in full (even if no space) set 'r' to error. */
81 if((new_ip = advance(ip, string, IGN_PERM)) == NULL) {
82 r = err_code;
83 if(r == ENOENT)
84 r = OK;
85 } else {
86 put_inode(new_ip);
87 r = EEXIST;
90 /* Try to link. */
91 if(r == OK)
92 r = search_dir(ip, string, &rip->i_num, ENTER, IGN_PERM);
94 /* If success, register the linking. */
95 if(r == OK) {
96 rip->i_nlinks++;
97 rip->i_update |= CTIME;
98 IN_MARKDIRTY(rip);
101 /* Done. Release both inodes. */
102 put_inode(rip);
103 put_inode(ip);
104 return(r);
108 /*===========================================================================*
109 * fs_unlink *
110 *===========================================================================*/
111 int fs_unlink()
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;
119 int r;
120 char string[MFS_NAME_MAX];
121 phys_bytes len;
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)
132 return(EINVAL);
134 /* The last directory exists. Does the file also exist? */
135 rip = advance(rldirp, string, IGN_PERM);
136 r = err_code;
138 /* If error, return inode. */
139 if(r != OK) {
140 /* Mount point? */
141 if (r == EENTERMOUNT || r == ELEAVEMOUNT) {
142 put_inode(rip);
143 r = EBUSY;
145 put_inode(rldirp);
146 return(r);
149 if(rip->i_sp->s_rd_only) {
150 r = EROFS;
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
154 * dir.*/
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);
159 } else {
160 r = remove_dir(rldirp, rip, string); /* call is RMDIR */
163 /* If unlink was possible, it has been done, otherwise it has not. */
164 put_inode(rip);
165 put_inode(rldirp);
166 return(r);
170 /*===========================================================================*
171 * fs_rdlink *
172 *===========================================================================*/
173 int fs_rdlink()
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 */
179 size_t copylen;
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)
185 return(EINVAL);
187 if(!S_ISLNK(rip->i_mode))
188 r = EACCES;
189 else if ((b = read_map(rip, (off_t) 0)) == NO_BLOCK)
190 r = EIO;
191 else {
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),
199 (size_t) copylen);
200 put_block(bp, DIRECTORY_BLOCK);
201 if (r == OK)
202 fs_m_out.RES_NBYTES = copylen;
205 put_inode(rip);
206 return(r);
210 /*===========================================================================*
211 * remove_dir *
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)
225 int r;
227 /* search_dir checks that rip is a directory too. */
228 if ((r = search_dir(rip, "", NULL, IS_EMPTY, IGN_PERM)) != OK)
229 return(r);
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);
242 return(OK);
246 /*===========================================================================*
247 * unlink_file *
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 */
257 int r;
259 /* If rip is not NULL, it is used to get faster access to the inode. */
260 if (rip == NULL) {
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);
265 } else {
266 dup_inode(rip); /* inode will be returned with put_inode */
269 r = search_dir(dirp, file_name, NULL, DELETE, IGN_PERM);
271 if (r == OK) {
272 rip->i_nlinks--; /* entry deleted from parent's dir */
273 rip->i_update |= CTIME;
274 IN_MARKDIRTY(rip);
277 put_inode(rip);
278 return(r);
282 /*===========================================================================*
283 * fs_rename *
284 *===========================================================================*/
285 int fs_rename()
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];
295 ino_t numb;
296 phys_bytes len;
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)
314 return(err_code);
316 old_ip = advance(old_dirp, old_name, IGN_PERM);
317 r = err_code;
319 if (r == EENTERMOUNT || r == ELEAVEMOUNT) {
320 put_inode(old_ip);
321 old_ip = NULL;
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) {
327 put_inode(old_dirp);
328 return(r);
331 /* Get new dir inode */
332 if( (new_dirp = get_inode(fs_dev, (ino_t) fs_m_in.REQ_REN_NEW_DIR)) == NULL) {
333 put_inode(old_ip);
334 put_inode(old_dirp);
335 return(err_code);
336 } else {
337 if (new_dirp->i_nlinks == NO_LINK) { /* Dir does not actually exist */
338 put_inode(old_ip);
339 put_inode(old_dirp);
340 put_inode(new_dirp);
341 return(ENOENT);
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) {
350 put_inode(new_ip);
351 new_ip = NULL;
352 r = EBUSY;
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. */
358 if(r == OK) {
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);
367 r = EINVAL;
368 break;
370 next_new_superdirp = advance(new_superdirp, dot2,
371 IGN_PERM);
373 put_inode(new_superdirp);
374 if(next_new_superdirp == new_superdirp) {
375 put_inode(new_superdirp);
376 break;
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);
382 err_code = OK;
383 break;
385 new_superdirp = next_new_superdirp;
386 if(new_superdirp == NULL) {
387 /* Missing ".." entry. Assume the worst. */
388 r = EINVAL;
389 break;
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) {
397 r = EINVAL;
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. */
403 if(new_ip == NULL) {
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) {
408 r = EMLINK;
410 } else {
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].
431 if(r == OK) {
432 if(new_ip != NULL) {
433 /* There is already an entry for 'new'. Try to remove it. */
434 if(odir)
435 r = remove_dir(new_dirp, new_ip, new_name);
436 else
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. */
443 if(r == OK) {
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 */
451 if(same_pdir) {
452 r = search_dir(old_dirp, old_name, NULL, DELETE, IGN_PERM);
453 /* shouldn't go wrong. */
454 if(r == OK)
455 (void) search_dir(old_dirp, new_name, &numb, ENTER,
456 IGN_PERM);
457 } else {
458 r = search_dir(new_dirp, new_name, &numb, ENTER, IGN_PERM);
459 if(r == OK)
460 (void) search_dir(old_dirp, old_name, NULL, DELETE,
461 IGN_PERM);
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. */
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_ftrunc *
489 *===========================================================================*/
490 int fs_ftrunc(void)
492 struct inode *rip;
493 off_t start, end;
494 int r;
496 if( (rip = find_inode(fs_dev, (ino_t) fs_m_in.REQ_INODE_NR)) == NULL)
497 return(EINVAL);
499 if(rip->i_sp->s_rd_only) {
500 r = EROFS;
501 } else {
502 start = fs_m_in.REQ_TRC_START_LO;
503 end = fs_m_in.REQ_TRC_END_LO;
505 if (end == 0)
506 r = truncate_inode(rip, start);
507 else
508 r = freesp_inode(rip, start, end);
511 return(r);
515 /*===========================================================================*
516 * truncate_inode *
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
528 * writing is done.
530 int r;
531 mode_t file_type;
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)
535 return(EINVAL);
536 if (newsize > rip->i_sp->s_max_size) /* don't let inode grow too big */
537 return(EFBIG);
539 /* Free the actual space if truncating. */
540 if (newsize < rip->i_size) {
541 if ((r = freesp_inode(rip, newsize, rip->i_size)) != OK)
542 return(r);
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;
551 IN_MARKDIRTY(rip);
553 return(OK);
557 /*===========================================================================*
558 * freesp_inode *
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
573 * fcntl().
575 off_t p, e;
576 int zone_size, r;
577 int zero_last, zero_first;
579 if(end > rip->i_size) /* freeing beyond end makes no sense */
580 end = rip->i_size;
581 if(end <= start) /* end is uninclusive, so start<end */
582 return(EINVAL);
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);
593 } else {
594 /* First zero unused part of partly used zones. */
595 if(zero_last)
596 zerozone_half(rip, start, LAST_HALF, zone_size);
597 if(zero_first)
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'.
605 e = end/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)
609 return(r);
614 rip->i_update |= CTIME | MTIME;
615 IN_MARKDIRTY(rip);
617 return(OK);
621 /*===========================================================================*
622 * nextblock *
623 *===========================================================================*/
624 static off_t nextblock(pos, zone_size)
625 off_t pos;
626 int 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.
632 off_t p;
633 p = (pos/zone_size)*zone_size;
634 if((pos % zone_size)) p += zone_size; /* Round up. */
635 return(p);
639 /*===========================================================================*
640 * zerozone_half *
641 *===========================================================================*/
642 static void zerozone_half(rip, pos, half, zone_size)
643 struct inode *rip;
644 off_t pos;
645 int half;
646 int 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
654 off_t offset, len;
656 /* Offset of zeroing boundary. */
657 offset = pos % zone_size;
659 if(half == LAST_HALF) {
660 len = zone_size - offset;
661 } else {
662 len = offset;
663 pos -= offset;
666 zerozone_range(rip, pos, len);
670 /*===========================================================================*
671 * zerozone_range *
672 *===========================================================================*/
673 static void zerozone_range(rip, pos, len)
674 struct inode *rip;
675 off_t pos;
676 off_t len;
678 /* Zero an arbitrary byte range in a zone, possibly spanning multiple blocks.
680 block_t b;
681 struct buf *bp;
682 off_t offset;
683 unsigned short block_size;
684 size_t bytes;
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;
690 while (len > 0) {
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)
696 bytes = len;
697 memset(b_data(bp) + offset, 0, bytes);
698 MARKDIRTY(bp);
699 put_block(bp, FULL_DATA_BLOCK);
701 pos += bytes;
702 len -= bytes;
703 b++;