PageRenderTime 63ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/linux-2.6.21.x/fs/xfs/xfs_attr.c

https://bitbucket.org/altlc/wive-rtnl-ralink-rt305x-routers-firmware-amod
C | 2732 lines | 1812 code | 285 blank | 635 comment | 374 complexity | 213fe2f6186e697935615049a65a5319 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, BSD-3-Clause, MPL-2.0-no-copyleft-exception, GPL-2.0, GPL-3.0, LGPL-3.0, 0BSD, AGPL-1.0, LGPL-2.1, LGPL-2.0
  1. /*
  2. * Copyright (c) 2000-2005 Silicon Graphics, Inc.
  3. * All Rights Reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it would be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write the Free Software Foundation,
  16. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include <linux/capability.h>
  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_inum.h"
  25. #include "xfs_trans.h"
  26. #include "xfs_sb.h"
  27. #include "xfs_ag.h"
  28. #include "xfs_dir2.h"
  29. #include "xfs_dmapi.h"
  30. #include "xfs_mount.h"
  31. #include "xfs_da_btree.h"
  32. #include "xfs_bmap_btree.h"
  33. #include "xfs_alloc_btree.h"
  34. #include "xfs_ialloc_btree.h"
  35. #include "xfs_dir2_sf.h"
  36. #include "xfs_attr_sf.h"
  37. #include "xfs_dinode.h"
  38. #include "xfs_inode.h"
  39. #include "xfs_alloc.h"
  40. #include "xfs_btree.h"
  41. #include "xfs_inode_item.h"
  42. #include "xfs_bmap.h"
  43. #include "xfs_attr.h"
  44. #include "xfs_attr_leaf.h"
  45. #include "xfs_error.h"
  46. #include "xfs_quota.h"
  47. #include "xfs_trans_space.h"
  48. #include "xfs_acl.h"
  49. #include "xfs_rw.h"
  50. /*
  51. * xfs_attr.c
  52. *
  53. * Provide the external interfaces to manage attribute lists.
  54. */
  55. #define ATTR_SYSCOUNT 2
  56. static struct attrnames posix_acl_access;
  57. static struct attrnames posix_acl_default;
  58. static struct attrnames *attr_system_names[ATTR_SYSCOUNT];
  59. /*========================================================================
  60. * Function prototypes for the kernel.
  61. *========================================================================*/
  62. /*
  63. * Internal routines when attribute list fits inside the inode.
  64. */
  65. STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
  66. /*
  67. * Internal routines when attribute list is one block.
  68. */
  69. STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
  70. STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
  71. STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
  72. STATIC int xfs_attr_leaf_list(xfs_attr_list_context_t *context);
  73. /*
  74. * Internal routines when attribute list is more than one block.
  75. */
  76. STATIC int xfs_attr_node_get(xfs_da_args_t *args);
  77. STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
  78. STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
  79. STATIC int xfs_attr_node_list(xfs_attr_list_context_t *context);
  80. STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
  81. STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
  82. /*
  83. * Routines to manipulate out-of-line attribute values.
  84. */
  85. STATIC int xfs_attr_rmtval_set(xfs_da_args_t *args);
  86. STATIC int xfs_attr_rmtval_remove(xfs_da_args_t *args);
  87. #define ATTR_RMTVALUE_MAPSIZE 1 /* # of map entries at once */
  88. #if defined(XFS_ATTR_TRACE)
  89. ktrace_t *xfs_attr_trace_buf;
  90. #endif
  91. /*========================================================================
  92. * Overall external interface routines.
  93. *========================================================================*/
  94. int
  95. xfs_attr_fetch(xfs_inode_t *ip, const char *name, int namelen,
  96. char *value, int *valuelenp, int flags, struct cred *cred)
  97. {
  98. xfs_da_args_t args;
  99. int error;
  100. if ((XFS_IFORK_Q(ip) == 0) ||
  101. (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  102. ip->i_d.di_anextents == 0))
  103. return(ENOATTR);
  104. /*
  105. * Fill in the arg structure for this request.
  106. */
  107. memset((char *)&args, 0, sizeof(args));
  108. args.name = name;
  109. args.namelen = namelen;
  110. args.value = value;
  111. args.valuelen = *valuelenp;
  112. args.flags = flags;
  113. args.hashval = xfs_da_hashname(args.name, args.namelen);
  114. args.dp = ip;
  115. args.whichfork = XFS_ATTR_FORK;
  116. /*
  117. * Decide on what work routines to call based on the inode size.
  118. */
  119. if (XFS_IFORK_Q(ip) == 0 ||
  120. (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  121. ip->i_d.di_anextents == 0)) {
  122. error = XFS_ERROR(ENOATTR);
  123. } else if (ip->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  124. error = xfs_attr_shortform_getvalue(&args);
  125. } else if (xfs_bmap_one_block(ip, XFS_ATTR_FORK)) {
  126. error = xfs_attr_leaf_get(&args);
  127. } else {
  128. error = xfs_attr_node_get(&args);
  129. }
  130. /*
  131. * Return the number of bytes in the value to the caller.
  132. */
  133. *valuelenp = args.valuelen;
  134. if (error == EEXIST)
  135. error = 0;
  136. return(error);
  137. }
  138. int
  139. xfs_attr_get(bhv_desc_t *bdp, const char *name, char *value, int *valuelenp,
  140. int flags, struct cred *cred)
  141. {
  142. xfs_inode_t *ip = XFS_BHVTOI(bdp);
  143. int error, namelen;
  144. XFS_STATS_INC(xs_attr_get);
  145. if (!name)
  146. return(EINVAL);
  147. namelen = strlen(name);
  148. if (namelen >= MAXNAMELEN)
  149. return(EFAULT); /* match IRIX behaviour */
  150. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  151. return(EIO);
  152. xfs_ilock(ip, XFS_ILOCK_SHARED);
  153. error = xfs_attr_fetch(ip, name, namelen, value, valuelenp, flags, cred);
  154. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  155. return(error);
  156. }
  157. int
  158. xfs_attr_set_int(xfs_inode_t *dp, const char *name, int namelen,
  159. char *value, int valuelen, int flags)
  160. {
  161. xfs_da_args_t args;
  162. xfs_fsblock_t firstblock;
  163. xfs_bmap_free_t flist;
  164. int error, err2, committed;
  165. int local, size;
  166. uint nblks;
  167. xfs_mount_t *mp = dp->i_mount;
  168. int rsvd = (flags & ATTR_ROOT) != 0;
  169. /*
  170. * Attach the dquots to the inode.
  171. */
  172. if ((error = XFS_QM_DQATTACH(mp, dp, 0)))
  173. return (error);
  174. /*
  175. * If the inode doesn't have an attribute fork, add one.
  176. * (inode must not be locked when we call this routine)
  177. */
  178. if (XFS_IFORK_Q(dp) == 0) {
  179. int sf_size = sizeof(xfs_attr_sf_hdr_t) +
  180. XFS_ATTR_SF_ENTSIZE_BYNAME(namelen, valuelen);
  181. if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
  182. return(error);
  183. }
  184. /*
  185. * Fill in the arg structure for this request.
  186. */
  187. memset((char *)&args, 0, sizeof(args));
  188. args.name = name;
  189. args.namelen = namelen;
  190. args.value = value;
  191. args.valuelen = valuelen;
  192. args.flags = flags;
  193. args.hashval = xfs_da_hashname(args.name, args.namelen);
  194. args.dp = dp;
  195. args.firstblock = &firstblock;
  196. args.flist = &flist;
  197. args.whichfork = XFS_ATTR_FORK;
  198. args.addname = 1;
  199. args.oknoent = 1;
  200. /*
  201. * Determine space new attribute will use, and if it would be
  202. * "local" or "remote" (note: local != inline).
  203. */
  204. size = xfs_attr_leaf_newentsize(namelen, valuelen,
  205. mp->m_sb.sb_blocksize, &local);
  206. nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
  207. if (local) {
  208. if (size > (mp->m_sb.sb_blocksize >> 1)) {
  209. /* Double split possible */
  210. nblks <<= 1;
  211. }
  212. } else {
  213. uint dblocks = XFS_B_TO_FSB(mp, valuelen);
  214. /* Out of line attribute, cannot double split, but make
  215. * room for the attribute value itself.
  216. */
  217. nblks += dblocks;
  218. nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
  219. }
  220. /* Size is now blocks for attribute data */
  221. args.total = nblks;
  222. /*
  223. * Start our first transaction of the day.
  224. *
  225. * All future transactions during this code must be "chained" off
  226. * this one via the trans_dup() call. All transactions will contain
  227. * the inode, and the inode will always be marked with trans_ihold().
  228. * Since the inode will be locked in all transactions, we must log
  229. * the inode in every transaction to let it float upward through
  230. * the log.
  231. */
  232. args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
  233. /*
  234. * Root fork attributes can use reserved data blocks for this
  235. * operation if necessary
  236. */
  237. if (rsvd)
  238. args.trans->t_flags |= XFS_TRANS_RESERVE;
  239. if ((error = xfs_trans_reserve(args.trans, (uint) nblks,
  240. XFS_ATTRSET_LOG_RES(mp, nblks),
  241. 0, XFS_TRANS_PERM_LOG_RES,
  242. XFS_ATTRSET_LOG_COUNT))) {
  243. xfs_trans_cancel(args.trans, 0);
  244. return(error);
  245. }
  246. xfs_ilock(dp, XFS_ILOCK_EXCL);
  247. error = XFS_TRANS_RESERVE_QUOTA_NBLKS(mp, args.trans, dp, nblks, 0,
  248. rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
  249. XFS_QMOPT_RES_REGBLKS);
  250. if (error) {
  251. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  252. xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  253. return (error);
  254. }
  255. xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
  256. xfs_trans_ihold(args.trans, dp);
  257. /*
  258. * If the attribute list is non-existent or a shortform list,
  259. * upgrade it to a single-leaf-block attribute list.
  260. */
  261. if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
  262. ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
  263. (dp->i_d.di_anextents == 0))) {
  264. /*
  265. * Build initial attribute list (if required).
  266. */
  267. if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
  268. xfs_attr_shortform_create(&args);
  269. /*
  270. * Try to add the attr to the attribute list in
  271. * the inode.
  272. */
  273. error = xfs_attr_shortform_addname(&args);
  274. if (error != ENOSPC) {
  275. /*
  276. * Commit the shortform mods, and we're done.
  277. * NOTE: this is also the error path (EEXIST, etc).
  278. */
  279. ASSERT(args.trans != NULL);
  280. /*
  281. * If this is a synchronous mount, make sure that
  282. * the transaction goes to disk before returning
  283. * to the user.
  284. */
  285. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  286. xfs_trans_set_sync(args.trans);
  287. }
  288. err2 = xfs_trans_commit(args.trans,
  289. XFS_TRANS_RELEASE_LOG_RES,
  290. NULL);
  291. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  292. /*
  293. * Hit the inode change time.
  294. */
  295. if (!error && (flags & ATTR_KERNOTIME) == 0) {
  296. xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
  297. }
  298. return(error == 0 ? err2 : error);
  299. }
  300. /*
  301. * It won't fit in the shortform, transform to a leaf block.
  302. * GROT: another possible req'mt for a double-split btree op.
  303. */
  304. XFS_BMAP_INIT(args.flist, args.firstblock);
  305. error = xfs_attr_shortform_to_leaf(&args);
  306. if (!error) {
  307. error = xfs_bmap_finish(&args.trans, args.flist,
  308. &committed);
  309. }
  310. if (error) {
  311. ASSERT(committed);
  312. args.trans = NULL;
  313. xfs_bmap_cancel(&flist);
  314. goto out;
  315. }
  316. /*
  317. * bmap_finish() may have committed the last trans and started
  318. * a new one. We need the inode to be in all transactions.
  319. */
  320. if (committed) {
  321. xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
  322. xfs_trans_ihold(args.trans, dp);
  323. }
  324. /*
  325. * Commit the leaf transformation. We'll need another (linked)
  326. * transaction to add the new attribute to the leaf.
  327. */
  328. if ((error = xfs_attr_rolltrans(&args.trans, dp)))
  329. goto out;
  330. }
  331. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  332. error = xfs_attr_leaf_addname(&args);
  333. } else {
  334. error = xfs_attr_node_addname(&args);
  335. }
  336. if (error) {
  337. goto out;
  338. }
  339. /*
  340. * If this is a synchronous mount, make sure that the
  341. * transaction goes to disk before returning to the user.
  342. */
  343. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  344. xfs_trans_set_sync(args.trans);
  345. }
  346. /*
  347. * Commit the last in the sequence of transactions.
  348. */
  349. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  350. error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES,
  351. NULL);
  352. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  353. /*
  354. * Hit the inode change time.
  355. */
  356. if (!error && (flags & ATTR_KERNOTIME) == 0) {
  357. xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
  358. }
  359. return(error);
  360. out:
  361. if (args.trans)
  362. xfs_trans_cancel(args.trans,
  363. XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  364. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  365. return(error);
  366. }
  367. int
  368. xfs_attr_set(bhv_desc_t *bdp, const char *name, char *value, int valuelen, int flags,
  369. struct cred *cred)
  370. {
  371. xfs_inode_t *dp;
  372. int namelen;
  373. namelen = strlen(name);
  374. if (namelen >= MAXNAMELEN)
  375. return EFAULT; /* match IRIX behaviour */
  376. XFS_STATS_INC(xs_attr_set);
  377. dp = XFS_BHVTOI(bdp);
  378. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  379. return (EIO);
  380. return xfs_attr_set_int(dp, name, namelen, value, valuelen, flags);
  381. }
  382. /*
  383. * Generic handler routine to remove a name from an attribute list.
  384. * Transitions attribute list from Btree to shortform as necessary.
  385. */
  386. int
  387. xfs_attr_remove_int(xfs_inode_t *dp, const char *name, int namelen, int flags)
  388. {
  389. xfs_da_args_t args;
  390. xfs_fsblock_t firstblock;
  391. xfs_bmap_free_t flist;
  392. int error;
  393. xfs_mount_t *mp = dp->i_mount;
  394. /*
  395. * Fill in the arg structure for this request.
  396. */
  397. memset((char *)&args, 0, sizeof(args));
  398. args.name = name;
  399. args.namelen = namelen;
  400. args.flags = flags;
  401. args.hashval = xfs_da_hashname(args.name, args.namelen);
  402. args.dp = dp;
  403. args.firstblock = &firstblock;
  404. args.flist = &flist;
  405. args.total = 0;
  406. args.whichfork = XFS_ATTR_FORK;
  407. /*
  408. * Attach the dquots to the inode.
  409. */
  410. if ((error = XFS_QM_DQATTACH(mp, dp, 0)))
  411. return (error);
  412. /*
  413. * Start our first transaction of the day.
  414. *
  415. * All future transactions during this code must be "chained" off
  416. * this one via the trans_dup() call. All transactions will contain
  417. * the inode, and the inode will always be marked with trans_ihold().
  418. * Since the inode will be locked in all transactions, we must log
  419. * the inode in every transaction to let it float upward through
  420. * the log.
  421. */
  422. args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
  423. /*
  424. * Root fork attributes can use reserved data blocks for this
  425. * operation if necessary
  426. */
  427. if (flags & ATTR_ROOT)
  428. args.trans->t_flags |= XFS_TRANS_RESERVE;
  429. if ((error = xfs_trans_reserve(args.trans,
  430. XFS_ATTRRM_SPACE_RES(mp),
  431. XFS_ATTRRM_LOG_RES(mp),
  432. 0, XFS_TRANS_PERM_LOG_RES,
  433. XFS_ATTRRM_LOG_COUNT))) {
  434. xfs_trans_cancel(args.trans, 0);
  435. return(error);
  436. }
  437. xfs_ilock(dp, XFS_ILOCK_EXCL);
  438. /*
  439. * No need to make quota reservations here. We expect to release some
  440. * blocks not allocate in the common case.
  441. */
  442. xfs_trans_ijoin(args.trans, dp, XFS_ILOCK_EXCL);
  443. xfs_trans_ihold(args.trans, dp);
  444. /*
  445. * Decide on what work routines to call based on the inode size.
  446. */
  447. if (XFS_IFORK_Q(dp) == 0 ||
  448. (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  449. dp->i_d.di_anextents == 0)) {
  450. error = XFS_ERROR(ENOATTR);
  451. goto out;
  452. }
  453. if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  454. ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
  455. error = xfs_attr_shortform_remove(&args);
  456. if (error) {
  457. goto out;
  458. }
  459. } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  460. error = xfs_attr_leaf_removename(&args);
  461. } else {
  462. error = xfs_attr_node_removename(&args);
  463. }
  464. if (error) {
  465. goto out;
  466. }
  467. /*
  468. * If this is a synchronous mount, make sure that the
  469. * transaction goes to disk before returning to the user.
  470. */
  471. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  472. xfs_trans_set_sync(args.trans);
  473. }
  474. /*
  475. * Commit the last in the sequence of transactions.
  476. */
  477. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  478. error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES,
  479. NULL);
  480. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  481. /*
  482. * Hit the inode change time.
  483. */
  484. if (!error && (flags & ATTR_KERNOTIME) == 0) {
  485. xfs_ichgtime(dp, XFS_ICHGTIME_CHG);
  486. }
  487. return(error);
  488. out:
  489. if (args.trans)
  490. xfs_trans_cancel(args.trans,
  491. XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  492. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  493. return(error);
  494. }
  495. int
  496. xfs_attr_remove(bhv_desc_t *bdp, const char *name, int flags, struct cred *cred)
  497. {
  498. xfs_inode_t *dp;
  499. int namelen;
  500. namelen = strlen(name);
  501. if (namelen >= MAXNAMELEN)
  502. return EFAULT; /* match IRIX behaviour */
  503. XFS_STATS_INC(xs_attr_remove);
  504. dp = XFS_BHVTOI(bdp);
  505. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  506. return (EIO);
  507. xfs_ilock(dp, XFS_ILOCK_SHARED);
  508. if (XFS_IFORK_Q(dp) == 0 ||
  509. (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  510. dp->i_d.di_anextents == 0)) {
  511. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  512. return(XFS_ERROR(ENOATTR));
  513. }
  514. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  515. return xfs_attr_remove_int(dp, name, namelen, flags);
  516. }
  517. int /* error */
  518. xfs_attr_list_int(xfs_attr_list_context_t *context)
  519. {
  520. int error;
  521. xfs_inode_t *dp = context->dp;
  522. /*
  523. * Decide on what work routines to call based on the inode size.
  524. */
  525. if (XFS_IFORK_Q(dp) == 0 ||
  526. (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  527. dp->i_d.di_anextents == 0)) {
  528. error = 0;
  529. } else if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  530. error = xfs_attr_shortform_list(context);
  531. } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  532. error = xfs_attr_leaf_list(context);
  533. } else {
  534. error = xfs_attr_node_list(context);
  535. }
  536. return error;
  537. }
  538. #define ATTR_ENTBASESIZE /* minimum bytes used by an attr */ \
  539. (((struct attrlist_ent *) 0)->a_name - (char *) 0)
  540. #define ATTR_ENTSIZE(namelen) /* actual bytes used by an attr */ \
  541. ((ATTR_ENTBASESIZE + (namelen) + 1 + sizeof(u_int32_t)-1) \
  542. & ~(sizeof(u_int32_t)-1))
  543. /*
  544. * Format an attribute and copy it out to the user's buffer.
  545. * Take care to check values and protect against them changing later,
  546. * we may be reading them directly out of a user buffer.
  547. */
  548. /*ARGSUSED*/
  549. STATIC int
  550. xfs_attr_put_listent(xfs_attr_list_context_t *context, attrnames_t *namesp,
  551. char *name, int namelen,
  552. int valuelen, char *value)
  553. {
  554. attrlist_ent_t *aep;
  555. int arraytop;
  556. ASSERT(!(context->flags & ATTR_KERNOVAL));
  557. ASSERT(context->count >= 0);
  558. ASSERT(context->count < (ATTR_MAX_VALUELEN/8));
  559. ASSERT(context->firstu >= sizeof(*context->alist));
  560. ASSERT(context->firstu <= context->bufsize);
  561. arraytop = sizeof(*context->alist) +
  562. context->count * sizeof(context->alist->al_offset[0]);
  563. context->firstu -= ATTR_ENTSIZE(namelen);
  564. if (context->firstu < arraytop) {
  565. xfs_attr_trace_l_c("buffer full", context);
  566. context->alist->al_more = 1;
  567. context->seen_enough = 1;
  568. return 1;
  569. }
  570. aep = (attrlist_ent_t *)&(((char *)context->alist)[ context->firstu ]);
  571. aep->a_valuelen = valuelen;
  572. memcpy(aep->a_name, name, namelen);
  573. aep->a_name[ namelen ] = 0;
  574. context->alist->al_offset[ context->count++ ] = context->firstu;
  575. context->alist->al_count = context->count;
  576. xfs_attr_trace_l_c("add", context);
  577. return 0;
  578. }
  579. STATIC int
  580. xfs_attr_kern_list(xfs_attr_list_context_t *context, attrnames_t *namesp,
  581. char *name, int namelen,
  582. int valuelen, char *value)
  583. {
  584. char *offset;
  585. int arraytop;
  586. ASSERT(context->count >= 0);
  587. arraytop = context->count + namesp->attr_namelen + namelen + 1;
  588. if (arraytop > context->firstu) {
  589. context->count = -1; /* insufficient space */
  590. return 1;
  591. }
  592. offset = (char *)context->alist + context->count;
  593. strncpy(offset, namesp->attr_name, namesp->attr_namelen);
  594. offset += namesp->attr_namelen;
  595. strncpy(offset, name, namelen); /* real name */
  596. offset += namelen;
  597. *offset = '\0';
  598. context->count += namesp->attr_namelen + namelen + 1;
  599. return 0;
  600. }
  601. /*ARGSUSED*/
  602. STATIC int
  603. xfs_attr_kern_list_sizes(xfs_attr_list_context_t *context, attrnames_t *namesp,
  604. char *name, int namelen,
  605. int valuelen, char *value)
  606. {
  607. context->count += namesp->attr_namelen + namelen + 1;
  608. return 0;
  609. }
  610. /*
  611. * Generate a list of extended attribute names and optionally
  612. * also value lengths. Positive return value follows the XFS
  613. * convention of being an error, zero or negative return code
  614. * is the length of the buffer returned (negated), indicating
  615. * success.
  616. */
  617. int
  618. xfs_attr_list(bhv_desc_t *bdp, char *buffer, int bufsize, int flags,
  619. attrlist_cursor_kern_t *cursor, struct cred *cred)
  620. {
  621. xfs_attr_list_context_t context;
  622. xfs_inode_t *dp;
  623. int error;
  624. XFS_STATS_INC(xs_attr_list);
  625. /*
  626. * Validate the cursor.
  627. */
  628. if (cursor->pad1 || cursor->pad2)
  629. return(XFS_ERROR(EINVAL));
  630. if ((cursor->initted == 0) &&
  631. (cursor->hashval || cursor->blkno || cursor->offset))
  632. return XFS_ERROR(EINVAL);
  633. /*
  634. * Check for a properly aligned buffer.
  635. */
  636. if (((long)buffer) & (sizeof(int)-1))
  637. return XFS_ERROR(EFAULT);
  638. if (flags & ATTR_KERNOVAL)
  639. bufsize = 0;
  640. /*
  641. * Initialize the output buffer.
  642. */
  643. context.dp = dp = XFS_BHVTOI(bdp);
  644. context.cursor = cursor;
  645. context.count = 0;
  646. context.dupcnt = 0;
  647. context.resynch = 1;
  648. context.flags = flags;
  649. context.seen_enough = 0;
  650. context.alist = (attrlist_t *)buffer;
  651. context.put_value = 0;
  652. if (flags & ATTR_KERNAMELS) {
  653. context.bufsize = bufsize;
  654. context.firstu = context.bufsize;
  655. if (flags & ATTR_KERNOVAL)
  656. context.put_listent = xfs_attr_kern_list_sizes;
  657. else
  658. context.put_listent = xfs_attr_kern_list;
  659. } else {
  660. context.bufsize = (bufsize & ~(sizeof(int)-1)); /* align */
  661. context.firstu = context.bufsize;
  662. context.alist->al_count = 0;
  663. context.alist->al_more = 0;
  664. context.alist->al_offset[0] = context.bufsize;
  665. context.put_listent = xfs_attr_put_listent;
  666. }
  667. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  668. return EIO;
  669. xfs_ilock(dp, XFS_ILOCK_SHARED);
  670. xfs_attr_trace_l_c("syscall start", &context);
  671. error = xfs_attr_list_int(&context);
  672. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  673. xfs_attr_trace_l_c("syscall end", &context);
  674. if (context.flags & (ATTR_KERNOVAL|ATTR_KERNAMELS)) {
  675. /* must return negated buffer size or the error */
  676. if (context.count < 0)
  677. error = XFS_ERROR(ERANGE);
  678. else
  679. error = -context.count;
  680. } else
  681. ASSERT(error >= 0);
  682. return error;
  683. }
  684. int /* error */
  685. xfs_attr_inactive(xfs_inode_t *dp)
  686. {
  687. xfs_trans_t *trans;
  688. xfs_mount_t *mp;
  689. int error;
  690. mp = dp->i_mount;
  691. ASSERT(! XFS_NOT_DQATTACHED(mp, dp));
  692. xfs_ilock(dp, XFS_ILOCK_SHARED);
  693. if ((XFS_IFORK_Q(dp) == 0) ||
  694. (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
  695. (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  696. dp->i_d.di_anextents == 0)) {
  697. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  698. return(0);
  699. }
  700. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  701. /*
  702. * Start our first transaction of the day.
  703. *
  704. * All future transactions during this code must be "chained" off
  705. * this one via the trans_dup() call. All transactions will contain
  706. * the inode, and the inode will always be marked with trans_ihold().
  707. * Since the inode will be locked in all transactions, we must log
  708. * the inode in every transaction to let it float upward through
  709. * the log.
  710. */
  711. trans = xfs_trans_alloc(mp, XFS_TRANS_ATTRINVAL);
  712. if ((error = xfs_trans_reserve(trans, 0, XFS_ATTRINVAL_LOG_RES(mp), 0,
  713. XFS_TRANS_PERM_LOG_RES,
  714. XFS_ATTRINVAL_LOG_COUNT))) {
  715. xfs_trans_cancel(trans, 0);
  716. return(error);
  717. }
  718. xfs_ilock(dp, XFS_ILOCK_EXCL);
  719. /*
  720. * No need to make quota reservations here. We expect to release some
  721. * blocks, not allocate, in the common case.
  722. */
  723. xfs_trans_ijoin(trans, dp, XFS_ILOCK_EXCL);
  724. xfs_trans_ihold(trans, dp);
  725. /*
  726. * Decide on what work routines to call based on the inode size.
  727. */
  728. if ((XFS_IFORK_Q(dp) == 0) ||
  729. (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
  730. (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  731. dp->i_d.di_anextents == 0)) {
  732. error = 0;
  733. goto out;
  734. }
  735. error = xfs_attr_root_inactive(&trans, dp);
  736. if (error)
  737. goto out;
  738. /*
  739. * signal synchronous inactive transactions unless this
  740. * is a synchronous mount filesystem in which case we
  741. * know that we're here because we've been called out of
  742. * xfs_inactive which means that the last reference is gone
  743. * and the unlink transaction has already hit the disk so
  744. * async inactive transactions are safe.
  745. */
  746. if ((error = xfs_itruncate_finish(&trans, dp, 0LL, XFS_ATTR_FORK,
  747. (!(mp->m_flags & XFS_MOUNT_WSYNC)
  748. ? 1 : 0))))
  749. goto out;
  750. /*
  751. * Commit the last in the sequence of transactions.
  752. */
  753. xfs_trans_log_inode(trans, dp, XFS_ILOG_CORE);
  754. error = xfs_trans_commit(trans, XFS_TRANS_RELEASE_LOG_RES,
  755. NULL);
  756. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  757. return(error);
  758. out:
  759. xfs_trans_cancel(trans, XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  760. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  761. return(error);
  762. }
  763. /*========================================================================
  764. * External routines when attribute list is inside the inode
  765. *========================================================================*/
  766. /*
  767. * Add a name to the shortform attribute list structure
  768. * This is the external routine.
  769. */
  770. STATIC int
  771. xfs_attr_shortform_addname(xfs_da_args_t *args)
  772. {
  773. int newsize, forkoff, retval;
  774. retval = xfs_attr_shortform_lookup(args);
  775. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  776. return(retval);
  777. } else if (retval == EEXIST) {
  778. if (args->flags & ATTR_CREATE)
  779. return(retval);
  780. retval = xfs_attr_shortform_remove(args);
  781. ASSERT(retval == 0);
  782. }
  783. if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
  784. args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
  785. return(XFS_ERROR(ENOSPC));
  786. newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
  787. newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
  788. forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
  789. if (!forkoff)
  790. return(XFS_ERROR(ENOSPC));
  791. xfs_attr_shortform_add(args, forkoff);
  792. return(0);
  793. }
  794. /*========================================================================
  795. * External routines when attribute list is one block
  796. *========================================================================*/
  797. /*
  798. * Add a name to the leaf attribute list structure
  799. *
  800. * This leaf block cannot have a "remote" value, we only call this routine
  801. * if bmap_one_block() says there is only one block (ie: no remote blks).
  802. */
  803. int
  804. xfs_attr_leaf_addname(xfs_da_args_t *args)
  805. {
  806. xfs_inode_t *dp;
  807. xfs_dabuf_t *bp;
  808. int retval, error, committed, forkoff;
  809. /*
  810. * Read the (only) block in the attribute list in.
  811. */
  812. dp = args->dp;
  813. args->blkno = 0;
  814. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
  815. XFS_ATTR_FORK);
  816. if (error)
  817. return(error);
  818. ASSERT(bp != NULL);
  819. /*
  820. * Look up the given attribute in the leaf block. Figure out if
  821. * the given flags produce an error or call for an atomic rename.
  822. */
  823. retval = xfs_attr_leaf_lookup_int(bp, args);
  824. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  825. xfs_da_brelse(args->trans, bp);
  826. return(retval);
  827. } else if (retval == EEXIST) {
  828. if (args->flags & ATTR_CREATE) { /* pure create op */
  829. xfs_da_brelse(args->trans, bp);
  830. return(retval);
  831. }
  832. args->rename = 1; /* an atomic rename */
  833. args->blkno2 = args->blkno; /* set 2nd entry info*/
  834. args->index2 = args->index;
  835. args->rmtblkno2 = args->rmtblkno;
  836. args->rmtblkcnt2 = args->rmtblkcnt;
  837. }
  838. /*
  839. * Add the attribute to the leaf block, transitioning to a Btree
  840. * if required.
  841. */
  842. retval = xfs_attr_leaf_add(bp, args);
  843. xfs_da_buf_done(bp);
  844. if (retval == ENOSPC) {
  845. /*
  846. * Promote the attribute list to the Btree format, then
  847. * Commit that transaction so that the node_addname() call
  848. * can manage its own transactions.
  849. */
  850. XFS_BMAP_INIT(args->flist, args->firstblock);
  851. error = xfs_attr_leaf_to_node(args);
  852. if (!error) {
  853. error = xfs_bmap_finish(&args->trans, args->flist,
  854. &committed);
  855. }
  856. if (error) {
  857. ASSERT(committed);
  858. args->trans = NULL;
  859. xfs_bmap_cancel(args->flist);
  860. return(error);
  861. }
  862. /*
  863. * bmap_finish() may have committed the last trans and started
  864. * a new one. We need the inode to be in all transactions.
  865. */
  866. if (committed) {
  867. xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
  868. xfs_trans_ihold(args->trans, dp);
  869. }
  870. /*
  871. * Commit the current trans (including the inode) and start
  872. * a new one.
  873. */
  874. if ((error = xfs_attr_rolltrans(&args->trans, dp)))
  875. return (error);
  876. /*
  877. * Fob the whole rest of the problem off on the Btree code.
  878. */
  879. error = xfs_attr_node_addname(args);
  880. return(error);
  881. }
  882. /*
  883. * Commit the transaction that added the attr name so that
  884. * later routines can manage their own transactions.
  885. */
  886. if ((error = xfs_attr_rolltrans(&args->trans, dp)))
  887. return (error);
  888. /*
  889. * If there was an out-of-line value, allocate the blocks we
  890. * identified for its storage and copy the value. This is done
  891. * after we create the attribute so that we don't overflow the
  892. * maximum size of a transaction and/or hit a deadlock.
  893. */
  894. if (args->rmtblkno > 0) {
  895. error = xfs_attr_rmtval_set(args);
  896. if (error)
  897. return(error);
  898. }
  899. /*
  900. * If this is an atomic rename operation, we must "flip" the
  901. * incomplete flags on the "new" and "old" attribute/value pairs
  902. * so that one disappears and one appears atomically. Then we
  903. * must remove the "old" attribute/value pair.
  904. */
  905. if (args->rename) {
  906. /*
  907. * In a separate transaction, set the incomplete flag on the
  908. * "old" attr and clear the incomplete flag on the "new" attr.
  909. */
  910. error = xfs_attr_leaf_flipflags(args);
  911. if (error)
  912. return(error);
  913. /*
  914. * Dismantle the "old" attribute/value pair by removing
  915. * a "remote" value (if it exists).
  916. */
  917. args->index = args->index2;
  918. args->blkno = args->blkno2;
  919. args->rmtblkno = args->rmtblkno2;
  920. args->rmtblkcnt = args->rmtblkcnt2;
  921. if (args->rmtblkno) {
  922. error = xfs_attr_rmtval_remove(args);
  923. if (error)
  924. return(error);
  925. }
  926. /*
  927. * Read in the block containing the "old" attr, then
  928. * remove the "old" attr from that block (neat, huh!)
  929. */
  930. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1,
  931. &bp, XFS_ATTR_FORK);
  932. if (error)
  933. return(error);
  934. ASSERT(bp != NULL);
  935. (void)xfs_attr_leaf_remove(bp, args);
  936. /*
  937. * If the result is small enough, shrink it all into the inode.
  938. */
  939. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  940. XFS_BMAP_INIT(args->flist, args->firstblock);
  941. error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
  942. /* bp is gone due to xfs_da_shrink_inode */
  943. if (!error) {
  944. error = xfs_bmap_finish(&args->trans,
  945. args->flist,
  946. &committed);
  947. }
  948. if (error) {
  949. ASSERT(committed);
  950. args->trans = NULL;
  951. xfs_bmap_cancel(args->flist);
  952. return(error);
  953. }
  954. /*
  955. * bmap_finish() may have committed the last trans
  956. * and started a new one. We need the inode to be
  957. * in all transactions.
  958. */
  959. if (committed) {
  960. xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
  961. xfs_trans_ihold(args->trans, dp);
  962. }
  963. } else
  964. xfs_da_buf_done(bp);
  965. /*
  966. * Commit the remove and start the next trans in series.
  967. */
  968. error = xfs_attr_rolltrans(&args->trans, dp);
  969. } else if (args->rmtblkno > 0) {
  970. /*
  971. * Added a "remote" value, just clear the incomplete flag.
  972. */
  973. error = xfs_attr_leaf_clearflag(args);
  974. }
  975. return(error);
  976. }
  977. /*
  978. * Remove a name from the leaf attribute list structure
  979. *
  980. * This leaf block cannot have a "remote" value, we only call this routine
  981. * if bmap_one_block() says there is only one block (ie: no remote blks).
  982. */
  983. STATIC int
  984. xfs_attr_leaf_removename(xfs_da_args_t *args)
  985. {
  986. xfs_inode_t *dp;
  987. xfs_dabuf_t *bp;
  988. int error, committed, forkoff;
  989. /*
  990. * Remove the attribute.
  991. */
  992. dp = args->dp;
  993. args->blkno = 0;
  994. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
  995. XFS_ATTR_FORK);
  996. if (error) {
  997. return(error);
  998. }
  999. ASSERT(bp != NULL);
  1000. error = xfs_attr_leaf_lookup_int(bp, args);
  1001. if (error == ENOATTR) {
  1002. xfs_da_brelse(args->trans, bp);
  1003. return(error);
  1004. }
  1005. (void)xfs_attr_leaf_remove(bp, args);
  1006. /*
  1007. * If the result is small enough, shrink it all into the inode.
  1008. */
  1009. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  1010. XFS_BMAP_INIT(args->flist, args->firstblock);
  1011. error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
  1012. /* bp is gone due to xfs_da_shrink_inode */
  1013. if (!error) {
  1014. error = xfs_bmap_finish(&args->trans, args->flist,
  1015. &committed);
  1016. }
  1017. if (error) {
  1018. ASSERT(committed);
  1019. args->trans = NULL;
  1020. xfs_bmap_cancel(args->flist);
  1021. return(error);
  1022. }
  1023. /*
  1024. * bmap_finish() may have committed the last trans and started
  1025. * a new one. We need the inode to be in all transactions.
  1026. */
  1027. if (committed) {
  1028. xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
  1029. xfs_trans_ihold(args->trans, dp);
  1030. }
  1031. } else
  1032. xfs_da_buf_done(bp);
  1033. return(0);
  1034. }
  1035. /*
  1036. * Look up a name in a leaf attribute list structure.
  1037. *
  1038. * This leaf block cannot have a "remote" value, we only call this routine
  1039. * if bmap_one_block() says there is only one block (ie: no remote blks).
  1040. */
  1041. STATIC int
  1042. xfs_attr_leaf_get(xfs_da_args_t *args)
  1043. {
  1044. xfs_dabuf_t *bp;
  1045. int error;
  1046. args->blkno = 0;
  1047. error = xfs_da_read_buf(args->trans, args->dp, args->blkno, -1, &bp,
  1048. XFS_ATTR_FORK);
  1049. if (error)
  1050. return(error);
  1051. ASSERT(bp != NULL);
  1052. error = xfs_attr_leaf_lookup_int(bp, args);
  1053. if (error != EEXIST) {
  1054. xfs_da_brelse(args->trans, bp);
  1055. return(error);
  1056. }
  1057. error = xfs_attr_leaf_getvalue(bp, args);
  1058. xfs_da_brelse(args->trans, bp);
  1059. if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
  1060. error = xfs_attr_rmtval_get(args);
  1061. }
  1062. return(error);
  1063. }
  1064. /*
  1065. * Copy out attribute entries for attr_list(), for leaf attribute lists.
  1066. */
  1067. STATIC int
  1068. xfs_attr_leaf_list(xfs_attr_list_context_t *context)
  1069. {
  1070. xfs_attr_leafblock_t *leaf;
  1071. int error;
  1072. xfs_dabuf_t *bp;
  1073. context->cursor->blkno = 0;
  1074. error = xfs_da_read_buf(NULL, context->dp, 0, -1, &bp, XFS_ATTR_FORK);
  1075. if (error)
  1076. return XFS_ERROR(error);
  1077. ASSERT(bp != NULL);
  1078. leaf = bp->data;
  1079. if (unlikely(be16_to_cpu(leaf->hdr.info.magic) != XFS_ATTR_LEAF_MAGIC)) {
  1080. XFS_CORRUPTION_ERROR("xfs_attr_leaf_list", XFS_ERRLEVEL_LOW,
  1081. context->dp->i_mount, leaf);
  1082. xfs_da_brelse(NULL, bp);
  1083. return XFS_ERROR(EFSCORRUPTED);
  1084. }
  1085. error = xfs_attr_leaf_list_int(bp, context);
  1086. xfs_da_brelse(NULL, bp);
  1087. return XFS_ERROR(error);
  1088. }
  1089. /*========================================================================
  1090. * External routines when attribute list size > XFS_LBSIZE(mp).
  1091. *========================================================================*/
  1092. /*
  1093. * Add a name to a Btree-format attribute list.
  1094. *
  1095. * This will involve walking down the Btree, and may involve splitting
  1096. * leaf nodes and even splitting intermediate nodes up to and including
  1097. * the root node (a special case of an intermediate node).
  1098. *
  1099. * "Remote" attribute values confuse the issue and atomic rename operations
  1100. * add a whole extra layer of confusion on top of that.
  1101. */
  1102. STATIC int
  1103. xfs_attr_node_addname(xfs_da_args_t *args)
  1104. {
  1105. xfs_da_state_t *state;
  1106. xfs_da_state_blk_t *blk;
  1107. xfs_inode_t *dp;
  1108. xfs_mount_t *mp;
  1109. int committed, retval, error;
  1110. /*
  1111. * Fill in bucket of arguments/results/context to carry around.
  1112. */
  1113. dp = args->dp;
  1114. mp = dp->i_mount;
  1115. restart:
  1116. state = xfs_da_state_alloc();
  1117. state->args = args;
  1118. state->mp = mp;
  1119. state->blocksize = state->mp->m_sb.sb_blocksize;
  1120. state->node_ents = state->mp->m_attr_node_ents;
  1121. /*
  1122. * Search to see if name already exists, and get back a pointer
  1123. * to where it should go.
  1124. */
  1125. error = xfs_da_node_lookup_int(state, &retval);
  1126. if (error)
  1127. goto out;
  1128. blk = &state->path.blk[ state->path.active-1 ];
  1129. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1130. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  1131. goto out;
  1132. } else if (retval == EEXIST) {
  1133. if (args->flags & ATTR_CREATE)
  1134. goto out;
  1135. args->rename = 1; /* atomic rename op */
  1136. args->blkno2 = args->blkno; /* set 2nd entry info*/
  1137. args->index2 = args->index;
  1138. args->rmtblkno2 = args->rmtblkno;
  1139. args->rmtblkcnt2 = args->rmtblkcnt;
  1140. args->rmtblkno = 0;
  1141. args->rmtblkcnt = 0;
  1142. }
  1143. retval = xfs_attr_leaf_add(blk->bp, state->args);
  1144. if (retval == ENOSPC) {
  1145. if (state->path.active == 1) {
  1146. /*
  1147. * Its really a single leaf node, but it had
  1148. * out-of-line values so it looked like it *might*
  1149. * have been a b-tree.
  1150. */
  1151. xfs_da_state_free(state);
  1152. XFS_BMAP_INIT(args->flist, args->firstblock);
  1153. error = xfs_attr_leaf_to_node(args);
  1154. if (!error) {
  1155. error = xfs_bmap_finish(&args->trans,
  1156. args->flist,
  1157. &committed);
  1158. }
  1159. if (error) {
  1160. ASSERT(committed);
  1161. args->trans = NULL;
  1162. xfs_bmap_cancel(args->flist);
  1163. goto out;
  1164. }
  1165. /*
  1166. * bmap_finish() may have committed the last trans
  1167. * and started a new one. We need the inode to be
  1168. * in all transactions.
  1169. */
  1170. if (committed) {
  1171. xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
  1172. xfs_trans_ihold(args->trans, dp);
  1173. }
  1174. /*
  1175. * Commit the node conversion and start the next
  1176. * trans in the chain.
  1177. */
  1178. if ((error = xfs_attr_rolltrans(&args->trans, dp)))
  1179. goto out;
  1180. goto restart;
  1181. }
  1182. /*
  1183. * Split as many Btree elements as required.
  1184. * This code tracks the new and old attr's location
  1185. * in the index/blkno/rmtblkno/rmtblkcnt fields and
  1186. * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
  1187. */
  1188. XFS_BMAP_INIT(args->flist, args->firstblock);
  1189. error = xfs_da_split(state);
  1190. if (!error) {
  1191. error = xfs_bmap_finish(&args->trans, args->flist,
  1192. &committed);
  1193. }
  1194. if (error) {
  1195. ASSERT(committed);
  1196. args->trans = NULL;
  1197. xfs_bmap_cancel(args->flist);
  1198. goto out;
  1199. }
  1200. /*
  1201. * bmap_finish() may have committed the last trans and started
  1202. * a new one. We need the inode to be in all transactions.
  1203. */
  1204. if (committed) {
  1205. xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
  1206. xfs_trans_ihold(args->trans, dp);
  1207. }
  1208. } else {
  1209. /*
  1210. * Addition succeeded, update Btree hashvals.
  1211. */
  1212. xfs_da_fixhashpath(state, &state->path);
  1213. }
  1214. /*
  1215. * Kill the state structure, we're done with it and need to
  1216. * allow the buffers to come back later.
  1217. */
  1218. xfs_da_state_free(state);
  1219. state = NULL;
  1220. /*
  1221. * Commit the leaf addition or btree split and start the next
  1222. * trans in the chain.
  1223. */
  1224. if ((error = xfs_attr_rolltrans(&args->trans, dp)))
  1225. goto out;
  1226. /*
  1227. * If there was an out-of-line value, allocate the blocks we
  1228. * identified for its storage and copy the value. This is done
  1229. * after we create the attribute so that we don't overflow the
  1230. * maximum size of a transaction and/or hit a deadlock.
  1231. */
  1232. if (args->rmtblkno > 0) {
  1233. error = xfs_attr_rmtval_set(args);
  1234. if (error)
  1235. return(error);
  1236. }
  1237. /*
  1238. * If this is an atomic rename operation, we must "flip" the
  1239. * incomplete flags on the "new" and "old" attribute/value pairs
  1240. * so that one disappears and one appears atomically. Then we
  1241. * must remove the "old" attribute/value pair.
  1242. */
  1243. if (args->rename) {
  1244. /*
  1245. * In a separate transaction, set the incomplete flag on the
  1246. * "old" attr and clear the incomplete flag on the "new" attr.
  1247. */
  1248. error = xfs_attr_leaf_flipflags(args);
  1249. if (error)
  1250. goto out;
  1251. /*
  1252. * Dismantle the "old" attribute/value pair by removing
  1253. * a "remote" value (if it exists).
  1254. */
  1255. args->index = args->index2;
  1256. args->blkno = args->blkno2;
  1257. args->rmtblkno = args->rmtblkno2;
  1258. args->rmtblkcnt = args->rmtblkcnt2;
  1259. if (args->rmtblkno) {
  1260. error = xfs_attr_rmtval_remove(args);
  1261. if (error)
  1262. return(error);
  1263. }
  1264. /*
  1265. * Re-find the "old" attribute entry after any split ops.
  1266. * The INCOMPLETE flag means that we will find the "old"
  1267. * attr, not the "new" one.
  1268. */
  1269. args->flags |= XFS_ATTR_INCOMPLETE;
  1270. state = xfs_da_state_alloc();
  1271. state->args = args;
  1272. state->mp = mp;
  1273. state->blocksize = state->mp->m_sb.sb_blocksize;
  1274. state->node_ents = state->mp->m_attr_node_ents;
  1275. state->inleaf = 0;
  1276. error = xfs_da_node_lookup_int(state, &retval);
  1277. if (error)
  1278. goto out;
  1279. /*
  1280. * Remove the name and update the hashvals in the tree.
  1281. */
  1282. blk = &state->path.blk[ state->path.active-1 ];
  1283. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1284. error = xfs_attr_leaf_remove(blk->bp, args);
  1285. xfs_da_fixhashpath(state, &state->path);
  1286. /*
  1287. * Check to see if the tree needs to be collapsed.
  1288. */
  1289. if (retval && (state->path.active > 1)) {
  1290. XFS_BMAP_INIT(args->flist, args->firstblock);
  1291. error = xfs_da_join(state);
  1292. if (!error) {
  1293. error = xfs_bmap_finish(&args->trans,
  1294. args->flist,
  1295. &committed);
  1296. }
  1297. if (error) {
  1298. ASSERT(committed);
  1299. args->trans = NULL;
  1300. xfs_bmap_cancel(args->flist);
  1301. goto out;
  1302. }
  1303. /*
  1304. * bmap_finish() may have committed the last trans
  1305. * and started a new one. We need the inode to be
  1306. * in all transactions.
  1307. */
  1308. if (committed) {
  1309. xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
  1310. xfs_trans_ihold(args->trans, dp);
  1311. }
  1312. }
  1313. /*
  1314. * Commit and start the next trans in the chain.
  1315. */
  1316. if ((error = xfs_attr_rolltrans(&args->trans, dp)))
  1317. goto out;
  1318. } else if (args->rmtblkno > 0) {
  1319. /*
  1320. * Added a "remote" value, just clear the incomplete flag.
  1321. */
  1322. error = xfs_attr_leaf_clearflag(args);
  1323. if (error)
  1324. goto out;
  1325. }
  1326. retval = error = 0;
  1327. out:
  1328. if (state)
  1329. xfs_da_state_free(state);
  1330. if (error)
  1331. return(error);
  1332. return(retval);
  1333. }
  1334. /*
  1335. * Remove a name from a B-tree attribute list.
  1336. *
  1337. * This will involve walking down the Btree, and may involve joining
  1338. * leaf nodes and even joining intermediate nodes up to and including
  1339. * the root node (a special case of an intermediate node).
  1340. */
  1341. STATIC int
  1342. xfs_attr_node_removename(xfs_da_args_t *args)
  1343. {
  1344. xfs_da_state_t *state;
  1345. xfs_da_state_blk_t *blk;
  1346. xfs_inode_t *dp;
  1347. xfs_dabuf_t *bp;
  1348. int retval, error, committed, forkoff;
  1349. /*
  1350. * Tie a string around our finger to remind us where we are.
  1351. */
  1352. dp = args->dp;
  1353. state = xfs_da_state_alloc();
  1354. state->args = args;
  1355. state->mp = dp->i_mount;
  1356. state->blocksize = state->mp->m_sb.sb_blocksize;
  1357. state->node_ents = state->mp->m_attr_node_ents;
  1358. /*
  1359. * Search to see if name exists, and get back a pointer to it.
  1360. */
  1361. error = xfs_da_node_lookup_int(state, &retval);
  1362. if (error || (retval != EEXIST)) {
  1363. if (error == 0)
  1364. error = retval;
  1365. goto out;
  1366. }
  1367. /*
  1368. * If there is an out-of-line value, de-allocate the blocks.
  1369. * This is done before we remove the attribute so that we don't
  1370. * overflow the maximum size of a transaction and/or hit a deadlock.
  1371. */
  1372. blk = &state->path.blk[ state->path.active-1 ];
  1373. ASSERT(blk->bp != NULL);
  1374. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1375. if (args->rmtblkno > 0) {
  1376. /*
  1377. * Fill in disk block numbers in the state structure
  1378. * so that we can get the buffers back after we commit
  1379. * several transactions in the following calls.
  1380. */
  1381. error = xfs_attr_fillstate(state);
  1382. if (error)
  1383. goto out;
  1384. /*
  1385. * Mark the attribute as INCOMPLETE, then bunmapi() the
  1386. * remote value.
  1387. */
  1388. error = xfs_attr_leaf_setflag(args);
  1389. if (error)
  1390. goto out;
  1391. error = xfs_attr_rmtval_remove(args);
  1392. if (error)
  1393. goto out;
  1394. /*
  1395. * Refill the state structure with buffers, the prior calls
  1396. * released our buffers.
  1397. */
  1398. error = xfs_attr_refillstate(state);
  1399. if (error)
  1400. goto out;
  1401. }
  1402. /*
  1403. * Remove the name and update the hashvals in the tree.
  1404. */
  1405. blk = &state->path.blk[ state->path.active-1 ];
  1406. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1407. retval = xfs_attr_leaf_remove(blk->bp, args);
  1408. xfs_da_fixhashpath(state, &state->path);
  1409. /*
  1410. * Check to see if the tree needs to be collapsed.
  1411. */
  1412. if (retval && (state->path.active > 1)) {
  1413. XFS_BMAP_INIT(args->flist, args->firstblock);
  1414. error = xfs_da_join(state);
  1415. if (!error) {
  1416. error = xfs_bmap_finish(&args->trans, args->flist,
  1417. &committed);
  1418. }
  1419. if (error) {
  1420. ASSERT(committed);
  1421. args->trans = NULL;
  1422. xfs_bmap_cancel(args->flist);
  1423. goto out;
  1424. }
  1425. /*
  1426. * bmap_finish() may have committed the last trans and started
  1427. * a new one. We need the inode to be in all transactions.
  1428. */
  1429. if (committed) {
  1430. xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
  1431. xfs_trans_ihold(args->trans, dp);
  1432. }
  1433. /*
  1434. * Commit the Btree join operation and start a new trans.
  1435. */
  1436. if ((error = xfs_attr_rolltrans(&args->trans, dp)))
  1437. goto out;
  1438. }
  1439. /*
  1440. * If the result is small enough, push it all into the inode.
  1441. */
  1442. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  1443. /*
  1444. * Have to get rid of the copy of this dabuf in the state.
  1445. */
  1446. ASSERT(state->path.active == 1);
  1447. ASSERT(state->path.blk[0].bp);
  1448. xfs_da_buf_done(state->path.blk[0].bp);
  1449. state->path.blk[0].bp = NULL;
  1450. error = xfs_da_read_buf(args->trans, args->dp, 0, -1, &bp,
  1451. XFS_ATTR_FORK);
  1452. if (error)
  1453. goto out;
  1454. ASSERT(be16_to_cpu(((xfs_attr_leafblock_t *)
  1455. bp->data)->hdr.info.magic)
  1456. == XFS_ATTR_LEAF_MAGIC);
  1457. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  1458. XFS_BMAP_INIT(args->flist, args->firstblock);
  1459. error = xfs_attr_leaf_to_shortform(bp, args, forkoff);
  1460. /* bp is gone due to xfs_da_shrink_inode */
  1461. if (!error) {
  1462. error = xfs_bmap_finish(&args->trans,
  1463. args->flist,
  1464. &committed);
  1465. }
  1466. if (error) {
  1467. ASSERT(committed);
  1468. args->trans = NULL;
  1469. xfs_bmap_cancel(args->flist);
  1470. goto out;
  1471. }
  1472. /*
  1473. * bmap_finish() may have committed the last trans
  1474. * and started a new one. We need the inode to be
  1475. * in all transactions.
  1476. */
  1477. if (committed) {
  1478. xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
  1479. xfs_trans_ihold(args->trans, dp);
  1480. }
  1481. } else
  1482. xfs_da_brelse(args->trans, bp);
  1483. }
  1484. error = 0;
  1485. out:
  1486. xfs_da_state_free(state);
  1487. return(error);
  1488. }
  1489. /*
  1490. * Fill in the disk block numbers in the state structure for the buffers
  1491. * that are attached to the state structure.
  1492. * This is done so that we can quickly reattach ourselves to those buffers
  1493. * after some set of transaction commits have released these buffers.
  1494. */
  1495. STATIC int
  1496. xfs_attr_fillstate(xfs_da_state_t *state)
  1497. {
  1498. xfs_da_state_path_t *path;
  1499. xfs_da_state_blk_t *blk;
  1500. int level;
  1501. /*
  1502. * Roll down the "path" in the state structure, storing the on-disk
  1503. * block number for those buffers in the "path".
  1504. */
  1505. path = &state->path;
  1506. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1507. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1508. if (blk->bp) {
  1509. blk->disk_blkno = xfs_da_blkno(blk->bp);
  1510. xfs_da_buf_done(blk->bp);
  1511. blk->bp = NULL;
  1512. } else {
  1513. blk->disk_blkno = 0;
  1514. }
  1515. }
  1516. /*
  1517. * Roll down the "altpath" in the state structure, storing the on-disk
  1518. * block number for those buffers in the "altpath".
  1519. */
  1520. path = &state->altpath;
  1521. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1522. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1523. if (blk->bp) {
  1524. blk->disk_blkno = xfs_da_blkno(blk->bp);
  1525. xfs_da_buf_done(blk->bp);
  1526. blk->bp = NULL;
  1527. } else {
  1528. blk->disk_blkno = 0;
  1529. }
  1530. }
  1531. return(0);
  1532. }
  1533. /*
  1534. * Reattach the buffers to the state structure based on the disk block
  1535. * numbers stored in the state structure.
  1536. * This is done after some set of transaction commits have released those
  1537. * buffers from our grip.
  1538. */
  1539. STATIC int
  1540. xfs_attr_refillstate(xfs_da_state_t *state)
  1541. {
  1542. xfs_da_state_path_t *path;
  1543. xfs_da_state_blk_t *blk;
  1544. int level, error;
  1545. /*
  1546. * Roll down the "path" in the state structure, storing the on-disk
  1547. * block number for those buffers in the "path".
  1548. */
  1549. path = &state->path;
  1550. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1551. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1552. if (blk->disk_blkno) {
  1553. error = xfs_da_read_buf(state->args->trans,
  1554. state->args->dp,
  1555. blk->blkno, blk->disk_blkno,
  1556. &blk->bp, XFS_ATTR_FORK);
  1557. if (error)
  1558. return(error);
  1559. } else {
  1560. blk->bp = NULL;
  1561. }
  1562. }
  1563. /*
  1564. * Roll down the "altpath" in the state structure, storing the on-disk
  1565. * block number for those buffers in the "altpath".
  1566. */
  1567. path = &state->altpath;
  1568. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1569. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1570. if (blk->disk_blkno) {
  1571. error = xfs_da_read_buf(state->args->trans,
  1572. state->args->dp,
  1573. blk->blkno, blk->disk_blkno,
  1574. &blk->bp, XFS_ATTR_FORK);
  1575. if (error)
  1576. return(error);
  1577. } else {
  1578. blk->bp = NULL;
  1579. }
  1580. }
  1581. return(0);
  1582. }
  1583. /*
  1584. * Look up a filename in a node attribute list.
  1585. *
  1586. * This routine gets called for any attribute fork that has more than one
  1587. * block, ie: both true Btree attr lists and for single-leaf-blocks with
  1588. * "remote" values taking up more blocks.
  1589. */
  1590. STATIC int
  1591. xfs_attr_node_get(xfs_da_args_t *args)
  1592. {
  1593. xfs_da_state_t *state;
  1594. xfs_da_state_blk_t *blk;
  1595. int error, retval;
  1596. int i;
  1597. state = xfs_da_state_alloc();
  1598. state->args = args;
  1599. state->mp = args->dp->i_mount;
  1600. state->blocksize = state->mp->m_sb.sb_blocksize;
  1601. state->node_ents = state->mp->m_attr_node_ents;
  1602. /*
  1603. * Search to see if name exists, and get back a pointer to it.
  1604. */
  1605. error = xfs_da_node_lookup_int(state, &retval);
  1606. if (error) {
  1607. retval = error;
  1608. } else if (retval == EEXIST) {
  1609. blk = &state->path.blk[ state->path.active-1 ];
  1610. ASSERT(blk->bp != NULL);
  1611. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1612. /*
  1613. * Get the value, local or "remote"
  1614. */
  1615. retval = xfs_attr_leaf_getvalue(blk->bp, args);
  1616. if (!retval && (args->rmtblkno > 0)
  1617. && !(args->flags & ATTR_KERNOVAL)) {
  1618. retval = xfs_attr_rmtval_get(args);
  1619. }
  1620. }
  1621. /*
  1622. * If not in a transaction, we have to release all the buffers.
  1623. */
  1624. for (i = 0; i < state->path.active; i++) {
  1625. xfs_da_brelse(args->trans, state->path.blk[i].bp);
  1626. state->path.blk[i].bp = NULL;
  1627. }
  1628. xfs_da_state_free(state);
  1629. return(retval);
  1630. }
  1631. STATIC int /* error */
  1632. xfs_attr_node_list(xfs_attr_list_context_t *context)
  1633. {
  1634. attrlist_cursor_kern_t *cursor;
  1635. xfs_attr_leafblock_t *leaf;
  1636. xfs_da_intnode_t *node;
  1637. xfs_da_node_entry_t *btree;
  1638. int error, i;
  1639. xfs_dabuf_t *bp;
  1640. cursor = context->cursor;
  1641. cursor->initted = 1;
  1642. /*
  1643. * Do all sorts of validation on the passed-in cursor structure.
  1644. * If anything is amiss, ignore the cursor and look up the hashval
  1645. * starting from the btree root.
  1646. */
  1647. bp = NULL;
  1648. if (cursor->blkno > 0) {
  1649. error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
  1650. &bp, XFS_ATTR_FORK);
  1651. if ((error != 0) && (error != EFSCORRUPTED))
  1652. return(error);
  1653. if (bp) {
  1654. node = bp->data;
  1655. switch (be16_to_cpu(node->hdr.info.magic)) {
  1656. case XFS_DA_NODE_MAGIC:
  1657. xfs_attr_trace_l_cn("wrong blk", context, node);
  1658. xfs_da_brelse(NULL, bp);
  1659. bp = NULL;
  1660. break;
  1661. case XFS_ATTR_LEAF_MAGIC:
  1662. leaf = bp->data;
  1663. if (cursor->hashval > be32_to_cpu(leaf->entries[
  1664. be16_to_cpu(leaf->hdr.count)-1].hashval)) {
  1665. xfs_attr_trace_l_cl("wrong blk",
  1666. context, leaf);
  1667. xfs_da_brelse(NULL, bp);
  1668. bp = NULL;
  1669. } else if (cursor->hashval <=
  1670. be32_to_cpu(leaf->entries[0].hashval)) {
  1671. xfs_attr_trace_l_cl("maybe wrong blk",
  1672. context, leaf);
  1673. xfs_da_brelse(NULL, bp);
  1674. bp = NULL;
  1675. }
  1676. break;
  1677. default:
  1678. xfs_attr_trace_l_c("wrong blk - ??", context);
  1679. xfs_da_brelse(NULL, bp);
  1680. bp = NULL;
  1681. }
  1682. }
  1683. }
  1684. /*
  1685. * We did not find what we expected given the cursor's contents,
  1686. * so we start from the top and work down based on the hash value.
  1687. * Note that start of node block is same as start of leaf block.
  1688. */
  1689. if (bp == NULL) {
  1690. cursor->blkno = 0;
  1691. for (;;) {
  1692. error = xfs_da_read_buf(NULL, context->dp,
  1693. cursor->blkno, -1, &bp,
  1694. XFS_ATTR_FORK);
  1695. if (error)
  1696. return(error);
  1697. if (unlikely(bp == NULL)) {
  1698. XFS_ERROR_REPORT("xfs_attr_node_list(2)",
  1699. XFS_ERRLEVEL_LOW,
  1700. context->dp->i_mount);
  1701. return(XFS_ERROR(EFSCORRUPTED));
  1702. }
  1703. node = bp->data;
  1704. if (be16_to_cpu(node->hdr.info.magic)
  1705. == XFS_ATTR_LEAF_MAGIC)
  1706. break;
  1707. if (unlikely(be16_to_cpu(node->hdr.info.magic)
  1708. != XFS_DA_NODE_MAGIC)) {
  1709. XFS_CORRUPTION_ERROR("xfs_attr_node_list(3)",
  1710. XFS_ERRLEVEL_LOW,
  1711. context->dp->i_mount,
  1712. node);
  1713. xfs_da_brelse(NULL, bp);
  1714. return(XFS_ERROR(EFSCORRUPTED));
  1715. }
  1716. btree = node->btree;
  1717. for (i = 0; i < be16_to_cpu(node->hdr.count);
  1718. btree++, i++) {
  1719. if (cursor->hashval
  1720. <= be32_to_cpu(btree->hashval)) {
  1721. cursor->blkno = be32_to_cpu(btree->before);
  1722. xfs_attr_trace_l_cb("descending",
  1723. context, btree);
  1724. break;
  1725. }
  1726. }
  1727. if (i == be16_to_cpu(node->hdr.count)) {
  1728. xfs_da_brelse(NULL, bp);
  1729. return(0);
  1730. }
  1731. xfs_da_brelse(NULL, bp);
  1732. }
  1733. }
  1734. ASSERT(bp != NULL);
  1735. /*
  1736. * Roll upward through the blocks, processing each leaf block in
  1737. * order. As long as there is space in the result buffer, keep
  1738. * adding the information.
  1739. */
  1740. for (;;) {
  1741. leaf = bp->data;
  1742. if (unlikely(be16_to_cpu(leaf->hdr.info.magic)
  1743. != XFS_ATTR_LEAF_MAGIC)) {
  1744. XFS_CORRUPTION_ERROR("xfs_attr_node_list(4)",
  1745. XFS_ERRLEVEL_LOW,
  1746. context->dp->i_mount, leaf);
  1747. xfs_da_brelse(NULL, bp);
  1748. return(XFS_ERROR(EFSCORRUPTED));
  1749. }
  1750. error = xfs_attr_leaf_list_int(bp, context);
  1751. if (error) {
  1752. xfs_da_brelse(NULL, bp);
  1753. return error;
  1754. }
  1755. if (context->seen_enough || leaf->hdr.info.forw == 0)
  1756. break;
  1757. cursor->blkno = be32_to_cpu(leaf->hdr.info.forw);
  1758. xfs_da_brelse(NULL, bp);
  1759. error = xfs_da_read_buf(NULL, context->dp, cursor->blkno, -1,
  1760. &bp, XFS_ATTR_FORK);
  1761. if (error)
  1762. return(error);
  1763. if (unlikely((bp == NULL))) {
  1764. XFS_ERROR_REPORT("xfs_attr_node_list(5)",
  1765. XFS_ERRLEVEL_LOW,
  1766. context->dp->i_mount);
  1767. return(XFS_ERROR(EFSCORRUPTED));
  1768. }
  1769. }
  1770. xfs_da_brelse(NULL, bp);
  1771. return(0);
  1772. }
  1773. /*========================================================================
  1774. * External routines for manipulating out-of-line attribute values.
  1775. *========================================================================*/
  1776. /*
  1777. * Read the value associated with an attribute from the out-of-line buffer
  1778. * that we stored it in.
  1779. */
  1780. int
  1781. xfs_attr_rmtval_get(xfs_da_args_t *args)
  1782. {
  1783. xfs_bmbt_irec_t map[ATTR_RMTVALUE_MAPSIZE];
  1784. xfs_mount_t *mp;
  1785. xfs_daddr_t dblkno;
  1786. xfs_caddr_t dst;
  1787. xfs_buf_t *bp;
  1788. int nmap, error, tmp, valuelen, blkcnt, i;
  1789. xfs_dablk_t lblkno;
  1790. ASSERT(!(args->flags & ATTR_KERNOVAL));
  1791. mp = args->dp->i_mount;
  1792. dst = args->value;
  1793. valuelen = args->valuelen;
  1794. lblkno = args->rmtblkno;
  1795. while (valuelen > 0) {
  1796. nmap = ATTR_RMTVALUE_MAPSIZE;
  1797. error = xfs_bmapi(args->trans, args->dp, (xfs_fileoff_t)lblkno,
  1798. args->rmtblkcnt,
  1799. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  1800. NULL, 0, map, &nmap, NULL, NULL);
  1801. if (error)
  1802. return(error);
  1803. ASSERT(nmap >= 1);
  1804. for (i = 0; (i < nmap) && (valuelen > 0); i++) {
  1805. ASSERT((map[i].br_startblock != DELAYSTARTBLOCK) &&
  1806. (map[i].br_startblock != HOLESTARTBLOCK));
  1807. dblkno = XFS_FSB_TO_DADDR(mp, map[i].br_startblock);
  1808. blkcnt = XFS_FSB_TO_BB(mp, map[i].br_blockcount);
  1809. error = xfs_read_buf(mp, mp->m_ddev_targp, dblkno,
  1810. blkcnt, XFS_BUF_LOCK, &bp);
  1811. if (error)
  1812. return(error);
  1813. tmp = (valuelen < XFS_BUF_SIZE(bp))
  1814. ? valuelen : XFS_BUF_SIZE(bp);
  1815. xfs_biomove(bp, 0, tmp, dst, XFS_B_READ);
  1816. xfs_buf_relse(bp);
  1817. dst += tmp;
  1818. valuelen -= tmp;
  1819. lblkno += map[i].br_blockcount;
  1820. }
  1821. }
  1822. ASSERT(valuelen == 0);
  1823. return(0);
  1824. }
  1825. /*
  1826. * Write the value associated with an attribute into the out-of-line buffer
  1827. * that we have defined for it.
  1828. */
  1829. STATIC int
  1830. xfs_attr_rmtval_set(xfs_da_args_t *args)
  1831. {
  1832. xfs_mount_t *mp;
  1833. xfs_fileoff_t lfileoff;
  1834. xfs_inode_t *dp;
  1835. xfs_bmbt_irec_t map;
  1836. xfs_daddr_t dblkno;
  1837. xfs_caddr_t src;
  1838. xfs_buf_t *bp;
  1839. xfs_dablk_t lblkno;
  1840. int blkcnt, valuelen, nmap, error, tmp, committed;
  1841. dp = args->dp;
  1842. mp = dp->i_mount;
  1843. src = args->value;
  1844. /*
  1845. * Find a "hole" in the attribute address space large enough for
  1846. * us to drop the new attribute's value into.
  1847. */
  1848. blkcnt = XFS_B_TO_FSB(mp, args->valuelen);
  1849. lfileoff = 0;
  1850. error = xfs_bmap_first_unused(args->trans, args->dp, blkcnt, &lfileoff,
  1851. XFS_ATTR_FORK);
  1852. if (error) {
  1853. return(error);
  1854. }
  1855. args->rmtblkno = lblkno = (xfs_dablk_t)lfileoff;
  1856. args->rmtblkcnt = blkcnt;
  1857. /*
  1858. * Roll through the "value", allocating blocks on disk as required.
  1859. */
  1860. while (blkcnt > 0) {
  1861. /*
  1862. * Allocate a single extent, up to the size of the value.
  1863. */
  1864. XFS_BMAP_INIT(args->flist, args->firstblock);
  1865. nmap = 1;
  1866. error = xfs_bmapi(args->trans, dp, (xfs_fileoff_t)lblkno,
  1867. blkcnt,
  1868. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA |
  1869. XFS_BMAPI_WRITE,
  1870. args->firstblock, args->total, &map, &nmap,
  1871. args->flist, NULL);
  1872. if (!error) {
  1873. error = xfs_bmap_finish(&args->trans, args->flist,
  1874. &committed);
  1875. }
  1876. if (error) {
  1877. ASSERT(committed);
  1878. args->trans = NULL;
  1879. xfs_bmap_cancel(args->flist);
  1880. return(error);
  1881. }
  1882. /*
  1883. * bmap_finish() may have committed the last trans and started
  1884. * a new one. We need the inode to be in all transactions.
  1885. */
  1886. if (committed) {
  1887. xfs_trans_ijoin(args->trans, dp, XFS_ILOCK_EXCL);
  1888. xfs_trans_ihold(args->trans, dp);
  1889. }
  1890. ASSERT(nmap == 1);
  1891. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  1892. (map.br_startblock != HOLESTARTBLOCK));
  1893. lblkno += map.br_blockcount;
  1894. blkcnt -= map.br_blockcount;
  1895. /*
  1896. * Start the next trans in the chain.
  1897. */
  1898. if ((error = xfs_attr_rolltrans(&args->trans, dp)))
  1899. return (error);
  1900. }
  1901. /*
  1902. * Roll through the "value", copying the attribute value to the
  1903. * already-allocated blocks. Blocks are written synchronously
  1904. * so that we can know they are all on disk before we turn off
  1905. * the INCOMPLETE flag.
  1906. */
  1907. lblkno = args->rmtblkno;
  1908. valuelen = args->valuelen;
  1909. while (valuelen > 0) {
  1910. /*
  1911. * Try to remember where we decided to put the value.
  1912. */
  1913. XFS_BMAP_INIT(args->flist, args->firstblock);
  1914. nmap = 1;
  1915. error = xfs_bmapi(NULL, dp, (xfs_fileoff_t)lblkno,
  1916. args->rmtblkcnt,
  1917. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  1918. args->firstblock, 0, &map, &nmap,
  1919. NULL, NULL);
  1920. if (error) {
  1921. return(error);
  1922. }
  1923. ASSERT(nmap == 1);
  1924. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  1925. (map.br_startblock != HOLESTARTBLOCK));
  1926. dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
  1927. blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
  1928. bp = xfs_buf_get_flags(mp->m_ddev_targp, dblkno,
  1929. blkcnt, XFS_BUF_LOCK);
  1930. ASSERT(bp);
  1931. ASSERT(!XFS_BUF_GETERROR(bp));
  1932. tmp = (valuelen < XFS_BUF_SIZE(bp)) ? valuelen :
  1933. XFS_BUF_SIZE(bp);
  1934. xfs_biomove(bp, 0, tmp, src, XFS_B_WRITE);
  1935. if (tmp < XFS_BUF_SIZE(bp))
  1936. xfs_biozero(bp, tmp, XFS_BUF_SIZE(bp) - tmp);
  1937. if ((error = xfs_bwrite(mp, bp))) {/* GROT: NOTE: synchronous write */
  1938. return (error);
  1939. }
  1940. src += tmp;
  1941. valuelen -= tmp;
  1942. lblkno += map.br_blockcount;
  1943. }
  1944. ASSERT(valuelen == 0);
  1945. return(0);
  1946. }
  1947. /*
  1948. * Remove the value associated with an attribute by deleting the
  1949. * out-of-line buffer that it is stored on.
  1950. */
  1951. STATIC int
  1952. xfs_attr_rmtval_remove(xfs_da_args_t *args)
  1953. {
  1954. xfs_mount_t *mp;
  1955. xfs_bmbt_irec_t map;
  1956. xfs_buf_t *bp;
  1957. xfs_daddr_t dblkno;
  1958. xfs_dablk_t lblkno;
  1959. int valuelen, blkcnt, nmap, error, done, committed;
  1960. mp = args->dp->i_mount;
  1961. /*
  1962. * Roll through the "value", invalidating the attribute value's
  1963. * blocks.
  1964. */
  1965. lblkno = args->rmtblkno;
  1966. valuelen = args->rmtblkcnt;
  1967. while (valuelen > 0) {
  1968. /*
  1969. * Try to remember where we decided to put the value.
  1970. */
  1971. XFS_BMAP_INIT(args->flist, args->firstblock);
  1972. nmap = 1;
  1973. error = xfs_bmapi(NULL, args->dp, (xfs_fileoff_t)lblkno,
  1974. args->rmtblkcnt,
  1975. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  1976. args->firstblock, 0, &map, &nmap,
  1977. args->flist, NULL);
  1978. if (error) {
  1979. return(error);
  1980. }
  1981. ASSERT(nmap == 1);
  1982. ASSERT((map.br_startblock != DELAYSTARTBLOCK) &&
  1983. (map.br_startblock != HOLESTARTBLOCK));
  1984. dblkno = XFS_FSB_TO_DADDR(mp, map.br_startblock),
  1985. blkcnt = XFS_FSB_TO_BB(mp, map.br_blockcount);
  1986. /*
  1987. * If the "remote" value is in the cache, remove it.
  1988. */
  1989. bp = xfs_incore(mp->m_ddev_targp, dblkno, blkcnt,
  1990. XFS_INCORE_TRYLOCK);
  1991. if (bp) {
  1992. XFS_BUF_STALE(bp);
  1993. XFS_BUF_UNDELAYWRITE(bp);
  1994. xfs_buf_relse(bp);
  1995. bp = NULL;
  1996. }
  1997. valuelen -= map.br_blockcount;
  1998. lblkno += map.br_blockcount;
  1999. }
  2000. /*
  2001. * Keep de-allocating extents until the remote-value region is gone.
  2002. */
  2003. lblkno = args->rmtblkno;
  2004. blkcnt = args->rmtblkcnt;
  2005. done = 0;
  2006. while (!done) {
  2007. XFS_BMAP_INIT(args->flist, args->firstblock);
  2008. error = xfs_bunmapi(args->trans, args->dp, lblkno, blkcnt,
  2009. XFS_BMAPI_ATTRFORK | XFS_BMAPI_METADATA,
  2010. 1, args->firstblock, args->flist,
  2011. NULL, &done);
  2012. if (!error) {
  2013. error = xfs_bmap_finish(&args->trans, args->flist,
  2014. &committed);
  2015. }
  2016. if (error) {
  2017. ASSERT(committed);
  2018. args->trans = NULL;
  2019. xfs_bmap_cancel(args->flist);
  2020. return(error);
  2021. }
  2022. /*
  2023. * bmap_finish() may have committed the last trans and started
  2024. * a new one. We need the inode to be in all transactions.
  2025. */
  2026. if (committed) {
  2027. xfs_trans_ijoin(args->trans, args->dp, XFS_ILOCK_EXCL);
  2028. xfs_trans_ihold(args->trans, args->dp);
  2029. }
  2030. /*
  2031. * Close out trans and start the next one in the chain.
  2032. */
  2033. if ((error = xfs_attr_rolltrans(&args->trans, args->dp)))
  2034. return (error);
  2035. }
  2036. return(0);
  2037. }
  2038. #if defined(XFS_ATTR_TRACE)
  2039. /*
  2040. * Add a trace buffer entry for an attr_list context structure.
  2041. */
  2042. void
  2043. xfs_attr_trace_l_c(char *where, struct xfs_attr_list_context *context)
  2044. {
  2045. xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_C, where,
  2046. (__psunsigned_t)context->dp,
  2047. (__psunsigned_t)context->cursor->hashval,
  2048. (__psunsigned_t)context->cursor->blkno,
  2049. (__psunsigned_t)context->cursor->offset,
  2050. (__psunsigned_t)context->alist,
  2051. (__psunsigned_t)context->bufsize,
  2052. (__psunsigned_t)context->count,
  2053. (__psunsigned_t)context->firstu,
  2054. (__psunsigned_t)
  2055. ((context->count > 0) &&
  2056. !(context->flags & (ATTR_KERNAMELS|ATTR_KERNOVAL)))
  2057. ? (ATTR_ENTRY(context->alist,
  2058. context->count-1)->a_valuelen)
  2059. : 0,
  2060. (__psunsigned_t)context->dupcnt,
  2061. (__psunsigned_t)context->flags,
  2062. (__psunsigned_t)NULL,
  2063. (__psunsigned_t)NULL,
  2064. (__psunsigned_t)NULL);
  2065. }
  2066. /*
  2067. * Add a trace buffer entry for a context structure and a Btree node.
  2068. */
  2069. void
  2070. xfs_attr_trace_l_cn(char *where, struct xfs_attr_list_context *context,
  2071. struct xfs_da_intnode *node)
  2072. {
  2073. xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CN, where,
  2074. (__psunsigned_t)context->dp,
  2075. (__psunsigned_t)context->cursor->hashval,
  2076. (__psunsigned_t)context->cursor->blkno,
  2077. (__psunsigned_t)context->cursor->offset,
  2078. (__psunsigned_t)context->alist,
  2079. (__psunsigned_t)context->bufsize,
  2080. (__psunsigned_t)context->count,
  2081. (__psunsigned_t)context->firstu,
  2082. (__psunsigned_t)
  2083. ((context->count > 0) &&
  2084. !(context->flags & (ATTR_KERNAMELS|ATTR_KERNOVAL)))
  2085. ? (ATTR_ENTRY(context->alist,
  2086. context->count-1)->a_valuelen)
  2087. : 0,
  2088. (__psunsigned_t)context->dupcnt,
  2089. (__psunsigned_t)context->flags,
  2090. (__psunsigned_t)be16_to_cpu(node->hdr.count),
  2091. (__psunsigned_t)be32_to_cpu(node->btree[0].hashval),
  2092. (__psunsigned_t)be32_to_cpu(node->btree[
  2093. be16_to_cpu(node->hdr.count)-1].hashval));
  2094. }
  2095. /*
  2096. * Add a trace buffer entry for a context structure and a Btree element.
  2097. */
  2098. void
  2099. xfs_attr_trace_l_cb(char *where, struct xfs_attr_list_context *context,
  2100. struct xfs_da_node_entry *btree)
  2101. {
  2102. xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CB, where,
  2103. (__psunsigned_t)context->dp,
  2104. (__psunsigned_t)context->cursor->hashval,
  2105. (__psunsigned_t)context->cursor->blkno,
  2106. (__psunsigned_t)context->cursor->offset,
  2107. (__psunsigned_t)context->alist,
  2108. (__psunsigned_t)context->bufsize,
  2109. (__psunsigned_t)context->count,
  2110. (__psunsigned_t)context->firstu,
  2111. (__psunsigned_t)
  2112. ((context->count > 0) &&
  2113. !(context->flags & (ATTR_KERNAMELS|ATTR_KERNOVAL)))
  2114. ? (ATTR_ENTRY(context->alist,
  2115. context->count-1)->a_valuelen)
  2116. : 0,
  2117. (__psunsigned_t)context->dupcnt,
  2118. (__psunsigned_t)context->flags,
  2119. (__psunsigned_t)be32_to_cpu(btree->hashval),
  2120. (__psunsigned_t)be32_to_cpu(btree->before),
  2121. (__psunsigned_t)NULL);
  2122. }
  2123. /*
  2124. * Add a trace buffer entry for a context structure and a leaf block.
  2125. */
  2126. void
  2127. xfs_attr_trace_l_cl(char *where, struct xfs_attr_list_context *context,
  2128. struct xfs_attr_leafblock *leaf)
  2129. {
  2130. xfs_attr_trace_enter(XFS_ATTR_KTRACE_L_CL, where,
  2131. (__psunsigned_t)context->dp,
  2132. (__psunsigned_t)context->cursor->hashval,
  2133. (__psunsigned_t)context->cursor->blkno,
  2134. (__psunsigned_t)context->cursor->offset,
  2135. (__psunsigned_t)context->alist,
  2136. (__psunsigned_t)context->bufsize,
  2137. (__psunsigned_t)context->count,
  2138. (__psunsigned_t)context->firstu,
  2139. (__psunsigned_t)
  2140. ((context->count > 0) &&
  2141. !(context->flags & (ATTR_KERNAMELS|ATTR_KERNOVAL)))
  2142. ? (ATTR_ENTRY(context->alist,
  2143. context->count-1)->a_valuelen)
  2144. : 0,
  2145. (__psunsigned_t)context->dupcnt,
  2146. (__psunsigned_t)context->flags,
  2147. (__psunsigned_t)be16_to_cpu(leaf->hdr.count),
  2148. (__psunsigned_t)be32_to_cpu(leaf->entries[0].hashval),
  2149. (__psunsigned_t)be32_to_cpu(leaf->entries[
  2150. be16_to_cpu(leaf->hdr.count)-1].hashval));
  2151. }
  2152. /*
  2153. * Add a trace buffer entry for the arguments given to the routine,
  2154. * generic form.
  2155. */
  2156. void
  2157. xfs_attr_trace_enter(int type, char *where,
  2158. __psunsigned_t a2, __psunsigned_t a3,
  2159. __psunsigned_t a4, __psunsigned_t a5,
  2160. __psunsigned_t a6, __psunsigned_t a7,
  2161. __psunsigned_t a8, __psunsigned_t a9,
  2162. __psunsigned_t a10, __psunsigned_t a11,
  2163. __psunsigned_t a12, __psunsigned_t a13,
  2164. __psunsigned_t a14, __psunsigned_t a15)
  2165. {
  2166. ASSERT(xfs_attr_trace_buf);
  2167. ktrace_enter(xfs_attr_trace_buf, (void *)((__psunsigned_t)type),
  2168. (void *)where,
  2169. (void *)a2, (void *)a3, (void *)a4,
  2170. (void *)a5, (void *)a6, (void *)a7,
  2171. (void *)a8, (void *)a9, (void *)a10,
  2172. (void *)a11, (void *)a12, (void *)a13,
  2173. (void *)a14, (void *)a15);
  2174. }
  2175. #endif /* XFS_ATTR_TRACE */
  2176. /*========================================================================
  2177. * System (pseudo) namespace attribute interface routines.
  2178. *========================================================================*/
  2179. STATIC int
  2180. posix_acl_access_set(
  2181. bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
  2182. {
  2183. return xfs_acl_vset(vp, data, size, _ACL_TYPE_ACCESS);
  2184. }
  2185. STATIC int
  2186. posix_acl_access_remove(
  2187. bhv_vnode_t *vp, char *name, int xflags)
  2188. {
  2189. return xfs_acl_vremove(vp, _ACL_TYPE_ACCESS);
  2190. }
  2191. STATIC int
  2192. posix_acl_access_get(
  2193. bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
  2194. {
  2195. return xfs_acl_vget(vp, data, size, _ACL_TYPE_ACCESS);
  2196. }
  2197. STATIC int
  2198. posix_acl_access_exists(
  2199. bhv_vnode_t *vp)
  2200. {
  2201. return xfs_acl_vhasacl_access(vp);
  2202. }
  2203. STATIC int
  2204. posix_acl_default_set(
  2205. bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
  2206. {
  2207. return xfs_acl_vset(vp, data, size, _ACL_TYPE_DEFAULT);
  2208. }
  2209. STATIC int
  2210. posix_acl_default_get(
  2211. bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
  2212. {
  2213. return xfs_acl_vget(vp, data, size, _ACL_TYPE_DEFAULT);
  2214. }
  2215. STATIC int
  2216. posix_acl_default_remove(
  2217. bhv_vnode_t *vp, char *name, int xflags)
  2218. {
  2219. return xfs_acl_vremove(vp, _ACL_TYPE_DEFAULT);
  2220. }
  2221. STATIC int
  2222. posix_acl_default_exists(
  2223. bhv_vnode_t *vp)
  2224. {
  2225. return xfs_acl_vhasacl_default(vp);
  2226. }
  2227. static struct attrnames posix_acl_access = {
  2228. .attr_name = "posix_acl_access",
  2229. .attr_namelen = sizeof("posix_acl_access") - 1,
  2230. .attr_get = posix_acl_access_get,
  2231. .attr_set = posix_acl_access_set,
  2232. .attr_remove = posix_acl_access_remove,
  2233. .attr_exists = posix_acl_access_exists,
  2234. };
  2235. static struct attrnames posix_acl_default = {
  2236. .attr_name = "posix_acl_default",
  2237. .attr_namelen = sizeof("posix_acl_default") - 1,
  2238. .attr_get = posix_acl_default_get,
  2239. .attr_set = posix_acl_default_set,
  2240. .attr_remove = posix_acl_default_remove,
  2241. .attr_exists = posix_acl_default_exists,
  2242. };
  2243. static struct attrnames *attr_system_names[] =
  2244. { &posix_acl_access, &posix_acl_default };
  2245. /*========================================================================
  2246. * Namespace-prefix-style attribute name interface routines.
  2247. *========================================================================*/
  2248. STATIC int
  2249. attr_generic_set(
  2250. bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
  2251. {
  2252. return -bhv_vop_attr_set(vp, name, data, size, xflags, NULL);
  2253. }
  2254. STATIC int
  2255. attr_generic_get(
  2256. bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
  2257. {
  2258. int error, asize = size;
  2259. error = bhv_vop_attr_get(vp, name, data, &asize, xflags, NULL);
  2260. if (!error)
  2261. return asize;
  2262. return -error;
  2263. }
  2264. STATIC int
  2265. attr_generic_remove(
  2266. bhv_vnode_t *vp, char *name, int xflags)
  2267. {
  2268. return -bhv_vop_attr_remove(vp, name, xflags, NULL);
  2269. }
  2270. STATIC int
  2271. attr_generic_listadd(
  2272. attrnames_t *prefix,
  2273. attrnames_t *namesp,
  2274. void *data,
  2275. size_t size,
  2276. ssize_t *result)
  2277. {
  2278. char *p = data + *result;
  2279. *result += prefix->attr_namelen;
  2280. *result += namesp->attr_namelen + 1;
  2281. if (!size)
  2282. return 0;
  2283. if (*result > size)
  2284. return -ERANGE;
  2285. strcpy(p, prefix->attr_name);
  2286. p += prefix->attr_namelen;
  2287. strcpy(p, namesp->attr_name);
  2288. p += namesp->attr_namelen + 1;
  2289. return 0;
  2290. }
  2291. STATIC int
  2292. attr_system_list(
  2293. bhv_vnode_t *vp,
  2294. void *data,
  2295. size_t size,
  2296. ssize_t *result)
  2297. {
  2298. attrnames_t *namesp;
  2299. int i, error = 0;
  2300. for (i = 0; i < ATTR_SYSCOUNT; i++) {
  2301. namesp = attr_system_names[i];
  2302. if (!namesp->attr_exists || !namesp->attr_exists(vp))
  2303. continue;
  2304. error = attr_generic_listadd(&attr_system, namesp,
  2305. data, size, result);
  2306. if (error)
  2307. break;
  2308. }
  2309. return error;
  2310. }
  2311. int
  2312. attr_generic_list(
  2313. bhv_vnode_t *vp, void *data, size_t size, int xflags, ssize_t *result)
  2314. {
  2315. attrlist_cursor_kern_t cursor = { 0 };
  2316. int error;
  2317. error = bhv_vop_attr_list(vp, data, size, xflags, &cursor, NULL);
  2318. if (error > 0)
  2319. return -error;
  2320. *result = -error;
  2321. return attr_system_list(vp, data, size, result);
  2322. }
  2323. attrnames_t *
  2324. attr_lookup_namespace(
  2325. char *name,
  2326. struct attrnames **names,
  2327. int nnames)
  2328. {
  2329. int i;
  2330. for (i = 0; i < nnames; i++)
  2331. if (!strncmp(name, names[i]->attr_name, names[i]->attr_namelen))
  2332. return names[i];
  2333. return NULL;
  2334. }
  2335. /*
  2336. * Some checks to prevent people abusing EAs to get over quota:
  2337. * - Don't allow modifying user EAs on devices/symlinks;
  2338. * - Don't allow modifying user EAs if sticky bit set;
  2339. */
  2340. STATIC int
  2341. attr_user_capable(
  2342. bhv_vnode_t *vp,
  2343. cred_t *cred)
  2344. {
  2345. struct inode *inode = vn_to_inode(vp);
  2346. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  2347. return -EPERM;
  2348. if (!S_ISREG(inode->i_mode) && !S_ISDIR(inode->i_mode) &&
  2349. !capable(CAP_SYS_ADMIN))
  2350. return -EPERM;
  2351. if (S_ISDIR(inode->i_mode) && (inode->i_mode & S_ISVTX) &&
  2352. (current_fsuid(cred) != inode->i_uid) && !capable(CAP_FOWNER))
  2353. return -EPERM;
  2354. return 0;
  2355. }
  2356. STATIC int
  2357. attr_trusted_capable(
  2358. bhv_vnode_t *vp,
  2359. cred_t *cred)
  2360. {
  2361. struct inode *inode = vn_to_inode(vp);
  2362. if (IS_IMMUTABLE(inode) || IS_APPEND(inode))
  2363. return -EPERM;
  2364. if (!capable(CAP_SYS_ADMIN))
  2365. return -EPERM;
  2366. return 0;
  2367. }
  2368. STATIC int
  2369. attr_secure_capable(
  2370. bhv_vnode_t *vp,
  2371. cred_t *cred)
  2372. {
  2373. return -ENOSECURITY;
  2374. }
  2375. STATIC int
  2376. attr_system_set(
  2377. bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
  2378. {
  2379. attrnames_t *namesp;
  2380. int error;
  2381. if (xflags & ATTR_CREATE)
  2382. return -EINVAL;
  2383. namesp = attr_lookup_namespace(name, attr_system_names, ATTR_SYSCOUNT);
  2384. if (!namesp)
  2385. return -EOPNOTSUPP;
  2386. error = namesp->attr_set(vp, name, data, size, xflags);
  2387. if (!error)
  2388. error = vn_revalidate(vp);
  2389. return error;
  2390. }
  2391. STATIC int
  2392. attr_system_get(
  2393. bhv_vnode_t *vp, char *name, void *data, size_t size, int xflags)
  2394. {
  2395. attrnames_t *namesp;
  2396. namesp = attr_lookup_namespace(name, attr_system_names, ATTR_SYSCOUNT);
  2397. if (!namesp)
  2398. return -EOPNOTSUPP;
  2399. return namesp->attr_get(vp, name, data, size, xflags);
  2400. }
  2401. STATIC int
  2402. attr_system_remove(
  2403. bhv_vnode_t *vp, char *name, int xflags)
  2404. {
  2405. attrnames_t *namesp;
  2406. namesp = attr_lookup_namespace(name, attr_system_names, ATTR_SYSCOUNT);
  2407. if (!namesp)
  2408. return -EOPNOTSUPP;
  2409. return namesp->attr_remove(vp, name, xflags);
  2410. }
  2411. struct attrnames attr_system = {
  2412. .attr_name = "system.",
  2413. .attr_namelen = sizeof("system.") - 1,
  2414. .attr_flag = ATTR_SYSTEM,
  2415. .attr_get = attr_system_get,
  2416. .attr_set = attr_system_set,
  2417. .attr_remove = attr_system_remove,
  2418. .attr_capable = (attrcapable_t)fs_noerr,
  2419. };
  2420. struct attrnames attr_trusted = {
  2421. .attr_name = "trusted.",
  2422. .attr_namelen = sizeof("trusted.") - 1,
  2423. .attr_flag = ATTR_ROOT,
  2424. .attr_get = attr_generic_get,
  2425. .attr_set = attr_generic_set,
  2426. .attr_remove = attr_generic_remove,
  2427. .attr_capable = attr_trusted_capable,
  2428. };
  2429. struct attrnames attr_secure = {
  2430. .attr_name = "security.",
  2431. .attr_namelen = sizeof("security.") - 1,
  2432. .attr_flag = ATTR_SECURE,
  2433. .attr_get = attr_generic_get,
  2434. .attr_set = attr_generic_set,
  2435. .attr_remove = attr_generic_remove,
  2436. .attr_capable = attr_secure_capable,
  2437. };
  2438. struct attrnames attr_user = {
  2439. .attr_name = "user.",
  2440. .attr_namelen = sizeof("user.") - 1,
  2441. .attr_get = attr_generic_get,
  2442. .attr_set = attr_generic_set,
  2443. .attr_remove = attr_generic_remove,
  2444. .attr_capable = attr_user_capable,
  2445. };
  2446. struct attrnames *attr_namespaces[] =
  2447. { &attr_system, &attr_trusted, &attr_secure, &attr_user };