diff options
Diffstat (limited to 'fs/ocfs2/file.c')
-rw-r--r-- | fs/ocfs2/file.c | 1238 |
1 files changed, 1238 insertions, 0 deletions
diff --git a/fs/ocfs2/file.c b/fs/ocfs2/file.c new file mode 100644 index 000000000000..eaf33caa0a1f --- /dev/null +++ b/fs/ocfs2/file.c | |||
@@ -0,0 +1,1238 @@ | |||
1 | /* -*- mode: c; c-basic-offset: 8; -*- | ||
2 | * vim: noexpandtab sw=8 ts=8 sts=0: | ||
3 | * | ||
4 | * file.c | ||
5 | * | ||
6 | * File open, close, extend, truncate | ||
7 | * | ||
8 | * Copyright (C) 2002, 2004 Oracle. All rights reserved. | ||
9 | * | ||
10 | * This program is free software; you can redistribute it and/or | ||
11 | * modify it under the terms of the GNU General Public | ||
12 | * License as published by the Free Software Foundation; either | ||
13 | * version 2 of the License, or (at your option) any later version. | ||
14 | * | ||
15 | * This program is distributed in the hope that it will be useful, | ||
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
18 | * General Public License for more details. | ||
19 | * | ||
20 | * You should have received a copy of the GNU General Public | ||
21 | * License along with this program; if not, write to the | ||
22 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, | ||
23 | * Boston, MA 021110-1307, USA. | ||
24 | */ | ||
25 | |||
26 | #include <linux/capability.h> | ||
27 | #include <linux/fs.h> | ||
28 | #include <linux/types.h> | ||
29 | #include <linux/slab.h> | ||
30 | #include <linux/highmem.h> | ||
31 | #include <linux/pagemap.h> | ||
32 | #include <linux/uio.h> | ||
33 | |||
34 | #define MLOG_MASK_PREFIX ML_INODE | ||
35 | #include <cluster/masklog.h> | ||
36 | |||
37 | #include "ocfs2.h" | ||
38 | |||
39 | #include "alloc.h" | ||
40 | #include "aops.h" | ||
41 | #include "dir.h" | ||
42 | #include "dlmglue.h" | ||
43 | #include "extent_map.h" | ||
44 | #include "file.h" | ||
45 | #include "sysfile.h" | ||
46 | #include "inode.h" | ||
47 | #include "journal.h" | ||
48 | #include "mmap.h" | ||
49 | #include "suballoc.h" | ||
50 | #include "super.h" | ||
51 | |||
52 | #include "buffer_head_io.h" | ||
53 | |||
54 | static int ocfs2_sync_inode(struct inode *inode) | ||
55 | { | ||
56 | filemap_fdatawrite(inode->i_mapping); | ||
57 | return sync_mapping_buffers(inode->i_mapping); | ||
58 | } | ||
59 | |||
60 | static int ocfs2_file_open(struct inode *inode, struct file *file) | ||
61 | { | ||
62 | int status; | ||
63 | int mode = file->f_flags; | ||
64 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | ||
65 | |||
66 | mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file, | ||
67 | file->f_dentry->d_name.len, file->f_dentry->d_name.name); | ||
68 | |||
69 | spin_lock(&oi->ip_lock); | ||
70 | |||
71 | /* Check that the inode hasn't been wiped from disk by another | ||
72 | * node. If it hasn't then we're safe as long as we hold the | ||
73 | * spin lock until our increment of open count. */ | ||
74 | if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_DELETED) { | ||
75 | spin_unlock(&oi->ip_lock); | ||
76 | |||
77 | status = -ENOENT; | ||
78 | goto leave; | ||
79 | } | ||
80 | |||
81 | if (mode & O_DIRECT) | ||
82 | oi->ip_flags |= OCFS2_INODE_OPEN_DIRECT; | ||
83 | |||
84 | oi->ip_open_count++; | ||
85 | spin_unlock(&oi->ip_lock); | ||
86 | status = 0; | ||
87 | leave: | ||
88 | mlog_exit(status); | ||
89 | return status; | ||
90 | } | ||
91 | |||
92 | static int ocfs2_file_release(struct inode *inode, struct file *file) | ||
93 | { | ||
94 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | ||
95 | |||
96 | mlog_entry("(0x%p, 0x%p, '%.*s')\n", inode, file, | ||
97 | file->f_dentry->d_name.len, | ||
98 | file->f_dentry->d_name.name); | ||
99 | |||
100 | spin_lock(&oi->ip_lock); | ||
101 | if (!--oi->ip_open_count) | ||
102 | oi->ip_flags &= ~OCFS2_INODE_OPEN_DIRECT; | ||
103 | spin_unlock(&oi->ip_lock); | ||
104 | |||
105 | mlog_exit(0); | ||
106 | |||
107 | return 0; | ||
108 | } | ||
109 | |||
110 | static int ocfs2_sync_file(struct file *file, | ||
111 | struct dentry *dentry, | ||
112 | int datasync) | ||
113 | { | ||
114 | int err = 0; | ||
115 | journal_t *journal; | ||
116 | struct inode *inode = dentry->d_inode; | ||
117 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | ||
118 | |||
119 | mlog_entry("(0x%p, 0x%p, %d, '%.*s')\n", file, dentry, datasync, | ||
120 | dentry->d_name.len, dentry->d_name.name); | ||
121 | |||
122 | err = ocfs2_sync_inode(dentry->d_inode); | ||
123 | if (err) | ||
124 | goto bail; | ||
125 | |||
126 | journal = osb->journal->j_journal; | ||
127 | err = journal_force_commit(journal); | ||
128 | |||
129 | bail: | ||
130 | mlog_exit(err); | ||
131 | |||
132 | return (err < 0) ? -EIO : 0; | ||
133 | } | ||
134 | |||
135 | int ocfs2_set_inode_size(struct ocfs2_journal_handle *handle, | ||
136 | struct inode *inode, | ||
137 | struct buffer_head *fe_bh, | ||
138 | u64 new_i_size) | ||
139 | { | ||
140 | int status; | ||
141 | |||
142 | mlog_entry_void(); | ||
143 | i_size_write(inode, new_i_size); | ||
144 | inode->i_blocks = ocfs2_align_bytes_to_sectors(new_i_size); | ||
145 | inode->i_ctime = inode->i_mtime = CURRENT_TIME; | ||
146 | |||
147 | status = ocfs2_mark_inode_dirty(handle, inode, fe_bh); | ||
148 | if (status < 0) { | ||
149 | mlog_errno(status); | ||
150 | goto bail; | ||
151 | } | ||
152 | |||
153 | bail: | ||
154 | mlog_exit(status); | ||
155 | return status; | ||
156 | } | ||
157 | |||
158 | static int ocfs2_simple_size_update(struct inode *inode, | ||
159 | struct buffer_head *di_bh, | ||
160 | u64 new_i_size) | ||
161 | { | ||
162 | int ret; | ||
163 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | ||
164 | struct ocfs2_journal_handle *handle = NULL; | ||
165 | |||
166 | handle = ocfs2_start_trans(osb, NULL, | ||
167 | OCFS2_INODE_UPDATE_CREDITS); | ||
168 | if (handle == NULL) { | ||
169 | ret = -ENOMEM; | ||
170 | mlog_errno(ret); | ||
171 | goto out; | ||
172 | } | ||
173 | |||
174 | ret = ocfs2_set_inode_size(handle, inode, di_bh, | ||
175 | new_i_size); | ||
176 | if (ret < 0) | ||
177 | mlog_errno(ret); | ||
178 | |||
179 | ocfs2_commit_trans(handle); | ||
180 | out: | ||
181 | return ret; | ||
182 | } | ||
183 | |||
184 | static int ocfs2_orphan_for_truncate(struct ocfs2_super *osb, | ||
185 | struct inode *inode, | ||
186 | struct buffer_head *fe_bh, | ||
187 | u64 new_i_size) | ||
188 | { | ||
189 | int status; | ||
190 | struct ocfs2_journal_handle *handle; | ||
191 | |||
192 | mlog_entry_void(); | ||
193 | |||
194 | /* TODO: This needs to actually orphan the inode in this | ||
195 | * transaction. */ | ||
196 | |||
197 | handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS); | ||
198 | if (IS_ERR(handle)) { | ||
199 | status = PTR_ERR(handle); | ||
200 | mlog_errno(status); | ||
201 | goto out; | ||
202 | } | ||
203 | |||
204 | status = ocfs2_set_inode_size(handle, inode, fe_bh, new_i_size); | ||
205 | if (status < 0) | ||
206 | mlog_errno(status); | ||
207 | |||
208 | ocfs2_commit_trans(handle); | ||
209 | out: | ||
210 | mlog_exit(status); | ||
211 | return status; | ||
212 | } | ||
213 | |||
214 | static int ocfs2_truncate_file(struct inode *inode, | ||
215 | struct buffer_head *di_bh, | ||
216 | u64 new_i_size) | ||
217 | { | ||
218 | int status = 0; | ||
219 | struct ocfs2_dinode *fe = NULL; | ||
220 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | ||
221 | struct ocfs2_truncate_context *tc = NULL; | ||
222 | |||
223 | mlog_entry("(inode = %"MLFu64", new_i_size = %"MLFu64"\n", | ||
224 | OCFS2_I(inode)->ip_blkno, new_i_size); | ||
225 | |||
226 | truncate_inode_pages(inode->i_mapping, new_i_size); | ||
227 | |||
228 | fe = (struct ocfs2_dinode *) di_bh->b_data; | ||
229 | if (!OCFS2_IS_VALID_DINODE(fe)) { | ||
230 | OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe); | ||
231 | status = -EIO; | ||
232 | goto bail; | ||
233 | } | ||
234 | |||
235 | mlog_bug_on_msg(le64_to_cpu(fe->i_size) != i_size_read(inode), | ||
236 | "Inode %"MLFu64", inode i_size = %lld != di " | ||
237 | "i_size = %"MLFu64", i_flags = 0x%x\n", | ||
238 | OCFS2_I(inode)->ip_blkno, | ||
239 | i_size_read(inode), | ||
240 | le64_to_cpu(fe->i_size), le32_to_cpu(fe->i_flags)); | ||
241 | |||
242 | if (new_i_size > le64_to_cpu(fe->i_size)) { | ||
243 | mlog(0, "asked to truncate file with size (%"MLFu64") " | ||
244 | "to size (%"MLFu64")!\n", | ||
245 | le64_to_cpu(fe->i_size), new_i_size); | ||
246 | status = -EINVAL; | ||
247 | mlog_errno(status); | ||
248 | goto bail; | ||
249 | } | ||
250 | |||
251 | mlog(0, "inode %"MLFu64", i_size = %"MLFu64", new_i_size = %"MLFu64"\n", | ||
252 | le64_to_cpu(fe->i_blkno), le64_to_cpu(fe->i_size), new_i_size); | ||
253 | |||
254 | /* lets handle the simple truncate cases before doing any more | ||
255 | * cluster locking. */ | ||
256 | if (new_i_size == le64_to_cpu(fe->i_size)) | ||
257 | goto bail; | ||
258 | |||
259 | if (le32_to_cpu(fe->i_clusters) == | ||
260 | ocfs2_clusters_for_bytes(osb->sb, new_i_size)) { | ||
261 | mlog(0, "fe->i_clusters = %u, so we do a simple truncate\n", | ||
262 | fe->i_clusters); | ||
263 | /* No allocation change is required, so lets fast path | ||
264 | * this truncate. */ | ||
265 | status = ocfs2_simple_size_update(inode, di_bh, new_i_size); | ||
266 | if (status < 0) | ||
267 | mlog_errno(status); | ||
268 | goto bail; | ||
269 | } | ||
270 | |||
271 | /* This forces other nodes to sync and drop their pages */ | ||
272 | status = ocfs2_data_lock(inode, 1); | ||
273 | if (status < 0) { | ||
274 | mlog_errno(status); | ||
275 | goto bail; | ||
276 | } | ||
277 | ocfs2_data_unlock(inode, 1); | ||
278 | |||
279 | /* alright, we're going to need to do a full blown alloc size | ||
280 | * change. Orphan the inode so that recovery can complete the | ||
281 | * truncate if necessary. This does the task of marking | ||
282 | * i_size. */ | ||
283 | status = ocfs2_orphan_for_truncate(osb, inode, di_bh, new_i_size); | ||
284 | if (status < 0) { | ||
285 | mlog_errno(status); | ||
286 | goto bail; | ||
287 | } | ||
288 | |||
289 | status = ocfs2_prepare_truncate(osb, inode, di_bh, &tc); | ||
290 | if (status < 0) { | ||
291 | mlog_errno(status); | ||
292 | goto bail; | ||
293 | } | ||
294 | |||
295 | status = ocfs2_commit_truncate(osb, inode, di_bh, tc); | ||
296 | if (status < 0) { | ||
297 | mlog_errno(status); | ||
298 | goto bail; | ||
299 | } | ||
300 | |||
301 | /* TODO: orphan dir cleanup here. */ | ||
302 | bail: | ||
303 | |||
304 | mlog_exit(status); | ||
305 | return status; | ||
306 | } | ||
307 | |||
308 | /* | ||
309 | * extend allocation only here. | ||
310 | * we'll update all the disk stuff, and oip->alloc_size | ||
311 | * | ||
312 | * expect stuff to be locked, a transaction started and enough data / | ||
313 | * metadata reservations in the contexts. | ||
314 | * | ||
315 | * Will return -EAGAIN, and a reason if a restart is needed. | ||
316 | * If passed in, *reason will always be set, even in error. | ||
317 | */ | ||
318 | int ocfs2_do_extend_allocation(struct ocfs2_super *osb, | ||
319 | struct inode *inode, | ||
320 | u32 clusters_to_add, | ||
321 | struct buffer_head *fe_bh, | ||
322 | struct ocfs2_journal_handle *handle, | ||
323 | struct ocfs2_alloc_context *data_ac, | ||
324 | struct ocfs2_alloc_context *meta_ac, | ||
325 | enum ocfs2_alloc_restarted *reason_ret) | ||
326 | { | ||
327 | int status = 0; | ||
328 | int free_extents; | ||
329 | struct ocfs2_dinode *fe = (struct ocfs2_dinode *) fe_bh->b_data; | ||
330 | enum ocfs2_alloc_restarted reason = RESTART_NONE; | ||
331 | u32 bit_off, num_bits; | ||
332 | u64 block; | ||
333 | |||
334 | BUG_ON(!clusters_to_add); | ||
335 | |||
336 | free_extents = ocfs2_num_free_extents(osb, inode, fe); | ||
337 | if (free_extents < 0) { | ||
338 | status = free_extents; | ||
339 | mlog_errno(status); | ||
340 | goto leave; | ||
341 | } | ||
342 | |||
343 | /* there are two cases which could cause us to EAGAIN in the | ||
344 | * we-need-more-metadata case: | ||
345 | * 1) we haven't reserved *any* | ||
346 | * 2) we are so fragmented, we've needed to add metadata too | ||
347 | * many times. */ | ||
348 | if (!free_extents && !meta_ac) { | ||
349 | mlog(0, "we haven't reserved any metadata!\n"); | ||
350 | status = -EAGAIN; | ||
351 | reason = RESTART_META; | ||
352 | goto leave; | ||
353 | } else if ((!free_extents) | ||
354 | && (ocfs2_alloc_context_bits_left(meta_ac) | ||
355 | < ocfs2_extend_meta_needed(fe))) { | ||
356 | mlog(0, "filesystem is really fragmented...\n"); | ||
357 | status = -EAGAIN; | ||
358 | reason = RESTART_META; | ||
359 | goto leave; | ||
360 | } | ||
361 | |||
362 | status = ocfs2_claim_clusters(osb, handle, data_ac, 1, | ||
363 | &bit_off, &num_bits); | ||
364 | if (status < 0) { | ||
365 | if (status != -ENOSPC) | ||
366 | mlog_errno(status); | ||
367 | goto leave; | ||
368 | } | ||
369 | |||
370 | BUG_ON(num_bits > clusters_to_add); | ||
371 | |||
372 | /* reserve our write early -- insert_extent may update the inode */ | ||
373 | status = ocfs2_journal_access(handle, inode, fe_bh, | ||
374 | OCFS2_JOURNAL_ACCESS_WRITE); | ||
375 | if (status < 0) { | ||
376 | mlog_errno(status); | ||
377 | goto leave; | ||
378 | } | ||
379 | |||
380 | block = ocfs2_clusters_to_blocks(osb->sb, bit_off); | ||
381 | mlog(0, "Allocating %u clusters at block %u for inode %"MLFu64"\n", | ||
382 | num_bits, bit_off, OCFS2_I(inode)->ip_blkno); | ||
383 | status = ocfs2_insert_extent(osb, handle, inode, fe_bh, block, | ||
384 | num_bits, meta_ac); | ||
385 | if (status < 0) { | ||
386 | mlog_errno(status); | ||
387 | goto leave; | ||
388 | } | ||
389 | |||
390 | le32_add_cpu(&fe->i_clusters, num_bits); | ||
391 | spin_lock(&OCFS2_I(inode)->ip_lock); | ||
392 | OCFS2_I(inode)->ip_clusters = le32_to_cpu(fe->i_clusters); | ||
393 | spin_unlock(&OCFS2_I(inode)->ip_lock); | ||
394 | |||
395 | status = ocfs2_journal_dirty(handle, fe_bh); | ||
396 | if (status < 0) { | ||
397 | mlog_errno(status); | ||
398 | goto leave; | ||
399 | } | ||
400 | |||
401 | clusters_to_add -= num_bits; | ||
402 | |||
403 | if (clusters_to_add) { | ||
404 | mlog(0, "need to alloc once more, clusters = %u, wanted = " | ||
405 | "%u\n", fe->i_clusters, clusters_to_add); | ||
406 | status = -EAGAIN; | ||
407 | reason = RESTART_TRANS; | ||
408 | } | ||
409 | |||
410 | leave: | ||
411 | mlog_exit(status); | ||
412 | if (reason_ret) | ||
413 | *reason_ret = reason; | ||
414 | return status; | ||
415 | } | ||
416 | |||
417 | static int ocfs2_extend_allocation(struct inode *inode, | ||
418 | u32 clusters_to_add) | ||
419 | { | ||
420 | int status = 0; | ||
421 | int restart_func = 0; | ||
422 | int drop_alloc_sem = 0; | ||
423 | int credits, num_free_extents; | ||
424 | u32 prev_clusters; | ||
425 | struct buffer_head *bh = NULL; | ||
426 | struct ocfs2_dinode *fe = NULL; | ||
427 | struct ocfs2_journal_handle *handle = NULL; | ||
428 | struct ocfs2_alloc_context *data_ac = NULL; | ||
429 | struct ocfs2_alloc_context *meta_ac = NULL; | ||
430 | enum ocfs2_alloc_restarted why; | ||
431 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | ||
432 | |||
433 | mlog_entry("(clusters_to_add = %u)\n", clusters_to_add); | ||
434 | |||
435 | status = ocfs2_read_block(osb, OCFS2_I(inode)->ip_blkno, &bh, | ||
436 | OCFS2_BH_CACHED, inode); | ||
437 | if (status < 0) { | ||
438 | mlog_errno(status); | ||
439 | goto leave; | ||
440 | } | ||
441 | |||
442 | fe = (struct ocfs2_dinode *) bh->b_data; | ||
443 | if (!OCFS2_IS_VALID_DINODE(fe)) { | ||
444 | OCFS2_RO_ON_INVALID_DINODE(inode->i_sb, fe); | ||
445 | status = -EIO; | ||
446 | goto leave; | ||
447 | } | ||
448 | |||
449 | restart_all: | ||
450 | BUG_ON(le32_to_cpu(fe->i_clusters) != OCFS2_I(inode)->ip_clusters); | ||
451 | |||
452 | mlog(0, "extend inode %"MLFu64", i_size = %lld, fe->i_clusters = %u, " | ||
453 | "clusters_to_add = %u\n", | ||
454 | OCFS2_I(inode)->ip_blkno, i_size_read(inode), | ||
455 | fe->i_clusters, clusters_to_add); | ||
456 | |||
457 | handle = ocfs2_alloc_handle(osb); | ||
458 | if (handle == NULL) { | ||
459 | status = -ENOMEM; | ||
460 | mlog_errno(status); | ||
461 | goto leave; | ||
462 | } | ||
463 | |||
464 | num_free_extents = ocfs2_num_free_extents(osb, | ||
465 | inode, | ||
466 | fe); | ||
467 | if (num_free_extents < 0) { | ||
468 | status = num_free_extents; | ||
469 | mlog_errno(status); | ||
470 | goto leave; | ||
471 | } | ||
472 | |||
473 | if (!num_free_extents) { | ||
474 | status = ocfs2_reserve_new_metadata(osb, | ||
475 | handle, | ||
476 | fe, | ||
477 | &meta_ac); | ||
478 | if (status < 0) { | ||
479 | if (status != -ENOSPC) | ||
480 | mlog_errno(status); | ||
481 | goto leave; | ||
482 | } | ||
483 | } | ||
484 | |||
485 | status = ocfs2_reserve_clusters(osb, | ||
486 | handle, | ||
487 | clusters_to_add, | ||
488 | &data_ac); | ||
489 | if (status < 0) { | ||
490 | if (status != -ENOSPC) | ||
491 | mlog_errno(status); | ||
492 | goto leave; | ||
493 | } | ||
494 | |||
495 | /* blocks peope in read/write from reading our allocation | ||
496 | * until we're done changing it. We depend on i_mutex to block | ||
497 | * other extend/truncate calls while we're here. Ordering wrt | ||
498 | * start_trans is important here -- always do it before! */ | ||
499 | down_write(&OCFS2_I(inode)->ip_alloc_sem); | ||
500 | drop_alloc_sem = 1; | ||
501 | |||
502 | credits = ocfs2_calc_extend_credits(osb->sb, fe, clusters_to_add); | ||
503 | handle = ocfs2_start_trans(osb, handle, credits); | ||
504 | if (IS_ERR(handle)) { | ||
505 | status = PTR_ERR(handle); | ||
506 | handle = NULL; | ||
507 | mlog_errno(status); | ||
508 | goto leave; | ||
509 | } | ||
510 | |||
511 | restarted_transaction: | ||
512 | /* reserve a write to the file entry early on - that we if we | ||
513 | * run out of credits in the allocation path, we can still | ||
514 | * update i_size. */ | ||
515 | status = ocfs2_journal_access(handle, inode, bh, | ||
516 | OCFS2_JOURNAL_ACCESS_WRITE); | ||
517 | if (status < 0) { | ||
518 | mlog_errno(status); | ||
519 | goto leave; | ||
520 | } | ||
521 | |||
522 | prev_clusters = OCFS2_I(inode)->ip_clusters; | ||
523 | |||
524 | status = ocfs2_do_extend_allocation(osb, | ||
525 | inode, | ||
526 | clusters_to_add, | ||
527 | bh, | ||
528 | handle, | ||
529 | data_ac, | ||
530 | meta_ac, | ||
531 | &why); | ||
532 | if ((status < 0) && (status != -EAGAIN)) { | ||
533 | if (status != -ENOSPC) | ||
534 | mlog_errno(status); | ||
535 | goto leave; | ||
536 | } | ||
537 | |||
538 | status = ocfs2_journal_dirty(handle, bh); | ||
539 | if (status < 0) { | ||
540 | mlog_errno(status); | ||
541 | goto leave; | ||
542 | } | ||
543 | |||
544 | spin_lock(&OCFS2_I(inode)->ip_lock); | ||
545 | clusters_to_add -= (OCFS2_I(inode)->ip_clusters - prev_clusters); | ||
546 | spin_unlock(&OCFS2_I(inode)->ip_lock); | ||
547 | |||
548 | if (why != RESTART_NONE && clusters_to_add) { | ||
549 | if (why == RESTART_META) { | ||
550 | mlog(0, "restarting function.\n"); | ||
551 | restart_func = 1; | ||
552 | } else { | ||
553 | BUG_ON(why != RESTART_TRANS); | ||
554 | |||
555 | mlog(0, "restarting transaction.\n"); | ||
556 | /* TODO: This can be more intelligent. */ | ||
557 | credits = ocfs2_calc_extend_credits(osb->sb, | ||
558 | fe, | ||
559 | clusters_to_add); | ||
560 | status = ocfs2_extend_trans(handle, credits); | ||
561 | if (status < 0) { | ||
562 | /* handle still has to be committed at | ||
563 | * this point. */ | ||
564 | status = -ENOMEM; | ||
565 | mlog_errno(status); | ||
566 | goto leave; | ||
567 | } | ||
568 | goto restarted_transaction; | ||
569 | } | ||
570 | } | ||
571 | |||
572 | mlog(0, "fe: i_clusters = %u, i_size=%"MLFu64"\n", | ||
573 | fe->i_clusters, fe->i_size); | ||
574 | mlog(0, "inode: ip_clusters=%u, i_size=%lld\n", | ||
575 | OCFS2_I(inode)->ip_clusters, i_size_read(inode)); | ||
576 | |||
577 | leave: | ||
578 | if (drop_alloc_sem) { | ||
579 | up_write(&OCFS2_I(inode)->ip_alloc_sem); | ||
580 | drop_alloc_sem = 0; | ||
581 | } | ||
582 | if (handle) { | ||
583 | ocfs2_commit_trans(handle); | ||
584 | handle = NULL; | ||
585 | } | ||
586 | if (data_ac) { | ||
587 | ocfs2_free_alloc_context(data_ac); | ||
588 | data_ac = NULL; | ||
589 | } | ||
590 | if (meta_ac) { | ||
591 | ocfs2_free_alloc_context(meta_ac); | ||
592 | meta_ac = NULL; | ||
593 | } | ||
594 | if ((!status) && restart_func) { | ||
595 | restart_func = 0; | ||
596 | goto restart_all; | ||
597 | } | ||
598 | if (bh) { | ||
599 | brelse(bh); | ||
600 | bh = NULL; | ||
601 | } | ||
602 | |||
603 | mlog_exit(status); | ||
604 | return status; | ||
605 | } | ||
606 | |||
607 | /* Some parts of this taken from generic_cont_expand, which turned out | ||
608 | * to be too fragile to do exactly what we need without us having to | ||
609 | * worry about recursive locking in ->commit_write(). */ | ||
610 | static int ocfs2_write_zero_page(struct inode *inode, | ||
611 | u64 size) | ||
612 | { | ||
613 | struct address_space *mapping = inode->i_mapping; | ||
614 | struct page *page; | ||
615 | unsigned long index; | ||
616 | unsigned int offset; | ||
617 | struct ocfs2_journal_handle *handle = NULL; | ||
618 | int ret; | ||
619 | |||
620 | offset = (size & (PAGE_CACHE_SIZE-1)); /* Within page */ | ||
621 | /* ugh. in prepare/commit_write, if from==to==start of block, we | ||
622 | ** skip the prepare. make sure we never send an offset for the start | ||
623 | ** of a block | ||
624 | */ | ||
625 | if ((offset & (inode->i_sb->s_blocksize - 1)) == 0) { | ||
626 | offset++; | ||
627 | } | ||
628 | index = size >> PAGE_CACHE_SHIFT; | ||
629 | |||
630 | page = grab_cache_page(mapping, index); | ||
631 | if (!page) { | ||
632 | ret = -ENOMEM; | ||
633 | mlog_errno(ret); | ||
634 | goto out; | ||
635 | } | ||
636 | |||
637 | ret = ocfs2_prepare_write(NULL, page, offset, offset); | ||
638 | if (ret < 0) { | ||
639 | mlog_errno(ret); | ||
640 | goto out_unlock; | ||
641 | } | ||
642 | |||
643 | if (ocfs2_should_order_data(inode)) { | ||
644 | handle = ocfs2_start_walk_page_trans(inode, page, offset, | ||
645 | offset); | ||
646 | if (IS_ERR(handle)) { | ||
647 | ret = PTR_ERR(handle); | ||
648 | handle = NULL; | ||
649 | goto out_unlock; | ||
650 | } | ||
651 | } | ||
652 | |||
653 | /* must not update i_size! */ | ||
654 | ret = block_commit_write(page, offset, offset); | ||
655 | if (ret < 0) | ||
656 | mlog_errno(ret); | ||
657 | else | ||
658 | ret = 0; | ||
659 | |||
660 | if (handle) | ||
661 | ocfs2_commit_trans(handle); | ||
662 | out_unlock: | ||
663 | unlock_page(page); | ||
664 | page_cache_release(page); | ||
665 | out: | ||
666 | return ret; | ||
667 | } | ||
668 | |||
669 | static int ocfs2_zero_extend(struct inode *inode, | ||
670 | u64 zero_to_size) | ||
671 | { | ||
672 | int ret = 0; | ||
673 | u64 start_off; | ||
674 | struct super_block *sb = inode->i_sb; | ||
675 | |||
676 | start_off = ocfs2_align_bytes_to_blocks(sb, i_size_read(inode)); | ||
677 | while (start_off < zero_to_size) { | ||
678 | ret = ocfs2_write_zero_page(inode, start_off); | ||
679 | if (ret < 0) { | ||
680 | mlog_errno(ret); | ||
681 | goto out; | ||
682 | } | ||
683 | |||
684 | start_off += sb->s_blocksize; | ||
685 | } | ||
686 | |||
687 | out: | ||
688 | return ret; | ||
689 | } | ||
690 | |||
691 | static int ocfs2_extend_file(struct inode *inode, | ||
692 | struct buffer_head *di_bh, | ||
693 | u64 new_i_size) | ||
694 | { | ||
695 | int ret = 0; | ||
696 | u32 clusters_to_add; | ||
697 | |||
698 | /* setattr sometimes calls us like this. */ | ||
699 | if (new_i_size == 0) | ||
700 | goto out; | ||
701 | |||
702 | if (i_size_read(inode) == new_i_size) | ||
703 | goto out; | ||
704 | BUG_ON(new_i_size < i_size_read(inode)); | ||
705 | |||
706 | clusters_to_add = ocfs2_clusters_for_bytes(inode->i_sb, new_i_size) - | ||
707 | OCFS2_I(inode)->ip_clusters; | ||
708 | |||
709 | if (clusters_to_add) { | ||
710 | ret = ocfs2_extend_allocation(inode, clusters_to_add); | ||
711 | if (ret < 0) { | ||
712 | mlog_errno(ret); | ||
713 | goto out; | ||
714 | } | ||
715 | |||
716 | ret = ocfs2_zero_extend(inode, new_i_size); | ||
717 | if (ret < 0) { | ||
718 | mlog_errno(ret); | ||
719 | goto out; | ||
720 | } | ||
721 | } | ||
722 | |||
723 | /* No allocation required, we just use this helper to | ||
724 | * do a trivial update of i_size. */ | ||
725 | ret = ocfs2_simple_size_update(inode, di_bh, new_i_size); | ||
726 | if (ret < 0) { | ||
727 | mlog_errno(ret); | ||
728 | goto out; | ||
729 | } | ||
730 | |||
731 | out: | ||
732 | return ret; | ||
733 | } | ||
734 | |||
735 | int ocfs2_setattr(struct dentry *dentry, struct iattr *attr) | ||
736 | { | ||
737 | int status = 0, size_change; | ||
738 | struct inode *inode = dentry->d_inode; | ||
739 | struct super_block *sb = inode->i_sb; | ||
740 | struct ocfs2_super *osb = OCFS2_SB(sb); | ||
741 | struct buffer_head *bh = NULL; | ||
742 | struct ocfs2_journal_handle *handle = NULL; | ||
743 | |||
744 | mlog_entry("(0x%p, '%.*s')\n", dentry, | ||
745 | dentry->d_name.len, dentry->d_name.name); | ||
746 | |||
747 | if (attr->ia_valid & ATTR_MODE) | ||
748 | mlog(0, "mode change: %d\n", attr->ia_mode); | ||
749 | if (attr->ia_valid & ATTR_UID) | ||
750 | mlog(0, "uid change: %d\n", attr->ia_uid); | ||
751 | if (attr->ia_valid & ATTR_GID) | ||
752 | mlog(0, "gid change: %d\n", attr->ia_gid); | ||
753 | if (attr->ia_valid & ATTR_SIZE) | ||
754 | mlog(0, "size change...\n"); | ||
755 | if (attr->ia_valid & (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME)) | ||
756 | mlog(0, "time change...\n"); | ||
757 | |||
758 | #define OCFS2_VALID_ATTRS (ATTR_ATIME | ATTR_MTIME | ATTR_CTIME | ATTR_SIZE \ | ||
759 | | ATTR_GID | ATTR_UID | ATTR_MODE) | ||
760 | if (!(attr->ia_valid & OCFS2_VALID_ATTRS)) { | ||
761 | mlog(0, "can't handle attrs: 0x%x\n", attr->ia_valid); | ||
762 | return 0; | ||
763 | } | ||
764 | |||
765 | status = inode_change_ok(inode, attr); | ||
766 | if (status) | ||
767 | return status; | ||
768 | |||
769 | size_change = S_ISREG(inode->i_mode) && attr->ia_valid & ATTR_SIZE; | ||
770 | if (size_change) { | ||
771 | status = ocfs2_rw_lock(inode, 1); | ||
772 | if (status < 0) { | ||
773 | mlog_errno(status); | ||
774 | goto bail; | ||
775 | } | ||
776 | } | ||
777 | |||
778 | status = ocfs2_meta_lock(inode, NULL, &bh, 1); | ||
779 | if (status < 0) { | ||
780 | if (status != -ENOENT) | ||
781 | mlog_errno(status); | ||
782 | goto bail_unlock_rw; | ||
783 | } | ||
784 | |||
785 | if (size_change && attr->ia_size != i_size_read(inode)) { | ||
786 | if (i_size_read(inode) > attr->ia_size) | ||
787 | status = ocfs2_truncate_file(inode, bh, attr->ia_size); | ||
788 | else | ||
789 | status = ocfs2_extend_file(inode, bh, attr->ia_size); | ||
790 | if (status < 0) { | ||
791 | if (status != -ENOSPC) | ||
792 | mlog_errno(status); | ||
793 | status = -ENOSPC; | ||
794 | goto bail_unlock; | ||
795 | } | ||
796 | } | ||
797 | |||
798 | handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS); | ||
799 | if (IS_ERR(handle)) { | ||
800 | status = PTR_ERR(handle); | ||
801 | mlog_errno(status); | ||
802 | goto bail_unlock; | ||
803 | } | ||
804 | |||
805 | status = inode_setattr(inode, attr); | ||
806 | if (status < 0) { | ||
807 | mlog_errno(status); | ||
808 | goto bail_commit; | ||
809 | } | ||
810 | |||
811 | status = ocfs2_mark_inode_dirty(handle, inode, bh); | ||
812 | if (status < 0) | ||
813 | mlog_errno(status); | ||
814 | |||
815 | bail_commit: | ||
816 | ocfs2_commit_trans(handle); | ||
817 | bail_unlock: | ||
818 | ocfs2_meta_unlock(inode, 1); | ||
819 | bail_unlock_rw: | ||
820 | if (size_change) | ||
821 | ocfs2_rw_unlock(inode, 1); | ||
822 | bail: | ||
823 | if (bh) | ||
824 | brelse(bh); | ||
825 | |||
826 | mlog_exit(status); | ||
827 | return status; | ||
828 | } | ||
829 | |||
830 | int ocfs2_getattr(struct vfsmount *mnt, | ||
831 | struct dentry *dentry, | ||
832 | struct kstat *stat) | ||
833 | { | ||
834 | struct inode *inode = dentry->d_inode; | ||
835 | struct super_block *sb = dentry->d_inode->i_sb; | ||
836 | struct ocfs2_super *osb = sb->s_fs_info; | ||
837 | int err; | ||
838 | |||
839 | mlog_entry_void(); | ||
840 | |||
841 | err = ocfs2_inode_revalidate(dentry); | ||
842 | if (err) { | ||
843 | if (err != -ENOENT) | ||
844 | mlog_errno(err); | ||
845 | goto bail; | ||
846 | } | ||
847 | |||
848 | generic_fillattr(inode, stat); | ||
849 | |||
850 | /* We set the blksize from the cluster size for performance */ | ||
851 | stat->blksize = osb->s_clustersize; | ||
852 | |||
853 | bail: | ||
854 | mlog_exit(err); | ||
855 | |||
856 | return err; | ||
857 | } | ||
858 | |||
859 | static int ocfs2_write_remove_suid(struct inode *inode) | ||
860 | { | ||
861 | int ret; | ||
862 | struct buffer_head *bh = NULL; | ||
863 | struct ocfs2_inode_info *oi = OCFS2_I(inode); | ||
864 | struct ocfs2_journal_handle *handle; | ||
865 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | ||
866 | struct ocfs2_dinode *di; | ||
867 | |||
868 | mlog_entry("(Inode %"MLFu64", mode 0%o)\n", oi->ip_blkno, | ||
869 | inode->i_mode); | ||
870 | |||
871 | handle = ocfs2_start_trans(osb, NULL, OCFS2_INODE_UPDATE_CREDITS); | ||
872 | if (handle == NULL) { | ||
873 | ret = -ENOMEM; | ||
874 | mlog_errno(ret); | ||
875 | goto out; | ||
876 | } | ||
877 | |||
878 | ret = ocfs2_read_block(osb, oi->ip_blkno, &bh, OCFS2_BH_CACHED, inode); | ||
879 | if (ret < 0) { | ||
880 | mlog_errno(ret); | ||
881 | goto out_trans; | ||
882 | } | ||
883 | |||
884 | ret = ocfs2_journal_access(handle, inode, bh, | ||
885 | OCFS2_JOURNAL_ACCESS_WRITE); | ||
886 | if (ret < 0) { | ||
887 | mlog_errno(ret); | ||
888 | goto out_bh; | ||
889 | } | ||
890 | |||
891 | inode->i_mode &= ~S_ISUID; | ||
892 | if ((inode->i_mode & S_ISGID) && (inode->i_mode & S_IXGRP)) | ||
893 | inode->i_mode &= ~S_ISGID; | ||
894 | |||
895 | di = (struct ocfs2_dinode *) bh->b_data; | ||
896 | di->i_mode = cpu_to_le16(inode->i_mode); | ||
897 | |||
898 | ret = ocfs2_journal_dirty(handle, bh); | ||
899 | if (ret < 0) | ||
900 | mlog_errno(ret); | ||
901 | out_bh: | ||
902 | brelse(bh); | ||
903 | out_trans: | ||
904 | ocfs2_commit_trans(handle); | ||
905 | out: | ||
906 | mlog_exit(ret); | ||
907 | return ret; | ||
908 | } | ||
909 | |||
910 | static inline int ocfs2_write_should_remove_suid(struct inode *inode) | ||
911 | { | ||
912 | mode_t mode = inode->i_mode; | ||
913 | |||
914 | if (!capable(CAP_FSETID)) { | ||
915 | if (unlikely(mode & S_ISUID)) | ||
916 | return 1; | ||
917 | |||
918 | if (unlikely((mode & S_ISGID) && (mode & S_IXGRP))) | ||
919 | return 1; | ||
920 | } | ||
921 | return 0; | ||
922 | } | ||
923 | |||
924 | static ssize_t ocfs2_file_aio_write(struct kiocb *iocb, | ||
925 | const char __user *buf, | ||
926 | size_t count, | ||
927 | loff_t pos) | ||
928 | { | ||
929 | struct iovec local_iov = { .iov_base = (void __user *)buf, | ||
930 | .iov_len = count }; | ||
931 | int ret, rw_level = -1, meta_level = -1, have_alloc_sem = 0; | ||
932 | u32 clusters; | ||
933 | struct file *filp = iocb->ki_filp; | ||
934 | struct inode *inode = filp->f_dentry->d_inode; | ||
935 | loff_t newsize, saved_pos; | ||
936 | #ifdef OCFS2_ORACORE_WORKAROUNDS | ||
937 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | ||
938 | #endif | ||
939 | |||
940 | mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", filp, buf, | ||
941 | (unsigned int)count, | ||
942 | filp->f_dentry->d_name.len, | ||
943 | filp->f_dentry->d_name.name); | ||
944 | |||
945 | /* happy write of zero bytes */ | ||
946 | if (count == 0) | ||
947 | return 0; | ||
948 | |||
949 | if (!inode) { | ||
950 | mlog(0, "bad inode\n"); | ||
951 | return -EIO; | ||
952 | } | ||
953 | |||
954 | #ifdef OCFS2_ORACORE_WORKAROUNDS | ||
955 | /* ugh, work around some applications which open everything O_DIRECT + | ||
956 | * O_APPEND and really don't mean to use O_DIRECT. */ | ||
957 | if (osb->s_mount_opt & OCFS2_MOUNT_COMPAT_OCFS && | ||
958 | (filp->f_flags & O_APPEND) && (filp->f_flags & O_DIRECT)) | ||
959 | filp->f_flags &= ~O_DIRECT; | ||
960 | #endif | ||
961 | |||
962 | mutex_lock(&inode->i_mutex); | ||
963 | /* to match setattr's i_mutex -> i_alloc_sem -> rw_lock ordering */ | ||
964 | if (filp->f_flags & O_DIRECT) { | ||
965 | have_alloc_sem = 1; | ||
966 | down_read(&inode->i_alloc_sem); | ||
967 | } | ||
968 | |||
969 | /* concurrent O_DIRECT writes are allowed */ | ||
970 | rw_level = (filp->f_flags & O_DIRECT) ? 0 : 1; | ||
971 | ret = ocfs2_rw_lock(inode, rw_level); | ||
972 | if (ret < 0) { | ||
973 | rw_level = -1; | ||
974 | mlog_errno(ret); | ||
975 | goto out; | ||
976 | } | ||
977 | |||
978 | /* | ||
979 | * We sample i_size under a read level meta lock to see if our write | ||
980 | * is extending the file, if it is we back off and get a write level | ||
981 | * meta lock. | ||
982 | */ | ||
983 | meta_level = (filp->f_flags & O_APPEND) ? 1 : 0; | ||
984 | for(;;) { | ||
985 | ret = ocfs2_meta_lock(inode, NULL, NULL, meta_level); | ||
986 | if (ret < 0) { | ||
987 | meta_level = -1; | ||
988 | mlog_errno(ret); | ||
989 | goto out; | ||
990 | } | ||
991 | |||
992 | /* Clear suid / sgid if necessary. We do this here | ||
993 | * instead of later in the write path because | ||
994 | * remove_suid() calls ->setattr without any hint that | ||
995 | * we may have already done our cluster locking. Since | ||
996 | * ocfs2_setattr() *must* take cluster locks to | ||
997 | * proceeed, this will lead us to recursively lock the | ||
998 | * inode. There's also the dinode i_size state which | ||
999 | * can be lost via setattr during extending writes (we | ||
1000 | * set inode->i_size at the end of a write. */ | ||
1001 | if (ocfs2_write_should_remove_suid(inode)) { | ||
1002 | if (meta_level == 0) { | ||
1003 | ocfs2_meta_unlock(inode, meta_level); | ||
1004 | meta_level = 1; | ||
1005 | continue; | ||
1006 | } | ||
1007 | |||
1008 | ret = ocfs2_write_remove_suid(inode); | ||
1009 | if (ret < 0) { | ||
1010 | mlog_errno(ret); | ||
1011 | goto out; | ||
1012 | } | ||
1013 | } | ||
1014 | |||
1015 | /* work on a copy of ppos until we're sure that we won't have | ||
1016 | * to recalculate it due to relocking. */ | ||
1017 | if (filp->f_flags & O_APPEND) { | ||
1018 | saved_pos = i_size_read(inode); | ||
1019 | mlog(0, "O_APPEND: inode->i_size=%llu\n", saved_pos); | ||
1020 | } else { | ||
1021 | saved_pos = iocb->ki_pos; | ||
1022 | } | ||
1023 | newsize = count + saved_pos; | ||
1024 | |||
1025 | mlog(0, "pos=%lld newsize=%"MLFu64" cursize=%lld\n", | ||
1026 | saved_pos, newsize, i_size_read(inode)); | ||
1027 | |||
1028 | /* No need for a higher level metadata lock if we're | ||
1029 | * never going past i_size. */ | ||
1030 | if (newsize <= i_size_read(inode)) | ||
1031 | break; | ||
1032 | |||
1033 | if (meta_level == 0) { | ||
1034 | ocfs2_meta_unlock(inode, meta_level); | ||
1035 | meta_level = 1; | ||
1036 | continue; | ||
1037 | } | ||
1038 | |||
1039 | spin_lock(&OCFS2_I(inode)->ip_lock); | ||
1040 | clusters = ocfs2_clusters_for_bytes(inode->i_sb, newsize) - | ||
1041 | OCFS2_I(inode)->ip_clusters; | ||
1042 | spin_unlock(&OCFS2_I(inode)->ip_lock); | ||
1043 | |||
1044 | mlog(0, "Writing at EOF, may need more allocation: " | ||
1045 | "i_size = %lld, newsize = %"MLFu64", need %u clusters\n", | ||
1046 | i_size_read(inode), newsize, clusters); | ||
1047 | |||
1048 | /* We only want to continue the rest of this loop if | ||
1049 | * our extend will actually require more | ||
1050 | * allocation. */ | ||
1051 | if (!clusters) | ||
1052 | break; | ||
1053 | |||
1054 | ret = ocfs2_extend_allocation(inode, clusters); | ||
1055 | if (ret < 0) { | ||
1056 | if (ret != -ENOSPC) | ||
1057 | mlog_errno(ret); | ||
1058 | goto out; | ||
1059 | } | ||
1060 | |||
1061 | /* Fill any holes which would've been created by this | ||
1062 | * write. If we're O_APPEND, this will wind up | ||
1063 | * (correctly) being a noop. */ | ||
1064 | ret = ocfs2_zero_extend(inode, (u64) newsize - count); | ||
1065 | if (ret < 0) { | ||
1066 | mlog_errno(ret); | ||
1067 | goto out; | ||
1068 | } | ||
1069 | break; | ||
1070 | } | ||
1071 | |||
1072 | /* ok, we're done with i_size and alloc work */ | ||
1073 | iocb->ki_pos = saved_pos; | ||
1074 | ocfs2_meta_unlock(inode, meta_level); | ||
1075 | meta_level = -1; | ||
1076 | |||
1077 | /* communicate with ocfs2_dio_end_io */ | ||
1078 | ocfs2_iocb_set_rw_locked(iocb); | ||
1079 | |||
1080 | #ifdef OCFS2_ORACORE_WORKAROUNDS | ||
1081 | if (osb->s_mount_opt & OCFS2_MOUNT_COMPAT_OCFS && | ||
1082 | filp->f_flags & O_DIRECT) { | ||
1083 | unsigned int saved_flags = filp->f_flags; | ||
1084 | int sector_size = 1 << osb->s_sectsize_bits; | ||
1085 | |||
1086 | if ((saved_pos & (sector_size - 1)) || | ||
1087 | (count & (sector_size - 1)) || | ||
1088 | ((unsigned long)buf & (sector_size - 1))) { | ||
1089 | filp->f_flags |= O_SYNC; | ||
1090 | filp->f_flags &= ~O_DIRECT; | ||
1091 | } | ||
1092 | |||
1093 | ret = generic_file_aio_write_nolock(iocb, &local_iov, 1, | ||
1094 | &iocb->ki_pos); | ||
1095 | |||
1096 | filp->f_flags = saved_flags; | ||
1097 | } else | ||
1098 | #endif | ||
1099 | ret = generic_file_aio_write_nolock(iocb, &local_iov, 1, | ||
1100 | &iocb->ki_pos); | ||
1101 | |||
1102 | /* buffered aio wouldn't have proper lock coverage today */ | ||
1103 | BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT)); | ||
1104 | |||
1105 | /* | ||
1106 | * deep in g_f_a_w_n()->ocfs2_direct_IO we pass in a ocfs2_dio_end_io | ||
1107 | * function pointer which is called when o_direct io completes so that | ||
1108 | * it can unlock our rw lock. (it's the clustered equivalent of | ||
1109 | * i_alloc_sem; protects truncate from racing with pending ios). | ||
1110 | * Unfortunately there are error cases which call end_io and others | ||
1111 | * that don't. so we don't have to unlock the rw_lock if either an | ||
1112 | * async dio is going to do it in the future or an end_io after an | ||
1113 | * error has already done it. | ||
1114 | */ | ||
1115 | if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) { | ||
1116 | rw_level = -1; | ||
1117 | have_alloc_sem = 0; | ||
1118 | } | ||
1119 | |||
1120 | out: | ||
1121 | if (meta_level != -1) | ||
1122 | ocfs2_meta_unlock(inode, meta_level); | ||
1123 | if (have_alloc_sem) | ||
1124 | up_read(&inode->i_alloc_sem); | ||
1125 | if (rw_level != -1) | ||
1126 | ocfs2_rw_unlock(inode, rw_level); | ||
1127 | mutex_unlock(&inode->i_mutex); | ||
1128 | |||
1129 | mlog_exit(ret); | ||
1130 | return ret; | ||
1131 | } | ||
1132 | |||
1133 | static ssize_t ocfs2_file_aio_read(struct kiocb *iocb, | ||
1134 | char __user *buf, | ||
1135 | size_t count, | ||
1136 | loff_t pos) | ||
1137 | { | ||
1138 | int ret = 0, rw_level = -1, have_alloc_sem = 0; | ||
1139 | struct file *filp = iocb->ki_filp; | ||
1140 | struct inode *inode = filp->f_dentry->d_inode; | ||
1141 | #ifdef OCFS2_ORACORE_WORKAROUNDS | ||
1142 | struct ocfs2_super *osb = OCFS2_SB(inode->i_sb); | ||
1143 | #endif | ||
1144 | |||
1145 | mlog_entry("(0x%p, 0x%p, %u, '%.*s')\n", filp, buf, | ||
1146 | (unsigned int)count, | ||
1147 | filp->f_dentry->d_name.len, | ||
1148 | filp->f_dentry->d_name.name); | ||
1149 | |||
1150 | if (!inode) { | ||
1151 | ret = -EINVAL; | ||
1152 | mlog_errno(ret); | ||
1153 | goto bail; | ||
1154 | } | ||
1155 | |||
1156 | #ifdef OCFS2_ORACORE_WORKAROUNDS | ||
1157 | if (osb->s_mount_opt & OCFS2_MOUNT_COMPAT_OCFS) { | ||
1158 | if (filp->f_flags & O_DIRECT) { | ||
1159 | int sector_size = 1 << osb->s_sectsize_bits; | ||
1160 | |||
1161 | if ((pos & (sector_size - 1)) || | ||
1162 | (count & (sector_size - 1)) || | ||
1163 | ((unsigned long)buf & (sector_size - 1)) || | ||
1164 | (i_size_read(inode) & (sector_size -1))) { | ||
1165 | filp->f_flags &= ~O_DIRECT; | ||
1166 | } | ||
1167 | } | ||
1168 | } | ||
1169 | #endif | ||
1170 | |||
1171 | /* | ||
1172 | * buffered reads protect themselves in ->readpage(). O_DIRECT reads | ||
1173 | * need locks to protect pending reads from racing with truncate. | ||
1174 | */ | ||
1175 | if (filp->f_flags & O_DIRECT) { | ||
1176 | down_read(&inode->i_alloc_sem); | ||
1177 | have_alloc_sem = 1; | ||
1178 | |||
1179 | ret = ocfs2_rw_lock(inode, 0); | ||
1180 | if (ret < 0) { | ||
1181 | mlog_errno(ret); | ||
1182 | goto bail; | ||
1183 | } | ||
1184 | rw_level = 0; | ||
1185 | /* communicate with ocfs2_dio_end_io */ | ||
1186 | ocfs2_iocb_set_rw_locked(iocb); | ||
1187 | } | ||
1188 | |||
1189 | ret = generic_file_aio_read(iocb, buf, count, iocb->ki_pos); | ||
1190 | if (ret == -EINVAL) | ||
1191 | mlog(ML_ERROR, "generic_file_aio_read returned -EINVAL\n"); | ||
1192 | |||
1193 | /* buffered aio wouldn't have proper lock coverage today */ | ||
1194 | BUG_ON(ret == -EIOCBQUEUED && !(filp->f_flags & O_DIRECT)); | ||
1195 | |||
1196 | /* see ocfs2_file_aio_write */ | ||
1197 | if (ret == -EIOCBQUEUED || !ocfs2_iocb_is_rw_locked(iocb)) { | ||
1198 | rw_level = -1; | ||
1199 | have_alloc_sem = 0; | ||
1200 | } | ||
1201 | |||
1202 | bail: | ||
1203 | if (have_alloc_sem) | ||
1204 | up_read(&inode->i_alloc_sem); | ||
1205 | if (rw_level != -1) | ||
1206 | ocfs2_rw_unlock(inode, rw_level); | ||
1207 | mlog_exit(ret); | ||
1208 | |||
1209 | return ret; | ||
1210 | } | ||
1211 | |||
1212 | struct inode_operations ocfs2_file_iops = { | ||
1213 | .setattr = ocfs2_setattr, | ||
1214 | .getattr = ocfs2_getattr, | ||
1215 | }; | ||
1216 | |||
1217 | struct inode_operations ocfs2_special_file_iops = { | ||
1218 | .setattr = ocfs2_setattr, | ||
1219 | .getattr = ocfs2_getattr, | ||
1220 | }; | ||
1221 | |||
1222 | struct file_operations ocfs2_fops = { | ||
1223 | .read = do_sync_read, | ||
1224 | .write = do_sync_write, | ||
1225 | .sendfile = generic_file_sendfile, | ||
1226 | .mmap = ocfs2_mmap, | ||
1227 | .fsync = ocfs2_sync_file, | ||
1228 | .release = ocfs2_file_release, | ||
1229 | .open = ocfs2_file_open, | ||
1230 | .aio_read = ocfs2_file_aio_read, | ||
1231 | .aio_write = ocfs2_file_aio_write, | ||
1232 | }; | ||
1233 | |||
1234 | struct file_operations ocfs2_dops = { | ||
1235 | .read = generic_read_dir, | ||
1236 | .readdir = ocfs2_readdir, | ||
1237 | .fsync = ocfs2_sync_file, | ||
1238 | }; | ||