Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | * |
David Woodhouse | 44d1b98 | 2008-06-05 22:46:18 -0700 | [diff] [blame] | 11 | * Authors: David Woodhouse <dwmw2@infradead.org> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * David Howells <dhowells@redhat.com> |
| 13 | * |
| 14 | */ |
| 15 | |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/module.h> |
| 18 | #include <linux/init.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | #include <linux/fs.h> |
| 20 | #include <linux/pagemap.h> |
Alexey Dobriyan | e8edc6e | 2007-05-21 01:22:52 +0400 | [diff] [blame] | 21 | #include <linux/sched.h> |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 22 | #include <linux/mount.h> |
| 23 | #include <linux/namei.h> |
Jeff Layton | a01179e | 2017-12-11 06:35:11 -0500 | [diff] [blame] | 24 | #include <linux/iversion.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | #include "internal.h" |
David Howells | a38a755 | 2019-05-14 12:29:11 +0100 | [diff] [blame] | 26 | #include "afs_fs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | |
David Howells | d3e3b7ea | 2017-07-06 15:50:27 +0100 | [diff] [blame] | 28 | static const struct inode_operations afs_symlink_inode_operations = { |
| 29 | .get_link = page_get_link, |
| 30 | .listxattr = afs_listxattr, |
| 31 | }; |
| 32 | |
David Howells | b134d68 | 2019-04-25 14:26:52 +0100 | [diff] [blame] | 33 | static noinline void dump_vnode(struct afs_vnode *vnode, struct afs_vnode *parent_vnode) |
| 34 | { |
| 35 | static unsigned long once_only; |
| 36 | |
| 37 | pr_warn("kAFS: AFS vnode with undefined type %u\n", |
| 38 | vnode->status.type); |
| 39 | pr_warn("kAFS: A=%d m=%o s=%llx v=%llx\n", |
| 40 | vnode->status.abort_code, |
| 41 | vnode->status.mode, |
| 42 | vnode->status.size, |
| 43 | vnode->status.data_version); |
| 44 | pr_warn("kAFS: vnode %llx:%llx:%x\n", |
| 45 | vnode->fid.vid, |
| 46 | vnode->fid.vnode, |
| 47 | vnode->fid.unique); |
| 48 | if (parent_vnode) |
| 49 | pr_warn("kAFS: dir %llx:%llx:%x\n", |
| 50 | parent_vnode->fid.vid, |
| 51 | parent_vnode->fid.vnode, |
| 52 | parent_vnode->fid.unique); |
| 53 | |
| 54 | if (!test_and_set_bit(0, &once_only)) |
| 55 | dump_stack(); |
| 56 | } |
| 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | /* |
David Howells | dd9fbcb | 2018-04-06 14:17:24 +0100 | [diff] [blame] | 59 | * Initialise an inode from the vnode status. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | */ |
David Howells | b134d68 | 2019-04-25 14:26:52 +0100 | [diff] [blame] | 61 | static int afs_inode_init_from_status(struct afs_vnode *vnode, struct key *key, |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 62 | struct afs_cb_interest *cbi, |
| 63 | struct afs_vnode *parent_vnode, |
| 64 | struct afs_status_cb *scb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 66 | struct afs_cb_interest *old_cbi = NULL; |
| 67 | struct afs_file_status *status = &scb->status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | struct inode *inode = AFS_VNODE_TO_I(vnode); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 69 | struct timespec64 t; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 71 | _debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu", |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 72 | status->type, |
| 73 | status->nlink, |
| 74 | (unsigned long long) status->size, |
| 75 | status->data_version, |
| 76 | status->mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 78 | write_seqlock(&vnode->cb_lock); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 79 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 80 | vnode->status = *status; |
David Howells | dd9fbcb | 2018-04-06 14:17:24 +0100 | [diff] [blame] | 81 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 82 | t = status->mtime_client; |
| 83 | inode->i_ctime = t; |
| 84 | inode->i_mtime = t; |
| 85 | inode->i_atime = t; |
| 86 | inode->i_uid = make_kuid(&init_user_ns, status->owner); |
| 87 | inode->i_gid = make_kgid(&init_user_ns, status->group); |
| 88 | set_nlink(&vnode->vfs_inode, status->nlink); |
| 89 | |
| 90 | switch (status->type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | case AFS_FTYPE_FILE: |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 92 | inode->i_mode = S_IFREG | status->mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | inode->i_op = &afs_file_inode_operations; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 94 | inode->i_fop = &afs_file_operations; |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 95 | inode->i_mapping->a_ops = &afs_fs_aops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | break; |
| 97 | case AFS_FTYPE_DIR: |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 98 | inode->i_mode = S_IFDIR | status->mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | inode->i_op = &afs_dir_inode_operations; |
| 100 | inode->i_fop = &afs_dir_file_operations; |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 101 | inode->i_mapping->a_ops = &afs_dir_aops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | break; |
| 103 | case AFS_FTYPE_SYMLINK: |
David Howells | 944c74f | 2017-03-16 16:27:45 +0000 | [diff] [blame] | 104 | /* Symlinks with a mode of 0644 are actually mountpoints. */ |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 105 | if ((status->mode & 0777) == 0644) { |
David Howells | 944c74f | 2017-03-16 16:27:45 +0000 | [diff] [blame] | 106 | inode->i_flags |= S_AUTOMOUNT; |
| 107 | |
David Howells | 944c74f | 2017-03-16 16:27:45 +0000 | [diff] [blame] | 108 | set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags); |
David Howells | 944c74f | 2017-03-16 16:27:45 +0000 | [diff] [blame] | 109 | |
| 110 | inode->i_mode = S_IFDIR | 0555; |
| 111 | inode->i_op = &afs_mntpt_inode_operations; |
| 112 | inode->i_fop = &afs_mntpt_file_operations; |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 113 | inode->i_mapping->a_ops = &afs_fs_aops; |
David Howells | 944c74f | 2017-03-16 16:27:45 +0000 | [diff] [blame] | 114 | } else { |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 115 | inode->i_mode = S_IFLNK | status->mode; |
David Howells | d3e3b7ea | 2017-07-06 15:50:27 +0100 | [diff] [blame] | 116 | inode->i_op = &afs_symlink_inode_operations; |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 117 | inode->i_mapping->a_ops = &afs_fs_aops; |
David Howells | 944c74f | 2017-03-16 16:27:45 +0000 | [diff] [blame] | 118 | } |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 119 | inode_nohighmem(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | break; |
| 121 | default: |
David Howells | b134d68 | 2019-04-25 14:26:52 +0100 | [diff] [blame] | 122 | dump_vnode(vnode, parent_vnode); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 123 | write_sequnlock(&vnode->cb_lock); |
David Howells | 160cb95 | 2018-10-20 00:57:56 +0100 | [diff] [blame] | 124 | return afs_protocol_error(NULL, -EBADMSG, afs_eproto_file_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Marc Dionne | c0abbb5 | 2019-04-25 12:04:37 -0300 | [diff] [blame] | 127 | /* |
| 128 | * Estimate 512 bytes blocks used, rounded up to nearest 1K |
| 129 | * for consistency with other AFS clients. |
| 130 | */ |
| 131 | inode->i_blocks = ((i_size_read(inode) + 1023) >> 10) << 1; |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 132 | i_size_write(&vnode->vfs_inode, status->size); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 133 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 134 | vnode->invalid_before = status->data_version; |
| 135 | inode_set_iversion_raw(&vnode->vfs_inode, status->data_version); |
| 136 | |
| 137 | if (!scb->have_cb) { |
| 138 | /* it's a symlink we just created (the fileserver |
| 139 | * didn't give us a callback) */ |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 140 | vnode->cb_expires_at = ktime_get_real_seconds(); |
| 141 | } else { |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 142 | vnode->cb_expires_at = scb->callback.expires_at; |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 143 | old_cbi = rcu_dereference_protected(vnode->cb_interest, |
| 144 | lockdep_is_held(&vnode->cb_lock.lock)); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 145 | if (cbi != old_cbi) |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 146 | rcu_assign_pointer(vnode->cb_interest, afs_get_cb_interest(cbi)); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 147 | else |
| 148 | old_cbi = NULL; |
| 149 | set_bit(AFS_VNODE_CB_PROMISED, &vnode->flags); |
| 150 | } |
| 151 | |
| 152 | write_sequnlock(&vnode->cb_lock); |
| 153 | afs_put_cb_interest(afs_v2net(vnode), old_cbi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | return 0; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 155 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | /* |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 158 | * Update the core inode struct from a returned status record. |
| 159 | */ |
| 160 | static void afs_apply_status(struct afs_fs_cursor *fc, |
| 161 | struct afs_vnode *vnode, |
| 162 | struct afs_status_cb *scb, |
| 163 | const afs_dataversion_t *expected_version) |
| 164 | { |
| 165 | struct afs_file_status *status = &scb->status; |
| 166 | struct timespec64 t; |
| 167 | umode_t mode; |
| 168 | bool data_changed = false; |
| 169 | |
| 170 | BUG_ON(test_bit(AFS_VNODE_UNSET, &vnode->flags)); |
| 171 | |
| 172 | if (status->type != vnode->status.type) { |
| 173 | pr_warning("Vnode %llx:%llx:%x changed type %u to %u\n", |
| 174 | vnode->fid.vid, |
| 175 | vnode->fid.vnode, |
| 176 | vnode->fid.unique, |
| 177 | status->type, vnode->status.type); |
| 178 | afs_protocol_error(NULL, -EBADMSG, afs_eproto_bad_status); |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | if (status->nlink != vnode->status.nlink) |
| 183 | set_nlink(&vnode->vfs_inode, status->nlink); |
| 184 | |
| 185 | if (status->owner != vnode->status.owner) |
| 186 | vnode->vfs_inode.i_uid = make_kuid(&init_user_ns, status->owner); |
| 187 | |
| 188 | if (status->group != vnode->status.group) |
| 189 | vnode->vfs_inode.i_gid = make_kgid(&init_user_ns, status->group); |
| 190 | |
| 191 | if (status->mode != vnode->status.mode) { |
| 192 | mode = vnode->vfs_inode.i_mode; |
| 193 | mode &= ~S_IALLUGO; |
| 194 | mode |= status->mode; |
| 195 | WRITE_ONCE(vnode->vfs_inode.i_mode, mode); |
| 196 | } |
| 197 | |
| 198 | t = status->mtime_client; |
| 199 | vnode->vfs_inode.i_ctime = t; |
| 200 | vnode->vfs_inode.i_mtime = t; |
| 201 | vnode->vfs_inode.i_atime = t; |
| 202 | |
| 203 | if (vnode->status.data_version != status->data_version) |
| 204 | data_changed = true; |
| 205 | |
| 206 | vnode->status = *status; |
| 207 | |
| 208 | if (expected_version && |
| 209 | *expected_version != status->data_version) { |
David Howells | 3647e42 | 2019-06-19 10:56:03 +0100 | [diff] [blame^] | 210 | if (test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) |
| 211 | pr_warn("kAFS: vnode modified {%llx:%llu} %llx->%llx %s\n", |
| 212 | vnode->fid.vid, vnode->fid.vnode, |
| 213 | (unsigned long long)*expected_version, |
| 214 | (unsigned long long)status->data_version, |
| 215 | fc->type ? fc->type->name : "???"); |
| 216 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 217 | vnode->invalid_before = status->data_version; |
| 218 | if (vnode->status.type == AFS_FTYPE_DIR) { |
| 219 | if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags)) |
| 220 | afs_stat_v(vnode, n_inval); |
| 221 | } else { |
| 222 | set_bit(AFS_VNODE_ZAP_DATA, &vnode->flags); |
| 223 | } |
| 224 | } else if (vnode->status.type == AFS_FTYPE_DIR) { |
| 225 | /* Expected directory change is handled elsewhere so |
| 226 | * that we can locally edit the directory and save on a |
| 227 | * download. |
| 228 | */ |
| 229 | if (test_bit(AFS_VNODE_DIR_VALID, &vnode->flags)) |
| 230 | data_changed = false; |
| 231 | } |
| 232 | |
| 233 | if (data_changed) { |
| 234 | inode_set_iversion_raw(&vnode->vfs_inode, status->data_version); |
| 235 | i_size_write(&vnode->vfs_inode, status->size); |
| 236 | } |
| 237 | } |
| 238 | |
| 239 | /* |
| 240 | * Apply a callback to a vnode. |
| 241 | */ |
| 242 | static void afs_apply_callback(struct afs_fs_cursor *fc, |
| 243 | struct afs_vnode *vnode, |
| 244 | struct afs_status_cb *scb, |
| 245 | unsigned int cb_break) |
| 246 | { |
| 247 | struct afs_cb_interest *old; |
| 248 | struct afs_callback *cb = &scb->callback; |
| 249 | |
| 250 | if (!afs_cb_is_broken(cb_break, vnode, fc->cbi)) { |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 251 | vnode->cb_expires_at = cb->expires_at; |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 252 | old = rcu_dereference_protected(vnode->cb_interest, |
| 253 | lockdep_is_held(&vnode->cb_lock.lock)); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 254 | if (old != fc->cbi) { |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 255 | rcu_assign_pointer(vnode->cb_interest, afs_get_cb_interest(fc->cbi)); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 256 | afs_put_cb_interest(afs_v2net(vnode), old); |
| 257 | } |
| 258 | set_bit(AFS_VNODE_CB_PROMISED, &vnode->flags); |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Apply the received status and callback to an inode all in the same critical |
| 264 | * section to avoid races with afs_validate(). |
| 265 | */ |
| 266 | void afs_vnode_commit_status(struct afs_fs_cursor *fc, |
| 267 | struct afs_vnode *vnode, |
| 268 | unsigned int cb_break, |
| 269 | const afs_dataversion_t *expected_version, |
| 270 | struct afs_status_cb *scb) |
| 271 | { |
| 272 | if (fc->ac.error != 0) |
| 273 | return; |
| 274 | |
| 275 | write_seqlock(&vnode->cb_lock); |
| 276 | |
David Howells | a38a755 | 2019-05-14 12:29:11 +0100 | [diff] [blame] | 277 | if (scb->have_error) { |
| 278 | if (scb->status.abort_code == VNOVNODE) { |
| 279 | set_bit(AFS_VNODE_DELETED, &vnode->flags); |
| 280 | clear_nlink(&vnode->vfs_inode); |
| 281 | __afs_break_callback(vnode); |
| 282 | } |
| 283 | } else { |
| 284 | if (scb->have_status) |
| 285 | afs_apply_status(fc, vnode, scb, expected_version); |
| 286 | if (scb->have_cb) |
| 287 | afs_apply_callback(fc, vnode, scb, cb_break); |
| 288 | } |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 289 | |
| 290 | write_sequnlock(&vnode->cb_lock); |
| 291 | |
David Howells | a38a755 | 2019-05-14 12:29:11 +0100 | [diff] [blame] | 292 | if (fc->ac.error == 0 && scb->have_status) |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 293 | afs_cache_permit(vnode, fc->key, cb_break, scb); |
| 294 | } |
| 295 | |
| 296 | /* |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 297 | * Fetch file status from the volume. |
| 298 | */ |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 299 | int afs_fetch_status(struct afs_vnode *vnode, struct key *key, bool is_new, |
| 300 | afs_access_t *_caller_access) |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 301 | { |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 302 | struct afs_status_cb *scb; |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 303 | struct afs_fs_cursor fc; |
| 304 | int ret; |
| 305 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 306 | _enter("%s,{%llx:%llu.%u,S=%lx}", |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 307 | vnode->volume->name, |
| 308 | vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique, |
| 309 | vnode->flags); |
| 310 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 311 | scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL); |
| 312 | if (!scb) |
| 313 | return -ENOMEM; |
| 314 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 315 | ret = -ERESTARTSYS; |
David Howells | 20b8391 | 2019-05-08 16:16:31 +0100 | [diff] [blame] | 316 | if (afs_begin_vnode_operation(&fc, vnode, key, true)) { |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 317 | afs_dataversion_t data_version = vnode->status.data_version; |
| 318 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 319 | while (afs_select_fileserver(&fc)) { |
David Howells | 68251f0 | 2018-05-12 22:31:33 +0100 | [diff] [blame] | 320 | fc.cb_break = afs_calc_vnode_cb_break(vnode); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 321 | afs_fs_fetch_file_status(&fc, scb, NULL); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 322 | } |
| 323 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 324 | if (fc.error) { |
| 325 | /* Do nothing. */ |
| 326 | } else if (is_new) { |
| 327 | ret = afs_inode_init_from_status(vnode, key, fc.cbi, |
| 328 | NULL, scb); |
| 329 | fc.error = ret; |
| 330 | if (ret == 0) |
| 331 | afs_cache_permit(vnode, key, fc.cb_break, scb); |
| 332 | } else { |
| 333 | afs_vnode_commit_status(&fc, vnode, fc.cb_break, |
| 334 | &data_version, scb); |
| 335 | } |
| 336 | afs_check_for_remote_deletion(&fc, vnode); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 337 | ret = afs_end_vnode_operation(&fc); |
| 338 | } |
| 339 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 340 | if (ret == 0 && _caller_access) |
| 341 | *_caller_access = scb->status.caller_access; |
| 342 | kfree(scb); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 343 | _leave(" = %d", ret); |
| 344 | return ret; |
| 345 | } |
| 346 | |
| 347 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | * iget5() comparator |
| 349 | */ |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 350 | int afs_iget5_test(struct inode *inode, void *opaque) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | { |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 352 | struct afs_iget_data *iget_data = opaque; |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 353 | struct afs_vnode *vnode = AFS_FS_I(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 355 | return memcmp(&vnode->fid, &iget_data->fid, sizeof(iget_data->fid)) == 0; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 356 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | /* |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 359 | * iget5() comparator for inode created by autocell operations |
| 360 | * |
| 361 | * These pseudo inodes don't match anything. |
| 362 | */ |
David Howells | 4d673da | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 363 | static int afs_iget5_pseudo_dir_test(struct inode *inode, void *opaque) |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 364 | { |
| 365 | return 0; |
| 366 | } |
| 367 | |
| 368 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | * iget5() inode initialiser |
| 370 | */ |
| 371 | static int afs_iget5_set(struct inode *inode, void *opaque) |
| 372 | { |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 373 | struct afs_iget_data *iget_data = opaque; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | struct afs_vnode *vnode = AFS_FS_I(inode); |
| 375 | |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 376 | vnode->fid = iget_data->fid; |
| 377 | vnode->volume = iget_data->volume; |
| 378 | vnode->cb_v_break = iget_data->cb_v_break; |
| 379 | vnode->cb_s_break = iget_data->cb_s_break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 381 | /* YFS supports 96-bit vnode IDs, but Linux only supports |
| 382 | * 64-bit inode numbers. |
| 383 | */ |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 384 | inode->i_ino = iget_data->fid.vnode; |
| 385 | inode->i_generation = iget_data->fid.unique; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | return 0; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 387 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | /* |
David Howells | 4d673da | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 390 | * Create an inode for a dynamic root directory or an autocell dynamic |
| 391 | * automount dir. |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 392 | */ |
David Howells | 4d673da | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 393 | struct inode *afs_iget_pseudo_dir(struct super_block *sb, bool root) |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 394 | { |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 395 | struct afs_super_info *as; |
| 396 | struct afs_vnode *vnode; |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 397 | struct inode *inode; |
| 398 | static atomic_t afs_autocell_ino; |
| 399 | |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 400 | struct afs_iget_data iget_data = { |
| 401 | .cb_v_break = 0, |
| 402 | .cb_s_break = 0, |
| 403 | }; |
| 404 | |
David Howells | 4d673da | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 405 | _enter(""); |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 406 | |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 407 | as = sb->s_fs_info; |
David Howells | 4d673da | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 408 | if (as->volume) { |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 409 | iget_data.volume = as->volume; |
| 410 | iget_data.fid.vid = as->volume->vid; |
David Howells | 4d673da | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 411 | } |
| 412 | if (root) { |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 413 | iget_data.fid.vnode = 1; |
| 414 | iget_data.fid.unique = 1; |
David Howells | 4d673da | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 415 | } else { |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 416 | iget_data.fid.vnode = atomic_inc_return(&afs_autocell_ino); |
| 417 | iget_data.fid.unique = 0; |
David Howells | 4d673da | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 418 | } |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 419 | |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 420 | inode = iget5_locked(sb, iget_data.fid.vnode, |
David Howells | 4d673da | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 421 | afs_iget5_pseudo_dir_test, afs_iget5_set, |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 422 | &iget_data); |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 423 | if (!inode) { |
| 424 | _leave(" = -ENOMEM"); |
| 425 | return ERR_PTR(-ENOMEM); |
| 426 | } |
| 427 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 428 | _debug("GOT INODE %p { ino=%lu, vl=%llx, vn=%llx, u=%x }", |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 429 | inode, inode->i_ino, iget_data.fid.vid, iget_data.fid.vnode, |
| 430 | iget_data.fid.unique); |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 431 | |
| 432 | vnode = AFS_FS_I(inode); |
| 433 | |
| 434 | /* there shouldn't be an existing inode */ |
| 435 | BUG_ON(!(inode->i_state & I_NEW)); |
| 436 | |
| 437 | inode->i_size = 0; |
| 438 | inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO; |
David Howells | 4d673da | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 439 | if (root) { |
| 440 | inode->i_op = &afs_dynroot_inode_operations; |
| 441 | inode->i_fop = &afs_dynroot_file_operations; |
| 442 | } else { |
| 443 | inode->i_op = &afs_autocell_inode_operations; |
| 444 | } |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 445 | set_nlink(inode, 2); |
Eric W. Biederman | a0a5386 | 2012-02-07 16:20:48 -0800 | [diff] [blame] | 446 | inode->i_uid = GLOBAL_ROOT_UID; |
| 447 | inode->i_gid = GLOBAL_ROOT_GID; |
Arnd Bergmann | ba25b81 | 2019-04-13 08:37:36 +0100 | [diff] [blame] | 448 | inode->i_ctime = inode->i_atime = inode->i_mtime = current_time(inode); |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 449 | inode->i_blocks = 0; |
Jeff Layton | a01179e | 2017-12-11 06:35:11 -0500 | [diff] [blame] | 450 | inode_set_iversion_raw(inode, 0); |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 451 | inode->i_generation = 0; |
| 452 | |
| 453 | set_bit(AFS_VNODE_PSEUDODIR, &vnode->flags); |
David Howells | 4d673da | 2018-02-06 06:26:30 +0000 | [diff] [blame] | 454 | if (!root) { |
| 455 | set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags); |
| 456 | inode->i_flags |= S_AUTOMOUNT; |
| 457 | } |
| 458 | |
| 459 | inode->i_flags |= S_NOATIME; |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 460 | unlock_new_inode(inode); |
| 461 | _leave(" = %p", inode); |
| 462 | return inode; |
| 463 | } |
| 464 | |
| 465 | /* |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 466 | * Get a cache cookie for an inode. |
| 467 | */ |
| 468 | static void afs_get_inode_cache(struct afs_vnode *vnode) |
| 469 | { |
| 470 | #ifdef CONFIG_AFS_FSCACHE |
| 471 | struct { |
| 472 | u32 vnode_id; |
| 473 | u32 unique; |
| 474 | u32 vnode_id_ext[2]; /* Allow for a 96-bit key */ |
| 475 | } __packed key; |
| 476 | struct afs_vnode_cache_aux aux; |
| 477 | |
David Howells | f3ddee8 | 2018-04-06 14:17:25 +0100 | [diff] [blame] | 478 | if (vnode->status.type == AFS_FTYPE_DIR) { |
| 479 | vnode->cache = NULL; |
| 480 | return; |
| 481 | } |
| 482 | |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 483 | key.vnode_id = vnode->fid.vnode; |
| 484 | key.unique = vnode->fid.unique; |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 485 | key.vnode_id_ext[0] = vnode->fid.vnode >> 32; |
| 486 | key.vnode_id_ext[1] = vnode->fid.vnode_hi; |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 487 | aux.data_version = vnode->status.data_version; |
| 488 | |
| 489 | vnode->cache = fscache_acquire_cookie(vnode->volume->cache, |
| 490 | &afs_vnode_cache_index_def, |
| 491 | &key, sizeof(key), |
| 492 | &aux, sizeof(aux), |
David Howells | ee1235a | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 493 | vnode, vnode->status.size, true); |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 494 | #endif |
| 495 | } |
| 496 | |
| 497 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | * inode retrieval |
| 499 | */ |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 500 | struct inode *afs_iget(struct super_block *sb, struct key *key, |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 501 | struct afs_iget_data *iget_data, |
| 502 | struct afs_status_cb *scb, |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 503 | struct afs_cb_interest *cbi, |
David Howells | b134d68 | 2019-04-25 14:26:52 +0100 | [diff] [blame] | 504 | struct afs_vnode *parent_vnode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | struct afs_super_info *as; |
| 507 | struct afs_vnode *vnode; |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 508 | struct afs_fid *fid = &iget_data->fid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | struct inode *inode; |
| 510 | int ret; |
| 511 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 512 | _enter(",{%llx:%llu.%u},,", fid->vid, fid->vnode, fid->unique); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
| 514 | as = sb->s_fs_info; |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 515 | iget_data->volume = as->volume; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | |
| 517 | inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set, |
David Howells | b835915 | 2019-05-14 12:23:43 +0100 | [diff] [blame] | 518 | iget_data); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | if (!inode) { |
| 520 | _leave(" = -ENOMEM"); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 521 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | } |
| 523 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 524 | _debug("GOT INODE %p { vl=%llx vn=%llx, u=%x }", |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 525 | inode, fid->vid, fid->vnode, fid->unique); |
| 526 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | vnode = AFS_FS_I(inode); |
| 528 | |
| 529 | /* deal with an existing inode */ |
| 530 | if (!(inode->i_state & I_NEW)) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 531 | _leave(" = %p", inode); |
| 532 | return inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | } |
| 534 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 535 | if (!scb) { |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 536 | /* it's a remotely extant inode */ |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 537 | ret = afs_fetch_status(vnode, key, true, NULL); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 538 | if (ret < 0) |
| 539 | goto bad_inode; |
| 540 | } else { |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 541 | ret = afs_inode_init_from_status(vnode, key, cbi, parent_vnode, |
| 542 | scb); |
| 543 | if (ret < 0) |
| 544 | goto bad_inode; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 545 | } |
| 546 | |
David Howells | 5800db8 | 2018-04-06 14:17:24 +0100 | [diff] [blame] | 547 | afs_get_inode_cache(vnode); |
| 548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | /* success */ |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 550 | clear_bit(AFS_VNODE_UNSET, &vnode->flags); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 551 | inode->i_flags |= S_NOATIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 552 | unlock_new_inode(inode); |
David Howells | 7c71245 | 2019-05-14 15:35:44 +0100 | [diff] [blame] | 553 | _leave(" = %p", inode); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 554 | return inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | |
| 556 | /* failure */ |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 557 | bad_inode: |
David Howells | aa7fa24 | 2008-02-07 00:15:28 -0800 | [diff] [blame] | 558 | iget_failed(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | _leave(" = %d [bad]", ret); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 560 | return ERR_PTR(ret); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 561 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | /* |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 564 | * mark the data attached to an inode as obsolete due to a write on the server |
| 565 | * - might also want to ditch all the outstanding writes and dirty pages |
| 566 | */ |
| 567 | void afs_zap_data(struct afs_vnode *vnode) |
| 568 | { |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 569 | _enter("{%llx:%llu}", vnode->fid.vid, vnode->fid.vnode); |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 570 | |
David Howells | c151599 | 2018-04-04 13:41:25 +0100 | [diff] [blame] | 571 | #ifdef CONFIG_AFS_FSCACHE |
| 572 | fscache_invalidate(vnode->cache); |
| 573 | #endif |
| 574 | |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 575 | /* nuke all the non-dirty pages that aren't locked, mapped or being |
David Howells | 0f300ca | 2007-05-10 22:22:20 -0700 | [diff] [blame] | 576 | * written back in a regular file and completely discard the pages in a |
| 577 | * directory or symlink */ |
| 578 | if (S_ISREG(vnode->vfs_inode.i_mode)) |
| 579 | invalidate_remote_inode(&vnode->vfs_inode); |
| 580 | else |
| 581 | invalidate_inode_pages2(vnode->vfs_inode.i_mapping); |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 582 | } |
| 583 | |
| 584 | /* |
David Howells | c925bd0 | 2019-05-09 14:31:11 +0100 | [diff] [blame] | 585 | * Check the validity of a vnode/inode. |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 586 | */ |
David Howells | c925bd0 | 2019-05-09 14:31:11 +0100 | [diff] [blame] | 587 | bool afs_check_validity(struct afs_vnode *vnode) |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 588 | { |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 589 | struct afs_cb_interest *cbi; |
| 590 | struct afs_server *server; |
| 591 | struct afs_volume *volume = vnode->volume; |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 592 | time64_t now = ktime_get_real_seconds(); |
David Howells | 61c347b | 2019-05-09 14:15:11 +0100 | [diff] [blame] | 593 | bool valid, need_clear = false; |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 594 | unsigned int cb_break, cb_s_break, cb_v_break; |
| 595 | int seq = 0; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 596 | |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 597 | do { |
| 598 | read_seqbegin_or_lock(&vnode->cb_lock, &seq); |
| 599 | cb_v_break = READ_ONCE(volume->cb_v_break); |
| 600 | cb_break = vnode->cb_break; |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 601 | |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 602 | if (test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) { |
| 603 | cbi = rcu_dereference(vnode->cb_interest); |
| 604 | server = rcu_dereference(cbi->server); |
| 605 | cb_s_break = READ_ONCE(server->cb_s_break); |
| 606 | |
| 607 | if (vnode->cb_s_break != cb_s_break || |
| 608 | vnode->cb_v_break != cb_v_break) { |
| 609 | vnode->cb_s_break = cb_s_break; |
| 610 | vnode->cb_v_break = cb_v_break; |
David Howells | 61c347b | 2019-05-09 14:15:11 +0100 | [diff] [blame] | 611 | need_clear = true; |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 612 | valid = false; |
| 613 | } else if (test_bit(AFS_VNODE_ZAP_DATA, &vnode->flags)) { |
David Howells | 61c347b | 2019-05-09 14:15:11 +0100 | [diff] [blame] | 614 | need_clear = true; |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 615 | valid = false; |
| 616 | } else if (vnode->cb_expires_at - 10 <= now) { |
David Howells | 61c347b | 2019-05-09 14:15:11 +0100 | [diff] [blame] | 617 | need_clear = true; |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 618 | valid = false; |
| 619 | } else { |
| 620 | valid = true; |
| 621 | } |
| 622 | } else if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) { |
David Howells | 68251f0 | 2018-05-12 22:31:33 +0100 | [diff] [blame] | 623 | valid = true; |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 624 | } else { |
| 625 | vnode->cb_v_break = cb_v_break; |
| 626 | valid = false; |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 627 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 628 | |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 629 | } while (need_seqretry(&vnode->cb_lock, seq)); |
| 630 | |
| 631 | done_seqretry(&vnode->cb_lock, seq); |
David Howells | 61c347b | 2019-05-09 14:15:11 +0100 | [diff] [blame] | 632 | |
| 633 | if (need_clear) { |
| 634 | write_seqlock(&vnode->cb_lock); |
| 635 | if (cb_break == vnode->cb_break) |
| 636 | __afs_break_callback(vnode); |
| 637 | write_sequnlock(&vnode->cb_lock); |
| 638 | valid = false; |
| 639 | } |
| 640 | |
David Howells | c925bd0 | 2019-05-09 14:31:11 +0100 | [diff] [blame] | 641 | return valid; |
| 642 | } |
| 643 | |
| 644 | /* |
| 645 | * validate a vnode/inode |
| 646 | * - there are several things we need to check |
| 647 | * - parent dir data changes (rm, rmdir, rename, mkdir, create, link, |
| 648 | * symlink) |
| 649 | * - parent dir metadata changed (security changes) |
| 650 | * - dentry data changed (write, truncate) |
| 651 | * - dentry metadata changed (security changes) |
| 652 | */ |
| 653 | int afs_validate(struct afs_vnode *vnode, struct key *key) |
| 654 | { |
| 655 | bool valid; |
| 656 | int ret; |
| 657 | |
| 658 | _enter("{v={%llx:%llu} fl=%lx},%x", |
| 659 | vnode->fid.vid, vnode->fid.vnode, vnode->flags, |
| 660 | key_serial(key)); |
| 661 | |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 662 | rcu_read_lock(); |
David Howells | c925bd0 | 2019-05-09 14:31:11 +0100 | [diff] [blame] | 663 | valid = afs_check_validity(vnode); |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 664 | rcu_read_unlock(); |
David Howells | 440fbc3 | 2018-01-02 10:02:19 +0000 | [diff] [blame] | 665 | |
| 666 | if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) |
| 667 | clear_nlink(&vnode->vfs_inode); |
| 668 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 669 | if (valid) |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 670 | goto valid; |
| 671 | |
David Howells | b61f7dc | 2018-04-27 20:46:22 +0100 | [diff] [blame] | 672 | down_write(&vnode->validate_lock); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 673 | |
| 674 | /* if the promise has expired, we need to check the server again to get |
| 675 | * a new promise - note that if the (parent) directory's metadata was |
| 676 | * changed then the security may be different and we may no longer have |
| 677 | * access */ |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 678 | if (!test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) { |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 679 | _debug("not promised"); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 680 | ret = afs_fetch_status(vnode, key, false, NULL); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 681 | if (ret < 0) { |
| 682 | if (ret == -ENOENT) { |
| 683 | set_bit(AFS_VNODE_DELETED, &vnode->flags); |
| 684 | ret = -ESTALE; |
| 685 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 686 | goto error_unlock; |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 687 | } |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 688 | _debug("new promise [fl=%lx]", vnode->flags); |
| 689 | } |
| 690 | |
| 691 | if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) { |
| 692 | _debug("file already deleted"); |
| 693 | ret = -ESTALE; |
| 694 | goto error_unlock; |
| 695 | } |
| 696 | |
| 697 | /* if the vnode's data version number changed then its contents are |
| 698 | * different */ |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 699 | if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags)) |
| 700 | afs_zap_data(vnode); |
David Howells | b61f7dc | 2018-04-27 20:46:22 +0100 | [diff] [blame] | 701 | up_write(&vnode->validate_lock); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 702 | valid: |
| 703 | _leave(" = 0"); |
| 704 | return 0; |
| 705 | |
| 706 | error_unlock: |
David Howells | b61f7dc | 2018-04-27 20:46:22 +0100 | [diff] [blame] | 707 | up_write(&vnode->validate_lock); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 708 | _leave(" = %d", ret); |
| 709 | return ret; |
| 710 | } |
| 711 | |
| 712 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | * read the attributes of an inode |
| 714 | */ |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 715 | int afs_getattr(const struct path *path, struct kstat *stat, |
| 716 | u32 request_mask, unsigned int query_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 718 | struct inode *inode = d_inode(path->dentry); |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 719 | struct afs_vnode *vnode = AFS_FS_I(inode); |
| 720 | int seq = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | |
David Howells | d6e43f7 | 2011-06-14 00:45:44 +0100 | [diff] [blame] | 722 | _enter("{ ino=%lu v=%u }", inode->i_ino, inode->i_generation); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 723 | |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 724 | do { |
| 725 | read_seqbegin_or_lock(&vnode->cb_lock, &seq); |
| 726 | generic_fillattr(inode, stat); |
| 727 | } while (need_seqretry(&vnode->cb_lock, seq)); |
| 728 | |
| 729 | done_seqretry(&vnode->cb_lock, seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 730 | return 0; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 731 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 732 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | /* |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 734 | * discard an AFS inode |
| 735 | */ |
| 736 | int afs_drop_inode(struct inode *inode) |
| 737 | { |
| 738 | _enter(""); |
| 739 | |
| 740 | if (test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(inode)->flags)) |
| 741 | return generic_delete_inode(inode); |
| 742 | else |
| 743 | return generic_drop_inode(inode); |
| 744 | } |
| 745 | |
| 746 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | * clear an AFS inode |
| 748 | */ |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 749 | void afs_evict_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 750 | { |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 751 | struct afs_cb_interest *cbi; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | struct afs_vnode *vnode; |
| 753 | |
| 754 | vnode = AFS_FS_I(inode); |
| 755 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 756 | _enter("{%llx:%llu.%d}", |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 757 | vnode->fid.vid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | vnode->fid.vnode, |
David Howells | c435ee3 | 2017-11-02 15:27:49 +0000 | [diff] [blame] | 759 | vnode->fid.unique); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 761 | _debug("CLEAR INODE %p", inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 763 | ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode); |
| 764 | |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 765 | truncate_inode_pages_final(&inode->i_data); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 766 | clear_inode(inode); |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 767 | |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 768 | write_seqlock(&vnode->cb_lock); |
| 769 | cbi = rcu_dereference_protected(vnode->cb_interest, |
| 770 | lockdep_is_held(&vnode->cb_lock.lock)); |
| 771 | if (cbi) { |
| 772 | afs_put_cb_interest(afs_i2net(inode), cbi); |
| 773 | rcu_assign_pointer(vnode->cb_interest, NULL); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 774 | } |
David Howells | f642404 | 2019-05-13 16:14:32 +0100 | [diff] [blame] | 775 | write_sequnlock(&vnode->cb_lock); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 776 | |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 777 | while (!list_empty(&vnode->wb_keys)) { |
| 778 | struct afs_wb_key *wbk = list_entry(vnode->wb_keys.next, |
| 779 | struct afs_wb_key, vnode_link); |
| 780 | list_del(&wbk->vnode_link); |
| 781 | afs_put_wb_key(wbk); |
| 782 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 784 | #ifdef CONFIG_AFS_FSCACHE |
David Howells | 402cb8d | 2018-04-04 13:41:28 +0100 | [diff] [blame] | 785 | { |
| 786 | struct afs_vnode_cache_aux aux; |
| 787 | |
| 788 | aux.data_version = vnode->status.data_version; |
| 789 | fscache_relinquish_cookie(vnode->cache, &aux, |
| 790 | test_bit(AFS_VNODE_DELETED, &vnode->flags)); |
| 791 | vnode->cache = NULL; |
| 792 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 793 | #endif |
| 794 | |
David Howells | a1b879e | 2019-05-15 12:09:17 +0100 | [diff] [blame] | 795 | afs_prune_wb_keys(vnode); |
David Howells | fe342cf | 2018-04-09 21:12:31 +0100 | [diff] [blame] | 796 | afs_put_permits(rcu_access_pointer(vnode->permit_cache)); |
David Howells | 79ddbfa | 2019-04-25 14:26:51 +0100 | [diff] [blame] | 797 | key_put(vnode->silly_key); |
| 798 | vnode->silly_key = NULL; |
David Howells | 59d4907 | 2019-01-09 17:23:54 +0000 | [diff] [blame] | 799 | key_put(vnode->lock_key); |
| 800 | vnode->lock_key = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 801 | _leave(""); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 802 | } |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 803 | |
| 804 | /* |
| 805 | * set the attributes of an inode |
| 806 | */ |
| 807 | int afs_setattr(struct dentry *dentry, struct iattr *attr) |
| 808 | { |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 809 | struct afs_fs_cursor fc; |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 810 | struct afs_status_cb *scb; |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 811 | struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 812 | struct key *key; |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 813 | int ret = -ENOMEM; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 814 | |
David Howells | 3b6492d | 2018-10-20 00:57:57 +0100 | [diff] [blame] | 815 | _enter("{%llx:%llu},{n=%pd},%x", |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 816 | vnode->fid.vid, vnode->fid.vnode, dentry, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 817 | attr->ia_valid); |
| 818 | |
| 819 | if (!(attr->ia_valid & (ATTR_SIZE | ATTR_MODE | ATTR_UID | ATTR_GID | |
| 820 | ATTR_MTIME))) { |
| 821 | _leave(" = 0 [unsupported]"); |
| 822 | return 0; |
| 823 | } |
| 824 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 825 | scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL); |
| 826 | if (!scb) |
| 827 | goto error; |
| 828 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 829 | /* flush any dirty data outstanding on a regular file */ |
David Howells | 4343d00 | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 830 | if (S_ISREG(vnode->vfs_inode.i_mode)) |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 831 | filemap_write_and_wait(vnode->vfs_inode.i_mapping); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 832 | |
| 833 | if (attr->ia_valid & ATTR_FILE) { |
David Howells | 215804a | 2017-11-02 15:27:52 +0000 | [diff] [blame] | 834 | key = afs_file_key(attr->ia_file); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 835 | } else { |
| 836 | key = afs_request_key(vnode->volume->cell); |
| 837 | if (IS_ERR(key)) { |
| 838 | ret = PTR_ERR(key); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 839 | goto error_scb; |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 843 | ret = -ERESTARTSYS; |
David Howells | 20b8391 | 2019-05-08 16:16:31 +0100 | [diff] [blame] | 844 | if (afs_begin_vnode_operation(&fc, vnode, key, false)) { |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 845 | afs_dataversion_t data_version = vnode->status.data_version; |
| 846 | |
| 847 | if (attr->ia_valid & ATTR_SIZE) |
| 848 | data_version++; |
| 849 | |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 850 | while (afs_select_fileserver(&fc)) { |
David Howells | 68251f0 | 2018-05-12 22:31:33 +0100 | [diff] [blame] | 851 | fc.cb_break = afs_calc_vnode_cb_break(vnode); |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 852 | afs_fs_setattr(&fc, attr, scb); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 853 | } |
| 854 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 855 | afs_check_for_remote_deletion(&fc, vnode); |
| 856 | afs_vnode_commit_status(&fc, vnode, fc.cb_break, |
| 857 | &data_version, scb); |
David Howells | d2ddc77 | 2017-11-02 15:27:50 +0000 | [diff] [blame] | 858 | ret = afs_end_vnode_operation(&fc); |
| 859 | } |
| 860 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 861 | if (!(attr->ia_valid & ATTR_FILE)) |
| 862 | key_put(key); |
| 863 | |
David Howells | a58823a | 2019-05-09 15:16:10 +0100 | [diff] [blame] | 864 | error_scb: |
| 865 | kfree(scb); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 866 | error: |
| 867 | _leave(" = %d", ret); |
| 868 | return ret; |
| 869 | } |