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