blob: d30c6db46241dc642c3780d274e60c899088f0c4 [file] [log] [blame]
David Teiglandb3b94fa2006-01-16 16:50:04 +00001/*
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>
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000021#include <linux/fs.h>
Steven Whitehouse5c676f62006-02-27 17:23:27 -050022#include <linux/gfs2_ondisk.h>
David Teiglandb3b94fa2006-01-16 16:50:04 +000023#include <asm/semaphore.h>
24#include <asm/uaccess.h>
25
26#include "gfs2.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050027#include "lm_interface.h"
28#include "incore.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000029#include "bmap.h"
30#include "dir.h"
31#include "glock.h"
32#include "glops.h"
33#include "inode.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000034#include "lm.h"
35#include "log.h"
36#include "meta_io.h"
37#include "ops_file.h"
38#include "ops_vm.h"
39#include "quota.h"
40#include "rgrp.h"
41#include "trans.h"
Steven Whitehouse5c676f62006-02-27 17:23:27 -050042#include "util.h"
David Teiglandb3b94fa2006-01-16 16:50:04 +000043
44/* "bad" is for NFS support */
45struct filldir_bad_entry {
46 char *fbe_name;
47 unsigned int fbe_length;
48 uint64_t fbe_offset;
49 struct gfs2_inum fbe_inum;
50 unsigned int fbe_type;
51};
52
53struct filldir_bad {
54 struct gfs2_sbd *fdb_sbd;
55
56 struct filldir_bad_entry *fdb_entry;
57 unsigned int fdb_entry_num;
58 unsigned int fdb_entry_off;
59
60 char *fdb_name;
61 unsigned int fdb_name_size;
62 unsigned int fdb_name_off;
63};
64
65/* For regular, non-NFS */
66struct filldir_reg {
67 struct gfs2_sbd *fdr_sbd;
68 int fdr_prefetch;
69
70 filldir_t fdr_filldir;
71 void *fdr_opaque;
72};
73
Steven Whitehouse61a30dc2006-02-15 10:15:18 +000074/*
75 * Most fields left uninitialised to catch anybody who tries to
76 * use them. f_flags set to prevent file_accessed() from touching
77 * any other part of this. Its use is purely as a flag so that we
78 * know (in readpage()) whether or not do to locking.
79 */
80struct file gfs2_internal_file_sentinal = {
81 .f_flags = O_NOATIME|O_RDONLY,
82};
83
Steven Whitehouse18ec7d52006-02-08 11:50:51 +000084static int gfs2_read_actor(read_descriptor_t *desc, struct page *page,
85 unsigned long offset, unsigned long size)
86{
87 char *kaddr;
88 unsigned long count = desc->count;
89
90 if (size > count)
91 size = count;
92
93 kaddr = kmap(page);
94 memcpy(desc->arg.buf, kaddr + offset, size);
95 kunmap(page);
96
97 desc->count = count - size;
98 desc->written += size;
99 desc->arg.buf += size;
100 return size;
101}
102
103int gfs2_internal_read(struct gfs2_inode *ip, struct file_ra_state *ra_state,
104 char *buf, loff_t *pos, unsigned size)
105{
106 struct inode *inode = ip->i_vnode;
107 read_descriptor_t desc;
108 desc.written = 0;
109 desc.arg.buf = buf;
110 desc.count = size;
111 desc.error = 0;
Steven Whitehouse61a30dc2006-02-15 10:15:18 +0000112 do_generic_mapping_read(inode->i_mapping, ra_state,
113 &gfs2_internal_file_sentinal, pos, &desc,
114 gfs2_read_actor);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000115 return desc.written ? desc.written : desc.error;
116}
David Teiglandb3b94fa2006-01-16 16:50:04 +0000117
118/**
119 * gfs2_llseek - seek to a location in a file
120 * @file: the file
121 * @offset: the offset
122 * @origin: Where to seek from (SEEK_SET, SEEK_CUR, or SEEK_END)
123 *
124 * SEEK_END requires the glock for the file because it references the
125 * file's size.
126 *
127 * Returns: The new offset, or errno
128 */
129
130static loff_t gfs2_llseek(struct file *file, loff_t offset, int origin)
131{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500132 struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000133 struct gfs2_holder i_gh;
134 loff_t error;
135
David Teiglandb3b94fa2006-01-16 16:50:04 +0000136 if (origin == 2) {
137 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
138 &i_gh);
139 if (!error) {
140 error = remote_llseek(file, offset, origin);
141 gfs2_glock_dq_uninit(&i_gh);
142 }
143 } else
144 error = remote_llseek(file, offset, origin);
145
146 return error;
147}
148
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000149
150static ssize_t gfs2_direct_IO_read(struct kiocb *iocb, const struct iovec *iov,
151 loff_t offset, unsigned long nr_segs)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000152{
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000153 struct file *file = iocb->ki_filp;
154 struct address_space *mapping = file->f_mapping;
155 ssize_t retval;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000156
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000157 retval = filemap_write_and_wait(mapping);
158 if (retval == 0) {
159 retval = mapping->a_ops->direct_IO(READ, iocb, iov, offset,
160 nr_segs);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000161 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000162 return retval;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000163}
164
165/**
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000166 * __gfs2_file_aio_read - The main GFS2 read function
167 *
168 * N.B. This is almost, but not quite the same as __generic_file_aio_read()
169 * the important subtle different being that inode->i_size isn't valid
170 * unless we are holding a lock, and we do this _only_ on the O_DIRECT
171 * path since otherwise locking is done entirely at the page cache
172 * layer.
David Teiglandb3b94fa2006-01-16 16:50:04 +0000173 */
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000174static ssize_t __gfs2_file_aio_read(struct kiocb *iocb,
175 const struct iovec *iov,
176 unsigned long nr_segs, loff_t *ppos)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000177{
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000178 struct file *filp = iocb->ki_filp;
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500179 struct gfs2_inode *ip = filp->f_mapping->host->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000180 struct gfs2_holder gh;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000181 ssize_t retval;
182 unsigned long seg;
183 size_t count;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000184
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000185 count = 0;
186 for (seg = 0; seg < nr_segs; seg++) {
187 const struct iovec *iv = &iov[seg];
David Teiglandb3b94fa2006-01-16 16:50:04 +0000188
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000189 /*
190 * If any segment has a negative length, or the cumulative
191 * length ever wraps negative then return -EINVAL.
192 */
Steven Whitehoused1665e42006-02-14 11:54:42 +0000193 count += iv->iov_len;
194 if (unlikely((ssize_t)(count|iv->iov_len) < 0))
195 return -EINVAL;
196 if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len))
197 continue;
198 if (seg == 0)
199 return -EFAULT;
200 nr_segs = seg;
201 count -= iv->iov_len; /* This segment is no good */
202 break;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000203 }
204
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000205 /* coalesce the iovecs and go direct-to-BIO for O_DIRECT */
206 if (filp->f_flags & O_DIRECT) {
207 loff_t pos = *ppos, size;
208 struct address_space *mapping;
209 struct inode *inode;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000210
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000211 mapping = filp->f_mapping;
212 inode = mapping->host;
213 retval = 0;
214 if (!count)
215 goto out; /* skip atime */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000216
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000217 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
218 retval = gfs2_glock_nq_m_atime(1, &gh);
219 if (retval)
220 goto out;
Steven Whitehoused1665e42006-02-14 11:54:42 +0000221 if (gfs2_is_stuffed(ip)) {
222 gfs2_glock_dq_m(1, &gh);
223 gfs2_holder_uninit(&gh);
224 goto fallback_to_normal;
225 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000226 size = i_size_read(inode);
227 if (pos < size) {
Steven Whitehoused1665e42006-02-14 11:54:42 +0000228 retval = gfs2_direct_IO_read(iocb, iov, pos, nr_segs);
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000229 if (retval > 0 && !is_sync_kiocb(iocb))
230 retval = -EIOCBQUEUED;
231 if (retval > 0)
232 *ppos = pos + retval;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000233 }
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000234 file_accessed(filp);
235 gfs2_glock_dq_m(1, &gh);
236 gfs2_holder_uninit(&gh);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000237 goto out;
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000238 }
David Teiglandb3b94fa2006-01-16 16:50:04 +0000239
Steven Whitehoused1665e42006-02-14 11:54:42 +0000240fallback_to_normal:
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000241 retval = 0;
242 if (count) {
243 for (seg = 0; seg < nr_segs; seg++) {
244 read_descriptor_t desc;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000245
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000246 desc.written = 0;
247 desc.arg.buf = iov[seg].iov_base;
248 desc.count = iov[seg].iov_len;
249 if (desc.count == 0)
250 continue;
251 desc.error = 0;
252 do_generic_file_read(filp,ppos,&desc,file_read_actor);
253 retval += desc.written;
254 if (desc.error) {
255 retval = retval ?: desc.error;
256 break;
257 }
258 }
259 }
260out:
261 return retval;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000262}
263
264/**
265 * gfs2_read - Read bytes from a file
266 * @file: The file to read from
267 * @buf: The buffer to copy into
268 * @size: The amount of data requested
269 * @offset: The current file offset
270 *
271 * Outputs: Offset - updated according to number of bytes read
272 *
273 * Returns: The number of bytes read, errno on failure
274 */
275
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000276static ssize_t gfs2_read(struct file *filp, char __user *buf, size_t size,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000277 loff_t *offset)
278{
David Teiglandb3b94fa2006-01-16 16:50:04 +0000279 struct iovec local_iov = { .iov_base = buf, .iov_len = size };
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000280 struct kiocb kiocb;
281 ssize_t ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000282
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000283 init_sync_kiocb(&kiocb, filp);
284 ret = __gfs2_file_aio_read(&kiocb, &local_iov, 1, offset);
285 if (-EIOCBQUEUED == ret)
286 ret = wait_on_sync_kiocb(&kiocb);
287 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000288}
289
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000290static ssize_t gfs2_file_readv(struct file *filp, const struct iovec *iov,
291 unsigned long nr_segs, loff_t *ppos)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000292{
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000293 struct kiocb kiocb;
294 ssize_t ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000295
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000296 init_sync_kiocb(&kiocb, filp);
297 ret = __gfs2_file_aio_read(&kiocb, iov, nr_segs, ppos);
298 if (-EIOCBQUEUED == ret)
299 ret = wait_on_sync_kiocb(&kiocb);
300 return ret;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000301}
302
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000303static ssize_t gfs2_file_aio_read(struct kiocb *iocb, char __user *buf,
304 size_t count, loff_t pos)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000305{
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000306 struct iovec local_iov = { .iov_base = buf, .iov_len = count };
David Teiglandb3b94fa2006-01-16 16:50:04 +0000307
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000308 BUG_ON(iocb->ki_pos != pos);
309 return __gfs2_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000310}
311
David Teiglandb3b94fa2006-01-16 16:50:04 +0000312
313/**
314 * filldir_reg_func - Report a directory entry to the caller of gfs2_dir_read()
315 * @opaque: opaque data used by the function
316 * @name: the name of the directory entry
317 * @length: the length of the name
318 * @offset: the entry's offset in the directory
319 * @inum: the inode number the entry points to
320 * @type: the type of inode the entry points to
321 *
322 * Returns: 0 on success, 1 if buffer full
323 */
324
325static int filldir_reg_func(void *opaque, const char *name, unsigned int length,
326 uint64_t offset, struct gfs2_inum *inum,
327 unsigned int type)
328{
329 struct filldir_reg *fdr = (struct filldir_reg *)opaque;
330 struct gfs2_sbd *sdp = fdr->fdr_sbd;
331 int error;
332
333 error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset,
334 inum->no_formal_ino, type);
335 if (error)
336 return 1;
337
338 if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) {
339 gfs2_glock_prefetch_num(sdp,
340 inum->no_addr, &gfs2_inode_glops,
341 LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
342 gfs2_glock_prefetch_num(sdp,
343 inum->no_addr, &gfs2_iopen_glops,
344 LM_ST_SHARED, LM_FLAG_TRY);
345 }
346
347 return 0;
348}
349
350/**
351 * readdir_reg - Read directory entries from a directory
352 * @file: The directory to read from
353 * @dirent: Buffer for dirents
354 * @filldir: Function used to do the copying
355 *
356 * Returns: errno
357 */
358
359static int readdir_reg(struct file *file, void *dirent, filldir_t filldir)
360{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500361 struct gfs2_inode *dip = file->f_mapping->host->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000362 struct filldir_reg fdr;
363 struct gfs2_holder d_gh;
364 uint64_t offset = file->f_pos;
365 int error;
366
367 fdr.fdr_sbd = dip->i_sbd;
368 fdr.fdr_prefetch = 1;
369 fdr.fdr_filldir = filldir;
370 fdr.fdr_opaque = dirent;
371
372 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
373 error = gfs2_glock_nq_atime(&d_gh);
374 if (error) {
375 gfs2_holder_uninit(&d_gh);
376 return error;
377 }
378
379 error = gfs2_dir_read(dip, &offset, &fdr, filldir_reg_func);
380
381 gfs2_glock_dq_uninit(&d_gh);
382
383 file->f_pos = offset;
384
385 return error;
386}
387
388/**
389 * filldir_bad_func - Report a directory entry to the caller of gfs2_dir_read()
390 * @opaque: opaque data used by the function
391 * @name: the name of the directory entry
392 * @length: the length of the name
393 * @offset: the entry's offset in the directory
394 * @inum: the inode number the entry points to
395 * @type: the type of inode the entry points to
396 *
397 * For supporting NFS.
398 *
399 * Returns: 0 on success, 1 if buffer full
400 */
401
402static int filldir_bad_func(void *opaque, const char *name, unsigned int length,
403 uint64_t offset, struct gfs2_inum *inum,
404 unsigned int type)
405{
406 struct filldir_bad *fdb = (struct filldir_bad *)opaque;
407 struct gfs2_sbd *sdp = fdb->fdb_sbd;
408 struct filldir_bad_entry *fbe;
409
410 if (fdb->fdb_entry_off == fdb->fdb_entry_num ||
411 fdb->fdb_name_off + length > fdb->fdb_name_size)
412 return 1;
413
414 fbe = &fdb->fdb_entry[fdb->fdb_entry_off];
415 fbe->fbe_name = fdb->fdb_name + fdb->fdb_name_off;
416 memcpy(fbe->fbe_name, name, length);
417 fbe->fbe_length = length;
418 fbe->fbe_offset = offset;
419 fbe->fbe_inum = *inum;
420 fbe->fbe_type = type;
421
422 fdb->fdb_entry_off++;
423 fdb->fdb_name_off += length;
424
425 if (!(length == 1 && *name == '.')) {
426 gfs2_glock_prefetch_num(sdp,
427 inum->no_addr, &gfs2_inode_glops,
428 LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY);
429 gfs2_glock_prefetch_num(sdp,
430 inum->no_addr, &gfs2_iopen_glops,
431 LM_ST_SHARED, LM_FLAG_TRY);
432 }
433
434 return 0;
435}
436
437/**
438 * readdir_bad - Read directory entries from a directory
439 * @file: The directory to read from
440 * @dirent: Buffer for dirents
441 * @filldir: Function used to do the copying
442 *
443 * For supporting NFS.
444 *
445 * Returns: errno
446 */
447
448static int readdir_bad(struct file *file, void *dirent, filldir_t filldir)
449{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500450 struct gfs2_inode *dip = file->f_mapping->host->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000451 struct gfs2_sbd *sdp = dip->i_sbd;
452 struct filldir_reg fdr;
453 unsigned int entries, size;
454 struct filldir_bad *fdb;
455 struct gfs2_holder d_gh;
456 uint64_t offset = file->f_pos;
457 unsigned int x;
458 struct filldir_bad_entry *fbe;
459 int error;
460
461 entries = gfs2_tune_get(sdp, gt_entries_per_readdir);
462 size = sizeof(struct filldir_bad) +
463 entries * (sizeof(struct filldir_bad_entry) + GFS2_FAST_NAME_SIZE);
464
465 fdb = kzalloc(size, GFP_KERNEL);
466 if (!fdb)
467 return -ENOMEM;
468
469 fdb->fdb_sbd = sdp;
470 fdb->fdb_entry = (struct filldir_bad_entry *)(fdb + 1);
471 fdb->fdb_entry_num = entries;
472 fdb->fdb_name = ((char *)fdb) + sizeof(struct filldir_bad) +
473 entries * sizeof(struct filldir_bad_entry);
474 fdb->fdb_name_size = entries * GFS2_FAST_NAME_SIZE;
475
476 gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh);
477 error = gfs2_glock_nq_atime(&d_gh);
478 if (error) {
479 gfs2_holder_uninit(&d_gh);
480 goto out;
481 }
482
483 error = gfs2_dir_read(dip, &offset, fdb, filldir_bad_func);
484
485 gfs2_glock_dq_uninit(&d_gh);
486
487 fdr.fdr_sbd = sdp;
488 fdr.fdr_prefetch = 0;
489 fdr.fdr_filldir = filldir;
490 fdr.fdr_opaque = dirent;
491
492 for (x = 0; x < fdb->fdb_entry_off; x++) {
493 fbe = &fdb->fdb_entry[x];
494
495 error = filldir_reg_func(&fdr,
496 fbe->fbe_name, fbe->fbe_length,
497 fbe->fbe_offset,
498 &fbe->fbe_inum, fbe->fbe_type);
499 if (error) {
500 file->f_pos = fbe->fbe_offset;
501 error = 0;
502 goto out;
503 }
504 }
505
506 file->f_pos = offset;
507
508 out:
509 kfree(fdb);
510
511 return error;
512}
513
514/**
515 * gfs2_readdir - Read directory entries from a directory
516 * @file: The directory to read from
517 * @dirent: Buffer for dirents
518 * @filldir: Function used to do the copying
519 *
520 * Returns: errno
521 */
522
523static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir)
524{
525 int error;
526
David Teiglandb3b94fa2006-01-16 16:50:04 +0000527 if (strcmp(current->comm, "nfsd") != 0)
528 error = readdir_reg(file, dirent, filldir);
529 else
530 error = readdir_bad(file, dirent, filldir);
531
532 return error;
533}
534
David Teigland6a6b3d02006-02-23 10:11:47 +0000535static int gfs2_ioctl_flags(struct gfs2_inode *ip, unsigned int cmd,
536 unsigned long arg)
David Teiglandb3b94fa2006-01-16 16:50:04 +0000537{
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500538 unsigned int lmode = (cmd == GFS2_IOCTL_SETFLAGS) ?
539 LM_ST_EXCLUSIVE : LM_ST_SHARED;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000540 struct buffer_head *dibh;
541 struct gfs2_holder i_gh;
542 int error;
543 __u32 flags = 0, change;
544
545 if (cmd == GFS2_IOCTL_SETFLAGS) {
546 error = get_user(flags, (__u32 __user *)arg);
547 if (error)
548 return -EFAULT;
549 }
550
551 error = gfs2_glock_nq_init(ip->i_gl, lmode, 0, &i_gh);
552 if (error)
553 return error;
554
555 if (cmd == GFS2_IOCTL_SETFLAGS) {
556 change = flags ^ ip->i_di.di_flags;
557 error = -EPERM;
558 if (change & (GFS2_DIF_IMMUTABLE|GFS2_DIF_APPENDONLY)) {
559 if (!capable(CAP_LINUX_IMMUTABLE))
560 goto out;
561 }
562 error = -EINVAL;
563 if (flags & (GFS2_DIF_JDATA|GFS2_DIF_DIRECTIO)) {
564 if (!S_ISREG(ip->i_di.di_mode))
565 goto out;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000566 }
Steven Whitehouse568f4c92006-02-27 12:00:42 -0500567 if (flags &
568 (GFS2_DIF_INHERIT_JDATA|GFS2_DIF_INHERIT_DIRECTIO)) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000569 if (!S_ISDIR(ip->i_di.di_mode))
570 goto out;
571 }
572
573 error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0);
574 if (error)
575 goto out;
576
577 error = gfs2_meta_inode_buffer(ip, &dibh);
578 if (error)
579 goto out_trans_end;
580
581 ip->i_di.di_flags = flags;
582
Steven Whitehoused4e9c4c2006-01-18 11:19:28 +0000583 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000584 gfs2_dinode_out(&ip->i_di, dibh->b_data);
585
586 brelse(dibh);
587
588out_trans_end:
589 gfs2_trans_end(ip->i_sbd);
590 } else {
591 flags = ip->i_di.di_flags;
592 }
593out:
594 gfs2_glock_dq_uninit(&i_gh);
595 if (cmd == GFS2_IOCTL_GETFLAGS) {
596 if (put_user(flags, (__u32 __user *)arg))
597 return -EFAULT;
598 }
599 return error;
600}
601
602/**
603 * gfs2_ioctl - do an ioctl on a file
604 * @inode: the inode
605 * @file: the file pointer
606 * @cmd: the ioctl command
607 * @arg: the argument
608 *
609 * Returns: errno
610 */
611
612static int gfs2_ioctl(struct inode *inode, struct file *file, unsigned int cmd,
613 unsigned long arg)
614{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500615 struct gfs2_inode *ip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000616
David Teiglandb3b94fa2006-01-16 16:50:04 +0000617 switch (cmd) {
David Teiglandb3b94fa2006-01-16 16:50:04 +0000618 case GFS2_IOCTL_SETFLAGS:
619 case GFS2_IOCTL_GETFLAGS:
620 return gfs2_ioctl_flags(ip, cmd, arg);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000621
622 default:
623 return -ENOTTY;
624 }
625}
626
627/**
628 * gfs2_mmap -
629 * @file: The file to map
630 * @vma: The VMA which described the mapping
631 *
632 * Returns: 0 or error code
633 */
634
635static int gfs2_mmap(struct file *file, struct vm_area_struct *vma)
636{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500637 struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000638 struct gfs2_holder i_gh;
639 int error;
640
David Teiglandb3b94fa2006-01-16 16:50:04 +0000641 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh);
642 error = gfs2_glock_nq_atime(&i_gh);
643 if (error) {
644 gfs2_holder_uninit(&i_gh);
645 return error;
646 }
647
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000648 /* This is VM_MAYWRITE instead of VM_WRITE because a call
649 to mprotect() can turn on VM_WRITE later. */
David Teiglandb3b94fa2006-01-16 16:50:04 +0000650
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000651 if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) ==
652 (VM_MAYSHARE | VM_MAYWRITE))
653 vma->vm_ops = &gfs2_vm_ops_sharewrite;
654 else
655 vma->vm_ops = &gfs2_vm_ops_private;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000656
657 gfs2_glock_dq_uninit(&i_gh);
658
659 return error;
660}
661
662/**
663 * gfs2_open - open a file
664 * @inode: the inode to open
665 * @file: the struct file for this opening
666 *
667 * Returns: errno
668 */
669
670static int gfs2_open(struct inode *inode, struct file *file)
671{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500672 struct gfs2_inode *ip = inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000673 struct gfs2_holder i_gh;
674 struct gfs2_file *fp;
675 int error;
676
David Teiglandb3b94fa2006-01-16 16:50:04 +0000677 fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL);
678 if (!fp)
679 return -ENOMEM;
680
Steven Whitehousef55ab262006-02-21 12:51:39 +0000681 mutex_init(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000682
683 fp->f_inode = ip;
684 fp->f_vfile = file;
685
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500686 gfs2_assert_warn(ip->i_sbd, !file->private_data);
687 file->private_data = fp;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000688
689 if (S_ISREG(ip->i_di.di_mode)) {
690 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY,
691 &i_gh);
692 if (error)
693 goto fail;
694
695 if (!(file->f_flags & O_LARGEFILE) &&
696 ip->i_di.di_size > MAX_NON_LFS) {
697 error = -EFBIG;
698 goto fail_gunlock;
699 }
700
701 /* Listen to the Direct I/O flag */
702
703 if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO)
704 file->f_flags |= O_DIRECT;
705
David Teiglandb3b94fa2006-01-16 16:50:04 +0000706 gfs2_glock_dq_uninit(&i_gh);
707 }
708
709 return 0;
710
711 fail_gunlock:
712 gfs2_glock_dq_uninit(&i_gh);
713
714 fail:
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500715 file->private_data = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000716 kfree(fp);
717
718 return error;
719}
720
721/**
722 * gfs2_close - called to close a struct file
723 * @inode: the inode the struct file belongs to
724 * @file: the struct file being closed
725 *
726 * Returns: errno
727 */
728
729static int gfs2_close(struct inode *inode, struct file *file)
730{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500731 struct gfs2_sbd *sdp = inode->i_sb->s_fs_info;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000732 struct gfs2_file *fp;
733
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500734 fp = file->private_data;
735 file->private_data = NULL;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000736
737 if (gfs2_assert_warn(sdp, fp))
738 return -EIO;
739
740 kfree(fp);
741
742 return 0;
743}
744
745/**
746 * gfs2_fsync - sync the dirty data for a file (across the cluster)
747 * @file: the file that points to the dentry (we ignore this)
748 * @dentry: the dentry that points to the inode to sync
749 *
750 * Returns: errno
751 */
752
753static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync)
754{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500755 struct gfs2_inode *ip = dentry->d_inode->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000756
David Teiglandb3b94fa2006-01-16 16:50:04 +0000757 gfs2_log_flush_glock(ip->i_gl);
758
759 return 0;
760}
761
762/**
763 * gfs2_lock - acquire/release a posix lock on a file
764 * @file: the file pointer
765 * @cmd: either modify or retrieve lock state, possibly wait
766 * @fl: type and range of lock
767 *
768 * Returns: errno
769 */
770
771static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl)
772{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500773 struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000774 struct gfs2_sbd *sdp = ip->i_sbd;
775 struct lm_lockname name =
776 { .ln_number = ip->i_num.no_addr,
777 .ln_type = LM_TYPE_PLOCK };
778
David Teiglandb3b94fa2006-01-16 16:50:04 +0000779 if (!(fl->fl_flags & FL_POSIX))
780 return -ENOLCK;
781 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
782 return -ENOLCK;
783
784 if (sdp->sd_args.ar_localflocks) {
785 if (IS_GETLK(cmd)) {
786 struct file_lock *tmp;
787 lock_kernel();
788 tmp = posix_test_lock(file, fl);
789 fl->fl_type = F_UNLCK;
790 if (tmp)
791 memcpy(fl, tmp, sizeof(struct file_lock));
792 unlock_kernel();
793 return 0;
794 } else {
795 int error;
796 lock_kernel();
797 error = posix_lock_file_wait(file, fl);
798 unlock_kernel();
799 return error;
800 }
801 }
802
803 if (IS_GETLK(cmd))
804 return gfs2_lm_plock_get(sdp, &name, file, fl);
805 else if (fl->fl_type == F_UNLCK)
806 return gfs2_lm_punlock(sdp, &name, file, fl);
807 else
808 return gfs2_lm_plock(sdp, &name, file, cmd, fl);
809}
810
811/**
812 * gfs2_sendfile - Send bytes to a file or socket
813 * @in_file: The file to read from
814 * @out_file: The file to write to
815 * @count: The amount of data
816 * @offset: The beginning file offset
817 *
818 * Outputs: offset - updated according to number of bytes read
819 *
820 * Returns: The number of bytes sent, errno on failure
821 */
822
823static ssize_t gfs2_sendfile(struct file *in_file, loff_t *offset, size_t count,
824 read_actor_t actor, void *target)
825{
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000826 return generic_file_sendfile(in_file, offset, count, actor, target);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000827}
828
829static int do_flock(struct file *file, int cmd, struct file_lock *fl)
830{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500831 struct gfs2_file *fp = file->private_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000832 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
833 struct gfs2_inode *ip = fp->f_inode;
834 struct gfs2_glock *gl;
835 unsigned int state;
836 int flags;
837 int error = 0;
838
839 state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED;
840 flags = ((IS_SETLKW(cmd)) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE;
841
Steven Whitehousef55ab262006-02-21 12:51:39 +0000842 mutex_lock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000843
844 gl = fl_gh->gh_gl;
845 if (gl) {
846 if (fl_gh->gh_state == state)
847 goto out;
848 gfs2_glock_hold(gl);
849 flock_lock_file_wait(file,
850 &(struct file_lock){.fl_type = F_UNLCK});
851 gfs2_glock_dq_uninit(fl_gh);
852 } else {
853 error = gfs2_glock_get(ip->i_sbd,
854 ip->i_num.no_addr, &gfs2_flock_glops,
855 CREATE, &gl);
856 if (error)
857 goto out;
858 }
859
860 gfs2_holder_init(gl, state, flags, fl_gh);
861 gfs2_glock_put(gl);
862
863 error = gfs2_glock_nq(fl_gh);
864 if (error) {
865 gfs2_holder_uninit(fl_gh);
866 if (error == GLR_TRYFAILED)
867 error = -EAGAIN;
868 } else {
869 error = flock_lock_file_wait(file, fl);
870 gfs2_assert_warn(ip->i_sbd, !error);
871 }
872
873 out:
Steven Whitehousef55ab262006-02-21 12:51:39 +0000874 mutex_unlock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000875
876 return error;
877}
878
879static void do_unflock(struct file *file, struct file_lock *fl)
880{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500881 struct gfs2_file *fp = file->private_data;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000882 struct gfs2_holder *fl_gh = &fp->f_fl_gh;
883
Steven Whitehousef55ab262006-02-21 12:51:39 +0000884 mutex_lock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000885 flock_lock_file_wait(file, fl);
886 if (fl_gh->gh_gl)
887 gfs2_glock_dq_uninit(fl_gh);
Steven Whitehousef55ab262006-02-21 12:51:39 +0000888 mutex_unlock(&fp->f_fl_mutex);
David Teiglandb3b94fa2006-01-16 16:50:04 +0000889}
890
891/**
892 * gfs2_flock - acquire/release a flock lock on a file
893 * @file: the file pointer
894 * @cmd: either modify or retrieve lock state, possibly wait
895 * @fl: type and range of lock
896 *
897 * Returns: errno
898 */
899
900static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl)
901{
Steven Whitehouse5c676f62006-02-27 17:23:27 -0500902 struct gfs2_inode *ip = file->f_mapping->host->u.generic_ip;
David Teiglandb3b94fa2006-01-16 16:50:04 +0000903 struct gfs2_sbd *sdp = ip->i_sbd;
904
David Teiglandb3b94fa2006-01-16 16:50:04 +0000905 if (!(fl->fl_flags & FL_FLOCK))
906 return -ENOLCK;
907 if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
908 return -ENOLCK;
909
910 if (sdp->sd_args.ar_localflocks)
911 return flock_lock_file_wait(file, fl);
912
913 if (fl->fl_type == F_UNLCK) {
914 do_unflock(file, fl);
915 return 0;
916 } else
917 return do_flock(file, cmd, fl);
918}
919
920struct file_operations gfs2_file_fops = {
921 .llseek = gfs2_llseek,
922 .read = gfs2_read,
Steven Whitehouse18ec7d52006-02-08 11:50:51 +0000923 .readv = gfs2_file_readv,
924 .aio_read = gfs2_file_aio_read,
925 .write = generic_file_write,
926 .writev = generic_file_writev,
927 .aio_write = generic_file_aio_write,
David Teiglandb3b94fa2006-01-16 16:50:04 +0000928 .ioctl = gfs2_ioctl,
929 .mmap = gfs2_mmap,
930 .open = gfs2_open,
931 .release = gfs2_close,
932 .fsync = gfs2_fsync,
933 .lock = gfs2_lock,
934 .sendfile = gfs2_sendfile,
935 .flock = gfs2_flock,
936};
937
938struct file_operations gfs2_dir_fops = {
939 .readdir = gfs2_readdir,
940 .ioctl = gfs2_ioctl,
941 .open = gfs2_open,
942 .release = gfs2_close,
943 .fsync = gfs2_fsync,
944 .lock = gfs2_lock,
945 .flock = gfs2_flock,
946};
947