9p-local.c 43 KB

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