9p-local.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600
  1. /*
  2. * 9p Posix callback
  3. *
  4. * Copyright IBM, Corp. 2010
  5. *
  6. * Authors:
  7. * Anthony Liguori <aliguori@us.ibm.com>
  8. *
  9. * This work is licensed under the terms of the GNU GPL, version 2. See
  10. * the COPYING file in the top-level directory.
  11. */
  12. #include "qemu/osdep.h"
  13. #include "9p.h"
  14. #include "9p-local.h"
  15. #include "9p-xattr.h"
  16. #include "9p-util.h"
  17. #include "fsdev/qemu-fsdev.h" /* local_ops */
  18. #include <arpa/inet.h>
  19. #include <pwd.h>
  20. #include <grp.h>
  21. #include <sys/socket.h>
  22. #include <sys/un.h>
  23. #include "qemu/xattr.h"
  24. #include "qapi/error.h"
  25. #include "qemu/cutils.h"
  26. #include "qemu/error-report.h"
  27. #include "qemu/option.h"
  28. #include <libgen.h>
  29. #include <linux/fs.h>
  30. #ifdef CONFIG_LINUX_MAGIC_H
  31. #include <linux/magic.h>
  32. #endif
  33. #include <sys/ioctl.h>
  34. #ifndef XFS_SUPER_MAGIC
  35. #define XFS_SUPER_MAGIC 0x58465342
  36. #endif
  37. #ifndef EXT2_SUPER_MAGIC
  38. #define EXT2_SUPER_MAGIC 0xEF53
  39. #endif
  40. #ifndef REISERFS_SUPER_MAGIC
  41. #define REISERFS_SUPER_MAGIC 0x52654973
  42. #endif
  43. #ifndef BTRFS_SUPER_MAGIC
  44. #define BTRFS_SUPER_MAGIC 0x9123683E
  45. #endif
  46. typedef struct {
  47. int mountfd;
  48. } LocalData;
  49. int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags,
  50. mode_t mode)
  51. {
  52. LocalData *data = fs_ctx->private;
  53. int fd = data->mountfd;
  54. while (*path && fd != -1) {
  55. const char *c;
  56. int next_fd;
  57. char *head;
  58. /* Only relative paths without consecutive slashes */
  59. assert(*path != '/');
  60. head = g_strdup(path);
  61. c = qemu_strchrnul(path, '/');
  62. if (*c) {
  63. /* Intermediate path element */
  64. head[c - path] = 0;
  65. path = c + 1;
  66. next_fd = openat_dir(fd, head);
  67. } else {
  68. /* Rightmost path element */
  69. next_fd = openat_file(fd, head, flags, mode);
  70. path = c;
  71. }
  72. g_free(head);
  73. if (fd != data->mountfd) {
  74. close_preserve_errno(fd);
  75. }
  76. fd = next_fd;
  77. }
  78. assert(fd != data->mountfd);
  79. return fd;
  80. }
  81. int local_opendir_nofollow(FsContext *fs_ctx, const char *path)
  82. {
  83. return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0);
  84. }
  85. static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd,
  86. const char *npath)
  87. {
  88. int serrno = errno;
  89. renameat(odirfd, opath, ndirfd, npath);
  90. errno = serrno;
  91. }
  92. static void unlinkat_preserve_errno(int dirfd, const char *path, int flags)
  93. {
  94. int serrno = errno;
  95. unlinkat(dirfd, path, flags);
  96. errno = serrno;
  97. }
  98. #define VIRTFS_META_DIR ".virtfs_metadata"
  99. #define VIRTFS_META_ROOT_FILE VIRTFS_META_DIR "_root"
  100. static FILE *local_fopenat(int dirfd, const char *name, const char *mode)
  101. {
  102. int fd, o_mode = 0;
  103. FILE *fp;
  104. int flags;
  105. /*
  106. * only supports two modes
  107. */
  108. if (mode[0] == 'r') {
  109. flags = O_RDONLY;
  110. } else if (mode[0] == 'w') {
  111. flags = O_WRONLY | O_TRUNC | O_CREAT;
  112. o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH;
  113. } else {
  114. return NULL;
  115. }
  116. fd = openat_file(dirfd, name, flags, o_mode);
  117. if (fd == -1) {
  118. return NULL;
  119. }
  120. fp = fdopen(fd, mode);
  121. if (!fp) {
  122. close(fd);
  123. }
  124. return fp;
  125. }
  126. #define ATTR_MAX 100
  127. static void local_mapped_file_attr(int dirfd, const char *name,
  128. struct stat *stbuf)
  129. {
  130. FILE *fp;
  131. char buf[ATTR_MAX];
  132. int map_dirfd;
  133. if (strcmp(name, ".")) {
  134. map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
  135. if (map_dirfd == -1) {
  136. return;
  137. }
  138. fp = local_fopenat(map_dirfd, name, "r");
  139. close_preserve_errno(map_dirfd);
  140. } else {
  141. fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "r");
  142. }
  143. if (!fp) {
  144. return;
  145. }
  146. memset(buf, 0, ATTR_MAX);
  147. while (fgets(buf, ATTR_MAX, fp)) {
  148. if (!strncmp(buf, "virtfs.uid", 10)) {
  149. stbuf->st_uid = atoi(buf+11);
  150. } else if (!strncmp(buf, "virtfs.gid", 10)) {
  151. stbuf->st_gid = atoi(buf+11);
  152. } else if (!strncmp(buf, "virtfs.mode", 11)) {
  153. stbuf->st_mode = atoi(buf+12);
  154. } else if (!strncmp(buf, "virtfs.rdev", 11)) {
  155. stbuf->st_rdev = atoi(buf+12);
  156. }
  157. memset(buf, 0, ATTR_MAX);
  158. }
  159. fclose(fp);
  160. }
  161. static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf)
  162. {
  163. int err = -1;
  164. char *dirpath = g_path_get_dirname(fs_path->data);
  165. char *name = g_path_get_basename(fs_path->data);
  166. int dirfd;
  167. dirfd = local_opendir_nofollow(fs_ctx, dirpath);
  168. if (dirfd == -1) {
  169. goto out;
  170. }
  171. err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW);
  172. if (err) {
  173. goto err_out;
  174. }
  175. if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
  176. /* Actual credentials are part of extended attrs */
  177. uid_t tmp_uid;
  178. gid_t tmp_gid;
  179. mode_t tmp_mode;
  180. dev_t tmp_dev;
  181. if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid,
  182. sizeof(uid_t)) > 0) {
  183. stbuf->st_uid = le32_to_cpu(tmp_uid);
  184. }
  185. if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid,
  186. sizeof(gid_t)) > 0) {
  187. stbuf->st_gid = le32_to_cpu(tmp_gid);
  188. }
  189. if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode,
  190. sizeof(mode_t)) > 0) {
  191. stbuf->st_mode = le32_to_cpu(tmp_mode);
  192. }
  193. if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev,
  194. sizeof(dev_t)) > 0) {
  195. stbuf->st_rdev = le64_to_cpu(tmp_dev);
  196. }
  197. } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
  198. local_mapped_file_attr(dirfd, name, stbuf);
  199. }
  200. err_out:
  201. close_preserve_errno(dirfd);
  202. out:
  203. g_free(name);
  204. g_free(dirpath);
  205. return err;
  206. }
  207. static int local_set_mapped_file_attrat(int dirfd, const char *name,
  208. FsCred *credp)
  209. {
  210. FILE *fp;
  211. int ret;
  212. char buf[ATTR_MAX];
  213. int uid = -1, gid = -1, mode = -1, rdev = -1;
  214. int map_dirfd = -1, map_fd;
  215. bool is_root = !strcmp(name, ".");
  216. if (is_root) {
  217. fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "r");
  218. if (!fp) {
  219. if (errno == ENOENT) {
  220. goto update_map_file;
  221. } else {
  222. return -1;
  223. }
  224. }
  225. } else {
  226. ret = mkdirat(dirfd, VIRTFS_META_DIR, 0700);
  227. if (ret < 0 && errno != EEXIST) {
  228. return -1;
  229. }
  230. map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
  231. if (map_dirfd == -1) {
  232. return -1;
  233. }
  234. fp = local_fopenat(map_dirfd, name, "r");
  235. if (!fp) {
  236. if (errno == ENOENT) {
  237. goto update_map_file;
  238. } else {
  239. close_preserve_errno(map_dirfd);
  240. return -1;
  241. }
  242. }
  243. }
  244. memset(buf, 0, ATTR_MAX);
  245. while (fgets(buf, ATTR_MAX, fp)) {
  246. if (!strncmp(buf, "virtfs.uid", 10)) {
  247. uid = atoi(buf + 11);
  248. } else if (!strncmp(buf, "virtfs.gid", 10)) {
  249. gid = atoi(buf + 11);
  250. } else if (!strncmp(buf, "virtfs.mode", 11)) {
  251. mode = atoi(buf + 12);
  252. } else if (!strncmp(buf, "virtfs.rdev", 11)) {
  253. rdev = atoi(buf + 12);
  254. }
  255. memset(buf, 0, ATTR_MAX);
  256. }
  257. fclose(fp);
  258. update_map_file:
  259. if (is_root) {
  260. fp = local_fopenat(dirfd, VIRTFS_META_ROOT_FILE, "w");
  261. } else {
  262. fp = local_fopenat(map_dirfd, name, "w");
  263. /* We can't go this far with map_dirfd not being a valid file descriptor
  264. * but some versions of gcc aren't smart enough to see it.
  265. */
  266. if (map_dirfd != -1) {
  267. close_preserve_errno(map_dirfd);
  268. }
  269. }
  270. if (!fp) {
  271. return -1;
  272. }
  273. map_fd = fileno(fp);
  274. assert(map_fd != -1);
  275. ret = fchmod(map_fd, 0600);
  276. assert(ret == 0);
  277. if (credp->fc_uid != -1) {
  278. uid = credp->fc_uid;
  279. }
  280. if (credp->fc_gid != -1) {
  281. gid = credp->fc_gid;
  282. }
  283. if (credp->fc_mode != (mode_t)-1) {
  284. mode = credp->fc_mode;
  285. }
  286. if (credp->fc_rdev != -1) {
  287. rdev = credp->fc_rdev;
  288. }
  289. if (uid != -1) {
  290. fprintf(fp, "virtfs.uid=%d\n", uid);
  291. }
  292. if (gid != -1) {
  293. fprintf(fp, "virtfs.gid=%d\n", gid);
  294. }
  295. if (mode != -1) {
  296. fprintf(fp, "virtfs.mode=%d\n", mode);
  297. }
  298. if (rdev != -1) {
  299. fprintf(fp, "virtfs.rdev=%d\n", rdev);
  300. }
  301. fclose(fp);
  302. return 0;
  303. }
  304. static int fchmodat_nofollow(int dirfd, const char *name, mode_t mode)
  305. {
  306. struct stat stbuf;
  307. int fd, ret;
  308. /* FIXME: this should be handled with fchmodat(AT_SYMLINK_NOFOLLOW).
  309. * Unfortunately, the linux kernel doesn't implement it yet.
  310. */
  311. /* First, we clear non-racing symlinks out of the way. */
  312. if (fstatat(dirfd, name, &stbuf, AT_SYMLINK_NOFOLLOW)) {
  313. return -1;
  314. }
  315. if (S_ISLNK(stbuf.st_mode)) {
  316. errno = ELOOP;
  317. return -1;
  318. }
  319. fd = openat_file(dirfd, name, O_RDONLY | O_PATH_9P_UTIL | O_NOFOLLOW, 0);
  320. #if O_PATH_9P_UTIL == 0
  321. /* Fallback for systems that don't support O_PATH: we depend on the file
  322. * being readable or writable.
  323. */
  324. if (fd == -1) {
  325. /* In case the file is writable-only and isn't a directory. */
  326. if (errno == EACCES) {
  327. fd = openat_file(dirfd, name, O_WRONLY, 0);
  328. }
  329. if (fd == -1 && errno == EISDIR) {
  330. errno = EACCES;
  331. }
  332. }
  333. if (fd == -1) {
  334. return -1;
  335. }
  336. ret = fchmod(fd, mode);
  337. #else
  338. /* Access modes are ignored when O_PATH is supported. If name is a symbolic
  339. * link, O_PATH | O_NOFOLLOW causes openat(2) to return a file descriptor
  340. * referring to the symbolic link.
  341. */
  342. if (fd == -1) {
  343. return -1;
  344. }
  345. /* Now we handle racing symlinks. */
  346. ret = fstat(fd, &stbuf);
  347. if (!ret) {
  348. if (S_ISLNK(stbuf.st_mode)) {
  349. errno = ELOOP;
  350. ret = -1;
  351. } else {
  352. char *proc_path = g_strdup_printf("/proc/self/fd/%d", fd);
  353. ret = chmod(proc_path, mode);
  354. g_free(proc_path);
  355. }
  356. }
  357. #endif
  358. close_preserve_errno(fd);
  359. return ret;
  360. }
  361. static int local_set_xattrat(int dirfd, const char *path, FsCred *credp)
  362. {
  363. int err;
  364. if (credp->fc_uid != -1) {
  365. uint32_t tmp_uid = cpu_to_le32(credp->fc_uid);
  366. err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.uid", &tmp_uid,
  367. sizeof(uid_t), 0);
  368. if (err) {
  369. return err;
  370. }
  371. }
  372. if (credp->fc_gid != -1) {
  373. uint32_t tmp_gid = cpu_to_le32(credp->fc_gid);
  374. err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.gid", &tmp_gid,
  375. sizeof(gid_t), 0);
  376. if (err) {
  377. return err;
  378. }
  379. }
  380. if (credp->fc_mode != (mode_t)-1) {
  381. uint32_t tmp_mode = cpu_to_le32(credp->fc_mode);
  382. err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.mode", &tmp_mode,
  383. sizeof(mode_t), 0);
  384. if (err) {
  385. return err;
  386. }
  387. }
  388. if (credp->fc_rdev != -1) {
  389. uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev);
  390. err = fsetxattrat_nofollow(dirfd, path, "user.virtfs.rdev", &tmp_rdev,
  391. sizeof(dev_t), 0);
  392. if (err) {
  393. return err;
  394. }
  395. }
  396. return 0;
  397. }
  398. static int local_set_cred_passthrough(FsContext *fs_ctx, int dirfd,
  399. const char *name, FsCred *credp)
  400. {
  401. if (fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
  402. AT_SYMLINK_NOFOLLOW) < 0) {
  403. /*
  404. * If we fail to change ownership and if we are
  405. * using security model none. Ignore the error
  406. */
  407. if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
  408. return -1;
  409. }
  410. }
  411. return fchmodat_nofollow(dirfd, name, credp->fc_mode & 07777);
  412. }
  413. static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path,
  414. char *buf, size_t bufsz)
  415. {
  416. ssize_t tsize = -1;
  417. if ((fs_ctx->export_flags & V9FS_SM_MAPPED) ||
  418. (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) {
  419. int fd;
  420. fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0);
  421. if (fd == -1) {
  422. return -1;
  423. }
  424. do {
  425. tsize = read(fd, (void *)buf, bufsz);
  426. } while (tsize == -1 && errno == EINTR);
  427. close_preserve_errno(fd);
  428. } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
  429. (fs_ctx->export_flags & V9FS_SM_NONE)) {
  430. char *dirpath = g_path_get_dirname(fs_path->data);
  431. char *name = g_path_get_basename(fs_path->data);
  432. int dirfd;
  433. dirfd = local_opendir_nofollow(fs_ctx, dirpath);
  434. if (dirfd == -1) {
  435. goto out;
  436. }
  437. tsize = readlinkat(dirfd, name, buf, bufsz);
  438. close_preserve_errno(dirfd);
  439. out:
  440. g_free(name);
  441. g_free(dirpath);
  442. }
  443. return tsize;
  444. }
  445. static int local_close(FsContext *ctx, V9fsFidOpenState *fs)
  446. {
  447. return close(fs->fd);
  448. }
  449. static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs)
  450. {
  451. return closedir(fs->dir.stream);
  452. }
  453. static int local_open(FsContext *ctx, V9fsPath *fs_path,
  454. int flags, V9fsFidOpenState *fs)
  455. {
  456. int fd;
  457. fd = local_open_nofollow(ctx, fs_path->data, flags, 0);
  458. if (fd == -1) {
  459. return -1;
  460. }
  461. fs->fd = fd;
  462. return fs->fd;
  463. }
  464. static int local_opendir(FsContext *ctx,
  465. V9fsPath *fs_path, V9fsFidOpenState *fs)
  466. {
  467. int dirfd;
  468. DIR *stream;
  469. dirfd = local_opendir_nofollow(ctx, fs_path->data);
  470. if (dirfd == -1) {
  471. return -1;
  472. }
  473. stream = fdopendir(dirfd);
  474. if (!stream) {
  475. close(dirfd);
  476. return -1;
  477. }
  478. fs->dir.stream = stream;
  479. return 0;
  480. }
  481. static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs)
  482. {
  483. rewinddir(fs->dir.stream);
  484. }
  485. static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs)
  486. {
  487. return telldir(fs->dir.stream);
  488. }
  489. static bool local_is_mapped_file_metadata(FsContext *fs_ctx, const char *name)
  490. {
  491. return
  492. !strcmp(name, VIRTFS_META_DIR) || !strcmp(name, VIRTFS_META_ROOT_FILE);
  493. }
  494. static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs)
  495. {
  496. struct dirent *entry;
  497. again:
  498. entry = readdir(fs->dir.stream);
  499. if (!entry) {
  500. return NULL;
  501. }
  502. if (ctx->export_flags & V9FS_SM_MAPPED) {
  503. entry->d_type = DT_UNKNOWN;
  504. } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
  505. if (local_is_mapped_file_metadata(ctx, entry->d_name)) {
  506. /* skip the meta data */
  507. goto again;
  508. }
  509. entry->d_type = DT_UNKNOWN;
  510. }
  511. return entry;
  512. }
  513. static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off)
  514. {
  515. seekdir(fs->dir.stream, off);
  516. }
  517. static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs,
  518. const struct iovec *iov,
  519. int iovcnt, off_t offset)
  520. {
  521. #ifdef CONFIG_PREADV
  522. return preadv(fs->fd, iov, iovcnt, offset);
  523. #else
  524. int err = lseek(fs->fd, offset, SEEK_SET);
  525. if (err == -1) {
  526. return err;
  527. } else {
  528. return readv(fs->fd, iov, iovcnt);
  529. }
  530. #endif
  531. }
  532. static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs,
  533. const struct iovec *iov,
  534. int iovcnt, off_t offset)
  535. {
  536. ssize_t ret;
  537. #ifdef CONFIG_PREADV
  538. ret = pwritev(fs->fd, iov, iovcnt, offset);
  539. #else
  540. int err = lseek(fs->fd, offset, SEEK_SET);
  541. if (err == -1) {
  542. return err;
  543. } else {
  544. ret = writev(fs->fd, iov, iovcnt);
  545. }
  546. #endif
  547. #ifdef CONFIG_SYNC_FILE_RANGE
  548. if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) {
  549. /*
  550. * Initiate a writeback. This is not a data integrity sync.
  551. * We want to ensure that we don't leave dirty pages in the cache
  552. * after write when writeout=immediate is sepcified.
  553. */
  554. sync_file_range(fs->fd, offset, ret,
  555. SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE);
  556. }
  557. #endif
  558. return ret;
  559. }
  560. static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
  561. {
  562. char *dirpath = g_path_get_dirname(fs_path->data);
  563. char *name = g_path_get_basename(fs_path->data);
  564. int ret = -1;
  565. int dirfd;
  566. dirfd = local_opendir_nofollow(fs_ctx, dirpath);
  567. if (dirfd == -1) {
  568. goto out;
  569. }
  570. if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
  571. ret = local_set_xattrat(dirfd, name, credp);
  572. } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
  573. ret = local_set_mapped_file_attrat(dirfd, name, credp);
  574. } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
  575. fs_ctx->export_flags & V9FS_SM_NONE) {
  576. ret = fchmodat_nofollow(dirfd, name, credp->fc_mode);
  577. }
  578. close_preserve_errno(dirfd);
  579. out:
  580. g_free(dirpath);
  581. g_free(name);
  582. return ret;
  583. }
  584. static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path,
  585. const char *name, FsCred *credp)
  586. {
  587. int err = -1;
  588. int dirfd;
  589. if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
  590. local_is_mapped_file_metadata(fs_ctx, name)) {
  591. errno = EINVAL;
  592. return -1;
  593. }
  594. dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
  595. if (dirfd == -1) {
  596. return -1;
  597. }
  598. if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
  599. fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
  600. err = mknodat(dirfd, name, fs_ctx->fmode | S_IFREG, 0);
  601. if (err == -1) {
  602. goto out;
  603. }
  604. if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
  605. err = local_set_xattrat(dirfd, name, credp);
  606. } else {
  607. err = local_set_mapped_file_attrat(dirfd, name, credp);
  608. }
  609. if (err == -1) {
  610. goto err_end;
  611. }
  612. } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
  613. fs_ctx->export_flags & V9FS_SM_NONE) {
  614. err = mknodat(dirfd, name, credp->fc_mode, credp->fc_rdev);
  615. if (err == -1) {
  616. goto out;
  617. }
  618. err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
  619. if (err == -1) {
  620. goto err_end;
  621. }
  622. }
  623. goto out;
  624. err_end:
  625. unlinkat_preserve_errno(dirfd, name, 0);
  626. out:
  627. close_preserve_errno(dirfd);
  628. return err;
  629. }
  630. static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path,
  631. const char *name, FsCred *credp)
  632. {
  633. int err = -1;
  634. int dirfd;
  635. if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
  636. local_is_mapped_file_metadata(fs_ctx, name)) {
  637. errno = EINVAL;
  638. return -1;
  639. }
  640. dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
  641. if (dirfd == -1) {
  642. return -1;
  643. }
  644. if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
  645. fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
  646. err = mkdirat(dirfd, name, fs_ctx->dmode);
  647. if (err == -1) {
  648. goto out;
  649. }
  650. credp->fc_mode = credp->fc_mode | S_IFDIR;
  651. if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
  652. err = local_set_xattrat(dirfd, name, credp);
  653. } else {
  654. err = local_set_mapped_file_attrat(dirfd, name, credp);
  655. }
  656. if (err == -1) {
  657. goto err_end;
  658. }
  659. } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
  660. fs_ctx->export_flags & V9FS_SM_NONE) {
  661. err = mkdirat(dirfd, name, credp->fc_mode);
  662. if (err == -1) {
  663. goto out;
  664. }
  665. err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
  666. if (err == -1) {
  667. goto err_end;
  668. }
  669. }
  670. goto out;
  671. err_end:
  672. unlinkat_preserve_errno(dirfd, name, AT_REMOVEDIR);
  673. out:
  674. close_preserve_errno(dirfd);
  675. return err;
  676. }
  677. static int local_fstat(FsContext *fs_ctx, int fid_type,
  678. V9fsFidOpenState *fs, struct stat *stbuf)
  679. {
  680. int err, fd;
  681. if (fid_type == P9_FID_DIR) {
  682. fd = dirfd(fs->dir.stream);
  683. } else {
  684. fd = fs->fd;
  685. }
  686. err = fstat(fd, stbuf);
  687. if (err) {
  688. return err;
  689. }
  690. if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
  691. /* Actual credentials are part of extended attrs */
  692. uid_t tmp_uid;
  693. gid_t tmp_gid;
  694. mode_t tmp_mode;
  695. dev_t tmp_dev;
  696. if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) {
  697. stbuf->st_uid = le32_to_cpu(tmp_uid);
  698. }
  699. if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) {
  700. stbuf->st_gid = le32_to_cpu(tmp_gid);
  701. }
  702. if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) {
  703. stbuf->st_mode = le32_to_cpu(tmp_mode);
  704. }
  705. if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) {
  706. stbuf->st_rdev = le64_to_cpu(tmp_dev);
  707. }
  708. } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
  709. errno = EOPNOTSUPP;
  710. return -1;
  711. }
  712. return err;
  713. }
  714. static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name,
  715. int flags, FsCred *credp, V9fsFidOpenState *fs)
  716. {
  717. int fd = -1;
  718. int err = -1;
  719. int dirfd;
  720. if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
  721. local_is_mapped_file_metadata(fs_ctx, name)) {
  722. errno = EINVAL;
  723. return -1;
  724. }
  725. /*
  726. * Mark all the open to not follow symlinks
  727. */
  728. flags |= O_NOFOLLOW;
  729. dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
  730. if (dirfd == -1) {
  731. return -1;
  732. }
  733. /* Determine the security model */
  734. if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
  735. fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
  736. fd = openat_file(dirfd, name, flags, fs_ctx->fmode);
  737. if (fd == -1) {
  738. goto out;
  739. }
  740. credp->fc_mode = credp->fc_mode|S_IFREG;
  741. if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
  742. /* Set cleint credentials in xattr */
  743. err = local_set_xattrat(dirfd, name, credp);
  744. } else {
  745. err = local_set_mapped_file_attrat(dirfd, name, credp);
  746. }
  747. if (err == -1) {
  748. goto err_end;
  749. }
  750. } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
  751. (fs_ctx->export_flags & V9FS_SM_NONE)) {
  752. fd = openat_file(dirfd, name, flags, credp->fc_mode);
  753. if (fd == -1) {
  754. goto out;
  755. }
  756. err = local_set_cred_passthrough(fs_ctx, dirfd, name, credp);
  757. if (err == -1) {
  758. goto err_end;
  759. }
  760. }
  761. err = fd;
  762. fs->fd = fd;
  763. goto out;
  764. err_end:
  765. unlinkat_preserve_errno(dirfd, name,
  766. flags & O_DIRECTORY ? AT_REMOVEDIR : 0);
  767. close_preserve_errno(fd);
  768. out:
  769. close_preserve_errno(dirfd);
  770. return err;
  771. }
  772. static int local_symlink(FsContext *fs_ctx, const char *oldpath,
  773. V9fsPath *dir_path, const char *name, FsCred *credp)
  774. {
  775. int err = -1;
  776. int dirfd;
  777. if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE &&
  778. local_is_mapped_file_metadata(fs_ctx, name)) {
  779. errno = EINVAL;
  780. return -1;
  781. }
  782. dirfd = local_opendir_nofollow(fs_ctx, dir_path->data);
  783. if (dirfd == -1) {
  784. return -1;
  785. }
  786. /* Determine the security model */
  787. if (fs_ctx->export_flags & V9FS_SM_MAPPED ||
  788. fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
  789. int fd;
  790. ssize_t oldpath_size, write_size;
  791. fd = openat_file(dirfd, name, O_CREAT | O_EXCL | O_RDWR,
  792. fs_ctx->fmode);
  793. if (fd == -1) {
  794. goto out;
  795. }
  796. /* Write the oldpath (target) to the file. */
  797. oldpath_size = strlen(oldpath);
  798. do {
  799. write_size = write(fd, (void *)oldpath, oldpath_size);
  800. } while (write_size == -1 && errno == EINTR);
  801. close_preserve_errno(fd);
  802. if (write_size != oldpath_size) {
  803. goto err_end;
  804. }
  805. /* Set cleint credentials in symlink's xattr */
  806. credp->fc_mode = credp->fc_mode | S_IFLNK;
  807. if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
  808. err = local_set_xattrat(dirfd, name, credp);
  809. } else {
  810. err = local_set_mapped_file_attrat(dirfd, name, credp);
  811. }
  812. if (err == -1) {
  813. goto err_end;
  814. }
  815. } else if (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH ||
  816. fs_ctx->export_flags & V9FS_SM_NONE) {
  817. err = symlinkat(oldpath, dirfd, name);
  818. if (err) {
  819. goto out;
  820. }
  821. err = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
  822. AT_SYMLINK_NOFOLLOW);
  823. if (err == -1) {
  824. /*
  825. * If we fail to change ownership and if we are
  826. * using security model none. Ignore the error
  827. */
  828. if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) {
  829. goto err_end;
  830. } else {
  831. err = 0;
  832. }
  833. }
  834. }
  835. goto out;
  836. err_end:
  837. unlinkat_preserve_errno(dirfd, name, 0);
  838. out:
  839. close_preserve_errno(dirfd);
  840. return err;
  841. }
  842. static int local_link(FsContext *ctx, V9fsPath *oldpath,
  843. V9fsPath *dirpath, const char *name)
  844. {
  845. char *odirpath = g_path_get_dirname(oldpath->data);
  846. char *oname = g_path_get_basename(oldpath->data);
  847. int ret = -1;
  848. int odirfd, ndirfd;
  849. if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
  850. local_is_mapped_file_metadata(ctx, name)) {
  851. errno = EINVAL;
  852. return -1;
  853. }
  854. odirfd = local_opendir_nofollow(ctx, odirpath);
  855. if (odirfd == -1) {
  856. goto out;
  857. }
  858. ndirfd = local_opendir_nofollow(ctx, dirpath->data);
  859. if (ndirfd == -1) {
  860. close_preserve_errno(odirfd);
  861. goto out;
  862. }
  863. ret = linkat(odirfd, oname, ndirfd, name, 0);
  864. if (ret < 0) {
  865. goto out_close;
  866. }
  867. /* now link the virtfs_metadata files */
  868. if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
  869. int omap_dirfd, nmap_dirfd;
  870. ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
  871. if (ret < 0 && errno != EEXIST) {
  872. goto err_undo_link;
  873. }
  874. omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
  875. if (omap_dirfd == -1) {
  876. goto err;
  877. }
  878. nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
  879. if (nmap_dirfd == -1) {
  880. close_preserve_errno(omap_dirfd);
  881. goto err;
  882. }
  883. ret = linkat(omap_dirfd, oname, nmap_dirfd, name, 0);
  884. close_preserve_errno(nmap_dirfd);
  885. close_preserve_errno(omap_dirfd);
  886. if (ret < 0 && errno != ENOENT) {
  887. goto err_undo_link;
  888. }
  889. ret = 0;
  890. }
  891. goto out_close;
  892. err:
  893. ret = -1;
  894. err_undo_link:
  895. unlinkat_preserve_errno(ndirfd, name, 0);
  896. out_close:
  897. close_preserve_errno(ndirfd);
  898. close_preserve_errno(odirfd);
  899. out:
  900. g_free(oname);
  901. g_free(odirpath);
  902. return ret;
  903. }
  904. static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size)
  905. {
  906. int fd, ret;
  907. fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0);
  908. if (fd == -1) {
  909. return -1;
  910. }
  911. ret = ftruncate(fd, size);
  912. close_preserve_errno(fd);
  913. return ret;
  914. }
  915. static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp)
  916. {
  917. char *dirpath = g_path_get_dirname(fs_path->data);
  918. char *name = g_path_get_basename(fs_path->data);
  919. int ret = -1;
  920. int dirfd;
  921. dirfd = local_opendir_nofollow(fs_ctx, dirpath);
  922. if (dirfd == -1) {
  923. goto out;
  924. }
  925. if ((credp->fc_uid == -1 && credp->fc_gid == -1) ||
  926. (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) ||
  927. (fs_ctx->export_flags & V9FS_SM_NONE)) {
  928. ret = fchownat(dirfd, name, credp->fc_uid, credp->fc_gid,
  929. AT_SYMLINK_NOFOLLOW);
  930. } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) {
  931. ret = local_set_xattrat(dirfd, name, credp);
  932. } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) {
  933. ret = local_set_mapped_file_attrat(dirfd, name, credp);
  934. }
  935. close_preserve_errno(dirfd);
  936. out:
  937. g_free(name);
  938. g_free(dirpath);
  939. return ret;
  940. }
  941. static int local_utimensat(FsContext *s, V9fsPath *fs_path,
  942. const struct timespec *buf)
  943. {
  944. char *dirpath = g_path_get_dirname(fs_path->data);
  945. char *name = g_path_get_basename(fs_path->data);
  946. int dirfd, ret = -1;
  947. dirfd = local_opendir_nofollow(s, dirpath);
  948. if (dirfd == -1) {
  949. goto out;
  950. }
  951. ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW);
  952. close_preserve_errno(dirfd);
  953. out:
  954. g_free(dirpath);
  955. g_free(name);
  956. return ret;
  957. }
  958. static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name,
  959. int flags)
  960. {
  961. int ret = -1;
  962. if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
  963. int map_dirfd;
  964. /* We need to remove the metadata as well:
  965. * - the metadata directory if we're removing a directory
  966. * - the metadata file in the parent's metadata directory
  967. *
  968. * If any of these are missing (ie, ENOENT) then we're probably
  969. * trying to remove something that wasn't created in mapped-file
  970. * mode. We just ignore the error.
  971. */
  972. if (flags == AT_REMOVEDIR) {
  973. int fd;
  974. fd = openat_dir(dirfd, name);
  975. if (fd == -1) {
  976. goto err_out;
  977. }
  978. ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR);
  979. close_preserve_errno(fd);
  980. if (ret < 0 && errno != ENOENT) {
  981. goto err_out;
  982. }
  983. }
  984. map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR);
  985. if (map_dirfd != -1) {
  986. ret = unlinkat(map_dirfd, name, 0);
  987. close_preserve_errno(map_dirfd);
  988. if (ret < 0 && errno != ENOENT) {
  989. goto err_out;
  990. }
  991. } else if (errno != ENOENT) {
  992. goto err_out;
  993. }
  994. }
  995. ret = unlinkat(dirfd, name, flags);
  996. err_out:
  997. return ret;
  998. }
  999. static int local_remove(FsContext *ctx, const char *path)
  1000. {
  1001. struct stat stbuf;
  1002. char *dirpath = g_path_get_dirname(path);
  1003. char *name = g_path_get_basename(path);
  1004. int flags = 0;
  1005. int dirfd;
  1006. int err = -1;
  1007. dirfd = local_opendir_nofollow(ctx, dirpath);
  1008. if (dirfd == -1) {
  1009. goto out;
  1010. }
  1011. if (fstatat(dirfd, name, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) {
  1012. goto err_out;
  1013. }
  1014. if (S_ISDIR(stbuf.st_mode)) {
  1015. flags |= AT_REMOVEDIR;
  1016. }
  1017. err = local_unlinkat_common(ctx, dirfd, name, flags);
  1018. err_out:
  1019. close_preserve_errno(dirfd);
  1020. out:
  1021. g_free(name);
  1022. g_free(dirpath);
  1023. return err;
  1024. }
  1025. static int local_fsync(FsContext *ctx, int fid_type,
  1026. V9fsFidOpenState *fs, int datasync)
  1027. {
  1028. int fd;
  1029. if (fid_type == P9_FID_DIR) {
  1030. fd = dirfd(fs->dir.stream);
  1031. } else {
  1032. fd = fs->fd;
  1033. }
  1034. if (datasync) {
  1035. return qemu_fdatasync(fd);
  1036. } else {
  1037. return fsync(fd);
  1038. }
  1039. }
  1040. static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf)
  1041. {
  1042. int fd, ret;
  1043. fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0);
  1044. if (fd == -1) {
  1045. return -1;
  1046. }
  1047. ret = fstatfs(fd, stbuf);
  1048. close_preserve_errno(fd);
  1049. return ret;
  1050. }
  1051. static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path,
  1052. const char *name, void *value, size_t size)
  1053. {
  1054. char *path = fs_path->data;
  1055. return v9fs_get_xattr(ctx, path, name, value, size);
  1056. }
  1057. static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path,
  1058. void *value, size_t size)
  1059. {
  1060. char *path = fs_path->data;
  1061. return v9fs_list_xattr(ctx, path, value, size);
  1062. }
  1063. static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name,
  1064. void *value, size_t size, int flags)
  1065. {
  1066. char *path = fs_path->data;
  1067. return v9fs_set_xattr(ctx, path, name, value, size, flags);
  1068. }
  1069. static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path,
  1070. const char *name)
  1071. {
  1072. char *path = fs_path->data;
  1073. return v9fs_remove_xattr(ctx, path, name);
  1074. }
  1075. static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path,
  1076. const char *name, V9fsPath *target)
  1077. {
  1078. if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
  1079. local_is_mapped_file_metadata(ctx, name)) {
  1080. errno = EINVAL;
  1081. return -1;
  1082. }
  1083. if (dir_path) {
  1084. if (!strcmp(name, ".")) {
  1085. /* "." relative to "foo/bar" is "foo/bar" */
  1086. v9fs_path_copy(target, dir_path);
  1087. } else if (!strcmp(name, "..")) {
  1088. if (!strcmp(dir_path->data, ".")) {
  1089. /* ".." relative to the root is "." */
  1090. v9fs_path_sprintf(target, ".");
  1091. } else {
  1092. char *tmp = g_path_get_dirname(dir_path->data);
  1093. /* Symbolic links are resolved by the client. We can assume
  1094. * that ".." relative to "foo/bar" is equivalent to "foo"
  1095. */
  1096. v9fs_path_sprintf(target, "%s", tmp);
  1097. g_free(tmp);
  1098. }
  1099. } else {
  1100. assert(!strchr(name, '/'));
  1101. v9fs_path_sprintf(target, "%s/%s", dir_path->data, name);
  1102. }
  1103. } else if (!strcmp(name, "/") || !strcmp(name, ".") ||
  1104. !strcmp(name, "..")) {
  1105. /* This is the root fid */
  1106. v9fs_path_sprintf(target, ".");
  1107. } else {
  1108. assert(!strchr(name, '/'));
  1109. v9fs_path_sprintf(target, "./%s", name);
  1110. }
  1111. return 0;
  1112. }
  1113. static int local_renameat(FsContext *ctx, V9fsPath *olddir,
  1114. const char *old_name, V9fsPath *newdir,
  1115. const char *new_name)
  1116. {
  1117. int ret;
  1118. int odirfd, ndirfd;
  1119. if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
  1120. (local_is_mapped_file_metadata(ctx, old_name) ||
  1121. local_is_mapped_file_metadata(ctx, new_name))) {
  1122. errno = EINVAL;
  1123. return -1;
  1124. }
  1125. odirfd = local_opendir_nofollow(ctx, olddir->data);
  1126. if (odirfd == -1) {
  1127. return -1;
  1128. }
  1129. ndirfd = local_opendir_nofollow(ctx, newdir->data);
  1130. if (ndirfd == -1) {
  1131. close_preserve_errno(odirfd);
  1132. return -1;
  1133. }
  1134. ret = renameat(odirfd, old_name, ndirfd, new_name);
  1135. if (ret < 0) {
  1136. goto out;
  1137. }
  1138. if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
  1139. int omap_dirfd, nmap_dirfd;
  1140. ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700);
  1141. if (ret < 0 && errno != EEXIST) {
  1142. goto err_undo_rename;
  1143. }
  1144. omap_dirfd = openat_dir(odirfd, VIRTFS_META_DIR);
  1145. if (omap_dirfd == -1) {
  1146. goto err;
  1147. }
  1148. nmap_dirfd = openat_dir(ndirfd, VIRTFS_META_DIR);
  1149. if (nmap_dirfd == -1) {
  1150. close_preserve_errno(omap_dirfd);
  1151. goto err;
  1152. }
  1153. /* rename the .virtfs_metadata files */
  1154. ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name);
  1155. close_preserve_errno(nmap_dirfd);
  1156. close_preserve_errno(omap_dirfd);
  1157. if (ret < 0 && errno != ENOENT) {
  1158. goto err_undo_rename;
  1159. }
  1160. ret = 0;
  1161. }
  1162. goto out;
  1163. err:
  1164. ret = -1;
  1165. err_undo_rename:
  1166. renameat_preserve_errno(ndirfd, new_name, odirfd, old_name);
  1167. out:
  1168. close_preserve_errno(ndirfd);
  1169. close_preserve_errno(odirfd);
  1170. return ret;
  1171. }
  1172. static void v9fs_path_init_dirname(V9fsPath *path, const char *str)
  1173. {
  1174. path->data = g_path_get_dirname(str);
  1175. path->size = strlen(path->data) + 1;
  1176. }
  1177. static int local_rename(FsContext *ctx, const char *oldpath,
  1178. const char *newpath)
  1179. {
  1180. int err;
  1181. char *oname = g_path_get_basename(oldpath);
  1182. char *nname = g_path_get_basename(newpath);
  1183. V9fsPath olddir, newdir;
  1184. v9fs_path_init_dirname(&olddir, oldpath);
  1185. v9fs_path_init_dirname(&newdir, newpath);
  1186. err = local_renameat(ctx, &olddir, oname, &newdir, nname);
  1187. v9fs_path_free(&newdir);
  1188. v9fs_path_free(&olddir);
  1189. g_free(nname);
  1190. g_free(oname);
  1191. return err;
  1192. }
  1193. static int local_unlinkat(FsContext *ctx, V9fsPath *dir,
  1194. const char *name, int flags)
  1195. {
  1196. int ret;
  1197. int dirfd;
  1198. if (ctx->export_flags & V9FS_SM_MAPPED_FILE &&
  1199. local_is_mapped_file_metadata(ctx, name)) {
  1200. errno = EINVAL;
  1201. return -1;
  1202. }
  1203. dirfd = local_opendir_nofollow(ctx, dir->data);
  1204. if (dirfd == -1) {
  1205. return -1;
  1206. }
  1207. ret = local_unlinkat_common(ctx, dirfd, name, flags);
  1208. close_preserve_errno(dirfd);
  1209. return ret;
  1210. }
  1211. #ifdef FS_IOC_GETVERSION
  1212. static int local_ioc_getversion(FsContext *ctx, V9fsPath *path,
  1213. mode_t st_mode, uint64_t *st_gen)
  1214. {
  1215. int err;
  1216. V9fsFidOpenState fid_open;
  1217. /*
  1218. * Do not try to open special files like device nodes, fifos etc
  1219. * We can get fd for regular files and directories only
  1220. */
  1221. if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) {
  1222. errno = ENOTTY;
  1223. return -1;
  1224. }
  1225. err = local_open(ctx, path, O_RDONLY, &fid_open);
  1226. if (err < 0) {
  1227. return err;
  1228. }
  1229. err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen);
  1230. local_close(ctx, &fid_open);
  1231. return err;
  1232. }
  1233. #endif
  1234. static int local_ioc_getversion_init(FsContext *ctx, LocalData *data, Error **errp)
  1235. {
  1236. #ifdef FS_IOC_GETVERSION
  1237. struct statfs stbuf;
  1238. /*
  1239. * use ioc_getversion only if the ioctl is definied
  1240. */
  1241. if (fstatfs(data->mountfd, &stbuf) < 0) {
  1242. error_setg_errno(errp, errno,
  1243. "failed to stat file system at '%s'", ctx->fs_root);
  1244. return -1;
  1245. }
  1246. switch (stbuf.f_type) {
  1247. case EXT2_SUPER_MAGIC:
  1248. case BTRFS_SUPER_MAGIC:
  1249. case REISERFS_SUPER_MAGIC:
  1250. case XFS_SUPER_MAGIC:
  1251. ctx->exops.get_st_gen = local_ioc_getversion;
  1252. break;
  1253. }
  1254. #endif
  1255. return 0;
  1256. }
  1257. static int local_init(FsContext *ctx, Error **errp)
  1258. {
  1259. LocalData *data = g_malloc(sizeof(*data));
  1260. data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY);
  1261. if (data->mountfd == -1) {
  1262. error_setg_errno(errp, errno, "failed to open '%s'", ctx->fs_root);
  1263. goto err;
  1264. }
  1265. if (local_ioc_getversion_init(ctx, data, errp) < 0) {
  1266. close(data->mountfd);
  1267. goto err;
  1268. }
  1269. if (ctx->export_flags & V9FS_SM_PASSTHROUGH) {
  1270. ctx->xops = passthrough_xattr_ops;
  1271. } else if (ctx->export_flags & V9FS_SM_MAPPED) {
  1272. ctx->xops = mapped_xattr_ops;
  1273. } else if (ctx->export_flags & V9FS_SM_NONE) {
  1274. ctx->xops = none_xattr_ops;
  1275. } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) {
  1276. /*
  1277. * xattr operation for mapped-file and passthrough
  1278. * remain same.
  1279. */
  1280. ctx->xops = passthrough_xattr_ops;
  1281. }
  1282. ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT;
  1283. ctx->private = data;
  1284. return 0;
  1285. err:
  1286. g_free(data);
  1287. return -1;
  1288. }
  1289. static void local_cleanup(FsContext *ctx)
  1290. {
  1291. LocalData *data = ctx->private;
  1292. if (!data) {
  1293. return;
  1294. }
  1295. close(data->mountfd);
  1296. g_free(data);
  1297. }
  1298. static void error_append_security_model_hint(Error **errp)
  1299. {
  1300. error_append_hint(errp, "Valid options are: security_model="
  1301. "[passthrough|mapped-xattr|mapped-file|none]\n");
  1302. }
  1303. static int local_parse_opts(QemuOpts *opts, FsDriverEntry *fse, Error **errp)
  1304. {
  1305. const char *sec_model = qemu_opt_get(opts, "security_model");
  1306. const char *path = qemu_opt_get(opts, "path");
  1307. const char *multidevs = qemu_opt_get(opts, "multidevs");
  1308. Error *local_err = NULL;
  1309. if (!sec_model) {
  1310. error_setg(errp, "security_model property not set");
  1311. error_append_security_model_hint(errp);
  1312. return -1;
  1313. }
  1314. if (!strcmp(sec_model, "passthrough")) {
  1315. fse->export_flags |= V9FS_SM_PASSTHROUGH;
  1316. } else if (!strcmp(sec_model, "mapped") ||
  1317. !strcmp(sec_model, "mapped-xattr")) {
  1318. fse->export_flags |= V9FS_SM_MAPPED;
  1319. } else if (!strcmp(sec_model, "none")) {
  1320. fse->export_flags |= V9FS_SM_NONE;
  1321. } else if (!strcmp(sec_model, "mapped-file")) {
  1322. fse->export_flags |= V9FS_SM_MAPPED_FILE;
  1323. } else {
  1324. error_setg(errp, "invalid security_model property '%s'", sec_model);
  1325. error_append_security_model_hint(errp);
  1326. return -1;
  1327. }
  1328. if (multidevs) {
  1329. if (!strcmp(multidevs, "remap")) {
  1330. fse->export_flags &= ~V9FS_FORBID_MULTIDEVS;
  1331. fse->export_flags |= V9FS_REMAP_INODES;
  1332. } else if (!strcmp(multidevs, "forbid")) {
  1333. fse->export_flags &= ~V9FS_REMAP_INODES;
  1334. fse->export_flags |= V9FS_FORBID_MULTIDEVS;
  1335. } else if (!strcmp(multidevs, "warn")) {
  1336. fse->export_flags &= ~V9FS_FORBID_MULTIDEVS;
  1337. fse->export_flags &= ~V9FS_REMAP_INODES;
  1338. } else {
  1339. error_setg(&local_err, "invalid multidevs property '%s'",
  1340. multidevs);
  1341. error_append_hint(&local_err, "Valid options are: multidevs="
  1342. "[remap|forbid|warn]\n");
  1343. error_propagate(errp, local_err);
  1344. return -1;
  1345. }
  1346. }
  1347. if (!path) {
  1348. error_setg(errp, "path property not set");
  1349. return -1;
  1350. }
  1351. if (fsdev_throttle_parse_opts(opts, &fse->fst, &local_err)) {
  1352. error_propagate_prepend(errp, local_err,
  1353. "invalid throttle configuration: ");
  1354. return -1;
  1355. }
  1356. if (fse->export_flags & V9FS_SM_MAPPED ||
  1357. fse->export_flags & V9FS_SM_MAPPED_FILE) {
  1358. fse->fmode =
  1359. qemu_opt_get_number(opts, "fmode", SM_LOCAL_MODE_BITS) & 0777;
  1360. fse->dmode =
  1361. qemu_opt_get_number(opts, "dmode", SM_LOCAL_DIR_MODE_BITS) & 0777;
  1362. } else {
  1363. if (qemu_opt_find(opts, "fmode")) {
  1364. error_setg(errp, "fmode is only valid for mapped security modes");
  1365. return -1;
  1366. }
  1367. if (qemu_opt_find(opts, "dmode")) {
  1368. error_setg(errp, "dmode is only valid for mapped security modes");
  1369. return -1;
  1370. }
  1371. }
  1372. fse->path = g_strdup(path);
  1373. return 0;
  1374. }
  1375. FileOperations local_ops = {
  1376. .parse_opts = local_parse_opts,
  1377. .init = local_init,
  1378. .cleanup = local_cleanup,
  1379. .lstat = local_lstat,
  1380. .readlink = local_readlink,
  1381. .close = local_close,
  1382. .closedir = local_closedir,
  1383. .open = local_open,
  1384. .opendir = local_opendir,
  1385. .rewinddir = local_rewinddir,
  1386. .telldir = local_telldir,
  1387. .readdir = local_readdir,
  1388. .seekdir = local_seekdir,
  1389. .preadv = local_preadv,
  1390. .pwritev = local_pwritev,
  1391. .chmod = local_chmod,
  1392. .mknod = local_mknod,
  1393. .mkdir = local_mkdir,
  1394. .fstat = local_fstat,
  1395. .open2 = local_open2,
  1396. .symlink = local_symlink,
  1397. .link = local_link,
  1398. .truncate = local_truncate,
  1399. .rename = local_rename,
  1400. .chown = local_chown,
  1401. .utimensat = local_utimensat,
  1402. .remove = local_remove,
  1403. .fsync = local_fsync,
  1404. .statfs = local_statfs,
  1405. .lgetxattr = local_lgetxattr,
  1406. .llistxattr = local_llistxattr,
  1407. .lsetxattr = local_lsetxattr,
  1408. .lremovexattr = local_lremovexattr,
  1409. .name_to_path = local_name_to_path,
  1410. .renameat = local_renameat,
  1411. .unlinkat = local_unlinkat,
  1412. };