HAMMER 17/many: Refactor IO backend, clean up buffer cache deadlocks.
[dragonfly.git] / sys / vfs / hammer / hammer_vnops.c
blob5eef2aecfa0ca2a13356471229130f8da6564c9a
1 /*
2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * 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
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS 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 OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * $DragonFly: src/sys/vfs/hammer/hammer_vnops.c,v 1.17 2008/01/10 07:41:03 dillon Exp $
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/fcntl.h>
41 #include <sys/namecache.h>
42 #include <sys/vnode.h>
43 #include <sys/lockf.h>
44 #include <sys/event.h>
45 #include <sys/stat.h>
46 #include <sys/dirent.h>
47 #include <vm/vm_extern.h>
48 #include <vfs/fifofs/fifo.h>
49 #include "hammer.h"
52 * USERFS VNOPS
54 /*static int hammer_vop_vnoperate(struct vop_generic_args *);*/
55 static int hammer_vop_fsync(struct vop_fsync_args *);
56 static int hammer_vop_read(struct vop_read_args *);
57 static int hammer_vop_write(struct vop_write_args *);
58 static int hammer_vop_access(struct vop_access_args *);
59 static int hammer_vop_advlock(struct vop_advlock_args *);
60 static int hammer_vop_close(struct vop_close_args *);
61 static int hammer_vop_ncreate(struct vop_ncreate_args *);
62 static int hammer_vop_getattr(struct vop_getattr_args *);
63 static int hammer_vop_nresolve(struct vop_nresolve_args *);
64 static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
65 static int hammer_vop_nlink(struct vop_nlink_args *);
66 static int hammer_vop_nmkdir(struct vop_nmkdir_args *);
67 static int hammer_vop_nmknod(struct vop_nmknod_args *);
68 static int hammer_vop_open(struct vop_open_args *);
69 static int hammer_vop_pathconf(struct vop_pathconf_args *);
70 static int hammer_vop_print(struct vop_print_args *);
71 static int hammer_vop_readdir(struct vop_readdir_args *);
72 static int hammer_vop_readlink(struct vop_readlink_args *);
73 static int hammer_vop_nremove(struct vop_nremove_args *);
74 static int hammer_vop_nrename(struct vop_nrename_args *);
75 static int hammer_vop_nrmdir(struct vop_nrmdir_args *);
76 static int hammer_vop_setattr(struct vop_setattr_args *);
77 static int hammer_vop_strategy(struct vop_strategy_args *);
78 static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
79 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
81 static int hammer_vop_fifoclose (struct vop_close_args *);
82 static int hammer_vop_fiforead (struct vop_read_args *);
83 static int hammer_vop_fifowrite (struct vop_write_args *);
85 static int hammer_vop_specclose (struct vop_close_args *);
86 static int hammer_vop_specread (struct vop_read_args *);
87 static int hammer_vop_specwrite (struct vop_write_args *);
89 struct vop_ops hammer_vnode_vops = {
90 .vop_default = vop_defaultop,
91 .vop_fsync = hammer_vop_fsync,
92 .vop_getpages = vop_stdgetpages,
93 .vop_putpages = vop_stdputpages,
94 .vop_read = hammer_vop_read,
95 .vop_write = hammer_vop_write,
96 .vop_access = hammer_vop_access,
97 .vop_advlock = hammer_vop_advlock,
98 .vop_close = hammer_vop_close,
99 .vop_ncreate = hammer_vop_ncreate,
100 .vop_getattr = hammer_vop_getattr,
101 .vop_inactive = hammer_vop_inactive,
102 .vop_reclaim = hammer_vop_reclaim,
103 .vop_nresolve = hammer_vop_nresolve,
104 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
105 .vop_nlink = hammer_vop_nlink,
106 .vop_nmkdir = hammer_vop_nmkdir,
107 .vop_nmknod = hammer_vop_nmknod,
108 .vop_open = hammer_vop_open,
109 .vop_pathconf = hammer_vop_pathconf,
110 .vop_print = hammer_vop_print,
111 .vop_readdir = hammer_vop_readdir,
112 .vop_readlink = hammer_vop_readlink,
113 .vop_nremove = hammer_vop_nremove,
114 .vop_nrename = hammer_vop_nrename,
115 .vop_nrmdir = hammer_vop_nrmdir,
116 .vop_setattr = hammer_vop_setattr,
117 .vop_strategy = hammer_vop_strategy,
118 .vop_nsymlink = hammer_vop_nsymlink,
119 .vop_nwhiteout = hammer_vop_nwhiteout
122 struct vop_ops hammer_spec_vops = {
123 .vop_default = spec_vnoperate,
124 .vop_fsync = hammer_vop_fsync,
125 .vop_read = hammer_vop_specread,
126 .vop_write = hammer_vop_specwrite,
127 .vop_access = hammer_vop_access,
128 .vop_close = hammer_vop_specclose,
129 .vop_getattr = hammer_vop_getattr,
130 .vop_inactive = hammer_vop_inactive,
131 .vop_reclaim = hammer_vop_reclaim,
132 .vop_setattr = hammer_vop_setattr
135 struct vop_ops hammer_fifo_vops = {
136 .vop_default = fifo_vnoperate,
137 .vop_fsync = hammer_vop_fsync,
138 .vop_read = hammer_vop_fiforead,
139 .vop_write = hammer_vop_fifowrite,
140 .vop_access = hammer_vop_access,
141 .vop_close = hammer_vop_fifoclose,
142 .vop_getattr = hammer_vop_getattr,
143 .vop_inactive = hammer_vop_inactive,
144 .vop_reclaim = hammer_vop_reclaim,
145 .vop_setattr = hammer_vop_setattr
148 static int hammer_dounlink(struct nchandle *nch, struct vnode *dvp,
149 struct ucred *cred, int flags);
150 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
151 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
153 #if 0
154 static
156 hammer_vop_vnoperate(struct vop_generic_args *)
158 return (VOCALL(&hammer_vnode_vops, ap));
160 #endif
163 * hammer_vop_fsync { vp, waitfor }
165 static
167 hammer_vop_fsync(struct vop_fsync_args *ap)
169 hammer_inode_t ip;
170 int error;
172 ip = VTOI(ap->a_vp);
173 error = hammer_sync_inode(ip, ap->a_waitfor, 0);
174 return (error);
178 * hammer_vop_read { vp, uio, ioflag, cred }
180 static
182 hammer_vop_read(struct vop_read_args *ap)
184 struct hammer_transaction trans;
185 hammer_inode_t ip;
186 off_t offset;
187 struct buf *bp;
188 struct uio *uio;
189 int error;
190 int n;
191 int seqcount;
193 if (ap->a_vp->v_type != VREG)
194 return (EINVAL);
195 ip = VTOI(ap->a_vp);
196 error = 0;
197 seqcount = ap->a_ioflag >> 16;
199 hammer_start_transaction(&trans, ip->hmp);
202 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
204 uio = ap->a_uio;
205 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_rec.ino_size) {
206 offset = uio->uio_offset & HAMMER_BUFMASK;
207 #if 0
208 error = cluster_read(ap->a_vp, ip->ino_rec.ino_size,
209 uio->uio_offset - offset, HAMMER_BUFSIZE,
210 MAXBSIZE, seqcount, &bp);
211 #endif
212 error = bread(ap->a_vp, uio->uio_offset - offset,
213 HAMMER_BUFSIZE, &bp);
214 if (error) {
215 brelse(bp);
216 break;
218 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
219 n = HAMMER_BUFSIZE - offset;
220 if (n > uio->uio_resid)
221 n = uio->uio_resid;
222 if (n > ip->ino_rec.ino_size - uio->uio_offset)
223 n = (int)(ip->ino_rec.ino_size - uio->uio_offset);
224 error = uiomove((char *)bp->b_data + offset, n, uio);
225 if (error) {
226 bqrelse(bp);
227 break;
229 if ((ip->flags & HAMMER_INODE_RO) == 0) {
230 ip->ino_rec.ino_atime = trans.tid;
231 hammer_modify_inode(&trans, ip, HAMMER_INODE_ITIMES);
233 bqrelse(bp);
235 hammer_commit_transaction(&trans);
236 return (error);
240 * hammer_vop_write { vp, uio, ioflag, cred }
242 static
244 hammer_vop_write(struct vop_write_args *ap)
246 struct hammer_transaction trans;
247 struct hammer_inode *ip;
248 struct uio *uio;
249 off_t offset;
250 struct buf *bp;
251 int error;
252 int n;
253 int flags;
255 if (ap->a_vp->v_type != VREG)
256 return (EINVAL);
257 ip = VTOI(ap->a_vp);
258 error = 0;
260 if (ip->flags & HAMMER_INODE_RO)
261 return (EROFS);
264 * Create a transaction to cover the operations we perform.
266 hammer_start_transaction(&trans, ip->hmp);
267 uio = ap->a_uio;
270 * Check append mode
272 if (ap->a_ioflag & IO_APPEND)
273 uio->uio_offset = ip->ino_rec.ino_size;
276 * Check for illegal write offsets. Valid range is 0...2^63-1
278 if (uio->uio_offset < 0 || uio->uio_offset + uio->uio_resid <= 0) {
279 hammer_commit_transaction(&trans);
280 return (EFBIG);
284 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
286 while (uio->uio_resid > 0) {
287 offset = uio->uio_offset & HAMMER_BUFMASK;
288 if (uio->uio_segflg == UIO_NOCOPY) {
290 * Issuing a write with the same data backing the
291 * buffer. Instantiate the buffer to collect the
292 * backing vm pages, then read-in any missing bits.
294 * This case is used by vop_stdputpages().
296 bp = getblk(ap->a_vp, uio->uio_offset, HAMMER_BUFSIZE,
297 GETBLK_BHEAVY, 0);
298 if ((bp->b_flags & B_CACHE) == 0) {
299 bqrelse(bp);
300 error = bread(ap->a_vp,
301 uio->uio_offset - offset,
302 HAMMER_BUFSIZE, &bp);
303 if (error) {
304 brelse(bp);
305 break;
308 } else if (offset == 0 && uio->uio_resid >= HAMMER_BUFSIZE) {
310 * entirely overwrite the buffer
312 bp = getblk(ap->a_vp, uio->uio_offset, HAMMER_BUFSIZE,
313 GETBLK_BHEAVY, 0);
314 } else if (offset == 0 && uio->uio_offset >= ip->ino_rec.ino_size) {
316 * XXX
318 bp = getblk(ap->a_vp, uio->uio_offset, HAMMER_BUFSIZE,
319 GETBLK_BHEAVY, 0);
320 vfs_bio_clrbuf(bp);
321 } else {
323 * Partial overwrite, read in any missing bits then
324 * replace the portion being written.
326 error = bread(ap->a_vp, uio->uio_offset - offset,
327 HAMMER_BUFSIZE, &bp);
328 if (error) {
329 brelse(bp);
330 break;
332 bheavy(bp);
334 n = HAMMER_BUFSIZE - offset;
335 if (n > uio->uio_resid)
336 n = uio->uio_resid;
337 error = uiomove((char *)bp->b_data + offset, n, uio);
338 if (error) {
339 brelse(bp);
340 break;
342 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
343 if (ip->ino_rec.ino_size < uio->uio_offset) {
344 ip->ino_rec.ino_size = uio->uio_offset;
345 flags = HAMMER_INODE_RDIRTY;
346 vnode_pager_setsize(ap->a_vp, ip->ino_rec.ino_size);
347 } else {
348 flags = 0;
350 ip->ino_rec.ino_mtime = trans.tid;
351 flags |= HAMMER_INODE_ITIMES;
352 hammer_modify_inode(&trans, ip, flags);
353 if (ap->a_ioflag & IO_SYNC) {
354 bwrite(bp);
355 } else if (ap->a_ioflag & IO_DIRECT) {
356 bawrite(bp);
357 } else {
358 bdwrite(bp);
361 if (error)
362 hammer_abort_transaction(&trans);
363 else
364 hammer_commit_transaction(&trans);
365 return (error);
369 * hammer_vop_access { vp, mode, cred }
371 static
373 hammer_vop_access(struct vop_access_args *ap)
375 struct hammer_inode *ip = VTOI(ap->a_vp);
376 uid_t uid;
377 gid_t gid;
378 int error;
380 uid = hammer_to_unix_xid(&ip->ino_data.uid);
381 gid = hammer_to_unix_xid(&ip->ino_data.gid);
383 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
384 ip->ino_data.uflags);
385 return (error);
389 * hammer_vop_advlock { vp, id, op, fl, flags }
391 static
393 hammer_vop_advlock(struct vop_advlock_args *ap)
395 struct hammer_inode *ip = VTOI(ap->a_vp);
397 return (lf_advlock(ap, &ip->advlock, ip->ino_rec.ino_size));
401 * hammer_vop_close { vp, fflag }
403 static
405 hammer_vop_close(struct vop_close_args *ap)
407 return (vop_stdclose(ap));
411 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
413 * The operating system has already ensured that the directory entry
414 * does not exist and done all appropriate namespace locking.
416 static
418 hammer_vop_ncreate(struct vop_ncreate_args *ap)
420 struct hammer_transaction trans;
421 struct hammer_inode *dip;
422 struct hammer_inode *nip;
423 struct nchandle *nch;
424 int error;
426 nch = ap->a_nch;
427 dip = VTOI(ap->a_dvp);
429 if (dip->flags & HAMMER_INODE_RO)
430 return (EROFS);
433 * Create a transaction to cover the operations we perform.
435 hammer_start_transaction(&trans, dip->hmp);
438 * Create a new filesystem object of the requested type. The
439 * returned inode will be referenced but not locked.
442 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
443 if (error)
444 kprintf("hammer_create_inode error %d\n", error);
445 if (error) {
446 hammer_abort_transaction(&trans);
447 *ap->a_vpp = NULL;
448 return (error);
452 * Add the new filesystem object to the directory. This will also
453 * bump the inode's link count.
455 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
456 if (error)
457 kprintf("hammer_ip_add_directory error %d\n", error);
460 * Finish up.
462 if (error) {
463 hammer_rel_inode(nip, 0);
464 hammer_abort_transaction(&trans);
465 *ap->a_vpp = NULL;
466 } else {
467 hammer_commit_transaction(&trans);
468 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
469 hammer_rel_inode(nip, 0);
470 if (error == 0) {
471 cache_setunresolved(ap->a_nch);
472 cache_setvp(ap->a_nch, *ap->a_vpp);
475 return (error);
479 * hammer_vop_getattr { vp, vap }
481 static
483 hammer_vop_getattr(struct vop_getattr_args *ap)
485 struct hammer_inode *ip = VTOI(ap->a_vp);
486 struct vattr *vap = ap->a_vap;
488 #if 0
489 if (cache_check_fsmid_vp(ap->a_vp, &ip->fsmid) &&
490 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0 &&
491 ip->obj_asof == XXX
493 /* LAZYMOD XXX */
495 hammer_itimes(ap->a_vp);
496 #endif
498 vap->va_fsid = ip->hmp->fsid_udev;
499 vap->va_fileid = ip->ino_rec.base.base.obj_id;
500 vap->va_mode = ip->ino_data.mode;
501 vap->va_nlink = ip->ino_rec.ino_nlinks;
502 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
503 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
504 vap->va_rmajor = 0;
505 vap->va_rminor = 0;
506 vap->va_size = ip->ino_rec.ino_size;
507 hammer_to_timespec(ip->ino_rec.ino_atime, &vap->va_atime);
508 hammer_to_timespec(ip->ino_rec.ino_mtime, &vap->va_mtime);
509 hammer_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
510 vap->va_flags = ip->ino_data.uflags;
511 vap->va_gen = 1; /* hammer inums are unique for all time */
512 vap->va_blocksize = 32768; /* XXX - extract from root volume */
513 vap->va_bytes = ip->ino_rec.ino_size;
514 vap->va_type = hammer_get_vnode_type(ip->ino_rec.base.base.obj_type);
515 vap->va_filerev = 0; /* XXX */
516 /* mtime uniquely identifies any adjustments made to the file */
517 vap->va_fsmid = ip->ino_rec.ino_mtime;
518 vap->va_uid_uuid = ip->ino_data.uid;
519 vap->va_gid_uuid = ip->ino_data.gid;
520 vap->va_fsid_uuid = ip->hmp->fsid;
521 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
522 VA_FSID_UUID_VALID;
524 switch (ip->ino_rec.base.base.obj_type) {
525 case HAMMER_OBJTYPE_CDEV:
526 case HAMMER_OBJTYPE_BDEV:
527 vap->va_rmajor = ip->ino_data.rmajor;
528 vap->va_rminor = ip->ino_data.rminor;
529 break;
530 default:
531 break;
534 return(0);
538 * hammer_vop_nresolve { nch, dvp, cred }
540 * Locate the requested directory entry.
542 static
544 hammer_vop_nresolve(struct vop_nresolve_args *ap)
546 struct namecache *ncp;
547 hammer_inode_t dip;
548 hammer_inode_t ip;
549 hammer_tid_t asof;
550 struct hammer_cursor cursor;
551 union hammer_record_ondisk *rec;
552 struct vnode *vp;
553 int64_t namekey;
554 int error;
555 int i;
556 int nlen;
557 int flags;
560 * Misc initialization, plus handle as-of name extensions. Look for
561 * the '@@' extension. Note that as-of files and directories cannot
562 * be modified.
564 dip = VTOI(ap->a_dvp);
565 ncp = ap->a_nch->ncp;
566 asof = dip->obj_asof;
567 nlen = ncp->nc_nlen;
568 flags = dip->flags;
570 for (i = 0; i < nlen; ++i) {
571 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
572 asof = hammer_str_to_tid(ncp->nc_name + i + 2);
573 kprintf("ASOF %016llx\n", asof);
574 flags |= HAMMER_INODE_RO;
575 break;
578 nlen = i;
581 * If there is no path component the time extension is relative to
582 * dip.
584 if (nlen == 0) {
585 ip = hammer_get_inode(dip->hmp, &dip->cache[1], dip->obj_id,
586 asof, flags, &error);
587 if (error == 0) {
588 error = hammer_get_vnode(ip, LK_EXCLUSIVE, &vp);
589 hammer_rel_inode(ip, 0);
590 } else {
591 vp = NULL;
593 if (error == 0) {
594 vn_unlock(vp);
595 cache_setvp(ap->a_nch, vp);
596 vrele(vp);
598 return(error);
602 * Calculate the namekey and setup the key range for the scan. This
603 * works kinda like a chained hash table where the lower 32 bits
604 * of the namekey synthesize the chain.
606 * The key range is inclusive of both key_beg and key_end.
608 namekey = hammer_directory_namekey(ncp->nc_name, nlen);
610 hammer_init_cursor_hmp(&cursor, &dip->cache[0], dip->hmp);
611 cursor.key_beg.obj_id = dip->obj_id;
612 cursor.key_beg.key = namekey;
613 cursor.key_beg.create_tid = asof;
614 cursor.key_beg.delete_tid = 0;
615 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
616 cursor.key_beg.obj_type = 0;
618 cursor.key_end = cursor.key_beg;
619 cursor.key_end.key |= 0xFFFFFFFFULL;
620 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
623 * Scan all matching records (the chain), locate the one matching
624 * the requested path component.
626 * The hammer_ip_*() functions merge in-memory records with on-disk
627 * records for the purposes of the search.
629 error = hammer_ip_first(&cursor, dip);
630 rec = NULL;
631 while (error == 0) {
632 error = hammer_ip_resolve_data(&cursor);
633 if (error)
634 break;
635 rec = cursor.record;
636 if (nlen == rec->entry.base.data_len &&
637 bcmp(ncp->nc_name, cursor.data, nlen) == 0) {
638 break;
640 error = hammer_ip_next(&cursor);
642 if (error == 0) {
643 ip = hammer_get_inode(dip->hmp, &dip->cache[1],
644 rec->entry.obj_id, asof,
645 flags, &error);
646 if (error == 0) {
647 error = hammer_get_vnode(ip, LK_EXCLUSIVE, &vp);
648 hammer_rel_inode(ip, 0);
649 } else {
650 vp = NULL;
652 if (error == 0) {
653 vn_unlock(vp);
654 cache_setvp(ap->a_nch, vp);
655 vrele(vp);
657 } else if (error == ENOENT) {
658 cache_setvp(ap->a_nch, NULL);
660 hammer_done_cursor(&cursor);
661 return (error);
665 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
667 * Locate the parent directory of a directory vnode.
669 * dvp is referenced but not locked. *vpp must be returned referenced and
670 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
671 * at the root, instead it could indicate that the directory we were in was
672 * removed.
674 static
676 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
678 struct hammer_inode *dip;
679 struct hammer_inode *ip;
680 u_int64_t parent_obj_id;
681 int error;
683 dip = VTOI(ap->a_dvp);
684 if ((parent_obj_id = dip->ino_data.parent_obj_id) == 0) {
685 *ap->a_vpp = NULL;
686 return ENOENT;
689 ip = hammer_get_inode(dip->hmp, &dip->cache[1], parent_obj_id,
690 dip->obj_asof, dip->flags, &error);
691 if (ip == NULL) {
692 *ap->a_vpp = NULL;
693 return(error);
695 error = hammer_get_vnode(ip, LK_EXCLUSIVE, ap->a_vpp);
696 hammer_rel_inode(ip, 0);
697 return (error);
701 * hammer_vop_nlink { nch, dvp, vp, cred }
703 static
705 hammer_vop_nlink(struct vop_nlink_args *ap)
707 struct hammer_transaction trans;
708 struct hammer_inode *dip;
709 struct hammer_inode *ip;
710 struct nchandle *nch;
711 int error;
713 nch = ap->a_nch;
714 dip = VTOI(ap->a_dvp);
715 ip = VTOI(ap->a_vp);
717 if (dip->flags & HAMMER_INODE_RO)
718 return (EROFS);
719 if (ip->flags & HAMMER_INODE_RO)
720 return (EROFS);
723 * Create a transaction to cover the operations we perform.
725 hammer_start_transaction(&trans, dip->hmp);
728 * Add the filesystem object to the directory. Note that neither
729 * dip nor ip are referenced or locked, but their vnodes are
730 * referenced. This function will bump the inode's link count.
732 error = hammer_ip_add_directory(&trans, dip, nch->ncp, ip);
735 * Finish up.
737 if (error) {
738 hammer_abort_transaction(&trans);
739 } else {
740 cache_setunresolved(nch);
741 cache_setvp(nch, ap->a_vp);
742 hammer_commit_transaction(&trans);
744 return (error);
748 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
750 * The operating system has already ensured that the directory entry
751 * does not exist and done all appropriate namespace locking.
753 static
755 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
757 struct hammer_transaction trans;
758 struct hammer_inode *dip;
759 struct hammer_inode *nip;
760 struct nchandle *nch;
761 int error;
763 nch = ap->a_nch;
764 dip = VTOI(ap->a_dvp);
766 if (dip->flags & HAMMER_INODE_RO)
767 return (EROFS);
770 * Create a transaction to cover the operations we perform.
772 hammer_start_transaction(&trans, dip->hmp);
775 * Create a new filesystem object of the requested type. The
776 * returned inode will be referenced but not locked.
778 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
779 if (error)
780 kprintf("hammer_mkdir error %d\n", error);
781 if (error) {
782 hammer_abort_transaction(&trans);
783 *ap->a_vpp = NULL;
784 return (error);
788 * Add the new filesystem object to the directory. This will also
789 * bump the inode's link count.
791 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
792 if (error)
793 kprintf("hammer_mkdir (add) error %d\n", error);
796 * Finish up.
798 if (error) {
799 hammer_rel_inode(nip, 0);
800 hammer_abort_transaction(&trans);
801 *ap->a_vpp = NULL;
802 } else {
803 hammer_commit_transaction(&trans);
804 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
805 hammer_rel_inode(nip, 0);
806 if (error == 0) {
807 cache_setunresolved(ap->a_nch);
808 cache_setvp(ap->a_nch, *ap->a_vpp);
811 return (error);
815 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
817 * The operating system has already ensured that the directory entry
818 * does not exist and done all appropriate namespace locking.
820 static
822 hammer_vop_nmknod(struct vop_nmknod_args *ap)
824 struct hammer_transaction trans;
825 struct hammer_inode *dip;
826 struct hammer_inode *nip;
827 struct nchandle *nch;
828 int error;
830 nch = ap->a_nch;
831 dip = VTOI(ap->a_dvp);
833 if (dip->flags & HAMMER_INODE_RO)
834 return (EROFS);
837 * Create a transaction to cover the operations we perform.
839 hammer_start_transaction(&trans, dip->hmp);
842 * Create a new filesystem object of the requested type. The
843 * returned inode will be referenced but not locked.
845 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
846 if (error) {
847 hammer_abort_transaction(&trans);
848 *ap->a_vpp = NULL;
849 return (error);
853 * Add the new filesystem object to the directory. This will also
854 * bump the inode's link count.
856 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
859 * Finish up.
861 if (error) {
862 hammer_rel_inode(nip, 0);
863 hammer_abort_transaction(&trans);
864 *ap->a_vpp = NULL;
865 } else {
866 hammer_commit_transaction(&trans);
867 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
868 hammer_rel_inode(nip, 0);
869 if (error == 0) {
870 cache_setunresolved(ap->a_nch);
871 cache_setvp(ap->a_nch, *ap->a_vpp);
874 return (error);
878 * hammer_vop_open { vp, mode, cred, fp }
880 static
882 hammer_vop_open(struct vop_open_args *ap)
884 if ((ap->a_mode & FWRITE) && (VTOI(ap->a_vp)->flags & HAMMER_INODE_RO))
885 return (EROFS);
887 return(vop_stdopen(ap));
891 * hammer_vop_pathconf { vp, name, retval }
893 static
895 hammer_vop_pathconf(struct vop_pathconf_args *ap)
897 return EOPNOTSUPP;
901 * hammer_vop_print { vp }
903 static
905 hammer_vop_print(struct vop_print_args *ap)
907 return EOPNOTSUPP;
911 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
913 static
915 hammer_vop_readdir(struct vop_readdir_args *ap)
917 struct hammer_cursor cursor;
918 struct hammer_inode *ip;
919 struct uio *uio;
920 hammer_record_ondisk_t rec;
921 hammer_base_elm_t base;
922 int error;
923 int cookie_index;
924 int ncookies;
925 off_t *cookies;
926 off_t saveoff;
927 int r;
929 ip = VTOI(ap->a_vp);
930 uio = ap->a_uio;
931 saveoff = uio->uio_offset;
933 if (ap->a_ncookies) {
934 ncookies = uio->uio_resid / 16 + 1;
935 if (ncookies > 1024)
936 ncookies = 1024;
937 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
938 cookie_index = 0;
939 } else {
940 ncookies = -1;
941 cookies = NULL;
942 cookie_index = 0;
946 * Handle artificial entries
948 error = 0;
949 if (saveoff == 0) {
950 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
951 if (r)
952 goto done;
953 if (cookies)
954 cookies[cookie_index] = saveoff;
955 ++saveoff;
956 ++cookie_index;
957 if (cookie_index == ncookies)
958 goto done;
960 if (saveoff == 1) {
961 if (ip->ino_data.parent_obj_id) {
962 r = vop_write_dirent(&error, uio,
963 ip->ino_data.parent_obj_id,
964 DT_DIR, 2, "..");
965 } else {
966 r = vop_write_dirent(&error, uio,
967 ip->obj_id, DT_DIR, 2, "..");
969 if (r)
970 goto done;
971 if (cookies)
972 cookies[cookie_index] = saveoff;
973 ++saveoff;
974 ++cookie_index;
975 if (cookie_index == ncookies)
976 goto done;
980 * Key range (begin and end inclusive) to scan. Directory keys
981 * directly translate to a 64 bit 'seek' position.
983 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
984 cursor.key_beg.obj_id = ip->obj_id;
985 cursor.key_beg.create_tid = ip->obj_asof;
986 cursor.key_beg.delete_tid = 0;
987 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
988 cursor.key_beg.obj_type = 0;
989 cursor.key_beg.key = saveoff;
991 cursor.key_end = cursor.key_beg;
992 cursor.key_end.key = HAMMER_MAX_KEY;
993 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
995 error = hammer_ip_first(&cursor, ip);
997 while (error == 0) {
998 error = hammer_ip_resolve_data(&cursor);
999 if (error)
1000 break;
1001 rec = cursor.record;
1002 base = &rec->base.base;
1003 saveoff = base->key;
1005 if (base->obj_id != ip->obj_id)
1006 panic("readdir: bad record at %p", cursor.node);
1008 r = vop_write_dirent(
1009 &error, uio, rec->entry.obj_id,
1010 hammer_get_dtype(rec->entry.base.base.obj_type),
1011 rec->entry.base.data_len,
1012 (void *)cursor.data);
1013 if (r)
1014 break;
1015 ++saveoff;
1016 if (cookies)
1017 cookies[cookie_index] = base->key;
1018 ++cookie_index;
1019 if (cookie_index == ncookies)
1020 break;
1021 error = hammer_ip_next(&cursor);
1023 hammer_done_cursor(&cursor);
1025 done:
1026 if (ap->a_eofflag)
1027 *ap->a_eofflag = (error == ENOENT);
1028 uio->uio_offset = saveoff;
1029 if (error && cookie_index == 0) {
1030 if (error == ENOENT)
1031 error = 0;
1032 if (cookies) {
1033 kfree(cookies, M_TEMP);
1034 *ap->a_ncookies = 0;
1035 *ap->a_cookies = NULL;
1037 } else {
1038 if (error == ENOENT)
1039 error = 0;
1040 if (cookies) {
1041 *ap->a_ncookies = cookie_index;
1042 *ap->a_cookies = cookies;
1045 return(error);
1049 * hammer_vop_readlink { vp, uio, cred }
1051 static
1053 hammer_vop_readlink(struct vop_readlink_args *ap)
1055 struct hammer_cursor cursor;
1056 struct hammer_inode *ip;
1057 int error;
1059 ip = VTOI(ap->a_vp);
1060 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1063 * Key range (begin and end inclusive) to scan. Directory keys
1064 * directly translate to a 64 bit 'seek' position.
1066 cursor.key_beg.obj_id = ip->obj_id;
1067 cursor.key_beg.create_tid = ip->obj_asof;
1068 cursor.key_beg.delete_tid = 0;
1069 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1070 cursor.key_beg.obj_type = 0;
1071 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1073 error = hammer_ip_lookup(&cursor, ip);
1074 if (error == 0) {
1075 error = hammer_ip_resolve_data(&cursor);
1076 if (error == 0) {
1077 error = uiomove((char *)cursor.data,
1078 cursor.record->generic.base.data_len,
1079 ap->a_uio);
1082 hammer_done_cursor(&cursor);
1083 return(error);
1087 * hammer_vop_nremove { nch, dvp, cred }
1089 static
1091 hammer_vop_nremove(struct vop_nremove_args *ap)
1093 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, 0));
1097 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1099 static
1101 hammer_vop_nrename(struct vop_nrename_args *ap)
1103 struct hammer_transaction trans;
1104 struct namecache *fncp;
1105 struct namecache *tncp;
1106 struct hammer_inode *fdip;
1107 struct hammer_inode *tdip;
1108 struct hammer_inode *ip;
1109 struct hammer_cursor cursor;
1110 union hammer_record_ondisk *rec;
1111 int64_t namekey;
1112 int error;
1114 fdip = VTOI(ap->a_fdvp);
1115 tdip = VTOI(ap->a_tdvp);
1116 fncp = ap->a_fnch->ncp;
1117 tncp = ap->a_tnch->ncp;
1118 ip = VTOI(fncp->nc_vp);
1119 KKASSERT(ip != NULL);
1121 if (fdip->flags & HAMMER_INODE_RO)
1122 return (EROFS);
1123 if (tdip->flags & HAMMER_INODE_RO)
1124 return (EROFS);
1125 if (ip->flags & HAMMER_INODE_RO)
1126 return (EROFS);
1128 hammer_start_transaction(&trans, fdip->hmp);
1131 * Remove tncp from the target directory and then link ip as
1132 * tncp. XXX pass trans to dounlink
1134 error = hammer_dounlink(ap->a_tnch, ap->a_tdvp, ap->a_cred, 0);
1135 if (error == 0 || error == ENOENT)
1136 error = hammer_ip_add_directory(&trans, tdip, tncp, ip);
1137 if (error)
1138 goto failed; /* XXX */
1141 * Locate the record in the originating directory and remove it.
1143 * Calculate the namekey and setup the key range for the scan. This
1144 * works kinda like a chained hash table where the lower 32 bits
1145 * of the namekey synthesize the chain.
1147 * The key range is inclusive of both key_beg and key_end.
1149 namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
1151 hammer_init_cursor_hmp(&cursor, &fdip->cache[0], fdip->hmp);
1152 cursor.key_beg.obj_id = fdip->obj_id;
1153 cursor.key_beg.key = namekey;
1154 cursor.key_beg.create_tid = fdip->obj_asof;
1155 cursor.key_beg.delete_tid = 0;
1156 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1157 cursor.key_beg.obj_type = 0;
1159 cursor.key_end = cursor.key_beg;
1160 cursor.key_end.key |= 0xFFFFFFFFULL;
1161 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1164 * Scan all matching records (the chain), locate the one matching
1165 * the requested path component.
1167 * The hammer_ip_*() functions merge in-memory records with on-disk
1168 * records for the purposes of the search.
1170 error = hammer_ip_first(&cursor, fdip);
1171 while (error == 0) {
1172 if (hammer_ip_resolve_data(&cursor) != 0)
1173 break;
1174 rec = cursor.record;
1175 if (fncp->nc_nlen == rec->entry.base.data_len &&
1176 bcmp(fncp->nc_name, cursor.data, fncp->nc_nlen) == 0) {
1177 break;
1179 error = hammer_ip_next(&cursor);
1183 * If all is ok we have to get the inode so we can adjust nlinks.
1185 if (error)
1186 goto done;
1187 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1189 if (error == 0) {
1190 cache_rename(ap->a_fnch, ap->a_tnch);
1191 cache_setvp(ap->a_tnch, ip->vp);
1193 done:
1194 hammer_done_cursor(&cursor);
1195 failed:
1196 if (error == 0) {
1197 hammer_commit_transaction(&trans);
1198 } else {
1199 hammer_abort_transaction(&trans);
1201 return (error);
1205 * hammer_vop_nrmdir { nch, dvp, cred }
1207 static
1209 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1211 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, 0));
1215 * hammer_vop_setattr { vp, vap, cred }
1217 static
1219 hammer_vop_setattr(struct vop_setattr_args *ap)
1221 struct hammer_transaction trans;
1222 struct hammer_cursor *spike = NULL;
1223 struct vattr *vap;
1224 struct hammer_inode *ip;
1225 int modflags;
1226 int error;
1227 int64_t aligned_size;
1228 u_int32_t flags;
1229 uuid_t uuid;
1231 vap = ap->a_vap;
1232 ip = ap->a_vp->v_data;
1233 modflags = 0;
1235 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1236 return(EROFS);
1237 if (ip->flags & HAMMER_INODE_RO)
1238 return (EROFS);
1240 hammer_start_transaction(&trans, ip->hmp);
1241 error = 0;
1243 if (vap->va_flags != VNOVAL) {
1244 flags = ip->ino_data.uflags;
1245 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1246 hammer_to_unix_xid(&ip->ino_data.uid),
1247 ap->a_cred);
1248 if (error == 0) {
1249 if (ip->ino_data.uflags != flags) {
1250 ip->ino_data.uflags = flags;
1251 modflags |= HAMMER_INODE_DDIRTY;
1253 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1254 error = 0;
1255 goto done;
1258 goto done;
1260 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1261 error = EPERM;
1262 goto done;
1264 if (vap->va_uid != (uid_t)VNOVAL) {
1265 hammer_guid_to_uuid(&uuid, vap->va_uid);
1266 if (bcmp(&uuid, &ip->ino_data.uid, sizeof(uuid)) != 0) {
1267 ip->ino_data.uid = uuid;
1268 modflags |= HAMMER_INODE_DDIRTY;
1271 if (vap->va_gid != (uid_t)VNOVAL) {
1272 hammer_guid_to_uuid(&uuid, vap->va_gid);
1273 if (bcmp(&uuid, &ip->ino_data.gid, sizeof(uuid)) != 0) {
1274 ip->ino_data.gid = uuid;
1275 modflags |= HAMMER_INODE_DDIRTY;
1278 while (vap->va_size != VNOVAL && ip->ino_rec.ino_size != vap->va_size) {
1279 switch(ap->a_vp->v_type) {
1280 case VREG:
1281 if (vap->va_size < ip->ino_rec.ino_size) {
1282 vtruncbuf(ap->a_vp, vap->va_size,
1283 HAMMER_BUFSIZE);
1284 } else if (vap->va_size > ip->ino_rec.ino_size) {
1285 vnode_pager_setsize(ap->a_vp, vap->va_size);
1287 aligned_size = (vap->va_size + HAMMER_BUFMASK) &
1288 ~(int64_t)HAMMER_BUFMASK;
1289 error = hammer_ip_delete_range(&trans, ip,
1290 aligned_size,
1291 0x7FFFFFFFFFFFFFFFLL,
1292 &spike);
1293 ip->ino_rec.ino_size = vap->va_size;
1294 modflags |= HAMMER_INODE_RDIRTY;
1295 break;
1296 case VDATABASE:
1297 error = hammer_ip_delete_range(&trans, ip,
1298 vap->va_size,
1299 0x7FFFFFFFFFFFFFFFLL,
1300 &spike);
1301 ip->ino_rec.ino_size = vap->va_size;
1302 modflags |= HAMMER_INODE_RDIRTY;
1303 break;
1304 default:
1305 error = EINVAL;
1306 goto done;
1308 if (error == ENOSPC) {
1309 error = hammer_spike(&spike);
1310 if (error == 0)
1311 continue;
1313 KKASSERT(spike == NULL);
1314 break;
1316 if (vap->va_atime.tv_sec != VNOVAL) {
1317 ip->ino_rec.ino_atime =
1318 hammer_timespec_to_transid(&vap->va_atime);
1319 modflags |= HAMMER_INODE_ITIMES;
1321 if (vap->va_mtime.tv_sec != VNOVAL) {
1322 ip->ino_rec.ino_mtime =
1323 hammer_timespec_to_transid(&vap->va_mtime);
1324 modflags |= HAMMER_INODE_ITIMES;
1326 if (vap->va_mode != (mode_t)VNOVAL) {
1327 if (ip->ino_data.mode != vap->va_mode) {
1328 ip->ino_data.mode = vap->va_mode;
1329 modflags |= HAMMER_INODE_DDIRTY;
1332 done:
1333 if (error) {
1334 hammer_abort_transaction(&trans);
1335 } else {
1336 hammer_modify_inode(&trans, ip, modflags);
1337 hammer_commit_transaction(&trans);
1339 return (error);
1343 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1345 static
1347 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
1349 struct hammer_transaction trans;
1350 struct hammer_inode *dip;
1351 struct hammer_inode *nip;
1352 struct nchandle *nch;
1353 hammer_record_t record;
1354 int error;
1355 int bytes;
1357 ap->a_vap->va_type = VLNK;
1359 nch = ap->a_nch;
1360 dip = VTOI(ap->a_dvp);
1362 if (dip->flags & HAMMER_INODE_RO)
1363 return (EROFS);
1366 * Create a transaction to cover the operations we perform.
1368 hammer_start_transaction(&trans, dip->hmp);
1371 * Create a new filesystem object of the requested type. The
1372 * returned inode will be referenced but not locked.
1375 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
1376 if (error) {
1377 hammer_abort_transaction(&trans);
1378 *ap->a_vpp = NULL;
1379 return (error);
1383 * Add the new filesystem object to the directory. This will also
1384 * bump the inode's link count.
1386 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
1389 * Add a record representing the symlink. symlink stores the link
1390 * as pure data, not a string, and is no \0 terminated.
1392 if (error == 0) {
1393 record = hammer_alloc_mem_record(nip);
1394 bytes = strlen(ap->a_target);
1396 record->rec.generic.base.base.key = HAMMER_FIXKEY_SYMLINK;
1397 record->rec.generic.base.base.rec_type = HAMMER_RECTYPE_FIX;
1398 record->rec.generic.base.data_len = bytes;
1399 if (bytes <= sizeof(record->rec.generic.filler)) {
1400 record->data = (void *)record->rec.generic.filler;
1401 bcopy(ap->a_target, record->data, bytes);
1402 } else {
1403 record->data = (void *)ap->a_target;
1404 /* will be reallocated by routine below */
1406 error = hammer_ip_add_record(&trans, record);
1410 * Finish up.
1412 if (error) {
1413 hammer_rel_inode(nip, 0);
1414 hammer_abort_transaction(&trans);
1415 *ap->a_vpp = NULL;
1416 } else {
1417 hammer_commit_transaction(&trans);
1418 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
1419 hammer_rel_inode(nip, 0);
1420 if (error == 0) {
1421 cache_setunresolved(ap->a_nch);
1422 cache_setvp(ap->a_nch, *ap->a_vpp);
1425 return (error);
1429 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1431 static
1433 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
1435 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, ap->a_flags));
1439 * hammer_vop_strategy { vp, bio }
1441 * Strategy call, used for regular file read & write only. Note that the
1442 * bp may represent a cluster.
1444 * To simplify operation and allow better optimizations in the future,
1445 * this code does not make any assumptions with regards to buffer alignment
1446 * or size.
1448 static
1450 hammer_vop_strategy(struct vop_strategy_args *ap)
1452 struct buf *bp;
1453 int error;
1455 bp = ap->a_bio->bio_buf;
1457 switch(bp->b_cmd) {
1458 case BUF_CMD_READ:
1459 error = hammer_vop_strategy_read(ap);
1460 break;
1461 case BUF_CMD_WRITE:
1462 error = hammer_vop_strategy_write(ap);
1463 break;
1464 default:
1465 error = EINVAL;
1466 break;
1468 bp->b_error = error;
1469 if (error)
1470 bp->b_flags |= B_ERROR;
1471 biodone(ap->a_bio);
1472 return (error);
1476 * Read from a regular file. Iterate the related records and fill in the
1477 * BIO/BUF. Gaps are zero-filled.
1479 * The support code in hammer_object.c should be used to deal with mixed
1480 * in-memory and on-disk records.
1482 * XXX atime update
1484 static
1486 hammer_vop_strategy_read(struct vop_strategy_args *ap)
1488 struct hammer_inode *ip = ap->a_vp->v_data;
1489 struct hammer_cursor cursor;
1490 hammer_record_ondisk_t rec;
1491 hammer_base_elm_t base;
1492 struct bio *bio;
1493 struct buf *bp;
1494 int64_t rec_offset;
1495 int64_t ran_end;
1496 int64_t tmp64;
1497 int error;
1498 int boff;
1499 int roff;
1500 int n;
1502 bio = ap->a_bio;
1503 bp = bio->bio_buf;
1505 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
1508 * Key range (begin and end inclusive) to scan. Note that the key's
1509 * stored in the actual records represent BASE+LEN, not BASE. The
1510 * first record containing bio_offset will have a key > bio_offset.
1512 cursor.key_beg.obj_id = ip->obj_id;
1513 cursor.key_beg.create_tid = ip->obj_asof;
1514 cursor.key_beg.delete_tid = 0;
1515 cursor.key_beg.obj_type = 0;
1516 cursor.key_beg.key = bio->bio_offset + 1;
1518 cursor.key_end = cursor.key_beg;
1519 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1520 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
1521 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
1522 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1523 } else {
1524 ran_end = bio->bio_offset + bp->b_bufsize;
1525 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
1526 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
1527 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
1528 if (tmp64 < ran_end)
1529 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1530 else
1531 cursor.key_end.key = ran_end + MAXPHYS + 1;
1533 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1535 error = hammer_ip_first(&cursor, ip);
1536 boff = 0;
1538 while (error == 0) {
1539 error = hammer_ip_resolve_data(&cursor);
1540 if (error)
1541 break;
1542 rec = cursor.record;
1543 base = &rec->base.base;
1545 rec_offset = base->key - rec->data.base.data_len;
1548 * Calculate the gap, if any, and zero-fill it.
1550 n = (int)(rec_offset - (bio->bio_offset + boff));
1551 if (n > 0) {
1552 if (n > bp->b_bufsize - boff)
1553 n = bp->b_bufsize - boff;
1554 kprintf("zfill %d bytes\n", n);
1555 bzero((char *)bp->b_data + boff, n);
1556 boff += n;
1557 n = 0;
1561 * Calculate the data offset in the record and the number
1562 * of bytes we can copy.
1564 * Note there is a degenerate case here where boff may
1565 * already be at bp->b_bufsize.
1567 roff = -n;
1568 n = rec->data.base.data_len - roff;
1569 KKASSERT(n > 0);
1570 if (n > bp->b_bufsize - boff)
1571 n = bp->b_bufsize - boff;
1572 bcopy((char *)cursor.data + roff, (char *)bp->b_data + boff, n);
1573 boff += n;
1574 if (boff == bp->b_bufsize)
1575 break;
1576 error = hammer_ip_next(&cursor);
1578 hammer_done_cursor(&cursor);
1581 * There may have been a gap after the last record
1583 if (error == ENOENT)
1584 error = 0;
1585 if (error == 0 && boff != bp->b_bufsize) {
1586 KKASSERT(boff < bp->b_bufsize);
1587 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
1588 /* boff = bp->b_bufsize; */
1590 bp->b_resid = 0;
1591 return(error);
1595 * Write to a regular file. Iterate the related records and mark for
1596 * deletion. If existing edge records (left and right side) overlap our
1597 * write they have to be marked deleted and new records created, usually
1598 * referencing a portion of the original data. Then add a record to
1599 * represent the buffer.
1601 * The support code in hammer_object.c should be used to deal with mixed
1602 * in-memory and on-disk records.
1604 static
1606 hammer_vop_strategy_write(struct vop_strategy_args *ap)
1608 struct hammer_transaction trans;
1609 struct hammer_cursor *spike = NULL;
1610 hammer_inode_t ip;
1611 struct bio *bio;
1612 struct buf *bp;
1613 int error;
1615 bio = ap->a_bio;
1616 bp = bio->bio_buf;
1617 ip = ap->a_vp->v_data;
1619 if (ip->flags & HAMMER_INODE_RO)
1620 return (EROFS);
1622 hammer_start_transaction(&trans, ip->hmp);
1624 retry:
1626 * Delete any records overlapping our range. This function will
1627 * (eventually) properly truncate partial overlaps.
1629 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1630 error = hammer_ip_delete_range(&trans, ip, bio->bio_offset,
1631 bio->bio_offset, &spike);
1632 } else {
1633 error = hammer_ip_delete_range(&trans, ip, bio->bio_offset,
1634 bio->bio_offset +
1635 bp->b_bufsize - 1,
1636 &spike);
1640 * Add a single record to cover the write
1642 if (error == 0) {
1643 error = hammer_ip_sync_data(&trans, ip, bio->bio_offset,
1644 bp->b_data, bp->b_bufsize,
1645 &spike);
1649 * If we ran out of space the spike structure will be filled in
1650 * and we must call hammer_spike with it, then retry.
1652 if (error == ENOSPC) {
1653 error = hammer_spike(&spike);
1654 if (error == 0)
1655 goto retry;
1657 KKASSERT(spike == NULL);
1660 * If an error occured abort the transaction
1662 if (error) {
1663 /* XXX undo deletion */
1664 hammer_abort_transaction(&trans);
1665 bp->b_resid = bp->b_bufsize;
1666 } else {
1667 hammer_commit_transaction(&trans);
1668 bp->b_resid = 0;
1670 return(error);
1674 * dounlink - disconnect a directory entry
1676 * XXX whiteout support not really in yet
1678 static int
1679 hammer_dounlink(struct nchandle *nch, struct vnode *dvp, struct ucred *cred,
1680 int flags)
1682 struct hammer_transaction trans;
1683 struct namecache *ncp;
1684 hammer_inode_t dip;
1685 hammer_inode_t ip;
1686 hammer_record_ondisk_t rec;
1687 struct hammer_cursor cursor;
1688 int64_t namekey;
1689 int error;
1692 * Calculate the namekey and setup the key range for the scan. This
1693 * works kinda like a chained hash table where the lower 32 bits
1694 * of the namekey synthesize the chain.
1696 * The key range is inclusive of both key_beg and key_end.
1698 dip = VTOI(dvp);
1699 ncp = nch->ncp;
1701 if (dip->flags & HAMMER_INODE_RO)
1702 return (EROFS);
1704 namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
1706 hammer_init_cursor_hmp(&cursor, &dip->cache[0], dip->hmp);
1707 cursor.key_beg.obj_id = dip->obj_id;
1708 cursor.key_beg.key = namekey;
1709 cursor.key_beg.create_tid = dip->obj_asof;
1710 cursor.key_beg.delete_tid = 0;
1711 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1712 cursor.key_beg.obj_type = 0;
1714 cursor.key_end = cursor.key_beg;
1715 cursor.key_end.key |= 0xFFFFFFFFULL;
1716 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1718 hammer_start_transaction(&trans, dip->hmp);
1721 * Scan all matching records (the chain), locate the one matching
1722 * the requested path component. info->last_error contains the
1723 * error code on search termination and could be 0, ENOENT, or
1724 * something else.
1726 * The hammer_ip_*() functions merge in-memory records with on-disk
1727 * records for the purposes of the search.
1729 error = hammer_ip_first(&cursor, dip);
1730 while (error == 0) {
1731 error = hammer_ip_resolve_data(&cursor);
1732 if (error)
1733 break;
1734 rec = cursor.record;
1735 if (ncp->nc_nlen == rec->entry.base.data_len &&
1736 bcmp(ncp->nc_name, cursor.data, ncp->nc_nlen) == 0) {
1737 break;
1739 error = hammer_ip_next(&cursor);
1743 * If all is ok we have to get the inode so we can adjust nlinks.
1745 * If the target is a directory, it must be empty.
1747 if (error == 0) {
1748 ip = hammer_get_inode(dip->hmp, &dip->cache[1],
1749 rec->entry.obj_id,
1750 dip->hmp->asof, 0, &error);
1751 if (error == 0 && ip->ino_rec.base.base.obj_type ==
1752 HAMMER_OBJTYPE_DIRECTORY) {
1753 error = hammer_ip_check_directory_empty(&trans, ip);
1755 if (error == 0)
1756 error = hammer_ip_del_directory(&trans, &cursor, dip, ip);
1757 if (error == 0) {
1758 cache_setunresolved(nch);
1759 cache_setvp(nch, NULL);
1760 /* XXX locking */
1761 if (ip->vp)
1762 cache_inval_vp(ip->vp, CINV_DESTROY);
1764 hammer_rel_inode(ip, 0);
1767 if (error == 0)
1768 hammer_commit_transaction(&trans);
1769 else
1770 hammer_abort_transaction(&trans);
1771 hammer_done_cursor(&cursor);
1772 return (error);
1775 /************************************************************************
1776 * FIFO AND SPECFS OPS *
1777 ************************************************************************
1781 static int
1782 hammer_vop_fifoclose (struct vop_close_args *ap)
1784 /* XXX update itimes */
1785 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
1788 static int
1789 hammer_vop_fiforead (struct vop_read_args *ap)
1791 int error;
1793 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1794 /* XXX update access time */
1795 return (error);
1798 static int
1799 hammer_vop_fifowrite (struct vop_write_args *ap)
1801 int error;
1803 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1804 /* XXX update access time */
1805 return (error);
1808 static int
1809 hammer_vop_specclose (struct vop_close_args *ap)
1811 /* XXX update itimes */
1812 return (VOCALL(&spec_vnode_vops, &ap->a_head));
1815 static int
1816 hammer_vop_specread (struct vop_read_args *ap)
1818 /* XXX update access time */
1819 return (VOCALL(&spec_vnode_vops, &ap->a_head));
1822 static int
1823 hammer_vop_specwrite (struct vop_write_args *ap)
1825 /* XXX update last change time */
1826 return (VOCALL(&spec_vnode_vops, &ap->a_head));