1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
5 #include "btrfs-util.h"
7 #include "homework-directory.h"
8 #include "homework-quota.h"
10 #include "mount-util.h"
11 #include "path-util.h"
13 #include "tmpfile-util.h"
14 #include "umask-util.h"
16 int home_setup_directory(UserRecord
*h
, HomeSetup
*setup
) {
20 setup
->root_fd
= open(user_record_image_path(h
), O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
);
21 if (setup
->root_fd
< 0)
22 return log_error_errno(errno
, "Failed to open home directory: %m");
27 int home_activate_directory(
31 UserRecord
**ret_home
) {
33 _cleanup_(user_record_unrefp
) UserRecord
*new_home
= NULL
, *header_home
= NULL
;
34 const char *hdo
, *hd
, *ipo
, *ip
;
38 assert(IN_SET(user_record_storage(h
), USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
));
42 assert_se(ipo
= user_record_image_path(h
));
43 ip
= strdupa_safe(ipo
); /* copy out, since reconciliation might cause changing of the field */
45 assert_se(hdo
= user_record_home_directory(h
));
46 hd
= strdupa_safe(hdo
);
48 r
= home_setup(h
, 0, cache
, setup
, &header_home
);
52 r
= home_refresh(h
, setup
, header_home
, cache
, NULL
, &new_home
);
56 setup
->root_fd
= safe_close(setup
->root_fd
);
58 /* Create mount point to mount over if necessary */
59 if (!path_equal(ip
, hd
))
60 (void) mkdir_p(hd
, 0700);
62 /* Create a mount point (even if the directory is already placed correctly), as a way to indicate
63 * this mount point is now "activated". Moreover, we want to set per-user
64 * MS_NOSUID/MS_NOEXEC/MS_NODEV. */
65 r
= mount_nofollow_verbose(LOG_ERR
, ip
, hd
, NULL
, MS_BIND
, NULL
);
69 r
= mount_nofollow_verbose(LOG_ERR
, NULL
, hd
, NULL
, MS_BIND
|MS_REMOUNT
|user_record_mount_flags(h
), NULL
);
71 (void) umount_verbose(LOG_ERR
, hd
, UMOUNT_NOFOLLOW
);
75 setup
->do_drop_caches
= false;
77 log_info("Everything completed.");
79 *ret_home
= TAKE_PTR(new_home
);
83 int home_create_directory_or_subvolume(UserRecord
*h
, HomeSetup
*setup
, UserRecord
**ret_home
) {
84 _cleanup_(rm_rf_subvolume_and_freep
) char *temporary
= NULL
;
85 _cleanup_(user_record_unrefp
) UserRecord
*new_home
= NULL
;
86 _cleanup_free_
char *d
= NULL
;
91 assert(IN_SET(user_record_storage(h
), USER_DIRECTORY
, USER_SUBVOLUME
));
95 assert_se(ip
= user_record_image_path(h
));
97 r
= tempfn_random(ip
, "homework", &d
);
99 return log_error_errno(r
, "Failed to allocate temporary directory: %m");
101 (void) mkdir_parents(d
, 0755);
103 switch (user_record_storage(h
)) {
107 r
= btrfs_subvol_make(d
);
110 log_info("Subvolume created.");
112 if (h
->disk_size
!= UINT64_MAX
) {
114 /* Enable quota for the subvolume we just created. Note we don't check for
115 * errors here and only log about debug level about this. */
116 r
= btrfs_quota_enable(d
, true);
118 log_debug_errno(r
, "Failed to enable quota on %s, ignoring: %m", d
);
120 r
= btrfs_subvol_auto_qgroup(d
, 0, false);
122 log_debug_errno(r
, "Failed to set up automatic quota group on %s, ignoring: %m", d
);
124 /* Actually configure the quota. We also ignore errors here, but we do log
125 * about them loudly, to keep things discoverable even though we don't
126 * consider lacking quota support in kernel fatal. */
127 (void) home_update_quota_btrfs(h
, d
);
133 return log_error_errno(r
, "Failed to create temporary home directory subvolume %s: %m", d
);
135 log_info("Creating subvolume %s is not supported, as file system does not support subvolumes. Falling back to regular directory.", d
);
140 if (mkdir(d
, 0700) < 0)
141 return log_error_errno(errno
, "Failed to create temporary home directory %s: %m", d
);
143 (void) home_update_quota_classic(h
, d
);
147 assert_not_reached();
150 temporary
= TAKE_PTR(d
); /* Needs to be destroyed now */
152 setup
->root_fd
= open(temporary
, O_RDONLY
|O_CLOEXEC
|O_DIRECTORY
|O_NOFOLLOW
);
153 if (setup
->root_fd
< 0)
154 return log_error_errno(errno
, "Failed to open temporary home directory: %m");
156 r
= home_populate(h
, setup
->root_fd
);
160 r
= home_sync_and_statfs(setup
->root_fd
, NULL
);
164 r
= user_record_clone(h
, USER_RECORD_LOAD_MASK_SECRET
|USER_RECORD_PERMISSIVE
, &new_home
);
166 return log_error_errno(r
, "Failed to clone record: %m");
168 r
= user_record_add_binding(
170 user_record_storage(h
),
183 return log_error_errno(r
, "Failed to add binding to record: %m");
185 if (rename(temporary
, ip
) < 0)
186 return log_error_errno(errno
, "Failed to rename %s to %s: %m", temporary
, ip
);
188 temporary
= mfree(temporary
);
190 log_info("Everything completed.");
192 *ret_home
= TAKE_PTR(new_home
);
196 int home_resize_directory(
198 HomeSetupFlags flags
,
199 PasswordCache
*cache
,
201 UserRecord
**ret_home
) {
203 _cleanup_(user_record_unrefp
) UserRecord
*embedded_home
= NULL
, *new_home
= NULL
;
209 assert(IN_SET(user_record_storage(h
), USER_DIRECTORY
, USER_SUBVOLUME
, USER_FSCRYPT
));
211 r
= home_setup(h
, flags
, cache
, setup
, NULL
);
215 r
= home_load_embedded_identity(h
, setup
->root_fd
, NULL
, USER_RECONCILE_REQUIRE_NEWER_OR_EQUAL
, cache
, &embedded_home
, &new_home
);
219 r
= home_update_quota_auto(h
, NULL
);
220 if (ERRNO_IS_NOT_SUPPORTED(r
))
221 return -ESOCKTNOSUPPORT
; /* make recognizable */
225 r
= home_store_embedded_identity(new_home
, setup
->root_fd
, h
->uid
, embedded_home
);
229 r
= home_extend_embedded_identity(new_home
, h
, setup
);
233 r
= home_sync_and_statfs(setup
->root_fd
, NULL
);
237 r
= home_setup_done(setup
);
241 log_info("Everything completed.");
243 *ret_home
= TAKE_PTR(new_home
);