pktgen: Clear ip.ip_sum before calling in_cksum_hdr()
[dragonfly.git] / sys / vfs / hammer2 / hammer2_vnops.c
CommitLineData
e118c14f
MD
1/*
2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * 3. Neither the name of The DragonFly Project nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific, prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
703720e4
MD
35#include <sys/param.h>
36#include <sys/systm.h>
37#include <sys/kernel.h>
38#include <sys/fcntl.h>
39#include <sys/buf.h>
40#include <sys/proc.h>
41#include <sys/namei.h>
42#include <sys/mount.h>
43#include <sys/vnode.h>
f0206a67 44#include <sys/mountctl.h>
e028fa74 45#include <sys/dirent.h>
4e2004ea 46#include <sys/uio.h>
703720e4
MD
47
48#include "hammer2.h"
49
db71f61f
MD
50#define ZFOFFSET (-2LL)
51
4e2004ea
MD
52static int hammer2_read_file(hammer2_inode_t *ip, struct uio *uio,
53 int seqcount);
1c9f601e
MD
54static int hammer2_write_file(hammer2_inode_t *ip, struct uio *uio, int ioflag,
55 int seqcount);
8cce658d
MD
56static hammer2_off_t hammer2_assign_physical(hammer2_inode_t *ip,
57 hammer2_key_t lbase, int lblksize, int *errorp);
58static void hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize);
3ac6a319
MD
59static void hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize);
60
b2b78aaa
MD
61static __inline
62void
63hammer2_knote(struct vnode *vp, int flags)
64{
65 if (flags)
66 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, flags);
67}
68
703720e4
MD
69/*
70 * Last reference to a vnode is going away but it is still cached.
71 */
e118c14f 72static
703720e4 73int
e118c14f 74hammer2_vop_inactive(struct vop_inactive_args *ap)
703720e4
MD
75{
76 struct vnode *vp;
77 struct hammer2_inode *ip;
e118c14f 78#if 0
703720e4 79 struct hammer2_mount *hmp;
e118c14f 80#endif
703720e4 81
703720e4
MD
82 vp = ap->a_vp;
83 ip = VTOI(vp);
703720e4 84
df9ea374
MD
85 /*
86 * Degenerate case
87 */
88 if (ip == NULL) {
89 vrecycle(vp);
90 return (0);
91 }
92
214f4a77
MD
93 /*
94 * Detect updates to the embedded data which may be synchronized by
95 * the strategy code. Simply mark the inode modified so it gets
96 * picked up by our normal flush.
97 */
98 if (ip->chain.flags & HAMMER2_CHAIN_DIRTYEMBED) {
99 hammer2_inode_lock_ex(ip);
100 atomic_clear_int(&ip->chain.flags, HAMMER2_CHAIN_DIRTYEMBED);
01eabad4 101 hammer2_chain_modify(ip->hmp, &ip->chain, 0);
214f4a77
MD
102 hammer2_inode_unlock_ex(ip);
103 }
104
105 /*
106 * Check for deleted inodes and recycle immediately.
107 */
108 if (ip->chain.flags & HAMMER2_CHAIN_DELETED) {
109 vrecycle(vp);
110 }
703720e4
MD
111 return (0);
112}
113
114/*
115 * Reclaim a vnode so that it can be reused; after the inode is
116 * disassociated, the filesystem must manage it alone.
117 */
e118c14f 118static
703720e4 119int
e118c14f 120hammer2_vop_reclaim(struct vop_reclaim_args *ap)
703720e4 121{
703720e4
MD
122 struct hammer2_inode *ip;
123 struct hammer2_mount *hmp;
b7926f31 124 struct vnode *vp;
703720e4 125
703720e4
MD
126 vp = ap->a_vp;
127 ip = VTOI(vp);
9c2e0de0
MD
128 if (ip == NULL)
129 return(0);
9c2e0de0 130 hmp = ip->hmp;
b7926f31 131
1c9f601e
MD
132 /*
133 * Set SUBMODIFIED so we can detect and propagate the DESTROYED
134 * bit in the flush code.
135 */
54eb943b 136 hammer2_inode_lock_ex(ip);
703720e4 137 vp->v_data = NULL;
0e92b724 138 ip->vp = NULL;
1c9f601e
MD
139 if (ip->chain.flags & HAMMER2_CHAIN_DELETED) {
140 atomic_set_int(&ip->chain.flags, HAMMER2_CHAIN_DESTROYED |
141 HAMMER2_CHAIN_SUBMODIFIED);
142 }
4d5318eb 143 hammer2_chain_flush(hmp, &ip->chain, 0);
54eb943b 144 hammer2_inode_unlock_ex(ip);
222d9e22 145 hammer2_chain_drop(hmp, &ip->chain); /* vp ref */
54eb943b
MD
146
147 /*
148 * XXX handle background sync when ip dirty, kernel will no longer
149 * notify us regarding this inode because there is no longer a
150 * vnode attached to it.
151 */
703720e4
MD
152
153 return (0);
154}
155
e118c14f 156static
703720e4 157int
e118c14f 158hammer2_vop_fsync(struct vop_fsync_args *ap)
703720e4 159{
b7926f31
MD
160 struct hammer2_inode *ip;
161 struct hammer2_mount *hmp;
162 struct vnode *vp;
163
164 vp = ap->a_vp;
165 ip = VTOI(vp);
166 hmp = ip->hmp;
167
168 hammer2_inode_lock_ex(ip);
169 vfsync(vp, ap->a_waitfor, 1, NULL, NULL);
6ba3b984 170
214f4a77
MD
171 /*
172 * Detect updates to the embedded data which may be synchronized by
173 * the strategy code. Simply mark the inode modified so it gets
174 * picked up by our normal flush.
175 */
176 if (ip->chain.flags & HAMMER2_CHAIN_DIRTYEMBED) {
177 atomic_clear_int(&ip->chain.flags, HAMMER2_CHAIN_DIRTYEMBED);
01eabad4 178 hammer2_chain_modify(hmp, &ip->chain, 0);
214f4a77
MD
179 }
180
6ba3b984
MD
181 /*
182 * Calling chain_flush here creates a lot of duplicative
183 * COW operations due to non-optimal vnode ordering.
184 *
185 * Only do it for an actual fsync() syscall. The other forms
186 * which call this function will eventually call chain_flush
187 * on the volume root as a catch-all, which is far more optimal.
188 */
189 if (ap->a_flags & VOP_FSYNC_SYSCALL)
4d5318eb 190 hammer2_chain_flush(hmp, &ip->chain, 0);
b7926f31
MD
191 hammer2_inode_unlock_ex(ip);
192 return (0);
703720e4
MD
193}
194
e118c14f 195static
703720e4 196int
e118c14f 197hammer2_vop_access(struct vop_access_args *ap)
703720e4 198{
37494cab
MD
199 hammer2_inode_t *ip = VTOI(ap->a_vp);
200 uid_t uid;
201 gid_t gid;
202 int error;
203
204 uid = hammer2_to_unix_xid(&ip->ip_data.uid);
205 gid = hammer2_to_unix_xid(&ip->ip_data.gid);
206
207 error = vop_helper_access(ap, uid, gid, ip->ip_data.mode,
208 ip->ip_data.uflags);
209 return (error);
703720e4
MD
210}
211
e118c14f 212static
703720e4 213int
e118c14f 214hammer2_vop_getattr(struct vop_getattr_args *ap)
703720e4 215{
e4e20f48 216 hammer2_pfsmount_t *pmp;
cd4b3d92 217 hammer2_inode_t *ip;
703720e4
MD
218 struct vnode *vp;
219 struct vattr *vap;
703720e4
MD
220
221 vp = ap->a_vp;
222 vap = ap->a_vap;
223
cd4b3d92 224 ip = VTOI(vp);
e4e20f48 225 pmp = ip->pmp;
cd4b3d92 226
703720e4
MD
227 hammer2_inode_lock_sh(ip);
228
e4e20f48 229 vap->va_fsid = pmp->mp->mnt_stat.f_fsid.val[0];
cd4b3d92
MD
230 vap->va_fileid = ip->ip_data.inum;
231 vap->va_mode = ip->ip_data.mode;
232 vap->va_nlink = ip->ip_data.nlinks;
b2b78aaa
MD
233 vap->va_uid = hammer2_to_unix_xid(&ip->ip_data.uid);
234 vap->va_gid = hammer2_to_unix_xid(&ip->ip_data.gid);
cd4b3d92
MD
235 vap->va_rmajor = 0;
236 vap->va_rminor = 0;
237 vap->va_size = ip->ip_data.size;
df9ea374 238 vap->va_blocksize = HAMMER2_PBUFSIZE;
cd4b3d92
MD
239 vap->va_flags = ip->ip_data.uflags;
240 hammer2_time_to_timespec(ip->ip_data.ctime, &vap->va_ctime);
241 hammer2_time_to_timespec(ip->ip_data.mtime, &vap->va_mtime);
242 hammer2_time_to_timespec(ip->ip_data.mtime, &vap->va_atime);
243 vap->va_gen = 1;
866d5273 244 vap->va_bytes = vap->va_size; /* XXX */
cd4b3d92
MD
245 vap->va_type = hammer2_get_vtype(ip);
246 vap->va_filerev = 0;
247 vap->va_uid_uuid = ip->ip_data.uid;
248 vap->va_gid_uuid = ip->ip_data.gid;
249 vap->va_vaflags = VA_UID_UUID_VALID | VA_GID_UUID_VALID |
250 VA_FSID_UUID_VALID;
703720e4
MD
251
252 hammer2_inode_unlock_sh(ip);
253
254 return (0);
255}
256
3ac6a319
MD
257static
258int
259hammer2_vop_setattr(struct vop_setattr_args *ap)
260{
261 hammer2_mount_t *hmp;
262 hammer2_inode_t *ip;
263 struct vnode *vp;
264 struct vattr *vap;
265 int error;
266 int kflags = 0;
3ac6a319 267 int domtime = 0;
b2b78aaa 268 uint64_t ctime;
3ac6a319
MD
269
270 vp = ap->a_vp;
271 vap = ap->a_vap;
b2b78aaa 272 hammer2_update_time(&ctime);
3ac6a319
MD
273
274 ip = VTOI(vp);
275 hmp = ip->hmp;
276
277 if (hmp->ronly)
278 return(EROFS);
279
280 hammer2_inode_lock_ex(ip);
281 error = 0;
282
283 if (vap->va_flags != VNOVAL) {
284 u_int32_t flags;
285
286 flags = ip->ip_data.uflags;
287 error = vop_helper_setattr_flags(&flags, vap->va_flags,
288 hammer2_to_unix_xid(&ip->ip_data.uid),
289 ap->a_cred);
290 if (error == 0) {
291 if (ip->ip_data.uflags != flags) {
01eabad4 292 hammer2_chain_modify(hmp, &ip->chain, 0);
3ac6a319 293 ip->ip_data.uflags = flags;
b2b78aaa 294 ip->ip_data.ctime = ctime;
3ac6a319
MD
295 kflags |= NOTE_ATTRIB;
296 }
297 if (ip->ip_data.uflags & (IMMUTABLE | APPEND)) {
298 error = 0;
299 goto done;
300 }
301 }
b2b78aaa 302 goto done;
3ac6a319 303 }
3ac6a319
MD
304 if (ip->ip_data.uflags & (IMMUTABLE | APPEND)) {
305 error = EPERM;
306 goto done;
307 }
b2b78aaa
MD
308 if (vap->va_uid != (uid_t)VNOVAL || vap->va_gid != (gid_t)VNOVAL) {
309 mode_t cur_mode = ip->ip_data.mode;
310 uid_t cur_uid = hammer2_to_unix_xid(&ip->ip_data.uid);
311 gid_t cur_gid = hammer2_to_unix_xid(&ip->ip_data.gid);
312 uuid_t uuid_uid;
313 uuid_t uuid_gid;
314
315 error = vop_helper_chown(ap->a_vp, vap->va_uid, vap->va_gid,
316 ap->a_cred,
317 &cur_uid, &cur_gid, &cur_mode);
318 if (error == 0) {
319 hammer2_guid_to_uuid(&uuid_uid, cur_uid);
320 hammer2_guid_to_uuid(&uuid_gid, cur_gid);
321 if (bcmp(&uuid_uid, &ip->ip_data.uid,
322 sizeof(uuid_uid)) ||
323 bcmp(&uuid_gid, &ip->ip_data.gid,
324 sizeof(uuid_gid)) ||
325 ip->ip_data.mode != cur_mode
326 ) {
327 hammer2_chain_modify(hmp, &ip->chain, 0);
328 ip->ip_data.uid = uuid_uid;
329 ip->ip_data.gid = uuid_gid;
330 ip->ip_data.mode = cur_mode;
331 ip->ip_data.ctime = ctime;
332 }
333 kflags |= NOTE_ATTRIB;
334 }
335 }
3ac6a319
MD
336
337 /*
338 * Resize the file
339 */
340 if (vap->va_size != VNOVAL && ip->ip_data.size != vap->va_size) {
341 switch(vp->v_type) {
342 case VREG:
343 if (vap->va_size == ip->ip_data.size)
344 break;
345 if (vap->va_size < ip->ip_data.size) {
3ac6a319 346 hammer2_truncate_file(ip, vap->va_size);
3ac6a319 347 } else {
8cce658d 348 hammer2_extend_file(ip, vap->va_size);
3ac6a319
MD
349 }
350 domtime = 1;
351 break;
352 default:
353 error = EINVAL;
354 goto done;
355 }
356 }
b2b78aaa
MD
357#if 0
358 /* atime not supported */
359 if (vap->va_atime.tv_sec != VNOVAL) {
360 hammer2_chain_modify(hmp, &ip->chain, 0);
361 ip->ip_data.atime = hammer2_timespec_to_time(&vap->va_atime);
362 kflags |= NOTE_ATTRIB;
363 }
364#endif
365 if (vap->va_mtime.tv_sec != VNOVAL) {
366 hammer2_chain_modify(hmp, &ip->chain, 0);
367 ip->ip_data.mtime = hammer2_timespec_to_time(&vap->va_mtime);
368 kflags |= NOTE_ATTRIB;
369 }
370 if (vap->va_mode != (mode_t)VNOVAL) {
371 mode_t cur_mode = ip->ip_data.mode;
372 uid_t cur_uid = hammer2_to_unix_xid(&ip->ip_data.uid);
373 gid_t cur_gid = hammer2_to_unix_xid(&ip->ip_data.gid);
374
375 error = vop_helper_chmod(ap->a_vp, vap->va_mode, ap->a_cred,
376 cur_uid, cur_gid, &cur_mode);
377 if (error == 0 && ip->ip_data.mode != cur_mode) {
378 ip->ip_data.mode = cur_mode;
379 ip->ip_data.ctime = ctime;
380 kflags |= NOTE_ATTRIB;
381 }
382 }
3ac6a319
MD
383done:
384 hammer2_inode_unlock_ex(ip);
385 return (error);
386}
387
e118c14f 388static
703720e4 389int
e118c14f 390hammer2_vop_readdir(struct vop_readdir_args *ap)
703720e4 391{
e028fa74
MD
392 hammer2_mount_t *hmp;
393 hammer2_inode_t *ip;
394 hammer2_inode_t *xip;
395 hammer2_chain_t *parent;
396 hammer2_chain_t *chain;
397 hammer2_key_t lkey;
398 struct uio *uio;
399 off_t *cookies;
400 off_t saveoff;
401 int cookie_index;
402 int ncookies;
403 int error;
404 int dtype;
405 int r;
406
407 ip = VTOI(ap->a_vp);
408 hmp = ip->hmp;
409 uio = ap->a_uio;
410 saveoff = uio->uio_offset;
411
412 /*
413 * Setup cookies directory entry cookies if requested
414 */
415 if (ap->a_ncookies) {
416 ncookies = uio->uio_resid / 16 + 1;
417 if (ncookies > 1024)
418 ncookies = 1024;
419 cookies = kmalloc(ncookies * sizeof(off_t), M_TEMP, M_WAITOK);
420 } else {
421 ncookies = -1;
422 cookies = NULL;
423 }
424 cookie_index = 0;
425
426 /*
427 * Handle artificial entries. To ensure that only positive 64 bit
428 * quantities are returned to userland we always strip off bit 63.
429 * The hash code is designed such that codes 0x0000-0x7FFF are not
430 * used, allowing us to use these codes for articial entries.
431 *
432 * Entry 0 is used for '.' and entry 1 is used for '..'. Do not
433 * allow '..' to cross the mount point into (e.g.) the super-root.
434 */
435 error = 0;
37aa19df 436 chain = (void *)(intptr_t)-1; /* non-NULL for early goto done case */
e028fa74
MD
437
438 if (saveoff == 0) {
439 r = vop_write_dirent(&error, uio,
440 ip->ip_data.inum &
441 HAMMER2_DIRHASH_USERMSK,
442 DT_DIR, 1, ".");
443 if (r)
444 goto done;
445 if (cookies)
446 cookies[cookie_index] = saveoff;
447 ++saveoff;
448 ++cookie_index;
449 if (cookie_index == ncookies)
450 goto done;
451 }
452 if (saveoff == 1) {
e4e20f48 453 if (ip->pip == NULL || ip == ip->pmp->iroot)
e028fa74
MD
454 xip = ip;
455 else
456 xip = ip->pip;
457
458 r = vop_write_dirent(&error, uio,
459 xip->ip_data.inum &
460 HAMMER2_DIRHASH_USERMSK,
461 DT_DIR, 2, "..");
462 if (r)
463 goto done;
464 if (cookies)
465 cookies[cookie_index] = saveoff;
466 ++saveoff;
467 ++cookie_index;
468 if (cookie_index == ncookies)
469 goto done;
470 }
471
472 lkey = saveoff | HAMMER2_DIRHASH_VISIBLE;
473
474 parent = &ip->chain;
a0ed3c24
MD
475 error = hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS |
476 HAMMER2_RESOLVE_SHARED);
e028fa74 477 if (error) {
01eabad4 478 hammer2_chain_unlock(hmp, parent);
e028fa74
MD
479 goto done;
480 }
a0ed3c24
MD
481 chain = hammer2_chain_lookup(hmp, &parent, lkey, lkey,
482 HAMMER2_LOOKUP_SHARED);
37aa19df
MD
483 if (chain == NULL) {
484 chain = hammer2_chain_lookup(hmp, &parent,
a0ed3c24
MD
485 lkey, (hammer2_key_t)-1,
486 HAMMER2_LOOKUP_SHARED);
37aa19df 487 }
e028fa74 488 while (chain) {
c667909f
MD
489 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
490 dtype = hammer2_get_dtype(chain->u.ip);
491 saveoff = chain->bref.key & HAMMER2_DIRHASH_USERMSK;
492 r = vop_write_dirent(&error, uio,
493 chain->u.ip->ip_data.inum &
494 HAMMER2_DIRHASH_USERMSK,
495 dtype, chain->u.ip->ip_data.name_len,
496 chain->u.ip->ip_data.filename);
497 if (r)
498 break;
499 if (cookies)
500 cookies[cookie_index] = saveoff;
501 ++cookie_index;
502 } else {
503 /* XXX chain error */
504 kprintf("bad chain type readdir %d\n",
505 chain->bref.type);
506 }
995e78dc
MD
507
508 /*
509 * Keys may not be returned in order so once we have a
510 * placemarker (chain) the scan must allow the full range
511 * or some entries will be missed.
512 */
e028fa74 513 chain = hammer2_chain_next(hmp, &parent, chain,
99535653 514 HAMMER2_DIRHASH_VISIBLE,
a0ed3c24
MD
515 (hammer2_key_t)-1,
516 HAMMER2_LOOKUP_SHARED);
028a55bb
MD
517 if (chain) {
518 saveoff = (chain->bref.key &
519 HAMMER2_DIRHASH_USERMSK) + 1;
520 } else {
521 saveoff = (hammer2_key_t)-1;
522 }
523 if (cookie_index == ncookies)
524 break;
e028fa74 525 }
028a55bb 526 if (chain)
01eabad4
MD
527 hammer2_chain_unlock(hmp, chain);
528 hammer2_chain_unlock(hmp, parent);
e028fa74
MD
529done:
530 if (ap->a_eofflag)
531 *ap->a_eofflag = (chain == NULL);
37aa19df 532 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
e028fa74
MD
533 if (error && cookie_index == 0) {
534 if (cookies) {
535 kfree(cookies, M_TEMP);
536 *ap->a_ncookies = 0;
537 *ap->a_cookies = NULL;
538 }
539 } else {
540 if (cookies) {
541 *ap->a_ncookies = cookie_index;
542 *ap->a_cookies = cookies;
543 }
544 }
545 return (error);
703720e4
MD
546}
547
4e2004ea
MD
548/*
549 * hammer2_vop_readlink { vp, uio, cred }
550 */
551static
552int
553hammer2_vop_readlink(struct vop_readlink_args *ap)
554{
555 struct vnode *vp;
556 hammer2_mount_t *hmp;
557 hammer2_inode_t *ip;
558 int error;
559
560 vp = ap->a_vp;
561 if (vp->v_type != VLNK)
562 return (EINVAL);
563 ip = VTOI(vp);
564 hmp = ip->hmp;
565
566 error = hammer2_read_file(ip, ap->a_uio, 0);
567 return (error);
568}
569
e118c14f 570static
703720e4 571int
e118c14f 572hammer2_vop_read(struct vop_read_args *ap)
703720e4 573{
db71f61f
MD
574 struct vnode *vp;
575 hammer2_mount_t *hmp;
576 hammer2_inode_t *ip;
db71f61f
MD
577 struct uio *uio;
578 int error;
579 int seqcount;
580 int bigread;
581
582 /*
583 * Read operations supported on this vnode?
584 */
585 vp = ap->a_vp;
586 if (vp->v_type != VREG)
587 return (EINVAL);
588
589 /*
590 * Misc
591 */
592 ip = VTOI(vp);
593 hmp = ip->hmp;
594 uio = ap->a_uio;
595 error = 0;
596
597 seqcount = ap->a_ioflag >> 16;
598 bigread = (uio->uio_resid > 100 * 1024 * 1024);
599
4e2004ea 600 error = hammer2_read_file(ip, uio, seqcount);
db71f61f 601 return (error);
47902fef 602}
703720e4 603
e118c14f 604static
47902fef 605int
e118c14f 606hammer2_vop_write(struct vop_write_args *ap)
47902fef 607{
db71f61f
MD
608 thread_t td;
609 struct vnode *vp;
610 hammer2_mount_t *hmp;
611 hammer2_inode_t *ip;
db71f61f
MD
612 struct uio *uio;
613 int error;
db71f61f
MD
614 int seqcount;
615 int bigwrite;
616
617 /*
618 * Read operations supported on this vnode?
619 */
620 vp = ap->a_vp;
621 if (vp->v_type != VREG)
622 return (EINVAL);
623
624 /*
625 * Misc
626 */
627 ip = VTOI(vp);
628 hmp = ip->hmp;
629 uio = ap->a_uio;
630 error = 0;
db71f61f
MD
631 if (hmp->ronly)
632 return (EROFS);
633
634 seqcount = ap->a_ioflag >> 16;
635 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
636
637 /*
638 * Check resource limit
639 */
640 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
641 uio->uio_offset + uio->uio_resid >
642 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
643 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
644 return (EFBIG);
645 }
646
647 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
648
649 /*
3ac6a319
MD
650 * ip must be locked if extending the file.
651 * ip must be locked to avoid racing a truncation.
8cce658d
MD
652 *
653 * ip must be marked modified, particularly because the write
654 * might wind up being copied into the embedded data area.
3ac6a319
MD
655 */
656 hammer2_inode_lock_ex(ip);
1c9f601e 657 error = hammer2_write_file(ip, uio, ap->a_ioflag, seqcount);
4e2004ea
MD
658 hammer2_inode_unlock_ex(ip);
659 return (error);
660}
661
662/*
663 * Perform read operations on a file or symlink given an UNLOCKED
664 * inode and uio.
665 */
666static
667int
668hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
669{
670 struct buf *bp;
671 int error;
672
673 error = 0;
674
675 /*
676 * UIO read loop
677 */
678 while (uio->uio_resid > 0 && uio->uio_offset < ip->ip_data.size) {
8cce658d
MD
679 hammer2_key_t lbase;
680 hammer2_key_t leof;
681 int lblksize;
682 int loff;
4e2004ea
MD
683 int n;
684
8cce658d
MD
685 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
686 &lbase, &leof);
4e2004ea 687
8cce658d 688 error = cluster_read(ip->vp, leof, lbase, lblksize,
6ba3b984
MD
689 uio->uio_resid, seqcount * BKVASIZE,
690 &bp);
8cce658d 691
4e2004ea
MD
692 if (error)
693 break;
8cce658d
MD
694 loff = (int)(uio->uio_offset - lbase);
695 n = lblksize - loff;
4e2004ea
MD
696 if (n > uio->uio_resid)
697 n = uio->uio_resid;
698 if (n > ip->ip_data.size - uio->uio_offset)
699 n = (int)(ip->ip_data.size - uio->uio_offset);
700 bp->b_flags |= B_AGE;
8cce658d 701 uiomove((char *)bp->b_data + loff, n, uio);
4e2004ea
MD
702 bqrelse(bp);
703 }
704 return (error);
705}
706
707/*
708 * Called with a locked (ip) to do the underlying write to a file or
709 * to build the symlink target.
710 */
711static
712int
1c9f601e
MD
713hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
714 int ioflag, int seqcount)
4e2004ea 715{
8cce658d 716 hammer2_key_t old_eof;
4e2004ea
MD
717 struct buf *bp;
718 int kflags;
719 int error;
b2b78aaa 720 int modified = 0;
3ac6a319 721
4e2004ea
MD
722 /*
723 * Setup if append
724 */
725 if (ioflag & IO_APPEND)
3ac6a319 726 uio->uio_offset = ip->ip_data.size;
4e2004ea
MD
727 kflags = 0;
728 error = 0;
3ac6a319 729
8cce658d
MD
730 /*
731 * Extend the file if necessary. If the write fails at some point
732 * we will truncate it back down to cover as much as we were able
733 * to write.
734 *
735 * Doing this now makes it easier to calculate buffer sizes in
736 * the loop.
737 */
738 old_eof = ip->ip_data.size;
739 if (uio->uio_offset + uio->uio_resid > ip->ip_data.size) {
b2b78aaa 740 modified = 1;
8cce658d
MD
741 hammer2_extend_file(ip, uio->uio_offset + uio->uio_resid);
742 kflags |= NOTE_EXTEND;
743 }
744
3ac6a319
MD
745 /*
746 * UIO write loop
db71f61f
MD
747 */
748 while (uio->uio_resid > 0) {
8cce658d
MD
749 hammer2_key_t lbase;
750 hammer2_key_t leof;
db71f61f 751 int trivial;
8cce658d
MD
752 int lblksize;
753 int loff;
754 int n;
db71f61f
MD
755
756 /*
757 * Don't allow the buffer build to blow out the buffer
758 * cache.
759 */
a92f82c4
MD
760 if ((ioflag & IO_RECURSE) == 0) {
761 /*
762 * XXX should try to leave this unlocked through
763 * the whole loop
764 */
765 hammer2_chain_unlock(ip->hmp, &ip->chain);
01eabad4
MD
766 bwillwrite(HAMMER2_PBUFSIZE);
767 hammer2_chain_lock(ip->hmp, &ip->chain,
768 HAMMER2_RESOLVE_ALWAYS);
a92f82c4 769 }
db71f61f 770
8cce658d
MD
771 /* XXX bigwrite & signal check test */
772
db71f61f 773 /*
8cce658d
MD
774 * This nominally tells us how much we can cluster and
775 * what the logical buffer size needs to be. Currently
776 * we don't try to cluster the write and just handle one
777 * block at a time.
db71f61f 778 */
8cce658d
MD
779 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
780 &lbase, &leof);
781 loff = (int)(uio->uio_offset - lbase);
782
783 /*
784 * Calculate bytes to copy this transfer and whether the
785 * copy completely covers the buffer or not.
786 */
787 trivial = 0;
788 n = lblksize - loff;
789 if (n > uio->uio_resid) {
790 n = uio->uio_resid;
791 if (uio->uio_offset + n == ip->ip_data.size)
db71f61f 792 trivial = 1;
8cce658d
MD
793 } else if (loff == 0) {
794 trivial = 1;
db71f61f
MD
795 }
796
8cce658d
MD
797 /*
798 * Get the buffer
799 */
db71f61f
MD
800 if (uio->uio_segflg == UIO_NOCOPY) {
801 /*
802 * Issuing a write with the same data backing the
803 * buffer. Instantiate the buffer to collect the
804 * backing vm pages, then read-in any missing bits.
805 *
806 * This case is used by vop_stdputpages().
807 */
8cce658d 808 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
db71f61f
MD
809 if ((bp->b_flags & B_CACHE) == 0) {
810 bqrelse(bp);
8cce658d 811 error = bread(ip->vp, lbase, lblksize, &bp);
db71f61f 812 }
8cce658d 813 } else if (trivial) {
db71f61f
MD
814 /*
815 * Even though we are entirely overwriting the buffer
816 * we may still have to zero it out to avoid a
817 * mmap/write visibility issue.
818 */
8cce658d 819 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
db71f61f
MD
820 if ((bp->b_flags & B_CACHE) == 0)
821 vfs_bio_clrbuf(bp);
db71f61f
MD
822 } else {
823 /*
824 * Partial overwrite, read in any missing bits then
825 * replace the portion being written.
8cce658d
MD
826 *
827 * (The strategy code will detect zero-fill physical
828 * blocks for this case).
db71f61f 829 */
8cce658d 830 error = bread(ip->vp, lbase, lblksize, &bp);
db71f61f
MD
831 if (error == 0)
832 bheavy(bp);
833 }
834
8cce658d
MD
835 if (error) {
836 brelse(bp);
837 break;
db71f61f
MD
838 }
839
8cce658d
MD
840 /*
841 * We have to assign physical storage to the buffer we intend
842 * to dirty or write now to avoid deadlocks in the strategy
843 * code later.
844 *
845 * This can return NOOFFSET for inode-embedded data. The
846 * strategy code will take care of it in that case.
847 */
848 bp->b_bio2.bio_offset =
849 hammer2_assign_physical(ip, lbase, lblksize, &error);
db71f61f
MD
850 if (error) {
851 brelse(bp);
db71f61f
MD
852 break;
853 }
8cce658d
MD
854
855 /*
856 * Ok, copy the data in
857 */
858 hammer2_chain_unlock(ip->hmp, &ip->chain);
859 error = uiomove(bp->b_data + loff, n, uio);
01eabad4 860 hammer2_chain_lock(ip->hmp, &ip->chain, HAMMER2_RESOLVE_ALWAYS);
db71f61f 861 kflags |= NOTE_WRITE;
b2b78aaa 862 modified = 1;
8cce658d
MD
863
864 if (error) {
865 brelse(bp);
866 break;
867 }
868
b2b78aaa 869 /* XXX update ip_data.mtime */
db71f61f
MD
870
871 /*
872 * Once we dirty a buffer any cached offset becomes invalid.
1c9f601e
MD
873 *
874 * NOTE: For cluster_write() always use the trailing block
875 * size, which is HAMMER2_PBUFSIZE. lblksize is the
876 * eof-straddling blocksize and is incorrect.
db71f61f 877 */
db71f61f 878 bp->b_flags |= B_AGE;
4e2004ea 879 if (ioflag & IO_SYNC) {
db71f61f 880 bwrite(bp);
8cce658d 881 } else if ((ioflag & IO_DIRECT) && loff + n == lblksize) {
004f88b4
MD
882 if (bp->b_bcount == HAMMER2_PBUFSIZE)
883 bp->b_flags |= B_CLUSTEROK;
8cce658d 884 bdwrite(bp);
4e2004ea 885 } else if (ioflag & IO_ASYNC) {
db71f61f 886 bawrite(bp);
1c9f601e 887 } else if (hammer2_cluster_enable) {
004f88b4
MD
888 if (bp->b_bcount == HAMMER2_PBUFSIZE)
889 bp->b_flags |= B_CLUSTEROK;
1c9f601e 890 cluster_write(bp, leof, HAMMER2_PBUFSIZE, seqcount);
db71f61f 891 } else {
004f88b4
MD
892 if (bp->b_bcount == HAMMER2_PBUFSIZE)
893 bp->b_flags |= B_CLUSTEROK;
db71f61f
MD
894 bdwrite(bp);
895 }
896 }
8cce658d
MD
897
898 /*
899 * Cleanup. If we extended the file EOF but failed to write through
900 * the entire write is a failure and we have to back-up.
901 */
b2b78aaa 902 if (error && ip->ip_data.size != old_eof) {
8cce658d 903 hammer2_truncate_file(ip, old_eof);
b2b78aaa
MD
904 } else if (modified) {
905 hammer2_chain_modify(ip->hmp, &ip->chain, 0);
906 hammer2_update_time(&ip->ip_data.mtime);
907 }
908 hammer2_knote(ip->vp, kflags);
4e2004ea 909 return error;
703720e4
MD
910}
911
3ac6a319 912/*
8cce658d
MD
913 * Assign physical storage to a logical block.
914 *
915 * NOOFFSET is returned if the data is inode-embedded. In this case the
916 * strategy code will simply bcopy() the data into the inode.
28ee5f14
MD
917 *
918 * The inode's delta_dcount is adjusted.
8cce658d
MD
919 */
920static
921hammer2_off_t
922hammer2_assign_physical(hammer2_inode_t *ip, hammer2_key_t lbase,
923 int lblksize, int *errorp)
924{
925 hammer2_mount_t *hmp;
926 hammer2_chain_t *parent;
927 hammer2_chain_t *chain;
928 hammer2_off_t pbase;
929
8cce658d
MD
930 /*
931 * Locate the chain associated with lbase, return a locked chain.
932 * However, do not instantiate any data reference (which utilizes a
933 * device buffer) because we will be using direct IO via the
934 * logical buffer cache buffer.
935 */
5f6853df
MD
936 hmp = ip->hmp;
937retry:
938 *errorp = 0;
8cce658d 939 parent = &ip->chain;
01eabad4 940 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
8cce658d
MD
941
942 chain = hammer2_chain_lookup(hmp, &parent,
943 lbase, lbase,
944 HAMMER2_LOOKUP_NODATA);
945
946 if (chain == NULL) {
947 /*
01eabad4
MD
948 * We found a hole, create a new chain entry.
949 *
950 * NOTE: DATA chains are created without device backing
951 * store (nor do we want any).
8cce658d
MD
952 */
953 chain = hammer2_chain_create(hmp, parent, NULL,
954 lbase, HAMMER2_PBUFRADIX,
955 HAMMER2_BREF_TYPE_DATA,
5f6853df
MD
956 lblksize, errorp);
957 if (chain == NULL) {
958 KKASSERT(*errorp == EAGAIN); /* XXX */
959 hammer2_chain_unlock(hmp, parent);
960 goto retry;
961 }
962
8cce658d 963 pbase = chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX;
28ee5f14 964 ip->delta_dcount += lblksize;
8cce658d
MD
965 } else {
966 switch (chain->bref.type) {
967 case HAMMER2_BREF_TYPE_INODE:
968 /*
214f4a77
MD
969 * The data is embedded in the inode. The
970 * caller is responsible for marking the inode
971 * modified and copying the data to the embedded
972 * area.
8cce658d 973 */
8cce658d
MD
974 pbase = NOOFFSET;
975 break;
976 case HAMMER2_BREF_TYPE_DATA:
977 if (chain->bytes != lblksize) {
978 panic("hammer2_assign_physical: "
979 "size mismatch %d/%d\n",
980 lblksize, chain->bytes);
981 }
01eabad4
MD
982 hammer2_chain_modify(hmp, chain,
983 HAMMER2_MODIFY_OPTDATA);
8cce658d
MD
984 pbase = chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX;
985 break;
986 default:
987 panic("hammer2_assign_physical: bad type");
988 /* NOT REACHED */
989 pbase = NOOFFSET;
990 break;
991 }
992 }
993
994 if (chain)
01eabad4
MD
995 hammer2_chain_unlock(hmp, chain);
996 hammer2_chain_unlock(hmp, parent);
8cce658d
MD
997
998 return (pbase);
999}
1000
1001/*
1002 * Truncate the size of a file.
1003 *
1004 * This routine adjusts ip->ip_data.size smaller, destroying any related
1005 * data beyond the new EOF and potentially resizing the block straddling
1006 * the EOF.
1007 *
1008 * The inode must be locked.
3ac6a319
MD
1009 */
1010static
1011void
1012hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
1013{
1014 hammer2_chain_t *parent;
1015 hammer2_chain_t *chain;
1016 hammer2_mount_t *hmp = ip->hmp;
8cce658d
MD
1017 hammer2_key_t lbase;
1018 hammer2_key_t leof;
1019 struct buf *bp;
1020 int loff;
3ac6a319 1021 int error;
8cce658d
MD
1022 int oblksize;
1023 int nblksize;
1024
01eabad4 1025 hammer2_chain_modify(hmp, &ip->chain, 0);
8cce658d 1026 bp = NULL;
3ac6a319
MD
1027
1028 /*
8cce658d
MD
1029 * Destroy any logical buffer cache buffers beyond the file EOF.
1030 *
1031 * We call nvtruncbuf() w/ trivial == 1 to prevent it from messing
1032 * around with the buffer straddling EOF, because we need to assign
1033 * a new physical offset to it.
3ac6a319
MD
1034 */
1035 if (ip->vp) {
1036 nvtruncbuf(ip->vp, nsize,
8cce658d
MD
1037 HAMMER2_PBUFSIZE, (int)nsize & HAMMER2_PBUFMASK,
1038 1);
3ac6a319 1039 }
3ac6a319
MD
1040
1041 /*
8cce658d 1042 * Setup for lookup/search
3ac6a319
MD
1043 */
1044 parent = &ip->chain;
01eabad4 1045 error = hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
3ac6a319 1046 if (error) {
01eabad4 1047 hammer2_chain_unlock(hmp, parent);
3ac6a319
MD
1048 /* XXX error reporting */
1049 return;
1050 }
1051
1052 /*
8cce658d
MD
1053 * Handle the case where a chain/logical-buffer straddles the new
1054 * EOF. We told nvtruncbuf() above not to mess with the logical
1055 * buffer straddling the EOF because we need to reassign its storage
1056 * and can't let the strategy code do it for us.
3ac6a319 1057 */
8cce658d
MD
1058 loff = (int)nsize & HAMMER2_PBUFMASK;
1059 if (loff && ip->vp) {
1060 oblksize = hammer2_calc_logical(ip, nsize, &lbase, &leof);
1061 error = bread(ip->vp, lbase, oblksize, &bp);
1062 KKASSERT(error == 0);
1063 }
1064 ip->ip_data.size = nsize;
1065 nblksize = hammer2_calc_logical(ip, nsize, &lbase, &leof);
3ac6a319 1066
866d5273 1067 /*
8cce658d
MD
1068 * Fixup the chain element. If we have a logical buffer in-hand
1069 * we don't want to create a conflicting device buffer.
866d5273 1070 */
8cce658d
MD
1071 if (loff && bp) {
1072 chain = hammer2_chain_lookup(hmp, &parent, lbase, lbase,
01eabad4 1073 HAMMER2_LOOKUP_NODATA);
8cce658d
MD
1074 if (chain) {
1075 allocbuf(bp, nblksize);
1076 switch(chain->bref.type) {
1077 case HAMMER2_BREF_TYPE_DATA:
28ee5f14 1078 hammer2_chain_resize(ip, chain,
01eabad4
MD
1079 hammer2_bytes_to_radix(nblksize),
1080 HAMMER2_MODIFY_OPTDATA);
8cce658d
MD
1081 bzero(bp->b_data + loff, nblksize - loff);
1082 bp->b_bio2.bio_offset = chain->bref.data_off &
1083 HAMMER2_OFF_MASK;
1084 break;
1085 case HAMMER2_BREF_TYPE_INODE:
1086 bzero(bp->b_data + loff, nblksize - loff);
1087 bp->b_bio2.bio_offset = NOOFFSET;
1088 break;
1089 default:
1090 panic("hammer2_truncate_file: bad type");
1091 break;
1092 }
01eabad4 1093 hammer2_chain_unlock(hmp, chain);
004f88b4
MD
1094 if (bp->b_bcount == HAMMER2_PBUFSIZE)
1095 bp->b_flags |= B_CLUSTEROK;
8cce658d
MD
1096 bdwrite(bp);
1097 } else {
1098 /*
1099 * Destroy clean buffer w/ wrong buffer size. Retain
1100 * backing store.
1101 */
1102 bp->b_flags |= B_RELBUF;
1103 KKASSERT(bp->b_bio2.bio_offset == NOOFFSET);
1104 KKASSERT((bp->b_flags & B_DIRTY) == 0);
1105 bqrelse(bp);
1106 }
1107 } else if (loff) {
01eabad4
MD
1108 /*
1109 * WARNING: This utilizes a device buffer for the data.
1110 *
004f88b4
MD
1111 * This case should not occur because file truncations without
1112 * a vnode (and hence no logical buffer cache) should only
1113 * always truncate to 0-length.
01eabad4
MD
1114 */
1115 panic("hammer2_truncate_file: non-zero truncation, no-vnode");
004f88b4 1116#if 0
8cce658d
MD
1117 chain = hammer2_chain_lookup(hmp, &parent, lbase, lbase, 0);
1118 if (chain) {
1119 switch(chain->bref.type) {
1120 case HAMMER2_BREF_TYPE_DATA:
28ee5f14 1121 hammer2_chain_resize(ip, chain,
01eabad4
MD
1122 hammer2_bytes_to_radix(nblksize),
1123 0);
1124 hammer2_chain_modify(hmp, chain, 0);
8cce658d
MD
1125 bzero(chain->data->buf + loff, nblksize - loff);
1126 break;
1127 case HAMMER2_BREF_TYPE_INODE:
1128 if (loff < HAMMER2_EMBEDDED_BYTES) {
01eabad4 1129 hammer2_chain_modify(hmp, chain, 0);
8cce658d
MD
1130 bzero(chain->data->ipdata.u.data + loff,
1131 HAMMER2_EMBEDDED_BYTES - loff);
1132 }
1133 break;
1134 }
01eabad4 1135 hammer2_chain_unlock(hmp, chain);
3ac6a319 1136 }
004f88b4 1137#endif
3ac6a319
MD
1138 }
1139
866d5273 1140 /*
8cce658d
MD
1141 * Clean up any fragmentory VM pages now that we have properly
1142 * resized the straddling buffer. These pages are no longer
1143 * part of the buffer.
866d5273 1144 */
8cce658d
MD
1145 if (ip->vp) {
1146 nvtruncbuf(ip->vp, nsize,
1147 nblksize, (int)nsize & (nblksize - 1),
1148 1);
1149 }
866d5273 1150
8cce658d
MD
1151 /*
1152 * Destroy any physical blocks after the new EOF point.
1153 */
1154 lbase = (nsize + HAMMER2_PBUFMASK64) & ~HAMMER2_PBUFMASK64;
3ac6a319 1155 chain = hammer2_chain_lookup(hmp, &parent,
8cce658d 1156 lbase, (hammer2_key_t)-1,
5f6853df 1157 HAMMER2_LOOKUP_NODATA);
3ac6a319
MD
1158 while (chain) {
1159 /*
1160 * Degenerate embedded data case, nothing to loop on.
1161 */
8e12e3c9
MD
1162 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
1163 hammer2_chain_unlock(hmp, chain);
3ac6a319 1164 break;
8e12e3c9 1165 }
3ac6a319
MD
1166
1167 /*
1168 * Delete physical data blocks past the file EOF.
1169 */
1170 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
28ee5f14 1171 ip->delta_dcount -= chain->bytes;
004f88b4 1172 hammer2_chain_delete(hmp, parent, chain, 0);
3ac6a319 1173 }
db0c2eb3 1174 /* XXX check parent if empty indirect block & delete */
3ac6a319 1175 chain = hammer2_chain_next(hmp, &parent, chain,
8cce658d 1176 lbase, (hammer2_key_t)-1,
5f6853df 1177 HAMMER2_LOOKUP_NODATA);
3ac6a319 1178 }
01eabad4 1179 hammer2_chain_unlock(hmp, parent);
3ac6a319
MD
1180}
1181
1182/*
8cce658d 1183 * Extend the size of a file. The inode must be locked.
866d5273 1184 *
8cce658d 1185 * We may have to resize the block straddling the old EOF.
3ac6a319
MD
1186 */
1187static
1188void
8cce658d 1189hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
3ac6a319 1190{
8cce658d
MD
1191 hammer2_mount_t *hmp;
1192 hammer2_chain_t *parent;
1193 hammer2_chain_t *chain;
3ac6a319 1194 struct buf *bp;
8cce658d
MD
1195 hammer2_key_t osize;
1196 hammer2_key_t obase;
1197 hammer2_key_t nbase;
1198 hammer2_key_t leof;
1199 int oblksize;
1200 int nblksize;
1201 int nradix;
3ac6a319
MD
1202 int error;
1203
8cce658d
MD
1204 KKASSERT(ip->vp);
1205 hmp = ip->hmp;
1206
01eabad4 1207 hammer2_chain_modify(hmp, &ip->chain, 0);
8cce658d 1208
3ac6a319 1209 /*
8cce658d 1210 * Nothing to do if the direct-data case is still intact
3ac6a319
MD
1211 */
1212 if ((ip->ip_data.op_flags & HAMMER2_OPFLAG_DIRECTDATA) &&
8cce658d
MD
1213 nsize <= HAMMER2_EMBEDDED_BYTES) {
1214 ip->ip_data.size = nsize;
004f88b4
MD
1215 nvextendbuf(ip->vp,
1216 ip->ip_data.size, nsize,
1217 0, HAMMER2_EMBEDDED_BYTES,
1218 0, (int)nsize,
1219 1);
8cce658d
MD
1220 return;
1221 }
1222
1223 /*
1224 * Calculate the blocksize at the original EOF and resize the block
1225 * if necessary. Adjust the file size in the inode.
1226 */
1227 osize = ip->ip_data.size;
1228 oblksize = hammer2_calc_logical(ip, osize, &obase, &leof);
1229 ip->ip_data.size = nsize;
1230 nblksize = hammer2_calc_logical(ip, osize, &nbase, &leof);
1231
1232 /*
1233 * Do all required vnode operations, but do not mess with the
1234 * buffer straddling the orignal EOF.
1235 */
1236 nvextendbuf(ip->vp,
1237 ip->ip_data.size, nsize,
1238 0, nblksize,
1239 0, (int)nsize & HAMMER2_PBUFMASK,
1240 1);
1241
1242 /*
1243 * Early return if we have no more work to do.
1244 */
1245 if (obase == nbase && oblksize == nblksize &&
1246 (ip->ip_data.op_flags & HAMMER2_OPFLAG_DIRECTDATA) == 0) {
1247 return;
1248 }
1249
1250 /*
1251 * We have work to do, including possibly resizing the buffer
004f88b4 1252 * at the previous EOF point and turning off DIRECTDATA mode.
8cce658d
MD
1253 */
1254 bp = NULL;
1255 if (((int)osize & HAMMER2_PBUFMASK)) {
1256 error = bread(ip->vp, obase, oblksize, &bp);
3ac6a319 1257 KKASSERT(error == 0);
8cce658d
MD
1258
1259 if (obase != nbase) {
004f88b4
MD
1260 if (oblksize != HAMMER2_PBUFSIZE)
1261 allocbuf(bp, HAMMER2_PBUFSIZE);
8cce658d 1262 } else {
004f88b4
MD
1263 if (oblksize != nblksize)
1264 allocbuf(bp, nblksize);
8cce658d 1265 }
8cce658d
MD
1266 }
1267
1268 /*
1269 * Disable direct-data mode by loading up a buffer cache buffer
1270 * with the data, then converting the inode data area into the
1271 * inode indirect block array area.
1272 */
1273 if (ip->ip_data.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
3ac6a319 1274 ip->ip_data.op_flags &= ~HAMMER2_OPFLAG_DIRECTDATA;
8cce658d 1275 bzero(&ip->ip_data.u.blockset, sizeof(ip->ip_data.u.blockset));
3ac6a319 1276 }
866d5273
MD
1277
1278 /*
8cce658d 1279 * Resize the chain element at the old EOF.
866d5273 1280 */
8cce658d 1281 if (((int)osize & HAMMER2_PBUFMASK)) {
5f6853df 1282retry:
8cce658d 1283 parent = &ip->chain;
01eabad4 1284 error = hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
8cce658d
MD
1285 KKASSERT(error == 0);
1286
1287 nradix = hammer2_bytes_to_radix(nblksize);
1288
1289 chain = hammer2_chain_lookup(hmp, &parent,
1290 obase, obase,
01eabad4 1291 HAMMER2_LOOKUP_NODATA);
8cce658d
MD
1292 if (chain == NULL) {
1293 chain = hammer2_chain_create(hmp, parent, NULL,
1294 obase, nblksize,
1295 HAMMER2_BREF_TYPE_DATA,
5f6853df
MD
1296 nblksize, &error);
1297 if (chain == NULL) {
1298 KKASSERT(error == EAGAIN);
1299 hammer2_chain_unlock(hmp, parent);
1300 goto retry;
1301 }
28ee5f14 1302 ip->delta_dcount += nblksize;
8cce658d
MD
1303 } else {
1304 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA);
28ee5f14 1305 hammer2_chain_resize(ip, chain, nradix,
01eabad4 1306 HAMMER2_MODIFY_OPTDATA);
8cce658d
MD
1307 }
1308 bp->b_bio2.bio_offset = chain->bref.data_off &
1309 HAMMER2_OFF_MASK;
01eabad4 1310 hammer2_chain_unlock(hmp, chain);
004f88b4
MD
1311 if (bp->b_bcount == HAMMER2_PBUFSIZE)
1312 bp->b_flags |= B_CLUSTEROK;
8cce658d 1313 bdwrite(bp);
01eabad4 1314 hammer2_chain_unlock(hmp, parent);
3ac6a319
MD
1315 }
1316}
1317
e118c14f 1318static
703720e4 1319int
e118c14f 1320hammer2_vop_nresolve(struct vop_nresolve_args *ap)
703720e4 1321{
37494cab 1322 hammer2_inode_t *dip;
e708f8b9 1323 hammer2_inode_t *ip;
37494cab
MD
1324 hammer2_mount_t *hmp;
1325 hammer2_chain_t *parent;
1326 hammer2_chain_t *chain;
1327 struct namecache *ncp;
1328 const uint8_t *name;
1329 size_t name_len;
1330 hammer2_key_t lhc;
1331 int error = 0;
1332 struct vnode *vp;
1333
1334 dip = VTOI(ap->a_dvp);
1335 hmp = dip->hmp;
1336 ncp = ap->a_nch->ncp;
1337 name = ncp->nc_name;
1338 name_len = ncp->nc_nlen;
1339 lhc = hammer2_dirhash(name, name_len);
1340
1341 /*
1342 * Note: In DragonFly the kernel handles '.' and '..'.
1343 */
1344 parent = &dip->chain;
a0ed3c24
MD
1345 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS |
1346 HAMMER2_RESOLVE_SHARED);
37494cab 1347 chain = hammer2_chain_lookup(hmp, &parent,
c667909f 1348 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
a0ed3c24 1349 HAMMER2_LOOKUP_SHARED);
37494cab
MD
1350 while (chain) {
1351 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1352 chain->u.ip &&
1353 name_len == chain->data->ipdata.name_len &&
1354 bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
1355 break;
1356 }
1357 chain = hammer2_chain_next(hmp, &parent, chain,
c667909f 1358 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
a0ed3c24 1359 HAMMER2_LOOKUP_SHARED);
37494cab 1360 }
01eabad4 1361 hammer2_chain_unlock(hmp, parent);
37494cab 1362
e708f8b9
MD
1363 /*
1364 * If the inode represents a forwarding entry for a hardlink we have
1365 * to locate the actual inode. The original ip is saved for possible
1366 * deconsolidation. (ip) will only be set to non-NULL when we have
1367 * to locate the real file via a hardlink. ip will be referenced but
1368 * not locked in that situation. chain is passed in locked and
1369 * returned locked.
a0ed3c24
MD
1370 *
1371 * XXX what kind of chain lock?
e708f8b9
MD
1372 */
1373 ip = NULL;
1374 if (chain && chain->u.ip->ip_data.type == HAMMER2_OBJTYPE_HARDLINK) {
e708f8b9
MD
1375 error = hammer2_hardlink_find(dip, &chain, &ip);
1376 if (error) {
004f88b4 1377 kprintf("hammer2: unable to find hardlink\n");
e708f8b9
MD
1378 if (chain) {
1379 hammer2_chain_unlock(hmp, chain);
1380 chain = NULL;
1381 }
1382 goto failed;
1383 }
1384 }
1385
1386 /*
1387 * Deconsolidate any hardlink whos nlinks == 1. Ignore errors.
1388 * If an error occurs chain and ip are left alone.
a0ed3c24
MD
1389 *
1390 * XXX upgrade shared lock?
e708f8b9
MD
1391 */
1392 if (ip && chain && chain->u.ip->ip_data.nlinks == 1 && !hmp->ronly) {
1393 kprintf("hammer2: need to unconsolidate hardlink for %s\n",
1394 chain->u.ip->ip_data.filename);
1395 hammer2_hardlink_deconsolidate(dip, &chain, &ip);
1396 }
1397
1398 /*
1399 * Acquire the related vnode
1400 */
37494cab
MD
1401 if (chain) {
1402 vp = hammer2_igetv(chain->u.ip, &error);
1403 if (error == 0) {
1404 vn_unlock(vp);
1405 cache_setvp(ap->a_nch, vp);
1406 vrele(vp);
f3843dc2
MD
1407 } else {
1408 cache_setvp(ap->a_nch, NULL);
37494cab 1409 }
01eabad4 1410 hammer2_chain_unlock(hmp, chain);
37494cab
MD
1411 } else {
1412 error = ENOENT;
e708f8b9 1413failed:
37494cab
MD
1414 cache_setvp(ap->a_nch, NULL);
1415 }
e708f8b9
MD
1416 if (ip)
1417 hammer2_inode_drop(ip);
37494cab
MD
1418 return error;
1419}
1420
1421static
1422int
1423hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1424{
1425 hammer2_inode_t *dip;
1426 hammer2_inode_t *ip;
1427 hammer2_mount_t *hmp;
1428 int error;
1429
1430 dip = VTOI(ap->a_dvp);
1431 hmp = dip->hmp;
1432
1433 if ((ip = dip->pip) == NULL) {
1434 *ap->a_vpp = NULL;
1435 return ENOENT;
1436 }
01eabad4 1437 hammer2_chain_lock(hmp, &ip->chain, HAMMER2_RESOLVE_ALWAYS);
37494cab 1438 *ap->a_vpp = hammer2_igetv(ip, &error);
01eabad4 1439 hammer2_chain_unlock(hmp, &ip->chain);
37494cab
MD
1440
1441 return error;
1442}
1443
1444static
1445int
1446hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1447{
1448 hammer2_mount_t *hmp;
1449 hammer2_inode_t *dip;
1450 hammer2_inode_t *nip;
1451 struct namecache *ncp;
1452 const uint8_t *name;
1453 size_t name_len;
1454 int error;
1455
1456 dip = VTOI(ap->a_dvp);
1457 hmp = dip->hmp;
db71f61f
MD
1458 if (hmp->ronly)
1459 return (EROFS);
1460
37494cab
MD
1461 ncp = ap->a_nch->ncp;
1462 name = ncp->nc_name;
1463 name_len = ncp->nc_nlen;
1464
e4e20f48
MD
1465 error = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1466 name, name_len, &nip);
37494cab
MD
1467 if (error) {
1468 KKASSERT(nip == NULL);
1469 *ap->a_vpp = NULL;
1470 return error;
1471 }
1472 *ap->a_vpp = hammer2_igetv(nip, &error);
01eabad4 1473 hammer2_chain_unlock(hmp, &nip->chain);
37494cab
MD
1474
1475 if (error == 0) {
1476 cache_setunresolved(ap->a_nch);
1477 cache_setvp(ap->a_nch, *ap->a_vpp);
1478 }
1479 return error;
703720e4
MD
1480}
1481
db71f61f
MD
1482/*
1483 * Return the largest contiguous physical disk range for the logical
1484 * request.
1485 *
1486 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
1487 */
e118c14f 1488static
703720e4 1489int
e118c14f 1490hammer2_vop_bmap(struct vop_bmap_args *ap)
703720e4 1491{
db71f61f
MD
1492 struct vnode *vp;
1493 hammer2_mount_t *hmp;
1494 hammer2_inode_t *ip;
1495 hammer2_chain_t *parent;
1496 hammer2_chain_t *chain;
6ba3b984
MD
1497 hammer2_key_t lbeg;
1498 hammer2_key_t lend;
1499 hammer2_off_t pbeg;
1500 hammer2_off_t pbytes;
1501 hammer2_off_t array[HAMMER2_BMAP_COUNT][2];
1502 int loff;
1503 int ai;
db71f61f
MD
1504
1505 /*
1506 * Only supported on regular files
1507 *
1508 * Only supported for read operations (required for cluster_read).
1509 * The block allocation is delayed for write operations.
1510 */
1511 vp = ap->a_vp;
1512 if (vp->v_type != VREG)
1513 return (EOPNOTSUPP);
1514 if (ap->a_cmd != BUF_CMD_READ)
1515 return (EOPNOTSUPP);
1516
1517 ip = VTOI(vp);
1518 hmp = ip->hmp;
6ba3b984 1519 bzero(array, sizeof(array));
5b4a2132 1520
6ba3b984
MD
1521 /*
1522 * Calculate logical range
1523 */
1524 KKASSERT((ap->a_loffset & HAMMER2_LBUFMASK64) == 0);
1525 lbeg = ap->a_loffset & HAMMER2_OFF_MASK_HI;
1526 lend = lbeg + HAMMER2_BMAP_COUNT * HAMMER2_PBUFSIZE - 1;
1527 if (lend < lbeg)
1528 lend = lbeg;
1529 loff = ap->a_loffset & HAMMER2_OFF_MASK_LO;
db71f61f
MD
1530
1531 parent = &ip->chain;
a0ed3c24
MD
1532 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS |
1533 HAMMER2_RESOLVE_SHARED);
6ba3b984
MD
1534 chain = hammer2_chain_lookup(hmp, &parent,
1535 lbeg, lend,
a0ed3c24
MD
1536 HAMMER2_LOOKUP_NODATA |
1537 HAMMER2_LOOKUP_SHARED);
3ac6a319 1538 if (chain == NULL) {
3ac6a319 1539 *ap->a_doffsetp = ZFOFFSET;
01eabad4 1540 hammer2_chain_unlock(hmp, parent);
6ba3b984
MD
1541 return (0);
1542 }
1543
1544 while (chain) {
1545 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
1546 ai = (chain->bref.key - lbeg) / HAMMER2_PBUFSIZE;
1547 KKASSERT(ai >= 0 && ai < HAMMER2_BMAP_COUNT);
1548 array[ai][0] = chain->bref.data_off & HAMMER2_OFF_MASK;
1549 array[ai][1] = chain->bytes;
1550 }
1551 chain = hammer2_chain_next(hmp, &parent, chain,
1552 lbeg, lend,
a0ed3c24
MD
1553 HAMMER2_LOOKUP_NODATA |
1554 HAMMER2_LOOKUP_SHARED);
db71f61f 1555 }
01eabad4 1556 hammer2_chain_unlock(hmp, parent);
6ba3b984
MD
1557
1558 /*
1559 * If the requested loffset is not mappable physically we can't
1560 * bmap. The caller will have to access the file data via a
1561 * device buffer.
1562 */
1563 if (array[0][0] == 0 || array[0][1] < loff + HAMMER2_LBUFSIZE) {
1564 *ap->a_doffsetp = NOOFFSET;
1565 return (0);
1566 }
1567
1568 /*
1569 * Calculate the physical disk offset range for array[0]
1570 */
1571 pbeg = array[0][0] + loff;
1572 pbytes = array[0][1] - loff;
1573
1574 for (ai = 1; ai < HAMMER2_BMAP_COUNT; ++ai) {
1575 if (array[ai][0] != pbeg + pbytes)
1576 break;
1577 pbytes += array[ai][1];
1578 }
1579
1580 *ap->a_doffsetp = pbeg;
1581 if (ap->a_runp)
1582 *ap->a_runp = pbytes;
db71f61f 1583 return (0);
703720e4
MD
1584}
1585
e118c14f 1586static
703720e4 1587int
e118c14f 1588hammer2_vop_open(struct vop_open_args *ap)
703720e4 1589{
703720e4
MD
1590 return vop_stdopen(ap);
1591}
1592
37aa19df 1593/*
db0c2eb3 1594 * hammer2_vop_advlock { vp, id, op, fl, flags }
37aa19df
MD
1595 */
1596static
1597int
1598hammer2_vop_advlock(struct vop_advlock_args *ap)
1599{
1600 hammer2_inode_t *ip = VTOI(ap->a_vp);
1601
1602 return (lf_advlock(ap, &ip->advlock, ip->ip_data.size));
1603}
1604
1605
c667909f
MD
1606static
1607int
1608hammer2_vop_close(struct vop_close_args *ap)
1609{
1610 return vop_stdclose(ap);
1611}
1612
1613/*
db0c2eb3
MD
1614 * hammer2_vop_nlink { nch, dvp, vp, cred }
1615 *
e708f8b9 1616 * Create a hardlink from (vp) to {dvp, nch}.
db0c2eb3
MD
1617 */
1618static
1619int
1620hammer2_vop_nlink(struct vop_nlink_args *ap)
1621{
e708f8b9 1622 hammer2_inode_t *dip; /* target directory to create link in */
db0c2eb3 1623 hammer2_inode_t *ip; /* inode we are hardlinking to */
99535653 1624 hammer2_inode_t *oip;
db0c2eb3
MD
1625 hammer2_mount_t *hmp;
1626 struct namecache *ncp;
1627 const uint8_t *name;
1628 size_t name_len;
1629 int error;
1630
1631 dip = VTOI(ap->a_dvp);
1632 hmp = dip->hmp;
1633 if (hmp->ronly)
1634 return (EROFS);
1635
e708f8b9
MD
1636 /*
1637 * (ip) is the inode we are linking to.
1638 */
99535653 1639 ip = oip = VTOI(ap->a_vp);
e708f8b9 1640 hammer2_inode_lock_nlinks(ip);
db0c2eb3
MD
1641
1642 ncp = ap->a_nch->ncp;
1643 name = ncp->nc_name;
1644 name_len = ncp->nc_nlen;
1645
e708f8b9
MD
1646 /*
1647 * Create a consolidated real file for the hardlink, adjust (ip),
1648 * and move the nlinks lock if necessary. Tell the function to
1649 * bump the hardlink count on the consolidated file.
1650 */
99535653 1651 error = hammer2_hardlink_consolidate(&ip, dip);
e708f8b9
MD
1652 if (error)
1653 goto done;
1654
1655 /*
99535653
MD
1656 * If the consolidation changed ip to a HARDLINK pointer we have
1657 * to adjust the vnode to point to the actual ip.
28ee5f14
MD
1658 *
1659 * XXX this can race against concurrent vnode ops.
99535653
MD
1660 */
1661 if (oip != ip) {
1662 hammer2_chain_ref(hmp, &ip->chain);
1663 hammer2_inode_lock_ex(ip);
1664 hammer2_inode_lock_ex(oip);
1665 ip->vp = ap->a_vp;
1666 ap->a_vp->v_data = ip;
1667 oip->vp = NULL;
1668 hammer2_inode_unlock_ex(oip);
1669 hammer2_inode_unlock_ex(ip);
1670 hammer2_chain_drop(hmp, &oip->chain);
1671 }
1672
1673 /*
1674 * The act of connecting the existing (ip) will properly bump the
1675 * nlinks count. However, vp will incorrectly point at the old
1676 * inode which has now been turned into a OBJTYPE_HARDLINK pointer.
1677 *
1678 * We must reconnect the vp.
e708f8b9 1679 */
e708f8b9 1680 error = hammer2_inode_connect(dip, ip, name, name_len);
db0c2eb3
MD
1681 if (error == 0) {
1682 cache_setunresolved(ap->a_nch);
1683 cache_setvp(ap->a_nch, ap->a_vp);
1684 }
e708f8b9
MD
1685done:
1686 hammer2_inode_unlock_nlinks(ip);
db0c2eb3
MD
1687 return error;
1688}
1689
1690/*
1691 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
c667909f
MD
1692 *
1693 * The operating system has already ensured that the directory entry
1694 * does not exist and done all appropriate namespace locking.
1695 */
1696static
1697int
1698hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1699{
1700 hammer2_mount_t *hmp;
1701 hammer2_inode_t *dip;
1702 hammer2_inode_t *nip;
1703 struct namecache *ncp;
1704 const uint8_t *name;
1705 size_t name_len;
1706 int error;
1707
1708 dip = VTOI(ap->a_dvp);
1709 hmp = dip->hmp;
1710 if (hmp->ronly)
1711 return (EROFS);
1712
1713 ncp = ap->a_nch->ncp;
1714 name = ncp->nc_name;
1715 name_len = ncp->nc_nlen;
1716
e4e20f48
MD
1717 error = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1718 name, name_len, &nip);
c667909f
MD
1719 if (error) {
1720 KKASSERT(nip == NULL);
1721 *ap->a_vpp = NULL;
1722 return error;
1723 }
1724 *ap->a_vpp = hammer2_igetv(nip, &error);
01eabad4 1725 hammer2_chain_unlock(hmp, &nip->chain);
c667909f
MD
1726
1727 if (error == 0) {
1728 cache_setunresolved(ap->a_nch);
1729 cache_setvp(ap->a_nch, *ap->a_vpp);
1730 }
1731 return error;
1732}
1733
4e2004ea
MD
1734/*
1735 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1736 */
1737static
1738int
1739hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1740{
1741 hammer2_mount_t *hmp;
1742 hammer2_inode_t *dip;
1743 hammer2_inode_t *nip;
1744 struct namecache *ncp;
1745 const uint8_t *name;
1746 size_t name_len;
1747 int error;
1748
1749 dip = VTOI(ap->a_dvp);
1750 hmp = dip->hmp;
1751 if (hmp->ronly)
1752 return (EROFS);
1753
1754 ncp = ap->a_nch->ncp;
1755 name = ncp->nc_name;
1756 name_len = ncp->nc_nlen;
1757
1758 ap->a_vap->va_type = VLNK; /* enforce type */
1759
e4e20f48
MD
1760 error = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1761 name, name_len, &nip);
4e2004ea
MD
1762 if (error) {
1763 KKASSERT(nip == NULL);
1764 *ap->a_vpp = NULL;
1765 return error;
1766 }
1767 *ap->a_vpp = hammer2_igetv(nip, &error);
1768
1769 /*
1770 * Build the softlink (~like file data) and finalize the namecache.
1771 */
1772 if (error == 0) {
1773 size_t bytes;
1774 struct uio auio;
1775 struct iovec aiov;
1776
1777 bytes = strlen(ap->a_target);
1778
1779 if (bytes <= HAMMER2_EMBEDDED_BYTES) {
1780 KKASSERT(nip->ip_data.op_flags &
1781 HAMMER2_OPFLAG_DIRECTDATA);
1782 bcopy(ap->a_target, nip->ip_data.u.data, bytes);
1783 nip->ip_data.size = bytes;
1784 } else {
1785 bzero(&auio, sizeof(auio));
1786 bzero(&aiov, sizeof(aiov));
1787 auio.uio_iov = &aiov;
1788 auio.uio_segflg = UIO_SYSSPACE;
1789 auio.uio_rw = UIO_WRITE;
1790 auio.uio_resid = bytes;
1791 auio.uio_iovcnt = 1;
1792 auio.uio_td = curthread;
1793 aiov.iov_base = ap->a_target;
1794 aiov.iov_len = bytes;
1c9f601e 1795 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
4e2004ea
MD
1796 /* XXX handle error */
1797 error = 0;
1798 }
1799 }
01eabad4 1800 hammer2_chain_unlock(hmp, &nip->chain);
4e2004ea
MD
1801
1802 /*
1803 * Finalize namecache
1804 */
1805 if (error == 0) {
1806 cache_setunresolved(ap->a_nch);
1807 cache_setvp(ap->a_nch, *ap->a_vpp);
1808 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1809 }
1810 return error;
1811}
1812
1813/*
1814 * hammer2_vop_nremove { nch, dvp, cred }
1815 */
1816static
1817int
1818hammer2_vop_nremove(struct vop_nremove_args *ap)
1819{
1820 hammer2_inode_t *dip;
1821 hammer2_mount_t *hmp;
1822 struct namecache *ncp;
1823 const uint8_t *name;
1824 size_t name_len;
1825 int error;
1826
1827 dip = VTOI(ap->a_dvp);
1828 hmp = dip->hmp;
1829 if (hmp->ronly)
1830 return(EROFS);
1831
1832 ncp = ap->a_nch->ncp;
1833 name = ncp->nc_name;
1834 name_len = ncp->nc_nlen;
1835
004f88b4 1836 error = hammer2_unlink_file(dip, name, name_len, 0, NULL);
4e2004ea
MD
1837
1838 if (error == 0) {
1839 cache_setunresolved(ap->a_nch);
1840 cache_setvp(ap->a_nch, NULL);
1841 }
1842 return (error);
1843}
1844
1845/*
1846 * hammer2_vop_nrmdir { nch, dvp, cred }
1847 */
1848static
1849int
1850hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1851{
1852 hammer2_inode_t *dip;
1853 hammer2_mount_t *hmp;
1854 struct namecache *ncp;
1855 const uint8_t *name;
1856 size_t name_len;
1857 int error;
1858
1859 dip = VTOI(ap->a_dvp);
1860 hmp = dip->hmp;
1861 if (hmp->ronly)
1862 return(EROFS);
1863
1864 ncp = ap->a_nch->ncp;
1865 name = ncp->nc_name;
1866 name_len = ncp->nc_nlen;
1867
004f88b4 1868 error = hammer2_unlink_file(dip, name, name_len, 1, NULL);
4e2004ea
MD
1869
1870 if (error == 0) {
1871 cache_setunresolved(ap->a_nch);
1872 cache_setvp(ap->a_nch, NULL);
1873 }
1874 return (error);
1875}
1876
6934ae32
MD
1877/*
1878 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1879 */
4e2004ea
MD
1880static
1881int
1882hammer2_vop_nrename(struct vop_nrename_args *ap)
1883{
6934ae32
MD
1884 struct namecache *fncp;
1885 struct namecache *tncp;
1886 hammer2_inode_t *fdip;
1887 hammer2_inode_t *tdip;
1888 hammer2_inode_t *ip;
1889 hammer2_mount_t *hmp;
1890 const uint8_t *fname;
1891 size_t fname_len;
1892 const uint8_t *tname;
1893 size_t tname_len;
1894 int error;
1895
1896 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1897 return(EXDEV);
1898 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1899 return(EXDEV);
1900
1901 fdip = VTOI(ap->a_fdvp); /* source directory */
1902 tdip = VTOI(ap->a_tdvp); /* target directory */
1903
1904 hmp = fdip->hmp; /* check read-only filesystem */
1905 if (hmp->ronly)
1906 return(EROFS);
1907
1908 fncp = ap->a_fnch->ncp; /* entry name in source */
1909 fname = fncp->nc_name;
1910 fname_len = fncp->nc_nlen;
1911
1912 tncp = ap->a_tnch->ncp; /* entry name in target */
1913 tname = tncp->nc_name;
1914 tname_len = tncp->nc_nlen;
1915
e708f8b9
MD
1916 /*
1917 * ip is the inode being removed. If this is a hardlink then
1918 * ip represents the actual file and not the hardlink marker.
1919 */
1920 ip = VTOI(fncp->nc_vp);
6934ae32
MD
1921
1922 /*
1923 * Keep a tight grip on the inode as removing it should disconnect
1924 * it and we don't want to destroy it.
222d9e22
MD
1925 *
1926 * NOTE: To avoid deadlocks we cannot lock (ip) while we are
e708f8b9
MD
1927 * unlinking elements from their directories. Locking
1928 * the nlinks field does not lock the whole inode.
6934ae32 1929 */
e708f8b9 1930 hammer2_inode_lock_nlinks(ip);
6934ae32
MD
1931
1932 /*
1933 * Remove target if it exists
1934 */
004f88b4 1935 error = hammer2_unlink_file(tdip, tname, tname_len, -1, NULL);
6934ae32
MD
1936 if (error && error != ENOENT)
1937 goto done;
1938 cache_setunresolved(ap->a_tnch);
1939 cache_setvp(ap->a_tnch, NULL);
1940
1941 /*
99535653
MD
1942 * Disconnect (fdip, fname) from the source directory. This will
1943 * disconnect (ip) if it represents a direct file. If (ip) represents
1944 * a hardlink the HARDLINK pointer object will be removed but the
1945 * hardlink will stay intact.
e708f8b9
MD
1946 *
1947 * If (ip) is already hardlinked we have to resolve to a consolidated
1948 * file but we do not bump the nlinks count. (ip) must hold the nlinks
1949 * lock & ref for the operation. If the consolidated file has been
1950 * relocated (ip) will be adjusted and the related nlinks lock moved
1951 * along with it.
1952 *
1953 * If (ip) does not have multiple links we can just copy the physical
1954 * contents of the inode.
6934ae32 1955 */
e708f8b9 1956 if (ip->ip_data.nlinks > 1) {
99535653 1957 error = hammer2_hardlink_consolidate(&ip, tdip);
e708f8b9
MD
1958 if (error)
1959 goto done;
1960 }
5f6853df
MD
1961
1962 /*
1963 * NOTE! Because we are retaining (ip) the unlink can fail with
1964 * an EAGAIN.
1965 */
1966 for (;;) {
1967 error = hammer2_unlink_file(fdip, fname, fname_len, -1, ip);
1968 if (error != EAGAIN)
1969 break;
1970 kprintf("hammer2_vop_nrename: unlink race %s\n", fname);
1971 tsleep(fdip, 0, "h2renr", 1);
1972 }
6934ae32
MD
1973 if (error)
1974 goto done;
1975
6934ae32
MD
1976 /*
1977 * Reconnect ip to target directory.
1c9f601e
MD
1978 *
1979 * WARNING: chain locks can lock buffer cache buffers, to avoid
1980 * deadlocks we want to unlock before issuing a cache_*()
1981 * op (that might have to lock a vnode).
6934ae32 1982 */
db0c2eb3 1983 error = hammer2_inode_connect(tdip, ip, tname, tname_len);
6934ae32
MD
1984 if (error == 0) {
1985 cache_rename(ap->a_fnch, ap->a_tnch);
1986 }
222d9e22 1987done:
e708f8b9 1988 hammer2_inode_unlock_nlinks(ip);
6934ae32
MD
1989
1990 return (error);
4e2004ea
MD
1991}
1992
db71f61f
MD
1993static int hammer2_strategy_read(struct vop_strategy_args *ap);
1994static int hammer2_strategy_write(struct vop_strategy_args *ap);
1995
e118c14f 1996static
703720e4 1997int
e118c14f 1998hammer2_vop_strategy(struct vop_strategy_args *ap)
703720e4 1999{
703720e4
MD
2000 struct bio *biop;
2001 struct buf *bp;
703720e4
MD
2002 int error;
2003
703720e4
MD
2004 biop = ap->a_bio;
2005 bp = biop->bio_buf;
703720e4
MD
2006
2007 switch(bp->b_cmd) {
9c2e0de0 2008 case BUF_CMD_READ:
db71f61f 2009 error = hammer2_strategy_read(ap);
01eabad4 2010 ++hammer2_iod_file_read;
db71f61f 2011 break;
9c2e0de0 2012 case BUF_CMD_WRITE:
db71f61f 2013 error = hammer2_strategy_write(ap);
01eabad4 2014 ++hammer2_iod_file_write;
db71f61f 2015 break;
703720e4
MD
2016 default:
2017 bp->b_error = error = EINVAL;
2018 bp->b_flags |= B_ERROR;
2019 biodone(biop);
2020 break;
2021 }
2022
2023 return (error);
2024}
2025
db71f61f
MD
2026static
2027int
2028hammer2_strategy_read(struct vop_strategy_args *ap)
2029{
2030 struct buf *bp;
2031 struct bio *bio;
2032 struct bio *nbio;
2033 hammer2_mount_t *hmp;
2034 hammer2_inode_t *ip;
2035 hammer2_chain_t *parent;
2036 hammer2_chain_t *chain;
8cce658d 2037 hammer2_key_t lbase;
db71f61f
MD
2038
2039 bio = ap->a_bio;
2040 bp = bio->bio_buf;
2041 ip = VTOI(ap->a_vp);
2042 hmp = ip->hmp;
2043 nbio = push_bio(bio);
2044
8cce658d 2045 lbase = bio->bio_offset;
866d5273 2046 chain = NULL;
8cce658d 2047 KKASSERT(((int)lbase & HAMMER2_PBUFMASK) == 0);
db71f61f 2048
866d5273
MD
2049 /*
2050 * We must characterize the logical->physical translation if it
2051 * has not already been cached.
2052 *
2053 * Physical data references < LBUFSIZE are never cached. This
2054 * includes both small-block allocations and inode-embedded data.
2055 */
2056 if (nbio->bio_offset == NOOFFSET) {
db71f61f 2057 parent = &ip->chain;
a0ed3c24
MD
2058 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS |
2059 HAMMER2_RESOLVE_SHARED);
c667909f 2060
8cce658d 2061 chain = hammer2_chain_lookup(hmp, &parent, lbase, lbase,
a0ed3c24
MD
2062 HAMMER2_LOOKUP_NODATA |
2063 HAMMER2_LOOKUP_SHARED);
3ac6a319
MD
2064 if (chain == NULL) {
2065 /*
2066 * Data is zero-fill
2067 */
2068 nbio->bio_offset = ZFOFFSET;
3ac6a319
MD
2069 } else if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
2070 /*
8cce658d 2071 * Data is embedded in the inode (do nothing)
3ac6a319 2072 */
3ac6a319 2073 KKASSERT(chain == parent);
01eabad4 2074 hammer2_chain_unlock(hmp, chain);
866d5273
MD
2075 } else if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
2076 /*
2077 * Data is on-media
866d5273 2078 */
8cce658d
MD
2079 KKASSERT(bp->b_bcount == chain->bytes);
2080 nbio->bio_offset = chain->bref.data_off &
2081 HAMMER2_OFF_MASK;
01eabad4 2082 hammer2_chain_unlock(hmp, chain);
8cce658d 2083 KKASSERT(nbio->bio_offset != 0);
db71f61f 2084 } else {
3ac6a319 2085 panic("hammer2_strategy_read: unknown bref type");
db71f61f 2086 }
01eabad4 2087 hammer2_chain_unlock(hmp, parent);
db71f61f 2088 }
866d5273 2089
8e12e3c9
MD
2090 if (hammer2_debug & 0x0020) {
2091 kprintf("read %016jx %016jx\n",
2092 bio->bio_offset, nbio->bio_offset);
2093 }
2094
866d5273 2095 if (nbio->bio_offset == ZFOFFSET) {
3ac6a319 2096 /*
866d5273 2097 * Data is zero-fill
3ac6a319
MD
2098 */
2099 bp->b_resid = 0;
2100 bp->b_error = 0;
8cce658d 2101 bzero(bp->b_data, bp->b_bcount);
3ac6a319 2102 biodone(nbio);
866d5273 2103 } else if (nbio->bio_offset != NOOFFSET) {
3ac6a319 2104 /*
8cce658d 2105 * Forward direct IO to the device
3ac6a319 2106 */
866d5273 2107 vn_strategy(hmp->devvp, nbio);
8cce658d 2108 } else {
866d5273 2109 /*
8cce658d 2110 * Data is embedded in inode.
866d5273 2111 */
8cce658d
MD
2112 bcopy(chain->data->ipdata.u.data, bp->b_data,
2113 HAMMER2_EMBEDDED_BYTES);
2114 bzero(bp->b_data + HAMMER2_EMBEDDED_BYTES,
2115 bp->b_bcount - HAMMER2_EMBEDDED_BYTES);
db71f61f
MD
2116 bp->b_resid = 0;
2117 bp->b_error = 0;
db71f61f 2118 biodone(nbio);
db71f61f
MD
2119 }
2120 return (0);
2121}
2122
2123static
2124int
2125hammer2_strategy_write(struct vop_strategy_args *ap)
2126{
2127 struct buf *bp;
2128 struct bio *bio;
2129 struct bio *nbio;
2130 hammer2_mount_t *hmp;
2131 hammer2_inode_t *ip;
db71f61f
MD
2132
2133 bio = ap->a_bio;
2134 bp = bio->bio_buf;
2135 ip = VTOI(ap->a_vp);
2136 hmp = ip->hmp;
2137 nbio = push_bio(bio);
2138
8cce658d
MD
2139 KKASSERT((bio->bio_offset & HAMMER2_PBUFMASK64) == 0);
2140 KKASSERT(nbio->bio_offset != 0 && nbio->bio_offset != ZFOFFSET);
db71f61f 2141
8cce658d 2142 if (nbio->bio_offset == NOOFFSET) {
3ac6a319 2143 /*
8cce658d 2144 * Must be embedded in the inode.
3ac6a319 2145 */
8cce658d
MD
2146 KKASSERT(bio->bio_offset == 0);
2147 bcopy(bp->b_data, ip->ip_data.u.data, HAMMER2_EMBEDDED_BYTES);
2148 bp->b_resid = 0;
2149 bp->b_error = 0;
2150 biodone(nbio);
214f4a77
MD
2151
2152 /*
2910a90c 2153 * This special flag does not follow the normal MODIFY rules
214f4a77
MD
2154 * because we might deadlock on ip. Instead we depend on
2155 * VOP_FSYNC() to detect the case.
2156 */
2157 atomic_set_int(&ip->chain.flags, HAMMER2_CHAIN_DIRTYEMBED);
8cce658d 2158 } else {
3ac6a319 2159 /*
8cce658d 2160 * Forward direct IO to the device
3ac6a319 2161 */
8cce658d 2162 vn_strategy(hmp->devvp, nbio);
a92f82c4 2163 }
db71f61f
MD
2164 return (0);
2165}
2166
2910a90c
MD
2167/*
2168 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2169 */
2170static
2171int
2172hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2173{
2174 hammer2_mount_t *hmp;
2175 hammer2_inode_t *ip;
2176 int error;
2177
2178 ip = VTOI(ap->a_vp);
2179 hmp = ip->hmp;
2180
2181 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2182 ap->a_fflag, ap->a_cred);
2183 return (error);
2184}
2185
e118c14f 2186static
f0206a67 2187int
e118c14f 2188hammer2_vop_mountctl(struct vop_mountctl_args *ap)
f0206a67
VS
2189{
2190 struct mount *mp;
e4e20f48 2191 hammer2_pfsmount_t *pmp;
f0206a67
VS
2192 int rc;
2193
2194 switch (ap->a_op) {
2195 case (MOUNTCTL_SET_EXPORT):
2196 mp = ap->a_head.a_ops->head.vv_mount;
e4e20f48 2197 pmp = MPTOPMP(mp);
f0206a67
VS
2198
2199 if (ap->a_ctllen != sizeof(struct export_args))
2200 rc = (EINVAL);
2201 else
e4e20f48 2202 rc = vfs_export(mp, &pmp->export,
10c5dee0 2203 (const struct export_args *)ap->a_ctl);
f0206a67
VS
2204 break;
2205 default:
2206 rc = vop_stdmountctl(ap);
2207 break;
2208 }
2209 return (rc);
2210}
2211
703720e4
MD
2212struct vop_ops hammer2_vnode_vops = {
2213 .vop_default = vop_defaultop,
e118c14f 2214 .vop_fsync = hammer2_vop_fsync,
703720e4
MD
2215 .vop_getpages = vop_stdgetpages,
2216 .vop_putpages = vop_stdputpages,
e118c14f 2217 .vop_access = hammer2_vop_access,
37aa19df 2218 .vop_advlock = hammer2_vop_advlock,
c667909f 2219 .vop_close = hammer2_vop_close,
db0c2eb3 2220 .vop_nlink = hammer2_vop_nlink,
c667909f 2221 .vop_ncreate = hammer2_vop_ncreate,
4e2004ea
MD
2222 .vop_nsymlink = hammer2_vop_nsymlink,
2223 .vop_nremove = hammer2_vop_nremove,
2224 .vop_nrmdir = hammer2_vop_nrmdir,
2225 .vop_nrename = hammer2_vop_nrename,
e118c14f 2226 .vop_getattr = hammer2_vop_getattr,
3ac6a319 2227 .vop_setattr = hammer2_vop_setattr,
e118c14f 2228 .vop_readdir = hammer2_vop_readdir,
4e2004ea 2229 .vop_readlink = hammer2_vop_readlink,
5b4a2132
MD
2230 .vop_getpages = vop_stdgetpages,
2231 .vop_putpages = vop_stdputpages,
e118c14f
MD
2232 .vop_read = hammer2_vop_read,
2233 .vop_write = hammer2_vop_write,
2234 .vop_open = hammer2_vop_open,
2235 .vop_inactive = hammer2_vop_inactive,
2236 .vop_reclaim = hammer2_vop_reclaim,
2237 .vop_nresolve = hammer2_vop_nresolve,
37494cab
MD
2238 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2239 .vop_nmkdir = hammer2_vop_nmkdir,
2910a90c 2240 .vop_ioctl = hammer2_vop_ioctl,
e118c14f
MD
2241 .vop_mountctl = hammer2_vop_mountctl,
2242 .vop_bmap = hammer2_vop_bmap,
2243 .vop_strategy = hammer2_vop_strategy,
703720e4
MD
2244};
2245
2246struct vop_ops hammer2_spec_vops = {
2247
2248};
2249
2250struct vop_ops hammer2_fifo_vops = {
2251
2252};