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