ufs.c revision 1.28 1 /* $NetBSD: ufs.c,v 1.28 1999/11/11 20:23:17 thorpej 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 * XXX NOTE: ufs.c (FFS) and lfs.c (LFS) should eventually use much common
67 * XXX code. until then, the two files should be easily diffable.
68 */
69
70 /*
71 * Stand-alone file reading package.
72 */
73
74 #include <sys/param.h>
75 #include <sys/time.h>
76 #include <ufs/ufs/dinode.h>
77 #include <ufs/ufs/dir.h>
78 #include <ufs/ffs/fs.h>
79
80 #include "stand.h"
81 #include "ufs.h"
82
83 #if defined(LIBSA_FS_SINGLECOMPONENT) && !defined(LIBSA_NO_FS_SYMLINK)
84 #define LIBSA_NO_FS_SYMLINK
85 #endif
86 #if defined(COMPAT_UFS) && defined(LIBSA_NO_COMPAT_UFS)
87 #undef COMPAT_UFS
88 #endif
89
90
91 /*
92 * In-core open file.
93 */
94 struct file {
95 off_t f_seekp; /* seek pointer */
96 struct fs *f_fs; /* pointer to super-block */
97 struct dinode f_di; /* copy of on-disk inode */
98 unsigned int f_nindir[NIADDR];
99 /* number of blocks mapped by
100 indirect block at level i */
101 char *f_blk[NIADDR]; /* buffer for indirect block at
102 level i */
103 size_t f_blksize[NIADDR];
104 /* size of buffer */
105 daddr_t f_blkno[NIADDR];/* disk address of block in buffer */
106 char *f_buf; /* buffer for data block */
107 size_t f_buf_size; /* size of data block */
108 daddr_t f_buf_blkno; /* block number of data block */
109 };
110
111 static int read_inode __P((ino_t, struct open_file *));
112 static int block_map __P((struct open_file *, daddr_t, daddr_t *));
113 static int buf_read_file __P((struct open_file *, char **, size_t *));
114 static int search_directory __P((char *, struct open_file *, ino_t *));
115 #ifdef COMPAT_UFS
116 static void ffs_oldfscompat __P((struct fs *));
117 #endif
118
119 /*
120 * Read a new inode into a file structure.
121 */
122 static int
123 read_inode(inumber, f)
124 ino_t inumber;
125 struct open_file *f;
126 {
127 register struct file *fp = (struct file *)f->f_fsdata;
128 register struct fs *fs = fp->f_fs;
129 char *buf;
130 size_t rsize;
131 int rc;
132
133 /*
134 * Read inode and save it.
135 */
136 buf = alloc(fs->fs_bsize);
137 #if !defined(LIBSA_NO_TWIDDLE)
138 twiddle();
139 #endif
140 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
141 fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize,
142 buf, &rsize);
143 if (rc)
144 goto out;
145 if (rsize != fs->fs_bsize) {
146 rc = EIO;
147 goto out;
148 }
149
150 {
151 register struct dinode *dp;
152
153 dp = (struct dinode *)buf;
154 fp->f_di = dp[ino_to_fsbo(fs, inumber)];
155 }
156
157 /*
158 * Clear out the old buffers
159 */
160 {
161 register int level;
162
163 for (level = 0; level < NIADDR; level++)
164 fp->f_blkno[level] = -1;
165 fp->f_buf_blkno = -1;
166 }
167 out:
168 free(buf, fs->fs_bsize);
169 return (rc);
170 }
171
172 /*
173 * Given an offset in a file, find the disk block number that
174 * contains that block.
175 */
176 static int
177 block_map(f, file_block, disk_block_p)
178 struct open_file *f;
179 daddr_t file_block;
180 daddr_t *disk_block_p; /* out */
181 {
182 register struct file *fp = (struct file *)f->f_fsdata;
183 register struct fs *fs = fp->f_fs;
184 int level;
185 int idx;
186 daddr_t ind_block_num;
187 daddr_t *ind_p;
188 int rc;
189
190 /*
191 * Index structure of an inode:
192 *
193 * di_db[0..NDADDR-1] hold block numbers for blocks
194 * 0..NDADDR-1
195 *
196 * di_ib[0] index block 0 is the single indirect block
197 * holds block numbers for blocks
198 * NDADDR .. NDADDR + NINDIR(fs)-1
199 *
200 * di_ib[1] index block 1 is the double indirect block
201 * holds block numbers for INDEX blocks for blocks
202 * NDADDR + NINDIR(fs) ..
203 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
204 *
205 * di_ib[2] index block 2 is the triple indirect block
206 * holds block numbers for double-indirect
207 * blocks for blocks
208 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
209 * NDADDR + NINDIR(fs) + NINDIR(fs)**2
210 * + NINDIR(fs)**3 - 1
211 */
212
213 if (file_block < NDADDR) {
214 /* Direct block. */
215 *disk_block_p = fp->f_di.di_db[file_block];
216 return (0);
217 }
218
219 file_block -= NDADDR;
220
221 /*
222 * nindir[0] = NINDIR
223 * nindir[1] = NINDIR**2
224 * nindir[2] = NINDIR**3
225 * etc
226 */
227 for (level = 0; level < NIADDR; level++) {
228 if (file_block < fp->f_nindir[level])
229 break;
230 file_block -= fp->f_nindir[level];
231 }
232 if (level == NIADDR) {
233 /* Block number too high */
234 return (EFBIG);
235 }
236
237 ind_block_num = fp->f_di.di_ib[level];
238
239 for (; level >= 0; level--) {
240 if (ind_block_num == 0) {
241 *disk_block_p = 0; /* missing */
242 return (0);
243 }
244
245 if (fp->f_blkno[level] != ind_block_num) {
246 if (fp->f_blk[level] == (char *)0)
247 fp->f_blk[level] =
248 alloc(fs->fs_bsize);
249 #if !defined(LIBSA_NO_TWIDDLE)
250 twiddle();
251 #endif
252 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
253 fsbtodb(fp->f_fs, ind_block_num),
254 fs->fs_bsize,
255 fp->f_blk[level],
256 &fp->f_blksize[level]);
257 if (rc)
258 return (rc);
259 if (fp->f_blksize[level] != fs->fs_bsize)
260 return (EIO);
261 fp->f_blkno[level] = ind_block_num;
262 }
263
264 ind_p = (daddr_t *)fp->f_blk[level];
265
266 if (level > 0) {
267 idx = file_block / fp->f_nindir[level - 1];
268 file_block %= fp->f_nindir[level - 1];
269 } else
270 idx = file_block;
271
272 ind_block_num = ind_p[idx];
273 }
274
275 *disk_block_p = ind_block_num;
276
277 return (0);
278 }
279
280 /*
281 * Read a portion of a file into an internal buffer. Return
282 * the location in the buffer and the amount in the buffer.
283 */
284 static int
285 buf_read_file(f, buf_p, size_p)
286 struct open_file *f;
287 char **buf_p; /* out */
288 size_t *size_p; /* out */
289 {
290 register struct file *fp = (struct file *)f->f_fsdata;
291 register struct fs *fs = fp->f_fs;
292 long off;
293 register daddr_t file_block;
294 daddr_t disk_block;
295 size_t block_size;
296 int rc;
297
298 off = blkoff(fs, fp->f_seekp);
299 file_block = lblkno(fs, fp->f_seekp);
300 block_size = dblksize(fs, &fp->f_di, file_block);
301
302 if (file_block != fp->f_buf_blkno) {
303 rc = block_map(f, file_block, &disk_block);
304 if (rc)
305 return (rc);
306
307 if (fp->f_buf == (char *)0)
308 fp->f_buf = alloc(fs->fs_bsize);
309
310 if (disk_block == 0) {
311 bzero(fp->f_buf, block_size);
312 fp->f_buf_size = block_size;
313 } else {
314 #if !defined(LIBSA_NO_TWIDDLE)
315 twiddle();
316 #endif
317 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
318 fsbtodb(fs, disk_block),
319 block_size, fp->f_buf, &fp->f_buf_size);
320 if (rc)
321 return (rc);
322 }
323
324 fp->f_buf_blkno = file_block;
325 }
326
327 /*
328 * Return address of byte in buffer corresponding to
329 * offset, and size of remainder of buffer after that
330 * byte.
331 */
332 *buf_p = fp->f_buf + off;
333 *size_p = block_size - off;
334
335 /*
336 * But truncate buffer at end of file.
337 */
338 if (*size_p > fp->f_di.di_size - fp->f_seekp)
339 *size_p = fp->f_di.di_size - fp->f_seekp;
340
341 return (0);
342 }
343
344 /*
345 * Search a directory for a name and return its
346 * i_number.
347 */
348 static int
349 search_directory(name, f, inumber_p)
350 char *name;
351 struct open_file *f;
352 ino_t *inumber_p; /* out */
353 {
354 register struct file *fp = (struct file *)f->f_fsdata;
355 register struct direct *dp;
356 struct direct *edp;
357 char *buf;
358 size_t buf_size;
359 int namlen, length;
360 int rc;
361
362 length = strlen(name);
363
364 fp->f_seekp = 0;
365 while (fp->f_seekp < fp->f_di.di_size) {
366 rc = buf_read_file(f, &buf, &buf_size);
367 if (rc)
368 return (rc);
369
370 dp = (struct direct *)buf;
371 edp = (struct direct *)(buf + buf_size);
372 while (dp < edp) {
373 if (dp->d_ino == (ino_t)0)
374 goto next;
375 #if BYTE_ORDER == LITTLE_ENDIAN
376 if (fp->f_fs->fs_maxsymlinklen <= 0)
377 namlen = dp->d_type;
378 else
379 #endif
380 namlen = dp->d_namlen;
381 if (namlen == length &&
382 !strcmp(name, dp->d_name)) {
383 /* found entry */
384 *inumber_p = dp->d_ino;
385 return (0);
386 }
387 next:
388 dp = (struct direct *)((char *)dp + dp->d_reclen);
389 }
390 fp->f_seekp += buf_size;
391 }
392 return (ENOENT);
393 }
394
395 /*
396 * Open a file.
397 */
398 int
399 ufs_open(path, f)
400 char *path;
401 struct open_file *f;
402 {
403 #ifndef LIBSA_FS_SINGLECOMPONENT
404 register char *cp, *ncp;
405 register int c;
406 #endif
407 ino_t inumber;
408 struct file *fp;
409 struct fs *fs;
410 int rc;
411 size_t buf_size;
412 #ifndef LIBSA_NO_FS_SYMLINK
413 ino_t parent_inumber;
414 int nlinks = 0;
415 char namebuf[MAXPATHLEN+1];
416 char *buf = NULL;
417 #endif
418
419 /* allocate file system specific data structure */
420 fp = alloc(sizeof(struct file));
421 bzero(fp, sizeof(struct file));
422 f->f_fsdata = (void *)fp;
423
424 /* allocate space and read super block */
425 fs = alloc(SBSIZE);
426 fp->f_fs = fs;
427 #if !defined(LIBSA_NO_TWIDDLE)
428 twiddle();
429 #endif
430 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata, F_READ,
431 SBLOCK, SBSIZE, (char *)fs, &buf_size);
432 if (rc)
433 goto out;
434
435 if (buf_size != SBSIZE || fs->fs_magic != FS_MAGIC ||
436 fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
437 rc = EINVAL;
438 goto out;
439 }
440 #ifdef COMPAT_UFS
441 ffs_oldfscompat(fs);
442 #endif
443
444 /*
445 * Calculate indirect block levels.
446 */
447 {
448 register int mult;
449 register int level;
450
451 mult = 1;
452 for (level = 0; level < NIADDR; level++) {
453 mult *= NINDIR(fs);
454 fp->f_nindir[level] = mult;
455 }
456 }
457
458 inumber = ROOTINO;
459 if ((rc = read_inode(inumber, f)) != 0)
460 goto out;
461
462 #ifndef LIBSA_FS_SINGLECOMPONENT
463 cp = path;
464 while (*cp) {
465
466 /*
467 * Remove extra separators
468 */
469 while (*cp == '/')
470 cp++;
471 if (*cp == '\0')
472 break;
473
474 /*
475 * Check that current node is a directory.
476 */
477 if ((fp->f_di.di_mode & IFMT) != IFDIR) {
478 rc = ENOTDIR;
479 goto out;
480 }
481
482 /*
483 * Get next component of path name.
484 */
485 {
486 register int len = 0;
487
488 ncp = cp;
489 while ((c = *cp) != '\0' && c != '/') {
490 if (++len > MAXNAMLEN) {
491 rc = ENOENT;
492 goto out;
493 }
494 cp++;
495 }
496 *cp = '\0';
497 }
498
499 /*
500 * Look up component in current directory.
501 * Save directory inumber in case we find a
502 * symbolic link.
503 */
504 #ifndef LIBSA_NO_FS_SYMLINK
505 parent_inumber = inumber;
506 #endif
507 rc = search_directory(ncp, f, &inumber);
508 *cp = c;
509 if (rc)
510 goto out;
511
512 /*
513 * Open next component.
514 */
515 if ((rc = read_inode(inumber, f)) != 0)
516 goto out;
517
518 #ifndef LIBSA_NO_FS_SYMLINK
519 /*
520 * Check for symbolic link.
521 */
522 if ((fp->f_di.di_mode & IFMT) == IFLNK) {
523 int link_len = fp->f_di.di_size;
524 int len;
525
526 len = strlen(cp);
527
528 if (link_len + len > MAXPATHLEN ||
529 ++nlinks > MAXSYMLINKS) {
530 rc = ENOENT;
531 goto out;
532 }
533
534 bcopy(cp, &namebuf[link_len], len + 1);
535
536 if (link_len < fs->fs_maxsymlinklen) {
537 bcopy(fp->f_di.di_shortlink, namebuf,
538 (unsigned) link_len);
539 } else {
540 /*
541 * Read file for symbolic link
542 */
543 size_t buf_size;
544 daddr_t disk_block;
545 register struct fs *fs = fp->f_fs;
546
547 if (!buf)
548 buf = alloc(fs->fs_bsize);
549 rc = block_map(f, (daddr_t)0, &disk_block);
550 if (rc)
551 goto out;
552
553 #if !defined(LIBSA_NO_TWIDDLE)
554 twiddle();
555 #endif
556 rc = DEV_STRATEGY(f->f_dev)(f->f_devdata,
557 F_READ, fsbtodb(fs, disk_block),
558 fs->fs_bsize, buf, &buf_size);
559 if (rc)
560 goto out;
561
562 bcopy((char *)buf, namebuf, (unsigned)link_len);
563 }
564
565 /*
566 * If relative pathname, restart at parent directory.
567 * If absolute pathname, restart at root.
568 */
569 cp = namebuf;
570 if (*cp != '/')
571 inumber = parent_inumber;
572 else
573 inumber = (ino_t)ROOTINO;
574
575 if ((rc = read_inode(inumber, f)) != 0)
576 goto out;
577 }
578 #endif /* !LIBSA_NO_FS_SYMLINK */
579 }
580
581 /*
582 * Found terminal component.
583 */
584 rc = 0;
585
586 #else /* !LIBSA_FS_SINGLECOMPONENT */
587
588 /* look up component in the current (root) directory */
589 rc = search_directory(path, f, &inumber);
590 if (rc)
591 goto out;
592
593 /* open it */
594 rc = read_inode(inumber, f);
595
596 #endif /* !LIBSA_FS_SINGLECOMPONENT */
597
598 fp->f_seekp = 0; /* reset seek pointer */
599
600 out:
601 #ifndef LIBSA_NO_FS_SYMLINK
602 if (buf)
603 free(buf, fs->fs_bsize);
604 #endif
605 if (rc) {
606 if (fp->f_buf)
607 free(fp->f_buf, fp->f_fs->fs_bsize);
608 free(fp->f_fs, SBSIZE);
609 free(fp, sizeof(struct file));
610 }
611 return (rc);
612 }
613
614 #ifndef LIBSA_NO_FS_CLOSE
615 int
616 ufs_close(f)
617 struct open_file *f;
618 {
619 register struct file *fp = (struct file *)f->f_fsdata;
620 int level;
621
622 f->f_fsdata = (void *)0;
623 if (fp == (struct file *)0)
624 return (0);
625
626 for (level = 0; level < NIADDR; level++) {
627 if (fp->f_blk[level])
628 free(fp->f_blk[level], fp->f_fs->fs_bsize);
629 }
630 if (fp->f_buf)
631 free(fp->f_buf, fp->f_fs->fs_bsize);
632 free(fp->f_fs, SBSIZE);
633 free(fp, sizeof(struct file));
634 return (0);
635 }
636 #endif /* !LIBSA_NO_FS_CLOSE */
637
638 /*
639 * Copy a portion of a file into kernel memory.
640 * Cross block boundaries when necessary.
641 */
642 int
643 ufs_read(f, start, size, resid)
644 struct open_file *f;
645 void *start;
646 size_t size;
647 size_t *resid; /* out */
648 {
649 register struct file *fp = (struct file *)f->f_fsdata;
650 register size_t csize;
651 char *buf;
652 size_t buf_size;
653 int rc = 0;
654 register char *addr = start;
655
656 while (size != 0) {
657 if (fp->f_seekp >= fp->f_di.di_size)
658 break;
659
660 rc = buf_read_file(f, &buf, &buf_size);
661 if (rc)
662 break;
663
664 csize = size;
665 if (csize > buf_size)
666 csize = buf_size;
667
668 bcopy(buf, addr, csize);
669
670 fp->f_seekp += csize;
671 addr += csize;
672 size -= csize;
673 }
674 if (resid)
675 *resid = size;
676 return (rc);
677 }
678
679 /*
680 * Not implemented.
681 */
682 #ifndef LIBSA_NO_FS_WRITE
683 int
684 ufs_write(f, start, size, resid)
685 struct open_file *f;
686 void *start;
687 size_t size;
688 size_t *resid; /* out */
689 {
690
691 return (EROFS);
692 }
693 #endif /* !LIBSA_NO_FS_WRITE */
694
695 #ifndef LIBSA_NO_FS_SEEK
696 off_t
697 ufs_seek(f, offset, where)
698 struct open_file *f;
699 off_t offset;
700 int where;
701 {
702 register struct file *fp = (struct file *)f->f_fsdata;
703
704 switch (where) {
705 case SEEK_SET:
706 fp->f_seekp = offset;
707 break;
708 case SEEK_CUR:
709 fp->f_seekp += offset;
710 break;
711 case SEEK_END:
712 fp->f_seekp = fp->f_di.di_size - offset;
713 break;
714 default:
715 return (-1);
716 }
717 return (fp->f_seekp);
718 }
719 #endif /* !LIBSA_NO_FS_SEEK */
720
721 int
722 ufs_stat(f, sb)
723 struct open_file *f;
724 struct stat *sb;
725 {
726 register struct file *fp = (struct file *)f->f_fsdata;
727
728 /* only important stuff */
729 sb->st_mode = fp->f_di.di_mode;
730 sb->st_uid = fp->f_di.di_uid;
731 sb->st_gid = fp->f_di.di_gid;
732 sb->st_size = fp->f_di.di_size;
733 return (0);
734 }
735
736 #ifdef COMPAT_UFS
737 /*
738 * Sanity checks for old file systems.
739 *
740 * XXX - goes away some day.
741 */
742 static void
743 ffs_oldfscompat(fs)
744 struct fs *fs;
745 {
746 int i;
747
748 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
749 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
750 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
751 fs->fs_nrpos = 8; /* XXX */
752 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
753 quad_t sizepb = fs->fs_bsize; /* XXX */
754 /* XXX */
755 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
756 for (i = 0; i < NIADDR; i++) { /* XXX */
757 sizepb *= NINDIR(fs); /* XXX */
758 fs->fs_maxfilesize += sizepb; /* XXX */
759 } /* XXX */
760 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
761 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
762 } /* XXX */
763 }
764 #endif
765