2 * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
4 * This software may be freely redistributed under the terms of the
5 * GNU General Public License.
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.
11 * Authors: David Woodhouse <dwmw2@infradead.org>
12 * David Howells <dhowells@redhat.com>
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/init.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>
27 static const struct inode_operations afs_symlink_inode_operations = {
28 .get_link = page_get_link,
29 .listxattr = afs_listxattr,
32 static noinline void dump_vnode(struct afs_vnode *vnode, struct afs_vnode *parent_vnode)
34 static unsigned long once_only;
36 pr_warn("kAFS: AFS vnode with undefined type %u\n",
38 pr_warn("kAFS: A=%d m=%o s=%llx v=%llx\n",
39 vnode->status.abort_code,
42 vnode->status.data_version);
43 pr_warn("kAFS: vnode %llx:%llx:%x\n",
48 pr_warn("kAFS: dir %llx:%llx:%x\n",
49 parent_vnode->fid.vid,
50 parent_vnode->fid.vnode,
51 parent_vnode->fid.unique);
53 if (!test_and_set_bit(0, &once_only))
58 * Initialise an inode from the vnode status.
60 static int afs_inode_init_from_status(struct afs_vnode *vnode, struct key *key,
61 struct afs_cb_interest *cbi,
62 struct afs_vnode *parent_vnode,
63 struct afs_status_cb *scb)
65 struct afs_cb_interest *old_cbi = NULL;
66 struct afs_file_status *status = &scb->status;
67 struct inode *inode = AFS_VNODE_TO_I(vnode);
70 _debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu",
73 (unsigned long long) status->size,
77 write_seqlock(&vnode->cb_lock);
79 vnode->status = *status;
81 t = status->mtime_client;
85 inode->i_uid = make_kuid(&init_user_ns, status->owner);
86 inode->i_gid = make_kgid(&init_user_ns, status->group);
87 set_nlink(&vnode->vfs_inode, status->nlink);
89 switch (status->type) {
91 inode->i_mode = S_IFREG | status->mode;
92 inode->i_op = &afs_file_inode_operations;
93 inode->i_fop = &afs_file_operations;
94 inode->i_mapping->a_ops = &afs_fs_aops;
97 inode->i_mode = S_IFDIR | status->mode;
98 inode->i_op = &afs_dir_inode_operations;
99 inode->i_fop = &afs_dir_file_operations;
100 inode->i_mapping->a_ops = &afs_dir_aops;
102 case AFS_FTYPE_SYMLINK:
103 /* Symlinks with a mode of 0644 are actually mountpoints. */
104 if ((status->mode & 0777) == 0644) {
105 inode->i_flags |= S_AUTOMOUNT;
107 set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
109 inode->i_mode = S_IFDIR | 0555;
110 inode->i_op = &afs_mntpt_inode_operations;
111 inode->i_fop = &afs_mntpt_file_operations;
112 inode->i_mapping->a_ops = &afs_fs_aops;
114 inode->i_mode = S_IFLNK | status->mode;
115 inode->i_op = &afs_symlink_inode_operations;
116 inode->i_mapping->a_ops = &afs_fs_aops;
118 inode_nohighmem(inode);
121 dump_vnode(vnode, parent_vnode);
122 write_sequnlock(&vnode->cb_lock);
123 return afs_protocol_error(NULL, -EBADMSG, afs_eproto_file_type);
127 * Estimate 512 bytes blocks used, rounded up to nearest 1K
128 * for consistency with other AFS clients.
130 inode->i_blocks = ((i_size_read(inode) + 1023) >> 10) << 1;
131 i_size_write(&vnode->vfs_inode, status->size);
133 vnode->invalid_before = status->data_version;
134 inode_set_iversion_raw(&vnode->vfs_inode, status->data_version);
137 /* it's a symlink we just created (the fileserver
138 * didn't give us a callback) */
139 vnode->cb_expires_at = ktime_get_real_seconds();
141 vnode->cb_expires_at = scb->callback.expires_at;
142 old_cbi = rcu_dereference_protected(vnode->cb_interest,
143 lockdep_is_held(&vnode->cb_lock.lock));
145 rcu_assign_pointer(vnode->cb_interest, afs_get_cb_interest(cbi));
148 set_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
151 write_sequnlock(&vnode->cb_lock);
152 afs_put_cb_interest(afs_v2net(vnode), old_cbi);
157 * Update the core inode struct from a returned status record.
159 static void afs_apply_status(struct afs_fs_cursor *fc,
160 struct afs_vnode *vnode,
161 struct afs_status_cb *scb,
162 const afs_dataversion_t *expected_version)
164 struct afs_file_status *status = &scb->status;
167 bool data_changed = false;
169 BUG_ON(test_bit(AFS_VNODE_UNSET, &vnode->flags));
171 if (status->type != vnode->status.type) {
172 pr_warning("Vnode %llx:%llx:%x changed type %u to %u\n",
176 status->type, vnode->status.type);
177 afs_protocol_error(NULL, -EBADMSG, afs_eproto_bad_status);
181 if (status->nlink != vnode->status.nlink)
182 set_nlink(&vnode->vfs_inode, status->nlink);
184 if (status->owner != vnode->status.owner)
185 vnode->vfs_inode.i_uid = make_kuid(&init_user_ns, status->owner);
187 if (status->group != vnode->status.group)
188 vnode->vfs_inode.i_gid = make_kgid(&init_user_ns, status->group);
190 if (status->mode != vnode->status.mode) {
191 mode = vnode->vfs_inode.i_mode;
193 mode |= status->mode;
194 WRITE_ONCE(vnode->vfs_inode.i_mode, mode);
197 t = status->mtime_client;
198 vnode->vfs_inode.i_ctime = t;
199 vnode->vfs_inode.i_mtime = t;
200 vnode->vfs_inode.i_atime = t;
202 if (vnode->status.data_version != status->data_version)
205 vnode->status = *status;
207 if (expected_version &&
208 *expected_version != status->data_version) {
209 kdebug("vnode modified %llx on {%llx:%llu} [exp %llx] %s",
210 (unsigned long long) status->data_version,
211 vnode->fid.vid, vnode->fid.vnode,
212 (unsigned long long) *expected_version,
213 fc->type ? fc->type->name : "???");
214 vnode->invalid_before = status->data_version;
215 if (vnode->status.type == AFS_FTYPE_DIR) {
216 if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
217 afs_stat_v(vnode, n_inval);
219 set_bit(AFS_VNODE_ZAP_DATA, &vnode->flags);
221 } else if (vnode->status.type == AFS_FTYPE_DIR) {
222 /* Expected directory change is handled elsewhere so
223 * that we can locally edit the directory and save on a
226 if (test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
227 data_changed = false;
231 inode_set_iversion_raw(&vnode->vfs_inode, status->data_version);
232 i_size_write(&vnode->vfs_inode, status->size);
237 * Apply a callback to a vnode.
239 static void afs_apply_callback(struct afs_fs_cursor *fc,
240 struct afs_vnode *vnode,
241 struct afs_status_cb *scb,
242 unsigned int cb_break)
244 struct afs_cb_interest *old;
245 struct afs_callback *cb = &scb->callback;
247 if (!afs_cb_is_broken(cb_break, vnode, fc->cbi)) {
248 vnode->cb_expires_at = cb->expires_at;
249 old = rcu_dereference_protected(vnode->cb_interest,
250 lockdep_is_held(&vnode->cb_lock.lock));
251 if (old != fc->cbi) {
252 rcu_assign_pointer(vnode->cb_interest, afs_get_cb_interest(fc->cbi));
253 afs_put_cb_interest(afs_v2net(vnode), old);
255 set_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
260 * Apply the received status and callback to an inode all in the same critical
261 * section to avoid races with afs_validate().
263 void afs_vnode_commit_status(struct afs_fs_cursor *fc,
264 struct afs_vnode *vnode,
265 unsigned int cb_break,
266 const afs_dataversion_t *expected_version,
267 struct afs_status_cb *scb)
269 if (fc->ac.error != 0)
272 write_seqlock(&vnode->cb_lock);
274 afs_apply_status(fc, vnode, scb, expected_version);
276 afs_apply_callback(fc, vnode, scb, cb_break);
278 write_sequnlock(&vnode->cb_lock);
280 if (fc->ac.error == 0)
281 afs_cache_permit(vnode, fc->key, cb_break, scb);
285 * Fetch file status from the volume.
287 int afs_fetch_status(struct afs_vnode *vnode, struct key *key, bool is_new,
288 afs_access_t *_caller_access)
290 struct afs_status_cb *scb;
291 struct afs_fs_cursor fc;
294 _enter("%s,{%llx:%llu.%u,S=%lx}",
296 vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique,
299 scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
304 if (afs_begin_vnode_operation(&fc, vnode, key, true)) {
305 afs_dataversion_t data_version = vnode->status.data_version;
307 while (afs_select_fileserver(&fc)) {
308 fc.cb_break = afs_calc_vnode_cb_break(vnode);
309 afs_fs_fetch_file_status(&fc, scb, NULL);
315 ret = afs_inode_init_from_status(vnode, key, fc.cbi,
319 afs_cache_permit(vnode, key, fc.cb_break, scb);
321 afs_vnode_commit_status(&fc, vnode, fc.cb_break,
324 afs_check_for_remote_deletion(&fc, vnode);
325 ret = afs_end_vnode_operation(&fc);
328 if (ret == 0 && _caller_access)
329 *_caller_access = scb->status.caller_access;
331 _leave(" = %d", ret);
338 int afs_iget5_test(struct inode *inode, void *opaque)
340 struct afs_iget_data *data = opaque;
341 struct afs_vnode *vnode = AFS_FS_I(inode);
343 return memcmp(&vnode->fid, &data->fid, sizeof(data->fid)) == 0;
347 * iget5() comparator for inode created by autocell operations
349 * These pseudo inodes don't match anything.
351 static int afs_iget5_pseudo_dir_test(struct inode *inode, void *opaque)
357 * iget5() inode initialiser
359 static int afs_iget5_set(struct inode *inode, void *opaque)
361 struct afs_iget_data *data = opaque;
362 struct afs_vnode *vnode = AFS_FS_I(inode);
364 vnode->fid = data->fid;
365 vnode->volume = data->volume;
367 /* YFS supports 96-bit vnode IDs, but Linux only supports
368 * 64-bit inode numbers.
370 inode->i_ino = data->fid.vnode;
371 inode->i_generation = data->fid.unique;
376 * Create an inode for a dynamic root directory or an autocell dynamic
379 struct inode *afs_iget_pseudo_dir(struct super_block *sb, bool root)
381 struct afs_iget_data data;
382 struct afs_super_info *as;
383 struct afs_vnode *vnode;
385 static atomic_t afs_autocell_ino;
391 data.volume = as->volume;
392 data.fid.vid = as->volume->vid;
398 data.fid.vnode = atomic_inc_return(&afs_autocell_ino);
402 inode = iget5_locked(sb, data.fid.vnode,
403 afs_iget5_pseudo_dir_test, afs_iget5_set,
406 _leave(" = -ENOMEM");
407 return ERR_PTR(-ENOMEM);
410 _debug("GOT INODE %p { ino=%lu, vl=%llx, vn=%llx, u=%x }",
411 inode, inode->i_ino, data.fid.vid, data.fid.vnode,
414 vnode = AFS_FS_I(inode);
416 /* there shouldn't be an existing inode */
417 BUG_ON(!(inode->i_state & I_NEW));
420 inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
422 inode->i_op = &afs_dynroot_inode_operations;
423 inode->i_fop = &afs_dynroot_file_operations;
425 inode->i_op = &afs_autocell_inode_operations;
428 inode->i_uid = GLOBAL_ROOT_UID;
429 inode->i_gid = GLOBAL_ROOT_GID;
430 inode->i_ctime = inode->i_atime = inode->i_mtime = current_time(inode);
432 inode_set_iversion_raw(inode, 0);
433 inode->i_generation = 0;
435 set_bit(AFS_VNODE_PSEUDODIR, &vnode->flags);
437 set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
438 inode->i_flags |= S_AUTOMOUNT;
441 inode->i_flags |= S_NOATIME;
442 unlock_new_inode(inode);
443 _leave(" = %p", inode);
448 * Get a cache cookie for an inode.
450 static void afs_get_inode_cache(struct afs_vnode *vnode)
452 #ifdef CONFIG_AFS_FSCACHE
456 u32 vnode_id_ext[2]; /* Allow for a 96-bit key */
458 struct afs_vnode_cache_aux aux;
460 if (vnode->status.type == AFS_FTYPE_DIR) {
465 key.vnode_id = vnode->fid.vnode;
466 key.unique = vnode->fid.unique;
467 key.vnode_id_ext[0] = vnode->fid.vnode >> 32;
468 key.vnode_id_ext[1] = vnode->fid.vnode_hi;
469 aux.data_version = vnode->status.data_version;
471 vnode->cache = fscache_acquire_cookie(vnode->volume->cache,
472 &afs_vnode_cache_index_def,
475 vnode, vnode->status.size, true);
482 struct inode *afs_iget(struct super_block *sb, struct key *key,
483 struct afs_fid *fid, struct afs_status_cb *scb,
484 struct afs_cb_interest *cbi,
485 struct afs_vnode *parent_vnode)
487 struct afs_iget_data data = { .fid = *fid };
488 struct afs_super_info *as;
489 struct afs_vnode *vnode;
493 _enter(",{%llx:%llu.%u},,", fid->vid, fid->vnode, fid->unique);
496 data.volume = as->volume;
498 inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
501 _leave(" = -ENOMEM");
502 return ERR_PTR(-ENOMEM);
505 _debug("GOT INODE %p { vl=%llx vn=%llx, u=%x }",
506 inode, fid->vid, fid->vnode, fid->unique);
508 vnode = AFS_FS_I(inode);
510 /* deal with an existing inode */
511 if (!(inode->i_state & I_NEW)) {
512 _leave(" = %p", inode);
517 /* it's a remotely extant inode */
518 ret = afs_fetch_status(vnode, key, true, NULL);
522 ret = afs_inode_init_from_status(vnode, key, cbi, parent_vnode,
528 afs_get_inode_cache(vnode);
531 clear_bit(AFS_VNODE_UNSET, &vnode->flags);
532 inode->i_flags |= S_NOATIME;
533 unlock_new_inode(inode);
534 _leave(" = %p", inode);
540 _leave(" = %d [bad]", ret);
545 * mark the data attached to an inode as obsolete due to a write on the server
546 * - might also want to ditch all the outstanding writes and dirty pages
548 void afs_zap_data(struct afs_vnode *vnode)
550 _enter("{%llx:%llu}", vnode->fid.vid, vnode->fid.vnode);
552 #ifdef CONFIG_AFS_FSCACHE
553 fscache_invalidate(vnode->cache);
556 /* nuke all the non-dirty pages that aren't locked, mapped or being
557 * written back in a regular file and completely discard the pages in a
558 * directory or symlink */
559 if (S_ISREG(vnode->vfs_inode.i_mode))
560 invalidate_remote_inode(&vnode->vfs_inode);
562 invalidate_inode_pages2(vnode->vfs_inode.i_mapping);
566 * Check the validity of a vnode/inode.
568 bool afs_check_validity(struct afs_vnode *vnode)
570 struct afs_cb_interest *cbi;
571 struct afs_server *server;
572 struct afs_volume *volume = vnode->volume;
573 time64_t now = ktime_get_real_seconds();
574 bool valid, need_clear = false;
575 unsigned int cb_break, cb_s_break, cb_v_break;
579 read_seqbegin_or_lock(&vnode->cb_lock, &seq);
580 cb_v_break = READ_ONCE(volume->cb_v_break);
581 cb_break = vnode->cb_break;
583 if (test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
584 cbi = rcu_dereference(vnode->cb_interest);
585 server = rcu_dereference(cbi->server);
586 cb_s_break = READ_ONCE(server->cb_s_break);
588 if (vnode->cb_s_break != cb_s_break ||
589 vnode->cb_v_break != cb_v_break) {
590 vnode->cb_s_break = cb_s_break;
591 vnode->cb_v_break = cb_v_break;
594 } else if (test_bit(AFS_VNODE_ZAP_DATA, &vnode->flags)) {
597 } else if (vnode->cb_expires_at - 10 <= now) {
603 } else if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
606 vnode->cb_v_break = cb_v_break;
610 } while (need_seqretry(&vnode->cb_lock, seq));
612 done_seqretry(&vnode->cb_lock, seq);
615 write_seqlock(&vnode->cb_lock);
616 if (cb_break == vnode->cb_break)
617 __afs_break_callback(vnode);
618 write_sequnlock(&vnode->cb_lock);
626 * validate a vnode/inode
627 * - there are several things we need to check
628 * - parent dir data changes (rm, rmdir, rename, mkdir, create, link,
630 * - parent dir metadata changed (security changes)
631 * - dentry data changed (write, truncate)
632 * - dentry metadata changed (security changes)
634 int afs_validate(struct afs_vnode *vnode, struct key *key)
639 _enter("{v={%llx:%llu} fl=%lx},%x",
640 vnode->fid.vid, vnode->fid.vnode, vnode->flags,
644 valid = afs_check_validity(vnode);
647 if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
648 clear_nlink(&vnode->vfs_inode);
653 down_write(&vnode->validate_lock);
655 /* if the promise has expired, we need to check the server again to get
656 * a new promise - note that if the (parent) directory's metadata was
657 * changed then the security may be different and we may no longer have
659 if (!test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
660 _debug("not promised");
661 ret = afs_fetch_status(vnode, key, false, NULL);
663 if (ret == -ENOENT) {
664 set_bit(AFS_VNODE_DELETED, &vnode->flags);
669 _debug("new promise [fl=%lx]", vnode->flags);
672 if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
673 _debug("file already deleted");
678 /* if the vnode's data version number changed then its contents are
680 if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags))
682 up_write(&vnode->validate_lock);
688 up_write(&vnode->validate_lock);
689 _leave(" = %d", ret);
694 * read the attributes of an inode
696 int afs_getattr(const struct path *path, struct kstat *stat,
697 u32 request_mask, unsigned int query_flags)
699 struct inode *inode = d_inode(path->dentry);
700 struct afs_vnode *vnode = AFS_FS_I(inode);
703 _enter("{ ino=%lu v=%u }", inode->i_ino, inode->i_generation);
706 read_seqbegin_or_lock(&vnode->cb_lock, &seq);
707 generic_fillattr(inode, stat);
708 } while (need_seqretry(&vnode->cb_lock, seq));
710 done_seqretry(&vnode->cb_lock, seq);
715 * discard an AFS inode
717 int afs_drop_inode(struct inode *inode)
721 if (test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(inode)->flags))
722 return generic_delete_inode(inode);
724 return generic_drop_inode(inode);
730 void afs_evict_inode(struct inode *inode)
732 struct afs_cb_interest *cbi;
733 struct afs_vnode *vnode;
735 vnode = AFS_FS_I(inode);
737 _enter("{%llx:%llu.%d}",
742 _debug("CLEAR INODE %p", inode);
744 ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
746 truncate_inode_pages_final(&inode->i_data);
749 write_seqlock(&vnode->cb_lock);
750 cbi = rcu_dereference_protected(vnode->cb_interest,
751 lockdep_is_held(&vnode->cb_lock.lock));
753 afs_put_cb_interest(afs_i2net(inode), cbi);
754 rcu_assign_pointer(vnode->cb_interest, NULL);
756 write_sequnlock(&vnode->cb_lock);
758 while (!list_empty(&vnode->wb_keys)) {
759 struct afs_wb_key *wbk = list_entry(vnode->wb_keys.next,
760 struct afs_wb_key, vnode_link);
761 list_del(&wbk->vnode_link);
765 #ifdef CONFIG_AFS_FSCACHE
767 struct afs_vnode_cache_aux aux;
769 aux.data_version = vnode->status.data_version;
770 fscache_relinquish_cookie(vnode->cache, &aux,
771 test_bit(AFS_VNODE_DELETED, &vnode->flags));
776 afs_prune_wb_keys(vnode);
777 afs_put_permits(rcu_access_pointer(vnode->permit_cache));
778 key_put(vnode->silly_key);
779 vnode->silly_key = NULL;
780 key_put(vnode->lock_key);
781 vnode->lock_key = NULL;
786 * set the attributes of an inode
788 int afs_setattr(struct dentry *dentry, struct iattr *attr)
790 struct afs_fs_cursor fc;
791 struct afs_status_cb *scb;
792 struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
796 _enter("{%llx:%llu},{n=%pd},%x",
797 vnode->fid.vid, vnode->fid.vnode, dentry,
800 if (!(attr->ia_valid & (ATTR_SIZE | ATTR_MODE | ATTR_UID | ATTR_GID |
802 _leave(" = 0 [unsupported]");
806 scb = kzalloc(sizeof(struct afs_status_cb), GFP_KERNEL);
810 /* flush any dirty data outstanding on a regular file */
811 if (S_ISREG(vnode->vfs_inode.i_mode))
812 filemap_write_and_wait(vnode->vfs_inode.i_mapping);
814 if (attr->ia_valid & ATTR_FILE) {
815 key = afs_file_key(attr->ia_file);
817 key = afs_request_key(vnode->volume->cell);
825 if (afs_begin_vnode_operation(&fc, vnode, key, false)) {
826 afs_dataversion_t data_version = vnode->status.data_version;
828 if (attr->ia_valid & ATTR_SIZE)
831 while (afs_select_fileserver(&fc)) {
832 fc.cb_break = afs_calc_vnode_cb_break(vnode);
833 afs_fs_setattr(&fc, attr, scb);
836 afs_check_for_remote_deletion(&fc, vnode);
837 afs_vnode_commit_status(&fc, vnode, fc.cb_break,
839 ret = afs_end_vnode_operation(&fc);
842 if (!(attr->ia_valid & ATTR_FILE))
848 _leave(" = %d", ret);