Linux Audio

Check our new training course

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