hostfs_kern.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040
  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. if(HOSTFS_I(inode)->host_filename)
  255. kfree(HOSTFS_I(inode)->host_filename);
  256. /*XXX: This should not happen, probably. The check is here for
  257. * additional safety.*/
  258. if(HOSTFS_I(inode)->fd != -1) {
  259. close_file(&HOSTFS_I(inode)->fd);
  260. printk(KERN_DEBUG "Closing host fd in .destroy_inode\n");
  261. }
  262. kfree(HOSTFS_I(inode));
  263. }
  264. static void hostfs_read_inode(struct inode *inode)
  265. {
  266. read_inode(inode);
  267. }
  268. static struct super_operations hostfs_sbops = {
  269. .alloc_inode = hostfs_alloc_inode,
  270. .drop_inode = generic_delete_inode,
  271. .delete_inode = hostfs_delete_inode,
  272. .destroy_inode = hostfs_destroy_inode,
  273. .read_inode = hostfs_read_inode,
  274. .statfs = hostfs_statfs,
  275. };
  276. int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
  277. {
  278. void *dir;
  279. char *name;
  280. unsigned long long next, ino;
  281. int error, len;
  282. name = dentry_name(file->f_dentry, 0);
  283. if(name == NULL) return(-ENOMEM);
  284. dir = open_dir(name, &error);
  285. kfree(name);
  286. if(dir == NULL) return(-error);
  287. next = file->f_pos;
  288. while((name = read_dir(dir, &next, &ino, &len)) != NULL){
  289. error = (*filldir)(ent, name, len, file->f_pos,
  290. ino, DT_UNKNOWN);
  291. if(error) break;
  292. file->f_pos = next;
  293. }
  294. close_dir(dir);
  295. return(0);
  296. }
  297. int hostfs_file_open(struct inode *ino, struct file *file)
  298. {
  299. char *name;
  300. int mode = 0, r = 0, w = 0, fd;
  301. mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
  302. if((mode & HOSTFS_I(ino)->mode) == mode)
  303. return(0);
  304. /* The file may already have been opened, but with the wrong access,
  305. * so this resets things and reopens the file with the new access.
  306. */
  307. if(HOSTFS_I(ino)->fd != -1){
  308. close_file(&HOSTFS_I(ino)->fd);
  309. HOSTFS_I(ino)->fd = -1;
  310. }
  311. HOSTFS_I(ino)->mode |= mode;
  312. if(HOSTFS_I(ino)->mode & FMODE_READ)
  313. r = 1;
  314. if(HOSTFS_I(ino)->mode & FMODE_WRITE)
  315. w = 1;
  316. if(w)
  317. r = 1;
  318. name = dentry_name(file->f_dentry, 0);
  319. if(name == NULL)
  320. return(-ENOMEM);
  321. fd = open_file(name, r, w, append);
  322. kfree(name);
  323. if(fd < 0) return(fd);
  324. FILE_HOSTFS_I(file)->fd = fd;
  325. return(0);
  326. }
  327. int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync)
  328. {
  329. return fsync_file(HOSTFS_I(dentry->d_inode)->fd, datasync);
  330. }
  331. static struct file_operations hostfs_file_fops = {
  332. .llseek = generic_file_llseek,
  333. .read = generic_file_read,
  334. .sendfile = generic_file_sendfile,
  335. .aio_read = generic_file_aio_read,
  336. .aio_write = generic_file_aio_write,
  337. .readv = generic_file_readv,
  338. .writev = generic_file_writev,
  339. .write = generic_file_write,
  340. .mmap = generic_file_mmap,
  341. .open = hostfs_file_open,
  342. .release = NULL,
  343. .fsync = hostfs_fsync,
  344. };
  345. static struct file_operations hostfs_dir_fops = {
  346. .llseek = generic_file_llseek,
  347. .readdir = hostfs_readdir,
  348. .read = generic_read_dir,
  349. };
  350. int hostfs_writepage(struct page *page, struct writeback_control *wbc)
  351. {
  352. struct address_space *mapping = page->mapping;
  353. struct inode *inode = mapping->host;
  354. char *buffer;
  355. unsigned long long base;
  356. int count = PAGE_CACHE_SIZE;
  357. int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
  358. int err;
  359. if (page->index >= end_index)
  360. count = inode->i_size & (PAGE_CACHE_SIZE-1);
  361. buffer = kmap(page);
  362. base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
  363. err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
  364. if(err != count){
  365. ClearPageUptodate(page);
  366. goto out;
  367. }
  368. if (base > inode->i_size)
  369. inode->i_size = base;
  370. if (PageError(page))
  371. ClearPageError(page);
  372. err = 0;
  373. out:
  374. kunmap(page);
  375. unlock_page(page);
  376. return err;
  377. }
  378. int hostfs_readpage(struct file *file, struct page *page)
  379. {
  380. char *buffer;
  381. long long start;
  382. int err = 0;
  383. start = (long long) page->index << PAGE_CACHE_SHIFT;
  384. buffer = kmap(page);
  385. err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
  386. PAGE_CACHE_SIZE);
  387. if(err < 0) goto out;
  388. memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
  389. flush_dcache_page(page);
  390. SetPageUptodate(page);
  391. if (PageError(page)) ClearPageError(page);
  392. err = 0;
  393. out:
  394. kunmap(page);
  395. unlock_page(page);
  396. return(err);
  397. }
  398. int hostfs_prepare_write(struct file *file, struct page *page,
  399. unsigned int from, unsigned int to)
  400. {
  401. char *buffer;
  402. long long start, tmp;
  403. int err;
  404. start = (long long) page->index << PAGE_CACHE_SHIFT;
  405. buffer = kmap(page);
  406. if(from != 0){
  407. tmp = start;
  408. err = read_file(FILE_HOSTFS_I(file)->fd, &tmp, buffer,
  409. from);
  410. if(err < 0) goto out;
  411. }
  412. if(to != PAGE_CACHE_SIZE){
  413. start += to;
  414. err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer + to,
  415. PAGE_CACHE_SIZE - to);
  416. if(err < 0) goto out;
  417. }
  418. err = 0;
  419. out:
  420. kunmap(page);
  421. return(err);
  422. }
  423. int hostfs_commit_write(struct file *file, struct page *page, unsigned from,
  424. unsigned to)
  425. {
  426. struct address_space *mapping = page->mapping;
  427. struct inode *inode = mapping->host;
  428. char *buffer;
  429. long long start;
  430. int err = 0;
  431. start = (long long) (page->index << PAGE_CACHE_SHIFT) + from;
  432. buffer = kmap(page);
  433. err = write_file(FILE_HOSTFS_I(file)->fd, &start, buffer + from,
  434. to - from);
  435. if(err > 0) err = 0;
  436. if(!err && (start > inode->i_size))
  437. inode->i_size = start;
  438. kunmap(page);
  439. return(err);
  440. }
  441. static 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. void hostfs_truncate(struct inode *ino)
  675. {
  676. not_implemented();
  677. }
  678. int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd)
  679. {
  680. char *name;
  681. int r = 0, w = 0, x = 0, err;
  682. if (desired & MAY_READ) r = 1;
  683. if (desired & MAY_WRITE) w = 1;
  684. if (desired & MAY_EXEC) x = 1;
  685. name = inode_name(ino, 0);
  686. if (name == NULL) return(-ENOMEM);
  687. if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
  688. S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
  689. err = 0;
  690. else
  691. err = access_file(name, r, w, x);
  692. kfree(name);
  693. if(!err)
  694. err = generic_permission(ino, desired, NULL);
  695. return err;
  696. }
  697. int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
  698. {
  699. struct hostfs_iattr attrs;
  700. char *name;
  701. int err;
  702. err = inode_change_ok(dentry->d_inode, attr);
  703. if (err)
  704. return err;
  705. if(append)
  706. attr->ia_valid &= ~ATTR_SIZE;
  707. attrs.ia_valid = 0;
  708. if(attr->ia_valid & ATTR_MODE){
  709. attrs.ia_valid |= HOSTFS_ATTR_MODE;
  710. attrs.ia_mode = attr->ia_mode;
  711. }
  712. if(attr->ia_valid & ATTR_UID){
  713. attrs.ia_valid |= HOSTFS_ATTR_UID;
  714. attrs.ia_uid = attr->ia_uid;
  715. }
  716. if(attr->ia_valid & ATTR_GID){
  717. attrs.ia_valid |= HOSTFS_ATTR_GID;
  718. attrs.ia_gid = attr->ia_gid;
  719. }
  720. if(attr->ia_valid & ATTR_SIZE){
  721. attrs.ia_valid |= HOSTFS_ATTR_SIZE;
  722. attrs.ia_size = attr->ia_size;
  723. }
  724. if(attr->ia_valid & ATTR_ATIME){
  725. attrs.ia_valid |= HOSTFS_ATTR_ATIME;
  726. attrs.ia_atime = attr->ia_atime;
  727. }
  728. if(attr->ia_valid & ATTR_MTIME){
  729. attrs.ia_valid |= HOSTFS_ATTR_MTIME;
  730. attrs.ia_mtime = attr->ia_mtime;
  731. }
  732. if(attr->ia_valid & ATTR_CTIME){
  733. attrs.ia_valid |= HOSTFS_ATTR_CTIME;
  734. attrs.ia_ctime = attr->ia_ctime;
  735. }
  736. if(attr->ia_valid & ATTR_ATIME_SET){
  737. attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
  738. }
  739. if(attr->ia_valid & ATTR_MTIME_SET){
  740. attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
  741. }
  742. name = dentry_name(dentry, 0);
  743. if(name == NULL) return(-ENOMEM);
  744. err = set_attr(name, &attrs);
  745. kfree(name);
  746. if(err)
  747. return(err);
  748. return(inode_setattr(dentry->d_inode, attr));
  749. }
  750. int hostfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  751. struct kstat *stat)
  752. {
  753. generic_fillattr(dentry->d_inode, stat);
  754. return(0);
  755. }
  756. static struct inode_operations hostfs_iops = {
  757. .create = hostfs_create,
  758. .link = hostfs_link,
  759. .unlink = hostfs_unlink,
  760. .symlink = hostfs_symlink,
  761. .mkdir = hostfs_mkdir,
  762. .rmdir = hostfs_rmdir,
  763. .mknod = hostfs_mknod,
  764. .rename = hostfs_rename,
  765. .truncate = hostfs_truncate,
  766. .permission = hostfs_permission,
  767. .setattr = hostfs_setattr,
  768. .getattr = hostfs_getattr,
  769. };
  770. static struct inode_operations hostfs_dir_iops = {
  771. .create = hostfs_create,
  772. .lookup = hostfs_lookup,
  773. .link = hostfs_link,
  774. .unlink = hostfs_unlink,
  775. .symlink = hostfs_symlink,
  776. .mkdir = hostfs_mkdir,
  777. .rmdir = hostfs_rmdir,
  778. .mknod = hostfs_mknod,
  779. .rename = hostfs_rename,
  780. .truncate = hostfs_truncate,
  781. .permission = hostfs_permission,
  782. .setattr = hostfs_setattr,
  783. .getattr = hostfs_getattr,
  784. };
  785. int hostfs_link_readpage(struct file *file, struct page *page)
  786. {
  787. char *buffer, *name;
  788. long long start;
  789. int err;
  790. start = page->index << PAGE_CACHE_SHIFT;
  791. buffer = kmap(page);
  792. name = inode_name(page->mapping->host, 0);
  793. if(name == NULL) return(-ENOMEM);
  794. err = do_readlink(name, buffer, PAGE_CACHE_SIZE);
  795. kfree(name);
  796. if(err == PAGE_CACHE_SIZE)
  797. err = -E2BIG;
  798. else if(err > 0){
  799. flush_dcache_page(page);
  800. SetPageUptodate(page);
  801. if (PageError(page)) ClearPageError(page);
  802. err = 0;
  803. }
  804. kunmap(page);
  805. unlock_page(page);
  806. return(err);
  807. }
  808. static struct address_space_operations hostfs_link_aops = {
  809. .readpage = hostfs_link_readpage,
  810. };
  811. static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
  812. {
  813. struct inode *root_inode;
  814. char *name, *data = d;
  815. int err;
  816. sb->s_blocksize = 1024;
  817. sb->s_blocksize_bits = 10;
  818. sb->s_magic = HOSTFS_SUPER_MAGIC;
  819. sb->s_op = &hostfs_sbops;
  820. if((data == NULL) || (*data == '\0'))
  821. data = root_ino;
  822. err = -ENOMEM;
  823. name = kmalloc(strlen(data) + 1, GFP_KERNEL);
  824. if(name == NULL)
  825. goto out;
  826. strcpy(name, data);
  827. root_inode = iget(sb, 0);
  828. if(root_inode == NULL)
  829. goto out_free;
  830. err = init_inode(root_inode, NULL);
  831. if(err)
  832. goto out_put;
  833. HOSTFS_I(root_inode)->host_filename = name;
  834. err = -ENOMEM;
  835. sb->s_root = d_alloc_root(root_inode);
  836. if(sb->s_root == NULL)
  837. goto out_put;
  838. err = read_inode(root_inode);
  839. if(err){
  840. /* No iput in this case because the dput does that for us */
  841. dput(sb->s_root);
  842. sb->s_root = NULL;
  843. goto out_free;
  844. }
  845. return(0);
  846. out_put:
  847. iput(root_inode);
  848. out_free:
  849. kfree(name);
  850. out:
  851. return(err);
  852. }
  853. static struct super_block *hostfs_read_sb(struct file_system_type *type,
  854. int flags, const char *dev_name,
  855. void *data)
  856. {
  857. return(get_sb_nodev(type, flags, data, hostfs_fill_sb_common));
  858. }
  859. static struct file_system_type hostfs_type = {
  860. .owner = THIS_MODULE,
  861. .name = "hostfs",
  862. .get_sb = hostfs_read_sb,
  863. .kill_sb = kill_anon_super,
  864. .fs_flags = 0,
  865. };
  866. static int __init init_hostfs(void)
  867. {
  868. return(register_filesystem(&hostfs_type));
  869. }
  870. static void __exit exit_hostfs(void)
  871. {
  872. unregister_filesystem(&hostfs_type);
  873. }
  874. module_init(init_hostfs)
  875. module_exit(exit_hostfs)
  876. MODULE_LICENSE("GPL");
  877. /*
  878. * Overrides for Emacs so that we follow Linus's tabbing style.
  879. * Emacs will notice this stuff at the end of the file and automatically
  880. * adjust the settings for this buffer only. This must remain at the end
  881. * of the file.
  882. * ---------------------------------------------------------------------------
  883. * Local variables:
  884. * c-file-style: "linux"
  885. * End:
  886. */