xfs_symlink.c 14 KB

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