hostfs_kern.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014
  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. kfree(HOSTFS_I(inode));
  216. }
  217. static void hostfs_destroy_inode(struct inode *inode)
  218. {
  219. call_rcu(&inode->i_rcu, hostfs_i_callback);
  220. }
  221. static int hostfs_show_options(struct seq_file *seq, struct dentry *root)
  222. {
  223. const char *root_path = root->d_sb->s_fs_info;
  224. size_t offset = strlen(root_ino) + 1;
  225. if (strlen(root_path) > offset)
  226. seq_printf(seq, ",%s", root_path + offset);
  227. return 0;
  228. }
  229. static const struct super_operations hostfs_sbops = {
  230. .alloc_inode = hostfs_alloc_inode,
  231. .destroy_inode = hostfs_destroy_inode,
  232. .evict_inode = hostfs_evict_inode,
  233. .statfs = hostfs_statfs,
  234. .show_options = hostfs_show_options,
  235. };
  236. int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
  237. {
  238. void *dir;
  239. char *name;
  240. unsigned long long next, ino;
  241. int error, len;
  242. name = dentry_name(file->f_path.dentry);
  243. if (name == NULL)
  244. return -ENOMEM;
  245. dir = open_dir(name, &error);
  246. __putname(name);
  247. if (dir == NULL)
  248. return -error;
  249. next = file->f_pos;
  250. while ((name = read_dir(dir, &next, &ino, &len)) != NULL) {
  251. error = (*filldir)(ent, name, len, file->f_pos,
  252. ino, DT_UNKNOWN);
  253. if (error) break;
  254. file->f_pos = next;
  255. }
  256. close_dir(dir);
  257. return 0;
  258. }
  259. int hostfs_file_open(struct inode *ino, struct file *file)
  260. {
  261. static DEFINE_MUTEX(open_mutex);
  262. char *name;
  263. fmode_t mode = 0;
  264. int err;
  265. int r = 0, w = 0, fd;
  266. mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
  267. if ((mode & HOSTFS_I(ino)->mode) == mode)
  268. return 0;
  269. mode |= HOSTFS_I(ino)->mode;
  270. retry:
  271. if (mode & FMODE_READ)
  272. r = 1;
  273. if (mode & FMODE_WRITE)
  274. w = 1;
  275. if (w)
  276. r = 1;
  277. name = dentry_name(file->f_path.dentry);
  278. if (name == NULL)
  279. return -ENOMEM;
  280. fd = open_file(name, r, w, append);
  281. __putname(name);
  282. if (fd < 0)
  283. return fd;
  284. mutex_lock(&open_mutex);
  285. /* somebody else had handled it first? */
  286. if ((mode & HOSTFS_I(ino)->mode) == mode) {
  287. mutex_unlock(&open_mutex);
  288. return 0;
  289. }
  290. if ((mode | HOSTFS_I(ino)->mode) != mode) {
  291. mode |= HOSTFS_I(ino)->mode;
  292. mutex_unlock(&open_mutex);
  293. close_file(&fd);
  294. goto retry;
  295. }
  296. if (HOSTFS_I(ino)->fd == -1) {
  297. HOSTFS_I(ino)->fd = fd;
  298. } else {
  299. err = replace_file(fd, HOSTFS_I(ino)->fd);
  300. close_file(&fd);
  301. if (err < 0) {
  302. mutex_unlock(&open_mutex);
  303. return err;
  304. }
  305. }
  306. HOSTFS_I(ino)->mode = mode;
  307. mutex_unlock(&open_mutex);
  308. return 0;
  309. }
  310. int hostfs_fsync(struct file *file, loff_t start, loff_t end, int datasync)
  311. {
  312. struct inode *inode = file->f_mapping->host;
  313. int ret;
  314. ret = filemap_write_and_wait_range(inode->i_mapping, start, end);
  315. if (ret)
  316. return ret;
  317. mutex_lock(&inode->i_mutex);
  318. ret = fsync_file(HOSTFS_I(inode)->fd, datasync);
  319. mutex_unlock(&inode->i_mutex);
  320. return ret;
  321. }
  322. static const struct file_operations hostfs_file_fops = {
  323. .llseek = generic_file_llseek,
  324. .read = do_sync_read,
  325. .splice_read = generic_file_splice_read,
  326. .aio_read = generic_file_aio_read,
  327. .aio_write = generic_file_aio_write,
  328. .write = do_sync_write,
  329. .mmap = generic_file_mmap,
  330. .open = hostfs_file_open,
  331. .release = NULL,
  332. .fsync = hostfs_fsync,
  333. };
  334. static const struct file_operations hostfs_dir_fops = {
  335. .llseek = generic_file_llseek,
  336. .readdir = hostfs_readdir,
  337. .read = generic_read_dir,
  338. };
  339. int hostfs_writepage(struct page *page, struct writeback_control *wbc)
  340. {
  341. struct address_space *mapping = page->mapping;
  342. struct inode *inode = mapping->host;
  343. char *buffer;
  344. unsigned long long base;
  345. int count = PAGE_CACHE_SIZE;
  346. int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
  347. int err;
  348. if (page->index >= end_index)
  349. count = inode->i_size & (PAGE_CACHE_SIZE-1);
  350. buffer = kmap(page);
  351. base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
  352. err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
  353. if (err != count) {
  354. ClearPageUptodate(page);
  355. goto out;
  356. }
  357. if (base > inode->i_size)
  358. inode->i_size = base;
  359. if (PageError(page))
  360. ClearPageError(page);
  361. err = 0;
  362. out:
  363. kunmap(page);
  364. unlock_page(page);
  365. return err;
  366. }
  367. int hostfs_readpage(struct file *file, struct page *page)
  368. {
  369. char *buffer;
  370. long long start;
  371. int err = 0;
  372. start = (long long) page->index << PAGE_CACHE_SHIFT;
  373. buffer = kmap(page);
  374. err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
  375. PAGE_CACHE_SIZE);
  376. if (err < 0)
  377. goto out;
  378. memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
  379. flush_dcache_page(page);
  380. SetPageUptodate(page);
  381. if (PageError(page)) ClearPageError(page);
  382. err = 0;
  383. out:
  384. kunmap(page);
  385. unlock_page(page);
  386. return err;
  387. }
  388. int hostfs_write_begin(struct file *file, struct address_space *mapping,
  389. loff_t pos, unsigned len, unsigned flags,
  390. struct page **pagep, void **fsdata)
  391. {
  392. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  393. *pagep = grab_cache_page_write_begin(mapping, index, flags);
  394. if (!*pagep)
  395. return -ENOMEM;
  396. return 0;
  397. }
  398. int hostfs_write_end(struct file *file, struct address_space *mapping,
  399. loff_t pos, unsigned len, unsigned copied,
  400. struct page *page, void *fsdata)
  401. {
  402. struct inode *inode = mapping->host;
  403. void *buffer;
  404. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  405. int err;
  406. buffer = kmap(page);
  407. err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
  408. kunmap(page);
  409. if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
  410. SetPageUptodate(page);
  411. /*
  412. * If err > 0, write_file has added err to pos, so we are comparing
  413. * i_size against the last byte written.
  414. */
  415. if (err > 0 && (pos > inode->i_size))
  416. inode->i_size = pos;
  417. unlock_page(page);
  418. page_cache_release(page);
  419. return err;
  420. }
  421. static const struct address_space_operations hostfs_aops = {
  422. .writepage = hostfs_writepage,
  423. .readpage = hostfs_readpage,
  424. .set_page_dirty = __set_page_dirty_nobuffers,
  425. .write_begin = hostfs_write_begin,
  426. .write_end = hostfs_write_end,
  427. };
  428. static int read_name(struct inode *ino, char *name)
  429. {
  430. dev_t rdev;
  431. struct hostfs_stat st;
  432. int err = stat_file(name, &st, -1);
  433. if (err)
  434. return err;
  435. /* Reencode maj and min with the kernel encoding.*/
  436. rdev = MKDEV(st.maj, st.min);
  437. switch (st.mode & S_IFMT) {
  438. case S_IFLNK:
  439. ino->i_op = &hostfs_link_iops;
  440. break;
  441. case S_IFDIR:
  442. ino->i_op = &hostfs_dir_iops;
  443. ino->i_fop = &hostfs_dir_fops;
  444. break;
  445. case S_IFCHR:
  446. case S_IFBLK:
  447. case S_IFIFO:
  448. case S_IFSOCK:
  449. init_special_inode(ino, st.mode & S_IFMT, rdev);
  450. ino->i_op = &hostfs_iops;
  451. break;
  452. default:
  453. ino->i_op = &hostfs_iops;
  454. ino->i_fop = &hostfs_file_fops;
  455. ino->i_mapping->a_ops = &hostfs_aops;
  456. }
  457. ino->i_ino = st.ino;
  458. ino->i_mode = st.mode;
  459. set_nlink(ino, st.nlink);
  460. ino->i_uid = st.uid;
  461. ino->i_gid = st.gid;
  462. ino->i_atime = st.atime;
  463. ino->i_mtime = st.mtime;
  464. ino->i_ctime = st.ctime;
  465. ino->i_size = st.size;
  466. ino->i_blocks = st.blocks;
  467. return 0;
  468. }
  469. int hostfs_create(struct inode *dir, struct dentry *dentry, umode_t mode,
  470. struct nameidata *nd)
  471. {
  472. struct inode *inode;
  473. char *name;
  474. int error, fd;
  475. inode = hostfs_iget(dir->i_sb);
  476. if (IS_ERR(inode)) {
  477. error = PTR_ERR(inode);
  478. goto out;
  479. }
  480. error = -ENOMEM;
  481. name = dentry_name(dentry);
  482. if (name == NULL)
  483. goto out_put;
  484. fd = file_create(name,
  485. mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
  486. mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
  487. mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
  488. if (fd < 0)
  489. error = fd;
  490. else
  491. error = read_name(inode, name);
  492. __putname(name);
  493. if (error)
  494. goto out_put;
  495. HOSTFS_I(inode)->fd = fd;
  496. HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
  497. d_instantiate(dentry, inode);
  498. return 0;
  499. out_put:
  500. iput(inode);
  501. out:
  502. return error;
  503. }
  504. struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
  505. struct nameidata *nd)
  506. {
  507. struct inode *inode;
  508. char *name;
  509. int err;
  510. inode = hostfs_iget(ino->i_sb);
  511. if (IS_ERR(inode)) {
  512. err = PTR_ERR(inode);
  513. goto out;
  514. }
  515. err = -ENOMEM;
  516. name = dentry_name(dentry);
  517. if (name == NULL)
  518. goto out_put;
  519. err = read_name(inode, name);
  520. __putname(name);
  521. if (err == -ENOENT) {
  522. iput(inode);
  523. inode = NULL;
  524. }
  525. else if (err)
  526. goto out_put;
  527. d_add(dentry, inode);
  528. return NULL;
  529. out_put:
  530. iput(inode);
  531. out:
  532. return ERR_PTR(err);
  533. }
  534. int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
  535. {
  536. char *from_name, *to_name;
  537. int err;
  538. if ((from_name = dentry_name(from)) == NULL)
  539. return -ENOMEM;
  540. to_name = dentry_name(to);
  541. if (to_name == NULL) {
  542. __putname(from_name);
  543. return -ENOMEM;
  544. }
  545. err = link_file(to_name, from_name);
  546. __putname(from_name);
  547. __putname(to_name);
  548. return err;
  549. }
  550. int hostfs_unlink(struct inode *ino, struct dentry *dentry)
  551. {
  552. char *file;
  553. int err;
  554. if (append)
  555. return -EPERM;
  556. if ((file = dentry_name(dentry)) == NULL)
  557. return -ENOMEM;
  558. err = unlink_file(file);
  559. __putname(file);
  560. return err;
  561. }
  562. int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
  563. {
  564. char *file;
  565. int err;
  566. if ((file = dentry_name(dentry)) == NULL)
  567. return -ENOMEM;
  568. err = make_symlink(file, to);
  569. __putname(file);
  570. return err;
  571. }
  572. int hostfs_mkdir(struct inode *ino, struct dentry *dentry, umode_t mode)
  573. {
  574. char *file;
  575. int err;
  576. if ((file = dentry_name(dentry)) == NULL)
  577. return -ENOMEM;
  578. err = do_mkdir(file, mode);
  579. __putname(file);
  580. return err;
  581. }
  582. int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
  583. {
  584. char *file;
  585. int err;
  586. if ((file = dentry_name(dentry)) == NULL)
  587. return -ENOMEM;
  588. err = do_rmdir(file);
  589. __putname(file);
  590. return err;
  591. }
  592. static int hostfs_mknod(struct inode *dir, struct dentry *dentry, umode_t mode, dev_t dev)
  593. {
  594. struct inode *inode;
  595. char *name;
  596. int err;
  597. inode = hostfs_iget(dir->i_sb);
  598. if (IS_ERR(inode)) {
  599. err = PTR_ERR(inode);
  600. goto out;
  601. }
  602. err = -ENOMEM;
  603. name = dentry_name(dentry);
  604. if (name == NULL)
  605. goto out_put;
  606. init_special_inode(inode, mode, dev);
  607. err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
  608. if (!err)
  609. goto out_free;
  610. err = read_name(inode, name);
  611. __putname(name);
  612. if (err)
  613. goto out_put;
  614. if (err)
  615. goto out_put;
  616. d_instantiate(dentry, inode);
  617. return 0;
  618. out_free:
  619. __putname(name);
  620. out_put:
  621. iput(inode);
  622. out:
  623. return err;
  624. }
  625. int hostfs_rename(struct inode *from_ino, struct dentry *from,
  626. struct inode *to_ino, struct dentry *to)
  627. {
  628. char *from_name, *to_name;
  629. int err;
  630. if ((from_name = dentry_name(from)) == NULL)
  631. return -ENOMEM;
  632. if ((to_name = dentry_name(to)) == NULL) {
  633. __putname(from_name);
  634. return -ENOMEM;
  635. }
  636. err = rename_file(from_name, to_name);
  637. __putname(from_name);
  638. __putname(to_name);
  639. return err;
  640. }
  641. int hostfs_permission(struct inode *ino, int desired)
  642. {
  643. char *name;
  644. int r = 0, w = 0, x = 0, err;
  645. if (desired & MAY_NOT_BLOCK)
  646. return -ECHILD;
  647. if (desired & MAY_READ) r = 1;
  648. if (desired & MAY_WRITE) w = 1;
  649. if (desired & MAY_EXEC) x = 1;
  650. name = inode_name(ino);
  651. if (name == NULL)
  652. return -ENOMEM;
  653. if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
  654. S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
  655. err = 0;
  656. else
  657. err = access_file(name, r, w, x);
  658. __putname(name);
  659. if (!err)
  660. err = generic_permission(ino, desired);
  661. return err;
  662. }
  663. int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
  664. {
  665. struct inode *inode = dentry->d_inode;
  666. struct hostfs_iattr attrs;
  667. char *name;
  668. int err;
  669. int fd = HOSTFS_I(inode)->fd;
  670. err = inode_change_ok(inode, attr);
  671. if (err)
  672. return err;
  673. if (append)
  674. attr->ia_valid &= ~ATTR_SIZE;
  675. attrs.ia_valid = 0;
  676. if (attr->ia_valid & ATTR_MODE) {
  677. attrs.ia_valid |= HOSTFS_ATTR_MODE;
  678. attrs.ia_mode = attr->ia_mode;
  679. }
  680. if (attr->ia_valid & ATTR_UID) {
  681. attrs.ia_valid |= HOSTFS_ATTR_UID;
  682. attrs.ia_uid = attr->ia_uid;
  683. }
  684. if (attr->ia_valid & ATTR_GID) {
  685. attrs.ia_valid |= HOSTFS_ATTR_GID;
  686. attrs.ia_gid = attr->ia_gid;
  687. }
  688. if (attr->ia_valid & ATTR_SIZE) {
  689. attrs.ia_valid |= HOSTFS_ATTR_SIZE;
  690. attrs.ia_size = attr->ia_size;
  691. }
  692. if (attr->ia_valid & ATTR_ATIME) {
  693. attrs.ia_valid |= HOSTFS_ATTR_ATIME;
  694. attrs.ia_atime = attr->ia_atime;
  695. }
  696. if (attr->ia_valid & ATTR_MTIME) {
  697. attrs.ia_valid |= HOSTFS_ATTR_MTIME;
  698. attrs.ia_mtime = attr->ia_mtime;
  699. }
  700. if (attr->ia_valid & ATTR_CTIME) {
  701. attrs.ia_valid |= HOSTFS_ATTR_CTIME;
  702. attrs.ia_ctime = attr->ia_ctime;
  703. }
  704. if (attr->ia_valid & ATTR_ATIME_SET) {
  705. attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
  706. }
  707. if (attr->ia_valid & ATTR_MTIME_SET) {
  708. attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
  709. }
  710. name = dentry_name(dentry);
  711. if (name == NULL)
  712. return -ENOMEM;
  713. err = set_attr(name, &attrs, fd);
  714. __putname(name);
  715. if (err)
  716. return err;
  717. if ((attr->ia_valid & ATTR_SIZE) &&
  718. attr->ia_size != i_size_read(inode)) {
  719. int error;
  720. error = vmtruncate(inode, attr->ia_size);
  721. if (err)
  722. return err;
  723. }
  724. setattr_copy(inode, attr);
  725. mark_inode_dirty(inode);
  726. return 0;
  727. }
  728. static const struct inode_operations hostfs_iops = {
  729. .create = hostfs_create,
  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. static const struct inode_operations hostfs_dir_iops = {
  741. .create = hostfs_create,
  742. .lookup = hostfs_lookup,
  743. .link = hostfs_link,
  744. .unlink = hostfs_unlink,
  745. .symlink = hostfs_symlink,
  746. .mkdir = hostfs_mkdir,
  747. .rmdir = hostfs_rmdir,
  748. .mknod = hostfs_mknod,
  749. .rename = hostfs_rename,
  750. .permission = hostfs_permission,
  751. .setattr = hostfs_setattr,
  752. };
  753. static void *hostfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  754. {
  755. char *link = __getname();
  756. if (link) {
  757. char *path = dentry_name(dentry);
  758. int err = -ENOMEM;
  759. if (path) {
  760. err = hostfs_do_readlink(path, link, PATH_MAX);
  761. if (err == PATH_MAX)
  762. err = -E2BIG;
  763. __putname(path);
  764. }
  765. if (err < 0) {
  766. __putname(link);
  767. link = ERR_PTR(err);
  768. }
  769. } else {
  770. link = ERR_PTR(-ENOMEM);
  771. }
  772. nd_set_link(nd, link);
  773. return NULL;
  774. }
  775. static void hostfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  776. {
  777. char *s = nd_get_link(nd);
  778. if (!IS_ERR(s))
  779. __putname(s);
  780. }
  781. static const struct inode_operations hostfs_link_iops = {
  782. .readlink = generic_readlink,
  783. .follow_link = hostfs_follow_link,
  784. .put_link = hostfs_put_link,
  785. };
  786. static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
  787. {
  788. struct inode *root_inode;
  789. char *host_root_path, *req_root = d;
  790. int err;
  791. sb->s_blocksize = 1024;
  792. sb->s_blocksize_bits = 10;
  793. sb->s_magic = HOSTFS_SUPER_MAGIC;
  794. sb->s_op = &hostfs_sbops;
  795. sb->s_d_op = &hostfs_dentry_ops;
  796. sb->s_maxbytes = MAX_LFS_FILESIZE;
  797. /* NULL is printed as <NULL> by sprintf: avoid that. */
  798. if (req_root == NULL)
  799. req_root = "";
  800. err = -ENOMEM;
  801. sb->s_fs_info = host_root_path =
  802. kmalloc(strlen(root_ino) + strlen(req_root) + 2, GFP_KERNEL);
  803. if (host_root_path == NULL)
  804. goto out;
  805. sprintf(host_root_path, "%s/%s", root_ino, req_root);
  806. root_inode = new_inode(sb);
  807. if (!root_inode)
  808. goto out;
  809. err = read_name(root_inode, host_root_path);
  810. if (err)
  811. goto out_put;
  812. if (S_ISLNK(root_inode->i_mode)) {
  813. char *name = follow_link(host_root_path);
  814. if (IS_ERR(name))
  815. err = PTR_ERR(name);
  816. else
  817. err = read_name(root_inode, name);
  818. kfree(name);
  819. if (err)
  820. goto out_put;
  821. }
  822. err = -ENOMEM;
  823. sb->s_root = d_make_root(root_inode);
  824. if (sb->s_root == NULL)
  825. goto out;
  826. return 0;
  827. out_put:
  828. iput(root_inode);
  829. out:
  830. return err;
  831. }
  832. static struct dentry *hostfs_read_sb(struct file_system_type *type,
  833. int flags, const char *dev_name,
  834. void *data)
  835. {
  836. return mount_nodev(type, flags, data, hostfs_fill_sb_common);
  837. }
  838. static void hostfs_kill_sb(struct super_block *s)
  839. {
  840. kill_anon_super(s);
  841. kfree(s->s_fs_info);
  842. }
  843. static struct file_system_type hostfs_type = {
  844. .owner = THIS_MODULE,
  845. .name = "hostfs",
  846. .mount = hostfs_read_sb,
  847. .kill_sb = hostfs_kill_sb,
  848. .fs_flags = 0,
  849. };
  850. static int __init init_hostfs(void)
  851. {
  852. return register_filesystem(&hostfs_type);
  853. }
  854. static void __exit exit_hostfs(void)
  855. {
  856. unregister_filesystem(&hostfs_type);
  857. }
  858. module_init(init_hostfs)
  859. module_exit(exit_hostfs)
  860. MODULE_LICENSE("GPL");