xfs_symlink.c 18 KB

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