]> bbs.cooldavid.org Git - net-next-2.6.git/blame - fs/gfs2/ops_file.c
[GFS2] Update truncate function (shrinking partial blocks)
[net-next-2.6.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>
20#include <linux/gfs2_ioctl.h>
21#include <asm/semaphore.h>
22#include <asm/uaccess.h>
23
24#include "gfs2.h"
25#include "bmap.h"
26#include "dir.h"
27#include "glock.h"
28#include "glops.h"
29#include "inode.h"
30#include "jdata.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 */
41struct 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
49struct 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 */
62struct filldir_reg {
63 struct gfs2_sbd *fdr_sbd;
64 int fdr_prefetch;
65
66 filldir_t fdr_filldir;
67 void *fdr_opaque;
68};
69
70typedef ssize_t(*do_rw_t) (struct file *file,
71 char __user *buf,
72 size_t size, loff_t *offset,
73 unsigned int num_gh, struct gfs2_holder *ghs);
74
75/**
76 * gfs2_llseek - seek to a location in a file
77 * @file: the file
78 * @offset: the offset
79 * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
80 *
81 * SEEK_END requires the glock for the file because it references the
82 * file's size.
83 *
84 * Returns: The new offset, or errno
85 */
86
87static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
88{
89 struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
90 struct gfs2_holder i_gh;
91 loff_t error;
92
93 atomic_inc(&ip->i_sbd->sd_ops_file);
94
95 if (origin == 2) {
96 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
97 &i_gh);
98 if (!error) {
99 error = remote_llseek(file, offset, origin);
100 gfs2_glock_dq_uninit(&i_gh);
101 }
102 } else
103 error = remote_llseek(file, offset, origin);
104
105 return error;
106}
107
108static inline unsigned int vma2state(struct vm_area_struct *vma)
109{
110 if ((vma->vm_flags & (VM_MAYWRITE | VM_MAYSHARE)) ==
111 (VM_MAYWRITE | VM_MAYSHARE))
112 return LM_ST_EXCLUSIVE;
113 return LM_ST_SHARED;
114}
115
116static ssize_t walk_vm_hard(struct file *file, const char __user *buf, size_t size,
117 loff_t *offset, do_rw_t operation)
118{
119 struct gfs2_holder *ghs;
120 unsigned int num_gh = 0;
121 ssize_t count;
122 struct super_block *sb = file->f_dentry->d_inode->i_sb;
123 struct mm_struct *mm = current->mm;
124 struct vm_area_struct *vma;
125 unsigned long start = (unsigned long)buf;
126 unsigned long end = start + size;
127 int dumping = (current->flags & PF_DUMPCORE);
128 unsigned int x = 0;
129
130 for (vma = find_vma(mm, start); vma; vma = vma->vm_next) {
131 if (end <= vma->vm_start)
132 break;
133 if (vma->vm_file &&
134 vma->vm_file->f_dentry->d_inode->i_sb == sb) {
135 num_gh++;
136 }
137 }
138
139 ghs = kcalloc((num_gh + 1), sizeof(struct gfs2_holder), GFP_KERNEL);
140 if (!ghs) {
141 if (!dumping)
142 up_read(&mm->mmap_sem);
143 return -ENOMEM;
144 }
145
146 for (vma = find_vma(mm, start); vma; vma = vma->vm_next) {
147 if (end <= vma->vm_start)
148 break;
149 if (vma->vm_file) {
150 struct inode *inode = vma->vm_file->f_dentry->d_inode;
151 if (inode->i_sb == sb)
152 gfs2_holder_init(get_v2ip(inode)->i_gl,
153 vma2state(vma), 0, &ghs[x++]);
154 }
155 }
156
157 if (!dumping)
158 up_read(&mm->mmap_sem);
159
160 gfs2_assert(get_v2sdp(sb), x == num_gh);
161
162 count = operation(file, buf, size, offset, num_gh, ghs);
163
164 while (num_gh--)
165 gfs2_holder_uninit(&ghs[num_gh]);
166 kfree(ghs);
167
168 return count;
169}
170
171/**
172 * walk_vm - Walk the vmas associated with a buffer for read or write.
173 * If any of them are gfs2, pass the gfs2 inode down to the read/write
174 * worker function so that locks can be acquired in the correct order.
175 * @file: The file to read/write from/to
176 * @buf: The buffer to copy to/from
177 * @size: The amount of data requested
178 * @offset: The current file offset
179 * @operation: The read or write worker function
180 *
181 * Outputs: Offset - updated according to number of bytes written
182 *
183 * Returns: The number of bytes written, errno on failure
184 */
185
186static ssize_t walk_vm(struct file *file, const char __user *buf, size_t size,
187 loff_t *offset, do_rw_t operation)
188{
189 struct gfs2_holder gh;
190
191 if (current->mm) {
192 struct super_block *sb = file->f_dentry->d_inode->i_sb;
193 struct mm_struct *mm = current->mm;
194 struct vm_area_struct *vma;
195 unsigned long start = (unsigned long)buf;
196 unsigned long end = start + size;
197 int dumping = (current->flags & PF_DUMPCORE);
198
199 if (!dumping)
200 down_read(&mm->mmap_sem);
201
202 for (vma = find_vma(mm, start); vma; vma = vma->vm_next) {
203 if (end <= vma->vm_start)
204 break;
205 if (vma->vm_file &&
206 vma->vm_file->f_dentry->d_inode->i_sb == sb)
207 goto do_locks;
208 }
209
210 if (!dumping)
211 up_read(&mm->mmap_sem);
212 }
213
214 return operation(file, buf, size, offset, 0, &gh);
215
216do_locks:
217 return walk_vm_hard(file, buf, size, offset, operation);
218}
219
220static ssize_t do_jdata_read(struct file *file, char __user *buf, size_t size,
221 loff_t *offset)
222{
223 struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
224 ssize_t count = 0;
225
226 if (*offset < 0)
227 return -EINVAL;
228 if (!access_ok(VERIFY_WRITE, buf, size))
229 return -EFAULT;
230
231 if (!(file->f_flags & O_LARGEFILE)) {
232 if (*offset >= MAX_NON_LFS)
233 return -EFBIG;
234 if (*offset + size > MAX_NON_LFS)
235 size = MAX_NON_LFS - *offset;
236 }
237
238 count = gfs2_jdata_read(ip, buf, *offset, size, gfs2_copy2user);
239
240 if (count > 0)
241 *offset += count;
242
243 return count;
244}
245
246/**
247 * do_read_direct - Read bytes from a file
248 * @file: The file to read from
249 * @buf: The buffer to copy into
250 * @size: The amount of data requested
251 * @offset: The current file offset
252 * @num_gh: The number of other locks we need to do the read
253 * @ghs: the locks we need plus one for our lock
254 *
255 * Outputs: Offset - updated according to number of bytes read
256 *
257 * Returns: The number of bytes read, errno on failure
258 */
259
260static ssize_t do_read_direct(struct file *file, char __user *buf, size_t size,
261 loff_t *offset, unsigned int num_gh,
262 struct gfs2_holder *ghs)
263{
264 struct inode *inode = file->f_mapping->host;
265 struct gfs2_inode *ip = get_v2ip(inode);
266 unsigned int state = LM_ST_DEFERRED;
267 int flags = 0;
268 unsigned int x;
269 ssize_t count = 0;
270 int error;
271
272 for (x = 0; x < num_gh; x++)
273 if (ghs[x].gh_gl == ip->i_gl) {
274 state = LM_ST_SHARED;
275 flags |= GL_LOCAL_EXCL;
276 break;
277 }
278
279 gfs2_holder_init(ip->i_gl, state, flags, &ghs[num_gh]);
280
281 error = gfs2_glock_nq_m(num_gh + 1, ghs);
282 if (error)
283 goto out;
284
285 error = -EINVAL;
286 if (gfs2_is_jdata(ip))
287 goto out_gunlock;
288
289 if (gfs2_is_stuffed(ip)) {
290 size_t mask = bdev_hardsect_size(inode->i_sb->s_bdev) - 1;
291
292 if (((*offset) & mask) || (((unsigned long)buf) & mask))
293 goto out_gunlock;
294
295 count = do_jdata_read(file, buf, size & ~mask, offset);
296 } else
297 count = generic_file_read(file, buf, size, offset);
298
299 error = 0;
300
301 out_gunlock:
302 gfs2_glock_dq_m(num_gh + 1, ghs);
303
304 out:
305 gfs2_holder_uninit(&ghs[num_gh]);
306
307 return (count) ? count : error;
308}
309
310/**
311 * do_read_buf - Read bytes from a file
312 * @file: The file to read from
313 * @buf: The buffer to copy into
314 * @size: The amount of data requested
315 * @offset: The current file offset
316 * @num_gh: The number of other locks we need to do the read
317 * @ghs: the locks we need plus one for our lock
318 *
319 * Outputs: Offset - updated according to number of bytes read
320 *
321 * Returns: The number of bytes read, errno on failure
322 */
323
324static ssize_t do_read_buf(struct file *file, char __user *buf, size_t size,
325 loff_t *offset, unsigned int num_gh,
326 struct gfs2_holder *ghs)
327{
328 struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
329 ssize_t count = 0;
330 int error;
331
332 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &ghs[num_gh]);
333
334 error = gfs2_glock_nq_m_atime(num_gh + 1, ghs);
335 if (error)
336 goto out;
337
338 if (gfs2_is_jdata(ip))
339 count = do_jdata_read(file, buf, size, offset);
340 else
341 count = generic_file_read(file, buf, size, offset);
342
343 gfs2_glock_dq_m(num_gh + 1, ghs);
344
345 out:
346 gfs2_holder_uninit(&ghs[num_gh]);
347
348 return (count) ? count : error;
349}
350
351/**
352 * gfs2_read - Read bytes from a file
353 * @file: The file to read from
354 * @buf: The buffer to copy into
355 * @size: The amount of data requested
356 * @offset: The current file offset
357 *
358 * Outputs: Offset - updated according to number of bytes read
359 *
360 * Returns: The number of bytes read, errno on failure
361 */
362
363static ssize_t gfs2_read(struct file *file, char __user *buf, size_t size,
364 loff_t *offset)
365{
366 atomic_inc(&get_v2sdp(file->f_mapping->host->i_sb)->sd_ops_file);
367
368 if (file->f_flags & O_DIRECT)
369 return walk_vm(file, buf, size, offset, do_read_direct);
370 else
371 return walk_vm(file, buf, size, offset, do_read_buf);
372}
373
374/**
375 * grope_mapping - feel up a mapping that needs to be written
376 * @buf: the start of the memory to be written
377 * @size: the size of the memory to be written
378 *
379 * We do this after acquiring the locks on the mapping,
380 * but before starting the write transaction. We need to make
381 * sure that we don't cause recursive transactions if blocks
382 * need to be allocated to the file backing the mapping.
383 *
384 * Returns: errno
385 */
386
387static int grope_mapping(const char __user *buf, size_t size)
388{
389 const char __user *stop = buf + size;
390 char c;
391
392 while (buf < stop) {
393 if (copy_from_user(&c, buf, 1))
394 return -EFAULT;
395 buf += PAGE_CACHE_SIZE;
396 buf = (const char __user *)PAGE_ALIGN((unsigned long)buf);
397 }
398
399 return 0;
400}
401
402/**
403 * do_write_direct_alloc - Write bytes to a file
404 * @file: The file to write to
405 * @buf: The buffer to copy from
406 * @size: The amount of data requested
407 * @offset: The current file offset
408 *
409 * Outputs: Offset - updated according to number of bytes written
410 *
411 * Returns: The number of bytes written, errno on failure
412 */
413
414static ssize_t do_write_direct_alloc(struct file *file, const char __user *buf, size_t size,
415 loff_t *offset)
416{
417 struct inode *inode = file->f_mapping->host;
418 struct gfs2_inode *ip = get_v2ip(inode);
419 struct gfs2_sbd *sdp = ip->i_sbd;
420 struct gfs2_alloc *al = NULL;
421 struct iovec local_iov = { .iov_base = buf, .iov_len = size };
422 struct buffer_head *dibh;
423 unsigned int data_blocks, ind_blocks;
424 ssize_t count;
425 int error;
426
427 gfs2_write_calc_reserv(ip, size, &data_blocks, &ind_blocks);
428
429 al = gfs2_alloc_get(ip);
430
431 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
432 if (error)
433 goto fail;
434
435 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
436 if (error)
437 goto fail_gunlock_q;
438
439 al->al_requested = data_blocks + ind_blocks;
440
441 error = gfs2_inplace_reserve(ip);
442 if (error)
443 goto fail_gunlock_q;
444
445 error = gfs2_trans_begin(sdp,
446 al->al_rgd->rd_ri.ri_length + ind_blocks +
447 RES_DINODE + RES_STATFS + RES_QUOTA, 0);
448 if (error)
449 goto fail_ipres;
450
451 if ((ip->i_di.di_mode & (S_ISUID | S_ISGID)) && !capable(CAP_FSETID)) {
452 error = gfs2_meta_inode_buffer(ip, &dibh);
453 if (error)
454 goto fail_end_trans;
455
456 ip->i_di.di_mode &= (ip->i_di.di_mode & S_IXGRP) ?
457 (~(S_ISUID | S_ISGID)) : (~S_ISUID);
458
d4e9c4c3 459 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
b3b94faa
DT
460 gfs2_dinode_out(&ip->i_di, dibh->b_data);
461 brelse(dibh);
462 }
463
464 if (gfs2_is_stuffed(ip)) {
465 error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_sync, NULL);
466 if (error)
467 goto fail_end_trans;
468 }
469
470 count = generic_file_write_nolock(file, &local_iov, 1, offset);
471 if (count < 0) {
472 error = count;
473 goto fail_end_trans;
474 }
475
476 error = gfs2_meta_inode_buffer(ip, &dibh);
477 if (error)
478 goto fail_end_trans;
479
480 if (ip->i_di.di_size < inode->i_size)
481 ip->i_di.di_size = inode->i_size;
482 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
483
d4e9c4c3 484 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
b3b94faa
DT
485 gfs2_dinode_out(&ip->i_di, dibh->b_data);
486 brelse(dibh);
487
488 gfs2_trans_end(sdp);
489
490 if (file->f_flags & O_SYNC)
491 gfs2_log_flush_glock(ip->i_gl);
492
493 gfs2_inplace_release(ip);
494 gfs2_quota_unlock(ip);
495 gfs2_alloc_put(ip);
496
497 if (file->f_mapping->nrpages) {
498 error = filemap_fdatawrite(file->f_mapping);
499 if (!error)
500 error = filemap_fdatawait(file->f_mapping);
501 }
502 if (error)
503 return error;
504
505 return count;
506
507 fail_end_trans:
508 gfs2_trans_end(sdp);
509
510 fail_ipres:
511 gfs2_inplace_release(ip);
512
513 fail_gunlock_q:
514 gfs2_quota_unlock(ip);
515
516 fail:
517 gfs2_alloc_put(ip);
518
519 return error;
520}
521
522/**
523 * do_write_direct - Write bytes to a file
524 * @file: The file to write to
525 * @buf: The buffer to copy from
526 * @size: The amount of data requested
527 * @offset: The current file offset
528 * @num_gh: The number of other locks we need to do the read
529 * @gh: the locks we need plus one for our lock
530 *
531 * Outputs: Offset - updated according to number of bytes written
532 *
533 * Returns: The number of bytes written, errno on failure
534 */
535
536static ssize_t do_write_direct(struct file *file, const char __user *buf, size_t size,
537 loff_t *offset, unsigned int num_gh,
538 struct gfs2_holder *ghs)
539{
540 struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
541 struct gfs2_sbd *sdp = ip->i_sbd;
542 struct gfs2_file *fp = get_v2fp(file);
543 unsigned int state = LM_ST_DEFERRED;
544 int alloc_required;
545 unsigned int x;
546 size_t s;
547 ssize_t count = 0;
548 int error;
549
550 if (test_bit(GFF_DID_DIRECT_ALLOC, &fp->f_flags))
551 state = LM_ST_EXCLUSIVE;
552 else
553 for (x = 0; x < num_gh; x++)
554 if (ghs[x].gh_gl == ip->i_gl) {
555 state = LM_ST_EXCLUSIVE;
556 break;
557 }
558
559 restart:
560 gfs2_holder_init(ip->i_gl, state, 0, &ghs[num_gh]);
561
562 error = gfs2_glock_nq_m(num_gh + 1, ghs);
563 if (error)
564 goto out;
565
566 error = -EINVAL;
567 if (gfs2_is_jdata(ip))
568 goto out_gunlock;
569
570 if (num_gh) {
571 error = grope_mapping(buf, size);
572 if (error)
573 goto out_gunlock;
574 }
575
576 if (file->f_flags & O_APPEND)
577 *offset = ip->i_di.di_size;
578
579 if (!(file->f_flags & O_LARGEFILE)) {
580 error = -EFBIG;
581 if (*offset >= MAX_NON_LFS)
582 goto out_gunlock;
583 if (*offset + size > MAX_NON_LFS)
584 size = MAX_NON_LFS - *offset;
585 }
586
587 if (gfs2_is_stuffed(ip) ||
588 *offset + size > ip->i_di.di_size ||
589 ((ip->i_di.di_mode & (S_ISUID | S_ISGID)) && !capable(CAP_FSETID)))
590 alloc_required = 1;
591 else {
592 error = gfs2_write_alloc_required(ip, *offset, size,
593 &alloc_required);
594 if (error)
595 goto out_gunlock;
596 }
597
598 if (alloc_required && state != LM_ST_EXCLUSIVE) {
599 gfs2_glock_dq_m(num_gh + 1, ghs);
600 gfs2_holder_uninit(&ghs[num_gh]);
601 state = LM_ST_EXCLUSIVE;
602 goto restart;
603 }
604
605 if (alloc_required) {
606 set_bit(GFF_DID_DIRECT_ALLOC, &fp->f_flags);
607
608 /* split large writes into smaller atomic transactions */
609 while (size) {
610 s = gfs2_tune_get(sdp, gt_max_atomic_write);
611 if (s > size)
612 s = size;
613
614 error = do_write_direct_alloc(file, buf, s, offset);
615 if (error < 0)
616 goto out_gunlock;
617
618 buf += error;
619 size -= error;
620 count += error;
621 }
622 } else {
623 struct iovec local_iov = { .iov_base = buf, .iov_len = size };
624 struct gfs2_holder t_gh;
625
626 clear_bit(GFF_DID_DIRECT_ALLOC, &fp->f_flags);
627
628 error = gfs2_glock_nq_init(sdp->sd_trans_gl, LM_ST_SHARED,
629 GL_NEVER_RECURSE, &t_gh);
630 if (error)
631 goto out_gunlock;
632
633 count = generic_file_write_nolock(file, &local_iov, 1, offset);
634
635 gfs2_glock_dq_uninit(&t_gh);
636 }
637
638 error = 0;
639
640 out_gunlock:
641 gfs2_glock_dq_m(num_gh + 1, ghs);
642
643 out:
644 gfs2_holder_uninit(&ghs[num_gh]);
645
646 return (count) ? count : error;
647}
648
649/**
650 * do_do_write_buf - Write bytes to a file
651 * @file: The file to write to
652 * @buf: The buffer to copy from
653 * @size: The amount of data requested
654 * @offset: The current file offset
655 *
656 * Outputs: Offset - updated according to number of bytes written
657 *
658 * Returns: The number of bytes written, errno on failure
659 */
660
661static ssize_t do_do_write_buf(struct file *file, const char __user *buf, size_t size,
662 loff_t *offset)
663{
664 struct inode *inode = file->f_mapping->host;
665 struct gfs2_inode *ip = get_v2ip(inode);
666 struct gfs2_sbd *sdp = ip->i_sbd;
667 struct gfs2_alloc *al = NULL;
668 struct buffer_head *dibh;
669 unsigned int data_blocks, ind_blocks;
670 int alloc_required, journaled;
671 ssize_t count;
672 int error;
673
674 journaled = gfs2_is_jdata(ip);
675
676 gfs2_write_calc_reserv(ip, size, &data_blocks, &ind_blocks);
677
678 error = gfs2_write_alloc_required(ip, *offset, size, &alloc_required);
679 if (error)
680 return error;
681
682 if (alloc_required) {
683 al = gfs2_alloc_get(ip);
684
685 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
686 if (error)
687 goto fail;
688
689 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
690 if (error)
691 goto fail_gunlock_q;
692
693 al->al_requested = data_blocks + ind_blocks;
694
695 error = gfs2_inplace_reserve(ip);
696 if (error)
697 goto fail_gunlock_q;
698
699 error = gfs2_trans_begin(sdp,
700 al->al_rgd->rd_ri.ri_length +
701 ind_blocks +
702 ((journaled) ? data_blocks : 0) +
703 RES_DINODE + RES_STATFS + RES_QUOTA,
704 0);
705 if (error)
706 goto fail_ipres;
707 } else {
708 error = gfs2_trans_begin(sdp,
709 ((journaled) ? data_blocks : 0) +
710 RES_DINODE,
711 0);
712 if (error)
713 goto fail_ipres;
714 }
715
716 if ((ip->i_di.di_mode & (S_ISUID | S_ISGID)) && !capable(CAP_FSETID)) {
717 error = gfs2_meta_inode_buffer(ip, &dibh);
718 if (error)
719 goto fail_end_trans;
720
721 ip->i_di.di_mode &= (ip->i_di.di_mode & S_IXGRP) ?
722 (~(S_ISUID | S_ISGID)) : (~S_ISUID);
723
d4e9c4c3 724 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
b3b94faa
DT
725 gfs2_dinode_out(&ip->i_di, dibh->b_data);
726 brelse(dibh);
727 }
728
729 if (journaled) {
730 count = gfs2_jdata_write(ip, buf, *offset, size,
731 gfs2_copy_from_user);
732 if (count < 0) {
733 error = count;
734 goto fail_end_trans;
735 }
736
737 *offset += count;
738 } else {
739 struct iovec local_iov = { .iov_base = buf, .iov_len = size };
740
741 count = generic_file_write_nolock(file, &local_iov, 1, offset);
742 if (count < 0) {
743 error = count;
744 goto fail_end_trans;
745 }
746
747 error = gfs2_meta_inode_buffer(ip, &dibh);
748 if (error)
749 goto fail_end_trans;
750
751 if (ip->i_di.di_size < inode->i_size)
752 ip->i_di.di_size = inode->i_size;
753 ip->i_di.di_mtime = ip->i_di.di_ctime = get_seconds();
754
d4e9c4c3 755 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
b3b94faa
DT
756 gfs2_dinode_out(&ip->i_di, dibh->b_data);
757 brelse(dibh);
758 }
759
760 gfs2_trans_end(sdp);
761
762 if (file->f_flags & O_SYNC || IS_SYNC(inode)) {
763 gfs2_log_flush_glock(ip->i_gl);
764 error = filemap_fdatawrite(file->f_mapping);
765 if (error == 0)
766 error = filemap_fdatawait(file->f_mapping);
767 if (error)
768 goto fail_ipres;
769 }
770
771 if (alloc_required) {
772 gfs2_assert_warn(sdp, count != size ||
773 al->al_alloced);
774 gfs2_inplace_release(ip);
775 gfs2_quota_unlock(ip);
776 gfs2_alloc_put(ip);
777 }
778
779 return count;
780
781 fail_end_trans:
782 gfs2_trans_end(sdp);
783
784 fail_ipres:
785 if (alloc_required)
786 gfs2_inplace_release(ip);
787
788 fail_gunlock_q:
789 if (alloc_required)
790 gfs2_quota_unlock(ip);
791
792 fail:
793 if (alloc_required)
794 gfs2_alloc_put(ip);
795
796 return error;
797}
798
799/**
800 * do_write_buf - Write bytes to a file
801 * @file: The file to write to
802 * @buf: The buffer to copy from
803 * @size: The amount of data requested
804 * @offset: The current file offset
805 * @num_gh: The number of other locks we need to do the read
806 * @gh: the locks we need plus one for our lock
807 *
808 * Outputs: Offset - updated according to number of bytes written
809 *
810 * Returns: The number of bytes written, errno on failure
811 */
812
813static ssize_t do_write_buf(struct file *file, const char __user *buf, size_t size,
814 loff_t *offset, unsigned int num_gh,
815 struct gfs2_holder *ghs)
816{
817 struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
818 struct gfs2_sbd *sdp = ip->i_sbd;
819 size_t s;
820 ssize_t count = 0;
821 int error;
822
823 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, 0, &ghs[num_gh]);
824
825 error = gfs2_glock_nq_m(num_gh + 1, ghs);
826 if (error)
827 goto out;
828
829 if (num_gh) {
830 error = grope_mapping(buf, size);
831 if (error)
832 goto out_gunlock;
833 }
834
835 if (file->f_flags & O_APPEND)
836 *offset = ip->i_di.di_size;
837
838 if (!(file->f_flags & O_LARGEFILE)) {
839 error = -EFBIG;
840 if (*offset >= MAX_NON_LFS)
841 goto out_gunlock;
842 if (*offset + size > MAX_NON_LFS)
843 size = MAX_NON_LFS - *offset;
844 }
845
846 /* split large writes into smaller atomic transactions */
847 while (size) {
848 s = gfs2_tune_get(sdp, gt_max_atomic_write);
849 if (s > size)
850 s = size;
851
852 error = do_do_write_buf(file, buf, s, offset);
853 if (error < 0)
854 goto out_gunlock;
855
856 buf += error;
857 size -= error;
858 count += error;
859 }
860
861 error = 0;
862
863 out_gunlock:
864 gfs2_glock_dq_m(num_gh + 1, ghs);
865
866 out:
867 gfs2_holder_uninit(&ghs[num_gh]);
868
869 return (count) ? count : error;
870}
871
872/**
873 * gfs2_write - Write bytes to a file
874 * @file: The file to write to
875 * @buf: The buffer to copy from
876 * @size: The amount of data requested
877 * @offset: The current file offset
878 *
879 * Outputs: Offset - updated according to number of bytes written
880 *
881 * Returns: The number of bytes written, errno on failure
882 */
883
884static ssize_t gfs2_write(struct file *file, const char __user *buf,
885 size_t size, loff_t *offset)
886{
887 struct inode *inode = file->f_mapping->host;
888 ssize_t count;
889
890 atomic_inc(&get_v2sdp(inode->i_sb)->sd_ops_file);
891
892 if (*offset < 0)
893 return -EINVAL;
894 if (!access_ok(VERIFY_READ, buf, size))
895 return -EFAULT;
896
897 mutex_lock(&inode->i_mutex);
898 if (file->f_flags & O_DIRECT)
899 count = walk_vm(file, buf, size, offset,
900 do_write_direct);
901 else
902 count = walk_vm(file, buf, size, offset, do_write_buf);
903 mutex_unlock(&inode->i_mutex);
904
905 return count;
906}
907
908/**
909 * filldir_reg_func - Report a directory entry to the caller of gfs2_dir_read()
910 * @opaque: opaque data used by the function
911 * @name: the name of the directory entry
912 * @length: the length of the name
913 * @offset: the entry's offset in the directory
914 * @inum: the inode number the entry points to
915 * @type: the type of inode the entry points to
916 *
917 * Returns: 0 on success, 1 if buffer full
918 */
919
920static int filldir_reg_func(void *opaque, const char *name, unsigned int length,
921 uint64_t offset, struct gfs2_inum *inum,
922 unsigned int type)
923{
924 struct filldir_reg *fdr = (struct filldir_reg *)opaque;
925 struct gfs2_sbd *sdp = fdr->fdr_sbd;
926 int error;
927
928 error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset,
929 inum->no_formal_ino, type);
930 if (error)
931 return 1;
932
933 if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) {
934 gfs2_glock_prefetch_num(sdp,
935 inum->no_addr, &gfs2_inode_glops,
936 LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
937 gfs2_glock_prefetch_num(sdp,
938 inum->no_addr, &gfs2_iopen_glops,
939 LM_ST_SHARED, LM_FLAG_TRY);
940 }
941
942 return 0;
943}
944
945/**
946 * readdir_reg - Read directory entries from a directory
947 * @file: The directory to read from
948 * @dirent: Buffer for dirents
949 * @filldir: Function used to do the copying
950 *
951 * Returns: errno
952 */
953
954static int readdir_reg(struct file *file, void *dirent, filldir_t filldir)
955{
956 struct gfs2_inode *dip = get_v2ip(file->f_mapping->host);
957 struct filldir_reg fdr;
958 struct gfs2_holder d_gh;
959 uint64_t offset = file->f_pos;
960 int error;
961
962 fdr.fdr_sbd = dip->i_sbd;
963 fdr.fdr_prefetch = 1;
964 fdr.fdr_filldir = filldir;
965 fdr.fdr_opaque = dirent;
966
967 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
968 error = gfs2_glock_nq_atime(&d_gh);
969 if (error) {
970 gfs2_holder_uninit(&d_gh);
971 return error;
972 }
973
974 error = gfs2_dir_read(dip, &offset, &fdr, filldir_reg_func);
975
976 gfs2_glock_dq_uninit(&d_gh);
977
978 file->f_pos = offset;
979
980 return error;
981}
982
983/**
984 * filldir_bad_func - Report a directory entry to the caller of gfs2_dir_read()
985 * @opaque: opaque data used by the function
986 * @name: the name of the directory entry
987 * @length: the length of the name
988 * @offset: the entry's offset in the directory
989 * @inum: the inode number the entry points to
990 * @type: the type of inode the entry points to
991 *
992 * For supporting NFS.
993 *
994 * Returns: 0 on success, 1 if buffer full
995 */
996
997static int filldir_bad_func(void *opaque, const char *name, unsigned int length,
998 uint64_t offset, struct gfs2_inum *inum,
999 unsigned int type)
1000{
1001 struct filldir_bad *fdb = (struct filldir_bad *)opaque;
1002 struct gfs2_sbd *sdp = fdb->fdb_sbd;
1003 struct filldir_bad_entry *fbe;
1004
1005 if (fdb->fdb_entry_off == fdb->fdb_entry_num ||
1006 fdb->fdb_name_off + length > fdb->fdb_name_size)
1007 return 1;
1008
1009 fbe = &fdb->fdb_entry[fdb->fdb_entry_off];
1010 fbe->fbe_name = fdb->fdb_name + fdb->fdb_name_off;
1011 memcpy(fbe->fbe_name, name, length);
1012 fbe->fbe_length = length;
1013 fbe->fbe_offset = offset;
1014 fbe->fbe_inum = *inum;
1015 fbe->fbe_type = type;
1016
1017 fdb->fdb_entry_off++;
1018 fdb->fdb_name_off += length;
1019
1020 if (!(length == 1 && *name == '.')) {
1021 gfs2_glock_prefetch_num(sdp,
1022 inum->no_addr, &gfs2_inode_glops,
1023 LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
1024 gfs2_glock_prefetch_num(sdp,
1025 inum->no_addr, &gfs2_iopen_glops,
1026 LM_ST_SHARED, LM_FLAG_TRY);
1027 }
1028
1029 return 0;
1030}
1031
1032/**
1033 * readdir_bad - Read directory entries from a directory
1034 * @file: The directory to read from
1035 * @dirent: Buffer for dirents
1036 * @filldir: Function used to do the copying
1037 *
1038 * For supporting NFS.
1039 *
1040 * Returns: errno
1041 */
1042
1043static int readdir_bad(struct file *file, void *dirent, filldir_t filldir)
1044{
1045 struct gfs2_inode *dip = get_v2ip(file->f_mapping->host);
1046 struct gfs2_sbd *sdp = dip->i_sbd;
1047 struct filldir_reg fdr;
1048 unsigned int entries, size;
1049 struct filldir_bad *fdb;
1050 struct gfs2_holder d_gh;
1051 uint64_t offset = file->f_pos;
1052 unsigned int x;
1053 struct filldir_bad_entry *fbe;
1054 int error;
1055
1056 entries = gfs2_tune_get(sdp, gt_entries_per_readdir);
1057 size = sizeof(struct filldir_bad) +
1058 entries * (sizeof(struct filldir_bad_entry) + GFS2_FAST_NAME_SIZE);
1059
1060 fdb = kzalloc(size, GFP_KERNEL);
1061 if (!fdb)
1062 return -ENOMEM;
1063
1064 fdb->fdb_sbd = sdp;
1065 fdb->fdb_entry = (struct filldir_bad_entry *)(fdb + 1);
1066 fdb->fdb_entry_num = entries;
1067 fdb->fdb_name = ((char *)fdb) + sizeof(struct filldir_bad) +
1068 entries * sizeof(struct filldir_bad_entry);
1069 fdb->fdb_name_size = entries * GFS2_FAST_NAME_SIZE;
1070
1071 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
1072 error = gfs2_glock_nq_atime(&d_gh);
1073 if (error) {
1074 gfs2_holder_uninit(&d_gh);
1075 goto out;
1076 }
1077
1078 error = gfs2_dir_read(dip, &offset, fdb, filldir_bad_func);
1079
1080 gfs2_glock_dq_uninit(&d_gh);
1081
1082 fdr.fdr_sbd = sdp;
1083 fdr.fdr_prefetch = 0;
1084 fdr.fdr_filldir = filldir;
1085 fdr.fdr_opaque = dirent;
1086
1087 for (x = 0; x < fdb->fdb_entry_off; x++) {
1088 fbe = &fdb->fdb_entry[x];
1089
1090 error = filldir_reg_func(&fdr,
1091 fbe->fbe_name, fbe->fbe_length,
1092 fbe->fbe_offset,
1093 &fbe->fbe_inum, fbe->fbe_type);
1094 if (error) {
1095 file->f_pos = fbe->fbe_offset;
1096 error = 0;
1097 goto out;
1098 }
1099 }
1100
1101 file->f_pos = offset;
1102
1103 out:
1104 kfree(fdb);
1105
1106 return error;
1107}
1108
1109/**
1110 * gfs2_readdir - Read directory entries from a directory
1111 * @file: The directory to read from
1112 * @dirent: Buffer for dirents
1113 * @filldir: Function used to do the copying
1114 *
1115 * Returns: errno
1116 */
1117
1118static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
1119{
1120 int error;
1121
1122 atomic_inc(&get_v2sdp(file->f_mapping->host->i_sb)->sd_ops_file);
1123
1124 if (strcmp(current->comm, "nfsd") != 0)
1125 error = readdir_reg(file, dirent, filldir);
1126 else
1127 error = readdir_bad(file, dirent, filldir);
1128
1129 return error;
1130}
1131
1132static int gfs2_ioctl_flags(struct gfs2_inode *ip, unsigned int cmd, unsigned long arg)
1133{
1134 unsigned int lmode = (cmd == GFS2_IOCTL_SETFLAGS) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
1135 struct buffer_head *dibh;
1136 struct gfs2_holder i_gh;
1137 int error;
1138 __u32 flags = 0, change;
1139
1140 if (cmd == GFS2_IOCTL_SETFLAGS) {
1141 error = get_user(flags, (__u32 __user *)arg);
1142 if (error)
1143 return -EFAULT;
1144 }
1145
1146 error = gfs2_glock_nq_init(ip->i_gl, lmode, 0, &i_gh);
1147 if (error)
1148 return error;
1149
1150 if (cmd == GFS2_IOCTL_SETFLAGS) {
1151 change = flags ^ ip->i_di.di_flags;
1152 error = -EPERM;
1153 if (change & (GFS2_DIF_IMMUTABLE|GFS2_DIF_APPENDONLY)) {
1154 if (!capable(CAP_LINUX_IMMUTABLE))
1155 goto out;
1156 }
1157 error = -EINVAL;
1158 if (flags & (GFS2_DIF_JDATA|GFS2_DIF_DIRECTIO)) {
1159 if (!S_ISREG(ip->i_di.di_mode))
1160 goto out;
1161 /* FIXME: Would be nice not to require the following test */
1162 if ((flags & GFS2_DIF_JDATA) && ip->i_di.di_size)
1163 goto out;
1164 }
1165 if (flags & (GFS2_DIF_INHERIT_JDATA|GFS2_DIF_INHERIT_DIRECTIO)) {
1166 if (!S_ISDIR(ip->i_di.di_mode))
1167 goto out;
1168 }
1169
1170 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0);
1171 if (error)
1172 goto out;
1173
1174 error = gfs2_meta_inode_buffer(ip, &dibh);
1175 if (error)
1176 goto out_trans_end;
1177
1178 ip->i_di.di_flags = flags;
1179
d4e9c4c3 1180 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
b3b94faa
DT
1181 gfs2_dinode_out(&ip->i_di, dibh->b_data);
1182
1183 brelse(dibh);
1184
1185out_trans_end:
1186 gfs2_trans_end(ip->i_sbd);
1187 } else {
1188 flags = ip->i_di.di_flags;
1189 }
1190out:
1191 gfs2_glock_dq_uninit(&i_gh);
1192 if (cmd == GFS2_IOCTL_GETFLAGS) {
1193 if (put_user(flags, (__u32 __user *)arg))
1194 return -EFAULT;
1195 }
1196 return error;
1197}
1198
1199/**
1200 * gfs2_ioctl - do an ioctl on a file
1201 * @inode: the inode
1202 * @file: the file pointer
1203 * @cmd: the ioctl command
1204 * @arg: the argument
1205 *
1206 * Returns: errno
1207 */
1208
1209static int gfs2_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
1210 unsigned long arg)
1211{
1212 struct gfs2_inode *ip = get_v2ip(inode);
1213
1214 atomic_inc(&ip->i_sbd->sd_ops_file);
1215
1216 switch (cmd) {
b3b94faa
DT
1217 case GFS2_IOCTL_SETFLAGS:
1218 case GFS2_IOCTL_GETFLAGS:
1219 return gfs2_ioctl_flags(ip, cmd, arg);
b3b94faa
DT
1220
1221 default:
1222 return -ENOTTY;
1223 }
1224}
1225
1226/**
1227 * gfs2_mmap -
1228 * @file: The file to map
1229 * @vma: The VMA which described the mapping
1230 *
1231 * Returns: 0 or error code
1232 */
1233
1234static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
1235{
1236 struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
1237 struct gfs2_holder i_gh;
1238 int error;
1239
1240 atomic_inc(&ip->i_sbd->sd_ops_file);
1241
1242 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
1243 error = gfs2_glock_nq_atime(&i_gh);
1244 if (error) {
1245 gfs2_holder_uninit(&i_gh);
1246 return error;
1247 }
1248
1249 if (gfs2_is_jdata(ip)) {
1250 if (vma->vm_flags & VM_MAYSHARE)
1251 error = -EOPNOTSUPP;
1252 else
1253 vma->vm_ops = &gfs2_vm_ops_private;
1254 } else {
1255 /* This is VM_MAYWRITE instead of VM_WRITE because a call
1256 to mprotect() can turn on VM_WRITE later. */
1257
1258 if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
1259 (VM_MAYSHARE | VM_MAYWRITE))
1260 vma->vm_ops = &gfs2_vm_ops_sharewrite;
1261 else
1262 vma->vm_ops = &gfs2_vm_ops_private;
1263 }
1264
1265 gfs2_glock_dq_uninit(&i_gh);
1266
1267 return error;
1268}
1269
1270/**
1271 * gfs2_open - open a file
1272 * @inode: the inode to open
1273 * @file: the struct file for this opening
1274 *
1275 * Returns: errno
1276 */
1277
1278static int gfs2_open(struct inode *inode, struct file *file)
1279{
1280 struct gfs2_inode *ip = get_v2ip(inode);
1281 struct gfs2_holder i_gh;
1282 struct gfs2_file *fp;
1283 int error;
1284
1285 atomic_inc(&ip->i_sbd->sd_ops_file);
1286
1287 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
1288 if (!fp)
1289 return -ENOMEM;
1290
1291 init_MUTEX(&fp->f_fl_mutex);
1292
1293 fp->f_inode = ip;
1294 fp->f_vfile = file;
1295
1296 gfs2_assert_warn(ip->i_sbd, !get_v2fp(file));
1297 set_v2fp(file, fp);
1298
1299 if (S_ISREG(ip->i_di.di_mode)) {
1300 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
1301 &i_gh);
1302 if (error)
1303 goto fail;
1304
1305 if (!(file->f_flags & O_LARGEFILE) &&
1306 ip->i_di.di_size > MAX_NON_LFS) {
1307 error = -EFBIG;
1308 goto fail_gunlock;
1309 }
1310
1311 /* Listen to the Direct I/O flag */
1312
1313 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
1314 file->f_flags |= O_DIRECT;
1315
1316 /* Don't let the user open O_DIRECT on a jdata file */
1317
1318 if ((file->f_flags & O_DIRECT) && gfs2_is_jdata(ip)) {
1319 error = -EINVAL;
1320 goto fail_gunlock;
1321 }
1322
1323 gfs2_glock_dq_uninit(&i_gh);
1324 }
1325
1326 return 0;
1327
1328 fail_gunlock:
1329 gfs2_glock_dq_uninit(&i_gh);
1330
1331 fail:
1332 set_v2fp(file, NULL);
1333 kfree(fp);
1334
1335 return error;
1336}
1337
1338/**
1339 * gfs2_close - called to close a struct file
1340 * @inode: the inode the struct file belongs to
1341 * @file: the struct file being closed
1342 *
1343 * Returns: errno
1344 */
1345
1346static int gfs2_close(struct inode *inode, struct file *file)
1347{
1348 struct gfs2_sbd *sdp = get_v2sdp(inode->i_sb);
1349 struct gfs2_file *fp;
1350
1351 atomic_inc(&sdp->sd_ops_file);
1352
1353 fp = get_v2fp(file);
1354 set_v2fp(file, NULL);
1355
1356 if (gfs2_assert_warn(sdp, fp))
1357 return -EIO;
1358
1359 kfree(fp);
1360
1361 return 0;
1362}
1363
1364/**
1365 * gfs2_fsync - sync the dirty data for a file (across the cluster)
1366 * @file: the file that points to the dentry (we ignore this)
1367 * @dentry: the dentry that points to the inode to sync
1368 *
1369 * Returns: errno
1370 */
1371
1372static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
1373{
1374 struct gfs2_inode *ip = get_v2ip(dentry->d_inode);
1375
1376 atomic_inc(&ip->i_sbd->sd_ops_file);
1377 gfs2_log_flush_glock(ip->i_gl);
1378
1379 return 0;
1380}
1381
1382/**
1383 * gfs2_lock - acquire/release a posix lock on a file
1384 * @file: the file pointer
1385 * @cmd: either modify or retrieve lock state, possibly wait
1386 * @fl: type and range of lock
1387 *
1388 * Returns: errno
1389 */
1390
1391static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
1392{
1393 struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
1394 struct gfs2_sbd *sdp = ip->i_sbd;
1395 struct lm_lockname name =
1396 { .ln_number = ip->i_num.no_addr,
1397 .ln_type = LM_TYPE_PLOCK };
1398
1399 atomic_inc(&sdp->sd_ops_file);
1400
1401 if (!(fl->fl_flags & FL_POSIX))
1402 return -ENOLCK;
1403 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
1404 return -ENOLCK;
1405
1406 if (sdp->sd_args.ar_localflocks) {
1407 if (IS_GETLK(cmd)) {
1408 struct file_lock *tmp;
1409 lock_kernel();
1410 tmp = posix_test_lock(file, fl);
1411 fl->fl_type = F_UNLCK;
1412 if (tmp)
1413 memcpy(fl, tmp, sizeof(struct file_lock));
1414 unlock_kernel();
1415 return 0;
1416 } else {
1417 int error;
1418 lock_kernel();
1419 error = posix_lock_file_wait(file, fl);
1420 unlock_kernel();
1421 return error;
1422 }
1423 }
1424
1425 if (IS_GETLK(cmd))
1426 return gfs2_lm_plock_get(sdp, &name, file, fl);
1427 else if (fl->fl_type == F_UNLCK)
1428 return gfs2_lm_punlock(sdp, &name, file, fl);
1429 else
1430 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
1431}
1432
1433/**
1434 * gfs2_sendfile - Send bytes to a file or socket
1435 * @in_file: The file to read from
1436 * @out_file: The file to write to
1437 * @count: The amount of data
1438 * @offset: The beginning file offset
1439 *
1440 * Outputs: offset - updated according to number of bytes read
1441 *
1442 * Returns: The number of bytes sent, errno on failure
1443 */
1444
1445static ssize_t gfs2_sendfile(struct file *in_file, loff_t *offset, size_t count,
1446 read_actor_t actor, void *target)
1447{
1448 struct gfs2_inode *ip = get_v2ip(in_file->f_mapping->host);
1449 struct gfs2_holder gh;
1450 ssize_t retval;
1451
1452 atomic_inc(&ip->i_sbd->sd_ops_file);
1453
1454 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
1455
1456 retval = gfs2_glock_nq_atime(&gh);
1457 if (retval)
1458 goto out;
1459
1460 if (gfs2_is_jdata(ip))
1461 retval = -EOPNOTSUPP;
1462 else
1463 retval = generic_file_sendfile(in_file, offset, count, actor,
1464 target);
1465
1466 gfs2_glock_dq(&gh);
1467
1468 out:
1469 gfs2_holder_uninit(&gh);
1470
1471 return retval;
1472}
1473
1474static int do_flock(struct file *file, int cmd, struct file_lock *fl)
1475{
1476 struct gfs2_file *fp = get_v2fp(file);
1477 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
1478 struct gfs2_inode *ip = fp->f_inode;
1479 struct gfs2_glock *gl;
1480 unsigned int state;
1481 int flags;
1482 int error = 0;
1483
1484 state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
1485 flags = ((IS_SETLKW(cmd)) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
1486
1487 down(&fp->f_fl_mutex);
1488
1489 gl = fl_gh->gh_gl;
1490 if (gl) {
1491 if (fl_gh->gh_state == state)
1492 goto out;
1493 gfs2_glock_hold(gl);
1494 flock_lock_file_wait(file,
1495 &(struct file_lock){.fl_type = F_UNLCK});
1496 gfs2_glock_dq_uninit(fl_gh);
1497 } else {
1498 error = gfs2_glock_get(ip->i_sbd,
1499 ip->i_num.no_addr, &gfs2_flock_glops,
1500 CREATE, &gl);
1501 if (error)
1502 goto out;
1503 }
1504
1505 gfs2_holder_init(gl, state, flags, fl_gh);
1506 gfs2_glock_put(gl);
1507
1508 error = gfs2_glock_nq(fl_gh);
1509 if (error) {
1510 gfs2_holder_uninit(fl_gh);
1511 if (error == GLR_TRYFAILED)
1512 error = -EAGAIN;
1513 } else {
1514 error = flock_lock_file_wait(file, fl);
1515 gfs2_assert_warn(ip->i_sbd, !error);
1516 }
1517
1518 out:
1519 up(&fp->f_fl_mutex);
1520
1521 return error;
1522}
1523
1524static void do_unflock(struct file *file, struct file_lock *fl)
1525{
1526 struct gfs2_file *fp = get_v2fp(file);
1527 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
1528
1529 down(&fp->f_fl_mutex);
1530 flock_lock_file_wait(file, fl);
1531 if (fl_gh->gh_gl)
1532 gfs2_glock_dq_uninit(fl_gh);
1533 up(&fp->f_fl_mutex);
1534}
1535
1536/**
1537 * gfs2_flock - acquire/release a flock lock on a file
1538 * @file: the file pointer
1539 * @cmd: either modify or retrieve lock state, possibly wait
1540 * @fl: type and range of lock
1541 *
1542 * Returns: errno
1543 */
1544
1545static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
1546{
1547 struct gfs2_inode *ip = get_v2ip(file->f_mapping->host);
1548 struct gfs2_sbd *sdp = ip->i_sbd;
1549
1550 atomic_inc(&ip->i_sbd->sd_ops_file);
1551
1552 if (!(fl->fl_flags & FL_FLOCK))
1553 return -ENOLCK;
1554 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
1555 return -ENOLCK;
1556
1557 if (sdp->sd_args.ar_localflocks)
1558 return flock_lock_file_wait(file, fl);
1559
1560 if (fl->fl_type == F_UNLCK) {
1561 do_unflock(file, fl);
1562 return 0;
1563 } else
1564 return do_flock(file, cmd, fl);
1565}
1566
1567struct file_operations gfs2_file_fops = {
1568 .llseek = gfs2_llseek,
1569 .read = gfs2_read,
1570 .write = gfs2_write,
1571 .ioctl = gfs2_ioctl,
1572 .mmap = gfs2_mmap,
1573 .open = gfs2_open,
1574 .release = gfs2_close,
1575 .fsync = gfs2_fsync,
1576 .lock = gfs2_lock,
1577 .sendfile = gfs2_sendfile,
1578 .flock = gfs2_flock,
1579};
1580
1581struct file_operations gfs2_dir_fops = {
1582 .readdir = gfs2_readdir,
1583 .ioctl = gfs2_ioctl,
1584 .open = gfs2_open,
1585 .release = gfs2_close,
1586 .fsync = gfs2_fsync,
1587 .lock = gfs2_lock,
1588 .flock = gfs2_flock,
1589};
1590