| 1 |
/* |
| 2 |
* posixovl - POSIX overlay filesystem |
| 3 |
* Copyright Jan Engelhardt, 2007-2008,2013 |
| 4 |
* |
| 5 |
* Development of posixovl sponsored by Slax (http://www.slax.org/) |
| 6 |
* |
| 7 |
* This program is free software; you can redistribute it and/or |
| 8 |
* modify it under the terms of the GNU General Public License as |
| 9 |
* published by the Free Software Foundation; either version 2 of |
| 10 |
* the License, or (at your option) any later version. |
| 11 |
*/ |
| 12 |
#define _ATFILE_SOURCE 1 |
| 13 |
#define _GNU_SOURCE 1 |
| 14 |
#define FUSE_USE_VERSION 26 |
| 15 |
#include "config.h" |
| 16 |
#include <sys/fsuid.h> |
| 17 |
#include <sys/stat.h> |
| 18 |
#include <sys/statvfs.h> |
| 19 |
#include <sys/types.h> |
| 20 |
#include <sys/time.h> |
| 21 |
#include <dirent.h> |
| 22 |
#include <ctype.h> |
| 23 |
#include <errno.h> |
| 24 |
#include <fcntl.h> |
| 25 |
#include <fuse.h> |
| 26 |
#include <getopt.h> |
| 27 |
#include <limits.h> |
| 28 |
#include <pthread.h> |
| 29 |
#include <stdarg.h> |
| 30 |
#include <stdbool.h> |
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <string.h> |
| 34 |
#include <unistd.h> |
| 35 |
#ifdef HAVE_XATTR_XATTR_H |
| 36 |
# include <attr/xattr.h> |
| 37 |
#endif |
| 38 |
#ifdef HAVE_SYS_XATTR_H |
| 39 |
# include <sys/xattr.h> |
| 40 |
#endif |
| 41 |
#include "config.h" |
| 42 |
#ifndef S_IRUGO |
| 43 |
# define S_IRUGO (S_IRUSR | S_IRGRP | S_IROTH) |
| 44 |
# define S_IWUGO (S_IWUSR | S_IWGRP | S_IWOTH) |
| 45 |
# define S_IXUGO (S_IXUSR | S_IXGRP | S_IXOTH) |
| 46 |
#endif |
| 47 |
#ifndef S_IRWXUGO |
| 48 |
# define S_IRWXUGO (S_IRUGO | S_IWUGO | S_IXUGO) |
| 49 |
#endif |
| 50 |
#define S_IFHARDLNK (S_IFLNK | S_ISVTX) |
| 51 |
#define S_IFSOFTLNK (S_IFLNK | S_IRWXUGO) |
| 52 |
#define S_ISHARDLNK(mode) ((mode) == S_IFHARDLNK) |
| 53 |
#define S_ISSOFTLNK(mode) ((mode) == S_IFSOFTLNK) |
| 54 |
|
| 55 |
#define static_cast(type, x) ((type)(x)) |
| 56 |
|
| 57 |
/* GLIBC device number handling. From ttyrpld. */ |
| 58 |
#define COMPAT_MAJOR(dev) \ |
| 59 |
static_cast(unsigned long, ((dev) & 0xFFF00) >> 8) |
| 60 |
#define COMPAT_MINOR(dev) \ |
| 61 |
static_cast(unsigned long, ((dev) & 0xFF) | \ |
| 62 |
(((dev) & 0xFFF00000) >> 12)) |
| 63 |
#define COMPAT_MKDEV(major, minor) \ |
| 64 |
static_cast(unsigned long, ((minor) & 0xFF) | \ |
| 65 |
(((minor) & 0xFFF00) << 12) | (((major) & 0xFFF) << 8)) |
| 66 |
|
| 67 |
/* Buggers */ |
| 68 |
#ifndef NDEBUG |
| 69 |
# define should_never_happen() \ |
| 70 |
do { \ |
| 71 |
fprintf(stderr, "CRITICAL: Should NEVER happen! %s:%u\n", \ |
| 72 |
__FILE__, __LINE__); \ |
| 73 |
abort(); \ |
| 74 |
} while (false) |
| 75 |
# define should_not_happen() \ |
| 76 |
do { \ |
| 77 |
fprintf(stderr, "WARNING: Should not happen! %s:%u\n", \ |
| 78 |
__FILE__, __LINE__); \ |
| 79 |
} while (false) |
| 80 |
#else |
| 81 |
# define should_never_happen() do {} while (false) |
| 82 |
# define should_not_happen() do {} while (false) |
| 83 |
#endif |
| 84 |
#define hcb_got_busted(path) \ |
| 85 |
fprintf(stderr, "HCB %s is corrupt\n", (path)) |
| 86 |
|
| 87 |
/* Definitions */ |
| 88 |
|
| 89 |
#define HCB_PREFIX ".pxovl." |
| 90 |
#define HCB_PREFIX_LEN (sizeof(HCB_PREFIX) - 1) |
| 91 |
/* |
| 92 |
* Trailing dots may be eaten, so we need to check some filenames for |
| 93 |
* another variant. |
| 94 |
*/ |
| 95 |
#define HCB_PREFIX1 ".pxovl" |
| 96 |
|
| 97 |
#define HL_DNODE_PREFIX ".pxovd." |
| 98 |
#define HL_DNODE_PREFIX_LEN (sizeof(HL_DNODE_PREFIX) - 1) |
| 99 |
#define HL_INODE_PREFIX ".pxovn." |
| 100 |
#define HL_INODE_PREFIX_LEN (sizeof(HL_INODE_PREFIX) - 1) |
| 101 |
|
| 102 |
enum { |
| 103 |
/* extra errno codes */ |
| 104 |
ENOENT_HCB = 4096, |
| 105 |
}; |
| 106 |
|
| 107 |
/** |
| 108 |
* ll_hcb - lowlevel HCB |
| 109 |
* @buf: buffer for contents of HCB file |
| 110 |
* @new_target: buffer to hold the link target |
| 111 |
* @target: pointer to @new_target (always?) |
| 112 |
* rest: inode properties |
| 113 |
*/ |
| 114 |
struct ll_hcb { |
| 115 |
char buf[PATH_MAX], new_target[PATH_MAX]; |
| 116 |
const char *target; |
| 117 |
mode_t mode; |
| 118 |
nlink_t nlink; |
| 119 |
uid_t uid; |
| 120 |
gid_t gid; |
| 121 |
dev_t rdev; |
| 122 |
size_t size; |
| 123 |
}; |
| 124 |
|
| 125 |
/** |
| 126 |
* The "hidden control block". |
| 127 |
*/ |
| 128 |
struct hcb { |
| 129 |
char path[PATH_MAX]; |
| 130 |
struct ll_hcb ll; |
| 131 |
struct stat sb; |
| 132 |
int fd; |
| 133 |
}; |
| 134 |
|
| 135 |
/* Global */ |
| 136 |
static mode_t default_mode = S_IRUGO | S_IWUSR; |
| 137 |
static unsigned int assume_vfat, single_threaded, hardlink_with_copy; |
| 138 |
static const char *root_dir; |
| 139 |
static int root_fd; |
| 140 |
static pthread_mutex_t posixovl_protect = PTHREAD_MUTEX_INITIALIZER; |
| 141 |
|
| 142 |
static inline int retcode_int(int code) |
| 143 |
{ |
| 144 |
return (code >= 0) ? code : -errno; |
| 145 |
} |
| 146 |
|
| 147 |
static inline ssize_t retcode_ssize(ssize_t code) |
| 148 |
{ |
| 149 |
return (code >= 0) ? code : -errno; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* Set a read lock on the entire of a file. |
| 154 |
*/ |
| 155 |
static int lock_read(int fd) |
| 156 |
{ |
| 157 |
static const struct flock fl = { |
| 158 |
.l_type = F_RDLCK, |
| 159 |
.l_whence = SEEK_SET, |
| 160 |
.l_start = 0, |
| 161 |
.l_len = 0, |
| 162 |
}; |
| 163 |
if (single_threaded) |
| 164 |
return 0; |
| 165 |
return fcntl(fd, F_SETLKW, &fl); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Set a write lock on the entire of a file. |
| 170 |
*/ |
| 171 |
static int lock_write(int fd) |
| 172 |
{ |
| 173 |
static const struct flock fl = { |
| 174 |
.l_type = F_WRLCK, |
| 175 |
.l_whence = SEEK_SET, |
| 176 |
.l_start = 0, |
| 177 |
.l_len = 0, |
| 178 |
}; |
| 179 |
if (single_threaded) |
| 180 |
return 0; |
| 181 |
return fcntl(fd, F_SETLK, &fl); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* at - turn a virtual path into a relative (to root_fd) one |
| 186 |
*/ |
| 187 |
static __attribute__((pure)) const char *at(const char *in) |
| 188 |
{ |
| 189 |
if (*in != '/') |
| 190 |
should_never_happen(); |
| 191 |
if (in[1] == '\0') |
| 192 |
return "."; |
| 193 |
return in + 1; |
| 194 |
} |
| 195 |
|
| 196 |
static char *strlcpy(char *dest, const char *src, size_t n) |
| 197 |
{ |
| 198 |
strncpy(dest, src, n-1); |
| 199 |
dest[n-1] = '\0'; |
| 200 |
return dest; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* __hl_dtoi - build the HL.I-node path from the HL.D-node path |
| 205 |
*/ |
| 206 |
static void __hl_dtoi(char *dest, size_t destsize, const char *src) |
| 207 |
{ |
| 208 |
char *last, *p; |
| 209 |
|
| 210 |
strlcpy(dest, src, destsize); |
| 211 |
last = dest; |
| 212 |
while ((p = strstr(last, "/" HL_DNODE_PREFIX)) != NULL) |
| 213 |
last = p + 1; |
| 214 |
|
| 215 |
memcpy(last, HL_INODE_PREFIX, HL_INODE_PREFIX_LEN); |
| 216 |
} |
| 217 |
|
| 218 |
#define hl_dtoi(dest, src) __hl_dtoi((dest), sizeof(dest), (src)) |
| 219 |
|
| 220 |
/** |
| 221 |
* __real_to_hcb - build the hidden control block (HCB) path from a real path |
| 222 |
*/ |
| 223 |
static int __real_to_hcb(char *dest, size_t destsize, const char *src) |
| 224 |
{ |
| 225 |
const char *directory_part = src; |
| 226 |
const char *filename_part; |
| 227 |
struct stat sb; |
| 228 |
int ret; |
| 229 |
|
| 230 |
/* |
| 231 |
* For directories, the HCB must be stored within the directory, |
| 232 |
* not "alongside" it. This is because there is no possible parallel |
| 233 |
* entry (on the same filesystem) for the root directory. |
| 234 |
*/ |
| 235 |
ret = fstatat(root_fd, at(src), &sb, AT_SYMLINK_NOFOLLOW); |
| 236 |
if (ret == 0 && S_ISDIR(sb.st_mode)) { |
| 237 |
if (strcmp(src, "/") == 0) |
| 238 |
/* Make sure there is only one slash */ |
| 239 |
ret = snprintf(dest, destsize, "/" HCB_PREFIX); |
| 240 |
else |
| 241 |
ret = snprintf(dest, destsize, "%s/" HCB_PREFIX, src); |
| 242 |
|
| 243 |
if (ret > destsize) |
| 244 |
return -ENAMETOOLONG; |
| 245 |
return 0; |
| 246 |
} |
| 247 |
|
| 248 |
filename_part = strrchr(src, '/'); |
| 249 |
if (filename_part++ == NULL) |
| 250 |
should_never_happen(); |
| 251 |
|
| 252 |
if (strncmp(filename_part, HL_DNODE_PREFIX, HL_DNODE_PREFIX_LEN) == 0) |
| 253 |
ret = snprintf(dest, destsize, "%.*s" HL_INODE_PREFIX "%s", |
| 254 |
(int)(filename_part - directory_part), directory_part, |
| 255 |
filename_part + HL_DNODE_PREFIX_LEN); |
| 256 |
else |
| 257 |
ret = snprintf(dest, destsize, "%.*s" HCB_PREFIX "%s", |
| 258 |
(int)(filename_part - directory_part), directory_part, |
| 259 |
filename_part); |
| 260 |
if (ret > destsize) |
| 261 |
return -ENAMETOOLONG; |
| 262 |
return 0; |
| 263 |
} |
| 264 |
|
| 265 |
#define real_to_hcb(dest, src) __real_to_hcb((dest), sizeof(dest), (src)) |
| 266 |
|
| 267 |
/** |
| 268 |
* ll_hcb_read - lowlevel read of HCB |
| 269 |
* @path: path to HCB (used for debug and unlink) |
| 270 |
* @info: destination structure |
| 271 |
* @fd: fd to read from |
| 272 |
*/ |
| 273 |
static int ll_hcb_read(const char *path, struct ll_hcb *info, int fd) |
| 274 |
{ |
| 275 |
const char *s_mode, *s_nlink, *s_uid, *s_gid, *s_rdev; |
| 276 |
char *toul_ptr = NULL; |
| 277 |
ssize_t ret; |
| 278 |
|
| 279 |
if ((ret = read(fd, info->buf, sizeof(info->buf) - 1)) < 0) |
| 280 |
return -errno; |
| 281 |
info->buf[ret] = '\0'; |
| 282 |
if (ret == 0) |
| 283 |
return -ENOENT; |
| 284 |
info->size = ret; |
| 285 |
|
| 286 |
s_mode = info->buf; |
| 287 |
s_nlink = strchr(info->buf, ' '); |
| 288 |
if (s_nlink++ == NULL) |
| 289 |
goto busted; |
| 290 |
|
| 291 |
s_uid = strchr(s_nlink, ' '); |
| 292 |
if (s_uid++ == NULL) |
| 293 |
goto busted; |
| 294 |
|
| 295 |
s_gid = strchr(s_uid, ' '); |
| 296 |
if (s_gid++ == NULL) |
| 297 |
goto busted; |
| 298 |
|
| 299 |
s_rdev = strchr(s_gid, ' '); |
| 300 |
if (s_rdev++ == NULL) |
| 301 |
goto busted; |
| 302 |
|
| 303 |
info->target = strchr(s_rdev, ' '); |
| 304 |
if (info->target++ == NULL) |
| 305 |
goto busted; |
| 306 |
|
| 307 |
info->mode = strtoul(s_mode, NULL, 8); |
| 308 |
info->nlink = strtoul(s_nlink, NULL, 0); |
| 309 |
info->uid = strtoul(s_uid, NULL, 0); |
| 310 |
info->gid = strtoul(s_gid, NULL, 0); |
| 311 |
info->rdev = strtoul(s_rdev, &toul_ptr, 0); |
| 312 |
if (toul_ptr == NULL || *toul_ptr != ':') |
| 313 |
goto busted; |
| 314 |
++toul_ptr; |
| 315 |
info->rdev = COMPAT_MKDEV(info->rdev, strtoul(toul_ptr, NULL, 0)); |
| 316 |
strncpy(info->new_target, info->target, sizeof(info->new_target)); |
| 317 |
info->new_target[sizeof(info->new_target)-1] = '\0'; |
| 318 |
|
| 319 |
return 0; |
| 320 |
|
| 321 |
busted: |
| 322 |
hcb_got_busted(path); |
| 323 |
unlinkat(root_fd, at(path), 0); |
| 324 |
return -EINVAL; |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* ll_hcb_write - lowlevel write of HCB |
| 329 |
* @path: path to HCB (used for debug and unlink) |
| 330 |
* @info: source structure |
| 331 |
* @fd: fd to write to |
| 332 |
* |
| 333 |
* Recalculates @info->buf from the structure and writes it out. |
| 334 |
*/ |
| 335 |
static int ll_hcb_write(const char *path, struct ll_hcb *info, int fd) |
| 336 |
{ |
| 337 |
ssize_t ret; |
| 338 |
size_t z; |
| 339 |
|
| 340 |
if (lseek(fd, 0, SEEK_SET) < 0) |
| 341 |
return -errno; |
| 342 |
if (ftruncate(fd, 0) < 0) |
| 343 |
should_not_happen(); |
| 344 |
ret = snprintf(info->buf, sizeof(info->buf), "%o %u %lu %lu %lu:%lu %s", |
| 345 |
static_cast(unsigned int, info->mode), |
| 346 |
static_cast(unsigned int, info->nlink), |
| 347 |
static_cast(unsigned long, info->uid), |
| 348 |
static_cast(unsigned long, info->gid), |
| 349 |
COMPAT_MAJOR(info->rdev), COMPAT_MINOR(info->rdev), |
| 350 |
info->new_target); |
| 351 |
if (ret >= sizeof(info->buf)) |
| 352 |
return -EIO; |
| 353 |
|
| 354 |
z = strlen(info->buf); |
| 355 |
ret = write(fd, info->buf, z); |
| 356 |
if (ret < 0) |
| 357 |
return -errno; |
| 358 |
if (ret != z) { |
| 359 |
hcb_got_busted(path); |
| 360 |
unlinkat(root_fd, at(path), 0); |
| 361 |
return -EIO; |
| 362 |
} |
| 363 |
return 0; |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* hcb_new - create new HCB |
| 368 |
* @path: file path (not HCB path) |
| 369 |
* @cb: destination structure |
| 370 |
* @reuse: reuse HCB (may have been filled in previously, |
| 371 |
* by a failed hcb_get() for example) |
| 372 |
*/ |
| 373 |
static int hcb_new(const char *path, struct hcb *cb, unsigned int reuse) |
| 374 |
{ |
| 375 |
int ret; |
| 376 |
|
| 377 |
if (reuse) { |
| 378 |
if (cb->fd >= 0) |
| 379 |
should_not_happen(); |
| 380 |
} else { |
| 381 |
memset(cb, 0, sizeof(*cb)); |
| 382 |
cb->fd = -1; |
| 383 |
if ((ret = real_to_hcb(cb->path, path)) < 0) |
| 384 |
return ret; |
| 385 |
if (fstatat(root_fd, at(path), &cb->sb, |
| 386 |
AT_SYMLINK_NOFOLLOW) < 0 && errno != ENOENT) |
| 387 |
return -errno; |
| 388 |
if (!S_ISDIR(cb->sb.st_mode)) |
| 389 |
cb->sb.st_mode &= ~S_IXUGO; |
| 390 |
} |
| 391 |
|
| 392 |
cb->ll.mode = cb->sb.st_mode; |
| 393 |
cb->ll.nlink = cb->sb.st_nlink; |
| 394 |
cb->ll.uid = cb->sb.st_uid; |
| 395 |
cb->ll.gid = cb->sb.st_gid; |
| 396 |
cb->ll.rdev = cb->sb.st_rdev; |
| 397 |
return 0; |
| 398 |
} |
| 399 |
|
| 400 |
/** |
| 401 |
* hcb_get - read HCB |
| 402 |
* @path: fuse-namespace relative file path (L0) |
| 403 |
* @cb: destination struct |
| 404 |
* |
| 405 |
* Read @path's HCB into @cb. This function does not follow posixovl hardlinks. |
| 406 |
* When the file given by @path was not found, -ENOENT will be returned, but if |
| 407 |
* the HCB was not found, this funciton returns -ENOENT_HCB instead to indicate |
| 408 |
* so. |
| 409 |
*/ |
| 410 |
static int hcb_get(const char *path, struct hcb *cb) |
| 411 |
{ |
| 412 |
int ret; |
| 413 |
|
| 414 |
memset(cb, 0, sizeof(*cb)); |
| 415 |
cb->fd = -1; |
| 416 |
|
| 417 |
/* Get inode number, size and times from the L0 file */ |
| 418 |
if (fstatat(root_fd, at(path), &cb->sb, AT_SYMLINK_NOFOLLOW) < 0) |
| 419 |
return -errno; |
| 420 |
|
| 421 |
cb->sb.st_mode &= S_IFMT; |
| 422 |
cb->sb.st_mode |= default_mode; |
| 423 |
if (S_ISDIR(cb->sb.st_mode)) |
| 424 |
cb->sb.st_mode |= S_IXUGO; |
| 425 |
|
| 426 |
if ((ret = real_to_hcb(cb->path, path)) < 0) |
| 427 |
return ret; |
| 428 |
cb->fd = openat(root_fd, at(cb->path), O_RDWR); |
| 429 |
if (cb->fd < 0 && errno == EACCES) |
| 430 |
/* Retry read-only */ |
| 431 |
cb->fd = openat(root_fd, at(cb->path), O_RDONLY); |
| 432 |
if (cb->fd < 0) { |
| 433 |
if (errno == ENOENT) |
| 434 |
return -ENOENT_HCB; |
| 435 |
else |
| 436 |
return -errno; |
| 437 |
} |
| 438 |
if (lock_read(cb->fd) < 0) { |
| 439 |
ret = -errno; |
| 440 |
close(cb->fd); |
| 441 |
return ret; |
| 442 |
} |
| 443 |
ret = ll_hcb_read(cb->path, &cb->ll, cb->fd); |
| 444 |
if (ret < 0) { |
| 445 |
close(cb->fd); |
| 446 |
return ret; |
| 447 |
} |
| 448 |
|
| 449 |
/* and extra attributes from the L0 HCB */ |
| 450 |
cb->sb.st_mode = cb->ll.mode; |
| 451 |
cb->sb.st_nlink = cb->ll.nlink; |
| 452 |
cb->sb.st_uid = cb->ll.uid; |
| 453 |
cb->sb.st_gid = cb->ll.gid; |
| 454 |
cb->sb.st_rdev = cb->ll.rdev; |
| 455 |
if (!S_ISREG(cb->ll.mode) && !S_ISDIR(cb->ll.mode)) |
| 456 |
cb->sb.st_size = cb->ll.size; |
| 457 |
|
| 458 |
return 0; |
| 459 |
} |
| 460 |
|
| 461 |
/** |
| 462 |
* hcb_put - release HCB |
| 463 |
* @cb: data |
| 464 |
* |
| 465 |
* Close the file descriptor to the HCB. This does _not_ flush any changes. |
| 466 |
* (Also because whether a change was made is not recorded. Explicitly call |
| 467 |
* hcb_update().) |
| 468 |
*/ |
| 469 |
static void hcb_put(const struct hcb *cb) |
| 470 |
{ |
| 471 |
if (cb->fd < 0) |
| 472 |
should_not_happen(); |
| 473 |
close(cb->fd); |
| 474 |
} |
| 475 |
|
| 476 |
/** |
| 477 |
* hcb_deref - dereference an S_IFHARDLNK HCB |
| 478 |
* @cb: data |
| 479 |
* |
| 480 |
* If the HCB is an S_IFHARDLNK, follow it and update attributes. |
| 481 |
* Non-hardlinks are ignored and success is returned. |
| 482 |
*/ |
| 483 |
static int hcb_deref(struct hcb *cb) |
| 484 |
{ |
| 485 |
struct stat sb; |
| 486 |
int ret; |
| 487 |
|
| 488 |
if (cb->fd < 0) |
| 489 |
should_never_happen(); |
| 490 |
if (!S_ISHARDLNK(cb->ll.mode)) |
| 491 |
return 0; |
| 492 |
|
| 493 |
close(cb->fd); |
| 494 |
if (fstatat(root_fd, at(cb->ll.target), &sb, |
| 495 |
AT_SYMLINK_NOFOLLOW) < 0) |
| 496 |
return -errno; |
| 497 |
|
| 498 |
/* Some things are stored on the L1 file... */ |
| 499 |
cb->sb.st_ino = sb.st_ino; |
| 500 |
cb->sb.st_size = sb.st_size; |
| 501 |
cb->sb.st_atime = sb.st_atime; |
| 502 |
cb->sb.st_ctime = sb.st_ctime; |
| 503 |
cb->sb.st_mtime = sb.st_mtime; |
| 504 |
|
| 505 |
hl_dtoi(cb->path, cb->ll.target); |
| 506 |
cb->fd = openat(root_fd, at(cb->path), O_RDWR); |
| 507 |
if (cb->fd < 0) |
| 508 |
return -errno; |
| 509 |
if (lock_read(cb->fd) < 0) { |
| 510 |
ret = -errno; |
| 511 |
close(cb->fd); |
| 512 |
return ret; |
| 513 |
} |
| 514 |
ret = ll_hcb_read(cb->path, &cb->ll, cb->fd); |
| 515 |
if (ret < 0) { |
| 516 |
close(cb->fd); |
| 517 |
return ret; |
| 518 |
} |
| 519 |
|
| 520 |
/* ...and some on the L1 HCB */ |
| 521 |
cb->sb.st_mode = cb->ll.mode; |
| 522 |
cb->sb.st_nlink = cb->ll.nlink; |
| 523 |
cb->sb.st_uid = cb->ll.uid; |
| 524 |
cb->sb.st_gid = cb->ll.gid; |
| 525 |
cb->sb.st_rdev = cb->ll.rdev; |
| 526 |
if (!S_ISREG(cb->ll.mode) && !S_ISDIR(cb->ll.mode)) |
| 527 |
cb->sb.st_size = cb->ll.size; |
| 528 |
|
| 529 |
return 0; |
| 530 |
} |
| 531 |
|
| 532 |
/** |
| 533 |
* hcb_get_deref - shortcut for hcb_get()+hcb_deref() |
| 534 |
* @path: virtual path |
| 535 |
* @cb: destination structure |
| 536 |
* |
| 537 |
* Retrieve the lowest HCB. |
| 538 |
*/ |
| 539 |
static int hcb_get_deref(const char *path, struct hcb *cb) |
| 540 |
{ |
| 541 |
int ret; |
| 542 |
|
| 543 |
if ((ret = hcb_get(path, cb)) < 0) |
| 544 |
return ret; |
| 545 |
if ((ret = hcb_deref(cb)) < 0) |
| 546 |
return ret; |
| 547 |
|
| 548 |
return 0; |
| 549 |
} |
| 550 |
|
| 551 |
/** |
| 552 |
* hcb_update - write HCB |
| 553 |
* @cb: data |
| 554 |
* |
| 555 |
* Write back the HCB with possibly changed data. hcb_put() is called |
| 556 |
* afterwards because that's what is usually intended. |
| 557 |
*/ |
| 558 |
static int hcb_update(struct hcb *cb) |
| 559 |
{ |
| 560 |
int ret; |
| 561 |
|
| 562 |
if (cb->fd < 0) { |
| 563 |
/* When this HCB was created using hcb_new() */ |
| 564 |
cb->fd = openat(root_fd, at(cb->path), O_RDWR | O_CREAT | |
| 565 |
O_EXCL, S_IRUGO | S_IWUSR); |
| 566 |
if (cb->fd < 0) |
| 567 |
return -errno; |
| 568 |
} |
| 569 |
|
| 570 |
if (lock_write(cb->fd) < 0) { |
| 571 |
ret = -errno; |
| 572 |
close(cb->fd); |
| 573 |
return ret; |
| 574 |
} |
| 575 |
|
| 576 |
ret = ll_hcb_write(cb->path, &cb->ll, cb->fd); |
| 577 |
hcb_put(cb); |
| 578 |
return ret; |
| 579 |
} |
| 580 |
|
| 581 |
/** |
| 582 |
* hcb_lookup - shortcut for hcb_get()+hcb_put() |
| 583 |
* @path: virtual path |
| 584 |
* @cb: destination structure |
| 585 |
* |
| 586 |
* Do a standard HCB lookup with hardlink following. |
| 587 |
*/ |
| 588 |
static int hcb_lookup(const char *path, struct hcb *cb) |
| 589 |
{ |
| 590 |
int ret; |
| 591 |
|
| 592 |
if ((ret = hcb_get(path, cb)) < 0) |
| 593 |
return ret; |
| 594 |
|
| 595 |
hcb_put(cb); |
| 596 |
return 0; |
| 597 |
} |
| 598 |
|
| 599 |
/** |
| 600 |
* hcb_lookup_deref - shortcut for hcb_get_deref()+hcb_put() |
| 601 |
* @path: virtual path |
| 602 |
* @cb: destination structure |
| 603 |
* |
| 604 |
* Do a standard HCB lookup with hardlink following. |
| 605 |
*/ |
| 606 |
static int hcb_lookup_deref(const char *path, struct hcb *cb) |
| 607 |
{ |
| 608 |
int ret; |
| 609 |
|
| 610 |
if ((ret = hcb_get_deref(path, cb)) < 0) |
| 611 |
return ret; |
| 612 |
|
| 613 |
hcb_put(cb); |
| 614 |
return 0; |
| 615 |
} |
| 616 |
|
| 617 |
/** |
| 618 |
* hcb_lookup_readdir - |
| 619 |
* @dir: working directory |
| 620 |
* @name: file |
| 621 |
* @info: destination structure |
| 622 |
* |
| 623 |
* Combines the working directory @dir with @name (to form an absolute path), |
| 624 |
* then retrieves the HCB. |
| 625 |
*/ |
| 626 |
static int hcb_lookup_readdir(const char *dir, const char *name, |
| 627 |
struct hcb *info) |
| 628 |
{ |
| 629 |
char path[PATH_MAX]; |
| 630 |
int ret; |
| 631 |
|
| 632 |
/* |
| 633 |
* Ensure that @path does not have two leading slashes or |
| 634 |
* the *at() logic does not do the right thing. |
| 635 |
*/ |
| 636 |
if (dir[1] == '\0') { |
| 637 |
/* |
| 638 |
* First character is always a slash, so if the second one is |
| 639 |
* '\0', it must be "/". I am trying to optimize here. |
| 640 |
*/ |
| 641 |
path[0] = '/'; |
| 642 |
strlcpy(&path[1], name, sizeof(path) - 1); |
| 643 |
ret = strlen(name) + 1; |
| 644 |
} else { |
| 645 |
ret = snprintf(path, sizeof(path), "%s/%s", dir, name); |
| 646 |
} |
| 647 |
|
| 648 |
if (ret >= sizeof(path)) |
| 649 |
return -ENAMETOOLONG; |
| 650 |
if ((ret = hcb_get_deref(path, info)) < 0) |
| 651 |
return ret; |
| 652 |
hcb_put(info); |
| 653 |
return 0; |
| 654 |
} |
| 655 |
|
| 656 |
static __attribute__((pure)) bool is_resv_name(const char *name) |
| 657 |
{ |
| 658 |
return strncmp(name, HCB_PREFIX, HCB_PREFIX_LEN) == 0 || |
| 659 |
strncmp(name, HL_DNODE_PREFIX, HL_DNODE_PREFIX_LEN) == 0 || |
| 660 |
strncmp(name, HL_INODE_PREFIX, HL_INODE_PREFIX_LEN) == 0 || |
| 661 |
strcmp(name, HCB_PREFIX1) == 0; |
| 662 |
} |
| 663 |
|
| 664 |
static __attribute__((pure)) bool is_resv(const char *path) |
| 665 |
{ |
| 666 |
const char *file = strrchr(path, '/'); |
| 667 |
if (file++ == NULL) |
| 668 |
should_never_happen(); |
| 669 |
return is_resv_name(file); |
| 670 |
} |
| 671 |
|
| 672 |
/** |
| 673 |
* supports_owners - check whether @path can do that |
| 674 |
* @path: path to existing file |
| 675 |
* @uid: uid to change to (used for figuring out) |
| 676 |
* @gid: gid to change to |
| 677 |
* @restore: restore permissions after check |
| 678 |
* |
| 679 |
* This has to be looked up on a per-path basis, because it is possible to |
| 680 |
* mount a filesystem supporting permissions on a directory on a filesystem |
| 681 |
* that does not, as in, for example, the following case: |
| 682 |
* mount -t vfat /dev/foo /mnt |
| 683 |
* mount -t xfs /dev/bar /mnt/sub |
| 684 |
* mount.posixovl /mnt |
| 685 |
* |
| 686 |
* Note that on a filesystem which supports owners, our fchownat() will |
| 687 |
* always succeed (or always fail), because the kernel checks for |
| 688 |
* capability rather than FSUID. (Good thing.) |
| 689 |
*/ |
| 690 |
static bool __supports_owners(const char *path, uid_t uid, |
| 691 |
gid_t gid, bool restore) |
| 692 |
{ |
| 693 |
struct stat orig_sb, new_sb; |
| 694 |
uid_t work_uid = -1; |
| 695 |
gid_t work_gid = -1; |
| 696 |
|
| 697 |
if (fstatat(root_fd, at(path), &orig_sb, AT_SYMLINK_NOFOLLOW) < 0) { |
| 698 |
perror("fstatat"); |
| 699 |
return false; |
| 700 |
} |
| 701 |
|
| 702 |
if (uid == -1) |
| 703 |
work_uid = orig_sb.st_uid; |
| 704 |
else if (orig_sb.st_uid != uid) |
| 705 |
work_uid = uid; |
| 706 |
else if (uid == 0) |
| 707 |
work_uid = -2; |
| 708 |
else |
| 709 |
should_not_happen(); |
| 710 |
|
| 711 |
if (gid == -1) |
| 712 |
work_gid = orig_sb.st_gid; |
| 713 |
else if (orig_sb.st_gid != gid) |
| 714 |
work_gid = gid; |
| 715 |
else if (uid == 0) |
| 716 |
work_gid = -2; |
| 717 |
else |
| 718 |
should_not_happen(); |
| 719 |
|
| 720 |
if (fchownat(root_fd, at(path), work_uid, work_gid, |
| 721 |
AT_SYMLINK_NOFOLLOW) < 0) |
| 722 |
return false; |
| 723 |
if (fstatat(root_fd, at(path), &new_sb, AT_SYMLINK_NOFOLLOW) < 0) { |
| 724 |
perror("fstatat"); |
| 725 |
return false; |
| 726 |
} |
| 727 |
|
| 728 |
if (restore) |
| 729 |
if (fchownat(root_fd, at(path), orig_sb.st_uid, |
| 730 |
orig_sb.st_gid, AT_SYMLINK_NOFOLLOW) < 0) |
| 731 |
should_not_happen(); |
| 732 |
|
| 733 |
return new_sb.st_uid == work_uid && new_sb.st_gid == work_gid; |
| 734 |
} |
| 735 |
|
| 736 |
static bool supports_owners(const char *path, uid_t uid, |
| 737 |
gid_t gid, bool restore) |
| 738 |
{ |
| 739 |
if (assume_vfat) |
| 740 |
return false; |
| 741 |
return __supports_owners(path, uid, gid, restore); |
| 742 |
} |
| 743 |
|
| 744 |
/** |
| 745 |
* supports_permissions - check whether @path can do that |
| 746 |
* @path: existing path to file |
| 747 |
* |
| 748 |
* Does not restore the original mode. |
| 749 |
*/ |
| 750 |
static bool __supports_permissions(const char *path) |
| 751 |
{ |
| 752 |
struct stat orig_sb, new_sb; |
| 753 |
mode_t work_mode; |
| 754 |
|
| 755 |
if (fstatat(root_fd, at(path), &orig_sb, AT_SYMLINK_NOFOLLOW) < 0) { |
| 756 |
/* literally BUG() */ |
| 757 |
perror("fstatat"); |
| 758 |
return false; |
| 759 |
} |
| 760 |
|
| 761 |
/* Pick some magic */ |
| 762 |
work_mode = (orig_sb.st_mode ^ S_IRUSR ^ S_IXGRP) & ~S_IROTH; |
| 763 |
|
| 764 |
if (fchmodat(root_fd, at(path), work_mode, AT_SYMLINK_NOFOLLOW) < 0) |
| 765 |
return false; |
| 766 |
if (fstatat(root_fd, at(path), &new_sb, AT_SYMLINK_NOFOLLOW) < 0) { |
| 767 |
perror("fstatat"); |
| 768 |
return false; |
| 769 |
} |
| 770 |
return new_sb.st_mode == work_mode; |
| 771 |
} |
| 772 |
|
| 773 |
static bool supports_permissions(const char *path) |
| 774 |
{ |
| 775 |
if (assume_vfat) |
| 776 |
return false; |
| 777 |
return __supports_permissions(path); |
| 778 |
} |
| 779 |
|
| 780 |
/** |
| 781 |
* @read_ptr: original to-disk filename |
| 782 |
* |
| 783 |
* Encode all the special characters that are invalid to use on VFAT by |
| 784 |
* substituting each by "%(xx)", where xx is the textual hexadecimal |
| 785 |
* representation. Returns a pointer to an allocated memory block containing |
| 786 |
* the encoded result. |
| 787 |
*/ |
| 788 |
static char *xfrm_to_disk(const char *read_ptr) |
| 789 |
{ |
| 790 |
unsigned int needed = 0; |
| 791 |
const char *next; |
| 792 |
char *out, *out_ptr; |
| 793 |
|
| 794 |
for (next = read_ptr; *next != '\0'; ++next) |
| 795 |
switch (*next) { |
| 796 |
case '\\': |
| 797 |
case ':': |
| 798 |
case '*': |
| 799 |
case '?': |
| 800 |
case '\"': |
| 801 |
case '<': |
| 802 |
case '>': |
| 803 |
case '|': |
| 804 |
needed += strlen("%(XX)"); |
| 805 |
break; |
| 806 |
/* Path separator ('/') is not to be encoded! */ |
| 807 |
default: |
| 808 |
++needed; |
| 809 |
break; |
| 810 |
} |
| 811 |
|
| 812 |
out = out_ptr = malloc(needed + 1); |
| 813 |
if (out == NULL) |
| 814 |
return NULL; |
| 815 |
|
| 816 |
do { |
| 817 |
next = strpbrk(read_ptr, "\\:*?\"<>|"); |
| 818 |
if (next == NULL) { |
| 819 |
strcpy(out_ptr, read_ptr); |
| 820 |
out_ptr += strlen(read_ptr); |
| 821 |
break; |
| 822 |
} else if (read_ptr != next) { |
| 823 |
strncpy(out_ptr, read_ptr, next - read_ptr); |
| 824 |
out_ptr += next - read_ptr; |
| 825 |
} |
| 826 |
sprintf(out_ptr, "%%(%02X)", *next); |
| 827 |
out_ptr += strlen("%(XX)"); |
| 828 |
read_ptr = next + 1; |
| 829 |
} while (*read_ptr != '\0'); |
| 830 |
|
| 831 |
*out_ptr = '\0'; |
| 832 |
return out; |
| 833 |
} |
| 834 |
|
| 835 |
static const unsigned char xfrm_table[] = { |
| 836 |
['0'] = 0, ['1'] = 1, ['2'] = 2, ['3'] = 3, |
| 837 |
['4'] = 4, ['5'] = 5, ['6'] = 6, ['7'] = 7, |
| 838 |
['8'] = 8, ['9'] = 9, ['A'] = 10, ['B'] = 11, |
| 839 |
['C'] = 12, ['D'] = 13, ['E'] = 14, ['F'] = 15, |
| 840 |
}; |
| 841 |
|
| 842 |
/** |
| 843 |
* @read_ptr: original on-disk filename |
| 844 |
* |
| 845 |
* Decode a name that has the %(xx) special sequences in it and return a |
| 846 |
* pointer to an allocated memory block with the result. |
| 847 |
*/ |
| 848 |
static char *xfrm_from_disk(const char *read_ptr) |
| 849 |
{ |
| 850 |
const char *seek_ptr = read_ptr, *next; |
| 851 |
char *out = malloc(strlen(read_ptr) + 1), *out_ptr = out; |
| 852 |
|
| 853 |
if (out == NULL) |
| 854 |
return NULL; |
| 855 |
|
| 856 |
do { |
| 857 |
next = strstr(seek_ptr, "%("); |
| 858 |
if (next == NULL) { |
| 859 |
strcpy(out_ptr, read_ptr); |
| 860 |
out_ptr += strlen(read_ptr); |
| 861 |
break; |
| 862 |
} |
| 863 |
if (!isxdigit(next[2]) || !isxdigit(next[3]) || |
| 864 |
next[4] != ')') { |
| 865 |
seek_ptr = next + 1; |
| 866 |
continue; |
| 867 |
} |
| 868 |
strncpy(out_ptr, read_ptr, next - read_ptr); |
| 869 |
out_ptr += next - read_ptr; |
| 870 |
read_ptr = seek_ptr = next + strlen("%(XX)"); |
| 871 |
*out_ptr++ = (xfrm_table[toupper(next[2])] << 4) | |
| 872 |
xfrm_table[toupper(next[3])]; |
| 873 |
} while (*read_ptr != '\0'); |
| 874 |
|
| 875 |
*out_ptr = '\0'; |
| 876 |
return out; |
| 877 |
} |
| 878 |
|
| 879 |
static int posixovl_chmod(const char *path, mode_t mode) |
| 880 |
{ |
| 881 |
struct hcb info; |
| 882 |
int ret; |
| 883 |
|
| 884 |
if (is_resv(path)) |
| 885 |
return -ENOENT; |
| 886 |
ret = hcb_get_deref(path, &info); |
| 887 |
if (ret == -ENOENT_HCB) { |
| 888 |
if (supports_permissions(path)) |
| 889 |
return retcode_int(fchmodat(root_fd, at(path), mode, |
| 890 |
AT_SYMLINK_NOFOLLOW)); |
| 891 |
if ((ret = hcb_new(path, &info, 1)) < 0) |
| 892 |
return ret; |
| 893 |
/* nlink already set (hcb_new() stat'ed @path) */ |
| 894 |
} else if (ret < 0) { |
| 895 |
return ret; |
| 896 |
} |
| 897 |
|
| 898 |
info.ll.mode = mode; |
| 899 |
return hcb_update(&info); |
| 900 |
} |
| 901 |
|
| 902 |
static int posixovl1_chmod(const char *path, mode_t mode) |
| 903 |
{ |
| 904 |
char *xpath = xfrm_to_disk(path); |
| 905 |
int ret = posixovl_chmod(xpath, mode); |
| 906 |
free(xpath); |
| 907 |
return ret; |
| 908 |
} |
| 909 |
|
| 910 |
static int posixovl_chown(const char *path, uid_t uid, gid_t gid) |
| 911 |
{ |
| 912 |
struct hcb info; |
| 913 |
int ret; |
| 914 |
|
| 915 |
if (is_resv(path)) |
| 916 |
return -ENOENT; |
| 917 |
ret = hcb_get_deref(path, &info); |
| 918 |
if (ret == -ENOENT_HCB) { |
| 919 |
if (supports_owners(path, uid, gid, false)) |
| 920 |
return retcode_int(fchownat(root_fd, at(path), uid, |
| 921 |
gid, AT_SYMLINK_NOFOLLOW)); |
| 922 |
if ((ret = hcb_new(path, &info, 1)) < 0) |
| 923 |
return ret; |
| 924 |
/* nlink already set (hcb_new() stat'ed @path) */ |
| 925 |
} else if (ret < 0) { |
| 926 |
return ret; |
| 927 |
} |
| 928 |
|
| 929 |
if (uid != (uid_t)-1) |
| 930 |
info.ll.uid = uid; |
| 931 |
if (gid != (gid_t)-1) |
| 932 |
info.ll.gid = gid; |
| 933 |
return hcb_update(&info); |
| 934 |
} |
| 935 |
|
| 936 |
static int posixovl1_chown(const char *path, uid_t uid, gid_t gid) |
| 937 |
{ |
| 938 |
char *xpath = xfrm_to_disk(path); |
| 939 |
int ret = posixovl_chown(xpath, uid, gid); |
| 940 |
free(xpath); |
| 941 |
return ret; |
| 942 |
} |
| 943 |
|
| 944 |
static int posixovl_close(const char *path, struct fuse_file_info *filp) |
| 945 |
{ |
| 946 |
return retcode_int(close(filp->fh)); |
| 947 |
} |
| 948 |
|
| 949 |
static __attribute__((pure)) bool could_be_too_long(const char *path) |
| 950 |
{ |
| 951 |
/* Longest possible case is S_ISDIR: /path/.pxovl. */ |
| 952 |
return strlen(path) + 1 + HCB_PREFIX_LEN >= PATH_MAX; |
| 953 |
} |
| 954 |
|
| 955 |
/** |
| 956 |
* parent_owner_match - |
| 957 |
* @path: path, of which the parent is to be checked |
| 958 |
* @uid: uid to test |
| 959 |
* |
| 960 |
* Checks whether @path's parent is owned by @uid. |
| 961 |
* @path denotes a path on the real volume, hence no HCB lookup here. |
| 962 |
*/ |
| 963 |
static bool parent_owner_match(const char *path, uid_t uid) |
| 964 |
{ |
| 965 |
struct stat sb; |
| 966 |
int ret; |
| 967 |
|
| 968 |
ret = fstatat(root_fd, at(path), &sb, AT_SYMLINK_NOFOLLOW); |
| 969 |
if (ret < 0) { |
| 970 |
should_not_happen(); |
| 971 |
return false; |
| 972 |
} |
| 973 |
|
| 974 |
return sb.st_uid == uid; |
| 975 |
} |
| 976 |
|
| 977 |
static int posixovl_create(const char *path, mode_t mode, |
| 978 |
struct fuse_file_info *filp) |
| 979 |
{ |
| 980 |
const struct fuse_context *ctx; |
| 981 |
struct hcb cb; |
| 982 |
int fd, ret; |
| 983 |
|
| 984 |
if (is_resv(path)) |
| 985 |
return -EPERM; |
| 986 |
if (could_be_too_long(path)) |
| 987 |
return -ENAMETOOLONG; |
| 988 |
|
| 989 |
fd = openat(root_fd, at(path), filp->flags, mode); |
| 990 |
if (fd < 0) |
| 991 |
return -errno; |
| 992 |
|
| 993 |
filp->fh = fd; |
| 994 |
|
| 995 |
/* |
| 996 |
* Default file permissions do not trigger creation of a HCB. |
| 997 |
* We need (rather: want) a HCB if the fsuid is different from |
| 998 |
* the owner of the underlying mount, if owners are not |
| 999 |
* supported. |
| 1000 |
* Fuse oddity: @mode includes S_IFREG (contraty to mkdir()) |
| 1001 |
*/ |
| 1002 |
ctx = fuse_get_context(); |
| 1003 |
|
| 1004 |
if (((mode & ~S_IWUSR) != (S_IFREG | (default_mode & ~S_IWUSR)) && |
| 1005 |
!supports_permissions(path)) || |
| 1006 |
(!parent_owner_match(path, ctx->uid) && |
| 1007 |
!supports_owners(path, ctx->uid, ctx->gid, true))) { |
| 1008 |
if ((ret = hcb_new(path, &cb, 0)) < 0) |
| 1009 |
return 0; |
| 1010 |
/* nlink already set (hcb_new() stat'ed @path) */ |
| 1011 |
cb.ll.mode = mode; |
| 1012 |
cb.ll.uid = ctx->uid; |
| 1013 |
cb.ll.gid = ctx->gid; |
| 1014 |
hcb_update(&cb); |
| 1015 |
} |
| 1016 |
|
| 1017 |
return 0; |
| 1018 |
} |
| 1019 |
|
| 1020 |
static int posixovl1_create(const char *path, mode_t mode, |
| 1021 |
struct fuse_file_info *filp) |
| 1022 |
{ |
| 1023 |
char *xpath = xfrm_to_disk(path); |
| 1024 |
int ret = posixovl_create(xpath, mode, filp); |
| 1025 |
free(xpath); |
| 1026 |
return ret; |
| 1027 |
} |
| 1028 |
|
| 1029 |
static int posixovl_ftruncate(const char *path, off_t length, |
| 1030 |
struct fuse_file_info *filp) |
| 1031 |
{ |
| 1032 |
return retcode_int(ftruncate(filp->fh, length)); |
| 1033 |
} |
| 1034 |
|
| 1035 |
/** |
| 1036 |
* hl_demote - collapse S_IFHARDLNK into normal file |
| 1037 |
* @l0_file: |
| 1038 |
* @l0_hcb: |
| 1039 |
* @l1_file: |
| 1040 |
* @l1_hcb: |
| 1041 |
* |
| 1042 |
* Unlink the L0 files and move the L1 ones into L0's place. |
| 1043 |
* Caller *must* ensure that the L1 HCB has nlink=1, or all other |
| 1044 |
* hardlink slaves will break. |
| 1045 |
*/ |
| 1046 |
static int hl_demote(const char *l0_file, const char *l0_hcb, |
| 1047 |
const char *l1_file, const char *l1_hcb) |
| 1048 |
{ |
| 1049 |
int ret = 0; |
| 1050 |
|
| 1051 |
pthread_mutex_lock(&posixovl_protect); |
| 1052 |
if (unlinkat(root_fd, at(l0_file), 0) < 0) { |
| 1053 |
pthread_mutex_unlock(&posixovl_protect); |
| 1054 |
return -errno; |
| 1055 |
} |
| 1056 |
unlinkat(root_fd, at(l0_hcb), 0); |
| 1057 |
if (renameat(root_fd, at(l1_hcb), root_fd, at(l0_hcb)) < 0) { |
| 1058 |
pthread_mutex_unlock(&posixovl_protect); |
| 1059 |
ret = -errno; |
| 1060 |
fprintf(stderr, "%s: rename %s -> %s failed: %s\n", |
| 1061 |
__func__, l1_hcb, l0_hcb, strerror(errno)); |
| 1062 |
return ret; |
| 1063 |
} |
| 1064 |
if (renameat(root_fd, at(l1_file), root_fd, at(l0_file)) < 0) { |
| 1065 |
ret = -errno; |
| 1066 |
fprintf(stderr, "%s: rename %s -> %s failed: %s\n", |
| 1067 |
__func__, l1_file, l0_file, strerror(errno)); |
| 1068 |
} |
| 1069 |
pthread_mutex_unlock(&posixovl_protect); |
| 1070 |
return ret; |
| 1071 |
} |
| 1072 |
|
| 1073 |
/** |
| 1074 |
* hl_try_demote - try to collapse a one-link hardlink net into one file |
| 1075 |
* @path: real path |
| 1076 |
* |
| 1077 |
* Check if @path is a S_IFHARDLNK and if its link count is 1, demote the |
| 1078 |
* four-file set (virtual file, L0 HCB, data file, L1 HCB) into a two-file |
| 1079 |
* set (virtual + HCB). |
| 1080 |
*/ |
| 1081 |
static int hl_try_demote(const char *path) |
| 1082 |
{ |
| 1083 |
struct hcb info_l0, info_l1; |
| 1084 |
int ret; |
| 1085 |
|
| 1086 |
ret = hcb_lookup(path, &info_l0); |
| 1087 |
if (ret == -ENOENT_HCB) |
| 1088 |
return 0; |
| 1089 |
if (ret < 0) |
| 1090 |
return ret; |
| 1091 |
if (!S_ISHARDLNK(info_l0.ll.mode)) |
| 1092 |
return 0; |
| 1093 |
|
| 1094 |
memcpy(&info_l1, &info_l0, sizeof(info_l0)); |
| 1095 |
if ((ret = hcb_deref(&info_l1)) < 0) |
| 1096 |
return ret; |
| 1097 |
hcb_put(&info_l1); |
| 1098 |
if (info_l1.ll.nlink != 1) |
| 1099 |
return 0; |
| 1100 |
|
| 1101 |
return hl_demote(path, info_l0.path, info_l0.ll.target, info_l1.path); |
| 1102 |
} |
| 1103 |
|
| 1104 |
static int posixovl_getattr(const char *path, struct stat *sb) |
| 1105 |
{ |
| 1106 |
struct hcb info; |
| 1107 |
int ret; |
| 1108 |
|
| 1109 |
if (is_resv(path)) |
| 1110 |
return -ENOENT; |
| 1111 |
ret = hcb_lookup_deref(path, &info); |
| 1112 |
if (ret < 0 && ret != -ENOENT_HCB && ret != -EACCES) |
| 1113 |
return ret; |
| 1114 |
memcpy(sb, &info.sb, sizeof(*sb)); |
| 1115 |
return 0; |
| 1116 |
} |
| 1117 |
|
| 1118 |
static int posixovl1_getattr(const char *path, struct stat *sb) |
| 1119 |
{ |
| 1120 |
char *xpath = xfrm_to_disk(path); |
| 1121 |
int ret = posixovl_getattr(xpath, sb); |
| 1122 |
free(xpath); |
| 1123 |
return ret; |
| 1124 |
} |
| 1125 |
|
| 1126 |
static int posixovl_getxattr(const char *path, const char *name, |
| 1127 |
char *value, size_t size) |
| 1128 |
{ |
| 1129 |
return retcode_int(lgetxattr(at(path), name, value, size)); |
| 1130 |
} |
| 1131 |
|
| 1132 |
static int posixovl1_fgetattr(const char *path, struct stat *sb, |
| 1133 |
struct fuse_file_info *filp) |
| 1134 |
{ |
| 1135 |
/* |
| 1136 |
* Need to use the normal getattr because we need to check for the |
| 1137 |
* HCB too, not just @filp->fh. |
| 1138 |
*/ |
| 1139 |
return posixovl1_getattr(path, sb); |
| 1140 |
} |
| 1141 |
|
| 1142 |
static void *posixovl_init(struct fuse_conn_info *conn) |
| 1143 |
{ |
| 1144 |
/* |
| 1145 |
* There is no fopendirat(), we need to use fchdir() and |
| 1146 |
* opendir(relative_path) instead. |
| 1147 |
*/ |
| 1148 |
if (fchdir(root_fd) < 0) |
| 1149 |
abort(); |
| 1150 |
return NULL; |
| 1151 |
} |
| 1152 |
|
| 1153 |
static int posixovl_fcopy(int dfd, const char *source) |
| 1154 |
{ |
| 1155 |
int sfd = open(source, O_RDONLY); |
| 1156 |
ssize_t ret; |
| 1157 |
char buffer[65536]; |
| 1158 |
|
| 1159 |
if (sfd < 0) |
| 1160 |
return -errno; |
| 1161 |
while ((ret = read(sfd, buffer, sizeof(buffer))) > 0) |
| 1162 |
if (write(dfd, buffer, ret) < 0) { |
| 1163 |
ret = -errno; |
| 1164 |
break; |
| 1165 |
} |
| 1166 |
return ret; |
| 1167 |
} |
| 1168 |
|
| 1169 |
/** |
| 1170 |
* The size of the inode type may be larger than that of the type that rand() |
| 1171 |
* returns, so call rand() as often as needed to fill all bits. |
| 1172 |
* Minus 1 is used because it may be a signed integer whose sign is never set. |
| 1173 |
* So in the usual case of ino_t being 64 bit and int being 32 bit, we will |
| 1174 |
* call rand() thrice. |
| 1175 |
*/ |
| 1176 |
static ino_t make_inum(void) |
| 1177 |
{ |
| 1178 |
static const unsigned int advance = |
| 1179 |
sizeof(__typeof__(rand())) * CHAR_BIT - 1; |
| 1180 |
ino_t ino = 0; |
| 1181 |
|
| 1182 |
for (unsigned int b = 0; b < sizeof(ino) * CHAR_BIT; b += advance) { |
| 1183 |
ino <<= advance; |
| 1184 |
ino ^= rand(); |
| 1185 |
} |
| 1186 |
return ino; |
| 1187 |
} |
| 1188 |
|
| 1189 |
/** |
| 1190 |
* hl_promote - transform state 0/1 file into hardlink master (state 2) |
| 1191 |
* @l0_path: path to real file |
| 1192 |
* @orig_info: L0 HCB |
| 1193 |
* @l0_hcb_exists: what it says |
| 1194 |
*/ |
| 1195 |
static int hl_promote(const char *l0_path, const struct hcb *orig_info, |
| 1196 |
bool l0_hcb_exists) |
| 1197 |
{ |
| 1198 |
char l1_path[PATH_MAX], l1_hcb[PATH_MAX]; |
| 1199 |
struct stat work_sb; |
| 1200 |
struct hcb new_info; |
| 1201 |
int fd, ret; |
| 1202 |
|
| 1203 |
/* |
| 1204 |
* Create a unique ID. Note that there may be underlying filesystems |
| 1205 |
* where inode numbers are dynamically generated. Hence they may |
| 1206 |
* overlap with posixovl IDs (encoded into the filename) from a |
| 1207 |
* previous mount, hence we rand() should the ID already exist. |
| 1208 |
*/ |
| 1209 |
work_sb.st_ino = orig_info->sb.st_ino; |
| 1210 |
do { |
| 1211 |
snprintf(l1_path, sizeof(l1_path), "/" HL_DNODE_PREFIX "%lx", |
| 1212 |
static_cast(unsigned long, work_sb.st_ino)); |
| 1213 |
snprintf(l1_hcb, sizeof(l1_hcb), "/" HL_INODE_PREFIX "%lx", |
| 1214 |
static_cast(unsigned long, work_sb.st_ino)); |
| 1215 |
if (fstatat(root_fd, at(l1_path), &work_sb, |
| 1216 |
AT_SYMLINK_NOFOLLOW) == 0) { |
| 1217 |
work_sb.st_ino = make_inum(); |
| 1218 |
continue; |
| 1219 |
} |
| 1220 |
if (errno == ENOENT) |
| 1221 |
/* ok, can use this ID */ |
| 1222 |
break; |
| 1223 |
return -errno; |
| 1224 |
} while (true); |
| 1225 |
|
| 1226 |
/* Move L0 to L1 */ |
| 1227 |
ret = renameat(root_fd, at(l0_path), root_fd, at(l1_path)); |
| 1228 |
if (ret < 0) |
| 1229 |
return -errno; |
| 1230 |
|
| 1231 |
/* move L0 HCB to L1 HCB */ |
| 1232 |
if (l0_hcb_exists) { |
| 1233 |
ret = renameat(root_fd, at(orig_info->path), |
| 1234 |
root_fd, at(l1_hcb)); |
| 1235 |
if (ret < 0) { |
| 1236 |
ret = -errno; |
| 1237 |
goto out; |
| 1238 |
} |
| 1239 |
/* nlink already ok */ |
| 1240 |
} else { |
| 1241 |
if ((ret = hcb_new(l1_path, &new_info, 0)) < 0) |
| 1242 |
goto out; |
| 1243 |
if ((ret = hcb_update(&new_info)) < 0) |
| 1244 |
goto out; |
| 1245 |
} |
| 1246 |
|
| 1247 |
/* initialize first link */ |
| 1248 |
if ((ret = hcb_new(l0_path, &new_info, 0)) < 0) |
| 1249 |
goto out2; |
| 1250 |
new_info.ll.mode = S_IFHARDLNK; |
| 1251 |
new_info.ll.nlink = 1; |
| 1252 |
strlcpy(new_info.ll.new_target, l1_path, sizeof(new_info.ll.new_target)); |
| 1253 |
hcb_update(&new_info); |
| 1254 |
|
| 1255 |
/* instantiate first link into readdir visibility */ |
| 1256 |
fd = openat(root_fd, at(l0_path), O_WRONLY | O_CREAT | O_EXCL, 0); |
| 1257 |
if (fd < 0) { |
| 1258 |
ret = -errno; |
| 1259 |
goto out3; |
| 1260 |
} |
| 1261 |
if (hardlink_with_copy) |
| 1262 |
posixovl_fcopy(fd, new_info.ll.new_target); |
| 1263 |
close(fd); |
| 1264 |
return 0; |
| 1265 |
|
| 1266 |
out3: |
| 1267 |
unlinkat(root_fd, at(orig_info->path), 0); |
| 1268 |
out2: |
| 1269 |
if (l0_hcb_exists) |
| 1270 |
renameat(root_fd, at(l1_hcb), root_fd, at(orig_info->path)); |
| 1271 |
out: |
| 1272 |
renameat(root_fd, at(l1_path), root_fd, at(l0_path)); |
| 1273 |
return ret; |
| 1274 |
} |
| 1275 |
|
| 1276 |
/** |
| 1277 |
* hl_up_nlink - increase nlink count of hardlink master |
| 1278 |
* @l1_path: name of the L1 file |
| 1279 |
* (Could have used the L0 file, but using L1 saves a derefernce, |
| 1280 |
* which has already been performed in the caller anyway) |
| 1281 |
*/ |
| 1282 |
static int hl_up_nlink(const char *l1_path) |
| 1283 |
{ |
| 1284 |
struct hcb cb; |
| 1285 |
int ret; |
| 1286 |
|
| 1287 |
if ((ret = hcb_get(l1_path, &cb)) < 0) |
| 1288 |
return ret; |
| 1289 |
if (S_ISHARDLNK(cb.ll.mode)) |
| 1290 |
should_not_happen(); |
| 1291 |
else |
| 1292 |
++cb.ll.nlink; |
| 1293 |
return hcb_update(&cb); |
| 1294 |
} |
| 1295 |
|
| 1296 |
/** |
| 1297 |
* hl_drop - drop nlink count of hardlink master |
| 1298 |
* @l1_path: name of the L1 file |
| 1299 |
* |
| 1300 |
* Drop the nlink of the hardlink master by one, and if it reaches zero, |
| 1301 |
* unlink the D-node. |
| 1302 |
*/ |
| 1303 |
static int hl_drop(const char *l1_path) |
| 1304 |
{ |
| 1305 |
struct hcb cb; |
| 1306 |
int ret; |
| 1307 |
|
| 1308 |
if ((ret = hcb_get(l1_path, &cb)) < 0) |
| 1309 |
return ret; |
| 1310 |
if (S_ISHARDLNK(cb.ll.mode)) { |
| 1311 |
should_not_happen(); |
| 1312 |
hcb_put(&cb); |
| 1313 |
return 0; |
| 1314 |
} |
| 1315 |
if (cb.ll.nlink == 0) |
| 1316 |
should_not_happen(); |
| 1317 |
if (cb.ll.nlink == 1) { |
| 1318 |
hcb_put(&cb); |
| 1319 |
pthread_mutex_lock(&posixovl_protect); |
| 1320 |
unlinkat(root_fd, at(l1_path), 0); |
| 1321 |
unlinkat(root_fd, at(cb.path), 0); |
| 1322 |
pthread_mutex_unlock(&posixovl_protect); |
| 1323 |
return 0; |
| 1324 |
} |
| 1325 |
|
| 1326 |
--cb.ll.nlink; |
| 1327 |
return hcb_update(&cb); |
| 1328 |
} |
| 1329 |
|
| 1330 |
/** |
| 1331 |
* hl_instantiate - make (another) hardlink |
| 1332 |
* @oldpath: |
| 1333 |
* @newpath: |
| 1334 |
*/ |
| 1335 |
static int hl_instantiate(const char *oldpath, const char *newpath) |
| 1336 |
{ |
| 1337 |
struct hcb cb_old, cb_new; |
| 1338 |
int fd, ret; |
| 1339 |
|
| 1340 |
pthread_mutex_lock(&posixovl_protect); |
| 1341 |
ret = hcb_lookup(oldpath, &cb_old); |
| 1342 |
if (ret == -ENOENT_HCB || (ret == 0 && !S_ISHARDLNK(cb_old.ll.mode))) { |
| 1343 |
/* |
| 1344 |
* If no HCB attached or if not a hardlink slave... |
| 1345 |
* Transform file from state 0/1 to 2. |
| 1346 |
*/ |
| 1347 |
if ((ret = hl_promote(oldpath, &cb_old, |
| 1348 |
ret != -ENOENT_HCB)) < 0) |
| 1349 |
goto unlock_and_out; |
| 1350 |
/* |
| 1351 |
* Relookup to get the dnode file path |
| 1352 |
*/ |
| 1353 |
if ((ret = hcb_lookup(oldpath, &cb_old)) < 0) |
| 1354 |
goto unlock_and_out; |
| 1355 |
} else if (ret < 0) { |
| 1356 |
pthread_mutex_unlock(&posixovl_protect); |
| 1357 |
return -errno; |
| 1358 |
} |
| 1359 |
|
| 1360 |
pthread_mutex_unlock(&posixovl_protect); |
| 1361 |
|
| 1362 |
/* now we can do the Nth link */ |
| 1363 |
if ((ret = hl_up_nlink(cb_old.ll.target)) < 0) |
| 1364 |
return ret; |
| 1365 |
|
| 1366 |
if ((ret = hcb_new(newpath, &cb_new, 0)) < 0) |
| 1367 |
goto out; |
| 1368 |
cb_new.ll.mode = S_IFHARDLNK; |
| 1369 |
cb_new.ll.nlink = 1; |
| 1370 |
strlcpy(cb_new.ll.new_target, cb_old.ll.target, |
| 1371 |
sizeof(cb_new.ll.new_target)); |
| 1372 |
if ((ret = hcb_update(&cb_new)) < 0) |
| 1373 |
goto out; |
| 1374 |
|
| 1375 |
fd = openat(root_fd, at(newpath), O_WRONLY | O_CREAT | O_EXCL, 0); |
| 1376 |
if (fd < 0) { |
| 1377 |
ret = -errno; |
| 1378 |
goto out2; |
| 1379 |
} |
| 1380 |
if (hardlink_with_copy) |
| 1381 |
posixovl_fcopy(fd, cb_new.ll.new_target); |
| 1382 |
close(fd); |
| 1383 |
return 0; |
| 1384 |
|
| 1385 |
out2: |
| 1386 |
unlinkat(root_fd, at(cb_new.path), 0); |
| 1387 |
out: |
| 1388 |
hl_drop(cb_old.ll.target); |
| 1389 |
return ret; |
| 1390 |
|
| 1391 |
unlock_and_out: |
| 1392 |
pthread_mutex_unlock(&posixovl_protect); |
| 1393 |
return ret; |
| 1394 |
} |
| 1395 |
|
| 1396 |
static int posixovl_link(const char *oldpath, const char *newpath) |
| 1397 |
{ |
| 1398 |
int ret; |
| 1399 |
|
| 1400 |
if (is_resv(oldpath)) |
| 1401 |
return -ENOENT; |
| 1402 |
if (is_resv(newpath)) |
| 1403 |
return -EPERM; |
| 1404 |
if (could_be_too_long(oldpath) || could_be_too_long(newpath)) |
| 1405 |
return -ENAMETOOLONG; |
| 1406 |
|
| 1407 |
/* |
| 1408 |
* Kernel/FUSE already takes care of prohibiting hardlinking |
| 1409 |
* directories. We never get to see these. |
| 1410 |
*/ |
| 1411 |
if (!assume_vfat) { |
| 1412 |
ret = linkat(root_fd, at(oldpath), root_fd, at(newpath), 0); |
| 1413 |
if (ret < 0 && errno != EPERM) |
| 1414 |
return ret; |
| 1415 |
else if (ret >= 0) |
| 1416 |
return 0; |
| 1417 |
} |
| 1418 |
|
| 1419 |
return hl_instantiate(oldpath, newpath); |
| 1420 |
} |
| 1421 |
|
| 1422 |
static int posixovl1_link(const char *oldpath, const char *newpath) |
| 1423 |
{ |
| 1424 |
char *xoldpath = xfrm_to_disk(oldpath); |
| 1425 |
char *xnewpath = xfrm_to_disk(newpath); |
| 1426 |
int ret = posixovl_link(xoldpath, xnewpath); |
| 1427 |
free(xoldpath); |
| 1428 |
free(xnewpath); |
| 1429 |
return ret; |
| 1430 |
} |
| 1431 |
|
| 1432 |
static int posixovl_listxattr(const char *path, char *list, size_t size) |
| 1433 |
{ |
| 1434 |
return retcode_int(llistxattr(at(path), list, size)); |
| 1435 |
} |
| 1436 |
|
| 1437 |
static int posixovl_mkdir(const char *path, mode_t mode) |
| 1438 |
{ |
| 1439 |
const struct fuse_context *ctx; |
| 1440 |
struct hcb cb; |
| 1441 |
int ret; |
| 1442 |
|
| 1443 |
if (is_resv(path)) |
| 1444 |
return -EPERM; |
| 1445 |
if (could_be_too_long(path)) |
| 1446 |
return -ENAMETOOLONG; |
| 1447 |
|
| 1448 |
ret = mkdirat(root_fd, at(path), mode); |
| 1449 |
if (ret < 0) |
| 1450 |
return -errno; |
| 1451 |
|
| 1452 |
/* FUSE oddity: @mode does not include S_IFDIR */ |
| 1453 |
ctx = fuse_get_context(); |
| 1454 |
|
| 1455 |
if (((mode & ~S_IWUSR) != ((default_mode & ~S_IWUSR) | S_IXUGO) && |
| 1456 |
!supports_permissions(path)) || |
| 1457 |
(!parent_owner_match(path, ctx->uid) && |
| 1458 |
!supports_owners(path, ctx->uid, ctx->gid, true))) { |
| 1459 |
if ((ret = hcb_new(path, &cb, 0)) < 0) |
| 1460 |
return 0; |
| 1461 |
/* nlink already set (hcb_new() stat'ed @path) */ |
| 1462 |
cb.ll.mode = S_IFDIR | mode; |
| 1463 |
cb.ll.uid = ctx->uid; |
| 1464 |
cb.ll.gid = ctx->gid; |
| 1465 |
hcb_update(&cb); |
| 1466 |
} |
| 1467 |
|
| 1468 |
return 0; |
| 1469 |
} |
| 1470 |
|
| 1471 |
static int posixovl1_mkdir(const char *path, mode_t mode) |
| 1472 |
{ |
| 1473 |
char *xpath = xfrm_to_disk(path); |
| 1474 |
int ret = posixovl_mkdir(xpath, mode); |
| 1475 |
free(xpath); |
| 1476 |
return ret; |
| 1477 |
} |
| 1478 |
|
| 1479 |
static int posixovl_mknod(const char *path, mode_t mode, dev_t rdev) |
| 1480 |
{ |
| 1481 |
const struct fuse_context *ctx; |
| 1482 |
struct hcb info; |
| 1483 |
int fd, ret; |
| 1484 |
|
| 1485 |
if (is_resv(path)) |
| 1486 |
return -EPERM; |
| 1487 |
|
| 1488 |
if (!assume_vfat) { |
| 1489 |
ret = mknodat(root_fd, at(path), mode, rdev); |
| 1490 |
if (ret < 0 && errno != EPERM) |
| 1491 |
return ret; |
| 1492 |
else if (ret >= 0) |
| 1493 |
return 0; |
| 1494 |
} |
| 1495 |
|
| 1496 |
/* |
| 1497 |
* The HCB is created first - since that one does not show up in |
| 1498 |
* readdir() and is not accessible either. |
| 1499 |
* Same goes for posixovl_symlink(). |
| 1500 |
*/ |
| 1501 |
if ((ret = hcb_new(path, &info, 0)) < 0) |
| 1502 |
return ret; |
| 1503 |
|
| 1504 |
ctx = fuse_get_context(); |
| 1505 |
info.ll.mode = mode; |
| 1506 |
info.ll.nlink = 1; |
| 1507 |
info.ll.uid = ctx->uid; |
| 1508 |
info.ll.gid = ctx->gid; |
| 1509 |
info.ll.rdev = rdev; |
| 1510 |
if ((ret = hcb_update(&info)) < 0) |
| 1511 |
return ret; |
| 1512 |
|
| 1513 |
fd = openat(root_fd, at(path), O_WRONLY | O_CREAT | O_EXCL, 0); |
| 1514 |
if (fd < 0) { |
| 1515 |
ret = -errno; |
| 1516 |
unlinkat(root_fd, at(info.path), 0); |
| 1517 |
} |
| 1518 |
close(fd); |
| 1519 |
return ret; |
| 1520 |
} |
| 1521 |
|
| 1522 |
static int posixovl1_mknod(const char *path, mode_t mode, dev_t rdev) |
| 1523 |
{ |
| 1524 |
char *xpath = xfrm_to_disk(path); |
| 1525 |
int ret = posixovl_mknod(xpath, mode, rdev); |
| 1526 |
free(xpath); |
| 1527 |
return ret; |
| 1528 |
} |
| 1529 |
|
| 1530 |
static int posixovl_open(const char *path, struct fuse_file_info *filp) |
| 1531 |
{ |
| 1532 |
struct hcb info; |
| 1533 |
int fd, ret; |
| 1534 |
|
| 1535 |
if (is_resv(path)) |
| 1536 |
return -ENOENT; |
| 1537 |
|
| 1538 |
if ((filp->flags & O_ACCMODE) == O_WRONLY || |
| 1539 |
(filp->flags & O_ACCMODE) == O_RDWR) |
| 1540 |
if ((ret = hl_try_demote(path)) < 0) |
| 1541 |
return ret; |
| 1542 |
|
| 1543 |
ret = hcb_lookup(path, &info); |
| 1544 |
if (ret < 0 && ret != -ENOENT_HCB) |
| 1545 |
return ret; |
| 1546 |
if (ret == 0 && S_ISHARDLNK(info.ll.mode)) |
| 1547 |
fd = openat(root_fd, at(info.ll.target), filp->flags); |
| 1548 |
else |
| 1549 |
/* |
| 1550 |
* no need to handle non-regular files -- kernel and fuse do |
| 1551 |
* that for us. |
| 1552 |
*/ |
| 1553 |
fd = openat(root_fd, at(path), filp->flags); |
| 1554 |
|
| 1555 |
if (fd < 0) |
| 1556 |
return -errno; |
| 1557 |
|
| 1558 |
filp->fh = fd; |
| 1559 |
return 0; |
| 1560 |
} |
| 1561 |
|
| 1562 |
static int posixovl1_open(const char *path, struct fuse_file_info *filp) |
| 1563 |
{ |
| 1564 |
char *xpath = xfrm_to_disk(path); |
| 1565 |
int ret = posixovl_open(xpath, filp); |
| 1566 |
free(xpath); |
| 1567 |
return ret; |
| 1568 |
} |
| 1569 |
|
| 1570 |
static int posixovl_read(const char *path, char *buffer, size_t size, |
| 1571 |
off_t offset, struct fuse_file_info *filp) |
| 1572 |
{ |
| 1573 |
return retcode_ssize(pread(filp->fh, buffer, size, offset)); |
| 1574 |
} |
| 1575 |
|
| 1576 |
static int posixovl_readdir(const char *path, void *buffer, |
| 1577 |
fuse_fill_dir_t filldir, off_t offset, struct fuse_file_info *filp) |
| 1578 |
{ |
| 1579 |
const struct dirent *dentry; |
| 1580 |
struct hcb info; |
| 1581 |
int ret = 0; |
| 1582 |
DIR *ptr; |
| 1583 |
char *x; |
| 1584 |
|
| 1585 |
if (is_resv(path)) |
| 1586 |
return -ENOENT; |
| 1587 |
/* |
| 1588 |
* Current working directory is root_fd (per posixovl_init()). |
| 1589 |
* Let's hope opendir(relative_path) works. |
| 1590 |
*/ |
| 1591 |
if ((ptr = opendir(at(path))) == NULL) |
| 1592 |
return -errno; |
| 1593 |
|
| 1594 |
while ((dentry = readdir(ptr)) != NULL) { |
| 1595 |
if (is_resv_name(dentry->d_name)) |
| 1596 |
continue; |
| 1597 |
ret = hcb_lookup_readdir(path, dentry->d_name, &info); |
| 1598 |
if (ret < 0 && ret != -ENOENT_HCB && ret != -EACCES) |
| 1599 |
break; |
| 1600 |
ret = 0; |
| 1601 |
x = xfrm_from_disk((char *)dentry->d_name); |
| 1602 |
if ((*filldir)(buffer, x, &info.sb, 0) > 0) |
| 1603 |
break; |
| 1604 |
free(x); |
| 1605 |
} |
| 1606 |
|
| 1607 |
closedir(ptr); |
| 1608 |
return ret; |
| 1609 |
} |
| 1610 |
|
| 1611 |
static int posixovl1_readdir(const char *path, void *buffer, |
| 1612 |
fuse_fill_dir_t filldir, off_t offset, struct fuse_file_info *filp) |
| 1613 |
{ |
| 1614 |
char *xpath = xfrm_to_disk(path); |
| 1615 |
int ret = posixovl_readdir(xpath, buffer, filldir, offset, filp); |
| 1616 |
free(xpath); |
| 1617 |
return ret; |
| 1618 |
} |
| 1619 |
|
| 1620 |
static int posixovl_readlink(const char *path, char *dest, size_t size) |
| 1621 |
{ |
| 1622 |
struct hcb info; |
| 1623 |
int ret; |
| 1624 |
|
| 1625 |
if (is_resv(path)) |
| 1626 |
return -ENOENT; |
| 1627 |
|
| 1628 |
ret = readlinkat(root_fd, at(path), dest, size - 1); |
| 1629 |
if (ret < 0 && errno != EINVAL) { |
| 1630 |
return ret; |
| 1631 |
} else if (ret >= 0) { |
| 1632 |
dest[ret] = '\0'; |
| 1633 |
return 0; |
| 1634 |
} |
| 1635 |
|
| 1636 |
ret = hcb_lookup_deref(path, &info); |
| 1637 |
if (ret == -ENOENT_HCB) |
| 1638 |
return -EINVAL; /* not a symlink */ |
| 1639 |
else if (ret < 0) |
| 1640 |
return ret; |
| 1641 |
if (!S_ISLNK(info.ll.mode)) |
| 1642 |
return -EINVAL; /* not a symbolic link */ |
| 1643 |
|
| 1644 |
memset(dest, 0, size); |
| 1645 |
strlcpy(dest, info.ll.target, size); |
| 1646 |
return 0; |
| 1647 |
} |
| 1648 |
|
| 1649 |
static int posixovl1_readlink(const char *path, char *dest, size_t size) |
| 1650 |
{ |
| 1651 |
char *xpath = xfrm_to_disk(path); |
| 1652 |
int ret = posixovl_readlink(xpath, dest, size); |
| 1653 |
free(xpath); |
| 1654 |
return ret; |
| 1655 |
} |
| 1656 |
|
| 1657 |
static int posixovl_removexattr(const char *path, const char *name) |
| 1658 |
{ |
| 1659 |
return retcode_int(lremovexattr(path, name)); |
| 1660 |
} |
| 1661 |
|
| 1662 |
static int posixovl_rename(const char *oldpath, const char *newpath) |
| 1663 |
{ |
| 1664 |
char new_hcbpath[PATH_MAX]; |
| 1665 |
struct hcb old_info; |
| 1666 |
int ret, ret_2; |
| 1667 |
|
| 1668 |
if (is_resv(oldpath)) |
| 1669 |
return -ENOENT; |
| 1670 |
if (is_resv(newpath)) |
| 1671 |
return -EPERM; |
| 1672 |
if (could_be_too_long(newpath)) |
| 1673 |
return -ENAMETOOLONG; |
| 1674 |
|
| 1675 |
ret = hcb_lookup(oldpath, &old_info); |
| 1676 |
if (ret == -ENOENT_HCB || S_ISDIR(old_info.sb.st_mode)) |
| 1677 |
return retcode_int(renameat(root_fd, at(oldpath), |
| 1678 |
root_fd, at(newpath))); |
| 1679 |
else if (ret < 0) |
| 1680 |
return ret; |
| 1681 |
|
| 1682 |
if ((ret = real_to_hcb(new_hcbpath, newpath)) < 0) |
| 1683 |
return ret; |
| 1684 |
|
| 1685 |
pthread_mutex_lock(&posixovl_protect); |
| 1686 |
ret_2 = renameat(root_fd, at(oldpath), root_fd, at(newpath)); |
| 1687 |
if (ret_2 < 0) { |
| 1688 |
ret = -errno; |
| 1689 |
pthread_mutex_unlock(&posixovl_protect); |
| 1690 |
return ret; |
| 1691 |
} |
| 1692 |
ret_2 = renameat(root_fd, at(old_info.path), root_fd, at(new_hcbpath)); |
| 1693 |
if (ret_2 < 0) { |
| 1694 |
/* !@#$%^& - error. Need to rename old file back. */ |
| 1695 |
ret = -errno; |
| 1696 |
if (renameat(root_fd, at(newpath), root_fd, at(oldpath)) < 0) { |
| 1697 |
/* Even that failed. Keep new name, but kill HCB. */ |
| 1698 |
unlinkat(root_fd, at(old_info.path), 0); |
| 1699 |
hcb_got_busted(old_info.path); |
| 1700 |
} |
| 1701 |
|
| 1702 |
pthread_mutex_unlock(&posixovl_protect); |
| 1703 |
return ret; |
| 1704 |
} |
| 1705 |
|
| 1706 |
pthread_mutex_unlock(&posixovl_protect); |
| 1707 |
return 0; |
| 1708 |
} |
| 1709 |
|
| 1710 |
static int posixovl1_rename(const char *oldpath, const char *newpath) |
| 1711 |
{ |
| 1712 |
char *xoldpath = xfrm_to_disk(oldpath); |
| 1713 |
char *xnewpath = xfrm_to_disk(newpath); |
| 1714 |
int ret = posixovl_rename(xoldpath, xnewpath); |
| 1715 |
free(xoldpath); |
| 1716 |
free(xnewpath); |
| 1717 |
return ret; |
| 1718 |
} |
| 1719 |
|
| 1720 |
static int posixovl_rmdir(const char *path) |
| 1721 |
{ |
| 1722 |
struct hcb info; |
| 1723 |
int ret; |
| 1724 |
|
| 1725 |
if (is_resv(path)) |
| 1726 |
return -ENOENT; |
| 1727 |
ret = hcb_lookup(path, &info); |
| 1728 |
if (ret == 0 && unlinkat(root_fd, at(info.path), 0) < 0) |
| 1729 |
return -errno; |
| 1730 |
return retcode_int(unlinkat(root_fd, at(path), AT_REMOVEDIR)); |
| 1731 |
} |
| 1732 |
|
| 1733 |
static int posixovl1_rmdir(const char *path) |
| 1734 |
{ |
| 1735 |
char *xpath = xfrm_to_disk(path); |
| 1736 |
int ret = posixovl_rmdir(xpath); |
| 1737 |
free(xpath); |
| 1738 |
return ret; |
| 1739 |
} |
| 1740 |
|
| 1741 |
static int posixovl_setxattr(const char *path, const char *name, |
| 1742 |
const char *value, size_t size, int flags) |
| 1743 |
{ |
| 1744 |
return retcode_int(lsetxattr(at(path), name, value, size, flags)); |
| 1745 |
} |
| 1746 |
|
| 1747 |
static int posixovl_statfs(const char *path, struct statvfs *sb) |
| 1748 |
{ |
| 1749 |
if (fstatvfs(root_fd, sb) < 0) |
| 1750 |
return -errno; |
| 1751 |
sb->f_fsid = 0; |
| 1752 |
return 0; |
| 1753 |
} |
| 1754 |
|
| 1755 |
static int posixovl_symlink(const char *oldpath, const char *newpath) |
| 1756 |
{ |
| 1757 |
const struct fuse_context *ctx; |
| 1758 |
struct hcb info; |
| 1759 |
int fd, ret; |
| 1760 |
|
| 1761 |
if (is_resv(newpath)) |
| 1762 |
return -EPERM; |
| 1763 |
|
| 1764 |
if (!assume_vfat) { |
| 1765 |
ret = symlinkat(oldpath, root_fd, at(newpath)); |
| 1766 |
if (ret < 0 && errno != EPERM) |
| 1767 |
return -errno; |
| 1768 |
else if (ret >= 0) |
| 1769 |
return 0; |
| 1770 |
} |
| 1771 |
|
| 1772 |
/* symlink() not supported on @path */ |
| 1773 |
if ((ret = hcb_new(newpath, &info, 0)) < 0) |
| 1774 |
return ret; |
| 1775 |
|
| 1776 |
ctx = fuse_get_context(); |
| 1777 |
info.ll.mode = S_IFSOFTLNK; |
| 1778 |
info.ll.nlink = 1; |
| 1779 |
info.ll.uid = ctx->uid; |
| 1780 |
info.ll.gid = ctx->gid; |
| 1781 |
strlcpy(info.ll.new_target, oldpath, sizeof(info.ll.new_target)); |
| 1782 |
if ((ret = hcb_update(&info)) < 0) |
| 1783 |
return ret; |
| 1784 |
|
| 1785 |
fd = openat(root_fd, at(newpath), O_WRONLY | O_CREAT | O_EXCL, 0); |
| 1786 |
if (fd < 0) { |
| 1787 |
ret = -errno; |
| 1788 |
unlinkat(root_fd, at(info.path), 0); |
| 1789 |
} else { |
| 1790 |
close(fd); |
| 1791 |
} |
| 1792 |
|
| 1793 |
return ret; |
| 1794 |
} |
| 1795 |
|
| 1796 |
static int posixovl1_symlink(const char *oldpath, const char *newpath) |
| 1797 |
{ |
| 1798 |
char *xoldpath = xfrm_to_disk(oldpath); |
| 1799 |
char *xnewpath = xfrm_to_disk(newpath); |
| 1800 |
int ret = posixovl_symlink(xoldpath, xnewpath); |
| 1801 |
free(xoldpath); |
| 1802 |
free(xnewpath); |
| 1803 |
return ret; |
| 1804 |
} |
| 1805 |
|
| 1806 |
static int posixovl_truncate(const char *path, off_t length) |
| 1807 |
{ |
| 1808 |
struct hcb info; |
| 1809 |
int fd, ret; |
| 1810 |
|
| 1811 |
if (is_resv(path)) |
| 1812 |
return -ENOENT; |
| 1813 |
if ((ret = hl_try_demote(path)) < 0) |
| 1814 |
return ret; |
| 1815 |
|
| 1816 |
ret = hcb_lookup_deref(path, &info); |
| 1817 |
if (ret < 0 && ret != -ENOENT_HCB) |
| 1818 |
return -errno; |
| 1819 |
else if (ret == 0 && !S_ISREG(info.ll.mode) && !S_ISDIR(info.ll.mode)) |
| 1820 |
/* |
| 1821 |
* A HCB was found. But truncating special |
| 1822 |
* files (e.g. /dev/zero) is invalid. |
| 1823 |
*/ |
| 1824 |
return -EINVAL; |
| 1825 |
|
| 1826 |
ret = hcb_lookup(path, &info); |
| 1827 |
if (ret < 0 && ret != -ENOENT_HCB) |
| 1828 |
return ret; |
| 1829 |
|
| 1830 |
/* |
| 1831 |
* There is no ftruncateat(), so need to use openat()+ftruncate() here. |
| 1832 |
*/ |
| 1833 |
if (ret == 0 && S_ISHARDLNK(info.ll.mode)) |
| 1834 |
fd = openat(root_fd, at(info.ll.target), O_WRONLY); |
| 1835 |
else |
| 1836 |
fd = openat(root_fd, at(path), O_WRONLY); |
| 1837 |
|
| 1838 |
if (fd < 0) |
| 1839 |
return -errno; |
| 1840 |
ret = 0; |
| 1841 |
if (ftruncate(fd, length) < 0) |
| 1842 |
ret = -errno; |
| 1843 |
|
| 1844 |
close(fd); |
| 1845 |
return ret; |
| 1846 |
} |
| 1847 |
|
| 1848 |
static int posixovl1_truncate(const char *path, off_t len) |
| 1849 |
{ |
| 1850 |
char *xpath = xfrm_to_disk(path); |
| 1851 |
int ret = posixovl_truncate(xpath, len); |
| 1852 |
free(xpath); |
| 1853 |
return ret; |
| 1854 |
} |
| 1855 |
|
| 1856 |
static int posixovl_unlink(const char *path) |
| 1857 |
{ |
| 1858 |
struct hcb info; |
| 1859 |
int ret, h_ret; |
| 1860 |
|
| 1861 |
if (is_resv(path)) |
| 1862 |
return -ENOENT; |
| 1863 |
|
| 1864 |
/* |
| 1865 |
* Need to unlink the real file first so that the potential case |
| 1866 |
* "HCB non-existent but real file existent" does not happen in |
| 1867 |
* readdir(). |
| 1868 |
*/ |
| 1869 |
h_ret = hcb_lookup(path, &info); |
| 1870 |
if (h_ret < 0 && h_ret != -ENOENT_HCB) |
| 1871 |
return h_ret; |
| 1872 |
|
| 1873 |
ret = unlinkat(root_fd, at(path), 0); |
| 1874 |
if (ret < 0) |
| 1875 |
return -errno; |
| 1876 |
|
| 1877 |
if (h_ret == 0) { |
| 1878 |
unlinkat(root_fd, at(info.path), 0); |
| 1879 |
if (S_ISHARDLNK(info.ll.mode)) |
| 1880 |
hl_drop(info.ll.target); |
| 1881 |
} |
| 1882 |
|
| 1883 |
return 0; |
| 1884 |
} |
| 1885 |
|
| 1886 |
static int posixovl1_unlink(const char *path) |
| 1887 |
{ |
| 1888 |
char *xpath = xfrm_to_disk(path); |
| 1889 |
int ret = posixovl_unlink(xpath); |
| 1890 |
free(xpath); |
| 1891 |
return ret; |
| 1892 |
} |
| 1893 |
|
| 1894 |
static int posixovl_utimens(const char *path, const struct timespec *ts) |
| 1895 |
{ |
| 1896 |
struct hcb info; |
| 1897 |
int ret; |
| 1898 |
#ifndef HAVE_UTIMENSAT |
| 1899 |
struct timeval tv[2]; |
| 1900 |
#endif |
| 1901 |
|
| 1902 |
if (is_resv(path)) |
| 1903 |
return -ENOENT; |
| 1904 |
|
| 1905 |
#ifndef HAVE_UTIMENSAT |
| 1906 |
tv[0].tv_sec = ts[0].tv_sec; |
| 1907 |
tv[0].tv_usec = ts[0].tv_nsec / 1000; |
| 1908 |
tv[1].tv_sec = ts[1].tv_sec; |
| 1909 |
tv[1].tv_usec = ts[1].tv_nsec / 1000; |
| 1910 |
#endif |
| 1911 |
|
| 1912 |
ret = hcb_lookup(path, &info); |
| 1913 |
if (ret < 0 && ret != -ENOENT_HCB) |
| 1914 |
return ret; |
| 1915 |
|
| 1916 |
/* |
| 1917 |
* The time attributes are always applied to the plain file, |
| 1918 |
* never the special file. |
| 1919 |
* (Until a filesystem that cannot store times comes along.) |
| 1920 |
* In case of S_IFHARDLNK, the .pxovd. file carries the stamp. |
| 1921 |
*/ |
| 1922 |
|
| 1923 |
#ifndef HAVE_UTIMENSAT |
| 1924 |
if (ret == 0 && S_ISHARDLNK(info.ll.mode)) |
| 1925 |
ret = futimesat(root_fd, at(info.ll.target), tv); |
| 1926 |
else |
| 1927 |
ret = futimesat(root_fd, at(path), tv); |
| 1928 |
#else |
| 1929 |
if (ret == 0 && S_ISHARDLNK(info.ll.mode)) |
| 1930 |
ret = utimensat(root_fd, at(info.ll.target), |
| 1931 |
ts, AT_SYMLINK_NOFOLLOW); |
| 1932 |
else |
| 1933 |
ret = utimensat(root_fd, at(path), ts, AT_SYMLINK_NOFOLLOW); |
| 1934 |
#endif |
| 1935 |
return retcode_int(ret); |
| 1936 |
} |
| 1937 |
|
| 1938 |
static int posixovl1_utimens(const char *path, const struct timespec *ts) |
| 1939 |
{ |
| 1940 |
char *xpath = xfrm_to_disk(path); |
| 1941 |
int ret = posixovl_utimens(xpath, ts); |
| 1942 |
free(xpath); |
| 1943 |
return ret; |
| 1944 |
} |
| 1945 |
|
| 1946 |
static int posixovl_write(const char *path, const char *buffer, size_t size, |
| 1947 |
off_t offset, struct fuse_file_info *filp) |
| 1948 |
{ |
| 1949 |
return retcode_ssize(pwrite(filp->fh, buffer, size, offset)); |
| 1950 |
} |
| 1951 |
|
| 1952 |
static bool user_allow_other(void) |
| 1953 |
{ |
| 1954 |
bool ret = false; |
| 1955 |
char buf[64]; |
| 1956 |
FILE *fp; |
| 1957 |
|
| 1958 |
if ((fp = fopen("/etc/fuse.conf", "r")) == NULL) |
| 1959 |
return false; |
| 1960 |
while (fgets(buf, sizeof(buf), fp) != NULL) |
| 1961 |
/* no fancy line ending checks or anything */ |
| 1962 |
if (strncmp(buf, "user_allow_other", |
| 1963 |
sizeof("user_allow_other") - 1) == 0) { |
| 1964 |
ret = true; |
| 1965 |
break; |
| 1966 |
} |
| 1967 |
|
| 1968 |
fclose(fp); |
| 1969 |
return ret; |
| 1970 |
} |
| 1971 |
|
| 1972 |
static const struct fuse_operations posixovl_ops = { |
| 1973 |
.chmod = posixovl1_chmod, |
| 1974 |
.chown = posixovl1_chown, |
| 1975 |
.create = posixovl1_create, |
| 1976 |
.fgetattr = posixovl1_fgetattr, |
| 1977 |
.ftruncate = posixovl_ftruncate, |
| 1978 |
.getattr = posixovl1_getattr, |
| 1979 |
.getxattr = posixovl_getxattr, |
| 1980 |
.init = posixovl_init, |
| 1981 |
.link = posixovl1_link, |
| 1982 |
.listxattr = posixovl_listxattr, |
| 1983 |
.mkdir = posixovl1_mkdir, |
| 1984 |
.mknod = posixovl1_mknod, |
| 1985 |
.open = posixovl1_open, |
| 1986 |
.read = posixovl_read, |
| 1987 |
.readdir = posixovl1_readdir, |
| 1988 |
.readlink = posixovl1_readlink, |
| 1989 |
.release = posixovl_close, |
| 1990 |
.removexattr = posixovl_removexattr, |
| 1991 |
.rename = posixovl1_rename, |
| 1992 |
.rmdir = posixovl1_rmdir, |
| 1993 |
.setxattr = posixovl_setxattr, |
| 1994 |
.statfs = posixovl_statfs, |
| 1995 |
.symlink = posixovl1_symlink, |
| 1996 |
.truncate = posixovl1_truncate, |
| 1997 |
.unlink = posixovl1_unlink, |
| 1998 |
.utimens = posixovl1_utimens, |
| 1999 |
.write = posixovl_write, |
| 2000 |
}; |
| 2001 |
|
| 2002 |
static void usage(const char *p) |
| 2003 |
{ |
| 2004 |
fprintf(stderr, |
| 2005 |
"Usage: %s [-FH] [-S source] mountpoint [-- fuseoptions]\n", p); |
| 2006 |
exit(EXIT_FAILURE); |
| 2007 |
} |
| 2008 |
|
| 2009 |
int main(int argc, char **argv) |
| 2010 |
{ |
| 2011 |
char **aptr, **new_argv; |
| 2012 |
int new_argc = 0, original_wd, c; |
| 2013 |
char xargs[256]; |
| 2014 |
char *mount_option = NULL; |
| 2015 |
|
| 2016 |
while ((c = getopt(argc, argv, "1FHS:o:")) > 0) { |
| 2017 |
switch (c) { |
| 2018 |
case '1': |
| 2019 |
single_threaded = true; |
| 2020 |
break; |
| 2021 |
case 'F': |
| 2022 |
assume_vfat = true; |
| 2023 |
break; |
| 2024 |
case 'H': |
| 2025 |
hardlink_with_copy = true; |
| 2026 |
break; |
| 2027 |
case 'S': |
| 2028 |
root_dir = optarg; |
| 2029 |
break; |
| 2030 |
case 'o': |
| 2031 |
mount_option = optarg; |
| 2032 |
break; |
| 2033 |
default: |
| 2034 |
usage(*argv); |
| 2035 |
return EXIT_FAILURE; |
| 2036 |
} |
| 2037 |
} |
| 2038 |
|
| 2039 |
if (argv[optind] == NULL) |
| 2040 |
usage(*argv); |
| 2041 |
if (root_dir == NULL) |
| 2042 |
{ |
| 2043 |
root_dir = argv[optind]; |
| 2044 |
/* If there is another one, it is a target path. */ |
| 2045 |
if (optind + 1 < argc) |
| 2046 |
optind++; |
| 2047 |
} |
| 2048 |
|
| 2049 |
umask(0); |
| 2050 |
if ((root_fd = open(root_dir, O_DIRECTORY)) < 0) { |
| 2051 |
fprintf(stderr, "Could not open(\"%s\"): %s\n", |
| 2052 |
root_dir, strerror(errno)); |
| 2053 |
return EXIT_FAILURE; |
| 2054 |
} |
| 2055 |
|
| 2056 |
original_wd = open(".", O_DIRECTORY); |
| 2057 |
|
| 2058 |
new_argv = malloc(sizeof(char *) * (argc + 7 - optind)); |
| 2059 |
new_argv[new_argc++] = argv[0]; |
| 2060 |
#ifdef HAVE_JUST_FUSE_2_6_5 |
| 2061 |
snprintf(xargs, sizeof(xargs), |
| 2062 |
"-oattr_timeout=0,default_permissions,use_ino,nonempty,dev," |
| 2063 |
"fsname=posix-overlay(%s)", root_dir); |
| 2064 |
#else |
| 2065 |
snprintf(xargs, sizeof(xargs), |
| 2066 |
"-oattr_timeout=0,default_permissions,use_ino,nonempty,dev," |
| 2067 |
"fsname=posix-overlay(%s),subtype=posixovl", root_dir); |
| 2068 |
#endif |
| 2069 |
new_argv[new_argc++] = xargs; |
| 2070 |
|
| 2071 |
if (user_allow_other()) |
| 2072 |
new_argv[new_argc++] = "-oallow_other"; |
| 2073 |
if (single_threaded) |
| 2074 |
new_argv[new_argc++] = "-s"; |
| 2075 |
if (mount_option != NULL) |
| 2076 |
{ |
| 2077 |
new_argv[new_argc++] = "-o"; |
| 2078 |
new_argv[new_argc++] = mount_option; |
| 2079 |
} |
| 2080 |
|
| 2081 |
for (aptr = &argv[optind]; *aptr != NULL; ++aptr) |
| 2082 |
{ |
| 2083 |
new_argv[new_argc++] = *aptr; |
| 2084 |
} |
| 2085 |
|
| 2086 |
new_argv[new_argc] = NULL; |
| 2087 |
c = fuse_main(new_argc, (char **)new_argv, &posixovl_ops, NULL); |
| 2088 |
if (fchdir(original_wd) < 0) |
| 2089 |
/* ignore */{} |
| 2090 |
return c; |
| 2091 |
} |