xfs_symlink.c 18 KB

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