Merge tag 'mm-hotfixes-stable-2025-07-11-16-16' of git://git.kernel.org/pub/scm/linux...
[linux-block.git] / fs / orangefs / inode.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
5db11c21
MM
2/*
3 * (C) 2001 Clemson University and The University of Chicago
afd9fb2a 4 * Copyright 2018 Omnibond Systems, L.L.C.
5db11c21
MM
5 *
6 * See COPYING in top-level directory.
7 */
8
9/*
10 * Linux VFS inode operations.
11 */
12
ccdf7741 13#include <linux/blkdev.h>
1f26b062 14#include <linux/fileattr.h>
5db11c21 15#include "protocol.h"
575e9461
MM
16#include "orangefs-kernel.h"
17#include "orangefs-bufmap.h"
5db11c21 18
40eca026
MWO
19static int orangefs_writepage_locked(struct folio *folio,
20 struct writeback_control *wbc)
85ac799c 21{
40eca026 22 struct inode *inode = folio->mapping->host;
52e2d0a3 23 struct orangefs_write_range *wr = NULL;
85ac799c
MB
24 struct iov_iter iter;
25 struct bio_vec bv;
062e8093 26 size_t wlen;
85ac799c 27 ssize_t ret;
062e8093 28 loff_t len, off;
85ac799c 29
40eca026 30 folio_start_writeback(folio);
85ac799c 31
85ac799c 32 len = i_size_read(inode);
40eca026
MWO
33 if (folio->private) {
34 wr = folio->private;
52e2d0a3 35 off = wr->pos;
219bf6ed 36 if ((off + wr->len > len) && (off <= len))
52e2d0a3
MB
37 wlen = len - off;
38 else
39 wlen = wr->len;
219bf6ed
MM
40 if (wlen == 0)
41 wlen = wr->len;
52e2d0a3
MB
42 } else {
43 WARN_ON(1);
40eca026
MWO
44 off = folio_pos(folio);
45 wlen = folio_size(folio);
46
47 if (wlen > len - off)
52e2d0a3 48 wlen = len - off;
85ac799c 49 }
85ac799c 50
52e2d0a3 51 WARN_ON(wlen == 0);
40eca026 52 bvec_set_folio(&bv, folio, wlen, offset_in_folio(folio, off));
de4eda9d 53 iov_iter_bvec(&iter, ITER_SOURCE, &bv, 1, wlen);
85ac799c
MB
54
55 ret = wait_for_direct_io(ORANGEFS_IO_WRITE, inode, &off, &iter, wlen,
f9bbb682 56 len, wr, NULL, NULL);
85ac799c 57 if (ret < 0) {
40eca026 58 mapping_set_error(folio->mapping, ret);
85ac799c
MB
59 } else {
60 ret = 0;
61 }
40eca026 62 kfree(folio_detach_private(folio));
52e2d0a3
MB
63 return ret;
64}
65
8f04e1be
MB
66struct orangefs_writepages {
67 loff_t off;
68 size_t len;
69 kuid_t uid;
70 kgid_t gid;
71 int maxpages;
73f233b9 72 int nfolios;
6420f179 73 struct address_space *mapping;
73f233b9 74 struct folio **folios;
8f04e1be
MB
75 struct bio_vec *bv;
76};
77
78static int orangefs_writepages_work(struct orangefs_writepages *ow,
6420f179 79 struct writeback_control *wbc)
8f04e1be 80{
6420f179 81 struct inode *inode = ow->mapping->host;
8f04e1be
MB
82 struct orangefs_write_range *wrp, wr;
83 struct iov_iter iter;
84 ssize_t ret;
377afd97 85 size_t start;
062e8093 86 loff_t len, off;
8f04e1be
MB
87 int i;
88
89 len = i_size_read(inode);
90
73f233b9
MWO
91 start = offset_in_folio(ow->folios[0], ow->off);
92 for (i = 0; i < ow->nfolios; i++) {
93 folio_start_writeback(ow->folios[i]);
94 bvec_set_folio(&ow->bv[i], ow->folios[i],
95 folio_size(ow->folios[i]) - start, start);
377afd97 96 start = 0;
8f04e1be 97 }
73f233b9 98 iov_iter_bvec(&iter, ITER_SOURCE, ow->bv, ow->nfolios, ow->len);
8f04e1be
MB
99
100 WARN_ON(ow->off >= len);
101 if (ow->off + ow->len > len)
102 ow->len = len - ow->off;
103
104 off = ow->off;
105 wr.uid = ow->uid;
106 wr.gid = ow->gid;
107 ret = wait_for_direct_io(ORANGEFS_IO_WRITE, inode, &off, &iter, ow->len,
f9bbb682 108 0, &wr, NULL, NULL);
f9ec2135 109 if (ret < 0)
6420f179 110 mapping_set_error(ow->mapping, ret);
f9ec2135 111 else
8f04e1be 112 ret = 0;
f9ec2135 113
73f233b9
MWO
114 for (i = 0; i < ow->nfolios; i++) {
115 wrp = folio_detach_private(ow->folios[i]);
116 kfree(wrp);
117 folio_end_writeback(ow->folios[i]);
118 folio_unlock(ow->folios[i]);
8f04e1be 119 }
f9ec2135 120
8f04e1be
MB
121 return ret;
122}
123
d585bdbe 124static int orangefs_writepages_callback(struct folio *folio,
6420f179 125 struct writeback_control *wbc, struct orangefs_writepages *ow)
8f04e1be 126{
d585bdbe 127 struct orangefs_write_range *wr = folio->private;
8f04e1be
MB
128 int ret;
129
d585bdbe
MWO
130 if (!wr) {
131 folio_unlock(folio);
8f04e1be
MB
132 /* It's not private so there's nothing to write, right? */
133 printk("writepages_callback not private!\n");
134 BUG();
135 return 0;
136 }
8f04e1be
MB
137
138 ret = -1;
73f233b9 139 if (ow->nfolios == 0) {
8f04e1be
MB
140 ow->off = wr->pos;
141 ow->len = wr->len;
142 ow->uid = wr->uid;
143 ow->gid = wr->gid;
73f233b9 144 ow->folios[ow->nfolios++] = folio;
8f04e1be
MB
145 ret = 0;
146 goto done;
147 }
148 if (!uid_eq(ow->uid, wr->uid) || !gid_eq(ow->gid, wr->gid)) {
149 orangefs_writepages_work(ow, wbc);
73f233b9 150 ow->nfolios = 0;
8f04e1be
MB
151 ret = -1;
152 goto done;
153 }
154 if (ow->off + ow->len == wr->pos) {
155 ow->len += wr->len;
73f233b9 156 ow->folios[ow->nfolios++] = folio;
8f04e1be
MB
157 ret = 0;
158 goto done;
159 }
160done:
161 if (ret == -1) {
73f233b9 162 if (ow->nfolios) {
8f04e1be 163 orangefs_writepages_work(ow, wbc);
73f233b9 164 ow->nfolios = 0;
8f04e1be 165 }
40eca026 166 ret = orangefs_writepage_locked(folio, wbc);
d585bdbe
MWO
167 mapping_set_error(folio->mapping, ret);
168 folio_unlock(folio);
169 folio_end_writeback(folio);
8f04e1be 170 } else {
73f233b9 171 if (ow->nfolios == ow->maxpages) {
8f04e1be 172 orangefs_writepages_work(ow, wbc);
73f233b9 173 ow->nfolios = 0;
8f04e1be
MB
174 }
175 }
176 return ret;
177}
178
179static int orangefs_writepages(struct address_space *mapping,
180 struct writeback_control *wbc)
181{
182 struct orangefs_writepages *ow;
183 struct blk_plug plug;
6420f179
MWO
184 int error;
185 struct folio *folio = NULL;
186
8f04e1be
MB
187 ow = kzalloc(sizeof(struct orangefs_writepages), GFP_KERNEL);
188 if (!ow)
189 return -ENOMEM;
190 ow->maxpages = orangefs_bufmap_size_query()/PAGE_SIZE;
73f233b9
MWO
191 ow->folios = kcalloc(ow->maxpages, sizeof(struct folio *), GFP_KERNEL);
192 if (!ow->folios) {
8f04e1be
MB
193 kfree(ow);
194 return -ENOMEM;
195 }
196 ow->bv = kcalloc(ow->maxpages, sizeof(struct bio_vec), GFP_KERNEL);
197 if (!ow->bv) {
73f233b9 198 kfree(ow->folios);
8f04e1be
MB
199 kfree(ow);
200 return -ENOMEM;
201 }
6420f179 202 ow->mapping = mapping;
8f04e1be 203 blk_start_plug(&plug);
6420f179
MWO
204 while ((folio = writeback_iter(mapping, wbc, folio, &error)))
205 error = orangefs_writepages_callback(folio, wbc, ow);
73f233b9 206 if (ow->nfolios)
6420f179 207 error = orangefs_writepages_work(ow, wbc);
8f04e1be 208 blk_finish_plug(&plug);
73f233b9 209 kfree(ow->folios);
8f04e1be
MB
210 kfree(ow->bv);
211 kfree(ow);
6420f179 212 return error;
8f04e1be
MB
213}
214
eabf038f 215static int orangefs_launder_folio(struct folio *);
dd59a647 216
0c4b7cad
MM
217static void orangefs_readahead(struct readahead_control *rac)
218{
219 loff_t offset;
220 struct iov_iter iter;
24523e45 221 struct inode *inode = rac->mapping->host;
0c4b7cad 222 struct xarray *i_pages;
cd01049d 223 struct folio *folio;
0c4b7cad
MM
224 loff_t new_start = readahead_pos(rac);
225 int ret;
226 size_t new_len = 0;
227
228 loff_t bytes_remaining = inode->i_size - readahead_pos(rac);
229 loff_t pages_remaining = bytes_remaining / PAGE_SIZE;
230
231 if (pages_remaining >= 1024)
232 new_len = 4194304;
233 else if (pages_remaining > readahead_count(rac))
234 new_len = bytes_remaining;
235
236 if (new_len)
237 readahead_expand(rac, new_start, new_len);
238
239 offset = readahead_pos(rac);
24523e45 240 i_pages = &rac->mapping->i_pages;
0c4b7cad 241
de4eda9d 242 iov_iter_xarray(&iter, ITER_DEST, i_pages, offset, readahead_length(rac));
0c4b7cad
MM
243
244 /* read in the pages. */
245 if ((ret = wait_for_direct_io(ORANGEFS_IO_READ, inode,
246 &offset, &iter, readahead_length(rac),
24523e45 247 inode->i_size, NULL, NULL, rac->file)) < 0)
0c4b7cad
MM
248 gossip_debug(GOSSIP_FILE_DEBUG,
249 "%s: wait_for_direct_io failed. \n", __func__);
250 else
251 ret = 0;
252
253 /* clean up. */
cd01049d
PR
254 while ((folio = readahead_folio(rac))) {
255 if (!ret)
256 folio_mark_uptodate(folio);
257 folio_unlock(folio);
0c4b7cad
MM
258 }
259}
260
1a641788 261static int orangefs_read_folio(struct file *file, struct folio *folio)
5db11c21 262{
1a641788 263 struct inode *inode = folio->mapping->host;
c453dcfc
MB
264 struct iov_iter iter;
265 struct bio_vec bv;
266 ssize_t ret;
1a641788 267 loff_t off; /* offset of this folio in the file */
dd59a647 268
eabf038f
MWO
269 if (folio_test_dirty(folio))
270 orangefs_launder_folio(folio);
c453dcfc 271
1a641788 272 off = folio_pos(folio);
8ead80b2 273 bvec_set_folio(&bv, folio, folio_size(folio), 0);
de4eda9d 274 iov_iter_bvec(&iter, ITER_DEST, &bv, 1, folio_size(folio));
c453dcfc
MB
275
276 ret = wait_for_direct_io(ORANGEFS_IO_READ, inode, &off, &iter,
1a641788 277 folio_size(folio), inode->i_size, NULL, NULL, file);
9329883a 278 /* this will only zero remaining unread portions of the folio data */
c453dcfc 279 iov_iter_zero(~0U, &iter);
5db11c21 280 /* takes care of potential aliasing */
1a641788 281 flush_dcache_folio(folio);
86b3d5f6 282 if (ret > 0)
5db11c21 283 ret = 0;
86b3d5f6
MWO
284 folio_end_read(folio, ret == 0);
285 return ret;
5db11c21
MM
286}
287
52e2d0a3 288static int orangefs_write_begin(struct file *file,
9d6b0cd7 289 struct address_space *mapping, loff_t pos, unsigned len,
1da86618 290 struct folio **foliop, void **fsdata)
52e2d0a3
MB
291{
292 struct orangefs_write_range *wr;
eabf038f 293 struct folio *folio;
52e2d0a3
MB
294 int ret;
295
4c7e1385
MWO
296 folio = __filemap_get_folio(mapping, pos / PAGE_SIZE, FGP_WRITEBEGIN,
297 mapping_gfp_mask(mapping));
298 if (IS_ERR(folio))
299 return PTR_ERR(folio);
52e2d0a3 300
1da86618 301 *foliop = folio;
52e2d0a3 302
eabf038f 303 if (folio_test_dirty(folio) && !folio_test_private(folio)) {
52e2d0a3
MB
304 /*
305 * Should be impossible. If it happens, launder the page
306 * since we don't know what's dirty. This will WARN in
307 * orangefs_writepage_locked.
308 */
eabf038f 309 ret = orangefs_launder_folio(folio);
52e2d0a3
MB
310 if (ret)
311 return ret;
312 }
eabf038f 313 if (folio_test_private(folio)) {
52e2d0a3 314 struct orangefs_write_range *wr;
eabf038f 315 wr = folio_get_private(folio);
52e2d0a3
MB
316 if (wr->pos + wr->len == pos &&
317 uid_eq(wr->uid, current_fsuid()) &&
318 gid_eq(wr->gid, current_fsgid())) {
319 wr->len += len;
320 goto okay;
321 } else {
219bf6ed
MM
322 wr->pos = pos;
323 wr->len = len;
eabf038f 324 ret = orangefs_launder_folio(folio);
52e2d0a3
MB
325 if (ret)
326 return ret;
327 }
52e2d0a3
MB
328 }
329
330 wr = kmalloc(sizeof *wr, GFP_KERNEL);
331 if (!wr)
332 return -ENOMEM;
333
334 wr->pos = pos;
335 wr->len = len;
336 wr->uid = current_fsuid();
337 wr->gid = current_fsgid();
eabf038f 338 folio_attach_private(folio, wr);
52e2d0a3 339okay:
52e2d0a3
MB
340 return 0;
341}
342
85ac799c 343static int orangefs_write_end(struct file *file, struct address_space *mapping,
a225800f
MWO
344 loff_t pos, unsigned len, unsigned copied, struct folio *folio,
345 void *fsdata)
85ac799c 346{
87969292 347 struct inode *inode = folio->mapping->host;
8f04e1be
MB
348 loff_t last_pos = pos + copied;
349
350 /*
351 * No need to use i_size_read() here, the i_size
352 * cannot change under us because we hold the i_mutex.
353 */
354 if (last_pos > inode->i_size)
355 i_size_write(inode, last_pos);
356
87969292
MWO
357 /* zero the stale part of the folio if we did a short copy */
358 if (!folio_test_uptodate(folio)) {
8f04e1be
MB
359 unsigned from = pos & (PAGE_SIZE - 1);
360 if (copied < len) {
87969292 361 folio_zero_range(folio, from + copied, len - copied);
8f04e1be
MB
362 }
363 /* Set fully written pages uptodate. */
87969292 364 if (pos == folio_pos(folio) &&
8f04e1be 365 (len == PAGE_SIZE || pos + len == inode->i_size)) {
87969292
MWO
366 folio_zero_segment(folio, from + copied, PAGE_SIZE);
367 folio_mark_uptodate(folio);
8f04e1be
MB
368 }
369 }
370
87969292
MWO
371 folio_mark_dirty(folio);
372 folio_unlock(folio);
373 folio_put(folio);
8f04e1be 374
85ac799c 375 mark_inode_dirty_sync(file_inode(file));
8f04e1be 376 return copied;
85ac799c
MB
377}
378
2a40be81
MWO
379static void orangefs_invalidate_folio(struct folio *folio,
380 size_t offset, size_t length)
5db11c21 381{
2a40be81 382 struct orangefs_write_range *wr = folio_get_private(folio);
52e2d0a3
MB
383
384 if (offset == 0 && length == PAGE_SIZE) {
2a40be81 385 kfree(folio_detach_private(folio));
8f04e1be 386 return;
52e2d0a3 387 /* write range entirely within invalidate range (or equal) */
2a40be81
MWO
388 } else if (folio_pos(folio) + offset <= wr->pos &&
389 wr->pos + wr->len <= folio_pos(folio) + offset + length) {
390 kfree(folio_detach_private(folio));
52e2d0a3 391 /* XXX is this right? only caller in fs */
2a40be81 392 folio_cancel_dirty(folio);
8f04e1be 393 return;
52e2d0a3 394 /* invalidate range chops off end of write range */
2a40be81
MWO
395 } else if (wr->pos < folio_pos(folio) + offset &&
396 wr->pos + wr->len <= folio_pos(folio) + offset + length &&
397 folio_pos(folio) + offset < wr->pos + wr->len) {
52e2d0a3 398 size_t x;
2a40be81 399 x = wr->pos + wr->len - (folio_pos(folio) + offset);
52e2d0a3
MB
400 WARN_ON(x > wr->len);
401 wr->len -= x;
402 wr->uid = current_fsuid();
403 wr->gid = current_fsgid();
404 /* invalidate range chops off beginning of write range */
2a40be81
MWO
405 } else if (folio_pos(folio) + offset <= wr->pos &&
406 folio_pos(folio) + offset + length < wr->pos + wr->len &&
407 wr->pos < folio_pos(folio) + offset + length) {
52e2d0a3 408 size_t x;
2a40be81 409 x = folio_pos(folio) + offset + length - wr->pos;
52e2d0a3
MB
410 WARN_ON(x > wr->len);
411 wr->pos += x;
412 wr->len -= x;
413 wr->uid = current_fsuid();
414 wr->gid = current_fsgid();
415 /* invalidate range entirely within write range (punch hole) */
2a40be81
MWO
416 } else if (wr->pos < folio_pos(folio) + offset &&
417 folio_pos(folio) + offset + length < wr->pos + wr->len) {
52e2d0a3
MB
418 /* XXX what do we do here... should not WARN_ON */
419 WARN_ON(1);
420 /* punch hole */
421 /*
422 * should we just ignore this and write it out anyway?
423 * it hardly makes sense
424 */
8f04e1be 425 return;
52e2d0a3
MB
426 /* non-overlapping ranges */
427 } else {
428 /* WARN if they do overlap */
2a40be81
MWO
429 if (!((folio_pos(folio) + offset + length <= wr->pos) ^
430 (wr->pos + wr->len <= folio_pos(folio) + offset))) {
52e2d0a3 431 WARN_ON(1);
2a40be81
MWO
432 printk("invalidate range offset %llu length %zu\n",
433 folio_pos(folio) + offset, length);
52e2d0a3
MB
434 printk("write range offset %llu length %zu\n",
435 wr->pos, wr->len);
436 }
8f04e1be 437 return;
52e2d0a3 438 }
8f04e1be
MB
439
440 /*
441 * Above there are returns where wr is freed or where we WARN.
442 * Thus the following runs if wr was modified above.
443 */
444
eabf038f 445 orangefs_launder_folio(folio);
52e2d0a3 446}
5db11c21 447
4993474a 448static bool orangefs_release_folio(struct folio *folio, gfp_t foo)
52e2d0a3 449{
4993474a 450 return !folio_test_private(folio);
52e2d0a3 451}
5db11c21 452
c78ac80e 453static void orangefs_free_folio(struct folio *folio)
52e2d0a3 454{
c78ac80e 455 kfree(folio_detach_private(folio));
5db11c21
MM
456}
457
eabf038f 458static int orangefs_launder_folio(struct folio *folio)
5db11c21 459{
52e2d0a3
MB
460 int r = 0;
461 struct writeback_control wbc = {
462 .sync_mode = WB_SYNC_ALL,
463 .nr_to_write = 0,
464 };
eabf038f
MWO
465 folio_wait_writeback(folio);
466 if (folio_clear_dirty_for_io(folio)) {
40eca026 467 r = orangefs_writepage_locked(folio, &wbc);
eabf038f 468 folio_end_writeback(folio);
52e2d0a3
MB
469 }
470 return r;
5db11c21
MM
471}
472
3903f150
MM
473static ssize_t orangefs_direct_IO(struct kiocb *iocb,
474 struct iov_iter *iter)
475{
3e9dfc6e
MB
476 /*
477 * Comment from original do_readv_writev:
478 * Common entry point for read/write/readv/writev
479 * This function will dispatch it to either the direct I/O
480 * or buffered I/O path depending on the mount options and/or
481 * augmented/extended metadata attached to the file.
482 * Note: File extended attributes override any mount options.
483 */
c453dcfc 484 struct file *file = iocb->ki_filp;
3e9dfc6e
MB
485 loff_t pos = iocb->ki_pos;
486 enum ORANGEFS_io_type type = iov_iter_rw(iter) == WRITE ?
487 ORANGEFS_IO_WRITE : ORANGEFS_IO_READ;
488 loff_t *offset = &pos;
489 struct inode *inode = file->f_mapping->host;
490 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
491 struct orangefs_khandle *handle = &orangefs_inode->refn.khandle;
492 size_t count = iov_iter_count(iter);
3e9dfc6e
MB
493 ssize_t total_count = 0;
494 ssize_t ret = -EINVAL;
3e9dfc6e
MB
495
496 gossip_debug(GOSSIP_FILE_DEBUG,
497 "%s-BEGIN(%pU): count(%d) after estimate_max_iovecs.\n",
498 __func__,
499 handle,
500 (int)count);
501
502 if (type == ORANGEFS_IO_WRITE) {
503 gossip_debug(GOSSIP_FILE_DEBUG,
504 "%s(%pU): proceeding with offset : %llu, "
505 "size %d\n",
506 __func__,
507 handle,
508 llu(*offset),
509 (int)count);
510 }
511
512 if (count == 0) {
513 ret = 0;
514 goto out;
515 }
516
517 while (iov_iter_count(iter)) {
518 size_t each_count = iov_iter_count(iter);
519 size_t amt_complete;
3e9dfc6e
MB
520
521 /* how much to transfer in this loop iteration */
522 if (each_count > orangefs_bufmap_size_query())
523 each_count = orangefs_bufmap_size_query();
524
525 gossip_debug(GOSSIP_FILE_DEBUG,
526 "%s(%pU): size of each_count(%d)\n",
527 __func__,
528 handle,
529 (int)each_count);
530 gossip_debug(GOSSIP_FILE_DEBUG,
531 "%s(%pU): BEFORE wait_for_io: offset is %d\n",
532 __func__,
533 handle,
534 (int)*offset);
535
536 ret = wait_for_direct_io(type, inode, offset, iter,
f9bbb682 537 each_count, 0, NULL, NULL, file);
3e9dfc6e
MB
538 gossip_debug(GOSSIP_FILE_DEBUG,
539 "%s(%pU): return from wait_for_io:%d\n",
540 __func__,
541 handle,
542 (int)ret);
543
544 if (ret < 0)
545 goto out;
546
547 *offset += ret;
548 total_count += ret;
549 amt_complete = ret;
550
551 gossip_debug(GOSSIP_FILE_DEBUG,
552 "%s(%pU): AFTER wait_for_io: offset is %d\n",
553 __func__,
554 handle,
555 (int)*offset);
556
557 /*
558 * if we got a short I/O operations,
559 * fall out and return what we got so far
560 */
561 if (amt_complete < each_count)
562 break;
563 } /*end while */
564
565out:
566 if (total_count > 0)
567 ret = total_count;
568 if (ret > 0) {
569 if (type == ORANGEFS_IO_READ) {
570 file_accessed(file);
571 } else {
572 file_update_time(file);
573 if (*offset > i_size_read(inode))
574 i_size_write(inode, *offset);
575 }
576 }
577
578 gossip_debug(GOSSIP_FILE_DEBUG,
579 "%s(%pU): Value(%d) returned.\n",
580 __func__,
581 handle,
582 (int)ret);
583
584 return ret;
3903f150 585}
5db11c21 586
8bb8aefd 587/** ORANGEFS2 implementation of address space operations */
bdd6f083 588static const struct address_space_operations orangefs_address_operations = {
0c4b7cad 589 .readahead = orangefs_readahead,
1a641788 590 .read_folio = orangefs_read_folio,
8f04e1be 591 .writepages = orangefs_writepages,
187c82cb 592 .dirty_folio = filemap_dirty_folio,
52e2d0a3 593 .write_begin = orangefs_write_begin,
85ac799c 594 .write_end = orangefs_write_end,
2a40be81 595 .invalidate_folio = orangefs_invalidate_folio,
4993474a 596 .release_folio = orangefs_release_folio,
c78ac80e 597 .free_folio = orangefs_free_folio,
506382db 598 .migrate_folio = filemap_migrate_folio,
eabf038f 599 .launder_folio = orangefs_launder_folio,
3903f150 600 .direct_IO = orangefs_direct_IO,
5db11c21
MM
601};
602
52e2d0a3
MB
603vm_fault_t orangefs_page_mkwrite(struct vm_fault *vmf)
604{
eabf038f 605 struct folio *folio = page_folio(vmf->page);
52e2d0a3 606 struct inode *inode = file_inode(vmf->vma->vm_file);
8f04e1be
MB
607 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
608 unsigned long *bitlock = &orangefs_inode->bitlock;
609 vm_fault_t ret;
52e2d0a3
MB
610 struct orangefs_write_range *wr;
611
8f04e1be
MB
612 sb_start_pagefault(inode->i_sb);
613
614 if (wait_on_bit(bitlock, 1, TASK_KILLABLE)) {
615 ret = VM_FAULT_RETRY;
616 goto out;
617 }
618
eabf038f
MWO
619 folio_lock(folio);
620 if (folio_test_dirty(folio) && !folio_test_private(folio)) {
52e2d0a3 621 /*
eabf038f 622 * Should be impossible. If it happens, launder the folio
52e2d0a3
MB
623 * since we don't know what's dirty. This will WARN in
624 * orangefs_writepage_locked.
625 */
eabf038f 626 if (orangefs_launder_folio(folio)) {
8f04e1be 627 ret = VM_FAULT_LOCKED|VM_FAULT_RETRY;
52e2d0a3
MB
628 goto out;
629 }
630 }
eabf038f
MWO
631 if (folio_test_private(folio)) {
632 wr = folio_get_private(folio);
52e2d0a3
MB
633 if (uid_eq(wr->uid, current_fsuid()) &&
634 gid_eq(wr->gid, current_fsgid())) {
eabf038f 635 wr->pos = page_offset(vmf->page);
52e2d0a3
MB
636 wr->len = PAGE_SIZE;
637 goto okay;
638 } else {
eabf038f 639 if (orangefs_launder_folio(folio)) {
8f04e1be 640 ret = VM_FAULT_LOCKED|VM_FAULT_RETRY;
52e2d0a3
MB
641 goto out;
642 }
643 }
644 }
645 wr = kmalloc(sizeof *wr, GFP_KERNEL);
646 if (!wr) {
8f04e1be 647 ret = VM_FAULT_LOCKED|VM_FAULT_RETRY;
52e2d0a3
MB
648 goto out;
649 }
eabf038f 650 wr->pos = page_offset(vmf->page);
52e2d0a3
MB
651 wr->len = PAGE_SIZE;
652 wr->uid = current_fsuid();
653 wr->gid = current_fsgid();
eabf038f 654 folio_attach_private(folio, wr);
52e2d0a3
MB
655okay:
656
52e2d0a3 657 file_update_time(vmf->vma->vm_file);
eabf038f
MWO
658 if (folio->mapping != inode->i_mapping) {
659 folio_unlock(folio);
8f04e1be 660 ret = VM_FAULT_LOCKED|VM_FAULT_NOPAGE;
52e2d0a3
MB
661 goto out;
662 }
663
664 /*
eabf038f 665 * We mark the folio dirty already here so that when freeze is in
52e2d0a3 666 * progress, we are guaranteed that writeback during freezing will
eabf038f 667 * see the dirty folio and writeprotect it again.
52e2d0a3 668 */
eabf038f
MWO
669 folio_mark_dirty(folio);
670 folio_wait_stable(folio);
8f04e1be 671 ret = VM_FAULT_LOCKED;
52e2d0a3
MB
672out:
673 sb_end_pagefault(inode->i_sb);
674 return ret;
675}
676
8bb8aefd 677static int orangefs_setattr_size(struct inode *inode, struct iattr *iattr)
5db11c21 678{
8bb8aefd
YL
679 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
680 struct orangefs_kernel_op_s *new_op;
fecd86aa 681 loff_t orig_size;
5db11c21
MM
682 int ret = -EINVAL;
683
684 gossip_debug(GOSSIP_INODE_DEBUG,
685 "%s: %pU: Handle is %pU | fs_id %d | size is %llu\n",
686 __func__,
687 get_khandle_from_ino(inode),
8bb8aefd
YL
688 &orangefs_inode->refn.khandle,
689 orangefs_inode->refn.fs_id,
5db11c21
MM
690 iattr->ia_size);
691
fecd86aa 692 /* Ensure that we have a up to date size, so we know if it changed. */
8b60785c 693 ret = orangefs_inode_getattr(inode, ORANGEFS_GETATTR_SIZE);
fecd86aa
MB
694 if (ret == -ESTALE)
695 ret = -EIO;
696 if (ret) {
697 gossip_err("%s: orangefs_inode_getattr failed, ret:%d:.\n",
698 __func__, ret);
699 return ret;
700 }
701 orig_size = i_size_read(inode);
702
33713cd0
MB
703 /* This is truncate_setsize in a different order. */
704 truncate_pagecache(inode, iattr->ia_size);
705 i_size_write(inode, iattr->ia_size);
706 if (iattr->ia_size > orig_size)
707 pagecache_isize_extended(inode, orig_size, iattr->ia_size);
5db11c21 708
8bb8aefd 709 new_op = op_alloc(ORANGEFS_VFS_OP_TRUNCATE);
5db11c21
MM
710 if (!new_op)
711 return -ENOMEM;
712
8bb8aefd 713 new_op->upcall.req.truncate.refn = orangefs_inode->refn;
5db11c21
MM
714 new_op->upcall.req.truncate.size = (__s64) iattr->ia_size;
715
95f5f88f
MM
716 ret = service_operation(new_op,
717 __func__,
718 get_interruptible_flag(inode));
5db11c21
MM
719
720 /*
721 * the truncate has no downcall members to retrieve, but
722 * the status value tells us if it went through ok or not
723 */
95f5f88f 724 gossip_debug(GOSSIP_INODE_DEBUG, "%s: ret:%d:\n", __func__, ret);
5db11c21
MM
725
726 op_release(new_op);
727
728 if (ret != 0)
729 return ret;
730
f83140c1 731 if (orig_size != i_size_read(inode))
5db11c21 732 iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
5db11c21
MM
733
734 return ret;
735}
736
afd9fb2a 737int __orangefs_setattr(struct inode *inode, struct iattr *iattr)
5db11c21 738{
df2d7337 739 int ret;
5db11c21 740
afd9fb2a
MB
741 if (iattr->ia_valid & ATTR_MODE) {
742 if (iattr->ia_mode & (S_ISVTX)) {
743 if (is_root_handle(inode)) {
744 /*
745 * allow sticky bit to be set on root (since
746 * it shows up that way by default anyhow),
747 * but don't show it to the server
748 */
749 iattr->ia_mode -= S_ISVTX;
750 } else {
751 gossip_debug(GOSSIP_UTILS_DEBUG,
752 "User attempted to set sticky bit on non-root directory; returning EINVAL.\n");
8f04e1be
MB
753 ret = -EINVAL;
754 goto out;
afd9fb2a
MB
755 }
756 }
757 if (iattr->ia_mode & (S_ISUID)) {
758 gossip_debug(GOSSIP_UTILS_DEBUG,
759 "Attempting to set setuid bit (not supported); returning EINVAL.\n");
8f04e1be
MB
760 ret = -EINVAL;
761 goto out;
afd9fb2a
MB
762 }
763 }
5db11c21 764
53950ef5 765 if (iattr->ia_valid & ATTR_SIZE) {
8bb8aefd 766 ret = orangefs_setattr_size(inode, iattr);
5db11c21
MM
767 if (ret)
768 goto out;
769 }
770
afd9fb2a
MB
771again:
772 spin_lock(&inode->i_lock);
773 if (ORANGEFS_I(inode)->attr_valid) {
774 if (uid_eq(ORANGEFS_I(inode)->attr_uid, current_fsuid()) &&
775 gid_eq(ORANGEFS_I(inode)->attr_gid, current_fsgid())) {
776 ORANGEFS_I(inode)->attr_valid = iattr->ia_valid;
777 } else {
778 spin_unlock(&inode->i_lock);
779 write_inode_now(inode, 1);
780 goto again;
781 }
782 } else {
783 ORANGEFS_I(inode)->attr_valid = iattr->ia_valid;
784 ORANGEFS_I(inode)->attr_uid = current_fsuid();
785 ORANGEFS_I(inode)->attr_gid = current_fsgid();
786 }
c1632a0f 787 setattr_copy(&nop_mnt_idmap, inode, iattr);
afd9fb2a 788 spin_unlock(&inode->i_lock);
5db11c21
MM
789 mark_inode_dirty(inode);
790
df2d7337 791 ret = 0;
5db11c21 792out:
afd9fb2a
MB
793 return ret;
794}
795
138060ba 796int __orangefs_setattr_mode(struct dentry *dentry, struct iattr *iattr)
4053d250
CB
797{
798 int ret;
138060ba 799 struct inode *inode = d_inode(dentry);
4053d250
CB
800
801 ret = __orangefs_setattr(inode, iattr);
802 /* change mode on a file that has ACLs */
803 if (!ret && (iattr->ia_valid & ATTR_MODE))
13e83a49 804 ret = posix_acl_chmod(&nop_mnt_idmap, dentry, inode->i_mode);
4053d250
CB
805 return ret;
806}
807
afd9fb2a
MB
808/*
809 * Change attributes of an object referenced by dentry.
810 */
c1632a0f 811int orangefs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
549c7297 812 struct iattr *iattr)
afd9fb2a
MB
813{
814 int ret;
815 gossip_debug(GOSSIP_INODE_DEBUG, "__orangefs_setattr: called on %pd\n",
816 dentry);
c1632a0f 817 ret = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
afd9fb2a
MB
818 if (ret)
819 goto out;
138060ba 820 ret = __orangefs_setattr_mode(dentry, iattr);
afd9fb2a
MB
821 sync_inode_metadata(d_inode(dentry), 1);
822out:
823 gossip_debug(GOSSIP_INODE_DEBUG, "orangefs_setattr: returning %d\n",
824 ret);
5db11c21
MM
825 return ret;
826}
827
828/*
829 * Obtain attributes of an object given a dentry
830 */
b74d24f7 831int orangefs_getattr(struct mnt_idmap *idmap, const struct path *path,
549c7297 832 struct kstat *stat, u32 request_mask, unsigned int flags)
5db11c21 833{
e6b998ab 834 int ret;
a528d35e 835 struct inode *inode = path->dentry->d_inode;
5db11c21
MM
836
837 gossip_debug(GOSSIP_INODE_DEBUG,
5e4f606e
MB
838 "orangefs_getattr: called on %pd mask %u\n",
839 path->dentry, request_mask);
5db11c21 840
8b60785c
MB
841 ret = orangefs_inode_getattr(inode,
842 request_mask & STATX_SIZE ? ORANGEFS_GETATTR_SIZE : 0);
5db11c21 843 if (ret == 0) {
0d72b928 844 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
a7d3e78a 845
5db11c21 846 /* override block size reported to stat */
5678b5d6
CH
847 if (!(request_mask & STATX_SIZE))
848 stat->result_mask &= ~STATX_SIZE;
7f54910f 849
4f911138 850 generic_fill_statx_attr(inode, stat);
5db11c21
MM
851 }
852 return ret;
853}
854
4609e1f1 855int orangefs_permission(struct mnt_idmap *idmap,
549c7297 856 struct inode *inode, int mask)
933287da
MB
857{
858 int ret;
859
860 if (mask & MAY_NOT_BLOCK)
861 return -ECHILD;
862
863 gossip_debug(GOSSIP_INODE_DEBUG, "%s: refreshing\n", __func__);
864
865 /* Make sure the permission (and other common attrs) are up to date. */
8b60785c 866 ret = orangefs_inode_getattr(inode, 0);
933287da
MB
867 if (ret < 0)
868 return ret;
869
4609e1f1 870 return generic_permission(&nop_mnt_idmap, inode, mask);
933287da
MB
871}
872
913e9928 873int orangefs_update_time(struct inode *inode, int flags)
a55f2d86
MB
874{
875 struct iattr iattr;
541d4c79 876
a55f2d86
MB
877 gossip_debug(GOSSIP_INODE_DEBUG, "orangefs_update_time: %pU\n",
878 get_khandle_from_ino(inode));
541d4c79 879 flags = generic_update_time(inode, flags);
a55f2d86
MB
880 memset(&iattr, 0, sizeof iattr);
881 if (flags & S_ATIME)
882 iattr.ia_valid |= ATTR_ATIME;
883 if (flags & S_CTIME)
884 iattr.ia_valid |= ATTR_CTIME;
885 if (flags & S_MTIME)
886 iattr.ia_valid |= ATTR_MTIME;
afd9fb2a 887 return __orangefs_setattr(inode, &iattr);
a55f2d86
MB
888}
889
1f26b062
MS
890static int orangefs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
891{
892 u64 val = 0;
893 int ret;
894
895 gossip_debug(GOSSIP_FILE_DEBUG, "%s: called on %pd\n", __func__,
896 dentry);
897
898 ret = orangefs_inode_getxattr(d_inode(dentry),
899 "user.pvfs2.meta_hint",
900 &val, sizeof(val));
901 if (ret < 0 && ret != -ENODATA)
902 return ret;
903
904 gossip_debug(GOSSIP_FILE_DEBUG, "%s: flags=%u\n", __func__, (u32) val);
905
906 fileattr_fill_flags(fa, val);
907 return 0;
908}
909
8782a9ae 910static int orangefs_fileattr_set(struct mnt_idmap *idmap,
1f26b062
MS
911 struct dentry *dentry, struct fileattr *fa)
912{
913 u64 val = 0;
914
915 gossip_debug(GOSSIP_FILE_DEBUG, "%s: called on %pd\n", __func__,
916 dentry);
917 /*
918 * ORANGEFS_MIRROR_FL is set internally when the mirroring mode is
919 * turned on for a file. The user is not allowed to turn on this bit,
920 * but the bit is present if the user first gets the flags and then
921 * updates the flags with some new settings. So, we ignore it in the
922 * following edit. bligon.
923 */
924 if (fileattr_has_fsx(fa) ||
925 (fa->flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NOATIME_FL | ORANGEFS_MIRROR_FL))) {
926 gossip_err("%s: only supports setting one of FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NOATIME_FL\n",
927 __func__);
928 return -EOPNOTSUPP;
929 }
930 val = fa->flags;
931 gossip_debug(GOSSIP_FILE_DEBUG, "%s: flags=%u\n", __func__, (u32) val);
932 return orangefs_inode_setxattr(d_inode(dentry),
933 "user.pvfs2.meta_hint",
934 &val, sizeof(val), 0);
935}
936
95f5f88f 937/* ORANGEFS2 implementation of VFS inode operations for files */
bdd6f083 938static const struct inode_operations orangefs_file_inode_operations = {
cac2f8b8 939 .get_inode_acl = orangefs_get_acl,
8bb8aefd
YL
940 .set_acl = orangefs_set_acl,
941 .setattr = orangefs_setattr,
942 .getattr = orangefs_getattr,
8bb8aefd 943 .listxattr = orangefs_listxattr,
933287da 944 .permission = orangefs_permission,
a55f2d86 945 .update_time = orangefs_update_time,
1f26b062
MS
946 .fileattr_get = orangefs_fileattr_get,
947 .fileattr_set = orangefs_fileattr_set,
5db11c21
MM
948};
949
8bb8aefd 950static int orangefs_init_iops(struct inode *inode)
5db11c21 951{
8bb8aefd 952 inode->i_mapping->a_ops = &orangefs_address_operations;
5db11c21
MM
953
954 switch (inode->i_mode & S_IFMT) {
955 case S_IFREG:
8bb8aefd
YL
956 inode->i_op = &orangefs_file_inode_operations;
957 inode->i_fop = &orangefs_file_operations;
5db11c21
MM
958 break;
959 case S_IFLNK:
8bb8aefd 960 inode->i_op = &orangefs_symlink_inode_operations;
5db11c21
MM
961 break;
962 case S_IFDIR:
8bb8aefd
YL
963 inode->i_op = &orangefs_dir_inode_operations;
964 inode->i_fop = &orangefs_dir_operations;
5db11c21
MM
965 break;
966 default:
967 gossip_debug(GOSSIP_INODE_DEBUG,
968 "%s: unsupported mode\n",
969 __func__);
970 return -EINVAL;
971 }
972
973 return 0;
974}
975
976/*
95f5f88f
MM
977 * Given an ORANGEFS object identifier (fsid, handle), convert it into
978 * a ino_t type that will be used as a hash-index from where the handle will
5db11c21
MM
979 * be searched for in the VFS hash table of inodes.
980 */
8bb8aefd 981static inline ino_t orangefs_handle_hash(struct orangefs_object_kref *ref)
5db11c21
MM
982{
983 if (!ref)
984 return 0;
8bb8aefd 985 return orangefs_khandle_to_ino(&(ref->khandle));
5db11c21
MM
986}
987
988/*
989 * Called to set up an inode from iget5_locked.
990 */
8bb8aefd 991static int orangefs_set_inode(struct inode *inode, void *data)
5db11c21 992{
8bb8aefd 993 struct orangefs_object_kref *ref = (struct orangefs_object_kref *) data;
a4c680a0
MB
994 ORANGEFS_I(inode)->refn.fs_id = ref->fs_id;
995 ORANGEFS_I(inode)->refn.khandle = ref->khandle;
afd9fb2a 996 ORANGEFS_I(inode)->attr_valid = 0;
fc2e2e9c 997 hash_init(ORANGEFS_I(inode)->xattr_cache);
8f04e1be
MB
998 ORANGEFS_I(inode)->mapping_time = jiffies - 1;
999 ORANGEFS_I(inode)->bitlock = 0;
5db11c21
MM
1000 return 0;
1001}
1002
1003/*
1004 * Called to determine if handles match.
1005 */
8bb8aefd 1006static int orangefs_test_inode(struct inode *inode, void *data)
5db11c21 1007{
8bb8aefd
YL
1008 struct orangefs_object_kref *ref = (struct orangefs_object_kref *) data;
1009 struct orangefs_inode_s *orangefs_inode = NULL;
5db11c21 1010
8bb8aefd 1011 orangefs_inode = ORANGEFS_I(inode);
95f5f88f
MM
1012 /* test handles and fs_ids... */
1013 return (!ORANGEFS_khandle_cmp(&(orangefs_inode->refn.khandle),
1014 &(ref->khandle)) &&
1015 orangefs_inode->refn.fs_id == ref->fs_id);
5db11c21
MM
1016}
1017
1018/*
8bb8aefd 1019 * Front-end to lookup the inode-cache maintained by the VFS using the ORANGEFS
5db11c21
MM
1020 * file handle.
1021 *
1022 * @sb: the file system super block instance.
95f5f88f 1023 * @ref: The ORANGEFS object for which we are trying to locate an inode.
5db11c21 1024 */
95f5f88f
MM
1025struct inode *orangefs_iget(struct super_block *sb,
1026 struct orangefs_object_kref *ref)
5db11c21
MM
1027{
1028 struct inode *inode = NULL;
1029 unsigned long hash;
1030 int error;
1031
8bb8aefd 1032 hash = orangefs_handle_hash(ref);
95f5f88f
MM
1033 inode = iget5_locked(sb,
1034 hash,
1035 orangefs_test_inode,
1036 orangefs_set_inode,
1037 ref);
b5d72cdc
MM
1038
1039 if (!inode)
1040 return ERR_PTR(-ENOMEM);
1041
1042 if (!(inode->i_state & I_NEW))
5db11c21
MM
1043 return inode;
1044
8b60785c 1045 error = orangefs_inode_getattr(inode, ORANGEFS_GETATTR_NEW);
5db11c21
MM
1046 if (error) {
1047 iget_failed(inode);
1048 return ERR_PTR(error);
1049 }
1050
1051 inode->i_ino = hash; /* needed for stat etc */
8bb8aefd 1052 orangefs_init_iops(inode);
5db11c21
MM
1053 unlock_new_inode(inode);
1054
1055 gossip_debug(GOSSIP_INODE_DEBUG,
1056 "iget handle %pU, fsid %d hash %ld i_ino %lu\n",
1057 &ref->khandle,
1058 ref->fs_id,
1059 hash,
1060 inode->i_ino);
1061
1062 return inode;
1063}
1064
1065/*
1066 * Allocate an inode for a newly created file and insert it into the inode hash.
1067 */
8bb8aefd 1068struct inode *orangefs_new_inode(struct super_block *sb, struct inode *dir,
4053d250 1069 umode_t mode, dev_t dev, struct orangefs_object_kref *ref)
5db11c21 1070{
4053d250 1071 struct posix_acl *acl = NULL, *default_acl = NULL;
8bb8aefd 1072 unsigned long hash = orangefs_handle_hash(ref);
5db11c21
MM
1073 struct inode *inode;
1074 int error;
1075
1076 gossip_debug(GOSSIP_INODE_DEBUG,
5253487e
MM
1077 "%s:(sb is %p | MAJOR(dev)=%u | MINOR(dev)=%u mode=%o)\n",
1078 __func__,
5db11c21
MM
1079 sb,
1080 MAJOR(dev),
1081 MINOR(dev),
1082 mode);
1083
1084 inode = new_inode(sb);
1085 if (!inode)
56249998 1086 return ERR_PTR(-ENOMEM);
5db11c21 1087
4053d250
CB
1088 error = posix_acl_create(dir, &mode, &default_acl, &acl);
1089 if (error)
1090 goto out_iput;
1091
8bb8aefd 1092 orangefs_set_inode(inode, ref);
5db11c21
MM
1093 inode->i_ino = hash; /* needed for stat etc */
1094
8b60785c 1095 error = orangefs_inode_getattr(inode, ORANGEFS_GETATTR_NEW);
5db11c21
MM
1096 if (error)
1097 goto out_iput;
1098
8bb8aefd 1099 orangefs_init_iops(inode);
5db11c21
MM
1100 inode->i_rdev = dev;
1101
4053d250
CB
1102 if (default_acl) {
1103 error = __orangefs_set_acl(inode, default_acl,
1104 ACL_TYPE_DEFAULT);
1105 if (error)
1106 goto out_iput;
1107 }
1108
1109 if (acl) {
1110 error = __orangefs_set_acl(inode, acl, ACL_TYPE_ACCESS);
1111 if (error)
1112 goto out_iput;
1113 }
1114
8bb8aefd 1115 error = insert_inode_locked4(inode, hash, orangefs_test_inode, ref);
5db11c21
MM
1116 if (error < 0)
1117 goto out_iput;
1118
1119 gossip_debug(GOSSIP_INODE_DEBUG,
1120 "Initializing ACL's for inode %pU\n",
1121 get_khandle_from_ino(inode));
e40df428
CB
1122 if (mode != inode->i_mode) {
1123 struct iattr iattr = {
1124 .ia_mode = mode,
1125 .ia_valid = ATTR_MODE,
1126 };
1127 inode->i_mode = mode;
1128 __orangefs_setattr(inode, &iattr);
1129 __posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
1130 }
4053d250
CB
1131 posix_acl_release(acl);
1132 posix_acl_release(default_acl);
5db11c21
MM
1133 return inode;
1134
1135out_iput:
1136 iput(inode);
4053d250
CB
1137 posix_acl_release(acl);
1138 posix_acl_release(default_acl);
5db11c21
MM
1139 return ERR_PTR(error);
1140}