dir_edit.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  1. /* AFS filesystem directory editing
  2. *
  3. * Copyright (C) 2018 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/fs.h>
  13. #include <linux/namei.h>
  14. #include <linux/pagemap.h>
  15. #include <linux/iversion.h>
  16. #include "internal.h"
  17. #include "xdr_fs.h"
  18. /*
  19. * Find a number of contiguous clear bits in a directory block bitmask.
  20. *
  21. * There are 64 slots, which means we can load the entire bitmap into a
  22. * variable. The first bit doesn't count as it corresponds to the block header
  23. * slot. nr_slots is between 1 and 9.
  24. */
  25. static int afs_find_contig_bits(union afs_xdr_dir_block *block, unsigned int nr_slots)
  26. {
  27. u64 bitmap;
  28. u32 mask;
  29. int bit, n;
  30. bitmap = (u64)block->hdr.bitmap[0] << 0 * 8;
  31. bitmap |= (u64)block->hdr.bitmap[1] << 1 * 8;
  32. bitmap |= (u64)block->hdr.bitmap[2] << 2 * 8;
  33. bitmap |= (u64)block->hdr.bitmap[3] << 3 * 8;
  34. bitmap |= (u64)block->hdr.bitmap[4] << 4 * 8;
  35. bitmap |= (u64)block->hdr.bitmap[5] << 5 * 8;
  36. bitmap |= (u64)block->hdr.bitmap[6] << 6 * 8;
  37. bitmap |= (u64)block->hdr.bitmap[7] << 7 * 8;
  38. bitmap >>= 1; /* The first entry is metadata */
  39. bit = 1;
  40. mask = (1 << nr_slots) - 1;
  41. do {
  42. if (sizeof(unsigned long) == 8)
  43. n = ffz(bitmap);
  44. else
  45. n = ((u32)bitmap) != 0 ?
  46. ffz((u32)bitmap) :
  47. ffz((u32)(bitmap >> 32)) + 32;
  48. bitmap >>= n;
  49. bit += n;
  50. if ((bitmap & mask) == 0) {
  51. if (bit > 64 - nr_slots)
  52. return -1;
  53. return bit;
  54. }
  55. n = __ffs(bitmap);
  56. bitmap >>= n;
  57. bit += n;
  58. } while (bitmap);
  59. return -1;
  60. }
  61. /*
  62. * Set a number of contiguous bits in the directory block bitmap.
  63. */
  64. static void afs_set_contig_bits(union afs_xdr_dir_block *block,
  65. int bit, unsigned int nr_slots)
  66. {
  67. u64 mask;
  68. mask = (1 << nr_slots) - 1;
  69. mask <<= bit;
  70. block->hdr.bitmap[0] |= (u8)(mask >> 0 * 8);
  71. block->hdr.bitmap[1] |= (u8)(mask >> 1 * 8);
  72. block->hdr.bitmap[2] |= (u8)(mask >> 2 * 8);
  73. block->hdr.bitmap[3] |= (u8)(mask >> 3 * 8);
  74. block->hdr.bitmap[4] |= (u8)(mask >> 4 * 8);
  75. block->hdr.bitmap[5] |= (u8)(mask >> 5 * 8);
  76. block->hdr.bitmap[6] |= (u8)(mask >> 6 * 8);
  77. block->hdr.bitmap[7] |= (u8)(mask >> 7 * 8);
  78. }
  79. /*
  80. * Clear a number of contiguous bits in the directory block bitmap.
  81. */
  82. static void afs_clear_contig_bits(union afs_xdr_dir_block *block,
  83. int bit, unsigned int nr_slots)
  84. {
  85. u64 mask;
  86. mask = (1 << nr_slots) - 1;
  87. mask <<= bit;
  88. block->hdr.bitmap[0] &= ~(u8)(mask >> 0 * 8);
  89. block->hdr.bitmap[1] &= ~(u8)(mask >> 1 * 8);
  90. block->hdr.bitmap[2] &= ~(u8)(mask >> 2 * 8);
  91. block->hdr.bitmap[3] &= ~(u8)(mask >> 3 * 8);
  92. block->hdr.bitmap[4] &= ~(u8)(mask >> 4 * 8);
  93. block->hdr.bitmap[5] &= ~(u8)(mask >> 5 * 8);
  94. block->hdr.bitmap[6] &= ~(u8)(mask >> 6 * 8);
  95. block->hdr.bitmap[7] &= ~(u8)(mask >> 7 * 8);
  96. }
  97. /*
  98. * Scan a directory block looking for a dirent of the right name.
  99. */
  100. static int afs_dir_scan_block(union afs_xdr_dir_block *block, struct qstr *name,
  101. unsigned int blocknum)
  102. {
  103. union afs_xdr_dirent *de;
  104. u64 bitmap;
  105. int d, len, n;
  106. _enter("");
  107. bitmap = (u64)block->hdr.bitmap[0] << 0 * 8;
  108. bitmap |= (u64)block->hdr.bitmap[1] << 1 * 8;
  109. bitmap |= (u64)block->hdr.bitmap[2] << 2 * 8;
  110. bitmap |= (u64)block->hdr.bitmap[3] << 3 * 8;
  111. bitmap |= (u64)block->hdr.bitmap[4] << 4 * 8;
  112. bitmap |= (u64)block->hdr.bitmap[5] << 5 * 8;
  113. bitmap |= (u64)block->hdr.bitmap[6] << 6 * 8;
  114. bitmap |= (u64)block->hdr.bitmap[7] << 7 * 8;
  115. for (d = (blocknum == 0 ? AFS_DIR_RESV_BLOCKS0 : AFS_DIR_RESV_BLOCKS);
  116. d < AFS_DIR_SLOTS_PER_BLOCK;
  117. d++) {
  118. if (!((bitmap >> d) & 1))
  119. continue;
  120. de = &block->dirents[d];
  121. if (de->u.valid != 1)
  122. continue;
  123. /* The block was NUL-terminated by afs_dir_check_page(). */
  124. len = strlen(de->u.name);
  125. if (len == name->len &&
  126. memcmp(de->u.name, name->name, name->len) == 0)
  127. return d;
  128. n = round_up(12 + len + 1 + 4, AFS_DIR_DIRENT_SIZE);
  129. n /= AFS_DIR_DIRENT_SIZE;
  130. d += n - 1;
  131. }
  132. return -1;
  133. }
  134. /*
  135. * Initialise a new directory block. Note that block 0 is special and contains
  136. * some extra metadata.
  137. */
  138. static void afs_edit_init_block(union afs_xdr_dir_block *meta,
  139. union afs_xdr_dir_block *block, int block_num)
  140. {
  141. memset(block, 0, sizeof(*block));
  142. block->hdr.npages = htons(1);
  143. block->hdr.magic = AFS_DIR_MAGIC;
  144. block->hdr.bitmap[0] = 1;
  145. if (block_num == 0) {
  146. block->hdr.bitmap[0] = 0xff;
  147. block->hdr.bitmap[1] = 0x1f;
  148. memset(block->meta.alloc_ctrs,
  149. AFS_DIR_SLOTS_PER_BLOCK,
  150. sizeof(block->meta.alloc_ctrs));
  151. meta->meta.alloc_ctrs[0] =
  152. AFS_DIR_SLOTS_PER_BLOCK - AFS_DIR_RESV_BLOCKS0;
  153. }
  154. if (block_num < AFS_DIR_BLOCKS_WITH_CTR)
  155. meta->meta.alloc_ctrs[block_num] =
  156. AFS_DIR_SLOTS_PER_BLOCK - AFS_DIR_RESV_BLOCKS;
  157. }
  158. /*
  159. * Edit a directory's file data to add a new directory entry. Doing this after
  160. * create, mkdir, symlink, link or rename if the data version number is
  161. * incremented by exactly one avoids the need to re-download the entire
  162. * directory contents.
  163. *
  164. * The caller must hold the inode locked.
  165. */
  166. void afs_edit_dir_add(struct afs_vnode *vnode,
  167. struct qstr *name, struct afs_fid *new_fid,
  168. enum afs_edit_dir_reason why)
  169. {
  170. union afs_xdr_dir_block *meta, *block;
  171. struct afs_xdr_dir_page *meta_page, *dir_page;
  172. union afs_xdr_dirent *de;
  173. struct page *page0, *page;
  174. unsigned int need_slots, nr_blocks, b;
  175. pgoff_t index;
  176. loff_t i_size;
  177. gfp_t gfp;
  178. int slot;
  179. _enter(",,{%d,%s},", name->len, name->name);
  180. i_size = i_size_read(&vnode->vfs_inode);
  181. if (i_size > AFS_DIR_BLOCK_SIZE * AFS_DIR_MAX_BLOCKS ||
  182. (i_size & (AFS_DIR_BLOCK_SIZE - 1))) {
  183. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  184. return;
  185. }
  186. gfp = vnode->vfs_inode.i_mapping->gfp_mask;
  187. page0 = find_or_create_page(vnode->vfs_inode.i_mapping, 0, gfp);
  188. if (!page0) {
  189. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  190. _leave(" [fgp]");
  191. return;
  192. }
  193. /* Work out how many slots we're going to need. */
  194. need_slots = round_up(12 + name->len + 1 + 4, AFS_DIR_DIRENT_SIZE);
  195. need_slots /= AFS_DIR_DIRENT_SIZE;
  196. meta_page = kmap(page0);
  197. meta = &meta_page->blocks[0];
  198. if (i_size == 0)
  199. goto new_directory;
  200. nr_blocks = i_size / AFS_DIR_BLOCK_SIZE;
  201. /* Find a block that has sufficient slots available. Each VM page
  202. * contains two or more directory blocks.
  203. */
  204. for (b = 0; b < nr_blocks + 1; b++) {
  205. /* If the directory extended into a new page, then we need to
  206. * tack a new page on the end.
  207. */
  208. index = b / AFS_DIR_BLOCKS_PER_PAGE;
  209. if (index == 0) {
  210. page = page0;
  211. dir_page = meta_page;
  212. } else {
  213. if (nr_blocks >= AFS_DIR_MAX_BLOCKS)
  214. goto error;
  215. gfp = vnode->vfs_inode.i_mapping->gfp_mask;
  216. page = find_or_create_page(vnode->vfs_inode.i_mapping,
  217. index, gfp);
  218. if (!page)
  219. goto error;
  220. if (!PagePrivate(page)) {
  221. set_page_private(page, 1);
  222. SetPagePrivate(page);
  223. }
  224. dir_page = kmap(page);
  225. }
  226. /* Abandon the edit if we got a callback break. */
  227. if (!test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
  228. goto invalidated;
  229. block = &dir_page->blocks[b % AFS_DIR_BLOCKS_PER_PAGE];
  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. i_size_write(&vnode->vfs_inode, (b + 1) * AFS_DIR_BLOCK_SIZE);
  240. }
  241. /* Only lower dir pages 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. if (page != page0) {
  254. unlock_page(page);
  255. kunmap(page);
  256. put_page(page);
  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. i_size_write(&vnode->vfs_inode, i_size);
  269. slot = AFS_DIR_RESV_BLOCKS0;
  270. page = page0;
  271. block = meta;
  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. if (page != page0) {
  289. unlock_page(page);
  290. kunmap(page);
  291. put_page(page);
  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->vfs_inode);
  297. afs_stat_v(vnode, n_dir_cr);
  298. _debug("Insert %s in %u[%u]", name->name, b, slot);
  299. out_unmap:
  300. unlock_page(page0);
  301. kunmap(page0);
  302. put_page(page0);
  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. if (page != page0) {
  309. kunmap(page);
  310. put_page(page);
  311. }
  312. goto out_unmap;
  313. error:
  314. trace_afs_edit_dir(vnode, why, afs_edit_dir_create_error, 0, 0, 0, 0, name->name);
  315. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  316. goto out_unmap;
  317. }
  318. /*
  319. * Edit a directory's file data to remove a new directory entry. Doing this
  320. * after unlink, rmdir or rename if the data version number is incremented by
  321. * exactly one avoids the need to re-download the entire directory contents.
  322. *
  323. * The caller must hold the inode locked.
  324. */
  325. void afs_edit_dir_remove(struct afs_vnode *vnode,
  326. struct qstr *name, enum afs_edit_dir_reason why)
  327. {
  328. struct afs_xdr_dir_page *meta_page, *dir_page;
  329. union afs_xdr_dir_block *meta, *block;
  330. union afs_xdr_dirent *de;
  331. struct page *page0, *page;
  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->vfs_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. page0 = find_lock_page(vnode->vfs_inode.i_mapping, 0);
  346. if (!page0) {
  347. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  348. _leave(" [fgp]");
  349. return;
  350. }
  351. /* Work out how many slots we're going to discard. */
  352. need_slots = round_up(12 + name->len + 1 + 4, AFS_DIR_DIRENT_SIZE);
  353. need_slots /= AFS_DIR_DIRENT_SIZE;
  354. meta_page = kmap(page0);
  355. meta = &meta_page->blocks[0];
  356. /* Find a page that has sufficient slots available. Each VM page
  357. * contains two or more directory blocks.
  358. */
  359. for (b = 0; b < nr_blocks; b++) {
  360. index = b / AFS_DIR_BLOCKS_PER_PAGE;
  361. if (index != 0) {
  362. page = find_lock_page(vnode->vfs_inode.i_mapping, index);
  363. if (!page)
  364. goto error;
  365. dir_page = kmap(page);
  366. } else {
  367. page = page0;
  368. dir_page = meta_page;
  369. }
  370. /* Abandon the edit if we got a callback break. */
  371. if (!test_bit(AFS_VNODE_DIR_VALID, &vnode->flags))
  372. goto invalidated;
  373. block = &dir_page->blocks[b % AFS_DIR_BLOCKS_PER_PAGE];
  374. if (b > AFS_DIR_BLOCKS_WITH_CTR ||
  375. meta->meta.alloc_ctrs[b] <= AFS_DIR_SLOTS_PER_BLOCK - 1 - need_slots) {
  376. slot = afs_dir_scan_block(block, name, b);
  377. if (slot >= 0)
  378. goto found_dirent;
  379. }
  380. if (page != page0) {
  381. unlock_page(page);
  382. kunmap(page);
  383. put_page(page);
  384. }
  385. }
  386. /* Didn't find the dirent to clobber. Download the directory again. */
  387. trace_afs_edit_dir(vnode, why, afs_edit_dir_delete_noent,
  388. 0, 0, 0, 0, name->name);
  389. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  390. goto out_unmap;
  391. found_dirent:
  392. de = &block->dirents[slot];
  393. trace_afs_edit_dir(vnode, why, afs_edit_dir_delete, b, slot,
  394. ntohl(de->u.vnode), ntohl(de->u.unique),
  395. name->name);
  396. memset(de, 0, sizeof(*de) * need_slots);
  397. /* Adjust the bitmap. */
  398. afs_clear_contig_bits(block, slot, need_slots);
  399. if (page != page0) {
  400. unlock_page(page);
  401. kunmap(page);
  402. put_page(page);
  403. }
  404. /* Adjust the allocation counter. */
  405. if (b < AFS_DIR_BLOCKS_WITH_CTR)
  406. meta->meta.alloc_ctrs[b] += need_slots;
  407. inode_set_iversion_raw(&vnode->vfs_inode, vnode->status.data_version);
  408. afs_stat_v(vnode, n_dir_rm);
  409. _debug("Remove %s from %u[%u]", name->name, b, slot);
  410. out_unmap:
  411. unlock_page(page0);
  412. kunmap(page0);
  413. put_page(page0);
  414. _leave("");
  415. return;
  416. invalidated:
  417. trace_afs_edit_dir(vnode, why, afs_edit_dir_delete_inval,
  418. 0, 0, 0, 0, name->name);
  419. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  420. if (page != page0) {
  421. unlock_page(page);
  422. kunmap(page);
  423. put_page(page);
  424. }
  425. goto out_unmap;
  426. error:
  427. trace_afs_edit_dir(vnode, why, afs_edit_dir_delete_error,
  428. 0, 0, 0, 0, name->name);
  429. clear_bit(AFS_VNODE_DIR_VALID, &vnode->flags);
  430. goto out_unmap;
  431. }