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