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