ufs.c revision 1.24 1 /* $NetBSD: ufs.c,v 1.24 1999/03/31 07:43:39 simonb Exp $ */
2
3 /*-
4 * Copyright (c) 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * The Mach Operating System project at Carnegie-Mellon University.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 *
38 *
39 * Copyright (c) 1990, 1991 Carnegie Mellon University
40 * All Rights Reserved.
41 *
42 * Author: David Golub
43 *
44 * Permission to use, copy, modify and distribute this software and its
45 * documentation is hereby granted, provided that both the copyright
46 * notice and this permission notice appear in all copies of the
47 * software, derivative works or modified versions, and any portions
48 * thereof, and that both notices appear in supporting documentation.
49 *
50 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
52 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53 *
54 * Carnegie Mellon requests users of this software to return to
55 *
56 * Software Distribution Coordinator or Software.Distribution (at) CS.CMU.EDU
57 * School of Computer Science
58 * Carnegie Mellon University
59 * Pittsburgh PA 15213-3890
60 *
61 * any improvements or extensions that they make and grant Carnegie the
62 * rights to redistribute these changes.
63 */
64
65 /*
66 * Stand-alone file reading package.
67 */
68
69 #include <sys/param.h>
70 #include <sys/time.h>
71 #include <ufs/ufs/dinode.h>
72 #include <ufs/ufs/dir.h>
73 #include <ufs/ffs/fs.h>
74 #ifdef _STANDALONE
75 #include <lib/libkern/libkern.h>
76 #else
77 #include <string.h>
78 inline u_int
79 max(a, b)
80 u_int a, b;
81 {
82 return (a > b ? a : b);
83 }
84 #endif
85
86 #include "stand.h"
87 #include "ufs.h"
88
89 #if defined(LIBSA_FS_SINGLECOMPONENT) && !defined(LIBSA_NO_FS_SYMLINK)
90 #define LIBSA_NO_FS_SYMLINK
91 #endif
92
93
94 /*
95 * In-core open file.
96 */
97 struct file {
98 off_t f_seekp; /* seek pointer */
99 struct fs *f_fs; /* pointer to super-block */
100 struct dinode f_di; /* copy of on-disk inode */
101 unsigned int f_nindir[NIADDR];
102 /* number of blocks mapped by
103 indirect block at level i */
104 char *f_blk[NIADDR]; /* buffer for indirect block at
105 level i */
106 size_t f_blksize[NIADDR];
107 /* size of buffer */
108 daddr_t f_blkno[NIADDR];/* disk address of block in buffer */
109 char *f_buf; /* buffer for data block */
110 size_t f_buf_size; /* size of data block */
111 daddr_t f_buf_blkno; /* block number of data block */
112 };
113
114 static int read_inode __P((ino_t, struct open_file *));
115 static int block_map __P((struct open_file *, daddr_t, daddr_t *));
116 static int buf_read_file __P((struct open_file *, char **, size_t *));
117 static int search_directory __P((char *, struct open_file *, ino_t *));
118 #ifdef COMPAT_UFS
119 static void ffs_oldfscompat __P((struct fs *));
120 #endif
121
122 /*
123 * Read a new inode into a file structure.
124 */
125 static int
126 read_inode(inumber, f)
127 ino_t inumber;
128 struct open_file *f;
129 {
130 register struct file *fp = (struct file *)f->f_fsdata;
131 register struct fs *fs = fp->f_fs;
132 char *buf;
133 size_t rsize;
134 int rc;
135
136 /*
137 * Read inode and save it.
138 */
139 buf = alloc(fs->fs_bsize);
140 #if !defined(LIBSA_NO_TWIDDLE)
141 twiddle();
142 #endif
143 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
144 fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize,
145 buf, &rsize);
146 if (rc)
147 goto out;
148 if (rsize != fs->fs_bsize) {
149 rc = EIO;
150 goto out;
151 }
152
153 {
154 register struct dinode *dp;
155
156 dp = (struct dinode *)buf;
157 fp->f_di = dp[ino_to_fsbo(fs, inumber)];
158 }
159
160 /*
161 * Clear out the old buffers
162 */
163 {
164 register int level;
165
166 for (level = 0; level < NIADDR; level++)
167 fp->f_blkno[level] = -1;
168 fp->f_buf_blkno = -1;
169 }
170 out:
171 free(buf, fs->fs_bsize);
172 return (rc);
173 }
174
175 /*
176 * Given an offset in a file, find the disk block number that
177 * contains that block.
178 */
179 static int
180 block_map(f, file_block, disk_block_p)
181 struct open_file *f;
182 daddr_t file_block;
183 daddr_t *disk_block_p; /* out */
184 {
185 register struct file *fp = (struct file *)f->f_fsdata;
186 register struct fs *fs = fp->f_fs;
187 int level;
188 int idx;
189 daddr_t ind_block_num;
190 daddr_t *ind_p;
191 int rc;
192
193 /*
194 * Index structure of an inode:
195 *
196 * di_db[0..NDADDR-1] hold block numbers for blocks
197 * 0..NDADDR-1
198 *
199 * di_ib[0] index block 0 is the single indirect block
200 * holds block numbers for blocks
201 * NDADDR .. NDADDR + NINDIR(fs)-1
202 *
203 * di_ib[1] index block 1 is the double indirect block
204 * holds block numbers for INDEX blocks for blocks
205 * NDADDR + NINDIR(fs) ..
206 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
207 *
208 * di_ib[2] index block 2 is the triple indirect block
209 * holds block numbers for double-indirect
210 * blocks for blocks
211 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
212 * NDADDR + NINDIR(fs) + NINDIR(fs)**2
213 * + NINDIR(fs)**3 - 1
214 */
215
216 if (file_block < NDADDR) {
217 /* Direct block. */
218 *disk_block_p = fp->f_di.di_db[file_block];
219 return (0);
220 }
221
222 file_block -= NDADDR;
223
224 /*
225 * nindir[0] = NINDIR
226 * nindir[1] = NINDIR**2
227 * nindir[2] = NINDIR**3
228 * etc
229 */
230 for (level = 0; level < NIADDR; level++) {
231 if (file_block < fp->f_nindir[level])
232 break;
233 file_block -= fp->f_nindir[level];
234 }
235 if (level == NIADDR) {
236 /* Block number too high */
237 return (EFBIG);
238 }
239
240 ind_block_num = fp->f_di.di_ib[level];
241
242 for (; level >= 0; level--) {
243 if (ind_block_num == 0) {
244 *disk_block_p = 0; /* missing */
245 return (0);
246 }
247
248 if (fp->f_blkno[level] != ind_block_num) {
249 if (fp->f_blk[level] == (char *)0)
250 fp->f_blk[level] =
251 alloc(fs->fs_bsize);
252 #if !defined(LIBSA_NO_TWIDDLE)
253 twiddle();
254 #endif
255 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
256 fsbtodb(fp->f_fs, ind_block_num),
257 fs->fs_bsize,
258 fp->f_blk[level],
259 &fp->f_blksize[level]);
260 if (rc)
261 return (rc);
262 if (fp->f_blksize[level] != fs->fs_bsize)
263 return (EIO);
264 fp->f_blkno[level] = ind_block_num;
265 }
266
267 ind_p = (daddr_t *)fp->f_blk[level];
268
269 if (level > 0) {
270 idx = file_block / fp->f_nindir[level - 1];
271 file_block %= fp->f_nindir[level - 1];
272 } else
273 idx = file_block;
274
275 ind_block_num = ind_p[idx];
276 }
277
278 *disk_block_p = ind_block_num;
279
280 return (0);
281 }
282
283 /*
284 * Read a portion of a file into an internal buffer. Return
285 * the location in the buffer and the amount in the buffer.
286 */
287 static int
288 buf_read_file(f, buf_p, size_p)
289 struct open_file *f;
290 char **buf_p; /* out */
291 size_t *size_p; /* out */
292 {
293 register struct file *fp = (struct file *)f->f_fsdata;
294 register struct fs *fs = fp->f_fs;
295 long off;
296 register daddr_t file_block;
297 daddr_t disk_block;
298 size_t block_size;
299 int rc;
300
301 off = blkoff(fs, fp->f_seekp);
302 file_block = lblkno(fs, fp->f_seekp);
303 block_size = dblksize(fs, &fp->f_di, file_block);
304
305 if (file_block != fp->f_buf_blkno) {
306 rc = block_map(f, file_block, &disk_block);
307 if (rc)
308 return (rc);
309
310 if (fp->f_buf == (char *)0)
311 fp->f_buf = alloc(fs->fs_bsize);
312
313 if (disk_block == 0) {
314 bzero(fp->f_buf, block_size);
315 fp->f_buf_size = block_size;
316 } else {
317 #if !defined(LIBSA_NO_TWIDDLE)
318 twiddle();
319 #endif
320 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
321 fsbtodb(fs, disk_block),
322 block_size, fp->f_buf, &fp->f_buf_size);
323 if (rc)
324 return (rc);
325 }
326
327 fp->f_buf_blkno = file_block;
328 }
329
330 /*
331 * Return address of byte in buffer corresponding to
332 * offset, and size of remainder of buffer after that
333 * byte.
334 */
335 *buf_p = fp->f_buf + off;
336 *size_p = block_size - off;
337
338 /*
339 * But truncate buffer at end of file.
340 */
341 if (*size_p > fp->f_di.di_size - fp->f_seekp)
342 *size_p = fp->f_di.di_size - fp->f_seekp;
343
344 return (0);
345 }
346
347 /*
348 * Search a directory for a name and return its
349 * i_number.
350 */
351 static int
352 search_directory(name, f, inumber_p)
353 char *name;
354 struct open_file *f;
355 ino_t *inumber_p; /* out */
356 {
357 register struct file *fp = (struct file *)f->f_fsdata;
358 register struct direct *dp;
359 struct direct *edp;
360 char *buf;
361 size_t buf_size;
362 int namlen, length;
363 int rc;
364
365 length = strlen(name);
366
367 fp->f_seekp = 0;
368 while (fp->f_seekp < fp->f_di.di_size) {
369 rc = buf_read_file(f, &buf, &buf_size);
370 if (rc)
371 return (rc);
372
373 dp = (struct direct *)buf;
374 edp = (struct direct *)(buf + buf_size);
375 while (dp < edp) {
376 if (dp->d_ino == (ino_t)0)
377 goto next;
378 #if BYTE_ORDER == LITTLE_ENDIAN
379 if (fp->f_fs->fs_maxsymlinklen <= 0)
380 namlen = dp->d_type;
381 else
382 #endif
383 namlen = dp->d_namlen;
384 if (namlen == length &&
385 !strcmp(name, dp->d_name)) {
386 /* found entry */
387 *inumber_p = dp->d_ino;
388 return (0);
389 }
390 next:
391 dp = (struct direct *)((char *)dp + dp->d_reclen);
392 }
393 fp->f_seekp += buf_size;
394 }
395 return (ENOENT);
396 }
397
398 /*
399 * Open a file.
400 */
401 int
402 ufs_open(path, f)
403 char *path;
404 struct open_file *f;
405 {
406 #ifndef LIBSA_FS_SINGLECOMPONENT
407 register char *cp, *ncp;
408 register int c;
409 #endif
410 ino_t inumber;
411 struct file *fp;
412 struct fs *fs;
413 int rc;
414 size_t buf_size;
415 #ifndef LIBSA_NO_FS_SYMLINK
416 ino_t parent_inumber;
417 int nlinks = 0;
418 char namebuf[MAXPATHLEN+1];
419 char *buf = NULL;
420 #endif
421
422 /* allocate file system specific data structure */
423 fp = alloc(sizeof(struct file));
424 bzero(fp, sizeof(struct file));
425 f->f_fsdata = (void *)fp;
426
427 /* allocate space and read super block */
428 fs = alloc(SBSIZE);
429 fp->f_fs = fs;
430 #if !defined(LIBSA_NO_TWIDDLE)
431 twiddle();
432 #endif
433 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
434 SBLOCK, SBSIZE, (char *)fs, &buf_size);
435 if (rc)
436 goto out;
437
438 if (buf_size != SBSIZE || fs->fs_magic != FS_MAGIC ||
439 fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
440 rc = EINVAL;
441 goto out;
442 }
443 #ifdef COMPAT_UFS
444 ffs_oldfscompat(fs);
445 #endif
446
447 /*
448 * Calculate indirect block levels.
449 */
450 {
451 register int mult;
452 register int level;
453
454 mult = 1;
455 for (level = 0; level < NIADDR; level++) {
456 mult *= NINDIR(fs);
457 fp->f_nindir[level] = mult;
458 }
459 }
460
461 inumber = ROOTINO;
462 if ((rc = read_inode(inumber, f)) != 0)
463 goto out;
464
465 #ifndef LIBSA_FS_SINGLECOMPONENT
466 cp = path;
467 while (*cp) {
468
469 /*
470 * Remove extra separators
471 */
472 while (*cp == '/')
473 cp++;
474 if (*cp == '\0')
475 break;
476
477 /*
478 * Check that current node is a directory.
479 */
480 if ((fp->f_di.di_mode & IFMT) != IFDIR) {
481 rc = ENOTDIR;
482 goto out;
483 }
484
485 /*
486 * Get next component of path name.
487 */
488 {
489 register int len = 0;
490
491 ncp = cp;
492 while ((c = *cp) != '\0' && c != '/') {
493 if (++len > MAXNAMLEN) {
494 rc = ENOENT;
495 goto out;
496 }
497 cp++;
498 }
499 *cp = '\0';
500 }
501
502 /*
503 * Look up component in current directory.
504 * Save directory inumber in case we find a
505 * symbolic link.
506 */
507 #ifndef LIBSA_NO_FS_SYMLINK
508 parent_inumber = inumber;
509 #endif
510 rc = search_directory(ncp, f, &inumber);
511 *cp = c;
512 if (rc)
513 goto out;
514
515 /*
516 * Open next component.
517 */
518 if ((rc = read_inode(inumber, f)) != 0)
519 goto out;
520
521 #ifndef LIBSA_NO_FS_SYMLINK
522 /*
523 * Check for symbolic link.
524 */
525 if ((fp->f_di.di_mode & IFMT) == IFLNK) {
526 int link_len = fp->f_di.di_size;
527 int len;
528
529 len = strlen(cp);
530
531 if (link_len + len > MAXPATHLEN ||
532 ++nlinks > MAXSYMLINKS) {
533 rc = ENOENT;
534 goto out;
535 }
536
537 bcopy(cp, &namebuf[link_len], len + 1);
538
539 if (link_len < fs->fs_maxsymlinklen) {
540 bcopy(fp->f_di.di_shortlink, namebuf,
541 (unsigned) link_len);
542 } else {
543 /*
544 * Read file for symbolic link
545 */
546 size_t buf_size;
547 daddr_t disk_block;
548 register struct fs *fs = fp->f_fs;
549
550 if (!buf)
551 buf = alloc(fs->fs_bsize);
552 rc = block_map(f, (daddr_t)0, &disk_block);
553 if (rc)
554 goto out;
555
556 #if !defined(LIBSA_NO_TWIDDLE)
557 twiddle();
558 #endif
559 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata,
560 F_READ, fsbtodb(fs, disk_block),
561 fs->fs_bsize, buf, &buf_size);
562 if (rc)
563 goto out;
564
565 bcopy((char *)buf, namebuf, (unsigned)link_len);
566 }
567
568 /*
569 * If relative pathname, restart at parent directory.
570 * If absolute pathname, restart at root.
571 */
572 cp = namebuf;
573 if (*cp != '/')
574 inumber = parent_inumber;
575 else
576 inumber = (ino_t)ROOTINO;
577
578 if ((rc = read_inode(inumber, f)) != 0)
579 goto out;
580 }
581 #endif /* !LIBSA_NO_FS_SYMLINK */
582 }
583
584 /*
585 * Found terminal component.
586 */
587 rc = 0;
588
589 #else /* !LIBSA_FS_SINGLECOMPONENT */
590
591 /* look up component in the current (root) directory */
592 rc = search_directory(path, f, &inumber);
593 if (rc)
594 goto out;
595
596 /* open it */
597 rc = read_inode(inumber, f);
598
599 #endif /* !LIBSA_FS_SINGLECOMPONENT */
600
601 out:
602 #ifndef LIBSA_NO_FS_SYMLINK
603 if (buf)
604 free(buf, fs->fs_bsize);
605 #endif
606 if (rc) {
607 if (fp->f_buf)
608 free(fp->f_buf, fp->f_fs->fs_bsize);
609 free(fp->f_fs, SBSIZE);
610 free(fp, sizeof(struct file));
611 }
612 return (rc);
613 }
614
615 #ifndef LIBSA_NO_FS_CLOSE
616 int
617 ufs_close(f)
618 struct open_file *f;
619 {
620 register struct file *fp = (struct file *)f->f_fsdata;
621 int level;
622
623 f->f_fsdata = (void *)0;
624 if (fp == (struct file *)0)
625 return (0);
626
627 for (level = 0; level < NIADDR; level++) {
628 if (fp->f_blk[level])
629 free(fp->f_blk[level], fp->f_fs->fs_bsize);
630 }
631 if (fp->f_buf)
632 free(fp->f_buf, fp->f_fs->fs_bsize);
633 free(fp->f_fs, SBSIZE);
634 free(fp, sizeof(struct file));
635 return (0);
636 }
637 #endif /* !LIBSA_NO_FS_CLOSE */
638
639 /*
640 * Copy a portion of a file into kernel memory.
641 * Cross block boundaries when necessary.
642 */
643 int
644 ufs_read(f, start, size, resid)
645 struct open_file *f;
646 void *start;
647 size_t size;
648 size_t *resid; /* out */
649 {
650 register struct file *fp = (struct file *)f->f_fsdata;
651 register size_t csize;
652 char *buf;
653 size_t buf_size;
654 int rc = 0;
655 register char *addr = start;
656
657 while (size != 0) {
658 if (fp->f_seekp >= fp->f_di.di_size)
659 break;
660
661 rc = buf_read_file(f, &buf, &buf_size);
662 if (rc)
663 break;
664
665 csize = size;
666 if (csize > buf_size)
667 csize = buf_size;
668
669 bcopy(buf, addr, csize);
670
671 fp->f_seekp += csize;
672 addr += csize;
673 size -= csize;
674 }
675 if (resid)
676 *resid = size;
677 return (rc);
678 }
679
680 /*
681 * Not implemented.
682 */
683 #ifndef LIBSA_NO_FS_WRITE
684 int
685 ufs_write(f, start, size, resid)
686 struct open_file *f;
687 void *start;
688 size_t size;
689 size_t *resid; /* out */
690 {
691
692 return (EROFS);
693 }
694 #endif /* !LIBSA_NO_FS_WRITE */
695
696 #ifndef LIBSA_NO_FS_SEEK
697 off_t
698 ufs_seek(f, offset, where)
699 struct open_file *f;
700 off_t offset;
701 int where;
702 {
703 register struct file *fp = (struct file *)f->f_fsdata;
704
705 switch (where) {
706 case SEEK_SET:
707 fp->f_seekp = offset;
708 break;
709 case SEEK_CUR:
710 fp->f_seekp += offset;
711 break;
712 case SEEK_END:
713 fp->f_seekp = fp->f_di.di_size - offset;
714 break;
715 default:
716 return (-1);
717 }
718 return (fp->f_seekp);
719 }
720 #endif /* !LIBSA_NO_FS_SEEK */
721
722 int
723 ufs_stat(f, sb)
724 struct open_file *f;
725 struct stat *sb;
726 {
727 register struct file *fp = (struct file *)f->f_fsdata;
728
729 /* only important stuff */
730 sb->st_mode = fp->f_di.di_mode;
731 sb->st_uid = fp->f_di.di_uid;
732 sb->st_gid = fp->f_di.di_gid;
733 sb->st_size = fp->f_di.di_size;
734 return (0);
735 }
736
737 #ifdef COMPAT_UFS
738 /*
739 * Sanity checks for old file systems.
740 *
741 * XXX - goes away some day.
742 */
743 static void
744 ffs_oldfscompat(fs)
745 struct fs *fs;
746 {
747 int i;
748
749 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
750 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
751 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
752 fs->fs_nrpos = 8; /* XXX */
753 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
754 quad_t sizepb = fs->fs_bsize; /* XXX */
755 /* XXX */
756 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
757 for (i = 0; i < NIADDR; i++) { /* XXX */
758 sizepb *= NINDIR(fs); /* XXX */
759 fs->fs_maxfilesize += sizepb; /* XXX */
760 } /* XXX */
761 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
762 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
763 } /* XXX */
764 }
765 #endif
766