hostfs_kern.c 22 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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/module.h>
  10. #include <linux/mm.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/statfs.h>
  13. #include <linux/slab.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/mount.h>
  16. #include "hostfs.h"
  17. #include "init.h"
  18. #include "kern.h"
  19. struct hostfs_inode_info {
  20. char *host_filename;
  21. int fd;
  22. fmode_t mode;
  23. struct inode vfs_inode;
  24. };
  25. static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
  26. {
  27. return list_entry(inode, struct hostfs_inode_info, vfs_inode);
  28. }
  29. #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_path.dentry->d_inode)
  30. static int hostfs_d_delete(struct dentry *dentry)
  31. {
  32. return 1;
  33. }
  34. static const struct dentry_operations hostfs_dentry_ops = {
  35. .d_delete = hostfs_d_delete,
  36. };
  37. /* Changed in hostfs_args before the kernel starts running */
  38. static char *root_ino = "";
  39. static int append = 0;
  40. #define HOSTFS_SUPER_MAGIC 0x00c0ffee
  41. static const struct inode_operations hostfs_iops;
  42. static const struct inode_operations hostfs_dir_iops;
  43. static const struct address_space_operations hostfs_link_aops;
  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, int extra)
  80. {
  81. struct dentry *parent;
  82. char *root, *name;
  83. int len;
  84. len = 0;
  85. parent = dentry;
  86. while (parent->d_parent != parent) {
  87. len += parent->d_name.len + 1;
  88. parent = parent->d_parent;
  89. }
  90. root = HOSTFS_I(parent->d_inode)->host_filename;
  91. len += strlen(root);
  92. name = kmalloc(len + extra + 1, GFP_KERNEL);
  93. if (name == NULL)
  94. return NULL;
  95. name[len] = '\0';
  96. parent = dentry;
  97. while (parent->d_parent != parent) {
  98. len -= parent->d_name.len + 1;
  99. name[len] = '/';
  100. strncpy(&name[len + 1], parent->d_name.name,
  101. parent->d_name.len);
  102. parent = parent->d_parent;
  103. }
  104. strncpy(name, root, strlen(root));
  105. return name;
  106. }
  107. static char *inode_name(struct inode *ino, int extra)
  108. {
  109. struct dentry *dentry;
  110. dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias);
  111. return dentry_name(dentry, extra);
  112. }
  113. static int read_name(struct inode *ino, char *name)
  114. {
  115. /*
  116. * The non-int inode fields are copied into ints by stat_file and
  117. * then copied into the inode because passing the actual pointers
  118. * in and having them treated as int * breaks on big-endian machines
  119. */
  120. int err;
  121. int i_mode, i_nlink, i_blksize;
  122. unsigned long long i_size;
  123. unsigned long long i_ino;
  124. unsigned long long i_blocks;
  125. err = stat_file(name, &i_ino, &i_mode, &i_nlink, &ino->i_uid,
  126. &ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime,
  127. &ino->i_ctime, &i_blksize, &i_blocks, -1);
  128. if (err)
  129. return err;
  130. ino->i_ino = i_ino;
  131. ino->i_mode = i_mode;
  132. ino->i_nlink = i_nlink;
  133. ino->i_size = i_size;
  134. ino->i_blocks = i_blocks;
  135. return 0;
  136. }
  137. static char *follow_link(char *link)
  138. {
  139. int len, n;
  140. char *name, *resolved, *end;
  141. len = 64;
  142. while (1) {
  143. n = -ENOMEM;
  144. name = kmalloc(len, GFP_KERNEL);
  145. if (name == NULL)
  146. goto out;
  147. n = hostfs_do_readlink(link, name, len);
  148. if (n < len)
  149. break;
  150. len *= 2;
  151. kfree(name);
  152. }
  153. if (n < 0)
  154. goto out_free;
  155. if (*name == '/')
  156. return name;
  157. end = strrchr(link, '/');
  158. if (end == NULL)
  159. return name;
  160. *(end + 1) = '\0';
  161. len = strlen(link) + strlen(name) + 1;
  162. resolved = kmalloc(len, GFP_KERNEL);
  163. if (resolved == NULL) {
  164. n = -ENOMEM;
  165. goto out_free;
  166. }
  167. sprintf(resolved, "%s%s", link, name);
  168. kfree(name);
  169. kfree(link);
  170. return resolved;
  171. out_free:
  172. kfree(name);
  173. out:
  174. return ERR_PTR(n);
  175. }
  176. static int hostfs_read_inode(struct inode *ino)
  177. {
  178. char *name;
  179. int err = 0;
  180. /*
  181. * Unfortunately, we are called from iget() when we don't have a dentry
  182. * allocated yet.
  183. */
  184. if (list_empty(&ino->i_dentry))
  185. goto out;
  186. err = -ENOMEM;
  187. name = inode_name(ino, 0);
  188. if (name == NULL)
  189. goto out;
  190. if (file_type(name, NULL, NULL) == OS_TYPE_SYMLINK) {
  191. name = follow_link(name);
  192. if (IS_ERR(name)) {
  193. err = PTR_ERR(name);
  194. goto out;
  195. }
  196. }
  197. err = read_name(ino, name);
  198. kfree(name);
  199. out:
  200. return err;
  201. }
  202. static struct inode *hostfs_iget(struct super_block *sb)
  203. {
  204. struct inode *inode;
  205. long ret;
  206. inode = iget_locked(sb, 0);
  207. if (!inode)
  208. return ERR_PTR(-ENOMEM);
  209. if (inode->i_state & I_NEW) {
  210. ret = hostfs_read_inode(inode);
  211. if (ret < 0) {
  212. iget_failed(inode);
  213. return ERR_PTR(ret);
  214. }
  215. unlock_new_inode(inode);
  216. }
  217. return inode;
  218. }
  219. int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
  220. {
  221. /*
  222. * do_statfs uses struct statfs64 internally, but the linux kernel
  223. * struct statfs still has 32-bit versions for most of these fields,
  224. * so we convert them here
  225. */
  226. int err;
  227. long long f_blocks;
  228. long long f_bfree;
  229. long long f_bavail;
  230. long long f_files;
  231. long long f_ffree;
  232. err = do_statfs(HOSTFS_I(dentry->d_sb->s_root->d_inode)->host_filename,
  233. &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
  234. &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
  235. &sf->f_namelen, sf->f_spare);
  236. if (err)
  237. return err;
  238. sf->f_blocks = f_blocks;
  239. sf->f_bfree = f_bfree;
  240. sf->f_bavail = f_bavail;
  241. sf->f_files = f_files;
  242. sf->f_ffree = f_ffree;
  243. sf->f_type = HOSTFS_SUPER_MAGIC;
  244. return 0;
  245. }
  246. static struct inode *hostfs_alloc_inode(struct super_block *sb)
  247. {
  248. struct hostfs_inode_info *hi;
  249. hi = kmalloc(sizeof(*hi), GFP_KERNEL);
  250. if (hi == NULL)
  251. return NULL;
  252. *hi = ((struct hostfs_inode_info) { .host_filename = NULL,
  253. .fd = -1,
  254. .mode = 0 });
  255. inode_init_once(&hi->vfs_inode);
  256. return &hi->vfs_inode;
  257. }
  258. static void hostfs_delete_inode(struct inode *inode)
  259. {
  260. truncate_inode_pages(&inode->i_data, 0);
  261. if (HOSTFS_I(inode)->fd != -1) {
  262. close_file(&HOSTFS_I(inode)->fd);
  263. HOSTFS_I(inode)->fd = -1;
  264. }
  265. clear_inode(inode);
  266. }
  267. static void hostfs_destroy_inode(struct inode *inode)
  268. {
  269. kfree(HOSTFS_I(inode)->host_filename);
  270. /*
  271. * XXX: This should not happen, probably. The check is here for
  272. * additional safety.
  273. */
  274. if (HOSTFS_I(inode)->fd != -1) {
  275. close_file(&HOSTFS_I(inode)->fd);
  276. printk(KERN_DEBUG "Closing host fd in .destroy_inode\n");
  277. }
  278. kfree(HOSTFS_I(inode));
  279. }
  280. static int hostfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
  281. {
  282. struct inode *root = vfs->mnt_sb->s_root->d_inode;
  283. const char *root_path = HOSTFS_I(root)->host_filename;
  284. size_t offset = strlen(root_ino) + 1;
  285. if (strlen(root_path) > offset)
  286. seq_printf(seq, ",%s", root_path + offset);
  287. return 0;
  288. }
  289. static const struct super_operations hostfs_sbops = {
  290. .alloc_inode = hostfs_alloc_inode,
  291. .drop_inode = generic_delete_inode,
  292. .delete_inode = hostfs_delete_inode,
  293. .destroy_inode = hostfs_destroy_inode,
  294. .statfs = hostfs_statfs,
  295. .show_options = hostfs_show_options,
  296. };
  297. int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
  298. {
  299. void *dir;
  300. char *name;
  301. unsigned long long next, ino;
  302. int error, len;
  303. name = dentry_name(file->f_path.dentry, 0);
  304. if (name == NULL)
  305. return -ENOMEM;
  306. dir = open_dir(name, &error);
  307. kfree(name);
  308. if (dir == NULL)
  309. return -error;
  310. next = file->f_pos;
  311. while ((name = read_dir(dir, &next, &ino, &len)) != NULL) {
  312. error = (*filldir)(ent, name, len, file->f_pos,
  313. ino, DT_UNKNOWN);
  314. if (error) break;
  315. file->f_pos = next;
  316. }
  317. close_dir(dir);
  318. return 0;
  319. }
  320. int hostfs_file_open(struct inode *ino, struct file *file)
  321. {
  322. char *name;
  323. fmode_t mode = 0;
  324. int r = 0, w = 0, fd;
  325. mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
  326. if ((mode & HOSTFS_I(ino)->mode) == mode)
  327. return 0;
  328. /*
  329. * The file may already have been opened, but with the wrong access,
  330. * so this resets things and reopens the file with the new access.
  331. */
  332. if (HOSTFS_I(ino)->fd != -1) {
  333. close_file(&HOSTFS_I(ino)->fd);
  334. HOSTFS_I(ino)->fd = -1;
  335. }
  336. HOSTFS_I(ino)->mode |= mode;
  337. if (HOSTFS_I(ino)->mode & FMODE_READ)
  338. r = 1;
  339. if (HOSTFS_I(ino)->mode & FMODE_WRITE)
  340. w = 1;
  341. if (w)
  342. r = 1;
  343. name = dentry_name(file->f_path.dentry, 0);
  344. if (name == NULL)
  345. return -ENOMEM;
  346. fd = open_file(name, r, w, append);
  347. kfree(name);
  348. if (fd < 0)
  349. return fd;
  350. FILE_HOSTFS_I(file)->fd = fd;
  351. return 0;
  352. }
  353. int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync)
  354. {
  355. return fsync_file(HOSTFS_I(dentry->d_inode)->fd, datasync);
  356. }
  357. static const struct file_operations hostfs_file_fops = {
  358. .llseek = generic_file_llseek,
  359. .read = do_sync_read,
  360. .splice_read = generic_file_splice_read,
  361. .aio_read = generic_file_aio_read,
  362. .aio_write = generic_file_aio_write,
  363. .write = do_sync_write,
  364. .mmap = generic_file_mmap,
  365. .open = hostfs_file_open,
  366. .release = NULL,
  367. .fsync = hostfs_fsync,
  368. };
  369. static const struct file_operations hostfs_dir_fops = {
  370. .llseek = generic_file_llseek,
  371. .readdir = hostfs_readdir,
  372. .read = generic_read_dir,
  373. };
  374. int hostfs_writepage(struct page *page, struct writeback_control *wbc)
  375. {
  376. struct address_space *mapping = page->mapping;
  377. struct inode *inode = mapping->host;
  378. char *buffer;
  379. unsigned long long base;
  380. int count = PAGE_CACHE_SIZE;
  381. int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
  382. int err;
  383. if (page->index >= end_index)
  384. count = inode->i_size & (PAGE_CACHE_SIZE-1);
  385. buffer = kmap(page);
  386. base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
  387. err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
  388. if (err != count) {
  389. ClearPageUptodate(page);
  390. goto out;
  391. }
  392. if (base > inode->i_size)
  393. inode->i_size = base;
  394. if (PageError(page))
  395. ClearPageError(page);
  396. err = 0;
  397. out:
  398. kunmap(page);
  399. unlock_page(page);
  400. return err;
  401. }
  402. int hostfs_readpage(struct file *file, struct page *page)
  403. {
  404. char *buffer;
  405. long long start;
  406. int err = 0;
  407. start = (long long) page->index << PAGE_CACHE_SHIFT;
  408. buffer = kmap(page);
  409. err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
  410. PAGE_CACHE_SIZE);
  411. if (err < 0)
  412. goto out;
  413. memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
  414. flush_dcache_page(page);
  415. SetPageUptodate(page);
  416. if (PageError(page)) ClearPageError(page);
  417. err = 0;
  418. out:
  419. kunmap(page);
  420. unlock_page(page);
  421. return err;
  422. }
  423. int hostfs_write_begin(struct file *file, struct address_space *mapping,
  424. loff_t pos, unsigned len, unsigned flags,
  425. struct page **pagep, void **fsdata)
  426. {
  427. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  428. *pagep = grab_cache_page_write_begin(mapping, index, flags);
  429. if (!*pagep)
  430. return -ENOMEM;
  431. return 0;
  432. }
  433. int hostfs_write_end(struct file *file, struct address_space *mapping,
  434. loff_t pos, unsigned len, unsigned copied,
  435. struct page *page, void *fsdata)
  436. {
  437. struct inode *inode = mapping->host;
  438. void *buffer;
  439. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  440. int err;
  441. buffer = kmap(page);
  442. err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
  443. kunmap(page);
  444. if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
  445. SetPageUptodate(page);
  446. /*
  447. * If err > 0, write_file has added err to pos, so we are comparing
  448. * i_size against the last byte written.
  449. */
  450. if (err > 0 && (pos > inode->i_size))
  451. inode->i_size = pos;
  452. unlock_page(page);
  453. page_cache_release(page);
  454. return err;
  455. }
  456. static const struct address_space_operations hostfs_aops = {
  457. .writepage = hostfs_writepage,
  458. .readpage = hostfs_readpage,
  459. .set_page_dirty = __set_page_dirty_nobuffers,
  460. .write_begin = hostfs_write_begin,
  461. .write_end = hostfs_write_end,
  462. };
  463. static int init_inode(struct inode *inode, struct dentry *dentry)
  464. {
  465. char *name;
  466. int type, err = -ENOMEM;
  467. int maj, min;
  468. dev_t rdev = 0;
  469. if (dentry) {
  470. name = dentry_name(dentry, 0);
  471. if (name == NULL)
  472. goto out;
  473. type = file_type(name, &maj, &min);
  474. /* Reencode maj and min with the kernel encoding.*/
  475. rdev = MKDEV(maj, min);
  476. kfree(name);
  477. }
  478. else type = OS_TYPE_DIR;
  479. err = 0;
  480. if (type == OS_TYPE_SYMLINK)
  481. inode->i_op = &page_symlink_inode_operations;
  482. else if (type == OS_TYPE_DIR)
  483. inode->i_op = &hostfs_dir_iops;
  484. else inode->i_op = &hostfs_iops;
  485. if (type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
  486. else inode->i_fop = &hostfs_file_fops;
  487. if (type == OS_TYPE_SYMLINK)
  488. inode->i_mapping->a_ops = &hostfs_link_aops;
  489. else inode->i_mapping->a_ops = &hostfs_aops;
  490. switch (type) {
  491. case OS_TYPE_CHARDEV:
  492. init_special_inode(inode, S_IFCHR, rdev);
  493. break;
  494. case OS_TYPE_BLOCKDEV:
  495. init_special_inode(inode, S_IFBLK, rdev);
  496. break;
  497. case OS_TYPE_FIFO:
  498. init_special_inode(inode, S_IFIFO, 0);
  499. break;
  500. case OS_TYPE_SOCK:
  501. init_special_inode(inode, S_IFSOCK, 0);
  502. break;
  503. }
  504. out:
  505. return err;
  506. }
  507. int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
  508. struct nameidata *nd)
  509. {
  510. struct inode *inode;
  511. char *name;
  512. int error, fd;
  513. inode = hostfs_iget(dir->i_sb);
  514. if (IS_ERR(inode)) {
  515. error = PTR_ERR(inode);
  516. goto out;
  517. }
  518. error = init_inode(inode, dentry);
  519. if (error)
  520. goto out_put;
  521. error = -ENOMEM;
  522. name = dentry_name(dentry, 0);
  523. if (name == NULL)
  524. goto out_put;
  525. fd = file_create(name,
  526. mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
  527. mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
  528. mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
  529. if (fd < 0)
  530. error = fd;
  531. else error = read_name(inode, name);
  532. kfree(name);
  533. if (error)
  534. goto out_put;
  535. HOSTFS_I(inode)->fd = fd;
  536. HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
  537. d_instantiate(dentry, inode);
  538. return 0;
  539. out_put:
  540. iput(inode);
  541. out:
  542. return error;
  543. }
  544. struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
  545. struct nameidata *nd)
  546. {
  547. struct inode *inode;
  548. char *name;
  549. int err;
  550. inode = hostfs_iget(ino->i_sb);
  551. if (IS_ERR(inode)) {
  552. err = PTR_ERR(inode);
  553. goto out;
  554. }
  555. err = init_inode(inode, dentry);
  556. if (err)
  557. goto out_put;
  558. err = -ENOMEM;
  559. name = dentry_name(dentry, 0);
  560. if (name == NULL)
  561. goto out_put;
  562. err = read_name(inode, name);
  563. kfree(name);
  564. if (err == -ENOENT) {
  565. iput(inode);
  566. inode = NULL;
  567. }
  568. else if (err)
  569. goto out_put;
  570. d_add(dentry, inode);
  571. dentry->d_op = &hostfs_dentry_ops;
  572. return NULL;
  573. out_put:
  574. iput(inode);
  575. out:
  576. return ERR_PTR(err);
  577. }
  578. static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
  579. {
  580. char *file;
  581. int len;
  582. file = inode_name(ino, dentry->d_name.len + 1);
  583. if (file == NULL)
  584. return NULL;
  585. strcat(file, "/");
  586. len = strlen(file);
  587. strncat(file, dentry->d_name.name, dentry->d_name.len);
  588. file[len + dentry->d_name.len] = '\0';
  589. return file;
  590. }
  591. int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
  592. {
  593. char *from_name, *to_name;
  594. int err;
  595. if ((from_name = inode_dentry_name(ino, from)) == NULL)
  596. return -ENOMEM;
  597. to_name = dentry_name(to, 0);
  598. if (to_name == NULL) {
  599. kfree(from_name);
  600. return -ENOMEM;
  601. }
  602. err = link_file(to_name, from_name);
  603. kfree(from_name);
  604. kfree(to_name);
  605. return err;
  606. }
  607. int hostfs_unlink(struct inode *ino, struct dentry *dentry)
  608. {
  609. char *file;
  610. int err;
  611. if ((file = inode_dentry_name(ino, dentry)) == NULL)
  612. return -ENOMEM;
  613. if (append)
  614. return -EPERM;
  615. err = unlink_file(file);
  616. kfree(file);
  617. return err;
  618. }
  619. int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
  620. {
  621. char *file;
  622. int err;
  623. if ((file = inode_dentry_name(ino, dentry)) == NULL)
  624. return -ENOMEM;
  625. err = make_symlink(file, to);
  626. kfree(file);
  627. return err;
  628. }
  629. int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
  630. {
  631. char *file;
  632. int err;
  633. if ((file = inode_dentry_name(ino, dentry)) == NULL)
  634. return -ENOMEM;
  635. err = do_mkdir(file, mode);
  636. kfree(file);
  637. return err;
  638. }
  639. int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
  640. {
  641. char *file;
  642. int err;
  643. if ((file = inode_dentry_name(ino, dentry)) == NULL)
  644. return -ENOMEM;
  645. err = do_rmdir(file);
  646. kfree(file);
  647. return err;
  648. }
  649. int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  650. {
  651. struct inode *inode;
  652. char *name;
  653. int err;
  654. inode = hostfs_iget(dir->i_sb);
  655. if (IS_ERR(inode)) {
  656. err = PTR_ERR(inode);
  657. goto out;
  658. }
  659. err = init_inode(inode, dentry);
  660. if (err)
  661. goto out_put;
  662. err = -ENOMEM;
  663. name = dentry_name(dentry, 0);
  664. if (name == NULL)
  665. goto out_put;
  666. init_special_inode(inode, mode, dev);
  667. err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
  668. if (err)
  669. goto out_free;
  670. err = read_name(inode, name);
  671. kfree(name);
  672. if (err)
  673. goto out_put;
  674. d_instantiate(dentry, inode);
  675. return 0;
  676. out_free:
  677. kfree(name);
  678. out_put:
  679. iput(inode);
  680. out:
  681. return err;
  682. }
  683. int hostfs_rename(struct inode *from_ino, struct dentry *from,
  684. struct inode *to_ino, struct dentry *to)
  685. {
  686. char *from_name, *to_name;
  687. int err;
  688. if ((from_name = inode_dentry_name(from_ino, from)) == NULL)
  689. return -ENOMEM;
  690. if ((to_name = inode_dentry_name(to_ino, to)) == NULL) {
  691. kfree(from_name);
  692. return -ENOMEM;
  693. }
  694. err = rename_file(from_name, to_name);
  695. kfree(from_name);
  696. kfree(to_name);
  697. return err;
  698. }
  699. int hostfs_permission(struct inode *ino, int desired)
  700. {
  701. char *name;
  702. int r = 0, w = 0, x = 0, err;
  703. if (desired & MAY_READ) r = 1;
  704. if (desired & MAY_WRITE) w = 1;
  705. if (desired & MAY_EXEC) x = 1;
  706. name = inode_name(ino, 0);
  707. if (name == NULL)
  708. return -ENOMEM;
  709. if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
  710. S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
  711. err = 0;
  712. else
  713. err = access_file(name, r, w, x);
  714. kfree(name);
  715. if (!err)
  716. err = generic_permission(ino, desired, NULL);
  717. return err;
  718. }
  719. int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
  720. {
  721. struct hostfs_iattr attrs;
  722. char *name;
  723. int err;
  724. int fd = HOSTFS_I(dentry->d_inode)->fd;
  725. err = inode_change_ok(dentry->d_inode, attr);
  726. if (err)
  727. return err;
  728. if (append)
  729. attr->ia_valid &= ~ATTR_SIZE;
  730. attrs.ia_valid = 0;
  731. if (attr->ia_valid & ATTR_MODE) {
  732. attrs.ia_valid |= HOSTFS_ATTR_MODE;
  733. attrs.ia_mode = attr->ia_mode;
  734. }
  735. if (attr->ia_valid & ATTR_UID) {
  736. attrs.ia_valid |= HOSTFS_ATTR_UID;
  737. attrs.ia_uid = attr->ia_uid;
  738. }
  739. if (attr->ia_valid & ATTR_GID) {
  740. attrs.ia_valid |= HOSTFS_ATTR_GID;
  741. attrs.ia_gid = attr->ia_gid;
  742. }
  743. if (attr->ia_valid & ATTR_SIZE) {
  744. attrs.ia_valid |= HOSTFS_ATTR_SIZE;
  745. attrs.ia_size = attr->ia_size;
  746. }
  747. if (attr->ia_valid & ATTR_ATIME) {
  748. attrs.ia_valid |= HOSTFS_ATTR_ATIME;
  749. attrs.ia_atime = attr->ia_atime;
  750. }
  751. if (attr->ia_valid & ATTR_MTIME) {
  752. attrs.ia_valid |= HOSTFS_ATTR_MTIME;
  753. attrs.ia_mtime = attr->ia_mtime;
  754. }
  755. if (attr->ia_valid & ATTR_CTIME) {
  756. attrs.ia_valid |= HOSTFS_ATTR_CTIME;
  757. attrs.ia_ctime = attr->ia_ctime;
  758. }
  759. if (attr->ia_valid & ATTR_ATIME_SET) {
  760. attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
  761. }
  762. if (attr->ia_valid & ATTR_MTIME_SET) {
  763. attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
  764. }
  765. name = dentry_name(dentry, 0);
  766. if (name == NULL)
  767. return -ENOMEM;
  768. err = set_attr(name, &attrs, fd);
  769. kfree(name);
  770. if (err)
  771. return err;
  772. return inode_setattr(dentry->d_inode, attr);
  773. }
  774. static const struct inode_operations hostfs_iops = {
  775. .create = hostfs_create,
  776. .link = hostfs_link,
  777. .unlink = hostfs_unlink,
  778. .symlink = hostfs_symlink,
  779. .mkdir = hostfs_mkdir,
  780. .rmdir = hostfs_rmdir,
  781. .mknod = hostfs_mknod,
  782. .rename = hostfs_rename,
  783. .permission = hostfs_permission,
  784. .setattr = hostfs_setattr,
  785. };
  786. static const struct inode_operations hostfs_dir_iops = {
  787. .create = hostfs_create,
  788. .lookup = hostfs_lookup,
  789. .link = hostfs_link,
  790. .unlink = hostfs_unlink,
  791. .symlink = hostfs_symlink,
  792. .mkdir = hostfs_mkdir,
  793. .rmdir = hostfs_rmdir,
  794. .mknod = hostfs_mknod,
  795. .rename = hostfs_rename,
  796. .permission = hostfs_permission,
  797. .setattr = hostfs_setattr,
  798. };
  799. int hostfs_link_readpage(struct file *file, struct page *page)
  800. {
  801. char *buffer, *name;
  802. int err;
  803. buffer = kmap(page);
  804. name = inode_name(page->mapping->host, 0);
  805. if (name == NULL)
  806. return -ENOMEM;
  807. err = hostfs_do_readlink(name, buffer, PAGE_CACHE_SIZE);
  808. kfree(name);
  809. if (err == PAGE_CACHE_SIZE)
  810. err = -E2BIG;
  811. else if (err > 0) {
  812. flush_dcache_page(page);
  813. SetPageUptodate(page);
  814. if (PageError(page)) ClearPageError(page);
  815. err = 0;
  816. }
  817. kunmap(page);
  818. unlock_page(page);
  819. return err;
  820. }
  821. static const struct address_space_operations hostfs_link_aops = {
  822. .readpage = hostfs_link_readpage,
  823. };
  824. static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
  825. {
  826. struct inode *root_inode;
  827. char *host_root_path, *req_root = d;
  828. int err;
  829. sb->s_blocksize = 1024;
  830. sb->s_blocksize_bits = 10;
  831. sb->s_magic = HOSTFS_SUPER_MAGIC;
  832. sb->s_op = &hostfs_sbops;
  833. sb->s_maxbytes = MAX_LFS_FILESIZE;
  834. /* NULL is printed as <NULL> by sprintf: avoid that. */
  835. if (req_root == NULL)
  836. req_root = "";
  837. err = -ENOMEM;
  838. host_root_path = kmalloc(strlen(root_ino) + 1
  839. + strlen(req_root) + 1, GFP_KERNEL);
  840. if (host_root_path == NULL)
  841. goto out;
  842. sprintf(host_root_path, "%s/%s", root_ino, req_root);
  843. root_inode = hostfs_iget(sb);
  844. if (IS_ERR(root_inode)) {
  845. err = PTR_ERR(root_inode);
  846. goto out_free;
  847. }
  848. err = init_inode(root_inode, NULL);
  849. if (err)
  850. goto out_put;
  851. HOSTFS_I(root_inode)->host_filename = host_root_path;
  852. /*
  853. * Avoid that in the error path, iput(root_inode) frees again
  854. * host_root_path through hostfs_destroy_inode!
  855. */
  856. host_root_path = NULL;
  857. err = -ENOMEM;
  858. sb->s_root = d_alloc_root(root_inode);
  859. if (sb->s_root == NULL)
  860. goto out_put;
  861. err = hostfs_read_inode(root_inode);
  862. if (err) {
  863. /* No iput in this case because the dput does that for us */
  864. dput(sb->s_root);
  865. sb->s_root = NULL;
  866. goto out;
  867. }
  868. return 0;
  869. out_put:
  870. iput(root_inode);
  871. out_free:
  872. kfree(host_root_path);
  873. out:
  874. return err;
  875. }
  876. static int hostfs_read_sb(struct file_system_type *type,
  877. int flags, const char *dev_name,
  878. void *data, struct vfsmount *mnt)
  879. {
  880. return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt);
  881. }
  882. static struct file_system_type hostfs_type = {
  883. .owner = THIS_MODULE,
  884. .name = "hostfs",
  885. .get_sb = hostfs_read_sb,
  886. .kill_sb = kill_anon_super,
  887. .fs_flags = 0,
  888. };
  889. static int __init init_hostfs(void)
  890. {
  891. return register_filesystem(&hostfs_type);
  892. }
  893. static void __exit exit_hostfs(void)
  894. {
  895. unregister_filesystem(&hostfs_type);
  896. }
  897. module_init(init_hostfs)
  898. module_exit(exit_hostfs)
  899. MODULE_LICENSE("GPL");