hostfs_kern.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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(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)
  80. {
  81. struct dentry *parent;
  82. char *root, *name;
  83. int len;
  84. len = 0;
  85. parent = dentry;
  86. while (parent->d_parent != parent) {
  87. len += parent->d_name.len + 1;
  88. parent = parent->d_parent;
  89. }
  90. root = parent->d_sb->s_fs_info;
  91. len += strlen(root);
  92. name = kmalloc(len + 1, GFP_KERNEL);
  93. if (name == NULL)
  94. return NULL;
  95. name[len] = '\0';
  96. parent = dentry;
  97. while (parent->d_parent != parent) {
  98. len -= parent->d_name.len + 1;
  99. name[len] = '/';
  100. strncpy(&name[len + 1], parent->d_name.name,
  101. parent->d_name.len);
  102. parent = parent->d_parent;
  103. }
  104. strncpy(name, root, strlen(root));
  105. return name;
  106. }
  107. static char *inode_name(struct inode *ino)
  108. {
  109. struct dentry *dentry;
  110. dentry = list_entry(ino->i_dentry.next, struct dentry, d_alias);
  111. return dentry_name(dentry);
  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. 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, sf->f_spare);
  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 = kzalloc(sizeof(*hi), GFP_KERNEL);
  190. if (hi == NULL)
  191. return NULL;
  192. hi->fd = -1;
  193. inode_init_once(&hi->vfs_inode);
  194. return &hi->vfs_inode;
  195. }
  196. static void hostfs_evict_inode(struct inode *inode)
  197. {
  198. truncate_inode_pages(&inode->i_data, 0);
  199. end_writeback(inode);
  200. if (HOSTFS_I(inode)->fd != -1) {
  201. close_file(&HOSTFS_I(inode)->fd);
  202. HOSTFS_I(inode)->fd = -1;
  203. }
  204. }
  205. static void hostfs_destroy_inode(struct inode *inode)
  206. {
  207. kfree(HOSTFS_I(inode));
  208. }
  209. static int hostfs_show_options(struct seq_file *seq, struct vfsmount *vfs)
  210. {
  211. const char *root_path = vfs->mnt_sb->s_fs_info;
  212. size_t offset = strlen(root_ino) + 1;
  213. if (strlen(root_path) > offset)
  214. seq_printf(seq, ",%s", root_path + offset);
  215. return 0;
  216. }
  217. static const struct super_operations hostfs_sbops = {
  218. .alloc_inode = hostfs_alloc_inode,
  219. .destroy_inode = hostfs_destroy_inode,
  220. .evict_inode = hostfs_evict_inode,
  221. .statfs = hostfs_statfs,
  222. .show_options = hostfs_show_options,
  223. };
  224. int hostfs_readdir(struct file *file, void *ent, filldir_t filldir)
  225. {
  226. void *dir;
  227. char *name;
  228. unsigned long long next, ino;
  229. int error, len;
  230. name = dentry_name(file->f_path.dentry);
  231. if (name == NULL)
  232. return -ENOMEM;
  233. dir = open_dir(name, &error);
  234. kfree(name);
  235. if (dir == NULL)
  236. return -error;
  237. next = file->f_pos;
  238. while ((name = read_dir(dir, &next, &ino, &len)) != NULL) {
  239. error = (*filldir)(ent, name, len, file->f_pos,
  240. ino, DT_UNKNOWN);
  241. if (error) break;
  242. file->f_pos = next;
  243. }
  244. close_dir(dir);
  245. return 0;
  246. }
  247. int hostfs_file_open(struct inode *ino, struct file *file)
  248. {
  249. char *name;
  250. fmode_t mode = 0;
  251. int r = 0, w = 0, fd;
  252. mode = file->f_mode & (FMODE_READ | FMODE_WRITE);
  253. if ((mode & HOSTFS_I(ino)->mode) == mode)
  254. return 0;
  255. /*
  256. * The file may already have been opened, but with the wrong access,
  257. * so this resets things and reopens the file with the new access.
  258. */
  259. if (HOSTFS_I(ino)->fd != -1) {
  260. close_file(&HOSTFS_I(ino)->fd);
  261. HOSTFS_I(ino)->fd = -1;
  262. }
  263. HOSTFS_I(ino)->mode |= mode;
  264. if (HOSTFS_I(ino)->mode & FMODE_READ)
  265. r = 1;
  266. if (HOSTFS_I(ino)->mode & FMODE_WRITE)
  267. w = 1;
  268. if (w)
  269. r = 1;
  270. name = dentry_name(file->f_path.dentry);
  271. if (name == NULL)
  272. return -ENOMEM;
  273. fd = open_file(name, r, w, append);
  274. kfree(name);
  275. if (fd < 0)
  276. return fd;
  277. FILE_HOSTFS_I(file)->fd = fd;
  278. return 0;
  279. }
  280. int hostfs_fsync(struct file *file, int datasync)
  281. {
  282. return fsync_file(HOSTFS_I(file->f_mapping->host)->fd, datasync);
  283. }
  284. static const struct file_operations hostfs_file_fops = {
  285. .llseek = generic_file_llseek,
  286. .read = do_sync_read,
  287. .splice_read = generic_file_splice_read,
  288. .aio_read = generic_file_aio_read,
  289. .aio_write = generic_file_aio_write,
  290. .write = do_sync_write,
  291. .mmap = generic_file_mmap,
  292. .open = hostfs_file_open,
  293. .release = NULL,
  294. .fsync = hostfs_fsync,
  295. };
  296. static const struct file_operations hostfs_dir_fops = {
  297. .llseek = generic_file_llseek,
  298. .readdir = hostfs_readdir,
  299. .read = generic_read_dir,
  300. };
  301. int hostfs_writepage(struct page *page, struct writeback_control *wbc)
  302. {
  303. struct address_space *mapping = page->mapping;
  304. struct inode *inode = mapping->host;
  305. char *buffer;
  306. unsigned long long base;
  307. int count = PAGE_CACHE_SIZE;
  308. int end_index = inode->i_size >> PAGE_CACHE_SHIFT;
  309. int err;
  310. if (page->index >= end_index)
  311. count = inode->i_size & (PAGE_CACHE_SIZE-1);
  312. buffer = kmap(page);
  313. base = ((unsigned long long) page->index) << PAGE_CACHE_SHIFT;
  314. err = write_file(HOSTFS_I(inode)->fd, &base, buffer, count);
  315. if (err != count) {
  316. ClearPageUptodate(page);
  317. goto out;
  318. }
  319. if (base > inode->i_size)
  320. inode->i_size = base;
  321. if (PageError(page))
  322. ClearPageError(page);
  323. err = 0;
  324. out:
  325. kunmap(page);
  326. unlock_page(page);
  327. return err;
  328. }
  329. int hostfs_readpage(struct file *file, struct page *page)
  330. {
  331. char *buffer;
  332. long long start;
  333. int err = 0;
  334. start = (long long) page->index << PAGE_CACHE_SHIFT;
  335. buffer = kmap(page);
  336. err = read_file(FILE_HOSTFS_I(file)->fd, &start, buffer,
  337. PAGE_CACHE_SIZE);
  338. if (err < 0)
  339. goto out;
  340. memset(&buffer[err], 0, PAGE_CACHE_SIZE - err);
  341. flush_dcache_page(page);
  342. SetPageUptodate(page);
  343. if (PageError(page)) ClearPageError(page);
  344. err = 0;
  345. out:
  346. kunmap(page);
  347. unlock_page(page);
  348. return err;
  349. }
  350. int hostfs_write_begin(struct file *file, struct address_space *mapping,
  351. loff_t pos, unsigned len, unsigned flags,
  352. struct page **pagep, void **fsdata)
  353. {
  354. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  355. *pagep = grab_cache_page_write_begin(mapping, index, flags);
  356. if (!*pagep)
  357. return -ENOMEM;
  358. return 0;
  359. }
  360. int hostfs_write_end(struct file *file, struct address_space *mapping,
  361. loff_t pos, unsigned len, unsigned copied,
  362. struct page *page, void *fsdata)
  363. {
  364. struct inode *inode = mapping->host;
  365. void *buffer;
  366. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  367. int err;
  368. buffer = kmap(page);
  369. err = write_file(FILE_HOSTFS_I(file)->fd, &pos, buffer + from, copied);
  370. kunmap(page);
  371. if (!PageUptodate(page) && err == PAGE_CACHE_SIZE)
  372. SetPageUptodate(page);
  373. /*
  374. * If err > 0, write_file has added err to pos, so we are comparing
  375. * i_size against the last byte written.
  376. */
  377. if (err > 0 && (pos > inode->i_size))
  378. inode->i_size = pos;
  379. unlock_page(page);
  380. page_cache_release(page);
  381. return err;
  382. }
  383. static const struct address_space_operations hostfs_aops = {
  384. .writepage = hostfs_writepage,
  385. .readpage = hostfs_readpage,
  386. .set_page_dirty = __set_page_dirty_nobuffers,
  387. .write_begin = hostfs_write_begin,
  388. .write_end = hostfs_write_end,
  389. };
  390. static int read_name(struct inode *ino, char *name)
  391. {
  392. dev_t rdev;
  393. struct hostfs_stat st;
  394. int err = stat_file(name, &st, -1);
  395. if (err)
  396. return err;
  397. /* Reencode maj and min with the kernel encoding.*/
  398. rdev = MKDEV(st.maj, st.min);
  399. switch (st.mode & S_IFMT) {
  400. case S_IFLNK:
  401. ino->i_op = &hostfs_link_iops;
  402. break;
  403. case S_IFDIR:
  404. ino->i_op = &hostfs_dir_iops;
  405. ino->i_fop = &hostfs_dir_fops;
  406. break;
  407. case S_IFCHR:
  408. case S_IFBLK:
  409. case S_IFIFO:
  410. case S_IFSOCK:
  411. init_special_inode(ino, st.mode & S_IFMT, rdev);
  412. ino->i_op = &hostfs_iops;
  413. break;
  414. default:
  415. ino->i_op = &hostfs_iops;
  416. ino->i_fop = &hostfs_file_fops;
  417. ino->i_mapping->a_ops = &hostfs_aops;
  418. }
  419. ino->i_ino = st.ino;
  420. ino->i_mode = st.mode;
  421. ino->i_nlink = st.nlink;
  422. ino->i_uid = st.uid;
  423. ino->i_gid = st.gid;
  424. ino->i_atime = st.atime;
  425. ino->i_mtime = st.mtime;
  426. ino->i_ctime = st.ctime;
  427. ino->i_size = st.size;
  428. ino->i_blocks = st.blocks;
  429. return 0;
  430. }
  431. int hostfs_create(struct inode *dir, struct dentry *dentry, int mode,
  432. struct nameidata *nd)
  433. {
  434. struct inode *inode;
  435. char *name;
  436. int error, fd;
  437. inode = hostfs_iget(dir->i_sb);
  438. if (IS_ERR(inode)) {
  439. error = PTR_ERR(inode);
  440. goto out;
  441. }
  442. error = -ENOMEM;
  443. name = dentry_name(dentry);
  444. if (name == NULL)
  445. goto out_put;
  446. fd = file_create(name,
  447. mode & S_IRUSR, mode & S_IWUSR, mode & S_IXUSR,
  448. mode & S_IRGRP, mode & S_IWGRP, mode & S_IXGRP,
  449. mode & S_IROTH, mode & S_IWOTH, mode & S_IXOTH);
  450. if (fd < 0)
  451. error = fd;
  452. else
  453. error = read_name(inode, name);
  454. kfree(name);
  455. if (error)
  456. goto out_put;
  457. HOSTFS_I(inode)->fd = fd;
  458. HOSTFS_I(inode)->mode = FMODE_READ | FMODE_WRITE;
  459. d_instantiate(dentry, inode);
  460. return 0;
  461. out_put:
  462. iput(inode);
  463. out:
  464. return error;
  465. }
  466. struct dentry *hostfs_lookup(struct inode *ino, struct dentry *dentry,
  467. struct nameidata *nd)
  468. {
  469. struct inode *inode;
  470. char *name;
  471. int err;
  472. inode = hostfs_iget(ino->i_sb);
  473. if (IS_ERR(inode)) {
  474. err = PTR_ERR(inode);
  475. goto out;
  476. }
  477. err = -ENOMEM;
  478. name = dentry_name(dentry);
  479. if (name == NULL)
  480. goto out_put;
  481. err = read_name(inode, name);
  482. kfree(name);
  483. if (err == -ENOENT) {
  484. iput(inode);
  485. inode = NULL;
  486. }
  487. else if (err)
  488. goto out_put;
  489. d_add(dentry, inode);
  490. dentry->d_op = &hostfs_dentry_ops;
  491. return NULL;
  492. out_put:
  493. iput(inode);
  494. out:
  495. return ERR_PTR(err);
  496. }
  497. int hostfs_link(struct dentry *to, struct inode *ino, struct dentry *from)
  498. {
  499. char *from_name, *to_name;
  500. int err;
  501. if ((from_name = dentry_name(from)) == NULL)
  502. return -ENOMEM;
  503. to_name = dentry_name(to);
  504. if (to_name == NULL) {
  505. kfree(from_name);
  506. return -ENOMEM;
  507. }
  508. err = link_file(to_name, from_name);
  509. kfree(from_name);
  510. kfree(to_name);
  511. return err;
  512. }
  513. int hostfs_unlink(struct inode *ino, struct dentry *dentry)
  514. {
  515. char *file;
  516. int err;
  517. if ((file = dentry_name(dentry)) == NULL)
  518. return -ENOMEM;
  519. if (append)
  520. return -EPERM;
  521. err = unlink_file(file);
  522. kfree(file);
  523. return err;
  524. }
  525. int hostfs_symlink(struct inode *ino, struct dentry *dentry, const char *to)
  526. {
  527. char *file;
  528. int err;
  529. if ((file = dentry_name(dentry)) == NULL)
  530. return -ENOMEM;
  531. err = make_symlink(file, to);
  532. kfree(file);
  533. return err;
  534. }
  535. int hostfs_mkdir(struct inode *ino, struct dentry *dentry, int mode)
  536. {
  537. char *file;
  538. int err;
  539. if ((file = dentry_name(dentry)) == NULL)
  540. return -ENOMEM;
  541. err = do_mkdir(file, mode);
  542. kfree(file);
  543. return err;
  544. }
  545. int hostfs_rmdir(struct inode *ino, struct dentry *dentry)
  546. {
  547. char *file;
  548. int err;
  549. if ((file = dentry_name(dentry)) == NULL)
  550. return -ENOMEM;
  551. err = do_rmdir(file);
  552. kfree(file);
  553. return err;
  554. }
  555. int hostfs_mknod(struct inode *dir, struct dentry *dentry, int mode, dev_t dev)
  556. {
  557. struct inode *inode;
  558. char *name;
  559. int err;
  560. inode = hostfs_iget(dir->i_sb);
  561. if (IS_ERR(inode)) {
  562. err = PTR_ERR(inode);
  563. goto out;
  564. }
  565. err = -ENOMEM;
  566. name = dentry_name(dentry);
  567. if (name == NULL)
  568. goto out_put;
  569. init_special_inode(inode, mode, dev);
  570. err = do_mknod(name, mode, MAJOR(dev), MINOR(dev));
  571. if (err)
  572. goto out_free;
  573. err = read_name(inode, name);
  574. if (err)
  575. goto out_put;
  576. kfree(name);
  577. if (err)
  578. goto out_put;
  579. d_instantiate(dentry, inode);
  580. return 0;
  581. out_free:
  582. kfree(name);
  583. out_put:
  584. iput(inode);
  585. out:
  586. return err;
  587. }
  588. int hostfs_rename(struct inode *from_ino, struct dentry *from,
  589. struct inode *to_ino, struct dentry *to)
  590. {
  591. char *from_name, *to_name;
  592. int err;
  593. if ((from_name = dentry_name(from)) == NULL)
  594. return -ENOMEM;
  595. if ((to_name = dentry_name(to)) == NULL) {
  596. kfree(from_name);
  597. return -ENOMEM;
  598. }
  599. err = rename_file(from_name, to_name);
  600. kfree(from_name);
  601. kfree(to_name);
  602. return err;
  603. }
  604. int hostfs_permission(struct inode *ino, int desired)
  605. {
  606. char *name;
  607. int r = 0, w = 0, x = 0, err;
  608. if (desired & MAY_READ) r = 1;
  609. if (desired & MAY_WRITE) w = 1;
  610. if (desired & MAY_EXEC) x = 1;
  611. name = inode_name(ino);
  612. if (name == NULL)
  613. return -ENOMEM;
  614. if (S_ISCHR(ino->i_mode) || S_ISBLK(ino->i_mode) ||
  615. S_ISFIFO(ino->i_mode) || S_ISSOCK(ino->i_mode))
  616. err = 0;
  617. else
  618. err = access_file(name, r, w, x);
  619. kfree(name);
  620. if (!err)
  621. err = generic_permission(ino, desired, NULL);
  622. return err;
  623. }
  624. int hostfs_setattr(struct dentry *dentry, struct iattr *attr)
  625. {
  626. struct inode *inode = dentry->d_inode;
  627. struct hostfs_iattr attrs;
  628. char *name;
  629. int err;
  630. int fd = HOSTFS_I(inode)->fd;
  631. err = inode_change_ok(inode, attr);
  632. if (err)
  633. return err;
  634. if (append)
  635. attr->ia_valid &= ~ATTR_SIZE;
  636. attrs.ia_valid = 0;
  637. if (attr->ia_valid & ATTR_MODE) {
  638. attrs.ia_valid |= HOSTFS_ATTR_MODE;
  639. attrs.ia_mode = attr->ia_mode;
  640. }
  641. if (attr->ia_valid & ATTR_UID) {
  642. attrs.ia_valid |= HOSTFS_ATTR_UID;
  643. attrs.ia_uid = attr->ia_uid;
  644. }
  645. if (attr->ia_valid & ATTR_GID) {
  646. attrs.ia_valid |= HOSTFS_ATTR_GID;
  647. attrs.ia_gid = attr->ia_gid;
  648. }
  649. if (attr->ia_valid & ATTR_SIZE) {
  650. attrs.ia_valid |= HOSTFS_ATTR_SIZE;
  651. attrs.ia_size = attr->ia_size;
  652. }
  653. if (attr->ia_valid & ATTR_ATIME) {
  654. attrs.ia_valid |= HOSTFS_ATTR_ATIME;
  655. attrs.ia_atime = attr->ia_atime;
  656. }
  657. if (attr->ia_valid & ATTR_MTIME) {
  658. attrs.ia_valid |= HOSTFS_ATTR_MTIME;
  659. attrs.ia_mtime = attr->ia_mtime;
  660. }
  661. if (attr->ia_valid & ATTR_CTIME) {
  662. attrs.ia_valid |= HOSTFS_ATTR_CTIME;
  663. attrs.ia_ctime = attr->ia_ctime;
  664. }
  665. if (attr->ia_valid & ATTR_ATIME_SET) {
  666. attrs.ia_valid |= HOSTFS_ATTR_ATIME_SET;
  667. }
  668. if (attr->ia_valid & ATTR_MTIME_SET) {
  669. attrs.ia_valid |= HOSTFS_ATTR_MTIME_SET;
  670. }
  671. name = dentry_name(dentry);
  672. if (name == NULL)
  673. return -ENOMEM;
  674. err = set_attr(name, &attrs, fd);
  675. kfree(name);
  676. if (err)
  677. return err;
  678. if ((attr->ia_valid & ATTR_SIZE) &&
  679. attr->ia_size != i_size_read(inode)) {
  680. int error;
  681. error = vmtruncate(inode, attr->ia_size);
  682. if (err)
  683. return err;
  684. }
  685. setattr_copy(inode, attr);
  686. mark_inode_dirty(inode);
  687. return 0;
  688. }
  689. static const struct inode_operations hostfs_iops = {
  690. .create = hostfs_create,
  691. .link = hostfs_link,
  692. .unlink = hostfs_unlink,
  693. .symlink = hostfs_symlink,
  694. .mkdir = hostfs_mkdir,
  695. .rmdir = hostfs_rmdir,
  696. .mknod = hostfs_mknod,
  697. .rename = hostfs_rename,
  698. .permission = hostfs_permission,
  699. .setattr = hostfs_setattr,
  700. };
  701. static const struct inode_operations hostfs_dir_iops = {
  702. .create = hostfs_create,
  703. .lookup = hostfs_lookup,
  704. .link = hostfs_link,
  705. .unlink = hostfs_unlink,
  706. .symlink = hostfs_symlink,
  707. .mkdir = hostfs_mkdir,
  708. .rmdir = hostfs_rmdir,
  709. .mknod = hostfs_mknod,
  710. .rename = hostfs_rename,
  711. .permission = hostfs_permission,
  712. .setattr = hostfs_setattr,
  713. };
  714. static void *hostfs_follow_link(struct dentry *dentry, struct nameidata *nd)
  715. {
  716. char *link = __getname();
  717. if (link) {
  718. char *path = dentry_name(dentry);
  719. int err = -ENOMEM;
  720. if (path) {
  721. int err = hostfs_do_readlink(path, link, PATH_MAX);
  722. if (err == PATH_MAX)
  723. err = -E2BIG;
  724. kfree(path);
  725. }
  726. if (err < 0) {
  727. __putname(link);
  728. link = ERR_PTR(err);
  729. }
  730. } else {
  731. link = ERR_PTR(-ENOMEM);
  732. }
  733. nd_set_link(nd, link);
  734. return NULL;
  735. }
  736. static void hostfs_put_link(struct dentry *dentry, struct nameidata *nd, void *cookie)
  737. {
  738. char *s = nd_get_link(nd);
  739. if (!IS_ERR(s))
  740. __putname(s);
  741. }
  742. static const struct inode_operations hostfs_link_iops = {
  743. .readlink = generic_readlink,
  744. .follow_link = hostfs_follow_link,
  745. .put_link = hostfs_put_link,
  746. };
  747. static int hostfs_fill_sb_common(struct super_block *sb, void *d, int silent)
  748. {
  749. struct inode *root_inode;
  750. char *host_root_path, *req_root = d;
  751. int err;
  752. sb->s_blocksize = 1024;
  753. sb->s_blocksize_bits = 10;
  754. sb->s_magic = HOSTFS_SUPER_MAGIC;
  755. sb->s_op = &hostfs_sbops;
  756. sb->s_maxbytes = MAX_LFS_FILESIZE;
  757. /* NULL is printed as <NULL> by sprintf: avoid that. */
  758. if (req_root == NULL)
  759. req_root = "";
  760. err = -ENOMEM;
  761. sb->s_fs_info = host_root_path =
  762. kmalloc(strlen(root_ino) + strlen(req_root) + 2, GFP_KERNEL);
  763. if (host_root_path == NULL)
  764. goto out;
  765. sprintf(host_root_path, "%s/%s", root_ino, req_root);
  766. root_inode = new_inode(sb);
  767. if (!root_inode)
  768. goto out;
  769. err = read_name(root_inode, host_root_path);
  770. if (err)
  771. goto out_put;
  772. if (S_ISLNK(root_inode->i_mode)) {
  773. char *name = follow_link(host_root_path);
  774. if (IS_ERR(name))
  775. err = PTR_ERR(name);
  776. else
  777. err = read_name(root_inode, name);
  778. kfree(name);
  779. if (err)
  780. goto out_put;
  781. }
  782. err = -ENOMEM;
  783. sb->s_root = d_alloc_root(root_inode);
  784. if (sb->s_root == NULL)
  785. goto out_put;
  786. return 0;
  787. out_put:
  788. iput(root_inode);
  789. out:
  790. return err;
  791. }
  792. static int hostfs_read_sb(struct file_system_type *type,
  793. int flags, const char *dev_name,
  794. void *data, struct vfsmount *mnt)
  795. {
  796. return get_sb_nodev(type, flags, data, hostfs_fill_sb_common, mnt);
  797. }
  798. static void hostfs_kill_sb(struct super_block *s)
  799. {
  800. kill_anon_super(s);
  801. kfree(s->s_fs_info);
  802. }
  803. static struct file_system_type hostfs_type = {
  804. .owner = THIS_MODULE,
  805. .name = "hostfs",
  806. .get_sb = hostfs_read_sb,
  807. .kill_sb = hostfs_kill_sb,
  808. .fs_flags = 0,
  809. };
  810. static int __init init_hostfs(void)
  811. {
  812. return register_filesystem(&hostfs_type);
  813. }
  814. static void __exit exit_hostfs(void)
  815. {
  816. unregister_filesystem(&hostfs_type);
  817. }
  818. module_init(init_hostfs)
  819. module_exit(exit_hostfs)
  820. MODULE_LICENSE("GPL");