1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2002, 2004 Oracle. All rights reserved.
4 */
5
6 #include <linux/fs.h>
7 #include <linux/slab.h>
8 #include <linux/highmem.h>
9 #include <linux/pagemap.h>
10 #include <asm/byteorder.h>
11 #include <linux/swap.h>
12 #include <linux/mpage.h>
13 #include <linux/quotaops.h>
14 #include <linux/blkdev.h>
15 #include <linux/uio.h>
16 #include <linux/mm.h>
17
18 #include <cluster/masklog.h>
19
20 #include "ocfs2.h"
21
22 #include "alloc.h"
23 #include "aops.h"
24 #include "dlmglue.h"
25 #include "extent_map.h"
26 #include "file.h"
27 #include "inode.h"
28 #include "journal.h"
29 #include "suballoc.h"
30 #include "super.h"
31 #include "symlink.h"
32 #include "refcounttree.h"
33 #include "ocfs2_trace.h"
34
35 #include "buffer_head_io.h"
36 #include "dir.h"
37 #include "namei.h"
38 #include "sysfile.h"
39
ocfs2_symlink_get_block(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create)40 static int ocfs2_symlink_get_block(struct inode *inode, sector_t iblock,
41 struct buffer_head *bh_result, int create)
42 {
43 int err = -EIO;
44 int status;
45 struct ocfs2_dinode *fe = NULL;
46 struct buffer_head *bh = NULL;
47 struct buffer_head *buffer_cache_bh = NULL;
48 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
49
50 trace_ocfs2_symlink_get_block(
51 (unsigned long long)OCFS2_I(inode)->ip_blkno,
52 (unsigned long long)iblock, bh_result, create);
53
54 BUG_ON(ocfs2_inode_is_fast_symlink(inode));
55
56 if ((iblock << inode->i_sb->s_blocksize_bits) > PATH_MAX + 1) {
57 mlog(ML_ERROR, "block offset > PATH_MAX: %llu",
58 (unsigned long long)iblock);
59 goto bail;
60 }
61
62 status = ocfs2_read_inode_block(inode, &bh);
63 if (status < 0) {
64 mlog_errno(status);
65 goto bail;
66 }
67 fe = (struct ocfs2_dinode *) bh->b_data;
68
69 if ((u64)iblock >= ocfs2_clusters_to_blocks(inode->i_sb,
70 le32_to_cpu(fe->i_clusters))) {
71 err = -ENOMEM;
72 mlog(ML_ERROR, "block offset is outside the allocated size: "
73 "%llu\n", (unsigned long long)iblock);
74 goto bail;
75 }
76
77 /* We don't use the page cache to create symlink data, so if
78 * need be, copy it over from the buffer cache. */
79 if (!buffer_uptodate(bh_result) && ocfs2_inode_is_new(inode)) {
80 u64 blkno = le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) +
81 iblock;
82 buffer_cache_bh = sb_getblk(osb->sb, blkno);
83 if (!buffer_cache_bh) {
84 err = -ENOMEM;
85 mlog(ML_ERROR, "couldn't getblock for symlink!\n");
86 goto bail;
87 }
88
89 /* we haven't locked out transactions, so a commit
90 * could've happened. Since we've got a reference on
91 * the bh, even if it commits while we're doing the
92 * copy, the data is still good. */
93 if (buffer_jbd(buffer_cache_bh) && ocfs2_inode_is_new(inode)) {
94 memcpy_to_folio(bh_result->b_folio,
95 bh_result->b_size * iblock,
96 buffer_cache_bh->b_data,
97 bh_result->b_size);
98 set_buffer_uptodate(bh_result);
99 }
100 brelse(buffer_cache_bh);
101 }
102
103 map_bh(bh_result, inode->i_sb,
104 le64_to_cpu(fe->id2.i_list.l_recs[0].e_blkno) + iblock);
105
106 err = 0;
107
108 bail:
109 brelse(bh);
110
111 return err;
112 }
113
ocfs2_lock_get_block(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create)114 static int ocfs2_lock_get_block(struct inode *inode, sector_t iblock,
115 struct buffer_head *bh_result, int create)
116 {
117 int ret = 0;
118 struct ocfs2_inode_info *oi = OCFS2_I(inode);
119
120 down_read(&oi->ip_alloc_sem);
121 ret = ocfs2_get_block(inode, iblock, bh_result, create);
122 up_read(&oi->ip_alloc_sem);
123
124 return ret;
125 }
126
ocfs2_get_block(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create)127 int ocfs2_get_block(struct inode *inode, sector_t iblock,
128 struct buffer_head *bh_result, int create)
129 {
130 int err = 0;
131 unsigned int ext_flags;
132 u64 max_blocks = bh_result->b_size >> inode->i_blkbits;
133 u64 p_blkno, count, past_eof;
134 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
135
136 trace_ocfs2_get_block((unsigned long long)OCFS2_I(inode)->ip_blkno,
137 (unsigned long long)iblock, bh_result, create);
138
139 if (OCFS2_I(inode)->ip_flags & OCFS2_INODE_SYSTEM_FILE)
140 mlog(ML_NOTICE, "get_block on system inode 0x%p (%lu)\n",
141 inode, inode->i_ino);
142
143 if (S_ISLNK(inode->i_mode)) {
144 /* this always does I/O for some reason. */
145 err = ocfs2_symlink_get_block(inode, iblock, bh_result, create);
146 goto bail;
147 }
148
149 err = ocfs2_extent_map_get_blocks(inode, iblock, &p_blkno, &count,
150 &ext_flags);
151 if (err) {
152 mlog(ML_ERROR, "get_blocks() failed, inode: 0x%p, "
153 "block: %llu\n", inode, (unsigned long long)iblock);
154 goto bail;
155 }
156
157 if (max_blocks < count)
158 count = max_blocks;
159
160 /*
161 * ocfs2 never allocates in this function - the only time we
162 * need to use BH_New is when we're extending i_size on a file
163 * system which doesn't support holes, in which case BH_New
164 * allows __block_write_begin() to zero.
165 *
166 * If we see this on a sparse file system, then a truncate has
167 * raced us and removed the cluster. In this case, we clear
168 * the buffers dirty and uptodate bits and let the buffer code
169 * ignore it as a hole.
170 */
171 if (create && p_blkno == 0 && ocfs2_sparse_alloc(osb)) {
172 clear_buffer_dirty(bh_result);
173 clear_buffer_uptodate(bh_result);
174 goto bail;
175 }
176
177 /* Treat the unwritten extent as a hole for zeroing purposes. */
178 if (p_blkno && !(ext_flags & OCFS2_EXT_UNWRITTEN))
179 map_bh(bh_result, inode->i_sb, p_blkno);
180
181 bh_result->b_size = count << inode->i_blkbits;
182
183 if (!ocfs2_sparse_alloc(osb)) {
184 if (p_blkno == 0) {
185 err = -EIO;
186 mlog(ML_ERROR,
187 "iblock = %llu p_blkno = %llu blkno=(%llu)\n",
188 (unsigned long long)iblock,
189 (unsigned long long)p_blkno,
190 (unsigned long long)OCFS2_I(inode)->ip_blkno);
191 mlog(ML_ERROR, "Size %llu, clusters %u\n", (unsigned long long)i_size_read(inode), OCFS2_I(inode)->ip_clusters);
192 dump_stack();
193 goto bail;
194 }
195 }
196
197 past_eof = ocfs2_blocks_for_bytes(inode->i_sb, i_size_read(inode));
198
199 trace_ocfs2_get_block_end((unsigned long long)OCFS2_I(inode)->ip_blkno,
200 (unsigned long long)past_eof);
201 if (create && (iblock >= past_eof))
202 set_buffer_new(bh_result);
203
204 bail:
205 if (err < 0)
206 err = -EIO;
207
208 return err;
209 }
210
ocfs2_read_inline_data(struct inode * inode,struct folio * folio,struct buffer_head * di_bh)211 int ocfs2_read_inline_data(struct inode *inode, struct folio *folio,
212 struct buffer_head *di_bh)
213 {
214 loff_t size;
215 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
216
217 if (!(le16_to_cpu(di->i_dyn_features) & OCFS2_INLINE_DATA_FL)) {
218 ocfs2_error(inode->i_sb, "Inode %llu lost inline data flag\n",
219 (unsigned long long)OCFS2_I(inode)->ip_blkno);
220 return -EROFS;
221 }
222
223 size = i_size_read(inode);
224
225 if (size > folio_size(folio) ||
226 size > ocfs2_max_inline_data_with_xattr(inode->i_sb, di)) {
227 ocfs2_error(inode->i_sb,
228 "Inode %llu has with inline data has bad size: %Lu\n",
229 (unsigned long long)OCFS2_I(inode)->ip_blkno,
230 (unsigned long long)size);
231 return -EROFS;
232 }
233
234 folio_fill_tail(folio, 0, di->id2.i_data.id_data, size);
235 folio_mark_uptodate(folio);
236
237 return 0;
238 }
239
ocfs2_readpage_inline(struct inode * inode,struct folio * folio)240 static int ocfs2_readpage_inline(struct inode *inode, struct folio *folio)
241 {
242 int ret;
243 struct buffer_head *di_bh = NULL;
244
245 BUG_ON(!folio_test_locked(folio));
246 BUG_ON(!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL));
247
248 ret = ocfs2_read_inode_block(inode, &di_bh);
249 if (ret) {
250 mlog_errno(ret);
251 goto out;
252 }
253
254 ret = ocfs2_read_inline_data(inode, folio, di_bh);
255 out:
256 folio_unlock(folio);
257
258 brelse(di_bh);
259 return ret;
260 }
261
ocfs2_read_folio(struct file * file,struct folio * folio)262 static int ocfs2_read_folio(struct file *file, struct folio *folio)
263 {
264 struct inode *inode = folio->mapping->host;
265 struct ocfs2_inode_info *oi = OCFS2_I(inode);
266 loff_t start = folio_pos(folio);
267 int ret, unlock = 1;
268
269 trace_ocfs2_readpage((unsigned long long)oi->ip_blkno, folio->index);
270
271 ret = ocfs2_inode_lock_with_folio(inode, NULL, 0, folio);
272 if (ret != 0) {
273 if (ret == AOP_TRUNCATED_PAGE)
274 unlock = 0;
275 mlog_errno(ret);
276 goto out;
277 }
278
279 if (down_read_trylock(&oi->ip_alloc_sem) == 0) {
280 /*
281 * Unlock the folio and cycle ip_alloc_sem so that we don't
282 * busyloop waiting for ip_alloc_sem to unlock
283 */
284 ret = AOP_TRUNCATED_PAGE;
285 folio_unlock(folio);
286 unlock = 0;
287 down_read(&oi->ip_alloc_sem);
288 up_read(&oi->ip_alloc_sem);
289 goto out_inode_unlock;
290 }
291
292 /*
293 * i_size might have just been updated as we grabbed the meta lock. We
294 * might now be discovering a truncate that hit on another node.
295 * block_read_full_folio->get_block freaks out if it is asked to read
296 * beyond the end of a file, so we check here. Callers
297 * (generic_file_read, vm_ops->fault) are clever enough to check i_size
298 * and notice that the folio they just read isn't needed.
299 *
300 * XXX sys_readahead() seems to get that wrong?
301 */
302 if (start >= i_size_read(inode)) {
303 folio_zero_segment(folio, 0, folio_size(folio));
304 folio_mark_uptodate(folio);
305 ret = 0;
306 goto out_alloc;
307 }
308
309 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
310 ret = ocfs2_readpage_inline(inode, folio);
311 else
312 ret = block_read_full_folio(folio, ocfs2_get_block);
313 unlock = 0;
314
315 out_alloc:
316 up_read(&oi->ip_alloc_sem);
317 out_inode_unlock:
318 ocfs2_inode_unlock(inode, 0);
319 out:
320 if (unlock)
321 folio_unlock(folio);
322 return ret;
323 }
324
325 /*
326 * This is used only for read-ahead. Failures or difficult to handle
327 * situations are safe to ignore.
328 *
329 * Right now, we don't bother with BH_Boundary - in-inode extent lists
330 * are quite large (243 extents on 4k blocks), so most inodes don't
331 * grow out to a tree. If need be, detecting boundary extents could
332 * trivially be added in a future version of ocfs2_get_block().
333 */
ocfs2_readahead(struct readahead_control * rac)334 static void ocfs2_readahead(struct readahead_control *rac)
335 {
336 int ret;
337 struct inode *inode = rac->mapping->host;
338 struct ocfs2_inode_info *oi = OCFS2_I(inode);
339
340 /*
341 * Use the nonblocking flag for the dlm code to avoid page
342 * lock inversion, but don't bother with retrying.
343 */
344 ret = ocfs2_inode_lock_full(inode, NULL, 0, OCFS2_LOCK_NONBLOCK);
345 if (ret)
346 return;
347
348 if (down_read_trylock(&oi->ip_alloc_sem) == 0)
349 goto out_unlock;
350
351 /*
352 * Don't bother with inline-data. There isn't anything
353 * to read-ahead in that case anyway...
354 */
355 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL)
356 goto out_up;
357
358 /*
359 * Check whether a remote node truncated this file - we just
360 * drop out in that case as it's not worth handling here.
361 */
362 if (readahead_pos(rac) >= i_size_read(inode))
363 goto out_up;
364
365 mpage_readahead(rac, ocfs2_get_block);
366
367 out_up:
368 up_read(&oi->ip_alloc_sem);
369 out_unlock:
370 ocfs2_inode_unlock(inode, 0);
371 }
372
373 /* Note: Because we don't support holes, our allocation has
374 * already happened (allocation writes zeros to the file data)
375 * so we don't have to worry about ordered writes in
376 * ocfs2_writepages.
377 *
378 * ->writepages is called during the process of invalidating the page cache
379 * during blocked lock processing. It can't block on any cluster locks
380 * to during block mapping. It's relying on the fact that the block
381 * mapping can't have disappeared under the dirty pages that it is
382 * being asked to write back.
383 */
ocfs2_writepages(struct address_space * mapping,struct writeback_control * wbc)384 static int ocfs2_writepages(struct address_space *mapping,
385 struct writeback_control *wbc)
386 {
387 return mpage_writepages(mapping, wbc, ocfs2_get_block);
388 }
389
390 /* Taken from ext3. We don't necessarily need the full blown
391 * functionality yet, but IMHO it's better to cut and paste the whole
392 * thing so we can avoid introducing our own bugs (and easily pick up
393 * their fixes when they happen) --Mark */
walk_page_buffers(handle_t * handle,struct buffer_head * head,unsigned from,unsigned to,int * partial,int (* fn)(handle_t * handle,struct buffer_head * bh))394 int walk_page_buffers( handle_t *handle,
395 struct buffer_head *head,
396 unsigned from,
397 unsigned to,
398 int *partial,
399 int (*fn)( handle_t *handle,
400 struct buffer_head *bh))
401 {
402 struct buffer_head *bh;
403 unsigned block_start, block_end;
404 unsigned blocksize = head->b_size;
405 int err, ret = 0;
406 struct buffer_head *next;
407
408 for ( bh = head, block_start = 0;
409 ret == 0 && (bh != head || !block_start);
410 block_start = block_end, bh = next)
411 {
412 next = bh->b_this_page;
413 block_end = block_start + blocksize;
414 if (block_end <= from || block_start >= to) {
415 if (partial && !buffer_uptodate(bh))
416 *partial = 1;
417 continue;
418 }
419 err = (*fn)(handle, bh);
420 if (!ret)
421 ret = err;
422 }
423 return ret;
424 }
425
ocfs2_bmap(struct address_space * mapping,sector_t block)426 static sector_t ocfs2_bmap(struct address_space *mapping, sector_t block)
427 {
428 sector_t status;
429 u64 p_blkno = 0;
430 int err = 0;
431 struct inode *inode = mapping->host;
432
433 trace_ocfs2_bmap((unsigned long long)OCFS2_I(inode)->ip_blkno,
434 (unsigned long long)block);
435
436 /*
437 * The swap code (ab-)uses ->bmap to get a block mapping and then
438 * bypasseѕ the file system for actual I/O. We really can't allow
439 * that on refcounted inodes, so we have to skip out here. And yes,
440 * 0 is the magic code for a bmap error..
441 */
442 if (ocfs2_is_refcount_inode(inode))
443 return 0;
444
445 /* We don't need to lock journal system files, since they aren't
446 * accessed concurrently from multiple nodes.
447 */
448 if (!INODE_JOURNAL(inode)) {
449 err = ocfs2_inode_lock(inode, NULL, 0);
450 if (err) {
451 if (err != -ENOENT)
452 mlog_errno(err);
453 goto bail;
454 }
455 down_read(&OCFS2_I(inode)->ip_alloc_sem);
456 }
457
458 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL))
459 err = ocfs2_extent_map_get_blocks(inode, block, &p_blkno, NULL,
460 NULL);
461
462 if (!INODE_JOURNAL(inode)) {
463 up_read(&OCFS2_I(inode)->ip_alloc_sem);
464 ocfs2_inode_unlock(inode, 0);
465 }
466
467 if (err) {
468 mlog(ML_ERROR, "get_blocks() failed, block = %llu\n",
469 (unsigned long long)block);
470 mlog_errno(err);
471 goto bail;
472 }
473
474 bail:
475 status = err ? 0 : p_blkno;
476
477 return status;
478 }
479
ocfs2_release_folio(struct folio * folio,gfp_t wait)480 static bool ocfs2_release_folio(struct folio *folio, gfp_t wait)
481 {
482 if (!folio_buffers(folio))
483 return false;
484 return try_to_free_buffers(folio);
485 }
486
ocfs2_figure_cluster_boundaries(struct ocfs2_super * osb,u32 cpos,unsigned int * start,unsigned int * end)487 static void ocfs2_figure_cluster_boundaries(struct ocfs2_super *osb,
488 u32 cpos,
489 unsigned int *start,
490 unsigned int *end)
491 {
492 unsigned int cluster_start = 0, cluster_end = PAGE_SIZE;
493
494 if (unlikely(PAGE_SHIFT > osb->s_clustersize_bits)) {
495 unsigned int cpp;
496
497 cpp = 1 << (PAGE_SHIFT - osb->s_clustersize_bits);
498
499 cluster_start = cpos % cpp;
500 cluster_start = cluster_start << osb->s_clustersize_bits;
501
502 cluster_end = cluster_start + osb->s_clustersize;
503 }
504
505 BUG_ON(cluster_start > PAGE_SIZE);
506 BUG_ON(cluster_end > PAGE_SIZE);
507
508 if (start)
509 *start = cluster_start;
510 if (end)
511 *end = cluster_end;
512 }
513
514 /*
515 * 'from' and 'to' are the region in the page to avoid zeroing.
516 *
517 * If pagesize > clustersize, this function will avoid zeroing outside
518 * of the cluster boundary.
519 *
520 * from == to == 0 is code for "zero the entire cluster region"
521 */
ocfs2_clear_folio_regions(struct folio * folio,struct ocfs2_super * osb,u32 cpos,unsigned from,unsigned to)522 static void ocfs2_clear_folio_regions(struct folio *folio,
523 struct ocfs2_super *osb, u32 cpos,
524 unsigned from, unsigned to)
525 {
526 void *kaddr;
527 unsigned int cluster_start, cluster_end;
528
529 ocfs2_figure_cluster_boundaries(osb, cpos, &cluster_start, &cluster_end);
530
531 kaddr = kmap_local_folio(folio, 0);
532
533 if (from || to) {
534 if (from > cluster_start)
535 memset(kaddr + cluster_start, 0, from - cluster_start);
536 if (to < cluster_end)
537 memset(kaddr + to, 0, cluster_end - to);
538 } else {
539 memset(kaddr + cluster_start, 0, cluster_end - cluster_start);
540 }
541
542 kunmap_local(kaddr);
543 }
544
545 /*
546 * Nonsparse file systems fully allocate before we get to the write
547 * code. This prevents ocfs2_write() from tagging the write as an
548 * allocating one, which means ocfs2_map_folio_blocks() might try to
549 * read-in the blocks at the tail of our file. Avoid reading them by
550 * testing i_size against each block offset.
551 */
ocfs2_should_read_blk(struct inode * inode,struct folio * folio,unsigned int block_start)552 static int ocfs2_should_read_blk(struct inode *inode, struct folio *folio,
553 unsigned int block_start)
554 {
555 u64 offset = folio_pos(folio) + block_start;
556
557 if (ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)))
558 return 1;
559
560 if (i_size_read(inode) > offset)
561 return 1;
562
563 return 0;
564 }
565
566 /*
567 * Some of this taken from __block_write_begin(). We already have our
568 * mapping by now though, and the entire write will be allocating or
569 * it won't, so not much need to use BH_New.
570 *
571 * This will also skip zeroing, which is handled externally.
572 */
ocfs2_map_folio_blocks(struct folio * folio,u64 * p_blkno,struct inode * inode,unsigned int from,unsigned int to,int new)573 int ocfs2_map_folio_blocks(struct folio *folio, u64 *p_blkno,
574 struct inode *inode, unsigned int from,
575 unsigned int to, int new)
576 {
577 int ret = 0;
578 struct buffer_head *head, *bh, *wait[2], **wait_bh = wait;
579 unsigned int block_end, block_start;
580 unsigned int bsize = i_blocksize(inode);
581
582 head = folio_buffers(folio);
583 if (!head)
584 head = create_empty_buffers(folio, bsize, 0);
585
586 for (bh = head, block_start = 0; bh != head || !block_start;
587 bh = bh->b_this_page, block_start += bsize) {
588 block_end = block_start + bsize;
589
590 clear_buffer_new(bh);
591
592 /*
593 * Ignore blocks outside of our i/o range -
594 * they may belong to unallocated clusters.
595 */
596 if (block_start >= to || block_end <= from) {
597 if (folio_test_uptodate(folio))
598 set_buffer_uptodate(bh);
599 continue;
600 }
601
602 /*
603 * For an allocating write with cluster size >= page
604 * size, we always write the entire page.
605 */
606 if (new)
607 set_buffer_new(bh);
608
609 if (!buffer_mapped(bh)) {
610 map_bh(bh, inode->i_sb, *p_blkno);
611 clean_bdev_bh_alias(bh);
612 }
613
614 if (folio_test_uptodate(folio)) {
615 set_buffer_uptodate(bh);
616 } else if (!buffer_uptodate(bh) && !buffer_delay(bh) &&
617 !buffer_new(bh) &&
618 ocfs2_should_read_blk(inode, folio, block_start) &&
619 (block_start < from || block_end > to)) {
620 bh_read_nowait(bh, 0);
621 *wait_bh++=bh;
622 }
623
624 *p_blkno = *p_blkno + 1;
625 }
626
627 /*
628 * If we issued read requests - let them complete.
629 */
630 while(wait_bh > wait) {
631 wait_on_buffer(*--wait_bh);
632 if (!buffer_uptodate(*wait_bh))
633 ret = -EIO;
634 }
635
636 if (ret == 0 || !new)
637 return ret;
638
639 /*
640 * If we get -EIO above, zero out any newly allocated blocks
641 * to avoid exposing stale data.
642 */
643 bh = head;
644 block_start = 0;
645 do {
646 block_end = block_start + bsize;
647 if (block_end <= from)
648 goto next_bh;
649 if (block_start >= to)
650 break;
651
652 folio_zero_range(folio, block_start, bh->b_size);
653 set_buffer_uptodate(bh);
654 mark_buffer_dirty(bh);
655
656 next_bh:
657 block_start = block_end;
658 bh = bh->b_this_page;
659 } while (bh != head);
660
661 return ret;
662 }
663
664 #if (PAGE_SIZE >= OCFS2_MAX_CLUSTERSIZE)
665 #define OCFS2_MAX_CTXT_PAGES 1
666 #else
667 #define OCFS2_MAX_CTXT_PAGES (OCFS2_MAX_CLUSTERSIZE / PAGE_SIZE)
668 #endif
669
670 #define OCFS2_MAX_CLUSTERS_PER_PAGE (PAGE_SIZE / OCFS2_MIN_CLUSTERSIZE)
671
672 struct ocfs2_unwritten_extent {
673 struct list_head ue_node;
674 struct list_head ue_ip_node;
675 u32 ue_cpos;
676 u32 ue_phys;
677 };
678
679 /*
680 * Describe the state of a single cluster to be written to.
681 */
682 struct ocfs2_write_cluster_desc {
683 u32 c_cpos;
684 u32 c_phys;
685 /*
686 * Give this a unique field because c_phys eventually gets
687 * filled.
688 */
689 unsigned c_new;
690 unsigned c_clear_unwritten;
691 unsigned c_needs_zero;
692 };
693
694 struct ocfs2_write_ctxt {
695 /* Logical cluster position / len of write */
696 u32 w_cpos;
697 u32 w_clen;
698
699 /* First cluster allocated in a nonsparse extend */
700 u32 w_first_new_cpos;
701
702 /* Type of caller. Must be one of buffer, mmap, direct. */
703 ocfs2_write_type_t w_type;
704
705 struct ocfs2_write_cluster_desc w_desc[OCFS2_MAX_CLUSTERS_PER_PAGE];
706
707 /*
708 * This is true if page_size > cluster_size.
709 *
710 * It triggers a set of special cases during write which might
711 * have to deal with allocating writes to partial pages.
712 */
713 unsigned int w_large_pages;
714
715 /*
716 * Folios involved in this write.
717 *
718 * w_target_folio is the folio being written to by the user.
719 *
720 * w_folios is an array of folios which always contains
721 * w_target_folio, and in the case of an allocating write with
722 * page_size < cluster size, it will contain zero'd and mapped
723 * pages adjacent to w_target_folio which need to be written
724 * out in so that future reads from that region will get
725 * zero's.
726 */
727 unsigned int w_num_folios;
728 struct folio *w_folios[OCFS2_MAX_CTXT_PAGES];
729 struct folio *w_target_folio;
730
731 /*
732 * w_target_locked is used for page_mkwrite path indicating no unlocking
733 * against w_target_folio in ocfs2_write_end_nolock.
734 */
735 unsigned int w_target_locked:1;
736
737 /*
738 * ocfs2_write_end() uses this to know what the real range to
739 * write in the target should be.
740 */
741 unsigned int w_target_from;
742 unsigned int w_target_to;
743
744 /*
745 * We could use journal_current_handle() but this is cleaner,
746 * IMHO -Mark
747 */
748 handle_t *w_handle;
749
750 struct buffer_head *w_di_bh;
751
752 struct ocfs2_cached_dealloc_ctxt w_dealloc;
753
754 struct list_head w_unwritten_list;
755 unsigned int w_unwritten_count;
756 };
757
ocfs2_unlock_and_free_folios(struct folio ** folios,int num_folios)758 void ocfs2_unlock_and_free_folios(struct folio **folios, int num_folios)
759 {
760 int i;
761
762 for(i = 0; i < num_folios; i++) {
763 if (!folios[i])
764 continue;
765 folio_unlock(folios[i]);
766 folio_mark_accessed(folios[i]);
767 folio_put(folios[i]);
768 }
769 }
770
ocfs2_unlock_folios(struct ocfs2_write_ctxt * wc)771 static void ocfs2_unlock_folios(struct ocfs2_write_ctxt *wc)
772 {
773 int i;
774
775 /*
776 * w_target_locked is only set to true in the page_mkwrite() case.
777 * The intent is to allow us to lock the target page from write_begin()
778 * to write_end(). The caller must hold a ref on w_target_folio.
779 */
780 if (wc->w_target_locked) {
781 BUG_ON(!wc->w_target_folio);
782 for (i = 0; i < wc->w_num_folios; i++) {
783 if (wc->w_target_folio == wc->w_folios[i]) {
784 wc->w_folios[i] = NULL;
785 break;
786 }
787 }
788 folio_mark_accessed(wc->w_target_folio);
789 folio_put(wc->w_target_folio);
790 }
791 ocfs2_unlock_and_free_folios(wc->w_folios, wc->w_num_folios);
792 }
793
ocfs2_free_unwritten_list(struct inode * inode,struct list_head * head)794 static void ocfs2_free_unwritten_list(struct inode *inode,
795 struct list_head *head)
796 {
797 struct ocfs2_inode_info *oi = OCFS2_I(inode);
798 struct ocfs2_unwritten_extent *ue = NULL, *tmp = NULL;
799
800 list_for_each_entry_safe(ue, tmp, head, ue_node) {
801 list_del(&ue->ue_node);
802 spin_lock(&oi->ip_lock);
803 list_del(&ue->ue_ip_node);
804 spin_unlock(&oi->ip_lock);
805 kfree(ue);
806 }
807 }
808
ocfs2_free_write_ctxt(struct inode * inode,struct ocfs2_write_ctxt * wc)809 static void ocfs2_free_write_ctxt(struct inode *inode,
810 struct ocfs2_write_ctxt *wc)
811 {
812 ocfs2_free_unwritten_list(inode, &wc->w_unwritten_list);
813 ocfs2_unlock_folios(wc);
814 brelse(wc->w_di_bh);
815 kfree(wc);
816 }
817
ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt ** wcp,struct ocfs2_super * osb,loff_t pos,unsigned len,ocfs2_write_type_t type,struct buffer_head * di_bh)818 static int ocfs2_alloc_write_ctxt(struct ocfs2_write_ctxt **wcp,
819 struct ocfs2_super *osb, loff_t pos,
820 unsigned len, ocfs2_write_type_t type,
821 struct buffer_head *di_bh)
822 {
823 u32 cend;
824 struct ocfs2_write_ctxt *wc;
825
826 wc = kzalloc(sizeof(struct ocfs2_write_ctxt), GFP_NOFS);
827 if (!wc)
828 return -ENOMEM;
829
830 wc->w_cpos = pos >> osb->s_clustersize_bits;
831 wc->w_first_new_cpos = UINT_MAX;
832 cend = (pos + len - 1) >> osb->s_clustersize_bits;
833 wc->w_clen = cend - wc->w_cpos + 1;
834 get_bh(di_bh);
835 wc->w_di_bh = di_bh;
836 wc->w_type = type;
837
838 if (unlikely(PAGE_SHIFT > osb->s_clustersize_bits))
839 wc->w_large_pages = 1;
840 else
841 wc->w_large_pages = 0;
842
843 ocfs2_init_dealloc_ctxt(&wc->w_dealloc);
844 INIT_LIST_HEAD(&wc->w_unwritten_list);
845
846 *wcp = wc;
847
848 return 0;
849 }
850
851 /*
852 * If a page has any new buffers, zero them out here, and mark them uptodate
853 * and dirty so they'll be written out (in order to prevent uninitialised
854 * block data from leaking). And clear the new bit.
855 */
ocfs2_zero_new_buffers(struct folio * folio,size_t from,size_t to)856 static void ocfs2_zero_new_buffers(struct folio *folio, size_t from, size_t to)
857 {
858 unsigned int block_start, block_end;
859 struct buffer_head *head, *bh;
860
861 BUG_ON(!folio_test_locked(folio));
862 head = folio_buffers(folio);
863 if (!head)
864 return;
865
866 bh = head;
867 block_start = 0;
868 do {
869 block_end = block_start + bh->b_size;
870
871 if (buffer_new(bh)) {
872 if (block_end > from && block_start < to) {
873 if (!folio_test_uptodate(folio)) {
874 unsigned start, end;
875
876 start = max(from, block_start);
877 end = min(to, block_end);
878
879 folio_zero_segment(folio, start, end);
880 set_buffer_uptodate(bh);
881 }
882
883 clear_buffer_new(bh);
884 mark_buffer_dirty(bh);
885 }
886 }
887
888 block_start = block_end;
889 bh = bh->b_this_page;
890 } while (bh != head);
891 }
892
893 /*
894 * Only called when we have a failure during allocating write to write
895 * zero's to the newly allocated region.
896 */
ocfs2_write_failure(struct inode * inode,struct ocfs2_write_ctxt * wc,loff_t user_pos,unsigned user_len)897 static void ocfs2_write_failure(struct inode *inode,
898 struct ocfs2_write_ctxt *wc,
899 loff_t user_pos, unsigned user_len)
900 {
901 int i;
902 unsigned from = user_pos & (PAGE_SIZE - 1),
903 to = user_pos + user_len;
904
905 if (wc->w_target_folio)
906 ocfs2_zero_new_buffers(wc->w_target_folio, from, to);
907
908 for (i = 0; i < wc->w_num_folios; i++) {
909 struct folio *folio = wc->w_folios[i];
910
911 if (folio && folio_buffers(folio)) {
912 if (ocfs2_should_order_data(inode))
913 ocfs2_jbd2_inode_add_write(wc->w_handle, inode,
914 user_pos, user_len);
915
916 block_commit_write(folio, from, to);
917 }
918 }
919 }
920
ocfs2_prepare_folio_for_write(struct inode * inode,u64 * p_blkno,struct ocfs2_write_ctxt * wc,struct folio * folio,u32 cpos,loff_t user_pos,unsigned user_len,int new)921 static int ocfs2_prepare_folio_for_write(struct inode *inode, u64 *p_blkno,
922 struct ocfs2_write_ctxt *wc, struct folio *folio, u32 cpos,
923 loff_t user_pos, unsigned user_len, int new)
924 {
925 int ret;
926 unsigned int map_from = 0, map_to = 0;
927 unsigned int cluster_start, cluster_end;
928 unsigned int user_data_from = 0, user_data_to = 0;
929
930 ocfs2_figure_cluster_boundaries(OCFS2_SB(inode->i_sb), cpos,
931 &cluster_start, &cluster_end);
932
933 /* treat the write as new if the a hole/lseek spanned across
934 * the page boundary.
935 */
936 new = new | ((i_size_read(inode) <= folio_pos(folio)) &&
937 (folio_pos(folio) <= user_pos));
938
939 if (folio == wc->w_target_folio) {
940 map_from = user_pos & (PAGE_SIZE - 1);
941 map_to = map_from + user_len;
942
943 if (new)
944 ret = ocfs2_map_folio_blocks(folio, p_blkno, inode,
945 cluster_start, cluster_end, new);
946 else
947 ret = ocfs2_map_folio_blocks(folio, p_blkno, inode,
948 map_from, map_to, new);
949 if (ret) {
950 mlog_errno(ret);
951 goto out;
952 }
953
954 user_data_from = map_from;
955 user_data_to = map_to;
956 if (new) {
957 map_from = cluster_start;
958 map_to = cluster_end;
959 }
960 } else {
961 /*
962 * If we haven't allocated the new folio yet, we
963 * shouldn't be writing it out without copying user
964 * data. This is likely a math error from the caller.
965 */
966 BUG_ON(!new);
967
968 map_from = cluster_start;
969 map_to = cluster_end;
970
971 ret = ocfs2_map_folio_blocks(folio, p_blkno, inode,
972 cluster_start, cluster_end, new);
973 if (ret) {
974 mlog_errno(ret);
975 goto out;
976 }
977 }
978
979 /*
980 * Parts of newly allocated folios need to be zero'd.
981 *
982 * Above, we have also rewritten 'to' and 'from' - as far as
983 * the rest of the function is concerned, the entire cluster
984 * range inside of a folio needs to be written.
985 *
986 * We can skip this if the folio is uptodate - it's already
987 * been zero'd from being read in as a hole.
988 */
989 if (new && !folio_test_uptodate(folio))
990 ocfs2_clear_folio_regions(folio, OCFS2_SB(inode->i_sb),
991 cpos, user_data_from, user_data_to);
992
993 flush_dcache_folio(folio);
994
995 out:
996 return ret;
997 }
998
999 /*
1000 * This function will only grab one clusters worth of pages.
1001 */
ocfs2_grab_folios_for_write(struct address_space * mapping,struct ocfs2_write_ctxt * wc,u32 cpos,loff_t user_pos,unsigned user_len,int new,struct folio * mmap_folio)1002 static int ocfs2_grab_folios_for_write(struct address_space *mapping,
1003 struct ocfs2_write_ctxt *wc, u32 cpos, loff_t user_pos,
1004 unsigned user_len, int new, struct folio *mmap_folio)
1005 {
1006 int ret = 0, i;
1007 unsigned long start, target_index, end_index, index;
1008 struct inode *inode = mapping->host;
1009 loff_t last_byte;
1010
1011 target_index = user_pos >> PAGE_SHIFT;
1012
1013 /*
1014 * Figure out how many pages we'll be manipulating here. For
1015 * non allocating write, we just change the one
1016 * page. Otherwise, we'll need a whole clusters worth. If we're
1017 * writing past i_size, we only need enough pages to cover the
1018 * last page of the write.
1019 */
1020 if (new) {
1021 wc->w_num_folios = ocfs2_pages_per_cluster(inode->i_sb);
1022 start = ocfs2_align_clusters_to_page_index(inode->i_sb, cpos);
1023 /*
1024 * We need the index *past* the last page we could possibly
1025 * touch. This is the page past the end of the write or
1026 * i_size, whichever is greater.
1027 */
1028 last_byte = max(user_pos + user_len, i_size_read(inode));
1029 BUG_ON(last_byte < 1);
1030 end_index = ((last_byte - 1) >> PAGE_SHIFT) + 1;
1031 if ((start + wc->w_num_folios) > end_index)
1032 wc->w_num_folios = end_index - start;
1033 } else {
1034 wc->w_num_folios = 1;
1035 start = target_index;
1036 }
1037 end_index = (user_pos + user_len - 1) >> PAGE_SHIFT;
1038
1039 for(i = 0; i < wc->w_num_folios; i++) {
1040 index = start + i;
1041
1042 if (index >= target_index && index <= end_index &&
1043 wc->w_type == OCFS2_WRITE_MMAP) {
1044 /*
1045 * ocfs2_pagemkwrite() is a little different
1046 * and wants us to directly use the page
1047 * passed in.
1048 */
1049 folio_lock(mmap_folio);
1050
1051 /* Exit and let the caller retry */
1052 if (mmap_folio->mapping != mapping) {
1053 WARN_ON(mmap_folio->mapping);
1054 folio_unlock(mmap_folio);
1055 ret = -EAGAIN;
1056 goto out;
1057 }
1058
1059 folio_get(mmap_folio);
1060 wc->w_folios[i] = mmap_folio;
1061 wc->w_target_locked = true;
1062 } else if (index >= target_index && index <= end_index &&
1063 wc->w_type == OCFS2_WRITE_DIRECT) {
1064 /* Direct write has no mapping page. */
1065 wc->w_folios[i] = NULL;
1066 continue;
1067 } else {
1068 wc->w_folios[i] = __filemap_get_folio(mapping, index,
1069 FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
1070 GFP_NOFS);
1071 if (IS_ERR(wc->w_folios[i])) {
1072 ret = PTR_ERR(wc->w_folios[i]);
1073 mlog_errno(ret);
1074 goto out;
1075 }
1076 }
1077 folio_wait_stable(wc->w_folios[i]);
1078
1079 if (index == target_index)
1080 wc->w_target_folio = wc->w_folios[i];
1081 }
1082 out:
1083 if (ret)
1084 wc->w_target_locked = false;
1085 return ret;
1086 }
1087
1088 /*
1089 * Prepare a single cluster for write one cluster into the file.
1090 */
ocfs2_write_cluster(struct address_space * mapping,u32 * phys,unsigned int new,unsigned int clear_unwritten,unsigned int should_zero,struct ocfs2_alloc_context * data_ac,struct ocfs2_alloc_context * meta_ac,struct ocfs2_write_ctxt * wc,u32 cpos,loff_t user_pos,unsigned user_len)1091 static int ocfs2_write_cluster(struct address_space *mapping,
1092 u32 *phys, unsigned int new,
1093 unsigned int clear_unwritten,
1094 unsigned int should_zero,
1095 struct ocfs2_alloc_context *data_ac,
1096 struct ocfs2_alloc_context *meta_ac,
1097 struct ocfs2_write_ctxt *wc, u32 cpos,
1098 loff_t user_pos, unsigned user_len)
1099 {
1100 int ret, i;
1101 u64 p_blkno;
1102 struct inode *inode = mapping->host;
1103 struct ocfs2_extent_tree et;
1104 int bpc = ocfs2_clusters_to_blocks(inode->i_sb, 1);
1105
1106 if (new) {
1107 u32 tmp_pos;
1108
1109 /*
1110 * This is safe to call with the page locks - it won't take
1111 * any additional semaphores or cluster locks.
1112 */
1113 tmp_pos = cpos;
1114 ret = ocfs2_add_inode_data(OCFS2_SB(inode->i_sb), inode,
1115 &tmp_pos, 1, !clear_unwritten,
1116 wc->w_di_bh, wc->w_handle,
1117 data_ac, meta_ac, NULL);
1118 /*
1119 * This shouldn't happen because we must have already
1120 * calculated the correct meta data allocation required. The
1121 * internal tree allocation code should know how to increase
1122 * transaction credits itself.
1123 *
1124 * If need be, we could handle -EAGAIN for a
1125 * RESTART_TRANS here.
1126 */
1127 mlog_bug_on_msg(ret == -EAGAIN,
1128 "Inode %llu: EAGAIN return during allocation.\n",
1129 (unsigned long long)OCFS2_I(inode)->ip_blkno);
1130 if (ret < 0) {
1131 mlog_errno(ret);
1132 goto out;
1133 }
1134 } else if (clear_unwritten) {
1135 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode),
1136 wc->w_di_bh);
1137 ret = ocfs2_mark_extent_written(inode, &et,
1138 wc->w_handle, cpos, 1, *phys,
1139 meta_ac, &wc->w_dealloc);
1140 if (ret < 0) {
1141 mlog_errno(ret);
1142 goto out;
1143 }
1144 }
1145
1146 /*
1147 * The only reason this should fail is due to an inability to
1148 * find the extent added.
1149 */
1150 ret = ocfs2_get_clusters(inode, cpos, phys, NULL, NULL);
1151 if (ret < 0) {
1152 mlog(ML_ERROR, "Get physical blkno failed for inode %llu, "
1153 "at logical cluster %u",
1154 (unsigned long long)OCFS2_I(inode)->ip_blkno, cpos);
1155 goto out;
1156 }
1157
1158 BUG_ON(*phys == 0);
1159
1160 p_blkno = ocfs2_clusters_to_blocks(inode->i_sb, *phys);
1161 if (!should_zero)
1162 p_blkno += (user_pos >> inode->i_sb->s_blocksize_bits) & (u64)(bpc - 1);
1163
1164 for (i = 0; i < wc->w_num_folios; i++) {
1165 int tmpret;
1166
1167 /* This is the direct io target page. */
1168 if (wc->w_folios[i] == NULL) {
1169 p_blkno += (1 << (PAGE_SHIFT - inode->i_sb->s_blocksize_bits));
1170 continue;
1171 }
1172
1173 tmpret = ocfs2_prepare_folio_for_write(inode, &p_blkno, wc,
1174 wc->w_folios[i], cpos, user_pos, user_len,
1175 should_zero);
1176 if (tmpret) {
1177 mlog_errno(tmpret);
1178 if (ret == 0)
1179 ret = tmpret;
1180 }
1181 }
1182
1183 /*
1184 * We only have cleanup to do in case of allocating write.
1185 */
1186 if (ret && new)
1187 ocfs2_write_failure(inode, wc, user_pos, user_len);
1188
1189 out:
1190
1191 return ret;
1192 }
1193
ocfs2_write_cluster_by_desc(struct address_space * mapping,struct ocfs2_alloc_context * data_ac,struct ocfs2_alloc_context * meta_ac,struct ocfs2_write_ctxt * wc,loff_t pos,unsigned len)1194 static int ocfs2_write_cluster_by_desc(struct address_space *mapping,
1195 struct ocfs2_alloc_context *data_ac,
1196 struct ocfs2_alloc_context *meta_ac,
1197 struct ocfs2_write_ctxt *wc,
1198 loff_t pos, unsigned len)
1199 {
1200 int ret, i;
1201 loff_t cluster_off;
1202 unsigned int local_len = len;
1203 struct ocfs2_write_cluster_desc *desc;
1204 struct ocfs2_super *osb = OCFS2_SB(mapping->host->i_sb);
1205
1206 for (i = 0; i < wc->w_clen; i++) {
1207 desc = &wc->w_desc[i];
1208
1209 /*
1210 * We have to make sure that the total write passed in
1211 * doesn't extend past a single cluster.
1212 */
1213 local_len = len;
1214 cluster_off = pos & (osb->s_clustersize - 1);
1215 if ((cluster_off + local_len) > osb->s_clustersize)
1216 local_len = osb->s_clustersize - cluster_off;
1217
1218 ret = ocfs2_write_cluster(mapping, &desc->c_phys,
1219 desc->c_new,
1220 desc->c_clear_unwritten,
1221 desc->c_needs_zero,
1222 data_ac, meta_ac,
1223 wc, desc->c_cpos, pos, local_len);
1224 if (ret) {
1225 mlog_errno(ret);
1226 goto out;
1227 }
1228
1229 len -= local_len;
1230 pos += local_len;
1231 }
1232
1233 ret = 0;
1234 out:
1235 return ret;
1236 }
1237
1238 /*
1239 * ocfs2_write_end() wants to know which parts of the target page it
1240 * should complete the write on. It's easiest to compute them ahead of
1241 * time when a more complete view of the write is available.
1242 */
ocfs2_set_target_boundaries(struct ocfs2_super * osb,struct ocfs2_write_ctxt * wc,loff_t pos,unsigned len,int alloc)1243 static void ocfs2_set_target_boundaries(struct ocfs2_super *osb,
1244 struct ocfs2_write_ctxt *wc,
1245 loff_t pos, unsigned len, int alloc)
1246 {
1247 struct ocfs2_write_cluster_desc *desc;
1248
1249 wc->w_target_from = pos & (PAGE_SIZE - 1);
1250 wc->w_target_to = wc->w_target_from + len;
1251
1252 if (alloc == 0)
1253 return;
1254
1255 /*
1256 * Allocating write - we may have different boundaries based
1257 * on page size and cluster size.
1258 *
1259 * NOTE: We can no longer compute one value from the other as
1260 * the actual write length and user provided length may be
1261 * different.
1262 */
1263
1264 if (wc->w_large_pages) {
1265 /*
1266 * We only care about the 1st and last cluster within
1267 * our range and whether they should be zero'd or not. Either
1268 * value may be extended out to the start/end of a
1269 * newly allocated cluster.
1270 */
1271 desc = &wc->w_desc[0];
1272 if (desc->c_needs_zero)
1273 ocfs2_figure_cluster_boundaries(osb,
1274 desc->c_cpos,
1275 &wc->w_target_from,
1276 NULL);
1277
1278 desc = &wc->w_desc[wc->w_clen - 1];
1279 if (desc->c_needs_zero)
1280 ocfs2_figure_cluster_boundaries(osb,
1281 desc->c_cpos,
1282 NULL,
1283 &wc->w_target_to);
1284 } else {
1285 wc->w_target_from = 0;
1286 wc->w_target_to = PAGE_SIZE;
1287 }
1288 }
1289
1290 /*
1291 * Check if this extent is marked UNWRITTEN by direct io. If so, we need not to
1292 * do the zero work. And should not to clear UNWRITTEN since it will be cleared
1293 * by the direct io procedure.
1294 * If this is a new extent that allocated by direct io, we should mark it in
1295 * the ip_unwritten_list.
1296 */
ocfs2_unwritten_check(struct inode * inode,struct ocfs2_write_ctxt * wc,struct ocfs2_write_cluster_desc * desc)1297 static int ocfs2_unwritten_check(struct inode *inode,
1298 struct ocfs2_write_ctxt *wc,
1299 struct ocfs2_write_cluster_desc *desc)
1300 {
1301 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1302 struct ocfs2_unwritten_extent *ue = NULL, *new = NULL;
1303 int ret = 0;
1304
1305 if (!desc->c_needs_zero)
1306 return 0;
1307
1308 retry:
1309 spin_lock(&oi->ip_lock);
1310 /* Needs not to zero no metter buffer or direct. The one who is zero
1311 * the cluster is doing zero. And he will clear unwritten after all
1312 * cluster io finished. */
1313 list_for_each_entry(ue, &oi->ip_unwritten_list, ue_ip_node) {
1314 if (desc->c_cpos == ue->ue_cpos) {
1315 BUG_ON(desc->c_new);
1316 desc->c_needs_zero = 0;
1317 desc->c_clear_unwritten = 0;
1318 goto unlock;
1319 }
1320 }
1321
1322 if (wc->w_type != OCFS2_WRITE_DIRECT)
1323 goto unlock;
1324
1325 if (new == NULL) {
1326 spin_unlock(&oi->ip_lock);
1327 new = kmalloc(sizeof(struct ocfs2_unwritten_extent),
1328 GFP_NOFS);
1329 if (new == NULL) {
1330 ret = -ENOMEM;
1331 goto out;
1332 }
1333 goto retry;
1334 }
1335 /* This direct write will doing zero. */
1336 new->ue_cpos = desc->c_cpos;
1337 new->ue_phys = desc->c_phys;
1338 desc->c_clear_unwritten = 0;
1339 list_add_tail(&new->ue_ip_node, &oi->ip_unwritten_list);
1340 list_add_tail(&new->ue_node, &wc->w_unwritten_list);
1341 wc->w_unwritten_count++;
1342 new = NULL;
1343 unlock:
1344 spin_unlock(&oi->ip_lock);
1345 out:
1346 kfree(new);
1347 return ret;
1348 }
1349
1350 /*
1351 * Populate each single-cluster write descriptor in the write context
1352 * with information about the i/o to be done.
1353 *
1354 * Returns the number of clusters that will have to be allocated, as
1355 * well as a worst case estimate of the number of extent records that
1356 * would have to be created during a write to an unwritten region.
1357 */
ocfs2_populate_write_desc(struct inode * inode,struct ocfs2_write_ctxt * wc,unsigned int * clusters_to_alloc,unsigned int * extents_to_split)1358 static int ocfs2_populate_write_desc(struct inode *inode,
1359 struct ocfs2_write_ctxt *wc,
1360 unsigned int *clusters_to_alloc,
1361 unsigned int *extents_to_split)
1362 {
1363 int ret;
1364 struct ocfs2_write_cluster_desc *desc;
1365 unsigned int num_clusters = 0;
1366 unsigned int ext_flags = 0;
1367 u32 phys = 0;
1368 int i;
1369
1370 *clusters_to_alloc = 0;
1371 *extents_to_split = 0;
1372
1373 for (i = 0; i < wc->w_clen; i++) {
1374 desc = &wc->w_desc[i];
1375 desc->c_cpos = wc->w_cpos + i;
1376
1377 if (num_clusters == 0) {
1378 /*
1379 * Need to look up the next extent record.
1380 */
1381 ret = ocfs2_get_clusters(inode, desc->c_cpos, &phys,
1382 &num_clusters, &ext_flags);
1383 if (ret) {
1384 mlog_errno(ret);
1385 goto out;
1386 }
1387
1388 /* We should already CoW the refcountd extent. */
1389 BUG_ON(ext_flags & OCFS2_EXT_REFCOUNTED);
1390
1391 /*
1392 * Assume worst case - that we're writing in
1393 * the middle of the extent.
1394 *
1395 * We can assume that the write proceeds from
1396 * left to right, in which case the extent
1397 * insert code is smart enough to coalesce the
1398 * next splits into the previous records created.
1399 */
1400 if (ext_flags & OCFS2_EXT_UNWRITTEN)
1401 *extents_to_split = *extents_to_split + 2;
1402 } else if (phys) {
1403 /*
1404 * Only increment phys if it doesn't describe
1405 * a hole.
1406 */
1407 phys++;
1408 }
1409
1410 /*
1411 * If w_first_new_cpos is < UINT_MAX, we have a non-sparse
1412 * file that got extended. w_first_new_cpos tells us
1413 * where the newly allocated clusters are so we can
1414 * zero them.
1415 */
1416 if (desc->c_cpos >= wc->w_first_new_cpos) {
1417 BUG_ON(phys == 0);
1418 desc->c_needs_zero = 1;
1419 }
1420
1421 desc->c_phys = phys;
1422 if (phys == 0) {
1423 desc->c_new = 1;
1424 desc->c_needs_zero = 1;
1425 desc->c_clear_unwritten = 1;
1426 *clusters_to_alloc = *clusters_to_alloc + 1;
1427 }
1428
1429 if (ext_flags & OCFS2_EXT_UNWRITTEN) {
1430 desc->c_clear_unwritten = 1;
1431 desc->c_needs_zero = 1;
1432 }
1433
1434 ret = ocfs2_unwritten_check(inode, wc, desc);
1435 if (ret) {
1436 mlog_errno(ret);
1437 goto out;
1438 }
1439
1440 num_clusters--;
1441 }
1442
1443 ret = 0;
1444 out:
1445 return ret;
1446 }
1447
ocfs2_write_begin_inline(struct address_space * mapping,struct inode * inode,struct ocfs2_write_ctxt * wc)1448 static int ocfs2_write_begin_inline(struct address_space *mapping,
1449 struct inode *inode,
1450 struct ocfs2_write_ctxt *wc)
1451 {
1452 int ret;
1453 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1454 struct folio *folio;
1455 handle_t *handle;
1456 struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
1457
1458 handle = ocfs2_start_trans(osb, OCFS2_INODE_UPDATE_CREDITS);
1459 if (IS_ERR(handle)) {
1460 ret = PTR_ERR(handle);
1461 mlog_errno(ret);
1462 goto out;
1463 }
1464
1465 folio = __filemap_get_folio(mapping, 0,
1466 FGP_LOCK | FGP_ACCESSED | FGP_CREAT, GFP_NOFS);
1467 if (IS_ERR(folio)) {
1468 ocfs2_commit_trans(osb, handle);
1469 ret = PTR_ERR(folio);
1470 mlog_errno(ret);
1471 goto out;
1472 }
1473 /*
1474 * If we don't set w_num_folios then this folio won't get unlocked
1475 * and freed on cleanup of the write context.
1476 */
1477 wc->w_target_folio = folio;
1478 wc->w_folios[0] = folio;
1479 wc->w_num_folios = 1;
1480
1481 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), wc->w_di_bh,
1482 OCFS2_JOURNAL_ACCESS_WRITE);
1483 if (ret) {
1484 ocfs2_commit_trans(osb, handle);
1485
1486 mlog_errno(ret);
1487 goto out;
1488 }
1489
1490 if (!(OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL))
1491 ocfs2_set_inode_data_inline(inode, di);
1492
1493 if (!folio_test_uptodate(folio)) {
1494 ret = ocfs2_read_inline_data(inode, folio, wc->w_di_bh);
1495 if (ret) {
1496 ocfs2_commit_trans(osb, handle);
1497
1498 goto out;
1499 }
1500 }
1501
1502 wc->w_handle = handle;
1503 out:
1504 return ret;
1505 }
1506
ocfs2_size_fits_inline_data(struct buffer_head * di_bh,u64 new_size)1507 int ocfs2_size_fits_inline_data(struct buffer_head *di_bh, u64 new_size)
1508 {
1509 struct ocfs2_dinode *di = (struct ocfs2_dinode *)di_bh->b_data;
1510
1511 if (new_size <= le16_to_cpu(di->id2.i_data.id_count))
1512 return 1;
1513 return 0;
1514 }
1515
ocfs2_try_to_write_inline_data(struct address_space * mapping,struct inode * inode,loff_t pos,size_t len,struct folio * mmap_folio,struct ocfs2_write_ctxt * wc)1516 static int ocfs2_try_to_write_inline_data(struct address_space *mapping,
1517 struct inode *inode, loff_t pos, size_t len,
1518 struct folio *mmap_folio, struct ocfs2_write_ctxt *wc)
1519 {
1520 int ret, written = 0;
1521 loff_t end = pos + len;
1522 struct ocfs2_inode_info *oi = OCFS2_I(inode);
1523 struct ocfs2_dinode *di = NULL;
1524
1525 trace_ocfs2_try_to_write_inline_data((unsigned long long)oi->ip_blkno,
1526 len, (unsigned long long)pos,
1527 oi->ip_dyn_features);
1528
1529 /*
1530 * Handle inodes which already have inline data 1st.
1531 */
1532 if (oi->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1533 if (mmap_folio == NULL &&
1534 ocfs2_size_fits_inline_data(wc->w_di_bh, end))
1535 goto do_inline_write;
1536
1537 /*
1538 * The write won't fit - we have to give this inode an
1539 * inline extent list now.
1540 */
1541 ret = ocfs2_convert_inline_data_to_extents(inode, wc->w_di_bh);
1542 if (ret)
1543 mlog_errno(ret);
1544 goto out;
1545 }
1546
1547 /*
1548 * Check whether the inode can accept inline data.
1549 */
1550 if (oi->ip_clusters != 0 || i_size_read(inode) != 0)
1551 return 0;
1552
1553 /*
1554 * Check whether the write can fit.
1555 */
1556 di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
1557 if (mmap_folio ||
1558 end > ocfs2_max_inline_data_with_xattr(inode->i_sb, di))
1559 return 0;
1560
1561 do_inline_write:
1562 ret = ocfs2_write_begin_inline(mapping, inode, wc);
1563 if (ret) {
1564 mlog_errno(ret);
1565 goto out;
1566 }
1567
1568 /*
1569 * This signals to the caller that the data can be written
1570 * inline.
1571 */
1572 written = 1;
1573 out:
1574 return written ? written : ret;
1575 }
1576
1577 /*
1578 * This function only does anything for file systems which can't
1579 * handle sparse files.
1580 *
1581 * What we want to do here is fill in any hole between the current end
1582 * of allocation and the end of our write. That way the rest of the
1583 * write path can treat it as an non-allocating write, which has no
1584 * special case code for sparse/nonsparse files.
1585 */
ocfs2_expand_nonsparse_inode(struct inode * inode,struct buffer_head * di_bh,loff_t pos,unsigned len,struct ocfs2_write_ctxt * wc)1586 static int ocfs2_expand_nonsparse_inode(struct inode *inode,
1587 struct buffer_head *di_bh,
1588 loff_t pos, unsigned len,
1589 struct ocfs2_write_ctxt *wc)
1590 {
1591 int ret;
1592 loff_t newsize = pos + len;
1593
1594 BUG_ON(ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)));
1595
1596 if (newsize <= i_size_read(inode))
1597 return 0;
1598
1599 ret = ocfs2_extend_no_holes(inode, di_bh, newsize, pos);
1600 if (ret)
1601 mlog_errno(ret);
1602
1603 /* There is no wc if this is call from direct. */
1604 if (wc)
1605 wc->w_first_new_cpos =
1606 ocfs2_clusters_for_bytes(inode->i_sb, i_size_read(inode));
1607
1608 return ret;
1609 }
1610
ocfs2_zero_tail(struct inode * inode,struct buffer_head * di_bh,loff_t pos)1611 static int ocfs2_zero_tail(struct inode *inode, struct buffer_head *di_bh,
1612 loff_t pos)
1613 {
1614 int ret = 0;
1615
1616 BUG_ON(!ocfs2_sparse_alloc(OCFS2_SB(inode->i_sb)));
1617 if (pos > i_size_read(inode))
1618 ret = ocfs2_zero_extend(inode, di_bh, pos);
1619
1620 return ret;
1621 }
1622
ocfs2_write_begin_nolock(struct address_space * mapping,loff_t pos,unsigned len,ocfs2_write_type_t type,struct folio ** foliop,void ** fsdata,struct buffer_head * di_bh,struct folio * mmap_folio)1623 int ocfs2_write_begin_nolock(struct address_space *mapping,
1624 loff_t pos, unsigned len, ocfs2_write_type_t type,
1625 struct folio **foliop, void **fsdata,
1626 struct buffer_head *di_bh, struct folio *mmap_folio)
1627 {
1628 int ret, cluster_of_pages, credits = OCFS2_INODE_UPDATE_CREDITS;
1629 unsigned int clusters_to_alloc, extents_to_split, clusters_need = 0;
1630 struct ocfs2_write_ctxt *wc;
1631 struct inode *inode = mapping->host;
1632 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1633 struct ocfs2_dinode *di;
1634 struct ocfs2_alloc_context *data_ac = NULL;
1635 struct ocfs2_alloc_context *meta_ac = NULL;
1636 handle_t *handle;
1637 struct ocfs2_extent_tree et;
1638 int try_free = 1, ret1;
1639
1640 try_again:
1641 ret = ocfs2_alloc_write_ctxt(&wc, osb, pos, len, type, di_bh);
1642 if (ret) {
1643 mlog_errno(ret);
1644 return ret;
1645 }
1646
1647 if (ocfs2_supports_inline_data(osb)) {
1648 ret = ocfs2_try_to_write_inline_data(mapping, inode, pos, len,
1649 mmap_folio, wc);
1650 if (ret == 1) {
1651 ret = 0;
1652 goto success;
1653 }
1654 if (ret < 0) {
1655 mlog_errno(ret);
1656 goto out;
1657 }
1658 }
1659
1660 /* Direct io change i_size late, should not zero tail here. */
1661 if (type != OCFS2_WRITE_DIRECT) {
1662 if (ocfs2_sparse_alloc(osb))
1663 ret = ocfs2_zero_tail(inode, di_bh, pos);
1664 else
1665 ret = ocfs2_expand_nonsparse_inode(inode, di_bh, pos,
1666 len, wc);
1667 if (ret) {
1668 mlog_errno(ret);
1669 goto out;
1670 }
1671 }
1672
1673 ret = ocfs2_check_range_for_refcount(inode, pos, len);
1674 if (ret < 0) {
1675 mlog_errno(ret);
1676 goto out;
1677 } else if (ret == 1) {
1678 clusters_need = wc->w_clen;
1679 ret = ocfs2_refcount_cow(inode, di_bh,
1680 wc->w_cpos, wc->w_clen, UINT_MAX);
1681 if (ret) {
1682 mlog_errno(ret);
1683 goto out;
1684 }
1685 }
1686
1687 ret = ocfs2_populate_write_desc(inode, wc, &clusters_to_alloc,
1688 &extents_to_split);
1689 if (ret) {
1690 mlog_errno(ret);
1691 goto out;
1692 }
1693 clusters_need += clusters_to_alloc;
1694
1695 di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
1696
1697 trace_ocfs2_write_begin_nolock(
1698 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1699 (long long)i_size_read(inode),
1700 le32_to_cpu(di->i_clusters),
1701 pos, len, type, mmap_folio,
1702 clusters_to_alloc, extents_to_split);
1703
1704 /*
1705 * We set w_target_from, w_target_to here so that
1706 * ocfs2_write_end() knows which range in the target page to
1707 * write out. An allocation requires that we write the entire
1708 * cluster range.
1709 */
1710 if (clusters_to_alloc || extents_to_split) {
1711 /*
1712 * XXX: We are stretching the limits of
1713 * ocfs2_lock_allocators(). It greatly over-estimates
1714 * the work to be done.
1715 */
1716 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode),
1717 wc->w_di_bh);
1718 ret = ocfs2_lock_allocators(inode, &et,
1719 clusters_to_alloc, extents_to_split,
1720 &data_ac, &meta_ac);
1721 if (ret) {
1722 mlog_errno(ret);
1723 goto out;
1724 }
1725
1726 if (data_ac)
1727 data_ac->ac_resv = &OCFS2_I(inode)->ip_la_data_resv;
1728
1729 credits = ocfs2_calc_extend_credits(inode->i_sb,
1730 &di->id2.i_list);
1731 } else if (type == OCFS2_WRITE_DIRECT)
1732 /* direct write needs not to start trans if no extents alloc. */
1733 goto success;
1734
1735 /*
1736 * We have to zero sparse allocated clusters, unwritten extent clusters,
1737 * and non-sparse clusters we just extended. For non-sparse writes,
1738 * we know zeros will only be needed in the first and/or last cluster.
1739 */
1740 if (wc->w_clen && (wc->w_desc[0].c_needs_zero ||
1741 wc->w_desc[wc->w_clen - 1].c_needs_zero))
1742 cluster_of_pages = 1;
1743 else
1744 cluster_of_pages = 0;
1745
1746 ocfs2_set_target_boundaries(osb, wc, pos, len, cluster_of_pages);
1747
1748 handle = ocfs2_start_trans(osb, credits);
1749 if (IS_ERR(handle)) {
1750 ret = PTR_ERR(handle);
1751 mlog_errno(ret);
1752 goto out;
1753 }
1754
1755 wc->w_handle = handle;
1756
1757 if (clusters_to_alloc) {
1758 ret = dquot_alloc_space_nodirty(inode,
1759 ocfs2_clusters_to_bytes(osb->sb, clusters_to_alloc));
1760 if (ret)
1761 goto out_commit;
1762 }
1763
1764 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), wc->w_di_bh,
1765 OCFS2_JOURNAL_ACCESS_WRITE);
1766 if (ret) {
1767 mlog_errno(ret);
1768 goto out_quota;
1769 }
1770
1771 /*
1772 * Fill our folio array first. That way we've grabbed enough so
1773 * that we can zero and flush if we error after adding the
1774 * extent.
1775 */
1776 ret = ocfs2_grab_folios_for_write(mapping, wc, wc->w_cpos, pos, len,
1777 cluster_of_pages, mmap_folio);
1778 if (ret) {
1779 /*
1780 * ocfs2_grab_folios_for_write() returns -EAGAIN if it
1781 * could not lock the target folio. In this case, we exit
1782 * with no error and no target folio. This will trigger
1783 * the caller, page_mkwrite(), to re-try the operation.
1784 */
1785 if (type == OCFS2_WRITE_MMAP && ret == -EAGAIN) {
1786 BUG_ON(wc->w_target_folio);
1787 ret = 0;
1788 goto out_quota;
1789 }
1790
1791 mlog_errno(ret);
1792 goto out_quota;
1793 }
1794
1795 ret = ocfs2_write_cluster_by_desc(mapping, data_ac, meta_ac, wc, pos,
1796 len);
1797 if (ret) {
1798 mlog_errno(ret);
1799 goto out_quota;
1800 }
1801
1802 if (data_ac)
1803 ocfs2_free_alloc_context(data_ac);
1804 if (meta_ac)
1805 ocfs2_free_alloc_context(meta_ac);
1806
1807 success:
1808 if (foliop)
1809 *foliop = wc->w_target_folio;
1810 *fsdata = wc;
1811 return 0;
1812 out_quota:
1813 if (clusters_to_alloc)
1814 dquot_free_space(inode,
1815 ocfs2_clusters_to_bytes(osb->sb, clusters_to_alloc));
1816 out_commit:
1817 ocfs2_commit_trans(osb, handle);
1818
1819 out:
1820 /*
1821 * The mmapped page won't be unlocked in ocfs2_free_write_ctxt(),
1822 * even in case of error here like ENOSPC and ENOMEM. So, we need
1823 * to unlock the target page manually to prevent deadlocks when
1824 * retrying again on ENOSPC, or when returning non-VM_FAULT_LOCKED
1825 * to VM code.
1826 */
1827 if (wc->w_target_locked)
1828 folio_unlock(mmap_folio);
1829
1830 ocfs2_free_write_ctxt(inode, wc);
1831
1832 if (data_ac) {
1833 ocfs2_free_alloc_context(data_ac);
1834 data_ac = NULL;
1835 }
1836 if (meta_ac) {
1837 ocfs2_free_alloc_context(meta_ac);
1838 meta_ac = NULL;
1839 }
1840
1841 if (ret == -ENOSPC && try_free) {
1842 /*
1843 * Try to free some truncate log so that we can have enough
1844 * clusters to allocate.
1845 */
1846 try_free = 0;
1847
1848 ret1 = ocfs2_try_to_free_truncate_log(osb, clusters_need);
1849 if (ret1 == 1)
1850 goto try_again;
1851
1852 if (ret1 < 0)
1853 mlog_errno(ret1);
1854 }
1855
1856 return ret;
1857 }
1858
ocfs2_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,struct folio ** foliop,void ** fsdata)1859 static int ocfs2_write_begin(struct file *file, struct address_space *mapping,
1860 loff_t pos, unsigned len,
1861 struct folio **foliop, void **fsdata)
1862 {
1863 int ret;
1864 struct buffer_head *di_bh = NULL;
1865 struct inode *inode = mapping->host;
1866
1867 ret = ocfs2_inode_lock(inode, &di_bh, 1);
1868 if (ret) {
1869 mlog_errno(ret);
1870 return ret;
1871 }
1872
1873 /*
1874 * Take alloc sem here to prevent concurrent lookups. That way
1875 * the mapping, zeroing and tree manipulation within
1876 * ocfs2_write() will be safe against ->read_folio(). This
1877 * should also serve to lock out allocation from a shared
1878 * writeable region.
1879 */
1880 down_write(&OCFS2_I(inode)->ip_alloc_sem);
1881
1882 ret = ocfs2_write_begin_nolock(mapping, pos, len, OCFS2_WRITE_BUFFER,
1883 foliop, fsdata, di_bh, NULL);
1884 if (ret) {
1885 mlog_errno(ret);
1886 goto out_fail;
1887 }
1888
1889 brelse(di_bh);
1890
1891 return 0;
1892
1893 out_fail:
1894 up_write(&OCFS2_I(inode)->ip_alloc_sem);
1895
1896 brelse(di_bh);
1897 ocfs2_inode_unlock(inode, 1);
1898
1899 return ret;
1900 }
1901
ocfs2_write_end_inline(struct inode * inode,loff_t pos,unsigned len,unsigned * copied,struct ocfs2_dinode * di,struct ocfs2_write_ctxt * wc)1902 static void ocfs2_write_end_inline(struct inode *inode, loff_t pos,
1903 unsigned len, unsigned *copied,
1904 struct ocfs2_dinode *di,
1905 struct ocfs2_write_ctxt *wc)
1906 {
1907 if (unlikely(*copied < len)) {
1908 if (!folio_test_uptodate(wc->w_target_folio)) {
1909 *copied = 0;
1910 return;
1911 }
1912 }
1913
1914 memcpy_from_folio(di->id2.i_data.id_data + pos, wc->w_target_folio,
1915 pos, *copied);
1916
1917 trace_ocfs2_write_end_inline(
1918 (unsigned long long)OCFS2_I(inode)->ip_blkno,
1919 (unsigned long long)pos, *copied,
1920 le16_to_cpu(di->id2.i_data.id_count),
1921 le16_to_cpu(di->i_dyn_features));
1922 }
1923
ocfs2_write_end_nolock(struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,void * fsdata)1924 int ocfs2_write_end_nolock(struct address_space *mapping, loff_t pos,
1925 unsigned len, unsigned copied, void *fsdata)
1926 {
1927 int i, ret;
1928 size_t from, to, start = pos & (PAGE_SIZE - 1);
1929 struct inode *inode = mapping->host;
1930 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
1931 struct ocfs2_write_ctxt *wc = fsdata;
1932 struct ocfs2_dinode *di = (struct ocfs2_dinode *)wc->w_di_bh->b_data;
1933 handle_t *handle = wc->w_handle;
1934
1935 BUG_ON(!list_empty(&wc->w_unwritten_list));
1936
1937 if (handle) {
1938 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode),
1939 wc->w_di_bh, OCFS2_JOURNAL_ACCESS_WRITE);
1940 if (ret) {
1941 copied = ret;
1942 mlog_errno(ret);
1943 goto out;
1944 }
1945 }
1946
1947 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL) {
1948 ocfs2_write_end_inline(inode, pos, len, &copied, di, wc);
1949 goto out_write_size;
1950 }
1951
1952 if (unlikely(copied < len) && wc->w_target_folio) {
1953 loff_t new_isize;
1954
1955 if (!folio_test_uptodate(wc->w_target_folio))
1956 copied = 0;
1957
1958 new_isize = max_t(loff_t, i_size_read(inode), pos + copied);
1959 if (new_isize > folio_pos(wc->w_target_folio))
1960 ocfs2_zero_new_buffers(wc->w_target_folio, start+copied,
1961 start+len);
1962 else {
1963 /*
1964 * When folio is fully beyond new isize (data copy
1965 * failed), do not bother zeroing the folio. Invalidate
1966 * it instead so that writeback does not get confused
1967 * put page & buffer dirty bits into inconsistent
1968 * state.
1969 */
1970 block_invalidate_folio(wc->w_target_folio, 0,
1971 folio_size(wc->w_target_folio));
1972 }
1973 }
1974 if (wc->w_target_folio)
1975 flush_dcache_folio(wc->w_target_folio);
1976
1977 for (i = 0; i < wc->w_num_folios; i++) {
1978 struct folio *folio = wc->w_folios[i];
1979
1980 /* This is the direct io target folio */
1981 if (folio == NULL)
1982 continue;
1983
1984 if (folio == wc->w_target_folio) {
1985 from = wc->w_target_from;
1986 to = wc->w_target_to;
1987
1988 BUG_ON(from > folio_size(folio) ||
1989 to > folio_size(folio) ||
1990 to < from);
1991 } else {
1992 /*
1993 * Pages adjacent to the target (if any) imply
1994 * a hole-filling write in which case we want
1995 * to flush their entire range.
1996 */
1997 from = 0;
1998 to = folio_size(folio);
1999 }
2000
2001 if (folio_buffers(folio)) {
2002 if (handle && ocfs2_should_order_data(inode)) {
2003 loff_t start_byte = folio_pos(folio) + from;
2004 loff_t length = to - from;
2005 ocfs2_jbd2_inode_add_write(handle, inode,
2006 start_byte, length);
2007 }
2008 block_commit_write(folio, from, to);
2009 }
2010 }
2011
2012 out_write_size:
2013 /* Direct io do not update i_size here. */
2014 if (wc->w_type != OCFS2_WRITE_DIRECT) {
2015 pos += copied;
2016 if (pos > i_size_read(inode)) {
2017 i_size_write(inode, pos);
2018 mark_inode_dirty(inode);
2019 }
2020 inode->i_blocks = ocfs2_inode_sector_count(inode);
2021 di->i_size = cpu_to_le64((u64)i_size_read(inode));
2022 inode_set_mtime_to_ts(inode, inode_set_ctime_current(inode));
2023 di->i_mtime = di->i_ctime = cpu_to_le64(inode_get_mtime_sec(inode));
2024 di->i_mtime_nsec = di->i_ctime_nsec = cpu_to_le32(inode_get_mtime_nsec(inode));
2025 if (handle)
2026 ocfs2_update_inode_fsync_trans(handle, inode, 1);
2027 }
2028 if (handle)
2029 ocfs2_journal_dirty(handle, wc->w_di_bh);
2030
2031 out:
2032 /* unlock pages before dealloc since it needs acquiring j_trans_barrier
2033 * lock, or it will cause a deadlock since journal commit threads holds
2034 * this lock and will ask for the page lock when flushing the data.
2035 * put it here to preserve the unlock order.
2036 */
2037 ocfs2_unlock_folios(wc);
2038
2039 if (handle)
2040 ocfs2_commit_trans(osb, handle);
2041
2042 ocfs2_run_deallocs(osb, &wc->w_dealloc);
2043
2044 brelse(wc->w_di_bh);
2045 kfree(wc);
2046
2047 return copied;
2048 }
2049
ocfs2_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct folio * folio,void * fsdata)2050 static int ocfs2_write_end(struct file *file, struct address_space *mapping,
2051 loff_t pos, unsigned len, unsigned copied,
2052 struct folio *folio, void *fsdata)
2053 {
2054 int ret;
2055 struct inode *inode = mapping->host;
2056
2057 ret = ocfs2_write_end_nolock(mapping, pos, len, copied, fsdata);
2058
2059 up_write(&OCFS2_I(inode)->ip_alloc_sem);
2060 ocfs2_inode_unlock(inode, 1);
2061
2062 return ret;
2063 }
2064
2065 struct ocfs2_dio_write_ctxt {
2066 struct list_head dw_zero_list;
2067 unsigned dw_zero_count;
2068 int dw_orphaned;
2069 pid_t dw_writer_pid;
2070 };
2071
2072 static struct ocfs2_dio_write_ctxt *
ocfs2_dio_alloc_write_ctx(struct buffer_head * bh,int * alloc)2073 ocfs2_dio_alloc_write_ctx(struct buffer_head *bh, int *alloc)
2074 {
2075 struct ocfs2_dio_write_ctxt *dwc = NULL;
2076
2077 if (bh->b_private)
2078 return bh->b_private;
2079
2080 dwc = kmalloc(sizeof(struct ocfs2_dio_write_ctxt), GFP_NOFS);
2081 if (dwc == NULL)
2082 return NULL;
2083 INIT_LIST_HEAD(&dwc->dw_zero_list);
2084 dwc->dw_zero_count = 0;
2085 dwc->dw_orphaned = 0;
2086 dwc->dw_writer_pid = task_pid_nr(current);
2087 bh->b_private = dwc;
2088 *alloc = 1;
2089
2090 return dwc;
2091 }
2092
ocfs2_dio_free_write_ctx(struct inode * inode,struct ocfs2_dio_write_ctxt * dwc)2093 static void ocfs2_dio_free_write_ctx(struct inode *inode,
2094 struct ocfs2_dio_write_ctxt *dwc)
2095 {
2096 ocfs2_free_unwritten_list(inode, &dwc->dw_zero_list);
2097 kfree(dwc);
2098 }
2099
2100 /*
2101 * TODO: Make this into a generic get_blocks function.
2102 *
2103 * From do_direct_io in direct-io.c:
2104 * "So what we do is to permit the ->get_blocks function to populate
2105 * bh.b_size with the size of IO which is permitted at this offset and
2106 * this i_blkbits."
2107 *
2108 * This function is called directly from get_more_blocks in direct-io.c.
2109 *
2110 * called like this: dio->get_blocks(dio->inode, fs_startblk,
2111 * fs_count, map_bh, dio->rw == WRITE);
2112 */
ocfs2_dio_wr_get_block(struct inode * inode,sector_t iblock,struct buffer_head * bh_result,int create)2113 static int ocfs2_dio_wr_get_block(struct inode *inode, sector_t iblock,
2114 struct buffer_head *bh_result, int create)
2115 {
2116 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2117 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2118 struct ocfs2_write_ctxt *wc;
2119 struct ocfs2_write_cluster_desc *desc = NULL;
2120 struct ocfs2_dio_write_ctxt *dwc = NULL;
2121 struct buffer_head *di_bh = NULL;
2122 u64 p_blkno;
2123 unsigned int i_blkbits = inode->i_sb->s_blocksize_bits;
2124 loff_t pos = iblock << i_blkbits;
2125 sector_t endblk = (i_size_read(inode) - 1) >> i_blkbits;
2126 unsigned len, total_len = bh_result->b_size;
2127 int ret = 0, first_get_block = 0;
2128
2129 len = osb->s_clustersize - (pos & (osb->s_clustersize - 1));
2130 len = min(total_len, len);
2131
2132 /*
2133 * bh_result->b_size is count in get_more_blocks according to write
2134 * "pos" and "end", we need map twice to return different buffer state:
2135 * 1. area in file size, not set NEW;
2136 * 2. area out file size, set NEW.
2137 *
2138 * iblock endblk
2139 * |--------|---------|---------|---------
2140 * |<-------area in file------->|
2141 */
2142
2143 if ((iblock <= endblk) &&
2144 ((iblock + ((len - 1) >> i_blkbits)) > endblk))
2145 len = (endblk - iblock + 1) << i_blkbits;
2146
2147 mlog(0, "get block of %lu at %llu:%u req %u\n",
2148 inode->i_ino, pos, len, total_len);
2149
2150 /*
2151 * Because we need to change file size in ocfs2_dio_end_io_write(), or
2152 * we may need to add it to orphan dir. So can not fall to fast path
2153 * while file size will be changed.
2154 */
2155 if (pos + total_len <= i_size_read(inode)) {
2156
2157 /* This is the fast path for re-write. */
2158 ret = ocfs2_lock_get_block(inode, iblock, bh_result, create);
2159 if (buffer_mapped(bh_result) &&
2160 !buffer_new(bh_result) &&
2161 ret == 0)
2162 goto out;
2163
2164 /* Clear state set by ocfs2_get_block. */
2165 bh_result->b_state = 0;
2166 }
2167
2168 dwc = ocfs2_dio_alloc_write_ctx(bh_result, &first_get_block);
2169 if (unlikely(dwc == NULL)) {
2170 ret = -ENOMEM;
2171 mlog_errno(ret);
2172 goto out;
2173 }
2174
2175 if (ocfs2_clusters_for_bytes(inode->i_sb, pos + total_len) >
2176 ocfs2_clusters_for_bytes(inode->i_sb, i_size_read(inode)) &&
2177 !dwc->dw_orphaned) {
2178 /*
2179 * when we are going to alloc extents beyond file size, add the
2180 * inode to orphan dir, so we can recall those spaces when
2181 * system crashed during write.
2182 */
2183 ret = ocfs2_add_inode_to_orphan(osb, inode);
2184 if (ret < 0) {
2185 mlog_errno(ret);
2186 goto out;
2187 }
2188 dwc->dw_orphaned = 1;
2189 }
2190
2191 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2192 if (ret) {
2193 mlog_errno(ret);
2194 goto out;
2195 }
2196
2197 down_write(&oi->ip_alloc_sem);
2198
2199 if (first_get_block) {
2200 if (ocfs2_sparse_alloc(osb))
2201 ret = ocfs2_zero_tail(inode, di_bh, pos);
2202 else
2203 ret = ocfs2_expand_nonsparse_inode(inode, di_bh, pos,
2204 total_len, NULL);
2205 if (ret < 0) {
2206 mlog_errno(ret);
2207 goto unlock;
2208 }
2209 }
2210
2211 ret = ocfs2_write_begin_nolock(inode->i_mapping, pos, len,
2212 OCFS2_WRITE_DIRECT, NULL,
2213 (void **)&wc, di_bh, NULL);
2214 if (ret) {
2215 mlog_errno(ret);
2216 goto unlock;
2217 }
2218
2219 desc = &wc->w_desc[0];
2220
2221 p_blkno = ocfs2_clusters_to_blocks(inode->i_sb, desc->c_phys);
2222 BUG_ON(p_blkno == 0);
2223 p_blkno += iblock & (u64)(ocfs2_clusters_to_blocks(inode->i_sb, 1) - 1);
2224
2225 map_bh(bh_result, inode->i_sb, p_blkno);
2226 bh_result->b_size = len;
2227 if (desc->c_needs_zero)
2228 set_buffer_new(bh_result);
2229
2230 if (iblock > endblk)
2231 set_buffer_new(bh_result);
2232
2233 /* May sleep in end_io. It should not happen in a irq context. So defer
2234 * it to dio work queue. */
2235 set_buffer_defer_completion(bh_result);
2236
2237 if (!list_empty(&wc->w_unwritten_list)) {
2238 struct ocfs2_unwritten_extent *ue = NULL;
2239
2240 ue = list_first_entry(&wc->w_unwritten_list,
2241 struct ocfs2_unwritten_extent,
2242 ue_node);
2243 BUG_ON(ue->ue_cpos != desc->c_cpos);
2244 /* The physical address may be 0, fill it. */
2245 ue->ue_phys = desc->c_phys;
2246
2247 list_splice_tail_init(&wc->w_unwritten_list, &dwc->dw_zero_list);
2248 dwc->dw_zero_count += wc->w_unwritten_count;
2249 }
2250
2251 ret = ocfs2_write_end_nolock(inode->i_mapping, pos, len, len, wc);
2252 BUG_ON(ret != len);
2253 ret = 0;
2254 unlock:
2255 up_write(&oi->ip_alloc_sem);
2256 ocfs2_inode_unlock(inode, 1);
2257 brelse(di_bh);
2258 out:
2259 return ret;
2260 }
2261
ocfs2_dio_end_io_write(struct inode * inode,struct ocfs2_dio_write_ctxt * dwc,loff_t offset,ssize_t bytes)2262 static int ocfs2_dio_end_io_write(struct inode *inode,
2263 struct ocfs2_dio_write_ctxt *dwc,
2264 loff_t offset,
2265 ssize_t bytes)
2266 {
2267 struct ocfs2_cached_dealloc_ctxt dealloc;
2268 struct ocfs2_extent_tree et;
2269 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2270 struct ocfs2_inode_info *oi = OCFS2_I(inode);
2271 struct ocfs2_unwritten_extent *ue = NULL;
2272 struct buffer_head *di_bh = NULL;
2273 struct ocfs2_dinode *di;
2274 struct ocfs2_alloc_context *data_ac = NULL;
2275 struct ocfs2_alloc_context *meta_ac = NULL;
2276 handle_t *handle = NULL;
2277 loff_t end = offset + bytes;
2278 int ret = 0, credits = 0;
2279
2280 ocfs2_init_dealloc_ctxt(&dealloc);
2281
2282 /* We do clear unwritten, delete orphan, change i_size here. If neither
2283 * of these happen, we can skip all this. */
2284 if (list_empty(&dwc->dw_zero_list) &&
2285 end <= i_size_read(inode) &&
2286 !dwc->dw_orphaned)
2287 goto out;
2288
2289 ret = ocfs2_inode_lock(inode, &di_bh, 1);
2290 if (ret < 0) {
2291 mlog_errno(ret);
2292 goto out;
2293 }
2294
2295 down_write(&oi->ip_alloc_sem);
2296
2297 /* Delete orphan before acquire i_rwsem. */
2298 if (dwc->dw_orphaned) {
2299 BUG_ON(dwc->dw_writer_pid != task_pid_nr(current));
2300
2301 end = end > i_size_read(inode) ? end : 0;
2302
2303 ret = ocfs2_del_inode_from_orphan(osb, inode, di_bh,
2304 !!end, end);
2305 if (ret < 0)
2306 mlog_errno(ret);
2307 }
2308
2309 di = (struct ocfs2_dinode *)di_bh->b_data;
2310
2311 ocfs2_init_dinode_extent_tree(&et, INODE_CACHE(inode), di_bh);
2312
2313 /* Attach dealloc with extent tree in case that we may reuse extents
2314 * which are already unlinked from current extent tree due to extent
2315 * rotation and merging.
2316 */
2317 et.et_dealloc = &dealloc;
2318
2319 ret = ocfs2_lock_allocators(inode, &et, 0, dwc->dw_zero_count*2,
2320 &data_ac, &meta_ac);
2321 if (ret) {
2322 mlog_errno(ret);
2323 goto unlock;
2324 }
2325
2326 credits = ocfs2_calc_extend_credits(inode->i_sb, &di->id2.i_list);
2327
2328 handle = ocfs2_start_trans(osb, credits);
2329 if (IS_ERR(handle)) {
2330 ret = PTR_ERR(handle);
2331 mlog_errno(ret);
2332 goto unlock;
2333 }
2334 ret = ocfs2_journal_access_di(handle, INODE_CACHE(inode), di_bh,
2335 OCFS2_JOURNAL_ACCESS_WRITE);
2336 if (ret) {
2337 mlog_errno(ret);
2338 goto commit;
2339 }
2340
2341 list_for_each_entry(ue, &dwc->dw_zero_list, ue_node) {
2342 ret = ocfs2_assure_trans_credits(handle, credits);
2343 if (ret < 0) {
2344 mlog_errno(ret);
2345 break;
2346 }
2347 ret = ocfs2_mark_extent_written(inode, &et, handle,
2348 ue->ue_cpos, 1,
2349 ue->ue_phys,
2350 meta_ac, &dealloc);
2351 if (ret < 0) {
2352 mlog_errno(ret);
2353 break;
2354 }
2355 }
2356
2357 if (end > i_size_read(inode)) {
2358 ret = ocfs2_set_inode_size(handle, inode, di_bh, end);
2359 if (ret < 0)
2360 mlog_errno(ret);
2361 }
2362 commit:
2363 ocfs2_commit_trans(osb, handle);
2364 unlock:
2365 up_write(&oi->ip_alloc_sem);
2366 ocfs2_inode_unlock(inode, 1);
2367 brelse(di_bh);
2368 out:
2369 if (data_ac)
2370 ocfs2_free_alloc_context(data_ac);
2371 if (meta_ac)
2372 ocfs2_free_alloc_context(meta_ac);
2373 ocfs2_run_deallocs(osb, &dealloc);
2374 ocfs2_dio_free_write_ctx(inode, dwc);
2375
2376 return ret;
2377 }
2378
2379 /*
2380 * ocfs2_dio_end_io is called by the dio core when a dio is finished. We're
2381 * particularly interested in the aio/dio case. We use the rw_lock DLM lock
2382 * to protect io on one node from truncation on another.
2383 */
ocfs2_dio_end_io(struct kiocb * iocb,loff_t offset,ssize_t bytes,void * private)2384 static int ocfs2_dio_end_io(struct kiocb *iocb,
2385 loff_t offset,
2386 ssize_t bytes,
2387 void *private)
2388 {
2389 struct inode *inode = file_inode(iocb->ki_filp);
2390 int level;
2391 int ret = 0;
2392
2393 /* this io's submitter should not have unlocked this before we could */
2394 BUG_ON(!ocfs2_iocb_is_rw_locked(iocb));
2395
2396 if (bytes <= 0)
2397 mlog_ratelimited(ML_ERROR, "Direct IO failed, bytes = %lld",
2398 (long long)bytes);
2399 if (private) {
2400 if (bytes > 0)
2401 ret = ocfs2_dio_end_io_write(inode, private, offset,
2402 bytes);
2403 else
2404 ocfs2_dio_free_write_ctx(inode, private);
2405 }
2406
2407 ocfs2_iocb_clear_rw_locked(iocb);
2408
2409 level = ocfs2_iocb_rw_locked_level(iocb);
2410 ocfs2_rw_unlock(inode, level);
2411 return ret;
2412 }
2413
ocfs2_direct_IO(struct kiocb * iocb,struct iov_iter * iter)2414 static ssize_t ocfs2_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
2415 {
2416 struct file *file = iocb->ki_filp;
2417 struct inode *inode = file->f_mapping->host;
2418 struct ocfs2_super *osb = OCFS2_SB(inode->i_sb);
2419 get_block_t *get_block;
2420
2421 /*
2422 * Fallback to buffered I/O if we see an inode without
2423 * extents.
2424 */
2425 if (OCFS2_I(inode)->ip_dyn_features & OCFS2_INLINE_DATA_FL)
2426 return 0;
2427
2428 /* Fallback to buffered I/O if we do not support append dio. */
2429 if (iocb->ki_pos + iter->count > i_size_read(inode) &&
2430 !ocfs2_supports_append_dio(osb))
2431 return 0;
2432
2433 if (iov_iter_rw(iter) == READ)
2434 get_block = ocfs2_lock_get_block;
2435 else
2436 get_block = ocfs2_dio_wr_get_block;
2437
2438 return __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
2439 iter, get_block,
2440 ocfs2_dio_end_io, 0);
2441 }
2442
2443 const struct address_space_operations ocfs2_aops = {
2444 .dirty_folio = block_dirty_folio,
2445 .read_folio = ocfs2_read_folio,
2446 .readahead = ocfs2_readahead,
2447 .writepages = ocfs2_writepages,
2448 .write_begin = ocfs2_write_begin,
2449 .write_end = ocfs2_write_end,
2450 .bmap = ocfs2_bmap,
2451 .direct_IO = ocfs2_direct_IO,
2452 .invalidate_folio = block_invalidate_folio,
2453 .release_folio = ocfs2_release_folio,
2454 .migrate_folio = buffer_migrate_folio,
2455 .is_partially_uptodate = block_is_partially_uptodate,
2456 .error_remove_folio = generic_error_remove_folio,
2457 };
2458