hostfs_kern.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998
  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(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(dentry, name, PATH_MAX);
  82. char *root;
  83. size_t len;
  84. spin_unlock(&dcache_lock);
  85. root = dentry->d_sb->s_fs_info;
  86. len = strlen(root);
  87. if (IS_ERR(p)) {
  88. __putname(name);
  89. return NULL;
  90. }
  91. strlcpy(name, root, PATH_MAX);
  92. if (len > p - name) {
  93. __putname(name);
  94. return NULL;
  95. }
  96. if (p > name + len) {
  97. char *s = name + len;
  98. while ((*s++ = *p++) != '\0')
  99. ;
  100. }
  101. return name;
  102. }
  103. static char *dentry_name(struct dentry *dentry)
  104. {
  105. char *name = __getname();
  106. if (!name)
  107. return NULL;
  108. spin_lock(&dcache_lock);
  109. return __dentry_name(dentry, name); /* will unlock */
  110. }
  111. static char *inode_name(struct inode *ino)
  112. {
  113. struct dentry *dentry;
  114. char *name = __getname();
  115. if (!name)
  116. return NULL;
  117. spin_lock(&dcache_lock);
  118. if (list_empty(&ino->i_dentry)) {
  119. spin_unlock(&dcache_lock);
  120. __putname(name);
  121. return NULL;
  122. }
  123. dentry = list_first_entry(&ino->i_dentry, struct dentry, d_alias);
  124. return __dentry_name(dentry, name); /* will unlock */
  125. }
  126. static char *follow_link(char *link)
  127. {
  128. int len, n;
  129. char *name, *resolved, *end;
  130. len = 64;
  131. while (1) {
  132. n = -ENOMEM;
  133. name = kmalloc(len, GFP_KERNEL);
  134. if (name == NULL)
  135. goto out;
  136. n = hostfs_do_readlink(link, name, len);
  137. if (n < len)
  138. break;
  139. len *= 2;
  140. kfree(name);
  141. }
  142. if (n < 0)
  143. goto out_free;
  144. if (*name == '/')
  145. return name;
  146. end = strrchr(link, '/');
  147. if (end == NULL)
  148. return name;
  149. *(end + 1) = '\0';
  150. len = strlen(link) + strlen(name) + 1;
  151. resolved = kmalloc(len, GFP_KERNEL);
  152. if (resolved == NULL) {
  153. n = -ENOMEM;
  154. goto out_free;
  155. }
  156. sprintf(resolved, "%s%s", link, name);
  157. kfree(name);
  158. kfree(link);
  159. return resolved;
  160. out_free:
  161. kfree(name);
  162. out:
  163. return ERR_PTR(n);
  164. }
  165. static struct inode *hostfs_iget(struct super_block *sb)
  166. {
  167. struct inode *inode = new_inode(sb);
  168. if (!inode)
  169. return ERR_PTR(-ENOMEM);
  170. return inode;
  171. }
  172. int hostfs_statfs(struct dentry *dentry, struct kstatfs *sf)
  173. {
  174. /*
  175. * do_statfs uses struct statfs64 internally, but the linux kernel
  176. * struct statfs still has 32-bit versions for most of these fields,
  177. * so we convert them here
  178. */
  179. int err;
  180. long long f_blocks;
  181. long long f_bfree;
  182. long long f_bavail;
  183. long long f_files;
  184. long long f_ffree;
  185. err = do_statfs(dentry->d_sb->s_fs_info,
  186. &sf->f_bsize, &f_blocks, &f_bfree, &f_bavail, &f_files,
  187. &f_ffree, &sf->f_fsid, sizeof(sf->f_fsid),
  188. &sf->f_namelen, sf->f_spare);
  189. if (err)
  190. return err;
  191. sf->f_blocks = f_blocks;
  192. sf->f_bfree = f_bfree;
  193. sf->f_bavail = f_bavail;
  194. sf->f_files = f_files;
  195. sf->f_ffree = f_ffree;
  196. sf->f_type = HOSTFS_SUPER_MAGIC;
  197. return 0;
  198. }
  199. static struct inode *hostfs_alloc_inode(struct super_block *sb)
  200. {
  201. struct hostfs_inode_info *hi;
  202. hi = kzalloc(sizeof(*hi), GFP_KERNEL);
  203. if (hi == NULL)
  204. return NULL;
  205. hi->fd = -1;
  206. inode_init_once(&hi->vfs_inode);
  207. return &hi->vfs_inode;
  208. }
  209. static void hostfs_evict_inode(struct inode *inode)
  210. {
  211. truncate_inode_pages(&inode->i_data, 0);
  212. end_writeback(inode);
  213. if (HOSTFS_I(inode)->fd != -1) {
  214. close_file(&HOSTFS_I(inode)->fd);
  215. HOSTFS_I(inode)->fd = -1;
  216. }
  217. }
  218. static void hostfs_destroy_inode(struct inode *inode)
  219. {
  220. kfree(HOSTFS_I(inode));
  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. dentry->d_op = &hostfs_dentry_ops;
  522. return NULL;
  523. out_put:
  524. iput(inode);
  525. out:
  526. return ERR_PTR(err);
  527. }
  528. int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
  529. {
  530. char *from_name, *to_name;
  531. int err;
  532. if ((from_name = dentry_name(from)) == NULL)
  533. return -ENOMEM;
  534. to_name = dentry_name(to);
  535. if (to_name == NULL) {
  536. __putname(from_name);
  537. return -ENOMEM;
  538. }
  539. err = link_file(to_name, from_name);
  540. __putname(from_name);
  541. __putname(to_name);
  542. return err;
  543. }
  544. int hostfs_unlink(struct inode *ino, struct dentry *dentry)
  545. {
  546. char *file;
  547. int err;
  548. if (append)
  549. return -EPERM;
  550. if ((file = dentry_name(dentry)) == NULL)
  551. return -ENOMEM;
  552. err = unlink_file(file);
  553. __putname(file);
  554. return err;
  555. }
  556. int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
  557. {
  558. char *file;
  559. int err;
  560. if ((file = dentry_name(dentry)) == NULL)
  561. return -ENOMEM;
  562. err = make_symlink(file, to);
  563. __putname(file);
  564. return err;
  565. }
  566. int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
  567. {
  568. char *file;
  569. int err;
  570. if ((file = dentry_name(dentry)) == NULL)
  571. return -ENOMEM;
  572. err = do_mkdir(file, mode);
  573. __putname(file);
  574. return err;
  575. }
  576. int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
  577. {
  578. char *file;
  579. int err;
  580. if ((file = dentry_name(dentry)) == NULL)
  581. return -ENOMEM;
  582. err = do_rmdir(file);
  583. __putname(file);
  584. return err;
  585. }
  586. int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  587. {
  588. struct inode *inode;
  589. char *name;
  590. int err;
  591. inode = hostfs_iget(dir->i_sb);
  592. if (IS_ERR(inode)) {
  593. err = PTR_ERR(inode);
  594. goto out;
  595. }
  596. err = -ENOMEM;
  597. name = dentry_name(dentry);
  598. if (name == NULL)
  599. goto out_put;
  600. init_special_inode(inode, mode, dev);
  601. err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
  602. if (!err)
  603. goto out_free;
  604. err = read_name(inode, name);
  605. __putname(name);
  606. if (err)
  607. goto out_put;
  608. if (err)
  609. goto out_put;
  610. d_instantiate(dentry, inode);
  611. return 0;
  612. out_free:
  613. __putname(name);
  614. out_put:
  615. iput(inode);
  616. out:
  617. return err;
  618. }
  619. int hostfs_rename(struct inode *from_ino, struct dentry *from,
  620. struct inode *to_ino, struct dentry *to)
  621. {
  622. char *from_name, *to_name;
  623. int err;
  624. if ((from_name = dentry_name(from)) == NULL)
  625. return -ENOMEM;
  626. if ((to_name = dentry_name(to)) == NULL) {
  627. __putname(from_name);
  628. return -ENOMEM;
  629. }
  630. err = rename_file(from_name, to_name);
  631. __putname(from_name);
  632. __putname(to_name);
  633. return err;
  634. }
  635. int hostfs_permission(struct inode *ino, int desired)
  636. {
  637. char *name;
  638. int r = 0, w = 0, x = 0, err;
  639. if (desired & MAY_READ) r = 1;
  640. if (desired & MAY_WRITE) w = 1;
  641. if (desired & MAY_EXEC) x = 1;
  642. name = inode_name(ino);
  643. if (name == NULL)
  644. return -ENOMEM;
  645. if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
  646. S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
  647. err = 0;
  648. else
  649. err = access_file(name, r, w, x);
  650. __putname(name);
  651. if (!err)
  652. err = generic_permission(ino, desired, NULL);
  653. return err;
  654. }
  655. int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
  656. {
  657. struct inode *inode = dentry->d_inode;
  658. struct hostfs_iattr attrs;
  659. char *name;
  660. int err;
  661. int fd = HOSTFS_I(inode)->fd;
  662. err = inode_change_ok(inode, attr);
  663. if (err)
  664. return err;
  665. if (append)
  666. attr->ia_valid &= ~ATTR_SIZE;
  667. attrs.ia_valid = 0;
  668. if (attr->ia_valid & ATTR_MODE) {
  669. attrs.ia_valid |= HOSTFS_ATTR_MODE;
  670. attrs.ia_mode = attr->ia_mode;
  671. }
  672. if (attr->ia_valid & ATTR_UID) {
  673. attrs.ia_valid |= HOSTFS_ATTR_UID;
  674. attrs.ia_uid = attr->ia_uid;
  675. }
  676. if (attr->ia_valid & ATTR_GID) {
  677. attrs.ia_valid |= HOSTFS_ATTR_GID;
  678. attrs.ia_gid = attr->ia_gid;
  679. }
  680. if (attr->ia_valid & ATTR_SIZE) {
  681. attrs.ia_valid |= HOSTFS_ATTR_SIZE;
  682. attrs.ia_size = attr->ia_size;
  683. }
  684. if (attr->ia_valid & ATTR_ATIME) {
  685. attrs.ia_valid |= HOSTFS_ATTR_ATIME;
  686. attrs.ia_atime = attr->ia_atime;
  687. }
  688. if (attr->ia_valid & ATTR_MTIME) {
  689. attrs.ia_valid |= HOSTFS_ATTR_MTIME;
  690. attrs.ia_mtime = attr->ia_mtime;
  691. }
  692. if (attr->ia_valid & ATTR_CTIME) {
  693. attrs.ia_valid |= HOSTFS_ATTR_CTIME;
  694. attrs.ia_ctime = attr->ia_ctime;
  695. }
  696. if (attr->ia_valid & ATTR_ATIME_SET) {
  697. attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
  698. }
  699. if (attr->ia_valid & ATTR_MTIME_SET) {
  700. attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
  701. }
  702. name = dentry_name(dentry);
  703. if (name == NULL)
  704. return -ENOMEM;
  705. err = set_attr(name, &attrs, fd);
  706. __putname(name);
  707. if (err)
  708. return err;
  709. if ((attr->ia_valid & ATTR_SIZE) &&
  710. attr->ia_size != i_size_read(inode)) {
  711. int error;
  712. error = vmtruncate(inode, attr->ia_size);
  713. if (err)
  714. return err;
  715. }
  716. setattr_copy(inode, attr);
  717. mark_inode_dirty(inode);
  718. return 0;
  719. }
  720. static const struct inode_operations hostfs_iops = {
  721. .create = hostfs_create,
  722. .link = hostfs_link,
  723. .unlink = hostfs_unlink,
  724. .symlink = hostfs_symlink,
  725. .mkdir = hostfs_mkdir,
  726. .rmdir = hostfs_rmdir,
  727. .mknod = hostfs_mknod,
  728. .rename = hostfs_rename,
  729. .permission = hostfs_permission,
  730. .setattr = hostfs_setattr,
  731. };
  732. static const struct inode_operations hostfs_dir_iops = {
  733. .create = hostfs_create,
  734. .lookup = hostfs_lookup,
  735. .link = hostfs_link,
  736. .unlink = hostfs_unlink,
  737. .symlink = hostfs_symlink,
  738. .mkdir = hostfs_mkdir,
  739. .rmdir = hostfs_rmdir,
  740. .mknod = hostfs_mknod,
  741. .rename = hostfs_rename,
  742. .permission = hostfs_permission,
  743. .setattr = hostfs_setattr,
  744. };
  745. static void *hostfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  746. {
  747. char *link = __getname();
  748. if (link) {
  749. char *path = dentry_name(dentry);
  750. int err = -ENOMEM;
  751. if (path) {
  752. err = hostfs_do_readlink(path, link, PATH_MAX);
  753. if (err == PATH_MAX)
  754. err = -E2BIG;
  755. __putname(path);
  756. }
  757. if (err < 0) {
  758. __putname(link);
  759. link = ERR_PTR(err);
  760. }
  761. } else {
  762. link = ERR_PTR(-ENOMEM);
  763. }
  764. nd_set_link(nd, link);
  765. return NULL;
  766. }
  767. static void hostfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  768. {
  769. char *s = nd_get_link(nd);
  770. if (!IS_ERR(s))
  771. __putname(s);
  772. }
  773. static const struct inode_operations hostfs_link_iops = {
  774. .readlink = generic_readlink,
  775. .follow_link = hostfs_follow_link,
  776. .put_link = hostfs_put_link,
  777. };
  778. static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
  779. {
  780. struct inode *root_inode;
  781. char *host_root_path, *req_root = d;
  782. int err;
  783. sb->s_blocksize = 1024;
  784. sb->s_blocksize_bits = 10;
  785. sb->s_magic = HOSTFS_SUPER_MAGIC;
  786. sb->s_op = &hostfs_sbops;
  787. sb->s_maxbytes = MAX_LFS_FILESIZE;
  788. /* NULL is printed as <NULL> by sprintf: avoid that. */
  789. if (req_root == NULL)
  790. req_root = "";
  791. err = -ENOMEM;
  792. sb->s_fs_info = host_root_path =
  793. kmalloc(strlen(root_ino) + strlen(req_root) + 2, GFP_KERNEL);
  794. if (host_root_path == NULL)
  795. goto out;
  796. sprintf(host_root_path, "%s/%s", root_ino, req_root);
  797. root_inode = new_inode(sb);
  798. if (!root_inode)
  799. goto out;
  800. err = read_name(root_inode, host_root_path);
  801. if (err)
  802. goto out_put;
  803. if (S_ISLNK(root_inode->i_mode)) {
  804. char *name = follow_link(host_root_path);
  805. if (IS_ERR(name))
  806. err = PTR_ERR(name);
  807. else
  808. err = read_name(root_inode, name);
  809. kfree(name);
  810. if (err)
  811. goto out_put;
  812. }
  813. err = -ENOMEM;
  814. sb->s_root = d_alloc_root(root_inode);
  815. if (sb->s_root == NULL)
  816. goto out_put;
  817. return 0;
  818. out_put:
  819. iput(root_inode);
  820. out:
  821. return err;
  822. }
  823. static int hostfs_read_sb(struct file_system_type *type,
  824. int flags, const char *dev_name,
  825. void *data, struct vfsmount *mnt)
  826. {
  827. return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt);
  828. }
  829. static void hostfs_kill_sb(struct super_block *s)
  830. {
  831. kill_anon_super(s);
  832. kfree(s->s_fs_info);
  833. }
  834. static struct file_system_type hostfs_type = {
  835. .owner = THIS_MODULE,
  836. .name = "hostfs",
  837. .get_sb = hostfs_read_sb,
  838. .kill_sb = hostfs_kill_sb,
  839. .fs_flags = 0,
  840. };
  841. static int __init init_hostfs(void)
  842. {
  843. return register_filesystem(&hostfs_type);
  844. }
  845. static void __exit exit_hostfs(void)
  846. {
  847. unregister_filesystem(&hostfs_type);
  848. }
  849. module_init(init_hostfs)
  850. module_exit(exit_hostfs)
  851. MODULE_LICENSE("GPL");