1 /* $NetBSD: ufs.c,v 1.20 1998/03/01 07:15:39 ross Exp $ */
2
3 /*-
4 * Copyright (c) 2002 Networks Associates Technology, Inc.
5 * All rights reserved.
6 *
7 * This software was developed for the FreeBSD Project by Marshall
8 * Kirk McKusick and Network Associates Laboratories, the Security
9 * Research Division of Network Associates, Inc. under DARPA/SPAWAR
10 * contract N66001-01-C-8035 ("CBOSS"), as part of the DARPA CHATS
11 * research program
12 *
13 * Copyright (c) 1982, 1989, 1993
14 * The Regents of the University of California. All rights reserved.
15 *
16 * This code is derived from software contributed to Berkeley by
17 * The Mach Operating System project at Carnegie-Mellon University.
18 *
19 * Redistribution and use in source and binary forms, with or without
20 * modification, are permitted provided that the following conditions
21 * are met:
22 * 1. Redistributions of source code must retain the above copyright
23 * notice, this list of conditions and the following disclaimer.
24 * 2. Redistributions in binary form must reproduce the above copyright
25 * notice, this list of conditions and the following disclaimer in the
26 * documentation and/or other materials provided with the distribution.
27 * 3. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
30 *
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * SUCH DAMAGE.
42 *
43 *
44 * Copyright (c) 1990, 1991 Carnegie Mellon University
45 * All Rights Reserved.
46 *
47 * Author: David Golub
48 *
49 * Permission to use, copy, modify and distribute this software and its
50 * documentation is hereby granted, provided that both the copyright
51 * notice and this permission notice appear in all copies of the
52 * software, derivative works or modified versions, and any portions
53 * thereof, and that both notices appear in supporting documentation.
54 *
55 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
57 * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58 *
59 * Carnegie Mellon requests users of this software to return to
60 *
61 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
62 * School of Computer Science
63 * Carnegie Mellon University
64 * Pittsburgh PA 15213-3890
65 *
66 * any improvements or extensions that they make and grant Carnegie the
67 * rights to redistribute these changes.
68 */
69
70 /*
71 * Stand-alone file reading package.
72 */
73
74 #include <sys/param.h>
75 #include <sys/disklabel.h>
76 #include <sys/time.h>
77 #include <ufs/ufs/dinode.h>
78 #include <ufs/ufs/dir.h>
79 #include <ufs/ffs/fs.h>
80 #include "stand.h"
81 #include "string.h"
82
83 static int ufs_open(const char *path, struct open_file *f);
84 static int ufs_write(struct open_file *f, const void *buf, size_t size,
85 size_t *resid);
86 static int ufs_close(struct open_file *f);
87 static int ufs_read(struct open_file *f, void *buf, size_t size, size_t *resid);
88 static off_t ufs_seek(struct open_file *f, off_t offset, int where);
89 static int ufs_stat(struct open_file *f, struct stat *sb);
90 static int ufs_readdir(struct open_file *f, struct dirent *d);
91 static int ufs_mount(const char *dev, const char *path, void **data);
92 static int ufs_unmount(const char *dev, void *data);
93
94 struct fs_ops ufs_fsops = {
95 .fs_name = "ufs",
96 .fo_open = ufs_open,
97 .fo_close = ufs_close,
98 .fo_read = ufs_read,
99 .fo_write = ufs_write,
100 .fo_seek = ufs_seek,
101 .fo_stat = ufs_stat,
102 .fo_readdir = ufs_readdir,
103 .fo_mount = ufs_mount,
104 .fo_unmount = ufs_unmount
105 };
106
107 /*
108 * In-core open file.
109 */
110 struct file {
111 off_t f_seekp; /* seek pointer */
112 struct fs *f_fs; /* pointer to super-block */
113 union dinode f_dp; /* copy of on-disk inode */
114 int f_nindir[UFS_NIADDR];
115 /* number of blocks mapped by
116 indirect block at level i */
117 char *f_blk[UFS_NIADDR]; /* buffer for indirect block at
118 level i */
119 size_t f_blksize[UFS_NIADDR];
120 /* size of buffer */
121 ufs2_daddr_t f_blkno[UFS_NIADDR];/* disk address of block in buffer */
122 ufs2_daddr_t f_buf_blkno; /* block number of data block */
123 char *f_buf; /* buffer for data block */
124 size_t f_buf_size; /* size of data block */
125 int f_inumber; /* inumber */
126 };
127 #define DIP(fp, field) \
128 ((fp)->f_fs->fs_magic == FS_UFS1_MAGIC ? \
129 (fp)->f_dp.dp1.field : (fp)->f_dp.dp2.field)
130
131 typedef struct ufs_mnt {
132 char *um_dev;
133 int um_fd;
134 STAILQ_ENTRY(ufs_mnt) um_link;
135 } ufs_mnt_t;
136
137 typedef STAILQ_HEAD(ufs_mnt_list, ufs_mnt) ufs_mnt_list_t;
138 static ufs_mnt_list_t mnt_list = STAILQ_HEAD_INITIALIZER(mnt_list);
139
140 static int read_inode(ino_t, struct open_file *);
141 static int block_map(struct open_file *, ufs2_daddr_t, ufs2_daddr_t *);
142 static int buf_read_file(struct open_file *, char **, size_t *);
143 static int buf_write_file(struct open_file *, const char *, size_t *);
144 static int search_directory(char *, struct open_file *, ino_t *);
145 static int ufs_use_sa_read(void *, off_t, void **, int);
146
147 /* from ffs_subr.c */
148 int ffs_sbget(void *devfd, struct fs **fsp, off_t sblock, int flags,
149 char *filltype,
150 int (*readfunc)(void *devfd, off_t loc, void **bufp, int size));
151 int ffs_sbsearch(void *, struct fs **, int, char *,
152 int (*)(void *, off_t, void **, int));
153
154 /*
155 * Read a new inode into a file structure.
156 */
157 static int
read_inode(ino_t inumber,struct open_file * f)158 read_inode(ino_t inumber, struct open_file *f)
159 {
160 struct file *fp = (struct file *)f->f_fsdata;
161 struct fs *fs = fp->f_fs;
162 char *buf;
163 size_t rsize;
164 int rc;
165
166 if (fs == NULL)
167 panic("fs == NULL");
168
169 /*
170 * Read inode and save it.
171 */
172 buf = malloc(fs->fs_bsize);
173 twiddle(1);
174 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
175 fsbtodb(fs, ino_to_fsba(fs, inumber)), fs->fs_bsize,
176 buf, &rsize);
177 if (rc)
178 goto out;
179 if (rsize != fs->fs_bsize) {
180 rc = EIO;
181 goto out;
182 }
183
184 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
185 fp->f_dp.dp1 = ((struct ufs1_dinode *)buf)
186 [ino_to_fsbo(fs, inumber)];
187 else
188 fp->f_dp.dp2 = ((struct ufs2_dinode *)buf)
189 [ino_to_fsbo(fs, inumber)];
190
191 /*
192 * Clear out the old buffers
193 */
194 {
195 int level;
196
197 for (level = 0; level < UFS_NIADDR; level++)
198 fp->f_blkno[level] = -1;
199 fp->f_buf_blkno = -1;
200 }
201 fp->f_seekp = 0;
202 fp->f_inumber = inumber;
203 out:
204 free(buf);
205 return (rc);
206 }
207
208 /*
209 * Given an offset in a file, find the disk block number that
210 * contains that block.
211 */
212 static int
block_map(struct open_file * f,ufs2_daddr_t file_block,ufs2_daddr_t * disk_block_p)213 block_map(struct open_file *f, ufs2_daddr_t file_block,
214 ufs2_daddr_t *disk_block_p)
215 {
216 struct file *fp = (struct file *)f->f_fsdata;
217 struct fs *fs = fp->f_fs;
218 int level;
219 int idx;
220 ufs2_daddr_t ind_block_num;
221 int rc;
222
223 /*
224 * Index structure of an inode:
225 *
226 * di_db[0..UFS_NDADDR-1] hold block numbers for blocks
227 * 0..UFS_NDADDR-1
228 *
229 * di_ib[0] index block 0 is the single indirect block
230 * holds block numbers for blocks
231 * UFS_NDADDR .. UFS_NDADDR + NINDIR(fs)-1
232 *
233 * di_ib[1] index block 1 is the double indirect block
234 * holds block numbers for INDEX blocks for blocks
235 * UFS_NDADDR + NINDIR(fs) ..
236 * UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2 - 1
237 *
238 * di_ib[2] index block 2 is the triple indirect block
239 * holds block numbers for double-indirect
240 * blocks for blocks
241 * UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2 ..
242 * UFS_NDADDR + NINDIR(fs) + NINDIR(fs)**2
243 * + NINDIR(fs)**3 - 1
244 */
245
246 if (file_block < UFS_NDADDR) {
247 /* Direct block. */
248 *disk_block_p = DIP(fp, di_db[file_block]);
249 return (0);
250 }
251
252 file_block -= UFS_NDADDR;
253
254 /*
255 * nindir[0] = NINDIR
256 * nindir[1] = NINDIR**2
257 * nindir[2] = NINDIR**3
258 * etc
259 */
260 for (level = 0; level < UFS_NIADDR; level++) {
261 if (file_block < fp->f_nindir[level])
262 break;
263 file_block -= fp->f_nindir[level];
264 }
265 if (level == UFS_NIADDR) {
266 /* Block number too high */
267 return (EFBIG);
268 }
269
270 ind_block_num = DIP(fp, di_ib[level]);
271
272 for (; level >= 0; level--) {
273 if (ind_block_num == 0) {
274 *disk_block_p = 0; /* missing */
275 return (0);
276 }
277
278 if (fp->f_blkno[level] != ind_block_num) {
279 if (fp->f_blk[level] == (char *)0)
280 fp->f_blk[level] =
281 malloc(fs->fs_bsize);
282 twiddle(1);
283 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
284 fsbtodb(fp->f_fs, ind_block_num),
285 fs->fs_bsize,
286 fp->f_blk[level],
287 &fp->f_blksize[level]);
288 if (rc)
289 return (rc);
290 if (fp->f_blksize[level] != fs->fs_bsize)
291 return (EIO);
292 fp->f_blkno[level] = ind_block_num;
293 }
294
295 if (level > 0) {
296 idx = file_block / fp->f_nindir[level - 1];
297 file_block %= fp->f_nindir[level - 1];
298 } else
299 idx = file_block;
300
301 if (fp->f_fs->fs_magic == FS_UFS1_MAGIC)
302 ind_block_num = ((ufs1_daddr_t *)fp->f_blk[level])[idx];
303 else
304 ind_block_num = ((ufs2_daddr_t *)fp->f_blk[level])[idx];
305 }
306
307 *disk_block_p = ind_block_num;
308
309 return (0);
310 }
311
312 /*
313 * Write a portion of a file from an internal buffer.
314 */
315 static int
buf_write_file(struct open_file * f,const char * buf_p,size_t * size_p)316 buf_write_file(struct open_file *f, const char *buf_p, size_t *size_p)
317 {
318 struct file *fp = (struct file *)f->f_fsdata;
319 struct fs *fs = fp->f_fs;
320 long off;
321 ufs_lbn_t file_block;
322 ufs2_daddr_t disk_block;
323 size_t block_size;
324 int rc;
325
326 /*
327 * Calculate the starting block address and offset.
328 */
329 off = blkoff(fs, fp->f_seekp);
330 file_block = lblkno(fs, fp->f_seekp);
331 block_size = sblksize(fs, DIP(fp, di_size), file_block);
332
333 rc = block_map(f, file_block, &disk_block);
334 if (rc)
335 return (rc);
336
337 if (disk_block == 0)
338 /* Because we can't allocate space on the drive */
339 return (EFBIG);
340
341 /*
342 * Truncate buffer at end of file, and at the end of
343 * this block.
344 */
345 if (*size_p > DIP(fp, di_size) - fp->f_seekp)
346 *size_p = DIP(fp, di_size) - fp->f_seekp;
347 if (*size_p > block_size - off)
348 *size_p = block_size - off;
349
350 /*
351 * If we don't entirely occlude the block and it's not
352 * in memory already, read it in first.
353 */
354 if (((off > 0) || (*size_p + off < block_size)) &&
355 (file_block != fp->f_buf_blkno)) {
356
357 if (fp->f_buf == (char *)0)
358 fp->f_buf = malloc(fs->fs_bsize);
359
360 twiddle(4);
361 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
362 fsbtodb(fs, disk_block),
363 block_size, fp->f_buf, &fp->f_buf_size);
364 if (rc)
365 return (rc);
366
367 fp->f_buf_blkno = file_block;
368 }
369
370 /*
371 * Copy the user data into the cached block.
372 */
373 bcopy(buf_p, fp->f_buf + off, *size_p);
374
375 /*
376 * Write the block out to storage.
377 */
378
379 twiddle(4);
380 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_WRITE,
381 fsbtodb(fs, disk_block),
382 block_size, fp->f_buf, &fp->f_buf_size);
383 return (rc);
384 }
385
386 /*
387 * Read a portion of a file into an internal buffer. Return
388 * the location in the buffer and the amount in the buffer.
389 */
390 static int
buf_read_file(struct open_file * f,char ** buf_p,size_t * size_p)391 buf_read_file(struct open_file *f, char **buf_p, size_t *size_p)
392 {
393 struct file *fp = (struct file *)f->f_fsdata;
394 struct fs *fs = fp->f_fs;
395 long off;
396 ufs_lbn_t file_block;
397 ufs2_daddr_t disk_block;
398 size_t block_size;
399 int rc;
400
401 off = blkoff(fs, fp->f_seekp);
402 file_block = lblkno(fs, fp->f_seekp);
403 block_size = sblksize(fs, DIP(fp, di_size), file_block);
404
405 if (file_block != fp->f_buf_blkno) {
406 if (fp->f_buf == NULL)
407 fp->f_buf = malloc(fs->fs_bsize);
408
409 rc = block_map(f, file_block, &disk_block);
410 if (rc)
411 return (rc);
412
413 if (disk_block == 0) {
414 bzero(fp->f_buf, block_size);
415 fp->f_buf_size = block_size;
416 } else {
417 twiddle(4);
418 rc = (f->f_dev->dv_strategy)(f->f_devdata, F_READ,
419 fsbtodb(fs, disk_block),
420 block_size, fp->f_buf, &fp->f_buf_size);
421 if (rc)
422 return (rc);
423 }
424
425 fp->f_buf_blkno = file_block;
426 }
427
428 /*
429 * Return address of byte in buffer corresponding to
430 * offset, and size of remainder of buffer after that
431 * byte.
432 */
433 *buf_p = fp->f_buf + off;
434 *size_p = block_size - off;
435
436 /*
437 * But truncate buffer at end of file.
438 */
439 if (*size_p > DIP(fp, di_size) - fp->f_seekp)
440 *size_p = DIP(fp, di_size) - fp->f_seekp;
441
442 return (0);
443 }
444
445 /*
446 * Search a directory for a name and return its
447 * i_number.
448 */
449 static int
search_directory(char * name,struct open_file * f,ino_t * inumber_p)450 search_directory(char *name, struct open_file *f, ino_t *inumber_p)
451 {
452 struct file *fp = (struct file *)f->f_fsdata;
453 struct direct *dp;
454 struct direct *edp;
455 char *buf;
456 size_t buf_size;
457 int namlen, length;
458 int rc;
459
460 length = strlen(name);
461
462 fp->f_seekp = 0;
463 while (fp->f_seekp < DIP(fp, di_size)) {
464 rc = buf_read_file(f, &buf, &buf_size);
465 if (rc)
466 return (rc);
467
468 dp = (struct direct *)buf;
469 edp = (struct direct *)(buf + buf_size);
470 while (dp < edp) {
471 if (dp->d_ino == (ino_t)0)
472 goto next;
473 #if BYTE_ORDER == LITTLE_ENDIAN
474 if (fp->f_fs->fs_maxsymlinklen <= 0)
475 namlen = dp->d_type;
476 else
477 #endif
478 namlen = dp->d_namlen;
479 if (namlen == length &&
480 !strcmp(name, dp->d_name)) {
481 /* found entry */
482 *inumber_p = dp->d_ino;
483 return (0);
484 }
485 next:
486 dp = (struct direct *)((char *)dp + dp->d_reclen);
487 }
488 fp->f_seekp += buf_size;
489 }
490 return (ENOENT);
491 }
492
493 /*
494 * Open a file.
495 */
496 static int
ufs_open(const char * upath,struct open_file * f)497 ufs_open(const char *upath, struct open_file *f)
498 {
499 char *cp, *ncp;
500 int c;
501 ino_t inumber, parent_inumber;
502 struct file *fp;
503 struct fs *fs;
504 int rc;
505 int nlinks = 0;
506 char namebuf[MAXPATHLEN+1];
507 char *buf = NULL;
508 char *path = NULL;
509 const char *dev;
510 ufs_mnt_t *mnt;
511
512 /* allocate file system specific data structure */
513 errno = 0;
514 fp = calloc(1, sizeof(struct file));
515 if (fp == NULL)
516 return (errno);
517 f->f_fsdata = (void *)fp;
518
519 dev = devformat((struct devdesc *)f->f_devdata);
520 /* Is this device mounted? */
521 STAILQ_FOREACH(mnt, &mnt_list, um_link) {
522 if (strcmp(dev, mnt->um_dev) == 0)
523 break;
524 }
525
526 if (mnt == NULL) {
527 /* read super block */
528 twiddle(1);
529 if ((rc = ffs_sbget(f, &fs, UFS_STDSB, UFS_NOHASHFAIL, "stand",
530 ufs_use_sa_read)) != 0) {
531 goto out;
532 }
533 } else {
534 struct open_file *sbf;
535 struct file *sfp;
536
537 /* get superblock from mounted file system */
538 sbf = fd2open_file(mnt->um_fd);
539 sfp = sbf->f_fsdata;
540 fs = sfp->f_fs;
541 }
542 fp->f_fs = fs;
543
544 /*
545 * Calculate indirect block levels.
546 */
547 {
548 ufs2_daddr_t mult;
549 int level;
550
551 mult = 1;
552 for (level = 0; level < UFS_NIADDR; level++) {
553 mult *= NINDIR(fs);
554 fp->f_nindir[level] = mult;
555 }
556 }
557
558 inumber = UFS_ROOTINO;
559 if ((rc = read_inode(inumber, f)) != 0)
560 goto out;
561
562 cp = path = strdup(upath);
563 if (path == NULL) {
564 rc = ENOMEM;
565 goto out;
566 }
567 while (*cp) {
568
569 /*
570 * Remove extra separators
571 */
572 while (*cp == '/')
573 cp++;
574 if (*cp == '\0')
575 break;
576
577 /*
578 * Check that current node is a directory.
579 */
580 if ((DIP(fp, di_mode) & IFMT) != IFDIR) {
581 rc = ENOTDIR;
582 goto out;
583 }
584
585 /*
586 * Get next component of path name.
587 */
588 {
589 int len = 0;
590
591 ncp = cp;
592 while ((c = *cp) != '\0' && c != '/') {
593 if (++len > UFS_MAXNAMLEN) {
594 rc = ENOENT;
595 goto out;
596 }
597 cp++;
598 }
599 *cp = '\0';
600 }
601
602 /*
603 * Look up component in current directory.
604 * Save directory inumber in case we find a
605 * symbolic link.
606 */
607 parent_inumber = inumber;
608 rc = search_directory(ncp, f, &inumber);
609 *cp = c;
610 if (rc)
611 goto out;
612
613 /*
614 * Open next component.
615 */
616 if ((rc = read_inode(inumber, f)) != 0)
617 goto out;
618
619 /*
620 * Check for symbolic link.
621 */
622 if ((DIP(fp, di_mode) & IFMT) == IFLNK) {
623 int link_len = DIP(fp, di_size);
624 int len;
625
626 len = strlen(cp);
627
628 if (link_len + len > MAXPATHLEN ||
629 ++nlinks > MAXSYMLINKS) {
630 rc = ENOENT;
631 goto out;
632 }
633
634 bcopy(cp, &namebuf[link_len], len + 1);
635
636 if (link_len < fs->fs_maxsymlinklen) {
637 bcopy(DIP(fp, di_shortlink), namebuf,
638 (unsigned) link_len);
639 } else {
640 /*
641 * Read file for symbolic link
642 */
643 size_t buf_size;
644 ufs2_daddr_t disk_block;
645 struct fs *fs = fp->f_fs;
646
647 if (!buf)
648 buf = malloc(fs->fs_bsize);
649 rc = block_map(f, (ufs2_daddr_t)0, &disk_block);
650 if (rc)
651 goto out;
652
653 twiddle(1);
654 rc = (f->f_dev->dv_strategy)(f->f_devdata,
655 F_READ, fsbtodb(fs, disk_block),
656 fs->fs_bsize, buf, &buf_size);
657 if (rc)
658 goto out;
659
660 bcopy((char *)buf, namebuf, (unsigned)link_len);
661 }
662
663 /*
664 * If relative pathname, restart at parent directory.
665 * If absolute pathname, restart at root.
666 */
667 cp = namebuf;
668 if (*cp != '/')
669 inumber = parent_inumber;
670 else
671 inumber = (ino_t)UFS_ROOTINO;
672
673 if ((rc = read_inode(inumber, f)) != 0)
674 goto out;
675 }
676 }
677
678 /*
679 * Found terminal component.
680 */
681 rc = 0;
682 fp->f_seekp = 0;
683 out:
684 free(buf);
685 free(path);
686 if (rc) {
687 free(fp->f_buf);
688
689 if (mnt == NULL && fp->f_fs != NULL) {
690 free(fp->f_fs->fs_csp);
691 free(fp->f_fs->fs_si);
692 free(fp->f_fs);
693 }
694 free(fp);
695 }
696 return (rc);
697 }
698
699 /*
700 * A read function for use by standalone-layer routines.
701 */
702 static int
ufs_use_sa_read(void * devfd,off_t loc,void ** bufp,int size)703 ufs_use_sa_read(void *devfd, off_t loc, void **bufp, int size)
704 {
705 struct open_file *f;
706 size_t buf_size;
707 int error;
708
709 f = (struct open_file *)devfd;
710 if ((*bufp = malloc(size)) == NULL)
711 return (ENOSPC);
712 error = (f->f_dev->dv_strategy)(f->f_devdata, F_READ, loc / DEV_BSIZE,
713 size, *bufp, &buf_size);
714 if (error != 0)
715 return (error);
716 if (buf_size != size)
717 return (EIO);
718 return (0);
719 }
720
721 static int
ufs_close(struct open_file * f)722 ufs_close(struct open_file *f)
723 {
724 ufs_mnt_t *mnt;
725 struct file *fp = (struct file *)f->f_fsdata;
726 int level;
727 char *dev;
728
729 f->f_fsdata = NULL;
730 if (fp == NULL)
731 return (0);
732
733 for (level = 0; level < UFS_NIADDR; level++) {
734 free(fp->f_blk[level]);
735 }
736 free(fp->f_buf);
737
738 dev = devformat((struct devdesc *)f->f_devdata);
739 STAILQ_FOREACH(mnt, &mnt_list, um_link) {
740 if (strcmp(dev, mnt->um_dev) == 0)
741 break;
742 }
743
744 if (mnt == NULL && fp->f_fs != NULL) {
745 free(fp->f_fs->fs_csp);
746 free(fp->f_fs->fs_si);
747 free(fp->f_fs);
748 }
749
750 free(fp);
751 return (0);
752 }
753
754 /*
755 * Copy a portion of a file into kernel memory.
756 * Cross block boundaries when necessary.
757 */
758 static int
ufs_read(struct open_file * f,void * start,size_t size,size_t * resid)759 ufs_read(struct open_file *f, void *start, size_t size, size_t *resid)
760 {
761 struct file *fp = (struct file *)f->f_fsdata;
762 size_t csize;
763 char *buf;
764 size_t buf_size;
765 int rc = 0;
766 char *addr = start;
767
768 while (size != 0) {
769 if (fp->f_seekp >= DIP(fp, di_size))
770 break;
771
772 rc = buf_read_file(f, &buf, &buf_size);
773 if (rc)
774 break;
775
776 csize = size;
777 if (csize > buf_size)
778 csize = buf_size;
779
780 bcopy(buf, addr, csize);
781
782 fp->f_seekp += csize;
783 addr += csize;
784 size -= csize;
785 }
786 if (resid)
787 *resid = size;
788 return (rc);
789 }
790
791 /*
792 * Write to a portion of an already allocated file.
793 * Cross block boundaries when necessary. Can not
794 * extend the file.
795 */
796 static int
ufs_write(struct open_file * f,const void * start,size_t size,size_t * resid)797 ufs_write(struct open_file *f, const void *start, size_t size, size_t *resid)
798 {
799 struct file *fp = (struct file *)f->f_fsdata;
800 size_t csize;
801 int rc = 0;
802 const char *addr = start;
803
804 csize = size;
805 while ((size != 0) && (csize != 0)) {
806 if (fp->f_seekp >= DIP(fp, di_size))
807 break;
808
809 if (csize >= 512) csize = 512; /* XXX */
810
811 rc = buf_write_file(f, addr, &csize);
812 if (rc)
813 break;
814
815 fp->f_seekp += csize;
816 addr += csize;
817 size -= csize;
818 }
819 if (resid)
820 *resid = size;
821 return (rc);
822 }
823
824 static off_t
ufs_seek(struct open_file * f,off_t offset,int where)825 ufs_seek(struct open_file *f, off_t offset, int where)
826 {
827 struct file *fp = (struct file *)f->f_fsdata;
828
829 switch (where) {
830 case SEEK_SET:
831 fp->f_seekp = offset;
832 break;
833 case SEEK_CUR:
834 fp->f_seekp += offset;
835 break;
836 case SEEK_END:
837 fp->f_seekp = DIP(fp, di_size) - offset;
838 break;
839 default:
840 errno = EINVAL;
841 return (-1);
842 }
843 return (fp->f_seekp);
844 }
845
846 static int
ufs_stat(struct open_file * f,struct stat * sb)847 ufs_stat(struct open_file *f, struct stat *sb)
848 {
849 struct file *fp = (struct file *)f->f_fsdata;
850
851 /* only important stuff */
852 sb->st_mode = DIP(fp, di_mode);
853 sb->st_uid = DIP(fp, di_uid);
854 sb->st_gid = DIP(fp, di_gid);
855 sb->st_size = DIP(fp, di_size);
856 sb->st_mtime = DIP(fp, di_mtime);
857 /*
858 * The items below are ufs specific!
859 * Other fs types will need their own solution
860 * if these fields are needed.
861 */
862 sb->st_ino = fp->f_inumber;
863 /*
864 * We need something to differentiate devs.
865 * fs_id is unique but 64bit, we xor the two
866 * halves to squeeze it into 32bits.
867 */
868 sb->st_dev = (dev_t)(fp->f_fs->fs_id[0] ^ fp->f_fs->fs_id[1]);
869
870 return (0);
871 }
872
873 static int
ufs_readdir(struct open_file * f,struct dirent * d)874 ufs_readdir(struct open_file *f, struct dirent *d)
875 {
876 struct file *fp = (struct file *)f->f_fsdata;
877 struct direct *dp;
878 char *buf;
879 size_t buf_size;
880 int error;
881
882 /*
883 * assume that a directory entry will not be split across blocks
884 */
885
886 do {
887 if (fp->f_seekp >= DIP(fp, di_size))
888 return (ENOENT);
889 error = buf_read_file(f, &buf, &buf_size);
890 if (error)
891 return (error);
892 dp = (struct direct *)buf;
893 fp->f_seekp += dp->d_reclen;
894 } while (dp->d_ino == (ino_t)0);
895
896 d->d_type = dp->d_type;
897 strcpy(d->d_name, dp->d_name);
898 return (0);
899 }
900
901 static int
ufs_mount(const char * dev,const char * path,void ** data)902 ufs_mount(const char *dev, const char *path, void **data)
903 {
904 char *fs;
905 ufs_mnt_t *mnt;
906 struct open_file *f;
907
908 errno = 0;
909 mnt = calloc(1, sizeof(*mnt));
910 if (mnt == NULL)
911 return (errno);
912 mnt->um_fd = -1;
913 mnt->um_dev = strdup(dev);
914 if (mnt->um_dev == NULL)
915 goto done;
916
917 if (asprintf(&fs, "%s%s", dev, path) < 0)
918 goto done;
919
920 mnt->um_fd = open(fs, O_RDONLY);
921 free(fs);
922 if (mnt->um_fd == -1)
923 goto done;
924
925 /* Is it ufs file system? */
926 f = fd2open_file(mnt->um_fd);
927 if (strcmp(f->f_ops->fs_name, "ufs") == 0)
928 STAILQ_INSERT_TAIL(&mnt_list, mnt, um_link);
929 else
930 errno = ENXIO;
931
932 done:
933 if (errno != 0) {
934 free(mnt->um_dev);
935 if (mnt->um_fd >= 0)
936 close(mnt->um_fd);
937 free(mnt);
938 } else {
939 *data = mnt;
940 }
941
942 return (errno);
943 }
944
945 static int
ufs_unmount(const char * dev __unused,void * data)946 ufs_unmount(const char *dev __unused, void *data)
947 {
948 ufs_mnt_t *mnt = data;
949
950 STAILQ_REMOVE(&mnt_list, mnt, ufs_mnt, um_link);
951 free(mnt->um_dev);
952 close(mnt->um_fd);
953 free(mnt);
954 return (0);
955 }
956