hammer2 - Flesh out hardlink infrastructure
[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/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
44 /*
45  * Adding a ref to an inode is only legal if the inode already has at least
46  * one ref.
47  */
48 void
49 hammer2_inode_ref(hammer2_inode_t *ip)
50 {
51         hammer2_chain_ref(ip->hmp, &ip->chain);
52 }
53
54 /*
55  * Drop an inode reference, freeing the inode when the last reference goes
56  * away.
57  */
58 void
59 hammer2_inode_drop(hammer2_inode_t *ip)
60 {
61         hammer2_chain_drop(ip->hmp, &ip->chain);
62 }
63
64 /*
65  * Get the vnode associated with the given inode, allocating the vnode if
66  * necessary.
67  *
68  * Great care must be taken to avoid deadlocks and vnode acquisition/reclaim
69  * races.
70  *
71  * The vnode will be returned exclusively locked and referenced.  The
72  * reference on the vnode prevents it from being reclaimed.
73  *
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.
76  */
77 struct vnode *
78 hammer2_igetv(hammer2_inode_t *ip, int *errorp)
79 {
80         struct vnode *vp;
81         hammer2_pfsmount_t *pmp;
82
83         pmp = ip->pmp;
84         KKASSERT(pmp != NULL);
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, pmp->mp, &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, ip->ip_data.size,
152                                   HAMMER2_LBUFSIZE,
153                                   (int)ip->ip_data.size & HAMMER2_LBUFMASK);
154                         break;
155                 case HAMMER2_OBJTYPE_SOFTLINK:
156                         /*
157                          * XXX for now we are using the generic file_read
158                          * and file_write code so we need a buffer cache
159                          * association.
160                          */
161                         vp->v_type = VLNK;
162                         vinitvmio(vp, ip->ip_data.size,
163                                   HAMMER2_LBUFSIZE,
164                                   (int)ip->ip_data.size & HAMMER2_LBUFMASK);
165                         break;
166                 /* XXX FIFO */
167                 default:
168                         panic("hammer2: unhandled objtype %d",
169                               ip->ip_data.type);
170                         break;
171                 }
172
173                 if (ip == pmp->iroot)
174                         vsetflags(vp, VROOT);
175
176                 vp->v_data = ip;
177                 ip->vp = vp;
178                 hammer2_chain_ref(ip->hmp, &ip->chain); /* vp association */
179                 hammer2_inode_unlock_ex(ip);
180                 break;
181         }
182
183         /*
184          * Return non-NULL vp and *errorp == 0, or NULL vp and *errorp != 0.
185          */
186         if (hammer2_debug & 0x0002) {
187                 kprintf("igetv vp %p refs %d aux %d\n",
188                         vp, vp->v_sysref.refcnt, vp->v_auxrefs);
189         }
190         return (vp);
191 }
192
193 /*
194  * Create a new inode in the specified directory using the vattr to
195  * figure out the type of inode.
196  *
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.
199  *
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.
203  */
204 int
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)
209 {
210         hammer2_mount_t *hmp = dip->hmp;
211         hammer2_chain_t *chain;
212         hammer2_chain_t *parent;
213         hammer2_inode_t *nip;
214         hammer2_key_t lhc;
215         int error;
216         uid_t xuid;
217
218         lhc = hammer2_dirhash(name, name_len);
219
220         /*
221          * Locate the inode or indirect block to create the new
222          * entry in.  At the same time check for key collisions
223          * and iterate until we don't get one.
224          */
225         parent = &dip->chain;
226         hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
227
228         error = 0;
229         while (error == 0) {
230                 chain = hammer2_chain_lookup(hmp, &parent, lhc, lhc, 0);
231                 if (chain == NULL)
232                         break;
233                 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
234                         error = ENOSPC;
235                 hammer2_chain_unlock(hmp, chain);
236                 chain = NULL;
237                 ++lhc;
238         }
239         if (error == 0) {
240                 chain = hammer2_chain_create(hmp, parent, NULL, lhc, 0,
241                                              HAMMER2_BREF_TYPE_INODE,
242                                              HAMMER2_INODE_BYTES);
243                 if (chain == NULL)
244                         error = EIO;
245         }
246         hammer2_chain_unlock(hmp, parent);
247
248         /*
249          * Handle the error case
250          */
251         if (error) {
252                 KKASSERT(chain == NULL);
253                 *nipp = NULL;
254                 return (error);
255         }
256
257         /*
258          * Set up the new inode
259          */
260         nip = chain->u.ip;
261         *nipp = nip;
262
263         hammer2_voldata_lock(hmp);
264         if (vap) {
265                 nip->ip_data.type = hammer2_get_obj_type(vap->va_type);
266                 nip->ip_data.inum = hmp->voldata.alloc_tid++;
267                 /* XXX modify/lock */
268         } else {
269                 nip->ip_data.type = HAMMER2_OBJTYPE_DIRECTORY;
270                 nip->ip_data.inum = 1;
271         }
272         hammer2_voldata_unlock(hmp);
273         nip->ip_data.version = HAMMER2_INODE_VERSION_ONE;
274         hammer2_update_time(&nip->ip_data.ctime);
275         nip->ip_data.mtime = nip->ip_data.ctime;
276         if (vap)
277                 nip->ip_data.mode = vap->va_mode;
278         nip->ip_data.nlinks = 1;
279         if (vap) {
280                 if (dip) {
281                         xuid = hammer2_to_unix_xid(&dip->ip_data.uid);
282                         xuid = vop_helper_create_uid(dip->pmp->mp,
283                                                      dip->ip_data.mode,
284                                                      xuid,
285                                                      cred,
286                                                      &vap->va_mode);
287                 } else {
288                         xuid = 0;
289                 }
290                 if (vap->va_vaflags & VA_UID_UUID_VALID)
291                         nip->ip_data.uid = vap->va_uid_uuid;
292                 else if (vap->va_uid != (uid_t)VNOVAL)
293                         hammer2_guid_to_uuid(&nip->ip_data.uid, vap->va_uid);
294                 else
295                         hammer2_guid_to_uuid(&nip->ip_data.uid, xuid);
296
297                 if (vap->va_vaflags & VA_GID_UUID_VALID)
298                         nip->ip_data.gid = vap->va_gid_uuid;
299                 else if (vap->va_gid != (gid_t)VNOVAL)
300                         hammer2_guid_to_uuid(&nip->ip_data.gid, vap->va_gid);
301                 else if (dip)
302                         nip->ip_data.gid = dip->ip_data.gid;
303         }
304
305         /*
306          * Regular files and softlinks allow a small amount of data to be
307          * directly embedded in the inode.  This flag will be cleared if
308          * the size is extended past the embedded limit.
309          */
310         if (nip->ip_data.type == HAMMER2_OBJTYPE_REGFILE ||
311             nip->ip_data.type == HAMMER2_OBJTYPE_SOFTLINK) {
312                 nip->ip_data.op_flags |= HAMMER2_OPFLAG_DIRECTDATA;
313         }
314
315         KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
316         bcopy(name, nip->ip_data.filename, name_len);
317         nip->ip_data.name_key = lhc;
318         nip->ip_data.name_len = name_len;
319
320         return (0);
321 }
322
323 /*
324  * Connect inode (ip) to the specified directory using the specified name.
325  * (ip) must be locked.
326  *
327  * If (ip) represents a hidden hardlink target file then the inode we create
328  * for the directory entry will be setup as OBJTYPE_HARDLINK.
329  */
330 int
331 hammer2_inode_connect(hammer2_inode_t *dip, hammer2_inode_t *ip,
332                       const uint8_t *name, size_t name_len)
333 {
334         hammer2_mount_t *hmp = dip->hmp;
335         hammer2_chain_t *chain;
336         hammer2_chain_t *parent;
337         hammer2_key_t lhc;
338         int error;
339
340         lhc = hammer2_dirhash(name, name_len);
341
342         /*
343          * Locate the inode or indirect block to create the new
344          * entry in.  At the same time check for key collisions
345          * and iterate until we don't get one.
346          */
347         parent = &dip->chain;
348         hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
349
350         error = 0;
351         while (error == 0) {
352                 chain = hammer2_chain_lookup(hmp, &parent, lhc, lhc, 0);
353                 if (chain == NULL)
354                         break;
355                 if ((lhc & HAMMER2_DIRHASH_LOMASK) == HAMMER2_DIRHASH_LOMASK)
356                         error = ENOSPC;
357                 hammer2_chain_unlock(hmp, chain);
358                 chain = NULL;
359                 ++lhc;
360         }
361
362         /*
363          * Passing a non-NULL chain to hammer2_chain_create() reconnects the
364          * existing chain instead of creating a new one.  The chain's bref
365          * will be properly updated.
366          */
367         if (error == 0) {
368                 chain = hammer2_chain_create(hmp, parent, &ip->chain, lhc, 0,
369                                              HAMMER2_BREF_TYPE_INODE /* n/a */,
370                                              HAMMER2_INODE_BYTES);   /* n/a */
371                 if (chain == NULL)
372                         error = EIO;
373         }
374         hammer2_chain_unlock(hmp, parent);
375
376         /*
377          * Handle the error case
378          */
379         if (error) {
380                 KKASSERT(chain == NULL);
381                 return (error);
382         }
383
384         /*
385          * Directory entries are inodes so if the name has changed we have
386          * to update the inode.
387          */
388         if (ip->ip_data.name_len != name_len ||
389             bcmp(ip->ip_data.filename, name, name_len) != 0) {
390                 hammer2_chain_modify(hmp, chain, 0);
391                 KKASSERT(name_len < HAMMER2_INODE_MAXNAME);
392                 bcopy(name, ip->ip_data.filename, name_len);
393                 ip->ip_data.name_key = lhc;
394                 ip->ip_data.name_len = name_len;
395         }
396         /*nip->ip_data.nlinks = 1;*/
397
398         return (0);
399 }
400
401 /*
402  * Unlink the file from the specified directory inode.  The directory inode
403  * does not need to be locked.
404  *
405  * isdir determines whether a directory/non-directory check should be made.
406  * No check is made if isdir is set to -1.
407  *
408  * adjlinks tells unlink that we want to adjust the nlinks count of the
409  * inode.  When removing the last link for a NON forwarding entry we can
410  * just ignore the link count... no point updating the inode that we are
411  * about to dereference, it would just result in a lot of wasted I/O.
412  *
413  * However, if the entry is a forwarding entry (aka a hardlink), and adjlinks
414  * is non-zero, we have to locate the hardlink and adjust its nlinks field.
415  */
416 int
417 hammer2_unlink_file(hammer2_inode_t *dip,
418                     const uint8_t *name, size_t name_len, int isdir)
419 {
420         hammer2_mount_t *hmp;
421         hammer2_chain_t *parent;
422         hammer2_chain_t *chain;
423         hammer2_chain_t *dparent;
424         hammer2_chain_t *dchain;
425         hammer2_key_t lhc;
426         int error;
427
428         error = 0;
429
430         hmp = dip->hmp;
431         lhc = hammer2_dirhash(name, name_len);
432
433         /*
434          * Search for the filename in the directory
435          */
436         parent = &dip->chain;
437         hammer2_chain_lock(hmp, parent, HAMMER2_RESOLVE_ALWAYS);
438         chain = hammer2_chain_lookup(hmp, &parent,
439                                      lhc, lhc + HAMMER2_DIRHASH_LOMASK,
440                                      0);
441         while (chain) {
442                 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
443                     chain->u.ip &&
444                     name_len == chain->data->ipdata.name_len &&
445                     bcmp(name, chain->data->ipdata.filename, name_len) == 0) {
446                         break;
447                 }
448                 chain = hammer2_chain_next(hmp, &parent, chain,
449                                            lhc, lhc + HAMMER2_DIRHASH_LOMASK,
450                                            0);
451         }
452
453         /*
454          * Not found or wrong type (isdir < 0 disables the type check).
455          */
456         if (chain == NULL) {
457                 hammer2_chain_unlock(hmp, parent);
458                 return ENOENT;
459         }
460         if (chain->data->ipdata.type == HAMMER2_OBJTYPE_DIRECTORY &&
461             isdir == 0) {
462                 error = ENOTDIR;
463                 goto done;
464         }
465         if (chain->data->ipdata.type != HAMMER2_OBJTYPE_DIRECTORY &&
466             isdir == 1) {
467                 error = EISDIR;
468                 goto done;
469         }
470
471         /*
472          * If this is a directory the directory must be empty.  However, if
473          * isdir < 0 we are doing a rename and the directory does not have
474          * to be empty.
475          */
476         if (chain->data->ipdata.type == HAMMER2_OBJTYPE_DIRECTORY &&
477             isdir >= 0) {
478                 dparent = chain;
479                 hammer2_chain_lock(hmp, dparent, HAMMER2_RESOLVE_ALWAYS);
480                 dchain = hammer2_chain_lookup(hmp, &dparent,
481                                               0, (hammer2_key_t)-1,
482                                               HAMMER2_LOOKUP_NODATA);
483                 if (dchain) {
484                         hammer2_chain_unlock(hmp, dchain);
485                         hammer2_chain_unlock(hmp, dparent);
486                         error = ENOTEMPTY;
487                         goto done;
488                 }
489                 hammer2_chain_unlock(hmp, dparent);
490                 dparent = NULL;
491                 /* dchain NULL */
492         }
493
494         /*
495          * Found, the chain represents the inode.  Remove the parent reference
496          * to the chain.  The chain itself is no longer referenced and will
497          * be marked unmodified by hammer2_chain_delete(), avoiding unnecessary
498          * I/O.
499          */
500         hammer2_chain_delete(hmp, parent, chain);
501         /* XXX nlinks (hardlink special case) */
502         /* XXX nlinks (parent directory) */
503
504         error = 0;
505
506 done:
507         hammer2_chain_unlock(hmp, chain);
508         hammer2_chain_unlock(hmp, parent);
509
510         return error;
511 }
512
513 /*
514  * Calculate the allocation size for the file fragment straddling EOF
515  */
516 int
517 hammer2_inode_calc_alloc(hammer2_key_t filesize)
518 {
519         int frag = (int)filesize & HAMMER2_PBUFMASK;
520         int radix;
521
522         if (frag == 0)
523                 return(0);
524         for (radix = HAMMER2_MINALLOCRADIX; frag > (1 << radix); ++radix)
525                 ;
526         return (radix);
527 }
528
529 void
530 hammer2_inode_lock_nlinks(hammer2_inode_t *ip)
531 {
532         hammer2_chain_ref(ip->hmp, &ip->chain);
533 }
534
535 void
536 hammer2_inode_unlock_nlinks(hammer2_inode_t *ip)
537 {
538         hammer2_chain_drop(ip->hmp, &ip->chain);
539 }
540
541 /*
542  * Consolidate for hard link creation.  This moves the specified terminal
543  * hardlink inode to a directory common to its current directory and tdip
544  * if necessary, replacing *ipp with the new inode chain element and
545  * modifying the original inode chain element to OBJTYPE_HARDLINK.
546  *
547  * The link count is bumped if requested.
548  */
549 int
550 hammer2_hardlink_consolidate(hammer2_inode_t **ipp, hammer2_inode_t *tdip,
551                              int bumpnlinks)
552 {
553         if (hammer2_hardlink_enable < 0)
554                 return (0);
555         if (hammer2_hardlink_enable == 0)
556                 return (ENOTSUP);
557         /* XXX */
558         return (0);
559 }
560
561 /*
562  * If (*ipp) is non-NULL it points to the forward OBJTYPE_HARDLINK inode while
563  * (*chainp) points to the resolved (hidden hardlink target) inode.  In this
564  * situation when nlinks is 1 we wish to deconsolidate the hardlink, moving
565  * it back to the directory that now represents the only remaining link.
566  */
567 int
568 hammer2_hardlink_deconsolidate(hammer2_inode_t *dip, hammer2_chain_t **chainp,
569                                hammer2_inode_t **ipp)
570 {
571         if (*ipp == NULL)
572                 return (0);
573         /* XXX */
574         return (0);
575 }
576
577 /*
578  * When presented with a (*chainp) representing an inode of type
579  * OBJTYPE_HARDLINK this code will save the original inode (with a ref)
580  * in (*ipp), and then locate the hidden hardlink target in (dip) or
581  * any parent directory above (dip).  The locked (*chainp) is replaced
582  * with a new locked (*chainp) representing the hardlink target.
583  */
584 int
585 hammer2_hardlink_find(hammer2_inode_t *dip, hammer2_chain_t **chainp,
586                       hammer2_inode_t **ipp)
587 {
588         return (EIO);
589 }