hostfs_kern.c 22 KB

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