hostfs_kern.c 20 KB

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