HAMMER 40E/Many: Inode/link-count sequencer cleanup pass.
[dragonfly.git] / sys / vfs / hammer / hammer_vnops.c
CommitLineData
427e5fc6 1/*
b84de5af 2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
427e5fc6
MD
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:
10 *
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.
20 *
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.
33 *
4e17f465 34 * $DragonFly: src/sys/vfs/hammer/hammer_vnops.c,v 1.45 2008/05/03 05:28:55 dillon Exp $
427e5fc6
MD
35 */
36
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>
b3deaf57 46#include <sys/dirent.h>
c0ade690 47#include <vm/vm_extern.h>
7a04d74f 48#include <vfs/fifofs/fifo.h>
427e5fc6
MD
49#include "hammer.h"
50
51/*
52 * USERFS VNOPS
53 */
54/*static int hammer_vop_vnoperate(struct vop_generic_args *);*/
66325755
MD
55static int hammer_vop_fsync(struct vop_fsync_args *);
56static int hammer_vop_read(struct vop_read_args *);
57static int hammer_vop_write(struct vop_write_args *);
58static int hammer_vop_access(struct vop_access_args *);
59static int hammer_vop_advlock(struct vop_advlock_args *);
60static int hammer_vop_close(struct vop_close_args *);
61static int hammer_vop_ncreate(struct vop_ncreate_args *);
62static int hammer_vop_getattr(struct vop_getattr_args *);
63static int hammer_vop_nresolve(struct vop_nresolve_args *);
64static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
65static int hammer_vop_nlink(struct vop_nlink_args *);
66static int hammer_vop_nmkdir(struct vop_nmkdir_args *);
67static int hammer_vop_nmknod(struct vop_nmknod_args *);
68static int hammer_vop_open(struct vop_open_args *);
69static int hammer_vop_pathconf(struct vop_pathconf_args *);
70static int hammer_vop_print(struct vop_print_args *);
71static int hammer_vop_readdir(struct vop_readdir_args *);
72static int hammer_vop_readlink(struct vop_readlink_args *);
73static int hammer_vop_nremove(struct vop_nremove_args *);
74static int hammer_vop_nrename(struct vop_nrename_args *);
75static int hammer_vop_nrmdir(struct vop_nrmdir_args *);
76static int hammer_vop_setattr(struct vop_setattr_args *);
77static int hammer_vop_strategy(struct vop_strategy_args *);
78static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
79static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
7dc57964 80static int hammer_vop_ioctl(struct vop_ioctl_args *);
513ca7d7 81static int hammer_vop_mountctl(struct vop_mountctl_args *);
427e5fc6 82
7a04d74f
MD
83static int hammer_vop_fifoclose (struct vop_close_args *);
84static int hammer_vop_fiforead (struct vop_read_args *);
85static int hammer_vop_fifowrite (struct vop_write_args *);
86
87static int hammer_vop_specclose (struct vop_close_args *);
88static int hammer_vop_specread (struct vop_read_args *);
89static int hammer_vop_specwrite (struct vop_write_args *);
90
427e5fc6
MD
91struct vop_ops hammer_vnode_vops = {
92 .vop_default = vop_defaultop,
93 .vop_fsync = hammer_vop_fsync,
c0ade690
MD
94 .vop_getpages = vop_stdgetpages,
95 .vop_putpages = vop_stdputpages,
427e5fc6
MD
96 .vop_read = hammer_vop_read,
97 .vop_write = hammer_vop_write,
98 .vop_access = hammer_vop_access,
99 .vop_advlock = hammer_vop_advlock,
100 .vop_close = hammer_vop_close,
101 .vop_ncreate = hammer_vop_ncreate,
102 .vop_getattr = hammer_vop_getattr,
103 .vop_inactive = hammer_vop_inactive,
104 .vop_reclaim = hammer_vop_reclaim,
105 .vop_nresolve = hammer_vop_nresolve,
106 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
107 .vop_nlink = hammer_vop_nlink,
108 .vop_nmkdir = hammer_vop_nmkdir,
109 .vop_nmknod = hammer_vop_nmknod,
110 .vop_open = hammer_vop_open,
111 .vop_pathconf = hammer_vop_pathconf,
112 .vop_print = hammer_vop_print,
113 .vop_readdir = hammer_vop_readdir,
114 .vop_readlink = hammer_vop_readlink,
115 .vop_nremove = hammer_vop_nremove,
116 .vop_nrename = hammer_vop_nrename,
117 .vop_nrmdir = hammer_vop_nrmdir,
118 .vop_setattr = hammer_vop_setattr,
119 .vop_strategy = hammer_vop_strategy,
120 .vop_nsymlink = hammer_vop_nsymlink,
7dc57964 121 .vop_nwhiteout = hammer_vop_nwhiteout,
513ca7d7
MD
122 .vop_ioctl = hammer_vop_ioctl,
123 .vop_mountctl = hammer_vop_mountctl
427e5fc6
MD
124};
125
7a04d74f
MD
126struct vop_ops hammer_spec_vops = {
127 .vop_default = spec_vnoperate,
128 .vop_fsync = hammer_vop_fsync,
129 .vop_read = hammer_vop_specread,
130 .vop_write = hammer_vop_specwrite,
131 .vop_access = hammer_vop_access,
132 .vop_close = hammer_vop_specclose,
133 .vop_getattr = hammer_vop_getattr,
134 .vop_inactive = hammer_vop_inactive,
135 .vop_reclaim = hammer_vop_reclaim,
136 .vop_setattr = hammer_vop_setattr
137};
138
139struct vop_ops hammer_fifo_vops = {
140 .vop_default = fifo_vnoperate,
141 .vop_fsync = hammer_vop_fsync,
142 .vop_read = hammer_vop_fiforead,
143 .vop_write = hammer_vop_fifowrite,
144 .vop_access = hammer_vop_access,
145 .vop_close = hammer_vop_fifoclose,
146 .vop_getattr = hammer_vop_getattr,
147 .vop_inactive = hammer_vop_inactive,
148 .vop_reclaim = hammer_vop_reclaim,
149 .vop_setattr = hammer_vop_setattr
150};
151
b84de5af
MD
152static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
153 struct vnode *dvp, struct ucred *cred, int flags);
8cd0a023
MD
154static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
155static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
156
427e5fc6
MD
157#if 0
158static
159int
160hammer_vop_vnoperate(struct vop_generic_args *)
161{
162 return (VOCALL(&hammer_vnode_vops, ap));
163}
164#endif
165
66325755
MD
166/*
167 * hammer_vop_fsync { vp, waitfor }
168 */
427e5fc6
MD
169static
170int
66325755 171hammer_vop_fsync(struct vop_fsync_args *ap)
427e5fc6 172{
b84de5af 173 hammer_inode_t ip = VTOI(ap->a_vp);
c0ade690 174
f90dde4c 175 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
b84de5af
MD
176 if (ap->a_waitfor == MNT_WAIT)
177 hammer_wait_inode(ip);
059819e3 178 return (ip->error);
427e5fc6
MD
179}
180
66325755
MD
181/*
182 * hammer_vop_read { vp, uio, ioflag, cred }
183 */
427e5fc6
MD
184static
185int
66325755 186hammer_vop_read(struct vop_read_args *ap)
427e5fc6 187{
66325755 188 struct hammer_transaction trans;
c0ade690 189 hammer_inode_t ip;
66325755
MD
190 off_t offset;
191 struct buf *bp;
192 struct uio *uio;
193 int error;
194 int n;
8cd0a023 195 int seqcount;
66325755
MD
196
197 if (ap->a_vp->v_type != VREG)
198 return (EINVAL);
199 ip = VTOI(ap->a_vp);
200 error = 0;
8cd0a023 201 seqcount = ap->a_ioflag >> 16;
66325755 202
8cd0a023 203 hammer_start_transaction(&trans, ip->hmp);
66325755
MD
204
205 /*
206 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
207 */
208 uio = ap->a_uio;
209 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_rec.ino_size) {
210 offset = uio->uio_offset & HAMMER_BUFMASK;
c0ade690 211#if 0
8cd0a023
MD
212 error = cluster_read(ap->a_vp, ip->ino_rec.ino_size,
213 uio->uio_offset - offset, HAMMER_BUFSIZE,
214 MAXBSIZE, seqcount, &bp);
c0ade690
MD
215#endif
216 error = bread(ap->a_vp, uio->uio_offset - offset,
217 HAMMER_BUFSIZE, &bp);
66325755
MD
218 if (error) {
219 brelse(bp);
220 break;
221 }
c0ade690 222 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
66325755
MD
223 n = HAMMER_BUFSIZE - offset;
224 if (n > uio->uio_resid)
225 n = uio->uio_resid;
226 if (n > ip->ino_rec.ino_size - uio->uio_offset)
227 n = (int)(ip->ino_rec.ino_size - uio->uio_offset);
228 error = uiomove((char *)bp->b_data + offset, n, uio);
229 if (error) {
8cd0a023 230 bqrelse(bp);
66325755
MD
231 break;
232 }
66325755
MD
233 bqrelse(bp);
234 }
b84de5af
MD
235 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
236 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
237 ip->ino_rec.ino_atime = trans.time;
238 hammer_modify_inode(&trans, ip, HAMMER_INODE_ITIMES);
239 }
240 hammer_done_transaction(&trans);
66325755 241 return (error);
427e5fc6
MD
242}
243
66325755
MD
244/*
245 * hammer_vop_write { vp, uio, ioflag, cred }
246 */
427e5fc6
MD
247static
248int
66325755 249hammer_vop_write(struct vop_write_args *ap)
427e5fc6 250{
66325755
MD
251 struct hammer_transaction trans;
252 struct hammer_inode *ip;
253 struct uio *uio;
254 off_t offset;
255 struct buf *bp;
256 int error;
257 int n;
c0ade690 258 int flags;
059819e3 259 int count;
66325755
MD
260
261 if (ap->a_vp->v_type != VREG)
262 return (EINVAL);
263 ip = VTOI(ap->a_vp);
264 error = 0;
265
d113fda1
MD
266 if (ip->flags & HAMMER_INODE_RO)
267 return (EROFS);
268
66325755
MD
269 /*
270 * Create a transaction to cover the operations we perform.
271 */
8cd0a023 272 hammer_start_transaction(&trans, ip->hmp);
66325755
MD
273 uio = ap->a_uio;
274
275 /*
276 * Check append mode
277 */
278 if (ap->a_ioflag & IO_APPEND)
279 uio->uio_offset = ip->ino_rec.ino_size;
280
281 /*
282 * Check for illegal write offsets. Valid range is 0...2^63-1
283 */
9c448776 284 if (uio->uio_offset < 0 || uio->uio_offset + uio->uio_resid <= 0) {
b84de5af 285 hammer_done_transaction(&trans);
66325755 286 return (EFBIG);
9c448776 287 }
66325755
MD
288
289 /*
290 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
291 */
059819e3 292 count = 0;
66325755 293 while (uio->uio_resid > 0) {
d5ef456e
MD
294 int fixsize = 0;
295
059819e3
MD
296 /*
297 * Do not allow huge writes to deadlock the buffer cache
298 */
299 if ((++count & 15) == 0) {
300 vn_unlock(ap->a_vp);
301 if ((ap->a_ioflag & IO_NOBWILL) == 0)
302 bwillwrite();
303 vn_lock(ap->a_vp, LK_EXCLUSIVE|LK_RETRY);
304 }
305
66325755 306 offset = uio->uio_offset & HAMMER_BUFMASK;
d5ef456e
MD
307 n = HAMMER_BUFSIZE - offset;
308 if (n > uio->uio_resid)
309 n = uio->uio_resid;
310 if (uio->uio_offset + n > ip->ino_rec.ino_size) {
311 vnode_pager_setsize(ap->a_vp, uio->uio_offset + n);
312 fixsize = 1;
313 }
314
c0ade690
MD
315 if (uio->uio_segflg == UIO_NOCOPY) {
316 /*
317 * Issuing a write with the same data backing the
318 * buffer. Instantiate the buffer to collect the
319 * backing vm pages, then read-in any missing bits.
320 *
321 * This case is used by vop_stdputpages().
322 */
d5ef456e
MD
323 bp = getblk(ap->a_vp, uio->uio_offset - offset,
324 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
c0ade690
MD
325 if ((bp->b_flags & B_CACHE) == 0) {
326 bqrelse(bp);
327 error = bread(ap->a_vp,
328 uio->uio_offset - offset,
329 HAMMER_BUFSIZE, &bp);
c0ade690
MD
330 }
331 } else if (offset == 0 && uio->uio_resid >= HAMMER_BUFSIZE) {
332 /*
a5fddc16
MD
333 * Even though we are entirely overwriting the buffer
334 * we may still have to zero it out to avoid a
335 * mmap/write visibility issue.
c0ade690 336 */
d5ef456e
MD
337 bp = getblk(ap->a_vp, uio->uio_offset - offset,
338 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
a5fddc16
MD
339 if ((bp->b_flags & B_CACHE) == 0)
340 vfs_bio_clrbuf(bp);
341 } else if (uio->uio_offset - offset >= ip->ino_rec.ino_size) {
c0ade690 342 /*
a5fddc16
MD
343 * If the base offset of the buffer is beyond the
344 * file EOF, we don't have to issue a read.
c0ade690 345 */
d5ef456e
MD
346 bp = getblk(ap->a_vp, uio->uio_offset - offset,
347 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
66325755
MD
348 vfs_bio_clrbuf(bp);
349 } else {
c0ade690
MD
350 /*
351 * Partial overwrite, read in any missing bits then
352 * replace the portion being written.
353 */
66325755
MD
354 error = bread(ap->a_vp, uio->uio_offset - offset,
355 HAMMER_BUFSIZE, &bp);
d5ef456e
MD
356 if (error == 0)
357 bheavy(bp);
66325755 358 }
d5ef456e
MD
359 if (error == 0)
360 error = uiomove((char *)bp->b_data + offset, n, uio);
361
362 /*
363 * If we screwed up we have to undo any VM size changes we
364 * made.
365 */
66325755
MD
366 if (error) {
367 brelse(bp);
d5ef456e
MD
368 if (fixsize) {
369 vtruncbuf(ap->a_vp, ip->ino_rec.ino_size,
370 HAMMER_BUFSIZE);
371 }
66325755
MD
372 break;
373 }
c0ade690 374 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
66325755
MD
375 if (ip->ino_rec.ino_size < uio->uio_offset) {
376 ip->ino_rec.ino_size = uio->uio_offset;
d113fda1 377 flags = HAMMER_INODE_RDIRTY;
c0ade690
MD
378 vnode_pager_setsize(ap->a_vp, ip->ino_rec.ino_size);
379 } else {
d113fda1 380 flags = 0;
66325755 381 }
b84de5af 382 ip->ino_rec.ino_mtime = trans.time;
f3b0f382 383 flags |= HAMMER_INODE_ITIMES | HAMMER_INODE_BUFS;
c0ade690 384 hammer_modify_inode(&trans, ip, flags);
32c90105 385
66325755
MD
386 if (ap->a_ioflag & IO_SYNC) {
387 bwrite(bp);
388 } else if (ap->a_ioflag & IO_DIRECT) {
66325755 389 bawrite(bp);
059819e3
MD
390#if 0
391 } else if ((ap->a_ioflag >> 16) == IO_SEQMAX &&
34d829f7 392 (uio->uio_offset & HAMMER_BUFMASK) == 0) {
059819e3
MD
393 /*
394 * XXX HAMMER can only fsync the whole inode,
395 * doing it on every buffer would be a bad idea.
396 */
34d829f7
MD
397 /*
398 * If seqcount indicates sequential operation and
399 * we just finished filling a buffer, push it out
400 * now to prevent the buffer cache from becoming
401 * too full, which would trigger non-optimal
402 * flushes.
403 */
059819e3
MD
404 bdwrite(bp);
405#endif
66325755 406 } else {
66325755
MD
407 bdwrite(bp);
408 }
409 }
b84de5af 410 hammer_done_transaction(&trans);
66325755 411 return (error);
427e5fc6
MD
412}
413
66325755
MD
414/*
415 * hammer_vop_access { vp, mode, cred }
416 */
427e5fc6
MD
417static
418int
66325755 419hammer_vop_access(struct vop_access_args *ap)
427e5fc6 420{
66325755
MD
421 struct hammer_inode *ip = VTOI(ap->a_vp);
422 uid_t uid;
423 gid_t gid;
424 int error;
425
426 uid = hammer_to_unix_xid(&ip->ino_data.uid);
427 gid = hammer_to_unix_xid(&ip->ino_data.gid);
428
429 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
430 ip->ino_data.uflags);
431 return (error);
427e5fc6
MD
432}
433
66325755
MD
434/*
435 * hammer_vop_advlock { vp, id, op, fl, flags }
436 */
427e5fc6
MD
437static
438int
66325755 439hammer_vop_advlock(struct vop_advlock_args *ap)
427e5fc6 440{
66325755
MD
441 struct hammer_inode *ip = VTOI(ap->a_vp);
442
443 return (lf_advlock(ap, &ip->advlock, ip->ino_rec.ino_size));
427e5fc6
MD
444}
445
66325755
MD
446/*
447 * hammer_vop_close { vp, fflag }
448 */
427e5fc6
MD
449static
450int
66325755 451hammer_vop_close(struct vop_close_args *ap)
427e5fc6 452{
a89aec1b 453 return (vop_stdclose(ap));
427e5fc6
MD
454}
455
66325755
MD
456/*
457 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
458 *
459 * The operating system has already ensured that the directory entry
460 * does not exist and done all appropriate namespace locking.
461 */
427e5fc6
MD
462static
463int
66325755 464hammer_vop_ncreate(struct vop_ncreate_args *ap)
427e5fc6 465{
66325755
MD
466 struct hammer_transaction trans;
467 struct hammer_inode *dip;
468 struct hammer_inode *nip;
469 struct nchandle *nch;
470 int error;
471
472 nch = ap->a_nch;
473 dip = VTOI(ap->a_dvp);
474
d113fda1
MD
475 if (dip->flags & HAMMER_INODE_RO)
476 return (EROFS);
477
66325755
MD
478 /*
479 * Create a transaction to cover the operations we perform.
480 */
8cd0a023 481 hammer_start_transaction(&trans, dip->hmp);
66325755
MD
482
483 /*
484 * Create a new filesystem object of the requested type. The
b84de5af
MD
485 * returned inode will be referenced and shared-locked to prevent
486 * it from being moved to the flusher.
66325755 487 */
8cd0a023
MD
488
489 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
66325755 490 if (error) {
9480ff55 491 kprintf("hammer_create_inode error %d\n", error);
b84de5af 492 hammer_done_transaction(&trans);
66325755
MD
493 *ap->a_vpp = NULL;
494 return (error);
495 }
66325755
MD
496
497 /*
498 * Add the new filesystem object to the directory. This will also
499 * bump the inode's link count.
500 */
a89aec1b 501 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
0b075555
MD
502 if (error)
503 kprintf("hammer_ip_add_directory error %d\n", error);
66325755
MD
504
505 /*
506 * Finish up.
507 */
508 if (error) {
a89aec1b 509 hammer_rel_inode(nip, 0);
b84de5af 510 hammer_done_transaction(&trans);
66325755
MD
511 *ap->a_vpp = NULL;
512 } else {
66325755 513 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
b84de5af 514 hammer_done_transaction(&trans);
a89aec1b
MD
515 hammer_rel_inode(nip, 0);
516 if (error == 0) {
517 cache_setunresolved(ap->a_nch);
518 cache_setvp(ap->a_nch, *ap->a_vpp);
519 }
66325755
MD
520 }
521 return (error);
427e5fc6
MD
522}
523
66325755
MD
524/*
525 * hammer_vop_getattr { vp, vap }
526 */
427e5fc6
MD
527static
528int
66325755 529hammer_vop_getattr(struct vop_getattr_args *ap)
427e5fc6 530{
66325755
MD
531 struct hammer_inode *ip = VTOI(ap->a_vp);
532 struct vattr *vap = ap->a_vap;
533
534#if 0
535 if (cache_check_fsmid_vp(ap->a_vp, &ip->fsmid) &&
536 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0 &&
7f7c1f84 537 ip->obj_asof == XXX
66325755
MD
538 ) {
539 /* LAZYMOD XXX */
540 }
541 hammer_itimes(ap->a_vp);
542#endif
543
544 vap->va_fsid = ip->hmp->fsid_udev;
545 vap->va_fileid = ip->ino_rec.base.base.obj_id;
546 vap->va_mode = ip->ino_data.mode;
547 vap->va_nlink = ip->ino_rec.ino_nlinks;
548 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
549 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
550 vap->va_rmajor = 0;
551 vap->va_rminor = 0;
552 vap->va_size = ip->ino_rec.ino_size;
553 hammer_to_timespec(ip->ino_rec.ino_atime, &vap->va_atime);
554 hammer_to_timespec(ip->ino_rec.ino_mtime, &vap->va_mtime);
555 hammer_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
556 vap->va_flags = ip->ino_data.uflags;
557 vap->va_gen = 1; /* hammer inums are unique for all time */
bf686dbe
MD
558 vap->va_blocksize = HAMMER_BUFSIZE;
559 vap->va_bytes = (ip->ino_rec.ino_size + 63) & ~63;
66325755
MD
560 vap->va_type = hammer_get_vnode_type(ip->ino_rec.base.base.obj_type);
561 vap->va_filerev = 0; /* XXX */
562 /* mtime uniquely identifies any adjustments made to the file */
563 vap->va_fsmid = ip->ino_rec.ino_mtime;
564 vap->va_uid_uuid = ip->ino_data.uid;
565 vap->va_gid_uuid = ip->ino_data.gid;
566 vap->va_fsid_uuid = ip->hmp->fsid;
567 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
568 VA_FSID_UUID_VALID;
7a04d74f
MD
569
570 switch (ip->ino_rec.base.base.obj_type) {
571 case HAMMER_OBJTYPE_CDEV:
572 case HAMMER_OBJTYPE_BDEV:
573 vap->va_rmajor = ip->ino_data.rmajor;
574 vap->va_rminor = ip->ino_data.rminor;
575 break;
576 default:
577 break;
578 }
579
66325755 580 return(0);
427e5fc6
MD
581}
582
66325755
MD
583/*
584 * hammer_vop_nresolve { nch, dvp, cred }
585 *
586 * Locate the requested directory entry.
587 */
427e5fc6
MD
588static
589int
66325755 590hammer_vop_nresolve(struct vop_nresolve_args *ap)
427e5fc6 591{
36f82b23 592 struct hammer_transaction trans;
66325755 593 struct namecache *ncp;
7f7c1f84
MD
594 hammer_inode_t dip;
595 hammer_inode_t ip;
596 hammer_tid_t asof;
8cd0a023
MD
597 struct hammer_cursor cursor;
598 union hammer_record_ondisk *rec;
66325755
MD
599 struct vnode *vp;
600 int64_t namekey;
601 int error;
7f7c1f84
MD
602 int i;
603 int nlen;
d113fda1 604 int flags;
6a37e7e4 605 u_int64_t obj_id;
7f7c1f84
MD
606
607 /*
608 * Misc initialization, plus handle as-of name extensions. Look for
609 * the '@@' extension. Note that as-of files and directories cannot
610 * be modified.
7f7c1f84
MD
611 */
612 dip = VTOI(ap->a_dvp);
613 ncp = ap->a_nch->ncp;
614 asof = dip->obj_asof;
615 nlen = ncp->nc_nlen;
d113fda1 616 flags = dip->flags;
7f7c1f84 617
36f82b23
MD
618 hammer_simple_transaction(&trans, dip->hmp);
619
7f7c1f84
MD
620 for (i = 0; i < nlen; ++i) {
621 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
d113fda1 622 asof = hammer_str_to_tid(ncp->nc_name + i + 2);
d113fda1 623 flags |= HAMMER_INODE_RO;
7f7c1f84
MD
624 break;
625 }
626 }
627 nlen = i;
66325755 628
d113fda1
MD
629 /*
630 * If there is no path component the time extension is relative to
631 * dip.
632 */
633 if (nlen == 0) {
36f82b23 634 ip = hammer_get_inode(&trans, &dip->cache[1], dip->obj_id,
61aeeb33 635 asof, flags, &error);
d113fda1
MD
636 if (error == 0) {
637 error = hammer_get_vnode(ip, LK_EXCLUSIVE, &vp);
638 hammer_rel_inode(ip, 0);
639 } else {
640 vp = NULL;
641 }
642 if (error == 0) {
643 vn_unlock(vp);
644 cache_setvp(ap->a_nch, vp);
645 vrele(vp);
646 }
36f82b23 647 goto done;
d113fda1
MD
648 }
649
8cd0a023
MD
650 /*
651 * Calculate the namekey and setup the key range for the scan. This
652 * works kinda like a chained hash table where the lower 32 bits
653 * of the namekey synthesize the chain.
654 *
655 * The key range is inclusive of both key_beg and key_end.
656 */
7f7c1f84 657 namekey = hammer_directory_namekey(ncp->nc_name, nlen);
66325755 658
4e17f465 659 error = hammer_init_cursor(&trans, &cursor, &dip->cache[0], dip);
8cd0a023
MD
660 cursor.key_beg.obj_id = dip->obj_id;
661 cursor.key_beg.key = namekey;
d5530d22 662 cursor.key_beg.create_tid = 0;
8cd0a023
MD
663 cursor.key_beg.delete_tid = 0;
664 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
665 cursor.key_beg.obj_type = 0;
66325755 666
8cd0a023
MD
667 cursor.key_end = cursor.key_beg;
668 cursor.key_end.key |= 0xFFFFFFFFULL;
d5530d22
MD
669 cursor.asof = asof;
670 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
66325755
MD
671
672 /*
8cd0a023 673 * Scan all matching records (the chain), locate the one matching
a89aec1b 674 * the requested path component.
8cd0a023
MD
675 *
676 * The hammer_ip_*() functions merge in-memory records with on-disk
677 * records for the purposes of the search.
66325755 678 */
6a37e7e4
MD
679 obj_id = 0;
680
4e17f465
MD
681 if (error == 0) {
682 rec = NULL;
683 error = hammer_ip_first(&cursor);
684 while (error == 0) {
685 error = hammer_ip_resolve_data(&cursor);
686 if (error)
687 break;
688 rec = cursor.record;
689 if (nlen == rec->entry.base.data_len &&
690 bcmp(ncp->nc_name, cursor.data, nlen) == 0) {
691 obj_id = rec->entry.obj_id;
692 break;
693 }
694 error = hammer_ip_next(&cursor);
66325755
MD
695 }
696 }
6a37e7e4 697 hammer_done_cursor(&cursor);
66325755 698 if (error == 0) {
36f82b23 699 ip = hammer_get_inode(&trans, &dip->cache[1],
6a37e7e4 700 obj_id, asof, flags, &error);
7f7c1f84
MD
701 if (error == 0) {
702 error = hammer_get_vnode(ip, LK_EXCLUSIVE, &vp);
703 hammer_rel_inode(ip, 0);
704 } else {
4e17f465
MD
705 kprintf("nresolve: lookup %s failed dip %p (%016llx) on"
706 " inode %016llx error %d\n",
707 ncp->nc_name,
708 dip, dip->obj_id, obj_id, error);
709 Debugger("x");
7f7c1f84
MD
710 vp = NULL;
711 }
66325755
MD
712 if (error == 0) {
713 vn_unlock(vp);
714 cache_setvp(ap->a_nch, vp);
715 vrele(vp);
716 }
717 } else if (error == ENOENT) {
718 cache_setvp(ap->a_nch, NULL);
719 }
36f82b23 720done:
b84de5af 721 hammer_done_transaction(&trans);
66325755 722 return (error);
427e5fc6
MD
723}
724
66325755
MD
725/*
726 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
727 *
728 * Locate the parent directory of a directory vnode.
729 *
730 * dvp is referenced but not locked. *vpp must be returned referenced and
731 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
732 * at the root, instead it could indicate that the directory we were in was
733 * removed.
42c7d26b
MD
734 *
735 * NOTE: as-of sequences are not linked into the directory structure. If
736 * we are at the root with a different asof then the mount point, reload
737 * the same directory with the mount point's asof. I'm not sure what this
738 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
739 * get confused, but it hasn't been tested.
66325755 740 */
427e5fc6
MD
741static
742int
66325755 743hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
427e5fc6 744{
36f82b23 745 struct hammer_transaction trans;
66325755 746 struct hammer_inode *dip;
d113fda1 747 struct hammer_inode *ip;
42c7d26b
MD
748 int64_t parent_obj_id;
749 hammer_tid_t asof;
d113fda1 750 int error;
66325755
MD
751
752 dip = VTOI(ap->a_dvp);
42c7d26b
MD
753 asof = dip->obj_asof;
754 parent_obj_id = dip->ino_data.parent_obj_id;
755
756 if (parent_obj_id == 0) {
757 if (dip->obj_id == HAMMER_OBJID_ROOT &&
758 asof != dip->hmp->asof) {
759 parent_obj_id = dip->obj_id;
760 asof = dip->hmp->asof;
761 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
762 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
763 dip->obj_asof);
764 } else {
765 *ap->a_vpp = NULL;
766 return ENOENT;
767 }
66325755 768 }
d113fda1 769
36f82b23
MD
770 hammer_simple_transaction(&trans, dip->hmp);
771
772 ip = hammer_get_inode(&trans, &dip->cache[1], parent_obj_id,
42c7d26b 773 asof, dip->flags, &error);
36f82b23
MD
774 if (ip) {
775 error = hammer_get_vnode(ip, LK_EXCLUSIVE, ap->a_vpp);
776 hammer_rel_inode(ip, 0);
777 } else {
d113fda1 778 *ap->a_vpp = NULL;
d113fda1 779 }
b84de5af 780 hammer_done_transaction(&trans);
d113fda1 781 return (error);
427e5fc6
MD
782}
783
66325755
MD
784/*
785 * hammer_vop_nlink { nch, dvp, vp, cred }
786 */
427e5fc6
MD
787static
788int
66325755 789hammer_vop_nlink(struct vop_nlink_args *ap)
427e5fc6 790{
66325755
MD
791 struct hammer_transaction trans;
792 struct hammer_inode *dip;
793 struct hammer_inode *ip;
794 struct nchandle *nch;
795 int error;
796
797 nch = ap->a_nch;
798 dip = VTOI(ap->a_dvp);
799 ip = VTOI(ap->a_vp);
800
d113fda1
MD
801 if (dip->flags & HAMMER_INODE_RO)
802 return (EROFS);
803 if (ip->flags & HAMMER_INODE_RO)
804 return (EROFS);
805
66325755
MD
806 /*
807 * Create a transaction to cover the operations we perform.
808 */
8cd0a023 809 hammer_start_transaction(&trans, dip->hmp);
66325755
MD
810
811 /*
812 * Add the filesystem object to the directory. Note that neither
813 * dip nor ip are referenced or locked, but their vnodes are
814 * referenced. This function will bump the inode's link count.
815 */
a89aec1b 816 error = hammer_ip_add_directory(&trans, dip, nch->ncp, ip);
66325755
MD
817
818 /*
819 * Finish up.
820 */
b84de5af 821 if (error == 0) {
6b4f890b
MD
822 cache_setunresolved(nch);
823 cache_setvp(nch, ap->a_vp);
66325755 824 }
b84de5af 825 hammer_done_transaction(&trans);
66325755 826 return (error);
427e5fc6
MD
827}
828
66325755
MD
829/*
830 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
831 *
832 * The operating system has already ensured that the directory entry
833 * does not exist and done all appropriate namespace locking.
834 */
427e5fc6
MD
835static
836int
66325755 837hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
427e5fc6 838{
66325755
MD
839 struct hammer_transaction trans;
840 struct hammer_inode *dip;
841 struct hammer_inode *nip;
842 struct nchandle *nch;
843 int error;
844
845 nch = ap->a_nch;
846 dip = VTOI(ap->a_dvp);
847
d113fda1
MD
848 if (dip->flags & HAMMER_INODE_RO)
849 return (EROFS);
850
66325755
MD
851 /*
852 * Create a transaction to cover the operations we perform.
853 */
8cd0a023 854 hammer_start_transaction(&trans, dip->hmp);
66325755
MD
855
856 /*
857 * Create a new filesystem object of the requested type. The
8cd0a023 858 * returned inode will be referenced but not locked.
66325755 859 */
8cd0a023 860 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
66325755 861 if (error) {
b84de5af
MD
862 kprintf("hammer_mkdir error %d\n", error);
863 hammer_done_transaction(&trans);
66325755
MD
864 *ap->a_vpp = NULL;
865 return (error);
866 }
66325755
MD
867 /*
868 * Add the new filesystem object to the directory. This will also
869 * bump the inode's link count.
870 */
a89aec1b 871 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
0b075555
MD
872 if (error)
873 kprintf("hammer_mkdir (add) error %d\n", error);
66325755
MD
874
875 /*
876 * Finish up.
877 */
878 if (error) {
a89aec1b 879 hammer_rel_inode(nip, 0);
66325755
MD
880 *ap->a_vpp = NULL;
881 } else {
66325755 882 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
a89aec1b
MD
883 hammer_rel_inode(nip, 0);
884 if (error == 0) {
885 cache_setunresolved(ap->a_nch);
886 cache_setvp(ap->a_nch, *ap->a_vpp);
887 }
66325755 888 }
b84de5af 889 hammer_done_transaction(&trans);
66325755 890 return (error);
427e5fc6
MD
891}
892
66325755
MD
893/*
894 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
895 *
896 * The operating system has already ensured that the directory entry
897 * does not exist and done all appropriate namespace locking.
898 */
427e5fc6
MD
899static
900int
66325755 901hammer_vop_nmknod(struct vop_nmknod_args *ap)
427e5fc6 902{
66325755
MD
903 struct hammer_transaction trans;
904 struct hammer_inode *dip;
905 struct hammer_inode *nip;
906 struct nchandle *nch;
907 int error;
908
909 nch = ap->a_nch;
910 dip = VTOI(ap->a_dvp);
911
d113fda1
MD
912 if (dip->flags & HAMMER_INODE_RO)
913 return (EROFS);
914
66325755
MD
915 /*
916 * Create a transaction to cover the operations we perform.
917 */
8cd0a023 918 hammer_start_transaction(&trans, dip->hmp);
66325755
MD
919
920 /*
921 * Create a new filesystem object of the requested type. The
8cd0a023 922 * returned inode will be referenced but not locked.
66325755 923 */
8cd0a023 924 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
66325755 925 if (error) {
b84de5af 926 hammer_done_transaction(&trans);
66325755
MD
927 *ap->a_vpp = NULL;
928 return (error);
929 }
66325755
MD
930
931 /*
932 * Add the new filesystem object to the directory. This will also
933 * bump the inode's link count.
934 */
a89aec1b 935 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
66325755
MD
936
937 /*
938 * Finish up.
939 */
940 if (error) {
a89aec1b 941 hammer_rel_inode(nip, 0);
66325755
MD
942 *ap->a_vpp = NULL;
943 } else {
66325755 944 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
a89aec1b
MD
945 hammer_rel_inode(nip, 0);
946 if (error == 0) {
947 cache_setunresolved(ap->a_nch);
948 cache_setvp(ap->a_nch, *ap->a_vpp);
949 }
66325755 950 }
b84de5af 951 hammer_done_transaction(&trans);
66325755 952 return (error);
427e5fc6
MD
953}
954
66325755
MD
955/*
956 * hammer_vop_open { vp, mode, cred, fp }
957 */
427e5fc6
MD
958static
959int
66325755 960hammer_vop_open(struct vop_open_args *ap)
427e5fc6 961{
d113fda1
MD
962 if ((ap->a_mode & FWRITE) && (VTOI(ap->a_vp)->flags & HAMMER_INODE_RO))
963 return (EROFS);
964
a89aec1b 965 return(vop_stdopen(ap));
427e5fc6
MD
966}
967
66325755
MD
968/*
969 * hammer_vop_pathconf { vp, name, retval }
970 */
427e5fc6
MD
971static
972int
66325755 973hammer_vop_pathconf(struct vop_pathconf_args *ap)
427e5fc6
MD
974{
975 return EOPNOTSUPP;
976}
977
66325755
MD
978/*
979 * hammer_vop_print { vp }
980 */
427e5fc6
MD
981static
982int
66325755 983hammer_vop_print(struct vop_print_args *ap)
427e5fc6
MD
984{
985 return EOPNOTSUPP;
986}
987
66325755 988/*
6b4f890b 989 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
66325755 990 */
427e5fc6
MD
991static
992int
66325755 993hammer_vop_readdir(struct vop_readdir_args *ap)
427e5fc6 994{
36f82b23 995 struct hammer_transaction trans;
6b4f890b
MD
996 struct hammer_cursor cursor;
997 struct hammer_inode *ip;
998 struct uio *uio;
999 hammer_record_ondisk_t rec;
1000 hammer_base_elm_t base;
1001 int error;
1002 int cookie_index;
1003 int ncookies;
1004 off_t *cookies;
1005 off_t saveoff;
1006 int r;
1007
1008 ip = VTOI(ap->a_vp);
1009 uio = ap->a_uio;
b3deaf57
MD
1010 saveoff = uio->uio_offset;
1011
1012 if (ap->a_ncookies) {
1013 ncookies = uio->uio_resid / 16 + 1;
1014 if (ncookies > 1024)
1015 ncookies = 1024;
1016 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1017 cookie_index = 0;
1018 } else {
1019 ncookies = -1;
1020 cookies = NULL;
1021 cookie_index = 0;
1022 }
1023
36f82b23
MD
1024 hammer_simple_transaction(&trans, ip->hmp);
1025
b3deaf57
MD
1026 /*
1027 * Handle artificial entries
1028 */
1029 error = 0;
1030 if (saveoff == 0) {
1031 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1032 if (r)
1033 goto done;
1034 if (cookies)
1035 cookies[cookie_index] = saveoff;
1036 ++saveoff;
1037 ++cookie_index;
1038 if (cookie_index == ncookies)
1039 goto done;
1040 }
1041 if (saveoff == 1) {
1042 if (ip->ino_data.parent_obj_id) {
1043 r = vop_write_dirent(&error, uio,
1044 ip->ino_data.parent_obj_id,
1045 DT_DIR, 2, "..");
1046 } else {
1047 r = vop_write_dirent(&error, uio,
1048 ip->obj_id, DT_DIR, 2, "..");
1049 }
1050 if (r)
1051 goto done;
1052 if (cookies)
1053 cookies[cookie_index] = saveoff;
1054 ++saveoff;
1055 ++cookie_index;
1056 if (cookie_index == ncookies)
1057 goto done;
1058 }
6b4f890b
MD
1059
1060 /*
1061 * Key range (begin and end inclusive) to scan. Directory keys
1062 * directly translate to a 64 bit 'seek' position.
1063 */
4e17f465 1064 hammer_init_cursor(&trans, &cursor, &ip->cache[0], ip);
6b4f890b 1065 cursor.key_beg.obj_id = ip->obj_id;
d5530d22 1066 cursor.key_beg.create_tid = 0;
6b4f890b
MD
1067 cursor.key_beg.delete_tid = 0;
1068 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1069 cursor.key_beg.obj_type = 0;
b3deaf57 1070 cursor.key_beg.key = saveoff;
6b4f890b
MD
1071
1072 cursor.key_end = cursor.key_beg;
1073 cursor.key_end.key = HAMMER_MAX_KEY;
d5530d22
MD
1074 cursor.asof = ip->obj_asof;
1075 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
6b4f890b 1076
4e17f465 1077 error = hammer_ip_first(&cursor);
6b4f890b
MD
1078
1079 while (error == 0) {
40043e7f 1080 error = hammer_ip_resolve_record_and_data(&cursor);
6b4f890b
MD
1081 if (error)
1082 break;
1083 rec = cursor.record;
1084 base = &rec->base.base;
1085 saveoff = base->key;
1086
7a04d74f
MD
1087 if (base->obj_id != ip->obj_id)
1088 panic("readdir: bad record at %p", cursor.node);
1089
6b4f890b
MD
1090 r = vop_write_dirent(
1091 &error, uio, rec->entry.obj_id,
6b4f890b 1092 hammer_get_dtype(rec->entry.base.base.obj_type),
7a04d74f 1093 rec->entry.base.data_len,
40043e7f 1094 (void *)cursor.data);
6b4f890b
MD
1095 if (r)
1096 break;
1097 ++saveoff;
1098 if (cookies)
1099 cookies[cookie_index] = base->key;
1100 ++cookie_index;
1101 if (cookie_index == ncookies)
1102 break;
1103 error = hammer_ip_next(&cursor);
1104 }
1105 hammer_done_cursor(&cursor);
1106
b3deaf57 1107done:
b84de5af 1108 hammer_done_transaction(&trans);
36f82b23 1109
6b4f890b
MD
1110 if (ap->a_eofflag)
1111 *ap->a_eofflag = (error == ENOENT);
6b4f890b
MD
1112 uio->uio_offset = saveoff;
1113 if (error && cookie_index == 0) {
b3deaf57
MD
1114 if (error == ENOENT)
1115 error = 0;
6b4f890b
MD
1116 if (cookies) {
1117 kfree(cookies, M_TEMP);
1118 *ap->a_ncookies = 0;
1119 *ap->a_cookies = NULL;
1120 }
1121 } else {
7a04d74f
MD
1122 if (error == ENOENT)
1123 error = 0;
6b4f890b
MD
1124 if (cookies) {
1125 *ap->a_ncookies = cookie_index;
1126 *ap->a_cookies = cookies;
1127 }
1128 }
1129 return(error);
427e5fc6
MD
1130}
1131
66325755
MD
1132/*
1133 * hammer_vop_readlink { vp, uio, cred }
1134 */
427e5fc6
MD
1135static
1136int
66325755 1137hammer_vop_readlink(struct vop_readlink_args *ap)
427e5fc6 1138{
36f82b23 1139 struct hammer_transaction trans;
7a04d74f
MD
1140 struct hammer_cursor cursor;
1141 struct hammer_inode *ip;
1142 int error;
1143
1144 ip = VTOI(ap->a_vp);
36f82b23
MD
1145
1146 hammer_simple_transaction(&trans, ip->hmp);
1147
4e17f465 1148 hammer_init_cursor(&trans, &cursor, &ip->cache[0], ip);
7a04d74f
MD
1149
1150 /*
1151 * Key range (begin and end inclusive) to scan. Directory keys
1152 * directly translate to a 64 bit 'seek' position.
1153 */
1154 cursor.key_beg.obj_id = ip->obj_id;
d5530d22 1155 cursor.key_beg.create_tid = 0;
7a04d74f
MD
1156 cursor.key_beg.delete_tid = 0;
1157 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1158 cursor.key_beg.obj_type = 0;
1159 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
d5530d22
MD
1160 cursor.asof = ip->obj_asof;
1161 cursor.flags |= HAMMER_CURSOR_ASOF;
7a04d74f
MD
1162
1163 error = hammer_ip_lookup(&cursor, ip);
1164 if (error == 0) {
1165 error = hammer_ip_resolve_data(&cursor);
1166 if (error == 0) {
40043e7f 1167 error = uiomove((char *)cursor.data,
47197d71 1168 cursor.record->base.data_len,
7a04d74f
MD
1169 ap->a_uio);
1170 }
1171 }
1172 hammer_done_cursor(&cursor);
b84de5af 1173 hammer_done_transaction(&trans);
7a04d74f 1174 return(error);
427e5fc6
MD
1175}
1176
66325755
MD
1177/*
1178 * hammer_vop_nremove { nch, dvp, cred }
1179 */
427e5fc6
MD
1180static
1181int
66325755 1182hammer_vop_nremove(struct vop_nremove_args *ap)
427e5fc6 1183{
b84de5af
MD
1184 struct hammer_transaction trans;
1185 int error;
1186
1187 hammer_start_transaction(&trans, VTOI(ap->a_dvp)->hmp);
1188 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1189 hammer_done_transaction(&trans);
1190
1191 return (error);
427e5fc6
MD
1192}
1193
66325755
MD
1194/*
1195 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1196 */
427e5fc6
MD
1197static
1198int
66325755 1199hammer_vop_nrename(struct vop_nrename_args *ap)
427e5fc6 1200{
8cd0a023
MD
1201 struct hammer_transaction trans;
1202 struct namecache *fncp;
1203 struct namecache *tncp;
1204 struct hammer_inode *fdip;
1205 struct hammer_inode *tdip;
1206 struct hammer_inode *ip;
1207 struct hammer_cursor cursor;
1208 union hammer_record_ondisk *rec;
1209 int64_t namekey;
1210 int error;
1211
1212 fdip = VTOI(ap->a_fdvp);
1213 tdip = VTOI(ap->a_tdvp);
1214 fncp = ap->a_fnch->ncp;
1215 tncp = ap->a_tnch->ncp;
b3deaf57
MD
1216 ip = VTOI(fncp->nc_vp);
1217 KKASSERT(ip != NULL);
d113fda1
MD
1218
1219 if (fdip->flags & HAMMER_INODE_RO)
1220 return (EROFS);
1221 if (tdip->flags & HAMMER_INODE_RO)
1222 return (EROFS);
1223 if (ip->flags & HAMMER_INODE_RO)
1224 return (EROFS);
1225
8cd0a023
MD
1226 hammer_start_transaction(&trans, fdip->hmp);
1227
1228 /*
b3deaf57
MD
1229 * Remove tncp from the target directory and then link ip as
1230 * tncp. XXX pass trans to dounlink
42c7d26b
MD
1231 *
1232 * Force the inode sync-time to match the transaction so it is
1233 * in-sync with the creation of the target directory entry.
8cd0a023 1234 */
b84de5af 1235 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp, ap->a_cred, 0);
42c7d26b 1236 if (error == 0 || error == ENOENT) {
b3deaf57 1237 error = hammer_ip_add_directory(&trans, tdip, tncp, ip);
42c7d26b
MD
1238 if (error == 0) {
1239 ip->ino_data.parent_obj_id = tdip->obj_id;
b84de5af 1240 hammer_modify_inode(&trans, ip, HAMMER_INODE_DDIRTY);
42c7d26b
MD
1241 }
1242 }
b3deaf57
MD
1243 if (error)
1244 goto failed; /* XXX */
8cd0a023
MD
1245
1246 /*
1247 * Locate the record in the originating directory and remove it.
1248 *
1249 * Calculate the namekey and setup the key range for the scan. This
1250 * works kinda like a chained hash table where the lower 32 bits
1251 * of the namekey synthesize the chain.
1252 *
1253 * The key range is inclusive of both key_beg and key_end.
1254 */
1255 namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
6a37e7e4 1256retry:
4e17f465 1257 hammer_init_cursor(&trans, &cursor, &fdip->cache[0], fdip);
8cd0a023
MD
1258 cursor.key_beg.obj_id = fdip->obj_id;
1259 cursor.key_beg.key = namekey;
d5530d22 1260 cursor.key_beg.create_tid = 0;
8cd0a023
MD
1261 cursor.key_beg.delete_tid = 0;
1262 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1263 cursor.key_beg.obj_type = 0;
1264
1265 cursor.key_end = cursor.key_beg;
1266 cursor.key_end.key |= 0xFFFFFFFFULL;
d5530d22
MD
1267 cursor.asof = fdip->obj_asof;
1268 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
8cd0a023
MD
1269
1270 /*
1271 * Scan all matching records (the chain), locate the one matching
a89aec1b 1272 * the requested path component.
8cd0a023
MD
1273 *
1274 * The hammer_ip_*() functions merge in-memory records with on-disk
1275 * records for the purposes of the search.
1276 */
4e17f465 1277 error = hammer_ip_first(&cursor);
a89aec1b 1278 while (error == 0) {
8cd0a023
MD
1279 if (hammer_ip_resolve_data(&cursor) != 0)
1280 break;
a89aec1b 1281 rec = cursor.record;
8cd0a023 1282 if (fncp->nc_nlen == rec->entry.base.data_len &&
40043e7f 1283 bcmp(fncp->nc_name, cursor.data, fncp->nc_nlen) == 0) {
8cd0a023
MD
1284 break;
1285 }
a89aec1b 1286 error = hammer_ip_next(&cursor);
8cd0a023 1287 }
8cd0a023
MD
1288
1289 /*
1290 * If all is ok we have to get the inode so we can adjust nlinks.
6a37e7e4
MD
1291 *
1292 * WARNING: hammer_ip_del_directory() may have to terminate the
1293 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1294 * twice.
8cd0a023 1295 */
9944ae54 1296 if (error == 0)
6a37e7e4 1297 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
b84de5af
MD
1298
1299 /*
1300 * XXX A deadlock here will break rename's atomicy for the purposes
1301 * of crash recovery.
1302 */
1303 if (error == EDEADLK) {
b84de5af 1304 hammer_done_cursor(&cursor);
b84de5af
MD
1305 goto retry;
1306 }
1307
1308 /*
1309 * Cleanup and tell the kernel that the rename succeeded.
1310 */
c0ade690 1311 hammer_done_cursor(&cursor);
6a37e7e4
MD
1312 if (error == 0)
1313 cache_rename(ap->a_fnch, ap->a_tnch);
b84de5af 1314
b3deaf57 1315failed:
b84de5af 1316 hammer_done_transaction(&trans);
8cd0a023 1317 return (error);
427e5fc6
MD
1318}
1319
66325755
MD
1320/*
1321 * hammer_vop_nrmdir { nch, dvp, cred }
1322 */
427e5fc6
MD
1323static
1324int
66325755 1325hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
427e5fc6 1326{
b84de5af
MD
1327 struct hammer_transaction trans;
1328 int error;
1329
1330 hammer_start_transaction(&trans, VTOI(ap->a_dvp)->hmp);
1331 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1332 hammer_done_transaction(&trans);
1333
1334 return (error);
427e5fc6
MD
1335}
1336
66325755
MD
1337/*
1338 * hammer_vop_setattr { vp, vap, cred }
1339 */
427e5fc6
MD
1340static
1341int
66325755 1342hammer_vop_setattr(struct vop_setattr_args *ap)
427e5fc6 1343{
8cd0a023
MD
1344 struct hammer_transaction trans;
1345 struct vattr *vap;
1346 struct hammer_inode *ip;
1347 int modflags;
1348 int error;
d5ef456e 1349 int truncating;
b84de5af 1350 off_t aligned_size;
8cd0a023
MD
1351 u_int32_t flags;
1352 uuid_t uuid;
1353
1354 vap = ap->a_vap;
1355 ip = ap->a_vp->v_data;
1356 modflags = 0;
1357
1358 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1359 return(EROFS);
d113fda1
MD
1360 if (ip->flags & HAMMER_INODE_RO)
1361 return (EROFS);
8cd0a023
MD
1362
1363 hammer_start_transaction(&trans, ip->hmp);
1364 error = 0;
1365
1366 if (vap->va_flags != VNOVAL) {
1367 flags = ip->ino_data.uflags;
1368 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1369 hammer_to_unix_xid(&ip->ino_data.uid),
1370 ap->a_cred);
1371 if (error == 0) {
1372 if (ip->ino_data.uflags != flags) {
1373 ip->ino_data.uflags = flags;
1374 modflags |= HAMMER_INODE_DDIRTY;
1375 }
1376 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1377 error = 0;
1378 goto done;
1379 }
1380 }
1381 goto done;
1382 }
1383 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1384 error = EPERM;
1385 goto done;
1386 }
1387 if (vap->va_uid != (uid_t)VNOVAL) {
1388 hammer_guid_to_uuid(&uuid, vap->va_uid);
6b4f890b 1389 if (bcmp(&uuid, &ip->ino_data.uid, sizeof(uuid)) != 0) {
8cd0a023
MD
1390 ip->ino_data.uid = uuid;
1391 modflags |= HAMMER_INODE_DDIRTY;
1392 }
1393 }
1394 if (vap->va_gid != (uid_t)VNOVAL) {
6b4f890b
MD
1395 hammer_guid_to_uuid(&uuid, vap->va_gid);
1396 if (bcmp(&uuid, &ip->ino_data.gid, sizeof(uuid)) != 0) {
8cd0a023
MD
1397 ip->ino_data.gid = uuid;
1398 modflags |= HAMMER_INODE_DDIRTY;
1399 }
1400 }
d26d0ae9 1401 while (vap->va_size != VNOVAL && ip->ino_rec.ino_size != vap->va_size) {
8cd0a023
MD
1402 switch(ap->a_vp->v_type) {
1403 case VREG:
d5ef456e
MD
1404 if (vap->va_size == ip->ino_rec.ino_size)
1405 break;
b84de5af
MD
1406 /*
1407 * XXX break atomicy, we can deadlock the backend
1408 * if we do not release the lock. Probably not a
1409 * big deal here.
1410 */
c0ade690
MD
1411 if (vap->va_size < ip->ino_rec.ino_size) {
1412 vtruncbuf(ap->a_vp, vap->va_size,
1413 HAMMER_BUFSIZE);
d5ef456e
MD
1414 truncating = 1;
1415 } else {
c0ade690 1416 vnode_pager_setsize(ap->a_vp, vap->va_size);
d5ef456e 1417 truncating = 0;
c0ade690 1418 }
d5ef456e
MD
1419 ip->ino_rec.ino_size = vap->va_size;
1420 modflags |= HAMMER_INODE_RDIRTY;
76376933 1421 aligned_size = (vap->va_size + HAMMER_BUFMASK) &
b84de5af 1422 ~HAMMER_BUFMASK64;
d5ef456e 1423
b84de5af
MD
1424 /*
1425 * on-media truncation is cached in the inode until
1426 * the inode is synchronized.
1427 */
d5ef456e 1428 if (truncating) {
b84de5af
MD
1429 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1430 ip->flags |= HAMMER_INODE_TRUNCATED;
1431 ip->trunc_off = vap->va_size;
1432 } else if (ip->trunc_off > vap->va_size) {
1433 ip->trunc_off = vap->va_size;
1434 }
d5ef456e 1435 }
b84de5af 1436
d5ef456e
MD
1437 /*
1438 * If truncating we have to clean out a portion of
b84de5af
MD
1439 * the last block on-disk. We do this in the
1440 * front-end buffer cache.
d5ef456e 1441 */
b84de5af 1442 if (truncating && vap->va_size < aligned_size) {
d5ef456e
MD
1443 struct buf *bp;
1444 int offset;
1445
1446 offset = vap->va_size & HAMMER_BUFMASK;
1447 error = bread(ap->a_vp,
1448 aligned_size - HAMMER_BUFSIZE,
1449 HAMMER_BUFSIZE, &bp);
1450 if (error == 0) {
1451 bzero(bp->b_data + offset,
1452 HAMMER_BUFSIZE - offset);
1453 bdwrite(bp);
1454 } else {
1455 brelse(bp);
1456 }
1457 }
76376933 1458 break;
8cd0a023 1459 case VDATABASE:
b84de5af
MD
1460 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1461 ip->flags |= HAMMER_INODE_TRUNCATED;
1462 ip->trunc_off = vap->va_size;
1463 } else if (ip->trunc_off > vap->va_size) {
1464 ip->trunc_off = vap->va_size;
1465 }
c0ade690
MD
1466 ip->ino_rec.ino_size = vap->va_size;
1467 modflags |= HAMMER_INODE_RDIRTY;
8cd0a023
MD
1468 break;
1469 default:
1470 error = EINVAL;
1471 goto done;
1472 }
d26d0ae9 1473 break;
8cd0a023
MD
1474 }
1475 if (vap->va_atime.tv_sec != VNOVAL) {
1476 ip->ino_rec.ino_atime =
1477 hammer_timespec_to_transid(&vap->va_atime);
1478 modflags |= HAMMER_INODE_ITIMES;
1479 }
1480 if (vap->va_mtime.tv_sec != VNOVAL) {
1481 ip->ino_rec.ino_mtime =
1482 hammer_timespec_to_transid(&vap->va_mtime);
1483 modflags |= HAMMER_INODE_ITIMES;
1484 }
1485 if (vap->va_mode != (mode_t)VNOVAL) {
1486 if (ip->ino_data.mode != vap->va_mode) {
1487 ip->ino_data.mode = vap->va_mode;
1488 modflags |= HAMMER_INODE_DDIRTY;
1489 }
1490 }
1491done:
b84de5af 1492 if (error == 0)
c0ade690 1493 hammer_modify_inode(&trans, ip, modflags);
b84de5af 1494 hammer_done_transaction(&trans);
8cd0a023 1495 return (error);
427e5fc6
MD
1496}
1497
66325755
MD
1498/*
1499 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1500 */
427e5fc6
MD
1501static
1502int
66325755 1503hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
427e5fc6 1504{
7a04d74f
MD
1505 struct hammer_transaction trans;
1506 struct hammer_inode *dip;
1507 struct hammer_inode *nip;
1508 struct nchandle *nch;
1509 hammer_record_t record;
1510 int error;
1511 int bytes;
1512
1513 ap->a_vap->va_type = VLNK;
1514
1515 nch = ap->a_nch;
1516 dip = VTOI(ap->a_dvp);
1517
d113fda1
MD
1518 if (dip->flags & HAMMER_INODE_RO)
1519 return (EROFS);
1520
7a04d74f
MD
1521 /*
1522 * Create a transaction to cover the operations we perform.
1523 */
1524 hammer_start_transaction(&trans, dip->hmp);
1525
1526 /*
1527 * Create a new filesystem object of the requested type. The
1528 * returned inode will be referenced but not locked.
1529 */
1530
1531 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
1532 if (error) {
b84de5af 1533 hammer_done_transaction(&trans);
7a04d74f
MD
1534 *ap->a_vpp = NULL;
1535 return (error);
1536 }
1537
7a04d74f
MD
1538 /*
1539 * Add a record representing the symlink. symlink stores the link
1540 * as pure data, not a string, and is no \0 terminated.
1541 */
1542 if (error == 0) {
40043e7f 1543 record = hammer_alloc_mem_record(nip);
7a04d74f
MD
1544 bytes = strlen(ap->a_target);
1545
47197d71
MD
1546 record->rec.base.base.key = HAMMER_FIXKEY_SYMLINK;
1547 record->rec.base.base.rec_type = HAMMER_RECTYPE_FIX;
1548 record->rec.base.data_len = bytes;
1549 record->data = (void *)ap->a_target;
1550 /* will be reallocated by routine below */
7a04d74f 1551 error = hammer_ip_add_record(&trans, record);
42c7d26b
MD
1552
1553 /*
1554 * Set the file size to the length of the link.
1555 */
1556 if (error == 0) {
1557 nip->ino_rec.ino_size = bytes;
1558 hammer_modify_inode(&trans, nip, HAMMER_INODE_RDIRTY);
1559 }
7a04d74f 1560 }
1f07f686
MD
1561 if (error == 0)
1562 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
7a04d74f
MD
1563
1564 /*
1565 * Finish up.
1566 */
1567 if (error) {
1568 hammer_rel_inode(nip, 0);
7a04d74f
MD
1569 *ap->a_vpp = NULL;
1570 } else {
7a04d74f
MD
1571 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
1572 hammer_rel_inode(nip, 0);
1573 if (error == 0) {
1574 cache_setunresolved(ap->a_nch);
1575 cache_setvp(ap->a_nch, *ap->a_vpp);
1576 }
1577 }
b84de5af 1578 hammer_done_transaction(&trans);
7a04d74f 1579 return (error);
427e5fc6
MD
1580}
1581
66325755
MD
1582/*
1583 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1584 */
427e5fc6
MD
1585static
1586int
66325755 1587hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
427e5fc6 1588{
b84de5af
MD
1589 struct hammer_transaction trans;
1590 int error;
1591
1592 hammer_start_transaction(&trans, VTOI(ap->a_dvp)->hmp);
1593 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
1594 ap->a_cred, ap->a_flags);
1595 hammer_done_transaction(&trans);
1596
1597 return (error);
427e5fc6
MD
1598}
1599
7dc57964
MD
1600/*
1601 * hammer_vop_ioctl { vp, command, data, fflag, cred }
1602 */
1603static
1604int
1605hammer_vop_ioctl(struct vop_ioctl_args *ap)
1606{
1607 struct hammer_inode *ip = ap->a_vp->v_data;
1608
1609 return(hammer_ioctl(ip, ap->a_command, ap->a_data,
1610 ap->a_fflag, ap->a_cred));
1611}
1612
513ca7d7
MD
1613static
1614int
1615hammer_vop_mountctl(struct vop_mountctl_args *ap)
1616{
1617 struct mount *mp;
1618 int error;
1619
1620 mp = ap->a_head.a_ops->head.vv_mount;
1621
1622 switch(ap->a_op) {
1623 case MOUNTCTL_SET_EXPORT:
1624 if (ap->a_ctllen != sizeof(struct export_args))
1625 error = EINVAL;
1626 error = hammer_vfs_export(mp, ap->a_op,
1627 (const struct export_args *)ap->a_ctl);
1628 break;
1629 default:
1630 error = journal_mountctl(ap);
1631 break;
1632 }
1633 return(error);
1634}
1635
66325755
MD
1636/*
1637 * hammer_vop_strategy { vp, bio }
8cd0a023
MD
1638 *
1639 * Strategy call, used for regular file read & write only. Note that the
1640 * bp may represent a cluster.
1641 *
1642 * To simplify operation and allow better optimizations in the future,
1643 * this code does not make any assumptions with regards to buffer alignment
1644 * or size.
66325755 1645 */
427e5fc6
MD
1646static
1647int
66325755 1648hammer_vop_strategy(struct vop_strategy_args *ap)
427e5fc6 1649{
8cd0a023
MD
1650 struct buf *bp;
1651 int error;
1652
1653 bp = ap->a_bio->bio_buf;
1654
1655 switch(bp->b_cmd) {
1656 case BUF_CMD_READ:
1657 error = hammer_vop_strategy_read(ap);
1658 break;
1659 case BUF_CMD_WRITE:
1660 error = hammer_vop_strategy_write(ap);
1661 break;
1662 default:
059819e3
MD
1663 bp->b_error = error = EINVAL;
1664 bp->b_flags |= B_ERROR;
1665 biodone(ap->a_bio);
8cd0a023
MD
1666 break;
1667 }
8cd0a023 1668 return (error);
427e5fc6
MD
1669}
1670
8cd0a023
MD
1671/*
1672 * Read from a regular file. Iterate the related records and fill in the
1673 * BIO/BUF. Gaps are zero-filled.
1674 *
1675 * The support code in hammer_object.c should be used to deal with mixed
1676 * in-memory and on-disk records.
1677 *
1678 * XXX atime update
1679 */
1680static
1681int
1682hammer_vop_strategy_read(struct vop_strategy_args *ap)
1683{
36f82b23
MD
1684 struct hammer_transaction trans;
1685 struct hammer_inode *ip;
8cd0a023
MD
1686 struct hammer_cursor cursor;
1687 hammer_record_ondisk_t rec;
1688 hammer_base_elm_t base;
1689 struct bio *bio;
1690 struct buf *bp;
1691 int64_t rec_offset;
a89aec1b 1692 int64_t ran_end;
195c19a1 1693 int64_t tmp64;
8cd0a023
MD
1694 int error;
1695 int boff;
1696 int roff;
1697 int n;
1698
1699 bio = ap->a_bio;
1700 bp = bio->bio_buf;
36f82b23 1701 ip = ap->a_vp->v_data;
8cd0a023 1702
36f82b23 1703 hammer_simple_transaction(&trans, ip->hmp);
4e17f465 1704 hammer_init_cursor(&trans, &cursor, &ip->cache[0], ip);
8cd0a023
MD
1705
1706 /*
1707 * Key range (begin and end inclusive) to scan. Note that the key's
c0ade690
MD
1708 * stored in the actual records represent BASE+LEN, not BASE. The
1709 * first record containing bio_offset will have a key > bio_offset.
8cd0a023
MD
1710 */
1711 cursor.key_beg.obj_id = ip->obj_id;
d5530d22 1712 cursor.key_beg.create_tid = 0;
8cd0a023 1713 cursor.key_beg.delete_tid = 0;
8cd0a023 1714 cursor.key_beg.obj_type = 0;
c0ade690 1715 cursor.key_beg.key = bio->bio_offset + 1;
d5530d22 1716 cursor.asof = ip->obj_asof;
47197d71 1717 cursor.flags |= HAMMER_CURSOR_ASOF | HAMMER_CURSOR_DATAEXTOK;
8cd0a023
MD
1718
1719 cursor.key_end = cursor.key_beg;
b84de5af
MD
1720 KKASSERT(ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_REGFILE);
1721#if 0
a89aec1b
MD
1722 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1723 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
1724 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
1725 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
b84de5af
MD
1726 } else
1727#endif
1728 {
c0ade690 1729 ran_end = bio->bio_offset + bp->b_bufsize;
a89aec1b
MD
1730 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
1731 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
195c19a1
MD
1732 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
1733 if (tmp64 < ran_end)
a89aec1b
MD
1734 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1735 else
7f7c1f84 1736 cursor.key_end.key = ran_end + MAXPHYS + 1;
a89aec1b 1737 }
d26d0ae9 1738 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
8cd0a023 1739
4e17f465 1740 error = hammer_ip_first(&cursor);
8cd0a023
MD
1741 boff = 0;
1742
a89aec1b
MD
1743 while (error == 0) {
1744 error = hammer_ip_resolve_data(&cursor);
1745 if (error)
66325755 1746 break;
a89aec1b 1747 rec = cursor.record;
8cd0a023
MD
1748 base = &rec->base.base;
1749
c0ade690 1750 rec_offset = base->key - rec->data.base.data_len;
8cd0a023 1751
66325755 1752 /*
a89aec1b 1753 * Calculate the gap, if any, and zero-fill it.
66325755 1754 */
8cd0a023
MD
1755 n = (int)(rec_offset - (bio->bio_offset + boff));
1756 if (n > 0) {
a89aec1b
MD
1757 if (n > bp->b_bufsize - boff)
1758 n = bp->b_bufsize - boff;
8cd0a023
MD
1759 bzero((char *)bp->b_data + boff, n);
1760 boff += n;
1761 n = 0;
66325755 1762 }
8cd0a023
MD
1763
1764 /*
1765 * Calculate the data offset in the record and the number
1766 * of bytes we can copy.
a89aec1b
MD
1767 *
1768 * Note there is a degenerate case here where boff may
1769 * already be at bp->b_bufsize.
8cd0a023
MD
1770 */
1771 roff = -n;
b84de5af 1772 rec_offset += roff;
8cd0a023
MD
1773 n = rec->data.base.data_len - roff;
1774 KKASSERT(n > 0);
1775 if (n > bp->b_bufsize - boff)
1776 n = bp->b_bufsize - boff;
059819e3 1777
b84de5af
MD
1778 /*
1779 * If we cached a truncation point on our front-end the
1780 * on-disk version may still have physical records beyond
1781 * that point. Truncate visibility.
1782 */
1783 if (ip->trunc_off <= rec_offset)
1784 n = 0;
1785 else if (ip->trunc_off < rec_offset + n)
1786 n = (int)(ip->trunc_off - rec_offset);
1787
1788 /*
1789 * Copy
1790 */
1791 if (n) {
1792 bcopy((char *)cursor.data + roff,
1793 (char *)bp->b_data + boff, n);
1794 boff += n;
1795 }
8cd0a023 1796 if (boff == bp->b_bufsize)
66325755 1797 break;
a89aec1b 1798 error = hammer_ip_next(&cursor);
66325755 1799 }
8cd0a023 1800 hammer_done_cursor(&cursor);
b84de5af 1801 hammer_done_transaction(&trans);
66325755
MD
1802
1803 /*
8cd0a023 1804 * There may have been a gap after the last record
66325755 1805 */
8cd0a023
MD
1806 if (error == ENOENT)
1807 error = 0;
1808 if (error == 0 && boff != bp->b_bufsize) {
7f7c1f84 1809 KKASSERT(boff < bp->b_bufsize);
8cd0a023
MD
1810 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
1811 /* boff = bp->b_bufsize; */
1812 }
1813 bp->b_resid = 0;
059819e3
MD
1814 bp->b_error = error;
1815 if (error)
1816 bp->b_flags |= B_ERROR;
1817 biodone(ap->a_bio);
8cd0a023
MD
1818 return(error);
1819}
1820
1821/*
059819e3
MD
1822 * Write to a regular file. Because this is a strategy call the OS is
1823 * trying to actually sync data to the media. HAMMER can only flush
1824 * the entire inode (so the TID remains properly synchronized).
8cd0a023 1825 *
059819e3
MD
1826 * Basically all we do here is place the bio on the inode's flush queue
1827 * and activate the flusher.
8cd0a023
MD
1828 */
1829static
1830int
1831hammer_vop_strategy_write(struct vop_strategy_args *ap)
1832{
8cd0a023
MD
1833 hammer_inode_t ip;
1834 struct bio *bio;
1835 struct buf *bp;
8cd0a023
MD
1836
1837 bio = ap->a_bio;
1838 bp = bio->bio_buf;
1839 ip = ap->a_vp->v_data;
d113fda1 1840
059819e3
MD
1841 if (ip->flags & HAMMER_INODE_RO) {
1842 bp->b_error = EROFS;
1843 bp->b_flags |= B_ERROR;
1844 biodone(ap->a_bio);
1845 return(EROFS);
1846 }
b84de5af
MD
1847
1848 /*
1849 * If the inode is being flushed we cannot re-queue buffers
1850 * it may have already flushed, or it could result in duplicate
1851 * records in the database.
1852 */
059819e3 1853 BUF_KERNPROC(bp);
1f07f686 1854 if (ip->flags & HAMMER_INODE_WRITE_ALT)
b84de5af
MD
1855 TAILQ_INSERT_TAIL(&ip->bio_alt_list, bio, bio_act);
1856 else
1857 TAILQ_INSERT_TAIL(&ip->bio_list, bio, bio_act);
1f07f686
MD
1858 ++hammer_bio_count;
1859 hammer_modify_inode(NULL, ip, HAMMER_INODE_BUFS);
4e17f465
MD
1860
1861 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
1862#if 0
1863 /*
1864 * XXX
1865 *
1866 * If the write was not part of an integrated flush operation then
1867 * signal a flush.
1868 */
1869 if (ip->flush_state != HAMMER_FST_FLUSH ||
1870 (ip->flags & HAMMER_INODE_WRITE_ALT)) {
1871 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
1872 }
1873#endif
059819e3
MD
1874 return(0);
1875}
1876
1877/*
b84de5af 1878 * Backend code which actually performs the write to the media. This
059819e3
MD
1879 * routine is typically called from the flusher. The bio will be disposed
1880 * of (biodone'd) by this routine.
1881 *
1882 * Iterate the related records and mark for deletion. If existing edge
1883 * records (left and right side) overlap our write they have to be marked
1884 * deleted and new records created, usually referencing a portion of the
1885 * original data. Then add a record to represent the buffer.
1886 */
1887int
4e17f465 1888hammer_dowrite(hammer_cursor_t cursor, hammer_inode_t ip, struct bio *bio)
059819e3
MD
1889{
1890 struct buf *bp = bio->bio_buf;
1891 int error;
8cd0a023 1892
b84de5af
MD
1893 KKASSERT(ip->flush_state == HAMMER_FST_FLUSH);
1894
869e8f55
MD
1895 /*
1896 * If the inode is going or gone, just throw away any frontend
1897 * buffers.
1898 */
1899 if (ip->flags & HAMMER_INODE_DELETED) {
1900 bp->b_resid = 0;
1901 biodone(bio);
1902 }
1903
8cd0a023
MD
1904 /*
1905 * Delete any records overlapping our range. This function will
d26d0ae9 1906 * (eventually) properly truncate partial overlaps.
8cd0a023 1907 */
b84de5af 1908 if (ip->sync_ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
4e17f465 1909 error = hammer_ip_delete_range(cursor, ip, bio->bio_offset,
47197d71 1910 bio->bio_offset);
a89aec1b 1911 } else {
4e17f465 1912 error = hammer_ip_delete_range(cursor, ip, bio->bio_offset,
d26d0ae9 1913 bio->bio_offset +
47197d71 1914 bp->b_bufsize - 1);
a89aec1b 1915 }
8cd0a023
MD
1916
1917 /*
e38e0b15
MD
1918 * Add a single record to cover the write. We can write a record
1919 * with only the actual file data - for example, a small 200 byte
1920 * file does not have to write out a 16K record.
1921 *
1922 * While the data size does not have to be aligned, we still do it
1923 * to reduce fragmentation in a future allocation model.
8cd0a023
MD
1924 */
1925 if (error == 0) {
e38e0b15
MD
1926 int limit_size;
1927
b84de5af
MD
1928 if (ip->sync_ino_rec.ino_size - bio->bio_offset >
1929 bp->b_bufsize) {
1930 limit_size = bp->b_bufsize;
e38e0b15 1931 } else {
b84de5af 1932 limit_size = (int)(ip->sync_ino_rec.ino_size -
e38e0b15
MD
1933 bio->bio_offset);
1934 KKASSERT(limit_size >= 0);
1935 limit_size = (limit_size + 63) & ~63;
1936 }
4e17f465
MD
1937 if (limit_size) {
1938 error = hammer_ip_sync_data(cursor, ip, bio->bio_offset,
1939 bp->b_data, limit_size);
1940 }
66325755 1941 }
a5fddc16
MD
1942 if (error)
1943 Debugger("hammer_dowrite: error");
66325755 1944
8cd0a023 1945 if (error) {
8cd0a023 1946 bp->b_resid = bp->b_bufsize;
059819e3
MD
1947 bp->b_error = error;
1948 bp->b_flags |= B_ERROR;
8cd0a023 1949 } else {
8cd0a023
MD
1950 bp->b_resid = 0;
1951 }
059819e3 1952 biodone(bio);
1f07f686 1953 --hammer_bio_count;
8cd0a023
MD
1954 return(error);
1955}
1956
1957/*
1958 * dounlink - disconnect a directory entry
1959 *
1960 * XXX whiteout support not really in yet
1961 */
1962static int
b84de5af
MD
1963hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
1964 struct vnode *dvp, struct ucred *cred, int flags)
8cd0a023 1965{
8cd0a023
MD
1966 struct namecache *ncp;
1967 hammer_inode_t dip;
1968 hammer_inode_t ip;
1969 hammer_record_ondisk_t rec;
1970 struct hammer_cursor cursor;
8cd0a023
MD
1971 int64_t namekey;
1972 int error;
1973
1974 /*
1975 * Calculate the namekey and setup the key range for the scan. This
1976 * works kinda like a chained hash table where the lower 32 bits
1977 * of the namekey synthesize the chain.
1978 *
1979 * The key range is inclusive of both key_beg and key_end.
1980 */
1981 dip = VTOI(dvp);
1982 ncp = nch->ncp;
d113fda1
MD
1983
1984 if (dip->flags & HAMMER_INODE_RO)
1985 return (EROFS);
1986
6a37e7e4
MD
1987 namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
1988retry:
4e17f465 1989 hammer_init_cursor(trans, &cursor, &dip->cache[0], dip);
8cd0a023
MD
1990 cursor.key_beg.obj_id = dip->obj_id;
1991 cursor.key_beg.key = namekey;
d5530d22 1992 cursor.key_beg.create_tid = 0;
8cd0a023
MD
1993 cursor.key_beg.delete_tid = 0;
1994 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1995 cursor.key_beg.obj_type = 0;
1996
1997 cursor.key_end = cursor.key_beg;
1998 cursor.key_end.key |= 0xFFFFFFFFULL;
d5530d22
MD
1999 cursor.asof = dip->obj_asof;
2000 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
8cd0a023 2001
8cd0a023
MD
2002 /*
2003 * Scan all matching records (the chain), locate the one matching
2004 * the requested path component. info->last_error contains the
2005 * error code on search termination and could be 0, ENOENT, or
2006 * something else.
2007 *
2008 * The hammer_ip_*() functions merge in-memory records with on-disk
2009 * records for the purposes of the search.
2010 */
4e17f465
MD
2011 rec = NULL;
2012 error = hammer_ip_first(&cursor);
2013
a89aec1b
MD
2014 while (error == 0) {
2015 error = hammer_ip_resolve_data(&cursor);
2016 if (error)
66325755 2017 break;
a89aec1b 2018 rec = cursor.record;
8cd0a023 2019 if (ncp->nc_nlen == rec->entry.base.data_len &&
40043e7f 2020 bcmp(ncp->nc_name, cursor.data, ncp->nc_nlen) == 0) {
66325755
MD
2021 break;
2022 }
a89aec1b 2023 error = hammer_ip_next(&cursor);
66325755 2024 }
8cd0a023
MD
2025
2026 /*
2027 * If all is ok we have to get the inode so we can adjust nlinks.
b3deaf57
MD
2028 *
2029 * If the target is a directory, it must be empty.
8cd0a023 2030 */
66325755 2031 if (error == 0) {
b84de5af 2032 ip = hammer_get_inode(trans, &dip->cache[1],
61aeeb33 2033 rec->entry.obj_id,
d113fda1 2034 dip->hmp->asof, 0, &error);
46fe7ae1
MD
2035 if (error == ENOENT) {
2036 kprintf("obj_id %016llx\n", rec->entry.obj_id);
10a5d1ba 2037 Debugger("ENOENT unlinking object that should exist");
46fe7ae1 2038 }
1f07f686
MD
2039
2040 /*
2041 * If we are trying to remove a directory the directory must
2042 * be empty.
2043 *
2044 * WARNING: hammer_ip_check_directory_empty() may have to
2045 * terminate the cursor to avoid a deadlock. It is ok to
2046 * call hammer_done_cursor() twice.
2047 */
b3deaf57
MD
2048 if (error == 0 && ip->ino_rec.base.base.obj_type ==
2049 HAMMER_OBJTYPE_DIRECTORY) {
1f07f686
MD
2050 error = hammer_ip_check_directory_empty(trans, &cursor,
2051 ip);
b3deaf57 2052 }
1f07f686 2053
6a37e7e4 2054 /*
1f07f686
MD
2055 * Delete the directory entry.
2056 *
6a37e7e4 2057 * WARNING: hammer_ip_del_directory() may have to terminate
1f07f686 2058 * the cursor to avoid a deadlock. It is ok to call
6a37e7e4
MD
2059 * hammer_done_cursor() twice.
2060 */
b84de5af 2061 if (error == 0) {
b84de5af
MD
2062 error = hammer_ip_del_directory(trans, &cursor,
2063 dip, ip);
b84de5af 2064 }
8cd0a023
MD
2065 if (error == 0) {
2066 cache_setunresolved(nch);
2067 cache_setvp(nch, NULL);
2068 /* XXX locking */
2069 if (ip->vp)
2070 cache_inval_vp(ip->vp, CINV_DESTROY);
2071 }
a89aec1b 2072 hammer_rel_inode(ip, 0);
66325755 2073 }
6a37e7e4
MD
2074 hammer_done_cursor(&cursor);
2075 if (error == EDEADLK)
2076 goto retry;
9c448776 2077
66325755 2078 return (error);
66325755
MD
2079}
2080
7a04d74f
MD
2081/************************************************************************
2082 * FIFO AND SPECFS OPS *
2083 ************************************************************************
2084 *
2085 */
2086
2087static int
2088hammer_vop_fifoclose (struct vop_close_args *ap)
2089{
2090 /* XXX update itimes */
2091 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
2092}
2093
2094static int
2095hammer_vop_fiforead (struct vop_read_args *ap)
2096{
2097 int error;
2098
2099 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2100 /* XXX update access time */
2101 return (error);
2102}
2103
2104static int
2105hammer_vop_fifowrite (struct vop_write_args *ap)
2106{
2107 int error;
2108
2109 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2110 /* XXX update access time */
2111 return (error);
2112}
2113
2114static int
2115hammer_vop_specclose (struct vop_close_args *ap)
2116{
2117 /* XXX update itimes */
2118 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2119}
2120
2121static int
2122hammer_vop_specread (struct vop_read_args *ap)
2123{
2124 /* XXX update access time */
2125 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2126}
2127
2128static int
2129hammer_vop_specwrite (struct vop_write_args *ap)
2130{
2131 /* XXX update last change time */
2132 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2133}
2134