David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
| 3 | * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved. |
| 4 | * |
| 5 | * This copyrighted material is made available to anyone wishing to use, |
| 6 | * modify, copy, or redistribute it subject to the terms and conditions |
| 7 | * of the GNU General Public License v.2. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/sched.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/spinlock.h> |
| 13 | #include <linux/completion.h> |
| 14 | #include <linux/buffer_head.h> |
| 15 | #include <linux/pagemap.h> |
| 16 | #include <linux/uio.h> |
| 17 | #include <linux/blkdev.h> |
| 18 | #include <linux/mm.h> |
| 19 | #include <linux/smp_lock.h> |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 20 | #include <linux/fs.h> |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 21 | #include <linux/gfs2_ondisk.h> |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 22 | #include <linux/ext2_fs.h> |
| 23 | #include <linux/crc32.h> |
Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 24 | #include <linux/iflags.h> |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 25 | #include <asm/semaphore.h> |
| 26 | #include <asm/uaccess.h> |
| 27 | |
| 28 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 29 | #include "lm_interface.h" |
| 30 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 31 | #include "bmap.h" |
| 32 | #include "dir.h" |
| 33 | #include "glock.h" |
| 34 | #include "glops.h" |
| 35 | #include "inode.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 36 | #include "lm.h" |
| 37 | #include "log.h" |
| 38 | #include "meta_io.h" |
| 39 | #include "ops_file.h" |
| 40 | #include "ops_vm.h" |
| 41 | #include "quota.h" |
| 42 | #include "rgrp.h" |
| 43 | #include "trans.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 44 | #include "util.h" |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 45 | #include "eaops.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 46 | |
| 47 | /* "bad" is for NFS support */ |
| 48 | struct filldir_bad_entry { |
| 49 | char *fbe_name; |
| 50 | unsigned int fbe_length; |
| 51 | uint64_t fbe_offset; |
| 52 | struct gfs2_inum fbe_inum; |
| 53 | unsigned int fbe_type; |
| 54 | }; |
| 55 | |
| 56 | struct filldir_bad { |
| 57 | struct gfs2_sbd *fdb_sbd; |
| 58 | |
| 59 | struct filldir_bad_entry *fdb_entry; |
| 60 | unsigned int fdb_entry_num; |
| 61 | unsigned int fdb_entry_off; |
| 62 | |
| 63 | char *fdb_name; |
| 64 | unsigned int fdb_name_size; |
| 65 | unsigned int fdb_name_off; |
| 66 | }; |
| 67 | |
| 68 | /* For regular, non-NFS */ |
| 69 | struct filldir_reg { |
| 70 | struct gfs2_sbd *fdr_sbd; |
| 71 | int fdr_prefetch; |
| 72 | |
| 73 | filldir_t fdr_filldir; |
| 74 | void *fdr_opaque; |
| 75 | }; |
| 76 | |
Steven Whitehouse | 61a30dc | 2006-02-15 10:15:18 +0000 | [diff] [blame] | 77 | /* |
| 78 | * Most fields left uninitialised to catch anybody who tries to |
| 79 | * use them. f_flags set to prevent file_accessed() from touching |
| 80 | * any other part of this. Its use is purely as a flag so that we |
| 81 | * know (in readpage()) whether or not do to locking. |
| 82 | */ |
| 83 | struct file gfs2_internal_file_sentinal = { |
| 84 | .f_flags = O_NOATIME|O_RDONLY, |
| 85 | }; |
| 86 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 87 | static int gfs2_read_actor(read_descriptor_t *desc, struct page *page, |
| 88 | unsigned long offset, unsigned long size) |
| 89 | { |
| 90 | char *kaddr; |
| 91 | unsigned long count = desc->count; |
| 92 | |
| 93 | if (size > count) |
| 94 | size = count; |
| 95 | |
| 96 | kaddr = kmap(page); |
| 97 | memcpy(desc->arg.buf, kaddr + offset, size); |
| 98 | kunmap(page); |
| 99 | |
| 100 | desc->count = count - size; |
| 101 | desc->written += size; |
| 102 | desc->arg.buf += size; |
| 103 | return size; |
| 104 | } |
| 105 | |
| 106 | int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state, |
| 107 | char *buf, loff_t *pos, unsigned size) |
| 108 | { |
| 109 | struct inode *inode = ip->i_vnode; |
| 110 | read_descriptor_t desc; |
| 111 | desc.written = 0; |
| 112 | desc.arg.buf = buf; |
| 113 | desc.count = size; |
| 114 | desc.error = 0; |
Steven Whitehouse | 61a30dc | 2006-02-15 10:15:18 +0000 | [diff] [blame] | 115 | do_generic_mapping_read(inode->i_mapping, ra_state, |
| 116 | &gfs2_internal_file_sentinal, pos, &desc, |
| 117 | gfs2_read_actor); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 118 | return desc.written ? desc.written : desc.error; |
| 119 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 120 | |
| 121 | /** |
| 122 | * gfs2_llseek - seek to a location in a file |
| 123 | * @file: the file |
| 124 | * @offset: the offset |
| 125 | * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END) |
| 126 | * |
| 127 | * SEEK_END requires the glock for the file because it references the |
| 128 | * file's size. |
| 129 | * |
| 130 | * Returns: The new offset, or errno |
| 131 | */ |
| 132 | |
| 133 | static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin) |
| 134 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 135 | struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 136 | struct gfs2_holder i_gh; |
| 137 | loff_t error; |
| 138 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 139 | if (origin == 2) { |
| 140 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, |
| 141 | &i_gh); |
| 142 | if (!error) { |
| 143 | error = remote_llseek(file, offset, origin); |
| 144 | gfs2_glock_dq_uninit(&i_gh); |
| 145 | } |
| 146 | } else |
| 147 | error = remote_llseek(file, offset, origin); |
| 148 | |
| 149 | return error; |
| 150 | } |
| 151 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 152 | |
| 153 | static ssize_t gfs2_direct_IO_read(struct kiocb *iocb, const struct iovec *iov, |
| 154 | loff_t offset, unsigned long nr_segs) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 155 | { |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 156 | struct file *file = iocb->ki_filp; |
| 157 | struct address_space *mapping = file->f_mapping; |
| 158 | ssize_t retval; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 159 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 160 | retval = filemap_write_and_wait(mapping); |
| 161 | if (retval == 0) { |
| 162 | retval = mapping->a_ops->direct_IO(READ, iocb, iov, offset, |
| 163 | nr_segs); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 164 | } |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 165 | return retval; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /** |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 169 | * __gfs2_file_aio_read - The main GFS2 read function |
| 170 | * |
| 171 | * N.B. This is almost, but not quite the same as __generic_file_aio_read() |
| 172 | * the important subtle different being that inode->i_size isn't valid |
| 173 | * unless we are holding a lock, and we do this _only_ on the O_DIRECT |
| 174 | * path since otherwise locking is done entirely at the page cache |
| 175 | * layer. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 176 | */ |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 177 | static ssize_t __gfs2_file_aio_read(struct kiocb *iocb, |
| 178 | const struct iovec *iov, |
| 179 | unsigned long nr_segs, loff_t *ppos) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 180 | { |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 181 | struct file *filp = iocb->ki_filp; |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 182 | struct gfs2_inode *ip = filp->f_mapping->host->u.generic_ip; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 183 | struct gfs2_holder gh; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 184 | ssize_t retval; |
| 185 | unsigned long seg; |
| 186 | size_t count; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 187 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 188 | count = 0; |
| 189 | for (seg = 0; seg < nr_segs; seg++) { |
| 190 | const struct iovec *iv = &iov[seg]; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 191 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 192 | /* |
| 193 | * If any segment has a negative length, or the cumulative |
| 194 | * length ever wraps negative then return -EINVAL. |
| 195 | */ |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 196 | count += iv->iov_len; |
| 197 | if (unlikely((ssize_t)(count|iv->iov_len) < 0)) |
| 198 | return -EINVAL; |
| 199 | if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len)) |
| 200 | continue; |
| 201 | if (seg == 0) |
| 202 | return -EFAULT; |
| 203 | nr_segs = seg; |
| 204 | count -= iv->iov_len; /* This segment is no good */ |
| 205 | break; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 208 | /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */ |
| 209 | if (filp->f_flags & O_DIRECT) { |
| 210 | loff_t pos = *ppos, size; |
| 211 | struct address_space *mapping; |
| 212 | struct inode *inode; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 213 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 214 | mapping = filp->f_mapping; |
| 215 | inode = mapping->host; |
| 216 | retval = 0; |
| 217 | if (!count) |
| 218 | goto out; /* skip atime */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 219 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 220 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh); |
| 221 | retval = gfs2_glock_nq_m_atime(1, &gh); |
| 222 | if (retval) |
| 223 | goto out; |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 224 | if (gfs2_is_stuffed(ip)) { |
| 225 | gfs2_glock_dq_m(1, &gh); |
| 226 | gfs2_holder_uninit(&gh); |
| 227 | goto fallback_to_normal; |
| 228 | } |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 229 | size = i_size_read(inode); |
| 230 | if (pos < size) { |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 231 | retval = gfs2_direct_IO_read(iocb, iov, pos, nr_segs); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 232 | if (retval > 0 && !is_sync_kiocb(iocb)) |
| 233 | retval = -EIOCBQUEUED; |
| 234 | if (retval > 0) |
| 235 | *ppos = pos + retval; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 236 | } |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 237 | file_accessed(filp); |
| 238 | gfs2_glock_dq_m(1, &gh); |
| 239 | gfs2_holder_uninit(&gh); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 240 | goto out; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 241 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 242 | |
Steven Whitehouse | d1665e4 | 2006-02-14 11:54:42 +0000 | [diff] [blame] | 243 | fallback_to_normal: |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 244 | retval = 0; |
| 245 | if (count) { |
| 246 | for (seg = 0; seg < nr_segs; seg++) { |
| 247 | read_descriptor_t desc; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 248 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 249 | desc.written = 0; |
| 250 | desc.arg.buf = iov[seg].iov_base; |
| 251 | desc.count = iov[seg].iov_len; |
| 252 | if (desc.count == 0) |
| 253 | continue; |
| 254 | desc.error = 0; |
| 255 | do_generic_file_read(filp,ppos,&desc,file_read_actor); |
| 256 | retval += desc.written; |
| 257 | if (desc.error) { |
| 258 | retval = retval ?: desc.error; |
| 259 | break; |
| 260 | } |
| 261 | } |
| 262 | } |
| 263 | out: |
| 264 | return retval; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | /** |
| 268 | * gfs2_read - Read bytes from a file |
| 269 | * @file: The file to read from |
| 270 | * @buf: The buffer to copy into |
| 271 | * @size: The amount of data requested |
| 272 | * @offset: The current file offset |
| 273 | * |
| 274 | * Outputs: Offset - updated according to number of bytes read |
| 275 | * |
| 276 | * Returns: The number of bytes read, errno on failure |
| 277 | */ |
| 278 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 279 | static ssize_t gfs2_read(struct file *filp, char __user *buf, size_t size, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 280 | loff_t *offset) |
| 281 | { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 282 | struct iovec local_iov = { .iov_base = buf, .iov_len = size }; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 283 | struct kiocb kiocb; |
| 284 | ssize_t ret; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 285 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 286 | init_sync_kiocb(&kiocb, filp); |
| 287 | ret = __gfs2_file_aio_read(&kiocb, &local_iov, 1, offset); |
| 288 | if (-EIOCBQUEUED == ret) |
| 289 | ret = wait_on_sync_kiocb(&kiocb); |
| 290 | return ret; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 293 | static ssize_t gfs2_file_readv(struct file *filp, const struct iovec *iov, |
| 294 | unsigned long nr_segs, loff_t *ppos) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 295 | { |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 296 | struct kiocb kiocb; |
| 297 | ssize_t ret; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 298 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 299 | init_sync_kiocb(&kiocb, filp); |
| 300 | ret = __gfs2_file_aio_read(&kiocb, iov, nr_segs, ppos); |
| 301 | if (-EIOCBQUEUED == ret) |
| 302 | ret = wait_on_sync_kiocb(&kiocb); |
| 303 | return ret; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 304 | } |
| 305 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 306 | static ssize_t gfs2_file_aio_read(struct kiocb *iocb, char __user *buf, |
| 307 | size_t count, loff_t pos) |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 308 | { |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 309 | struct iovec local_iov = { .iov_base = buf, .iov_len = count }; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 310 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 311 | BUG_ON(iocb->ki_pos != pos); |
| 312 | return __gfs2_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 313 | } |
| 314 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 315 | |
| 316 | /** |
| 317 | * filldir_reg_func - Report a directory entry to the caller of gfs2_dir_read() |
| 318 | * @opaque: opaque data used by the function |
| 319 | * @name: the name of the directory entry |
| 320 | * @length: the length of the name |
| 321 | * @offset: the entry's offset in the directory |
| 322 | * @inum: the inode number the entry points to |
| 323 | * @type: the type of inode the entry points to |
| 324 | * |
| 325 | * Returns: 0 on success, 1 if buffer full |
| 326 | */ |
| 327 | |
| 328 | static int filldir_reg_func(void *opaque, const char *name, unsigned int length, |
| 329 | uint64_t offset, struct gfs2_inum *inum, |
| 330 | unsigned int type) |
| 331 | { |
| 332 | struct filldir_reg *fdr = (struct filldir_reg *)opaque; |
| 333 | struct gfs2_sbd *sdp = fdr->fdr_sbd; |
| 334 | int error; |
| 335 | |
| 336 | error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset, |
| 337 | inum->no_formal_ino, type); |
| 338 | if (error) |
| 339 | return 1; |
| 340 | |
| 341 | if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) { |
| 342 | gfs2_glock_prefetch_num(sdp, |
| 343 | inum->no_addr, &gfs2_inode_glops, |
| 344 | LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY); |
| 345 | gfs2_glock_prefetch_num(sdp, |
| 346 | inum->no_addr, &gfs2_iopen_glops, |
| 347 | LM_ST_SHARED, LM_FLAG_TRY); |
| 348 | } |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | /** |
| 354 | * readdir_reg - Read directory entries from a directory |
| 355 | * @file: The directory to read from |
| 356 | * @dirent: Buffer for dirents |
| 357 | * @filldir: Function used to do the copying |
| 358 | * |
| 359 | * Returns: errno |
| 360 | */ |
| 361 | |
| 362 | static int readdir_reg(struct file *file, void *dirent, filldir_t filldir) |
| 363 | { |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 364 | struct inode *dir = file->f_mapping->host; |
| 365 | struct gfs2_inode *dip = dir->u.generic_ip; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 366 | struct filldir_reg fdr; |
| 367 | struct gfs2_holder d_gh; |
| 368 | uint64_t offset = file->f_pos; |
| 369 | int error; |
| 370 | |
| 371 | fdr.fdr_sbd = dip->i_sbd; |
| 372 | fdr.fdr_prefetch = 1; |
| 373 | fdr.fdr_filldir = filldir; |
| 374 | fdr.fdr_opaque = dirent; |
| 375 | |
| 376 | gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh); |
| 377 | error = gfs2_glock_nq_atime(&d_gh); |
| 378 | if (error) { |
| 379 | gfs2_holder_uninit(&d_gh); |
| 380 | return error; |
| 381 | } |
| 382 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 383 | error = gfs2_dir_read(dir, &offset, &fdr, filldir_reg_func); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 384 | |
| 385 | gfs2_glock_dq_uninit(&d_gh); |
| 386 | |
| 387 | file->f_pos = offset; |
| 388 | |
| 389 | return error; |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * filldir_bad_func - Report a directory entry to the caller of gfs2_dir_read() |
| 394 | * @opaque: opaque data used by the function |
| 395 | * @name: the name of the directory entry |
| 396 | * @length: the length of the name |
| 397 | * @offset: the entry's offset in the directory |
| 398 | * @inum: the inode number the entry points to |
| 399 | * @type: the type of inode the entry points to |
| 400 | * |
| 401 | * For supporting NFS. |
| 402 | * |
| 403 | * Returns: 0 on success, 1 if buffer full |
| 404 | */ |
| 405 | |
| 406 | static int filldir_bad_func(void *opaque, const char *name, unsigned int length, |
| 407 | uint64_t offset, struct gfs2_inum *inum, |
| 408 | unsigned int type) |
| 409 | { |
| 410 | struct filldir_bad *fdb = (struct filldir_bad *)opaque; |
| 411 | struct gfs2_sbd *sdp = fdb->fdb_sbd; |
| 412 | struct filldir_bad_entry *fbe; |
| 413 | |
| 414 | if (fdb->fdb_entry_off == fdb->fdb_entry_num || |
| 415 | fdb->fdb_name_off + length > fdb->fdb_name_size) |
| 416 | return 1; |
| 417 | |
| 418 | fbe = &fdb->fdb_entry[fdb->fdb_entry_off]; |
| 419 | fbe->fbe_name = fdb->fdb_name + fdb->fdb_name_off; |
| 420 | memcpy(fbe->fbe_name, name, length); |
| 421 | fbe->fbe_length = length; |
| 422 | fbe->fbe_offset = offset; |
| 423 | fbe->fbe_inum = *inum; |
| 424 | fbe->fbe_type = type; |
| 425 | |
| 426 | fdb->fdb_entry_off++; |
| 427 | fdb->fdb_name_off += length; |
| 428 | |
| 429 | if (!(length == 1 && *name == '.')) { |
| 430 | gfs2_glock_prefetch_num(sdp, |
| 431 | inum->no_addr, &gfs2_inode_glops, |
| 432 | LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY); |
| 433 | gfs2_glock_prefetch_num(sdp, |
| 434 | inum->no_addr, &gfs2_iopen_glops, |
| 435 | LM_ST_SHARED, LM_FLAG_TRY); |
| 436 | } |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | /** |
| 442 | * readdir_bad - Read directory entries from a directory |
| 443 | * @file: The directory to read from |
| 444 | * @dirent: Buffer for dirents |
| 445 | * @filldir: Function used to do the copying |
| 446 | * |
| 447 | * For supporting NFS. |
| 448 | * |
| 449 | * Returns: errno |
| 450 | */ |
| 451 | |
| 452 | static int readdir_bad(struct file *file, void *dirent, filldir_t filldir) |
| 453 | { |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 454 | struct inode *dir = file->f_mapping->host; |
| 455 | struct gfs2_inode *dip = dir->u.generic_ip; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 456 | struct gfs2_sbd *sdp = dip->i_sbd; |
| 457 | struct filldir_reg fdr; |
| 458 | unsigned int entries, size; |
| 459 | struct filldir_bad *fdb; |
| 460 | struct gfs2_holder d_gh; |
| 461 | uint64_t offset = file->f_pos; |
| 462 | unsigned int x; |
| 463 | struct filldir_bad_entry *fbe; |
| 464 | int error; |
| 465 | |
| 466 | entries = gfs2_tune_get(sdp, gt_entries_per_readdir); |
| 467 | size = sizeof(struct filldir_bad) + |
| 468 | entries * (sizeof(struct filldir_bad_entry) + GFS2_FAST_NAME_SIZE); |
| 469 | |
| 470 | fdb = kzalloc(size, GFP_KERNEL); |
| 471 | if (!fdb) |
| 472 | return -ENOMEM; |
| 473 | |
| 474 | fdb->fdb_sbd = sdp; |
| 475 | fdb->fdb_entry = (struct filldir_bad_entry *)(fdb + 1); |
| 476 | fdb->fdb_entry_num = entries; |
| 477 | fdb->fdb_name = ((char *)fdb) + sizeof(struct filldir_bad) + |
| 478 | entries * sizeof(struct filldir_bad_entry); |
| 479 | fdb->fdb_name_size = entries * GFS2_FAST_NAME_SIZE; |
| 480 | |
| 481 | gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh); |
| 482 | error = gfs2_glock_nq_atime(&d_gh); |
| 483 | if (error) { |
| 484 | gfs2_holder_uninit(&d_gh); |
| 485 | goto out; |
| 486 | } |
| 487 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 488 | error = gfs2_dir_read(dir, &offset, fdb, filldir_bad_func); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 489 | |
| 490 | gfs2_glock_dq_uninit(&d_gh); |
| 491 | |
| 492 | fdr.fdr_sbd = sdp; |
| 493 | fdr.fdr_prefetch = 0; |
| 494 | fdr.fdr_filldir = filldir; |
| 495 | fdr.fdr_opaque = dirent; |
| 496 | |
| 497 | for (x = 0; x < fdb->fdb_entry_off; x++) { |
| 498 | fbe = &fdb->fdb_entry[x]; |
| 499 | |
| 500 | error = filldir_reg_func(&fdr, |
| 501 | fbe->fbe_name, fbe->fbe_length, |
| 502 | fbe->fbe_offset, |
| 503 | &fbe->fbe_inum, fbe->fbe_type); |
| 504 | if (error) { |
| 505 | file->f_pos = fbe->fbe_offset; |
| 506 | error = 0; |
| 507 | goto out; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | file->f_pos = offset; |
| 512 | |
| 513 | out: |
| 514 | kfree(fdb); |
| 515 | |
| 516 | return error; |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * gfs2_readdir - Read directory entries from a directory |
| 521 | * @file: The directory to read from |
| 522 | * @dirent: Buffer for dirents |
| 523 | * @filldir: Function used to do the copying |
| 524 | * |
| 525 | * Returns: errno |
| 526 | */ |
| 527 | |
| 528 | static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir) |
| 529 | { |
| 530 | int error; |
| 531 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 532 | if (strcmp(current->comm, "nfsd") != 0) |
| 533 | error = readdir_reg(file, dirent, filldir); |
| 534 | else |
| 535 | error = readdir_bad(file, dirent, filldir); |
| 536 | |
| 537 | return error; |
| 538 | } |
| 539 | |
Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 540 | static const u32 iflags_to_gfs2[32] = { |
| 541 | [iflag_Sync] = GFS2_DIF_SYNC, |
| 542 | [iflag_Immutable] = GFS2_DIF_IMMUTABLE, |
| 543 | [iflag_Append] = GFS2_DIF_APPENDONLY, |
| 544 | [iflag_NoAtime] = GFS2_DIF_NOATIME, |
| 545 | [iflag_Index] = GFS2_DIF_EXHASH, |
| 546 | [iflag_JournalData] = GFS2_DIF_JDATA, |
| 547 | [iflag_DirectIO] = GFS2_DIF_DIRECTIO, |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 548 | }; |
| 549 | |
Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 550 | static const u32 gfs2_to_iflags[32] = { |
| 551 | [gfs2fl_Sync] = IFLAG_SYNC, |
| 552 | [gfs2fl_Immutable] = IFLAG_IMMUTABLE, |
| 553 | [gfs2fl_AppendOnly] = IFLAG_APPEND, |
| 554 | [gfs2fl_NoAtime] = IFLAG_NOATIME, |
| 555 | [gfs2fl_ExHash] = IFLAG_INDEX, |
| 556 | [gfs2fl_Jdata] = IFLAG_JOURNAL_DATA, |
| 557 | [gfs2fl_Directio] = IFLAG_DIRECTIO, |
Steven Whitehouse | 4bcf709 | 2006-04-25 13:20:27 -0400 | [diff] [blame^] | 558 | [gfs2fl_InheritDirectio] = IFLAG_DIRECTIO, |
| 559 | [gfs2fl_InheritJdata] = IFLAG_JOURNAL_DATA, |
Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 560 | }; |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 561 | |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 562 | static int gfs2_get_flags(struct file *filp, u32 __user *ptr) |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 563 | { |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 564 | struct inode *inode = filp->f_dentry->d_inode; |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 565 | struct gfs2_inode *ip = inode->u.generic_ip; |
| 566 | struct gfs2_holder gh; |
| 567 | int error; |
Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 568 | u32 iflags; |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 569 | |
| 570 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh); |
| 571 | error = gfs2_glock_nq_m_atime(1, &gh); |
| 572 | if (error) |
| 573 | return error; |
Steven Whitehouse | 55eccc6 | 2006-04-04 14:29:30 -0400 | [diff] [blame] | 574 | |
Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 575 | iflags = iflags_cvt(gfs2_to_iflags, ip->i_di.di_flags); |
| 576 | if (put_user(iflags, ptr)) |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 577 | error = -EFAULT; |
| 578 | |
| 579 | gfs2_glock_dq_m(1, &gh); |
| 580 | gfs2_holder_uninit(&gh); |
| 581 | return error; |
| 582 | } |
| 583 | |
| 584 | /* Flags that can be set by user space */ |
| 585 | #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \ |
| 586 | GFS2_DIF_DIRECTIO| \ |
| 587 | GFS2_DIF_IMMUTABLE| \ |
| 588 | GFS2_DIF_APPENDONLY| \ |
| 589 | GFS2_DIF_NOATIME| \ |
| 590 | GFS2_DIF_SYNC| \ |
| 591 | GFS2_DIF_SYSTEM| \ |
| 592 | GFS2_DIF_INHERIT_DIRECTIO| \ |
| 593 | GFS2_DIF_INHERIT_JDATA) |
| 594 | |
| 595 | /** |
| 596 | * gfs2_set_flags - set flags on an inode |
| 597 | * @inode: The inode |
| 598 | * @flags: The flags to set |
| 599 | * @mask: Indicates which flags are valid |
| 600 | * |
| 601 | */ |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 602 | static int do_gfs2_set_flags(struct file *filp, u32 reqflags, u32 mask) |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 603 | { |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 604 | struct inode *inode = filp->f_dentry->d_inode; |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 605 | struct gfs2_inode *ip = inode->u.generic_ip; |
Steven Whitehouse | 55eccc6 | 2006-04-04 14:29:30 -0400 | [diff] [blame] | 606 | struct gfs2_sbd *sdp = ip->i_sbd; |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 607 | struct buffer_head *bh; |
| 608 | struct gfs2_holder gh; |
| 609 | int error; |
Steven Whitehouse | 55eccc6 | 2006-04-04 14:29:30 -0400 | [diff] [blame] | 610 | u32 new_flags, flags; |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 611 | |
| 612 | gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); |
| 613 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); |
| 614 | if (error) |
| 615 | return error; |
| 616 | |
Steven Whitehouse | 55eccc6 | 2006-04-04 14:29:30 -0400 | [diff] [blame] | 617 | flags = ip->i_di.di_flags; |
| 618 | new_flags = (flags & ~mask) | (reqflags & mask); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 619 | if ((new_flags ^ flags) == 0) |
| 620 | goto out; |
| 621 | |
Steven Whitehouse | 4bcf709 | 2006-04-25 13:20:27 -0400 | [diff] [blame^] | 622 | if (S_ISDIR(inode->i_mode)) { |
| 623 | if ((new_flags ^ flags) & GFS2_DIF_JDATA) |
| 624 | new_flags ^= (GFS2_DIF_JDATA|GFS2_DIF_INHERIT_JDATA); |
| 625 | if ((new_flags ^ flags) & GFS2_DIF_DIRECTIO) |
| 626 | new_flags ^= (GFS2_DIF_DIRECTIO|GFS2_DIF_INHERIT_DIRECTIO); |
| 627 | } |
| 628 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 629 | error = -EINVAL; |
| 630 | if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET) |
| 631 | goto out; |
| 632 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 633 | error = -EPERM; |
| 634 | if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE)) |
| 635 | goto out; |
| 636 | if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY)) |
| 637 | goto out; |
| 638 | error = gfs2_repermission(inode, MAY_WRITE, NULL); |
| 639 | if (error) |
| 640 | goto out; |
| 641 | |
Steven Whitehouse | 55eccc6 | 2006-04-04 14:29:30 -0400 | [diff] [blame] | 642 | error = gfs2_trans_begin(sdp, RES_DINODE, 0); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 643 | if (error) |
| 644 | goto out; |
Steven Whitehouse | 55eccc6 | 2006-04-04 14:29:30 -0400 | [diff] [blame] | 645 | error = gfs2_meta_inode_buffer(ip, &bh); |
| 646 | if (error) |
| 647 | goto out_trans_end; |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 648 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
| 649 | ip->i_di.di_flags = new_flags; |
| 650 | gfs2_dinode_out(&ip->i_di, bh->b_data); |
| 651 | brelse(bh); |
Steven Whitehouse | 55eccc6 | 2006-04-04 14:29:30 -0400 | [diff] [blame] | 652 | out_trans_end: |
| 653 | gfs2_trans_end(sdp); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 654 | out: |
| 655 | gfs2_glock_dq_uninit(&gh); |
| 656 | return error; |
| 657 | } |
| 658 | |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 659 | static int gfs2_set_flags(struct file *filp, u32 __user *ptr) |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 660 | { |
Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 661 | u32 iflags, gfsflags; |
| 662 | if (get_user(iflags, ptr)) |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 663 | return -EFAULT; |
Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 664 | gfsflags = iflags_cvt(iflags_to_gfs2, iflags); |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 665 | return do_gfs2_set_flags(filp, gfsflags, ~0); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 666 | } |
| 667 | |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 668 | static long gfs2_ioctl(struct file *filp, unsigned int cmd, unsigned long arg) |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 669 | { |
| 670 | switch(cmd) { |
Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 671 | case IFLAGS_GET_IOC: |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 672 | return gfs2_get_flags(filp, (u32 __user *)arg); |
Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 673 | case IFLAGS_SET_IOC: |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 674 | return gfs2_set_flags(filp, (u32 __user *)arg); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 675 | } |
| 676 | return -ENOTTY; |
| 677 | } |
| 678 | |
| 679 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 680 | /** |
| 681 | * gfs2_mmap - |
| 682 | * @file: The file to map |
| 683 | * @vma: The VMA which described the mapping |
| 684 | * |
| 685 | * Returns: 0 or error code |
| 686 | */ |
| 687 | |
| 688 | static int gfs2_mmap(struct file *file, struct vm_area_struct *vma) |
| 689 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 690 | struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 691 | struct gfs2_holder i_gh; |
| 692 | int error; |
| 693 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 694 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh); |
| 695 | error = gfs2_glock_nq_atime(&i_gh); |
| 696 | if (error) { |
| 697 | gfs2_holder_uninit(&i_gh); |
| 698 | return error; |
| 699 | } |
| 700 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 701 | /* This is VM_MAYWRITE instead of VM_WRITE because a call |
| 702 | to mprotect() can turn on VM_WRITE later. */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 703 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 704 | if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) == |
| 705 | (VM_MAYSHARE | VM_MAYWRITE)) |
| 706 | vma->vm_ops = &gfs2_vm_ops_sharewrite; |
| 707 | else |
| 708 | vma->vm_ops = &gfs2_vm_ops_private; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 709 | |
| 710 | gfs2_glock_dq_uninit(&i_gh); |
| 711 | |
| 712 | return error; |
| 713 | } |
| 714 | |
| 715 | /** |
| 716 | * gfs2_open - open a file |
| 717 | * @inode: the inode to open |
| 718 | * @file: the struct file for this opening |
| 719 | * |
| 720 | * Returns: errno |
| 721 | */ |
| 722 | |
| 723 | static int gfs2_open(struct inode *inode, struct file *file) |
| 724 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 725 | struct gfs2_inode *ip = inode->u.generic_ip; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 726 | struct gfs2_holder i_gh; |
| 727 | struct gfs2_file *fp; |
| 728 | int error; |
| 729 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 730 | fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL); |
| 731 | if (!fp) |
| 732 | return -ENOMEM; |
| 733 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 734 | mutex_init(&fp->f_fl_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 735 | |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 736 | gfs2_assert_warn(ip->i_sbd, !file->private_data); |
| 737 | file->private_data = fp; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 738 | |
| 739 | if (S_ISREG(ip->i_di.di_mode)) { |
| 740 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, |
| 741 | &i_gh); |
| 742 | if (error) |
| 743 | goto fail; |
| 744 | |
| 745 | if (!(file->f_flags & O_LARGEFILE) && |
| 746 | ip->i_di.di_size > MAX_NON_LFS) { |
| 747 | error = -EFBIG; |
| 748 | goto fail_gunlock; |
| 749 | } |
| 750 | |
| 751 | /* Listen to the Direct I/O flag */ |
| 752 | |
| 753 | if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO) |
| 754 | file->f_flags |= O_DIRECT; |
| 755 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 756 | gfs2_glock_dq_uninit(&i_gh); |
| 757 | } |
| 758 | |
| 759 | return 0; |
| 760 | |
| 761 | fail_gunlock: |
| 762 | gfs2_glock_dq_uninit(&i_gh); |
| 763 | |
| 764 | fail: |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 765 | file->private_data = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 766 | kfree(fp); |
| 767 | |
| 768 | return error; |
| 769 | } |
| 770 | |
| 771 | /** |
| 772 | * gfs2_close - called to close a struct file |
| 773 | * @inode: the inode the struct file belongs to |
| 774 | * @file: the struct file being closed |
| 775 | * |
| 776 | * Returns: errno |
| 777 | */ |
| 778 | |
| 779 | static int gfs2_close(struct inode *inode, struct file *file) |
| 780 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 781 | struct gfs2_sbd *sdp = inode->i_sb->s_fs_info; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 782 | struct gfs2_file *fp; |
| 783 | |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 784 | fp = file->private_data; |
| 785 | file->private_data = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 786 | |
| 787 | if (gfs2_assert_warn(sdp, fp)) |
| 788 | return -EIO; |
| 789 | |
| 790 | kfree(fp); |
| 791 | |
| 792 | return 0; |
| 793 | } |
| 794 | |
| 795 | /** |
| 796 | * gfs2_fsync - sync the dirty data for a file (across the cluster) |
| 797 | * @file: the file that points to the dentry (we ignore this) |
| 798 | * @dentry: the dentry that points to the inode to sync |
| 799 | * |
| 800 | * Returns: errno |
| 801 | */ |
| 802 | |
| 803 | static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync) |
| 804 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 805 | struct gfs2_inode *ip = dentry->d_inode->u.generic_ip; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 806 | |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 807 | gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 808 | |
| 809 | return 0; |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * gfs2_lock - acquire/release a posix lock on a file |
| 814 | * @file: the file pointer |
| 815 | * @cmd: either modify or retrieve lock state, possibly wait |
| 816 | * @fl: type and range of lock |
| 817 | * |
| 818 | * Returns: errno |
| 819 | */ |
| 820 | |
| 821 | static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl) |
| 822 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 823 | struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 824 | struct gfs2_sbd *sdp = ip->i_sbd; |
| 825 | struct lm_lockname name = |
| 826 | { .ln_number = ip->i_num.no_addr, |
| 827 | .ln_type = LM_TYPE_PLOCK }; |
| 828 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 829 | if (!(fl->fl_flags & FL_POSIX)) |
| 830 | return -ENOLCK; |
| 831 | if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID) |
| 832 | return -ENOLCK; |
| 833 | |
| 834 | if (sdp->sd_args.ar_localflocks) { |
| 835 | if (IS_GETLK(cmd)) { |
Steven Whitehouse | 8628de0 | 2006-03-31 16:48:41 -0500 | [diff] [blame] | 836 | struct file_lock tmp; |
| 837 | int ret; |
| 838 | ret = posix_test_lock(file, fl, &tmp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 839 | fl->fl_type = F_UNLCK; |
Steven Whitehouse | 8628de0 | 2006-03-31 16:48:41 -0500 | [diff] [blame] | 840 | if (ret) |
| 841 | memcpy(fl, &tmp, sizeof(struct file_lock)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 842 | return 0; |
| 843 | } else { |
Steven Whitehouse | 8628de0 | 2006-03-31 16:48:41 -0500 | [diff] [blame] | 844 | return posix_lock_file_wait(file, fl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 845 | } |
| 846 | } |
| 847 | |
| 848 | if (IS_GETLK(cmd)) |
| 849 | return gfs2_lm_plock_get(sdp, &name, file, fl); |
| 850 | else if (fl->fl_type == F_UNLCK) |
| 851 | return gfs2_lm_punlock(sdp, &name, file, fl); |
| 852 | else |
| 853 | return gfs2_lm_plock(sdp, &name, file, cmd, fl); |
| 854 | } |
| 855 | |
| 856 | /** |
| 857 | * gfs2_sendfile - Send bytes to a file or socket |
| 858 | * @in_file: The file to read from |
| 859 | * @out_file: The file to write to |
| 860 | * @count: The amount of data |
| 861 | * @offset: The beginning file offset |
| 862 | * |
| 863 | * Outputs: offset - updated according to number of bytes read |
| 864 | * |
| 865 | * Returns: The number of bytes sent, errno on failure |
| 866 | */ |
| 867 | |
| 868 | static ssize_t gfs2_sendfile(struct file *in_file, loff_t *offset, size_t count, |
| 869 | read_actor_t actor, void *target) |
| 870 | { |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 871 | return generic_file_sendfile(in_file, offset, count, actor, target); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | static int do_flock(struct file *file, int cmd, struct file_lock *fl) |
| 875 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 876 | struct gfs2_file *fp = file->private_data; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 877 | struct gfs2_holder *fl_gh = &fp->f_fl_gh; |
Steven Whitehouse | 55eccc6 | 2006-04-04 14:29:30 -0400 | [diff] [blame] | 878 | struct gfs2_inode *ip = file->f_dentry->d_inode->u.generic_ip; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 879 | struct gfs2_glock *gl; |
| 880 | unsigned int state; |
| 881 | int flags; |
| 882 | int error = 0; |
| 883 | |
| 884 | state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED; |
| 885 | flags = ((IS_SETLKW(cmd)) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE; |
| 886 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 887 | mutex_lock(&fp->f_fl_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 888 | |
| 889 | gl = fl_gh->gh_gl; |
| 890 | if (gl) { |
| 891 | if (fl_gh->gh_state == state) |
| 892 | goto out; |
| 893 | gfs2_glock_hold(gl); |
| 894 | flock_lock_file_wait(file, |
| 895 | &(struct file_lock){.fl_type = F_UNLCK}); |
| 896 | gfs2_glock_dq_uninit(fl_gh); |
| 897 | } else { |
| 898 | error = gfs2_glock_get(ip->i_sbd, |
| 899 | ip->i_num.no_addr, &gfs2_flock_glops, |
| 900 | CREATE, &gl); |
| 901 | if (error) |
| 902 | goto out; |
| 903 | } |
| 904 | |
| 905 | gfs2_holder_init(gl, state, flags, fl_gh); |
| 906 | gfs2_glock_put(gl); |
| 907 | |
| 908 | error = gfs2_glock_nq(fl_gh); |
| 909 | if (error) { |
| 910 | gfs2_holder_uninit(fl_gh); |
| 911 | if (error == GLR_TRYFAILED) |
| 912 | error = -EAGAIN; |
| 913 | } else { |
| 914 | error = flock_lock_file_wait(file, fl); |
| 915 | gfs2_assert_warn(ip->i_sbd, !error); |
| 916 | } |
| 917 | |
| 918 | out: |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 919 | mutex_unlock(&fp->f_fl_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 920 | |
| 921 | return error; |
| 922 | } |
| 923 | |
| 924 | static void do_unflock(struct file *file, struct file_lock *fl) |
| 925 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 926 | struct gfs2_file *fp = file->private_data; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 927 | struct gfs2_holder *fl_gh = &fp->f_fl_gh; |
| 928 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 929 | mutex_lock(&fp->f_fl_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 930 | flock_lock_file_wait(file, fl); |
| 931 | if (fl_gh->gh_gl) |
| 932 | gfs2_glock_dq_uninit(fl_gh); |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 933 | mutex_unlock(&fp->f_fl_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 934 | } |
| 935 | |
| 936 | /** |
| 937 | * gfs2_flock - acquire/release a flock lock on a file |
| 938 | * @file: the file pointer |
| 939 | * @cmd: either modify or retrieve lock state, possibly wait |
| 940 | * @fl: type and range of lock |
| 941 | * |
| 942 | * Returns: errno |
| 943 | */ |
| 944 | |
| 945 | static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl) |
| 946 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 947 | struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 948 | struct gfs2_sbd *sdp = ip->i_sbd; |
| 949 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 950 | if (!(fl->fl_flags & FL_FLOCK)) |
| 951 | return -ENOLCK; |
| 952 | if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID) |
| 953 | return -ENOLCK; |
| 954 | |
| 955 | if (sdp->sd_args.ar_localflocks) |
| 956 | return flock_lock_file_wait(file, fl); |
| 957 | |
| 958 | if (fl->fl_type == F_UNLCK) { |
| 959 | do_unflock(file, fl); |
| 960 | return 0; |
| 961 | } else |
| 962 | return do_flock(file, cmd, fl); |
| 963 | } |
| 964 | |
| 965 | struct file_operations gfs2_file_fops = { |
| 966 | .llseek = gfs2_llseek, |
| 967 | .read = gfs2_read, |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 968 | .readv = gfs2_file_readv, |
| 969 | .aio_read = gfs2_file_aio_read, |
| 970 | .write = generic_file_write, |
| 971 | .writev = generic_file_writev, |
| 972 | .aio_write = generic_file_aio_write, |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 973 | .unlocked_ioctl = gfs2_ioctl, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 974 | .mmap = gfs2_mmap, |
| 975 | .open = gfs2_open, |
| 976 | .release = gfs2_close, |
| 977 | .fsync = gfs2_fsync, |
| 978 | .lock = gfs2_lock, |
| 979 | .sendfile = gfs2_sendfile, |
| 980 | .flock = gfs2_flock, |
Steven Whitehouse | 8628de0 | 2006-03-31 16:48:41 -0500 | [diff] [blame] | 981 | .splice_read = generic_file_splice_read, |
| 982 | .splice_write = generic_file_splice_write, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 983 | }; |
| 984 | |
| 985 | struct file_operations gfs2_dir_fops = { |
| 986 | .readdir = gfs2_readdir, |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 987 | .unlocked_ioctl = gfs2_ioctl, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 988 | .open = gfs2_open, |
| 989 | .release = gfs2_close, |
| 990 | .fsync = gfs2_fsync, |
| 991 | .lock = gfs2_lock, |
| 992 | .flock = gfs2_flock, |
| 993 | }; |
| 994 | |