inode.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817
  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. * Set parameters for the netfs library
  52. */
  53. static void afs_set_netfs_context(struct afs_vnode *vnode)
  54. {
  55. netfs_inode_init(&vnode->netfs, &afs_req_ops, true);
  56. }
  57. /*
  58. * Initialise an inode from the vnode status.
  59. */
  60. static int afs_inode_init_from_status(struct afs_operation *op,
  61. struct afs_vnode_param *vp,
  62. struct afs_vnode *vnode)
  63. {
  64. struct afs_file_status *status = &vp->scb.status;
  65. struct inode *inode = AFS_VNODE_TO_I(vnode);
  66. struct timespec64 t;
  67. _enter("{%llx:%llu.%u} %s",
  68. vp->fid.vid, vp->fid.vnode, vp->fid.unique,
  69. op->type ? op->type->name : "???");
  70. _debug("FS: ft=%d lk=%d sz=%llu ver=%Lu mod=%hu",
  71. status->type,
  72. status->nlink,
  73. (unsigned long long) status->size,
  74. status->data_version,
  75. status->mode);
  76. write_seqlock(&vnode->cb_lock);
  77. vnode->cb_v_check = op->cb_v_break;
  78. vnode->status = *status;
  79. t = status->mtime_client;
  80. inode_set_ctime_to_ts(inode, t);
  81. inode_set_mtime_to_ts(inode, t);
  82. inode_set_atime_to_ts(inode, t);
  83. inode->i_flags |= S_NOATIME;
  84. inode->i_uid = make_kuid(&init_user_ns, status->owner);
  85. inode->i_gid = make_kgid(&init_user_ns, status->group);
  86. set_nlink(&vnode->netfs.inode, status->nlink);
  87. switch (status->type) {
  88. case AFS_FTYPE_FILE:
  89. inode->i_mode = S_IFREG | (status->mode & S_IALLUGO);
  90. inode->i_op = &afs_file_inode_operations;
  91. inode->i_fop = &afs_file_operations;
  92. inode->i_mapping->a_ops = &afs_file_aops;
  93. mapping_set_large_folios(inode->i_mapping);
  94. break;
  95. case AFS_FTYPE_DIR:
  96. inode->i_mode = S_IFDIR | (status->mode & S_IALLUGO);
  97. inode->i_op = &afs_dir_inode_operations;
  98. inode->i_fop = &afs_dir_file_operations;
  99. inode->i_mapping->a_ops = &afs_dir_aops;
  100. mapping_set_large_folios(inode->i_mapping);
  101. break;
  102. case AFS_FTYPE_SYMLINK:
  103. /* Symlinks with a mode of 0644 are actually mountpoints. */
  104. if ((status->mode & 0777) == 0644) {
  105. inode->i_flags |= S_AUTOMOUNT;
  106. set_bit(AFS_VNODE_MOUNTPOINT, &vnode->flags);
  107. inode->i_mode = S_IFDIR | 0555;
  108. inode->i_op = &afs_mntpt_inode_operations;
  109. inode->i_fop = &afs_mntpt_file_operations;
  110. inode->i_mapping->a_ops = &afs_symlink_aops;
  111. } else {
  112. inode->i_mode = S_IFLNK | status->mode;
  113. inode->i_op = &afs_symlink_inode_operations;
  114. inode->i_mapping->a_ops = &afs_symlink_aops;
  115. }
  116. inode_nohighmem(inode);
  117. break;
  118. default:
  119. dump_vnode(vnode, op->file[0].vnode != vnode ? op->file[0].vnode : NULL);
  120. write_sequnlock(&vnode->cb_lock);
  121. return afs_protocol_error(NULL, afs_eproto_file_type);
  122. }
  123. afs_set_i_size(vnode, status->size);
  124. afs_set_netfs_context(vnode);
  125. vnode->invalid_before = status->data_version;
  126. inode_set_iversion_raw(&vnode->netfs.inode, status->data_version);
  127. if (!vp->scb.have_cb) {
  128. /* it's a symlink we just created (the fileserver
  129. * didn't give us a callback) */
  130. atomic64_set(&vnode->cb_expires_at, AFS_NO_CB_PROMISE);
  131. } else {
  132. vnode->cb_server = op->server;
  133. atomic64_set(&vnode->cb_expires_at, vp->scb.callback.expires_at);
  134. }
  135. write_sequnlock(&vnode->cb_lock);
  136. return 0;
  137. }
  138. /*
  139. * Update the core inode struct from a returned status record.
  140. */
  141. static void afs_apply_status(struct afs_operation *op,
  142. struct afs_vnode_param *vp)
  143. {
  144. struct afs_file_status *status = &vp->scb.status;
  145. struct afs_vnode *vnode = vp->vnode;
  146. struct inode *inode = &vnode->netfs.inode;
  147. struct timespec64 t;
  148. umode_t mode;
  149. bool unexpected_jump = false;
  150. bool data_changed = false;
  151. bool change_size = vp->set_size;
  152. _enter("{%llx:%llu.%u} %s",
  153. vp->fid.vid, vp->fid.vnode, vp->fid.unique,
  154. op->type ? op->type->name : "???");
  155. BUG_ON(test_bit(AFS_VNODE_UNSET, &vnode->flags));
  156. if (status->type != vnode->status.type) {
  157. pr_warn("Vnode %llx:%llx:%x changed type %u to %u\n",
  158. vnode->fid.vid,
  159. vnode->fid.vnode,
  160. vnode->fid.unique,
  161. status->type, vnode->status.type);
  162. afs_protocol_error(NULL, afs_eproto_bad_status);
  163. return;
  164. }
  165. if (status->nlink != vnode->status.nlink)
  166. set_nlink(inode, status->nlink);
  167. if (status->owner != vnode->status.owner)
  168. inode->i_uid = make_kuid(&init_user_ns, status->owner);
  169. if (status->group != vnode->status.group)
  170. inode->i_gid = make_kgid(&init_user_ns, status->group);
  171. if (status->mode != vnode->status.mode) {
  172. mode = inode->i_mode;
  173. mode &= ~S_IALLUGO;
  174. mode |= status->mode & S_IALLUGO;
  175. WRITE_ONCE(inode->i_mode, mode);
  176. }
  177. t = status->mtime_client;
  178. inode_set_mtime_to_ts(inode, t);
  179. if (vp->update_ctime)
  180. inode_set_ctime_to_ts(inode, op->ctime);
  181. if (vnode->status.data_version != status->data_version)
  182. data_changed = true;
  183. vnode->status = *status;
  184. if (vp->dv_before + vp->dv_delta != status->data_version) {
  185. if (vnode->cb_ro_snapshot == atomic_read(&vnode->volume->cb_ro_snapshot) &&
  186. atomic64_read(&vnode->cb_expires_at) != AFS_NO_CB_PROMISE)
  187. pr_warn("kAFS: vnode modified {%llx:%llu} %llx->%llx %s (op=%x)\n",
  188. vnode->fid.vid, vnode->fid.vnode,
  189. (unsigned long long)vp->dv_before + vp->dv_delta,
  190. (unsigned long long)status->data_version,
  191. op->type ? op->type->name : "???",
  192. op->debug_id);
  193. vnode->invalid_before = status->data_version;
  194. if (vnode->status.type == AFS_FTYPE_DIR) {
  195. if (test_and_clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
  196. afs_stat_v(vnode, n_inval);
  197. } else {
  198. set_bit(AFS_VNODE_ZAP_DATA, &vnode->flags);
  199. }
  200. change_size = true;
  201. data_changed = true;
  202. unexpected_jump = true;
  203. } else if (vnode->status.type == AFS_FTYPE_DIR) {
  204. /* Expected directory change is handled elsewhere so
  205. * that we can locally edit the directory and save on a
  206. * download.
  207. */
  208. if (test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
  209. data_changed = false;
  210. change_size = true;
  211. }
  212. if (data_changed) {
  213. inode_set_iversion_raw(inode, status->data_version);
  214. /* Only update the size if the data version jumped. If the
  215. * file is being modified locally, then we might have our own
  216. * idea of what the size should be that's not the same as
  217. * what's on the server.
  218. */
  219. vnode->netfs.remote_i_size = status->size;
  220. if (change_size || status->size > i_size_read(inode)) {
  221. afs_set_i_size(vnode, status->size);
  222. if (unexpected_jump)
  223. vnode->netfs.zero_point = status->size;
  224. inode_set_ctime_to_ts(inode, t);
  225. inode_set_atime_to_ts(inode, t);
  226. }
  227. }
  228. }
  229. /*
  230. * Apply a callback to a vnode.
  231. */
  232. static void afs_apply_callback(struct afs_operation *op,
  233. struct afs_vnode_param *vp)
  234. {
  235. struct afs_callback *cb = &vp->scb.callback;
  236. struct afs_vnode *vnode = vp->vnode;
  237. if (!afs_cb_is_broken(vp->cb_break_before, vnode)) {
  238. if (op->volume->type == AFSVL_RWVOL)
  239. vnode->cb_server = op->server;
  240. atomic64_set(&vnode->cb_expires_at, cb->expires_at);
  241. }
  242. }
  243. /*
  244. * Apply the received status and callback to an inode all in the same critical
  245. * section to avoid races with afs_validate().
  246. */
  247. void afs_vnode_commit_status(struct afs_operation *op, struct afs_vnode_param *vp)
  248. {
  249. struct afs_vnode *vnode = vp->vnode;
  250. _enter("");
  251. write_seqlock(&vnode->cb_lock);
  252. if (vp->scb.have_error) {
  253. /* A YFS server will return this from RemoveFile2 and AFS and
  254. * YFS will return this from InlineBulkStatus.
  255. */
  256. if (vp->scb.status.abort_code == VNOVNODE) {
  257. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  258. clear_nlink(&vnode->netfs.inode);
  259. __afs_break_callback(vnode, afs_cb_break_for_deleted);
  260. op->flags &= ~AFS_OPERATION_DIR_CONFLICT;
  261. }
  262. } else if (vp->scb.have_status) {
  263. if (vp->speculative &&
  264. (test_bit(AFS_VNODE_MODIFYING, &vnode->flags) ||
  265. vp->dv_before != vnode->status.data_version))
  266. /* Ignore the result of a speculative bulk status fetch
  267. * if it splits around a modification op, thereby
  268. * appearing to regress the data version.
  269. */
  270. goto out;
  271. afs_apply_status(op, vp);
  272. if (vp->scb.have_cb)
  273. afs_apply_callback(op, vp);
  274. } else if (vp->op_unlinked && !(op->flags & AFS_OPERATION_DIR_CONFLICT)) {
  275. drop_nlink(&vnode->netfs.inode);
  276. if (vnode->netfs.inode.i_nlink == 0) {
  277. set_bit(AFS_VNODE_DELETED, &vnode->flags);
  278. __afs_break_callback(vnode, afs_cb_break_for_deleted);
  279. }
  280. }
  281. out:
  282. write_sequnlock(&vnode->cb_lock);
  283. if (vp->scb.have_status)
  284. afs_cache_permit(vnode, op->key, vp->cb_break_before, &vp->scb);
  285. }
  286. static void afs_fetch_status_success(struct afs_operation *op)
  287. {
  288. struct afs_vnode_param *vp = &op->file[op->fetch_status.which];
  289. struct afs_vnode *vnode = vp->vnode;
  290. int ret;
  291. if (vnode->netfs.inode.i_state & I_NEW) {
  292. ret = afs_inode_init_from_status(op, vp, vnode);
  293. afs_op_set_error(op, ret);
  294. if (ret == 0)
  295. afs_cache_permit(vnode, op->key, vp->cb_break_before, &vp->scb);
  296. } else {
  297. afs_vnode_commit_status(op, vp);
  298. }
  299. }
  300. const struct afs_operation_ops afs_fetch_status_operation = {
  301. .issue_afs_rpc = afs_fs_fetch_status,
  302. .issue_yfs_rpc = yfs_fs_fetch_status,
  303. .success = afs_fetch_status_success,
  304. .aborted = afs_check_for_remote_deletion,
  305. };
  306. /*
  307. * Fetch file status from the volume.
  308. */
  309. int afs_fetch_status(struct afs_vnode *vnode, struct key *key, bool is_new,
  310. afs_access_t *_caller_access)
  311. {
  312. struct afs_operation *op;
  313. _enter("%s,{%llx:%llu.%u,S=%lx}",
  314. vnode->volume->name,
  315. vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique,
  316. vnode->flags);
  317. op = afs_alloc_operation(key, vnode->volume);
  318. if (IS_ERR(op))
  319. return PTR_ERR(op);
  320. afs_op_set_vnode(op, 0, vnode);
  321. op->nr_files = 1;
  322. op->ops = &afs_fetch_status_operation;
  323. afs_begin_vnode_operation(op);
  324. afs_wait_for_operation(op);
  325. if (_caller_access)
  326. *_caller_access = op->file[0].scb.status.caller_access;
  327. return afs_put_operation(op);
  328. }
  329. /*
  330. * ilookup() comparator
  331. */
  332. int afs_ilookup5_test_by_fid(struct inode *inode, void *opaque)
  333. {
  334. struct afs_vnode *vnode = AFS_FS_I(inode);
  335. struct afs_fid *fid = opaque;
  336. return (fid->vnode == vnode->fid.vnode &&
  337. fid->vnode_hi == vnode->fid.vnode_hi &&
  338. fid->unique == vnode->fid.unique);
  339. }
  340. /*
  341. * iget5() comparator
  342. */
  343. static int afs_iget5_test(struct inode *inode, void *opaque)
  344. {
  345. struct afs_vnode_param *vp = opaque;
  346. //struct afs_vnode *vnode = AFS_FS_I(inode);
  347. return afs_ilookup5_test_by_fid(inode, &vp->fid);
  348. }
  349. /*
  350. * iget5() inode initialiser
  351. */
  352. static int afs_iget5_set(struct inode *inode, void *opaque)
  353. {
  354. struct afs_vnode_param *vp = opaque;
  355. struct afs_super_info *as = AFS_FS_S(inode->i_sb);
  356. struct afs_vnode *vnode = AFS_FS_I(inode);
  357. vnode->volume = as->volume;
  358. vnode->fid = vp->fid;
  359. /* YFS supports 96-bit vnode IDs, but Linux only supports
  360. * 64-bit inode numbers.
  361. */
  362. inode->i_ino = vnode->fid.vnode;
  363. inode->i_generation = vnode->fid.unique;
  364. return 0;
  365. }
  366. /*
  367. * Get a cache cookie for an inode.
  368. */
  369. static void afs_get_inode_cache(struct afs_vnode *vnode)
  370. {
  371. #ifdef CONFIG_AFS_FSCACHE
  372. struct {
  373. __be32 vnode_id;
  374. __be32 unique;
  375. __be32 vnode_id_ext[2]; /* Allow for a 96-bit key */
  376. } __packed key;
  377. struct afs_vnode_cache_aux aux;
  378. if (vnode->status.type != AFS_FTYPE_FILE) {
  379. vnode->netfs.cache = NULL;
  380. return;
  381. }
  382. key.vnode_id = htonl(vnode->fid.vnode);
  383. key.unique = htonl(vnode->fid.unique);
  384. key.vnode_id_ext[0] = htonl(vnode->fid.vnode >> 32);
  385. key.vnode_id_ext[1] = htonl(vnode->fid.vnode_hi);
  386. afs_set_cache_aux(vnode, &aux);
  387. afs_vnode_set_cache(vnode,
  388. fscache_acquire_cookie(
  389. vnode->volume->cache,
  390. vnode->status.type == AFS_FTYPE_FILE ?
  391. 0 : FSCACHE_ADV_SINGLE_CHUNK,
  392. &key, sizeof(key),
  393. &aux, sizeof(aux),
  394. i_size_read(&vnode->netfs.inode)));
  395. #endif
  396. }
  397. /*
  398. * inode retrieval
  399. */
  400. struct inode *afs_iget(struct afs_operation *op, struct afs_vnode_param *vp)
  401. {
  402. struct afs_vnode_param *dvp = &op->file[0];
  403. struct super_block *sb = dvp->vnode->netfs.inode.i_sb;
  404. struct afs_vnode *vnode;
  405. struct inode *inode;
  406. int ret;
  407. _enter(",{%llx:%llu.%u},,", vp->fid.vid, vp->fid.vnode, vp->fid.unique);
  408. inode = iget5_locked(sb, vp->fid.vnode, afs_iget5_test, afs_iget5_set, vp);
  409. if (!inode) {
  410. _leave(" = -ENOMEM");
  411. return ERR_PTR(-ENOMEM);
  412. }
  413. vnode = AFS_FS_I(inode);
  414. _debug("GOT INODE %p { vl=%llx vn=%llx, u=%x }",
  415. inode, vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique);
  416. /* deal with an existing inode */
  417. if (!(inode->i_state & I_NEW)) {
  418. _leave(" = %p", inode);
  419. return inode;
  420. }
  421. ret = afs_inode_init_from_status(op, vp, vnode);
  422. if (ret < 0)
  423. goto bad_inode;
  424. afs_get_inode_cache(vnode);
  425. /* success */
  426. clear_bit(AFS_VNODE_UNSET, &vnode->flags);
  427. unlock_new_inode(inode);
  428. _leave(" = %p", inode);
  429. return inode;
  430. /* failure */
  431. bad_inode:
  432. iget_failed(inode);
  433. _leave(" = %d [bad]", ret);
  434. return ERR_PTR(ret);
  435. }
  436. static int afs_iget5_set_root(struct inode *inode, void *opaque)
  437. {
  438. struct afs_super_info *as = AFS_FS_S(inode->i_sb);
  439. struct afs_vnode *vnode = AFS_FS_I(inode);
  440. vnode->volume = as->volume;
  441. vnode->fid.vid = as->volume->vid;
  442. vnode->fid.vnode = 1;
  443. vnode->fid.unique = 1;
  444. inode->i_ino = 1;
  445. inode->i_generation = 1;
  446. return 0;
  447. }
  448. /*
  449. * Set up the root inode for a volume. This is always vnode 1, unique 1 within
  450. * the volume.
  451. */
  452. struct inode *afs_root_iget(struct super_block *sb, struct key *key)
  453. {
  454. struct afs_super_info *as = AFS_FS_S(sb);
  455. struct afs_operation *op;
  456. struct afs_vnode *vnode;
  457. struct inode *inode;
  458. int ret;
  459. _enter(",{%llx},,", as->volume->vid);
  460. inode = iget5_locked(sb, 1, NULL, afs_iget5_set_root, NULL);
  461. if (!inode) {
  462. _leave(" = -ENOMEM");
  463. return ERR_PTR(-ENOMEM);
  464. }
  465. _debug("GOT ROOT INODE %p { vl=%llx }", inode, as->volume->vid);
  466. BUG_ON(!(inode->i_state & I_NEW));
  467. vnode = AFS_FS_I(inode);
  468. vnode->cb_v_check = atomic_read(&as->volume->cb_v_break);
  469. afs_set_netfs_context(vnode);
  470. op = afs_alloc_operation(key, as->volume);
  471. if (IS_ERR(op)) {
  472. ret = PTR_ERR(op);
  473. goto error;
  474. }
  475. afs_op_set_vnode(op, 0, vnode);
  476. op->nr_files = 1;
  477. op->ops = &afs_fetch_status_operation;
  478. ret = afs_do_sync_operation(op);
  479. if (ret < 0)
  480. goto error;
  481. afs_get_inode_cache(vnode);
  482. clear_bit(AFS_VNODE_UNSET, &vnode->flags);
  483. unlock_new_inode(inode);
  484. _leave(" = %p", inode);
  485. return inode;
  486. error:
  487. iget_failed(inode);
  488. _leave(" = %d [bad]", ret);
  489. return ERR_PTR(ret);
  490. }
  491. /*
  492. * read the attributes of an inode
  493. */
  494. int afs_getattr(struct mnt_idmap *idmap, const struct path *path,
  495. struct kstat *stat, u32 request_mask, unsigned int query_flags)
  496. {
  497. struct inode *inode = d_inode(path->dentry);
  498. struct afs_vnode *vnode = AFS_FS_I(inode);
  499. struct key *key;
  500. int ret, seq;
  501. _enter("{ ino=%lu v=%u }", inode->i_ino, inode->i_generation);
  502. if (vnode->volume &&
  503. !(query_flags & AT_STATX_DONT_SYNC) &&
  504. atomic64_read(&vnode->cb_expires_at) == AFS_NO_CB_PROMISE) {
  505. key = afs_request_key(vnode->volume->cell);
  506. if (IS_ERR(key))
  507. return PTR_ERR(key);
  508. ret = afs_validate(vnode, key);
  509. key_put(key);
  510. if (ret < 0)
  511. return ret;
  512. }
  513. do {
  514. seq = read_seqbegin(&vnode->cb_lock);
  515. generic_fillattr(&nop_mnt_idmap, request_mask, inode, stat);
  516. if (test_bit(AFS_VNODE_SILLY_DELETED, &vnode->flags) &&
  517. stat->nlink > 0)
  518. stat->nlink -= 1;
  519. /* Lie about the size of directories. We maintain a locally
  520. * edited copy and may make different allocation decisions on
  521. * it, but we need to give userspace the server's size.
  522. */
  523. if (S_ISDIR(inode->i_mode))
  524. stat->size = vnode->netfs.remote_i_size;
  525. } while (read_seqretry(&vnode->cb_lock, seq));
  526. return 0;
  527. }
  528. /*
  529. * discard an AFS inode
  530. */
  531. int afs_drop_inode(struct inode *inode)
  532. {
  533. _enter("");
  534. if (test_bit(AFS_VNODE_PSEUDODIR, &AFS_FS_I(inode)->flags))
  535. return generic_delete_inode(inode);
  536. else
  537. return generic_drop_inode(inode);
  538. }
  539. /*
  540. * clear an AFS inode
  541. */
  542. void afs_evict_inode(struct inode *inode)
  543. {
  544. struct afs_vnode_cache_aux aux;
  545. struct afs_vnode *vnode = AFS_FS_I(inode);
  546. _enter("{%llx:%llu.%d}",
  547. vnode->fid.vid,
  548. vnode->fid.vnode,
  549. vnode->fid.unique);
  550. _debug("CLEAR INODE %p", inode);
  551. ASSERTCMP(inode->i_ino, ==, vnode->fid.vnode);
  552. netfs_wait_for_outstanding_io(inode);
  553. truncate_inode_pages_final(&inode->i_data);
  554. afs_set_cache_aux(vnode, &aux);
  555. netfs_clear_inode_writeback(inode, &aux);
  556. clear_inode(inode);
  557. while (!list_empty(&vnode->wb_keys)) {
  558. struct afs_wb_key *wbk = list_entry(vnode->wb_keys.next,
  559. struct afs_wb_key, vnode_link);
  560. list_del(&wbk->vnode_link);
  561. afs_put_wb_key(wbk);
  562. }
  563. fscache_relinquish_cookie(afs_vnode_cache(vnode),
  564. test_bit(AFS_VNODE_DELETED, &vnode->flags));
  565. afs_prune_wb_keys(vnode);
  566. afs_put_permits(rcu_access_pointer(vnode->permit_cache));
  567. key_put(vnode->silly_key);
  568. vnode->silly_key = NULL;
  569. key_put(vnode->lock_key);
  570. vnode->lock_key = NULL;
  571. _leave("");
  572. }
  573. static void afs_setattr_success(struct afs_operation *op)
  574. {
  575. struct afs_vnode_param *vp = &op->file[0];
  576. struct inode *inode = &vp->vnode->netfs.inode;
  577. loff_t old_i_size = i_size_read(inode);
  578. op->setattr.old_i_size = old_i_size;
  579. afs_vnode_commit_status(op, vp);
  580. /* inode->i_size has now been changed. */
  581. if (op->setattr.attr->ia_valid & ATTR_SIZE) {
  582. loff_t size = op->setattr.attr->ia_size;
  583. if (size > old_i_size)
  584. pagecache_isize_extended(inode, old_i_size, size);
  585. }
  586. }
  587. static void afs_setattr_edit_file(struct afs_operation *op)
  588. {
  589. struct afs_vnode_param *vp = &op->file[0];
  590. struct afs_vnode *vnode = vp->vnode;
  591. struct inode *inode = &vnode->netfs.inode;
  592. if (op->setattr.attr->ia_valid & ATTR_SIZE) {
  593. loff_t size = op->setattr.attr->ia_size;
  594. loff_t old = op->setattr.old_i_size;
  595. /* Note: inode->i_size was updated by afs_apply_status() inside
  596. * the I/O and callback locks.
  597. */
  598. if (size != old) {
  599. truncate_pagecache(inode, size);
  600. netfs_resize_file(&vnode->netfs, size, true);
  601. fscache_resize_cookie(afs_vnode_cache(vnode), size);
  602. }
  603. }
  604. }
  605. static const struct afs_operation_ops afs_setattr_operation = {
  606. .issue_afs_rpc = afs_fs_setattr,
  607. .issue_yfs_rpc = yfs_fs_setattr,
  608. .success = afs_setattr_success,
  609. .edit_dir = afs_setattr_edit_file,
  610. };
  611. /*
  612. * set the attributes of an inode
  613. */
  614. int afs_setattr(struct mnt_idmap *idmap, struct dentry *dentry,
  615. struct iattr *attr)
  616. {
  617. const unsigned int supported =
  618. ATTR_SIZE | ATTR_MODE | ATTR_UID | ATTR_GID |
  619. ATTR_MTIME | ATTR_MTIME_SET | ATTR_TIMES_SET | ATTR_TOUCH;
  620. struct afs_operation *op;
  621. struct afs_vnode *vnode = AFS_FS_I(d_inode(dentry));
  622. struct inode *inode = &vnode->netfs.inode;
  623. loff_t i_size;
  624. int ret;
  625. _enter("{%llx:%llu},{n=%pd},%x",
  626. vnode->fid.vid, vnode->fid.vnode, dentry,
  627. attr->ia_valid);
  628. if (!(attr->ia_valid & supported)) {
  629. _leave(" = 0 [unsupported]");
  630. return 0;
  631. }
  632. i_size = i_size_read(inode);
  633. if (attr->ia_valid & ATTR_SIZE) {
  634. if (!S_ISREG(inode->i_mode))
  635. return -EISDIR;
  636. ret = inode_newsize_ok(inode, attr->ia_size);
  637. if (ret)
  638. return ret;
  639. if (attr->ia_size == i_size)
  640. attr->ia_valid &= ~ATTR_SIZE;
  641. }
  642. fscache_use_cookie(afs_vnode_cache(vnode), true);
  643. /* Prevent any new writebacks from starting whilst we do this. */
  644. down_write(&vnode->validate_lock);
  645. if ((attr->ia_valid & ATTR_SIZE) && S_ISREG(inode->i_mode)) {
  646. loff_t size = attr->ia_size;
  647. /* Wait for any outstanding writes to the server to complete */
  648. loff_t from = min(size, i_size);
  649. loff_t to = max(size, i_size);
  650. ret = filemap_fdatawait_range(inode->i_mapping, from, to);
  651. if (ret < 0)
  652. goto out_unlock;
  653. /* Don't talk to the server if we're just shortening in-memory
  654. * writes that haven't gone to the server yet.
  655. */
  656. if (!(attr->ia_valid & (supported & ~ATTR_SIZE & ~ATTR_MTIME)) &&
  657. attr->ia_size < i_size &&
  658. attr->ia_size > vnode->netfs.remote_i_size) {
  659. truncate_setsize(inode, attr->ia_size);
  660. netfs_resize_file(&vnode->netfs, size, false);
  661. fscache_resize_cookie(afs_vnode_cache(vnode),
  662. attr->ia_size);
  663. ret = 0;
  664. goto out_unlock;
  665. }
  666. }
  667. op = afs_alloc_operation(((attr->ia_valid & ATTR_FILE) ?
  668. afs_file_key(attr->ia_file) : NULL),
  669. vnode->volume);
  670. if (IS_ERR(op)) {
  671. ret = PTR_ERR(op);
  672. goto out_unlock;
  673. }
  674. afs_op_set_vnode(op, 0, vnode);
  675. op->setattr.attr = attr;
  676. if (attr->ia_valid & ATTR_SIZE) {
  677. op->file[0].dv_delta = 1;
  678. op->file[0].set_size = true;
  679. }
  680. op->ctime = attr->ia_ctime;
  681. op->file[0].update_ctime = 1;
  682. op->file[0].modification = true;
  683. op->ops = &afs_setattr_operation;
  684. ret = afs_do_sync_operation(op);
  685. out_unlock:
  686. up_write(&vnode->validate_lock);
  687. fscache_unuse_cookie(afs_vnode_cache(vnode), NULL, NULL);
  688. _leave(" = %d", ret);
  689. return ret;
  690. }