hostfs_kern.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
  3. * Licensed under the GPL
  4. *
  5. * Ported the filesystem routines to 2.5.
  6. * 2003-02-10 Petr Baudis <pasky@ucw.cz>
  7. */
  8. #include <linux/fs.h>
  9. #include <linux/magic.h>
  10. #include <linux/module.h>
  11. #include <linux/mm.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/statfs.h>
  14. #include <linux/slab.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/writeback.h>
  17. #include <linux/mount.h>
  18. #include <linux/fs_context.h>
  19. #include <linux/fs_parser.h>
  20. #include <linux/namei.h>
  21. #include "hostfs.h"
  22. #include <init.h>
  23. #include <kern.h>
  24. struct hostfs_fs_info {
  25. char *host_root_path;
  26. };
  27. struct hostfs_inode_info {
  28. int fd;
  29. fmode_t mode;
  30. struct inode vfs_inode;
  31. struct mutex open_mutex;
  32. dev_t dev;
  33. struct hostfs_timespec btime;
  34. };
  35. static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
  36. {
  37. return list_entry(inode, struct hostfs_inode_info, vfs_inode);
  38. }
  39. #define FILE_HOSTFS_I(file) HOSTFS_I(file_inode(file))
  40. static struct kmem_cache *hostfs_inode_cache;
  41. /* Changed in hostfs_args before the kernel starts running */
  42. static char *root_ino = "";
  43. static int append = 0;
  44. static const struct inode_operations hostfs_iops;
  45. static const struct inode_operations hostfs_dir_iops;
  46. static const struct inode_operations hostfs_link_iops;
  47. #ifndef MODULE
  48. static int __init hostfs_args(char *options, int *add)
  49. {
  50. char *ptr;
  51. *add = 0;
  52. ptr = strchr(options, ',');
  53. if (ptr != NULL)
  54. *ptr++ = '\0';
  55. if (*options != '\0')
  56. root_ino = options;
  57. options = ptr;
  58. while (options) {
  59. ptr = strchr(options, ',');
  60. if (ptr != NULL)
  61. *ptr++ = '\0';
  62. if (*options != '\0') {
  63. if (!strcmp(options, "append"))
  64. append = 1;
  65. else printf("hostfs_args - unsupported option - %s\n",
  66. options);
  67. }
  68. options = ptr;
  69. }
  70. return 0;
  71. }
  72. __uml_setup("hostfs=", hostfs_args,
  73. "hostfs=<root dir>,<flags>,...\n"
  74. " This is used to set hostfs parameters. The root directory argument\n"
  75. " is used to confine all hostfs mounts to within the specified directory\n"
  76. " tree on the host. If this isn't specified, then a user inside UML can\n"
  77. " mount anything on the host that's accessible to the user that's running\n"
  78. " it.\n"
  79. " The only flag currently supported is 'append', which specifies that all\n"
  80. " files opened by hostfs will be opened in append mode.\n\n"
  81. );
  82. #endif
  83. static char *__dentry_name(struct dentry *dentry, char *name)
  84. {
  85. char *p = dentry_path_raw(dentry, name, PATH_MAX);
  86. struct hostfs_fs_info *fsi = dentry->d_sb->s_fs_info;
  87. char *root = fsi->host_root_path;
  88. size_t len = strlen(root);
  89. if (IS_ERR(p) || len > p - name) {
  90. __putname(name);
  91. return NULL;
  92. }
  93. memcpy(name, root, len);
  94. memmove(name + len, p, name + PATH_MAX - p);
  95. return name;
  96. }
  97. static char *dentry_name(struct dentry *dentry)
  98. {
  99. char *name = __getname();
  100. if (!name)
  101. return NULL;
  102. return __dentry_name(dentry, name);
  103. }
  104. static char *inode_name(struct inode *ino)
  105. {
  106. struct dentry *dentry;
  107. char *name;
  108. dentry = d_find_alias(ino);
  109. if (!dentry)
  110. return NULL;
  111. name = dentry_name(dentry);
  112. dput(dentry);
  113. return name;
  114. }
  115. static char *follow_link(char *link)
  116. {
  117. char *name, *resolved, *end;
  118. int n;
  119. name = kmalloc(PATH_MAX, GFP_KERNEL);
  120. if (!name) {
  121. n = -ENOMEM;
  122. goto out_free;
  123. }
  124. n = hostfs_do_readlink(link, name, PATH_MAX);
  125. if (n < 0)
  126. goto out_free;
  127. else if (n == PATH_MAX) {
  128. n = -E2BIG;
  129. goto out_free;
  130. }
  131. if (*name == '/')
  132. return name;
  133. end = strrchr(link, '/');
  134. if (end == NULL)
  135. return name;
  136. *(end + 1) = '\0';
  137. resolved = kasprintf(GFP_KERNEL, "%s%s", link, name);
  138. if (resolved == NULL) {
  139. n = -ENOMEM;
  140. goto out_free;
  141. }
  142. kfree(name);
  143. return resolved;
  144. out_free:
  145. kfree(name);
  146. return ERR_PTR(n);
  147. }
  148. static int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
  149. {
  150. /*
  151. * do_statfs uses struct statfs64 internally, but the linux kernel
  152. * struct statfs still has 32-bit versions for most of these fields,
  153. * so we convert them here
  154. */
  155. int err;
  156. long long f_blocks;
  157. long long f_bfree;
  158. long long f_bavail;
  159. long long f_files;
  160. long long f_ffree;
  161. struct hostfs_fs_info *fsi;
  162. fsi = dentry->d_sb->s_fs_info;
  163. err = do_statfs(fsi->host_root_path,
  164. &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
  165. &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
  166. &sf->f_namelen);
  167. if (err)
  168. return err;
  169. sf->f_blocks = f_blocks;
  170. sf->f_bfree = f_bfree;
  171. sf->f_bavail = f_bavail;
  172. sf->f_files = f_files;
  173. sf->f_ffree = f_ffree;
  174. sf->f_type = HOSTFS_SUPER_MAGIC;
  175. return 0;
  176. }
  177. static struct inode *hostfs_alloc_inode(struct super_block *sb)
  178. {
  179. struct hostfs_inode_info *hi;
  180. hi = alloc_inode_sb(sb, hostfs_inode_cache, GFP_KERNEL_ACCOUNT);
  181. if (hi == NULL)
  182. return NULL;
  183. hi->fd = -1;
  184. hi->mode = 0;
  185. hi->dev = 0;
  186. inode_init_once(&hi->vfs_inode);
  187. mutex_init(&hi->open_mutex);
  188. return &hi->vfs_inode;
  189. }
  190. static void hostfs_evict_inode(struct inode *inode)
  191. {
  192. truncate_inode_pages_final(&inode->i_data);
  193. clear_inode(inode);
  194. if (HOSTFS_I(inode)->fd != -1) {
  195. close_file(&HOSTFS_I(inode)->fd);
  196. HOSTFS_I(inode)->fd = -1;
  197. HOSTFS_I(inode)->dev = 0;
  198. }
  199. }
  200. static void hostfs_free_inode(struct inode *inode)
  201. {
  202. kmem_cache_free(hostfs_inode_cache, HOSTFS_I(inode));
  203. }
  204. static int hostfs_show_options(struct seq_file *seq, struct dentry *root)
  205. {
  206. struct hostfs_fs_info *fsi;
  207. const char *root_path;
  208. fsi = root->d_sb->s_fs_info;
  209. root_path = fsi->host_root_path;
  210. size_t offset = strlen(root_ino) + 1;
  211. if (strlen(root_path) > offset)
  212. seq_show_option(seq, root_path + offset, NULL);
  213. if (append)
  214. seq_puts(seq, ",append");
  215. return 0;
  216. }
  217. static const struct super_operations hostfs_sbops = {
  218. .alloc_inode = hostfs_alloc_inode,
  219. .free_inode = hostfs_free_inode,
  220. .drop_inode = inode_just_drop,
  221. .evict_inode = hostfs_evict_inode,
  222. .statfs = hostfs_statfs,
  223. .show_options = hostfs_show_options,
  224. };
  225. static int hostfs_readdir(struct file *file, struct dir_context *ctx)
  226. {
  227. void *dir;
  228. char *name;
  229. unsigned long long next, ino;
  230. int error, len;
  231. unsigned int type;
  232. name = dentry_name(file->f_path.dentry);
  233. if (name == NULL)
  234. return -ENOMEM;
  235. dir = open_dir(name, &error);
  236. __putname(name);
  237. if (dir == NULL)
  238. return -error;
  239. next = ctx->pos;
  240. seek_dir(dir, next);
  241. while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) {
  242. if (!dir_emit(ctx, name, len, ino, type))
  243. break;
  244. ctx->pos = next;
  245. }
  246. close_dir(dir);
  247. return 0;
  248. }
  249. static int hostfs_open(struct inode *ino, struct file *file)
  250. {
  251. char *name;
  252. fmode_t mode;
  253. int err;
  254. int r, w, fd;
  255. mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
  256. if ((mode & HOSTFS_I(ino)->mode) == mode)
  257. return 0;
  258. mode |= HOSTFS_I(ino)->mode;
  259. retry:
  260. r = w = 0;
  261. if (mode & FMODE_READ)
  262. r = 1;
  263. if (mode & FMODE_WRITE)
  264. r = w = 1;
  265. name = dentry_name(file_dentry(file));
  266. if (name == NULL)
  267. return -ENOMEM;
  268. fd = open_file(name, r, w, append);
  269. __putname(name);
  270. if (fd < 0)
  271. return fd;
  272. mutex_lock(&HOSTFS_I(ino)->open_mutex);
  273. /* somebody else had handled it first? */
  274. if ((mode & HOSTFS_I(ino)->mode) == mode) {
  275. mutex_unlock(&HOSTFS_I(ino)->open_mutex);
  276. close_file(&fd);
  277. return 0;
  278. }
  279. if ((mode | HOSTFS_I(ino)->mode) != mode) {
  280. mode |= HOSTFS_I(ino)->mode;
  281. mutex_unlock(&HOSTFS_I(ino)->open_mutex);
  282. close_file(&fd);
  283. goto retry;
  284. }
  285. if (HOSTFS_I(ino)->fd == -1) {
  286. HOSTFS_I(ino)->fd = fd;
  287. } else {
  288. err = replace_file(fd, HOSTFS_I(ino)->fd);
  289. close_file(&fd);
  290. if (err < 0) {
  291. mutex_unlock(&HOSTFS_I(ino)->open_mutex);
  292. return err;
  293. }
  294. }
  295. HOSTFS_I(ino)->mode = mode;
  296. mutex_unlock(&HOSTFS_I(ino)->open_mutex);
  297. return 0;
  298. }
  299. static int hostfs_file_release(struct inode *inode, struct file *file)
  300. {
  301. filemap_write_and_wait(inode->i_mapping);
  302. return 0;
  303. }
  304. static int hostfs_fsync(struct file *file, loff_t start, loff_t end,
  305. int datasync)
  306. {
  307. struct inode *inode = file->f_mapping->host;
  308. int ret;
  309. ret = file_write_and_wait_range(file, start, end);
  310. if (ret)
  311. return ret;
  312. inode_lock(inode);
  313. ret = fsync_file(HOSTFS_I(inode)->fd, datasync);
  314. inode_unlock(inode);
  315. return ret;
  316. }
  317. static const struct file_operations hostfs_file_fops = {
  318. .llseek = generic_file_llseek,
  319. .splice_read = filemap_splice_read,
  320. .splice_write = iter_file_splice_write,
  321. .read_iter = generic_file_read_iter,
  322. .write_iter = generic_file_write_iter,
  323. .mmap_prepare = generic_file_mmap_prepare,
  324. .open = hostfs_open,
  325. .release = hostfs_file_release,
  326. .fsync = hostfs_fsync,
  327. };
  328. static const struct file_operations hostfs_dir_fops = {
  329. .llseek = generic_file_llseek,
  330. .iterate_shared = hostfs_readdir,
  331. .read = generic_read_dir,
  332. .open = hostfs_open,
  333. .fsync = hostfs_fsync,
  334. };
  335. static int hostfs_writepages(struct address_space *mapping,
  336. struct writeback_control *wbc)
  337. {
  338. struct inode *inode = mapping->host;
  339. struct folio *folio = NULL;
  340. loff_t i_size = i_size_read(inode);
  341. int err = 0;
  342. while ((folio = writeback_iter(mapping, wbc, folio, &err))) {
  343. loff_t pos = folio_pos(folio);
  344. size_t count = folio_size(folio);
  345. char *buffer;
  346. int ret;
  347. if (count > i_size - pos)
  348. count = i_size - pos;
  349. buffer = kmap_local_folio(folio, 0);
  350. ret = write_file(HOSTFS_I(inode)->fd, &pos, buffer, count);
  351. kunmap_local(buffer);
  352. folio_unlock(folio);
  353. if (ret != count) {
  354. err = ret < 0 ? ret : -EIO;
  355. mapping_set_error(mapping, err);
  356. }
  357. }
  358. return err;
  359. }
  360. static int hostfs_read_folio(struct file *file, struct folio *folio)
  361. {
  362. char *buffer;
  363. loff_t start = folio_pos(folio);
  364. int bytes_read, ret = 0;
  365. buffer = kmap_local_folio(folio, 0);
  366. bytes_read = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
  367. PAGE_SIZE);
  368. if (bytes_read < 0)
  369. ret = bytes_read;
  370. else
  371. buffer = folio_zero_tail(folio, bytes_read, buffer + bytes_read);
  372. kunmap_local(buffer);
  373. folio_end_read(folio, ret == 0);
  374. return ret;
  375. }
  376. static int hostfs_write_begin(const struct kiocb *iocb,
  377. struct address_space *mapping,
  378. loff_t pos, unsigned len,
  379. struct folio **foliop, void **fsdata)
  380. {
  381. pgoff_t index = pos >> PAGE_SHIFT;
  382. *foliop = __filemap_get_folio(mapping, index, FGP_WRITEBEGIN,
  383. mapping_gfp_mask(mapping));
  384. if (IS_ERR(*foliop))
  385. return PTR_ERR(*foliop);
  386. return 0;
  387. }
  388. static int hostfs_write_end(const struct kiocb *iocb,
  389. struct address_space *mapping,
  390. loff_t pos, unsigned len, unsigned copied,
  391. struct folio *folio, void *fsdata)
  392. {
  393. struct inode *inode = mapping->host;
  394. void *buffer;
  395. size_t from = offset_in_folio(folio, pos);
  396. int err;
  397. buffer = kmap_local_folio(folio, from);
  398. err = write_file(FILE_HOSTFS_I(iocb->ki_filp)->fd, &pos, buffer, copied);
  399. kunmap_local(buffer);
  400. if (!folio_test_uptodate(folio) && err == folio_size(folio))
  401. folio_mark_uptodate(folio);
  402. /*
  403. * If err > 0, write_file has added err to pos, so we are comparing
  404. * i_size against the last byte written.
  405. */
  406. if (err > 0 && (pos > inode->i_size))
  407. inode->i_size = pos;
  408. folio_unlock(folio);
  409. folio_put(folio);
  410. return err;
  411. }
  412. static const struct address_space_operations hostfs_aops = {
  413. .writepages = hostfs_writepages,
  414. .read_folio = hostfs_read_folio,
  415. .dirty_folio = filemap_dirty_folio,
  416. .write_begin = hostfs_write_begin,
  417. .write_end = hostfs_write_end,
  418. .migrate_folio = filemap_migrate_folio,
  419. };
  420. static int hostfs_inode_update(struct inode *ino, const struct hostfs_stat *st)
  421. {
  422. set_nlink(ino, st->nlink);
  423. i_uid_write(ino, st->uid);
  424. i_gid_write(ino, st->gid);
  425. inode_set_atime_to_ts(ino, (struct timespec64){
  426. st->atime.tv_sec,
  427. st->atime.tv_nsec,
  428. });
  429. inode_set_mtime_to_ts(ino, (struct timespec64){
  430. st->mtime.tv_sec,
  431. st->mtime.tv_nsec,
  432. });
  433. inode_set_ctime(ino, st->ctime.tv_sec, st->ctime.tv_nsec);
  434. ino->i_size = st->size;
  435. ino->i_blocks = st->blocks;
  436. return 0;
  437. }
  438. static int hostfs_inode_set(struct inode *ino, void *data)
  439. {
  440. struct hostfs_stat *st = data;
  441. dev_t dev, rdev;
  442. /* Reencode maj and min with the kernel encoding.*/
  443. rdev = MKDEV(st->rdev.maj, st->rdev.min);
  444. dev = MKDEV(st->dev.maj, st->dev.min);
  445. switch (st->mode & S_IFMT) {
  446. case S_IFLNK:
  447. ino->i_op = &hostfs_link_iops;
  448. break;
  449. case S_IFDIR:
  450. ino->i_op = &hostfs_dir_iops;
  451. ino->i_fop = &hostfs_dir_fops;
  452. break;
  453. case S_IFCHR:
  454. case S_IFBLK:
  455. case S_IFIFO:
  456. case S_IFSOCK:
  457. init_special_inode(ino, st->mode & S_IFMT, rdev);
  458. ino->i_op = &hostfs_iops;
  459. break;
  460. case S_IFREG:
  461. ino->i_op = &hostfs_iops;
  462. ino->i_fop = &hostfs_file_fops;
  463. ino->i_mapping->a_ops = &hostfs_aops;
  464. break;
  465. default:
  466. return -EIO;
  467. }
  468. HOSTFS_I(ino)->dev = dev;
  469. HOSTFS_I(ino)->btime = st->btime;
  470. ino->i_ino = st->ino;
  471. ino->i_mode = st->mode;
  472. return hostfs_inode_update(ino, st);
  473. }
  474. static int hostfs_inode_test(struct inode *inode, void *data)
  475. {
  476. const struct hostfs_stat *st = data;
  477. dev_t dev = MKDEV(st->dev.maj, st->dev.min);
  478. return inode->i_ino == st->ino && HOSTFS_I(inode)->dev == dev &&
  479. (inode->i_mode & S_IFMT) == (st->mode & S_IFMT) &&
  480. HOSTFS_I(inode)->btime.tv_sec == st->btime.tv_sec &&
  481. HOSTFS_I(inode)->btime.tv_nsec == st->btime.tv_nsec;
  482. }
  483. static struct inode *hostfs_iget(struct super_block *sb, char *name)
  484. {
  485. struct inode *inode;
  486. struct hostfs_stat st;
  487. int err = stat_file(name, &st, -1);
  488. if (err)
  489. return ERR_PTR(err);
  490. inode = iget5_locked(sb, st.ino, hostfs_inode_test, hostfs_inode_set,
  491. &st);
  492. if (!inode)
  493. return ERR_PTR(-ENOMEM);
  494. if (inode_state_read_once(inode) & I_NEW) {
  495. unlock_new_inode(inode);
  496. } else {
  497. spin_lock(&inode->i_lock);
  498. hostfs_inode_update(inode, &st);
  499. spin_unlock(&inode->i_lock);
  500. }
  501. return inode;
  502. }
  503. static int hostfs_create(struct mnt_idmap *idmap, struct inode *dir,
  504. struct dentry *dentry, umode_t mode, bool excl)
  505. {
  506. struct inode *inode;
  507. char *name;
  508. int fd;
  509. name = dentry_name(dentry);
  510. if (name == NULL)
  511. return -ENOMEM;
  512. fd = file_create(name, mode & 0777);
  513. if (fd < 0) {
  514. __putname(name);
  515. return fd;
  516. }
  517. inode = hostfs_iget(dir->i_sb, name);
  518. __putname(name);
  519. if (IS_ERR(inode))
  520. return PTR_ERR(inode);
  521. HOSTFS_I(inode)->fd = fd;
  522. HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
  523. d_instantiate(dentry, inode);
  524. return 0;
  525. }
  526. static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
  527. unsigned int flags)
  528. {
  529. struct inode *inode = NULL;
  530. char *name;
  531. name = dentry_name(dentry);
  532. if (name == NULL)
  533. return ERR_PTR(-ENOMEM);
  534. inode = hostfs_iget(ino->i_sb, name);
  535. __putname(name);
  536. if (inode == ERR_PTR(-ENOENT))
  537. inode = NULL;
  538. return d_splice_alias(inode, dentry);
  539. }
  540. static int hostfs_link(struct dentry *to, struct inode *ino,
  541. struct dentry *from)
  542. {
  543. char *from_name, *to_name;
  544. int err;
  545. if ((from_name = dentry_name(from)) == NULL)
  546. return -ENOMEM;
  547. to_name = dentry_name(to);
  548. if (to_name == NULL) {
  549. __putname(from_name);
  550. return -ENOMEM;
  551. }
  552. err = link_file(to_name, from_name);
  553. __putname(from_name);
  554. __putname(to_name);
  555. return err;
  556. }
  557. static int hostfs_unlink(struct inode *ino, struct dentry *dentry)
  558. {
  559. char *file;
  560. int err;
  561. if (append)
  562. return -EPERM;
  563. if ((file = dentry_name(dentry)) == NULL)
  564. return -ENOMEM;
  565. err = unlink_file(file);
  566. __putname(file);
  567. return err;
  568. }
  569. static int hostfs_symlink(struct mnt_idmap *idmap, struct inode *ino,
  570. struct dentry *dentry, const char *to)
  571. {
  572. char *file;
  573. int err;
  574. if ((file = dentry_name(dentry)) == NULL)
  575. return -ENOMEM;
  576. err = make_symlink(file, to);
  577. __putname(file);
  578. return err;
  579. }
  580. static struct dentry *hostfs_mkdir(struct mnt_idmap *idmap, struct inode *ino,
  581. struct dentry *dentry, umode_t mode)
  582. {
  583. struct inode *inode;
  584. char *file;
  585. int err;
  586. if ((file = dentry_name(dentry)) == NULL)
  587. return ERR_PTR(-ENOMEM);
  588. err = do_mkdir(file, mode);
  589. if (err) {
  590. dentry = ERR_PTR(err);
  591. } else {
  592. inode = hostfs_iget(dentry->d_sb, file);
  593. d_drop(dentry);
  594. dentry = d_splice_alias(inode, dentry);
  595. }
  596. __putname(file);
  597. return dentry;
  598. }
  599. static int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
  600. {
  601. char *file;
  602. int err;
  603. if ((file = dentry_name(dentry)) == NULL)
  604. return -ENOMEM;
  605. err = hostfs_do_rmdir(file);
  606. __putname(file);
  607. return err;
  608. }
  609. static int hostfs_mknod(struct mnt_idmap *idmap, struct inode *dir,
  610. struct dentry *dentry, umode_t mode, dev_t dev)
  611. {
  612. struct inode *inode;
  613. char *name;
  614. int err;
  615. name = dentry_name(dentry);
  616. if (name == NULL)
  617. return -ENOMEM;
  618. err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
  619. if (err) {
  620. __putname(name);
  621. return err;
  622. }
  623. inode = hostfs_iget(dir->i_sb, name);
  624. __putname(name);
  625. if (IS_ERR(inode))
  626. return PTR_ERR(inode);
  627. d_instantiate(dentry, inode);
  628. return 0;
  629. }
  630. static int hostfs_rename2(struct mnt_idmap *idmap,
  631. struct inode *old_dir, struct dentry *old_dentry,
  632. struct inode *new_dir, struct dentry *new_dentry,
  633. unsigned int flags)
  634. {
  635. char *old_name, *new_name;
  636. int err;
  637. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
  638. return -EINVAL;
  639. old_name = dentry_name(old_dentry);
  640. if (old_name == NULL)
  641. return -ENOMEM;
  642. new_name = dentry_name(new_dentry);
  643. if (new_name == NULL) {
  644. __putname(old_name);
  645. return -ENOMEM;
  646. }
  647. if (!flags)
  648. err = rename_file(old_name, new_name);
  649. else
  650. err = rename2_file(old_name, new_name, flags);
  651. __putname(old_name);
  652. __putname(new_name);
  653. return err;
  654. }
  655. static int hostfs_permission(struct mnt_idmap *idmap,
  656. struct inode *ino, int desired)
  657. {
  658. char *name;
  659. int r = 0, w = 0, x = 0, err;
  660. if (desired & MAY_NOT_BLOCK)
  661. return -ECHILD;
  662. if (desired & MAY_READ) r = 1;
  663. if (desired & MAY_WRITE) w = 1;
  664. if (desired & MAY_EXEC) x = 1;
  665. name = inode_name(ino);
  666. if (name == NULL)
  667. return -ENOMEM;
  668. if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
  669. S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
  670. err = 0;
  671. else
  672. err = access_file(name, r, w, x);
  673. __putname(name);
  674. if (!err)
  675. err = generic_permission(&nop_mnt_idmap, ino, desired);
  676. return err;
  677. }
  678. static int hostfs_setattr(struct mnt_idmap *idmap,
  679. struct dentry *dentry, struct iattr *attr)
  680. {
  681. struct inode *inode = d_inode(dentry);
  682. struct hostfs_iattr attrs;
  683. char *name;
  684. int err;
  685. int fd = HOSTFS_I(inode)->fd;
  686. err = setattr_prepare(&nop_mnt_idmap, dentry, attr);
  687. if (err)
  688. return err;
  689. if (append)
  690. attr->ia_valid &= ~ATTR_SIZE;
  691. attrs.ia_valid = 0;
  692. if (attr->ia_valid & ATTR_MODE) {
  693. attrs.ia_valid |= HOSTFS_ATTR_MODE;
  694. attrs.ia_mode = attr->ia_mode;
  695. }
  696. if (attr->ia_valid & ATTR_UID) {
  697. attrs.ia_valid |= HOSTFS_ATTR_UID;
  698. attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid);
  699. }
  700. if (attr->ia_valid & ATTR_GID) {
  701. attrs.ia_valid |= HOSTFS_ATTR_GID;
  702. attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid);
  703. }
  704. if (attr->ia_valid & ATTR_SIZE) {
  705. attrs.ia_valid |= HOSTFS_ATTR_SIZE;
  706. attrs.ia_size = attr->ia_size;
  707. }
  708. if (attr->ia_valid & ATTR_ATIME) {
  709. attrs.ia_valid |= HOSTFS_ATTR_ATIME;
  710. attrs.ia_atime = (struct hostfs_timespec)
  711. { attr->ia_atime.tv_sec, attr->ia_atime.tv_nsec };
  712. }
  713. if (attr->ia_valid & ATTR_MTIME) {
  714. attrs.ia_valid |= HOSTFS_ATTR_MTIME;
  715. attrs.ia_mtime = (struct hostfs_timespec)
  716. { attr->ia_mtime.tv_sec, attr->ia_mtime.tv_nsec };
  717. }
  718. if (attr->ia_valid & ATTR_CTIME) {
  719. attrs.ia_valid |= HOSTFS_ATTR_CTIME;
  720. attrs.ia_ctime = (struct hostfs_timespec)
  721. { attr->ia_ctime.tv_sec, attr->ia_ctime.tv_nsec };
  722. }
  723. if (attr->ia_valid & ATTR_ATIME_SET) {
  724. attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
  725. }
  726. if (attr->ia_valid & ATTR_MTIME_SET) {
  727. attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
  728. }
  729. name = dentry_name(dentry);
  730. if (name == NULL)
  731. return -ENOMEM;
  732. err = set_attr(name, &attrs, fd);
  733. __putname(name);
  734. if (err)
  735. return err;
  736. if ((attr->ia_valid & ATTR_SIZE) &&
  737. attr->ia_size != i_size_read(inode))
  738. truncate_setsize(inode, attr->ia_size);
  739. setattr_copy(&nop_mnt_idmap, inode, attr);
  740. mark_inode_dirty(inode);
  741. return 0;
  742. }
  743. static const struct inode_operations hostfs_iops = {
  744. .permission = hostfs_permission,
  745. .setattr = hostfs_setattr,
  746. };
  747. static const struct inode_operations hostfs_dir_iops = {
  748. .create = hostfs_create,
  749. .lookup = hostfs_lookup,
  750. .link = hostfs_link,
  751. .unlink = hostfs_unlink,
  752. .symlink = hostfs_symlink,
  753. .mkdir = hostfs_mkdir,
  754. .rmdir = hostfs_rmdir,
  755. .mknod = hostfs_mknod,
  756. .rename = hostfs_rename2,
  757. .permission = hostfs_permission,
  758. .setattr = hostfs_setattr,
  759. };
  760. static const char *hostfs_get_link(struct dentry *dentry,
  761. struct inode *inode,
  762. struct delayed_call *done)
  763. {
  764. char *link;
  765. if (!dentry)
  766. return ERR_PTR(-ECHILD);
  767. link = kmalloc(PATH_MAX, GFP_KERNEL);
  768. if (link) {
  769. char *path = dentry_name(dentry);
  770. int err = -ENOMEM;
  771. if (path) {
  772. err = hostfs_do_readlink(path, link, PATH_MAX);
  773. if (err == PATH_MAX)
  774. err = -E2BIG;
  775. __putname(path);
  776. }
  777. if (err < 0) {
  778. kfree(link);
  779. return ERR_PTR(err);
  780. }
  781. } else {
  782. return ERR_PTR(-ENOMEM);
  783. }
  784. set_delayed_call(done, kfree_link, link);
  785. return link;
  786. }
  787. static const struct inode_operations hostfs_link_iops = {
  788. .get_link = hostfs_get_link,
  789. };
  790. static int hostfs_fill_super(struct super_block *sb, struct fs_context *fc)
  791. {
  792. struct hostfs_fs_info *fsi = sb->s_fs_info;
  793. struct inode *root_inode;
  794. int err;
  795. sb->s_blocksize = 1024;
  796. sb->s_blocksize_bits = 10;
  797. sb->s_magic = HOSTFS_SUPER_MAGIC;
  798. sb->s_op = &hostfs_sbops;
  799. sb->s_d_flags = DCACHE_DONTCACHE;
  800. sb->s_maxbytes = MAX_LFS_FILESIZE;
  801. err = super_setup_bdi(sb);
  802. if (err)
  803. return err;
  804. root_inode = hostfs_iget(sb, fsi->host_root_path);
  805. if (IS_ERR(root_inode))
  806. return PTR_ERR(root_inode);
  807. if (S_ISLNK(root_inode->i_mode)) {
  808. char *name;
  809. iput(root_inode);
  810. name = follow_link(fsi->host_root_path);
  811. if (IS_ERR(name))
  812. return PTR_ERR(name);
  813. root_inode = hostfs_iget(sb, name);
  814. kfree(name);
  815. if (IS_ERR(root_inode))
  816. return PTR_ERR(root_inode);
  817. }
  818. sb->s_root = d_make_root(root_inode);
  819. if (sb->s_root == NULL)
  820. return -ENOMEM;
  821. return 0;
  822. }
  823. enum hostfs_parma {
  824. Opt_hostfs,
  825. };
  826. static const struct fs_parameter_spec hostfs_param_specs[] = {
  827. fsparam_string_empty("hostfs", Opt_hostfs),
  828. {}
  829. };
  830. static int hostfs_parse_param(struct fs_context *fc, struct fs_parameter *param)
  831. {
  832. struct hostfs_fs_info *fsi = fc->s_fs_info;
  833. struct fs_parse_result result;
  834. char *host_root, *tmp_root;
  835. int opt;
  836. opt = fs_parse(fc, hostfs_param_specs, param, &result);
  837. if (opt < 0)
  838. return opt;
  839. switch (opt) {
  840. case Opt_hostfs:
  841. host_root = param->string;
  842. if (!*host_root)
  843. break;
  844. tmp_root = kasprintf(GFP_KERNEL, "%s%s",
  845. fsi->host_root_path, host_root);
  846. if (!tmp_root)
  847. return -ENOMEM;
  848. kfree(fsi->host_root_path);
  849. fsi->host_root_path = tmp_root;
  850. break;
  851. }
  852. return 0;
  853. }
  854. static int hostfs_parse_monolithic(struct fs_context *fc, void *data)
  855. {
  856. struct hostfs_fs_info *fsi = fc->s_fs_info;
  857. char *tmp_root, *host_root = (char *)data;
  858. /* NULL is printed as '(null)' by printf(): avoid that. */
  859. if (host_root == NULL)
  860. return 0;
  861. tmp_root = kasprintf(GFP_KERNEL, "%s%s", fsi->host_root_path, host_root);
  862. if (!tmp_root)
  863. return -ENOMEM;
  864. kfree(fsi->host_root_path);
  865. fsi->host_root_path = tmp_root;
  866. return 0;
  867. }
  868. static int hostfs_fc_get_tree(struct fs_context *fc)
  869. {
  870. return get_tree_nodev(fc, hostfs_fill_super);
  871. }
  872. static void hostfs_fc_free(struct fs_context *fc)
  873. {
  874. struct hostfs_fs_info *fsi = fc->s_fs_info;
  875. if (!fsi)
  876. return;
  877. kfree(fsi->host_root_path);
  878. kfree(fsi);
  879. }
  880. static const struct fs_context_operations hostfs_context_ops = {
  881. .parse_monolithic = hostfs_parse_monolithic,
  882. .parse_param = hostfs_parse_param,
  883. .get_tree = hostfs_fc_get_tree,
  884. .free = hostfs_fc_free,
  885. };
  886. static int hostfs_init_fs_context(struct fs_context *fc)
  887. {
  888. struct hostfs_fs_info *fsi;
  889. fsi = kzalloc_obj(*fsi);
  890. if (!fsi)
  891. return -ENOMEM;
  892. fsi->host_root_path = kasprintf(GFP_KERNEL, "%s/", root_ino);
  893. if (!fsi->host_root_path) {
  894. kfree(fsi);
  895. return -ENOMEM;
  896. }
  897. fc->s_fs_info = fsi;
  898. fc->ops = &hostfs_context_ops;
  899. return 0;
  900. }
  901. static void hostfs_kill_sb(struct super_block *s)
  902. {
  903. kill_anon_super(s);
  904. kfree(s->s_fs_info);
  905. }
  906. static struct file_system_type hostfs_type = {
  907. .owner = THIS_MODULE,
  908. .name = "hostfs",
  909. .init_fs_context = hostfs_init_fs_context,
  910. .kill_sb = hostfs_kill_sb,
  911. .fs_flags = 0,
  912. };
  913. MODULE_ALIAS_FS("hostfs");
  914. static int __init init_hostfs(void)
  915. {
  916. hostfs_inode_cache = KMEM_CACHE(hostfs_inode_info, 0);
  917. if (!hostfs_inode_cache)
  918. return -ENOMEM;
  919. return register_filesystem(&hostfs_type);
  920. }
  921. static void __exit exit_hostfs(void)
  922. {
  923. unregister_filesystem(&hostfs_type);
  924. kmem_cache_destroy(hostfs_inode_cache);
  925. }
  926. module_init(init_hostfs)
  927. module_exit(exit_hostfs)
  928. MODULE_DESCRIPTION("User-Mode Linux Host filesystem");
  929. MODULE_LICENSE("GPL");