kernel - Cluster fixes + Enable clustering for HAMMER1
[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 *
fbb84158 34 * $DragonFly: src/sys/vfs/hammer/hammer_vnops.c,v 1.102 2008/10/16 17:24:16 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>
fbb84158 47#include <sys/file.h>
c0ade690 48#include <vm/vm_extern.h>
18bee4a2 49#include <vm/swap_pager.h>
7a04d74f 50#include <vfs/fifofs/fifo.h>
684a93c4 51
427e5fc6
MD
52#include "hammer.h"
53
54/*
55 * USERFS VNOPS
56 */
57/*static int hammer_vop_vnoperate(struct vop_generic_args *);*/
66325755
MD
58static int hammer_vop_fsync(struct vop_fsync_args *);
59static int hammer_vop_read(struct vop_read_args *);
60static int hammer_vop_write(struct vop_write_args *);
61static int hammer_vop_access(struct vop_access_args *);
62static int hammer_vop_advlock(struct vop_advlock_args *);
63static int hammer_vop_close(struct vop_close_args *);
64static int hammer_vop_ncreate(struct vop_ncreate_args *);
65static int hammer_vop_getattr(struct vop_getattr_args *);
66static int hammer_vop_nresolve(struct vop_nresolve_args *);
67static int hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *);
68static int hammer_vop_nlink(struct vop_nlink_args *);
69static int hammer_vop_nmkdir(struct vop_nmkdir_args *);
70static int hammer_vop_nmknod(struct vop_nmknod_args *);
71static int hammer_vop_open(struct vop_open_args *);
66325755
MD
72static int hammer_vop_print(struct vop_print_args *);
73static int hammer_vop_readdir(struct vop_readdir_args *);
74static int hammer_vop_readlink(struct vop_readlink_args *);
75static int hammer_vop_nremove(struct vop_nremove_args *);
76static int hammer_vop_nrename(struct vop_nrename_args *);
77static int hammer_vop_nrmdir(struct vop_nrmdir_args *);
349433c9 78static int hammer_vop_markatime(struct vop_markatime_args *);
66325755
MD
79static int hammer_vop_setattr(struct vop_setattr_args *);
80static int hammer_vop_strategy(struct vop_strategy_args *);
a99b9ea2 81static int hammer_vop_bmap(struct vop_bmap_args *ap);
66325755
MD
82static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
83static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
7dc57964 84static int hammer_vop_ioctl(struct vop_ioctl_args *);
513ca7d7 85static int hammer_vop_mountctl(struct vop_mountctl_args *);
fbb84158 86static int hammer_vop_kqfilter (struct vop_kqfilter_args *);
427e5fc6 87
7a04d74f
MD
88static int hammer_vop_fifoclose (struct vop_close_args *);
89static int hammer_vop_fiforead (struct vop_read_args *);
90static int hammer_vop_fifowrite (struct vop_write_args *);
fbb84158 91static int hammer_vop_fifokqfilter (struct vop_kqfilter_args *);
7a04d74f 92
427e5fc6
MD
93struct vop_ops hammer_vnode_vops = {
94 .vop_default = vop_defaultop,
95 .vop_fsync = hammer_vop_fsync,
c0ade690
MD
96 .vop_getpages = vop_stdgetpages,
97 .vop_putpages = vop_stdputpages,
427e5fc6
MD
98 .vop_read = hammer_vop_read,
99 .vop_write = hammer_vop_write,
100 .vop_access = hammer_vop_access,
101 .vop_advlock = hammer_vop_advlock,
102 .vop_close = hammer_vop_close,
103 .vop_ncreate = hammer_vop_ncreate,
104 .vop_getattr = hammer_vop_getattr,
105 .vop_inactive = hammer_vop_inactive,
106 .vop_reclaim = hammer_vop_reclaim,
107 .vop_nresolve = hammer_vop_nresolve,
108 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
109 .vop_nlink = hammer_vop_nlink,
110 .vop_nmkdir = hammer_vop_nmkdir,
111 .vop_nmknod = hammer_vop_nmknod,
112 .vop_open = hammer_vop_open,
64950f31 113 .vop_pathconf = vop_stdpathconf,
427e5fc6
MD
114 .vop_print = hammer_vop_print,
115 .vop_readdir = hammer_vop_readdir,
116 .vop_readlink = hammer_vop_readlink,
117 .vop_nremove = hammer_vop_nremove,
118 .vop_nrename = hammer_vop_nrename,
119 .vop_nrmdir = hammer_vop_nrmdir,
349433c9 120 .vop_markatime = hammer_vop_markatime,
427e5fc6 121 .vop_setattr = hammer_vop_setattr,
a99b9ea2 122 .vop_bmap = hammer_vop_bmap,
427e5fc6
MD
123 .vop_strategy = hammer_vop_strategy,
124 .vop_nsymlink = hammer_vop_nsymlink,
7dc57964 125 .vop_nwhiteout = hammer_vop_nwhiteout,
513ca7d7 126 .vop_ioctl = hammer_vop_ioctl,
fbb84158
MD
127 .vop_mountctl = hammer_vop_mountctl,
128 .vop_kqfilter = hammer_vop_kqfilter
427e5fc6
MD
129};
130
7a04d74f 131struct vop_ops hammer_spec_vops = {
8be7edad 132 .vop_default = vop_defaultop,
7a04d74f 133 .vop_fsync = hammer_vop_fsync,
8be7edad
MD
134 .vop_read = vop_stdnoread,
135 .vop_write = vop_stdnowrite,
7a04d74f 136 .vop_access = hammer_vop_access,
8be7edad 137 .vop_close = hammer_vop_close,
349433c9 138 .vop_markatime = hammer_vop_markatime,
8be7edad 139 .vop_getattr = hammer_vop_getattr,
7a04d74f
MD
140 .vop_inactive = hammer_vop_inactive,
141 .vop_reclaim = hammer_vop_reclaim,
142 .vop_setattr = hammer_vop_setattr
143};
144
145struct vop_ops hammer_fifo_vops = {
146 .vop_default = fifo_vnoperate,
147 .vop_fsync = hammer_vop_fsync,
148 .vop_read = hammer_vop_fiforead,
149 .vop_write = hammer_vop_fifowrite,
150 .vop_access = hammer_vop_access,
151 .vop_close = hammer_vop_fifoclose,
349433c9 152 .vop_markatime = hammer_vop_markatime,
7a04d74f
MD
153 .vop_getattr = hammer_vop_getattr,
154 .vop_inactive = hammer_vop_inactive,
155 .vop_reclaim = hammer_vop_reclaim,
fbb84158
MD
156 .vop_setattr = hammer_vop_setattr,
157 .vop_kqfilter = hammer_vop_fifokqfilter
7a04d74f
MD
158};
159
fbb84158
MD
160static __inline
161void
162hammer_knote(struct vnode *vp, int flags)
163{
164 if (flags)
5b22f1a7 165 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
fbb84158
MD
166}
167
0832c9bb
MD
168#ifdef DEBUG_TRUNCATE
169struct hammer_inode *HammerTruncIp;
170#endif
171
b84de5af 172static int hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
d7e278bb
MD
173 struct vnode *dvp, struct ucred *cred,
174 int flags, int isdir);
8cd0a023
MD
175static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
176static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
177
427e5fc6
MD
178#if 0
179static
180int
181hammer_vop_vnoperate(struct vop_generic_args *)
182{
183 return (VOCALL(&hammer_vnode_vops, ap));
184}
185#endif
186
66325755
MD
187/*
188 * hammer_vop_fsync { vp, waitfor }
ddfdf542
MD
189 *
190 * fsync() an inode to disk and wait for it to be completely committed
191 * such that the information would not be undone if a crash occured after
192 * return.
6f3d87c0
MD
193 *
194 * NOTE: HAMMER's fsync()'s are going to remain expensive until we implement
195 * a REDO log. A sysctl is provided to relax HAMMER's fsync()
196 * operation.
197 *
198 * Ultimately the combination of a REDO log and use of fast storage
199 * to front-end cluster caches will make fsync fast, but it aint
200 * here yet. And, in anycase, we need real transactional
201 * all-or-nothing features which are not restricted to a single file.
66325755 202 */
427e5fc6
MD
203static
204int
66325755 205hammer_vop_fsync(struct vop_fsync_args *ap)
427e5fc6 206{
b84de5af 207 hammer_inode_t ip = VTOI(ap->a_vp);
9192654c 208 hammer_mount_t hmp = ip->hmp;
6f3d87c0 209 int waitfor = ap->a_waitfor;
9192654c 210 int mode;
6f3d87c0 211
b0aab9b9
MD
212 lwkt_gettoken(&hmp->fs_token);
213
6f3d87c0 214 /*
9192654c
MD
215 * Fsync rule relaxation (default is either full synchronous flush
216 * or REDO semantics with synchronous flush).
6f3d87c0
MD
217 */
218 if (ap->a_flags & VOP_FSYNC_SYSCALL) {
219 switch(hammer_fsync_mode) {
220 case 0:
9192654c 221mode0:
47f363f1 222 /* no REDO, full synchronous flush */
9192654c 223 goto skip;
6f3d87c0 224 case 1:
9192654c 225mode1:
47f363f1 226 /* no REDO, full asynchronous flush */
6f3d87c0
MD
227 if (waitfor == MNT_WAIT)
228 waitfor = MNT_NOWAIT;
9192654c 229 goto skip;
6f3d87c0 230 case 2:
9192654c
MD
231 /* REDO semantics, synchronous flush */
232 if (hmp->version < HAMMER_VOL_VERSION_FOUR)
233 goto mode0;
234 mode = HAMMER_FLUSH_UNDOS_AUTO;
235 break;
6f3d87c0 236 case 3:
9192654c
MD
237 /* REDO semantics, relaxed asynchronous flush */
238 if (hmp->version < HAMMER_VOL_VERSION_FOUR)
239 goto mode1;
240 mode = HAMMER_FLUSH_UNDOS_RELAXED;
241 if (waitfor == MNT_WAIT)
242 waitfor = MNT_NOWAIT;
243 break;
244 case 4:
245 /* ignore the fsync() system call */
b0aab9b9 246 lwkt_reltoken(&hmp->fs_token);
6f3d87c0
MD
247 return(0);
248 default:
9192654c
MD
249 /* we have to do something */
250 mode = HAMMER_FLUSH_UNDOS_RELAXED;
251 if (waitfor == MNT_WAIT)
252 waitfor = MNT_NOWAIT;
253 break;
254 }
255
256 /*
47f363f1
MD
257 * Fast fsync only needs to flush the UNDO/REDO fifo if
258 * HAMMER_INODE_REDO is non-zero and the only modifications
259 * made to the file are write or write-extends.
9192654c 260 */
47f363f1 261 if ((ip->flags & HAMMER_INODE_REDO) &&
9192654c
MD
262 (ip->flags & HAMMER_INODE_MODMASK_NOREDO) == 0
263 ) {
264 ++hammer_count_fsyncs;
265 hammer_flusher_flush_undos(hmp, mode);
266 ip->redo_count = 0;
b0aab9b9 267 lwkt_reltoken(&hmp->fs_token);
6f3d87c0
MD
268 return(0);
269 }
47f363f1
MD
270
271 /*
272 * REDO is enabled by fsync(), the idea being we really only
273 * want to lay down REDO records when programs are using
274 * fsync() heavily. The first fsync() on the file starts
275 * the gravy train going and later fsync()s keep it hot by
276 * resetting the redo_count.
277 *
278 * We weren't running REDOs before now so we have to fall
279 * through and do a full fsync of what we have.
280 */
c58123da
MD
281 if (hmp->version >= HAMMER_VOL_VERSION_FOUR &&
282 (hmp->flags & HAMMER_MOUNT_REDO_RECOVERY_RUN) == 0) {
47f363f1
MD
283 ip->flags |= HAMMER_INODE_REDO;
284 ip->redo_count = 0;
285 }
6f3d87c0 286 }
9192654c 287skip:
c0ade690 288
6f3d87c0 289 /*
9192654c 290 * Do a full flush sequence.
9defaf9a
MD
291 *
292 * Attempt to release the vnode while waiting for the inode to
293 * finish flushing. This can really mess up inactive->reclaim
294 * sequences so only do it if the vnode is active.
6f3d87c0 295 */
7a61b85d 296 ++hammer_count_fsyncs;
6f3d87c0 297 vfsync(ap->a_vp, waitfor, 1, NULL, NULL);
af209b0f 298 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
6f3d87c0 299 if (waitfor == MNT_WAIT) {
9defaf9a
MD
300 if ((ap->a_vp->v_flag & VINACTIVE) == 0)
301 vn_unlock(ap->a_vp);
b84de5af 302 hammer_wait_inode(ip);
9defaf9a
MD
303 if ((ap->a_vp->v_flag & VINACTIVE) == 0)
304 vn_lock(ap->a_vp, LK_EXCLUSIVE | LK_RETRY);
b424ca30 305 }
b0aab9b9 306 lwkt_reltoken(&hmp->fs_token);
059819e3 307 return (ip->error);
427e5fc6
MD
308}
309
66325755
MD
310/*
311 * hammer_vop_read { vp, uio, ioflag, cred }
42cd5131 312 *
b0aab9b9 313 * MPSAFE (for the cache safe does not require fs_token)
66325755 314 */
427e5fc6
MD
315static
316int
66325755 317hammer_vop_read(struct vop_read_args *ap)
427e5fc6 318{
66325755 319 struct hammer_transaction trans;
c0ade690 320 hammer_inode_t ip;
b0aab9b9 321 hammer_mount_t hmp;
66325755
MD
322 off_t offset;
323 struct buf *bp;
324 struct uio *uio;
325 int error;
326 int n;
8cd0a023 327 int seqcount;
4a2796f3
MD
328 int ioseqcount;
329 int blksize;
f864373f 330 int bigread;
b0aab9b9 331 int got_fstoken;
66325755
MD
332
333 if (ap->a_vp->v_type != VREG)
334 return (EINVAL);
335 ip = VTOI(ap->a_vp);
b0aab9b9 336 hmp = ip->hmp;
66325755 337 error = 0;
4a2796f3
MD
338 uio = ap->a_uio;
339
340 /*
341 * Allow the UIO's size to override the sequential heuristic.
342 */
343 blksize = hammer_blocksize(uio->uio_offset);
69adbed4
MD
344 seqcount = (uio->uio_resid + (BKVASIZE - 1)) / BKVASIZE;
345 ioseqcount = (ap->a_ioflag >> 16);
4a2796f3
MD
346 if (seqcount < ioseqcount)
347 seqcount = ioseqcount;
66325755 348
7ff770b4 349 /*
f864373f
MD
350 * If reading or writing a huge amount of data we have to break
351 * atomicy and allow the operation to be interrupted by a signal
352 * or it can DOS the machine.
353 */
354 bigread = (uio->uio_resid > 100 * 1024 * 1024);
b0aab9b9 355 got_fstoken = 0;
f864373f
MD
356
357 /*
4a2796f3
MD
358 * Access the data typically in HAMMER_BUFSIZE blocks via the
359 * buffer cache, but HAMMER may use a variable block size based
360 * on the offset.
42cd5131
MD
361 *
362 * XXX Temporary hack, delay the start transaction while we remain
363 * MPSAFE. NOTE: ino_data.size cannot change while vnode is
364 * locked-shared.
66325755 365 */
11ad5ade 366 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_data.size) {
4a2796f3
MD
367 int64_t base_offset;
368 int64_t file_limit;
369
370 blksize = hammer_blocksize(uio->uio_offset);
371 offset = (int)uio->uio_offset & (blksize - 1);
372 base_offset = uio->uio_offset - offset;
373
f864373f
MD
374 if (bigread && (error = hammer_signal_check(ip->hmp)) != 0)
375 break;
376
42cd5131
MD
377 /*
378 * MPSAFE
379 */
54341a3b
MD
380 bp = getblk(ap->a_vp, base_offset, blksize, 0, 0);
381 if ((bp->b_flags & (B_INVAL | B_CACHE | B_RAM)) == B_CACHE) {
382 bp->b_flags &= ~B_AGE;
42cd5131
MD
383 error = 0;
384 goto skip;
54341a3b
MD
385 }
386 if (ap->a_ioflag & IO_NRDELAY) {
387 bqrelse(bp);
388 return (EWOULDBLOCK);
42cd5131
MD
389 }
390
391 /*
392 * MPUNSAFE
393 */
b0aab9b9
MD
394 if (got_fstoken == 0) {
395 lwkt_gettoken(&hmp->fs_token);
396 got_fstoken = 1;
42cd5131
MD
397 hammer_start_transaction(&trans, ip->hmp);
398 }
399
54341a3b
MD
400 /*
401 * NOTE: A valid bp has already been acquired, but was not
402 * B_CACHE.
403 */
1b0ab2c3 404 if (hammer_cluster_enable) {
4a2796f3
MD
405 /*
406 * Use file_limit to prevent cluster_read() from
407 * creating buffers of the wrong block size past
408 * the demarc.
409 */
410 file_limit = ip->ino_data.size;
411 if (base_offset < HAMMER_XDEMARC &&
412 file_limit > HAMMER_XDEMARC) {
413 file_limit = HAMMER_XDEMARC;
414 }
54341a3b 415 error = cluster_readx(ap->a_vp,
4a2796f3 416 file_limit, base_offset,
364c022c
MD
417 blksize, uio->uio_resid,
418 seqcount * BKVASIZE, &bp);
a99b9ea2 419 } else {
54341a3b
MD
420 error = breadnx(ap->a_vp, base_offset, blksize,
421 NULL, NULL, 0, &bp);
a99b9ea2 422 }
66325755
MD
423 if (error) {
424 brelse(bp);
425 break;
426 }
42cd5131 427skip:
24c8374a
MD
428 if ((hammer_debug_io & 0x0001) && (bp->b_flags & B_IODEBUG)) {
429 kprintf("doff %016jx read file %016jx@%016jx\n",
430 (intmax_t)bp->b_bio2.bio_offset,
431 (intmax_t)ip->obj_id,
432 (intmax_t)bp->b_loffset);
433 }
434 bp->b_flags &= ~B_IODEBUG;
7bc5b8c2 435
9d4e78c7 436 bp->b_flags |= B_CLUSTEROK;
4a2796f3 437 n = blksize - offset;
66325755
MD
438 if (n > uio->uio_resid)
439 n = uio->uio_resid;
11ad5ade
MD
440 if (n > ip->ino_data.size - uio->uio_offset)
441 n = (int)(ip->ino_data.size - uio->uio_offset);
df01a101
MD
442 if (got_fstoken)
443 lwkt_reltoken(&hmp->fs_token);
283b9448
MD
444
445 /*
446 * Set B_AGE, data has a lower priority than meta-data.
447 *
448 * Use a hold/unlock/drop sequence to run the uiomove
449 * with the buffer unlocked, avoiding deadlocks against
450 * read()s on mmap()'d spaces.
451 */
452 bp->b_flags |= B_AGE;
453 bqhold(bp);
454 bqrelse(bp);
66325755 455 error = uiomove((char *)bp->b_data + offset, n, uio);
283b9448
MD
456 bqdrop(bp);
457
df01a101
MD
458 if (got_fstoken)
459 lwkt_gettoken(&hmp->fs_token);
7bc5b8c2 460
af209b0f
MD
461 if (error)
462 break;
ce0138a6 463 hammer_stats_file_read += n;
66325755 464 }
42cd5131
MD
465
466 /*
70125e78
MD
467 * Try to update the atime with just the inode lock for maximum
468 * concurrency. If we can't shortcut it we have to get the full
469 * blown transaction.
42cd5131 470 */
70125e78
MD
471 if (got_fstoken == 0 && hammer_update_atime_quick(ip) < 0) {
472 lwkt_gettoken(&hmp->fs_token);
473 got_fstoken = 1;
474 hammer_start_transaction(&trans, ip->hmp);
475 }
476
b0aab9b9 477 if (got_fstoken) {
42cd5131
MD
478 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
479 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
480 ip->ino_data.atime = trans.time;
e98f1b96 481 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
42cd5131
MD
482 }
483 hammer_done_transaction(&trans);
b0aab9b9 484 lwkt_reltoken(&hmp->fs_token);
b84de5af 485 }
66325755 486 return (error);
427e5fc6
MD
487}
488
66325755
MD
489/*
490 * hammer_vop_write { vp, uio, ioflag, cred }
491 */
427e5fc6
MD
492static
493int
66325755 494hammer_vop_write(struct vop_write_args *ap)
427e5fc6 495{
66325755
MD
496 struct hammer_transaction trans;
497 struct hammer_inode *ip;
4a2796f3 498 hammer_mount_t hmp;
1589191a 499 thread_t td;
66325755 500 struct uio *uio;
4a2796f3 501 int offset;
47637bff 502 off_t base_offset;
9d4e78c7 503 int64_t cluster_eof;
66325755 504 struct buf *bp;
fbb84158 505 int kflags;
66325755
MD
506 int error;
507 int n;
c0ade690 508 int flags;
cb51be26 509 int seqcount;
f864373f 510 int bigwrite;
66325755
MD
511
512 if (ap->a_vp->v_type != VREG)
513 return (EINVAL);
514 ip = VTOI(ap->a_vp);
4a2796f3 515 hmp = ip->hmp;
66325755 516 error = 0;
fbb84158 517 kflags = 0;
cb51be26 518 seqcount = ap->a_ioflag >> 16;
66325755 519
d113fda1
MD
520 if (ip->flags & HAMMER_INODE_RO)
521 return (EROFS);
522
66325755
MD
523 /*
524 * Create a transaction to cover the operations we perform.
525 */
b0aab9b9 526 lwkt_gettoken(&hmp->fs_token);
4a2796f3 527 hammer_start_transaction(&trans, hmp);
66325755
MD
528 uio = ap->a_uio;
529
530 /*
531 * Check append mode
532 */
533 if (ap->a_ioflag & IO_APPEND)
11ad5ade 534 uio->uio_offset = ip->ino_data.size;
66325755
MD
535
536 /*
af209b0f
MD
537 * Check for illegal write offsets. Valid range is 0...2^63-1.
538 *
539 * NOTE: the base_off assignment is required to work around what
540 * I consider to be a GCC-4 optimization bug.
66325755 541 */
af209b0f
MD
542 if (uio->uio_offset < 0) {
543 hammer_done_transaction(&trans);
b0aab9b9 544 lwkt_reltoken(&hmp->fs_token);
af209b0f
MD
545 return (EFBIG);
546 }
547 base_offset = uio->uio_offset + uio->uio_resid; /* work around gcc-4 */
e54488bb 548 if (uio->uio_resid > 0 && base_offset <= uio->uio_offset) {
b84de5af 549 hammer_done_transaction(&trans);
b0aab9b9 550 lwkt_reltoken(&hmp->fs_token);
66325755 551 return (EFBIG);
9c448776 552 }
66325755 553
1589191a
MD
554 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
555 base_offset > td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
556 hammer_done_transaction(&trans);
557 lwkt_reltoken(&hmp->fs_token);
558 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
559 return (EFBIG);
560 }
561
66325755 562 /*
f864373f
MD
563 * If reading or writing a huge amount of data we have to break
564 * atomicy and allow the operation to be interrupted by a signal
565 * or it can DOS the machine.
9192654c 566 *
47f363f1
MD
567 * Preset redo_count so we stop generating REDOs earlier if the
568 * limit is exceeded.
f864373f
MD
569 */
570 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
47f363f1
MD
571 if ((ip->flags & HAMMER_INODE_REDO) &&
572 ip->redo_count < hammer_limit_redo) {
9192654c 573 ip->redo_count += uio->uio_resid;
47f363f1 574 }
f864373f
MD
575
576 /*
4a2796f3
MD
577 * Access the data typically in HAMMER_BUFSIZE blocks via the
578 * buffer cache, but HAMMER may use a variable block size based
579 * on the offset.
66325755
MD
580 */
581 while (uio->uio_resid > 0) {
d5ef456e 582 int fixsize = 0;
4a2796f3
MD
583 int blksize;
584 int blkmask;
6362a262 585 int trivial;
d1eff1f7 586 int endofblk;
6362a262 587 off_t nsize;
d5ef456e 588
93291532 589 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_WRITE)) != 0)
e63644f0 590 break;
f864373f
MD
591 if (bigwrite && (error = hammer_signal_check(hmp)) != 0)
592 break;
e63644f0 593
a9d52b76
MD
594 blksize = hammer_blocksize(uio->uio_offset);
595
059819e3 596 /*
4a2796f3
MD
597 * Do not allow HAMMER to blow out the buffer cache. Very
598 * large UIOs can lockout other processes due to bwillwrite()
599 * mechanics.
47637bff 600 *
df301614
MD
601 * The hammer inode is not locked during these operations.
602 * The vnode is locked which can interfere with the pageout
603 * daemon for non-UIO_NOCOPY writes but should not interfere
604 * with the buffer cache. Even so, we cannot afford to
605 * allow the pageout daemon to build up too many dirty buffer
606 * cache buffers.
cb63d1bc
MD
607 *
608 * Only call this if we aren't being recursively called from
609 * a virtual disk device (vn), else we may deadlock.
df301614 610 */
cb63d1bc
MD
611 if ((ap->a_ioflag & IO_RECURSE) == 0)
612 bwillwrite(blksize);
df301614
MD
613
614 /*
de996e86
MD
615 * Control the number of pending records associated with
616 * this inode. If too many have accumulated start a
617 * flush. Try to maintain a pipeline with the flusher.
a117fbeb
MD
618 *
619 * NOTE: It is possible for other sources to grow the
620 * records but not necessarily issue another flush,
621 * so use a timeout and ensure that a re-flush occurs.
de996e86
MD
622 */
623 if (ip->rsv_recs >= hammer_limit_inode_recs) {
624 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
a117fbeb
MD
625 while (ip->rsv_recs >= hammer_limit_inode_recs * 2) {
626 ip->flags |= HAMMER_INODE_RECSW;
de996e86 627 tsleep(&ip->rsv_recs, 0, "hmrwww", hz);
a117fbeb 628 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
de996e86 629 }
de996e86
MD
630 }
631
632#if 0
633 /*
e4a5ff06
MD
634 * Do not allow HAMMER to blow out system memory by
635 * accumulating too many records. Records are so well
636 * decoupled from the buffer cache that it is possible
637 * for userland to push data out to the media via
638 * direct-write, but build up the records queued to the
639 * backend faster then the backend can flush them out.
640 * HAMMER has hit its write limit but the frontend has
641 * no pushback to slow it down.
059819e3 642 */
df301614 643 if (hmp->rsv_recs > hammer_limit_recs / 2) {
4a2796f3 644 /*
df301614 645 * Get the inode on the flush list
4a2796f3 646 */
df301614
MD
647 if (ip->rsv_recs >= 64)
648 hammer_flush_inode(ip, HAMMER_FLUSH_SIGNAL);
649 else if (ip->rsv_recs >= 16)
650 hammer_flush_inode(ip, 0);
4a2796f3
MD
651
652 /*
df301614
MD
653 * Keep the flusher going if the system keeps
654 * queueing records.
4a2796f3 655 */
df301614
MD
656 delta = hmp->count_newrecords -
657 hmp->last_newrecords;
658 if (delta < 0 || delta > hammer_limit_recs / 2) {
659 hmp->last_newrecords = hmp->count_newrecords;
660 hammer_sync_hmp(hmp, MNT_NOWAIT);
4a2796f3
MD
661 }
662
df301614
MD
663 /*
664 * If we have gotten behind start slowing
665 * down the writers.
666 */
667 delta = (hmp->rsv_recs - hammer_limit_recs) *
668 hz / hammer_limit_recs;
669 if (delta > 0)
670 tsleep(&trans, 0, "hmrslo", delta);
059819e3 671 }
de996e86 672#endif
059819e3 673
4a2796f3
MD
674 /*
675 * Calculate the blocksize at the current offset and figure
676 * out how much we can actually write.
677 */
4a2796f3
MD
678 blkmask = blksize - 1;
679 offset = (int)uio->uio_offset & blkmask;
680 base_offset = uio->uio_offset & ~(int64_t)blkmask;
681 n = blksize - offset;
d1eff1f7 682 if (n > uio->uio_resid) {
d5ef456e 683 n = uio->uio_resid;
d1eff1f7
MD
684 endofblk = 0;
685 } else {
686 endofblk = 1;
687 }
6362a262
MD
688 nsize = uio->uio_offset + n;
689 if (nsize > ip->ino_data.size) {
690 if (uio->uio_offset > ip->ino_data.size)
691 trivial = 0;
692 else
693 trivial = 1;
694 nvextendbuf(ap->a_vp,
695 ip->ino_data.size,
696 nsize,
697 hammer_blocksize(ip->ino_data.size),
698 hammer_blocksize(nsize),
699 hammer_blockoff(ip->ino_data.size),
700 hammer_blockoff(nsize),
701 trivial);
d5ef456e 702 fixsize = 1;
fbb84158 703 kflags |= NOTE_EXTEND;
d5ef456e
MD
704 }
705
c0ade690
MD
706 if (uio->uio_segflg == UIO_NOCOPY) {
707 /*
708 * Issuing a write with the same data backing the
709 * buffer. Instantiate the buffer to collect the
710 * backing vm pages, then read-in any missing bits.
711 *
712 * This case is used by vop_stdputpages().
713 */
47637bff 714 bp = getblk(ap->a_vp, base_offset,
4a2796f3 715 blksize, GETBLK_BHEAVY, 0);
c0ade690
MD
716 if ((bp->b_flags & B_CACHE) == 0) {
717 bqrelse(bp);
47637bff 718 error = bread(ap->a_vp, base_offset,
4a2796f3 719 blksize, &bp);
c0ade690 720 }
4a2796f3 721 } else if (offset == 0 && uio->uio_resid >= blksize) {
c0ade690 722 /*
a5fddc16
MD
723 * Even though we are entirely overwriting the buffer
724 * we may still have to zero it out to avoid a
725 * mmap/write visibility issue.
c0ade690 726 */
4a2796f3 727 bp = getblk(ap->a_vp, base_offset, blksize, GETBLK_BHEAVY, 0);
a5fddc16
MD
728 if ((bp->b_flags & B_CACHE) == 0)
729 vfs_bio_clrbuf(bp);
47637bff 730 } else if (base_offset >= ip->ino_data.size) {
c0ade690 731 /*
a5fddc16
MD
732 * If the base offset of the buffer is beyond the
733 * file EOF, we don't have to issue a read.
c0ade690 734 */
47637bff 735 bp = getblk(ap->a_vp, base_offset,
4a2796f3 736 blksize, GETBLK_BHEAVY, 0);
66325755
MD
737 vfs_bio_clrbuf(bp);
738 } else {
c0ade690
MD
739 /*
740 * Partial overwrite, read in any missing bits then
741 * replace the portion being written.
742 */
4a2796f3 743 error = bread(ap->a_vp, base_offset, blksize, &bp);
d5ef456e
MD
744 if (error == 0)
745 bheavy(bp);
66325755 746 }
df01a101
MD
747 if (error == 0) {
748 lwkt_reltoken(&hmp->fs_token);
9192654c 749 error = uiomove(bp->b_data + offset, n, uio);
df01a101
MD
750 lwkt_gettoken(&hmp->fs_token);
751 }
9192654c
MD
752
753 /*
47f363f1
MD
754 * Generate REDO records if enabled and redo_count will not
755 * exceeded the limit.
756 *
757 * If redo_count exceeds the limit we stop generating records
758 * and clear HAMMER_INODE_REDO. This will cause the next
759 * fsync() to do a full meta-data sync instead of just an
760 * UNDO/REDO fifo update.
761 *
762 * When clearing HAMMER_INODE_REDO any pre-existing REDOs
763 * will still be tracked. The tracks will be terminated
764 * when the related meta-data (including possible data
765 * modifications which are not tracked via REDO) is
766 * flushed.
9192654c 767 */
47f363f1
MD
768 if ((ip->flags & HAMMER_INODE_REDO) && error == 0) {
769 if (ip->redo_count < hammer_limit_redo) {
770 bp->b_flags |= B_VFSFLAG1;
771 error = hammer_generate_redo(&trans, ip,
9192654c 772 base_offset + offset,
47f363f1 773 HAMMER_REDO_WRITE,
9192654c
MD
774 bp->b_data + offset,
775 (size_t)n);
47f363f1
MD
776 } else {
777 ip->flags &= ~HAMMER_INODE_REDO;
778 }
47637bff 779 }
d5ef456e
MD
780
781 /*
782 * If we screwed up we have to undo any VM size changes we
783 * made.
784 */
66325755
MD
785 if (error) {
786 brelse(bp);
d5ef456e 787 if (fixsize) {
6362a262
MD
788 nvtruncbuf(ap->a_vp, ip->ino_data.size,
789 hammer_blocksize(ip->ino_data.size),
790 hammer_blockoff(ip->ino_data.size));
d5ef456e 791 }
66325755
MD
792 break;
793 }
fbb84158 794 kflags |= NOTE_WRITE;
ce0138a6 795 hammer_stats_file_write += n;
9d4e78c7 796 bp->b_flags |= B_CLUSTEROK;
11ad5ade
MD
797 if (ip->ino_data.size < uio->uio_offset) {
798 ip->ino_data.size = uio->uio_offset;
9192654c 799 flags = HAMMER_INODE_SDIRTY;
c0ade690 800 } else {
d113fda1 801 flags = 0;
66325755 802 }
11ad5ade 803 ip->ino_data.mtime = trans.time;
ddfdf542 804 flags |= HAMMER_INODE_MTIME | HAMMER_INODE_BUFS;
e98f1b96 805 hammer_modify_inode(&trans, ip, flags);
32c90105 806
0832c9bb 807 /*
1b0ab2c3
MD
808 * Once we dirty the buffer any cached zone-X offset
809 * becomes invalid. HAMMER NOTE: no-history mode cannot
810 * allow overwriting over the same data sector unless
811 * we provide UNDOs for the old data, which we don't.
812 */
813 bp->b_bio2.bio_offset = NOOFFSET;
814
815 /*
47637bff 816 * Final buffer disposition.
de996e86
MD
817 *
818 * Because meta-data updates are deferred, HAMMER is
819 * especially sensitive to excessive bdwrite()s because
820 * the I/O stream is not broken up by disk reads. So the
821 * buffer cache simply cannot keep up.
822 *
823 * WARNING! blksize is variable. cluster_write() is
d1eff1f7
MD
824 * expected to not blow up if it encounters
825 * buffers that do not match the passed blksize.
710733a6
MD
826 *
827 * NOTE! Hammer shouldn't need to bawrite()/cluster_write().
828 * The ip->rsv_recs check should burst-flush the data.
829 * If we queue it immediately the buf could be left
830 * locked on the device queue for a very long time.
d1eff1f7 831 *
55b50bd5
MD
832 * However, failing to flush a dirty buffer out when
833 * issued from the pageout daemon can result in a low
834 * memory deadlock against bio_page_alloc(), so we
835 * have to bawrite() on IO_ASYNC as well.
836 *
d1eff1f7
MD
837 * NOTE! To avoid degenerate stalls due to mismatched block
838 * sizes we only honor IO_DIRECT on the write which
839 * abuts the end of the buffer. However, we must
840 * honor IO_SYNC in case someone is silly enough to
841 * configure a HAMMER file as swap, or when HAMMER
842 * is serving NFS (for commits). Ick ick.
47637bff 843 */
9d4e78c7 844 bp->b_flags |= B_AGE | B_CLUSTEROK;
66325755
MD
845 if (ap->a_ioflag & IO_SYNC) {
846 bwrite(bp);
d1eff1f7 847 } else if ((ap->a_ioflag & IO_DIRECT) && endofblk) {
66325755 848 bawrite(bp);
55b50bd5
MD
849 } else if (ap->a_ioflag & IO_ASYNC) {
850 bawrite(bp);
9d4e78c7
MD
851 } else if (hammer_cluster_enable &&
852 !(ap->a_vp->v_mount->mnt_flag & MNT_NOCLUSTERW)) {
853 if (base_offset < HAMMER_XDEMARC)
854 cluster_eof = hammer_blockdemarc(base_offset,
855 ip->ino_data.size);
856 else
857 cluster_eof = ip->ino_data.size;
858 cluster_write(bp, cluster_eof, blksize, seqcount);
710733a6 859 } else {
4a2796f3
MD
860 bdwrite(bp);
861 }
66325755 862 }
b84de5af 863 hammer_done_transaction(&trans);
fbb84158 864 hammer_knote(ap->a_vp, kflags);
b0aab9b9 865 lwkt_reltoken(&hmp->fs_token);
66325755 866 return (error);
427e5fc6
MD
867}
868
66325755
MD
869/*
870 * hammer_vop_access { vp, mode, cred }
b0aab9b9
MD
871 *
872 * MPSAFE - does not require fs_token
66325755 873 */
427e5fc6
MD
874static
875int
66325755 876hammer_vop_access(struct vop_access_args *ap)
427e5fc6 877{
66325755
MD
878 struct hammer_inode *ip = VTOI(ap->a_vp);
879 uid_t uid;
880 gid_t gid;
881 int error;
882
ce0138a6 883 ++hammer_stats_file_iopsr;
66325755
MD
884 uid = hammer_to_unix_xid(&ip->ino_data.uid);
885 gid = hammer_to_unix_xid(&ip->ino_data.gid);
886
887 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
888 ip->ino_data.uflags);
889 return (error);
427e5fc6
MD
890}
891
66325755
MD
892/*
893 * hammer_vop_advlock { vp, id, op, fl, flags }
b0aab9b9
MD
894 *
895 * MPSAFE - does not require fs_token
66325755 896 */
427e5fc6
MD
897static
898int
66325755 899hammer_vop_advlock(struct vop_advlock_args *ap)
427e5fc6 900{
4a2796f3 901 hammer_inode_t ip = VTOI(ap->a_vp);
66325755 902
11ad5ade 903 return (lf_advlock(ap, &ip->advlock, ip->ino_data.size));
427e5fc6
MD
904}
905
66325755
MD
906/*
907 * hammer_vop_close { vp, fflag }
6f3d87c0 908 *
b0aab9b9 909 * We can only sync-on-close for normal closes. XXX disabled for now.
66325755 910 */
427e5fc6
MD
911static
912int
66325755 913hammer_vop_close(struct vop_close_args *ap)
427e5fc6 914{
9192654c 915#if 0
6f3d87c0
MD
916 struct vnode *vp = ap->a_vp;
917 hammer_inode_t ip = VTOI(vp);
918 int waitfor;
6f3d87c0
MD
919 if (ip->flags & (HAMMER_INODE_CLOSESYNC|HAMMER_INODE_CLOSEASYNC)) {
920 if (vn_islocked(vp) == LK_EXCLUSIVE &&
921 (vp->v_flag & (VINACTIVE|VRECLAIMED)) == 0) {
922 if (ip->flags & HAMMER_INODE_CLOSESYNC)
923 waitfor = MNT_WAIT;
924 else
925 waitfor = MNT_NOWAIT;
926 ip->flags &= ~(HAMMER_INODE_CLOSESYNC |
927 HAMMER_INODE_CLOSEASYNC);
928 VOP_FSYNC(vp, MNT_NOWAIT, waitfor);
929 }
930 }
9192654c 931#endif
a89aec1b 932 return (vop_stdclose(ap));
427e5fc6
MD
933}
934
66325755
MD
935/*
936 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
937 *
938 * The operating system has already ensured that the directory entry
939 * does not exist and done all appropriate namespace locking.
940 */
427e5fc6
MD
941static
942int
66325755 943hammer_vop_ncreate(struct vop_ncreate_args *ap)
427e5fc6 944{
66325755
MD
945 struct hammer_transaction trans;
946 struct hammer_inode *dip;
947 struct hammer_inode *nip;
948 struct nchandle *nch;
b0aab9b9 949 hammer_mount_t hmp;
66325755
MD
950 int error;
951
952 nch = ap->a_nch;
953 dip = VTOI(ap->a_dvp);
b0aab9b9 954 hmp = dip->hmp;
66325755 955
d113fda1
MD
956 if (dip->flags & HAMMER_INODE_RO)
957 return (EROFS);
b0aab9b9 958 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
e63644f0 959 return (error);
d113fda1 960
66325755
MD
961 /*
962 * Create a transaction to cover the operations we perform.
963 */
b0aab9b9
MD
964 lwkt_gettoken(&hmp->fs_token);
965 hammer_start_transaction(&trans, hmp);
ce0138a6 966 ++hammer_stats_file_iopsw;
66325755
MD
967
968 /*
969 * Create a new filesystem object of the requested type. The
b84de5af
MD
970 * returned inode will be referenced and shared-locked to prevent
971 * it from being moved to the flusher.
66325755 972 */
5a930e66 973 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
5a64efa1
MD
974 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
975 NULL, &nip);
66325755 976 if (error) {
77062c8a 977 hkprintf("hammer_create_inode error %d\n", error);
b84de5af 978 hammer_done_transaction(&trans);
66325755 979 *ap->a_vpp = NULL;
b0aab9b9 980 lwkt_reltoken(&hmp->fs_token);
66325755
MD
981 return (error);
982 }
66325755
MD
983
984 /*
985 * Add the new filesystem object to the directory. This will also
986 * bump the inode's link count.
987 */
5a930e66
MD
988 error = hammer_ip_add_directory(&trans, dip,
989 nch->ncp->nc_name, nch->ncp->nc_nlen,
990 nip);
0b075555 991 if (error)
77062c8a 992 hkprintf("hammer_ip_add_directory error %d\n", error);
66325755
MD
993
994 /*
995 * Finish up.
996 */
997 if (error) {
a89aec1b 998 hammer_rel_inode(nip, 0);
b84de5af 999 hammer_done_transaction(&trans);
66325755
MD
1000 *ap->a_vpp = NULL;
1001 } else {
e8599db1 1002 error = hammer_get_vnode(nip, ap->a_vpp);
b84de5af 1003 hammer_done_transaction(&trans);
a89aec1b
MD
1004 hammer_rel_inode(nip, 0);
1005 if (error == 0) {
1006 cache_setunresolved(ap->a_nch);
1007 cache_setvp(ap->a_nch, *ap->a_vpp);
1008 }
fbb84158 1009 hammer_knote(ap->a_dvp, NOTE_WRITE);
66325755 1010 }
b0aab9b9 1011 lwkt_reltoken(&hmp->fs_token);
66325755 1012 return (error);
427e5fc6
MD
1013}
1014
66325755
MD
1015/*
1016 * hammer_vop_getattr { vp, vap }
98f7132d
MD
1017 *
1018 * Retrieve an inode's attribute information. When accessing inodes
1019 * historically we fake the atime field to ensure consistent results.
1020 * The atime field is stored in the B-Tree element and allowed to be
1021 * updated without cycling the element.
899eb297 1022 *
b0aab9b9 1023 * MPSAFE - does not require fs_token
66325755 1024 */
427e5fc6
MD
1025static
1026int
66325755 1027hammer_vop_getattr(struct vop_getattr_args *ap)
427e5fc6 1028{
66325755
MD
1029 struct hammer_inode *ip = VTOI(ap->a_vp);
1030 struct vattr *vap = ap->a_vap;
1031
a56cb012
MD
1032 /*
1033 * We want the fsid to be different when accessing a filesystem
1034 * with different as-of's so programs like diff don't think
1035 * the files are the same.
1036 *
1037 * We also want the fsid to be the same when comparing snapshots,
1038 * or when comparing mirrors (which might be backed by different
1039 * physical devices). HAMMER fsids are based on the PFS's
1040 * shared_uuid field.
1041 *
1042 * XXX there is a chance of collision here. The va_fsid reported
1043 * by stat is different from the more involved fsid used in the
1044 * mount structure.
c82af904 1045 */
ce0138a6 1046 ++hammer_stats_file_iopsr;
899eb297 1047 hammer_lock_sh(&ip->lock);
a56cb012
MD
1048 vap->va_fsid = ip->pfsm->fsid_udev ^ (u_int32_t)ip->obj_asof ^
1049 (u_int32_t)(ip->obj_asof >> 32);
1050
11ad5ade 1051 vap->va_fileid = ip->ino_leaf.base.obj_id;
66325755 1052 vap->va_mode = ip->ino_data.mode;
11ad5ade 1053 vap->va_nlink = ip->ino_data.nlinks;
66325755
MD
1054 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1055 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1056 vap->va_rmajor = 0;
1057 vap->va_rminor = 0;
11ad5ade 1058 vap->va_size = ip->ino_data.size;
bcac4bbb
MD
1059
1060 /*
f437a2ab
MD
1061 * Special case for @@PFS softlinks. The actual size of the
1062 * expanded softlink is "@@0x%016llx:%05d" == 26 bytes.
cb3c760c 1063 * or for MAX_TID is "@@-1:%05d" == 10 bytes.
f437a2ab
MD
1064 */
1065 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_SOFTLINK &&
1066 ip->ino_data.size == 10 &&
1067 ip->obj_asof == HAMMER_MAX_TID &&
1068 ip->obj_localization == 0 &&
1069 strncmp(ip->ino_data.ext.symlink, "@@PFS", 5) == 0) {
cb3c760c
MD
1070 if (ip->pfsm->pfsd.mirror_flags & HAMMER_PFSD_SLAVE)
1071 vap->va_size = 26;
1072 else
1073 vap->va_size = 10;
f437a2ab
MD
1074 }
1075
1076 /*
bcac4bbb
MD
1077 * We must provide a consistent atime and mtime for snapshots
1078 * so people can do a 'tar cf - ... | md5' on them and get
1079 * consistent results.
1080 */
1081 if (ip->flags & HAMMER_INODE_RO) {
ddfdf542
MD
1082 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
1083 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
bcac4bbb 1084 } else {
ddfdf542
MD
1085 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
1086 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
bcac4bbb 1087 }
ddfdf542 1088 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
66325755
MD
1089 vap->va_flags = ip->ino_data.uflags;
1090 vap->va_gen = 1; /* hammer inums are unique for all time */
bf686dbe 1091 vap->va_blocksize = HAMMER_BUFSIZE;
4a2796f3
MD
1092 if (ip->ino_data.size >= HAMMER_XDEMARC) {
1093 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
1094 ~HAMMER_XBUFMASK64;
1095 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
1096 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
1097 ~HAMMER_BUFMASK64;
1098 } else {
1099 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
1100 }
64950f31 1101
11ad5ade 1102 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
66325755 1103 vap->va_filerev = 0; /* XXX */
66325755
MD
1104 vap->va_uid_uuid = ip->ino_data.uid;
1105 vap->va_gid_uuid = ip->ino_data.gid;
1106 vap->va_fsid_uuid = ip->hmp->fsid;
1107 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
1108 VA_FSID_UUID_VALID;
7a04d74f 1109
11ad5ade 1110 switch (ip->ino_data.obj_type) {
7a04d74f
MD
1111 case HAMMER_OBJTYPE_CDEV:
1112 case HAMMER_OBJTYPE_BDEV:
1113 vap->va_rmajor = ip->ino_data.rmajor;
1114 vap->va_rminor = ip->ino_data.rminor;
1115 break;
1116 default:
1117 break;
1118 }
899eb297 1119 hammer_unlock(&ip->lock);
66325755 1120 return(0);
427e5fc6
MD
1121}
1122
66325755
MD
1123/*
1124 * hammer_vop_nresolve { nch, dvp, cred }
1125 *
1126 * Locate the requested directory entry.
1127 */
427e5fc6
MD
1128static
1129int
66325755 1130hammer_vop_nresolve(struct vop_nresolve_args *ap)
427e5fc6 1131{
36f82b23 1132 struct hammer_transaction trans;
66325755 1133 struct namecache *ncp;
b0aab9b9 1134 hammer_mount_t hmp;
7f7c1f84
MD
1135 hammer_inode_t dip;
1136 hammer_inode_t ip;
1137 hammer_tid_t asof;
8cd0a023 1138 struct hammer_cursor cursor;
66325755
MD
1139 struct vnode *vp;
1140 int64_t namekey;
1141 int error;
7f7c1f84
MD
1142 int i;
1143 int nlen;
d113fda1 1144 int flags;
a56cb012 1145 int ispfs;
adf01747 1146 int64_t obj_id;
ddfdf542 1147 u_int32_t localization;
5e435c92 1148 u_int32_t max_iterations;
7f7c1f84
MD
1149
1150 /*
1151 * Misc initialization, plus handle as-of name extensions. Look for
1152 * the '@@' extension. Note that as-of files and directories cannot
1153 * be modified.
7f7c1f84
MD
1154 */
1155 dip = VTOI(ap->a_dvp);
1156 ncp = ap->a_nch->ncp;
1157 asof = dip->obj_asof;
bc6c1f13 1158 localization = dip->obj_localization; /* for code consistency */
7f7c1f84 1159 nlen = ncp->nc_nlen;
ea434b6f 1160 flags = dip->flags & HAMMER_INODE_RO;
a56cb012 1161 ispfs = 0;
b0aab9b9 1162 hmp = dip->hmp;
7f7c1f84 1163
b0aab9b9
MD
1164 lwkt_gettoken(&hmp->fs_token);
1165 hammer_simple_transaction(&trans, hmp);
ce0138a6 1166 ++hammer_stats_file_iopsr;
36f82b23 1167
7f7c1f84
MD
1168 for (i = 0; i < nlen; ++i) {
1169 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
bc6c1f13
MD
1170 error = hammer_str_to_tid(ncp->nc_name + i + 2,
1171 &ispfs, &asof, &localization);
1172 if (error != 0) {
1173 i = nlen;
1174 break;
1175 }
ea434b6f
MD
1176 if (asof != HAMMER_MAX_TID)
1177 flags |= HAMMER_INODE_RO;
7f7c1f84
MD
1178 break;
1179 }
1180 }
1181 nlen = i;
66325755 1182
8cd0a023 1183 /*
ea434b6f
MD
1184 * If this is a PFS softlink we dive into the PFS
1185 */
1186 if (ispfs && nlen == 0) {
1187 ip = hammer_get_inode(&trans, dip, HAMMER_OBJID_ROOT,
1188 asof, localization,
1189 flags, &error);
1190 if (error == 0) {
1191 error = hammer_get_vnode(ip, &vp);
1192 hammer_rel_inode(ip, 0);
1193 } else {
1194 vp = NULL;
1195 }
1196 if (error == 0) {
1197 vn_unlock(vp);
1198 cache_setvp(ap->a_nch, vp);
1199 vrele(vp);
1200 }
1201 goto done;
1202 }
1203
1204 /*
294aec9f
MD
1205 * If there is no path component the time extension is relative to dip.
1206 * e.g. "fubar/@@<snapshot>"
1207 *
1208 * "." is handled by the kernel, but ".@@<snapshot>" is not.
1209 * e.g. "fubar/.@@<snapshot>"
1210 *
1211 * ".." is handled by the kernel. We do not currently handle
1212 * "..@<snapshot>".
d113fda1 1213 */
294aec9f 1214 if (nlen == 0 || (nlen == 1 && ncp->nc_name[0] == '.')) {
bcac4bbb 1215 ip = hammer_get_inode(&trans, dip, dip->obj_id,
ddfdf542
MD
1216 asof, dip->obj_localization,
1217 flags, &error);
d113fda1 1218 if (error == 0) {
e8599db1 1219 error = hammer_get_vnode(ip, &vp);
d113fda1
MD
1220 hammer_rel_inode(ip, 0);
1221 } else {
1222 vp = NULL;
1223 }
1224 if (error == 0) {
1225 vn_unlock(vp);
1226 cache_setvp(ap->a_nch, vp);
1227 vrele(vp);
1228 }
36f82b23 1229 goto done;
d113fda1
MD
1230 }
1231
1232 /*
8cd0a023
MD
1233 * Calculate the namekey and setup the key range for the scan. This
1234 * works kinda like a chained hash table where the lower 32 bits
1235 * of the namekey synthesize the chain.
1236 *
1237 * The key range is inclusive of both key_beg and key_end.
1238 */
5e435c92
MD
1239 namekey = hammer_directory_namekey(dip, ncp->nc_name, nlen,
1240 &max_iterations);
66325755 1241
bcac4bbb 1242 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
5a930e66 1243 cursor.key_beg.localization = dip->obj_localization +
beec5dc4 1244 hammer_dir_localization(dip);
8cd0a023
MD
1245 cursor.key_beg.obj_id = dip->obj_id;
1246 cursor.key_beg.key = namekey;
d5530d22 1247 cursor.key_beg.create_tid = 0;
8cd0a023
MD
1248 cursor.key_beg.delete_tid = 0;
1249 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1250 cursor.key_beg.obj_type = 0;
66325755 1251
8cd0a023 1252 cursor.key_end = cursor.key_beg;
5e435c92 1253 cursor.key_end.key += max_iterations;
d5530d22
MD
1254 cursor.asof = asof;
1255 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
66325755
MD
1256
1257 /*
8cd0a023 1258 * Scan all matching records (the chain), locate the one matching
a89aec1b 1259 * the requested path component.
8cd0a023
MD
1260 *
1261 * The hammer_ip_*() functions merge in-memory records with on-disk
1262 * records for the purposes of the search.
66325755 1263 */
6a37e7e4 1264 obj_id = 0;
43c665ae 1265 localization = HAMMER_DEF_LOCALIZATION;
6a37e7e4 1266
4e17f465 1267 if (error == 0) {
4e17f465
MD
1268 error = hammer_ip_first(&cursor);
1269 while (error == 0) {
1270 error = hammer_ip_resolve_data(&cursor);
1271 if (error)
1272 break;
11ad5ade
MD
1273 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
1274 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
1275 obj_id = cursor.data->entry.obj_id;
ddfdf542 1276 localization = cursor.data->entry.localization;
4e17f465
MD
1277 break;
1278 }
1279 error = hammer_ip_next(&cursor);
66325755
MD
1280 }
1281 }
6a37e7e4 1282 hammer_done_cursor(&cursor);
4c286c36
MD
1283
1284 /*
1285 * Lookup the obj_id. This should always succeed. If it does not
1286 * the filesystem may be damaged and we return a dummy inode.
1287 */
66325755 1288 if (error == 0) {
bcac4bbb 1289 ip = hammer_get_inode(&trans, dip, obj_id,
ddfdf542
MD
1290 asof, localization,
1291 flags, &error);
4c286c36
MD
1292 if (error == ENOENT) {
1293 kprintf("HAMMER: WARNING: Missing "
1294 "inode for dirent \"%s\"\n"
3d30bff3
MD
1295 "\tobj_id = %016llx, asof=%016llx, lo=%08x\n",
1296 ncp->nc_name,
1297 (long long)obj_id, (long long)asof,
1298 localization);
4c286c36
MD
1299 error = 0;
1300 ip = hammer_get_dummy_inode(&trans, dip, obj_id,
1301 asof, localization,
1302 flags, &error);
1303 }
7f7c1f84 1304 if (error == 0) {
e8599db1 1305 error = hammer_get_vnode(ip, &vp);
7f7c1f84
MD
1306 hammer_rel_inode(ip, 0);
1307 } else {
1308 vp = NULL;
1309 }
66325755
MD
1310 if (error == 0) {
1311 vn_unlock(vp);
1312 cache_setvp(ap->a_nch, vp);
1313 vrele(vp);
1314 }
1315 } else if (error == ENOENT) {
1316 cache_setvp(ap->a_nch, NULL);
1317 }
36f82b23 1318done:
b84de5af 1319 hammer_done_transaction(&trans);
b0aab9b9 1320 lwkt_reltoken(&hmp->fs_token);
66325755 1321 return (error);
427e5fc6
MD
1322}
1323
66325755
MD
1324/*
1325 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
1326 *
1327 * Locate the parent directory of a directory vnode.
1328 *
1329 * dvp is referenced but not locked. *vpp must be returned referenced and
1330 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
1331 * at the root, instead it could indicate that the directory we were in was
1332 * removed.
42c7d26b
MD
1333 *
1334 * NOTE: as-of sequences are not linked into the directory structure. If
1335 * we are at the root with a different asof then the mount point, reload
1336 * the same directory with the mount point's asof. I'm not sure what this
1337 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
1338 * get confused, but it hasn't been tested.
66325755 1339 */
427e5fc6
MD
1340static
1341int
66325755 1342hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
427e5fc6 1343{
36f82b23 1344 struct hammer_transaction trans;
66325755 1345 struct hammer_inode *dip;
d113fda1 1346 struct hammer_inode *ip;
b0aab9b9 1347 hammer_mount_t hmp;
42c7d26b 1348 int64_t parent_obj_id;
5a930e66 1349 u_int32_t parent_obj_localization;
42c7d26b 1350 hammer_tid_t asof;
d113fda1 1351 int error;
66325755
MD
1352
1353 dip = VTOI(ap->a_dvp);
42c7d26b 1354 asof = dip->obj_asof;
b0aab9b9 1355 hmp = dip->hmp;
5a930e66
MD
1356
1357 /*
1358 * Whos are parent? This could be the root of a pseudo-filesystem
1359 * whos parent is in another localization domain.
1360 */
b0aab9b9 1361 lwkt_gettoken(&hmp->fs_token);
42c7d26b 1362 parent_obj_id = dip->ino_data.parent_obj_id;
5a930e66
MD
1363 if (dip->obj_id == HAMMER_OBJID_ROOT)
1364 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
1365 else
1366 parent_obj_localization = dip->obj_localization;
42c7d26b
MD
1367
1368 if (parent_obj_id == 0) {
1369 if (dip->obj_id == HAMMER_OBJID_ROOT &&
b0aab9b9 1370 asof != hmp->asof) {
42c7d26b 1371 parent_obj_id = dip->obj_id;
b0aab9b9 1372 asof = hmp->asof;
42c7d26b
MD
1373 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
1374 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
973c11b9 1375 (long long)dip->obj_asof);
42c7d26b
MD
1376 } else {
1377 *ap->a_vpp = NULL;
b0aab9b9 1378 lwkt_reltoken(&hmp->fs_token);
42c7d26b
MD
1379 return ENOENT;
1380 }
66325755 1381 }
d113fda1 1382
b0aab9b9 1383 hammer_simple_transaction(&trans, hmp);
ce0138a6 1384 ++hammer_stats_file_iopsr;
36f82b23 1385
bcac4bbb 1386 ip = hammer_get_inode(&trans, dip, parent_obj_id,
5a930e66 1387 asof, parent_obj_localization,
ddfdf542 1388 dip->flags, &error);
36f82b23 1389 if (ip) {
e8599db1 1390 error = hammer_get_vnode(ip, ap->a_vpp);
36f82b23
MD
1391 hammer_rel_inode(ip, 0);
1392 } else {
d113fda1 1393 *ap->a_vpp = NULL;
d113fda1 1394 }
b84de5af 1395 hammer_done_transaction(&trans);
b0aab9b9 1396 lwkt_reltoken(&hmp->fs_token);
d113fda1 1397 return (error);
427e5fc6
MD
1398}
1399
66325755
MD
1400/*
1401 * hammer_vop_nlink { nch, dvp, vp, cred }
1402 */
427e5fc6
MD
1403static
1404int
66325755 1405hammer_vop_nlink(struct vop_nlink_args *ap)
427e5fc6 1406{
66325755
MD
1407 struct hammer_transaction trans;
1408 struct hammer_inode *dip;
1409 struct hammer_inode *ip;
1410 struct nchandle *nch;
b0aab9b9 1411 hammer_mount_t hmp;
66325755
MD
1412 int error;
1413
f437a2ab
MD
1414 if (ap->a_dvp->v_mount != ap->a_vp->v_mount)
1415 return(EXDEV);
1416
66325755
MD
1417 nch = ap->a_nch;
1418 dip = VTOI(ap->a_dvp);
1419 ip = VTOI(ap->a_vp);
b0aab9b9 1420 hmp = dip->hmp;
66325755 1421
f437a2ab
MD
1422 if (dip->obj_localization != ip->obj_localization)
1423 return(EXDEV);
1424
d113fda1
MD
1425 if (dip->flags & HAMMER_INODE_RO)
1426 return (EROFS);
1427 if (ip->flags & HAMMER_INODE_RO)
1428 return (EROFS);
b0aab9b9 1429 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
e63644f0 1430 return (error);
d113fda1 1431
66325755
MD
1432 /*
1433 * Create a transaction to cover the operations we perform.
1434 */
b0aab9b9
MD
1435 lwkt_gettoken(&hmp->fs_token);
1436 hammer_start_transaction(&trans, hmp);
ce0138a6 1437 ++hammer_stats_file_iopsw;
66325755
MD
1438
1439 /*
1440 * Add the filesystem object to the directory. Note that neither
1441 * dip nor ip are referenced or locked, but their vnodes are
1442 * referenced. This function will bump the inode's link count.
1443 */
5a930e66
MD
1444 error = hammer_ip_add_directory(&trans, dip,
1445 nch->ncp->nc_name, nch->ncp->nc_nlen,
1446 ip);
66325755
MD
1447
1448 /*
1449 * Finish up.
1450 */
b84de5af 1451 if (error == 0) {
6b4f890b
MD
1452 cache_setunresolved(nch);
1453 cache_setvp(nch, ap->a_vp);
66325755 1454 }
b84de5af 1455 hammer_done_transaction(&trans);
fbb84158
MD
1456 hammer_knote(ap->a_vp, NOTE_LINK);
1457 hammer_knote(ap->a_dvp, NOTE_WRITE);
b0aab9b9 1458 lwkt_reltoken(&hmp->fs_token);
66325755 1459 return (error);
427e5fc6
MD
1460}
1461
66325755
MD
1462/*
1463 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1464 *
1465 * The operating system has already ensured that the directory entry
1466 * does not exist and done all appropriate namespace locking.
1467 */
427e5fc6
MD
1468static
1469int
66325755 1470hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
427e5fc6 1471{
66325755
MD
1472 struct hammer_transaction trans;
1473 struct hammer_inode *dip;
1474 struct hammer_inode *nip;
1475 struct nchandle *nch;
b0aab9b9 1476 hammer_mount_t hmp;
66325755
MD
1477 int error;
1478
1479 nch = ap->a_nch;
1480 dip = VTOI(ap->a_dvp);
b0aab9b9 1481 hmp = dip->hmp;
66325755 1482
d113fda1
MD
1483 if (dip->flags & HAMMER_INODE_RO)
1484 return (EROFS);
b0aab9b9 1485 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
e63644f0 1486 return (error);
d113fda1 1487
66325755
MD
1488 /*
1489 * Create a transaction to cover the operations we perform.
1490 */
b0aab9b9
MD
1491 lwkt_gettoken(&hmp->fs_token);
1492 hammer_start_transaction(&trans, hmp);
ce0138a6 1493 ++hammer_stats_file_iopsw;
66325755
MD
1494
1495 /*
1496 * Create a new filesystem object of the requested type. The
8cd0a023 1497 * returned inode will be referenced but not locked.
66325755 1498 */
5a930e66 1499 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
5a64efa1
MD
1500 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1501 NULL, &nip);
66325755 1502 if (error) {
77062c8a 1503 hkprintf("hammer_mkdir error %d\n", error);
b84de5af 1504 hammer_done_transaction(&trans);
66325755 1505 *ap->a_vpp = NULL;
b0aab9b9 1506 lwkt_reltoken(&hmp->fs_token);
66325755
MD
1507 return (error);
1508 }
66325755
MD
1509 /*
1510 * Add the new filesystem object to the directory. This will also
1511 * bump the inode's link count.
1512 */
5a930e66
MD
1513 error = hammer_ip_add_directory(&trans, dip,
1514 nch->ncp->nc_name, nch->ncp->nc_nlen,
1515 nip);
0b075555 1516 if (error)
77062c8a 1517 hkprintf("hammer_mkdir (add) error %d\n", error);
66325755
MD
1518
1519 /*
1520 * Finish up.
1521 */
1522 if (error) {
a89aec1b 1523 hammer_rel_inode(nip, 0);
66325755
MD
1524 *ap->a_vpp = NULL;
1525 } else {
e8599db1 1526 error = hammer_get_vnode(nip, ap->a_vpp);
a89aec1b
MD
1527 hammer_rel_inode(nip, 0);
1528 if (error == 0) {
1529 cache_setunresolved(ap->a_nch);
1530 cache_setvp(ap->a_nch, *ap->a_vpp);
1531 }
66325755 1532 }
b84de5af 1533 hammer_done_transaction(&trans);
fbb84158
MD
1534 if (error == 0)
1535 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
b0aab9b9 1536 lwkt_reltoken(&hmp->fs_token);
66325755 1537 return (error);
427e5fc6
MD
1538}
1539
66325755
MD
1540/*
1541 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1542 *
1543 * The operating system has already ensured that the directory entry
1544 * does not exist and done all appropriate namespace locking.
1545 */
427e5fc6
MD
1546static
1547int
66325755 1548hammer_vop_nmknod(struct vop_nmknod_args *ap)
427e5fc6 1549{
66325755
MD
1550 struct hammer_transaction trans;
1551 struct hammer_inode *dip;
1552 struct hammer_inode *nip;
1553 struct nchandle *nch;
b0aab9b9 1554 hammer_mount_t hmp;
66325755
MD
1555 int error;
1556
1557 nch = ap->a_nch;
1558 dip = VTOI(ap->a_dvp);
b0aab9b9 1559 hmp = dip->hmp;
66325755 1560
d113fda1
MD
1561 if (dip->flags & HAMMER_INODE_RO)
1562 return (EROFS);
b0aab9b9 1563 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
e63644f0 1564 return (error);
d113fda1 1565
66325755
MD
1566 /*
1567 * Create a transaction to cover the operations we perform.
1568 */
b0aab9b9
MD
1569 lwkt_gettoken(&hmp->fs_token);
1570 hammer_start_transaction(&trans, hmp);
ce0138a6 1571 ++hammer_stats_file_iopsw;
66325755
MD
1572
1573 /*
1574 * Create a new filesystem object of the requested type. The
8cd0a023 1575 * returned inode will be referenced but not locked.
5a930e66
MD
1576 *
1577 * If mknod specifies a directory a pseudo-fs is created.
66325755 1578 */
5a930e66 1579 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
5a64efa1
MD
1580 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
1581 NULL, &nip);
66325755 1582 if (error) {
b84de5af 1583 hammer_done_transaction(&trans);
66325755 1584 *ap->a_vpp = NULL;
b0aab9b9 1585 lwkt_reltoken(&hmp->fs_token);
66325755
MD
1586 return (error);
1587 }
66325755
MD
1588
1589 /*
1590 * Add the new filesystem object to the directory. This will also
1591 * bump the inode's link count.
1592 */
5a930e66
MD
1593 error = hammer_ip_add_directory(&trans, dip,
1594 nch->ncp->nc_name, nch->ncp->nc_nlen,
1595 nip);
66325755
MD
1596
1597 /*
1598 * Finish up.
1599 */
1600 if (error) {
a89aec1b 1601 hammer_rel_inode(nip, 0);
66325755
MD
1602 *ap->a_vpp = NULL;
1603 } else {
e8599db1 1604 error = hammer_get_vnode(nip, ap->a_vpp);
a89aec1b
MD
1605 hammer_rel_inode(nip, 0);
1606 if (error == 0) {
1607 cache_setunresolved(ap->a_nch);
1608 cache_setvp(ap->a_nch, *ap->a_vpp);
1609 }
66325755 1610 }
b84de5af 1611 hammer_done_transaction(&trans);
fbb84158
MD
1612 if (error == 0)
1613 hammer_knote(ap->a_dvp, NOTE_WRITE);
b0aab9b9 1614 lwkt_reltoken(&hmp->fs_token);
66325755 1615 return (error);
427e5fc6
MD
1616}
1617
66325755
MD
1618/*
1619 * hammer_vop_open { vp, mode, cred, fp }
b0aab9b9
MD
1620 *
1621 * MPSAFE (does not require fs_token)
66325755 1622 */
427e5fc6
MD
1623static
1624int
66325755 1625hammer_vop_open(struct vop_open_args *ap)
427e5fc6 1626{
9f5097dc
MD
1627 hammer_inode_t ip;
1628
ce0138a6 1629 ++hammer_stats_file_iopsr;
9f5097dc
MD
1630 ip = VTOI(ap->a_vp);
1631
1632 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
d113fda1 1633 return (EROFS);
a89aec1b 1634 return(vop_stdopen(ap));
427e5fc6
MD
1635}
1636
66325755 1637/*
66325755
MD
1638 * hammer_vop_print { vp }
1639 */
427e5fc6
MD
1640static
1641int
66325755 1642hammer_vop_print(struct vop_print_args *ap)
427e5fc6
MD
1643{
1644 return EOPNOTSUPP;
1645}
1646
66325755 1647/*
6b4f890b 1648 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
66325755 1649 */
427e5fc6
MD
1650static
1651int
66325755 1652hammer_vop_readdir(struct vop_readdir_args *ap)
427e5fc6 1653{
36f82b23 1654 struct hammer_transaction trans;
6b4f890b
MD
1655 struct hammer_cursor cursor;
1656 struct hammer_inode *ip;
b0aab9b9 1657 hammer_mount_t hmp;
6b4f890b 1658 struct uio *uio;
6b4f890b
MD
1659 hammer_base_elm_t base;
1660 int error;
1661 int cookie_index;
1662 int ncookies;
1663 off_t *cookies;
1664 off_t saveoff;
1665 int r;
ea434b6f 1666 int dtype;
6b4f890b 1667
ce0138a6 1668 ++hammer_stats_file_iopsr;
6b4f890b
MD
1669 ip = VTOI(ap->a_vp);
1670 uio = ap->a_uio;
b3deaf57 1671 saveoff = uio->uio_offset;
b0aab9b9 1672 hmp = ip->hmp;
b3deaf57
MD
1673
1674 if (ap->a_ncookies) {
1675 ncookies = uio->uio_resid / 16 + 1;
1676 if (ncookies > 1024)
1677 ncookies = 1024;
1678 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1679 cookie_index = 0;
1680 } else {
1681 ncookies = -1;
1682 cookies = NULL;
1683 cookie_index = 0;
1684 }
1685
b0aab9b9
MD
1686 lwkt_gettoken(&hmp->fs_token);
1687 hammer_simple_transaction(&trans, hmp);
36f82b23 1688
b3deaf57
MD
1689 /*
1690 * Handle artificial entries
4c286c36
MD
1691 *
1692 * It should be noted that the minimum value for a directory
1693 * hash key on-media is 0x0000000100000000, so we can use anything
1694 * less then that to represent our 'special' key space.
b3deaf57
MD
1695 */
1696 error = 0;
1697 if (saveoff == 0) {
1698 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1699 if (r)
1700 goto done;
1701 if (cookies)
1702 cookies[cookie_index] = saveoff;
1703 ++saveoff;
1704 ++cookie_index;
1705 if (cookie_index == ncookies)
1706 goto done;
1707 }
1708 if (saveoff == 1) {
1709 if (ip->ino_data.parent_obj_id) {
1710 r = vop_write_dirent(&error, uio,
1711 ip->ino_data.parent_obj_id,
1712 DT_DIR, 2, "..");
1713 } else {
1714 r = vop_write_dirent(&error, uio,
1715 ip->obj_id, DT_DIR, 2, "..");
1716 }
1717 if (r)
1718 goto done;
1719 if (cookies)
1720 cookies[cookie_index] = saveoff;
1721 ++saveoff;
1722 ++cookie_index;
1723 if (cookie_index == ncookies)
1724 goto done;
1725 }
6b4f890b
MD
1726
1727 /*
1728 * Key range (begin and end inclusive) to scan. Directory keys
1729 * directly translate to a 64 bit 'seek' position.
1730 */
bcac4bbb 1731 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
5a930e66 1732 cursor.key_beg.localization = ip->obj_localization +
beec5dc4 1733 hammer_dir_localization(ip);
6b4f890b 1734 cursor.key_beg.obj_id = ip->obj_id;
d5530d22 1735 cursor.key_beg.create_tid = 0;
6b4f890b
MD
1736 cursor.key_beg.delete_tid = 0;
1737 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1738 cursor.key_beg.obj_type = 0;
b3deaf57 1739 cursor.key_beg.key = saveoff;
6b4f890b
MD
1740
1741 cursor.key_end = cursor.key_beg;
1742 cursor.key_end.key = HAMMER_MAX_KEY;
d5530d22
MD
1743 cursor.asof = ip->obj_asof;
1744 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
6b4f890b 1745
4e17f465 1746 error = hammer_ip_first(&cursor);
6b4f890b
MD
1747
1748 while (error == 0) {
11ad5ade 1749 error = hammer_ip_resolve_data(&cursor);
6b4f890b
MD
1750 if (error)
1751 break;
11ad5ade 1752 base = &cursor.leaf->base;
6b4f890b 1753 saveoff = base->key;
11ad5ade 1754 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
6b4f890b 1755
7a04d74f
MD
1756 if (base->obj_id != ip->obj_id)
1757 panic("readdir: bad record at %p", cursor.node);
1758
ea434b6f
MD
1759 /*
1760 * Convert pseudo-filesystems into softlinks
1761 */
1762 dtype = hammer_get_dtype(cursor.leaf->base.obj_type);
6b4f890b 1763 r = vop_write_dirent(
11ad5ade 1764 &error, uio, cursor.data->entry.obj_id,
ea434b6f 1765 dtype,
11ad5ade
MD
1766 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1767 (void *)cursor.data->entry.name);
6b4f890b
MD
1768 if (r)
1769 break;
1770 ++saveoff;
1771 if (cookies)
1772 cookies[cookie_index] = base->key;
1773 ++cookie_index;
1774 if (cookie_index == ncookies)
1775 break;
1776 error = hammer_ip_next(&cursor);
1777 }
1778 hammer_done_cursor(&cursor);
1779
b3deaf57 1780done:
b84de5af 1781 hammer_done_transaction(&trans);
36f82b23 1782
6b4f890b
MD
1783 if (ap->a_eofflag)
1784 *ap->a_eofflag = (error == ENOENT);
6b4f890b
MD
1785 uio->uio_offset = saveoff;
1786 if (error && cookie_index == 0) {
b3deaf57
MD
1787 if (error == ENOENT)
1788 error = 0;
6b4f890b
MD
1789 if (cookies) {
1790 kfree(cookies, M_TEMP);
1791 *ap->a_ncookies = 0;
1792 *ap->a_cookies = NULL;
1793 }
1794 } else {
7a04d74f
MD
1795 if (error == ENOENT)
1796 error = 0;
6b4f890b
MD
1797 if (cookies) {
1798 *ap->a_ncookies = cookie_index;
1799 *ap->a_cookies = cookies;
1800 }
1801 }
b0aab9b9 1802 lwkt_reltoken(&hmp->fs_token);
6b4f890b 1803 return(error);
427e5fc6
MD
1804}
1805
66325755
MD
1806/*
1807 * hammer_vop_readlink { vp, uio, cred }
1808 */
427e5fc6
MD
1809static
1810int
66325755 1811hammer_vop_readlink(struct vop_readlink_args *ap)
427e5fc6 1812{
36f82b23 1813 struct hammer_transaction trans;
7a04d74f
MD
1814 struct hammer_cursor cursor;
1815 struct hammer_inode *ip;
b0aab9b9 1816 hammer_mount_t hmp;
ea434b6f
MD
1817 char buf[32];
1818 u_int32_t localization;
1819 hammer_pseudofs_inmem_t pfsm;
7a04d74f
MD
1820 int error;
1821
1822 ip = VTOI(ap->a_vp);
b0aab9b9
MD
1823 hmp = ip->hmp;
1824
1825 lwkt_gettoken(&hmp->fs_token);
36f82b23 1826
2f85fa4d
MD
1827 /*
1828 * Shortcut if the symlink data was stuffed into ino_data.
ea434b6f 1829 *
842e7a70
MD
1830 * Also expand special "@@PFS%05d" softlinks (expansion only
1831 * occurs for non-historical (current) accesses made from the
1832 * primary filesystem).
2f85fa4d
MD
1833 */
1834 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
ea434b6f
MD
1835 char *ptr;
1836 int bytes;
1837
1838 ptr = ip->ino_data.ext.symlink;
1839 bytes = (int)ip->ino_data.size;
842e7a70
MD
1840 if (bytes == 10 &&
1841 ip->obj_asof == HAMMER_MAX_TID &&
1842 ip->obj_localization == 0 &&
1843 strncmp(ptr, "@@PFS", 5) == 0) {
b0aab9b9 1844 hammer_simple_transaction(&trans, hmp);
ea434b6f
MD
1845 bcopy(ptr + 5, buf, 5);
1846 buf[5] = 0;
1847 localization = strtoul(buf, NULL, 10) << 16;
1848 pfsm = hammer_load_pseudofs(&trans, localization,
1849 &error);
1850 if (error == 0) {
4c038e17
MD
1851 if (pfsm->pfsd.mirror_flags &
1852 HAMMER_PFSD_SLAVE) {
cb3c760c 1853 /* vap->va_size == 26 */
4c038e17
MD
1854 ksnprintf(buf, sizeof(buf),
1855 "@@0x%016llx:%05d",
973c11b9 1856 (long long)pfsm->pfsd.sync_end_tid,
4c038e17
MD
1857 localization >> 16);
1858 } else {
cb3c760c
MD
1859 /* vap->va_size == 10 */
1860 ksnprintf(buf, sizeof(buf),
1861 "@@-1:%05d",
1862 localization >> 16);
1863#if 0
4c038e17
MD
1864 ksnprintf(buf, sizeof(buf),
1865 "@@0x%016llx:%05d",
973c11b9 1866 (long long)HAMMER_MAX_TID,
4c038e17 1867 localization >> 16);
cb3c760c 1868#endif
4c038e17 1869 }
ea434b6f
MD
1870 ptr = buf;
1871 bytes = strlen(buf);
1872 }
1873 if (pfsm)
b0aab9b9 1874 hammer_rel_pseudofs(hmp, pfsm);
ea434b6f
MD
1875 hammer_done_transaction(&trans);
1876 }
1877 error = uiomove(ptr, bytes, ap->a_uio);
b0aab9b9 1878 lwkt_reltoken(&hmp->fs_token);
2f85fa4d
MD
1879 return(error);
1880 }
36f82b23 1881
2f85fa4d
MD
1882 /*
1883 * Long version
1884 */
b0aab9b9 1885 hammer_simple_transaction(&trans, hmp);
ce0138a6 1886 ++hammer_stats_file_iopsr;
bcac4bbb 1887 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
7a04d74f
MD
1888
1889 /*
1890 * Key range (begin and end inclusive) to scan. Directory keys
1891 * directly translate to a 64 bit 'seek' position.
1892 */
5a930e66
MD
1893 cursor.key_beg.localization = ip->obj_localization +
1894 HAMMER_LOCALIZE_MISC;
7a04d74f 1895 cursor.key_beg.obj_id = ip->obj_id;
d5530d22 1896 cursor.key_beg.create_tid = 0;
7a04d74f
MD
1897 cursor.key_beg.delete_tid = 0;
1898 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1899 cursor.key_beg.obj_type = 0;
1900 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
d5530d22
MD
1901 cursor.asof = ip->obj_asof;
1902 cursor.flags |= HAMMER_CURSOR_ASOF;
7a04d74f 1903
45a014dc 1904 error = hammer_ip_lookup(&cursor);
7a04d74f
MD
1905 if (error == 0) {
1906 error = hammer_ip_resolve_data(&cursor);
1907 if (error == 0) {
11ad5ade
MD
1908 KKASSERT(cursor.leaf->data_len >=
1909 HAMMER_SYMLINK_NAME_OFF);
1910 error = uiomove(cursor.data->symlink.name,
1911 cursor.leaf->data_len -
1912 HAMMER_SYMLINK_NAME_OFF,
7a04d74f
MD
1913 ap->a_uio);
1914 }
1915 }
1916 hammer_done_cursor(&cursor);
b84de5af 1917 hammer_done_transaction(&trans);
b0aab9b9 1918 lwkt_reltoken(&hmp->fs_token);
7a04d74f 1919 return(error);
427e5fc6
MD
1920}
1921
66325755
MD
1922/*
1923 * hammer_vop_nremove { nch, dvp, cred }
1924 */
427e5fc6
MD
1925static
1926int
66325755 1927hammer_vop_nremove(struct vop_nremove_args *ap)
427e5fc6 1928{
b84de5af 1929 struct hammer_transaction trans;
e63644f0 1930 struct hammer_inode *dip;
b0aab9b9 1931 hammer_mount_t hmp;
b84de5af
MD
1932 int error;
1933
e63644f0 1934 dip = VTOI(ap->a_dvp);
b0aab9b9 1935 hmp = dip->hmp;
e63644f0
MD
1936
1937 if (hammer_nohistory(dip) == 0 &&
b0aab9b9 1938 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
e63644f0
MD
1939 return (error);
1940 }
1941
b0aab9b9
MD
1942 lwkt_gettoken(&hmp->fs_token);
1943 hammer_start_transaction(&trans, hmp);
ce0138a6 1944 ++hammer_stats_file_iopsw;
d7e278bb 1945 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 0);
b84de5af 1946 hammer_done_transaction(&trans);
fbb84158
MD
1947 if (error == 0)
1948 hammer_knote(ap->a_dvp, NOTE_WRITE);
b0aab9b9 1949 lwkt_reltoken(&hmp->fs_token);
b84de5af 1950 return (error);
427e5fc6
MD
1951}
1952
66325755
MD
1953/*
1954 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1955 */
427e5fc6
MD
1956static
1957int
66325755 1958hammer_vop_nrename(struct vop_nrename_args *ap)
427e5fc6 1959{
8cd0a023
MD
1960 struct hammer_transaction trans;
1961 struct namecache *fncp;
1962 struct namecache *tncp;
1963 struct hammer_inode *fdip;
1964 struct hammer_inode *tdip;
1965 struct hammer_inode *ip;
b0aab9b9 1966 hammer_mount_t hmp;
8cd0a023 1967 struct hammer_cursor cursor;
8cd0a023 1968 int64_t namekey;
5e435c92 1969 u_int32_t max_iterations;
11ad5ade 1970 int nlen, error;
8cd0a023 1971
f437a2ab
MD
1972 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1973 return(EXDEV);
1974 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1975 return(EXDEV);
1976
8cd0a023
MD
1977 fdip = VTOI(ap->a_fdvp);
1978 tdip = VTOI(ap->a_tdvp);
1979 fncp = ap->a_fnch->ncp;
1980 tncp = ap->a_tnch->ncp;
b3deaf57
MD
1981 ip = VTOI(fncp->nc_vp);
1982 KKASSERT(ip != NULL);
d113fda1 1983
b0aab9b9
MD
1984 hmp = ip->hmp;
1985
f437a2ab
MD
1986 if (fdip->obj_localization != tdip->obj_localization)
1987 return(EXDEV);
1988 if (fdip->obj_localization != ip->obj_localization)
1989 return(EXDEV);
1990
d113fda1
MD
1991 if (fdip->flags & HAMMER_INODE_RO)
1992 return (EROFS);
1993 if (tdip->flags & HAMMER_INODE_RO)
1994 return (EROFS);
1995 if (ip->flags & HAMMER_INODE_RO)
1996 return (EROFS);
b0aab9b9 1997 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
e63644f0 1998 return (error);
d113fda1 1999
b0aab9b9
MD
2000 lwkt_gettoken(&hmp->fs_token);
2001 hammer_start_transaction(&trans, hmp);
ce0138a6 2002 ++hammer_stats_file_iopsw;
8cd0a023
MD
2003
2004 /*
b3deaf57
MD
2005 * Remove tncp from the target directory and then link ip as
2006 * tncp. XXX pass trans to dounlink
42c7d26b
MD
2007 *
2008 * Force the inode sync-time to match the transaction so it is
2009 * in-sync with the creation of the target directory entry.
8cd0a023 2010 */
d7e278bb
MD
2011 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp,
2012 ap->a_cred, 0, -1);
42c7d26b 2013 if (error == 0 || error == ENOENT) {
5a930e66
MD
2014 error = hammer_ip_add_directory(&trans, tdip,
2015 tncp->nc_name, tncp->nc_nlen,
2016 ip);
42c7d26b
MD
2017 if (error == 0) {
2018 ip->ino_data.parent_obj_id = tdip->obj_id;
cc0758d0 2019 ip->ino_data.ctime = trans.time;
e98f1b96 2020 hammer_modify_inode(&trans, ip, HAMMER_INODE_DDIRTY);
42c7d26b
MD
2021 }
2022 }
b3deaf57
MD
2023 if (error)
2024 goto failed; /* XXX */
8cd0a023
MD
2025
2026 /*
2027 * Locate the record in the originating directory and remove it.
2028 *
2029 * Calculate the namekey and setup the key range for the scan. This
2030 * works kinda like a chained hash table where the lower 32 bits
2031 * of the namekey synthesize the chain.
2032 *
2033 * The key range is inclusive of both key_beg and key_end.
2034 */
5e435c92
MD
2035 namekey = hammer_directory_namekey(fdip, fncp->nc_name, fncp->nc_nlen,
2036 &max_iterations);
6a37e7e4 2037retry:
bcac4bbb 2038 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
5a930e66 2039 cursor.key_beg.localization = fdip->obj_localization +
beec5dc4 2040 hammer_dir_localization(fdip);
8cd0a023
MD
2041 cursor.key_beg.obj_id = fdip->obj_id;
2042 cursor.key_beg.key = namekey;
d5530d22 2043 cursor.key_beg.create_tid = 0;
8cd0a023
MD
2044 cursor.key_beg.delete_tid = 0;
2045 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2046 cursor.key_beg.obj_type = 0;
2047
2048 cursor.key_end = cursor.key_beg;
5e435c92 2049 cursor.key_end.key += max_iterations;
d5530d22
MD
2050 cursor.asof = fdip->obj_asof;
2051 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
8cd0a023
MD
2052
2053 /*
2054 * Scan all matching records (the chain), locate the one matching
a89aec1b 2055 * the requested path component.
8cd0a023
MD
2056 *
2057 * The hammer_ip_*() functions merge in-memory records with on-disk
2058 * records for the purposes of the search.
2059 */
4e17f465 2060 error = hammer_ip_first(&cursor);
a89aec1b 2061 while (error == 0) {
8cd0a023
MD
2062 if (hammer_ip_resolve_data(&cursor) != 0)
2063 break;
11ad5ade
MD
2064 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2065 KKASSERT(nlen > 0);
2066 if (fncp->nc_nlen == nlen &&
2067 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
8cd0a023
MD
2068 break;
2069 }
a89aec1b 2070 error = hammer_ip_next(&cursor);
8cd0a023 2071 }
8cd0a023
MD
2072
2073 /*
2074 * If all is ok we have to get the inode so we can adjust nlinks.
6a37e7e4
MD
2075 *
2076 * WARNING: hammer_ip_del_directory() may have to terminate the
2077 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
2078 * twice.
8cd0a023 2079 */
9944ae54 2080 if (error == 0)
6a37e7e4 2081 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
b84de5af
MD
2082
2083 /*
2084 * XXX A deadlock here will break rename's atomicy for the purposes
2085 * of crash recovery.
2086 */
2087 if (error == EDEADLK) {
b84de5af 2088 hammer_done_cursor(&cursor);
b84de5af
MD
2089 goto retry;
2090 }
2091
2092 /*
2093 * Cleanup and tell the kernel that the rename succeeded.
036ea0c3
MD
2094 *
2095 * NOTE: ip->vp, if non-NULL, cannot be directly referenced
2096 * without formally acquiring the vp since the vp might
2097 * have zero refs on it, or in the middle of a reclaim,
2098 * etc.
b84de5af 2099 */
c0ade690 2100 hammer_done_cursor(&cursor);
fbb84158 2101 if (error == 0) {
6a37e7e4 2102 cache_rename(ap->a_fnch, ap->a_tnch);
fbb84158
MD
2103 hammer_knote(ap->a_fdvp, NOTE_WRITE);
2104 hammer_knote(ap->a_tdvp, NOTE_WRITE);
036ea0c3
MD
2105 while (ip->vp) {
2106 struct vnode *vp;
2107
2108 error = hammer_get_vnode(ip, &vp);
2109 if (error == 0 && vp) {
2110 vn_unlock(vp);
2111 hammer_knote(ip->vp, NOTE_RENAME);
2112 vrele(vp);
2113 break;
2114 }
2115 kprintf("Debug: HAMMER ip/vp race2 avoided\n");
2116 }
fbb84158 2117 }
b84de5af 2118
b3deaf57 2119failed:
b84de5af 2120 hammer_done_transaction(&trans);
b0aab9b9 2121 lwkt_reltoken(&hmp->fs_token);
8cd0a023 2122 return (error);
427e5fc6
MD
2123}
2124
66325755
MD
2125/*
2126 * hammer_vop_nrmdir { nch, dvp, cred }
2127 */
427e5fc6
MD
2128static
2129int
66325755 2130hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
427e5fc6 2131{
b84de5af 2132 struct hammer_transaction trans;
e63644f0 2133 struct hammer_inode *dip;
b0aab9b9 2134 hammer_mount_t hmp;
b84de5af
MD
2135 int error;
2136
e63644f0 2137 dip = VTOI(ap->a_dvp);
b0aab9b9 2138 hmp = dip->hmp;
e63644f0
MD
2139
2140 if (hammer_nohistory(dip) == 0 &&
b0aab9b9 2141 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
e63644f0
MD
2142 return (error);
2143 }
2144
b0aab9b9
MD
2145 lwkt_gettoken(&hmp->fs_token);
2146 hammer_start_transaction(&trans, hmp);
ce0138a6 2147 ++hammer_stats_file_iopsw;
d7e278bb 2148 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0, 1);
b84de5af 2149 hammer_done_transaction(&trans);
fbb84158
MD
2150 if (error == 0)
2151 hammer_knote(ap->a_dvp, NOTE_WRITE | NOTE_LINK);
b0aab9b9 2152 lwkt_reltoken(&hmp->fs_token);
b84de5af 2153 return (error);
427e5fc6
MD
2154}
2155
66325755 2156/*
349433c9
MD
2157 * hammer_vop_markatime { vp, cred }
2158 */
2159static
2160int
2161hammer_vop_markatime(struct vop_markatime_args *ap)
2162{
2163 struct hammer_transaction trans;
2164 struct hammer_inode *ip;
b0aab9b9 2165 hammer_mount_t hmp;
349433c9
MD
2166
2167 ip = VTOI(ap->a_vp);
2168 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2169 return (EROFS);
2170 if (ip->flags & HAMMER_INODE_RO)
2171 return (EROFS);
b0aab9b9
MD
2172 hmp = ip->hmp;
2173 if (hmp->mp->mnt_flag & MNT_NOATIME)
349433c9 2174 return (0);
b0aab9b9
MD
2175 lwkt_gettoken(&hmp->fs_token);
2176 hammer_start_transaction(&trans, hmp);
349433c9
MD
2177 ++hammer_stats_file_iopsw;
2178
2179 ip->ino_data.atime = trans.time;
e98f1b96 2180 hammer_modify_inode(&trans, ip, HAMMER_INODE_ATIME);
349433c9
MD
2181 hammer_done_transaction(&trans);
2182 hammer_knote(ap->a_vp, NOTE_ATTRIB);
b0aab9b9 2183 lwkt_reltoken(&hmp->fs_token);
349433c9
MD
2184 return (0);
2185}
2186
2187/*
66325755
MD
2188 * hammer_vop_setattr { vp, vap, cred }
2189 */
427e5fc6
MD
2190static
2191int
66325755 2192hammer_vop_setattr(struct vop_setattr_args *ap)
427e5fc6 2193{
8cd0a023 2194 struct hammer_transaction trans;
8cd0a023 2195 struct hammer_inode *ip;
b0aab9b9
MD
2196 struct vattr *vap;
2197 hammer_mount_t hmp;
8cd0a023
MD
2198 int modflags;
2199 int error;
d5ef456e 2200 int truncating;
4a2796f3 2201 int blksize;
fbb84158 2202 int kflags;
6362a262 2203#if 0
4a2796f3 2204 int64_t aligned_size;
6362a262 2205#endif
8cd0a023 2206 u_int32_t flags;
8cd0a023
MD
2207
2208 vap = ap->a_vap;
2209 ip = ap->a_vp->v_data;
2210 modflags = 0;
fbb84158 2211 kflags = 0;
b0aab9b9 2212 hmp = ip->hmp;
8cd0a023
MD
2213
2214 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
2215 return(EROFS);
d113fda1
MD
2216 if (ip->flags & HAMMER_INODE_RO)
2217 return (EROFS);
e63644f0 2218 if (hammer_nohistory(ip) == 0 &&
b0aab9b9 2219 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_REMOVE)) != 0) {
e63644f0
MD
2220 return (error);
2221 }
8cd0a023 2222
b0aab9b9
MD
2223 lwkt_gettoken(&hmp->fs_token);
2224 hammer_start_transaction(&trans, hmp);
ce0138a6 2225 ++hammer_stats_file_iopsw;
8cd0a023
MD
2226 error = 0;
2227
2228 if (vap->va_flags != VNOVAL) {
2229 flags = ip->ino_data.uflags;
2230 error = vop_helper_setattr_flags(&flags, vap->va_flags,
2231 hammer_to_unix_xid(&ip->ino_data.uid),
2232 ap->a_cred);
2233 if (error == 0) {
2234 if (ip->ino_data.uflags != flags) {
2235 ip->ino_data.uflags = flags;
cc0758d0 2236 ip->ino_data.ctime = trans.time;
8cd0a023 2237 modflags |= HAMMER_INODE_DDIRTY;
fbb84158 2238 kflags |= NOTE_ATTRIB;
8cd0a023
MD
2239 }
2240 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2241 error = 0;
2242 goto done;
2243 }
2244 }
2245 goto done;
2246 }
2247 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
2248 error = EPERM;
2249 goto done;
2250 }
7538695e
MD
2251 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
2252 mode_t cur_mode = ip->ino_data.mode;
2253 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2254 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2255 uuid_t uuid_uid;
2256 uuid_t uuid_gid;
2257
2258 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
2259 ap->a_cred,
2260 &cur_uid, &cur_gid, &cur_mode);
2261 if (error == 0) {
2262 hammer_guid_to_uuid(&uuid_uid, cur_uid);
2263 hammer_guid_to_uuid(&uuid_gid, cur_gid);
2264 if (bcmp(&uuid_uid, &ip->ino_data.uid,
2265 sizeof(uuid_uid)) ||
2266 bcmp(&uuid_gid, &ip->ino_data.gid,
2267 sizeof(uuid_gid)) ||
2268 ip->ino_data.mode != cur_mode
2269 ) {
2270 ip->ino_data.uid = uuid_uid;
2271 ip->ino_data.gid = uuid_gid;
2272 ip->ino_data.mode = cur_mode;
cc0758d0
MD
2273 ip->ino_data.ctime = trans.time;
2274 modflags |= HAMMER_INODE_DDIRTY;
7538695e 2275 }
fbb84158 2276 kflags |= NOTE_ATTRIB;
8cd0a023
MD
2277 }
2278 }
11ad5ade 2279 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
8cd0a023
MD
2280 switch(ap->a_vp->v_type) {
2281 case VREG:
11ad5ade 2282 if (vap->va_size == ip->ino_data.size)
d5ef456e 2283 break;
47f363f1
MD
2284
2285 /*
c58123da
MD
2286 * Log the operation if in fast-fsync mode or if
2287 * there are unterminated redo write records present.
2288 *
2289 * The second check is needed so the recovery code
2290 * properly truncates write redos even if nominal
2291 * REDO operations is turned off due to excessive
2292 * writes, because the related records might be
2293 * destroyed and never lay down a TERM_WRITE.
47f363f1 2294 */
c58123da
MD
2295 if ((ip->flags & HAMMER_INODE_REDO) ||
2296 (ip->flags & HAMMER_INODE_RDIRTY)) {
47f363f1
MD
2297 error = hammer_generate_redo(&trans, ip,
2298 vap->va_size,
2299 HAMMER_REDO_TRUNC,
2300 NULL, 0);
2301 }
2302 blksize = hammer_blocksize(vap->va_size);
2303
b84de5af
MD
2304 /*
2305 * XXX break atomicy, we can deadlock the backend
2306 * if we do not release the lock. Probably not a
2307 * big deal here.
2308 */
11ad5ade 2309 if (vap->va_size < ip->ino_data.size) {
6362a262
MD
2310 nvtruncbuf(ap->a_vp, vap->va_size,
2311 blksize,
2312 hammer_blockoff(vap->va_size));
d5ef456e 2313 truncating = 1;
fbb84158 2314 kflags |= NOTE_WRITE;
d5ef456e 2315 } else {
6362a262
MD
2316 nvextendbuf(ap->a_vp,
2317 ip->ino_data.size,
2318 vap->va_size,
2319 hammer_blocksize(ip->ino_data.size),
2320 hammer_blocksize(vap->va_size),
2321 hammer_blockoff(ip->ino_data.size),
2322 hammer_blockoff(vap->va_size),
2323 0);
d5ef456e 2324 truncating = 0;
fbb84158 2325 kflags |= NOTE_WRITE | NOTE_EXTEND;
c0ade690 2326 }
11ad5ade 2327 ip->ino_data.size = vap->va_size;
cc0758d0 2328 ip->ino_data.mtime = trans.time;
47f363f1 2329 /* XXX safe to use SDIRTY instead of DDIRTY here? */
cc0758d0 2330 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
d5ef456e 2331
b84de5af 2332 /*
6362a262
MD
2333 * On-media truncation is cached in the inode until
2334 * the inode is synchronized. We must immediately
2335 * handle any frontend records.
b84de5af 2336 */
d5ef456e 2337 if (truncating) {
47637bff 2338 hammer_ip_frontend_trunc(ip, vap->va_size);
0832c9bb
MD
2339#ifdef DEBUG_TRUNCATE
2340 if (HammerTruncIp == NULL)
2341 HammerTruncIp = ip;
2342#endif
b84de5af
MD
2343 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2344 ip->flags |= HAMMER_INODE_TRUNCATED;
2345 ip->trunc_off = vap->va_size;
0832c9bb
MD
2346#ifdef DEBUG_TRUNCATE
2347 if (ip == HammerTruncIp)
973c11b9
MD
2348 kprintf("truncate1 %016llx\n",
2349 (long long)ip->trunc_off);
0832c9bb 2350#endif
b84de5af
MD
2351 } else if (ip->trunc_off > vap->va_size) {
2352 ip->trunc_off = vap->va_size;
0832c9bb
MD
2353#ifdef DEBUG_TRUNCATE
2354 if (ip == HammerTruncIp)
973c11b9
MD
2355 kprintf("truncate2 %016llx\n",
2356 (long long)ip->trunc_off);
0832c9bb
MD
2357#endif
2358 } else {
2359#ifdef DEBUG_TRUNCATE
2360 if (ip == HammerTruncIp)
973c11b9
MD
2361 kprintf("truncate3 %016llx (ignored)\n",
2362 (long long)vap->va_size);
0832c9bb 2363#endif
b84de5af 2364 }
d5ef456e 2365 }
b84de5af 2366
6362a262 2367#if 0
d5ef456e 2368 /*
6362a262
MD
2369 * When truncating, nvtruncbuf() may have cleaned out
2370 * a portion of the last block on-disk in the buffer
2371 * cache. We must clean out any frontend records
2372 * for blocks beyond the new last block.
d5ef456e 2373 */
4a2796f3
MD
2374 aligned_size = (vap->va_size + (blksize - 1)) &
2375 ~(int64_t)(blksize - 1);
b84de5af 2376 if (truncating && vap->va_size < aligned_size) {
4a2796f3 2377 aligned_size -= blksize;
47637bff 2378 hammer_ip_frontend_trunc(ip, aligned_size);
d5ef456e 2379 }
6362a262 2380#endif
76376933 2381 break;
8cd0a023 2382 case VDATABASE:
b84de5af
MD
2383 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
2384 ip->flags |= HAMMER_INODE_TRUNCATED;
2385 ip->trunc_off = vap->va_size;
2386 } else if (ip->trunc_off > vap->va_size) {
2387 ip->trunc_off = vap->va_size;
2388 }
47637bff 2389 hammer_ip_frontend_trunc(ip, vap->va_size);
11ad5ade 2390 ip->ino_data.size = vap->va_size;
cc0758d0
MD
2391 ip->ino_data.mtime = trans.time;
2392 modflags |= HAMMER_INODE_MTIME | HAMMER_INODE_DDIRTY;
fbb84158 2393 kflags |= NOTE_ATTRIB;
8cd0a023
MD
2394 break;
2395 default:
2396 error = EINVAL;
2397 goto done;
2398 }
d26d0ae9 2399 break;
8cd0a023
MD
2400 }
2401 if (vap->va_atime.tv_sec != VNOVAL) {
cc0758d0 2402 ip->ino_data.atime = hammer_timespec_to_time(&vap->va_atime);
ddfdf542 2403 modflags |= HAMMER_INODE_ATIME;
fbb84158 2404 kflags |= NOTE_ATTRIB;
8cd0a023
MD
2405 }
2406 if (vap->va_mtime.tv_sec != VNOVAL) {
cc0758d0 2407 ip->ino_data.mtime = hammer_timespec_to_time(&vap->va_mtime);
ddfdf542 2408 modflags |= HAMMER_INODE_MTIME;
fbb84158 2409 kflags |= NOTE_ATTRIB;
8cd0a023
MD
2410 }
2411 if (vap->va_mode != (mode_t)VNOVAL) {
7538695e
MD
2412 mode_t cur_mode = ip->ino_data.mode;
2413 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
2414 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
2415
2416 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
2417 cur_uid, cur_gid, &cur_mode);
2418 if (error == 0 && ip->ino_data.mode != cur_mode) {
2419 ip->ino_data.mode = cur_mode;
cc0758d0 2420 ip->ino_data.ctime = trans.time;
8cd0a023 2421 modflags |= HAMMER_INODE_DDIRTY;
fbb84158 2422 kflags |= NOTE_ATTRIB;
8cd0a023
MD
2423 }
2424 }
2425done:
b84de5af 2426 if (error == 0)
e98f1b96 2427 hammer_modify_inode(&trans, ip, modflags);
b84de5af 2428 hammer_done_transaction(&trans);
fbb84158 2429 hammer_knote(ap->a_vp, kflags);
b0aab9b9 2430 lwkt_reltoken(&hmp->fs_token);
8cd0a023 2431 return (error);
427e5fc6
MD
2432}
2433
66325755
MD
2434/*
2435 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
2436 */
427e5fc6
MD
2437static
2438int
66325755 2439hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
427e5fc6 2440{
7a04d74f
MD
2441 struct hammer_transaction trans;
2442 struct hammer_inode *dip;
2443 struct hammer_inode *nip;
7a04d74f 2444 hammer_record_t record;
b0aab9b9
MD
2445 struct nchandle *nch;
2446 hammer_mount_t hmp;
7a04d74f
MD
2447 int error;
2448 int bytes;
2449
2450 ap->a_vap->va_type = VLNK;
2451
2452 nch = ap->a_nch;
2453 dip = VTOI(ap->a_dvp);
b0aab9b9 2454 hmp = dip->hmp;
7a04d74f 2455
d113fda1
MD
2456 if (dip->flags & HAMMER_INODE_RO)
2457 return (EROFS);
b0aab9b9 2458 if ((error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0)
e63644f0 2459 return (error);
d113fda1 2460
7a04d74f
MD
2461 /*
2462 * Create a transaction to cover the operations we perform.
2463 */
b0aab9b9
MD
2464 lwkt_gettoken(&hmp->fs_token);
2465 hammer_start_transaction(&trans, hmp);
ce0138a6 2466 ++hammer_stats_file_iopsw;
7a04d74f
MD
2467
2468 /*
2469 * Create a new filesystem object of the requested type. The
2470 * returned inode will be referenced but not locked.
2471 */
2472
5a930e66 2473 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
5a64efa1
MD
2474 dip, nch->ncp->nc_name, nch->ncp->nc_nlen,
2475 NULL, &nip);
7a04d74f 2476 if (error) {
b84de5af 2477 hammer_done_transaction(&trans);
7a04d74f 2478 *ap->a_vpp = NULL;
b0aab9b9 2479 lwkt_reltoken(&hmp->fs_token);
7a04d74f
MD
2480 return (error);
2481 }
2482
2483 /*
7a04d74f
MD
2484 * Add a record representing the symlink. symlink stores the link
2485 * as pure data, not a string, and is no \0 terminated.
2486 */
2487 if (error == 0) {
7a04d74f
MD
2488 bytes = strlen(ap->a_target);
2489
2f85fa4d
MD
2490 if (bytes <= HAMMER_INODE_BASESYMLEN) {
2491 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
2492 } else {
2493 record = hammer_alloc_mem_record(nip, bytes);
2494 record->type = HAMMER_MEM_RECORD_GENERAL;
2495
5a930e66
MD
2496 record->leaf.base.localization = nip->obj_localization +
2497 HAMMER_LOCALIZE_MISC;
2f85fa4d
MD
2498 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
2499 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
2500 record->leaf.data_len = bytes;
2501 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
2502 bcopy(ap->a_target, record->data->symlink.name, bytes);
2503 error = hammer_ip_add_record(&trans, record);
2504 }
42c7d26b
MD
2505
2506 /*
2507 * Set the file size to the length of the link.
2508 */
2509 if (error == 0) {
11ad5ade 2510 nip->ino_data.size = bytes;
e98f1b96 2511 hammer_modify_inode(&trans, nip, HAMMER_INODE_DDIRTY);
42c7d26b 2512 }
7a04d74f 2513 }
1f07f686 2514 if (error == 0)
5a930e66
MD
2515 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
2516 nch->ncp->nc_nlen, nip);
7a04d74f
MD
2517
2518 /*
2519 * Finish up.
2520 */
2521 if (error) {
2522 hammer_rel_inode(nip, 0);
7a04d74f
MD
2523 *ap->a_vpp = NULL;
2524 } else {
e8599db1 2525 error = hammer_get_vnode(nip, ap->a_vpp);
7a04d74f
MD
2526 hammer_rel_inode(nip, 0);
2527 if (error == 0) {
2528 cache_setunresolved(ap->a_nch);
2529 cache_setvp(ap->a_nch, *ap->a_vpp);
fbb84158 2530 hammer_knote(ap->a_dvp, NOTE_WRITE);
7a04d74f
MD
2531 }
2532 }
b84de5af 2533 hammer_done_transaction(&trans);
b0aab9b9 2534 lwkt_reltoken(&hmp->fs_token);
7a04d74f 2535 return (error);
427e5fc6
MD
2536}
2537
66325755
MD
2538/*
2539 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
2540 */
427e5fc6
MD
2541static
2542int
66325755 2543hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
427e5fc6 2544{
b84de5af 2545 struct hammer_transaction trans;
e63644f0 2546 struct hammer_inode *dip;
b0aab9b9 2547 hammer_mount_t hmp;
b84de5af
MD
2548 int error;
2549
e63644f0 2550 dip = VTOI(ap->a_dvp);
b0aab9b9 2551 hmp = dip->hmp;
e63644f0
MD
2552
2553 if (hammer_nohistory(dip) == 0 &&
b0aab9b9 2554 (error = hammer_checkspace(hmp, HAMMER_CHKSPC_CREATE)) != 0) {
e63644f0
MD
2555 return (error);
2556 }
2557
b0aab9b9
MD
2558 lwkt_gettoken(&hmp->fs_token);
2559 hammer_start_transaction(&trans, hmp);
ce0138a6 2560 ++hammer_stats_file_iopsw;
b84de5af 2561 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
d7e278bb 2562 ap->a_cred, ap->a_flags, -1);
b84de5af 2563 hammer_done_transaction(&trans);
b0aab9b9 2564 lwkt_reltoken(&hmp->fs_token);
b84de5af
MD
2565
2566 return (error);
427e5fc6
MD
2567}
2568
66325755 2569/*
7dc57964
MD
2570 * hammer_vop_ioctl { vp, command, data, fflag, cred }
2571 */
2572static
2573int
2574hammer_vop_ioctl(struct vop_ioctl_args *ap)
2575{
2576 struct hammer_inode *ip = ap->a_vp->v_data;
b0aab9b9
MD
2577 hammer_mount_t hmp = ip->hmp;
2578 int error;
7dc57964 2579
ce0138a6 2580 ++hammer_stats_file_iopsr;
b0aab9b9
MD
2581 lwkt_gettoken(&hmp->fs_token);
2582 error = hammer_ioctl(ip, ap->a_command, ap->a_data,
2583 ap->a_fflag, ap->a_cred);
2584 lwkt_reltoken(&hmp->fs_token);
2585 return (error);
7dc57964
MD
2586}
2587
513ca7d7
MD
2588static
2589int
2590hammer_vop_mountctl(struct vop_mountctl_args *ap)
2591{
dad088a5
MD
2592 static const struct mountctl_opt extraopt[] = {
2593 { HMNT_NOHISTORY, "nohistory" },
2594 { HMNT_MASTERID, "master" },
2595 { 0, NULL}
2596
2597 };
2598 struct hammer_mount *hmp;
513ca7d7 2599 struct mount *mp;
dad088a5 2600 int usedbytes;
513ca7d7
MD
2601 int error;
2602
dad088a5
MD
2603 error = 0;
2604 usedbytes = 0;
513ca7d7 2605 mp = ap->a_head.a_ops->head.vv_mount;
dad088a5
MD
2606 KKASSERT(mp->mnt_data != NULL);
2607 hmp = (struct hammer_mount *)mp->mnt_data;
513ca7d7 2608
b0aab9b9 2609 lwkt_gettoken(&hmp->fs_token);
dad088a5 2610
b0aab9b9 2611 switch(ap->a_op) {
513ca7d7
MD
2612 case MOUNTCTL_SET_EXPORT:
2613 if (ap->a_ctllen != sizeof(struct export_args))
2614 error = EINVAL;
b424ca30
MD
2615 else
2616 error = hammer_vfs_export(mp, ap->a_op,
513ca7d7
MD
2617 (const struct export_args *)ap->a_ctl);
2618 break;
dad088a5
MD
2619 case MOUNTCTL_MOUNTFLAGS:
2620 {
2621 /*
2622 * Call standard mountctl VOP function
2623 * so we get user mount flags.
2624 */
2625 error = vop_stdmountctl(ap);
2626 if (error)
2627 break;
2628
2629 usedbytes = *ap->a_res;
2630
eac446c5 2631 if (usedbytes > 0 && usedbytes < ap->a_buflen) {
b0aab9b9
MD
2632 usedbytes += vfs_flagstostr(hmp->hflags, extraopt,
2633 ap->a_buf,
dad088a5
MD
2634 ap->a_buflen - usedbytes,
2635 &error);
dad088a5
MD
2636 }
2637
2638 *ap->a_res += usedbytes;
2639 break;
2640 }
513ca7d7 2641 default:
726e0641 2642 error = vop_stdmountctl(ap);
513ca7d7
MD
2643 break;
2644 }
b0aab9b9 2645 lwkt_reltoken(&hmp->fs_token);
513ca7d7
MD
2646 return(error);
2647}
2648
7dc57964 2649/*
66325755 2650 * hammer_vop_strategy { vp, bio }
8cd0a023
MD
2651 *
2652 * Strategy call, used for regular file read & write only. Note that the
2653 * bp may represent a cluster.
2654 *
2655 * To simplify operation and allow better optimizations in the future,
2656 * this code does not make any assumptions with regards to buffer alignment
2657 * or size.
66325755 2658 */
427e5fc6
MD
2659static
2660int
66325755 2661hammer_vop_strategy(struct vop_strategy_args *ap)
427e5fc6 2662{
8cd0a023
MD
2663 struct buf *bp;
2664 int error;
2665
2666 bp = ap->a_bio->bio_buf;
2667
2668 switch(bp->b_cmd) {
2669 case BUF_CMD_READ:
2670 error = hammer_vop_strategy_read(ap);
2671 break;
2672 case BUF_CMD_WRITE:
2673 error = hammer_vop_strategy_write(ap);
2674 break;
2675 default:
059819e3
MD
2676 bp->b_error = error = EINVAL;
2677 bp->b_flags |= B_ERROR;
2678 biodone(ap->a_bio);
8cd0a023
MD
2679 break;
2680 }
507df98a
ID
2681
2682 /* hammer_dump_dedup_cache(((hammer_inode_t)ap->a_vp->v_data)->hmp); */
2683
8cd0a023 2684 return (error);
427e5fc6
MD
2685}
2686
8cd0a023
MD
2687/*
2688 * Read from a regular file. Iterate the related records and fill in the
2689 * BIO/BUF. Gaps are zero-filled.
2690 *
2691 * The support code in hammer_object.c should be used to deal with mixed
2692 * in-memory and on-disk records.
2693 *
4a2796f3
MD
2694 * NOTE: Can be called from the cluster code with an oversized buf.
2695 *
8cd0a023
MD
2696 * XXX atime update
2697 */
2698static
2699int
2700hammer_vop_strategy_read(struct vop_strategy_args *ap)
2701{
36f82b23
MD
2702 struct hammer_transaction trans;
2703 struct hammer_inode *ip;
39d8fd63 2704 struct hammer_inode *dip;
b0aab9b9 2705 hammer_mount_t hmp;
8cd0a023 2706 struct hammer_cursor cursor;
8cd0a023 2707 hammer_base_elm_t base;
4a2796f3 2708 hammer_off_t disk_offset;
8cd0a023 2709 struct bio *bio;
a99b9ea2 2710 struct bio *nbio;
8cd0a023
MD
2711 struct buf *bp;
2712 int64_t rec_offset;
a89aec1b 2713 int64_t ran_end;
195c19a1 2714 int64_t tmp64;
8cd0a023
MD
2715 int error;
2716 int boff;
2717 int roff;
2718 int n;
b4f86ea3 2719 int isdedupable;
8cd0a023
MD
2720
2721 bio = ap->a_bio;
2722 bp = bio->bio_buf;
36f82b23 2723 ip = ap->a_vp->v_data;
b0aab9b9 2724 hmp = ip->hmp;
8cd0a023 2725
a99b9ea2
MD
2726 /*
2727 * The zone-2 disk offset may have been set by the cluster code via
4a2796f3 2728 * a BMAP operation, or else should be NOOFFSET.
a99b9ea2 2729 *
4a2796f3 2730 * Checking the high bits for a match against zone-2 should suffice.
b4f86ea3
MD
2731 *
2732 * In cases where a lot of data duplication is present it may be
2733 * more beneficial to drop through and doubule-buffer through the
2734 * device.
a99b9ea2
MD
2735 */
2736 nbio = push_bio(bio);
9a98f3cc 2737 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
1b0ab2c3 2738 HAMMER_ZONE_LARGE_DATA) {
9a98f3cc
MD
2739 if (hammer_double_buffer == 0) {
2740 lwkt_gettoken(&hmp->fs_token);
2741 error = hammer_io_direct_read(hmp, nbio, NULL);
2742 lwkt_reltoken(&hmp->fs_token);
2743 return (error);
2744 }
2745
2746 /*
2747 * Try to shortcut requests for double_buffer mode too.
2748 * Since this mode runs through the device buffer cache
2749 * only compatible buffer sizes (meaning those generated
2750 * by normal filesystem buffers) are legal.
2751 */
2752 if (hammer_live_dedup == 0 && (bp->b_flags & B_PAGING) == 0) {
2753 error = hammer_io_indirect_read(hmp, nbio, NULL);
2754 return (error);
2755 }
a99b9ea2
MD
2756 }
2757
2758 /*
4a2796f3
MD
2759 * Well, that sucked. Do it the hard way. If all the stars are
2760 * aligned we may still be able to issue a direct-read.
a99b9ea2 2761 */
b0aab9b9
MD
2762 lwkt_gettoken(&hmp->fs_token);
2763 hammer_simple_transaction(&trans, hmp);
47637bff 2764 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
8cd0a023
MD
2765
2766 /*
2767 * Key range (begin and end inclusive) to scan. Note that the key's
c0ade690
MD
2768 * stored in the actual records represent BASE+LEN, not BASE. The
2769 * first record containing bio_offset will have a key > bio_offset.
8cd0a023 2770 */
5a930e66
MD
2771 cursor.key_beg.localization = ip->obj_localization +
2772 HAMMER_LOCALIZE_MISC;
8cd0a023 2773 cursor.key_beg.obj_id = ip->obj_id;
d5530d22 2774 cursor.key_beg.create_tid = 0;
8cd0a023 2775 cursor.key_beg.delete_tid = 0;
8cd0a023 2776 cursor.key_beg.obj_type = 0;
c0ade690 2777 cursor.key_beg.key = bio->bio_offset + 1;
d5530d22 2778 cursor.asof = ip->obj_asof;
bf3b416b 2779 cursor.flags |= HAMMER_CURSOR_ASOF;
8cd0a023
MD
2780
2781 cursor.key_end = cursor.key_beg;
11ad5ade 2782 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
b84de5af 2783#if 0
11ad5ade 2784 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
a89aec1b
MD
2785 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2786 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2787 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
b84de5af
MD
2788 } else
2789#endif
2790 {
c0ade690 2791 ran_end = bio->bio_offset + bp->b_bufsize;
a89aec1b
MD
2792 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2793 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
195c19a1
MD
2794 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2795 if (tmp64 < ran_end)
a89aec1b
MD
2796 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2797 else
7f7c1f84 2798 cursor.key_end.key = ran_end + MAXPHYS + 1;
a89aec1b 2799 }
d26d0ae9 2800 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
8cd0a023 2801
18bee4a2
MD
2802 /*
2803 * Set NOSWAPCACHE for cursor data extraction if double buffering
2804 * is disabled or (if the file is not marked cacheable via chflags
2805 * and vm.swapcache_use_chflags is enabled).
2806 */
2807 if (hammer_double_buffer == 0 ||
2808 ((ap->a_vp->v_flag & VSWAPCACHE) == 0 &&
2809 vm_swapcache_use_chflags)) {
2810 cursor.flags |= HAMMER_CURSOR_NOSWAPCACHE;
2811 }
2812
4e17f465 2813 error = hammer_ip_first(&cursor);
8cd0a023
MD
2814 boff = 0;
2815
a89aec1b 2816 while (error == 0) {
47637bff
MD
2817 /*
2818 * Get the base file offset of the record. The key for
2819 * data records is (base + bytes) rather then (base).
2820 */
11ad5ade 2821 base = &cursor.leaf->base;
11ad5ade 2822 rec_offset = base->key - cursor.leaf->data_len;
8cd0a023 2823
66325755 2824 /*
a89aec1b 2825 * Calculate the gap, if any, and zero-fill it.
1fef775e
MD
2826 *
2827 * n is the offset of the start of the record verses our
2828 * current seek offset in the bio.
66325755 2829 */
8cd0a023
MD
2830 n = (int)(rec_offset - (bio->bio_offset + boff));
2831 if (n > 0) {
a89aec1b
MD
2832 if (n > bp->b_bufsize - boff)
2833 n = bp->b_bufsize - boff;
8cd0a023
MD
2834 bzero((char *)bp->b_data + boff, n);
2835 boff += n;
2836 n = 0;
66325755 2837 }
8cd0a023
MD
2838
2839 /*
2840 * Calculate the data offset in the record and the number
2841 * of bytes we can copy.
a89aec1b 2842 *
1fef775e
MD
2843 * There are two degenerate cases. First, boff may already
2844 * be at bp->b_bufsize. Secondly, the data offset within
2845 * the record may exceed the record's size.
8cd0a023
MD
2846 */
2847 roff = -n;
b84de5af 2848 rec_offset += roff;
11ad5ade 2849 n = cursor.leaf->data_len - roff;
1fef775e
MD
2850 if (n <= 0) {
2851 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2852 n = 0;
2853 } else if (n > bp->b_bufsize - boff) {
8cd0a023 2854 n = bp->b_bufsize - boff;
1fef775e 2855 }
059819e3 2856
b84de5af 2857 /*
47637bff
MD
2858 * Deal with cached truncations. This cool bit of code
2859 * allows truncate()/ftruncate() to avoid having to sync
2860 * the file.
2861 *
2862 * If the frontend is truncated then all backend records are
2863 * subject to the frontend's truncation.
2864 *
2865 * If the backend is truncated then backend records on-disk
2866 * (but not in-memory) are subject to the backend's
2867 * truncation. In-memory records owned by the backend
2868 * represent data written after the truncation point on the
2869 * backend and must not be truncated.
2870 *
2871 * Truncate operations deal with frontend buffer cache
2872 * buffers and frontend-owned in-memory records synchronously.
b84de5af 2873 */
47637bff 2874 if (ip->flags & HAMMER_INODE_TRUNCATED) {
6362a262
MD
2875 if (hammer_cursor_ondisk(&cursor)/* ||
2876 cursor.iprec->flush_state == HAMMER_FST_FLUSH*/) {
47637bff
MD
2877 if (ip->trunc_off <= rec_offset)
2878 n = 0;
2879 else if (ip->trunc_off < rec_offset + n)
2880 n = (int)(ip->trunc_off - rec_offset);
2881 }
2882 }
2883 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2884 if (hammer_cursor_ondisk(&cursor)) {
2885 if (ip->sync_trunc_off <= rec_offset)
2886 n = 0;
2887 else if (ip->sync_trunc_off < rec_offset + n)
2888 n = (int)(ip->sync_trunc_off - rec_offset);
2889 }
2890 }
b84de5af
MD
2891
2892 /*
47637bff
MD
2893 * Try to issue a direct read into our bio if possible,
2894 * otherwise resolve the element data into a hammer_buffer
2895 * and copy.
4a2796f3
MD
2896 *
2897 * The buffer on-disk should be zerod past any real
2898 * truncation point, but may not be for any synthesized
2899 * truncation point from above.
9a98f3cc
MD
2900 *
2901 * NOTE: disk_offset is only valid if the cursor data is
2902 * on-disk.
b84de5af 2903 */
1b0ab2c3 2904 disk_offset = cursor.leaf->data_offset + roff;
b4f86ea3
MD
2905 isdedupable = (boff == 0 && n == bp->b_bufsize &&
2906 hammer_cursor_ondisk(&cursor) &&
2907 ((int)disk_offset & HAMMER_BUFMASK) == 0);
2908
2909 if (isdedupable && hammer_double_buffer == 0) {
9a98f3cc
MD
2910 /*
2911 * Direct read case
2912 */
1b0ab2c3
MD
2913 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2914 HAMMER_ZONE_LARGE_DATA);
4a2796f3 2915 nbio->bio_offset = disk_offset;
b0aab9b9 2916 error = hammer_io_direct_read(hmp, nbio, cursor.leaf);
b4f86ea3 2917 if (hammer_live_dedup && error == 0)
507df98a 2918 hammer_dedup_cache_add(ip, cursor.leaf);
47637bff 2919 goto done;
9a98f3cc
MD
2920 } else if (isdedupable) {
2921 /*
2922 * Async I/O case for reading from backing store
2923 * and copying the data to the filesystem buffer.
2924 * live-dedup has to verify the data anyway if it
2925 * gets a hit later so we can just add the entry
2926 * now.
2927 */
2928 KKASSERT((disk_offset & HAMMER_OFF_ZONE_MASK) ==
2929 HAMMER_ZONE_LARGE_DATA);
2930 nbio->bio_offset = disk_offset;
2931 if (hammer_live_dedup)
2932 hammer_dedup_cache_add(ip, cursor.leaf);
2933 error = hammer_io_indirect_read(hmp, nbio, cursor.leaf);
2934 goto done;
47637bff
MD
2935 } else if (n) {
2936 error = hammer_ip_resolve_data(&cursor);
2937 if (error == 0) {
b4f86ea3
MD
2938 if (hammer_live_dedup && isdedupable)
2939 hammer_dedup_cache_add(ip, cursor.leaf);
47637bff
MD
2940 bcopy((char *)cursor.data + roff,
2941 (char *)bp->b_data + boff, n);
2942 }
b84de5af 2943 }
47637bff
MD
2944 if (error)
2945 break;
2946
2947 /*
507df98a
ID
2948 * We have to be sure that the only elements added to the
2949 * dedup cache are those which are already on-media.
2950 */
2951 if (hammer_live_dedup && hammer_cursor_ondisk(&cursor))
2952 hammer_dedup_cache_add(ip, cursor.leaf);
2953
2954 /*
47637bff
MD
2955 * Iterate until we have filled the request.
2956 */
2957 boff += n;
8cd0a023 2958 if (boff == bp->b_bufsize)
66325755 2959 break;
a89aec1b 2960 error = hammer_ip_next(&cursor);
66325755
MD
2961 }
2962
2963 /*
8cd0a023 2964 * There may have been a gap after the last record
66325755 2965 */
8cd0a023
MD
2966 if (error == ENOENT)
2967 error = 0;
2968 if (error == 0 && boff != bp->b_bufsize) {
7f7c1f84 2969 KKASSERT(boff < bp->b_bufsize);
8cd0a023
MD
2970 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2971 /* boff = bp->b_bufsize; */
2972 }
18bee4a2
MD
2973
2974 /*
2975 * Disallow swapcache operation on the vnode buffer if double
2976 * buffering is enabled, the swapcache will get the data via
2977 * the block device buffer.
2978 */
2979 if (hammer_double_buffer)
2980 bp->b_flags |= B_NOTMETA;
2981
2982 /*
2983 * Cleanup
2984 */
8cd0a023 2985 bp->b_resid = 0;
059819e3
MD
2986 bp->b_error = error;
2987 if (error)
2988 bp->b_flags |= B_ERROR;
2989 biodone(ap->a_bio);
47637bff
MD
2990
2991done:
39d8fd63
MD
2992 /*
2993 * Cache the b-tree node for the last data read in cache[1].
2994 *
2995 * If we hit the file EOF then also cache the node in the
2996 * governing director's cache[3], it will be used to initialize
2997 * the inode's cache[1] for any inodes looked up via the directory.
2998 *
2999 * This doesn't reduce disk accesses since the B-Tree chain is
3000 * likely cached, but it does reduce cpu overhead when looking
3001 * up file offsets for cpdup/tar/cpio style iterations.
3002 */
47637bff 3003 if (cursor.node)
bcac4bbb 3004 hammer_cache_node(&ip->cache[1], cursor.node);
39d8fd63
MD
3005 if (ran_end >= ip->ino_data.size) {
3006 dip = hammer_find_inode(&trans, ip->ino_data.parent_obj_id,
3007 ip->obj_asof, ip->obj_localization);
3008 if (dip) {
3009 hammer_cache_node(&dip->cache[3], cursor.node);
3010 hammer_rel_inode(dip, 0);
3011 }
3012 }
47637bff
MD
3013 hammer_done_cursor(&cursor);
3014 hammer_done_transaction(&trans);
b0aab9b9 3015 lwkt_reltoken(&hmp->fs_token);
8cd0a023
MD
3016 return(error);
3017}
3018
3019/*
a99b9ea2
MD
3020 * BMAP operation - used to support cluster_read() only.
3021 *
3022 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
3023 *
3024 * This routine may return EOPNOTSUPP if the opration is not supported for
3025 * the specified offset. The contents of the pointer arguments do not
3026 * need to be initialized in that case.
3027 *
3028 * If a disk address is available and properly aligned return 0 with
3029 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
3030 * to the run-length relative to that offset. Callers may assume that
3031 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
3032 * large, so return EOPNOTSUPP if it is not sufficiently large.
3033 */
3034static
3035int
3036hammer_vop_bmap(struct vop_bmap_args *ap)
3037{
3038 struct hammer_transaction trans;
3039 struct hammer_inode *ip;
b0aab9b9 3040 hammer_mount_t hmp;
a99b9ea2
MD
3041 struct hammer_cursor cursor;
3042 hammer_base_elm_t base;
3043 int64_t rec_offset;
3044 int64_t ran_end;
3045 int64_t tmp64;
3046 int64_t base_offset;
3047 int64_t base_disk_offset;
3048 int64_t last_offset;
3049 hammer_off_t last_disk_offset;
3050 hammer_off_t disk_offset;
3051 int rec_len;
3052 int error;
4a2796f3 3053 int blksize;
a99b9ea2 3054
ce0138a6 3055 ++hammer_stats_file_iopsr;
a99b9ea2 3056 ip = ap->a_vp->v_data;
b0aab9b9 3057 hmp = ip->hmp;
a99b9ea2
MD
3058
3059 /*
3060 * We can only BMAP regular files. We can't BMAP database files,
3061 * directories, etc.
3062 */
3063 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
3064 return(EOPNOTSUPP);
3065
3066 /*
3067 * bmap is typically called with runp/runb both NULL when used
3068 * for writing. We do not support BMAP for writing atm.
3069 */
4a2796f3 3070 if (ap->a_cmd != BUF_CMD_READ)
a99b9ea2
MD
3071 return(EOPNOTSUPP);
3072
3073 /*
3074 * Scan the B-Tree to acquire blockmap addresses, then translate
3075 * to raw addresses.
3076 */
b0aab9b9
MD
3077 lwkt_gettoken(&hmp->fs_token);
3078 hammer_simple_transaction(&trans, hmp);
cb51be26 3079#if 0
973c11b9
MD
3080 kprintf("bmap_beg %016llx ip->cache %p\n",
3081 (long long)ap->a_loffset, ip->cache[1]);
cb51be26 3082#endif
a99b9ea2
MD
3083 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
3084
3085 /*
3086 * Key range (begin and end inclusive) to scan. Note that the key's
3087 * stored in the actual records represent BASE+LEN, not BASE. The
3088 * first record containing bio_offset will have a key > bio_offset.
3089 */
5a930e66
MD
3090 cursor.key_beg.localization = ip->obj_localization +
3091 HAMMER_LOCALIZE_MISC;
a99b9ea2
MD
3092 cursor.key_beg.obj_id = ip->obj_id;
3093 cursor.key_beg.create_tid = 0;
3094 cursor.key_beg.delete_tid = 0;
3095 cursor.key_beg.obj_type = 0;
3096 if (ap->a_runb)
3097 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
3098 else
3099 cursor.key_beg.key = ap->a_loffset + 1;
3100 if (cursor.key_beg.key < 0)
3101 cursor.key_beg.key = 0;
3102 cursor.asof = ip->obj_asof;
bf3b416b 3103 cursor.flags |= HAMMER_CURSOR_ASOF;
a99b9ea2
MD
3104
3105 cursor.key_end = cursor.key_beg;
3106 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
3107
3108 ran_end = ap->a_loffset + MAXPHYS;
3109 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
3110 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
3111 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
3112 if (tmp64 < ran_end)
3113 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
3114 else
3115 cursor.key_end.key = ran_end + MAXPHYS + 1;
3116
3117 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
3118
3119 error = hammer_ip_first(&cursor);
3120 base_offset = last_offset = 0;
3121 base_disk_offset = last_disk_offset = 0;
3122
3123 while (error == 0) {
3124 /*
3125 * Get the base file offset of the record. The key for
3126 * data records is (base + bytes) rather then (base).
4a2796f3
MD
3127 *
3128 * NOTE: rec_offset + rec_len may exceed the end-of-file.
3129 * The extra bytes should be zero on-disk and the BMAP op
3130 * should still be ok.
a99b9ea2
MD
3131 */
3132 base = &cursor.leaf->base;
3133 rec_offset = base->key - cursor.leaf->data_len;
3134 rec_len = cursor.leaf->data_len;
3135
3136 /*
4a2796f3
MD
3137 * Incorporate any cached truncation.
3138 *
3139 * NOTE: Modifications to rec_len based on synthesized
3140 * truncation points remove the guarantee that any extended
3141 * data on disk is zero (since the truncations may not have
3142 * taken place on-media yet).
a99b9ea2
MD
3143 */
3144 if (ip->flags & HAMMER_INODE_TRUNCATED) {
3145 if (hammer_cursor_ondisk(&cursor) ||
3146 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
3147 if (ip->trunc_off <= rec_offset)
3148 rec_len = 0;
3149 else if (ip->trunc_off < rec_offset + rec_len)
3150 rec_len = (int)(ip->trunc_off - rec_offset);
3151 }
3152 }
3153 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
3154 if (hammer_cursor_ondisk(&cursor)) {
3155 if (ip->sync_trunc_off <= rec_offset)
3156 rec_len = 0;
3157 else if (ip->sync_trunc_off < rec_offset + rec_len)
3158 rec_len = (int)(ip->sync_trunc_off - rec_offset);
3159 }
3160 }
3161
3162 /*
3163 * Accumulate information. If we have hit a discontiguous
3164 * block reset base_offset unless we are already beyond the
3165 * requested offset. If we are, that's it, we stop.
3166 */
a99b9ea2
MD
3167 if (error)
3168 break;
1b0ab2c3
MD
3169 if (hammer_cursor_ondisk(&cursor)) {
3170 disk_offset = cursor.leaf->data_offset;
3171 if (rec_offset != last_offset ||
3172 disk_offset != last_disk_offset) {
3173 if (rec_offset > ap->a_loffset)
3174 break;
3175 base_offset = rec_offset;
3176 base_disk_offset = disk_offset;
3177 }
3178 last_offset = rec_offset + rec_len;
3179 last_disk_offset = disk_offset + rec_len;
507df98a
ID
3180
3181 if (hammer_live_dedup)
3182 hammer_dedup_cache_add(ip, cursor.leaf);