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 void 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 46 static 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 54 const 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 62 if (!dentry) { 63 /* RCU pathwalk. */ 64 if (!test_bit(AFS_VNODE_DIR_READ, &vnode->flags) || !afs_check_validity(vnode)) 65 return ERR_PTR(-ECHILD); 66 goto good; 67 } 68 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 80 fetch: 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 86 good: 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 94 int 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 113 static const struct inode_operations afs_symlink_inode_operations = { 114 .get_link = afs_get_link, 115 .readlink = afs_readlink, 116 }; 117 118 static noinline void dump_vnode(struct afs_vnode *vnode, struct afs_vnode *parent_vnode) 119 { 120 static unsigned long once_only; 121 122 pr_warn("kAFS: AFS vnode with undefined type %u\n", vnode->status.type); 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 142 /* 143 * Set parameters for the netfs library 144 */ 145 static void afs_set_netfs_context(struct afs_vnode *vnode) 146 { 147 netfs_inode_init(&vnode->netfs, &afs_req_ops, true); 148 } 149 150 /* 151 * Initialise an inode from the vnode status. 152 */ 153 static int afs_inode_init_from_status(struct afs_operation *op, 154 struct afs_vnode_param *vp, 155 struct afs_vnode *vnode) 156 { 157 struct afs_file_status *status = &vp->scb.status; 158 struct inode *inode = AFS_VNODE_TO_I(vnode); 159 struct timespec64 t; 160 161 _enter("{%llx:%llu.%u} %s", 162 vp->fid.vid, vp->fid.vnode, vp->fid.unique, 163 op->type ? op->type->name : "???"); 164 165 _debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu", 166 status->type, 167 status->nlink, 168 (unsigned long long) status->size, 169 status->data_version, 170 status->mode); 171 172 write_seqlock(&vnode->cb_lock); 173 174 vnode->cb_v_check = op->cb_v_break; 175 vnode->status = *status; 176 177 t = status->mtime_client; 178 inode_set_ctime_to_ts(inode, t); 179 inode_set_mtime_to_ts(inode, t); 180 inode_set_atime_to_ts(inode, t); 181 inode->i_flags |= S_NOATIME; 182 inode->i_uid = make_kuid(&init_user_ns, status->owner); 183 inode->i_gid = make_kgid(&init_user_ns, status->group); 184 set_nlink(&vnode->netfs.inode, status->nlink); 185 186 switch (status->type) { 187 case AFS_FTYPE_FILE: 188 inode->i_mode = S_IFREG | (status->mode & S_IALLUGO); 189 inode->i_op = &afs_file_inode_operations; 190 inode->i_fop = &afs_file_operations; 191 inode->i_mapping->a_ops = &afs_file_aops; 192 mapping_set_large_folios(inode->i_mapping); 193 break; 194 case AFS_FTYPE_DIR: 195 inode->i_mode = S_IFDIR | (status->mode & S_IALLUGO); 196 inode->i_op = &afs_dir_inode_operations; 197 inode->i_fop = &afs_dir_file_operations; 198 inode->i_mapping->a_ops = &afs_dir_aops; 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); 202 break; 203 case AFS_FTYPE_SYMLINK: 204 /* Symlinks with a mode of 0644 are actually mountpoints. */ 205 if ((status->mode & 0777) == 0644) { 206 inode->i_flags |= S_AUTOMOUNT; 207 208 set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags); 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 { 214 inode->i_mode = S_IFLNK | status->mode; 215 inode->i_op = &afs_symlink_inode_operations; 216 } 217 inode->i_mapping->a_ops = &afs_dir_aops; 218 inode_nohighmem(inode); 219 mapping_set_release_always(inode->i_mapping); 220 break; 221 default: 222 dump_vnode(vnode, op->file[0].vnode != vnode ? op->file[0].vnode : NULL); 223 write_sequnlock(&vnode->cb_lock); 224 return afs_protocol_error(NULL, afs_eproto_file_type); 225 } 226 227 afs_set_i_size(vnode, status->size); 228 afs_set_netfs_context(vnode); 229 230 vnode->invalid_before = status->data_version; 231 trace_afs_set_dv(vnode, status->data_version); 232 inode_set_iversion_raw(&vnode->netfs.inode, status->data_version); 233 234 if (!vp->scb.have_cb) { 235 /* it's a symlink we just created (the fileserver 236 * didn't give us a callback) */ 237 afs_clear_cb_promise(vnode, afs_cb_promise_set_new_symlink); 238 } else { 239 vnode->cb_server = op->server; 240 afs_set_cb_promise(vnode, vp->scb.callback.expires_at, 241 afs_cb_promise_set_new_inode); 242 } 243 244 write_sequnlock(&vnode->cb_lock); 245 return 0; 246 } 247 248 /* 249 * Update the core inode struct from a returned status record. 250 */ 251 static void afs_apply_status(struct afs_operation *op, 252 struct afs_vnode_param *vp) 253 { 254 struct afs_file_status *status = &vp->scb.status; 255 struct afs_vnode *vnode = vp->vnode; 256 struct inode *inode = &vnode->netfs.inode; 257 struct timespec64 t; 258 umode_t mode; 259 bool unexpected_jump = false; 260 bool data_changed = false; 261 bool change_size = vp->set_size; 262 263 _enter("{%llx:%llu.%u} %s", 264 vp->fid.vid, vp->fid.vnode, vp->fid.unique, 265 op->type ? op->type->name : "???"); 266 267 BUG_ON(test_bit(AFS_VNODE_UNSET, &vnode->flags)); 268 269 if (status->type != vnode->status.type) { 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); 275 afs_protocol_error(NULL, afs_eproto_bad_status); 276 return; 277 } 278 279 if (status->nlink != vnode->status.nlink) 280 set_nlink(inode, status->nlink); 281 282 if (status->owner != vnode->status.owner) 283 inode->i_uid = make_kuid(&init_user_ns, status->owner); 284 285 if (status->group != vnode->status.group) 286 inode->i_gid = make_kgid(&init_user_ns, status->group); 287 288 if (status->mode != vnode->status.mode) { 289 mode = inode->i_mode; 290 mode &= ~S_IALLUGO; 291 mode |= status->mode & S_IALLUGO; 292 WRITE_ONCE(inode->i_mode, mode); 293 } 294 295 t = status->mtime_client; 296 inode_set_mtime_to_ts(inode, t); 297 if (vp->update_ctime) 298 inode_set_ctime_to_ts(inode, op->ctime); 299 300 if (vnode->status.data_version != status->data_version) { 301 trace_afs_set_dv(vnode, status->data_version); 302 data_changed = true; 303 } 304 305 vnode->status = *status; 306 307 if (vp->dv_before + vp->dv_delta != status->data_version) { 308 trace_afs_dv_mismatch(vnode, vp->dv_before, vp->dv_delta, 309 status->data_version); 310 311 if (vnode->cb_ro_snapshot == atomic_read(&vnode->volume->cb_ro_snapshot) && 312 atomic64_read(&vnode->cb_expires_at) != AFS_NO_CB_PROMISE) 313 pr_warn("kAFS: vnode modified {%llx:%llu} %llx->%llx %s (op=%x)\n", 314 vnode->fid.vid, vnode->fid.vnode, 315 (unsigned long long)vp->dv_before + vp->dv_delta, 316 (unsigned long long)status->data_version, 317 op->type ? op->type->name : "???", 318 op->debug_id); 319 320 vnode->invalid_before = status->data_version; 321 if (vnode->status.type == AFS_FTYPE_DIR) 322 afs_invalidate_dir(vnode, afs_dir_invalid_dv_mismatch); 323 else 324 set_bit(AFS_VNODE_ZAP_DATA, &vnode->flags); 325 change_size = true; 326 data_changed = true; 327 unexpected_jump = true; 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; 335 change_size = true; 336 } 337 338 if (data_changed) { 339 inode_set_iversion_raw(inode, status->data_version); 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 */ 346 vnode->netfs.remote_i_size = status->size; 347 if (change_size || status->size > i_size_read(inode)) { 348 afs_set_i_size(vnode, status->size); 349 if (unexpected_jump) 350 vnode->netfs.zero_point = status->size; 351 inode_set_ctime_to_ts(inode, t); 352 inode_set_atime_to_ts(inode, t); 353 } 354 if (op->ops == &afs_fetch_data_operation) 355 op->fetch.subreq->rreq->i_size = status->size; 356 } 357 } 358 359 /* 360 * Apply a callback to a vnode. 361 */ 362 static void afs_apply_callback(struct afs_operation *op, 363 struct afs_vnode_param *vp) 364 { 365 struct afs_callback *cb = &vp->scb.callback; 366 struct afs_vnode *vnode = vp->vnode; 367 368 if (!afs_cb_is_broken(vp->cb_break_before, vnode)) { 369 if (op->volume->type == AFSVL_RWVOL) 370 vnode->cb_server = op->server; 371 afs_set_cb_promise(vnode, cb->expires_at, afs_cb_promise_set_apply_cb); 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 */ 379 void afs_vnode_commit_status(struct afs_operation *op, struct afs_vnode_param *vp) 380 { 381 struct afs_vnode *vnode = vp->vnode; 382 383 _enter(""); 384 385 write_seqlock(&vnode->cb_lock); 386 387 if (vp->scb.have_error) { 388 /* A YFS server will return this from RemoveFile2 and AFS and 389 * YFS will return this from InlineBulkStatus. 390 */ 391 if (vp->scb.status.abort_code == VNOVNODE) { 392 set_bit(AFS_VNODE_DELETED, &vnode->flags); 393 clear_nlink(&vnode->netfs.inode); 394 __afs_break_callback(vnode, afs_cb_break_for_deleted); 395 op->flags &= ~AFS_OPERATION_DIR_CONFLICT; 396 } 397 } else if (vp->scb.have_status) { 398 if (vp->speculative && 399 (test_bit(AFS_VNODE_MODIFYING, &vnode->flags) || 400 vp->dv_before != vnode->status.data_version)) 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; 406 afs_apply_status(op, vp); 407 if (vp->scb.have_cb) 408 afs_apply_callback(op, vp); 409 } else if (vp->op_unlinked && !(op->flags & AFS_OPERATION_DIR_CONFLICT)) { 410 drop_nlink(&vnode->netfs.inode); 411 if (vnode->netfs.inode.i_nlink == 0) { 412 set_bit(AFS_VNODE_DELETED, &vnode->flags); 413 __afs_break_callback(vnode, afs_cb_break_for_deleted); 414 } 415 } 416 417 out: 418 write_sequnlock(&vnode->cb_lock); 419 420 if (vp->scb.have_status) 421 afs_cache_permit(vnode, op->key, vp->cb_break_before, &vp->scb); 422 } 423 424 static void afs_fetch_status_success(struct afs_operation *op) 425 { 426 struct afs_vnode_param *vp = &op->file[op->fetch_status.which]; 427 struct afs_vnode *vnode = vp->vnode; 428 int ret; 429 430 if (vnode->netfs.inode.i_state & I_NEW) { 431 ret = afs_inode_init_from_status(op, vp, vnode); 432 afs_op_set_error(op, ret); 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 440 const struct afs_operation_ops afs_fetch_status_operation = { 441 .issue_afs_rpc = afs_fs_fetch_status, 442 .issue_yfs_rpc = yfs_fs_fetch_status, 443 .success = afs_fetch_status_success, 444 .aborted = afs_check_for_remote_deletion, 445 }; 446 447 /* 448 * Fetch file status from the volume. 449 */ 450 int afs_fetch_status(struct afs_vnode *vnode, struct key *key, bool is_new, 451 afs_access_t *_caller_access) 452 { 453 struct afs_operation *op; 454 455 _enter("%s,{%llx:%llu.%u,S=%lx}", 456 vnode->volume->name, 457 vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique, 458 vnode->flags); 459 460 op = afs_alloc_operation(key, vnode->volume); 461 if (IS_ERR(op)) 462 return PTR_ERR(op); 463 464 afs_op_set_vnode(op, 0, vnode); 465 466 op->nr_files = 1; 467 op->ops = &afs_fetch_status_operation; 468 afs_begin_vnode_operation(op); 469 afs_wait_for_operation(op); 470 471 if (_caller_access) 472 *_caller_access = op->file[0].scb.status.caller_access; 473 return afs_put_operation(op); 474 } 475 476 /* 477 * ilookup() comparator 478 */ 479 int afs_ilookup5_test_by_fid(struct inode *inode, void *opaque) 480 { 481 struct afs_vnode *vnode = AFS_FS_I(inode); 482 struct afs_fid *fid = opaque; 483 484 return (fid->vnode == vnode->fid.vnode && 485 fid->vnode_hi == vnode->fid.vnode_hi && 486 fid->unique == vnode->fid.unique); 487 } 488 489 /* 490 * iget5() comparator 491 */ 492 static int afs_iget5_test(struct inode *inode, void *opaque) 493 { 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); 498 } 499 500 /* 501 * iget5() inode initialiser 502 */ 503 static int afs_iget5_set(struct inode *inode, void *opaque) 504 { 505 struct afs_vnode_param *vp = opaque; 506 struct afs_super_info *as = AFS_FS_S(inode->i_sb); 507 struct afs_vnode *vnode = AFS_FS_I(inode); 508 509 vnode->volume = as->volume; 510 vnode->fid = vp->fid; 511 512 /* YFS supports 96-bit vnode IDs, but Linux only supports 513 * 64-bit inode numbers. 514 */ 515 inode->i_ino = vnode->fid.vnode; 516 inode->i_generation = vnode->fid.unique; 517 return 0; 518 } 519 520 /* 521 * Get a cache cookie for an inode. 522 */ 523 static void afs_get_inode_cache(struct afs_vnode *vnode) 524 { 525 #ifdef CONFIG_AFS_FSCACHE 526 struct { 527 __be32 vnode_id; 528 __be32 unique; 529 __be32 vnode_id_ext[2]; /* Allow for a 96-bit key */ 530 } __packed key; 531 struct afs_vnode_cache_aux aux; 532 533 if (vnode->status.type != AFS_FTYPE_FILE && 534 vnode->status.type != AFS_FTYPE_DIR && 535 vnode->status.type != AFS_FTYPE_SYMLINK) { 536 vnode->netfs.cache = NULL; 537 return; 538 } 539 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 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), 553 i_size_read(&vnode->netfs.inode))); 554 #endif 555 } 556 557 /* 558 * inode retrieval 559 */ 560 struct inode *afs_iget(struct afs_operation *op, struct afs_vnode_param *vp) 561 { 562 struct afs_vnode_param *dvp = &op->file[0]; 563 struct super_block *sb = dvp->vnode->netfs.inode.i_sb; 564 struct afs_vnode *vnode; 565 struct inode *inode; 566 int ret; 567 568 _enter(",{%llx:%llu.%u},,", vp->fid.vid, vp->fid.vnode, vp->fid.unique); 569 570 inode = iget5_locked(sb, vp->fid.vnode, afs_iget5_test, afs_iget5_set, vp); 571 if (!inode) { 572 _leave(" = -ENOMEM"); 573 return ERR_PTR(-ENOMEM); 574 } 575 576 vnode = AFS_FS_I(inode); 577 578 _debug("GOT INODE %p { vl=%llx vn=%llx, u=%x }", 579 inode, vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique); 580 581 /* deal with an existing inode */ 582 if (!(inode->i_state & I_NEW)) { 583 _leave(" = %p", inode); 584 return inode; 585 } 586 587 ret = afs_inode_init_from_status(op, vp, vnode); 588 if (ret < 0) 589 goto bad_inode; 590 591 afs_get_inode_cache(vnode); 592 593 /* success */ 594 clear_bit(AFS_VNODE_UNSET, &vnode->flags); 595 unlock_new_inode(inode); 596 _leave(" = %p", inode); 597 return inode; 598 599 /* failure */ 600 bad_inode: 601 iget_failed(inode); 602 _leave(" = %d [bad]", ret); 603 return ERR_PTR(ret); 604 } 605 606 static 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; 612 vnode->fid.vid = as->volume->vid; 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 */ 624 struct 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); 645 vnode->cb_v_check = atomic_read(&as->volume->cb_v_break); 646 afs_set_netfs_context(vnode); 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 669 error: 670 iget_failed(inode); 671 _leave(" = %d [bad]", ret); 672 return ERR_PTR(ret); 673 } 674 675 /* 676 * read the attributes of an inode 677 */ 678 int afs_getattr(struct mnt_idmap *idmap, const struct path *path, 679 struct kstat *stat, u32 request_mask, unsigned int query_flags) 680 { 681 struct inode *inode = d_inode(path->dentry); 682 struct afs_vnode *vnode = AFS_FS_I(inode); 683 struct key *key; 684 int ret, seq; 685 686 _enter("{ ino=%lu v=%u }", inode->i_ino, inode->i_generation); 687 688 if (vnode->volume && 689 !(query_flags & AT_STATX_DONT_SYNC) && 690 atomic64_read(&vnode->cb_expires_at) == AFS_NO_CB_PROMISE) { 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 700 do { 701 seq = read_seqbegin(&vnode->cb_lock); 702 generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat); 703 if (test_bit(AFS_VNODE_SILLY_DELETED, &vnode->flags) && 704 stat->nlink > 0) 705 stat->nlink -= 1; 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; 713 } while (read_seqretry(&vnode->cb_lock, seq)); 714 715 return 0; 716 } 717 718 /* 719 * discard an AFS inode 720 */ 721 int 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 731 /* 732 * clear an AFS inode 733 */ 734 void afs_evict_inode(struct inode *inode) 735 { 736 struct afs_vnode_cache_aux aux; 737 struct afs_super_info *sbi = AFS_FS_S(inode->i_sb); 738 struct afs_vnode *vnode = AFS_FS_I(inode); 739 740 _enter("{%llx:%llu.%d}", 741 vnode->fid.vid, 742 vnode->fid.vnode, 743 vnode->fid.unique); 744 745 _debug("CLEAR INODE %p", inode); 746 747 ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode); 748 749 if ((S_ISDIR(inode->i_mode) || 750 S_ISLNK(inode->i_mode)) && 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 762 netfs_wait_for_outstanding_io(inode); 763 truncate_inode_pages_final(&inode->i_data); 764 netfs_free_folioq_buffer(vnode->directory); 765 766 afs_set_cache_aux(vnode, &aux); 767 netfs_clear_inode_writeback(inode, &aux); 768 clear_inode(inode); 769 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 } 776 777 fscache_relinquish_cookie(afs_vnode_cache(vnode), 778 test_bit(AFS_VNODE_DELETED, &vnode->flags)); 779 780 afs_prune_wb_keys(vnode); 781 afs_put_permits(rcu_access_pointer(vnode->permit_cache)); 782 key_put(vnode->silly_key); 783 vnode->silly_key = NULL; 784 key_put(vnode->lock_key); 785 vnode->lock_key = NULL; 786 _leave(""); 787 } 788 789 static void afs_setattr_success(struct afs_operation *op) 790 { 791 struct afs_vnode_param *vp = &op->file[0]; 792 struct inode *inode = &vp->vnode->netfs.inode; 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 806 static void afs_setattr_edit_file(struct afs_operation *op) 807 { 808 struct afs_vnode_param *vp = &op->file[0]; 809 struct afs_vnode *vnode = vp->vnode; 810 struct inode *inode = &vnode->netfs.inode; 811 812 if (op->setattr.attr->ia_valid & ATTR_SIZE) { 813 loff_t size = op->setattr.attr->ia_size; 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 */ 819 820 if (size != old) { 821 truncate_pagecache(inode, size); 822 netfs_resize_file(&vnode->netfs, size, true); 823 fscache_resize_cookie(afs_vnode_cache(vnode), size); 824 } 825 } 826 } 827 828 static 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, 832 .edit_dir = afs_setattr_edit_file, 833 }; 834 835 /* 836 * set the attributes of an inode 837 */ 838 int afs_setattr(struct mnt_idmap *idmap, struct dentry *dentry, 839 struct iattr *attr) 840 { 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; 844 struct afs_operation *op; 845 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); 846 struct inode *inode = &vnode->netfs.inode; 847 loff_t i_size; 848 int ret; 849 850 _enter("{%llx:%llu},{n=%pd},%x", 851 vnode->fid.vid, vnode->fid.vnode, dentry, 852 attr->ia_valid); 853 854 if (!(attr->ia_valid & supported)) { 855 _leave(" = 0 [unsupported]"); 856 return 0; 857 } 858 859 i_size = i_size_read(inode); 860 if (attr->ia_valid & ATTR_SIZE) { 861 if (!S_ISREG(inode->i_mode)) 862 return -EISDIR; 863 864 ret = inode_newsize_ok(inode, attr->ia_size); 865 if (ret) 866 return ret; 867 868 if (attr->ia_size == i_size) 869 attr->ia_valid &= ~ATTR_SIZE; 870 } 871 872 fscache_use_cookie(afs_vnode_cache(vnode), true); 873 874 /* Prevent any new writebacks from starting whilst we do this. */ 875 down_write(&vnode->validate_lock); 876 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 && 892 attr->ia_size > vnode->netfs.remote_i_size) { 893 truncate_setsize(inode, attr->ia_size); 894 netfs_resize_file(&vnode->netfs, size, false); 895 fscache_resize_cookie(afs_vnode_cache(vnode), 896 attr->ia_size); 897 ret = 0; 898 goto out_unlock; 899 } 900 } 901 902 op = afs_alloc_operation(((attr->ia_valid & ATTR_FILE) ? 903 afs_file_key(attr->ia_file) : NULL), 904 vnode->volume); 905 if (IS_ERR(op)) { 906 ret = PTR_ERR(op); 907 goto out_unlock; 908 } 909 910 afs_op_set_vnode(op, 0, vnode); 911 op->setattr.attr = attr; 912 913 if (attr->ia_valid & ATTR_SIZE) { 914 op->file[0].dv_delta = 1; 915 op->file[0].set_size = true; 916 } 917 op->ctime = attr->ia_ctime; 918 op->file[0].update_ctime = 1; 919 op->file[0].modification = true; 920 921 op->ops = &afs_setattr_operation; 922 ret = afs_do_sync_operation(op); 923 924 out_unlock: 925 up_write(&vnode->validate_lock); 926 fscache_unuse_cookie(afs_vnode_cache(vnode), NULL, NULL); 927 _leave(" = %d", ret); 928 return ret; 929 } 930