orangefs: migrate to generic_file_read_iter
[linux-block.git] / fs / orangefs / inode.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
5db11c21
MM
2/*
3 * (C) 2001 Clemson University and The University of Chicago
afd9fb2a 4 * Copyright 2018 Omnibond Systems, L.L.C.
5db11c21
MM
5 *
6 * See COPYING in top-level directory.
7 */
8
9/*
10 * Linux VFS inode operations.
11 */
12
2f8b5444 13#include <linux/bvec.h>
5db11c21 14#include "protocol.h"
575e9461
MM
15#include "orangefs-kernel.h"
16#include "orangefs-bufmap.h"
5db11c21 17
a68d9c60 18static int orangefs_readpage(struct file *file, struct page *page)
5db11c21 19{
5db11c21 20 struct inode *inode = page->mapping->host;
c453dcfc
MB
21 struct iov_iter iter;
22 struct bio_vec bv;
23 ssize_t ret;
24 loff_t off;
25
26 off = page_offset(page);
27 bv.bv_page = page;
28 bv.bv_len = PAGE_SIZE;
29 bv.bv_offset = 0;
30 iov_iter_bvec(&iter, READ, &bv, 1, PAGE_SIZE);
31
32 ret = wait_for_direct_io(ORANGEFS_IO_READ, inode, &off, &iter,
33 PAGE_SIZE, inode->i_size);
74f68fce 34 /* this will only zero remaining unread portions of the page data */
c453dcfc 35 iov_iter_zero(~0U, &iter);
5db11c21
MM
36 /* takes care of potential aliasing */
37 flush_dcache_page(page);
c453dcfc 38 if (ret < 0) {
5db11c21
MM
39 SetPageError(page);
40 } else {
41 SetPageUptodate(page);
42 if (PageError(page))
43 ClearPageError(page);
44 ret = 0;
45 }
5db11c21
MM
46 /* unlock the page after the ->readpage() routine completes */
47 unlock_page(page);
48 return ret;
49}
50
8bb8aefd 51static void orangefs_invalidatepage(struct page *page,
5db11c21
MM
52 unsigned int offset,
53 unsigned int length)
54{
55 gossip_debug(GOSSIP_INODE_DEBUG,
8bb8aefd 56 "orangefs_invalidatepage called on page %p "
5db11c21
MM
57 "(offset is %u)\n",
58 page,
59 offset);
60
61 ClearPageUptodate(page);
62 ClearPageMappedToDisk(page);
63 return;
64
65}
66
8bb8aefd 67static int orangefs_releasepage(struct page *page, gfp_t foo)
5db11c21
MM
68{
69 gossip_debug(GOSSIP_INODE_DEBUG,
8bb8aefd 70 "orangefs_releasepage called on page %p\n",
5db11c21
MM
71 page);
72 return 0;
73}
74
3903f150
MM
75static ssize_t orangefs_direct_IO(struct kiocb *iocb,
76 struct iov_iter *iter)
77{
c453dcfc
MB
78 struct file *file = iocb->ki_filp;
79 loff_t pos = *(&iocb->ki_pos);
80 /*
81 * This cannot happen until write_iter becomes
82 * generic_file_write_iter.
83 */
84 BUG_ON(iov_iter_rw(iter) != READ);
85 return do_readv_writev(ORANGEFS_IO_READ, file, &pos, iter);
3903f150 86}
5db11c21 87
8bb8aefd 88/** ORANGEFS2 implementation of address space operations */
bdd6f083 89static const struct address_space_operations orangefs_address_operations = {
8bb8aefd 90 .readpage = orangefs_readpage,
8bb8aefd
YL
91 .invalidatepage = orangefs_invalidatepage,
92 .releasepage = orangefs_releasepage,
3903f150 93 .direct_IO = orangefs_direct_IO,
5db11c21
MM
94};
95
8bb8aefd 96static int orangefs_setattr_size(struct inode *inode, struct iattr *iattr)
5db11c21 97{
8bb8aefd
YL
98 struct orangefs_inode_s *orangefs_inode = ORANGEFS_I(inode);
99 struct orangefs_kernel_op_s *new_op;
fecd86aa 100 loff_t orig_size;
5db11c21
MM
101 int ret = -EINVAL;
102
103 gossip_debug(GOSSIP_INODE_DEBUG,
104 "%s: %pU: Handle is %pU | fs_id %d | size is %llu\n",
105 __func__,
106 get_khandle_from_ino(inode),
8bb8aefd
YL
107 &orangefs_inode->refn.khandle,
108 orangefs_inode->refn.fs_id,
5db11c21
MM
109 iattr->ia_size);
110
fecd86aa 111 /* Ensure that we have a up to date size, so we know if it changed. */
8b60785c 112 ret = orangefs_inode_getattr(inode, ORANGEFS_GETATTR_SIZE);
fecd86aa
MB
113 if (ret == -ESTALE)
114 ret = -EIO;
115 if (ret) {
116 gossip_err("%s: orangefs_inode_getattr failed, ret:%d:.\n",
117 __func__, ret);
118 return ret;
119 }
120 orig_size = i_size_read(inode);
121
5db11c21
MM
122 truncate_setsize(inode, iattr->ia_size);
123
8bb8aefd 124 new_op = op_alloc(ORANGEFS_VFS_OP_TRUNCATE);
5db11c21
MM
125 if (!new_op)
126 return -ENOMEM;
127
8bb8aefd 128 new_op->upcall.req.truncate.refn = orangefs_inode->refn;
5db11c21
MM
129 new_op->upcall.req.truncate.size = (__s64) iattr->ia_size;
130
95f5f88f
MM
131 ret = service_operation(new_op,
132 __func__,
133 get_interruptible_flag(inode));
5db11c21
MM
134
135 /*
136 * the truncate has no downcall members to retrieve, but
137 * the status value tells us if it went through ok or not
138 */
95f5f88f 139 gossip_debug(GOSSIP_INODE_DEBUG, "%s: ret:%d:\n", __func__, ret);
5db11c21
MM
140
141 op_release(new_op);
142
143 if (ret != 0)
144 return ret;
145
f83140c1 146 if (orig_size != i_size_read(inode))
5db11c21 147 iattr->ia_valid |= ATTR_CTIME | ATTR_MTIME;
5db11c21
MM
148
149 return ret;
150}
151
afd9fb2a 152int __orangefs_setattr(struct inode *inode, struct iattr *iattr)
5db11c21 153{
df2d7337 154 int ret;
5db11c21 155
afd9fb2a
MB
156 if (iattr->ia_valid & ATTR_MODE) {
157 if (iattr->ia_mode & (S_ISVTX)) {
158 if (is_root_handle(inode)) {
159 /*
160 * allow sticky bit to be set on root (since
161 * it shows up that way by default anyhow),
162 * but don't show it to the server
163 */
164 iattr->ia_mode -= S_ISVTX;
165 } else {
166 gossip_debug(GOSSIP_UTILS_DEBUG,
167 "User attempted to set sticky bit on non-root directory; returning EINVAL.\n");
168 return -EINVAL;
169 }
170 }
171 if (iattr->ia_mode & (S_ISUID)) {
172 gossip_debug(GOSSIP_UTILS_DEBUG,
173 "Attempting to set setuid bit (not supported); returning EINVAL.\n");
174 return -EINVAL;
175 }
176 }
5db11c21 177
53950ef5 178 if (iattr->ia_valid & ATTR_SIZE) {
8bb8aefd 179 ret = orangefs_setattr_size(inode, iattr);
5db11c21
MM
180 if (ret)
181 goto out;
182 }
183
afd9fb2a
MB
184again:
185 spin_lock(&inode->i_lock);
186 if (ORANGEFS_I(inode)->attr_valid) {
187 if (uid_eq(ORANGEFS_I(inode)->attr_uid, current_fsuid()) &&
188 gid_eq(ORANGEFS_I(inode)->attr_gid, current_fsgid())) {
189 ORANGEFS_I(inode)->attr_valid = iattr->ia_valid;
190 } else {
191 spin_unlock(&inode->i_lock);
192 write_inode_now(inode, 1);
193 goto again;
194 }
195 } else {
196 ORANGEFS_I(inode)->attr_valid = iattr->ia_valid;
197 ORANGEFS_I(inode)->attr_uid = current_fsuid();
198 ORANGEFS_I(inode)->attr_gid = current_fsgid();
199 }
5db11c21 200 setattr_copy(inode, iattr);
afd9fb2a 201 spin_unlock(&inode->i_lock);
5db11c21
MM
202 mark_inode_dirty(inode);
203
df2d7337 204 if (iattr->ia_valid & ATTR_MODE)
5db11c21
MM
205 /* change mod on a file that has ACLs */
206 ret = posix_acl_chmod(inode, inode->i_mode);
207
df2d7337 208 ret = 0;
5db11c21 209out:
afd9fb2a
MB
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;
219 gossip_debug(GOSSIP_INODE_DEBUG, "__orangefs_setattr: called on %pd\n",
220 dentry);
221 ret = setattr_prepare(dentry, iattr);
222 if (ret)
223 goto out;
224 ret = __orangefs_setattr(d_inode(dentry), iattr);
225 sync_inode_metadata(d_inode(dentry), 1);
226out:
227 gossip_debug(GOSSIP_INODE_DEBUG, "orangefs_setattr: returning %d\n",
228 ret);
5db11c21
MM
229 return ret;
230}
231
232/*
233 * Obtain attributes of an object given a dentry
234 */
a528d35e
DH
235int orangefs_getattr(const struct path *path, struct kstat *stat,
236 u32 request_mask, unsigned int flags)
5db11c21
MM
237{
238 int ret = -ENOENT;
a528d35e 239 struct inode *inode = path->dentry->d_inode;
5db11c21
MM
240
241 gossip_debug(GOSSIP_INODE_DEBUG,
5e4f606e
MB
242 "orangefs_getattr: called on %pd mask %u\n",
243 path->dentry, request_mask);
5db11c21 244
8b60785c
MB
245 ret = orangefs_inode_getattr(inode,
246 request_mask & STATX_SIZE ? ORANGEFS_GETATTR_SIZE : 0);
5db11c21 247 if (ret == 0) {
a528d35e 248 generic_fillattr(inode, stat);
a7d3e78a 249
5db11c21 250 /* override block size reported to stat */
5678b5d6
CH
251 if (!(request_mask & STATX_SIZE))
252 stat->result_mask &= ~STATX_SIZE;
7f54910f
MB
253
254 stat->attributes_mask = STATX_ATTR_IMMUTABLE |
255 STATX_ATTR_APPEND;
256 if (inode->i_flags & S_IMMUTABLE)
257 stat->attributes |= STATX_ATTR_IMMUTABLE;
258 if (inode->i_flags & S_APPEND)
259 stat->attributes |= STATX_ATTR_APPEND;
5db11c21
MM
260 }
261 return ret;
262}
263
933287da
MB
264int orangefs_permission(struct inode *inode, int mask)
265{
266 int ret;
267
268 if (mask & MAY_NOT_BLOCK)
269 return -ECHILD;
270
271 gossip_debug(GOSSIP_INODE_DEBUG, "%s: refreshing\n", __func__);
272
273 /* Make sure the permission (and other common attrs) are up to date. */
8b60785c 274 ret = orangefs_inode_getattr(inode, 0);
933287da
MB
275 if (ret < 0)
276 return ret;
277
278 return generic_permission(inode, mask);
279}
280
95582b00 281int orangefs_update_time(struct inode *inode, struct timespec64 *time, int flags)
a55f2d86
MB
282{
283 struct iattr iattr;
284 gossip_debug(GOSSIP_INODE_DEBUG, "orangefs_update_time: %pU\n",
285 get_khandle_from_ino(inode));
286 generic_update_time(inode, time, flags);
287 memset(&iattr, 0, sizeof iattr);
288 if (flags & S_ATIME)
289 iattr.ia_valid |= ATTR_ATIME;
290 if (flags & S_CTIME)
291 iattr.ia_valid |= ATTR_CTIME;
292 if (flags & S_MTIME)
293 iattr.ia_valid |= ATTR_MTIME;
afd9fb2a 294 return __orangefs_setattr(inode, &iattr);
a55f2d86
MB
295}
296
95f5f88f 297/* ORANGEFS2 implementation of VFS inode operations for files */
bdd6f083 298static const struct inode_operations orangefs_file_inode_operations = {
8bb8aefd
YL
299 .get_acl = orangefs_get_acl,
300 .set_acl = orangefs_set_acl,
301 .setattr = orangefs_setattr,
302 .getattr = orangefs_getattr,
8bb8aefd 303 .listxattr = orangefs_listxattr,
933287da 304 .permission = orangefs_permission,
a55f2d86 305 .update_time = orangefs_update_time,
5db11c21
MM
306};
307
8bb8aefd 308static int orangefs_init_iops(struct inode *inode)
5db11c21 309{
8bb8aefd 310 inode->i_mapping->a_ops = &orangefs_address_operations;
5db11c21
MM
311
312 switch (inode->i_mode & S_IFMT) {
313 case S_IFREG:
8bb8aefd
YL
314 inode->i_op = &orangefs_file_inode_operations;
315 inode->i_fop = &orangefs_file_operations;
5db11c21
MM
316 break;
317 case S_IFLNK:
8bb8aefd 318 inode->i_op = &orangefs_symlink_inode_operations;
5db11c21
MM
319 break;
320 case S_IFDIR:
8bb8aefd
YL
321 inode->i_op = &orangefs_dir_inode_operations;
322 inode->i_fop = &orangefs_dir_operations;
5db11c21
MM
323 break;
324 default:
325 gossip_debug(GOSSIP_INODE_DEBUG,
326 "%s: unsupported mode\n",
327 __func__);
328 return -EINVAL;
329 }
330
331 return 0;
332}
333
334/*
95f5f88f
MM
335 * Given an ORANGEFS object identifier (fsid, handle), convert it into
336 * a ino_t type that will be used as a hash-index from where the handle will
5db11c21
MM
337 * be searched for in the VFS hash table of inodes.
338 */
8bb8aefd 339static inline ino_t orangefs_handle_hash(struct orangefs_object_kref *ref)
5db11c21
MM
340{
341 if (!ref)
342 return 0;
8bb8aefd 343 return orangefs_khandle_to_ino(&(ref->khandle));
5db11c21
MM
344}
345
346/*
347 * Called to set up an inode from iget5_locked.
348 */
8bb8aefd 349static int orangefs_set_inode(struct inode *inode, void *data)
5db11c21 350{
8bb8aefd 351 struct orangefs_object_kref *ref = (struct orangefs_object_kref *) data;
a4c680a0
MB
352 ORANGEFS_I(inode)->refn.fs_id = ref->fs_id;
353 ORANGEFS_I(inode)->refn.khandle = ref->khandle;
afd9fb2a 354 ORANGEFS_I(inode)->attr_valid = 0;
fc2e2e9c 355 hash_init(ORANGEFS_I(inode)->xattr_cache);
5db11c21
MM
356 return 0;
357}
358
359/*
360 * Called to determine if handles match.
361 */
8bb8aefd 362static int orangefs_test_inode(struct inode *inode, void *data)
5db11c21 363{
8bb8aefd
YL
364 struct orangefs_object_kref *ref = (struct orangefs_object_kref *) data;
365 struct orangefs_inode_s *orangefs_inode = NULL;
5db11c21 366
8bb8aefd 367 orangefs_inode = ORANGEFS_I(inode);
95f5f88f
MM
368 /* test handles and fs_ids... */
369 return (!ORANGEFS_khandle_cmp(&(orangefs_inode->refn.khandle),
370 &(ref->khandle)) &&
371 orangefs_inode->refn.fs_id == ref->fs_id);
5db11c21
MM
372}
373
374/*
8bb8aefd 375 * Front-end to lookup the inode-cache maintained by the VFS using the ORANGEFS
5db11c21
MM
376 * file handle.
377 *
378 * @sb: the file system super block instance.
95f5f88f 379 * @ref: The ORANGEFS object for which we are trying to locate an inode.
5db11c21 380 */
95f5f88f
MM
381struct inode *orangefs_iget(struct super_block *sb,
382 struct orangefs_object_kref *ref)
5db11c21
MM
383{
384 struct inode *inode = NULL;
385 unsigned long hash;
386 int error;
387
8bb8aefd 388 hash = orangefs_handle_hash(ref);
95f5f88f
MM
389 inode = iget5_locked(sb,
390 hash,
391 orangefs_test_inode,
392 orangefs_set_inode,
393 ref);
b5d72cdc
MM
394
395 if (!inode)
396 return ERR_PTR(-ENOMEM);
397
398 if (!(inode->i_state & I_NEW))
5db11c21
MM
399 return inode;
400
8b60785c 401 error = orangefs_inode_getattr(inode, ORANGEFS_GETATTR_NEW);
5db11c21
MM
402 if (error) {
403 iget_failed(inode);
404 return ERR_PTR(error);
405 }
406
407 inode->i_ino = hash; /* needed for stat etc */
8bb8aefd 408 orangefs_init_iops(inode);
5db11c21
MM
409 unlock_new_inode(inode);
410
411 gossip_debug(GOSSIP_INODE_DEBUG,
412 "iget handle %pU, fsid %d hash %ld i_ino %lu\n",
413 &ref->khandle,
414 ref->fs_id,
415 hash,
416 inode->i_ino);
417
418 return inode;
419}
420
421/*
422 * Allocate an inode for a newly created file and insert it into the inode hash.
423 */
8bb8aefd
YL
424struct inode *orangefs_new_inode(struct super_block *sb, struct inode *dir,
425 int mode, dev_t dev, struct orangefs_object_kref *ref)
5db11c21 426{
8bb8aefd 427 unsigned long hash = orangefs_handle_hash(ref);
5db11c21
MM
428 struct inode *inode;
429 int error;
430
431 gossip_debug(GOSSIP_INODE_DEBUG,
5253487e
MM
432 "%s:(sb is %p | MAJOR(dev)=%u | MINOR(dev)=%u mode=%o)\n",
433 __func__,
5db11c21
MM
434 sb,
435 MAJOR(dev),
436 MINOR(dev),
437 mode);
438
439 inode = new_inode(sb);
440 if (!inode)
56249998 441 return ERR_PTR(-ENOMEM);
5db11c21 442
8bb8aefd 443 orangefs_set_inode(inode, ref);
5db11c21
MM
444 inode->i_ino = hash; /* needed for stat etc */
445
8b60785c 446 error = orangefs_inode_getattr(inode, ORANGEFS_GETATTR_NEW);
5db11c21
MM
447 if (error)
448 goto out_iput;
449
8bb8aefd 450 orangefs_init_iops(inode);
5db11c21
MM
451 inode->i_rdev = dev;
452
8bb8aefd 453 error = insert_inode_locked4(inode, hash, orangefs_test_inode, ref);
5db11c21
MM
454 if (error < 0)
455 goto out_iput;
456
457 gossip_debug(GOSSIP_INODE_DEBUG,
458 "Initializing ACL's for inode %pU\n",
459 get_khandle_from_ino(inode));
8bb8aefd 460 orangefs_init_acl(inode, dir);
5db11c21
MM
461 return inode;
462
463out_iput:
464 iput(inode);
465 return ERR_PTR(error);
466}