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