inode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. /*
  2. * VFS-related code for RelayFS, a high-speed data relay filesystem.
  3. *
  4. * Copyright (C) 2003-2005 - Tom Zanussi <zanussi@us.ibm.com>, IBM Corp
  5. * Copyright (C) 2003-2005 - Karim Yaghmour <karim@opersys.com>
  6. *
  7. * Based on ramfs, Copyright (C) 2002 - Linus Torvalds
  8. *
  9. * This file is released under the GPL.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/fs.h>
  13. #include <linux/mount.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/init.h>
  16. #include <linux/string.h>
  17. #include <linux/backing-dev.h>
  18. #include <linux/namei.h>
  19. #include <linux/poll.h>
  20. #include <linux/relayfs_fs.h>
  21. #include "relay.h"
  22. #include "buffers.h"
  23. #define RELAYFS_MAGIC 0xF0B4A981
  24. static struct vfsmount * relayfs_mount;
  25. static int relayfs_mount_count;
  26. static kmem_cache_t * relayfs_inode_cachep;
  27. static struct backing_dev_info relayfs_backing_dev_info = {
  28. .ra_pages = 0, /* No readahead */
  29. .capabilities = BDI_CAP_NO_ACCT_DIRTY | BDI_CAP_NO_WRITEBACK,
  30. };
  31. static struct inode *relayfs_get_inode(struct super_block *sb,
  32. int mode,
  33. struct file_operations *fops,
  34. void *data)
  35. {
  36. struct inode *inode;
  37. inode = new_inode(sb);
  38. if (!inode)
  39. return NULL;
  40. inode->i_mode = mode;
  41. inode->i_uid = 0;
  42. inode->i_gid = 0;
  43. inode->i_blksize = PAGE_CACHE_SIZE;
  44. inode->i_blocks = 0;
  45. inode->i_mapping->backing_dev_info = &relayfs_backing_dev_info;
  46. inode->i_atime = inode->i_mtime = inode->i_ctime = CURRENT_TIME;
  47. switch (mode & S_IFMT) {
  48. case S_IFREG:
  49. inode->i_fop = fops;
  50. RELAYFS_I(inode)->data = data;
  51. break;
  52. case S_IFDIR:
  53. inode->i_op = &simple_dir_inode_operations;
  54. inode->i_fop = &simple_dir_operations;
  55. /* directory inodes start off with i_nlink == 2 (for "." entry) */
  56. inode->i_nlink++;
  57. break;
  58. default:
  59. break;
  60. }
  61. return inode;
  62. }
  63. /**
  64. * relayfs_create_entry - create a relayfs directory or file
  65. * @name: the name of the file to create
  66. * @parent: parent directory
  67. * @mode: mode
  68. * @fops: file operations to use for the file
  69. * @data: user-associated data for this file
  70. *
  71. * Returns the new dentry, NULL on failure
  72. *
  73. * Creates a file or directory with the specifed permissions.
  74. */
  75. static struct dentry *relayfs_create_entry(const char *name,
  76. struct dentry *parent,
  77. int mode,
  78. struct file_operations *fops,
  79. void *data)
  80. {
  81. struct dentry *d;
  82. struct inode *inode;
  83. int error = 0;
  84. BUG_ON(!name || !(S_ISREG(mode) || S_ISDIR(mode)));
  85. error = simple_pin_fs("relayfs", &relayfs_mount, &relayfs_mount_count);
  86. if (error) {
  87. printk(KERN_ERR "Couldn't mount relayfs: errcode %d\n", error);
  88. return NULL;
  89. }
  90. if (!parent && relayfs_mount && relayfs_mount->mnt_sb)
  91. parent = relayfs_mount->mnt_sb->s_root;
  92. if (!parent) {
  93. simple_release_fs(&relayfs_mount, &relayfs_mount_count);
  94. return NULL;
  95. }
  96. parent = dget(parent);
  97. down(&parent->d_inode->i_sem);
  98. d = lookup_one_len(name, parent, strlen(name));
  99. if (IS_ERR(d)) {
  100. d = NULL;
  101. goto release_mount;
  102. }
  103. if (d->d_inode) {
  104. d = NULL;
  105. goto release_mount;
  106. }
  107. inode = relayfs_get_inode(parent->d_inode->i_sb, mode, fops, data);
  108. if (!inode) {
  109. d = NULL;
  110. goto release_mount;
  111. }
  112. d_instantiate(d, inode);
  113. dget(d); /* Extra count - pin the dentry in core */
  114. if (S_ISDIR(mode))
  115. parent->d_inode->i_nlink++;
  116. goto exit;
  117. release_mount:
  118. simple_release_fs(&relayfs_mount, &relayfs_mount_count);
  119. exit:
  120. up(&parent->d_inode->i_sem);
  121. dput(parent);
  122. return d;
  123. }
  124. /**
  125. * relayfs_create_file - create a file in the relay filesystem
  126. * @name: the name of the file to create
  127. * @parent: parent directory
  128. * @mode: mode, if not specied the default perms are used
  129. * @fops: file operations to use for the file
  130. * @data: user-associated data for this file
  131. *
  132. * Returns file dentry if successful, NULL otherwise.
  133. *
  134. * The file will be created user r on behalf of current user.
  135. */
  136. struct dentry *relayfs_create_file(const char *name,
  137. struct dentry *parent,
  138. int mode,
  139. struct file_operations *fops,
  140. void *data)
  141. {
  142. BUG_ON(!fops);
  143. if (!mode)
  144. mode = S_IRUSR;
  145. mode = (mode & S_IALLUGO) | S_IFREG;
  146. return relayfs_create_entry(name, parent, mode, fops, data);
  147. }
  148. /**
  149. * relayfs_create_dir - create a directory in the relay filesystem
  150. * @name: the name of the directory to create
  151. * @parent: parent directory, NULL if parent should be fs root
  152. *
  153. * Returns directory dentry if successful, NULL otherwise.
  154. *
  155. * The directory will be created world rwx on behalf of current user.
  156. */
  157. struct dentry *relayfs_create_dir(const char *name, struct dentry *parent)
  158. {
  159. int mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
  160. return relayfs_create_entry(name, parent, mode, NULL, NULL);
  161. }
  162. /**
  163. * relayfs_remove - remove a file or directory in the relay filesystem
  164. * @dentry: file or directory dentry
  165. *
  166. * Returns 0 if successful, negative otherwise.
  167. */
  168. int relayfs_remove(struct dentry *dentry)
  169. {
  170. struct dentry *parent;
  171. int error = 0;
  172. if (!dentry)
  173. return -EINVAL;
  174. parent = dentry->d_parent;
  175. if (!parent)
  176. return -EINVAL;
  177. parent = dget(parent);
  178. down(&parent->d_inode->i_sem);
  179. if (dentry->d_inode) {
  180. if (S_ISDIR(dentry->d_inode->i_mode))
  181. error = simple_rmdir(parent->d_inode, dentry);
  182. else
  183. error = simple_unlink(parent->d_inode, dentry);
  184. if (!error)
  185. d_delete(dentry);
  186. }
  187. if (!error)
  188. dput(dentry);
  189. up(&parent->d_inode->i_sem);
  190. dput(parent);
  191. if (!error)
  192. simple_release_fs(&relayfs_mount, &relayfs_mount_count);
  193. return error;
  194. }
  195. /**
  196. * relayfs_remove_file - remove a file from relay filesystem
  197. * @dentry: directory dentry
  198. *
  199. * Returns 0 if successful, negative otherwise.
  200. */
  201. int relayfs_remove_file(struct dentry *dentry)
  202. {
  203. return relayfs_remove(dentry);
  204. }
  205. /**
  206. * relayfs_remove_dir - remove a directory in the relay filesystem
  207. * @dentry: directory dentry
  208. *
  209. * Returns 0 if successful, negative otherwise.
  210. */
  211. int relayfs_remove_dir(struct dentry *dentry)
  212. {
  213. return relayfs_remove(dentry);
  214. }
  215. /**
  216. * relayfs_open - open file op for relayfs files
  217. * @inode: the inode
  218. * @filp: the file
  219. *
  220. * Increments the channel buffer refcount.
  221. */
  222. static int relayfs_open(struct inode *inode, struct file *filp)
  223. {
  224. struct rchan_buf *buf = RELAYFS_I(inode)->data;
  225. kref_get(&buf->kref);
  226. return 0;
  227. }
  228. /**
  229. * relayfs_mmap - mmap file op for relayfs files
  230. * @filp: the file
  231. * @vma: the vma describing what to map
  232. *
  233. * Calls upon relay_mmap_buf to map the file into user space.
  234. */
  235. static int relayfs_mmap(struct file *filp, struct vm_area_struct *vma)
  236. {
  237. struct inode *inode = filp->f_dentry->d_inode;
  238. return relay_mmap_buf(RELAYFS_I(inode)->data, vma);
  239. }
  240. /**
  241. * relayfs_poll - poll file op for relayfs files
  242. * @filp: the file
  243. * @wait: poll table
  244. *
  245. * Poll implemention.
  246. */
  247. static unsigned int relayfs_poll(struct file *filp, poll_table *wait)
  248. {
  249. unsigned int mask = 0;
  250. struct inode *inode = filp->f_dentry->d_inode;
  251. struct rchan_buf *buf = RELAYFS_I(inode)->data;
  252. if (buf->finalized)
  253. return POLLERR;
  254. if (filp->f_mode & FMODE_READ) {
  255. poll_wait(filp, &buf->read_wait, wait);
  256. if (!relay_buf_empty(buf))
  257. mask |= POLLIN | POLLRDNORM;
  258. }
  259. return mask;
  260. }
  261. /**
  262. * relayfs_release - release file op for relayfs files
  263. * @inode: the inode
  264. * @filp: the file
  265. *
  266. * Decrements the channel refcount, as the filesystem is
  267. * no longer using it.
  268. */
  269. static int relayfs_release(struct inode *inode, struct file *filp)
  270. {
  271. struct rchan_buf *buf = RELAYFS_I(inode)->data;
  272. kref_put(&buf->kref, relay_remove_buf);
  273. return 0;
  274. }
  275. /**
  276. * relayfs_read_consume - update the consumed count for the buffer
  277. */
  278. static void relayfs_read_consume(struct rchan_buf *buf,
  279. size_t read_pos,
  280. size_t bytes_consumed)
  281. {
  282. size_t subbuf_size = buf->chan->subbuf_size;
  283. size_t n_subbufs = buf->chan->n_subbufs;
  284. size_t read_subbuf;
  285. if (buf->bytes_consumed + bytes_consumed > subbuf_size) {
  286. relay_subbufs_consumed(buf->chan, buf->cpu, 1);
  287. buf->bytes_consumed = 0;
  288. }
  289. buf->bytes_consumed += bytes_consumed;
  290. read_subbuf = read_pos / buf->chan->subbuf_size;
  291. if (buf->bytes_consumed + buf->padding[read_subbuf] == subbuf_size) {
  292. if ((read_subbuf == buf->subbufs_produced % n_subbufs) &&
  293. (buf->offset == subbuf_size))
  294. return;
  295. relay_subbufs_consumed(buf->chan, buf->cpu, 1);
  296. buf->bytes_consumed = 0;
  297. }
  298. }
  299. /**
  300. * relayfs_read_avail - boolean, are there unconsumed bytes available?
  301. */
  302. static int relayfs_read_avail(struct rchan_buf *buf, size_t read_pos)
  303. {
  304. size_t bytes_produced, bytes_consumed, write_offset;
  305. size_t subbuf_size = buf->chan->subbuf_size;
  306. size_t n_subbufs = buf->chan->n_subbufs;
  307. size_t produced = buf->subbufs_produced % n_subbufs;
  308. size_t consumed = buf->subbufs_consumed % n_subbufs;
  309. write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
  310. if (consumed > produced) {
  311. if ((produced > n_subbufs) &&
  312. (produced + n_subbufs - consumed <= n_subbufs))
  313. produced += n_subbufs;
  314. } else if (consumed == produced) {
  315. if (buf->offset > subbuf_size) {
  316. produced += n_subbufs;
  317. if (buf->subbufs_produced == buf->subbufs_consumed)
  318. consumed += n_subbufs;
  319. }
  320. }
  321. if (buf->offset > subbuf_size)
  322. bytes_produced = (produced - 1) * subbuf_size + write_offset;
  323. else
  324. bytes_produced = produced * subbuf_size + write_offset;
  325. bytes_consumed = consumed * subbuf_size + buf->bytes_consumed;
  326. if (bytes_produced == bytes_consumed)
  327. return 0;
  328. relayfs_read_consume(buf, read_pos, 0);
  329. return 1;
  330. }
  331. /**
  332. * relayfs_read_subbuf_avail - return bytes available in sub-buffer
  333. */
  334. static size_t relayfs_read_subbuf_avail(size_t read_pos,
  335. struct rchan_buf *buf)
  336. {
  337. size_t padding, avail = 0;
  338. size_t read_subbuf, read_offset, write_subbuf, write_offset;
  339. size_t subbuf_size = buf->chan->subbuf_size;
  340. write_subbuf = (buf->data - buf->start) / subbuf_size;
  341. write_offset = buf->offset > subbuf_size ? subbuf_size : buf->offset;
  342. read_subbuf = read_pos / subbuf_size;
  343. read_offset = read_pos % subbuf_size;
  344. padding = buf->padding[read_subbuf];
  345. if (read_subbuf == write_subbuf) {
  346. if (read_offset + padding < write_offset)
  347. avail = write_offset - (read_offset + padding);
  348. } else
  349. avail = (subbuf_size - padding) - read_offset;
  350. return avail;
  351. }
  352. /**
  353. * relayfs_read_start_pos - find the first available byte to read
  354. *
  355. * If the read_pos is in the middle of padding, return the
  356. * position of the first actually available byte, otherwise
  357. * return the original value.
  358. */
  359. static size_t relayfs_read_start_pos(size_t read_pos,
  360. struct rchan_buf *buf)
  361. {
  362. size_t read_subbuf, padding, padding_start, padding_end;
  363. size_t subbuf_size = buf->chan->subbuf_size;
  364. size_t n_subbufs = buf->chan->n_subbufs;
  365. read_subbuf = read_pos / subbuf_size;
  366. padding = buf->padding[read_subbuf];
  367. padding_start = (read_subbuf + 1) * subbuf_size - padding;
  368. padding_end = (read_subbuf + 1) * subbuf_size;
  369. if (read_pos >= padding_start && read_pos < padding_end) {
  370. read_subbuf = (read_subbuf + 1) % n_subbufs;
  371. read_pos = read_subbuf * subbuf_size;
  372. }
  373. return read_pos;
  374. }
  375. /**
  376. * relayfs_read_end_pos - return the new read position
  377. */
  378. static size_t relayfs_read_end_pos(struct rchan_buf *buf,
  379. size_t read_pos,
  380. size_t count)
  381. {
  382. size_t read_subbuf, padding, end_pos;
  383. size_t subbuf_size = buf->chan->subbuf_size;
  384. size_t n_subbufs = buf->chan->n_subbufs;
  385. read_subbuf = read_pos / subbuf_size;
  386. padding = buf->padding[read_subbuf];
  387. if (read_pos % subbuf_size + count + padding == subbuf_size)
  388. end_pos = (read_subbuf + 1) * subbuf_size;
  389. else
  390. end_pos = read_pos + count;
  391. if (end_pos >= subbuf_size * n_subbufs)
  392. end_pos = 0;
  393. return end_pos;
  394. }
  395. /**
  396. * relayfs_read - read file op for relayfs files
  397. * @filp: the file
  398. * @buffer: the userspace buffer
  399. * @count: number of bytes to read
  400. * @ppos: position to read from
  401. *
  402. * Reads count bytes or the number of bytes available in the
  403. * current sub-buffer being read, whichever is smaller.
  404. */
  405. static ssize_t relayfs_read(struct file *filp,
  406. char __user *buffer,
  407. size_t count,
  408. loff_t *ppos)
  409. {
  410. struct inode *inode = filp->f_dentry->d_inode;
  411. struct rchan_buf *buf = RELAYFS_I(inode)->data;
  412. size_t read_start, avail;
  413. ssize_t ret = 0;
  414. void *from;
  415. down(&inode->i_sem);
  416. if(!relayfs_read_avail(buf, *ppos))
  417. goto out;
  418. read_start = relayfs_read_start_pos(*ppos, buf);
  419. avail = relayfs_read_subbuf_avail(read_start, buf);
  420. if (!avail)
  421. goto out;
  422. from = buf->start + read_start;
  423. ret = count = min(count, avail);
  424. if (copy_to_user(buffer, from, count)) {
  425. ret = -EFAULT;
  426. goto out;
  427. }
  428. relayfs_read_consume(buf, read_start, count);
  429. *ppos = relayfs_read_end_pos(buf, read_start, count);
  430. out:
  431. up(&inode->i_sem);
  432. return ret;
  433. }
  434. /**
  435. * relayfs alloc_inode() implementation
  436. */
  437. static struct inode *relayfs_alloc_inode(struct super_block *sb)
  438. {
  439. struct relayfs_inode_info *p = kmem_cache_alloc(relayfs_inode_cachep, SLAB_KERNEL);
  440. if (!p)
  441. return NULL;
  442. p->data = NULL;
  443. return &p->vfs_inode;
  444. }
  445. /**
  446. * relayfs destroy_inode() implementation
  447. */
  448. static void relayfs_destroy_inode(struct inode *inode)
  449. {
  450. kmem_cache_free(relayfs_inode_cachep, RELAYFS_I(inode));
  451. }
  452. static void init_once(void *p, kmem_cache_t *cachep, unsigned long flags)
  453. {
  454. struct relayfs_inode_info *i = p;
  455. if ((flags & (SLAB_CTOR_VERIFY | SLAB_CTOR_CONSTRUCTOR)) == SLAB_CTOR_CONSTRUCTOR)
  456. inode_init_once(&i->vfs_inode);
  457. }
  458. struct file_operations relayfs_file_operations = {
  459. .open = relayfs_open,
  460. .poll = relayfs_poll,
  461. .mmap = relayfs_mmap,
  462. .read = relayfs_read,
  463. .llseek = no_llseek,
  464. .release = relayfs_release,
  465. };
  466. static struct super_operations relayfs_ops = {
  467. .statfs = simple_statfs,
  468. .drop_inode = generic_delete_inode,
  469. .alloc_inode = relayfs_alloc_inode,
  470. .destroy_inode = relayfs_destroy_inode,
  471. };
  472. static int relayfs_fill_super(struct super_block * sb, void * data, int silent)
  473. {
  474. struct inode *inode;
  475. struct dentry *root;
  476. int mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
  477. sb->s_blocksize = PAGE_CACHE_SIZE;
  478. sb->s_blocksize_bits = PAGE_CACHE_SHIFT;
  479. sb->s_magic = RELAYFS_MAGIC;
  480. sb->s_op = &relayfs_ops;
  481. inode = relayfs_get_inode(sb, mode, NULL, NULL);
  482. if (!inode)
  483. return -ENOMEM;
  484. root = d_alloc_root(inode);
  485. if (!root) {
  486. iput(inode);
  487. return -ENOMEM;
  488. }
  489. sb->s_root = root;
  490. return 0;
  491. }
  492. static struct super_block * relayfs_get_sb(struct file_system_type *fs_type,
  493. int flags, const char *dev_name,
  494. void *data)
  495. {
  496. return get_sb_single(fs_type, flags, data, relayfs_fill_super);
  497. }
  498. static struct file_system_type relayfs_fs_type = {
  499. .owner = THIS_MODULE,
  500. .name = "relayfs",
  501. .get_sb = relayfs_get_sb,
  502. .kill_sb = kill_litter_super,
  503. };
  504. static int __init init_relayfs_fs(void)
  505. {
  506. int err;
  507. relayfs_inode_cachep = kmem_cache_create("relayfs_inode_cache",
  508. sizeof(struct relayfs_inode_info), 0,
  509. 0, init_once, NULL);
  510. if (!relayfs_inode_cachep)
  511. return -ENOMEM;
  512. err = register_filesystem(&relayfs_fs_type);
  513. if (err)
  514. kmem_cache_destroy(relayfs_inode_cachep);
  515. return err;
  516. }
  517. static void __exit exit_relayfs_fs(void)
  518. {
  519. unregister_filesystem(&relayfs_fs_type);
  520. kmem_cache_destroy(relayfs_inode_cachep);
  521. }
  522. module_init(init_relayfs_fs)
  523. module_exit(exit_relayfs_fs)
  524. EXPORT_SYMBOL_GPL(relayfs_file_operations);
  525. EXPORT_SYMBOL_GPL(relayfs_create_dir);
  526. EXPORT_SYMBOL_GPL(relayfs_remove_dir);
  527. EXPORT_SYMBOL_GPL(relayfs_create_file);
  528. EXPORT_SYMBOL_GPL(relayfs_remove_file);
  529. MODULE_AUTHOR("Tom Zanussi <zanussi@us.ibm.com> and Karim Yaghmour <karim@opersys.com>");
  530. MODULE_DESCRIPTION("Relay Filesystem");
  531. MODULE_LICENSE("GPL");