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