hostfs_kern.c 21 KB

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