hostfs_kern.c 21 KB

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