inode.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2002 Red Hat, Inc. All rights reserved.
  3. *
  4. * This software may be freely redistributed under the terms of the
  5. * GNU General Public License.
  6. *
  7. * You should have received a copy of the GNU General Public License
  8. * along with this program; if not, write to the Free Software
  9. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  10. *
  11. * Authors: David Woodhouse <dwmw2@cambridge.redhat.com>
  12. * David Howells <dhowells@redhat.com>
  13. *
  14. */
  15. #include <linux/kernel.h>
  16. #include <linux/module.h>
  17. #include <linux/init.h>
  18. #include <linux/slab.h>
  19. #include <linux/fs.h>
  20. #include <linux/pagemap.h>
  21. #include "internal.h"
  22. struct afs_iget_data {
  23. struct afs_fid fid;
  24. struct afs_volume *volume; /* volume on which resides */
  25. };
  26. /*
  27. * map the AFS file status to the inode member variables
  28. */
  29. static int afs_inode_map_status(struct afs_vnode *vnode, struct key *key)
  30. {
  31. struct inode *inode = AFS_VNODE_TO_I(vnode);
  32. _debug("FS: ft=%d lk=%d sz=%Zu ver=%Lu mod=%hu",
  33. vnode->status.type,
  34. vnode->status.nlink,
  35. vnode->status.size,
  36. vnode->status.data_version,
  37. vnode->status.mode);
  38. switch (vnode->status.type) {
  39. case AFS_FTYPE_FILE:
  40. inode->i_mode = S_IFREG | vnode->status.mode;
  41. inode->i_op = &afs_file_inode_operations;
  42. inode->i_fop = &afs_file_operations;
  43. break;
  44. case AFS_FTYPE_DIR:
  45. inode->i_mode = S_IFDIR | vnode->status.mode;
  46. inode->i_op = &afs_dir_inode_operations;
  47. inode->i_fop = &afs_dir_file_operations;
  48. break;
  49. case AFS_FTYPE_SYMLINK:
  50. inode->i_mode = S_IFLNK | vnode->status.mode;
  51. inode->i_op = &page_symlink_inode_operations;
  52. break;
  53. default:
  54. printk("kAFS: AFS vnode with undefined type\n");
  55. return -EBADMSG;
  56. }
  57. inode->i_nlink = vnode->status.nlink;
  58. inode->i_uid = vnode->status.owner;
  59. inode->i_gid = 0;
  60. inode->i_size = vnode->status.size;
  61. inode->i_ctime.tv_sec = vnode->status.mtime_server;
  62. inode->i_ctime.tv_nsec = 0;
  63. inode->i_atime = inode->i_mtime = inode->i_ctime;
  64. inode->i_blocks = 0;
  65. inode->i_version = vnode->fid.unique;
  66. inode->i_mapping->a_ops = &afs_fs_aops;
  67. /* check to see whether a symbolic link is really a mountpoint */
  68. if (vnode->status.type == AFS_FTYPE_SYMLINK) {
  69. afs_mntpt_check_symlink(vnode, key);
  70. if (test_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags)) {
  71. inode->i_mode = S_IFDIR | vnode->status.mode;
  72. inode->i_op = &afs_mntpt_inode_operations;
  73. inode->i_fop = &afs_mntpt_file_operations;
  74. }
  75. }
  76. return 0;
  77. }
  78. /*
  79. * iget5() comparator
  80. */
  81. static int afs_iget5_test(struct inode *inode, void *opaque)
  82. {
  83. struct afs_iget_data *data = opaque;
  84. return inode->i_ino == data->fid.vnode &&
  85. inode->i_version == data->fid.unique;
  86. }
  87. /*
  88. * iget5() inode initialiser
  89. */
  90. static int afs_iget5_set(struct inode *inode, void *opaque)
  91. {
  92. struct afs_iget_data *data = opaque;
  93. struct afs_vnode *vnode = AFS_FS_I(inode);
  94. inode->i_ino = data->fid.vnode;
  95. inode->i_version = data->fid.unique;
  96. vnode->fid = data->fid;
  97. vnode->volume = data->volume;
  98. return 0;
  99. }
  100. /*
  101. * inode retrieval
  102. */
  103. inline struct inode *afs_iget(struct super_block *sb, struct key *key,
  104. struct afs_fid *fid)
  105. {
  106. struct afs_iget_data data = { .fid = *fid };
  107. struct afs_super_info *as;
  108. struct afs_vnode *vnode;
  109. struct inode *inode;
  110. int ret;
  111. _enter(",{%u,%u,%u},,", fid->vid, fid->vnode, fid->unique);
  112. as = sb->s_fs_info;
  113. data.volume = as->volume;
  114. inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
  115. &data);
  116. if (!inode) {
  117. _leave(" = -ENOMEM");
  118. return ERR_PTR(-ENOMEM);
  119. }
  120. _debug("GOT INODE %p { vl=%x vn=%x, u=%x }",
  121. inode, fid->vid, fid->vnode, fid->unique);
  122. vnode = AFS_FS_I(inode);
  123. /* deal with an existing inode */
  124. if (!(inode->i_state & I_NEW)) {
  125. _leave(" = %p", inode);
  126. return inode;
  127. }
  128. #ifdef AFS_CACHING_SUPPORT
  129. /* set up caching before reading the status, as fetch-status reads the
  130. * first page of symlinks to see if they're really mntpts */
  131. cachefs_acquire_cookie(vnode->volume->cache,
  132. NULL,
  133. vnode,
  134. &vnode->cache);
  135. #endif
  136. /* okay... it's a new inode */
  137. set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
  138. ret = afs_vnode_fetch_status(vnode, NULL, key);
  139. if (ret < 0)
  140. goto bad_inode;
  141. ret = afs_inode_map_status(vnode, key);
  142. if (ret < 0)
  143. goto bad_inode;
  144. /* success */
  145. inode->i_flags |= S_NOATIME;
  146. unlock_new_inode(inode);
  147. _leave(" = %p [CB { v=%u t=%u }]", inode, vnode->cb_version, vnode->cb_type);
  148. return inode;
  149. /* failure */
  150. bad_inode:
  151. make_bad_inode(inode);
  152. unlock_new_inode(inode);
  153. iput(inode);
  154. _leave(" = %d [bad]", ret);
  155. return ERR_PTR(ret);
  156. }
  157. /*
  158. * read the attributes of an inode
  159. */
  160. int afs_inode_getattr(struct vfsmount *mnt, struct dentry *dentry,
  161. struct kstat *stat)
  162. {
  163. struct inode *inode;
  164. inode = dentry->d_inode;
  165. _enter("{ ino=%lu v=%lu }", inode->i_ino, inode->i_version);
  166. generic_fillattr(inode, stat);
  167. return 0;
  168. }
  169. /*
  170. * clear an AFS inode
  171. */
  172. void afs_clear_inode(struct inode *inode)
  173. {
  174. struct afs_permits *permits;
  175. struct afs_vnode *vnode;
  176. vnode = AFS_FS_I(inode);
  177. _enter("ino=%lu { vn=%08x v=%u x=%u t=%u }",
  178. inode->i_ino,
  179. vnode->fid.vnode,
  180. vnode->cb_version,
  181. vnode->cb_expiry,
  182. vnode->cb_type);
  183. _debug("CLEAR INODE %p", inode);
  184. ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
  185. afs_give_up_callback(vnode);
  186. if (vnode->server) {
  187. spin_lock(&vnode->server->fs_lock);
  188. rb_erase(&vnode->server_rb, &vnode->server->fs_vnodes);
  189. spin_unlock(&vnode->server->fs_lock);
  190. afs_put_server(vnode->server);
  191. vnode->server = NULL;
  192. }
  193. ASSERT(!vnode->cb_promised);
  194. #ifdef AFS_CACHING_SUPPORT
  195. cachefs_relinquish_cookie(vnode->cache, 0);
  196. vnode->cache = NULL;
  197. #endif
  198. mutex_lock(&vnode->permits_lock);
  199. permits = vnode->permits;
  200. rcu_assign_pointer(vnode->permits, NULL);
  201. mutex_unlock(&vnode->permits_lock);
  202. if (permits)
  203. call_rcu(&permits->rcu, afs_zap_permits);
  204. _leave("");
  205. }