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/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 "user_util.h"
  23. #include "init.h"
  24. struct hostfs_inode_info {
  25. char *host_filename;
  26. int fd;
  27. int mode;
  28. struct inode vfs_inode;
  29. };
  30. static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
  31. {
  32. return(list_entry(inode, struct hostfs_inode_info, vfs_inode));
  33. }
  34. #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_dentry->d_inode)
  35. int hostfs_d_delete(struct dentry *dentry)
  36. {
  37. return(1);
  38. }
  39. struct dentry_operations hostfs_dentry_ops = {
  40. .d_delete = hostfs_d_delete,
  41. };
  42. /* Changed in hostfs_args before the kernel starts running */
  43. static char *root_ino = "/";
  44. static int append = 0;
  45. #define HOSTFS_SUPER_MAGIC 0x00c0ffee
  46. static struct inode_operations hostfs_iops;
  47. static struct inode_operations hostfs_dir_iops;
  48. static const struct address_space_operations hostfs_link_aops;
  49. #ifndef MODULE
  50. static int __init hostfs_args(char *options, int *add)
  51. {
  52. char *ptr;
  53. ptr = strchr(options, ',');
  54. if(ptr != NULL)
  55. *ptr++ = '\0';
  56. if(*options != '\0')
  57. root_ino = options;
  58. options = ptr;
  59. while(options){
  60. ptr = strchr(options, ',');
  61. if(ptr != NULL)
  62. *ptr++ = '\0';
  63. if(*options != '\0'){
  64. if(!strcmp(options, "append"))
  65. append = 1;
  66. else printf("hostfs_args - unsupported option - %s\n",
  67. options);
  68. }
  69. options = ptr;
  70. }
  71. return(0);
  72. }
  73. __uml_setup("hostfs=", hostfs_args,
  74. "hostfs=<root dir>,<flags>,...\n"
  75. " This is used to set hostfs parameters. The root directory argument\n"
  76. " is used to confine all hostfs mounts to within the specified directory\n"
  77. " tree on the host. If this isn't specified, then a user inside UML can\n"
  78. " mount anything on the host that's accessible to the user that's running\n"
  79. " it.\n"
  80. " The only flag currently supported is 'append', which specifies that all\n"
  81. " files opened by hostfs will be opened in append mode.\n\n"
  82. );
  83. #endif
  84. static char *dentry_name(struct dentry *dentry, int extra)
  85. {
  86. struct dentry *parent;
  87. char *root, *name;
  88. int len;
  89. len = 0;
  90. parent = dentry;
  91. while(parent->d_parent != parent){
  92. len += parent->d_name.len + 1;
  93. parent = parent->d_parent;
  94. }
  95. root = HOSTFS_I(parent->d_inode)->host_filename;
  96. len += strlen(root);
  97. name = kmalloc(len + extra + 1, GFP_KERNEL);
  98. if(name == NULL) 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);
  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) return(err);
  221. sf->f_blocks = f_blocks;
  222. sf->f_bfree = f_bfree;
  223. sf->f_bavail = f_bavail;
  224. sf->f_files = f_files;
  225. sf->f_ffree = f_ffree;
  226. sf->f_type = HOSTFS_SUPER_MAGIC;
  227. return(0);
  228. }
  229. static struct inode *hostfs_alloc_inode(struct super_block *sb)
  230. {
  231. struct hostfs_inode_info *hi;
  232. hi = kmalloc(sizeof(*hi), GFP_KERNEL);
  233. if(hi == NULL)
  234. return(NULL);
  235. *hi = ((struct hostfs_inode_info) { .host_filename = NULL,
  236. .fd = -1,
  237. .mode = 0 });
  238. inode_init_once(&hi->vfs_inode);
  239. return(&hi->vfs_inode);
  240. }
  241. static void hostfs_delete_inode(struct inode *inode)
  242. {
  243. truncate_inode_pages(&inode->i_data, 0);
  244. if(HOSTFS_I(inode)->fd != -1) {
  245. close_file(&HOSTFS_I(inode)->fd);
  246. HOSTFS_I(inode)->fd = -1;
  247. }
  248. clear_inode(inode);
  249. }
  250. static void hostfs_destroy_inode(struct inode *inode)
  251. {
  252. kfree(HOSTFS_I(inode)->host_filename);
  253. /*XXX: This should not happen, probably. The check is here for
  254. * additional safety.*/
  255. if(HOSTFS_I(inode)->fd != -1) {
  256. close_file(&HOSTFS_I(inode)->fd);
  257. printk(KERN_DEBUG "Closing host fd in .destroy_inode\n");
  258. }
  259. kfree(HOSTFS_I(inode));
  260. }
  261. static void hostfs_read_inode(struct inode *inode)
  262. {
  263. read_inode(inode);
  264. }
  265. static struct super_operations hostfs_sbops = {
  266. .alloc_inode = hostfs_alloc_inode,
  267. .drop_inode = generic_delete_inode,
  268. .delete_inode = hostfs_delete_inode,
  269. .destroy_inode = hostfs_destroy_inode,
  270. .read_inode = hostfs_read_inode,
  271. .statfs = hostfs_statfs,
  272. };
  273. int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
  274. {
  275. void *dir;
  276. char *name;
  277. unsigned long long next, ino;
  278. int error, len;
  279. name = dentry_name(file->f_dentry, 0);
  280. if(name == NULL) return(-ENOMEM);
  281. dir = open_dir(name, &error);
  282. kfree(name);
  283. if(dir == NULL) return(-error);
  284. next = file->f_pos;
  285. while((name = read_dir(dir, &next, &ino, &len)) != NULL){
  286. error = (*filldir)(ent, name, len, file->f_pos,
  287. ino, DT_UNKNOWN);
  288. if(error) break;
  289. file->f_pos = next;
  290. }
  291. close_dir(dir);
  292. return(0);
  293. }
  294. int hostfs_file_open(struct inode *ino, struct file *file)
  295. {
  296. char *name;
  297. int mode = 0, r = 0, w = 0, fd;
  298. mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
  299. if((mode & HOSTFS_I(ino)->mode) == mode)
  300. return(0);
  301. /* The file may already have been opened, but with the wrong access,
  302. * so this resets things and reopens the file with the new access.
  303. */
  304. if(HOSTFS_I(ino)->fd != -1){
  305. close_file(&HOSTFS_I(ino)->fd);
  306. HOSTFS_I(ino)->fd = -1;
  307. }
  308. HOSTFS_I(ino)->mode |= mode;
  309. if(HOSTFS_I(ino)->mode & FMODE_READ)
  310. r = 1;
  311. if(HOSTFS_I(ino)->mode & FMODE_WRITE)
  312. w = 1;
  313. if(w)
  314. r = 1;
  315. name = dentry_name(file->f_dentry, 0);
  316. if(name == NULL)
  317. return(-ENOMEM);
  318. fd = open_file(name, r, w, append);
  319. kfree(name);
  320. if(fd < 0) return(fd);
  321. FILE_HOSTFS_I(file)->fd = fd;
  322. return(0);
  323. }
  324. int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync)
  325. {
  326. return fsync_file(HOSTFS_I(dentry->d_inode)->fd, datasync);
  327. }
  328. static const struct file_operations hostfs_file_fops = {
  329. .llseek = generic_file_llseek,
  330. .read = generic_file_read,
  331. .sendfile = generic_file_sendfile,
  332. .aio_read = generic_file_aio_read,
  333. .aio_write = generic_file_aio_write,
  334. .readv = generic_file_readv,
  335. .writev = generic_file_writev,
  336. .write = generic_file_write,
  337. .mmap = generic_file_mmap,
  338. .open = hostfs_file_open,
  339. .release = NULL,
  340. .fsync = hostfs_fsync,
  341. };
  342. static const struct file_operations hostfs_dir_fops = {
  343. .llseek = generic_file_llseek,
  344. .readdir = hostfs_readdir,
  345. .read = generic_read_dir,
  346. };
  347. int hostfs_writepage(struct page *page, struct writeback_control *wbc)
  348. {
  349. struct address_space *mapping = page->mapping;
  350. struct inode *inode = mapping->host;
  351. char *buffer;
  352. unsigned long long base;
  353. int count = PAGE_CACHE_SIZE;
  354. int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
  355. int err;
  356. if (page->index >= end_index)
  357. count = inode->i_size & (PAGE_CACHE_SIZE-1);
  358. buffer = kmap(page);
  359. base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
  360. err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
  361. if(err != count){
  362. ClearPageUptodate(page);
  363. goto out;
  364. }
  365. if (base > inode->i_size)
  366. inode->i_size = base;
  367. if (PageError(page))
  368. ClearPageError(page);
  369. err = 0;
  370. out:
  371. kunmap(page);
  372. unlock_page(page);
  373. return err;
  374. }
  375. int hostfs_readpage(struct file *file, struct page *page)
  376. {
  377. char *buffer;
  378. long long start;
  379. int err = 0;
  380. start = (long long) page->index << PAGE_CACHE_SHIFT;
  381. buffer = kmap(page);
  382. err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
  383. PAGE_CACHE_SIZE);
  384. if(err < 0) goto out;
  385. memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
  386. flush_dcache_page(page);
  387. SetPageUptodate(page);
  388. if (PageError(page)) ClearPageError(page);
  389. err = 0;
  390. out:
  391. kunmap(page);
  392. unlock_page(page);
  393. return(err);
  394. }
  395. int hostfs_prepare_write(struct file *file, struct page *page,
  396. unsigned int from, unsigned int to)
  397. {
  398. char *buffer;
  399. long long start, tmp;
  400. int err;
  401. start = (long long) page->index << PAGE_CACHE_SHIFT;
  402. buffer = kmap(page);
  403. if(from != 0){
  404. tmp = start;
  405. err = read_file(FILE_HOSTFS_I(file)->fd, &tmp, buffer,
  406. from);
  407. if(err < 0) goto out;
  408. }
  409. if(to != PAGE_CACHE_SIZE){
  410. start += to;
  411. err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer + to,
  412. PAGE_CACHE_SIZE - to);
  413. if(err < 0) goto out;
  414. }
  415. err = 0;
  416. out:
  417. kunmap(page);
  418. return(err);
  419. }
  420. int hostfs_commit_write(struct file *file, struct page *page, unsigned from,
  421. unsigned to)
  422. {
  423. struct address_space *mapping = page->mapping;
  424. struct inode *inode = mapping->host;
  425. char *buffer;
  426. long long start;
  427. int err = 0;
  428. start = (((long long) page->index) << PAGE_CACHE_SHIFT) + from;
  429. buffer = kmap(page);
  430. err = write_file(FILE_HOSTFS_I(file)->fd, &start, buffer + from,
  431. to - from);
  432. if(err > 0) err = 0;
  433. /* Actually, if !err, write_file has added to-from to start, so, despite
  434. * the appearance, we are comparing i_size against the _last_ written
  435. * location, as we should. */
  436. if(!err && (start > inode->i_size))
  437. inode->i_size = start;
  438. kunmap(page);
  439. return(err);
  440. }
  441. static const 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. int err;
  783. buffer = kmap(page);
  784. name = inode_name(page->mapping->host, 0);
  785. if(name == NULL) return(-ENOMEM);
  786. err = do_readlink(name, buffer, PAGE_CACHE_SIZE);
  787. kfree(name);
  788. if(err == PAGE_CACHE_SIZE)
  789. err = -E2BIG;
  790. else if(err > 0){
  791. flush_dcache_page(page);
  792. SetPageUptodate(page);
  793. if (PageError(page)) ClearPageError(page);
  794. err = 0;
  795. }
  796. kunmap(page);
  797. unlock_page(page);
  798. return(err);
  799. }
  800. static const struct address_space_operations hostfs_link_aops = {
  801. .readpage = hostfs_link_readpage,
  802. };
  803. static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
  804. {
  805. struct inode *root_inode;
  806. char *name, *data = d;
  807. int err;
  808. sb->s_blocksize = 1024;
  809. sb->s_blocksize_bits = 10;
  810. sb->s_magic = HOSTFS_SUPER_MAGIC;
  811. sb->s_op = &hostfs_sbops;
  812. if((data == NULL) || (*data == '\0'))
  813. data = root_ino;
  814. err = -ENOMEM;
  815. name = kmalloc(strlen(data) + 1, GFP_KERNEL);
  816. if(name == NULL)
  817. goto out;
  818. strcpy(name, data);
  819. root_inode = iget(sb, 0);
  820. if(root_inode == NULL)
  821. goto out_free;
  822. err = init_inode(root_inode, NULL);
  823. if(err)
  824. goto out_put;
  825. HOSTFS_I(root_inode)->host_filename = name;
  826. err = -ENOMEM;
  827. sb->s_root = d_alloc_root(root_inode);
  828. if(sb->s_root == NULL)
  829. goto out_put;
  830. err = read_inode(root_inode);
  831. if(err){
  832. /* No iput in this case because the dput does that for us */
  833. dput(sb->s_root);
  834. sb->s_root = NULL;
  835. goto out_free;
  836. }
  837. return(0);
  838. out_put:
  839. iput(root_inode);
  840. out_free:
  841. kfree(name);
  842. out:
  843. return(err);
  844. }
  845. static int hostfs_read_sb(struct file_system_type *type,
  846. int flags, const char *dev_name,
  847. void *data, struct vfsmount *mnt)
  848. {
  849. return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt);
  850. }
  851. static struct file_system_type hostfs_type = {
  852. .owner = THIS_MODULE,
  853. .name = "hostfs",
  854. .get_sb = hostfs_read_sb,
  855. .kill_sb = kill_anon_super,
  856. .fs_flags = 0,
  857. };
  858. static int __init init_hostfs(void)
  859. {
  860. return(register_filesystem(&hostfs_type));
  861. }
  862. static void __exit exit_hostfs(void)
  863. {
  864. unregister_filesystem(&hostfs_type);
  865. }
  866. module_init(init_hostfs)
  867. module_exit(exit_hostfs)
  868. MODULE_LICENSE("GPL");
  869. /*
  870. * Overrides for Emacs so that we follow Linus's tabbing style.
  871. * Emacs will notice this stuff at the end of the file and automatically
  872. * adjust the settings for this buffer only. This must remain at the end
  873. * of the file.
  874. * ---------------------------------------------------------------------------
  875. * Local variables:
  876. * c-file-style: "linux"
  877. * End:
  878. */