hostfs_kern.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032
  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. kfree(HOSTFS_I(inode)->host_filename);
  255. /*XXX: This should not happen, probably. The check is here for
  256. * additional safety.*/
  257. if(HOSTFS_I(inode)->fd != -1) {
  258. close_file(&HOSTFS_I(inode)->fd);
  259. printk(KERN_DEBUG "Closing host fd in .destroy_inode\n");
  260. }
  261. kfree(HOSTFS_I(inode));
  262. }
  263. static void hostfs_read_inode(struct inode *inode)
  264. {
  265. read_inode(inode);
  266. }
  267. static struct super_operations hostfs_sbops = {
  268. .alloc_inode = hostfs_alloc_inode,
  269. .drop_inode = generic_delete_inode,
  270. .delete_inode = hostfs_delete_inode,
  271. .destroy_inode = hostfs_destroy_inode,
  272. .read_inode = hostfs_read_inode,
  273. .statfs = hostfs_statfs,
  274. };
  275. int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
  276. {
  277. void *dir;
  278. char *name;
  279. unsigned long long next, ino;
  280. int error, len;
  281. name = dentry_name(file->f_dentry, 0);
  282. if(name == NULL) return(-ENOMEM);
  283. dir = open_dir(name, &error);
  284. kfree(name);
  285. if(dir == NULL) return(-error);
  286. next = file->f_pos;
  287. while((name = read_dir(dir, &next, &ino, &len)) != NULL){
  288. error = (*filldir)(ent, name, len, file->f_pos,
  289. ino, DT_UNKNOWN);
  290. if(error) break;
  291. file->f_pos = next;
  292. }
  293. close_dir(dir);
  294. return(0);
  295. }
  296. int hostfs_file_open(struct inode *ino, struct file *file)
  297. {
  298. char *name;
  299. int mode = 0, r = 0, w = 0, fd;
  300. mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
  301. if((mode & HOSTFS_I(ino)->mode) == mode)
  302. return(0);
  303. /* The file may already have been opened, but with the wrong access,
  304. * so this resets things and reopens the file with the new access.
  305. */
  306. if(HOSTFS_I(ino)->fd != -1){
  307. close_file(&HOSTFS_I(ino)->fd);
  308. HOSTFS_I(ino)->fd = -1;
  309. }
  310. HOSTFS_I(ino)->mode |= mode;
  311. if(HOSTFS_I(ino)->mode & FMODE_READ)
  312. r = 1;
  313. if(HOSTFS_I(ino)->mode & FMODE_WRITE)
  314. w = 1;
  315. if(w)
  316. r = 1;
  317. name = dentry_name(file->f_dentry, 0);
  318. if(name == NULL)
  319. return(-ENOMEM);
  320. fd = open_file(name, r, w, append);
  321. kfree(name);
  322. if(fd < 0) return(fd);
  323. FILE_HOSTFS_I(file)->fd = fd;
  324. return(0);
  325. }
  326. int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync)
  327. {
  328. return fsync_file(HOSTFS_I(dentry->d_inode)->fd, datasync);
  329. }
  330. static struct file_operations hostfs_file_fops = {
  331. .llseek = generic_file_llseek,
  332. .read = generic_file_read,
  333. .sendfile = generic_file_sendfile,
  334. .aio_read = generic_file_aio_read,
  335. .aio_write = generic_file_aio_write,
  336. .readv = generic_file_readv,
  337. .writev = generic_file_writev,
  338. .write = generic_file_write,
  339. .mmap = generic_file_mmap,
  340. .open = hostfs_file_open,
  341. .release = NULL,
  342. .fsync = hostfs_fsync,
  343. };
  344. static struct file_operations hostfs_dir_fops = {
  345. .llseek = generic_file_llseek,
  346. .readdir = hostfs_readdir,
  347. .read = generic_read_dir,
  348. };
  349. int hostfs_writepage(struct page *page, struct writeback_control *wbc)
  350. {
  351. struct address_space *mapping = page->mapping;
  352. struct inode *inode = mapping->host;
  353. char *buffer;
  354. unsigned long long base;
  355. int count = PAGE_CACHE_SIZE;
  356. int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
  357. int err;
  358. if (page->index >= end_index)
  359. count = inode->i_size & (PAGE_CACHE_SIZE-1);
  360. buffer = kmap(page);
  361. base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
  362. err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
  363. if(err != count){
  364. ClearPageUptodate(page);
  365. goto out;
  366. }
  367. if (base > inode->i_size)
  368. inode->i_size = base;
  369. if (PageError(page))
  370. ClearPageError(page);
  371. err = 0;
  372. out:
  373. kunmap(page);
  374. unlock_page(page);
  375. return err;
  376. }
  377. int hostfs_readpage(struct file *file, struct page *page)
  378. {
  379. char *buffer;
  380. long long start;
  381. int err = 0;
  382. start = (long long) page->index << PAGE_CACHE_SHIFT;
  383. buffer = kmap(page);
  384. err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
  385. PAGE_CACHE_SIZE);
  386. if(err < 0) goto out;
  387. memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
  388. flush_dcache_page(page);
  389. SetPageUptodate(page);
  390. if (PageError(page)) ClearPageError(page);
  391. err = 0;
  392. out:
  393. kunmap(page);
  394. unlock_page(page);
  395. return(err);
  396. }
  397. int hostfs_prepare_write(struct file *file, struct page *page,
  398. unsigned int from, unsigned int to)
  399. {
  400. char *buffer;
  401. long long start, tmp;
  402. int err;
  403. start = (long long) page->index << PAGE_CACHE_SHIFT;
  404. buffer = kmap(page);
  405. if(from != 0){
  406. tmp = start;
  407. err = read_file(FILE_HOSTFS_I(file)->fd, &tmp, buffer,
  408. from);
  409. if(err < 0) goto out;
  410. }
  411. if(to != PAGE_CACHE_SIZE){
  412. start += to;
  413. err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer + to,
  414. PAGE_CACHE_SIZE - to);
  415. if(err < 0) goto out;
  416. }
  417. err = 0;
  418. out:
  419. kunmap(page);
  420. return(err);
  421. }
  422. int hostfs_commit_write(struct file *file, struct page *page, unsigned from,
  423. unsigned to)
  424. {
  425. struct address_space *mapping = page->mapping;
  426. struct inode *inode = mapping->host;
  427. char *buffer;
  428. long long start;
  429. int err = 0;
  430. start = (long long) (page->index << PAGE_CACHE_SHIFT) + from;
  431. buffer = kmap(page);
  432. err = write_file(FILE_HOSTFS_I(file)->fd, &start, buffer + from,
  433. to - from);
  434. if(err > 0) err = 0;
  435. if(!err && (start > inode->i_size))
  436. inode->i_size = start;
  437. kunmap(page);
  438. return(err);
  439. }
  440. static struct address_space_operations hostfs_aops = {
  441. .writepage = hostfs_writepage,
  442. .readpage = hostfs_readpage,
  443. .set_page_dirty = __set_page_dirty_nobuffers,
  444. .prepare_write = hostfs_prepare_write,
  445. .commit_write = hostfs_commit_write
  446. };
  447. static int init_inode(struct inode *inode, struct dentry *dentry)
  448. {
  449. char *name;
  450. int type, err = -ENOMEM;
  451. int maj, min;
  452. dev_t rdev = 0;
  453. if(dentry){
  454. name = dentry_name(dentry, 0);
  455. if(name == NULL)
  456. goto out;
  457. type = file_type(name, &maj, &min);
  458. /*Reencode maj and min with the kernel encoding.*/
  459. rdev = MKDEV(maj, min);
  460. kfree(name);
  461. }
  462. else type = OS_TYPE_DIR;
  463. err = 0;
  464. if(type == OS_TYPE_SYMLINK)
  465. inode->i_op = &page_symlink_inode_operations;
  466. else if(type == OS_TYPE_DIR)
  467. inode->i_op = &hostfs_dir_iops;
  468. else inode->i_op = &hostfs_iops;
  469. if(type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
  470. else inode->i_fop = &hostfs_file_fops;
  471. if(type == OS_TYPE_SYMLINK)
  472. inode->i_mapping->a_ops = &hostfs_link_aops;
  473. else inode->i_mapping->a_ops = &hostfs_aops;
  474. switch (type) {
  475. case OS_TYPE_CHARDEV:
  476. init_special_inode(inode, S_IFCHR, rdev);
  477. break;
  478. case OS_TYPE_BLOCKDEV:
  479. init_special_inode(inode, S_IFBLK, rdev);
  480. break;
  481. case OS_TYPE_FIFO:
  482. init_special_inode(inode, S_IFIFO, 0);
  483. break;
  484. case OS_TYPE_SOCK:
  485. init_special_inode(inode, S_IFSOCK, 0);
  486. break;
  487. }
  488. out:
  489. return(err);
  490. }
  491. int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
  492. struct nameidata *nd)
  493. {
  494. struct inode *inode;
  495. char *name;
  496. int error, fd;
  497. error = -ENOMEM;
  498. inode = iget(dir->i_sb, 0);
  499. if(inode == NULL) goto out;
  500. error = init_inode(inode, dentry);
  501. if(error)
  502. goto out_put;
  503. error = -ENOMEM;
  504. name = dentry_name(dentry, 0);
  505. if(name == NULL)
  506. goto out_put;
  507. fd = file_create(name,
  508. mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
  509. mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
  510. mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
  511. if(fd < 0)
  512. error = fd;
  513. else error = read_name(inode, name);
  514. kfree(name);
  515. if(error)
  516. goto out_put;
  517. HOSTFS_I(inode)->fd = fd;
  518. HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
  519. d_instantiate(dentry, inode);
  520. return(0);
  521. out_put:
  522. iput(inode);
  523. out:
  524. return(error);
  525. }
  526. struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
  527. struct nameidata *nd)
  528. {
  529. struct inode *inode;
  530. char *name;
  531. int err;
  532. err = -ENOMEM;
  533. inode = iget(ino->i_sb, 0);
  534. if(inode == NULL)
  535. goto out;
  536. err = init_inode(inode, dentry);
  537. if(err)
  538. goto out_put;
  539. err = -ENOMEM;
  540. name = dentry_name(dentry, 0);
  541. if(name == NULL)
  542. goto out_put;
  543. err = read_name(inode, name);
  544. kfree(name);
  545. if(err == -ENOENT){
  546. iput(inode);
  547. inode = NULL;
  548. }
  549. else if(err)
  550. goto out_put;
  551. d_add(dentry, inode);
  552. dentry->d_op = &hostfs_dentry_ops;
  553. return(NULL);
  554. out_put:
  555. iput(inode);
  556. out:
  557. return(ERR_PTR(err));
  558. }
  559. static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
  560. {
  561. char *file;
  562. int len;
  563. file = inode_name(ino, dentry->d_name.len + 1);
  564. if(file == NULL) return(NULL);
  565. strcat(file, "/");
  566. len = strlen(file);
  567. strncat(file, dentry->d_name.name, dentry->d_name.len);
  568. file[len + dentry->d_name.len] = '\0';
  569. return(file);
  570. }
  571. int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
  572. {
  573. char *from_name, *to_name;
  574. int err;
  575. if((from_name = inode_dentry_name(ino, from)) == NULL)
  576. return(-ENOMEM);
  577. to_name = dentry_name(to, 0);
  578. if(to_name == NULL){
  579. kfree(from_name);
  580. return(-ENOMEM);
  581. }
  582. err = link_file(to_name, from_name);
  583. kfree(from_name);
  584. kfree(to_name);
  585. return(err);
  586. }
  587. int hostfs_unlink(struct inode *ino, struct dentry *dentry)
  588. {
  589. char *file;
  590. int err;
  591. if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
  592. if(append)
  593. return(-EPERM);
  594. err = unlink_file(file);
  595. kfree(file);
  596. return(err);
  597. }
  598. int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
  599. {
  600. char *file;
  601. int err;
  602. if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
  603. err = make_symlink(file, to);
  604. kfree(file);
  605. return(err);
  606. }
  607. int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
  608. {
  609. char *file;
  610. int err;
  611. if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
  612. err = do_mkdir(file, mode);
  613. kfree(file);
  614. return(err);
  615. }
  616. int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
  617. {
  618. char *file;
  619. int err;
  620. if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
  621. err = do_rmdir(file);
  622. kfree(file);
  623. return(err);
  624. }
  625. int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  626. {
  627. struct inode *inode;
  628. char *name;
  629. int err = -ENOMEM;
  630. inode = iget(dir->i_sb, 0);
  631. if(inode == NULL)
  632. goto out;
  633. err = init_inode(inode, dentry);
  634. if(err)
  635. goto out_put;
  636. err = -ENOMEM;
  637. name = dentry_name(dentry, 0);
  638. if(name == NULL)
  639. goto out_put;
  640. init_special_inode(inode, mode, dev);
  641. err = do_mknod(name, mode, dev);
  642. if(err)
  643. goto out_free;
  644. err = read_name(inode, name);
  645. kfree(name);
  646. if(err)
  647. goto out_put;
  648. d_instantiate(dentry, inode);
  649. return(0);
  650. out_free:
  651. kfree(name);
  652. out_put:
  653. iput(inode);
  654. out:
  655. return(err);
  656. }
  657. int hostfs_rename(struct inode *from_ino, struct dentry *from,
  658. struct inode *to_ino, struct dentry *to)
  659. {
  660. char *from_name, *to_name;
  661. int err;
  662. if((from_name = inode_dentry_name(from_ino, from)) == NULL)
  663. return(-ENOMEM);
  664. if((to_name = inode_dentry_name(to_ino, to)) == NULL){
  665. kfree(from_name);
  666. return(-ENOMEM);
  667. }
  668. err = rename_file(from_name, to_name);
  669. kfree(from_name);
  670. kfree(to_name);
  671. return(err);
  672. }
  673. int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd)
  674. {
  675. char *name;
  676. int r = 0, w = 0, x = 0, err;
  677. if (desired & MAY_READ) r = 1;
  678. if (desired & MAY_WRITE) w = 1;
  679. if (desired & MAY_EXEC) x = 1;
  680. name = inode_name(ino, 0);
  681. if (name == NULL) return(-ENOMEM);
  682. if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
  683. S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
  684. err = 0;
  685. else
  686. err = access_file(name, r, w, x);
  687. kfree(name);
  688. if(!err)
  689. err = generic_permission(ino, desired, NULL);
  690. return err;
  691. }
  692. int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
  693. {
  694. struct hostfs_iattr attrs;
  695. char *name;
  696. int err;
  697. err = inode_change_ok(dentry->d_inode, attr);
  698. if (err)
  699. return err;
  700. if(append)
  701. attr->ia_valid &= ~ATTR_SIZE;
  702. attrs.ia_valid = 0;
  703. if(attr->ia_valid & ATTR_MODE){
  704. attrs.ia_valid |= HOSTFS_ATTR_MODE;
  705. attrs.ia_mode = attr->ia_mode;
  706. }
  707. if(attr->ia_valid & ATTR_UID){
  708. attrs.ia_valid |= HOSTFS_ATTR_UID;
  709. attrs.ia_uid = attr->ia_uid;
  710. }
  711. if(attr->ia_valid & ATTR_GID){
  712. attrs.ia_valid |= HOSTFS_ATTR_GID;
  713. attrs.ia_gid = attr->ia_gid;
  714. }
  715. if(attr->ia_valid & ATTR_SIZE){
  716. attrs.ia_valid |= HOSTFS_ATTR_SIZE;
  717. attrs.ia_size = attr->ia_size;
  718. }
  719. if(attr->ia_valid & ATTR_ATIME){
  720. attrs.ia_valid |= HOSTFS_ATTR_ATIME;
  721. attrs.ia_atime = attr->ia_atime;
  722. }
  723. if(attr->ia_valid & ATTR_MTIME){
  724. attrs.ia_valid |= HOSTFS_ATTR_MTIME;
  725. attrs.ia_mtime = attr->ia_mtime;
  726. }
  727. if(attr->ia_valid & ATTR_CTIME){
  728. attrs.ia_valid |= HOSTFS_ATTR_CTIME;
  729. attrs.ia_ctime = attr->ia_ctime;
  730. }
  731. if(attr->ia_valid & ATTR_ATIME_SET){
  732. attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
  733. }
  734. if(attr->ia_valid & ATTR_MTIME_SET){
  735. attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
  736. }
  737. name = dentry_name(dentry, 0);
  738. if(name == NULL) return(-ENOMEM);
  739. err = set_attr(name, &attrs);
  740. kfree(name);
  741. if(err)
  742. return(err);
  743. return(inode_setattr(dentry->d_inode, attr));
  744. }
  745. int hostfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  746. struct kstat *stat)
  747. {
  748. generic_fillattr(dentry->d_inode, stat);
  749. return(0);
  750. }
  751. static struct inode_operations hostfs_iops = {
  752. .create = hostfs_create,
  753. .link = hostfs_link,
  754. .unlink = hostfs_unlink,
  755. .symlink = hostfs_symlink,
  756. .mkdir = hostfs_mkdir,
  757. .rmdir = hostfs_rmdir,
  758. .mknod = hostfs_mknod,
  759. .rename = hostfs_rename,
  760. .permission = hostfs_permission,
  761. .setattr = hostfs_setattr,
  762. .getattr = hostfs_getattr,
  763. };
  764. static struct inode_operations hostfs_dir_iops = {
  765. .create = hostfs_create,
  766. .lookup = hostfs_lookup,
  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. .permission = hostfs_permission,
  775. .setattr = hostfs_setattr,
  776. .getattr = hostfs_getattr,
  777. };
  778. int hostfs_link_readpage(struct file *file, struct page *page)
  779. {
  780. char *buffer, *name;
  781. long long start;
  782. int err;
  783. start = page->index << PAGE_CACHE_SHIFT;
  784. buffer = kmap(page);
  785. name = inode_name(page->mapping->host, 0);
  786. if(name == NULL) return(-ENOMEM);
  787. err = do_readlink(name, buffer, PAGE_CACHE_SIZE);
  788. kfree(name);
  789. if(err == PAGE_CACHE_SIZE)
  790. err = -E2BIG;
  791. else if(err > 0){
  792. flush_dcache_page(page);
  793. SetPageUptodate(page);
  794. if (PageError(page)) ClearPageError(page);
  795. err = 0;
  796. }
  797. kunmap(page);
  798. unlock_page(page);
  799. return(err);
  800. }
  801. static struct address_space_operations hostfs_link_aops = {
  802. .readpage = hostfs_link_readpage,
  803. };
  804. static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
  805. {
  806. struct inode *root_inode;
  807. char *name, *data = d;
  808. int err;
  809. sb->s_blocksize = 1024;
  810. sb->s_blocksize_bits = 10;
  811. sb->s_magic = HOSTFS_SUPER_MAGIC;
  812. sb->s_op = &hostfs_sbops;
  813. if((data == NULL) || (*data == '\0'))
  814. data = root_ino;
  815. err = -ENOMEM;
  816. name = kmalloc(strlen(data) + 1, GFP_KERNEL);
  817. if(name == NULL)
  818. goto out;
  819. strcpy(name, data);
  820. root_inode = iget(sb, 0);
  821. if(root_inode == NULL)
  822. goto out_free;
  823. err = init_inode(root_inode, NULL);
  824. if(err)
  825. goto out_put;
  826. HOSTFS_I(root_inode)->host_filename = name;
  827. err = -ENOMEM;
  828. sb->s_root = d_alloc_root(root_inode);
  829. if(sb->s_root == NULL)
  830. goto out_put;
  831. err = read_inode(root_inode);
  832. if(err){
  833. /* No iput in this case because the dput does that for us */
  834. dput(sb->s_root);
  835. sb->s_root = NULL;
  836. goto out_free;
  837. }
  838. return(0);
  839. out_put:
  840. iput(root_inode);
  841. out_free:
  842. kfree(name);
  843. out:
  844. return(err);
  845. }
  846. static struct super_block *hostfs_read_sb(struct file_system_type *type,
  847. int flags, const char *dev_name,
  848. void *data)
  849. {
  850. return(get_sb_nodev(type, flags, data, hostfs_fill_sb_common));
  851. }
  852. static struct file_system_type hostfs_type = {
  853. .owner = THIS_MODULE,
  854. .name = "hostfs",
  855. .get_sb = hostfs_read_sb,
  856. .kill_sb = kill_anon_super,
  857. .fs_flags = 0,
  858. };
  859. static int __init init_hostfs(void)
  860. {
  861. return(register_filesystem(&hostfs_type));
  862. }
  863. static void __exit exit_hostfs(void)
  864. {
  865. unregister_filesystem(&hostfs_type);
  866. }
  867. module_init(init_hostfs)
  868. module_exit(exit_hostfs)
  869. MODULE_LICENSE("GPL");
  870. /*
  871. * Overrides for Emacs so that we follow Linus's tabbing style.
  872. * Emacs will notice this stuff at the end of the file and automatically
  873. * adjust the settings for this buffer only. This must remain at the end
  874. * of the file.
  875. * ---------------------------------------------------------------------------
  876. * Local variables:
  877. * c-file-style: "linux"
  878. * End:
  879. */