hostfs_kern.c 21 KB

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