blob: e5e3e500ed4627b4e450803ff9af603af43ec56d [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Mike Marshall5db11c22015-07-17 10:38:12 -04002/*
3 * (C) 2001 Clemson University and The University of Chicago
Martin Brandenburgafd9fb22018-02-13 20:13:46 +00004 * Copyright 2018 Omnibond Systems, L.L.C.
Mike Marshall5db11c22015-07-17 10:38:12 -04005 *
6 * See COPYING in top-level directory.
7 */
8
9/*
10 * Linux VFS inode operations.
11 */
12
Christoph Hellwigccdf7742021-09-20 14:33:14 +020013#include <linux/blkdev.h>
Miklos Szeredi1f26b062021-04-07 14:36:43 +020014#include <linux/fileattr.h>
Mike Marshall5db11c22015-07-17 10:38:12 -040015#include "protocol.h"
Mike Marshall575e9462015-12-04 12:56:14 -050016#include "orangefs-kernel.h"
17#include "orangefs-bufmap.h"
Mike Marshall5db11c22015-07-17 10:38:12 -040018
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -050019static int orangefs_writepage_locked(struct page *page,
20 struct writeback_control *wbc)
Martin Brandenburg85ac7992018-02-22 18:10:43 +000021{
22 struct inode *inode = page->mapping->host;
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -050023 struct orangefs_write_range *wr = NULL;
Martin Brandenburg85ac7992018-02-22 18:10:43 +000024 struct iov_iter iter;
25 struct bio_vec bv;
26 size_t len, wlen;
27 ssize_t ret;
28 loff_t off;
29
30 set_page_writeback(page);
31
Martin Brandenburg85ac7992018-02-22 18:10:43 +000032 len = i_size_read(inode);
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -050033 if (PagePrivate(page)) {
34 wr = (struct orangefs_write_range *)page_private(page);
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +000035 WARN_ON(wr->pos >= len);
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -050036 off = wr->pos;
37 if (off + wr->len > len)
38 wlen = len - off;
39 else
40 wlen = wr->len;
41 } else {
42 WARN_ON(1);
43 off = page_offset(page);
44 if (off + PAGE_SIZE > len)
45 wlen = len - off;
46 else
47 wlen = PAGE_SIZE;
Martin Brandenburg85ac7992018-02-22 18:10:43 +000048 }
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -050049 /* Should've been handled in orangefs_invalidatepage. */
50 WARN_ON(off == len || off + wlen > len);
Martin Brandenburg85ac7992018-02-22 18:10:43 +000051
52 bv.bv_page = page;
53 bv.bv_len = wlen;
54 bv.bv_offset = off % PAGE_SIZE;
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -050055 WARN_ON(wlen == 0);
Martin Brandenburg85ac7992018-02-22 18:10:43 +000056 iov_iter_bvec(&iter, WRITE, &bv, 1, wlen);
57
58 ret = wait_for_direct_io(ORANGEFS_IO_WRITE, inode, &off, &iter, wlen,
Mike Marshallf9bbb682019-11-26 12:39:37 -050059 len, wr, NULL, NULL);
Martin Brandenburg85ac7992018-02-22 18:10:43 +000060 if (ret < 0) {
61 SetPageError(page);
62 mapping_set_error(page->mapping, ret);
63 } else {
64 ret = 0;
65 }
Guoqing Jiang4c42be32020-06-01 21:48:00 -070066 kfree(detach_page_private(page));
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -050067 return ret;
68}
69
70static int orangefs_writepage(struct page *page, struct writeback_control *wbc)
71{
72 int ret;
73 ret = orangefs_writepage_locked(page, wbc);
Martin Brandenburg85ac7992018-02-22 18:10:43 +000074 unlock_page(page);
75 end_page_writeback(page);
76 return ret;
77}
78
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +000079struct orangefs_writepages {
80 loff_t off;
81 size_t len;
82 kuid_t uid;
83 kgid_t gid;
84 int maxpages;
85 int npages;
86 struct page **pages;
87 struct bio_vec *bv;
88};
89
90static int orangefs_writepages_work(struct orangefs_writepages *ow,
91 struct writeback_control *wbc)
92{
93 struct inode *inode = ow->pages[0]->mapping->host;
94 struct orangefs_write_range *wrp, wr;
95 struct iov_iter iter;
96 ssize_t ret;
97 size_t len;
98 loff_t off;
99 int i;
100
101 len = i_size_read(inode);
102
103 for (i = 0; i < ow->npages; i++) {
104 set_page_writeback(ow->pages[i]);
105 ow->bv[i].bv_page = ow->pages[i];
106 ow->bv[i].bv_len = min(page_offset(ow->pages[i]) + PAGE_SIZE,
107 ow->off + ow->len) -
108 max(ow->off, page_offset(ow->pages[i]));
109 if (i == 0)
110 ow->bv[i].bv_offset = ow->off -
111 page_offset(ow->pages[i]);
112 else
113 ow->bv[i].bv_offset = 0;
114 }
115 iov_iter_bvec(&iter, WRITE, ow->bv, ow->npages, ow->len);
116
117 WARN_ON(ow->off >= len);
118 if (ow->off + ow->len > len)
119 ow->len = len - ow->off;
120
121 off = ow->off;
122 wr.uid = ow->uid;
123 wr.gid = ow->gid;
124 ret = wait_for_direct_io(ORANGEFS_IO_WRITE, inode, &off, &iter, ow->len,
Mike Marshallf9bbb682019-11-26 12:39:37 -0500125 0, &wr, NULL, NULL);
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +0000126 if (ret < 0) {
127 for (i = 0; i < ow->npages; i++) {
128 SetPageError(ow->pages[i]);
129 mapping_set_error(ow->pages[i]->mapping, ret);
130 if (PagePrivate(ow->pages[i])) {
131 wrp = (struct orangefs_write_range *)
132 page_private(ow->pages[i]);
133 ClearPagePrivate(ow->pages[i]);
134 put_page(ow->pages[i]);
135 kfree(wrp);
136 }
137 end_page_writeback(ow->pages[i]);
138 unlock_page(ow->pages[i]);
139 }
140 } else {
141 ret = 0;
142 for (i = 0; i < ow->npages; i++) {
143 if (PagePrivate(ow->pages[i])) {
144 wrp = (struct orangefs_write_range *)
145 page_private(ow->pages[i]);
146 ClearPagePrivate(ow->pages[i]);
147 put_page(ow->pages[i]);
148 kfree(wrp);
149 }
150 end_page_writeback(ow->pages[i]);
151 unlock_page(ow->pages[i]);
152 }
153 }
154 return ret;
155}
156
157static int orangefs_writepages_callback(struct page *page,
158 struct writeback_control *wbc, void *data)
159{
160 struct orangefs_writepages *ow = data;
161 struct orangefs_write_range *wr;
162 int ret;
163
164 if (!PagePrivate(page)) {
165 unlock_page(page);
166 /* It's not private so there's nothing to write, right? */
167 printk("writepages_callback not private!\n");
168 BUG();
169 return 0;
170 }
171 wr = (struct orangefs_write_range *)page_private(page);
172
173 ret = -1;
174 if (ow->npages == 0) {
175 ow->off = wr->pos;
176 ow->len = wr->len;
177 ow->uid = wr->uid;
178 ow->gid = wr->gid;
179 ow->pages[ow->npages++] = page;
180 ret = 0;
181 goto done;
182 }
183 if (!uid_eq(ow->uid, wr->uid) || !gid_eq(ow->gid, wr->gid)) {
184 orangefs_writepages_work(ow, wbc);
185 ow->npages = 0;
186 ret = -1;
187 goto done;
188 }
189 if (ow->off + ow->len == wr->pos) {
190 ow->len += wr->len;
191 ow->pages[ow->npages++] = page;
192 ret = 0;
193 goto done;
194 }
195done:
196 if (ret == -1) {
197 if (ow->npages) {
198 orangefs_writepages_work(ow, wbc);
199 ow->npages = 0;
200 }
201 ret = orangefs_writepage_locked(page, wbc);
202 mapping_set_error(page->mapping, ret);
203 unlock_page(page);
204 end_page_writeback(page);
205 } else {
206 if (ow->npages == ow->maxpages) {
207 orangefs_writepages_work(ow, wbc);
208 ow->npages = 0;
209 }
210 }
211 return ret;
212}
213
214static int orangefs_writepages(struct address_space *mapping,
215 struct writeback_control *wbc)
216{
217 struct orangefs_writepages *ow;
218 struct blk_plug plug;
219 int ret;
220 ow = kzalloc(sizeof(struct orangefs_writepages), GFP_KERNEL);
221 if (!ow)
222 return -ENOMEM;
223 ow->maxpages = orangefs_bufmap_size_query()/PAGE_SIZE;
224 ow->pages = kcalloc(ow->maxpages, sizeof(struct page *), GFP_KERNEL);
225 if (!ow->pages) {
226 kfree(ow);
227 return -ENOMEM;
228 }
229 ow->bv = kcalloc(ow->maxpages, sizeof(struct bio_vec), GFP_KERNEL);
230 if (!ow->bv) {
231 kfree(ow->pages);
232 kfree(ow);
233 return -ENOMEM;
234 }
235 blk_start_plug(&plug);
236 ret = write_cache_pages(mapping, wbc, orangefs_writepages_callback, ow);
237 if (ow->npages)
238 ret = orangefs_writepages_work(ow, wbc);
239 blk_finish_plug(&plug);
240 kfree(ow->pages);
241 kfree(ow->bv);
242 kfree(ow);
243 return ret;
244}
245
Mike Marshalldd59a642019-03-25 18:59:29 -0400246static int orangefs_launder_page(struct page *);
247
Mike Marshall0c4b7ca2021-03-28 17:18:11 -0400248static void orangefs_readahead(struct readahead_control *rac)
249{
250 loff_t offset;
251 struct iov_iter iter;
Mike Marshall24523e452021-05-18 08:07:21 -0400252 struct inode *inode = rac->mapping->host;
Mike Marshall0c4b7ca2021-03-28 17:18:11 -0400253 struct xarray *i_pages;
254 struct page *page;
255 loff_t new_start = readahead_pos(rac);
256 int ret;
257 size_t new_len = 0;
258
259 loff_t bytes_remaining = inode->i_size - readahead_pos(rac);
260 loff_t pages_remaining = bytes_remaining / PAGE_SIZE;
261
262 if (pages_remaining >= 1024)
263 new_len = 4194304;
264 else if (pages_remaining > readahead_count(rac))
265 new_len = bytes_remaining;
266
267 if (new_len)
268 readahead_expand(rac, new_start, new_len);
269
270 offset = readahead_pos(rac);
Mike Marshall24523e452021-05-18 08:07:21 -0400271 i_pages = &rac->mapping->i_pages;
Mike Marshall0c4b7ca2021-03-28 17:18:11 -0400272
273 iov_iter_xarray(&iter, READ, i_pages, offset, readahead_length(rac));
274
275 /* read in the pages. */
276 if ((ret = wait_for_direct_io(ORANGEFS_IO_READ, inode,
277 &offset, &iter, readahead_length(rac),
Mike Marshall24523e452021-05-18 08:07:21 -0400278 inode->i_size, NULL, NULL, rac->file)) < 0)
Mike Marshall0c4b7ca2021-03-28 17:18:11 -0400279 gossip_debug(GOSSIP_FILE_DEBUG,
280 "%s: wait_for_direct_io failed. \n", __func__);
281 else
282 ret = 0;
283
284 /* clean up. */
285 while ((page = readahead_page(rac))) {
286 page_endio(page, false, ret);
287 put_page(page);
288 }
289}
290
Martin Brandenburga68d9c62018-02-15 18:02:43 +0000291static int orangefs_readpage(struct file *file, struct page *page)
Mike Marshall5db11c22015-07-17 10:38:12 -0400292{
Mike Marshall5db11c22015-07-17 10:38:12 -0400293 struct inode *inode = page->mapping->host;
Martin Brandenburgc453dcf2018-02-16 20:51:24 +0000294 struct iov_iter iter;
295 struct bio_vec bv;
296 ssize_t ret;
Mike Marshalldd59a642019-03-25 18:59:29 -0400297 loff_t off; /* offset into this page */
Mike Marshalldd59a642019-03-25 18:59:29 -0400298
299 if (PageDirty(page))
300 orangefs_launder_page(page);
Al Viro74f68fc2015-10-08 18:31:05 -0400301
Martin Brandenburgc453dcf2018-02-16 20:51:24 +0000302 off = page_offset(page);
303 bv.bv_page = page;
304 bv.bv_len = PAGE_SIZE;
305 bv.bv_offset = 0;
306 iov_iter_bvec(&iter, READ, &bv, 1, PAGE_SIZE);
Mike Marshall5db11c22015-07-17 10:38:12 -0400307
Martin Brandenburgc453dcf2018-02-16 20:51:24 +0000308 ret = wait_for_direct_io(ORANGEFS_IO_READ, inode, &off, &iter,
Mike Marshall0c4b7ca2021-03-28 17:18:11 -0400309 PAGE_SIZE, inode->i_size, NULL, NULL, file);
Al Viro74f68fc2015-10-08 18:31:05 -0400310 /* this will only zero remaining unread portions of the page data */
Martin Brandenburgc453dcf2018-02-16 20:51:24 +0000311 iov_iter_zero(~0U, &iter);
Mike Marshall5db11c22015-07-17 10:38:12 -0400312 /* takes care of potential aliasing */
313 flush_dcache_page(page);
Martin Brandenburgc453dcf2018-02-16 20:51:24 +0000314 if (ret < 0) {
Mike Marshall5db11c22015-07-17 10:38:12 -0400315 SetPageError(page);
316 } else {
317 SetPageUptodate(page);
318 if (PageError(page))
319 ClearPageError(page);
320 ret = 0;
321 }
Mike Marshall5db11c22015-07-17 10:38:12 -0400322 /* unlock the page after the ->readpage() routine completes */
323 unlock_page(page);
Mike Marshall0c4b7ca2021-03-28 17:18:11 -0400324 return ret;
Mike Marshall5db11c22015-07-17 10:38:12 -0400325}
326
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500327static int orangefs_write_begin(struct file *file,
328 struct address_space *mapping,
329 loff_t pos, unsigned len, unsigned flags, struct page **pagep,
330 void **fsdata)
331{
332 struct orangefs_write_range *wr;
333 struct page *page;
334 pgoff_t index;
335 int ret;
336
337 index = pos >> PAGE_SHIFT;
338
339 page = grab_cache_page_write_begin(mapping, index, flags);
340 if (!page)
341 return -ENOMEM;
342
343 *pagep = page;
344
345 if (PageDirty(page) && !PagePrivate(page)) {
346 /*
347 * Should be impossible. If it happens, launder the page
348 * since we don't know what's dirty. This will WARN in
349 * orangefs_writepage_locked.
350 */
351 ret = orangefs_launder_page(page);
352 if (ret)
353 return ret;
354 }
355 if (PagePrivate(page)) {
356 struct orangefs_write_range *wr;
357 wr = (struct orangefs_write_range *)page_private(page);
358 if (wr->pos + wr->len == pos &&
359 uid_eq(wr->uid, current_fsuid()) &&
360 gid_eq(wr->gid, current_fsgid())) {
361 wr->len += len;
362 goto okay;
363 } else {
364 ret = orangefs_launder_page(page);
365 if (ret)
366 return ret;
367 }
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500368 }
369
370 wr = kmalloc(sizeof *wr, GFP_KERNEL);
371 if (!wr)
372 return -ENOMEM;
373
374 wr->pos = pos;
375 wr->len = len;
376 wr->uid = current_fsuid();
377 wr->gid = current_fsgid();
Guoqing Jiang4c42be32020-06-01 21:48:00 -0700378 attach_page_private(page, wr);
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500379okay:
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500380 return 0;
381}
382
Martin Brandenburg85ac7992018-02-22 18:10:43 +0000383static int orangefs_write_end(struct file *file, struct address_space *mapping,
384 loff_t pos, unsigned len, unsigned copied, struct page *page, void *fsdata)
385{
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +0000386 struct inode *inode = page->mapping->host;
387 loff_t last_pos = pos + copied;
388
389 /*
390 * No need to use i_size_read() here, the i_size
391 * cannot change under us because we hold the i_mutex.
392 */
393 if (last_pos > inode->i_size)
394 i_size_write(inode, last_pos);
395
396 /* zero the stale part of the page if we did a short copy */
397 if (!PageUptodate(page)) {
398 unsigned from = pos & (PAGE_SIZE - 1);
399 if (copied < len) {
400 zero_user(page, from + copied, len - copied);
401 }
402 /* Set fully written pages uptodate. */
403 if (pos == page_offset(page) &&
404 (len == PAGE_SIZE || pos + len == inode->i_size)) {
405 zero_user_segment(page, from + copied, PAGE_SIZE);
406 SetPageUptodate(page);
407 }
408 }
409
410 set_page_dirty(page);
411 unlock_page(page);
412 put_page(page);
413
Martin Brandenburg85ac7992018-02-22 18:10:43 +0000414 mark_inode_dirty_sync(file_inode(file));
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +0000415 return copied;
Martin Brandenburg85ac7992018-02-22 18:10:43 +0000416}
417
Yi Liu8bb8aef2015-11-24 15:12:14 -0500418static void orangefs_invalidatepage(struct page *page,
Mike Marshall5db11c22015-07-17 10:38:12 -0400419 unsigned int offset,
420 unsigned int length)
421{
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500422 struct orangefs_write_range *wr;
423 wr = (struct orangefs_write_range *)page_private(page);
Mike Marshall5db11c22015-07-17 10:38:12 -0400424
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500425 if (offset == 0 && length == PAGE_SIZE) {
Guoqing Jiang4c42be32020-06-01 21:48:00 -0700426 kfree(detach_page_private(page));
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +0000427 return;
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500428 /* write range entirely within invalidate range (or equal) */
429 } else if (page_offset(page) + offset <= wr->pos &&
430 wr->pos + wr->len <= page_offset(page) + offset + length) {
Guoqing Jiang4c42be32020-06-01 21:48:00 -0700431 kfree(detach_page_private(page));
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500432 /* XXX is this right? only caller in fs */
433 cancel_dirty_page(page);
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +0000434 return;
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500435 /* invalidate range chops off end of write range */
436 } else if (wr->pos < page_offset(page) + offset &&
437 wr->pos + wr->len <= page_offset(page) + offset + length &&
438 page_offset(page) + offset < wr->pos + wr->len) {
439 size_t x;
440 x = wr->pos + wr->len - (page_offset(page) + offset);
441 WARN_ON(x > wr->len);
442 wr->len -= x;
443 wr->uid = current_fsuid();
444 wr->gid = current_fsgid();
445 /* invalidate range chops off beginning of write range */
446 } else if (page_offset(page) + offset <= wr->pos &&
447 page_offset(page) + offset + length < wr->pos + wr->len &&
448 wr->pos < page_offset(page) + offset + length) {
449 size_t x;
450 x = page_offset(page) + offset + length - wr->pos;
451 WARN_ON(x > wr->len);
452 wr->pos += x;
453 wr->len -= x;
454 wr->uid = current_fsuid();
455 wr->gid = current_fsgid();
456 /* invalidate range entirely within write range (punch hole) */
457 } else if (wr->pos < page_offset(page) + offset &&
458 page_offset(page) + offset + length < wr->pos + wr->len) {
459 /* XXX what do we do here... should not WARN_ON */
460 WARN_ON(1);
461 /* punch hole */
462 /*
463 * should we just ignore this and write it out anyway?
464 * it hardly makes sense
465 */
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +0000466 return;
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500467 /* non-overlapping ranges */
468 } else {
469 /* WARN if they do overlap */
470 if (!((page_offset(page) + offset + length <= wr->pos) ^
471 (wr->pos + wr->len <= page_offset(page) + offset))) {
472 WARN_ON(1);
473 printk("invalidate range offset %llu length %u\n",
474 page_offset(page) + offset, length);
475 printk("write range offset %llu length %zu\n",
476 wr->pos, wr->len);
477 }
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +0000478 return;
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500479 }
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +0000480
481 /*
482 * Above there are returns where wr is freed or where we WARN.
483 * Thus the following runs if wr was modified above.
484 */
485
486 orangefs_launder_page(page);
Mike Marshall5db11c22015-07-17 10:38:12 -0400487}
488
Yi Liu8bb8aef2015-11-24 15:12:14 -0500489static int orangefs_releasepage(struct page *page, gfp_t foo)
Mike Marshall5db11c22015-07-17 10:38:12 -0400490{
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500491 return !PagePrivate(page);
492}
493
494static void orangefs_freepage(struct page *page)
495{
Guoqing Jiang4c42be32020-06-01 21:48:00 -0700496 kfree(detach_page_private(page));
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500497}
498
499static int orangefs_launder_page(struct page *page)
500{
501 int r = 0;
502 struct writeback_control wbc = {
503 .sync_mode = WB_SYNC_ALL,
504 .nr_to_write = 0,
505 };
506 wait_on_page_writeback(page);
507 if (clear_page_dirty_for_io(page)) {
508 r = orangefs_writepage_locked(page, &wbc);
509 end_page_writeback(page);
510 }
511 return r;
Mike Marshall5db11c22015-07-17 10:38:12 -0400512}
513
Mike Marshall3903f152016-06-09 15:38:03 -0400514static ssize_t orangefs_direct_IO(struct kiocb *iocb,
515 struct iov_iter *iter)
516{
Martin Brandenburg3e9dfc62018-11-06 19:54:49 +0000517 /*
518 * Comment from original do_readv_writev:
519 * Common entry point for read/write/readv/writev
520 * This function will dispatch it to either the direct I/O
521 * or buffered I/O path depending on the mount options and/or
522 * augmented/extended metadata attached to the file.
523 * Note: File extended attributes override any mount options.
524 */
Martin Brandenburgc453dcf2018-02-16 20:51:24 +0000525 struct file *file = iocb->ki_filp;
Martin Brandenburg3e9dfc62018-11-06 19:54:49 +0000526 loff_t pos = iocb->ki_pos;
527 enum ORANGEFS_io_type type = iov_iter_rw(iter) == WRITE ?
528 ORANGEFS_IO_WRITE : ORANGEFS_IO_READ;
529 loff_t *offset = &pos;
530 struct inode *inode = file->f_mapping->host;
531 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
532 struct orangefs_khandle *handle = &orangefs_inode->refn.khandle;
533 size_t count = iov_iter_count(iter);
Martin Brandenburg3e9dfc62018-11-06 19:54:49 +0000534 ssize_t total_count = 0;
535 ssize_t ret = -EINVAL;
536 int i = 0;
537
538 gossip_debug(GOSSIP_FILE_DEBUG,
539 "%s-BEGIN(%pU): count(%d) after estimate_max_iovecs.\n",
540 __func__,
541 handle,
542 (int)count);
543
544 if (type == ORANGEFS_IO_WRITE) {
545 gossip_debug(GOSSIP_FILE_DEBUG,
546 "%s(%pU): proceeding with offset : %llu, "
547 "size %d\n",
548 __func__,
549 handle,
550 llu(*offset),
551 (int)count);
552 }
553
554 if (count == 0) {
555 ret = 0;
556 goto out;
557 }
558
559 while (iov_iter_count(iter)) {
560 size_t each_count = iov_iter_count(iter);
561 size_t amt_complete;
562 i++;
563
564 /* how much to transfer in this loop iteration */
565 if (each_count > orangefs_bufmap_size_query())
566 each_count = orangefs_bufmap_size_query();
567
568 gossip_debug(GOSSIP_FILE_DEBUG,
569 "%s(%pU): size of each_count(%d)\n",
570 __func__,
571 handle,
572 (int)each_count);
573 gossip_debug(GOSSIP_FILE_DEBUG,
574 "%s(%pU): BEFORE wait_for_io: offset is %d\n",
575 __func__,
576 handle,
577 (int)*offset);
578
579 ret = wait_for_direct_io(type, inode, offset, iter,
Mike Marshallf9bbb682019-11-26 12:39:37 -0500580 each_count, 0, NULL, NULL, file);
Martin Brandenburg3e9dfc62018-11-06 19:54:49 +0000581 gossip_debug(GOSSIP_FILE_DEBUG,
582 "%s(%pU): return from wait_for_io:%d\n",
583 __func__,
584 handle,
585 (int)ret);
586
587 if (ret < 0)
588 goto out;
589
590 *offset += ret;
591 total_count += ret;
592 amt_complete = ret;
593
594 gossip_debug(GOSSIP_FILE_DEBUG,
595 "%s(%pU): AFTER wait_for_io: offset is %d\n",
596 __func__,
597 handle,
598 (int)*offset);
599
600 /*
601 * if we got a short I/O operations,
602 * fall out and return what we got so far
603 */
604 if (amt_complete < each_count)
605 break;
606 } /*end while */
607
608out:
609 if (total_count > 0)
610 ret = total_count;
611 if (ret > 0) {
612 if (type == ORANGEFS_IO_READ) {
613 file_accessed(file);
614 } else {
615 file_update_time(file);
616 if (*offset > i_size_read(inode))
617 i_size_write(inode, *offset);
618 }
619 }
620
621 gossip_debug(GOSSIP_FILE_DEBUG,
622 "%s(%pU): Value(%d) returned.\n",
623 __func__,
624 handle,
625 (int)ret);
626
627 return ret;
Mike Marshall3903f152016-06-09 15:38:03 -0400628}
Mike Marshall5db11c22015-07-17 10:38:12 -0400629
Yi Liu8bb8aef2015-11-24 15:12:14 -0500630/** ORANGEFS2 implementation of address space operations */
Martin Brandenburgbdd6f082018-04-03 16:27:13 +0000631static const struct address_space_operations orangefs_address_operations = {
Martin Brandenburg85ac7992018-02-22 18:10:43 +0000632 .writepage = orangefs_writepage,
Mike Marshall0c4b7ca2021-03-28 17:18:11 -0400633 .readahead = orangefs_readahead,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500634 .readpage = orangefs_readpage,
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +0000635 .writepages = orangefs_writepages,
Martin Brandenburg85ac7992018-02-22 18:10:43 +0000636 .set_page_dirty = __set_page_dirty_nobuffers,
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500637 .write_begin = orangefs_write_begin,
Martin Brandenburg85ac7992018-02-22 18:10:43 +0000638 .write_end = orangefs_write_end,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500639 .invalidatepage = orangefs_invalidatepage,
640 .releasepage = orangefs_releasepage,
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500641 .freepage = orangefs_freepage,
642 .launder_page = orangefs_launder_page,
Mike Marshall3903f152016-06-09 15:38:03 -0400643 .direct_IO = orangefs_direct_IO,
Mike Marshall5db11c22015-07-17 10:38:12 -0400644};
645
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500646vm_fault_t orangefs_page_mkwrite(struct vm_fault *vmf)
647{
648 struct page *page = vmf->page;
649 struct inode *inode = file_inode(vmf->vma->vm_file);
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +0000650 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
651 unsigned long *bitlock = &orangefs_inode->bitlock;
652 vm_fault_t ret;
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500653 struct orangefs_write_range *wr;
654
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +0000655 sb_start_pagefault(inode->i_sb);
656
657 if (wait_on_bit(bitlock, 1, TASK_KILLABLE)) {
658 ret = VM_FAULT_RETRY;
659 goto out;
660 }
661
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500662 lock_page(page);
663 if (PageDirty(page) && !PagePrivate(page)) {
664 /*
665 * Should be impossible. If it happens, launder the page
666 * since we don't know what's dirty. This will WARN in
667 * orangefs_writepage_locked.
668 */
669 if (orangefs_launder_page(page)) {
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +0000670 ret = VM_FAULT_LOCKED|VM_FAULT_RETRY;
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500671 goto out;
672 }
673 }
674 if (PagePrivate(page)) {
675 wr = (struct orangefs_write_range *)page_private(page);
676 if (uid_eq(wr->uid, current_fsuid()) &&
677 gid_eq(wr->gid, current_fsgid())) {
678 wr->pos = page_offset(page);
679 wr->len = PAGE_SIZE;
680 goto okay;
681 } else {
682 if (orangefs_launder_page(page)) {
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +0000683 ret = VM_FAULT_LOCKED|VM_FAULT_RETRY;
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500684 goto out;
685 }
686 }
687 }
688 wr = kmalloc(sizeof *wr, GFP_KERNEL);
689 if (!wr) {
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +0000690 ret = VM_FAULT_LOCKED|VM_FAULT_RETRY;
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500691 goto out;
692 }
693 wr->pos = page_offset(page);
694 wr->len = PAGE_SIZE;
695 wr->uid = current_fsuid();
696 wr->gid = current_fsgid();
Guoqing Jiang4c42be32020-06-01 21:48:00 -0700697 attach_page_private(page, wr);
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500698okay:
699
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500700 file_update_time(vmf->vma->vm_file);
701 if (page->mapping != inode->i_mapping) {
702 unlock_page(page);
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +0000703 ret = VM_FAULT_LOCKED|VM_FAULT_NOPAGE;
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500704 goto out;
705 }
706
707 /*
708 * We mark the page dirty already here so that when freeze is in
709 * progress, we are guaranteed that writeback during freezing will
710 * see the dirty page and writeprotect it again.
711 */
712 set_page_dirty(page);
713 wait_for_stable_page(page);
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +0000714 ret = VM_FAULT_LOCKED;
Martin Brandenburg52e2d0a2018-12-14 15:24:43 -0500715out:
716 sb_end_pagefault(inode->i_sb);
717 return ret;
718}
719
Yi Liu8bb8aef2015-11-24 15:12:14 -0500720static int orangefs_setattr_size(struct inode *inode, struct iattr *iattr)
Mike Marshall5db11c22015-07-17 10:38:12 -0400721{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500722 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
723 struct orangefs_kernel_op_s *new_op;
Martin Brandenburgfecd86a2016-03-23 17:06:25 -0400724 loff_t orig_size;
Mike Marshall5db11c22015-07-17 10:38:12 -0400725 int ret = -EINVAL;
726
727 gossip_debug(GOSSIP_INODE_DEBUG,
728 "%s: %pU: Handle is %pU | fs_id %d | size is %llu\n",
729 __func__,
730 get_khandle_from_ino(inode),
Yi Liu8bb8aef2015-11-24 15:12:14 -0500731 &orangefs_inode->refn.khandle,
732 orangefs_inode->refn.fs_id,
Mike Marshall5db11c22015-07-17 10:38:12 -0400733 iattr->ia_size);
734
Martin Brandenburgfecd86a2016-03-23 17:06:25 -0400735 /* Ensure that we have a up to date size, so we know if it changed. */
Martin Brandenburg8b607852018-02-07 18:44:50 +0000736 ret = orangefs_inode_getattr(inode, ORANGEFS_GETATTR_SIZE);
Martin Brandenburgfecd86a2016-03-23 17:06:25 -0400737 if (ret == -ESTALE)
738 ret = -EIO;
739 if (ret) {
740 gossip_err("%s: orangefs_inode_getattr failed, ret:%d:.\n",
741 __func__, ret);
742 return ret;
743 }
744 orig_size = i_size_read(inode);
745
Martin Brandenburg33713cd2019-04-29 17:09:48 +0000746 /* This is truncate_setsize in a different order. */
747 truncate_pagecache(inode, iattr->ia_size);
748 i_size_write(inode, iattr->ia_size);
749 if (iattr->ia_size > orig_size)
750 pagecache_isize_extended(inode, orig_size, iattr->ia_size);
Mike Marshall5db11c22015-07-17 10:38:12 -0400751
Yi Liu8bb8aef2015-11-24 15:12:14 -0500752 new_op = op_alloc(ORANGEFS_VFS_OP_TRUNCATE);
Mike Marshall5db11c22015-07-17 10:38:12 -0400753 if (!new_op)
754 return -ENOMEM;
755
Yi Liu8bb8aef2015-11-24 15:12:14 -0500756 new_op->upcall.req.truncate.refn = orangefs_inode->refn;
Mike Marshall5db11c22015-07-17 10:38:12 -0400757 new_op->upcall.req.truncate.size = (__s64) iattr->ia_size;
758
Mike Marshall95f5f882018-05-11 17:11:48 -0400759 ret = service_operation(new_op,
760 __func__,
761 get_interruptible_flag(inode));
Mike Marshall5db11c22015-07-17 10:38:12 -0400762
763 /*
764 * the truncate has no downcall members to retrieve, but
765 * the status value tells us if it went through ok or not
766 */
Mike Marshall95f5f882018-05-11 17:11:48 -0400767 gossip_debug(GOSSIP_INODE_DEBUG, "%s: ret:%d:\n", __func__, ret);
Mike Marshall5db11c22015-07-17 10:38:12 -0400768
769 op_release(new_op);
770
771 if (ret != 0)
772 return ret;
773
Martin Brandenburgf83140c2016-04-04 16:26:36 -0400774 if (orig_size != i_size_read(inode))
Mike Marshall5db11c22015-07-17 10:38:12 -0400775 iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
Mike Marshall5db11c22015-07-17 10:38:12 -0400776
777 return ret;
778}
779
Martin Brandenburgafd9fb22018-02-13 20:13:46 +0000780int __orangefs_setattr(struct inode *inode, struct iattr *iattr)
Mike Marshall5db11c22015-07-17 10:38:12 -0400781{
Martin Brandenburgdf2d7332018-02-12 20:29:37 +0000782 int ret;
Mike Marshall5db11c22015-07-17 10:38:12 -0400783
Martin Brandenburgafd9fb22018-02-13 20:13:46 +0000784 if (iattr->ia_valid & ATTR_MODE) {
785 if (iattr->ia_mode & (S_ISVTX)) {
786 if (is_root_handle(inode)) {
787 /*
788 * allow sticky bit to be set on root (since
789 * it shows up that way by default anyhow),
790 * but don't show it to the server
791 */
792 iattr->ia_mode -= S_ISVTX;
793 } else {
794 gossip_debug(GOSSIP_UTILS_DEBUG,
795 "User attempted to set sticky bit on non-root directory; returning EINVAL.\n");
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +0000796 ret = -EINVAL;
797 goto out;
Martin Brandenburgafd9fb22018-02-13 20:13:46 +0000798 }
799 }
800 if (iattr->ia_mode & (S_ISUID)) {
801 gossip_debug(GOSSIP_UTILS_DEBUG,
802 "Attempting to set setuid bit (not supported); returning EINVAL.\n");
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +0000803 ret = -EINVAL;
804 goto out;
Martin Brandenburgafd9fb22018-02-13 20:13:46 +0000805 }
806 }
Mike Marshall5db11c22015-07-17 10:38:12 -0400807
Martin Brandenburg53950ef2017-04-25 15:38:04 -0400808 if (iattr->ia_valid & ATTR_SIZE) {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500809 ret = orangefs_setattr_size(inode, iattr);
Mike Marshall5db11c22015-07-17 10:38:12 -0400810 if (ret)
811 goto out;
812 }
813
Martin Brandenburgafd9fb22018-02-13 20:13:46 +0000814again:
815 spin_lock(&inode->i_lock);
816 if (ORANGEFS_I(inode)->attr_valid) {
817 if (uid_eq(ORANGEFS_I(inode)->attr_uid, current_fsuid()) &&
818 gid_eq(ORANGEFS_I(inode)->attr_gid, current_fsgid())) {
819 ORANGEFS_I(inode)->attr_valid = iattr->ia_valid;
820 } else {
821 spin_unlock(&inode->i_lock);
822 write_inode_now(inode, 1);
823 goto again;
824 }
825 } else {
826 ORANGEFS_I(inode)->attr_valid = iattr->ia_valid;
827 ORANGEFS_I(inode)->attr_uid = current_fsuid();
828 ORANGEFS_I(inode)->attr_gid = current_fsgid();
829 }
Christian Brauner2f221d62021-01-21 14:19:26 +0100830 setattr_copy(&init_user_ns, inode, iattr);
Martin Brandenburgafd9fb22018-02-13 20:13:46 +0000831 spin_unlock(&inode->i_lock);
Mike Marshall5db11c22015-07-17 10:38:12 -0400832 mark_inode_dirty(inode);
833
Martin Brandenburgdf2d7332018-02-12 20:29:37 +0000834 if (iattr->ia_valid & ATTR_MODE)
Mike Marshall5db11c22015-07-17 10:38:12 -0400835 /* change mod on a file that has ACLs */
Christian Braunere65ce2a2021-01-21 14:19:27 +0100836 ret = posix_acl_chmod(&init_user_ns, inode, inode->i_mode);
Mike Marshall5db11c22015-07-17 10:38:12 -0400837
Martin Brandenburgdf2d7332018-02-12 20:29:37 +0000838 ret = 0;
Mike Marshall5db11c22015-07-17 10:38:12 -0400839out:
Martin Brandenburgafd9fb22018-02-13 20:13:46 +0000840 return ret;
841}
842
843/*
844 * Change attributes of an object referenced by dentry.
845 */
Christian Brauner549c7292021-01-21 14:19:43 +0100846int orangefs_setattr(struct user_namespace *mnt_userns, struct dentry *dentry,
847 struct iattr *iattr)
Martin Brandenburgafd9fb22018-02-13 20:13:46 +0000848{
849 int ret;
850 gossip_debug(GOSSIP_INODE_DEBUG, "__orangefs_setattr: called on %pd\n",
851 dentry);
Christian Brauner2f221d62021-01-21 14:19:26 +0100852 ret = setattr_prepare(&init_user_ns, dentry, iattr);
Martin Brandenburgafd9fb22018-02-13 20:13:46 +0000853 if (ret)
854 goto out;
855 ret = __orangefs_setattr(d_inode(dentry), iattr);
856 sync_inode_metadata(d_inode(dentry), 1);
857out:
858 gossip_debug(GOSSIP_INODE_DEBUG, "orangefs_setattr: returning %d\n",
859 ret);
Mike Marshall5db11c22015-07-17 10:38:12 -0400860 return ret;
861}
862
863/*
864 * Obtain attributes of an object given a dentry
865 */
Christian Brauner549c7292021-01-21 14:19:43 +0100866int orangefs_getattr(struct user_namespace *mnt_userns, const struct path *path,
867 struct kstat *stat, u32 request_mask, unsigned int flags)
Mike Marshall5db11c22015-07-17 10:38:12 -0400868{
Colin Ian Kinge6b998a2019-07-28 19:04:07 +0100869 int ret;
David Howellsa528d352017-01-31 16:46:22 +0000870 struct inode *inode = path->dentry->d_inode;
Mike Marshall5db11c22015-07-17 10:38:12 -0400871
872 gossip_debug(GOSSIP_INODE_DEBUG,
Martin Brandenburg5e4f6062018-02-12 17:04:57 +0000873 "orangefs_getattr: called on %pd mask %u\n",
874 path->dentry, request_mask);
Mike Marshall5db11c22015-07-17 10:38:12 -0400875
Martin Brandenburg8b607852018-02-07 18:44:50 +0000876 ret = orangefs_inode_getattr(inode,
877 request_mask & STATX_SIZE ? ORANGEFS_GETATTR_SIZE : 0);
Mike Marshall5db11c22015-07-17 10:38:12 -0400878 if (ret == 0) {
Christian Brauner0d56a452021-01-21 14:19:30 +0100879 generic_fillattr(&init_user_ns, inode, stat);
Mike Marshalla7d3e782016-03-14 15:30:03 -0400880
Mike Marshall5db11c22015-07-17 10:38:12 -0400881 /* override block size reported to stat */
Christoph Hellwig5678b5d2019-01-21 16:23:25 +0100882 if (!(request_mask & STATX_SIZE))
883 stat->result_mask &= ~STATX_SIZE;
Martin Brandenburg7f549102018-05-31 16:37:00 +0000884
Amir Goldstein4f911132021-06-19 12:26:16 +0300885 generic_fill_statx_attr(inode, stat);
Mike Marshall5db11c22015-07-17 10:38:12 -0400886 }
887 return ret;
888}
889
Christian Brauner549c7292021-01-21 14:19:43 +0100890int orangefs_permission(struct user_namespace *mnt_userns,
891 struct inode *inode, int mask)
Martin Brandenburg933287d2016-01-30 13:46:54 -0500892{
893 int ret;
894
895 if (mask & MAY_NOT_BLOCK)
896 return -ECHILD;
897
898 gossip_debug(GOSSIP_INODE_DEBUG, "%s: refreshing\n", __func__);
899
900 /* Make sure the permission (and other common attrs) are up to date. */
Martin Brandenburg8b607852018-02-07 18:44:50 +0000901 ret = orangefs_inode_getattr(inode, 0);
Martin Brandenburg933287d2016-01-30 13:46:54 -0500902 if (ret < 0)
903 return ret;
904
Christian Brauner47291ba2021-01-21 14:19:24 +0100905 return generic_permission(&init_user_ns, inode, mask);
Martin Brandenburg933287d2016-01-30 13:46:54 -0500906}
907
Deepa Dinamani95582b02018-05-08 19:36:02 -0700908int orangefs_update_time(struct inode *inode, struct timespec64 *time, int flags)
Martin Brandenburga55f2d82017-11-07 15:01:40 -0500909{
910 struct iattr iattr;
911 gossip_debug(GOSSIP_INODE_DEBUG, "orangefs_update_time: %pU\n",
912 get_khandle_from_ino(inode));
913 generic_update_time(inode, time, flags);
914 memset(&iattr, 0, sizeof iattr);
915 if (flags & S_ATIME)
916 iattr.ia_valid |= ATTR_ATIME;
917 if (flags & S_CTIME)
918 iattr.ia_valid |= ATTR_CTIME;
919 if (flags & S_MTIME)
920 iattr.ia_valid |= ATTR_MTIME;
Martin Brandenburgafd9fb22018-02-13 20:13:46 +0000921 return __orangefs_setattr(inode, &iattr);
Martin Brandenburga55f2d82017-11-07 15:01:40 -0500922}
923
Miklos Szeredi1f26b062021-04-07 14:36:43 +0200924static int orangefs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
925{
926 u64 val = 0;
927 int ret;
928
929 gossip_debug(GOSSIP_FILE_DEBUG, "%s: called on %pd\n", __func__,
930 dentry);
931
932 ret = orangefs_inode_getxattr(d_inode(dentry),
933 "user.pvfs2.meta_hint",
934 &val, sizeof(val));
935 if (ret < 0 && ret != -ENODATA)
936 return ret;
937
938 gossip_debug(GOSSIP_FILE_DEBUG, "%s: flags=%u\n", __func__, (u32) val);
939
940 fileattr_fill_flags(fa, val);
941 return 0;
942}
943
944static int orangefs_fileattr_set(struct user_namespace *mnt_userns,
945 struct dentry *dentry, struct fileattr *fa)
946{
947 u64 val = 0;
948
949 gossip_debug(GOSSIP_FILE_DEBUG, "%s: called on %pd\n", __func__,
950 dentry);
951 /*
952 * ORANGEFS_MIRROR_FL is set internally when the mirroring mode is
953 * turned on for a file. The user is not allowed to turn on this bit,
954 * but the bit is present if the user first gets the flags and then
955 * updates the flags with some new settings. So, we ignore it in the
956 * following edit. bligon.
957 */
958 if (fileattr_has_fsx(fa) ||
959 (fa->flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NOATIME_FL | ORANGEFS_MIRROR_FL))) {
960 gossip_err("%s: only supports setting one of FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NOATIME_FL\n",
961 __func__);
962 return -EOPNOTSUPP;
963 }
964 val = fa->flags;
965 gossip_debug(GOSSIP_FILE_DEBUG, "%s: flags=%u\n", __func__, (u32) val);
966 return orangefs_inode_setxattr(d_inode(dentry),
967 "user.pvfs2.meta_hint",
968 &val, sizeof(val), 0);
969}
970
Mike Marshall95f5f882018-05-11 17:11:48 -0400971/* ORANGEFS2 implementation of VFS inode operations for files */
Martin Brandenburgbdd6f082018-04-03 16:27:13 +0000972static const struct inode_operations orangefs_file_inode_operations = {
Yi Liu8bb8aef2015-11-24 15:12:14 -0500973 .get_acl = orangefs_get_acl,
974 .set_acl = orangefs_set_acl,
975 .setattr = orangefs_setattr,
976 .getattr = orangefs_getattr,
Yi Liu8bb8aef2015-11-24 15:12:14 -0500977 .listxattr = orangefs_listxattr,
Martin Brandenburg933287d2016-01-30 13:46:54 -0500978 .permission = orangefs_permission,
Martin Brandenburga55f2d82017-11-07 15:01:40 -0500979 .update_time = orangefs_update_time,
Miklos Szeredi1f26b062021-04-07 14:36:43 +0200980 .fileattr_get = orangefs_fileattr_get,
981 .fileattr_set = orangefs_fileattr_set,
Mike Marshall5db11c22015-07-17 10:38:12 -0400982};
983
Yi Liu8bb8aef2015-11-24 15:12:14 -0500984static int orangefs_init_iops(struct inode *inode)
Mike Marshall5db11c22015-07-17 10:38:12 -0400985{
Yi Liu8bb8aef2015-11-24 15:12:14 -0500986 inode->i_mapping->a_ops = &orangefs_address_operations;
Mike Marshall5db11c22015-07-17 10:38:12 -0400987
988 switch (inode->i_mode & S_IFMT) {
989 case S_IFREG:
Yi Liu8bb8aef2015-11-24 15:12:14 -0500990 inode->i_op = &orangefs_file_inode_operations;
991 inode->i_fop = &orangefs_file_operations;
Mike Marshall5db11c22015-07-17 10:38:12 -0400992 break;
993 case S_IFLNK:
Yi Liu8bb8aef2015-11-24 15:12:14 -0500994 inode->i_op = &orangefs_symlink_inode_operations;
Mike Marshall5db11c22015-07-17 10:38:12 -0400995 break;
996 case S_IFDIR:
Yi Liu8bb8aef2015-11-24 15:12:14 -0500997 inode->i_op = &orangefs_dir_inode_operations;
998 inode->i_fop = &orangefs_dir_operations;
Mike Marshall5db11c22015-07-17 10:38:12 -0400999 break;
1000 default:
1001 gossip_debug(GOSSIP_INODE_DEBUG,
1002 "%s: unsupported mode\n",
1003 __func__);
1004 return -EINVAL;
1005 }
1006
1007 return 0;
1008}
1009
1010/*
Mike Marshall95f5f882018-05-11 17:11:48 -04001011 * Given an ORANGEFS object identifier (fsid, handle), convert it into
1012 * a ino_t type that will be used as a hash-index from where the handle will
Mike Marshall5db11c22015-07-17 10:38:12 -04001013 * be searched for in the VFS hash table of inodes.
1014 */
Yi Liu8bb8aef2015-11-24 15:12:14 -05001015static inline ino_t orangefs_handle_hash(struct orangefs_object_kref *ref)
Mike Marshall5db11c22015-07-17 10:38:12 -04001016{
1017 if (!ref)
1018 return 0;
Yi Liu8bb8aef2015-11-24 15:12:14 -05001019 return orangefs_khandle_to_ino(&(ref->khandle));
Mike Marshall5db11c22015-07-17 10:38:12 -04001020}
1021
1022/*
1023 * Called to set up an inode from iget5_locked.
1024 */
Yi Liu8bb8aef2015-11-24 15:12:14 -05001025static int orangefs_set_inode(struct inode *inode, void *data)
Mike Marshall5db11c22015-07-17 10:38:12 -04001026{
Yi Liu8bb8aef2015-11-24 15:12:14 -05001027 struct orangefs_object_kref *ref = (struct orangefs_object_kref *) data;
Martin Brandenburga4c680a2016-03-16 14:35:21 -04001028 ORANGEFS_I(inode)->refn.fs_id = ref->fs_id;
1029 ORANGEFS_I(inode)->refn.khandle = ref->khandle;
Martin Brandenburgafd9fb22018-02-13 20:13:46 +00001030 ORANGEFS_I(inode)->attr_valid = 0;
Martin Brandenburgfc2e2e92017-12-12 13:46:30 -05001031 hash_init(ORANGEFS_I(inode)->xattr_cache);
Martin Brandenburg8f04e1b2019-02-12 20:19:06 +00001032 ORANGEFS_I(inode)->mapping_time = jiffies - 1;
1033 ORANGEFS_I(inode)->bitlock = 0;
Mike Marshall5db11c22015-07-17 10:38:12 -04001034 return 0;
1035}
1036
1037/*
1038 * Called to determine if handles match.
1039 */
Yi Liu8bb8aef2015-11-24 15:12:14 -05001040static int orangefs_test_inode(struct inode *inode, void *data)
Mike Marshall5db11c22015-07-17 10:38:12 -04001041{
Yi Liu8bb8aef2015-11-24 15:12:14 -05001042 struct orangefs_object_kref *ref = (struct orangefs_object_kref *) data;
1043 struct orangefs_inode_s *orangefs_inode = NULL;
Mike Marshall5db11c22015-07-17 10:38:12 -04001044
Yi Liu8bb8aef2015-11-24 15:12:14 -05001045 orangefs_inode = ORANGEFS_I(inode);
Mike Marshall95f5f882018-05-11 17:11:48 -04001046 /* test handles and fs_ids... */
1047 return (!ORANGEFS_khandle_cmp(&(orangefs_inode->refn.khandle),
1048 &(ref->khandle)) &&
1049 orangefs_inode->refn.fs_id == ref->fs_id);
Mike Marshall5db11c22015-07-17 10:38:12 -04001050}
1051
1052/*
Yi Liu8bb8aef2015-11-24 15:12:14 -05001053 * Front-end to lookup the inode-cache maintained by the VFS using the ORANGEFS
Mike Marshall5db11c22015-07-17 10:38:12 -04001054 * file handle.
1055 *
1056 * @sb: the file system super block instance.
Mike Marshall95f5f882018-05-11 17:11:48 -04001057 * @ref: The ORANGEFS object for which we are trying to locate an inode.
Mike Marshall5db11c22015-07-17 10:38:12 -04001058 */
Mike Marshall95f5f882018-05-11 17:11:48 -04001059struct inode *orangefs_iget(struct super_block *sb,
1060 struct orangefs_object_kref *ref)
Mike Marshall5db11c22015-07-17 10:38:12 -04001061{
1062 struct inode *inode = NULL;
1063 unsigned long hash;
1064 int error;
1065
Yi Liu8bb8aef2015-11-24 15:12:14 -05001066 hash = orangefs_handle_hash(ref);
Mike Marshall95f5f882018-05-11 17:11:48 -04001067 inode = iget5_locked(sb,
1068 hash,
1069 orangefs_test_inode,
1070 orangefs_set_inode,
1071 ref);
Mike Marshallb5d72cd2018-10-18 13:52:23 -04001072
1073 if (!inode)
1074 return ERR_PTR(-ENOMEM);
1075
1076 if (!(inode->i_state & I_NEW))
Mike Marshall5db11c22015-07-17 10:38:12 -04001077 return inode;
1078
Martin Brandenburg8b607852018-02-07 18:44:50 +00001079 error = orangefs_inode_getattr(inode, ORANGEFS_GETATTR_NEW);
Mike Marshall5db11c22015-07-17 10:38:12 -04001080 if (error) {
1081 iget_failed(inode);
1082 return ERR_PTR(error);
1083 }
1084
1085 inode->i_ino = hash; /* needed for stat etc */
Yi Liu8bb8aef2015-11-24 15:12:14 -05001086 orangefs_init_iops(inode);
Mike Marshall5db11c22015-07-17 10:38:12 -04001087 unlock_new_inode(inode);
1088
1089 gossip_debug(GOSSIP_INODE_DEBUG,
1090 "iget handle %pU, fsid %d hash %ld i_ino %lu\n",
1091 &ref->khandle,
1092 ref->fs_id,
1093 hash,
1094 inode->i_ino);
1095
1096 return inode;
1097}
1098
1099/*
1100 * Allocate an inode for a newly created file and insert it into the inode hash.
1101 */
Yi Liu8bb8aef2015-11-24 15:12:14 -05001102struct inode *orangefs_new_inode(struct super_block *sb, struct inode *dir,
1103 int mode, dev_t dev, struct orangefs_object_kref *ref)
Mike Marshall5db11c22015-07-17 10:38:12 -04001104{
Yi Liu8bb8aef2015-11-24 15:12:14 -05001105 unsigned long hash = orangefs_handle_hash(ref);
Mike Marshall5db11c22015-07-17 10:38:12 -04001106 struct inode *inode;
1107 int error;
1108
1109 gossip_debug(GOSSIP_INODE_DEBUG,
Mike Marshall52534872016-02-16 17:09:09 -05001110 "%s:(sb is %p | MAJOR(dev)=%u | MINOR(dev)=%u mode=%o)\n",
1111 __func__,
Mike Marshall5db11c22015-07-17 10:38:12 -04001112 sb,
1113 MAJOR(dev),
1114 MINOR(dev),
1115 mode);
1116
1117 inode = new_inode(sb);
1118 if (!inode)
Mike Marshall56249992018-10-18 13:47:16 -04001119 return ERR_PTR(-ENOMEM);
Mike Marshall5db11c22015-07-17 10:38:12 -04001120
Yi Liu8bb8aef2015-11-24 15:12:14 -05001121 orangefs_set_inode(inode, ref);
Mike Marshall5db11c22015-07-17 10:38:12 -04001122 inode->i_ino = hash; /* needed for stat etc */
1123
Martin Brandenburg8b607852018-02-07 18:44:50 +00001124 error = orangefs_inode_getattr(inode, ORANGEFS_GETATTR_NEW);
Mike Marshall5db11c22015-07-17 10:38:12 -04001125 if (error)
1126 goto out_iput;
1127
Yi Liu8bb8aef2015-11-24 15:12:14 -05001128 orangefs_init_iops(inode);
Mike Marshall5db11c22015-07-17 10:38:12 -04001129 inode->i_rdev = dev;
1130
Yi Liu8bb8aef2015-11-24 15:12:14 -05001131 error = insert_inode_locked4(inode, hash, orangefs_test_inode, ref);
Mike Marshall5db11c22015-07-17 10:38:12 -04001132 if (error < 0)
1133 goto out_iput;
1134
1135 gossip_debug(GOSSIP_INODE_DEBUG,
1136 "Initializing ACL's for inode %pU\n",
1137 get_khandle_from_ino(inode));
Yi Liu8bb8aef2015-11-24 15:12:14 -05001138 orangefs_init_acl(inode, dir);
Mike Marshall5db11c22015-07-17 10:38:12 -04001139 return inode;
1140
1141out_iput:
1142 iput(inode);
1143 return ERR_PTR(error);
1144}