hostfs_kern.c 22 KB

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