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