hostfs_kern.c 22 KB

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