ufs.c revision 1.3 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.3 1994/06/20 08:39:01 pk 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
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 /*
100 * Read a new inode into a file structure.
101 */
102 static int
103 read_inode(inumber, f)
104 ino_t inumber;
105 struct open_file *f;
106 {
107 register struct file *fp = (struct file *)f->f_fsdata;
108 register struct fs *fs = fp->f_fs;
109 char *buf;
110 u_int rsize;
111 int rc;
112
113 /*
114 * Read inode and save it.
115 */
116 buf = alloc(fs->fs_bsize);
117 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
118 fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize, buf, &rsize);
119 if (rc)
120 goto out;
121 if (rsize != fs->fs_bsize) {
122 rc = EIO;
123 goto out;
124 }
125
126 {
127 register struct dinode *dp;
128
129 dp = (struct dinode *)buf;
130 fp->f_di = dp[ino_to_fsbo(fs, inumber)];
131 }
132
133 /*
134 * Clear out the old buffers
135 */
136 {
137 register int level;
138
139 for (level = 0; level < NIADDR; level++)
140 fp->f_blkno[level] = -1;
141 fp->f_buf_blkno = -1;
142 }
143 out:
144 free(buf, fs->fs_bsize);
145 return (0);
146 }
147
148 /*
149 * Given an offset in a file, find the disk block number that
150 * contains that block.
151 */
152 static int
153 block_map(f, file_block, disk_block_p)
154 struct open_file *f;
155 daddr_t file_block;
156 daddr_t *disk_block_p; /* out */
157 {
158 register struct file *fp = (struct file *)f->f_fsdata;
159 register struct fs *fs = fp->f_fs;
160 int level;
161 int idx;
162 daddr_t ind_block_num;
163 daddr_t *ind_p;
164 int rc;
165
166 /*
167 * Index structure of an inode:
168 *
169 * di_db[0..NDADDR-1] hold block numbers for blocks
170 * 0..NDADDR-1
171 *
172 * di_ib[0] index block 0 is the single indirect block
173 * holds block numbers for blocks
174 * NDADDR .. NDADDR + NINDIR(fs)-1
175 *
176 * di_ib[1] index block 1 is the double indirect block
177 * holds block numbers for INDEX blocks for blocks
178 * NDADDR + NINDIR(fs) ..
179 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
180 *
181 * di_ib[2] index block 2 is the triple indirect block
182 * holds block numbers for double-indirect
183 * blocks for blocks
184 * NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
185 * NDADDR + NINDIR(fs) + NINDIR(fs)**2
186 * + NINDIR(fs)**3 - 1
187 */
188
189 if (file_block < NDADDR) {
190 /* Direct block. */
191 *disk_block_p = fp->f_di.di_db[file_block];
192 return (0);
193 }
194
195 file_block -= NDADDR;
196
197 /*
198 * nindir[0] = NINDIR
199 * nindir[1] = NINDIR**2
200 * nindir[2] = NINDIR**3
201 * etc
202 */
203 for (level = 0; level < NIADDR; level++) {
204 if (file_block < fp->f_nindir[level])
205 break;
206 file_block -= fp->f_nindir[level];
207 }
208 if (level == NIADDR) {
209 /* Block number too high */
210 return (EFBIG);
211 }
212
213 ind_block_num = fp->f_di.di_ib[level];
214
215 for (; level >= 0; level--) {
216 if (ind_block_num == 0) {
217 *disk_block_p = 0; /* missing */
218 return (0);
219 }
220
221 if (fp->f_blkno[level] != ind_block_num) {
222 if (fp->f_blk[level] == (char *)0)
223 fp->f_blk[level] =
224 alloc(fs->fs_bsize);
225 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
226 fsbtodb(fp->f_fs, ind_block_num),
227 fs->fs_bsize,
228 fp->f_blk[level],
229 (u_int *)&fp->f_blksize[level]);
230 if (rc)
231 return (rc);
232 if (fp->f_blksize[level] != fs->fs_bsize)
233 return (EIO);
234 fp->f_blkno[level] = ind_block_num;
235 }
236
237 ind_p = (daddr_t *)fp->f_blk[level];
238
239 if (level > 0) {
240 idx = file_block / fp->f_nindir[level - 1];
241 file_block %= fp->f_nindir[level - 1];
242 } else
243 idx = file_block;
244
245 ind_block_num = ind_p[idx];
246 }
247
248 *disk_block_p = ind_block_num;
249
250 return (0);
251 }
252
253 /*
254 * Read a portion of a file into an internal buffer. Return
255 * the location in the buffer and the amount in the buffer.
256 */
257 static int
258 buf_read_file(f, buf_p, size_p)
259 struct open_file *f;
260 char **buf_p; /* out */
261 u_int *size_p; /* out */
262 {
263 register struct file *fp = (struct file *)f->f_fsdata;
264 register struct fs *fs = fp->f_fs;
265 long off;
266 register daddr_t file_block;
267 daddr_t disk_block;
268 long block_size;
269 int rc;
270
271 off = blkoff(fs, fp->f_seekp);
272 file_block = lblkno(fs, fp->f_seekp);
273 block_size = dblksize(fs, &fp->f_di, file_block);
274
275 if (file_block != fp->f_buf_blkno) {
276 rc = block_map(f, file_block, &disk_block);
277 if (rc)
278 return (rc);
279
280 if (fp->f_buf == (char *)0)
281 fp->f_buf = alloc(fs->fs_bsize);
282
283 if (disk_block == 0) {
284 bzero(fp->f_buf, block_size);
285 fp->f_buf_size = block_size;
286 } else {
287 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
288 fsbtodb(fs, disk_block),
289 block_size, fp->f_buf, &fp->f_buf_size);
290 if (rc)
291 return (rc);
292 }
293
294 fp->f_buf_blkno = file_block;
295 }
296
297 /*
298 * Return address of byte in buffer corresponding to
299 * offset, and size of remainder of buffer after that
300 * byte.
301 */
302 *buf_p = fp->f_buf + off;
303 *size_p = block_size - off;
304
305 /*
306 * But truncate buffer at end of file.
307 */
308 if (*size_p > fp->f_di.di_size - fp->f_seekp)
309 *size_p = fp->f_di.di_size - fp->f_seekp;
310
311 return (0);
312 }
313
314 /*
315 * Search a directory for a name and return its
316 * i_number.
317 */
318 static int
319 search_directory(name, f, inumber_p)
320 char *name;
321 struct open_file *f;
322 ino_t *inumber_p; /* out */
323 {
324 register struct file *fp = (struct file *)f->f_fsdata;
325 register struct direct *dp;
326 struct direct *edp;
327 char *buf;
328 u_int buf_size;
329 int namlen, length;
330 int rc;
331
332 length = strlen(name);
333
334 fp->f_seekp = 0;
335 while (fp->f_seekp < fp->f_di.di_size) {
336 rc = buf_read_file(f, &buf, &buf_size);
337 if (rc)
338 return (rc);
339
340 dp = (struct direct *)buf;
341 edp = (struct direct *)(buf + buf_size);
342 while (dp < edp) {
343 if (dp->d_ino == (ino_t)0)
344 goto next;
345 #if 0
346 #if BYTE_ORDER == LITTLE_ENDIAN
347 if (fp->f_fs->fs_maxsymlinklen <= 0)
348 namlen = dp->d_type;
349 else
350 #endif
351 #endif
352 namlen = dp->d_namlen;
353 if (namlen == length &&
354 !strcmp(name, dp->d_name)) {
355 /* found entry */
356 *inumber_p = dp->d_ino;
357 return (0);
358 }
359 next:
360 dp = (struct direct *)((char *)dp + dp->d_reclen);
361 }
362 fp->f_seekp += buf_size;
363 }
364 return (ENOENT);
365 }
366
367 /*
368 * Open a file.
369 */
370 int
371 ufs_open(path, f)
372 char *path;
373 struct open_file *f;
374 {
375 register char *cp, *ncp;
376 register int c;
377 ino_t inumber, parent_inumber;
378 int nlinks = 0;
379 struct file *fp;
380 struct fs *fs;
381 int rc;
382 u_int buf_size;
383 #if 0
384 char namebuf[MAXPATHLEN+1];
385 #endif
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
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