inode.c 12 KB

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