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