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