inode.c 10 KB

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