Commit Graph

219 Commits

Author SHA1 Message Date
Rado Vrbovsky 6146ba8ff0 Merge: CVE-2022-48989: fscache: Fix oops due to race with cookie_lru and use_cookie
MR: https://gitlab.com/redhat/centos-stream/src/kernel/centos-stream-9/-/merge_requests/5525

JIRA: https://issues.redhat.com/browse/RHEL-63701
CVE: CVE-2022-48989

```
fscache: Fix oops due to race with cookie_lru and use_cookie

If a cookie expires from the LRU and the LRU_DISCARD flag is set, but
the state machine has not run yet, it's possible another thread can call
fscache_use_cookie and begin to use it.

When the cookie_worker finally runs, it will see the LRU_DISCARD flag
set, transition the cookie->state to LRU_DISCARDING, which will then
withdraw the cookie.  Once the cookie is withdrawn the object is removed
the below oops will occur because the object associated with the cookie
is now NULL.

Fix the oops by clearing the LRU_DISCARD bit if another thread uses the
cookie before the cookie_worker runs.

  BUG: kernel NULL pointer dereference, address: 0000000000000008
  ...
  CPU: 31 PID: 44773 Comm: kworker/u130:1 Tainted: G     E    6.0.0-5.dneg.x86_64 #1
  Hardware name: Google Compute Engine/Google Compute Engine, BIOS Google 08/26/2022
  Workqueue: events_unbound netfs_rreq_write_to_cache_work [netfs]
  RIP: 0010:cachefiles_prepare_write+0x28/0x90 [cachefiles]
  ...
  Call Trace:
    netfs_rreq_write_to_cache_work+0x11c/0x320 [netfs]
    process_one_work+0x217/0x3e0
    worker_thread+0x4a/0x3b0
    kthread+0xd6/0x100

Fixes: 12bb21a29c19 ("fscache: Implement cookie user counting and resource pinning")
Reported-by: Daire Byrne <daire.byrne@gmail.com>
Signed-off-by: Dave Wysochanski <dwysocha@redhat.com>
Signed-off-by: David Howells <dhowells@redhat.com>
Tested-by: Daire Byrne <daire@dneg.com>
Link: https://lore.kernel.org/r/20221117115023.1350181-1-dwysocha@redhat.com/ # v1
Link: https://lore.kernel.org/r/20221117142915.1366990-1-dwysocha@redhat.com/ # v2
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
(cherry picked from commit b5b52de3214a29911f949459a79f6640969b5487)
```

Signed-off-by: CKI Backport Bot <cki-ci-bot+cki-gitlab-backport-bot@redhat.com>

---

<small>Created 2024-10-22 12:03 UTC by backporter - [KWF FAQ](https://red.ht/kernel_workflow_doc) - [Slack #team-kernel-workflow](https://redhat-internal.slack.com/archives/C04LRUPMJQ5) - [Source](https://gitlab.com/cki-project/kernel-workflow/-/blob/main/webhook/utils/backporter.py) - [Documentation](https://gitlab.com/cki-project/kernel-workflow/-/blob/main/docs/README.backporter.md) - [Report an issue](https://gitlab.com/cki-project/kernel-workflow/-/issues/new?issue%5Btitle%5D=backporter%20webhook%20issue)</small>

Approved-by: David Howells <dhowells@redhat.com>
Approved-by: Brian Foster <bfoster@redhat.com>
Approved-by: CKI KWF Bot <cki-ci-bot+kwf-gitlab-com@redhat.com>

Merged-by: Rado Vrbovsky <rvrbovsk@redhat.com>
2024-12-11 14:23:32 +00:00
CKI Backport Bot 4ec44df1aa fs/netfs/fscache_cookie: add missing "n_accesses" check
JIRA: https://issues.redhat.com/browse/RHEL-57214
CVE: CVE-2024-45000

commit f71aa06398aabc2e3eaac25acdf3d62e0094ba70
Author: Max Kellermann <max.kellermann@ionos.com>
Date:   Mon Jul 29 17:19:30 2024 +0100

    fs/netfs/fscache_cookie: add missing "n_accesses" check

    This fixes a NULL pointer dereference bug due to a data race which
    looks like this:

      BUG: kernel NULL pointer dereference, address: 0000000000000008
      #PF: supervisor read access in kernel mode
      #PF: error_code(0x0000) - not-present page
      PGD 0 P4D 0
      Oops: 0000 [#1] SMP PTI
      CPU: 33 PID: 16573 Comm: kworker/u97:799 Not tainted 6.8.7-cm4all1-hp+ #43
      Hardware name: HP ProLiant DL380 Gen9/ProLiant DL380 Gen9, BIOS P89 10/17/2018
      Workqueue: events_unbound netfs_rreq_write_to_cache_work
      RIP: 0010:cachefiles_prepare_write+0x30/0xa0
      Code: 57 41 56 45 89 ce 41 55 49 89 cd 41 54 49 89 d4 55 53 48 89 fb 48 83 ec 08 48 8b 47 08 48 83 7f 10 00 48 89 34 24 48 8b 68 20 <48> 8b 45 08 4c 8b 38 74 45 49 8b 7f 50 e8 4e a9 b0 ff 48 8b 73 10
      RSP: 0018:ffffb4e78113bde0 EFLAGS: 00010286
      RAX: ffff976126be6d10 RBX: ffff97615cdb8438 RCX: 0000000000020000
      RDX: ffff97605e6c4c68 RSI: ffff97605e6c4c60 RDI: ffff97615cdb8438
      RBP: 0000000000000000 R08: 0000000000278333 R09: 0000000000000001
      R10: ffff97605e6c4600 R11: 0000000000000001 R12: ffff97605e6c4c68
      R13: 0000000000020000 R14: 0000000000000001 R15: ffff976064fe2c00
      FS:  0000000000000000(0000) GS:ffff9776dfd40000(0000) knlGS:0000000000000000
      CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
      CR2: 0000000000000008 CR3: 000000005942c002 CR4: 00000000001706f0
      Call Trace:
       <TASK>
       ? __die+0x1f/0x70
       ? page_fault_oops+0x15d/0x440
       ? search_module_extables+0xe/0x40
       ? fixup_exception+0x22/0x2f0
       ? exc_page_fault+0x5f/0x100
       ? asm_exc_page_fault+0x22/0x30
       ? cachefiles_prepare_write+0x30/0xa0
       netfs_rreq_write_to_cache_work+0x135/0x2e0
       process_one_work+0x137/0x2c0
       worker_thread+0x2e9/0x400
       ? __pfx_worker_thread+0x10/0x10
       kthread+0xcc/0x100
       ? __pfx_kthread+0x10/0x10
       ret_from_fork+0x30/0x50
       ? __pfx_kthread+0x10/0x10
       ret_from_fork_asm+0x1b/0x30
       </TASK>
      Modules linked in:
      CR2: 0000000000000008
      ---[ end trace 0000000000000000 ]---

    This happened because fscache_cookie_state_machine() was slow and was
    still running while another process invoked fscache_unuse_cookie();
    this led to a fscache_cookie_lru_do_one() call, setting the
    FSCACHE_COOKIE_DO_LRU_DISCARD flag, which was picked up by
    fscache_cookie_state_machine(), withdrawing the cookie via
    cachefiles_withdraw_cookie(), clearing cookie->cache_priv.

    At the same time, yet another process invoked
    cachefiles_prepare_write(), which found a NULL pointer in this code
    line:

      struct cachefiles_object *object = cachefiles_cres_object(cres);

    The next line crashes, obviously:

      struct cachefiles_cache *cache = object->volume->cache;

    During cachefiles_prepare_write(), the "n_accesses" counter is
    non-zero (via fscache_begin_operation()).  The cookie must not be
    withdrawn until it drops to zero.

    The counter is checked by fscache_cookie_state_machine() before
    switching to FSCACHE_COOKIE_STATE_RELINQUISHING and
    FSCACHE_COOKIE_STATE_WITHDRAWING (in "case
    FSCACHE_COOKIE_STATE_FAILED"), but not for
    FSCACHE_COOKIE_STATE_LRU_DISCARDING ("case
    FSCACHE_COOKIE_STATE_ACTIVE").

    This patch adds the missing check.  With a non-zero access counter,
    the function returns and the next fscache_end_cookie_access() call
    will queue another fscache_cookie_state_machine() call to handle the
    still-pending FSCACHE_COOKIE_DO_LRU_DISCARD.

    Fixes: 12bb21a29c19 ("fscache: Implement cookie user counting and resource pinning")
    Signed-off-by: Max Kellermann <max.kellermann@ionos.com>
    Signed-off-by: David Howells <dhowells@redhat.com>
    Link: https://lore.kernel.org/r/20240729162002.3436763-2-dhowells@redhat.com
    cc: Jeff Layton <jlayton@kernel.org>
    cc: netfs@lists.linux.dev
    cc: linux-fsdevel@vger.kernel.org
    cc: stable@vger.kernel.org
    Signed-off-by: Christian Brauner <brauner@kernel.org>

Signed-off-by: CKI Backport Bot <cki-ci-bot+cki-gitlab-backport-bot@redhat.com>
2024-11-25 17:23:40 +00:00
CKI Backport Bot cbf662a477 fscache: Fix oops due to race with cookie_lru and use_cookie
JIRA: https://issues.redhat.com/browse/RHEL-63701
CVE: CVE-2022-48989

commit b5b52de3214a29911f949459a79f6640969b5487
Author: Dave Wysochanski <dwysocha@redhat.com>
Date:   Wed Dec 7 13:49:15 2022 +0000

    fscache: Fix oops due to race with cookie_lru and use_cookie

    If a cookie expires from the LRU and the LRU_DISCARD flag is set, but
    the state machine has not run yet, it's possible another thread can call
    fscache_use_cookie and begin to use it.

    When the cookie_worker finally runs, it will see the LRU_DISCARD flag
    set, transition the cookie->state to LRU_DISCARDING, which will then
    withdraw the cookie.  Once the cookie is withdrawn the object is removed
    the below oops will occur because the object associated with the cookie
    is now NULL.

    Fix the oops by clearing the LRU_DISCARD bit if another thread uses the
    cookie before the cookie_worker runs.

      BUG: kernel NULL pointer dereference, address: 0000000000000008
      ...
      CPU: 31 PID: 44773 Comm: kworker/u130:1 Tainted: G     E    6.0.0-5.dneg.x86_64 #1
      Hardware name: Google Compute Engine/Google Compute Engine, BIOS Google 08/26/2022
      Workqueue: events_unbound netfs_rreq_write_to_cache_work [netfs]
      RIP: 0010:cachefiles_prepare_write+0x28/0x90 [cachefiles]
      ...
      Call Trace:
        netfs_rreq_write_to_cache_work+0x11c/0x320 [netfs]
        process_one_work+0x217/0x3e0
        worker_thread+0x4a/0x3b0
        kthread+0xd6/0x100

    Fixes: 12bb21a29c19 ("fscache: Implement cookie user counting and resource pinning")
    Reported-by: Daire Byrne <daire.byrne@gmail.com>
    Signed-off-by: Dave Wysochanski <dwysocha@redhat.com>
    Signed-off-by: David Howells <dhowells@redhat.com>
    Tested-by: Daire Byrne <daire@dneg.com>
    Link: https://lore.kernel.org/r/20221117115023.1350181-1-dwysocha@redhat.com/ # v1
    Link: https://lore.kernel.org/r/20221117142915.1366990-1-dwysocha@redhat.com/ # v2
    Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

Signed-off-by: CKI Backport Bot <cki-ci-bot+cki-gitlab-backport-bot@redhat.com>
2024-10-22 12:03:22 +00:00
CKI Backport Bot 24901d3a08 fscache: delete fscache_cookie_lru_timer when fscache exits to avoid UAF
JIRA: https://issues.redhat.com/browse/RHEL-59451
CVE: CVE-2024-46786

commit 72a6e22c604c95ddb3b10b5d3bb85b6ff4dbc34f
Author: Baokun Li <libaokun1@huawei.com>
Date:   Mon Aug 26 19:20:56 2024 +0800

    fscache: delete fscache_cookie_lru_timer when fscache exits to avoid UAF

    The fscache_cookie_lru_timer is initialized when the fscache module
    is inserted, but is not deleted when the fscache module is removed.
    If timer_reduce() is called before removing the fscache module,
    the fscache_cookie_lru_timer will be added to the timer list of
    the current cpu. Afterwards, a use-after-free will be triggered
    in the softIRQ after removing the fscache module, as follows:

    ==================================================================
    BUG: unable to handle page fault for address: fffffbfff803c9e9
     PF: supervisor read access in kernel mode
     PF: error_code(0x0000) - not-present page
    PGD 21ffea067 P4D 21ffea067 PUD 21ffe6067 PMD 110a7c067 PTE 0
    Oops: Oops: 0000 [#1] PREEMPT SMP KASAN PTI
    CPU: 1 UID: 0 PID: 0 Comm: swapper/1 Tainted: G W 6.11.0-rc3 #855
    Tainted: [W]=WARN
    RIP: 0010:__run_timer_base.part.0+0x254/0x8a0
    Call Trace:
     <IRQ>
     tmigr_handle_remote_up+0x627/0x810
     __walk_groups.isra.0+0x47/0x140
     tmigr_handle_remote+0x1fa/0x2f0
     handle_softirqs+0x180/0x590
     irq_exit_rcu+0x84/0xb0
     sysvec_apic_timer_interrupt+0x6e/0x90
     </IRQ>
     <TASK>
     asm_sysvec_apic_timer_interrupt+0x1a/0x20
    RIP: 0010:default_idle+0xf/0x20
     default_idle_call+0x38/0x60
     do_idle+0x2b5/0x300
     cpu_startup_entry+0x54/0x60
     start_secondary+0x20d/0x280
     common_startup_64+0x13e/0x148
     </TASK>
    Modules linked in: [last unloaded: netfs]
    ==================================================================

    Therefore delete fscache_cookie_lru_timer when removing the fscahe module.

    Fixes: 12bb21a29c19 ("fscache: Implement cookie user counting and resource pinning")
    Cc: stable@kernel.org
    Signed-off-by: Baokun Li <libaokun1@huawei.com>
    Link: https://lore.kernel.org/r/20240826112056.2458299-1-libaokun@huaweicloud.com
    Acked-by: David Howells <dhowells@redhat.com>
    Signed-off-by: Christian Brauner <brauner@kernel.org>

Signed-off-by: CKI Backport Bot <cki-ci-bot+cki-gitlab-backport-bot@redhat.com>
2024-09-18 12:04:39 +00:00
Pavel Reichl f6925fab19 netfs, fscache: export fscache_put_volume() and add fscache_try_get_volume()
Conflicts: Mismatch of file names from fs/fscache/{internal.h, volume.c}
to fs/netfs/{internal.h, fscache_volume.c} caused by the absence of
upstream commit 47757ea83a54 ("netfs, fscache: Move fs/fscache/* into
fs/netfs/").

JIRA: https://issues.redhat.com/browse/RHEL-51157
JIRA: https://issues.redhat.com/browse/RHEL-51163

Export fscache_put_volume() and add fscache_try_get_volume()
helper function to allow cachefiles to get/put fscache_volume
via linux/fscache-cache.h.

Signed-off-by: Baokun Li <libaokun1@huawei.com>
Link: https://lore.kernel.org/r/20240628062930.2467993-2-libaokun@huaweicloud.com
Signed-off-by: Christian Brauner <brauner@kernel.org>
(cherry picked from commit 85b08b31a22b481ec6528130daf94eee4452e23f)
Signed-off-by: Pavel Reichl <preichl@redhat.com>
2024-08-01 17:40:55 +02:00
Baoquan He 78ef223d06 use less confusing names for iov_iter direction initializers
JIRA: https://issues.redhat.com/browse/RHEL-32199

Upstream Status: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git

Note: The core of this patch was already backported in commit 0d33f8e1f3.
      And later commit a9e6d7970e back ported cifs part. This patch tries
      to back port the remaining parts which doesn't have conficts. This
      change is easing code reading and understanding, not related to
      functionality or features, hence leave those parts to module developer
      to back port when dependency is met.

commit de4eda9de2d957ef2d6a8365a01e26a435e958cb
Author: Al Viro <viro@zeniv.linux.org.uk>
Date:   Thu Sep 15 20:25:47 2022 -0400

    use less confusing names for iov_iter direction initializers

    READ/WRITE proved to be actively confusing - the meanings are
    "data destination, as used with read(2)" and "data source, as
    used with write(2)", but people keep interpreting those as
    "we read data from it" and "we write data to it", i.e. exactly
    the wrong way.

    Call them ITER_DEST and ITER_SOURCE - at least that is harder
    to misinterpret...

    Signed-off-by: Al Viro <viro@zeniv.linux.org.uk>

Signed-off-by: Baoquan He <bhe@redhat.com>
2024-05-15 13:56:26 +08:00
Pavel Reichl 6436dda66e netfs, fscache: Prevent Oops in fscache_put_cache()
This function dereferences "cache" and then checks if it's
IS_ERR_OR_NULL().  Check first, then dereference.

Fixes: 9549332df4ed ("fscache: Implement cache registration")
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
Signed-off-by: David Howells <dhowells@redhat.com>
Link: https://lore.kernel.org/r/e84bc740-3502-4f16-982a-a40d5676615c@moroto.mountain/ # v2
(cherry picked from commit 3be0b3ed1d76c6703b9ee482b55f7e01c369cc68)

JIRA: https://issues.redhat.com/browse/RHEL-28890
CVE: CVE-2024-26612
Signed-off-by: Pavel Reichl <preichl@redhat.com>
2024-03-16 01:17:58 +01:00
Chris von Recklinghausen 6c04cf960a fscache: Convert fscache_set_page_dirty() to fscache_dirty_folio()
Conflicts:
	drop changes to fs/9p/vfs_addr.c - unsupported fs
	fs/afs/file.c - We already have
		bc899ee1c898 ("netfs: Add a netfs inode context")
		so just remove the .set_page_dirty entry and replace it with
		the .dirty_folio entry

Bugzilla: https://bugzilla.redhat.com/2120352

commit 8fb72b4a76933ae6f86725cc8e4a8190ba84d755
Author: Matthew Wilcox (Oracle) <willy@infradead.org>
Date:   Wed Feb 9 20:22:01 2022 +0000

    fscache: Convert fscache_set_page_dirty() to fscache_dirty_folio()

    Convert all users of fscache_set_page_dirty to use fscache_dirty_folio.

    Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
    Tested-by: Damien Le Moal <damien.lemoal@opensource.wdc.com>
    Acked-by: Damien Le Moal <damien.lemoal@opensource.wdc.com>
    Tested-by: Mike Marshall <hubcap@omnibond.com> # orangefs
    Tested-by: David Howells <dhowells@redhat.com> # afs

Signed-off-by: Chris von Recklinghausen <crecklin@redhat.com>
2022-10-12 07:27:48 -04:00
Jeffrey Layton 2015d8489d fscache: add tracepoint when failing cookie
Bugzilla: http://bugzilla.redhat.com/1229736

commit 1a1e3aca9d4957e282945cdc2b58e7c560b8e0d2
Author: Jeff Layton <jlayton@kernel.org>
Date:   Fri Aug 5 06:43:48 2022 -0400

    fscache: add tracepoint when failing cookie

    Signed-off-by: Jeff Layton <jlayton@kernel.org>
    Signed-off-by: David Howells <dhowells@redhat.com>

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:32:28 -04:00
Jeffrey Layton 1c1bf4ef81 fscache: don't leak cookie access refs if invalidation is in progress or failed
Bugzilla: http://bugzilla.redhat.com/1229736

commit fb24771faf72a2fd62b3b6287af3c610c3ec9cf1
Author: Jeff Layton <jlayton@kernel.org>
Date:   Fri Aug 5 06:42:45 2022 -0400

    fscache: don't leak cookie access refs if invalidation is in progress or failed

    It's possible for a request to invalidate a fscache_cookie will come in
    while we're already processing an invalidation. If that happens we
    currently take an extra access reference that will leak. Only call
    __fscache_begin_cookie_access if the FSCACHE_COOKIE_DO_INVALIDATE bit
    was previously clear.

    Also, ensure that we attempt to clear the bit when the cookie is
    "FAILED" and put the reference to avoid an access leak.

    Fixes: 85e4ea1049c7 ("fscache: Fix invalidation/lookup race")
    Suggested-by: David Howells <dhowells@redhat.com>
    Signed-off-by: Jeff Layton <jlayton@kernel.org>
    Signed-off-by: David Howells <dhowells@redhat.com>

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:32:28 -04:00
Jeffrey Layton 1cf947921a fscache: Fix invalidation/lookup race
Bugzilla: http://bugzilla.redhat.com/1229736

commit 85e4ea1049c70fb99de5c6057e835d151fb647da
Author: David Howells <dhowells@redhat.com>
Date:   Wed Apr 20 14:27:17 2022 +0100

    fscache: Fix invalidation/lookup race

    If an NFS file is opened for writing and closed, fscache_invalidate() will
    be asked to invalidate the file - however, if the cookie is in the
    LOOKING_UP state (or the CREATING state), then request to invalidate
    doesn't get recorded for fscache_cookie_state_machine() to do something
    with.

    Fix this by making __fscache_invalidate() set a flag if it sees the cookie
    is in the LOOKING_UP state to indicate that we need to go to invalidation.
    Note that this requires a count on the n_accesses counter for the state
    machine, which that will release when it's done.

    fscache_cookie_state_machine() then shifts to the INVALIDATING state if it
    sees the flag.

    Without this, an nfs file can get corrupted if it gets modified locally and
    then read locally as the cache contents may not get updated.

    Fixes: d24af13e2e23 ("fscache: Implement cookie invalidation")
    Reported-by: Max Kellermann <mk@cm4all.com>
    Signed-off-by: David Howells <dhowells@redhat.com>
    Tested-by: Max Kellermann <mk@cm4all.com>
    Link: https://lore.kernel.org/r/YlWWbpW5Foynjllo@rabbit.intern.cm-ag [1]

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:32:28 -04:00
Jeffrey Layton a977c5f4b1 fscache: Introduce fscache_cookie_is_dropped()
Bugzilla: http://bugzilla.redhat.com/1229736

commit 5c4588aea6675b69e328d468c5b6be5127e19a79
Author: Yue Hu <zbestahu@gmail.com>
Date:   Wed May 25 10:51:50 2022 +0800

    fscache: Introduce fscache_cookie_is_dropped()

    FSCACHE_COOKIE_STATE_DROPPED will be read more than once, so let's add a
    helper to avoid code duplication.

    Signed-off-by: Yue Hu <huyue2@coolpad.com>
    Signed-off-by: David Howells <dhowells@redhat.com>
    Link: https://listman.redhat.com/archives/linux-cachefs/2022-May/006919.html

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:32:28 -04:00
Jeffrey Layton 4e5007697b fscache: Fix if condition in fscache_wait_on_volume_collision()
Bugzilla: http://bugzilla.redhat.com/1229736

commit bf17455b9cbd4b10bf30d39c047307e1d774fb1a
Author: Yue Hu <huyue2@coolpad.com>
Date:   Wed May 25 10:32:22 2022 +0800

    fscache: Fix if condition in fscache_wait_on_volume_collision()

    After waiting for the volume to complete the acquisition with timeout,
    the if condition under which potential volume collision occurs should be
    acquire the volume is still pending rather than not pending so that we
    will continue to wait until the pending flag is cleared. Also, use the
    existing test pending wrapper directly instead of test_bit().

    Fixes: 62ab63352350 ("fscache: Implement volume registration")
    Signed-off-by: Yue Hu <huyue2@coolpad.com>
    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Gao Xiang <hsiangkao@linux.alibaba.com>
    Reviewed-by: Jeffle Xu <jefflexu@linux.alibaba.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    Link: https://listman.redhat.com/archives/linux-cachefs/2022-May/006918.html

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:32:28 -04:00
Jeffrey Layton 8fe7159362 fscache: remove FSCACHE_OLD_API Kconfig option
Bugzilla: http://bugzilla.redhat.com/1229736

commit 61132ceeda723d2c48cbc2610ca3213a7fcb083b
Author: Yue Hu <huyue2@coolpad.com>
Date:   Thu Mar 31 19:57:18 2022 +0800

    fscache: remove FSCACHE_OLD_API Kconfig option

    Commit 01491a756578 ("fscache, cachefiles: Disable configuration") added
    the FSCACHE_OLD_API configuration when rewritten. Now, it's not used any
    more. Remove it.

    Signed-off-by: Yue Hu <huyue2@coolpad.com>
    Signed-off-by: David Howells <dhowells@redhat.com>
    cc: linux-cachefs@redhat.com
    Link: https://listman.redhat.com/archives/linux-cachefs/2022-March/006647.html # v1

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:32:26 -04:00
Jeffrey Layton 756f96ac81 fscache: Use wrapper fscache_set_cache_state() directly when relinquishing
Bugzilla: http://bugzilla.redhat.com/1229736

commit b3c958c20a61fb8514fa16e3edcb421703600ee0
Author: Yue Hu <huyue2@coolpad.com>
Date:   Fri Apr 1 14:37:15 2022 +0800

    fscache: Use wrapper fscache_set_cache_state() directly when relinquishing

    We already have the wrapper function to set cache state.

    Signed-off-by: Yue Hu <huyue2@coolpad.com>
    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeffle Xu <jefflexu@linux.alibaba.com>
    cc: linux-cachefs@redhat.com
    Link: https://listman.redhat.com/archives/linux-cachefs/2022-April/006648.html # v1

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:32:26 -04:00
Jeffrey Layton d9e79e7864 fscache: Move fscache_cookies_seq_ops specific code under CONFIG_PROC_FS
Bugzilla: http://bugzilla.redhat.com/1229736

commit 19517e53740ec671c335f05089abe1f0720103c7
Author: Yue Hu <huyue2@coolpad.com>
Date:   Sat Apr 2 12:47:43 2022 +0800

    fscache: Move fscache_cookies_seq_ops specific code under CONFIG_PROC_FS

    fscache_cookies_seq_ops is only used in proc.c that is compiled under
    enabled CONFIG_PROC_FS, so move related code under this config. The
    same case exsits in internal.h.

    Also, make fscache_lru_cookie_timeout static due to no user outside
    of cookie.c.

    Signed-off-by: Yue Hu <huyue2@coolpad.com>
    Signed-off-by: David Howells <dhowells@redhat.com>
    cc: linux-cachefs@redhat.com
    Link: https://listman.redhat.com/archives/linux-cachefs/2022-April/006649.html # v1

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:32:26 -04:00
Jeffrey Layton 84a68faadf fscache: Remove the cookie parameter from fscache_clear_page_bits()
Bugzilla: http://bugzilla.redhat.com/1229736
Conflicts: Documentation conflicts

commit 2c547f299827c12244d613eb2ee3616d88f56088
Author: Yue Hu <huyue2@coolpad.com>
Date:   Wed Apr 6 11:50:17 2022 +0800

    fscache: Remove the cookie parameter from fscache_clear_page_bits()

    The cookie is not used at all, remove it and update the usage in io.c
    and afs/write.c (which is the only user outside of fscache currently)
    at the same time.

    [DH: Amended the documentation also]

    Signed-off-by: Yue Hu <huyue2@coolpad.com>
    Signed-off-by: David Howells <dhowells@redhat.com>
    cc: linux-cachefs@redhat.com
    Link: https://listman.redhat.com/archives/linux-cachefs/2022-April/006659.html

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:32:26 -04:00
Jeffrey Layton 6ddfb06158 fscache: export fscache_end_operation()
Bugzilla: http://bugzilla.redhat.com/1229736

commit e9b57aaae605eb869a628fdc21fe7d2c77a2205d
Author: Jeffle Xu <jefflexu@linux.alibaba.com>
Date:   Wed Feb 9 14:00:47 2022 +0800

    fscache: export fscache_end_operation()

    Export fscache_end_operation() to avoid code duplication.

    Besides, considering the paired fscache_begin_read_operation() is
    already exported, it shall make sense to also export
    fscache_end_operation().

    Signed-off-by: Jeffle Xu <jefflexu@linux.alibaba.com>
    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com

    Link: https://lore.kernel.org/r/20220302125134.131039-2-jefflexu@linux.alibaba.com/ # Jeffle's v4
    Link: https://lore.kernel.org/r/164622971432.3564931.12184135678781328146.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/164678190346.1200972.7453733431978569479.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/164692888334.2099075.5166283293894267365.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/20220316131723.111553-2-jefflexu@linux.alibaba.com/ # v5

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:32:24 -04:00
Jeffrey Layton a16dbfc982 fscache: Fix the volume collision wait condition
Bugzilla: http://bugzilla.redhat.com/1229736

commit 80a00ab8344f0fe4d555a1f97960215b659436e9
Author: David Howells <dhowells@redhat.com>
Date:   Fri Jan 14 13:30:17 2022 +0000

    fscache: Fix the volume collision wait condition

    The condition that the waits in fscache_wait_on_volume_collision() are
    waiting until are inverted.  This suddenly started happening on the
    upstream kernel with something like the following appearing in dmesg when
    running xfstests:

            CacheFiles: cachefiles: Inode already in use: Iafs,example.com,100055

    Fix them by inverting the conditions.

    Fixes: 62ab63352350 ("fscache: Implement volume registration")
    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/164251398010.3435901.943876048104930939.stgit@warthog.procyon.org.uk/ # v1

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:32:23 -04:00
Jeffrey Layton 290c3e33d9 fscache: Add a tracepoint for cookie use/unuse
Bugzilla: http://bugzilla.redhat.com/1229736

commit e6435f1e02f410e3507f02a37c0fbb17971ddc7c
Author: David Howells <dhowells@redhat.com>
Date:   Mon Dec 6 15:54:04 2021 +0000

    fscache: Add a tracepoint for cookie use/unuse

    Add a tracepoint to track fscache_use/unuse_cookie().

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/164021588628.640689.12942919367404043608.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:32:22 -04:00
Jeffrey Layton 9450678eca nfs: Implement cache I/O by accessing the cache directly
Bugzilla: http://bugzilla.redhat.com/1229736

commit 16f2f4e679cfdaa9552574484f104014908a76c6
Author: David Howells <dhowells@redhat.com>
Date:   Fri Aug 27 15:19:34 2021 +0100

    nfs: Implement cache I/O by accessing the cache directly

    Move NFS to using fscache DIO API instead of the old upstream I/O API as
    that has been removed.  This is a stopgap solution as the intention is that
    at sometime in the future, the cache will move to using larger blocks and
    won't be able to store individual pages in order to deal with the potential
    for data corruption due to the backing filesystem being able insert/remove
    bridging blocks of zeros into its extent list[1].

    NFS then reads and writes cache pages synchronously and one page at a time.

    The preferred change would be to use the netfs lib, but the new I/O API can
    be used directly.  It's just that as the cache now needs to track data for
    itself, caching blocks may exceed page size...

    This code is somewhat borrowed from my "fallback I/O" patchset[2].

    Changes
    =======
    ver #3:
     - Restore lost =n fallback for nfs_fscache_release_page()[2].

    Signed-off-by: David Howells <dhowells@redhat.com>
    Tested-by: Dave Wysochanski <dwysocha@redhat.com>
    Acked-by: Jeff Layton <jlayton@kernel.org>
    cc: Trond Myklebust <trond.myklebust@hammerspace.com>
    cc: Anna Schumaker <anna.schumaker@netapp.com>
    cc: linux-nfs@vger.kernel.org
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/YO17ZNOcq+9PajfQ@mit.edu [1]
    Link: https://lore.kernel.org/r/202112100957.2oEDT20W-lkp@intel.com/ [2]
    Link: https://lore.kernel.org/r/163189108292.2509237.12615909591150927232.stgit@warthog.procyon.org.uk/ [2]
    Link: https://lore.kernel.org/r/163906981318.143852.17220018647843475985.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967184451.1823006.6450645559828329590.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021577632.640689.11069627070150063812.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:32:22 -04:00
Jeffrey Layton aed72b69e2 fscache, cachefiles: Display stat of culling events
Bugzilla: http://bugzilla.redhat.com/1229736

commit 9f08ebc3438baaaefcc79654b330209b83397f17
Author: David Howells <dhowells@redhat.com>
Date:   Fri Oct 22 09:17:58 2021 +0100

    fscache, cachefiles: Display stat of culling events

    Add a stat counter of culling events whereby the cache backend culls a file
    to make space (when asked by cachefilesd in this case) and display in
    /proc/fs/fscache/stats.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819654165.215744.3797804661644212436.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906961387.143852.9291157239960289090.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967168266.1823006.14436200166581605746.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021567619.640689.4339228906248763197.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:36 -04:00
Jeffrey Layton f460ff00c8 fscache, cachefiles: Display stats of no-space events
Bugzilla: http://bugzilla.redhat.com/1229736

commit 3929eca769b5a231010b4978acc61c0735da198f
Author: David Howells <dhowells@redhat.com>
Date:   Thu Oct 21 21:58:29 2021 +0100

    fscache, cachefiles: Display stats of no-space events

    Add stat counters of no-space events that caused caching not to happen and
    display in /proc/fs/fscache/stats.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819653216.215744.17210522251617386509.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906958369.143852.7257100711818401748.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967166917.1823006.14842444049198947892.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021566184.640689.4417328329632709265.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:36 -04:00
Jeffrey Layton 2e0e76f653 fscache, cachefiles: Store the volume coherency data
Bugzilla: http://bugzilla.redhat.com/1229736

commit 32e150037dce368d129996ffe5f98217b1974d9e
Author: David Howells <dhowells@redhat.com>
Date:   Tue Dec 14 09:51:43 2021 +0000

    fscache, cachefiles: Store the volume coherency data

    Store the volume coherency data in an xattr and check it when we rebind the
    volume.  If it doesn't match the cache volume is moved to the graveyard and
    rebuilt anew.

    Changes
    =======
    ver #4:
     - Remove a couple of debugging prints.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    Link: https://lore.kernel.org/r/163967164397.1823006.2950539849831291830.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021563138.640689.15851092065380543119.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:36 -04:00
Jeffrey Layton 8465b805cc fscache: Provide a function to resize a cookie
Bugzilla: http://bugzilla.redhat.com/1229736

commit 16a96bdf92d5af06f9fa6a01a4b08e2fdfed2e5b
Author: David Howells <dhowells@redhat.com>
Date:   Wed Oct 20 14:06:34 2021 +0100

    fscache: Provide a function to resize a cookie

    Provide a function to change the size of the storage attached to a cookie,
    to match the size of the file being cached when it's changed by truncate or
    fallocate:

            void fscache_resize_cookie(struct fscache_cookie *cookie,
                                       loff_t new_size);

    This acts synchronously and is expected to run under the inode lock of the
    caller.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819621839.215744.7895597119803515402.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906922387.143852.16394459879816147793.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967128998.1823006.10740669081985775576.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021527861.640689.3466382085497236267.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:34 -04:00
Jeffrey Layton 777e571cdd vfs, fscache: Implement pinning of cache usage for writeback
Bugzilla: http://bugzilla.redhat.com/1229736

commit 08276bdae68b022a7726edf7416b6748e3df5395
Author: David Howells <dhowells@redhat.com>
Date:   Wed Oct 20 23:50:01 2021 +0100

    vfs, fscache: Implement pinning of cache usage for writeback

    Cachefiles has a problem in that it needs to keep the backing file for a
    cookie open whilst there are local modifications pending that need to be
    written to it.  However, we don't want to keep the file open indefinitely,
    as that causes EMFILE/ENFILE/ENOMEM problems.

    Reopening the cache file, however, is a problem if this is being done due
    to writeback triggered by exit().  Some filesystems will oops if we try to
    open a file in that context because they want to access current->fs or
    other resources that have already been dismantled.

    To get around this, I added the following:

     (1) An inode flag, I_PINNING_FSCACHE_WB, to be set on a network filesystem
         inode to indicate that we have a usage count on the cookie caching
         that inode.

     (2) A flag in struct writeback_control, unpinned_fscache_wb, that is set
         when __writeback_single_inode() clears the last dirty page from
         i_pages - at which point it clears I_PINNING_FSCACHE_WB and sets this
         flag.

         This has to be done here so that clearing I_PINNING_FSCACHE_WB can be
         done atomically with the check of PAGECACHE_TAG_DIRTY that clears
         I_DIRTY_PAGES.

     (3) A function, fscache_set_page_dirty(), which if it is not set, sets
         I_PINNING_FSCACHE_WB and calls fscache_use_cookie() to pin the cache
         resources.

     (4) A function, fscache_unpin_writeback(), to be called by ->write_inode()
         to unuse the cookie.

     (5) A function, fscache_clear_inode_writeback(), to be called when the
         inode is evicted, before clear_inode() is called.  This cleans up any
         lingering I_PINNING_FSCACHE_WB.

    The network filesystem can then use these tools to make sure that
    fscache_write_to_cache() can write locally modified data to the cache as
    well as to the server.

    For the future, I'm working on write helpers for netfs lib that should
    allow this facility to be removed by keeping track of the dirty regions
    separately - but that's incomplete at the moment and is also going to be
    affected by folios, one way or another, since it deals with pages

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819615157.215744.17623791756928043114.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906917856.143852.8224898306177154573.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967124567.1823006.14188359004568060298.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021524705.640689.17824932021727663017.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:34 -04:00
Jeffrey Layton 052ceaa003 fscache: Implement higher-level write I/O interface
Bugzilla: http://bugzilla.redhat.com/1229736

commit b6e16652d6c0e4f9e9b120f66966ec153f0623fc
Author: David Howells <dhowells@redhat.com>
Date:   Wed Oct 20 14:06:34 2021 +0100

    fscache: Implement higher-level write I/O interface

    Provide a higher-level function than fscache_write() to perform a write
    from an inode's pagecache to the cache, whilst fending off concurrent
    writes by means of the PG_fscache mark on a page:

            void fscache_write_to_cache(struct fscache_cookie *cookie,
                                        struct address_space *mapping,
                                        loff_t start,
                                        size_t len,
                                        loff_t i_size,
                                        netfs_io_terminated_t term_func,
                                        void *term_func_priv,
                                        bool caching);

    If caching is false, this function does nothing except call (*term_func)()
    if given.  It assumes that, in such a case, PG_fscache will not have been
    set on the pages.

    Otherwise, if caching is true, this function requires the source pages to
    have had PG_fscache set on them before calling.  start and len define the
    region of the file to be modified and i_size indicates the new file size.
    The source pages are extracted from the mapping.

    term_func and term_func_priv work as for fscache_write().  The PG_fscache
    marks will be cleared at the end of the operation, before term_func is
    called or the function otherwise returns.

    There is an additonal helper function to clear the PG_fscache bits from a
    range of pages:

            void fscache_clear_page_bits(struct fscache_cookie *cookie,
                                         struct address_space *mapping,
                                         loff_t start, size_t len,
                                         bool caching);

    If caching is true, the pages to be managed are expected to be located on
    mapping in the range defined by start and len.  If caching is false, it
    does nothing.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819614155.215744.5528123235123721230.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906916346.143852.15632773570362489926.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967123599.1823006.12946816026724657428.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021522672.640689.4381958316198807813.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:34 -04:00
Jeffrey Layton e9bbbcc65a fscache: Provide read/write stat counters for the cache
Bugzilla: http://bugzilla.redhat.com/1229736

commit 8e7a867bb7309fbf47e8c2a68798b919fc02523f
Author: David Howells <dhowells@redhat.com>
Date:   Wed Oct 20 23:06:16 2021 +0100

    fscache: Provide read/write stat counters for the cache

    Provide read/write stat counters for the cache backend to use.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819609532.215744.10821082637727410554.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906912598.143852.12960327989649429069.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967113830.1823006.3222957649202368162.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021517502.640689.6077928311710357342.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:34 -04:00
Jeffrey Layton 33cc8aab96 fscache: Count data storage objects in a cache
Bugzilla: http://bugzilla.redhat.com/1229736

commit cdf262f29488e6c3432911ec487ea41918fcbcd7
Author: David Howells <dhowells@redhat.com>
Date:   Thu Nov 11 23:14:29 2021 +0000

    fscache: Count data storage objects in a cache

    Count the data storage objects that are currently allocated in a cache.
    This is used to pin certain cache structures until cache withdrawal is
    complete.

    Three helpers are provided to manage and make use of the count:

     (1) void fscache_count_object(struct fscache_cache *cache);

         This should be called by the cache backend to note that an object has
         been allocated and attached to the cache.

     (2) void fscache_uncount_object(struct fscache_cache *cache);

         This should be called by the backend to note that an object has been
         destroyed.  This sends a wakeup event that allows cache withdrawal to
         proceed if it was waiting for that object.

     (3) void fscache_wait_for_objects(struct fscache_cache *cache);

         This can be used by the backend to wait for all outstanding cache
         object to be destroyed.

    Each cache's counter is displayed as part of /proc/fs/fscache/caches.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819608594.215744.1812706538117388252.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906911646.143852.168184059935530127.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967111846.1823006.9868154941573671255.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021516219.640689.4934796654308958158.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:33 -04:00
Jeffrey Layton aad1d1ed21 fscache: Provide a means to begin an operation
Bugzilla: http://bugzilla.redhat.com/1229736

commit d64f4554dd177c5891c02424a8d9e80590b55b35
Author: David Howells <dhowells@redhat.com>
Date:   Wed Oct 20 14:06:34 2021 +0100

    fscache: Provide a means to begin an operation

    Provide a function to begin a read operation:

            int fscache_begin_read_operation(
                    struct netfs_cache_resources *cres,
                    struct fscache_cookie *cookie)

    This is primarily intended to be called by network filesystems on behalf of
    netfslib, but may also be called to use the I/O access functions directly.
    It attaches the resources required by the cache to cres struct from the
    supplied cookie.

    This holds access to the cache behind the cookie for the duration of the
    operation and forces cache withdrawal and cookie invalidation to perform
    synchronisation on the operation.  cres->inval_counter is set from the
    cookie at this point so that it can be compared at the end of the
    operation.

    Note that this does not guarantee that the cache state is fully set up and
    able to perform I/O immediately; looking up and creation may be left in
    progress in the background.  The operations intended to be called by the
    network filesystem, such as reading and writing, are expected to wait for
    the cookie to move to the correct state.

    This will, however, potentially sleep, waiting for a certain minimum state
    to be set or for operations such as invalidate to advance far enough that
    I/O can resume.

    Also provide a function for the cache to call to wait for the cache object
    to get to a state where it can be used for certain things:

            bool fscache_wait_for_operation(struct netfs_cache_resources *cres,
                                            enum fscache_want_stage stage);

    This looks at the cache resources provided by the begin function and waits
    for them to get to an appropriate stage.  There's a choice of wanting just
    some parameters (FSCACHE_WANT_PARAM) or the ability to do I/O
    (FSCACHE_WANT_READ or FSCACHE_WANT_WRITE).

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819603692.215744.146724961588817028.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906910672.143852.13856103384424986357.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967110245.1823006.2239170567540431836.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021513617.640689.16627329360866150606.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:33 -04:00
Jeffrey Layton df3a2ad6e4 fscache: Implement cookie invalidation
Bugzilla: http://bugzilla.redhat.com/1229736

commit d24af13e2e2358a602740c7817ea90da43d3e740
Author: David Howells <dhowells@redhat.com>
Date:   Wed Oct 20 15:53:34 2021 +0100

    fscache: Implement cookie invalidation

    Add a function to invalidate the cache behind a cookie:

            void fscache_invalidate(struct fscache_cookie *cookie,
                                    const void *aux_data,
                                    loff_t size,
                                    unsigned int flags)

    This causes any cached data for the specified cookie to be discarded.  If
    the cookie is marked as being in use, a new cache object will be created if
    possible and future I/O will use that instead.  In-flight I/O should be
    abandoned (writes) or reconsidered (reads).  Each time it is called
    cookie->inval_counter is incremented and this can be used to detect
    invalidation at the end of an I/O operation.

    The coherency data attached to the cookie can be updated and the cookie
    size should be reset.  One flag is available, FSCACHE_INVAL_DIO_WRITE,
    which should be used to indicate invalidation due to a DIO write on a
    file.  This will temporarily disable caching for this cookie.

    Changes
    =======
    ver #2:
     - Should only change to inval state if can get access to cache.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819602231.215744.11206598147269491575.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906909707.143852.18056070560477964891.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967107447.1823006.5945029409592119962.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021512640.640689.11418616313147754172.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:33 -04:00
Jeffrey Layton d4354db3e2 fscache: Implement cookie user counting and resource pinning
Bugzilla: http://bugzilla.redhat.com/1229736

commit 12bb21a29c19aae50cfad4e2bb5c943108f34a7d
Author: David Howells <dhowells@redhat.com>
Date:   Wed Oct 20 15:53:34 2021 +0100

    fscache: Implement cookie user counting and resource pinning

    Provide a pair of functions to count the number of users of a cookie (open
    files, writeback, invalidation, resizing, reads, writes), to obtain and pin
    resources for the cookie and to prevent culling for the whilst there are
    users.

    The first function marks a cookie as being in use:

            void fscache_use_cookie(struct fscache_cookie *cookie,
                                    bool will_modify);

    The caller should indicate the cookie to use and whether or not the caller
    is in a context that may modify the cookie (e.g. a file open O_RDWR).

    If the cookie is not already resourced, fscache will ask the cache backend
    in the background to do whatever it needs to look up, create or otherwise
    obtain the resources necessary to access data.  This is pinned to the
    cookie and may not be culled, though it may be withdrawn if the cache as a
    whole is withdrawn.

    The second function removes the in-use mark from a cookie and, optionally,
    updates the coherency data:

            void fscache_unuse_cookie(struct fscache_cookie *cookie,
                                      const void *aux_data,
                                      const loff_t *object_size);

    If non-NULL, the aux_data buffer and/or the object_size will be saved into
    the cookie and will be set on the backing store when the object is
    committed.

    If this removes the last usage on a cookie, the cookie is placed onto an
    LRU list from which it will be removed and closed after a couple of seconds
    if it doesn't get reused.  This prevents resource overload in the cache -
    in particular it prevents it from holding too many files open.

    Changes
    =======
    ver #2:
     - Fix fscache_unuse_cookie() to use atomic_dec_and_lock() to avoid a
       potential race if the cookie gets reused before it completes the
       unusement.
     - Added missing transition to LRU_DISCARDING state.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819600612.215744.13678350304176542741.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906907567.143852.16979631199380722019.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967106467.1823006.6790864931048582667.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021511674.640689.10084988363699111860.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:33 -04:00
Jeffrey Layton 2021c1cbef fscache: Implement simple cookie state machine
Bugzilla: http://bugzilla.redhat.com/1229736

commit 5d00e426f95e7ea036fec2a0aceb3f71d6dbdf92
Author: David Howells <dhowells@redhat.com>
Date:   Wed Oct 20 15:53:34 2021 +0100

    fscache: Implement simple cookie state machine

    Implement a very simple cookie state machine to handle lookup,
    invalidation, withdrawal, relinquishment and, to be added later, commit on
    LRU discard.

    Three cache methods are provided: ->lookup_cookie() to look up and, if
    necessary, create a data storage object; ->withdraw_cookie() to free the
    resources associated with that object and potentially delete it; and
    ->prepare_to_write(), to do prepare for changes to the cached data to be
    modified locally.

    Changes
    =======
    ver #3:
     - Fix a race between LRU discard and relinquishment whereby the former
       would override the latter and thus the latter would never happen[1].

    ver #2:
     - Don't hold n_accesses elevated whilst cache is bound to a cookie, but
       rather add a flag that prevents the state machine from being queued when
       n_accesses reaches 0.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/599331.1639410068@warthog.procyon.org.uk/ [1]
    Link: https://lore.kernel.org/r/163819599657.215744.15799615296912341745.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906903925.143852.1805855338154353867.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967105456.1823006.14730395299835841776.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021510706.640689.7961423370243272583.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:33 -04:00
Jeffrey Layton f8a5a623ff fscache: Add a function for a cache backend to note an I/O error
Bugzilla: http://bugzilla.redhat.com/1229736

commit 29f18e79fe7c5f8011befeda9be6b220a350f947
Author: David Howells <dhowells@redhat.com>
Date:   Wed Oct 20 15:00:26 2021 +0100

    fscache: Add a function for a cache backend to note an I/O error

    Add a function to the backend API to note an I/O error in a cache.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819598741.215744.891281275151382095.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906901316.143852.15225412215771586528.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967100721.1823006.16435671567428949398.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021508840.640689.11902836226570620424.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:33 -04:00
Jeffrey Layton 35a43d404b fscache: Provide and use cache methods to lookup/create/free a volume
Bugzilla: http://bugzilla.redhat.com/1229736

commit bfa22da3ed652aa15acd4246fa13a0de6dbe4a59
Author: David Howells <dhowells@redhat.com>
Date:   Wed Oct 20 15:26:17 2021 +0100

    fscache: Provide and use cache methods to lookup/create/free a volume

    Add cache methods to lookup, create and remove a volume.

    Looking up or creating the volume requires the cache pinning for access;
    freeing the volume requires the volume pinning for access.  The
    ->acquire_volume() method is used to ask the cache backend to lookup and,
    if necessary, create a volume; the ->free_volume() method is used to free
    the resources for a volume.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819597821.215744.5225318658134989949.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906898645.143852.8537799955945956818.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967099771.1823006.1455197910571061835.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021507345.640689.4073511598838843040.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:33 -04:00
Jeffrey Layton 213dc01e95 fscache: Implement functions add/remove a cache
Bugzilla: http://bugzilla.redhat.com/1229736

commit 2e0c76aee25f33c482abda6224bd87732359354d
Author: David Howells <dhowells@redhat.com>
Date:   Wed Oct 20 15:00:26 2021 +0100

    fscache: Implement functions add/remove a cache

    Implement functions to allow the cache backend to add or remove a cache:

     (1) Declare a cache to be live:

            int fscache_add_cache(struct fscache_cache *cache,
                                  const struct fscache_cache_ops *ops,
                                  void *cache_priv);

         Take a previously acquired cache cookie, set the operations table and
         private data and mark the cache open for access.

     (2) Withdraw a cache from service:

            void fscache_withdraw_cache(struct fscache_cache *cache);

         This marks the cache as withdrawn and thus prevents further
         cache-level and volume-level accesses.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819596022.215744.8799712491432238827.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906896599.143852.17049208999019262884.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967097870.1823006.3470041000971522030.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021505541.640689.1819714759326331054.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:33 -04:00
Jeffrey Layton a6583558f4 fscache: Implement cookie-level access helpers
Bugzilla: http://bugzilla.redhat.com/1229736

commit a7733fb632722a2f085f9324f14783effe268ed3
Author: David Howells <dhowells@redhat.com>
Date:   Wed Oct 20 15:53:34 2021 +0100

    fscache: Implement cookie-level access helpers

    Add a number of helper functions to manage access to a cookie, pinning the
    cache object in place for the duration to prevent cache withdrawal from
    removing it:

     (1) void fscache_init_access_gate(struct fscache_cookie *cookie);

         This function initialises the access count when a cache binds to a
         cookie.  An extra ref is taken on the access count to prevent wakeups
         while the cache is active.  We're only interested in the wakeup when a
         cookie is being withdrawn and we're waiting for it to quiesce - at
         which point the counter will be decremented before the wait.

         The FSCACHE_COOKIE_NACC_ELEVATED flag is set on the cookie to keep
         track of the extra ref in order to handle a race between
         relinquishment and withdrawal both trying to drop the extra ref.

     (2) bool fscache_begin_cookie_access(struct fscache_cookie *cookie,
                                          enum fscache_access_trace why);

         This function attempts to begin access upon a cookie, pinning it in
         place if it's cached.  If successful, it returns true and leaves a the
         access count incremented.

     (3) void fscache_end_cookie_access(struct fscache_cookie *cookie,
                                        enum fscache_access_trace why);

         This function drops the access count obtained by (2), permitting
         object withdrawal to take place when it reaches zero.

    A tracepoint is provided to track changes to the access counter on a
    cookie.

    Changes
    =======
    ver #2:
     - Don't hold n_accesses elevated whilst cache is bound to a cookie, but
       rather add a flag that prevents the state machine from being queued when
       n_accesses reaches 0.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819595085.215744.1706073049250505427.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906895313.143852.10141619544149102193.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967095980.1823006.1133648159424418877.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021503063.640689.8870918985269528670.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:33 -04:00
Jeffrey Layton 468fe999dd fscache: Implement volume-level access helpers
Bugzilla: http://bugzilla.redhat.com/1229736

commit e6acd3299badbfb5fb0231d42481d4f5dedf5599
Author: David Howells <dhowells@redhat.com>
Date:   Wed Oct 20 15:26:17 2021 +0100

    fscache: Implement volume-level access helpers

    Add a pair of helper functions to manage access to a volume, pinning the
    volume in place for the duration to prevent cache withdrawal from removing
    it:

            bool fscache_begin_volume_access(struct fscache_volume *volume,
                                             enum fscache_access_trace why);
            void fscache_end_volume_access(struct fscache_volume *volume,
                                           enum fscache_access_trace why);

    The way the access gate on the volume works/will work is:

      (1) If the cache tests as not live (state is not FSCACHE_CACHE_IS_ACTIVE),
          then we return false to indicate access was not permitted.

      (2) If the cache tests as live, then we increment the volume's n_accesses
          count and then recheck the cache liveness, ending the access if it
          ceased to be live.

      (3) When we end the access, we decrement the volume's n_accesses and wake
          up the any waiters if it reaches 0.

      (4) Whilst the cache is caching, the volume's n_accesses is kept
          artificially incremented to prevent wakeups from happening.

      (5) When the cache is taken offline, the state is changed to prevent new
          accesses, the volume's n_accesses is decremented and we wait for it to
          become 0.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819594158.215744.8285859817391683254.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906894315.143852.5454793807544710479.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967095028.1823006.9173132503876627466.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021501546.640689.9631510472149608443.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:33 -04:00
Jeffrey Layton cb1d98e5eb fscache: Implement cache-level access helpers
Bugzilla: http://bugzilla.redhat.com/1229736

commit 23e12e285a6ab7320a8bceead29cfe13190a6e3c
Author: David Howells <dhowells@redhat.com>
Date:   Wed Oct 20 15:00:26 2021 +0100

    fscache: Implement cache-level access helpers

    Add a pair of functions to pin/unpin a cache that we're wanting to do a
    high-level access to (such as creating or removing a volume):

            bool fscache_begin_cache_access(struct fscache_cache *cache,
                                            enum fscache_access_trace why);
            void fscache_end_cache_access(struct fscache_cache *cache,
                                          enum fscache_access_trace why);

    The way the access gate works/will work is:

     (1) If the cache tests as not live (state is not FSCACHE_CACHE_IS_ACTIVE),
         then we return false to indicate access was not permitted.

     (2) If the cache tests as live, then we increment the n_accesses count and
         then recheck the liveness, ending the access if it ceased to be live.

     (3) When we end the access, we decrement n_accesses and wake up the any
         waiters if it reaches 0.

     (4) Whilst the cache is caching, n_accesses is kept artificially
         incremented to prevent wakeups from happening.

     (5) When the cache is taken offline, the state is changed to prevent new
         accesses, n_accesses is decremented and we wait for n_accesses to
         become 0.

    Note that some of this is implemented in a later patch.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819593239.215744.7537428720603638088.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906893368.143852.14164004598465617981.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967093977.1823006.6967886507023056409.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021499995.640689.18286203753480287850.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:33 -04:00
Jeffrey Layton b854e3a09d fscache: Implement cookie registration
Bugzilla: http://bugzilla.redhat.com/1229736

commit 7f3283aba39a0f395700c3b5defa4ec49d9914b3
Author: David Howells <dhowells@redhat.com>
Date:   Wed Oct 20 15:53:34 2021 +0100

    fscache: Implement cookie registration

    Add functions to the fscache API to allow data file cookies to be acquired
    and relinquished by the network filesystem.  It is intended that the
    filesystem will create such cookies per-inode under a volume.

    To request a cookie, the filesystem should call:

            struct fscache_cookie *
            fscache_acquire_cookie(struct fscache_volume *volume,
                                   u8 advice,
                                   const void *index_key,
                                   size_t index_key_len,
                                   const void *aux_data,
                                   size_t aux_data_len,
                                   loff_t object_size)

    The filesystem must first have created a volume cookie, which is passed in
    here.  If it passes in NULL then the function will just return a NULL
    cookie.

    A binary key should be passed in index_key and is of size index_key_len.
    This is saved in the cookie and is used to locate the associated data in
    the cache.

    A coherency data buffer of size aux_data_len will be allocated and
    initialised from the buffer pointed to by aux_data.  This is used to
    validate cache objects when they're opened and is stored on disk with them
    when they're committed.  The data is stored in the cookie and will be
    updateable by various functions in later patches.

    The object_size must also be given.  This is also used to perform a
    coherency check and to size the backing storage appropriately.

    This function disallows a cookie from being acquired twice in parallel,
    though it will cause the second user to wait if the first is busy
    relinquishing its cookie.

    When a network filesystem has finished with a cookie, it should call:

            void
            fscache_relinquish_cookie(struct fscache_volume *volume,
                                      bool retire)

    If retire is true, any backing data will be discarded immediately.

    Changes
    =======
    ver #3:
     - fscache_hash()'s size parameter is now in bytes.  Use __le32 as the unit
       to round up to.
     - When comparing cookies, simply see if the attributes are the same rather
       than subtracting them to produce a strcmp-style return[1].
     - Add a check to see if the cookie is still hashed at the point of
       freeing.

    ver #2:
     - Don't hold n_accesses elevated whilst cache is bound to a cookie, but
       rather add a flag that prevents the state machine from being queued when
       n_accesses reaches 0.
     - Remove the unused cookie pointer field from the fscache_acquire
       tracepoint.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/CAHk-=whtkzB446+hX0zdLsdcUJsJ=8_-0S1mE_R+YurThfUbLA@mail.gmail.com/ [1]
    Link: https://lore.kernel.org/r/163819590658.215744.14934902514281054323.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906891983.143852.6219772337558577395.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967088507.1823006.12659006350221417165.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021498432.640689.12743483856927722772.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:33 -04:00
Jeffrey Layton 159f64b92f fscache: Implement volume registration
Bugzilla: http://bugzilla.redhat.com/1229736

commit 62ab63352350e881ae693a8236b35d7d0516c78b
Author: David Howells <dhowells@redhat.com>
Date:   Wed Oct 20 15:26:17 2021 +0100

    fscache: Implement volume registration

    Add functions to the fscache API to allow volumes to be acquired and
    relinquished by the network filesystem.  A volume is an index of data
    storage cache objects.  A volume is represented by a volume cookie in the
    API.  A filesystem would typically create a volume for a superblock and
    then create per-inode cookies within it.

    To request a volume, the filesystem calls:

            struct fscache_volume *
            fscache_acquire_volume(const char *volume_key,
                                   const char *cache_name,
                                   const void *coherency_data,
                                   size_t coherency_len)

    The volume_key is a printable string used to match the volume in the cache.
    It should not contain any '/' characters.  For AFS, for example, this would
    be "afs,<cellname>,<volume_id>", e.g. "afs,example.com,523001".

    The cache_name can be NULL, but if not it should be a string indicating the
    name of the cache to use if there's more than one available.

    The coherency data, if given, is an arbitrarily-sized blob that's attached
    to the volume and is compared when the volume is looked up.  If it doesn't
    match, the old volume is judged to be out of date and it and everything
    within it is discarded.

    Acquiring a volume twice concurrently is disallowed, though the function
    will wait if an old volume cookie is being relinquishing.

    When a network filesystem has finished with a volume, it should return the
    volume cookie by calling:

            void
            fscache_relinquish_volume(struct fscache_volume *volume,
                                      const void *coherency_data,
                                      bool invalidate)

    If invalidate is true, the entire volume will be discarded; if false, the
    volume will be synced and the coherency data will be updated.

    Changes
    =======
    ver #4:
     - Removed an extraneous param from kdoc on fscache_relinquish_volume()[3].

    ver #3:
     - fscache_hash()'s size parameter is now in bytes.  Use __le32 as the unit
       to round up to.
     - When comparing cookies, simply see if the attributes are the same rather
       than subtracting them to produce a strcmp-style return[2].
     - Make the coherency data an arbitrary blob rather than a u64, but don't
       store it for the moment.

    ver #2:
     - Fix error check[1].
     - Make a fscache_acquire_volume() return errors, including EBUSY if a
       conflicting volume cookie already exists.  No error is printed now -
       that's left to the netfs.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/20211203095608.GC2480@kili/ [1]
    Link: https://lore.kernel.org/r/CAHk-=whtkzB446+hX0zdLsdcUJsJ=8_-0S1mE_R+YurThfUbLA@mail.gmail.com/ [2]
    Link: https://lore.kernel.org/r/20211220224646.30e8205c@canb.auug.org.au/ [3]
    Link: https://lore.kernel.org/r/163819588944.215744.1629085755564865996.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906890630.143852.13972180614535611154.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967086836.1823006.8191672796841981763.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021495816.640689.4403156093668590217.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:32 -04:00
Jeffrey Layton c73bfda8b2 fscache: Implement cache registration
Bugzilla: http://bugzilla.redhat.com/1229736

commit 9549332df4ed4e761a1d41c83f2c25d28bb22431
Author: David Howells <dhowells@redhat.com>
Date:   Wed Oct 20 15:00:26 2021 +0100

    fscache: Implement cache registration

    Implement a register of caches and provide functions to manage it.

    Two functions are provided for the cache backend to use:

     (1) Acquire a cache cookie:

            struct fscache_cache *fscache_acquire_cache(const char *name)

         This gets the cache cookie for a cache of the specified name and moves
         it to the preparation state.  If a nameless cache cookie exists, that
         will be given this name and used.

     (2) Relinquish a cache cookie:

            void fscache_relinquish_cache(struct fscache_cache *cache);

         This relinquishes a cache cookie, cleans it and makes it available if
         it's still referenced by a network filesystem.

    Note that network filesystems don't deal with cache cookies directly, but
    rather go straight to the volume registration.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819587157.215744.13523139317322503286.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906889665.143852.10378009165231294456.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967085081.1823006.2218944206363626210.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021494847.640689.10109692261640524343.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:32 -04:00
Jeffrey Layton 81383a7440 fscache: Implement a hash function
Bugzilla: http://bugzilla.redhat.com/1229736

commit e8a07c9d22afdace966353231d0273d29efe0890
Author: David Howells <dhowells@redhat.com>
Date:   Wed Oct 20 15:45:28 2021 +0100

    fscache: Implement a hash function

    Implement a function to generate hashes.  It needs to be stable over time
    and endianness-independent as the hashes will appear on disk in future
    patches.  It can assume that its input is a multiple of four bytes in size
    and alignment.

    This is borrowed from the VFS and simplified.  le32_to_cpu() is added to
    make it endianness-independent.

    Changes
    =======
    ver #3:
     - Read the data being hashed in an endianness-independent way[1].
     - Change the size parameter to be in bytes rather than words.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/CAHk-=whtkzB446+hX0zdLsdcUJsJ=8_-0S1mE_R+YurThfUbLA@mail.gmail.com [1]
    Link: https://lore.kernel.org/r/163819586113.215744.1699465806130102367.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906888735.143852.10944614318596881429.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967082342.1823006.8915671045444488742.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021493624.640689.9990442668811178628.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:32 -04:00
Jeffrey Layton 78fa58129a fscache: Introduce new driver
Bugzilla: http://bugzilla.redhat.com/1229736

commit 1e1236b841166f1d2daf36fdf6bb3e656bc5f5ca
Author: David Howells <dhowells@redhat.com>
Date:   Wed Oct 20 14:34:41 2021 +0100

    fscache: Introduce new driver

    Introduce basic skeleton of the new, rewritten fscache driver.

    Changes
    =======
    ver #3:
     - Use remove_proc_subtree(), not remove_proc_entry() to remove a populated
       dir.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819584034.215744.4290533472390439030.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906887770.143852.3577888294989185666.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967080039.1823006.5702921801104057922.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021491014.640689.4292699878317589512.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:32 -04:00
Jeffrey Layton 5a48f4e096 fscache: Remove the contents of the fscache driver, pending rewrite
Bugzilla: http://bugzilla.redhat.com/1229736
Conflicts: Minor whitespace damage to the code in RHEL9 makes this patch
           not apply cleanly. Since we're removing it all anyway though,
	   no matter.

commit 2cee6fbb7f01bcb25f11ef1439e89a29de4c0c1d
Author: David Howells <dhowells@redhat.com>
Date:   Mon Oct 25 21:53:44 2021 +0100

    fscache: Remove the contents of the fscache driver, pending rewrite

    Remove the code that comprises the fscache driver as it's going to be
    substantially rewritten, with the majority of the code being erased in the
    rewrite.

    A small piece of linux/fscache.h is left as that is #included by a bunch of
    network filesystems.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819578724.215744.18210619052245724238.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906884814.143852.6727245089843862889.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967077097.1823006.1377665951499979089.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021485548.640689.13876080567388696162.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:32 -04:00
Jeffrey Layton cff4e0cf44 fscache, cachefiles: Disable configuration
Bugzilla: http://bugzilla.redhat.com/1229736

commit 01491a756578d8602dfdfc79f2638c519bfdadb7
Author: David Howells <dhowells@redhat.com>
Date:   Mon Oct 25 21:41:26 2021 +0100

    fscache, cachefiles: Disable configuration

    Disable fscache and cachefiles in Kconfig whilst it is rewritten.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/163819576672.215744.12444272479560406780.stgit@warthog.procyon.org.uk/ # v1
    Link: https://lore.kernel.org/r/163906882835.143852.11073015983885872901.stgit@warthog.procyon.org.uk/ # v2
    Link: https://lore.kernel.org/r/163967075113.1823006.277316290062782998.stgit@warthog.procyon.org.uk/ # v3
    Link: https://lore.kernel.org/r/164021481179.640689.2004199594774033658.stgit@warthog.procyon.org.uk/ # v4

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:32 -04:00
Jeffrey Layton 89c8cae442 fscache: Remove an unused static variable
Bugzilla: http://bugzilla.redhat.com/1229736

commit ef31499a87cf842bdf6719f44473d93e99d09fe2
Author: David Howells <dhowells@redhat.com>
Date:   Mon Sep 20 10:33:55 2021 +0100

    fscache: Remove an unused static variable

    The fscache object CREATE_OBJECT work state isn't ever referred to, so
    remove it and avoid the unused variable warning caused by W=1.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: linux-fsdevel@vger.kernel.org
    cc: linux-doc@vger.kernel.org
    Link: https://lore.kernel.org/r/163214005516.2945267.7000234432243167892.stgit@warthog.procyon.org.uk/ # rfc v1
    Link: https://lore.kernel.org/r/163281899704.2790286.9177774252843775348.stgit@warthog.procyon.org.uk/ # rfc v2

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:30 -04:00
Jeffrey Layton 92d3fa0faf fscache: Fix some kerneldoc warnings shown up by W=1
Bugzilla: http://bugzilla.redhat.com/1229736

commit d9e3f82279bfe8419f437a637ff37c075598bd91
Author: David Howells <dhowells@redhat.com>
Date:   Mon Oct 4 22:08:50 2021 +0100

    fscache: Fix some kerneldoc warnings shown up by W=1

    Fix some kerneldoc warnings in the fscache driver that are shown up by W=1.

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@kernel.org>
    cc: Mauro Carvalho Chehab <mchehab@kernel.org>
    cc: linux-fsdevel@vger.kernel.org
    cc: linux-doc@vger.kernel.org
    Link: https://lore.kernel.org/r/163214005516.2945267.7000234432243167892.stgit@warthog.procyon.org.uk/ # rfc v1
    Link: https://lore.kernel.org/r/163281899704.2790286.9177774252843775348.stgit@warthog.procyon.org.uk/ # rfc v2

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:30 -04:00
Jeffrey Layton 1ff56cad01 fscache: Use refcount_t for the cookie refcount instead of atomic_t
Bugzilla: http://bugzilla.redhat.com/1229736

commit 20ec197bfa13c5b799fc9527790ea7b5374fc8f2
Author: David Howells <dhowells@redhat.com>
Date:   Mon Mar 29 13:53:50 2021 +0100

    fscache: Use refcount_t for the cookie refcount instead of atomic_t

    Use refcount_t for the fscache_cookie refcount instead of atomic_t and
    rename the 'usage' member to 'ref' in such cases.  The tracepoints that
    reference it change from showing "u=%d" to "r=%d".

    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@redhat.com>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/162431204358.2908479.8006938388213098079.stgit@warthog.procyon.org.uk/

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:29 -04:00
Jeffrey Layton 06d9adc025 fscache: Fix fscache_cookie_put() to not deref after dec
Bugzilla: http://bugzilla.redhat.com/1229736

commit 33cba859220b0878b3b2931caa1629a3d2432379
Author: David Howells <dhowells@redhat.com>
Date:   Fri Jun 18 11:19:49 2021 +0100

    fscache: Fix fscache_cookie_put() to not deref after dec

    fscache_cookie_put() accesses the cookie it has just put inside the
    tracepoint that monitors the change - but this is something it's not
    allowed to do if we didn't reduce the count to zero.

    Fix this by dropping most of those values from the tracepoint and grabbing
    the cookie debug ID before doing the dec.

    Also take the opportunity to switch over the usage and where arguments on
    the tracepoint to put the reason last.

    Fixes: a18feb5576 ("fscache: Add tracepoints")
    Signed-off-by: David Howells <dhowells@redhat.com>
    Reviewed-by: Jeff Layton <jlayton@redhat.com>
    cc: linux-cachefs@redhat.com
    Link: https://lore.kernel.org/r/162431203107.2908479.3259582550347000088.stgit@warthog.procyon.org.uk/

Signed-off-by: Jeffrey Layton <jlayton@redhat.com>
2022-08-22 12:31:28 -04:00