inode.c 12 KB

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