xfs_attr.c 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537
  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 "xfs.h"
  19. #include "xfs_fs.h"
  20. #include "xfs_format.h"
  21. #include "xfs_bit.h"
  22. #include "xfs_log.h"
  23. #include "xfs_trans.h"
  24. #include "xfs_trans_priv.h"
  25. #include "xfs_sb.h"
  26. #include "xfs_ag.h"
  27. #include "xfs_mount.h"
  28. #include "xfs_da_btree.h"
  29. #include "xfs_bmap_btree.h"
  30. #include "xfs_attr_sf.h"
  31. #include "xfs_dinode.h"
  32. #include "xfs_inode.h"
  33. #include "xfs_alloc.h"
  34. #include "xfs_inode_item.h"
  35. #include "xfs_bmap.h"
  36. #include "xfs_attr.h"
  37. #include "xfs_attr_leaf.h"
  38. #include "xfs_attr_remote.h"
  39. #include "xfs_error.h"
  40. #include "xfs_quota.h"
  41. #include "xfs_trans_space.h"
  42. #include "xfs_vnodeops.h"
  43. #include "xfs_trace.h"
  44. /*
  45. * xfs_attr.c
  46. *
  47. * Provide the external interfaces to manage attribute lists.
  48. */
  49. /*========================================================================
  50. * Function prototypes for the kernel.
  51. *========================================================================*/
  52. /*
  53. * Internal routines when attribute list fits inside the inode.
  54. */
  55. STATIC int xfs_attr_shortform_addname(xfs_da_args_t *args);
  56. /*
  57. * Internal routines when attribute list is one block.
  58. */
  59. STATIC int xfs_attr_leaf_get(xfs_da_args_t *args);
  60. STATIC int xfs_attr_leaf_addname(xfs_da_args_t *args);
  61. STATIC int xfs_attr_leaf_removename(xfs_da_args_t *args);
  62. /*
  63. * Internal routines when attribute list is more than one block.
  64. */
  65. STATIC int xfs_attr_node_get(xfs_da_args_t *args);
  66. STATIC int xfs_attr_node_addname(xfs_da_args_t *args);
  67. STATIC int xfs_attr_node_removename(xfs_da_args_t *args);
  68. STATIC int xfs_attr_fillstate(xfs_da_state_t *state);
  69. STATIC int xfs_attr_refillstate(xfs_da_state_t *state);
  70. STATIC int
  71. xfs_attr_name_to_xname(
  72. struct xfs_name *xname,
  73. const unsigned char *aname)
  74. {
  75. if (!aname)
  76. return EINVAL;
  77. xname->name = aname;
  78. xname->len = strlen((char *)aname);
  79. if (xname->len >= MAXNAMELEN)
  80. return EFAULT; /* match IRIX behaviour */
  81. return 0;
  82. }
  83. int
  84. xfs_inode_hasattr(
  85. struct xfs_inode *ip)
  86. {
  87. if (!XFS_IFORK_Q(ip) ||
  88. (ip->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS &&
  89. ip->i_d.di_anextents == 0))
  90. return 0;
  91. return 1;
  92. }
  93. /*========================================================================
  94. * Overall external interface routines.
  95. *========================================================================*/
  96. STATIC int
  97. xfs_attr_get_int(
  98. struct xfs_inode *ip,
  99. struct xfs_name *name,
  100. unsigned char *value,
  101. int *valuelenp,
  102. int flags)
  103. {
  104. xfs_da_args_t args;
  105. int error;
  106. if (!xfs_inode_hasattr(ip))
  107. return ENOATTR;
  108. /*
  109. * Fill in the arg structure for this request.
  110. */
  111. memset((char *)&args, 0, sizeof(args));
  112. args.name = name->name;
  113. args.namelen = name->len;
  114. args.value = value;
  115. args.valuelen = *valuelenp;
  116. args.flags = flags;
  117. args.hashval = xfs_da_hashname(args.name, args.namelen);
  118. args.dp = ip;
  119. args.whichfork = XFS_ATTR_FORK;
  120. /*
  121. * Decide on what work routines to call based on the inode size.
  122. */
  123. 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(
  140. xfs_inode_t *ip,
  141. const unsigned char *name,
  142. unsigned char *value,
  143. int *valuelenp,
  144. int flags)
  145. {
  146. int error;
  147. struct xfs_name xname;
  148. XFS_STATS_INC(xs_attr_get);
  149. if (XFS_FORCED_SHUTDOWN(ip->i_mount))
  150. return(EIO);
  151. error = xfs_attr_name_to_xname(&xname, name);
  152. if (error)
  153. return error;
  154. xfs_ilock(ip, XFS_ILOCK_SHARED);
  155. error = xfs_attr_get_int(ip, &xname, value, valuelenp, flags);
  156. xfs_iunlock(ip, XFS_ILOCK_SHARED);
  157. return(error);
  158. }
  159. /*
  160. * Calculate how many blocks we need for the new attribute,
  161. */
  162. STATIC int
  163. xfs_attr_calc_size(
  164. struct xfs_inode *ip,
  165. int namelen,
  166. int valuelen,
  167. int *local)
  168. {
  169. struct xfs_mount *mp = ip->i_mount;
  170. int size;
  171. int nblks;
  172. /*
  173. * Determine space new attribute will use, and if it would be
  174. * "local" or "remote" (note: local != inline).
  175. */
  176. size = xfs_attr_leaf_newentsize(namelen, valuelen,
  177. mp->m_sb.sb_blocksize, local);
  178. nblks = XFS_DAENTER_SPACE_RES(mp, XFS_ATTR_FORK);
  179. if (*local) {
  180. if (size > (mp->m_sb.sb_blocksize >> 1)) {
  181. /* Double split possible */
  182. nblks *= 2;
  183. }
  184. } else {
  185. /*
  186. * Out of line attribute, cannot double split, but
  187. * make room for the attribute value itself.
  188. */
  189. uint dblocks = XFS_B_TO_FSB(mp, valuelen);
  190. nblks += dblocks;
  191. nblks += XFS_NEXTENTADD_SPACE_RES(mp, dblocks, XFS_ATTR_FORK);
  192. }
  193. return nblks;
  194. }
  195. STATIC int
  196. xfs_attr_set_int(
  197. struct xfs_inode *dp,
  198. struct xfs_name *name,
  199. unsigned char *value,
  200. int valuelen,
  201. int flags)
  202. {
  203. xfs_da_args_t args;
  204. xfs_fsblock_t firstblock;
  205. xfs_bmap_free_t flist;
  206. int error, err2, committed;
  207. xfs_mount_t *mp = dp->i_mount;
  208. int rsvd = (flags & ATTR_ROOT) != 0;
  209. int local;
  210. /*
  211. * Attach the dquots to the inode.
  212. */
  213. error = xfs_qm_dqattach(dp, 0);
  214. if (error)
  215. return error;
  216. /*
  217. * If the inode doesn't have an attribute fork, add one.
  218. * (inode must not be locked when we call this routine)
  219. */
  220. if (XFS_IFORK_Q(dp) == 0) {
  221. int sf_size = sizeof(xfs_attr_sf_hdr_t) +
  222. XFS_ATTR_SF_ENTSIZE_BYNAME(name->len, valuelen);
  223. if ((error = xfs_bmap_add_attrfork(dp, sf_size, rsvd)))
  224. return(error);
  225. }
  226. /*
  227. * Fill in the arg structure for this request.
  228. */
  229. memset((char *)&args, 0, sizeof(args));
  230. args.name = name->name;
  231. args.namelen = name->len;
  232. args.value = value;
  233. args.valuelen = valuelen;
  234. args.flags = flags;
  235. args.hashval = xfs_da_hashname(args.name, args.namelen);
  236. args.dp = dp;
  237. args.firstblock = &firstblock;
  238. args.flist = &flist;
  239. args.whichfork = XFS_ATTR_FORK;
  240. args.op_flags = XFS_DA_OP_ADDNAME | XFS_DA_OP_OKNOENT;
  241. /* Size is now blocks for attribute data */
  242. args.total = xfs_attr_calc_size(dp, name->len, valuelen, &local);
  243. /*
  244. * Start our first transaction of the day.
  245. *
  246. * All future transactions during this code must be "chained" off
  247. * this one via the trans_dup() call. All transactions will contain
  248. * the inode, and the inode will always be marked with trans_ihold().
  249. * Since the inode will be locked in all transactions, we must log
  250. * the inode in every transaction to let it float upward through
  251. * the log.
  252. */
  253. args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_SET);
  254. /*
  255. * Root fork attributes can use reserved data blocks for this
  256. * operation if necessary
  257. */
  258. if (rsvd)
  259. args.trans->t_flags |= XFS_TRANS_RESERVE;
  260. error = xfs_trans_reserve(args.trans, args.total,
  261. XFS_ATTRSETM_LOG_RES(mp) +
  262. XFS_ATTRSETRT_LOG_RES(mp) * args.total,
  263. 0, XFS_TRANS_PERM_LOG_RES,
  264. XFS_ATTRSET_LOG_COUNT);
  265. if (error) {
  266. xfs_trans_cancel(args.trans, 0);
  267. return(error);
  268. }
  269. xfs_ilock(dp, XFS_ILOCK_EXCL);
  270. error = xfs_trans_reserve_quota_nblks(args.trans, dp, args.total, 0,
  271. rsvd ? XFS_QMOPT_RES_REGBLKS | XFS_QMOPT_FORCE_RES :
  272. XFS_QMOPT_RES_REGBLKS);
  273. if (error) {
  274. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  275. xfs_trans_cancel(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  276. return (error);
  277. }
  278. xfs_trans_ijoin(args.trans, dp, 0);
  279. /*
  280. * If the attribute list is non-existent or a shortform list,
  281. * upgrade it to a single-leaf-block attribute list.
  282. */
  283. if ((dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) ||
  284. ((dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS) &&
  285. (dp->i_d.di_anextents == 0))) {
  286. /*
  287. * Build initial attribute list (if required).
  288. */
  289. if (dp->i_d.di_aformat == XFS_DINODE_FMT_EXTENTS)
  290. xfs_attr_shortform_create(&args);
  291. /*
  292. * Try to add the attr to the attribute list in
  293. * the inode.
  294. */
  295. error = xfs_attr_shortform_addname(&args);
  296. if (error != ENOSPC) {
  297. /*
  298. * Commit the shortform mods, and we're done.
  299. * NOTE: this is also the error path (EEXIST, etc).
  300. */
  301. ASSERT(args.trans != NULL);
  302. /*
  303. * If this is a synchronous mount, make sure that
  304. * the transaction goes to disk before returning
  305. * to the user.
  306. */
  307. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  308. xfs_trans_set_sync(args.trans);
  309. }
  310. if (!error && (flags & ATTR_KERNOTIME) == 0) {
  311. xfs_trans_ichgtime(args.trans, dp,
  312. XFS_ICHGTIME_CHG);
  313. }
  314. err2 = xfs_trans_commit(args.trans,
  315. XFS_TRANS_RELEASE_LOG_RES);
  316. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  317. return(error == 0 ? err2 : error);
  318. }
  319. /*
  320. * It won't fit in the shortform, transform to a leaf block.
  321. * GROT: another possible req'mt for a double-split btree op.
  322. */
  323. xfs_bmap_init(args.flist, args.firstblock);
  324. error = xfs_attr_shortform_to_leaf(&args);
  325. if (!error) {
  326. error = xfs_bmap_finish(&args.trans, args.flist,
  327. &committed);
  328. }
  329. if (error) {
  330. ASSERT(committed);
  331. args.trans = NULL;
  332. xfs_bmap_cancel(&flist);
  333. goto out;
  334. }
  335. /*
  336. * bmap_finish() may have committed the last trans and started
  337. * a new one. We need the inode to be in all transactions.
  338. */
  339. if (committed)
  340. xfs_trans_ijoin(args.trans, dp, 0);
  341. /*
  342. * Commit the leaf transformation. We'll need another (linked)
  343. * transaction to add the new attribute to the leaf.
  344. */
  345. error = xfs_trans_roll(&args.trans, dp);
  346. if (error)
  347. goto out;
  348. }
  349. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  350. error = xfs_attr_leaf_addname(&args);
  351. } else {
  352. error = xfs_attr_node_addname(&args);
  353. }
  354. if (error) {
  355. goto out;
  356. }
  357. /*
  358. * If this is a synchronous mount, make sure that the
  359. * transaction goes to disk before returning to the user.
  360. */
  361. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  362. xfs_trans_set_sync(args.trans);
  363. }
  364. if ((flags & ATTR_KERNOTIME) == 0)
  365. xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
  366. /*
  367. * Commit the last in the sequence of transactions.
  368. */
  369. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  370. error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  371. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  372. return(error);
  373. out:
  374. if (args.trans)
  375. xfs_trans_cancel(args.trans,
  376. XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  377. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  378. return(error);
  379. }
  380. int
  381. xfs_attr_set(
  382. xfs_inode_t *dp,
  383. const unsigned char *name,
  384. unsigned char *value,
  385. int valuelen,
  386. int flags)
  387. {
  388. int error;
  389. struct xfs_name xname;
  390. XFS_STATS_INC(xs_attr_set);
  391. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  392. return (EIO);
  393. error = xfs_attr_name_to_xname(&xname, name);
  394. if (error)
  395. return error;
  396. return xfs_attr_set_int(dp, &xname, value, valuelen, flags);
  397. }
  398. /*
  399. * Generic handler routine to remove a name from an attribute list.
  400. * Transitions attribute list from Btree to shortform as necessary.
  401. */
  402. STATIC int
  403. xfs_attr_remove_int(xfs_inode_t *dp, struct xfs_name *name, int flags)
  404. {
  405. xfs_da_args_t args;
  406. xfs_fsblock_t firstblock;
  407. xfs_bmap_free_t flist;
  408. int error;
  409. xfs_mount_t *mp = dp->i_mount;
  410. /*
  411. * Fill in the arg structure for this request.
  412. */
  413. memset((char *)&args, 0, sizeof(args));
  414. args.name = name->name;
  415. args.namelen = name->len;
  416. args.flags = flags;
  417. args.hashval = xfs_da_hashname(args.name, args.namelen);
  418. args.dp = dp;
  419. args.firstblock = &firstblock;
  420. args.flist = &flist;
  421. args.total = 0;
  422. args.whichfork = XFS_ATTR_FORK;
  423. /*
  424. * we have no control over the attribute names that userspace passes us
  425. * to remove, so we have to allow the name lookup prior to attribute
  426. * removal to fail.
  427. */
  428. args.op_flags = XFS_DA_OP_OKNOENT;
  429. /*
  430. * Attach the dquots to the inode.
  431. */
  432. error = xfs_qm_dqattach(dp, 0);
  433. if (error)
  434. return error;
  435. /*
  436. * Start our first transaction of the day.
  437. *
  438. * All future transactions during this code must be "chained" off
  439. * this one via the trans_dup() call. All transactions will contain
  440. * the inode, and the inode will always be marked with trans_ihold().
  441. * Since the inode will be locked in all transactions, we must log
  442. * the inode in every transaction to let it float upward through
  443. * the log.
  444. */
  445. args.trans = xfs_trans_alloc(mp, XFS_TRANS_ATTR_RM);
  446. /*
  447. * Root fork attributes can use reserved data blocks for this
  448. * operation if necessary
  449. */
  450. if (flags & ATTR_ROOT)
  451. args.trans->t_flags |= XFS_TRANS_RESERVE;
  452. if ((error = xfs_trans_reserve(args.trans,
  453. XFS_ATTRRM_SPACE_RES(mp),
  454. XFS_ATTRRM_LOG_RES(mp),
  455. 0, XFS_TRANS_PERM_LOG_RES,
  456. XFS_ATTRRM_LOG_COUNT))) {
  457. xfs_trans_cancel(args.trans, 0);
  458. return(error);
  459. }
  460. xfs_ilock(dp, XFS_ILOCK_EXCL);
  461. /*
  462. * No need to make quota reservations here. We expect to release some
  463. * blocks not allocate in the common case.
  464. */
  465. xfs_trans_ijoin(args.trans, dp, 0);
  466. /*
  467. * Decide on what work routines to call based on the inode size.
  468. */
  469. if (!xfs_inode_hasattr(dp)) {
  470. error = XFS_ERROR(ENOATTR);
  471. goto out;
  472. }
  473. if (dp->i_d.di_aformat == XFS_DINODE_FMT_LOCAL) {
  474. ASSERT(dp->i_afp->if_flags & XFS_IFINLINE);
  475. error = xfs_attr_shortform_remove(&args);
  476. if (error) {
  477. goto out;
  478. }
  479. } else if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  480. error = xfs_attr_leaf_removename(&args);
  481. } else {
  482. error = xfs_attr_node_removename(&args);
  483. }
  484. if (error) {
  485. goto out;
  486. }
  487. /*
  488. * If this is a synchronous mount, make sure that the
  489. * transaction goes to disk before returning to the user.
  490. */
  491. if (mp->m_flags & XFS_MOUNT_WSYNC) {
  492. xfs_trans_set_sync(args.trans);
  493. }
  494. if ((flags & ATTR_KERNOTIME) == 0)
  495. xfs_trans_ichgtime(args.trans, dp, XFS_ICHGTIME_CHG);
  496. /*
  497. * Commit the last in the sequence of transactions.
  498. */
  499. xfs_trans_log_inode(args.trans, dp, XFS_ILOG_CORE);
  500. error = xfs_trans_commit(args.trans, XFS_TRANS_RELEASE_LOG_RES);
  501. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  502. return(error);
  503. out:
  504. if (args.trans)
  505. xfs_trans_cancel(args.trans,
  506. XFS_TRANS_RELEASE_LOG_RES|XFS_TRANS_ABORT);
  507. xfs_iunlock(dp, XFS_ILOCK_EXCL);
  508. return(error);
  509. }
  510. int
  511. xfs_attr_remove(
  512. xfs_inode_t *dp,
  513. const unsigned char *name,
  514. int flags)
  515. {
  516. int error;
  517. struct xfs_name xname;
  518. XFS_STATS_INC(xs_attr_remove);
  519. if (XFS_FORCED_SHUTDOWN(dp->i_mount))
  520. return (EIO);
  521. error = xfs_attr_name_to_xname(&xname, name);
  522. if (error)
  523. return error;
  524. xfs_ilock(dp, XFS_ILOCK_SHARED);
  525. if (!xfs_inode_hasattr(dp)) {
  526. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  527. return XFS_ERROR(ENOATTR);
  528. }
  529. xfs_iunlock(dp, XFS_ILOCK_SHARED);
  530. return xfs_attr_remove_int(dp, &xname, flags);
  531. }
  532. /*========================================================================
  533. * External routines when attribute list is inside the inode
  534. *========================================================================*/
  535. /*
  536. * Add a name to the shortform attribute list structure
  537. * This is the external routine.
  538. */
  539. STATIC int
  540. xfs_attr_shortform_addname(xfs_da_args_t *args)
  541. {
  542. int newsize, forkoff, retval;
  543. trace_xfs_attr_sf_addname(args);
  544. retval = xfs_attr_shortform_lookup(args);
  545. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  546. return(retval);
  547. } else if (retval == EEXIST) {
  548. if (args->flags & ATTR_CREATE)
  549. return(retval);
  550. retval = xfs_attr_shortform_remove(args);
  551. ASSERT(retval == 0);
  552. }
  553. if (args->namelen >= XFS_ATTR_SF_ENTSIZE_MAX ||
  554. args->valuelen >= XFS_ATTR_SF_ENTSIZE_MAX)
  555. return(XFS_ERROR(ENOSPC));
  556. newsize = XFS_ATTR_SF_TOTSIZE(args->dp);
  557. newsize += XFS_ATTR_SF_ENTSIZE_BYNAME(args->namelen, args->valuelen);
  558. forkoff = xfs_attr_shortform_bytesfit(args->dp, newsize);
  559. if (!forkoff)
  560. return(XFS_ERROR(ENOSPC));
  561. xfs_attr_shortform_add(args, forkoff);
  562. return(0);
  563. }
  564. /*========================================================================
  565. * External routines when attribute list is one block
  566. *========================================================================*/
  567. /*
  568. * Add a name to the leaf attribute list structure
  569. *
  570. * This leaf block cannot have a "remote" value, we only call this routine
  571. * if bmap_one_block() says there is only one block (ie: no remote blks).
  572. */
  573. STATIC int
  574. xfs_attr_leaf_addname(xfs_da_args_t *args)
  575. {
  576. xfs_inode_t *dp;
  577. struct xfs_buf *bp;
  578. int retval, error, committed, forkoff;
  579. trace_xfs_attr_leaf_addname(args);
  580. /*
  581. * Read the (only) block in the attribute list in.
  582. */
  583. dp = args->dp;
  584. args->blkno = 0;
  585. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  586. if (error)
  587. return error;
  588. /*
  589. * Look up the given attribute in the leaf block. Figure out if
  590. * the given flags produce an error or call for an atomic rename.
  591. */
  592. retval = xfs_attr3_leaf_lookup_int(bp, args);
  593. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  594. xfs_trans_brelse(args->trans, bp);
  595. return retval;
  596. } else if (retval == EEXIST) {
  597. if (args->flags & ATTR_CREATE) { /* pure create op */
  598. xfs_trans_brelse(args->trans, bp);
  599. return retval;
  600. }
  601. trace_xfs_attr_leaf_replace(args);
  602. args->op_flags |= XFS_DA_OP_RENAME; /* an atomic rename */
  603. args->blkno2 = args->blkno; /* set 2nd entry info*/
  604. args->index2 = args->index;
  605. args->rmtblkno2 = args->rmtblkno;
  606. args->rmtblkcnt2 = args->rmtblkcnt;
  607. }
  608. /*
  609. * Add the attribute to the leaf block, transitioning to a Btree
  610. * if required.
  611. */
  612. retval = xfs_attr3_leaf_add(bp, args);
  613. if (retval == ENOSPC) {
  614. /*
  615. * Promote the attribute list to the Btree format, then
  616. * Commit that transaction so that the node_addname() call
  617. * can manage its own transactions.
  618. */
  619. xfs_bmap_init(args->flist, args->firstblock);
  620. error = xfs_attr3_leaf_to_node(args);
  621. if (!error) {
  622. error = xfs_bmap_finish(&args->trans, args->flist,
  623. &committed);
  624. }
  625. if (error) {
  626. ASSERT(committed);
  627. args->trans = NULL;
  628. xfs_bmap_cancel(args->flist);
  629. return(error);
  630. }
  631. /*
  632. * bmap_finish() may have committed the last trans and started
  633. * a new one. We need the inode to be in all transactions.
  634. */
  635. if (committed)
  636. xfs_trans_ijoin(args->trans, dp, 0);
  637. /*
  638. * Commit the current trans (including the inode) and start
  639. * a new one.
  640. */
  641. error = xfs_trans_roll(&args->trans, dp);
  642. if (error)
  643. return (error);
  644. /*
  645. * Fob the whole rest of the problem off on the Btree code.
  646. */
  647. error = xfs_attr_node_addname(args);
  648. return(error);
  649. }
  650. /*
  651. * Commit the transaction that added the attr name so that
  652. * later routines can manage their own transactions.
  653. */
  654. error = xfs_trans_roll(&args->trans, dp);
  655. if (error)
  656. return (error);
  657. /*
  658. * If there was an out-of-line value, allocate the blocks we
  659. * identified for its storage and copy the value. This is done
  660. * after we create the attribute so that we don't overflow the
  661. * maximum size of a transaction and/or hit a deadlock.
  662. */
  663. if (args->rmtblkno > 0) {
  664. error = xfs_attr_rmtval_set(args);
  665. if (error)
  666. return(error);
  667. }
  668. /*
  669. * If this is an atomic rename operation, we must "flip" the
  670. * incomplete flags on the "new" and "old" attribute/value pairs
  671. * so that one disappears and one appears atomically. Then we
  672. * must remove the "old" attribute/value pair.
  673. */
  674. if (args->op_flags & XFS_DA_OP_RENAME) {
  675. /*
  676. * In a separate transaction, set the incomplete flag on the
  677. * "old" attr and clear the incomplete flag on the "new" attr.
  678. */
  679. error = xfs_attr3_leaf_flipflags(args);
  680. if (error)
  681. return(error);
  682. /*
  683. * Dismantle the "old" attribute/value pair by removing
  684. * a "remote" value (if it exists).
  685. */
  686. args->index = args->index2;
  687. args->blkno = args->blkno2;
  688. args->rmtblkno = args->rmtblkno2;
  689. args->rmtblkcnt = args->rmtblkcnt2;
  690. if (args->rmtblkno) {
  691. error = xfs_attr_rmtval_remove(args);
  692. if (error)
  693. return(error);
  694. }
  695. /*
  696. * Read in the block containing the "old" attr, then
  697. * remove the "old" attr from that block (neat, huh!)
  698. */
  699. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno,
  700. -1, &bp);
  701. if (error)
  702. return error;
  703. xfs_attr3_leaf_remove(bp, args);
  704. /*
  705. * If the result is small enough, shrink it all into the inode.
  706. */
  707. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  708. xfs_bmap_init(args->flist, args->firstblock);
  709. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  710. /* bp is gone due to xfs_da_shrink_inode */
  711. if (!error) {
  712. error = xfs_bmap_finish(&args->trans,
  713. args->flist,
  714. &committed);
  715. }
  716. if (error) {
  717. ASSERT(committed);
  718. args->trans = NULL;
  719. xfs_bmap_cancel(args->flist);
  720. return(error);
  721. }
  722. /*
  723. * bmap_finish() may have committed the last trans
  724. * and started a new one. We need the inode to be
  725. * in all transactions.
  726. */
  727. if (committed)
  728. xfs_trans_ijoin(args->trans, dp, 0);
  729. }
  730. /*
  731. * Commit the remove and start the next trans in series.
  732. */
  733. error = xfs_trans_roll(&args->trans, dp);
  734. } else if (args->rmtblkno > 0) {
  735. /*
  736. * Added a "remote" value, just clear the incomplete flag.
  737. */
  738. error = xfs_attr3_leaf_clearflag(args);
  739. }
  740. return error;
  741. }
  742. /*
  743. * Remove a name from the leaf attribute list structure
  744. *
  745. * This leaf block cannot have a "remote" value, we only call this routine
  746. * if bmap_one_block() says there is only one block (ie: no remote blks).
  747. */
  748. STATIC int
  749. xfs_attr_leaf_removename(xfs_da_args_t *args)
  750. {
  751. xfs_inode_t *dp;
  752. struct xfs_buf *bp;
  753. int error, committed, forkoff;
  754. trace_xfs_attr_leaf_removename(args);
  755. /*
  756. * Remove the attribute.
  757. */
  758. dp = args->dp;
  759. args->blkno = 0;
  760. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  761. if (error)
  762. return error;
  763. error = xfs_attr3_leaf_lookup_int(bp, args);
  764. if (error == ENOATTR) {
  765. xfs_trans_brelse(args->trans, bp);
  766. return error;
  767. }
  768. xfs_attr3_leaf_remove(bp, args);
  769. /*
  770. * If the result is small enough, shrink it all into the inode.
  771. */
  772. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  773. xfs_bmap_init(args->flist, args->firstblock);
  774. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  775. /* bp is gone due to xfs_da_shrink_inode */
  776. if (!error) {
  777. error = xfs_bmap_finish(&args->trans, args->flist,
  778. &committed);
  779. }
  780. if (error) {
  781. ASSERT(committed);
  782. args->trans = NULL;
  783. xfs_bmap_cancel(args->flist);
  784. return error;
  785. }
  786. /*
  787. * bmap_finish() may have committed the last trans and started
  788. * a new one. We need the inode to be in all transactions.
  789. */
  790. if (committed)
  791. xfs_trans_ijoin(args->trans, dp, 0);
  792. }
  793. return 0;
  794. }
  795. /*
  796. * Look up a name in a leaf attribute list structure.
  797. *
  798. * This leaf block cannot have a "remote" value, we only call this routine
  799. * if bmap_one_block() says there is only one block (ie: no remote blks).
  800. */
  801. STATIC int
  802. xfs_attr_leaf_get(xfs_da_args_t *args)
  803. {
  804. struct xfs_buf *bp;
  805. int error;
  806. trace_xfs_attr_leaf_get(args);
  807. args->blkno = 0;
  808. error = xfs_attr3_leaf_read(args->trans, args->dp, args->blkno, -1, &bp);
  809. if (error)
  810. return error;
  811. error = xfs_attr3_leaf_lookup_int(bp, args);
  812. if (error != EEXIST) {
  813. xfs_trans_brelse(args->trans, bp);
  814. return error;
  815. }
  816. error = xfs_attr3_leaf_getvalue(bp, args);
  817. xfs_trans_brelse(args->trans, bp);
  818. if (!error && (args->rmtblkno > 0) && !(args->flags & ATTR_KERNOVAL)) {
  819. error = xfs_attr_rmtval_get(args);
  820. }
  821. return error;
  822. }
  823. /*========================================================================
  824. * External routines when attribute list size > XFS_LBSIZE(mp).
  825. *========================================================================*/
  826. /*
  827. * Add a name to a Btree-format attribute list.
  828. *
  829. * This will involve walking down the Btree, and may involve splitting
  830. * leaf nodes and even splitting intermediate nodes up to and including
  831. * the root node (a special case of an intermediate node).
  832. *
  833. * "Remote" attribute values confuse the issue and atomic rename operations
  834. * add a whole extra layer of confusion on top of that.
  835. */
  836. STATIC int
  837. xfs_attr_node_addname(xfs_da_args_t *args)
  838. {
  839. xfs_da_state_t *state;
  840. xfs_da_state_blk_t *blk;
  841. xfs_inode_t *dp;
  842. xfs_mount_t *mp;
  843. int committed, retval, error;
  844. trace_xfs_attr_node_addname(args);
  845. /*
  846. * Fill in bucket of arguments/results/context to carry around.
  847. */
  848. dp = args->dp;
  849. mp = dp->i_mount;
  850. restart:
  851. state = xfs_da_state_alloc();
  852. state->args = args;
  853. state->mp = mp;
  854. state->blocksize = state->mp->m_sb.sb_blocksize;
  855. state->node_ents = state->mp->m_attr_node_ents;
  856. /*
  857. * Search to see if name already exists, and get back a pointer
  858. * to where it should go.
  859. */
  860. error = xfs_da3_node_lookup_int(state, &retval);
  861. if (error)
  862. goto out;
  863. blk = &state->path.blk[ state->path.active-1 ];
  864. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  865. if ((args->flags & ATTR_REPLACE) && (retval == ENOATTR)) {
  866. goto out;
  867. } else if (retval == EEXIST) {
  868. if (args->flags & ATTR_CREATE)
  869. goto out;
  870. trace_xfs_attr_node_replace(args);
  871. args->op_flags |= XFS_DA_OP_RENAME; /* atomic rename op */
  872. args->blkno2 = args->blkno; /* set 2nd entry info*/
  873. args->index2 = args->index;
  874. args->rmtblkno2 = args->rmtblkno;
  875. args->rmtblkcnt2 = args->rmtblkcnt;
  876. args->rmtblkno = 0;
  877. args->rmtblkcnt = 0;
  878. }
  879. retval = xfs_attr3_leaf_add(blk->bp, state->args);
  880. if (retval == ENOSPC) {
  881. if (state->path.active == 1) {
  882. /*
  883. * Its really a single leaf node, but it had
  884. * out-of-line values so it looked like it *might*
  885. * have been a b-tree.
  886. */
  887. xfs_da_state_free(state);
  888. xfs_bmap_init(args->flist, args->firstblock);
  889. error = xfs_attr3_leaf_to_node(args);
  890. if (!error) {
  891. error = xfs_bmap_finish(&args->trans,
  892. args->flist,
  893. &committed);
  894. }
  895. if (error) {
  896. ASSERT(committed);
  897. args->trans = NULL;
  898. xfs_bmap_cancel(args->flist);
  899. goto out;
  900. }
  901. /*
  902. * bmap_finish() may have committed the last trans
  903. * and started a new one. We need the inode to be
  904. * in all transactions.
  905. */
  906. if (committed)
  907. xfs_trans_ijoin(args->trans, dp, 0);
  908. /*
  909. * Commit the node conversion and start the next
  910. * trans in the chain.
  911. */
  912. error = xfs_trans_roll(&args->trans, dp);
  913. if (error)
  914. goto out;
  915. goto restart;
  916. }
  917. /*
  918. * Split as many Btree elements as required.
  919. * This code tracks the new and old attr's location
  920. * in the index/blkno/rmtblkno/rmtblkcnt fields and
  921. * in the index2/blkno2/rmtblkno2/rmtblkcnt2 fields.
  922. */
  923. xfs_bmap_init(args->flist, args->firstblock);
  924. error = xfs_da3_split(state);
  925. if (!error) {
  926. error = xfs_bmap_finish(&args->trans, args->flist,
  927. &committed);
  928. }
  929. if (error) {
  930. ASSERT(committed);
  931. args->trans = NULL;
  932. xfs_bmap_cancel(args->flist);
  933. goto out;
  934. }
  935. /*
  936. * bmap_finish() may have committed the last trans and started
  937. * a new one. We need the inode to be in all transactions.
  938. */
  939. if (committed)
  940. xfs_trans_ijoin(args->trans, dp, 0);
  941. } else {
  942. /*
  943. * Addition succeeded, update Btree hashvals.
  944. */
  945. xfs_da3_fixhashpath(state, &state->path);
  946. }
  947. /*
  948. * Kill the state structure, we're done with it and need to
  949. * allow the buffers to come back later.
  950. */
  951. xfs_da_state_free(state);
  952. state = NULL;
  953. /*
  954. * Commit the leaf addition or btree split and start the next
  955. * trans in the chain.
  956. */
  957. error = xfs_trans_roll(&args->trans, dp);
  958. if (error)
  959. goto out;
  960. /*
  961. * If there was an out-of-line value, allocate the blocks we
  962. * identified for its storage and copy the value. This is done
  963. * after we create the attribute so that we don't overflow the
  964. * maximum size of a transaction and/or hit a deadlock.
  965. */
  966. if (args->rmtblkno > 0) {
  967. error = xfs_attr_rmtval_set(args);
  968. if (error)
  969. return(error);
  970. }
  971. /*
  972. * If this is an atomic rename operation, we must "flip" the
  973. * incomplete flags on the "new" and "old" attribute/value pairs
  974. * so that one disappears and one appears atomically. Then we
  975. * must remove the "old" attribute/value pair.
  976. */
  977. if (args->op_flags & XFS_DA_OP_RENAME) {
  978. /*
  979. * In a separate transaction, set the incomplete flag on the
  980. * "old" attr and clear the incomplete flag on the "new" attr.
  981. */
  982. error = xfs_attr3_leaf_flipflags(args);
  983. if (error)
  984. goto out;
  985. /*
  986. * Dismantle the "old" attribute/value pair by removing
  987. * a "remote" value (if it exists).
  988. */
  989. args->index = args->index2;
  990. args->blkno = args->blkno2;
  991. args->rmtblkno = args->rmtblkno2;
  992. args->rmtblkcnt = args->rmtblkcnt2;
  993. if (args->rmtblkno) {
  994. error = xfs_attr_rmtval_remove(args);
  995. if (error)
  996. return(error);
  997. }
  998. /*
  999. * Re-find the "old" attribute entry after any split ops.
  1000. * The INCOMPLETE flag means that we will find the "old"
  1001. * attr, not the "new" one.
  1002. */
  1003. args->flags |= XFS_ATTR_INCOMPLETE;
  1004. state = xfs_da_state_alloc();
  1005. state->args = args;
  1006. state->mp = mp;
  1007. state->blocksize = state->mp->m_sb.sb_blocksize;
  1008. state->node_ents = state->mp->m_attr_node_ents;
  1009. state->inleaf = 0;
  1010. error = xfs_da3_node_lookup_int(state, &retval);
  1011. if (error)
  1012. goto out;
  1013. /*
  1014. * Remove the name and update the hashvals in the tree.
  1015. */
  1016. blk = &state->path.blk[ state->path.active-1 ];
  1017. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1018. error = xfs_attr3_leaf_remove(blk->bp, args);
  1019. xfs_da3_fixhashpath(state, &state->path);
  1020. /*
  1021. * Check to see if the tree needs to be collapsed.
  1022. */
  1023. if (retval && (state->path.active > 1)) {
  1024. xfs_bmap_init(args->flist, args->firstblock);
  1025. error = xfs_da3_join(state);
  1026. if (!error) {
  1027. error = xfs_bmap_finish(&args->trans,
  1028. args->flist,
  1029. &committed);
  1030. }
  1031. if (error) {
  1032. ASSERT(committed);
  1033. args->trans = NULL;
  1034. xfs_bmap_cancel(args->flist);
  1035. goto out;
  1036. }
  1037. /*
  1038. * bmap_finish() may have committed the last trans
  1039. * and started a new one. We need the inode to be
  1040. * in all transactions.
  1041. */
  1042. if (committed)
  1043. xfs_trans_ijoin(args->trans, dp, 0);
  1044. }
  1045. /*
  1046. * Commit and start the next trans in the chain.
  1047. */
  1048. error = xfs_trans_roll(&args->trans, dp);
  1049. if (error)
  1050. goto out;
  1051. } else if (args->rmtblkno > 0) {
  1052. /*
  1053. * Added a "remote" value, just clear the incomplete flag.
  1054. */
  1055. error = xfs_attr3_leaf_clearflag(args);
  1056. if (error)
  1057. goto out;
  1058. }
  1059. retval = error = 0;
  1060. out:
  1061. if (state)
  1062. xfs_da_state_free(state);
  1063. if (error)
  1064. return(error);
  1065. return(retval);
  1066. }
  1067. /*
  1068. * Remove a name from a B-tree attribute list.
  1069. *
  1070. * This will involve walking down the Btree, and may involve joining
  1071. * leaf nodes and even joining intermediate nodes up to and including
  1072. * the root node (a special case of an intermediate node).
  1073. */
  1074. STATIC int
  1075. xfs_attr_node_removename(xfs_da_args_t *args)
  1076. {
  1077. xfs_da_state_t *state;
  1078. xfs_da_state_blk_t *blk;
  1079. xfs_inode_t *dp;
  1080. struct xfs_buf *bp;
  1081. int retval, error, committed, forkoff;
  1082. trace_xfs_attr_node_removename(args);
  1083. /*
  1084. * Tie a string around our finger to remind us where we are.
  1085. */
  1086. dp = args->dp;
  1087. state = xfs_da_state_alloc();
  1088. state->args = args;
  1089. state->mp = dp->i_mount;
  1090. state->blocksize = state->mp->m_sb.sb_blocksize;
  1091. state->node_ents = state->mp->m_attr_node_ents;
  1092. /*
  1093. * Search to see if name exists, and get back a pointer to it.
  1094. */
  1095. error = xfs_da3_node_lookup_int(state, &retval);
  1096. if (error || (retval != EEXIST)) {
  1097. if (error == 0)
  1098. error = retval;
  1099. goto out;
  1100. }
  1101. /*
  1102. * If there is an out-of-line value, de-allocate the blocks.
  1103. * This is done before we remove the attribute so that we don't
  1104. * overflow the maximum size of a transaction and/or hit a deadlock.
  1105. */
  1106. blk = &state->path.blk[ state->path.active-1 ];
  1107. ASSERT(blk->bp != NULL);
  1108. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1109. if (args->rmtblkno > 0) {
  1110. /*
  1111. * Fill in disk block numbers in the state structure
  1112. * so that we can get the buffers back after we commit
  1113. * several transactions in the following calls.
  1114. */
  1115. error = xfs_attr_fillstate(state);
  1116. if (error)
  1117. goto out;
  1118. /*
  1119. * Mark the attribute as INCOMPLETE, then bunmapi() the
  1120. * remote value.
  1121. */
  1122. error = xfs_attr3_leaf_setflag(args);
  1123. if (error)
  1124. goto out;
  1125. error = xfs_attr_rmtval_remove(args);
  1126. if (error)
  1127. goto out;
  1128. /*
  1129. * Refill the state structure with buffers, the prior calls
  1130. * released our buffers.
  1131. */
  1132. error = xfs_attr_refillstate(state);
  1133. if (error)
  1134. goto out;
  1135. }
  1136. /*
  1137. * Remove the name and update the hashvals in the tree.
  1138. */
  1139. blk = &state->path.blk[ state->path.active-1 ];
  1140. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1141. retval = xfs_attr3_leaf_remove(blk->bp, args);
  1142. xfs_da3_fixhashpath(state, &state->path);
  1143. /*
  1144. * Check to see if the tree needs to be collapsed.
  1145. */
  1146. if (retval && (state->path.active > 1)) {
  1147. xfs_bmap_init(args->flist, args->firstblock);
  1148. error = xfs_da3_join(state);
  1149. if (!error) {
  1150. error = xfs_bmap_finish(&args->trans, args->flist,
  1151. &committed);
  1152. }
  1153. if (error) {
  1154. ASSERT(committed);
  1155. args->trans = NULL;
  1156. xfs_bmap_cancel(args->flist);
  1157. goto out;
  1158. }
  1159. /*
  1160. * bmap_finish() may have committed the last trans and started
  1161. * a new one. We need the inode to be in all transactions.
  1162. */
  1163. if (committed)
  1164. xfs_trans_ijoin(args->trans, dp, 0);
  1165. /*
  1166. * Commit the Btree join operation and start a new trans.
  1167. */
  1168. error = xfs_trans_roll(&args->trans, dp);
  1169. if (error)
  1170. goto out;
  1171. }
  1172. /*
  1173. * If the result is small enough, push it all into the inode.
  1174. */
  1175. if (xfs_bmap_one_block(dp, XFS_ATTR_FORK)) {
  1176. /*
  1177. * Have to get rid of the copy of this dabuf in the state.
  1178. */
  1179. ASSERT(state->path.active == 1);
  1180. ASSERT(state->path.blk[0].bp);
  1181. state->path.blk[0].bp = NULL;
  1182. error = xfs_attr3_leaf_read(args->trans, args->dp, 0, -1, &bp);
  1183. if (error)
  1184. goto out;
  1185. if ((forkoff = xfs_attr_shortform_allfit(bp, dp))) {
  1186. xfs_bmap_init(args->flist, args->firstblock);
  1187. error = xfs_attr3_leaf_to_shortform(bp, args, forkoff);
  1188. /* bp is gone due to xfs_da_shrink_inode */
  1189. if (!error) {
  1190. error = xfs_bmap_finish(&args->trans,
  1191. 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
  1202. * and started a new one. We need the inode to be
  1203. * in all transactions.
  1204. */
  1205. if (committed)
  1206. xfs_trans_ijoin(args->trans, dp, 0);
  1207. } else
  1208. xfs_trans_brelse(args->trans, bp);
  1209. }
  1210. error = 0;
  1211. out:
  1212. xfs_da_state_free(state);
  1213. return(error);
  1214. }
  1215. /*
  1216. * Fill in the disk block numbers in the state structure for the buffers
  1217. * that are attached to the state structure.
  1218. * This is done so that we can quickly reattach ourselves to those buffers
  1219. * after some set of transaction commits have released these buffers.
  1220. */
  1221. STATIC int
  1222. xfs_attr_fillstate(xfs_da_state_t *state)
  1223. {
  1224. xfs_da_state_path_t *path;
  1225. xfs_da_state_blk_t *blk;
  1226. int level;
  1227. trace_xfs_attr_fillstate(state->args);
  1228. /*
  1229. * Roll down the "path" in the state structure, storing the on-disk
  1230. * block number for those buffers in the "path".
  1231. */
  1232. path = &state->path;
  1233. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1234. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1235. if (blk->bp) {
  1236. blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
  1237. blk->bp = NULL;
  1238. } else {
  1239. blk->disk_blkno = 0;
  1240. }
  1241. }
  1242. /*
  1243. * Roll down the "altpath" in the state structure, storing the on-disk
  1244. * block number for those buffers in the "altpath".
  1245. */
  1246. path = &state->altpath;
  1247. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1248. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1249. if (blk->bp) {
  1250. blk->disk_blkno = XFS_BUF_ADDR(blk->bp);
  1251. blk->bp = NULL;
  1252. } else {
  1253. blk->disk_blkno = 0;
  1254. }
  1255. }
  1256. return(0);
  1257. }
  1258. /*
  1259. * Reattach the buffers to the state structure based on the disk block
  1260. * numbers stored in the state structure.
  1261. * This is done after some set of transaction commits have released those
  1262. * buffers from our grip.
  1263. */
  1264. STATIC int
  1265. xfs_attr_refillstate(xfs_da_state_t *state)
  1266. {
  1267. xfs_da_state_path_t *path;
  1268. xfs_da_state_blk_t *blk;
  1269. int level, error;
  1270. trace_xfs_attr_refillstate(state->args);
  1271. /*
  1272. * Roll down the "path" in the state structure, storing the on-disk
  1273. * block number for those buffers in the "path".
  1274. */
  1275. path = &state->path;
  1276. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1277. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1278. if (blk->disk_blkno) {
  1279. error = xfs_da3_node_read(state->args->trans,
  1280. state->args->dp,
  1281. blk->blkno, blk->disk_blkno,
  1282. &blk->bp, XFS_ATTR_FORK);
  1283. if (error)
  1284. return(error);
  1285. } else {
  1286. blk->bp = NULL;
  1287. }
  1288. }
  1289. /*
  1290. * Roll down the "altpath" in the state structure, storing the on-disk
  1291. * block number for those buffers in the "altpath".
  1292. */
  1293. path = &state->altpath;
  1294. ASSERT((path->active >= 0) && (path->active < XFS_DA_NODE_MAXDEPTH));
  1295. for (blk = path->blk, level = 0; level < path->active; blk++, level++) {
  1296. if (blk->disk_blkno) {
  1297. error = xfs_da3_node_read(state->args->trans,
  1298. state->args->dp,
  1299. blk->blkno, blk->disk_blkno,
  1300. &blk->bp, XFS_ATTR_FORK);
  1301. if (error)
  1302. return(error);
  1303. } else {
  1304. blk->bp = NULL;
  1305. }
  1306. }
  1307. return(0);
  1308. }
  1309. /*
  1310. * Look up a filename in a node attribute list.
  1311. *
  1312. * This routine gets called for any attribute fork that has more than one
  1313. * block, ie: both true Btree attr lists and for single-leaf-blocks with
  1314. * "remote" values taking up more blocks.
  1315. */
  1316. STATIC int
  1317. xfs_attr_node_get(xfs_da_args_t *args)
  1318. {
  1319. xfs_da_state_t *state;
  1320. xfs_da_state_blk_t *blk;
  1321. int error, retval;
  1322. int i;
  1323. trace_xfs_attr_node_get(args);
  1324. state = xfs_da_state_alloc();
  1325. state->args = args;
  1326. state->mp = args->dp->i_mount;
  1327. state->blocksize = state->mp->m_sb.sb_blocksize;
  1328. state->node_ents = state->mp->m_attr_node_ents;
  1329. /*
  1330. * Search to see if name exists, and get back a pointer to it.
  1331. */
  1332. error = xfs_da3_node_lookup_int(state, &retval);
  1333. if (error) {
  1334. retval = error;
  1335. } else if (retval == EEXIST) {
  1336. blk = &state->path.blk[ state->path.active-1 ];
  1337. ASSERT(blk->bp != NULL);
  1338. ASSERT(blk->magic == XFS_ATTR_LEAF_MAGIC);
  1339. /*
  1340. * Get the value, local or "remote"
  1341. */
  1342. retval = xfs_attr3_leaf_getvalue(blk->bp, args);
  1343. if (!retval && (args->rmtblkno > 0)
  1344. && !(args->flags & ATTR_KERNOVAL)) {
  1345. retval = xfs_attr_rmtval_get(args);
  1346. }
  1347. }
  1348. /*
  1349. * If not in a transaction, we have to release all the buffers.
  1350. */
  1351. for (i = 0; i < state->path.active; i++) {
  1352. xfs_trans_brelse(args->trans, state->path.blk[i].bp);
  1353. state->path.blk[i].bp = NULL;
  1354. }
  1355. xfs_da_state_free(state);
  1356. return(retval);
  1357. }