| 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 | 
| Steven Whitehouse | e9fc2aa | 2006-09-01 11:05:15 -0400 | [diff] [blame] | 7 |  * of the GNU General Public License version 2. | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 8 |  */ | 
 | 9 |  | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 10 | #include <linux/slab.h> | 
 | 11 | #include <linux/spinlock.h> | 
 | 12 | #include <linux/completion.h> | 
 | 13 | #include <linux/buffer_head.h> | 
 | 14 | #include <linux/pagemap.h> | 
 | 15 | #include <linux/uio.h> | 
 | 16 | #include <linux/blkdev.h> | 
 | 17 | #include <linux/mm.h> | 
| Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 18 | #include <linux/fs.h> | 
| Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 19 | #include <linux/gfs2_ondisk.h> | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 20 | #include <linux/ext2_fs.h> | 
 | 21 | #include <linux/crc32.h> | 
| Fabio Massimo Di Nitto | 7d30859 | 2006-09-19 07:56:29 +0200 | [diff] [blame] | 22 | #include <linux/lm_interface.h> | 
| Steven Whitehouse | 33c3de3 | 2006-11-30 10:14:32 -0500 | [diff] [blame] | 23 | #include <linux/writeback.h> | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 24 | #include <asm/uaccess.h> | 
 | 25 |  | 
 | 26 | #include "gfs2.h" | 
| Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 27 | #include "incore.h" | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 28 | #include "bmap.h" | 
 | 29 | #include "dir.h" | 
 | 30 | #include "glock.h" | 
 | 31 | #include "glops.h" | 
 | 32 | #include "inode.h" | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 33 | #include "lm.h" | 
 | 34 | #include "log.h" | 
 | 35 | #include "meta_io.h" | 
 | 36 | #include "ops_file.h" | 
 | 37 | #include "ops_vm.h" | 
 | 38 | #include "quota.h" | 
 | 39 | #include "rgrp.h" | 
 | 40 | #include "trans.h" | 
| Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 41 | #include "util.h" | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 42 | #include "eaops.h" | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 43 |  | 
| Steven Whitehouse | 61a30dc | 2006-02-15 10:15:18 +0000 | [diff] [blame] | 44 | /* | 
 | 45 |  * Most fields left uninitialised to catch anybody who tries to | 
 | 46 |  * use them. f_flags set to prevent file_accessed() from touching | 
 | 47 |  * any other part of this. Its use is purely as a flag so that we | 
 | 48 |  * know (in readpage()) whether or not do to locking. | 
 | 49 |  */ | 
| Steven Whitehouse | dd538c8 | 2006-09-04 14:53:30 -0400 | [diff] [blame] | 50 | struct file gfs2_internal_file_sentinel = { | 
| Steven Whitehouse | 61a30dc | 2006-02-15 10:15:18 +0000 | [diff] [blame] | 51 | 	.f_flags = O_NOATIME|O_RDONLY, | 
 | 52 | }; | 
 | 53 |  | 
| Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 54 | static int gfs2_read_actor(read_descriptor_t *desc, struct page *page, | 
 | 55 | 			   unsigned long offset, unsigned long size) | 
 | 56 | { | 
 | 57 | 	char *kaddr; | 
 | 58 | 	unsigned long count = desc->count; | 
 | 59 |  | 
 | 60 | 	if (size > count) | 
 | 61 | 		size = count; | 
 | 62 |  | 
 | 63 | 	kaddr = kmap(page); | 
| Al Viro | 9c9ab3d | 2006-10-13 23:49:23 -0400 | [diff] [blame] | 64 | 	memcpy(desc->arg.data, kaddr + offset, size); | 
| Steven Whitehouse | 26c1a57 | 2006-09-04 15:32:10 -0400 | [diff] [blame] | 65 | 	kunmap(page); | 
| Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 66 |  | 
| Steven Whitehouse | 26c1a57 | 2006-09-04 15:32:10 -0400 | [diff] [blame] | 67 | 	desc->count = count - size; | 
 | 68 | 	desc->written += size; | 
 | 69 | 	desc->arg.buf += size; | 
 | 70 | 	return size; | 
| Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 71 | } | 
 | 72 |  | 
 | 73 | int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state, | 
 | 74 | 		       char *buf, loff_t *pos, unsigned size) | 
 | 75 | { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 76 | 	struct inode *inode = &ip->i_inode; | 
| Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 77 | 	read_descriptor_t desc; | 
 | 78 | 	desc.written = 0; | 
| Al Viro | 9c9ab3d | 2006-10-13 23:49:23 -0400 | [diff] [blame] | 79 | 	desc.arg.data = buf; | 
| Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 80 | 	desc.count = size; | 
 | 81 | 	desc.error = 0; | 
| Steven Whitehouse | 61a30dc | 2006-02-15 10:15:18 +0000 | [diff] [blame] | 82 | 	do_generic_mapping_read(inode->i_mapping, ra_state, | 
| Steven Whitehouse | dd538c8 | 2006-09-04 14:53:30 -0400 | [diff] [blame] | 83 | 				&gfs2_internal_file_sentinel, pos, &desc, | 
| Steven Whitehouse | 61a30dc | 2006-02-15 10:15:18 +0000 | [diff] [blame] | 84 | 				gfs2_read_actor); | 
| Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 85 | 	return desc.written ? desc.written : desc.error; | 
 | 86 | } | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 87 |  | 
 | 88 | /** | 
 | 89 |  * gfs2_llseek - seek to a location in a file | 
 | 90 |  * @file: the file | 
 | 91 |  * @offset: the offset | 
 | 92 |  * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END) | 
 | 93 |  * | 
 | 94 |  * SEEK_END requires the glock for the file because it references the | 
 | 95 |  * file's size. | 
 | 96 |  * | 
 | 97 |  * Returns: The new offset, or errno | 
 | 98 |  */ | 
 | 99 |  | 
 | 100 | static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin) | 
 | 101 | { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 102 | 	struct gfs2_inode *ip = GFS2_I(file->f_mapping->host); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 103 | 	struct gfs2_holder i_gh; | 
 | 104 | 	loff_t error; | 
 | 105 |  | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 106 | 	if (origin == 2) { | 
 | 107 | 		error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, | 
 | 108 | 					   &i_gh); | 
 | 109 | 		if (!error) { | 
 | 110 | 			error = remote_llseek(file, offset, origin); | 
 | 111 | 			gfs2_glock_dq_uninit(&i_gh); | 
 | 112 | 		} | 
 | 113 | 	} else | 
 | 114 | 		error = remote_llseek(file, offset, origin); | 
 | 115 |  | 
 | 116 | 	return error; | 
 | 117 | } | 
 | 118 |  | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 119 | /** | 
| Steven Whitehouse | f0e522a | 2006-09-19 16:41:11 -0400 | [diff] [blame] | 120 |  * gfs2_readdir - Read directory entries from a directory | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 121 |  * @file: The directory to read from | 
 | 122 |  * @dirent: Buffer for dirents | 
 | 123 |  * @filldir: Function used to do the copying | 
 | 124 |  * | 
 | 125 |  * Returns: errno | 
 | 126 |  */ | 
 | 127 |  | 
| Steven Whitehouse | f0e522a | 2006-09-19 16:41:11 -0400 | [diff] [blame] | 128 | static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir) | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 129 | { | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 130 | 	struct inode *dir = file->f_mapping->host; | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 131 | 	struct gfs2_inode *dip = GFS2_I(dir); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 132 | 	struct gfs2_holder d_gh; | 
| Steven Whitehouse | cd91549 | 2006-09-04 12:49:07 -0400 | [diff] [blame] | 133 | 	u64 offset = file->f_pos; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 134 | 	int error; | 
 | 135 |  | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 136 | 	gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh); | 
 | 137 | 	error = gfs2_glock_nq_atime(&d_gh); | 
 | 138 | 	if (error) { | 
 | 139 | 		gfs2_holder_uninit(&d_gh); | 
 | 140 | 		return error; | 
 | 141 | 	} | 
 | 142 |  | 
| Steven Whitehouse | 3699e3a | 2007-01-17 15:09:20 +0000 | [diff] [blame] | 143 | 	error = gfs2_dir_read(dir, &offset, dirent, filldir); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 144 |  | 
 | 145 | 	gfs2_glock_dq_uninit(&d_gh); | 
 | 146 |  | 
 | 147 | 	file->f_pos = offset; | 
 | 148 |  | 
 | 149 | 	return error; | 
 | 150 | } | 
 | 151 |  | 
| Steven Whitehouse | 128e5eb | 2006-10-02 11:24:43 -0400 | [diff] [blame] | 152 | /** | 
 | 153 |  * fsflags_cvt | 
 | 154 |  * @table: A table of 32 u32 flags | 
 | 155 |  * @val: a 32 bit value to convert | 
 | 156 |  * | 
 | 157 |  * This function can be used to convert between fsflags values and | 
 | 158 |  * GFS2's own flags values. | 
 | 159 |  * | 
 | 160 |  * Returns: the converted flags | 
 | 161 |  */ | 
 | 162 | static u32 fsflags_cvt(const u32 *table, u32 val) | 
 | 163 | { | 
 | 164 | 	u32 res = 0; | 
 | 165 | 	while(val) { | 
 | 166 | 		if (val & 1) | 
 | 167 | 			res |= *table; | 
 | 168 | 		table++; | 
 | 169 | 		val >>= 1; | 
 | 170 | 	} | 
 | 171 | 	return res; | 
 | 172 | } | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 173 |  | 
| Steven Whitehouse | 128e5eb | 2006-10-02 11:24:43 -0400 | [diff] [blame] | 174 | static const u32 fsflags_to_gfs2[32] = { | 
 | 175 | 	[3] = GFS2_DIF_SYNC, | 
 | 176 | 	[4] = GFS2_DIF_IMMUTABLE, | 
 | 177 | 	[5] = GFS2_DIF_APPENDONLY, | 
 | 178 | 	[7] = GFS2_DIF_NOATIME, | 
 | 179 | 	[12] = GFS2_DIF_EXHASH, | 
 | 180 | 	[14] = GFS2_DIF_JDATA, | 
 | 181 | 	[20] = GFS2_DIF_DIRECTIO, | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 182 | }; | 
 | 183 |  | 
| Steven Whitehouse | 128e5eb | 2006-10-02 11:24:43 -0400 | [diff] [blame] | 184 | static const u32 gfs2_to_fsflags[32] = { | 
 | 185 | 	[gfs2fl_Sync] = FS_SYNC_FL, | 
 | 186 | 	[gfs2fl_Immutable] = FS_IMMUTABLE_FL, | 
 | 187 | 	[gfs2fl_AppendOnly] = FS_APPEND_FL, | 
 | 188 | 	[gfs2fl_NoAtime] = FS_NOATIME_FL, | 
 | 189 | 	[gfs2fl_ExHash] = FS_INDEX_FL, | 
 | 190 | 	[gfs2fl_Jdata] = FS_JOURNAL_DATA_FL, | 
 | 191 | 	[gfs2fl_Directio] = FS_DIRECTIO_FL, | 
 | 192 | 	[gfs2fl_InheritDirectio] = FS_DIRECTIO_FL, | 
 | 193 | 	[gfs2fl_InheritJdata] = FS_JOURNAL_DATA_FL, | 
| Steven Whitehouse | 7ea9ea8 | 2006-03-31 15:01:28 -0500 | [diff] [blame] | 194 | }; | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 195 |  | 
| Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 196 | static int gfs2_get_flags(struct file *filp, u32 __user *ptr) | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 197 | { | 
| Josef Sipek | 8145409 | 2006-12-08 02:37:03 -0800 | [diff] [blame] | 198 | 	struct inode *inode = filp->f_path.dentry->d_inode; | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 199 | 	struct gfs2_inode *ip = GFS2_I(inode); | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 200 | 	struct gfs2_holder gh; | 
 | 201 | 	int error; | 
| Steven Whitehouse | 128e5eb | 2006-10-02 11:24:43 -0400 | [diff] [blame] | 202 | 	u32 fsflags; | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 203 |  | 
 | 204 | 	gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh); | 
| Steven Whitehouse | dcd2479 | 2006-11-16 11:08:16 -0500 | [diff] [blame] | 205 | 	error = gfs2_glock_nq_atime(&gh); | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 206 | 	if (error) | 
 | 207 | 		return error; | 
| Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 208 |  | 
| Steven Whitehouse | 128e5eb | 2006-10-02 11:24:43 -0400 | [diff] [blame] | 209 | 	fsflags = fsflags_cvt(gfs2_to_fsflags, ip->i_di.di_flags); | 
 | 210 | 	if (put_user(fsflags, ptr)) | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 211 | 		error = -EFAULT; | 
 | 212 |  | 
 | 213 | 	gfs2_glock_dq_m(1, &gh); | 
 | 214 | 	gfs2_holder_uninit(&gh); | 
 | 215 | 	return error; | 
 | 216 | } | 
 | 217 |  | 
| Steven Whitehouse | 6b124d8 | 2006-11-08 12:51:06 -0500 | [diff] [blame] | 218 | void gfs2_set_inode_flags(struct inode *inode) | 
 | 219 | { | 
 | 220 | 	struct gfs2_inode *ip = GFS2_I(inode); | 
 | 221 | 	struct gfs2_dinode_host *di = &ip->i_di; | 
 | 222 | 	unsigned int flags = inode->i_flags; | 
 | 223 |  | 
 | 224 | 	flags &= ~(S_SYNC|S_APPEND|S_IMMUTABLE|S_NOATIME|S_DIRSYNC); | 
 | 225 | 	if (di->di_flags & GFS2_DIF_IMMUTABLE) | 
 | 226 | 		flags |= S_IMMUTABLE; | 
 | 227 | 	if (di->di_flags & GFS2_DIF_APPENDONLY) | 
 | 228 | 		flags |= S_APPEND; | 
 | 229 | 	if (di->di_flags & GFS2_DIF_NOATIME) | 
 | 230 | 		flags |= S_NOATIME; | 
 | 231 | 	if (di->di_flags & GFS2_DIF_SYNC) | 
 | 232 | 		flags |= S_SYNC; | 
 | 233 | 	inode->i_flags = flags; | 
 | 234 | } | 
 | 235 |  | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 236 | /* Flags that can be set by user space */ | 
 | 237 | #define GFS2_FLAGS_USER_SET (GFS2_DIF_JDATA|			\ | 
 | 238 | 			     GFS2_DIF_DIRECTIO|			\ | 
 | 239 | 			     GFS2_DIF_IMMUTABLE|		\ | 
 | 240 | 			     GFS2_DIF_APPENDONLY|		\ | 
 | 241 | 			     GFS2_DIF_NOATIME|			\ | 
 | 242 | 			     GFS2_DIF_SYNC|			\ | 
 | 243 | 			     GFS2_DIF_SYSTEM|			\ | 
 | 244 | 			     GFS2_DIF_INHERIT_DIRECTIO|		\ | 
 | 245 | 			     GFS2_DIF_INHERIT_JDATA) | 
 | 246 |  | 
 | 247 | /** | 
 | 248 |  * gfs2_set_flags - set flags on an inode | 
 | 249 |  * @inode: The inode | 
 | 250 |  * @flags: The flags to set | 
 | 251 |  * @mask: Indicates which flags are valid | 
 | 252 |  * | 
 | 253 |  */ | 
| Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 254 | 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] | 255 | { | 
| Josef Sipek | 8145409 | 2006-12-08 02:37:03 -0800 | [diff] [blame] | 256 | 	struct inode *inode = filp->f_path.dentry->d_inode; | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 257 | 	struct gfs2_inode *ip = GFS2_I(inode); | 
 | 258 | 	struct gfs2_sbd *sdp = GFS2_SB(inode); | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 259 | 	struct buffer_head *bh; | 
 | 260 | 	struct gfs2_holder gh; | 
 | 261 | 	int error; | 
| Steven Whitehouse | 55eccc6 | 2006-04-04 14:29:30 -0400 | [diff] [blame] | 262 | 	u32 new_flags, flags; | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 263 |  | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 264 | 	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] | 265 | 	if (error) | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 266 | 		return error; | 
 | 267 |  | 
| Steven Whitehouse | 55eccc6 | 2006-04-04 14:29:30 -0400 | [diff] [blame] | 268 | 	flags = ip->i_di.di_flags; | 
 | 269 | 	new_flags = (flags & ~mask) | (reqflags & mask); | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 270 | 	if ((new_flags ^ flags) == 0) | 
 | 271 | 		goto out; | 
 | 272 |  | 
| Steven Whitehouse | 4bcf709 | 2006-04-25 13:20:27 -0400 | [diff] [blame] | 273 | 	if (S_ISDIR(inode->i_mode)) { | 
 | 274 | 		if ((new_flags ^ flags) & GFS2_DIF_JDATA) | 
 | 275 | 			new_flags ^= (GFS2_DIF_JDATA|GFS2_DIF_INHERIT_JDATA); | 
 | 276 | 		if ((new_flags ^ flags) & GFS2_DIF_DIRECTIO) | 
 | 277 | 			new_flags ^= (GFS2_DIF_DIRECTIO|GFS2_DIF_INHERIT_DIRECTIO); | 
 | 278 | 	} | 
 | 279 |  | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 280 | 	error = -EINVAL; | 
 | 281 | 	if ((new_flags ^ flags) & ~GFS2_FLAGS_USER_SET) | 
 | 282 | 		goto out; | 
 | 283 |  | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 284 | 	error = -EPERM; | 
 | 285 | 	if (IS_IMMUTABLE(inode) && (new_flags & GFS2_DIF_IMMUTABLE)) | 
 | 286 | 		goto out; | 
 | 287 | 	if (IS_APPEND(inode) && (new_flags & GFS2_DIF_APPENDONLY)) | 
 | 288 | 		goto out; | 
| Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 289 | 	if (((new_flags ^ flags) & GFS2_DIF_IMMUTABLE) && | 
| Steven Whitehouse | b9cb981 | 2006-05-12 17:07:56 -0400 | [diff] [blame] | 290 | 	    !capable(CAP_LINUX_IMMUTABLE)) | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 291 | 		goto out; | 
| Steven Whitehouse | b9cb981 | 2006-05-12 17:07:56 -0400 | [diff] [blame] | 292 | 	if (!IS_IMMUTABLE(inode)) { | 
| Steven Whitehouse | faf450e | 2006-06-22 10:59:10 -0400 | [diff] [blame] | 293 | 		error = permission(inode, MAY_WRITE, NULL); | 
| Steven Whitehouse | b9cb981 | 2006-05-12 17:07:56 -0400 | [diff] [blame] | 294 | 		if (error) | 
 | 295 | 			goto out; | 
 | 296 | 	} | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 297 |  | 
| Steven Whitehouse | 55eccc6 | 2006-04-04 14:29:30 -0400 | [diff] [blame] | 298 | 	error = gfs2_trans_begin(sdp, RES_DINODE, 0); | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 299 | 	if (error) | 
 | 300 | 		goto out; | 
| Steven Whitehouse | 55eccc6 | 2006-04-04 14:29:30 -0400 | [diff] [blame] | 301 | 	error = gfs2_meta_inode_buffer(ip, &bh); | 
 | 302 | 	if (error) | 
 | 303 | 		goto out_trans_end; | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 304 | 	gfs2_trans_add_bh(ip->i_gl, bh, 1); | 
 | 305 | 	ip->i_di.di_flags = new_flags; | 
| Steven Whitehouse | 539e5d6 | 2006-10-31 15:07:05 -0500 | [diff] [blame] | 306 | 	gfs2_dinode_out(ip, bh->b_data); | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 307 | 	brelse(bh); | 
| Steven Whitehouse | 6b124d8 | 2006-11-08 12:51:06 -0500 | [diff] [blame] | 308 | 	gfs2_set_inode_flags(inode); | 
| Steven Whitehouse | 55eccc6 | 2006-04-04 14:29:30 -0400 | [diff] [blame] | 309 | out_trans_end: | 
 | 310 | 	gfs2_trans_end(sdp); | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 311 | out: | 
 | 312 | 	gfs2_glock_dq_uninit(&gh); | 
 | 313 | 	return error; | 
 | 314 | } | 
 | 315 |  | 
| Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 316 | static int gfs2_set_flags(struct file *filp, u32 __user *ptr) | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 317 | { | 
| Steven Whitehouse | 128e5eb | 2006-10-02 11:24:43 -0400 | [diff] [blame] | 318 | 	u32 fsflags, gfsflags; | 
 | 319 | 	if (get_user(fsflags, ptr)) | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 320 | 		return -EFAULT; | 
| Steven Whitehouse | 128e5eb | 2006-10-02 11:24:43 -0400 | [diff] [blame] | 321 | 	gfsflags = fsflags_cvt(fsflags_to_gfs2, fsflags); | 
| Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 322 | 	return do_gfs2_set_flags(filp, gfsflags, ~0); | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 323 | } | 
 | 324 |  | 
| Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 325 | 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] | 326 | { | 
 | 327 | 	switch(cmd) { | 
| Steven Whitehouse | 128e5eb | 2006-10-02 11:24:43 -0400 | [diff] [blame] | 328 | 	case FS_IOC_GETFLAGS: | 
| Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 329 | 		return gfs2_get_flags(filp, (u32 __user *)arg); | 
| Steven Whitehouse | 128e5eb | 2006-10-02 11:24:43 -0400 | [diff] [blame] | 330 | 	case FS_IOC_SETFLAGS: | 
| Steven Whitehouse | b09e593 | 2006-04-07 11:17:32 -0400 | [diff] [blame] | 331 | 		return gfs2_set_flags(filp, (u32 __user *)arg); | 
| Steven Whitehouse | 71b86f5 | 2006-03-28 14:14:04 -0500 | [diff] [blame] | 332 | 	} | 
 | 333 | 	return -ENOTTY; | 
 | 334 | } | 
 | 335 |  | 
 | 336 |  | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 337 | /** | 
 | 338 |  * gfs2_mmap - | 
 | 339 |  * @file: The file to map | 
 | 340 |  * @vma: The VMA which described the mapping | 
 | 341 |  * | 
 | 342 |  * Returns: 0 or error code | 
 | 343 |  */ | 
 | 344 |  | 
 | 345 | static int gfs2_mmap(struct file *file, struct vm_area_struct *vma) | 
 | 346 | { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 347 | 	struct gfs2_inode *ip = GFS2_I(file->f_mapping->host); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 348 | 	struct gfs2_holder i_gh; | 
 | 349 | 	int error; | 
 | 350 |  | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 351 | 	gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh); | 
 | 352 | 	error = gfs2_glock_nq_atime(&i_gh); | 
 | 353 | 	if (error) { | 
 | 354 | 		gfs2_holder_uninit(&i_gh); | 
 | 355 | 		return error; | 
 | 356 | 	} | 
 | 357 |  | 
| Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 358 | 	/* This is VM_MAYWRITE instead of VM_WRITE because a call | 
 | 359 | 	   to mprotect() can turn on VM_WRITE later. */ | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 360 |  | 
| Steven Whitehouse | 18ec7d5 | 2006-02-08 11:50:51 +0000 | [diff] [blame] | 361 | 	if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) == | 
 | 362 | 	    (VM_MAYSHARE | VM_MAYWRITE)) | 
 | 363 | 		vma->vm_ops = &gfs2_vm_ops_sharewrite; | 
 | 364 | 	else | 
 | 365 | 		vma->vm_ops = &gfs2_vm_ops_private; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 366 |  | 
 | 367 | 	gfs2_glock_dq_uninit(&i_gh); | 
 | 368 |  | 
 | 369 | 	return error; | 
 | 370 | } | 
 | 371 |  | 
 | 372 | /** | 
 | 373 |  * gfs2_open - open a file | 
 | 374 |  * @inode: the inode to open | 
 | 375 |  * @file: the struct file for this opening | 
 | 376 |  * | 
 | 377 |  * Returns: errno | 
 | 378 |  */ | 
 | 379 |  | 
 | 380 | static int gfs2_open(struct inode *inode, struct file *file) | 
 | 381 | { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 382 | 	struct gfs2_inode *ip = GFS2_I(inode); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 383 | 	struct gfs2_holder i_gh; | 
 | 384 | 	struct gfs2_file *fp; | 
 | 385 | 	int error; | 
 | 386 |  | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 387 | 	fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL); | 
 | 388 | 	if (!fp) | 
 | 389 | 		return -ENOMEM; | 
 | 390 |  | 
| Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 391 | 	mutex_init(&fp->f_fl_mutex); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 392 |  | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 393 | 	gfs2_assert_warn(GFS2_SB(inode), !file->private_data); | 
| Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 394 | 	file->private_data = fp; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 395 |  | 
| Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 396 | 	if (S_ISREG(ip->i_inode.i_mode)) { | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 397 | 		error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, | 
 | 398 | 					   &i_gh); | 
 | 399 | 		if (error) | 
 | 400 | 			goto fail; | 
 | 401 |  | 
 | 402 | 		if (!(file->f_flags & O_LARGEFILE) && | 
 | 403 | 		    ip->i_di.di_size > MAX_NON_LFS) { | 
 | 404 | 			error = -EFBIG; | 
 | 405 | 			goto fail_gunlock; | 
 | 406 | 		} | 
 | 407 |  | 
 | 408 | 		/* Listen to the Direct I/O flag */ | 
 | 409 |  | 
 | 410 | 		if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO) | 
 | 411 | 			file->f_flags |= O_DIRECT; | 
 | 412 |  | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 413 | 		gfs2_glock_dq_uninit(&i_gh); | 
 | 414 | 	} | 
 | 415 |  | 
 | 416 | 	return 0; | 
 | 417 |  | 
| Steven Whitehouse | 420b9e5 | 2006-07-31 15:42:17 -0400 | [diff] [blame] | 418 | fail_gunlock: | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 419 | 	gfs2_glock_dq_uninit(&i_gh); | 
| Steven Whitehouse | 420b9e5 | 2006-07-31 15:42:17 -0400 | [diff] [blame] | 420 | fail: | 
| Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 421 | 	file->private_data = NULL; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 422 | 	kfree(fp); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 423 | 	return error; | 
 | 424 | } | 
 | 425 |  | 
 | 426 | /** | 
 | 427 |  * gfs2_close - called to close a struct file | 
 | 428 |  * @inode: the inode the struct file belongs to | 
 | 429 |  * @file: the struct file being closed | 
 | 430 |  * | 
 | 431 |  * Returns: errno | 
 | 432 |  */ | 
 | 433 |  | 
 | 434 | static int gfs2_close(struct inode *inode, struct file *file) | 
 | 435 | { | 
| Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 436 | 	struct gfs2_sbd *sdp = inode->i_sb->s_fs_info; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 437 | 	struct gfs2_file *fp; | 
 | 438 |  | 
| Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 439 | 	fp = file->private_data; | 
 | 440 | 	file->private_data = NULL; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 441 |  | 
 | 442 | 	if (gfs2_assert_warn(sdp, fp)) | 
 | 443 | 		return -EIO; | 
 | 444 |  | 
 | 445 | 	kfree(fp); | 
 | 446 |  | 
 | 447 | 	return 0; | 
 | 448 | } | 
 | 449 |  | 
 | 450 | /** | 
 | 451 |  * gfs2_fsync - sync the dirty data for a file (across the cluster) | 
 | 452 |  * @file: the file that points to the dentry (we ignore this) | 
 | 453 |  * @dentry: the dentry that points to the inode to sync | 
 | 454 |  * | 
| Steven Whitehouse | 33c3de3 | 2006-11-30 10:14:32 -0500 | [diff] [blame] | 455 |  * The VFS will flush "normal" data for us. We only need to worry | 
 | 456 |  * about metadata here. For journaled data, we just do a log flush | 
 | 457 |  * as we can't avoid it. Otherwise we can just bale out if datasync | 
 | 458 |  * is set. For stuffed inodes we must flush the log in order to | 
 | 459 |  * ensure that all data is on disk. | 
 | 460 |  * | 
| Steven Whitehouse | 34126f9 | 2006-12-07 09:13:14 -0500 | [diff] [blame] | 461 |  * The call to write_inode_now() is there to write back metadata and | 
 | 462 |  * the inode itself. It does also try and write the data, but thats | 
 | 463 |  * (hopefully) a no-op due to the VFS having already called filemap_fdatawrite() | 
 | 464 |  * for us. | 
 | 465 |  * | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 466 |  * Returns: errno | 
 | 467 |  */ | 
 | 468 |  | 
 | 469 | static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync) | 
 | 470 | { | 
| Steven Whitehouse | 33c3de3 | 2006-11-30 10:14:32 -0500 | [diff] [blame] | 471 | 	struct inode *inode = dentry->d_inode; | 
 | 472 | 	int sync_state = inode->i_state & (I_DIRTY_SYNC|I_DIRTY_DATASYNC); | 
 | 473 | 	int ret = 0; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 474 |  | 
| Steven Whitehouse | 33c3de3 | 2006-11-30 10:14:32 -0500 | [diff] [blame] | 475 | 	if (gfs2_is_jdata(GFS2_I(inode))) { | 
 | 476 | 		gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl); | 
 | 477 | 		return 0; | 
 | 478 | 	} | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 479 |  | 
| Steven Whitehouse | 33c3de3 | 2006-11-30 10:14:32 -0500 | [diff] [blame] | 480 | 	if (sync_state != 0) { | 
 | 481 | 		if (!datasync) | 
| Steven Whitehouse | 34126f9 | 2006-12-07 09:13:14 -0500 | [diff] [blame] | 482 | 			ret = write_inode_now(inode, 0); | 
| Steven Whitehouse | 33c3de3 | 2006-11-30 10:14:32 -0500 | [diff] [blame] | 483 |  | 
 | 484 | 		if (gfs2_is_stuffed(GFS2_I(inode))) | 
 | 485 | 			gfs2_log_flush(GFS2_SB(inode), GFS2_I(inode)->i_gl); | 
 | 486 | 	} | 
 | 487 |  | 
 | 488 | 	return ret; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 489 | } | 
 | 490 |  | 
 | 491 | /** | 
 | 492 |  * gfs2_lock - acquire/release a posix lock on a file | 
 | 493 |  * @file: the file pointer | 
 | 494 |  * @cmd: either modify or retrieve lock state, possibly wait | 
 | 495 |  * @fl: type and range of lock | 
 | 496 |  * | 
 | 497 |  * Returns: errno | 
 | 498 |  */ | 
 | 499 |  | 
 | 500 | static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl) | 
 | 501 | { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 502 | 	struct gfs2_inode *ip = GFS2_I(file->f_mapping->host); | 
 | 503 | 	struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 504 | 	struct lm_lockname name = | 
 | 505 | 		{ .ln_number = ip->i_num.no_addr, | 
 | 506 | 		  .ln_type = LM_TYPE_PLOCK }; | 
 | 507 |  | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 508 | 	if (!(fl->fl_flags & FL_POSIX)) | 
 | 509 | 		return -ENOLCK; | 
| Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 510 | 	if ((ip->i_inode.i_mode & (S_ISGID | S_IXGRP)) == S_ISGID) | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 511 | 		return -ENOLCK; | 
 | 512 |  | 
 | 513 | 	if (sdp->sd_args.ar_localflocks) { | 
 | 514 | 		if (IS_GETLK(cmd)) { | 
| Marc Eshel | 9d6a8c5 | 2007-02-21 00:55:18 -0500 | [diff] [blame] | 515 | 			posix_test_lock(file, fl); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 516 | 			return 0; | 
 | 517 | 		} else { | 
| Steven Whitehouse | 8628de0 | 2006-03-31 16:48:41 -0500 | [diff] [blame] | 518 | 			return posix_lock_file_wait(file, fl); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 519 | 		} | 
 | 520 | 	} | 
 | 521 |  | 
| Marc Eshel | 586759f | 2006-11-14 16:37:25 -0500 | [diff] [blame] | 522 | 	if (cmd == F_CANCELLK) { | 
 | 523 | 		/* Hack: */ | 
 | 524 | 		cmd = F_SETLK; | 
 | 525 | 		fl->fl_type = F_UNLCK; | 
 | 526 | 	} | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 527 | 	if (IS_GETLK(cmd)) | 
 | 528 | 		return gfs2_lm_plock_get(sdp, &name, file, fl); | 
 | 529 | 	else if (fl->fl_type == F_UNLCK) | 
 | 530 | 		return gfs2_lm_punlock(sdp, &name, file, fl); | 
 | 531 | 	else | 
 | 532 | 		return gfs2_lm_plock(sdp, &name, file, cmd, fl); | 
 | 533 | } | 
 | 534 |  | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 535 | static int do_flock(struct file *file, int cmd, struct file_lock *fl) | 
 | 536 | { | 
| Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 537 | 	struct gfs2_file *fp = file->private_data; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 538 | 	struct gfs2_holder *fl_gh = &fp->f_fl_gh; | 
| Josef Sipek | 8145409 | 2006-12-08 02:37:03 -0800 | [diff] [blame] | 539 | 	struct gfs2_inode *ip = GFS2_I(file->f_path.dentry->d_inode); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 540 | 	struct gfs2_glock *gl; | 
 | 541 | 	unsigned int state; | 
 | 542 | 	int flags; | 
 | 543 | 	int error = 0; | 
 | 544 |  | 
 | 545 | 	state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED; | 
| Steven Whitehouse | 26c1a57 | 2006-09-04 15:32:10 -0400 | [diff] [blame] | 546 | 	flags = (IS_SETLKW(cmd) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 547 |  | 
| Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 548 | 	mutex_lock(&fp->f_fl_mutex); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 549 |  | 
 | 550 | 	gl = fl_gh->gh_gl; | 
 | 551 | 	if (gl) { | 
 | 552 | 		if (fl_gh->gh_state == state) | 
 | 553 | 			goto out; | 
 | 554 | 		gfs2_glock_hold(gl); | 
 | 555 | 		flock_lock_file_wait(file, | 
| Steven Whitehouse | 907b9bc | 2006-09-25 09:26:04 -0400 | [diff] [blame] | 556 | 				     &(struct file_lock){.fl_type = F_UNLCK}); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 557 | 		gfs2_glock_dq_uninit(fl_gh); | 
 | 558 | 	} else { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 559 | 		error = gfs2_glock_get(GFS2_SB(&ip->i_inode), | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 560 | 				      ip->i_num.no_addr, &gfs2_flock_glops, | 
 | 561 | 				      CREATE, &gl); | 
 | 562 | 		if (error) | 
 | 563 | 			goto out; | 
 | 564 | 	} | 
 | 565 |  | 
 | 566 | 	gfs2_holder_init(gl, state, flags, fl_gh); | 
 | 567 | 	gfs2_glock_put(gl); | 
 | 568 |  | 
 | 569 | 	error = gfs2_glock_nq(fl_gh); | 
 | 570 | 	if (error) { | 
 | 571 | 		gfs2_holder_uninit(fl_gh); | 
 | 572 | 		if (error == GLR_TRYFAILED) | 
 | 573 | 			error = -EAGAIN; | 
 | 574 | 	} else { | 
 | 575 | 		error = flock_lock_file_wait(file, fl); | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 576 | 		gfs2_assert_warn(GFS2_SB(&ip->i_inode), !error); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 577 | 	} | 
 | 578 |  | 
| Steven Whitehouse | 420b9e5 | 2006-07-31 15:42:17 -0400 | [diff] [blame] | 579 | out: | 
| Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 580 | 	mutex_unlock(&fp->f_fl_mutex); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 581 | 	return error; | 
 | 582 | } | 
 | 583 |  | 
 | 584 | static void do_unflock(struct file *file, struct file_lock *fl) | 
 | 585 | { | 
| Steven Whitehouse | 5c676f6 | 2006-02-27 17:23:27 -0500 | [diff] [blame] | 586 | 	struct gfs2_file *fp = file->private_data; | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 587 | 	struct gfs2_holder *fl_gh = &fp->f_fl_gh; | 
 | 588 |  | 
| Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 589 | 	mutex_lock(&fp->f_fl_mutex); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 590 | 	flock_lock_file_wait(file, fl); | 
 | 591 | 	if (fl_gh->gh_gl) | 
 | 592 | 		gfs2_glock_dq_uninit(fl_gh); | 
| Steven Whitehouse | f55ab26 | 2006-02-21 12:51:39 +0000 | [diff] [blame] | 593 | 	mutex_unlock(&fp->f_fl_mutex); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 594 | } | 
 | 595 |  | 
 | 596 | /** | 
 | 597 |  * gfs2_flock - acquire/release a flock lock on a file | 
 | 598 |  * @file: the file pointer | 
 | 599 |  * @cmd: either modify or retrieve lock state, possibly wait | 
 | 600 |  * @fl: type and range of lock | 
 | 601 |  * | 
 | 602 |  * Returns: errno | 
 | 603 |  */ | 
 | 604 |  | 
 | 605 | static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl) | 
 | 606 | { | 
| Steven Whitehouse | feaa7bb | 2006-06-14 15:32:57 -0400 | [diff] [blame] | 607 | 	struct gfs2_inode *ip = GFS2_I(file->f_mapping->host); | 
 | 608 | 	struct gfs2_sbd *sdp = GFS2_SB(file->f_mapping->host); | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 609 |  | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 610 | 	if (!(fl->fl_flags & FL_FLOCK)) | 
 | 611 | 		return -ENOLCK; | 
| Steven Whitehouse | b60623c | 2006-11-01 12:22:46 -0500 | [diff] [blame] | 612 | 	if ((ip->i_inode.i_mode & (S_ISGID | S_IXGRP)) == S_ISGID) | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 613 | 		return -ENOLCK; | 
 | 614 |  | 
 | 615 | 	if (sdp->sd_args.ar_localflocks) | 
 | 616 | 		return flock_lock_file_wait(file, fl); | 
 | 617 |  | 
 | 618 | 	if (fl->fl_type == F_UNLCK) { | 
 | 619 | 		do_unflock(file, fl); | 
 | 620 | 		return 0; | 
| Steven Whitehouse | d00223f | 2006-10-02 10:28:05 -0400 | [diff] [blame] | 621 | 	} else { | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 622 | 		return do_flock(file, cmd, fl); | 
| Steven Whitehouse | d00223f | 2006-10-02 10:28:05 -0400 | [diff] [blame] | 623 | 	} | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 624 | } | 
 | 625 |  | 
| Steven Whitehouse | b0dd930 | 2006-07-03 13:47:02 -0400 | [diff] [blame] | 626 | const struct file_operations gfs2_file_fops = { | 
| Steven Whitehouse | 26c1a57 | 2006-09-04 15:32:10 -0400 | [diff] [blame] | 627 | 	.llseek		= gfs2_llseek, | 
| Steven Whitehouse | d00223f | 2006-10-02 10:28:05 -0400 | [diff] [blame] | 628 | 	.read		= do_sync_read, | 
| Steven Whitehouse | 26c1a57 | 2006-09-04 15:32:10 -0400 | [diff] [blame] | 629 | 	.aio_read	= generic_file_aio_read, | 
| Steven Whitehouse | d00223f | 2006-10-02 10:28:05 -0400 | [diff] [blame] | 630 | 	.write		= do_sync_write, | 
| Steven Whitehouse | 26c1a57 | 2006-09-04 15:32:10 -0400 | [diff] [blame] | 631 | 	.aio_write	= generic_file_aio_write, | 
 | 632 | 	.unlocked_ioctl	= gfs2_ioctl, | 
 | 633 | 	.mmap		= gfs2_mmap, | 
 | 634 | 	.open		= gfs2_open, | 
 | 635 | 	.release	= gfs2_close, | 
 | 636 | 	.fsync		= gfs2_fsync, | 
 | 637 | 	.lock		= gfs2_lock, | 
 | 638 | 	.sendfile	= generic_file_sendfile, | 
 | 639 | 	.flock		= gfs2_flock, | 
 | 640 | 	.splice_read	= generic_file_splice_read, | 
 | 641 | 	.splice_write	= generic_file_splice_write, | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 642 | }; | 
 | 643 |  | 
| Steven Whitehouse | b0dd930 | 2006-07-03 13:47:02 -0400 | [diff] [blame] | 644 | const struct file_operations gfs2_dir_fops = { | 
| Steven Whitehouse | 26c1a57 | 2006-09-04 15:32:10 -0400 | [diff] [blame] | 645 | 	.readdir	= gfs2_readdir, | 
 | 646 | 	.unlocked_ioctl	= gfs2_ioctl, | 
 | 647 | 	.open		= gfs2_open, | 
 | 648 | 	.release	= gfs2_close, | 
 | 649 | 	.fsync		= gfs2_fsync, | 
 | 650 | 	.lock		= gfs2_lock, | 
 | 651 | 	.flock		= gfs2_flock, | 
| David Teigland | b3b94fa | 2006-01-16 16:50:04 +0000 | [diff] [blame] | 652 | }; | 
 | 653 |  |