xfs_symlink.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. * Copyright (c) 2000-2006 Silicon Graphics, Inc.
  3. * Copyright (c) 2012-2013 Red Hat, Inc.
  4. * All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it would be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #include "xfs.h"
  20. #include "xfs_fs.h"
  21. #include "xfs_types.h"
  22. #include "xfs_bit.h"
  23. #include "xfs_log.h"
  24. #include "xfs_trans.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_dir2.h"
  28. #include "xfs_mount.h"
  29. #include "xfs_da_btree.h"
  30. #include "xfs_bmap_btree.h"
  31. #include "xfs_ialloc_btree.h"
  32. #include "xfs_dinode.h"
  33. #include "xfs_inode.h"
  34. #include "xfs_inode_item.h"
  35. #include "xfs_itable.h"
  36. #include "xfs_ialloc.h"
  37. #include "xfs_alloc.h"
  38. #include "xfs_bmap.h"
  39. #include "xfs_error.h"
  40. #include "xfs_quota.h"
  41. #include "xfs_utils.h"
  42. #include "xfs_trans_space.h"
  43. #include "xfs_log_priv.h"
  44. #include "xfs_trace.h"
  45. #include "xfs_symlink.h"
  46. #include "xfs_cksum.h"
  47. #include "xfs_buf_item.h"
  48. /*
  49. * Each contiguous block has a header, so it is not just a simple pathlen
  50. * to FSB conversion.
  51. */
  52. int
  53. xfs_symlink_blocks(
  54. struct xfs_mount *mp,
  55. int pathlen)
  56. {
  57. int fsblocks = 0;
  58. int len = pathlen;
  59. do {
  60. fsblocks++;
  61. len -= XFS_SYMLINK_BUF_SPACE(mp, mp->m_sb.sb_blocksize);
  62. } while (len > 0);
  63. ASSERT(fsblocks <= XFS_SYMLINK_MAPS);
  64. return fsblocks;
  65. }
  66. static int
  67. xfs_symlink_hdr_set(
  68. struct xfs_mount *mp,
  69. xfs_ino_t ino,
  70. uint32_t offset,
  71. uint32_t size,
  72. struct xfs_buf *bp)
  73. {
  74. struct xfs_dsymlink_hdr *dsl = bp->b_addr;
  75. if (!xfs_sb_version_hascrc(&mp->m_sb))
  76. return 0;
  77. dsl->sl_magic = cpu_to_be32(XFS_SYMLINK_MAGIC);
  78. dsl->sl_offset = cpu_to_be32(offset);
  79. dsl->sl_bytes = cpu_to_be32(size);
  80. uuid_copy(&dsl->sl_uuid, &mp->m_sb.sb_uuid);
  81. dsl->sl_owner = cpu_to_be64(ino);
  82. dsl->sl_blkno = cpu_to_be64(bp->b_bn);
  83. bp->b_ops = &xfs_symlink_buf_ops;
  84. return sizeof(struct xfs_dsymlink_hdr);
  85. }
  86. /*
  87. * Checking of the symlink header is split into two parts. the verifier does
  88. * CRC, location and bounds checking, the unpacking function checks the path
  89. * parameters and owner.
  90. */
  91. bool
  92. xfs_symlink_hdr_ok(
  93. struct xfs_mount *mp,
  94. xfs_ino_t ino,
  95. uint32_t offset,
  96. uint32_t size,
  97. struct xfs_buf *bp)
  98. {
  99. struct xfs_dsymlink_hdr *dsl = bp->b_addr;
  100. if (offset != be32_to_cpu(dsl->sl_offset))
  101. return false;
  102. if (size != be32_to_cpu(dsl->sl_bytes))
  103. return false;
  104. if (ino != be64_to_cpu(dsl->sl_owner))
  105. return false;
  106. /* ok */
  107. return true;
  108. }
  109. static bool
  110. xfs_symlink_verify(
  111. struct xfs_buf *bp)
  112. {
  113. struct xfs_mount *mp = bp->b_target->bt_mount;
  114. struct xfs_dsymlink_hdr *dsl = bp->b_addr;
  115. if (!xfs_sb_version_hascrc(&mp->m_sb))
  116. return false;
  117. if (dsl->sl_magic != cpu_to_be32(XFS_SYMLINK_MAGIC))
  118. return false;
  119. if (!uuid_equal(&dsl->sl_uuid, &mp->m_sb.sb_uuid))
  120. return false;
  121. if (bp->b_bn != be64_to_cpu(dsl->sl_blkno))
  122. return false;
  123. if (be32_to_cpu(dsl->sl_offset) +
  124. be32_to_cpu(dsl->sl_bytes) >= MAXPATHLEN)
  125. return false;
  126. if (dsl->sl_owner == 0)
  127. return false;
  128. return true;
  129. }
  130. static void
  131. xfs_symlink_read_verify(
  132. struct xfs_buf *bp)
  133. {
  134. struct xfs_mount *mp = bp->b_target->bt_mount;
  135. /* no verification of non-crc buffers */
  136. if (!xfs_sb_version_hascrc(&mp->m_sb))
  137. return;
  138. if (!xfs_verify_cksum(bp->b_addr, BBTOB(bp->b_length),
  139. offsetof(struct xfs_dsymlink_hdr, sl_crc)) ||
  140. !xfs_symlink_verify(bp)) {
  141. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  142. xfs_buf_ioerror(bp, EFSCORRUPTED);
  143. }
  144. }
  145. static void
  146. xfs_symlink_write_verify(
  147. struct xfs_buf *bp)
  148. {
  149. struct xfs_mount *mp = bp->b_target->bt_mount;
  150. struct xfs_buf_log_item *bip = bp->b_fspriv;
  151. /* no verification of non-crc buffers */
  152. if (!xfs_sb_version_hascrc(&mp->m_sb))
  153. return;
  154. if (!xfs_symlink_verify(bp)) {
  155. XFS_CORRUPTION_ERROR(__func__, XFS_ERRLEVEL_LOW, mp, bp->b_addr);
  156. xfs_buf_ioerror(bp, EFSCORRUPTED);
  157. return;
  158. }
  159. if (bip) {
  160. struct xfs_dsymlink_hdr *dsl = bp->b_addr;
  161. dsl->sl_lsn = cpu_to_be64(bip->bli_item.li_lsn);
  162. }
  163. xfs_update_cksum(bp->b_addr, BBTOB(bp->b_length),
  164. offsetof(struct xfs_dsymlink_hdr, sl_crc));
  165. }
  166. const struct xfs_buf_ops xfs_symlink_buf_ops = {
  167. .verify_read = xfs_symlink_read_verify,
  168. .verify_write = xfs_symlink_write_verify,
  169. };
  170. void
  171. xfs_symlink_local_to_remote(
  172. struct xfs_trans *tp,
  173. struct xfs_buf *bp,
  174. struct xfs_inode *ip,
  175. struct xfs_ifork *ifp)
  176. {
  177. struct xfs_mount *mp = ip->i_mount;
  178. char *buf;
  179. if (!xfs_sb_version_hascrc(&mp->m_sb)) {
  180. bp->b_ops = NULL;
  181. memcpy(bp->b_addr, ifp->if_u1.if_data, ifp->if_bytes);
  182. return;
  183. }
  184. /*
  185. * As this symlink fits in an inode literal area, it must also fit in
  186. * the smallest buffer the filesystem supports.
  187. */
  188. ASSERT(BBTOB(bp->b_length) >=
  189. ifp->if_bytes + sizeof(struct xfs_dsymlink_hdr));
  190. bp->b_ops = &xfs_symlink_buf_ops;
  191. buf = bp->b_addr;
  192. buf += xfs_symlink_hdr_set(mp, ip->i_ino, 0, ifp->if_bytes, bp);
  193. memcpy(buf, ifp->if_u1.if_data, ifp->if_bytes);
  194. }
  195. /* ----- Kernel only functions below ----- */
  196. STATIC int
  197. xfs_readlink_bmap(
  198. struct xfs_inode *ip,
  199. char *link)
  200. {
  201. struct xfs_mount *mp = ip->i_mount;
  202. struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
  203. struct xfs_buf *bp;
  204. xfs_daddr_t d;
  205. char *cur_chunk;
  206. int pathlen = ip->i_d.di_size;
  207. int nmaps = XFS_SYMLINK_MAPS;
  208. int byte_cnt;
  209. int n;
  210. int error = 0;
  211. int fsblocks = 0;
  212. int offset;
  213. fsblocks = xfs_symlink_blocks(mp, pathlen);
  214. error = xfs_bmapi_read(ip, 0, fsblocks, mval, &nmaps, 0);
  215. if (error)
  216. goto out;
  217. offset = 0;
  218. for (n = 0; n < nmaps; n++) {
  219. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  220. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  221. bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0,
  222. &xfs_symlink_buf_ops);
  223. if (!bp)
  224. return XFS_ERROR(ENOMEM);
  225. error = bp->b_error;
  226. if (error) {
  227. xfs_buf_ioerror_alert(bp, __func__);
  228. xfs_buf_relse(bp);
  229. goto out;
  230. }
  231. byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
  232. if (pathlen < byte_cnt)
  233. byte_cnt = pathlen;
  234. cur_chunk = bp->b_addr;
  235. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  236. if (!xfs_symlink_hdr_ok(mp, ip->i_ino, offset,
  237. byte_cnt, bp)) {
  238. error = EFSCORRUPTED;
  239. xfs_alert(mp,
  240. "symlink header does not match required off/len/owner (0x%x/Ox%x,0x%llx)",
  241. offset, byte_cnt, ip->i_ino);
  242. xfs_buf_relse(bp);
  243. goto out;
  244. }
  245. cur_chunk += sizeof(struct xfs_dsymlink_hdr);
  246. }
  247. memcpy(link + offset, bp->b_addr, byte_cnt);
  248. pathlen -= byte_cnt;
  249. offset += byte_cnt;
  250. xfs_buf_relse(bp);
  251. }
  252. ASSERT(pathlen == 0);
  253. link[ip->i_d.di_size] = '\0';
  254. error = 0;
  255. out:
  256. return error;
  257. }
  258. int
  259. xfs_readlink(
  260. struct xfs_inode *ip,
  261. char *link)
  262. {
  263. struct xfs_mount *mp = ip->i_mount;
  264. xfs_fsize_t pathlen;
  265. int error = 0;
  266. trace_xfs_readlink(ip);
  267. if (XFS_FORCED_SHUTDOWN(mp))
  268. return XFS_ERROR(EIO);
  269. xfs_ilock(ip, XFS_ILOCK_SHARED);
  270. pathlen = ip->i_d.di_size;
  271. if (!pathlen)
  272. goto out;
  273. if (pathlen < 0 || pathlen > MAXPATHLEN) {
  274. xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
  275. __func__, (unsigned long long) ip->i_ino,
  276. (long long) pathlen);
  277. ASSERT(0);
  278. error = XFS_ERROR(EFSCORRUPTED);
  279. goto out;
  280. }
  281. if (ip->i_df.if_flags & XFS_IFINLINE) {
  282. memcpy(link, ip->i_df.if_u1.if_data, pathlen);
  283. link[pathlen] = '\0';
  284. } else {
  285. error = xfs_readlink_bmap(ip, link);
  286. }
  287. out:
  288. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  289. return error;
  290. }
  291. int
  292. xfs_symlink(
  293. struct xfs_inode *dp,
  294. struct xfs_name *link_name,
  295. const char *target_path,
  296. umode_t mode,
  297. struct xfs_inode **ipp)
  298. {
  299. struct xfs_mount *mp = dp->i_mount;
  300. struct xfs_trans *tp = NULL;
  301. struct xfs_inode *ip = NULL;
  302. int error = 0;
  303. int pathlen;
  304. struct xfs_bmap_free free_list;
  305. xfs_fsblock_t first_block;
  306. bool unlock_dp_on_error = false;
  307. uint cancel_flags;
  308. int committed;
  309. xfs_fileoff_t first_fsb;
  310. xfs_filblks_t fs_blocks;
  311. int nmaps;
  312. struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
  313. xfs_daddr_t d;
  314. const char *cur_chunk;
  315. int byte_cnt;
  316. int n;
  317. xfs_buf_t *bp;
  318. prid_t prid;
  319. struct xfs_dquot *udqp, *gdqp;
  320. uint resblks;
  321. *ipp = NULL;
  322. trace_xfs_symlink(dp, link_name);
  323. if (XFS_FORCED_SHUTDOWN(mp))
  324. return XFS_ERROR(EIO);
  325. /*
  326. * Check component lengths of the target path name.
  327. */
  328. pathlen = strlen(target_path);
  329. if (pathlen >= MAXPATHLEN) /* total string too long */
  330. return XFS_ERROR(ENAMETOOLONG);
  331. udqp = gdqp = NULL;
  332. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  333. prid = xfs_get_projid(dp);
  334. else
  335. prid = XFS_PROJID_DEFAULT;
  336. /*
  337. * Make sure that we have allocated dquot(s) on disk.
  338. */
  339. error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
  340. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  341. if (error)
  342. goto std_return;
  343. tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
  344. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  345. /*
  346. * The symlink will fit into the inode data fork?
  347. * There can't be any attributes so we get the whole variable part.
  348. */
  349. if (pathlen <= XFS_LITINO(mp, dp->i_d.di_version))
  350. fs_blocks = 0;
  351. else
  352. fs_blocks = XFS_B_TO_FSB(mp, pathlen);
  353. resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
  354. error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
  355. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  356. if (error == ENOSPC && fs_blocks == 0) {
  357. resblks = 0;
  358. error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
  359. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  360. }
  361. if (error) {
  362. cancel_flags = 0;
  363. goto error_return;
  364. }
  365. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  366. unlock_dp_on_error = true;
  367. /*
  368. * Check whether the directory allows new symlinks or not.
  369. */
  370. if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
  371. error = XFS_ERROR(EPERM);
  372. goto error_return;
  373. }
  374. /*
  375. * Reserve disk quota : blocks and inode.
  376. */
  377. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
  378. if (error)
  379. goto error_return;
  380. /*
  381. * Check for ability to enter directory entry, if no space reserved.
  382. */
  383. error = xfs_dir_canenter(tp, dp, link_name, resblks);
  384. if (error)
  385. goto error_return;
  386. /*
  387. * Initialize the bmap freelist prior to calling either
  388. * bmapi or the directory create code.
  389. */
  390. xfs_bmap_init(&free_list, &first_block);
  391. /*
  392. * Allocate an inode for the symlink.
  393. */
  394. error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
  395. prid, resblks > 0, &ip, NULL);
  396. if (error) {
  397. if (error == ENOSPC)
  398. goto error_return;
  399. goto error1;
  400. }
  401. /*
  402. * An error after we've joined dp to the transaction will result in the
  403. * transaction cancel unlocking dp so don't do it explicitly in the
  404. * error path.
  405. */
  406. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  407. unlock_dp_on_error = false;
  408. /*
  409. * Also attach the dquot(s) to it, if applicable.
  410. */
  411. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
  412. if (resblks)
  413. resblks -= XFS_IALLOC_SPACE_RES(mp);
  414. /*
  415. * If the symlink will fit into the inode, write it inline.
  416. */
  417. if (pathlen <= XFS_IFORK_DSIZE(ip)) {
  418. xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
  419. memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
  420. ip->i_d.di_size = pathlen;
  421. /*
  422. * The inode was initially created in extent format.
  423. */
  424. ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
  425. ip->i_df.if_flags |= XFS_IFINLINE;
  426. ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  427. xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
  428. } else {
  429. int offset;
  430. first_fsb = 0;
  431. nmaps = XFS_SYMLINK_MAPS;
  432. error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
  433. XFS_BMAPI_METADATA, &first_block, resblks,
  434. mval, &nmaps, &free_list);
  435. if (error)
  436. goto error2;
  437. if (resblks)
  438. resblks -= fs_blocks;
  439. ip->i_d.di_size = pathlen;
  440. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  441. cur_chunk = target_path;
  442. offset = 0;
  443. for (n = 0; n < nmaps; n++) {
  444. char *buf;
  445. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  446. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  447. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  448. BTOBB(byte_cnt), 0);
  449. if (!bp) {
  450. error = ENOMEM;
  451. goto error2;
  452. }
  453. bp->b_ops = &xfs_symlink_buf_ops;
  454. byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
  455. if (pathlen < byte_cnt) {
  456. byte_cnt = pathlen;
  457. }
  458. buf = bp->b_addr;
  459. buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset,
  460. byte_cnt, bp);
  461. memcpy(buf, cur_chunk, byte_cnt);
  462. cur_chunk += byte_cnt;
  463. pathlen -= byte_cnt;
  464. offset += byte_cnt;
  465. xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) -
  466. (char *)bp->b_addr);
  467. }
  468. }
  469. /*
  470. * Create the directory entry for the symlink.
  471. */
  472. error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
  473. &first_block, &free_list, resblks);
  474. if (error)
  475. goto error2;
  476. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  477. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  478. /*
  479. * If this is a synchronous mount, make sure that the
  480. * symlink transaction goes to disk before returning to
  481. * the user.
  482. */
  483. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  484. xfs_trans_set_sync(tp);
  485. }
  486. error = xfs_bmap_finish(&tp, &free_list, &committed);
  487. if (error) {
  488. goto error2;
  489. }
  490. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  491. xfs_qm_dqrele(udqp);
  492. xfs_qm_dqrele(gdqp);
  493. *ipp = ip;
  494. return 0;
  495. error2:
  496. IRELE(ip);
  497. error1:
  498. xfs_bmap_cancel(&free_list);
  499. cancel_flags |= XFS_TRANS_ABORT;
  500. error_return:
  501. xfs_trans_cancel(tp, cancel_flags);
  502. xfs_qm_dqrele(udqp);
  503. xfs_qm_dqrele(gdqp);
  504. if (unlock_dp_on_error)
  505. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  506. std_return:
  507. return error;
  508. }
  509. /*
  510. * Free a symlink that has blocks associated with it.
  511. */
  512. int
  513. xfs_inactive_symlink_rmt(
  514. xfs_inode_t *ip,
  515. xfs_trans_t **tpp)
  516. {
  517. xfs_buf_t *bp;
  518. int committed;
  519. int done;
  520. int error;
  521. xfs_fsblock_t first_block;
  522. xfs_bmap_free_t free_list;
  523. int i;
  524. xfs_mount_t *mp;
  525. xfs_bmbt_irec_t mval[XFS_SYMLINK_MAPS];
  526. int nmaps;
  527. xfs_trans_t *ntp;
  528. int size;
  529. xfs_trans_t *tp;
  530. tp = *tpp;
  531. mp = ip->i_mount;
  532. ASSERT(ip->i_d.di_size > XFS_IFORK_DSIZE(ip));
  533. /*
  534. * We're freeing a symlink that has some
  535. * blocks allocated to it. Free the
  536. * blocks here. We know that we've got
  537. * either 1 or 2 extents and that we can
  538. * free them all in one bunmapi call.
  539. */
  540. ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
  541. /*
  542. * Lock the inode, fix the size, and join it to the transaction.
  543. * Hold it so in the normal path, we still have it locked for
  544. * the second transaction. In the error paths we need it
  545. * held so the cancel won't rele it, see below.
  546. */
  547. size = (int)ip->i_d.di_size;
  548. ip->i_d.di_size = 0;
  549. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  550. /*
  551. * Find the block(s) so we can inval and unmap them.
  552. */
  553. done = 0;
  554. xfs_bmap_init(&free_list, &first_block);
  555. nmaps = ARRAY_SIZE(mval);
  556. error = xfs_bmapi_read(ip, 0, xfs_symlink_blocks(mp, size),
  557. mval, &nmaps, 0);
  558. if (error)
  559. goto error0;
  560. /*
  561. * Invalidate the block(s). No validation is done.
  562. */
  563. for (i = 0; i < nmaps; i++) {
  564. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  565. XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
  566. XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
  567. if (!bp) {
  568. error = ENOMEM;
  569. goto error1;
  570. }
  571. xfs_trans_binval(tp, bp);
  572. }
  573. /*
  574. * Unmap the dead block(s) to the free_list.
  575. */
  576. if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
  577. &first_block, &free_list, &done)))
  578. goto error1;
  579. ASSERT(done);
  580. /*
  581. * Commit the first transaction. This logs the EFI and the inode.
  582. */
  583. if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
  584. goto error1;
  585. /*
  586. * The transaction must have been committed, since there were
  587. * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
  588. * The new tp has the extent freeing and EFDs.
  589. */
  590. ASSERT(committed);
  591. /*
  592. * The first xact was committed, so add the inode to the new one.
  593. * Mark it dirty so it will be logged and moved forward in the log as
  594. * part of every commit.
  595. */
  596. xfs_trans_ijoin(tp, ip, 0);
  597. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  598. /*
  599. * Get a new, empty transaction to return to our caller.
  600. */
  601. ntp = xfs_trans_dup(tp);
  602. /*
  603. * Commit the transaction containing extent freeing and EFDs.
  604. * If we get an error on the commit here or on the reserve below,
  605. * we need to unlock the inode since the new transaction doesn't
  606. * have the inode attached.
  607. */
  608. error = xfs_trans_commit(tp, 0);
  609. tp = ntp;
  610. if (error) {
  611. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  612. goto error0;
  613. }
  614. /*
  615. * transaction commit worked ok so we can drop the extra ticket
  616. * reference that we gained in xfs_trans_dup()
  617. */
  618. xfs_log_ticket_put(tp->t_ticket);
  619. /*
  620. * Remove the memory for extent descriptions (just bookkeeping).
  621. */
  622. if (ip->i_df.if_bytes)
  623. xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
  624. ASSERT(ip->i_df.if_bytes == 0);
  625. /*
  626. * Put an itruncate log reservation in the new transaction
  627. * for our caller.
  628. */
  629. if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  630. XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
  631. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  632. goto error0;
  633. }
  634. xfs_trans_ijoin(tp, ip, 0);
  635. *tpp = tp;
  636. return 0;
  637. error1:
  638. xfs_bmap_cancel(&free_list);
  639. error0:
  640. return error;
  641. }