hammer2 - hardlink stabilization pass
[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;
01eabad4 475 error = hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
e028fa74 476 if (error) {
01eabad4 477 hammer2_chain_unlock(hmp, parent);
e028fa74
MD
478 goto done;
479 }
37aa19df
MD
480 chain = hammer2_chain_lookup(hmp, &parent, lkey, lkey, 0);
481 if (chain == NULL) {
482 chain = hammer2_chain_lookup(hmp, &parent,
483 lkey, (hammer2_key_t)-1, 0);
484 }
e028fa74 485 while (chain) {
c667909f
MD
486 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
487 dtype = hammer2_get_dtype(chain->u.ip);
488 saveoff = chain->bref.key & HAMMER2_DIRHASH_USERMSK;
489 r = vop_write_dirent(&error, uio,
490 chain->u.ip->ip_data.inum &
491 HAMMER2_DIRHASH_USERMSK,
492 dtype, chain->u.ip->ip_data.name_len,
493 chain->u.ip->ip_data.filename);
494 if (r)
495 break;
496 if (cookies)
497 cookies[cookie_index] = saveoff;
498 ++cookie_index;
499 } else {
500 /* XXX chain error */
501 kprintf("bad chain type readdir %d\n",
502 chain->bref.type);
503 }
995e78dc
MD
504
505 /*
506 * Keys may not be returned in order so once we have a
507 * placemarker (chain) the scan must allow the full range
508 * or some entries will be missed.
509 */
e028fa74 510 chain = hammer2_chain_next(hmp, &parent, chain,
99535653
MD
511 HAMMER2_DIRHASH_VISIBLE,
512 (hammer2_key_t)-1, 0);
028a55bb
MD
513 if (chain) {
514 saveoff = (chain->bref.key &
515 HAMMER2_DIRHASH_USERMSK) + 1;
516 } else {
517 saveoff = (hammer2_key_t)-1;
518 }
519 if (cookie_index == ncookies)
520 break;
e028fa74 521 }
028a55bb 522 if (chain)
01eabad4
MD
523 hammer2_chain_unlock(hmp, chain);
524 hammer2_chain_unlock(hmp, parent);
e028fa74
MD
525done:
526 if (ap->a_eofflag)
527 *ap->a_eofflag = (chain == NULL);
37aa19df 528 uio->uio_offset = saveoff & ~HAMMER2_DIRHASH_VISIBLE;
e028fa74
MD
529 if (error && cookie_index == 0) {
530 if (cookies) {
531 kfree(cookies, M_TEMP);
532 *ap->a_ncookies = 0;
533 *ap->a_cookies = NULL;
534 }
535 } else {
536 if (cookies) {
537 *ap->a_ncookies = cookie_index;
538 *ap->a_cookies = cookies;
539 }
540 }
541 return (error);
703720e4
MD
542}
543
4e2004ea
MD
544/*
545 * hammer2_vop_readlink { vp, uio, cred }
546 */
547static
548int
549hammer2_vop_readlink(struct vop_readlink_args *ap)
550{
551 struct vnode *vp;
552 hammer2_mount_t *hmp;
553 hammer2_inode_t *ip;
554 int error;
555
556 vp = ap->a_vp;
557 if (vp->v_type != VLNK)
558 return (EINVAL);
559 ip = VTOI(vp);
560 hmp = ip->hmp;
561
562 error = hammer2_read_file(ip, ap->a_uio, 0);
563 return (error);
564}
565
e118c14f 566static
703720e4 567int
e118c14f 568hammer2_vop_read(struct vop_read_args *ap)
703720e4 569{
db71f61f
MD
570 struct vnode *vp;
571 hammer2_mount_t *hmp;
572 hammer2_inode_t *ip;
db71f61f
MD
573 struct uio *uio;
574 int error;
575 int seqcount;
576 int bigread;
577
578 /*
579 * Read operations supported on this vnode?
580 */
581 vp = ap->a_vp;
582 if (vp->v_type != VREG)
583 return (EINVAL);
584
585 /*
586 * Misc
587 */
588 ip = VTOI(vp);
589 hmp = ip->hmp;
590 uio = ap->a_uio;
591 error = 0;
592
593 seqcount = ap->a_ioflag >> 16;
594 bigread = (uio->uio_resid > 100 * 1024 * 1024);
595
4e2004ea 596 error = hammer2_read_file(ip, uio, seqcount);
db71f61f 597 return (error);
47902fef 598}
703720e4 599
e118c14f 600static
47902fef 601int
e118c14f 602hammer2_vop_write(struct vop_write_args *ap)
47902fef 603{
db71f61f
MD
604 thread_t td;
605 struct vnode *vp;
606 hammer2_mount_t *hmp;
607 hammer2_inode_t *ip;
db71f61f
MD
608 struct uio *uio;
609 int error;
db71f61f
MD
610 int seqcount;
611 int bigwrite;
612
613 /*
614 * Read operations supported on this vnode?
615 */
616 vp = ap->a_vp;
617 if (vp->v_type != VREG)
618 return (EINVAL);
619
620 /*
621 * Misc
622 */
623 ip = VTOI(vp);
624 hmp = ip->hmp;
625 uio = ap->a_uio;
626 error = 0;
db71f61f
MD
627 if (hmp->ronly)
628 return (EROFS);
629
630 seqcount = ap->a_ioflag >> 16;
631 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
632
633 /*
634 * Check resource limit
635 */
636 if (uio->uio_resid > 0 && (td = uio->uio_td) != NULL && td->td_proc &&
637 uio->uio_offset + uio->uio_resid >
638 td->td_proc->p_rlimit[RLIMIT_FSIZE].rlim_cur) {
639 lwpsignal(td->td_proc, td->td_lwp, SIGXFSZ);
640 return (EFBIG);
641 }
642
643 bigwrite = (uio->uio_resid > 100 * 1024 * 1024);
644
645 /*
3ac6a319
MD
646 * ip must be locked if extending the file.
647 * ip must be locked to avoid racing a truncation.
8cce658d
MD
648 *
649 * ip must be marked modified, particularly because the write
650 * might wind up being copied into the embedded data area.
3ac6a319
MD
651 */
652 hammer2_inode_lock_ex(ip);
1c9f601e 653 error = hammer2_write_file(ip, uio, ap->a_ioflag, seqcount);
4e2004ea
MD
654
655 hammer2_inode_unlock_ex(ip);
656 return (error);
657}
658
659/*
660 * Perform read operations on a file or symlink given an UNLOCKED
661 * inode and uio.
662 */
663static
664int
665hammer2_read_file(hammer2_inode_t *ip, struct uio *uio, int seqcount)
666{
667 struct buf *bp;
668 int error;
669
670 error = 0;
671
672 /*
673 * UIO read loop
674 */
675 while (uio->uio_resid > 0 && uio->uio_offset < ip->ip_data.size) {
8cce658d
MD
676 hammer2_key_t lbase;
677 hammer2_key_t leof;
678 int lblksize;
679 int loff;
4e2004ea
MD
680 int n;
681
8cce658d
MD
682 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
683 &lbase, &leof);
4e2004ea 684
8cce658d 685 error = cluster_read(ip->vp, leof, lbase, lblksize,
6ba3b984
MD
686 uio->uio_resid, seqcount * BKVASIZE,
687 &bp);
8cce658d 688
4e2004ea
MD
689 if (error)
690 break;
8cce658d
MD
691 loff = (int)(uio->uio_offset - lbase);
692 n = lblksize - loff;
4e2004ea
MD
693 if (n > uio->uio_resid)
694 n = uio->uio_resid;
695 if (n > ip->ip_data.size - uio->uio_offset)
696 n = (int)(ip->ip_data.size - uio->uio_offset);
697 bp->b_flags |= B_AGE;
8cce658d 698 uiomove((char *)bp->b_data + loff, n, uio);
4e2004ea
MD
699 bqrelse(bp);
700 }
701 return (error);
702}
703
704/*
705 * Called with a locked (ip) to do the underlying write to a file or
706 * to build the symlink target.
707 */
708static
709int
1c9f601e
MD
710hammer2_write_file(hammer2_inode_t *ip, struct uio *uio,
711 int ioflag, int seqcount)
4e2004ea 712{
8cce658d 713 hammer2_key_t old_eof;
4e2004ea
MD
714 struct buf *bp;
715 int kflags;
716 int error;
b2b78aaa 717 int modified = 0;
3ac6a319 718
4e2004ea
MD
719 /*
720 * Setup if append
721 */
722 if (ioflag & IO_APPEND)
3ac6a319 723 uio->uio_offset = ip->ip_data.size;
4e2004ea
MD
724 kflags = 0;
725 error = 0;
3ac6a319 726
8cce658d
MD
727 /*
728 * Extend the file if necessary. If the write fails at some point
729 * we will truncate it back down to cover as much as we were able
730 * to write.
731 *
732 * Doing this now makes it easier to calculate buffer sizes in
733 * the loop.
734 */
735 old_eof = ip->ip_data.size;
736 if (uio->uio_offset + uio->uio_resid > ip->ip_data.size) {
b2b78aaa 737 modified = 1;
8cce658d
MD
738 hammer2_extend_file(ip, uio->uio_offset + uio->uio_resid);
739 kflags |= NOTE_EXTEND;
740 }
741
3ac6a319
MD
742 /*
743 * UIO write loop
db71f61f
MD
744 */
745 while (uio->uio_resid > 0) {
8cce658d
MD
746 hammer2_key_t lbase;
747 hammer2_key_t leof;
db71f61f 748 int trivial;
8cce658d
MD
749 int lblksize;
750 int loff;
751 int n;
db71f61f
MD
752
753 /*
754 * Don't allow the buffer build to blow out the buffer
755 * cache.
756 */
a92f82c4
MD
757 if ((ioflag & IO_RECURSE) == 0) {
758 /*
759 * XXX should try to leave this unlocked through
760 * the whole loop
761 */
762 hammer2_chain_unlock(ip->hmp, &ip->chain);
01eabad4
MD
763 bwillwrite(HAMMER2_PBUFSIZE);
764 hammer2_chain_lock(ip->hmp, &ip->chain,
765 HAMMER2_RESOLVE_ALWAYS);
a92f82c4 766 }
db71f61f 767
8cce658d
MD
768 /* XXX bigwrite & signal check test */
769
db71f61f 770 /*
8cce658d
MD
771 * This nominally tells us how much we can cluster and
772 * what the logical buffer size needs to be. Currently
773 * we don't try to cluster the write and just handle one
774 * block at a time.
db71f61f 775 */
8cce658d
MD
776 lblksize = hammer2_calc_logical(ip, uio->uio_offset,
777 &lbase, &leof);
778 loff = (int)(uio->uio_offset - lbase);
779
780 /*
781 * Calculate bytes to copy this transfer and whether the
782 * copy completely covers the buffer or not.
783 */
784 trivial = 0;
785 n = lblksize - loff;
786 if (n > uio->uio_resid) {
787 n = uio->uio_resid;
788 if (uio->uio_offset + n == ip->ip_data.size)
db71f61f 789 trivial = 1;
8cce658d
MD
790 } else if (loff == 0) {
791 trivial = 1;
db71f61f
MD
792 }
793
8cce658d
MD
794 /*
795 * Get the buffer
796 */
db71f61f
MD
797 if (uio->uio_segflg == UIO_NOCOPY) {
798 /*
799 * Issuing a write with the same data backing the
800 * buffer. Instantiate the buffer to collect the
801 * backing vm pages, then read-in any missing bits.
802 *
803 * This case is used by vop_stdputpages().
804 */
8cce658d 805 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
db71f61f
MD
806 if ((bp->b_flags & B_CACHE) == 0) {
807 bqrelse(bp);
8cce658d 808 error = bread(ip->vp, lbase, lblksize, &bp);
db71f61f 809 }
8cce658d 810 } else if (trivial) {
db71f61f
MD
811 /*
812 * Even though we are entirely overwriting the buffer
813 * we may still have to zero it out to avoid a
814 * mmap/write visibility issue.
815 */
8cce658d 816 bp = getblk(ip->vp, lbase, lblksize, GETBLK_BHEAVY, 0);
db71f61f
MD
817 if ((bp->b_flags & B_CACHE) == 0)
818 vfs_bio_clrbuf(bp);
db71f61f
MD
819 } else {
820 /*
821 * Partial overwrite, read in any missing bits then
822 * replace the portion being written.
8cce658d
MD
823 *
824 * (The strategy code will detect zero-fill physical
825 * blocks for this case).
db71f61f 826 */
8cce658d 827 error = bread(ip->vp, lbase, lblksize, &bp);
db71f61f
MD
828 if (error == 0)
829 bheavy(bp);
830 }
831
8cce658d
MD
832 if (error) {
833 brelse(bp);
834 break;
db71f61f
MD
835 }
836
8cce658d
MD
837 /*
838 * We have to assign physical storage to the buffer we intend
839 * to dirty or write now to avoid deadlocks in the strategy
840 * code later.
841 *
842 * This can return NOOFFSET for inode-embedded data. The
843 * strategy code will take care of it in that case.
844 */
845 bp->b_bio2.bio_offset =
846 hammer2_assign_physical(ip, lbase, lblksize, &error);
db71f61f
MD
847 if (error) {
848 brelse(bp);
db71f61f
MD
849 break;
850 }
8cce658d
MD
851
852 /*
853 * Ok, copy the data in
854 */
855 hammer2_chain_unlock(ip->hmp, &ip->chain);
856 error = uiomove(bp->b_data + loff, n, uio);
01eabad4 857 hammer2_chain_lock(ip->hmp, &ip->chain, HAMMER2_RESOLVE_ALWAYS);
db71f61f 858 kflags |= NOTE_WRITE;
b2b78aaa 859 modified = 1;
8cce658d
MD
860
861 if (error) {
862 brelse(bp);
863 break;
864 }
865
b2b78aaa 866 /* XXX update ip_data.mtime */
db71f61f
MD
867
868 /*
869 * Once we dirty a buffer any cached offset becomes invalid.
1c9f601e
MD
870 *
871 * NOTE: For cluster_write() always use the trailing block
872 * size, which is HAMMER2_PBUFSIZE. lblksize is the
873 * eof-straddling blocksize and is incorrect.
db71f61f 874 */
db71f61f 875 bp->b_flags |= B_AGE;
4e2004ea 876 if (ioflag & IO_SYNC) {
db71f61f 877 bwrite(bp);
8cce658d 878 } else if ((ioflag & IO_DIRECT) && loff + n == lblksize) {
01eabad4 879 bp->b_flags |= B_CLUSTEROK;
8cce658d 880 bdwrite(bp);
4e2004ea 881 } else if (ioflag & IO_ASYNC) {
db71f61f 882 bawrite(bp);
1c9f601e
MD
883 } else if (hammer2_cluster_enable) {
884 bp->b_flags |= B_CLUSTEROK;
885 cluster_write(bp, leof, HAMMER2_PBUFSIZE, seqcount);
db71f61f 886 } else {
01eabad4 887 bp->b_flags |= B_CLUSTEROK;
db71f61f
MD
888 bdwrite(bp);
889 }
890 }
8cce658d
MD
891
892 /*
893 * Cleanup. If we extended the file EOF but failed to write through
894 * the entire write is a failure and we have to back-up.
895 */
b2b78aaa 896 if (error && ip->ip_data.size != old_eof) {
8cce658d 897 hammer2_truncate_file(ip, old_eof);
b2b78aaa
MD
898 } else if (modified) {
899 hammer2_chain_modify(ip->hmp, &ip->chain, 0);
900 hammer2_update_time(&ip->ip_data.mtime);
901 }
902 hammer2_knote(ip->vp, kflags);
4e2004ea 903 return error;
703720e4
MD
904}
905
3ac6a319 906/*
8cce658d
MD
907 * Assign physical storage to a logical block.
908 *
909 * NOOFFSET is returned if the data is inode-embedded. In this case the
910 * strategy code will simply bcopy() the data into the inode.
911 */
912static
913hammer2_off_t
914hammer2_assign_physical(hammer2_inode_t *ip, hammer2_key_t lbase,
915 int lblksize, int *errorp)
916{
917 hammer2_mount_t *hmp;
918 hammer2_chain_t *parent;
919 hammer2_chain_t *chain;
920 hammer2_off_t pbase;
921
922 *errorp = 0;
923 hmp = ip->hmp;
924
925 /*
926 * Locate the chain associated with lbase, return a locked chain.
927 * However, do not instantiate any data reference (which utilizes a
928 * device buffer) because we will be using direct IO via the
929 * logical buffer cache buffer.
930 */
931 parent = &ip->chain;
01eabad4 932 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
8cce658d
MD
933
934 chain = hammer2_chain_lookup(hmp, &parent,
935 lbase, lbase,
936 HAMMER2_LOOKUP_NODATA);
937
938 if (chain == NULL) {
939 /*
01eabad4
MD
940 * We found a hole, create a new chain entry.
941 *
942 * NOTE: DATA chains are created without device backing
943 * store (nor do we want any).
8cce658d
MD
944 */
945 chain = hammer2_chain_create(hmp, parent, NULL,
946 lbase, HAMMER2_PBUFRADIX,
947 HAMMER2_BREF_TYPE_DATA,
948 lblksize);
949 pbase = chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX;
950 } else {
951 switch (chain->bref.type) {
952 case HAMMER2_BREF_TYPE_INODE:
953 /*
214f4a77
MD
954 * The data is embedded in the inode. The
955 * caller is responsible for marking the inode
956 * modified and copying the data to the embedded
957 * area.
8cce658d 958 */
8cce658d
MD
959 pbase = NOOFFSET;
960 break;
961 case HAMMER2_BREF_TYPE_DATA:
962 if (chain->bytes != lblksize) {
963 panic("hammer2_assign_physical: "
964 "size mismatch %d/%d\n",
965 lblksize, chain->bytes);
966 }
01eabad4
MD
967 hammer2_chain_modify(hmp, chain,
968 HAMMER2_MODIFY_OPTDATA);
8cce658d
MD
969 pbase = chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX;
970 break;
971 default:
972 panic("hammer2_assign_physical: bad type");
973 /* NOT REACHED */
974 pbase = NOOFFSET;
975 break;
976 }
977 }
978
979 if (chain)
01eabad4
MD
980 hammer2_chain_unlock(hmp, chain);
981 hammer2_chain_unlock(hmp, parent);
8cce658d
MD
982
983 return (pbase);
984}
985
986/*
987 * Truncate the size of a file.
988 *
989 * This routine adjusts ip->ip_data.size smaller, destroying any related
990 * data beyond the new EOF and potentially resizing the block straddling
991 * the EOF.
992 *
993 * The inode must be locked.
3ac6a319
MD
994 */
995static
996void
997hammer2_truncate_file(hammer2_inode_t *ip, hammer2_key_t nsize)
998{
999 hammer2_chain_t *parent;
1000 hammer2_chain_t *chain;
1001 hammer2_mount_t *hmp = ip->hmp;
8cce658d
MD
1002 hammer2_key_t lbase;
1003 hammer2_key_t leof;
1004 struct buf *bp;
1005 int loff;
3ac6a319 1006 int error;
8cce658d
MD
1007 int oblksize;
1008 int nblksize;
1009
01eabad4 1010 hammer2_chain_modify(hmp, &ip->chain, 0);
8cce658d 1011 bp = NULL;
3ac6a319
MD
1012
1013 /*
8cce658d
MD
1014 * Destroy any logical buffer cache buffers beyond the file EOF.
1015 *
1016 * We call nvtruncbuf() w/ trivial == 1 to prevent it from messing
1017 * around with the buffer straddling EOF, because we need to assign
1018 * a new physical offset to it.
3ac6a319
MD
1019 */
1020 if (ip->vp) {
1021 nvtruncbuf(ip->vp, nsize,
8cce658d
MD
1022 HAMMER2_PBUFSIZE, (int)nsize & HAMMER2_PBUFMASK,
1023 1);
3ac6a319 1024 }
3ac6a319
MD
1025
1026 /*
8cce658d 1027 * Setup for lookup/search
3ac6a319
MD
1028 */
1029 parent = &ip->chain;
01eabad4 1030 error = hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
3ac6a319 1031 if (error) {
01eabad4 1032 hammer2_chain_unlock(hmp, parent);
3ac6a319
MD
1033 /* XXX error reporting */
1034 return;
1035 }
1036
1037 /*
8cce658d
MD
1038 * Handle the case where a chain/logical-buffer straddles the new
1039 * EOF. We told nvtruncbuf() above not to mess with the logical
1040 * buffer straddling the EOF because we need to reassign its storage
1041 * and can't let the strategy code do it for us.
3ac6a319 1042 */
8cce658d
MD
1043 loff = (int)nsize & HAMMER2_PBUFMASK;
1044 if (loff && ip->vp) {
1045 oblksize = hammer2_calc_logical(ip, nsize, &lbase, &leof);
1046 error = bread(ip->vp, lbase, oblksize, &bp);
1047 KKASSERT(error == 0);
1048 }
1049 ip->ip_data.size = nsize;
1050 nblksize = hammer2_calc_logical(ip, nsize, &lbase, &leof);
3ac6a319 1051
866d5273 1052 /*
8cce658d
MD
1053 * Fixup the chain element. If we have a logical buffer in-hand
1054 * we don't want to create a conflicting device buffer.
866d5273 1055 */
8cce658d
MD
1056 if (loff && bp) {
1057 chain = hammer2_chain_lookup(hmp, &parent, lbase, lbase,
01eabad4 1058 HAMMER2_LOOKUP_NODATA);
8cce658d
MD
1059 if (chain) {
1060 allocbuf(bp, nblksize);
1061 switch(chain->bref.type) {
1062 case HAMMER2_BREF_TYPE_DATA:
01eabad4
MD
1063 hammer2_chain_resize(hmp, chain,
1064 hammer2_bytes_to_radix(nblksize),
1065 HAMMER2_MODIFY_OPTDATA);
8cce658d
MD
1066 bzero(bp->b_data + loff, nblksize - loff);
1067 bp->b_bio2.bio_offset = chain->bref.data_off &
1068 HAMMER2_OFF_MASK;
1069 break;
1070 case HAMMER2_BREF_TYPE_INODE:
1071 bzero(bp->b_data + loff, nblksize - loff);
1072 bp->b_bio2.bio_offset = NOOFFSET;
1073 break;
1074 default:
1075 panic("hammer2_truncate_file: bad type");
1076 break;
1077 }
01eabad4
MD
1078 hammer2_chain_unlock(hmp, chain);
1079 bp->b_flags |= B_CLUSTEROK;
8cce658d
MD
1080 bdwrite(bp);
1081 } else {
1082 /*
1083 * Destroy clean buffer w/ wrong buffer size. Retain
1084 * backing store.
1085 */
1086 bp->b_flags |= B_RELBUF;
1087 KKASSERT(bp->b_bio2.bio_offset == NOOFFSET);
1088 KKASSERT((bp->b_flags & B_DIRTY) == 0);
1089 bqrelse(bp);
1090 }
1091 } else if (loff) {
01eabad4
MD
1092 /*
1093 * WARNING: This utilizes a device buffer for the data.
1094 *
1095 * XXX case should not occur
1096 */
1097 panic("hammer2_truncate_file: non-zero truncation, no-vnode");
8cce658d
MD
1098 chain = hammer2_chain_lookup(hmp, &parent, lbase, lbase, 0);
1099 if (chain) {
1100 switch(chain->bref.type) {
1101 case HAMMER2_BREF_TYPE_DATA:
1102 hammer2_chain_resize(hmp, chain,
01eabad4
MD
1103 hammer2_bytes_to_radix(nblksize),
1104 0);
1105 hammer2_chain_modify(hmp, chain, 0);
8cce658d
MD
1106 bzero(chain->data->buf + loff, nblksize - loff);
1107 break;
1108 case HAMMER2_BREF_TYPE_INODE:
1109 if (loff < HAMMER2_EMBEDDED_BYTES) {
01eabad4 1110 hammer2_chain_modify(hmp, chain, 0);
8cce658d
MD
1111 bzero(chain->data->ipdata.u.data + loff,
1112 HAMMER2_EMBEDDED_BYTES - loff);
1113 }
1114 break;
1115 }
01eabad4 1116 hammer2_chain_unlock(hmp, chain);
3ac6a319
MD
1117 }
1118 }
1119
866d5273 1120 /*
8cce658d
MD
1121 * Clean up any fragmentory VM pages now that we have properly
1122 * resized the straddling buffer. These pages are no longer
1123 * part of the buffer.
866d5273 1124 */
8cce658d
MD
1125 if (ip->vp) {
1126 nvtruncbuf(ip->vp, nsize,
1127 nblksize, (int)nsize & (nblksize - 1),
1128 1);
1129 }
866d5273 1130
8cce658d
MD
1131 /*
1132 * Destroy any physical blocks after the new EOF point.
1133 */
1134 lbase = (nsize + HAMMER2_PBUFMASK64) & ~HAMMER2_PBUFMASK64;
3ac6a319 1135 chain = hammer2_chain_lookup(hmp, &parent,
8cce658d 1136 lbase, (hammer2_key_t)-1,
01eabad4 1137 HAMMER2_LOOKUP_NODATA);
3ac6a319
MD
1138 while (chain) {
1139 /*
1140 * Degenerate embedded data case, nothing to loop on.
1141 */
8e12e3c9
MD
1142 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
1143 hammer2_chain_unlock(hmp, chain);
3ac6a319 1144 break;
8e12e3c9 1145 }
3ac6a319
MD
1146
1147 /*
1148 * Delete physical data blocks past the file EOF.
1149 */
1150 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
1151 hammer2_chain_delete(hmp, parent, chain);
1152 }
db0c2eb3 1153 /* XXX check parent if empty indirect block & delete */
3ac6a319 1154 chain = hammer2_chain_next(hmp, &parent, chain,
8cce658d 1155 lbase, (hammer2_key_t)-1,
01eabad4 1156 HAMMER2_LOOKUP_NODATA);
3ac6a319 1157 }
01eabad4 1158 hammer2_chain_unlock(hmp, parent);
3ac6a319
MD
1159}
1160
1161/*
8cce658d 1162 * Extend the size of a file. The inode must be locked.
866d5273 1163 *
8cce658d 1164 * We may have to resize the block straddling the old EOF.
3ac6a319
MD
1165 */
1166static
1167void
8cce658d 1168hammer2_extend_file(hammer2_inode_t *ip, hammer2_key_t nsize)
3ac6a319 1169{
8cce658d
MD
1170 hammer2_mount_t *hmp;
1171 hammer2_chain_t *parent;
1172 hammer2_chain_t *chain;
3ac6a319 1173 struct buf *bp;
8cce658d
MD
1174 hammer2_key_t osize;
1175 hammer2_key_t obase;
1176 hammer2_key_t nbase;
1177 hammer2_key_t leof;
1178 int oblksize;
1179 int nblksize;
1180 int nradix;
3ac6a319
MD
1181 int error;
1182
8cce658d
MD
1183 KKASSERT(ip->vp);
1184 hmp = ip->hmp;
1185
01eabad4 1186 hammer2_chain_modify(hmp, &ip->chain, 0);
8cce658d 1187
3ac6a319 1188 /*
8cce658d 1189 * Nothing to do if the direct-data case is still intact
3ac6a319
MD
1190 */
1191 if ((ip->ip_data.op_flags & HAMMER2_OPFLAG_DIRECTDATA) &&
8cce658d
MD
1192 nsize <= HAMMER2_EMBEDDED_BYTES) {
1193 ip->ip_data.size = nsize;
1194 return;
1195 }
1196
1197 /*
1198 * Calculate the blocksize at the original EOF and resize the block
1199 * if necessary. Adjust the file size in the inode.
1200 */
1201 osize = ip->ip_data.size;
1202 oblksize = hammer2_calc_logical(ip, osize, &obase, &leof);
1203 ip->ip_data.size = nsize;
1204 nblksize = hammer2_calc_logical(ip, osize, &nbase, &leof);
1205
1206 /*
1207 * Do all required vnode operations, but do not mess with the
1208 * buffer straddling the orignal EOF.
1209 */
1210 nvextendbuf(ip->vp,
1211 ip->ip_data.size, nsize,
1212 0, nblksize,
1213 0, (int)nsize & HAMMER2_PBUFMASK,
1214 1);
1215
1216 /*
1217 * Early return if we have no more work to do.
1218 */
1219 if (obase == nbase && oblksize == nblksize &&
1220 (ip->ip_data.op_flags & HAMMER2_OPFLAG_DIRECTDATA) == 0) {
1221 return;
1222 }
1223
1224 /*
1225 * We have work to do, including possibly resizing the buffer
1226 * at the EOF point and turning off DIRECTDATA mode.
1227 */
1228 bp = NULL;
1229 if (((int)osize & HAMMER2_PBUFMASK)) {
1230 error = bread(ip->vp, obase, oblksize, &bp);
3ac6a319 1231 KKASSERT(error == 0);
8cce658d
MD
1232
1233 if (obase != nbase) {
1234 allocbuf(bp, HAMMER2_PBUFSIZE);
1235 } else {
1236 allocbuf(bp, nblksize);
1237 }
1238 vfs_bio_clrbuf(bp);
1239 }
1240
1241 /*
1242 * Disable direct-data mode by loading up a buffer cache buffer
1243 * with the data, then converting the inode data area into the
1244 * inode indirect block array area.
1245 */
1246 if (ip->ip_data.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
3ac6a319 1247 ip->ip_data.op_flags &= ~HAMMER2_OPFLAG_DIRECTDATA;
8cce658d 1248 bzero(&ip->ip_data.u.blockset, sizeof(ip->ip_data.u.blockset));
3ac6a319 1249 }
866d5273
MD
1250
1251 /*
8cce658d 1252 * Resize the chain element at the old EOF.
866d5273 1253 */
8cce658d
MD
1254 if (((int)osize & HAMMER2_PBUFMASK)) {
1255 parent = &ip->chain;
01eabad4 1256 error = hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
8cce658d
MD
1257 KKASSERT(error == 0);
1258
1259 nradix = hammer2_bytes_to_radix(nblksize);
1260
1261 chain = hammer2_chain_lookup(hmp, &parent,
1262 obase, obase,
01eabad4 1263 HAMMER2_LOOKUP_NODATA);
8cce658d
MD
1264 if (chain == NULL) {
1265 chain = hammer2_chain_create(hmp, parent, NULL,
1266 obase, nblksize,
1267 HAMMER2_BREF_TYPE_DATA,
1c9f601e 1268 nblksize);
8cce658d
MD
1269 } else {
1270 KKASSERT(chain->bref.type == HAMMER2_BREF_TYPE_DATA);
01eabad4
MD
1271 hammer2_chain_resize(hmp, chain, nradix,
1272 HAMMER2_MODIFY_OPTDATA);
8cce658d
MD
1273 }
1274 bp->b_bio2.bio_offset = chain->bref.data_off &
1275 HAMMER2_OFF_MASK;
01eabad4
MD
1276 hammer2_chain_unlock(hmp, chain);
1277 bp->b_flags |= B_CLUSTEROK;
8cce658d 1278 bdwrite(bp);
01eabad4 1279 hammer2_chain_unlock(hmp, parent);
3ac6a319
MD
1280 }
1281}
1282
e118c14f 1283static
703720e4 1284int
e118c14f 1285hammer2_vop_nresolve(struct vop_nresolve_args *ap)
703720e4 1286{
37494cab 1287 hammer2_inode_t *dip;
e708f8b9 1288 hammer2_inode_t *ip;
37494cab
MD
1289 hammer2_mount_t *hmp;
1290 hammer2_chain_t *parent;
1291 hammer2_chain_t *chain;
1292 struct namecache *ncp;
1293 const uint8_t *name;
1294 size_t name_len;
1295 hammer2_key_t lhc;
1296 int error = 0;
1297 struct vnode *vp;
1298
1299 dip = VTOI(ap->a_dvp);
1300 hmp = dip->hmp;
1301 ncp = ap->a_nch->ncp;
1302 name = ncp->nc_name;
1303 name_len = ncp->nc_nlen;
1304 lhc = hammer2_dirhash(name, name_len);
1305
1306 /*
1307 * Note: In DragonFly the kernel handles '.' and '..'.
1308 */
1309 parent = &dip->chain;
01eabad4 1310 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
37494cab 1311 chain = hammer2_chain_lookup(hmp, &parent,
c667909f
MD
1312 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1313 0);
37494cab
MD
1314 while (chain) {
1315 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1316 chain->u.ip &&
1317 name_len == chain->data->ipdata.name_len &&
1318 bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
1319 break;
1320 }
1321 chain = hammer2_chain_next(hmp, &parent, chain,
c667909f
MD
1322 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1323 0);
37494cab 1324 }
01eabad4 1325 hammer2_chain_unlock(hmp, parent);
37494cab 1326
e708f8b9
MD
1327 /*
1328 * If the inode represents a forwarding entry for a hardlink we have
1329 * to locate the actual inode. The original ip is saved for possible
1330 * deconsolidation. (ip) will only be set to non-NULL when we have
1331 * to locate the real file via a hardlink. ip will be referenced but
1332 * not locked in that situation. chain is passed in locked and
1333 * returned locked.
1334 */
1335 ip = NULL;
1336 if (chain && chain->u.ip->ip_data.type == HAMMER2_OBJTYPE_HARDLINK) {
1337 kprintf("hammer2: need to find hardlink for %s\n",
1338 chain->u.ip->ip_data.filename);
1339 error = hammer2_hardlink_find(dip, &chain, &ip);
1340 if (error) {
1341 if (chain) {
1342 hammer2_chain_unlock(hmp, chain);
1343 chain = NULL;
1344 }
1345 goto failed;
1346 }
1347 }
1348
1349 /*
1350 * Deconsolidate any hardlink whos nlinks == 1. Ignore errors.
1351 * If an error occurs chain and ip are left alone.
1352 */
1353 if (ip && chain && chain->u.ip->ip_data.nlinks == 1 && !hmp->ronly) {
1354 kprintf("hammer2: need to unconsolidate hardlink for %s\n",
1355 chain->u.ip->ip_data.filename);
1356 hammer2_hardlink_deconsolidate(dip, &chain, &ip);
1357 }
1358
1359 /*
1360 * Acquire the related vnode
1361 */
37494cab
MD
1362 if (chain) {
1363 vp = hammer2_igetv(chain->u.ip, &error);
1364 if (error == 0) {
1365 vn_unlock(vp);
1366 cache_setvp(ap->a_nch, vp);
1367 vrele(vp);
1368 }
01eabad4 1369 hammer2_chain_unlock(hmp, chain);
37494cab
MD
1370 } else {
1371 error = ENOENT;
e708f8b9 1372failed:
37494cab
MD
1373 cache_setvp(ap->a_nch, NULL);
1374 }
e708f8b9
MD
1375 if (ip)
1376 hammer2_inode_drop(ip);
37494cab
MD
1377 return error;
1378}
1379
1380static
1381int
1382hammer2_vop_nlookupdotdot(struct vop_nlookupdotdot_args *ap)
1383{
1384 hammer2_inode_t *dip;
1385 hammer2_inode_t *ip;
1386 hammer2_mount_t *hmp;
1387 int error;
1388
1389 dip = VTOI(ap->a_dvp);
1390 hmp = dip->hmp;
1391
1392 if ((ip = dip->pip) == NULL) {
1393 *ap->a_vpp = NULL;
1394 return ENOENT;
1395 }
01eabad4 1396 hammer2_chain_lock(hmp, &ip->chain, HAMMER2_RESOLVE_ALWAYS);
37494cab 1397 *ap->a_vpp = hammer2_igetv(ip, &error);
01eabad4 1398 hammer2_chain_unlock(hmp, &ip->chain);
37494cab
MD
1399
1400 return error;
1401}
1402
1403static
1404int
1405hammer2_vop_nmkdir(struct vop_nmkdir_args *ap)
1406{
1407 hammer2_mount_t *hmp;
1408 hammer2_inode_t *dip;
1409 hammer2_inode_t *nip;
1410 struct namecache *ncp;
1411 const uint8_t *name;
1412 size_t name_len;
1413 int error;
1414
1415 dip = VTOI(ap->a_dvp);
1416 hmp = dip->hmp;
db71f61f
MD
1417 if (hmp->ronly)
1418 return (EROFS);
1419
37494cab
MD
1420 ncp = ap->a_nch->ncp;
1421 name = ncp->nc_name;
1422 name_len = ncp->nc_nlen;
1423
e4e20f48
MD
1424 error = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1425 name, name_len, &nip);
37494cab
MD
1426 if (error) {
1427 KKASSERT(nip == NULL);
1428 *ap->a_vpp = NULL;
1429 return error;
1430 }
1431 *ap->a_vpp = hammer2_igetv(nip, &error);
01eabad4 1432 hammer2_chain_unlock(hmp, &nip->chain);
37494cab
MD
1433
1434 if (error == 0) {
1435 cache_setunresolved(ap->a_nch);
1436 cache_setvp(ap->a_nch, *ap->a_vpp);
1437 }
1438 return error;
703720e4
MD
1439}
1440
db71f61f
MD
1441/*
1442 * Return the largest contiguous physical disk range for the logical
1443 * request.
1444 *
1445 * (struct vnode *vp, off_t loffset, off_t *doffsetp, int *runp, int *runb)
1446 */
e118c14f 1447static
703720e4 1448int
e118c14f 1449hammer2_vop_bmap(struct vop_bmap_args *ap)
703720e4 1450{
db71f61f
MD
1451 struct vnode *vp;
1452 hammer2_mount_t *hmp;
1453 hammer2_inode_t *ip;
1454 hammer2_chain_t *parent;
1455 hammer2_chain_t *chain;
6ba3b984
MD
1456 hammer2_key_t lbeg;
1457 hammer2_key_t lend;
1458 hammer2_off_t pbeg;
1459 hammer2_off_t pbytes;
1460 hammer2_off_t array[HAMMER2_BMAP_COUNT][2];
1461 int loff;
1462 int ai;
db71f61f
MD
1463
1464 /*
1465 * Only supported on regular files
1466 *
1467 * Only supported for read operations (required for cluster_read).
1468 * The block allocation is delayed for write operations.
1469 */
1470 vp = ap->a_vp;
1471 if (vp->v_type != VREG)
1472 return (EOPNOTSUPP);
1473 if (ap->a_cmd != BUF_CMD_READ)
1474 return (EOPNOTSUPP);
1475
1476 ip = VTOI(vp);
1477 hmp = ip->hmp;
6ba3b984 1478 bzero(array, sizeof(array));
5b4a2132 1479
6ba3b984
MD
1480 /*
1481 * Calculate logical range
1482 */
1483 KKASSERT((ap->a_loffset & HAMMER2_LBUFMASK64) == 0);
1484 lbeg = ap->a_loffset & HAMMER2_OFF_MASK_HI;
1485 lend = lbeg + HAMMER2_BMAP_COUNT * HAMMER2_PBUFSIZE - 1;
1486 if (lend < lbeg)
1487 lend = lbeg;
1488 loff = ap->a_loffset & HAMMER2_OFF_MASK_LO;
db71f61f
MD
1489
1490 parent = &ip->chain;
01eabad4 1491 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
6ba3b984
MD
1492 chain = hammer2_chain_lookup(hmp, &parent,
1493 lbeg, lend,
01eabad4 1494 HAMMER2_LOOKUP_NODATA);
3ac6a319 1495 if (chain == NULL) {
3ac6a319 1496 *ap->a_doffsetp = ZFOFFSET;
01eabad4 1497 hammer2_chain_unlock(hmp, parent);
6ba3b984
MD
1498 return (0);
1499 }
1500
1501 while (chain) {
1502 if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
1503 ai = (chain->bref.key - lbeg) / HAMMER2_PBUFSIZE;
1504 KKASSERT(ai >= 0 && ai < HAMMER2_BMAP_COUNT);
1505 array[ai][0] = chain->bref.data_off & HAMMER2_OFF_MASK;
1506 array[ai][1] = chain->bytes;
1507 }
1508 chain = hammer2_chain_next(hmp, &parent, chain,
1509 lbeg, lend,
01eabad4 1510 HAMMER2_LOOKUP_NODATA);
db71f61f 1511 }
01eabad4 1512 hammer2_chain_unlock(hmp, parent);
6ba3b984
MD
1513
1514 /*
1515 * If the requested loffset is not mappable physically we can't
1516 * bmap. The caller will have to access the file data via a
1517 * device buffer.
1518 */
1519 if (array[0][0] == 0 || array[0][1] < loff + HAMMER2_LBUFSIZE) {
1520 *ap->a_doffsetp = NOOFFSET;
1521 return (0);
1522 }
1523
1524 /*
1525 * Calculate the physical disk offset range for array[0]
1526 */
1527 pbeg = array[0][0] + loff;
1528 pbytes = array[0][1] - loff;
1529
1530 for (ai = 1; ai < HAMMER2_BMAP_COUNT; ++ai) {
1531 if (array[ai][0] != pbeg + pbytes)
1532 break;
1533 pbytes += array[ai][1];
1534 }
1535
1536 *ap->a_doffsetp = pbeg;
1537 if (ap->a_runp)
1538 *ap->a_runp = pbytes;
db71f61f 1539 return (0);
703720e4
MD
1540}
1541
e118c14f 1542static
703720e4 1543int
e118c14f 1544hammer2_vop_open(struct vop_open_args *ap)
703720e4 1545{
703720e4
MD
1546 return vop_stdopen(ap);
1547}
1548
37aa19df 1549/*
db0c2eb3 1550 * hammer2_vop_advlock { vp, id, op, fl, flags }
37aa19df
MD
1551 */
1552static
1553int
1554hammer2_vop_advlock(struct vop_advlock_args *ap)
1555{
1556 hammer2_inode_t *ip = VTOI(ap->a_vp);
1557
1558 return (lf_advlock(ap, &ip->advlock, ip->ip_data.size));
1559}
1560
1561
c667909f
MD
1562static
1563int
1564hammer2_vop_close(struct vop_close_args *ap)
1565{
1566 return vop_stdclose(ap);
1567}
1568
1569/*
db0c2eb3
MD
1570 * hammer2_vop_nlink { nch, dvp, vp, cred }
1571 *
e708f8b9 1572 * Create a hardlink from (vp) to {dvp, nch}.
db0c2eb3
MD
1573 */
1574static
1575int
1576hammer2_vop_nlink(struct vop_nlink_args *ap)
1577{
e708f8b9 1578 hammer2_inode_t *dip; /* target directory to create link in */
db0c2eb3 1579 hammer2_inode_t *ip; /* inode we are hardlinking to */
99535653 1580 hammer2_inode_t *oip;
db0c2eb3
MD
1581 hammer2_mount_t *hmp;
1582 struct namecache *ncp;
1583 const uint8_t *name;
1584 size_t name_len;
1585 int error;
1586
1587 dip = VTOI(ap->a_dvp);
1588 hmp = dip->hmp;
1589 if (hmp->ronly)
1590 return (EROFS);
1591
e708f8b9
MD
1592 /*
1593 * (ip) is the inode we are linking to.
1594 */
99535653 1595 ip = oip = VTOI(ap->a_vp);
e708f8b9 1596 hammer2_inode_lock_nlinks(ip);
db0c2eb3
MD
1597
1598 ncp = ap->a_nch->ncp;
1599 name = ncp->nc_name;
1600 name_len = ncp->nc_nlen;
1601
e708f8b9
MD
1602 /*
1603 * Create a consolidated real file for the hardlink, adjust (ip),
1604 * and move the nlinks lock if necessary. Tell the function to
1605 * bump the hardlink count on the consolidated file.
1606 */
99535653 1607 error = hammer2_hardlink_consolidate(&ip, dip);
e708f8b9
MD
1608 if (error)
1609 goto done;
1610
1611 /*
99535653
MD
1612 * If the consolidation changed ip to a HARDLINK pointer we have
1613 * to adjust the vnode to point to the actual ip.
1614 */
1615 if (oip != ip) {
1616 hammer2_chain_ref(hmp, &ip->chain);
1617 hammer2_inode_lock_ex(ip);
1618 hammer2_inode_lock_ex(oip);
1619 ip->vp = ap->a_vp;
1620 ap->a_vp->v_data = ip;
1621 oip->vp = NULL;
1622 hammer2_inode_unlock_ex(oip);
1623 hammer2_inode_unlock_ex(ip);
1624 hammer2_chain_drop(hmp, &oip->chain);
1625 }
1626
1627 /*
1628 * The act of connecting the existing (ip) will properly bump the
1629 * nlinks count. However, vp will incorrectly point at the old
1630 * inode which has now been turned into a OBJTYPE_HARDLINK pointer.
1631 *
1632 * We must reconnect the vp.
e708f8b9
MD
1633 */
1634 hammer2_chain_lock(hmp, &ip->chain, HAMMER2_RESOLVE_ALWAYS);
1635 error = hammer2_inode_connect(dip, ip, name, name_len);
e708f8b9 1636 hammer2_chain_unlock(hmp, &ip->chain);
db0c2eb3
MD
1637 if (error == 0) {
1638 cache_setunresolved(ap->a_nch);
1639 cache_setvp(ap->a_nch, ap->a_vp);
1640 }
e708f8b9
MD
1641done:
1642 hammer2_inode_unlock_nlinks(ip);
db0c2eb3
MD
1643 return error;
1644}
1645
1646/*
1647 * hammer2_vop_ncreate { nch, dvp, vpp, cred, vap }
c667909f
MD
1648 *
1649 * The operating system has already ensured that the directory entry
1650 * does not exist and done all appropriate namespace locking.
1651 */
1652static
1653int
1654hammer2_vop_ncreate(struct vop_ncreate_args *ap)
1655{
1656 hammer2_mount_t *hmp;
1657 hammer2_inode_t *dip;
1658 hammer2_inode_t *nip;
1659 struct namecache *ncp;
1660 const uint8_t *name;
1661 size_t name_len;
1662 int error;
1663
1664 dip = VTOI(ap->a_dvp);
1665 hmp = dip->hmp;
1666 if (hmp->ronly)
1667 return (EROFS);
1668
1669 ncp = ap->a_nch->ncp;
1670 name = ncp->nc_name;
1671 name_len = ncp->nc_nlen;
1672
e4e20f48
MD
1673 error = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1674 name, name_len, &nip);
c667909f
MD
1675 if (error) {
1676 KKASSERT(nip == NULL);
1677 *ap->a_vpp = NULL;
1678 return error;
1679 }
1680 *ap->a_vpp = hammer2_igetv(nip, &error);
01eabad4 1681 hammer2_chain_unlock(hmp, &nip->chain);
c667909f
MD
1682
1683 if (error == 0) {
1684 cache_setunresolved(ap->a_nch);
1685 cache_setvp(ap->a_nch, *ap->a_vpp);
1686 }
1687 return error;
1688}
1689
4e2004ea
MD
1690/*
1691 * hammer2_vop_nsymlink { nch, dvp, vpp, cred, vap, target }
1692 */
1693static
1694int
1695hammer2_vop_nsymlink(struct vop_nsymlink_args *ap)
1696{
1697 hammer2_mount_t *hmp;
1698 hammer2_inode_t *dip;
1699 hammer2_inode_t *nip;
1700 struct namecache *ncp;
1701 const uint8_t *name;
1702 size_t name_len;
1703 int error;
1704
1705 dip = VTOI(ap->a_dvp);
1706 hmp = dip->hmp;
1707 if (hmp->ronly)
1708 return (EROFS);
1709
1710 ncp = ap->a_nch->ncp;
1711 name = ncp->nc_name;
1712 name_len = ncp->nc_nlen;
1713
1714 ap->a_vap->va_type = VLNK; /* enforce type */
1715
e4e20f48
MD
1716 error = hammer2_inode_create(dip, ap->a_vap, ap->a_cred,
1717 name, name_len, &nip);
4e2004ea
MD
1718 if (error) {
1719 KKASSERT(nip == NULL);
1720 *ap->a_vpp = NULL;
1721 return error;
1722 }
1723 *ap->a_vpp = hammer2_igetv(nip, &error);
1724
1725 /*
1726 * Build the softlink (~like file data) and finalize the namecache.
1727 */
1728 if (error == 0) {
1729 size_t bytes;
1730 struct uio auio;
1731 struct iovec aiov;
1732
1733 bytes = strlen(ap->a_target);
1734
1735 if (bytes <= HAMMER2_EMBEDDED_BYTES) {
1736 KKASSERT(nip->ip_data.op_flags &
1737 HAMMER2_OPFLAG_DIRECTDATA);
1738 bcopy(ap->a_target, nip->ip_data.u.data, bytes);
1739 nip->ip_data.size = bytes;
1740 } else {
1741 bzero(&auio, sizeof(auio));
1742 bzero(&aiov, sizeof(aiov));
1743 auio.uio_iov = &aiov;
1744 auio.uio_segflg = UIO_SYSSPACE;
1745 auio.uio_rw = UIO_WRITE;
1746 auio.uio_resid = bytes;
1747 auio.uio_iovcnt = 1;
1748 auio.uio_td = curthread;
1749 aiov.iov_base = ap->a_target;
1750 aiov.iov_len = bytes;
1c9f601e 1751 error = hammer2_write_file(nip, &auio, IO_APPEND, 0);
4e2004ea
MD
1752 /* XXX handle error */
1753 error = 0;
1754 }
1755 }
01eabad4 1756 hammer2_chain_unlock(hmp, &nip->chain);
4e2004ea
MD
1757
1758 /*
1759 * Finalize namecache
1760 */
1761 if (error == 0) {
1762 cache_setunresolved(ap->a_nch);
1763 cache_setvp(ap->a_nch, *ap->a_vpp);
1764 /* hammer2_knote(ap->a_dvp, NOTE_WRITE); */
1765 }
1766 return error;
1767}
1768
1769/*
1770 * hammer2_vop_nremove { nch, dvp, cred }
1771 */
1772static
1773int
1774hammer2_vop_nremove(struct vop_nremove_args *ap)
1775{
1776 hammer2_inode_t *dip;
1777 hammer2_mount_t *hmp;
1778 struct namecache *ncp;
1779 const uint8_t *name;
1780 size_t name_len;
1781 int error;
1782
1783 dip = VTOI(ap->a_dvp);
1784 hmp = dip->hmp;
1785 if (hmp->ronly)
1786 return(EROFS);
1787
1788 ncp = ap->a_nch->ncp;
1789 name = ncp->nc_name;
1790 name_len = ncp->nc_nlen;
1791
e708f8b9 1792 error = hammer2_unlink_file(dip, name, name_len, 0);
4e2004ea
MD
1793
1794 if (error == 0) {
1795 cache_setunresolved(ap->a_nch);
1796 cache_setvp(ap->a_nch, NULL);
1797 }
1798 return (error);
1799}
1800
1801/*
1802 * hammer2_vop_nrmdir { nch, dvp, cred }
1803 */
1804static
1805int
1806hammer2_vop_nrmdir(struct vop_nrmdir_args *ap)
1807{
1808 hammer2_inode_t *dip;
1809 hammer2_mount_t *hmp;
1810 struct namecache *ncp;
1811 const uint8_t *name;
1812 size_t name_len;
1813 int error;
1814
1815 dip = VTOI(ap->a_dvp);
1816 hmp = dip->hmp;
1817 if (hmp->ronly)
1818 return(EROFS);
1819
1820 ncp = ap->a_nch->ncp;
1821 name = ncp->nc_name;
1822 name_len = ncp->nc_nlen;
1823
e708f8b9 1824 error = hammer2_unlink_file(dip, name, name_len, 1);
4e2004ea
MD
1825
1826 if (error == 0) {
1827 cache_setunresolved(ap->a_nch);
1828 cache_setvp(ap->a_nch, NULL);
1829 }
1830 return (error);
1831}
1832
6934ae32
MD
1833/*
1834 * hammer2_vop_nrename { fnch, tnch, fdvp, tdvp, cred }
1835 */
4e2004ea
MD
1836static
1837int
1838hammer2_vop_nrename(struct vop_nrename_args *ap)
1839{
6934ae32
MD
1840 struct namecache *fncp;
1841 struct namecache *tncp;
1842 hammer2_inode_t *fdip;
1843 hammer2_inode_t *tdip;
1844 hammer2_inode_t *ip;
1845 hammer2_mount_t *hmp;
1846 const uint8_t *fname;
1847 size_t fname_len;
1848 const uint8_t *tname;
1849 size_t tname_len;
1850 int error;
1851
1852 if (ap->a_fdvp->v_mount != ap->a_tdvp->v_mount)
1853 return(EXDEV);
1854 if (ap->a_fdvp->v_mount != ap->a_fnch->ncp->nc_vp->v_mount)
1855 return(EXDEV);
1856
1857 fdip = VTOI(ap->a_fdvp); /* source directory */
1858 tdip = VTOI(ap->a_tdvp); /* target directory */
1859
1860 hmp = fdip->hmp; /* check read-only filesystem */
1861 if (hmp->ronly)
1862 return(EROFS);
1863
1864 fncp = ap->a_fnch->ncp; /* entry name in source */
1865 fname = fncp->nc_name;
1866 fname_len = fncp->nc_nlen;
1867
1868 tncp = ap->a_tnch->ncp; /* entry name in target */
1869 tname = tncp->nc_name;
1870 tname_len = tncp->nc_nlen;
1871
e708f8b9
MD
1872 /*
1873 * ip is the inode being removed. If this is a hardlink then
1874 * ip represents the actual file and not the hardlink marker.
1875 */
1876 ip = VTOI(fncp->nc_vp);
6934ae32
MD
1877
1878 /*
1879 * Keep a tight grip on the inode as removing it should disconnect
1880 * it and we don't want to destroy it.
222d9e22
MD
1881 *
1882 * NOTE: To avoid deadlocks we cannot lock (ip) while we are
e708f8b9
MD
1883 * unlinking elements from their directories. Locking
1884 * the nlinks field does not lock the whole inode.
6934ae32 1885 */
e708f8b9 1886 hammer2_inode_lock_nlinks(ip);
6934ae32
MD
1887
1888 /*
1889 * Remove target if it exists
1890 */
e708f8b9 1891 error = hammer2_unlink_file(tdip, tname, tname_len, -1);
6934ae32
MD
1892 if (error && error != ENOENT)
1893 goto done;
1894 cache_setunresolved(ap->a_tnch);
1895 cache_setvp(ap->a_tnch, NULL);
1896
1897 /*
99535653
MD
1898 * Disconnect (fdip, fname) from the source directory. This will
1899 * disconnect (ip) if it represents a direct file. If (ip) represents
1900 * a hardlink the HARDLINK pointer object will be removed but the
1901 * hardlink will stay intact.
e708f8b9
MD
1902 *
1903 * If (ip) is already hardlinked we have to resolve to a consolidated
1904 * file but we do not bump the nlinks count. (ip) must hold the nlinks
1905 * lock & ref for the operation. If the consolidated file has been
1906 * relocated (ip) will be adjusted and the related nlinks lock moved
1907 * along with it.
1908 *
1909 * If (ip) does not have multiple links we can just copy the physical
1910 * contents of the inode.
6934ae32 1911 */
e708f8b9 1912 if (ip->ip_data.nlinks > 1) {
99535653 1913 error = hammer2_hardlink_consolidate(&ip, tdip);
e708f8b9
MD
1914 if (error)
1915 goto done;
1916 }
1917 error = hammer2_unlink_file(fdip, fname, fname_len, -1);
6934ae32
MD
1918 if (error)
1919 goto done;
1920
6934ae32
MD
1921 /*
1922 * Reconnect ip to target directory.
1c9f601e
MD
1923 *
1924 * WARNING: chain locks can lock buffer cache buffers, to avoid
1925 * deadlocks we want to unlock before issuing a cache_*()
1926 * op (that might have to lock a vnode).
6934ae32 1927 */
01eabad4 1928 hammer2_chain_lock(hmp, &ip->chain, HAMMER2_RESOLVE_ALWAYS);
db0c2eb3 1929 error = hammer2_inode_connect(tdip, ip, tname, tname_len);
1c9f601e 1930 hammer2_chain_unlock(hmp, &ip->chain);
6934ae32
MD
1931
1932 if (error == 0) {
1933 cache_rename(ap->a_fnch, ap->a_tnch);
1934 }
222d9e22 1935done:
e708f8b9 1936 hammer2_inode_unlock_nlinks(ip);
6934ae32
MD
1937
1938 return (error);
4e2004ea
MD
1939}
1940
db71f61f
MD
1941static int hammer2_strategy_read(struct vop_strategy_args *ap);
1942static int hammer2_strategy_write(struct vop_strategy_args *ap);
1943
e118c14f 1944static
703720e4 1945int
e118c14f 1946hammer2_vop_strategy(struct vop_strategy_args *ap)
703720e4 1947{
703720e4
MD
1948 struct bio *biop;
1949 struct buf *bp;
703720e4
MD
1950 int error;
1951
703720e4
MD
1952 biop = ap->a_bio;
1953 bp = biop->bio_buf;
703720e4
MD
1954
1955 switch(bp->b_cmd) {
9c2e0de0 1956 case BUF_CMD_READ:
db71f61f 1957 error = hammer2_strategy_read(ap);
01eabad4 1958 ++hammer2_iod_file_read;
db71f61f 1959 break;
9c2e0de0 1960 case BUF_CMD_WRITE:
db71f61f 1961 error = hammer2_strategy_write(ap);
01eabad4 1962 ++hammer2_iod_file_write;
db71f61f 1963 break;
703720e4
MD
1964 default:
1965 bp->b_error = error = EINVAL;
1966 bp->b_flags |= B_ERROR;
1967 biodone(biop);
1968 break;
1969 }
1970
1971 return (error);
1972}
1973
db71f61f
MD
1974static
1975int
1976hammer2_strategy_read(struct vop_strategy_args *ap)
1977{
1978 struct buf *bp;
1979 struct bio *bio;
1980 struct bio *nbio;
1981 hammer2_mount_t *hmp;
1982 hammer2_inode_t *ip;
1983 hammer2_chain_t *parent;
1984 hammer2_chain_t *chain;
8cce658d 1985 hammer2_key_t lbase;
db71f61f
MD
1986
1987 bio = ap->a_bio;
1988 bp = bio->bio_buf;
1989 ip = VTOI(ap->a_vp);
1990 hmp = ip->hmp;
1991 nbio = push_bio(bio);
1992
8cce658d 1993 lbase = bio->bio_offset;
866d5273 1994 chain = NULL;
8cce658d 1995 KKASSERT(((int)lbase & HAMMER2_PBUFMASK) == 0);
db71f61f 1996
866d5273
MD
1997 /*
1998 * We must characterize the logical->physical translation if it
1999 * has not already been cached.
2000 *
2001 * Physical data references < LBUFSIZE are never cached. This
2002 * includes both small-block allocations and inode-embedded data.
2003 */
2004 if (nbio->bio_offset == NOOFFSET) {
db71f61f 2005 parent = &ip->chain;
01eabad4 2006 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
c667909f 2007
8cce658d 2008 chain = hammer2_chain_lookup(hmp, &parent, lbase, lbase,
01eabad4 2009 HAMMER2_LOOKUP_NODATA);
3ac6a319
MD
2010 if (chain == NULL) {
2011 /*
2012 * Data is zero-fill
2013 */
2014 nbio->bio_offset = ZFOFFSET;
3ac6a319
MD
2015 } else if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
2016 /*
8cce658d 2017 * Data is embedded in the inode (do nothing)
3ac6a319 2018 */
3ac6a319 2019 KKASSERT(chain == parent);
01eabad4 2020 hammer2_chain_unlock(hmp, chain);
866d5273
MD
2021 } else if (chain->bref.type == HAMMER2_BREF_TYPE_DATA) {
2022 /*
2023 * Data is on-media
866d5273 2024 */
8cce658d
MD
2025 KKASSERT(bp->b_bcount == chain->bytes);
2026 nbio->bio_offset = chain->bref.data_off &
2027 HAMMER2_OFF_MASK;
01eabad4 2028 hammer2_chain_unlock(hmp, chain);
8cce658d 2029 KKASSERT(nbio->bio_offset != 0);
db71f61f 2030 } else {
3ac6a319 2031 panic("hammer2_strategy_read: unknown bref type");
db71f61f 2032 }
01eabad4 2033 hammer2_chain_unlock(hmp, parent);
db71f61f 2034 }
866d5273 2035
8e12e3c9
MD
2036 if (hammer2_debug & 0x0020) {
2037 kprintf("read %016jx %016jx\n",
2038 bio->bio_offset, nbio->bio_offset);
2039 }
2040
866d5273 2041 if (nbio->bio_offset == ZFOFFSET) {
3ac6a319 2042 /*
866d5273 2043 * Data is zero-fill
3ac6a319
MD
2044 */
2045 bp->b_resid = 0;
2046 bp->b_error = 0;
8cce658d 2047 bzero(bp->b_data, bp->b_bcount);
3ac6a319 2048 biodone(nbio);
866d5273 2049 } else if (nbio->bio_offset != NOOFFSET) {
3ac6a319 2050 /*
8cce658d 2051 * Forward direct IO to the device
3ac6a319 2052 */
866d5273 2053 vn_strategy(hmp->devvp, nbio);
8cce658d 2054 } else {
866d5273 2055 /*
8cce658d 2056 * Data is embedded in inode.
866d5273 2057 */
8cce658d
MD
2058 bcopy(chain->data->ipdata.u.data, bp->b_data,
2059 HAMMER2_EMBEDDED_BYTES);
2060 bzero(bp->b_data + HAMMER2_EMBEDDED_BYTES,
2061 bp->b_bcount - HAMMER2_EMBEDDED_BYTES);
db71f61f
MD
2062 bp->b_resid = 0;
2063 bp->b_error = 0;
db71f61f 2064 biodone(nbio);
db71f61f
MD
2065 }
2066 return (0);
2067}
2068
2069static
2070int
2071hammer2_strategy_write(struct vop_strategy_args *ap)
2072{
2073 struct buf *bp;
2074 struct bio *bio;
2075 struct bio *nbio;
2076 hammer2_mount_t *hmp;
2077 hammer2_inode_t *ip;
db71f61f
MD
2078
2079 bio = ap->a_bio;
2080 bp = bio->bio_buf;
2081 ip = VTOI(ap->a_vp);
2082 hmp = ip->hmp;
2083 nbio = push_bio(bio);
2084
8cce658d
MD
2085 KKASSERT((bio->bio_offset & HAMMER2_PBUFMASK64) == 0);
2086 KKASSERT(nbio->bio_offset != 0 && nbio->bio_offset != ZFOFFSET);
db71f61f 2087
8cce658d 2088 if (nbio->bio_offset == NOOFFSET) {
3ac6a319 2089 /*
8cce658d 2090 * Must be embedded in the inode.
3ac6a319 2091 */
8cce658d
MD
2092 KKASSERT(bio->bio_offset == 0);
2093 bcopy(bp->b_data, ip->ip_data.u.data, HAMMER2_EMBEDDED_BYTES);
2094 bp->b_resid = 0;
2095 bp->b_error = 0;
2096 biodone(nbio);
214f4a77
MD
2097
2098 /*
2910a90c 2099 * This special flag does not follow the normal MODIFY rules
214f4a77
MD
2100 * because we might deadlock on ip. Instead we depend on
2101 * VOP_FSYNC() to detect the case.
2102 */
2103 atomic_set_int(&ip->chain.flags, HAMMER2_CHAIN_DIRTYEMBED);
8cce658d 2104 } else {
3ac6a319 2105 /*
8cce658d 2106 * Forward direct IO to the device
3ac6a319 2107 */
8cce658d 2108 vn_strategy(hmp->devvp, nbio);
a92f82c4 2109 }
db71f61f
MD
2110 return (0);
2111}
2112
2910a90c
MD
2113/*
2114 * hammer2_vop_ioctl { vp, command, data, fflag, cred }
2115 */
2116static
2117int
2118hammer2_vop_ioctl(struct vop_ioctl_args *ap)
2119{
2120 hammer2_mount_t *hmp;
2121 hammer2_inode_t *ip;
2122 int error;
2123
2124 ip = VTOI(ap->a_vp);
2125 hmp = ip->hmp;
2126
2127 error = hammer2_ioctl(ip, ap->a_command, (void *)ap->a_data,
2128 ap->a_fflag, ap->a_cred);
2129 return (error);
2130}
2131
e118c14f 2132static
f0206a67 2133int
e118c14f 2134hammer2_vop_mountctl(struct vop_mountctl_args *ap)
f0206a67
VS
2135{
2136 struct mount *mp;
e4e20f48 2137 hammer2_pfsmount_t *pmp;
f0206a67
VS
2138 int rc;
2139
2140 switch (ap->a_op) {
2141 case (MOUNTCTL_SET_EXPORT):
2142 mp = ap->a_head.a_ops->head.vv_mount;
e4e20f48 2143 pmp = MPTOPMP(mp);
f0206a67
VS
2144
2145 if (ap->a_ctllen != sizeof(struct export_args))
2146 rc = (EINVAL);
2147 else
e4e20f48 2148 rc = vfs_export(mp, &pmp->export,
10c5dee0 2149 (const struct export_args *)ap->a_ctl);
f0206a67
VS
2150 break;
2151 default:
2152 rc = vop_stdmountctl(ap);
2153 break;
2154 }
2155 return (rc);
2156}
2157
703720e4
MD
2158struct vop_ops hammer2_vnode_vops = {
2159 .vop_default = vop_defaultop,
e118c14f 2160 .vop_fsync = hammer2_vop_fsync,
703720e4
MD
2161 .vop_getpages = vop_stdgetpages,
2162 .vop_putpages = vop_stdputpages,
e118c14f 2163 .vop_access = hammer2_vop_access,
37aa19df 2164 .vop_advlock = hammer2_vop_advlock,
c667909f 2165 .vop_close = hammer2_vop_close,
db0c2eb3 2166 .vop_nlink = hammer2_vop_nlink,
c667909f 2167 .vop_ncreate = hammer2_vop_ncreate,
4e2004ea
MD
2168 .vop_nsymlink = hammer2_vop_nsymlink,
2169 .vop_nremove = hammer2_vop_nremove,
2170 .vop_nrmdir = hammer2_vop_nrmdir,
2171 .vop_nrename = hammer2_vop_nrename,
e118c14f 2172 .vop_getattr = hammer2_vop_getattr,
3ac6a319 2173 .vop_setattr = hammer2_vop_setattr,
e118c14f 2174 .vop_readdir = hammer2_vop_readdir,
4e2004ea 2175 .vop_readlink = hammer2_vop_readlink,
5b4a2132
MD
2176 .vop_getpages = vop_stdgetpages,
2177 .vop_putpages = vop_stdputpages,
e118c14f
MD
2178 .vop_read = hammer2_vop_read,
2179 .vop_write = hammer2_vop_write,
2180 .vop_open = hammer2_vop_open,
2181 .vop_inactive = hammer2_vop_inactive,
2182 .vop_reclaim = hammer2_vop_reclaim,
2183 .vop_nresolve = hammer2_vop_nresolve,
37494cab
MD
2184 .vop_nlookupdotdot = hammer2_vop_nlookupdotdot,
2185 .vop_nmkdir = hammer2_vop_nmkdir,
2910a90c 2186 .vop_ioctl = hammer2_vop_ioctl,
e118c14f
MD
2187 .vop_mountctl = hammer2_vop_mountctl,
2188 .vop_bmap = hammer2_vop_bmap,
2189 .vop_strategy = hammer2_vop_strategy,
703720e4
MD
2190};
2191
2192struct vop_ops hammer2_spec_vops = {
2193
2194};
2195
2196struct vop_ops hammer2_fifo_vops = {
2197
2198};