hostfs_kern.c 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  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. int len, n;
  119. char *name, *resolved, *end;
  120. name = __getname();
  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. len = strlen(link) + strlen(name) + 1;
  139. resolved = kmalloc(len, GFP_KERNEL);
  140. if (resolved == NULL) {
  141. n = -ENOMEM;
  142. goto out_free;
  143. }
  144. sprintf(resolved, "%s%s", link, name);
  145. __putname(name);
  146. kfree(link);
  147. return resolved;
  148. out_free:
  149. __putname(name);
  150. return ERR_PTR(n);
  151. }
  152. static struct inode *hostfs_iget(struct super_block *sb)
  153. {
  154. struct inode *inode = new_inode(sb);
  155. if (!inode)
  156. return ERR_PTR(-ENOMEM);
  157. return inode;
  158. }
  159. static int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
  160. {
  161. /*
  162. * do_statfs uses struct statfs64 internally, but the linux kernel
  163. * struct statfs still has 32-bit versions for most of these fields,
  164. * so we convert them here
  165. */
  166. int err;
  167. long long f_blocks;
  168. long long f_bfree;
  169. long long f_bavail;
  170. long long f_files;
  171. long long f_ffree;
  172. err = do_statfs(dentry->d_sb->s_fs_info,
  173. &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
  174. &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
  175. &sf->f_namelen);
  176. if (err)
  177. return err;
  178. sf->f_blocks = f_blocks;
  179. sf->f_bfree = f_bfree;
  180. sf->f_bavail = f_bavail;
  181. sf->f_files = f_files;
  182. sf->f_ffree = f_ffree;
  183. sf->f_type = HOSTFS_SUPER_MAGIC;
  184. return 0;
  185. }
  186. static struct inode *hostfs_alloc_inode(struct super_block *sb)
  187. {
  188. struct hostfs_inode_info *hi;
  189. hi = kmalloc(sizeof(*hi), GFP_KERNEL);
  190. if (hi == NULL)
  191. return NULL;
  192. hi->fd = -1;
  193. hi->mode = 0;
  194. inode_init_once(&hi->vfs_inode);
  195. mutex_init(&hi->open_mutex);
  196. return &hi->vfs_inode;
  197. }
  198. static void hostfs_evict_inode(struct inode *inode)
  199. {
  200. truncate_inode_pages_final(&inode->i_data);
  201. clear_inode(inode);
  202. if (HOSTFS_I(inode)->fd != -1) {
  203. close_file(&HOSTFS_I(inode)->fd);
  204. HOSTFS_I(inode)->fd = -1;
  205. }
  206. }
  207. static void hostfs_i_callback(struct rcu_head *head)
  208. {
  209. struct inode *inode = container_of(head, struct inode, i_rcu);
  210. kfree(HOSTFS_I(inode));
  211. }
  212. static void hostfs_destroy_inode(struct inode *inode)
  213. {
  214. call_rcu(&inode->i_rcu, hostfs_i_callback);
  215. }
  216. static int hostfs_show_options(struct seq_file *seq, struct dentry *root)
  217. {
  218. const char *root_path = root->d_sb->s_fs_info;
  219. size_t offset = strlen(root_ino) + 1;
  220. if (strlen(root_path) > offset)
  221. seq_printf(seq, ",%s", root_path + offset);
  222. if (append)
  223. seq_puts(seq, ",append");
  224. return 0;
  225. }
  226. static const struct super_operations hostfs_sbops = {
  227. .alloc_inode = hostfs_alloc_inode,
  228. .destroy_inode = hostfs_destroy_inode,
  229. .evict_inode = hostfs_evict_inode,
  230. .statfs = hostfs_statfs,
  231. .show_options = hostfs_show_options,
  232. };
  233. static int hostfs_readdir(struct file *file, struct dir_context *ctx)
  234. {
  235. void *dir;
  236. char *name;
  237. unsigned long long next, ino;
  238. int error, len;
  239. unsigned int type;
  240. name = dentry_name(file->f_path.dentry);
  241. if (name == NULL)
  242. return -ENOMEM;
  243. dir = open_dir(name, &error);
  244. __putname(name);
  245. if (dir == NULL)
  246. return -error;
  247. next = ctx->pos;
  248. seek_dir(dir, next);
  249. while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) {
  250. if (!dir_emit(ctx, name, len, ino, type))
  251. break;
  252. ctx->pos = next;
  253. }
  254. close_dir(dir);
  255. return 0;
  256. }
  257. static int hostfs_open(struct inode *ino, struct file *file)
  258. {
  259. char *name;
  260. fmode_t mode;
  261. int err;
  262. int r, w, fd;
  263. mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
  264. if ((mode & HOSTFS_I(ino)->mode) == mode)
  265. return 0;
  266. mode |= HOSTFS_I(ino)->mode;
  267. retry:
  268. r = w = 0;
  269. if (mode & FMODE_READ)
  270. r = 1;
  271. if (mode & FMODE_WRITE)
  272. r = w = 1;
  273. name = dentry_name(file->f_path.dentry);
  274. if (name == NULL)
  275. return -ENOMEM;
  276. fd = open_file(name, r, w, append);
  277. __putname(name);
  278. if (fd < 0)
  279. return fd;
  280. mutex_lock(&HOSTFS_I(ino)->open_mutex);
  281. /* somebody else had handled it first? */
  282. if ((mode & HOSTFS_I(ino)->mode) == mode) {
  283. mutex_unlock(&HOSTFS_I(ino)->open_mutex);
  284. close_file(&fd);
  285. return 0;
  286. }
  287. if ((mode | HOSTFS_I(ino)->mode) != mode) {
  288. mode |= HOSTFS_I(ino)->mode;
  289. mutex_unlock(&HOSTFS_I(ino)->open_mutex);
  290. close_file(&fd);
  291. goto retry;
  292. }
  293. if (HOSTFS_I(ino)->fd == -1) {
  294. HOSTFS_I(ino)->fd = fd;
  295. } else {
  296. err = replace_file(fd, HOSTFS_I(ino)->fd);
  297. close_file(&fd);
  298. if (err < 0) {
  299. mutex_unlock(&HOSTFS_I(ino)->open_mutex);
  300. return err;
  301. }
  302. }
  303. HOSTFS_I(ino)->mode = mode;
  304. mutex_unlock(&HOSTFS_I(ino)->open_mutex);
  305. return 0;
  306. }
  307. static int hostfs_file_release(struct inode *inode, struct file *file)
  308. {
  309. filemap_write_and_wait(inode->i_mapping);
  310. return 0;
  311. }
  312. static int hostfs_fsync(struct file *file, loff_t start, loff_t end,
  313. int datasync)
  314. {
  315. struct inode *inode = file->f_mapping->host;
  316. int ret;
  317. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  318. if (ret)
  319. return ret;
  320. mutex_lock(&inode->i_mutex);
  321. ret = fsync_file(HOSTFS_I(inode)->fd, datasync);
  322. mutex_unlock(&inode->i_mutex);
  323. return ret;
  324. }
  325. static const struct file_operations hostfs_file_fops = {
  326. .llseek = generic_file_llseek,
  327. .splice_read = generic_file_splice_read,
  328. .read_iter = generic_file_read_iter,
  329. .write_iter = generic_file_write_iter,
  330. .mmap = generic_file_mmap,
  331. .open = hostfs_open,
  332. .release = hostfs_file_release,
  333. .fsync = hostfs_fsync,
  334. };
  335. static const struct file_operations hostfs_dir_fops = {
  336. .llseek = generic_file_llseek,
  337. .iterate = hostfs_readdir,
  338. .read = generic_read_dir,
  339. .open = hostfs_open,
  340. .fsync = hostfs_fsync,
  341. };
  342. static int hostfs_writepage(struct page *page, struct writeback_control *wbc)
  343. {
  344. struct address_space *mapping = page->mapping;
  345. struct inode *inode = mapping->host;
  346. char *buffer;
  347. loff_t base = page_offset(page);
  348. int count = PAGE_CACHE_SIZE;
  349. int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
  350. int err;
  351. if (page->index >= end_index)
  352. count = inode->i_size & (PAGE_CACHE_SIZE-1);
  353. buffer = kmap(page);
  354. err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
  355. if (err != count) {
  356. ClearPageUptodate(page);
  357. goto out;
  358. }
  359. if (base > inode->i_size)
  360. inode->i_size = base;
  361. if (PageError(page))
  362. ClearPageError(page);
  363. err = 0;
  364. out:
  365. kunmap(page);
  366. unlock_page(page);
  367. return err;
  368. }
  369. static int hostfs_readpage(struct file *file, struct page *page)
  370. {
  371. char *buffer;
  372. loff_t start = page_offset(page);
  373. int bytes_read, ret = 0;
  374. buffer = kmap(page);
  375. bytes_read = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
  376. PAGE_CACHE_SIZE);
  377. if (bytes_read < 0) {
  378. ClearPageUptodate(page);
  379. SetPageError(page);
  380. ret = bytes_read;
  381. goto out;
  382. }
  383. memset(buffer + bytes_read, 0, PAGE_CACHE_SIZE - bytes_read);
  384. ClearPageError(page);
  385. SetPageUptodate(page);
  386. out:
  387. flush_dcache_page(page);
  388. kunmap(page);
  389. unlock_page(page);
  390. return ret;
  391. }
  392. static int hostfs_write_begin(struct file *file, struct address_space *mapping,
  393. loff_t pos, unsigned len, unsigned flags,
  394. struct page **pagep, void **fsdata)
  395. {
  396. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  397. *pagep = grab_cache_page_write_begin(mapping, index, flags);
  398. if (!*pagep)
  399. return -ENOMEM;
  400. return 0;
  401. }
  402. static int hostfs_write_end(struct file *file, struct address_space *mapping,
  403. loff_t pos, unsigned len, unsigned copied,
  404. struct page *page, void *fsdata)
  405. {
  406. struct inode *inode = mapping->host;
  407. void *buffer;
  408. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  409. int err;
  410. buffer = kmap(page);
  411. err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
  412. kunmap(page);
  413. if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
  414. SetPageUptodate(page);
  415. /*
  416. * If err > 0, write_file has added err to pos, so we are comparing
  417. * i_size against the last byte written.
  418. */
  419. if (err > 0 && (pos > inode->i_size))
  420. inode->i_size = pos;
  421. unlock_page(page);
  422. page_cache_release(page);
  423. return err;
  424. }
  425. static const struct address_space_operations hostfs_aops = {
  426. .writepage = hostfs_writepage,
  427. .readpage = hostfs_readpage,
  428. .set_page_dirty = __set_page_dirty_nobuffers,
  429. .write_begin = hostfs_write_begin,
  430. .write_end = hostfs_write_end,
  431. };
  432. static int read_name(struct inode *ino, char *name)
  433. {
  434. dev_t rdev;
  435. struct hostfs_stat st;
  436. int err = stat_file(name, &st, -1);
  437. if (err)
  438. return err;
  439. /* Reencode maj and min with the kernel encoding.*/
  440. rdev = MKDEV(st.maj, st.min);
  441. switch (st.mode & S_IFMT) {
  442. case S_IFLNK:
  443. ino->i_op = &hostfs_link_iops;
  444. break;
  445. case S_IFDIR:
  446. ino->i_op = &hostfs_dir_iops;
  447. ino->i_fop = &hostfs_dir_fops;
  448. break;
  449. case S_IFCHR:
  450. case S_IFBLK:
  451. case S_IFIFO:
  452. case S_IFSOCK:
  453. init_special_inode(ino, st.mode & S_IFMT, rdev);
  454. ino->i_op = &hostfs_iops;
  455. break;
  456. case S_IFREG:
  457. ino->i_op = &hostfs_iops;
  458. ino->i_fop = &hostfs_file_fops;
  459. ino->i_mapping->a_ops = &hostfs_aops;
  460. break;
  461. default:
  462. return -EIO;
  463. }
  464. ino->i_ino = st.ino;
  465. ino->i_mode = st.mode;
  466. set_nlink(ino, st.nlink);
  467. i_uid_write(ino, st.uid);
  468. i_gid_write(ino, st.gid);
  469. ino->i_atime = st.atime;
  470. ino->i_mtime = st.mtime;
  471. ino->i_ctime = st.ctime;
  472. ino->i_size = st.size;
  473. ino->i_blocks = st.blocks;
  474. return 0;
  475. }
  476. static int hostfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  477. bool excl)
  478. {
  479. struct inode *inode;
  480. char *name;
  481. int error, fd;
  482. inode = hostfs_iget(dir->i_sb);
  483. if (IS_ERR(inode)) {
  484. error = PTR_ERR(inode);
  485. goto out;
  486. }
  487. error = -ENOMEM;
  488. name = dentry_name(dentry);
  489. if (name == NULL)
  490. goto out_put;
  491. fd = file_create(name, mode & 0777);
  492. if (fd < 0)
  493. error = fd;
  494. else
  495. error = read_name(inode, name);
  496. __putname(name);
  497. if (error)
  498. goto out_put;
  499. HOSTFS_I(inode)->fd = fd;
  500. HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
  501. d_instantiate(dentry, inode);
  502. return 0;
  503. out_put:
  504. iput(inode);
  505. out:
  506. return error;
  507. }
  508. static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
  509. unsigned int flags)
  510. {
  511. struct inode *inode;
  512. char *name;
  513. int err;
  514. inode = hostfs_iget(ino->i_sb);
  515. if (IS_ERR(inode)) {
  516. err = PTR_ERR(inode);
  517. goto out;
  518. }
  519. err = -ENOMEM;
  520. name = dentry_name(dentry);
  521. if (name == NULL)
  522. goto out_put;
  523. err = read_name(inode, name);
  524. __putname(name);
  525. if (err == -ENOENT) {
  526. iput(inode);
  527. inode = NULL;
  528. }
  529. else if (err)
  530. goto out_put;
  531. d_add(dentry, inode);
  532. return NULL;
  533. out_put:
  534. iput(inode);
  535. out:
  536. return ERR_PTR(err);
  537. }
  538. static int hostfs_link(struct dentry *to, struct inode *ino,
  539. struct dentry *from)
  540. {
  541. char *from_name, *to_name;
  542. int err;
  543. if ((from_name = dentry_name(from)) == NULL)
  544. return -ENOMEM;
  545. to_name = dentry_name(to);
  546. if (to_name == NULL) {
  547. __putname(from_name);
  548. return -ENOMEM;
  549. }
  550. err = link_file(to_name, from_name);
  551. __putname(from_name);
  552. __putname(to_name);
  553. return err;
  554. }
  555. static int hostfs_unlink(struct inode *ino, struct dentry *dentry)
  556. {
  557. char *file;
  558. int err;
  559. if (append)
  560. return -EPERM;
  561. if ((file = dentry_name(dentry)) == NULL)
  562. return -ENOMEM;
  563. err = unlink_file(file);
  564. __putname(file);
  565. return err;
  566. }
  567. static int hostfs_symlink(struct inode *ino, struct dentry *dentry,
  568. const char *to)
  569. {
  570. char *file;
  571. int err;
  572. if ((file = dentry_name(dentry)) == NULL)
  573. return -ENOMEM;
  574. err = make_symlink(file, to);
  575. __putname(file);
  576. return err;
  577. }
  578. static int hostfs_mkdir(struct inode *ino, struct dentry *dentry, umode_t mode)
  579. {
  580. char *file;
  581. int err;
  582. if ((file = dentry_name(dentry)) == NULL)
  583. return -ENOMEM;
  584. err = do_mkdir(file, mode);
  585. __putname(file);
  586. return err;
  587. }
  588. static int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
  589. {
  590. char *file;
  591. int err;
  592. if ((file = dentry_name(dentry)) == NULL)
  593. return -ENOMEM;
  594. err = do_rmdir(file);
  595. __putname(file);
  596. return err;
  597. }
  598. static int hostfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  599. {
  600. struct inode *inode;
  601. char *name;
  602. int err;
  603. inode = hostfs_iget(dir->i_sb);
  604. if (IS_ERR(inode)) {
  605. err = PTR_ERR(inode);
  606. goto out;
  607. }
  608. err = -ENOMEM;
  609. name = dentry_name(dentry);
  610. if (name == NULL)
  611. goto out_put;
  612. init_special_inode(inode, mode, dev);
  613. err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
  614. if (!err)
  615. goto out_free;
  616. err = read_name(inode, name);
  617. __putname(name);
  618. if (err)
  619. goto out_put;
  620. if (err)
  621. goto out_put;
  622. d_instantiate(dentry, inode);
  623. return 0;
  624. out_free:
  625. __putname(name);
  626. out_put:
  627. iput(inode);
  628. out:
  629. return err;
  630. }
  631. static int hostfs_rename2(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 inode *ino, int desired)
  656. {
  657. char *name;
  658. int r = 0, w = 0, x = 0, err;
  659. if (desired & MAY_NOT_BLOCK)
  660. return -ECHILD;
  661. if (desired & MAY_READ) r = 1;
  662. if (desired & MAY_WRITE) w = 1;
  663. if (desired & MAY_EXEC) x = 1;
  664. name = inode_name(ino);
  665. if (name == NULL)
  666. return -ENOMEM;
  667. if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
  668. S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
  669. err = 0;
  670. else
  671. err = access_file(name, r, w, x);
  672. __putname(name);
  673. if (!err)
  674. err = generic_permission(ino, desired);
  675. return err;
  676. }
  677. static int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
  678. {
  679. struct inode *inode = d_inode(dentry);
  680. struct hostfs_iattr attrs;
  681. char *name;
  682. int err;
  683. int fd = HOSTFS_I(inode)->fd;
  684. err = inode_change_ok(inode, attr);
  685. if (err)
  686. return err;
  687. if (append)
  688. attr->ia_valid &= ~ATTR_SIZE;
  689. attrs.ia_valid = 0;
  690. if (attr->ia_valid & ATTR_MODE) {
  691. attrs.ia_valid |= HOSTFS_ATTR_MODE;
  692. attrs.ia_mode = attr->ia_mode;
  693. }
  694. if (attr->ia_valid & ATTR_UID) {
  695. attrs.ia_valid |= HOSTFS_ATTR_UID;
  696. attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid);
  697. }
  698. if (attr->ia_valid & ATTR_GID) {
  699. attrs.ia_valid |= HOSTFS_ATTR_GID;
  700. attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid);
  701. }
  702. if (attr->ia_valid & ATTR_SIZE) {
  703. attrs.ia_valid |= HOSTFS_ATTR_SIZE;
  704. attrs.ia_size = attr->ia_size;
  705. }
  706. if (attr->ia_valid & ATTR_ATIME) {
  707. attrs.ia_valid |= HOSTFS_ATTR_ATIME;
  708. attrs.ia_atime = attr->ia_atime;
  709. }
  710. if (attr->ia_valid & ATTR_MTIME) {
  711. attrs.ia_valid |= HOSTFS_ATTR_MTIME;
  712. attrs.ia_mtime = attr->ia_mtime;
  713. }
  714. if (attr->ia_valid & ATTR_CTIME) {
  715. attrs.ia_valid |= HOSTFS_ATTR_CTIME;
  716. attrs.ia_ctime = attr->ia_ctime;
  717. }
  718. if (attr->ia_valid & ATTR_ATIME_SET) {
  719. attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
  720. }
  721. if (attr->ia_valid & ATTR_MTIME_SET) {
  722. attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
  723. }
  724. name = dentry_name(dentry);
  725. if (name == NULL)
  726. return -ENOMEM;
  727. err = set_attr(name, &attrs, fd);
  728. __putname(name);
  729. if (err)
  730. return err;
  731. if ((attr->ia_valid & ATTR_SIZE) &&
  732. attr->ia_size != i_size_read(inode))
  733. truncate_setsize(inode, attr->ia_size);
  734. setattr_copy(inode, attr);
  735. mark_inode_dirty(inode);
  736. return 0;
  737. }
  738. static const struct inode_operations hostfs_iops = {
  739. .permission = hostfs_permission,
  740. .setattr = hostfs_setattr,
  741. };
  742. static const struct inode_operations hostfs_dir_iops = {
  743. .create = hostfs_create,
  744. .lookup = hostfs_lookup,
  745. .link = hostfs_link,
  746. .unlink = hostfs_unlink,
  747. .symlink = hostfs_symlink,
  748. .mkdir = hostfs_mkdir,
  749. .rmdir = hostfs_rmdir,
  750. .mknod = hostfs_mknod,
  751. .rename2 = hostfs_rename2,
  752. .permission = hostfs_permission,
  753. .setattr = hostfs_setattr,
  754. };
  755. static const char *hostfs_follow_link(struct dentry *dentry, void **cookie)
  756. {
  757. char *link = __getname();
  758. if (link) {
  759. char *path = dentry_name(dentry);
  760. int err = -ENOMEM;
  761. if (path) {
  762. err = hostfs_do_readlink(path, link, PATH_MAX);
  763. if (err == PATH_MAX)
  764. err = -E2BIG;
  765. __putname(path);
  766. }
  767. if (err < 0) {
  768. __putname(link);
  769. return ERR_PTR(err);
  770. }
  771. } else {
  772. return ERR_PTR(-ENOMEM);
  773. }
  774. return *cookie = link;
  775. }
  776. static void hostfs_put_link(struct inode *unused, void *cookie)
  777. {
  778. __putname(cookie);
  779. }
  780. static const struct inode_operations hostfs_link_iops = {
  781. .readlink = generic_readlink,
  782. .follow_link = hostfs_follow_link,
  783. .put_link = hostfs_put_link,
  784. };
  785. static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
  786. {
  787. struct inode *root_inode;
  788. char *host_root_path, *req_root = d;
  789. int err;
  790. sb->s_blocksize = 1024;
  791. sb->s_blocksize_bits = 10;
  792. sb->s_magic = HOSTFS_SUPER_MAGIC;
  793. sb->s_op = &hostfs_sbops;
  794. sb->s_d_op = &simple_dentry_operations;
  795. sb->s_maxbytes = MAX_LFS_FILESIZE;
  796. /* NULL is printed as <NULL> by sprintf: avoid that. */
  797. if (req_root == NULL)
  798. req_root = "";
  799. err = -ENOMEM;
  800. sb->s_fs_info = host_root_path =
  801. kmalloc(strlen(root_ino) + strlen(req_root) + 2, GFP_KERNEL);
  802. if (host_root_path == NULL)
  803. goto out;
  804. sprintf(host_root_path, "%s/%s", root_ino, req_root);
  805. root_inode = new_inode(sb);
  806. if (!root_inode)
  807. goto out;
  808. err = read_name(root_inode, host_root_path);
  809. if (err)
  810. goto out_put;
  811. if (S_ISLNK(root_inode->i_mode)) {
  812. char *name = follow_link(host_root_path);
  813. if (IS_ERR(name))
  814. err = PTR_ERR(name);
  815. else
  816. err = read_name(root_inode, name);
  817. kfree(name);
  818. if (err)
  819. goto out_put;
  820. }
  821. err = -ENOMEM;
  822. sb->s_root = d_make_root(root_inode);
  823. if (sb->s_root == NULL)
  824. goto out;
  825. return 0;
  826. out_put:
  827. iput(root_inode);
  828. out:
  829. return err;
  830. }
  831. static struct dentry *hostfs_read_sb(struct file_system_type *type,
  832. int flags, const char *dev_name,
  833. void *data)
  834. {
  835. return mount_nodev(type, flags, data, hostfs_fill_sb_common);
  836. }
  837. static void hostfs_kill_sb(struct super_block *s)
  838. {
  839. kill_anon_super(s);
  840. kfree(s->s_fs_info);
  841. }
  842. static struct file_system_type hostfs_type = {
  843. .owner = THIS_MODULE,
  844. .name = "hostfs",
  845. .mount = hostfs_read_sb,
  846. .kill_sb = hostfs_kill_sb,
  847. .fs_flags = 0,
  848. };
  849. MODULE_ALIAS_FS("hostfs");
  850. static int __init init_hostfs(void)
  851. {
  852. return register_filesystem(&hostfs_type);
  853. }
  854. static void __exit exit_hostfs(void)
  855. {
  856. unregister_filesystem(&hostfs_type);
  857. }
  858. module_init(init_hostfs)
  859. module_exit(exit_hostfs)
  860. MODULE_LICENSE("GPL");