xfs_dir2.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (c) 2000-2001,2005 Silicon Graphics, Inc.
  4. * All Rights Reserved.
  5. */
  6. #include "xfs.h"
  7. #include "xfs_fs.h"
  8. #include "xfs_shared.h"
  9. #include "xfs_format.h"
  10. #include "xfs_log_format.h"
  11. #include "xfs_trans_resv.h"
  12. #include "xfs_mount.h"
  13. #include "xfs_inode.h"
  14. #include "xfs_trans.h"
  15. #include "xfs_bmap.h"
  16. #include "xfs_dir2.h"
  17. #include "xfs_dir2_priv.h"
  18. #include "xfs_errortag.h"
  19. #include "xfs_error.h"
  20. #include "xfs_trace.h"
  21. const struct xfs_name xfs_name_dotdot = {
  22. .name = (const unsigned char *)"..",
  23. .len = 2,
  24. .type = XFS_DIR3_FT_DIR,
  25. };
  26. /*
  27. * Convert inode mode to directory entry filetype
  28. */
  29. unsigned char
  30. xfs_mode_to_ftype(
  31. int mode)
  32. {
  33. switch (mode & S_IFMT) {
  34. case S_IFREG:
  35. return XFS_DIR3_FT_REG_FILE;
  36. case S_IFDIR:
  37. return XFS_DIR3_FT_DIR;
  38. case S_IFCHR:
  39. return XFS_DIR3_FT_CHRDEV;
  40. case S_IFBLK:
  41. return XFS_DIR3_FT_BLKDEV;
  42. case S_IFIFO:
  43. return XFS_DIR3_FT_FIFO;
  44. case S_IFSOCK:
  45. return XFS_DIR3_FT_SOCK;
  46. case S_IFLNK:
  47. return XFS_DIR3_FT_SYMLINK;
  48. default:
  49. return XFS_DIR3_FT_UNKNOWN;
  50. }
  51. }
  52. /*
  53. * ASCII case-insensitive (ie. A-Z) support for directories that was
  54. * used in IRIX.
  55. */
  56. xfs_dahash_t
  57. xfs_ascii_ci_hashname(
  58. const struct xfs_name *name)
  59. {
  60. xfs_dahash_t hash;
  61. int i;
  62. for (i = 0, hash = 0; i < name->len; i++)
  63. hash = tolower(name->name[i]) ^ rol32(hash, 7);
  64. return hash;
  65. }
  66. enum xfs_dacmp
  67. xfs_ascii_ci_compname(
  68. struct xfs_da_args *args,
  69. const unsigned char *name,
  70. int len)
  71. {
  72. enum xfs_dacmp result;
  73. int i;
  74. if (args->namelen != len)
  75. return XFS_CMP_DIFFERENT;
  76. result = XFS_CMP_EXACT;
  77. for (i = 0; i < len; i++) {
  78. if (args->name[i] == name[i])
  79. continue;
  80. if (tolower(args->name[i]) != tolower(name[i]))
  81. return XFS_CMP_DIFFERENT;
  82. result = XFS_CMP_CASE;
  83. }
  84. return result;
  85. }
  86. int
  87. xfs_da_mount(
  88. struct xfs_mount *mp)
  89. {
  90. struct xfs_da_geometry *dageo;
  91. ASSERT(mp->m_sb.sb_versionnum & XFS_SB_VERSION_DIRV2BIT);
  92. ASSERT(xfs_dir2_dirblock_bytes(&mp->m_sb) <= XFS_MAX_BLOCKSIZE);
  93. mp->m_dir_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
  94. KM_MAYFAIL);
  95. mp->m_attr_geo = kmem_zalloc(sizeof(struct xfs_da_geometry),
  96. KM_MAYFAIL);
  97. if (!mp->m_dir_geo || !mp->m_attr_geo) {
  98. kmem_free(mp->m_dir_geo);
  99. kmem_free(mp->m_attr_geo);
  100. return -ENOMEM;
  101. }
  102. /* set up directory geometry */
  103. dageo = mp->m_dir_geo;
  104. dageo->blklog = mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog;
  105. dageo->fsblog = mp->m_sb.sb_blocklog;
  106. dageo->blksize = xfs_dir2_dirblock_bytes(&mp->m_sb);
  107. dageo->fsbcount = 1 << mp->m_sb.sb_dirblklog;
  108. if (xfs_has_crc(mp)) {
  109. dageo->node_hdr_size = sizeof(struct xfs_da3_node_hdr);
  110. dageo->leaf_hdr_size = sizeof(struct xfs_dir3_leaf_hdr);
  111. dageo->free_hdr_size = sizeof(struct xfs_dir3_free_hdr);
  112. dageo->data_entry_offset =
  113. sizeof(struct xfs_dir3_data_hdr);
  114. } else {
  115. dageo->node_hdr_size = sizeof(struct xfs_da_node_hdr);
  116. dageo->leaf_hdr_size = sizeof(struct xfs_dir2_leaf_hdr);
  117. dageo->free_hdr_size = sizeof(struct xfs_dir2_free_hdr);
  118. dageo->data_entry_offset =
  119. sizeof(struct xfs_dir2_data_hdr);
  120. }
  121. dageo->leaf_max_ents = (dageo->blksize - dageo->leaf_hdr_size) /
  122. sizeof(struct xfs_dir2_leaf_entry);
  123. dageo->free_max_bests = (dageo->blksize - dageo->free_hdr_size) /
  124. sizeof(xfs_dir2_data_off_t);
  125. dageo->data_first_offset = dageo->data_entry_offset +
  126. xfs_dir2_data_entsize(mp, 1) +
  127. xfs_dir2_data_entsize(mp, 2);
  128. /*
  129. * Now we've set up the block conversion variables, we can calculate the
  130. * segment block constants using the geometry structure.
  131. */
  132. dageo->datablk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_DATA_OFFSET);
  133. dageo->leafblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_LEAF_OFFSET);
  134. dageo->freeblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_FREE_OFFSET);
  135. dageo->node_ents = (dageo->blksize - dageo->node_hdr_size) /
  136. (uint)sizeof(xfs_da_node_entry_t);
  137. dageo->max_extents = (XFS_DIR2_MAX_SPACES * XFS_DIR2_SPACE_SIZE) >>
  138. mp->m_sb.sb_blocklog;
  139. dageo->magicpct = (dageo->blksize * 37) / 100;
  140. /* set up attribute geometry - single fsb only */
  141. dageo = mp->m_attr_geo;
  142. dageo->blklog = mp->m_sb.sb_blocklog;
  143. dageo->fsblog = mp->m_sb.sb_blocklog;
  144. dageo->blksize = 1 << dageo->blklog;
  145. dageo->fsbcount = 1;
  146. dageo->node_hdr_size = mp->m_dir_geo->node_hdr_size;
  147. dageo->node_ents = (dageo->blksize - dageo->node_hdr_size) /
  148. (uint)sizeof(xfs_da_node_entry_t);
  149. if (xfs_has_large_extent_counts(mp))
  150. dageo->max_extents = XFS_MAX_EXTCNT_ATTR_FORK_LARGE;
  151. else
  152. dageo->max_extents = XFS_MAX_EXTCNT_ATTR_FORK_SMALL;
  153. dageo->magicpct = (dageo->blksize * 37) / 100;
  154. return 0;
  155. }
  156. void
  157. xfs_da_unmount(
  158. struct xfs_mount *mp)
  159. {
  160. kmem_free(mp->m_dir_geo);
  161. kmem_free(mp->m_attr_geo);
  162. }
  163. /*
  164. * Return 1 if directory contains only "." and "..".
  165. */
  166. int
  167. xfs_dir_isempty(
  168. xfs_inode_t *dp)
  169. {
  170. xfs_dir2_sf_hdr_t *sfp;
  171. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  172. if (dp->i_disk_size == 0) /* might happen during shutdown. */
  173. return 1;
  174. if (dp->i_disk_size > xfs_inode_data_fork_size(dp))
  175. return 0;
  176. sfp = (xfs_dir2_sf_hdr_t *)dp->i_df.if_u1.if_data;
  177. return !sfp->count;
  178. }
  179. /*
  180. * Validate a given inode number.
  181. */
  182. int
  183. xfs_dir_ino_validate(
  184. xfs_mount_t *mp,
  185. xfs_ino_t ino)
  186. {
  187. bool ino_ok = xfs_verify_dir_ino(mp, ino);
  188. if (XFS_IS_CORRUPT(mp, !ino_ok) ||
  189. XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DIR_INO_VALIDATE)) {
  190. xfs_warn(mp, "Invalid inode number 0x%Lx",
  191. (unsigned long long) ino);
  192. return -EFSCORRUPTED;
  193. }
  194. return 0;
  195. }
  196. /*
  197. * Initialize a directory with its "." and ".." entries.
  198. */
  199. int
  200. xfs_dir_init(
  201. xfs_trans_t *tp,
  202. xfs_inode_t *dp,
  203. xfs_inode_t *pdp)
  204. {
  205. struct xfs_da_args *args;
  206. int error;
  207. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  208. error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino);
  209. if (error)
  210. return error;
  211. args = kmem_zalloc(sizeof(*args), KM_NOFS);
  212. if (!args)
  213. return -ENOMEM;
  214. args->geo = dp->i_mount->m_dir_geo;
  215. args->dp = dp;
  216. args->trans = tp;
  217. error = xfs_dir2_sf_create(args, pdp->i_ino);
  218. kmem_free(args);
  219. return error;
  220. }
  221. /*
  222. * Enter a name in a directory, or check for available space.
  223. * If inum is 0, only the available space test is performed.
  224. */
  225. int
  226. xfs_dir_createname(
  227. struct xfs_trans *tp,
  228. struct xfs_inode *dp,
  229. const struct xfs_name *name,
  230. xfs_ino_t inum, /* new entry inode number */
  231. xfs_extlen_t total) /* bmap's total block count */
  232. {
  233. struct xfs_da_args *args;
  234. int rval;
  235. bool v;
  236. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  237. if (inum) {
  238. rval = xfs_dir_ino_validate(tp->t_mountp, inum);
  239. if (rval)
  240. return rval;
  241. XFS_STATS_INC(dp->i_mount, xs_dir_create);
  242. }
  243. args = kmem_zalloc(sizeof(*args), KM_NOFS);
  244. if (!args)
  245. return -ENOMEM;
  246. args->geo = dp->i_mount->m_dir_geo;
  247. args->name = name->name;
  248. args->namelen = name->len;
  249. args->filetype = name->type;
  250. args->hashval = xfs_dir2_hashname(dp->i_mount, name);
  251. args->inumber = inum;
  252. args->dp = dp;
  253. args->total = total;
  254. args->whichfork = XFS_DATA_FORK;
  255. args->trans = tp;
  256. args->op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
  257. if (!inum)
  258. args->op_flags |= XFS_DA_OP_JUSTCHECK;
  259. if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
  260. rval = xfs_dir2_sf_addname(args);
  261. goto out_free;
  262. }
  263. rval = xfs_dir2_isblock(args, &v);
  264. if (rval)
  265. goto out_free;
  266. if (v) {
  267. rval = xfs_dir2_block_addname(args);
  268. goto out_free;
  269. }
  270. rval = xfs_dir2_isleaf(args, &v);
  271. if (rval)
  272. goto out_free;
  273. if (v)
  274. rval = xfs_dir2_leaf_addname(args);
  275. else
  276. rval = xfs_dir2_node_addname(args);
  277. out_free:
  278. kmem_free(args);
  279. return rval;
  280. }
  281. /*
  282. * If doing a CI lookup and case-insensitive match, dup actual name into
  283. * args.value. Return EEXIST for success (ie. name found) or an error.
  284. */
  285. int
  286. xfs_dir_cilookup_result(
  287. struct xfs_da_args *args,
  288. const unsigned char *name,
  289. int len)
  290. {
  291. if (args->cmpresult == XFS_CMP_DIFFERENT)
  292. return -ENOENT;
  293. if (args->cmpresult != XFS_CMP_CASE ||
  294. !(args->op_flags & XFS_DA_OP_CILOOKUP))
  295. return -EEXIST;
  296. args->value = kmem_alloc(len, KM_NOFS | KM_MAYFAIL);
  297. if (!args->value)
  298. return -ENOMEM;
  299. memcpy(args->value, name, len);
  300. args->valuelen = len;
  301. return -EEXIST;
  302. }
  303. /*
  304. * Lookup a name in a directory, give back the inode number.
  305. * If ci_name is not NULL, returns the actual name in ci_name if it differs
  306. * to name, or ci_name->name is set to NULL for an exact match.
  307. */
  308. int
  309. xfs_dir_lookup(
  310. struct xfs_trans *tp,
  311. struct xfs_inode *dp,
  312. const struct xfs_name *name,
  313. xfs_ino_t *inum, /* out: inode number */
  314. struct xfs_name *ci_name) /* out: actual name if CI match */
  315. {
  316. struct xfs_da_args *args;
  317. int rval;
  318. bool v;
  319. int lock_mode;
  320. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  321. XFS_STATS_INC(dp->i_mount, xs_dir_lookup);
  322. /*
  323. * We need to use KM_NOFS here so that lockdep will not throw false
  324. * positive deadlock warnings on a non-transactional lookup path. It is
  325. * safe to recurse into inode recalim in that case, but lockdep can't
  326. * easily be taught about it. Hence KM_NOFS avoids having to add more
  327. * lockdep Doing this avoids having to add a bunch of lockdep class
  328. * annotations into the reclaim path for the ilock.
  329. */
  330. args = kmem_zalloc(sizeof(*args), KM_NOFS);
  331. args->geo = dp->i_mount->m_dir_geo;
  332. args->name = name->name;
  333. args->namelen = name->len;
  334. args->filetype = name->type;
  335. args->hashval = xfs_dir2_hashname(dp->i_mount, name);
  336. args->dp = dp;
  337. args->whichfork = XFS_DATA_FORK;
  338. args->trans = tp;
  339. args->op_flags = XFS_DA_OP_OKNOENT;
  340. if (ci_name)
  341. args->op_flags |= XFS_DA_OP_CILOOKUP;
  342. lock_mode = xfs_ilock_data_map_shared(dp);
  343. if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
  344. rval = xfs_dir2_sf_lookup(args);
  345. goto out_check_rval;
  346. }
  347. rval = xfs_dir2_isblock(args, &v);
  348. if (rval)
  349. goto out_free;
  350. if (v) {
  351. rval = xfs_dir2_block_lookup(args);
  352. goto out_check_rval;
  353. }
  354. rval = xfs_dir2_isleaf(args, &v);
  355. if (rval)
  356. goto out_free;
  357. if (v)
  358. rval = xfs_dir2_leaf_lookup(args);
  359. else
  360. rval = xfs_dir2_node_lookup(args);
  361. out_check_rval:
  362. if (rval == -EEXIST)
  363. rval = 0;
  364. if (!rval) {
  365. *inum = args->inumber;
  366. if (ci_name) {
  367. ci_name->name = args->value;
  368. ci_name->len = args->valuelen;
  369. }
  370. }
  371. out_free:
  372. xfs_iunlock(dp, lock_mode);
  373. kmem_free(args);
  374. return rval;
  375. }
  376. /*
  377. * Remove an entry from a directory.
  378. */
  379. int
  380. xfs_dir_removename(
  381. struct xfs_trans *tp,
  382. struct xfs_inode *dp,
  383. struct xfs_name *name,
  384. xfs_ino_t ino,
  385. xfs_extlen_t total) /* bmap's total block count */
  386. {
  387. struct xfs_da_args *args;
  388. int rval;
  389. bool v;
  390. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  391. XFS_STATS_INC(dp->i_mount, xs_dir_remove);
  392. args = kmem_zalloc(sizeof(*args), KM_NOFS);
  393. if (!args)
  394. return -ENOMEM;
  395. args->geo = dp->i_mount->m_dir_geo;
  396. args->name = name->name;
  397. args->namelen = name->len;
  398. args->filetype = name->type;
  399. args->hashval = xfs_dir2_hashname(dp->i_mount, name);
  400. args->inumber = ino;
  401. args->dp = dp;
  402. args->total = total;
  403. args->whichfork = XFS_DATA_FORK;
  404. args->trans = tp;
  405. if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
  406. rval = xfs_dir2_sf_removename(args);
  407. goto out_free;
  408. }
  409. rval = xfs_dir2_isblock(args, &v);
  410. if (rval)
  411. goto out_free;
  412. if (v) {
  413. rval = xfs_dir2_block_removename(args);
  414. goto out_free;
  415. }
  416. rval = xfs_dir2_isleaf(args, &v);
  417. if (rval)
  418. goto out_free;
  419. if (v)
  420. rval = xfs_dir2_leaf_removename(args);
  421. else
  422. rval = xfs_dir2_node_removename(args);
  423. out_free:
  424. kmem_free(args);
  425. return rval;
  426. }
  427. /*
  428. * Replace the inode number of a directory entry.
  429. */
  430. int
  431. xfs_dir_replace(
  432. struct xfs_trans *tp,
  433. struct xfs_inode *dp,
  434. const struct xfs_name *name, /* name of entry to replace */
  435. xfs_ino_t inum, /* new inode number */
  436. xfs_extlen_t total) /* bmap's total block count */
  437. {
  438. struct xfs_da_args *args;
  439. int rval;
  440. bool v;
  441. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  442. rval = xfs_dir_ino_validate(tp->t_mountp, inum);
  443. if (rval)
  444. return rval;
  445. args = kmem_zalloc(sizeof(*args), KM_NOFS);
  446. if (!args)
  447. return -ENOMEM;
  448. args->geo = dp->i_mount->m_dir_geo;
  449. args->name = name->name;
  450. args->namelen = name->len;
  451. args->filetype = name->type;
  452. args->hashval = xfs_dir2_hashname(dp->i_mount, name);
  453. args->inumber = inum;
  454. args->dp = dp;
  455. args->total = total;
  456. args->whichfork = XFS_DATA_FORK;
  457. args->trans = tp;
  458. if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL) {
  459. rval = xfs_dir2_sf_replace(args);
  460. goto out_free;
  461. }
  462. rval = xfs_dir2_isblock(args, &v);
  463. if (rval)
  464. goto out_free;
  465. if (v) {
  466. rval = xfs_dir2_block_replace(args);
  467. goto out_free;
  468. }
  469. rval = xfs_dir2_isleaf(args, &v);
  470. if (rval)
  471. goto out_free;
  472. if (v)
  473. rval = xfs_dir2_leaf_replace(args);
  474. else
  475. rval = xfs_dir2_node_replace(args);
  476. out_free:
  477. kmem_free(args);
  478. return rval;
  479. }
  480. /*
  481. * See if this entry can be added to the directory without allocating space.
  482. */
  483. int
  484. xfs_dir_canenter(
  485. xfs_trans_t *tp,
  486. xfs_inode_t *dp,
  487. struct xfs_name *name) /* name of entry to add */
  488. {
  489. return xfs_dir_createname(tp, dp, name, 0, 0);
  490. }
  491. /*
  492. * Utility routines.
  493. */
  494. /*
  495. * Add a block to the directory.
  496. *
  497. * This routine is for data and free blocks, not leaf/node blocks which are
  498. * handled by xfs_da_grow_inode.
  499. */
  500. int
  501. xfs_dir2_grow_inode(
  502. struct xfs_da_args *args,
  503. int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
  504. xfs_dir2_db_t *dbp) /* out: block number added */
  505. {
  506. struct xfs_inode *dp = args->dp;
  507. struct xfs_mount *mp = dp->i_mount;
  508. xfs_fileoff_t bno; /* directory offset of new block */
  509. int count; /* count of filesystem blocks */
  510. int error;
  511. trace_xfs_dir2_grow_inode(args, space);
  512. /*
  513. * Set lowest possible block in the space requested.
  514. */
  515. bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
  516. count = args->geo->fsbcount;
  517. error = xfs_da_grow_inode_int(args, &bno, count);
  518. if (error)
  519. return error;
  520. *dbp = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)bno);
  521. /*
  522. * Update file's size if this is the data space and it grew.
  523. */
  524. if (space == XFS_DIR2_DATA_SPACE) {
  525. xfs_fsize_t size; /* directory file (data) size */
  526. size = XFS_FSB_TO_B(mp, bno + count);
  527. if (size > dp->i_disk_size) {
  528. dp->i_disk_size = size;
  529. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
  530. }
  531. }
  532. return 0;
  533. }
  534. /*
  535. * See if the directory is a single-block form directory.
  536. */
  537. int
  538. xfs_dir2_isblock(
  539. struct xfs_da_args *args,
  540. bool *isblock)
  541. {
  542. struct xfs_mount *mp = args->dp->i_mount;
  543. xfs_fileoff_t eof;
  544. int error;
  545. error = xfs_bmap_last_offset(args->dp, &eof, XFS_DATA_FORK);
  546. if (error)
  547. return error;
  548. *isblock = false;
  549. if (XFS_FSB_TO_B(mp, eof) != args->geo->blksize)
  550. return 0;
  551. *isblock = true;
  552. if (XFS_IS_CORRUPT(mp, args->dp->i_disk_size != args->geo->blksize))
  553. return -EFSCORRUPTED;
  554. return 0;
  555. }
  556. /*
  557. * See if the directory is a single-leaf form directory.
  558. */
  559. int
  560. xfs_dir2_isleaf(
  561. struct xfs_da_args *args,
  562. bool *isleaf)
  563. {
  564. xfs_fileoff_t eof;
  565. int error;
  566. error = xfs_bmap_last_offset(args->dp, &eof, XFS_DATA_FORK);
  567. if (error)
  568. return error;
  569. *isleaf = false;
  570. if (eof != args->geo->leafblk + args->geo->fsbcount)
  571. return 0;
  572. *isleaf = true;
  573. return 0;
  574. }
  575. /*
  576. * Remove the given block from the directory.
  577. * This routine is used for data and free blocks, leaf/node are done
  578. * by xfs_da_shrink_inode.
  579. */
  580. int
  581. xfs_dir2_shrink_inode(
  582. struct xfs_da_args *args,
  583. xfs_dir2_db_t db,
  584. struct xfs_buf *bp)
  585. {
  586. xfs_fileoff_t bno; /* directory file offset */
  587. xfs_dablk_t da; /* directory file offset */
  588. int done; /* bunmap is finished */
  589. struct xfs_inode *dp;
  590. int error;
  591. struct xfs_mount *mp;
  592. struct xfs_trans *tp;
  593. trace_xfs_dir2_shrink_inode(args, db);
  594. dp = args->dp;
  595. mp = dp->i_mount;
  596. tp = args->trans;
  597. da = xfs_dir2_db_to_da(args->geo, db);
  598. /* Unmap the fsblock(s). */
  599. error = xfs_bunmapi(tp, dp, da, args->geo->fsbcount, 0, 0, &done);
  600. if (error) {
  601. /*
  602. * ENOSPC actually can happen if we're in a removename with no
  603. * space reservation, and the resulting block removal would
  604. * cause a bmap btree split or conversion from extents to btree.
  605. * This can only happen for un-fragmented directory blocks,
  606. * since you need to be punching out the middle of an extent.
  607. * In this case we need to leave the block in the file, and not
  608. * binval it. So the block has to be in a consistent empty
  609. * state and appropriately logged. We don't free up the buffer,
  610. * the caller can tell it hasn't happened since it got an error
  611. * back.
  612. */
  613. return error;
  614. }
  615. ASSERT(done);
  616. /*
  617. * Invalidate the buffer from the transaction.
  618. */
  619. xfs_trans_binval(tp, bp);
  620. /*
  621. * If it's not a data block, we're done.
  622. */
  623. if (db >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET))
  624. return 0;
  625. /*
  626. * If the block isn't the last one in the directory, we're done.
  627. */
  628. if (dp->i_disk_size > xfs_dir2_db_off_to_byte(args->geo, db + 1, 0))
  629. return 0;
  630. bno = da;
  631. if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
  632. /*
  633. * This can't really happen unless there's kernel corruption.
  634. */
  635. return error;
  636. }
  637. if (db == args->geo->datablk)
  638. ASSERT(bno == 0);
  639. else
  640. ASSERT(bno > 0);
  641. /*
  642. * Set the size to the new last block.
  643. */
  644. dp->i_disk_size = XFS_FSB_TO_B(mp, bno);
  645. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  646. return 0;
  647. }
  648. /* Returns true if the directory entry name is valid. */
  649. bool
  650. xfs_dir2_namecheck(
  651. const void *name,
  652. size_t length)
  653. {
  654. /*
  655. * MAXNAMELEN includes the trailing null, but (name/length) leave it
  656. * out, so use >= for the length check.
  657. */
  658. if (length >= MAXNAMELEN)
  659. return false;
  660. /* There shouldn't be any slashes or nulls here */
  661. return !memchr(name, '/', length) && !memchr(name, 0, length);
  662. }
  663. xfs_dahash_t
  664. xfs_dir2_hashname(
  665. struct xfs_mount *mp,
  666. const struct xfs_name *name)
  667. {
  668. if (unlikely(xfs_has_asciici(mp)))
  669. return xfs_ascii_ci_hashname(name);
  670. return xfs_da_hashname(name->name, name->len);
  671. }
  672. enum xfs_dacmp
  673. xfs_dir2_compname(
  674. struct xfs_da_args *args,
  675. const unsigned char *name,
  676. int len)
  677. {
  678. if (unlikely(xfs_has_asciici(args->dp->i_mount)))
  679. return xfs_ascii_ci_compname(args, name, len);
  680. return xfs_da_compname(args, name, len);
  681. }