xfs_dir2.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413
  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. #include "xfs_health.h"
  22. #include "xfs_bmap_btree.h"
  23. #include "xfs_trans_space.h"
  24. #include "xfs_parent.h"
  25. #include "xfs_ag.h"
  26. #include "xfs_ialloc.h"
  27. const struct xfs_name xfs_name_dotdot = {
  28. .name = (const unsigned char *)"..",
  29. .len = 2,
  30. .type = XFS_DIR3_FT_DIR,
  31. };
  32. const struct xfs_name xfs_name_dot = {
  33. .name = (const unsigned char *)".",
  34. .len = 1,
  35. .type = XFS_DIR3_FT_DIR,
  36. };
  37. /*
  38. * Convert inode mode to directory entry filetype
  39. */
  40. unsigned char
  41. xfs_mode_to_ftype(
  42. int mode)
  43. {
  44. switch (mode & S_IFMT) {
  45. case S_IFREG:
  46. return XFS_DIR3_FT_REG_FILE;
  47. case S_IFDIR:
  48. return XFS_DIR3_FT_DIR;
  49. case S_IFCHR:
  50. return XFS_DIR3_FT_CHRDEV;
  51. case S_IFBLK:
  52. return XFS_DIR3_FT_BLKDEV;
  53. case S_IFIFO:
  54. return XFS_DIR3_FT_FIFO;
  55. case S_IFSOCK:
  56. return XFS_DIR3_FT_SOCK;
  57. case S_IFLNK:
  58. return XFS_DIR3_FT_SYMLINK;
  59. default:
  60. return XFS_DIR3_FT_UNKNOWN;
  61. }
  62. }
  63. /*
  64. * ASCII case-insensitive (ie. A-Z) support for directories that was
  65. * used in IRIX.
  66. */
  67. xfs_dahash_t
  68. xfs_ascii_ci_hashname(
  69. const struct xfs_name *name)
  70. {
  71. xfs_dahash_t hash;
  72. int i;
  73. for (i = 0, hash = 0; i < name->len; i++)
  74. hash = xfs_ascii_ci_xfrm(name->name[i]) ^ rol32(hash, 7);
  75. return hash;
  76. }
  77. enum xfs_dacmp
  78. xfs_ascii_ci_compname(
  79. struct xfs_da_args *args,
  80. const unsigned char *name,
  81. int len)
  82. {
  83. enum xfs_dacmp result;
  84. int i;
  85. if (args->namelen != len)
  86. return XFS_CMP_DIFFERENT;
  87. result = XFS_CMP_EXACT;
  88. for (i = 0; i < len; i++) {
  89. if (args->name[i] == name[i])
  90. continue;
  91. if (xfs_ascii_ci_xfrm(args->name[i]) !=
  92. xfs_ascii_ci_xfrm(name[i]))
  93. return XFS_CMP_DIFFERENT;
  94. result = XFS_CMP_CASE;
  95. }
  96. return result;
  97. }
  98. int
  99. xfs_da_mount(
  100. struct xfs_mount *mp)
  101. {
  102. struct xfs_da_geometry *dageo;
  103. ASSERT(mp->m_sb.sb_versionnum & XFS_SB_VERSION_DIRV2BIT);
  104. ASSERT(xfs_dir2_dirblock_bytes(&mp->m_sb) <= XFS_MAX_BLOCKSIZE);
  105. mp->m_dir_geo = kzalloc(sizeof(struct xfs_da_geometry),
  106. GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  107. mp->m_attr_geo = kzalloc(sizeof(struct xfs_da_geometry),
  108. GFP_KERNEL | __GFP_RETRY_MAYFAIL);
  109. if (!mp->m_dir_geo || !mp->m_attr_geo) {
  110. kfree(mp->m_dir_geo);
  111. kfree(mp->m_attr_geo);
  112. return -ENOMEM;
  113. }
  114. /* set up directory geometry */
  115. dageo = mp->m_dir_geo;
  116. dageo->blklog = mp->m_sb.sb_blocklog + mp->m_sb.sb_dirblklog;
  117. dageo->fsblog = mp->m_sb.sb_blocklog;
  118. dageo->blksize = xfs_dir2_dirblock_bytes(&mp->m_sb);
  119. dageo->fsbcount = 1 << mp->m_sb.sb_dirblklog;
  120. if (xfs_has_crc(mp)) {
  121. dageo->node_hdr_size = sizeof(struct xfs_da3_node_hdr);
  122. dageo->leaf_hdr_size = sizeof(struct xfs_dir3_leaf_hdr);
  123. dageo->free_hdr_size = sizeof(struct xfs_dir3_free_hdr);
  124. dageo->data_entry_offset =
  125. sizeof(struct xfs_dir3_data_hdr);
  126. } else {
  127. dageo->node_hdr_size = sizeof(struct xfs_da_node_hdr);
  128. dageo->leaf_hdr_size = sizeof(struct xfs_dir2_leaf_hdr);
  129. dageo->free_hdr_size = sizeof(struct xfs_dir2_free_hdr);
  130. dageo->data_entry_offset =
  131. sizeof(struct xfs_dir2_data_hdr);
  132. }
  133. dageo->leaf_max_ents = (dageo->blksize - dageo->leaf_hdr_size) /
  134. sizeof(struct xfs_dir2_leaf_entry);
  135. dageo->free_max_bests = (dageo->blksize - dageo->free_hdr_size) /
  136. sizeof(xfs_dir2_data_off_t);
  137. dageo->data_first_offset = dageo->data_entry_offset +
  138. xfs_dir2_data_entsize(mp, 1) +
  139. xfs_dir2_data_entsize(mp, 2);
  140. /*
  141. * Now we've set up the block conversion variables, we can calculate the
  142. * segment block constants using the geometry structure.
  143. */
  144. dageo->datablk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_DATA_OFFSET);
  145. dageo->leafblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_LEAF_OFFSET);
  146. dageo->freeblk = xfs_dir2_byte_to_da(dageo, XFS_DIR2_FREE_OFFSET);
  147. dageo->node_ents = (dageo->blksize - dageo->node_hdr_size) /
  148. (uint)sizeof(xfs_da_node_entry_t);
  149. dageo->max_extents = (XFS_DIR2_MAX_SPACES * XFS_DIR2_SPACE_SIZE) >>
  150. mp->m_sb.sb_blocklog;
  151. dageo->magicpct = (dageo->blksize * 37) / 100;
  152. /* set up attribute geometry - single fsb only */
  153. dageo = mp->m_attr_geo;
  154. dageo->blklog = mp->m_sb.sb_blocklog;
  155. dageo->fsblog = mp->m_sb.sb_blocklog;
  156. dageo->blksize = 1 << dageo->blklog;
  157. dageo->fsbcount = 1;
  158. dageo->node_hdr_size = mp->m_dir_geo->node_hdr_size;
  159. dageo->node_ents = (dageo->blksize - dageo->node_hdr_size) /
  160. (uint)sizeof(xfs_da_node_entry_t);
  161. if (xfs_has_large_extent_counts(mp))
  162. dageo->max_extents = XFS_MAX_EXTCNT_ATTR_FORK_LARGE;
  163. else
  164. dageo->max_extents = XFS_MAX_EXTCNT_ATTR_FORK_SMALL;
  165. dageo->magicpct = (dageo->blksize * 37) / 100;
  166. return 0;
  167. }
  168. void
  169. xfs_da_unmount(
  170. struct xfs_mount *mp)
  171. {
  172. kfree(mp->m_dir_geo);
  173. kfree(mp->m_attr_geo);
  174. }
  175. /*
  176. * Return 1 if directory contains only "." and "..".
  177. */
  178. int
  179. xfs_dir_isempty(
  180. xfs_inode_t *dp)
  181. {
  182. xfs_dir2_sf_hdr_t *sfp;
  183. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  184. if (dp->i_disk_size == 0) /* might happen during shutdown. */
  185. return 1;
  186. if (dp->i_disk_size > xfs_inode_data_fork_size(dp))
  187. return 0;
  188. sfp = dp->i_df.if_data;
  189. return !sfp->count;
  190. }
  191. /*
  192. * Validate a given inode number.
  193. */
  194. int
  195. xfs_dir_ino_validate(
  196. xfs_mount_t *mp,
  197. xfs_ino_t ino)
  198. {
  199. bool ino_ok = xfs_verify_dir_ino(mp, ino);
  200. if (XFS_IS_CORRUPT(mp, !ino_ok) ||
  201. XFS_TEST_ERROR(false, mp, XFS_ERRTAG_DIR_INO_VALIDATE)) {
  202. xfs_warn(mp, "Invalid inode number 0x%Lx",
  203. (unsigned long long) ino);
  204. return -EFSCORRUPTED;
  205. }
  206. return 0;
  207. }
  208. /*
  209. * Initialize a directory with its "." and ".." entries.
  210. */
  211. int
  212. xfs_dir_init(
  213. xfs_trans_t *tp,
  214. xfs_inode_t *dp,
  215. xfs_inode_t *pdp)
  216. {
  217. struct xfs_da_args *args;
  218. int error;
  219. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  220. error = xfs_dir_ino_validate(tp->t_mountp, pdp->i_ino);
  221. if (error)
  222. return error;
  223. args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL);
  224. if (!args)
  225. return -ENOMEM;
  226. args->geo = dp->i_mount->m_dir_geo;
  227. args->dp = dp;
  228. args->trans = tp;
  229. args->owner = dp->i_ino;
  230. error = xfs_dir2_sf_create(args, pdp->i_ino);
  231. kfree(args);
  232. return error;
  233. }
  234. enum xfs_dir2_fmt
  235. xfs_dir2_format(
  236. struct xfs_da_args *args,
  237. int *error)
  238. {
  239. struct xfs_inode *dp = args->dp;
  240. struct xfs_mount *mp = dp->i_mount;
  241. struct xfs_da_geometry *geo = mp->m_dir_geo;
  242. xfs_fileoff_t eof;
  243. xfs_assert_ilocked(dp, XFS_ILOCK_SHARED | XFS_ILOCK_EXCL);
  244. *error = 0;
  245. if (dp->i_df.if_format == XFS_DINODE_FMT_LOCAL)
  246. return XFS_DIR2_FMT_SF;
  247. *error = xfs_bmap_last_offset(dp, &eof, XFS_DATA_FORK);
  248. if (*error)
  249. return XFS_DIR2_FMT_ERROR;
  250. if (eof == XFS_B_TO_FSB(mp, geo->blksize)) {
  251. if (XFS_IS_CORRUPT(mp, dp->i_disk_size != geo->blksize)) {
  252. xfs_da_mark_sick(args);
  253. *error = -EFSCORRUPTED;
  254. return XFS_DIR2_FMT_ERROR;
  255. }
  256. return XFS_DIR2_FMT_BLOCK;
  257. }
  258. if (eof == geo->leafblk + geo->fsbcount)
  259. return XFS_DIR2_FMT_LEAF;
  260. return XFS_DIR2_FMT_NODE;
  261. }
  262. int
  263. xfs_dir_createname_args(
  264. struct xfs_da_args *args)
  265. {
  266. int error;
  267. if (!args->inumber)
  268. args->op_flags |= XFS_DA_OP_JUSTCHECK;
  269. switch (xfs_dir2_format(args, &error)) {
  270. case XFS_DIR2_FMT_SF:
  271. return xfs_dir2_sf_addname(args);
  272. case XFS_DIR2_FMT_BLOCK:
  273. return xfs_dir2_block_addname(args);
  274. case XFS_DIR2_FMT_LEAF:
  275. return xfs_dir2_leaf_addname(args);
  276. case XFS_DIR2_FMT_NODE:
  277. return xfs_dir2_node_addname(args);
  278. default:
  279. return error;
  280. }
  281. }
  282. /*
  283. * Enter a name in a directory, or check for available space.
  284. * If inum is 0, only the available space test is performed.
  285. */
  286. int
  287. xfs_dir_createname(
  288. struct xfs_trans *tp,
  289. struct xfs_inode *dp,
  290. const struct xfs_name *name,
  291. xfs_ino_t inum, /* new entry inode number */
  292. xfs_extlen_t total) /* bmap's total block count */
  293. {
  294. struct xfs_da_args *args;
  295. int rval;
  296. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  297. if (inum) {
  298. rval = xfs_dir_ino_validate(tp->t_mountp, inum);
  299. if (rval)
  300. return rval;
  301. XFS_STATS_INC(dp->i_mount, xs_dir_create);
  302. }
  303. args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL);
  304. if (!args)
  305. return -ENOMEM;
  306. args->geo = dp->i_mount->m_dir_geo;
  307. args->name = name->name;
  308. args->namelen = name->len;
  309. args->filetype = name->type;
  310. args->hashval = xfs_dir2_hashname(dp->i_mount, name);
  311. args->inumber = inum;
  312. args->dp = dp;
  313. args->total = total;
  314. args->whichfork = XFS_DATA_FORK;
  315. args->trans = tp;
  316. args->op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
  317. args->owner = dp->i_ino;
  318. rval = xfs_dir_createname_args(args);
  319. kfree(args);
  320. return rval;
  321. }
  322. /*
  323. * If doing a CI lookup and case-insensitive match, dup actual name into
  324. * args.value. Return EEXIST for success (ie. name found) or an error.
  325. */
  326. int
  327. xfs_dir_cilookup_result(
  328. struct xfs_da_args *args,
  329. const unsigned char *name,
  330. int len)
  331. {
  332. if (args->cmpresult == XFS_CMP_DIFFERENT)
  333. return -ENOENT;
  334. if (args->cmpresult != XFS_CMP_CASE ||
  335. !(args->op_flags & XFS_DA_OP_CILOOKUP))
  336. return -EEXIST;
  337. args->value = kmalloc(len,
  338. GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_RETRY_MAYFAIL);
  339. if (!args->value)
  340. return -ENOMEM;
  341. memcpy(args->value, name, len);
  342. args->valuelen = len;
  343. return -EEXIST;
  344. }
  345. int
  346. xfs_dir_lookup_args(
  347. struct xfs_da_args *args)
  348. {
  349. int error;
  350. switch (xfs_dir2_format(args, &error)) {
  351. case XFS_DIR2_FMT_SF:
  352. error = xfs_dir2_sf_lookup(args);
  353. break;
  354. case XFS_DIR2_FMT_BLOCK:
  355. error = xfs_dir2_block_lookup(args);
  356. break;
  357. case XFS_DIR2_FMT_LEAF:
  358. error = xfs_dir2_leaf_lookup(args);
  359. break;
  360. case XFS_DIR2_FMT_NODE:
  361. error = xfs_dir2_node_lookup(args);
  362. break;
  363. default:
  364. break;
  365. }
  366. if (error != -EEXIST)
  367. return error;
  368. return 0;
  369. }
  370. /*
  371. * Lookup a name in a directory, give back the inode number.
  372. * If ci_name is not NULL, returns the actual name in ci_name if it differs
  373. * to name, or ci_name->name is set to NULL for an exact match.
  374. */
  375. int
  376. xfs_dir_lookup(
  377. struct xfs_trans *tp,
  378. struct xfs_inode *dp,
  379. const struct xfs_name *name,
  380. xfs_ino_t *inum, /* out: inode number */
  381. struct xfs_name *ci_name) /* out: actual name if CI match */
  382. {
  383. struct xfs_da_args *args;
  384. int rval;
  385. int lock_mode;
  386. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  387. XFS_STATS_INC(dp->i_mount, xs_dir_lookup);
  388. args = kzalloc(sizeof(*args),
  389. GFP_KERNEL | __GFP_NOLOCKDEP | __GFP_NOFAIL);
  390. args->geo = dp->i_mount->m_dir_geo;
  391. args->name = name->name;
  392. args->namelen = name->len;
  393. args->filetype = name->type;
  394. args->hashval = xfs_dir2_hashname(dp->i_mount, name);
  395. args->dp = dp;
  396. args->whichfork = XFS_DATA_FORK;
  397. args->trans = tp;
  398. args->op_flags = XFS_DA_OP_OKNOENT;
  399. args->owner = dp->i_ino;
  400. if (ci_name)
  401. args->op_flags |= XFS_DA_OP_CILOOKUP;
  402. lock_mode = xfs_ilock_data_map_shared(dp);
  403. rval = xfs_dir_lookup_args(args);
  404. if (!rval) {
  405. *inum = args->inumber;
  406. if (ci_name) {
  407. ci_name->name = args->value;
  408. ci_name->len = args->valuelen;
  409. }
  410. }
  411. xfs_iunlock(dp, lock_mode);
  412. kfree(args);
  413. return rval;
  414. }
  415. int
  416. xfs_dir_removename_args(
  417. struct xfs_da_args *args)
  418. {
  419. int error;
  420. switch (xfs_dir2_format(args, &error)) {
  421. case XFS_DIR2_FMT_SF:
  422. return xfs_dir2_sf_removename(args);
  423. case XFS_DIR2_FMT_BLOCK:
  424. return xfs_dir2_block_removename(args);
  425. case XFS_DIR2_FMT_LEAF:
  426. return xfs_dir2_leaf_removename(args);
  427. case XFS_DIR2_FMT_NODE:
  428. return xfs_dir2_node_removename(args);
  429. default:
  430. return error;
  431. }
  432. }
  433. /*
  434. * Remove an entry from a directory.
  435. */
  436. int
  437. xfs_dir_removename(
  438. struct xfs_trans *tp,
  439. struct xfs_inode *dp,
  440. const struct xfs_name *name,
  441. xfs_ino_t ino,
  442. xfs_extlen_t total) /* bmap's total block count */
  443. {
  444. struct xfs_da_args *args;
  445. int rval;
  446. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  447. XFS_STATS_INC(dp->i_mount, xs_dir_remove);
  448. args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL);
  449. if (!args)
  450. return -ENOMEM;
  451. args->geo = dp->i_mount->m_dir_geo;
  452. args->name = name->name;
  453. args->namelen = name->len;
  454. args->filetype = name->type;
  455. args->hashval = xfs_dir2_hashname(dp->i_mount, name);
  456. args->inumber = ino;
  457. args->dp = dp;
  458. args->total = total;
  459. args->whichfork = XFS_DATA_FORK;
  460. args->trans = tp;
  461. args->owner = dp->i_ino;
  462. rval = xfs_dir_removename_args(args);
  463. kfree(args);
  464. return rval;
  465. }
  466. int
  467. xfs_dir_replace_args(
  468. struct xfs_da_args *args)
  469. {
  470. int error;
  471. switch (xfs_dir2_format(args, &error)) {
  472. case XFS_DIR2_FMT_SF:
  473. return xfs_dir2_sf_replace(args);
  474. case XFS_DIR2_FMT_BLOCK:
  475. return xfs_dir2_block_replace(args);
  476. case XFS_DIR2_FMT_LEAF:
  477. return xfs_dir2_leaf_replace(args);
  478. case XFS_DIR2_FMT_NODE:
  479. return xfs_dir2_node_replace(args);
  480. default:
  481. return error;
  482. }
  483. }
  484. /*
  485. * Replace the inode number of a directory entry.
  486. */
  487. int
  488. xfs_dir_replace(
  489. struct xfs_trans *tp,
  490. struct xfs_inode *dp,
  491. const struct xfs_name *name, /* name of entry to replace */
  492. xfs_ino_t inum, /* new inode number */
  493. xfs_extlen_t total) /* bmap's total block count */
  494. {
  495. struct xfs_da_args *args;
  496. int rval;
  497. ASSERT(S_ISDIR(VFS_I(dp)->i_mode));
  498. rval = xfs_dir_ino_validate(tp->t_mountp, inum);
  499. if (rval)
  500. return rval;
  501. args = kzalloc(sizeof(*args), GFP_KERNEL | __GFP_NOFAIL);
  502. if (!args)
  503. return -ENOMEM;
  504. args->geo = dp->i_mount->m_dir_geo;
  505. args->name = name->name;
  506. args->namelen = name->len;
  507. args->filetype = name->type;
  508. args->hashval = xfs_dir2_hashname(dp->i_mount, name);
  509. args->inumber = inum;
  510. args->dp = dp;
  511. args->total = total;
  512. args->whichfork = XFS_DATA_FORK;
  513. args->trans = tp;
  514. args->owner = dp->i_ino;
  515. rval = xfs_dir_replace_args(args);
  516. kfree(args);
  517. return rval;
  518. }
  519. /*
  520. * See if this entry can be added to the directory without allocating space.
  521. */
  522. int
  523. xfs_dir_canenter(
  524. struct xfs_trans *tp,
  525. struct xfs_inode *dp,
  526. const struct xfs_name *name) /* name of entry to add */
  527. {
  528. return xfs_dir_createname(tp, dp, name, 0, 0);
  529. }
  530. /*
  531. * Utility routines.
  532. */
  533. /*
  534. * Add a block to the directory.
  535. *
  536. * This routine is for data and free blocks, not leaf/node blocks which are
  537. * handled by xfs_da_grow_inode.
  538. */
  539. int
  540. xfs_dir2_grow_inode(
  541. struct xfs_da_args *args,
  542. int space, /* v2 dir's space XFS_DIR2_xxx_SPACE */
  543. xfs_dir2_db_t *dbp) /* out: block number added */
  544. {
  545. struct xfs_inode *dp = args->dp;
  546. struct xfs_mount *mp = dp->i_mount;
  547. xfs_fileoff_t bno; /* directory offset of new block */
  548. int count; /* count of filesystem blocks */
  549. int error;
  550. trace_xfs_dir2_grow_inode(args, space);
  551. /*
  552. * Set lowest possible block in the space requested.
  553. */
  554. bno = XFS_B_TO_FSBT(mp, space * XFS_DIR2_SPACE_SIZE);
  555. count = args->geo->fsbcount;
  556. error = xfs_da_grow_inode_int(args, &bno, count);
  557. if (error)
  558. return error;
  559. *dbp = xfs_dir2_da_to_db(args->geo, (xfs_dablk_t)bno);
  560. /*
  561. * Update file's size if this is the data space and it grew.
  562. */
  563. if (space == XFS_DIR2_DATA_SPACE) {
  564. xfs_fsize_t size; /* directory file (data) size */
  565. size = XFS_FSB_TO_B(mp, bno + count);
  566. if (size > dp->i_disk_size) {
  567. dp->i_disk_size = size;
  568. xfs_trans_log_inode(args->trans, dp, XFS_ILOG_CORE);
  569. }
  570. }
  571. return 0;
  572. }
  573. /*
  574. * Remove the given block from the directory.
  575. * This routine is used for data and free blocks, leaf/node are done
  576. * by xfs_da_shrink_inode.
  577. */
  578. int
  579. xfs_dir2_shrink_inode(
  580. struct xfs_da_args *args,
  581. xfs_dir2_db_t db,
  582. struct xfs_buf *bp)
  583. {
  584. xfs_fileoff_t bno; /* directory file offset */
  585. xfs_dablk_t da; /* directory file offset */
  586. int done; /* bunmap is finished */
  587. struct xfs_inode *dp;
  588. int error;
  589. struct xfs_mount *mp;
  590. struct xfs_trans *tp;
  591. trace_xfs_dir2_shrink_inode(args, db);
  592. dp = args->dp;
  593. mp = dp->i_mount;
  594. tp = args->trans;
  595. da = xfs_dir2_db_to_da(args->geo, db);
  596. /* Unmap the fsblock(s). */
  597. error = xfs_bunmapi(tp, dp, da, args->geo->fsbcount, 0, 0, &done);
  598. if (error) {
  599. /*
  600. * ENOSPC actually can happen if we're in a removename with no
  601. * space reservation, and the resulting block removal would
  602. * cause a bmap btree split or conversion from extents to btree.
  603. * This can only happen for un-fragmented directory blocks,
  604. * since you need to be punching out the middle of an extent.
  605. * In this case we need to leave the block in the file, and not
  606. * binval it. So the block has to be in a consistent empty
  607. * state and appropriately logged. We don't free up the buffer,
  608. * the caller can tell it hasn't happened since it got an error
  609. * back.
  610. */
  611. return error;
  612. }
  613. ASSERT(done);
  614. /*
  615. * Invalidate the buffer from the transaction.
  616. */
  617. xfs_trans_binval(tp, bp);
  618. /*
  619. * If it's not a data block, we're done.
  620. */
  621. if (db >= xfs_dir2_byte_to_db(args->geo, XFS_DIR2_LEAF_OFFSET))
  622. return 0;
  623. /*
  624. * If the block isn't the last one in the directory, we're done.
  625. */
  626. if (dp->i_disk_size > xfs_dir2_db_off_to_byte(args->geo, db + 1, 0))
  627. return 0;
  628. bno = da;
  629. if ((error = xfs_bmap_last_before(tp, dp, &bno, XFS_DATA_FORK))) {
  630. /*
  631. * This can't really happen unless there's kernel corruption.
  632. */
  633. return error;
  634. }
  635. if (db == args->geo->datablk)
  636. ASSERT(bno == 0);
  637. else
  638. ASSERT(bno > 0);
  639. /*
  640. * Set the size to the new last block.
  641. */
  642. dp->i_disk_size = XFS_FSB_TO_B(mp, bno);
  643. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  644. return 0;
  645. }
  646. /* Returns true if the directory entry name is valid. */
  647. bool
  648. xfs_dir2_namecheck(
  649. const void *name,
  650. size_t length)
  651. {
  652. /*
  653. * MAXNAMELEN includes the trailing null, but (name/length) leave it
  654. * out, so use >= for the length check.
  655. */
  656. if (length >= MAXNAMELEN)
  657. return false;
  658. /* There shouldn't be any slashes or nulls here */
  659. return !memchr(name, '/', length) && !memchr(name, 0, length);
  660. }
  661. xfs_dahash_t
  662. xfs_dir2_hashname(
  663. struct xfs_mount *mp,
  664. const struct xfs_name *name)
  665. {
  666. if (unlikely(xfs_has_asciici(mp)))
  667. return xfs_ascii_ci_hashname(name);
  668. return xfs_da_hashname(name->name, name->len);
  669. }
  670. enum xfs_dacmp
  671. xfs_dir2_compname(
  672. struct xfs_da_args *args,
  673. const unsigned char *name,
  674. int len)
  675. {
  676. if (unlikely(xfs_has_asciici(args->dp->i_mount)))
  677. return xfs_ascii_ci_compname(args, name, len);
  678. return xfs_da_compname(args, name, len);
  679. }
  680. #ifdef CONFIG_XFS_LIVE_HOOKS
  681. /*
  682. * Use a static key here to reduce the overhead of directory live update hooks.
  683. * If the compiler supports jump labels, the static branch will be replaced by
  684. * a nop sled when there are no hook users. Online fsck is currently the only
  685. * caller, so this is a reasonable tradeoff.
  686. *
  687. * Note: Patching the kernel code requires taking the cpu hotplug lock. Other
  688. * parts of the kernel allocate memory with that lock held, which means that
  689. * XFS callers cannot hold any locks that might be used by memory reclaim or
  690. * writeback when calling the static_branch_{inc,dec} functions.
  691. */
  692. DEFINE_STATIC_XFS_HOOK_SWITCH(xfs_dir_hooks_switch);
  693. void
  694. xfs_dir_hook_disable(void)
  695. {
  696. xfs_hooks_switch_off(&xfs_dir_hooks_switch);
  697. }
  698. void
  699. xfs_dir_hook_enable(void)
  700. {
  701. xfs_hooks_switch_on(&xfs_dir_hooks_switch);
  702. }
  703. /* Call hooks for a directory update relating to a child dirent update. */
  704. inline void
  705. xfs_dir_update_hook(
  706. struct xfs_inode *dp,
  707. struct xfs_inode *ip,
  708. int delta,
  709. const struct xfs_name *name)
  710. {
  711. if (xfs_hooks_switched_on(&xfs_dir_hooks_switch)) {
  712. struct xfs_dir_update_params p = {
  713. .dp = dp,
  714. .ip = ip,
  715. .delta = delta,
  716. .name = name,
  717. };
  718. struct xfs_mount *mp = ip->i_mount;
  719. xfs_hooks_call(&mp->m_dir_update_hooks, 0, &p);
  720. }
  721. }
  722. /* Call the specified function during a directory update. */
  723. int
  724. xfs_dir_hook_add(
  725. struct xfs_mount *mp,
  726. struct xfs_dir_hook *hook)
  727. {
  728. return xfs_hooks_add(&mp->m_dir_update_hooks, &hook->dirent_hook);
  729. }
  730. /* Stop calling the specified function during a directory update. */
  731. void
  732. xfs_dir_hook_del(
  733. struct xfs_mount *mp,
  734. struct xfs_dir_hook *hook)
  735. {
  736. xfs_hooks_del(&mp->m_dir_update_hooks, &hook->dirent_hook);
  737. }
  738. /* Configure directory update hook functions. */
  739. void
  740. xfs_dir_hook_setup(
  741. struct xfs_dir_hook *hook,
  742. notifier_fn_t mod_fn)
  743. {
  744. xfs_hook_setup(&hook->dirent_hook, mod_fn);
  745. }
  746. #endif /* CONFIG_XFS_LIVE_HOOKS */
  747. /*
  748. * Given a directory @dp, a newly allocated inode @ip, and a @name, link @ip
  749. * into @dp under the given @name. If @ip is a directory, it will be
  750. * initialized. Both inodes must have the ILOCK held and the transaction must
  751. * have sufficient blocks reserved.
  752. */
  753. int
  754. xfs_dir_create_child(
  755. struct xfs_trans *tp,
  756. unsigned int resblks,
  757. struct xfs_dir_update *du)
  758. {
  759. struct xfs_inode *dp = du->dp;
  760. const struct xfs_name *name = du->name;
  761. struct xfs_inode *ip = du->ip;
  762. int error;
  763. xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
  764. xfs_assert_ilocked(dp, XFS_ILOCK_EXCL);
  765. error = xfs_dir_createname(tp, dp, name, ip->i_ino, resblks);
  766. if (error) {
  767. ASSERT(error != -ENOSPC);
  768. return error;
  769. }
  770. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  771. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  772. if (S_ISDIR(VFS_I(ip)->i_mode)) {
  773. error = xfs_dir_init(tp, ip, dp);
  774. if (error)
  775. return error;
  776. xfs_bumplink(tp, dp);
  777. }
  778. /*
  779. * If we have parent pointers, we need to add the attribute containing
  780. * the parent information now.
  781. */
  782. if (du->ppargs) {
  783. error = xfs_parent_addname(tp, du->ppargs, dp, name, ip);
  784. if (error)
  785. return error;
  786. }
  787. xfs_dir_update_hook(dp, ip, 1, name);
  788. return 0;
  789. }
  790. /*
  791. * Given a directory @dp, an existing non-directory inode @ip, and a @name,
  792. * link @ip into @dp under the given @name. Both inodes must have the ILOCK
  793. * held.
  794. */
  795. int
  796. xfs_dir_add_child(
  797. struct xfs_trans *tp,
  798. unsigned int resblks,
  799. struct xfs_dir_update *du)
  800. {
  801. struct xfs_inode *dp = du->dp;
  802. const struct xfs_name *name = du->name;
  803. struct xfs_inode *ip = du->ip;
  804. struct xfs_mount *mp = tp->t_mountp;
  805. int error;
  806. xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
  807. xfs_assert_ilocked(dp, XFS_ILOCK_EXCL);
  808. ASSERT(!S_ISDIR(VFS_I(ip)->i_mode));
  809. if (!resblks) {
  810. error = xfs_dir_canenter(tp, dp, name);
  811. if (error)
  812. return error;
  813. }
  814. /*
  815. * Handle initial link state of O_TMPFILE inode
  816. */
  817. if (VFS_I(ip)->i_nlink == 0) {
  818. struct xfs_perag *pag;
  819. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, ip->i_ino));
  820. error = xfs_iunlink_remove(tp, pag, ip);
  821. xfs_perag_put(pag);
  822. if (error)
  823. return error;
  824. }
  825. error = xfs_dir_createname(tp, dp, name, ip->i_ino, resblks);
  826. if (error)
  827. return error;
  828. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  829. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  830. xfs_bumplink(tp, ip);
  831. /*
  832. * If we have parent pointers, we now need to add the parent record to
  833. * the attribute fork of the inode. If this is the initial parent
  834. * attribute, we need to create it correctly, otherwise we can just add
  835. * the parent to the inode.
  836. */
  837. if (du->ppargs) {
  838. error = xfs_parent_addname(tp, du->ppargs, dp, name, ip);
  839. if (error)
  840. return error;
  841. }
  842. xfs_dir_update_hook(dp, ip, 1, name);
  843. return 0;
  844. }
  845. /*
  846. * Given a directory @dp, a child @ip, and a @name, remove the (@name, @ip)
  847. * entry from the directory. Both inodes must have the ILOCK held.
  848. */
  849. int
  850. xfs_dir_remove_child(
  851. struct xfs_trans *tp,
  852. unsigned int resblks,
  853. struct xfs_dir_update *du)
  854. {
  855. struct xfs_inode *dp = du->dp;
  856. const struct xfs_name *name = du->name;
  857. struct xfs_inode *ip = du->ip;
  858. int error;
  859. xfs_assert_ilocked(ip, XFS_ILOCK_EXCL);
  860. xfs_assert_ilocked(dp, XFS_ILOCK_EXCL);
  861. /*
  862. * If we're removing a directory perform some additional validation.
  863. */
  864. if (S_ISDIR(VFS_I(ip)->i_mode)) {
  865. ASSERT(VFS_I(ip)->i_nlink >= 2);
  866. if (VFS_I(ip)->i_nlink != 2)
  867. return -ENOTEMPTY;
  868. if (!xfs_dir_isempty(ip))
  869. return -ENOTEMPTY;
  870. /* Drop the link from ip's "..". */
  871. error = xfs_droplink(tp, dp);
  872. if (error)
  873. return error;
  874. /* Drop the "." link from ip to self. */
  875. error = xfs_droplink(tp, ip);
  876. if (error)
  877. return error;
  878. /*
  879. * Point the unlinked child directory's ".." entry to the root
  880. * directory to eliminate back-references to inodes that may
  881. * get freed before the child directory is closed. If the fs
  882. * gets shrunk, this can lead to dirent inode validation errors.
  883. */
  884. if (dp->i_ino != tp->t_mountp->m_sb.sb_rootino) {
  885. error = xfs_dir_replace(tp, ip, &xfs_name_dotdot,
  886. tp->t_mountp->m_sb.sb_rootino, 0);
  887. if (error)
  888. return error;
  889. }
  890. } else {
  891. /*
  892. * When removing a non-directory we need to log the parent
  893. * inode here. For a directory this is done implicitly
  894. * by the xfs_droplink call for the ".." entry.
  895. */
  896. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  897. }
  898. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  899. /* Drop the link from dp to ip. */
  900. error = xfs_droplink(tp, ip);
  901. if (error)
  902. return error;
  903. error = xfs_dir_removename(tp, dp, name, ip->i_ino, resblks);
  904. if (error) {
  905. ASSERT(error != -ENOENT);
  906. return error;
  907. }
  908. /* Remove parent pointer. */
  909. if (du->ppargs) {
  910. error = xfs_parent_removename(tp, du->ppargs, dp, name, ip);
  911. if (error)
  912. return error;
  913. }
  914. xfs_dir_update_hook(dp, ip, -1, name);
  915. return 0;
  916. }
  917. /*
  918. * Exchange the entry (@name1, @ip1) in directory @dp1 with the entry (@name2,
  919. * @ip2) in directory @dp2, and update '..' @ip1 and @ip2's entries as needed.
  920. * @ip1 and @ip2 need not be of the same type.
  921. *
  922. * All inodes must have the ILOCK held, and both entries must already exist.
  923. */
  924. int
  925. xfs_dir_exchange_children(
  926. struct xfs_trans *tp,
  927. struct xfs_dir_update *du1,
  928. struct xfs_dir_update *du2,
  929. unsigned int spaceres)
  930. {
  931. struct xfs_inode *dp1 = du1->dp;
  932. const struct xfs_name *name1 = du1->name;
  933. struct xfs_inode *ip1 = du1->ip;
  934. struct xfs_inode *dp2 = du2->dp;
  935. const struct xfs_name *name2 = du2->name;
  936. struct xfs_inode *ip2 = du2->ip;
  937. int ip1_flags = 0;
  938. int ip2_flags = 0;
  939. int dp2_flags = 0;
  940. int error;
  941. /* Swap inode number for dirent in first parent */
  942. error = xfs_dir_replace(tp, dp1, name1, ip2->i_ino, spaceres);
  943. if (error)
  944. return error;
  945. /* Swap inode number for dirent in second parent */
  946. error = xfs_dir_replace(tp, dp2, name2, ip1->i_ino, spaceres);
  947. if (error)
  948. return error;
  949. /*
  950. * If we're renaming one or more directories across different parents,
  951. * update the respective ".." entries (and link counts) to match the new
  952. * parents.
  953. */
  954. if (dp1 != dp2) {
  955. dp2_flags = XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  956. if (S_ISDIR(VFS_I(ip2)->i_mode)) {
  957. error = xfs_dir_replace(tp, ip2, &xfs_name_dotdot,
  958. dp1->i_ino, spaceres);
  959. if (error)
  960. return error;
  961. /* transfer ip2 ".." reference to dp1 */
  962. if (!S_ISDIR(VFS_I(ip1)->i_mode)) {
  963. error = xfs_droplink(tp, dp2);
  964. if (error)
  965. return error;
  966. xfs_bumplink(tp, dp1);
  967. }
  968. /*
  969. * Although ip1 isn't changed here, userspace needs
  970. * to be warned about the change, so that applications
  971. * relying on it (like backup ones), will properly
  972. * notify the change
  973. */
  974. ip1_flags |= XFS_ICHGTIME_CHG;
  975. ip2_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  976. }
  977. if (S_ISDIR(VFS_I(ip1)->i_mode)) {
  978. error = xfs_dir_replace(tp, ip1, &xfs_name_dotdot,
  979. dp2->i_ino, spaceres);
  980. if (error)
  981. return error;
  982. /* transfer ip1 ".." reference to dp2 */
  983. if (!S_ISDIR(VFS_I(ip2)->i_mode)) {
  984. error = xfs_droplink(tp, dp1);
  985. if (error)
  986. return error;
  987. xfs_bumplink(tp, dp2);
  988. }
  989. /*
  990. * Although ip2 isn't changed here, userspace needs
  991. * to be warned about the change, so that applications
  992. * relying on it (like backup ones), will properly
  993. * notify the change
  994. */
  995. ip1_flags |= XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG;
  996. ip2_flags |= XFS_ICHGTIME_CHG;
  997. }
  998. }
  999. if (ip1_flags) {
  1000. xfs_trans_ichgtime(tp, ip1, ip1_flags);
  1001. xfs_trans_log_inode(tp, ip1, XFS_ILOG_CORE);
  1002. }
  1003. if (ip2_flags) {
  1004. xfs_trans_ichgtime(tp, ip2, ip2_flags);
  1005. xfs_trans_log_inode(tp, ip2, XFS_ILOG_CORE);
  1006. }
  1007. if (dp2_flags) {
  1008. xfs_trans_ichgtime(tp, dp2, dp2_flags);
  1009. xfs_trans_log_inode(tp, dp2, XFS_ILOG_CORE);
  1010. }
  1011. xfs_trans_ichgtime(tp, dp1, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1012. xfs_trans_log_inode(tp, dp1, XFS_ILOG_CORE);
  1013. /* Schedule parent pointer replacements */
  1014. if (du1->ppargs) {
  1015. error = xfs_parent_replacename(tp, du1->ppargs, dp1, name1,
  1016. dp2, name2, ip1);
  1017. if (error)
  1018. return error;
  1019. }
  1020. if (du2->ppargs) {
  1021. error = xfs_parent_replacename(tp, du2->ppargs, dp2, name2,
  1022. dp1, name1, ip2);
  1023. if (error)
  1024. return error;
  1025. }
  1026. /*
  1027. * Inform our hook clients that we've finished an exchange operation as
  1028. * follows: removed the source and target files from their directories;
  1029. * added the target to the source directory; and added the source to
  1030. * the target directory. All inodes are locked, so it's ok to model a
  1031. * rename this way so long as we say we deleted entries before we add
  1032. * new ones.
  1033. */
  1034. xfs_dir_update_hook(dp1, ip1, -1, name1);
  1035. xfs_dir_update_hook(dp2, ip2, -1, name2);
  1036. xfs_dir_update_hook(dp1, ip2, 1, name1);
  1037. xfs_dir_update_hook(dp2, ip1, 1, name2);
  1038. return 0;
  1039. }
  1040. /*
  1041. * Given an entry (@src_name, @src_ip) in directory @src_dp, make the entry
  1042. * @target_name in directory @target_dp point to @src_ip and remove the
  1043. * original entry, cleaning up everything left behind.
  1044. *
  1045. * Cleanup involves dropping a link count on @target_ip, and either removing
  1046. * the (@src_name, @src_ip) entry from @src_dp or simply replacing the entry
  1047. * with (@src_name, @wip) if a whiteout inode @wip is supplied.
  1048. *
  1049. * All inodes must have the ILOCK held. We assume that if @src_ip is a
  1050. * directory then its '..' doesn't already point to @target_dp, and that @wip
  1051. * is a freshly allocated whiteout.
  1052. */
  1053. int
  1054. xfs_dir_rename_children(
  1055. struct xfs_trans *tp,
  1056. struct xfs_dir_update *du_src,
  1057. struct xfs_dir_update *du_tgt,
  1058. unsigned int spaceres,
  1059. struct xfs_dir_update *du_wip)
  1060. {
  1061. struct xfs_mount *mp = tp->t_mountp;
  1062. struct xfs_inode *src_dp = du_src->dp;
  1063. const struct xfs_name *src_name = du_src->name;
  1064. struct xfs_inode *src_ip = du_src->ip;
  1065. struct xfs_inode *target_dp = du_tgt->dp;
  1066. const struct xfs_name *target_name = du_tgt->name;
  1067. struct xfs_inode *target_ip = du_tgt->ip;
  1068. bool new_parent = (src_dp != target_dp);
  1069. bool src_is_directory;
  1070. int error;
  1071. src_is_directory = S_ISDIR(VFS_I(src_ip)->i_mode);
  1072. /*
  1073. * Check for expected errors before we dirty the transaction
  1074. * so we can return an error without a transaction abort.
  1075. */
  1076. if (target_ip == NULL) {
  1077. /*
  1078. * If there's no space reservation, check the entry will
  1079. * fit before actually inserting it.
  1080. */
  1081. if (!spaceres) {
  1082. error = xfs_dir_canenter(tp, target_dp, target_name);
  1083. if (error)
  1084. return error;
  1085. }
  1086. } else {
  1087. /*
  1088. * If target exists and it's a directory, check that whether
  1089. * it can be destroyed.
  1090. */
  1091. if (S_ISDIR(VFS_I(target_ip)->i_mode) &&
  1092. (!xfs_dir_isempty(target_ip) ||
  1093. (VFS_I(target_ip)->i_nlink > 2)))
  1094. return -EEXIST;
  1095. }
  1096. /*
  1097. * Directory entry creation below may acquire the AGF. Remove
  1098. * the whiteout from the unlinked list first to preserve correct
  1099. * AGI/AGF locking order. This dirties the transaction so failures
  1100. * after this point will abort and log recovery will clean up the
  1101. * mess.
  1102. *
  1103. * For whiteouts, we need to bump the link count on the whiteout
  1104. * inode. After this point, we have a real link, clear the tmpfile
  1105. * state flag from the inode so it doesn't accidentally get misused
  1106. * in future.
  1107. */
  1108. if (du_wip->ip) {
  1109. struct xfs_perag *pag;
  1110. ASSERT(VFS_I(du_wip->ip)->i_nlink == 0);
  1111. pag = xfs_perag_get(mp, XFS_INO_TO_AGNO(mp, du_wip->ip->i_ino));
  1112. error = xfs_iunlink_remove(tp, pag, du_wip->ip);
  1113. xfs_perag_put(pag);
  1114. if (error)
  1115. return error;
  1116. xfs_bumplink(tp, du_wip->ip);
  1117. }
  1118. /*
  1119. * Set up the target.
  1120. */
  1121. if (target_ip == NULL) {
  1122. /*
  1123. * If target does not exist and the rename crosses
  1124. * directories, adjust the target directory link count
  1125. * to account for the ".." reference from the new entry.
  1126. */
  1127. error = xfs_dir_createname(tp, target_dp, target_name,
  1128. src_ip->i_ino, spaceres);
  1129. if (error)
  1130. return error;
  1131. xfs_trans_ichgtime(tp, target_dp,
  1132. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1133. if (new_parent && src_is_directory) {
  1134. xfs_bumplink(tp, target_dp);
  1135. }
  1136. } else { /* target_ip != NULL */
  1137. /*
  1138. * Link the source inode under the target name.
  1139. * If the source inode is a directory and we are moving
  1140. * it across directories, its ".." entry will be
  1141. * inconsistent until we replace that down below.
  1142. *
  1143. * In case there is already an entry with the same
  1144. * name at the destination directory, remove it first.
  1145. */
  1146. error = xfs_dir_replace(tp, target_dp, target_name,
  1147. src_ip->i_ino, spaceres);
  1148. if (error)
  1149. return error;
  1150. xfs_trans_ichgtime(tp, target_dp,
  1151. XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1152. /*
  1153. * Decrement the link count on the target since the target
  1154. * dir no longer points to it.
  1155. */
  1156. error = xfs_droplink(tp, target_ip);
  1157. if (error)
  1158. return error;
  1159. if (src_is_directory) {
  1160. /*
  1161. * Drop the link from the old "." entry.
  1162. */
  1163. error = xfs_droplink(tp, target_ip);
  1164. if (error)
  1165. return error;
  1166. }
  1167. } /* target_ip != NULL */
  1168. /*
  1169. * Remove the source.
  1170. */
  1171. if (new_parent && src_is_directory) {
  1172. /*
  1173. * Rewrite the ".." entry to point to the new
  1174. * directory.
  1175. */
  1176. error = xfs_dir_replace(tp, src_ip, &xfs_name_dotdot,
  1177. target_dp->i_ino, spaceres);
  1178. ASSERT(error != -EEXIST);
  1179. if (error)
  1180. return error;
  1181. }
  1182. /*
  1183. * We always want to hit the ctime on the source inode.
  1184. *
  1185. * This isn't strictly required by the standards since the source
  1186. * inode isn't really being changed, but old unix file systems did
  1187. * it and some incremental backup programs won't work without it.
  1188. */
  1189. xfs_trans_ichgtime(tp, src_ip, XFS_ICHGTIME_CHG);
  1190. xfs_trans_log_inode(tp, src_ip, XFS_ILOG_CORE);
  1191. /*
  1192. * Adjust the link count on src_dp. This is necessary when
  1193. * renaming a directory, either within one parent when
  1194. * the target existed, or across two parent directories.
  1195. */
  1196. if (src_is_directory && (new_parent || target_ip != NULL)) {
  1197. /*
  1198. * Decrement link count on src_directory since the
  1199. * entry that's moved no longer points to it.
  1200. */
  1201. error = xfs_droplink(tp, src_dp);
  1202. if (error)
  1203. return error;
  1204. }
  1205. /*
  1206. * For whiteouts, we only need to update the source dirent with the
  1207. * inode number of the whiteout inode rather than removing it
  1208. * altogether.
  1209. */
  1210. if (du_wip->ip)
  1211. error = xfs_dir_replace(tp, src_dp, src_name, du_wip->ip->i_ino,
  1212. spaceres);
  1213. else
  1214. error = xfs_dir_removename(tp, src_dp, src_name, src_ip->i_ino,
  1215. spaceres);
  1216. if (error)
  1217. return error;
  1218. xfs_trans_ichgtime(tp, src_dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  1219. xfs_trans_log_inode(tp, src_dp, XFS_ILOG_CORE);
  1220. if (new_parent)
  1221. xfs_trans_log_inode(tp, target_dp, XFS_ILOG_CORE);
  1222. /* Schedule parent pointer updates. */
  1223. if (du_wip->ppargs) {
  1224. error = xfs_parent_addname(tp, du_wip->ppargs, src_dp,
  1225. src_name, du_wip->ip);
  1226. if (error)
  1227. return error;
  1228. }
  1229. if (du_src->ppargs) {
  1230. error = xfs_parent_replacename(tp, du_src->ppargs, src_dp,
  1231. src_name, target_dp, target_name, src_ip);
  1232. if (error)
  1233. return error;
  1234. }
  1235. if (du_tgt->ppargs) {
  1236. error = xfs_parent_removename(tp, du_tgt->ppargs, target_dp,
  1237. target_name, target_ip);
  1238. if (error)
  1239. return error;
  1240. }
  1241. /*
  1242. * Inform our hook clients that we've finished a rename operation as
  1243. * follows: removed the source and target files from their directories;
  1244. * that we've added the source to the target directory; and finally
  1245. * that we've added the whiteout, if there was one. All inodes are
  1246. * locked, so it's ok to model a rename this way so long as we say we
  1247. * deleted entries before we add new ones.
  1248. */
  1249. if (target_ip)
  1250. xfs_dir_update_hook(target_dp, target_ip, -1, target_name);
  1251. xfs_dir_update_hook(src_dp, src_ip, -1, src_name);
  1252. xfs_dir_update_hook(target_dp, src_ip, 1, target_name);
  1253. if (du_wip->ip)
  1254. xfs_dir_update_hook(src_dp, du_wip->ip, 1, src_name);
  1255. return 0;
  1256. }