nbd_trim(3)

πŸ’… nbd_trim(3) β€” the libnbd girlies' guide

okay so bestie, welcome to the nbd_trim(3) manual page from LIBNBD. yes it's a C function. no you're not gonna be scared of it. let's go πŸŽ€


✨ NAME ✨

nbd_trim β€” send a trim command to the NBD server. that's it, that's the vibe. you're telling the server "hey, delete this chunk for me" and the server either gets it or it doesn't πŸ’…


🌸 SYNOPSIS 🌸

#include <libnbd.h>

int nbd_trim (
      struct nbd_handle *h, uint64_t count,
      uint64_t offset, uint32_t flags
    );

look at that signature. four args. h is your handle, count is the size, offset is where, flags is the extras. she's neat, she's tidy, she's giving main character energy πŸ”₯


πŸ¦‹ DESCRIPTION πŸ¦‹

so what actually happens: you issue a trim command to the NBD server, and if the server supports it, it punches a hole in the backing store β€” starting at offset and ending at offset + count - 1. like literally carving out a chunk. iconic honestly ✨

the call returns when the server has acknowledged the command, or when there's an error. not before, not after, she waits her turn 🫢

heads up bestie: this will generally return an error if nbd_can_trim(3) is false, or if nbd_is_read_only(3) is true. like duh, you can't trim a read-only thing. be so serious rn 😭

also β€” and this is the part everyone forgets β€” not all servers can support a count of 4GiB or larger. nbd_get_extended_headers_negotiated(3) is your bestie here: it tells you which servers will actually parse a request bigger than 32 bits. the NBD protocol does not yet have a way for a client to learn if the server will enforce an even smaller maximum trim size β€” although a future extension may add a constraint visible in nbd_get_block_size(3). so like. watch this space ✨

about flags

the flags parameter may be 0 for no flags, or it may contain LIBNBD_CMD_FLAG_FUA β€” which means the server shouldn't return until the data has been committed to permanent storage. but only if that is supported β€” some servers literally cannot do this, check nbd_can_fua(3) first, i'm begging πŸ™ˆ


πŸ’– RETURN VALUE πŸ’–

if the call is successful, the function returns 0. slay. that's the good ending πŸ’…


πŸ”₯ ERRORS πŸ”₯

on error, -1 is returned. that's the not-so-girlboss ending πŸ’€

refer to "ERROR HANDLING" in libnbd(3) for how to get further details of the error. the lore is all in there, iykyk.

also: the following parameters must not be NULL: h. do not pass NULL. i'm not asking. for more info see "Non-NULL parameters" in libnbd(3) 🫢


⭐ NOTES ⭐

Strict call

by default, libnbd will reject attempts to use this function with parameters that are likely to result in server failure β€” like requesting an unknown command flag. rude? maybe. sensible? yes bestie πŸ’–

the nbd_set_strict_mode(3) function can be used to alter which scenarios should await a server reply rather than failing fast. so if you want to be chaotic and hear the server out, that's your girl.


πŸ‘‘ HANDLE STATE πŸ‘‘

nbd_trim can be called when the handle is in the following state:

  • Handle created, before connecting β€” ❌ error
  • Connecting β€” ❌ error
  • Connecting & handshaking (opt_mode) β€” ❌ error
  • Connected to the server β€” βœ… allowed
  • Connection shut down β€” ❌ error
  • Handle dead β€” ❌ error

so basically: connect first. there's exactly one green check and it's the one where you're actually connected. the way that every other row is just no 😭


🌈 VERSION 🌈

this function first appeared in libnbd 1.0. a legend from the beginning, no cap πŸ‘‘

if you need to test if this function is available at compile time, check if the following macro is defined:

#define LIBNBD_HAVE_NBD_TRIM 1

πŸŽ€ SEE ALSO πŸŽ€

nbd_aio_trim(3), nbd_can_fua(3), nbd_can_trim(3), nbd_create(3), nbd_get_block_size(3), nbd_get_extended_headers_negotiated(3), nbd_is_read_only(3), nbd_set_strict_mode(3), libnbd(3).

the whole friend group, fr fr 🫢


πŸ“ AUTHORS πŸ“

  • Eric Blake
  • Richard W.M. Jones

the actual masterminds behind this. respect the architects πŸ’œ


Copyright Red Hat


πŸ’œ LICENSE πŸ’œ

This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.


okay that's the whole page bestie, you ate that πŸ’… this was libnbd-1.25.8, dated 2026-10-13, and she's filed under nbd_trim(3). go punch some holes in a backing store responsibly 🌸✨

πŸ“¦ where this page comes from