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. static int hostfs_d_delete(const struct dentry *dentry)
  32. {
  33. return 1;
  34. }
  35. static const struct dentry_operations hostfs_dentry_ops = {
  36. .d_delete = hostfs_d_delete,
  37. };
  38. /* Changed in hostfs_args before the kernel starts running */
  39. static char *root_ino = "";
  40. static int append = 0;
  41. static const struct inode_operations hostfs_iops;
  42. static const struct inode_operations hostfs_dir_iops;
  43. static const struct inode_operations hostfs_link_iops;
  44. #ifndef MODULE
  45. static int __init hostfs_args(char *options, int *add)
  46. {
  47. char *ptr;
  48. ptr = strchr(options, ',');
  49. if (ptr != NULL)
  50. *ptr++ = '\0';
  51. if (*options != '\0')
  52. root_ino = options;
  53. options = ptr;
  54. while (options) {
  55. ptr = strchr(options, ',');
  56. if (ptr != NULL)
  57. *ptr++ = '\0';
  58. if (*options != '\0') {
  59. if (!strcmp(options, "append"))
  60. append = 1;
  61. else printf("hostfs_args - unsupported option - %s\n",
  62. options);
  63. }
  64. options = ptr;
  65. }
  66. return 0;
  67. }
  68. __uml_setup("hostfs=", hostfs_args,
  69. "hostfs=<root dir>,<flags>,...\n"
  70. " This is used to set hostfs parameters. The root directory argument\n"
  71. " is used to confine all hostfs mounts to within the specified directory\n"
  72. " tree on the host. If this isn't specified, then a user inside UML can\n"
  73. " mount anything on the host that's accessible to the user that's running\n"
  74. " it.\n"
  75. " The only flag currently supported is 'append', which specifies that all\n"
  76. " files opened by hostfs will be opened in append mode.\n\n"
  77. );
  78. #endif
  79. static char *__dentry_name(struct dentry *dentry, char *name)
  80. {
  81. char *p = dentry_path_raw(dentry, name, PATH_MAX);
  82. char *root;
  83. size_t len;
  84. root = dentry->d_sb->s_fs_info;
  85. len = strlen(root);
  86. if (IS_ERR(p)) {
  87. __putname(name);
  88. return NULL;
  89. }
  90. strlcpy(name, root, PATH_MAX);
  91. if (len > p - name) {
  92. __putname(name);
  93. return NULL;
  94. }
  95. if (p > name + len) {
  96. char *s = name + len;
  97. while ((*s++ = *p++) != '\0')
  98. ;
  99. }
  100. return name;
  101. }
  102. static char *dentry_name(struct dentry *dentry)
  103. {
  104. char *name = __getname();
  105. if (!name)
  106. return NULL;
  107. return __dentry_name(dentry, name);
  108. }
  109. static char *inode_name(struct inode *ino)
  110. {
  111. struct dentry *dentry;
  112. char *name;
  113. dentry = d_find_alias(ino);
  114. if (!dentry)
  115. return NULL;
  116. name = dentry_name(dentry);
  117. dput(dentry);
  118. return name;
  119. }
  120. static char *follow_link(char *link)
  121. {
  122. int len, n;
  123. char *name, *resolved, *end;
  124. len = 64;
  125. while (1) {
  126. n = -ENOMEM;
  127. name = kmalloc(len, GFP_KERNEL);
  128. if (name == NULL)
  129. goto out;
  130. n = hostfs_do_readlink(link, name, len);
  131. if (n < len)
  132. break;
  133. len *= 2;
  134. kfree(name);
  135. }
  136. if (n < 0)
  137. goto out_free;
  138. if (*name == '/')
  139. return name;
  140. end = strrchr(link, '/');
  141. if (end == NULL)
  142. return name;
  143. *(end + 1) = '\0';
  144. len = strlen(link) + strlen(name) + 1;
  145. resolved = kmalloc(len, GFP_KERNEL);
  146. if (resolved == NULL) {
  147. n = -ENOMEM;
  148. goto out_free;
  149. }
  150. sprintf(resolved, "%s%s", link, name);
  151. kfree(name);
  152. kfree(link);
  153. return resolved;
  154. out_free:
  155. kfree(name);
  156. out:
  157. return ERR_PTR(n);
  158. }
  159. static struct inode *hostfs_iget(struct super_block *sb)
  160. {
  161. struct inode *inode = new_inode(sb);
  162. if (!inode)
  163. return ERR_PTR(-ENOMEM);
  164. return inode;
  165. }
  166. int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
  167. {
  168. /*
  169. * do_statfs uses struct statfs64 internally, but the linux kernel
  170. * struct statfs still has 32-bit versions for most of these fields,
  171. * so we convert them here
  172. */
  173. int err;
  174. long long f_blocks;
  175. long long f_bfree;
  176. long long f_bavail;
  177. long long f_files;
  178. long long f_ffree;
  179. err = do_statfs(dentry->d_sb->s_fs_info,
  180. &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
  181. &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
  182. &sf->f_namelen);
  183. if (err)
  184. return err;
  185. sf->f_blocks = f_blocks;
  186. sf->f_bfree = f_bfree;
  187. sf->f_bavail = f_bavail;
  188. sf->f_files = f_files;
  189. sf->f_ffree = f_ffree;
  190. sf->f_type = HOSTFS_SUPER_MAGIC;
  191. return 0;
  192. }
  193. static struct inode *hostfs_alloc_inode(struct super_block *sb)
  194. {
  195. struct hostfs_inode_info *hi;
  196. hi = kmalloc(sizeof(*hi), GFP_KERNEL);
  197. if (hi == NULL)
  198. return NULL;
  199. hi->fd = -1;
  200. hi->mode = 0;
  201. inode_init_once(&hi->vfs_inode);
  202. return &hi->vfs_inode;
  203. }
  204. static void hostfs_evict_inode(struct inode *inode)
  205. {
  206. truncate_inode_pages(&inode->i_data, 0);
  207. clear_inode(inode);
  208. if (HOSTFS_I(inode)->fd != -1) {
  209. close_file(&HOSTFS_I(inode)->fd);
  210. HOSTFS_I(inode)->fd = -1;
  211. }
  212. }
  213. static void hostfs_i_callback(struct rcu_head *head)
  214. {
  215. struct inode *inode = container_of(head, struct inode, i_rcu);
  216. kfree(HOSTFS_I(inode));
  217. }
  218. static void hostfs_destroy_inode(struct inode *inode)
  219. {
  220. call_rcu(&inode->i_rcu, hostfs_i_callback);
  221. }
  222. static int hostfs_show_options(struct seq_file *seq, struct dentry *root)
  223. {
  224. const char *root_path = root->d_sb->s_fs_info;
  225. size_t offset = strlen(root_ino) + 1;
  226. if (strlen(root_path) > offset)
  227. seq_printf(seq, ",%s", root_path + offset);
  228. return 0;
  229. }
  230. static const struct super_operations hostfs_sbops = {
  231. .alloc_inode = hostfs_alloc_inode,
  232. .destroy_inode = hostfs_destroy_inode,
  233. .evict_inode = hostfs_evict_inode,
  234. .statfs = hostfs_statfs,
  235. .show_options = hostfs_show_options,
  236. };
  237. int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
  238. {
  239. void *dir;
  240. char *name;
  241. unsigned long long next, ino;
  242. int error, len;
  243. unsigned int type;
  244. name = dentry_name(file->f_path.dentry);
  245. if (name == NULL)
  246. return -ENOMEM;
  247. dir = open_dir(name, &error);
  248. __putname(name);
  249. if (dir == NULL)
  250. return -error;
  251. next = file->f_pos;
  252. while ((name = read_dir(dir, &next, &ino, &len, &type)) != NULL) {
  253. error = (*filldir)(ent, name, len, file->f_pos,
  254. ino, type);
  255. if (error) break;
  256. file->f_pos = next;
  257. }
  258. close_dir(dir);
  259. return 0;
  260. }
  261. int hostfs_file_open(struct inode *ino, struct file *file)
  262. {
  263. static DEFINE_MUTEX(open_mutex);
  264. char *name;
  265. fmode_t mode = 0;
  266. int err;
  267. int r = 0, w = 0, fd;
  268. mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
  269. if ((mode & HOSTFS_I(ino)->mode) == mode)
  270. return 0;
  271. mode |= HOSTFS_I(ino)->mode;
  272. retry:
  273. if (mode & FMODE_READ)
  274. r = 1;
  275. if (mode & FMODE_WRITE)
  276. w = 1;
  277. if (w)
  278. r = 1;
  279. name = dentry_name(file->f_path.dentry);
  280. if (name == NULL)
  281. return -ENOMEM;
  282. fd = open_file(name, r, w, append);
  283. __putname(name);
  284. if (fd < 0)
  285. return fd;
  286. mutex_lock(&open_mutex);
  287. /* somebody else had handled it first? */
  288. if ((mode & HOSTFS_I(ino)->mode) == mode) {
  289. mutex_unlock(&open_mutex);
  290. return 0;
  291. }
  292. if ((mode | HOSTFS_I(ino)->mode) != mode) {
  293. mode |= HOSTFS_I(ino)->mode;
  294. mutex_unlock(&open_mutex);
  295. close_file(&fd);
  296. goto retry;
  297. }
  298. if (HOSTFS_I(ino)->fd == -1) {
  299. HOSTFS_I(ino)->fd = fd;
  300. } else {
  301. err = replace_file(fd, HOSTFS_I(ino)->fd);
  302. close_file(&fd);
  303. if (err < 0) {
  304. mutex_unlock(&open_mutex);
  305. return err;
  306. }
  307. }
  308. HOSTFS_I(ino)->mode = mode;
  309. mutex_unlock(&open_mutex);
  310. return 0;
  311. }
  312. int hostfs_fsync(struct file *file, loff_t start, loff_t end, 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 = do_sync_read,
  327. .splice_read = generic_file_splice_read,
  328. .aio_read = generic_file_aio_read,
  329. .aio_write = generic_file_aio_write,
  330. .write = do_sync_write,
  331. .mmap = generic_file_mmap,
  332. .open = hostfs_file_open,
  333. .release = NULL,
  334. .fsync = hostfs_fsync,
  335. };
  336. static const struct file_operations hostfs_dir_fops = {
  337. .llseek = generic_file_llseek,
  338. .readdir = hostfs_readdir,
  339. .read = generic_read_dir,
  340. };
  341. int hostfs_writepage(struct page *page, struct writeback_control *wbc)
  342. {
  343. struct address_space *mapping = page->mapping;
  344. struct inode *inode = mapping->host;
  345. char *buffer;
  346. unsigned long long base;
  347. int count = PAGE_CACHE_SIZE;
  348. int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
  349. int err;
  350. if (page->index >= end_index)
  351. count = inode->i_size & (PAGE_CACHE_SIZE-1);
  352. buffer = kmap(page);
  353. base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
  354. err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
  355. if (err != count) {
  356. ClearPageUptodate(page);
  357. goto out;
  358. }
  359. if (base > inode->i_size)
  360. inode->i_size = base;
  361. if (PageError(page))
  362. ClearPageError(page);
  363. err = 0;
  364. out:
  365. kunmap(page);
  366. unlock_page(page);
  367. return err;
  368. }
  369. int hostfs_readpage(struct file *file, struct page *page)
  370. {
  371. char *buffer;
  372. long long start;
  373. int err = 0;
  374. start = (long long) page->index << PAGE_CACHE_SHIFT;
  375. buffer = kmap(page);
  376. err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
  377. PAGE_CACHE_SIZE);
  378. if (err < 0)
  379. goto out;
  380. memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
  381. flush_dcache_page(page);
  382. SetPageUptodate(page);
  383. if (PageError(page)) ClearPageError(page);
  384. err = 0;
  385. out:
  386. kunmap(page);
  387. unlock_page(page);
  388. return err;
  389. }
  390. int hostfs_write_begin(struct file *file, struct address_space *mapping,
  391. loff_t pos, unsigned len, unsigned flags,
  392. struct page **pagep, void **fsdata)
  393. {
  394. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  395. *pagep = grab_cache_page_write_begin(mapping, index, flags);
  396. if (!*pagep)
  397. return -ENOMEM;
  398. return 0;
  399. }
  400. int hostfs_write_end(struct file *file, struct address_space *mapping,
  401. loff_t pos, unsigned len, unsigned copied,
  402. struct page *page, void *fsdata)
  403. {
  404. struct inode *inode = mapping->host;
  405. void *buffer;
  406. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  407. int err;
  408. buffer = kmap(page);
  409. err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
  410. kunmap(page);
  411. if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
  412. SetPageUptodate(page);
  413. /*
  414. * If err > 0, write_file has added err to pos, so we are comparing
  415. * i_size against the last byte written.
  416. */
  417. if (err > 0 && (pos > inode->i_size))
  418. inode->i_size = pos;
  419. unlock_page(page);
  420. page_cache_release(page);
  421. return err;
  422. }
  423. static const struct address_space_operations hostfs_aops = {
  424. .writepage = hostfs_writepage,
  425. .readpage = hostfs_readpage,
  426. .set_page_dirty = __set_page_dirty_nobuffers,
  427. .write_begin = hostfs_write_begin,
  428. .write_end = hostfs_write_end,
  429. };
  430. static int read_name(struct inode *ino, char *name)
  431. {
  432. dev_t rdev;
  433. struct hostfs_stat st;
  434. int err = stat_file(name, &st, -1);
  435. if (err)
  436. return err;
  437. /* Reencode maj and min with the kernel encoding.*/
  438. rdev = MKDEV(st.maj, st.min);
  439. switch (st.mode & S_IFMT) {
  440. case S_IFLNK:
  441. ino->i_op = &hostfs_link_iops;
  442. break;
  443. case S_IFDIR:
  444. ino->i_op = &hostfs_dir_iops;
  445. ino->i_fop = &hostfs_dir_fops;
  446. break;
  447. case S_IFCHR:
  448. case S_IFBLK:
  449. case S_IFIFO:
  450. case S_IFSOCK:
  451. init_special_inode(ino, st.mode & S_IFMT, rdev);
  452. ino->i_op = &hostfs_iops;
  453. break;
  454. default:
  455. ino->i_op = &hostfs_iops;
  456. ino->i_fop = &hostfs_file_fops;
  457. ino->i_mapping->a_ops = &hostfs_aops;
  458. }
  459. ino->i_ino = st.ino;
  460. ino->i_mode = st.mode;
  461. set_nlink(ino, st.nlink);
  462. i_uid_write(ino, st.uid);
  463. i_gid_write(ino, st.gid);
  464. ino->i_atime = st.atime;
  465. ino->i_mtime = st.mtime;
  466. ino->i_ctime = st.ctime;
  467. ino->i_size = st.size;
  468. ino->i_blocks = st.blocks;
  469. return 0;
  470. }
  471. int hostfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  472. bool excl)
  473. {
  474. struct inode *inode;
  475. char *name;
  476. int error, fd;
  477. inode = hostfs_iget(dir->i_sb);
  478. if (IS_ERR(inode)) {
  479. error = PTR_ERR(inode);
  480. goto out;
  481. }
  482. error = -ENOMEM;
  483. name = dentry_name(dentry);
  484. if (name == NULL)
  485. goto out_put;
  486. fd = file_create(name,
  487. mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
  488. mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
  489. mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
  490. if (fd < 0)
  491. error = fd;
  492. else
  493. error = read_name(inode, name);
  494. __putname(name);
  495. if (error)
  496. goto out_put;
  497. HOSTFS_I(inode)->fd = fd;
  498. HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
  499. d_instantiate(dentry, inode);
  500. return 0;
  501. out_put:
  502. iput(inode);
  503. out:
  504. return error;
  505. }
  506. struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
  507. unsigned int flags)
  508. {
  509. struct inode *inode;
  510. char *name;
  511. int err;
  512. inode = hostfs_iget(ino->i_sb);
  513. if (IS_ERR(inode)) {
  514. err = PTR_ERR(inode);
  515. goto out;
  516. }
  517. err = -ENOMEM;
  518. name = dentry_name(dentry);
  519. if (name == NULL)
  520. goto out_put;
  521. err = read_name(inode, name);
  522. __putname(name);
  523. if (err == -ENOENT) {
  524. iput(inode);
  525. inode = NULL;
  526. }
  527. else if (err)
  528. goto out_put;
  529. d_add(dentry, inode);
  530. return NULL;
  531. out_put:
  532. iput(inode);
  533. out:
  534. return ERR_PTR(err);
  535. }
  536. int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
  537. {
  538. char *from_name, *to_name;
  539. int err;
  540. if ((from_name = dentry_name(from)) == NULL)
  541. return -ENOMEM;
  542. to_name = dentry_name(to);
  543. if (to_name == NULL) {
  544. __putname(from_name);
  545. return -ENOMEM;
  546. }
  547. err = link_file(to_name, from_name);
  548. __putname(from_name);
  549. __putname(to_name);
  550. return err;
  551. }
  552. int hostfs_unlink(struct inode *ino, struct dentry *dentry)
  553. {
  554. char *file;
  555. int err;
  556. if (append)
  557. return -EPERM;
  558. if ((file = dentry_name(dentry)) == NULL)
  559. return -ENOMEM;
  560. err = unlink_file(file);
  561. __putname(file);
  562. return err;
  563. }
  564. int hostfs_symlink(struct inode *ino, struct dentry *dentry, 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. 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. 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. 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. 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. 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 = &hostfs_dentry_ops;
  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");