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