David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved. |
Steven Whitehouse | 3a8a9a1 | 2006-05-18 15:09:15 -0400 | [diff] [blame] | 3 | * Copyright (C) 2004-2006 Red Hat, Inc. All rights reserved. |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 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/uaccess.h> |
| 26 | |
| 27 | #include "gfs2.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 28 | #include "lm_interface.h" |
| 29 | #include "incore.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 30 | #include "bmap.h" |
| 31 | #include "dir.h" |
| 32 | #include "glock.h" |
| 33 | #include "glops.h" |
| 34 | #include "inode.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 35 | #include "lm.h" |
| 36 | #include "log.h" |
| 37 | #include "meta_io.h" |
| 38 | #include "ops_file.h" |
| 39 | #include "ops_vm.h" |
| 40 | #include "quota.h" |
| 41 | #include "rgrp.h" |
| 42 | #include "trans.h" |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 43 | #include "util.h" |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 44 | #include "eaops.h" |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 45 | |
| 46 | /* "bad" is for NFS support */ |
| 47 | struct filldir_bad_entry { |
| 48 | char *fbe_name; |
| 49 | unsigned int fbe_length; |
| 50 | uint64_t fbe_offset; |
| 51 | struct gfs2_inum fbe_inum; |
| 52 | unsigned int fbe_type; |
| 53 | }; |
| 54 | |
| 55 | struct filldir_bad { |
| 56 | struct gfs2_sbd *fdb_sbd; |
| 57 | |
| 58 | struct filldir_bad_entry *fdb_entry; |
| 59 | unsigned int fdb_entry_num; |
| 60 | unsigned int fdb_entry_off; |
| 61 | |
| 62 | char *fdb_name; |
| 63 | unsigned int fdb_name_size; |
| 64 | unsigned int fdb_name_off; |
| 65 | }; |
| 66 | |
| 67 | /* For regular, non-NFS */ |
| 68 | struct filldir_reg { |
| 69 | struct gfs2_sbd *fdr_sbd; |
| 70 | int fdr_prefetch; |
| 71 | |
| 72 | filldir_t fdr_filldir; |
| 73 | void *fdr_opaque; |
| 74 | }; |
| 75 | |
Steven Whitehouse | 61a30dc | 2006-02-15 10:15:18 +0000 | [diff] [blame] | 76 | /* |
| 77 | * Most fields left uninitialised to catch anybody who tries to |
| 78 | * use them. f_flags set to prevent file_accessed() from touching |
| 79 | * any other part of this. Its use is purely as a flag so that we |
| 80 | * know (in readpage()) whether or not do to locking. |
| 81 | */ |
| 82 | struct file gfs2_internal_file_sentinal = { |
| 83 | .f_flags = O_NOATIME|O_RDONLY, |
| 84 | }; |
| 85 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 86 | static int gfs2_read_actor(read_descriptor_t *desc, struct page *page, |
| 87 | unsigned long offset, unsigned long size) |
| 88 | { |
| 89 | char *kaddr; |
| 90 | unsigned long count = desc->count; |
| 91 | |
| 92 | if (size > count) |
| 93 | size = count; |
| 94 | |
| 95 | kaddr = kmap(page); |
| 96 | memcpy(desc->arg.buf, kaddr + offset, size); |
| 97 | kunmap(page); |
| 98 | |
| 99 | desc->count = count - size; |
| 100 | desc->written += size; |
| 101 | desc->arg.buf += size; |
| 102 | return size; |
| 103 | } |
| 104 | |
| 105 | int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state, |
| 106 | char *buf, loff_t *pos, unsigned size) |
| 107 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 108 | struct inode *inode = &ip->i_inode; |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 109 | read_descriptor_t desc; |
| 110 | desc.written = 0; |
| 111 | desc.arg.buf = buf; |
| 112 | desc.count = size; |
| 113 | desc.error = 0; |
Steven Whitehouse | 61a30dc | 2006-02-15 10:15:18 +0000 | [diff] [blame] | 114 | do_generic_mapping_read(inode->i_mapping, ra_state, |
| 115 | &gfs2_internal_file_sentinal, pos, &desc, |
| 116 | gfs2_read_actor); |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 117 | return desc.written ? desc.written : desc.error; |
| 118 | } |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 119 | |
| 120 | /** |
| 121 | * gfs2_llseek - seek to a location in a file |
| 122 | * @file: the file |
| 123 | * @offset: the offset |
| 124 | * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END) |
| 125 | * |
| 126 | * SEEK_END requires the glock for the file because it references the |
| 127 | * file's size. |
| 128 | * |
| 129 | * Returns: The new offset, or errno |
| 130 | */ |
| 131 | |
| 132 | static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin) |
| 133 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 134 | struct gfs2_inode *ip = GFS2_I(file->f_mapping->host); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 135 | struct gfs2_holder i_gh; |
| 136 | loff_t error; |
| 137 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 138 | if (origin == 2) { |
| 139 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, |
| 140 | &i_gh); |
| 141 | if (!error) { |
| 142 | error = remote_llseek(file, offset, origin); |
| 143 | gfs2_glock_dq_uninit(&i_gh); |
| 144 | } |
| 145 | } else |
| 146 | error = remote_llseek(file, offset, origin); |
| 147 | |
| 148 | return error; |
| 149 | } |
| 150 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 151 | /** |
| 152 | * filldir_reg_func - Report a directory entry to the caller of gfs2_dir_read() |
| 153 | * @opaque: opaque data used by the function |
| 154 | * @name: the name of the directory entry |
| 155 | * @length: the length of the name |
| 156 | * @offset: the entry's offset in the directory |
| 157 | * @inum: the inode number the entry points to |
| 158 | * @type: the type of inode the entry points to |
| 159 | * |
| 160 | * Returns: 0 on success, 1 if buffer full |
| 161 | */ |
| 162 | |
| 163 | static int filldir_reg_func(void *opaque, const char *name, unsigned int length, |
| 164 | uint64_t offset, struct gfs2_inum *inum, |
| 165 | unsigned int type) |
| 166 | { |
| 167 | struct filldir_reg *fdr = (struct filldir_reg *)opaque; |
| 168 | struct gfs2_sbd *sdp = fdr->fdr_sbd; |
| 169 | int error; |
| 170 | |
| 171 | error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset, |
Steven Whitehouse | 4340fe6 | 2006-07-11 09:46:33 -0400 | [diff] [blame] | 172 | inum->no_addr, type); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 173 | if (error) |
| 174 | return 1; |
| 175 | |
| 176 | if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) { |
| 177 | gfs2_glock_prefetch_num(sdp, |
| 178 | inum->no_addr, &gfs2_inode_glops, |
| 179 | LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY); |
| 180 | gfs2_glock_prefetch_num(sdp, |
| 181 | inum->no_addr, &gfs2_iopen_glops, |
| 182 | LM_ST_SHARED, LM_FLAG_TRY); |
| 183 | } |
| 184 | |
| 185 | return 0; |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * readdir_reg - Read directory entries from a directory |
| 190 | * @file: The directory to read from |
| 191 | * @dirent: Buffer for dirents |
| 192 | * @filldir: Function used to do the copying |
| 193 | * |
| 194 | * Returns: errno |
| 195 | */ |
| 196 | |
| 197 | static int readdir_reg(struct file *file, void *dirent, filldir_t filldir) |
| 198 | { |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 199 | struct inode *dir = file->f_mapping->host; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 200 | struct gfs2_inode *dip = GFS2_I(dir); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 201 | struct filldir_reg fdr; |
| 202 | struct gfs2_holder d_gh; |
| 203 | uint64_t offset = file->f_pos; |
| 204 | int error; |
| 205 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 206 | fdr.fdr_sbd = GFS2_SB(dir); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 207 | fdr.fdr_prefetch = 1; |
| 208 | fdr.fdr_filldir = filldir; |
| 209 | fdr.fdr_opaque = dirent; |
| 210 | |
| 211 | gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh); |
| 212 | error = gfs2_glock_nq_atime(&d_gh); |
| 213 | if (error) { |
| 214 | gfs2_holder_uninit(&d_gh); |
| 215 | return error; |
| 216 | } |
| 217 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 218 | error = gfs2_dir_read(dir, &offset, &fdr, filldir_reg_func); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 219 | |
| 220 | gfs2_glock_dq_uninit(&d_gh); |
| 221 | |
| 222 | file->f_pos = offset; |
| 223 | |
| 224 | return error; |
| 225 | } |
| 226 | |
| 227 | /** |
| 228 | * filldir_bad_func - Report a directory entry to the caller of gfs2_dir_read() |
| 229 | * @opaque: opaque data used by the function |
| 230 | * @name: the name of the directory entry |
| 231 | * @length: the length of the name |
| 232 | * @offset: the entry's offset in the directory |
| 233 | * @inum: the inode number the entry points to |
| 234 | * @type: the type of inode the entry points to |
| 235 | * |
| 236 | * For supporting NFS. |
| 237 | * |
| 238 | * Returns: 0 on success, 1 if buffer full |
| 239 | */ |
| 240 | |
| 241 | static int filldir_bad_func(void *opaque, const char *name, unsigned int length, |
| 242 | uint64_t offset, struct gfs2_inum *inum, |
| 243 | unsigned int type) |
| 244 | { |
| 245 | struct filldir_bad *fdb = (struct filldir_bad *)opaque; |
| 246 | struct gfs2_sbd *sdp = fdb->fdb_sbd; |
| 247 | struct filldir_bad_entry *fbe; |
| 248 | |
| 249 | if (fdb->fdb_entry_off == fdb->fdb_entry_num || |
| 250 | fdb->fdb_name_off + length > fdb->fdb_name_size) |
| 251 | return 1; |
| 252 | |
| 253 | fbe = &fdb->fdb_entry[fdb->fdb_entry_off]; |
| 254 | fbe->fbe_name = fdb->fdb_name + fdb->fdb_name_off; |
| 255 | memcpy(fbe->fbe_name, name, length); |
| 256 | fbe->fbe_length = length; |
| 257 | fbe->fbe_offset = offset; |
| 258 | fbe->fbe_inum = *inum; |
| 259 | fbe->fbe_type = type; |
| 260 | |
| 261 | fdb->fdb_entry_off++; |
| 262 | fdb->fdb_name_off += length; |
| 263 | |
| 264 | if (!(length == 1 && *name == '.')) { |
| 265 | gfs2_glock_prefetch_num(sdp, |
| 266 | inum->no_addr, &gfs2_inode_glops, |
| 267 | LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY); |
| 268 | gfs2_glock_prefetch_num(sdp, |
| 269 | inum->no_addr, &gfs2_iopen_glops, |
| 270 | LM_ST_SHARED, LM_FLAG_TRY); |
| 271 | } |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
| 276 | /** |
| 277 | * readdir_bad - Read directory entries from a directory |
| 278 | * @file: The directory to read from |
| 279 | * @dirent: Buffer for dirents |
| 280 | * @filldir: Function used to do the copying |
| 281 | * |
| 282 | * For supporting NFS. |
| 283 | * |
| 284 | * Returns: errno |
| 285 | */ |
| 286 | |
| 287 | static int readdir_bad(struct file *file, void *dirent, filldir_t filldir) |
| 288 | { |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 289 | struct inode *dir = file->f_mapping->host; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 290 | struct gfs2_inode *dip = GFS2_I(dir); |
| 291 | struct gfs2_sbd *sdp = GFS2_SB(dir); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 292 | struct filldir_reg fdr; |
| 293 | unsigned int entries, size; |
| 294 | struct filldir_bad *fdb; |
| 295 | struct gfs2_holder d_gh; |
| 296 | uint64_t offset = file->f_pos; |
| 297 | unsigned int x; |
| 298 | struct filldir_bad_entry *fbe; |
| 299 | int error; |
| 300 | |
| 301 | entries = gfs2_tune_get(sdp, gt_entries_per_readdir); |
| 302 | size = sizeof(struct filldir_bad) + |
| 303 | entries * (sizeof(struct filldir_bad_entry) + GFS2_FAST_NAME_SIZE); |
| 304 | |
| 305 | fdb = kzalloc(size, GFP_KERNEL); |
| 306 | if (!fdb) |
| 307 | return -ENOMEM; |
| 308 | |
| 309 | fdb->fdb_sbd = sdp; |
| 310 | fdb->fdb_entry = (struct filldir_bad_entry *)(fdb + 1); |
| 311 | fdb->fdb_entry_num = entries; |
| 312 | fdb->fdb_name = ((char *)fdb) + sizeof(struct filldir_bad) + |
| 313 | entries * sizeof(struct filldir_bad_entry); |
| 314 | fdb->fdb_name_size = entries * GFS2_FAST_NAME_SIZE; |
| 315 | |
| 316 | gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh); |
| 317 | error = gfs2_glock_nq_atime(&d_gh); |
| 318 | if (error) { |
| 319 | gfs2_holder_uninit(&d_gh); |
| 320 | goto out; |
| 321 | } |
| 322 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 323 | error = gfs2_dir_read(dir, &offset, fdb, filldir_bad_func); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 324 | |
| 325 | gfs2_glock_dq_uninit(&d_gh); |
| 326 | |
| 327 | fdr.fdr_sbd = sdp; |
| 328 | fdr.fdr_prefetch = 0; |
| 329 | fdr.fdr_filldir = filldir; |
| 330 | fdr.fdr_opaque = dirent; |
| 331 | |
| 332 | for (x = 0; x < fdb->fdb_entry_off; x++) { |
| 333 | fbe = &fdb->fdb_entry[x]; |
| 334 | |
| 335 | error = filldir_reg_func(&fdr, |
| 336 | fbe->fbe_name, fbe->fbe_length, |
| 337 | fbe->fbe_offset, |
| 338 | &fbe->fbe_inum, fbe->fbe_type); |
| 339 | if (error) { |
| 340 | file->f_pos = fbe->fbe_offset; |
| 341 | error = 0; |
| 342 | goto out; |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | file->f_pos = offset; |
| 347 | |
| 348 | out: |
| 349 | kfree(fdb); |
| 350 | |
| 351 | return error; |
| 352 | } |
| 353 | |
| 354 | /** |
| 355 | * gfs2_readdir - Read directory entries from a directory |
| 356 | * @file: The directory to read from |
| 357 | * @dirent: Buffer for dirents |
| 358 | * @filldir: Function used to do the copying |
| 359 | * |
| 360 | * Returns: errno |
| 361 | */ |
| 362 | |
| 363 | static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir) |
| 364 | { |
| 365 | int error; |
| 366 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 367 | if (strcmp(current->comm, "nfsd") != 0) |
| 368 | error = readdir_reg(file, dirent, filldir); |
| 369 | else |
| 370 | error = readdir_bad(file, dirent, filldir); |
| 371 | |
| 372 | return error; |
| 373 | } |
| 374 | |
Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 375 | static const u32 iflags_to_gfs2[32] = { |
| 376 | [iflag_Sync] = GFS2_DIF_SYNC, |
| 377 | [iflag_Immutable] = GFS2_DIF_IMMUTABLE, |
| 378 | [iflag_Append] = GFS2_DIF_APPENDONLY, |
| 379 | [iflag_NoAtime] = GFS2_DIF_NOATIME, |
| 380 | [iflag_Index] = GFS2_DIF_EXHASH, |
| 381 | [iflag_JournalData] = GFS2_DIF_JDATA, |
| 382 | [iflag_DirectIO] = GFS2_DIF_DIRECTIO, |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 383 | }; |
| 384 | |
Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 385 | static const u32 gfs2_to_iflags[32] = { |
| 386 | [gfs2fl_Sync] = IFLAG_SYNC, |
| 387 | [gfs2fl_Immutable] = IFLAG_IMMUTABLE, |
| 388 | [gfs2fl_AppendOnly] = IFLAG_APPEND, |
| 389 | [gfs2fl_NoAtime] = IFLAG_NOATIME, |
| 390 | [gfs2fl_ExHash] = IFLAG_INDEX, |
| 391 | [gfs2fl_Jdata] = IFLAG_JOURNAL_DATA, |
| 392 | [gfs2fl_Directio] = IFLAG_DIRECTIO, |
Steven Whitehouse | 4bcf709 | 2006-04-25 13:20:27 -0400 | [diff] [blame] | 393 | [gfs2fl_InheritDirectio] = IFLAG_DIRECTIO, |
| 394 | [gfs2fl_InheritJdata] = IFLAG_JOURNAL_DATA, |
Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 395 | }; |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 396 | |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 397 | static int gfs2_get_flags(struct file *filp, u32 __user *ptr) |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 398 | { |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 399 | struct inode *inode = filp->f_dentry->d_inode; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 400 | struct gfs2_inode *ip = GFS2_I(inode); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 401 | struct gfs2_holder gh; |
| 402 | int error; |
Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 403 | u32 iflags; |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 404 | |
| 405 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh); |
| 406 | error = gfs2_glock_nq_m_atime(1, &gh); |
| 407 | if (error) |
| 408 | return error; |
Steven Whitehouse | 55eccc6 | 2006-04-04 14:29:30 -0400 | [diff] [blame] | 409 | |
Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 410 | iflags = iflags_cvt(gfs2_to_iflags, ip->i_di.di_flags); |
| 411 | if (put_user(iflags, ptr)) |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 412 | error = -EFAULT; |
| 413 | |
| 414 | gfs2_glock_dq_m(1, &gh); |
| 415 | gfs2_holder_uninit(&gh); |
| 416 | return error; |
| 417 | } |
| 418 | |
| 419 | /* Flags that can be set by user space */ |
| 420 | #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA| \ |
| 421 | GFS2_DIF_DIRECTIO| \ |
| 422 | GFS2_DIF_IMMUTABLE| \ |
| 423 | GFS2_DIF_APPENDONLY| \ |
| 424 | GFS2_DIF_NOATIME| \ |
| 425 | GFS2_DIF_SYNC| \ |
| 426 | GFS2_DIF_SYSTEM| \ |
| 427 | GFS2_DIF_INHERIT_DIRECTIO| \ |
| 428 | GFS2_DIF_INHERIT_JDATA) |
| 429 | |
| 430 | /** |
| 431 | * gfs2_set_flags - set flags on an inode |
| 432 | * @inode: The inode |
| 433 | * @flags: The flags to set |
| 434 | * @mask: Indicates which flags are valid |
| 435 | * |
| 436 | */ |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 437 | 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] | 438 | { |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 439 | struct inode *inode = filp->f_dentry->d_inode; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 440 | struct gfs2_inode *ip = GFS2_I(inode); |
| 441 | struct gfs2_sbd *sdp = GFS2_SB(inode); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 442 | struct buffer_head *bh; |
| 443 | struct gfs2_holder gh; |
| 444 | int error; |
Steven Whitehouse | 55eccc6 | 2006-04-04 14:29:30 -0400 | [diff] [blame] | 445 | u32 new_flags, flags; |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 446 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 447 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &gh); |
Abhijith Das | 52f341c | 2006-07-21 02:03:21 -0400 | [diff] [blame] | 448 | if (error) |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 449 | return error; |
| 450 | |
Steven Whitehouse | 55eccc6 | 2006-04-04 14:29:30 -0400 | [diff] [blame] | 451 | flags = ip->i_di.di_flags; |
| 452 | new_flags = (flags & ~mask) | (reqflags & mask); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 453 | if ((new_flags ^ flags) == 0) |
| 454 | goto out; |
| 455 | |
Steven Whitehouse | 4bcf709 | 2006-04-25 13:20:27 -0400 | [diff] [blame] | 456 | if (S_ISDIR(inode->i_mode)) { |
| 457 | if ((new_flags ^ flags) & GFS2_DIF_JDATA) |
| 458 | new_flags ^= (GFS2_DIF_JDATA|GFS2_DIF_INHERIT_JDATA); |
| 459 | if ((new_flags ^ flags) & GFS2_DIF_DIRECTIO) |
| 460 | new_flags ^= (GFS2_DIF_DIRECTIO|GFS2_DIF_INHERIT_DIRECTIO); |
| 461 | } |
| 462 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 463 | error = -EINVAL; |
| 464 | if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET) |
| 465 | goto out; |
| 466 | |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 467 | error = -EPERM; |
| 468 | if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE)) |
| 469 | goto out; |
| 470 | if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY)) |
| 471 | goto out; |
Steven Whitehouse | b9cb981 | 2006-05-12 17:07:56 -0400 | [diff] [blame] | 472 | if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) && |
| 473 | !capable(CAP_LINUX_IMMUTABLE)) |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 474 | goto out; |
Steven Whitehouse | b9cb981 | 2006-05-12 17:07:56 -0400 | [diff] [blame] | 475 | if (!IS_IMMUTABLE(inode)) { |
Steven Whitehouse | faf450e | 2006-06-22 10:59:10 -0400 | [diff] [blame] | 476 | error = permission(inode, MAY_WRITE, NULL); |
Steven Whitehouse | b9cb981 | 2006-05-12 17:07:56 -0400 | [diff] [blame] | 477 | if (error) |
| 478 | goto out; |
| 479 | } |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 480 | |
Steven Whitehouse | 55eccc6 | 2006-04-04 14:29:30 -0400 | [diff] [blame] | 481 | error = gfs2_trans_begin(sdp, RES_DINODE, 0); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 482 | if (error) |
| 483 | goto out; |
Steven Whitehouse | 55eccc6 | 2006-04-04 14:29:30 -0400 | [diff] [blame] | 484 | error = gfs2_meta_inode_buffer(ip, &bh); |
| 485 | if (error) |
| 486 | goto out_trans_end; |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 487 | gfs2_trans_add_bh(ip->i_gl, bh, 1); |
| 488 | ip->i_di.di_flags = new_flags; |
| 489 | gfs2_dinode_out(&ip->i_di, bh->b_data); |
| 490 | brelse(bh); |
Steven Whitehouse | 55eccc6 | 2006-04-04 14:29:30 -0400 | [diff] [blame] | 491 | out_trans_end: |
| 492 | gfs2_trans_end(sdp); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 493 | out: |
| 494 | gfs2_glock_dq_uninit(&gh); |
| 495 | return error; |
| 496 | } |
| 497 | |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 498 | static int gfs2_set_flags(struct file *filp, u32 __user *ptr) |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 499 | { |
Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 500 | u32 iflags, gfsflags; |
| 501 | if (get_user(iflags, ptr)) |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 502 | return -EFAULT; |
Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 503 | gfsflags = iflags_cvt(iflags_to_gfs2, iflags); |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 504 | return do_gfs2_set_flags(filp, gfsflags, ~0); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 505 | } |
| 506 | |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 507 | 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] | 508 | { |
| 509 | switch(cmd) { |
Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 510 | case IFLAGS_GET_IOC: |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 511 | return gfs2_get_flags(filp, (u32 __user *)arg); |
Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 512 | case IFLAGS_SET_IOC: |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 513 | return gfs2_set_flags(filp, (u32 __user *)arg); |
Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 514 | } |
| 515 | return -ENOTTY; |
| 516 | } |
| 517 | |
| 518 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 519 | /** |
| 520 | * gfs2_mmap - |
| 521 | * @file: The file to map |
| 522 | * @vma: The VMA which described the mapping |
| 523 | * |
| 524 | * Returns: 0 or error code |
| 525 | */ |
| 526 | |
| 527 | static int gfs2_mmap(struct file *file, struct vm_area_struct *vma) |
| 528 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 529 | struct gfs2_inode *ip = GFS2_I(file->f_mapping->host); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 530 | struct gfs2_holder i_gh; |
| 531 | int error; |
| 532 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 533 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh); |
| 534 | error = gfs2_glock_nq_atime(&i_gh); |
| 535 | if (error) { |
| 536 | gfs2_holder_uninit(&i_gh); |
| 537 | return error; |
| 538 | } |
| 539 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 540 | /* This is VM_MAYWRITE instead of VM_WRITE because a call |
| 541 | to mprotect() can turn on VM_WRITE later. */ |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 542 | |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 543 | if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) == |
| 544 | (VM_MAYSHARE | VM_MAYWRITE)) |
| 545 | vma->vm_ops = &gfs2_vm_ops_sharewrite; |
| 546 | else |
| 547 | vma->vm_ops = &gfs2_vm_ops_private; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 548 | |
| 549 | gfs2_glock_dq_uninit(&i_gh); |
| 550 | |
| 551 | return error; |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * gfs2_open - open a file |
| 556 | * @inode: the inode to open |
| 557 | * @file: the struct file for this opening |
| 558 | * |
| 559 | * Returns: errno |
| 560 | */ |
| 561 | |
| 562 | static int gfs2_open(struct inode *inode, struct file *file) |
| 563 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 564 | struct gfs2_inode *ip = GFS2_I(inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 565 | struct gfs2_holder i_gh; |
| 566 | struct gfs2_file *fp; |
| 567 | int error; |
| 568 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 569 | fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL); |
| 570 | if (!fp) |
| 571 | return -ENOMEM; |
| 572 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 573 | mutex_init(&fp->f_fl_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 574 | |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 575 | gfs2_assert_warn(GFS2_SB(inode), !file->private_data); |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 576 | file->private_data = fp; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 577 | |
| 578 | if (S_ISREG(ip->i_di.di_mode)) { |
| 579 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, |
| 580 | &i_gh); |
| 581 | if (error) |
| 582 | goto fail; |
| 583 | |
| 584 | if (!(file->f_flags & O_LARGEFILE) && |
| 585 | ip->i_di.di_size > MAX_NON_LFS) { |
| 586 | error = -EFBIG; |
| 587 | goto fail_gunlock; |
| 588 | } |
| 589 | |
| 590 | /* Listen to the Direct I/O flag */ |
| 591 | |
| 592 | if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO) |
| 593 | file->f_flags |= O_DIRECT; |
| 594 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 595 | gfs2_glock_dq_uninit(&i_gh); |
| 596 | } |
| 597 | |
| 598 | return 0; |
| 599 | |
Steven Whitehouse | 420b9e5 | 2006-07-31 15:42:17 -0400 | [diff] [blame] | 600 | fail_gunlock: |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 601 | gfs2_glock_dq_uninit(&i_gh); |
Steven Whitehouse | 420b9e5 | 2006-07-31 15:42:17 -0400 | [diff] [blame] | 602 | fail: |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 603 | file->private_data = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 604 | kfree(fp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 605 | return error; |
| 606 | } |
| 607 | |
| 608 | /** |
| 609 | * gfs2_close - called to close a struct file |
| 610 | * @inode: the inode the struct file belongs to |
| 611 | * @file: the struct file being closed |
| 612 | * |
| 613 | * Returns: errno |
| 614 | */ |
| 615 | |
| 616 | static int gfs2_close(struct inode *inode, struct file *file) |
| 617 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 618 | struct gfs2_sbd *sdp = inode->i_sb->s_fs_info; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 619 | struct gfs2_file *fp; |
| 620 | |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 621 | fp = file->private_data; |
| 622 | file->private_data = NULL; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 623 | |
| 624 | if (gfs2_assert_warn(sdp, fp)) |
| 625 | return -EIO; |
| 626 | |
| 627 | kfree(fp); |
| 628 | |
| 629 | return 0; |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * gfs2_fsync - sync the dirty data for a file (across the cluster) |
| 634 | * @file: the file that points to the dentry (we ignore this) |
| 635 | * @dentry: the dentry that points to the inode to sync |
| 636 | * |
| 637 | * Returns: errno |
| 638 | */ |
| 639 | |
| 640 | static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync) |
| 641 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 642 | struct gfs2_inode *ip = GFS2_I(dentry->d_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 643 | |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 644 | gfs2_log_flush(ip->i_gl->gl_sbd, ip->i_gl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 645 | |
| 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | /** |
| 650 | * gfs2_lock - acquire/release a posix lock on a file |
| 651 | * @file: the file pointer |
| 652 | * @cmd: either modify or retrieve lock state, possibly wait |
| 653 | * @fl: type and range of lock |
| 654 | * |
| 655 | * Returns: errno |
| 656 | */ |
| 657 | |
| 658 | static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl) |
| 659 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 660 | struct gfs2_inode *ip = GFS2_I(file->f_mapping->host); |
| 661 | struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 662 | struct lm_lockname name = |
| 663 | { .ln_number = ip->i_num.no_addr, |
| 664 | .ln_type = LM_TYPE_PLOCK }; |
| 665 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 666 | if (!(fl->fl_flags & FL_POSIX)) |
| 667 | return -ENOLCK; |
| 668 | if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID) |
| 669 | return -ENOLCK; |
| 670 | |
| 671 | if (sdp->sd_args.ar_localflocks) { |
| 672 | if (IS_GETLK(cmd)) { |
Steven Whitehouse | 8628de0 | 2006-03-31 16:48:41 -0500 | [diff] [blame] | 673 | struct file_lock tmp; |
| 674 | int ret; |
| 675 | ret = posix_test_lock(file, fl, &tmp); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 676 | fl->fl_type = F_UNLCK; |
Steven Whitehouse | 8628de0 | 2006-03-31 16:48:41 -0500 | [diff] [blame] | 677 | if (ret) |
| 678 | memcpy(fl, &tmp, sizeof(struct file_lock)); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 679 | return 0; |
| 680 | } else { |
Steven Whitehouse | 8628de0 | 2006-03-31 16:48:41 -0500 | [diff] [blame] | 681 | return posix_lock_file_wait(file, fl); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 682 | } |
| 683 | } |
| 684 | |
| 685 | if (IS_GETLK(cmd)) |
| 686 | return gfs2_lm_plock_get(sdp, &name, file, fl); |
| 687 | else if (fl->fl_type == F_UNLCK) |
| 688 | return gfs2_lm_punlock(sdp, &name, file, fl); |
| 689 | else |
| 690 | return gfs2_lm_plock(sdp, &name, file, cmd, fl); |
| 691 | } |
| 692 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 693 | static int do_flock(struct file *file, int cmd, struct file_lock *fl) |
| 694 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 695 | struct gfs2_file *fp = file->private_data; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 696 | struct gfs2_holder *fl_gh = &fp->f_fl_gh; |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 697 | struct gfs2_inode *ip = GFS2_I(file->f_dentry->d_inode); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 698 | struct gfs2_glock *gl; |
| 699 | unsigned int state; |
| 700 | int flags; |
| 701 | int error = 0; |
| 702 | |
| 703 | state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED; |
| 704 | flags = ((IS_SETLKW(cmd)) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE; |
| 705 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 706 | mutex_lock(&fp->f_fl_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 707 | |
| 708 | gl = fl_gh->gh_gl; |
| 709 | if (gl) { |
| 710 | if (fl_gh->gh_state == state) |
| 711 | goto out; |
| 712 | gfs2_glock_hold(gl); |
| 713 | flock_lock_file_wait(file, |
| 714 | &(struct file_lock){.fl_type = F_UNLCK}); |
| 715 | gfs2_glock_dq_uninit(fl_gh); |
| 716 | } else { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 717 | error = gfs2_glock_get(GFS2_SB(&ip->i_inode), |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 718 | ip->i_num.no_addr, &gfs2_flock_glops, |
| 719 | CREATE, &gl); |
| 720 | if (error) |
| 721 | goto out; |
| 722 | } |
| 723 | |
| 724 | gfs2_holder_init(gl, state, flags, fl_gh); |
| 725 | gfs2_glock_put(gl); |
| 726 | |
| 727 | error = gfs2_glock_nq(fl_gh); |
| 728 | if (error) { |
| 729 | gfs2_holder_uninit(fl_gh); |
| 730 | if (error == GLR_TRYFAILED) |
| 731 | error = -EAGAIN; |
| 732 | } else { |
| 733 | error = flock_lock_file_wait(file, fl); |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 734 | gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 735 | } |
| 736 | |
Steven Whitehouse | 420b9e5 | 2006-07-31 15:42:17 -0400 | [diff] [blame] | 737 | out: |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 738 | mutex_unlock(&fp->f_fl_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 739 | return error; |
| 740 | } |
| 741 | |
| 742 | static void do_unflock(struct file *file, struct file_lock *fl) |
| 743 | { |
Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 744 | struct gfs2_file *fp = file->private_data; |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 745 | struct gfs2_holder *fl_gh = &fp->f_fl_gh; |
| 746 | |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 747 | mutex_lock(&fp->f_fl_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 748 | flock_lock_file_wait(file, fl); |
| 749 | if (fl_gh->gh_gl) |
| 750 | gfs2_glock_dq_uninit(fl_gh); |
Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 751 | mutex_unlock(&fp->f_fl_mutex); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 752 | } |
| 753 | |
| 754 | /** |
| 755 | * gfs2_flock - acquire/release a flock lock on a file |
| 756 | * @file: the file pointer |
| 757 | * @cmd: either modify or retrieve lock state, possibly wait |
| 758 | * @fl: type and range of lock |
| 759 | * |
| 760 | * Returns: errno |
| 761 | */ |
| 762 | |
| 763 | static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl) |
| 764 | { |
Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 765 | struct gfs2_inode *ip = GFS2_I(file->f_mapping->host); |
| 766 | struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host); |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 767 | |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 768 | if (!(fl->fl_flags & FL_FLOCK)) |
| 769 | return -ENOLCK; |
| 770 | if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID) |
| 771 | return -ENOLCK; |
| 772 | |
| 773 | if (sdp->sd_args.ar_localflocks) |
| 774 | return flock_lock_file_wait(file, fl); |
| 775 | |
| 776 | if (fl->fl_type == F_UNLCK) { |
| 777 | do_unflock(file, fl); |
| 778 | return 0; |
| 779 | } else |
| 780 | return do_flock(file, cmd, fl); |
| 781 | } |
| 782 | |
Steven Whitehouse | b0dd930 | 2006-07-03 13:47:02 -0400 | [diff] [blame] | 783 | const struct file_operations gfs2_file_fops = { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 784 | .llseek = gfs2_llseek, |
Steven Whitehouse | a9e5f4d | 2006-07-25 17:24:12 -0400 | [diff] [blame] | 785 | .read = generic_file_read, |
| 786 | .readv = generic_file_readv, |
| 787 | .aio_read = generic_file_aio_read, |
Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 788 | .write = generic_file_write, |
| 789 | .writev = generic_file_writev, |
| 790 | .aio_write = generic_file_aio_write, |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 791 | .unlocked_ioctl = gfs2_ioctl, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 792 | .mmap = gfs2_mmap, |
| 793 | .open = gfs2_open, |
| 794 | .release = gfs2_close, |
| 795 | .fsync = gfs2_fsync, |
| 796 | .lock = gfs2_lock, |
Steven Whitehouse | a53311d | 2006-06-23 16:16:29 -0400 | [diff] [blame] | 797 | .sendfile = generic_file_sendfile, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 798 | .flock = gfs2_flock, |
Steven Whitehouse | 8628de0 | 2006-03-31 16:48:41 -0500 | [diff] [blame] | 799 | .splice_read = generic_file_splice_read, |
| 800 | .splice_write = generic_file_splice_write, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 801 | }; |
| 802 | |
Steven Whitehouse | b0dd930 | 2006-07-03 13:47:02 -0400 | [diff] [blame] | 803 | const struct file_operations gfs2_dir_fops = { |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 804 | .readdir = gfs2_readdir, |
Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 805 | .unlocked_ioctl = gfs2_ioctl, |
David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 806 | .open = gfs2_open, |
| 807 | .release = gfs2_close, |
| 808 | .fsync = gfs2_fsync, |
| 809 | .lock = gfs2_lock, |
| 810 | .flock = gfs2_flock, |
| 811 | }; |
| 812 | |