hostfs_kern.c 22 KB

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