LCOV - code coverage report
Current view: top level - fs/xfs/scrub - xfblob.c (source / functions) Hit Total Coverage
Test: fstests of 6.5.0-rc3-achx @ Mon Jul 31 20:08:12 PDT 2023 Lines: 52 65 80.0 %
Date: 2023-07-31 20:08:12 Functions: 7 7 100.0 %

          Line data    Source code
       1             : // SPDX-License-Identifier: GPL-2.0-or-later
       2             : /*
       3             :  * Copyright (C) 2021-2023 Oracle.  All Rights Reserved.
       4             :  * Author: Darrick J. Wong <djwong@kernel.org>
       5             :  */
       6             : #include "xfs.h"
       7             : #include "xfs_fs.h"
       8             : #include "xfs_shared.h"
       9             : #include "xfs_format.h"
      10             : #include "scrub/scrub.h"
      11             : #include "scrub/xfile.h"
      12             : #include "scrub/xfarray.h"
      13             : #include "scrub/xfblob.h"
      14             : 
      15             : /*
      16             :  * XFS Blob Storage
      17             :  * ================
      18             :  * Stores and retrieves blobs using an xfile.  Objects are appended to the file
      19             :  * and the offset is returned as a magic cookie for retrieval.
      20             :  */
      21             : 
      22             : #define XB_KEY_MAGIC    0xABAADDAD
      23             : struct xb_key {
      24             :         uint32_t                xb_magic;  /* XB_KEY_MAGIC */
      25             :         uint32_t                xb_size;   /* size of the blob, in bytes */
      26             :         loff_t                  xb_offset; /* byte offset of this key */
      27             :         /* blob comes after here */
      28             : } __packed;
      29             : 
      30             : /* Initialize a blob storage object. */
      31             : int
      32   217943487 : xfblob_create(
      33             :         const char              *description,
      34             :         struct xfblob           **blobp)
      35             : {
      36   217943487 :         struct xfblob           *blob;
      37   217943487 :         struct xfile            *xfile;
      38   217943487 :         int                     error;
      39             : 
      40   217943487 :         error = xfile_create(description, 0, &xfile);
      41   218697712 :         if (error)
      42             :                 return error;
      43             : 
      44   218701077 :         blob = kmalloc(sizeof(struct xfblob), XCHK_GFP_FLAGS);
      45   218644681 :         if (!blob) {
      46           0 :                 error = -ENOMEM;
      47           0 :                 goto out_xfile;
      48             :         }
      49             : 
      50   218644681 :         blob->xfile = xfile;
      51   218644681 :         blob->last_offset = PAGE_SIZE;
      52             : 
      53   218644681 :         *blobp = blob;
      54   218644681 :         return 0;
      55             : 
      56             : out_xfile:
      57           0 :         xfile_destroy(xfile);
      58           0 :         return error;
      59             : }
      60             : 
      61             : /* Destroy a blob storage object. */
      62             : void
      63   217821282 : xfblob_destroy(
      64             :         struct xfblob   *blob)
      65             : {
      66   217821282 :         xfile_destroy(blob->xfile);
      67   218131670 :         kfree(blob);
      68   218105895 : }
      69             : 
      70             : /* Retrieve a blob. */
      71             : int
      72    32656672 : xfblob_load(
      73             :         struct xfblob   *blob,
      74             :         xfblob_cookie   cookie,
      75             :         void            *ptr,
      76             :         uint32_t        size)
      77             : {
      78    32656672 :         struct xb_key   key;
      79    32656672 :         int             error;
      80             : 
      81    32656672 :         error = xfile_obj_load(blob->xfile, &key, sizeof(key), cookie);
      82    32848737 :         if (error)
      83             :                 return error;
      84             : 
      85    32848737 :         if (key.xb_magic != XB_KEY_MAGIC || key.xb_offset != cookie) {
      86           0 :                 ASSERT(0);
      87           0 :                 return -ENODATA;
      88             :         }
      89    32848737 :         if (size < key.xb_size) {
      90           0 :                 ASSERT(0);
      91           0 :                 return -EFBIG;
      92             :         }
      93             : 
      94    32848737 :         return xfile_obj_load(blob->xfile, ptr, key.xb_size,
      95    32848737 :                         cookie + sizeof(key));
      96             : }
      97             : 
      98             : /* Store a blob. */
      99             : int
     100  1788249648 : xfblob_store(
     101             :         struct xfblob   *blob,
     102             :         xfblob_cookie   *cookie,
     103             :         const void      *ptr,
     104             :         uint32_t        size)
     105             : {
     106  1788249648 :         struct xb_key   key = {
     107  1788249648 :                 .xb_offset = blob->last_offset,
     108             :                 .xb_magic = XB_KEY_MAGIC,
     109             :                 .xb_size = size,
     110             :         };
     111  1788249648 :         loff_t          pos = blob->last_offset;
     112  1788249648 :         int             error;
     113             : 
     114  1788249648 :         error = xfile_obj_store(blob->xfile, &key, sizeof(key), pos);
     115  1763045783 :         if (error)
     116             :                 return error;
     117             : 
     118  1763502815 :         pos += sizeof(key);
     119  1763502815 :         error = xfile_obj_store(blob->xfile, ptr, size, pos);
     120  1771892883 :         if (error)
     121           0 :                 goto out_err;
     122             : 
     123  1771892883 :         *cookie = blob->last_offset;
     124  1771892883 :         blob->last_offset += sizeof(key) + size;
     125  1771892883 :         return 0;
     126             : out_err:
     127           0 :         xfile_discard(blob->xfile, blob->last_offset, sizeof(key));
     128           0 :         return error;
     129             : }
     130             : 
     131             : /* Free a blob. */
     132             : int
     133     6973473 : xfblob_free(
     134             :         struct xfblob   *blob,
     135             :         xfblob_cookie   cookie)
     136             : {
     137     6973473 :         struct xb_key   key;
     138     6973473 :         int             error;
     139             : 
     140     6973473 :         error = xfile_obj_load(blob->xfile, &key, sizeof(key), cookie);
     141     6973481 :         if (error)
     142             :                 return error;
     143             : 
     144     6973481 :         if (key.xb_magic != XB_KEY_MAGIC || key.xb_offset != cookie) {
     145           0 :                 ASSERT(0);
     146           0 :                 return -ENODATA;
     147             :         }
     148             : 
     149     6973481 :         xfile_discard(blob->xfile, cookie, sizeof(key) + key.xb_size);
     150     6973481 :         return 0;
     151             : }
     152             : 
     153             : /* How many bytes is this blob storage object consuming? */
     154             : unsigned long long
     155 35741430249 : xfblob_bytes(
     156             :         struct xfblob           *blob)
     157             : {
     158 35741430249 :         return xfile_bytes(blob->xfile);
     159             : }
     160             : 
     161             : /* Drop all the blobs. */
     162             : void
     163   193861384 : xfblob_truncate(
     164             :         struct xfblob   *blob)
     165             : {
     166   193861384 :         xfile_discard(blob->xfile, PAGE_SIZE, MAX_LFS_FILESIZE - PAGE_SIZE);
     167   194171803 :         blob->last_offset = PAGE_SIZE;
     168   194171803 : }

Generated by: LCOV version 1.14