hostfs_kern.c 22 KB

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