dir_edit.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* AFS filesystem directory editing
  3. *
  4. * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/fs.h>
  9. #include <linux/namei.h>
  10. #include <linux/pagemap.h>
  11. #include <linux/iversion.h>
  12. #include <linux/folio_queue.h>
  13. #include "internal.h"
  14. #include "xdr_fs.h"
  15. /*
  16. * Find a number of contiguous clear bits in a directory block bitmask.
  17. *
  18. * There are 64 slots, which means we can load the entire bitmap into a
  19. * variable. The first bit doesn't count as it corresponds to the block header
  20. * slot. nr_slots is between 1 and 9.
  21. */
  22. static int afs_find_contig_bits(union afs_xdr_dir_block *block, unsigned int nr_slots)
  23. {
  24. u64 bitmap;
  25. u32 mask;
  26. int bit, n;
  27. bitmap = (u64)block->hdr.bitmap[0] << 0 * 8;
  28. bitmap |= (u64)block->hdr.bitmap[1] << 1 * 8;
  29. bitmap |= (u64)block->hdr.bitmap[2] << 2 * 8;
  30. bitmap |= (u64)block->hdr.bitmap[3] << 3 * 8;
  31. bitmap |= (u64)block->hdr.bitmap[4] << 4 * 8;
  32. bitmap |= (u64)block->hdr.bitmap[5] << 5 * 8;
  33. bitmap |= (u64)block->hdr.bitmap[6] << 6 * 8;
  34. bitmap |= (u64)block->hdr.bitmap[7] << 7 * 8;
  35. bitmap >>= 1; /* The first entry is metadata */
  36. bit = 1;
  37. mask = (1 << nr_slots) - 1;
  38. do {
  39. if (sizeof(unsigned long) == 8)
  40. n = ffz(bitmap);
  41. else
  42. n = ((u32)bitmap) != 0 ?
  43. ffz((u32)bitmap) :
  44. ffz((u32)(bitmap >> 32)) + 32;
  45. bitmap >>= n;
  46. bit += n;
  47. if ((bitmap & mask) == 0) {
  48. if (bit > 64 - nr_slots)
  49. return -1;
  50. return bit;
  51. }
  52. n = __ffs(bitmap);
  53. bitmap >>= n;
  54. bit += n;
  55. } while (bitmap);
  56. return -1;
  57. }
  58. /*
  59. * Set a number of contiguous bits in the directory block bitmap.
  60. */
  61. static void afs_set_contig_bits(union afs_xdr_dir_block *block,
  62. int bit, unsigned int nr_slots)
  63. {
  64. u64 mask;
  65. mask = (1 << nr_slots) - 1;
  66. mask <<= bit;
  67. block->hdr.bitmap[0] |= (u8)(mask >> 0 * 8);
  68. block->hdr.bitmap[1] |= (u8)(mask >> 1 * 8);
  69. block->hdr.bitmap[2] |= (u8)(mask >> 2 * 8);
  70. block->hdr.bitmap[3] |= (u8)(mask >> 3 * 8);
  71. block->hdr.bitmap[4] |= (u8)(mask >> 4 * 8);
  72. block->hdr.bitmap[5] |= (u8)(mask >> 5 * 8);
  73. block->hdr.bitmap[6] |= (u8)(mask >> 6 * 8);
  74. block->hdr.bitmap[7] |= (u8)(mask >> 7 * 8);
  75. }
  76. /*
  77. * Clear a number of contiguous bits in the directory block bitmap.
  78. */
  79. static void afs_clear_contig_bits(union afs_xdr_dir_block *block,
  80. int bit, unsigned int nr_slots)
  81. {
  82. u64 mask;
  83. mask = (1 << nr_slots) - 1;
  84. mask <<= bit;
  85. block->hdr.bitmap[0] &= ~(u8)(mask >> 0 * 8);
  86. block->hdr.bitmap[1] &= ~(u8)(mask >> 1 * 8);
  87. block->hdr.bitmap[2] &= ~(u8)(mask >> 2 * 8);
  88. block->hdr.bitmap[3] &= ~(u8)(mask >> 3 * 8);
  89. block->hdr.bitmap[4] &= ~(u8)(mask >> 4 * 8);
  90. block->hdr.bitmap[5] &= ~(u8)(mask >> 5 * 8);
  91. block->hdr.bitmap[6] &= ~(u8)(mask >> 6 * 8);
  92. block->hdr.bitmap[7] &= ~(u8)(mask >> 7 * 8);
  93. }
  94. /*
  95. * Get a specific block, extending the directory storage to cover it as needed.
  96. */
  97. static union afs_xdr_dir_block *afs_dir_get_block(struct afs_dir_iter *iter, size_t block)
  98. {
  99. struct folio_queue *fq;
  100. struct afs_vnode *dvnode = iter->dvnode;
  101. struct folio *folio;
  102. size_t blpos = block * AFS_DIR_BLOCK_SIZE;
  103. size_t blend = (block + 1) * AFS_DIR_BLOCK_SIZE, fpos = iter->fpos;
  104. int ret;
  105. if (dvnode->directory_size < blend) {
  106. size_t cur_size = dvnode->directory_size;
  107. ret = netfs_alloc_folioq_buffer(
  108. NULL, &dvnode->directory, &cur_size, blend,
  109. mapping_gfp_mask(dvnode->netfs.inode.i_mapping));
  110. dvnode->directory_size = cur_size;
  111. if (ret < 0)
  112. goto fail;
  113. }
  114. fq = iter->fq;
  115. if (!fq)
  116. fq = dvnode->directory;
  117. /* Search the folio queue for the folio containing the block... */
  118. for (; fq; fq = fq->next) {
  119. for (int s = iter->fq_slot; s < folioq_count(fq); s++) {
  120. size_t fsize = folioq_folio_size(fq, s);
  121. if (blend <= fpos + fsize) {
  122. /* ... and then return the mapped block. */
  123. folio = folioq_folio(fq, s);
  124. if (WARN_ON_ONCE(folio_pos(folio) != fpos))
  125. goto fail;
  126. iter->fq = fq;
  127. iter->fq_slot = s;
  128. iter->fpos = fpos;
  129. return kmap_local_folio(folio, blpos - fpos);
  130. }
  131. fpos += fsize;
  132. }
  133. iter->fq_slot = 0;
  134. }
  135. fail:
  136. iter->fq = NULL;
  137. iter->fq_slot = 0;
  138. afs_invalidate_dir(dvnode, afs_dir_invalid_edit_get_block);
  139. return NULL;
  140. }
  141. /*
  142. * Scan a directory block looking for a dirent of the right name.
  143. */
  144. static int afs_dir_scan_block(const union afs_xdr_dir_block *block, const struct qstr *name,
  145. unsigned int blocknum)
  146. {
  147. const union afs_xdr_dirent *de;
  148. u64 bitmap;
  149. int d, len, n;
  150. _enter("");
  151. bitmap = (u64)block->hdr.bitmap[0] << 0 * 8;
  152. bitmap |= (u64)block->hdr.bitmap[1] << 1 * 8;
  153. bitmap |= (u64)block->hdr.bitmap[2] << 2 * 8;
  154. bitmap |= (u64)block->hdr.bitmap[3] << 3 * 8;
  155. bitmap |= (u64)block->hdr.bitmap[4] << 4 * 8;
  156. bitmap |= (u64)block->hdr.bitmap[5] << 5 * 8;
  157. bitmap |= (u64)block->hdr.bitmap[6] << 6 * 8;
  158. bitmap |= (u64)block->hdr.bitmap[7] << 7 * 8;
  159. for (d = (blocknum == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
  160. d < AFS_DIR_SLOTS_PER_BLOCK;
  161. d++) {
  162. if (!((bitmap >> d) & 1))
  163. continue;
  164. de = &block->dirents[d];
  165. if (de->u.valid != 1)
  166. continue;
  167. /* The block was NUL-terminated by afs_dir_check_page(). */
  168. len = strlen(de->u.name);
  169. if (len == name->len &&
  170. memcmp(de->u.name, name->name, name->len) == 0)
  171. return d;
  172. n = round_up(12 + len + 1 + 4, AFS_DIR_DIRENT_SIZE);
  173. n /= AFS_DIR_DIRENT_SIZE;
  174. d += n - 1;
  175. }
  176. return -1;
  177. }
  178. /*
  179. * Initialise a new directory block. Note that block 0 is special and contains
  180. * some extra metadata.
  181. */
  182. static void afs_edit_init_block(union afs_xdr_dir_block *meta,
  183. union afs_xdr_dir_block *block, int block_num)
  184. {
  185. memset(block, 0, sizeof(*block));
  186. block->hdr.npages = htons(1);
  187. block->hdr.magic = AFS_DIR_MAGIC;
  188. block->hdr.bitmap[0] = 1;
  189. if (block_num == 0) {
  190. block->hdr.bitmap[0] = 0xff;
  191. block->hdr.bitmap[1] = 0x1f;
  192. memset(block->meta.alloc_ctrs,
  193. AFS_DIR_SLOTS_PER_BLOCK,
  194. sizeof(block->meta.alloc_ctrs));
  195. meta->meta.alloc_ctrs[0] =
  196. AFS_DIR_SLOTS_PER_BLOCK - AFS_DIR_RESV_BLOCKS0;
  197. }
  198. if (block_num < AFS_DIR_BLOCKS_WITH_CTR)
  199. meta->meta.alloc_ctrs[block_num] =
  200. AFS_DIR_SLOTS_PER_BLOCK - AFS_DIR_RESV_BLOCKS;
  201. }
  202. /*
  203. * Edit a directory's file data to add a new directory entry. Doing this after
  204. * create, mkdir, symlink, link or rename if the data version number is
  205. * incremented by exactly one avoids the need to re-download the entire
  206. * directory contents.
  207. *
  208. * The caller must hold the inode locked.
  209. */
  210. void afs_edit_dir_add(struct afs_vnode *vnode,
  211. const struct qstr *name, struct afs_fid *new_fid,
  212. enum afs_edit_dir_reason why)
  213. {
  214. union afs_xdr_dir_block *meta, *block;
  215. union afs_xdr_dirent *de;
  216. struct afs_dir_iter iter = { .dvnode = vnode };
  217. unsigned int nr_blocks, b, entry;
  218. loff_t i_size;
  219. int slot;
  220. _enter(",,{%d,%s},", name->len, name->name);
  221. i_size = i_size_read(&vnode->netfs.inode);
  222. if (i_size > AFS_DIR_BLOCK_SIZE * AFS_DIR_MAX_BLOCKS ||
  223. (i_size & (AFS_DIR_BLOCK_SIZE - 1))) {
  224. afs_invalidate_dir(vnode, afs_dir_invalid_edit_add_bad_size);
  225. return;
  226. }
  227. meta = afs_dir_get_block(&iter, 0);
  228. if (!meta)
  229. return;
  230. /* Work out how many slots we're going to need. */
  231. iter.nr_slots = afs_dir_calc_slots(name->len);
  232. if (i_size == 0)
  233. goto new_directory;
  234. nr_blocks = i_size / AFS_DIR_BLOCK_SIZE;
  235. /* Find a block that has sufficient slots available. Each folio
  236. * contains two or more directory blocks.
  237. */
  238. for (b = 0; b < nr_blocks + 1; b++) {
  239. /* If the directory extended into a new folio, then we need to
  240. * tack a new folio on the end.
  241. */
  242. if (nr_blocks >= AFS_DIR_MAX_BLOCKS)
  243. goto error_too_many_blocks;
  244. /* Lower dir blocks have a counter in the header we can check. */
  245. if (b < AFS_DIR_BLOCKS_WITH_CTR &&
  246. meta->meta.alloc_ctrs[b] < iter.nr_slots)
  247. continue;
  248. block = afs_dir_get_block(&iter, b);
  249. if (!block)
  250. goto error;
  251. /* Abandon the edit if we got a callback break. */
  252. if (!test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
  253. goto already_invalidated;
  254. _debug("block %u: %2u %3u %u",
  255. b,
  256. (b < AFS_DIR_BLOCKS_WITH_CTR) ? meta->meta.alloc_ctrs[b] : 99,
  257. ntohs(block->hdr.npages),
  258. ntohs(block->hdr.magic));
  259. /* Initialise the block if necessary. */
  260. if (b == nr_blocks) {
  261. _debug("init %u", b);
  262. afs_edit_init_block(meta, block, b);
  263. afs_set_i_size(vnode, (b + 1) * AFS_DIR_BLOCK_SIZE);
  264. }
  265. /* We need to try and find one or more consecutive slots to
  266. * hold the entry.
  267. */
  268. slot = afs_find_contig_bits(block, iter.nr_slots);
  269. if (slot >= 0) {
  270. _debug("slot %u", slot);
  271. goto found_space;
  272. }
  273. kunmap_local(block);
  274. }
  275. /* There are no spare slots of sufficient size, yet the operation
  276. * succeeded. Download the directory again.
  277. */
  278. trace_afs_edit_dir(vnode, why, afs_edit_dir_create_nospc, 0, 0, 0, 0, name->name);
  279. afs_invalidate_dir(vnode, afs_dir_invalid_edit_add_no_slots);
  280. goto out_unmap;
  281. new_directory:
  282. afs_edit_init_block(meta, meta, 0);
  283. i_size = AFS_DIR_BLOCK_SIZE;
  284. afs_set_i_size(vnode, i_size);
  285. slot = AFS_DIR_RESV_BLOCKS0;
  286. block = afs_dir_get_block(&iter, 0);
  287. nr_blocks = 1;
  288. b = 0;
  289. found_space:
  290. /* Set the dirent slot. */
  291. trace_afs_edit_dir(vnode, why, afs_edit_dir_create, b, slot,
  292. new_fid->vnode, new_fid->unique, name->name);
  293. de = &block->dirents[slot];
  294. de->u.valid = 1;
  295. de->u.unused[0] = 0;
  296. de->u.hash_next = 0; // TODO: Really need to maintain this
  297. de->u.vnode = htonl(new_fid->vnode);
  298. de->u.unique = htonl(new_fid->unique);
  299. memcpy(de->u.name, name->name, name->len + 1);
  300. de->u.name[name->len] = 0;
  301. /* Adjust the bitmap. */
  302. afs_set_contig_bits(block, slot, iter.nr_slots);
  303. /* Adjust the allocation counter. */
  304. if (b < AFS_DIR_BLOCKS_WITH_CTR)
  305. meta->meta.alloc_ctrs[b] -= iter.nr_slots;
  306. /* Adjust the hash chain. */
  307. entry = b * AFS_DIR_SLOTS_PER_BLOCK + slot;
  308. iter.bucket = afs_dir_hash_name(name);
  309. de->u.hash_next = meta->meta.hashtable[iter.bucket];
  310. meta->meta.hashtable[iter.bucket] = htons(entry);
  311. kunmap_local(block);
  312. inode_inc_iversion_raw(&vnode->netfs.inode);
  313. afs_stat_v(vnode, n_dir_cr);
  314. _debug("Insert %s in %u[%u]", name->name, b, slot);
  315. netfs_single_mark_inode_dirty(&vnode->netfs.inode);
  316. out_unmap:
  317. kunmap_local(meta);
  318. _leave("");
  319. return;
  320. already_invalidated:
  321. trace_afs_edit_dir(vnode, why, afs_edit_dir_create_inval, 0, 0, 0, 0, name->name);
  322. kunmap_local(block);
  323. goto out_unmap;
  324. error_too_many_blocks:
  325. afs_invalidate_dir(vnode, afs_dir_invalid_edit_add_too_many_blocks);
  326. error:
  327. trace_afs_edit_dir(vnode, why, afs_edit_dir_create_error, 0, 0, 0, 0, name->name);
  328. goto out_unmap;
  329. }
  330. /*
  331. * Edit a directory's file data to remove a new directory entry. Doing this
  332. * after unlink, rmdir or rename if the data version number is incremented by
  333. * exactly one avoids the need to re-download the entire directory contents.
  334. *
  335. * The caller must hold the inode locked.
  336. */
  337. void afs_edit_dir_remove(struct afs_vnode *vnode,
  338. const struct qstr *name, enum afs_edit_dir_reason why)
  339. {
  340. union afs_xdr_dir_block *meta, *block, *pblock;
  341. union afs_xdr_dirent *de, *pde;
  342. struct afs_dir_iter iter = { .dvnode = vnode };
  343. struct afs_fid fid;
  344. unsigned int b, slot, entry;
  345. loff_t i_size;
  346. __be16 next;
  347. int found;
  348. _enter(",,{%d,%s},", name->len, name->name);
  349. i_size = i_size_read(&vnode->netfs.inode);
  350. if (i_size < AFS_DIR_BLOCK_SIZE ||
  351. i_size > AFS_DIR_BLOCK_SIZE * AFS_DIR_MAX_BLOCKS ||
  352. (i_size & (AFS_DIR_BLOCK_SIZE - 1))) {
  353. afs_invalidate_dir(vnode, afs_dir_invalid_edit_rem_bad_size);
  354. return;
  355. }
  356. if (!afs_dir_init_iter(&iter, name))
  357. return;
  358. meta = afs_dir_find_block(&iter, 0);
  359. if (!meta)
  360. return;
  361. /* Find the entry in the blob. */
  362. found = afs_dir_search_bucket(&iter, name, &fid);
  363. if (found < 0) {
  364. /* Didn't find the dirent to clobber. Re-download. */
  365. trace_afs_edit_dir(vnode, why, afs_edit_dir_delete_noent,
  366. 0, 0, 0, 0, name->name);
  367. afs_invalidate_dir(vnode, afs_dir_invalid_edit_rem_wrong_name);
  368. goto out_unmap;
  369. }
  370. entry = found;
  371. b = entry / AFS_DIR_SLOTS_PER_BLOCK;
  372. slot = entry % AFS_DIR_SLOTS_PER_BLOCK;
  373. block = afs_dir_find_block(&iter, b);
  374. if (!block)
  375. goto error;
  376. if (!test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
  377. goto already_invalidated;
  378. /* Check and clear the entry. */
  379. de = &block->dirents[slot];
  380. if (de->u.valid != 1)
  381. goto error_unmap;
  382. trace_afs_edit_dir(vnode, why, afs_edit_dir_delete, b, slot,
  383. ntohl(de->u.vnode), ntohl(de->u.unique),
  384. name->name);
  385. /* Adjust the bitmap. */
  386. afs_clear_contig_bits(block, slot, iter.nr_slots);
  387. /* Adjust the allocation counter. */
  388. if (b < AFS_DIR_BLOCKS_WITH_CTR)
  389. meta->meta.alloc_ctrs[b] += iter.nr_slots;
  390. /* Clear the constituent entries. */
  391. next = de->u.hash_next;
  392. memset(de, 0, sizeof(*de) * iter.nr_slots);
  393. kunmap_local(block);
  394. /* Adjust the hash chain: if iter->prev_entry is 0, the hashtable head
  395. * index is previous; otherwise it's slot number of the previous entry.
  396. */
  397. if (!iter.prev_entry) {
  398. __be16 prev_next = meta->meta.hashtable[iter.bucket];
  399. if (unlikely(prev_next != htons(entry))) {
  400. pr_warn("%llx:%llx:%x: not head of chain b=%x p=%x,%x e=%x %*s",
  401. vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique,
  402. iter.bucket, iter.prev_entry, prev_next, entry,
  403. name->len, name->name);
  404. goto error;
  405. }
  406. meta->meta.hashtable[iter.bucket] = next;
  407. } else {
  408. unsigned int pb = iter.prev_entry / AFS_DIR_SLOTS_PER_BLOCK;
  409. unsigned int ps = iter.prev_entry % AFS_DIR_SLOTS_PER_BLOCK;
  410. __be16 prev_next;
  411. pblock = afs_dir_find_block(&iter, pb);
  412. if (!pblock)
  413. goto error;
  414. pde = &pblock->dirents[ps];
  415. prev_next = pde->u.hash_next;
  416. if (prev_next != htons(entry)) {
  417. kunmap_local(pblock);
  418. pr_warn("%llx:%llx:%x: not prev in chain b=%x p=%x,%x e=%x %*s",
  419. vnode->fid.vid, vnode->fid.vnode, vnode->fid.unique,
  420. iter.bucket, iter.prev_entry, prev_next, entry,
  421. name->len, name->name);
  422. goto error;
  423. }
  424. pde->u.hash_next = next;
  425. kunmap_local(pblock);
  426. }
  427. netfs_single_mark_inode_dirty(&vnode->netfs.inode);
  428. inode_set_iversion_raw(&vnode->netfs.inode, vnode->status.data_version);
  429. afs_stat_v(vnode, n_dir_rm);
  430. _debug("Remove %s from %u[%u]", name->name, b, slot);
  431. out_unmap:
  432. kunmap_local(meta);
  433. _leave("");
  434. return;
  435. already_invalidated:
  436. kunmap_local(block);
  437. trace_afs_edit_dir(vnode, why, afs_edit_dir_delete_inval,
  438. 0, 0, 0, 0, name->name);
  439. goto out_unmap;
  440. error_unmap:
  441. kunmap_local(block);
  442. error:
  443. trace_afs_edit_dir(vnode, why, afs_edit_dir_delete_error,
  444. 0, 0, 0, 0, name->name);
  445. goto out_unmap;
  446. }
  447. /*
  448. * Edit an entry in a directory to update the vnode it refers to. This is also
  449. * used to update the ".." entry in a directory.
  450. */
  451. void afs_edit_dir_update(struct afs_vnode *vnode, const struct qstr *name,
  452. struct afs_vnode *new_dvnode, enum afs_edit_dir_reason why)
  453. {
  454. union afs_xdr_dir_block *block;
  455. union afs_xdr_dirent *de;
  456. struct afs_dir_iter iter = { .dvnode = vnode };
  457. unsigned int nr_blocks, b;
  458. loff_t i_size;
  459. int slot;
  460. _enter("");
  461. i_size = i_size_read(&vnode->netfs.inode);
  462. if (i_size < AFS_DIR_BLOCK_SIZE) {
  463. afs_invalidate_dir(vnode, afs_dir_invalid_edit_upd_bad_size);
  464. return;
  465. }
  466. nr_blocks = i_size / AFS_DIR_BLOCK_SIZE;
  467. /* Find a block that has sufficient slots available. Each folio
  468. * contains two or more directory blocks.
  469. */
  470. for (b = 0; b < nr_blocks; b++) {
  471. block = afs_dir_get_block(&iter, b);
  472. if (!block)
  473. goto error;
  474. /* Abandon the edit if we got a callback break. */
  475. if (!test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
  476. goto already_invalidated;
  477. slot = afs_dir_scan_block(block, name, b);
  478. if (slot >= 0)
  479. goto found_dirent;
  480. kunmap_local(block);
  481. }
  482. /* Didn't find the dirent to clobber. Download the directory again. */
  483. trace_afs_edit_dir(vnode, why, afs_edit_dir_update_nodd,
  484. 0, 0, 0, 0, name->name);
  485. afs_invalidate_dir(vnode, afs_dir_invalid_edit_upd_no_dd);
  486. goto out;
  487. found_dirent:
  488. de = &block->dirents[slot];
  489. de->u.vnode = htonl(new_dvnode->fid.vnode);
  490. de->u.unique = htonl(new_dvnode->fid.unique);
  491. trace_afs_edit_dir(vnode, why, afs_edit_dir_update_dd, b, slot,
  492. ntohl(de->u.vnode), ntohl(de->u.unique), name->name);
  493. kunmap_local(block);
  494. netfs_single_mark_inode_dirty(&vnode->netfs.inode);
  495. inode_set_iversion_raw(&vnode->netfs.inode, vnode->status.data_version);
  496. out:
  497. _leave("");
  498. return;
  499. already_invalidated:
  500. kunmap_local(block);
  501. trace_afs_edit_dir(vnode, why, afs_edit_dir_update_inval,
  502. 0, 0, 0, 0, name->name);
  503. goto out;
  504. error:
  505. trace_afs_edit_dir(vnode, why, afs_edit_dir_update_error,
  506. 0, 0, 0, 0, name->name);
  507. goto out;
  508. }
  509. /*
  510. * Initialise a new directory. We need to fill in the "." and ".." entries.
  511. */
  512. void afs_mkdir_init_dir(struct afs_vnode *dvnode, struct afs_vnode *parent_dvnode)
  513. {
  514. union afs_xdr_dir_block *meta;
  515. struct afs_dir_iter iter = { .dvnode = dvnode };
  516. union afs_xdr_dirent *de;
  517. unsigned int slot = AFS_DIR_RESV_BLOCKS0;
  518. loff_t i_size;
  519. i_size = i_size_read(&dvnode->netfs.inode);
  520. if (i_size != AFS_DIR_BLOCK_SIZE) {
  521. afs_invalidate_dir(dvnode, afs_dir_invalid_edit_add_bad_size);
  522. return;
  523. }
  524. meta = afs_dir_get_block(&iter, 0);
  525. if (!meta)
  526. return;
  527. afs_edit_init_block(meta, meta, 0);
  528. de = &meta->dirents[slot];
  529. de->u.valid = 1;
  530. de->u.vnode = htonl(dvnode->fid.vnode);
  531. de->u.unique = htonl(dvnode->fid.unique);
  532. memcpy(de->u.name, ".", 2);
  533. trace_afs_edit_dir(dvnode, afs_edit_dir_for_mkdir, afs_edit_dir_mkdir, 0, slot,
  534. dvnode->fid.vnode, dvnode->fid.unique, ".");
  535. slot++;
  536. de = &meta->dirents[slot];
  537. de->u.valid = 1;
  538. de->u.vnode = htonl(parent_dvnode->fid.vnode);
  539. de->u.unique = htonl(parent_dvnode->fid.unique);
  540. memcpy(de->u.name, "..", 3);
  541. trace_afs_edit_dir(dvnode, afs_edit_dir_for_mkdir, afs_edit_dir_mkdir, 0, slot,
  542. parent_dvnode->fid.vnode, parent_dvnode->fid.unique, "..");
  543. afs_set_contig_bits(meta, AFS_DIR_RESV_BLOCKS0, 2);
  544. meta->meta.alloc_ctrs[0] -= 2;
  545. kunmap_local(meta);
  546. netfs_single_mark_inode_dirty(&dvnode->netfs.inode);
  547. set_bit(AFS_VNODE_DIR_VALID, &dvnode->flags);
  548. set_bit(AFS_VNODE_DIR_READ, &dvnode->flags);
  549. }