Sync the adduser(8) command with FreeBSD. Summary:
[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 *
195c19a1 34 * $DragonFly: src/sys/vfs/hammer/hammer_vnops.c,v 1.9 2007/11/30 00:16:56 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 &&
7f7c1f84 440 ip->obj_asof == XXX
66325755
MD
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 484 struct namecache *ncp;
7f7c1f84
MD
485 hammer_inode_t dip;
486 hammer_inode_t ip;
487 hammer_tid_t asof;
8cd0a023
MD
488 struct hammer_cursor cursor;
489 union hammer_record_ondisk *rec;
66325755
MD
490 struct vnode *vp;
491 int64_t namekey;
492 int error;
7f7c1f84
MD
493 int i;
494 int nlen;
495
496 /*
497 * Misc initialization, plus handle as-of name extensions. Look for
498 * the '@@' extension. Note that as-of files and directories cannot
499 * be modified.
500 *
501 *
502 */
503 dip = VTOI(ap->a_dvp);
504 ncp = ap->a_nch->ncp;
505 asof = dip->obj_asof;
506 nlen = ncp->nc_nlen;
507
508 for (i = 0; i < nlen; ++i) {
509 if (ncp->nc_name[i] == '@' && ncp->nc_name[i+1] == '@') {
510 asof = hammer_now_tid() -
511 strtoq(ncp->nc_name + i + 2, NULL, 0) *
512 1000000000LL;
513 kprintf("ASOF %016llx\n", asof);
514 break;
515 }
516 }
517 nlen = i;
66325755 518
8cd0a023
MD
519 /*
520 * Calculate the namekey and setup the key range for the scan. This
521 * works kinda like a chained hash table where the lower 32 bits
522 * of the namekey synthesize the chain.
523 *
524 * The key range is inclusive of both key_beg and key_end.
525 */
7f7c1f84 526 namekey = hammer_directory_namekey(ncp->nc_name, nlen);
66325755 527
8cd0a023
MD
528 hammer_init_cursor_ip(&cursor, dip);
529 cursor.key_beg.obj_id = dip->obj_id;
530 cursor.key_beg.key = namekey;
7f7c1f84 531 cursor.key_beg.create_tid = asof;
8cd0a023
MD
532 cursor.key_beg.delete_tid = 0;
533 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
534 cursor.key_beg.obj_type = 0;
66325755 535
8cd0a023
MD
536 cursor.key_end = cursor.key_beg;
537 cursor.key_end.key |= 0xFFFFFFFFULL;
66325755
MD
538
539 /*
8cd0a023 540 * Scan all matching records (the chain), locate the one matching
a89aec1b 541 * the requested path component.
8cd0a023
MD
542 *
543 * The hammer_ip_*() functions merge in-memory records with on-disk
544 * records for the purposes of the search.
66325755 545 */
a89aec1b
MD
546 error = hammer_ip_first(&cursor, dip);
547 while (error == 0) {
548 error = hammer_ip_resolve_data(&cursor);
549 if (error)
66325755 550 break;
a89aec1b 551 rec = cursor.record;
7f7c1f84
MD
552 if (nlen == rec->entry.base.data_len &&
553 bcmp(ncp->nc_name, cursor.data, nlen) == 0) {
66325755
MD
554 break;
555 }
a89aec1b 556 error = hammer_ip_next(&cursor);
66325755
MD
557 }
558 if (error == 0) {
7f7c1f84
MD
559 ip = hammer_get_inode(dip->hmp, rec->entry.obj_id,
560 asof, &error);
561 if (error == 0) {
562 error = hammer_get_vnode(ip, LK_EXCLUSIVE, &vp);
563 hammer_rel_inode(ip, 0);
564 } else {
565 vp = NULL;
566 }
66325755
MD
567 if (error == 0) {
568 vn_unlock(vp);
569 cache_setvp(ap->a_nch, vp);
570 vrele(vp);
571 }
572 } else if (error == ENOENT) {
573 cache_setvp(ap->a_nch, NULL);
574 }
8cd0a023 575 hammer_done_cursor(&cursor);
66325755 576 return (error);
427e5fc6
MD
577}
578
66325755
MD
579/*
580 * hammer_vop_nlookupdotdot { dvp, vpp, cred }
581 *
582 * Locate the parent directory of a directory vnode.
583 *
584 * dvp is referenced but not locked. *vpp must be returned referenced and
585 * locked. A parent_obj_id of 0 does not necessarily indicate that we are
586 * at the root, instead it could indicate that the directory we were in was
587 * removed.
588 */
427e5fc6
MD
589static
590int
66325755 591hammer_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
427e5fc6 592{
66325755
MD
593 struct hammer_inode *dip;
594 u_int64_t parent_obj_id;
595
596 dip = VTOI(ap->a_dvp);
597 if ((parent_obj_id = dip->ino_data.parent_obj_id) == 0) {
598 *ap->a_vpp = NULL;
599 return ENOENT;
600 }
601 return(hammer_vfs_vget(dip->hmp->mp, parent_obj_id, ap->a_vpp));
427e5fc6
MD
602}
603
66325755
MD
604/*
605 * hammer_vop_nlink { nch, dvp, vp, cred }
606 */
427e5fc6
MD
607static
608int
66325755 609hammer_vop_nlink(struct vop_nlink_args *ap)
427e5fc6 610{
66325755
MD
611 struct hammer_transaction trans;
612 struct hammer_inode *dip;
613 struct hammer_inode *ip;
614 struct nchandle *nch;
615 int error;
616
617 nch = ap->a_nch;
618 dip = VTOI(ap->a_dvp);
619 ip = VTOI(ap->a_vp);
620
621 /*
622 * Create a transaction to cover the operations we perform.
623 */
8cd0a023 624 hammer_start_transaction(&trans, dip->hmp);
66325755
MD
625
626 /*
627 * Add the filesystem object to the directory. Note that neither
628 * dip nor ip are referenced or locked, but their vnodes are
629 * referenced. This function will bump the inode's link count.
630 */
a89aec1b 631 error = hammer_ip_add_directory(&trans, dip, nch->ncp, ip);
66325755
MD
632
633 /*
634 * Finish up.
635 */
636 if (error) {
637 hammer_abort_transaction(&trans);
638 } else {
6b4f890b
MD
639 cache_setunresolved(nch);
640 cache_setvp(nch, ap->a_vp);
66325755
MD
641 hammer_commit_transaction(&trans);
642 }
643 return (error);
427e5fc6
MD
644}
645
66325755
MD
646/*
647 * hammer_vop_nmkdir { nch, dvp, vpp, cred, vap }
648 *
649 * The operating system has already ensured that the directory entry
650 * does not exist and done all appropriate namespace locking.
651 */
427e5fc6
MD
652static
653int
66325755 654hammer_vop_nmkdir(struct vop_nmkdir_args *ap)
427e5fc6 655{
66325755
MD
656 struct hammer_transaction trans;
657 struct hammer_inode *dip;
658 struct hammer_inode *nip;
659 struct nchandle *nch;
660 int error;
661
662 nch = ap->a_nch;
663 dip = VTOI(ap->a_dvp);
664
665 /*
666 * Create a transaction to cover the operations we perform.
667 */
8cd0a023 668 hammer_start_transaction(&trans, dip->hmp);
66325755
MD
669
670 /*
671 * Create a new filesystem object of the requested type. The
8cd0a023 672 * returned inode will be referenced but not locked.
66325755 673 */
8cd0a023 674 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
66325755
MD
675 if (error) {
676 hammer_abort_transaction(&trans);
677 *ap->a_vpp = NULL;
678 return (error);
679 }
66325755
MD
680
681 /*
682 * Add the new filesystem object to the directory. This will also
683 * bump the inode's link count.
684 */
a89aec1b 685 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
66325755
MD
686
687 /*
688 * Finish up.
689 */
690 if (error) {
a89aec1b 691 hammer_rel_inode(nip, 0);
66325755
MD
692 hammer_abort_transaction(&trans);
693 *ap->a_vpp = NULL;
694 } else {
695 hammer_commit_transaction(&trans);
696 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
a89aec1b
MD
697 hammer_rel_inode(nip, 0);
698 if (error == 0) {
699 cache_setunresolved(ap->a_nch);
700 cache_setvp(ap->a_nch, *ap->a_vpp);
701 }
66325755
MD
702 }
703 return (error);
427e5fc6
MD
704}
705
66325755
MD
706/*
707 * hammer_vop_nmknod { nch, dvp, vpp, cred, vap }
708 *
709 * The operating system has already ensured that the directory entry
710 * does not exist and done all appropriate namespace locking.
711 */
427e5fc6
MD
712static
713int
66325755 714hammer_vop_nmknod(struct vop_nmknod_args *ap)
427e5fc6 715{
66325755
MD
716 struct hammer_transaction trans;
717 struct hammer_inode *dip;
718 struct hammer_inode *nip;
719 struct nchandle *nch;
720 int error;
721
722 nch = ap->a_nch;
723 dip = VTOI(ap->a_dvp);
724
725 /*
726 * Create a transaction to cover the operations we perform.
727 */
8cd0a023 728 hammer_start_transaction(&trans, dip->hmp);
66325755
MD
729
730 /*
731 * Create a new filesystem object of the requested type. The
8cd0a023 732 * returned inode will be referenced but not locked.
66325755 733 */
8cd0a023 734 error = hammer_create_inode(&trans, ap->a_vap, ap->a_cred, dip, &nip);
66325755
MD
735 if (error) {
736 hammer_abort_transaction(&trans);
737 *ap->a_vpp = NULL;
738 return (error);
739 }
66325755
MD
740
741 /*
742 * Add the new filesystem object to the directory. This will also
743 * bump the inode's link count.
744 */
a89aec1b 745 error = hammer_ip_add_directory(&trans, dip, nch->ncp, nip);
66325755
MD
746
747 /*
748 * Finish up.
749 */
750 if (error) {
a89aec1b 751 hammer_rel_inode(nip, 0);
66325755
MD
752 hammer_abort_transaction(&trans);
753 *ap->a_vpp = NULL;
754 } else {
755 hammer_commit_transaction(&trans);
756 error = hammer_get_vnode(nip, LK_EXCLUSIVE, ap->a_vpp);
a89aec1b
MD
757 hammer_rel_inode(nip, 0);
758 if (error == 0) {
759 cache_setunresolved(ap->a_nch);
760 cache_setvp(ap->a_nch, *ap->a_vpp);
761 }
66325755
MD
762 }
763 return (error);
427e5fc6
MD
764}
765
66325755
MD
766/*
767 * hammer_vop_open { vp, mode, cred, fp }
768 */
427e5fc6
MD
769static
770int
66325755 771hammer_vop_open(struct vop_open_args *ap)
427e5fc6 772{
a89aec1b 773 return(vop_stdopen(ap));
427e5fc6
MD
774}
775
66325755
MD
776/*
777 * hammer_vop_pathconf { vp, name, retval }
778 */
427e5fc6
MD
779static
780int
66325755 781hammer_vop_pathconf(struct vop_pathconf_args *ap)
427e5fc6
MD
782{
783 return EOPNOTSUPP;
784}
785
66325755
MD
786/*
787 * hammer_vop_print { vp }
788 */
427e5fc6
MD
789static
790int
66325755 791hammer_vop_print(struct vop_print_args *ap)
427e5fc6
MD
792{
793 return EOPNOTSUPP;
794}
795
66325755 796/*
6b4f890b 797 * hammer_vop_readdir { vp, uio, cred, *eofflag, *ncookies, off_t **cookies }
66325755 798 */
427e5fc6
MD
799static
800int
66325755 801hammer_vop_readdir(struct vop_readdir_args *ap)
427e5fc6 802{
6b4f890b
MD
803 struct hammer_cursor cursor;
804 struct hammer_inode *ip;
805 struct uio *uio;
806 hammer_record_ondisk_t rec;
807 hammer_base_elm_t base;
808 int error;
809 int cookie_index;
810 int ncookies;
811 off_t *cookies;
812 off_t saveoff;
813 int r;
814
815 ip = VTOI(ap->a_vp);
816 uio = ap->a_uio;
817 hammer_init_cursor_ip(&cursor, ip);
818
819 /*
820 * Key range (begin and end inclusive) to scan. Directory keys
821 * directly translate to a 64 bit 'seek' position.
822 */
823 cursor.key_beg.obj_id = ip->obj_id;
824 cursor.key_beg.create_tid = ip->obj_asof;
825 cursor.key_beg.delete_tid = 0;
826 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
827 cursor.key_beg.obj_type = 0;
828 cursor.key_beg.key = uio->uio_offset;
829
830 cursor.key_end = cursor.key_beg;
831 cursor.key_end.key = HAMMER_MAX_KEY;
832
833 if (ap->a_ncookies) {
834 ncookies = uio->uio_resid / 16 + 1;
835 if (ncookies > 1024)
836 ncookies = 1024;
837 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
838 cookie_index = 0;
839 } else {
840 ncookies = -1;
841 cookies = NULL;
842 cookie_index = 0;
843 }
844
845 saveoff = cursor.key_beg.key;
846 error = hammer_ip_first(&cursor, ip);
847
848 while (error == 0) {
849 error = hammer_ip_resolve_data(&cursor);
850 if (error)
851 break;
852 rec = cursor.record;
853 base = &rec->base.base;
854 saveoff = base->key;
855
856 r = vop_write_dirent(
857 &error, uio, rec->entry.obj_id,
858 rec->entry.base.data_len,
859 hammer_get_dtype(rec->entry.base.base.obj_type),
860 (void *)cursor.data);
861 if (r)
862 break;
863 ++saveoff;
864 if (cookies)
865 cookies[cookie_index] = base->key;
866 ++cookie_index;
867 if (cookie_index == ncookies)
868 break;
869 error = hammer_ip_next(&cursor);
870 }
871 hammer_done_cursor(&cursor);
872
873 if (ap->a_eofflag)
874 *ap->a_eofflag = (error == ENOENT);
875 if (error == ENOENT)
876 error = 0;
877 uio->uio_offset = saveoff;
878 if (error && cookie_index == 0) {
879 if (cookies) {
880 kfree(cookies, M_TEMP);
881 *ap->a_ncookies = 0;
882 *ap->a_cookies = NULL;
883 }
884 } else {
885 error = 0;
886 if (cookies) {
887 *ap->a_ncookies = cookie_index;
888 *ap->a_cookies = cookies;
889 }
890 }
891 return(error);
427e5fc6
MD
892}
893
66325755
MD
894/*
895 * hammer_vop_readlink { vp, uio, cred }
896 */
427e5fc6
MD
897static
898int
66325755 899hammer_vop_readlink(struct vop_readlink_args *ap)
427e5fc6
MD
900{
901 return EOPNOTSUPP;
902}
903
66325755
MD
904/*
905 * hammer_vop_nremove { nch, dvp, cred }
906 */
427e5fc6
MD
907static
908int
66325755 909hammer_vop_nremove(struct vop_nremove_args *ap)
427e5fc6 910{
8cd0a023 911 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, 0));
427e5fc6
MD
912}
913
66325755
MD
914/*
915 * hammer_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
916 */
427e5fc6
MD
917static
918int
66325755 919hammer_vop_nrename(struct vop_nrename_args *ap)
427e5fc6 920{
8cd0a023
MD
921 struct hammer_transaction trans;
922 struct namecache *fncp;
923 struct namecache *tncp;
924 struct hammer_inode *fdip;
925 struct hammer_inode *tdip;
926 struct hammer_inode *ip;
927 struct hammer_cursor cursor;
928 union hammer_record_ondisk *rec;
929 int64_t namekey;
930 int error;
931
932 fdip = VTOI(ap->a_fdvp);
933 tdip = VTOI(ap->a_tdvp);
934 fncp = ap->a_fnch->ncp;
935 tncp = ap->a_tnch->ncp;
936 hammer_start_transaction(&trans, fdip->hmp);
937
938 /*
939 * Extract the hammer_inode from fncp and add link to the target
940 * directory.
941 */
942 ip = VTOI(fncp->nc_vp);
943 KKASSERT(ip != NULL);
944
a89aec1b 945 error = hammer_ip_add_directory(&trans, tdip, tncp, ip);
8cd0a023
MD
946
947 /*
948 * Locate the record in the originating directory and remove it.
949 *
950 * Calculate the namekey and setup the key range for the scan. This
951 * works kinda like a chained hash table where the lower 32 bits
952 * of the namekey synthesize the chain.
953 *
954 * The key range is inclusive of both key_beg and key_end.
955 */
956 namekey = hammer_directory_namekey(fncp->nc_name, fncp->nc_nlen);
957
958 hammer_init_cursor_ip(&cursor, fdip);
959 cursor.key_beg.obj_id = fdip->obj_id;
960 cursor.key_beg.key = namekey;
961 cursor.key_beg.create_tid = fdip->obj_asof;
962 cursor.key_beg.delete_tid = 0;
963 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
964 cursor.key_beg.obj_type = 0;
965
966 cursor.key_end = cursor.key_beg;
967 cursor.key_end.key |= 0xFFFFFFFFULL;
968
969 /*
970 * Scan all matching records (the chain), locate the one matching
a89aec1b 971 * the requested path component.
8cd0a023
MD
972 *
973 * The hammer_ip_*() functions merge in-memory records with on-disk
974 * records for the purposes of the search.
975 */
a89aec1b
MD
976 error = hammer_ip_first(&cursor, fdip);
977 while (error == 0) {
8cd0a023
MD
978 if (hammer_ip_resolve_data(&cursor) != 0)
979 break;
a89aec1b 980 rec = cursor.record;
8cd0a023
MD
981 if (fncp->nc_nlen == rec->entry.base.data_len &&
982 bcmp(fncp->nc_name, cursor.data, fncp->nc_nlen) == 0) {
983 break;
984 }
a89aec1b 985 error = hammer_ip_next(&cursor);
8cd0a023 986 }
8cd0a023
MD
987
988 /*
989 * If all is ok we have to get the inode so we can adjust nlinks.
990 */
991 if (error)
992 goto done;
a89aec1b 993 error = hammer_ip_del_directory(&trans, &cursor, fdip, ip);
8cd0a023
MD
994 if (error == 0) {
995 cache_rename(ap->a_fnch, ap->a_tnch);
996 cache_setvp(ap->a_tnch, ip->vp);
997 }
998done:
c0ade690 999 hammer_done_cursor(&cursor);
8cd0a023
MD
1000 if (error == 0) {
1001 hammer_commit_transaction(&trans);
1002 } else {
1003 hammer_abort_transaction(&trans);
1004 }
8cd0a023 1005 return (error);
427e5fc6
MD
1006}
1007
66325755
MD
1008/*
1009 * hammer_vop_nrmdir { nch, dvp, cred }
1010 */
427e5fc6
MD
1011static
1012int
66325755 1013hammer_vop_nrmdir(struct vop_nrmdir_args *ap)
427e5fc6 1014{
8cd0a023
MD
1015 /* XXX check that directory is empty */
1016
1017 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, 0));
427e5fc6
MD
1018}
1019
66325755
MD
1020/*
1021 * hammer_vop_setattr { vp, vap, cred }
1022 */
427e5fc6
MD
1023static
1024int
66325755 1025hammer_vop_setattr(struct vop_setattr_args *ap)
427e5fc6 1026{
8cd0a023
MD
1027 struct hammer_transaction trans;
1028 struct vattr *vap;
1029 struct hammer_inode *ip;
1030 int modflags;
1031 int error;
76376933 1032 int64_t aligned_size;
8cd0a023
MD
1033 u_int32_t flags;
1034 uuid_t uuid;
1035
1036 vap = ap->a_vap;
1037 ip = ap->a_vp->v_data;
1038 modflags = 0;
1039
1040 if (ap->a_vp->v_mount->mnt_flag & MNT_RDONLY)
1041 return(EROFS);
1042
1043 hammer_start_transaction(&trans, ip->hmp);
1044 error = 0;
1045
1046 if (vap->va_flags != VNOVAL) {
1047 flags = ip->ino_data.uflags;
1048 error = vop_helper_setattr_flags(&flags, vap->va_flags,
1049 hammer_to_unix_xid(&ip->ino_data.uid),
1050 ap->a_cred);
1051 if (error == 0) {
1052 if (ip->ino_data.uflags != flags) {
1053 ip->ino_data.uflags = flags;
1054 modflags |= HAMMER_INODE_DDIRTY;
1055 }
1056 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1057 error = 0;
1058 goto done;
1059 }
1060 }
1061 goto done;
1062 }
1063 if (ip->ino_data.uflags & (IMMUTABLE | APPEND)) {
1064 error = EPERM;
1065 goto done;
1066 }
1067 if (vap->va_uid != (uid_t)VNOVAL) {
1068 hammer_guid_to_uuid(&uuid, vap->va_uid);
6b4f890b 1069 if (bcmp(&uuid, &ip->ino_data.uid, sizeof(uuid)) != 0) {
8cd0a023
MD
1070 ip->ino_data.uid = uuid;
1071 modflags |= HAMMER_INODE_DDIRTY;
1072 }
1073 }
1074 if (vap->va_gid != (uid_t)VNOVAL) {
6b4f890b
MD
1075 hammer_guid_to_uuid(&uuid, vap->va_gid);
1076 if (bcmp(&uuid, &ip->ino_data.gid, sizeof(uuid)) != 0) {
8cd0a023
MD
1077 ip->ino_data.gid = uuid;
1078 modflags |= HAMMER_INODE_DDIRTY;
1079 }
1080 }
c0ade690 1081 if (vap->va_size != VNOVAL && ip->ino_rec.ino_size != vap->va_size) {
8cd0a023
MD
1082 switch(ap->a_vp->v_type) {
1083 case VREG:
c0ade690
MD
1084 if (vap->va_size < ip->ino_rec.ino_size) {
1085 vtruncbuf(ap->a_vp, vap->va_size,
1086 HAMMER_BUFSIZE);
1087 } else if (vap->va_size > ip->ino_rec.ino_size) {
1088 vnode_pager_setsize(ap->a_vp, vap->va_size);
1089 }
76376933
MD
1090 aligned_size = (vap->va_size + HAMMER_BUFMASK) &
1091 ~(int64_t)HAMMER_BUFMASK;
1092 error = hammer_ip_delete_range(&trans, ip,
1093 aligned_size,
1094 0x7FFFFFFFFFFFFFFFLL);
1095 ip->ino_rec.ino_size = vap->va_size;
1096 modflags |= HAMMER_INODE_RDIRTY;
1097 break;
8cd0a023 1098 case VDATABASE:
a89aec1b 1099 error = hammer_ip_delete_range(&trans, ip,
8cd0a023
MD
1100 vap->va_size,
1101 0x7FFFFFFFFFFFFFFFLL);
c0ade690
MD
1102 ip->ino_rec.ino_size = vap->va_size;
1103 modflags |= HAMMER_INODE_RDIRTY;
8cd0a023
MD
1104 break;
1105 default:
1106 error = EINVAL;
1107 goto done;
1108 }
1109 }
1110 if (vap->va_atime.tv_sec != VNOVAL) {
1111 ip->ino_rec.ino_atime =
1112 hammer_timespec_to_transid(&vap->va_atime);
1113 modflags |= HAMMER_INODE_ITIMES;
1114 }
1115 if (vap->va_mtime.tv_sec != VNOVAL) {
1116 ip->ino_rec.ino_mtime =
1117 hammer_timespec_to_transid(&vap->va_mtime);
1118 modflags |= HAMMER_INODE_ITIMES;
1119 }
1120 if (vap->va_mode != (mode_t)VNOVAL) {
1121 if (ip->ino_data.mode != vap->va_mode) {
1122 ip->ino_data.mode = vap->va_mode;
1123 modflags |= HAMMER_INODE_DDIRTY;
1124 }
1125 }
1126done:
1127 if (error) {
1128 hammer_abort_transaction(&trans);
1129 } else {
c0ade690
MD
1130 if (modflags & (HAMMER_INODE_RDIRTY | HAMMER_INODE_DDIRTY))
1131 modflags |= HAMMER_INODE_TID;
1132 hammer_modify_inode(&trans, ip, modflags);
8cd0a023
MD
1133 hammer_commit_transaction(&trans);
1134 }
1135 return (error);
427e5fc6
MD
1136}
1137
66325755
MD
1138/*
1139 * hammer_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1140 */
427e5fc6
MD
1141static
1142int
66325755 1143hammer_vop_nsymlink(struct vop_nsymlink_args *ap)
427e5fc6
MD
1144{
1145 return EOPNOTSUPP;
1146}
1147
66325755
MD
1148/*
1149 * hammer_vop_nwhiteout { nch, dvp, cred, flags }
1150 */
427e5fc6
MD
1151static
1152int
66325755 1153hammer_vop_nwhiteout(struct vop_nwhiteout_args *ap)
427e5fc6 1154{
8cd0a023 1155 return(hammer_dounlink(ap->a_nch, ap->a_dvp, ap->a_cred, ap->a_flags));
427e5fc6
MD
1156}
1157
66325755
MD
1158/*
1159 * hammer_vop_strategy { vp, bio }
8cd0a023
MD
1160 *
1161 * Strategy call, used for regular file read & write only. Note that the
1162 * bp may represent a cluster.
1163 *
1164 * To simplify operation and allow better optimizations in the future,
1165 * this code does not make any assumptions with regards to buffer alignment
1166 * or size.
66325755 1167 */
427e5fc6
MD
1168static
1169int
66325755 1170hammer_vop_strategy(struct vop_strategy_args *ap)
427e5fc6 1171{
8cd0a023
MD
1172 struct buf *bp;
1173 int error;
1174
1175 bp = ap->a_bio->bio_buf;
1176
1177 switch(bp->b_cmd) {
1178 case BUF_CMD_READ:
1179 error = hammer_vop_strategy_read(ap);
1180 break;
1181 case BUF_CMD_WRITE:
1182 error = hammer_vop_strategy_write(ap);
1183 break;
1184 default:
1185 error = EINVAL;
1186 break;
1187 }
1188 bp->b_error = error;
1189 if (error)
1190 bp->b_flags |= B_ERROR;
1191 biodone(ap->a_bio);
1192 return (error);
427e5fc6
MD
1193}
1194
8cd0a023
MD
1195/*
1196 * Read from a regular file. Iterate the related records and fill in the
1197 * BIO/BUF. Gaps are zero-filled.
1198 *
1199 * The support code in hammer_object.c should be used to deal with mixed
1200 * in-memory and on-disk records.
1201 *
1202 * XXX atime update
1203 */
1204static
1205int
1206hammer_vop_strategy_read(struct vop_strategy_args *ap)
1207{
1208 struct hammer_inode *ip = ap->a_vp->v_data;
1209 struct hammer_cursor cursor;
1210 hammer_record_ondisk_t rec;
1211 hammer_base_elm_t base;
1212 struct bio *bio;
1213 struct buf *bp;
1214 int64_t rec_offset;
a89aec1b 1215 int64_t ran_end;
195c19a1 1216 int64_t tmp64;
8cd0a023
MD
1217 int error;
1218 int boff;
1219 int roff;
1220 int n;
1221
1222 bio = ap->a_bio;
1223 bp = bio->bio_buf;
1224
1225 hammer_init_cursor_ip(&cursor, ip);
1226
1227 /*
1228 * Key range (begin and end inclusive) to scan. Note that the key's
c0ade690
MD
1229 * stored in the actual records represent BASE+LEN, not BASE. The
1230 * first record containing bio_offset will have a key > bio_offset.
8cd0a023
MD
1231 */
1232 cursor.key_beg.obj_id = ip->obj_id;
1233 cursor.key_beg.create_tid = ip->obj_asof;
1234 cursor.key_beg.delete_tid = 0;
8cd0a023 1235 cursor.key_beg.obj_type = 0;
c0ade690 1236 cursor.key_beg.key = bio->bio_offset + 1;
8cd0a023
MD
1237
1238 cursor.key_end = cursor.key_beg;
a89aec1b
MD
1239 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1240 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
1241 cursor.key_end.rec_type = HAMMER_RECTYPE_DB;
1242 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1243 } else {
c0ade690 1244 ran_end = bio->bio_offset + bp->b_bufsize;
a89aec1b
MD
1245 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
1246 cursor.key_end.rec_type = HAMMER_RECTYPE_DATA;
195c19a1
MD
1247 tmp64 = ran_end + MAXPHYS + 1; /* work-around GCC-4 bug */
1248 if (tmp64 < ran_end)
a89aec1b
MD
1249 cursor.key_end.key = 0x7FFFFFFFFFFFFFFFLL;
1250 else
7f7c1f84 1251 cursor.key_end.key = ran_end + MAXPHYS + 1;
a89aec1b 1252 }
8cd0a023 1253
a89aec1b 1254 error = hammer_ip_first(&cursor, ip);
8cd0a023
MD
1255 boff = 0;
1256
a89aec1b
MD
1257 while (error == 0) {
1258 error = hammer_ip_resolve_data(&cursor);
1259 if (error)
66325755 1260 break;
a89aec1b 1261 rec = cursor.record;
8cd0a023
MD
1262 base = &rec->base.base;
1263
c0ade690 1264 rec_offset = base->key - rec->data.base.data_len;
8cd0a023 1265
66325755 1266 /*
a89aec1b 1267 * Calculate the gap, if any, and zero-fill it.
66325755 1268 */
8cd0a023
MD
1269 n = (int)(rec_offset - (bio->bio_offset + boff));
1270 if (n > 0) {
a89aec1b
MD
1271 if (n > bp->b_bufsize - boff)
1272 n = bp->b_bufsize - boff;
8cd0a023
MD
1273 kprintf("zfill %d bytes\n", n);
1274 bzero((char *)bp->b_data + boff, n);
1275 boff += n;
1276 n = 0;
66325755 1277 }
8cd0a023
MD
1278
1279 /*
1280 * Calculate the data offset in the record and the number
1281 * of bytes we can copy.
a89aec1b
MD
1282 *
1283 * Note there is a degenerate case here where boff may
1284 * already be at bp->b_bufsize.
8cd0a023
MD
1285 */
1286 roff = -n;
1287 n = rec->data.base.data_len - roff;
1288 KKASSERT(n > 0);
1289 if (n > bp->b_bufsize - boff)
1290 n = bp->b_bufsize - boff;
1291 bcopy((char *)cursor.data + roff, (char *)bp->b_data + boff, n);
1292 boff += n;
1293 if (boff == bp->b_bufsize)
66325755 1294 break;
a89aec1b 1295 error = hammer_ip_next(&cursor);
66325755 1296 }
8cd0a023 1297 hammer_done_cursor(&cursor);
66325755
MD
1298
1299 /*
8cd0a023 1300 * There may have been a gap after the last record
66325755 1301 */
8cd0a023
MD
1302 if (error == ENOENT)
1303 error = 0;
1304 if (error == 0 && boff != bp->b_bufsize) {
7f7c1f84 1305 KKASSERT(boff < bp->b_bufsize);
8cd0a023
MD
1306 bzero((char *)bp->b_data + boff, bp->b_bufsize - boff);
1307 /* boff = bp->b_bufsize; */
1308 }
1309 bp->b_resid = 0;
1310 return(error);
1311}
1312
1313/*
1314 * Write to a regular file. Iterate the related records and mark for
1315 * deletion. If existing edge records (left and right side) overlap our
1316 * write they have to be marked deleted and new records created, usually
1317 * referencing a portion of the original data. Then add a record to
1318 * represent the buffer.
1319 *
1320 * The support code in hammer_object.c should be used to deal with mixed
1321 * in-memory and on-disk records.
1322 */
1323static
1324int
1325hammer_vop_strategy_write(struct vop_strategy_args *ap)
1326{
1327 struct hammer_transaction trans;
1328 hammer_inode_t ip;
1329 struct bio *bio;
1330 struct buf *bp;
1331 int error;
1332
1333 bio = ap->a_bio;
1334 bp = bio->bio_buf;
1335 ip = ap->a_vp->v_data;
1336 hammer_start_transaction(&trans, ip->hmp);
1337
1338 /*
1339 * Delete any records overlapping our range. This function will
1340 * properly
1341 */
a89aec1b
MD
1342 if (ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_DBFILE) {
1343 error = hammer_ip_delete_range(&trans, ip, bio->bio_offset,
1344 bio->bio_offset);
1345 } else {
1346 error = hammer_ip_delete_range(&trans, ip, bio->bio_offset,
1347 bio->bio_offset + bp->b_bufsize - 1);
1348 }
8cd0a023
MD
1349
1350 /*
1351 * Add a single record to cover the write
1352 */
1353 if (error == 0) {
c0ade690
MD
1354 error = hammer_ip_sync_data(&trans, ip, bio->bio_offset,
1355 bp->b_data, bp->b_bufsize);
66325755
MD
1356 }
1357
1358 /*
8cd0a023 1359 * If an error occured abort the transaction
66325755 1360 */
8cd0a023
MD
1361 if (error) {
1362 /* XXX undo deletion */
1363 hammer_abort_transaction(&trans);
1364 bp->b_resid = bp->b_bufsize;
1365 } else {
1366 hammer_commit_transaction(&trans);
1367 bp->b_resid = 0;
1368 }
1369 return(error);
1370}
1371
1372/*
1373 * dounlink - disconnect a directory entry
1374 *
1375 * XXX whiteout support not really in yet
1376 */
1377static int
1378hammer_dounlink(struct nchandle *nch, struct vnode *dvp, struct ucred *cred,
1379 int flags)
1380{
1381 struct hammer_transaction trans;
1382 struct namecache *ncp;
1383 hammer_inode_t dip;
1384 hammer_inode_t ip;
1385 hammer_record_ondisk_t rec;
1386 struct hammer_cursor cursor;
8cd0a023
MD
1387 int64_t namekey;
1388 int error;
1389
1390 /*
1391 * Calculate the namekey and setup the key range for the scan. This
1392 * works kinda like a chained hash table where the lower 32 bits
1393 * of the namekey synthesize the chain.
1394 *
1395 * The key range is inclusive of both key_beg and key_end.
1396 */
1397 dip = VTOI(dvp);
1398 ncp = nch->ncp;
1399 namekey = hammer_directory_namekey(ncp->nc_name, ncp->nc_nlen);
1400
1401 hammer_init_cursor_ip(&cursor, dip);
1402 cursor.key_beg.obj_id = dip->obj_id;
1403 cursor.key_beg.key = namekey;
1404 cursor.key_beg.create_tid = dip->obj_asof;
1405 cursor.key_beg.delete_tid = 0;
1406 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
1407 cursor.key_beg.obj_type = 0;
1408
1409 cursor.key_end = cursor.key_beg;
1410 cursor.key_end.key |= 0xFFFFFFFFULL;
1411
1412 hammer_start_transaction(&trans, dip->hmp);
1413
1414 /*
1415 * Scan all matching records (the chain), locate the one matching
1416 * the requested path component. info->last_error contains the
1417 * error code on search termination and could be 0, ENOENT, or
1418 * something else.
1419 *
1420 * The hammer_ip_*() functions merge in-memory records with on-disk
1421 * records for the purposes of the search.
1422 */
a89aec1b
MD
1423 error = hammer_ip_first(&cursor, dip);
1424 while (error == 0) {
1425 error = hammer_ip_resolve_data(&cursor);
1426 if (error)
66325755 1427 break;
a89aec1b 1428 rec = cursor.record;
8cd0a023
MD
1429 if (ncp->nc_nlen == rec->entry.base.data_len &&
1430 bcmp(ncp->nc_name, cursor.data, ncp->nc_nlen) == 0) {
66325755
MD
1431 break;
1432 }
a89aec1b 1433 error = hammer_ip_next(&cursor);
66325755 1434 }
8cd0a023
MD
1435
1436 /*
1437 * If all is ok we have to get the inode so we can adjust nlinks.
1438 */
66325755 1439 if (error == 0) {
7f7c1f84
MD
1440 ip = hammer_get_inode(dip->hmp, rec->entry.obj_id,
1441 dip->hmp->asof, &error);
8cd0a023 1442 if (error == 0)
a89aec1b 1443 error = hammer_ip_del_directory(&trans, &cursor, dip, ip);
8cd0a023
MD
1444 if (error == 0) {
1445 cache_setunresolved(nch);
1446 cache_setvp(nch, NULL);
1447 /* XXX locking */
1448 if (ip->vp)
1449 cache_inval_vp(ip->vp, CINV_DESTROY);
1450 }
a89aec1b 1451 hammer_rel_inode(ip, 0);
8cd0a023 1452
66325755 1453 if (error == 0) {
8cd0a023
MD
1454 hammer_commit_transaction(&trans);
1455 } else {
1456 hammer_abort_transaction(&trans);
66325755 1457 }
66325755 1458 }
8cd0a023 1459 hammer_done_cursor(&cursor);
66325755 1460 return (error);
66325755
MD
1461}
1462