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