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