hostfs_kern.c 21 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016
  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_show_option(seq, root_path + offset, NULL);
  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. struct nameidata *nd)
  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. struct nameidata *nd)
  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 = vmtruncate(inode, attr->ia_size);
  722. if (err)
  723. return err;
  724. }
  725. setattr_copy(inode, attr);
  726. mark_inode_dirty(inode);
  727. return 0;
  728. }
  729. static const struct inode_operations hostfs_iops = {
  730. .create = hostfs_create,
  731. .link = hostfs_link,
  732. .unlink = hostfs_unlink,
  733. .symlink = hostfs_symlink,
  734. .mkdir = hostfs_mkdir,
  735. .rmdir = hostfs_rmdir,
  736. .mknod = hostfs_mknod,
  737. .rename = hostfs_rename,
  738. .permission = hostfs_permission,
  739. .setattr = hostfs_setattr,
  740. };
  741. static const struct inode_operations hostfs_dir_iops = {
  742. .create = hostfs_create,
  743. .lookup = hostfs_lookup,
  744. .link = hostfs_link,
  745. .unlink = hostfs_unlink,
  746. .symlink = hostfs_symlink,
  747. .mkdir = hostfs_mkdir,
  748. .rmdir = hostfs_rmdir,
  749. .mknod = hostfs_mknod,
  750. .rename = hostfs_rename,
  751. .permission = hostfs_permission,
  752. .setattr = hostfs_setattr,
  753. };
  754. static void *hostfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  755. {
  756. char *link = __getname();
  757. if (link) {
  758. char *path = dentry_name(dentry);
  759. int err = -ENOMEM;
  760. if (path) {
  761. err = hostfs_do_readlink(path, link, PATH_MAX);
  762. if (err == PATH_MAX)
  763. err = -E2BIG;
  764. __putname(path);
  765. }
  766. if (err < 0) {
  767. __putname(link);
  768. link = ERR_PTR(err);
  769. }
  770. } else {
  771. link = ERR_PTR(-ENOMEM);
  772. }
  773. nd_set_link(nd, link);
  774. return NULL;
  775. }
  776. static void hostfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  777. {
  778. char *s = nd_get_link(nd);
  779. if (!IS_ERR(s))
  780. __putname(s);
  781. }
  782. static const struct inode_operations hostfs_link_iops = {
  783. .readlink = generic_readlink,
  784. .follow_link = hostfs_follow_link,
  785. .put_link = hostfs_put_link,
  786. };
  787. static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
  788. {
  789. struct inode *root_inode;
  790. char *host_root_path, *req_root = d;
  791. int err;
  792. sb->s_blocksize = 1024;
  793. sb->s_blocksize_bits = 10;
  794. sb->s_magic = HOSTFS_SUPER_MAGIC;
  795. sb->s_op = &hostfs_sbops;
  796. sb->s_d_op = &hostfs_dentry_ops;
  797. sb->s_maxbytes = MAX_LFS_FILESIZE;
  798. /* NULL is printed as <NULL> by sprintf: avoid that. */
  799. if (req_root == NULL)
  800. req_root = "";
  801. err = -ENOMEM;
  802. sb->s_fs_info = host_root_path =
  803. kmalloc(strlen(root_ino) + strlen(req_root) + 2, GFP_KERNEL);
  804. if (host_root_path == NULL)
  805. goto out;
  806. sprintf(host_root_path, "%s/%s", root_ino, req_root);
  807. root_inode = new_inode(sb);
  808. if (!root_inode)
  809. goto out;
  810. err = read_name(root_inode, host_root_path);
  811. if (err)
  812. goto out_put;
  813. if (S_ISLNK(root_inode->i_mode)) {
  814. char *name = follow_link(host_root_path);
  815. if (IS_ERR(name))
  816. err = PTR_ERR(name);
  817. else
  818. err = read_name(root_inode, name);
  819. kfree(name);
  820. if (err)
  821. goto out_put;
  822. }
  823. err = -ENOMEM;
  824. sb->s_root = d_make_root(root_inode);
  825. if (sb->s_root == NULL)
  826. goto out;
  827. return 0;
  828. out_put:
  829. iput(root_inode);
  830. out:
  831. return err;
  832. }
  833. static struct dentry *hostfs_read_sb(struct file_system_type *type,
  834. int flags, const char *dev_name,
  835. void *data)
  836. {
  837. return mount_nodev(type, flags, data, hostfs_fill_sb_common);
  838. }
  839. static void hostfs_kill_sb(struct super_block *s)
  840. {
  841. kill_anon_super(s);
  842. kfree(s->s_fs_info);
  843. }
  844. static struct file_system_type hostfs_type = {
  845. .owner = THIS_MODULE,
  846. .name = "hostfs",
  847. .mount = hostfs_read_sb,
  848. .kill_sb = hostfs_kill_sb,
  849. .fs_flags = 0,
  850. };
  851. static int __init init_hostfs(void)
  852. {
  853. return register_filesystem(&hostfs_type);
  854. }
  855. static void __exit exit_hostfs(void)
  856. {
  857. unregister_filesystem(&hostfs_type);
  858. }
  859. module_init(init_hostfs)
  860. module_exit(exit_hostfs)
  861. MODULE_LICENSE("GPL");