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