hostfs_kern.c 22 KB

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