hostfs_kern.c 22 KB

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