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