inode.c 15 KB

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