Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* cnode related routines for the coda kernel code |
| 2 | (C) 1996 Peter Braam |
| 3 | */ |
| 4 | |
| 5 | #include <linux/types.h> |
| 6 | #include <linux/string.h> |
| 7 | #include <linux/time.h> |
| 8 | |
| 9 | #include <linux/coda.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <linux/coda_psdev.h> |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 11 | #include <linux/pagemap.h> |
Al Viro | 31a203d | 2011-01-12 16:36:09 -0500 | [diff] [blame] | 12 | #include "coda_linux.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
| 14 | static inline int coda_fideq(struct CodaFid *fid1, struct CodaFid *fid2) |
| 15 | { |
| 16 | return memcmp(fid1, fid2, sizeof(*fid1)) == 0; |
| 17 | } |
| 18 | |
Arjan van de Ven | 754661f | 2007-02-12 00:55:38 -0800 | [diff] [blame] | 19 | static const struct inode_operations coda_symlink_inode_operations = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | .readlink = generic_readlink, |
Al Viro | 6b25539 | 2015-11-17 10:20:54 -0500 | [diff] [blame] | 21 | .get_link = page_get_link, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | .put_link = page_put_link, |
| 23 | .setattr = coda_setattr, |
| 24 | }; |
| 25 | |
| 26 | /* cnode.c */ |
| 27 | static void coda_fill_inode(struct inode *inode, struct coda_vattr *attr) |
| 28 | { |
| 29 | coda_vattr_to_iattr(inode, attr); |
| 30 | |
| 31 | if (S_ISREG(inode->i_mode)) { |
| 32 | inode->i_op = &coda_file_inode_operations; |
| 33 | inode->i_fop = &coda_file_operations; |
| 34 | } else if (S_ISDIR(inode->i_mode)) { |
| 35 | inode->i_op = &coda_dir_inode_operations; |
| 36 | inode->i_fop = &coda_dir_operations; |
| 37 | } else if (S_ISLNK(inode->i_mode)) { |
| 38 | inode->i_op = &coda_symlink_inode_operations; |
Al Viro | 21fc61c | 2015-11-17 01:07:57 -0500 | [diff] [blame] | 39 | inode_nohighmem(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | inode->i_data.a_ops = &coda_symlink_aops; |
| 41 | inode->i_mapping = &inode->i_data; |
| 42 | } else |
| 43 | init_special_inode(inode, inode->i_mode, huge_decode_dev(attr->va_rdev)); |
| 44 | } |
| 45 | |
| 46 | static int coda_test_inode(struct inode *inode, void *data) |
| 47 | { |
| 48 | struct CodaFid *fid = (struct CodaFid *)data; |
Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 49 | struct coda_inode_info *cii = ITOC(inode); |
| 50 | return coda_fideq(&cii->c_fid, fid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | static int coda_set_inode(struct inode *inode, void *data) |
| 54 | { |
| 55 | struct CodaFid *fid = (struct CodaFid *)data; |
Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 56 | struct coda_inode_info *cii = ITOC(inode); |
| 57 | cii->c_fid = *fid; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | return 0; |
| 59 | } |
| 60 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | struct inode * coda_iget(struct super_block * sb, struct CodaFid * fid, |
| 62 | struct coda_vattr * attr) |
| 63 | { |
| 64 | struct inode *inode; |
| 65 | struct coda_inode_info *cii; |
| 66 | unsigned long hash = coda_f2i(fid); |
| 67 | |
| 68 | inode = iget5_locked(sb, hash, coda_test_inode, coda_set_inode, fid); |
| 69 | |
| 70 | if (!inode) |
| 71 | return ERR_PTR(-ENOMEM); |
| 72 | |
| 73 | if (inode->i_state & I_NEW) { |
| 74 | cii = ITOC(inode); |
| 75 | /* we still need to set i_ino for things like stat(2) */ |
| 76 | inode->i_ino = hash; |
Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 77 | /* inode is locked and unique, no need to grab cii->c_lock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | cii->c_mapcount = 0; |
| 79 | unlock_new_inode(inode); |
| 80 | } |
| 81 | |
| 82 | /* always replace the attributes, type might have changed */ |
| 83 | coda_fill_inode(inode, attr); |
| 84 | return inode; |
| 85 | } |
| 86 | |
| 87 | /* this is effectively coda_iget: |
| 88 | - get attributes (might be cached) |
| 89 | - get the inode for the fid using vfs iget |
| 90 | - link the two up if this is needed |
| 91 | - fill in the attributes |
| 92 | */ |
Al Viro | f4947fb | 2012-01-10 11:11:49 -0500 | [diff] [blame] | 93 | struct inode *coda_cnode_make(struct CodaFid *fid, struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
| 95 | struct coda_vattr attr; |
Al Viro | f4947fb | 2012-01-10 11:11:49 -0500 | [diff] [blame] | 96 | struct inode *inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | int error; |
| 98 | |
| 99 | /* We get inode numbers from Venus -- see venus source */ |
| 100 | error = venus_getattr(sb, fid, &attr); |
Al Viro | f4947fb | 2012-01-10 11:11:49 -0500 | [diff] [blame] | 101 | if (error) |
| 102 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | |
Al Viro | f4947fb | 2012-01-10 11:11:49 -0500 | [diff] [blame] | 104 | inode = coda_iget(sb, fid, &attr); |
| 105 | if (IS_ERR(inode)) |
Fabian Frederick | 6d6bd94 | 2014-06-06 14:36:20 -0700 | [diff] [blame] | 106 | pr_warn("%s: coda_iget failed\n", __func__); |
Al Viro | f4947fb | 2012-01-10 11:11:49 -0500 | [diff] [blame] | 107 | return inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | |
Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 111 | /* Although we treat Coda file identifiers as immutable, there is one |
| 112 | * special case for files created during a disconnection where they may |
| 113 | * not be globally unique. When an identifier collision is detected we |
| 114 | * first try to flush the cached inode from the kernel and finally |
| 115 | * resort to renaming/rehashing in-place. Userspace remembers both old |
| 116 | * and new values of the identifier to handle any in-flight upcalls. |
| 117 | * The real solution is to use globally unique UUIDs as identifiers, but |
| 118 | * retrofitting the existing userspace code for this is non-trivial. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 119 | void coda_replace_fid(struct inode *inode, struct CodaFid *oldfid, |
| 120 | struct CodaFid *newfid) |
| 121 | { |
Yoshihisa Abe | b5ce1d8 | 2010-10-25 02:03:44 -0400 | [diff] [blame] | 122 | struct coda_inode_info *cii = ITOC(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | unsigned long hash = coda_f2i(newfid); |
| 124 | |
Eric Sesterhenn | c5d3237 | 2006-03-24 18:42:13 +0100 | [diff] [blame] | 125 | BUG_ON(!coda_fideq(&cii->c_fid, oldfid)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | |
| 127 | /* replace fid and rehash inode */ |
| 128 | /* XXX we probably need to hold some lock here! */ |
| 129 | remove_inode_hash(inode); |
| 130 | cii->c_fid = *newfid; |
| 131 | inode->i_ino = hash; |
| 132 | __insert_inode_hash(inode, hash); |
| 133 | } |
| 134 | |
| 135 | /* convert a fid to an inode. */ |
| 136 | struct inode *coda_fid_to_inode(struct CodaFid *fid, struct super_block *sb) |
| 137 | { |
| 138 | struct inode *inode; |
| 139 | unsigned long hash = coda_f2i(fid); |
| 140 | |
| 141 | if ( !sb ) { |
Fabian Frederick | 6d6bd94 | 2014-06-06 14:36:20 -0700 | [diff] [blame] | 142 | pr_warn("%s: no sb!\n", __func__); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | return NULL; |
| 144 | } |
| 145 | |
Jan Harkes | ed31a7d | 2007-07-19 01:48:44 -0700 | [diff] [blame] | 146 | inode = ilookup5(sb, hash, coda_test_inode, fid); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | if ( !inode ) |
| 148 | return NULL; |
| 149 | |
| 150 | /* we should never see newly created inodes because we intentionally |
| 151 | * fail in the initialization callback */ |
| 152 | BUG_ON(inode->i_state & I_NEW); |
| 153 | |
| 154 | return inode; |
| 155 | } |
| 156 | |
| 157 | /* the CONTROL inode is made without asking attributes from Venus */ |
Al Viro | 0b2c4e3 | 2012-01-10 10:46:03 -0500 | [diff] [blame] | 158 | struct inode *coda_cnode_makectl(struct super_block *sb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | { |
Al Viro | 0b2c4e3 | 2012-01-10 10:46:03 -0500 | [diff] [blame] | 160 | struct inode *inode = new_inode(sb); |
| 161 | if (inode) { |
| 162 | inode->i_ino = CTL_INO; |
| 163 | inode->i_op = &coda_ioctl_inode_operations; |
| 164 | inode->i_fop = &coda_ioctl_operations; |
| 165 | inode->i_mode = 0444; |
| 166 | return inode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | } |
Al Viro | 0b2c4e3 | 2012-01-10 10:46:03 -0500 | [diff] [blame] | 168 | return ERR_PTR(-ENOMEM); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | } |
| 170 | |