Merge branches 'hammer2' and 'master' of ssh://crater.dragonflybsd.org/repository...
[dragonfly.git] / sys / vfs / hammer2 / hammer2_inode.c
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  */
35 #include <sys/cdefs.h>
36 #include <sys/cdefs.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/types.h>
40 #include <sys/lock.h>
41 #include <sys/uuid.h>
42
43 #include "hammer2.h"
44
45 /*
46  * Adding a ref to an inode is only legal if the inode already has at least
47  * one ref.
48  */
49 void
50 hammer2_inode_ref(hammer2_inode_t *ip)
51 {
52         hammer2_chain_ref(ip->hmp, &ip->chain);
53 }
54
55 /*
56  * Drop an inode reference, freeing the inode when the last reference goes
57  * away.
58  */
59 void
60 hammer2_inode_drop(hammer2_inode_t *ip)
61 {
62         hammer2_chain_drop(ip->hmp, &ip->chain);
63 }
64
65 /*
66  * Get the vnode associated with the given inode, allocating the vnode if
67  * necessary.
68  *
69  * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
70  * races.
71  *
72  * The vnode will be returned exclusively locked and referenced.  The
73  * reference on the vnode prevents it from being reclaimed.
74  *
75  * The inode (ip) must be referenced by the caller and not locked to avoid
76  * it getting ripped out from under us or deadlocked.
77  */
78 struct vnode *
79 hammer2_igetv(hammer2_inode_t *ip, int *errorp)
80 {
81         struct vnode *vp;
82         hammer2_mount_t *hmp;
83
84         hmp = ip->hmp;
85         *errorp = 0;
86
87         for (;;) {
88                 /*
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.
93                  */
94                 vp = ip->vp;
95                 if (vp) {
96                         /*
97                          * Lock the inode and check for a reclaim race
98                          */
99                         hammer2_inode_lock_ex(ip);
100                         if (ip->vp != vp) {
101                                 hammer2_inode_unlock_ex(ip);
102                                 continue;
103                         }
104
105                         /*
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.
110                          */
111                         vhold_interlocked(vp);
112                         hammer2_inode_unlock_ex(ip);
113                         if (vget(vp, LK_EXCLUSIVE)) {
114                                 vdrop(vp);
115                                 continue;
116                         }
117                         vdrop(vp);
118                         /* vp still locked and ref from vget */
119                         *errorp = 0;
120                         break;
121                 }
122
123                 /*
124                  * No vnode exists, allocate a new vnode.  Beware of
125                  * allocation races.  This function will return an
126                  * exclusively locked and referenced vnode.
127                  */
128                 *errorp = getnewvnode(VT_HAMMER2, H2TOMP(hmp), &vp, 0, 0);
129                 if (*errorp) {
130                         vp = NULL;
131                         break;
132                 }
133
134                 /*
135                  * Lock the inode and check for an allocation race.
136                  */
137                 hammer2_inode_lock_ex(ip);
138                 if (ip->vp != NULL) {
139                         vp->v_type = VBAD;
140                         vx_put(vp);
141                         hammer2_inode_unlock_ex(ip);
142                         continue;
143                 }
144
145                 switch (ip->ip_data.type) {
146                 case HAMMER2_OBJTYPE_DIRECTORY:
147                         vp->v_type = VDIR;
148                         break;
149                 case HAMMER2_OBJTYPE_REGFILE:
150                         vp->v_type = VREG;
151                         vinitvmio(vp, 0, HAMMER2_LBUFSIZE,
152                                   (int)ip->ip_data.size & HAMMER2_LBUFMASK);
153                         break;
154                 case HAMMER2_OBJTYPE_SOFTLINK:
155                         /*
156                          * XXX for now we are using the generic file_read
157                          * and file_write code so we need a buffer cache
158                          * association.
159                          */
160                         vp->v_type = VLNK;
161                         vinitvmio(vp, 0, HAMMER2_LBUFSIZE,
162                                   (int)ip->ip_data.size & HAMMER2_LBUFMASK);
163                         break;
164                 /* XXX FIFO */
165                 default:
166                         panic("hammer2: unhandled objtype %d",
167                               ip->ip_data.type);
168                         break;
169                 }
170
171                 if (ip == hmp->iroot)
172                         vsetflags(vp, VROOT);
173
174                 vp->v_data = ip;
175                 ip->vp = vp;
176                 hammer2_chain_ref(hmp, &ip->chain);     /* vp association */
177                 hammer2_inode_unlock_ex(ip);
178                 break;
179         }
180
181         /*
182          * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
183          */
184         if (hammer2_debug & 0x0002) {
185                 kprintf("igetv vp %p refs %d aux %d\n",
186                         vp, vp->v_sysref.refcnt, vp->v_auxrefs);
187         }
188         return (vp);
189 }
190
191 /*
192  * Create a new inode in the specified directory using the vattr to
193  * figure out the type of inode.
194  *
195  * If no error occurs the new inode with its chain locked is returned in
196  * *nipp, otherwise an error is returned and *nipp is set to NULL.
197  */
198 int
199 hammer2_inode_create(hammer2_mount_t *hmp,
200                      struct vattr *vap, struct ucred *cred,
201                      hammer2_inode_t *dip,
202                      const uint8_t *name, size_t name_len,
203                      hammer2_inode_t **nipp)
204 {
205         hammer2_chain_t *chain;
206         hammer2_chain_t *parent;
207         hammer2_inode_t *nip;
208         hammer2_key_t lhc;
209         int error;
210
211         lhc = hammer2_dirhash(name, name_len);
212
213         /*
214          * Locate the inode or indirect block to create the new
215          * entry in.  At the same time check for key collisions
216          * and iterate until we don't get one.
217          */
218         parent = &dip->chain;
219         hammer2_chain_ref(hmp, parent);
220         hammer2_chain_lock(hmp, parent);
221
222         error = 0;
223         while (error == 0) {
224                 chain = hammer2_chain_lookup(hmp, &parent, lhc, lhc, 0);
225                 if (chain == NULL)
226                         break;
227                 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
228                         error = ENOSPC;
229                 hammer2_chain_put(hmp, chain);
230                 chain = NULL;
231                 ++lhc;
232         }
233         if (error == 0) {
234                 chain = hammer2_chain_create(hmp, parent, NULL, lhc, 0,
235                                              HAMMER2_BREF_TYPE_INODE,
236                                              HAMMER2_INODE_BYTES);
237                 if (chain == NULL)
238                         error = EIO;
239         }
240         hammer2_chain_put(hmp, parent);
241
242         /*
243          * Handle the error case
244          */
245         if (error) {
246                 KKASSERT(chain == NULL);
247                 *nipp = NULL;
248                 return (error);
249         }
250
251         /*
252          * Set up the new inode
253          */
254         nip = chain->u.ip;
255         *nipp = nip;
256
257         nip->ip_data.type = hammer2_get_obj_type(vap->va_type);
258         nip->ip_data.inum = hmp->voldata.alloc_tid++;   /* XXX modify/lock */
259         nip->ip_data.version = HAMMER2_INODE_VERSION_ONE;
260         nip->ip_data.ctime = 0;
261         nip->ip_data.mtime = 0;
262         nip->ip_data.mode = vap->va_mode;
263         nip->ip_data.nlinks = 1;
264         /* uid, gid, etc */
265
266         /*
267          * Regular files and softlinks allow a small amount of data to be
268          * directly embedded in the inode.  This flag will be cleared if
269          * the size is extended past the embedded limit.
270          */
271         if (nip->ip_data.type == HAMMER2_OBJTYPE_REGFILE ||
272             nip->ip_data.type == HAMMER2_OBJTYPE_SOFTLINK) {
273                 nip->ip_data.op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
274         }
275
276         KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
277         bcopy(name, nip->ip_data.filename, name_len);
278         nip->ip_data.name_key = lhc;
279         nip->ip_data.name_len = name_len;
280
281         return (0);
282 }
283
284 /*
285  * Connect inode (ip) to the specified directory using the specified name.
286  * (ip) must be locked.
287  */
288 int
289 hammer2_inode_connect(hammer2_inode_t *dip, hammer2_inode_t *ip,
290                       const uint8_t *name, size_t name_len)
291 {
292         hammer2_mount_t *hmp = dip->hmp;
293         hammer2_chain_t *chain;
294         hammer2_chain_t *parent;
295         hammer2_key_t lhc;
296         int error;
297
298         lhc = hammer2_dirhash(name, name_len);
299
300         /*
301          * Locate the inode or indirect block to create the new
302          * entry in.  At the same time check for key collisions
303          * and iterate until we don't get one.
304          */
305         parent = &dip->chain;
306         hammer2_chain_ref(hmp, parent);
307         hammer2_chain_lock(hmp, parent);
308
309         error = 0;
310         while (error == 0) {
311                 chain = hammer2_chain_lookup(hmp, &parent, lhc, lhc, 0);
312                 if (chain == NULL)
313                         break;
314                 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
315                         error = ENOSPC;
316                 hammer2_chain_put(hmp, chain);
317                 chain = NULL;
318                 ++lhc;
319         }
320
321         /*
322          * Passing a non-NULL chain to hammer2_chain_create() reconnects the
323          * existing chain instead of creating a new one.  The chain's bref
324          * will be properly updated.
325          */
326         if (error == 0) {
327                 chain = hammer2_chain_create(hmp, parent, &ip->chain, lhc, 0,
328                                              HAMMER2_BREF_TYPE_INODE /* n/a */,
329                                              HAMMER2_INODE_BYTES);   /* n/a */
330                 if (chain == NULL)
331                         error = EIO;
332         }
333         hammer2_chain_put(hmp, parent);
334
335         /*
336          * Handle the error case
337          */
338         if (error) {
339                 KKASSERT(chain == NULL);
340                 return (error);
341         }
342
343         /*
344          * Directory entries are inodes so if the name has changed we have
345          * to update the inode.
346          */
347         if (ip->ip_data.name_len != name_len ||
348             bcmp(ip->ip_data.filename, name, name_len) != 0) {
349                 hammer2_chain_modify(hmp, chain);
350                 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
351                 bcopy(name, ip->ip_data.filename, name_len);
352                 ip->ip_data.name_key = lhc;
353                 ip->ip_data.name_len = name_len;
354         }
355         /*nip->ip_data.nlinks = 1;*/
356
357         return (0);
358 }
359
360 /*
361  * Create a hardlink forwarding entry (dip, name) to the specified (ip).
362  *
363  * This is one of the more complex implementations in HAMMER2.  The
364  * filesystem strictly updates its chains bottom-up in a copy-on-write
365  * fashion.  This makes hardlinks difficult to implement but we've come up
366  * with a dandy solution.
367  *
368  * When a file has more than one link the actual inode is created as a
369  * hidden directory entry (indexed by inode number) in a common parent of
370  * all hardlinks which reference the file.  The hardlinks in each directory
371  * are merely forwarding entries to the hidden inode.
372  *
373  * Implementation:
374  *
375  *      Most VOPs can be blissfully unaware of the forwarding entries.
376  *      nresolve, nlink, and remove code have to be forwarding-aware
377  *      in order to return the (ip/vp) for the actual file (and otherwise do
378  *      the right thing).
379  *
380  *      (1) If the ip we are linking to is a normal embedded inode (nlinks==1)
381  *          we have to replace the directory entry with a forwarding inode
382  *          and move the normal ip/vp to a hidden entry indexed by the inode
383  *          number in a common parent directory.
384  *
385  *      (2) If the ip we are linking to is already a hidden entry but is not
386  *          a common parent we have to move its entry to a common parent by
387  *          moving the entry upward.
388  *
389  *      (3) The trivial case is the entry is already hidden and already a
390  *          common parent.  We adjust nlinks for the entry and are done.
391  *          (this is the fall-through case).
392  */
393 int
394 hammer2_hardlink_create(hammer2_inode_t *ip, hammer2_inode_t *dip,
395                         const uint8_t *name, size_t name_len)
396 {
397         return ENOTSUP;
398 #if 0
399         hammer2_inode_t *nip;
400         hammer2_inode_t *xip;
401
402
403        hammer2_inode_t *nip;   /* hardlink forwarding inode */
404         error = hammer2_inode_create(hmp, NULL, ap->a_cred,
405                                      dip, name, name_len, &nip);
406         if (error) {
407                 KKASSERT(nip == NULL);
408                 return error;
409         }
410         KKASSERT(nip->ip_data.type == HAMMER2_OBJTYPE_HARDLINK);
411         hammer2_chain_modify(&nip->chain);
412         nip->ip_data.inum = ip->ip_data.inum;
413         hammer2_chain_put(hmp, &nip->chain);
414         /
415 #endif
416 }
417
418 int
419 hammer2_inode_calc_alloc(hammer2_key_t filesize)
420 {
421         int frag = (int)filesize & HAMMER2_PBUFMASK;
422         int radix;
423
424         if (frag == 0)
425                 return(0);
426         for (radix = HAMMER2_MINIORADIX; frag > (1 << radix); ++radix)
427                 ;
428         return (radix);
429 }