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