xfs_symlink.c 14 KB

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