xfs_symlink.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  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, *gdqp;
  314. uint resblks;
  315. *ipp = NULL;
  316. trace_xfs_symlink(dp, link_name);
  317. if (XFS_FORCED_SHUTDOWN(mp))
  318. return XFS_ERROR(EIO);
  319. /*
  320. * Check component lengths of the target path name.
  321. */
  322. pathlen = strlen(target_path);
  323. if (pathlen >= MAXPATHLEN) /* total string too long */
  324. return XFS_ERROR(ENAMETOOLONG);
  325. udqp = gdqp = NULL;
  326. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  327. prid = xfs_get_projid(dp);
  328. else
  329. prid = XFS_PROJID_DEFAULT;
  330. /*
  331. * Make sure that we have allocated dquot(s) on disk.
  332. */
  333. error = xfs_qm_vop_dqalloc(dp, current_fsuid(), current_fsgid(), prid,
  334. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT, &udqp, &gdqp);
  335. if (error)
  336. goto std_return;
  337. tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
  338. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  339. /*
  340. * The symlink will fit into the inode data fork?
  341. * There can't be any attributes so we get the whole variable part.
  342. */
  343. if (pathlen <= XFS_LITINO(mp, dp->i_d.di_version))
  344. fs_blocks = 0;
  345. else
  346. fs_blocks = xfs_symlink_blocks(mp, pathlen);
  347. resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
  348. error = xfs_trans_reserve(tp, resblks, XFS_SYMLINK_LOG_RES(mp), 0,
  349. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  350. if (error == ENOSPC && fs_blocks == 0) {
  351. resblks = 0;
  352. error = xfs_trans_reserve(tp, 0, XFS_SYMLINK_LOG_RES(mp), 0,
  353. XFS_TRANS_PERM_LOG_RES, XFS_SYMLINK_LOG_COUNT);
  354. }
  355. if (error) {
  356. cancel_flags = 0;
  357. goto error_return;
  358. }
  359. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  360. unlock_dp_on_error = true;
  361. /*
  362. * Check whether the directory allows new symlinks or not.
  363. */
  364. if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
  365. error = XFS_ERROR(EPERM);
  366. goto error_return;
  367. }
  368. /*
  369. * Reserve disk quota : blocks and inode.
  370. */
  371. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp, resblks, 1, 0);
  372. if (error)
  373. goto error_return;
  374. /*
  375. * Check for ability to enter directory entry, if no space reserved.
  376. */
  377. error = xfs_dir_canenter(tp, dp, link_name, resblks);
  378. if (error)
  379. goto error_return;
  380. /*
  381. * Initialize the bmap freelist prior to calling either
  382. * bmapi or the directory create code.
  383. */
  384. xfs_bmap_init(&free_list, &first_block);
  385. /*
  386. * Allocate an inode for the symlink.
  387. */
  388. error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
  389. prid, resblks > 0, &ip, NULL);
  390. if (error) {
  391. if (error == ENOSPC)
  392. goto error_return;
  393. goto error1;
  394. }
  395. /*
  396. * An error after we've joined dp to the transaction will result in the
  397. * transaction cancel unlocking dp so don't do it explicitly in the
  398. * error path.
  399. */
  400. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  401. unlock_dp_on_error = false;
  402. /*
  403. * Also attach the dquot(s) to it, if applicable.
  404. */
  405. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp);
  406. if (resblks)
  407. resblks -= XFS_IALLOC_SPACE_RES(mp);
  408. /*
  409. * If the symlink will fit into the inode, write it inline.
  410. */
  411. if (pathlen <= XFS_IFORK_DSIZE(ip)) {
  412. xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
  413. memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
  414. ip->i_d.di_size = pathlen;
  415. /*
  416. * The inode was initially created in extent format.
  417. */
  418. ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
  419. ip->i_df.if_flags |= XFS_IFINLINE;
  420. ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  421. xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
  422. } else {
  423. int offset;
  424. first_fsb = 0;
  425. nmaps = XFS_SYMLINK_MAPS;
  426. error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
  427. XFS_BMAPI_METADATA, &first_block, resblks,
  428. mval, &nmaps, &free_list);
  429. if (error)
  430. goto error2;
  431. if (resblks)
  432. resblks -= fs_blocks;
  433. ip->i_d.di_size = pathlen;
  434. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  435. cur_chunk = target_path;
  436. offset = 0;
  437. for (n = 0; n < nmaps; n++) {
  438. char *buf;
  439. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  440. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  441. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  442. BTOBB(byte_cnt), 0);
  443. if (!bp) {
  444. error = ENOMEM;
  445. goto error2;
  446. }
  447. bp->b_ops = &xfs_symlink_buf_ops;
  448. byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
  449. byte_cnt = min(byte_cnt, pathlen);
  450. buf = bp->b_addr;
  451. buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset,
  452. byte_cnt, bp);
  453. memcpy(buf, cur_chunk, byte_cnt);
  454. cur_chunk += byte_cnt;
  455. pathlen -= byte_cnt;
  456. offset += byte_cnt;
  457. xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) -
  458. (char *)bp->b_addr);
  459. }
  460. ASSERT(pathlen == 0);
  461. }
  462. /*
  463. * Create the directory entry for the symlink.
  464. */
  465. error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
  466. &first_block, &free_list, resblks);
  467. if (error)
  468. goto error2;
  469. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  470. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  471. /*
  472. * If this is a synchronous mount, make sure that the
  473. * symlink transaction goes to disk before returning to
  474. * the user.
  475. */
  476. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  477. xfs_trans_set_sync(tp);
  478. }
  479. error = xfs_bmap_finish(&tp, &free_list, &committed);
  480. if (error) {
  481. goto error2;
  482. }
  483. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  484. xfs_qm_dqrele(udqp);
  485. xfs_qm_dqrele(gdqp);
  486. *ipp = ip;
  487. return 0;
  488. error2:
  489. IRELE(ip);
  490. error1:
  491. xfs_bmap_cancel(&free_list);
  492. cancel_flags |= XFS_TRANS_ABORT;
  493. error_return:
  494. xfs_trans_cancel(tp, cancel_flags);
  495. xfs_qm_dqrele(udqp);
  496. xfs_qm_dqrele(gdqp);
  497. if (unlock_dp_on_error)
  498. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  499. std_return:
  500. return error;
  501. }
  502. /*
  503. * Free a symlink that has blocks associated with it.
  504. */
  505. STATIC int
  506. xfs_inactive_symlink_rmt(
  507. xfs_inode_t *ip,
  508. xfs_trans_t **tpp)
  509. {
  510. xfs_buf_t *bp;
  511. int committed;
  512. int done;
  513. int error;
  514. xfs_fsblock_t first_block;
  515. xfs_bmap_free_t free_list;
  516. int i;
  517. xfs_mount_t *mp;
  518. xfs_bmbt_irec_t mval[XFS_SYMLINK_MAPS];
  519. int nmaps;
  520. xfs_trans_t *ntp;
  521. int size;
  522. xfs_trans_t *tp;
  523. tp = *tpp;
  524. mp = ip->i_mount;
  525. ASSERT(ip->i_df.if_flags & XFS_IFEXTENTS);
  526. /*
  527. * We're freeing a symlink that has some
  528. * blocks allocated to it. Free the
  529. * blocks here. We know that we've got
  530. * either 1 or 2 extents and that we can
  531. * free them all in one bunmapi call.
  532. */
  533. ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
  534. /*
  535. * Lock the inode, fix the size, and join it to the transaction.
  536. * Hold it so in the normal path, we still have it locked for
  537. * the second transaction. In the error paths we need it
  538. * held so the cancel won't rele it, see below.
  539. */
  540. size = (int)ip->i_d.di_size;
  541. ip->i_d.di_size = 0;
  542. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  543. /*
  544. * Find the block(s) so we can inval and unmap them.
  545. */
  546. done = 0;
  547. xfs_bmap_init(&free_list, &first_block);
  548. nmaps = ARRAY_SIZE(mval);
  549. error = xfs_bmapi_read(ip, 0, xfs_symlink_blocks(mp, size),
  550. mval, &nmaps, 0);
  551. if (error)
  552. goto error0;
  553. /*
  554. * Invalidate the block(s). No validation is done.
  555. */
  556. for (i = 0; i < nmaps; i++) {
  557. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  558. XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
  559. XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
  560. if (!bp) {
  561. error = ENOMEM;
  562. goto error1;
  563. }
  564. xfs_trans_binval(tp, bp);
  565. }
  566. /*
  567. * Unmap the dead block(s) to the free_list.
  568. */
  569. if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
  570. &first_block, &free_list, &done)))
  571. goto error1;
  572. ASSERT(done);
  573. /*
  574. * Commit the first transaction. This logs the EFI and the inode.
  575. */
  576. if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
  577. goto error1;
  578. /*
  579. * The transaction must have been committed, since there were
  580. * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
  581. * The new tp has the extent freeing and EFDs.
  582. */
  583. ASSERT(committed);
  584. /*
  585. * The first xact was committed, so add the inode to the new one.
  586. * Mark it dirty so it will be logged and moved forward in the log as
  587. * part of every commit.
  588. */
  589. xfs_trans_ijoin(tp, ip, 0);
  590. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  591. /*
  592. * Get a new, empty transaction to return to our caller.
  593. */
  594. ntp = xfs_trans_dup(tp);
  595. /*
  596. * Commit the transaction containing extent freeing and EFDs.
  597. * If we get an error on the commit here or on the reserve below,
  598. * we need to unlock the inode since the new transaction doesn't
  599. * have the inode attached.
  600. */
  601. error = xfs_trans_commit(tp, 0);
  602. tp = ntp;
  603. if (error) {
  604. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  605. goto error0;
  606. }
  607. /*
  608. * transaction commit worked ok so we can drop the extra ticket
  609. * reference that we gained in xfs_trans_dup()
  610. */
  611. xfs_log_ticket_put(tp->t_ticket);
  612. /*
  613. * Remove the memory for extent descriptions (just bookkeeping).
  614. */
  615. if (ip->i_df.if_bytes)
  616. xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
  617. ASSERT(ip->i_df.if_bytes == 0);
  618. /*
  619. * Put an itruncate log reservation in the new transaction
  620. * for our caller.
  621. */
  622. if ((error = xfs_trans_reserve(tp, 0, XFS_ITRUNCATE_LOG_RES(mp), 0,
  623. XFS_TRANS_PERM_LOG_RES, XFS_ITRUNCATE_LOG_COUNT))) {
  624. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  625. goto error0;
  626. }
  627. xfs_trans_ijoin(tp, ip, 0);
  628. *tpp = tp;
  629. return 0;
  630. error1:
  631. xfs_bmap_cancel(&free_list);
  632. error0:
  633. return error;
  634. }
  635. /*
  636. * xfs_inactive_symlink - free a symlink
  637. */
  638. int
  639. xfs_inactive_symlink(
  640. struct xfs_inode *ip,
  641. struct xfs_trans **tp)
  642. {
  643. struct xfs_mount *mp = ip->i_mount;
  644. int pathlen;
  645. trace_xfs_inactive_symlink(ip);
  646. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  647. if (XFS_FORCED_SHUTDOWN(mp))
  648. return XFS_ERROR(EIO);
  649. /*
  650. * Zero length symlinks _can_ exist.
  651. */
  652. pathlen = (int)ip->i_d.di_size;
  653. if (!pathlen)
  654. return 0;
  655. if (pathlen < 0 || pathlen > MAXPATHLEN) {
  656. xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)",
  657. __func__, (unsigned long long)ip->i_ino, pathlen);
  658. ASSERT(0);
  659. return XFS_ERROR(EFSCORRUPTED);
  660. }
  661. if (ip->i_df.if_flags & XFS_IFINLINE) {
  662. if (ip->i_df.if_bytes > 0)
  663. xfs_idata_realloc(ip, -(ip->i_df.if_bytes),
  664. XFS_DATA_FORK);
  665. ASSERT(ip->i_df.if_bytes == 0);
  666. return 0;
  667. }
  668. /* remove the remote symlink */
  669. return xfs_inactive_symlink_rmt(ip, tp);
  670. }