hostfs_kern.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020
  1. /*
  2. * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.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/fs.h>
  9. #include <linux/module.h>
  10. #include <linux/mm.h>
  11. #include <linux/pagemap.h>
  12. #include <linux/statfs.h>
  13. #include "hostfs.h"
  14. #include "init.h"
  15. #include "kern.h"
  16. struct hostfs_inode_info {
  17. char *host_filename;
  18. int fd;
  19. int mode;
  20. struct inode vfs_inode;
  21. };
  22. static inline struct hostfs_inode_info *HOSTFS_I(struct inode *inode)
  23. {
  24. return list_entry(inode, struct hostfs_inode_info, vfs_inode);
  25. }
  26. #define FILE_HOSTFS_I(file) HOSTFS_I((file)->f_path.dentry->d_inode)
  27. int hostfs_d_delete(struct dentry *dentry)
  28. {
  29. return 1;
  30. }
  31. struct dentry_operations hostfs_dentry_ops = {
  32. .d_delete = hostfs_d_delete,
  33. };
  34. /* Changed in hostfs_args before the kernel starts running */
  35. static char *root_ino = "";
  36. static int append = 0;
  37. #define HOSTFS_SUPER_MAGIC 0x00c0ffee
  38. static const struct inode_operations hostfs_iops;
  39. static const struct inode_operations hostfs_dir_iops;
  40. static const struct address_space_operations hostfs_link_aops;
  41. #ifndef MODULE
  42. static int __init hostfs_args(char *options, int *add)
  43. {
  44. char *ptr;
  45. ptr = strchr(options, ',');
  46. if (ptr != NULL)
  47. *ptr++ = '\0';
  48. if (*options != '\0')
  49. root_ino = options;
  50. options = ptr;
  51. while (options) {
  52. ptr = strchr(options, ',');
  53. if (ptr != NULL)
  54. *ptr++ = '\0';
  55. if (*options != '\0') {
  56. if (!strcmp(options, "append"))
  57. append = 1;
  58. else printf("hostfs_args - unsupported option - %s\n",
  59. options);
  60. }
  61. options = ptr;
  62. }
  63. return 0;
  64. }
  65. __uml_setup("hostfs=", hostfs_args,
  66. "hostfs=<root dir>,<flags>,...\n"
  67. " This is used to set hostfs parameters. The root directory argument\n"
  68. " is used to confine all hostfs mounts to within the specified directory\n"
  69. " tree on the host. If this isn't specified, then a user inside UML can\n"
  70. " mount anything on the host that's accessible to the user that's running\n"
  71. " it.\n"
  72. " The only flag currently supported is 'append', which specifies that all\n"
  73. " files opened by hostfs will be opened in append mode.\n\n"
  74. );
  75. #endif
  76. static char *dentry_name(struct dentry *dentry, int extra)
  77. {
  78. struct dentry *parent;
  79. char *root, *name;
  80. int len;
  81. len = 0;
  82. parent = dentry;
  83. while (parent->d_parent != parent) {
  84. len += parent->d_name.len + 1;
  85. parent = parent->d_parent;
  86. }
  87. root = HOSTFS_I(parent->d_inode)->host_filename;
  88. len += strlen(root);
  89. name = kmalloc(len + extra + 1, GFP_KERNEL);
  90. if (name == NULL)
  91. return NULL;
  92. name[len] = '\0';
  93. parent = dentry;
  94. while (parent->d_parent != parent) {
  95. len -= parent->d_name.len + 1;
  96. name[len] = '/';
  97. strncpy(&name[len + 1], parent->d_name.name,
  98. parent->d_name.len);
  99. parent = parent->d_parent;
  100. }
  101. strncpy(name, root, strlen(root));
  102. return name;
  103. }
  104. static char *inode_name(struct inode *ino, int extra)
  105. {
  106. struct dentry *dentry;
  107. dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias);
  108. return dentry_name(dentry, extra);
  109. }
  110. static int read_name(struct inode *ino, char *name)
  111. {
  112. /*
  113. * The non-int inode fields are copied into ints by stat_file and
  114. * then copied into the inode because passing the actual pointers
  115. * in and having them treated as int * breaks on big-endian machines
  116. */
  117. int err;
  118. int i_mode, i_nlink, i_blksize;
  119. unsigned long long i_size;
  120. unsigned long long i_ino;
  121. unsigned long long i_blocks;
  122. err = stat_file(name, &i_ino, &i_mode, &i_nlink, &ino->i_uid,
  123. &ino->i_gid, &i_size, &ino->i_atime, &ino->i_mtime,
  124. &ino->i_ctime, &i_blksize, &i_blocks, -1);
  125. if (err)
  126. return err;
  127. ino->i_ino = i_ino;
  128. ino->i_mode = i_mode;
  129. ino->i_nlink = i_nlink;
  130. ino->i_size = i_size;
  131. ino->i_blocks = i_blocks;
  132. return 0;
  133. }
  134. static char *follow_link(char *link)
  135. {
  136. int len, n;
  137. char *name, *resolved, *end;
  138. len = 64;
  139. while (1) {
  140. n = -ENOMEM;
  141. name = kmalloc(len, GFP_KERNEL);
  142. if (name == NULL)
  143. goto out;
  144. n = do_readlink(link, name, len);
  145. if (n < len)
  146. break;
  147. len *= 2;
  148. kfree(name);
  149. }
  150. if (n < 0)
  151. goto out_free;
  152. if (*name == '/')
  153. return name;
  154. end = strrchr(link, '/');
  155. if (end == NULL)
  156. return name;
  157. *(end + 1) = '\0';
  158. len = strlen(link) + strlen(name) + 1;
  159. resolved = kmalloc(len, GFP_KERNEL);
  160. if (resolved == NULL) {
  161. n = -ENOMEM;
  162. goto out_free;
  163. }
  164. sprintf(resolved, "%s%s", link, name);
  165. kfree(name);
  166. kfree(link);
  167. return resolved;
  168. out_free:
  169. kfree(name);
  170. out:
  171. return ERR_PTR(n);
  172. }
  173. static int read_inode(struct inode *ino)
  174. {
  175. char *name;
  176. int err = 0;
  177. /*
  178. * Unfortunately, we are called from iget() when we don't have a dentry
  179. * allocated yet.
  180. */
  181. if (list_empty(&ino->i_dentry))
  182. goto out;
  183. err = -ENOMEM;
  184. name = inode_name(ino, 0);
  185. if (name == NULL)
  186. goto out;
  187. if (file_type(name, NULL, NULL) == OS_TYPE_SYMLINK) {
  188. name = follow_link(name);
  189. if (IS_ERR(name)) {
  190. err = PTR_ERR(name);
  191. goto out;
  192. }
  193. }
  194. err = read_name(ino, name);
  195. kfree(name);
  196. out:
  197. return err;
  198. }
  199. int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
  200. {
  201. /*
  202. * do_statfs uses struct statfs64 internally, but the linux kernel
  203. * struct statfs still has 32-bit versions for most of these fields,
  204. * so we convert them here
  205. */
  206. int err;
  207. long long f_blocks;
  208. long long f_bfree;
  209. long long f_bavail;
  210. long long f_files;
  211. long long f_ffree;
  212. err = do_statfs(HOSTFS_I(dentry->d_sb->s_root->d_inode)->host_filename,
  213. &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
  214. &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
  215. &sf->f_namelen, sf->f_spare);
  216. if (err)
  217. return err;
  218. sf->f_blocks = f_blocks;
  219. sf->f_bfree = f_bfree;
  220. sf->f_bavail = f_bavail;
  221. sf->f_files = f_files;
  222. sf->f_ffree = f_ffree;
  223. sf->f_type = HOSTFS_SUPER_MAGIC;
  224. return 0;
  225. }
  226. static struct inode *hostfs_alloc_inode(struct super_block *sb)
  227. {
  228. struct hostfs_inode_info *hi;
  229. hi = kmalloc(sizeof(*hi), GFP_KERNEL);
  230. if (hi == NULL)
  231. return NULL;
  232. *hi = ((struct hostfs_inode_info) { .host_filename = NULL,
  233. .fd = -1,
  234. .mode = 0 });
  235. inode_init_once(&hi->vfs_inode);
  236. return &hi->vfs_inode;
  237. }
  238. static void hostfs_delete_inode(struct inode *inode)
  239. {
  240. truncate_inode_pages(&inode->i_data, 0);
  241. if (HOSTFS_I(inode)->fd != -1) {
  242. close_file(&HOSTFS_I(inode)->fd);
  243. HOSTFS_I(inode)->fd = -1;
  244. }
  245. clear_inode(inode);
  246. }
  247. static void hostfs_destroy_inode(struct inode *inode)
  248. {
  249. kfree(HOSTFS_I(inode)->host_filename);
  250. /*
  251. * XXX: This should not happen, probably. The check is here for
  252. * additional safety.
  253. */
  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)
  280. return -ENOMEM;
  281. dir = open_dir(name, &error);
  282. kfree(name);
  283. if (dir == NULL)
  284. 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. /*
  303. * The file may already have been opened, but with the wrong access,
  304. * so this resets things and reopens the file with the new access.
  305. */
  306. if (HOSTFS_I(ino)->fd != -1) {
  307. close_file(&HOSTFS_I(ino)->fd);
  308. HOSTFS_I(ino)->fd = -1;
  309. }
  310. HOSTFS_I(ino)->mode |= mode;
  311. if (HOSTFS_I(ino)->mode & FMODE_READ)
  312. r = 1;
  313. if (HOSTFS_I(ino)->mode & FMODE_WRITE)
  314. w = 1;
  315. if (w)
  316. r = 1;
  317. name = dentry_name(file->f_path.dentry, 0);
  318. if (name == NULL)
  319. return -ENOMEM;
  320. fd = open_file(name, r, w, append);
  321. kfree(name);
  322. if (fd < 0)
  323. 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 const struct file_operations hostfs_file_fops = {
  332. .llseek = generic_file_llseek,
  333. .read = do_sync_read,
  334. .splice_read = generic_file_splice_read,
  335. .aio_read = generic_file_aio_read,
  336. .aio_write = generic_file_aio_write,
  337. .write = do_sync_write,
  338. .mmap = generic_file_mmap,
  339. .open = hostfs_file_open,
  340. .release = NULL,
  341. .fsync = hostfs_fsync,
  342. };
  343. static const 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)
  386. goto out;
  387. memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
  388. flush_dcache_page(page);
  389. SetPageUptodate(page);
  390. if (PageError(page)) ClearPageError(page);
  391. err = 0;
  392. out:
  393. kunmap(page);
  394. unlock_page(page);
  395. return err;
  396. }
  397. int hostfs_write_begin(struct file *file, struct address_space *mapping,
  398. loff_t pos, unsigned len, unsigned flags,
  399. struct page **pagep, void **fsdata)
  400. {
  401. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  402. *pagep = __grab_cache_page(mapping, index);
  403. if (!*pagep)
  404. return -ENOMEM;
  405. return 0;
  406. }
  407. int hostfs_write_end(struct file *file, struct address_space *mapping,
  408. loff_t pos, unsigned len, unsigned copied,
  409. struct page *page, void *fsdata)
  410. {
  411. struct inode *inode = mapping->host;
  412. void *buffer;
  413. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  414. int err;
  415. buffer = kmap(page);
  416. err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
  417. kunmap(page);
  418. if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
  419. SetPageUptodate(page);
  420. /*
  421. * If err > 0, write_file has added err to pos, so we are comparing
  422. * i_size against the last byte written.
  423. */
  424. if (err > 0 && (pos > inode->i_size))
  425. inode->i_size = pos;
  426. unlock_page(page);
  427. page_cache_release(page);
  428. return err;
  429. }
  430. static const struct address_space_operations hostfs_aops = {
  431. .writepage = hostfs_writepage,
  432. .readpage = hostfs_readpage,
  433. .set_page_dirty = __set_page_dirty_nobuffers,
  434. .write_begin = hostfs_write_begin,
  435. .write_end = hostfs_write_end,
  436. };
  437. static int init_inode(struct inode *inode, struct dentry *dentry)
  438. {
  439. char *name;
  440. int type, err = -ENOMEM;
  441. int maj, min;
  442. dev_t rdev = 0;
  443. if (dentry) {
  444. name = dentry_name(dentry, 0);
  445. if (name == NULL)
  446. goto out;
  447. type = file_type(name, &maj, &min);
  448. /* Reencode maj and min with the kernel encoding.*/
  449. rdev = MKDEV(maj, min);
  450. kfree(name);
  451. }
  452. else type = OS_TYPE_DIR;
  453. err = 0;
  454. if (type == OS_TYPE_SYMLINK)
  455. inode->i_op = &page_symlink_inode_operations;
  456. else if (type == OS_TYPE_DIR)
  457. inode->i_op = &hostfs_dir_iops;
  458. else inode->i_op = &hostfs_iops;
  459. if (type == OS_TYPE_DIR) inode->i_fop = &hostfs_dir_fops;
  460. else inode->i_fop = &hostfs_file_fops;
  461. if (type == OS_TYPE_SYMLINK)
  462. inode->i_mapping->a_ops = &hostfs_link_aops;
  463. else inode->i_mapping->a_ops = &hostfs_aops;
  464. switch (type) {
  465. case OS_TYPE_CHARDEV:
  466. init_special_inode(inode, S_IFCHR, rdev);
  467. break;
  468. case OS_TYPE_BLOCKDEV:
  469. init_special_inode(inode, S_IFBLK, rdev);
  470. break;
  471. case OS_TYPE_FIFO:
  472. init_special_inode(inode, S_IFIFO, 0);
  473. break;
  474. case OS_TYPE_SOCK:
  475. init_special_inode(inode, S_IFSOCK, 0);
  476. break;
  477. }
  478. out:
  479. return err;
  480. }
  481. int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
  482. struct nameidata *nd)
  483. {
  484. struct inode *inode;
  485. char *name;
  486. int error, fd;
  487. error = -ENOMEM;
  488. inode = iget(dir->i_sb, 0);
  489. if (inode == NULL)
  490. goto out;
  491. error = init_inode(inode, dentry);
  492. if (error)
  493. goto out_put;
  494. error = -ENOMEM;
  495. name = dentry_name(dentry, 0);
  496. if (name == NULL)
  497. goto out_put;
  498. fd = file_create(name,
  499. mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
  500. mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
  501. mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
  502. if (fd < 0)
  503. error = fd;
  504. else error = read_name(inode, name);
  505. kfree(name);
  506. if (error)
  507. goto out_put;
  508. HOSTFS_I(inode)->fd = fd;
  509. HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
  510. d_instantiate(dentry, inode);
  511. return 0;
  512. out_put:
  513. iput(inode);
  514. out:
  515. return error;
  516. }
  517. struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
  518. struct nameidata *nd)
  519. {
  520. struct inode *inode;
  521. char *name;
  522. int err;
  523. err = -ENOMEM;
  524. inode = iget(ino->i_sb, 0);
  525. if (inode == NULL)
  526. goto out;
  527. err = init_inode(inode, dentry);
  528. if (err)
  529. goto out_put;
  530. err = -ENOMEM;
  531. name = dentry_name(dentry, 0);
  532. if (name == NULL)
  533. goto out_put;
  534. err = read_name(inode, name);
  535. kfree(name);
  536. if (err == -ENOENT) {
  537. iput(inode);
  538. inode = NULL;
  539. }
  540. else if (err)
  541. goto out_put;
  542. d_add(dentry, inode);
  543. dentry->d_op = &hostfs_dentry_ops;
  544. return NULL;
  545. out_put:
  546. iput(inode);
  547. out:
  548. return ERR_PTR(err);
  549. }
  550. static char *inode_dentry_name(struct inode *ino, struct dentry *dentry)
  551. {
  552. char *file;
  553. int len;
  554. file = inode_name(ino, dentry->d_name.len + 1);
  555. if (file == NULL)
  556. return NULL;
  557. strcat(file, "/");
  558. len = strlen(file);
  559. strncat(file, dentry->d_name.name, dentry->d_name.len);
  560. file[len + dentry->d_name.len] = '\0';
  561. return file;
  562. }
  563. int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
  564. {
  565. char *from_name, *to_name;
  566. int err;
  567. if ((from_name = inode_dentry_name(ino, from)) == NULL)
  568. return -ENOMEM;
  569. to_name = dentry_name(to, 0);
  570. if (to_name == NULL) {
  571. kfree(from_name);
  572. return -ENOMEM;
  573. }
  574. err = link_file(to_name, from_name);
  575. kfree(from_name);
  576. kfree(to_name);
  577. return err;
  578. }
  579. int hostfs_unlink(struct inode *ino, struct dentry *dentry)
  580. {
  581. char *file;
  582. int err;
  583. if ((file = inode_dentry_name(ino, dentry)) == NULL)
  584. return -ENOMEM;
  585. if (append)
  586. return -EPERM;
  587. err = unlink_file(file);
  588. kfree(file);
  589. return err;
  590. }
  591. int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
  592. {
  593. char *file;
  594. int err;
  595. if ((file = inode_dentry_name(ino, dentry)) == NULL)
  596. return -ENOMEM;
  597. err = make_symlink(file, to);
  598. kfree(file);
  599. return err;
  600. }
  601. int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
  602. {
  603. char *file;
  604. int err;
  605. if ((file = inode_dentry_name(ino, dentry)) == NULL)
  606. return -ENOMEM;
  607. err = do_mkdir(file, mode);
  608. kfree(file);
  609. return err;
  610. }
  611. int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
  612. {
  613. char *file;
  614. int err;
  615. if ((file = inode_dentry_name(ino, dentry)) == NULL)
  616. return -ENOMEM;
  617. err = do_rmdir(file);
  618. kfree(file);
  619. return err;
  620. }
  621. int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  622. {
  623. struct inode *inode;
  624. char *name;
  625. int err = -ENOMEM;
  626. inode = iget(dir->i_sb, 0);
  627. if (inode == NULL)
  628. goto out;
  629. err = init_inode(inode, dentry);
  630. if (err)
  631. goto out_put;
  632. err = -ENOMEM;
  633. name = dentry_name(dentry, 0);
  634. if (name == NULL)
  635. goto out_put;
  636. init_special_inode(inode, mode, dev);
  637. err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
  638. if (err)
  639. goto out_free;
  640. err = read_name(inode, name);
  641. kfree(name);
  642. if (err)
  643. goto out_put;
  644. d_instantiate(dentry, inode);
  645. return 0;
  646. out_free:
  647. kfree(name);
  648. out_put:
  649. iput(inode);
  650. out:
  651. return err;
  652. }
  653. int hostfs_rename(struct inode *from_ino, struct dentry *from,
  654. struct inode *to_ino, struct dentry *to)
  655. {
  656. char *from_name, *to_name;
  657. int err;
  658. if ((from_name = inode_dentry_name(from_ino, from)) == NULL)
  659. return -ENOMEM;
  660. if ((to_name = inode_dentry_name(to_ino, to)) == NULL) {
  661. kfree(from_name);
  662. return -ENOMEM;
  663. }
  664. err = rename_file(from_name, to_name);
  665. kfree(from_name);
  666. kfree(to_name);
  667. return err;
  668. }
  669. int hostfs_permission(struct inode *ino, int desired, struct nameidata *nd)
  670. {
  671. char *name;
  672. int r = 0, w = 0, x = 0, err;
  673. if (desired & MAY_READ) r = 1;
  674. if (desired & MAY_WRITE) w = 1;
  675. if (desired & MAY_EXEC) x = 1;
  676. name = inode_name(ino, 0);
  677. if (name == NULL)
  678. return -ENOMEM;
  679. if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
  680. S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
  681. err = 0;
  682. else
  683. err = access_file(name, r, w, x);
  684. kfree(name);
  685. if (!err)
  686. err = generic_permission(ino, desired, NULL);
  687. return err;
  688. }
  689. int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
  690. {
  691. struct hostfs_iattr attrs;
  692. char *name;
  693. int err;
  694. int fd = HOSTFS_I(dentry->d_inode)->fd;
  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)
  737. return -ENOMEM;
  738. err = set_attr(name, &attrs, fd);
  739. kfree(name);
  740. if (err)
  741. return err;
  742. return inode_setattr(dentry->d_inode, attr);
  743. }
  744. static const struct inode_operations hostfs_iops = {
  745. .create = hostfs_create,
  746. .link = hostfs_link,
  747. .unlink = hostfs_unlink,
  748. .symlink = hostfs_symlink,
  749. .mkdir = hostfs_mkdir,
  750. .rmdir = hostfs_rmdir,
  751. .mknod = hostfs_mknod,
  752. .rename = hostfs_rename,
  753. .permission = hostfs_permission,
  754. .setattr = hostfs_setattr,
  755. };
  756. static const struct inode_operations hostfs_dir_iops = {
  757. .create = hostfs_create,
  758. .lookup = hostfs_lookup,
  759. .link = hostfs_link,
  760. .unlink = hostfs_unlink,
  761. .symlink = hostfs_symlink,
  762. .mkdir = hostfs_mkdir,
  763. .rmdir = hostfs_rmdir,
  764. .mknod = hostfs_mknod,
  765. .rename = hostfs_rename,
  766. .permission = hostfs_permission,
  767. .setattr = hostfs_setattr,
  768. };
  769. int hostfs_link_readpage(struct file *file, struct page *page)
  770. {
  771. char *buffer, *name;
  772. int err;
  773. buffer = kmap(page);
  774. name = inode_name(page->mapping->host, 0);
  775. if (name == NULL)
  776. return -ENOMEM;
  777. err = do_readlink(name, buffer, PAGE_CACHE_SIZE);
  778. kfree(name);
  779. if (err == PAGE_CACHE_SIZE)
  780. err = -E2BIG;
  781. else if (err > 0) {
  782. flush_dcache_page(page);
  783. SetPageUptodate(page);
  784. if (PageError(page)) ClearPageError(page);
  785. err = 0;
  786. }
  787. kunmap(page);
  788. unlock_page(page);
  789. return err;
  790. }
  791. static const struct address_space_operations hostfs_link_aops = {
  792. .readpage = hostfs_link_readpage,
  793. };
  794. static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
  795. {
  796. struct inode *root_inode;
  797. char *host_root_path, *req_root = d;
  798. int err;
  799. sb->s_blocksize = 1024;
  800. sb->s_blocksize_bits = 10;
  801. sb->s_magic = HOSTFS_SUPER_MAGIC;
  802. sb->s_op = &hostfs_sbops;
  803. /* NULL is printed as <NULL> by sprintf: avoid that. */
  804. if (req_root == NULL)
  805. req_root = "";
  806. err = -ENOMEM;
  807. host_root_path = kmalloc(strlen(root_ino) + 1
  808. + strlen(req_root) + 1, GFP_KERNEL);
  809. if (host_root_path == NULL)
  810. goto out;
  811. sprintf(host_root_path, "%s/%s", root_ino, req_root);
  812. root_inode = iget(sb, 0);
  813. if (root_inode == NULL)
  814. goto out_free;
  815. err = init_inode(root_inode, NULL);
  816. if (err)
  817. goto out_put;
  818. HOSTFS_I(root_inode)->host_filename = host_root_path;
  819. /*
  820. * Avoid that in the error path, iput(root_inode) frees again
  821. * host_root_path through hostfs_destroy_inode!
  822. */
  823. host_root_path = NULL;
  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;
  834. }
  835. return 0;
  836. out_put:
  837. iput(root_inode);
  838. out_free:
  839. kfree(host_root_path);
  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");