PageRenderTime 106ms CodeModel.GetById 3ms app.highlight 91ms RepoModel.GetById 1ms app.codeStats 1ms

/sys/ufs/chfs/chfs_vnops.c

https://bitbucket.org/gthummalapalle/minix
C | 1765 lines | 1258 code | 281 blank | 226 comment | 311 complexity | 293b77fd7126401922520b7e07a07e58 MD5 | raw file
   1/*	$NetBSD: chfs_vnops.c,v 1.2 2011/11/24 21:09:37 agc Exp $	*/
   2
   3/*-
   4 * Copyright (c) 2010 Department of Software Engineering,
   5 *		      University of Szeged, Hungary
   6 * Copyright (C) 2010 Tamas Toth <ttoth@inf.u-szeged.hu>
   7 * Copyright (C) 2010 Adam Hoka <ahoka@NetBSD.org>
   8 * All rights reserved.
   9 *
  10 * This code is derived from software contributed to The NetBSD Foundation
  11 * by the Department of Software Engineering, University of Szeged, Hungary
  12 *
  13 * Redistribution and use in source and binary forms, with or without
  14 * modification, are permitted provided that the following conditions
  15 * are met:
  16 * 1. Redistributions of source code must retain the above copyright
  17 *    notice, this list of conditions and the following disclaimer.
  18 * 2. Redistributions in binary form must reproduce the above copyright
  19 *    notice, this list of conditions and the following disclaimer in the
  20 *    documentation and/or other materials provided with the distribution.
  21 *
  22 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  23 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  24 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  25 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  32 * SUCH DAMAGE.
  33 */
  34
  35#include <sys/param.h>
  36#include <miscfs/specfs/specdev.h>
  37#include <miscfs/fifofs/fifo.h>
  38#include <miscfs/genfs/genfs.h>
  39#include <ufs/ufs/dir.h>
  40#include <ufs/ufs/ufs_extern.h>
  41#include <uvm/uvm.h>
  42#include <sys/namei.h>
  43#include <sys/stat.h>
  44#include <sys/fcntl.h>
  45#include <sys/buf.h>
  46#include <sys/fstrans.h>
  47#include <sys/vnode.h>
  48
  49#include "chfs.h"
  50
  51#define READ_S  "chfs_read"
  52
  53int
  54chfs_lookup(void *v)
  55{
  56	struct vnode *dvp = ((struct vop_lookup_args *) v)->a_dvp;
  57	struct vnode **vpp = ((struct vop_lookup_args *) v)->a_vpp;
  58	struct componentname *cnp = ((struct vop_lookup_args *) v)->a_cnp;
  59
  60	int error;
  61	struct chfs_inode* ip;
  62	struct ufsmount* ump;
  63	struct chfs_mount* chmp;
  64	struct chfs_vnode_cache* chvc;
  65	struct chfs_dirent* fd;
  66
  67	dbg("lookup(): %s\n", cnp->cn_nameptr);
  68
  69	KASSERT(VOP_ISLOCKED(dvp));
  70
  71	*vpp = NULL;
  72
  73	// Check accessibility of requested node as a first step.
  74	error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred);
  75	if (error != 0) {
  76		goto out;
  77	}
  78
  79	// If requesting the last path component on a read-only file system
  80	// with a write operation, deny it.
  81	if ((cnp->cn_flags & ISLASTCN) && (dvp->v_mount->mnt_flag & MNT_RDONLY)
  82	    && (cnp->cn_nameiop == DELETE || cnp->cn_nameiop == RENAME)) {
  83		error = EROFS;
  84		goto out;
  85	}
  86
  87	// Avoid doing a linear scan of the directory if the requested
  88	// directory/name couple is already in the cache.
  89	error = cache_lookup(dvp, vpp, cnp);
  90	if (error >= 0) {
  91		goto out;
  92	}
  93
  94	ip = VTOI(dvp);
  95	ump = VFSTOUFS(dvp->v_mount);
  96	chmp = ump->um_chfs;
  97	if (ip->ino == 0) {
  98		ip->ino = ++chmp->chm_max_vno;
  99	}
 100	mutex_enter(&chmp->chm_lock_vnocache);
 101	chvc = chfs_vnode_cache_get(chmp, ip->ino);
 102	mutex_exit(&chmp->chm_lock_vnocache);
 103
 104	// We cannot be requesting the parent directory of the root node.
 105	KASSERT(IMPLIES(dvp->v_type == VDIR && chvc->pvno == chvc->vno,
 106		!(cnp->cn_flags & ISDOTDOT)));
 107
 108	if (cnp->cn_flags & ISDOTDOT) {
 109		VOP_UNLOCK(dvp);
 110		error = VFS_VGET(dvp->v_mount, ip->chvc->pvno, vpp);
 111		vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
 112	} else if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {
 113		vref(dvp);
 114		*vpp = dvp;
 115		error = 0;
 116	} else {
 117		fd = chfs_dir_lookup(ip, cnp);
 118
 119		if (fd == NULL) {
 120			dbg("fd null\n");
 121			// The entry was not found in the directory.
 122			// This is OK if we are creating or renaming an
 123			// entry and are working on the last component of
 124			// the path name.
 125			if ((cnp->cn_flags & ISLASTCN) && (cnp->cn_nameiop == CREATE
 126				|| cnp->cn_nameiop == RENAME)) {
 127				error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred);
 128				if (error) {
 129					dbg("after the entry was not found in dir\n");
 130					goto out;
 131				}
 132
 133				dbg("return EJUSTRETURN\n");
 134				error = EJUSTRETURN;
 135			} else {
 136				error = ENOENT;
 137			}
 138		} else {
 139			// If we are not at the last path component and
 140			// found a non-directory or non-link entry (which
 141			// may itself be pointing to a directory), raise
 142			// an error.
 143			if ((fd->type != VDIR && fd->type != VLNK) && !(cnp->cn_flags
 144				& ISLASTCN)) {
 145				error = ENOTDIR;
 146				goto out;
 147			}
 148
 149			dbg("vno@allocating new vnode: %llu\n",
 150				(unsigned long long)fd->vno);
 151			error = VFS_VGET(dvp->v_mount, fd->vno, vpp);
 152		}
 153	}
 154	// Store the result of this lookup in the cache.  Avoid this if the
 155	// request was for creation, as it does not improve timings on
 156	// emprical tests.
 157	if ((cnp->cn_flags & MAKEENTRY) && cnp->cn_nameiop != CREATE
 158	    && (cnp->cn_flags & ISDOTDOT) == 0)
 159		cache_enter(dvp, *vpp, cnp);
 160
 161out:
 162	// If there were no errors, *vpp cannot be null and it must be
 163	// locked.
 164	KASSERT(IFF(error == 0, *vpp != NULL && VOP_ISLOCKED(*vpp)));
 165
 166	// dvp must always be locked.
 167	KASSERT(VOP_ISLOCKED(dvp));
 168
 169	return error;
 170}
 171
 172/* --------------------------------------------------------------------- */
 173
 174int
 175chfs_create(void *v)
 176{
 177	struct vop_create_args /* {
 178				  struct vnode *a_dvp;
 179				  struct vnode **a_vpp;
 180				  struct componentname *a_cnp;
 181				  struct vattr *a_vap;
 182				  } */*ap = v;
 183	int error, mode;
 184	dbg("create()\n");
 185
 186	mode = MAKEIMODE(ap->a_vap->va_type, ap->a_vap->va_mode);
 187
 188	if ((mode & IFMT) == 0) {
 189		if (ap->a_vap->va_type == VREG)
 190			mode |= IFREG;
 191		if (ap->a_vap->va_type == VSOCK)
 192			mode |= IFSOCK;
 193	}
 194
 195	error = chfs_makeinode(mode, ap->a_dvp,	ap->a_vpp, ap->a_cnp, ap->a_vap->va_type);
 196
 197	if (error) {
 198		dbg("error: %d\n", error);
 199		return error;
 200	}
 201
 202	VN_KNOTE(ap->a_dvp, NOTE_WRITE);
 203	return 0;
 204}
 205/* --------------------------------------------------------------------- */
 206
 207int
 208chfs_mknod(void *v)
 209{
 210	struct vnode *dvp = ((struct vop_mknod_args *) v)->a_dvp;
 211	struct vnode **vpp = ((struct vop_mknod_args *) v)->a_vpp;
 212	struct componentname *cnp = ((struct vop_mknod_args *) v)->a_cnp;
 213	struct vattr *vap = ((struct vop_mknod_args *) v)->a_vap;
 214	int mode, err = 0;
 215	struct chfs_inode *ip;
 216	struct vnode *vp;
 217
 218	struct ufsmount *ump;
 219	struct chfs_mount *chmp;
 220	ino_t ino;
 221
 222	struct chfs_full_dnode *fd;
 223	struct buf *bp;
 224	int len;
 225	dbg("mknod()\n");
 226
 227	ump = VFSTOUFS(dvp->v_mount);
 228	chmp = ump->um_chfs;
 229
 230	if (vap->va_type != VBLK && vap->va_type != VCHR && vap->va_type != VFIFO)
 231		return EINVAL;
 232
 233	vp = *vpp;
 234
 235	mode = MAKEIMODE(vap->va_type, vap->va_mode);
 236
 237	if ((mode & IFMT) == 0) {
 238		switch (vap->va_type) {
 239		case VBLK:
 240			mode |= IFBLK;
 241			break;
 242		case VCHR:
 243			mode |= IFCHR;
 244			break;
 245		case VFIFO:
 246			mode |= IFIFO;
 247			break;
 248		default:
 249			break;
 250		}
 251	}
 252
 253	err = chfs_makeinode(mode, dvp, &vp, cnp, vap->va_type);
 254
 255	ip = VTOI(vp);
 256	ino = ip->ino;
 257	if (vap->va_rdev != VNOVAL)
 258		ip->rdev = vap->va_rdev;
 259
 260	if (vap->va_type == VFIFO)
 261		vp->v_op = chfs_fifoop_p;
 262	else {
 263		vp->v_op = chfs_specop_p;
 264		spec_node_init(vp, ip->rdev);
 265	}
 266
 267	if (err)
 268		return err;
 269
 270	len = sizeof(dev_t);
 271	chfs_set_vnode_size(vp, len);
 272	bp = getiobuf(vp, true);
 273	bp->b_bufsize = bp->b_resid = len;
 274	bp->b_data = kmem_alloc(len, KM_SLEEP);
 275	memcpy(bp->b_data, &ip->rdev, len);
 276	bp->b_blkno = 0;
 277
 278	fd = chfs_alloc_full_dnode();
 279
 280	mutex_enter(&chmp->chm_lock_mountfields);
 281
 282	err = chfs_write_flash_dnode(chmp, vp, bp, fd);
 283	if (err) {
 284		mutex_exit(&chmp->chm_lock_mountfields);
 285		kmem_free(bp->b_data, len);
 286		return err;
 287	}
 288
 289	err = chfs_add_full_dnode_to_inode(chmp, ip, fd);
 290	if (err) {
 291		mutex_exit(&chmp->chm_lock_mountfields);
 292		kmem_free(bp->b_data, len);
 293		return err;
 294	}
 295
 296	mutex_exit(&chmp->chm_lock_mountfields);
 297
 298	*vpp = vp;
 299	kmem_free(bp->b_data, len);
 300	putiobuf(bp);
 301
 302	return 0;
 303}
 304
 305/* --------------------------------------------------------------------- */
 306
 307int
 308chfs_open(void *v)
 309{
 310	struct vnode *vp = ((struct vop_open_args *) v)->a_vp;
 311	int mode = ((struct vop_open_args *) v)->a_mode;
 312	dbg("open()\n");
 313
 314	int error;
 315	struct chfs_inode *ip;
 316
 317	KASSERT(VOP_ISLOCKED(vp));
 318
 319	ip = VTOI(vp);
 320
 321	KASSERT(vp->v_size == ip->size);
 322	if (ip->chvc->nlink < 1) {
 323		error = ENOENT;
 324		goto out;
 325	}
 326
 327	// If the file is marked append-only, deny write requests.
 328	if (ip->flags & APPEND && (mode & (FWRITE | O_APPEND)) == FWRITE)
 329		error = EPERM;
 330	else
 331		error = 0;
 332
 333out:
 334	KASSERT(VOP_ISLOCKED(vp));
 335	return error;
 336}
 337
 338/* --------------------------------------------------------------------- */
 339
 340int
 341chfs_close(void *v)
 342{
 343	struct vnode *vp = ((struct vop_close_args *) v)->a_vp;
 344	dbg("close()\n");
 345
 346	struct chfs_inode *ip;
 347
 348	KASSERT(VOP_ISLOCKED(vp));
 349
 350	ip = VTOI(vp);
 351
 352	if (ip->chvc->nlink > 0) {
 353		//ip->chvc->nlink = 0;
 354		chfs_update(vp, NULL, NULL, UPDATE_CLOSE);
 355	}
 356
 357	return 0;
 358}
 359
 360/* --------------------------------------------------------------------- */
 361
 362int
 363chfs_access(void *v)
 364{
 365	struct vnode *vp = ((struct vop_access_args *) v)->a_vp;
 366	int mode = ((struct vop_access_args *) v)->a_mode;
 367	kauth_cred_t cred = ((struct vop_access_args *) v)->a_cred;
 368
 369	dbg("access()\n");
 370	struct chfs_inode *ip = VTOI(vp);
 371
 372	if (mode & VWRITE) {
 373		switch (vp->v_type) {
 374		case VLNK:
 375		case VDIR:
 376		case VREG:
 377			if (vp->v_mount->mnt_flag & MNT_RDONLY)
 378				return (EROFS);
 379			break;
 380		case VBLK:
 381		case VCHR:
 382		case VSOCK:
 383		case VFIFO:
 384			break;
 385		default:
 386			break;
 387		}
 388	}
 389
 390	if (mode & VWRITE && ip->flags & IMMUTABLE)
 391		return (EPERM);
 392
 393	return genfs_can_access(vp->v_type, ip->mode & ALLPERMS,
 394	    ip->uid, ip->gid, mode, cred);
 395}
 396
 397/* --------------------------------------------------------------------- */
 398
 399int
 400chfs_getattr(void *v)
 401{
 402	struct vnode *vp = ((struct vop_getattr_args *) v)->a_vp;
 403	struct vattr *vap = ((struct vop_getattr_args *) v)->a_vap;
 404
 405	struct chfs_inode *ip = VTOI(vp);
 406	dbg("getattr()\n");
 407
 408	KASSERT(vp->v_size == ip->size);
 409
 410	vattr_null(vap);
 411	CHFS_ITIMES(ip, NULL, NULL, NULL);
 412
 413	vap->va_type = vp->v_type;
 414	vap->va_mode = ip->mode & ALLPERMS;
 415	vap->va_nlink = ip->chvc->nlink;
 416	vap->va_uid = ip->uid;
 417	vap->va_gid = ip->gid;
 418	vap->va_fsid = ip->dev;
 419	vap->va_fileid = ip->ino;
 420	vap->va_size = ip->size;
 421	vap->va_blocksize = PAGE_SIZE;
 422	vap->va_atime.tv_sec = ip->atime;
 423	vap->va_atime.tv_nsec = 0;
 424	vap->va_mtime.tv_sec = ip->mtime;
 425	vap->va_mtime.tv_nsec = 0;
 426	vap->va_ctime.tv_sec = ip->ctime;
 427	vap->va_ctime.tv_nsec = 0;
 428	vap->va_gen = ip->version;
 429	vap->va_flags = ip->flags;
 430	vap->va_rdev = ip->rdev;
 431	vap->va_bytes = round_page(ip->size);
 432	vap->va_filerev = VNOVAL;
 433	vap->va_vaflags = 0;
 434	vap->va_spare = VNOVAL;
 435
 436	return 0;
 437}
 438
 439/* --------------------------------------------------------------------- */
 440
 441/* Note: modelled after tmpfs's same function */
 442
 443int
 444chfs_setattr(void *v)
 445{
 446	struct vnode *vp = ((struct vop_setattr_args *) v)->a_vp;
 447	struct vattr *vap = ((struct vop_setattr_args *) v)->a_vap;
 448	kauth_cred_t cred = ((struct vop_setattr_args *) v)->a_cred;
 449
 450	struct chfs_inode *ip;
 451	struct ufsmount *ump = VFSTOUFS(vp->v_mount);
 452	struct chfs_mount *chmp = ump->um_chfs;
 453	int error = 0;
 454
 455	dbg("setattr()\n");
 456
 457	KASSERT(VOP_ISLOCKED(vp));
 458	ip = VTOI(vp);
 459
 460	/* Abort if any unsettable attribute is given. */
 461	if (vap->va_type != VNON || vap->va_nlink != VNOVAL ||
 462	    vap->va_fsid != VNOVAL || vap->va_fileid != VNOVAL ||
 463	    vap->va_blocksize != VNOVAL /*|| GOODTIME(&vap->va_ctime)*/ ||
 464	    vap->va_gen != VNOVAL || vap->va_rdev != VNOVAL ||
 465	    vap->va_bytes != VNOVAL) {
 466		return EINVAL;
 467	}
 468
 469	if (error == 0 && (vap->va_flags != VNOVAL))
 470		error = chfs_chflags(vp, vap->va_flags, cred);
 471
 472	if (error == 0 && (vap->va_size != VNOVAL))
 473		error = chfs_chsize(vp, vap->va_size, cred);
 474
 475	if (error == 0 && (vap->va_uid != VNOVAL || vap->va_gid != VNOVAL))
 476		error = chfs_chown(vp, vap->va_uid, vap->va_gid, cred);
 477
 478	if (error == 0 && (vap->va_mode != VNOVAL))
 479		error = chfs_chmod(vp, vap->va_mode, cred);
 480
 481#if 0
 482	/* why do we need that? */
 483	if (ip->flags & (IMMUTABLE | APPEND))
 484		return EPERM;
 485#endif
 486
 487	if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
 488		error = genfs_can_chtimes(vp, vap->va_vaflags, ip->uid, cred);
 489		if (error)
 490			return error;
 491		if (vap->va_atime.tv_sec != VNOVAL)
 492			ip->iflag |= IN_ACCESS;
 493		if (vap->va_mtime.tv_sec != VNOVAL)
 494			ip->iflag |= IN_CHANGE | IN_UPDATE;
 495		error = chfs_update(vp,
 496		    &vap->va_atime, &vap->va_mtime, UPDATE_WAIT);
 497		if (error)
 498			return error;
 499	}
 500
 501	mutex_enter(&chmp->chm_lock_mountfields);
 502	error = chfs_write_flash_vnode(chmp, ip, ALLOC_NORMAL);
 503	mutex_exit(&chmp->chm_lock_mountfields);
 504
 505	return error;
 506}
 507
 508int
 509chfs_chmod(struct vnode *vp, int mode, kauth_cred_t cred)
 510{
 511	struct chfs_inode *ip = VTOI(vp);
 512	int error;
 513	dbg("chmod\n");
 514
 515	error = genfs_can_chmod(vp, cred, ip->uid, ip->gid, mode);
 516	if (error)
 517		return error;
 518	ip->mode &= ~ALLPERMS;
 519	ip->mode |= (mode & ALLPERMS);
 520	ip->iflag |= IN_CHANGE;
 521
 522	error = chfs_update(vp, NULL, NULL, UPDATE_WAIT);
 523	if (error)
 524		return error;
 525
 526	return 0;
 527}
 528
 529int
 530chfs_chown(struct vnode *vp, uid_t uid, gid_t gid, kauth_cred_t cred)
 531{
 532	struct chfs_inode *ip = VTOI(vp);
 533	int error;
 534	dbg("chown\n");
 535
 536	if (uid == (uid_t)VNOVAL)
 537		uid = ip->uid;
 538	if (gid == (gid_t)VNOVAL)
 539		gid = ip->gid;
 540
 541	error = genfs_can_chown(vp, cred, ip->uid, ip->gid, uid, gid);
 542	if (error)
 543		return error;
 544
 545	ip->gid = gid;
 546	ip->uid = uid;
 547	ip->iflag |= IN_CHANGE;
 548
 549	error = chfs_update(vp, NULL, NULL, UPDATE_WAIT);
 550	if (error)
 551		return error;
 552
 553	return 0;
 554}
 555
 556
 557/* --------------------------------------------------------------------- */
 558/* calculates ((off_t)blk * chmp->chm_chm_fs_bsize) */
 559#define	lblktosize(chmp, blk)						      \
 560	(((off_t)(blk)) << (chmp)->chm_fs_bshift)
 561
 562/* calculates (loc % chmp->chm_chm_fs_bsize) */
 563#define	blkoff(chmp, loc)							      \
 564	((loc) & (chmp)->chm_fs_qbmask)
 565
 566/* calculates (loc / chmp->chm_chm_fs_bsize) */
 567#define	lblkno(chmp, loc)							      \
 568	((loc) >> (chmp)->chm_fs_bshift)
 569
 570/* calculates roundup(size, chmp->chm_chm_fs_fsize) */
 571#define	fragroundup(chmp, size)						      \
 572	(((size) + (chmp)->chm_fs_qfmask) & (chmp)->chm_fs_fmask)
 573
 574#define	blksize(chmp, ip, lbn)						      \
 575	(((lbn) >= NDADDR || (ip)->size >= lblktosize(chmp, (lbn) + 1))	      \
 576	    ? (chmp)->chm_fs_bsize					      \
 577	    : (fragroundup(chmp, blkoff(chmp, (ip)->size))))
 578
 579/* calculates roundup(size, chmp->chm_chm_fs_bsize) */
 580#define	blkroundup(chmp, size)						      \
 581 	(((size) + (chmp)->chm_fs_qbmask) & (chmp)->chm_fs_bmask)
 582
 583int
 584chfs_read(void *v)
 585{
 586	struct vop_read_args /* {
 587				struct vnode *a_vp;
 588				struct uio *a_uio;
 589				int a_ioflag;
 590				kauth_cred_t a_cred;
 591				} */ *ap = v;
 592	struct vnode *vp;
 593	struct chfs_inode *ip;
 594	struct uio *uio;
 595	struct ufsmount *ump;
 596	struct buf *bp;
 597	struct chfs_mount *chmp;
 598	daddr_t lbn, nextlbn;
 599	off_t bytesinfile;
 600	long size, xfersize, blkoffset;
 601	int error, ioflag;
 602	vsize_t bytelen;
 603	bool usepc = false;
 604
 605	dbg("chfs_read\n");
 606
 607	vp = ap->a_vp;
 608	ip = VTOI(vp);
 609	ump = ip->ump;
 610	uio = ap->a_uio;
 611	ioflag = ap->a_ioflag;
 612	error = 0;
 613
 614	dbg("ip->size:%llu\n", (unsigned long long)ip->size);
 615
 616#ifdef DIAGNOSTIC
 617	if (uio->uio_rw != UIO_READ)
 618		panic("%s: mode", READ_S);
 619
 620	if (vp->v_type == VLNK) {
 621		if (ip->size < ump->um_maxsymlinklen)
 622			panic("%s: short symlink", READ_S);
 623	} else if (vp->v_type != VREG && vp->v_type != VDIR)
 624		panic("%s: type %d", READ_S, vp->v_type);
 625#endif
 626	chmp = ip->chmp;
 627	if ((u_int64_t)uio->uio_offset > ump->um_maxfilesize)
 628		return (EFBIG);
 629	if (uio->uio_resid == 0)
 630		return (0);
 631
 632	fstrans_start(vp->v_mount, FSTRANS_SHARED);
 633
 634	if (uio->uio_offset >= ip->size)
 635		goto out;
 636
 637	usepc = vp->v_type == VREG;
 638	bytelen = 0;
 639	if (usepc) {
 640		const int advice = IO_ADV_DECODE(ap->a_ioflag);
 641
 642		while (uio->uio_resid > 0) {
 643			if (ioflag & IO_DIRECT) {
 644				genfs_directio(vp, uio, ioflag);
 645			}
 646			bytelen = MIN(ip->size - uio->uio_offset,
 647			    uio->uio_resid);
 648			if (bytelen == 0)
 649				break;
 650			error = ubc_uiomove(&vp->v_uobj, uio, bytelen, advice,
 651			    UBC_READ | UBC_PARTIALOK |
 652			    (UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0));
 653			if (error)
 654				break;
 655
 656		}
 657		goto out;
 658	}
 659
 660
 661	dbg("start reading\n");
 662	for (error = 0, bp = NULL; uio->uio_resid > 0; bp = NULL) {
 663		bytesinfile = ip->size - uio->uio_offset;
 664		if (bytesinfile <= 0)
 665			break;
 666		lbn = lblkno(chmp, uio->uio_offset);
 667		nextlbn = lbn + 1;
 668		size = blksize(chmp, ip, lbn);
 669		blkoffset = blkoff(chmp, uio->uio_offset);
 670		xfersize = MIN(MIN(chmp->chm_fs_bsize - blkoffset, uio->uio_resid),
 671		    bytesinfile);
 672
 673		if (lblktosize(chmp, nextlbn) >= ip->size) {
 674			error = bread(vp, lbn, size, NOCRED, 0, &bp);
 675			dbg("after bread\n");
 676		} else {
 677			int nextsize = blksize(chmp, ip, nextlbn);
 678			dbg("size: %ld\n", size);
 679			error = breadn(vp, lbn,
 680			    size, &nextlbn, &nextsize, 1, NOCRED, 0, &bp);
 681			dbg("after breadN\n");
 682		}
 683		if (error)
 684			break;
 685
 686		/*
 687		 * We should only get non-zero b_resid when an I/O error
 688		 * has occurred, which should cause us to break above.
 689		 * However, if the short read did not cause an error,
 690		 * then we want to ensure that we do not uiomove bad
 691		 * or uninitialized data.
 692		 */
 693		size -= bp->b_resid;
 694		if (size < xfersize) {
 695			if (size == 0)
 696				break;
 697			xfersize = size;
 698		}
 699		dbg("uiomove\n");
 700		error = uiomove((char *)bp->b_data + blkoffset, xfersize, uio);
 701		if (error)
 702			break;
 703		brelse(bp, 0);
 704	}
 705	if (bp != NULL)
 706		brelse(bp, 0);
 707
 708out:
 709	if (!(vp->v_mount->mnt_flag & MNT_NOATIME)) {
 710		ip->iflag |= IN_ACCESS;
 711		if ((ap->a_ioflag & IO_SYNC) == IO_SYNC) {
 712			//error = UFS_WAPBL_BEGIN(vp->v_mount);
 713			if (error) {
 714				fstrans_done(vp->v_mount);
 715				return error;
 716			}
 717			error = chfs_update(vp, NULL, NULL, UPDATE_WAIT);
 718			//UFS_WAPBL_END(vp->v_mount);
 719		}
 720	}
 721
 722	dbg("[END]\n");
 723	fstrans_done(vp->v_mount);
 724	return (error);
 725}
 726
 727
 728/* --------------------------------------------------------------------- */
 729
 730/*from ffs write*/
 731int
 732chfs_write(void *v)
 733{
 734	struct vop_write_args /* {
 735				 struct vnode *a_vp;
 736				 struct uio *a_uio;
 737				 int a_ioflag;
 738				 kauth_cred_t a_cred;
 739				 } */ *ap = v;
 740	struct vnode *vp ;
 741	struct uio *uio;
 742	struct chfs_inode *ip;
 743	struct chfs_mount *chmp;
 744	struct lwp *l;
 745	kauth_cred_t cred;
 746	off_t osize, origoff, oldoff, preallocoff, endallocoff, nsize;
 747	int blkoffset, error, flags, ioflag, resid;
 748	int aflag;
 749	int extended=0;
 750	vsize_t bytelen;
 751	bool async;
 752	struct ufsmount *ump;
 753
 754
 755	cred = ap->a_cred;
 756	ioflag = ap->a_ioflag;
 757	uio = ap->a_uio;
 758	vp = ap->a_vp;
 759	ip = VTOI(vp);
 760	//dbg("file size (vp): %llu\n", (unsigned long long)vp->v_size);
 761	//dbg("file size (ip): %llu\n", (unsigned long long)ip->i_size);
 762	ump = ip->ump;
 763
 764	//dbg("uio->resid: %d\n", uio->uio_resid);
 765	dbg("write\n");
 766
 767	KASSERT(vp->v_size == ip->size);
 768
 769	switch (vp->v_type) {
 770	case VREG:
 771		if (ioflag & IO_APPEND)
 772			uio->uio_offset = ip->size;
 773		if ((ip->flags & APPEND) && uio->uio_offset != ip->size)
 774			return (EPERM);
 775		/* FALLTHROUGH */
 776	case VLNK:
 777		break;
 778	case VDIR:
 779		if ((ioflag & IO_SYNC) == 0)
 780			panic("chfs_write: nonsync dir write");
 781		break;
 782	default:
 783		panic("chfs_write: type");
 784	}
 785
 786	chmp = ip->chmp;
 787	if (uio->uio_offset < 0 ||
 788	    (u_int64_t)uio->uio_offset +
 789	    uio->uio_resid > ump->um_maxfilesize) {
 790		dbg("uio->uio_offset = %lld | uio->uio_offset + "
 791		    "uio->uio_resid (%llu) > ump->um_maxfilesize (%lld)\n",
 792		    (long long)uio->uio_offset,
 793		    (uint64_t)uio->uio_offset + uio->uio_resid,
 794		    (long long)ump->um_maxfilesize);
 795		return (EFBIG);
 796	}
 797	/*
 798	 * Maybe this should be above the vnode op call, but so long as
 799	 * file servers have no limits, I don't think it matters.
 800	 */
 801	l = curlwp;
 802	if (vp->v_type == VREG && l &&
 803	    uio->uio_offset + uio->uio_resid >
 804	    l->l_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
 805		mutex_enter(proc_lock);
 806		psignal(l->l_proc, SIGXFSZ);
 807		mutex_exit(proc_lock);
 808		return (EFBIG);
 809	}
 810	if (uio->uio_resid == 0)
 811		return (0);
 812
 813	//mutex_enter(&ip->inode_lock);
 814	fstrans_start(vp->v_mount, FSTRANS_SHARED);
 815
 816	flags = ioflag & IO_SYNC ? B_SYNC : 0;
 817	async = vp->v_mount->mnt_flag & MNT_ASYNC;
 818	origoff = uio->uio_offset;
 819	resid = uio->uio_resid;
 820	osize = ip->size;
 821	error = 0;
 822
 823
 824	/*if ((ioflag & IO_JOURNALLOCKED) == 0) {
 825	  error = UFS_WAPBL_BEGIN(vp->v_mount);
 826	  if (error) {
 827	  fstrans_done(vp->v_mount);
 828	  return error;
 829	  }
 830	  }*/
 831
 832	preallocoff = round_page(blkroundup(chmp,
 833		MAX(osize, uio->uio_offset)));
 834	aflag = ioflag & IO_SYNC ? B_SYNC : 0;
 835	nsize = MAX(osize, uio->uio_offset + uio->uio_resid);
 836	endallocoff = nsize - blkoff(chmp, nsize);
 837
 838	/*
 839	 * if we're increasing the file size, deal with expanding
 840	 * the fragment if there is one.
 841	 */
 842
 843	if (nsize > osize && lblkno(chmp, osize) < NDADDR &&
 844	    lblkno(chmp, osize) != lblkno(chmp, nsize) &&
 845	    blkroundup(chmp, osize) != osize) {
 846		off_t eob;
 847
 848		eob = blkroundup(chmp, osize);
 849		uvm_vnp_setwritesize(vp, eob);
 850		error = ufs_balloc_range(vp, osize, eob - osize, cred, aflag);
 851		if (error)
 852			goto out;
 853		if (flags & B_SYNC) {
 854			mutex_enter(vp->v_interlock);
 855			VOP_PUTPAGES(vp,
 856			    trunc_page(osize & chmp->chm_fs_bmask),
 857			    round_page(eob),
 858			    PGO_CLEANIT | PGO_SYNCIO | PGO_JOURNALLOCKED);
 859		}
 860	}
 861
 862	while (uio->uio_resid > 0) {
 863		int ubc_flags = UBC_WRITE;
 864		bool overwrite; /* if we're overwrite a whole block */
 865		off_t newoff;
 866
 867		if (ioflag & IO_DIRECT) {
 868			genfs_directio(vp, uio, ioflag | IO_JOURNALLOCKED);
 869		}
 870
 871		oldoff = uio->uio_offset;
 872		blkoffset = blkoff(chmp, uio->uio_offset);
 873		bytelen = MIN(chmp->chm_fs_bsize - blkoffset, uio->uio_resid);
 874		if (bytelen == 0) {
 875			break;
 876		}
 877
 878		/*
 879		 * if we're filling in a hole, allocate the blocks now and
 880		 * initialize the pages first.  if we're extending the file,
 881		 * we can safely allocate blocks without initializing pages
 882		 * since the new blocks will be inaccessible until the write
 883		 * is complete.
 884		 */
 885		overwrite = uio->uio_offset >= preallocoff &&
 886		    uio->uio_offset < endallocoff;
 887		if (!overwrite && (vp->v_vflag & VV_MAPPED) == 0 &&
 888		    blkoff(chmp, uio->uio_offset) == 0 &&
 889		    (uio->uio_offset & PAGE_MASK) == 0) {
 890			vsize_t len;
 891
 892			len = trunc_page(bytelen);
 893			len -= blkoff(chmp, len);
 894			if (len > 0) {
 895				overwrite = true;
 896				bytelen = len;
 897			}
 898		}
 899
 900		newoff = oldoff + bytelen;
 901		if (vp->v_size < newoff) {
 902			uvm_vnp_setwritesize(vp, newoff);
 903		}
 904
 905		if (!overwrite) {
 906			error = ufs_balloc_range(vp, uio->uio_offset, bytelen,
 907			    cred, aflag);
 908			if (error)
 909				break;
 910		} else {
 911			genfs_node_wrlock(vp);
 912			error = GOP_ALLOC(vp, uio->uio_offset, bytelen,
 913			    aflag, cred);
 914			genfs_node_unlock(vp);
 915			if (error)
 916				break;
 917			ubc_flags |= UBC_FAULTBUSY;
 918		}
 919
 920		/*
 921		 * copy the data.
 922		 */
 923
 924		ubc_flags |= UBC_WANT_UNMAP(vp) ? UBC_UNMAP : 0;
 925		error = ubc_uiomove(&vp->v_uobj, uio, bytelen,
 926		    IO_ADV_DECODE(ioflag), ubc_flags);
 927
 928		/*
 929		 * update UVM's notion of the size now that we've
 930		 * copied the data into the vnode's pages.
 931		 *
 932		 * we should update the size even when uiomove failed.
 933		 */
 934
 935		if (vp->v_size < newoff) {
 936			uvm_vnp_setsize(vp, newoff);
 937			extended = 1;
 938		}
 939
 940		if (error)
 941			break;
 942
 943		/*
 944		 * flush what we just wrote if necessary.
 945		 * XXXUBC simplistic async flushing.
 946		 */
 947
 948		if (!async && oldoff >> 16 != uio->uio_offset >> 16) {
 949			mutex_enter(vp->v_interlock);
 950			error = VOP_PUTPAGES(vp, (oldoff >> 16) << 16,
 951			    (uio->uio_offset >> 16) << 16,
 952			    PGO_CLEANIT | PGO_JOURNALLOCKED);
 953			if (error)
 954				break;
 955		}
 956	}
 957out:
 958	if (error == 0 && ioflag & IO_SYNC) {
 959		mutex_enter(vp->v_interlock);
 960		error = VOP_PUTPAGES(vp,
 961		    trunc_page(origoff & chmp->chm_fs_bmask),
 962		    round_page(blkroundup(chmp, uio->uio_offset)),
 963		    PGO_CLEANIT | PGO_SYNCIO | PGO_JOURNALLOCKED);
 964	}
 965	ip->iflag |= IN_CHANGE | IN_UPDATE;
 966	if (resid > uio->uio_resid && ap->a_cred &&
 967	    kauth_authorize_generic(ap->a_cred, KAUTH_GENERIC_ISSUSER, NULL)) {
 968		ip->mode &= ~(ISUID | ISGID);
 969	}
 970	if (resid > uio->uio_resid)
 971		VN_KNOTE(vp, NOTE_WRITE | (extended ? NOTE_EXTEND : 0));
 972	if (error) {
 973		(void) UFS_TRUNCATE(vp, osize, ioflag & IO_SYNC, ap->a_cred);
 974		uio->uio_offset -= resid - uio->uio_resid;
 975		uio->uio_resid = resid;
 976	} else if (resid > uio->uio_resid && (ioflag & IO_SYNC) == IO_SYNC)
 977		error = UFS_UPDATE(vp, NULL, NULL, UPDATE_WAIT);
 978
 979	//XXX hack, i write the next line after i know ip->i_size and vp->v_size don't equal
 980	chfs_set_vnode_size(vp, vp->v_size);
 981
 982
 983	//dbg("end file size (vp): %llu\n", (unsigned long long)vp->v_size);
 984	//dbg("end file size (ip): %llu\n", (unsigned long long)ip->i_size);
 985	KASSERT(vp->v_size == ip->size);
 986	fstrans_done(vp->v_mount);
 987
 988	mutex_enter(&chmp->chm_lock_mountfields);
 989	error = chfs_write_flash_vnode(chmp, ip, ALLOC_NORMAL);
 990	mutex_exit(&chmp->chm_lock_mountfields);
 991
 992	//mutex_exit(&ip->inode_lock);
 993	//dbg("end\n");
 994	return (error);
 995}
 996
 997
 998/* --------------------------------------------------------------------- */
 999
1000int
1001chfs_fsync(void *v)
1002{
1003	//dbg("fsync\n");
1004	struct vop_fsync_args /* {
1005				 struct vnode *a_vp;
1006				 kauth_cred_t a_cred;
1007				 int a_flags;
1008				 off_t offlo;
1009				 off_t offhi;
1010				 } */ *ap = v;
1011	struct vnode *vp = ap->a_vp;
1012	int wait;
1013
1014	if (ap->a_flags & FSYNC_CACHE) {
1015		return ENODEV;
1016	}
1017	wait = (ap->a_flags & FSYNC_WAIT) != 0;
1018 	vflushbuf(vp, wait);
1019	//struct chfs_inode *ip = VTOI(vp);
1020	//chfs_set_vnode_size(vp, ip->write_size);
1021
1022	return 0;
1023}
1024
1025/* --------------------------------------------------------------------- */
1026
1027int
1028chfs_remove(void *v)
1029{
1030	struct vnode *dvp = ((struct vop_remove_args *) v)->a_dvp;
1031	struct vnode *vp = ((struct vop_remove_args *) v)->a_vp;
1032	struct componentname *cnp = (((struct vop_remove_args *) v)->a_cnp);
1033	dbg("remove\n");
1034
1035	KASSERT(VOP_ISLOCKED(dvp));
1036	KASSERT(VOP_ISLOCKED(vp));
1037
1038	struct chfs_inode *ip = VTOI(vp);
1039	struct chfs_inode *parent = VTOI(dvp);
1040	int error = 0;
1041
1042	KASSERT(ip->chvc->vno != ip->chvc->pvno);
1043
1044	error = chfs_do_unlink(ip,
1045	    parent, cnp->cn_nameptr, cnp->cn_namelen);
1046
1047	vput(dvp);
1048	vput(vp);
1049
1050	return error;
1051}
1052
1053/* --------------------------------------------------------------------- */
1054
1055int
1056chfs_link(void *v)
1057{
1058	struct vnode *dvp = ((struct vop_link_args *) v)->a_dvp;
1059	struct vnode *vp = ((struct vop_link_args *) v)->a_vp;
1060	struct componentname *cnp = ((struct vop_link_args *) v)->a_cnp;
1061
1062	struct chfs_inode *ip, *parent;
1063	int error = 0;
1064
1065	if (vp->v_type == VDIR) {
1066		VOP_ABORTOP(dvp, cnp);
1067		error = EISDIR;
1068		goto out;
1069	}
1070	if (dvp->v_mount != vp->v_mount) {
1071		VOP_ABORTOP(dvp, cnp);
1072		error = EXDEV;
1073		goto out;
1074	}
1075	if (dvp != vp && (error = vn_lock(vp, LK_EXCLUSIVE))) {
1076		VOP_ABORTOP(dvp, cnp);
1077		goto out;
1078	}
1079
1080	parent = VTOI(dvp);
1081	ip = VTOI(vp);
1082
1083	error = chfs_do_link(ip,
1084	    parent, cnp->cn_nameptr, cnp->cn_namelen, vp->v_type);
1085
1086	if (dvp != vp)
1087		VOP_UNLOCK(vp);
1088out:
1089	vput(dvp);
1090	return error;
1091}
1092
1093/* --------------------------------------------------------------------- */
1094
1095int
1096chfs_rename(void *v)
1097{
1098	struct vnode *fdvp = ((struct vop_rename_args *) v)->a_fdvp;
1099	struct vnode *fvp = ((struct vop_rename_args *) v)->a_fvp;
1100	struct componentname *fcnp = ((struct vop_rename_args *) v)->a_fcnp;
1101	struct vnode *tdvp = ((struct vop_rename_args *) v)->a_tdvp;
1102	struct vnode *tvp = ((struct vop_rename_args *) v)->a_tvp;
1103	struct componentname *tcnp = ((struct vop_rename_args *) v)->a_tcnp;
1104
1105	struct chfs_inode *oldparent, *old;
1106	struct chfs_inode *newparent;
1107	struct chfs_dirent *fd;//, *oldfd;
1108	struct chfs_inode *ip;
1109	int error = 0;
1110	dbg("rename\n");
1111
1112	KASSERT(VOP_ISLOCKED(tdvp));
1113	KASSERT(IMPLIES(tvp != NULL, VOP_ISLOCKED(tvp) == LK_EXCLUSIVE));
1114
1115	oldparent = VTOI(fdvp);
1116	old = VTOI(fvp);
1117	newparent = VTOI(tdvp);
1118	if (tvp) {
1119		dbg("tvp not null\n");
1120		ip = VTOI(tvp);
1121		if (tvp->v_type == VDIR) {
1122			//TODO: lock
1123//			fd = ip->dents;
1124//			while (fd) {
1125			TAILQ_FOREACH(fd, &ip->dents, fds) {
1126				if (fd->vno) {
1127					//TODO: unlock
1128					error = ENOTEMPTY;
1129					goto out_unlocked;
1130				}
1131//				fd = fd->next;
1132			}
1133			//TODO: unlock
1134		}
1135		error = chfs_do_unlink(ip,
1136		    newparent, tcnp->cn_nameptr, tcnp->cn_namelen);
1137		vput(tvp);
1138	}
1139	VFS_VGET(tdvp->v_mount, old->ino, &tvp);
1140	ip = VTOI(tvp);
1141
1142//	for (oldfd = oldparent->dents;
1143//	     oldfd->vno != old->ino;
1144//	     oldfd = oldfd->next);
1145
1146	error = chfs_do_link(ip,
1147	    newparent, tcnp->cn_nameptr, tcnp->cn_namelen, tvp->v_type);
1148	error = chfs_do_unlink(old,
1149	    oldparent, fcnp->cn_nameptr, fcnp->cn_namelen);
1150
1151//out:
1152//	if (fchnode != tchnode)
1153//	VOP_UNLOCK(fdvp, 0);
1154
1155out_unlocked:
1156	// Release target nodes.
1157	if (tdvp == tvp)
1158		vrele(tdvp);
1159	else
1160		vput(tdvp);
1161	if (tvp != NULL)
1162		vput(tvp);
1163
1164	// Release source nodes.
1165	vrele(fdvp);
1166	vrele(fvp);
1167
1168	return error;
1169}
1170
1171/* --------------------------------------------------------------------- */
1172
1173int
1174chfs_mkdir(void *v)
1175{
1176	struct vnode *dvp = ((struct vop_mkdir_args *) v)->a_dvp;
1177	struct vnode **vpp = ((struct vop_mkdir_args *)v)->a_vpp;
1178	struct componentname *cnp = ((struct vop_mkdir_args *) v)->a_cnp;
1179	struct vattr *vap = ((struct vop_mkdir_args *) v)->a_vap;
1180	dbg("mkdir()\n");
1181
1182	int mode;
1183
1184	mode = vap->va_mode & ACCESSPERMS;
1185	if ((mode & IFMT) == 0) {
1186		mode |= IFDIR;
1187	}
1188
1189	KASSERT(vap->va_type == VDIR);
1190
1191	return chfs_makeinode(mode, dvp, vpp, cnp, VDIR);
1192}
1193
1194/* --------------------------------------------------------------------- */
1195
1196int
1197chfs_rmdir(void *v)
1198{
1199	struct vnode *dvp = ((struct vop_rmdir_args *) v)->a_dvp;
1200	struct vnode *vp = ((struct vop_rmdir_args *) v)->a_vp;
1201	struct componentname *cnp = ((struct vop_rmdir_args *) v)->a_cnp;
1202	dbg("rmdir()\n");
1203
1204	KASSERT(VOP_ISLOCKED(dvp));
1205	KASSERT(VOP_ISLOCKED(vp));
1206
1207	struct chfs_inode *ip = VTOI(vp);
1208	struct chfs_inode *parent = VTOI(dvp);
1209	struct chfs_dirent *fd;
1210	int error = 0;
1211
1212	if (vp->v_type != VDIR) {
1213		error = ENOTDIR;
1214		goto out;
1215	}
1216
1217	KASSERT(ip->chvc->vno != ip->chvc->pvno);
1218
1219//	for (fd = ip->dents; fd; fd = fd->next) {
1220	TAILQ_FOREACH(fd, &ip->dents, fds) {
1221		if (fd->vno) {
1222			error = ENOTEMPTY;
1223			goto out;
1224		}
1225	}
1226
1227	error = chfs_do_unlink(ip,
1228	    parent, cnp->cn_nameptr, cnp->cn_namelen);
1229
1230out:
1231	vput(dvp);
1232	vput(vp);
1233
1234	return error;
1235}
1236
1237/* --------------------------------------------------------------------- */
1238
1239int
1240chfs_symlink(void *v)
1241{
1242	struct vnode *dvp = ((struct vop_symlink_args *) v)->a_dvp;
1243	struct vnode **vpp = ((struct vop_symlink_args *) v)->a_vpp;
1244	struct componentname *cnp = ((struct vop_symlink_args *) v)->a_cnp;
1245	struct vattr *vap = ((struct vop_symlink_args *) v)->a_vap;
1246	char *target = ((struct vop_symlink_args *) v)->a_target;
1247
1248	struct ufsmount *ump;
1249	struct chfs_mount *chmp;
1250	struct vnode *vp;
1251	struct chfs_inode *ip;
1252	int len, err;
1253	struct chfs_full_dnode *fd;
1254	struct buf *bp;
1255	dbg("symlink()\n");
1256
1257	ump = VFSTOUFS(dvp->v_mount);
1258	chmp = ump->um_chfs;
1259
1260	err = chfs_makeinode(IFLNK | vap->va_mode, dvp, vpp, cnp, VLNK);
1261	if (err)
1262		return (err);
1263	VN_KNOTE(dvp, NOTE_WRITE);
1264	vp = *vpp;
1265	len = strlen(target);
1266	ip = VTOI(vp);
1267	/* TODO max symlink len instead of "100" */
1268	if (len < 100) {
1269		ip->target = kmem_alloc(len, KM_SLEEP);
1270		memcpy(ip->target, target, len);
1271		chfs_set_vnode_size(vp, len);
1272		ip->iflag |= IN_CHANGE | IN_UPDATE;
1273
1274		bp = getiobuf(vp, true);
1275		bp->b_bufsize = bp->b_resid = len;
1276		bp->b_data = kmem_alloc(len, KM_SLEEP);
1277		memcpy(bp->b_data, target, len);
1278		bp->b_blkno = 0;
1279
1280		fd = chfs_alloc_full_dnode();
1281
1282		mutex_enter(&chmp->chm_lock_mountfields);
1283
1284		err = chfs_write_flash_dnode(chmp, vp, bp, fd);
1285		if (err) {
1286			mutex_exit(&chmp->chm_lock_mountfields);
1287			goto out;
1288		}
1289
1290		err = chfs_add_full_dnode_to_inode(chmp, ip, fd);
1291		if (err) {
1292			mutex_exit(&chmp->chm_lock_mountfields);
1293			goto out;
1294		}
1295
1296		mutex_exit(&chmp->chm_lock_mountfields);
1297
1298		kmem_free(bp->b_data, len);
1299		putiobuf(bp);
1300
1301		uvm_vnp_setsize(vp, len);
1302	} else {
1303		err = vn_rdwr(UIO_WRITE, vp, target, len, (off_t)0,
1304		    UIO_SYSSPACE, IO_NODELOCKED, cnp->cn_cred,
1305		    (size_t *)0, NULL);
1306	}
1307
1308out:
1309	if (err)
1310		vput(vp);
1311
1312	return (err);
1313}
1314
1315/* --------------------------------------------------------------------- */
1316
1317int
1318chfs_readdir(void *v)
1319{
1320	struct vnode *vp = ((struct vop_readdir_args *) v)->a_vp;
1321	struct uio *uio = ((struct vop_readdir_args *) v)->a_uio;
1322	int *eofflag = ((struct vop_readdir_args *) v)->a_eofflag;
1323
1324	int error = 0;
1325	off_t skip, offset;
1326	struct chfs_inode *ip;
1327	struct chfs_dirent *fd;
1328
1329	struct ufsmount *ump;
1330	struct chfs_mount *chmp;
1331	struct chfs_vnode_cache *chvc;
1332
1333	KASSERT(VOP_ISLOCKED(vp));
1334
1335	/* This operation only makes sense on directory nodes. */
1336	if (vp->v_type != VDIR) {
1337		error = ENOTDIR;
1338		goto out;
1339	}
1340
1341	ip = VTOI(vp);
1342
1343	/* uiomove in chfs_filldir automatically increments the
1344	 * uio_offset by an arbitrary size, so we discard any change
1345	 * to uio_offset and set it to our own value on return
1346	 */
1347	offset = uio->uio_offset;
1348
1349	if (offset == CHFS_OFFSET_DOT) {
1350		error = chfs_filldir(uio, ip->ino, ".", 1, VDIR);
1351		if (error == -1) {
1352			error = 0;
1353			goto outok;
1354		} else if (error != 0)
1355			goto outok;
1356
1357		offset = CHFS_OFFSET_DOTDOT;
1358	}
1359
1360	if (offset == CHFS_OFFSET_DOTDOT) {
1361		ump = VFSTOUFS(vp->v_mount);
1362		chmp = ump->um_chfs;
1363		mutex_enter(&chmp->chm_lock_vnocache);
1364		chvc = chfs_vnode_cache_get(chmp, ip->ino);
1365		mutex_exit(&chmp->chm_lock_vnocache);
1366
1367		error = chfs_filldir(uio, chvc->pvno, "..", 2, VDIR);
1368		if (error == -1) {
1369			error = 0;
1370			goto outok;
1371		} else if (error != 0) {
1372			goto outok;
1373		}
1374
1375		if (TAILQ_EMPTY(&ip->dents)) {
1376			offset = CHFS_OFFSET_EOF;
1377		} else {
1378			offset = CHFS_OFFSET_FIRST;
1379		}
1380	}
1381
1382	if (offset != CHFS_OFFSET_EOF) {
1383		skip = offset - CHFS_OFFSET_FIRST;
1384
1385		TAILQ_FOREACH(fd, &ip->dents, fds) {
1386			/* seek to offset by skipping items */
1387			/* XXX race conditions by changed dirent? */
1388			if (skip > 0) {
1389				skip--;
1390				continue;
1391			}
1392
1393			if (fd->vno != 0) {
1394				error = chfs_filldir(uio, fd->vno,
1395				    fd->name, fd->nsize, fd->type);
1396				if (error == -1) {
1397					error = 0;
1398					goto outok;
1399				} else if (error != 0) {
1400					dbg("err %d\n", error);
1401					goto outok;
1402				}
1403			}
1404			offset++;
1405		}
1406	}
1407	offset = CHFS_OFFSET_EOF;
1408
1409outok:
1410	uio->uio_offset = offset;
1411
1412	if (eofflag != NULL) {
1413		*eofflag = (error == 0 &&
1414		    uio->uio_offset == CHFS_OFFSET_EOF);
1415	}
1416
1417out:
1418	KASSERT(VOP_ISLOCKED(vp));
1419
1420	return error;
1421}
1422
1423/* --------------------------------------------------------------------- */
1424
1425int
1426chfs_readlink(void *v)
1427{
1428
1429	struct vnode *vp = ((struct vop_readlink_args *) v)->a_vp;
1430	struct uio *uio = ((struct vop_readlink_args *) v)->a_uio;
1431	kauth_cred_t cred = ((struct vop_readlink_args *) v)->a_cred;
1432
1433	struct chfs_inode *ip = VTOI(vp);
1434
1435	dbg("readlink()\n");
1436
1437	/* TODO max symlink len instead of "100" */
1438	if (ip->size < 100) {
1439		uiomove(ip->target, ip->size, uio);
1440		return (0);
1441	}
1442
1443	return (VOP_READ(vp, uio, 0, cred));
1444}
1445
1446/* --------------------------------------------------------------------- */
1447
1448int
1449chfs_inactive(void *v)
1450{
1451	struct vnode *vp = ((struct vop_inactive_args *) v)->a_vp;
1452	struct chfs_inode *ip = VTOI(vp);
1453	struct chfs_vnode_cache *chvc;
1454	dbg("inactive | vno: %llu\n", (unsigned long long)ip->ino);
1455
1456	KASSERT(VOP_ISLOCKED(vp));
1457
1458	if (ip->ino) {
1459		chvc = ip->chvc;
1460		if (chvc->nlink)
1461			*((struct vop_inactive_args *) v)->a_recycle = 0;
1462	} else {
1463		*((struct vop_inactive_args *) v)->a_recycle = 1;
1464	}
1465
1466	VOP_UNLOCK(vp);
1467
1468	return 0;
1469}
1470
1471/* --------------------------------------------------------------------- */
1472
1473int
1474chfs_reclaim(void *v)
1475{
1476	struct vop_reclaim_args *ap = v;
1477	struct vnode *vp = ap->a_vp;
1478	struct chfs_inode *ip = VTOI(vp);
1479	struct chfs_mount *chmp = ip->chmp;
1480	struct chfs_dirent *fd;
1481
1482	//dbg("reclaim() | ino: %llu\n", (unsigned long long)ip->ino);
1483	//mutex_enter(&ip->inode_lock);
1484
1485	mutex_enter(&chmp->chm_lock_vnocache);
1486	chfs_vnode_cache_set_state(chmp,
1487	    ip->chvc, VNO_STATE_CHECKEDABSENT);
1488	mutex_exit(&chmp->chm_lock_vnocache);
1489
1490	chfs_update(vp, NULL, NULL, UPDATE_CLOSE);
1491
1492	if (vp->v_type == VREG || vp->v_type == VLNK || vp->v_type == VCHR ||
1493	    vp->v_type == VBLK || vp->v_type == VFIFO || vp->v_type == VSOCK)
1494		chfs_kill_fragtree(&ip->fragtree);
1495
1496	fd = TAILQ_FIRST(&ip->dents);
1497	while(fd) {
1498		TAILQ_REMOVE(&ip->dents, fd, fds);
1499		chfs_free_dirent(fd);
1500		fd = TAILQ_FIRST(&ip->dents);
1501	}
1502	//mutex_exit(&ip->inode_lock);
1503	//mutex_destroy(&ip->inode_lock);
1504
1505	cache_purge(vp);
1506	if (ip->devvp) {
1507		vrele(ip->devvp);
1508		ip->devvp = 0;
1509	}
1510	chfs_ihashrem(ip);
1511
1512	genfs_node_destroy(vp);
1513	pool_put(&chfs_inode_pool, vp->v_data);
1514	vp->v_data = NULL;
1515	return (0);
1516}
1517
1518/* --------------------------------------------------------------------- */
1519
1520int
1521chfs_advlock(void *v)
1522{
1523	//struct vnode *vp = ((struct vop_advlock_args *) v)->a_vp;
1524	dbg("advlock()\n");
1525	/*
1526	  struct chfs_node *node;
1527
1528	  node = VP_TO_CHFS_NODE(vp);
1529
1530	  return lf_advlock(v, &node->chn_lockf, node->chn_size);
1531	*/
1532	return 0;
1533}
1534
1535/* --------------------------------------------------------------------- */
1536int
1537chfs_strategy(void *v)
1538{
1539	struct vop_strategy_args /* {
1540				    const struct vnodeop_desc *a_desc;
1541				    struct vnode *a_vp;
1542				    struct buf *a_bp;
1543				    } */ *ap = v;
1544	struct chfs_full_dnode *fd;
1545	struct buf *bp = ap->a_bp;
1546	struct vnode *vp = ap->a_vp;
1547	struct chfs_inode *ip = VTOI(vp);
1548	struct chfs_mount *chmp = ip->chmp;
1549	int read = (bp->b_flags & B_READ) ? 1 : 0;
1550	int err = 0;
1551
1552/*	dbg("bp dump:\n");
1553	dbg("	->b_bcount: %d\n", bp->b_bcount);
1554	dbg("	->b_resid:  %d\n", bp->b_resid);
1555	dbg("	->b_blkno:  %llu\n", (unsigned long long)bp->b_blkno);
1556	dbg("	->b_error:  %d\n", bp->b_error);*/
1557	if (read) {
1558		err = chfs_read_data(chmp, vp, bp);
1559	} else {
1560		fd = chfs_alloc_full_dnode();
1561
1562		mutex_enter(&chmp->chm_lock_mountfields);
1563
1564		err = chfs_write_flash_dnode(chmp, vp, bp, fd);
1565		if (err) {
1566			mutex_exit(&chmp->chm_lock_mountfields);
1567			goto out;
1568		}
1569
1570		err = chfs_add_full_dnode_to_inode(chmp, ip, fd);
1571		/*if (err) {
1572			mutex_exit(&chmp->chm_lock_mountfields);
1573			goto out;
1574		}*/
1575
1576		mutex_exit(&chmp->chm_lock_mountfields);
1577	}
1578out:
1579	biodone(bp);
1580	//dbg("end\n");
1581	return err;
1582}
1583
1584int
1585chfs_bmap(void *v)
1586{
1587	struct vop_bmap_args /* {
1588				struct vnode *a_vp;
1589				daddr_t  a_bn;
1590				struct vnode **a_vpp;
1591				daddr_t *a_bnp;
1592				int *a_runp;
1593				int *a_runb;
1594				} */ *ap = v;
1595	if (ap->a_vpp != NULL)
1596		*ap->a_vpp = ap->a_vp;
1597	if (ap->a_bnp != NULL)
1598		*ap->a_bnp = ap->a_bn;
1599	if (ap->a_runp != NULL)
1600		*ap->a_runp = 0;
1601	return (0);
1602}
1603
1604/*
1605 * vnode operations vector used for files stored in a chfs file system.
1606 */
1607int
1608(**chfs_vnodeop_p)(void *);
1609const struct vnodeopv_entry_desc chfs_vnodeop_entries[] =
1610	{
1611		{ &vop_default_desc, vn_default_error },
1612		{ &vop_lookup_desc, chfs_lookup },
1613		{ &vop_create_desc, chfs_create },
1614		{ &vop_mknod_desc, chfs_mknod },
1615		{ &vop_open_desc, chfs_open },
1616		{ &vop_close_desc, chfs_close },
1617		{ &vop_access_desc, chfs_access },
1618		{ &vop_getattr_desc, chfs_getattr },
1619		{ &vop_setattr_desc, chfs_setattr },
1620		{ &vop_read_desc, chfs_read },
1621		{ &vop_write_desc, chfs_write },
1622		{ &vop_ioctl_desc, genfs_enoioctl },
1623		{ &vop_fcntl_desc, genfs_fcntl },
1624		{ &vop_poll_desc, genfs_poll },
1625		{ &vop_kqfilter_desc, genfs_kqfilter },
1626		{ &vop_revoke_desc, genfs_revoke },
1627		{ &vop_mmap_desc, genfs_mmap },
1628		{ &vop_fsync_desc, chfs_fsync },
1629		{ &vop_seek_desc, genfs_seek },
1630		{ &vop_remove_desc, chfs_remove },
1631		{ &vop_link_desc, chfs_link },
1632		{ &vop_rename_desc, chfs_rename },
1633		{ &vop_mkdir_desc, chfs_mkdir },
1634		{ &vop_rmdir_desc, chfs_rmdir },
1635		{ &vop_symlink_desc, chfs_symlink },
1636		{ &vop_readdir_desc, chfs_readdir },
1637		{ &vop_readlink_desc, chfs_readlink },
1638		{ &vop_abortop_desc, genfs_abortop },
1639		{ &vop_inactive_desc, chfs_inactive },
1640		{ &vop_reclaim_desc, chfs_reclaim },
1641		{ &vop_lock_desc, genfs_lock },
1642		{ &vop_unlock_desc, genfs_unlock },
1643		{ &vop_bmap_desc, chfs_bmap },
1644		{ &vop_strategy_desc, chfs_strategy },
1645		{ &vop_print_desc, ufs_print },
1646		{ &vop_pathconf_desc, ufs_pathconf },
1647		{ &vop_islocked_desc, genfs_islocked },
1648		{ &vop_advlock_desc, chfs_advlock },
1649		{ &vop_bwrite_desc, vn_bwrite },
1650		{ &vop_getpages_desc, genfs_getpages },
1651		{ &vop_putpages_desc, genfs_putpages },
1652		{ NULL, NULL } };
1653
1654const struct vnodeopv_desc chfs_vnodeop_opv_desc =
1655	{ &chfs_vnodeop_p, chfs_vnodeop_entries };
1656
1657/* --------------------------------------------------------------------- */
1658
1659/*
1660 * vnode operations vector used for special devices stored in a chfs
1661 * file system.
1662 */
1663int
1664(**chfs_specop_p)(void *);
1665const struct vnodeopv_entry_desc chfs_specop_entries[] =
1666	{
1667		{ &vop_default_desc, vn_default_error },
1668		{ &vop_lookup_desc, spec_lookup },
1669		{ &vop_create_desc, spec_create },
1670		{ &vop_mknod_desc, spec_mknod },
1671		{ &vop_open_desc, spec_open },
1672		{ &vop_close_desc, ufsspec_close },
1673		{ &vop_access_desc, chfs_access },
1674		{ &vop_getattr_desc, chfs_getattr },
1675		{ &vop_setattr_desc, chfs_setattr },
1676		{ &vop_read_desc, chfs_read },
1677		{ &vop_write_desc, chfs_write },
1678		{ &vop_ioctl_desc, spec_ioctl },
1679		{ &vop_fcntl_desc, genfs_fcntl },
1680		{ &vop_poll_desc, spec_poll },
1681		{ &vop_kqfilter_desc, spec_kqfilter },
1682		{ &vop_revoke_desc, spec_revoke },
1683		{ &vop_mmap_desc, spec_mmap },
1684		{ &vop_fsync_desc, spec_fsync },
1685		{ &vop_seek_desc, spec_seek },
1686		{ &vop_remove_desc, spec_remove },
1687		{ &vop_link_desc, spec_link },
1688		{ &vop_rename_desc, spec_rename },
1689		{ &vop_mkdir_desc, spec_mkdir },
1690		{ &vop_rmdir_desc, spec_rmdir },
1691		{ &vop_symlink_desc, spec_symlink },
1692		{ &vop_readdir_desc, spec_readdir },
1693		{ &vop_readlink_desc, spec_readlink },
1694		{ &vop_abortop_desc, spec_abortop },
1695		{ &vop_inactive_desc, chfs_inactive },
1696		{ &vop_reclaim_desc, chfs_reclaim },
1697		{ &vop_lock_desc, genfs_lock },
1698		{ &vop_unlock_desc, genfs_unlock },
1699		{ &vop_bmap_desc, spec_bmap },
1700		{ &vop_strategy_desc, spec_strategy },
1701		{ &vop_print_desc, ufs_print },
1702		{ &vop_pathconf_desc, spec_pathconf },
1703		{ &vop_islocked_desc, genfs_islocked },
1704		{ &vop_advlock_desc, spec_advlock },
1705		{ &vop_bwrite_desc, vn_bwrite },
1706		{ &vop_getpages_desc, spec_getpages },
1707		{ &vop_putpages_desc, spec_putpages },
1708		{ NULL, NULL } };
1709
1710const struct vnodeopv_desc chfs_specop_opv_desc =
1711	{ &chfs_specop_p, chfs_specop_entries };
1712
1713/* --------------------------------------------------------------------- */
1714/*
1715 * vnode operations vector used for fifos stored in a chfs file system.
1716 */
1717int
1718(**chfs_fifoop_p)(void *);
1719const struct vnodeopv_entry_desc chfs_fifoop_entries[] =
1720	{
1721		{ &vop_default_desc, vn_default_error },
1722		{ &vop_lookup_desc, vn_fifo_bypass },
1723		{ &vop_create_desc, vn_fifo_bypass },
1724		{ &vop_mknod_desc, vn_fifo_bypass },
1725		{ &vop_open_desc, vn_fifo_bypass },
1726		{ &vop_close_desc, ufsfifo_close },
1727		{ &vop_access_desc, chfs_access },
1728		{ &vop_getattr_desc, chfs_getattr },
1729		{ &vop_setattr_desc, chfs_setattr },
1730		{ &vop_read_desc, ufsfifo_read },
1731		{ &vop_write_desc, ufsfifo_write },
1732		{ &vop_ioctl_desc, vn_fifo_bypass },
1733		{ &vop_fcntl_desc, genfs_fcntl },
1734		{ &vop_poll_desc, vn_fifo_bypass },
1735		{ &vop_kqfilter_desc, vn_fifo_bypass },
1736		{ &vop_revoke_desc, vn_fifo_bypass },
1737		{ &vop_mmap_desc, vn_fifo_bypass },
1738		{ &vop_fsync_desc, vn_fifo_bypass },
1739		{ &vop_seek_desc, vn_fifo_bypass },
1740		{ &vop_remove_desc, vn_fifo_bypass },
1741		{ &vop_link_desc, vn_fifo_bypass },
1742		{ &vop_rename_desc, vn_fifo_bypass },
1743		{ &vop_mkdir_desc, vn_fifo_bypass },
1744		{ &vop_rmdir_desc, vn_fifo_bypass },
1745		{ &vop_symlink_desc, vn_fifo_bypass },
1746		{ &vop_readdir_desc, vn_fifo_bypass },
1747		{ &vop_readlink_desc, vn_fifo_bypass },
1748		{ &vop_abortop_desc, vn_fifo_bypass },
1749		{ &vop_inactive_desc, chfs_inactive },
1750		{ &vop_reclaim_desc, chfs_reclaim },
1751		{ &vop_lock_desc, genfs_lock },
1752		{ &vop_unlock_desc, genfs_unlock },
1753		{ &vop_bmap_desc, vn_fifo_bypass },
1754		{ &vop_strategy_desc, vn_fifo_bypass },
1755		{ &vop_print_desc, ufs_print },
1756		{ &vop_pathconf_desc, vn_fifo_bypass },
1757		{ &vop_islocked_desc, genfs_islocked },
1758		{ &vop_advlock_desc, vn_fifo_bypass },
1759		{ &vop_bwrite_desc, genfs_nullop },
1760		{ &vop_getpages_desc, genfs_badop },
1761		{ &vop_putpages_desc, vn_fifo_bypass },
1762		{ NULL, NULL } };
1763
1764const struct vnodeopv_desc chfs_fifoop_opv_desc =
1765	{ &chfs_fifoop_p, chfs_fifoop_entries };