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