hostfs_kern.c 21 KB

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