hostfs_kern.c 20 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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. };
  26. static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
  27. {
  28. return list_entry(inode, struct hostfs_inode_info, vfs_inode);
  29. }
  30. #define FILE_HOSTFS_I(file) HOSTFS_I(file_inode(file))
  31. /* Changed in hostfs_args before the kernel starts running */
  32. static char *root_ino = "";
  33. static int append = 0;
  34. static const struct inode_operations hostfs_iops;
  35. static const struct inode_operations hostfs_dir_iops;
  36. static const struct inode_operations hostfs_link_iops;
  37. #ifndef MODULE
  38. static int __init hostfs_args(char *options, int *add)
  39. {
  40. char *ptr;
  41. ptr = strchr(options, ',');
  42. if (ptr != NULL)
  43. *ptr++ = '\0';
  44. if (*options != '\0')
  45. root_ino = options;
  46. options = ptr;
  47. while (options) {
  48. ptr = strchr(options, ',');
  49. if (ptr != NULL)
  50. *ptr++ = '\0';
  51. if (*options != '\0') {
  52. if (!strcmp(options, "append"))
  53. append = 1;
  54. else printf("hostfs_args - unsupported option - %s\n",
  55. options);
  56. }
  57. options = ptr;
  58. }
  59. return 0;
  60. }
  61. __uml_setup("hostfs=", hostfs_args,
  62. "hostfs=<root dir>,<flags>,...\n"
  63. " This is used to set hostfs parameters. The root directory argument\n"
  64. " is used to confine all hostfs mounts to within the specified directory\n"
  65. " tree on the host. If this isn't specified, then a user inside UML can\n"
  66. " mount anything on the host that's accessible to the user that's running\n"
  67. " it.\n"
  68. " The only flag currently supported is 'append', which specifies that all\n"
  69. " files opened by hostfs will be opened in append mode.\n\n"
  70. );
  71. #endif
  72. static char *__dentry_name(struct dentry *dentry, char *name)
  73. {
  74. char *p = dentry_path_raw(dentry, name, PATH_MAX);
  75. char *root;
  76. size_t len;
  77. root = dentry->d_sb->s_fs_info;
  78. len = strlen(root);
  79. if (IS_ERR(p)) {
  80. __putname(name);
  81. return NULL;
  82. }
  83. strlcpy(name, root, PATH_MAX);
  84. if (len > p - name) {
  85. __putname(name);
  86. return NULL;
  87. }
  88. if (p > name + len) {
  89. char *s = name + len;
  90. while ((*s++ = *p++) != '\0')
  91. ;
  92. }
  93. return name;
  94. }
  95. static char *dentry_name(struct dentry *dentry)
  96. {
  97. char *name = __getname();
  98. if (!name)
  99. return NULL;
  100. return __dentry_name(dentry, name);
  101. }
  102. static char *inode_name(struct inode *ino)
  103. {
  104. struct dentry *dentry;
  105. char *name;
  106. dentry = d_find_alias(ino);
  107. if (!dentry)
  108. return NULL;
  109. name = dentry_name(dentry);
  110. dput(dentry);
  111. return name;
  112. }
  113. static char *follow_link(char *link)
  114. {
  115. int len, n;
  116. char *name, *resolved, *end;
  117. len = 64;
  118. while (1) {
  119. n = -ENOMEM;
  120. name = kmalloc(len, GFP_KERNEL);
  121. if (name == NULL)
  122. goto out;
  123. n = hostfs_do_readlink(link, name, len);
  124. if (n < len)
  125. break;
  126. len *= 2;
  127. kfree(name);
  128. }
  129. if (n < 0)
  130. goto out_free;
  131. if (*name == '/')
  132. return name;
  133. end = strrchr(link, '/');
  134. if (end == NULL)
  135. return name;
  136. *(end + 1) = '\0';
  137. len = strlen(link) + strlen(name) + 1;
  138. resolved = kmalloc(len, GFP_KERNEL);
  139. if (resolved == NULL) {
  140. n = -ENOMEM;
  141. goto out_free;
  142. }
  143. sprintf(resolved, "%s%s", link, name);
  144. kfree(name);
  145. kfree(link);
  146. return resolved;
  147. out_free:
  148. kfree(name);
  149. out:
  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. 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. return &hi->vfs_inode;
  196. }
  197. static void hostfs_evict_inode(struct inode *inode)
  198. {
  199. truncate_inode_pages(&inode->i_data, 0);
  200. clear_inode(inode);
  201. if (HOSTFS_I(inode)->fd != -1) {
  202. close_file(&HOSTFS_I(inode)->fd);
  203. HOSTFS_I(inode)->fd = -1;
  204. }
  205. }
  206. static void hostfs_i_callback(struct rcu_head *head)
  207. {
  208. struct inode *inode = container_of(head, struct inode, i_rcu);
  209. kfree(HOSTFS_I(inode));
  210. }
  211. static void hostfs_destroy_inode(struct inode *inode)
  212. {
  213. call_rcu(&inode->i_rcu, hostfs_i_callback);
  214. }
  215. static int hostfs_show_options(struct seq_file *seq, struct dentry *root)
  216. {
  217. const char *root_path = root->d_sb->s_fs_info;
  218. size_t offset = strlen(root_ino) + 1;
  219. if (strlen(root_path) > offset)
  220. seq_printf(seq, ",%s", root_path + offset);
  221. return 0;
  222. }
  223. static const struct super_operations hostfs_sbops = {
  224. .alloc_inode = hostfs_alloc_inode,
  225. .destroy_inode = hostfs_destroy_inode,
  226. .evict_inode = hostfs_evict_inode,
  227. .statfs = hostfs_statfs,
  228. .show_options = hostfs_show_options,
  229. };
  230. int hostfs_readdir(struct file *file, struct dir_context *ctx)
  231. {
  232. void *dir;
  233. char *name;
  234. unsigned long long next, ino;
  235. int error, len;
  236. unsigned int type;
  237. name = dentry_name(file->f_path.dentry);
  238. if (name == NULL)
  239. return -ENOMEM;
  240. dir = open_dir(name, &error);
  241. __putname(name);
  242. if (dir == NULL)
  243. return -error;
  244. next = ctx->pos;
  245. while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) {
  246. if (!dir_emit(ctx, name, len, ino, type))
  247. break;
  248. ctx->pos = next;
  249. }
  250. close_dir(dir);
  251. return 0;
  252. }
  253. int hostfs_file_open(struct inode *ino, struct file *file)
  254. {
  255. static DEFINE_MUTEX(open_mutex);
  256. char *name;
  257. fmode_t mode = 0;
  258. int err;
  259. int r = 0, w = 0, fd;
  260. mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
  261. if ((mode & HOSTFS_I(ino)->mode) == mode)
  262. return 0;
  263. mode |= HOSTFS_I(ino)->mode;
  264. retry:
  265. if (mode & FMODE_READ)
  266. r = 1;
  267. if (mode & FMODE_WRITE)
  268. w = 1;
  269. if (w)
  270. r = 1;
  271. name = dentry_name(file->f_path.dentry);
  272. if (name == NULL)
  273. return -ENOMEM;
  274. fd = open_file(name, r, w, append);
  275. __putname(name);
  276. if (fd < 0)
  277. return fd;
  278. mutex_lock(&open_mutex);
  279. /* somebody else had handled it first? */
  280. if ((mode & HOSTFS_I(ino)->mode) == mode) {
  281. mutex_unlock(&open_mutex);
  282. return 0;
  283. }
  284. if ((mode | HOSTFS_I(ino)->mode) != mode) {
  285. mode |= HOSTFS_I(ino)->mode;
  286. mutex_unlock(&open_mutex);
  287. close_file(&fd);
  288. goto retry;
  289. }
  290. if (HOSTFS_I(ino)->fd == -1) {
  291. HOSTFS_I(ino)->fd = fd;
  292. } else {
  293. err = replace_file(fd, HOSTFS_I(ino)->fd);
  294. close_file(&fd);
  295. if (err < 0) {
  296. mutex_unlock(&open_mutex);
  297. return err;
  298. }
  299. }
  300. HOSTFS_I(ino)->mode = mode;
  301. mutex_unlock(&open_mutex);
  302. return 0;
  303. }
  304. static int hostfs_file_release(struct inode *inode, struct file *file)
  305. {
  306. filemap_write_and_wait(inode->i_mapping);
  307. return 0;
  308. }
  309. int hostfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  310. {
  311. struct inode *inode = file->f_mapping->host;
  312. int ret;
  313. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  314. if (ret)
  315. return ret;
  316. mutex_lock(&inode->i_mutex);
  317. ret = fsync_file(HOSTFS_I(inode)->fd, datasync);
  318. mutex_unlock(&inode->i_mutex);
  319. return ret;
  320. }
  321. static const struct file_operations hostfs_file_fops = {
  322. .llseek = generic_file_llseek,
  323. .read = do_sync_read,
  324. .splice_read = generic_file_splice_read,
  325. .aio_read = generic_file_aio_read,
  326. .aio_write = generic_file_aio_write,
  327. .write = do_sync_write,
  328. .mmap = generic_file_mmap,
  329. .open = hostfs_file_open,
  330. .release = hostfs_file_release,
  331. .fsync = hostfs_fsync,
  332. };
  333. static const struct file_operations hostfs_dir_fops = {
  334. .llseek = generic_file_llseek,
  335. .iterate = hostfs_readdir,
  336. .read = generic_read_dir,
  337. };
  338. int hostfs_writepage(struct page *page, struct writeback_control *wbc)
  339. {
  340. struct address_space *mapping = page->mapping;
  341. struct inode *inode = mapping->host;
  342. char *buffer;
  343. unsigned long long base;
  344. int count = PAGE_CACHE_SIZE;
  345. int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
  346. int err;
  347. if (page->index >= end_index)
  348. count = inode->i_size & (PAGE_CACHE_SIZE-1);
  349. buffer = kmap(page);
  350. base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
  351. err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
  352. if (err != count) {
  353. ClearPageUptodate(page);
  354. goto out;
  355. }
  356. if (base > inode->i_size)
  357. inode->i_size = base;
  358. if (PageError(page))
  359. ClearPageError(page);
  360. err = 0;
  361. out:
  362. kunmap(page);
  363. unlock_page(page);
  364. return err;
  365. }
  366. int hostfs_readpage(struct file *file, struct page *page)
  367. {
  368. char *buffer;
  369. long long start;
  370. int err = 0;
  371. start = (long long) page->index << PAGE_CACHE_SHIFT;
  372. buffer = kmap(page);
  373. err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
  374. PAGE_CACHE_SIZE);
  375. if (err < 0)
  376. goto out;
  377. memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
  378. flush_dcache_page(page);
  379. SetPageUptodate(page);
  380. if (PageError(page)) ClearPageError(page);
  381. err = 0;
  382. out:
  383. kunmap(page);
  384. unlock_page(page);
  385. return err;
  386. }
  387. int hostfs_write_begin(struct file *file, struct address_space *mapping,
  388. loff_t pos, unsigned len, unsigned flags,
  389. struct page **pagep, void **fsdata)
  390. {
  391. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  392. *pagep = grab_cache_page_write_begin(mapping, index, flags);
  393. if (!*pagep)
  394. return -ENOMEM;
  395. return 0;
  396. }
  397. int hostfs_write_end(struct file *file, struct address_space *mapping,
  398. loff_t pos, unsigned len, unsigned copied,
  399. struct page *page, void *fsdata)
  400. {
  401. struct inode *inode = mapping->host;
  402. void *buffer;
  403. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  404. int err;
  405. buffer = kmap(page);
  406. err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
  407. kunmap(page);
  408. if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
  409. SetPageUptodate(page);
  410. /*
  411. * If err > 0, write_file has added err to pos, so we are comparing
  412. * i_size against the last byte written.
  413. */
  414. if (err > 0 && (pos > inode->i_size))
  415. inode->i_size = pos;
  416. unlock_page(page);
  417. page_cache_release(page);
  418. return err;
  419. }
  420. static const struct address_space_operations hostfs_aops = {
  421. .writepage = hostfs_writepage,
  422. .readpage = hostfs_readpage,
  423. .set_page_dirty = __set_page_dirty_nobuffers,
  424. .write_begin = hostfs_write_begin,
  425. .write_end = hostfs_write_end,
  426. };
  427. static int read_name(struct inode *ino, char *name)
  428. {
  429. dev_t rdev;
  430. struct hostfs_stat st;
  431. int err = stat_file(name, &st, -1);
  432. if (err)
  433. return err;
  434. /* Reencode maj and min with the kernel encoding.*/
  435. rdev = MKDEV(st.maj, st.min);
  436. switch (st.mode & S_IFMT) {
  437. case S_IFLNK:
  438. ino->i_op = &hostfs_link_iops;
  439. break;
  440. case S_IFDIR:
  441. ino->i_op = &hostfs_dir_iops;
  442. ino->i_fop = &hostfs_dir_fops;
  443. break;
  444. case S_IFCHR:
  445. case S_IFBLK:
  446. case S_IFIFO:
  447. case S_IFSOCK:
  448. init_special_inode(ino, st.mode & S_IFMT, rdev);
  449. ino->i_op = &hostfs_iops;
  450. break;
  451. default:
  452. ino->i_op = &hostfs_iops;
  453. ino->i_fop = &hostfs_file_fops;
  454. ino->i_mapping->a_ops = &hostfs_aops;
  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 = st.atime;
  462. ino->i_mtime = st.mtime;
  463. ino->i_ctime = st.ctime;
  464. ino->i_size = st.size;
  465. ino->i_blocks = st.blocks;
  466. return 0;
  467. }
  468. 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,
  484. mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
  485. mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
  486. mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
  487. if (fd < 0)
  488. error = fd;
  489. else
  490. error = read_name(inode, name);
  491. __putname(name);
  492. if (error)
  493. goto out_put;
  494. HOSTFS_I(inode)->fd = fd;
  495. HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
  496. d_instantiate(dentry, inode);
  497. return 0;
  498. out_put:
  499. iput(inode);
  500. out:
  501. return error;
  502. }
  503. struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
  504. unsigned int flags)
  505. {
  506. struct inode *inode;
  507. char *name;
  508. int err;
  509. inode = hostfs_iget(ino->i_sb);
  510. if (IS_ERR(inode)) {
  511. err = PTR_ERR(inode);
  512. goto out;
  513. }
  514. err = -ENOMEM;
  515. name = dentry_name(dentry);
  516. if (name == NULL)
  517. goto out_put;
  518. err = read_name(inode, name);
  519. __putname(name);
  520. if (err == -ENOENT) {
  521. iput(inode);
  522. inode = NULL;
  523. }
  524. else if (err)
  525. goto out_put;
  526. d_add(dentry, inode);
  527. return NULL;
  528. out_put:
  529. iput(inode);
  530. out:
  531. return ERR_PTR(err);
  532. }
  533. int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
  534. {
  535. char *from_name, *to_name;
  536. int err;
  537. if ((from_name = dentry_name(from)) == NULL)
  538. return -ENOMEM;
  539. to_name = dentry_name(to);
  540. if (to_name == NULL) {
  541. __putname(from_name);
  542. return -ENOMEM;
  543. }
  544. err = link_file(to_name, from_name);
  545. __putname(from_name);
  546. __putname(to_name);
  547. return err;
  548. }
  549. int hostfs_unlink(struct inode *ino, struct dentry *dentry)
  550. {
  551. char *file;
  552. int err;
  553. if (append)
  554. return -EPERM;
  555. if ((file = dentry_name(dentry)) == NULL)
  556. return -ENOMEM;
  557. err = unlink_file(file);
  558. __putname(file);
  559. return err;
  560. }
  561. int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
  562. {
  563. char *file;
  564. int err;
  565. if ((file = dentry_name(dentry)) == NULL)
  566. return -ENOMEM;
  567. err = make_symlink(file, to);
  568. __putname(file);
  569. return err;
  570. }
  571. int hostfs_mkdir(struct inode *ino, struct dentry *dentry, umode_t mode)
  572. {
  573. char *file;
  574. int err;
  575. if ((file = dentry_name(dentry)) == NULL)
  576. return -ENOMEM;
  577. err = do_mkdir(file, mode);
  578. __putname(file);
  579. return err;
  580. }
  581. int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
  582. {
  583. char *file;
  584. int err;
  585. if ((file = dentry_name(dentry)) == NULL)
  586. return -ENOMEM;
  587. err = do_rmdir(file);
  588. __putname(file);
  589. return err;
  590. }
  591. static int hostfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  592. {
  593. struct inode *inode;
  594. char *name;
  595. int err;
  596. inode = hostfs_iget(dir->i_sb);
  597. if (IS_ERR(inode)) {
  598. err = PTR_ERR(inode);
  599. goto out;
  600. }
  601. err = -ENOMEM;
  602. name = dentry_name(dentry);
  603. if (name == NULL)
  604. goto out_put;
  605. init_special_inode(inode, mode, dev);
  606. err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
  607. if (!err)
  608. goto out_free;
  609. err = read_name(inode, name);
  610. __putname(name);
  611. if (err)
  612. goto out_put;
  613. if (err)
  614. goto out_put;
  615. d_instantiate(dentry, inode);
  616. return 0;
  617. out_free:
  618. __putname(name);
  619. out_put:
  620. iput(inode);
  621. out:
  622. return err;
  623. }
  624. int hostfs_rename(struct inode *from_ino, struct dentry *from,
  625. struct inode *to_ino, struct dentry *to)
  626. {
  627. char *from_name, *to_name;
  628. int err;
  629. if ((from_name = dentry_name(from)) == NULL)
  630. return -ENOMEM;
  631. if ((to_name = dentry_name(to)) == NULL) {
  632. __putname(from_name);
  633. return -ENOMEM;
  634. }
  635. err = rename_file(from_name, to_name);
  636. __putname(from_name);
  637. __putname(to_name);
  638. return err;
  639. }
  640. int hostfs_permission(struct inode *ino, int desired)
  641. {
  642. char *name;
  643. int r = 0, w = 0, x = 0, err;
  644. if (desired & MAY_NOT_BLOCK)
  645. return -ECHILD;
  646. if (desired & MAY_READ) r = 1;
  647. if (desired & MAY_WRITE) w = 1;
  648. if (desired & MAY_EXEC) x = 1;
  649. name = inode_name(ino);
  650. if (name == NULL)
  651. return -ENOMEM;
  652. if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
  653. S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
  654. err = 0;
  655. else
  656. err = access_file(name, r, w, x);
  657. __putname(name);
  658. if (!err)
  659. err = generic_permission(ino, desired);
  660. return err;
  661. }
  662. int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
  663. {
  664. struct inode *inode = dentry->d_inode;
  665. struct hostfs_iattr attrs;
  666. char *name;
  667. int err;
  668. int fd = HOSTFS_I(inode)->fd;
  669. err = inode_change_ok(inode, attr);
  670. if (err)
  671. return err;
  672. if (append)
  673. attr->ia_valid &= ~ATTR_SIZE;
  674. attrs.ia_valid = 0;
  675. if (attr->ia_valid & ATTR_MODE) {
  676. attrs.ia_valid |= HOSTFS_ATTR_MODE;
  677. attrs.ia_mode = attr->ia_mode;
  678. }
  679. if (attr->ia_valid & ATTR_UID) {
  680. attrs.ia_valid |= HOSTFS_ATTR_UID;
  681. attrs.ia_uid = from_kuid(&init_user_ns, attr->ia_uid);
  682. }
  683. if (attr->ia_valid & ATTR_GID) {
  684. attrs.ia_valid |= HOSTFS_ATTR_GID;
  685. attrs.ia_gid = from_kgid(&init_user_ns, attr->ia_gid);
  686. }
  687. if (attr->ia_valid & ATTR_SIZE) {
  688. attrs.ia_valid |= HOSTFS_ATTR_SIZE;
  689. attrs.ia_size = attr->ia_size;
  690. }
  691. if (attr->ia_valid & ATTR_ATIME) {
  692. attrs.ia_valid |= HOSTFS_ATTR_ATIME;
  693. attrs.ia_atime = attr->ia_atime;
  694. }
  695. if (attr->ia_valid & ATTR_MTIME) {
  696. attrs.ia_valid |= HOSTFS_ATTR_MTIME;
  697. attrs.ia_mtime = attr->ia_mtime;
  698. }
  699. if (attr->ia_valid & ATTR_CTIME) {
  700. attrs.ia_valid |= HOSTFS_ATTR_CTIME;
  701. attrs.ia_ctime = attr->ia_ctime;
  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_rename,
  737. .permission = hostfs_permission,
  738. .setattr = hostfs_setattr,
  739. };
  740. static void *hostfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  741. {
  742. char *link = __getname();
  743. if (link) {
  744. char *path = dentry_name(dentry);
  745. int err = -ENOMEM;
  746. if (path) {
  747. err = hostfs_do_readlink(path, link, PATH_MAX);
  748. if (err == PATH_MAX)
  749. err = -E2BIG;
  750. __putname(path);
  751. }
  752. if (err < 0) {
  753. __putname(link);
  754. link = ERR_PTR(err);
  755. }
  756. } else {
  757. link = ERR_PTR(-ENOMEM);
  758. }
  759. nd_set_link(nd, link);
  760. return NULL;
  761. }
  762. static void hostfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  763. {
  764. char *s = nd_get_link(nd);
  765. if (!IS_ERR(s))
  766. __putname(s);
  767. }
  768. static const struct inode_operations hostfs_link_iops = {
  769. .readlink = generic_readlink,
  770. .follow_link = hostfs_follow_link,
  771. .put_link = hostfs_put_link,
  772. };
  773. static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
  774. {
  775. struct inode *root_inode;
  776. char *host_root_path, *req_root = d;
  777. int err;
  778. sb->s_blocksize = 1024;
  779. sb->s_blocksize_bits = 10;
  780. sb->s_magic = HOSTFS_SUPER_MAGIC;
  781. sb->s_op = &hostfs_sbops;
  782. sb->s_d_op = &simple_dentry_operations;
  783. sb->s_maxbytes = MAX_LFS_FILESIZE;
  784. /* NULL is printed as <NULL> by sprintf: avoid that. */
  785. if (req_root == NULL)
  786. req_root = "";
  787. err = -ENOMEM;
  788. sb->s_fs_info = host_root_path =
  789. kmalloc(strlen(root_ino) + strlen(req_root) + 2, GFP_KERNEL);
  790. if (host_root_path == NULL)
  791. goto out;
  792. sprintf(host_root_path, "%s/%s", root_ino, req_root);
  793. root_inode = new_inode(sb);
  794. if (!root_inode)
  795. goto out;
  796. err = read_name(root_inode, host_root_path);
  797. if (err)
  798. goto out_put;
  799. if (S_ISLNK(root_inode->i_mode)) {
  800. char *name = follow_link(host_root_path);
  801. if (IS_ERR(name))
  802. err = PTR_ERR(name);
  803. else
  804. err = read_name(root_inode, name);
  805. kfree(name);
  806. if (err)
  807. goto out_put;
  808. }
  809. err = -ENOMEM;
  810. sb->s_root = d_make_root(root_inode);
  811. if (sb->s_root == NULL)
  812. goto out;
  813. return 0;
  814. out_put:
  815. iput(root_inode);
  816. out:
  817. return err;
  818. }
  819. static struct dentry *hostfs_read_sb(struct file_system_type *type,
  820. int flags, const char *dev_name,
  821. void *data)
  822. {
  823. return mount_nodev(type, flags, data, hostfs_fill_sb_common);
  824. }
  825. static void hostfs_kill_sb(struct super_block *s)
  826. {
  827. kill_anon_super(s);
  828. kfree(s->s_fs_info);
  829. }
  830. static struct file_system_type hostfs_type = {
  831. .owner = THIS_MODULE,
  832. .name = "hostfs",
  833. .mount = hostfs_read_sb,
  834. .kill_sb = hostfs_kill_sb,
  835. .fs_flags = 0,
  836. };
  837. MODULE_ALIAS_FS("hostfs");
  838. static int __init init_hostfs(void)
  839. {
  840. return register_filesystem(&hostfs_type);
  841. }
  842. static void __exit exit_hostfs(void)
  843. {
  844. unregister_filesystem(&hostfs_type);
  845. }
  846. module_init(init_hostfs)
  847. module_exit(exit_hostfs)
  848. MODULE_LICENSE("GPL");