inode.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912
  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. #include "afs_fs.h"
  26. static const struct inode_operations afs_symlink_inode_operations = {
  27. .get_link = page_get_link,
  28. };
  29. static noinline void dump_vnode(struct afs_vnode *vnode, struct afs_vnode *parent_vnode)
  30. {
  31. static unsigned long once_only;
  32. pr_warn("kAFS: AFS vnode with undefined type %u\n", vnode->status.type);
  33. pr_warn("kAFS: A=%d m=%o s=%llx v=%llx\n",
  34. vnode->status.abort_code,
  35. vnode->status.mode,
  36. vnode->status.size,
  37. vnode->status.data_version);
  38. pr_warn("kAFS: vnode %llx:%llx:%x\n",
  39. vnode->fid.vid,
  40. vnode->fid.vnode,
  41. vnode->fid.unique);
  42. if (parent_vnode)
  43. pr_warn("kAFS: dir %llx:%llx:%x\n",
  44. parent_vnode->fid.vid,
  45. parent_vnode->fid.vnode,
  46. parent_vnode->fid.unique);
  47. if (!test_and_set_bit(0, &once_only))
  48. dump_stack();
  49. }
  50. /*
  51. * Initialise an inode from the vnode status.
  52. */
  53. static int afs_inode_init_from_status(struct afs_operation *op,
  54. struct afs_vnode_param *vp,
  55. struct afs_vnode *vnode)
  56. {
  57. struct afs_file_status *status = &vp->scb.status;
  58. struct inode *inode = AFS_VNODE_TO_I(vnode);
  59. struct timespec64 t;
  60. _enter("{%llx:%llu.%u} %s",
  61. vp->fid.vid, vp->fid.vnode, vp->fid.unique,
  62. op->type ? op->type->name : "???");
  63. _debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu",
  64. status->type,
  65. status->nlink,
  66. (unsigned long long) status->size,
  67. status->data_version,
  68. status->mode);
  69. write_seqlock(&vnode->cb_lock);
  70. vnode->cb_v_break = op->cb_v_break;
  71. vnode->cb_s_break = op->cb_s_break;
  72. vnode->status = *status;
  73. t = status->mtime_client;
  74. inode->i_ctime = t;
  75. inode->i_mtime = t;
  76. inode->i_atime = t;
  77. inode->i_flags |= S_NOATIME;
  78. inode->i_uid = make_kuid(&init_user_ns, status->owner);
  79. inode->i_gid = make_kgid(&init_user_ns, status->group);
  80. set_nlink(&vnode->vfs_inode, status->nlink);
  81. switch (status->type) {
  82. case AFS_FTYPE_FILE:
  83. inode->i_mode = S_IFREG | (status->mode & S_IALLUGO);
  84. inode->i_op = &afs_file_inode_operations;
  85. inode->i_fop = &afs_file_operations;
  86. inode->i_mapping->a_ops = &afs_fs_aops;
  87. break;
  88. case AFS_FTYPE_DIR:
  89. inode->i_mode = S_IFDIR | (status->mode & S_IALLUGO);
  90. inode->i_op = &afs_dir_inode_operations;
  91. inode->i_fop = &afs_dir_file_operations;
  92. inode->i_mapping->a_ops = &afs_dir_aops;
  93. break;
  94. case AFS_FTYPE_SYMLINK:
  95. /* Symlinks with a mode of 0644 are actually mountpoints. */
  96. if ((status->mode & 0777) == 0644) {
  97. inode->i_flags |= S_AUTOMOUNT;
  98. set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
  99. inode->i_mode = S_IFDIR | 0555;
  100. inode->i_op = &afs_mntpt_inode_operations;
  101. inode->i_fop = &afs_mntpt_file_operations;
  102. inode->i_mapping->a_ops = &afs_fs_aops;
  103. } else {
  104. inode->i_mode = S_IFLNK | status->mode;
  105. inode->i_op = &afs_symlink_inode_operations;
  106. inode->i_mapping->a_ops = &afs_fs_aops;
  107. }
  108. inode_nohighmem(inode);
  109. break;
  110. default:
  111. dump_vnode(vnode, op->file[0].vnode != vnode ? op->file[0].vnode : NULL);
  112. write_sequnlock(&vnode->cb_lock);
  113. return afs_protocol_error(NULL, afs_eproto_file_type);
  114. }
  115. afs_set_i_size(vnode, status->size);
  116. vnode->invalid_before = status->data_version;
  117. inode_set_iversion_raw(&vnode->vfs_inode, status->data_version);
  118. if (!vp->scb.have_cb) {
  119. /* it's a symlink we just created (the fileserver
  120. * didn't give us a callback) */
  121. vnode->cb_expires_at = ktime_get_real_seconds();
  122. } else {
  123. vnode->cb_expires_at = vp->scb.callback.expires_at;
  124. vnode->cb_server = op->server;
  125. set_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
  126. }
  127. write_sequnlock(&vnode->cb_lock);
  128. return 0;
  129. }
  130. /*
  131. * Update the core inode struct from a returned status record.
  132. */
  133. static void afs_apply_status(struct afs_operation *op,
  134. struct afs_vnode_param *vp)
  135. {
  136. struct afs_file_status *status = &vp->scb.status;
  137. struct afs_vnode *vnode = vp->vnode;
  138. struct inode *inode = &vnode->vfs_inode;
  139. struct timespec64 t;
  140. umode_t mode;
  141. bool data_changed = false;
  142. bool change_size = vp->set_size;
  143. _enter("{%llx:%llu.%u} %s",
  144. vp->fid.vid, vp->fid.vnode, vp->fid.unique,
  145. op->type ? op->type->name : "???");
  146. BUG_ON(test_bit(AFS_VNODE_UNSET, &vnode->flags));
  147. if (status->type != vnode->status.type) {
  148. pr_warn("Vnode %llx:%llx:%x changed type %u to %u\n",
  149. vnode->fid.vid,
  150. vnode->fid.vnode,
  151. vnode->fid.unique,
  152. status->type, vnode->status.type);
  153. afs_protocol_error(NULL, afs_eproto_bad_status);
  154. return;
  155. }
  156. if (status->nlink != vnode->status.nlink)
  157. set_nlink(inode, status->nlink);
  158. if (status->owner != vnode->status.owner)
  159. inode->i_uid = make_kuid(&init_user_ns, status->owner);
  160. if (status->group != vnode->status.group)
  161. inode->i_gid = make_kgid(&init_user_ns, status->group);
  162. if (status->mode != vnode->status.mode) {
  163. mode = inode->i_mode;
  164. mode &= ~S_IALLUGO;
  165. mode |= status->mode & S_IALLUGO;
  166. WRITE_ONCE(inode->i_mode, mode);
  167. }
  168. t = status->mtime_client;
  169. inode->i_mtime = t;
  170. if (vp->update_ctime)
  171. inode->i_ctime = op->ctime;
  172. if (vnode->status.data_version != status->data_version)
  173. data_changed = true;
  174. vnode->status = *status;
  175. if (vp->dv_before + vp->dv_delta != status->data_version) {
  176. if (test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags))
  177. pr_warn("kAFS: vnode modified {%llx:%llu} %llx->%llx %s\n",
  178. vnode->fid.vid, vnode->fid.vnode,
  179. (unsigned long long)vp->dv_before + vp->dv_delta,
  180. (unsigned long long)status->data_version,
  181. op->type ? op->type->name : "???");
  182. vnode->invalid_before = status->data_version;
  183. if (vnode->status.type == AFS_FTYPE_DIR) {
  184. if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
  185. afs_stat_v(vnode, n_inval);
  186. } else {
  187. set_bit(AFS_VNODE_ZAP_DATA, &vnode->flags);
  188. }
  189. change_size = true;
  190. } else if (vnode->status.type == AFS_FTYPE_DIR) {
  191. /* Expected directory change is handled elsewhere so
  192. * that we can locally edit the directory and save on a
  193. * download.
  194. */
  195. if (test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
  196. data_changed = false;
  197. change_size = true;
  198. }
  199. if (data_changed) {
  200. inode_set_iversion_raw(inode, status->data_version);
  201. /* Only update the size if the data version jumped. If the
  202. * file is being modified locally, then we might have our own
  203. * idea of what the size should be that's not the same as
  204. * what's on the server.
  205. */
  206. if (change_size) {
  207. afs_set_i_size(vnode, status->size);
  208. inode->i_ctime = t;
  209. inode->i_atime = t;
  210. }
  211. }
  212. }
  213. /*
  214. * Apply a callback to a vnode.
  215. */
  216. static void afs_apply_callback(struct afs_operation *op,
  217. struct afs_vnode_param *vp)
  218. {
  219. struct afs_callback *cb = &vp->scb.callback;
  220. struct afs_vnode *vnode = vp->vnode;
  221. if (!afs_cb_is_broken(vp->cb_break_before, vnode)) {
  222. vnode->cb_expires_at = cb->expires_at;
  223. vnode->cb_server = op->server;
  224. set_bit(AFS_VNODE_CB_PROMISED, &vnode->flags);
  225. }
  226. }
  227. /*
  228. * Apply the received status and callback to an inode all in the same critical
  229. * section to avoid races with afs_validate().
  230. */
  231. void afs_vnode_commit_status(struct afs_operation *op, struct afs_vnode_param *vp)
  232. {
  233. struct afs_vnode *vnode = vp->vnode;
  234. _enter("");
  235. write_seqlock(&vnode->cb_lock);
  236. if (vp->scb.have_error) {
  237. /* A YFS server will return this from RemoveFile2 and AFS and
  238. * YFS will return this from InlineBulkStatus.
  239. */
  240. if (vp->scb.status.abort_code == VNOVNODE) {
  241. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  242. clear_nlink(&vnode->vfs_inode);
  243. __afs_break_callback(vnode, afs_cb_break_for_deleted);
  244. op->flags &= ~AFS_OPERATION_DIR_CONFLICT;
  245. }
  246. } else if (vp->scb.have_status) {
  247. if (vp->speculative &&
  248. (test_bit(AFS_VNODE_MODIFYING, &vnode->flags) ||
  249. vp->dv_before != vnode->status.data_version))
  250. /* Ignore the result of a speculative bulk status fetch
  251. * if it splits around a modification op, thereby
  252. * appearing to regress the data version.
  253. */
  254. goto out;
  255. afs_apply_status(op, vp);
  256. if (vp->scb.have_cb)
  257. afs_apply_callback(op, vp);
  258. } else if (vp->op_unlinked && !(op->flags & AFS_OPERATION_DIR_CONFLICT)) {
  259. drop_nlink(&vnode->vfs_inode);
  260. if (vnode->vfs_inode.i_nlink == 0) {
  261. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  262. __afs_break_callback(vnode, afs_cb_break_for_deleted);
  263. }
  264. }
  265. out:
  266. write_sequnlock(&vnode->cb_lock);
  267. if (vp->scb.have_status)
  268. afs_cache_permit(vnode, op->key, vp->cb_break_before, &vp->scb);
  269. }
  270. static void afs_fetch_status_success(struct afs_operation *op)
  271. {
  272. struct afs_vnode_param *vp = &op->file[op->fetch_status.which];
  273. struct afs_vnode *vnode = vp->vnode;
  274. int ret;
  275. if (vnode->vfs_inode.i_state & I_NEW) {
  276. ret = afs_inode_init_from_status(op, vp, vnode);
  277. op->error = ret;
  278. if (ret == 0)
  279. afs_cache_permit(vnode, op->key, vp->cb_break_before, &vp->scb);
  280. } else {
  281. afs_vnode_commit_status(op, vp);
  282. }
  283. }
  284. const struct afs_operation_ops afs_fetch_status_operation = {
  285. .issue_afs_rpc = afs_fs_fetch_status,
  286. .issue_yfs_rpc = yfs_fs_fetch_status,
  287. .success = afs_fetch_status_success,
  288. .aborted = afs_check_for_remote_deletion,
  289. };
  290. /*
  291. * Fetch file status from the volume.
  292. */
  293. int afs_fetch_status(struct afs_vnode *vnode, struct key *key, bool is_new,
  294. afs_access_t *_caller_access)
  295. {
  296. struct afs_operation *op;
  297. _enter("%s,{%llx:%llu.%u,S=%lx}",
  298. vnode->volume->name,
  299. vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique,
  300. vnode->flags);
  301. op = afs_alloc_operation(key, vnode->volume);
  302. if (IS_ERR(op))
  303. return PTR_ERR(op);
  304. afs_op_set_vnode(op, 0, vnode);
  305. op->nr_files = 1;
  306. op->ops = &afs_fetch_status_operation;
  307. afs_begin_vnode_operation(op);
  308. afs_wait_for_operation(op);
  309. if (_caller_access)
  310. *_caller_access = op->file[0].scb.status.caller_access;
  311. return afs_put_operation(op);
  312. }
  313. /*
  314. * ilookup() comparator
  315. */
  316. int afs_ilookup5_test_by_fid(struct inode *inode, void *opaque)
  317. {
  318. struct afs_vnode *vnode = AFS_FS_I(inode);
  319. struct afs_fid *fid = opaque;
  320. return (fid->vnode == vnode->fid.vnode &&
  321. fid->vnode_hi == vnode->fid.vnode_hi &&
  322. fid->unique == vnode->fid.unique);
  323. }
  324. /*
  325. * iget5() comparator
  326. */
  327. static int afs_iget5_test(struct inode *inode, void *opaque)
  328. {
  329. struct afs_vnode_param *vp = opaque;
  330. //struct afs_vnode *vnode = AFS_FS_I(inode);
  331. return afs_ilookup5_test_by_fid(inode, &vp->fid);
  332. }
  333. /*
  334. * iget5() inode initialiser
  335. */
  336. static int afs_iget5_set(struct inode *inode, void *opaque)
  337. {
  338. struct afs_vnode_param *vp = opaque;
  339. struct afs_super_info *as = AFS_FS_S(inode->i_sb);
  340. struct afs_vnode *vnode = AFS_FS_I(inode);
  341. vnode->volume = as->volume;
  342. vnode->fid = vp->fid;
  343. /* YFS supports 96-bit vnode IDs, but Linux only supports
  344. * 64-bit inode numbers.
  345. */
  346. inode->i_ino = vnode->fid.vnode;
  347. inode->i_generation = vnode->fid.unique;
  348. return 0;
  349. }
  350. /*
  351. * Get a cache cookie for an inode.
  352. */
  353. static void afs_get_inode_cache(struct afs_vnode *vnode)
  354. {
  355. #ifdef CONFIG_AFS_FSCACHE
  356. struct {
  357. u32 vnode_id;
  358. u32 unique;
  359. u32 vnode_id_ext[2]; /* Allow for a 96-bit key */
  360. } __packed key;
  361. struct afs_vnode_cache_aux aux;
  362. if (vnode->status.type == AFS_FTYPE_DIR) {
  363. vnode->cache = NULL;
  364. return;
  365. }
  366. key.vnode_id = vnode->fid.vnode;
  367. key.unique = vnode->fid.unique;
  368. key.vnode_id_ext[0] = vnode->fid.vnode >> 32;
  369. key.vnode_id_ext[1] = vnode->fid.vnode_hi;
  370. aux.data_version = vnode->status.data_version;
  371. vnode->cache = fscache_acquire_cookie(vnode->volume->cache,
  372. &afs_vnode_cache_index_def,
  373. &key, sizeof(key),
  374. &aux, sizeof(aux),
  375. vnode, vnode->status.size, true);
  376. #endif
  377. }
  378. /*
  379. * inode retrieval
  380. */
  381. struct inode *afs_iget(struct afs_operation *op, struct afs_vnode_param *vp)
  382. {
  383. struct afs_vnode_param *dvp = &op->file[0];
  384. struct super_block *sb = dvp->vnode->vfs_inode.i_sb;
  385. struct afs_vnode *vnode;
  386. struct inode *inode;
  387. int ret;
  388. _enter(",{%llx:%llu.%u},,", vp->fid.vid, vp->fid.vnode, vp->fid.unique);
  389. inode = iget5_locked(sb, vp->fid.vnode, afs_iget5_test, afs_iget5_set, vp);
  390. if (!inode) {
  391. _leave(" = -ENOMEM");
  392. return ERR_PTR(-ENOMEM);
  393. }
  394. vnode = AFS_FS_I(inode);
  395. _debug("GOT INODE %p { vl=%llx vn=%llx, u=%x }",
  396. inode, vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
  397. /* deal with an existing inode */
  398. if (!(inode->i_state & I_NEW)) {
  399. _leave(" = %p", inode);
  400. return inode;
  401. }
  402. ret = afs_inode_init_from_status(op, vp, vnode);
  403. if (ret < 0)
  404. goto bad_inode;
  405. afs_get_inode_cache(vnode);
  406. /* success */
  407. clear_bit(AFS_VNODE_UNSET, &vnode->flags);
  408. unlock_new_inode(inode);
  409. _leave(" = %p", inode);
  410. return inode;
  411. /* failure */
  412. bad_inode:
  413. iget_failed(inode);
  414. _leave(" = %d [bad]", ret);
  415. return ERR_PTR(ret);
  416. }
  417. static int afs_iget5_set_root(struct inode *inode, void *opaque)
  418. {
  419. struct afs_super_info *as = AFS_FS_S(inode->i_sb);
  420. struct afs_vnode *vnode = AFS_FS_I(inode);
  421. vnode->volume = as->volume;
  422. vnode->fid.vid = as->volume->vid,
  423. vnode->fid.vnode = 1;
  424. vnode->fid.unique = 1;
  425. inode->i_ino = 1;
  426. inode->i_generation = 1;
  427. return 0;
  428. }
  429. /*
  430. * Set up the root inode for a volume. This is always vnode 1, unique 1 within
  431. * the volume.
  432. */
  433. struct inode *afs_root_iget(struct super_block *sb, struct key *key)
  434. {
  435. struct afs_super_info *as = AFS_FS_S(sb);
  436. struct afs_operation *op;
  437. struct afs_vnode *vnode;
  438. struct inode *inode;
  439. int ret;
  440. _enter(",{%llx},,", as->volume->vid);
  441. inode = iget5_locked(sb, 1, NULL, afs_iget5_set_root, NULL);
  442. if (!inode) {
  443. _leave(" = -ENOMEM");
  444. return ERR_PTR(-ENOMEM);
  445. }
  446. _debug("GOT ROOT INODE %p { vl=%llx }", inode, as->volume->vid);
  447. BUG_ON(!(inode->i_state & I_NEW));
  448. vnode = AFS_FS_I(inode);
  449. vnode->cb_v_break = as->volume->cb_v_break,
  450. op = afs_alloc_operation(key, as->volume);
  451. if (IS_ERR(op)) {
  452. ret = PTR_ERR(op);
  453. goto error;
  454. }
  455. afs_op_set_vnode(op, 0, vnode);
  456. op->nr_files = 1;
  457. op->ops = &afs_fetch_status_operation;
  458. ret = afs_do_sync_operation(op);
  459. if (ret < 0)
  460. goto error;
  461. afs_get_inode_cache(vnode);
  462. clear_bit(AFS_VNODE_UNSET, &vnode->flags);
  463. unlock_new_inode(inode);
  464. _leave(" = %p", inode);
  465. return inode;
  466. error:
  467. iget_failed(inode);
  468. _leave(" = %d [bad]", ret);
  469. return ERR_PTR(ret);
  470. }
  471. /*
  472. * mark the data attached to an inode as obsolete due to a write on the server
  473. * - might also want to ditch all the outstanding writes and dirty pages
  474. */
  475. static void afs_zap_data(struct afs_vnode *vnode)
  476. {
  477. _enter("{%llx:%llu}", vnode->fid.vid, vnode->fid.vnode);
  478. #ifdef CONFIG_AFS_FSCACHE
  479. fscache_invalidate(vnode->cache);
  480. #endif
  481. /* nuke all the non-dirty pages that aren't locked, mapped or being
  482. * written back in a regular file and completely discard the pages in a
  483. * directory or symlink */
  484. if (S_ISREG(vnode->vfs_inode.i_mode))
  485. invalidate_remote_inode(&vnode->vfs_inode);
  486. else
  487. invalidate_inode_pages2(vnode->vfs_inode.i_mapping);
  488. }
  489. /*
  490. * Get the server reinit counter for a vnode's current server.
  491. */
  492. static bool afs_get_s_break_rcu(struct afs_vnode *vnode, unsigned int *_s_break)
  493. {
  494. struct afs_server_list *slist = rcu_dereference(vnode->volume->servers);
  495. struct afs_server *server;
  496. int i;
  497. for (i = 0; i < slist->nr_servers; i++) {
  498. server = slist->servers[i].server;
  499. if (server == vnode->cb_server) {
  500. *_s_break = READ_ONCE(server->cb_s_break);
  501. return true;
  502. }
  503. }
  504. return false;
  505. }
  506. /*
  507. * Check the validity of a vnode/inode.
  508. */
  509. bool afs_check_validity(struct afs_vnode *vnode)
  510. {
  511. struct afs_volume *volume = vnode->volume;
  512. enum afs_cb_break_reason need_clear = afs_cb_break_no_break;
  513. time64_t now = ktime_get_real_seconds();
  514. bool valid;
  515. unsigned int cb_break, cb_s_break, cb_v_break;
  516. int seq = 0;
  517. do {
  518. read_seqbegin_or_lock(&vnode->cb_lock, &seq);
  519. cb_v_break = READ_ONCE(volume->cb_v_break);
  520. cb_break = vnode->cb_break;
  521. if (test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags) &&
  522. afs_get_s_break_rcu(vnode, &cb_s_break)) {
  523. if (vnode->cb_s_break != cb_s_break ||
  524. vnode->cb_v_break != cb_v_break) {
  525. vnode->cb_s_break = cb_s_break;
  526. vnode->cb_v_break = cb_v_break;
  527. need_clear = afs_cb_break_for_vsbreak;
  528. valid = false;
  529. } else if (test_bit(AFS_VNODE_ZAP_DATA, &vnode->flags)) {
  530. need_clear = afs_cb_break_for_zap;
  531. valid = false;
  532. } else if (vnode->cb_expires_at - 10 <= now) {
  533. need_clear = afs_cb_break_for_lapsed;
  534. valid = false;
  535. } else {
  536. valid = true;
  537. }
  538. } else if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
  539. valid = true;
  540. } else {
  541. vnode->cb_v_break = cb_v_break;
  542. valid = false;
  543. }
  544. } while (need_seqretry(&vnode->cb_lock, seq));
  545. done_seqretry(&vnode->cb_lock, seq);
  546. if (need_clear != afs_cb_break_no_break) {
  547. write_seqlock(&vnode->cb_lock);
  548. if (cb_break == vnode->cb_break)
  549. __afs_break_callback(vnode, need_clear);
  550. else
  551. trace_afs_cb_miss(&vnode->fid, need_clear);
  552. write_sequnlock(&vnode->cb_lock);
  553. valid = false;
  554. }
  555. return valid;
  556. }
  557. /*
  558. * validate a vnode/inode
  559. * - there are several things we need to check
  560. * - parent dir data changes (rm, rmdir, rename, mkdir, create, link,
  561. * symlink)
  562. * - parent dir metadata changed (security changes)
  563. * - dentry data changed (write, truncate)
  564. * - dentry metadata changed (security changes)
  565. */
  566. int afs_validate(struct afs_vnode *vnode, struct key *key)
  567. {
  568. bool valid;
  569. int ret;
  570. _enter("{v={%llx:%llu} fl=%lx},%x",
  571. vnode->fid.vid, vnode->fid.vnode, vnode->flags,
  572. key_serial(key));
  573. rcu_read_lock();
  574. valid = afs_check_validity(vnode);
  575. rcu_read_unlock();
  576. if (test_bit(AFS_VNODE_DELETED, &vnode->flags))
  577. clear_nlink(&vnode->vfs_inode);
  578. if (valid)
  579. goto valid;
  580. down_write(&vnode->validate_lock);
  581. /* if the promise has expired, we need to check the server again to get
  582. * a new promise - note that if the (parent) directory's metadata was
  583. * changed then the security may be different and we may no longer have
  584. * access */
  585. if (!test_bit(AFS_VNODE_CB_PROMISED, &vnode->flags)) {
  586. _debug("not promised");
  587. ret = afs_fetch_status(vnode, key, false, NULL);
  588. if (ret < 0) {
  589. if (ret == -ENOENT) {
  590. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  591. ret = -ESTALE;
  592. }
  593. goto error_unlock;
  594. }
  595. _debug("new promise [fl=%lx]", vnode->flags);
  596. }
  597. if (test_bit(AFS_VNODE_DELETED, &vnode->flags)) {
  598. _debug("file already deleted");
  599. ret = -ESTALE;
  600. goto error_unlock;
  601. }
  602. /* if the vnode's data version number changed then its contents are
  603. * different */
  604. if (test_and_clear_bit(AFS_VNODE_ZAP_DATA, &vnode->flags))
  605. afs_zap_data(vnode);
  606. up_write(&vnode->validate_lock);
  607. valid:
  608. _leave(" = 0");
  609. return 0;
  610. error_unlock:
  611. up_write(&vnode->validate_lock);
  612. _leave(" = %d", ret);
  613. return ret;
  614. }
  615. /*
  616. * read the attributes of an inode
  617. */
  618. int afs_getattr(const struct path *path, struct kstat *stat,
  619. u32 request_mask, unsigned int query_flags)
  620. {
  621. struct inode *inode = d_inode(path->dentry);
  622. struct afs_vnode *vnode = AFS_FS_I(inode);
  623. int seq = 0;
  624. _enter("{ ino=%lu v=%u }", inode->i_ino, inode->i_generation);
  625. do {
  626. read_seqbegin_or_lock(&vnode->cb_lock, &seq);
  627. generic_fillattr(inode, stat);
  628. if (test_bit(AFS_VNODE_SILLY_DELETED, &vnode->flags) &&
  629. stat->nlink > 0)
  630. stat->nlink -= 1;
  631. } while (need_seqretry(&vnode->cb_lock, seq));
  632. done_seqretry(&vnode->cb_lock, seq);
  633. return 0;
  634. }
  635. /*
  636. * discard an AFS inode
  637. */
  638. int afs_drop_inode(struct inode *inode)
  639. {
  640. _enter("");
  641. if (test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(inode)->flags))
  642. return generic_delete_inode(inode);
  643. else
  644. return generic_drop_inode(inode);
  645. }
  646. /*
  647. * clear an AFS inode
  648. */
  649. void afs_evict_inode(struct inode *inode)
  650. {
  651. struct afs_vnode *vnode;
  652. vnode = AFS_FS_I(inode);
  653. _enter("{%llx:%llu.%d}",
  654. vnode->fid.vid,
  655. vnode->fid.vnode,
  656. vnode->fid.unique);
  657. _debug("CLEAR INODE %p", inode);
  658. ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
  659. truncate_inode_pages_final(&inode->i_data);
  660. clear_inode(inode);
  661. while (!list_empty(&vnode->wb_keys)) {
  662. struct afs_wb_key *wbk = list_entry(vnode->wb_keys.next,
  663. struct afs_wb_key, vnode_link);
  664. list_del(&wbk->vnode_link);
  665. afs_put_wb_key(wbk);
  666. }
  667. #ifdef CONFIG_AFS_FSCACHE
  668. {
  669. struct afs_vnode_cache_aux aux;
  670. aux.data_version = vnode->status.data_version;
  671. fscache_relinquish_cookie(vnode->cache, &aux,
  672. test_bit(AFS_VNODE_DELETED, &vnode->flags));
  673. vnode->cache = NULL;
  674. }
  675. #endif
  676. afs_prune_wb_keys(vnode);
  677. afs_put_permits(rcu_access_pointer(vnode->permit_cache));
  678. key_put(vnode->silly_key);
  679. vnode->silly_key = NULL;
  680. key_put(vnode->lock_key);
  681. vnode->lock_key = NULL;
  682. _leave("");
  683. }
  684. static void afs_setattr_success(struct afs_operation *op)
  685. {
  686. struct afs_vnode_param *vp = &op->file[0];
  687. struct inode *inode = &vp->vnode->vfs_inode;
  688. loff_t old_i_size = i_size_read(inode);
  689. op->setattr.old_i_size = old_i_size;
  690. afs_vnode_commit_status(op, vp);
  691. /* inode->i_size has now been changed. */
  692. if (op->setattr.attr->ia_valid & ATTR_SIZE) {
  693. loff_t size = op->setattr.attr->ia_size;
  694. if (size > old_i_size)
  695. pagecache_isize_extended(inode, old_i_size, size);
  696. }
  697. }
  698. static void afs_setattr_edit_file(struct afs_operation *op)
  699. {
  700. struct afs_vnode_param *vp = &op->file[0];
  701. struct inode *inode = &vp->vnode->vfs_inode;
  702. if (op->setattr.attr->ia_valid & ATTR_SIZE) {
  703. loff_t size = op->setattr.attr->ia_size;
  704. loff_t i_size = op->setattr.old_i_size;
  705. if (size < i_size)
  706. truncate_pagecache(inode, size);
  707. }
  708. }
  709. static const struct afs_operation_ops afs_setattr_operation = {
  710. .issue_afs_rpc = afs_fs_setattr,
  711. .issue_yfs_rpc = yfs_fs_setattr,
  712. .success = afs_setattr_success,
  713. .edit_dir = afs_setattr_edit_file,
  714. };
  715. /*
  716. * set the attributes of an inode
  717. */
  718. int afs_setattr(struct dentry *dentry, struct iattr *attr)
  719. {
  720. struct afs_operation *op;
  721. struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
  722. int ret;
  723. _enter("{%llx:%llu},{n=%pd},%x",
  724. vnode->fid.vid, vnode->fid.vnode, dentry,
  725. attr->ia_valid);
  726. if (!(attr->ia_valid & (ATTR_SIZE | ATTR_MODE | ATTR_UID | ATTR_GID |
  727. ATTR_MTIME | ATTR_MTIME_SET | ATTR_TIMES_SET |
  728. ATTR_TOUCH))) {
  729. _leave(" = 0 [unsupported]");
  730. return 0;
  731. }
  732. if (attr->ia_valid & ATTR_SIZE) {
  733. if (!S_ISREG(vnode->vfs_inode.i_mode))
  734. return -EISDIR;
  735. ret = inode_newsize_ok(&vnode->vfs_inode, attr->ia_size);
  736. if (ret)
  737. return ret;
  738. if (attr->ia_size == i_size_read(&vnode->vfs_inode))
  739. attr->ia_valid &= ~ATTR_SIZE;
  740. }
  741. /* flush any dirty data outstanding on a regular file */
  742. if (S_ISREG(vnode->vfs_inode.i_mode))
  743. filemap_write_and_wait(vnode->vfs_inode.i_mapping);
  744. /* Prevent any new writebacks from starting whilst we do this. */
  745. down_write(&vnode->validate_lock);
  746. op = afs_alloc_operation(((attr->ia_valid & ATTR_FILE) ?
  747. afs_file_key(attr->ia_file) : NULL),
  748. vnode->volume);
  749. if (IS_ERR(op)) {
  750. ret = PTR_ERR(op);
  751. goto out_unlock;
  752. }
  753. afs_op_set_vnode(op, 0, vnode);
  754. op->setattr.attr = attr;
  755. if (attr->ia_valid & ATTR_SIZE) {
  756. op->file[0].dv_delta = 1;
  757. op->file[0].set_size = true;
  758. }
  759. op->ctime = attr->ia_ctime;
  760. op->file[0].update_ctime = 1;
  761. op->file[0].modification = true;
  762. op->ops = &afs_setattr_operation;
  763. ret = afs_do_sync_operation(op);
  764. out_unlock:
  765. up_write(&vnode->validate_lock);
  766. _leave(" = %d", ret);
  767. return ret;
  768. }