xfs_symlink.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604
  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_shared.h"
  21. #include "xfs_fs.h"
  22. #include "xfs_format.h"
  23. #include "xfs_log_format.h"
  24. #include "xfs_trans_resv.h"
  25. #include "xfs_bit.h"
  26. #include "xfs_mount.h"
  27. #include "xfs_da_format.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_dir2.h"
  30. #include "xfs_inode.h"
  31. #include "xfs_ialloc.h"
  32. #include "xfs_alloc.h"
  33. #include "xfs_bmap.h"
  34. #include "xfs_bmap_btree.h"
  35. #include "xfs_bmap_util.h"
  36. #include "xfs_error.h"
  37. #include "xfs_quota.h"
  38. #include "xfs_trans_space.h"
  39. #include "xfs_trace.h"
  40. #include "xfs_symlink.h"
  41. #include "xfs_trans.h"
  42. #include "xfs_log.h"
  43. /* ----- Kernel only functions below ----- */
  44. STATIC int
  45. xfs_readlink_bmap(
  46. struct xfs_inode *ip,
  47. char *link)
  48. {
  49. struct xfs_mount *mp = ip->i_mount;
  50. struct xfs_bmbt_irec mval[XFS_SYMLINK_MAPS];
  51. struct xfs_buf *bp;
  52. xfs_daddr_t d;
  53. char *cur_chunk;
  54. int pathlen = ip->i_d.di_size;
  55. int nmaps = XFS_SYMLINK_MAPS;
  56. int byte_cnt;
  57. int n;
  58. int error = 0;
  59. int fsblocks = 0;
  60. int offset;
  61. fsblocks = xfs_symlink_blocks(mp, pathlen);
  62. error = xfs_bmapi_read(ip, 0, fsblocks, mval, &nmaps, 0);
  63. if (error)
  64. goto out;
  65. offset = 0;
  66. for (n = 0; n < nmaps; n++) {
  67. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  68. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  69. bp = xfs_buf_read(mp->m_ddev_targp, d, BTOBB(byte_cnt), 0,
  70. &xfs_symlink_buf_ops);
  71. if (!bp)
  72. return -ENOMEM;
  73. error = bp->b_error;
  74. if (error) {
  75. xfs_buf_ioerror_alert(bp, __func__);
  76. xfs_buf_relse(bp);
  77. /* bad CRC means corrupted metadata */
  78. if (error == -EFSBADCRC)
  79. error = -EFSCORRUPTED;
  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(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, cur_chunk, 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 -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 = -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. 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 -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 -ENAMETOOLONG;
  183. udqp = gdqp = NULL;
  184. prid = xfs_get_initial_prid(dp);
  185. /*
  186. * Make sure that we have allocated dquot(s) on disk.
  187. */
  188. error = xfs_qm_vop_dqalloc(dp,
  189. xfs_kuid_to_uid(current_fsuid()),
  190. xfs_kgid_to_gid(current_fsgid()), prid,
  191. XFS_QMOPT_QUOTALL | XFS_QMOPT_INHERIT,
  192. &udqp, &gdqp, &pdqp);
  193. if (error)
  194. return error;
  195. tp = xfs_trans_alloc(mp, XFS_TRANS_SYMLINK);
  196. /*
  197. * The symlink will fit into the inode data fork?
  198. * There can't be any attributes so we get the whole variable part.
  199. */
  200. if (pathlen <= XFS_LITINO(mp, dp->i_d.di_version))
  201. fs_blocks = 0;
  202. else
  203. fs_blocks = xfs_symlink_blocks(mp, pathlen);
  204. resblks = XFS_SYMLINK_SPACE_RES(mp, link_name->len, fs_blocks);
  205. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_symlink, resblks, 0);
  206. if (error == -ENOSPC && fs_blocks == 0) {
  207. resblks = 0;
  208. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_symlink, 0, 0);
  209. }
  210. if (error)
  211. goto out_trans_cancel;
  212. xfs_ilock(dp, XFS_ILOCK_EXCL | XFS_ILOCK_PARENT);
  213. unlock_dp_on_error = true;
  214. /*
  215. * Check whether the directory allows new symlinks or not.
  216. */
  217. if (dp->i_d.di_flags & XFS_DIFLAG_NOSYMLINKS) {
  218. error = -EPERM;
  219. goto out_trans_cancel;
  220. }
  221. /*
  222. * Reserve disk quota : blocks and inode.
  223. */
  224. error = xfs_trans_reserve_quota(tp, mp, udqp, gdqp,
  225. pdqp, resblks, 1, 0);
  226. if (error)
  227. goto out_trans_cancel;
  228. /*
  229. * Check for ability to enter directory entry, if no space reserved.
  230. */
  231. if (!resblks) {
  232. error = xfs_dir_canenter(tp, dp, link_name);
  233. if (error)
  234. goto out_trans_cancel;
  235. }
  236. /*
  237. * Initialize the bmap freelist prior to calling either
  238. * bmapi or the directory create code.
  239. */
  240. xfs_bmap_init(&free_list, &first_block);
  241. /*
  242. * Allocate an inode for the symlink.
  243. */
  244. error = xfs_dir_ialloc(&tp, dp, S_IFLNK | (mode & ~S_IFMT), 1, 0,
  245. prid, resblks > 0, &ip, NULL);
  246. if (error)
  247. goto out_trans_cancel;
  248. /*
  249. * Now we join the directory inode to the transaction. We do not do it
  250. * earlier because xfs_dir_ialloc might commit the previous transaction
  251. * (and release all the locks). An error from here on will result in
  252. * the transaction cancel unlocking dp so don't do it explicitly in the
  253. * error path.
  254. */
  255. xfs_trans_ijoin(tp, dp, XFS_ILOCK_EXCL);
  256. unlock_dp_on_error = false;
  257. /*
  258. * Also attach the dquot(s) to it, if applicable.
  259. */
  260. xfs_qm_vop_create_dqattach(tp, ip, udqp, gdqp, pdqp);
  261. if (resblks)
  262. resblks -= XFS_IALLOC_SPACE_RES(mp);
  263. /*
  264. * If the symlink will fit into the inode, write it inline.
  265. */
  266. if (pathlen <= XFS_IFORK_DSIZE(ip)) {
  267. xfs_idata_realloc(ip, pathlen, XFS_DATA_FORK);
  268. memcpy(ip->i_df.if_u1.if_data, target_path, pathlen);
  269. ip->i_d.di_size = pathlen;
  270. /*
  271. * The inode was initially created in extent format.
  272. */
  273. ip->i_df.if_flags &= ~(XFS_IFEXTENTS | XFS_IFBROOT);
  274. ip->i_df.if_flags |= XFS_IFINLINE;
  275. ip->i_d.di_format = XFS_DINODE_FMT_LOCAL;
  276. xfs_trans_log_inode(tp, ip, XFS_ILOG_DDATA | XFS_ILOG_CORE);
  277. } else {
  278. int offset;
  279. first_fsb = 0;
  280. nmaps = XFS_SYMLINK_MAPS;
  281. error = xfs_bmapi_write(tp, ip, first_fsb, fs_blocks,
  282. XFS_BMAPI_METADATA, &first_block, resblks,
  283. mval, &nmaps, &free_list);
  284. if (error)
  285. goto out_bmap_cancel;
  286. if (resblks)
  287. resblks -= fs_blocks;
  288. ip->i_d.di_size = pathlen;
  289. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  290. cur_chunk = target_path;
  291. offset = 0;
  292. for (n = 0; n < nmaps; n++) {
  293. char *buf;
  294. d = XFS_FSB_TO_DADDR(mp, mval[n].br_startblock);
  295. byte_cnt = XFS_FSB_TO_B(mp, mval[n].br_blockcount);
  296. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp, d,
  297. BTOBB(byte_cnt), 0);
  298. if (!bp) {
  299. error = -ENOMEM;
  300. goto out_bmap_cancel;
  301. }
  302. bp->b_ops = &xfs_symlink_buf_ops;
  303. byte_cnt = XFS_SYMLINK_BUF_SPACE(mp, byte_cnt);
  304. byte_cnt = min(byte_cnt, pathlen);
  305. buf = bp->b_addr;
  306. buf += xfs_symlink_hdr_set(mp, ip->i_ino, offset,
  307. byte_cnt, bp);
  308. memcpy(buf, cur_chunk, byte_cnt);
  309. cur_chunk += byte_cnt;
  310. pathlen -= byte_cnt;
  311. offset += byte_cnt;
  312. xfs_trans_buf_set_type(tp, bp, XFS_BLFT_SYMLINK_BUF);
  313. xfs_trans_log_buf(tp, bp, 0, (buf + byte_cnt - 1) -
  314. (char *)bp->b_addr);
  315. }
  316. ASSERT(pathlen == 0);
  317. }
  318. /*
  319. * Create the directory entry for the symlink.
  320. */
  321. error = xfs_dir_createname(tp, dp, link_name, ip->i_ino,
  322. &first_block, &free_list, resblks);
  323. if (error)
  324. goto out_bmap_cancel;
  325. xfs_trans_ichgtime(tp, dp, XFS_ICHGTIME_MOD | XFS_ICHGTIME_CHG);
  326. xfs_trans_log_inode(tp, dp, XFS_ILOG_CORE);
  327. /*
  328. * If this is a synchronous mount, make sure that the
  329. * symlink transaction goes to disk before returning to
  330. * the user.
  331. */
  332. if (mp->m_flags & (XFS_MOUNT_WSYNC|XFS_MOUNT_DIRSYNC)) {
  333. xfs_trans_set_sync(tp);
  334. }
  335. error = xfs_bmap_finish(&tp, &free_list, &committed);
  336. if (error)
  337. goto out_bmap_cancel;
  338. error = xfs_trans_commit(tp);
  339. if (error)
  340. goto out_release_inode;
  341. xfs_qm_dqrele(udqp);
  342. xfs_qm_dqrele(gdqp);
  343. xfs_qm_dqrele(pdqp);
  344. *ipp = ip;
  345. return 0;
  346. out_bmap_cancel:
  347. xfs_bmap_cancel(&free_list);
  348. out_trans_cancel:
  349. xfs_trans_cancel(tp);
  350. out_release_inode:
  351. /*
  352. * Wait until after the current transaction is aborted to finish the
  353. * setup of the inode and release the inode. This prevents recursive
  354. * transactions and deadlocks from xfs_inactive.
  355. */
  356. if (ip) {
  357. xfs_finish_inode_setup(ip);
  358. IRELE(ip);
  359. }
  360. xfs_qm_dqrele(udqp);
  361. xfs_qm_dqrele(gdqp);
  362. xfs_qm_dqrele(pdqp);
  363. if (unlock_dp_on_error)
  364. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  365. return error;
  366. }
  367. /*
  368. * Free a symlink that has blocks associated with it.
  369. */
  370. STATIC int
  371. xfs_inactive_symlink_rmt(
  372. struct xfs_inode *ip)
  373. {
  374. xfs_buf_t *bp;
  375. int committed;
  376. int done;
  377. int error;
  378. xfs_fsblock_t first_block;
  379. xfs_bmap_free_t free_list;
  380. int i;
  381. xfs_mount_t *mp;
  382. xfs_bmbt_irec_t mval[XFS_SYMLINK_MAPS];
  383. int nmaps;
  384. int size;
  385. xfs_trans_t *tp;
  386. mp = ip->i_mount;
  387. ASSERT(ip->i_df.if_flags & XFS_IFEXTENTS);
  388. /*
  389. * We're freeing a symlink that has some
  390. * blocks allocated to it. Free the
  391. * blocks here. We know that we've got
  392. * either 1 or 2 extents and that we can
  393. * free them all in one bunmapi call.
  394. */
  395. ASSERT(ip->i_d.di_nextents > 0 && ip->i_d.di_nextents <= 2);
  396. tp = xfs_trans_alloc(mp, XFS_TRANS_INACTIVE);
  397. error = xfs_trans_reserve(tp, &M_RES(mp)->tr_itruncate, 0, 0);
  398. if (error) {
  399. xfs_trans_cancel(tp);
  400. return error;
  401. }
  402. xfs_ilock(ip, XFS_ILOCK_EXCL);
  403. xfs_trans_ijoin(tp, ip, 0);
  404. /*
  405. * Lock the inode, fix the size, and join it to the transaction.
  406. * Hold it so in the normal path, we still have it locked for
  407. * the second transaction. In the error paths we need it
  408. * held so the cancel won't rele it, see below.
  409. */
  410. size = (int)ip->i_d.di_size;
  411. ip->i_d.di_size = 0;
  412. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  413. /*
  414. * Find the block(s) so we can inval and unmap them.
  415. */
  416. done = 0;
  417. xfs_bmap_init(&free_list, &first_block);
  418. nmaps = ARRAY_SIZE(mval);
  419. error = xfs_bmapi_read(ip, 0, xfs_symlink_blocks(mp, size),
  420. mval, &nmaps, 0);
  421. if (error)
  422. goto error_trans_cancel;
  423. /*
  424. * Invalidate the block(s). No validation is done.
  425. */
  426. for (i = 0; i < nmaps; i++) {
  427. bp = xfs_trans_get_buf(tp, mp->m_ddev_targp,
  428. XFS_FSB_TO_DADDR(mp, mval[i].br_startblock),
  429. XFS_FSB_TO_BB(mp, mval[i].br_blockcount), 0);
  430. if (!bp) {
  431. error = -ENOMEM;
  432. goto error_bmap_cancel;
  433. }
  434. xfs_trans_binval(tp, bp);
  435. }
  436. /*
  437. * Unmap the dead block(s) to the free_list.
  438. */
  439. error = xfs_bunmapi(tp, ip, 0, size, XFS_BMAPI_METADATA, nmaps,
  440. &first_block, &free_list, &done);
  441. if (error)
  442. goto error_bmap_cancel;
  443. ASSERT(done);
  444. /*
  445. * Commit the first transaction. This logs the EFI and the inode.
  446. */
  447. error = xfs_bmap_finish(&tp, &free_list, &committed);
  448. if (error)
  449. goto error_bmap_cancel;
  450. /*
  451. * The transaction must have been committed, since there were
  452. * actually extents freed by xfs_bunmapi. See xfs_bmap_finish.
  453. * The new tp has the extent freeing and EFDs.
  454. */
  455. ASSERT(committed);
  456. /*
  457. * The first xact was committed, so add the inode to the new one.
  458. * Mark it dirty so it will be logged and moved forward in the log as
  459. * part of every commit.
  460. */
  461. xfs_trans_ijoin(tp, ip, 0);
  462. xfs_trans_log_inode(tp, ip, XFS_ILOG_CORE);
  463. /*
  464. * Commit the transaction containing extent freeing and EFDs.
  465. */
  466. error = xfs_trans_commit(tp);
  467. if (error) {
  468. ASSERT(XFS_FORCED_SHUTDOWN(mp));
  469. goto error_unlock;
  470. }
  471. /*
  472. * Remove the memory for extent descriptions (just bookkeeping).
  473. */
  474. if (ip->i_df.if_bytes)
  475. xfs_idata_realloc(ip, -ip->i_df.if_bytes, XFS_DATA_FORK);
  476. ASSERT(ip->i_df.if_bytes == 0);
  477. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  478. return 0;
  479. error_bmap_cancel:
  480. xfs_bmap_cancel(&free_list);
  481. error_trans_cancel:
  482. xfs_trans_cancel(tp);
  483. error_unlock:
  484. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  485. return error;
  486. }
  487. /*
  488. * xfs_inactive_symlink - free a symlink
  489. */
  490. int
  491. xfs_inactive_symlink(
  492. struct xfs_inode *ip)
  493. {
  494. struct xfs_mount *mp = ip->i_mount;
  495. int pathlen;
  496. trace_xfs_inactive_symlink(ip);
  497. if (XFS_FORCED_SHUTDOWN(mp))
  498. return -EIO;
  499. xfs_ilock(ip, XFS_ILOCK_EXCL);
  500. /*
  501. * Zero length symlinks _can_ exist.
  502. */
  503. pathlen = (int)ip->i_d.di_size;
  504. if (!pathlen) {
  505. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  506. return 0;
  507. }
  508. if (pathlen < 0 || pathlen > MAXPATHLEN) {
  509. xfs_alert(mp, "%s: inode (0x%llx) bad symlink length (%d)",
  510. __func__, (unsigned long long)ip->i_ino, pathlen);
  511. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  512. ASSERT(0);
  513. return -EFSCORRUPTED;
  514. }
  515. if (ip->i_df.if_flags & XFS_IFINLINE) {
  516. if (ip->i_df.if_bytes > 0)
  517. xfs_idata_realloc(ip, -(ip->i_df.if_bytes),
  518. XFS_DATA_FORK);
  519. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  520. ASSERT(ip->i_df.if_bytes == 0);
  521. return 0;
  522. }
  523. xfs_iunlock(ip, XFS_ILOCK_EXCL);
  524. /* remove the remote symlink */
  525. return xfs_inactive_symlink_rmt(ip);
  526. }