chiark / gitweb /
shared/util: respect buffer boundary on incomplete escape sequences
[elogind.git] / src / shared / machine-image.c
index 7c041fab7624315d3a7e31b39c73fab7fd5f67ea..25689ca93cba91042ab0f9eec37520d7b7b54912 100644 (file)
@@ -20,6 +20,7 @@
 ***/
 
 #include <sys/statfs.h>
+#include <linux/fs.h>
 #include <fcntl.h>
 
 #include "strv.h"
@@ -71,6 +72,8 @@ static int image_new(
         i->read_only = read_only;
         i->crtime = crtime;
         i->mtime = mtime;
+        i->size = i->size_exclusive = (uint64_t) -1;
+        i->limit = i->limit_exclusive = (uint64_t) -1;
 
         i->name = strdup(pretty);
         if (!i->name)
@@ -138,6 +141,7 @@ static int image_make(
 
                         if (F_TYPE_EQUAL(sfs.f_type, BTRFS_SUPER_MAGIC)) {
                                 BtrfsSubvolInfo info;
+                                BtrfsQuotaInfo quota;
 
                                 /* It's a btrfs subvolume */
 
@@ -156,6 +160,15 @@ static int image_make(
                                 if (r < 0)
                                         return r;
 
+                                r = btrfs_subvol_get_quota_fd(fd, &quota);
+                                if (r >= 0) {
+                                        (*ret)->size = quota.referred;
+                                        (*ret)->size_exclusive = quota.exclusive;
+
+                                        (*ret)->limit = quota.referred_max;
+                                        (*ret)->limit_exclusive = quota.exclusive_max;
+                                }
+
                                 return 1;
                         }
                 }
@@ -199,6 +212,9 @@ static int image_make(
                 if (r < 0)
                         return r;
 
+                (*ret)->size = (*ret)->size_exclusive = st.st_blocks * 512;
+                (*ret)->limit = (*ret)->limit_exclusive = st.st_size;
+
                 return 1;
         }
 
@@ -425,7 +441,7 @@ int image_clone(Image *i, const char *new_name, bool read_only) {
         case IMAGE_GPT:
                 new_path = strappenda("/var/lib/container/", new_name, ".gpt");
 
-                r = copy_file_atomic(i->path, new_path, read_only ? 0444 : 0644, false);
+                r = copy_file_atomic(i->path, new_path, read_only ? 0444 : 0644, false, FS_NOCOW_FL);
                 break;
 
         default:
@@ -462,6 +478,12 @@ int image_read_only(Image *i, bool b) {
 
                 if (chmod(i->path, (st.st_mode & 0444) | (b ? 0000 : 0200)) < 0)
                         return -errno;
+
+                /* If the images is now read-only, it's a good time to
+                 * defrag it, given that no write patterns will
+                 * fragment it again. */
+                if (b)
+                        (void) btrfs_defrag(i->path);
                 break;
         }