xfs_attr.c 36 KB

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