Skip to content

Add a function to re-read size of the loop device#1126

Merged
vojtechtrefny merged 1 commit into
storaged-project:masterfrom
vojtechtrefny:master_loop-set-capacity
Sep 23, 2025
Merged

Add a function to re-read size of the loop device#1126
vojtechtrefny merged 1 commit into
storaged-project:masterfrom
vojtechtrefny:master_loop-set-capacity

Conversation

@vojtechtrefny
Copy link
Copy Markdown
Member

No description provided.

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @vojtechtrefny, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a critical enhancement to the libblockdev library by adding the bd_loop_set_capacity function. This new API addresses the challenge of dynamically updating the size of an active loop device when its underlying backing file changes. By providing a mechanism to force the kernel to re-evaluate the device's capacity, this change ensures that applications using loop devices can maintain accurate size information without requiring disruptive device re-initialization, thereby improving flexibility and reliability in storage management scenarios.

Highlights

  • New Functionality: A new function, bd_loop_set_capacity, has been added to the libblockdev library. This function allows users to force the kernel to re-read the size of a loop device's backing file, ensuring the device's reported capacity is up-to-date after the backing file has been resized.
  • Robust Implementation: The implementation of bd_loop_set_capacity includes error handling for device access and a retry mechanism for the LOOP_SET_CAPACITY ioctl call, specifically handling EAGAIN errors with a short delay.
  • Comprehensive Testing: A new dedicated test case, LoopTestSetCapacity, has been added to verify the correct behavior of the new function. This test simulates resizing a backing file and confirms that bd_loop_set_capacity successfully updates the loop device's reported size.
  • API and Documentation Updates: The new function has been properly declared in the loop.api and loop.h files with Doxygen comments, and its entry has been added to the libblockdev-sections.txt documentation index.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a new function bd_loop_set_capacity to allow re-reading the size of a loop device, which is useful when the backing file has been resized. The implementation includes the core logic, API definitions, documentation updates, and a new test case.

My review has identified a bug in the progress message formatting that could lead to incorrect paths in log messages. I've also provided suggestions to improve code readability in the C implementation and reduce code duplication in the Python test suite. Overall, the changes are well-structured and the new functionality is a valuable addition.

Comment thread src/plugins/loop.c Outdated
Comment thread src/plugins/loop.c
Comment thread tests/loop_test.py Outdated
@vojtechtrefny vojtechtrefny force-pushed the master_loop-set-capacity branch from 9df1103 to f191bf8 Compare September 22, 2025 07:48
@vojtechtrefny
Copy link
Copy Markdown
Member Author

/gemini review

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds a new function, bd_loop_set_capacity, which allows forcing a loop device to re-read the size of its backing file. The implementation is sound, and it's accompanied by good documentation and a comprehensive test case. The changes are well-structured. I have one suggestion to improve code readability in the C implementation.

Comment thread src/plugins/loop.c
Comment on lines +560 to +566
for (n_try=10, status=-1; (status != 0) && (n_try > 0); n_try--) {
status = ioctl (fd, LOOP_SET_CAPACITY, 0);
if (status < 0 && errno == EAGAIN)
g_usleep (100 * 1000); /* microseconds */
else
break;
}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic in this for loop is a bit dense due to the combined initialization and condition, along with the if/else break structure. A more conventional loop structure would be more readable and easier for future maintenance.

    for (n_try = 10; n_try > 0; n_try--) {
        status = ioctl (fd, LOOP_SET_CAPACITY, 0);
        if (status == 0)
            break;
        if (errno != EAGAIN)
            break;
        g_usleep (100 * 1000); /* microseconds */
    }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this better!

Comment thread src/plugins/loop.c
Comment on lines +560 to +566
for (n_try=10, status=-1; (status != 0) && (n_try > 0); n_try--) {
status = ioctl (fd, LOOP_SET_CAPACITY, 0);
if (status < 0 && errno == EAGAIN)
g_usleep (100 * 1000); /* microseconds */
else
break;
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this better!

Comment thread src/plugins/loop.c
g_free (dev_loop);
if (fd < 0) {
g_set_error (&l_error, BD_LOOP_ERROR, BD_LOOP_ERROR_DEVICE,
"Failed to open device %s: %m", loop);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So previously you use a complex construct like dev_loop ? dev_loop : loop for messages but in the rest of the code you just use loop... I don't mind, I wonder why the AI didn't spot this.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I copied it from two different functions with two different messages :-)

@vojtechtrefny vojtechtrefny merged commit 24f99d3 into storaged-project:master Sep 23, 2025
48 of 50 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants