dir_edit.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  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 "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 (IS_ERR(folio)) {
  104. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  105. return NULL;
  106. }
  107. if (!folio_test_private(folio))
  108. folio_attach_private(folio, (void *)1);
  109. return folio;
  110. }
  111. /*
  112. * Scan a directory block looking for a dirent of the right name.
  113. */
  114. static int afs_dir_scan_block(const union afs_xdr_dir_block *block, const struct qstr *name,
  115. unsigned int blocknum)
  116. {
  117. const union afs_xdr_dirent *de;
  118. u64 bitmap;
  119. int d, len, n;
  120. _enter("");
  121. bitmap = (u64)block->hdr.bitmap[0] << 0 * 8;
  122. bitmap |= (u64)block->hdr.bitmap[1] << 1 * 8;
  123. bitmap |= (u64)block->hdr.bitmap[2] << 2 * 8;
  124. bitmap |= (u64)block->hdr.bitmap[3] << 3 * 8;
  125. bitmap |= (u64)block->hdr.bitmap[4] << 4 * 8;
  126. bitmap |= (u64)block->hdr.bitmap[5] << 5 * 8;
  127. bitmap |= (u64)block->hdr.bitmap[6] << 6 * 8;
  128. bitmap |= (u64)block->hdr.bitmap[7] << 7 * 8;
  129. for (d = (blocknum == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
  130. d < AFS_DIR_SLOTS_PER_BLOCK;
  131. d++) {
  132. if (!((bitmap >> d) & 1))
  133. continue;
  134. de = &block->dirents[d];
  135. if (de->u.valid != 1)
  136. continue;
  137. /* The block was NUL-terminated by afs_dir_check_page(). */
  138. len = strlen(de->u.name);
  139. if (len == name->len &&
  140. memcmp(de->u.name, name->name, name->len) == 0)
  141. return d;
  142. n = round_up(12 + len + 1 + 4, AFS_DIR_DIRENT_SIZE);
  143. n /= AFS_DIR_DIRENT_SIZE;
  144. d += n - 1;
  145. }
  146. return -1;
  147. }
  148. /*
  149. * Initialise a new directory block. Note that block 0 is special and contains
  150. * some extra metadata.
  151. */
  152. static void afs_edit_init_block(union afs_xdr_dir_block *meta,
  153. union afs_xdr_dir_block *block, int block_num)
  154. {
  155. memset(block, 0, sizeof(*block));
  156. block->hdr.npages = htons(1);
  157. block->hdr.magic = AFS_DIR_MAGIC;
  158. block->hdr.bitmap[0] = 1;
  159. if (block_num == 0) {
  160. block->hdr.bitmap[0] = 0xff;
  161. block->hdr.bitmap[1] = 0x1f;
  162. memset(block->meta.alloc_ctrs,
  163. AFS_DIR_SLOTS_PER_BLOCK,
  164. sizeof(block->meta.alloc_ctrs));
  165. meta->meta.alloc_ctrs[0] =
  166. AFS_DIR_SLOTS_PER_BLOCK - AFS_DIR_RESV_BLOCKS0;
  167. }
  168. if (block_num < AFS_DIR_BLOCKS_WITH_CTR)
  169. meta->meta.alloc_ctrs[block_num] =
  170. AFS_DIR_SLOTS_PER_BLOCK - AFS_DIR_RESV_BLOCKS;
  171. }
  172. /*
  173. * Edit a directory's file data to add a new directory entry. Doing this after
  174. * create, mkdir, symlink, link or rename if the data version number is
  175. * incremented by exactly one avoids the need to re-download the entire
  176. * directory contents.
  177. *
  178. * The caller must hold the inode locked.
  179. */
  180. void afs_edit_dir_add(struct afs_vnode *vnode,
  181. struct qstr *name, struct afs_fid *new_fid,
  182. enum afs_edit_dir_reason why)
  183. {
  184. union afs_xdr_dir_block *meta, *block;
  185. union afs_xdr_dirent *de;
  186. struct folio *folio0, *folio;
  187. unsigned int need_slots, nr_blocks, b;
  188. pgoff_t index;
  189. loff_t i_size;
  190. int slot;
  191. _enter(",,{%d,%s},", name->len, name->name);
  192. i_size = i_size_read(&vnode->netfs.inode);
  193. if (i_size > AFS_DIR_BLOCK_SIZE * AFS_DIR_MAX_BLOCKS ||
  194. (i_size & (AFS_DIR_BLOCK_SIZE - 1))) {
  195. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  196. return;
  197. }
  198. folio0 = afs_dir_get_folio(vnode, 0);
  199. if (!folio0) {
  200. _leave(" [fgp]");
  201. return;
  202. }
  203. /* Work out how many slots we're going to need. */
  204. need_slots = afs_dir_calc_slots(name->len);
  205. meta = kmap_local_folio(folio0, 0);
  206. if (i_size == 0)
  207. goto new_directory;
  208. nr_blocks = i_size / AFS_DIR_BLOCK_SIZE;
  209. /* Find a block that has sufficient slots available. Each folio
  210. * contains two or more directory blocks.
  211. */
  212. for (b = 0; b < nr_blocks + 1; b++) {
  213. /* If the directory extended into a new folio, then we need to
  214. * tack a new folio on the end.
  215. */
  216. index = b / AFS_DIR_BLOCKS_PER_PAGE;
  217. if (nr_blocks >= AFS_DIR_MAX_BLOCKS)
  218. goto error;
  219. if (index >= folio_nr_pages(folio0)) {
  220. folio = afs_dir_get_folio(vnode, index);
  221. if (!folio)
  222. goto error;
  223. } else {
  224. folio = folio0;
  225. }
  226. block = kmap_local_folio(folio, b * AFS_DIR_BLOCK_SIZE - folio_pos(folio));
  227. /* Abandon the edit if we got a callback break. */
  228. if (!test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
  229. goto invalidated;
  230. _debug("block %u: %2u %3u %u",
  231. b,
  232. (b < AFS_DIR_BLOCKS_WITH_CTR) ? meta->meta.alloc_ctrs[b] : 99,
  233. ntohs(block->hdr.npages),
  234. ntohs(block->hdr.magic));
  235. /* Initialise the block if necessary. */
  236. if (b == nr_blocks) {
  237. _debug("init %u", b);
  238. afs_edit_init_block(meta, block, b);
  239. afs_set_i_size(vnode, (b + 1) * AFS_DIR_BLOCK_SIZE);
  240. }
  241. /* Only lower dir blocks have a counter in the header. */
  242. if (b >= AFS_DIR_BLOCKS_WITH_CTR ||
  243. meta->meta.alloc_ctrs[b] >= need_slots) {
  244. /* We need to try and find one or more consecutive
  245. * slots to hold the entry.
  246. */
  247. slot = afs_find_contig_bits(block, need_slots);
  248. if (slot >= 0) {
  249. _debug("slot %u", slot);
  250. goto found_space;
  251. }
  252. }
  253. kunmap_local(block);
  254. if (folio != folio0) {
  255. folio_unlock(folio);
  256. folio_put(folio);
  257. }
  258. }
  259. /* There are no spare slots of sufficient size, yet the operation
  260. * succeeded. Download the directory again.
  261. */
  262. trace_afs_edit_dir(vnode, why, afs_edit_dir_create_nospc, 0, 0, 0, 0, name->name);
  263. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  264. goto out_unmap;
  265. new_directory:
  266. afs_edit_init_block(meta, meta, 0);
  267. i_size = AFS_DIR_BLOCK_SIZE;
  268. afs_set_i_size(vnode, i_size);
  269. slot = AFS_DIR_RESV_BLOCKS0;
  270. folio = folio0;
  271. block = kmap_local_folio(folio, 0);
  272. nr_blocks = 1;
  273. b = 0;
  274. found_space:
  275. /* Set the dirent slot. */
  276. trace_afs_edit_dir(vnode, why, afs_edit_dir_create, b, slot,
  277. new_fid->vnode, new_fid->unique, name->name);
  278. de = &block->dirents[slot];
  279. de->u.valid = 1;
  280. de->u.unused[0] = 0;
  281. de->u.hash_next = 0; // TODO: Really need to maintain this
  282. de->u.vnode = htonl(new_fid->vnode);
  283. de->u.unique = htonl(new_fid->unique);
  284. memcpy(de->u.name, name->name, name->len + 1);
  285. de->u.name[name->len] = 0;
  286. /* Adjust the bitmap. */
  287. afs_set_contig_bits(block, slot, need_slots);
  288. kunmap_local(block);
  289. if (folio != folio0) {
  290. folio_unlock(folio);
  291. folio_put(folio);
  292. }
  293. /* Adjust the allocation counter. */
  294. if (b < AFS_DIR_BLOCKS_WITH_CTR)
  295. meta->meta.alloc_ctrs[b] -= need_slots;
  296. inode_inc_iversion_raw(&vnode->netfs.inode);
  297. afs_stat_v(vnode, n_dir_cr);
  298. _debug("Insert %s in %u[%u]", name->name, b, slot);
  299. out_unmap:
  300. kunmap_local(meta);
  301. folio_unlock(folio0);
  302. folio_put(folio0);
  303. _leave("");
  304. return;
  305. invalidated:
  306. trace_afs_edit_dir(vnode, why, afs_edit_dir_create_inval, 0, 0, 0, 0, name->name);
  307. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  308. kunmap_local(block);
  309. if (folio != folio0) {
  310. folio_unlock(folio);
  311. folio_put(folio);
  312. }
  313. goto out_unmap;
  314. error:
  315. trace_afs_edit_dir(vnode, why, afs_edit_dir_create_error, 0, 0, 0, 0, name->name);
  316. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  317. goto out_unmap;
  318. }
  319. /*
  320. * Edit a directory's file data to remove a new directory entry. Doing this
  321. * after unlink, rmdir or rename if the data version number is incremented by
  322. * exactly one avoids the need to re-download the entire directory contents.
  323. *
  324. * The caller must hold the inode locked.
  325. */
  326. void afs_edit_dir_remove(struct afs_vnode *vnode,
  327. struct qstr *name, enum afs_edit_dir_reason why)
  328. {
  329. union afs_xdr_dir_block *meta, *block;
  330. union afs_xdr_dirent *de;
  331. struct folio *folio0, *folio;
  332. unsigned int need_slots, nr_blocks, b;
  333. pgoff_t index;
  334. loff_t i_size;
  335. int slot;
  336. _enter(",,{%d,%s},", name->len, name->name);
  337. i_size = i_size_read(&vnode->netfs.inode);
  338. if (i_size < AFS_DIR_BLOCK_SIZE ||
  339. i_size > AFS_DIR_BLOCK_SIZE * AFS_DIR_MAX_BLOCKS ||
  340. (i_size & (AFS_DIR_BLOCK_SIZE - 1))) {
  341. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  342. return;
  343. }
  344. nr_blocks = i_size / AFS_DIR_BLOCK_SIZE;
  345. folio0 = afs_dir_get_folio(vnode, 0);
  346. if (!folio0) {
  347. _leave(" [fgp]");
  348. return;
  349. }
  350. /* Work out how many slots we're going to discard. */
  351. need_slots = afs_dir_calc_slots(name->len);
  352. meta = kmap_local_folio(folio0, 0);
  353. /* Find a block that has sufficient slots available. Each folio
  354. * contains two or more directory blocks.
  355. */
  356. for (b = 0; b < nr_blocks; b++) {
  357. index = b / AFS_DIR_BLOCKS_PER_PAGE;
  358. if (index >= folio_nr_pages(folio0)) {
  359. folio = afs_dir_get_folio(vnode, index);
  360. if (!folio)
  361. goto error;
  362. } else {
  363. folio = folio0;
  364. }
  365. block = kmap_local_folio(folio, b * AFS_DIR_BLOCK_SIZE - folio_pos(folio));
  366. /* Abandon the edit if we got a callback break. */
  367. if (!test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
  368. goto invalidated;
  369. if (b > AFS_DIR_BLOCKS_WITH_CTR ||
  370. meta->meta.alloc_ctrs[b] <= AFS_DIR_SLOTS_PER_BLOCK - 1 - need_slots) {
  371. slot = afs_dir_scan_block(block, name, b);
  372. if (slot >= 0)
  373. goto found_dirent;
  374. }
  375. kunmap_local(block);
  376. if (folio != folio0) {
  377. folio_unlock(folio);
  378. folio_put(folio);
  379. }
  380. }
  381. /* Didn't find the dirent to clobber. Download the directory again. */
  382. trace_afs_edit_dir(vnode, why, afs_edit_dir_delete_noent,
  383. 0, 0, 0, 0, name->name);
  384. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  385. goto out_unmap;
  386. found_dirent:
  387. de = &block->dirents[slot];
  388. trace_afs_edit_dir(vnode, why, afs_edit_dir_delete, b, slot,
  389. ntohl(de->u.vnode), ntohl(de->u.unique),
  390. name->name);
  391. memset(de, 0, sizeof(*de) * need_slots);
  392. /* Adjust the bitmap. */
  393. afs_clear_contig_bits(block, slot, need_slots);
  394. kunmap_local(block);
  395. if (folio != folio0) {
  396. folio_unlock(folio);
  397. folio_put(folio);
  398. }
  399. /* Adjust the allocation counter. */
  400. if (b < AFS_DIR_BLOCKS_WITH_CTR)
  401. meta->meta.alloc_ctrs[b] += need_slots;
  402. inode_set_iversion_raw(&vnode->netfs.inode, vnode->status.data_version);
  403. afs_stat_v(vnode, n_dir_rm);
  404. _debug("Remove %s from %u[%u]", name->name, b, slot);
  405. out_unmap:
  406. kunmap_local(meta);
  407. folio_unlock(folio0);
  408. folio_put(folio0);
  409. _leave("");
  410. return;
  411. invalidated:
  412. trace_afs_edit_dir(vnode, why, afs_edit_dir_delete_inval,
  413. 0, 0, 0, 0, name->name);
  414. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  415. kunmap_local(block);
  416. if (folio != folio0) {
  417. folio_unlock(folio);
  418. folio_put(folio);
  419. }
  420. goto out_unmap;
  421. error:
  422. trace_afs_edit_dir(vnode, why, afs_edit_dir_delete_error,
  423. 0, 0, 0, 0, name->name);
  424. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  425. goto out_unmap;
  426. }
  427. /*
  428. * Edit a subdirectory that has been moved between directories to update the
  429. * ".." entry.
  430. */
  431. void afs_edit_dir_update_dotdot(struct afs_vnode *vnode, struct afs_vnode *new_dvnode,
  432. enum afs_edit_dir_reason why)
  433. {
  434. union afs_xdr_dir_block *block;
  435. union afs_xdr_dirent *de;
  436. struct folio *folio;
  437. unsigned int nr_blocks, b;
  438. pgoff_t index;
  439. loff_t i_size;
  440. int slot;
  441. _enter("");
  442. i_size = i_size_read(&vnode->netfs.inode);
  443. if (i_size < AFS_DIR_BLOCK_SIZE) {
  444. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  445. return;
  446. }
  447. nr_blocks = i_size / AFS_DIR_BLOCK_SIZE;
  448. /* Find a block that has sufficient slots available. Each folio
  449. * contains two or more directory blocks.
  450. */
  451. for (b = 0; b < nr_blocks; b++) {
  452. index = b / AFS_DIR_BLOCKS_PER_PAGE;
  453. folio = afs_dir_get_folio(vnode, index);
  454. if (!folio)
  455. goto error;
  456. block = kmap_local_folio(folio, b * AFS_DIR_BLOCK_SIZE - folio_pos(folio));
  457. /* Abandon the edit if we got a callback break. */
  458. if (!test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
  459. goto invalidated;
  460. slot = afs_dir_scan_block(block, &dotdot_name, b);
  461. if (slot >= 0)
  462. goto found_dirent;
  463. kunmap_local(block);
  464. folio_unlock(folio);
  465. folio_put(folio);
  466. }
  467. /* Didn't find the dirent to clobber. Download the directory again. */
  468. trace_afs_edit_dir(vnode, why, afs_edit_dir_update_nodd,
  469. 0, 0, 0, 0, "..");
  470. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  471. goto out;
  472. found_dirent:
  473. de = &block->dirents[slot];
  474. de->u.vnode = htonl(new_dvnode->fid.vnode);
  475. de->u.unique = htonl(new_dvnode->fid.unique);
  476. trace_afs_edit_dir(vnode, why, afs_edit_dir_update_dd, b, slot,
  477. ntohl(de->u.vnode), ntohl(de->u.unique), "..");
  478. kunmap_local(block);
  479. folio_unlock(folio);
  480. folio_put(folio);
  481. inode_set_iversion_raw(&vnode->netfs.inode, vnode->status.data_version);
  482. out:
  483. _leave("");
  484. return;
  485. invalidated:
  486. kunmap_local(block);
  487. folio_unlock(folio);
  488. folio_put(folio);
  489. trace_afs_edit_dir(vnode, why, afs_edit_dir_update_inval,
  490. 0, 0, 0, 0, "..");
  491. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  492. goto out;
  493. error:
  494. trace_afs_edit_dir(vnode, why, afs_edit_dir_update_error,
  495. 0, 0, 0, 0, "..");
  496. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  497. goto out;
  498. }