hostfs_kern.c 22 KB

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