hostfs_kern.c 20 KB

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