2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/types.h>
45 * Adding a ref to an inode is only legal if the inode already has at least
49 hammer2_inode_ref(hammer2_inode_t *ip)
51 hammer2_chain_ref(ip->hmp, &ip->chain);
55 * Drop an inode reference, freeing the inode when the last reference goes
59 hammer2_inode_drop(hammer2_inode_t *ip)
61 hammer2_chain_drop(ip->hmp, &ip->chain);
65 * Get the vnode associated with the given inode, allocating the vnode if
68 * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
71 * The vnode will be returned exclusively locked and referenced. The
72 * reference on the vnode prevents it from being reclaimed.
74 * The inode (ip) must be referenced by the caller and not locked to avoid
75 * it getting ripped out from under us or deadlocked.
78 hammer2_igetv(hammer2_inode_t *ip, int *errorp)
81 hammer2_pfsmount_t *pmp;
84 KKASSERT(pmp != NULL);
89 * Attempt to reuse an existing vnode assignment. It is
90 * possible to race a reclaim so the vget() may fail. The
91 * inode must be unlocked during the vget() to avoid a
92 * deadlock against a reclaim.
97 * Lock the inode and check for a reclaim race
99 hammer2_inode_lock_ex(ip);
101 hammer2_inode_unlock_ex(ip);
106 * Inode must be unlocked during the vget() to avoid
107 * possible deadlocks, vnode is held to prevent
108 * destruction during the vget(). The vget() can
109 * still fail if we lost a reclaim race on the vnode.
111 vhold_interlocked(vp);
112 hammer2_inode_unlock_ex(ip);
113 if (vget(vp, LK_EXCLUSIVE)) {
118 /* vp still locked and ref from vget */
124 * No vnode exists, allocate a new vnode. Beware of
125 * allocation races. This function will return an
126 * exclusively locked and referenced vnode.
128 *errorp = getnewvnode(VT_HAMMER2, pmp->mp, &vp, 0, 0);
135 * Lock the inode and check for an allocation race.
137 hammer2_inode_lock_ex(ip);
138 if (ip->vp != NULL) {
141 hammer2_inode_unlock_ex(ip);
145 switch (ip->ip_data.type) {
146 case HAMMER2_OBJTYPE_DIRECTORY:
149 case HAMMER2_OBJTYPE_REGFILE:
151 vinitvmio(vp, ip->ip_data.size,
153 (int)ip->ip_data.size & HAMMER2_LBUFMASK);
155 case HAMMER2_OBJTYPE_SOFTLINK:
157 * XXX for now we are using the generic file_read
158 * and file_write code so we need a buffer cache
162 vinitvmio(vp, ip->ip_data.size,
164 (int)ip->ip_data.size & HAMMER2_LBUFMASK);
168 panic("hammer2: unhandled objtype %d",
173 if (ip == pmp->iroot)
174 vsetflags(vp, VROOT);
178 hammer2_chain_ref(ip->hmp, &ip->chain); /* vp association */
179 hammer2_inode_unlock_ex(ip);
184 * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
186 if (hammer2_debug & 0x0002) {
187 kprintf("igetv vp %p refs %d aux %d\n",
188 vp, vp->v_sysref.refcnt, vp->v_auxrefs);
194 * Create a new inode in the specified directory using the vattr to
195 * figure out the type of inode.
197 * If no error occurs the new inode with its chain locked is returned in
198 * *nipp, otherwise an error is returned and *nipp is set to NULL.
200 * If vap and/or cred are NULL the related fields are not set and the
201 * inode type defaults to a directory. This is used when creating PFSs
202 * under the super-root, so the inode number is set to 1 in this case.
205 hammer2_inode_create(hammer2_inode_t *dip,
206 struct vattr *vap, struct ucred *cred,
207 const uint8_t *name, size_t name_len,
208 hammer2_inode_t **nipp)
210 hammer2_mount_t *hmp = dip->hmp;
211 hammer2_chain_t *chain;
212 hammer2_chain_t *parent;
213 hammer2_inode_t *nip;
217 lhc = hammer2_dirhash(name, name_len);
220 * Locate the inode or indirect block to create the new
221 * entry in. At the same time check for key collisions
222 * and iterate until we don't get one.
224 parent = &dip->chain;
225 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
229 chain = hammer2_chain_lookup(hmp, &parent, lhc, lhc, 0);
232 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
234 hammer2_chain_unlock(hmp, chain);
239 chain = hammer2_chain_create(hmp, parent, NULL, lhc, 0,
240 HAMMER2_BREF_TYPE_INODE,
241 HAMMER2_INODE_BYTES);
245 hammer2_chain_unlock(hmp, parent);
248 * Handle the error case
251 KKASSERT(chain == NULL);
257 * Set up the new inode
262 hammer2_voldata_lock(hmp);
264 nip->ip_data.type = hammer2_get_obj_type(vap->va_type);
265 nip->ip_data.inum = hmp->voldata.alloc_tid++;
266 /* XXX modify/lock */
268 nip->ip_data.type = HAMMER2_OBJTYPE_DIRECTORY;
269 nip->ip_data.inum = 1;
271 hammer2_voldata_unlock(hmp);
272 nip->ip_data.version = HAMMER2_INODE_VERSION_ONE;
273 nip->ip_data.ctime = 0;
274 nip->ip_data.mtime = 0;
276 nip->ip_data.mode = vap->va_mode;
277 nip->ip_data.nlinks = 1;
281 * Regular files and softlinks allow a small amount of data to be
282 * directly embedded in the inode. This flag will be cleared if
283 * the size is extended past the embedded limit.
285 if (nip->ip_data.type == HAMMER2_OBJTYPE_REGFILE ||
286 nip->ip_data.type == HAMMER2_OBJTYPE_SOFTLINK) {
287 nip->ip_data.op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
290 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
291 bcopy(name, nip->ip_data.filename, name_len);
292 nip->ip_data.name_key = lhc;
293 nip->ip_data.name_len = name_len;
299 * Connect inode (ip) to the specified directory using the specified name.
300 * (ip) must be locked.
303 hammer2_inode_connect(hammer2_inode_t *dip, hammer2_inode_t *ip,
304 const uint8_t *name, size_t name_len)
306 hammer2_mount_t *hmp = dip->hmp;
307 hammer2_chain_t *chain;
308 hammer2_chain_t *parent;
312 lhc = hammer2_dirhash(name, name_len);
315 * Locate the inode or indirect block to create the new
316 * entry in. At the same time check for key collisions
317 * and iterate until we don't get one.
319 parent = &dip->chain;
320 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
324 chain = hammer2_chain_lookup(hmp, &parent, lhc, lhc, 0);
327 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
329 hammer2_chain_unlock(hmp, chain);
335 * Passing a non-NULL chain to hammer2_chain_create() reconnects the
336 * existing chain instead of creating a new one. The chain's bref
337 * will be properly updated.
340 chain = hammer2_chain_create(hmp, parent, &ip->chain, lhc, 0,
341 HAMMER2_BREF_TYPE_INODE /* n/a */,
342 HAMMER2_INODE_BYTES); /* n/a */
346 hammer2_chain_unlock(hmp, parent);
349 * Handle the error case
352 KKASSERT(chain == NULL);
357 * Directory entries are inodes so if the name has changed we have
358 * to update the inode.
360 if (ip->ip_data.name_len != name_len ||
361 bcmp(ip->ip_data.filename, name, name_len) != 0) {
362 hammer2_chain_modify(hmp, chain, 0);
363 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
364 bcopy(name, ip->ip_data.filename, name_len);
365 ip->ip_data.name_key = lhc;
366 ip->ip_data.name_len = name_len;
368 /*nip->ip_data.nlinks = 1;*/
374 * Create a hardlink forwarding entry (dip, name) to the specified (ip).
376 * This is one of the more complex implementations in HAMMER2. The
377 * filesystem strictly updates its chains bottom-up in a copy-on-write
378 * fashion. This makes hardlinks difficult to implement but we've come up
379 * with a dandy solution.
381 * When a file has more than one link the actual inode is created as a
382 * hidden directory entry (indexed by inode number) in a common parent of
383 * all hardlinks which reference the file. The hardlinks in each directory
384 * are merely forwarding entries to the hidden inode.
388 * Most VOPs can be blissfully unaware of the forwarding entries.
389 * nresolve, nlink, and remove code have to be forwarding-aware
390 * in order to return the (ip/vp) for the actual file (and otherwise do
393 * (1) If the ip we are linking to is a normal embedded inode (nlinks==1)
394 * we have to replace the directory entry with a forwarding inode
395 * and move the normal ip/vp to a hidden entry indexed by the inode
396 * number in a common parent directory.
398 * (2) If the ip we are linking to is already a hidden entry but is not
399 * a common parent we have to move its entry to a common parent by
400 * moving the entry upward.
402 * (3) The trivial case is the entry is already hidden and already a
403 * common parent. We adjust nlinks for the entry and are done.
404 * (this is the fall-through case).
407 hammer2_hardlink_create(hammer2_inode_t *ip, hammer2_inode_t *dip,
408 const uint8_t *name, size_t name_len)
412 hammer2_inode_t *nip;
413 hammer2_inode_t *xip;
416 hammer2_inode_t *nip; /* hardlink forwarding inode */
417 error = hammer2_inode_create(hmp, NULL, ap->a_cred,
418 dip, name, name_len, &nip);
420 KKASSERT(nip == NULL);
423 KKASSERT(nip->ip_data.type == HAMMER2_OBJTYPE_HARDLINK);
424 hammer2_chain_modify(&nip->chain, 0);
425 nip->ip_data.inum = ip->ip_data.inum;
426 hammer2_chain_unlock(hmp, &nip->chain);
432 * Unlink the file from the specified directory inode. The directory inode
433 * does not need to be locked.
435 * isdir determines whether a directory/non-directory check should be made.
436 * No check is made if isdir is set to -1.
438 * adjlinks tells unlink that we want to adjust the nlinks count of the
439 * inode. When removing the last link for a NON forwarding entry we can
440 * just ignore the link count... no point updating the inode that we are
441 * about to dereference, it would just result in a lot of wasted I/O.
443 * However, if the entry is a forwarding entry (aka a hardlink), and adjlinks
444 * is non-zero, we have to locate the hardlink and adjust its nlinks field.
447 hammer2_unlink_file(hammer2_inode_t *dip, const uint8_t *name, size_t name_len,
448 int isdir, int adjlinks)
450 hammer2_mount_t *hmp;
451 hammer2_chain_t *parent;
452 hammer2_chain_t *chain;
453 hammer2_chain_t *dparent;
454 hammer2_chain_t *dchain;
461 lhc = hammer2_dirhash(name, name_len);
464 * Search for the filename in the directory
466 parent = &dip->chain;
467 hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
468 chain = hammer2_chain_lookup(hmp, &parent,
469 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
472 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
474 name_len == chain->data->ipdata.name_len &&
475 bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
478 chain = hammer2_chain_next(hmp, &parent, chain,
479 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
484 * Not found or wrong type (isdir < 0 disables the type check).
487 hammer2_chain_unlock(hmp, parent);
490 if (chain->data->ipdata.type == HAMMER2_OBJTYPE_DIRECTORY &&
495 if (chain->data->ipdata.type != HAMMER2_OBJTYPE_DIRECTORY &&
502 * If this is a directory the directory must be empty. However, if
503 * isdir < 0 we are doing a rename and the directory does not have
506 if (chain->data->ipdata.type == HAMMER2_OBJTYPE_DIRECTORY &&
509 hammer2_chain_lock(hmp, dparent, HAMMER2_RESOLVE_ALWAYS);
510 dchain = hammer2_chain_lookup(hmp, &dparent,
511 0, (hammer2_key_t)-1,
512 HAMMER2_LOOKUP_NODATA);
514 hammer2_chain_unlock(hmp, dchain);
515 hammer2_chain_unlock(hmp, dparent);
519 hammer2_chain_unlock(hmp, dparent);
526 * If adjlinks is non-zero this is a real deletion (otherwise it is
527 * probably a rename). XXX
530 if (chain->data->ipdata.type == HAMMER2_OBJTYPE_HARDLINK) {
531 /*hammer2_adjust_hardlink(chain->u.ip, -1);*/
542 * Found, the chain represents the inode. Remove the parent reference
543 * to the chain. The chain itself is no longer referenced and will
544 * be marked unmodified by hammer2_chain_delete(), avoiding unnecessary
547 hammer2_chain_delete(hmp, parent, chain);
548 /* XXX nlinks (hardlink special case) */
549 /* XXX nlinks (parent directory) */
553 * Destroy any associated vnode, but only if this was the last
554 * link. XXX this might not be needed.
556 if (chain->u.ip->vp) {
558 vp = hammer2_igetv(chain->u.ip, &error);
561 /* hammer2_knote(vp, NOTE_DELETE); */
562 cache_inval_vp(vp, CINV_DESTROY);
570 hammer2_chain_unlock(hmp, chain);
571 hammer2_chain_unlock(hmp, parent);
577 * Calculate the allocation size for the file fragment straddling EOF
580 hammer2_inode_calc_alloc(hammer2_key_t filesize)
582 int frag = (int)filesize & HAMMER2_PBUFMASK;
587 for (radix = HAMMER2_MINALLOCRADIX; frag > (1 << radix); ++radix)