hammer2 - update documentation, begin working on callback I/O
[dragonfly.git] / sys / vfs / hammer2 / hammer2_inode.c
CommitLineData
7cfa8da5 1/*
8138a154 2 * Copyright (c) 2011-2014 The DragonFly Project. All rights reserved.
7cfa8da5
MD
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 */
35#include <sys/cdefs.h>
7cfa8da5
MD
36#include <sys/param.h>
37#include <sys/systm.h>
38#include <sys/types.h>
39#include <sys/lock.h>
40#include <sys/uuid.h>
41
42#include "hammer2.h"
43
8138a154
MD
44#define INODE_DEBUG 0
45
044541cd 46static void hammer2_inode_move_to_hidden(hammer2_trans_t *trans,
da6f36f4 47 hammer2_cluster_t **cparentp,
278ab2b2 48 hammer2_cluster_t **clusterp,
044541cd
MD
49 hammer2_tid_t inum);
50
9797e933
MD
51RB_GENERATE2(hammer2_inode_tree, hammer2_inode, rbnode, hammer2_inode_cmp,
52 hammer2_tid_t, inum);
53
54int
55hammer2_inode_cmp(hammer2_inode_t *ip1, hammer2_inode_t *ip2)
56{
57 if (ip1->inum < ip2->inum)
58 return(-1);
59 if (ip1->inum > ip2->inum)
60 return(1);
61 return(0);
62}
63
731b2a84
MD
64/*
65 * HAMMER2 inode locks
66 *
67 * HAMMER2 offers shared locks and exclusive locks on inodes.
68 *
278ab2b2
MD
69 * The inode locking function locks the inode itself, resolves any stale
70 * chains in the inode's cluster, and allocates a fresh copy of the
71 * cluster with 1 ref and all the underlying chains locked. Duplication
72 * races are handled by this function.
731b2a84 73 *
278ab2b2 74 * ip->cluster will be stable while the inode is locked.
731b2a84
MD
75 *
76 * NOTE: We don't combine the inode/chain lock because putting away an
77 * inode would otherwise confuse multiple lock holders of the inode.
278ab2b2 78 *
05dd26e4
MD
79 * NOTE: In-memory inodes always point to hardlink targets (the actual file),
80 * and never point to a hardlink pointer.
731b2a84 81 */
278ab2b2 82hammer2_cluster_t *
731b2a84
MD
83hammer2_inode_lock_ex(hammer2_inode_t *ip)
84{
bca9f8e6 85 const hammer2_inode_data_t *ripdata;
278ab2b2 86 hammer2_cluster_t *cluster;
731b2a84 87 hammer2_chain_t *chain;
278ab2b2 88 int i;
731b2a84
MD
89
90 hammer2_inode_ref(ip);
91 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_EXCLUSIVE);
837bd39b
MD
92 cluster = hammer2_cluster_copy(&ip->cluster,
93 HAMMER2_CLUSTER_COPY_NOCHAINS);
278ab2b2 94
84e47819
MD
95 ip->cluster.focus = NULL;
96 cluster->focus = NULL;
97
278ab2b2
MD
98 for (i = 0; i < cluster->nchains; ++i) {
99 chain = ip->cluster.array[i];
837bd39b
MD
100 if (chain == NULL) {
101 kprintf("inode_lock: %p: missing chain\n", ip);
102 continue;
103 }
da6f36f4
MD
104
105 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS);
278ab2b2 106 cluster->array[i] = chain;
84e47819
MD
107 if (cluster->focus == NULL)
108 cluster->focus = chain;
58e43599
MD
109 if (ip->cluster.focus == NULL)
110 ip->cluster.focus = chain;
731b2a84 111 }
278ab2b2
MD
112
113 /*
114 * Returned cluster must resolve hardlink pointers
115 */
bca9f8e6
MD
116 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
117 KKASSERT(ripdata->type != HAMMER2_OBJTYPE_HARDLINK);
05dd26e4 118 /*
bca9f8e6 119 if (ripdata->type == HAMMER2_OBJTYPE_HARDLINK &&
278ab2b2 120 (cluster->focus->flags & HAMMER2_CHAIN_DELETED) == 0) {
da6f36f4 121 error = hammer2_hardlink_find(ip->pip, NULL, cluster);
0924b3f8 122 KKASSERT(error == 0);
0924b3f8 123 }
05dd26e4 124 */
278ab2b2
MD
125
126 return (cluster);
731b2a84
MD
127}
128
129void
278ab2b2 130hammer2_inode_unlock_ex(hammer2_inode_t *ip, hammer2_cluster_t *cluster)
731b2a84 131{
278ab2b2
MD
132 if (cluster)
133 hammer2_cluster_unlock(cluster);
731b2a84
MD
134 ccms_thread_unlock(&ip->topo_cst);
135 hammer2_inode_drop(ip);
136}
137
138/*
139 * NOTE: We don't combine the inode/chain lock because putting away an
140 * inode would otherwise confuse multiple lock holders of the inode.
141 *
142 * Shared locks are especially sensitive to having too many shared
143 * lock counts (from the same thread) on certain paths which might
144 * need to upgrade them. Only one count of a shared lock can be
145 * upgraded.
146 */
278ab2b2 147hammer2_cluster_t *
731b2a84
MD
148hammer2_inode_lock_sh(hammer2_inode_t *ip)
149{
bca9f8e6 150 const hammer2_inode_data_t *ripdata;
278ab2b2 151 hammer2_cluster_t *cluster;
731b2a84 152 hammer2_chain_t *chain;
278ab2b2 153 int i;
731b2a84
MD
154
155 hammer2_inode_ref(ip);
837bd39b
MD
156 cluster = hammer2_cluster_copy(&ip->cluster,
157 HAMMER2_CLUSTER_COPY_NOCHAINS);
278ab2b2 158 ccms_thread_lock(&ip->topo_cst, CCMS_STATE_SHARED);
731b2a84 159
84e47819
MD
160 cluster->focus = NULL;
161
278ab2b2
MD
162 for (i = 0; i < cluster->nchains; ++i) {
163 chain = ip->cluster.array[i];
837bd39b
MD
164
165 if (chain == NULL) {
166 kprintf("inode_lock: %p: missing chain\n", ip);
167 continue;
168 }
169
294fa5f9
MD
170 hammer2_chain_lock(chain, HAMMER2_RESOLVE_ALWAYS |
171 HAMMER2_RESOLVE_SHARED);
278ab2b2 172 cluster->array[i] = chain;
84e47819
MD
173 if (cluster->focus == NULL)
174 cluster->focus = chain;
731b2a84 175 }
278ab2b2
MD
176
177 /*
178 * Returned cluster must resolve hardlink pointers
179 */
bca9f8e6
MD
180 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
181 KKASSERT(ripdata->type != HAMMER2_OBJTYPE_HARDLINK);
05dd26e4 182 /*
bca9f8e6 183 if (ripdata->type == HAMMER2_OBJTYPE_HARDLINK &&
278ab2b2 184 (cluster->focus->flags & HAMMER2_CHAIN_DELETED) == 0) {
da6f36f4 185 error = hammer2_hardlink_find(ip->pip, NULL, cluster);
278ab2b2
MD
186 KKASSERT(error == 0);
187 }
05dd26e4 188 */
278ab2b2
MD
189
190 return (cluster);
731b2a84
MD
191}
192
193void
278ab2b2 194hammer2_inode_unlock_sh(hammer2_inode_t *ip, hammer2_cluster_t *cluster)
731b2a84 195{
278ab2b2
MD
196 if (cluster)
197 hammer2_cluster_unlock(cluster);
731b2a84
MD
198 ccms_thread_unlock(&ip->topo_cst);
199 hammer2_inode_drop(ip);
200}
201
202ccms_state_t
203hammer2_inode_lock_temp_release(hammer2_inode_t *ip)
204{
205 return(ccms_thread_lock_temp_release(&ip->topo_cst));
206}
207
208void
209hammer2_inode_lock_temp_restore(hammer2_inode_t *ip, ccms_state_t ostate)
210{
211 ccms_thread_lock_temp_restore(&ip->topo_cst, ostate);
212}
213
214ccms_state_t
215hammer2_inode_lock_upgrade(hammer2_inode_t *ip)
216{
217 return(ccms_thread_lock_upgrade(&ip->topo_cst));
218}
219
220void
221hammer2_inode_lock_downgrade(hammer2_inode_t *ip, ccms_state_t ostate)
222{
223 ccms_thread_lock_downgrade(&ip->topo_cst, ostate);
224}
225
226/*
227 * Lookup an inode by inode number
228 */
9797e933
MD
229hammer2_inode_t *
230hammer2_inode_lookup(hammer2_pfsmount_t *pmp, hammer2_tid_t inum)
231{
232 hammer2_inode_t *ip;
233
50456506
MD
234 KKASSERT(pmp);
235 if (pmp->spmp_hmp) {
236 ip = NULL;
237 } else {
9797e933
MD
238 spin_lock(&pmp->inum_spin);
239 ip = RB_LOOKUP(hammer2_inode_tree, &pmp->inum_tree, inum);
240 if (ip)
241 hammer2_inode_ref(ip);
242 spin_unlock(&pmp->inum_spin);
9797e933
MD
243 }
244 return(ip);
245}
246
7cfa8da5
MD
247/*
248 * Adding a ref to an inode is only legal if the inode already has at least
249 * one ref.
af791767
MD
250 *
251 * (can be called with spinlock held)
7cfa8da5
MD
252 */
253void
254hammer2_inode_ref(hammer2_inode_t *ip)
255{
476d2aad 256 atomic_add_int(&ip->refs, 1);
7cfa8da5
MD
257}
258
259/*
260 * Drop an inode reference, freeing the inode when the last reference goes
261 * away.
262 */
263void
264hammer2_inode_drop(hammer2_inode_t *ip)
265{
99da41ea 266 hammer2_pfsmount_t *pmp;
10252dc7 267 hammer2_inode_t *pip;
10252dc7 268 u_int refs;
476d2aad 269
ea155208 270 while (ip) {
476d2aad
MD
271 refs = ip->refs;
272 cpu_ccfence();
273 if (refs == 1) {
9797e933
MD
274 /*
275 * Transition to zero, must interlock with
276 * the inode inumber lookup tree (if applicable).
9797e933 277 */
99da41ea 278 pmp = ip->pmp;
50456506
MD
279 KKASSERT(pmp);
280 spin_lock(&pmp->inum_spin);
9797e933 281
476d2aad 282 if (atomic_cmpset_int(&ip->refs, 1, 0)) {
476d2aad 283 KKASSERT(ip->topo_cst.count == 0);
9797e933
MD
284 if (ip->flags & HAMMER2_INODE_ONRBTREE) {
285 atomic_clear_int(&ip->flags,
286 HAMMER2_INODE_ONRBTREE);
287 RB_REMOVE(hammer2_inode_tree,
99da41ea 288 &pmp->inum_tree, ip);
9797e933 289 }
50456506 290 spin_unlock(&pmp->inum_spin);
10252dc7 291
10252dc7
MD
292 pip = ip->pip;
293 ip->pip = NULL;
99da41ea 294 ip->pmp = NULL;
cd189b1e
MD
295
296 /*
84e47819 297 * Cleaning out ip->cluster isn't entirely
cd189b1e
MD
298 * trivial.
299 */
300 hammer2_inode_repoint(ip, NULL, NULL);
10252dc7
MD
301
302 /*
303 * We have to drop pip (if non-NULL) to
304 * dispose of our implied reference from
305 * ip->pip. We can simply loop on it.
306 */
50456506
MD
307 kfree(ip, pmp->minode);
308 atomic_add_long(&pmp->inmem_inodes, -1);
10252dc7 309 ip = pip;
ea155208 310 /* continue with pip (can be NULL) */
9797e933 311 } else {
50456506 312 spin_unlock(&ip->pmp->inum_spin);
476d2aad
MD
313 }
314 } else {
9797e933
MD
315 /*
316 * Non zero transition
317 */
476d2aad
MD
318 if (atomic_cmpset_int(&ip->refs, refs, refs - 1))
319 break;
320 }
321 }
7cfa8da5
MD
322}
323
324/*
325 * Get the vnode associated with the given inode, allocating the vnode if
a0ed3c24
MD
326 * necessary. The vnode will be returned exclusively locked.
327 *
328 * The caller must lock the inode (shared or exclusive).
7cfa8da5
MD
329 *
330 * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
331 * races.
7cfa8da5
MD
332 */
333struct vnode *
84e47819 334hammer2_igetv(hammer2_inode_t *ip, hammer2_cluster_t *cparent, int *errorp)
7cfa8da5 335{
bca9f8e6 336 const hammer2_inode_data_t *ripdata;
e4e20f48 337 hammer2_pfsmount_t *pmp;
476d2aad 338 struct vnode *vp;
a0ed3c24 339 ccms_state_t ostate;
7cfa8da5 340
e4e20f48
MD
341 pmp = ip->pmp;
342 KKASSERT(pmp != NULL);
7cfa8da5 343 *errorp = 0;
278ab2b2 344
bca9f8e6 345 ripdata = &hammer2_cluster_rdata(cparent)->ipdata;
7cfa8da5
MD
346
347 for (;;) {
348 /*
349 * Attempt to reuse an existing vnode assignment. It is
350 * possible to race a reclaim so the vget() may fail. The
351 * inode must be unlocked during the vget() to avoid a
352 * deadlock against a reclaim.
353 */
354 vp = ip->vp;
355 if (vp) {
7cfa8da5
MD
356 /*
357 * Inode must be unlocked during the vget() to avoid
476d2aad
MD
358 * possible deadlocks, but leave the ip ref intact.
359 *
360 * vnode is held to prevent destruction during the
361 * vget(). The vget() can still fail if we lost
362 * a reclaim race on the vnode.
7cfa8da5 363 */
ee173d09 364 vhold(vp);
476d2aad 365 ostate = hammer2_inode_lock_temp_release(ip);
7cfa8da5
MD
366 if (vget(vp, LK_EXCLUSIVE)) {
367 vdrop(vp);
9797e933 368 hammer2_inode_lock_temp_restore(ip, ostate);
7cfa8da5
MD
369 continue;
370 }
9797e933 371 hammer2_inode_lock_temp_restore(ip, ostate);
7cfa8da5
MD
372 vdrop(vp);
373 /* vp still locked and ref from vget */
3fc4c63d
MD
374 if (ip->vp != vp) {
375 kprintf("hammer2: igetv race %p/%p\n",
376 ip->vp, vp);
377 vput(vp);
378 continue;
379 }
7cfa8da5
MD
380 *errorp = 0;
381 break;
382 }
383
384 /*
385 * No vnode exists, allocate a new vnode. Beware of
386 * allocation races. This function will return an
387 * exclusively locked and referenced vnode.
388 */
e4e20f48 389 *errorp = getnewvnode(VT_HAMMER2, pmp->mp, &vp, 0, 0);
7cfa8da5 390 if (*errorp) {
3fc4c63d
MD
391 kprintf("hammer2: igetv getnewvnode failed %d\n",
392 *errorp);
7cfa8da5
MD
393 vp = NULL;
394 break;
395 }
396
397 /*
398 * Lock the inode and check for an allocation race.
399 */
476d2aad 400 ostate = hammer2_inode_lock_upgrade(ip);
7cfa8da5
MD
401 if (ip->vp != NULL) {
402 vp->v_type = VBAD;
403 vx_put(vp);
9797e933 404 hammer2_inode_lock_downgrade(ip, ostate);
7cfa8da5
MD
405 continue;
406 }
407
bca9f8e6 408 switch (ripdata->type) {
df9ea374 409 case HAMMER2_OBJTYPE_DIRECTORY:
7cfa8da5
MD
410 vp->v_type = VDIR;
411 break;
df9ea374 412 case HAMMER2_OBJTYPE_REGFILE:
7cfa8da5 413 vp->v_type = VREG;
bca9f8e6 414 vinitvmio(vp, ripdata->size,
6ba3b984 415 HAMMER2_LBUFSIZE,
bca9f8e6 416 (int)ripdata->size & HAMMER2_LBUFMASK);
7cfa8da5 417 break;
4e2004ea
MD
418 case HAMMER2_OBJTYPE_SOFTLINK:
419 /*
420 * XXX for now we are using the generic file_read
421 * and file_write code so we need a buffer cache
422 * association.
423 */
424 vp->v_type = VLNK;
bca9f8e6 425 vinitvmio(vp, ripdata->size,
6ba3b984 426 HAMMER2_LBUFSIZE,
bca9f8e6 427 (int)ripdata->size & HAMMER2_LBUFMASK);
4e2004ea 428 break;
41c34a6d
MD
429 case HAMMER2_OBJTYPE_CDEV:
430 vp->v_type = VCHR;
431 /* fall through */
432 case HAMMER2_OBJTYPE_BDEV:
433 vp->v_ops = &pmp->mp->mnt_vn_spec_ops;
bca9f8e6 434 if (ripdata->type != HAMMER2_OBJTYPE_CDEV)
41c34a6d 435 vp->v_type = VBLK;
bca9f8e6 436 addaliasu(vp, ripdata->rmajor, ripdata->rminor);
41c34a6d
MD
437 break;
438 case HAMMER2_OBJTYPE_FIFO:
439 vp->v_type = VFIFO;
440 vp->v_ops = &pmp->mp->mnt_vn_fifo_ops;
441 break;
7cfa8da5 442 default:
bca9f8e6 443 panic("hammer2: unhandled objtype %d", ripdata->type);
7cfa8da5
MD
444 break;
445 }
446
e4e20f48 447 if (ip == pmp->iroot)
7cfa8da5
MD
448 vsetflags(vp, VROOT);
449
450 vp->v_data = ip;
451 ip->vp = vp;
476d2aad 452 hammer2_inode_ref(ip); /* vp association */
9797e933 453 hammer2_inode_lock_downgrade(ip, ostate);
7cfa8da5
MD
454 break;
455 }
456
457 /*
458 * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
459 */
37aa19df 460 if (hammer2_debug & 0x0002) {
ee173d09
SW
461 kprintf("igetv vp %p refs 0x%08x aux 0x%08x\n",
462 vp, vp->v_refcnt, vp->v_auxrefs);
37aa19df 463 }
7cfa8da5
MD
464 return (vp);
465}
466
476d2aad 467/*
278ab2b2
MD
468 * Returns the inode associated with the passed-in cluster, creating the
469 * inode if necessary and synchronizing it to the passed-in cluster otherwise.
470 *
471 * The passed-in chain must be locked and will remain locked on return.
472 * The returned inode will be locked and the caller may dispose of both
473 * via hammer2_inode_unlock_ex(). However, if the caller needs to resolve
474 * a hardlink it must ref/unlock/relock/drop the inode.
10252dc7
MD
475 *
476 * The hammer2_inode structure regulates the interface between the high level
477 * kernel VNOPS API and the filesystem backend (the chains).
476d2aad
MD
478 */
479hammer2_inode_t *
a5913bdf 480hammer2_inode_get(hammer2_pfsmount_t *pmp, hammer2_inode_t *dip,
278ab2b2 481 hammer2_cluster_t *cluster)
476d2aad 482{
476d2aad 483 hammer2_inode_t *nip;
6a5f4fe6
MD
484 const hammer2_inode_data_t *iptmp;
485 const hammer2_inode_data_t *nipdata;
476d2aad 486
278ab2b2 487 KKASSERT(hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE);
50456506 488 KKASSERT(pmp);
10252dc7 489
9797e933
MD
490 /*
491 * Interlocked lookup/ref of the inode. This code is only needed
492 * when looking up inodes with nlinks != 0 (TODO: optimize out
493 * otherwise and test for duplicates).
494 */
495again:
496 for (;;) {
bca9f8e6 497 iptmp = &hammer2_cluster_rdata(cluster)->ipdata;
278ab2b2 498 nip = hammer2_inode_lookup(pmp, iptmp->inum);
9797e933
MD
499 if (nip == NULL)
500 break;
278ab2b2 501
9797e933 502 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
50456506
MD
503
504 /*
505 * Handle SMP race (not applicable to the super-root spmp
506 * which can't index inodes due to duplicative inode numbers).
507 */
508 if (pmp->spmp_hmp == NULL &&
509 (nip->flags & HAMMER2_INODE_ONRBTREE) == 0) {
9797e933
MD
510 ccms_thread_unlock(&nip->topo_cst);
511 hammer2_inode_drop(nip);
512 continue;
513 }
278ab2b2 514 hammer2_inode_repoint(nip, NULL, cluster);
9797e933
MD
515 return nip;
516 }
10252dc7 517
9797e933
MD
518 /*
519 * We couldn't find the inode number, create a new inode.
520 */
50456506
MD
521 nip = kmalloc(sizeof(*nip), pmp->minode, M_WAITOK | M_ZERO);
522 atomic_add_long(&pmp->inmem_inodes, 1);
523 hammer2_pfs_memory_inc(pmp);
524 hammer2_pfs_memory_wakeup(pmp);
525 if (pmp->spmp_hmp)
99da41ea 526 nip->flags = HAMMER2_INODE_SROOT;
278ab2b2
MD
527
528 /*
529 * Initialize nip's cluster
530 */
531 nip->cluster.refs = 1;
84e47819
MD
532 nip->cluster.pmp = pmp;
533 nip->cluster.flags |= HAMMER2_CLUSTER_INODE;
534 hammer2_cluster_replace(&nip->cluster, cluster);
278ab2b2 535
bca9f8e6 536 nipdata = &hammer2_cluster_rdata(cluster)->ipdata;
278ab2b2
MD
537 nip->inum = nipdata->inum;
538 nip->size = nipdata->size;
539 nip->mtime = nipdata->mtime;
540 hammer2_inode_repoint(nip, NULL, cluster);
10252dc7
MD
541 nip->pip = dip; /* can be NULL */
542 if (dip)
543 hammer2_inode_ref(dip); /* ref dip for nip->pip */
544
545 nip->pmp = pmp;
10252dc7
MD
546
547 /*
548 * ref and lock on nip gives it state compatible to after a
549 * hammer2_inode_lock_ex() call.
550 */
551 nip->refs = 1;
278ab2b2 552 ccms_cst_init(&nip->topo_cst, &nip->cluster);
476d2aad
MD
553 ccms_thread_lock(&nip->topo_cst, CCMS_STATE_EXCLUSIVE);
554 /* combination of thread lock and chain lock == inode lock */
555
9797e933
MD
556 /*
557 * Attempt to add the inode. If it fails we raced another inode
558 * get. Undo all the work and try again.
559 */
50456506 560 if (pmp->spmp_hmp == NULL) {
9797e933
MD
561 spin_lock(&pmp->inum_spin);
562 if (RB_INSERT(hammer2_inode_tree, &pmp->inum_tree, nip)) {
563 spin_unlock(&pmp->inum_spin);
564 ccms_thread_unlock(&nip->topo_cst);
565 hammer2_inode_drop(nip);
566 goto again;
567 }
568 atomic_set_int(&nip->flags, HAMMER2_INODE_ONRBTREE);
569 spin_unlock(&pmp->inum_spin);
570 }
571
476d2aad
MD
572 return (nip);
573}
574
7cfa8da5 575/*
37494cab
MD
576 * Create a new inode in the specified directory using the vattr to
577 * figure out the type of inode.
7cfa8da5 578 *
278ab2b2 579 * If no error occurs the new inode with its cluster locked is returned in
db71f61f 580 * *nipp, otherwise an error is returned and *nipp is set to NULL.
ae183399
MD
581 *
582 * If vap and/or cred are NULL the related fields are not set and the
583 * inode type defaults to a directory. This is used when creating PFSs
584 * under the super-root, so the inode number is set to 1 in this case.
476d2aad
MD
585 *
586 * dip is not locked on entry.
50456506
MD
587 *
588 * NOTE: When used to create a snapshot, the inode is temporarily associated
589 * with the super-root spmp. XXX should pass new pmp for snapshot.
7cfa8da5 590 */
0dea3156
MD
591hammer2_inode_t *
592hammer2_inode_create(hammer2_trans_t *trans, hammer2_inode_t *dip,
37494cab 593 struct vattr *vap, struct ucred *cred,
37494cab 594 const uint8_t *name, size_t name_len,
278ab2b2 595 hammer2_cluster_t **clusterp, int *errorp)
7cfa8da5 596{
6a5f4fe6 597 const hammer2_inode_data_t *dipdata;
476d2aad 598 hammer2_inode_data_t *nipdata;
278ab2b2
MD
599 hammer2_cluster_t *cluster;
600 hammer2_cluster_t *cparent;
37494cab 601 hammer2_inode_t *nip;
1897c66e 602 hammer2_key_t key_dummy;
37494cab
MD
603 hammer2_key_t lhc;
604 int error;
b2b78aaa 605 uid_t xuid;
476d2aad
MD
606 uuid_t dip_uid;
607 uuid_t dip_gid;
608 uint32_t dip_mode;
e07becf8
MD
609 uint8_t dip_comp_algo;
610 uint8_t dip_check_algo;
278ab2b2 611 int ddflag;
7cfa8da5 612
37494cab 613 lhc = hammer2_dirhash(name, name_len);
0dea3156 614 *errorp = 0;
7cfa8da5 615
37494cab
MD
616 /*
617 * Locate the inode or indirect block to create the new
618 * entry in. At the same time check for key collisions
619 * and iterate until we don't get one.
9797e933
MD
620 *
621 * NOTE: hidden inodes do not have iterators.
37494cab 622 */
5f6853df 623retry:
278ab2b2 624 cparent = hammer2_inode_lock_ex(dip);
bca9f8e6 625 dipdata = &hammer2_cluster_rdata(cparent)->ipdata;
0dea3156
MD
626 dip_uid = dipdata->uid;
627 dip_gid = dipdata->gid;
628 dip_mode = dipdata->mode;
e07becf8
MD
629 dip_comp_algo = dipdata->comp_algo;
630 dip_check_algo = dipdata->check_algo;
7cfa8da5 631
37494cab
MD
632 error = 0;
633 while (error == 0) {
278ab2b2
MD
634 cluster = hammer2_cluster_lookup(cparent, &key_dummy,
635 lhc, lhc, 0, &ddflag);
636 if (cluster == NULL)
37494cab 637 break;
92a2bc79
MD
638 if ((lhc & HAMMER2_DIRHASH_VISIBLE) == 0)
639 error = ENOSPC;
37494cab
MD
640 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
641 error = ENOSPC;
278ab2b2
MD
642 hammer2_cluster_unlock(cluster);
643 cluster = NULL;
37494cab
MD
644 ++lhc;
645 }
1e5c08ba
MD
646
647 if (error == 0) {
278ab2b2 648 error = hammer2_cluster_create(trans, cparent, &cluster,
0dea3156 649 lhc, 0,
37494cab 650 HAMMER2_BREF_TYPE_INODE,
b3659de2
MD
651 HAMMER2_INODE_BYTES,
652 0);
1e5c08ba 653 }
8138a154
MD
654#if INODE_DEBUG
655 kprintf("CREATE INODE %*.*s chain=%p\n",
278ab2b2
MD
656 (int)name_len, (int)name_len, name,
657 (cluster ? cluster->focus : NULL));
8138a154 658#endif
7cfa8da5 659
37494cab 660 /*
0dea3156 661 * Cleanup and handle retries.
37494cab 662 */
0dea3156 663 if (error == EAGAIN) {
278ab2b2
MD
664 hammer2_cluster_ref(cparent);
665 hammer2_inode_unlock_ex(dip, cparent);
666 hammer2_cluster_wait(cparent);
667 hammer2_cluster_drop(cparent);
0dea3156
MD
668 goto retry;
669 }
278ab2b2
MD
670 hammer2_inode_unlock_ex(dip, cparent);
671 cparent = NULL;
0dea3156 672
37494cab 673 if (error) {
278ab2b2 674 KKASSERT(cluster == NULL);
0dea3156
MD
675 *errorp = error;
676 return (NULL);
37494cab 677 }
7cfa8da5 678
37494cab 679 /*
476d2aad
MD
680 * Set up the new inode.
681 *
682 * NOTE: *_get() integrates chain's lock into the inode lock.
0dea3156
MD
683 *
684 * NOTE: Only one new inode can currently be created per
685 * transaction. If the need arises we can adjust
686 * hammer2_trans_init() to allow more.
1e5c08ba
MD
687 *
688 * NOTE: nipdata will have chain's blockset data.
37494cab 689 */
6a5f4fe6
MD
690 KKASSERT(cluster->focus->flags & HAMMER2_CHAIN_MODIFIED);
691 nipdata = &hammer2_cluster_wdata(cluster)->ipdata;
278ab2b2 692 nipdata->inum = trans->inode_tid;
03188ed8 693 hammer2_cluster_modsync(cluster);
278ab2b2 694 nip = hammer2_inode_get(dip->pmp, dip, cluster);
6a5f4fe6 695 nipdata = &hammer2_cluster_wdata(cluster)->ipdata;
7cfa8da5 696
ae183399 697 if (vap) {
0dea3156 698 KKASSERT(trans->inodes_created == 0);
476d2aad 699 nipdata->type = hammer2_get_obj_type(vap->va_type);
a4dc31e0 700 nipdata->inum = trans->inode_tid;
0dea3156 701 ++trans->inodes_created;
41c34a6d
MD
702
703 switch (nipdata->type) {
704 case HAMMER2_OBJTYPE_CDEV:
705 case HAMMER2_OBJTYPE_BDEV:
706 nipdata->rmajor = vap->va_rmajor;
707 nipdata->rminor = vap->va_rminor;
708 break;
709 default:
710 break;
711 }
ae183399 712 } else {
476d2aad
MD
713 nipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
714 nipdata->inum = 1;
ae183399 715 }
355d67fc
MD
716
717 /* Inherit parent's inode compression mode. */
1e5c08ba 718 nip->comp_heuristic = 0;
e07becf8
MD
719 nipdata->comp_algo = dip_comp_algo;
720 nipdata->check_algo = dip_check_algo;
476d2aad
MD
721 nipdata->version = HAMMER2_INODE_VERSION_ONE;
722 hammer2_update_time(&nipdata->ctime);
723 nipdata->mtime = nipdata->ctime;
ae183399 724 if (vap)
476d2aad
MD
725 nipdata->mode = vap->va_mode;
726 nipdata->nlinks = 1;
b2b78aaa 727 if (vap) {
9b6b3df4 728 if (dip && dip->pmp) {
476d2aad 729 xuid = hammer2_to_unix_xid(&dip_uid);
b2b78aaa 730 xuid = vop_helper_create_uid(dip->pmp->mp,
476d2aad 731 dip_mode,
b2b78aaa
MD
732 xuid,
733 cred,
734 &vap->va_mode);
735 } else {
9b6b3df4 736 /* super-root has no dip and/or pmp */
b2b78aaa
MD
737 xuid = 0;
738 }
739 if (vap->va_vaflags & VA_UID_UUID_VALID)
476d2aad 740 nipdata->uid = vap->va_uid_uuid;
b2b78aaa 741 else if (vap->va_uid != (uid_t)VNOVAL)
476d2aad 742 hammer2_guid_to_uuid(&nipdata->uid, vap->va_uid);
b2b78aaa 743 else
476d2aad 744 hammer2_guid_to_uuid(&nipdata->uid, xuid);
b2b78aaa
MD
745
746 if (vap->va_vaflags & VA_GID_UUID_VALID)
476d2aad 747 nipdata->gid = vap->va_gid_uuid;
b2b78aaa 748 else if (vap->va_gid != (gid_t)VNOVAL)
476d2aad 749 hammer2_guid_to_uuid(&nipdata->gid, vap->va_gid);
b2b78aaa 750 else if (dip)
476d2aad 751 nipdata->gid = dip_gid;
b2b78aaa 752 }
232a50f9 753
3ac6a319
MD
754 /*
755 * Regular files and softlinks allow a small amount of data to be
756 * directly embedded in the inode. This flag will be cleared if
757 * the size is extended past the embedded limit.
758 */
476d2aad
MD
759 if (nipdata->type == HAMMER2_OBJTYPE_REGFILE ||
760 nipdata->type == HAMMER2_OBJTYPE_SOFTLINK) {
761 nipdata->op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
3ac6a319
MD
762 }
763
37494cab 764 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
476d2aad
MD
765 bcopy(name, nipdata->filename, name_len);
766 nipdata->name_key = lhc;
767 nipdata->name_len = name_len;
03188ed8 768 hammer2_cluster_modsync(cluster);
278ab2b2 769 *clusterp = cluster;
7cfa8da5 770
0dea3156 771 return (nip);
7cfa8da5 772}
6934ae32 773
9797e933 774/*
da6f36f4
MD
775 * The cluster has been removed from the original directory and replaced
776 * with a hardlink pointer. Move the cluster to the specified parent
777 * directory, change the filename to "0xINODENUMBER", and adjust the key.
778 * The cluster becomes our invisible hardlink target.
9797e933 779 *
da6f36f4 780 * The original cluster must be deleted on entry.
99535653 781 */
9797e933 782static
0924b3f8 783void
278ab2b2
MD
784hammer2_hardlink_shiftup(hammer2_trans_t *trans, hammer2_cluster_t *cluster,
785 hammer2_inode_t *dip, hammer2_cluster_t *dcluster,
9b21452a 786 int nlinks, int *errorp)
99535653 787{
6a5f4fe6 788 const hammer2_inode_data_t *iptmp;
476d2aad 789 hammer2_inode_data_t *nipdata;
278ab2b2 790 hammer2_cluster_t *xcluster;
1897c66e 791 hammer2_key_t key_dummy;
99535653 792 hammer2_key_t lhc;
ea155208 793 hammer2_blockref_t bref;
278ab2b2 794 int ddflag;
99535653 795
bca9f8e6 796 iptmp = &hammer2_cluster_rdata(cluster)->ipdata;
278ab2b2 797 lhc = iptmp->inum;
51bf8e9b 798 KKASSERT((lhc & HAMMER2_DIRHASH_VISIBLE) == 0);
99535653
MD
799
800 /*
801 * Locate the inode or indirect block to create the new
ea155208
MD
802 * entry in. lhc represents the inode number so there is
803 * no collision iteration.
51bf8e9b
MD
804 *
805 * There should be no key collisions with invisible inode keys.
294fa5f9
MD
806 *
807 * WARNING! Must use inode_lock_ex() on dip to handle a stale
278ab2b2 808 * dip->cluster cache.
99535653 809 */
0924b3f8 810 *errorp = 0;
278ab2b2
MD
811 xcluster = hammer2_cluster_lookup(dcluster, &key_dummy,
812 lhc, lhc, 0, &ddflag);
813 if (xcluster) {
9b21452a 814 kprintf("X3 chain %p dip %p dchain %p dip->chain %p\n",
278ab2b2
MD
815 xcluster->focus, dip, dcluster->focus,
816 dip->cluster.focus);
817 hammer2_cluster_unlock(xcluster);
818 xcluster = NULL;
0dea3156 819 *errorp = ENOSPC;
a4dc31e0 820#if 0
9797e933
MD
821 Debugger("X3");
822#endif
99535653
MD
823 }
824
99535653
MD
825 /*
826 * Handle the error case
827 */
0dea3156 828 if (*errorp) {
0924b3f8 829 panic("error2");
278ab2b2 830 KKASSERT(xcluster == NULL);
0924b3f8 831 return;
99535653 832 }
99535653 833
28ee5f14 834 /*
278ab2b2
MD
835 * Use xcluster as a placeholder for (lhc). Duplicate cluster to the
836 * same target bref as xcluster and then delete xcluster. The
837 * duplication occurs after xcluster in flush order even though
838 * xcluster is deleted after the duplication. XXX
28ee5f14 839 *
0924b3f8 840 * WARNING! Duplications (to a different parent) can cause indirect
278ab2b2 841 * blocks to be inserted, refactor xcluster.
84e47819
MD
842 *
843 * WARNING! Only key and keybits is extracted from a passed-in bref.
28ee5f14 844 */
278ab2b2 845 hammer2_cluster_bref(cluster, &bref);
9797e933 846 bref.key = lhc; /* invisible dir entry key */
ea155208 847 bref.keybits = 0;
b3659de2 848 hammer2_cluster_rename(trans, &bref, dcluster, cluster, 0);
51bf8e9b
MD
849
850 /*
278ab2b2 851 * cluster is now 'live' again.. adjust the filename.
9797e933 852 *
51bf8e9b
MD
853 * Directory entries are inodes but this is a hidden hardlink
854 * target. The name isn't used but to ease debugging give it
855 * a name after its inode number.
856 */
278ab2b2 857 hammer2_cluster_modify(trans, cluster, 0);
6a5f4fe6 858 nipdata = &hammer2_cluster_wdata(cluster)->ipdata;
51bf8e9b
MD
859 ksnprintf(nipdata->filename, sizeof(nipdata->filename),
860 "0x%016jx", (intmax_t)nipdata->inum);
861 nipdata->name_len = strlen(nipdata->filename);
862 nipdata->name_key = lhc;
0924b3f8 863 nipdata->nlinks += nlinks;
6a5f4fe6 864 hammer2_cluster_modsync(cluster);
99535653
MD
865}
866
6934ae32 867/*
da6f36f4 868 * Connect the target inode represented by (cluster) to the media topology
59c5121a
MD
869 * at (dip, name, len). The caller can pass a rough *chainp, this function
870 * will issue lookup()s to position the parent chain properly for the
871 * chain insertion.
0dea3156 872 *
9797e933 873 * If hlink is TRUE this function creates an OBJTYPE_HARDLINK directory
da6f36f4 874 * entry instead of connecting (cluster).
d5fabb70 875 *
da6f36f4 876 * If hlink is FALSE this function expects (cluster) to be unparented.
6934ae32
MD
877 */
878int
9b21452a 879hammer2_inode_connect(hammer2_trans_t *trans,
278ab2b2
MD
880 hammer2_cluster_t **clusterp, int hlink,
881 hammer2_inode_t *dip, hammer2_cluster_t *dcluster,
044541cd
MD
882 const uint8_t *name, size_t name_len,
883 hammer2_key_t lhc)
6934ae32 884{
6a5f4fe6 885 hammer2_inode_data_t *wipdata;
278ab2b2
MD
886 hammer2_cluster_t *ocluster;
887 hammer2_cluster_t *ncluster;
1897c66e 888 hammer2_key_t key_dummy;
278ab2b2 889 int ddflag;
6934ae32
MD
890 int error;
891
f3843dc2 892 /*
278ab2b2
MD
893 * Since ocluster is either disconnected from the topology or
894 * represents a hardlink terminus which is always a parent of or
895 * equal to dip, we should be able to safely lock dip->chain for
896 * our setup.
294fa5f9
MD
897 *
898 * WARNING! Must use inode_lock_ex() on dip to handle a stale
278ab2b2 899 * dip->cluster.
da6f36f4 900 *
044541cd
MD
901 * If name is non-NULL we calculate lhc, else we use the passed-in
902 * lhc.
6934ae32 903 */
da6f36f4
MD
904 ocluster = *clusterp;
905
044541cd
MD
906 if (name) {
907 lhc = hammer2_dirhash(name, name_len);
908
909 /*
910 * Locate the inode or indirect block to create the new
911 * entry in. At the same time check for key collisions
912 * and iterate until we don't get one.
913 */
914 error = 0;
915 while (error == 0) {
278ab2b2 916 ncluster = hammer2_cluster_lookup(dcluster, &key_dummy,
044541cd 917 lhc, lhc,
278ab2b2
MD
918 0, &ddflag);
919 if (ncluster == NULL)
044541cd
MD
920 break;
921 if ((lhc & HAMMER2_DIRHASH_LOMASK) ==
922 HAMMER2_DIRHASH_LOMASK) {
923 error = ENOSPC;
924 }
278ab2b2
MD
925 hammer2_cluster_unlock(ncluster);
926 ncluster = NULL;
044541cd
MD
927 ++lhc;
928 }
59c5121a
MD
929 } else {
930 /*
931 * Reconnect to specific key (used when moving
932 * unlinked-but-open files into the hidden directory).
933 */
278ab2b2
MD
934 ncluster = hammer2_cluster_lookup(dcluster, &key_dummy,
935 lhc, lhc,
936 0, &ddflag);
937 KKASSERT(ncluster == NULL);
6934ae32
MD
938 }
939
6934ae32 940 if (error == 0) {
99535653 941 if (hlink) {
0dea3156
MD
942 /*
943 * Hardlink pointer needed, create totally fresh
944 * directory entry.
0924b3f8 945 *
278ab2b2
MD
946 * We must refactor ocluster because it might have
947 * been shifted into an indirect cluster by the
0924b3f8 948 * create.
0dea3156 949 */
278ab2b2
MD
950 KKASSERT(ncluster == NULL);
951 error = hammer2_cluster_create(trans,
952 dcluster, &ncluster,
953 lhc, 0,
954 HAMMER2_BREF_TYPE_INODE,
b3659de2
MD
955 HAMMER2_INODE_BYTES,
956 0);
99535653 957 } else {
51bf8e9b 958 /*
da6f36f4
MD
959 * Reconnect the original cluster under the new name.
960 * Original cluster must have already been deleted by
961 * teh caller.
0924b3f8 962 *
278ab2b2 963 * WARNING! Can cause held-over clusters to require a
0924b3f8 964 * refactor. Fortunately we have none (our
278ab2b2 965 * locked clusters are passed into and
0924b3f8 966 * modified by the call).
51bf8e9b 967 */
278ab2b2
MD
968 ncluster = ocluster;
969 ocluster = NULL;
278ab2b2
MD
970 error = hammer2_cluster_create(trans,
971 dcluster, &ncluster,
972 lhc, 0,
973 HAMMER2_BREF_TYPE_INODE,
b3659de2
MD
974 HAMMER2_INODE_BYTES,
975 0);
99535653 976 }
6934ae32 977 }
476d2aad
MD
978
979 /*
0dea3156 980 * Unlock stuff.
476d2aad 981 */
0dea3156 982 KKASSERT(error != EAGAIN);
6934ae32
MD
983
984 /*
278ab2b2
MD
985 * ncluster should be NULL on error, leave ocluster
986 * (ocluster == *clusterp) alone.
6934ae32
MD
987 */
988 if (error) {
278ab2b2 989 KKASSERT(ncluster == NULL);
6934ae32
MD
990 return (error);
991 }
992
993 /*
994 * Directory entries are inodes so if the name has changed we have
995 * to update the inode.
99535653
MD
996 *
997 * When creating an OBJTYPE_HARDLINK entry remember to unlock the
278ab2b2 998 * cluster, the caller will access the hardlink via the actual hardlink
0dea3156 999 * target file and not the hardlink pointer entry, so we must still
278ab2b2 1000 * return ocluster.
6934ae32 1001 */
004f88b4 1002 if (hlink && hammer2_hardlink_enable >= 0) {
92a2bc79
MD
1003 /*
1004 * Create the HARDLINK pointer. oip represents the hardlink
1005 * target in this situation.
d5fabb70 1006 *
278ab2b2 1007 * We will return ocluster (the hardlink target).
92a2bc79 1008 */
278ab2b2 1009 hammer2_cluster_modify(trans, ncluster, 0);
6934ae32 1010 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
6a5f4fe6
MD
1011 wipdata = &hammer2_cluster_wdata(ncluster)->ipdata;
1012 bcopy(name, wipdata->filename, name_len);
1013 wipdata->name_key = lhc;
1014 wipdata->name_len = name_len;
1015 wipdata->target_type =
bca9f8e6 1016 hammer2_cluster_rdata(ocluster)->ipdata.type;
6a5f4fe6 1017 wipdata->type = HAMMER2_OBJTYPE_HARDLINK;
bca9f8e6 1018 wipdata->inum = hammer2_cluster_rdata(ocluster)->ipdata.inum;
a71db85d 1019 wipdata->version = HAMMER2_INODE_VERSION_ONE;
6a5f4fe6 1020 wipdata->nlinks = 1;
a71db85d 1021 wipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA;
6a5f4fe6 1022 hammer2_cluster_modsync(ncluster);
278ab2b2
MD
1023 hammer2_cluster_unlock(ncluster);
1024 ncluster = ocluster;
1025 ocluster = NULL;
99535653 1026 } else {
92a2bc79 1027 /*
278ab2b2 1028 * ncluster is a duplicate of ocluster at the new location.
da6f36f4
MD
1029 * We must fixup the name stored in the inode data.
1030 * The bref key has already been adjusted by inode_connect().
92a2bc79 1031 */
278ab2b2 1032 hammer2_cluster_modify(trans, ncluster, 0);
6a5f4fe6 1033 wipdata = &hammer2_cluster_wdata(ncluster)->ipdata;
476d2aad 1034
9797e933 1035 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
6a5f4fe6
MD
1036 bcopy(name, wipdata->filename, name_len);
1037 wipdata->name_key = lhc;
1038 wipdata->name_len = name_len;
1039 wipdata->nlinks = 1;
1040 hammer2_cluster_modsync(ncluster);
6934ae32 1041 }
d5fabb70
MD
1042
1043 /*
278ab2b2
MD
1044 * We are replacing ocluster with ncluster, unlock ocluster. In the
1045 * case where ocluster is left unchanged the code above sets
1046 * ncluster to ocluster and ocluster to NULL, resulting in a NOP here.
d5fabb70 1047 */
278ab2b2
MD
1048 if (ocluster)
1049 hammer2_cluster_unlock(ocluster);
1050 *clusterp = ncluster;
0dea3156 1051
d5fabb70
MD
1052 return (0);
1053}
1054
1055/*
84e47819
MD
1056 * Repoint ip->cluster's chains to cluster's chains. Caller must hold
1057 * the inode exclusively locked. cluster may be NULL to clean out any
1058 * chains in ip->cluster.
d5fabb70
MD
1059 */
1060void
ea155208 1061hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
278ab2b2 1062 hammer2_cluster_t *cluster)
d5fabb70 1063{
d5fabb70 1064 hammer2_chain_t *ochain;
278ab2b2 1065 hammer2_chain_t *nchain;
ea155208 1066 hammer2_inode_t *opip;
278ab2b2 1067 int i;
ea155208 1068
84e47819
MD
1069 /*
1070 * Replace chains in ip->cluster with chains from cluster and
1071 * adjust the focus if necessary.
1072 *
1073 * NOTE: nchain and/or ochain can be NULL due to gaps
1074 * in the cluster arrays.
1075 */
1076 ip->cluster.focus = NULL;
1077 for (i = 0; cluster && i < cluster->nchains; ++i) {
278ab2b2
MD
1078 nchain = cluster->array[i];
1079 if (i < ip->cluster.nchains) {
1080 ochain = ip->cluster.array[i];
58e43599
MD
1081 if (ochain == nchain) {
1082 if (ip->cluster.focus == NULL)
1083 ip->cluster.focus = nchain;
278ab2b2 1084 continue;
58e43599 1085 }
278ab2b2
MD
1086 } else {
1087 ochain = NULL;
1088 }
1089
1090 /*
84e47819 1091 * Make adjustments
278ab2b2
MD
1092 */
1093 ip->cluster.array[i] = nchain;
84e47819
MD
1094 if (ip->cluster.focus == NULL)
1095 ip->cluster.focus = nchain;
278ab2b2
MD
1096 if (nchain)
1097 hammer2_chain_ref(nchain);
1098 if (ochain)
1099 hammer2_chain_drop(ochain);
1100 }
84e47819
MD
1101
1102 /*
1103 * Release any left-over chains in ip->cluster.
1104 */
1105 while (i < ip->cluster.nchains) {
1106 nchain = ip->cluster.array[i];
1107 if (nchain) {
1108 ip->cluster.array[i] = NULL;
1109 hammer2_chain_drop(nchain);
1110 }
1111 ++i;
1112 }
1113 ip->cluster.nchains = cluster ? cluster->nchains : 0;
d5fabb70 1114
0dea3156 1115 /*
cd189b1e 1116 * Repoint ip->pip if requested (non-NULL pip).
0dea3156 1117 */
cd189b1e 1118 if (pip && ip->pip != pip) {
ea155208 1119 opip = ip->pip;
cd189b1e 1120 hammer2_inode_ref(pip);
ea155208
MD
1121 ip->pip = pip;
1122 if (opip)
1123 hammer2_inode_drop(opip);
0dea3156 1124 }
6934ae32 1125}
db0c2eb3 1126
ae183399
MD
1127/*
1128 * Unlink the file from the specified directory inode. The directory inode
731b2a84 1129 * does not need to be locked.
ae183399
MD
1130 *
1131 * isdir determines whether a directory/non-directory check should be made.
1132 * No check is made if isdir is set to -1.
5f6853df 1133 *
044541cd
MD
1134 * isopen specifies whether special unlink-with-open-descriptor handling
1135 * must be performed. If set to -1 the caller is deleting a PFS and we
1136 * check whether the chain is mounted or not (chain->pmp != NULL). 1 is
1137 * implied if it is mounted.
1138 *
1139 * If isopen is 1 and nlinks drops to 0 this function must move the chain
1140 * to a special hidden directory until last-close occurs on the file.
1141 *
91abd410
MD
1142 * NOTE! The underlying file can still be active with open descriptors
1143 * or if the chain is being manually held (e.g. for rename).
1144 *
1145 * The caller is responsible for fixing up ip->chain if e.g. a
1146 * rename occurs (see chain_duplicate()).
da6f36f4
MD
1147 *
1148 * NOTE! The chain is not deleted if it is moved to the hidden directory,
1149 * but otherwise will be deleted.
ae183399
MD
1150 */
1151int
0dea3156 1152hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip,
9797e933 1153 const uint8_t *name, size_t name_len,
da6f36f4
MD
1154 int isdir, int *hlinkp, struct nchandle *nch,
1155 int nlinks)
ae183399 1156{
6a5f4fe6
MD
1157 const hammer2_inode_data_t *ripdata;
1158 hammer2_inode_data_t *wipdata;
278ab2b2 1159 hammer2_cluster_t *cparent;
da6f36f4
MD
1160 hammer2_cluster_t *hcluster;
1161 hammer2_cluster_t *hparent;
278ab2b2
MD
1162 hammer2_cluster_t *cluster;
1163 hammer2_cluster_t *dparent;
1164 hammer2_cluster_t *dcluster;
1897c66e
MD
1165 hammer2_key_t key_dummy;
1166 hammer2_key_t key_next;
ae183399
MD
1167 hammer2_key_t lhc;
1168 int error;
278ab2b2 1169 int ddflag;
05dd26e4 1170 int hlink;
99535653 1171 uint8_t type;
ae183399
MD
1172
1173 error = 0;
05dd26e4 1174 hlink = 0;
da6f36f4
MD
1175 hcluster = NULL;
1176 hparent = NULL;
ae183399
MD
1177 lhc = hammer2_dirhash(name, name_len);
1178
da6f36f4 1179again:
ae183399
MD
1180 /*
1181 * Search for the filename in the directory
1182 */
278ab2b2
MD
1183 cparent = hammer2_inode_lock_ex(dip);
1184 cluster = hammer2_cluster_lookup(cparent, &key_next,
ae183399 1185 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
278ab2b2
MD
1186 0, &ddflag);
1187 while (cluster) {
1188 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE) {
bca9f8e6 1189 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
6a5f4fe6
MD
1190 if (ripdata->name_len == name_len &&
1191 bcmp(ripdata->filename, name, name_len) == 0) {
278ab2b2
MD
1192 break;
1193 }
ae183399 1194 }
278ab2b2
MD
1195 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
1196 key_next,
1197 lhc + HAMMER2_DIRHASH_LOMASK,
1198 0);
ae183399 1199 }
05dd26e4 1200 hammer2_inode_unlock_ex(dip, NULL); /* retain cparent */
ae183399
MD
1201
1202 /*
1203 * Not found or wrong type (isdir < 0 disables the type check).
51bf8e9b 1204 * If a hardlink pointer, type checks use the hardlink target.
ae183399 1205 */
278ab2b2 1206 if (cluster == NULL) {
e2e9e2db
MD
1207 error = ENOENT;
1208 goto done;
ae183399 1209 }
bca9f8e6 1210 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
6a5f4fe6 1211 type = ripdata->type;
278ab2b2 1212 if (type == HAMMER2_OBJTYPE_HARDLINK) {
05dd26e4 1213 hlink = 1;
6a5f4fe6 1214 type = ripdata->target_type;
9797e933 1215 }
99535653
MD
1216
1217 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 0) {
ae183399
MD
1218 error = ENOTDIR;
1219 goto done;
1220 }
a864c5d9 1221 if (type != HAMMER2_OBJTYPE_DIRECTORY && isdir >= 1) {
ae183399
MD
1222 error = EISDIR;
1223 goto done;
1224 }
1225
99535653 1226 /*
9b21452a 1227 * Hardlink must be resolved. We can't hold the parent locked
da6f36f4
MD
1228 * while we do this or we could deadlock. The physical file will
1229 * be located at or above the current directory.
476d2aad 1230 *
da6f36f4 1231 * We loop to reacquire the hardlink origination.
278ab2b2 1232 *
da6f36f4
MD
1233 * NOTE: hammer2_hardlink_find() will locate the hardlink target,
1234 * returning a modified hparent and hcluster.
99535653 1235 */
6a5f4fe6 1236 if (ripdata->type == HAMMER2_OBJTYPE_HARDLINK) {
da6f36f4
MD
1237 if (hcluster == NULL) {
1238 hcluster = cluster;
05dd26e4 1239 cluster = NULL; /* safety */
da6f36f4
MD
1240 hammer2_cluster_unlock(cparent);
1241 cparent = NULL; /* safety */
05dd26e4 1242 ripdata = NULL; /* safety (associated w/cparent) */
da6f36f4 1243 error = hammer2_hardlink_find(dip, &hparent, hcluster);
05dd26e4
MD
1244
1245 /*
1246 * If we couldn't find the hardlink target then some
1247 * parent directory containing the hardlink pointer
1248 * probably got renamed to above the original target,
1249 * a case not yet handled by H2.
1250 */
1251 if (error) {
1252 kprintf("H2 unlink_file: hardlink target for "
1253 "\"%s\" not found\n",
1254 name);
1255 kprintf("(likely due to known directory "
1256 "rename bug)\n");
1257 goto done;
1258 }
da6f36f4
MD
1259 goto again;
1260 }
99535653
MD
1261 }
1262
ae183399
MD
1263 /*
1264 * If this is a directory the directory must be empty. However, if
1265 * isdir < 0 we are doing a rename and the directory does not have
a864c5d9
MD
1266 * to be empty, and if isdir > 1 we are deleting a PFS/snapshot
1267 * and the directory does not have to be empty.
26b047fa
MD
1268 *
1269 * NOTE: We check the full key range here which covers both visible
1270 * and invisible entries. Theoretically there should be no
1271 * invisible (hardlink target) entries if there are no visible
1272 * entries.
ae183399 1273 */
a864c5d9 1274 if (type == HAMMER2_OBJTYPE_DIRECTORY && isdir == 1) {
278ab2b2
MD
1275 dparent = hammer2_cluster_lookup_init(cluster, 0);
1276 dcluster = hammer2_cluster_lookup(dparent, &key_dummy,
1277 0, (hammer2_key_t)-1,
1278 HAMMER2_LOOKUP_NODATA,
1279 &ddflag);
1280 if (dcluster) {
1281 hammer2_cluster_unlock(dcluster);
1282 hammer2_cluster_lookup_done(dparent);
ae183399
MD
1283 error = ENOTEMPTY;
1284 goto done;
1285 }
278ab2b2 1286 hammer2_cluster_lookup_done(dparent);
ae183399 1287 dparent = NULL;
278ab2b2 1288 /* dcluster NULL */
ae183399
MD
1289 }
1290
ae183399 1291 /*
05dd26e4 1292 * If this was a hardlink then (cparent, cluster) is the hardlink
da6f36f4
MD
1293 * pointer, which we can simply destroy outright. Discard the
1294 * clusters and replace with the hardlink target.
ae183399 1295 */
da6f36f4
MD
1296 if (hcluster) {
1297 hammer2_cluster_delete(trans, cparent, cluster,
1298 HAMMER2_DELETE_PERMANENT);
1299 hammer2_cluster_unlock(cparent);
1300 hammer2_cluster_unlock(cluster);
1301 cparent = hparent;
1302 cluster = hcluster;
1303 hparent = NULL;
1304 hcluster = NULL;
044541cd 1305 }
99535653 1306
044541cd 1307 /*
da6f36f4
MD
1308 * This leaves us with the hardlink target or non-hardlinked file
1309 * or directory in (cparent, cluster).
1310 *
1311 * Delete the target when nlinks reaches 0 with special handling
1312 * if (isopen) is set.
044541cd
MD
1313 *
1314 * NOTE! In DragonFly the vnops function calls cache_unlink() after
1315 * calling us here to clean out the namecache association,
1316 * (which does not represent a ref for the open-test), and to
1317 * force finalization of the vnode if/when the last ref gets
1318 * dropped.
9b21452a
MD
1319 *
1320 * NOTE! Files are unlinked by rename and then relinked. nch will be
1321 * passed as NULL in this situation. hammer2_inode_connect()
1322 * will bump nlinks.
044541cd 1323 */
278ab2b2
MD
1324 KKASSERT(cluster != NULL);
1325 hammer2_cluster_modify(trans, cluster, 0);
6a5f4fe6
MD
1326 wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1327 ripdata = wipdata;
da6f36f4 1328 wipdata->nlinks += nlinks;
6a5f4fe6
MD
1329 if ((int64_t)wipdata->nlinks < 0) { /* XXX debugging */
1330 wipdata->nlinks = 0;
1331 }
1332 hammer2_cluster_modsync(cluster);
1333
1334 if (wipdata->nlinks == 0) {
05dd26e4
MD
1335 /*
1336 * Target nlinks has reached 0, file now unlinked (but may
1337 * still be open).
1338 */
18e8ab5f 1339 /* XXX need interlock if mounted
278ab2b2
MD
1340 if ((cluster->focus->flags & HAMMER2_CHAIN_PFSROOT) &&
1341 cluster->pmp) {
044541cd
MD
1342 error = EINVAL;
1343 kprintf("hammer2: PFS \"%s\" cannot be deleted "
1344 "while still mounted\n",
6a5f4fe6 1345 wipdata->filename);
044541cd
MD
1346 goto done;
1347 }
18e8ab5f 1348 */
044541cd 1349 if (nch && cache_isopen(nch)) {
da6f36f4 1350 hammer2_inode_move_to_hidden(trans, &cparent, &cluster,
6a5f4fe6 1351 wipdata->inum);
044541cd 1352 } else {
850687d2
MD
1353 /*
1354 * This won't get everything if a vnode is still
1355 * present, but the cache_unlink() call the caller
1356 * makes will.
1357 */
1358 hammer2_cluster_delete(trans, cparent, cluster,
1359 HAMMER2_DELETE_PERMANENT);
044541cd 1360 }
05dd26e4 1361 } else if (hlink == 0) {
da6f36f4 1362 /*
05dd26e4
MD
1363 * In this situation a normal non-hardlinked file (which can
1364 * only have nlinks == 1) still has a non-zero nlinks, the
1365 * caller must be doing a RENAME operation and so is passing
1366 * a nlinks adjustment of 0, and only wishes to remove file
1367 * in order to be able to reconnect it under a different name.
1368 *
1369 * In this situation we do a non-permanent deletion of the
1370 * chain in order to allow the file to be reconnected in
1371 * a different location.
da6f36f4
MD
1372 */
1373 KKASSERT(nlinks == 0);
1374 hammer2_cluster_delete(trans, cparent, cluster, 0);
99535653 1375 }
ae183399 1376 error = 0;
ae183399 1377done:
da6f36f4
MD
1378 if (cparent)
1379 hammer2_cluster_unlock(cparent);
278ab2b2
MD
1380 if (cluster)
1381 hammer2_cluster_unlock(cluster);
da6f36f4
MD
1382 if (hparent)
1383 hammer2_cluster_unlock(hparent);
1384 if (hcluster)
1385 hammer2_cluster_unlock(hcluster);
05dd26e4
MD
1386 if (hlinkp)
1387 *hlinkp = hlink;
ae183399
MD
1388
1389 return error;
1390}
1391
044541cd
MD
1392/*
1393 * This is called from the mount code to initialize pmp->ihidden
1394 */
1395void
1396hammer2_inode_install_hidden(hammer2_pfsmount_t *pmp)
1397{
1398 hammer2_trans_t trans;
278ab2b2
MD
1399 hammer2_cluster_t *cparent;
1400 hammer2_cluster_t *cluster;
1401 hammer2_cluster_t *scan;
e07becf8 1402 const hammer2_inode_data_t *ripdata;
6a5f4fe6 1403 hammer2_inode_data_t *wipdata;
044541cd
MD
1404 hammer2_key_t key_dummy;
1405 hammer2_key_t key_next;
278ab2b2 1406 int ddflag;
044541cd
MD
1407 int error;
1408 int count;
e07becf8
MD
1409 int dip_check_algo;
1410 int dip_comp_algo;
044541cd
MD
1411
1412 if (pmp->ihidden)
1413 return;
1414
1415 /*
1416 * Find the hidden directory
1417 */
1418 bzero(&key_dummy, sizeof(key_dummy));
50456506 1419 hammer2_trans_init(&trans, pmp, 0);
044541cd 1420
e07becf8
MD
1421 /*
1422 * Setup for lookup, retrieve iroot's check and compression
1423 * algorithm request which was likely generated by newfs_hammer2.
1424 *
1425 * The check/comp fields will probably never be used since inodes
1426 * are renamed into the hidden directory and not created relative to
1427 * the hidden directory, chain creation inherits from bref.methods,
1428 * and data chains inherit from their respective file inode *_algo
1429 * fields.
1430 */
278ab2b2 1431 cparent = hammer2_inode_lock_ex(pmp->iroot);
bca9f8e6 1432 ripdata = &hammer2_cluster_rdata(cparent)->ipdata;
e07becf8
MD
1433 dip_check_algo = ripdata->check_algo;
1434 dip_comp_algo = ripdata->comp_algo;
1435 ripdata = NULL;
1436
278ab2b2
MD
1437 cluster = hammer2_cluster_lookup(cparent, &key_dummy,
1438 HAMMER2_INODE_HIDDENDIR,
1439 HAMMER2_INODE_HIDDENDIR,
1440 0, &ddflag);
1441 if (cluster) {
1442 pmp->ihidden = hammer2_inode_get(pmp, pmp->iroot, cluster);
044541cd
MD
1443 hammer2_inode_ref(pmp->ihidden);
1444
1445 /*
1446 * Remove any unlinked files which were left open as-of
1447 * any system crash.
5c88f2c7
MD
1448 *
1449 * Don't pass NODATA, we need the inode data so the delete
1450 * can do proper statistics updates.
044541cd
MD
1451 */
1452 count = 0;
278ab2b2 1453 scan = hammer2_cluster_lookup(cluster, &key_next,
50456506 1454 0, HAMMER2_TID_MAX,
5c88f2c7 1455 0, &ddflag);
044541cd 1456 while (scan) {
278ab2b2
MD
1457 if (hammer2_cluster_type(scan) ==
1458 HAMMER2_BREF_TYPE_INODE) {
da6f36f4
MD
1459 hammer2_cluster_delete(&trans, cluster, scan,
1460 HAMMER2_DELETE_PERMANENT);
044541cd
MD
1461 ++count;
1462 }
278ab2b2 1463 scan = hammer2_cluster_next(cluster, scan, &key_next,
5c88f2c7 1464 0, HAMMER2_TID_MAX, 0);
044541cd
MD
1465 }
1466
278ab2b2
MD
1467 hammer2_inode_unlock_ex(pmp->ihidden, cluster);
1468 hammer2_inode_unlock_ex(pmp->iroot, cparent);
044541cd
MD
1469 hammer2_trans_done(&trans);
1470 kprintf("hammer2: PFS loaded hidden dir, "
1471 "removed %d dead entries\n", count);
1472 return;
1473 }
1474
1475 /*
1476 * Create the hidden directory
1477 */
278ab2b2
MD
1478 error = hammer2_cluster_create(&trans, cparent, &cluster,
1479 HAMMER2_INODE_HIDDENDIR, 0,
1480 HAMMER2_BREF_TYPE_INODE,
b3659de2
MD
1481 HAMMER2_INODE_BYTES,
1482 0);
278ab2b2 1483 hammer2_inode_unlock_ex(pmp->iroot, cparent);
6a5f4fe6 1484
278ab2b2 1485 hammer2_cluster_modify(&trans, cluster, 0);
6a5f4fe6
MD
1486 wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1487 wipdata->type = HAMMER2_OBJTYPE_DIRECTORY;
1488 wipdata->inum = HAMMER2_INODE_HIDDENDIR;
1489 wipdata->nlinks = 1;
e07becf8
MD
1490 wipdata->comp_algo = dip_comp_algo;
1491 wipdata->check_algo = dip_check_algo;
6a5f4fe6 1492 hammer2_cluster_modsync(cluster);
044541cd
MD
1493 kprintf("hammer2: PFS root missing hidden directory, creating\n");
1494
278ab2b2 1495 pmp->ihidden = hammer2_inode_get(pmp, pmp->iroot, cluster);
044541cd 1496 hammer2_inode_ref(pmp->ihidden);
278ab2b2 1497 hammer2_inode_unlock_ex(pmp->ihidden, cluster);
044541cd
MD
1498 hammer2_trans_done(&trans);
1499}
1500
1501/*
1502 * If an open file is unlinked H2 needs to retain the file in the topology
1503 * to ensure that its backing store is not recovered by the bulk free scan.
1504 * This also allows us to avoid having to special-case the CHAIN_DELETED flag.
1505 *
1506 * To do this the file is moved to a hidden directory in the PFS root and
1507 * renamed. The hidden directory must be created if it does not exist.
1508 */
1509static
1510void
278ab2b2 1511hammer2_inode_move_to_hidden(hammer2_trans_t *trans,
da6f36f4
MD
1512 hammer2_cluster_t **cparentp,
1513 hammer2_cluster_t **clusterp,
1514 hammer2_tid_t inum)
044541cd 1515{
278ab2b2 1516 hammer2_cluster_t *dcluster;
044541cd
MD
1517 hammer2_pfsmount_t *pmp;
1518 int error;
1519
278ab2b2 1520 pmp = (*clusterp)->pmp;
044541cd
MD
1521 KKASSERT(pmp != NULL);
1522 KKASSERT(pmp->ihidden != NULL);
59c5121a 1523
da6f36f4 1524 hammer2_cluster_delete(trans, *cparentp, *clusterp, 0);
278ab2b2
MD
1525 dcluster = hammer2_inode_lock_ex(pmp->ihidden);
1526 error = hammer2_inode_connect(trans, clusterp, 0,
1527 pmp->ihidden, dcluster,
044541cd 1528 NULL, 0, inum);
278ab2b2 1529 hammer2_inode_unlock_ex(pmp->ihidden, dcluster);
044541cd
MD
1530 KKASSERT(error == 0);
1531}
1532
e708f8b9 1533/*
da6f36f4 1534 * Given an exclusively locked inode and cluster we consolidate the cluster
0924b3f8 1535 * for hardlink creation, adding (nlinks) to the file's link count and
da6f36f4
MD
1536 * potentially relocating the inode to (cdip) which is a parent directory
1537 * common to both the current location of the inode and the intended new
1538 * hardlink.
d5fabb70 1539 *
278ab2b2
MD
1540 * Replaces (*clusterp) if consolidation occurred, unlocking the old cluster
1541 * and returning a new locked cluster.
99535653 1542 *
278ab2b2 1543 * NOTE! This function will also replace ip->cluster.
e708f8b9
MD
1544 */
1545int
9b21452a 1546hammer2_hardlink_consolidate(hammer2_trans_t *trans,
278ab2b2
MD
1547 hammer2_inode_t *ip,
1548 hammer2_cluster_t **clusterp,
1549 hammer2_inode_t *cdip,
1550 hammer2_cluster_t *cdcluster,
9b21452a 1551 int nlinks)
e708f8b9 1552{
6a5f4fe6
MD
1553 const hammer2_inode_data_t *ripdata;
1554 hammer2_inode_data_t *wipdata;
278ab2b2 1555 hammer2_cluster_t *cluster;
da6f36f4 1556 hammer2_cluster_t *cparent;
99535653
MD
1557 int error;
1558
278ab2b2 1559 cluster = *clusterp;
bca9f8e6 1560 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
51bf8e9b 1561 if (nlinks == 0 && /* no hardlink needed */
6a5f4fe6 1562 (ripdata->name_key & HAMMER2_DIRHASH_VISIBLE)) {
e708f8b9 1563 return (0);
51bf8e9b 1564 }
0dea3156 1565
51bf8e9b 1566 if (hammer2_hardlink_enable == 0) { /* disallow hardlinks */
278ab2b2
MD
1567 hammer2_cluster_unlock(cluster);
1568 *clusterp = NULL;
e708f8b9 1569 return (ENOTSUP);
51bf8e9b 1570 }
99535653 1571
da6f36f4
MD
1572 cparent = NULL;
1573
99535653 1574 /*
51bf8e9b
MD
1575 * If no change in the hardlink's target directory is required and
1576 * this is already a hardlink target, all we need to do is adjust
1577 * the link count.
99535653 1578 */
bca9f8e6 1579 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
9b21452a 1580 if (cdip == ip->pip &&
6a5f4fe6 1581 (ripdata->name_key & HAMMER2_DIRHASH_VISIBLE) == 0) {
51bf8e9b 1582 if (nlinks) {
278ab2b2 1583 hammer2_cluster_modify(trans, cluster, 0);
6a5f4fe6
MD
1584 wipdata = &hammer2_cluster_wdata(cluster)->ipdata;
1585 wipdata->nlinks += nlinks;
1586 hammer2_cluster_modsync(cluster);
1587 ripdata = wipdata;
476d2aad 1588 }
51bf8e9b
MD
1589 error = 0;
1590 goto done;
99535653
MD
1591 }
1592
1593 /*
da6f36f4
MD
1594 * Cluster is the real inode. The originating directory is locked
1595 * by the caller so we can manipulate it without worrying about races
1596 * against other lookups.
1597 *
1598 * If cluster is visible we need to delete it from the current
1599 * location and create a hardlink pointer in its place. If it is
1600 * not visible we need only delete it. Then later cluster will be
1601 * renamed to a parent directory and converted (if necessary) to
1602 * a hidden inode (via shiftup).
1603 *
1604 * NOTE! We must hold cparent locked through the delete/create/rename
1605 * operation to ensure that other threads block resolving to
1606 * the same hardlink, otherwise the other threads may not see
1607 * the hardlink.
99535653 1608 */
278ab2b2 1609 KKASSERT((cluster->focus->flags & HAMMER2_CHAIN_DELETED) == 0);
da6f36f4
MD
1610 cparent = hammer2_cluster_parent(cluster);
1611
1612 hammer2_cluster_delete(trans, cparent, cluster, 0);
1613
bca9f8e6 1614 ripdata = &hammer2_cluster_rdata(cluster)->ipdata;
6a5f4fe6
MD
1615 KKASSERT(ripdata->type != HAMMER2_OBJTYPE_HARDLINK);
1616 if (ripdata->name_key & HAMMER2_DIRHASH_VISIBLE) {
da6f36f4
MD
1617 hammer2_cluster_t *ncluster;
1618 hammer2_key_t lhc;
1619
1620 ncluster = NULL;
1621 lhc = cluster->focus->bref.key;
1622 error = hammer2_cluster_create(trans, cparent, &ncluster,
1623 lhc, 0,
1624 HAMMER2_BREF_TYPE_INODE,
b3659de2
MD
1625 HAMMER2_INODE_BYTES,
1626 0);
da6f36f4 1627 hammer2_cluster_modify(trans, ncluster, 0);
6a5f4fe6 1628 wipdata = &hammer2_cluster_wdata(ncluster)->ipdata;
da6f36f4 1629
a71db85d
MD
1630 /* wipdata->comp_algo = ripdata->comp_algo; */
1631 wipdata->comp_algo = 0;
e07becf8 1632 wipdata->check_algo = 0;
da6f36f4
MD
1633 wipdata->version = HAMMER2_INODE_VERSION_ONE;
1634 wipdata->inum = ripdata->inum;
1635 wipdata->target_type = ripdata->type;
6a5f4fe6
MD
1636 wipdata->type = HAMMER2_OBJTYPE_HARDLINK;
1637 wipdata->uflags = 0;
1638 wipdata->rmajor = 0;
1639 wipdata->rminor = 0;
1640 wipdata->ctime = 0;
1641 wipdata->mtime = 0;
1642 wipdata->atime = 0;
1643 wipdata->btime = 0;
1644 bzero(&wipdata->uid, sizeof(wipdata->uid));
1645 bzero(&wipdata->gid, sizeof(wipdata->gid));
1646 wipdata->op_flags = HAMMER2_OPFLAG_DIRECTDATA;
1647 wipdata->cap_flags = 0;
1648 wipdata->mode = 0;
1649 wipdata->size = 0;
1650 wipdata->nlinks = 1;
1651 wipdata->iparent = 0; /* XXX */
1652 wipdata->pfs_type = 0;
1653 wipdata->pfs_inum = 0;
1654 bzero(&wipdata->pfs_clid, sizeof(wipdata->pfs_clid));
1655 bzero(&wipdata->pfs_fsid, sizeof(wipdata->pfs_fsid));
1656 wipdata->data_quota = 0;
1657 wipdata->data_count = 0;
1658 wipdata->inode_quota = 0;
1659 wipdata->inode_count = 0;
1660 wipdata->attr_tid = 0;
1661 wipdata->dirent_tid = 0;
1662 bzero(&wipdata->u, sizeof(wipdata->u));
da6f36f4
MD
1663 bcopy(ripdata->filename, wipdata->filename, ripdata->name_len);
1664 wipdata->name_key = ncluster->focus->bref.key;
1665 wipdata->name_len = ripdata->name_len;
0924b3f8 1666 /* XXX transaction ids */
6a5f4fe6 1667 hammer2_cluster_modsync(ncluster);
da6f36f4 1668 hammer2_cluster_unlock(ncluster);
99535653 1669 }
6a5f4fe6 1670 ripdata = wipdata;
51bf8e9b
MD
1671
1672 /*
278ab2b2 1673 * cluster represents the hardlink target and is now flagged deleted.
0924b3f8
MD
1674 * duplicate it to the parent directory and adjust nlinks.
1675 *
278ab2b2
MD
1676 * WARNING! The shiftup() call can cause ncluster to be moved into
1677 * an indirect block, and our ncluster will wind up pointing
0924b3f8 1678 * to the older/original version.
51bf8e9b 1679 */
278ab2b2
MD
1680 KKASSERT(cluster->focus->flags & HAMMER2_CHAIN_DELETED);
1681 hammer2_hardlink_shiftup(trans, cluster, cdip, cdcluster,
1682 nlinks, &error);
0924b3f8
MD
1683
1684 if (error == 0)
278ab2b2 1685 hammer2_inode_repoint(ip, cdip, cluster);
0924b3f8 1686
51bf8e9b 1687done:
0924b3f8 1688 /*
278ab2b2 1689 * Cleanup, cluster/ncluster already dealt with.
da6f36f4
MD
1690 *
1691 * Return the shifted cluster in *clusterp.
0924b3f8 1692 */
da6f36f4
MD
1693 if (cparent)
1694 hammer2_cluster_unlock(cparent);
278ab2b2 1695 *clusterp = cluster;
99535653
MD
1696
1697 return (error);
e708f8b9
MD
1698}
1699
1700/*
476d2aad
MD
1701 * If (*ochainp) is non-NULL it points to the forward OBJTYPE_HARDLINK
1702 * inode while (*chainp) points to the resolved (hidden hardlink
1703 * target) inode. In this situation when nlinks is 1 we wish to
1704 * deconsolidate the hardlink, moving it back to the directory that now
1705 * represents the only remaining link.
e708f8b9
MD
1706 */
1707int
0dea3156
MD
1708hammer2_hardlink_deconsolidate(hammer2_trans_t *trans,
1709 hammer2_inode_t *dip,
476d2aad
MD
1710 hammer2_chain_t **chainp,
1711 hammer2_chain_t **ochainp)
e708f8b9 1712{
476d2aad 1713 if (*ochainp == NULL)
e708f8b9
MD
1714 return (0);
1715 /* XXX */
1716 return (0);
1717}
1718
1719/*
da6f36f4 1720 * The caller presents a locked cluster with an obj_type of
05dd26e4
MD
1721 * HAMMER2_OBJTYPE_HARDLINK. This routine will locate and replace the
1722 * cluster with the target hardlink, also locked.
1723 *
1724 * If cparentp is not NULL a locked cluster representing the hardlink's
1725 * parent is also returned.
476d2aad 1726 *
05dd26e4
MD
1727 * If we are unable to locate the hardlink target EIO is returned and
1728 * (*cparentp) is set to NULL. The passed-in cluster still needs to be
1729 * unlocked by the caller but will be degenerate... not have any chains.
e708f8b9
MD
1730 */
1731int
da6f36f4
MD
1732hammer2_hardlink_find(hammer2_inode_t *dip,
1733 hammer2_cluster_t **cparentp, hammer2_cluster_t *cluster)
e708f8b9 1734{
6a5f4fe6 1735 const hammer2_inode_data_t *ipdata;
278ab2b2
MD
1736 hammer2_cluster_t *cparent;
1737 hammer2_cluster_t *rcluster;
476d2aad 1738 hammer2_inode_t *ip;
99535653 1739 hammer2_inode_t *pip;
1897c66e 1740 hammer2_key_t key_dummy;
99535653 1741 hammer2_key_t lhc;
278ab2b2 1742 int ddflag;
99535653 1743
476d2aad
MD
1744 pip = dip;
1745 hammer2_inode_ref(pip); /* for loop */
99535653 1746
476d2aad 1747 /*
278ab2b2 1748 * Locate the hardlink. pip is referenced and not locked.
476d2aad 1749 */
bca9f8e6 1750 ipdata = &hammer2_cluster_rdata(cluster)->ipdata;
278ab2b2
MD
1751 lhc = ipdata->inum;
1752
1753 /*
1754 * We don't need the cluster's chains, but we need to retain the
1755 * cluster structure itself so we can load the hardlink search
1756 * result into it.
1757 */
1758 KKASSERT(cluster->refs == 1);
1759 atomic_add_int(&cluster->refs, 1);
1760 hammer2_cluster_unlock(cluster); /* hack */
1761 cluster->nchains = 0; /* hack */
1762
1763 rcluster = NULL;
da6f36f4 1764 cparent = NULL;
99535653 1765
476d2aad 1766 while ((ip = pip) != NULL) {
278ab2b2 1767 cparent = hammer2_inode_lock_ex(ip);
10252dc7 1768 hammer2_inode_drop(ip); /* loop */
278ab2b2
MD
1769 KKASSERT(hammer2_cluster_type(cparent) ==
1770 HAMMER2_BREF_TYPE_INODE);
1771 rcluster = hammer2_cluster_lookup(cparent, &key_dummy,
1772 lhc, lhc, 0, &ddflag);
278ab2b2 1773 if (rcluster)
99535653 1774 break;
da6f36f4 1775 hammer2_cluster_lookup_done(cparent); /* discard parent */
05dd26e4 1776 cparent = NULL; /* safety */
476d2aad
MD
1777 pip = ip->pip; /* safe, ip held locked */
1778 if (pip)
10252dc7 1779 hammer2_inode_ref(pip); /* loop */
9596b8c4 1780 hammer2_inode_unlock_ex(ip, NULL);
99535653 1781 }
476d2aad
MD
1782
1783 /*
1784 * chain is locked, ip is locked. Unlock ip, return the locked
1785 * chain. *ipp is already set w/a ref count and not locked.
e2e9e2db 1786 *
278ab2b2 1787 * (cparent is already unlocked).
476d2aad 1788 */
278ab2b2
MD
1789 if (rcluster) {
1790 hammer2_cluster_replace(cluster, rcluster);
1791 hammer2_cluster_drop(rcluster);
05dd26e4 1792 if (cparentp) {
da6f36f4 1793 *cparentp = cparent;
05dd26e4
MD
1794 hammer2_inode_unlock_ex(ip, NULL);
1795 } else {
da6f36f4 1796 hammer2_inode_unlock_ex(ip, cparent);
05dd26e4 1797 }
99535653
MD
1798 return (0);
1799 } else {
da6f36f4
MD
1800 if (cparentp)
1801 *cparentp = NULL;
1802 if (ip)
1803 hammer2_inode_unlock_ex(ip, cparent);
99535653
MD
1804 return (EIO);
1805 }
e708f8b9 1806}
f3843dc2
MD
1807
1808/*
850687d2
MD
1809 * Find the directory common to both fdip and tdip.
1810 *
1811 * Returns a held but not locked inode. Caller typically locks the inode,
1812 * and when through unlocks AND drops it.
f3843dc2
MD
1813 */
1814hammer2_inode_t *
0dea3156 1815hammer2_inode_common_parent(hammer2_inode_t *fdip, hammer2_inode_t *tdip)
f3843dc2
MD
1816{
1817 hammer2_inode_t *scan1;
1818 hammer2_inode_t *scan2;
1819
1820 /*
1821 * We used to have a depth field but it complicated matters too
1822 * much for directory renames. So now its ugly. Check for
1823 * simple cases before giving up and doing it the expensive way.
1824 *
1825 * XXX need a bottom-up topology stability lock
1826 */
1827 if (fdip == tdip || fdip == tdip->pip) {
1828 hammer2_inode_ref(fdip);
1829 return(fdip);
1830 }
1831 if (fdip->pip == tdip) {
1832 hammer2_inode_ref(tdip);
1833 return(tdip);
1834 }
51bf8e9b
MD
1835
1836 /*
1837 * XXX not MPSAFE
1838 */
f3843dc2
MD
1839 for (scan1 = fdip; scan1->pmp == fdip->pmp; scan1 = scan1->pip) {
1840 scan2 = tdip;
1841 while (scan2->pmp == tdip->pmp) {
1842 if (scan1 == scan2) {
1843 hammer2_inode_ref(scan1);
1844 return(scan1);
1845 }
1846 scan2 = scan2->pip;
9797e933
MD
1847 if (scan2 == NULL)
1848 break;
f3843dc2
MD
1849 }
1850 }
1851 panic("hammer2_inode_common_parent: no common parent %p %p\n",
1852 fdip, tdip);
1853 /* NOT REACHED */
1854 return(NULL);
1855}
355d67fc
MD
1856
1857/*
1858 * Synchronize the inode's frontend state with the chain state prior
1859 * to any explicit flush of the inode or any strategy write call.
1860 *
1861 * Called with a locked inode.
1862 */
1863void
1864hammer2_inode_fsync(hammer2_trans_t *trans, hammer2_inode_t *ip,
278ab2b2 1865 hammer2_cluster_t *cparent)
355d67fc 1866{
6a5f4fe6
MD
1867 const hammer2_inode_data_t *ripdata;
1868 hammer2_inode_data_t *wipdata;
278ab2b2
MD
1869 hammer2_cluster_t *dparent;
1870 hammer2_cluster_t *cluster;
355d67fc 1871 hammer2_key_t lbase;
1897c66e 1872 hammer2_key_t key_next;
6a5f4fe6 1873 int dosync = 0;
278ab2b2 1874 int ddflag;
355d67fc 1875
bca9f8e6 1876 ripdata = &hammer2_cluster_rdata(cparent)->ipdata; /* target file */
355d67fc
MD
1877
1878 if (ip->flags & HAMMER2_INODE_MTIME) {
6a5f4fe6 1879 wipdata = hammer2_cluster_modify_ip(trans, ip, cparent, 0);
355d67fc 1880 atomic_clear_int(&ip->flags, HAMMER2_INODE_MTIME);
6a5f4fe6
MD
1881 wipdata->mtime = ip->mtime;
1882 dosync = 1;
1883 ripdata = wipdata;
355d67fc 1884 }
6a5f4fe6
MD
1885 if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size < ripdata->size) {
1886 wipdata = hammer2_cluster_modify_ip(trans, ip, cparent, 0);
1887 wipdata->size = ip->size;
1888 dosync = 1;
1889 ripdata = wipdata;
355d67fc
MD
1890 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1891
1892 /*
1893 * We must delete any chains beyond the EOF. The chain
1894 * straddling the EOF will be pending in the bioq.
1895 */
6a5f4fe6 1896 lbase = (ripdata->size + HAMMER2_PBUFMASK64) &
355d67fc 1897 ~HAMMER2_PBUFMASK64;
278ab2b2
MD
1898 dparent = hammer2_cluster_lookup_init(&ip->cluster, 0);
1899 cluster = hammer2_cluster_lookup(dparent, &key_next,
1900 lbase, (hammer2_key_t)-1,
1901 HAMMER2_LOOKUP_NODATA,
1902 &ddflag);
1903 while (cluster) {
355d67fc
MD
1904 /*
1905 * Degenerate embedded case, nothing to loop on
1906 */
278ab2b2
MD
1907 switch (hammer2_cluster_type(cluster)) {
1908 case HAMMER2_BREF_TYPE_INODE:
1909 hammer2_cluster_unlock(cluster);
1910 cluster = NULL;
355d67fc 1911 break;
278ab2b2 1912 case HAMMER2_BREF_TYPE_DATA:
da6f36f4
MD
1913 hammer2_cluster_delete(trans, dparent, cluster,
1914 HAMMER2_DELETE_PERMANENT);
278ab2b2
MD
1915 /* fall through */
1916 default:
1917 cluster = hammer2_cluster_next(dparent, cluster,
1918 &key_next,
1897c66e 1919 key_next, (hammer2_key_t)-1,
355d67fc 1920 HAMMER2_LOOKUP_NODATA);
278ab2b2
MD
1921 break;
1922 }
355d67fc 1923 }
278ab2b2 1924 hammer2_cluster_lookup_done(dparent);
355d67fc 1925 } else
6a5f4fe6
MD
1926 if ((ip->flags & HAMMER2_INODE_RESIZED) && ip->size > ripdata->size) {
1927 wipdata = hammer2_cluster_modify_ip(trans, ip, cparent, 0);
1928 wipdata->size = ip->size;
355d67fc
MD
1929 atomic_clear_int(&ip->flags, HAMMER2_INODE_RESIZED);
1930
1931 /*
1932 * When resizing larger we may not have any direct-data
1933 * available.
1934 */
6a5f4fe6 1935 if ((wipdata->op_flags & HAMMER2_OPFLAG_DIRECTDATA) &&
355d67fc 1936 ip->size > HAMMER2_EMBEDDED_BYTES) {
6a5f4fe6
MD
1937 wipdata->op_flags &= ~HAMMER2_OPFLAG_DIRECTDATA;
1938 bzero(&wipdata->u.blockset,
1939 sizeof(wipdata->u.blockset));
355d67fc 1940 }
6a5f4fe6
MD
1941 dosync = 1;
1942 ripdata = wipdata;
355d67fc 1943 }
6a5f4fe6
MD
1944 if (dosync)
1945 hammer2_cluster_modsync(cparent);
355d67fc 1946}