ufs.c revision 1.10 1 /* $NetBSD: ufs.c,v 1.10 1995/01/07 20:53:31 ws 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;
383 u_int buf_size;
384 int nlinks = 0;
385 char namebuf[MAXPATHLEN+1];
386 char *buf = NULL;
387
388 /* allocate file system specific data structure */
389 fp = alloc(sizeof(struct file));
390 bzero(fp, sizeof(struct file));
391 f->f_fsdata = (void *)fp;
392
393 /* allocate space and read super block */
394 fs = alloc(SBSIZE);
395 fp->f_fs = fs;
396 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
397 SBLOCK, SBSIZE, (char *)fs, &buf_size);
398 if (rc)
399 goto out;
400
401 if (buf_size != SBSIZE || fs->fs_magic != FS_MAGIC ||
402 fs->fs_bsize > MAXBSIZE || fs->fs_bsize < sizeof(struct fs)) {
403 rc = EINVAL;
404 goto out;
405 }
406 #ifdef COMPAT_UFS
407 ffs_oldfscompat(fs);
408 #endif
409
410 /*
411 * Calculate indirect block levels.
412 */
413 {
414 register int mult;
415 register int level;
416
417 mult = 1;
418 for (level = 0; level < NIADDR; level++) {
419 mult *= NINDIR(fs);
420 fp->f_nindir[level] = mult;
421 }
422 }
423
424 inumber = ROOTINO;
425 if ((rc = read_inode(inumber, f)) != 0)
426 goto out;
427
428 cp = path;
429 while (*cp) {
430
431 /*
432 * Remove extra separators
433 */
434 while (*cp == '/')
435 cp++;
436 if (*cp == '\0')
437 break;
438
439 /*
440 * Check that current node is a directory.
441 */
442 if ((fp->f_di.di_mode & IFMT) != IFDIR) {
443 rc = ENOTDIR;
444 goto out;
445 }
446
447 /*
448 * Get next component of path name.
449 */
450 {
451 register int len = 0;
452
453 ncp = cp;
454 while ((c = *cp) != '\0' && c != '/') {
455 if (++len > MAXNAMLEN) {
456 rc = ENOENT;
457 goto out;
458 }
459 cp++;
460 }
461 *cp = '\0';
462 }
463
464 /*
465 * Look up component in current directory.
466 * Save directory inumber in case we find a
467 * symbolic link.
468 */
469 parent_inumber = inumber;
470 rc = search_directory(ncp, f, &inumber);
471 *cp = c;
472 if (rc)
473 goto out;
474
475 /*
476 * Open next component.
477 */
478 if ((rc = read_inode(inumber, f)) != 0)
479 goto out;
480
481 /*
482 * Check for symbolic link.
483 */
484 if ((fp->f_di.di_mode & IFMT) == IFLNK) {
485 int link_len = fp->f_di.di_size;
486 int len;
487
488 len = strlen(cp);
489
490 if (link_len + len > MAXPATHLEN ||
491 ++nlinks > MAXSYMLINKS) {
492 rc = ENOENT;
493 goto out;
494 }
495
496 bcopy(cp, &namebuf[link_len], len + 1);
497
498 if (link_len < fs->fs_maxsymlinklen) {
499 bcopy(fp->f_di.di_shortlink, namebuf,
500 (unsigned) link_len);
501 } else {
502 /*
503 * Read file for symbolic link
504 */
505 u_int buf_size;
506 daddr_t disk_block;
507 register struct fs *fs = fp->f_fs;
508
509 if (!buf)
510 buf = alloc(fs->fs_bsize);
511 rc = block_map(f, (daddr_t)0, &disk_block);
512 if (rc)
513 goto out;
514
515 rc = (f->f_dev->dv_strategy)(f->f_devdata,
516 F_READ, fsbtodb(fs, disk_block),
517 fs->fs_bsize, buf, &buf_size);
518 if (rc)
519 goto out;
520
521 bcopy((char *)buf, namebuf, (unsigned)link_len);
522 }
523
524 /*
525 * If relative pathname, restart at parent directory.
526 * If absolute pathname, restart at root.
527 */
528 cp = namebuf;
529 if (*cp != '/')
530 inumber = parent_inumber;
531 else
532 inumber = (ino_t)ROOTINO;
533
534 if ((rc = read_inode(inumber, f)) != 0)
535 goto out;
536 }
537 }
538
539 /*
540 * Found terminal component.
541 */
542 rc = 0;
543 out:
544 if (buf)
545 free(buf, fs->fs_bsize);
546 if (rc)
547 free(fp, sizeof(struct file));
548 return (rc);
549 }
550
551 int
552 ufs_close(f)
553 struct open_file *f;
554 {
555 register struct file *fp = (struct file *)f->f_fsdata;
556 int level;
557
558 f->f_fsdata = (void *)0;
559 if (fp == (struct file *)0)
560 return (0);
561
562 for (level = 0; level < NIADDR; level++) {
563 if (fp->f_blk[level])
564 free(fp->f_blk[level], fp->f_fs->fs_bsize);
565 }
566 if (fp->f_buf)
567 free(fp->f_buf, fp->f_fs->fs_bsize);
568 free(fp->f_fs, SBSIZE);
569 free(fp, sizeof(struct file));
570 return (0);
571 }
572
573 /*
574 * Copy a portion of a file into kernel memory.
575 * Cross block boundaries when necessary.
576 */
577 int
578 ufs_read(f, start, size, resid)
579 struct open_file *f;
580 char *start;
581 u_int size;
582 u_int *resid; /* out */
583 {
584 register struct file *fp = (struct file *)f->f_fsdata;
585 register u_int csize;
586 char *buf;
587 u_int buf_size;
588 int rc = 0;
589
590 while (size != 0) {
591 if (fp->f_seekp >= fp->f_di.di_size)
592 break;
593
594 rc = buf_read_file(f, &buf, &buf_size);
595 if (rc)
596 break;
597
598 csize = size;
599 if (csize > buf_size)
600 csize = buf_size;
601
602 bcopy(buf, start, csize);
603
604 fp->f_seekp += csize;
605 start += csize;
606 size -= csize;
607 }
608 if (resid)
609 *resid = size;
610 return (rc);
611 }
612
613 /*
614 * Not implemented.
615 */
616 int
617 ufs_write(f, start, size, resid)
618 struct open_file *f;
619 char *start;
620 u_int size;
621 u_int *resid; /* out */
622 {
623
624 return (EROFS);
625 }
626
627 off_t
628 ufs_seek(f, offset, where)
629 struct open_file *f;
630 off_t offset;
631 int where;
632 {
633 register struct file *fp = (struct file *)f->f_fsdata;
634
635 switch (where) {
636 case SEEK_SET:
637 fp->f_seekp = offset;
638 break;
639 case SEEK_CUR:
640 fp->f_seekp += offset;
641 break;
642 case SEEK_END:
643 fp->f_seekp = fp->f_di.di_size - offset;
644 break;
645 default:
646 return (-1);
647 }
648 return (fp->f_seekp);
649 }
650
651 int
652 ufs_stat(f, sb)
653 struct open_file *f;
654 struct stat *sb;
655 {
656 register struct file *fp = (struct file *)f->f_fsdata;
657
658 /* only important stuff */
659 sb->st_mode = fp->f_di.di_mode;
660 sb->st_uid = fp->f_di.di_uid;
661 sb->st_gid = fp->f_di.di_gid;
662 sb->st_size = fp->f_di.di_size;
663 return (0);
664 }
665
666 #ifdef COMPAT_UFS
667 /*
668 * Sanity checks for old file systems.
669 *
670 * XXX - goes away some day.
671 */
672 void
673 ffs_oldfscompat(fs)
674 struct fs *fs;
675 {
676 int i;
677
678 fs->fs_npsect = max(fs->fs_npsect, fs->fs_nsect); /* XXX */
679 fs->fs_interleave = max(fs->fs_interleave, 1); /* XXX */
680 if (fs->fs_postblformat == FS_42POSTBLFMT) /* XXX */
681 fs->fs_nrpos = 8; /* XXX */
682 if (fs->fs_inodefmt < FS_44INODEFMT) { /* XXX */
683 quad_t sizepb = fs->fs_bsize; /* XXX */
684 /* XXX */
685 fs->fs_maxfilesize = fs->fs_bsize * NDADDR - 1; /* XXX */
686 for (i = 0; i < NIADDR; i++) { /* XXX */
687 sizepb *= NINDIR(fs); /* XXX */
688 fs->fs_maxfilesize += sizepb; /* XXX */
689 } /* XXX */
690 fs->fs_qbmask = ~fs->fs_bmask; /* XXX */
691 fs->fs_qfmask = ~fs->fs_fmask; /* XXX */
692 } /* XXX */
693 }
694 #endif
695