hostfs_kern.c 22 KB

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