hostfs_kern.c 21 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) {
  249. if (!dir_emit(ctx, name, len, ino, type))
  250. break;
  251. ctx->pos = next;
  252. }
  253. close_dir(dir);
  254. return 0;
  255. }
  256. static int hostfs_open(struct inode *ino, struct file *file)
  257. {
  258. char *name;
  259. fmode_t mode;
  260. int err;
  261. int r, w, fd;
  262. mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
  263. if ((mode & HOSTFS_I(ino)->mode) == mode)
  264. return 0;
  265. mode |= HOSTFS_I(ino)->mode;
  266. retry:
  267. r = w = 0;
  268. if (mode & FMODE_READ)
  269. r = 1;
  270. if (mode & FMODE_WRITE)
  271. r = w = 1;
  272. name = dentry_name(file->f_path.dentry);
  273. if (name == NULL)
  274. return -ENOMEM;
  275. fd = open_file(name, r, w, append);
  276. __putname(name);
  277. if (fd < 0)
  278. return fd;
  279. mutex_lock(&HOSTFS_I(ino)->open_mutex);
  280. /* somebody else had handled it first? */
  281. if ((mode & HOSTFS_I(ino)->mode) == mode) {
  282. mutex_unlock(&HOSTFS_I(ino)->open_mutex);
  283. close_file(&fd);
  284. return 0;
  285. }
  286. if ((mode | HOSTFS_I(ino)->mode) != mode) {
  287. mode |= HOSTFS_I(ino)->mode;
  288. mutex_unlock(&HOSTFS_I(ino)->open_mutex);
  289. close_file(&fd);
  290. goto retry;
  291. }
  292. if (HOSTFS_I(ino)->fd == -1) {
  293. HOSTFS_I(ino)->fd = fd;
  294. } else {
  295. err = replace_file(fd, HOSTFS_I(ino)->fd);
  296. close_file(&fd);
  297. if (err < 0) {
  298. mutex_unlock(&HOSTFS_I(ino)->open_mutex);
  299. return err;
  300. }
  301. }
  302. HOSTFS_I(ino)->mode = mode;
  303. mutex_unlock(&HOSTFS_I(ino)->open_mutex);
  304. return 0;
  305. }
  306. static int hostfs_file_release(struct inode *inode, struct file *file)
  307. {
  308. filemap_write_and_wait(inode->i_mapping);
  309. return 0;
  310. }
  311. static int hostfs_fsync(struct file *file, loff_t start, loff_t end,
  312. int datasync)
  313. {
  314. struct inode *inode = file->f_mapping->host;
  315. int ret;
  316. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  317. if (ret)
  318. return ret;
  319. mutex_lock(&inode->i_mutex);
  320. ret = fsync_file(HOSTFS_I(inode)->fd, datasync);
  321. mutex_unlock(&inode->i_mutex);
  322. return ret;
  323. }
  324. static const struct file_operations hostfs_file_fops = {
  325. .llseek = generic_file_llseek,
  326. .read = new_sync_read,
  327. .splice_read = generic_file_splice_read,
  328. .read_iter = generic_file_read_iter,
  329. .write_iter = generic_file_write_iter,
  330. .write = new_sync_write,
  331. .mmap = generic_file_mmap,
  332. .open = hostfs_open,
  333. .release = hostfs_file_release,
  334. .fsync = hostfs_fsync,
  335. };
  336. static const struct file_operations hostfs_dir_fops = {
  337. .llseek = generic_file_llseek,
  338. .iterate = hostfs_readdir,
  339. .read = generic_read_dir,
  340. .open = hostfs_open,
  341. .fsync = hostfs_fsync,
  342. };
  343. static int hostfs_writepage(struct page *page, struct writeback_control *wbc)
  344. {
  345. struct address_space *mapping = page->mapping;
  346. struct inode *inode = mapping->host;
  347. char *buffer;
  348. loff_t base = page_offset(page);
  349. int count = PAGE_CACHE_SIZE;
  350. int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
  351. int err;
  352. if (page->index >= end_index)
  353. count = inode->i_size & (PAGE_CACHE_SIZE-1);
  354. buffer = kmap(page);
  355. err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
  356. if (err != count) {
  357. ClearPageUptodate(page);
  358. goto out;
  359. }
  360. if (base > inode->i_size)
  361. inode->i_size = base;
  362. if (PageError(page))
  363. ClearPageError(page);
  364. err = 0;
  365. out:
  366. kunmap(page);
  367. unlock_page(page);
  368. return err;
  369. }
  370. static int hostfs_readpage(struct file *file, struct page *page)
  371. {
  372. char *buffer;
  373. loff_t start = page_offset(page);
  374. int bytes_read, ret = 0;
  375. buffer = kmap(page);
  376. bytes_read = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
  377. PAGE_CACHE_SIZE);
  378. if (bytes_read < 0) {
  379. ClearPageUptodate(page);
  380. SetPageError(page);
  381. ret = bytes_read;
  382. goto out;
  383. }
  384. memset(buffer + bytes_read, 0, PAGE_CACHE_SIZE - bytes_read);
  385. ClearPageError(page);
  386. SetPageUptodate(page);
  387. out:
  388. flush_dcache_page(page);
  389. kunmap(page);
  390. unlock_page(page);
  391. return ret;
  392. }
  393. static int hostfs_write_begin(struct file *file, struct address_space *mapping,
  394. loff_t pos, unsigned len, unsigned flags,
  395. struct page **pagep, void **fsdata)
  396. {
  397. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  398. *pagep = grab_cache_page_write_begin(mapping, index, flags);
  399. if (!*pagep)
  400. return -ENOMEM;
  401. return 0;
  402. }
  403. static int hostfs_write_end(struct file *file, struct address_space *mapping,
  404. loff_t pos, unsigned len, unsigned copied,
  405. struct page *page, void *fsdata)
  406. {
  407. struct inode *inode = mapping->host;
  408. void *buffer;
  409. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  410. int err;
  411. buffer = kmap(page);
  412. err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
  413. kunmap(page);
  414. if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
  415. SetPageUptodate(page);
  416. /*
  417. * If err > 0, write_file has added err to pos, so we are comparing
  418. * i_size against the last byte written.
  419. */
  420. if (err > 0 && (pos > inode->i_size))
  421. inode->i_size = pos;
  422. unlock_page(page);
  423. page_cache_release(page);
  424. return err;
  425. }
  426. static const struct address_space_operations hostfs_aops = {
  427. .writepage = hostfs_writepage,
  428. .readpage = hostfs_readpage,
  429. .set_page_dirty = __set_page_dirty_nobuffers,
  430. .write_begin = hostfs_write_begin,
  431. .write_end = hostfs_write_end,
  432. };
  433. static int read_name(struct inode *ino, char *name)
  434. {
  435. dev_t rdev;
  436. struct hostfs_stat st;
  437. int err = stat_file(name, &st, -1);
  438. if (err)
  439. return err;
  440. /* Reencode maj and min with the kernel encoding.*/
  441. rdev = MKDEV(st.maj, st.min);
  442. switch (st.mode & S_IFMT) {
  443. case S_IFLNK:
  444. ino->i_op = &hostfs_link_iops;
  445. break;
  446. case S_IFDIR:
  447. ino->i_op = &hostfs_dir_iops;
  448. ino->i_fop = &hostfs_dir_fops;
  449. break;
  450. case S_IFCHR:
  451. case S_IFBLK:
  452. case S_IFIFO:
  453. case S_IFSOCK:
  454. init_special_inode(ino, st.mode & S_IFMT, rdev);
  455. ino->i_op = &hostfs_iops;
  456. break;
  457. case S_IFREG:
  458. ino->i_op = &hostfs_iops;
  459. ino->i_fop = &hostfs_file_fops;
  460. ino->i_mapping->a_ops = &hostfs_aops;
  461. break;
  462. default:
  463. return -EIO;
  464. }
  465. ino->i_ino = st.ino;
  466. ino->i_mode = st.mode;
  467. set_nlink(ino, st.nlink);
  468. i_uid_write(ino, st.uid);
  469. i_gid_write(ino, st.gid);
  470. ino->i_atime = st.atime;
  471. ino->i_mtime = st.mtime;
  472. ino->i_ctime = st.ctime;
  473. ino->i_size = st.size;
  474. ino->i_blocks = st.blocks;
  475. return 0;
  476. }
  477. static int hostfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  478. bool excl)
  479. {
  480. struct inode *inode;
  481. char *name;
  482. int error, fd;
  483. inode = hostfs_iget(dir->i_sb);
  484. if (IS_ERR(inode)) {
  485. error = PTR_ERR(inode);
  486. goto out;
  487. }
  488. error = -ENOMEM;
  489. name = dentry_name(dentry);
  490. if (name == NULL)
  491. goto out_put;
  492. fd = file_create(name, mode & S_IFMT);
  493. if (fd < 0)
  494. error = fd;
  495. else
  496. error = read_name(inode, name);
  497. __putname(name);
  498. if (error)
  499. goto out_put;
  500. HOSTFS_I(inode)->fd = fd;
  501. HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
  502. d_instantiate(dentry, inode);
  503. return 0;
  504. out_put:
  505. iput(inode);
  506. out:
  507. return error;
  508. }
  509. static struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
  510. unsigned int flags)
  511. {
  512. struct inode *inode;
  513. char *name;
  514. int err;
  515. inode = hostfs_iget(ino->i_sb);
  516. if (IS_ERR(inode)) {
  517. err = PTR_ERR(inode);
  518. goto out;
  519. }
  520. err = -ENOMEM;
  521. name = dentry_name(dentry);
  522. if (name == NULL)
  523. goto out_put;
  524. err = read_name(inode, name);
  525. __putname(name);
  526. if (err == -ENOENT) {
  527. iput(inode);
  528. inode = NULL;
  529. }
  530. else if (err)
  531. goto out_put;
  532. d_add(dentry, inode);
  533. return NULL;
  534. out_put:
  535. iput(inode);
  536. out:
  537. return ERR_PTR(err);
  538. }
  539. static int hostfs_link(struct dentry *to, struct inode *ino,
  540. struct dentry *from)
  541. {
  542. char *from_name, *to_name;
  543. int err;
  544. if ((from_name = dentry_name(from)) == NULL)
  545. return -ENOMEM;
  546. to_name = dentry_name(to);
  547. if (to_name == NULL) {
  548. __putname(from_name);
  549. return -ENOMEM;
  550. }
  551. err = link_file(to_name, from_name);
  552. __putname(from_name);
  553. __putname(to_name);
  554. return err;
  555. }
  556. static int hostfs_unlink(struct inode *ino, struct dentry *dentry)
  557. {
  558. char *file;
  559. int err;
  560. if (append)
  561. return -EPERM;
  562. if ((file = dentry_name(dentry)) == NULL)
  563. return -ENOMEM;
  564. err = unlink_file(file);
  565. __putname(file);
  566. return err;
  567. }
  568. static int hostfs_symlink(struct inode *ino, struct dentry *dentry,
  569. const char *to)
  570. {
  571. char *file;
  572. int err;
  573. if ((file = dentry_name(dentry)) == NULL)
  574. return -ENOMEM;
  575. err = make_symlink(file, to);
  576. __putname(file);
  577. return err;
  578. }
  579. static int hostfs_mkdir(struct inode *ino, struct dentry *dentry, umode_t mode)
  580. {
  581. char *file;
  582. int err;
  583. if ((file = dentry_name(dentry)) == NULL)
  584. return -ENOMEM;
  585. err = do_mkdir(file, mode);
  586. __putname(file);
  587. return err;
  588. }
  589. static int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
  590. {
  591. char *file;
  592. int err;
  593. if ((file = dentry_name(dentry)) == NULL)
  594. return -ENOMEM;
  595. err = do_rmdir(file);
  596. __putname(file);
  597. return err;
  598. }
  599. static int hostfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  600. {
  601. struct inode *inode;
  602. char *name;
  603. int err;
  604. inode = hostfs_iget(dir->i_sb);
  605. if (IS_ERR(inode)) {
  606. err = PTR_ERR(inode);
  607. goto out;
  608. }
  609. err = -ENOMEM;
  610. name = dentry_name(dentry);
  611. if (name == NULL)
  612. goto out_put;
  613. init_special_inode(inode, mode, dev);
  614. err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
  615. if (!err)
  616. goto out_free;
  617. err = read_name(inode, name);
  618. __putname(name);
  619. if (err)
  620. goto out_put;
  621. if (err)
  622. goto out_put;
  623. d_instantiate(dentry, inode);
  624. return 0;
  625. out_free:
  626. __putname(name);
  627. out_put:
  628. iput(inode);
  629. out:
  630. return err;
  631. }
  632. static int hostfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
  633. struct inode *new_dir, struct dentry *new_dentry,
  634. unsigned int flags)
  635. {
  636. char *old_name, *new_name;
  637. int err;
  638. if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE))
  639. return -EINVAL;
  640. old_name = dentry_name(old_dentry);
  641. if (old_name == NULL)
  642. return -ENOMEM;
  643. new_name = dentry_name(new_dentry);
  644. if (new_name == NULL) {
  645. __putname(old_name);
  646. return -ENOMEM;
  647. }
  648. if (!flags)
  649. err = rename_file(old_name, new_name);
  650. else
  651. err = rename2_file(old_name, new_name, flags);
  652. __putname(old_name);
  653. __putname(new_name);
  654. return err;
  655. }
  656. static int hostfs_permission(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(ino, desired);
  676. return err;
  677. }
  678. static int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
  679. {
  680. struct inode *inode = dentry->d_inode;
  681. struct hostfs_iattr attrs;
  682. char *name;
  683. int err;
  684. int fd = HOSTFS_I(inode)->fd;
  685. err = inode_change_ok(inode, attr);
  686. if (err)
  687. return err;
  688. if (append)
  689. attr->ia_valid &= ~ATTR_SIZE;
  690. attrs.ia_valid = 0;
  691. if (attr->ia_valid & ATTR_MODE) {
  692. attrs.ia_valid |= HOSTFS_ATTR_MODE;
  693. attrs.ia_mode = attr->ia_mode;
  694. }
  695. if (attr->ia_valid & ATTR_UID) {
  696. attrs.ia_valid |= HOSTFS_ATTR_UID;
  697. attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid);
  698. }
  699. if (attr->ia_valid & ATTR_GID) {
  700. attrs.ia_valid |= HOSTFS_ATTR_GID;
  701. attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid);
  702. }
  703. if (attr->ia_valid & ATTR_SIZE) {
  704. attrs.ia_valid |= HOSTFS_ATTR_SIZE;
  705. attrs.ia_size = attr->ia_size;
  706. }
  707. if (attr->ia_valid & ATTR_ATIME) {
  708. attrs.ia_valid |= HOSTFS_ATTR_ATIME;
  709. attrs.ia_atime = attr->ia_atime;
  710. }
  711. if (attr->ia_valid & ATTR_MTIME) {
  712. attrs.ia_valid |= HOSTFS_ATTR_MTIME;
  713. attrs.ia_mtime = attr->ia_mtime;
  714. }
  715. if (attr->ia_valid & ATTR_CTIME) {
  716. attrs.ia_valid |= HOSTFS_ATTR_CTIME;
  717. attrs.ia_ctime = attr->ia_ctime;
  718. }
  719. if (attr->ia_valid & ATTR_ATIME_SET) {
  720. attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
  721. }
  722. if (attr->ia_valid & ATTR_MTIME_SET) {
  723. attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
  724. }
  725. name = dentry_name(dentry);
  726. if (name == NULL)
  727. return -ENOMEM;
  728. err = set_attr(name, &attrs, fd);
  729. __putname(name);
  730. if (err)
  731. return err;
  732. if ((attr->ia_valid & ATTR_SIZE) &&
  733. attr->ia_size != i_size_read(inode))
  734. truncate_setsize(inode, attr->ia_size);
  735. setattr_copy(inode, attr);
  736. mark_inode_dirty(inode);
  737. return 0;
  738. }
  739. static const struct inode_operations hostfs_iops = {
  740. .permission = hostfs_permission,
  741. .setattr = hostfs_setattr,
  742. };
  743. static const struct inode_operations hostfs_dir_iops = {
  744. .create = hostfs_create,
  745. .lookup = hostfs_lookup,
  746. .link = hostfs_link,
  747. .unlink = hostfs_unlink,
  748. .symlink = hostfs_symlink,
  749. .mkdir = hostfs_mkdir,
  750. .rmdir = hostfs_rmdir,
  751. .mknod = hostfs_mknod,
  752. .rename2 = hostfs_rename2,
  753. .permission = hostfs_permission,
  754. .setattr = hostfs_setattr,
  755. };
  756. static void *hostfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  757. {
  758. char *link = __getname();
  759. if (link) {
  760. char *path = dentry_name(dentry);
  761. int err = -ENOMEM;
  762. if (path) {
  763. err = hostfs_do_readlink(path, link, PATH_MAX);
  764. if (err == PATH_MAX)
  765. err = -E2BIG;
  766. __putname(path);
  767. }
  768. if (err < 0) {
  769. __putname(link);
  770. link = ERR_PTR(err);
  771. }
  772. } else {
  773. link = ERR_PTR(-ENOMEM);
  774. }
  775. nd_set_link(nd, link);
  776. return NULL;
  777. }
  778. static void hostfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  779. {
  780. char *s = nd_get_link(nd);
  781. if (!IS_ERR(s))
  782. __putname(s);
  783. }
  784. static const struct inode_operations hostfs_link_iops = {
  785. .readlink = generic_readlink,
  786. .follow_link = hostfs_follow_link,
  787. .put_link = hostfs_put_link,
  788. };
  789. static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
  790. {
  791. struct inode *root_inode;
  792. char *host_root_path, *req_root = d;
  793. int err;
  794. sb->s_blocksize = 1024;
  795. sb->s_blocksize_bits = 10;
  796. sb->s_magic = HOSTFS_SUPER_MAGIC;
  797. sb->s_op = &hostfs_sbops;
  798. sb->s_d_op = &simple_dentry_operations;
  799. sb->s_maxbytes = MAX_LFS_FILESIZE;
  800. /* NULL is printed as <NULL> by sprintf: avoid that. */
  801. if (req_root == NULL)
  802. req_root = "";
  803. err = -ENOMEM;
  804. sb->s_fs_info = host_root_path =
  805. kmalloc(strlen(root_ino) + strlen(req_root) + 2, GFP_KERNEL);
  806. if (host_root_path == NULL)
  807. goto out;
  808. sprintf(host_root_path, "%s/%s", root_ino, req_root);
  809. root_inode = new_inode(sb);
  810. if (!root_inode)
  811. goto out;
  812. err = read_name(root_inode, host_root_path);
  813. if (err)
  814. goto out_put;
  815. if (S_ISLNK(root_inode->i_mode)) {
  816. char *name = follow_link(host_root_path);
  817. if (IS_ERR(name))
  818. err = PTR_ERR(name);
  819. else
  820. err = read_name(root_inode, name);
  821. kfree(name);
  822. if (err)
  823. goto out_put;
  824. }
  825. err = -ENOMEM;
  826. sb->s_root = d_make_root(root_inode);
  827. if (sb->s_root == NULL)
  828. goto out;
  829. return 0;
  830. out_put:
  831. iput(root_inode);
  832. out:
  833. return err;
  834. }
  835. static struct dentry *hostfs_read_sb(struct file_system_type *type,
  836. int flags, const char *dev_name,
  837. void *data)
  838. {
  839. return mount_nodev(type, flags, data, hostfs_fill_sb_common);
  840. }
  841. static void hostfs_kill_sb(struct super_block *s)
  842. {
  843. kill_anon_super(s);
  844. kfree(s->s_fs_info);
  845. }
  846. static struct file_system_type hostfs_type = {
  847. .owner = THIS_MODULE,
  848. .name = "hostfs",
  849. .mount = hostfs_read_sb,
  850. .kill_sb = hostfs_kill_sb,
  851. .fs_flags = 0,
  852. };
  853. MODULE_ALIAS_FS("hostfs");
  854. static int __init init_hostfs(void)
  855. {
  856. return register_filesystem(&hostfs_type);
  857. }
  858. static void __exit exit_hostfs(void)
  859. {
  860. unregister_filesystem(&hostfs_type);
  861. }
  862. module_init(init_hostfs)
  863. module_exit(exit_hostfs)
  864. MODULE_LICENSE("GPL");