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