Tiny step to keep ether header in mbuf on ethernet input path:
[dragonfly.git] / sys / vfs / hammer / hammer_vnops.c
CommitLineData
427e5fc6
MD
1/*
2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
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 *
bf686dbe 34 * $DragonFly: src/sys/vfs/hammer/hammer_vnops.c,v 1.35 2008/03/18 05:19:16 dillon Exp $
427e5fc6
MD
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/kernel.h>
40#include <sys/fcntl.h>
41#include <sys/namecache.h>
42#include <sys/vnode.h>
43#include <sys/lockf.h>
44#include <sys/event.h>
45#include <sys/stat.h>
b3deaf57 46#include <sys/dirent.h>
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 *);
78static int hammer_vop_nsymlink(struct vop_nsymlink_args *);
79static int hammer_vop_nwhiteout(struct vop_nwhiteout_args *);
7dc57964 80static int hammer_vop_ioctl(struct vop_ioctl_args *);
513ca7d7 81static int hammer_vop_mountctl(struct vop_mountctl_args *);
427e5fc6 82
7a04d74f
MD
83static int hammer_vop_fifoclose (struct vop_close_args *);
84static int hammer_vop_fiforead (struct vop_read_args *);
85static int hammer_vop_fifowrite (struct vop_write_args *);
86
87static int hammer_vop_specclose (struct vop_close_args *);
88static int hammer_vop_specread (struct vop_read_args *);
89static int hammer_vop_specwrite (struct vop_write_args *);
90
427e5fc6
MD
91struct vop_ops hammer_vnode_vops = {
92 .vop_default = vop_defaultop,
93 .vop_fsync = hammer_vop_fsync,
c0ade690
MD
94 .vop_getpages = vop_stdgetpages,
95 .vop_putpages = vop_stdputpages,
427e5fc6
MD
96 .vop_read = hammer_vop_read,
97 .vop_write = hammer_vop_write,
98 .vop_access = hammer_vop_access,
99 .vop_advlock = hammer_vop_advlock,
100 .vop_close = hammer_vop_close,
101 .vop_ncreate = hammer_vop_ncreate,
102 .vop_getattr = hammer_vop_getattr,
103 .vop_inactive = hammer_vop_inactive,
104 .vop_reclaim = hammer_vop_reclaim,
105 .vop_nresolve = hammer_vop_nresolve,
106 .vop_nlookupdotdot = hammer_vop_nlookupdotdot,
107 .vop_nlink = hammer_vop_nlink,
108 .vop_nmkdir = hammer_vop_nmkdir,
109 .vop_nmknod = hammer_vop_nmknod,
110 .vop_open = hammer_vop_open,
111 .vop_pathconf = hammer_vop_pathconf,
112 .vop_print = hammer_vop_print,
113 .vop_readdir = hammer_vop_readdir,
114 .vop_readlink = hammer_vop_readlink,
115 .vop_nremove = hammer_vop_nremove,
116 .vop_nrename = hammer_vop_nrename,
117 .vop_nrmdir = hammer_vop_nrmdir,
118 .vop_setattr = hammer_vop_setattr,
119 .vop_strategy = hammer_vop_strategy,
120 .vop_nsymlink = hammer_vop_nsymlink,
7dc57964 121 .vop_nwhiteout = hammer_vop_nwhiteout,
513ca7d7
MD
122 .vop_ioctl = hammer_vop_ioctl,
123 .vop_mountctl = hammer_vop_mountctl
427e5fc6
MD
124};
125
7a04d74f
MD
126struct vop_ops hammer_spec_vops = {
127 .vop_default = spec_vnoperate,
128 .vop_fsync = hammer_vop_fsync,
129 .vop_read = hammer_vop_specread,
130 .vop_write = hammer_vop_specwrite,
131 .vop_access = hammer_vop_access,
132 .vop_close = hammer_vop_specclose,
133 .vop_getattr = hammer_vop_getattr,
134 .vop_inactive = hammer_vop_inactive,
135 .vop_reclaim = hammer_vop_reclaim,
136 .vop_setattr = hammer_vop_setattr
137};
138
139struct vop_ops hammer_fifo_vops = {
140 .vop_default = fifo_vnoperate,
141 .vop_fsync = hammer_vop_fsync,
142 .vop_read = hammer_vop_fiforead,
143 .vop_write = hammer_vop_fifowrite,
144 .vop_access = hammer_vop_access,
145 .vop_close = hammer_vop_fifoclose,
146 .vop_getattr = hammer_vop_getattr,
147 .vop_inactive = hammer_vop_inactive,
148 .vop_reclaim = hammer_vop_reclaim,
149 .vop_setattr = hammer_vop_setattr
150};
151
8cd0a023
MD
152static int hammer_dounlink(struct nchandle *nch, struct vnode *dvp,
153 struct ucred *cred, int flags);
154static int hammer_vop_strategy_read(struct vop_strategy_args *ap);
155static int hammer_vop_strategy_write(struct vop_strategy_args *ap);
156
427e5fc6
MD
157#if 0
158static
159int
160hammer_vop_vnoperate(struct vop_generic_args *)
161{
162 return (VOCALL(&hammer_vnode_vops, ap));
163}
164#endif
165
66325755
MD
166/*
167 * hammer_vop_fsync { vp, waitfor }
168 */
427e5fc6
MD
169static
170int
66325755 171hammer_vop_fsync(struct vop_fsync_args *ap)
427e5fc6 172{
c0ade690
MD
173 hammer_inode_t ip;
174 int error;
175
176 ip = VTOI(ap->a_vp);
177 error = hammer_sync_inode(ip, ap->a_waitfor, 0);
178 return (error);
427e5fc6
MD
179}
180
66325755
MD
181/*
182 * hammer_vop_read { vp, uio, ioflag, cred }
183 */
427e5fc6
MD
184static
185int
66325755 186hammer_vop_read(struct vop_read_args *ap)
427e5fc6 187{
66325755 188 struct hammer_transaction trans;
c0ade690 189 hammer_inode_t ip;
66325755
MD
190 off_t offset;
191 struct buf *bp;
192 struct uio *uio;
193 int error;
194 int n;
8cd0a023 195 int seqcount;
66325755
MD
196
197 if (ap->a_vp->v_type != VREG)
198 return (EINVAL);
199 ip = VTOI(ap->a_vp);
200 error = 0;
8cd0a023 201 seqcount = ap->a_ioflag >> 16;
66325755 202
8cd0a023 203 hammer_start_transaction(&trans, ip->hmp);
66325755
MD
204
205 /*
206 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
207 */
208 uio = ap->a_uio;
209 while (uio->uio_resid > 0 && uio->uio_offset < ip->ino_rec.ino_size) {
210 offset = uio->uio_offset & HAMMER_BUFMASK;
c0ade690 211#if 0
8cd0a023
MD
212 error = cluster_read(ap->a_vp, ip->ino_rec.ino_size,
213 uio->uio_offset - offset, HAMMER_BUFSIZE,
214 MAXBSIZE, seqcount, &bp);
c0ade690
MD
215#endif
216 error = bread(ap->a_vp, uio->uio_offset - offset,
217 HAMMER_BUFSIZE, &bp);
66325755
MD
218 if (error) {
219 brelse(bp);
220 break;
221 }
c0ade690 222 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
66325755
MD
223 n = HAMMER_BUFSIZE - offset;
224 if (n > uio->uio_resid)
225 n = uio->uio_resid;
226 if (n > ip->ino_rec.ino_size - uio->uio_offset)
227 n = (int)(ip->ino_rec.ino_size - uio->uio_offset);
228 error = uiomove((char *)bp->b_data + offset, n, uio);
229 if (error) {
8cd0a023 230 bqrelse(bp);
66325755
MD
231 break;
232 }
42c7d26b
MD
233 if ((ip->flags & HAMMER_INODE_RO) == 0 &&
234 (ip->hmp->mp->mnt_flag & MNT_NOATIME) == 0) {
d113fda1
MD
235 ip->ino_rec.ino_atime = trans.tid;
236 hammer_modify_inode(&trans, ip, HAMMER_INODE_ITIMES);
237 }
66325755
MD
238 bqrelse(bp);
239 }
240 hammer_commit_transaction(&trans);
241 return (error);
427e5fc6
MD
242}
243
66325755
MD
244/*
245 * hammer_vop_write { vp, uio, ioflag, cred }
246 */
427e5fc6
MD
247static
248int
66325755 249hammer_vop_write(struct vop_write_args *ap)
427e5fc6 250{
66325755
MD
251 struct hammer_transaction trans;
252 struct hammer_inode *ip;
253 struct uio *uio;
254 off_t offset;
255 struct buf *bp;
256 int error;
257 int n;
c0ade690 258 int flags;
66325755
MD
259
260 if (ap->a_vp->v_type != VREG)
261 return (EINVAL);
262 ip = VTOI(ap->a_vp);
263 error = 0;
264
d113fda1
MD
265 if (ip->flags & HAMMER_INODE_RO)
266 return (EROFS);
267
66325755
MD
268 /*
269 * Create a transaction to cover the operations we perform.
270 */
8cd0a023 271 hammer_start_transaction(&trans, ip->hmp);
66325755
MD
272 uio = ap->a_uio;
273
274 /*
275 * Check append mode
276 */
277 if (ap->a_ioflag & IO_APPEND)
278 uio->uio_offset = ip->ino_rec.ino_size;
279
280 /*
281 * Check for illegal write offsets. Valid range is 0...2^63-1
282 */
9c448776
MD
283 if (uio->uio_offset < 0 || uio->uio_offset + uio->uio_resid <= 0) {
284 hammer_commit_transaction(&trans);
66325755 285 return (EFBIG);
9c448776 286 }
66325755
MD
287
288 /*
289 * Access the data in HAMMER_BUFSIZE blocks via the buffer cache.
290 */
291 while (uio->uio_resid > 0) {
d5ef456e
MD
292 int fixsize = 0;
293
66325755 294 offset = uio->uio_offset & HAMMER_BUFMASK;
d5ef456e
MD
295 n = HAMMER_BUFSIZE - offset;
296 if (n > uio->uio_resid)
297 n = uio->uio_resid;
298 if (uio->uio_offset + n > ip->ino_rec.ino_size) {
299 vnode_pager_setsize(ap->a_vp, uio->uio_offset + n);
300 fixsize = 1;
301 }
302
c0ade690
MD
303 if (uio->uio_segflg == UIO_NOCOPY) {
304 /*
305 * Issuing a write with the same data backing the
306 * buffer. Instantiate the buffer to collect the
307 * backing vm pages, then read-in any missing bits.
308 *
309 * This case is used by vop_stdputpages().
310 */
d5ef456e
MD
311 bp = getblk(ap->a_vp, uio->uio_offset - offset,
312 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
c0ade690
MD
313 if ((bp->b_flags & B_CACHE) == 0) {
314 bqrelse(bp);
315 error = bread(ap->a_vp,
316 uio->uio_offset - offset,
317 HAMMER_BUFSIZE, &bp);
c0ade690
MD
318 }
319 } else if (offset == 0 && uio->uio_resid >= HAMMER_BUFSIZE) {
320 /*
321 * entirely overwrite the buffer
322 */
d5ef456e
MD
323 bp = getblk(ap->a_vp, uio->uio_offset - offset,
324 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
66325755 325 } else if (offset == 0 && uio->uio_offset >= ip->ino_rec.ino_size) {
c0ade690
MD
326 /*
327 * XXX
328 */
d5ef456e
MD
329 bp = getblk(ap->a_vp, uio->uio_offset - offset,
330 HAMMER_BUFSIZE, GETBLK_BHEAVY, 0);
66325755
MD
331 vfs_bio_clrbuf(bp);
332 } else {
c0ade690
MD
333 /*
334 * Partial overwrite, read in any missing bits then
335 * replace the portion being written.
336 */
66325755
MD
337 error = bread(ap->a_vp, uio->uio_offset - offset,
338 HAMMER_BUFSIZE, &bp);
d5ef456e
MD
339 if (error == 0)
340 bheavy(bp);
66325755 341 }
d5ef456e
MD
342 if (error == 0)
343 error = uiomove((char *)bp->b_data + offset, n, uio);
344
345 /*
346 * If we screwed up we have to undo any VM size changes we
347 * made.
348 */
66325755
MD
349 if (error) {
350 brelse(bp);
d5ef456e
MD
351 if (fixsize) {
352 vtruncbuf(ap->a_vp, ip->ino_rec.ino_size,
353 HAMMER_BUFSIZE);
354 }
66325755
MD
355 break;
356 }
c0ade690 357 /* bp->b_flags |= B_CLUSTEROK; temporarily disabled */
66325755
MD
358 if (ip->ino_rec.ino_size < uio->uio_offset) {
359 ip->ino_rec.ino_size = uio->uio_offset;
d113fda1 360 flags = HAMMER_INODE_RDIRTY;
c0ade690
MD
361 vnode_pager_setsize(ap->a_vp, ip->ino_rec.ino_size);
362 } else {
d113fda1 363 flags = 0;
66325755 364 }
d113fda1 365 ip->ino_rec.ino_mtime = trans.tid;
f3b0f382 366 flags |= HAMMER_INODE_ITIMES | HAMMER_INODE_BUFS;
c0ade690 367 hammer_modify_inode(&trans, ip, flags);
32c90105 368
1a51eb21 369#if 0
32c90105
MD
370 /*
371 * The file write must be tagged with the same TID as the
372 * inode, for consistency in case the inode changed size.
373 * This guarantees the on-disk data records will have a
374 * TID <= the inode TID representing the size change.
375 *
376 * If a prior write has not yet flushed, retain its TID.
377 */
378 if (bp->b_tid == 0)
379 bp->b_tid = ip->last_tid;
1a51eb21
MD
380#endif
381 /*
382 * For now we can't use ip->last_tid because we may wind
383 * up trying to flush the same buffer with the same TID
384 * (but different data) multiple times, which will cause
385 * a panic.
386 */
387 if (bp->b_tid == 0)
388 bp->b_tid = trans.tid;
32c90105 389
66325755
MD
390 if (ap->a_ioflag & IO_SYNC) {
391 bwrite(bp);
392 } else if (ap->a_ioflag & IO_DIRECT) {
66325755 393 bawrite(bp);
34d829f7
MD
394 } else if ((ap->a_ioflag >> 16) > 1 &&
395 (uio->uio_offset & HAMMER_BUFMASK) == 0) {
396 /*
397 * If seqcount indicates sequential operation and
398 * we just finished filling a buffer, push it out
399 * now to prevent the buffer cache from becoming
400 * too full, which would trigger non-optimal
401 * flushes.
402 */
403 bawrite(bp);
66325755 404 } else {
66325755
MD
405 bdwrite(bp);
406 }
407 }
408 if (error)
409 hammer_abort_transaction(&trans);
410 else
411 hammer_commit_transaction(&trans);
412 return (error);
427e5fc6
MD
413}
414
66325755
MD
415/*
416 * hammer_vop_access { vp, mode, cred }
417 */
427e5fc6
MD
418static
419int
66325755 420hammer_vop_access(struct vop_access_args *ap)
427e5fc6 421{
66325755
MD
422 struct hammer_inode *ip = VTOI(ap->a_vp);
423 uid_t uid;
424 gid_t gid;
425 int error;
426
427 uid = hammer_to_unix_xid(&ip->ino_data.uid);
428 gid = hammer_to_unix_xid(&ip->ino_data.gid);
429
430 error = vop_helper_access(ap, uid, gid, ip->ino_data.mode,
431 ip->ino_data.uflags);
432 return (error);
427e5fc6
MD
433}
434
66325755
MD
435/*
436 * hammer_vop_advlock { vp, id, op, fl, flags }
437 */
427e5fc6
MD
438static
439int
66325755 440hammer_vop_advlock(struct vop_advlock_args *ap)
427e5fc6 441{
66325755
MD
442 struct hammer_inode *ip = VTOI(ap->a_vp);
443
444 return (lf_advlock(ap, &ip->advlock, ip->ino_rec.ino_size));
427e5fc6
MD
445}
446
66325755
MD
447/*
448 * hammer_vop_close { vp, fflag }
449 */
427e5fc6
MD
450static
451int
66325755 452hammer_vop_close(struct vop_close_args *ap)
427e5fc6 453{
a89aec1b 454 return (vop_stdclose(ap));
427e5fc6
MD
455}
456
66325755
MD
457/*
458 * hammer_vop_ncreate { nch, dvp, vpp, cred, vap }
459 *
460 * The operating system has already ensured that the directory entry
461 * does not exist and done all appropriate namespace locking.
462 */
427e5fc6
MD
463static
464int
66325755 465hammer_vop_ncreate(struct vop_ncreate_args *ap)
427e5fc6 466{
66325755
MD
467 struct hammer_transaction trans;
468 struct hammer_inode *dip;
469 struct hammer_inode *nip;
470 struct nchandle *nch;
471 int error;
472
473 nch = ap->a_nch;
474 dip = VTOI(ap->a_dvp);
475
d113fda1
MD
476 if (dip->flags & HAMMER_INODE_RO)
477 return (EROFS);
478
66325755
MD
479 /*
480 * Create a transaction to cover the operations we perform.
481 */
8cd0a023 482 hammer_start_transaction(&trans, dip->hmp);
66325755
MD
483
484 /*
485 * Create a new filesystem object of the requested type. The
7a04d74f 486 * returned inode will be referenced but not locked.
66325755 487 */
8cd0a023
MD
488
489 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
0b075555
MD
490 if (error)
491 kprintf("hammer_create_inode error %d\n", error);
66325755
MD
492 if (error) {
493 hammer_abort_transaction(&trans);
494 *ap->a_vpp = NULL;
495 return (error);
496 }
66325755
MD
497
498 /*
499 * Add the new filesystem object to the directory. This will also
500 * bump the inode's link count.
501 */
a89aec1b 502 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
0b075555
MD
503 if (error)
504 kprintf("hammer_ip_add_directory error %d\n", error);
66325755
MD
505
506 /*
507 * Finish up.
508 */
509 if (error) {
a89aec1b 510 hammer_rel_inode(nip, 0);
66325755
MD
511 hammer_abort_transaction(&trans);
512 *ap->a_vpp = NULL;
513 } else {
514 hammer_commit_transaction(&trans);
515 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
a89aec1b
MD
516 hammer_rel_inode(nip, 0);
517 if (error == 0) {
518 cache_setunresolved(ap->a_nch);
519 cache_setvp(ap->a_nch, *ap->a_vpp);
520 }
66325755
MD
521 }
522 return (error);
427e5fc6
MD
523}
524
66325755
MD
525/*
526 * hammer_vop_getattr { vp, vap }
527 */
427e5fc6
MD
528static
529int
66325755 530hammer_vop_getattr(struct vop_getattr_args *ap)
427e5fc6 531{
66325755
MD
532 struct hammer_inode *ip = VTOI(ap->a_vp);
533 struct vattr *vap = ap->a_vap;
534
535#if 0
536 if (cache_check_fsmid_vp(ap->a_vp, &ip->fsmid) &&
537 (vp->v_mount->mnt_flag & MNT_RDONLY) == 0 &&
7f7c1f84 538 ip->obj_asof == XXX
66325755
MD
539 ) {
540 /* LAZYMOD XXX */
541 }
542 hammer_itimes(ap->a_vp);
543#endif
544
545 vap->va_fsid = ip->hmp->fsid_udev;
546 vap->va_fileid = ip->ino_rec.base.base.obj_id;
547 vap->va_mode = ip->ino_data.mode;
548 vap->va_nlink = ip->ino_rec.ino_nlinks;
549 vap->va_uid = hammer_to_unix_xid(&ip->ino_data.uid);
550 vap->va_gid = hammer_to_unix_xid(&ip->ino_data.gid);
551 vap->va_rmajor = 0;
552 vap->va_rminor = 0;
553 vap->va_size = ip->ino_rec.ino_size;
554 hammer_to_timespec(ip->ino_rec.ino_atime, &vap->va_atime);
555 hammer_to_timespec(ip->ino_rec.ino_mtime, &vap->va_mtime);
556 hammer_to_timespec(ip->ino_data.ctime, &vap->va_ctime);
557 vap->va_flags = ip->ino_data.uflags;
558 vap->va_gen = 1; /* hammer inums are unique for all time */
bf686dbe
MD
559 vap->va_blocksize = HAMMER_BUFSIZE;
560 vap->va_bytes = (ip->ino_rec.ino_size + 63) & ~63;
66325755
MD
561 vap->va_type = hammer_get_vnode_type(ip->ino_rec.base.base.obj_type);
562 vap->va_filerev = 0; /* XXX */
563 /* mtime uniquely identifies any adjustments made to the file */
564 vap->va_fsmid = ip->ino_rec.ino_mtime;
565 vap->va_uid_uuid = ip->ino_data.uid;
566 vap->va_gid_uuid = ip->ino_data.gid;
567 vap->va_fsid_uuid = ip->hmp->fsid;
568 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
569 VA_FSID_UUID_VALID;
7a04d74f
MD
570
571 switch (ip->ino_rec.base.base.obj_type) {
572 case HAMMER_OBJTYPE_CDEV:
573 case HAMMER_OBJTYPE_BDEV:
574 vap->va_rmajor = ip->ino_data.rmajor;
575 vap->va_rminor = ip->ino_data.rminor;
576 break;
577 default:
578 break;
579 }
580
66325755 581 return(0);
427e5fc6
MD
582}
583
66325755
MD
584/*
585 * hammer_vop_nresolve { nch, dvp, cred }
586 *
587 * Locate the requested directory entry.
588 */
427e5fc6
MD
589static
590int
66325755 591hammer_vop_nresolve(struct vop_nresolve_args *ap)
427e5fc6 592{
66325755 593 struct namecache *ncp;
7f7c1f84
MD
594 hammer_inode_t dip;
595 hammer_inode_t ip;
596 hammer_tid_t asof;
8cd0a023
MD
597 struct hammer_cursor cursor;
598 union hammer_record_ondisk *rec;
66325755
MD
599 struct vnode *vp;
600 int64_t namekey;
601 int error;
7f7c1f84
MD
602 int i;
603 int nlen;
d113fda1 604 int flags;
6a37e7e4 605 u_int64_t obj_id;
7f7c1f84
MD
606
607 /*
608 * Misc initialization, plus handle as-of name extensions. Look for
609 * the '@@' extension. Note that as-of files and directories cannot
610 * be modified.
7f7c1f84
MD
611 */
612 dip = VTOI(ap->a_dvp);
613 ncp = ap->a_nch->ncp;
614 asof = dip->obj_asof;
615 nlen = ncp->nc_nlen;
d113fda1 616 flags = dip->flags;
7f7c1f84
MD
617
618 for (i = 0; i < nlen; ++i) {
619 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
d113fda1 620 asof = hammer_str_to_tid(ncp->nc_name + i + 2);
d113fda1 621 flags |= HAMMER_INODE_RO;
7f7c1f84
MD
622 break;
623 }
624 }
625 nlen = i;
66325755 626
d113fda1
MD
627 /*
628 * If there is no path component the time extension is relative to
629 * dip.
630 */
631 if (nlen == 0) {
61aeeb33
MD
632 ip = hammer_get_inode(dip->hmp, &dip->cache[1], dip->obj_id,
633 asof, flags, &error);
d113fda1
MD
634 if (error == 0) {
635 error = hammer_get_vnode(ip, LK_EXCLUSIVE, &vp);
636 hammer_rel_inode(ip, 0);
637 } else {
638 vp = NULL;
639 }
640 if (error == 0) {
641 vn_unlock(vp);
642 cache_setvp(ap->a_nch, vp);
643 vrele(vp);
644 }
645 return(error);
646 }
647
8cd0a023
MD
648 /*
649 * Calculate the namekey and setup the key range for the scan. This
650 * works kinda like a chained hash table where the lower 32 bits
651 * of the namekey synthesize the chain.
652 *
653 * The key range is inclusive of both key_beg and key_end.
654 */
7f7c1f84 655 namekey = hammer_directory_namekey(ncp->nc_name, nlen);
66325755 656
7dc57964 657 error = hammer_init_cursor_hmp(&cursor, &dip->cache[0], dip->hmp);
8cd0a023
MD
658 cursor.key_beg.obj_id = dip->obj_id;
659 cursor.key_beg.key = namekey;
d5530d22 660 cursor.key_beg.create_tid = 0;
8cd0a023
MD
661 cursor.key_beg.delete_tid = 0;
662 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
663 cursor.key_beg.obj_type = 0;
66325755 664
8cd0a023
MD
665 cursor.key_end = cursor.key_beg;
666 cursor.key_end.key |= 0xFFFFFFFFULL;
d5530d22
MD
667 cursor.asof = asof;
668 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
66325755
MD
669
670 /*
8cd0a023 671 * Scan all matching records (the chain), locate the one matching
a89aec1b 672 * the requested path component.
8cd0a023
MD
673 *
674 * The hammer_ip_*() functions merge in-memory records with on-disk
675 * records for the purposes of the search.
66325755 676 */
7dc57964
MD
677 if (error == 0)
678 error = hammer_ip_first(&cursor, dip);
679
0b075555 680 rec = NULL;
6a37e7e4
MD
681 obj_id = 0;
682
a89aec1b
MD
683 while (error == 0) {
684 error = hammer_ip_resolve_data(&cursor);
685 if (error)
66325755 686 break;
a89aec1b 687 rec = cursor.record;
7f7c1f84 688 if (nlen == rec->entry.base.data_len &&
40043e7f 689 bcmp(ncp->nc_name, cursor.data, nlen) == 0) {
6a37e7e4 690 obj_id = rec->entry.obj_id;
66325755
MD
691 break;
692 }
a89aec1b 693 error = hammer_ip_next(&cursor);
66325755 694 }
6a37e7e4 695 hammer_done_cursor(&cursor);
66325755 696 if (error == 0) {
61aeeb33 697 ip = hammer_get_inode(dip->hmp, &dip->cache[1],
6a37e7e4 698 obj_id, asof, flags, &error);
7f7c1f84
MD
699 if (error == 0) {
700 error = hammer_get_vnode(ip, LK_EXCLUSIVE, &vp);
701 hammer_rel_inode(ip, 0);
702 } else {
703 vp = NULL;
704 }
66325755
MD
705 if (error == 0) {
706 vn_unlock(vp);
707 cache_setvp(ap->a_nch, vp);
708 vrele(vp);
709 }
710 } else if (error == ENOENT) {
711 cache_setvp(ap->a_nch, NULL);
712 }
66325755 713 return (error);
427e5fc6
MD
714}
715
66325755
MD
716/*
717 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
718 *
719 * Locate the parent directory of a directory vnode.
720 *
721 * dvp is referenced but not locked. *vpp must be returned referenced and
722 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
723 * at the root, instead it could indicate that the directory we were in was
724 * removed.
42c7d26b
MD
725 *
726 * NOTE: as-of sequences are not linked into the directory structure. If
727 * we are at the root with a different asof then the mount point, reload
728 * the same directory with the mount point's asof. I'm not sure what this
729 * will do to NFS. We encode ASOF stamps in NFS file handles so it might not
730 * get confused, but it hasn't been tested.
66325755 731 */
427e5fc6
MD
732static
733int
66325755 734hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
427e5fc6 735{
66325755 736 struct hammer_inode *dip;
d113fda1 737 struct hammer_inode *ip;
42c7d26b
MD
738 int64_t parent_obj_id;
739 hammer_tid_t asof;
d113fda1 740 int error;
66325755
MD
741
742 dip = VTOI(ap->a_dvp);
42c7d26b
MD
743 asof = dip->obj_asof;
744 parent_obj_id = dip->ino_data.parent_obj_id;
745
746 if (parent_obj_id == 0) {
747 if (dip->obj_id == HAMMER_OBJID_ROOT &&
748 asof != dip->hmp->asof) {
749 parent_obj_id = dip->obj_id;
750 asof = dip->hmp->asof;
751 *ap->a_fakename = kmalloc(19, M_TEMP, M_WAITOK);
752 ksnprintf(*ap->a_fakename, 19, "0x%016llx",
753 dip->obj_asof);
754 } else {
755 *ap->a_vpp = NULL;
756 return ENOENT;
757 }
66325755 758 }
d113fda1 759
61aeeb33 760 ip = hammer_get_inode(dip->hmp, &dip->cache[1], parent_obj_id,
42c7d26b 761 asof, dip->flags, &error);
d113fda1
MD
762 if (ip == NULL) {
763 *ap->a_vpp = NULL;
764 return(error);
765 }
766 error = hammer_get_vnode(ip, LK_EXCLUSIVE, ap->a_vpp);
767 hammer_rel_inode(ip, 0);
768 return (error);
427e5fc6
MD
769}
770
66325755
MD
771/*
772 * hammer_vop_nlink { nch, dvp, vp, cred }
773 */
427e5fc6
MD
774static
775int
66325755 776hammer_vop_nlink(struct vop_nlink_args *ap)
427e5fc6 777{
66325755
MD
778 struct hammer_transaction trans;
779 struct hammer_inode *dip;
780 struct hammer_inode *ip;
781 struct nchandle *nch;
782 int error;
783
784 nch = ap->a_nch;
785 dip = VTOI(ap->a_dvp);
786 ip = VTOI(ap->a_vp);
787
d113fda1
MD
788 if (dip->flags & HAMMER_INODE_RO)
789 return (EROFS);
790 if (ip->flags & HAMMER_INODE_RO)
791 return (EROFS);
792
66325755
MD
793 /*
794 * Create a transaction to cover the operations we perform.
795 */
8cd0a023 796 hammer_start_transaction(&trans, dip->hmp);
66325755
MD
797
798 /*
799 * Add the filesystem object to the directory. Note that neither
800 * dip nor ip are referenced or locked, but their vnodes are
801 * referenced. This function will bump the inode's link count.
802 */
a89aec1b 803 error = hammer_ip_add_directory(&trans, dip, nch->ncp, ip);
66325755
MD
804
805 /*
806 * Finish up.
807 */
808 if (error) {
809 hammer_abort_transaction(&trans);
810 } else {
6b4f890b
MD
811 cache_setunresolved(nch);
812 cache_setvp(nch, ap->a_vp);
66325755
MD
813 hammer_commit_transaction(&trans);
814 }
815 return (error);
427e5fc6
MD
816}
817
66325755
MD
818/*
819 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
820 *
821 * The operating system has already ensured that the directory entry
822 * does not exist and done all appropriate namespace locking.
823 */
427e5fc6
MD
824static
825int
66325755 826hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
427e5fc6 827{
66325755
MD
828 struct hammer_transaction trans;
829 struct hammer_inode *dip;
830 struct hammer_inode *nip;
831 struct nchandle *nch;
832 int error;
833
834 nch = ap->a_nch;
835 dip = VTOI(ap->a_dvp);
836
d113fda1
MD
837 if (dip->flags & HAMMER_INODE_RO)
838 return (EROFS);
839
66325755
MD
840 /*
841 * Create a transaction to cover the operations we perform.
842 */
8cd0a023 843 hammer_start_transaction(&trans, dip->hmp);
66325755
MD
844
845 /*
846 * Create a new filesystem object of the requested type. The
8cd0a023 847 * returned inode will be referenced but not locked.
66325755 848 */
8cd0a023 849 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
0b075555
MD
850 if (error)
851 kprintf("hammer_mkdir error %d\n", error);
66325755
MD
852 if (error) {
853 hammer_abort_transaction(&trans);
854 *ap->a_vpp = NULL;
855 return (error);
856 }
66325755
MD
857
858 /*
859 * Add the new filesystem object to the directory. This will also
860 * bump the inode's link count.
861 */
a89aec1b 862 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
0b075555
MD
863 if (error)
864 kprintf("hammer_mkdir (add) error %d\n", error);
66325755
MD
865
866 /*
867 * Finish up.
868 */
869 if (error) {
a89aec1b 870 hammer_rel_inode(nip, 0);
66325755
MD
871 hammer_abort_transaction(&trans);
872 *ap->a_vpp = NULL;
873 } else {
874 hammer_commit_transaction(&trans);
875 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
a89aec1b
MD
876 hammer_rel_inode(nip, 0);
877 if (error == 0) {
878 cache_setunresolved(ap->a_nch);
879 cache_setvp(ap->a_nch, *ap->a_vpp);
880 }
66325755
MD
881 }
882 return (error);
427e5fc6
MD
883}
884
66325755
MD
885/*
886 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
887 *
888 * The operating system has already ensured that the directory entry
889 * does not exist and done all appropriate namespace locking.
890 */
427e5fc6
MD
891static
892int
66325755 893hammer_vop_nmknod(struct vop_nmknod_args *ap)
427e5fc6 894{
66325755
MD
895 struct hammer_transaction trans;
896 struct hammer_inode *dip;
897 struct hammer_inode *nip;
898 struct nchandle *nch;
899 int error;
900
901 nch = ap->a_nch;
902 dip = VTOI(ap->a_dvp);
903
d113fda1
MD
904 if (dip->flags & HAMMER_INODE_RO)
905 return (EROFS);
906
66325755
MD
907 /*
908 * Create a transaction to cover the operations we perform.
909 */
8cd0a023 910 hammer_start_transaction(&trans, dip->hmp);
66325755
MD
911
912 /*
913 * Create a new filesystem object of the requested type. The
8cd0a023 914 * returned inode will be referenced but not locked.
66325755 915 */
8cd0a023 916 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
66325755
MD
917 if (error) {
918 hammer_abort_transaction(&trans);
919 *ap->a_vpp = NULL;
920 return (error);
921 }
66325755
MD
922
923 /*
924 * Add the new filesystem object to the directory. This will also
925 * bump the inode's link count.
926 */
a89aec1b 927 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
66325755
MD
928
929 /*
930 * Finish up.
931 */
932 if (error) {
a89aec1b 933 hammer_rel_inode(nip, 0);
66325755
MD
934 hammer_abort_transaction(&trans);
935 *ap->a_vpp = NULL;
936 } else {
937 hammer_commit_transaction(&trans);
938 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
a89aec1b
MD
939 hammer_rel_inode(nip, 0);
940 if (error == 0) {
941 cache_setunresolved(ap->a_nch);
942 cache_setvp(ap->a_nch, *ap->a_vpp);
943 }
66325755
MD
944 }
945 return (error);
427e5fc6
MD
946}
947
66325755
MD
948/*
949 * hammer_vop_open { vp, mode, cred, fp }
950 */
427e5fc6
MD
951static
952int
66325755 953hammer_vop_open(struct vop_open_args *ap)
427e5fc6 954{
d113fda1
MD
955 if ((ap->a_mode & FWRITE) && (VTOI(ap->a_vp)->flags & HAMMER_INODE_RO))
956 return (EROFS);
957
a89aec1b 958 return(vop_stdopen(ap));
427e5fc6
MD
959}
960
66325755
MD
961/*
962 * hammer_vop_pathconf { vp, name, retval }
963 */
427e5fc6
MD
964static
965int
66325755 966hammer_vop_pathconf(struct vop_pathconf_args *ap)
427e5fc6
MD
967{
968 return EOPNOTSUPP;
969}
970
66325755
MD
971/*
972 * hammer_vop_print { vp }
973 */
427e5fc6
MD
974static
975int
66325755 976hammer_vop_print(struct vop_print_args *ap)
427e5fc6
MD
977{
978 return EOPNOTSUPP;
979}
980
66325755 981/*
6b4f890b 982 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
66325755 983 */
427e5fc6
MD
984static
985int
66325755 986hammer_vop_readdir(struct vop_readdir_args *ap)
427e5fc6 987{
6b4f890b
MD
988 struct hammer_cursor cursor;
989 struct hammer_inode *ip;
990 struct uio *uio;
991 hammer_record_ondisk_t rec;
992 hammer_base_elm_t base;
993 int error;
994 int cookie_index;
995 int ncookies;
996 off_t *cookies;
997 off_t saveoff;
998 int r;
999
1000 ip = VTOI(ap->a_vp);
1001 uio = ap->a_uio;
b3deaf57
MD
1002 saveoff = uio->uio_offset;
1003
1004 if (ap->a_ncookies) {
1005 ncookies = uio->uio_resid / 16 + 1;
1006 if (ncookies > 1024)
1007 ncookies = 1024;
1008 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
1009 cookie_index = 0;
1010 } else {
1011 ncookies = -1;
1012 cookies = NULL;
1013 cookie_index = 0;
1014 }
1015
1016 /*
1017 * Handle artificial entries
1018 */
1019 error = 0;
1020 if (saveoff == 0) {
1021 r = vop_write_dirent(&error, uio, ip->obj_id, DT_DIR, 1, ".");
1022 if (r)
1023 goto done;
1024 if (cookies)
1025 cookies[cookie_index] = saveoff;
1026 ++saveoff;
1027 ++cookie_index;
1028 if (cookie_index == ncookies)
1029 goto done;
1030 }
1031 if (saveoff == 1) {
1032 if (ip->ino_data.parent_obj_id) {
1033 r = vop_write_dirent(&error, uio,
1034 ip->ino_data.parent_obj_id,
1035 DT_DIR, 2, "..");
1036 } else {
1037 r = vop_write_dirent(&error, uio,
1038 ip->obj_id, DT_DIR, 2, "..");
1039 }
1040 if (r)
1041 goto done;
1042 if (cookies)
1043 cookies[cookie_index] = saveoff;
1044 ++saveoff;
1045 ++cookie_index;
1046 if (cookie_index == ncookies)
1047 goto done;
1048 }
6b4f890b
MD
1049
1050 /*
1051 * Key range (begin and end inclusive) to scan. Directory keys
1052 * directly translate to a 64 bit 'seek' position.
1053 */
61aeeb33 1054 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
6b4f890b 1055 cursor.key_beg.obj_id = ip->obj_id;
d5530d22 1056 cursor.key_beg.create_tid = 0;
6b4f890b
MD
1057 cursor.key_beg.delete_tid = 0;
1058 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1059 cursor.key_beg.obj_type = 0;
b3deaf57 1060 cursor.key_beg.key = saveoff;
6b4f890b
MD
1061
1062 cursor.key_end = cursor.key_beg;
1063 cursor.key_end.key = HAMMER_MAX_KEY;
d5530d22
MD
1064 cursor.asof = ip->obj_asof;
1065 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
6b4f890b 1066
6b4f890b
MD
1067 error = hammer_ip_first(&cursor, ip);
1068
1069 while (error == 0) {
40043e7f 1070 error = hammer_ip_resolve_record_and_data(&cursor);
6b4f890b
MD
1071 if (error)
1072 break;
1073 rec = cursor.record;
1074 base = &rec->base.base;
1075 saveoff = base->key;
1076
7a04d74f
MD
1077 if (base->obj_id != ip->obj_id)
1078 panic("readdir: bad record at %p", cursor.node);
1079
6b4f890b
MD
1080 r = vop_write_dirent(
1081 &error, uio, rec->entry.obj_id,
6b4f890b 1082 hammer_get_dtype(rec->entry.base.base.obj_type),
7a04d74f 1083 rec->entry.base.data_len,
40043e7f 1084 (void *)cursor.data);
6b4f890b
MD
1085 if (r)
1086 break;
1087 ++saveoff;
1088 if (cookies)
1089 cookies[cookie_index] = base->key;
1090 ++cookie_index;
1091 if (cookie_index == ncookies)
1092 break;
1093 error = hammer_ip_next(&cursor);
1094 }
1095 hammer_done_cursor(&cursor);
1096
b3deaf57 1097done:
6b4f890b
MD
1098 if (ap->a_eofflag)
1099 *ap->a_eofflag = (error == ENOENT);
6b4f890b
MD
1100 uio->uio_offset = saveoff;
1101 if (error && cookie_index == 0) {
b3deaf57
MD
1102 if (error == ENOENT)
1103 error = 0;
6b4f890b
MD
1104 if (cookies) {
1105 kfree(cookies, M_TEMP);
1106 *ap->a_ncookies = 0;
1107 *ap->a_cookies = NULL;
1108 }
1109 } else {
7a04d74f
MD
1110 if (error == ENOENT)
1111 error = 0;
6b4f890b
MD
1112 if (cookies) {
1113 *ap->a_ncookies = cookie_index;
1114 *ap->a_cookies = cookies;
1115 }
1116 }
1117 return(error);
427e5fc6
MD
1118}
1119
66325755
MD
1120/*
1121 * hammer_vop_readlink { vp, uio, cred }
1122 */
427e5fc6
MD
1123static
1124int
66325755 1125hammer_vop_readlink(struct vop_readlink_args *ap)
427e5fc6 1126{
7a04d74f
MD
1127 struct hammer_cursor cursor;
1128 struct hammer_inode *ip;
1129 int error;
1130
1131 ip = VTOI(ap->a_vp);
61aeeb33 1132 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
7a04d74f
MD
1133
1134 /*
1135 * Key range (begin and end inclusive) to scan. Directory keys
1136 * directly translate to a 64 bit 'seek' position.
1137 */
1138 cursor.key_beg.obj_id = ip->obj_id;
d5530d22 1139 cursor.key_beg.create_tid = 0;
7a04d74f
MD
1140 cursor.key_beg.delete_tid = 0;
1141 cursor.key_beg.rec_type = HAMMER_RECTYPE_FIX;
1142 cursor.key_beg.obj_type = 0;
1143 cursor.key_beg.key = HAMMER_FIXKEY_SYMLINK;
d5530d22
MD
1144 cursor.asof = ip->obj_asof;
1145 cursor.flags |= HAMMER_CURSOR_ASOF;
7a04d74f
MD
1146
1147 error = hammer_ip_lookup(&cursor, ip);
1148 if (error == 0) {
1149 error = hammer_ip_resolve_data(&cursor);
1150 if (error == 0) {
40043e7f 1151 error = uiomove((char *)cursor.data,
47197d71 1152 cursor.record->base.data_len,
7a04d74f
MD
1153 ap->a_uio);
1154 }
1155 }
1156 hammer_done_cursor(&cursor);
1157 return(error);
427e5fc6
MD
1158}
1159
66325755
MD
1160/*
1161 * hammer_vop_nremove { nch, dvp, cred }
1162 */
427e5fc6
MD
1163static
1164int
66325755 1165hammer_vop_nremove(struct vop_nremove_args *ap)
427e5fc6 1166{
8cd0a023 1167 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, 0));
427e5fc6
MD
1168}
1169
66325755
MD
1170/*
1171 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1172 */
427e5fc6
MD
1173static
1174int
66325755 1175hammer_vop_nrename(struct vop_nrename_args *ap)
427e5fc6 1176{
8cd0a023
MD
1177 struct hammer_transaction trans;
1178 struct namecache *fncp;
1179 struct namecache *tncp;
1180 struct hammer_inode *fdip;
1181 struct hammer_inode *tdip;
1182 struct hammer_inode *ip;
1183 struct hammer_cursor cursor;
1184 union hammer_record_ondisk *rec;
1185 int64_t namekey;
1186 int error;
1187
1188 fdip = VTOI(ap->a_fdvp);
1189 tdip = VTOI(ap->a_tdvp);
1190 fncp = ap->a_fnch->ncp;
1191 tncp = ap->a_tnch->ncp;
b3deaf57
MD
1192 ip = VTOI(fncp->nc_vp);
1193 KKASSERT(ip != NULL);
d113fda1
MD
1194
1195 if (fdip->flags & HAMMER_INODE_RO)
1196 return (EROFS);
1197 if (tdip->flags & HAMMER_INODE_RO)
1198 return (EROFS);
1199 if (ip->flags & HAMMER_INODE_RO)
1200 return (EROFS);
1201
8cd0a023
MD
1202 hammer_start_transaction(&trans, fdip->hmp);
1203
1204 /*
b3deaf57
MD
1205 * Remove tncp from the target directory and then link ip as
1206 * tncp. XXX pass trans to dounlink
42c7d26b
MD
1207 *
1208 * Force the inode sync-time to match the transaction so it is
1209 * in-sync with the creation of the target directory entry.
8cd0a023 1210 */
b3deaf57 1211 error = hammer_dounlink(ap->a_tnch, ap->a_tdvp, ap->a_cred, 0);
42c7d26b 1212 if (error == 0 || error == ENOENT) {
b3deaf57 1213 error = hammer_ip_add_directory(&trans, tdip, tncp, ip);
42c7d26b
MD
1214 if (error == 0) {
1215 ip->ino_data.parent_obj_id = tdip->obj_id;
1216 hammer_modify_inode(&trans, ip,
1217 HAMMER_INODE_DDIRTY | HAMMER_INODE_TIDLOCKED);
1218 }
1219 }
b3deaf57
MD
1220 if (error)
1221 goto failed; /* XXX */
8cd0a023
MD
1222
1223 /*
1224 * Locate the record in the originating directory and remove it.
1225 *
1226 * Calculate the namekey and setup the key range for the scan. This
1227 * works kinda like a chained hash table where the lower 32 bits
1228 * of the namekey synthesize the chain.
1229 *
1230 * The key range is inclusive of both key_beg and key_end.
1231 */
1232 namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
6a37e7e4 1233retry:
61aeeb33 1234 hammer_init_cursor_hmp(&cursor, &fdip->cache[0], fdip->hmp);
8cd0a023
MD
1235 cursor.key_beg.obj_id = fdip->obj_id;
1236 cursor.key_beg.key = namekey;
d5530d22 1237 cursor.key_beg.create_tid = 0;
8cd0a023
MD
1238 cursor.key_beg.delete_tid = 0;
1239 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1240 cursor.key_beg.obj_type = 0;
1241
1242 cursor.key_end = cursor.key_beg;
1243 cursor.key_end.key |= 0xFFFFFFFFULL;
d5530d22
MD
1244 cursor.asof = fdip->obj_asof;
1245 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
8cd0a023
MD
1246
1247 /*
1248 * Scan all matching records (the chain), locate the one matching
a89aec1b 1249 * the requested path component.
8cd0a023
MD
1250 *
1251 * The hammer_ip_*() functions merge in-memory records with on-disk
1252 * records for the purposes of the search.
1253 */
a89aec1b
MD
1254 error = hammer_ip_first(&cursor, fdip);
1255 while (error == 0) {
8cd0a023
MD
1256 if (hammer_ip_resolve_data(&cursor) != 0)
1257 break;
a89aec1b 1258 rec = cursor.record;
8cd0a023 1259 if (fncp->nc_nlen == rec->entry.base.data_len &&
40043e7f 1260 bcmp(fncp->nc_name, cursor.data, fncp->nc_nlen) == 0) {
8cd0a023
MD
1261 break;
1262 }
a89aec1b 1263 error = hammer_ip_next(&cursor);
8cd0a023 1264 }
8cd0a023
MD
1265
1266 /*
1267 * If all is ok we have to get the inode so we can adjust nlinks.
6a37e7e4
MD
1268 *
1269 * WARNING: hammer_ip_del_directory() may have to terminate the
1270 * cursor to avoid a recursion. It's ok to call hammer_done_cursor()
1271 * twice.
8cd0a023 1272 */
9944ae54 1273 if (error == 0)
6a37e7e4 1274 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
c0ade690 1275 hammer_done_cursor(&cursor);
6a37e7e4
MD
1276 if (error == 0)
1277 cache_rename(ap->a_fnch, ap->a_tnch);
1278 if (error == EDEADLK)
1279 goto retry;
b3deaf57 1280failed:
8cd0a023
MD
1281 if (error == 0) {
1282 hammer_commit_transaction(&trans);
1283 } else {
1284 hammer_abort_transaction(&trans);
1285 }
8cd0a023 1286 return (error);
427e5fc6
MD
1287}
1288
66325755
MD
1289/*
1290 * hammer_vop_nrmdir { nch, dvp, cred }
1291 */
427e5fc6
MD
1292static
1293int
66325755 1294hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
427e5fc6 1295{
8cd0a023 1296 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, 0));
427e5fc6
MD
1297}
1298
66325755
MD
1299/*
1300 * hammer_vop_setattr { vp, vap, cred }
1301 */
427e5fc6
MD
1302static
1303int
66325755 1304hammer_vop_setattr(struct vop_setattr_args *ap)
427e5fc6 1305{
8cd0a023
MD
1306 struct hammer_transaction trans;
1307 struct vattr *vap;
1308 struct hammer_inode *ip;
1309 int modflags;
1310 int error;
d5ef456e 1311 int truncating;
76376933 1312 int64_t aligned_size;
8cd0a023
MD
1313 u_int32_t flags;
1314 uuid_t uuid;
1315
1316 vap = ap->a_vap;
1317 ip = ap->a_vp->v_data;
1318 modflags = 0;
1319
1320 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1321 return(EROFS);
d113fda1
MD
1322 if (ip->flags & HAMMER_INODE_RO)
1323 return (EROFS);
8cd0a023
MD
1324
1325 hammer_start_transaction(&trans, ip->hmp);
1326 error = 0;
1327
1328 if (vap->va_flags != VNOVAL) {
1329 flags = ip->ino_data.uflags;
1330 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1331 hammer_to_unix_xid(&ip->ino_data.uid),
1332 ap->a_cred);
1333 if (error == 0) {
1334 if (ip->ino_data.uflags != flags) {
1335 ip->ino_data.uflags = flags;
1336 modflags |= HAMMER_INODE_DDIRTY;
1337 }
1338 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1339 error = 0;
1340 goto done;
1341 }
1342 }
1343 goto done;
1344 }
1345 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1346 error = EPERM;
1347 goto done;
1348 }
1349 if (vap->va_uid != (uid_t)VNOVAL) {
1350 hammer_guid_to_uuid(&uuid, vap->va_uid);
6b4f890b 1351 if (bcmp(&uuid, &ip->ino_data.uid, sizeof(uuid)) != 0) {
8cd0a023
MD
1352 ip->ino_data.uid = uuid;
1353 modflags |= HAMMER_INODE_DDIRTY;
1354 }
1355 }
1356 if (vap->va_gid != (uid_t)VNOVAL) {
6b4f890b
MD
1357 hammer_guid_to_uuid(&uuid, vap->va_gid);
1358 if (bcmp(&uuid, &ip->ino_data.gid, sizeof(uuid)) != 0) {
8cd0a023
MD
1359 ip->ino_data.gid = uuid;
1360 modflags |= HAMMER_INODE_DDIRTY;
1361 }
1362 }
d26d0ae9 1363 while (vap->va_size != VNOVAL && ip->ino_rec.ino_size != vap->va_size) {
8cd0a023
MD
1364 switch(ap->a_vp->v_type) {
1365 case VREG:
d5ef456e
MD
1366 if (vap->va_size == ip->ino_rec.ino_size)
1367 break;
c0ade690
MD
1368 if (vap->va_size < ip->ino_rec.ino_size) {
1369 vtruncbuf(ap->a_vp, vap->va_size,
1370 HAMMER_BUFSIZE);
d5ef456e
MD
1371 truncating = 1;
1372 } else {
c0ade690 1373 vnode_pager_setsize(ap->a_vp, vap->va_size);
d5ef456e 1374 truncating = 0;
c0ade690 1375 }
d5ef456e
MD
1376 ip->ino_rec.ino_size = vap->va_size;
1377 modflags |= HAMMER_INODE_RDIRTY;
76376933
MD
1378 aligned_size = (vap->va_size + HAMMER_BUFMASK) &
1379 ~(int64_t)HAMMER_BUFMASK;
d5ef456e
MD
1380
1381 if (truncating) {
1382 error = hammer_ip_delete_range(&trans, ip,
76376933 1383 aligned_size,
47197d71 1384 0x7FFFFFFFFFFFFFFFLL);
d5ef456e
MD
1385 }
1386 /*
1387 * If truncating we have to clean out a portion of
1388 * the last block on-disk.
1389 */
1390 if (truncating && error == 0 &&
1391 vap->va_size < aligned_size) {
1392 struct buf *bp;
1393 int offset;
1394
1395 offset = vap->va_size & HAMMER_BUFMASK;
1396 error = bread(ap->a_vp,
1397 aligned_size - HAMMER_BUFSIZE,
1398 HAMMER_BUFSIZE, &bp);
1399 if (error == 0) {
1400 bzero(bp->b_data + offset,
1401 HAMMER_BUFSIZE - offset);
a9cd537b
MD
1402 if (bp->b_tid == 0)
1403 bp->b_tid = trans.tid;
d5ef456e
MD
1404 bdwrite(bp);
1405 } else {
1406 brelse(bp);
1407 }
1408 }
76376933 1409 break;
8cd0a023 1410 case VDATABASE:
a89aec1b 1411 error = hammer_ip_delete_range(&trans, ip,
8cd0a023 1412 vap->va_size,
47197d71 1413 0x7FFFFFFFFFFFFFFFLL);
c0ade690
MD
1414 ip->ino_rec.ino_size = vap->va_size;
1415 modflags |= HAMMER_INODE_RDIRTY;
8cd0a023
MD
1416 break;
1417 default:
1418 error = EINVAL;
1419 goto done;
1420 }
d26d0ae9 1421 break;
8cd0a023
MD
1422 }
1423 if (vap->va_atime.tv_sec != VNOVAL) {
1424 ip->ino_rec.ino_atime =
1425 hammer_timespec_to_transid(&vap->va_atime);
1426 modflags |= HAMMER_INODE_ITIMES;
1427 }
1428 if (vap->va_mtime.tv_sec != VNOVAL) {
1429 ip->ino_rec.ino_mtime =
1430 hammer_timespec_to_transid(&vap->va_mtime);
1431 modflags |= HAMMER_INODE_ITIMES;
1432 }
1433 if (vap->va_mode != (mode_t)VNOVAL) {
1434 if (ip->ino_data.mode != vap->va_mode) {
1435 ip->ino_data.mode = vap->va_mode;
1436 modflags |= HAMMER_INODE_DDIRTY;
1437 }
1438 }
1439done:
1440 if (error) {
1441 hammer_abort_transaction(&trans);
1442 } else {
c0ade690 1443 hammer_modify_inode(&trans, ip, modflags);
8cd0a023
MD
1444 hammer_commit_transaction(&trans);
1445 }
1446 return (error);
427e5fc6
MD
1447}
1448
66325755
MD
1449/*
1450 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1451 */
427e5fc6
MD
1452static
1453int
66325755 1454hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
427e5fc6 1455{
7a04d74f
MD
1456 struct hammer_transaction trans;
1457 struct hammer_inode *dip;
1458 struct hammer_inode *nip;
1459 struct nchandle *nch;
1460 hammer_record_t record;
1461 int error;
1462 int bytes;
1463
1464 ap->a_vap->va_type = VLNK;
1465
1466 nch = ap->a_nch;
1467 dip = VTOI(ap->a_dvp);
1468
d113fda1
MD
1469 if (dip->flags & HAMMER_INODE_RO)
1470 return (EROFS);
1471
7a04d74f
MD
1472 /*
1473 * Create a transaction to cover the operations we perform.
1474 */
1475 hammer_start_transaction(&trans, dip->hmp);
1476
1477 /*
1478 * Create a new filesystem object of the requested type. The
1479 * returned inode will be referenced but not locked.
1480 */
1481
1482 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
1483 if (error) {
1484 hammer_abort_transaction(&trans);
1485 *ap->a_vpp = NULL;
1486 return (error);
1487 }
1488
1489 /*
1490 * Add the new filesystem object to the directory. This will also
1491 * bump the inode's link count.
1492 */
1493 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
1494
1495 /*
1496 * Add a record representing the symlink. symlink stores the link
1497 * as pure data, not a string, and is no \0 terminated.
1498 */
1499 if (error == 0) {
40043e7f 1500 record = hammer_alloc_mem_record(nip);
7a04d74f
MD
1501 bytes = strlen(ap->a_target);
1502
47197d71
MD
1503 record->rec.base.base.key = HAMMER_FIXKEY_SYMLINK;
1504 record->rec.base.base.rec_type = HAMMER_RECTYPE_FIX;
1505 record->rec.base.data_len = bytes;
1506 record->data = (void *)ap->a_target;
1507 /* will be reallocated by routine below */
7a04d74f 1508 error = hammer_ip_add_record(&trans, record);
42c7d26b
MD
1509
1510 /*
1511 * Set the file size to the length of the link.
1512 */
1513 if (error == 0) {
1514 nip->ino_rec.ino_size = bytes;
1515 hammer_modify_inode(&trans, nip, HAMMER_INODE_RDIRTY);
1516 }
7a04d74f
MD
1517 }
1518
1519 /*
1520 * Finish up.
1521 */
1522 if (error) {
1523 hammer_rel_inode(nip, 0);
1524 hammer_abort_transaction(&trans);
1525 *ap->a_vpp = NULL;
1526 } else {
1527 hammer_commit_transaction(&trans);
1528 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
1529 hammer_rel_inode(nip, 0);
1530 if (error == 0) {
1531 cache_setunresolved(ap->a_nch);
1532 cache_setvp(ap->a_nch, *ap->a_vpp);
1533 }
1534 }
1535 return (error);
427e5fc6
MD
1536}
1537
66325755
MD
1538/*
1539 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1540 */
427e5fc6
MD
1541static
1542int
66325755 1543hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
427e5fc6 1544{
8cd0a023 1545 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, ap->a_flags));
427e5fc6
MD
1546}
1547
7dc57964
MD
1548/*
1549 * hammer_vop_ioctl { vp, command, data, fflag, cred }
1550 */
1551static
1552int
1553hammer_vop_ioctl(struct vop_ioctl_args *ap)
1554{
1555 struct hammer_inode *ip = ap->a_vp->v_data;
1556
1557 return(hammer_ioctl(ip, ap->a_command, ap->a_data,
1558 ap->a_fflag, ap->a_cred));
1559}
1560
513ca7d7
MD
1561static
1562int
1563hammer_vop_mountctl(struct vop_mountctl_args *ap)
1564{
1565 struct mount *mp;
1566 int error;
1567
1568 mp = ap->a_head.a_ops->head.vv_mount;
1569
1570 switch(ap->a_op) {
1571 case MOUNTCTL_SET_EXPORT:
1572 if (ap->a_ctllen != sizeof(struct export_args))
1573 error = EINVAL;
1574 error = hammer_vfs_export(mp, ap->a_op,
1575 (const struct export_args *)ap->a_ctl);
1576 break;
1577 default:
1578 error = journal_mountctl(ap);
1579 break;
1580 }
1581 return(error);
1582}
1583
66325755
MD
1584/*
1585 * hammer_vop_strategy { vp, bio }
8cd0a023
MD
1586 *
1587 * Strategy call, used for regular file read & write only. Note that the
1588 * bp may represent a cluster.
1589 *
1590 * To simplify operation and allow better optimizations in the future,
1591 * this code does not make any assumptions with regards to buffer alignment
1592 * or size.
66325755 1593 */
427e5fc6
MD
1594static
1595int
66325755 1596hammer_vop_strategy(struct vop_strategy_args *ap)
427e5fc6 1597{
8cd0a023
MD
1598 struct buf *bp;
1599 int error;
1600
1601 bp = ap->a_bio->bio_buf;
1602
1603 switch(bp->b_cmd) {
1604 case BUF_CMD_READ:
1605 error = hammer_vop_strategy_read(ap);
1606 break;
1607 case BUF_CMD_WRITE:
1608 error = hammer_vop_strategy_write(ap);
1609 break;
1610 default:
1611 error = EINVAL;
1612 break;
1613 }
1614 bp->b_error = error;
1615 if (error)
1616 bp->b_flags |= B_ERROR;
1617 biodone(ap->a_bio);
1618 return (error);
427e5fc6
MD
1619}
1620
8cd0a023
MD
1621/*
1622 * Read from a regular file. Iterate the related records and fill in the
1623 * BIO/BUF. Gaps are zero-filled.
1624 *
1625 * The support code in hammer_object.c should be used to deal with mixed
1626 * in-memory and on-disk records.
1627 *
1628 * XXX atime update
1629 */
1630static
1631int
1632hammer_vop_strategy_read(struct vop_strategy_args *ap)
1633{
1634 struct hammer_inode *ip = ap->a_vp->v_data;
1635 struct hammer_cursor cursor;
1636 hammer_record_ondisk_t rec;
1637 hammer_base_elm_t base;
1638 struct bio *bio;
1639 struct buf *bp;
1640 int64_t rec_offset;
a89aec1b 1641 int64_t ran_end;
195c19a1 1642 int64_t tmp64;
8cd0a023
MD
1643 int error;
1644 int boff;
1645 int roff;
1646 int n;
1647
1648 bio = ap->a_bio;
1649 bp = bio->bio_buf;
1650
61aeeb33 1651 hammer_init_cursor_hmp(&cursor, &ip->cache[0], ip->hmp);
8cd0a023
MD
1652
1653 /*
1654 * Key range (begin and end inclusive) to scan. Note that the key's
c0ade690
MD
1655 * stored in the actual records represent BASE+LEN, not BASE. The
1656 * first record containing bio_offset will have a key > bio_offset.
8cd0a023
MD
1657 */
1658 cursor.key_beg.obj_id = ip->obj_id;
d5530d22 1659 cursor.key_beg.create_tid = 0;
8cd0a023 1660 cursor.key_beg.delete_tid = 0;
8cd0a023 1661 cursor.key_beg.obj_type = 0;
c0ade690 1662 cursor.key_beg.key = bio->bio_offset + 1;
d5530d22 1663 cursor.asof = ip->obj_asof;
47197d71 1664 cursor.flags |= HAMMER_CURSOR_ASOF | HAMMER_CURSOR_DATAEXTOK;
8cd0a023
MD
1665
1666 cursor.key_end = cursor.key_beg;
a89aec1b
MD
1667 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1668 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
1669 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
1670 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1671 } else {
c0ade690 1672 ran_end = bio->bio_offset + bp->b_bufsize;
a89aec1b
MD
1673 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
1674 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
195c19a1
MD
1675 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
1676 if (tmp64 < ran_end)
a89aec1b
MD
1677 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1678 else
7f7c1f84 1679 cursor.key_end.key = ran_end + MAXPHYS + 1;
a89aec1b 1680 }
d26d0ae9 1681 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
8cd0a023 1682
a89aec1b 1683 error = hammer_ip_first(&cursor, ip);
8cd0a023
MD
1684 boff = 0;
1685
a89aec1b
MD
1686 while (error == 0) {
1687 error = hammer_ip_resolve_data(&cursor);
1688 if (error)
66325755 1689 break;
a89aec1b 1690 rec = cursor.record;
8cd0a023
MD
1691 base = &rec->base.base;
1692
c0ade690 1693 rec_offset = base->key - rec->data.base.data_len;
8cd0a023 1694
66325755 1695 /*
a89aec1b 1696 * Calculate the gap, if any, and zero-fill it.
66325755 1697 */
8cd0a023
MD
1698 n = (int)(rec_offset - (bio->bio_offset + boff));
1699 if (n > 0) {
a89aec1b
MD
1700 if (n > bp->b_bufsize - boff)
1701 n = bp->b_bufsize - boff;
8cd0a023
MD
1702 bzero((char *)bp->b_data + boff, n);
1703 boff += n;
1704 n = 0;
66325755 1705 }
8cd0a023
MD
1706
1707 /*
1708 * Calculate the data offset in the record and the number
1709 * of bytes we can copy.
a89aec1b
MD
1710 *
1711 * Note there is a degenerate case here where boff may
1712 * already be at bp->b_bufsize.
8cd0a023
MD
1713 */
1714 roff = -n;
1715 n = rec->data.base.data_len - roff;
1716 KKASSERT(n > 0);
1717 if (n > bp->b_bufsize - boff)
1718 n = bp->b_bufsize - boff;
40043e7f
MD
1719 bcopy((char *)cursor.data + roff,
1720 (char *)bp->b_data + boff, n);
8cd0a023
MD
1721 boff += n;
1722 if (boff == bp->b_bufsize)
66325755 1723 break;
a89aec1b 1724 error = hammer_ip_next(&cursor);
66325755 1725 }
8cd0a023 1726 hammer_done_cursor(&cursor);
66325755
MD
1727
1728 /*
8cd0a023 1729 * There may have been a gap after the last record
66325755 1730 */
8cd0a023
MD
1731 if (error == ENOENT)
1732 error = 0;
1733 if (error == 0 && boff != bp->b_bufsize) {
7f7c1f84 1734 KKASSERT(boff < bp->b_bufsize);
8cd0a023
MD
1735 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
1736 /* boff = bp->b_bufsize; */
1737 }
1738 bp->b_resid = 0;
1739 return(error);
1740}
1741
1742/*
1743 * Write to a regular file. Iterate the related records and mark for
1744 * deletion. If existing edge records (left and right side) overlap our
1745 * write they have to be marked deleted and new records created, usually
1746 * referencing a portion of the original data. Then add a record to
1747 * represent the buffer.
1748 *
1749 * The support code in hammer_object.c should be used to deal with mixed
1750 * in-memory and on-disk records.
1751 */
1752static
1753int
1754hammer_vop_strategy_write(struct vop_strategy_args *ap)
1755{
1756 struct hammer_transaction trans;
1757 hammer_inode_t ip;
1758 struct bio *bio;
1759 struct buf *bp;
1760 int error;
1761
1762 bio = ap->a_bio;
1763 bp = bio->bio_buf;
1764 ip = ap->a_vp->v_data;
d113fda1
MD
1765
1766 if (ip->flags & HAMMER_INODE_RO)
1767 return (EROFS);
1768
32c90105
MD
1769 /*
1770 * Start a transaction using the TID stored with the bp.
1771 */
1772 KKASSERT(bp->b_tid != 0);
1773 hammer_start_transaction_tid(&trans, ip->hmp, bp->b_tid);
8cd0a023
MD
1774
1775 /*
1776 * Delete any records overlapping our range. This function will
d26d0ae9 1777 * (eventually) properly truncate partial overlaps.
8cd0a023 1778 */
a89aec1b
MD
1779 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1780 error = hammer_ip_delete_range(&trans, ip, bio->bio_offset,
47197d71 1781 bio->bio_offset);
a89aec1b
MD
1782 } else {
1783 error = hammer_ip_delete_range(&trans, ip, bio->bio_offset,
d26d0ae9 1784 bio->bio_offset +
47197d71 1785 bp->b_bufsize - 1);
a89aec1b 1786 }
8cd0a023
MD
1787
1788 /*
e38e0b15
MD
1789 * Add a single record to cover the write. We can write a record
1790 * with only the actual file data - for example, a small 200 byte
1791 * file does not have to write out a 16K record.
1792 *
1793 * While the data size does not have to be aligned, we still do it
1794 * to reduce fragmentation in a future allocation model.
8cd0a023
MD
1795 */
1796 if (error == 0) {
e38e0b15
MD
1797 int limit_size;
1798
1799 if (ip->ino_rec.ino_size - bio->bio_offset > bp->b_bufsize) {
1800 limit_size = bp->b_bufsize;
1801 } else {
1802 limit_size = (int)(ip->ino_rec.ino_size -
1803 bio->bio_offset);
1804 KKASSERT(limit_size >= 0);
1805 limit_size = (limit_size + 63) & ~63;
1806 }
c0ade690 1807 error = hammer_ip_sync_data(&trans, ip, bio->bio_offset,
e38e0b15 1808 bp->b_data, limit_size);
66325755
MD
1809 }
1810
1811 /*
8cd0a023 1812 * If an error occured abort the transaction
66325755 1813 */
8cd0a023
MD
1814 if (error) {
1815 /* XXX undo deletion */
1816 hammer_abort_transaction(&trans);
1817 bp->b_resid = bp->b_bufsize;
1818 } else {
1819 hammer_commit_transaction(&trans);
1820 bp->b_resid = 0;
32c90105 1821 bp->b_tid = 0;
8cd0a023
MD
1822 }
1823 return(error);
1824}
1825
1826/*
1827 * dounlink - disconnect a directory entry
1828 *
1829 * XXX whiteout support not really in yet
1830 */
1831static int
1832hammer_dounlink(struct nchandle *nch, struct vnode *dvp, struct ucred *cred,
1833 int flags)
1834{
1835 struct hammer_transaction trans;
1836 struct namecache *ncp;
1837 hammer_inode_t dip;
1838 hammer_inode_t ip;
1839 hammer_record_ondisk_t rec;
1840 struct hammer_cursor cursor;
8cd0a023
MD
1841 int64_t namekey;
1842 int error;
1843
1844 /*
1845 * Calculate the namekey and setup the key range for the scan. This
1846 * works kinda like a chained hash table where the lower 32 bits
1847 * of the namekey synthesize the chain.
1848 *
1849 * The key range is inclusive of both key_beg and key_end.
1850 */
1851 dip = VTOI(dvp);
1852 ncp = nch->ncp;
d113fda1
MD
1853
1854 if (dip->flags & HAMMER_INODE_RO)
1855 return (EROFS);
1856
6a37e7e4 1857 hammer_start_transaction(&trans, dip->hmp);
8cd0a023 1858
6a37e7e4
MD
1859 namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
1860retry:
61aeeb33 1861 hammer_init_cursor_hmp(&cursor, &dip->cache[0], dip->hmp);
8cd0a023
MD
1862 cursor.key_beg.obj_id = dip->obj_id;
1863 cursor.key_beg.key = namekey;
d5530d22 1864 cursor.key_beg.create_tid = 0;
8cd0a023
MD
1865 cursor.key_beg.delete_tid = 0;
1866 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1867 cursor.key_beg.obj_type = 0;
1868
1869 cursor.key_end = cursor.key_beg;
1870 cursor.key_end.key |= 0xFFFFFFFFULL;
d5530d22
MD
1871 cursor.asof = dip->obj_asof;
1872 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE | HAMMER_CURSOR_ASOF;
8cd0a023 1873
8cd0a023
MD
1874 /*
1875 * Scan all matching records (the chain), locate the one matching
1876 * the requested path component. info->last_error contains the
1877 * error code on search termination and could be 0, ENOENT, or
1878 * something else.
1879 *
1880 * The hammer_ip_*() functions merge in-memory records with on-disk
1881 * records for the purposes of the search.
1882 */
a89aec1b
MD
1883 error = hammer_ip_first(&cursor, dip);
1884 while (error == 0) {
1885 error = hammer_ip_resolve_data(&cursor);
1886 if (error)
66325755 1887 break;
a89aec1b 1888 rec = cursor.record;
8cd0a023 1889 if (ncp->nc_nlen == rec->entry.base.data_len &&
40043e7f 1890 bcmp(ncp->nc_name, cursor.data, ncp->nc_nlen) == 0) {
66325755
MD
1891 break;
1892 }
a89aec1b 1893 error = hammer_ip_next(&cursor);
66325755 1894 }
8cd0a023
MD
1895
1896 /*
1897 * If all is ok we have to get the inode so we can adjust nlinks.
b3deaf57
MD
1898 *
1899 * If the target is a directory, it must be empty.
8cd0a023 1900 */
66325755 1901 if (error == 0) {
61aeeb33
MD
1902 ip = hammer_get_inode(dip->hmp, &dip->cache[1],
1903 rec->entry.obj_id,
d113fda1 1904 dip->hmp->asof, 0, &error);
46fe7ae1
MD
1905 if (error == ENOENT) {
1906 kprintf("obj_id %016llx\n", rec->entry.obj_id);
1907 Debugger("ENOENT unlinking object that should exist, cont to sync");
1908 hammer_sync_hmp(dip->hmp, MNT_NOWAIT);
1909 Debugger("ENOENT - sync done");
1910 }
b3deaf57
MD
1911 if (error == 0 && ip->ino_rec.base.base.obj_type ==
1912 HAMMER_OBJTYPE_DIRECTORY) {
1913 error = hammer_ip_check_directory_empty(&trans, ip);
1914 }
6a37e7e4
MD
1915 /*
1916 * WARNING: hammer_ip_del_directory() may have to terminate
1917 * the cursor to avoid a lock recursion. It's ok to call
1918 * hammer_done_cursor() twice.
1919 */
8cd0a023 1920 if (error == 0)
a89aec1b 1921 error = hammer_ip_del_directory(&trans, &cursor, dip, ip);
8cd0a023
MD
1922 if (error == 0) {
1923 cache_setunresolved(nch);
1924 cache_setvp(nch, NULL);
1925 /* XXX locking */
1926 if (ip->vp)
1927 cache_inval_vp(ip->vp, CINV_DESTROY);
1928 }
a89aec1b 1929 hammer_rel_inode(ip, 0);
66325755 1930 }
6a37e7e4
MD
1931 hammer_done_cursor(&cursor);
1932 if (error == EDEADLK)
1933 goto retry;
9c448776
MD
1934
1935 if (error == 0)
1936 hammer_commit_transaction(&trans);
1937 else
1938 hammer_abort_transaction(&trans);
66325755 1939 return (error);
66325755
MD
1940}
1941
7a04d74f
MD
1942/************************************************************************
1943 * FIFO AND SPECFS OPS *
1944 ************************************************************************
1945 *
1946 */
1947
1948static int
1949hammer_vop_fifoclose (struct vop_close_args *ap)
1950{
1951 /* XXX update itimes */
1952 return (VOCALL(&fifo_vnode_vops, &ap->a_head));
1953}
1954
1955static int
1956hammer_vop_fiforead (struct vop_read_args *ap)
1957{
1958 int error;
1959
1960 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1961 /* XXX update access time */
1962 return (error);
1963}
1964
1965static int
1966hammer_vop_fifowrite (struct vop_write_args *ap)
1967{
1968 int error;
1969
1970 error = VOCALL(&fifo_vnode_vops, &ap->a_head);
1971 /* XXX update access time */
1972 return (error);
1973}
1974
1975static int
1976hammer_vop_specclose (struct vop_close_args *ap)
1977{
1978 /* XXX update itimes */
1979 return (VOCALL(&spec_vnode_vops, &ap->a_head));
1980}
1981
1982static int
1983hammer_vop_specread (struct vop_read_args *ap)
1984{
1985 /* XXX update access time */
1986 return (VOCALL(&spec_vnode_vops, &ap->a_head));
1987}
1988
1989static int
1990hammer_vop_specwrite (struct vop_write_args *ap)
1991{
1992 /* XXX update last change time */
1993 return (VOCALL(&spec_vnode_vops, &ap->a_head));
1994}
1995