inode.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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 "volume.h"
  22. #include "vnode.h"
  23. #include "super.h"
  24. #include "internal.h"
  25. struct afs_iget_data {
  26. struct afs_fid fid;
  27. struct afs_volume *volume; /* volume on which resides */
  28. };
  29. /*****************************************************************************/
  30. /*
  31. * map the AFS file status to the inode member variables
  32. */
  33. static int afs_inode_map_status(struct afs_vnode *vnode)
  34. {
  35. struct inode *inode = AFS_VNODE_TO_I(vnode);
  36. _debug("FS: ft=%d lk=%d sz=%Zu ver=%Lu mod=%hu",
  37. vnode->status.type,
  38. vnode->status.nlink,
  39. vnode->status.size,
  40. vnode->status.version,
  41. vnode->status.mode);
  42. switch (vnode->status.type) {
  43. case AFS_FTYPE_FILE:
  44. inode->i_mode = S_IFREG | vnode->status.mode;
  45. inode->i_op = &afs_file_inode_operations;
  46. inode->i_fop = &generic_ro_fops;
  47. break;
  48. case AFS_FTYPE_DIR:
  49. inode->i_mode = S_IFDIR | vnode->status.mode;
  50. inode->i_op = &afs_dir_inode_operations;
  51. inode->i_fop = &afs_dir_file_operations;
  52. break;
  53. case AFS_FTYPE_SYMLINK:
  54. inode->i_mode = S_IFLNK | vnode->status.mode;
  55. inode->i_op = &page_symlink_inode_operations;
  56. break;
  57. default:
  58. printk("kAFS: AFS vnode with undefined type\n");
  59. return -EBADMSG;
  60. }
  61. inode->i_nlink = vnode->status.nlink;
  62. inode->i_uid = vnode->status.owner;
  63. inode->i_gid = 0;
  64. inode->i_size = vnode->status.size;
  65. inode->i_ctime.tv_sec = vnode->status.mtime_server;
  66. inode->i_ctime.tv_nsec = 0;
  67. inode->i_atime = inode->i_mtime = inode->i_ctime;
  68. inode->i_blocks = 0;
  69. inode->i_version = vnode->fid.unique;
  70. inode->i_mapping->a_ops = &afs_fs_aops;
  71. /* check to see whether a symbolic link is really a mountpoint */
  72. if (vnode->status.type == AFS_FTYPE_SYMLINK) {
  73. afs_mntpt_check_symlink(vnode);
  74. if (vnode->flags & AFS_VNODE_MOUNTPOINT) {
  75. inode->i_mode = S_IFDIR | vnode->status.mode;
  76. inode->i_op = &afs_mntpt_inode_operations;
  77. inode->i_fop = &afs_mntpt_file_operations;
  78. }
  79. }
  80. return 0;
  81. } /* end afs_inode_map_status() */
  82. /*****************************************************************************/
  83. /*
  84. * attempt to fetch the status of an inode, coelescing multiple simultaneous
  85. * fetches
  86. */
  87. static int afs_inode_fetch_status(struct inode *inode)
  88. {
  89. struct afs_vnode *vnode;
  90. int ret;
  91. vnode = AFS_FS_I(inode);
  92. ret = afs_vnode_fetch_status(vnode);
  93. if (ret == 0)
  94. ret = afs_inode_map_status(vnode);
  95. return ret;
  96. } /* end afs_inode_fetch_status() */
  97. /*****************************************************************************/
  98. /*
  99. * iget5() comparator
  100. */
  101. static int afs_iget5_test(struct inode *inode, void *opaque)
  102. {
  103. struct afs_iget_data *data = opaque;
  104. return inode->i_ino == data->fid.vnode &&
  105. inode->i_version == data->fid.unique;
  106. } /* end afs_iget5_test() */
  107. /*****************************************************************************/
  108. /*
  109. * iget5() inode initialiser
  110. */
  111. static int afs_iget5_set(struct inode *inode, void *opaque)
  112. {
  113. struct afs_iget_data *data = opaque;
  114. struct afs_vnode *vnode = AFS_FS_I(inode);
  115. inode->i_ino = data->fid.vnode;
  116. inode->i_version = data->fid.unique;
  117. vnode->fid = data->fid;
  118. vnode->volume = data->volume;
  119. return 0;
  120. } /* end afs_iget5_set() */
  121. /*****************************************************************************/
  122. /*
  123. * inode retrieval
  124. */
  125. inline int afs_iget(struct super_block *sb, struct afs_fid *fid,
  126. struct inode **_inode)
  127. {
  128. struct afs_iget_data data = { .fid = *fid };
  129. struct afs_super_info *as;
  130. struct afs_vnode *vnode;
  131. struct inode *inode;
  132. int ret;
  133. _enter(",{%u,%u,%u},,", fid->vid, fid->vnode, fid->unique);
  134. as = sb->s_fs_info;
  135. data.volume = as->volume;
  136. inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
  137. &data);
  138. if (!inode) {
  139. _leave(" = -ENOMEM");
  140. return -ENOMEM;
  141. }
  142. vnode = AFS_FS_I(inode);
  143. /* deal with an existing inode */
  144. if (!(inode->i_state & I_NEW)) {
  145. ret = afs_vnode_fetch_status(vnode);
  146. if (ret==0)
  147. *_inode = inode;
  148. else
  149. iput(inode);
  150. _leave(" = %d", ret);
  151. return ret;
  152. }
  153. #ifdef AFS_CACHING_SUPPORT
  154. /* set up caching before reading the status, as fetch-status reads the
  155. * first page of symlinks to see if they're really mntpts */
  156. cachefs_acquire_cookie(vnode->volume->cache,
  157. NULL,
  158. vnode,
  159. &vnode->cache);
  160. #endif
  161. /* okay... it's a new inode */
  162. inode->i_flags |= S_NOATIME;
  163. vnode->flags |= AFS_VNODE_CHANGED;
  164. ret = afs_inode_fetch_status(inode);
  165. if (ret<0)
  166. goto bad_inode;
  167. /* success */
  168. unlock_new_inode(inode);
  169. *_inode = inode;
  170. _leave(" = 0 [CB { v=%u x=%lu t=%u }]",
  171. vnode->cb_version,
  172. vnode->cb_timeout.timo_jif,
  173. vnode->cb_type);
  174. return 0;
  175. /* failure */
  176. bad_inode:
  177. make_bad_inode(inode);
  178. unlock_new_inode(inode);
  179. iput(inode);
  180. _leave(" = %d [bad]", ret);
  181. return ret;
  182. } /* end afs_iget() */
  183. /*****************************************************************************/
  184. /*
  185. * read the attributes of an inode
  186. */
  187. int afs_inode_getattr(struct vfsmount *mnt, struct dentry *dentry,
  188. struct kstat *stat)
  189. {
  190. struct afs_vnode *vnode;
  191. struct inode *inode;
  192. int ret;
  193. inode = dentry->d_inode;
  194. _enter("{ ino=%lu v=%lu }", inode->i_ino, inode->i_version);
  195. vnode = AFS_FS_I(inode);
  196. ret = afs_inode_fetch_status(inode);
  197. if (ret == -ENOENT) {
  198. _leave(" = %d [%d %p]",
  199. ret, atomic_read(&dentry->d_count), dentry->d_inode);
  200. return ret;
  201. }
  202. else if (ret < 0) {
  203. make_bad_inode(inode);
  204. _leave(" = %d", ret);
  205. return ret;
  206. }
  207. /* transfer attributes from the inode structure to the stat
  208. * structure */
  209. generic_fillattr(inode, stat);
  210. _leave(" = 0 CB { v=%u x=%u t=%u }",
  211. vnode->cb_version,
  212. vnode->cb_expiry,
  213. vnode->cb_type);
  214. return 0;
  215. } /* end afs_inode_getattr() */
  216. /*****************************************************************************/
  217. /*
  218. * clear an AFS inode
  219. */
  220. void afs_clear_inode(struct inode *inode)
  221. {
  222. struct afs_vnode *vnode;
  223. vnode = AFS_FS_I(inode);
  224. _enter("ino=%lu { vn=%08x v=%u x=%u t=%u }",
  225. inode->i_ino,
  226. vnode->fid.vnode,
  227. vnode->cb_version,
  228. vnode->cb_expiry,
  229. vnode->cb_type
  230. );
  231. BUG_ON(inode->i_ino != vnode->fid.vnode);
  232. afs_vnode_give_up_callback(vnode);
  233. #ifdef AFS_CACHING_SUPPORT
  234. cachefs_relinquish_cookie(vnode->cache, 0);
  235. vnode->cache = NULL;
  236. #endif
  237. _leave("");
  238. } /* end afs_clear_inode() */