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