]>
Commit | Line | Data |
---|---|---|
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> | |
18ec7d5c | 21 | #include <linux/fs.h> |
b3b94faa DT |
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" | |
b3b94faa DT |
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 | ||
61a30dcb SW |
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 | ||
18ec7d5c SW |
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; | |
61a30dcb SW |
108 | do_generic_mapping_read(inode->i_mapping, ra_state, |
109 | &gfs2_internal_file_sentinal, pos, &desc, | |
110 | gfs2_read_actor); | |
18ec7d5c SW |
111 | return desc.written ? desc.written : desc.error; |
112 | } | |
b3b94faa DT |
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 | ||
b3b94faa DT |
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 | ||
b3b94faa | 145 | |
18ec7d5c SW |
146 | static ssize_t gfs2_direct_IO_read(struct kiocb *iocb, const struct iovec *iov, |
147 | loff_t offset, unsigned long nr_segs) | |
b3b94faa | 148 | { |
18ec7d5c SW |
149 | struct file *file = iocb->ki_filp; |
150 | struct address_space *mapping = file->f_mapping; | |
151 | ssize_t retval; | |
b3b94faa | 152 | |
18ec7d5c SW |
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); | |
b3b94faa | 157 | } |
18ec7d5c | 158 | return retval; |
b3b94faa DT |
159 | } |
160 | ||
161 | /** | |
18ec7d5c SW |
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. | |
b3b94faa | 169 | */ |
18ec7d5c SW |
170 | static ssize_t __gfs2_file_aio_read(struct kiocb *iocb, |
171 | const struct iovec *iov, | |
172 | unsigned long nr_segs, loff_t *ppos) | |
b3b94faa | 173 | { |
18ec7d5c SW |
174 | struct file *filp = iocb->ki_filp; |
175 | struct gfs2_inode *ip = get_v2ip(filp->f_mapping->host); | |
b3b94faa | 176 | struct gfs2_holder gh; |
18ec7d5c SW |
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 | */ | |
d1665e41 SW |
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; | |
18ec7d5c SW |
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; | |
d1665e41 SW |
217 | if (gfs2_is_stuffed(ip)) { |
218 | gfs2_glock_dq_m(1, &gh); | |
219 | gfs2_holder_uninit(&gh); | |
220 | goto fallback_to_normal; | |
221 | } | |
18ec7d5c SW |
222 | size = i_size_read(inode); |
223 | if (pos < size) { | |
d1665e41 | 224 | retval = gfs2_direct_IO_read(iocb, iov, pos, nr_segs); |
18ec7d5c SW |
225 | if (retval > 0 && !is_sync_kiocb(iocb)) |
226 | retval = -EIOCBQUEUED; | |
227 | if (retval > 0) | |
228 | *ppos = pos + retval; | |
b3b94faa | 229 | } |
18ec7d5c SW |
230 | file_accessed(filp); |
231 | gfs2_glock_dq_m(1, &gh); | |
232 | gfs2_holder_uninit(&gh); | |
b3b94faa | 233 | goto out; |
18ec7d5c | 234 | } |
b3b94faa | 235 | |
d1665e41 | 236 | fallback_to_normal: |
18ec7d5c SW |
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; | |
b3b94faa DT |
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 | ||
18ec7d5c | 272 | static ssize_t gfs2_read(struct file *filp, char __user *buf, size_t size, |
b3b94faa DT |
273 | loff_t *offset) |
274 | { | |
b3b94faa | 275 | struct iovec local_iov = { .iov_base = buf, .iov_len = size }; |
18ec7d5c SW |
276 | struct kiocb kiocb; |
277 | ssize_t ret; | |
b3b94faa | 278 | |
18ec7d5c SW |
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; | |
b3b94faa DT |
284 | } |
285 | ||
18ec7d5c SW |
286 | static ssize_t gfs2_file_readv(struct file *filp, const struct iovec *iov, |
287 | unsigned long nr_segs, loff_t *ppos) | |
b3b94faa | 288 | { |
18ec7d5c SW |
289 | struct kiocb kiocb; |
290 | ssize_t ret; | |
b3b94faa | 291 | |
18ec7d5c SW |
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; | |
b3b94faa DT |
297 | } |
298 | ||
18ec7d5c SW |
299 | static ssize_t gfs2_file_aio_read(struct kiocb *iocb, char __user *buf, |
300 | size_t count, loff_t pos) | |
b3b94faa | 301 | { |
18ec7d5c SW |
302 | struct file *filp = iocb->ki_filp; |
303 | struct iovec local_iov = { .iov_base = buf, .iov_len = count }; | |
b3b94faa | 304 | |
18ec7d5c SW |
305 | BUG_ON(iocb->ki_pos != pos); |
306 | return __gfs2_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos); | |
b3b94faa DT |
307 | } |
308 | ||
b3b94faa DT |
309 | |
310 | /** | |
311 | * filldir_reg_func - Report a directory entry to the caller of gfs2_dir_read() | |
312 | * @opaque: opaque data used by the function | |
313 | * @name: the name of the directory entry | |
314 | * @length: the length of the name | |
315 | * @offset: the entry's offset in the directory | |
316 | * @inum: the inode number the entry points to | |
317 | * @type: the type of inode the entry points to | |
318 | * | |
319 | * Returns: 0 on success, 1 if buffer full | |
320 | */ | |
321 | ||
322 | static int filldir_reg_func(void *opaque, const char *name, unsigned int length, | |
323 | uint64_t offset, struct gfs2_inum *inum, | |
324 | unsigned int type) | |
325 | { | |
326 | struct filldir_reg *fdr = (struct filldir_reg *)opaque; | |
327 | struct gfs2_sbd *sdp = fdr->fdr_sbd; | |
328 | int error; | |
329 | ||
330 | error = fdr->fdr_filldir(fdr->fdr_opaque, name, length, offset, | |
331 | inum->no_formal_ino, type); | |
332 | if (error) | |
333 | return 1; | |
334 | ||
335 | if (fdr->fdr_prefetch && !(length == 1 && *name == '.')) { | |
336 | gfs2_glock_prefetch_num(sdp, | |
337 | inum->no_addr, &gfs2_inode_glops, | |
338 | LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY); | |
339 | gfs2_glock_prefetch_num(sdp, | |
340 | inum->no_addr, &gfs2_iopen_glops, | |
341 | LM_ST_SHARED, LM_FLAG_TRY); | |
342 | } | |
343 | ||
344 | return 0; | |
345 | } | |
346 | ||
347 | /** | |
348 | * readdir_reg - Read directory entries from a directory | |
349 | * @file: The directory to read from | |
350 | * @dirent: Buffer for dirents | |
351 | * @filldir: Function used to do the copying | |
352 | * | |
353 | * Returns: errno | |
354 | */ | |
355 | ||
356 | static int readdir_reg(struct file *file, void *dirent, filldir_t filldir) | |
357 | { | |
358 | struct gfs2_inode *dip = get_v2ip(file->f_mapping->host); | |
359 | struct filldir_reg fdr; | |
360 | struct gfs2_holder d_gh; | |
361 | uint64_t offset = file->f_pos; | |
362 | int error; | |
363 | ||
364 | fdr.fdr_sbd = dip->i_sbd; | |
365 | fdr.fdr_prefetch = 1; | |
366 | fdr.fdr_filldir = filldir; | |
367 | fdr.fdr_opaque = dirent; | |
368 | ||
369 | gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh); | |
370 | error = gfs2_glock_nq_atime(&d_gh); | |
371 | if (error) { | |
372 | gfs2_holder_uninit(&d_gh); | |
373 | return error; | |
374 | } | |
375 | ||
376 | error = gfs2_dir_read(dip, &offset, &fdr, filldir_reg_func); | |
377 | ||
378 | gfs2_glock_dq_uninit(&d_gh); | |
379 | ||
380 | file->f_pos = offset; | |
381 | ||
382 | return error; | |
383 | } | |
384 | ||
385 | /** | |
386 | * filldir_bad_func - Report a directory entry to the caller of gfs2_dir_read() | |
387 | * @opaque: opaque data used by the function | |
388 | * @name: the name of the directory entry | |
389 | * @length: the length of the name | |
390 | * @offset: the entry's offset in the directory | |
391 | * @inum: the inode number the entry points to | |
392 | * @type: the type of inode the entry points to | |
393 | * | |
394 | * For supporting NFS. | |
395 | * | |
396 | * Returns: 0 on success, 1 if buffer full | |
397 | */ | |
398 | ||
399 | static int filldir_bad_func(void *opaque, const char *name, unsigned int length, | |
400 | uint64_t offset, struct gfs2_inum *inum, | |
401 | unsigned int type) | |
402 | { | |
403 | struct filldir_bad *fdb = (struct filldir_bad *)opaque; | |
404 | struct gfs2_sbd *sdp = fdb->fdb_sbd; | |
405 | struct filldir_bad_entry *fbe; | |
406 | ||
407 | if (fdb->fdb_entry_off == fdb->fdb_entry_num || | |
408 | fdb->fdb_name_off + length > fdb->fdb_name_size) | |
409 | return 1; | |
410 | ||
411 | fbe = &fdb->fdb_entry[fdb->fdb_entry_off]; | |
412 | fbe->fbe_name = fdb->fdb_name + fdb->fdb_name_off; | |
413 | memcpy(fbe->fbe_name, name, length); | |
414 | fbe->fbe_length = length; | |
415 | fbe->fbe_offset = offset; | |
416 | fbe->fbe_inum = *inum; | |
417 | fbe->fbe_type = type; | |
418 | ||
419 | fdb->fdb_entry_off++; | |
420 | fdb->fdb_name_off += length; | |
421 | ||
422 | if (!(length == 1 && *name == '.')) { | |
423 | gfs2_glock_prefetch_num(sdp, | |
424 | inum->no_addr, &gfs2_inode_glops, | |
425 | LM_ST_SHARED, LM_FLAG_TRY | LM_FLAG_ANY); | |
426 | gfs2_glock_prefetch_num(sdp, | |
427 | inum->no_addr, &gfs2_iopen_glops, | |
428 | LM_ST_SHARED, LM_FLAG_TRY); | |
429 | } | |
430 | ||
431 | return 0; | |
432 | } | |
433 | ||
434 | /** | |
435 | * readdir_bad - Read directory entries from a directory | |
436 | * @file: The directory to read from | |
437 | * @dirent: Buffer for dirents | |
438 | * @filldir: Function used to do the copying | |
439 | * | |
440 | * For supporting NFS. | |
441 | * | |
442 | * Returns: errno | |
443 | */ | |
444 | ||
445 | static int readdir_bad(struct file *file, void *dirent, filldir_t filldir) | |
446 | { | |
447 | struct gfs2_inode *dip = get_v2ip(file->f_mapping->host); | |
448 | struct gfs2_sbd *sdp = dip->i_sbd; | |
449 | struct filldir_reg fdr; | |
450 | unsigned int entries, size; | |
451 | struct filldir_bad *fdb; | |
452 | struct gfs2_holder d_gh; | |
453 | uint64_t offset = file->f_pos; | |
454 | unsigned int x; | |
455 | struct filldir_bad_entry *fbe; | |
456 | int error; | |
457 | ||
458 | entries = gfs2_tune_get(sdp, gt_entries_per_readdir); | |
459 | size = sizeof(struct filldir_bad) + | |
460 | entries * (sizeof(struct filldir_bad_entry) + GFS2_FAST_NAME_SIZE); | |
461 | ||
462 | fdb = kzalloc(size, GFP_KERNEL); | |
463 | if (!fdb) | |
464 | return -ENOMEM; | |
465 | ||
466 | fdb->fdb_sbd = sdp; | |
467 | fdb->fdb_entry = (struct filldir_bad_entry *)(fdb + 1); | |
468 | fdb->fdb_entry_num = entries; | |
469 | fdb->fdb_name = ((char *)fdb) + sizeof(struct filldir_bad) + | |
470 | entries * sizeof(struct filldir_bad_entry); | |
471 | fdb->fdb_name_size = entries * GFS2_FAST_NAME_SIZE; | |
472 | ||
473 | gfs2_holder_init(dip->i_gl, LM_ST_SHARED, GL_ATIME, &d_gh); | |
474 | error = gfs2_glock_nq_atime(&d_gh); | |
475 | if (error) { | |
476 | gfs2_holder_uninit(&d_gh); | |
477 | goto out; | |
478 | } | |
479 | ||
480 | error = gfs2_dir_read(dip, &offset, fdb, filldir_bad_func); | |
481 | ||
482 | gfs2_glock_dq_uninit(&d_gh); | |
483 | ||
484 | fdr.fdr_sbd = sdp; | |
485 | fdr.fdr_prefetch = 0; | |
486 | fdr.fdr_filldir = filldir; | |
487 | fdr.fdr_opaque = dirent; | |
488 | ||
489 | for (x = 0; x < fdb->fdb_entry_off; x++) { | |
490 | fbe = &fdb->fdb_entry[x]; | |
491 | ||
492 | error = filldir_reg_func(&fdr, | |
493 | fbe->fbe_name, fbe->fbe_length, | |
494 | fbe->fbe_offset, | |
495 | &fbe->fbe_inum, fbe->fbe_type); | |
496 | if (error) { | |
497 | file->f_pos = fbe->fbe_offset; | |
498 | error = 0; | |
499 | goto out; | |
500 | } | |
501 | } | |
502 | ||
503 | file->f_pos = offset; | |
504 | ||
505 | out: | |
506 | kfree(fdb); | |
507 | ||
508 | return error; | |
509 | } | |
510 | ||
511 | /** | |
512 | * gfs2_readdir - Read directory entries from a directory | |
513 | * @file: The directory to read from | |
514 | * @dirent: Buffer for dirents | |
515 | * @filldir: Function used to do the copying | |
516 | * | |
517 | * Returns: errno | |
518 | */ | |
519 | ||
520 | static int gfs2_readdir(struct file *file, void *dirent, filldir_t filldir) | |
521 | { | |
522 | int error; | |
523 | ||
b3b94faa DT |
524 | if (strcmp(current->comm, "nfsd") != 0) |
525 | error = readdir_reg(file, dirent, filldir); | |
526 | else | |
527 | error = readdir_bad(file, dirent, filldir); | |
528 | ||
529 | return error; | |
530 | } | |
531 | ||
6a6b3d01 DT |
532 | static int gfs2_ioctl_flags(struct gfs2_inode *ip, unsigned int cmd, |
533 | unsigned long arg) | |
b3b94faa DT |
534 | { |
535 | unsigned int lmode = (cmd == GFS2_IOCTL_SETFLAGS) ? LM_ST_EXCLUSIVE : LM_ST_SHARED; | |
536 | struct buffer_head *dibh; | |
537 | struct gfs2_holder i_gh; | |
538 | int error; | |
539 | __u32 flags = 0, change; | |
540 | ||
541 | if (cmd == GFS2_IOCTL_SETFLAGS) { | |
542 | error = get_user(flags, (__u32 __user *)arg); | |
543 | if (error) | |
544 | return -EFAULT; | |
545 | } | |
546 | ||
547 | error = gfs2_glock_nq_init(ip->i_gl, lmode, 0, &i_gh); | |
548 | if (error) | |
549 | return error; | |
550 | ||
551 | if (cmd == GFS2_IOCTL_SETFLAGS) { | |
552 | change = flags ^ ip->i_di.di_flags; | |
553 | error = -EPERM; | |
554 | if (change & (GFS2_DIF_IMMUTABLE|GFS2_DIF_APPENDONLY)) { | |
555 | if (!capable(CAP_LINUX_IMMUTABLE)) | |
556 | goto out; | |
557 | } | |
558 | error = -EINVAL; | |
559 | if (flags & (GFS2_DIF_JDATA|GFS2_DIF_DIRECTIO)) { | |
560 | if (!S_ISREG(ip->i_di.di_mode)) | |
561 | goto out; | |
b3b94faa DT |
562 | } |
563 | if (flags & (GFS2_DIF_INHERIT_JDATA|GFS2_DIF_INHERIT_DIRECTIO)) { | |
564 | if (!S_ISDIR(ip->i_di.di_mode)) | |
565 | goto out; | |
566 | } | |
567 | ||
568 | error = gfs2_trans_begin(ip->i_sbd, RES_DINODE, 0); | |
569 | if (error) | |
570 | goto out; | |
571 | ||
572 | error = gfs2_meta_inode_buffer(ip, &dibh); | |
573 | if (error) | |
574 | goto out_trans_end; | |
575 | ||
576 | ip->i_di.di_flags = flags; | |
577 | ||
d4e9c4c3 | 578 | gfs2_trans_add_bh(ip->i_gl, dibh, 1); |
b3b94faa DT |
579 | gfs2_dinode_out(&ip->i_di, dibh->b_data); |
580 | ||
581 | brelse(dibh); | |
582 | ||
583 | out_trans_end: | |
584 | gfs2_trans_end(ip->i_sbd); | |
585 | } else { | |
586 | flags = ip->i_di.di_flags; | |
587 | } | |
588 | out: | |
589 | gfs2_glock_dq_uninit(&i_gh); | |
590 | if (cmd == GFS2_IOCTL_GETFLAGS) { | |
591 | if (put_user(flags, (__u32 __user *)arg)) | |
592 | return -EFAULT; | |
593 | } | |
594 | return error; | |
595 | } | |
596 | ||
597 | /** | |
598 | * gfs2_ioctl - do an ioctl on a file | |
599 | * @inode: the inode | |
600 | * @file: the file pointer | |
601 | * @cmd: the ioctl command | |
602 | * @arg: the argument | |
603 | * | |
604 | * Returns: errno | |
605 | */ | |
606 | ||
607 | static int gfs2_ioctl(struct inode *inode, struct file *file, unsigned int cmd, | |
608 | unsigned long arg) | |
609 | { | |
610 | struct gfs2_inode *ip = get_v2ip(inode); | |
611 | ||
b3b94faa | 612 | switch (cmd) { |
b3b94faa DT |
613 | case GFS2_IOCTL_SETFLAGS: |
614 | case GFS2_IOCTL_GETFLAGS: | |
615 | return gfs2_ioctl_flags(ip, cmd, arg); | |
b3b94faa DT |
616 | |
617 | default: | |
618 | return -ENOTTY; | |
619 | } | |
620 | } | |
621 | ||
622 | /** | |
623 | * gfs2_mmap - | |
624 | * @file: The file to map | |
625 | * @vma: The VMA which described the mapping | |
626 | * | |
627 | * Returns: 0 or error code | |
628 | */ | |
629 | ||
630 | static int gfs2_mmap(struct file *file, struct vm_area_struct *vma) | |
631 | { | |
632 | struct gfs2_inode *ip = get_v2ip(file->f_mapping->host); | |
633 | struct gfs2_holder i_gh; | |
634 | int error; | |
635 | ||
b3b94faa DT |
636 | gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &i_gh); |
637 | error = gfs2_glock_nq_atime(&i_gh); | |
638 | if (error) { | |
639 | gfs2_holder_uninit(&i_gh); | |
640 | return error; | |
641 | } | |
642 | ||
18ec7d5c SW |
643 | /* This is VM_MAYWRITE instead of VM_WRITE because a call |
644 | to mprotect() can turn on VM_WRITE later. */ | |
645 | ||
646 | if ((vma->vm_flags & (VM_MAYSHARE | VM_MAYWRITE)) == | |
647 | (VM_MAYSHARE | VM_MAYWRITE)) | |
648 | vma->vm_ops = &gfs2_vm_ops_sharewrite; | |
649 | else | |
650 | vma->vm_ops = &gfs2_vm_ops_private; | |
b3b94faa DT |
651 | |
652 | gfs2_glock_dq_uninit(&i_gh); | |
653 | ||
654 | return error; | |
655 | } | |
656 | ||
657 | /** | |
658 | * gfs2_open - open a file | |
659 | * @inode: the inode to open | |
660 | * @file: the struct file for this opening | |
661 | * | |
662 | * Returns: errno | |
663 | */ | |
664 | ||
665 | static int gfs2_open(struct inode *inode, struct file *file) | |
666 | { | |
667 | struct gfs2_inode *ip = get_v2ip(inode); | |
668 | struct gfs2_holder i_gh; | |
669 | struct gfs2_file *fp; | |
670 | int error; | |
671 | ||
b3b94faa DT |
672 | fp = kzalloc(sizeof(struct gfs2_file), GFP_KERNEL); |
673 | if (!fp) | |
674 | return -ENOMEM; | |
675 | ||
f55ab26a | 676 | mutex_init(&fp->f_fl_mutex); |
b3b94faa DT |
677 | |
678 | fp->f_inode = ip; | |
679 | fp->f_vfile = file; | |
680 | ||
681 | gfs2_assert_warn(ip->i_sbd, !get_v2fp(file)); | |
682 | set_v2fp(file, fp); | |
683 | ||
684 | if (S_ISREG(ip->i_di.di_mode)) { | |
685 | error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, | |
686 | &i_gh); | |
687 | if (error) | |
688 | goto fail; | |
689 | ||
690 | if (!(file->f_flags & O_LARGEFILE) && | |
691 | ip->i_di.di_size > MAX_NON_LFS) { | |
692 | error = -EFBIG; | |
693 | goto fail_gunlock; | |
694 | } | |
695 | ||
696 | /* Listen to the Direct I/O flag */ | |
697 | ||
698 | if (ip->i_di.di_flags & GFS2_DIF_DIRECTIO) | |
699 | file->f_flags |= O_DIRECT; | |
700 | ||
b3b94faa DT |
701 | gfs2_glock_dq_uninit(&i_gh); |
702 | } | |
703 | ||
704 | return 0; | |
705 | ||
706 | fail_gunlock: | |
707 | gfs2_glock_dq_uninit(&i_gh); | |
708 | ||
709 | fail: | |
710 | set_v2fp(file, NULL); | |
711 | kfree(fp); | |
712 | ||
713 | return error; | |
714 | } | |
715 | ||
716 | /** | |
717 | * gfs2_close - called to close a struct file | |
718 | * @inode: the inode the struct file belongs to | |
719 | * @file: the struct file being closed | |
720 | * | |
721 | * Returns: errno | |
722 | */ | |
723 | ||
724 | static int gfs2_close(struct inode *inode, struct file *file) | |
725 | { | |
726 | struct gfs2_sbd *sdp = get_v2sdp(inode->i_sb); | |
727 | struct gfs2_file *fp; | |
728 | ||
b3b94faa DT |
729 | fp = get_v2fp(file); |
730 | set_v2fp(file, NULL); | |
731 | ||
732 | if (gfs2_assert_warn(sdp, fp)) | |
733 | return -EIO; | |
734 | ||
735 | kfree(fp); | |
736 | ||
737 | return 0; | |
738 | } | |
739 | ||
740 | /** | |
741 | * gfs2_fsync - sync the dirty data for a file (across the cluster) | |
742 | * @file: the file that points to the dentry (we ignore this) | |
743 | * @dentry: the dentry that points to the inode to sync | |
744 | * | |
745 | * Returns: errno | |
746 | */ | |
747 | ||
748 | static int gfs2_fsync(struct file *file, struct dentry *dentry, int datasync) | |
749 | { | |
750 | struct gfs2_inode *ip = get_v2ip(dentry->d_inode); | |
751 | ||
b3b94faa DT |
752 | gfs2_log_flush_glock(ip->i_gl); |
753 | ||
754 | return 0; | |
755 | } | |
756 | ||
757 | /** | |
758 | * gfs2_lock - acquire/release a posix lock on a file | |
759 | * @file: the file pointer | |
760 | * @cmd: either modify or retrieve lock state, possibly wait | |
761 | * @fl: type and range of lock | |
762 | * | |
763 | * Returns: errno | |
764 | */ | |
765 | ||
766 | static int gfs2_lock(struct file *file, int cmd, struct file_lock *fl) | |
767 | { | |
768 | struct gfs2_inode *ip = get_v2ip(file->f_mapping->host); | |
769 | struct gfs2_sbd *sdp = ip->i_sbd; | |
770 | struct lm_lockname name = | |
771 | { .ln_number = ip->i_num.no_addr, | |
772 | .ln_type = LM_TYPE_PLOCK }; | |
773 | ||
b3b94faa DT |
774 | if (!(fl->fl_flags & FL_POSIX)) |
775 | return -ENOLCK; | |
776 | if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID) | |
777 | return -ENOLCK; | |
778 | ||
779 | if (sdp->sd_args.ar_localflocks) { | |
780 | if (IS_GETLK(cmd)) { | |
781 | struct file_lock *tmp; | |
782 | lock_kernel(); | |
783 | tmp = posix_test_lock(file, fl); | |
784 | fl->fl_type = F_UNLCK; | |
785 | if (tmp) | |
786 | memcpy(fl, tmp, sizeof(struct file_lock)); | |
787 | unlock_kernel(); | |
788 | return 0; | |
789 | } else { | |
790 | int error; | |
791 | lock_kernel(); | |
792 | error = posix_lock_file_wait(file, fl); | |
793 | unlock_kernel(); | |
794 | return error; | |
795 | } | |
796 | } | |
797 | ||
798 | if (IS_GETLK(cmd)) | |
799 | return gfs2_lm_plock_get(sdp, &name, file, fl); | |
800 | else if (fl->fl_type == F_UNLCK) | |
801 | return gfs2_lm_punlock(sdp, &name, file, fl); | |
802 | else | |
803 | return gfs2_lm_plock(sdp, &name, file, cmd, fl); | |
804 | } | |
805 | ||
806 | /** | |
807 | * gfs2_sendfile - Send bytes to a file or socket | |
808 | * @in_file: The file to read from | |
809 | * @out_file: The file to write to | |
810 | * @count: The amount of data | |
811 | * @offset: The beginning file offset | |
812 | * | |
813 | * Outputs: offset - updated according to number of bytes read | |
814 | * | |
815 | * Returns: The number of bytes sent, errno on failure | |
816 | */ | |
817 | ||
818 | static ssize_t gfs2_sendfile(struct file *in_file, loff_t *offset, size_t count, | |
819 | read_actor_t actor, void *target) | |
820 | { | |
821 | struct gfs2_inode *ip = get_v2ip(in_file->f_mapping->host); | |
18ec7d5c | 822 | return generic_file_sendfile(in_file, offset, count, actor, target); |
b3b94faa DT |
823 | } |
824 | ||
825 | static int do_flock(struct file *file, int cmd, struct file_lock *fl) | |
826 | { | |
827 | struct gfs2_file *fp = get_v2fp(file); | |
828 | struct gfs2_holder *fl_gh = &fp->f_fl_gh; | |
829 | struct gfs2_inode *ip = fp->f_inode; | |
830 | struct gfs2_glock *gl; | |
831 | unsigned int state; | |
832 | int flags; | |
833 | int error = 0; | |
834 | ||
835 | state = (fl->fl_type == F_WRLCK) ? LM_ST_EXCLUSIVE : LM_ST_SHARED; | |
836 | flags = ((IS_SETLKW(cmd)) ? 0 : LM_FLAG_TRY) | GL_EXACT | GL_NOCACHE; | |
837 | ||
f55ab26a | 838 | mutex_lock(&fp->f_fl_mutex); |
b3b94faa DT |
839 | |
840 | gl = fl_gh->gh_gl; | |
841 | if (gl) { | |
842 | if (fl_gh->gh_state == state) | |
843 | goto out; | |
844 | gfs2_glock_hold(gl); | |
845 | flock_lock_file_wait(file, | |
846 | &(struct file_lock){.fl_type = F_UNLCK}); | |
847 | gfs2_glock_dq_uninit(fl_gh); | |
848 | } else { | |
849 | error = gfs2_glock_get(ip->i_sbd, | |
850 | ip->i_num.no_addr, &gfs2_flock_glops, | |
851 | CREATE, &gl); | |
852 | if (error) | |
853 | goto out; | |
854 | } | |
855 | ||
856 | gfs2_holder_init(gl, state, flags, fl_gh); | |
857 | gfs2_glock_put(gl); | |
858 | ||
859 | error = gfs2_glock_nq(fl_gh); | |
860 | if (error) { | |
861 | gfs2_holder_uninit(fl_gh); | |
862 | if (error == GLR_TRYFAILED) | |
863 | error = -EAGAIN; | |
864 | } else { | |
865 | error = flock_lock_file_wait(file, fl); | |
866 | gfs2_assert_warn(ip->i_sbd, !error); | |
867 | } | |
868 | ||
869 | out: | |
f55ab26a | 870 | mutex_unlock(&fp->f_fl_mutex); |
b3b94faa DT |
871 | |
872 | return error; | |
873 | } | |
874 | ||
875 | static void do_unflock(struct file *file, struct file_lock *fl) | |
876 | { | |
877 | struct gfs2_file *fp = get_v2fp(file); | |
878 | struct gfs2_holder *fl_gh = &fp->f_fl_gh; | |
879 | ||
f55ab26a | 880 | mutex_lock(&fp->f_fl_mutex); |
b3b94faa DT |
881 | flock_lock_file_wait(file, fl); |
882 | if (fl_gh->gh_gl) | |
883 | gfs2_glock_dq_uninit(fl_gh); | |
f55ab26a | 884 | mutex_unlock(&fp->f_fl_mutex); |
b3b94faa DT |
885 | } |
886 | ||
887 | /** | |
888 | * gfs2_flock - acquire/release a flock lock on a file | |
889 | * @file: the file pointer | |
890 | * @cmd: either modify or retrieve lock state, possibly wait | |
891 | * @fl: type and range of lock | |
892 | * | |
893 | * Returns: errno | |
894 | */ | |
895 | ||
896 | static int gfs2_flock(struct file *file, int cmd, struct file_lock *fl) | |
897 | { | |
898 | struct gfs2_inode *ip = get_v2ip(file->f_mapping->host); | |
899 | struct gfs2_sbd *sdp = ip->i_sbd; | |
900 | ||
b3b94faa DT |
901 | if (!(fl->fl_flags & FL_FLOCK)) |
902 | return -ENOLCK; | |
903 | if ((ip->i_di.di_mode & (S_ISGID | S_IXGRP)) == S_ISGID) | |
904 | return -ENOLCK; | |
905 | ||
906 | if (sdp->sd_args.ar_localflocks) | |
907 | return flock_lock_file_wait(file, fl); | |
908 | ||
909 | if (fl->fl_type == F_UNLCK) { | |
910 | do_unflock(file, fl); | |
911 | return 0; | |
912 | } else | |
913 | return do_flock(file, cmd, fl); | |
914 | } | |
915 | ||
916 | struct file_operations gfs2_file_fops = { | |
917 | .llseek = gfs2_llseek, | |
918 | .read = gfs2_read, | |
18ec7d5c SW |
919 | .readv = gfs2_file_readv, |
920 | .aio_read = gfs2_file_aio_read, | |
921 | .write = generic_file_write, | |
922 | .writev = generic_file_writev, | |
923 | .aio_write = generic_file_aio_write, | |
b3b94faa DT |
924 | .ioctl = gfs2_ioctl, |
925 | .mmap = gfs2_mmap, | |
926 | .open = gfs2_open, | |
927 | .release = gfs2_close, | |
928 | .fsync = gfs2_fsync, | |
929 | .lock = gfs2_lock, | |
930 | .sendfile = gfs2_sendfile, | |
931 | .flock = gfs2_flock, | |
932 | }; | |
933 | ||
934 | struct file_operations gfs2_dir_fops = { | |
935 | .readdir = gfs2_readdir, | |
936 | .ioctl = gfs2_ioctl, | |
937 | .open = gfs2_open, | |
938 | .release = gfs2_close, | |
939 | .fsync = gfs2_fsync, | |
940 | .lock = gfs2_lock, | |
941 | .flock = gfs2_flock, | |
942 | }; | |
943 |