HAMMER 61F2/Many: Fix bug in last commit
[dragonfly.git] / sys / vfs / hammer / hammer_vnops.c
blob750e1317e31b96fd8b9656a932090320b4e86092
1 /*
2 * Copyright (c) 2007-2008 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.90 2008/07/12 23:55:22 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_bmap(struct vop_bmap_args *ap);
79 static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
80 static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
81 static int hammer_vop_ioctl(struct vop_ioctl_args *);
82 static int hammer_vop_mountctl(struct vop_mountctl_args *);
84 static int hammer_vop_fifoclose (struct vop_close_args *);
85 static int hammer_vop_fiforead (struct vop_read_args *);
86 static int hammer_vop_fifowrite (struct vop_write_args *);
88 static int hammer_vop_specclose (struct vop_close_args *);
89 static int hammer_vop_specread (struct vop_read_args *);
90 static int hammer_vop_specwrite (struct vop_write_args *);
92 struct vop_ops hammer_vnode_vops = {
93 .vop_default = vop_defaultop,
94 .vop_fsync = hammer_vop_fsync,
95 .vop_getpages = vop_stdgetpages,
96 .vop_putpages = vop_stdputpages,
97 .vop_read = hammer_vop_read,
98 .vop_write = hammer_vop_write,
99 .vop_access = hammer_vop_access,
100 .vop_advlock = hammer_vop_advlock,
101 .vop_close = hammer_vop_close,
102 .vop_ncreate = hammer_vop_ncreate,
103 .vop_getattr = hammer_vop_getattr,
104 .vop_inactive = hammer_vop_inactive,
105 .vop_reclaim = hammer_vop_reclaim,
106 .vop_nresolve = hammer_vop_nresolve,
107 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
108 .vop_nlink = hammer_vop_nlink,
109 .vop_nmkdir = hammer_vop_nmkdir,
110 .vop_nmknod = hammer_vop_nmknod,
111 .vop_open = hammer_vop_open,
112 .vop_pathconf = hammer_vop_pathconf,
113 .vop_print = hammer_vop_print,
114 .vop_readdir = hammer_vop_readdir,
115 .vop_readlink = hammer_vop_readlink,
116 .vop_nremove = hammer_vop_nremove,
117 .vop_nrename = hammer_vop_nrename,
118 .vop_nrmdir = hammer_vop_nrmdir,
119 .vop_setattr = hammer_vop_setattr,
120 .vop_bmap = hammer_vop_bmap,
121 .vop_strategy = hammer_vop_strategy,
122 .vop_nsymlink = hammer_vop_nsymlink,
123 .vop_nwhiteout = hammer_vop_nwhiteout,
124 .vop_ioctl = hammer_vop_ioctl,
125 .vop_mountctl = hammer_vop_mountctl
128 struct vop_ops hammer_spec_vops = {
129 .vop_default = spec_vnoperate,
130 .vop_fsync = hammer_vop_fsync,
131 .vop_read = hammer_vop_specread,
132 .vop_write = hammer_vop_specwrite,
133 .vop_access = hammer_vop_access,
134 .vop_close = hammer_vop_specclose,
135 .vop_getattr = hammer_vop_getattr,
136 .vop_inactive = hammer_vop_inactive,
137 .vop_reclaim = hammer_vop_reclaim,
138 .vop_setattr = hammer_vop_setattr
141 struct vop_ops hammer_fifo_vops = {
142 .vop_default = fifo_vnoperate,
143 .vop_fsync = hammer_vop_fsync,
144 .vop_read = hammer_vop_fiforead,
145 .vop_write = hammer_vop_fifowrite,
146 .vop_access = hammer_vop_access,
147 .vop_close = hammer_vop_fifoclose,
148 .vop_getattr = hammer_vop_getattr,
149 .vop_inactive = hammer_vop_inactive,
150 .vop_reclaim = hammer_vop_reclaim,
151 .vop_setattr = hammer_vop_setattr
154 #ifdef DEBUG_TRUNCATE
155 struct hammer_inode *HammerTruncIp;
156 #endif
158 static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
159 struct vnode *dvp, struct ucred *cred, int flags);
160 static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
161 static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
163 #if 0
164 static
166 hammer_vop_vnoperate(struct vop_generic_args *)
168 return (VOCALL(&hammer_vnode_vops, ap));
170 #endif
173 * hammer_vop_fsync { vp, waitfor }
175 * fsync() an inode to disk and wait for it to be completely committed
176 * such that the information would not be undone if a crash occured after
177 * return.
179 static
181 hammer_vop_fsync(struct vop_fsync_args *ap)
183 hammer_inode_t ip = VTOI(ap->a_vp);
185 ++hammer_count_fsyncs;
186 vfsync(ap->a_vp, ap->a_waitfor, 1, NULL, NULL);
187 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
188 if (ap->a_waitfor == MNT_WAIT)
189 hammer_wait_inode(ip);
190 return (ip->error);
194 * hammer_vop_read { vp, uio, ioflag, cred }
196 static
198 hammer_vop_read(struct vop_read_args *ap)
200 struct hammer_transaction trans;
201 hammer_inode_t ip;
202 off_t offset;
203 struct buf *bp;
204 struct uio *uio;
205 int error;
206 int n;
207 int seqcount;
208 int ioseqcount;
209 int blksize;
211 if (ap->a_vp->v_type != VREG)
212 return (EINVAL);
213 ip = VTOI(ap->a_vp);
214 error = 0;
215 uio = ap->a_uio;
218 * Allow the UIO's size to override the sequential heuristic.
220 blksize = hammer_blocksize(uio->uio_offset);
221 seqcount = (uio->uio_resid + (blksize - 1)) / blksize;
222 ioseqcount = ap->a_ioflag >> 16;
223 if (seqcount < ioseqcount)
224 seqcount = ioseqcount;
226 hammer_start_transaction(&trans, ip->hmp);
229 * Access the data typically in HAMMER_BUFSIZE blocks via the
230 * buffer cache, but HAMMER may use a variable block size based
231 * on the offset.
233 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
234 int64_t base_offset;
235 int64_t file_limit;
237 blksize = hammer_blocksize(uio->uio_offset);
238 offset = (int)uio->uio_offset & (blksize - 1);
239 base_offset = uio->uio_offset - offset;
241 if (hammer_debug_cluster_enable) {
243 * Use file_limit to prevent cluster_read() from
244 * creating buffers of the wrong block size past
245 * the demarc.
247 file_limit = ip->ino_data.size;
248 if (base_offset < HAMMER_XDEMARC &&
249 file_limit > HAMMER_XDEMARC) {
250 file_limit = HAMMER_XDEMARC;
252 error = cluster_read(ap->a_vp,
253 file_limit, base_offset,
254 blksize, MAXPHYS,
255 seqcount, &bp);
256 } else {
257 error = bread(ap->a_vp, base_offset, blksize, &bp);
259 if (error) {
260 kprintf("error %d\n", error);
261 brelse(bp);
262 break;
265 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
266 n = blksize - offset;
267 if (n > uio->uio_resid)
268 n = uio->uio_resid;
269 if (n > ip->ino_data.size - uio->uio_offset)
270 n = (int)(ip->ino_data.size - uio->uio_offset);
271 error = uiomove((char *)bp->b_data + offset, n, uio);
273 /* data has a lower priority then meta-data */
274 bp->b_flags |= B_AGE;
275 bqrelse(bp);
276 if (error)
277 break;
279 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
280 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
281 ip->ino_data.atime = trans.time;
282 hammer_modify_inode(ip, HAMMER_INODE_ATIME);
284 hammer_done_transaction(&trans);
285 return (error);
289 * hammer_vop_write { vp, uio, ioflag, cred }
291 static
293 hammer_vop_write(struct vop_write_args *ap)
295 struct hammer_transaction trans;
296 struct hammer_inode *ip;
297 hammer_mount_t hmp;
298 struct uio *uio;
299 int offset;
300 off_t base_offset;
301 struct buf *bp;
302 int error;
303 int n;
304 int flags;
305 int delta;
306 int seqcount;
308 if (ap->a_vp->v_type != VREG)
309 return (EINVAL);
310 ip = VTOI(ap->a_vp);
311 hmp = ip->hmp;
312 error = 0;
313 seqcount = ap->a_ioflag >> 16;
315 if (ip->flags & HAMMER_INODE_RO)
316 return (EROFS);
319 * Create a transaction to cover the operations we perform.
321 hammer_start_transaction(&trans, hmp);
322 uio = ap->a_uio;
325 * Check append mode
327 if (ap->a_ioflag & IO_APPEND)
328 uio->uio_offset = ip->ino_data.size;
331 * Check for illegal write offsets. Valid range is 0...2^63-1.
333 * NOTE: the base_off assignment is required to work around what
334 * I consider to be a GCC-4 optimization bug.
336 if (uio->uio_offset < 0) {
337 hammer_done_transaction(&trans);
338 return (EFBIG);
340 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
341 if (uio->uio_resid > 0 && base_offset <= 0) {
342 hammer_done_transaction(&trans);
343 return (EFBIG);
347 * Access the data typically in HAMMER_BUFSIZE blocks via the
348 * buffer cache, but HAMMER may use a variable block size based
349 * on the offset.
351 while (uio->uio_resid > 0) {
352 int fixsize = 0;
353 int blksize;
354 int blkmask;
356 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
357 break;
359 blksize = hammer_blocksize(uio->uio_offset);
362 * Do not allow HAMMER to blow out the buffer cache. Very
363 * large UIOs can lockout other processes due to bwillwrite()
364 * mechanics.
366 * The hammer inode is not locked during these operations.
367 * The vnode is locked which can interfere with the pageout
368 * daemon for non-UIO_NOCOPY writes but should not interfere
369 * with the buffer cache. Even so, we cannot afford to
370 * allow the pageout daemon to build up too many dirty buffer
371 * cache buffers.
373 /*if (((int)uio->uio_offset & (blksize - 1)) == 0)*/
374 bwillwrite(blksize);
377 * Do not allow HAMMER to blow out system memory by
378 * accumulating too many records. Records are so well
379 * decoupled from the buffer cache that it is possible
380 * for userland to push data out to the media via
381 * direct-write, but build up the records queued to the
382 * backend faster then the backend can flush them out.
383 * HAMMER has hit its write limit but the frontend has
384 * no pushback to slow it down.
386 if (hmp->rsv_recs > hammer_limit_recs / 2) {
388 * Get the inode on the flush list
390 if (ip->rsv_recs >= 64)
391 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
392 else if (ip->rsv_recs >= 16)
393 hammer_flush_inode(ip, 0);
396 * Keep the flusher going if the system keeps
397 * queueing records.
399 delta = hmp->count_newrecords -
400 hmp->last_newrecords;
401 if (delta < 0 || delta > hammer_limit_recs / 2) {
402 hmp->last_newrecords = hmp->count_newrecords;
403 hammer_sync_hmp(hmp, MNT_NOWAIT);
407 * If we have gotten behind start slowing
408 * down the writers.
410 delta = (hmp->rsv_recs - hammer_limit_recs) *
411 hz / hammer_limit_recs;
412 if (delta > 0)
413 tsleep(&trans, 0, "hmrslo", delta);
417 * Calculate the blocksize at the current offset and figure
418 * out how much we can actually write.
420 blkmask = blksize - 1;
421 offset = (int)uio->uio_offset & blkmask;
422 base_offset = uio->uio_offset & ~(int64_t)blkmask;
423 n = blksize - offset;
424 if (n > uio->uio_resid)
425 n = uio->uio_resid;
426 if (uio->uio_offset + n > ip->ino_data.size) {
427 vnode_pager_setsize(ap->a_vp, uio->uio_offset + n);
428 fixsize = 1;
431 if (uio->uio_segflg == UIO_NOCOPY) {
433 * Issuing a write with the same data backing the
434 * buffer. Instantiate the buffer to collect the
435 * backing vm pages, then read-in any missing bits.
437 * This case is used by vop_stdputpages().
439 bp = getblk(ap->a_vp, base_offset,
440 blksize, GETBLK_BHEAVY, 0);
441 if ((bp->b_flags & B_CACHE) == 0) {
442 bqrelse(bp);
443 error = bread(ap->a_vp, base_offset,
444 blksize, &bp);
446 } else if (offset == 0 && uio->uio_resid >= blksize) {
448 * Even though we are entirely overwriting the buffer
449 * we may still have to zero it out to avoid a
450 * mmap/write visibility issue.
452 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
453 if ((bp->b_flags & B_CACHE) == 0)
454 vfs_bio_clrbuf(bp);
455 } else if (base_offset >= ip->ino_data.size) {
457 * If the base offset of the buffer is beyond the
458 * file EOF, we don't have to issue a read.
460 bp = getblk(ap->a_vp, base_offset,
461 blksize, GETBLK_BHEAVY, 0);
462 vfs_bio_clrbuf(bp);
463 } else {
465 * Partial overwrite, read in any missing bits then
466 * replace the portion being written.
468 error = bread(ap->a_vp, base_offset, blksize, &bp);
469 if (error == 0)
470 bheavy(bp);
472 if (error == 0) {
473 error = uiomove((char *)bp->b_data + offset,
474 n, uio);
478 * If we screwed up we have to undo any VM size changes we
479 * made.
481 if (error) {
482 brelse(bp);
483 if (fixsize) {
484 vtruncbuf(ap->a_vp, ip->ino_data.size,
485 hammer_blocksize(ip->ino_data.size));
487 break;
489 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
490 if (ip->ino_data.size < uio->uio_offset) {
491 ip->ino_data.size = uio->uio_offset;
492 flags = HAMMER_INODE_DDIRTY;
493 vnode_pager_setsize(ap->a_vp, ip->ino_data.size);
494 } else {
495 flags = 0;
497 ip->ino_data.mtime = trans.time;
498 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
499 hammer_modify_inode(ip, flags);
502 * Final buffer disposition.
504 bp->b_flags |= B_AGE;
505 if (ap->a_ioflag & IO_SYNC) {
506 bwrite(bp);
507 } else if (ap->a_ioflag & IO_DIRECT) {
508 bawrite(bp);
509 } else {
510 bdwrite(bp);
513 hammer_done_transaction(&trans);
514 return (error);
518 * hammer_vop_access { vp, mode, cred }
520 static
522 hammer_vop_access(struct vop_access_args *ap)
524 struct hammer_inode *ip = VTOI(ap->a_vp);
525 uid_t uid;
526 gid_t gid;
527 int error;
529 uid = hammer_to_unix_xid(&ip->ino_data.uid);
530 gid = hammer_to_unix_xid(&ip->ino_data.gid);
532 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
533 ip->ino_data.uflags);
534 return (error);
538 * hammer_vop_advlock { vp, id, op, fl, flags }
540 static
542 hammer_vop_advlock(struct vop_advlock_args *ap)
544 hammer_inode_t ip = VTOI(ap->a_vp);
546 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
550 * hammer_vop_close { vp, fflag }
552 static
554 hammer_vop_close(struct vop_close_args *ap)
556 hammer_inode_t ip = VTOI(ap->a_vp);
558 if ((ip->flags | ip->sync_flags) & HAMMER_INODE_MODMASK)
559 hammer_inode_waitreclaims(ip->hmp);
560 return (vop_stdclose(ap));
564 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
566 * The operating system has already ensured that the directory entry
567 * does not exist and done all appropriate namespace locking.
569 static
571 hammer_vop_ncreate(struct vop_ncreate_args *ap)
573 struct hammer_transaction trans;
574 struct hammer_inode *dip;
575 struct hammer_inode *nip;
576 struct nchandle *nch;
577 int error;
579 nch = ap->a_nch;
580 dip = VTOI(ap->a_dvp);
582 if (dip->flags & HAMMER_INODE_RO)
583 return (EROFS);
584 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
585 return (error);
588 * Create a transaction to cover the operations we perform.
590 hammer_start_transaction(&trans, dip->hmp);
593 * Create a new filesystem object of the requested type. The
594 * returned inode will be referenced and shared-locked to prevent
595 * it from being moved to the flusher.
598 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
599 dip, NULL, &nip);
600 if (error) {
601 hkprintf("hammer_create_inode error %d\n", error);
602 hammer_done_transaction(&trans);
603 *ap->a_vpp = NULL;
604 return (error);
608 * Add the new filesystem object to the directory. This will also
609 * bump the inode's link count.
611 error = hammer_ip_add_directory(&trans, dip,
612 nch->ncp->nc_name, nch->ncp->nc_nlen,
613 nip);
614 if (error)
615 hkprintf("hammer_ip_add_directory error %d\n", error);
618 * Finish up.
620 if (error) {
621 hammer_rel_inode(nip, 0);
622 hammer_done_transaction(&trans);
623 *ap->a_vpp = NULL;
624 } else {
625 error = hammer_get_vnode(nip, ap->a_vpp);
626 hammer_done_transaction(&trans);
627 hammer_rel_inode(nip, 0);
628 if (error == 0) {
629 cache_setunresolved(ap->a_nch);
630 cache_setvp(ap->a_nch, *ap->a_vpp);
633 return (error);
637 * hammer_vop_getattr { vp, vap }
639 * Retrieve an inode's attribute information. When accessing inodes
640 * historically we fake the atime field to ensure consistent results.
641 * The atime field is stored in the B-Tree element and allowed to be
642 * updated without cycling the element.
644 static
646 hammer_vop_getattr(struct vop_getattr_args *ap)
648 struct hammer_inode *ip = VTOI(ap->a_vp);
649 struct vattr *vap = ap->a_vap;
652 * We want the fsid to be different when accessing a filesystem
653 * with different as-of's so programs like diff don't think
654 * the files are the same.
656 * We also want the fsid to be the same when comparing snapshots,
657 * or when comparing mirrors (which might be backed by different
658 * physical devices). HAMMER fsids are based on the PFS's
659 * shared_uuid field.
661 * XXX there is a chance of collision here. The va_fsid reported
662 * by stat is different from the more involved fsid used in the
663 * mount structure.
665 vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
666 (u_int32_t)(ip->obj_asof >> 32);
668 vap->va_fileid = ip->ino_leaf.base.obj_id;
669 vap->va_mode = ip->ino_data.mode;
670 vap->va_nlink = ip->ino_data.nlinks;
671 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
672 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
673 vap->va_rmajor = 0;
674 vap->va_rminor = 0;
675 vap->va_size = ip->ino_data.size;
678 * We must provide a consistent atime and mtime for snapshots
679 * so people can do a 'tar cf - ... | md5' on them and get
680 * consistent results.
682 if (ip->flags & HAMMER_INODE_RO) {
683 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
684 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
685 } else {
686 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
687 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
689 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
690 vap->va_flags = ip->ino_data.uflags;
691 vap->va_gen = 1; /* hammer inums are unique for all time */
692 vap->va_blocksize = HAMMER_BUFSIZE;
693 if (ip->ino_data.size >= HAMMER_XDEMARC) {
694 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
695 ~HAMMER_XBUFMASK64;
696 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
697 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
698 ~HAMMER_BUFMASK64;
699 } else {
700 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
702 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
703 vap->va_filerev = 0; /* XXX */
704 /* mtime uniquely identifies any adjustments made to the file XXX */
705 vap->va_fsmid = ip->ino_data.mtime;
706 vap->va_uid_uuid = ip->ino_data.uid;
707 vap->va_gid_uuid = ip->ino_data.gid;
708 vap->va_fsid_uuid = ip->hmp->fsid;
709 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
710 VA_FSID_UUID_VALID;
712 switch (ip->ino_data.obj_type) {
713 case HAMMER_OBJTYPE_CDEV:
714 case HAMMER_OBJTYPE_BDEV:
715 vap->va_rmajor = ip->ino_data.rmajor;
716 vap->va_rminor = ip->ino_data.rminor;
717 break;
718 default:
719 break;
721 return(0);
725 * hammer_vop_nresolve { nch, dvp, cred }
727 * Locate the requested directory entry.
729 static
731 hammer_vop_nresolve(struct vop_nresolve_args *ap)
733 struct hammer_transaction trans;
734 struct namecache *ncp;
735 hammer_inode_t dip;
736 hammer_inode_t ip;
737 hammer_tid_t asof;
738 struct hammer_cursor cursor;
739 struct vnode *vp;
740 int64_t namekey;
741 int error;
742 int i;
743 int nlen;
744 int flags;
745 int ispfs;
746 int64_t obj_id;
747 u_int32_t localization;
750 * Misc initialization, plus handle as-of name extensions. Look for
751 * the '@@' extension. Note that as-of files and directories cannot
752 * be modified.
754 dip = VTOI(ap->a_dvp);
755 ncp = ap->a_nch->ncp;
756 asof = dip->obj_asof;
757 nlen = ncp->nc_nlen;
758 flags = dip->flags & HAMMER_INODE_RO;
759 ispfs = 0;
761 hammer_simple_transaction(&trans, dip->hmp);
763 for (i = 0; i < nlen; ++i) {
764 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
765 asof = hammer_str_to_tid(ncp->nc_name + i + 2,
766 &ispfs, &localization);
767 if (asof != HAMMER_MAX_TID)
768 flags |= HAMMER_INODE_RO;
769 break;
772 nlen = i;
775 * If this is a PFS softlink we dive into the PFS
777 if (ispfs && nlen == 0) {
778 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
779 asof, localization,
780 flags, &error);
781 if (error == 0) {
782 error = hammer_get_vnode(ip, &vp);
783 hammer_rel_inode(ip, 0);
784 } else {
785 vp = NULL;
787 if (error == 0) {
788 vn_unlock(vp);
789 cache_setvp(ap->a_nch, vp);
790 vrele(vp);
792 goto done;
796 * If there is no path component the time extension is relative to
797 * dip.
799 if (nlen == 0) {
800 ip = hammer_get_inode(&trans, dip, dip->obj_id,
801 asof, dip->obj_localization,
802 flags, &error);
803 if (error == 0) {
804 error = hammer_get_vnode(ip, &vp);
805 hammer_rel_inode(ip, 0);
806 } else {
807 vp = NULL;
809 if (error == 0) {
810 vn_unlock(vp);
811 cache_setvp(ap->a_nch, vp);
812 vrele(vp);
814 goto done;
818 * Calculate the namekey and setup the key range for the scan. This
819 * works kinda like a chained hash table where the lower 32 bits
820 * of the namekey synthesize the chain.
822 * The key range is inclusive of both key_beg and key_end.
824 namekey = hammer_directory_namekey(ncp->nc_name, nlen);
826 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
827 cursor.key_beg.localization = dip->obj_localization +
828 HAMMER_LOCALIZE_MISC;
829 cursor.key_beg.obj_id = dip->obj_id;
830 cursor.key_beg.key = namekey;
831 cursor.key_beg.create_tid = 0;
832 cursor.key_beg.delete_tid = 0;
833 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
834 cursor.key_beg.obj_type = 0;
836 cursor.key_end = cursor.key_beg;
837 cursor.key_end.key |= 0xFFFFFFFFULL;
838 cursor.asof = asof;
839 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
842 * Scan all matching records (the chain), locate the one matching
843 * the requested path component.
845 * The hammer_ip_*() functions merge in-memory records with on-disk
846 * records for the purposes of the search.
848 obj_id = 0;
849 localization = HAMMER_DEF_LOCALIZATION;
851 if (error == 0) {
852 error = hammer_ip_first(&cursor);
853 while (error == 0) {
854 error = hammer_ip_resolve_data(&cursor);
855 if (error)
856 break;
857 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
858 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
859 obj_id = cursor.data->entry.obj_id;
860 localization = cursor.data->entry.localization;
861 break;
863 error = hammer_ip_next(&cursor);
866 hammer_done_cursor(&cursor);
867 if (error == 0) {
868 ip = hammer_get_inode(&trans, dip, obj_id,
869 asof, localization,
870 flags, &error);
871 if (error == 0) {
872 error = hammer_get_vnode(ip, &vp);
873 hammer_rel_inode(ip, 0);
874 } else {
875 vp = NULL;
877 if (error == 0) {
878 vn_unlock(vp);
879 cache_setvp(ap->a_nch, vp);
880 vrele(vp);
882 } else if (error == ENOENT) {
883 cache_setvp(ap->a_nch, NULL);
885 done:
886 hammer_done_transaction(&trans);
887 return (error);
891 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
893 * Locate the parent directory of a directory vnode.
895 * dvp is referenced but not locked. *vpp must be returned referenced and
896 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
897 * at the root, instead it could indicate that the directory we were in was
898 * removed.
900 * NOTE: as-of sequences are not linked into the directory structure. If
901 * we are at the root with a different asof then the mount point, reload
902 * the same directory with the mount point's asof. I'm not sure what this
903 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
904 * get confused, but it hasn't been tested.
906 static
908 hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
910 struct hammer_transaction trans;
911 struct hammer_inode *dip;
912 struct hammer_inode *ip;
913 int64_t parent_obj_id;
914 u_int32_t parent_obj_localization;
915 hammer_tid_t asof;
916 int error;
918 dip = VTOI(ap->a_dvp);
919 asof = dip->obj_asof;
922 * Whos are parent? This could be the root of a pseudo-filesystem
923 * whos parent is in another localization domain.
925 parent_obj_id = dip->ino_data.parent_obj_id;
926 if (dip->obj_id == HAMMER_OBJID_ROOT)
927 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
928 else
929 parent_obj_localization = dip->obj_localization;
931 if (parent_obj_id == 0) {
932 if (dip->obj_id == HAMMER_OBJID_ROOT &&
933 asof != dip->hmp->asof) {
934 parent_obj_id = dip->obj_id;
935 asof = dip->hmp->asof;
936 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
937 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
938 dip->obj_asof);
939 } else {
940 *ap->a_vpp = NULL;
941 return ENOENT;
945 hammer_simple_transaction(&trans, dip->hmp);
947 ip = hammer_get_inode(&trans, dip, parent_obj_id,
948 asof, parent_obj_localization,
949 dip->flags, &error);
950 if (ip) {
951 error = hammer_get_vnode(ip, ap->a_vpp);
952 hammer_rel_inode(ip, 0);
953 } else {
954 *ap->a_vpp = NULL;
956 hammer_done_transaction(&trans);
957 return (error);
961 * hammer_vop_nlink { nch, dvp, vp, cred }
963 static
965 hammer_vop_nlink(struct vop_nlink_args *ap)
967 struct hammer_transaction trans;
968 struct hammer_inode *dip;
969 struct hammer_inode *ip;
970 struct nchandle *nch;
971 int error;
973 nch = ap->a_nch;
974 dip = VTOI(ap->a_dvp);
975 ip = VTOI(ap->a_vp);
977 if (dip->flags & HAMMER_INODE_RO)
978 return (EROFS);
979 if (ip->flags & HAMMER_INODE_RO)
980 return (EROFS);
981 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
982 return (error);
985 * Create a transaction to cover the operations we perform.
987 hammer_start_transaction(&trans, dip->hmp);
990 * Add the filesystem object to the directory. Note that neither
991 * dip nor ip are referenced or locked, but their vnodes are
992 * referenced. This function will bump the inode's link count.
994 error = hammer_ip_add_directory(&trans, dip,
995 nch->ncp->nc_name, nch->ncp->nc_nlen,
996 ip);
999 * Finish up.
1001 if (error == 0) {
1002 cache_setunresolved(nch);
1003 cache_setvp(nch, ap->a_vp);
1005 hammer_done_transaction(&trans);
1006 return (error);
1010 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1012 * The operating system has already ensured that the directory entry
1013 * does not exist and done all appropriate namespace locking.
1015 static
1017 hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
1019 struct hammer_transaction trans;
1020 struct hammer_inode *dip;
1021 struct hammer_inode *nip;
1022 struct nchandle *nch;
1023 int error;
1025 nch = ap->a_nch;
1026 dip = VTOI(ap->a_dvp);
1028 if (dip->flags & HAMMER_INODE_RO)
1029 return (EROFS);
1030 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1031 return (error);
1034 * Create a transaction to cover the operations we perform.
1036 hammer_start_transaction(&trans, dip->hmp);
1039 * Create a new filesystem object of the requested type. The
1040 * returned inode will be referenced but not locked.
1042 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1043 dip, NULL, &nip);
1044 if (error) {
1045 hkprintf("hammer_mkdir error %d\n", error);
1046 hammer_done_transaction(&trans);
1047 *ap->a_vpp = NULL;
1048 return (error);
1051 * Add the new filesystem object to the directory. This will also
1052 * bump the inode's link count.
1054 error = hammer_ip_add_directory(&trans, dip,
1055 nch->ncp->nc_name, nch->ncp->nc_nlen,
1056 nip);
1057 if (error)
1058 hkprintf("hammer_mkdir (add) error %d\n", error);
1061 * Finish up.
1063 if (error) {
1064 hammer_rel_inode(nip, 0);
1065 *ap->a_vpp = NULL;
1066 } else {
1067 error = hammer_get_vnode(nip, ap->a_vpp);
1068 hammer_rel_inode(nip, 0);
1069 if (error == 0) {
1070 cache_setunresolved(ap->a_nch);
1071 cache_setvp(ap->a_nch, *ap->a_vpp);
1074 hammer_done_transaction(&trans);
1075 return (error);
1079 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1081 * The operating system has already ensured that the directory entry
1082 * does not exist and done all appropriate namespace locking.
1084 static
1086 hammer_vop_nmknod(struct vop_nmknod_args *ap)
1088 struct hammer_transaction trans;
1089 struct hammer_inode *dip;
1090 struct hammer_inode *nip;
1091 struct nchandle *nch;
1092 int error;
1094 nch = ap->a_nch;
1095 dip = VTOI(ap->a_dvp);
1097 if (dip->flags & HAMMER_INODE_RO)
1098 return (EROFS);
1099 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1100 return (error);
1103 * Create a transaction to cover the operations we perform.
1105 hammer_start_transaction(&trans, dip->hmp);
1108 * Create a new filesystem object of the requested type. The
1109 * returned inode will be referenced but not locked.
1111 * If mknod specifies a directory a pseudo-fs is created.
1113 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1114 dip, NULL, &nip);
1115 if (error) {
1116 hammer_done_transaction(&trans);
1117 *ap->a_vpp = NULL;
1118 return (error);
1122 * Add the new filesystem object to the directory. This will also
1123 * bump the inode's link count.
1125 error = hammer_ip_add_directory(&trans, dip,
1126 nch->ncp->nc_name, nch->ncp->nc_nlen,
1127 nip);
1130 * Finish up.
1132 if (error) {
1133 hammer_rel_inode(nip, 0);
1134 *ap->a_vpp = NULL;
1135 } else {
1136 error = hammer_get_vnode(nip, ap->a_vpp);
1137 hammer_rel_inode(nip, 0);
1138 if (error == 0) {
1139 cache_setunresolved(ap->a_nch);
1140 cache_setvp(ap->a_nch, *ap->a_vpp);
1143 hammer_done_transaction(&trans);
1144 return (error);
1148 * hammer_vop_open { vp, mode, cred, fp }
1150 static
1152 hammer_vop_open(struct vop_open_args *ap)
1154 hammer_inode_t ip;
1156 ip = VTOI(ap->a_vp);
1158 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
1159 return (EROFS);
1160 return(vop_stdopen(ap));
1164 * hammer_vop_pathconf { vp, name, retval }
1166 static
1168 hammer_vop_pathconf(struct vop_pathconf_args *ap)
1170 return EOPNOTSUPP;
1174 * hammer_vop_print { vp }
1176 static
1178 hammer_vop_print(struct vop_print_args *ap)
1180 return EOPNOTSUPP;
1184 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
1186 static
1188 hammer_vop_readdir(struct vop_readdir_args *ap)
1190 struct hammer_transaction trans;
1191 struct hammer_cursor cursor;
1192 struct hammer_inode *ip;
1193 struct uio *uio;
1194 hammer_base_elm_t base;
1195 int error;
1196 int cookie_index;
1197 int ncookies;
1198 off_t *cookies;
1199 off_t saveoff;
1200 int r;
1201 int dtype;
1203 ip = VTOI(ap->a_vp);
1204 uio = ap->a_uio;
1205 saveoff = uio->uio_offset;
1207 if (ap->a_ncookies) {
1208 ncookies = uio->uio_resid / 16 + 1;
1209 if (ncookies > 1024)
1210 ncookies = 1024;
1211 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1212 cookie_index = 0;
1213 } else {
1214 ncookies = -1;
1215 cookies = NULL;
1216 cookie_index = 0;
1219 hammer_simple_transaction(&trans, ip->hmp);
1222 * Handle artificial entries
1224 error = 0;
1225 if (saveoff == 0) {
1226 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1227 if (r)
1228 goto done;
1229 if (cookies)
1230 cookies[cookie_index] = saveoff;
1231 ++saveoff;
1232 ++cookie_index;
1233 if (cookie_index == ncookies)
1234 goto done;
1236 if (saveoff == 1) {
1237 if (ip->ino_data.parent_obj_id) {
1238 r = vop_write_dirent(&error, uio,
1239 ip->ino_data.parent_obj_id,
1240 DT_DIR, 2, "..");
1241 } else {
1242 r = vop_write_dirent(&error, uio,
1243 ip->obj_id, DT_DIR, 2, "..");
1245 if (r)
1246 goto done;
1247 if (cookies)
1248 cookies[cookie_index] = saveoff;
1249 ++saveoff;
1250 ++cookie_index;
1251 if (cookie_index == ncookies)
1252 goto done;
1256 * Key range (begin and end inclusive) to scan. Directory keys
1257 * directly translate to a 64 bit 'seek' position.
1259 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1260 cursor.key_beg.localization = ip->obj_localization +
1261 HAMMER_LOCALIZE_MISC;
1262 cursor.key_beg.obj_id = ip->obj_id;
1263 cursor.key_beg.create_tid = 0;
1264 cursor.key_beg.delete_tid = 0;
1265 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1266 cursor.key_beg.obj_type = 0;
1267 cursor.key_beg.key = saveoff;
1269 cursor.key_end = cursor.key_beg;
1270 cursor.key_end.key = HAMMER_MAX_KEY;
1271 cursor.asof = ip->obj_asof;
1272 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1274 error = hammer_ip_first(&cursor);
1276 while (error == 0) {
1277 error = hammer_ip_resolve_data(&cursor);
1278 if (error)
1279 break;
1280 base = &cursor.leaf->base;
1281 saveoff = base->key;
1282 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
1284 if (base->obj_id != ip->obj_id)
1285 panic("readdir: bad record at %p", cursor.node);
1288 * Convert pseudo-filesystems into softlinks
1290 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
1291 r = vop_write_dirent(
1292 &error, uio, cursor.data->entry.obj_id,
1293 dtype,
1294 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1295 (void *)cursor.data->entry.name);
1296 if (r)
1297 break;
1298 ++saveoff;
1299 if (cookies)
1300 cookies[cookie_index] = base->key;
1301 ++cookie_index;
1302 if (cookie_index == ncookies)
1303 break;
1304 error = hammer_ip_next(&cursor);
1306 hammer_done_cursor(&cursor);
1308 done:
1309 hammer_done_transaction(&trans);
1311 if (ap->a_eofflag)
1312 *ap->a_eofflag = (error == ENOENT);
1313 uio->uio_offset = saveoff;
1314 if (error && cookie_index == 0) {
1315 if (error == ENOENT)
1316 error = 0;
1317 if (cookies) {
1318 kfree(cookies, M_TEMP);
1319 *ap->a_ncookies = 0;
1320 *ap->a_cookies = NULL;
1322 } else {
1323 if (error == ENOENT)
1324 error = 0;
1325 if (cookies) {
1326 *ap->a_ncookies = cookie_index;
1327 *ap->a_cookies = cookies;
1330 return(error);
1334 * hammer_vop_readlink { vp, uio, cred }
1336 static
1338 hammer_vop_readlink(struct vop_readlink_args *ap)
1340 struct hammer_transaction trans;
1341 struct hammer_cursor cursor;
1342 struct hammer_inode *ip;
1343 char buf[32];
1344 u_int32_t localization;
1345 hammer_pseudofs_inmem_t pfsm;
1346 int error;
1348 ip = VTOI(ap->a_vp);
1351 * Shortcut if the symlink data was stuffed into ino_data.
1353 * Also expand special "@@PFS%05d" softlinks (expansion only
1354 * occurs for non-historical (current) accesses made from the
1355 * primary filesystem).
1357 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1358 char *ptr;
1359 int bytes;
1361 ptr = ip->ino_data.ext.symlink;
1362 bytes = (int)ip->ino_data.size;
1363 if (bytes == 10 &&
1364 ip->obj_asof == HAMMER_MAX_TID &&
1365 ip->obj_localization == 0 &&
1366 strncmp(ptr, "@@PFS", 5) == 0) {
1367 hammer_simple_transaction(&trans, ip->hmp);
1368 bcopy(ptr + 5, buf, 5);
1369 buf[5] = 0;
1370 localization = strtoul(buf, NULL, 10) << 16;
1371 pfsm = hammer_load_pseudofs(&trans, localization,
1372 &error);
1373 if (error == 0) {
1374 if (pfsm->pfsd.mirror_flags &
1375 HAMMER_PFSD_SLAVE) {
1376 ksnprintf(buf, sizeof(buf),
1377 "@@0x%016llx:%05d",
1378 pfsm->pfsd.sync_end_tid,
1379 localization >> 16);
1380 } else {
1381 ksnprintf(buf, sizeof(buf),
1382 "@@0x%016llx:%05d",
1383 HAMMER_MAX_TID,
1384 localization >> 16);
1386 ptr = buf;
1387 bytes = strlen(buf);
1389 if (pfsm)
1390 hammer_rel_pseudofs(trans.hmp, pfsm);
1391 hammer_done_transaction(&trans);
1393 error = uiomove(ptr, bytes, ap->a_uio);
1394 return(error);
1398 * Long version
1400 hammer_simple_transaction(&trans, ip->hmp);
1401 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
1404 * Key range (begin and end inclusive) to scan. Directory keys
1405 * directly translate to a 64 bit 'seek' position.
1407 cursor.key_beg.localization = ip->obj_localization +
1408 HAMMER_LOCALIZE_MISC;
1409 cursor.key_beg.obj_id = ip->obj_id;
1410 cursor.key_beg.create_tid = 0;
1411 cursor.key_beg.delete_tid = 0;
1412 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1413 cursor.key_beg.obj_type = 0;
1414 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
1415 cursor.asof = ip->obj_asof;
1416 cursor.flags |= HAMMER_CURSOR_ASOF;
1418 error = hammer_ip_lookup(&cursor);
1419 if (error == 0) {
1420 error = hammer_ip_resolve_data(&cursor);
1421 if (error == 0) {
1422 KKASSERT(cursor.leaf->data_len >=
1423 HAMMER_SYMLINK_NAME_OFF);
1424 error = uiomove(cursor.data->symlink.name,
1425 cursor.leaf->data_len -
1426 HAMMER_SYMLINK_NAME_OFF,
1427 ap->a_uio);
1430 hammer_done_cursor(&cursor);
1431 hammer_done_transaction(&trans);
1432 return(error);
1436 * hammer_vop_nremove { nch, dvp, cred }
1438 static
1440 hammer_vop_nremove(struct vop_nremove_args *ap)
1442 struct hammer_transaction trans;
1443 struct hammer_inode *dip;
1444 int error;
1446 dip = VTOI(ap->a_dvp);
1448 if (hammer_nohistory(dip) == 0 &&
1449 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1450 return (error);
1453 hammer_start_transaction(&trans, dip->hmp);
1454 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1455 hammer_done_transaction(&trans);
1457 return (error);
1461 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1463 static
1465 hammer_vop_nrename(struct vop_nrename_args *ap)
1467 struct hammer_transaction trans;
1468 struct namecache *fncp;
1469 struct namecache *tncp;
1470 struct hammer_inode *fdip;
1471 struct hammer_inode *tdip;
1472 struct hammer_inode *ip;
1473 struct hammer_cursor cursor;
1474 int64_t namekey;
1475 int nlen, error;
1477 fdip = VTOI(ap->a_fdvp);
1478 tdip = VTOI(ap->a_tdvp);
1479 fncp = ap->a_fnch->ncp;
1480 tncp = ap->a_tnch->ncp;
1481 ip = VTOI(fncp->nc_vp);
1482 KKASSERT(ip != NULL);
1484 if (fdip->flags & HAMMER_INODE_RO)
1485 return (EROFS);
1486 if (tdip->flags & HAMMER_INODE_RO)
1487 return (EROFS);
1488 if (ip->flags & HAMMER_INODE_RO)
1489 return (EROFS);
1490 if ((error = hammer_checkspace(fdip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1491 return (error);
1493 hammer_start_transaction(&trans, fdip->hmp);
1496 * Remove tncp from the target directory and then link ip as
1497 * tncp. XXX pass trans to dounlink
1499 * Force the inode sync-time to match the transaction so it is
1500 * in-sync with the creation of the target directory entry.
1502 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp, ap->a_cred, 0);
1503 if (error == 0 || error == ENOENT) {
1504 error = hammer_ip_add_directory(&trans, tdip,
1505 tncp->nc_name, tncp->nc_nlen,
1506 ip);
1507 if (error == 0) {
1508 ip->ino_data.parent_obj_id = tdip->obj_id;
1509 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
1512 if (error)
1513 goto failed; /* XXX */
1516 * Locate the record in the originating directory and remove it.
1518 * Calculate the namekey and setup the key range for the scan. This
1519 * works kinda like a chained hash table where the lower 32 bits
1520 * of the namekey synthesize the chain.
1522 * The key range is inclusive of both key_beg and key_end.
1524 namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
1525 retry:
1526 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
1527 cursor.key_beg.localization = fdip->obj_localization +
1528 HAMMER_LOCALIZE_MISC;
1529 cursor.key_beg.obj_id = fdip->obj_id;
1530 cursor.key_beg.key = namekey;
1531 cursor.key_beg.create_tid = 0;
1532 cursor.key_beg.delete_tid = 0;
1533 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1534 cursor.key_beg.obj_type = 0;
1536 cursor.key_end = cursor.key_beg;
1537 cursor.key_end.key |= 0xFFFFFFFFULL;
1538 cursor.asof = fdip->obj_asof;
1539 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
1542 * Scan all matching records (the chain), locate the one matching
1543 * the requested path component.
1545 * The hammer_ip_*() functions merge in-memory records with on-disk
1546 * records for the purposes of the search.
1548 error = hammer_ip_first(&cursor);
1549 while (error == 0) {
1550 if (hammer_ip_resolve_data(&cursor) != 0)
1551 break;
1552 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
1553 KKASSERT(nlen > 0);
1554 if (fncp->nc_nlen == nlen &&
1555 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1556 break;
1558 error = hammer_ip_next(&cursor);
1562 * If all is ok we have to get the inode so we can adjust nlinks.
1564 * WARNING: hammer_ip_del_directory() may have to terminate the
1565 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1566 * twice.
1568 if (error == 0)
1569 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
1572 * XXX A deadlock here will break rename's atomicy for the purposes
1573 * of crash recovery.
1575 if (error == EDEADLK) {
1576 hammer_done_cursor(&cursor);
1577 goto retry;
1581 * Cleanup and tell the kernel that the rename succeeded.
1583 hammer_done_cursor(&cursor);
1584 if (error == 0)
1585 cache_rename(ap->a_fnch, ap->a_tnch);
1587 failed:
1588 hammer_done_transaction(&trans);
1589 return (error);
1593 * hammer_vop_nrmdir { nch, dvp, cred }
1595 static
1597 hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
1599 struct hammer_transaction trans;
1600 struct hammer_inode *dip;
1601 int error;
1603 dip = VTOI(ap->a_dvp);
1605 if (hammer_nohistory(dip) == 0 &&
1606 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1607 return (error);
1610 hammer_start_transaction(&trans, dip->hmp);
1611 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1612 hammer_done_transaction(&trans);
1614 return (error);
1618 * hammer_vop_setattr { vp, vap, cred }
1620 static
1622 hammer_vop_setattr(struct vop_setattr_args *ap)
1624 struct hammer_transaction trans;
1625 struct vattr *vap;
1626 struct hammer_inode *ip;
1627 int modflags;
1628 int error;
1629 int truncating;
1630 int blksize;
1631 int64_t aligned_size;
1632 u_int32_t flags;
1634 vap = ap->a_vap;
1635 ip = ap->a_vp->v_data;
1636 modflags = 0;
1638 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1639 return(EROFS);
1640 if (ip->flags & HAMMER_INODE_RO)
1641 return (EROFS);
1642 if (hammer_nohistory(ip) == 0 &&
1643 (error = hammer_checkspace(ip->hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
1644 return (error);
1647 hammer_start_transaction(&trans, ip->hmp);
1648 error = 0;
1650 if (vap->va_flags != VNOVAL) {
1651 flags = ip->ino_data.uflags;
1652 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1653 hammer_to_unix_xid(&ip->ino_data.uid),
1654 ap->a_cred);
1655 if (error == 0) {
1656 if (ip->ino_data.uflags != flags) {
1657 ip->ino_data.uflags = flags;
1658 modflags |= HAMMER_INODE_DDIRTY;
1660 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1661 error = 0;
1662 goto done;
1665 goto done;
1667 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1668 error = EPERM;
1669 goto done;
1671 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
1672 mode_t cur_mode = ip->ino_data.mode;
1673 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1674 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1675 uuid_t uuid_uid;
1676 uuid_t uuid_gid;
1678 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
1679 ap->a_cred,
1680 &cur_uid, &cur_gid, &cur_mode);
1681 if (error == 0) {
1682 hammer_guid_to_uuid(&uuid_uid, cur_uid);
1683 hammer_guid_to_uuid(&uuid_gid, cur_gid);
1684 if (bcmp(&uuid_uid, &ip->ino_data.uid,
1685 sizeof(uuid_uid)) ||
1686 bcmp(&uuid_gid, &ip->ino_data.gid,
1687 sizeof(uuid_gid)) ||
1688 ip->ino_data.mode != cur_mode
1690 ip->ino_data.uid = uuid_uid;
1691 ip->ino_data.gid = uuid_gid;
1692 ip->ino_data.mode = cur_mode;
1694 modflags |= HAMMER_INODE_DDIRTY;
1697 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
1698 switch(ap->a_vp->v_type) {
1699 case VREG:
1700 if (vap->va_size == ip->ino_data.size)
1701 break;
1703 * XXX break atomicy, we can deadlock the backend
1704 * if we do not release the lock. Probably not a
1705 * big deal here.
1707 blksize = hammer_blocksize(vap->va_size);
1708 if (vap->va_size < ip->ino_data.size) {
1709 vtruncbuf(ap->a_vp, vap->va_size, blksize);
1710 truncating = 1;
1711 } else {
1712 vnode_pager_setsize(ap->a_vp, vap->va_size);
1713 truncating = 0;
1715 ip->ino_data.size = vap->va_size;
1716 modflags |= HAMMER_INODE_DDIRTY;
1719 * on-media truncation is cached in the inode until
1720 * the inode is synchronized.
1722 if (truncating) {
1723 hammer_ip_frontend_trunc(ip, vap->va_size);
1724 #ifdef DEBUG_TRUNCATE
1725 if (HammerTruncIp == NULL)
1726 HammerTruncIp = ip;
1727 #endif
1728 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1729 ip->flags |= HAMMER_INODE_TRUNCATED;
1730 ip->trunc_off = vap->va_size;
1731 #ifdef DEBUG_TRUNCATE
1732 if (ip == HammerTruncIp)
1733 kprintf("truncate1 %016llx\n", ip->trunc_off);
1734 #endif
1735 } else if (ip->trunc_off > vap->va_size) {
1736 ip->trunc_off = vap->va_size;
1737 #ifdef DEBUG_TRUNCATE
1738 if (ip == HammerTruncIp)
1739 kprintf("truncate2 %016llx\n", ip->trunc_off);
1740 #endif
1741 } else {
1742 #ifdef DEBUG_TRUNCATE
1743 if (ip == HammerTruncIp)
1744 kprintf("truncate3 %016llx (ignored)\n", vap->va_size);
1745 #endif
1750 * If truncating we have to clean out a portion of
1751 * the last block on-disk. We do this in the
1752 * front-end buffer cache.
1754 aligned_size = (vap->va_size + (blksize - 1)) &
1755 ~(int64_t)(blksize - 1);
1756 if (truncating && vap->va_size < aligned_size) {
1757 struct buf *bp;
1758 int offset;
1760 aligned_size -= blksize;
1762 offset = (int)vap->va_size & (blksize - 1);
1763 error = bread(ap->a_vp, aligned_size,
1764 blksize, &bp);
1765 hammer_ip_frontend_trunc(ip, aligned_size);
1766 if (error == 0) {
1767 bzero(bp->b_data + offset,
1768 blksize - offset);
1769 bdwrite(bp);
1770 } else {
1771 kprintf("ERROR %d\n", error);
1772 brelse(bp);
1775 break;
1776 case VDATABASE:
1777 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1778 ip->flags |= HAMMER_INODE_TRUNCATED;
1779 ip->trunc_off = vap->va_size;
1780 } else if (ip->trunc_off > vap->va_size) {
1781 ip->trunc_off = vap->va_size;
1783 hammer_ip_frontend_trunc(ip, vap->va_size);
1784 ip->ino_data.size = vap->va_size;
1785 modflags |= HAMMER_INODE_DDIRTY;
1786 break;
1787 default:
1788 error = EINVAL;
1789 goto done;
1791 break;
1793 if (vap->va_atime.tv_sec != VNOVAL) {
1794 ip->ino_data.atime =
1795 hammer_timespec_to_time(&vap->va_atime);
1796 modflags |= HAMMER_INODE_ATIME;
1798 if (vap->va_mtime.tv_sec != VNOVAL) {
1799 ip->ino_data.mtime =
1800 hammer_timespec_to_time(&vap->va_mtime);
1801 modflags |= HAMMER_INODE_MTIME;
1803 if (vap->va_mode != (mode_t)VNOVAL) {
1804 mode_t cur_mode = ip->ino_data.mode;
1805 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1806 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1808 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
1809 cur_uid, cur_gid, &cur_mode);
1810 if (error == 0 && ip->ino_data.mode != cur_mode) {
1811 ip->ino_data.mode = cur_mode;
1812 modflags |= HAMMER_INODE_DDIRTY;
1815 done:
1816 if (error == 0)
1817 hammer_modify_inode(ip, modflags);
1818 hammer_done_transaction(&trans);
1819 return (error);
1823 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1825 static
1827 hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
1829 struct hammer_transaction trans;
1830 struct hammer_inode *dip;
1831 struct hammer_inode *nip;
1832 struct nchandle *nch;
1833 hammer_record_t record;
1834 int error;
1835 int bytes;
1837 ap->a_vap->va_type = VLNK;
1839 nch = ap->a_nch;
1840 dip = VTOI(ap->a_dvp);
1842 if (dip->flags & HAMMER_INODE_RO)
1843 return (EROFS);
1844 if ((error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0)
1845 return (error);
1848 * Create a transaction to cover the operations we perform.
1850 hammer_start_transaction(&trans, dip->hmp);
1853 * Create a new filesystem object of the requested type. The
1854 * returned inode will be referenced but not locked.
1857 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1858 dip, NULL, &nip);
1859 if (error) {
1860 hammer_done_transaction(&trans);
1861 *ap->a_vpp = NULL;
1862 return (error);
1866 * Add a record representing the symlink. symlink stores the link
1867 * as pure data, not a string, and is no \0 terminated.
1869 if (error == 0) {
1870 bytes = strlen(ap->a_target);
1872 if (bytes <= HAMMER_INODE_BASESYMLEN) {
1873 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
1874 } else {
1875 record = hammer_alloc_mem_record(nip, bytes);
1876 record->type = HAMMER_MEM_RECORD_GENERAL;
1878 record->leaf.base.localization = nip->obj_localization +
1879 HAMMER_LOCALIZE_MISC;
1880 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
1881 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
1882 record->leaf.data_len = bytes;
1883 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
1884 bcopy(ap->a_target, record->data->symlink.name, bytes);
1885 error = hammer_ip_add_record(&trans, record);
1889 * Set the file size to the length of the link.
1891 if (error == 0) {
1892 nip->ino_data.size = bytes;
1893 hammer_modify_inode(nip, HAMMER_INODE_DDIRTY);
1896 if (error == 0)
1897 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
1898 nch->ncp->nc_nlen, nip);
1901 * Finish up.
1903 if (error) {
1904 hammer_rel_inode(nip, 0);
1905 *ap->a_vpp = NULL;
1906 } else {
1907 error = hammer_get_vnode(nip, ap->a_vpp);
1908 hammer_rel_inode(nip, 0);
1909 if (error == 0) {
1910 cache_setunresolved(ap->a_nch);
1911 cache_setvp(ap->a_nch, *ap->a_vpp);
1914 hammer_done_transaction(&trans);
1915 return (error);
1919 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1921 static
1923 hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
1925 struct hammer_transaction trans;
1926 struct hammer_inode *dip;
1927 int error;
1929 dip = VTOI(ap->a_dvp);
1931 if (hammer_nohistory(dip) == 0 &&
1932 (error = hammer_checkspace(dip->hmp, HAMMER_CHKSPC_CREATE)) != 0) {
1933 return (error);
1936 hammer_start_transaction(&trans, dip->hmp);
1937 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
1938 ap->a_cred, ap->a_flags);
1939 hammer_done_transaction(&trans);
1941 return (error);
1945 * hammer_vop_ioctl { vp, command, data, fflag, cred }
1947 static
1949 hammer_vop_ioctl(struct vop_ioctl_args *ap)
1951 struct hammer_inode *ip = ap->a_vp->v_data;
1953 return(hammer_ioctl(ip, ap->a_command, ap->a_data,
1954 ap->a_fflag, ap->a_cred));
1957 static
1959 hammer_vop_mountctl(struct vop_mountctl_args *ap)
1961 struct mount *mp;
1962 int error;
1964 mp = ap->a_head.a_ops->head.vv_mount;
1966 switch(ap->a_op) {
1967 case MOUNTCTL_SET_EXPORT:
1968 if (ap->a_ctllen != sizeof(struct export_args))
1969 error = EINVAL;
1970 error = hammer_vfs_export(mp, ap->a_op,
1971 (const struct export_args *)ap->a_ctl);
1972 break;
1973 default:
1974 error = journal_mountctl(ap);
1975 break;
1977 return(error);
1981 * hammer_vop_strategy { vp, bio }
1983 * Strategy call, used for regular file read & write only. Note that the
1984 * bp may represent a cluster.
1986 * To simplify operation and allow better optimizations in the future,
1987 * this code does not make any assumptions with regards to buffer alignment
1988 * or size.
1990 static
1992 hammer_vop_strategy(struct vop_strategy_args *ap)
1994 struct buf *bp;
1995 int error;
1997 bp = ap->a_bio->bio_buf;
1999 switch(bp->b_cmd) {
2000 case BUF_CMD_READ:
2001 error = hammer_vop_strategy_read(ap);
2002 break;
2003 case BUF_CMD_WRITE:
2004 error = hammer_vop_strategy_write(ap);
2005 break;
2006 default:
2007 bp->b_error = error = EINVAL;
2008 bp->b_flags |= B_ERROR;
2009 biodone(ap->a_bio);
2010 break;
2012 return (error);
2016 * Read from a regular file. Iterate the related records and fill in the
2017 * BIO/BUF. Gaps are zero-filled.
2019 * The support code in hammer_object.c should be used to deal with mixed
2020 * in-memory and on-disk records.
2022 * NOTE: Can be called from the cluster code with an oversized buf.
2024 * XXX atime update
2026 static
2028 hammer_vop_strategy_read(struct vop_strategy_args *ap)
2030 struct hammer_transaction trans;
2031 struct hammer_inode *ip;
2032 struct hammer_cursor cursor;
2033 hammer_base_elm_t base;
2034 hammer_off_t disk_offset;
2035 struct bio *bio;
2036 struct bio *nbio;
2037 struct buf *bp;
2038 int64_t rec_offset;
2039 int64_t ran_end;
2040 int64_t tmp64;
2041 int error;
2042 int boff;
2043 int roff;
2044 int n;
2046 bio = ap->a_bio;
2047 bp = bio->bio_buf;
2048 ip = ap->a_vp->v_data;
2051 * The zone-2 disk offset may have been set by the cluster code via
2052 * a BMAP operation, or else should be NOOFFSET.
2054 * Checking the high bits for a match against zone-2 should suffice.
2056 nbio = push_bio(bio);
2057 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2058 HAMMER_ZONE_RAW_BUFFER) {
2059 error = hammer_io_direct_read(ip->hmp, nbio);
2060 return (error);
2064 * Well, that sucked. Do it the hard way. If all the stars are
2065 * aligned we may still be able to issue a direct-read.
2067 hammer_simple_transaction(&trans, ip->hmp);
2068 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2071 * Key range (begin and end inclusive) to scan. Note that the key's
2072 * stored in the actual records represent BASE+LEN, not BASE. The
2073 * first record containing bio_offset will have a key > bio_offset.
2075 cursor.key_beg.localization = ip->obj_localization +
2076 HAMMER_LOCALIZE_MISC;
2077 cursor.key_beg.obj_id = ip->obj_id;
2078 cursor.key_beg.create_tid = 0;
2079 cursor.key_beg.delete_tid = 0;
2080 cursor.key_beg.obj_type = 0;
2081 cursor.key_beg.key = bio->bio_offset + 1;
2082 cursor.asof = ip->obj_asof;
2083 cursor.flags |= HAMMER_CURSOR_ASOF;
2085 cursor.key_end = cursor.key_beg;
2086 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2087 #if 0
2088 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
2089 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2090 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2091 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2092 } else
2093 #endif
2095 ran_end = bio->bio_offset + bp->b_bufsize;
2096 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2097 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2098 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2099 if (tmp64 < ran_end)
2100 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2101 else
2102 cursor.key_end.key = ran_end + MAXPHYS + 1;
2104 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2106 error = hammer_ip_first(&cursor);
2107 boff = 0;
2109 while (error == 0) {
2111 * Get the base file offset of the record. The key for
2112 * data records is (base + bytes) rather then (base).
2114 base = &cursor.leaf->base;
2115 rec_offset = base->key - cursor.leaf->data_len;
2118 * Calculate the gap, if any, and zero-fill it.
2120 * n is the offset of the start of the record verses our
2121 * current seek offset in the bio.
2123 n = (int)(rec_offset - (bio->bio_offset + boff));
2124 if (n > 0) {
2125 if (n > bp->b_bufsize - boff)
2126 n = bp->b_bufsize - boff;
2127 bzero((char *)bp->b_data + boff, n);
2128 boff += n;
2129 n = 0;
2133 * Calculate the data offset in the record and the number
2134 * of bytes we can copy.
2136 * There are two degenerate cases. First, boff may already
2137 * be at bp->b_bufsize. Secondly, the data offset within
2138 * the record may exceed the record's size.
2140 roff = -n;
2141 rec_offset += roff;
2142 n = cursor.leaf->data_len - roff;
2143 if (n <= 0) {
2144 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2145 n = 0;
2146 } else if (n > bp->b_bufsize - boff) {
2147 n = bp->b_bufsize - boff;
2151 * Deal with cached truncations. This cool bit of code
2152 * allows truncate()/ftruncate() to avoid having to sync
2153 * the file.
2155 * If the frontend is truncated then all backend records are
2156 * subject to the frontend's truncation.
2158 * If the backend is truncated then backend records on-disk
2159 * (but not in-memory) are subject to the backend's
2160 * truncation. In-memory records owned by the backend
2161 * represent data written after the truncation point on the
2162 * backend and must not be truncated.
2164 * Truncate operations deal with frontend buffer cache
2165 * buffers and frontend-owned in-memory records synchronously.
2167 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2168 if (hammer_cursor_ondisk(&cursor) ||
2169 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2170 if (ip->trunc_off <= rec_offset)
2171 n = 0;
2172 else if (ip->trunc_off < rec_offset + n)
2173 n = (int)(ip->trunc_off - rec_offset);
2176 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2177 if (hammer_cursor_ondisk(&cursor)) {
2178 if (ip->sync_trunc_off <= rec_offset)
2179 n = 0;
2180 else if (ip->sync_trunc_off < rec_offset + n)
2181 n = (int)(ip->sync_trunc_off - rec_offset);
2186 * Try to issue a direct read into our bio if possible,
2187 * otherwise resolve the element data into a hammer_buffer
2188 * and copy.
2190 * The buffer on-disk should be zerod past any real
2191 * truncation point, but may not be for any synthesized
2192 * truncation point from above.
2194 if (boff == 0 && n == bp->b_bufsize &&
2195 ((cursor.leaf->data_offset + roff) & HAMMER_BUFMASK) == 0) {
2196 disk_offset = hammer_blockmap_lookup(
2197 trans.hmp,
2198 cursor.leaf->data_offset + roff,
2199 &error);
2200 if (error)
2201 break;
2202 nbio->bio_offset = disk_offset;
2203 error = hammer_io_direct_read(trans.hmp, nbio);
2204 goto done;
2205 } else if (n) {
2206 error = hammer_ip_resolve_data(&cursor);
2207 if (error == 0) {
2208 bcopy((char *)cursor.data + roff,
2209 (char *)bp->b_data + boff, n);
2212 if (error)
2213 break;
2216 * Iterate until we have filled the request.
2218 boff += n;
2219 if (boff == bp->b_bufsize)
2220 break;
2221 error = hammer_ip_next(&cursor);
2225 * There may have been a gap after the last record
2227 if (error == ENOENT)
2228 error = 0;
2229 if (error == 0 && boff != bp->b_bufsize) {
2230 KKASSERT(boff < bp->b_bufsize);
2231 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2232 /* boff = bp->b_bufsize; */
2234 bp->b_resid = 0;
2235 bp->b_error = error;
2236 if (error)
2237 bp->b_flags |= B_ERROR;
2238 biodone(ap->a_bio);
2240 done:
2241 if (cursor.node)
2242 hammer_cache_node(&ip->cache[1], cursor.node);
2243 hammer_done_cursor(&cursor);
2244 hammer_done_transaction(&trans);
2245 return(error);
2249 * BMAP operation - used to support cluster_read() only.
2251 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2253 * This routine may return EOPNOTSUPP if the opration is not supported for
2254 * the specified offset. The contents of the pointer arguments do not
2255 * need to be initialized in that case.
2257 * If a disk address is available and properly aligned return 0 with
2258 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2259 * to the run-length relative to that offset. Callers may assume that
2260 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2261 * large, so return EOPNOTSUPP if it is not sufficiently large.
2263 static
2265 hammer_vop_bmap(struct vop_bmap_args *ap)
2267 struct hammer_transaction trans;
2268 struct hammer_inode *ip;
2269 struct hammer_cursor cursor;
2270 hammer_base_elm_t base;
2271 int64_t rec_offset;
2272 int64_t ran_end;
2273 int64_t tmp64;
2274 int64_t base_offset;
2275 int64_t base_disk_offset;
2276 int64_t last_offset;
2277 hammer_off_t last_disk_offset;
2278 hammer_off_t disk_offset;
2279 int rec_len;
2280 int error;
2281 int blksize;
2283 ip = ap->a_vp->v_data;
2286 * We can only BMAP regular files. We can't BMAP database files,
2287 * directories, etc.
2289 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
2290 return(EOPNOTSUPP);
2293 * bmap is typically called with runp/runb both NULL when used
2294 * for writing. We do not support BMAP for writing atm.
2296 if (ap->a_cmd != BUF_CMD_READ)
2297 return(EOPNOTSUPP);
2300 * Scan the B-Tree to acquire blockmap addresses, then translate
2301 * to raw addresses.
2303 hammer_simple_transaction(&trans, ip->hmp);
2304 #if 0
2305 kprintf("bmap_beg %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]);
2306 #endif
2307 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2310 * Key range (begin and end inclusive) to scan. Note that the key's
2311 * stored in the actual records represent BASE+LEN, not BASE. The
2312 * first record containing bio_offset will have a key > bio_offset.
2314 cursor.key_beg.localization = ip->obj_localization +
2315 HAMMER_LOCALIZE_MISC;
2316 cursor.key_beg.obj_id = ip->obj_id;
2317 cursor.key_beg.create_tid = 0;
2318 cursor.key_beg.delete_tid = 0;
2319 cursor.key_beg.obj_type = 0;
2320 if (ap->a_runb)
2321 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
2322 else
2323 cursor.key_beg.key = ap->a_loffset + 1;
2324 if (cursor.key_beg.key < 0)
2325 cursor.key_beg.key = 0;
2326 cursor.asof = ip->obj_asof;
2327 cursor.flags |= HAMMER_CURSOR_ASOF;
2329 cursor.key_end = cursor.key_beg;
2330 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2332 ran_end = ap->a_loffset + MAXPHYS;
2333 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2334 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2335 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2336 if (tmp64 < ran_end)
2337 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2338 else
2339 cursor.key_end.key = ran_end + MAXPHYS + 1;
2341 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2343 error = hammer_ip_first(&cursor);
2344 base_offset = last_offset = 0;
2345 base_disk_offset = last_disk_offset = 0;
2347 while (error == 0) {
2349 * Get the base file offset of the record. The key for
2350 * data records is (base + bytes) rather then (base).
2352 * NOTE: rec_offset + rec_len may exceed the end-of-file.
2353 * The extra bytes should be zero on-disk and the BMAP op
2354 * should still be ok.
2356 base = &cursor.leaf->base;
2357 rec_offset = base->key - cursor.leaf->data_len;
2358 rec_len = cursor.leaf->data_len;
2361 * Incorporate any cached truncation.
2363 * NOTE: Modifications to rec_len based on synthesized
2364 * truncation points remove the guarantee that any extended
2365 * data on disk is zero (since the truncations may not have
2366 * taken place on-media yet).
2368 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2369 if (hammer_cursor_ondisk(&cursor) ||
2370 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2371 if (ip->trunc_off <= rec_offset)
2372 rec_len = 0;
2373 else if (ip->trunc_off < rec_offset + rec_len)
2374 rec_len = (int)(ip->trunc_off - rec_offset);
2377 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2378 if (hammer_cursor_ondisk(&cursor)) {
2379 if (ip->sync_trunc_off <= rec_offset)
2380 rec_len = 0;
2381 else if (ip->sync_trunc_off < rec_offset + rec_len)
2382 rec_len = (int)(ip->sync_trunc_off - rec_offset);
2387 * Accumulate information. If we have hit a discontiguous
2388 * block reset base_offset unless we are already beyond the
2389 * requested offset. If we are, that's it, we stop.
2391 disk_offset = hammer_blockmap_lookup(trans.hmp,
2392 cursor.leaf->data_offset,
2393 &error);
2394 if (error)
2395 break;
2396 if (rec_offset != last_offset ||
2397 disk_offset != last_disk_offset) {
2398 if (rec_offset > ap->a_loffset)
2399 break;
2400 base_offset = rec_offset;
2401 base_disk_offset = disk_offset;
2403 last_offset = rec_offset + rec_len;
2404 last_disk_offset = disk_offset + rec_len;
2406 error = hammer_ip_next(&cursor);
2409 #if 0
2410 kprintf("BMAP %016llx: %016llx - %016llx\n",
2411 ap->a_loffset, base_offset, last_offset);
2412 kprintf("BMAP %16s: %016llx - %016llx\n",
2413 "", base_disk_offset, last_disk_offset);
2414 #endif
2416 if (cursor.node) {
2417 hammer_cache_node(&ip->cache[1], cursor.node);
2418 #if 0
2419 kprintf("bmap_end2 %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]);
2420 #endif
2422 hammer_done_cursor(&cursor);
2423 hammer_done_transaction(&trans);
2426 * If we couldn't find any records or the records we did find were
2427 * all behind the requested offset, return failure. A forward
2428 * truncation can leave a hole w/ no on-disk records.
2430 if (last_offset == 0 || last_offset < ap->a_loffset)
2431 return (EOPNOTSUPP);
2434 * Figure out the block size at the requested offset and adjust
2435 * our limits so the cluster_read() does not create inappropriately
2436 * sized buffer cache buffers.
2438 blksize = hammer_blocksize(ap->a_loffset);
2439 if (hammer_blocksize(base_offset) != blksize) {
2440 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
2442 if (last_offset != ap->a_loffset &&
2443 hammer_blocksize(last_offset - 1) != blksize) {
2444 last_offset = hammer_blockdemarc(ap->a_loffset,
2445 last_offset - 1);
2449 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
2450 * from occuring.
2452 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
2455 * If doffsetp is not aligned or the forward run size does
2456 * not cover a whole buffer, disallow the direct I/O.
2458 if ((disk_offset & HAMMER_BUFMASK) ||
2459 (last_offset - ap->a_loffset) < blksize) {
2460 error = EOPNOTSUPP;
2461 } else {
2462 *ap->a_doffsetp = disk_offset;
2463 if (ap->a_runb) {
2464 *ap->a_runb = ap->a_loffset - base_offset;
2465 KKASSERT(*ap->a_runb >= 0);
2467 if (ap->a_runp) {
2468 *ap->a_runp = last_offset - ap->a_loffset;
2469 KKASSERT(*ap->a_runp >= 0);
2471 error = 0;
2473 return(error);
2477 * Write to a regular file. Because this is a strategy call the OS is
2478 * trying to actually get data onto the media.
2480 static
2482 hammer_vop_strategy_write(struct vop_strategy_args *ap)
2484 hammer_record_t record;
2485 hammer_mount_t hmp;
2486 hammer_inode_t ip;
2487 struct bio *bio;
2488 struct buf *bp;
2489 int blksize;
2490 int bytes;
2491 int error;
2493 bio = ap->a_bio;
2494 bp = bio->bio_buf;
2495 ip = ap->a_vp->v_data;
2496 hmp = ip->hmp;
2498 blksize = hammer_blocksize(bio->bio_offset);
2499 KKASSERT(bp->b_bufsize == blksize);
2501 if (ip->flags & HAMMER_INODE_RO) {
2502 bp->b_error = EROFS;
2503 bp->b_flags |= B_ERROR;
2504 biodone(ap->a_bio);
2505 return(EROFS);
2509 * Interlock with inode destruction (no in-kernel or directory
2510 * topology visibility). If we queue new IO while trying to
2511 * destroy the inode we can deadlock the vtrunc call in
2512 * hammer_inode_unloadable_check().
2514 if (ip->flags & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
2515 bp->b_resid = 0;
2516 biodone(ap->a_bio);
2517 return(0);
2521 * Reserve space and issue a direct-write from the front-end.
2522 * NOTE: The direct_io code will hammer_bread/bcopy smaller
2523 * allocations.
2525 * An in-memory record will be installed to reference the storage
2526 * until the flusher can get to it.
2528 * Since we own the high level bio the front-end will not try to
2529 * do a direct-read until the write completes.
2531 * NOTE: The only time we do not reserve a full-sized buffers
2532 * worth of data is if the file is small. We do not try to
2533 * allocate a fragment (from the small-data zone) at the end of
2534 * an otherwise large file as this can lead to wildly separated
2535 * data.
2537 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
2538 KKASSERT(bio->bio_offset < ip->ino_data.size);
2539 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
2540 bytes = bp->b_bufsize;
2541 else
2542 bytes = ((int)ip->ino_data.size + 15) & ~15;
2544 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
2545 bytes, &error);
2546 if (record) {
2547 hammer_io_direct_write(hmp, &record->leaf, bio);
2548 hammer_rel_mem_record(record);
2549 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
2550 hammer_flush_inode(ip, 0);
2551 } else {
2552 bp->b_bio2.bio_offset = NOOFFSET;
2553 bp->b_error = error;
2554 bp->b_flags |= B_ERROR;
2555 biodone(ap->a_bio);
2557 return(error);
2561 * dounlink - disconnect a directory entry
2563 * XXX whiteout support not really in yet
2565 static int
2566 hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
2567 struct vnode *dvp, struct ucred *cred, int flags)
2569 struct namecache *ncp;
2570 hammer_inode_t dip;
2571 hammer_inode_t ip;
2572 struct hammer_cursor cursor;
2573 int64_t namekey;
2574 int nlen, error;
2577 * Calculate the namekey and setup the key range for the scan. This
2578 * works kinda like a chained hash table where the lower 32 bits
2579 * of the namekey synthesize the chain.
2581 * The key range is inclusive of both key_beg and key_end.
2583 dip = VTOI(dvp);
2584 ncp = nch->ncp;
2586 if (dip->flags & HAMMER_INODE_RO)
2587 return (EROFS);
2589 namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
2590 retry:
2591 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
2592 cursor.key_beg.localization = dip->obj_localization +
2593 HAMMER_LOCALIZE_MISC;
2594 cursor.key_beg.obj_id = dip->obj_id;
2595 cursor.key_beg.key = namekey;
2596 cursor.key_beg.create_tid = 0;
2597 cursor.key_beg.delete_tid = 0;
2598 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2599 cursor.key_beg.obj_type = 0;
2601 cursor.key_end = cursor.key_beg;
2602 cursor.key_end.key |= 0xFFFFFFFFULL;
2603 cursor.asof = dip->obj_asof;
2604 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
2607 * Scan all matching records (the chain), locate the one matching
2608 * the requested path component. info->last_error contains the
2609 * error code on search termination and could be 0, ENOENT, or
2610 * something else.
2612 * The hammer_ip_*() functions merge in-memory records with on-disk
2613 * records for the purposes of the search.
2615 error = hammer_ip_first(&cursor);
2617 while (error == 0) {
2618 error = hammer_ip_resolve_data(&cursor);
2619 if (error)
2620 break;
2621 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2622 KKASSERT(nlen > 0);
2623 if (ncp->nc_nlen == nlen &&
2624 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
2625 break;
2627 error = hammer_ip_next(&cursor);
2631 * If all is ok we have to get the inode so we can adjust nlinks.
2632 * To avoid a deadlock with the flusher we must release the inode
2633 * lock on the directory when acquiring the inode for the entry.
2635 * If the target is a directory, it must be empty.
2637 if (error == 0) {
2638 hammer_unlock(&cursor.ip->lock);
2639 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
2640 dip->hmp->asof,
2641 cursor.data->entry.localization,
2642 0, &error);
2643 hammer_lock_sh(&cursor.ip->lock);
2644 if (error == ENOENT) {
2645 kprintf("obj_id %016llx\n", cursor.data->entry.obj_id);
2646 Debugger("ENOENT unlinking object that should exist");
2650 * If we are trying to remove a directory the directory must
2651 * be empty.
2653 * WARNING: hammer_ip_check_directory_empty() may have to
2654 * terminate the cursor to avoid a deadlock. It is ok to
2655 * call hammer_done_cursor() twice.
2657 if (error == 0 && ip->ino_data.obj_type ==
2658 HAMMER_OBJTYPE_DIRECTORY) {
2659 error = hammer_ip_check_directory_empty(trans, ip);
2663 * Delete the directory entry.
2665 * WARNING: hammer_ip_del_directory() may have to terminate
2666 * the cursor to avoid a deadlock. It is ok to call
2667 * hammer_done_cursor() twice.
2669 if (error == 0) {
2670 error = hammer_ip_del_directory(trans, &cursor,
2671 dip, ip);
2673 hammer_done_cursor(&cursor);
2674 if (error == 0) {
2675 cache_setunresolved(nch);
2676 cache_setvp(nch, NULL);
2677 /* XXX locking */
2678 if (ip->vp)
2679 cache_inval_vp(ip->vp, CINV_DESTROY);
2681 if (ip)
2682 hammer_rel_inode(ip, 0);
2683 } else {
2684 hammer_done_cursor(&cursor);
2686 hammer_inode_waitreclaims(dip->hmp);
2687 if (error == EDEADLK)
2688 goto retry;
2690 return (error);
2693 /************************************************************************
2694 * FIFO AND SPECFS OPS *
2695 ************************************************************************
2699 static int
2700 hammer_vop_fifoclose (struct vop_close_args *ap)
2702 /* XXX update itimes */
2703 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
2706 static int
2707 hammer_vop_fiforead (struct vop_read_args *ap)
2709 int error;
2711 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2712 /* XXX update access time */
2713 return (error);
2716 static int
2717 hammer_vop_fifowrite (struct vop_write_args *ap)
2719 int error;
2721 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2722 /* XXX update access time */
2723 return (error);
2726 static int
2727 hammer_vop_specclose (struct vop_close_args *ap)
2729 /* XXX update itimes */
2730 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2733 static int
2734 hammer_vop_specread (struct vop_read_args *ap)
2736 /* XXX update access time */
2737 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2740 static int
2741 hammer_vop_specwrite (struct vop_write_args *ap)
2743 /* XXX update last change time */
2744 return (VOCALL(&spec_vnode_vops, &ap->a_head));