inode.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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 <linux/sched.h>
  22. #include "internal.h"
  23. struct afs_iget_data {
  24. struct afs_fid fid;
  25. struct afs_volume *volume; /* volume on which resides */
  26. };
  27. /*
  28. * map the AFS file status to the inode member variables
  29. */
  30. static int afs_inode_map_status(struct afs_vnode *vnode, struct key *key)
  31. {
  32. struct inode *inode = AFS_VNODE_TO_I(vnode);
  33. _debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu",
  34. vnode->status.type,
  35. vnode->status.nlink,
  36. (unsigned long long) vnode->status.size,
  37. vnode->status.data_version,
  38. vnode->status.mode);
  39. switch (vnode->status.type) {
  40. case AFS_FTYPE_FILE:
  41. inode->i_mode = S_IFREG | vnode->status.mode;
  42. inode->i_op = &afs_file_inode_operations;
  43. inode->i_fop = &afs_file_operations;
  44. break;
  45. case AFS_FTYPE_DIR:
  46. inode->i_mode = S_IFDIR | vnode->status.mode;
  47. inode->i_op = &afs_dir_inode_operations;
  48. inode->i_fop = &afs_dir_file_operations;
  49. break;
  50. case AFS_FTYPE_SYMLINK:
  51. inode->i_mode = S_IFLNK | vnode->status.mode;
  52. inode->i_op = &page_symlink_inode_operations;
  53. break;
  54. default:
  55. printk("kAFS: AFS vnode with undefined type\n");
  56. return -EBADMSG;
  57. }
  58. inode->i_nlink = vnode->status.nlink;
  59. inode->i_uid = vnode->status.owner;
  60. inode->i_gid = 0;
  61. inode->i_size = vnode->status.size;
  62. inode->i_ctime.tv_sec = vnode->status.mtime_server;
  63. inode->i_ctime.tv_nsec = 0;
  64. inode->i_atime = inode->i_mtime = inode->i_ctime;
  65. inode->i_blocks = 0;
  66. inode->i_version = vnode->fid.unique;
  67. inode->i_mapping->a_ops = &afs_fs_aops;
  68. /* check to see whether a symbolic link is really a mountpoint */
  69. if (vnode->status.type == AFS_FTYPE_SYMLINK) {
  70. afs_mntpt_check_symlink(vnode, key);
  71. if (test_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags)) {
  72. inode->i_mode = S_IFDIR | vnode->status.mode;
  73. inode->i_op = &afs_mntpt_inode_operations;
  74. inode->i_fop = &afs_mntpt_file_operations;
  75. }
  76. }
  77. return 0;
  78. }
  79. /*
  80. * iget5() comparator
  81. */
  82. static int afs_iget5_test(struct inode *inode, void *opaque)
  83. {
  84. struct afs_iget_data *data = opaque;
  85. return inode->i_ino == data->fid.vnode &&
  86. inode->i_version == data->fid.unique;
  87. }
  88. /*
  89. * iget5() inode initialiser
  90. */
  91. static int afs_iget5_set(struct inode *inode, void *opaque)
  92. {
  93. struct afs_iget_data *data = opaque;
  94. struct afs_vnode *vnode = AFS_FS_I(inode);
  95. inode->i_ino = data->fid.vnode;
  96. inode->i_version = data->fid.unique;
  97. vnode->fid = data->fid;
  98. vnode->volume = data->volume;
  99. return 0;
  100. }
  101. /*
  102. * inode retrieval
  103. */
  104. struct inode *afs_iget(struct super_block *sb, struct key *key,
  105. struct afs_fid *fid, struct afs_file_status *status,
  106. struct afs_callback *cb)
  107. {
  108. struct afs_iget_data data = { .fid = *fid };
  109. struct afs_super_info *as;
  110. struct afs_vnode *vnode;
  111. struct inode *inode;
  112. int ret;
  113. _enter(",{%x:%u.%u},,", fid->vid, fid->vnode, fid->unique);
  114. as = sb->s_fs_info;
  115. data.volume = as->volume;
  116. inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
  117. &data);
  118. if (!inode) {
  119. _leave(" = -ENOMEM");
  120. return ERR_PTR(-ENOMEM);
  121. }
  122. _debug("GOT INODE %p { vl=%x vn=%x, u=%x }",
  123. inode, fid->vid, fid->vnode, fid->unique);
  124. vnode = AFS_FS_I(inode);
  125. /* deal with an existing inode */
  126. if (!(inode->i_state & I_NEW)) {
  127. _leave(" = %p", inode);
  128. return inode;
  129. }
  130. #ifdef AFS_CACHING_SUPPORT
  131. /* set up caching before reading the status, as fetch-status reads the
  132. * first page of symlinks to see if they're really mntpts */
  133. cachefs_acquire_cookie(vnode->volume->cache,
  134. NULL,
  135. vnode,
  136. &vnode->cache);
  137. #endif
  138. if (!status) {
  139. /* it's a remotely extant inode */
  140. set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
  141. ret = afs_vnode_fetch_status(vnode, NULL, key);
  142. if (ret < 0)
  143. goto bad_inode;
  144. } else {
  145. /* it's an inode we just created */
  146. memcpy(&vnode->status, status, sizeof(vnode->status));
  147. if (!cb) {
  148. /* it's a symlink we just created (the fileserver
  149. * didn't give us a callback) */
  150. vnode->cb_version = 0;
  151. vnode->cb_expiry = 0;
  152. vnode->cb_type = 0;
  153. vnode->cb_expires = get_seconds();
  154. } else {
  155. vnode->cb_version = cb->version;
  156. vnode->cb_expiry = cb->expiry;
  157. vnode->cb_type = cb->type;
  158. vnode->cb_expires = vnode->cb_expiry + get_seconds();
  159. }
  160. }
  161. ret = afs_inode_map_status(vnode, key);
  162. if (ret < 0)
  163. goto bad_inode;
  164. /* success */
  165. clear_bit(AFS_VNODE_UNSET, &vnode->flags);
  166. inode->i_flags |= S_NOATIME;
  167. unlock_new_inode(inode);
  168. _leave(" = %p [CB { v=%u t=%u }]", inode, vnode->cb_version, vnode->cb_type);
  169. return inode;
  170. /* failure */
  171. bad_inode:
  172. make_bad_inode(inode);
  173. unlock_new_inode(inode);
  174. iput(inode);
  175. _leave(" = %d [bad]", ret);
  176. return ERR_PTR(ret);
  177. }
  178. /*
  179. * mark the data attached to an inode as obsolete due to a write on the server
  180. * - might also want to ditch all the outstanding writes and dirty pages
  181. */
  182. void afs_zap_data(struct afs_vnode *vnode)
  183. {
  184. _enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);
  185. /* nuke all the non-dirty pages that aren't locked, mapped or being
  186. * written back in a regular file and completely discard the pages in a
  187. * directory or symlink */
  188. if (S_ISREG(vnode->vfs_inode.i_mode))
  189. invalidate_remote_inode(&vnode->vfs_inode);
  190. else
  191. invalidate_inode_pages2(vnode->vfs_inode.i_mapping);
  192. }
  193. /*
  194. * validate a vnode/inode
  195. * - there are several things we need to check
  196. * - parent dir data changes (rm, rmdir, rename, mkdir, create, link,
  197. * symlink)
  198. * - parent dir metadata changed (security changes)
  199. * - dentry data changed (write, truncate)
  200. * - dentry metadata changed (security changes)
  201. */
  202. int afs_validate(struct afs_vnode *vnode, struct key *key)
  203. {
  204. int ret;
  205. _enter("{v={%x:%u} fl=%lx},%x",
  206. vnode->fid.vid, vnode->fid.vnode, vnode->flags,
  207. key_serial(key));
  208. if (vnode->cb_promised &&
  209. !test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags) &&
  210. !test_bit(AFS_VNODE_MODIFIED, &vnode->flags) &&
  211. !test_bit(AFS_VNODE_ZAP_DATA, &vnode->flags)) {
  212. if (vnode->cb_expires < get_seconds() + 10) {
  213. _debug("callback expired");
  214. set_bit(AFS_VNODE_CB_BROKEN, &vnode->flags);
  215. } else {
  216. goto valid;
  217. }
  218. }
  219. if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
  220. goto valid;
  221. mutex_lock(&vnode->validate_lock);
  222. /* if the promise has expired, we need to check the server again to get
  223. * a new promise - note that if the (parent) directory's metadata was
  224. * changed then the security may be different and we may no longer have
  225. * access */
  226. if (!vnode->cb_promised ||
  227. test_bit(AFS_VNODE_CB_BROKEN, &vnode->flags)) {
  228. _debug("not promised");
  229. ret = afs_vnode_fetch_status(vnode, NULL, key);
  230. if (ret < 0)
  231. goto error_unlock;
  232. _debug("new promise [fl=%lx]", vnode->flags);
  233. }
  234. if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
  235. _debug("file already deleted");
  236. ret = -ESTALE;
  237. goto error_unlock;
  238. }
  239. /* if the vnode's data version number changed then its contents are
  240. * different */
  241. if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags))
  242. afs_zap_data(vnode);
  243. clear_bit(AFS_VNODE_MODIFIED, &vnode->flags);
  244. mutex_unlock(&vnode->validate_lock);
  245. valid:
  246. _leave(" = 0");
  247. return 0;
  248. error_unlock:
  249. mutex_unlock(&vnode->validate_lock);
  250. _leave(" = %d", ret);
  251. return ret;
  252. }
  253. /*
  254. * read the attributes of an inode
  255. */
  256. int afs_getattr(struct vfsmount *mnt, struct dentry *dentry,
  257. struct kstat *stat)
  258. {
  259. struct inode *inode;
  260. inode = dentry->d_inode;
  261. _enter("{ ino=%lu v=%lu }", inode->i_ino, inode->i_version);
  262. generic_fillattr(inode, stat);
  263. return 0;
  264. }
  265. /*
  266. * clear an AFS inode
  267. */
  268. void afs_clear_inode(struct inode *inode)
  269. {
  270. struct afs_permits *permits;
  271. struct afs_vnode *vnode;
  272. vnode = AFS_FS_I(inode);
  273. _enter("{%x:%u.%d} v=%u x=%u t=%u }",
  274. vnode->fid.vid,
  275. vnode->fid.vnode,
  276. vnode->fid.unique,
  277. vnode->cb_version,
  278. vnode->cb_expiry,
  279. vnode->cb_type);
  280. _debug("CLEAR INODE %p", inode);
  281. ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
  282. afs_give_up_callback(vnode);
  283. if (vnode->server) {
  284. spin_lock(&vnode->server->fs_lock);
  285. rb_erase(&vnode->server_rb, &vnode->server->fs_vnodes);
  286. spin_unlock(&vnode->server->fs_lock);
  287. afs_put_server(vnode->server);
  288. vnode->server = NULL;
  289. }
  290. ASSERT(list_empty(&vnode->writebacks));
  291. ASSERT(!vnode->cb_promised);
  292. #ifdef AFS_CACHING_SUPPORT
  293. cachefs_relinquish_cookie(vnode->cache, 0);
  294. vnode->cache = NULL;
  295. #endif
  296. mutex_lock(&vnode->permits_lock);
  297. permits = vnode->permits;
  298. rcu_assign_pointer(vnode->permits, NULL);
  299. mutex_unlock(&vnode->permits_lock);
  300. if (permits)
  301. call_rcu(&permits->rcu, afs_zap_permits);
  302. _leave("");
  303. }
  304. /*
  305. * set the attributes of an inode
  306. */
  307. int afs_setattr(struct dentry *dentry, struct iattr *attr)
  308. {
  309. struct afs_vnode *vnode = AFS_FS_I(dentry->d_inode);
  310. struct key *key;
  311. int ret;
  312. _enter("{%x:%u},{n=%s},%x",
  313. vnode->fid.vid, vnode->fid.vnode, dentry->d_name.name,
  314. attr->ia_valid);
  315. if (!(attr->ia_valid & (ATTR_SIZE | ATTR_MODE | ATTR_UID | ATTR_GID |
  316. ATTR_MTIME))) {
  317. _leave(" = 0 [unsupported]");
  318. return 0;
  319. }
  320. /* flush any dirty data outstanding on a regular file */
  321. if (S_ISREG(vnode->vfs_inode.i_mode)) {
  322. filemap_write_and_wait(vnode->vfs_inode.i_mapping);
  323. afs_writeback_all(vnode);
  324. }
  325. if (attr->ia_valid & ATTR_FILE) {
  326. key = attr->ia_file->private_data;
  327. } else {
  328. key = afs_request_key(vnode->volume->cell);
  329. if (IS_ERR(key)) {
  330. ret = PTR_ERR(key);
  331. goto error;
  332. }
  333. }
  334. ret = afs_vnode_setattr(vnode, key, attr);
  335. if (!(attr->ia_valid & ATTR_FILE))
  336. key_put(key);
  337. error:
  338. _leave(" = %d", ret);
  339. return ret;
  340. }