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