dir_edit.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 ([email protected])
  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 "internal.h"
  13. #include "xdr_fs.h"
  14. /*
  15. * Find a number of contiguous clear bits in a directory block bitmask.
  16. *
  17. * There are 64 slots, which means we can load the entire bitmap into a
  18. * variable. The first bit doesn't count as it corresponds to the block header
  19. * slot. nr_slots is between 1 and 9.
  20. */
  21. static int afs_find_contig_bits(union afs_xdr_dir_block *block, unsigned int nr_slots)
  22. {
  23. u64 bitmap;
  24. u32 mask;
  25. int bit, n;
  26. bitmap = (u64)block->hdr.bitmap[0] << 0 * 8;
  27. bitmap |= (u64)block->hdr.bitmap[1] << 1 * 8;
  28. bitmap |= (u64)block->hdr.bitmap[2] << 2 * 8;
  29. bitmap |= (u64)block->hdr.bitmap[3] << 3 * 8;
  30. bitmap |= (u64)block->hdr.bitmap[4] << 4 * 8;
  31. bitmap |= (u64)block->hdr.bitmap[5] << 5 * 8;
  32. bitmap |= (u64)block->hdr.bitmap[6] << 6 * 8;
  33. bitmap |= (u64)block->hdr.bitmap[7] << 7 * 8;
  34. bitmap >>= 1; /* The first entry is metadata */
  35. bit = 1;
  36. mask = (1 << nr_slots) - 1;
  37. do {
  38. if (sizeof(unsigned long) == 8)
  39. n = ffz(bitmap);
  40. else
  41. n = ((u32)bitmap) != 0 ?
  42. ffz((u32)bitmap) :
  43. ffz((u32)(bitmap >> 32)) + 32;
  44. bitmap >>= n;
  45. bit += n;
  46. if ((bitmap & mask) == 0) {
  47. if (bit > 64 - nr_slots)
  48. return -1;
  49. return bit;
  50. }
  51. n = __ffs(bitmap);
  52. bitmap >>= n;
  53. bit += n;
  54. } while (bitmap);
  55. return -1;
  56. }
  57. /*
  58. * Set a number of contiguous bits in the directory block bitmap.
  59. */
  60. static void afs_set_contig_bits(union afs_xdr_dir_block *block,
  61. int bit, unsigned int nr_slots)
  62. {
  63. u64 mask;
  64. mask = (1 << nr_slots) - 1;
  65. mask <<= bit;
  66. block->hdr.bitmap[0] |= (u8)(mask >> 0 * 8);
  67. block->hdr.bitmap[1] |= (u8)(mask >> 1 * 8);
  68. block->hdr.bitmap[2] |= (u8)(mask >> 2 * 8);
  69. block->hdr.bitmap[3] |= (u8)(mask >> 3 * 8);
  70. block->hdr.bitmap[4] |= (u8)(mask >> 4 * 8);
  71. block->hdr.bitmap[5] |= (u8)(mask >> 5 * 8);
  72. block->hdr.bitmap[6] |= (u8)(mask >> 6 * 8);
  73. block->hdr.bitmap[7] |= (u8)(mask >> 7 * 8);
  74. }
  75. /*
  76. * Clear a number of contiguous bits in the directory block bitmap.
  77. */
  78. static void afs_clear_contig_bits(union afs_xdr_dir_block *block,
  79. int bit, unsigned int nr_slots)
  80. {
  81. u64 mask;
  82. mask = (1 << nr_slots) - 1;
  83. mask <<= bit;
  84. block->hdr.bitmap[0] &= ~(u8)(mask >> 0 * 8);
  85. block->hdr.bitmap[1] &= ~(u8)(mask >> 1 * 8);
  86. block->hdr.bitmap[2] &= ~(u8)(mask >> 2 * 8);
  87. block->hdr.bitmap[3] &= ~(u8)(mask >> 3 * 8);
  88. block->hdr.bitmap[4] &= ~(u8)(mask >> 4 * 8);
  89. block->hdr.bitmap[5] &= ~(u8)(mask >> 5 * 8);
  90. block->hdr.bitmap[6] &= ~(u8)(mask >> 6 * 8);
  91. block->hdr.bitmap[7] &= ~(u8)(mask >> 7 * 8);
  92. }
  93. /*
  94. * Get a new directory folio.
  95. */
  96. static struct folio *afs_dir_get_folio(struct afs_vnode *vnode, pgoff_t index)
  97. {
  98. struct address_space *mapping = vnode->netfs.inode.i_mapping;
  99. struct folio *folio;
  100. folio = __filemap_get_folio(mapping, index,
  101. FGP_LOCK | FGP_ACCESSED | FGP_CREAT,
  102. mapping->gfp_mask);
  103. if (!folio)
  104. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  105. else if (folio && !folio_test_private(folio))
  106. folio_attach_private(folio, (void *)1);
  107. return folio;
  108. }
  109. /*
  110. * Scan a directory block looking for a dirent of the right name.
  111. */
  112. static int afs_dir_scan_block(union afs_xdr_dir_block *block, struct qstr *name,
  113. unsigned int blocknum)
  114. {
  115. union afs_xdr_dirent *de;
  116. u64 bitmap;
  117. int d, len, n;
  118. _enter("");
  119. bitmap = (u64)block->hdr.bitmap[0] << 0 * 8;
  120. bitmap |= (u64)block->hdr.bitmap[1] << 1 * 8;
  121. bitmap |= (u64)block->hdr.bitmap[2] << 2 * 8;
  122. bitmap |= (u64)block->hdr.bitmap[3] << 3 * 8;
  123. bitmap |= (u64)block->hdr.bitmap[4] << 4 * 8;
  124. bitmap |= (u64)block->hdr.bitmap[5] << 5 * 8;
  125. bitmap |= (u64)block->hdr.bitmap[6] << 6 * 8;
  126. bitmap |= (u64)block->hdr.bitmap[7] << 7 * 8;
  127. for (d = (blocknum == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
  128. d < AFS_DIR_SLOTS_PER_BLOCK;
  129. d++) {
  130. if (!((bitmap >> d) & 1))
  131. continue;
  132. de = &block->dirents[d];
  133. if (de->u.valid != 1)
  134. continue;
  135. /* The block was NUL-terminated by afs_dir_check_page(). */
  136. len = strlen(de->u.name);
  137. if (len == name->len &&
  138. memcmp(de->u.name, name->name, name->len) == 0)
  139. return d;
  140. n = round_up(12 + len + 1 + 4, AFS_DIR_DIRENT_SIZE);
  141. n /= AFS_DIR_DIRENT_SIZE;
  142. d += n - 1;
  143. }
  144. return -1;
  145. }
  146. /*
  147. * Initialise a new directory block. Note that block 0 is special and contains
  148. * some extra metadata.
  149. */
  150. static void afs_edit_init_block(union afs_xdr_dir_block *meta,
  151. union afs_xdr_dir_block *block, int block_num)
  152. {
  153. memset(block, 0, sizeof(*block));
  154. block->hdr.npages = htons(1);
  155. block->hdr.magic = AFS_DIR_MAGIC;
  156. block->hdr.bitmap[0] = 1;
  157. if (block_num == 0) {
  158. block->hdr.bitmap[0] = 0xff;
  159. block->hdr.bitmap[1] = 0x1f;
  160. memset(block->meta.alloc_ctrs,
  161. AFS_DIR_SLOTS_PER_BLOCK,
  162. sizeof(block->meta.alloc_ctrs));
  163. meta->meta.alloc_ctrs[0] =
  164. AFS_DIR_SLOTS_PER_BLOCK - AFS_DIR_RESV_BLOCKS0;
  165. }
  166. if (block_num < AFS_DIR_BLOCKS_WITH_CTR)
  167. meta->meta.alloc_ctrs[block_num] =
  168. AFS_DIR_SLOTS_PER_BLOCK - AFS_DIR_RESV_BLOCKS;
  169. }
  170. /*
  171. * Edit a directory's file data to add a new directory entry. Doing this after
  172. * create, mkdir, symlink, link or rename if the data version number is
  173. * incremented by exactly one avoids the need to re-download the entire
  174. * directory contents.
  175. *
  176. * The caller must hold the inode locked.
  177. */
  178. void afs_edit_dir_add(struct afs_vnode *vnode,
  179. struct qstr *name, struct afs_fid *new_fid,
  180. enum afs_edit_dir_reason why)
  181. {
  182. union afs_xdr_dir_block *meta, *block;
  183. union afs_xdr_dirent *de;
  184. struct folio *folio0, *folio;
  185. unsigned int need_slots, nr_blocks, b;
  186. pgoff_t index;
  187. loff_t i_size;
  188. int slot;
  189. _enter(",,{%d,%s},", name->len, name->name);
  190. i_size = i_size_read(&vnode->netfs.inode);
  191. if (i_size > AFS_DIR_BLOCK_SIZE * AFS_DIR_MAX_BLOCKS ||
  192. (i_size & (AFS_DIR_BLOCK_SIZE - 1))) {
  193. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  194. return;
  195. }
  196. folio0 = afs_dir_get_folio(vnode, 0);
  197. if (!folio0) {
  198. _leave(" [fgp]");
  199. return;
  200. }
  201. /* Work out how many slots we're going to need. */
  202. need_slots = afs_dir_calc_slots(name->len);
  203. meta = kmap_local_folio(folio0, 0);
  204. if (i_size == 0)
  205. goto new_directory;
  206. nr_blocks = i_size / AFS_DIR_BLOCK_SIZE;
  207. /* Find a block that has sufficient slots available. Each folio
  208. * contains two or more directory blocks.
  209. */
  210. for (b = 0; b < nr_blocks + 1; b++) {
  211. /* If the directory extended into a new folio, then we need to
  212. * tack a new folio on the end.
  213. */
  214. index = b / AFS_DIR_BLOCKS_PER_PAGE;
  215. if (nr_blocks >= AFS_DIR_MAX_BLOCKS)
  216. goto error;
  217. if (index >= folio_nr_pages(folio0)) {
  218. folio = afs_dir_get_folio(vnode, index);
  219. if (!folio)
  220. goto error;
  221. } else {
  222. folio = folio0;
  223. }
  224. block = kmap_local_folio(folio, b * AFS_DIR_BLOCK_SIZE - folio_file_pos(folio));
  225. /* Abandon the edit if we got a callback break. */
  226. if (!test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
  227. goto invalidated;
  228. _debug("block %u: %2u %3u %u",
  229. b,
  230. (b < AFS_DIR_BLOCKS_WITH_CTR) ? meta->meta.alloc_ctrs[b] : 99,
  231. ntohs(block->hdr.npages),
  232. ntohs(block->hdr.magic));
  233. /* Initialise the block if necessary. */
  234. if (b == nr_blocks) {
  235. _debug("init %u", b);
  236. afs_edit_init_block(meta, block, b);
  237. afs_set_i_size(vnode, (b + 1) * AFS_DIR_BLOCK_SIZE);
  238. }
  239. /* Only lower dir blocks have a counter in the header. */
  240. if (b >= AFS_DIR_BLOCKS_WITH_CTR ||
  241. meta->meta.alloc_ctrs[b] >= need_slots) {
  242. /* We need to try and find one or more consecutive
  243. * slots to hold the entry.
  244. */
  245. slot = afs_find_contig_bits(block, need_slots);
  246. if (slot >= 0) {
  247. _debug("slot %u", slot);
  248. goto found_space;
  249. }
  250. }
  251. kunmap_local(block);
  252. if (folio != folio0) {
  253. folio_unlock(folio);
  254. folio_put(folio);
  255. }
  256. }
  257. /* There are no spare slots of sufficient size, yet the operation
  258. * succeeded. Download the directory again.
  259. */
  260. trace_afs_edit_dir(vnode, why, afs_edit_dir_create_nospc, 0, 0, 0, 0, name->name);
  261. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  262. goto out_unmap;
  263. new_directory:
  264. afs_edit_init_block(meta, meta, 0);
  265. i_size = AFS_DIR_BLOCK_SIZE;
  266. afs_set_i_size(vnode, i_size);
  267. slot = AFS_DIR_RESV_BLOCKS0;
  268. folio = folio0;
  269. block = kmap_local_folio(folio, 0);
  270. nr_blocks = 1;
  271. b = 0;
  272. found_space:
  273. /* Set the dirent slot. */
  274. trace_afs_edit_dir(vnode, why, afs_edit_dir_create, b, slot,
  275. new_fid->vnode, new_fid->unique, name->name);
  276. de = &block->dirents[slot];
  277. de->u.valid = 1;
  278. de->u.unused[0] = 0;
  279. de->u.hash_next = 0; // TODO: Really need to maintain this
  280. de->u.vnode = htonl(new_fid->vnode);
  281. de->u.unique = htonl(new_fid->unique);
  282. memcpy(de->u.name, name->name, name->len + 1);
  283. de->u.name[name->len] = 0;
  284. /* Adjust the bitmap. */
  285. afs_set_contig_bits(block, slot, need_slots);
  286. kunmap_local(block);
  287. if (folio != folio0) {
  288. folio_unlock(folio);
  289. folio_put(folio);
  290. }
  291. /* Adjust the allocation counter. */
  292. if (b < AFS_DIR_BLOCKS_WITH_CTR)
  293. meta->meta.alloc_ctrs[b] -= need_slots;
  294. inode_inc_iversion_raw(&vnode->netfs.inode);
  295. afs_stat_v(vnode, n_dir_cr);
  296. _debug("Insert %s in %u[%u]", name->name, b, slot);
  297. out_unmap:
  298. kunmap_local(meta);
  299. folio_unlock(folio0);
  300. folio_put(folio0);
  301. _leave("");
  302. return;
  303. invalidated:
  304. trace_afs_edit_dir(vnode, why, afs_edit_dir_create_inval, 0, 0, 0, 0, name->name);
  305. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  306. kunmap_local(block);
  307. if (folio != folio0) {
  308. folio_unlock(folio);
  309. folio_put(folio);
  310. }
  311. goto out_unmap;
  312. error:
  313. trace_afs_edit_dir(vnode, why, afs_edit_dir_create_error, 0, 0, 0, 0, name->name);
  314. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  315. goto out_unmap;
  316. }
  317. /*
  318. * Edit a directory's file data to remove a new directory entry. Doing this
  319. * after unlink, rmdir or rename if the data version number is incremented by
  320. * exactly one avoids the need to re-download the entire directory contents.
  321. *
  322. * The caller must hold the inode locked.
  323. */
  324. void afs_edit_dir_remove(struct afs_vnode *vnode,
  325. struct qstr *name, enum afs_edit_dir_reason why)
  326. {
  327. union afs_xdr_dir_block *meta, *block;
  328. union afs_xdr_dirent *de;
  329. struct folio *folio0, *folio;
  330. unsigned int need_slots, nr_blocks, b;
  331. pgoff_t index;
  332. loff_t i_size;
  333. int slot;
  334. _enter(",,{%d,%s},", name->len, name->name);
  335. i_size = i_size_read(&vnode->netfs.inode);
  336. if (i_size < AFS_DIR_BLOCK_SIZE ||
  337. i_size > AFS_DIR_BLOCK_SIZE * AFS_DIR_MAX_BLOCKS ||
  338. (i_size & (AFS_DIR_BLOCK_SIZE - 1))) {
  339. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  340. return;
  341. }
  342. nr_blocks = i_size / AFS_DIR_BLOCK_SIZE;
  343. folio0 = afs_dir_get_folio(vnode, 0);
  344. if (!folio0) {
  345. _leave(" [fgp]");
  346. return;
  347. }
  348. /* Work out how many slots we're going to discard. */
  349. need_slots = afs_dir_calc_slots(name->len);
  350. meta = kmap_local_folio(folio0, 0);
  351. /* Find a block that has sufficient slots available. Each folio
  352. * contains two or more directory blocks.
  353. */
  354. for (b = 0; b < nr_blocks; b++) {
  355. index = b / AFS_DIR_BLOCKS_PER_PAGE;
  356. if (index >= folio_nr_pages(folio0)) {
  357. folio = afs_dir_get_folio(vnode, index);
  358. if (!folio)
  359. goto error;
  360. } else {
  361. folio = folio0;
  362. }
  363. block = kmap_local_folio(folio, b * AFS_DIR_BLOCK_SIZE - folio_file_pos(folio));
  364. /* Abandon the edit if we got a callback break. */
  365. if (!test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
  366. goto invalidated;
  367. if (b > AFS_DIR_BLOCKS_WITH_CTR ||
  368. meta->meta.alloc_ctrs[b] <= AFS_DIR_SLOTS_PER_BLOCK - 1 - need_slots) {
  369. slot = afs_dir_scan_block(block, name, b);
  370. if (slot >= 0)
  371. goto found_dirent;
  372. }
  373. kunmap_local(block);
  374. if (folio != folio0) {
  375. folio_unlock(folio);
  376. folio_put(folio);
  377. }
  378. }
  379. /* Didn't find the dirent to clobber. Download the directory again. */
  380. trace_afs_edit_dir(vnode, why, afs_edit_dir_delete_noent,
  381. 0, 0, 0, 0, name->name);
  382. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  383. goto out_unmap;
  384. found_dirent:
  385. de = &block->dirents[slot];
  386. trace_afs_edit_dir(vnode, why, afs_edit_dir_delete, b, slot,
  387. ntohl(de->u.vnode), ntohl(de->u.unique),
  388. name->name);
  389. memset(de, 0, sizeof(*de) * need_slots);
  390. /* Adjust the bitmap. */
  391. afs_clear_contig_bits(block, slot, need_slots);
  392. kunmap_local(block);
  393. if (folio != folio0) {
  394. folio_unlock(folio);
  395. folio_put(folio);
  396. }
  397. /* Adjust the allocation counter. */
  398. if (b < AFS_DIR_BLOCKS_WITH_CTR)
  399. meta->meta.alloc_ctrs[b] += need_slots;
  400. inode_set_iversion_raw(&vnode->netfs.inode, vnode->status.data_version);
  401. afs_stat_v(vnode, n_dir_rm);
  402. _debug("Remove %s from %u[%u]", name->name, b, slot);
  403. out_unmap:
  404. kunmap_local(meta);
  405. folio_unlock(folio0);
  406. folio_put(folio0);
  407. _leave("");
  408. return;
  409. invalidated:
  410. trace_afs_edit_dir(vnode, why, afs_edit_dir_delete_inval,
  411. 0, 0, 0, 0, name->name);
  412. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  413. kunmap_local(block);
  414. if (folio != folio0) {
  415. folio_unlock(folio);
  416. folio_put(folio);
  417. }
  418. goto out_unmap;
  419. error:
  420. trace_afs_edit_dir(vnode, why, afs_edit_dir_delete_error,
  421. 0, 0, 0, 0, name->name);
  422. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  423. goto out_unmap;
  424. }