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