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