1 /* 2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)ffs_vnops.c 7.70 (Berkeley) 03/22/92 8 */ 9 10 #include <sys/param.h> 11 #include <sys/systm.h> 12 #include <sys/resourcevar.h> 13 #include <sys/kernel.h> 14 #include <sys/file.h> 15 #include <sys/stat.h> 16 #include <sys/buf.h> 17 #include <sys/proc.h> 18 #include <sys/conf.h> 19 #include <sys/mount.h> 20 #include <sys/vnode.h> 21 #include <sys/specdev.h> 22 #include <sys/fifo.h> 23 #include <sys/malloc.h> 24 25 #include <ufs/ufs/lockf.h> 26 #include <ufs/ufs/quota.h> 27 #include <ufs/ufs/inode.h> 28 #include <ufs/ufs/dir.h> 29 #include <ufs/ufs/ufs_extern.h> 30 31 #include <ufs/ffs/fs.h> 32 #include <ufs/ffs/ffs_extern.h> 33 34 /* Global vfs data structures for ufs. */ 35 struct vnodeops ffs_vnodeops = { 36 ufs_lookup, /* lookup */ 37 ufs_create, /* create */ 38 ufs_mknod, /* mknod */ 39 ufs_open, /* open */ 40 ufs_close, /* close */ 41 ufs_access, /* access */ 42 ufs_getattr, /* getattr */ 43 ufs_setattr, /* setattr */ 44 ffs_read, /* read */ 45 ffs_write, /* write */ 46 ufs_ioctl, /* ioctl */ 47 ufs_select, /* select */ 48 ufs_mmap, /* mmap */ 49 ffs_fsync, /* fsync */ 50 ufs_seek, /* seek */ 51 ufs_remove, /* remove */ 52 ufs_link, /* link */ 53 ufs_rename, /* rename */ 54 ufs_mkdir, /* mkdir */ 55 ufs_rmdir, /* rmdir */ 56 ufs_symlink, /* symlink */ 57 ufs_readdir, /* readdir */ 58 ufs_readlink, /* readlink */ 59 ufs_abortop, /* abortop */ 60 ffs_inactive, /* inactive */ 61 ufs_reclaim, /* reclaim */ 62 ufs_lock, /* lock */ 63 ufs_unlock, /* unlock */ 64 ffs_bmap, /* bmap */ 65 ufs_strategy, /* strategy */ 66 ufs_print, /* print */ 67 ufs_islocked, /* islocked */ 68 ufs_advlock, /* advlock */ 69 ffs_blkatoff, /* blkatoff */ 70 ffs_vget, /* vget */ 71 ffs_valloc, /* valloc */ 72 ffs_vfree, /* vfree */ 73 ffs_truncate, /* truncate */ 74 ffs_update, /* update */ 75 bwrite, /* bwrite */ 76 }; 77 78 struct vnodeops ffs_specops = { 79 spec_lookup, /* lookup */ 80 spec_create, /* create */ 81 spec_mknod, /* mknod */ 82 spec_open, /* open */ 83 ufsspec_close, /* close */ 84 ufs_access, /* access */ 85 ufs_getattr, /* getattr */ 86 ufs_setattr, /* setattr */ 87 ufsspec_read, /* read */ 88 ufsspec_write, /* write */ 89 spec_ioctl, /* ioctl */ 90 spec_select, /* select */ 91 spec_mmap, /* mmap */ 92 spec_fsync, /* fsync */ 93 spec_seek, /* seek */ 94 spec_remove, /* remove */ 95 spec_link, /* link */ 96 spec_rename, /* rename */ 97 spec_mkdir, /* mkdir */ 98 spec_rmdir, /* rmdir */ 99 spec_symlink, /* symlink */ 100 spec_readdir, /* readdir */ 101 spec_readlink, /* readlink */ 102 spec_abortop, /* abortop */ 103 ffs_inactive, /* inactive */ 104 ufs_reclaim, /* reclaim */ 105 ufs_lock, /* lock */ 106 ufs_unlock, /* unlock */ 107 spec_bmap, /* bmap */ 108 spec_strategy, /* strategy */ 109 ufs_print, /* print */ 110 ufs_islocked, /* islocked */ 111 spec_advlock, /* advlock */ 112 spec_blkatoff, /* blkatoff */ 113 spec_vget, /* vget */ 114 spec_valloc, /* valloc */ 115 spec_vfree, /* vfree */ 116 spec_truncate, /* truncate */ 117 ffs_update, /* update */ 118 bwrite, /* bwrite */ 119 }; 120 121 #ifdef FIFO 122 struct vnodeops ffs_fifoops = { 123 fifo_lookup, /* lookup */ 124 fifo_create, /* create */ 125 fifo_mknod, /* mknod */ 126 fifo_open, /* open */ 127 ufsfifo_close, /* close */ 128 ufs_access, /* access */ 129 ufs_getattr, /* getattr */ 130 ufs_setattr, /* setattr */ 131 ufsfifo_read, /* read */ 132 ufsfifo_write, /* write */ 133 fifo_ioctl, /* ioctl */ 134 fifo_select, /* select */ 135 fifo_mmap, /* mmap */ 136 fifo_fsync, /* fsync */ 137 fifo_seek, /* seek */ 138 fifo_remove, /* remove */ 139 fifo_link, /* link */ 140 fifo_rename, /* rename */ 141 fifo_mkdir, /* mkdir */ 142 fifo_rmdir, /* rmdir */ 143 fifo_symlink, /* symlink */ 144 fifo_readdir, /* readdir */ 145 fifo_readlink, /* readlink */ 146 fifo_abortop, /* abortop */ 147 ffs_inactive, /* inactive */ 148 ufs_reclaim, /* reclaim */ 149 ufs_lock, /* lock */ 150 ufs_unlock, /* unlock */ 151 fifo_bmap, /* bmap */ 152 fifo_strategy, /* strategy */ 153 ufs_print, /* print */ 154 ufs_islocked, /* islocked */ 155 fifo_advlock, /* advlock */ 156 fifo_blkatoff, /* blkatoff */ 157 fifo_vget, /* vget */ 158 fifo_valloc, /* valloc */ 159 fifo_vfree, /* vfree */ 160 fifo_truncate, /* truncate */ 161 ffs_update, /* update */ 162 bwrite, /* bwrite */ 163 }; 164 #endif /* FIFO */ 165 166 167 /* 168 * Vnode op for reading. 169 */ 170 /* ARGSUSED */ 171 ffs_read(vp, uio, ioflag, cred) 172 struct vnode *vp; 173 register struct uio *uio; 174 int ioflag; 175 struct ucred *cred; 176 { 177 register struct inode *ip = VTOI(vp); 178 register struct fs *fs; 179 struct buf *bp; 180 daddr_t lbn, bn, rablock; 181 int size, rasize, diff, error = 0; 182 long n, on, type; 183 184 #ifdef DIAGNOSTIC 185 if (uio->uio_rw != UIO_READ) 186 panic("ffs_read mode"); 187 type = ip->i_mode & IFMT; 188 if (type != IFDIR && type != IFREG && type != IFLNK) 189 panic("ffs_read type"); 190 #endif 191 if (uio->uio_resid == 0) 192 return (0); 193 if (uio->uio_offset < 0) 194 return (EINVAL); 195 ip->i_flag |= IACC; 196 fs = ip->i_fs; 197 do { 198 lbn = lblkno(fs, uio->uio_offset); 199 on = blkoff(fs, uio->uio_offset); 200 n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid); 201 diff = ip->i_size - uio->uio_offset; 202 if (diff <= 0) 203 return (0); 204 if (diff < n) 205 n = diff; 206 size = blksize(fs, ip, lbn); 207 rablock = lbn + 1; 208 if (vp->v_lastr + 1 == lbn && 209 lblktosize(fs, rablock) < ip->i_size) { 210 rasize = blksize(fs, ip, rablock); 211 error = breadn(vp, lbn, size, &rablock, 212 &rasize, 1, NOCRED, &bp); 213 } else 214 error = bread(vp, lbn, size, NOCRED, &bp); 215 vp->v_lastr = lbn; 216 n = MIN(n, size - bp->b_resid); 217 if (error) { 218 brelse(bp); 219 return (error); 220 } 221 error = uiomove(bp->b_un.b_addr + on, (int)n, uio); 222 if (n + on == fs->fs_bsize || uio->uio_offset == ip->i_size) 223 bp->b_flags |= B_AGE; 224 brelse(bp); 225 } while (error == 0 && uio->uio_resid > 0 && n != 0); 226 return (error); 227 } 228 229 /* 230 * Vnode op for writing. 231 */ 232 ffs_write(vp, uio, ioflag, cred) 233 register struct vnode *vp; 234 struct uio *uio; 235 int ioflag; 236 struct ucred *cred; 237 { 238 struct proc *p = uio->uio_procp; 239 register struct inode *ip = VTOI(vp); 240 register struct fs *fs; 241 struct buf *bp; 242 daddr_t lbn, bn; 243 u_long osize; 244 int n, on, flags; 245 int size, resid, error = 0; 246 247 #ifdef DIAGNOSTIC 248 if (uio->uio_rw != UIO_WRITE) 249 panic("ffs_write mode"); 250 #endif 251 switch (vp->v_type) { 252 case VREG: 253 if (ioflag & IO_APPEND) 254 uio->uio_offset = ip->i_size; 255 /* fall through */ 256 case VLNK: 257 break; 258 259 case VDIR: 260 if ((ioflag & IO_SYNC) == 0) 261 panic("ffs_write nonsync dir write"); 262 break; 263 264 default: 265 panic("ffs_write type"); 266 } 267 if (uio->uio_offset < 0) 268 return (EINVAL); 269 if (uio->uio_resid == 0) 270 return (0); 271 /* 272 * Maybe this should be above the vnode op call, but so long as 273 * file servers have no limits, i don't think it matters 274 */ 275 if (vp->v_type == VREG && p && 276 uio->uio_offset + uio->uio_resid > 277 p->p_rlimit[RLIMIT_FSIZE].rlim_cur) { 278 psignal(p, SIGXFSZ); 279 return (EFBIG); 280 } 281 resid = uio->uio_resid; 282 osize = ip->i_size; 283 fs = ip->i_fs; 284 flags = 0; 285 if (ioflag & IO_SYNC) 286 flags = B_SYNC; 287 do { 288 lbn = lblkno(fs, uio->uio_offset); 289 on = blkoff(fs, uio->uio_offset); 290 n = MIN((unsigned)(fs->fs_bsize - on), uio->uio_resid); 291 if (n < fs->fs_bsize) 292 flags |= B_CLRBUF; 293 else 294 flags &= ~B_CLRBUF; 295 if (error = ffs_balloc(ip, lbn, (int)(on + n), &bp, flags)) 296 break; 297 bn = bp->b_blkno; 298 if (uio->uio_offset + n > ip->i_size) { 299 ip->i_size = uio->uio_offset + n; 300 vnode_pager_setsize(vp, (u_long)ip->i_size); 301 } 302 size = blksize(fs, ip, lbn); 303 (void) vnode_pager_uncache(vp); 304 n = MIN(n, size - bp->b_resid); 305 error = uiomove(bp->b_un.b_addr + on, n, uio); 306 if (ioflag & IO_SYNC) 307 (void) bwrite(bp); 308 else if (n + on == fs->fs_bsize) { 309 bp->b_flags |= B_AGE; 310 bawrite(bp); 311 } else 312 bdwrite(bp); 313 ip->i_flag |= IUPD|ICHG; 314 if (cred->cr_uid != 0) 315 ip->i_mode &= ~(ISUID|ISGID); 316 } while (error == 0 && uio->uio_resid > 0 && n != 0); 317 if (error && (ioflag & IO_UNIT)) { 318 (void)ffs_truncate(vp, osize, ioflag & IO_SYNC); 319 uio->uio_offset -= resid - uio->uio_resid; 320 uio->uio_resid = resid; 321 } 322 if (!error && (ioflag & IO_SYNC)) 323 error = ffs_update(vp, &time, &time, 1); 324 return (error); 325 } 326 327 /* 328 * Synch an open file. 329 */ 330 /* ARGSUSED */ 331 int 332 ffs_fsync(vp, fflags, cred, waitfor, p) 333 struct vnode *vp; 334 int fflags; 335 struct ucred *cred; 336 int waitfor; 337 struct proc *p; 338 { 339 struct inode *ip = VTOI(vp); 340 341 if (fflags & FWRITE) 342 ip->i_flag |= ICHG; 343 vflushbuf(vp, waitfor == MNT_WAIT ? B_SYNC : 0); 344 return (ffs_update(vp, &time, &time, waitfor == MNT_WAIT)); 345 } 346 347 /* 348 * Last reference to an inode, write the inode out and if necessary, 349 * truncate and deallocate the file. 350 */ 351 int 352 ffs_inactive(vp, p) 353 struct vnode *vp; 354 struct proc *p; 355 { 356 register struct inode *ip; 357 int mode, error; 358 extern int prtactive; 359 360 if (prtactive && vp->v_usecount != 0) 361 vprint("ffs_inactive: pushing active", vp); 362 363 /* Get rid of inodes related to stale file handles. */ 364 ip = VTOI(vp); 365 if (ip->i_mode == 0) { 366 if ((vp->v_flag & VXLOCK) == 0) 367 vgone(vp); 368 return (0); 369 } 370 371 error = 0; 372 ILOCK(ip); 373 if (ip->i_nlink <= 0 && (vp->v_mount->mnt_flag & MNT_RDONLY) == 0) { 374 #ifdef QUOTA 375 if (!getinoquota(ip)) 376 (void)chkiq(ip, -1, NOCRED, 0); 377 #endif 378 error = ffs_truncate(vp, (u_long)0, 0); 379 mode = ip->i_mode; 380 ip->i_mode = 0; 381 ip->i_rdev = 0; 382 ip->i_flag |= IUPD|ICHG; 383 ffs_vfree(vp, ip->i_number, mode); 384 } 385 if (ip->i_flag&(IUPD|IACC|ICHG|IMOD)) 386 ffs_update(vp, &time, &time, 0); 387 IUNLOCK(ip); 388 ip->i_flag = 0; 389 /* 390 * If we are done with the inode, reclaim it 391 * so that it can be reused immediately. 392 */ 393 if (vp->v_usecount == 0 && ip->i_mode == 0) 394 vgone(vp); 395 return (error); 396 } 397