hostfs_kern.c 21 KB

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