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