hostfs_kern.c 20 KB

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