xref: /openbmc/linux/fs/orangefs/inode.c (revision 913e9928)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
25db11c21SMike Marshall /*
35db11c21SMike Marshall  * (C) 2001 Clemson University and The University of Chicago
4afd9fb2aSMartin Brandenburg  * Copyright 2018 Omnibond Systems, L.L.C.
55db11c21SMike Marshall  *
65db11c21SMike Marshall  * See COPYING in top-level directory.
75db11c21SMike Marshall  */
85db11c21SMike Marshall 
95db11c21SMike Marshall /*
105db11c21SMike Marshall  *  Linux VFS inode operations.
115db11c21SMike Marshall  */
125db11c21SMike Marshall 
13ccdf7741SChristoph Hellwig #include <linux/blkdev.h>
141f26b062SMiklos Szeredi #include <linux/fileattr.h>
155db11c21SMike Marshall #include "protocol.h"
16575e9461SMike Marshall #include "orangefs-kernel.h"
17575e9461SMike Marshall #include "orangefs-bufmap.h"
185db11c21SMike Marshall 
orangefs_writepage_locked(struct page * page,struct writeback_control * wbc)1952e2d0a3SMartin Brandenburg static int orangefs_writepage_locked(struct page *page,
2052e2d0a3SMartin Brandenburg     struct writeback_control *wbc)
2185ac799cSMartin Brandenburg {
2285ac799cSMartin Brandenburg 	struct inode *inode = page->mapping->host;
2352e2d0a3SMartin Brandenburg 	struct orangefs_write_range *wr = NULL;
2485ac799cSMartin Brandenburg 	struct iov_iter iter;
2585ac799cSMartin Brandenburg 	struct bio_vec bv;
2685ac799cSMartin Brandenburg 	size_t len, wlen;
2785ac799cSMartin Brandenburg 	ssize_t ret;
2885ac799cSMartin Brandenburg 	loff_t off;
2985ac799cSMartin Brandenburg 
3085ac799cSMartin Brandenburg 	set_page_writeback(page);
3185ac799cSMartin Brandenburg 
3285ac799cSMartin Brandenburg 	len = i_size_read(inode);
3352e2d0a3SMartin Brandenburg 	if (PagePrivate(page)) {
3452e2d0a3SMartin Brandenburg 		wr = (struct orangefs_write_range *)page_private(page);
358f04e1beSMartin Brandenburg 		WARN_ON(wr->pos >= len);
3652e2d0a3SMartin Brandenburg 		off = wr->pos;
3752e2d0a3SMartin Brandenburg 		if (off + wr->len > len)
3852e2d0a3SMartin Brandenburg 			wlen = len - off;
3952e2d0a3SMartin Brandenburg 		else
4052e2d0a3SMartin Brandenburg 			wlen = wr->len;
4152e2d0a3SMartin Brandenburg 	} else {
4252e2d0a3SMartin Brandenburg 		WARN_ON(1);
4352e2d0a3SMartin Brandenburg 		off = page_offset(page);
4485ac799cSMartin Brandenburg 		if (off + PAGE_SIZE > len)
4585ac799cSMartin Brandenburg 			wlen = len - off;
4685ac799cSMartin Brandenburg 		else
4785ac799cSMartin Brandenburg 			wlen = PAGE_SIZE;
4852e2d0a3SMartin Brandenburg 	}
492a40be81SMatthew Wilcox (Oracle) 	/* Should've been handled in orangefs_invalidate_folio. */
5052e2d0a3SMartin Brandenburg 	WARN_ON(off == len || off + wlen > len);
5185ac799cSMartin Brandenburg 
5252e2d0a3SMartin Brandenburg 	WARN_ON(wlen == 0);
538ead80b2SChristoph Hellwig 	bvec_set_page(&bv, page, wlen, off % PAGE_SIZE);
54de4eda9dSAl Viro 	iov_iter_bvec(&iter, ITER_SOURCE, &bv, 1, wlen);
5585ac799cSMartin Brandenburg 
5685ac799cSMartin Brandenburg 	ret = wait_for_direct_io(ORANGEFS_IO_WRITE, inode, &off, &iter, wlen,
57f9bbb682SMike Marshall 	    len, wr, NULL, NULL);
5885ac799cSMartin Brandenburg 	if (ret < 0) {
5985ac799cSMartin Brandenburg 		SetPageError(page);
6085ac799cSMartin Brandenburg 		mapping_set_error(page->mapping, ret);
6185ac799cSMartin Brandenburg 	} else {
6285ac799cSMartin Brandenburg 		ret = 0;
6385ac799cSMartin Brandenburg 	}
644c42be38SGuoqing Jiang 	kfree(detach_page_private(page));
6552e2d0a3SMartin Brandenburg 	return ret;
6652e2d0a3SMartin Brandenburg }
6752e2d0a3SMartin Brandenburg 
orangefs_writepage(struct page * page,struct writeback_control * wbc)6852e2d0a3SMartin Brandenburg static int orangefs_writepage(struct page *page, struct writeback_control *wbc)
6952e2d0a3SMartin Brandenburg {
7052e2d0a3SMartin Brandenburg 	int ret;
7152e2d0a3SMartin Brandenburg 	ret = orangefs_writepage_locked(page, wbc);
7285ac799cSMartin Brandenburg 	unlock_page(page);
7385ac799cSMartin Brandenburg 	end_page_writeback(page);
7485ac799cSMartin Brandenburg 	return ret;
7585ac799cSMartin Brandenburg }
7685ac799cSMartin Brandenburg 
778f04e1beSMartin Brandenburg struct orangefs_writepages {
788f04e1beSMartin Brandenburg 	loff_t off;
798f04e1beSMartin Brandenburg 	size_t len;
808f04e1beSMartin Brandenburg 	kuid_t uid;
818f04e1beSMartin Brandenburg 	kgid_t gid;
828f04e1beSMartin Brandenburg 	int maxpages;
838f04e1beSMartin Brandenburg 	int npages;
848f04e1beSMartin Brandenburg 	struct page **pages;
858f04e1beSMartin Brandenburg 	struct bio_vec *bv;
868f04e1beSMartin Brandenburg };
878f04e1beSMartin Brandenburg 
orangefs_writepages_work(struct orangefs_writepages * ow,struct writeback_control * wbc)888f04e1beSMartin Brandenburg static int orangefs_writepages_work(struct orangefs_writepages *ow,
898f04e1beSMartin Brandenburg     struct writeback_control *wbc)
908f04e1beSMartin Brandenburg {
918f04e1beSMartin Brandenburg 	struct inode *inode = ow->pages[0]->mapping->host;
928f04e1beSMartin Brandenburg 	struct orangefs_write_range *wrp, wr;
938f04e1beSMartin Brandenburg 	struct iov_iter iter;
948f04e1beSMartin Brandenburg 	ssize_t ret;
958f04e1beSMartin Brandenburg 	size_t len;
968f04e1beSMartin Brandenburg 	loff_t off;
978f04e1beSMartin Brandenburg 	int i;
988f04e1beSMartin Brandenburg 
998f04e1beSMartin Brandenburg 	len = i_size_read(inode);
1008f04e1beSMartin Brandenburg 
1018f04e1beSMartin Brandenburg 	for (i = 0; i < ow->npages; i++) {
1028f04e1beSMartin Brandenburg 		set_page_writeback(ow->pages[i]);
1038ead80b2SChristoph Hellwig 		bvec_set_page(&ow->bv[i], ow->pages[i],
1048ead80b2SChristoph Hellwig 			      min(page_offset(ow->pages[i]) + PAGE_SIZE,
1058f04e1beSMartin Brandenburg 			          ow->off + ow->len) -
1068ead80b2SChristoph Hellwig 			      max(ow->off, page_offset(ow->pages[i])),
1078ead80b2SChristoph Hellwig 			      i == 0 ? ow->off - page_offset(ow->pages[i]) : 0);
1088f04e1beSMartin Brandenburg 	}
109de4eda9dSAl Viro 	iov_iter_bvec(&iter, ITER_SOURCE, ow->bv, ow->npages, ow->len);
1108f04e1beSMartin Brandenburg 
1118f04e1beSMartin Brandenburg 	WARN_ON(ow->off >= len);
1128f04e1beSMartin Brandenburg 	if (ow->off + ow->len > len)
1138f04e1beSMartin Brandenburg 		ow->len = len - ow->off;
1148f04e1beSMartin Brandenburg 
1158f04e1beSMartin Brandenburg 	off = ow->off;
1168f04e1beSMartin Brandenburg 	wr.uid = ow->uid;
1178f04e1beSMartin Brandenburg 	wr.gid = ow->gid;
1188f04e1beSMartin Brandenburg 	ret = wait_for_direct_io(ORANGEFS_IO_WRITE, inode, &off, &iter, ow->len,
119f9bbb682SMike Marshall 	    0, &wr, NULL, NULL);
1208f04e1beSMartin Brandenburg 	if (ret < 0) {
1218f04e1beSMartin Brandenburg 		for (i = 0; i < ow->npages; i++) {
1228f04e1beSMartin Brandenburg 			SetPageError(ow->pages[i]);
1238f04e1beSMartin Brandenburg 			mapping_set_error(ow->pages[i]->mapping, ret);
1248f04e1beSMartin Brandenburg 			if (PagePrivate(ow->pages[i])) {
1258f04e1beSMartin Brandenburg 				wrp = (struct orangefs_write_range *)
1268f04e1beSMartin Brandenburg 				    page_private(ow->pages[i]);
1278f04e1beSMartin Brandenburg 				ClearPagePrivate(ow->pages[i]);
1288f04e1beSMartin Brandenburg 				put_page(ow->pages[i]);
1298f04e1beSMartin Brandenburg 				kfree(wrp);
1308f04e1beSMartin Brandenburg 			}
1318f04e1beSMartin Brandenburg 			end_page_writeback(ow->pages[i]);
1328f04e1beSMartin Brandenburg 			unlock_page(ow->pages[i]);
1338f04e1beSMartin Brandenburg 		}
1348f04e1beSMartin Brandenburg 	} else {
1358f04e1beSMartin Brandenburg 		ret = 0;
1368f04e1beSMartin Brandenburg 		for (i = 0; i < ow->npages; i++) {
1378f04e1beSMartin Brandenburg 			if (PagePrivate(ow->pages[i])) {
1388f04e1beSMartin Brandenburg 				wrp = (struct orangefs_write_range *)
1398f04e1beSMartin Brandenburg 				    page_private(ow->pages[i]);
1408f04e1beSMartin Brandenburg 				ClearPagePrivate(ow->pages[i]);
1418f04e1beSMartin Brandenburg 				put_page(ow->pages[i]);
1428f04e1beSMartin Brandenburg 				kfree(wrp);
1438f04e1beSMartin Brandenburg 			}
1448f04e1beSMartin Brandenburg 			end_page_writeback(ow->pages[i]);
1458f04e1beSMartin Brandenburg 			unlock_page(ow->pages[i]);
1468f04e1beSMartin Brandenburg 		}
1478f04e1beSMartin Brandenburg 	}
1488f04e1beSMartin Brandenburg 	return ret;
1498f04e1beSMartin Brandenburg }
1508f04e1beSMartin Brandenburg 
orangefs_writepages_callback(struct folio * folio,struct writeback_control * wbc,void * data)151d585bdbeSMatthew Wilcox (Oracle) static int orangefs_writepages_callback(struct folio *folio,
1528f04e1beSMartin Brandenburg 		struct writeback_control *wbc, void *data)
1538f04e1beSMartin Brandenburg {
1548f04e1beSMartin Brandenburg 	struct orangefs_writepages *ow = data;
155d585bdbeSMatthew Wilcox (Oracle) 	struct orangefs_write_range *wr = folio->private;
1568f04e1beSMartin Brandenburg 	int ret;
1578f04e1beSMartin Brandenburg 
158d585bdbeSMatthew Wilcox (Oracle) 	if (!wr) {
159d585bdbeSMatthew Wilcox (Oracle) 		folio_unlock(folio);
1608f04e1beSMartin Brandenburg 		/* It's not private so there's nothing to write, right? */
1618f04e1beSMartin Brandenburg 		printk("writepages_callback not private!\n");
1628f04e1beSMartin Brandenburg 		BUG();
1638f04e1beSMartin Brandenburg 		return 0;
1648f04e1beSMartin Brandenburg 	}
1658f04e1beSMartin Brandenburg 
1668f04e1beSMartin Brandenburg 	ret = -1;
1678f04e1beSMartin Brandenburg 	if (ow->npages == 0) {
1688f04e1beSMartin Brandenburg 		ow->off = wr->pos;
1698f04e1beSMartin Brandenburg 		ow->len = wr->len;
1708f04e1beSMartin Brandenburg 		ow->uid = wr->uid;
1718f04e1beSMartin Brandenburg 		ow->gid = wr->gid;
172d585bdbeSMatthew Wilcox (Oracle) 		ow->pages[ow->npages++] = &folio->page;
1738f04e1beSMartin Brandenburg 		ret = 0;
1748f04e1beSMartin Brandenburg 		goto done;
1758f04e1beSMartin Brandenburg 	}
1768f04e1beSMartin Brandenburg 	if (!uid_eq(ow->uid, wr->uid) || !gid_eq(ow->gid, wr->gid)) {
1778f04e1beSMartin Brandenburg 		orangefs_writepages_work(ow, wbc);
1788f04e1beSMartin Brandenburg 		ow->npages = 0;
1798f04e1beSMartin Brandenburg 		ret = -1;
1808f04e1beSMartin Brandenburg 		goto done;
1818f04e1beSMartin Brandenburg 	}
1828f04e1beSMartin Brandenburg 	if (ow->off + ow->len == wr->pos) {
1838f04e1beSMartin Brandenburg 		ow->len += wr->len;
184d585bdbeSMatthew Wilcox (Oracle) 		ow->pages[ow->npages++] = &folio->page;
1858f04e1beSMartin Brandenburg 		ret = 0;
1868f04e1beSMartin Brandenburg 		goto done;
1878f04e1beSMartin Brandenburg 	}
1888f04e1beSMartin Brandenburg done:
1898f04e1beSMartin Brandenburg 	if (ret == -1) {
1908f04e1beSMartin Brandenburg 		if (ow->npages) {
1918f04e1beSMartin Brandenburg 			orangefs_writepages_work(ow, wbc);
1928f04e1beSMartin Brandenburg 			ow->npages = 0;
1938f04e1beSMartin Brandenburg 		}
194d585bdbeSMatthew Wilcox (Oracle) 		ret = orangefs_writepage_locked(&folio->page, wbc);
195d585bdbeSMatthew Wilcox (Oracle) 		mapping_set_error(folio->mapping, ret);
196d585bdbeSMatthew Wilcox (Oracle) 		folio_unlock(folio);
197d585bdbeSMatthew Wilcox (Oracle) 		folio_end_writeback(folio);
1988f04e1beSMartin Brandenburg 	} else {
1998f04e1beSMartin Brandenburg 		if (ow->npages == ow->maxpages) {
2008f04e1beSMartin Brandenburg 			orangefs_writepages_work(ow, wbc);
2018f04e1beSMartin Brandenburg 			ow->npages = 0;
2028f04e1beSMartin Brandenburg 		}
2038f04e1beSMartin Brandenburg 	}
2048f04e1beSMartin Brandenburg 	return ret;
2058f04e1beSMartin Brandenburg }
2068f04e1beSMartin Brandenburg 
orangefs_writepages(struct address_space * mapping,struct writeback_control * wbc)2078f04e1beSMartin Brandenburg static int orangefs_writepages(struct address_space *mapping,
2088f04e1beSMartin Brandenburg     struct writeback_control *wbc)
2098f04e1beSMartin Brandenburg {
2108f04e1beSMartin Brandenburg 	struct orangefs_writepages *ow;
2118f04e1beSMartin Brandenburg 	struct blk_plug plug;
2128f04e1beSMartin Brandenburg 	int ret;
2138f04e1beSMartin Brandenburg 	ow = kzalloc(sizeof(struct orangefs_writepages), GFP_KERNEL);
2148f04e1beSMartin Brandenburg 	if (!ow)
2158f04e1beSMartin Brandenburg 		return -ENOMEM;
2168f04e1beSMartin Brandenburg 	ow->maxpages = orangefs_bufmap_size_query()/PAGE_SIZE;
2178f04e1beSMartin Brandenburg 	ow->pages = kcalloc(ow->maxpages, sizeof(struct page *), GFP_KERNEL);
2188f04e1beSMartin Brandenburg 	if (!ow->pages) {
2198f04e1beSMartin Brandenburg 		kfree(ow);
2208f04e1beSMartin Brandenburg 		return -ENOMEM;
2218f04e1beSMartin Brandenburg 	}
2228f04e1beSMartin Brandenburg 	ow->bv = kcalloc(ow->maxpages, sizeof(struct bio_vec), GFP_KERNEL);
2238f04e1beSMartin Brandenburg 	if (!ow->bv) {
2248f04e1beSMartin Brandenburg 		kfree(ow->pages);
2258f04e1beSMartin Brandenburg 		kfree(ow);
2268f04e1beSMartin Brandenburg 		return -ENOMEM;
2278f04e1beSMartin Brandenburg 	}
2288f04e1beSMartin Brandenburg 	blk_start_plug(&plug);
2298f04e1beSMartin Brandenburg 	ret = write_cache_pages(mapping, wbc, orangefs_writepages_callback, ow);
2308f04e1beSMartin Brandenburg 	if (ow->npages)
2318f04e1beSMartin Brandenburg 		ret = orangefs_writepages_work(ow, wbc);
2328f04e1beSMartin Brandenburg 	blk_finish_plug(&plug);
2338f04e1beSMartin Brandenburg 	kfree(ow->pages);
2348f04e1beSMartin Brandenburg 	kfree(ow->bv);
2358f04e1beSMartin Brandenburg 	kfree(ow);
2368f04e1beSMartin Brandenburg 	return ret;
2378f04e1beSMartin Brandenburg }
2388f04e1beSMartin Brandenburg 
239eabf038fSMatthew Wilcox (Oracle) static int orangefs_launder_folio(struct folio *);
240dd59a647SMike Marshall 
orangefs_readahead(struct readahead_control * rac)2410c4b7cadSMike Marshall static void orangefs_readahead(struct readahead_control *rac)
2420c4b7cadSMike Marshall {
2430c4b7cadSMike Marshall 	loff_t offset;
2440c4b7cadSMike Marshall 	struct iov_iter iter;
24524523e45SMike Marshall 	struct inode *inode = rac->mapping->host;
2460c4b7cadSMike Marshall 	struct xarray *i_pages;
247cd01049dSPankaj Raghav 	struct folio *folio;
2480c4b7cadSMike Marshall 	loff_t new_start = readahead_pos(rac);
2490c4b7cadSMike Marshall 	int ret;
2500c4b7cadSMike Marshall 	size_t new_len = 0;
2510c4b7cadSMike Marshall 
2520c4b7cadSMike Marshall 	loff_t bytes_remaining = inode->i_size - readahead_pos(rac);
2530c4b7cadSMike Marshall 	loff_t pages_remaining = bytes_remaining / PAGE_SIZE;
2540c4b7cadSMike Marshall 
2550c4b7cadSMike Marshall 	if (pages_remaining >= 1024)
2560c4b7cadSMike Marshall 		new_len = 4194304;
2570c4b7cadSMike Marshall 	else if (pages_remaining > readahead_count(rac))
2580c4b7cadSMike Marshall 		new_len = bytes_remaining;
2590c4b7cadSMike Marshall 
2600c4b7cadSMike Marshall 	if (new_len)
2610c4b7cadSMike Marshall 		readahead_expand(rac, new_start, new_len);
2620c4b7cadSMike Marshall 
2630c4b7cadSMike Marshall 	offset = readahead_pos(rac);
26424523e45SMike Marshall 	i_pages = &rac->mapping->i_pages;
2650c4b7cadSMike Marshall 
266de4eda9dSAl Viro 	iov_iter_xarray(&iter, ITER_DEST, i_pages, offset, readahead_length(rac));
2670c4b7cadSMike Marshall 
2680c4b7cadSMike Marshall 	/* read in the pages. */
2690c4b7cadSMike Marshall 	if ((ret = wait_for_direct_io(ORANGEFS_IO_READ, inode,
2700c4b7cadSMike Marshall 			&offset, &iter, readahead_length(rac),
27124523e45SMike Marshall 			inode->i_size, NULL, NULL, rac->file)) < 0)
2720c4b7cadSMike Marshall 		gossip_debug(GOSSIP_FILE_DEBUG,
2730c4b7cadSMike Marshall 			"%s: wait_for_direct_io failed. \n", __func__);
2740c4b7cadSMike Marshall 	else
2750c4b7cadSMike Marshall 		ret = 0;
2760c4b7cadSMike Marshall 
2770c4b7cadSMike Marshall 	/* clean up. */
278cd01049dSPankaj Raghav 	while ((folio = readahead_folio(rac))) {
279cd01049dSPankaj Raghav 		if (!ret)
280cd01049dSPankaj Raghav 			folio_mark_uptodate(folio);
281cd01049dSPankaj Raghav 		folio_unlock(folio);
2820c4b7cadSMike Marshall 	}
2830c4b7cadSMike Marshall }
2840c4b7cadSMike Marshall 
orangefs_read_folio(struct file * file,struct folio * folio)2851a641788SMatthew Wilcox (Oracle) static int orangefs_read_folio(struct file *file, struct folio *folio)
2865db11c21SMike Marshall {
2871a641788SMatthew Wilcox (Oracle) 	struct inode *inode = folio->mapping->host;
288c453dcfcSMartin Brandenburg 	struct iov_iter iter;
289c453dcfcSMartin Brandenburg 	struct bio_vec bv;
290c453dcfcSMartin Brandenburg 	ssize_t ret;
2911a641788SMatthew Wilcox (Oracle) 	loff_t off; /* offset of this folio in the file */
292dd59a647SMike Marshall 
293eabf038fSMatthew Wilcox (Oracle) 	if (folio_test_dirty(folio))
294eabf038fSMatthew Wilcox (Oracle) 		orangefs_launder_folio(folio);
29574f68fceSAl Viro 
2961a641788SMatthew Wilcox (Oracle) 	off = folio_pos(folio);
2978ead80b2SChristoph Hellwig 	bvec_set_folio(&bv, folio, folio_size(folio), 0);
298de4eda9dSAl Viro 	iov_iter_bvec(&iter, ITER_DEST, &bv, 1, folio_size(folio));
2995db11c21SMike Marshall 
300c453dcfcSMartin Brandenburg 	ret = wait_for_direct_io(ORANGEFS_IO_READ, inode, &off, &iter,
3011a641788SMatthew Wilcox (Oracle) 			folio_size(folio), inode->i_size, NULL, NULL, file);
3029329883aSMatthew Wilcox (Oracle) 	/* this will only zero remaining unread portions of the folio data */
303c453dcfcSMartin Brandenburg 	iov_iter_zero(~0U, &iter);
3045db11c21SMike Marshall 	/* takes care of potential aliasing */
3051a641788SMatthew Wilcox (Oracle) 	flush_dcache_folio(folio);
306c453dcfcSMartin Brandenburg 	if (ret < 0) {
3071a641788SMatthew Wilcox (Oracle) 		folio_set_error(folio);
3085db11c21SMike Marshall 	} else {
3091a641788SMatthew Wilcox (Oracle) 		folio_mark_uptodate(folio);
3105db11c21SMike Marshall 		ret = 0;
3115db11c21SMike Marshall 	}
3121a641788SMatthew Wilcox (Oracle) 	/* unlock the folio after the ->read_folio() routine completes */
3131a641788SMatthew Wilcox (Oracle) 	folio_unlock(folio);
314dd59a647SMike Marshall         return ret;
315dd59a647SMike Marshall }
31652e2d0a3SMartin Brandenburg 
orangefs_write_begin(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,struct page ** pagep,void ** fsdata)31752e2d0a3SMartin Brandenburg static int orangefs_write_begin(struct file *file,
3189d6b0cd7SMatthew Wilcox (Oracle) 		struct address_space *mapping, loff_t pos, unsigned len,
3199d6b0cd7SMatthew Wilcox (Oracle) 		struct page **pagep, void **fsdata)
32052e2d0a3SMartin Brandenburg {
32152e2d0a3SMartin Brandenburg 	struct orangefs_write_range *wr;
322eabf038fSMatthew Wilcox (Oracle) 	struct folio *folio;
32352e2d0a3SMartin Brandenburg 	struct page *page;
32452e2d0a3SMartin Brandenburg 	pgoff_t index;
32552e2d0a3SMartin Brandenburg 	int ret;
32652e2d0a3SMartin Brandenburg 
32752e2d0a3SMartin Brandenburg 	index = pos >> PAGE_SHIFT;
32852e2d0a3SMartin Brandenburg 
329b7446e7cSMatthew Wilcox (Oracle) 	page = grab_cache_page_write_begin(mapping, index);
33052e2d0a3SMartin Brandenburg 	if (!page)
33152e2d0a3SMartin Brandenburg 		return -ENOMEM;
33252e2d0a3SMartin Brandenburg 
33352e2d0a3SMartin Brandenburg 	*pagep = page;
334eabf038fSMatthew Wilcox (Oracle) 	folio = page_folio(page);
33552e2d0a3SMartin Brandenburg 
336eabf038fSMatthew Wilcox (Oracle) 	if (folio_test_dirty(folio) && !folio_test_private(folio)) {
33752e2d0a3SMartin Brandenburg 		/*
33852e2d0a3SMartin Brandenburg 		 * Should be impossible.  If it happens, launder the page
33952e2d0a3SMartin Brandenburg 		 * since we don't know what's dirty.  This will WARN in
34052e2d0a3SMartin Brandenburg 		 * orangefs_writepage_locked.
34152e2d0a3SMartin Brandenburg 		 */
342eabf038fSMatthew Wilcox (Oracle) 		ret = orangefs_launder_folio(folio);
34352e2d0a3SMartin Brandenburg 		if (ret)
34452e2d0a3SMartin Brandenburg 			return ret;
34552e2d0a3SMartin Brandenburg 	}
346eabf038fSMatthew Wilcox (Oracle) 	if (folio_test_private(folio)) {
34752e2d0a3SMartin Brandenburg 		struct orangefs_write_range *wr;
348eabf038fSMatthew Wilcox (Oracle) 		wr = folio_get_private(folio);
34952e2d0a3SMartin Brandenburg 		if (wr->pos + wr->len == pos &&
35052e2d0a3SMartin Brandenburg 		    uid_eq(wr->uid, current_fsuid()) &&
35152e2d0a3SMartin Brandenburg 		    gid_eq(wr->gid, current_fsgid())) {
35252e2d0a3SMartin Brandenburg 			wr->len += len;
35352e2d0a3SMartin Brandenburg 			goto okay;
35452e2d0a3SMartin Brandenburg 		} else {
355eabf038fSMatthew Wilcox (Oracle) 			ret = orangefs_launder_folio(folio);
35652e2d0a3SMartin Brandenburg 			if (ret)
35752e2d0a3SMartin Brandenburg 				return ret;
35852e2d0a3SMartin Brandenburg 		}
35952e2d0a3SMartin Brandenburg 	}
36052e2d0a3SMartin Brandenburg 
36152e2d0a3SMartin Brandenburg 	wr = kmalloc(sizeof *wr, GFP_KERNEL);
36252e2d0a3SMartin Brandenburg 	if (!wr)
36352e2d0a3SMartin Brandenburg 		return -ENOMEM;
36452e2d0a3SMartin Brandenburg 
36552e2d0a3SMartin Brandenburg 	wr->pos = pos;
36652e2d0a3SMartin Brandenburg 	wr->len = len;
36752e2d0a3SMartin Brandenburg 	wr->uid = current_fsuid();
36852e2d0a3SMartin Brandenburg 	wr->gid = current_fsgid();
369eabf038fSMatthew Wilcox (Oracle) 	folio_attach_private(folio, wr);
37052e2d0a3SMartin Brandenburg okay:
37152e2d0a3SMartin Brandenburg 	return 0;
37252e2d0a3SMartin Brandenburg }
37352e2d0a3SMartin Brandenburg 
orangefs_write_end(struct file * file,struct address_space * mapping,loff_t pos,unsigned len,unsigned copied,struct page * page,void * fsdata)37485ac799cSMartin Brandenburg static int orangefs_write_end(struct file *file, struct address_space *mapping,
37585ac799cSMartin Brandenburg     loff_t pos, unsigned len, unsigned copied, struct page *page, void *fsdata)
37685ac799cSMartin Brandenburg {
3778f04e1beSMartin Brandenburg 	struct inode *inode = page->mapping->host;
3788f04e1beSMartin Brandenburg 	loff_t last_pos = pos + copied;
3798f04e1beSMartin Brandenburg 
3808f04e1beSMartin Brandenburg 	/*
3818f04e1beSMartin Brandenburg 	 * No need to use i_size_read() here, the i_size
3828f04e1beSMartin Brandenburg 	 * cannot change under us because we hold the i_mutex.
3838f04e1beSMartin Brandenburg 	 */
3848f04e1beSMartin Brandenburg 	if (last_pos > inode->i_size)
3858f04e1beSMartin Brandenburg 		i_size_write(inode, last_pos);
3868f04e1beSMartin Brandenburg 
3878f04e1beSMartin Brandenburg 	/* zero the stale part of the page if we did a short copy */
3888f04e1beSMartin Brandenburg 	if (!PageUptodate(page)) {
3898f04e1beSMartin Brandenburg 		unsigned from = pos & (PAGE_SIZE - 1);
3908f04e1beSMartin Brandenburg 		if (copied < len) {
3918f04e1beSMartin Brandenburg 			zero_user(page, from + copied, len - copied);
3928f04e1beSMartin Brandenburg 		}
3938f04e1beSMartin Brandenburg 		/* Set fully written pages uptodate. */
3948f04e1beSMartin Brandenburg 		if (pos == page_offset(page) &&
3958f04e1beSMartin Brandenburg 		    (len == PAGE_SIZE || pos + len == inode->i_size)) {
3968f04e1beSMartin Brandenburg 			zero_user_segment(page, from + copied, PAGE_SIZE);
3978f04e1beSMartin Brandenburg 			SetPageUptodate(page);
3988f04e1beSMartin Brandenburg 		}
3998f04e1beSMartin Brandenburg 	}
4008f04e1beSMartin Brandenburg 
4018f04e1beSMartin Brandenburg 	set_page_dirty(page);
4028f04e1beSMartin Brandenburg 	unlock_page(page);
4038f04e1beSMartin Brandenburg 	put_page(page);
4048f04e1beSMartin Brandenburg 
40585ac799cSMartin Brandenburg 	mark_inode_dirty_sync(file_inode(file));
4068f04e1beSMartin Brandenburg 	return copied;
40785ac799cSMartin Brandenburg }
40885ac799cSMartin Brandenburg 
orangefs_invalidate_folio(struct folio * folio,size_t offset,size_t length)4092a40be81SMatthew Wilcox (Oracle) static void orangefs_invalidate_folio(struct folio *folio,
4102a40be81SMatthew Wilcox (Oracle) 				 size_t offset, size_t length)
4115db11c21SMike Marshall {
4122a40be81SMatthew Wilcox (Oracle) 	struct orangefs_write_range *wr = folio_get_private(folio);
4135db11c21SMike Marshall 
41452e2d0a3SMartin Brandenburg 	if (offset == 0 && length == PAGE_SIZE) {
4152a40be81SMatthew Wilcox (Oracle) 		kfree(folio_detach_private(folio));
4168f04e1beSMartin Brandenburg 		return;
41752e2d0a3SMartin Brandenburg 	/* write range entirely within invalidate range (or equal) */
4182a40be81SMatthew Wilcox (Oracle) 	} else if (folio_pos(folio) + offset <= wr->pos &&
4192a40be81SMatthew Wilcox (Oracle) 	    wr->pos + wr->len <= folio_pos(folio) + offset + length) {
4202a40be81SMatthew Wilcox (Oracle) 		kfree(folio_detach_private(folio));
42152e2d0a3SMartin Brandenburg 		/* XXX is this right? only caller in fs */
4222a40be81SMatthew Wilcox (Oracle) 		folio_cancel_dirty(folio);
4238f04e1beSMartin Brandenburg 		return;
42452e2d0a3SMartin Brandenburg 	/* invalidate range chops off end of write range */
4252a40be81SMatthew Wilcox (Oracle) 	} else if (wr->pos < folio_pos(folio) + offset &&
4262a40be81SMatthew Wilcox (Oracle) 	    wr->pos + wr->len <= folio_pos(folio) + offset + length &&
4272a40be81SMatthew Wilcox (Oracle) 	     folio_pos(folio) + offset < wr->pos + wr->len) {
42852e2d0a3SMartin Brandenburg 		size_t x;
4292a40be81SMatthew Wilcox (Oracle) 		x = wr->pos + wr->len - (folio_pos(folio) + offset);
43052e2d0a3SMartin Brandenburg 		WARN_ON(x > wr->len);
43152e2d0a3SMartin Brandenburg 		wr->len -= x;
43252e2d0a3SMartin Brandenburg 		wr->uid = current_fsuid();
43352e2d0a3SMartin Brandenburg 		wr->gid = current_fsgid();
43452e2d0a3SMartin Brandenburg 	/* invalidate range chops off beginning of write range */
4352a40be81SMatthew Wilcox (Oracle) 	} else if (folio_pos(folio) + offset <= wr->pos &&
4362a40be81SMatthew Wilcox (Oracle) 	    folio_pos(folio) + offset + length < wr->pos + wr->len &&
4372a40be81SMatthew Wilcox (Oracle) 	    wr->pos < folio_pos(folio) + offset + length) {
43852e2d0a3SMartin Brandenburg 		size_t x;
4392a40be81SMatthew Wilcox (Oracle) 		x = folio_pos(folio) + offset + length - wr->pos;
44052e2d0a3SMartin Brandenburg 		WARN_ON(x > wr->len);
44152e2d0a3SMartin Brandenburg 		wr->pos += x;
44252e2d0a3SMartin Brandenburg 		wr->len -= x;
44352e2d0a3SMartin Brandenburg 		wr->uid = current_fsuid();
44452e2d0a3SMartin Brandenburg 		wr->gid = current_fsgid();
44552e2d0a3SMartin Brandenburg 	/* invalidate range entirely within write range (punch hole) */
4462a40be81SMatthew Wilcox (Oracle) 	} else if (wr->pos < folio_pos(folio) + offset &&
4472a40be81SMatthew Wilcox (Oracle) 	    folio_pos(folio) + offset + length < wr->pos + wr->len) {
44852e2d0a3SMartin Brandenburg 		/* XXX what do we do here... should not WARN_ON */
44952e2d0a3SMartin Brandenburg 		WARN_ON(1);
45052e2d0a3SMartin Brandenburg 		/* punch hole */
45152e2d0a3SMartin Brandenburg 		/*
45252e2d0a3SMartin Brandenburg 		 * should we just ignore this and write it out anyway?
45352e2d0a3SMartin Brandenburg 		 * it hardly makes sense
45452e2d0a3SMartin Brandenburg 		 */
4558f04e1beSMartin Brandenburg 		return;
45652e2d0a3SMartin Brandenburg 	/* non-overlapping ranges */
45752e2d0a3SMartin Brandenburg 	} else {
45852e2d0a3SMartin Brandenburg 		/* WARN if they do overlap */
4592a40be81SMatthew Wilcox (Oracle) 		if (!((folio_pos(folio) + offset + length <= wr->pos) ^
4602a40be81SMatthew Wilcox (Oracle) 		    (wr->pos + wr->len <= folio_pos(folio) + offset))) {
46152e2d0a3SMartin Brandenburg 			WARN_ON(1);
4622a40be81SMatthew Wilcox (Oracle) 			printk("invalidate range offset %llu length %zu\n",
4632a40be81SMatthew Wilcox (Oracle) 			    folio_pos(folio) + offset, length);
46452e2d0a3SMartin Brandenburg 			printk("write range offset %llu length %zu\n",
46552e2d0a3SMartin Brandenburg 			    wr->pos, wr->len);
46652e2d0a3SMartin Brandenburg 		}
4678f04e1beSMartin Brandenburg 		return;
46852e2d0a3SMartin Brandenburg 	}
4698f04e1beSMartin Brandenburg 
4708f04e1beSMartin Brandenburg 	/*
4718f04e1beSMartin Brandenburg 	 * Above there are returns where wr is freed or where we WARN.
4728f04e1beSMartin Brandenburg 	 * Thus the following runs if wr was modified above.
4738f04e1beSMartin Brandenburg 	 */
4748f04e1beSMartin Brandenburg 
475eabf038fSMatthew Wilcox (Oracle) 	orangefs_launder_folio(folio);
4765db11c21SMike Marshall }
4775db11c21SMike Marshall 
orangefs_release_folio(struct folio * folio,gfp_t foo)4784993474aSMatthew Wilcox (Oracle) static bool orangefs_release_folio(struct folio *folio, gfp_t foo)
4795db11c21SMike Marshall {
4804993474aSMatthew Wilcox (Oracle) 	return !folio_test_private(folio);
48152e2d0a3SMartin Brandenburg }
48252e2d0a3SMartin Brandenburg 
orangefs_free_folio(struct folio * folio)483c78ac80eSMatthew Wilcox (Oracle) static void orangefs_free_folio(struct folio *folio)
48452e2d0a3SMartin Brandenburg {
485c78ac80eSMatthew Wilcox (Oracle) 	kfree(folio_detach_private(folio));
48652e2d0a3SMartin Brandenburg }
48752e2d0a3SMartin Brandenburg 
orangefs_launder_folio(struct folio * folio)488eabf038fSMatthew Wilcox (Oracle) static int orangefs_launder_folio(struct folio *folio)
48952e2d0a3SMartin Brandenburg {
49052e2d0a3SMartin Brandenburg 	int r = 0;
49152e2d0a3SMartin Brandenburg 	struct writeback_control wbc = {
49252e2d0a3SMartin Brandenburg 		.sync_mode = WB_SYNC_ALL,
49352e2d0a3SMartin Brandenburg 		.nr_to_write = 0,
49452e2d0a3SMartin Brandenburg 	};
495eabf038fSMatthew Wilcox (Oracle) 	folio_wait_writeback(folio);
496eabf038fSMatthew Wilcox (Oracle) 	if (folio_clear_dirty_for_io(folio)) {
497eabf038fSMatthew Wilcox (Oracle) 		r = orangefs_writepage_locked(&folio->page, &wbc);
498eabf038fSMatthew Wilcox (Oracle) 		folio_end_writeback(folio);
49952e2d0a3SMartin Brandenburg 	}
50052e2d0a3SMartin Brandenburg 	return r;
5015db11c21SMike Marshall }
5025db11c21SMike Marshall 
orangefs_direct_IO(struct kiocb * iocb,struct iov_iter * iter)5033903f150SMike Marshall static ssize_t orangefs_direct_IO(struct kiocb *iocb,
5043903f150SMike Marshall 				  struct iov_iter *iter)
5053903f150SMike Marshall {
5063e9dfc6eSMartin Brandenburg 	/*
5073e9dfc6eSMartin Brandenburg 	 * Comment from original do_readv_writev:
5083e9dfc6eSMartin Brandenburg 	 * Common entry point for read/write/readv/writev
5093e9dfc6eSMartin Brandenburg 	 * This function will dispatch it to either the direct I/O
5103e9dfc6eSMartin Brandenburg 	 * or buffered I/O path depending on the mount options and/or
5113e9dfc6eSMartin Brandenburg 	 * augmented/extended metadata attached to the file.
5123e9dfc6eSMartin Brandenburg 	 * Note: File extended attributes override any mount options.
5133e9dfc6eSMartin Brandenburg 	 */
514c453dcfcSMartin Brandenburg 	struct file *file = iocb->ki_filp;
5153e9dfc6eSMartin Brandenburg 	loff_t pos = iocb->ki_pos;
5163e9dfc6eSMartin Brandenburg 	enum ORANGEFS_io_type type = iov_iter_rw(iter) == WRITE ?
5173e9dfc6eSMartin Brandenburg             ORANGEFS_IO_WRITE : ORANGEFS_IO_READ;
5183e9dfc6eSMartin Brandenburg 	loff_t *offset = &pos;
5193e9dfc6eSMartin Brandenburg 	struct inode *inode = file->f_mapping->host;
5203e9dfc6eSMartin Brandenburg 	struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
5213e9dfc6eSMartin Brandenburg 	struct orangefs_khandle *handle = &orangefs_inode->refn.khandle;
5223e9dfc6eSMartin Brandenburg 	size_t count = iov_iter_count(iter);
5233e9dfc6eSMartin Brandenburg 	ssize_t total_count = 0;
5243e9dfc6eSMartin Brandenburg 	ssize_t ret = -EINVAL;
5253e9dfc6eSMartin Brandenburg 
5263e9dfc6eSMartin Brandenburg 	gossip_debug(GOSSIP_FILE_DEBUG,
5273e9dfc6eSMartin Brandenburg 		"%s-BEGIN(%pU): count(%d) after estimate_max_iovecs.\n",
5283e9dfc6eSMartin Brandenburg 		__func__,
5293e9dfc6eSMartin Brandenburg 		handle,
5303e9dfc6eSMartin Brandenburg 		(int)count);
5313e9dfc6eSMartin Brandenburg 
5323e9dfc6eSMartin Brandenburg 	if (type == ORANGEFS_IO_WRITE) {
5333e9dfc6eSMartin Brandenburg 		gossip_debug(GOSSIP_FILE_DEBUG,
5343e9dfc6eSMartin Brandenburg 			     "%s(%pU): proceeding with offset : %llu, "
5353e9dfc6eSMartin Brandenburg 			     "size %d\n",
5363e9dfc6eSMartin Brandenburg 			     __func__,
5373e9dfc6eSMartin Brandenburg 			     handle,
5383e9dfc6eSMartin Brandenburg 			     llu(*offset),
5393e9dfc6eSMartin Brandenburg 			     (int)count);
5403e9dfc6eSMartin Brandenburg 	}
5413e9dfc6eSMartin Brandenburg 
5423e9dfc6eSMartin Brandenburg 	if (count == 0) {
5433e9dfc6eSMartin Brandenburg 		ret = 0;
5443e9dfc6eSMartin Brandenburg 		goto out;
5453e9dfc6eSMartin Brandenburg 	}
5463e9dfc6eSMartin Brandenburg 
5473e9dfc6eSMartin Brandenburg 	while (iov_iter_count(iter)) {
5483e9dfc6eSMartin Brandenburg 		size_t each_count = iov_iter_count(iter);
5493e9dfc6eSMartin Brandenburg 		size_t amt_complete;
5503e9dfc6eSMartin Brandenburg 
5513e9dfc6eSMartin Brandenburg 		/* how much to transfer in this loop iteration */
5523e9dfc6eSMartin Brandenburg 		if (each_count > orangefs_bufmap_size_query())
5533e9dfc6eSMartin Brandenburg 			each_count = orangefs_bufmap_size_query();
5543e9dfc6eSMartin Brandenburg 
5553e9dfc6eSMartin Brandenburg 		gossip_debug(GOSSIP_FILE_DEBUG,
5563e9dfc6eSMartin Brandenburg 			     "%s(%pU): size of each_count(%d)\n",
5573e9dfc6eSMartin Brandenburg 			     __func__,
5583e9dfc6eSMartin Brandenburg 			     handle,
5593e9dfc6eSMartin Brandenburg 			     (int)each_count);
5603e9dfc6eSMartin Brandenburg 		gossip_debug(GOSSIP_FILE_DEBUG,
5613e9dfc6eSMartin Brandenburg 			     "%s(%pU): BEFORE wait_for_io: offset is %d\n",
5623e9dfc6eSMartin Brandenburg 			     __func__,
5633e9dfc6eSMartin Brandenburg 			     handle,
5643e9dfc6eSMartin Brandenburg 			     (int)*offset);
5653e9dfc6eSMartin Brandenburg 
5663e9dfc6eSMartin Brandenburg 		ret = wait_for_direct_io(type, inode, offset, iter,
567f9bbb682SMike Marshall 				each_count, 0, NULL, NULL, file);
5683e9dfc6eSMartin Brandenburg 		gossip_debug(GOSSIP_FILE_DEBUG,
5693e9dfc6eSMartin Brandenburg 			     "%s(%pU): return from wait_for_io:%d\n",
5703e9dfc6eSMartin Brandenburg 			     __func__,
5713e9dfc6eSMartin Brandenburg 			     handle,
5723e9dfc6eSMartin Brandenburg 			     (int)ret);
5733e9dfc6eSMartin Brandenburg 
5743e9dfc6eSMartin Brandenburg 		if (ret < 0)
5753e9dfc6eSMartin Brandenburg 			goto out;
5763e9dfc6eSMartin Brandenburg 
5773e9dfc6eSMartin Brandenburg 		*offset += ret;
5783e9dfc6eSMartin Brandenburg 		total_count += ret;
5793e9dfc6eSMartin Brandenburg 		amt_complete = ret;
5803e9dfc6eSMartin Brandenburg 
5813e9dfc6eSMartin Brandenburg 		gossip_debug(GOSSIP_FILE_DEBUG,
5823e9dfc6eSMartin Brandenburg 			     "%s(%pU): AFTER wait_for_io: offset is %d\n",
5833e9dfc6eSMartin Brandenburg 			     __func__,
5843e9dfc6eSMartin Brandenburg 			     handle,
5853e9dfc6eSMartin Brandenburg 			     (int)*offset);
5863e9dfc6eSMartin Brandenburg 
5873e9dfc6eSMartin Brandenburg 		/*
5883e9dfc6eSMartin Brandenburg 		 * if we got a short I/O operations,
5893e9dfc6eSMartin Brandenburg 		 * fall out and return what we got so far
5903e9dfc6eSMartin Brandenburg 		 */
5913e9dfc6eSMartin Brandenburg 		if (amt_complete < each_count)
5923e9dfc6eSMartin Brandenburg 			break;
5933e9dfc6eSMartin Brandenburg 	} /*end while */
5943e9dfc6eSMartin Brandenburg 
5953e9dfc6eSMartin Brandenburg out:
5963e9dfc6eSMartin Brandenburg 	if (total_count > 0)
5973e9dfc6eSMartin Brandenburg 		ret = total_count;
5983e9dfc6eSMartin Brandenburg 	if (ret > 0) {
5993e9dfc6eSMartin Brandenburg 		if (type == ORANGEFS_IO_READ) {
6003e9dfc6eSMartin Brandenburg 			file_accessed(file);
6013e9dfc6eSMartin Brandenburg 		} else {
6023e9dfc6eSMartin Brandenburg 			file_update_time(file);
6033e9dfc6eSMartin Brandenburg 			if (*offset > i_size_read(inode))
6043e9dfc6eSMartin Brandenburg 				i_size_write(inode, *offset);
6053e9dfc6eSMartin Brandenburg 		}
6063e9dfc6eSMartin Brandenburg 	}
6073e9dfc6eSMartin Brandenburg 
6083e9dfc6eSMartin Brandenburg 	gossip_debug(GOSSIP_FILE_DEBUG,
6093e9dfc6eSMartin Brandenburg 		     "%s(%pU): Value(%d) returned.\n",
6103e9dfc6eSMartin Brandenburg 		     __func__,
6113e9dfc6eSMartin Brandenburg 		     handle,
6123e9dfc6eSMartin Brandenburg 		     (int)ret);
6133e9dfc6eSMartin Brandenburg 
6143e9dfc6eSMartin Brandenburg 	return ret;
6153903f150SMike Marshall }
6165db11c21SMike Marshall 
6178bb8aefdSYi Liu /** ORANGEFS2 implementation of address space operations */
618bdd6f083SMartin Brandenburg static const struct address_space_operations orangefs_address_operations = {
61985ac799cSMartin Brandenburg 	.writepage = orangefs_writepage,
6200c4b7cadSMike Marshall 	.readahead = orangefs_readahead,
6211a641788SMatthew Wilcox (Oracle) 	.read_folio = orangefs_read_folio,
6228f04e1beSMartin Brandenburg 	.writepages = orangefs_writepages,
623187c82cbSMatthew Wilcox (Oracle) 	.dirty_folio = filemap_dirty_folio,
62452e2d0a3SMartin Brandenburg 	.write_begin = orangefs_write_begin,
62585ac799cSMartin Brandenburg 	.write_end = orangefs_write_end,
6262a40be81SMatthew Wilcox (Oracle) 	.invalidate_folio = orangefs_invalidate_folio,
6274993474aSMatthew Wilcox (Oracle) 	.release_folio = orangefs_release_folio,
628c78ac80eSMatthew Wilcox (Oracle) 	.free_folio = orangefs_free_folio,
629eabf038fSMatthew Wilcox (Oracle) 	.launder_folio = orangefs_launder_folio,
6303903f150SMike Marshall 	.direct_IO = orangefs_direct_IO,
6315db11c21SMike Marshall };
6325db11c21SMike Marshall 
orangefs_page_mkwrite(struct vm_fault * vmf)63352e2d0a3SMartin Brandenburg vm_fault_t orangefs_page_mkwrite(struct vm_fault *vmf)
63452e2d0a3SMartin Brandenburg {
635eabf038fSMatthew Wilcox (Oracle) 	struct folio *folio = page_folio(vmf->page);
63652e2d0a3SMartin Brandenburg 	struct inode *inode = file_inode(vmf->vma->vm_file);
6378f04e1beSMartin Brandenburg 	struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
6388f04e1beSMartin Brandenburg 	unsigned long *bitlock = &orangefs_inode->bitlock;
6398f04e1beSMartin Brandenburg 	vm_fault_t ret;
64052e2d0a3SMartin Brandenburg 	struct orangefs_write_range *wr;
64152e2d0a3SMartin Brandenburg 
6428f04e1beSMartin Brandenburg 	sb_start_pagefault(inode->i_sb);
6438f04e1beSMartin Brandenburg 
6448f04e1beSMartin Brandenburg 	if (wait_on_bit(bitlock, 1, TASK_KILLABLE)) {
6458f04e1beSMartin Brandenburg 		ret = VM_FAULT_RETRY;
6468f04e1beSMartin Brandenburg 		goto out;
6478f04e1beSMartin Brandenburg 	}
6488f04e1beSMartin Brandenburg 
649eabf038fSMatthew Wilcox (Oracle) 	folio_lock(folio);
650eabf038fSMatthew Wilcox (Oracle) 	if (folio_test_dirty(folio) && !folio_test_private(folio)) {
65152e2d0a3SMartin Brandenburg 		/*
652eabf038fSMatthew Wilcox (Oracle) 		 * Should be impossible.  If it happens, launder the folio
65352e2d0a3SMartin Brandenburg 		 * since we don't know what's dirty.  This will WARN in
65452e2d0a3SMartin Brandenburg 		 * orangefs_writepage_locked.
65552e2d0a3SMartin Brandenburg 		 */
656eabf038fSMatthew Wilcox (Oracle) 		if (orangefs_launder_folio(folio)) {
6578f04e1beSMartin Brandenburg 			ret = VM_FAULT_LOCKED|VM_FAULT_RETRY;
65852e2d0a3SMartin Brandenburg 			goto out;
65952e2d0a3SMartin Brandenburg 		}
66052e2d0a3SMartin Brandenburg 	}
661eabf038fSMatthew Wilcox (Oracle) 	if (folio_test_private(folio)) {
662eabf038fSMatthew Wilcox (Oracle) 		wr = folio_get_private(folio);
66352e2d0a3SMartin Brandenburg 		if (uid_eq(wr->uid, current_fsuid()) &&
66452e2d0a3SMartin Brandenburg 		    gid_eq(wr->gid, current_fsgid())) {
665eabf038fSMatthew Wilcox (Oracle) 			wr->pos = page_offset(vmf->page);
66652e2d0a3SMartin Brandenburg 			wr->len = PAGE_SIZE;
66752e2d0a3SMartin Brandenburg 			goto okay;
66852e2d0a3SMartin Brandenburg 		} else {
669eabf038fSMatthew Wilcox (Oracle) 			if (orangefs_launder_folio(folio)) {
6708f04e1beSMartin Brandenburg 				ret = VM_FAULT_LOCKED|VM_FAULT_RETRY;
67152e2d0a3SMartin Brandenburg 				goto out;
67252e2d0a3SMartin Brandenburg 			}
67352e2d0a3SMartin Brandenburg 		}
67452e2d0a3SMartin Brandenburg 	}
67552e2d0a3SMartin Brandenburg 	wr = kmalloc(sizeof *wr, GFP_KERNEL);
67652e2d0a3SMartin Brandenburg 	if (!wr) {
6778f04e1beSMartin Brandenburg 		ret = VM_FAULT_LOCKED|VM_FAULT_RETRY;
67852e2d0a3SMartin Brandenburg 		goto out;
67952e2d0a3SMartin Brandenburg 	}
680eabf038fSMatthew Wilcox (Oracle) 	wr->pos = page_offset(vmf->page);
68152e2d0a3SMartin Brandenburg 	wr->len = PAGE_SIZE;
68252e2d0a3SMartin Brandenburg 	wr->uid = current_fsuid();
68352e2d0a3SMartin Brandenburg 	wr->gid = current_fsgid();
684eabf038fSMatthew Wilcox (Oracle) 	folio_attach_private(folio, wr);
68552e2d0a3SMartin Brandenburg okay:
68652e2d0a3SMartin Brandenburg 
68752e2d0a3SMartin Brandenburg 	file_update_time(vmf->vma->vm_file);
688eabf038fSMatthew Wilcox (Oracle) 	if (folio->mapping != inode->i_mapping) {
689eabf038fSMatthew Wilcox (Oracle) 		folio_unlock(folio);
6908f04e1beSMartin Brandenburg 		ret = VM_FAULT_LOCKED|VM_FAULT_NOPAGE;
69152e2d0a3SMartin Brandenburg 		goto out;
69252e2d0a3SMartin Brandenburg 	}
69352e2d0a3SMartin Brandenburg 
69452e2d0a3SMartin Brandenburg 	/*
695eabf038fSMatthew Wilcox (Oracle) 	 * We mark the folio dirty already here so that when freeze is in
69652e2d0a3SMartin Brandenburg 	 * progress, we are guaranteed that writeback during freezing will
697eabf038fSMatthew Wilcox (Oracle) 	 * see the dirty folio and writeprotect it again.
69852e2d0a3SMartin Brandenburg 	 */
699eabf038fSMatthew Wilcox (Oracle) 	folio_mark_dirty(folio);
700eabf038fSMatthew Wilcox (Oracle) 	folio_wait_stable(folio);
7018f04e1beSMartin Brandenburg 	ret = VM_FAULT_LOCKED;
70252e2d0a3SMartin Brandenburg out:
70352e2d0a3SMartin Brandenburg 	sb_end_pagefault(inode->i_sb);
70452e2d0a3SMartin Brandenburg 	return ret;
70552e2d0a3SMartin Brandenburg }
70652e2d0a3SMartin Brandenburg 
orangefs_setattr_size(struct inode * inode,struct iattr * iattr)7078bb8aefdSYi Liu static int orangefs_setattr_size(struct inode *inode, struct iattr *iattr)
7085db11c21SMike Marshall {
7098bb8aefdSYi Liu 	struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
7108bb8aefdSYi Liu 	struct orangefs_kernel_op_s *new_op;
711fecd86aaSMartin Brandenburg 	loff_t orig_size;
7125db11c21SMike Marshall 	int ret = -EINVAL;
7135db11c21SMike Marshall 
7145db11c21SMike Marshall 	gossip_debug(GOSSIP_INODE_DEBUG,
7155db11c21SMike Marshall 		     "%s: %pU: Handle is %pU | fs_id %d | size is %llu\n",
7165db11c21SMike Marshall 		     __func__,
7175db11c21SMike Marshall 		     get_khandle_from_ino(inode),
7188bb8aefdSYi Liu 		     &orangefs_inode->refn.khandle,
7198bb8aefdSYi Liu 		     orangefs_inode->refn.fs_id,
7205db11c21SMike Marshall 		     iattr->ia_size);
7215db11c21SMike Marshall 
722fecd86aaSMartin Brandenburg 	/* Ensure that we have a up to date size, so we know if it changed. */
7238b60785cSMartin Brandenburg 	ret = orangefs_inode_getattr(inode, ORANGEFS_GETATTR_SIZE);
724fecd86aaSMartin Brandenburg 	if (ret == -ESTALE)
725fecd86aaSMartin Brandenburg 		ret = -EIO;
726fecd86aaSMartin Brandenburg 	if (ret) {
727fecd86aaSMartin Brandenburg 		gossip_err("%s: orangefs_inode_getattr failed, ret:%d:.\n",
728fecd86aaSMartin Brandenburg 		    __func__, ret);
729fecd86aaSMartin Brandenburg 		return ret;
730fecd86aaSMartin Brandenburg 	}
731fecd86aaSMartin Brandenburg 	orig_size = i_size_read(inode);
732fecd86aaSMartin Brandenburg 
73333713cd0SMartin Brandenburg 	/* This is truncate_setsize in a different order. */
73433713cd0SMartin Brandenburg 	truncate_pagecache(inode, iattr->ia_size);
73533713cd0SMartin Brandenburg 	i_size_write(inode, iattr->ia_size);
73633713cd0SMartin Brandenburg 	if (iattr->ia_size > orig_size)
73733713cd0SMartin Brandenburg 		pagecache_isize_extended(inode, orig_size, iattr->ia_size);
7385db11c21SMike Marshall 
7398bb8aefdSYi Liu 	new_op = op_alloc(ORANGEFS_VFS_OP_TRUNCATE);
7405db11c21SMike Marshall 	if (!new_op)
7415db11c21SMike Marshall 		return -ENOMEM;
7425db11c21SMike Marshall 
7438bb8aefdSYi Liu 	new_op->upcall.req.truncate.refn = orangefs_inode->refn;
7445db11c21SMike Marshall 	new_op->upcall.req.truncate.size = (__s64) iattr->ia_size;
7455db11c21SMike Marshall 
74695f5f88fSMike Marshall 	ret = service_operation(new_op,
74795f5f88fSMike Marshall 		__func__,
7485db11c21SMike Marshall 		get_interruptible_flag(inode));
7495db11c21SMike Marshall 
7505db11c21SMike Marshall 	/*
7515db11c21SMike Marshall 	 * the truncate has no downcall members to retrieve, but
7525db11c21SMike Marshall 	 * the status value tells us if it went through ok or not
7535db11c21SMike Marshall 	 */
75495f5f88fSMike Marshall 	gossip_debug(GOSSIP_INODE_DEBUG, "%s: ret:%d:\n", __func__, ret);
7555db11c21SMike Marshall 
7565db11c21SMike Marshall 	op_release(new_op);
7575db11c21SMike Marshall 
7585db11c21SMike Marshall 	if (ret != 0)
7595db11c21SMike Marshall 		return ret;
7605db11c21SMike Marshall 
761f83140c1SMartin Brandenburg 	if (orig_size != i_size_read(inode))
7625db11c21SMike Marshall 		iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
7635db11c21SMike Marshall 
7645db11c21SMike Marshall 	return ret;
7655db11c21SMike Marshall }
7665db11c21SMike Marshall 
__orangefs_setattr(struct inode * inode,struct iattr * iattr)767afd9fb2aSMartin Brandenburg int __orangefs_setattr(struct inode *inode, struct iattr *iattr)
7685db11c21SMike Marshall {
769df2d7337SMartin Brandenburg 	int ret;
7705db11c21SMike Marshall 
771afd9fb2aSMartin Brandenburg 	if (iattr->ia_valid & ATTR_MODE) {
772afd9fb2aSMartin Brandenburg 		if (iattr->ia_mode & (S_ISVTX)) {
773afd9fb2aSMartin Brandenburg 			if (is_root_handle(inode)) {
774afd9fb2aSMartin Brandenburg 				/*
775afd9fb2aSMartin Brandenburg 				 * allow sticky bit to be set on root (since
776afd9fb2aSMartin Brandenburg 				 * it shows up that way by default anyhow),
777afd9fb2aSMartin Brandenburg 				 * but don't show it to the server
778afd9fb2aSMartin Brandenburg 				 */
779afd9fb2aSMartin Brandenburg 				iattr->ia_mode -= S_ISVTX;
780afd9fb2aSMartin Brandenburg 			} else {
781afd9fb2aSMartin Brandenburg 				gossip_debug(GOSSIP_UTILS_DEBUG,
782afd9fb2aSMartin Brandenburg 					     "User attempted to set sticky bit on non-root directory; returning EINVAL.\n");
7838f04e1beSMartin Brandenburg 				ret = -EINVAL;
7848f04e1beSMartin Brandenburg 				goto out;
785afd9fb2aSMartin Brandenburg 			}
786afd9fb2aSMartin Brandenburg 		}
787afd9fb2aSMartin Brandenburg 		if (iattr->ia_mode & (S_ISUID)) {
788afd9fb2aSMartin Brandenburg 			gossip_debug(GOSSIP_UTILS_DEBUG,
789afd9fb2aSMartin Brandenburg 				     "Attempting to set setuid bit (not supported); returning EINVAL.\n");
7908f04e1beSMartin Brandenburg 			ret = -EINVAL;
7918f04e1beSMartin Brandenburg 			goto out;
792afd9fb2aSMartin Brandenburg 		}
793afd9fb2aSMartin Brandenburg 	}
7945db11c21SMike Marshall 
79553950ef5SMartin Brandenburg 	if (iattr->ia_valid & ATTR_SIZE) {
7968bb8aefdSYi Liu 		ret = orangefs_setattr_size(inode, iattr);
7975db11c21SMike Marshall 		if (ret)
7985db11c21SMike Marshall 			goto out;
7995db11c21SMike Marshall 	}
8005db11c21SMike Marshall 
801afd9fb2aSMartin Brandenburg again:
802afd9fb2aSMartin Brandenburg 	spin_lock(&inode->i_lock);
803afd9fb2aSMartin Brandenburg 	if (ORANGEFS_I(inode)->attr_valid) {
804afd9fb2aSMartin Brandenburg 		if (uid_eq(ORANGEFS_I(inode)->attr_uid, current_fsuid()) &&
805afd9fb2aSMartin Brandenburg 		    gid_eq(ORANGEFS_I(inode)->attr_gid, current_fsgid())) {
806afd9fb2aSMartin Brandenburg 			ORANGEFS_I(inode)->attr_valid = iattr->ia_valid;
807afd9fb2aSMartin Brandenburg 		} else {
808afd9fb2aSMartin Brandenburg 			spin_unlock(&inode->i_lock);
809afd9fb2aSMartin Brandenburg 			write_inode_now(inode, 1);
810afd9fb2aSMartin Brandenburg 			goto again;
811afd9fb2aSMartin Brandenburg 		}
812afd9fb2aSMartin Brandenburg 	} else {
813afd9fb2aSMartin Brandenburg 		ORANGEFS_I(inode)->attr_valid = iattr->ia_valid;
814afd9fb2aSMartin Brandenburg 		ORANGEFS_I(inode)->attr_uid = current_fsuid();
815afd9fb2aSMartin Brandenburg 		ORANGEFS_I(inode)->attr_gid = current_fsgid();
816afd9fb2aSMartin Brandenburg 	}
817c1632a0fSChristian Brauner 	setattr_copy(&nop_mnt_idmap, inode, iattr);
818afd9fb2aSMartin Brandenburg 	spin_unlock(&inode->i_lock);
8195db11c21SMike Marshall 	mark_inode_dirty(inode);
8205db11c21SMike Marshall 
821df2d7337SMartin Brandenburg 	ret = 0;
8225db11c21SMike Marshall out:
823afd9fb2aSMartin Brandenburg 	return ret;
824afd9fb2aSMartin Brandenburg }
825afd9fb2aSMartin Brandenburg 
__orangefs_setattr_mode(struct dentry * dentry,struct iattr * iattr)826138060baSChristian Brauner int __orangefs_setattr_mode(struct dentry *dentry, struct iattr *iattr)
8274053d250SChristian Brauner {
8284053d250SChristian Brauner 	int ret;
829138060baSChristian Brauner 	struct inode *inode = d_inode(dentry);
8304053d250SChristian Brauner 
8314053d250SChristian Brauner 	ret = __orangefs_setattr(inode, iattr);
8324053d250SChristian Brauner 	/* change mode on a file that has ACLs */
8334053d250SChristian Brauner 	if (!ret && (iattr->ia_valid & ATTR_MODE))
83413e83a49SChristian Brauner 		ret = posix_acl_chmod(&nop_mnt_idmap, dentry, inode->i_mode);
8354053d250SChristian Brauner 	return ret;
8364053d250SChristian Brauner }
8374053d250SChristian Brauner 
838afd9fb2aSMartin Brandenburg /*
839afd9fb2aSMartin Brandenburg  * Change attributes of an object referenced by dentry.
840afd9fb2aSMartin Brandenburg  */
orangefs_setattr(struct mnt_idmap * idmap,struct dentry * dentry,struct iattr * iattr)841c1632a0fSChristian Brauner int orangefs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
842549c7297SChristian Brauner 		     struct iattr *iattr)
843afd9fb2aSMartin Brandenburg {
844afd9fb2aSMartin Brandenburg 	int ret;
845afd9fb2aSMartin Brandenburg 	gossip_debug(GOSSIP_INODE_DEBUG, "__orangefs_setattr: called on %pd\n",
846afd9fb2aSMartin Brandenburg 	    dentry);
847c1632a0fSChristian Brauner 	ret = setattr_prepare(&nop_mnt_idmap, dentry, iattr);
848afd9fb2aSMartin Brandenburg 	if (ret)
849afd9fb2aSMartin Brandenburg 	        goto out;
850138060baSChristian Brauner 	ret = __orangefs_setattr_mode(dentry, iattr);
851afd9fb2aSMartin Brandenburg 	sync_inode_metadata(d_inode(dentry), 1);
852afd9fb2aSMartin Brandenburg out:
853afd9fb2aSMartin Brandenburg 	gossip_debug(GOSSIP_INODE_DEBUG, "orangefs_setattr: returning %d\n",
854afd9fb2aSMartin Brandenburg 	    ret);
8555db11c21SMike Marshall 	return ret;
8565db11c21SMike Marshall }
8575db11c21SMike Marshall 
8585db11c21SMike Marshall /*
8595db11c21SMike Marshall  * Obtain attributes of an object given a dentry
8605db11c21SMike Marshall  */
orangefs_getattr(struct mnt_idmap * idmap,const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)861b74d24f7SChristian Brauner int orangefs_getattr(struct mnt_idmap *idmap, const struct path *path,
862549c7297SChristian Brauner 		     struct kstat *stat, u32 request_mask, unsigned int flags)
8635db11c21SMike Marshall {
864e6b998abSColin Ian King 	int ret;
865a528d35eSDavid Howells 	struct inode *inode = path->dentry->d_inode;
8665db11c21SMike Marshall 
8675db11c21SMike Marshall 	gossip_debug(GOSSIP_INODE_DEBUG,
8685e4f606eSMartin Brandenburg 		     "orangefs_getattr: called on %pd mask %u\n",
8695e4f606eSMartin Brandenburg 		     path->dentry, request_mask);
8705db11c21SMike Marshall 
8718b60785cSMartin Brandenburg 	ret = orangefs_inode_getattr(inode,
8728b60785cSMartin Brandenburg 	    request_mask & STATX_SIZE ? ORANGEFS_GETATTR_SIZE : 0);
8735db11c21SMike Marshall 	if (ret == 0) {
8740d72b928SJeff Layton 		generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
875a7d3e78aSMike Marshall 
8765db11c21SMike Marshall 		/* override block size reported to stat */
8775678b5d6SChristoph Hellwig 		if (!(request_mask & STATX_SIZE))
8785678b5d6SChristoph Hellwig 			stat->result_mask &= ~STATX_SIZE;
8797f54910fSMartin Brandenburg 
8804f911138SAmir Goldstein 		generic_fill_statx_attr(inode, stat);
8815db11c21SMike Marshall 	}
8825db11c21SMike Marshall 	return ret;
8835db11c21SMike Marshall }
8845db11c21SMike Marshall 
orangefs_permission(struct mnt_idmap * idmap,struct inode * inode,int mask)8854609e1f1SChristian Brauner int orangefs_permission(struct mnt_idmap *idmap,
886549c7297SChristian Brauner 			struct inode *inode, int mask)
887933287daSMartin Brandenburg {
888933287daSMartin Brandenburg 	int ret;
889933287daSMartin Brandenburg 
890933287daSMartin Brandenburg 	if (mask & MAY_NOT_BLOCK)
891933287daSMartin Brandenburg 		return -ECHILD;
892933287daSMartin Brandenburg 
893933287daSMartin Brandenburg 	gossip_debug(GOSSIP_INODE_DEBUG, "%s: refreshing\n", __func__);
894933287daSMartin Brandenburg 
895933287daSMartin Brandenburg 	/* Make sure the permission (and other common attrs) are up to date. */
8968b60785cSMartin Brandenburg 	ret = orangefs_inode_getattr(inode, 0);
897933287daSMartin Brandenburg 	if (ret < 0)
898933287daSMartin Brandenburg 		return ret;
899933287daSMartin Brandenburg 
9004609e1f1SChristian Brauner 	return generic_permission(&nop_mnt_idmap, inode, mask);
901933287daSMartin Brandenburg }
902933287daSMartin Brandenburg 
orangefs_update_time(struct inode * inode,int flags)903*913e9928SJeff Layton int orangefs_update_time(struct inode *inode, int flags)
904a55f2d86SMartin Brandenburg {
905a55f2d86SMartin Brandenburg 	struct iattr iattr;
906541d4c79SJeff Layton 
907a55f2d86SMartin Brandenburg 	gossip_debug(GOSSIP_INODE_DEBUG, "orangefs_update_time: %pU\n",
908a55f2d86SMartin Brandenburg 	    get_khandle_from_ino(inode));
909541d4c79SJeff Layton 	flags = generic_update_time(inode, flags);
910a55f2d86SMartin Brandenburg 	memset(&iattr, 0, sizeof iattr);
911a55f2d86SMartin Brandenburg         if (flags & S_ATIME)
912a55f2d86SMartin Brandenburg 		iattr.ia_valid |= ATTR_ATIME;
913a55f2d86SMartin Brandenburg 	if (flags & S_CTIME)
914a55f2d86SMartin Brandenburg 		iattr.ia_valid |= ATTR_CTIME;
915a55f2d86SMartin Brandenburg 	if (flags & S_MTIME)
916a55f2d86SMartin Brandenburg 		iattr.ia_valid |= ATTR_MTIME;
917afd9fb2aSMartin Brandenburg 	return __orangefs_setattr(inode, &iattr);
918a55f2d86SMartin Brandenburg }
919a55f2d86SMartin Brandenburg 
orangefs_fileattr_get(struct dentry * dentry,struct fileattr * fa)9201f26b062SMiklos Szeredi static int orangefs_fileattr_get(struct dentry *dentry, struct fileattr *fa)
9211f26b062SMiklos Szeredi {
9221f26b062SMiklos Szeredi 	u64 val = 0;
9231f26b062SMiklos Szeredi 	int ret;
9241f26b062SMiklos Szeredi 
9251f26b062SMiklos Szeredi 	gossip_debug(GOSSIP_FILE_DEBUG, "%s: called on %pd\n", __func__,
9261f26b062SMiklos Szeredi 		     dentry);
9271f26b062SMiklos Szeredi 
9281f26b062SMiklos Szeredi 	ret = orangefs_inode_getxattr(d_inode(dentry),
9291f26b062SMiklos Szeredi 				      "user.pvfs2.meta_hint",
9301f26b062SMiklos Szeredi 				      &val, sizeof(val));
9311f26b062SMiklos Szeredi 	if (ret < 0 && ret != -ENODATA)
9321f26b062SMiklos Szeredi 		return ret;
9331f26b062SMiklos Szeredi 
9341f26b062SMiklos Szeredi 	gossip_debug(GOSSIP_FILE_DEBUG, "%s: flags=%u\n", __func__, (u32) val);
9351f26b062SMiklos Szeredi 
9361f26b062SMiklos Szeredi 	fileattr_fill_flags(fa, val);
9371f26b062SMiklos Szeredi 	return 0;
9381f26b062SMiklos Szeredi }
9391f26b062SMiklos Szeredi 
orangefs_fileattr_set(struct mnt_idmap * idmap,struct dentry * dentry,struct fileattr * fa)9408782a9aeSChristian Brauner static int orangefs_fileattr_set(struct mnt_idmap *idmap,
9411f26b062SMiklos Szeredi 				 struct dentry *dentry, struct fileattr *fa)
9421f26b062SMiklos Szeredi {
9431f26b062SMiklos Szeredi 	u64 val = 0;
9441f26b062SMiklos Szeredi 
9451f26b062SMiklos Szeredi 	gossip_debug(GOSSIP_FILE_DEBUG, "%s: called on %pd\n", __func__,
9461f26b062SMiklos Szeredi 		     dentry);
9471f26b062SMiklos Szeredi 	/*
9481f26b062SMiklos Szeredi 	 * ORANGEFS_MIRROR_FL is set internally when the mirroring mode is
9491f26b062SMiklos Szeredi 	 * turned on for a file. The user is not allowed to turn on this bit,
9501f26b062SMiklos Szeredi 	 * but the bit is present if the user first gets the flags and then
9511f26b062SMiklos Szeredi 	 * updates the flags with some new settings. So, we ignore it in the
9521f26b062SMiklos Szeredi 	 * following edit. bligon.
9531f26b062SMiklos Szeredi 	 */
9541f26b062SMiklos Szeredi 	if (fileattr_has_fsx(fa) ||
9551f26b062SMiklos Szeredi 	    (fa->flags & ~(FS_IMMUTABLE_FL | FS_APPEND_FL | FS_NOATIME_FL | ORANGEFS_MIRROR_FL))) {
9561f26b062SMiklos Szeredi 		gossip_err("%s: only supports setting one of FS_IMMUTABLE_FL|FS_APPEND_FL|FS_NOATIME_FL\n",
9571f26b062SMiklos Szeredi 			   __func__);
9581f26b062SMiklos Szeredi 		return -EOPNOTSUPP;
9591f26b062SMiklos Szeredi 	}
9601f26b062SMiklos Szeredi 	val = fa->flags;
9611f26b062SMiklos Szeredi 	gossip_debug(GOSSIP_FILE_DEBUG, "%s: flags=%u\n", __func__, (u32) val);
9621f26b062SMiklos Szeredi 	return orangefs_inode_setxattr(d_inode(dentry),
9631f26b062SMiklos Szeredi 				       "user.pvfs2.meta_hint",
9641f26b062SMiklos Szeredi 				       &val, sizeof(val), 0);
9651f26b062SMiklos Szeredi }
9661f26b062SMiklos Szeredi 
96795f5f88fSMike Marshall /* ORANGEFS2 implementation of VFS inode operations for files */
968bdd6f083SMartin Brandenburg static const struct inode_operations orangefs_file_inode_operations = {
969cac2f8b8SChristian Brauner 	.get_inode_acl = orangefs_get_acl,
9708bb8aefdSYi Liu 	.set_acl = orangefs_set_acl,
9718bb8aefdSYi Liu 	.setattr = orangefs_setattr,
9728bb8aefdSYi Liu 	.getattr = orangefs_getattr,
9738bb8aefdSYi Liu 	.listxattr = orangefs_listxattr,
974933287daSMartin Brandenburg 	.permission = orangefs_permission,
975a55f2d86SMartin Brandenburg 	.update_time = orangefs_update_time,
9761f26b062SMiklos Szeredi 	.fileattr_get = orangefs_fileattr_get,
9771f26b062SMiklos Szeredi 	.fileattr_set = orangefs_fileattr_set,
9785db11c21SMike Marshall };
9795db11c21SMike Marshall 
orangefs_init_iops(struct inode * inode)9808bb8aefdSYi Liu static int orangefs_init_iops(struct inode *inode)
9815db11c21SMike Marshall {
9828bb8aefdSYi Liu 	inode->i_mapping->a_ops = &orangefs_address_operations;
9835db11c21SMike Marshall 
9845db11c21SMike Marshall 	switch (inode->i_mode & S_IFMT) {
9855db11c21SMike Marshall 	case S_IFREG:
9868bb8aefdSYi Liu 		inode->i_op = &orangefs_file_inode_operations;
9878bb8aefdSYi Liu 		inode->i_fop = &orangefs_file_operations;
9885db11c21SMike Marshall 		break;
9895db11c21SMike Marshall 	case S_IFLNK:
9908bb8aefdSYi Liu 		inode->i_op = &orangefs_symlink_inode_operations;
9915db11c21SMike Marshall 		break;
9925db11c21SMike Marshall 	case S_IFDIR:
9938bb8aefdSYi Liu 		inode->i_op = &orangefs_dir_inode_operations;
9948bb8aefdSYi Liu 		inode->i_fop = &orangefs_dir_operations;
9955db11c21SMike Marshall 		break;
9965db11c21SMike Marshall 	default:
9975db11c21SMike Marshall 		gossip_debug(GOSSIP_INODE_DEBUG,
9985db11c21SMike Marshall 			     "%s: unsupported mode\n",
9995db11c21SMike Marshall 			     __func__);
10005db11c21SMike Marshall 		return -EINVAL;
10015db11c21SMike Marshall 	}
10025db11c21SMike Marshall 
10035db11c21SMike Marshall 	return 0;
10045db11c21SMike Marshall }
10055db11c21SMike Marshall 
10065db11c21SMike Marshall /*
100795f5f88fSMike Marshall  * Given an ORANGEFS object identifier (fsid, handle), convert it into
100895f5f88fSMike Marshall  * a ino_t type that will be used as a hash-index from where the handle will
10095db11c21SMike Marshall  * be searched for in the VFS hash table of inodes.
10105db11c21SMike Marshall  */
orangefs_handle_hash(struct orangefs_object_kref * ref)10118bb8aefdSYi Liu static inline ino_t orangefs_handle_hash(struct orangefs_object_kref *ref)
10125db11c21SMike Marshall {
10135db11c21SMike Marshall 	if (!ref)
10145db11c21SMike Marshall 		return 0;
10158bb8aefdSYi Liu 	return orangefs_khandle_to_ino(&(ref->khandle));
10165db11c21SMike Marshall }
10175db11c21SMike Marshall 
10185db11c21SMike Marshall /*
10195db11c21SMike Marshall  * Called to set up an inode from iget5_locked.
10205db11c21SMike Marshall  */
orangefs_set_inode(struct inode * inode,void * data)10218bb8aefdSYi Liu static int orangefs_set_inode(struct inode *inode, void *data)
10225db11c21SMike Marshall {
10238bb8aefdSYi Liu 	struct orangefs_object_kref *ref = (struct orangefs_object_kref *) data;
1024a4c680a0SMartin Brandenburg 	ORANGEFS_I(inode)->refn.fs_id = ref->fs_id;
1025a4c680a0SMartin Brandenburg 	ORANGEFS_I(inode)->refn.khandle = ref->khandle;
1026afd9fb2aSMartin Brandenburg 	ORANGEFS_I(inode)->attr_valid = 0;
1027fc2e2e9cSMartin Brandenburg 	hash_init(ORANGEFS_I(inode)->xattr_cache);
10288f04e1beSMartin Brandenburg 	ORANGEFS_I(inode)->mapping_time = jiffies - 1;
10298f04e1beSMartin Brandenburg 	ORANGEFS_I(inode)->bitlock = 0;
10305db11c21SMike Marshall 	return 0;
10315db11c21SMike Marshall }
10325db11c21SMike Marshall 
10335db11c21SMike Marshall /*
10345db11c21SMike Marshall  * Called to determine if handles match.
10355db11c21SMike Marshall  */
orangefs_test_inode(struct inode * inode,void * data)10368bb8aefdSYi Liu static int orangefs_test_inode(struct inode *inode, void *data)
10375db11c21SMike Marshall {
10388bb8aefdSYi Liu 	struct orangefs_object_kref *ref = (struct orangefs_object_kref *) data;
10398bb8aefdSYi Liu 	struct orangefs_inode_s *orangefs_inode = NULL;
10405db11c21SMike Marshall 
10418bb8aefdSYi Liu 	orangefs_inode = ORANGEFS_I(inode);
104295f5f88fSMike Marshall 	/* test handles and fs_ids... */
104395f5f88fSMike Marshall 	return (!ORANGEFS_khandle_cmp(&(orangefs_inode->refn.khandle),
104495f5f88fSMike Marshall 				&(ref->khandle)) &&
104595f5f88fSMike Marshall 			orangefs_inode->refn.fs_id == ref->fs_id);
10465db11c21SMike Marshall }
10475db11c21SMike Marshall 
10485db11c21SMike Marshall /*
10498bb8aefdSYi Liu  * Front-end to lookup the inode-cache maintained by the VFS using the ORANGEFS
10505db11c21SMike Marshall  * file handle.
10515db11c21SMike Marshall  *
10525db11c21SMike Marshall  * @sb: the file system super block instance.
105395f5f88fSMike Marshall  * @ref: The ORANGEFS object for which we are trying to locate an inode.
10545db11c21SMike Marshall  */
orangefs_iget(struct super_block * sb,struct orangefs_object_kref * ref)105595f5f88fSMike Marshall struct inode *orangefs_iget(struct super_block *sb,
105695f5f88fSMike Marshall 		struct orangefs_object_kref *ref)
10575db11c21SMike Marshall {
10585db11c21SMike Marshall 	struct inode *inode = NULL;
10595db11c21SMike Marshall 	unsigned long hash;
10605db11c21SMike Marshall 	int error;
10615db11c21SMike Marshall 
10628bb8aefdSYi Liu 	hash = orangefs_handle_hash(ref);
106395f5f88fSMike Marshall 	inode = iget5_locked(sb,
106495f5f88fSMike Marshall 			hash,
106595f5f88fSMike Marshall 			orangefs_test_inode,
106695f5f88fSMike Marshall 			orangefs_set_inode,
106795f5f88fSMike Marshall 			ref);
1068b5d72cdcSMike Marshall 
1069b5d72cdcSMike Marshall 	if (!inode)
1070b5d72cdcSMike Marshall 		return ERR_PTR(-ENOMEM);
1071b5d72cdcSMike Marshall 
1072b5d72cdcSMike Marshall 	if (!(inode->i_state & I_NEW))
10735db11c21SMike Marshall 		return inode;
10745db11c21SMike Marshall 
10758b60785cSMartin Brandenburg 	error = orangefs_inode_getattr(inode, ORANGEFS_GETATTR_NEW);
10765db11c21SMike Marshall 	if (error) {
10775db11c21SMike Marshall 		iget_failed(inode);
10785db11c21SMike Marshall 		return ERR_PTR(error);
10795db11c21SMike Marshall 	}
10805db11c21SMike Marshall 
10815db11c21SMike Marshall 	inode->i_ino = hash;	/* needed for stat etc */
10828bb8aefdSYi Liu 	orangefs_init_iops(inode);
10835db11c21SMike Marshall 	unlock_new_inode(inode);
10845db11c21SMike Marshall 
10855db11c21SMike Marshall 	gossip_debug(GOSSIP_INODE_DEBUG,
10865db11c21SMike Marshall 		     "iget handle %pU, fsid %d hash %ld i_ino %lu\n",
10875db11c21SMike Marshall 		     &ref->khandle,
10885db11c21SMike Marshall 		     ref->fs_id,
10895db11c21SMike Marshall 		     hash,
10905db11c21SMike Marshall 		     inode->i_ino);
10915db11c21SMike Marshall 
10925db11c21SMike Marshall 	return inode;
10935db11c21SMike Marshall }
10945db11c21SMike Marshall 
10955db11c21SMike Marshall /*
10965db11c21SMike Marshall  * Allocate an inode for a newly created file and insert it into the inode hash.
10975db11c21SMike Marshall  */
orangefs_new_inode(struct super_block * sb,struct inode * dir,umode_t mode,dev_t dev,struct orangefs_object_kref * ref)10988bb8aefdSYi Liu struct inode *orangefs_new_inode(struct super_block *sb, struct inode *dir,
10994053d250SChristian Brauner 		umode_t mode, dev_t dev, struct orangefs_object_kref *ref)
11005db11c21SMike Marshall {
11014053d250SChristian Brauner 	struct posix_acl *acl = NULL, *default_acl = NULL;
11028bb8aefdSYi Liu 	unsigned long hash = orangefs_handle_hash(ref);
11035db11c21SMike Marshall 	struct inode *inode;
11045db11c21SMike Marshall 	int error;
11055db11c21SMike Marshall 
11065db11c21SMike Marshall 	gossip_debug(GOSSIP_INODE_DEBUG,
11075253487eSMike Marshall 		     "%s:(sb is %p | MAJOR(dev)=%u | MINOR(dev)=%u mode=%o)\n",
11085253487eSMike Marshall 		     __func__,
11095db11c21SMike Marshall 		     sb,
11105db11c21SMike Marshall 		     MAJOR(dev),
11115db11c21SMike Marshall 		     MINOR(dev),
11125db11c21SMike Marshall 		     mode);
11135db11c21SMike Marshall 
11145db11c21SMike Marshall 	inode = new_inode(sb);
11155db11c21SMike Marshall 	if (!inode)
111656249998SMike Marshall 		return ERR_PTR(-ENOMEM);
11175db11c21SMike Marshall 
11184053d250SChristian Brauner 	error = posix_acl_create(dir, &mode, &default_acl, &acl);
11194053d250SChristian Brauner 	if (error)
11204053d250SChristian Brauner 		goto out_iput;
11214053d250SChristian Brauner 
11228bb8aefdSYi Liu 	orangefs_set_inode(inode, ref);
11235db11c21SMike Marshall 	inode->i_ino = hash;	/* needed for stat etc */
11245db11c21SMike Marshall 
11258b60785cSMartin Brandenburg 	error = orangefs_inode_getattr(inode, ORANGEFS_GETATTR_NEW);
11265db11c21SMike Marshall 	if (error)
11275db11c21SMike Marshall 		goto out_iput;
11285db11c21SMike Marshall 
11298bb8aefdSYi Liu 	orangefs_init_iops(inode);
11305db11c21SMike Marshall 	inode->i_rdev = dev;
11315db11c21SMike Marshall 
11324053d250SChristian Brauner 	if (default_acl) {
11334053d250SChristian Brauner 		error = __orangefs_set_acl(inode, default_acl,
11344053d250SChristian Brauner 					   ACL_TYPE_DEFAULT);
11354053d250SChristian Brauner 		if (error)
11364053d250SChristian Brauner 			goto out_iput;
11374053d250SChristian Brauner 	}
11384053d250SChristian Brauner 
11394053d250SChristian Brauner 	if (acl) {
11404053d250SChristian Brauner 		error = __orangefs_set_acl(inode, acl, ACL_TYPE_ACCESS);
11414053d250SChristian Brauner 		if (error)
11424053d250SChristian Brauner 			goto out_iput;
11434053d250SChristian Brauner 	}
11444053d250SChristian Brauner 
11458bb8aefdSYi Liu 	error = insert_inode_locked4(inode, hash, orangefs_test_inode, ref);
11465db11c21SMike Marshall 	if (error < 0)
11475db11c21SMike Marshall 		goto out_iput;
11485db11c21SMike Marshall 
11495db11c21SMike Marshall 	gossip_debug(GOSSIP_INODE_DEBUG,
11505db11c21SMike Marshall 		     "Initializing ACL's for inode %pU\n",
11515db11c21SMike Marshall 		     get_khandle_from_ino(inode));
1152e40df428SChristian Brauner 	if (mode != inode->i_mode) {
1153e40df428SChristian Brauner 		struct iattr iattr = {
1154e40df428SChristian Brauner 			.ia_mode = mode,
1155e40df428SChristian Brauner 			.ia_valid = ATTR_MODE,
1156e40df428SChristian Brauner 		};
1157e40df428SChristian Brauner 		inode->i_mode = mode;
1158e40df428SChristian Brauner 		__orangefs_setattr(inode, &iattr);
1159e40df428SChristian Brauner 		__posix_acl_chmod(&acl, GFP_KERNEL, inode->i_mode);
1160e40df428SChristian Brauner 	}
11614053d250SChristian Brauner 	posix_acl_release(acl);
11624053d250SChristian Brauner 	posix_acl_release(default_acl);
11635db11c21SMike Marshall 	return inode;
11645db11c21SMike Marshall 
11655db11c21SMike Marshall out_iput:
11665db11c21SMike Marshall 	iput(inode);
11674053d250SChristian Brauner 	posix_acl_release(acl);
11684053d250SChristian Brauner 	posix_acl_release(default_acl);
11695db11c21SMike Marshall 	return ERR_PTR(error);
11705db11c21SMike Marshall }
1171