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