inode.c 24 KB

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