Merge tag 'pci-v6.16-fixes-3' of git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci
[linux-2.6-block.git] / fs / afs / inode.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
3 *
4 * This software may be freely redistributed under the terms of the
5 * GNU General Public License.
6 *
7 * You should have received a copy of the GNU General Public License
8 * along with this program; if not, write to the Free Software
9 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
10 *
44d1b980 11 * Authors: David Woodhouse <dwmw2@infradead.org>
1da177e4
LT
12 * David Howells <dhowells@redhat.com>
13 *
14 */
15
16#include <linux/kernel.h>
17#include <linux/module.h>
18#include <linux/init.h>
1da177e4
LT
19#include <linux/fs.h>
20#include <linux/pagemap.h>
e8edc6e0 21#include <linux/sched.h>
bec5eb61 22#include <linux/mount.h>
23#include <linux/namei.h>
a01179e6 24#include <linux/iversion.h>
1da177e4 25#include "internal.h"
a38a7558 26#include "afs_fs.h"
1da177e4 27
6698c02d
DH
28void afs_init_new_symlink(struct afs_vnode *vnode, struct afs_operation *op)
29{
30 size_t size = strlen(op->create.symlink) + 1;
31 size_t dsize = 0;
32 char *p;
33
34 if (netfs_alloc_folioq_buffer(NULL, &vnode->directory, &dsize, size,
35 mapping_gfp_mask(vnode->netfs.inode.i_mapping)) < 0)
36 return;
37
38 vnode->directory_size = dsize;
39 p = kmap_local_folio(folioq_folio(vnode->directory, 0), 0);
40 memcpy(p, op->create.symlink, size);
41 kunmap_local(p);
42 set_bit(AFS_VNODE_DIR_READ, &vnode->flags);
43 netfs_single_mark_inode_dirty(&vnode->netfs.inode);
44}
45
eae9e789
DH
46static void afs_put_link(void *arg)
47{
48 struct folio *folio = virt_to_folio(arg);
49
50 kunmap_local(arg);
51 folio_put(folio);
52}
53
54const char *afs_get_link(struct dentry *dentry, struct inode *inode,
55 struct delayed_call *callback)
56{
57 struct afs_vnode *vnode = AFS_FS_I(inode);
58 struct folio *folio;
59 char *content;
60 ssize_t ret;
61
6698c02d
DH
62 if (!dentry) {
63 /* RCU pathwalk. */
64 if (!test_bit(AFS_VNODE_DIR_READ, &vnode->flags) || !afs_check_validity(vnode))
eae9e789 65 return ERR_PTR(-ECHILD);
6698c02d 66 goto good;
eae9e789
DH
67 }
68
6698c02d
DH
69 if (test_bit(AFS_VNODE_DIR_READ, &vnode->flags))
70 goto fetch;
71
72 ret = afs_validate(vnode, NULL);
73 if (ret < 0)
74 return ERR_PTR(ret);
75
76 if (!test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags) &&
77 test_bit(AFS_VNODE_DIR_READ, &vnode->flags))
78 goto good;
79
80fetch:
81 ret = afs_read_single(vnode, NULL);
82 if (ret < 0)
83 return ERR_PTR(ret);
84 set_bit(AFS_VNODE_DIR_READ, &vnode->flags);
85
86good:
eae9e789
DH
87 folio = folioq_folio(vnode->directory, 0);
88 folio_get(folio);
89 content = kmap_local_folio(folio, 0);
90 set_delayed_call(callback, afs_put_link, content);
91 return content;
92}
93
94int afs_readlink(struct dentry *dentry, char __user *buffer, int buflen)
95{
96 DEFINE_DELAYED_CALL(done);
97 const char *content;
98 int len;
99
100 content = afs_get_link(dentry, d_inode(dentry), &done);
101 if (IS_ERR(content)) {
102 do_delayed_call(&done);
103 return PTR_ERR(content);
104 }
105
106 len = umin(strlen(content), buflen);
107 if (copy_to_user(buffer, content, len))
108 len = -EFAULT;
109 do_delayed_call(&done);
110 return len;
111}
112
d3e3b7ea 113static const struct inode_operations afs_symlink_inode_operations = {
eae9e789
DH
114 .get_link = afs_get_link,
115 .readlink = afs_readlink,
d3e3b7ea
DH
116};
117
b134d687
DH
118static noinline void dump_vnode(struct afs_vnode *vnode, struct afs_vnode *parent_vnode)
119{
120 static unsigned long once_only;
121
a4e530ae 122 pr_warn("kAFS: AFS vnode with undefined type %u\n", vnode->status.type);
b134d687
DH
123 pr_warn("kAFS: A=%d m=%o s=%llx v=%llx\n",
124 vnode->status.abort_code,
125 vnode->status.mode,
126 vnode->status.size,
127 vnode->status.data_version);
128 pr_warn("kAFS: vnode %llx:%llx:%x\n",
129 vnode->fid.vid,
130 vnode->fid.vnode,
131 vnode->fid.unique);
132 if (parent_vnode)
133 pr_warn("kAFS: dir %llx:%llx:%x\n",
134 parent_vnode->fid.vid,
135 parent_vnode->fid.vnode,
136 parent_vnode->fid.unique);
137
138 if (!test_and_set_bit(0, &once_only))
139 dump_stack();
140}
141
bc899ee1
DH
142/*
143 * Set parameters for the netfs library
144 */
145static void afs_set_netfs_context(struct afs_vnode *vnode)
146{
100ccd18 147 netfs_inode_init(&vnode->netfs, &afs_req_ops, true);
bc899ee1
DH
148}
149
1da177e4 150/*
dd9fbcb8 151 * Initialise an inode from the vnode status.
1da177e4 152 */
e49c7b2f
DH
153static int afs_inode_init_from_status(struct afs_operation *op,
154 struct afs_vnode_param *vp,
155 struct afs_vnode *vnode)
1da177e4 156{
e49c7b2f 157 struct afs_file_status *status = &vp->scb.status;
1da177e4 158 struct inode *inode = AFS_VNODE_TO_I(vnode);
a58823ac 159 struct timespec64 t;
1da177e4 160
e49c7b2f
DH
161 _enter("{%llx:%llu.%u} %s",
162 vp->fid.vid, vp->fid.vnode, vp->fid.unique,
163 op->type ? op->type->name : "???");
164
260a9803 165 _debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu",
a58823ac
DH
166 status->type,
167 status->nlink,
168 (unsigned long long) status->size,
169 status->data_version,
170 status->mode);
1da177e4 171
a58823ac
DH
172 write_seqlock(&vnode->cb_lock);
173
453924de 174 vnode->cb_v_check = op->cb_v_break;
a58823ac 175 vnode->status = *status;
c435ee34 176
a58823ac 177 t = status->mtime_client;
b9170a28 178 inode_set_ctime_to_ts(inode, t);
562ce1f7
JL
179 inode_set_mtime_to_ts(inode, t);
180 inode_set_atime_to_ts(inode, t);
e49c7b2f 181 inode->i_flags |= S_NOATIME;
a58823ac
DH
182 inode->i_uid = make_kuid(&init_user_ns, status->owner);
183 inode->i_gid = make_kgid(&init_user_ns, status->group);
874c8ca1 184 set_nlink(&vnode->netfs.inode, status->nlink);
dd9fbcb8 185
a58823ac 186 switch (status->type) {
1da177e4 187 case AFS_FTYPE_FILE:
6e1eb04a 188 inode->i_mode = S_IFREG | (status->mode & S_IALLUGO);
1da177e4 189 inode->i_op = &afs_file_inode_operations;
00d3b7a4 190 inode->i_fop = &afs_file_operations;
75bd228d 191 inode->i_mapping->a_ops = &afs_file_aops;
8549a263 192 mapping_set_large_folios(inode->i_mapping);
1da177e4
LT
193 break;
194 case AFS_FTYPE_DIR:
6e1eb04a 195 inode->i_mode = S_IFDIR | (status->mode & S_IALLUGO);
1da177e4
LT
196 inode->i_op = &afs_dir_inode_operations;
197 inode->i_fop = &afs_dir_file_operations;
f3ddee8d 198 inode->i_mapping->a_ops = &afs_dir_aops;
6dd80936
DH
199 __set_bit(NETFS_ICTX_SINGLE_NO_UPLOAD, &vnode->netfs.flags);
200 /* Assume locally cached directory data will be valid. */
201 __set_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
1da177e4
LT
202 break;
203 case AFS_FTYPE_SYMLINK:
944c74f4 204 /* Symlinks with a mode of 0644 are actually mountpoints. */
a58823ac 205 if ((status->mode & 0777) == 0644) {
944c74f4
DH
206 inode->i_flags |= S_AUTOMOUNT;
207
944c74f4 208 set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
944c74f4
DH
209
210 inode->i_mode = S_IFDIR | 0555;
211 inode->i_op = &afs_mntpt_inode_operations;
212 inode->i_fop = &afs_mntpt_file_operations;
213 } else {
a58823ac 214 inode->i_mode = S_IFLNK | status->mode;
d3e3b7ea 215 inode->i_op = &afs_symlink_inode_operations;
944c74f4 216 }
eae9e789 217 inode->i_mapping->a_ops = &afs_dir_aops;
21fc61c7 218 inode_nohighmem(inode);
eae9e789 219 mapping_set_release_always(inode->i_mapping);
1da177e4
LT
220 break;
221 default:
e49c7b2f 222 dump_vnode(vnode, op->file[0].vnode != vnode ? op->file[0].vnode : NULL);
a58823ac 223 write_sequnlock(&vnode->cb_lock);
7126ead9 224 return afs_protocol_error(NULL, afs_eproto_file_type);
1da177e4
LT
225 }
226
2cd42d19 227 afs_set_i_size(vnode, status->size);
bc899ee1 228 afs_set_netfs_context(vnode);
c435ee34 229
a58823ac 230 vnode->invalid_before = status->data_version;
9e705016 231 trace_afs_set_dv(vnode, status->data_version);
874c8ca1 232 inode_set_iversion_raw(&vnode->netfs.inode, status->data_version);
a58823ac 233
e49c7b2f 234 if (!vp->scb.have_cb) {
a58823ac
DH
235 /* it's a symlink we just created (the fileserver
236 * didn't give us a callback) */
9e705016 237 afs_clear_cb_promise(vnode, afs_cb_promise_set_new_symlink);
a58823ac 238 } else {
20325960 239 vnode->cb_server = op->server;
9e705016
DH
240 afs_set_cb_promise(vnode, vp->scb.callback.expires_at,
241 afs_cb_promise_set_new_inode);
a58823ac
DH
242 }
243
244 write_sequnlock(&vnode->cb_lock);
1da177e4 245 return 0;
ec26815a 246}
1da177e4 247
a58823ac
DH
248/*
249 * Update the core inode struct from a returned status record.
250 */
e49c7b2f
DH
251static void afs_apply_status(struct afs_operation *op,
252 struct afs_vnode_param *vp)
a58823ac 253{
e49c7b2f
DH
254 struct afs_file_status *status = &vp->scb.status;
255 struct afs_vnode *vnode = vp->vnode;
874c8ca1 256 struct inode *inode = &vnode->netfs.inode;
a58823ac
DH
257 struct timespec64 t;
258 umode_t mode;
100ccd18 259 bool unexpected_jump = false;
a58823ac 260 bool data_changed = false;
793fe82e 261 bool change_size = vp->set_size;
a58823ac 262
e49c7b2f
DH
263 _enter("{%llx:%llu.%u} %s",
264 vp->fid.vid, vp->fid.vnode, vp->fid.unique,
265 op->type ? op->type->name : "???");
266
a58823ac
DH
267 BUG_ON(test_bit(AFS_VNODE_UNSET, &vnode->flags));
268
269 if (status->type != vnode->status.type) {
a4e530ae
KW
270 pr_warn("Vnode %llx:%llx:%x changed type %u to %u\n",
271 vnode->fid.vid,
272 vnode->fid.vnode,
273 vnode->fid.unique,
274 status->type, vnode->status.type);
7126ead9 275 afs_protocol_error(NULL, afs_eproto_bad_status);
a58823ac
DH
276 return;
277 }
278
279 if (status->nlink != vnode->status.nlink)
da8d0755 280 set_nlink(inode, status->nlink);
a58823ac
DH
281
282 if (status->owner != vnode->status.owner)
da8d0755 283 inode->i_uid = make_kuid(&init_user_ns, status->owner);
a58823ac
DH
284
285 if (status->group != vnode->status.group)
da8d0755 286 inode->i_gid = make_kgid(&init_user_ns, status->group);
a58823ac
DH
287
288 if (status->mode != vnode->status.mode) {
da8d0755 289 mode = inode->i_mode;
a58823ac 290 mode &= ~S_IALLUGO;
6e1eb04a 291 mode |= status->mode & S_IALLUGO;
da8d0755 292 WRITE_ONCE(inode->i_mode, mode);
a58823ac
DH
293 }
294
295 t = status->mtime_client;
562ce1f7 296 inode_set_mtime_to_ts(inode, t);
da8d0755 297 if (vp->update_ctime)
b9170a28 298 inode_set_ctime_to_ts(inode, op->ctime);
a58823ac 299
9e705016
DH
300 if (vnode->status.data_version != status->data_version) {
301 trace_afs_set_dv(vnode, status->data_version);
a58823ac 302 data_changed = true;
9e705016 303 }
a58823ac
DH
304
305 vnode->status = *status;
306
e49c7b2f 307 if (vp->dv_before + vp->dv_delta != status->data_version) {
9e705016
DH
308 trace_afs_dv_mismatch(vnode, vp->dv_before, vp->dv_delta,
309 status->data_version);
310
453924de
DH
311 if (vnode->cb_ro_snapshot == atomic_read(&vnode->volume->cb_ro_snapshot) &&
312 atomic64_read(&vnode->cb_expires_at) != AFS_NO_CB_PROMISE)
f015cf1d 313 pr_warn("kAFS: vnode modified {%llx:%llu} %llx->%llx %s (op=%x)\n",
3647e42b 314 vnode->fid.vid, vnode->fid.vnode,
e49c7b2f 315 (unsigned long long)vp->dv_before + vp->dv_delta,
3647e42b 316 (unsigned long long)status->data_version,
f015cf1d
DH
317 op->type ? op->type->name : "???",
318 op->debug_id);
3647e42b 319
a58823ac 320 vnode->invalid_before = status->data_version;
9e705016
DH
321 if (vnode->status.type == AFS_FTYPE_DIR)
322 afs_invalidate_dir(vnode, afs_dir_invalid_dv_mismatch);
323 else
a58823ac 324 set_bit(AFS_VNODE_ZAP_DATA, &vnode->flags);
3f4aa981 325 change_size = true;
d7f74e9a 326 data_changed = true;
100ccd18 327 unexpected_jump = true;
a58823ac
DH
328 } else if (vnode->status.type == AFS_FTYPE_DIR) {
329 /* Expected directory change is handled elsewhere so
330 * that we can locally edit the directory and save on a
331 * download.
332 */
333 if (test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
334 data_changed = false;
3f4aa981 335 change_size = true;
a58823ac
DH
336 }
337
338 if (data_changed) {
da8d0755 339 inode_set_iversion_raw(inode, status->data_version);
3f4aa981
DH
340
341 /* Only update the size if the data version jumped. If the
342 * file is being modified locally, then we might have our own
343 * idea of what the size should be that's not the same as
344 * what's on the server.
345 */
874c8ca1 346 vnode->netfs.remote_i_size = status->size;
153a9961 347 if (change_size || status->size > i_size_read(inode)) {
3f4aa981 348 afs_set_i_size(vnode, status->size);
100ccd18
DH
349 if (unexpected_jump)
350 vnode->netfs.zero_point = status->size;
b9170a28 351 inode_set_ctime_to_ts(inode, t);
562ce1f7 352 inode_set_atime_to_ts(inode, t);
da8d0755 353 }
f28fc201
DH
354 if (op->ops == &afs_fetch_data_operation)
355 op->fetch.subreq->rreq->i_size = status->size;
a58823ac
DH
356 }
357}
358
359/*
360 * Apply a callback to a vnode.
361 */
e49c7b2f
DH
362static void afs_apply_callback(struct afs_operation *op,
363 struct afs_vnode_param *vp)
a58823ac 364{
e49c7b2f
DH
365 struct afs_callback *cb = &vp->scb.callback;
366 struct afs_vnode *vnode = vp->vnode;
a58823ac 367
20325960 368 if (!afs_cb_is_broken(vp->cb_break_before, vnode)) {
453924de
DH
369 if (op->volume->type == AFSVL_RWVOL)
370 vnode->cb_server = op->server;
9e705016 371 afs_set_cb_promise(vnode, cb->expires_at, afs_cb_promise_set_apply_cb);
a58823ac
DH
372 }
373}
374
375/*
376 * Apply the received status and callback to an inode all in the same critical
377 * section to avoid races with afs_validate().
378 */
e49c7b2f 379void afs_vnode_commit_status(struct afs_operation *op, struct afs_vnode_param *vp)
a58823ac 380{
e49c7b2f
DH
381 struct afs_vnode *vnode = vp->vnode;
382
383 _enter("");
384
a58823ac
DH
385 write_seqlock(&vnode->cb_lock);
386
e49c7b2f 387 if (vp->scb.have_error) {
b6489a49
DH
388 /* A YFS server will return this from RemoveFile2 and AFS and
389 * YFS will return this from InlineBulkStatus.
390 */
e49c7b2f 391 if (vp->scb.status.abort_code == VNOVNODE) {
a38a7558 392 set_bit(AFS_VNODE_DELETED, &vnode->flags);
874c8ca1 393 clear_nlink(&vnode->netfs.inode);
051d2525 394 __afs_break_callback(vnode, afs_cb_break_for_deleted);
b6489a49 395 op->flags &= ~AFS_OPERATION_DIR_CONFLICT;
a38a7558 396 }
b6489a49 397 } else if (vp->scb.have_status) {
22650f14
DH
398 if (vp->speculative &&
399 (test_bit(AFS_VNODE_MODIFYING, &vnode->flags) ||
400 vp->dv_before != vnode->status.data_version))
a9e5c87c
DH
401 /* Ignore the result of a speculative bulk status fetch
402 * if it splits around a modification op, thereby
403 * appearing to regress the data version.
404 */
405 goto out;
b6489a49 406 afs_apply_status(op, vp);
e49c7b2f
DH
407 if (vp->scb.have_cb)
408 afs_apply_callback(op, vp);
b6489a49 409 } else if (vp->op_unlinked && !(op->flags & AFS_OPERATION_DIR_CONFLICT)) {
874c8ca1
DH
410 drop_nlink(&vnode->netfs.inode);
411 if (vnode->netfs.inode.i_nlink == 0) {
b6489a49
DH
412 set_bit(AFS_VNODE_DELETED, &vnode->flags);
413 __afs_break_callback(vnode, afs_cb_break_for_deleted);
414 }
a38a7558 415 }
a58823ac 416
a9e5c87c 417out:
a58823ac
DH
418 write_sequnlock(&vnode->cb_lock);
419
7c295eec 420 if (vp->scb.have_status)
e49c7b2f 421 afs_cache_permit(vnode, op->key, vp->cb_break_before, &vp->scb);
a58823ac
DH
422}
423
e49c7b2f
DH
424static void afs_fetch_status_success(struct afs_operation *op)
425{
b6489a49 426 struct afs_vnode_param *vp = &op->file[op->fetch_status.which];
e49c7b2f
DH
427 struct afs_vnode *vnode = vp->vnode;
428 int ret;
429
874c8ca1 430 if (vnode->netfs.inode.i_state & I_NEW) {
e49c7b2f 431 ret = afs_inode_init_from_status(op, vp, vnode);
2de5599f 432 afs_op_set_error(op, ret);
e49c7b2f
DH
433 if (ret == 0)
434 afs_cache_permit(vnode, op->key, vp->cb_break_before, &vp->scb);
435 } else {
436 afs_vnode_commit_status(op, vp);
437 }
438}
439
b6489a49 440const struct afs_operation_ops afs_fetch_status_operation = {
e49c7b2f
DH
441 .issue_afs_rpc = afs_fs_fetch_status,
442 .issue_yfs_rpc = yfs_fs_fetch_status,
443 .success = afs_fetch_status_success,
728279a5 444 .aborted = afs_check_for_remote_deletion,
e49c7b2f
DH
445};
446
d2ddc776
DH
447/*
448 * Fetch file status from the volume.
449 */
a58823ac
DH
450int afs_fetch_status(struct afs_vnode *vnode, struct key *key, bool is_new,
451 afs_access_t *_caller_access)
d2ddc776 452{
e49c7b2f 453 struct afs_operation *op;
d2ddc776 454
3b6492df 455 _enter("%s,{%llx:%llu.%u,S=%lx}",
d2ddc776
DH
456 vnode->volume->name,
457 vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique,
458 vnode->flags);
459
e49c7b2f
DH
460 op = afs_alloc_operation(key, vnode->volume);
461 if (IS_ERR(op))
462 return PTR_ERR(op);
a58823ac 463
e49c7b2f 464 afs_op_set_vnode(op, 0, vnode);
a58823ac 465
e49c7b2f
DH
466 op->nr_files = 1;
467 op->ops = &afs_fetch_status_operation;
468 afs_begin_vnode_operation(op);
469 afs_wait_for_operation(op);
d2ddc776 470
e49c7b2f
DH
471 if (_caller_access)
472 *_caller_access = op->file[0].scb.status.caller_access;
473 return afs_put_operation(op);
d2ddc776
DH
474}
475
1da177e4 476/*
e49c7b2f 477 * ilookup() comparator
1da177e4 478 */
e49c7b2f 479int afs_ilookup5_test_by_fid(struct inode *inode, void *opaque)
1da177e4 480{
3b6492df 481 struct afs_vnode *vnode = AFS_FS_I(inode);
e49c7b2f 482 struct afs_fid *fid = opaque;
1da177e4 483
e49c7b2f
DH
484 return (fid->vnode == vnode->fid.vnode &&
485 fid->vnode_hi == vnode->fid.vnode_hi &&
486 fid->unique == vnode->fid.unique);
ec26815a 487}
1da177e4 488
bec5eb61 489/*
e49c7b2f 490 * iget5() comparator
bec5eb61 491 */
e49c7b2f 492static int afs_iget5_test(struct inode *inode, void *opaque)
bec5eb61 493{
e49c7b2f
DH
494 struct afs_vnode_param *vp = opaque;
495 //struct afs_vnode *vnode = AFS_FS_I(inode);
496
497 return afs_ilookup5_test_by_fid(inode, &vp->fid);
bec5eb61 498}
499
1da177e4
LT
500/*
501 * iget5() inode initialiser
502 */
503static int afs_iget5_set(struct inode *inode, void *opaque)
504{
e49c7b2f
DH
505 struct afs_vnode_param *vp = opaque;
506 struct afs_super_info *as = AFS_FS_S(inode->i_sb);
1da177e4
LT
507 struct afs_vnode *vnode = AFS_FS_I(inode);
508
e49c7b2f
DH
509 vnode->volume = as->volume;
510 vnode->fid = vp->fid;
1da177e4 511
3b6492df
DH
512 /* YFS supports 96-bit vnode IDs, but Linux only supports
513 * 64-bit inode numbers.
514 */
e49c7b2f
DH
515 inode->i_ino = vnode->fid.vnode;
516 inode->i_generation = vnode->fid.unique;
1da177e4 517 return 0;
ec26815a 518}
1da177e4 519
402cb8dd
DH
520/*
521 * Get a cache cookie for an inode.
522 */
523static void afs_get_inode_cache(struct afs_vnode *vnode)
524{
525#ifdef CONFIG_AFS_FSCACHE
526 struct {
523d27cd
DH
527 __be32 vnode_id;
528 __be32 unique;
529 __be32 vnode_id_ext[2]; /* Allow for a 96-bit key */
402cb8dd
DH
530 } __packed key;
531 struct afs_vnode_cache_aux aux;
532
6dd80936 533 if (vnode->status.type != AFS_FTYPE_FILE &&
eae9e789
DH
534 vnode->status.type != AFS_FTYPE_DIR &&
535 vnode->status.type != AFS_FTYPE_SYMLINK) {
874c8ca1 536 vnode->netfs.cache = NULL;
f3ddee8d
DH
537 return;
538 }
539
523d27cd
DH
540 key.vnode_id = htonl(vnode->fid.vnode);
541 key.unique = htonl(vnode->fid.unique);
542 key.vnode_id_ext[0] = htonl(vnode->fid.vnode >> 32);
543 key.vnode_id_ext[1] = htonl(vnode->fid.vnode_hi);
544 afs_set_cache_aux(vnode, &aux);
545
bc899ee1
DH
546 afs_vnode_set_cache(vnode,
547 fscache_acquire_cookie(
548 vnode->volume->cache,
549 vnode->status.type == AFS_FTYPE_FILE ?
550 0 : FSCACHE_ADV_SINGLE_CHUNK,
551 &key, sizeof(key),
552 &aux, sizeof(aux),
45f66fa0 553 i_size_read(&vnode->netfs.inode)));
402cb8dd
DH
554#endif
555}
556
1da177e4
LT
557/*
558 * inode retrieval
559 */
e49c7b2f 560struct inode *afs_iget(struct afs_operation *op, struct afs_vnode_param *vp)
1da177e4 561{
e49c7b2f 562 struct afs_vnode_param *dvp = &op->file[0];
874c8ca1 563 struct super_block *sb = dvp->vnode->netfs.inode.i_sb;
1da177e4
LT
564 struct afs_vnode *vnode;
565 struct inode *inode;
566 int ret;
567
e49c7b2f 568 _enter(",{%llx:%llu.%u},,", vp->fid.vid, vp->fid.vnode, vp->fid.unique);
1da177e4 569
e49c7b2f 570 inode = iget5_locked(sb, vp->fid.vnode, afs_iget5_test, afs_iget5_set, vp);
1da177e4
LT
571 if (!inode) {
572 _leave(" = -ENOMEM");
08e0e7c8 573 return ERR_PTR(-ENOMEM);
1da177e4
LT
574 }
575
576 vnode = AFS_FS_I(inode);
577
e49c7b2f
DH
578 _debug("GOT INODE %p { vl=%llx vn=%llx, u=%x }",
579 inode, vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
580
1da177e4
LT
581 /* deal with an existing inode */
582 if (!(inode->i_state & I_NEW)) {
08e0e7c8
DH
583 _leave(" = %p", inode);
584 return inode;
1da177e4
LT
585 }
586
e49c7b2f
DH
587 ret = afs_inode_init_from_status(op, vp, vnode);
588 if (ret < 0)
589 goto bad_inode;
260a9803 590
5800db81
DH
591 afs_get_inode_cache(vnode);
592
1da177e4 593 /* success */
260a9803 594 clear_bit(AFS_VNODE_UNSET, &vnode->flags);
1da177e4 595 unlock_new_inode(inode);
7c712458 596 _leave(" = %p", inode);
08e0e7c8 597 return inode;
1da177e4
LT
598
599 /* failure */
ec26815a 600bad_inode:
aa7fa240 601 iget_failed(inode);
1da177e4 602 _leave(" = %d [bad]", ret);
08e0e7c8 603 return ERR_PTR(ret);
ec26815a 604}
1da177e4 605
e49c7b2f
DH
606static int afs_iget5_set_root(struct inode *inode, void *opaque)
607{
608 struct afs_super_info *as = AFS_FS_S(inode->i_sb);
609 struct afs_vnode *vnode = AFS_FS_I(inode);
610
611 vnode->volume = as->volume;
655593a4 612 vnode->fid.vid = as->volume->vid;
e49c7b2f
DH
613 vnode->fid.vnode = 1;
614 vnode->fid.unique = 1;
615 inode->i_ino = 1;
616 inode->i_generation = 1;
617 return 0;
618}
619
620/*
621 * Set up the root inode for a volume. This is always vnode 1, unique 1 within
622 * the volume.
623 */
624struct inode *afs_root_iget(struct super_block *sb, struct key *key)
625{
626 struct afs_super_info *as = AFS_FS_S(sb);
627 struct afs_operation *op;
628 struct afs_vnode *vnode;
629 struct inode *inode;
630 int ret;
631
632 _enter(",{%llx},,", as->volume->vid);
633
634 inode = iget5_locked(sb, 1, NULL, afs_iget5_set_root, NULL);
635 if (!inode) {
636 _leave(" = -ENOMEM");
637 return ERR_PTR(-ENOMEM);
638 }
639
640 _debug("GOT ROOT INODE %p { vl=%llx }", inode, as->volume->vid);
641
642 BUG_ON(!(inode->i_state & I_NEW));
643
644 vnode = AFS_FS_I(inode);
655593a4 645 vnode->cb_v_check = atomic_read(&as->volume->cb_v_break);
bc899ee1 646 afs_set_netfs_context(vnode);
e49c7b2f
DH
647
648 op = afs_alloc_operation(key, as->volume);
649 if (IS_ERR(op)) {
650 ret = PTR_ERR(op);
651 goto error;
652 }
653
654 afs_op_set_vnode(op, 0, vnode);
655
656 op->nr_files = 1;
657 op->ops = &afs_fetch_status_operation;
658 ret = afs_do_sync_operation(op);
659 if (ret < 0)
660 goto error;
661
662 afs_get_inode_cache(vnode);
663
664 clear_bit(AFS_VNODE_UNSET, &vnode->flags);
665 unlock_new_inode(inode);
666 _leave(" = %p", inode);
667 return inode;
668
669error:
670 iget_failed(inode);
671 _leave(" = %d [bad]", ret);
672 return ERR_PTR(ret);
673}
674
1da177e4
LT
675/*
676 * read the attributes of an inode
677 */
b74d24f7 678int afs_getattr(struct mnt_idmap *idmap, const struct path *path,
549c7297 679 struct kstat *stat, u32 request_mask, unsigned int query_flags)
1da177e4 680{
a528d35e 681 struct inode *inode = d_inode(path->dentry);
c435ee34 682 struct afs_vnode *vnode = AFS_FS_I(inode);
2aeb8c86 683 struct key *key;
df91b9df 684 int ret, seq;
1da177e4 685
d6e43f75 686 _enter("{ ino=%lu v=%u }", inode->i_ino, inode->i_generation);
1da177e4 687
cb78d1b5
DH
688 if (vnode->volume &&
689 !(query_flags & AT_STATX_DONT_SYNC) &&
453924de 690 atomic64_read(&vnode->cb_expires_at) == AFS_NO_CB_PROMISE) {
2aeb8c86
DH
691 key = afs_request_key(vnode->volume->cell);
692 if (IS_ERR(key))
693 return PTR_ERR(key);
694 ret = afs_validate(vnode, key);
695 key_put(key);
696 if (ret < 0)
697 return ret;
698 }
699
c435ee34 700 do {
df91b9df 701 seq = read_seqbegin(&vnode->cb_lock);
0d72b928 702 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
b6489a49
DH
703 if (test_bit(AFS_VNODE_SILLY_DELETED, &vnode->flags) &&
704 stat->nlink > 0)
705 stat->nlink -= 1;
45f66fa0
DH
706
707 /* Lie about the size of directories. We maintain a locally
708 * edited copy and may make different allocation decisions on
709 * it, but we need to give userspace the server's size.
710 */
711 if (S_ISDIR(inode->i_mode))
712 stat->size = vnode->netfs.remote_i_size;
df91b9df 713 } while (read_seqretry(&vnode->cb_lock, seq));
c435ee34 714
1da177e4 715 return 0;
ec26815a 716}
1da177e4 717
bec5eb61 718/*
719 * discard an AFS inode
720 */
721int afs_drop_inode(struct inode *inode)
722{
723 _enter("");
724
725 if (test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(inode)->flags))
726 return generic_delete_inode(inode);
727 else
728 return generic_drop_inode(inode);
729}
730
1da177e4
LT
731/*
732 * clear an AFS inode
733 */
b57922d9 734void afs_evict_inode(struct inode *inode)
1da177e4 735{
c7f75ef3 736 struct afs_vnode_cache_aux aux;
6dd80936 737 struct afs_super_info *sbi = AFS_FS_S(inode->i_sb);
c7f75ef3 738 struct afs_vnode *vnode = AFS_FS_I(inode);
1da177e4 739
3b6492df 740 _enter("{%llx:%llu.%d}",
260a9803 741 vnode->fid.vid,
1da177e4 742 vnode->fid.vnode,
c435ee34 743 vnode->fid.unique);
1da177e4 744
08e0e7c8
DH
745 _debug("CLEAR INODE %p", inode);
746
747 ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
748
eae9e789
DH
749 if ((S_ISDIR(inode->i_mode) ||
750 S_ISLNK(inode->i_mode)) &&
6dd80936
DH
751 (inode->i_state & I_DIRTY) &&
752 !sbi->dyn_root) {
753 struct writeback_control wbc = {
754 .sync_mode = WB_SYNC_ALL,
755 .for_sync = true,
756 .range_end = LLONG_MAX,
757 };
758
759 afs_single_writepages(inode->i_mapping, &wbc);
760 }
761
f89ea63f 762 netfs_wait_for_outstanding_io(inode);
91b0abe3 763 truncate_inode_pages_final(&inode->i_data);
6dd80936 764 netfs_free_folioq_buffer(vnode->directory);
c7f75ef3
DH
765
766 afs_set_cache_aux(vnode, &aux);
c9c4ff12 767 netfs_clear_inode_writeback(inode, &aux);
dbd5768f 768 clear_inode(inode);
b57922d9 769
4343d008
DH
770 while (!list_empty(&vnode->wb_keys)) {
771 struct afs_wb_key *wbk = list_entry(vnode->wb_keys.next,
772 struct afs_wb_key, vnode_link);
773 list_del(&wbk->vnode_link);
774 afs_put_wb_key(wbk);
775 }
1da177e4 776
bc899ee1 777 fscache_relinquish_cookie(afs_vnode_cache(vnode),
523d27cd 778 test_bit(AFS_VNODE_DELETED, &vnode->flags));
1da177e4 779
a1b879ee 780 afs_prune_wb_keys(vnode);
fe342cf7 781 afs_put_permits(rcu_access_pointer(vnode->permit_cache));
79ddbfa5
DH
782 key_put(vnode->silly_key);
783 vnode->silly_key = NULL;
59d49076
DH
784 key_put(vnode->lock_key);
785 vnode->lock_key = NULL;
1da177e4 786 _leave("");
ec26815a 787}
31143d5d 788
e49c7b2f
DH
789static void afs_setattr_success(struct afs_operation *op)
790{
ec0fa0b6 791 struct afs_vnode_param *vp = &op->file[0];
874c8ca1 792 struct inode *inode = &vp->vnode->netfs.inode;
ec0fa0b6
DH
793 loff_t old_i_size = i_size_read(inode);
794
795 op->setattr.old_i_size = old_i_size;
796 afs_vnode_commit_status(op, vp);
797 /* inode->i_size has now been changed. */
798
799 if (op->setattr.attr->ia_valid & ATTR_SIZE) {
800 loff_t size = op->setattr.attr->ia_size;
801 if (size > old_i_size)
802 pagecache_isize_extended(inode, old_i_size, size);
803 }
804}
805
806static void afs_setattr_edit_file(struct afs_operation *op)
807{
808 struct afs_vnode_param *vp = &op->file[0];
100ccd18 809 struct afs_vnode *vnode = vp->vnode;
a74ee0e8 810 struct inode *inode = &vnode->netfs.inode;
793fe82e 811
793fe82e 812 if (op->setattr.attr->ia_valid & ATTR_SIZE) {
ec0fa0b6 813 loff_t size = op->setattr.attr->ia_size;
a74ee0e8
DH
814 loff_t old = op->setattr.old_i_size;
815
816 /* Note: inode->i_size was updated by afs_apply_status() inside
817 * the I/O and callback locks.
818 */
ec0fa0b6 819
a74ee0e8
DH
820 if (size != old) {
821 truncate_pagecache(inode, size);
100ccd18
DH
822 netfs_resize_file(&vnode->netfs, size, true);
823 fscache_resize_cookie(afs_vnode_cache(vnode), size);
824 }
793fe82e 825 }
e49c7b2f
DH
826}
827
828static const struct afs_operation_ops afs_setattr_operation = {
829 .issue_afs_rpc = afs_fs_setattr,
830 .issue_yfs_rpc = yfs_fs_setattr,
831 .success = afs_setattr_success,
ec0fa0b6 832 .edit_dir = afs_setattr_edit_file,
e49c7b2f
DH
833};
834
31143d5d
DH
835/*
836 * set the attributes of an inode
837 */
c1632a0f 838int afs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
549c7297 839 struct iattr *attr)
31143d5d 840{
0770bd41
DH
841 const unsigned int supported =
842 ATTR_SIZE | ATTR_MODE | ATTR_UID | ATTR_GID |
843 ATTR_MTIME | ATTR_MTIME_SET | ATTR_TIMES_SET | ATTR_TOUCH;
e49c7b2f 844 struct afs_operation *op;
2b0143b5 845 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
874c8ca1 846 struct inode *inode = &vnode->netfs.inode;
0770bd41 847 loff_t i_size;
793fe82e 848 int ret;
31143d5d 849
3b6492df 850 _enter("{%llx:%llu},{n=%pd},%x",
a455589f 851 vnode->fid.vid, vnode->fid.vnode, dentry,
31143d5d
DH
852 attr->ia_valid);
853
0770bd41 854 if (!(attr->ia_valid & supported)) {
31143d5d
DH
855 _leave(" = 0 [unsupported]");
856 return 0;
857 }
858
0770bd41 859 i_size = i_size_read(inode);
793fe82e 860 if (attr->ia_valid & ATTR_SIZE) {
0770bd41 861 if (!S_ISREG(inode->i_mode))
793fe82e
DH
862 return -EISDIR;
863
0770bd41 864 ret = inode_newsize_ok(inode, attr->ia_size);
793fe82e
DH
865 if (ret)
866 return ret;
867
0770bd41 868 if (attr->ia_size == i_size)
793fe82e
DH
869 attr->ia_valid &= ~ATTR_SIZE;
870 }
871
523d27cd
DH
872 fscache_use_cookie(afs_vnode_cache(vnode), true);
873
ec0fa0b6
DH
874 /* Prevent any new writebacks from starting whilst we do this. */
875 down_write(&vnode->validate_lock);
876
0770bd41
DH
877 if ((attr->ia_valid & ATTR_SIZE) && S_ISREG(inode->i_mode)) {
878 loff_t size = attr->ia_size;
879
880 /* Wait for any outstanding writes to the server to complete */
881 loff_t from = min(size, i_size);
882 loff_t to = max(size, i_size);
883 ret = filemap_fdatawait_range(inode->i_mapping, from, to);
884 if (ret < 0)
885 goto out_unlock;
886
887 /* Don't talk to the server if we're just shortening in-memory
888 * writes that haven't gone to the server yet.
889 */
890 if (!(attr->ia_valid & (supported & ~ATTR_SIZE & ~ATTR_MTIME)) &&
891 attr->ia_size < i_size &&
100ccd18
DH
892 attr->ia_size > vnode->netfs.remote_i_size) {
893 truncate_setsize(inode, attr->ia_size);
894 netfs_resize_file(&vnode->netfs, size, false);
0770bd41
DH
895 fscache_resize_cookie(afs_vnode_cache(vnode),
896 attr->ia_size);
0770bd41
DH
897 ret = 0;
898 goto out_unlock;
899 }
900 }
901
e49c7b2f
DH
902 op = afs_alloc_operation(((attr->ia_valid & ATTR_FILE) ?
903 afs_file_key(attr->ia_file) : NULL),
904 vnode->volume);
ec0fa0b6
DH
905 if (IS_ERR(op)) {
906 ret = PTR_ERR(op);
907 goto out_unlock;
908 }
d2ddc776 909
e49c7b2f
DH
910 afs_op_set_vnode(op, 0, vnode);
911 op->setattr.attr = attr;
d2ddc776 912
793fe82e 913 if (attr->ia_valid & ATTR_SIZE) {
e49c7b2f 914 op->file[0].dv_delta = 1;
793fe82e
DH
915 op->file[0].set_size = true;
916 }
da8d0755
DH
917 op->ctime = attr->ia_ctime;
918 op->file[0].update_ctime = 1;
22650f14 919 op->file[0].modification = true;
31143d5d 920
e49c7b2f 921 op->ops = &afs_setattr_operation;
ec0fa0b6
DH
922 ret = afs_do_sync_operation(op);
923
924out_unlock:
925 up_write(&vnode->validate_lock);
523d27cd 926 fscache_unuse_cookie(afs_vnode_cache(vnode), NULL, NULL);
ec0fa0b6
DH
927 _leave(" = %d", ret);
928 return ret;
31143d5d 929}