Merge from vendor branch NETGRAPH:
[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 *
c82af904 34 * $DragonFly: src/sys/vfs/hammer/hammer_vnops.c,v 1.77 2008/06/26 04:06:23 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 647
5a930e66
MD
648 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
649 dip, 0, &nip);
66325755 650 if (error) {
77062c8a 651 hkprintf("hammer_create_inode error %d\n", error);
b84de5af 652 hammer_done_transaction(&trans);
66325755
MD
653 *ap->a_vpp = NULL;
654 return (error);
655 }
66325755
MD
656
657 /*
658 * Add the new filesystem object to the directory. This will also
659 * bump the inode's link count.
660 */
5a930e66
MD
661 error = hammer_ip_add_directory(&trans, dip,
662 nch->ncp->nc_name, nch->ncp->nc_nlen,
663 nip);
0b075555 664 if (error)
77062c8a 665 hkprintf("hammer_ip_add_directory error %d\n", error);
66325755
MD
666
667 /*
668 * Finish up.
669 */
670 if (error) {
a89aec1b 671 hammer_rel_inode(nip, 0);
b84de5af 672 hammer_done_transaction(&trans);
66325755
MD
673 *ap->a_vpp = NULL;
674 } else {
e8599db1 675 error = hammer_get_vnode(nip, ap->a_vpp);
b84de5af 676 hammer_done_transaction(&trans);
a89aec1b
MD
677 hammer_rel_inode(nip, 0);
678 if (error == 0) {
679 cache_setunresolved(ap->a_nch);
680 cache_setvp(ap->a_nch, *ap->a_vpp);
681 }
66325755
MD
682 }
683 return (error);
427e5fc6
MD
684}
685
66325755
MD
686/*
687 * hammer_vop_getattr { vp, vap }
98f7132d
MD
688 *
689 * Retrieve an inode's attribute information. When accessing inodes
690 * historically we fake the atime field to ensure consistent results.
691 * The atime field is stored in the B-Tree element and allowed to be
692 * updated without cycling the element.
66325755 693 */
427e5fc6
MD
694static
695int
66325755 696hammer_vop_getattr(struct vop_getattr_args *ap)
427e5fc6 697{
66325755
MD
698 struct hammer_inode *ip = VTOI(ap->a_vp);
699 struct vattr *vap = ap->a_vap;
700
66325755 701 vap->va_fsid = ip->hmp->fsid_udev;
c82af904
MD
702 /*
703 * XXX munge the device if we are in a pseudo-fs, so user utilities
704 * do not think its the same 'filesystem'.
705 */
706 if (ip->obj_localization)
707 vap->va_fsid += ip->obj_localization;
11ad5ade 708 vap->va_fileid = ip->ino_leaf.base.obj_id;
66325755 709 vap->va_mode = ip->ino_data.mode;
11ad5ade 710 vap->va_nlink = ip->ino_data.nlinks;
66325755
MD
711 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
712 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
713 vap->va_rmajor = 0;
714 vap->va_rminor = 0;
11ad5ade 715 vap->va_size = ip->ino_data.size;
bcac4bbb
MD
716
717 /*
718 * We must provide a consistent atime and mtime for snapshots
719 * so people can do a 'tar cf - ... | md5' on them and get
720 * consistent results.
721 */
722 if (ip->flags & HAMMER_INODE_RO) {
ddfdf542
MD
723 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_atime);
724 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_mtime);
bcac4bbb 725 } else {
ddfdf542
MD
726 hammer_time_to_timespec(ip->ino_data.atime, &vap->va_atime);
727 hammer_time_to_timespec(ip->ino_data.mtime, &vap->va_mtime);
bcac4bbb 728 }
ddfdf542 729 hammer_time_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
66325755
MD
730 vap->va_flags = ip->ino_data.uflags;
731 vap->va_gen = 1; /* hammer inums are unique for all time */
bf686dbe 732 vap->va_blocksize = HAMMER_BUFSIZE;
4a2796f3
MD
733 if (ip->ino_data.size >= HAMMER_XDEMARC) {
734 vap->va_bytes = (ip->ino_data.size + HAMMER_XBUFMASK64) &
735 ~HAMMER_XBUFMASK64;
736 } else if (ip->ino_data.size > HAMMER_BUFSIZE / 2) {
737 vap->va_bytes = (ip->ino_data.size + HAMMER_BUFMASK64) &
738 ~HAMMER_BUFMASK64;
739 } else {
740 vap->va_bytes = (ip->ino_data.size + 15) & ~15;
741 }
11ad5ade 742 vap->va_type = hammer_get_vnode_type(ip->ino_data.obj_type);
66325755 743 vap->va_filerev = 0; /* XXX */
4a2796f3 744 /* mtime uniquely identifies any adjustments made to the file XXX */
11ad5ade 745 vap->va_fsmid = ip->ino_data.mtime;
66325755
MD
746 vap->va_uid_uuid = ip->ino_data.uid;
747 vap->va_gid_uuid = ip->ino_data.gid;
748 vap->va_fsid_uuid = ip->hmp->fsid;
749 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
750 VA_FSID_UUID_VALID;
7a04d74f 751
11ad5ade 752 switch (ip->ino_data.obj_type) {
7a04d74f
MD
753 case HAMMER_OBJTYPE_CDEV:
754 case HAMMER_OBJTYPE_BDEV:
755 vap->va_rmajor = ip->ino_data.rmajor;
756 vap->va_rminor = ip->ino_data.rminor;
757 break;
758 default:
759 break;
760 }
761
66325755 762 return(0);
427e5fc6
MD
763}
764
66325755
MD
765/*
766 * hammer_vop_nresolve { nch, dvp, cred }
767 *
768 * Locate the requested directory entry.
769 */
427e5fc6
MD
770static
771int
66325755 772hammer_vop_nresolve(struct vop_nresolve_args *ap)
427e5fc6 773{
36f82b23 774 struct hammer_transaction trans;
66325755 775 struct namecache *ncp;
7f7c1f84
MD
776 hammer_inode_t dip;
777 hammer_inode_t ip;
778 hammer_tid_t asof;
8cd0a023 779 struct hammer_cursor cursor;
66325755
MD
780 struct vnode *vp;
781 int64_t namekey;
782 int error;
7f7c1f84
MD
783 int i;
784 int nlen;
d113fda1 785 int flags;
6a37e7e4 786 u_int64_t obj_id;
ddfdf542 787 u_int32_t localization;
7f7c1f84
MD
788
789 /*
790 * Misc initialization, plus handle as-of name extensions. Look for
791 * the '@@' extension. Note that as-of files and directories cannot
792 * be modified.
7f7c1f84
MD
793 */
794 dip = VTOI(ap->a_dvp);
795 ncp = ap->a_nch->ncp;
796 asof = dip->obj_asof;
797 nlen = ncp->nc_nlen;
d113fda1 798 flags = dip->flags;
7f7c1f84 799
36f82b23
MD
800 hammer_simple_transaction(&trans, dip->hmp);
801
7f7c1f84
MD
802 for (i = 0; i < nlen; ++i) {
803 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
d113fda1 804 asof = hammer_str_to_tid(ncp->nc_name + i + 2);
d113fda1 805 flags |= HAMMER_INODE_RO;
7f7c1f84
MD
806 break;
807 }
808 }
809 nlen = i;
66325755 810
d113fda1
MD
811 /*
812 * If there is no path component the time extension is relative to
813 * dip.
814 */
815 if (nlen == 0) {
bcac4bbb 816 ip = hammer_get_inode(&trans, dip, dip->obj_id,
ddfdf542
MD
817 asof, dip->obj_localization,
818 flags, &error);
d113fda1 819 if (error == 0) {
e8599db1 820 error = hammer_get_vnode(ip, &vp);
d113fda1
MD
821 hammer_rel_inode(ip, 0);
822 } else {
823 vp = NULL;
824 }
825 if (error == 0) {
826 vn_unlock(vp);
827 cache_setvp(ap->a_nch, vp);
828 vrele(vp);
829 }
36f82b23 830 goto done;
d113fda1
MD
831 }
832
8cd0a023
MD
833 /*
834 * Calculate the namekey and setup the key range for the scan. This
835 * works kinda like a chained hash table where the lower 32 bits
836 * of the namekey synthesize the chain.
837 *
838 * The key range is inclusive of both key_beg and key_end.
839 */
7f7c1f84 840 namekey = hammer_directory_namekey(ncp->nc_name, nlen);
66325755 841
bcac4bbb 842 error = hammer_init_cursor(&trans, &cursor, &dip->cache[1], dip);
5a930e66
MD
843 cursor.key_beg.localization = dip->obj_localization +
844 HAMMER_LOCALIZE_MISC;
8cd0a023
MD
845 cursor.key_beg.obj_id = dip->obj_id;
846 cursor.key_beg.key = namekey;
d5530d22 847 cursor.key_beg.create_tid = 0;
8cd0a023
MD
848 cursor.key_beg.delete_tid = 0;
849 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
850 cursor.key_beg.obj_type = 0;
66325755 851
8cd0a023
MD
852 cursor.key_end = cursor.key_beg;
853 cursor.key_end.key |= 0xFFFFFFFFULL;
d5530d22
MD
854 cursor.asof = asof;
855 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
66325755
MD
856
857 /*
8cd0a023 858 * Scan all matching records (the chain), locate the one matching
a89aec1b 859 * the requested path component.
8cd0a023
MD
860 *
861 * The hammer_ip_*() functions merge in-memory records with on-disk
862 * records for the purposes of the search.
66325755 863 */
6a37e7e4 864 obj_id = 0;
43c665ae 865 localization = HAMMER_DEF_LOCALIZATION;
6a37e7e4 866
4e17f465 867 if (error == 0) {
4e17f465
MD
868 error = hammer_ip_first(&cursor);
869 while (error == 0) {
870 error = hammer_ip_resolve_data(&cursor);
871 if (error)
872 break;
11ad5ade
MD
873 if (nlen == cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF &&
874 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
875 obj_id = cursor.data->entry.obj_id;
ddfdf542 876 localization = cursor.data->entry.localization;
4e17f465
MD
877 break;
878 }
879 error = hammer_ip_next(&cursor);
66325755
MD
880 }
881 }
6a37e7e4 882 hammer_done_cursor(&cursor);
66325755 883 if (error == 0) {
bcac4bbb 884 ip = hammer_get_inode(&trans, dip, obj_id,
ddfdf542
MD
885 asof, localization,
886 flags, &error);
7f7c1f84 887 if (error == 0) {
e8599db1 888 error = hammer_get_vnode(ip, &vp);
7f7c1f84
MD
889 hammer_rel_inode(ip, 0);
890 } else {
891 vp = NULL;
892 }
66325755
MD
893 if (error == 0) {
894 vn_unlock(vp);
895 cache_setvp(ap->a_nch, vp);
896 vrele(vp);
897 }
898 } else if (error == ENOENT) {
899 cache_setvp(ap->a_nch, NULL);
900 }
36f82b23 901done:
b84de5af 902 hammer_done_transaction(&trans);
66325755 903 return (error);
427e5fc6
MD
904}
905
66325755
MD
906/*
907 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
908 *
909 * Locate the parent directory of a directory vnode.
910 *
911 * dvp is referenced but not locked. *vpp must be returned referenced and
912 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
913 * at the root, instead it could indicate that the directory we were in was
914 * removed.
42c7d26b
MD
915 *
916 * NOTE: as-of sequences are not linked into the directory structure. If
917 * we are at the root with a different asof then the mount point, reload
918 * the same directory with the mount point's asof. I'm not sure what this
919 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
920 * get confused, but it hasn't been tested.
66325755 921 */
427e5fc6
MD
922static
923int
66325755 924hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
427e5fc6 925{
36f82b23 926 struct hammer_transaction trans;
66325755 927 struct hammer_inode *dip;
d113fda1 928 struct hammer_inode *ip;
42c7d26b 929 int64_t parent_obj_id;
5a930e66 930 u_int32_t parent_obj_localization;
42c7d26b 931 hammer_tid_t asof;
d113fda1 932 int error;
66325755
MD
933
934 dip = VTOI(ap->a_dvp);
42c7d26b 935 asof = dip->obj_asof;
5a930e66
MD
936
937 /*
938 * Whos are parent? This could be the root of a pseudo-filesystem
939 * whos parent is in another localization domain.
940 */
42c7d26b 941 parent_obj_id = dip->ino_data.parent_obj_id;
5a930e66
MD
942 if (dip->obj_id == HAMMER_OBJID_ROOT)
943 parent_obj_localization = dip->ino_data.ext.obj.parent_obj_localization;
944 else
945 parent_obj_localization = dip->obj_localization;
42c7d26b
MD
946
947 if (parent_obj_id == 0) {
948 if (dip->obj_id == HAMMER_OBJID_ROOT &&
949 asof != dip->hmp->asof) {
950 parent_obj_id = dip->obj_id;
951 asof = dip->hmp->asof;
952 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
953 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
954 dip->obj_asof);
955 } else {
956 *ap->a_vpp = NULL;
957 return ENOENT;
958 }
66325755 959 }
d113fda1 960
36f82b23
MD
961 hammer_simple_transaction(&trans, dip->hmp);
962
bcac4bbb 963 ip = hammer_get_inode(&trans, dip, parent_obj_id,
5a930e66 964 asof, parent_obj_localization,
ddfdf542 965 dip->flags, &error);
36f82b23 966 if (ip) {
e8599db1 967 error = hammer_get_vnode(ip, ap->a_vpp);
36f82b23
MD
968 hammer_rel_inode(ip, 0);
969 } else {
d113fda1 970 *ap->a_vpp = NULL;
d113fda1 971 }
b84de5af 972 hammer_done_transaction(&trans);
d113fda1 973 return (error);
427e5fc6
MD
974}
975
66325755
MD
976/*
977 * hammer_vop_nlink { nch, dvp, vp, cred }
978 */
427e5fc6
MD
979static
980int
66325755 981hammer_vop_nlink(struct vop_nlink_args *ap)
427e5fc6 982{
66325755
MD
983 struct hammer_transaction trans;
984 struct hammer_inode *dip;
985 struct hammer_inode *ip;
986 struct nchandle *nch;
987 int error;
988
989 nch = ap->a_nch;
990 dip = VTOI(ap->a_dvp);
991 ip = VTOI(ap->a_vp);
992
d113fda1
MD
993 if (dip->flags & HAMMER_INODE_RO)
994 return (EROFS);
995 if (ip->flags & HAMMER_INODE_RO)
996 return (EROFS);
e63644f0
MD
997 if ((error = hammer_checkspace(dip->hmp)) != 0)
998 return (error);
d113fda1 999
66325755
MD
1000 /*
1001 * Create a transaction to cover the operations we perform.
1002 */
8cd0a023 1003 hammer_start_transaction(&trans, dip->hmp);
66325755
MD
1004
1005 /*
1006 * Add the filesystem object to the directory. Note that neither
1007 * dip nor ip are referenced or locked, but their vnodes are
1008 * referenced. This function will bump the inode's link count.
1009 */
5a930e66
MD
1010 error = hammer_ip_add_directory(&trans, dip,
1011 nch->ncp->nc_name, nch->ncp->nc_nlen,
1012 ip);
66325755
MD
1013
1014 /*
1015 * Finish up.
1016 */
b84de5af 1017 if (error == 0) {
6b4f890b
MD
1018 cache_setunresolved(nch);
1019 cache_setvp(nch, ap->a_vp);
66325755 1020 }
b84de5af 1021 hammer_done_transaction(&trans);
66325755 1022 return (error);
427e5fc6
MD
1023}
1024
66325755
MD
1025/*
1026 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
1027 *
1028 * The operating system has already ensured that the directory entry
1029 * does not exist and done all appropriate namespace locking.
1030 */
427e5fc6
MD
1031static
1032int
66325755 1033hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
427e5fc6 1034{
66325755
MD
1035 struct hammer_transaction trans;
1036 struct hammer_inode *dip;
1037 struct hammer_inode *nip;
1038 struct nchandle *nch;
1039 int error;
1040
1041 nch = ap->a_nch;
1042 dip = VTOI(ap->a_dvp);
1043
d113fda1
MD
1044 if (dip->flags & HAMMER_INODE_RO)
1045 return (EROFS);
e63644f0
MD
1046 if ((error = hammer_checkspace(dip->hmp)) != 0)
1047 return (error);
d113fda1 1048
66325755
MD
1049 /*
1050 * Create a transaction to cover the operations we perform.
1051 */
8cd0a023 1052 hammer_start_transaction(&trans, dip->hmp);
66325755
MD
1053
1054 /*
1055 * Create a new filesystem object of the requested type. The
8cd0a023 1056 * returned inode will be referenced but not locked.
66325755 1057 */
5a930e66
MD
1058 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1059 dip, 0, &nip);
66325755 1060 if (error) {
77062c8a 1061 hkprintf("hammer_mkdir error %d\n", error);
b84de5af 1062 hammer_done_transaction(&trans);
66325755
MD
1063 *ap->a_vpp = NULL;
1064 return (error);
1065 }
66325755
MD
1066 /*
1067 * Add the new filesystem object to the directory. This will also
1068 * bump the inode's link count.
1069 */
5a930e66
MD
1070 error = hammer_ip_add_directory(&trans, dip,
1071 nch->ncp->nc_name, nch->ncp->nc_nlen,
1072 nip);
0b075555 1073 if (error)
77062c8a 1074 hkprintf("hammer_mkdir (add) error %d\n", error);
66325755
MD
1075
1076 /*
1077 * Finish up.
1078 */
1079 if (error) {
a89aec1b 1080 hammer_rel_inode(nip, 0);
66325755
MD
1081 *ap->a_vpp = NULL;
1082 } else {
e8599db1 1083 error = hammer_get_vnode(nip, ap->a_vpp);
a89aec1b
MD
1084 hammer_rel_inode(nip, 0);
1085 if (error == 0) {
1086 cache_setunresolved(ap->a_nch);
1087 cache_setvp(ap->a_nch, *ap->a_vpp);
1088 }
66325755 1089 }
b84de5af 1090 hammer_done_transaction(&trans);
66325755 1091 return (error);
427e5fc6
MD
1092}
1093
66325755
MD
1094/*
1095 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
1096 *
1097 * The operating system has already ensured that the directory entry
1098 * does not exist and done all appropriate namespace locking.
1099 */
427e5fc6
MD
1100static
1101int
66325755 1102hammer_vop_nmknod(struct vop_nmknod_args *ap)
427e5fc6 1103{
66325755
MD
1104 struct hammer_transaction trans;
1105 struct hammer_inode *dip;
1106 struct hammer_inode *nip;
1107 struct nchandle *nch;
1108 int error;
5a930e66 1109 int pseudofs;
66325755
MD
1110
1111 nch = ap->a_nch;
1112 dip = VTOI(ap->a_dvp);
1113
d113fda1
MD
1114 if (dip->flags & HAMMER_INODE_RO)
1115 return (EROFS);
e63644f0
MD
1116 if ((error = hammer_checkspace(dip->hmp)) != 0)
1117 return (error);
d113fda1 1118
66325755
MD
1119 /*
1120 * Create a transaction to cover the operations we perform.
1121 */
8cd0a023 1122 hammer_start_transaction(&trans, dip->hmp);
66325755
MD
1123
1124 /*
1125 * Create a new filesystem object of the requested type. The
8cd0a023 1126 * returned inode will be referenced but not locked.
5a930e66
MD
1127 *
1128 * If mknod specifies a directory a pseudo-fs is created.
66325755 1129 */
5a930e66
MD
1130 pseudofs = (ap->a_vap->va_type == VDIR);
1131 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1132 dip, pseudofs, &nip);
66325755 1133 if (error) {
b84de5af 1134 hammer_done_transaction(&trans);
66325755
MD
1135 *ap->a_vpp = NULL;
1136 return (error);
1137 }
66325755
MD
1138
1139 /*
1140 * Add the new filesystem object to the directory. This will also
1141 * bump the inode's link count.
1142 */
5a930e66
MD
1143 error = hammer_ip_add_directory(&trans, dip,
1144 nch->ncp->nc_name, nch->ncp->nc_nlen,
1145 nip);
66325755
MD
1146
1147 /*
1148 * Finish up.
1149 */
1150 if (error) {
a89aec1b 1151 hammer_rel_inode(nip, 0);
66325755
MD
1152 *ap->a_vpp = NULL;
1153 } else {
e8599db1 1154 error = hammer_get_vnode(nip, ap->a_vpp);
a89aec1b
MD
1155 hammer_rel_inode(nip, 0);
1156 if (error == 0) {
1157 cache_setunresolved(ap->a_nch);
1158 cache_setvp(ap->a_nch, *ap->a_vpp);
1159 }
66325755 1160 }
b84de5af 1161 hammer_done_transaction(&trans);
66325755 1162 return (error);
427e5fc6
MD
1163}
1164
66325755
MD
1165/*
1166 * hammer_vop_open { vp, mode, cred, fp }
1167 */
427e5fc6
MD
1168static
1169int
66325755 1170hammer_vop_open(struct vop_open_args *ap)
427e5fc6 1171{
9f5097dc
MD
1172 hammer_inode_t ip;
1173
1174 ip = VTOI(ap->a_vp);
1175
1176 if ((ap->a_mode & FWRITE) && (ip->flags & HAMMER_INODE_RO))
d113fda1 1177 return (EROFS);
a89aec1b 1178 return(vop_stdopen(ap));
427e5fc6
MD
1179}
1180
66325755
MD
1181/*
1182 * hammer_vop_pathconf { vp, name, retval }
1183 */
427e5fc6
MD
1184static
1185int
66325755 1186hammer_vop_pathconf(struct vop_pathconf_args *ap)
427e5fc6
MD
1187{
1188 return EOPNOTSUPP;
1189}
1190
66325755
MD
1191/*
1192 * hammer_vop_print { vp }
1193 */
427e5fc6
MD
1194static
1195int
66325755 1196hammer_vop_print(struct vop_print_args *ap)
427e5fc6
MD
1197{
1198 return EOPNOTSUPP;
1199}
1200
66325755 1201/*
6b4f890b 1202 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
66325755 1203 */
427e5fc6
MD
1204static
1205int
66325755 1206hammer_vop_readdir(struct vop_readdir_args *ap)
427e5fc6 1207{
36f82b23 1208 struct hammer_transaction trans;
6b4f890b
MD
1209 struct hammer_cursor cursor;
1210 struct hammer_inode *ip;
1211 struct uio *uio;
6b4f890b
MD
1212 hammer_base_elm_t base;
1213 int error;
1214 int cookie_index;
1215 int ncookies;
1216 off_t *cookies;
1217 off_t saveoff;
1218 int r;
1219
1220 ip = VTOI(ap->a_vp);
1221 uio = ap->a_uio;
b3deaf57
MD
1222 saveoff = uio->uio_offset;
1223
1224 if (ap->a_ncookies) {
1225 ncookies = uio->uio_resid / 16 + 1;
1226 if (ncookies > 1024)
1227 ncookies = 1024;
1228 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1229 cookie_index = 0;
1230 } else {
1231 ncookies = -1;
1232 cookies = NULL;
1233 cookie_index = 0;
1234 }
1235
36f82b23
MD
1236 hammer_simple_transaction(&trans, ip->hmp);
1237
b3deaf57
MD
1238 /*
1239 * Handle artificial entries
1240 */
1241 error = 0;
1242 if (saveoff == 0) {
1243 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1244 if (r)
1245 goto done;
1246 if (cookies)
1247 cookies[cookie_index] = saveoff;
1248 ++saveoff;
1249 ++cookie_index;
1250 if (cookie_index == ncookies)
1251 goto done;
1252 }
1253 if (saveoff == 1) {
1254 if (ip->ino_data.parent_obj_id) {
1255 r = vop_write_dirent(&error, uio,
1256 ip->ino_data.parent_obj_id,
1257 DT_DIR, 2, "..");
1258 } else {
1259 r = vop_write_dirent(&error, uio,
1260 ip->obj_id, DT_DIR, 2, "..");
1261 }
1262 if (r)
1263 goto done;
1264 if (cookies)
1265 cookies[cookie_index] = saveoff;
1266 ++saveoff;
1267 ++cookie_index;
1268 if (cookie_index == ncookies)
1269 goto done;
1270 }
6b4f890b
MD
1271
1272 /*
1273 * Key range (begin and end inclusive) to scan. Directory keys
1274 * directly translate to a 64 bit 'seek' position.
1275 */
bcac4bbb 1276 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
5a930e66
MD
1277 cursor.key_beg.localization = ip->obj_localization +
1278 HAMMER_LOCALIZE_MISC;
6b4f890b 1279 cursor.key_beg.obj_id = ip->obj_id;
d5530d22 1280 cursor.key_beg.create_tid = 0;
6b4f890b
MD
1281 cursor.key_beg.delete_tid = 0;
1282 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1283 cursor.key_beg.obj_type = 0;
b3deaf57 1284 cursor.key_beg.key = saveoff;
6b4f890b
MD
1285
1286 cursor.key_end = cursor.key_beg;
1287 cursor.key_end.key = HAMMER_MAX_KEY;
d5530d22
MD
1288 cursor.asof = ip->obj_asof;
1289 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
6b4f890b 1290
4e17f465 1291 error = hammer_ip_first(&cursor);
6b4f890b
MD
1292
1293 while (error == 0) {
11ad5ade 1294 error = hammer_ip_resolve_data(&cursor);
6b4f890b
MD
1295 if (error)
1296 break;
11ad5ade 1297 base = &cursor.leaf->base;
6b4f890b 1298 saveoff = base->key;
11ad5ade 1299 KKASSERT(cursor.leaf->data_len > HAMMER_ENTRY_NAME_OFF);
6b4f890b 1300
7a04d74f
MD
1301 if (base->obj_id != ip->obj_id)
1302 panic("readdir: bad record at %p", cursor.node);
1303
6b4f890b 1304 r = vop_write_dirent(
11ad5ade
MD
1305 &error, uio, cursor.data->entry.obj_id,
1306 hammer_get_dtype(cursor.leaf->base.obj_type),
1307 cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF ,
1308 (void *)cursor.data->entry.name);
6b4f890b
MD
1309 if (r)
1310 break;
1311 ++saveoff;
1312 if (cookies)
1313 cookies[cookie_index] = base->key;
1314 ++cookie_index;
1315 if (cookie_index == ncookies)
1316 break;
1317 error = hammer_ip_next(&cursor);
1318 }
1319 hammer_done_cursor(&cursor);
1320
b3deaf57 1321done:
b84de5af 1322 hammer_done_transaction(&trans);
36f82b23 1323
6b4f890b
MD
1324 if (ap->a_eofflag)
1325 *ap->a_eofflag = (error == ENOENT);
6b4f890b
MD
1326 uio->uio_offset = saveoff;
1327 if (error && cookie_index == 0) {
b3deaf57
MD
1328 if (error == ENOENT)
1329 error = 0;
6b4f890b
MD
1330 if (cookies) {
1331 kfree(cookies, M_TEMP);
1332 *ap->a_ncookies = 0;
1333 *ap->a_cookies = NULL;
1334 }
1335 } else {
7a04d74f
MD
1336 if (error == ENOENT)
1337 error = 0;
6b4f890b
MD
1338 if (cookies) {
1339 *ap->a_ncookies = cookie_index;
1340 *ap->a_cookies = cookies;
1341 }
1342 }
1343 return(error);
427e5fc6
MD
1344}
1345
66325755
MD
1346/*
1347 * hammer_vop_readlink { vp, uio, cred }
1348 */
427e5fc6
MD
1349static
1350int
66325755 1351hammer_vop_readlink(struct vop_readlink_args *ap)
427e5fc6 1352{
36f82b23 1353 struct hammer_transaction trans;
7a04d74f
MD
1354 struct hammer_cursor cursor;
1355 struct hammer_inode *ip;
1356 int error;
1357
1358 ip = VTOI(ap->a_vp);
36f82b23 1359
2f85fa4d
MD
1360 /*
1361 * Shortcut if the symlink data was stuffed into ino_data.
1362 */
1363 if (ip->ino_data.size <= HAMMER_INODE_BASESYMLEN) {
1364 error = uiomove(ip->ino_data.ext.symlink,
1365 ip->ino_data.size, ap->a_uio);
1366 return(error);
1367 }
36f82b23 1368
2f85fa4d
MD
1369 /*
1370 * Long version
1371 */
1372 hammer_simple_transaction(&trans, ip->hmp);
bcac4bbb 1373 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
7a04d74f
MD
1374
1375 /*
1376 * Key range (begin and end inclusive) to scan. Directory keys
1377 * directly translate to a 64 bit 'seek' position.
1378 */
5a930e66
MD
1379 cursor.key_beg.localization = ip->obj_localization +
1380 HAMMER_LOCALIZE_MISC;
7a04d74f 1381 cursor.key_beg.obj_id = ip->obj_id;
d5530d22 1382 cursor.key_beg.create_tid = 0;
7a04d74f
MD
1383 cursor.key_beg.delete_tid = 0;
1384 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1385 cursor.key_beg.obj_type = 0;
1386 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
d5530d22
MD
1387 cursor.asof = ip->obj_asof;
1388 cursor.flags |= HAMMER_CURSOR_ASOF;
7a04d74f 1389
45a014dc 1390 error = hammer_ip_lookup(&cursor);
7a04d74f
MD
1391 if (error == 0) {
1392 error = hammer_ip_resolve_data(&cursor);
1393 if (error == 0) {
11ad5ade
MD
1394 KKASSERT(cursor.leaf->data_len >=
1395 HAMMER_SYMLINK_NAME_OFF);
1396 error = uiomove(cursor.data->symlink.name,
1397 cursor.leaf->data_len -
1398 HAMMER_SYMLINK_NAME_OFF,
7a04d74f
MD
1399 ap->a_uio);
1400 }
1401 }
1402 hammer_done_cursor(&cursor);
b84de5af 1403 hammer_done_transaction(&trans);
7a04d74f 1404 return(error);
427e5fc6
MD
1405}
1406
66325755
MD
1407/*
1408 * hammer_vop_nremove { nch, dvp, cred }
1409 */
427e5fc6
MD
1410static
1411int
66325755 1412hammer_vop_nremove(struct vop_nremove_args *ap)
427e5fc6 1413{
b84de5af 1414 struct hammer_transaction trans;
e63644f0 1415 struct hammer_inode *dip;
b84de5af
MD
1416 int error;
1417
e63644f0
MD
1418 dip = VTOI(ap->a_dvp);
1419
1420 if (hammer_nohistory(dip) == 0 &&
1421 (error = hammer_checkspace(dip->hmp)) != 0) {
1422 return (error);
1423 }
1424
1425 hammer_start_transaction(&trans, dip->hmp);
b84de5af
MD
1426 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1427 hammer_done_transaction(&trans);
1428
1429 return (error);
427e5fc6
MD
1430}
1431
66325755
MD
1432/*
1433 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1434 */
427e5fc6
MD
1435static
1436int
66325755 1437hammer_vop_nrename(struct vop_nrename_args *ap)
427e5fc6 1438{
8cd0a023
MD
1439 struct hammer_transaction trans;
1440 struct namecache *fncp;
1441 struct namecache *tncp;
1442 struct hammer_inode *fdip;
1443 struct hammer_inode *tdip;
1444 struct hammer_inode *ip;
1445 struct hammer_cursor cursor;
8cd0a023 1446 int64_t namekey;
11ad5ade 1447 int nlen, error;
8cd0a023
MD
1448
1449 fdip = VTOI(ap->a_fdvp);
1450 tdip = VTOI(ap->a_tdvp);
1451 fncp = ap->a_fnch->ncp;
1452 tncp = ap->a_tnch->ncp;
b3deaf57
MD
1453 ip = VTOI(fncp->nc_vp);
1454 KKASSERT(ip != NULL);
d113fda1
MD
1455
1456 if (fdip->flags & HAMMER_INODE_RO)
1457 return (EROFS);
1458 if (tdip->flags & HAMMER_INODE_RO)
1459 return (EROFS);
1460 if (ip->flags & HAMMER_INODE_RO)
1461 return (EROFS);
e63644f0
MD
1462 if ((error = hammer_checkspace(fdip->hmp)) != 0)
1463 return (error);
d113fda1 1464
8cd0a023
MD
1465 hammer_start_transaction(&trans, fdip->hmp);
1466
1467 /*
b3deaf57
MD
1468 * Remove tncp from the target directory and then link ip as
1469 * tncp. XXX pass trans to dounlink
42c7d26b
MD
1470 *
1471 * Force the inode sync-time to match the transaction so it is
1472 * in-sync with the creation of the target directory entry.
8cd0a023 1473 */
b84de5af 1474 error = hammer_dounlink(&trans, ap->a_tnch, ap->a_tdvp, ap->a_cred, 0);
42c7d26b 1475 if (error == 0 || error == ENOENT) {
5a930e66
MD
1476 error = hammer_ip_add_directory(&trans, tdip,
1477 tncp->nc_name, tncp->nc_nlen,
1478 ip);
42c7d26b
MD
1479 if (error == 0) {
1480 ip->ino_data.parent_obj_id = tdip->obj_id;
47637bff 1481 hammer_modify_inode(ip, HAMMER_INODE_DDIRTY);
42c7d26b
MD
1482 }
1483 }
b3deaf57
MD
1484 if (error)
1485 goto failed; /* XXX */
8cd0a023
MD
1486
1487 /*
1488 * Locate the record in the originating directory and remove it.
1489 *
1490 * Calculate the namekey and setup the key range for the scan. This
1491 * works kinda like a chained hash table where the lower 32 bits
1492 * of the namekey synthesize the chain.
1493 *
1494 * The key range is inclusive of both key_beg and key_end.
1495 */
1496 namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
6a37e7e4 1497retry:
bcac4bbb 1498 hammer_init_cursor(&trans, &cursor, &fdip->cache[1], fdip);
5a930e66
MD
1499 cursor.key_beg.localization = fdip->obj_localization +
1500 HAMMER_LOCALIZE_MISC;
8cd0a023
MD
1501 cursor.key_beg.obj_id = fdip->obj_id;
1502 cursor.key_beg.key = namekey;
d5530d22 1503 cursor.key_beg.create_tid = 0;
8cd0a023
MD
1504 cursor.key_beg.delete_tid = 0;
1505 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1506 cursor.key_beg.obj_type = 0;
1507
1508 cursor.key_end = cursor.key_beg;
1509 cursor.key_end.key |= 0xFFFFFFFFULL;
d5530d22
MD
1510 cursor.asof = fdip->obj_asof;
1511 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
8cd0a023
MD
1512
1513 /*
1514 * Scan all matching records (the chain), locate the one matching
a89aec1b 1515 * the requested path component.
8cd0a023
MD
1516 *
1517 * The hammer_ip_*() functions merge in-memory records with on-disk
1518 * records for the purposes of the search.
1519 */
4e17f465 1520 error = hammer_ip_first(&cursor);
a89aec1b 1521 while (error == 0) {
8cd0a023
MD
1522 if (hammer_ip_resolve_data(&cursor) != 0)
1523 break;
11ad5ade
MD
1524 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
1525 KKASSERT(nlen > 0);
1526 if (fncp->nc_nlen == nlen &&
1527 bcmp(fncp->nc_name, cursor.data->entry.name, nlen) == 0) {
8cd0a023
MD
1528 break;
1529 }
a89aec1b 1530 error = hammer_ip_next(&cursor);
8cd0a023 1531 }
8cd0a023
MD
1532
1533 /*
1534 * If all is ok we have to get the inode so we can adjust nlinks.
6a37e7e4
MD
1535 *
1536 * WARNING: hammer_ip_del_directory() may have to terminate the
1537 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1538 * twice.
8cd0a023 1539 */
9944ae54 1540 if (error == 0)
6a37e7e4 1541 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
b84de5af
MD
1542
1543 /*
1544 * XXX A deadlock here will break rename's atomicy for the purposes
1545 * of crash recovery.
1546 */
1547 if (error == EDEADLK) {
b84de5af 1548 hammer_done_cursor(&cursor);
b84de5af
MD
1549 goto retry;
1550 }
1551
1552 /*
1553 * Cleanup and tell the kernel that the rename succeeded.
1554 */
c0ade690 1555 hammer_done_cursor(&cursor);
6a37e7e4
MD
1556 if (error == 0)
1557 cache_rename(ap->a_fnch, ap->a_tnch);
b84de5af 1558
b3deaf57 1559failed:
b84de5af 1560 hammer_done_transaction(&trans);
8cd0a023 1561 return (error);
427e5fc6
MD
1562}
1563
66325755
MD
1564/*
1565 * hammer_vop_nrmdir { nch, dvp, cred }
1566 */
427e5fc6
MD
1567static
1568int
66325755 1569hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
427e5fc6 1570{
b84de5af 1571 struct hammer_transaction trans;
e63644f0 1572 struct hammer_inode *dip;
b84de5af
MD
1573 int error;
1574
e63644f0
MD
1575 dip = VTOI(ap->a_dvp);
1576
1577 if (hammer_nohistory(dip) == 0 &&
1578 (error = hammer_checkspace(dip->hmp)) != 0) {
1579 return (error);
1580 }
1581
1582 hammer_start_transaction(&trans, dip->hmp);
b84de5af
MD
1583 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp, ap->a_cred, 0);
1584 hammer_done_transaction(&trans);
1585
1586 return (error);
427e5fc6
MD
1587}
1588
66325755
MD
1589/*
1590 * hammer_vop_setattr { vp, vap, cred }
1591 */
427e5fc6
MD
1592static
1593int
66325755 1594hammer_vop_setattr(struct vop_setattr_args *ap)
427e5fc6 1595{
8cd0a023
MD
1596 struct hammer_transaction trans;
1597 struct vattr *vap;
1598 struct hammer_inode *ip;
1599 int modflags;
1600 int error;
d5ef456e 1601 int truncating;
4a2796f3
MD
1602 int blksize;
1603 int64_t aligned_size;
8cd0a023 1604 u_int32_t flags;
8cd0a023
MD
1605
1606 vap = ap->a_vap;
1607 ip = ap->a_vp->v_data;
1608 modflags = 0;
1609
1610 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1611 return(EROFS);
d113fda1
MD
1612 if (ip->flags & HAMMER_INODE_RO)
1613 return (EROFS);
e63644f0
MD
1614 if (hammer_nohistory(ip) == 0 &&
1615 (error = hammer_checkspace(ip->hmp)) != 0) {
1616 return (error);
1617 }
8cd0a023
MD
1618
1619 hammer_start_transaction(&trans, ip->hmp);
1620 error = 0;
1621
1622 if (vap->va_flags != VNOVAL) {
1623 flags = ip->ino_data.uflags;
1624 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1625 hammer_to_unix_xid(&ip->ino_data.uid),
1626 ap->a_cred);
1627 if (error == 0) {
1628 if (ip->ino_data.uflags != flags) {
1629 ip->ino_data.uflags = flags;
1630 modflags |= HAMMER_INODE_DDIRTY;
1631 }
1632 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1633 error = 0;
1634 goto done;
1635 }
1636 }
1637 goto done;
1638 }
1639 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1640 error = EPERM;
1641 goto done;
1642 }
7538695e
MD
1643 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
1644 mode_t cur_mode = ip->ino_data.mode;
1645 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1646 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1647 uuid_t uuid_uid;
1648 uuid_t uuid_gid;
1649
1650 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
1651 ap->a_cred,
1652 &cur_uid, &cur_gid, &cur_mode);
1653 if (error == 0) {
1654 hammer_guid_to_uuid(&uuid_uid, cur_uid);
1655 hammer_guid_to_uuid(&uuid_gid, cur_gid);
1656 if (bcmp(&uuid_uid, &ip->ino_data.uid,
1657 sizeof(uuid_uid)) ||
1658 bcmp(&uuid_gid, &ip->ino_data.gid,
1659 sizeof(uuid_gid)) ||
1660 ip->ino_data.mode != cur_mode
1661 ) {
1662 ip->ino_data.uid = uuid_uid;
1663 ip->ino_data.gid = uuid_gid;
1664 ip->ino_data.mode = cur_mode;
1665 }
8cd0a023
MD
1666 modflags |= HAMMER_INODE_DDIRTY;
1667 }
1668 }
11ad5ade 1669 while (vap->va_size != VNOVAL && ip->ino_data.size != vap->va_size) {
8cd0a023
MD
1670 switch(ap->a_vp->v_type) {
1671 case VREG:
11ad5ade 1672 if (vap->va_size == ip->ino_data.size)
d5ef456e 1673 break;
b84de5af
MD
1674 /*
1675 * XXX break atomicy, we can deadlock the backend
1676 * if we do not release the lock. Probably not a
1677 * big deal here.
1678 */
4a2796f3 1679 blksize = hammer_blocksize(vap->va_size);
11ad5ade 1680 if (vap->va_size < ip->ino_data.size) {
4a2796f3 1681 vtruncbuf(ap->a_vp, vap->va_size, blksize);
d5ef456e
MD
1682 truncating = 1;
1683 } else {
c0ade690 1684 vnode_pager_setsize(ap->a_vp, vap->va_size);
d5ef456e 1685 truncating = 0;
c0ade690 1686 }
11ad5ade
MD
1687 ip->ino_data.size = vap->va_size;
1688 modflags |= HAMMER_INODE_DDIRTY;
d5ef456e 1689
b84de5af
MD
1690 /*
1691 * on-media truncation is cached in the inode until
1692 * the inode is synchronized.
1693 */
d5ef456e 1694 if (truncating) {
47637bff 1695 hammer_ip_frontend_trunc(ip, vap->va_size);
0832c9bb
MD
1696 hammer_update_rsv_databufs(ip);
1697#ifdef DEBUG_TRUNCATE
1698 if (HammerTruncIp == NULL)
1699 HammerTruncIp = ip;
1700#endif
b84de5af
MD
1701 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1702 ip->flags |= HAMMER_INODE_TRUNCATED;
1703 ip->trunc_off = vap->va_size;
0832c9bb
MD
1704#ifdef DEBUG_TRUNCATE
1705 if (ip == HammerTruncIp)
1706 kprintf("truncate1 %016llx\n", ip->trunc_off);
1707#endif
b84de5af
MD
1708 } else if (ip->trunc_off > vap->va_size) {
1709 ip->trunc_off = vap->va_size;
0832c9bb
MD
1710#ifdef DEBUG_TRUNCATE
1711 if (ip == HammerTruncIp)
1712 kprintf("truncate2 %016llx\n", ip->trunc_off);
1713#endif
1714 } else {
1715#ifdef DEBUG_TRUNCATE
1716 if (ip == HammerTruncIp)
1717 kprintf("truncate3 %016llx (ignored)\n", vap->va_size);
1718#endif
b84de5af 1719 }
d5ef456e 1720 }
b84de5af 1721
d5ef456e
MD
1722 /*
1723 * If truncating we have to clean out a portion of
b84de5af
MD
1724 * the last block on-disk. We do this in the
1725 * front-end buffer cache.
d5ef456e 1726 */
4a2796f3
MD
1727 aligned_size = (vap->va_size + (blksize - 1)) &
1728 ~(int64_t)(blksize - 1);
b84de5af 1729 if (truncating && vap->va_size < aligned_size) {
d5ef456e
MD
1730 struct buf *bp;
1731 int offset;
1732
4a2796f3 1733 aligned_size -= blksize;
47637bff 1734
4a2796f3 1735 offset = (int)vap->va_size & (blksize - 1);
47637bff 1736 error = bread(ap->a_vp, aligned_size,
4a2796f3 1737 blksize, &bp);
47637bff 1738 hammer_ip_frontend_trunc(ip, aligned_size);
d5ef456e
MD
1739 if (error == 0) {
1740 bzero(bp->b_data + offset,
4a2796f3 1741 blksize - offset);
d5ef456e
MD
1742 bdwrite(bp);
1743 } else {
47637bff 1744 kprintf("ERROR %d\n", error);
d5ef456e
MD
1745 brelse(bp);
1746 }
1747 }
76376933 1748 break;
8cd0a023 1749 case VDATABASE:
b84de5af
MD
1750 if ((ip->flags & HAMMER_INODE_TRUNCATED) == 0) {
1751 ip->flags |= HAMMER_INODE_TRUNCATED;
1752 ip->trunc_off = vap->va_size;
1753 } else if (ip->trunc_off > vap->va_size) {
1754 ip->trunc_off = vap->va_size;
1755 }
47637bff 1756 hammer_ip_frontend_trunc(ip, vap->va_size);
11ad5ade
MD
1757 ip->ino_data.size = vap->va_size;
1758 modflags |= HAMMER_INODE_DDIRTY;
8cd0a023
MD
1759 break;
1760 default:
1761 error = EINVAL;
1762 goto done;
1763 }
d26d0ae9 1764 break;
8cd0a023
MD
1765 }
1766 if (vap->va_atime.tv_sec != VNOVAL) {
bcac4bbb 1767 ip->ino_data.atime =
ddfdf542
MD
1768 hammer_timespec_to_time(&vap->va_atime);
1769 modflags |= HAMMER_INODE_ATIME;
8cd0a023
MD
1770 }
1771 if (vap->va_mtime.tv_sec != VNOVAL) {
11ad5ade 1772 ip->ino_data.mtime =
ddfdf542
MD
1773 hammer_timespec_to_time(&vap->va_mtime);
1774 modflags |= HAMMER_INODE_MTIME;
8cd0a023
MD
1775 }
1776 if (vap->va_mode != (mode_t)VNOVAL) {
7538695e
MD
1777 mode_t cur_mode = ip->ino_data.mode;
1778 uid_t cur_uid = hammer_to_unix_xid(&ip->ino_data.uid);
1779 gid_t cur_gid = hammer_to_unix_xid(&ip->ino_data.gid);
1780
1781 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
1782 cur_uid, cur_gid, &cur_mode);
1783 if (error == 0 && ip->ino_data.mode != cur_mode) {
1784 ip->ino_data.mode = cur_mode;
8cd0a023
MD
1785 modflags |= HAMMER_INODE_DDIRTY;
1786 }
1787 }
1788done:
b84de5af 1789 if (error == 0)
47637bff 1790 hammer_modify_inode(ip, modflags);
b84de5af 1791 hammer_done_transaction(&trans);
8cd0a023 1792 return (error);
427e5fc6
MD
1793}
1794
66325755
MD
1795/*
1796 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1797 */
427e5fc6
MD
1798static
1799int
66325755 1800hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
427e5fc6 1801{
7a04d74f
MD
1802 struct hammer_transaction trans;
1803 struct hammer_inode *dip;
1804 struct hammer_inode *nip;
1805 struct nchandle *nch;
1806 hammer_record_t record;
1807 int error;
1808 int bytes;
1809
1810 ap->a_vap->va_type = VLNK;
1811
1812 nch = ap->a_nch;
1813 dip = VTOI(ap->a_dvp);
1814
d113fda1
MD
1815 if (dip->flags & HAMMER_INODE_RO)
1816 return (EROFS);
e63644f0
MD
1817 if ((error = hammer_checkspace(dip->hmp)) != 0)
1818 return (error);
d113fda1 1819
7a04d74f
MD
1820 /*
1821 * Create a transaction to cover the operations we perform.
1822 */
1823 hammer_start_transaction(&trans, dip->hmp);
1824
1825 /*
1826 * Create a new filesystem object of the requested type. The
1827 * returned inode will be referenced but not locked.
1828 */
1829
5a930e66
MD
1830 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred,
1831 dip, 0, &nip);
7a04d74f 1832 if (error) {
b84de5af 1833 hammer_done_transaction(&trans);
7a04d74f
MD
1834 *ap->a_vpp = NULL;
1835 return (error);
1836 }
1837
7a04d74f
MD
1838 /*
1839 * Add a record representing the symlink. symlink stores the link
1840 * as pure data, not a string, and is no \0 terminated.
1841 */
1842 if (error == 0) {
7a04d74f
MD
1843 bytes = strlen(ap->a_target);
1844
2f85fa4d
MD
1845 if (bytes <= HAMMER_INODE_BASESYMLEN) {
1846 bcopy(ap->a_target, nip->ino_data.ext.symlink, bytes);
1847 } else {
1848 record = hammer_alloc_mem_record(nip, bytes);
1849 record->type = HAMMER_MEM_RECORD_GENERAL;
1850
5a930e66
MD
1851 record->leaf.base.localization = nip->obj_localization +
1852 HAMMER_LOCALIZE_MISC;
2f85fa4d
MD
1853 record->leaf.base.key = HAMMER_FIXKEY_SYMLINK;
1854 record->leaf.base.rec_type = HAMMER_RECTYPE_FIX;
1855 record->leaf.data_len = bytes;
1856 KKASSERT(HAMMER_SYMLINK_NAME_OFF == 0);
1857 bcopy(ap->a_target, record->data->symlink.name, bytes);
1858 error = hammer_ip_add_record(&trans, record);
1859 }
42c7d26b
MD
1860
1861 /*
1862 * Set the file size to the length of the link.
1863 */
1864 if (error == 0) {
11ad5ade 1865 nip->ino_data.size = bytes;
47637bff 1866 hammer_modify_inode(nip, HAMMER_INODE_DDIRTY);
42c7d26b 1867 }
7a04d74f 1868 }
1f07f686 1869 if (error == 0)
5a930e66
MD
1870 error = hammer_ip_add_directory(&trans, dip, nch->ncp->nc_name,
1871 nch->ncp->nc_nlen, nip);
7a04d74f
MD
1872
1873 /*
1874 * Finish up.
1875 */
1876 if (error) {
1877 hammer_rel_inode(nip, 0);
7a04d74f
MD
1878 *ap->a_vpp = NULL;
1879 } else {
e8599db1 1880 error = hammer_get_vnode(nip, ap->a_vpp);
7a04d74f
MD
1881 hammer_rel_inode(nip, 0);
1882 if (error == 0) {
1883 cache_setunresolved(ap->a_nch);
1884 cache_setvp(ap->a_nch, *ap->a_vpp);
1885 }
1886 }
b84de5af 1887 hammer_done_transaction(&trans);
7a04d74f 1888 return (error);
427e5fc6
MD
1889}
1890
66325755
MD
1891/*
1892 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1893 */
427e5fc6
MD
1894static
1895int
66325755 1896hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
427e5fc6 1897{
b84de5af 1898 struct hammer_transaction trans;
e63644f0 1899 struct hammer_inode *dip;
b84de5af
MD
1900 int error;
1901
e63644f0
MD
1902 dip = VTOI(ap->a_dvp);
1903
1904 if (hammer_nohistory(dip) == 0 &&
1905 (error = hammer_checkspace(dip->hmp)) != 0) {
1906 return (error);
1907 }
1908
1909 hammer_start_transaction(&trans, dip->hmp);
b84de5af
MD
1910 error = hammer_dounlink(&trans, ap->a_nch, ap->a_dvp,
1911 ap->a_cred, ap->a_flags);
1912 hammer_done_transaction(&trans);
1913
1914 return (error);
427e5fc6
MD
1915}
1916
7dc57964
MD
1917/*
1918 * hammer_vop_ioctl { vp, command, data, fflag, cred }
1919 */
1920static
1921int
1922hammer_vop_ioctl(struct vop_ioctl_args *ap)
1923{
1924 struct hammer_inode *ip = ap->a_vp->v_data;
1925
1926 return(hammer_ioctl(ip, ap->a_command, ap->a_data,
1927 ap->a_fflag, ap->a_cred));
1928}
1929
513ca7d7
MD
1930static
1931int
1932hammer_vop_mountctl(struct vop_mountctl_args *ap)
1933{
1934 struct mount *mp;
1935 int error;
1936
1937 mp = ap->a_head.a_ops->head.vv_mount;
1938
1939 switch(ap->a_op) {
1940 case MOUNTCTL_SET_EXPORT:
1941 if (ap->a_ctllen != sizeof(struct export_args))
1942 error = EINVAL;
1943 error = hammer_vfs_export(mp, ap->a_op,
1944 (const struct export_args *)ap->a_ctl);
1945 break;
1946 default:
1947 error = journal_mountctl(ap);
1948 break;
1949 }
1950 return(error);
1951}
1952
66325755
MD
1953/*
1954 * hammer_vop_strategy { vp, bio }
8cd0a023
MD
1955 *
1956 * Strategy call, used for regular file read & write only. Note that the
1957 * bp may represent a cluster.
1958 *
1959 * To simplify operation and allow better optimizations in the future,
1960 * this code does not make any assumptions with regards to buffer alignment
1961 * or size.
66325755 1962 */
427e5fc6
MD
1963static
1964int
66325755 1965hammer_vop_strategy(struct vop_strategy_args *ap)
427e5fc6 1966{
8cd0a023
MD
1967 struct buf *bp;
1968 int error;
1969
1970 bp = ap->a_bio->bio_buf;
1971
1972 switch(bp->b_cmd) {
1973 case BUF_CMD_READ:
1974 error = hammer_vop_strategy_read(ap);
1975 break;
1976 case BUF_CMD_WRITE:
1977 error = hammer_vop_strategy_write(ap);
1978 break;
1979 default:
059819e3
MD
1980 bp->b_error = error = EINVAL;
1981 bp->b_flags |= B_ERROR;
1982 biodone(ap->a_bio);
8cd0a023
MD
1983 break;
1984 }
8cd0a023 1985 return (error);
427e5fc6
MD
1986}
1987
8cd0a023
MD
1988/*
1989 * Read from a regular file. Iterate the related records and fill in the
1990 * BIO/BUF. Gaps are zero-filled.
1991 *
1992 * The support code in hammer_object.c should be used to deal with mixed
1993 * in-memory and on-disk records.
1994 *
4a2796f3
MD
1995 * NOTE: Can be called from the cluster code with an oversized buf.
1996 *
8cd0a023
MD
1997 * XXX atime update
1998 */
1999static
2000int
2001hammer_vop_strategy_read(struct vop_strategy_args *ap)
2002{
36f82b23
MD
2003 struct hammer_transaction trans;
2004 struct hammer_inode *ip;
8cd0a023 2005 struct hammer_cursor cursor;
8cd0a023 2006 hammer_base_elm_t base;
4a2796f3 2007 hammer_off_t disk_offset;
8cd0a023 2008 struct bio *bio;
a99b9ea2 2009 struct bio *nbio;
8cd0a023
MD
2010 struct buf *bp;
2011 int64_t rec_offset;
a89aec1b 2012 int64_t ran_end;
195c19a1 2013 int64_t tmp64;
8cd0a023
MD
2014 int error;
2015 int boff;
2016 int roff;
2017 int n;
2018
2019 bio = ap->a_bio;
2020 bp = bio->bio_buf;
36f82b23 2021 ip = ap->a_vp->v_data;
8cd0a023 2022
a99b9ea2
MD
2023 /*
2024 * The zone-2 disk offset may have been set by the cluster code via
4a2796f3 2025 * a BMAP operation, or else should be NOOFFSET.
a99b9ea2 2026 *
4a2796f3 2027 * Checking the high bits for a match against zone-2 should suffice.
a99b9ea2
MD
2028 */
2029 nbio = push_bio(bio);
6aeaa7bd
MD
2030 if ((nbio->bio_offset & HAMMER_OFF_ZONE_MASK) ==
2031 HAMMER_ZONE_RAW_BUFFER) {
4a2796f3 2032 error = hammer_io_direct_read(ip->hmp, nbio);
a99b9ea2
MD
2033 return (error);
2034 }
2035
2036 /*
4a2796f3
MD
2037 * Well, that sucked. Do it the hard way. If all the stars are
2038 * aligned we may still be able to issue a direct-read.
a99b9ea2 2039 */
36f82b23 2040 hammer_simple_transaction(&trans, ip->hmp);
47637bff 2041 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
8cd0a023
MD
2042
2043 /*
2044 * Key range (begin and end inclusive) to scan. Note that the key's
c0ade690
MD
2045 * stored in the actual records represent BASE+LEN, not BASE. The
2046 * first record containing bio_offset will have a key > bio_offset.
8cd0a023 2047 */
5a930e66
MD
2048 cursor.key_beg.localization = ip->obj_localization +
2049 HAMMER_LOCALIZE_MISC;
8cd0a023 2050 cursor.key_beg.obj_id = ip->obj_id;
d5530d22 2051 cursor.key_beg.create_tid = 0;
8cd0a023 2052 cursor.key_beg.delete_tid = 0;
8cd0a023 2053 cursor.key_beg.obj_type = 0;
c0ade690 2054 cursor.key_beg.key = bio->bio_offset + 1;
d5530d22 2055 cursor.asof = ip->obj_asof;
bf3b416b 2056 cursor.flags |= HAMMER_CURSOR_ASOF;
8cd0a023
MD
2057
2058 cursor.key_end = cursor.key_beg;
11ad5ade 2059 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
b84de5af 2060#if 0
11ad5ade 2061 if (ip->ino_data.obj_type == HAMMER_OBJTYPE_DBFILE) {
a89aec1b
MD
2062 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
2063 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
2064 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
b84de5af
MD
2065 } else
2066#endif
2067 {
c0ade690 2068 ran_end = bio->bio_offset + bp->b_bufsize;
a89aec1b
MD
2069 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2070 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
195c19a1
MD
2071 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2072 if (tmp64 < ran_end)
a89aec1b
MD
2073 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2074 else
7f7c1f84 2075 cursor.key_end.key = ran_end + MAXPHYS + 1;
a89aec1b 2076 }
d26d0ae9 2077 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
8cd0a023 2078
4e17f465 2079 error = hammer_ip_first(&cursor);
8cd0a023
MD
2080 boff = 0;
2081
a89aec1b 2082 while (error == 0) {
47637bff
MD
2083 /*
2084 * Get the base file offset of the record. The key for
2085 * data records is (base + bytes) rather then (base).
2086 */
11ad5ade 2087 base = &cursor.leaf->base;
11ad5ade 2088 rec_offset = base->key - cursor.leaf->data_len;
8cd0a023 2089
66325755 2090 /*
a89aec1b 2091 * Calculate the gap, if any, and zero-fill it.
1fef775e
MD
2092 *
2093 * n is the offset of the start of the record verses our
2094 * current seek offset in the bio.
66325755 2095 */
8cd0a023
MD
2096 n = (int)(rec_offset - (bio->bio_offset + boff));
2097 if (n > 0) {
a89aec1b
MD
2098 if (n > bp->b_bufsize - boff)
2099 n = bp->b_bufsize - boff;
8cd0a023
MD
2100 bzero((char *)bp->b_data + boff, n);
2101 boff += n;
2102 n = 0;
66325755 2103 }
8cd0a023
MD
2104
2105 /*
2106 * Calculate the data offset in the record and the number
2107 * of bytes we can copy.
a89aec1b 2108 *
1fef775e
MD
2109 * There are two degenerate cases. First, boff may already
2110 * be at bp->b_bufsize. Secondly, the data offset within
2111 * the record may exceed the record's size.
8cd0a023
MD
2112 */
2113 roff = -n;
b84de5af 2114 rec_offset += roff;
11ad5ade 2115 n = cursor.leaf->data_len - roff;
1fef775e
MD
2116 if (n <= 0) {
2117 kprintf("strategy_read: bad n=%d roff=%d\n", n, roff);
2118 n = 0;
2119 } else if (n > bp->b_bufsize - boff) {
8cd0a023 2120 n = bp->b_bufsize - boff;
1fef775e 2121 }
059819e3 2122
b84de5af 2123 /*
47637bff
MD
2124 * Deal with cached truncations. This cool bit of code
2125 * allows truncate()/ftruncate() to avoid having to sync
2126 * the file.
2127 *
2128 * If the frontend is truncated then all backend records are
2129 * subject to the frontend's truncation.
2130 *
2131 * If the backend is truncated then backend records on-disk
2132 * (but not in-memory) are subject to the backend's
2133 * truncation. In-memory records owned by the backend
2134 * represent data written after the truncation point on the
2135 * backend and must not be truncated.
2136 *
2137 * Truncate operations deal with frontend buffer cache
2138 * buffers and frontend-owned in-memory records synchronously.
b84de5af 2139 */
47637bff
MD
2140 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2141 if (hammer_cursor_ondisk(&cursor) ||
2142 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2143 if (ip->trunc_off <= rec_offset)
2144 n = 0;
2145 else if (ip->trunc_off < rec_offset + n)
2146 n = (int)(ip->trunc_off - rec_offset);
2147 }
2148 }
2149 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2150 if (hammer_cursor_ondisk(&cursor)) {
2151 if (ip->sync_trunc_off <= rec_offset)
2152 n = 0;
2153 else if (ip->sync_trunc_off < rec_offset + n)
2154 n = (int)(ip->sync_trunc_off - rec_offset);
2155 }
2156 }
b84de5af
MD
2157
2158 /*
47637bff
MD
2159 * Try to issue a direct read into our bio if possible,
2160 * otherwise resolve the element data into a hammer_buffer
2161 * and copy.
4a2796f3
MD
2162 *
2163 * The buffer on-disk should be zerod past any real
2164 * truncation point, but may not be for any synthesized
2165 * truncation point from above.
b84de5af 2166 */
4a2796f3 2167 if (boff == 0 && n == bp->b_bufsize &&
a99b9ea2 2168 ((cursor.leaf->data_offset + roff) & HAMMER_BUFMASK) == 0) {
4a2796f3
MD
2169 disk_offset = hammer_blockmap_lookup(
2170 trans.hmp,
2171 cursor.leaf->data_offset + roff,
2172 &error);
2173 if (error)
2174 break;
2175 nbio->bio_offset = disk_offset;
2176 error = hammer_io_direct_read(trans.hmp, nbio);
47637bff
MD
2177 goto done;
2178 } else if (n) {
2179 error = hammer_ip_resolve_data(&cursor);
2180 if (error == 0) {
2181 bcopy((char *)cursor.data + roff,
2182 (char *)bp->b_data + boff, n);
2183 }
b84de5af 2184 }
47637bff
MD
2185 if (error)
2186 break;
2187
2188 /*
2189 * Iterate until we have filled the request.
2190 */
2191 boff += n;
8cd0a023 2192 if (boff == bp->b_bufsize)
66325755 2193 break;
a89aec1b 2194 error = hammer_ip_next(&cursor);
66325755
MD
2195 }
2196
2197 /*
8cd0a023 2198 * There may have been a gap after the last record
66325755 2199 */
8cd0a023
MD
2200 if (error == ENOENT)
2201 error = 0;
2202 if (error == 0 && boff != bp->b_bufsize) {
7f7c1f84 2203 KKASSERT(boff < bp->b_bufsize);
8cd0a023
MD
2204 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
2205 /* boff = bp->b_bufsize; */
2206 }
2207 bp->b_resid = 0;
059819e3
MD
2208 bp->b_error = error;
2209 if (error)
2210 bp->b_flags |= B_ERROR;
2211 biodone(ap->a_bio);
47637bff
MD
2212
2213done:
2214 if (cursor.node)
bcac4bbb 2215 hammer_cache_node(&ip->cache[1], cursor.node);
47637bff
MD
2216 hammer_done_cursor(&cursor);
2217 hammer_done_transaction(&trans);
8cd0a023
MD
2218 return(error);
2219}
2220
a99b9ea2
MD
2221/*
2222 * BMAP operation - used to support cluster_read() only.
2223 *
2224 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
2225 *
2226 * This routine may return EOPNOTSUPP if the opration is not supported for
2227 * the specified offset. The contents of the pointer arguments do not
2228 * need to be initialized in that case.
2229 *
2230 * If a disk address is available and properly aligned return 0 with
2231 * *doffsetp set to the zone-2 address, and *runp / *runb set appropriately
2232 * to the run-length relative to that offset. Callers may assume that
2233 * *doffsetp is valid if 0 is returned, even if *runp is not sufficiently
2234 * large, so return EOPNOTSUPP if it is not sufficiently large.
2235 */
2236static
2237int
2238hammer_vop_bmap(struct vop_bmap_args *ap)
2239{
2240 struct hammer_transaction trans;
2241 struct hammer_inode *ip;
2242 struct hammer_cursor cursor;
2243 hammer_base_elm_t base;
2244 int64_t rec_offset;
2245 int64_t ran_end;
2246 int64_t tmp64;
2247 int64_t base_offset;
2248 int64_t base_disk_offset;
2249 int64_t last_offset;
2250 hammer_off_t last_disk_offset;
2251 hammer_off_t disk_offset;
2252 int rec_len;
2253 int error;
4a2796f3 2254 int blksize;
a99b9ea2
MD
2255
2256 ip = ap->a_vp->v_data;
2257
2258 /*
2259 * We can only BMAP regular files. We can't BMAP database files,
2260 * directories, etc.
2261 */
2262 if (ip->ino_data.obj_type != HAMMER_OBJTYPE_REGFILE)
2263 return(EOPNOTSUPP);
2264
2265 /*
2266 * bmap is typically called with runp/runb both NULL when used
2267 * for writing. We do not support BMAP for writing atm.
2268 */
4a2796f3 2269 if (ap->a_cmd != BUF_CMD_READ)
a99b9ea2
MD
2270 return(EOPNOTSUPP);
2271
2272 /*
2273 * Scan the B-Tree to acquire blockmap addresses, then translate
2274 * to raw addresses.
2275 */
2276 hammer_simple_transaction(&trans, ip->hmp);
cb51be26
MD
2277#if 0
2278 kprintf("bmap_beg %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]);
2279#endif
a99b9ea2
MD
2280 hammer_init_cursor(&trans, &cursor, &ip->cache[1], ip);
2281
2282 /*
2283 * Key range (begin and end inclusive) to scan. Note that the key's
2284 * stored in the actual records represent BASE+LEN, not BASE. The
2285 * first record containing bio_offset will have a key > bio_offset.
2286 */
5a930e66
MD
2287 cursor.key_beg.localization = ip->obj_localization +
2288 HAMMER_LOCALIZE_MISC;
a99b9ea2
MD
2289 cursor.key_beg.obj_id = ip->obj_id;
2290 cursor.key_beg.create_tid = 0;
2291 cursor.key_beg.delete_tid = 0;
2292 cursor.key_beg.obj_type = 0;
2293 if (ap->a_runb)
2294 cursor.key_beg.key = ap->a_loffset - MAXPHYS + 1;
2295 else
2296 cursor.key_beg.key = ap->a_loffset + 1;
2297 if (cursor.key_beg.key < 0)
2298 cursor.key_beg.key = 0;
2299 cursor.asof = ip->obj_asof;
bf3b416b 2300 cursor.flags |= HAMMER_CURSOR_ASOF;
a99b9ea2
MD
2301
2302 cursor.key_end = cursor.key_beg;
2303 KKASSERT(ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE);
2304
2305 ran_end = ap->a_loffset + MAXPHYS;
2306 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
2307 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
2308 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
2309 if (tmp64 < ran_end)
2310 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
2311 else
2312 cursor.key_end.key = ran_end + MAXPHYS + 1;
2313
2314 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
2315
2316 error = hammer_ip_first(&cursor);
2317 base_offset = last_offset = 0;
2318 base_disk_offset = last_disk_offset = 0;
2319
2320 while (error == 0) {
2321 /*
2322 * Get the base file offset of the record. The key for
2323 * data records is (base + bytes) rather then (base).
4a2796f3
MD
2324 *
2325 * NOTE: rec_offset + rec_len may exceed the end-of-file.
2326 * The extra bytes should be zero on-disk and the BMAP op
2327 * should still be ok.
a99b9ea2
MD
2328 */
2329 base = &cursor.leaf->base;
2330 rec_offset = base->key - cursor.leaf->data_len;
2331 rec_len = cursor.leaf->data_len;
2332
2333 /*
4a2796f3
MD
2334 * Incorporate any cached truncation.
2335 *
2336 * NOTE: Modifications to rec_len based on synthesized
2337 * truncation points remove the guarantee that any extended
2338 * data on disk is zero (since the truncations may not have
2339 * taken place on-media yet).
a99b9ea2
MD
2340 */
2341 if (ip->flags & HAMMER_INODE_TRUNCATED) {
2342 if (hammer_cursor_ondisk(&cursor) ||
2343 cursor.iprec->flush_state == HAMMER_FST_FLUSH) {
2344 if (ip->trunc_off <= rec_offset)
2345 rec_len = 0;
2346 else if (ip->trunc_off < rec_offset + rec_len)
2347 rec_len = (int)(ip->trunc_off - rec_offset);
2348 }
2349 }
2350 if (ip->sync_flags & HAMMER_INODE_TRUNCATED) {
2351 if (hammer_cursor_ondisk(&cursor)) {
2352 if (ip->sync_trunc_off <= rec_offset)
2353 rec_len = 0;
2354 else if (ip->sync_trunc_off < rec_offset + rec_len)
2355 rec_len = (int)(ip->sync_trunc_off - rec_offset);
2356 }
2357 }
2358
2359 /*
2360 * Accumulate information. If we have hit a discontiguous
2361 * block reset base_offset unless we are already beyond the
2362 * requested offset. If we are, that's it, we stop.
2363 */
2364 disk_offset = hammer_blockmap_lookup(trans.hmp,
2365 cursor.leaf->data_offset,
2366 &error);
2367 if (error)
2368 break;
2369 if (rec_offset != last_offset ||
2370 disk_offset != last_disk_offset) {
2371 if (rec_offset > ap->a_loffset)
2372 break;
2373 base_offset = rec_offset;
2374 base_disk_offset = disk_offset;
2375 }
2376 last_offset = rec_offset + rec_len;
2377 last_disk_offset = disk_offset + rec_len;
2378
2379 error = hammer_ip_next(&cursor);
2380 }
2381
2382#if 0
2383 kprintf("BMAP %016llx: %016llx - %016llx\n",
2384 ap->a_loffset, base_offset, last_offset);
2385 kprintf("BMAP %16s: %016llx - %016llx\n",
2386 "", base_disk_offset, last_disk_offset);
2387#endif
2388
cb51be26 2389 if (cursor.node) {
bcac4bbb 2390 hammer_cache_node(&ip->cache[1], cursor.node);
cb51be26
MD
2391#if 0
2392 kprintf("bmap_end2 %016llx ip->cache %p\n", ap->a_loffset, ip->cache[1]);
2393#endif
2394 }
a99b9ea2
MD
2395 hammer_done_cursor(&cursor);
2396 hammer_done_transaction(&trans);
2397
4a2796f3
MD
2398 /*
2399 * If we couldn't find any records or the records we did find were
2400 * all behind the requested offset, return failure. A forward
2401 * truncation can leave a hole w/ no on-disk records.
2402 */
2403 if (last_offset == 0 || last_offset < ap->a_loffset)
2404 return (EOPNOTSUPP);
2405
2406 /*
2407 * Figure out the block size at the requested offset and adjust
2408 * our limits so the cluster_read() does not create inappropriately
2409 * sized buffer cache buffers.
2410 */
2411 blksize = hammer_blocksize(ap->a_loffset);
2412 if (hammer_blocksize(base_offset) != blksize) {
2413 base_offset = hammer_blockdemarc(base_offset, ap->a_loffset);
2414 }
2415 if (last_offset != ap->a_loffset &&
2416 hammer_blocksize(last_offset - 1) != blksize) {
2417 last_offset = hammer_blockdemarc(ap->a_loffset,
2418 last_offset - 1);
2419 }
2420
2421 /*
2422 * Returning EOPNOTSUPP simply prevents the direct-IO optimization
2423 * from occuring.
2424 */
2425 disk_offset = base_disk_offset + (ap->a_loffset - base_offset);
2426
2427 /*
2428 * If doffsetp is not aligned or the forward run size does
2429 * not cover a whole buffer, disallow the direct I/O.
2430 */
2431 if ((disk_offset & HAMMER_BUFMASK) ||
2432 (last_offset - ap->a_loffset) < blksize) {
a99b9ea2
MD
2433 error = EOPNOTSUPP;
2434 } else {
4a2796f3
MD
2435 *ap->a_doffsetp = disk_offset;
2436 if (ap->a_runb) {
2437 *ap->a_runb = ap->a_loffset - base_offset;
2438 KKASSERT(*ap->a_runb >= 0);
a99b9ea2 2439 }
4a2796f3
MD
2440 if (ap->a_runp) {
2441 *ap->a_runp = last_offset - ap->a_loffset;
2442 KKASSERT(*ap->a_runp >= 0);
2443 }
2444 error = 0;
a99b9ea2
MD
2445 }
2446 return(error);
2447}
2448
8cd0a023 2449/*
059819e3 2450 * Write to a regular file. Because this is a strategy call the OS is
bcac4bbb 2451 * trying to actually get data onto the media.
8cd0a023
MD
2452 */
2453static
2454int
2455hammer_vop_strategy_write(struct vop_strategy_args *ap)
2456{
47637bff 2457 hammer_record_t record;
af209b0f 2458 hammer_mount_t hmp;
8cd0a023
MD
2459 hammer_inode_t ip;
2460 struct bio *bio;
2461 struct buf *bp;
0832c9bb
MD
2462 int bytes;
2463 int error;
8cd0a023
MD
2464
2465 bio = ap->a_bio;
2466 bp = bio->bio_buf;
2467 ip = ap->a_vp->v_data;
af209b0f 2468 hmp = ip->hmp;
d113fda1 2469
4a2796f3
MD
2470 KKASSERT(bp->b_bufsize == hammer_blocksize(bio->bio_offset));
2471
059819e3
MD
2472 if (ip->flags & HAMMER_INODE_RO) {
2473 bp->b_error = EROFS;
2474 bp->b_flags |= B_ERROR;
2475 biodone(ap->a_bio);
e63644f0 2476 hammer_cleanup_write_io(ip);
059819e3
MD
2477 return(EROFS);
2478 }
b84de5af 2479
29ce0677
MD
2480 /*
2481 * Interlock with inode destruction (no in-kernel or directory
2482 * topology visibility). If we queue new IO while trying to
2483 * destroy the inode we can deadlock the vtrunc call in
2484 * hammer_inode_unloadable_check().
2485 */
2486 if (ip->flags & (HAMMER_INODE_DELETING|HAMMER_INODE_DELETED)) {
2487 bp->b_resid = 0;
2488 biodone(ap->a_bio);
e63644f0 2489 hammer_cleanup_write_io(ip);
29ce0677
MD
2490 return(0);
2491 }
2492
b84de5af 2493 /*
a99b9ea2
MD
2494 * Reserve space and issue a direct-write from the front-end.
2495 * NOTE: The direct_io code will hammer_bread/bcopy smaller
2496 * allocations.
47637bff 2497 *
a99b9ea2
MD
2498 * An in-memory record will be installed to reference the storage
2499 * until the flusher can get to it.
47637bff
MD
2500 *
2501 * Since we own the high level bio the front-end will not try to
0832c9bb 2502 * do a direct-read until the write completes.
a99b9ea2
MD
2503 *
2504 * NOTE: The only time we do not reserve a full-sized buffers
2505 * worth of data is if the file is small. We do not try to
2506 * allocate a fragment (from the small-data zone) at the end of
2507 * an otherwise large file as this can lead to wildly separated
2508 * data.
47637bff 2509 */
0832c9bb
MD
2510 KKASSERT((bio->bio_offset & HAMMER_BUFMASK) == 0);
2511 KKASSERT(bio->bio_offset < ip->ino_data.size);
a99b9ea2 2512 if (bio->bio_offset || ip->ino_data.size > HAMMER_BUFSIZE / 2)
4a2796f3 2513 bytes = bp->b_bufsize;
b84de5af 2514 else
a99b9ea2 2515 bytes = ((int)ip->ino_data.size + 15) & ~15;
0832c9bb
MD
2516
2517 record = hammer_ip_add_bulk(ip, bio->bio_offset, bp->b_data,
2518 bytes, &error);
2519 if (record) {
af209b0f 2520 hammer_io_direct_write(hmp, &record->leaf, bio);
0832c9bb 2521 hammer_rel_mem_record(record);
4a2796f3
MD
2522 if (ip->rsv_recs > 1 && hmp->rsv_recs > hammer_limit_recs)
2523 hammer_flush_inode(ip, 0);
0832c9bb 2524 } else {
a99b9ea2 2525 bp->b_bio2.bio_offset = NOOFFSET;
0832c9bb
MD
2526 bp->b_error = error;
2527 bp->b_flags |= B_ERROR;
2528 biodone(ap->a_bio);
2529 }
2530 hammer_cleanup_write_io(ip);
2531 return(error);
059819e3
MD
2532}
2533
2534/*
47637bff
MD
2535 * Clean-up after disposing of a dirty frontend buffer's data.
2536 * This is somewhat heuristical so try to be robust.
059819e3 2537 */
0832c9bb 2538static void
e63644f0
MD
2539hammer_cleanup_write_io(hammer_inode_t ip)
2540{
2541 if (ip->rsv_databufs) {
2542 --ip->rsv_databufs;
2543 --ip->hmp->rsv_databufs;
2544 }
2545}
2546
0832c9bb
MD
2547/*
2548 * We can lose track of dirty buffer cache buffers if we truncate, this
2549 * routine will resynchronize the count.
2550 */
2551static
2552void
2553hammer_update_rsv_databufs(hammer_inode_t ip)
2554{
2555 struct buf *bp;
2556 int delta;
2557 int n;
2558
2559 if (ip->vp) {
2560 n = 0;
2561 RB_FOREACH(bp, buf_rb_tree, &ip->vp->v_rbdirty_tree) {
2562 ++n;
2563 }
2564 } else {
2565 n = 0;
2566 }
2567 delta = n - ip->rsv_databufs;
2568 ip->rsv_databufs += delta;
2569 ip->hmp->rsv_databufs += delta;
2570}
2571
8cd0a023
MD
2572/*
2573 * dounlink - disconnect a directory entry
2574 *
2575 * XXX whiteout support not really in yet
2576 */
2577static int
b84de5af
MD
2578hammer_dounlink(hammer_transaction_t trans, struct nchandle *nch,
2579 struct vnode *dvp, struct ucred *cred, int flags)
8cd0a023 2580{
8cd0a023
MD
2581 struct namecache *ncp;
2582 hammer_inode_t dip;
2583 hammer_inode_t ip;
8cd0a023 2584 struct hammer_cursor cursor;
8cd0a023 2585 int64_t namekey;
11ad5ade 2586 int nlen, error;
8cd0a023
MD
2587
2588 /*
2589 * Calculate the namekey and setup the key range for the scan. This
2590 * works kinda like a chained hash table where the lower 32 bits
2591 * of the namekey synthesize the chain.
2592 *
2593 * The key range is inclusive of both key_beg and key_end.
2594 */
2595 dip = VTOI(dvp);
2596 ncp = nch->ncp;
d113fda1
MD
2597
2598 if (dip->flags & HAMMER_INODE_RO)
2599 return (EROFS);
2600
6a37e7e4
MD
2601 namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
2602retry:
bcac4bbb 2603 hammer_init_cursor(trans, &cursor, &dip->cache[1], dip);
5a930e66
MD
2604 cursor.key_beg.localization = dip->obj_localization +
2605 HAMMER_LOCALIZE_MISC;
8cd0a023
MD
2606 cursor.key_beg.obj_id = dip->obj_id;
2607 cursor.key_beg.key = namekey;
d5530d22 2608 cursor.key_beg.create_tid = 0;
8cd0a023
MD
2609 cursor.key_beg.delete_tid = 0;
2610 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
2611 cursor.key_beg.obj_type = 0;
2612
2613 cursor.key_end = cursor.key_beg;
2614 cursor.key_end.key |= 0xFFFFFFFFULL;
d5530d22
MD
2615 cursor.asof = dip->obj_asof;
2616 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
8cd0a023 2617
8cd0a023
MD
2618 /*
2619 * Scan all matching records (the chain), locate the one matching
2620 * the requested path component. info->last_error contains the
2621 * error code on search termination and could be 0, ENOENT, or
2622 * something else.
2623 *
2624 * The hammer_ip_*() functions merge in-memory records with on-disk
2625 * records for the purposes of the search.
2626 */
4e17f465
MD
2627 error = hammer_ip_first(&cursor);
2628
a89aec1b
MD
2629 while (error == 0) {
2630 error = hammer_ip_resolve_data(&cursor);
2631 if (error)
66325755 2632 break;
11ad5ade
MD
2633 nlen = cursor.leaf->data_len - HAMMER_ENTRY_NAME_OFF;
2634 KKASSERT(nlen > 0);
2635 if (ncp->nc_nlen == nlen &&
2636 bcmp(ncp->nc_name, cursor.data->entry.name, nlen) == 0) {
66325755
MD
2637 break;
2638 }
a89aec1b 2639 error = hammer_ip_next(&cursor);
66325755 2640 }
8cd0a023
MD
2641
2642 /*
2643 * If all is ok we have to get the inode so we can adjust nlinks.
269c5eab
MD
2644 * To avoid a deadlock with the flusher we must release the inode
2645 * lock on the directory when acquiring the inode for the entry.
b3deaf57
MD
2646 *
2647 * If the target is a directory, it must be empty.
8cd0a023 2648 */
66325755 2649 if (error == 0) {
269c5eab 2650 hammer_unlock(&cursor.ip->lock);
bcac4bbb 2651 ip = hammer_get_inode(trans, dip, cursor.data->entry.obj_id,
ddfdf542
MD
2652 dip->hmp->asof,
2653 cursor.data->entry.localization,
2654 0, &error);
269c5eab 2655 hammer_lock_sh(&cursor.ip->lock);
46fe7ae1 2656 if (error == ENOENT) {
11ad5ade 2657 kprintf("obj_id %016llx\n", cursor.data->entry.obj_id);
10a5d1ba 2658 Debugger("ENOENT unlinking object that should exist");
46fe7ae1 2659 }
1f07f686
MD
2660
2661 /*
2662 * If we are trying to remove a directory the directory must
2663 * be empty.
2664 *
2665 * WARNING: hammer_ip_check_directory_empty() may have to
2666 * terminate the cursor to avoid a deadlock. It is ok to
2667 * call hammer_done_cursor() twice.
2668 */
11ad5ade 2669 if (error == 0 && ip->ino_data.obj_type ==
b3deaf57 2670 HAMMER_OBJTYPE_DIRECTORY) {
98f7132d 2671 error = hammer_ip_check_directory_empty(trans, ip);
b3deaf57 2672 }
1f07f686 2673
6a37e7e4 2674 /*
1f07f686
MD
2675 * Delete the directory entry.
2676 *
6a37e7e4 2677 * WARNING: hammer_ip_del_directory() may have to terminate
1f07f686 2678 * the cursor to avoid a deadlock. It is ok to call
6a37e7e4
MD
2679 * hammer_done_cursor() twice.
2680 */
b84de5af 2681 if (error == 0) {
b84de5af
MD
2682 error = hammer_ip_del_directory(trans, &cursor,
2683 dip, ip);
b84de5af 2684 }
269c5eab 2685 hammer_done_cursor(&cursor);
8cd0a023
MD
2686 if (error == 0) {
2687 cache_setunresolved(nch);
2688 cache_setvp(nch, NULL);
2689 /* XXX locking */
2690 if (ip->vp)
2691 cache_inval_vp(ip->vp, CINV_DESTROY);
2692 }
af209b0f
MD
2693 if (ip)
2694 hammer_rel_inode(ip, 0);
269c5eab
MD
2695 } else {
2696 hammer_done_cursor(&cursor);
66325755 2697 }
6a37e7e4
MD
2698 if (error == EDEADLK)
2699 goto retry;
9c448776 2700
66325755 2701 return (error);
66325755
MD
2702}
2703
7a04d74f
MD
2704/************************************************************************
2705 * FIFO AND SPECFS OPS *
2706 ************************************************************************
2707 *
2708 */
2709
2710static int
2711hammer_vop_fifoclose (struct vop_close_args *ap)
2712{
2713 /* XXX update itimes */
2714 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
2715}
2716
2717static int
2718hammer_vop_fiforead (struct vop_read_args *ap)
2719{
2720 int error;
2721
2722 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2723 /* XXX update access time */
2724 return (error);
2725}
2726
2727static int
2728hammer_vop_fifowrite (struct vop_write_args *ap)
2729{
2730 int error;
2731
2732 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
2733 /* XXX update access time */
2734 return (error);
2735}
2736
2737static int
2738hammer_vop_specclose (struct vop_close_args *ap)
2739{
2740 /* XXX update itimes */
2741 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2742}
2743
2744static int
2745hammer_vop_specread (struct vop_read_args *ap)
2746{
2747 /* XXX update access time */
2748 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2749}
2750
2751static int
2752hammer_vop_specwrite (struct vop_write_args *ap)
2753{
2754 /* XXX update last change time */
2755 return (VOCALL(&spec_vnode_vops, &ap->a_head));
2756}
2757