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