hostfs_kern.c 21 KB

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