inode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  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 <linux/iversion.h>
  24. #include "internal.h"
  25. static const struct inode_operations afs_symlink_inode_operations = {
  26. .get_link = page_get_link,
  27. .listxattr = afs_listxattr,
  28. };
  29. /*
  30. * Initialise an inode from the vnode status.
  31. */
  32. static int afs_inode_init_from_status(struct afs_vnode *vnode, struct key *key)
  33. {
  34. struct inode *inode = AFS_VNODE_TO_I(vnode);
  35. _debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu",
  36. vnode->status.type,
  37. vnode->status.nlink,
  38. (unsigned long long) vnode->status.size,
  39. vnode->status.data_version,
  40. vnode->status.mode);
  41. read_seqlock_excl(&vnode->cb_lock);
  42. afs_update_inode_from_status(vnode, &vnode->status, NULL,
  43. AFS_VNODE_NOT_YET_SET);
  44. switch (vnode->status.type) {
  45. case AFS_FTYPE_FILE:
  46. inode->i_mode = S_IFREG | vnode->status.mode;
  47. inode->i_op = &afs_file_inode_operations;
  48. inode->i_fop = &afs_file_operations;
  49. inode->i_mapping->a_ops = &afs_fs_aops;
  50. break;
  51. case AFS_FTYPE_DIR:
  52. inode->i_mode = S_IFDIR | vnode->status.mode;
  53. inode->i_op = &afs_dir_inode_operations;
  54. inode->i_fop = &afs_dir_file_operations;
  55. inode->i_mapping->a_ops = &afs_dir_aops;
  56. break;
  57. case AFS_FTYPE_SYMLINK:
  58. /* Symlinks with a mode of 0644 are actually mountpoints. */
  59. if ((vnode->status.mode & 0777) == 0644) {
  60. inode->i_flags |= S_AUTOMOUNT;
  61. set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
  62. inode->i_mode = S_IFDIR | 0555;
  63. inode->i_op = &afs_mntpt_inode_operations;
  64. inode->i_fop = &afs_mntpt_file_operations;
  65. inode->i_mapping->a_ops = &afs_fs_aops;
  66. } else {
  67. inode->i_mode = S_IFLNK | vnode->status.mode;
  68. inode->i_op = &afs_symlink_inode_operations;
  69. inode->i_mapping->a_ops = &afs_fs_aops;
  70. }
  71. inode_nohighmem(inode);
  72. break;
  73. default:
  74. printk("kAFS: AFS vnode with undefined type\n");
  75. read_sequnlock_excl(&vnode->cb_lock);
  76. return afs_protocol_error(NULL, -EBADMSG);
  77. }
  78. inode->i_blocks = 0;
  79. vnode->invalid_before = vnode->status.data_version;
  80. read_sequnlock_excl(&vnode->cb_lock);
  81. return 0;
  82. }
  83. /*
  84. * Fetch file status from the volume.
  85. */
  86. int afs_fetch_status(struct afs_vnode *vnode, struct key *key, bool new_inode)
  87. {
  88. struct afs_fs_cursor fc;
  89. int ret;
  90. _enter("%s,{%x:%u.%u,S=%lx}",
  91. vnode->volume->name,
  92. vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique,
  93. vnode->flags);
  94. ret = -ERESTARTSYS;
  95. if (afs_begin_vnode_operation(&fc, vnode, key)) {
  96. while (afs_select_fileserver(&fc)) {
  97. fc.cb_break = afs_calc_vnode_cb_break(vnode);
  98. afs_fs_fetch_file_status(&fc, NULL, new_inode);
  99. }
  100. afs_check_for_remote_deletion(&fc, fc.vnode);
  101. afs_vnode_commit_status(&fc, vnode, fc.cb_break);
  102. ret = afs_end_vnode_operation(&fc);
  103. }
  104. _leave(" = %d", ret);
  105. return ret;
  106. }
  107. /*
  108. * iget5() comparator
  109. */
  110. int afs_iget5_test(struct inode *inode, void *opaque)
  111. {
  112. struct afs_iget_data *data = opaque;
  113. return inode->i_ino == data->fid.vnode &&
  114. inode->i_generation == data->fid.unique;
  115. }
  116. /*
  117. * iget5() comparator for inode created by autocell operations
  118. *
  119. * These pseudo inodes don't match anything.
  120. */
  121. static int afs_iget5_pseudo_dir_test(struct inode *inode, void *opaque)
  122. {
  123. return 0;
  124. }
  125. /*
  126. * iget5() inode initialiser
  127. */
  128. static int afs_iget5_set(struct inode *inode, void *opaque)
  129. {
  130. struct afs_iget_data *data = opaque;
  131. struct afs_vnode *vnode = AFS_FS_I(inode);
  132. inode->i_ino = data->fid.vnode;
  133. inode->i_generation = data->fid.unique;
  134. vnode->fid = data->fid;
  135. vnode->volume = data->volume;
  136. return 0;
  137. }
  138. /*
  139. * Create an inode for a dynamic root directory or an autocell dynamic
  140. * automount dir.
  141. */
  142. struct inode *afs_iget_pseudo_dir(struct super_block *sb, bool root)
  143. {
  144. struct afs_iget_data data;
  145. struct afs_super_info *as;
  146. struct afs_vnode *vnode;
  147. struct inode *inode;
  148. static atomic_t afs_autocell_ino;
  149. _enter("");
  150. as = sb->s_fs_info;
  151. if (as->volume) {
  152. data.volume = as->volume;
  153. data.fid.vid = as->volume->vid;
  154. }
  155. if (root) {
  156. data.fid.vnode = 1;
  157. data.fid.unique = 1;
  158. } else {
  159. data.fid.vnode = atomic_inc_return(&afs_autocell_ino);
  160. data.fid.unique = 0;
  161. }
  162. inode = iget5_locked(sb, data.fid.vnode,
  163. afs_iget5_pseudo_dir_test, afs_iget5_set,
  164. &data);
  165. if (!inode) {
  166. _leave(" = -ENOMEM");
  167. return ERR_PTR(-ENOMEM);
  168. }
  169. _debug("GOT INODE %p { ino=%lu, vl=%x, vn=%x, u=%x }",
  170. inode, inode->i_ino, data.fid.vid, data.fid.vnode,
  171. data.fid.unique);
  172. vnode = AFS_FS_I(inode);
  173. /* there shouldn't be an existing inode */
  174. BUG_ON(!(inode->i_state & I_NEW));
  175. inode->i_size = 0;
  176. inode->i_mode = S_IFDIR | S_IRUGO | S_IXUGO;
  177. if (root) {
  178. inode->i_op = &afs_dynroot_inode_operations;
  179. inode->i_fop = &afs_dynroot_file_operations;
  180. } else {
  181. inode->i_op = &afs_autocell_inode_operations;
  182. }
  183. set_nlink(inode, 2);
  184. inode->i_uid = GLOBAL_ROOT_UID;
  185. inode->i_gid = GLOBAL_ROOT_GID;
  186. inode->i_ctime.tv_sec = get_seconds();
  187. inode->i_ctime.tv_nsec = 0;
  188. inode->i_atime = inode->i_mtime = inode->i_ctime;
  189. inode->i_blocks = 0;
  190. inode_set_iversion_raw(inode, 0);
  191. inode->i_generation = 0;
  192. set_bit(AFS_VNODE_PSEUDODIR, &vnode->flags);
  193. if (!root) {
  194. set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
  195. inode->i_flags |= S_AUTOMOUNT;
  196. }
  197. inode->i_flags |= S_NOATIME;
  198. unlock_new_inode(inode);
  199. _leave(" = %p", inode);
  200. return inode;
  201. }
  202. /*
  203. * Get a cache cookie for an inode.
  204. */
  205. static void afs_get_inode_cache(struct afs_vnode *vnode)
  206. {
  207. #ifdef CONFIG_AFS_FSCACHE
  208. struct {
  209. u32 vnode_id;
  210. u32 unique;
  211. u32 vnode_id_ext[2]; /* Allow for a 96-bit key */
  212. } __packed key;
  213. struct afs_vnode_cache_aux aux;
  214. if (vnode->status.type == AFS_FTYPE_DIR) {
  215. vnode->cache = NULL;
  216. return;
  217. }
  218. key.vnode_id = vnode->fid.vnode;
  219. key.unique = vnode->fid.unique;
  220. key.vnode_id_ext[0] = 0;
  221. key.vnode_id_ext[1] = 0;
  222. aux.data_version = vnode->status.data_version;
  223. vnode->cache = fscache_acquire_cookie(vnode->volume->cache,
  224. &afs_vnode_cache_index_def,
  225. &key, sizeof(key),
  226. &aux, sizeof(aux),
  227. vnode, vnode->status.size, true);
  228. #endif
  229. }
  230. /*
  231. * inode retrieval
  232. */
  233. struct inode *afs_iget(struct super_block *sb, struct key *key,
  234. struct afs_fid *fid, struct afs_file_status *status,
  235. struct afs_callback *cb, struct afs_cb_interest *cbi)
  236. {
  237. struct afs_iget_data data = { .fid = *fid };
  238. struct afs_super_info *as;
  239. struct afs_vnode *vnode;
  240. struct inode *inode;
  241. int ret;
  242. _enter(",{%x:%u.%u},,", fid->vid, fid->vnode, fid->unique);
  243. as = sb->s_fs_info;
  244. data.volume = as->volume;
  245. inode = iget5_locked(sb, fid->vnode, afs_iget5_test, afs_iget5_set,
  246. &data);
  247. if (!inode) {
  248. _leave(" = -ENOMEM");
  249. return ERR_PTR(-ENOMEM);
  250. }
  251. _debug("GOT INODE %p { vl=%x vn=%x, u=%x }",
  252. inode, fid->vid, fid->vnode, fid->unique);
  253. vnode = AFS_FS_I(inode);
  254. /* deal with an existing inode */
  255. if (!(inode->i_state & I_NEW)) {
  256. _leave(" = %p", inode);
  257. return inode;
  258. }
  259. if (!status) {
  260. /* it's a remotely extant inode */
  261. ret = afs_fetch_status(vnode, key, true);
  262. if (ret < 0)
  263. goto bad_inode;
  264. } else {
  265. /* it's an inode we just created */
  266. memcpy(&vnode->status, status, sizeof(vnode->status));
  267. if (!cb) {
  268. /* it's a symlink we just created (the fileserver
  269. * didn't give us a callback) */
  270. vnode->cb_version = 0;
  271. vnode->cb_type = 0;
  272. vnode->cb_expires_at = 0;
  273. } else {
  274. vnode->cb_version = cb->version;
  275. vnode->cb_type = cb->type;
  276. vnode->cb_expires_at = cb->expiry;
  277. vnode->cb_interest = afs_get_cb_interest(cbi);
  278. set_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
  279. }
  280. vnode->cb_expires_at += ktime_get_real_seconds();
  281. }
  282. ret = afs_inode_init_from_status(vnode, key);
  283. if (ret < 0)
  284. goto bad_inode;
  285. afs_get_inode_cache(vnode);
  286. /* success */
  287. clear_bit(AFS_VNODE_UNSET, &vnode->flags);
  288. inode->i_flags |= S_NOATIME;
  289. unlock_new_inode(inode);
  290. _leave(" = %p [CB { v=%u t=%u }]", inode, vnode->cb_version, vnode->cb_type);
  291. return inode;
  292. /* failure */
  293. bad_inode:
  294. iget_failed(inode);
  295. _leave(" = %d [bad]", ret);
  296. return ERR_PTR(ret);
  297. }
  298. /*
  299. * mark the data attached to an inode as obsolete due to a write on the server
  300. * - might also want to ditch all the outstanding writes and dirty pages
  301. */
  302. void afs_zap_data(struct afs_vnode *vnode)
  303. {
  304. _enter("{%x:%u}", vnode->fid.vid, vnode->fid.vnode);
  305. #ifdef CONFIG_AFS_FSCACHE
  306. fscache_invalidate(vnode->cache);
  307. #endif
  308. /* nuke all the non-dirty pages that aren't locked, mapped or being
  309. * written back in a regular file and completely discard the pages in a
  310. * directory or symlink */
  311. if (S_ISREG(vnode->vfs_inode.i_mode))
  312. invalidate_remote_inode(&vnode->vfs_inode);
  313. else
  314. invalidate_inode_pages2(vnode->vfs_inode.i_mapping);
  315. }
  316. /*
  317. * validate a vnode/inode
  318. * - there are several things we need to check
  319. * - parent dir data changes (rm, rmdir, rename, mkdir, create, link,
  320. * symlink)
  321. * - parent dir metadata changed (security changes)
  322. * - dentry data changed (write, truncate)
  323. * - dentry metadata changed (security changes)
  324. */
  325. int afs_validate(struct afs_vnode *vnode, struct key *key)
  326. {
  327. time64_t now = ktime_get_real_seconds();
  328. bool valid;
  329. int ret;
  330. _enter("{v={%x:%u} fl=%lx},%x",
  331. vnode->fid.vid, vnode->fid.vnode, vnode->flags,
  332. key_serial(key));
  333. /* Quickly check the callback state. Ideally, we'd use read_seqbegin
  334. * here, but we have no way to pass the net namespace to the RCU
  335. * cleanup for the server record.
  336. */
  337. read_seqlock_excl(&vnode->cb_lock);
  338. if (test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
  339. if (vnode->cb_s_break != vnode->cb_interest->server->cb_s_break ||
  340. vnode->cb_v_break != vnode->volume->cb_v_break) {
  341. vnode->cb_s_break = vnode->cb_interest->server->cb_s_break;
  342. vnode->cb_v_break = vnode->volume->cb_v_break;
  343. valid = false;
  344. } else if (test_bit(AFS_VNODE_ZAP_DATA, &vnode->flags)) {
  345. valid = false;
  346. } else if (vnode->cb_expires_at - 10 <= now) {
  347. valid = false;
  348. } else {
  349. valid = true;
  350. }
  351. } else if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
  352. valid = true;
  353. } else {
  354. vnode->cb_v_break = vnode->volume->cb_v_break;
  355. valid = false;
  356. }
  357. read_sequnlock_excl(&vnode->cb_lock);
  358. if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
  359. clear_nlink(&vnode->vfs_inode);
  360. if (valid)
  361. goto valid;
  362. down_write(&vnode->validate_lock);
  363. /* if the promise has expired, we need to check the server again to get
  364. * a new promise - note that if the (parent) directory's metadata was
  365. * changed then the security may be different and we may no longer have
  366. * access */
  367. if (!test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
  368. _debug("not promised");
  369. ret = afs_fetch_status(vnode, key, false);
  370. if (ret < 0) {
  371. if (ret == -ENOENT) {
  372. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  373. ret = -ESTALE;
  374. }
  375. goto error_unlock;
  376. }
  377. _debug("new promise [fl=%lx]", vnode->flags);
  378. }
  379. if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
  380. _debug("file already deleted");
  381. ret = -ESTALE;
  382. goto error_unlock;
  383. }
  384. /* if the vnode's data version number changed then its contents are
  385. * different */
  386. if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags))
  387. afs_zap_data(vnode);
  388. up_write(&vnode->validate_lock);
  389. valid:
  390. _leave(" = 0");
  391. return 0;
  392. error_unlock:
  393. up_write(&vnode->validate_lock);
  394. _leave(" = %d", ret);
  395. return ret;
  396. }
  397. /*
  398. * read the attributes of an inode
  399. */
  400. int afs_getattr(const struct path *path, struct kstat *stat,
  401. u32 request_mask, unsigned int query_flags)
  402. {
  403. struct inode *inode = d_inode(path->dentry);
  404. struct afs_vnode *vnode = AFS_FS_I(inode);
  405. int seq = 0;
  406. _enter("{ ino=%lu v=%u }", inode->i_ino, inode->i_generation);
  407. do {
  408. read_seqbegin_or_lock(&vnode->cb_lock, &seq);
  409. generic_fillattr(inode, stat);
  410. } while (need_seqretry(&vnode->cb_lock, seq));
  411. done_seqretry(&vnode->cb_lock, seq);
  412. return 0;
  413. }
  414. /*
  415. * discard an AFS inode
  416. */
  417. int afs_drop_inode(struct inode *inode)
  418. {
  419. _enter("");
  420. if (test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(inode)->flags))
  421. return generic_delete_inode(inode);
  422. else
  423. return generic_drop_inode(inode);
  424. }
  425. /*
  426. * clear an AFS inode
  427. */
  428. void afs_evict_inode(struct inode *inode)
  429. {
  430. struct afs_vnode *vnode;
  431. vnode = AFS_FS_I(inode);
  432. _enter("{%x:%u.%d}",
  433. vnode->fid.vid,
  434. vnode->fid.vnode,
  435. vnode->fid.unique);
  436. _debug("CLEAR INODE %p", inode);
  437. ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
  438. truncate_inode_pages_final(&inode->i_data);
  439. clear_inode(inode);
  440. if (vnode->cb_interest) {
  441. afs_put_cb_interest(afs_i2net(inode), vnode->cb_interest);
  442. vnode->cb_interest = NULL;
  443. }
  444. while (!list_empty(&vnode->wb_keys)) {
  445. struct afs_wb_key *wbk = list_entry(vnode->wb_keys.next,
  446. struct afs_wb_key, vnode_link);
  447. list_del(&wbk->vnode_link);
  448. afs_put_wb_key(wbk);
  449. }
  450. #ifdef CONFIG_AFS_FSCACHE
  451. {
  452. struct afs_vnode_cache_aux aux;
  453. aux.data_version = vnode->status.data_version;
  454. fscache_relinquish_cookie(vnode->cache, &aux,
  455. test_bit(AFS_VNODE_DELETED, &vnode->flags));
  456. vnode->cache = NULL;
  457. }
  458. #endif
  459. afs_prune_wb_keys(vnode);
  460. afs_put_permits(rcu_access_pointer(vnode->permit_cache));
  461. key_put(vnode->lock_key);
  462. vnode->lock_key = NULL;
  463. _leave("");
  464. }
  465. /*
  466. * set the attributes of an inode
  467. */
  468. int afs_setattr(struct dentry *dentry, struct iattr *attr)
  469. {
  470. struct afs_fs_cursor fc;
  471. struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
  472. struct key *key;
  473. int ret;
  474. _enter("{%x:%u},{n=%pd},%x",
  475. vnode->fid.vid, vnode->fid.vnode, dentry,
  476. attr->ia_valid);
  477. if (!(attr->ia_valid & (ATTR_SIZE | ATTR_MODE | ATTR_UID | ATTR_GID |
  478. ATTR_MTIME))) {
  479. _leave(" = 0 [unsupported]");
  480. return 0;
  481. }
  482. /* flush any dirty data outstanding on a regular file */
  483. if (S_ISREG(vnode->vfs_inode.i_mode))
  484. filemap_write_and_wait(vnode->vfs_inode.i_mapping);
  485. if (attr->ia_valid & ATTR_FILE) {
  486. key = afs_file_key(attr->ia_file);
  487. } else {
  488. key = afs_request_key(vnode->volume->cell);
  489. if (IS_ERR(key)) {
  490. ret = PTR_ERR(key);
  491. goto error;
  492. }
  493. }
  494. ret = -ERESTARTSYS;
  495. if (afs_begin_vnode_operation(&fc, vnode, key)) {
  496. while (afs_select_fileserver(&fc)) {
  497. fc.cb_break = afs_calc_vnode_cb_break(vnode);
  498. afs_fs_setattr(&fc, attr);
  499. }
  500. afs_check_for_remote_deletion(&fc, fc.vnode);
  501. afs_vnode_commit_status(&fc, vnode, fc.cb_break);
  502. ret = afs_end_vnode_operation(&fc);
  503. }
  504. if (!(attr->ia_valid & ATTR_FILE))
  505. key_put(key);
  506. error:
  507. _leave(" = %d", ret);
  508. return ret;
  509. }