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