xfs_symlink.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  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_ialloc.h"
  36. #include "xfs_alloc.h"
  37. #include "xfs_bmap.h"
  38. #include "xfs_bmap_util.h"
  39. #include "xfs_error.h"
  40. #include "xfs_quota.h"
  41. #include "xfs_trans_space.h"
  42. #include "xfs_trace.h"
  43. #include "xfs_symlink.h"
  44. #include "xfs_buf_item.h"
  45. /* ----- Kernel only functions below ----- */
  46. STATIC int
  47. xfs_readlink_bmap(
  48. struct xfs_inode *ip,
  49. char *link)
  50. {
  51. struct xfs_mount *mp = ip->i_mount;
  52. struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
  53. struct xfs_buf *bp;
  54. xfs_daddr_t d;
  55. char *cur_chunk;
  56. int pathlen = ip->i_d.di_size;
  57. int nmaps = XFS_SYMLINK_MAPS;
  58. int byte_cnt;
  59. int n;
  60. int error = 0;
  61. int fsblocks = 0;
  62. int offset;
  63. fsblocks = xfs_symlink_blocks(mp, pathlen);
  64. error = xfs_bmapi_read(ip, 0, fsblocks, mval, &nmaps, 0);
  65. if (error)
  66. goto out;
  67. offset = 0;
  68. for (n = 0; n < nmaps; n++) {
  69. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  70. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  71. bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0,
  72. &xfs_symlink_buf_ops);
  73. if (!bp)
  74. return XFS_ERROR(ENOMEM);
  75. error = bp->b_error;
  76. if (error) {
  77. xfs_buf_ioerror_alert(bp, __func__);
  78. xfs_buf_relse(bp);
  79. goto out;
  80. }
  81. byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
  82. if (pathlen < byte_cnt)
  83. byte_cnt = pathlen;
  84. cur_chunk = bp->b_addr;
  85. if (xfs_sb_version_hascrc(&mp->m_sb)) {
  86. if (!xfs_symlink_hdr_ok(mp, ip->i_ino, offset,
  87. byte_cnt, bp)) {
  88. error = EFSCORRUPTED;
  89. xfs_alert(mp,
  90. "symlink header does not match required off/len/owner (0x%x/Ox%x,0x%llx)",
  91. offset, byte_cnt, ip->i_ino);
  92. xfs_buf_relse(bp);
  93. goto out;
  94. }
  95. cur_chunk += sizeof(struct xfs_dsymlink_hdr);
  96. }
  97. memcpy(link + offset, bp->b_addr, byte_cnt);
  98. pathlen -= byte_cnt;
  99. offset += byte_cnt;
  100. xfs_buf_relse(bp);
  101. }
  102. ASSERT(pathlen == 0);
  103. link[ip->i_d.di_size] = '\0';
  104. error = 0;
  105. out:
  106. return error;
  107. }
  108. int
  109. xfs_readlink(
  110. struct xfs_inode *ip,
  111. char *link)
  112. {
  113. struct xfs_mount *mp = ip->i_mount;
  114. xfs_fsize_t pathlen;
  115. int error = 0;
  116. trace_xfs_readlink(ip);
  117. if (XFS_FORCED_SHUTDOWN(mp))
  118. return XFS_ERROR(EIO);
  119. xfs_ilock(ip, XFS_ILOCK_SHARED);
  120. pathlen = ip->i_d.di_size;
  121. if (!pathlen)
  122. goto out;
  123. if (pathlen < 0 || pathlen > MAXPATHLEN) {
  124. xfs_alert(mp, "%s: inode (%llu) bad symlink length (%lld)",
  125. __func__, (unsigned long long) ip->i_ino,
  126. (long long) pathlen);
  127. ASSERT(0);
  128. error = XFS_ERROR(EFSCORRUPTED);
  129. goto out;
  130. }
  131. if (ip->i_df.if_flags & XFS_IFINLINE) {
  132. memcpy(link, ip->i_df.if_u1.if_data, pathlen);
  133. link[pathlen] = '\0';
  134. } else {
  135. error = xfs_readlink_bmap(ip, link);
  136. }
  137. out:
  138. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  139. return error;
  140. }
  141. int
  142. xfs_symlink(
  143. struct xfs_inode *dp,
  144. struct xfs_name *link_name,
  145. const char *target_path,
  146. umode_t mode,
  147. struct xfs_inode **ipp)
  148. {
  149. struct xfs_mount *mp = dp->i_mount;
  150. struct xfs_trans *tp = NULL;
  151. struct xfs_inode *ip = NULL;
  152. int error = 0;
  153. int pathlen;
  154. struct xfs_bmap_free free_list;
  155. xfs_fsblock_t first_block;
  156. bool unlock_dp_on_error = false;
  157. uint cancel_flags;
  158. int committed;
  159. xfs_fileoff_t first_fsb;
  160. xfs_filblks_t fs_blocks;
  161. int nmaps;
  162. struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
  163. xfs_daddr_t d;
  164. const char *cur_chunk;
  165. int byte_cnt;
  166. int n;
  167. xfs_buf_t *bp;
  168. prid_t prid;
  169. struct xfs_dquot *udqp = NULL;
  170. struct xfs_dquot *gdqp = NULL;
  171. struct xfs_dquot *pdqp = NULL;
  172. uint resblks;
  173. *ipp = NULL;
  174. trace_xfs_symlink(dp, link_name);
  175. if (XFS_FORCED_SHUTDOWN(mp))
  176. return XFS_ERROR(EIO);
  177. /*
  178. * Check component lengths of the target path name.
  179. */
  180. pathlen = strlen(target_path);
  181. if (pathlen >= MAXPATHLEN) /* total string too long */
  182. return XFS_ERROR(ENAMETOOLONG);
  183. udqp = gdqp = NULL;
  184. if (dp->i_d.di_flags & XFS_DIFLAG_PROJINHERIT)
  185. prid = xfs_get_projid(dp);
  186. else
  187. prid = XFS_PROJID_DEFAULT;
  188. /*
  189. * Make sure that we have allocated dquot(s) on disk.
  190. */
  191. error = xfs_qm_vop_dqalloc(dp,
  192. xfs_kuid_to_uid(current_fsuid()),
  193. xfs_kgid_to_gid(current_fsgid()), prid,
  194. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
  195. &udqp, &gdqp, &pdqp);
  196. if (error)
  197. goto std_return;
  198. tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
  199. cancel_flags = XFS_TRANS_RELEASE_LOG_RES;
  200. /*
  201. * The symlink will fit into the inode data fork?
  202. * There can't be any attributes so we get the whole variable part.
  203. */
  204. if (pathlen <= XFS_LITINO(mp, dp->i_d.di_version))
  205. fs_blocks = 0;
  206. else
  207. fs_blocks = xfs_symlink_blocks(mp, pathlen);
  208. resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
  209. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_symlink, resblks, 0);
  210. if (error == ENOSPC && fs_blocks == 0) {
  211. resblks = 0;
  212. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_symlink, 0, 0);
  213. }
  214. if (error) {
  215. cancel_flags = 0;
  216. goto error_return;
  217. }
  218. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  219. unlock_dp_on_error = true;
  220. /*
  221. * Check whether the directory allows new symlinks or not.
  222. */
  223. if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
  224. error = XFS_ERROR(EPERM);
  225. goto error_return;
  226. }
  227. /*
  228. * Reserve disk quota : blocks and inode.
  229. */
  230. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
  231. pdqp, resblks, 1, 0);
  232. if (error)
  233. goto error_return;
  234. /*
  235. * Check for ability to enter directory entry, if no space reserved.
  236. */
  237. error = xfs_dir_canenter(tp, dp, link_name, resblks);
  238. if (error)
  239. goto error_return;
  240. /*
  241. * Initialize the bmap freelist prior to calling either
  242. * bmapi or the directory create code.
  243. */
  244. xfs_bmap_init(&free_list, &first_block);
  245. /*
  246. * Allocate an inode for the symlink.
  247. */
  248. error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
  249. prid, resblks > 0, &ip, NULL);
  250. if (error) {
  251. if (error == ENOSPC)
  252. goto error_return;
  253. goto error1;
  254. }
  255. /*
  256. * An error after we've joined dp to the transaction will result in the
  257. * transaction cancel unlocking dp so don't do it explicitly in the
  258. * error path.
  259. */
  260. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  261. unlock_dp_on_error = false;
  262. /*
  263. * Also attach the dquot(s) to it, if applicable.
  264. */
  265. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
  266. if (resblks)
  267. resblks -= XFS_IALLOC_SPACE_RES(mp);
  268. /*
  269. * If the symlink will fit into the inode, write it inline.
  270. */
  271. if (pathlen <= XFS_IFORK_DSIZE(ip)) {
  272. xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
  273. memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
  274. ip->i_d.di_size = pathlen;
  275. /*
  276. * The inode was initially created in extent format.
  277. */
  278. ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
  279. ip->i_df.if_flags |= XFS_IFINLINE;
  280. ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  281. xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
  282. } else {
  283. int offset;
  284. first_fsb = 0;
  285. nmaps = XFS_SYMLINK_MAPS;
  286. error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
  287. XFS_BMAPI_METADATA, &first_block, resblks,
  288. mval, &nmaps, &free_list);
  289. if (error)
  290. goto error2;
  291. if (resblks)
  292. resblks -= fs_blocks;
  293. ip->i_d.di_size = pathlen;
  294. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  295. cur_chunk = target_path;
  296. offset = 0;
  297. for (n = 0; n < nmaps; n++) {
  298. char *buf;
  299. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  300. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  301. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  302. BTOBB(byte_cnt), 0);
  303. if (!bp) {
  304. error = ENOMEM;
  305. goto error2;
  306. }
  307. bp->b_ops = &xfs_symlink_buf_ops;
  308. byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
  309. byte_cnt = min(byte_cnt, pathlen);
  310. buf = bp->b_addr;
  311. buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset,
  312. byte_cnt, bp);
  313. memcpy(buf, cur_chunk, byte_cnt);
  314. cur_chunk += byte_cnt;
  315. pathlen -= byte_cnt;
  316. offset += byte_cnt;
  317. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
  318. xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) -
  319. (char *)bp->b_addr);
  320. }
  321. ASSERT(pathlen == 0);
  322. }
  323. /*
  324. * Create the directory entry for the symlink.
  325. */
  326. error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
  327. &first_block, &free_list, resblks);
  328. if (error)
  329. goto error2;
  330. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  331. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  332. /*
  333. * If this is a synchronous mount, make sure that the
  334. * symlink transaction goes to disk before returning to
  335. * the user.
  336. */
  337. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  338. xfs_trans_set_sync(tp);
  339. }
  340. error = xfs_bmap_finish(&tp, &free_list, &committed);
  341. if (error) {
  342. goto error2;
  343. }
  344. error = xfs_trans_commit(tp, XFS_TRANS_RELEASE_LOG_RES);
  345. xfs_qm_dqrele(udqp);
  346. xfs_qm_dqrele(gdqp);
  347. xfs_qm_dqrele(pdqp);
  348. *ipp = ip;
  349. return 0;
  350. error2:
  351. IRELE(ip);
  352. error1:
  353. xfs_bmap_cancel(&free_list);
  354. cancel_flags |= XFS_TRANS_ABORT;
  355. error_return:
  356. xfs_trans_cancel(tp, cancel_flags);
  357. xfs_qm_dqrele(udqp);
  358. xfs_qm_dqrele(gdqp);
  359. xfs_qm_dqrele(pdqp);
  360. if (unlock_dp_on_error)
  361. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  362. std_return:
  363. return error;
  364. }
  365. /*
  366. * Free a symlink that has blocks associated with it.
  367. */
  368. STATIC int
  369. xfs_inactive_symlink_rmt(
  370. xfs_inode_t *ip,
  371. xfs_trans_t **tpp)
  372. {
  373. xfs_buf_t *bp;
  374. int committed;
  375. int done;
  376. int error;
  377. xfs_fsblock_t first_block;
  378. xfs_bmap_free_t free_list;
  379. int i;
  380. xfs_mount_t *mp;
  381. xfs_bmbt_irec_t mval[XFS_SYMLINK_MAPS];
  382. int nmaps;
  383. xfs_trans_t *ntp;
  384. int size;
  385. xfs_trans_t *tp;
  386. tp = *tpp;
  387. mp = ip->i_mount;
  388. ASSERT(ip->i_df.if_flags & XFS_IFEXTENTS);
  389. /*
  390. * We're freeing a symlink that has some
  391. * blocks allocated to it. Free the
  392. * blocks here. We know that we've got
  393. * either 1 or 2 extents and that we can
  394. * free them all in one bunmapi call.
  395. */
  396. ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
  397. /*
  398. * Lock the inode, fix the size, and join it to the transaction.
  399. * Hold it so in the normal path, we still have it locked for
  400. * the second transaction. In the error paths we need it
  401. * held so the cancel won't rele it, see below.
  402. */
  403. size = (int)ip->i_d.di_size;
  404. ip->i_d.di_size = 0;
  405. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  406. /*
  407. * Find the block(s) so we can inval and unmap them.
  408. */
  409. done = 0;
  410. xfs_bmap_init(&free_list, &first_block);
  411. nmaps = ARRAY_SIZE(mval);
  412. error = xfs_bmapi_read(ip, 0, xfs_symlink_blocks(mp, size),
  413. mval, &nmaps, 0);
  414. if (error)
  415. goto error0;
  416. /*
  417. * Invalidate the block(s). No validation is done.
  418. */
  419. for (i = 0; i < nmaps; i++) {
  420. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  421. XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
  422. XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
  423. if (!bp) {
  424. error = ENOMEM;
  425. goto error1;
  426. }
  427. xfs_trans_binval(tp, bp);
  428. }
  429. /*
  430. * Unmap the dead block(s) to the free_list.
  431. */
  432. if ((error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
  433. &first_block, &free_list, &done)))
  434. goto error1;
  435. ASSERT(done);
  436. /*
  437. * Commit the first transaction. This logs the EFI and the inode.
  438. */
  439. if ((error = xfs_bmap_finish(&tp, &free_list, &committed)))
  440. goto error1;
  441. /*
  442. * The transaction must have been committed, since there were
  443. * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
  444. * The new tp has the extent freeing and EFDs.
  445. */
  446. ASSERT(committed);
  447. /*
  448. * The first xact was committed, so add the inode to the new one.
  449. * Mark it dirty so it will be logged and moved forward in the log as
  450. * part of every commit.
  451. */
  452. xfs_trans_ijoin(tp, ip, 0);
  453. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  454. /*
  455. * Get a new, empty transaction to return to our caller.
  456. */
  457. ntp = xfs_trans_dup(tp);
  458. /*
  459. * Commit the transaction containing extent freeing and EFDs.
  460. * If we get an error on the commit here or on the reserve below,
  461. * we need to unlock the inode since the new transaction doesn't
  462. * have the inode attached.
  463. */
  464. error = xfs_trans_commit(tp, 0);
  465. tp = ntp;
  466. if (error) {
  467. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  468. goto error0;
  469. }
  470. /*
  471. * transaction commit worked ok so we can drop the extra ticket
  472. * reference that we gained in xfs_trans_dup()
  473. */
  474. xfs_log_ticket_put(tp->t_ticket);
  475. /*
  476. * Remove the memory for extent descriptions (just bookkeeping).
  477. */
  478. if (ip->i_df.if_bytes)
  479. xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
  480. ASSERT(ip->i_df.if_bytes == 0);
  481. /*
  482. * Put an itruncate log reservation in the new transaction
  483. * for our caller.
  484. */
  485. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
  486. if (error) {
  487. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  488. goto error0;
  489. }
  490. xfs_trans_ijoin(tp, ip, 0);
  491. *tpp = tp;
  492. return 0;
  493. error1:
  494. xfs_bmap_cancel(&free_list);
  495. error0:
  496. return error;
  497. }
  498. /*
  499. * xfs_inactive_symlink - free a symlink
  500. */
  501. int
  502. xfs_inactive_symlink(
  503. struct xfs_inode *ip,
  504. struct xfs_trans **tp)
  505. {
  506. struct xfs_mount *mp = ip->i_mount;
  507. int pathlen;
  508. trace_xfs_inactive_symlink(ip);
  509. ASSERT(xfs_isilocked(ip, XFS_ILOCK_EXCL));
  510. if (XFS_FORCED_SHUTDOWN(mp))
  511. return XFS_ERROR(EIO);
  512. /*
  513. * Zero length symlinks _can_ exist.
  514. */
  515. pathlen = (int)ip->i_d.di_size;
  516. if (!pathlen)
  517. return 0;
  518. if (pathlen < 0 || pathlen > MAXPATHLEN) {
  519. xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)",
  520. __func__, (unsigned long long)ip->i_ino, pathlen);
  521. ASSERT(0);
  522. return XFS_ERROR(EFSCORRUPTED);
  523. }
  524. if (ip->i_df.if_flags & XFS_IFINLINE) {
  525. if (ip->i_df.if_bytes > 0)
  526. xfs_idata_realloc(ip, -(ip->i_df.if_bytes),
  527. XFS_DATA_FORK);
  528. ASSERT(ip->i_df.if_bytes == 0);
  529. return 0;
  530. }
  531. /* remove the remote symlink */
  532. return xfs_inactive_symlink_rmt(ip, tp);
  533. }