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