hostfs_kern.c 21 KB

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