xfs_symlink.c 18 KB

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