]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - fs/gfs2/ops_address.c
[GFS2] Set d_ops for root inode
[mirror_ubuntu-jammy-kernel.git] / fs / gfs2 / ops_address.c
CommitLineData
b3b94faa
DT
1/*
2 * Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
3 * Copyright (C) 2004-2005 Red Hat, Inc. All rights reserved.
4 *
5 * This copyrighted material is made available to anyone wishing to use,
6 * modify, copy, or redistribute it subject to the terms and conditions
7 * of the GNU General Public License v.2.
8 */
9
10#include <linux/sched.h>
11#include <linux/slab.h>
12#include <linux/spinlock.h>
13#include <linux/completion.h>
14#include <linux/buffer_head.h>
15#include <linux/pagemap.h>
9b124fbb 16#include <linux/mpage.h>
d1665e41 17#include <linux/fs.h>
5c676f6d 18#include <linux/gfs2_ondisk.h>
b3b94faa
DT
19#include <asm/semaphore.h>
20
21#include "gfs2.h"
5c676f6d
SW
22#include "lm_interface.h"
23#include "incore.h"
b3b94faa
DT
24#include "bmap.h"
25#include "glock.h"
26#include "inode.h"
b3b94faa
DT
27#include "log.h"
28#include "meta_io.h"
29#include "ops_address.h"
30#include "page.h"
31#include "quota.h"
32#include "trans.h"
18ec7d5c 33#include "rgrp.h"
61a30dcb 34#include "ops_file.h"
5c676f6d 35#include "util.h"
b3b94faa
DT
36
37/**
4ff14670 38 * gfs2_get_block - Fills in a buffer head with details about a block
b3b94faa
DT
39 * @inode: The inode
40 * @lblock: The block number to look up
41 * @bh_result: The buffer head to return the result in
42 * @create: Non-zero if we may add block to the file
43 *
44 * Returns: errno
45 */
46
4ff14670
SW
47int gfs2_get_block(struct inode *inode, sector_t lblock,
48 struct buffer_head *bh_result, int create)
b3b94faa 49{
5c676f6d 50 struct gfs2_inode *ip = inode->u.generic_ip;
b3b94faa
DT
51 int new = create;
52 uint64_t dblock;
53 int error;
54
55 error = gfs2_block_map(ip, lblock, &new, &dblock, NULL);
56 if (error)
57 return error;
58
59 if (!dblock)
60 return 0;
61
62 map_bh(bh_result, inode->i_sb, dblock);
63 if (new)
64 set_buffer_new(bh_result);
65
66 return 0;
67}
68
69/**
70 * get_block_noalloc - Fills in a buffer head with details about a block
71 * @inode: The inode
72 * @lblock: The block number to look up
73 * @bh_result: The buffer head to return the result in
74 * @create: Non-zero if we may add block to the file
75 *
76 * Returns: errno
77 */
78
79static int get_block_noalloc(struct inode *inode, sector_t lblock,
80 struct buffer_head *bh_result, int create)
81{
5c676f6d 82 struct gfs2_inode *ip = inode->u.generic_ip;
b3b94faa
DT
83 int new = 0;
84 uint64_t dblock;
85 int error;
86
87 error = gfs2_block_map(ip, lblock, &new, &dblock, NULL);
88 if (error)
89 return error;
90
91 if (dblock)
92 map_bh(bh_result, inode->i_sb, dblock);
93 else if (gfs2_assert_withdraw(ip->i_sbd, !create))
94 error = -EIO;
95
96 return error;
97}
98
b3b94faa
DT
99/**
100 * gfs2_writepage - Write complete page
101 * @page: Page to write
102 *
103 * Returns: errno
104 *
18ec7d5c
SW
105 * Some of this is copied from block_write_full_page() although we still
106 * call it to do most of the work.
b3b94faa
DT
107 */
108
109static int gfs2_writepage(struct page *page, struct writeback_control *wbc)
110{
18ec7d5c 111 struct inode *inode = page->mapping->host;
5c676f6d 112 struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
b3b94faa 113 struct gfs2_sbd *sdp = ip->i_sbd;
18ec7d5c
SW
114 loff_t i_size = i_size_read(inode);
115 pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT;
116 unsigned offset;
b3b94faa 117 int error;
18ec7d5c 118 int done_trans = 0;
b3b94faa 119
b3b94faa
DT
120 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_held_excl(ip->i_gl))) {
121 unlock_page(page);
122 return -EIO;
123 }
5c676f6d 124 if (current->journal_info)
18ec7d5c
SW
125 goto out_ignore;
126
127 /* Is the page fully outside i_size? (truncate in progress) */
128 offset = i_size & (PAGE_CACHE_SIZE-1);
d2d7b8a2 129 if (page->index > end_index || (page->index == end_index && !offset)) {
18ec7d5c 130 page->mapping->a_ops->invalidatepage(page, 0);
b3b94faa 131 unlock_page(page);
18ec7d5c 132 return 0; /* don't care */
b3b94faa
DT
133 }
134
18ec7d5c
SW
135 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED || gfs2_is_jdata(ip)) {
136 error = gfs2_trans_begin(sdp, RES_DINODE + 1, 0);
137 if (error)
138 goto out_ignore;
139 gfs2_page_add_databufs(ip, page, 0, sdp->sd_vfs->s_blocksize-1);
140 done_trans = 1;
141 }
18ec7d5c
SW
142 error = block_write_full_page(page, get_block_noalloc, wbc);
143 if (done_trans)
144 gfs2_trans_end(sdp);
b3b94faa 145 gfs2_meta_cache_flush(ip);
b3b94faa 146 return error;
18ec7d5c
SW
147
148out_ignore:
149 redirty_page_for_writepage(wbc, page);
150 unlock_page(page);
151 return 0;
b3b94faa
DT
152}
153
154/**
155 * stuffed_readpage - Fill in a Linux page with stuffed file data
156 * @ip: the inode
157 * @page: the page
158 *
159 * Returns: errno
160 */
161
162static int stuffed_readpage(struct gfs2_inode *ip, struct page *page)
163{
164 struct buffer_head *dibh;
165 void *kaddr;
166 int error;
167
168 error = gfs2_meta_inode_buffer(ip, &dibh);
169 if (error)
170 return error;
171
5c4e9e03 172 kaddr = kmap_atomic(page, KM_USER0);
b3b94faa
DT
173 memcpy((char *)kaddr,
174 dibh->b_data + sizeof(struct gfs2_dinode),
175 ip->i_di.di_size);
176 memset((char *)kaddr + ip->i_di.di_size,
177 0,
178 PAGE_CACHE_SIZE - ip->i_di.di_size);
5c4e9e03 179 kunmap_atomic(page, KM_USER0);
b3b94faa
DT
180
181 brelse(dibh);
182
183 SetPageUptodate(page);
184
185 return 0;
186}
187
188static int zero_readpage(struct page *page)
189{
190 void *kaddr;
191
5c4e9e03 192 kaddr = kmap_atomic(page, KM_USER0);
b3b94faa 193 memset(kaddr, 0, PAGE_CACHE_SIZE);
5c4e9e03 194 kunmap_atomic(page, KM_USER0);
b3b94faa
DT
195
196 SetPageUptodate(page);
197 unlock_page(page);
198
199 return 0;
200}
201
b3b94faa
DT
202/**
203 * gfs2_readpage - readpage with locking
18ec7d5c
SW
204 * @file: The file to read a page for. N.B. This may be NULL if we are
205 * reading an internal file.
b3b94faa
DT
206 * @page: The page to read
207 *
208 * Returns: errno
209 */
210
211static int gfs2_readpage(struct file *file, struct page *page)
212{
5c676f6d 213 struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
b3b94faa 214 struct gfs2_sbd *sdp = ip->i_sbd;
18ec7d5c 215 struct gfs2_holder gh;
b3b94faa
DT
216 int error;
217
61a30dcb 218 if (file != &gfs2_internal_file_sentinal) {
fe1bdedc 219 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME|GL_AOP, &gh);
61a30dcb
SW
220 error = gfs2_glock_nq_m_atime(1, &gh);
221 if (error)
222 goto out_unlock;
223 }
b3b94faa 224
18ec7d5c
SW
225 if (gfs2_is_stuffed(ip)) {
226 if (!page->index) {
227 error = stuffed_readpage(ip, page);
228 unlock_page(page);
b3b94faa 229 } else
18ec7d5c 230 error = zero_readpage(page);
b3b94faa 231 } else
18ec7d5c 232 error = mpage_readpage(page, gfs2_get_block);
b3b94faa
DT
233
234 if (unlikely(test_bit(SDF_SHUTDOWN, &sdp->sd_flags)))
235 error = -EIO;
236
61a30dcb
SW
237 if (file != &gfs2_internal_file_sentinal) {
238 gfs2_glock_dq_m(1, &gh);
239 gfs2_holder_uninit(&gh);
240 }
18ec7d5c 241out:
b3b94faa 242 return error;
18ec7d5c
SW
243out_unlock:
244 unlock_page(page);
245 goto out;
b3b94faa
DT
246}
247
248/**
249 * gfs2_prepare_write - Prepare to write a page to a file
250 * @file: The file to write to
251 * @page: The page which is to be prepared for writing
252 * @from: From (byte range within page)
253 * @to: To (byte range within page)
254 *
255 * Returns: errno
256 */
257
258static int gfs2_prepare_write(struct file *file, struct page *page,
259 unsigned from, unsigned to)
260{
5c676f6d 261 struct gfs2_inode *ip = page->mapping->host->u.generic_ip;
b3b94faa 262 struct gfs2_sbd *sdp = ip->i_sbd;
18ec7d5c
SW
263 unsigned int data_blocks, ind_blocks, rblocks;
264 int alloc_required;
b3b94faa 265 int error = 0;
18ec7d5c
SW
266 loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + from;
267 loff_t end = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to;
268 struct gfs2_alloc *al;
b3b94faa 269
fe1bdedc 270 gfs2_holder_init(ip->i_gl, LM_ST_EXCLUSIVE, GL_ATIME|GL_AOP, &ip->i_gh);
18ec7d5c
SW
271 error = gfs2_glock_nq_m_atime(1, &ip->i_gh);
272 if (error)
273 goto out_uninit;
b3b94faa 274
18ec7d5c
SW
275 gfs2_write_calc_reserv(ip, to - from, &data_blocks, &ind_blocks);
276
277 error = gfs2_write_alloc_required(ip, pos, from - to, &alloc_required);
278 if (error)
279 goto out_unlock;
b3b94faa 280
18ec7d5c
SW
281
282 if (alloc_required) {
283 al = gfs2_alloc_get(ip);
284
285 error = gfs2_quota_lock(ip, NO_QUOTA_CHANGE, NO_QUOTA_CHANGE);
286 if (error)
287 goto out_alloc_put;
288
289 error = gfs2_quota_check(ip, ip->i_di.di_uid, ip->i_di.di_gid);
290 if (error)
291 goto out_qunlock;
292
293 al->al_requested = data_blocks + ind_blocks;
294 error = gfs2_inplace_reserve(ip);
295 if (error)
296 goto out_qunlock;
297 }
298
299 rblocks = RES_DINODE + ind_blocks;
300 if (gfs2_is_jdata(ip))
301 rblocks += data_blocks ? data_blocks : 1;
302 if (ind_blocks || data_blocks)
303 rblocks += RES_STATFS + RES_QUOTA;
304
305 error = gfs2_trans_begin(sdp, rblocks, 0);
306 if (error)
307 goto out;
308
309 if (gfs2_is_stuffed(ip)) {
310 if (end > sdp->sd_sb.sb_bsize - sizeof(struct gfs2_dinode)) {
568f4c96
SW
311 error = gfs2_unstuff_dinode(ip, gfs2_unstuffer_page,
312 page);
5c4e9e03
SW
313 if (error == 0)
314 goto prepare_write;
315 } else if (!PageUptodate(page))
b3b94faa 316 error = stuffed_readpage(ip, page);
5c4e9e03 317 goto out;
18ec7d5c
SW
318 }
319
5c4e9e03 320prepare_write:
18ec7d5c
SW
321 error = block_prepare_write(page, from, to, gfs2_get_block);
322
323out:
324 if (error) {
325 gfs2_trans_end(sdp);
326 if (alloc_required) {
327 gfs2_inplace_release(ip);
328out_qunlock:
329 gfs2_quota_unlock(ip);
330out_alloc_put:
331 gfs2_alloc_put(ip);
332 }
333out_unlock:
334 gfs2_glock_dq_m(1, &ip->i_gh);
335out_uninit:
336 gfs2_holder_uninit(&ip->i_gh);
337 }
b3b94faa
DT
338
339 return error;
340}
341
342/**
343 * gfs2_commit_write - Commit write to a file
344 * @file: The file to write to
345 * @page: The page containing the data
346 * @from: From (byte range within page)
347 * @to: To (byte range within page)
348 *
349 * Returns: errno
350 */
351
352static int gfs2_commit_write(struct file *file, struct page *page,
353 unsigned from, unsigned to)
354{
355 struct inode *inode = page->mapping->host;
5c676f6d 356 struct gfs2_inode *ip = inode->u.generic_ip;
b3b94faa 357 struct gfs2_sbd *sdp = ip->i_sbd;
18ec7d5c
SW
358 int error = -EOPNOTSUPP;
359 struct buffer_head *dibh;
360 struct gfs2_alloc *al = &ip->i_alloc;;
b3b94faa 361
18ec7d5c
SW
362 if (gfs2_assert_withdraw(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)))
363 goto fail_nounlock;
364
365 error = gfs2_meta_inode_buffer(ip, &dibh);
366 if (error)
367 goto fail_endtrans;
368
369 gfs2_trans_add_bh(ip->i_gl, dibh, 1);
370
b3b94faa 371 if (gfs2_is_stuffed(ip)) {
b3b94faa
DT
372 uint64_t file_size;
373 void *kaddr;
374
375 file_size = ((uint64_t)page->index << PAGE_CACHE_SHIFT) + to;
376
18ec7d5c 377 kaddr = kmap_atomic(page, KM_USER0);
b3b94faa 378 memcpy(dibh->b_data + sizeof(struct gfs2_dinode) + from,
18ec7d5c
SW
379 (char *)kaddr + from, to - from);
380 kunmap_atomic(page, KM_USER0);
b3b94faa
DT
381
382 SetPageUptodate(page);
383
384 if (inode->i_size < file_size)
385 i_size_write(inode, file_size);
386 } else {
568f4c96
SW
387 if (sdp->sd_args.ar_data == GFS2_DATA_ORDERED ||
388 gfs2_is_jdata(ip))
257f9b4e 389 gfs2_page_add_databufs(ip, page, from, to);
b3b94faa
DT
390 error = generic_commit_write(file, page, from, to);
391 if (error)
392 goto fail;
393 }
394
18ec7d5c
SW
395 if (ip->i_di.di_size < inode->i_size)
396 ip->i_di.di_size = inode->i_size;
397
398 gfs2_dinode_out(&ip->i_di, dibh->b_data);
399 brelse(dibh);
400 gfs2_trans_end(sdp);
401 if (al->al_requested) {
402 gfs2_inplace_release(ip);
403 gfs2_quota_unlock(ip);
404 gfs2_alloc_put(ip);
405 }
406 gfs2_glock_dq_m(1, &ip->i_gh);
407 gfs2_holder_uninit(&ip->i_gh);
b3b94faa
DT
408 return 0;
409
18ec7d5c
SW
410fail:
411 brelse(dibh);
412fail_endtrans:
413 gfs2_trans_end(sdp);
414 if (al->al_requested) {
415 gfs2_inplace_release(ip);
416 gfs2_quota_unlock(ip);
417 gfs2_alloc_put(ip);
418 }
419 gfs2_glock_dq_m(1, &ip->i_gh);
420 gfs2_holder_uninit(&ip->i_gh);
421fail_nounlock:
b3b94faa 422 ClearPageUptodate(page);
b3b94faa
DT
423 return error;
424}
425
426/**
427 * gfs2_bmap - Block map function
428 * @mapping: Address space info
429 * @lblock: The block to map
430 *
431 * Returns: The disk address for the block or 0 on hole or error
432 */
433
434static sector_t gfs2_bmap(struct address_space *mapping, sector_t lblock)
435{
5c676f6d 436 struct gfs2_inode *ip = mapping->host->u.generic_ip;
b3b94faa
DT
437 struct gfs2_holder i_gh;
438 sector_t dblock = 0;
439 int error;
440
b3b94faa
DT
441 error = gfs2_glock_nq_init(ip->i_gl, LM_ST_SHARED, LM_FLAG_ANY, &i_gh);
442 if (error)
443 return 0;
444
445 if (!gfs2_is_stuffed(ip))
4ff14670 446 dblock = generic_block_bmap(mapping, lblock, gfs2_get_block);
b3b94faa
DT
447
448 gfs2_glock_dq_uninit(&i_gh);
449
450 return dblock;
451}
452
453static void discard_buffer(struct gfs2_sbd *sdp, struct buffer_head *bh)
454{
64fb4eb7 455 struct gfs2_bufdata *bd;
b3b94faa
DT
456
457 gfs2_log_lock(sdp);
5c676f6d 458 bd = bh->b_private;
64fb4eb7
SW
459 if (bd) {
460 bd->bd_bh = NULL;
5c676f6d 461 bh->b_private = NULL;
b3b94faa
DT
462 gfs2_log_unlock(sdp);
463 brelse(bh);
464 } else
465 gfs2_log_unlock(sdp);
466
467 lock_buffer(bh);
468 clear_buffer_dirty(bh);
469 bh->b_bdev = NULL;
470 clear_buffer_mapped(bh);
471 clear_buffer_req(bh);
472 clear_buffer_new(bh);
473 clear_buffer_delay(bh);
474 unlock_buffer(bh);
475}
476
8628de05 477static void gfs2_invalidatepage(struct page *page, unsigned long offset)
b3b94faa 478{
5c676f6d 479 struct gfs2_sbd *sdp = page->mapping->host->i_sb->s_fs_info;
b3b94faa
DT
480 struct buffer_head *head, *bh, *next;
481 unsigned int curr_off = 0;
b3b94faa
DT
482
483 BUG_ON(!PageLocked(page));
484 if (!page_has_buffers(page))
8628de05 485 return;
b3b94faa
DT
486
487 bh = head = page_buffers(page);
488 do {
489 unsigned int next_off = curr_off + bh->b_size;
490 next = bh->b_this_page;
491
492 if (offset <= curr_off)
493 discard_buffer(sdp, bh);
494
495 curr_off = next_off;
496 bh = next;
497 } while (bh != head);
498
499 if (!offset)
8628de05 500 try_to_release_page(page, 0);
b3b94faa 501
8628de05 502 return;
b3b94faa
DT
503}
504
d1665e41
SW
505static ssize_t gfs2_direct_IO_write(struct kiocb *iocb, const struct iovec *iov,
506 loff_t offset, unsigned long nr_segs)
507{
508 struct file *file = iocb->ki_filp;
509 struct inode *inode = file->f_mapping->host;
5c676f6d 510 struct gfs2_inode *ip = inode->u.generic_ip;
d1665e41
SW
511 struct gfs2_holder gh;
512 int rv;
513
514 /*
515 * Shared lock, even though its write, since we do no allocation
516 * on this path. All we need change is atime.
517 */
518 gfs2_holder_init(ip->i_gl, LM_ST_SHARED, GL_ATIME, &gh);
519 rv = gfs2_glock_nq_m_atime(1, &gh);
520 if (rv)
521 goto out;
522
523 /*
524 * Should we return an error here? I can't see that O_DIRECT for
525 * a journaled file makes any sense. For now we'll silently fall
526 * back to buffered I/O, likewise we do the same for stuffed
527 * files since they are (a) small and (b) unaligned.
528 */
529 if (gfs2_is_jdata(ip))
530 goto out;
531
532 if (gfs2_is_stuffed(ip))
533 goto out;
534
535 rv = __blockdev_direct_IO(WRITE, iocb, inode, inode->i_sb->s_bdev,
8628de05 536 iov, offset, nr_segs, gfs2_get_block,
d1665e41
SW
537 NULL, DIO_OWN_LOCKING);
538out:
539 gfs2_glock_dq_m(1, &gh);
540 gfs2_holder_uninit(&gh);
541
542 return rv;
543}
544
545/**
546 * gfs2_direct_IO
547 *
548 * This is called with a shared lock already held for the read path.
549 * Currently, no locks are held when the write path is called.
550 */
551static ssize_t gfs2_direct_IO(int rw, struct kiocb *iocb,
552 const struct iovec *iov, loff_t offset,
553 unsigned long nr_segs)
b3b94faa
DT
554{
555 struct file *file = iocb->ki_filp;
556 struct inode *inode = file->f_mapping->host;
5c676f6d 557 struct gfs2_inode *ip = inode->u.generic_ip;
b3b94faa 558 struct gfs2_sbd *sdp = ip->i_sbd;
b3b94faa 559
d1665e41
SW
560 if (rw == WRITE)
561 return gfs2_direct_IO_write(iocb, iov, offset, nr_segs);
b3b94faa 562
d1665e41
SW
563 if (gfs2_assert_warn(sdp, gfs2_glock_is_locked_by_me(ip->i_gl)) ||
564 gfs2_assert_warn(sdp, !gfs2_is_stuffed(ip)))
565 return -EINVAL;
b3b94faa 566
d1665e41 567 return __blockdev_direct_IO(READ, iocb, inode, inode->i_sb->s_bdev, iov,
8628de05 568 offset, nr_segs, gfs2_get_block, NULL,
d1665e41 569 DIO_OWN_LOCKING);
b3b94faa
DT
570}
571
572struct address_space_operations gfs2_file_aops = {
573 .writepage = gfs2_writepage,
574 .readpage = gfs2_readpage,
575 .sync_page = block_sync_page,
576 .prepare_write = gfs2_prepare_write,
577 .commit_write = gfs2_commit_write,
578 .bmap = gfs2_bmap,
579 .invalidatepage = gfs2_invalidatepage,
580 .direct_IO = gfs2_direct_IO,
581};
582