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