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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "internal.h" |
| 25 | |
| 26 | struct afs_iget_data { |
| 27 | struct afs_fid fid; |
| 28 | struct afs_volume *volume; /* volume on which resides */ |
| 29 | }; |
| 30 | |
David Howells | d3e3b7ea | 2017-07-06 15:50:27 +0100 | [diff] [blame^] | 31 | static const struct inode_operations afs_symlink_inode_operations = { |
| 32 | .get_link = page_get_link, |
| 33 | .listxattr = afs_listxattr, |
| 34 | }; |
| 35 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | /* |
| 37 | * map the AFS file status to the inode member variables |
| 38 | */ |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 39 | static int afs_inode_map_status(struct afs_vnode *vnode, struct key *key) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | { |
| 41 | struct inode *inode = AFS_VNODE_TO_I(vnode); |
| 42 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 43 | _debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | vnode->status.type, |
| 45 | vnode->status.nlink, |
David S. Miller | ba3e0e1 | 2007-04-26 16:06:22 -0700 | [diff] [blame] | 46 | (unsigned long long) vnode->status.size, |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 47 | vnode->status.data_version, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | vnode->status.mode); |
| 49 | |
| 50 | switch (vnode->status.type) { |
| 51 | case AFS_FTYPE_FILE: |
| 52 | inode->i_mode = S_IFREG | vnode->status.mode; |
| 53 | inode->i_op = &afs_file_inode_operations; |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 54 | inode->i_fop = &afs_file_operations; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | break; |
| 56 | case AFS_FTYPE_DIR: |
| 57 | inode->i_mode = S_IFDIR | vnode->status.mode; |
| 58 | inode->i_op = &afs_dir_inode_operations; |
| 59 | inode->i_fop = &afs_dir_file_operations; |
| 60 | break; |
| 61 | case AFS_FTYPE_SYMLINK: |
David Howells | 944c74f | 2017-03-16 16:27:45 +0000 | [diff] [blame] | 62 | /* Symlinks with a mode of 0644 are actually mountpoints. */ |
| 63 | if ((vnode->status.mode & 0777) == 0644) { |
| 64 | inode->i_flags |= S_AUTOMOUNT; |
| 65 | |
| 66 | spin_lock(&vnode->lock); |
| 67 | set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags); |
| 68 | spin_unlock(&vnode->lock); |
| 69 | |
| 70 | inode->i_mode = S_IFDIR | 0555; |
| 71 | inode->i_op = &afs_mntpt_inode_operations; |
| 72 | inode->i_fop = &afs_mntpt_file_operations; |
| 73 | } else { |
| 74 | inode->i_mode = S_IFLNK | vnode->status.mode; |
David Howells | d3e3b7ea | 2017-07-06 15:50:27 +0100 | [diff] [blame^] | 75 | inode->i_op = &afs_symlink_inode_operations; |
David Howells | 944c74f | 2017-03-16 16:27:45 +0000 | [diff] [blame] | 76 | } |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 77 | inode_nohighmem(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | break; |
| 79 | default: |
| 80 | printk("kAFS: AFS vnode with undefined type\n"); |
| 81 | return -EBADMSG; |
| 82 | } |
| 83 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 84 | #ifdef CONFIG_AFS_FSCACHE |
| 85 | if (vnode->status.size != inode->i_size) |
| 86 | fscache_attr_changed(vnode->cache); |
| 87 | #endif |
| 88 | |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 89 | set_nlink(inode, vnode->status.nlink); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | inode->i_uid = vnode->status.owner; |
Marc Dionne | 6186f07 | 2017-03-16 16:27:43 +0000 | [diff] [blame] | 91 | inode->i_gid = vnode->status.group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | inode->i_size = vnode->status.size; |
Marc Dionne | ab94f5d | 2017-03-16 16:27:47 +0000 | [diff] [blame] | 93 | inode->i_ctime.tv_sec = vnode->status.mtime_client; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | inode->i_ctime.tv_nsec = 0; |
| 95 | inode->i_atime = inode->i_mtime = inode->i_ctime; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | inode->i_blocks = 0; |
David Howells | d6e43f7 | 2011-06-14 00:45:44 +0100 | [diff] [blame] | 97 | inode->i_generation = vnode->fid.unique; |
| 98 | inode->i_version = vnode->status.data_version; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | inode->i_mapping->a_ops = &afs_fs_aops; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | return 0; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 101 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | * iget5() comparator |
| 105 | */ |
| 106 | static int afs_iget5_test(struct inode *inode, void *opaque) |
| 107 | { |
| 108 | struct afs_iget_data *data = opaque; |
| 109 | |
| 110 | return inode->i_ino == data->fid.vnode && |
David Howells | d6e43f7 | 2011-06-14 00:45:44 +0100 | [diff] [blame] | 111 | inode->i_generation == data->fid.unique; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | /* |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 115 | * iget5() comparator for inode created by autocell operations |
| 116 | * |
| 117 | * These pseudo inodes don't match anything. |
| 118 | */ |
| 119 | static int afs_iget5_autocell_test(struct inode *inode, void *opaque) |
| 120 | { |
| 121 | return 0; |
| 122 | } |
| 123 | |
| 124 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | * iget5() inode initialiser |
| 126 | */ |
| 127 | static int afs_iget5_set(struct inode *inode, void *opaque) |
| 128 | { |
| 129 | struct afs_iget_data *data = opaque; |
| 130 | struct afs_vnode *vnode = AFS_FS_I(inode); |
| 131 | |
| 132 | inode->i_ino = data->fid.vnode; |
David Howells | d6e43f7 | 2011-06-14 00:45:44 +0100 | [diff] [blame] | 133 | inode->i_generation = data->fid.unique; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | vnode->fid = data->fid; |
| 135 | vnode->volume = data->volume; |
| 136 | |
| 137 | return 0; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 138 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 139 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | /* |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 141 | * inode retrieval for autocell |
| 142 | */ |
| 143 | struct inode *afs_iget_autocell(struct inode *dir, const char *dev_name, |
| 144 | int namesz, struct key *key) |
| 145 | { |
| 146 | struct afs_iget_data data; |
| 147 | struct afs_super_info *as; |
| 148 | struct afs_vnode *vnode; |
| 149 | struct super_block *sb; |
| 150 | struct inode *inode; |
| 151 | static atomic_t afs_autocell_ino; |
| 152 | |
| 153 | _enter("{%x:%u},%*.*s,", |
| 154 | AFS_FS_I(dir)->fid.vid, AFS_FS_I(dir)->fid.vnode, |
| 155 | namesz, namesz, dev_name ?: ""); |
| 156 | |
| 157 | sb = dir->i_sb; |
| 158 | as = sb->s_fs_info; |
| 159 | data.volume = as->volume; |
| 160 | data.fid.vid = as->volume->vid; |
| 161 | data.fid.unique = 0; |
| 162 | data.fid.vnode = 0; |
| 163 | |
| 164 | inode = iget5_locked(sb, atomic_inc_return(&afs_autocell_ino), |
| 165 | afs_iget5_autocell_test, afs_iget5_set, |
| 166 | &data); |
| 167 | if (!inode) { |
| 168 | _leave(" = -ENOMEM"); |
| 169 | return ERR_PTR(-ENOMEM); |
| 170 | } |
| 171 | |
| 172 | _debug("GOT INODE %p { ino=%lu, vl=%x, vn=%x, u=%x }", |
| 173 | inode, inode->i_ino, data.fid.vid, data.fid.vnode, |
| 174 | data.fid.unique); |
| 175 | |
| 176 | vnode = AFS_FS_I(inode); |
| 177 | |
| 178 | /* there shouldn't be an existing inode */ |
| 179 | BUG_ON(!(inode->i_state & I_NEW)); |
| 180 | |
| 181 | inode->i_size = 0; |
| 182 | inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO; |
| 183 | inode->i_op = &afs_autocell_inode_operations; |
Miklos Szeredi | bfe8684 | 2011-10-28 14:13:29 +0200 | [diff] [blame] | 184 | set_nlink(inode, 2); |
Eric W. Biederman | a0a5386 | 2012-02-07 16:20:48 -0800 | [diff] [blame] | 185 | inode->i_uid = GLOBAL_ROOT_UID; |
| 186 | inode->i_gid = GLOBAL_ROOT_GID; |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 187 | inode->i_ctime.tv_sec = get_seconds(); |
| 188 | inode->i_ctime.tv_nsec = 0; |
| 189 | inode->i_atime = inode->i_mtime = inode->i_ctime; |
| 190 | inode->i_blocks = 0; |
| 191 | inode->i_version = 0; |
| 192 | inode->i_generation = 0; |
| 193 | |
| 194 | set_bit(AFS_VNODE_PSEUDODIR, &vnode->flags); |
David Howells | d18610b | 2011-01-14 19:04:05 +0000 | [diff] [blame] | 195 | set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags); |
| 196 | inode->i_flags |= S_AUTOMOUNT | S_NOATIME; |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 197 | unlock_new_inode(inode); |
| 198 | _leave(" = %p", inode); |
| 199 | return inode; |
| 200 | } |
| 201 | |
| 202 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | * inode retrieval |
| 204 | */ |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 205 | struct inode *afs_iget(struct super_block *sb, struct key *key, |
| 206 | struct afs_fid *fid, struct afs_file_status *status, |
| 207 | struct afs_callback *cb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | { |
| 209 | struct afs_iget_data data = { .fid = *fid }; |
| 210 | struct afs_super_info *as; |
| 211 | struct afs_vnode *vnode; |
| 212 | struct inode *inode; |
| 213 | int ret; |
| 214 | |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 215 | _enter(",{%x:%u.%u},,", fid->vid, fid->vnode, fid->unique); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
| 217 | as = sb->s_fs_info; |
| 218 | data.volume = as->volume; |
| 219 | |
| 220 | inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set, |
| 221 | &data); |
| 222 | if (!inode) { |
| 223 | _leave(" = -ENOMEM"); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 224 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
| 226 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 227 | _debug("GOT INODE %p { vl=%x vn=%x, u=%x }", |
| 228 | inode, fid->vid, fid->vnode, fid->unique); |
| 229 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | vnode = AFS_FS_I(inode); |
| 231 | |
| 232 | /* deal with an existing inode */ |
| 233 | if (!(inode->i_state & I_NEW)) { |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 234 | _leave(" = %p", inode); |
| 235 | return inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | } |
| 237 | |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 238 | if (!status) { |
| 239 | /* it's a remotely extant inode */ |
| 240 | set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags); |
| 241 | ret = afs_vnode_fetch_status(vnode, NULL, key); |
| 242 | if (ret < 0) |
| 243 | goto bad_inode; |
| 244 | } else { |
| 245 | /* it's an inode we just created */ |
| 246 | memcpy(&vnode->status, status, sizeof(vnode->status)); |
| 247 | |
| 248 | if (!cb) { |
| 249 | /* it's a symlink we just created (the fileserver |
| 250 | * didn't give us a callback) */ |
| 251 | vnode->cb_version = 0; |
| 252 | vnode->cb_expiry = 0; |
| 253 | vnode->cb_type = 0; |
Tina Ruchandani | 56e7143 | 2017-03-16 16:27:46 +0000 | [diff] [blame] | 254 | vnode->cb_expires = ktime_get_real_seconds(); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 255 | } else { |
| 256 | vnode->cb_version = cb->version; |
| 257 | vnode->cb_expiry = cb->expiry; |
| 258 | vnode->cb_type = cb->type; |
Tina Ruchandani | 56e7143 | 2017-03-16 16:27:46 +0000 | [diff] [blame] | 259 | vnode->cb_expires = vnode->cb_expiry + |
| 260 | ktime_get_real_seconds(); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 261 | } |
| 262 | } |
| 263 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 264 | /* set up caching before mapping the status, as map-status reads the |
| 265 | * first page of symlinks to see if they're really mountpoints */ |
| 266 | inode->i_size = vnode->status.size; |
| 267 | #ifdef CONFIG_AFS_FSCACHE |
| 268 | vnode->cache = fscache_acquire_cookie(vnode->volume->cache, |
| 269 | &afs_vnode_cache_index_def, |
David Howells | 94d30ae | 2013-09-21 00:09:31 +0100 | [diff] [blame] | 270 | vnode, true); |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 271 | #endif |
| 272 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 273 | ret = afs_inode_map_status(vnode, key); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 274 | if (ret < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 275 | goto bad_inode; |
| 276 | |
| 277 | /* success */ |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 278 | clear_bit(AFS_VNODE_UNSET, &vnode->flags); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 279 | inode->i_flags |= S_NOATIME; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | unlock_new_inode(inode); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 281 | _leave(" = %p [CB { v=%u t=%u }]", inode, vnode->cb_version, vnode->cb_type); |
| 282 | return inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | |
| 284 | /* failure */ |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 285 | bad_inode: |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 286 | #ifdef CONFIG_AFS_FSCACHE |
| 287 | fscache_relinquish_cookie(vnode->cache, 0); |
| 288 | vnode->cache = NULL; |
| 289 | #endif |
David Howells | aa7fa24 | 2008-02-07 00:15:28 -0800 | [diff] [blame] | 290 | iget_failed(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | _leave(" = %d [bad]", ret); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 292 | return ERR_PTR(ret); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 293 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | /* |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 296 | * mark the data attached to an inode as obsolete due to a write on the server |
| 297 | * - might also want to ditch all the outstanding writes and dirty pages |
| 298 | */ |
| 299 | void afs_zap_data(struct afs_vnode *vnode) |
| 300 | { |
David Howells | 0f300ca | 2007-05-10 22:22:20 -0700 | [diff] [blame] | 301 | _enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode); |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 302 | |
| 303 | /* 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] | 304 | * written back in a regular file and completely discard the pages in a |
| 305 | * directory or symlink */ |
| 306 | if (S_ISREG(vnode->vfs_inode.i_mode)) |
| 307 | invalidate_remote_inode(&vnode->vfs_inode); |
| 308 | else |
| 309 | invalidate_inode_pages2(vnode->vfs_inode.i_mapping); |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | /* |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 313 | * validate a vnode/inode |
| 314 | * - there are several things we need to check |
| 315 | * - parent dir data changes (rm, rmdir, rename, mkdir, create, link, |
| 316 | * symlink) |
| 317 | * - parent dir metadata changed (security changes) |
| 318 | * - dentry data changed (write, truncate) |
| 319 | * - dentry metadata changed (security changes) |
| 320 | */ |
| 321 | int afs_validate(struct afs_vnode *vnode, struct key *key) |
| 322 | { |
| 323 | int ret; |
| 324 | |
| 325 | _enter("{v={%x:%u} fl=%lx},%x", |
| 326 | vnode->fid.vid, vnode->fid.vnode, vnode->flags, |
| 327 | key_serial(key)); |
| 328 | |
| 329 | if (vnode->cb_promised && |
| 330 | !test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags) && |
| 331 | !test_bit(AFS_VNODE_MODIFIED, &vnode->flags) && |
| 332 | !test_bit(AFS_VNODE_ZAP_DATA, &vnode->flags)) { |
Tina Ruchandani | 56e7143 | 2017-03-16 16:27:46 +0000 | [diff] [blame] | 333 | if (vnode->cb_expires < ktime_get_real_seconds() + 10) { |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 334 | _debug("callback expired"); |
| 335 | set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags); |
| 336 | } else { |
| 337 | goto valid; |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) |
| 342 | goto valid; |
| 343 | |
| 344 | mutex_lock(&vnode->validate_lock); |
| 345 | |
| 346 | /* if the promise has expired, we need to check the server again to get |
| 347 | * a new promise - note that if the (parent) directory's metadata was |
| 348 | * changed then the security may be different and we may no longer have |
| 349 | * access */ |
| 350 | if (!vnode->cb_promised || |
| 351 | test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags)) { |
| 352 | _debug("not promised"); |
| 353 | ret = afs_vnode_fetch_status(vnode, NULL, key); |
| 354 | if (ret < 0) |
| 355 | goto error_unlock; |
| 356 | _debug("new promise [fl=%lx]", vnode->flags); |
| 357 | } |
| 358 | |
| 359 | if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) { |
| 360 | _debug("file already deleted"); |
| 361 | ret = -ESTALE; |
| 362 | goto error_unlock; |
| 363 | } |
| 364 | |
| 365 | /* if the vnode's data version number changed then its contents are |
| 366 | * different */ |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 367 | if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags)) |
| 368 | afs_zap_data(vnode); |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 369 | |
| 370 | clear_bit(AFS_VNODE_MODIFIED, &vnode->flags); |
| 371 | mutex_unlock(&vnode->validate_lock); |
| 372 | valid: |
| 373 | _leave(" = 0"); |
| 374 | return 0; |
| 375 | |
| 376 | error_unlock: |
| 377 | mutex_unlock(&vnode->validate_lock); |
| 378 | _leave(" = %d", ret); |
| 379 | return ret; |
| 380 | } |
| 381 | |
| 382 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | * read the attributes of an inode |
| 384 | */ |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 385 | int afs_getattr(const struct path *path, struct kstat *stat, |
| 386 | u32 request_mask, unsigned int query_flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 387 | { |
David Howells | a528d35 | 2017-01-31 16:46:22 +0000 | [diff] [blame] | 388 | struct inode *inode = d_inode(path->dentry); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
David Howells | d6e43f7 | 2011-06-14 00:45:44 +0100 | [diff] [blame] | 390 | _enter("{ ino=%lu v=%u }", inode->i_ino, inode->i_generation); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | generic_fillattr(inode, stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | return 0; |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 394 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | /* |
wanglei | bec5eb6 | 2010-08-11 09:38:04 +0100 | [diff] [blame] | 397 | * discard an AFS inode |
| 398 | */ |
| 399 | int afs_drop_inode(struct inode *inode) |
| 400 | { |
| 401 | _enter(""); |
| 402 | |
| 403 | if (test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(inode)->flags)) |
| 404 | return generic_delete_inode(inode); |
| 405 | else |
| 406 | return generic_drop_inode(inode); |
| 407 | } |
| 408 | |
| 409 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | * clear an AFS inode |
| 411 | */ |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 412 | void afs_evict_inode(struct inode *inode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 413 | { |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 414 | struct afs_permits *permits; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | struct afs_vnode *vnode; |
| 416 | |
| 417 | vnode = AFS_FS_I(inode); |
| 418 | |
David Howells | 416351f | 2007-05-09 02:33:45 -0700 | [diff] [blame] | 419 | _enter("{%x:%u.%d} v=%u x=%u t=%u }", |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 420 | vnode->fid.vid, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | vnode->fid.vnode, |
David Howells | 260a980 | 2007-04-26 15:59:35 -0700 | [diff] [blame] | 422 | vnode->fid.unique, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | vnode->cb_version, |
| 424 | vnode->cb_expiry, |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 425 | vnode->cb_type); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 427 | _debug("CLEAR INODE %p", inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 429 | ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode); |
| 430 | |
Johannes Weiner | 91b0abe | 2014-04-03 14:47:49 -0700 | [diff] [blame] | 431 | truncate_inode_pages_final(&inode->i_data); |
Jan Kara | dbd5768 | 2012-05-03 14:48:02 +0200 | [diff] [blame] | 432 | clear_inode(inode); |
Al Viro | b57922d | 2010-06-07 14:34:48 -0400 | [diff] [blame] | 433 | |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 434 | afs_give_up_callback(vnode); |
| 435 | |
| 436 | if (vnode->server) { |
| 437 | spin_lock(&vnode->server->fs_lock); |
| 438 | rb_erase(&vnode->server_rb, &vnode->server->fs_vnodes); |
| 439 | spin_unlock(&vnode->server->fs_lock); |
| 440 | afs_put_server(vnode->server); |
| 441 | vnode->server = NULL; |
| 442 | } |
| 443 | |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 444 | ASSERT(list_empty(&vnode->writebacks)); |
David Howells | 08e0e7c | 2007-04-26 15:55:03 -0700 | [diff] [blame] | 445 | ASSERT(!vnode->cb_promised); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | |
David Howells | 9b3f26c | 2009-04-03 16:42:41 +0100 | [diff] [blame] | 447 | #ifdef CONFIG_AFS_FSCACHE |
| 448 | fscache_relinquish_cookie(vnode->cache, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | vnode->cache = NULL; |
| 450 | #endif |
| 451 | |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 452 | mutex_lock(&vnode->permits_lock); |
| 453 | permits = vnode->permits; |
Andreea-Cristina Bernat | 1d7e4eb | 2017-03-16 16:27:45 +0000 | [diff] [blame] | 454 | RCU_INIT_POINTER(vnode->permits, NULL); |
David Howells | 00d3b7a | 2007-04-26 15:57:07 -0700 | [diff] [blame] | 455 | mutex_unlock(&vnode->permits_lock); |
| 456 | if (permits) |
| 457 | call_rcu(&permits->rcu, afs_zap_permits); |
| 458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | _leave(""); |
David Howells | ec26815 | 2007-04-26 15:49:28 -0700 | [diff] [blame] | 460 | } |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 461 | |
| 462 | /* |
| 463 | * set the attributes of an inode |
| 464 | */ |
| 465 | int afs_setattr(struct dentry *dentry, struct iattr *attr) |
| 466 | { |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 467 | struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry)); |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 468 | struct key *key; |
| 469 | int ret; |
| 470 | |
Al Viro | a455589 | 2014-10-21 20:11:25 -0400 | [diff] [blame] | 471 | _enter("{%x:%u},{n=%pd},%x", |
| 472 | vnode->fid.vid, vnode->fid.vnode, dentry, |
David Howells | 31143d5 | 2007-05-09 02:33:46 -0700 | [diff] [blame] | 473 | attr->ia_valid); |
| 474 | |
| 475 | if (!(attr->ia_valid & (ATTR_SIZE | ATTR_MODE | ATTR_UID | ATTR_GID | |
| 476 | ATTR_MTIME))) { |
| 477 | _leave(" = 0 [unsupported]"); |
| 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | /* flush any dirty data outstanding on a regular file */ |
| 482 | if (S_ISREG(vnode->vfs_inode.i_mode)) { |
| 483 | filemap_write_and_wait(vnode->vfs_inode.i_mapping); |
| 484 | afs_writeback_all(vnode); |
| 485 | } |
| 486 | |
| 487 | if (attr->ia_valid & ATTR_FILE) { |
| 488 | key = attr->ia_file->private_data; |
| 489 | } else { |
| 490 | key = afs_request_key(vnode->volume->cell); |
| 491 | if (IS_ERR(key)) { |
| 492 | ret = PTR_ERR(key); |
| 493 | goto error; |
| 494 | } |
| 495 | } |
| 496 | |
| 497 | ret = afs_vnode_setattr(vnode, key, attr); |
| 498 | if (!(attr->ia_valid & ATTR_FILE)) |
| 499 | key_put(key); |
| 500 | |
| 501 | error: |
| 502 | _leave(" = %d", ret); |
| 503 | return ret; |
| 504 | } |