hostfs_kern.c 22 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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 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_blksize = i_blksize;
  138. ino->i_blocks = i_blocks;
  139. return(0);
  140. }
  141. static char *follow_link(char *link)
  142. {
  143. int len, n;
  144. char *name, *resolved, *end;
  145. len = 64;
  146. while(1){
  147. n = -ENOMEM;
  148. name = kmalloc(len, GFP_KERNEL);
  149. if(name == NULL)
  150. goto out;
  151. n = do_readlink(link, name, len);
  152. if(n < len)
  153. break;
  154. len *= 2;
  155. kfree(name);
  156. }
  157. if(n < 0)
  158. goto out_free;
  159. if(*name == '/')
  160. return(name);
  161. end = strrchr(link, '/');
  162. if(end == NULL)
  163. return(name);
  164. *(end + 1) = '\0';
  165. len = strlen(link) + strlen(name) + 1;
  166. resolved = kmalloc(len, GFP_KERNEL);
  167. if(resolved == NULL){
  168. n = -ENOMEM;
  169. goto out_free;
  170. }
  171. sprintf(resolved, "%s%s", link, name);
  172. kfree(name);
  173. kfree(link);
  174. return(resolved);
  175. out_free:
  176. kfree(name);
  177. out:
  178. return(ERR_PTR(n));
  179. }
  180. static int read_inode(struct inode *ino)
  181. {
  182. char *name;
  183. int err = 0;
  184. /* Unfortunately, we are called from iget() when we don't have a dentry
  185. * allocated yet.
  186. */
  187. if(list_empty(&ino->i_dentry))
  188. goto out;
  189. err = -ENOMEM;
  190. name = inode_name(ino, 0);
  191. if(name == NULL)
  192. goto out;
  193. if(file_type(name, NULL, NULL) == OS_TYPE_SYMLINK){
  194. name = follow_link(name);
  195. if(IS_ERR(name)){
  196. err = PTR_ERR(name);
  197. goto out;
  198. }
  199. }
  200. err = read_name(ino, name);
  201. kfree(name);
  202. out:
  203. return(err);
  204. }
  205. int hostfs_statfs(struct super_block *sb, struct kstatfs *sf)
  206. {
  207. /* do_statfs uses struct statfs64 internally, but the linux kernel
  208. * struct statfs still has 32-bit versions for most of these fields,
  209. * so we convert them here
  210. */
  211. int err;
  212. long long f_blocks;
  213. long long f_bfree;
  214. long long f_bavail;
  215. long long f_files;
  216. long long f_ffree;
  217. err = do_statfs(HOSTFS_I(sb->s_root->d_inode)->host_filename,
  218. &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
  219. &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
  220. &sf->f_namelen, sf->f_spare);
  221. if(err) return(err);
  222. sf->f_blocks = f_blocks;
  223. sf->f_bfree = f_bfree;
  224. sf->f_bavail = f_bavail;
  225. sf->f_files = f_files;
  226. sf->f_ffree = f_ffree;
  227. sf->f_type = HOSTFS_SUPER_MAGIC;
  228. return(0);
  229. }
  230. static struct inode *hostfs_alloc_inode(struct super_block *sb)
  231. {
  232. struct hostfs_inode_info *hi;
  233. hi = kmalloc(sizeof(*hi), GFP_KERNEL);
  234. if(hi == NULL)
  235. return(NULL);
  236. *hi = ((struct hostfs_inode_info) { .host_filename = NULL,
  237. .fd = -1,
  238. .mode = 0 });
  239. inode_init_once(&hi->vfs_inode);
  240. return(&hi->vfs_inode);
  241. }
  242. static void hostfs_delete_inode(struct inode *inode)
  243. {
  244. truncate_inode_pages(&inode->i_data, 0);
  245. if(HOSTFS_I(inode)->fd != -1) {
  246. close_file(&HOSTFS_I(inode)->fd);
  247. HOSTFS_I(inode)->fd = -1;
  248. }
  249. clear_inode(inode);
  250. }
  251. static void hostfs_destroy_inode(struct inode *inode)
  252. {
  253. kfree(HOSTFS_I(inode)->host_filename);
  254. /*XXX: This should not happen, probably. The check is here for
  255. * additional safety.*/
  256. if(HOSTFS_I(inode)->fd != -1) {
  257. close_file(&HOSTFS_I(inode)->fd);
  258. printk(KERN_DEBUG "Closing host fd in .destroy_inode\n");
  259. }
  260. kfree(HOSTFS_I(inode));
  261. }
  262. static void hostfs_read_inode(struct inode *inode)
  263. {
  264. read_inode(inode);
  265. }
  266. static struct super_operations hostfs_sbops = {
  267. .alloc_inode = hostfs_alloc_inode,
  268. .drop_inode = generic_delete_inode,
  269. .delete_inode = hostfs_delete_inode,
  270. .destroy_inode = hostfs_destroy_inode,
  271. .read_inode = hostfs_read_inode,
  272. .statfs = hostfs_statfs,
  273. };
  274. int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
  275. {
  276. void *dir;
  277. char *name;
  278. unsigned long long next, ino;
  279. int error, len;
  280. name = dentry_name(file->f_dentry, 0);
  281. if(name == NULL) return(-ENOMEM);
  282. dir = open_dir(name, &error);
  283. kfree(name);
  284. if(dir == NULL) return(-error);
  285. next = file->f_pos;
  286. while((name = read_dir(dir, &next, &ino, &len)) != NULL){
  287. error = (*filldir)(ent, name, len, file->f_pos,
  288. ino, DT_UNKNOWN);
  289. if(error) break;
  290. file->f_pos = next;
  291. }
  292. close_dir(dir);
  293. return(0);
  294. }
  295. int hostfs_file_open(struct inode *ino, struct file *file)
  296. {
  297. char *name;
  298. int mode = 0, r = 0, w = 0, fd;
  299. mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
  300. if((mode & HOSTFS_I(ino)->mode) == mode)
  301. return(0);
  302. /* The file may already have been opened, but with the wrong access,
  303. * so this resets things and reopens the file with the new access.
  304. */
  305. if(HOSTFS_I(ino)->fd != -1){
  306. close_file(&HOSTFS_I(ino)->fd);
  307. HOSTFS_I(ino)->fd = -1;
  308. }
  309. HOSTFS_I(ino)->mode |= mode;
  310. if(HOSTFS_I(ino)->mode & FMODE_READ)
  311. r = 1;
  312. if(HOSTFS_I(ino)->mode & FMODE_WRITE)
  313. w = 1;
  314. if(w)
  315. r = 1;
  316. name = dentry_name(file->f_dentry, 0);
  317. if(name == NULL)
  318. return(-ENOMEM);
  319. fd = open_file(name, r, w, append);
  320. kfree(name);
  321. if(fd < 0) return(fd);
  322. FILE_HOSTFS_I(file)->fd = fd;
  323. return(0);
  324. }
  325. int hostfs_fsync(struct file *file, struct dentry *dentry, int datasync)
  326. {
  327. return fsync_file(HOSTFS_I(dentry->d_inode)->fd, datasync);
  328. }
  329. static struct file_operations hostfs_file_fops = {
  330. .llseek = generic_file_llseek,
  331. .read = generic_file_read,
  332. .sendfile = generic_file_sendfile,
  333. .aio_read = generic_file_aio_read,
  334. .aio_write = generic_file_aio_write,
  335. .readv = generic_file_readv,
  336. .writev = generic_file_writev,
  337. .write = generic_file_write,
  338. .mmap = generic_file_mmap,
  339. .open = hostfs_file_open,
  340. .release = NULL,
  341. .fsync = hostfs_fsync,
  342. };
  343. static struct file_operations hostfs_dir_fops = {
  344. .llseek = generic_file_llseek,
  345. .readdir = hostfs_readdir,
  346. .read = generic_read_dir,
  347. };
  348. int hostfs_writepage(struct page *page, struct writeback_control *wbc)
  349. {
  350. struct address_space *mapping = page->mapping;
  351. struct inode *inode = mapping->host;
  352. char *buffer;
  353. unsigned long long base;
  354. int count = PAGE_CACHE_SIZE;
  355. int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
  356. int err;
  357. if (page->index >= end_index)
  358. count = inode->i_size & (PAGE_CACHE_SIZE-1);
  359. buffer = kmap(page);
  360. base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
  361. err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
  362. if(err != count){
  363. ClearPageUptodate(page);
  364. goto out;
  365. }
  366. if (base > inode->i_size)
  367. inode->i_size = base;
  368. if (PageError(page))
  369. ClearPageError(page);
  370. err = 0;
  371. out:
  372. kunmap(page);
  373. unlock_page(page);
  374. return err;
  375. }
  376. int hostfs_readpage(struct file *file, struct page *page)
  377. {
  378. char *buffer;
  379. long long start;
  380. int err = 0;
  381. start = (long long) page->index << PAGE_CACHE_SHIFT;
  382. buffer = kmap(page);
  383. err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
  384. PAGE_CACHE_SIZE);
  385. if(err < 0) goto out;
  386. memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
  387. flush_dcache_page(page);
  388. SetPageUptodate(page);
  389. if (PageError(page)) ClearPageError(page);
  390. err = 0;
  391. out:
  392. kunmap(page);
  393. unlock_page(page);
  394. return(err);
  395. }
  396. int hostfs_prepare_write(struct file *file, struct page *page,
  397. unsigned int from, unsigned int to)
  398. {
  399. char *buffer;
  400. long long start, tmp;
  401. int err;
  402. start = (long long) page->index << PAGE_CACHE_SHIFT;
  403. buffer = kmap(page);
  404. if(from != 0){
  405. tmp = start;
  406. err = read_file(FILE_HOSTFS_I(file)->fd, &tmp, buffer,
  407. from);
  408. if(err < 0) goto out;
  409. }
  410. if(to != PAGE_CACHE_SIZE){
  411. start += to;
  412. err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer + to,
  413. PAGE_CACHE_SIZE - to);
  414. if(err < 0) goto out;
  415. }
  416. err = 0;
  417. out:
  418. kunmap(page);
  419. return(err);
  420. }
  421. int hostfs_commit_write(struct file *file, struct page *page, unsigned from,
  422. unsigned to)
  423. {
  424. struct address_space *mapping = page->mapping;
  425. struct inode *inode = mapping->host;
  426. char *buffer;
  427. long long start;
  428. int err = 0;
  429. start = (((long long) page->index) << PAGE_CACHE_SHIFT) + from;
  430. buffer = kmap(page);
  431. err = write_file(FILE_HOSTFS_I(file)->fd, &start, buffer + from,
  432. to - from);
  433. if(err > 0) err = 0;
  434. /* Actually, if !err, write_file has added to-from to start, so, despite
  435. * the appearance, we are comparing i_size against the _last_ written
  436. * location, as we should. */
  437. if(!err && (start > inode->i_size))
  438. inode->i_size = start;
  439. kunmap(page);
  440. return(err);
  441. }
  442. static struct address_space_operations hostfs_aops = {
  443. .writepage = hostfs_writepage,
  444. .readpage = hostfs_readpage,
  445. .set_page_dirty = __set_page_dirty_nobuffers,
  446. .prepare_write = hostfs_prepare_write,
  447. .commit_write = hostfs_commit_write
  448. };
  449. static int init_inode(struct inode *inode, struct dentry *dentry)
  450. {
  451. char *name;
  452. int type, err = -ENOMEM;
  453. int maj, min;
  454. dev_t rdev = 0;
  455. if(dentry){
  456. name = dentry_name(dentry, 0);
  457. if(name == NULL)
  458. goto out;
  459. type = file_type(name, &maj, &min);
  460. /*Reencode maj and min with the kernel encoding.*/
  461. rdev = MKDEV(maj, min);
  462. kfree(name);
  463. }
  464. else type = OS_TYPE_DIR;
  465. err = 0;
  466. if(type == OS_TYPE_SYMLINK)
  467. inode->i_op = &page_symlink_inode_operations;
  468. else if(type == OS_TYPE_DIR)
  469. inode->i_op = &hostfs_dir_iops;
  470. else inode->i_op = &hostfs_iops;
  471. if(type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
  472. else inode->i_fop = &hostfs_file_fops;
  473. if(type == OS_TYPE_SYMLINK)
  474. inode->i_mapping->a_ops = &hostfs_link_aops;
  475. else inode->i_mapping->a_ops = &hostfs_aops;
  476. switch (type) {
  477. case OS_TYPE_CHARDEV:
  478. init_special_inode(inode, S_IFCHR, rdev);
  479. break;
  480. case OS_TYPE_BLOCKDEV:
  481. init_special_inode(inode, S_IFBLK, rdev);
  482. break;
  483. case OS_TYPE_FIFO:
  484. init_special_inode(inode, S_IFIFO, 0);
  485. break;
  486. case OS_TYPE_SOCK:
  487. init_special_inode(inode, S_IFSOCK, 0);
  488. break;
  489. }
  490. out:
  491. return(err);
  492. }
  493. int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
  494. struct nameidata *nd)
  495. {
  496. struct inode *inode;
  497. char *name;
  498. int error, fd;
  499. error = -ENOMEM;
  500. inode = iget(dir->i_sb, 0);
  501. if(inode == NULL) goto out;
  502. error = init_inode(inode, dentry);
  503. if(error)
  504. goto out_put;
  505. error = -ENOMEM;
  506. name = dentry_name(dentry, 0);
  507. if(name == NULL)
  508. goto out_put;
  509. fd = file_create(name,
  510. mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
  511. mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
  512. mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
  513. if(fd < 0)
  514. error = fd;
  515. else error = read_name(inode, name);
  516. kfree(name);
  517. if(error)
  518. goto out_put;
  519. HOSTFS_I(inode)->fd = fd;
  520. HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
  521. d_instantiate(dentry, inode);
  522. return(0);
  523. out_put:
  524. iput(inode);
  525. out:
  526. return(error);
  527. }
  528. struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
  529. struct nameidata *nd)
  530. {
  531. struct inode *inode;
  532. char *name;
  533. int err;
  534. err = -ENOMEM;
  535. inode = iget(ino->i_sb, 0);
  536. if(inode == NULL)
  537. goto out;
  538. err = init_inode(inode, dentry);
  539. if(err)
  540. goto out_put;
  541. err = -ENOMEM;
  542. name = dentry_name(dentry, 0);
  543. if(name == NULL)
  544. goto out_put;
  545. err = read_name(inode, name);
  546. kfree(name);
  547. if(err == -ENOENT){
  548. iput(inode);
  549. inode = NULL;
  550. }
  551. else if(err)
  552. goto out_put;
  553. d_add(dentry, inode);
  554. dentry->d_op = &hostfs_dentry_ops;
  555. return(NULL);
  556. out_put:
  557. iput(inode);
  558. out:
  559. return(ERR_PTR(err));
  560. }
  561. static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
  562. {
  563. char *file;
  564. int len;
  565. file = inode_name(ino, dentry->d_name.len + 1);
  566. if(file == NULL) return(NULL);
  567. strcat(file, "/");
  568. len = strlen(file);
  569. strncat(file, dentry->d_name.name, dentry->d_name.len);
  570. file[len + dentry->d_name.len] = '\0';
  571. return(file);
  572. }
  573. int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
  574. {
  575. char *from_name, *to_name;
  576. int err;
  577. if((from_name = inode_dentry_name(ino, from)) == NULL)
  578. return(-ENOMEM);
  579. to_name = dentry_name(to, 0);
  580. if(to_name == NULL){
  581. kfree(from_name);
  582. return(-ENOMEM);
  583. }
  584. err = link_file(to_name, from_name);
  585. kfree(from_name);
  586. kfree(to_name);
  587. return(err);
  588. }
  589. int hostfs_unlink(struct inode *ino, struct dentry *dentry)
  590. {
  591. char *file;
  592. int err;
  593. if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
  594. if(append)
  595. return(-EPERM);
  596. err = unlink_file(file);
  597. kfree(file);
  598. return(err);
  599. }
  600. int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
  601. {
  602. char *file;
  603. int err;
  604. if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
  605. err = make_symlink(file, to);
  606. kfree(file);
  607. return(err);
  608. }
  609. int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
  610. {
  611. char *file;
  612. int err;
  613. if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
  614. err = do_mkdir(file, mode);
  615. kfree(file);
  616. return(err);
  617. }
  618. int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
  619. {
  620. char *file;
  621. int err;
  622. if((file = inode_dentry_name(ino, dentry)) == NULL) return(-ENOMEM);
  623. err = do_rmdir(file);
  624. kfree(file);
  625. return(err);
  626. }
  627. int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  628. {
  629. struct inode *inode;
  630. char *name;
  631. int err = -ENOMEM;
  632. inode = iget(dir->i_sb, 0);
  633. if(inode == NULL)
  634. goto out;
  635. err = init_inode(inode, dentry);
  636. if(err)
  637. goto out_put;
  638. err = -ENOMEM;
  639. name = dentry_name(dentry, 0);
  640. if(name == NULL)
  641. goto out_put;
  642. init_special_inode(inode, mode, dev);
  643. err = do_mknod(name, mode, dev);
  644. if(err)
  645. goto out_free;
  646. err = read_name(inode, name);
  647. kfree(name);
  648. if(err)
  649. goto out_put;
  650. d_instantiate(dentry, inode);
  651. return(0);
  652. out_free:
  653. kfree(name);
  654. out_put:
  655. iput(inode);
  656. out:
  657. return(err);
  658. }
  659. int hostfs_rename(struct inode *from_ino, struct dentry *from,
  660. struct inode *to_ino, struct dentry *to)
  661. {
  662. char *from_name, *to_name;
  663. int err;
  664. if((from_name = inode_dentry_name(from_ino, from)) == NULL)
  665. return(-ENOMEM);
  666. if((to_name = inode_dentry_name(to_ino, to)) == NULL){
  667. kfree(from_name);
  668. return(-ENOMEM);
  669. }
  670. err = rename_file(from_name, to_name);
  671. kfree(from_name);
  672. kfree(to_name);
  673. return(err);
  674. }
  675. int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd)
  676. {
  677. char *name;
  678. int r = 0, w = 0, x = 0, err;
  679. if (desired & MAY_READ) r = 1;
  680. if (desired & MAY_WRITE) w = 1;
  681. if (desired & MAY_EXEC) x = 1;
  682. name = inode_name(ino, 0);
  683. if (name == NULL) return(-ENOMEM);
  684. if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
  685. S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
  686. err = 0;
  687. else
  688. err = access_file(name, r, w, x);
  689. kfree(name);
  690. if(!err)
  691. err = generic_permission(ino, desired, NULL);
  692. return err;
  693. }
  694. int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
  695. {
  696. struct hostfs_iattr attrs;
  697. char *name;
  698. int err;
  699. err = inode_change_ok(dentry->d_inode, attr);
  700. if (err)
  701. return err;
  702. if(append)
  703. attr->ia_valid &= ~ATTR_SIZE;
  704. attrs.ia_valid = 0;
  705. if(attr->ia_valid & ATTR_MODE){
  706. attrs.ia_valid |= HOSTFS_ATTR_MODE;
  707. attrs.ia_mode = attr->ia_mode;
  708. }
  709. if(attr->ia_valid & ATTR_UID){
  710. attrs.ia_valid |= HOSTFS_ATTR_UID;
  711. attrs.ia_uid = attr->ia_uid;
  712. }
  713. if(attr->ia_valid & ATTR_GID){
  714. attrs.ia_valid |= HOSTFS_ATTR_GID;
  715. attrs.ia_gid = attr->ia_gid;
  716. }
  717. if(attr->ia_valid & ATTR_SIZE){
  718. attrs.ia_valid |= HOSTFS_ATTR_SIZE;
  719. attrs.ia_size = attr->ia_size;
  720. }
  721. if(attr->ia_valid & ATTR_ATIME){
  722. attrs.ia_valid |= HOSTFS_ATTR_ATIME;
  723. attrs.ia_atime = attr->ia_atime;
  724. }
  725. if(attr->ia_valid & ATTR_MTIME){
  726. attrs.ia_valid |= HOSTFS_ATTR_MTIME;
  727. attrs.ia_mtime = attr->ia_mtime;
  728. }
  729. if(attr->ia_valid & ATTR_CTIME){
  730. attrs.ia_valid |= HOSTFS_ATTR_CTIME;
  731. attrs.ia_ctime = attr->ia_ctime;
  732. }
  733. if(attr->ia_valid & ATTR_ATIME_SET){
  734. attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
  735. }
  736. if(attr->ia_valid & ATTR_MTIME_SET){
  737. attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
  738. }
  739. name = dentry_name(dentry, 0);
  740. if(name == NULL) return(-ENOMEM);
  741. err = set_attr(name, &attrs);
  742. kfree(name);
  743. if(err)
  744. return(err);
  745. return(inode_setattr(dentry->d_inode, attr));
  746. }
  747. int hostfs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  748. struct kstat *stat)
  749. {
  750. generic_fillattr(dentry->d_inode, stat);
  751. return(0);
  752. }
  753. static struct inode_operations hostfs_iops = {
  754. .create = hostfs_create,
  755. .link = hostfs_link,
  756. .unlink = hostfs_unlink,
  757. .symlink = hostfs_symlink,
  758. .mkdir = hostfs_mkdir,
  759. .rmdir = hostfs_rmdir,
  760. .mknod = hostfs_mknod,
  761. .rename = hostfs_rename,
  762. .permission = hostfs_permission,
  763. .setattr = hostfs_setattr,
  764. .getattr = hostfs_getattr,
  765. };
  766. static struct inode_operations hostfs_dir_iops = {
  767. .create = hostfs_create,
  768. .lookup = hostfs_lookup,
  769. .link = hostfs_link,
  770. .unlink = hostfs_unlink,
  771. .symlink = hostfs_symlink,
  772. .mkdir = hostfs_mkdir,
  773. .rmdir = hostfs_rmdir,
  774. .mknod = hostfs_mknod,
  775. .rename = hostfs_rename,
  776. .permission = hostfs_permission,
  777. .setattr = hostfs_setattr,
  778. .getattr = hostfs_getattr,
  779. };
  780. int hostfs_link_readpage(struct file *file, struct page *page)
  781. {
  782. char *buffer, *name;
  783. int err;
  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. */