hostfs_kern.c 22 KB

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